Fix bug #11367 with assertion violation during vertical motion in egg.el.
[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 pop_it (struct it *);
843 static void sync_frame_with_window_matrix_rows (struct window *);
844 static void select_frame_for_redisplay (Lisp_Object);
845 static void redisplay_internal (void);
846 static int echo_area_display (int);
847 static void redisplay_windows (Lisp_Object);
848 static void redisplay_window (Lisp_Object, int);
849 static Lisp_Object redisplay_window_error (Lisp_Object);
850 static Lisp_Object redisplay_window_0 (Lisp_Object);
851 static Lisp_Object redisplay_window_1 (Lisp_Object);
852 static int set_cursor_from_row (struct window *, struct glyph_row *,
853 struct glyph_matrix *, EMACS_INT, EMACS_INT,
854 int, int);
855 static int update_menu_bar (struct frame *, int, int);
856 static int try_window_reusing_current_matrix (struct window *);
857 static int try_window_id (struct window *);
858 static int display_line (struct it *);
859 static int display_mode_lines (struct window *);
860 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
861 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
862 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
863 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
864 static void display_menu_bar (struct window *);
865 static EMACS_INT display_count_lines (EMACS_INT, EMACS_INT, EMACS_INT,
866 EMACS_INT *);
867 static int display_string (const char *, Lisp_Object, Lisp_Object,
868 EMACS_INT, EMACS_INT, struct it *, int, int, int, int);
869 static void compute_line_metrics (struct it *);
870 static void run_redisplay_end_trigger_hook (struct it *);
871 static int get_overlay_strings (struct it *, EMACS_INT);
872 static int get_overlay_strings_1 (struct it *, EMACS_INT, int);
873 static void next_overlay_string (struct it *);
874 static void reseat (struct it *, struct text_pos, int);
875 static void reseat_1 (struct it *, struct text_pos, int);
876 static void back_to_previous_visible_line_start (struct it *);
877 void reseat_at_previous_visible_line_start (struct it *);
878 static void reseat_at_next_visible_line_start (struct it *, int);
879 static int next_element_from_ellipsis (struct it *);
880 static int next_element_from_display_vector (struct it *);
881 static int next_element_from_string (struct it *);
882 static int next_element_from_c_string (struct it *);
883 static int next_element_from_buffer (struct it *);
884 static int next_element_from_composition (struct it *);
885 static int next_element_from_image (struct it *);
886 static int next_element_from_stretch (struct it *);
887 static void load_overlay_strings (struct it *, EMACS_INT);
888 static int init_from_display_pos (struct it *, struct window *,
889 struct display_pos *);
890 static void reseat_to_string (struct it *, const char *,
891 Lisp_Object, EMACS_INT, EMACS_INT, int, int);
892 static int get_next_display_element (struct it *);
893 static enum move_it_result
894 move_it_in_display_line_to (struct it *, EMACS_INT, int,
895 enum move_operation_enum);
896 void move_it_vertically_backward (struct it *, int);
897 static void init_to_row_start (struct it *, struct window *,
898 struct glyph_row *);
899 static int init_to_row_end (struct it *, struct window *,
900 struct glyph_row *);
901 static void back_to_previous_line_start (struct it *);
902 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
903 static struct text_pos string_pos_nchars_ahead (struct text_pos,
904 Lisp_Object, EMACS_INT);
905 static struct text_pos string_pos (EMACS_INT, Lisp_Object);
906 static struct text_pos c_string_pos (EMACS_INT, const char *, int);
907 static EMACS_INT number_of_chars (const char *, int);
908 static void compute_stop_pos (struct it *);
909 static void compute_string_pos (struct text_pos *, struct text_pos,
910 Lisp_Object);
911 static int face_before_or_after_it_pos (struct it *, int);
912 static EMACS_INT next_overlay_change (EMACS_INT);
913 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
914 Lisp_Object, struct text_pos *, EMACS_INT, int);
915 static int handle_single_display_spec (struct it *, Lisp_Object,
916 Lisp_Object, Lisp_Object,
917 struct text_pos *, EMACS_INT, int, int);
918 static int underlying_face_id (struct it *);
919 static int in_ellipses_for_invisible_text_p (struct display_pos *,
920 struct window *);
921
922 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
923 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
924
925 #ifdef HAVE_WINDOW_SYSTEM
926
927 static void x_consider_frame_title (Lisp_Object);
928 static int tool_bar_lines_needed (struct frame *, int *);
929 static void update_tool_bar (struct frame *, int);
930 static void build_desired_tool_bar_string (struct frame *f);
931 static int redisplay_tool_bar (struct frame *);
932 static void display_tool_bar_line (struct it *, int);
933 static void notice_overwritten_cursor (struct window *,
934 enum glyph_row_area,
935 int, int, int, int);
936 static void append_stretch_glyph (struct it *, Lisp_Object,
937 int, int, int);
938
939
940 #endif /* HAVE_WINDOW_SYSTEM */
941
942 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
943 static int coords_in_mouse_face_p (struct window *, int, int);
944
945
946 \f
947 /***********************************************************************
948 Window display dimensions
949 ***********************************************************************/
950
951 /* Return the bottom boundary y-position for text lines in window W.
952 This is the first y position at which a line cannot start.
953 It is relative to the top of the window.
954
955 This is the height of W minus the height of a mode line, if any. */
956
957 int
958 window_text_bottom_y (struct window *w)
959 {
960 int height = WINDOW_TOTAL_HEIGHT (w);
961
962 if (WINDOW_WANTS_MODELINE_P (w))
963 height -= CURRENT_MODE_LINE_HEIGHT (w);
964 return height;
965 }
966
967 /* Return the pixel width of display area AREA of window W. AREA < 0
968 means return the total width of W, not including fringes to
969 the left and right of the window. */
970
971 int
972 window_box_width (struct window *w, int area)
973 {
974 int cols = XFASTINT (w->total_cols);
975 int pixels = 0;
976
977 if (!w->pseudo_window_p)
978 {
979 cols -= WINDOW_SCROLL_BAR_COLS (w);
980
981 if (area == TEXT_AREA)
982 {
983 if (INTEGERP (w->left_margin_cols))
984 cols -= XFASTINT (w->left_margin_cols);
985 if (INTEGERP (w->right_margin_cols))
986 cols -= XFASTINT (w->right_margin_cols);
987 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
988 }
989 else if (area == LEFT_MARGIN_AREA)
990 {
991 cols = (INTEGERP (w->left_margin_cols)
992 ? XFASTINT (w->left_margin_cols) : 0);
993 pixels = 0;
994 }
995 else if (area == RIGHT_MARGIN_AREA)
996 {
997 cols = (INTEGERP (w->right_margin_cols)
998 ? XFASTINT (w->right_margin_cols) : 0);
999 pixels = 0;
1000 }
1001 }
1002
1003 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1004 }
1005
1006
1007 /* Return the pixel height of the display area of window W, not
1008 including mode lines of W, if any. */
1009
1010 int
1011 window_box_height (struct window *w)
1012 {
1013 struct frame *f = XFRAME (w->frame);
1014 int height = WINDOW_TOTAL_HEIGHT (w);
1015
1016 xassert (height >= 0);
1017
1018 /* Note: the code below that determines the mode-line/header-line
1019 height is essentially the same as that contained in the macro
1020 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1021 the appropriate glyph row has its `mode_line_p' flag set,
1022 and if it doesn't, uses estimate_mode_line_height instead. */
1023
1024 if (WINDOW_WANTS_MODELINE_P (w))
1025 {
1026 struct glyph_row *ml_row
1027 = (w->current_matrix && w->current_matrix->rows
1028 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1029 : 0);
1030 if (ml_row && ml_row->mode_line_p)
1031 height -= ml_row->height;
1032 else
1033 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1034 }
1035
1036 if (WINDOW_WANTS_HEADER_LINE_P (w))
1037 {
1038 struct glyph_row *hl_row
1039 = (w->current_matrix && w->current_matrix->rows
1040 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1041 : 0);
1042 if (hl_row && hl_row->mode_line_p)
1043 height -= hl_row->height;
1044 else
1045 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1046 }
1047
1048 /* With a very small font and a mode-line that's taller than
1049 default, we might end up with a negative height. */
1050 return max (0, height);
1051 }
1052
1053 /* Return the window-relative coordinate of the left edge of display
1054 area AREA of window W. AREA < 0 means return the left edge of the
1055 whole window, to the right of the left fringe of W. */
1056
1057 int
1058 window_box_left_offset (struct window *w, int area)
1059 {
1060 int x;
1061
1062 if (w->pseudo_window_p)
1063 return 0;
1064
1065 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1066
1067 if (area == TEXT_AREA)
1068 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1069 + window_box_width (w, LEFT_MARGIN_AREA));
1070 else if (area == RIGHT_MARGIN_AREA)
1071 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1072 + window_box_width (w, LEFT_MARGIN_AREA)
1073 + window_box_width (w, TEXT_AREA)
1074 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1075 ? 0
1076 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1077 else if (area == LEFT_MARGIN_AREA
1078 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1079 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1080
1081 return x;
1082 }
1083
1084
1085 /* Return the window-relative coordinate of the right edge of display
1086 area AREA of window W. AREA < 0 means return the right edge of the
1087 whole window, to the left of the right fringe of W. */
1088
1089 int
1090 window_box_right_offset (struct window *w, int area)
1091 {
1092 return window_box_left_offset (w, area) + window_box_width (w, area);
1093 }
1094
1095 /* Return the frame-relative coordinate of the left edge of display
1096 area AREA of window W. AREA < 0 means return the left edge of the
1097 whole window, to the right of the left fringe of W. */
1098
1099 int
1100 window_box_left (struct window *w, int area)
1101 {
1102 struct frame *f = XFRAME (w->frame);
1103 int x;
1104
1105 if (w->pseudo_window_p)
1106 return FRAME_INTERNAL_BORDER_WIDTH (f);
1107
1108 x = (WINDOW_LEFT_EDGE_X (w)
1109 + window_box_left_offset (w, area));
1110
1111 return x;
1112 }
1113
1114
1115 /* Return the frame-relative coordinate of the right edge of display
1116 area AREA of window W. AREA < 0 means return the right edge of the
1117 whole window, to the left of the right fringe of W. */
1118
1119 int
1120 window_box_right (struct window *w, int area)
1121 {
1122 return window_box_left (w, area) + window_box_width (w, area);
1123 }
1124
1125 /* Get the bounding box of the display area AREA of window W, without
1126 mode lines, in frame-relative coordinates. AREA < 0 means the
1127 whole window, not including the left and right fringes of
1128 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1129 coordinates of the upper-left corner of the box. Return in
1130 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1131
1132 void
1133 window_box (struct window *w, int area, int *box_x, int *box_y,
1134 int *box_width, int *box_height)
1135 {
1136 if (box_width)
1137 *box_width = window_box_width (w, area);
1138 if (box_height)
1139 *box_height = window_box_height (w);
1140 if (box_x)
1141 *box_x = window_box_left (w, area);
1142 if (box_y)
1143 {
1144 *box_y = WINDOW_TOP_EDGE_Y (w);
1145 if (WINDOW_WANTS_HEADER_LINE_P (w))
1146 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1147 }
1148 }
1149
1150
1151 /* Get the bounding box of the display area AREA of window W, without
1152 mode lines. AREA < 0 means the whole window, not including the
1153 left and right fringe of the window. Return in *TOP_LEFT_X
1154 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1155 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1156 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1157 box. */
1158
1159 static inline void
1160 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1161 int *bottom_right_x, int *bottom_right_y)
1162 {
1163 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1164 bottom_right_y);
1165 *bottom_right_x += *top_left_x;
1166 *bottom_right_y += *top_left_y;
1167 }
1168
1169
1170 \f
1171 /***********************************************************************
1172 Utilities
1173 ***********************************************************************/
1174
1175 /* Return the bottom y-position of the line the iterator IT is in.
1176 This can modify IT's settings. */
1177
1178 int
1179 line_bottom_y (struct it *it)
1180 {
1181 int line_height = it->max_ascent + it->max_descent;
1182 int line_top_y = it->current_y;
1183
1184 if (line_height == 0)
1185 {
1186 if (last_height)
1187 line_height = last_height;
1188 else if (IT_CHARPOS (*it) < ZV)
1189 {
1190 move_it_by_lines (it, 1);
1191 line_height = (it->max_ascent || it->max_descent
1192 ? it->max_ascent + it->max_descent
1193 : last_height);
1194 }
1195 else
1196 {
1197 struct glyph_row *row = it->glyph_row;
1198
1199 /* Use the default character height. */
1200 it->glyph_row = NULL;
1201 it->what = IT_CHARACTER;
1202 it->c = ' ';
1203 it->len = 1;
1204 PRODUCE_GLYPHS (it);
1205 line_height = it->ascent + it->descent;
1206 it->glyph_row = row;
1207 }
1208 }
1209
1210 return line_top_y + line_height;
1211 }
1212
1213 /* Subroutine of pos_visible_p below. Extracts a display string, if
1214 any, from the display spec given as its argument. */
1215 static Lisp_Object
1216 string_from_display_spec (Lisp_Object spec)
1217 {
1218 if (CONSP (spec))
1219 {
1220 while (CONSP (spec))
1221 {
1222 if (STRINGP (XCAR (spec)))
1223 return XCAR (spec);
1224 spec = XCDR (spec);
1225 }
1226 }
1227 else if (VECTORP (spec))
1228 {
1229 ptrdiff_t i;
1230
1231 for (i = 0; i < ASIZE (spec); i++)
1232 {
1233 if (STRINGP (AREF (spec, i)))
1234 return AREF (spec, i);
1235 }
1236 return Qnil;
1237 }
1238
1239 return spec;
1240 }
1241
1242 /* Return 1 if position CHARPOS is visible in window W.
1243 CHARPOS < 0 means return info about WINDOW_END position.
1244 If visible, set *X and *Y to pixel coordinates of top left corner.
1245 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1246 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1247
1248 int
1249 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1250 int *rtop, int *rbot, int *rowh, int *vpos)
1251 {
1252 struct it it;
1253 void *itdata = bidi_shelve_cache ();
1254 struct text_pos top;
1255 int visible_p = 0;
1256 struct buffer *old_buffer = NULL;
1257
1258 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1259 return visible_p;
1260
1261 if (XBUFFER (w->buffer) != current_buffer)
1262 {
1263 old_buffer = current_buffer;
1264 set_buffer_internal_1 (XBUFFER (w->buffer));
1265 }
1266
1267 SET_TEXT_POS_FROM_MARKER (top, w->start);
1268 /* Scrolling a minibuffer window via scroll bar when the echo area
1269 shows long text sometimes resets the minibuffer contents behind
1270 our backs. */
1271 if (CHARPOS (top) > ZV)
1272 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1273
1274 /* Compute exact mode line heights. */
1275 if (WINDOW_WANTS_MODELINE_P (w))
1276 current_mode_line_height
1277 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1278 BVAR (current_buffer, mode_line_format));
1279
1280 if (WINDOW_WANTS_HEADER_LINE_P (w))
1281 current_header_line_height
1282 = display_mode_line (w, HEADER_LINE_FACE_ID,
1283 BVAR (current_buffer, header_line_format));
1284
1285 start_display (&it, w, top);
1286 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1287 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1288
1289 if (charpos >= 0
1290 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1291 && IT_CHARPOS (it) >= charpos)
1292 /* When scanning backwards under bidi iteration, move_it_to
1293 stops at or _before_ CHARPOS, because it stops at or to
1294 the _right_ of the character at CHARPOS. */
1295 || (it.bidi_p && it.bidi_it.scan_dir == -1
1296 && IT_CHARPOS (it) <= charpos)))
1297 {
1298 /* We have reached CHARPOS, or passed it. How the call to
1299 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1300 or covered by a display property, move_it_to stops at the end
1301 of the invisible text, to the right of CHARPOS. (ii) If
1302 CHARPOS is in a display vector, move_it_to stops on its last
1303 glyph. */
1304 int top_x = it.current_x;
1305 int top_y = it.current_y;
1306 enum it_method it_method = it.method;
1307 /* Calling line_bottom_y may change it.method, it.position, etc. */
1308 int bottom_y = (last_height = 0, line_bottom_y (&it));
1309 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1310
1311 if (top_y < window_top_y)
1312 visible_p = bottom_y > window_top_y;
1313 else if (top_y < it.last_visible_y)
1314 visible_p = 1;
1315 if (visible_p)
1316 {
1317 if (it_method == GET_FROM_DISPLAY_VECTOR)
1318 {
1319 /* We stopped on the last glyph of a display vector.
1320 Try and recompute. Hack alert! */
1321 if (charpos < 2 || top.charpos >= charpos)
1322 top_x = it.glyph_row->x;
1323 else
1324 {
1325 struct it it2;
1326 start_display (&it2, w, top);
1327 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1328 get_next_display_element (&it2);
1329 PRODUCE_GLYPHS (&it2);
1330 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1331 || it2.current_x > it2.last_visible_x)
1332 top_x = it.glyph_row->x;
1333 else
1334 {
1335 top_x = it2.current_x;
1336 top_y = it2.current_y;
1337 }
1338 }
1339 }
1340 else if (IT_CHARPOS (it) != charpos)
1341 {
1342 Lisp_Object cpos = make_number (charpos);
1343 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1344 Lisp_Object string = string_from_display_spec (spec);
1345 int newline_in_string = 0;
1346
1347 if (STRINGP (string))
1348 {
1349 const char *s = SSDATA (string);
1350 const char *e = s + SBYTES (string);
1351 while (s < e)
1352 {
1353 if (*s++ == '\n')
1354 {
1355 newline_in_string = 1;
1356 break;
1357 }
1358 }
1359 }
1360 /* The tricky code below is needed because there's a
1361 discrepancy between move_it_to and how we set cursor
1362 when the display line ends in a newline from a
1363 display string. move_it_to will stop _after_ such
1364 display strings, whereas set_cursor_from_row
1365 conspires with cursor_row_p to place the cursor on
1366 the first glyph produced from the display string. */
1367
1368 /* We have overshoot PT because it is covered by a
1369 display property whose value is a string. If the
1370 string includes embedded newlines, we are also in the
1371 wrong display line. Backtrack to the correct line,
1372 where the display string begins. */
1373 if (newline_in_string)
1374 {
1375 Lisp_Object startpos, endpos;
1376 EMACS_INT start, end;
1377 struct it it3;
1378 int it3_moved;
1379
1380 /* Find the first and the last buffer positions
1381 covered by the display string. */
1382 endpos =
1383 Fnext_single_char_property_change (cpos, Qdisplay,
1384 Qnil, Qnil);
1385 startpos =
1386 Fprevious_single_char_property_change (endpos, Qdisplay,
1387 Qnil, Qnil);
1388 start = XFASTINT (startpos);
1389 end = XFASTINT (endpos);
1390 /* Move to the last buffer position before the
1391 display property. */
1392 start_display (&it3, w, top);
1393 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1394 /* Move forward one more line if the position before
1395 the display string is a newline or if it is the
1396 rightmost character on a line that is
1397 continued or word-wrapped. */
1398 if (it3.method == GET_FROM_BUFFER
1399 && it3.c == '\n')
1400 move_it_by_lines (&it3, 1);
1401 else if (move_it_in_display_line_to (&it3, -1,
1402 it3.current_x
1403 + it3.pixel_width,
1404 MOVE_TO_X)
1405 == MOVE_LINE_CONTINUED)
1406 {
1407 move_it_by_lines (&it3, 1);
1408 /* When we are under word-wrap, the #$@%!
1409 move_it_by_lines moves 2 lines, so we need to
1410 fix that up. */
1411 if (it3.line_wrap == WORD_WRAP)
1412 move_it_by_lines (&it3, -1);
1413 }
1414
1415 /* Record the vertical coordinate of the display
1416 line where we wound up. */
1417 top_y = it3.current_y;
1418 if (it3.bidi_p)
1419 {
1420 /* When characters are reordered for display,
1421 the character displayed to the left of the
1422 display string could be _after_ the display
1423 property in the logical order. Use the
1424 smallest vertical position of these two. */
1425 start_display (&it3, w, top);
1426 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1427 if (it3.current_y < top_y)
1428 top_y = it3.current_y;
1429 }
1430 /* Move from the top of the window to the beginning
1431 of the display line where the display string
1432 begins. */
1433 start_display (&it3, w, top);
1434 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1435 /* If it3_moved stays zero after the 'while' loop
1436 below, that means we already were at a newline
1437 before the loop (e.g., the display string begins
1438 with a newline), so we don't need to (and cannot)
1439 inspect the glyphs of it3.glyph_row, because
1440 PRODUCE_GLYPHS will not produce anything for a
1441 newline, and thus it3.glyph_row stays at its
1442 stale content it got at top of the window. */
1443 it3_moved = 0;
1444 /* Finally, advance the iterator until we hit the
1445 first display element whose character position is
1446 CHARPOS, or until the first newline from the
1447 display string, which signals the end of the
1448 display line. */
1449 while (get_next_display_element (&it3))
1450 {
1451 PRODUCE_GLYPHS (&it3);
1452 if (IT_CHARPOS (it3) == charpos
1453 || ITERATOR_AT_END_OF_LINE_P (&it3))
1454 break;
1455 it3_moved = 1;
1456 set_iterator_to_next (&it3, 0);
1457 }
1458 top_x = it3.current_x - it3.pixel_width;
1459 /* Normally, we would exit the above loop because we
1460 found the display element whose character
1461 position is CHARPOS. For the contingency that we
1462 didn't, and stopped at the first newline from the
1463 display string, move back over the glyphs
1464 produced from the string, until we find the
1465 rightmost glyph not from the string. */
1466 if (it3_moved
1467 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1468 {
1469 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1470 + it3.glyph_row->used[TEXT_AREA];
1471
1472 while (EQ ((g - 1)->object, string))
1473 {
1474 --g;
1475 top_x -= g->pixel_width;
1476 }
1477 xassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1478 + it3.glyph_row->used[TEXT_AREA]);
1479 }
1480 }
1481 }
1482
1483 *x = top_x;
1484 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1485 *rtop = max (0, window_top_y - top_y);
1486 *rbot = max (0, bottom_y - it.last_visible_y);
1487 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1488 - max (top_y, window_top_y)));
1489 *vpos = it.vpos;
1490 }
1491 }
1492 else
1493 {
1494 /* We were asked to provide info about WINDOW_END. */
1495 struct it it2;
1496 void *it2data = NULL;
1497
1498 SAVE_IT (it2, it, it2data);
1499 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1500 move_it_by_lines (&it, 1);
1501 if (charpos < IT_CHARPOS (it)
1502 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1503 {
1504 visible_p = 1;
1505 RESTORE_IT (&it2, &it2, it2data);
1506 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1507 *x = it2.current_x;
1508 *y = it2.current_y + it2.max_ascent - it2.ascent;
1509 *rtop = max (0, -it2.current_y);
1510 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1511 - it.last_visible_y));
1512 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1513 it.last_visible_y)
1514 - max (it2.current_y,
1515 WINDOW_HEADER_LINE_HEIGHT (w))));
1516 *vpos = it2.vpos;
1517 }
1518 else
1519 bidi_unshelve_cache (it2data, 1);
1520 }
1521 bidi_unshelve_cache (itdata, 0);
1522
1523 if (old_buffer)
1524 set_buffer_internal_1 (old_buffer);
1525
1526 current_header_line_height = current_mode_line_height = -1;
1527
1528 if (visible_p && XFASTINT (w->hscroll) > 0)
1529 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1530
1531 #if 0
1532 /* Debugging code. */
1533 if (visible_p)
1534 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1535 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1536 else
1537 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1538 #endif
1539
1540 return visible_p;
1541 }
1542
1543
1544 /* Return the next character from STR. Return in *LEN the length of
1545 the character. This is like STRING_CHAR_AND_LENGTH but never
1546 returns an invalid character. If we find one, we return a `?', but
1547 with the length of the invalid character. */
1548
1549 static inline int
1550 string_char_and_length (const unsigned char *str, int *len)
1551 {
1552 int c;
1553
1554 c = STRING_CHAR_AND_LENGTH (str, *len);
1555 if (!CHAR_VALID_P (c))
1556 /* We may not change the length here because other places in Emacs
1557 don't use this function, i.e. they silently accept invalid
1558 characters. */
1559 c = '?';
1560
1561 return c;
1562 }
1563
1564
1565
1566 /* Given a position POS containing a valid character and byte position
1567 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1568
1569 static struct text_pos
1570 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1571 {
1572 xassert (STRINGP (string) && nchars >= 0);
1573
1574 if (STRING_MULTIBYTE (string))
1575 {
1576 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1577 int len;
1578
1579 while (nchars--)
1580 {
1581 string_char_and_length (p, &len);
1582 p += len;
1583 CHARPOS (pos) += 1;
1584 BYTEPOS (pos) += len;
1585 }
1586 }
1587 else
1588 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1589
1590 return pos;
1591 }
1592
1593
1594 /* Value is the text position, i.e. character and byte position,
1595 for character position CHARPOS in STRING. */
1596
1597 static inline struct text_pos
1598 string_pos (EMACS_INT charpos, Lisp_Object string)
1599 {
1600 struct text_pos pos;
1601 xassert (STRINGP (string));
1602 xassert (charpos >= 0);
1603 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1604 return pos;
1605 }
1606
1607
1608 /* Value is a text position, i.e. character and byte position, for
1609 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1610 means recognize multibyte characters. */
1611
1612 static struct text_pos
1613 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1614 {
1615 struct text_pos pos;
1616
1617 xassert (s != NULL);
1618 xassert (charpos >= 0);
1619
1620 if (multibyte_p)
1621 {
1622 int len;
1623
1624 SET_TEXT_POS (pos, 0, 0);
1625 while (charpos--)
1626 {
1627 string_char_and_length ((const unsigned char *) s, &len);
1628 s += len;
1629 CHARPOS (pos) += 1;
1630 BYTEPOS (pos) += len;
1631 }
1632 }
1633 else
1634 SET_TEXT_POS (pos, charpos, charpos);
1635
1636 return pos;
1637 }
1638
1639
1640 /* Value is the number of characters in C string S. MULTIBYTE_P
1641 non-zero means recognize multibyte characters. */
1642
1643 static EMACS_INT
1644 number_of_chars (const char *s, int multibyte_p)
1645 {
1646 EMACS_INT nchars;
1647
1648 if (multibyte_p)
1649 {
1650 EMACS_INT rest = strlen (s);
1651 int len;
1652 const unsigned char *p = (const unsigned char *) s;
1653
1654 for (nchars = 0; rest > 0; ++nchars)
1655 {
1656 string_char_and_length (p, &len);
1657 rest -= len, p += len;
1658 }
1659 }
1660 else
1661 nchars = strlen (s);
1662
1663 return nchars;
1664 }
1665
1666
1667 /* Compute byte position NEWPOS->bytepos corresponding to
1668 NEWPOS->charpos. POS is a known position in string STRING.
1669 NEWPOS->charpos must be >= POS.charpos. */
1670
1671 static void
1672 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1673 {
1674 xassert (STRINGP (string));
1675 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1676
1677 if (STRING_MULTIBYTE (string))
1678 *newpos = string_pos_nchars_ahead (pos, string,
1679 CHARPOS (*newpos) - CHARPOS (pos));
1680 else
1681 BYTEPOS (*newpos) = CHARPOS (*newpos);
1682 }
1683
1684 /* EXPORT:
1685 Return an estimation of the pixel height of mode or header lines on
1686 frame F. FACE_ID specifies what line's height to estimate. */
1687
1688 int
1689 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1690 {
1691 #ifdef HAVE_WINDOW_SYSTEM
1692 if (FRAME_WINDOW_P (f))
1693 {
1694 int height = FONT_HEIGHT (FRAME_FONT (f));
1695
1696 /* This function is called so early when Emacs starts that the face
1697 cache and mode line face are not yet initialized. */
1698 if (FRAME_FACE_CACHE (f))
1699 {
1700 struct face *face = FACE_FROM_ID (f, face_id);
1701 if (face)
1702 {
1703 if (face->font)
1704 height = FONT_HEIGHT (face->font);
1705 if (face->box_line_width > 0)
1706 height += 2 * face->box_line_width;
1707 }
1708 }
1709
1710 return height;
1711 }
1712 #endif
1713
1714 return 1;
1715 }
1716
1717 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1718 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1719 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1720 not force the value into range. */
1721
1722 void
1723 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1724 int *x, int *y, NativeRectangle *bounds, int noclip)
1725 {
1726
1727 #ifdef HAVE_WINDOW_SYSTEM
1728 if (FRAME_WINDOW_P (f))
1729 {
1730 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1731 even for negative values. */
1732 if (pix_x < 0)
1733 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1734 if (pix_y < 0)
1735 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1736
1737 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1738 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1739
1740 if (bounds)
1741 STORE_NATIVE_RECT (*bounds,
1742 FRAME_COL_TO_PIXEL_X (f, pix_x),
1743 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1744 FRAME_COLUMN_WIDTH (f) - 1,
1745 FRAME_LINE_HEIGHT (f) - 1);
1746
1747 if (!noclip)
1748 {
1749 if (pix_x < 0)
1750 pix_x = 0;
1751 else if (pix_x > FRAME_TOTAL_COLS (f))
1752 pix_x = FRAME_TOTAL_COLS (f);
1753
1754 if (pix_y < 0)
1755 pix_y = 0;
1756 else if (pix_y > FRAME_LINES (f))
1757 pix_y = FRAME_LINES (f);
1758 }
1759 }
1760 #endif
1761
1762 *x = pix_x;
1763 *y = pix_y;
1764 }
1765
1766
1767 /* Find the glyph under window-relative coordinates X/Y in window W.
1768 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1769 strings. Return in *HPOS and *VPOS the row and column number of
1770 the glyph found. Return in *AREA the glyph area containing X.
1771 Value is a pointer to the glyph found or null if X/Y is not on
1772 text, or we can't tell because W's current matrix is not up to
1773 date. */
1774
1775 static
1776 struct glyph *
1777 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1778 int *dx, int *dy, int *area)
1779 {
1780 struct glyph *glyph, *end;
1781 struct glyph_row *row = NULL;
1782 int x0, i;
1783
1784 /* Find row containing Y. Give up if some row is not enabled. */
1785 for (i = 0; i < w->current_matrix->nrows; ++i)
1786 {
1787 row = MATRIX_ROW (w->current_matrix, i);
1788 if (!row->enabled_p)
1789 return NULL;
1790 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1791 break;
1792 }
1793
1794 *vpos = i;
1795 *hpos = 0;
1796
1797 /* Give up if Y is not in the window. */
1798 if (i == w->current_matrix->nrows)
1799 return NULL;
1800
1801 /* Get the glyph area containing X. */
1802 if (w->pseudo_window_p)
1803 {
1804 *area = TEXT_AREA;
1805 x0 = 0;
1806 }
1807 else
1808 {
1809 if (x < window_box_left_offset (w, TEXT_AREA))
1810 {
1811 *area = LEFT_MARGIN_AREA;
1812 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1813 }
1814 else if (x < window_box_right_offset (w, TEXT_AREA))
1815 {
1816 *area = TEXT_AREA;
1817 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1818 }
1819 else
1820 {
1821 *area = RIGHT_MARGIN_AREA;
1822 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1823 }
1824 }
1825
1826 /* Find glyph containing X. */
1827 glyph = row->glyphs[*area];
1828 end = glyph + row->used[*area];
1829 x -= x0;
1830 while (glyph < end && x >= glyph->pixel_width)
1831 {
1832 x -= glyph->pixel_width;
1833 ++glyph;
1834 }
1835
1836 if (glyph == end)
1837 return NULL;
1838
1839 if (dx)
1840 {
1841 *dx = x;
1842 *dy = y - (row->y + row->ascent - glyph->ascent);
1843 }
1844
1845 *hpos = glyph - row->glyphs[*area];
1846 return glyph;
1847 }
1848
1849 /* Convert frame-relative x/y to coordinates relative to window W.
1850 Takes pseudo-windows into account. */
1851
1852 static void
1853 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1854 {
1855 if (w->pseudo_window_p)
1856 {
1857 /* A pseudo-window is always full-width, and starts at the
1858 left edge of the frame, plus a frame border. */
1859 struct frame *f = XFRAME (w->frame);
1860 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1861 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1862 }
1863 else
1864 {
1865 *x -= WINDOW_LEFT_EDGE_X (w);
1866 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1867 }
1868 }
1869
1870 #ifdef HAVE_WINDOW_SYSTEM
1871
1872 /* EXPORT:
1873 Return in RECTS[] at most N clipping rectangles for glyph string S.
1874 Return the number of stored rectangles. */
1875
1876 int
1877 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1878 {
1879 XRectangle r;
1880
1881 if (n <= 0)
1882 return 0;
1883
1884 if (s->row->full_width_p)
1885 {
1886 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1887 r.x = WINDOW_LEFT_EDGE_X (s->w);
1888 r.width = WINDOW_TOTAL_WIDTH (s->w);
1889
1890 /* Unless displaying a mode or menu bar line, which are always
1891 fully visible, clip to the visible part of the row. */
1892 if (s->w->pseudo_window_p)
1893 r.height = s->row->visible_height;
1894 else
1895 r.height = s->height;
1896 }
1897 else
1898 {
1899 /* This is a text line that may be partially visible. */
1900 r.x = window_box_left (s->w, s->area);
1901 r.width = window_box_width (s->w, s->area);
1902 r.height = s->row->visible_height;
1903 }
1904
1905 if (s->clip_head)
1906 if (r.x < s->clip_head->x)
1907 {
1908 if (r.width >= s->clip_head->x - r.x)
1909 r.width -= s->clip_head->x - r.x;
1910 else
1911 r.width = 0;
1912 r.x = s->clip_head->x;
1913 }
1914 if (s->clip_tail)
1915 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1916 {
1917 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1918 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1919 else
1920 r.width = 0;
1921 }
1922
1923 /* If S draws overlapping rows, it's sufficient to use the top and
1924 bottom of the window for clipping because this glyph string
1925 intentionally draws over other lines. */
1926 if (s->for_overlaps)
1927 {
1928 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1929 r.height = window_text_bottom_y (s->w) - r.y;
1930
1931 /* Alas, the above simple strategy does not work for the
1932 environments with anti-aliased text: if the same text is
1933 drawn onto the same place multiple times, it gets thicker.
1934 If the overlap we are processing is for the erased cursor, we
1935 take the intersection with the rectangle of the cursor. */
1936 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1937 {
1938 XRectangle rc, r_save = r;
1939
1940 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1941 rc.y = s->w->phys_cursor.y;
1942 rc.width = s->w->phys_cursor_width;
1943 rc.height = s->w->phys_cursor_height;
1944
1945 x_intersect_rectangles (&r_save, &rc, &r);
1946 }
1947 }
1948 else
1949 {
1950 /* Don't use S->y for clipping because it doesn't take partially
1951 visible lines into account. For example, it can be negative for
1952 partially visible lines at the top of a window. */
1953 if (!s->row->full_width_p
1954 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1955 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1956 else
1957 r.y = max (0, s->row->y);
1958 }
1959
1960 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1961
1962 /* If drawing the cursor, don't let glyph draw outside its
1963 advertised boundaries. Cleartype does this under some circumstances. */
1964 if (s->hl == DRAW_CURSOR)
1965 {
1966 struct glyph *glyph = s->first_glyph;
1967 int height, max_y;
1968
1969 if (s->x > r.x)
1970 {
1971 r.width -= s->x - r.x;
1972 r.x = s->x;
1973 }
1974 r.width = min (r.width, glyph->pixel_width);
1975
1976 /* If r.y is below window bottom, ensure that we still see a cursor. */
1977 height = min (glyph->ascent + glyph->descent,
1978 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1979 max_y = window_text_bottom_y (s->w) - height;
1980 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1981 if (s->ybase - glyph->ascent > max_y)
1982 {
1983 r.y = max_y;
1984 r.height = height;
1985 }
1986 else
1987 {
1988 /* Don't draw cursor glyph taller than our actual glyph. */
1989 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1990 if (height < r.height)
1991 {
1992 max_y = r.y + r.height;
1993 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1994 r.height = min (max_y - r.y, height);
1995 }
1996 }
1997 }
1998
1999 if (s->row->clip)
2000 {
2001 XRectangle r_save = r;
2002
2003 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2004 r.width = 0;
2005 }
2006
2007 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2008 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2009 {
2010 #ifdef CONVERT_FROM_XRECT
2011 CONVERT_FROM_XRECT (r, *rects);
2012 #else
2013 *rects = r;
2014 #endif
2015 return 1;
2016 }
2017 else
2018 {
2019 /* If we are processing overlapping and allowed to return
2020 multiple clipping rectangles, we exclude the row of the glyph
2021 string from the clipping rectangle. This is to avoid drawing
2022 the same text on the environment with anti-aliasing. */
2023 #ifdef CONVERT_FROM_XRECT
2024 XRectangle rs[2];
2025 #else
2026 XRectangle *rs = rects;
2027 #endif
2028 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2029
2030 if (s->for_overlaps & OVERLAPS_PRED)
2031 {
2032 rs[i] = r;
2033 if (r.y + r.height > row_y)
2034 {
2035 if (r.y < row_y)
2036 rs[i].height = row_y - r.y;
2037 else
2038 rs[i].height = 0;
2039 }
2040 i++;
2041 }
2042 if (s->for_overlaps & OVERLAPS_SUCC)
2043 {
2044 rs[i] = r;
2045 if (r.y < row_y + s->row->visible_height)
2046 {
2047 if (r.y + r.height > row_y + s->row->visible_height)
2048 {
2049 rs[i].y = row_y + s->row->visible_height;
2050 rs[i].height = r.y + r.height - rs[i].y;
2051 }
2052 else
2053 rs[i].height = 0;
2054 }
2055 i++;
2056 }
2057
2058 n = i;
2059 #ifdef CONVERT_FROM_XRECT
2060 for (i = 0; i < n; i++)
2061 CONVERT_FROM_XRECT (rs[i], rects[i]);
2062 #endif
2063 return n;
2064 }
2065 }
2066
2067 /* EXPORT:
2068 Return in *NR the clipping rectangle for glyph string S. */
2069
2070 void
2071 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2072 {
2073 get_glyph_string_clip_rects (s, nr, 1);
2074 }
2075
2076
2077 /* EXPORT:
2078 Return the position and height of the phys cursor in window W.
2079 Set w->phys_cursor_width to width of phys cursor.
2080 */
2081
2082 void
2083 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2084 struct glyph *glyph, int *xp, int *yp, int *heightp)
2085 {
2086 struct frame *f = XFRAME (WINDOW_FRAME (w));
2087 int x, y, wd, h, h0, y0;
2088
2089 /* Compute the width of the rectangle to draw. If on a stretch
2090 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2091 rectangle as wide as the glyph, but use a canonical character
2092 width instead. */
2093 wd = glyph->pixel_width - 1;
2094 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2095 wd++; /* Why? */
2096 #endif
2097
2098 x = w->phys_cursor.x;
2099 if (x < 0)
2100 {
2101 wd += x;
2102 x = 0;
2103 }
2104
2105 if (glyph->type == STRETCH_GLYPH
2106 && !x_stretch_cursor_p)
2107 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2108 w->phys_cursor_width = wd;
2109
2110 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2111
2112 /* If y is below window bottom, ensure that we still see a cursor. */
2113 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2114
2115 h = max (h0, glyph->ascent + glyph->descent);
2116 h0 = min (h0, glyph->ascent + glyph->descent);
2117
2118 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2119 if (y < y0)
2120 {
2121 h = max (h - (y0 - y) + 1, h0);
2122 y = y0 - 1;
2123 }
2124 else
2125 {
2126 y0 = window_text_bottom_y (w) - h0;
2127 if (y > y0)
2128 {
2129 h += y - y0;
2130 y = y0;
2131 }
2132 }
2133
2134 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2135 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2136 *heightp = h;
2137 }
2138
2139 /*
2140 * Remember which glyph the mouse is over.
2141 */
2142
2143 void
2144 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2145 {
2146 Lisp_Object window;
2147 struct window *w;
2148 struct glyph_row *r, *gr, *end_row;
2149 enum window_part part;
2150 enum glyph_row_area area;
2151 int x, y, width, height;
2152
2153 /* Try to determine frame pixel position and size of the glyph under
2154 frame pixel coordinates X/Y on frame F. */
2155
2156 if (!f->glyphs_initialized_p
2157 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2158 NILP (window)))
2159 {
2160 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2161 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2162 goto virtual_glyph;
2163 }
2164
2165 w = XWINDOW (window);
2166 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2167 height = WINDOW_FRAME_LINE_HEIGHT (w);
2168
2169 x = window_relative_x_coord (w, part, gx);
2170 y = gy - WINDOW_TOP_EDGE_Y (w);
2171
2172 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2173 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2174
2175 if (w->pseudo_window_p)
2176 {
2177 area = TEXT_AREA;
2178 part = ON_MODE_LINE; /* Don't adjust margin. */
2179 goto text_glyph;
2180 }
2181
2182 switch (part)
2183 {
2184 case ON_LEFT_MARGIN:
2185 area = LEFT_MARGIN_AREA;
2186 goto text_glyph;
2187
2188 case ON_RIGHT_MARGIN:
2189 area = RIGHT_MARGIN_AREA;
2190 goto text_glyph;
2191
2192 case ON_HEADER_LINE:
2193 case ON_MODE_LINE:
2194 gr = (part == ON_HEADER_LINE
2195 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2196 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2197 gy = gr->y;
2198 area = TEXT_AREA;
2199 goto text_glyph_row_found;
2200
2201 case ON_TEXT:
2202 area = TEXT_AREA;
2203
2204 text_glyph:
2205 gr = 0; gy = 0;
2206 for (; r <= end_row && r->enabled_p; ++r)
2207 if (r->y + r->height > y)
2208 {
2209 gr = r; gy = r->y;
2210 break;
2211 }
2212
2213 text_glyph_row_found:
2214 if (gr && gy <= y)
2215 {
2216 struct glyph *g = gr->glyphs[area];
2217 struct glyph *end = g + gr->used[area];
2218
2219 height = gr->height;
2220 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2221 if (gx + g->pixel_width > x)
2222 break;
2223
2224 if (g < end)
2225 {
2226 if (g->type == IMAGE_GLYPH)
2227 {
2228 /* Don't remember when mouse is over image, as
2229 image may have hot-spots. */
2230 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2231 return;
2232 }
2233 width = g->pixel_width;
2234 }
2235 else
2236 {
2237 /* Use nominal char spacing at end of line. */
2238 x -= gx;
2239 gx += (x / width) * width;
2240 }
2241
2242 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2243 gx += window_box_left_offset (w, area);
2244 }
2245 else
2246 {
2247 /* Use nominal line height at end of window. */
2248 gx = (x / width) * width;
2249 y -= gy;
2250 gy += (y / height) * height;
2251 }
2252 break;
2253
2254 case ON_LEFT_FRINGE:
2255 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2256 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2257 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2258 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2259 goto row_glyph;
2260
2261 case ON_RIGHT_FRINGE:
2262 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2263 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2264 : window_box_right_offset (w, TEXT_AREA));
2265 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2266 goto row_glyph;
2267
2268 case ON_SCROLL_BAR:
2269 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2270 ? 0
2271 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2272 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2273 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2274 : 0)));
2275 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2276
2277 row_glyph:
2278 gr = 0, gy = 0;
2279 for (; r <= end_row && r->enabled_p; ++r)
2280 if (r->y + r->height > y)
2281 {
2282 gr = r; gy = r->y;
2283 break;
2284 }
2285
2286 if (gr && gy <= y)
2287 height = gr->height;
2288 else
2289 {
2290 /* Use nominal line height at end of window. */
2291 y -= gy;
2292 gy += (y / height) * height;
2293 }
2294 break;
2295
2296 default:
2297 ;
2298 virtual_glyph:
2299 /* If there is no glyph under the mouse, then we divide the screen
2300 into a grid of the smallest glyph in the frame, and use that
2301 as our "glyph". */
2302
2303 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2304 round down even for negative values. */
2305 if (gx < 0)
2306 gx -= width - 1;
2307 if (gy < 0)
2308 gy -= height - 1;
2309
2310 gx = (gx / width) * width;
2311 gy = (gy / height) * height;
2312
2313 goto store_rect;
2314 }
2315
2316 gx += WINDOW_LEFT_EDGE_X (w);
2317 gy += WINDOW_TOP_EDGE_Y (w);
2318
2319 store_rect:
2320 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2321
2322 /* Visible feedback for debugging. */
2323 #if 0
2324 #if HAVE_X_WINDOWS
2325 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2326 f->output_data.x->normal_gc,
2327 gx, gy, width, height);
2328 #endif
2329 #endif
2330 }
2331
2332
2333 #endif /* HAVE_WINDOW_SYSTEM */
2334
2335 \f
2336 /***********************************************************************
2337 Lisp form evaluation
2338 ***********************************************************************/
2339
2340 /* Error handler for safe_eval and safe_call. */
2341
2342 static Lisp_Object
2343 safe_eval_handler (Lisp_Object arg)
2344 {
2345 add_to_log ("Error during redisplay: %S", arg, Qnil);
2346 return Qnil;
2347 }
2348
2349
2350 /* Evaluate SEXPR and return the result, or nil if something went
2351 wrong. Prevent redisplay during the evaluation. */
2352
2353 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2354 Return the result, or nil if something went wrong. Prevent
2355 redisplay during the evaluation. */
2356
2357 Lisp_Object
2358 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2359 {
2360 Lisp_Object val;
2361
2362 if (inhibit_eval_during_redisplay)
2363 val = Qnil;
2364 else
2365 {
2366 int count = SPECPDL_INDEX ();
2367 struct gcpro gcpro1;
2368
2369 GCPRO1 (args[0]);
2370 gcpro1.nvars = nargs;
2371 specbind (Qinhibit_redisplay, Qt);
2372 /* Use Qt to ensure debugger does not run,
2373 so there is no possibility of wanting to redisplay. */
2374 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2375 safe_eval_handler);
2376 UNGCPRO;
2377 val = unbind_to (count, val);
2378 }
2379
2380 return val;
2381 }
2382
2383
2384 /* Call function FN with one argument ARG.
2385 Return the result, or nil if something went wrong. */
2386
2387 Lisp_Object
2388 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2389 {
2390 Lisp_Object args[2];
2391 args[0] = fn;
2392 args[1] = arg;
2393 return safe_call (2, args);
2394 }
2395
2396 static Lisp_Object Qeval;
2397
2398 Lisp_Object
2399 safe_eval (Lisp_Object sexpr)
2400 {
2401 return safe_call1 (Qeval, sexpr);
2402 }
2403
2404 /* Call function FN with one argument ARG.
2405 Return the result, or nil if something went wrong. */
2406
2407 Lisp_Object
2408 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2409 {
2410 Lisp_Object args[3];
2411 args[0] = fn;
2412 args[1] = arg1;
2413 args[2] = arg2;
2414 return safe_call (3, args);
2415 }
2416
2417
2418 \f
2419 /***********************************************************************
2420 Debugging
2421 ***********************************************************************/
2422
2423 #if 0
2424
2425 /* Define CHECK_IT to perform sanity checks on iterators.
2426 This is for debugging. It is too slow to do unconditionally. */
2427
2428 static void
2429 check_it (struct it *it)
2430 {
2431 if (it->method == GET_FROM_STRING)
2432 {
2433 xassert (STRINGP (it->string));
2434 xassert (IT_STRING_CHARPOS (*it) >= 0);
2435 }
2436 else
2437 {
2438 xassert (IT_STRING_CHARPOS (*it) < 0);
2439 if (it->method == GET_FROM_BUFFER)
2440 {
2441 /* Check that character and byte positions agree. */
2442 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2443 }
2444 }
2445
2446 if (it->dpvec)
2447 xassert (it->current.dpvec_index >= 0);
2448 else
2449 xassert (it->current.dpvec_index < 0);
2450 }
2451
2452 #define CHECK_IT(IT) check_it ((IT))
2453
2454 #else /* not 0 */
2455
2456 #define CHECK_IT(IT) (void) 0
2457
2458 #endif /* not 0 */
2459
2460
2461 #if GLYPH_DEBUG && XASSERTS
2462
2463 /* Check that the window end of window W is what we expect it
2464 to be---the last row in the current matrix displaying text. */
2465
2466 static void
2467 check_window_end (struct window *w)
2468 {
2469 if (!MINI_WINDOW_P (w)
2470 && !NILP (w->window_end_valid))
2471 {
2472 struct glyph_row *row;
2473 xassert ((row = MATRIX_ROW (w->current_matrix,
2474 XFASTINT (w->window_end_vpos)),
2475 !row->enabled_p
2476 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2477 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2478 }
2479 }
2480
2481 #define CHECK_WINDOW_END(W) check_window_end ((W))
2482
2483 #else
2484
2485 #define CHECK_WINDOW_END(W) (void) 0
2486
2487 #endif
2488
2489
2490 \f
2491 /***********************************************************************
2492 Iterator initialization
2493 ***********************************************************************/
2494
2495 /* Initialize IT for displaying current_buffer in window W, starting
2496 at character position CHARPOS. CHARPOS < 0 means that no buffer
2497 position is specified which is useful when the iterator is assigned
2498 a position later. BYTEPOS is the byte position corresponding to
2499 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2500
2501 If ROW is not null, calls to produce_glyphs with IT as parameter
2502 will produce glyphs in that row.
2503
2504 BASE_FACE_ID is the id of a base face to use. It must be one of
2505 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2506 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2507 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2508
2509 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2510 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2511 will be initialized to use the corresponding mode line glyph row of
2512 the desired matrix of W. */
2513
2514 void
2515 init_iterator (struct it *it, struct window *w,
2516 EMACS_INT charpos, EMACS_INT bytepos,
2517 struct glyph_row *row, enum face_id base_face_id)
2518 {
2519 int highlight_region_p;
2520 enum face_id remapped_base_face_id = base_face_id;
2521
2522 /* Some precondition checks. */
2523 xassert (w != NULL && it != NULL);
2524 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2525 && charpos <= ZV));
2526
2527 /* If face attributes have been changed since the last redisplay,
2528 free realized faces now because they depend on face definitions
2529 that might have changed. Don't free faces while there might be
2530 desired matrices pending which reference these faces. */
2531 if (face_change_count && !inhibit_free_realized_faces)
2532 {
2533 face_change_count = 0;
2534 free_all_realized_faces (Qnil);
2535 }
2536
2537 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2538 if (! NILP (Vface_remapping_alist))
2539 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2540
2541 /* Use one of the mode line rows of W's desired matrix if
2542 appropriate. */
2543 if (row == NULL)
2544 {
2545 if (base_face_id == MODE_LINE_FACE_ID
2546 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2547 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2548 else if (base_face_id == HEADER_LINE_FACE_ID)
2549 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2550 }
2551
2552 /* Clear IT. */
2553 memset (it, 0, sizeof *it);
2554 it->current.overlay_string_index = -1;
2555 it->current.dpvec_index = -1;
2556 it->base_face_id = remapped_base_face_id;
2557 it->string = Qnil;
2558 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2559 it->paragraph_embedding = L2R;
2560 it->bidi_it.string.lstring = Qnil;
2561 it->bidi_it.string.s = NULL;
2562 it->bidi_it.string.bufpos = 0;
2563
2564 /* The window in which we iterate over current_buffer: */
2565 XSETWINDOW (it->window, w);
2566 it->w = w;
2567 it->f = XFRAME (w->frame);
2568
2569 it->cmp_it.id = -1;
2570
2571 /* Extra space between lines (on window systems only). */
2572 if (base_face_id == DEFAULT_FACE_ID
2573 && FRAME_WINDOW_P (it->f))
2574 {
2575 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2576 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2577 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2578 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2579 * FRAME_LINE_HEIGHT (it->f));
2580 else if (it->f->extra_line_spacing > 0)
2581 it->extra_line_spacing = it->f->extra_line_spacing;
2582 it->max_extra_line_spacing = 0;
2583 }
2584
2585 /* If realized faces have been removed, e.g. because of face
2586 attribute changes of named faces, recompute them. When running
2587 in batch mode, the face cache of the initial frame is null. If
2588 we happen to get called, make a dummy face cache. */
2589 if (FRAME_FACE_CACHE (it->f) == NULL)
2590 init_frame_faces (it->f);
2591 if (FRAME_FACE_CACHE (it->f)->used == 0)
2592 recompute_basic_faces (it->f);
2593
2594 /* Current value of the `slice', `space-width', and 'height' properties. */
2595 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2596 it->space_width = Qnil;
2597 it->font_height = Qnil;
2598 it->override_ascent = -1;
2599
2600 /* Are control characters displayed as `^C'? */
2601 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2602
2603 /* -1 means everything between a CR and the following line end
2604 is invisible. >0 means lines indented more than this value are
2605 invisible. */
2606 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2607 ? XINT (BVAR (current_buffer, selective_display))
2608 : (!NILP (BVAR (current_buffer, selective_display))
2609 ? -1 : 0));
2610 it->selective_display_ellipsis_p
2611 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2612
2613 /* Display table to use. */
2614 it->dp = window_display_table (w);
2615
2616 /* Are multibyte characters enabled in current_buffer? */
2617 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2618
2619 /* Non-zero if we should highlight the region. */
2620 highlight_region_p
2621 = (!NILP (Vtransient_mark_mode)
2622 && !NILP (BVAR (current_buffer, mark_active))
2623 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2624
2625 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2626 start and end of a visible region in window IT->w. Set both to
2627 -1 to indicate no region. */
2628 if (highlight_region_p
2629 /* Maybe highlight only in selected window. */
2630 && (/* Either show region everywhere. */
2631 highlight_nonselected_windows
2632 /* Or show region in the selected window. */
2633 || w == XWINDOW (selected_window)
2634 /* Or show the region if we are in the mini-buffer and W is
2635 the window the mini-buffer refers to. */
2636 || (MINI_WINDOW_P (XWINDOW (selected_window))
2637 && WINDOWP (minibuf_selected_window)
2638 && w == XWINDOW (minibuf_selected_window))))
2639 {
2640 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2641 it->region_beg_charpos = min (PT, markpos);
2642 it->region_end_charpos = max (PT, markpos);
2643 }
2644 else
2645 it->region_beg_charpos = it->region_end_charpos = -1;
2646
2647 /* Get the position at which the redisplay_end_trigger hook should
2648 be run, if it is to be run at all. */
2649 if (MARKERP (w->redisplay_end_trigger)
2650 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2651 it->redisplay_end_trigger_charpos
2652 = marker_position (w->redisplay_end_trigger);
2653 else if (INTEGERP (w->redisplay_end_trigger))
2654 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2655
2656 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2657
2658 /* Are lines in the display truncated? */
2659 if (base_face_id != DEFAULT_FACE_ID
2660 || XINT (it->w->hscroll)
2661 || (! WINDOW_FULL_WIDTH_P (it->w)
2662 && ((!NILP (Vtruncate_partial_width_windows)
2663 && !INTEGERP (Vtruncate_partial_width_windows))
2664 || (INTEGERP (Vtruncate_partial_width_windows)
2665 && (WINDOW_TOTAL_COLS (it->w)
2666 < XINT (Vtruncate_partial_width_windows))))))
2667 it->line_wrap = TRUNCATE;
2668 else if (NILP (BVAR (current_buffer, truncate_lines)))
2669 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2670 ? WINDOW_WRAP : WORD_WRAP;
2671 else
2672 it->line_wrap = TRUNCATE;
2673
2674 /* Get dimensions of truncation and continuation glyphs. These are
2675 displayed as fringe bitmaps under X, so we don't need them for such
2676 frames. */
2677 if (!FRAME_WINDOW_P (it->f))
2678 {
2679 if (it->line_wrap == TRUNCATE)
2680 {
2681 /* We will need the truncation glyph. */
2682 xassert (it->glyph_row == NULL);
2683 produce_special_glyphs (it, IT_TRUNCATION);
2684 it->truncation_pixel_width = it->pixel_width;
2685 }
2686 else
2687 {
2688 /* We will need the continuation glyph. */
2689 xassert (it->glyph_row == NULL);
2690 produce_special_glyphs (it, IT_CONTINUATION);
2691 it->continuation_pixel_width = it->pixel_width;
2692 }
2693
2694 /* Reset these values to zero because the produce_special_glyphs
2695 above has changed them. */
2696 it->pixel_width = it->ascent = it->descent = 0;
2697 it->phys_ascent = it->phys_descent = 0;
2698 }
2699
2700 /* Set this after getting the dimensions of truncation and
2701 continuation glyphs, so that we don't produce glyphs when calling
2702 produce_special_glyphs, above. */
2703 it->glyph_row = row;
2704 it->area = TEXT_AREA;
2705
2706 /* Forget any previous info about this row being reversed. */
2707 if (it->glyph_row)
2708 it->glyph_row->reversed_p = 0;
2709
2710 /* Get the dimensions of the display area. The display area
2711 consists of the visible window area plus a horizontally scrolled
2712 part to the left of the window. All x-values are relative to the
2713 start of this total display area. */
2714 if (base_face_id != DEFAULT_FACE_ID)
2715 {
2716 /* Mode lines, menu bar in terminal frames. */
2717 it->first_visible_x = 0;
2718 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2719 }
2720 else
2721 {
2722 it->first_visible_x
2723 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2724 it->last_visible_x = (it->first_visible_x
2725 + window_box_width (w, TEXT_AREA));
2726
2727 /* If we truncate lines, leave room for the truncator glyph(s) at
2728 the right margin. Otherwise, leave room for the continuation
2729 glyph(s). Truncation and continuation glyphs are not inserted
2730 for window-based redisplay. */
2731 if (!FRAME_WINDOW_P (it->f))
2732 {
2733 if (it->line_wrap == TRUNCATE)
2734 it->last_visible_x -= it->truncation_pixel_width;
2735 else
2736 it->last_visible_x -= it->continuation_pixel_width;
2737 }
2738
2739 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2740 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2741 }
2742
2743 /* Leave room for a border glyph. */
2744 if (!FRAME_WINDOW_P (it->f)
2745 && !WINDOW_RIGHTMOST_P (it->w))
2746 it->last_visible_x -= 1;
2747
2748 it->last_visible_y = window_text_bottom_y (w);
2749
2750 /* For mode lines and alike, arrange for the first glyph having a
2751 left box line if the face specifies a box. */
2752 if (base_face_id != DEFAULT_FACE_ID)
2753 {
2754 struct face *face;
2755
2756 it->face_id = remapped_base_face_id;
2757
2758 /* If we have a boxed mode line, make the first character appear
2759 with a left box line. */
2760 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2761 if (face->box != FACE_NO_BOX)
2762 it->start_of_box_run_p = 1;
2763 }
2764
2765 /* If a buffer position was specified, set the iterator there,
2766 getting overlays and face properties from that position. */
2767 if (charpos >= BUF_BEG (current_buffer))
2768 {
2769 it->end_charpos = ZV;
2770 IT_CHARPOS (*it) = charpos;
2771
2772 /* We will rely on `reseat' to set this up properly, via
2773 handle_face_prop. */
2774 it->face_id = it->base_face_id;
2775
2776 /* Compute byte position if not specified. */
2777 if (bytepos < charpos)
2778 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2779 else
2780 IT_BYTEPOS (*it) = bytepos;
2781
2782 it->start = it->current;
2783 /* Do we need to reorder bidirectional text? Not if this is a
2784 unibyte buffer: by definition, none of the single-byte
2785 characters are strong R2L, so no reordering is needed. And
2786 bidi.c doesn't support unibyte buffers anyway. Also, don't
2787 reorder while we are loading loadup.el, since the tables of
2788 character properties needed for reordering are not yet
2789 available. */
2790 it->bidi_p =
2791 NILP (Vpurify_flag)
2792 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2793 && it->multibyte_p;
2794
2795 /* If we are to reorder bidirectional text, init the bidi
2796 iterator. */
2797 if (it->bidi_p)
2798 {
2799 /* Note the paragraph direction that this buffer wants to
2800 use. */
2801 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2802 Qleft_to_right))
2803 it->paragraph_embedding = L2R;
2804 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2805 Qright_to_left))
2806 it->paragraph_embedding = R2L;
2807 else
2808 it->paragraph_embedding = NEUTRAL_DIR;
2809 bidi_unshelve_cache (NULL, 0);
2810 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2811 &it->bidi_it);
2812 }
2813
2814 /* Compute faces etc. */
2815 reseat (it, it->current.pos, 1);
2816 }
2817
2818 CHECK_IT (it);
2819 }
2820
2821
2822 /* Initialize IT for the display of window W with window start POS. */
2823
2824 void
2825 start_display (struct it *it, struct window *w, struct text_pos pos)
2826 {
2827 struct glyph_row *row;
2828 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2829
2830 row = w->desired_matrix->rows + first_vpos;
2831 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2832 it->first_vpos = first_vpos;
2833
2834 /* Don't reseat to previous visible line start if current start
2835 position is in a string or image. */
2836 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2837 {
2838 int start_at_line_beg_p;
2839 int first_y = it->current_y;
2840
2841 /* If window start is not at a line start, skip forward to POS to
2842 get the correct continuation lines width. */
2843 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2844 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2845 if (!start_at_line_beg_p)
2846 {
2847 int new_x;
2848
2849 reseat_at_previous_visible_line_start (it);
2850 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2851
2852 new_x = it->current_x + it->pixel_width;
2853
2854 /* If lines are continued, this line may end in the middle
2855 of a multi-glyph character (e.g. a control character
2856 displayed as \003, or in the middle of an overlay
2857 string). In this case move_it_to above will not have
2858 taken us to the start of the continuation line but to the
2859 end of the continued line. */
2860 if (it->current_x > 0
2861 && it->line_wrap != TRUNCATE /* Lines are continued. */
2862 && (/* And glyph doesn't fit on the line. */
2863 new_x > it->last_visible_x
2864 /* Or it fits exactly and we're on a window
2865 system frame. */
2866 || (new_x == it->last_visible_x
2867 && FRAME_WINDOW_P (it->f))))
2868 {
2869 if ((it->current.dpvec_index >= 0
2870 || it->current.overlay_string_index >= 0)
2871 /* If we are on a newline from a display vector or
2872 overlay string, then we are already at the end of
2873 a screen line; no need to go to the next line in
2874 that case, as this line is not really continued.
2875 (If we do go to the next line, C-e will not DTRT.) */
2876 && it->c != '\n')
2877 {
2878 set_iterator_to_next (it, 1);
2879 move_it_in_display_line_to (it, -1, -1, 0);
2880 }
2881
2882 it->continuation_lines_width += it->current_x;
2883 }
2884 /* If the character at POS is displayed via a display
2885 vector, move_it_to above stops at the final glyph of
2886 IT->dpvec. To make the caller redisplay that character
2887 again (a.k.a. start at POS), we need to reset the
2888 dpvec_index to the beginning of IT->dpvec. */
2889 else if (it->current.dpvec_index >= 0)
2890 it->current.dpvec_index = 0;
2891
2892 /* We're starting a new display line, not affected by the
2893 height of the continued line, so clear the appropriate
2894 fields in the iterator structure. */
2895 it->max_ascent = it->max_descent = 0;
2896 it->max_phys_ascent = it->max_phys_descent = 0;
2897
2898 it->current_y = first_y;
2899 it->vpos = 0;
2900 it->current_x = it->hpos = 0;
2901 }
2902 }
2903 }
2904
2905
2906 /* Return 1 if POS is a position in ellipses displayed for invisible
2907 text. W is the window we display, for text property lookup. */
2908
2909 static int
2910 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2911 {
2912 Lisp_Object prop, window;
2913 int ellipses_p = 0;
2914 EMACS_INT charpos = CHARPOS (pos->pos);
2915
2916 /* If POS specifies a position in a display vector, this might
2917 be for an ellipsis displayed for invisible text. We won't
2918 get the iterator set up for delivering that ellipsis unless
2919 we make sure that it gets aware of the invisible text. */
2920 if (pos->dpvec_index >= 0
2921 && pos->overlay_string_index < 0
2922 && CHARPOS (pos->string_pos) < 0
2923 && charpos > BEGV
2924 && (XSETWINDOW (window, w),
2925 prop = Fget_char_property (make_number (charpos),
2926 Qinvisible, window),
2927 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2928 {
2929 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2930 window);
2931 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2932 }
2933
2934 return ellipses_p;
2935 }
2936
2937
2938 /* Initialize IT for stepping through current_buffer in window W,
2939 starting at position POS that includes overlay string and display
2940 vector/ control character translation position information. Value
2941 is zero if there are overlay strings with newlines at POS. */
2942
2943 static int
2944 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2945 {
2946 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2947 int i, overlay_strings_with_newlines = 0;
2948
2949 /* If POS specifies a position in a display vector, this might
2950 be for an ellipsis displayed for invisible text. We won't
2951 get the iterator set up for delivering that ellipsis unless
2952 we make sure that it gets aware of the invisible text. */
2953 if (in_ellipses_for_invisible_text_p (pos, w))
2954 {
2955 --charpos;
2956 bytepos = 0;
2957 }
2958
2959 /* Keep in mind: the call to reseat in init_iterator skips invisible
2960 text, so we might end up at a position different from POS. This
2961 is only a problem when POS is a row start after a newline and an
2962 overlay starts there with an after-string, and the overlay has an
2963 invisible property. Since we don't skip invisible text in
2964 display_line and elsewhere immediately after consuming the
2965 newline before the row start, such a POS will not be in a string,
2966 but the call to init_iterator below will move us to the
2967 after-string. */
2968 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2969
2970 /* This only scans the current chunk -- it should scan all chunks.
2971 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2972 to 16 in 22.1 to make this a lesser problem. */
2973 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2974 {
2975 const char *s = SSDATA (it->overlay_strings[i]);
2976 const char *e = s + SBYTES (it->overlay_strings[i]);
2977
2978 while (s < e && *s != '\n')
2979 ++s;
2980
2981 if (s < e)
2982 {
2983 overlay_strings_with_newlines = 1;
2984 break;
2985 }
2986 }
2987
2988 /* If position is within an overlay string, set up IT to the right
2989 overlay string. */
2990 if (pos->overlay_string_index >= 0)
2991 {
2992 int relative_index;
2993
2994 /* If the first overlay string happens to have a `display'
2995 property for an image, the iterator will be set up for that
2996 image, and we have to undo that setup first before we can
2997 correct the overlay string index. */
2998 if (it->method == GET_FROM_IMAGE)
2999 pop_it (it);
3000
3001 /* We already have the first chunk of overlay strings in
3002 IT->overlay_strings. Load more until the one for
3003 pos->overlay_string_index is in IT->overlay_strings. */
3004 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3005 {
3006 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3007 it->current.overlay_string_index = 0;
3008 while (n--)
3009 {
3010 load_overlay_strings (it, 0);
3011 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3012 }
3013 }
3014
3015 it->current.overlay_string_index = pos->overlay_string_index;
3016 relative_index = (it->current.overlay_string_index
3017 % OVERLAY_STRING_CHUNK_SIZE);
3018 it->string = it->overlay_strings[relative_index];
3019 xassert (STRINGP (it->string));
3020 it->current.string_pos = pos->string_pos;
3021 it->method = GET_FROM_STRING;
3022 }
3023
3024 if (CHARPOS (pos->string_pos) >= 0)
3025 {
3026 /* Recorded position is not in an overlay string, but in another
3027 string. This can only be a string from a `display' property.
3028 IT should already be filled with that string. */
3029 it->current.string_pos = pos->string_pos;
3030 xassert (STRINGP (it->string));
3031 }
3032
3033 /* Restore position in display vector translations, control
3034 character translations or ellipses. */
3035 if (pos->dpvec_index >= 0)
3036 {
3037 if (it->dpvec == NULL)
3038 get_next_display_element (it);
3039 xassert (it->dpvec && it->current.dpvec_index == 0);
3040 it->current.dpvec_index = pos->dpvec_index;
3041 }
3042
3043 CHECK_IT (it);
3044 return !overlay_strings_with_newlines;
3045 }
3046
3047
3048 /* Initialize IT for stepping through current_buffer in window W
3049 starting at ROW->start. */
3050
3051 static void
3052 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3053 {
3054 init_from_display_pos (it, w, &row->start);
3055 it->start = row->start;
3056 it->continuation_lines_width = row->continuation_lines_width;
3057 CHECK_IT (it);
3058 }
3059
3060
3061 /* Initialize IT for stepping through current_buffer in window W
3062 starting in the line following ROW, i.e. starting at ROW->end.
3063 Value is zero if there are overlay strings with newlines at ROW's
3064 end position. */
3065
3066 static int
3067 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3068 {
3069 int success = 0;
3070
3071 if (init_from_display_pos (it, w, &row->end))
3072 {
3073 if (row->continued_p)
3074 it->continuation_lines_width
3075 = row->continuation_lines_width + row->pixel_width;
3076 CHECK_IT (it);
3077 success = 1;
3078 }
3079
3080 return success;
3081 }
3082
3083
3084
3085 \f
3086 /***********************************************************************
3087 Text properties
3088 ***********************************************************************/
3089
3090 /* Called when IT reaches IT->stop_charpos. Handle text property and
3091 overlay changes. Set IT->stop_charpos to the next position where
3092 to stop. */
3093
3094 static void
3095 handle_stop (struct it *it)
3096 {
3097 enum prop_handled handled;
3098 int handle_overlay_change_p;
3099 struct props *p;
3100
3101 it->dpvec = NULL;
3102 it->current.dpvec_index = -1;
3103 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3104 it->ignore_overlay_strings_at_pos_p = 0;
3105 it->ellipsis_p = 0;
3106
3107 /* Use face of preceding text for ellipsis (if invisible) */
3108 if (it->selective_display_ellipsis_p)
3109 it->saved_face_id = it->face_id;
3110
3111 do
3112 {
3113 handled = HANDLED_NORMALLY;
3114
3115 /* Call text property handlers. */
3116 for (p = it_props; p->handler; ++p)
3117 {
3118 handled = p->handler (it);
3119
3120 if (handled == HANDLED_RECOMPUTE_PROPS)
3121 break;
3122 else if (handled == HANDLED_RETURN)
3123 {
3124 /* We still want to show before and after strings from
3125 overlays even if the actual buffer text is replaced. */
3126 if (!handle_overlay_change_p
3127 || it->sp > 1
3128 || !get_overlay_strings_1 (it, 0, 0))
3129 {
3130 if (it->ellipsis_p)
3131 setup_for_ellipsis (it, 0);
3132 /* When handling a display spec, we might load an
3133 empty string. In that case, discard it here. We
3134 used to discard it in handle_single_display_spec,
3135 but that causes get_overlay_strings_1, above, to
3136 ignore overlay strings that we must check. */
3137 if (STRINGP (it->string) && !SCHARS (it->string))
3138 pop_it (it);
3139 return;
3140 }
3141 else if (STRINGP (it->string) && !SCHARS (it->string))
3142 pop_it (it);
3143 else
3144 {
3145 it->ignore_overlay_strings_at_pos_p = 1;
3146 it->string_from_display_prop_p = 0;
3147 it->from_disp_prop_p = 0;
3148 handle_overlay_change_p = 0;
3149 }
3150 handled = HANDLED_RECOMPUTE_PROPS;
3151 break;
3152 }
3153 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3154 handle_overlay_change_p = 0;
3155 }
3156
3157 if (handled != HANDLED_RECOMPUTE_PROPS)
3158 {
3159 /* Don't check for overlay strings below when set to deliver
3160 characters from a display vector. */
3161 if (it->method == GET_FROM_DISPLAY_VECTOR)
3162 handle_overlay_change_p = 0;
3163
3164 /* Handle overlay changes.
3165 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3166 if it finds overlays. */
3167 if (handle_overlay_change_p)
3168 handled = handle_overlay_change (it);
3169 }
3170
3171 if (it->ellipsis_p)
3172 {
3173 setup_for_ellipsis (it, 0);
3174 break;
3175 }
3176 }
3177 while (handled == HANDLED_RECOMPUTE_PROPS);
3178
3179 /* Determine where to stop next. */
3180 if (handled == HANDLED_NORMALLY)
3181 compute_stop_pos (it);
3182 }
3183
3184
3185 /* Compute IT->stop_charpos from text property and overlay change
3186 information for IT's current position. */
3187
3188 static void
3189 compute_stop_pos (struct it *it)
3190 {
3191 register INTERVAL iv, next_iv;
3192 Lisp_Object object, limit, position;
3193 EMACS_INT charpos, bytepos;
3194
3195 if (STRINGP (it->string))
3196 {
3197 /* Strings are usually short, so don't limit the search for
3198 properties. */
3199 it->stop_charpos = it->end_charpos;
3200 object = it->string;
3201 limit = Qnil;
3202 charpos = IT_STRING_CHARPOS (*it);
3203 bytepos = IT_STRING_BYTEPOS (*it);
3204 }
3205 else
3206 {
3207 EMACS_INT pos;
3208
3209 /* If end_charpos is out of range for some reason, such as a
3210 misbehaving display function, rationalize it (Bug#5984). */
3211 if (it->end_charpos > ZV)
3212 it->end_charpos = ZV;
3213 it->stop_charpos = it->end_charpos;
3214
3215 /* If next overlay change is in front of the current stop pos
3216 (which is IT->end_charpos), stop there. Note: value of
3217 next_overlay_change is point-max if no overlay change
3218 follows. */
3219 charpos = IT_CHARPOS (*it);
3220 bytepos = IT_BYTEPOS (*it);
3221 pos = next_overlay_change (charpos);
3222 if (pos < it->stop_charpos)
3223 it->stop_charpos = pos;
3224
3225 /* If showing the region, we have to stop at the region
3226 start or end because the face might change there. */
3227 if (it->region_beg_charpos > 0)
3228 {
3229 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3230 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3231 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3232 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3233 }
3234
3235 /* Set up variables for computing the stop position from text
3236 property changes. */
3237 XSETBUFFER (object, current_buffer);
3238 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3239 }
3240
3241 /* Get the interval containing IT's position. Value is a null
3242 interval if there isn't such an interval. */
3243 position = make_number (charpos);
3244 iv = validate_interval_range (object, &position, &position, 0);
3245 if (!NULL_INTERVAL_P (iv))
3246 {
3247 Lisp_Object values_here[LAST_PROP_IDX];
3248 struct props *p;
3249
3250 /* Get properties here. */
3251 for (p = it_props; p->handler; ++p)
3252 values_here[p->idx] = textget (iv->plist, *p->name);
3253
3254 /* Look for an interval following iv that has different
3255 properties. */
3256 for (next_iv = next_interval (iv);
3257 (!NULL_INTERVAL_P (next_iv)
3258 && (NILP (limit)
3259 || XFASTINT (limit) > next_iv->position));
3260 next_iv = next_interval (next_iv))
3261 {
3262 for (p = it_props; p->handler; ++p)
3263 {
3264 Lisp_Object new_value;
3265
3266 new_value = textget (next_iv->plist, *p->name);
3267 if (!EQ (values_here[p->idx], new_value))
3268 break;
3269 }
3270
3271 if (p->handler)
3272 break;
3273 }
3274
3275 if (!NULL_INTERVAL_P (next_iv))
3276 {
3277 if (INTEGERP (limit)
3278 && next_iv->position >= XFASTINT (limit))
3279 /* No text property change up to limit. */
3280 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3281 else
3282 /* Text properties change in next_iv. */
3283 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3284 }
3285 }
3286
3287 if (it->cmp_it.id < 0)
3288 {
3289 EMACS_INT stoppos = it->end_charpos;
3290
3291 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3292 stoppos = -1;
3293 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3294 stoppos, it->string);
3295 }
3296
3297 xassert (STRINGP (it->string)
3298 || (it->stop_charpos >= BEGV
3299 && it->stop_charpos >= IT_CHARPOS (*it)));
3300 }
3301
3302
3303 /* Return the position of the next overlay change after POS in
3304 current_buffer. Value is point-max if no overlay change
3305 follows. This is like `next-overlay-change' but doesn't use
3306 xmalloc. */
3307
3308 static EMACS_INT
3309 next_overlay_change (EMACS_INT pos)
3310 {
3311 ptrdiff_t i, noverlays;
3312 EMACS_INT endpos;
3313 Lisp_Object *overlays;
3314
3315 /* Get all overlays at the given position. */
3316 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3317
3318 /* If any of these overlays ends before endpos,
3319 use its ending point instead. */
3320 for (i = 0; i < noverlays; ++i)
3321 {
3322 Lisp_Object oend;
3323 EMACS_INT oendpos;
3324
3325 oend = OVERLAY_END (overlays[i]);
3326 oendpos = OVERLAY_POSITION (oend);
3327 endpos = min (endpos, oendpos);
3328 }
3329
3330 return endpos;
3331 }
3332
3333 /* How many characters forward to search for a display property or
3334 display string. Searching too far forward makes the bidi display
3335 sluggish, especially in small windows. */
3336 #define MAX_DISP_SCAN 250
3337
3338 /* Return the character position of a display string at or after
3339 position specified by POSITION. If no display string exists at or
3340 after POSITION, return ZV. A display string is either an overlay
3341 with `display' property whose value is a string, or a `display'
3342 text property whose value is a string. STRING is data about the
3343 string to iterate; if STRING->lstring is nil, we are iterating a
3344 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3345 on a GUI frame. DISP_PROP is set to zero if we searched
3346 MAX_DISP_SCAN characters forward without finding any display
3347 strings, non-zero otherwise. It is set to 2 if the display string
3348 uses any kind of `(space ...)' spec that will produce a stretch of
3349 white space in the text area. */
3350 EMACS_INT
3351 compute_display_string_pos (struct text_pos *position,
3352 struct bidi_string_data *string,
3353 int frame_window_p, int *disp_prop)
3354 {
3355 /* OBJECT = nil means current buffer. */
3356 Lisp_Object object =
3357 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3358 Lisp_Object pos, spec, limpos;
3359 int string_p = (string && (STRINGP (string->lstring) || string->s));
3360 EMACS_INT eob = string_p ? string->schars : ZV;
3361 EMACS_INT begb = string_p ? 0 : BEGV;
3362 EMACS_INT bufpos, charpos = CHARPOS (*position);
3363 EMACS_INT lim =
3364 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3365 struct text_pos tpos;
3366 int rv = 0;
3367
3368 *disp_prop = 1;
3369
3370 if (charpos >= eob
3371 /* We don't support display properties whose values are strings
3372 that have display string properties. */
3373 || string->from_disp_str
3374 /* C strings cannot have display properties. */
3375 || (string->s && !STRINGP (object)))
3376 {
3377 *disp_prop = 0;
3378 return eob;
3379 }
3380
3381 /* If the character at CHARPOS is where the display string begins,
3382 return CHARPOS. */
3383 pos = make_number (charpos);
3384 if (STRINGP (object))
3385 bufpos = string->bufpos;
3386 else
3387 bufpos = charpos;
3388 tpos = *position;
3389 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3390 && (charpos <= begb
3391 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3392 object),
3393 spec))
3394 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3395 frame_window_p)))
3396 {
3397 if (rv == 2)
3398 *disp_prop = 2;
3399 return charpos;
3400 }
3401
3402 /* Look forward for the first character with a `display' property
3403 that will replace the underlying text when displayed. */
3404 limpos = make_number (lim);
3405 do {
3406 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3407 CHARPOS (tpos) = XFASTINT (pos);
3408 if (CHARPOS (tpos) >= lim)
3409 {
3410 *disp_prop = 0;
3411 break;
3412 }
3413 if (STRINGP (object))
3414 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3415 else
3416 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3417 spec = Fget_char_property (pos, Qdisplay, object);
3418 if (!STRINGP (object))
3419 bufpos = CHARPOS (tpos);
3420 } while (NILP (spec)
3421 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3422 bufpos, frame_window_p)));
3423 if (rv == 2)
3424 *disp_prop = 2;
3425
3426 return CHARPOS (tpos);
3427 }
3428
3429 /* Return the character position of the end of the display string that
3430 started at CHARPOS. If there's no display string at CHARPOS,
3431 return -1. A display string is either an overlay with `display'
3432 property whose value is a string or a `display' text property whose
3433 value is a string. */
3434 EMACS_INT
3435 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3436 {
3437 /* OBJECT = nil means current buffer. */
3438 Lisp_Object object =
3439 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3440 Lisp_Object pos = make_number (charpos);
3441 EMACS_INT eob =
3442 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3443
3444 if (charpos >= eob || (string->s && !STRINGP (object)))
3445 return eob;
3446
3447 /* It could happen that the display property or overlay was removed
3448 since we found it in compute_display_string_pos above. One way
3449 this can happen is if JIT font-lock was called (through
3450 handle_fontified_prop), and jit-lock-functions remove text
3451 properties or overlays from the portion of buffer that includes
3452 CHARPOS. Muse mode is known to do that, for example. In this
3453 case, we return -1 to the caller, to signal that no display
3454 string is actually present at CHARPOS. See bidi_fetch_char for
3455 how this is handled.
3456
3457 An alternative would be to never look for display properties past
3458 it->stop_charpos. But neither compute_display_string_pos nor
3459 bidi_fetch_char that calls it know or care where the next
3460 stop_charpos is. */
3461 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3462 return -1;
3463
3464 /* Look forward for the first character where the `display' property
3465 changes. */
3466 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3467
3468 return XFASTINT (pos);
3469 }
3470
3471
3472 \f
3473 /***********************************************************************
3474 Fontification
3475 ***********************************************************************/
3476
3477 /* Handle changes in the `fontified' property of the current buffer by
3478 calling hook functions from Qfontification_functions to fontify
3479 regions of text. */
3480
3481 static enum prop_handled
3482 handle_fontified_prop (struct it *it)
3483 {
3484 Lisp_Object prop, pos;
3485 enum prop_handled handled = HANDLED_NORMALLY;
3486
3487 if (!NILP (Vmemory_full))
3488 return handled;
3489
3490 /* Get the value of the `fontified' property at IT's current buffer
3491 position. (The `fontified' property doesn't have a special
3492 meaning in strings.) If the value is nil, call functions from
3493 Qfontification_functions. */
3494 if (!STRINGP (it->string)
3495 && it->s == NULL
3496 && !NILP (Vfontification_functions)
3497 && !NILP (Vrun_hooks)
3498 && (pos = make_number (IT_CHARPOS (*it)),
3499 prop = Fget_char_property (pos, Qfontified, Qnil),
3500 /* Ignore the special cased nil value always present at EOB since
3501 no amount of fontifying will be able to change it. */
3502 NILP (prop) && IT_CHARPOS (*it) < Z))
3503 {
3504 int count = SPECPDL_INDEX ();
3505 Lisp_Object val;
3506 struct buffer *obuf = current_buffer;
3507 int begv = BEGV, zv = ZV;
3508 int old_clip_changed = current_buffer->clip_changed;
3509
3510 val = Vfontification_functions;
3511 specbind (Qfontification_functions, Qnil);
3512
3513 xassert (it->end_charpos == ZV);
3514
3515 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3516 safe_call1 (val, pos);
3517 else
3518 {
3519 Lisp_Object fns, fn;
3520 struct gcpro gcpro1, gcpro2;
3521
3522 fns = Qnil;
3523 GCPRO2 (val, fns);
3524
3525 for (; CONSP (val); val = XCDR (val))
3526 {
3527 fn = XCAR (val);
3528
3529 if (EQ (fn, Qt))
3530 {
3531 /* A value of t indicates this hook has a local
3532 binding; it means to run the global binding too.
3533 In a global value, t should not occur. If it
3534 does, we must ignore it to avoid an endless
3535 loop. */
3536 for (fns = Fdefault_value (Qfontification_functions);
3537 CONSP (fns);
3538 fns = XCDR (fns))
3539 {
3540 fn = XCAR (fns);
3541 if (!EQ (fn, Qt))
3542 safe_call1 (fn, pos);
3543 }
3544 }
3545 else
3546 safe_call1 (fn, pos);
3547 }
3548
3549 UNGCPRO;
3550 }
3551
3552 unbind_to (count, Qnil);
3553
3554 /* Fontification functions routinely call `save-restriction'.
3555 Normally, this tags clip_changed, which can confuse redisplay
3556 (see discussion in Bug#6671). Since we don't perform any
3557 special handling of fontification changes in the case where
3558 `save-restriction' isn't called, there's no point doing so in
3559 this case either. So, if the buffer's restrictions are
3560 actually left unchanged, reset clip_changed. */
3561 if (obuf == current_buffer)
3562 {
3563 if (begv == BEGV && zv == ZV)
3564 current_buffer->clip_changed = old_clip_changed;
3565 }
3566 /* There isn't much we can reasonably do to protect against
3567 misbehaving fontification, but here's a fig leaf. */
3568 else if (!NILP (BVAR (obuf, name)))
3569 set_buffer_internal_1 (obuf);
3570
3571 /* The fontification code may have added/removed text.
3572 It could do even a lot worse, but let's at least protect against
3573 the most obvious case where only the text past `pos' gets changed',
3574 as is/was done in grep.el where some escapes sequences are turned
3575 into face properties (bug#7876). */
3576 it->end_charpos = ZV;
3577
3578 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3579 something. This avoids an endless loop if they failed to
3580 fontify the text for which reason ever. */
3581 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3582 handled = HANDLED_RECOMPUTE_PROPS;
3583 }
3584
3585 return handled;
3586 }
3587
3588
3589 \f
3590 /***********************************************************************
3591 Faces
3592 ***********************************************************************/
3593
3594 /* Set up iterator IT from face properties at its current position.
3595 Called from handle_stop. */
3596
3597 static enum prop_handled
3598 handle_face_prop (struct it *it)
3599 {
3600 int new_face_id;
3601 EMACS_INT next_stop;
3602
3603 if (!STRINGP (it->string))
3604 {
3605 new_face_id
3606 = face_at_buffer_position (it->w,
3607 IT_CHARPOS (*it),
3608 it->region_beg_charpos,
3609 it->region_end_charpos,
3610 &next_stop,
3611 (IT_CHARPOS (*it)
3612 + TEXT_PROP_DISTANCE_LIMIT),
3613 0, it->base_face_id);
3614
3615 /* Is this a start of a run of characters with box face?
3616 Caveat: this can be called for a freshly initialized
3617 iterator; face_id is -1 in this case. We know that the new
3618 face will not change until limit, i.e. if the new face has a
3619 box, all characters up to limit will have one. But, as
3620 usual, we don't know whether limit is really the end. */
3621 if (new_face_id != it->face_id)
3622 {
3623 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3624
3625 /* If new face has a box but old face has not, this is
3626 the start of a run of characters with box, i.e. it has
3627 a shadow on the left side. The value of face_id of the
3628 iterator will be -1 if this is the initial call that gets
3629 the face. In this case, we have to look in front of IT's
3630 position and see whether there is a face != new_face_id. */
3631 it->start_of_box_run_p
3632 = (new_face->box != FACE_NO_BOX
3633 && (it->face_id >= 0
3634 || IT_CHARPOS (*it) == BEG
3635 || new_face_id != face_before_it_pos (it)));
3636 it->face_box_p = new_face->box != FACE_NO_BOX;
3637 }
3638 }
3639 else
3640 {
3641 int base_face_id;
3642 EMACS_INT bufpos;
3643 int i;
3644 Lisp_Object from_overlay
3645 = (it->current.overlay_string_index >= 0
3646 ? it->string_overlays[it->current.overlay_string_index]
3647 : Qnil);
3648
3649 /* See if we got to this string directly or indirectly from
3650 an overlay property. That includes the before-string or
3651 after-string of an overlay, strings in display properties
3652 provided by an overlay, their text properties, etc.
3653
3654 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3655 if (! NILP (from_overlay))
3656 for (i = it->sp - 1; i >= 0; i--)
3657 {
3658 if (it->stack[i].current.overlay_string_index >= 0)
3659 from_overlay
3660 = it->string_overlays[it->stack[i].current.overlay_string_index];
3661 else if (! NILP (it->stack[i].from_overlay))
3662 from_overlay = it->stack[i].from_overlay;
3663
3664 if (!NILP (from_overlay))
3665 break;
3666 }
3667
3668 if (! NILP (from_overlay))
3669 {
3670 bufpos = IT_CHARPOS (*it);
3671 /* For a string from an overlay, the base face depends
3672 only on text properties and ignores overlays. */
3673 base_face_id
3674 = face_for_overlay_string (it->w,
3675 IT_CHARPOS (*it),
3676 it->region_beg_charpos,
3677 it->region_end_charpos,
3678 &next_stop,
3679 (IT_CHARPOS (*it)
3680 + TEXT_PROP_DISTANCE_LIMIT),
3681 0,
3682 from_overlay);
3683 }
3684 else
3685 {
3686 bufpos = 0;
3687
3688 /* For strings from a `display' property, use the face at
3689 IT's current buffer position as the base face to merge
3690 with, so that overlay strings appear in the same face as
3691 surrounding text, unless they specify their own
3692 faces. */
3693 base_face_id = it->string_from_prefix_prop_p
3694 ? DEFAULT_FACE_ID
3695 : underlying_face_id (it);
3696 }
3697
3698 new_face_id = face_at_string_position (it->w,
3699 it->string,
3700 IT_STRING_CHARPOS (*it),
3701 bufpos,
3702 it->region_beg_charpos,
3703 it->region_end_charpos,
3704 &next_stop,
3705 base_face_id, 0);
3706
3707 /* Is this a start of a run of characters with box? Caveat:
3708 this can be called for a freshly allocated iterator; face_id
3709 is -1 is this case. We know that the new face will not
3710 change until the next check pos, i.e. if the new face has a
3711 box, all characters up to that position will have a
3712 box. But, as usual, we don't know whether that position
3713 is really the end. */
3714 if (new_face_id != it->face_id)
3715 {
3716 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3717 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3718
3719 /* If new face has a box but old face hasn't, this is the
3720 start of a run of characters with box, i.e. it has a
3721 shadow on the left side. */
3722 it->start_of_box_run_p
3723 = new_face->box && (old_face == NULL || !old_face->box);
3724 it->face_box_p = new_face->box != FACE_NO_BOX;
3725 }
3726 }
3727
3728 it->face_id = new_face_id;
3729 return HANDLED_NORMALLY;
3730 }
3731
3732
3733 /* Return the ID of the face ``underlying'' IT's current position,
3734 which is in a string. If the iterator is associated with a
3735 buffer, return the face at IT's current buffer position.
3736 Otherwise, use the iterator's base_face_id. */
3737
3738 static int
3739 underlying_face_id (struct it *it)
3740 {
3741 int face_id = it->base_face_id, i;
3742
3743 xassert (STRINGP (it->string));
3744
3745 for (i = it->sp - 1; i >= 0; --i)
3746 if (NILP (it->stack[i].string))
3747 face_id = it->stack[i].face_id;
3748
3749 return face_id;
3750 }
3751
3752
3753 /* Compute the face one character before or after the current position
3754 of IT, in the visual order. BEFORE_P non-zero means get the face
3755 in front (to the left in L2R paragraphs, to the right in R2L
3756 paragraphs) of IT's screen position. Value is the ID of the face. */
3757
3758 static int
3759 face_before_or_after_it_pos (struct it *it, int before_p)
3760 {
3761 int face_id, limit;
3762 EMACS_INT next_check_charpos;
3763 struct it it_copy;
3764 void *it_copy_data = NULL;
3765
3766 xassert (it->s == NULL);
3767
3768 if (STRINGP (it->string))
3769 {
3770 EMACS_INT bufpos, charpos;
3771 int base_face_id;
3772
3773 /* No face change past the end of the string (for the case
3774 we are padding with spaces). No face change before the
3775 string start. */
3776 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3777 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3778 return it->face_id;
3779
3780 if (!it->bidi_p)
3781 {
3782 /* Set charpos to the position before or after IT's current
3783 position, in the logical order, which in the non-bidi
3784 case is the same as the visual order. */
3785 if (before_p)
3786 charpos = IT_STRING_CHARPOS (*it) - 1;
3787 else if (it->what == IT_COMPOSITION)
3788 /* For composition, we must check the character after the
3789 composition. */
3790 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3791 else
3792 charpos = IT_STRING_CHARPOS (*it) + 1;
3793 }
3794 else
3795 {
3796 if (before_p)
3797 {
3798 /* With bidi iteration, the character before the current
3799 in the visual order cannot be found by simple
3800 iteration, because "reverse" reordering is not
3801 supported. Instead, we need to use the move_it_*
3802 family of functions. */
3803 /* Ignore face changes before the first visible
3804 character on this display line. */
3805 if (it->current_x <= it->first_visible_x)
3806 return it->face_id;
3807 SAVE_IT (it_copy, *it, it_copy_data);
3808 /* Implementation note: Since move_it_in_display_line
3809 works in the iterator geometry, and thinks the first
3810 character is always the leftmost, even in R2L lines,
3811 we don't need to distinguish between the R2L and L2R
3812 cases here. */
3813 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3814 it_copy.current_x - 1, MOVE_TO_X);
3815 charpos = IT_STRING_CHARPOS (it_copy);
3816 RESTORE_IT (it, it, it_copy_data);
3817 }
3818 else
3819 {
3820 /* Set charpos to the string position of the character
3821 that comes after IT's current position in the visual
3822 order. */
3823 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3824
3825 it_copy = *it;
3826 while (n--)
3827 bidi_move_to_visually_next (&it_copy.bidi_it);
3828
3829 charpos = it_copy.bidi_it.charpos;
3830 }
3831 }
3832 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3833
3834 if (it->current.overlay_string_index >= 0)
3835 bufpos = IT_CHARPOS (*it);
3836 else
3837 bufpos = 0;
3838
3839 base_face_id = underlying_face_id (it);
3840
3841 /* Get the face for ASCII, or unibyte. */
3842 face_id = face_at_string_position (it->w,
3843 it->string,
3844 charpos,
3845 bufpos,
3846 it->region_beg_charpos,
3847 it->region_end_charpos,
3848 &next_check_charpos,
3849 base_face_id, 0);
3850
3851 /* Correct the face for charsets different from ASCII. Do it
3852 for the multibyte case only. The face returned above is
3853 suitable for unibyte text if IT->string is unibyte. */
3854 if (STRING_MULTIBYTE (it->string))
3855 {
3856 struct text_pos pos1 = string_pos (charpos, it->string);
3857 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3858 int c, len;
3859 struct face *face = FACE_FROM_ID (it->f, face_id);
3860
3861 c = string_char_and_length (p, &len);
3862 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3863 }
3864 }
3865 else
3866 {
3867 struct text_pos pos;
3868
3869 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3870 || (IT_CHARPOS (*it) <= BEGV && before_p))
3871 return it->face_id;
3872
3873 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3874 pos = it->current.pos;
3875
3876 if (!it->bidi_p)
3877 {
3878 if (before_p)
3879 DEC_TEXT_POS (pos, it->multibyte_p);
3880 else
3881 {
3882 if (it->what == IT_COMPOSITION)
3883 {
3884 /* For composition, we must check the position after
3885 the composition. */
3886 pos.charpos += it->cmp_it.nchars;
3887 pos.bytepos += it->len;
3888 }
3889 else
3890 INC_TEXT_POS (pos, it->multibyte_p);
3891 }
3892 }
3893 else
3894 {
3895 if (before_p)
3896 {
3897 /* With bidi iteration, the character before the current
3898 in the visual order cannot be found by simple
3899 iteration, because "reverse" reordering is not
3900 supported. Instead, we need to use the move_it_*
3901 family of functions. */
3902 /* Ignore face changes before the first visible
3903 character on this display line. */
3904 if (it->current_x <= it->first_visible_x)
3905 return it->face_id;
3906 SAVE_IT (it_copy, *it, it_copy_data);
3907 /* Implementation note: Since move_it_in_display_line
3908 works in the iterator geometry, and thinks the first
3909 character is always the leftmost, even in R2L lines,
3910 we don't need to distinguish between the R2L and L2R
3911 cases here. */
3912 move_it_in_display_line (&it_copy, ZV,
3913 it_copy.current_x - 1, MOVE_TO_X);
3914 pos = it_copy.current.pos;
3915 RESTORE_IT (it, it, it_copy_data);
3916 }
3917 else
3918 {
3919 /* Set charpos to the buffer position of the character
3920 that comes after IT's current position in the visual
3921 order. */
3922 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3923
3924 it_copy = *it;
3925 while (n--)
3926 bidi_move_to_visually_next (&it_copy.bidi_it);
3927
3928 SET_TEXT_POS (pos,
3929 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3930 }
3931 }
3932 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3933
3934 /* Determine face for CHARSET_ASCII, or unibyte. */
3935 face_id = face_at_buffer_position (it->w,
3936 CHARPOS (pos),
3937 it->region_beg_charpos,
3938 it->region_end_charpos,
3939 &next_check_charpos,
3940 limit, 0, -1);
3941
3942 /* Correct the face for charsets different from ASCII. Do it
3943 for the multibyte case only. The face returned above is
3944 suitable for unibyte text if current_buffer is unibyte. */
3945 if (it->multibyte_p)
3946 {
3947 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3948 struct face *face = FACE_FROM_ID (it->f, face_id);
3949 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3950 }
3951 }
3952
3953 return face_id;
3954 }
3955
3956
3957 \f
3958 /***********************************************************************
3959 Invisible text
3960 ***********************************************************************/
3961
3962 /* Set up iterator IT from invisible properties at its current
3963 position. Called from handle_stop. */
3964
3965 static enum prop_handled
3966 handle_invisible_prop (struct it *it)
3967 {
3968 enum prop_handled handled = HANDLED_NORMALLY;
3969
3970 if (STRINGP (it->string))
3971 {
3972 Lisp_Object prop, end_charpos, limit, charpos;
3973
3974 /* Get the value of the invisible text property at the
3975 current position. Value will be nil if there is no such
3976 property. */
3977 charpos = make_number (IT_STRING_CHARPOS (*it));
3978 prop = Fget_text_property (charpos, Qinvisible, it->string);
3979
3980 if (!NILP (prop)
3981 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3982 {
3983 EMACS_INT endpos;
3984
3985 handled = HANDLED_RECOMPUTE_PROPS;
3986
3987 /* Get the position at which the next change of the
3988 invisible text property can be found in IT->string.
3989 Value will be nil if the property value is the same for
3990 all the rest of IT->string. */
3991 XSETINT (limit, SCHARS (it->string));
3992 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3993 it->string, limit);
3994
3995 /* Text at current position is invisible. The next
3996 change in the property is at position end_charpos.
3997 Move IT's current position to that position. */
3998 if (INTEGERP (end_charpos)
3999 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
4000 {
4001 struct text_pos old;
4002 EMACS_INT oldpos;
4003
4004 old = it->current.string_pos;
4005 oldpos = CHARPOS (old);
4006 if (it->bidi_p)
4007 {
4008 if (it->bidi_it.first_elt
4009 && it->bidi_it.charpos < SCHARS (it->string))
4010 bidi_paragraph_init (it->paragraph_embedding,
4011 &it->bidi_it, 1);
4012 /* Bidi-iterate out of the invisible text. */
4013 do
4014 {
4015 bidi_move_to_visually_next (&it->bidi_it);
4016 }
4017 while (oldpos <= it->bidi_it.charpos
4018 && it->bidi_it.charpos < endpos);
4019
4020 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4021 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4022 if (IT_CHARPOS (*it) >= endpos)
4023 it->prev_stop = endpos;
4024 }
4025 else
4026 {
4027 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4028 compute_string_pos (&it->current.string_pos, old, it->string);
4029 }
4030 }
4031 else
4032 {
4033 /* The rest of the string is invisible. If this is an
4034 overlay string, proceed with the next overlay string
4035 or whatever comes and return a character from there. */
4036 if (it->current.overlay_string_index >= 0)
4037 {
4038 next_overlay_string (it);
4039 /* Don't check for overlay strings when we just
4040 finished processing them. */
4041 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4042 }
4043 else
4044 {
4045 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4046 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4047 }
4048 }
4049 }
4050 }
4051 else
4052 {
4053 int invis_p;
4054 EMACS_INT newpos, next_stop, start_charpos, tem;
4055 Lisp_Object pos, prop, overlay;
4056
4057 /* First of all, is there invisible text at this position? */
4058 tem = start_charpos = IT_CHARPOS (*it);
4059 pos = make_number (tem);
4060 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4061 &overlay);
4062 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4063
4064 /* If we are on invisible text, skip over it. */
4065 if (invis_p && start_charpos < it->end_charpos)
4066 {
4067 /* Record whether we have to display an ellipsis for the
4068 invisible text. */
4069 int display_ellipsis_p = invis_p == 2;
4070
4071 handled = HANDLED_RECOMPUTE_PROPS;
4072
4073 /* Loop skipping over invisible text. The loop is left at
4074 ZV or with IT on the first char being visible again. */
4075 do
4076 {
4077 /* Try to skip some invisible text. Return value is the
4078 position reached which can be equal to where we start
4079 if there is nothing invisible there. This skips both
4080 over invisible text properties and overlays with
4081 invisible property. */
4082 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4083
4084 /* If we skipped nothing at all we weren't at invisible
4085 text in the first place. If everything to the end of
4086 the buffer was skipped, end the loop. */
4087 if (newpos == tem || newpos >= ZV)
4088 invis_p = 0;
4089 else
4090 {
4091 /* We skipped some characters but not necessarily
4092 all there are. Check if we ended up on visible
4093 text. Fget_char_property returns the property of
4094 the char before the given position, i.e. if we
4095 get invis_p = 0, this means that the char at
4096 newpos is visible. */
4097 pos = make_number (newpos);
4098 prop = Fget_char_property (pos, Qinvisible, it->window);
4099 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4100 }
4101
4102 /* If we ended up on invisible text, proceed to
4103 skip starting with next_stop. */
4104 if (invis_p)
4105 tem = next_stop;
4106
4107 /* If there are adjacent invisible texts, don't lose the
4108 second one's ellipsis. */
4109 if (invis_p == 2)
4110 display_ellipsis_p = 1;
4111 }
4112 while (invis_p);
4113
4114 /* The position newpos is now either ZV or on visible text. */
4115 if (it->bidi_p)
4116 {
4117 EMACS_INT bpos = CHAR_TO_BYTE (newpos);
4118 int on_newline =
4119 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4120 int after_newline =
4121 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4122
4123 /* If the invisible text ends on a newline or on a
4124 character after a newline, we can avoid the costly,
4125 character by character, bidi iteration to NEWPOS, and
4126 instead simply reseat the iterator there. That's
4127 because all bidi reordering information is tossed at
4128 the newline. This is a big win for modes that hide
4129 complete lines, like Outline, Org, etc. */
4130 if (on_newline || after_newline)
4131 {
4132 struct text_pos tpos;
4133 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4134
4135 SET_TEXT_POS (tpos, newpos, bpos);
4136 reseat_1 (it, tpos, 0);
4137 /* If we reseat on a newline/ZV, we need to prep the
4138 bidi iterator for advancing to the next character
4139 after the newline/EOB, keeping the current paragraph
4140 direction (so that PRODUCE_GLYPHS does TRT wrt
4141 prepending/appending glyphs to a glyph row). */
4142 if (on_newline)
4143 {
4144 it->bidi_it.first_elt = 0;
4145 it->bidi_it.paragraph_dir = pdir;
4146 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4147 it->bidi_it.nchars = 1;
4148 it->bidi_it.ch_len = 1;
4149 }
4150 }
4151 else /* Must use the slow method. */
4152 {
4153 /* With bidi iteration, the region of invisible text
4154 could start and/or end in the middle of a
4155 non-base embedding level. Therefore, we need to
4156 skip invisible text using the bidi iterator,
4157 starting at IT's current position, until we find
4158 ourselves outside of the invisible text.
4159 Skipping invisible text _after_ bidi iteration
4160 avoids affecting the visual order of the
4161 displayed text when invisible properties are
4162 added or removed. */
4163 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4164 {
4165 /* If we were `reseat'ed to a new paragraph,
4166 determine the paragraph base direction. We
4167 need to do it now because
4168 next_element_from_buffer may not have a
4169 chance to do it, if we are going to skip any
4170 text at the beginning, which resets the
4171 FIRST_ELT flag. */
4172 bidi_paragraph_init (it->paragraph_embedding,
4173 &it->bidi_it, 1);
4174 }
4175 do
4176 {
4177 bidi_move_to_visually_next (&it->bidi_it);
4178 }
4179 while (it->stop_charpos <= it->bidi_it.charpos
4180 && it->bidi_it.charpos < newpos);
4181 IT_CHARPOS (*it) = it->bidi_it.charpos;
4182 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4183 /* If we overstepped NEWPOS, record its position in
4184 the iterator, so that we skip invisible text if
4185 later the bidi iteration lands us in the
4186 invisible region again. */
4187 if (IT_CHARPOS (*it) >= newpos)
4188 it->prev_stop = newpos;
4189 }
4190 }
4191 else
4192 {
4193 IT_CHARPOS (*it) = newpos;
4194 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4195 }
4196
4197 /* If there are before-strings at the start of invisible
4198 text, and the text is invisible because of a text
4199 property, arrange to show before-strings because 20.x did
4200 it that way. (If the text is invisible because of an
4201 overlay property instead of a text property, this is
4202 already handled in the overlay code.) */
4203 if (NILP (overlay)
4204 && get_overlay_strings (it, it->stop_charpos))
4205 {
4206 handled = HANDLED_RECOMPUTE_PROPS;
4207 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4208 }
4209 else if (display_ellipsis_p)
4210 {
4211 /* Make sure that the glyphs of the ellipsis will get
4212 correct `charpos' values. If we would not update
4213 it->position here, the glyphs would belong to the
4214 last visible character _before_ the invisible
4215 text, which confuses `set_cursor_from_row'.
4216
4217 We use the last invisible position instead of the
4218 first because this way the cursor is always drawn on
4219 the first "." of the ellipsis, whenever PT is inside
4220 the invisible text. Otherwise the cursor would be
4221 placed _after_ the ellipsis when the point is after the
4222 first invisible character. */
4223 if (!STRINGP (it->object))
4224 {
4225 it->position.charpos = newpos - 1;
4226 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4227 }
4228 it->ellipsis_p = 1;
4229 /* Let the ellipsis display before
4230 considering any properties of the following char.
4231 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4232 handled = HANDLED_RETURN;
4233 }
4234 }
4235 }
4236
4237 return handled;
4238 }
4239
4240
4241 /* Make iterator IT return `...' next.
4242 Replaces LEN characters from buffer. */
4243
4244 static void
4245 setup_for_ellipsis (struct it *it, int len)
4246 {
4247 /* Use the display table definition for `...'. Invalid glyphs
4248 will be handled by the method returning elements from dpvec. */
4249 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4250 {
4251 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4252 it->dpvec = v->contents;
4253 it->dpend = v->contents + v->header.size;
4254 }
4255 else
4256 {
4257 /* Default `...'. */
4258 it->dpvec = default_invis_vector;
4259 it->dpend = default_invis_vector + 3;
4260 }
4261
4262 it->dpvec_char_len = len;
4263 it->current.dpvec_index = 0;
4264 it->dpvec_face_id = -1;
4265
4266 /* Remember the current face id in case glyphs specify faces.
4267 IT's face is restored in set_iterator_to_next.
4268 saved_face_id was set to preceding char's face in handle_stop. */
4269 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4270 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4271
4272 it->method = GET_FROM_DISPLAY_VECTOR;
4273 it->ellipsis_p = 1;
4274 }
4275
4276
4277 \f
4278 /***********************************************************************
4279 'display' property
4280 ***********************************************************************/
4281
4282 /* Set up iterator IT from `display' property at its current position.
4283 Called from handle_stop.
4284 We return HANDLED_RETURN if some part of the display property
4285 overrides the display of the buffer text itself.
4286 Otherwise we return HANDLED_NORMALLY. */
4287
4288 static enum prop_handled
4289 handle_display_prop (struct it *it)
4290 {
4291 Lisp_Object propval, object, overlay;
4292 struct text_pos *position;
4293 EMACS_INT bufpos;
4294 /* Nonzero if some property replaces the display of the text itself. */
4295 int display_replaced_p = 0;
4296
4297 if (STRINGP (it->string))
4298 {
4299 object = it->string;
4300 position = &it->current.string_pos;
4301 bufpos = CHARPOS (it->current.pos);
4302 }
4303 else
4304 {
4305 XSETWINDOW (object, it->w);
4306 position = &it->current.pos;
4307 bufpos = CHARPOS (*position);
4308 }
4309
4310 /* Reset those iterator values set from display property values. */
4311 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4312 it->space_width = Qnil;
4313 it->font_height = Qnil;
4314 it->voffset = 0;
4315
4316 /* We don't support recursive `display' properties, i.e. string
4317 values that have a string `display' property, that have a string
4318 `display' property etc. */
4319 if (!it->string_from_display_prop_p)
4320 it->area = TEXT_AREA;
4321
4322 propval = get_char_property_and_overlay (make_number (position->charpos),
4323 Qdisplay, object, &overlay);
4324 if (NILP (propval))
4325 return HANDLED_NORMALLY;
4326 /* Now OVERLAY is the overlay that gave us this property, or nil
4327 if it was a text property. */
4328
4329 if (!STRINGP (it->string))
4330 object = it->w->buffer;
4331
4332 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4333 position, bufpos,
4334 FRAME_WINDOW_P (it->f));
4335
4336 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4337 }
4338
4339 /* Subroutine of handle_display_prop. Returns non-zero if the display
4340 specification in SPEC is a replacing specification, i.e. it would
4341 replace the text covered by `display' property with something else,
4342 such as an image or a display string. If SPEC includes any kind or
4343 `(space ...) specification, the value is 2; this is used by
4344 compute_display_string_pos, which see.
4345
4346 See handle_single_display_spec for documentation of arguments.
4347 frame_window_p is non-zero if the window being redisplayed is on a
4348 GUI frame; this argument is used only if IT is NULL, see below.
4349
4350 IT can be NULL, if this is called by the bidi reordering code
4351 through compute_display_string_pos, which see. In that case, this
4352 function only examines SPEC, but does not otherwise "handle" it, in
4353 the sense that it doesn't set up members of IT from the display
4354 spec. */
4355 static int
4356 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4357 Lisp_Object overlay, struct text_pos *position,
4358 EMACS_INT bufpos, int frame_window_p)
4359 {
4360 int replacing_p = 0;
4361 int rv;
4362
4363 if (CONSP (spec)
4364 /* Simple specifications. */
4365 && !EQ (XCAR (spec), Qimage)
4366 && !EQ (XCAR (spec), Qspace)
4367 && !EQ (XCAR (spec), Qwhen)
4368 && !EQ (XCAR (spec), Qslice)
4369 && !EQ (XCAR (spec), Qspace_width)
4370 && !EQ (XCAR (spec), Qheight)
4371 && !EQ (XCAR (spec), Qraise)
4372 /* Marginal area specifications. */
4373 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4374 && !EQ (XCAR (spec), Qleft_fringe)
4375 && !EQ (XCAR (spec), Qright_fringe)
4376 && !NILP (XCAR (spec)))
4377 {
4378 for (; CONSP (spec); spec = XCDR (spec))
4379 {
4380 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4381 overlay, position, bufpos,
4382 replacing_p, frame_window_p)))
4383 {
4384 replacing_p = rv;
4385 /* If some text in a string is replaced, `position' no
4386 longer points to the position of `object'. */
4387 if (!it || STRINGP (object))
4388 break;
4389 }
4390 }
4391 }
4392 else if (VECTORP (spec))
4393 {
4394 int i;
4395 for (i = 0; i < ASIZE (spec); ++i)
4396 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4397 overlay, position, bufpos,
4398 replacing_p, frame_window_p)))
4399 {
4400 replacing_p = rv;
4401 /* If some text in a string is replaced, `position' no
4402 longer points to the position of `object'. */
4403 if (!it || STRINGP (object))
4404 break;
4405 }
4406 }
4407 else
4408 {
4409 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4410 position, bufpos, 0,
4411 frame_window_p)))
4412 replacing_p = rv;
4413 }
4414
4415 return replacing_p;
4416 }
4417
4418 /* Value is the position of the end of the `display' property starting
4419 at START_POS in OBJECT. */
4420
4421 static struct text_pos
4422 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4423 {
4424 Lisp_Object end;
4425 struct text_pos end_pos;
4426
4427 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4428 Qdisplay, object, Qnil);
4429 CHARPOS (end_pos) = XFASTINT (end);
4430 if (STRINGP (object))
4431 compute_string_pos (&end_pos, start_pos, it->string);
4432 else
4433 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4434
4435 return end_pos;
4436 }
4437
4438
4439 /* Set up IT from a single `display' property specification SPEC. OBJECT
4440 is the object in which the `display' property was found. *POSITION
4441 is the position in OBJECT at which the `display' property was found.
4442 BUFPOS is the buffer position of OBJECT (different from POSITION if
4443 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4444 previously saw a display specification which already replaced text
4445 display with something else, for example an image; we ignore such
4446 properties after the first one has been processed.
4447
4448 OVERLAY is the overlay this `display' property came from,
4449 or nil if it was a text property.
4450
4451 If SPEC is a `space' or `image' specification, and in some other
4452 cases too, set *POSITION to the position where the `display'
4453 property ends.
4454
4455 If IT is NULL, only examine the property specification in SPEC, but
4456 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4457 is intended to be displayed in a window on a GUI frame.
4458
4459 Value is non-zero if something was found which replaces the display
4460 of buffer or string text. */
4461
4462 static int
4463 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4464 Lisp_Object overlay, struct text_pos *position,
4465 EMACS_INT bufpos, int display_replaced_p,
4466 int frame_window_p)
4467 {
4468 Lisp_Object form;
4469 Lisp_Object location, value;
4470 struct text_pos start_pos = *position;
4471 int valid_p;
4472
4473 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4474 If the result is non-nil, use VALUE instead of SPEC. */
4475 form = Qt;
4476 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4477 {
4478 spec = XCDR (spec);
4479 if (!CONSP (spec))
4480 return 0;
4481 form = XCAR (spec);
4482 spec = XCDR (spec);
4483 }
4484
4485 if (!NILP (form) && !EQ (form, Qt))
4486 {
4487 int count = SPECPDL_INDEX ();
4488 struct gcpro gcpro1;
4489
4490 /* Bind `object' to the object having the `display' property, a
4491 buffer or string. Bind `position' to the position in the
4492 object where the property was found, and `buffer-position'
4493 to the current position in the buffer. */
4494
4495 if (NILP (object))
4496 XSETBUFFER (object, current_buffer);
4497 specbind (Qobject, object);
4498 specbind (Qposition, make_number (CHARPOS (*position)));
4499 specbind (Qbuffer_position, make_number (bufpos));
4500 GCPRO1 (form);
4501 form = safe_eval (form);
4502 UNGCPRO;
4503 unbind_to (count, Qnil);
4504 }
4505
4506 if (NILP (form))
4507 return 0;
4508
4509 /* Handle `(height HEIGHT)' specifications. */
4510 if (CONSP (spec)
4511 && EQ (XCAR (spec), Qheight)
4512 && CONSP (XCDR (spec)))
4513 {
4514 if (it)
4515 {
4516 if (!FRAME_WINDOW_P (it->f))
4517 return 0;
4518
4519 it->font_height = XCAR (XCDR (spec));
4520 if (!NILP (it->font_height))
4521 {
4522 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4523 int new_height = -1;
4524
4525 if (CONSP (it->font_height)
4526 && (EQ (XCAR (it->font_height), Qplus)
4527 || EQ (XCAR (it->font_height), Qminus))
4528 && CONSP (XCDR (it->font_height))
4529 && INTEGERP (XCAR (XCDR (it->font_height))))
4530 {
4531 /* `(+ N)' or `(- N)' where N is an integer. */
4532 int steps = XINT (XCAR (XCDR (it->font_height)));
4533 if (EQ (XCAR (it->font_height), Qplus))
4534 steps = - steps;
4535 it->face_id = smaller_face (it->f, it->face_id, steps);
4536 }
4537 else if (FUNCTIONP (it->font_height))
4538 {
4539 /* Call function with current height as argument.
4540 Value is the new height. */
4541 Lisp_Object height;
4542 height = safe_call1 (it->font_height,
4543 face->lface[LFACE_HEIGHT_INDEX]);
4544 if (NUMBERP (height))
4545 new_height = XFLOATINT (height);
4546 }
4547 else if (NUMBERP (it->font_height))
4548 {
4549 /* Value is a multiple of the canonical char height. */
4550 struct face *f;
4551
4552 f = FACE_FROM_ID (it->f,
4553 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4554 new_height = (XFLOATINT (it->font_height)
4555 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4556 }
4557 else
4558 {
4559 /* Evaluate IT->font_height with `height' bound to the
4560 current specified height to get the new height. */
4561 int count = SPECPDL_INDEX ();
4562
4563 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4564 value = safe_eval (it->font_height);
4565 unbind_to (count, Qnil);
4566
4567 if (NUMBERP (value))
4568 new_height = XFLOATINT (value);
4569 }
4570
4571 if (new_height > 0)
4572 it->face_id = face_with_height (it->f, it->face_id, new_height);
4573 }
4574 }
4575
4576 return 0;
4577 }
4578
4579 /* Handle `(space-width WIDTH)'. */
4580 if (CONSP (spec)
4581 && EQ (XCAR (spec), Qspace_width)
4582 && CONSP (XCDR (spec)))
4583 {
4584 if (it)
4585 {
4586 if (!FRAME_WINDOW_P (it->f))
4587 return 0;
4588
4589 value = XCAR (XCDR (spec));
4590 if (NUMBERP (value) && XFLOATINT (value) > 0)
4591 it->space_width = value;
4592 }
4593
4594 return 0;
4595 }
4596
4597 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4598 if (CONSP (spec)
4599 && EQ (XCAR (spec), Qslice))
4600 {
4601 Lisp_Object tem;
4602
4603 if (it)
4604 {
4605 if (!FRAME_WINDOW_P (it->f))
4606 return 0;
4607
4608 if (tem = XCDR (spec), CONSP (tem))
4609 {
4610 it->slice.x = XCAR (tem);
4611 if (tem = XCDR (tem), CONSP (tem))
4612 {
4613 it->slice.y = XCAR (tem);
4614 if (tem = XCDR (tem), CONSP (tem))
4615 {
4616 it->slice.width = XCAR (tem);
4617 if (tem = XCDR (tem), CONSP (tem))
4618 it->slice.height = XCAR (tem);
4619 }
4620 }
4621 }
4622 }
4623
4624 return 0;
4625 }
4626
4627 /* Handle `(raise FACTOR)'. */
4628 if (CONSP (spec)
4629 && EQ (XCAR (spec), Qraise)
4630 && CONSP (XCDR (spec)))
4631 {
4632 if (it)
4633 {
4634 if (!FRAME_WINDOW_P (it->f))
4635 return 0;
4636
4637 #ifdef HAVE_WINDOW_SYSTEM
4638 value = XCAR (XCDR (spec));
4639 if (NUMBERP (value))
4640 {
4641 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4642 it->voffset = - (XFLOATINT (value)
4643 * (FONT_HEIGHT (face->font)));
4644 }
4645 #endif /* HAVE_WINDOW_SYSTEM */
4646 }
4647
4648 return 0;
4649 }
4650
4651 /* Don't handle the other kinds of display specifications
4652 inside a string that we got from a `display' property. */
4653 if (it && it->string_from_display_prop_p)
4654 return 0;
4655
4656 /* Characters having this form of property are not displayed, so
4657 we have to find the end of the property. */
4658 if (it)
4659 {
4660 start_pos = *position;
4661 *position = display_prop_end (it, object, start_pos);
4662 }
4663 value = Qnil;
4664
4665 /* Stop the scan at that end position--we assume that all
4666 text properties change there. */
4667 if (it)
4668 it->stop_charpos = position->charpos;
4669
4670 /* Handle `(left-fringe BITMAP [FACE])'
4671 and `(right-fringe BITMAP [FACE])'. */
4672 if (CONSP (spec)
4673 && (EQ (XCAR (spec), Qleft_fringe)
4674 || EQ (XCAR (spec), Qright_fringe))
4675 && CONSP (XCDR (spec)))
4676 {
4677 int fringe_bitmap;
4678
4679 if (it)
4680 {
4681 if (!FRAME_WINDOW_P (it->f))
4682 /* If we return here, POSITION has been advanced
4683 across the text with this property. */
4684 return 0;
4685 }
4686 else if (!frame_window_p)
4687 return 0;
4688
4689 #ifdef HAVE_WINDOW_SYSTEM
4690 value = XCAR (XCDR (spec));
4691 if (!SYMBOLP (value)
4692 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4693 /* If we return here, POSITION has been advanced
4694 across the text with this property. */
4695 return 0;
4696
4697 if (it)
4698 {
4699 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4700
4701 if (CONSP (XCDR (XCDR (spec))))
4702 {
4703 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4704 int face_id2 = lookup_derived_face (it->f, face_name,
4705 FRINGE_FACE_ID, 0);
4706 if (face_id2 >= 0)
4707 face_id = face_id2;
4708 }
4709
4710 /* Save current settings of IT so that we can restore them
4711 when we are finished with the glyph property value. */
4712 push_it (it, position);
4713
4714 it->area = TEXT_AREA;
4715 it->what = IT_IMAGE;
4716 it->image_id = -1; /* no image */
4717 it->position = start_pos;
4718 it->object = NILP (object) ? it->w->buffer : object;
4719 it->method = GET_FROM_IMAGE;
4720 it->from_overlay = Qnil;
4721 it->face_id = face_id;
4722 it->from_disp_prop_p = 1;
4723
4724 /* Say that we haven't consumed the characters with
4725 `display' property yet. The call to pop_it in
4726 set_iterator_to_next will clean this up. */
4727 *position = start_pos;
4728
4729 if (EQ (XCAR (spec), Qleft_fringe))
4730 {
4731 it->left_user_fringe_bitmap = fringe_bitmap;
4732 it->left_user_fringe_face_id = face_id;
4733 }
4734 else
4735 {
4736 it->right_user_fringe_bitmap = fringe_bitmap;
4737 it->right_user_fringe_face_id = face_id;
4738 }
4739 }
4740 #endif /* HAVE_WINDOW_SYSTEM */
4741 return 1;
4742 }
4743
4744 /* Prepare to handle `((margin left-margin) ...)',
4745 `((margin right-margin) ...)' and `((margin nil) ...)'
4746 prefixes for display specifications. */
4747 location = Qunbound;
4748 if (CONSP (spec) && CONSP (XCAR (spec)))
4749 {
4750 Lisp_Object tem;
4751
4752 value = XCDR (spec);
4753 if (CONSP (value))
4754 value = XCAR (value);
4755
4756 tem = XCAR (spec);
4757 if (EQ (XCAR (tem), Qmargin)
4758 && (tem = XCDR (tem),
4759 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4760 (NILP (tem)
4761 || EQ (tem, Qleft_margin)
4762 || EQ (tem, Qright_margin))))
4763 location = tem;
4764 }
4765
4766 if (EQ (location, Qunbound))
4767 {
4768 location = Qnil;
4769 value = spec;
4770 }
4771
4772 /* After this point, VALUE is the property after any
4773 margin prefix has been stripped. It must be a string,
4774 an image specification, or `(space ...)'.
4775
4776 LOCATION specifies where to display: `left-margin',
4777 `right-margin' or nil. */
4778
4779 valid_p = (STRINGP (value)
4780 #ifdef HAVE_WINDOW_SYSTEM
4781 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4782 && valid_image_p (value))
4783 #endif /* not HAVE_WINDOW_SYSTEM */
4784 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4785
4786 if (valid_p && !display_replaced_p)
4787 {
4788 int retval = 1;
4789
4790 if (!it)
4791 {
4792 /* Callers need to know whether the display spec is any kind
4793 of `(space ...)' spec that is about to affect text-area
4794 display. */
4795 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4796 retval = 2;
4797 return retval;
4798 }
4799
4800 /* Save current settings of IT so that we can restore them
4801 when we are finished with the glyph property value. */
4802 push_it (it, position);
4803 it->from_overlay = overlay;
4804 it->from_disp_prop_p = 1;
4805
4806 if (NILP (location))
4807 it->area = TEXT_AREA;
4808 else if (EQ (location, Qleft_margin))
4809 it->area = LEFT_MARGIN_AREA;
4810 else
4811 it->area = RIGHT_MARGIN_AREA;
4812
4813 if (STRINGP (value))
4814 {
4815 it->string = value;
4816 it->multibyte_p = STRING_MULTIBYTE (it->string);
4817 it->current.overlay_string_index = -1;
4818 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4819 it->end_charpos = it->string_nchars = SCHARS (it->string);
4820 it->method = GET_FROM_STRING;
4821 it->stop_charpos = 0;
4822 it->prev_stop = 0;
4823 it->base_level_stop = 0;
4824 it->string_from_display_prop_p = 1;
4825 /* Say that we haven't consumed the characters with
4826 `display' property yet. The call to pop_it in
4827 set_iterator_to_next will clean this up. */
4828 if (BUFFERP (object))
4829 *position = start_pos;
4830
4831 /* Force paragraph direction to be that of the parent
4832 object. If the parent object's paragraph direction is
4833 not yet determined, default to L2R. */
4834 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4835 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4836 else
4837 it->paragraph_embedding = L2R;
4838
4839 /* Set up the bidi iterator for this display string. */
4840 if (it->bidi_p)
4841 {
4842 it->bidi_it.string.lstring = it->string;
4843 it->bidi_it.string.s = NULL;
4844 it->bidi_it.string.schars = it->end_charpos;
4845 it->bidi_it.string.bufpos = bufpos;
4846 it->bidi_it.string.from_disp_str = 1;
4847 it->bidi_it.string.unibyte = !it->multibyte_p;
4848 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4849 }
4850 }
4851 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4852 {
4853 it->method = GET_FROM_STRETCH;
4854 it->object = value;
4855 *position = it->position = start_pos;
4856 retval = 1 + (it->area == TEXT_AREA);
4857 }
4858 #ifdef HAVE_WINDOW_SYSTEM
4859 else
4860 {
4861 it->what = IT_IMAGE;
4862 it->image_id = lookup_image (it->f, value);
4863 it->position = start_pos;
4864 it->object = NILP (object) ? it->w->buffer : object;
4865 it->method = GET_FROM_IMAGE;
4866
4867 /* Say that we haven't consumed the characters with
4868 `display' property yet. The call to pop_it in
4869 set_iterator_to_next will clean this up. */
4870 *position = start_pos;
4871 }
4872 #endif /* HAVE_WINDOW_SYSTEM */
4873
4874 return retval;
4875 }
4876
4877 /* Invalid property or property not supported. Restore
4878 POSITION to what it was before. */
4879 *position = start_pos;
4880 return 0;
4881 }
4882
4883 /* Check if PROP is a display property value whose text should be
4884 treated as intangible. OVERLAY is the overlay from which PROP
4885 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4886 specify the buffer position covered by PROP. */
4887
4888 int
4889 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4890 EMACS_INT charpos, EMACS_INT bytepos)
4891 {
4892 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4893 struct text_pos position;
4894
4895 SET_TEXT_POS (position, charpos, bytepos);
4896 return handle_display_spec (NULL, prop, Qnil, overlay,
4897 &position, charpos, frame_window_p);
4898 }
4899
4900
4901 /* Return 1 if PROP is a display sub-property value containing STRING.
4902
4903 Implementation note: this and the following function are really
4904 special cases of handle_display_spec and
4905 handle_single_display_spec, and should ideally use the same code.
4906 Until they do, these two pairs must be consistent and must be
4907 modified in sync. */
4908
4909 static int
4910 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4911 {
4912 if (EQ (string, prop))
4913 return 1;
4914
4915 /* Skip over `when FORM'. */
4916 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4917 {
4918 prop = XCDR (prop);
4919 if (!CONSP (prop))
4920 return 0;
4921 /* Actually, the condition following `when' should be eval'ed,
4922 like handle_single_display_spec does, and we should return
4923 zero if it evaluates to nil. However, this function is
4924 called only when the buffer was already displayed and some
4925 glyph in the glyph matrix was found to come from a display
4926 string. Therefore, the condition was already evaluated, and
4927 the result was non-nil, otherwise the display string wouldn't
4928 have been displayed and we would have never been called for
4929 this property. Thus, we can skip the evaluation and assume
4930 its result is non-nil. */
4931 prop = XCDR (prop);
4932 }
4933
4934 if (CONSP (prop))
4935 /* Skip over `margin LOCATION'. */
4936 if (EQ (XCAR (prop), Qmargin))
4937 {
4938 prop = XCDR (prop);
4939 if (!CONSP (prop))
4940 return 0;
4941
4942 prop = XCDR (prop);
4943 if (!CONSP (prop))
4944 return 0;
4945 }
4946
4947 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4948 }
4949
4950
4951 /* Return 1 if STRING appears in the `display' property PROP. */
4952
4953 static int
4954 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4955 {
4956 if (CONSP (prop)
4957 && !EQ (XCAR (prop), Qwhen)
4958 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4959 {
4960 /* A list of sub-properties. */
4961 while (CONSP (prop))
4962 {
4963 if (single_display_spec_string_p (XCAR (prop), string))
4964 return 1;
4965 prop = XCDR (prop);
4966 }
4967 }
4968 else if (VECTORP (prop))
4969 {
4970 /* A vector of sub-properties. */
4971 int i;
4972 for (i = 0; i < ASIZE (prop); ++i)
4973 if (single_display_spec_string_p (AREF (prop, i), string))
4974 return 1;
4975 }
4976 else
4977 return single_display_spec_string_p (prop, string);
4978
4979 return 0;
4980 }
4981
4982 /* Look for STRING in overlays and text properties in the current
4983 buffer, between character positions FROM and TO (excluding TO).
4984 BACK_P non-zero means look back (in this case, TO is supposed to be
4985 less than FROM).
4986 Value is the first character position where STRING was found, or
4987 zero if it wasn't found before hitting TO.
4988
4989 This function may only use code that doesn't eval because it is
4990 called asynchronously from note_mouse_highlight. */
4991
4992 static EMACS_INT
4993 string_buffer_position_lim (Lisp_Object string,
4994 EMACS_INT from, EMACS_INT to, int back_p)
4995 {
4996 Lisp_Object limit, prop, pos;
4997 int found = 0;
4998
4999 pos = make_number (max (from, BEGV));
5000
5001 if (!back_p) /* looking forward */
5002 {
5003 limit = make_number (min (to, ZV));
5004 while (!found && !EQ (pos, limit))
5005 {
5006 prop = Fget_char_property (pos, Qdisplay, Qnil);
5007 if (!NILP (prop) && display_prop_string_p (prop, string))
5008 found = 1;
5009 else
5010 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5011 limit);
5012 }
5013 }
5014 else /* looking back */
5015 {
5016 limit = make_number (max (to, BEGV));
5017 while (!found && !EQ (pos, limit))
5018 {
5019 prop = Fget_char_property (pos, Qdisplay, Qnil);
5020 if (!NILP (prop) && display_prop_string_p (prop, string))
5021 found = 1;
5022 else
5023 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5024 limit);
5025 }
5026 }
5027
5028 return found ? XINT (pos) : 0;
5029 }
5030
5031 /* Determine which buffer position in current buffer STRING comes from.
5032 AROUND_CHARPOS is an approximate position where it could come from.
5033 Value is the buffer position or 0 if it couldn't be determined.
5034
5035 This function is necessary because we don't record buffer positions
5036 in glyphs generated from strings (to keep struct glyph small).
5037 This function may only use code that doesn't eval because it is
5038 called asynchronously from note_mouse_highlight. */
5039
5040 static EMACS_INT
5041 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
5042 {
5043 const int MAX_DISTANCE = 1000;
5044 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
5045 around_charpos + MAX_DISTANCE,
5046 0);
5047
5048 if (!found)
5049 found = string_buffer_position_lim (string, around_charpos,
5050 around_charpos - MAX_DISTANCE, 1);
5051 return found;
5052 }
5053
5054
5055 \f
5056 /***********************************************************************
5057 `composition' property
5058 ***********************************************************************/
5059
5060 /* Set up iterator IT from `composition' property at its current
5061 position. Called from handle_stop. */
5062
5063 static enum prop_handled
5064 handle_composition_prop (struct it *it)
5065 {
5066 Lisp_Object prop, string;
5067 EMACS_INT pos, pos_byte, start, end;
5068
5069 if (STRINGP (it->string))
5070 {
5071 unsigned char *s;
5072
5073 pos = IT_STRING_CHARPOS (*it);
5074 pos_byte = IT_STRING_BYTEPOS (*it);
5075 string = it->string;
5076 s = SDATA (string) + pos_byte;
5077 it->c = STRING_CHAR (s);
5078 }
5079 else
5080 {
5081 pos = IT_CHARPOS (*it);
5082 pos_byte = IT_BYTEPOS (*it);
5083 string = Qnil;
5084 it->c = FETCH_CHAR (pos_byte);
5085 }
5086
5087 /* If there's a valid composition and point is not inside of the
5088 composition (in the case that the composition is from the current
5089 buffer), draw a glyph composed from the composition components. */
5090 if (find_composition (pos, -1, &start, &end, &prop, string)
5091 && COMPOSITION_VALID_P (start, end, prop)
5092 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5093 {
5094 if (start < pos)
5095 /* As we can't handle this situation (perhaps font-lock added
5096 a new composition), we just return here hoping that next
5097 redisplay will detect this composition much earlier. */
5098 return HANDLED_NORMALLY;
5099 if (start != pos)
5100 {
5101 if (STRINGP (it->string))
5102 pos_byte = string_char_to_byte (it->string, start);
5103 else
5104 pos_byte = CHAR_TO_BYTE (start);
5105 }
5106 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5107 prop, string);
5108
5109 if (it->cmp_it.id >= 0)
5110 {
5111 it->cmp_it.ch = -1;
5112 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5113 it->cmp_it.nglyphs = -1;
5114 }
5115 }
5116
5117 return HANDLED_NORMALLY;
5118 }
5119
5120
5121 \f
5122 /***********************************************************************
5123 Overlay strings
5124 ***********************************************************************/
5125
5126 /* The following structure is used to record overlay strings for
5127 later sorting in load_overlay_strings. */
5128
5129 struct overlay_entry
5130 {
5131 Lisp_Object overlay;
5132 Lisp_Object string;
5133 int priority;
5134 int after_string_p;
5135 };
5136
5137
5138 /* Set up iterator IT from overlay strings at its current position.
5139 Called from handle_stop. */
5140
5141 static enum prop_handled
5142 handle_overlay_change (struct it *it)
5143 {
5144 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5145 return HANDLED_RECOMPUTE_PROPS;
5146 else
5147 return HANDLED_NORMALLY;
5148 }
5149
5150
5151 /* Set up the next overlay string for delivery by IT, if there is an
5152 overlay string to deliver. Called by set_iterator_to_next when the
5153 end of the current overlay string is reached. If there are more
5154 overlay strings to display, IT->string and
5155 IT->current.overlay_string_index are set appropriately here.
5156 Otherwise IT->string is set to nil. */
5157
5158 static void
5159 next_overlay_string (struct it *it)
5160 {
5161 ++it->current.overlay_string_index;
5162 if (it->current.overlay_string_index == it->n_overlay_strings)
5163 {
5164 /* No more overlay strings. Restore IT's settings to what
5165 they were before overlay strings were processed, and
5166 continue to deliver from current_buffer. */
5167
5168 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5169 pop_it (it);
5170 xassert (it->sp > 0
5171 || (NILP (it->string)
5172 && it->method == GET_FROM_BUFFER
5173 && it->stop_charpos >= BEGV
5174 && it->stop_charpos <= it->end_charpos));
5175 it->current.overlay_string_index = -1;
5176 it->n_overlay_strings = 0;
5177 it->overlay_strings_charpos = -1;
5178 /* If there's an empty display string on the stack, pop the
5179 stack, to resync the bidi iterator with IT's position. Such
5180 empty strings are pushed onto the stack in
5181 get_overlay_strings_1. */
5182 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5183 pop_it (it);
5184
5185 /* If we're at the end of the buffer, record that we have
5186 processed the overlay strings there already, so that
5187 next_element_from_buffer doesn't try it again. */
5188 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5189 it->overlay_strings_at_end_processed_p = 1;
5190 }
5191 else
5192 {
5193 /* There are more overlay strings to process. If
5194 IT->current.overlay_string_index has advanced to a position
5195 where we must load IT->overlay_strings with more strings, do
5196 it. We must load at the IT->overlay_strings_charpos where
5197 IT->n_overlay_strings was originally computed; when invisible
5198 text is present, this might not be IT_CHARPOS (Bug#7016). */
5199 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5200
5201 if (it->current.overlay_string_index && i == 0)
5202 load_overlay_strings (it, it->overlay_strings_charpos);
5203
5204 /* Initialize IT to deliver display elements from the overlay
5205 string. */
5206 it->string = it->overlay_strings[i];
5207 it->multibyte_p = STRING_MULTIBYTE (it->string);
5208 SET_TEXT_POS (it->current.string_pos, 0, 0);
5209 it->method = GET_FROM_STRING;
5210 it->stop_charpos = 0;
5211 if (it->cmp_it.stop_pos >= 0)
5212 it->cmp_it.stop_pos = 0;
5213 it->prev_stop = 0;
5214 it->base_level_stop = 0;
5215
5216 /* Set up the bidi iterator for this overlay string. */
5217 if (it->bidi_p)
5218 {
5219 it->bidi_it.string.lstring = it->string;
5220 it->bidi_it.string.s = NULL;
5221 it->bidi_it.string.schars = SCHARS (it->string);
5222 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5223 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5224 it->bidi_it.string.unibyte = !it->multibyte_p;
5225 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5226 }
5227 }
5228
5229 CHECK_IT (it);
5230 }
5231
5232
5233 /* Compare two overlay_entry structures E1 and E2. Used as a
5234 comparison function for qsort in load_overlay_strings. Overlay
5235 strings for the same position are sorted so that
5236
5237 1. All after-strings come in front of before-strings, except
5238 when they come from the same overlay.
5239
5240 2. Within after-strings, strings are sorted so that overlay strings
5241 from overlays with higher priorities come first.
5242
5243 2. Within before-strings, strings are sorted so that overlay
5244 strings from overlays with higher priorities come last.
5245
5246 Value is analogous to strcmp. */
5247
5248
5249 static int
5250 compare_overlay_entries (const void *e1, const void *e2)
5251 {
5252 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5253 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5254 int result;
5255
5256 if (entry1->after_string_p != entry2->after_string_p)
5257 {
5258 /* Let after-strings appear in front of before-strings if
5259 they come from different overlays. */
5260 if (EQ (entry1->overlay, entry2->overlay))
5261 result = entry1->after_string_p ? 1 : -1;
5262 else
5263 result = entry1->after_string_p ? -1 : 1;
5264 }
5265 else if (entry1->after_string_p)
5266 /* After-strings sorted in order of decreasing priority. */
5267 result = entry2->priority - entry1->priority;
5268 else
5269 /* Before-strings sorted in order of increasing priority. */
5270 result = entry1->priority - entry2->priority;
5271
5272 return result;
5273 }
5274
5275
5276 /* Load the vector IT->overlay_strings with overlay strings from IT's
5277 current buffer position, or from CHARPOS if that is > 0. Set
5278 IT->n_overlays to the total number of overlay strings found.
5279
5280 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5281 a time. On entry into load_overlay_strings,
5282 IT->current.overlay_string_index gives the number of overlay
5283 strings that have already been loaded by previous calls to this
5284 function.
5285
5286 IT->add_overlay_start contains an additional overlay start
5287 position to consider for taking overlay strings from, if non-zero.
5288 This position comes into play when the overlay has an `invisible'
5289 property, and both before and after-strings. When we've skipped to
5290 the end of the overlay, because of its `invisible' property, we
5291 nevertheless want its before-string to appear.
5292 IT->add_overlay_start will contain the overlay start position
5293 in this case.
5294
5295 Overlay strings are sorted so that after-string strings come in
5296 front of before-string strings. Within before and after-strings,
5297 strings are sorted by overlay priority. See also function
5298 compare_overlay_entries. */
5299
5300 static void
5301 load_overlay_strings (struct it *it, EMACS_INT charpos)
5302 {
5303 Lisp_Object overlay, window, str, invisible;
5304 struct Lisp_Overlay *ov;
5305 EMACS_INT start, end;
5306 int size = 20;
5307 int n = 0, i, j, invis_p;
5308 struct overlay_entry *entries
5309 = (struct overlay_entry *) alloca (size * sizeof *entries);
5310
5311 if (charpos <= 0)
5312 charpos = IT_CHARPOS (*it);
5313
5314 /* Append the overlay string STRING of overlay OVERLAY to vector
5315 `entries' which has size `size' and currently contains `n'
5316 elements. AFTER_P non-zero means STRING is an after-string of
5317 OVERLAY. */
5318 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5319 do \
5320 { \
5321 Lisp_Object priority; \
5322 \
5323 if (n == size) \
5324 { \
5325 int new_size = 2 * size; \
5326 struct overlay_entry *old = entries; \
5327 entries = \
5328 (struct overlay_entry *) alloca (new_size \
5329 * sizeof *entries); \
5330 memcpy (entries, old, size * sizeof *entries); \
5331 size = new_size; \
5332 } \
5333 \
5334 entries[n].string = (STRING); \
5335 entries[n].overlay = (OVERLAY); \
5336 priority = Foverlay_get ((OVERLAY), Qpriority); \
5337 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5338 entries[n].after_string_p = (AFTER_P); \
5339 ++n; \
5340 } \
5341 while (0)
5342
5343 /* Process overlay before the overlay center. */
5344 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5345 {
5346 XSETMISC (overlay, ov);
5347 xassert (OVERLAYP (overlay));
5348 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5349 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5350
5351 if (end < charpos)
5352 break;
5353
5354 /* Skip this overlay if it doesn't start or end at IT's current
5355 position. */
5356 if (end != charpos && start != charpos)
5357 continue;
5358
5359 /* Skip this overlay if it doesn't apply to IT->w. */
5360 window = Foverlay_get (overlay, Qwindow);
5361 if (WINDOWP (window) && XWINDOW (window) != it->w)
5362 continue;
5363
5364 /* If the text ``under'' the overlay is invisible, both before-
5365 and after-strings from this overlay are visible; start and
5366 end position are indistinguishable. */
5367 invisible = Foverlay_get (overlay, Qinvisible);
5368 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5369
5370 /* If overlay has a non-empty before-string, record it. */
5371 if ((start == charpos || (end == charpos && invis_p))
5372 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5373 && SCHARS (str))
5374 RECORD_OVERLAY_STRING (overlay, str, 0);
5375
5376 /* If overlay has a non-empty after-string, record it. */
5377 if ((end == charpos || (start == charpos && invis_p))
5378 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5379 && SCHARS (str))
5380 RECORD_OVERLAY_STRING (overlay, str, 1);
5381 }
5382
5383 /* Process overlays after the overlay center. */
5384 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5385 {
5386 XSETMISC (overlay, ov);
5387 xassert (OVERLAYP (overlay));
5388 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5389 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5390
5391 if (start > charpos)
5392 break;
5393
5394 /* Skip this overlay if it doesn't start or end at IT's current
5395 position. */
5396 if (end != charpos && start != charpos)
5397 continue;
5398
5399 /* Skip this overlay if it doesn't apply to IT->w. */
5400 window = Foverlay_get (overlay, Qwindow);
5401 if (WINDOWP (window) && XWINDOW (window) != it->w)
5402 continue;
5403
5404 /* If the text ``under'' the overlay is invisible, it has a zero
5405 dimension, and both before- and after-strings apply. */
5406 invisible = Foverlay_get (overlay, Qinvisible);
5407 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5408
5409 /* If overlay has a non-empty before-string, record it. */
5410 if ((start == charpos || (end == charpos && invis_p))
5411 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5412 && SCHARS (str))
5413 RECORD_OVERLAY_STRING (overlay, str, 0);
5414
5415 /* If overlay has a non-empty after-string, record it. */
5416 if ((end == charpos || (start == charpos && invis_p))
5417 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5418 && SCHARS (str))
5419 RECORD_OVERLAY_STRING (overlay, str, 1);
5420 }
5421
5422 #undef RECORD_OVERLAY_STRING
5423
5424 /* Sort entries. */
5425 if (n > 1)
5426 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5427
5428 /* Record number of overlay strings, and where we computed it. */
5429 it->n_overlay_strings = n;
5430 it->overlay_strings_charpos = charpos;
5431
5432 /* IT->current.overlay_string_index is the number of overlay strings
5433 that have already been consumed by IT. Copy some of the
5434 remaining overlay strings to IT->overlay_strings. */
5435 i = 0;
5436 j = it->current.overlay_string_index;
5437 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5438 {
5439 it->overlay_strings[i] = entries[j].string;
5440 it->string_overlays[i++] = entries[j++].overlay;
5441 }
5442
5443 CHECK_IT (it);
5444 }
5445
5446
5447 /* Get the first chunk of overlay strings at IT's current buffer
5448 position, or at CHARPOS if that is > 0. Value is non-zero if at
5449 least one overlay string was found. */
5450
5451 static int
5452 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5453 {
5454 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5455 process. This fills IT->overlay_strings with strings, and sets
5456 IT->n_overlay_strings to the total number of strings to process.
5457 IT->pos.overlay_string_index has to be set temporarily to zero
5458 because load_overlay_strings needs this; it must be set to -1
5459 when no overlay strings are found because a zero value would
5460 indicate a position in the first overlay string. */
5461 it->current.overlay_string_index = 0;
5462 load_overlay_strings (it, charpos);
5463
5464 /* If we found overlay strings, set up IT to deliver display
5465 elements from the first one. Otherwise set up IT to deliver
5466 from current_buffer. */
5467 if (it->n_overlay_strings)
5468 {
5469 /* Make sure we know settings in current_buffer, so that we can
5470 restore meaningful values when we're done with the overlay
5471 strings. */
5472 if (compute_stop_p)
5473 compute_stop_pos (it);
5474 xassert (it->face_id >= 0);
5475
5476 /* Save IT's settings. They are restored after all overlay
5477 strings have been processed. */
5478 xassert (!compute_stop_p || it->sp == 0);
5479
5480 /* When called from handle_stop, there might be an empty display
5481 string loaded. In that case, don't bother saving it. But
5482 don't use this optimization with the bidi iterator, since we
5483 need the corresponding pop_it call to resync the bidi
5484 iterator's position with IT's position, after we are done
5485 with the overlay strings. (The corresponding call to pop_it
5486 in case of an empty display string is in
5487 next_overlay_string.) */
5488 if (!(!it->bidi_p
5489 && STRINGP (it->string) && !SCHARS (it->string)))
5490 push_it (it, NULL);
5491
5492 /* Set up IT to deliver display elements from the first overlay
5493 string. */
5494 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5495 it->string = it->overlay_strings[0];
5496 it->from_overlay = Qnil;
5497 it->stop_charpos = 0;
5498 xassert (STRINGP (it->string));
5499 it->end_charpos = SCHARS (it->string);
5500 it->prev_stop = 0;
5501 it->base_level_stop = 0;
5502 it->multibyte_p = STRING_MULTIBYTE (it->string);
5503 it->method = GET_FROM_STRING;
5504 it->from_disp_prop_p = 0;
5505
5506 /* Force paragraph direction to be that of the parent
5507 buffer. */
5508 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5509 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5510 else
5511 it->paragraph_embedding = L2R;
5512
5513 /* Set up the bidi iterator for this overlay string. */
5514 if (it->bidi_p)
5515 {
5516 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5517
5518 it->bidi_it.string.lstring = it->string;
5519 it->bidi_it.string.s = NULL;
5520 it->bidi_it.string.schars = SCHARS (it->string);
5521 it->bidi_it.string.bufpos = pos;
5522 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5523 it->bidi_it.string.unibyte = !it->multibyte_p;
5524 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5525 }
5526 return 1;
5527 }
5528
5529 it->current.overlay_string_index = -1;
5530 return 0;
5531 }
5532
5533 static int
5534 get_overlay_strings (struct it *it, EMACS_INT charpos)
5535 {
5536 it->string = Qnil;
5537 it->method = GET_FROM_BUFFER;
5538
5539 (void) get_overlay_strings_1 (it, charpos, 1);
5540
5541 CHECK_IT (it);
5542
5543 /* Value is non-zero if we found at least one overlay string. */
5544 return STRINGP (it->string);
5545 }
5546
5547
5548 \f
5549 /***********************************************************************
5550 Saving and restoring state
5551 ***********************************************************************/
5552
5553 /* Save current settings of IT on IT->stack. Called, for example,
5554 before setting up IT for an overlay string, to be able to restore
5555 IT's settings to what they were after the overlay string has been
5556 processed. If POSITION is non-NULL, it is the position to save on
5557 the stack instead of IT->position. */
5558
5559 static void
5560 push_it (struct it *it, struct text_pos *position)
5561 {
5562 struct iterator_stack_entry *p;
5563
5564 xassert (it->sp < IT_STACK_SIZE);
5565 p = it->stack + it->sp;
5566
5567 p->stop_charpos = it->stop_charpos;
5568 p->prev_stop = it->prev_stop;
5569 p->base_level_stop = it->base_level_stop;
5570 p->cmp_it = it->cmp_it;
5571 xassert (it->face_id >= 0);
5572 p->face_id = it->face_id;
5573 p->string = it->string;
5574 p->method = it->method;
5575 p->from_overlay = it->from_overlay;
5576 switch (p->method)
5577 {
5578 case GET_FROM_IMAGE:
5579 p->u.image.object = it->object;
5580 p->u.image.image_id = it->image_id;
5581 p->u.image.slice = it->slice;
5582 break;
5583 case GET_FROM_STRETCH:
5584 p->u.stretch.object = it->object;
5585 break;
5586 }
5587 p->position = position ? *position : it->position;
5588 p->current = it->current;
5589 p->end_charpos = it->end_charpos;
5590 p->string_nchars = it->string_nchars;
5591 p->area = it->area;
5592 p->multibyte_p = it->multibyte_p;
5593 p->avoid_cursor_p = it->avoid_cursor_p;
5594 p->space_width = it->space_width;
5595 p->font_height = it->font_height;
5596 p->voffset = it->voffset;
5597 p->string_from_display_prop_p = it->string_from_display_prop_p;
5598 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5599 p->display_ellipsis_p = 0;
5600 p->line_wrap = it->line_wrap;
5601 p->bidi_p = it->bidi_p;
5602 p->paragraph_embedding = it->paragraph_embedding;
5603 p->from_disp_prop_p = it->from_disp_prop_p;
5604 ++it->sp;
5605
5606 /* Save the state of the bidi iterator as well. */
5607 if (it->bidi_p)
5608 bidi_push_it (&it->bidi_it);
5609 }
5610
5611 static void
5612 iterate_out_of_display_property (struct it *it)
5613 {
5614 int buffer_p = BUFFERP (it->object);
5615 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5616 EMACS_INT bob = (buffer_p ? BEGV : 0);
5617
5618 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5619
5620 /* Maybe initialize paragraph direction. If we are at the beginning
5621 of a new paragraph, next_element_from_buffer may not have a
5622 chance to do that. */
5623 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5624 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5625 /* prev_stop can be zero, so check against BEGV as well. */
5626 while (it->bidi_it.charpos >= bob
5627 && it->prev_stop <= it->bidi_it.charpos
5628 && it->bidi_it.charpos < CHARPOS (it->position)
5629 && it->bidi_it.charpos < eob)
5630 bidi_move_to_visually_next (&it->bidi_it);
5631 /* Record the stop_pos we just crossed, for when we cross it
5632 back, maybe. */
5633 if (it->bidi_it.charpos > CHARPOS (it->position))
5634 it->prev_stop = CHARPOS (it->position);
5635 /* If we ended up not where pop_it put us, resync IT's
5636 positional members with the bidi iterator. */
5637 if (it->bidi_it.charpos != CHARPOS (it->position))
5638 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5639 if (buffer_p)
5640 it->current.pos = it->position;
5641 else
5642 it->current.string_pos = it->position;
5643 }
5644
5645 /* Restore IT's settings from IT->stack. Called, for example, when no
5646 more overlay strings must be processed, and we return to delivering
5647 display elements from a buffer, or when the end of a string from a
5648 `display' property is reached and we return to delivering display
5649 elements from an overlay string, or from a buffer. */
5650
5651 static void
5652 pop_it (struct it *it)
5653 {
5654 struct iterator_stack_entry *p;
5655 int from_display_prop = it->from_disp_prop_p;
5656
5657 xassert (it->sp > 0);
5658 --it->sp;
5659 p = it->stack + it->sp;
5660 it->stop_charpos = p->stop_charpos;
5661 it->prev_stop = p->prev_stop;
5662 it->base_level_stop = p->base_level_stop;
5663 it->cmp_it = p->cmp_it;
5664 it->face_id = p->face_id;
5665 it->current = p->current;
5666 it->position = p->position;
5667 it->string = p->string;
5668 it->from_overlay = p->from_overlay;
5669 if (NILP (it->string))
5670 SET_TEXT_POS (it->current.string_pos, -1, -1);
5671 it->method = p->method;
5672 switch (it->method)
5673 {
5674 case GET_FROM_IMAGE:
5675 it->image_id = p->u.image.image_id;
5676 it->object = p->u.image.object;
5677 it->slice = p->u.image.slice;
5678 break;
5679 case GET_FROM_STRETCH:
5680 it->object = p->u.stretch.object;
5681 break;
5682 case GET_FROM_BUFFER:
5683 it->object = it->w->buffer;
5684 break;
5685 case GET_FROM_STRING:
5686 it->object = it->string;
5687 break;
5688 case GET_FROM_DISPLAY_VECTOR:
5689 if (it->s)
5690 it->method = GET_FROM_C_STRING;
5691 else if (STRINGP (it->string))
5692 it->method = GET_FROM_STRING;
5693 else
5694 {
5695 it->method = GET_FROM_BUFFER;
5696 it->object = it->w->buffer;
5697 }
5698 }
5699 it->end_charpos = p->end_charpos;
5700 it->string_nchars = p->string_nchars;
5701 it->area = p->area;
5702 it->multibyte_p = p->multibyte_p;
5703 it->avoid_cursor_p = p->avoid_cursor_p;
5704 it->space_width = p->space_width;
5705 it->font_height = p->font_height;
5706 it->voffset = p->voffset;
5707 it->string_from_display_prop_p = p->string_from_display_prop_p;
5708 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5709 it->line_wrap = p->line_wrap;
5710 it->bidi_p = p->bidi_p;
5711 it->paragraph_embedding = p->paragraph_embedding;
5712 it->from_disp_prop_p = p->from_disp_prop_p;
5713 if (it->bidi_p)
5714 {
5715 bidi_pop_it (&it->bidi_it);
5716 /* Bidi-iterate until we get out of the portion of text, if any,
5717 covered by a `display' text property or by an overlay with
5718 `display' property. (We cannot just jump there, because the
5719 internal coherency of the bidi iterator state can not be
5720 preserved across such jumps.) We also must determine the
5721 paragraph base direction if the overlay we just processed is
5722 at the beginning of a new paragraph. */
5723 if (from_display_prop
5724 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5725 iterate_out_of_display_property (it);
5726
5727 xassert ((BUFFERP (it->object)
5728 && IT_CHARPOS (*it) == it->bidi_it.charpos
5729 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5730 || (STRINGP (it->object)
5731 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5732 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5733 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5734 }
5735 }
5736
5737
5738 \f
5739 /***********************************************************************
5740 Moving over lines
5741 ***********************************************************************/
5742
5743 /* Set IT's current position to the previous line start. */
5744
5745 static void
5746 back_to_previous_line_start (struct it *it)
5747 {
5748 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5749 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5750 }
5751
5752
5753 /* Move IT to the next line start.
5754
5755 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5756 we skipped over part of the text (as opposed to moving the iterator
5757 continuously over the text). Otherwise, don't change the value
5758 of *SKIPPED_P.
5759
5760 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5761 iterator on the newline, if it was found.
5762
5763 Newlines may come from buffer text, overlay strings, or strings
5764 displayed via the `display' property. That's the reason we can't
5765 simply use find_next_newline_no_quit.
5766
5767 Note that this function may not skip over invisible text that is so
5768 because of text properties and immediately follows a newline. If
5769 it would, function reseat_at_next_visible_line_start, when called
5770 from set_iterator_to_next, would effectively make invisible
5771 characters following a newline part of the wrong glyph row, which
5772 leads to wrong cursor motion. */
5773
5774 static int
5775 forward_to_next_line_start (struct it *it, int *skipped_p,
5776 struct bidi_it *bidi_it_prev)
5777 {
5778 EMACS_INT old_selective;
5779 int newline_found_p, n;
5780 const int MAX_NEWLINE_DISTANCE = 500;
5781
5782 /* If already on a newline, just consume it to avoid unintended
5783 skipping over invisible text below. */
5784 if (it->what == IT_CHARACTER
5785 && it->c == '\n'
5786 && CHARPOS (it->position) == IT_CHARPOS (*it))
5787 {
5788 if (it->bidi_p && bidi_it_prev)
5789 *bidi_it_prev = it->bidi_it;
5790 set_iterator_to_next (it, 0);
5791 it->c = 0;
5792 return 1;
5793 }
5794
5795 /* Don't handle selective display in the following. It's (a)
5796 unnecessary because it's done by the caller, and (b) leads to an
5797 infinite recursion because next_element_from_ellipsis indirectly
5798 calls this function. */
5799 old_selective = it->selective;
5800 it->selective = 0;
5801
5802 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5803 from buffer text. */
5804 for (n = newline_found_p = 0;
5805 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5806 n += STRINGP (it->string) ? 0 : 1)
5807 {
5808 if (!get_next_display_element (it))
5809 return 0;
5810 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5811 if (newline_found_p && it->bidi_p && bidi_it_prev)
5812 *bidi_it_prev = it->bidi_it;
5813 set_iterator_to_next (it, 0);
5814 }
5815
5816 /* If we didn't find a newline near enough, see if we can use a
5817 short-cut. */
5818 if (!newline_found_p)
5819 {
5820 EMACS_INT start = IT_CHARPOS (*it);
5821 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5822 Lisp_Object pos;
5823
5824 xassert (!STRINGP (it->string));
5825
5826 /* If there isn't any `display' property in sight, and no
5827 overlays, we can just use the position of the newline in
5828 buffer text. */
5829 if (it->stop_charpos >= limit
5830 || ((pos = Fnext_single_property_change (make_number (start),
5831 Qdisplay, Qnil,
5832 make_number (limit)),
5833 NILP (pos))
5834 && next_overlay_change (start) == ZV))
5835 {
5836 if (!it->bidi_p)
5837 {
5838 IT_CHARPOS (*it) = limit;
5839 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5840 }
5841 else
5842 {
5843 struct bidi_it bprev;
5844
5845 /* Help bidi.c avoid expensive searches for display
5846 properties and overlays, by telling it that there are
5847 none up to `limit'. */
5848 if (it->bidi_it.disp_pos < limit)
5849 {
5850 it->bidi_it.disp_pos = limit;
5851 it->bidi_it.disp_prop = 0;
5852 }
5853 do {
5854 bprev = it->bidi_it;
5855 bidi_move_to_visually_next (&it->bidi_it);
5856 } while (it->bidi_it.charpos != limit);
5857 IT_CHARPOS (*it) = limit;
5858 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5859 if (bidi_it_prev)
5860 *bidi_it_prev = bprev;
5861 }
5862 *skipped_p = newline_found_p = 1;
5863 }
5864 else
5865 {
5866 while (get_next_display_element (it)
5867 && !newline_found_p)
5868 {
5869 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5870 if (newline_found_p && it->bidi_p && bidi_it_prev)
5871 *bidi_it_prev = it->bidi_it;
5872 set_iterator_to_next (it, 0);
5873 }
5874 }
5875 }
5876
5877 it->selective = old_selective;
5878 return newline_found_p;
5879 }
5880
5881
5882 /* Set IT's current position to the previous visible line start. Skip
5883 invisible text that is so either due to text properties or due to
5884 selective display. Caution: this does not change IT->current_x and
5885 IT->hpos. */
5886
5887 static void
5888 back_to_previous_visible_line_start (struct it *it)
5889 {
5890 while (IT_CHARPOS (*it) > BEGV)
5891 {
5892 back_to_previous_line_start (it);
5893
5894 if (IT_CHARPOS (*it) <= BEGV)
5895 break;
5896
5897 /* If selective > 0, then lines indented more than its value are
5898 invisible. */
5899 if (it->selective > 0
5900 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5901 it->selective))
5902 continue;
5903
5904 /* Check the newline before point for invisibility. */
5905 {
5906 Lisp_Object prop;
5907 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5908 Qinvisible, it->window);
5909 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5910 continue;
5911 }
5912
5913 if (IT_CHARPOS (*it) <= BEGV)
5914 break;
5915
5916 {
5917 struct it it2;
5918 void *it2data = NULL;
5919 EMACS_INT pos;
5920 EMACS_INT beg, end;
5921 Lisp_Object val, overlay;
5922
5923 SAVE_IT (it2, *it, it2data);
5924
5925 /* If newline is part of a composition, continue from start of composition */
5926 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5927 && beg < IT_CHARPOS (*it))
5928 goto replaced;
5929
5930 /* If newline is replaced by a display property, find start of overlay
5931 or interval and continue search from that point. */
5932 pos = --IT_CHARPOS (it2);
5933 --IT_BYTEPOS (it2);
5934 it2.sp = 0;
5935 bidi_unshelve_cache (NULL, 0);
5936 it2.string_from_display_prop_p = 0;
5937 it2.from_disp_prop_p = 0;
5938 if (handle_display_prop (&it2) == HANDLED_RETURN
5939 && !NILP (val = get_char_property_and_overlay
5940 (make_number (pos), Qdisplay, Qnil, &overlay))
5941 && (OVERLAYP (overlay)
5942 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5943 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5944 {
5945 RESTORE_IT (it, it, it2data);
5946 goto replaced;
5947 }
5948
5949 /* Newline is not replaced by anything -- so we are done. */
5950 RESTORE_IT (it, it, it2data);
5951 break;
5952
5953 replaced:
5954 if (beg < BEGV)
5955 beg = BEGV;
5956 IT_CHARPOS (*it) = beg;
5957 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5958 }
5959 }
5960
5961 it->continuation_lines_width = 0;
5962
5963 xassert (IT_CHARPOS (*it) >= BEGV);
5964 xassert (IT_CHARPOS (*it) == BEGV
5965 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5966 CHECK_IT (it);
5967 }
5968
5969
5970 /* Reseat iterator IT at the previous visible line start. Skip
5971 invisible text that is so either due to text properties or due to
5972 selective display. At the end, update IT's overlay information,
5973 face information etc. */
5974
5975 void
5976 reseat_at_previous_visible_line_start (struct it *it)
5977 {
5978 back_to_previous_visible_line_start (it);
5979 reseat (it, it->current.pos, 1);
5980 CHECK_IT (it);
5981 }
5982
5983
5984 /* Reseat iterator IT on the next visible line start in the current
5985 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5986 preceding the line start. Skip over invisible text that is so
5987 because of selective display. Compute faces, overlays etc at the
5988 new position. Note that this function does not skip over text that
5989 is invisible because of text properties. */
5990
5991 static void
5992 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5993 {
5994 int newline_found_p, skipped_p = 0;
5995 struct bidi_it bidi_it_prev;
5996
5997 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5998
5999 /* Skip over lines that are invisible because they are indented
6000 more than the value of IT->selective. */
6001 if (it->selective > 0)
6002 while (IT_CHARPOS (*it) < ZV
6003 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6004 it->selective))
6005 {
6006 xassert (IT_BYTEPOS (*it) == BEGV
6007 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6008 newline_found_p =
6009 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6010 }
6011
6012 /* Position on the newline if that's what's requested. */
6013 if (on_newline_p && newline_found_p)
6014 {
6015 if (STRINGP (it->string))
6016 {
6017 if (IT_STRING_CHARPOS (*it) > 0)
6018 {
6019 if (!it->bidi_p)
6020 {
6021 --IT_STRING_CHARPOS (*it);
6022 --IT_STRING_BYTEPOS (*it);
6023 }
6024 else
6025 {
6026 /* We need to restore the bidi iterator to the state
6027 it had on the newline, and resync the IT's
6028 position with that. */
6029 it->bidi_it = bidi_it_prev;
6030 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6031 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6032 }
6033 }
6034 }
6035 else if (IT_CHARPOS (*it) > BEGV)
6036 {
6037 if (!it->bidi_p)
6038 {
6039 --IT_CHARPOS (*it);
6040 --IT_BYTEPOS (*it);
6041 }
6042 else
6043 {
6044 /* We need to restore the bidi iterator to the state it
6045 had on the newline and resync IT with that. */
6046 it->bidi_it = bidi_it_prev;
6047 IT_CHARPOS (*it) = it->bidi_it.charpos;
6048 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6049 }
6050 reseat (it, it->current.pos, 0);
6051 }
6052 }
6053 else if (skipped_p)
6054 reseat (it, it->current.pos, 0);
6055
6056 CHECK_IT (it);
6057 }
6058
6059
6060 \f
6061 /***********************************************************************
6062 Changing an iterator's position
6063 ***********************************************************************/
6064
6065 /* Change IT's current position to POS in current_buffer. If FORCE_P
6066 is non-zero, always check for text properties at the new position.
6067 Otherwise, text properties are only looked up if POS >=
6068 IT->check_charpos of a property. */
6069
6070 static void
6071 reseat (struct it *it, struct text_pos pos, int force_p)
6072 {
6073 EMACS_INT original_pos = IT_CHARPOS (*it);
6074
6075 reseat_1 (it, pos, 0);
6076
6077 /* Determine where to check text properties. Avoid doing it
6078 where possible because text property lookup is very expensive. */
6079 if (force_p
6080 || CHARPOS (pos) > it->stop_charpos
6081 || CHARPOS (pos) < original_pos)
6082 {
6083 if (it->bidi_p)
6084 {
6085 /* For bidi iteration, we need to prime prev_stop and
6086 base_level_stop with our best estimations. */
6087 /* Implementation note: Of course, POS is not necessarily a
6088 stop position, so assigning prev_pos to it is a lie; we
6089 should have called compute_stop_backwards. However, if
6090 the current buffer does not include any R2L characters,
6091 that call would be a waste of cycles, because the
6092 iterator will never move back, and thus never cross this
6093 "fake" stop position. So we delay that backward search
6094 until the time we really need it, in next_element_from_buffer. */
6095 if (CHARPOS (pos) != it->prev_stop)
6096 it->prev_stop = CHARPOS (pos);
6097 if (CHARPOS (pos) < it->base_level_stop)
6098 it->base_level_stop = 0; /* meaning it's unknown */
6099 handle_stop (it);
6100 }
6101 else
6102 {
6103 handle_stop (it);
6104 it->prev_stop = it->base_level_stop = 0;
6105 }
6106
6107 }
6108
6109 CHECK_IT (it);
6110 }
6111
6112
6113 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6114 IT->stop_pos to POS, also. */
6115
6116 static void
6117 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6118 {
6119 /* Don't call this function when scanning a C string. */
6120 xassert (it->s == NULL);
6121
6122 /* POS must be a reasonable value. */
6123 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6124
6125 it->current.pos = it->position = pos;
6126 it->end_charpos = ZV;
6127 it->dpvec = NULL;
6128 it->current.dpvec_index = -1;
6129 it->current.overlay_string_index = -1;
6130 IT_STRING_CHARPOS (*it) = -1;
6131 IT_STRING_BYTEPOS (*it) = -1;
6132 it->string = Qnil;
6133 it->method = GET_FROM_BUFFER;
6134 it->object = it->w->buffer;
6135 it->area = TEXT_AREA;
6136 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6137 it->sp = 0;
6138 it->string_from_display_prop_p = 0;
6139 it->string_from_prefix_prop_p = 0;
6140
6141 it->from_disp_prop_p = 0;
6142 it->face_before_selective_p = 0;
6143 if (it->bidi_p)
6144 {
6145 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6146 &it->bidi_it);
6147 bidi_unshelve_cache (NULL, 0);
6148 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6149 it->bidi_it.string.s = NULL;
6150 it->bidi_it.string.lstring = Qnil;
6151 it->bidi_it.string.bufpos = 0;
6152 it->bidi_it.string.unibyte = 0;
6153 }
6154
6155 if (set_stop_p)
6156 {
6157 it->stop_charpos = CHARPOS (pos);
6158 it->base_level_stop = CHARPOS (pos);
6159 }
6160 }
6161
6162
6163 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6164 If S is non-null, it is a C string to iterate over. Otherwise,
6165 STRING gives a Lisp string to iterate over.
6166
6167 If PRECISION > 0, don't return more then PRECISION number of
6168 characters from the string.
6169
6170 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6171 characters have been returned. FIELD_WIDTH < 0 means an infinite
6172 field width.
6173
6174 MULTIBYTE = 0 means disable processing of multibyte characters,
6175 MULTIBYTE > 0 means enable it,
6176 MULTIBYTE < 0 means use IT->multibyte_p.
6177
6178 IT must be initialized via a prior call to init_iterator before
6179 calling this function. */
6180
6181 static void
6182 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6183 EMACS_INT charpos, EMACS_INT precision, int field_width,
6184 int multibyte)
6185 {
6186 /* No region in strings. */
6187 it->region_beg_charpos = it->region_end_charpos = -1;
6188
6189 /* No text property checks performed by default, but see below. */
6190 it->stop_charpos = -1;
6191
6192 /* Set iterator position and end position. */
6193 memset (&it->current, 0, sizeof it->current);
6194 it->current.overlay_string_index = -1;
6195 it->current.dpvec_index = -1;
6196 xassert (charpos >= 0);
6197
6198 /* If STRING is specified, use its multibyteness, otherwise use the
6199 setting of MULTIBYTE, if specified. */
6200 if (multibyte >= 0)
6201 it->multibyte_p = multibyte > 0;
6202
6203 /* Bidirectional reordering of strings is controlled by the default
6204 value of bidi-display-reordering. Don't try to reorder while
6205 loading loadup.el, as the necessary character property tables are
6206 not yet available. */
6207 it->bidi_p =
6208 NILP (Vpurify_flag)
6209 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6210
6211 if (s == NULL)
6212 {
6213 xassert (STRINGP (string));
6214 it->string = string;
6215 it->s = NULL;
6216 it->end_charpos = it->string_nchars = SCHARS (string);
6217 it->method = GET_FROM_STRING;
6218 it->current.string_pos = string_pos (charpos, string);
6219
6220 if (it->bidi_p)
6221 {
6222 it->bidi_it.string.lstring = string;
6223 it->bidi_it.string.s = NULL;
6224 it->bidi_it.string.schars = it->end_charpos;
6225 it->bidi_it.string.bufpos = 0;
6226 it->bidi_it.string.from_disp_str = 0;
6227 it->bidi_it.string.unibyte = !it->multibyte_p;
6228 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6229 FRAME_WINDOW_P (it->f), &it->bidi_it);
6230 }
6231 }
6232 else
6233 {
6234 it->s = (const unsigned char *) s;
6235 it->string = Qnil;
6236
6237 /* Note that we use IT->current.pos, not it->current.string_pos,
6238 for displaying C strings. */
6239 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6240 if (it->multibyte_p)
6241 {
6242 it->current.pos = c_string_pos (charpos, s, 1);
6243 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6244 }
6245 else
6246 {
6247 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6248 it->end_charpos = it->string_nchars = strlen (s);
6249 }
6250
6251 if (it->bidi_p)
6252 {
6253 it->bidi_it.string.lstring = Qnil;
6254 it->bidi_it.string.s = (const unsigned char *) s;
6255 it->bidi_it.string.schars = it->end_charpos;
6256 it->bidi_it.string.bufpos = 0;
6257 it->bidi_it.string.from_disp_str = 0;
6258 it->bidi_it.string.unibyte = !it->multibyte_p;
6259 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6260 &it->bidi_it);
6261 }
6262 it->method = GET_FROM_C_STRING;
6263 }
6264
6265 /* PRECISION > 0 means don't return more than PRECISION characters
6266 from the string. */
6267 if (precision > 0 && it->end_charpos - charpos > precision)
6268 {
6269 it->end_charpos = it->string_nchars = charpos + precision;
6270 if (it->bidi_p)
6271 it->bidi_it.string.schars = it->end_charpos;
6272 }
6273
6274 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6275 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6276 FIELD_WIDTH < 0 means infinite field width. This is useful for
6277 padding with `-' at the end of a mode line. */
6278 if (field_width < 0)
6279 field_width = INFINITY;
6280 /* Implementation note: We deliberately don't enlarge
6281 it->bidi_it.string.schars here to fit it->end_charpos, because
6282 the bidi iterator cannot produce characters out of thin air. */
6283 if (field_width > it->end_charpos - charpos)
6284 it->end_charpos = charpos + field_width;
6285
6286 /* Use the standard display table for displaying strings. */
6287 if (DISP_TABLE_P (Vstandard_display_table))
6288 it->dp = XCHAR_TABLE (Vstandard_display_table);
6289
6290 it->stop_charpos = charpos;
6291 it->prev_stop = charpos;
6292 it->base_level_stop = 0;
6293 if (it->bidi_p)
6294 {
6295 it->bidi_it.first_elt = 1;
6296 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6297 it->bidi_it.disp_pos = -1;
6298 }
6299 if (s == NULL && it->multibyte_p)
6300 {
6301 EMACS_INT endpos = SCHARS (it->string);
6302 if (endpos > it->end_charpos)
6303 endpos = it->end_charpos;
6304 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6305 it->string);
6306 }
6307 CHECK_IT (it);
6308 }
6309
6310
6311 \f
6312 /***********************************************************************
6313 Iteration
6314 ***********************************************************************/
6315
6316 /* Map enum it_method value to corresponding next_element_from_* function. */
6317
6318 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6319 {
6320 next_element_from_buffer,
6321 next_element_from_display_vector,
6322 next_element_from_string,
6323 next_element_from_c_string,
6324 next_element_from_image,
6325 next_element_from_stretch
6326 };
6327
6328 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6329
6330
6331 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6332 (possibly with the following characters). */
6333
6334 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6335 ((IT)->cmp_it.id >= 0 \
6336 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6337 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6338 END_CHARPOS, (IT)->w, \
6339 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6340 (IT)->string)))
6341
6342
6343 /* Lookup the char-table Vglyphless_char_display for character C (-1
6344 if we want information for no-font case), and return the display
6345 method symbol. By side-effect, update it->what and
6346 it->glyphless_method. This function is called from
6347 get_next_display_element for each character element, and from
6348 x_produce_glyphs when no suitable font was found. */
6349
6350 Lisp_Object
6351 lookup_glyphless_char_display (int c, struct it *it)
6352 {
6353 Lisp_Object glyphless_method = Qnil;
6354
6355 if (CHAR_TABLE_P (Vglyphless_char_display)
6356 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6357 {
6358 if (c >= 0)
6359 {
6360 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6361 if (CONSP (glyphless_method))
6362 glyphless_method = FRAME_WINDOW_P (it->f)
6363 ? XCAR (glyphless_method)
6364 : XCDR (glyphless_method);
6365 }
6366 else
6367 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6368 }
6369
6370 retry:
6371 if (NILP (glyphless_method))
6372 {
6373 if (c >= 0)
6374 /* The default is to display the character by a proper font. */
6375 return Qnil;
6376 /* The default for the no-font case is to display an empty box. */
6377 glyphless_method = Qempty_box;
6378 }
6379 if (EQ (glyphless_method, Qzero_width))
6380 {
6381 if (c >= 0)
6382 return glyphless_method;
6383 /* This method can't be used for the no-font case. */
6384 glyphless_method = Qempty_box;
6385 }
6386 if (EQ (glyphless_method, Qthin_space))
6387 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6388 else if (EQ (glyphless_method, Qempty_box))
6389 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6390 else if (EQ (glyphless_method, Qhex_code))
6391 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6392 else if (STRINGP (glyphless_method))
6393 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6394 else
6395 {
6396 /* Invalid value. We use the default method. */
6397 glyphless_method = Qnil;
6398 goto retry;
6399 }
6400 it->what = IT_GLYPHLESS;
6401 return glyphless_method;
6402 }
6403
6404 /* Load IT's display element fields with information about the next
6405 display element from the current position of IT. Value is zero if
6406 end of buffer (or C string) is reached. */
6407
6408 static struct frame *last_escape_glyph_frame = NULL;
6409 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6410 static int last_escape_glyph_merged_face_id = 0;
6411
6412 struct frame *last_glyphless_glyph_frame = NULL;
6413 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6414 int last_glyphless_glyph_merged_face_id = 0;
6415
6416 static int
6417 get_next_display_element (struct it *it)
6418 {
6419 /* Non-zero means that we found a display element. Zero means that
6420 we hit the end of what we iterate over. Performance note: the
6421 function pointer `method' used here turns out to be faster than
6422 using a sequence of if-statements. */
6423 int success_p;
6424
6425 get_next:
6426 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6427
6428 if (it->what == IT_CHARACTER)
6429 {
6430 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6431 and only if (a) the resolved directionality of that character
6432 is R..." */
6433 /* FIXME: Do we need an exception for characters from display
6434 tables? */
6435 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6436 it->c = bidi_mirror_char (it->c);
6437 /* Map via display table or translate control characters.
6438 IT->c, IT->len etc. have been set to the next character by
6439 the function call above. If we have a display table, and it
6440 contains an entry for IT->c, translate it. Don't do this if
6441 IT->c itself comes from a display table, otherwise we could
6442 end up in an infinite recursion. (An alternative could be to
6443 count the recursion depth of this function and signal an
6444 error when a certain maximum depth is reached.) Is it worth
6445 it? */
6446 if (success_p && it->dpvec == NULL)
6447 {
6448 Lisp_Object dv;
6449 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6450 int nonascii_space_p = 0;
6451 int nonascii_hyphen_p = 0;
6452 int c = it->c; /* This is the character to display. */
6453
6454 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6455 {
6456 xassert (SINGLE_BYTE_CHAR_P (c));
6457 if (unibyte_display_via_language_environment)
6458 {
6459 c = DECODE_CHAR (unibyte, c);
6460 if (c < 0)
6461 c = BYTE8_TO_CHAR (it->c);
6462 }
6463 else
6464 c = BYTE8_TO_CHAR (it->c);
6465 }
6466
6467 if (it->dp
6468 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6469 VECTORP (dv)))
6470 {
6471 struct Lisp_Vector *v = XVECTOR (dv);
6472
6473 /* Return the first character from the display table
6474 entry, if not empty. If empty, don't display the
6475 current character. */
6476 if (v->header.size)
6477 {
6478 it->dpvec_char_len = it->len;
6479 it->dpvec = v->contents;
6480 it->dpend = v->contents + v->header.size;
6481 it->current.dpvec_index = 0;
6482 it->dpvec_face_id = -1;
6483 it->saved_face_id = it->face_id;
6484 it->method = GET_FROM_DISPLAY_VECTOR;
6485 it->ellipsis_p = 0;
6486 }
6487 else
6488 {
6489 set_iterator_to_next (it, 0);
6490 }
6491 goto get_next;
6492 }
6493
6494 if (! NILP (lookup_glyphless_char_display (c, it)))
6495 {
6496 if (it->what == IT_GLYPHLESS)
6497 goto done;
6498 /* Don't display this character. */
6499 set_iterator_to_next (it, 0);
6500 goto get_next;
6501 }
6502
6503 /* If `nobreak-char-display' is non-nil, we display
6504 non-ASCII spaces and hyphens specially. */
6505 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6506 {
6507 if (c == 0xA0)
6508 nonascii_space_p = 1;
6509 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6510 nonascii_hyphen_p = 1;
6511 }
6512
6513 /* Translate control characters into `\003' or `^C' form.
6514 Control characters coming from a display table entry are
6515 currently not translated because we use IT->dpvec to hold
6516 the translation. This could easily be changed but I
6517 don't believe that it is worth doing.
6518
6519 The characters handled by `nobreak-char-display' must be
6520 translated too.
6521
6522 Non-printable characters and raw-byte characters are also
6523 translated to octal form. */
6524 if (((c < ' ' || c == 127) /* ASCII control chars */
6525 ? (it->area != TEXT_AREA
6526 /* In mode line, treat \n, \t like other crl chars. */
6527 || (c != '\t'
6528 && it->glyph_row
6529 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6530 || (c != '\n' && c != '\t'))
6531 : (nonascii_space_p
6532 || nonascii_hyphen_p
6533 || CHAR_BYTE8_P (c)
6534 || ! CHAR_PRINTABLE_P (c))))
6535 {
6536 /* C is a control character, non-ASCII space/hyphen,
6537 raw-byte, or a non-printable character which must be
6538 displayed either as '\003' or as `^C' where the '\\'
6539 and '^' can be defined in the display table. Fill
6540 IT->ctl_chars with glyphs for what we have to
6541 display. Then, set IT->dpvec to these glyphs. */
6542 Lisp_Object gc;
6543 int ctl_len;
6544 int face_id;
6545 EMACS_INT lface_id = 0;
6546 int escape_glyph;
6547
6548 /* Handle control characters with ^. */
6549
6550 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6551 {
6552 int g;
6553
6554 g = '^'; /* default glyph for Control */
6555 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6556 if (it->dp
6557 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6558 && GLYPH_CODE_CHAR_VALID_P (gc))
6559 {
6560 g = GLYPH_CODE_CHAR (gc);
6561 lface_id = GLYPH_CODE_FACE (gc);
6562 }
6563 if (lface_id)
6564 {
6565 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6566 }
6567 else if (it->f == last_escape_glyph_frame
6568 && it->face_id == last_escape_glyph_face_id)
6569 {
6570 face_id = last_escape_glyph_merged_face_id;
6571 }
6572 else
6573 {
6574 /* Merge the escape-glyph face into the current face. */
6575 face_id = merge_faces (it->f, Qescape_glyph, 0,
6576 it->face_id);
6577 last_escape_glyph_frame = it->f;
6578 last_escape_glyph_face_id = it->face_id;
6579 last_escape_glyph_merged_face_id = face_id;
6580 }
6581
6582 XSETINT (it->ctl_chars[0], g);
6583 XSETINT (it->ctl_chars[1], c ^ 0100);
6584 ctl_len = 2;
6585 goto display_control;
6586 }
6587
6588 /* Handle non-ascii space in the mode where it only gets
6589 highlighting. */
6590
6591 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6592 {
6593 /* Merge `nobreak-space' into the current face. */
6594 face_id = merge_faces (it->f, Qnobreak_space, 0,
6595 it->face_id);
6596 XSETINT (it->ctl_chars[0], ' ');
6597 ctl_len = 1;
6598 goto display_control;
6599 }
6600
6601 /* Handle sequences that start with the "escape glyph". */
6602
6603 /* the default escape glyph is \. */
6604 escape_glyph = '\\';
6605
6606 if (it->dp
6607 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6608 && GLYPH_CODE_CHAR_VALID_P (gc))
6609 {
6610 escape_glyph = GLYPH_CODE_CHAR (gc);
6611 lface_id = GLYPH_CODE_FACE (gc);
6612 }
6613 if (lface_id)
6614 {
6615 /* The display table specified a face.
6616 Merge it into face_id and also into escape_glyph. */
6617 face_id = merge_faces (it->f, Qt, lface_id,
6618 it->face_id);
6619 }
6620 else if (it->f == last_escape_glyph_frame
6621 && it->face_id == last_escape_glyph_face_id)
6622 {
6623 face_id = last_escape_glyph_merged_face_id;
6624 }
6625 else
6626 {
6627 /* Merge the escape-glyph face into the current face. */
6628 face_id = merge_faces (it->f, Qescape_glyph, 0,
6629 it->face_id);
6630 last_escape_glyph_frame = it->f;
6631 last_escape_glyph_face_id = it->face_id;
6632 last_escape_glyph_merged_face_id = face_id;
6633 }
6634
6635 /* Draw non-ASCII hyphen with just highlighting: */
6636
6637 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6638 {
6639 XSETINT (it->ctl_chars[0], '-');
6640 ctl_len = 1;
6641 goto display_control;
6642 }
6643
6644 /* Draw non-ASCII space/hyphen with escape glyph: */
6645
6646 if (nonascii_space_p || nonascii_hyphen_p)
6647 {
6648 XSETINT (it->ctl_chars[0], escape_glyph);
6649 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6650 ctl_len = 2;
6651 goto display_control;
6652 }
6653
6654 {
6655 char str[10];
6656 int len, i;
6657
6658 if (CHAR_BYTE8_P (c))
6659 /* Display \200 instead of \17777600. */
6660 c = CHAR_TO_BYTE8 (c);
6661 len = sprintf (str, "%03o", c);
6662
6663 XSETINT (it->ctl_chars[0], escape_glyph);
6664 for (i = 0; i < len; i++)
6665 XSETINT (it->ctl_chars[i + 1], str[i]);
6666 ctl_len = len + 1;
6667 }
6668
6669 display_control:
6670 /* Set up IT->dpvec and return first character from it. */
6671 it->dpvec_char_len = it->len;
6672 it->dpvec = it->ctl_chars;
6673 it->dpend = it->dpvec + ctl_len;
6674 it->current.dpvec_index = 0;
6675 it->dpvec_face_id = face_id;
6676 it->saved_face_id = it->face_id;
6677 it->method = GET_FROM_DISPLAY_VECTOR;
6678 it->ellipsis_p = 0;
6679 goto get_next;
6680 }
6681 it->char_to_display = c;
6682 }
6683 else if (success_p)
6684 {
6685 it->char_to_display = it->c;
6686 }
6687 }
6688
6689 /* Adjust face id for a multibyte character. There are no multibyte
6690 character in unibyte text. */
6691 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6692 && it->multibyte_p
6693 && success_p
6694 && FRAME_WINDOW_P (it->f))
6695 {
6696 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6697
6698 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6699 {
6700 /* Automatic composition with glyph-string. */
6701 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6702
6703 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6704 }
6705 else
6706 {
6707 EMACS_INT pos = (it->s ? -1
6708 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6709 : IT_CHARPOS (*it));
6710 int c;
6711
6712 if (it->what == IT_CHARACTER)
6713 c = it->char_to_display;
6714 else
6715 {
6716 struct composition *cmp = composition_table[it->cmp_it.id];
6717 int i;
6718
6719 c = ' ';
6720 for (i = 0; i < cmp->glyph_len; i++)
6721 /* TAB in a composition means display glyphs with
6722 padding space on the left or right. */
6723 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6724 break;
6725 }
6726 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6727 }
6728 }
6729
6730 done:
6731 /* Is this character the last one of a run of characters with
6732 box? If yes, set IT->end_of_box_run_p to 1. */
6733 if (it->face_box_p
6734 && it->s == NULL)
6735 {
6736 if (it->method == GET_FROM_STRING && it->sp)
6737 {
6738 int face_id = underlying_face_id (it);
6739 struct face *face = FACE_FROM_ID (it->f, face_id);
6740
6741 if (face)
6742 {
6743 if (face->box == FACE_NO_BOX)
6744 {
6745 /* If the box comes from face properties in a
6746 display string, check faces in that string. */
6747 int string_face_id = face_after_it_pos (it);
6748 it->end_of_box_run_p
6749 = (FACE_FROM_ID (it->f, string_face_id)->box
6750 == FACE_NO_BOX);
6751 }
6752 /* Otherwise, the box comes from the underlying face.
6753 If this is the last string character displayed, check
6754 the next buffer location. */
6755 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6756 && (it->current.overlay_string_index
6757 == it->n_overlay_strings - 1))
6758 {
6759 EMACS_INT ignore;
6760 int next_face_id;
6761 struct text_pos pos = it->current.pos;
6762 INC_TEXT_POS (pos, it->multibyte_p);
6763
6764 next_face_id = face_at_buffer_position
6765 (it->w, CHARPOS (pos), it->region_beg_charpos,
6766 it->region_end_charpos, &ignore,
6767 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6768 -1);
6769 it->end_of_box_run_p
6770 = (FACE_FROM_ID (it->f, next_face_id)->box
6771 == FACE_NO_BOX);
6772 }
6773 }
6774 }
6775 else
6776 {
6777 int face_id = face_after_it_pos (it);
6778 it->end_of_box_run_p
6779 = (face_id != it->face_id
6780 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6781 }
6782 }
6783
6784 /* Value is 0 if end of buffer or string reached. */
6785 return success_p;
6786 }
6787
6788
6789 /* Move IT to the next display element.
6790
6791 RESEAT_P non-zero means if called on a newline in buffer text,
6792 skip to the next visible line start.
6793
6794 Functions get_next_display_element and set_iterator_to_next are
6795 separate because I find this arrangement easier to handle than a
6796 get_next_display_element function that also increments IT's
6797 position. The way it is we can first look at an iterator's current
6798 display element, decide whether it fits on a line, and if it does,
6799 increment the iterator position. The other way around we probably
6800 would either need a flag indicating whether the iterator has to be
6801 incremented the next time, or we would have to implement a
6802 decrement position function which would not be easy to write. */
6803
6804 void
6805 set_iterator_to_next (struct it *it, int reseat_p)
6806 {
6807 /* Reset flags indicating start and end of a sequence of characters
6808 with box. Reset them at the start of this function because
6809 moving the iterator to a new position might set them. */
6810 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6811
6812 switch (it->method)
6813 {
6814 case GET_FROM_BUFFER:
6815 /* The current display element of IT is a character from
6816 current_buffer. Advance in the buffer, and maybe skip over
6817 invisible lines that are so because of selective display. */
6818 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6819 reseat_at_next_visible_line_start (it, 0);
6820 else if (it->cmp_it.id >= 0)
6821 {
6822 /* We are currently getting glyphs from a composition. */
6823 int i;
6824
6825 if (! it->bidi_p)
6826 {
6827 IT_CHARPOS (*it) += it->cmp_it.nchars;
6828 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6829 if (it->cmp_it.to < it->cmp_it.nglyphs)
6830 {
6831 it->cmp_it.from = it->cmp_it.to;
6832 }
6833 else
6834 {
6835 it->cmp_it.id = -1;
6836 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6837 IT_BYTEPOS (*it),
6838 it->end_charpos, Qnil);
6839 }
6840 }
6841 else if (! it->cmp_it.reversed_p)
6842 {
6843 /* Composition created while scanning forward. */
6844 /* Update IT's char/byte positions to point to the first
6845 character of the next grapheme cluster, or to the
6846 character visually after the current composition. */
6847 for (i = 0; i < it->cmp_it.nchars; i++)
6848 bidi_move_to_visually_next (&it->bidi_it);
6849 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6850 IT_CHARPOS (*it) = it->bidi_it.charpos;
6851
6852 if (it->cmp_it.to < it->cmp_it.nglyphs)
6853 {
6854 /* Proceed to the next grapheme cluster. */
6855 it->cmp_it.from = it->cmp_it.to;
6856 }
6857 else
6858 {
6859 /* No more grapheme clusters in this composition.
6860 Find the next stop position. */
6861 EMACS_INT stop = it->end_charpos;
6862 if (it->bidi_it.scan_dir < 0)
6863 /* Now we are scanning backward and don't know
6864 where to stop. */
6865 stop = -1;
6866 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6867 IT_BYTEPOS (*it), stop, Qnil);
6868 }
6869 }
6870 else
6871 {
6872 /* Composition created while scanning backward. */
6873 /* Update IT's char/byte positions to point to the last
6874 character of the previous grapheme cluster, or the
6875 character visually after the current composition. */
6876 for (i = 0; i < it->cmp_it.nchars; i++)
6877 bidi_move_to_visually_next (&it->bidi_it);
6878 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6879 IT_CHARPOS (*it) = it->bidi_it.charpos;
6880 if (it->cmp_it.from > 0)
6881 {
6882 /* Proceed to the previous grapheme cluster. */
6883 it->cmp_it.to = it->cmp_it.from;
6884 }
6885 else
6886 {
6887 /* No more grapheme clusters in this composition.
6888 Find the next stop position. */
6889 EMACS_INT stop = it->end_charpos;
6890 if (it->bidi_it.scan_dir < 0)
6891 /* Now we are scanning backward and don't know
6892 where to stop. */
6893 stop = -1;
6894 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6895 IT_BYTEPOS (*it), stop, Qnil);
6896 }
6897 }
6898 }
6899 else
6900 {
6901 xassert (it->len != 0);
6902
6903 if (!it->bidi_p)
6904 {
6905 IT_BYTEPOS (*it) += it->len;
6906 IT_CHARPOS (*it) += 1;
6907 }
6908 else
6909 {
6910 int prev_scan_dir = it->bidi_it.scan_dir;
6911 /* If this is a new paragraph, determine its base
6912 direction (a.k.a. its base embedding level). */
6913 if (it->bidi_it.new_paragraph)
6914 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6915 bidi_move_to_visually_next (&it->bidi_it);
6916 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6917 IT_CHARPOS (*it) = it->bidi_it.charpos;
6918 if (prev_scan_dir != it->bidi_it.scan_dir)
6919 {
6920 /* As the scan direction was changed, we must
6921 re-compute the stop position for composition. */
6922 EMACS_INT stop = it->end_charpos;
6923 if (it->bidi_it.scan_dir < 0)
6924 stop = -1;
6925 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6926 IT_BYTEPOS (*it), stop, Qnil);
6927 }
6928 }
6929 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6930 }
6931 break;
6932
6933 case GET_FROM_C_STRING:
6934 /* Current display element of IT is from a C string. */
6935 if (!it->bidi_p
6936 /* If the string position is beyond string's end, it means
6937 next_element_from_c_string is padding the string with
6938 blanks, in which case we bypass the bidi iterator,
6939 because it cannot deal with such virtual characters. */
6940 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
6941 {
6942 IT_BYTEPOS (*it) += it->len;
6943 IT_CHARPOS (*it) += 1;
6944 }
6945 else
6946 {
6947 bidi_move_to_visually_next (&it->bidi_it);
6948 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6949 IT_CHARPOS (*it) = it->bidi_it.charpos;
6950 }
6951 break;
6952
6953 case GET_FROM_DISPLAY_VECTOR:
6954 /* Current display element of IT is from a display table entry.
6955 Advance in the display table definition. Reset it to null if
6956 end reached, and continue with characters from buffers/
6957 strings. */
6958 ++it->current.dpvec_index;
6959
6960 /* Restore face of the iterator to what they were before the
6961 display vector entry (these entries may contain faces). */
6962 it->face_id = it->saved_face_id;
6963
6964 if (it->dpvec + it->current.dpvec_index == it->dpend)
6965 {
6966 int recheck_faces = it->ellipsis_p;
6967
6968 if (it->s)
6969 it->method = GET_FROM_C_STRING;
6970 else if (STRINGP (it->string))
6971 it->method = GET_FROM_STRING;
6972 else
6973 {
6974 it->method = GET_FROM_BUFFER;
6975 it->object = it->w->buffer;
6976 }
6977
6978 it->dpvec = NULL;
6979 it->current.dpvec_index = -1;
6980
6981 /* Skip over characters which were displayed via IT->dpvec. */
6982 if (it->dpvec_char_len < 0)
6983 reseat_at_next_visible_line_start (it, 1);
6984 else if (it->dpvec_char_len > 0)
6985 {
6986 if (it->method == GET_FROM_STRING
6987 && it->n_overlay_strings > 0)
6988 it->ignore_overlay_strings_at_pos_p = 1;
6989 it->len = it->dpvec_char_len;
6990 set_iterator_to_next (it, reseat_p);
6991 }
6992
6993 /* Maybe recheck faces after display vector */
6994 if (recheck_faces)
6995 it->stop_charpos = IT_CHARPOS (*it);
6996 }
6997 break;
6998
6999 case GET_FROM_STRING:
7000 /* Current display element is a character from a Lisp string. */
7001 xassert (it->s == NULL && STRINGP (it->string));
7002 if (it->cmp_it.id >= 0)
7003 {
7004 int i;
7005
7006 if (! it->bidi_p)
7007 {
7008 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7009 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7010 if (it->cmp_it.to < it->cmp_it.nglyphs)
7011 it->cmp_it.from = it->cmp_it.to;
7012 else
7013 {
7014 it->cmp_it.id = -1;
7015 composition_compute_stop_pos (&it->cmp_it,
7016 IT_STRING_CHARPOS (*it),
7017 IT_STRING_BYTEPOS (*it),
7018 it->end_charpos, it->string);
7019 }
7020 }
7021 else if (! it->cmp_it.reversed_p)
7022 {
7023 for (i = 0; i < it->cmp_it.nchars; i++)
7024 bidi_move_to_visually_next (&it->bidi_it);
7025 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7026 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7027
7028 if (it->cmp_it.to < it->cmp_it.nglyphs)
7029 it->cmp_it.from = it->cmp_it.to;
7030 else
7031 {
7032 EMACS_INT stop = it->end_charpos;
7033 if (it->bidi_it.scan_dir < 0)
7034 stop = -1;
7035 composition_compute_stop_pos (&it->cmp_it,
7036 IT_STRING_CHARPOS (*it),
7037 IT_STRING_BYTEPOS (*it), stop,
7038 it->string);
7039 }
7040 }
7041 else
7042 {
7043 for (i = 0; i < it->cmp_it.nchars; i++)
7044 bidi_move_to_visually_next (&it->bidi_it);
7045 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7046 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7047 if (it->cmp_it.from > 0)
7048 it->cmp_it.to = it->cmp_it.from;
7049 else
7050 {
7051 EMACS_INT stop = it->end_charpos;
7052 if (it->bidi_it.scan_dir < 0)
7053 stop = -1;
7054 composition_compute_stop_pos (&it->cmp_it,
7055 IT_STRING_CHARPOS (*it),
7056 IT_STRING_BYTEPOS (*it), stop,
7057 it->string);
7058 }
7059 }
7060 }
7061 else
7062 {
7063 if (!it->bidi_p
7064 /* If the string position is beyond string's end, it
7065 means next_element_from_string is padding the string
7066 with blanks, in which case we bypass the bidi
7067 iterator, because it cannot deal with such virtual
7068 characters. */
7069 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7070 {
7071 IT_STRING_BYTEPOS (*it) += it->len;
7072 IT_STRING_CHARPOS (*it) += 1;
7073 }
7074 else
7075 {
7076 int prev_scan_dir = it->bidi_it.scan_dir;
7077
7078 bidi_move_to_visually_next (&it->bidi_it);
7079 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7080 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7081 if (prev_scan_dir != it->bidi_it.scan_dir)
7082 {
7083 EMACS_INT stop = it->end_charpos;
7084
7085 if (it->bidi_it.scan_dir < 0)
7086 stop = -1;
7087 composition_compute_stop_pos (&it->cmp_it,
7088 IT_STRING_CHARPOS (*it),
7089 IT_STRING_BYTEPOS (*it), stop,
7090 it->string);
7091 }
7092 }
7093 }
7094
7095 consider_string_end:
7096
7097 if (it->current.overlay_string_index >= 0)
7098 {
7099 /* IT->string is an overlay string. Advance to the
7100 next, if there is one. */
7101 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7102 {
7103 it->ellipsis_p = 0;
7104 next_overlay_string (it);
7105 if (it->ellipsis_p)
7106 setup_for_ellipsis (it, 0);
7107 }
7108 }
7109 else
7110 {
7111 /* IT->string is not an overlay string. If we reached
7112 its end, and there is something on IT->stack, proceed
7113 with what is on the stack. This can be either another
7114 string, this time an overlay string, or a buffer. */
7115 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7116 && it->sp > 0)
7117 {
7118 pop_it (it);
7119 if (it->method == GET_FROM_STRING)
7120 goto consider_string_end;
7121 }
7122 }
7123 break;
7124
7125 case GET_FROM_IMAGE:
7126 case GET_FROM_STRETCH:
7127 /* The position etc with which we have to proceed are on
7128 the stack. The position may be at the end of a string,
7129 if the `display' property takes up the whole string. */
7130 xassert (it->sp > 0);
7131 pop_it (it);
7132 if (it->method == GET_FROM_STRING)
7133 goto consider_string_end;
7134 break;
7135
7136 default:
7137 /* There are no other methods defined, so this should be a bug. */
7138 abort ();
7139 }
7140
7141 xassert (it->method != GET_FROM_STRING
7142 || (STRINGP (it->string)
7143 && IT_STRING_CHARPOS (*it) >= 0));
7144 }
7145
7146 /* Load IT's display element fields with information about the next
7147 display element which comes from a display table entry or from the
7148 result of translating a control character to one of the forms `^C'
7149 or `\003'.
7150
7151 IT->dpvec holds the glyphs to return as characters.
7152 IT->saved_face_id holds the face id before the display vector--it
7153 is restored into IT->face_id in set_iterator_to_next. */
7154
7155 static int
7156 next_element_from_display_vector (struct it *it)
7157 {
7158 Lisp_Object gc;
7159
7160 /* Precondition. */
7161 xassert (it->dpvec && it->current.dpvec_index >= 0);
7162
7163 it->face_id = it->saved_face_id;
7164
7165 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7166 That seemed totally bogus - so I changed it... */
7167 gc = it->dpvec[it->current.dpvec_index];
7168
7169 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
7170 {
7171 it->c = GLYPH_CODE_CHAR (gc);
7172 it->len = CHAR_BYTES (it->c);
7173
7174 /* The entry may contain a face id to use. Such a face id is
7175 the id of a Lisp face, not a realized face. A face id of
7176 zero means no face is specified. */
7177 if (it->dpvec_face_id >= 0)
7178 it->face_id = it->dpvec_face_id;
7179 else
7180 {
7181 EMACS_INT lface_id = GLYPH_CODE_FACE (gc);
7182 if (lface_id > 0)
7183 it->face_id = merge_faces (it->f, Qt, lface_id,
7184 it->saved_face_id);
7185 }
7186 }
7187 else
7188 /* Display table entry is invalid. Return a space. */
7189 it->c = ' ', it->len = 1;
7190
7191 /* Don't change position and object of the iterator here. They are
7192 still the values of the character that had this display table
7193 entry or was translated, and that's what we want. */
7194 it->what = IT_CHARACTER;
7195 return 1;
7196 }
7197
7198 /* Get the first element of string/buffer in the visual order, after
7199 being reseated to a new position in a string or a buffer. */
7200 static void
7201 get_visually_first_element (struct it *it)
7202 {
7203 int string_p = STRINGP (it->string) || it->s;
7204 EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
7205 EMACS_INT bob = (string_p ? 0 : BEGV);
7206
7207 if (STRINGP (it->string))
7208 {
7209 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7210 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7211 }
7212 else
7213 {
7214 it->bidi_it.charpos = IT_CHARPOS (*it);
7215 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7216 }
7217
7218 if (it->bidi_it.charpos == eob)
7219 {
7220 /* Nothing to do, but reset the FIRST_ELT flag, like
7221 bidi_paragraph_init does, because we are not going to
7222 call it. */
7223 it->bidi_it.first_elt = 0;
7224 }
7225 else if (it->bidi_it.charpos == bob
7226 || (!string_p
7227 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7228 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7229 {
7230 /* If we are at the beginning of a line/string, we can produce
7231 the next element right away. */
7232 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7233 bidi_move_to_visually_next (&it->bidi_it);
7234 }
7235 else
7236 {
7237 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
7238
7239 /* We need to prime the bidi iterator starting at the line's or
7240 string's beginning, before we will be able to produce the
7241 next element. */
7242 if (string_p)
7243 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7244 else
7245 {
7246 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7247 -1);
7248 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7249 }
7250 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7251 do
7252 {
7253 /* Now return to buffer/string position where we were asked
7254 to get the next display element, and produce that. */
7255 bidi_move_to_visually_next (&it->bidi_it);
7256 }
7257 while (it->bidi_it.bytepos != orig_bytepos
7258 && it->bidi_it.charpos < eob);
7259 }
7260
7261 /* Adjust IT's position information to where we ended up. */
7262 if (STRINGP (it->string))
7263 {
7264 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7265 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7266 }
7267 else
7268 {
7269 IT_CHARPOS (*it) = it->bidi_it.charpos;
7270 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7271 }
7272
7273 if (STRINGP (it->string) || !it->s)
7274 {
7275 EMACS_INT stop, charpos, bytepos;
7276
7277 if (STRINGP (it->string))
7278 {
7279 xassert (!it->s);
7280 stop = SCHARS (it->string);
7281 if (stop > it->end_charpos)
7282 stop = it->end_charpos;
7283 charpos = IT_STRING_CHARPOS (*it);
7284 bytepos = IT_STRING_BYTEPOS (*it);
7285 }
7286 else
7287 {
7288 stop = it->end_charpos;
7289 charpos = IT_CHARPOS (*it);
7290 bytepos = IT_BYTEPOS (*it);
7291 }
7292 if (it->bidi_it.scan_dir < 0)
7293 stop = -1;
7294 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7295 it->string);
7296 }
7297 }
7298
7299 /* Load IT with the next display element from Lisp string IT->string.
7300 IT->current.string_pos is the current position within the string.
7301 If IT->current.overlay_string_index >= 0, the Lisp string is an
7302 overlay string. */
7303
7304 static int
7305 next_element_from_string (struct it *it)
7306 {
7307 struct text_pos position;
7308
7309 xassert (STRINGP (it->string));
7310 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7311 xassert (IT_STRING_CHARPOS (*it) >= 0);
7312 position = it->current.string_pos;
7313
7314 /* With bidi reordering, the character to display might not be the
7315 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7316 that we were reseat()ed to a new string, whose paragraph
7317 direction is not known. */
7318 if (it->bidi_p && it->bidi_it.first_elt)
7319 {
7320 get_visually_first_element (it);
7321 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7322 }
7323
7324 /* Time to check for invisible text? */
7325 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7326 {
7327 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7328 {
7329 if (!(!it->bidi_p
7330 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7331 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7332 {
7333 /* With bidi non-linear iteration, we could find
7334 ourselves far beyond the last computed stop_charpos,
7335 with several other stop positions in between that we
7336 missed. Scan them all now, in buffer's logical
7337 order, until we find and handle the last stop_charpos
7338 that precedes our current position. */
7339 handle_stop_backwards (it, it->stop_charpos);
7340 return GET_NEXT_DISPLAY_ELEMENT (it);
7341 }
7342 else
7343 {
7344 if (it->bidi_p)
7345 {
7346 /* Take note of the stop position we just moved
7347 across, for when we will move back across it. */
7348 it->prev_stop = it->stop_charpos;
7349 /* If we are at base paragraph embedding level, take
7350 note of the last stop position seen at this
7351 level. */
7352 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7353 it->base_level_stop = it->stop_charpos;
7354 }
7355 handle_stop (it);
7356
7357 /* Since a handler may have changed IT->method, we must
7358 recurse here. */
7359 return GET_NEXT_DISPLAY_ELEMENT (it);
7360 }
7361 }
7362 else if (it->bidi_p
7363 /* If we are before prev_stop, we may have overstepped
7364 on our way backwards a stop_pos, and if so, we need
7365 to handle that stop_pos. */
7366 && IT_STRING_CHARPOS (*it) < it->prev_stop
7367 /* We can sometimes back up for reasons that have nothing
7368 to do with bidi reordering. E.g., compositions. The
7369 code below is only needed when we are above the base
7370 embedding level, so test for that explicitly. */
7371 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7372 {
7373 /* If we lost track of base_level_stop, we have no better
7374 place for handle_stop_backwards to start from than string
7375 beginning. This happens, e.g., when we were reseated to
7376 the previous screenful of text by vertical-motion. */
7377 if (it->base_level_stop <= 0
7378 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7379 it->base_level_stop = 0;
7380 handle_stop_backwards (it, it->base_level_stop);
7381 return GET_NEXT_DISPLAY_ELEMENT (it);
7382 }
7383 }
7384
7385 if (it->current.overlay_string_index >= 0)
7386 {
7387 /* Get the next character from an overlay string. In overlay
7388 strings, there is no field width or padding with spaces to
7389 do. */
7390 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7391 {
7392 it->what = IT_EOB;
7393 return 0;
7394 }
7395 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7396 IT_STRING_BYTEPOS (*it),
7397 it->bidi_it.scan_dir < 0
7398 ? -1
7399 : SCHARS (it->string))
7400 && next_element_from_composition (it))
7401 {
7402 return 1;
7403 }
7404 else if (STRING_MULTIBYTE (it->string))
7405 {
7406 const unsigned char *s = (SDATA (it->string)
7407 + IT_STRING_BYTEPOS (*it));
7408 it->c = string_char_and_length (s, &it->len);
7409 }
7410 else
7411 {
7412 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7413 it->len = 1;
7414 }
7415 }
7416 else
7417 {
7418 /* Get the next character from a Lisp string that is not an
7419 overlay string. Such strings come from the mode line, for
7420 example. We may have to pad with spaces, or truncate the
7421 string. See also next_element_from_c_string. */
7422 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7423 {
7424 it->what = IT_EOB;
7425 return 0;
7426 }
7427 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7428 {
7429 /* Pad with spaces. */
7430 it->c = ' ', it->len = 1;
7431 CHARPOS (position) = BYTEPOS (position) = -1;
7432 }
7433 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7434 IT_STRING_BYTEPOS (*it),
7435 it->bidi_it.scan_dir < 0
7436 ? -1
7437 : it->string_nchars)
7438 && next_element_from_composition (it))
7439 {
7440 return 1;
7441 }
7442 else if (STRING_MULTIBYTE (it->string))
7443 {
7444 const unsigned char *s = (SDATA (it->string)
7445 + IT_STRING_BYTEPOS (*it));
7446 it->c = string_char_and_length (s, &it->len);
7447 }
7448 else
7449 {
7450 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7451 it->len = 1;
7452 }
7453 }
7454
7455 /* Record what we have and where it came from. */
7456 it->what = IT_CHARACTER;
7457 it->object = it->string;
7458 it->position = position;
7459 return 1;
7460 }
7461
7462
7463 /* Load IT with next display element from C string IT->s.
7464 IT->string_nchars is the maximum number of characters to return
7465 from the string. IT->end_charpos may be greater than
7466 IT->string_nchars when this function is called, in which case we
7467 may have to return padding spaces. Value is zero if end of string
7468 reached, including padding spaces. */
7469
7470 static int
7471 next_element_from_c_string (struct it *it)
7472 {
7473 int success_p = 1;
7474
7475 xassert (it->s);
7476 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7477 it->what = IT_CHARACTER;
7478 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7479 it->object = Qnil;
7480
7481 /* With bidi reordering, the character to display might not be the
7482 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7483 we were reseated to a new string, whose paragraph direction is
7484 not known. */
7485 if (it->bidi_p && it->bidi_it.first_elt)
7486 get_visually_first_element (it);
7487
7488 /* IT's position can be greater than IT->string_nchars in case a
7489 field width or precision has been specified when the iterator was
7490 initialized. */
7491 if (IT_CHARPOS (*it) >= it->end_charpos)
7492 {
7493 /* End of the game. */
7494 it->what = IT_EOB;
7495 success_p = 0;
7496 }
7497 else if (IT_CHARPOS (*it) >= it->string_nchars)
7498 {
7499 /* Pad with spaces. */
7500 it->c = ' ', it->len = 1;
7501 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7502 }
7503 else if (it->multibyte_p)
7504 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7505 else
7506 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7507
7508 return success_p;
7509 }
7510
7511
7512 /* Set up IT to return characters from an ellipsis, if appropriate.
7513 The definition of the ellipsis glyphs may come from a display table
7514 entry. This function fills IT with the first glyph from the
7515 ellipsis if an ellipsis is to be displayed. */
7516
7517 static int
7518 next_element_from_ellipsis (struct it *it)
7519 {
7520 if (it->selective_display_ellipsis_p)
7521 setup_for_ellipsis (it, it->len);
7522 else
7523 {
7524 /* The face at the current position may be different from the
7525 face we find after the invisible text. Remember what it
7526 was in IT->saved_face_id, and signal that it's there by
7527 setting face_before_selective_p. */
7528 it->saved_face_id = it->face_id;
7529 it->method = GET_FROM_BUFFER;
7530 it->object = it->w->buffer;
7531 reseat_at_next_visible_line_start (it, 1);
7532 it->face_before_selective_p = 1;
7533 }
7534
7535 return GET_NEXT_DISPLAY_ELEMENT (it);
7536 }
7537
7538
7539 /* Deliver an image display element. The iterator IT is already
7540 filled with image information (done in handle_display_prop). Value
7541 is always 1. */
7542
7543
7544 static int
7545 next_element_from_image (struct it *it)
7546 {
7547 it->what = IT_IMAGE;
7548 it->ignore_overlay_strings_at_pos_p = 0;
7549 return 1;
7550 }
7551
7552
7553 /* Fill iterator IT with next display element from a stretch glyph
7554 property. IT->object is the value of the text property. Value is
7555 always 1. */
7556
7557 static int
7558 next_element_from_stretch (struct it *it)
7559 {
7560 it->what = IT_STRETCH;
7561 return 1;
7562 }
7563
7564 /* Scan backwards from IT's current position until we find a stop
7565 position, or until BEGV. This is called when we find ourself
7566 before both the last known prev_stop and base_level_stop while
7567 reordering bidirectional text. */
7568
7569 static void
7570 compute_stop_pos_backwards (struct it *it)
7571 {
7572 const int SCAN_BACK_LIMIT = 1000;
7573 struct text_pos pos;
7574 struct display_pos save_current = it->current;
7575 struct text_pos save_position = it->position;
7576 EMACS_INT charpos = IT_CHARPOS (*it);
7577 EMACS_INT where_we_are = charpos;
7578 EMACS_INT save_stop_pos = it->stop_charpos;
7579 EMACS_INT save_end_pos = it->end_charpos;
7580
7581 xassert (NILP (it->string) && !it->s);
7582 xassert (it->bidi_p);
7583 it->bidi_p = 0;
7584 do
7585 {
7586 it->end_charpos = min (charpos + 1, ZV);
7587 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7588 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7589 reseat_1 (it, pos, 0);
7590 compute_stop_pos (it);
7591 /* We must advance forward, right? */
7592 if (it->stop_charpos <= charpos)
7593 abort ();
7594 }
7595 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7596
7597 if (it->stop_charpos <= where_we_are)
7598 it->prev_stop = it->stop_charpos;
7599 else
7600 it->prev_stop = BEGV;
7601 it->bidi_p = 1;
7602 it->current = save_current;
7603 it->position = save_position;
7604 it->stop_charpos = save_stop_pos;
7605 it->end_charpos = save_end_pos;
7606 }
7607
7608 /* Scan forward from CHARPOS in the current buffer/string, until we
7609 find a stop position > current IT's position. Then handle the stop
7610 position before that. This is called when we bump into a stop
7611 position while reordering bidirectional text. CHARPOS should be
7612 the last previously processed stop_pos (or BEGV/0, if none were
7613 processed yet) whose position is less that IT's current
7614 position. */
7615
7616 static void
7617 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7618 {
7619 int bufp = !STRINGP (it->string);
7620 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7621 struct display_pos save_current = it->current;
7622 struct text_pos save_position = it->position;
7623 struct text_pos pos1;
7624 EMACS_INT next_stop;
7625
7626 /* Scan in strict logical order. */
7627 xassert (it->bidi_p);
7628 it->bidi_p = 0;
7629 do
7630 {
7631 it->prev_stop = charpos;
7632 if (bufp)
7633 {
7634 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7635 reseat_1 (it, pos1, 0);
7636 }
7637 else
7638 it->current.string_pos = string_pos (charpos, it->string);
7639 compute_stop_pos (it);
7640 /* We must advance forward, right? */
7641 if (it->stop_charpos <= it->prev_stop)
7642 abort ();
7643 charpos = it->stop_charpos;
7644 }
7645 while (charpos <= where_we_are);
7646
7647 it->bidi_p = 1;
7648 it->current = save_current;
7649 it->position = save_position;
7650 next_stop = it->stop_charpos;
7651 it->stop_charpos = it->prev_stop;
7652 handle_stop (it);
7653 it->stop_charpos = next_stop;
7654 }
7655
7656 /* Load IT with the next display element from current_buffer. Value
7657 is zero if end of buffer reached. IT->stop_charpos is the next
7658 position at which to stop and check for text properties or buffer
7659 end. */
7660
7661 static int
7662 next_element_from_buffer (struct it *it)
7663 {
7664 int success_p = 1;
7665
7666 xassert (IT_CHARPOS (*it) >= BEGV);
7667 xassert (NILP (it->string) && !it->s);
7668 xassert (!it->bidi_p
7669 || (EQ (it->bidi_it.string.lstring, Qnil)
7670 && it->bidi_it.string.s == NULL));
7671
7672 /* With bidi reordering, the character to display might not be the
7673 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7674 we were reseat()ed to a new buffer position, which is potentially
7675 a different paragraph. */
7676 if (it->bidi_p && it->bidi_it.first_elt)
7677 {
7678 get_visually_first_element (it);
7679 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7680 }
7681
7682 if (IT_CHARPOS (*it) >= it->stop_charpos)
7683 {
7684 if (IT_CHARPOS (*it) >= it->end_charpos)
7685 {
7686 int overlay_strings_follow_p;
7687
7688 /* End of the game, except when overlay strings follow that
7689 haven't been returned yet. */
7690 if (it->overlay_strings_at_end_processed_p)
7691 overlay_strings_follow_p = 0;
7692 else
7693 {
7694 it->overlay_strings_at_end_processed_p = 1;
7695 overlay_strings_follow_p = get_overlay_strings (it, 0);
7696 }
7697
7698 if (overlay_strings_follow_p)
7699 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7700 else
7701 {
7702 it->what = IT_EOB;
7703 it->position = it->current.pos;
7704 success_p = 0;
7705 }
7706 }
7707 else if (!(!it->bidi_p
7708 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7709 || IT_CHARPOS (*it) == it->stop_charpos))
7710 {
7711 /* With bidi non-linear iteration, we could find ourselves
7712 far beyond the last computed stop_charpos, with several
7713 other stop positions in between that we missed. Scan
7714 them all now, in buffer's logical order, until we find
7715 and handle the last stop_charpos that precedes our
7716 current position. */
7717 handle_stop_backwards (it, it->stop_charpos);
7718 return GET_NEXT_DISPLAY_ELEMENT (it);
7719 }
7720 else
7721 {
7722 if (it->bidi_p)
7723 {
7724 /* Take note of the stop position we just moved across,
7725 for when we will move back across it. */
7726 it->prev_stop = it->stop_charpos;
7727 /* If we are at base paragraph embedding level, take
7728 note of the last stop position seen at this
7729 level. */
7730 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7731 it->base_level_stop = it->stop_charpos;
7732 }
7733 handle_stop (it);
7734 return GET_NEXT_DISPLAY_ELEMENT (it);
7735 }
7736 }
7737 else if (it->bidi_p
7738 /* If we are before prev_stop, we may have overstepped on
7739 our way backwards a stop_pos, and if so, we need to
7740 handle that stop_pos. */
7741 && IT_CHARPOS (*it) < it->prev_stop
7742 /* We can sometimes back up for reasons that have nothing
7743 to do with bidi reordering. E.g., compositions. The
7744 code below is only needed when we are above the base
7745 embedding level, so test for that explicitly. */
7746 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7747 {
7748 if (it->base_level_stop <= 0
7749 || IT_CHARPOS (*it) < it->base_level_stop)
7750 {
7751 /* If we lost track of base_level_stop, we need to find
7752 prev_stop by looking backwards. This happens, e.g., when
7753 we were reseated to the previous screenful of text by
7754 vertical-motion. */
7755 it->base_level_stop = BEGV;
7756 compute_stop_pos_backwards (it);
7757 handle_stop_backwards (it, it->prev_stop);
7758 }
7759 else
7760 handle_stop_backwards (it, it->base_level_stop);
7761 return GET_NEXT_DISPLAY_ELEMENT (it);
7762 }
7763 else
7764 {
7765 /* No face changes, overlays etc. in sight, so just return a
7766 character from current_buffer. */
7767 unsigned char *p;
7768 EMACS_INT stop;
7769
7770 /* Maybe run the redisplay end trigger hook. Performance note:
7771 This doesn't seem to cost measurable time. */
7772 if (it->redisplay_end_trigger_charpos
7773 && it->glyph_row
7774 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7775 run_redisplay_end_trigger_hook (it);
7776
7777 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7778 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7779 stop)
7780 && next_element_from_composition (it))
7781 {
7782 return 1;
7783 }
7784
7785 /* Get the next character, maybe multibyte. */
7786 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7787 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7788 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7789 else
7790 it->c = *p, it->len = 1;
7791
7792 /* Record what we have and where it came from. */
7793 it->what = IT_CHARACTER;
7794 it->object = it->w->buffer;
7795 it->position = it->current.pos;
7796
7797 /* Normally we return the character found above, except when we
7798 really want to return an ellipsis for selective display. */
7799 if (it->selective)
7800 {
7801 if (it->c == '\n')
7802 {
7803 /* A value of selective > 0 means hide lines indented more
7804 than that number of columns. */
7805 if (it->selective > 0
7806 && IT_CHARPOS (*it) + 1 < ZV
7807 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7808 IT_BYTEPOS (*it) + 1,
7809 it->selective))
7810 {
7811 success_p = next_element_from_ellipsis (it);
7812 it->dpvec_char_len = -1;
7813 }
7814 }
7815 else if (it->c == '\r' && it->selective == -1)
7816 {
7817 /* A value of selective == -1 means that everything from the
7818 CR to the end of the line is invisible, with maybe an
7819 ellipsis displayed for it. */
7820 success_p = next_element_from_ellipsis (it);
7821 it->dpvec_char_len = -1;
7822 }
7823 }
7824 }
7825
7826 /* Value is zero if end of buffer reached. */
7827 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7828 return success_p;
7829 }
7830
7831
7832 /* Run the redisplay end trigger hook for IT. */
7833
7834 static void
7835 run_redisplay_end_trigger_hook (struct it *it)
7836 {
7837 Lisp_Object args[3];
7838
7839 /* IT->glyph_row should be non-null, i.e. we should be actually
7840 displaying something, or otherwise we should not run the hook. */
7841 xassert (it->glyph_row);
7842
7843 /* Set up hook arguments. */
7844 args[0] = Qredisplay_end_trigger_functions;
7845 args[1] = it->window;
7846 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7847 it->redisplay_end_trigger_charpos = 0;
7848
7849 /* Since we are *trying* to run these functions, don't try to run
7850 them again, even if they get an error. */
7851 it->w->redisplay_end_trigger = Qnil;
7852 Frun_hook_with_args (3, args);
7853
7854 /* Notice if it changed the face of the character we are on. */
7855 handle_face_prop (it);
7856 }
7857
7858
7859 /* Deliver a composition display element. Unlike the other
7860 next_element_from_XXX, this function is not registered in the array
7861 get_next_element[]. It is called from next_element_from_buffer and
7862 next_element_from_string when necessary. */
7863
7864 static int
7865 next_element_from_composition (struct it *it)
7866 {
7867 it->what = IT_COMPOSITION;
7868 it->len = it->cmp_it.nbytes;
7869 if (STRINGP (it->string))
7870 {
7871 if (it->c < 0)
7872 {
7873 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7874 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7875 return 0;
7876 }
7877 it->position = it->current.string_pos;
7878 it->object = it->string;
7879 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7880 IT_STRING_BYTEPOS (*it), it->string);
7881 }
7882 else
7883 {
7884 if (it->c < 0)
7885 {
7886 IT_CHARPOS (*it) += it->cmp_it.nchars;
7887 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7888 if (it->bidi_p)
7889 {
7890 if (it->bidi_it.new_paragraph)
7891 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7892 /* Resync the bidi iterator with IT's new position.
7893 FIXME: this doesn't support bidirectional text. */
7894 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7895 bidi_move_to_visually_next (&it->bidi_it);
7896 }
7897 return 0;
7898 }
7899 it->position = it->current.pos;
7900 it->object = it->w->buffer;
7901 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7902 IT_BYTEPOS (*it), Qnil);
7903 }
7904 return 1;
7905 }
7906
7907
7908 \f
7909 /***********************************************************************
7910 Moving an iterator without producing glyphs
7911 ***********************************************************************/
7912
7913 /* Check if iterator is at a position corresponding to a valid buffer
7914 position after some move_it_ call. */
7915
7916 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7917 ((it)->method == GET_FROM_STRING \
7918 ? IT_STRING_CHARPOS (*it) == 0 \
7919 : 1)
7920
7921
7922 /* Move iterator IT to a specified buffer or X position within one
7923 line on the display without producing glyphs.
7924
7925 OP should be a bit mask including some or all of these bits:
7926 MOVE_TO_X: Stop upon reaching x-position TO_X.
7927 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7928 Regardless of OP's value, stop upon reaching the end of the display line.
7929
7930 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7931 This means, in particular, that TO_X includes window's horizontal
7932 scroll amount.
7933
7934 The return value has several possible values that
7935 say what condition caused the scan to stop:
7936
7937 MOVE_POS_MATCH_OR_ZV
7938 - when TO_POS or ZV was reached.
7939
7940 MOVE_X_REACHED
7941 -when TO_X was reached before TO_POS or ZV were reached.
7942
7943 MOVE_LINE_CONTINUED
7944 - when we reached the end of the display area and the line must
7945 be continued.
7946
7947 MOVE_LINE_TRUNCATED
7948 - when we reached the end of the display area and the line is
7949 truncated.
7950
7951 MOVE_NEWLINE_OR_CR
7952 - when we stopped at a line end, i.e. a newline or a CR and selective
7953 display is on. */
7954
7955 static enum move_it_result
7956 move_it_in_display_line_to (struct it *it,
7957 EMACS_INT to_charpos, int to_x,
7958 enum move_operation_enum op)
7959 {
7960 enum move_it_result result = MOVE_UNDEFINED;
7961 struct glyph_row *saved_glyph_row;
7962 struct it wrap_it, atpos_it, atx_it, ppos_it;
7963 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
7964 void *ppos_data = NULL;
7965 int may_wrap = 0;
7966 enum it_method prev_method = it->method;
7967 EMACS_INT prev_pos = IT_CHARPOS (*it);
7968 int saw_smaller_pos = prev_pos < to_charpos;
7969
7970 /* Don't produce glyphs in produce_glyphs. */
7971 saved_glyph_row = it->glyph_row;
7972 it->glyph_row = NULL;
7973
7974 /* Use wrap_it to save a copy of IT wherever a word wrap could
7975 occur. Use atpos_it to save a copy of IT at the desired buffer
7976 position, if found, so that we can scan ahead and check if the
7977 word later overshoots the window edge. Use atx_it similarly, for
7978 pixel positions. */
7979 wrap_it.sp = -1;
7980 atpos_it.sp = -1;
7981 atx_it.sp = -1;
7982
7983 /* Use ppos_it under bidi reordering to save a copy of IT for the
7984 position > CHARPOS that is the closest to CHARPOS. We restore
7985 that position in IT when we have scanned the entire display line
7986 without finding a match for CHARPOS and all the character
7987 positions are greater than CHARPOS. */
7988 if (it->bidi_p)
7989 {
7990 SAVE_IT (ppos_it, *it, ppos_data);
7991 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
7992 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
7993 SAVE_IT (ppos_it, *it, ppos_data);
7994 }
7995
7996 #define BUFFER_POS_REACHED_P() \
7997 ((op & MOVE_TO_POS) != 0 \
7998 && BUFFERP (it->object) \
7999 && (IT_CHARPOS (*it) == to_charpos \
8000 || ((!it->bidi_p \
8001 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8002 && IT_CHARPOS (*it) > to_charpos) \
8003 || (it->what == IT_COMPOSITION \
8004 && ((IT_CHARPOS (*it) > to_charpos \
8005 && to_charpos >= it->cmp_it.charpos) \
8006 || (IT_CHARPOS (*it) < to_charpos \
8007 && to_charpos <= it->cmp_it.charpos)))) \
8008 && (it->method == GET_FROM_BUFFER \
8009 || (it->method == GET_FROM_DISPLAY_VECTOR \
8010 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8011
8012 /* If there's a line-/wrap-prefix, handle it. */
8013 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8014 && it->current_y < it->last_visible_y)
8015 handle_line_prefix (it);
8016
8017 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8018 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8019
8020 while (1)
8021 {
8022 int x, i, ascent = 0, descent = 0;
8023
8024 /* Utility macro to reset an iterator with x, ascent, and descent. */
8025 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8026 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8027 (IT)->max_descent = descent)
8028
8029 /* Stop if we move beyond TO_CHARPOS (after an image or a
8030 display string or stretch glyph). */
8031 if ((op & MOVE_TO_POS) != 0
8032 && BUFFERP (it->object)
8033 && it->method == GET_FROM_BUFFER
8034 && (((!it->bidi_p
8035 /* When the iterator is at base embedding level, we
8036 are guaranteed that characters are delivered for
8037 display in strictly increasing order of their
8038 buffer positions. */
8039 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8040 && IT_CHARPOS (*it) > to_charpos)
8041 || (it->bidi_p
8042 && (prev_method == GET_FROM_IMAGE
8043 || prev_method == GET_FROM_STRETCH
8044 || prev_method == GET_FROM_STRING)
8045 /* Passed TO_CHARPOS from left to right. */
8046 && ((prev_pos < to_charpos
8047 && IT_CHARPOS (*it) > to_charpos)
8048 /* Passed TO_CHARPOS from right to left. */
8049 || (prev_pos > to_charpos
8050 && IT_CHARPOS (*it) < to_charpos)))))
8051 {
8052 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8053 {
8054 result = MOVE_POS_MATCH_OR_ZV;
8055 break;
8056 }
8057 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8058 /* If wrap_it is valid, the current position might be in a
8059 word that is wrapped. So, save the iterator in
8060 atpos_it and continue to see if wrapping happens. */
8061 SAVE_IT (atpos_it, *it, atpos_data);
8062 }
8063
8064 /* Stop when ZV reached.
8065 We used to stop here when TO_CHARPOS reached as well, but that is
8066 too soon if this glyph does not fit on this line. So we handle it
8067 explicitly below. */
8068 if (!get_next_display_element (it))
8069 {
8070 result = MOVE_POS_MATCH_OR_ZV;
8071 break;
8072 }
8073
8074 if (it->line_wrap == TRUNCATE)
8075 {
8076 if (BUFFER_POS_REACHED_P ())
8077 {
8078 result = MOVE_POS_MATCH_OR_ZV;
8079 break;
8080 }
8081 }
8082 else
8083 {
8084 if (it->line_wrap == WORD_WRAP)
8085 {
8086 if (IT_DISPLAYING_WHITESPACE (it))
8087 may_wrap = 1;
8088 else if (may_wrap)
8089 {
8090 /* We have reached a glyph that follows one or more
8091 whitespace characters. If the position is
8092 already found, we are done. */
8093 if (atpos_it.sp >= 0)
8094 {
8095 RESTORE_IT (it, &atpos_it, atpos_data);
8096 result = MOVE_POS_MATCH_OR_ZV;
8097 goto done;
8098 }
8099 if (atx_it.sp >= 0)
8100 {
8101 RESTORE_IT (it, &atx_it, atx_data);
8102 result = MOVE_X_REACHED;
8103 goto done;
8104 }
8105 /* Otherwise, we can wrap here. */
8106 SAVE_IT (wrap_it, *it, wrap_data);
8107 may_wrap = 0;
8108 }
8109 }
8110 }
8111
8112 /* Remember the line height for the current line, in case
8113 the next element doesn't fit on the line. */
8114 ascent = it->max_ascent;
8115 descent = it->max_descent;
8116
8117 /* The call to produce_glyphs will get the metrics of the
8118 display element IT is loaded with. Record the x-position
8119 before this display element, in case it doesn't fit on the
8120 line. */
8121 x = it->current_x;
8122
8123 PRODUCE_GLYPHS (it);
8124
8125 if (it->area != TEXT_AREA)
8126 {
8127 prev_method = it->method;
8128 if (it->method == GET_FROM_BUFFER)
8129 prev_pos = IT_CHARPOS (*it);
8130 set_iterator_to_next (it, 1);
8131 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8132 SET_TEXT_POS (this_line_min_pos,
8133 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8134 if (it->bidi_p
8135 && (op & MOVE_TO_POS)
8136 && IT_CHARPOS (*it) > to_charpos
8137 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8138 SAVE_IT (ppos_it, *it, ppos_data);
8139 continue;
8140 }
8141
8142 /* The number of glyphs we get back in IT->nglyphs will normally
8143 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8144 character on a terminal frame, or (iii) a line end. For the
8145 second case, IT->nglyphs - 1 padding glyphs will be present.
8146 (On X frames, there is only one glyph produced for a
8147 composite character.)
8148
8149 The behavior implemented below means, for continuation lines,
8150 that as many spaces of a TAB as fit on the current line are
8151 displayed there. For terminal frames, as many glyphs of a
8152 multi-glyph character are displayed in the current line, too.
8153 This is what the old redisplay code did, and we keep it that
8154 way. Under X, the whole shape of a complex character must
8155 fit on the line or it will be completely displayed in the
8156 next line.
8157
8158 Note that both for tabs and padding glyphs, all glyphs have
8159 the same width. */
8160 if (it->nglyphs)
8161 {
8162 /* More than one glyph or glyph doesn't fit on line. All
8163 glyphs have the same width. */
8164 int single_glyph_width = it->pixel_width / it->nglyphs;
8165 int new_x;
8166 int x_before_this_char = x;
8167 int hpos_before_this_char = it->hpos;
8168
8169 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8170 {
8171 new_x = x + single_glyph_width;
8172
8173 /* We want to leave anything reaching TO_X to the caller. */
8174 if ((op & MOVE_TO_X) && new_x > to_x)
8175 {
8176 if (BUFFER_POS_REACHED_P ())
8177 {
8178 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8179 goto buffer_pos_reached;
8180 if (atpos_it.sp < 0)
8181 {
8182 SAVE_IT (atpos_it, *it, atpos_data);
8183 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8184 }
8185 }
8186 else
8187 {
8188 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8189 {
8190 it->current_x = x;
8191 result = MOVE_X_REACHED;
8192 break;
8193 }
8194 if (atx_it.sp < 0)
8195 {
8196 SAVE_IT (atx_it, *it, atx_data);
8197 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8198 }
8199 }
8200 }
8201
8202 if (/* Lines are continued. */
8203 it->line_wrap != TRUNCATE
8204 && (/* And glyph doesn't fit on the line. */
8205 new_x > it->last_visible_x
8206 /* Or it fits exactly and we're on a window
8207 system frame. */
8208 || (new_x == it->last_visible_x
8209 && FRAME_WINDOW_P (it->f))))
8210 {
8211 if (/* IT->hpos == 0 means the very first glyph
8212 doesn't fit on the line, e.g. a wide image. */
8213 it->hpos == 0
8214 || (new_x == it->last_visible_x
8215 && FRAME_WINDOW_P (it->f)))
8216 {
8217 ++it->hpos;
8218 it->current_x = new_x;
8219
8220 /* The character's last glyph just barely fits
8221 in this row. */
8222 if (i == it->nglyphs - 1)
8223 {
8224 /* If this is the destination position,
8225 return a position *before* it in this row,
8226 now that we know it fits in this row. */
8227 if (BUFFER_POS_REACHED_P ())
8228 {
8229 if (it->line_wrap != WORD_WRAP
8230 || wrap_it.sp < 0)
8231 {
8232 it->hpos = hpos_before_this_char;
8233 it->current_x = x_before_this_char;
8234 result = MOVE_POS_MATCH_OR_ZV;
8235 break;
8236 }
8237 if (it->line_wrap == WORD_WRAP
8238 && atpos_it.sp < 0)
8239 {
8240 SAVE_IT (atpos_it, *it, atpos_data);
8241 atpos_it.current_x = x_before_this_char;
8242 atpos_it.hpos = hpos_before_this_char;
8243 }
8244 }
8245
8246 prev_method = it->method;
8247 if (it->method == GET_FROM_BUFFER)
8248 prev_pos = IT_CHARPOS (*it);
8249 set_iterator_to_next (it, 1);
8250 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8251 SET_TEXT_POS (this_line_min_pos,
8252 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8253 /* On graphical terminals, newlines may
8254 "overflow" into the fringe if
8255 overflow-newline-into-fringe is non-nil.
8256 On text-only terminals, newlines may
8257 overflow into the last glyph on the
8258 display line.*/
8259 if (!FRAME_WINDOW_P (it->f)
8260 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8261 {
8262 if (!get_next_display_element (it))
8263 {
8264 result = MOVE_POS_MATCH_OR_ZV;
8265 break;
8266 }
8267 if (BUFFER_POS_REACHED_P ())
8268 {
8269 if (ITERATOR_AT_END_OF_LINE_P (it))
8270 result = MOVE_POS_MATCH_OR_ZV;
8271 else
8272 result = MOVE_LINE_CONTINUED;
8273 break;
8274 }
8275 if (ITERATOR_AT_END_OF_LINE_P (it))
8276 {
8277 result = MOVE_NEWLINE_OR_CR;
8278 break;
8279 }
8280 }
8281 }
8282 }
8283 else
8284 IT_RESET_X_ASCENT_DESCENT (it);
8285
8286 if (wrap_it.sp >= 0)
8287 {
8288 RESTORE_IT (it, &wrap_it, wrap_data);
8289 atpos_it.sp = -1;
8290 atx_it.sp = -1;
8291 }
8292
8293 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8294 IT_CHARPOS (*it)));
8295 result = MOVE_LINE_CONTINUED;
8296 break;
8297 }
8298
8299 if (BUFFER_POS_REACHED_P ())
8300 {
8301 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8302 goto buffer_pos_reached;
8303 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8304 {
8305 SAVE_IT (atpos_it, *it, atpos_data);
8306 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8307 }
8308 }
8309
8310 if (new_x > it->first_visible_x)
8311 {
8312 /* Glyph is visible. Increment number of glyphs that
8313 would be displayed. */
8314 ++it->hpos;
8315 }
8316 }
8317
8318 if (result != MOVE_UNDEFINED)
8319 break;
8320 }
8321 else if (BUFFER_POS_REACHED_P ())
8322 {
8323 buffer_pos_reached:
8324 IT_RESET_X_ASCENT_DESCENT (it);
8325 result = MOVE_POS_MATCH_OR_ZV;
8326 break;
8327 }
8328 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8329 {
8330 /* Stop when TO_X specified and reached. This check is
8331 necessary here because of lines consisting of a line end,
8332 only. The line end will not produce any glyphs and we
8333 would never get MOVE_X_REACHED. */
8334 xassert (it->nglyphs == 0);
8335 result = MOVE_X_REACHED;
8336 break;
8337 }
8338
8339 /* Is this a line end? If yes, we're done. */
8340 if (ITERATOR_AT_END_OF_LINE_P (it))
8341 {
8342 /* If we are past TO_CHARPOS, but never saw any character
8343 positions smaller than TO_CHARPOS, return
8344 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8345 did. */
8346 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8347 {
8348 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8349 {
8350 if (IT_CHARPOS (ppos_it) < ZV)
8351 {
8352 RESTORE_IT (it, &ppos_it, ppos_data);
8353 result = MOVE_POS_MATCH_OR_ZV;
8354 }
8355 else
8356 goto buffer_pos_reached;
8357 }
8358 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8359 && IT_CHARPOS (*it) > to_charpos)
8360 goto buffer_pos_reached;
8361 else
8362 result = MOVE_NEWLINE_OR_CR;
8363 }
8364 else
8365 result = MOVE_NEWLINE_OR_CR;
8366 break;
8367 }
8368
8369 prev_method = it->method;
8370 if (it->method == GET_FROM_BUFFER)
8371 prev_pos = IT_CHARPOS (*it);
8372 /* The current display element has been consumed. Advance
8373 to the next. */
8374 set_iterator_to_next (it, 1);
8375 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8376 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8377 if (IT_CHARPOS (*it) < to_charpos)
8378 saw_smaller_pos = 1;
8379 if (it->bidi_p
8380 && (op & MOVE_TO_POS)
8381 && IT_CHARPOS (*it) >= to_charpos
8382 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8383 SAVE_IT (ppos_it, *it, ppos_data);
8384
8385 /* Stop if lines are truncated and IT's current x-position is
8386 past the right edge of the window now. */
8387 if (it->line_wrap == TRUNCATE
8388 && it->current_x >= it->last_visible_x)
8389 {
8390 if (!FRAME_WINDOW_P (it->f)
8391 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8392 {
8393 int at_eob_p = 0;
8394
8395 if ((at_eob_p = !get_next_display_element (it))
8396 || BUFFER_POS_REACHED_P ()
8397 /* If we are past TO_CHARPOS, but never saw any
8398 character positions smaller than TO_CHARPOS,
8399 return MOVE_POS_MATCH_OR_ZV, like the
8400 unidirectional display did. */
8401 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8402 && !saw_smaller_pos
8403 && IT_CHARPOS (*it) > to_charpos))
8404 {
8405 if (it->bidi_p
8406 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8407 RESTORE_IT (it, &ppos_it, ppos_data);
8408 result = MOVE_POS_MATCH_OR_ZV;
8409 break;
8410 }
8411 if (ITERATOR_AT_END_OF_LINE_P (it))
8412 {
8413 result = MOVE_NEWLINE_OR_CR;
8414 break;
8415 }
8416 }
8417 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8418 && !saw_smaller_pos
8419 && IT_CHARPOS (*it) > to_charpos)
8420 {
8421 if (IT_CHARPOS (ppos_it) < ZV)
8422 RESTORE_IT (it, &ppos_it, ppos_data);
8423 result = MOVE_POS_MATCH_OR_ZV;
8424 break;
8425 }
8426 result = MOVE_LINE_TRUNCATED;
8427 break;
8428 }
8429 #undef IT_RESET_X_ASCENT_DESCENT
8430 }
8431
8432 #undef BUFFER_POS_REACHED_P
8433
8434 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8435 restore the saved iterator. */
8436 if (atpos_it.sp >= 0)
8437 RESTORE_IT (it, &atpos_it, atpos_data);
8438 else if (atx_it.sp >= 0)
8439 RESTORE_IT (it, &atx_it, atx_data);
8440
8441 done:
8442
8443 if (atpos_data)
8444 bidi_unshelve_cache (atpos_data, 1);
8445 if (atx_data)
8446 bidi_unshelve_cache (atx_data, 1);
8447 if (wrap_data)
8448 bidi_unshelve_cache (wrap_data, 1);
8449 if (ppos_data)
8450 bidi_unshelve_cache (ppos_data, 1);
8451
8452 /* Restore the iterator settings altered at the beginning of this
8453 function. */
8454 it->glyph_row = saved_glyph_row;
8455 return result;
8456 }
8457
8458 /* For external use. */
8459 void
8460 move_it_in_display_line (struct it *it,
8461 EMACS_INT to_charpos, int to_x,
8462 enum move_operation_enum op)
8463 {
8464 if (it->line_wrap == WORD_WRAP
8465 && (op & MOVE_TO_X))
8466 {
8467 struct it save_it;
8468 void *save_data = NULL;
8469 int skip;
8470
8471 SAVE_IT (save_it, *it, save_data);
8472 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8473 /* When word-wrap is on, TO_X may lie past the end
8474 of a wrapped line. Then it->current is the
8475 character on the next line, so backtrack to the
8476 space before the wrap point. */
8477 if (skip == MOVE_LINE_CONTINUED)
8478 {
8479 int prev_x = max (it->current_x - 1, 0);
8480 RESTORE_IT (it, &save_it, save_data);
8481 move_it_in_display_line_to
8482 (it, -1, prev_x, MOVE_TO_X);
8483 }
8484 else
8485 bidi_unshelve_cache (save_data, 1);
8486 }
8487 else
8488 move_it_in_display_line_to (it, to_charpos, to_x, op);
8489 }
8490
8491
8492 /* Move IT forward until it satisfies one or more of the criteria in
8493 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8494
8495 OP is a bit-mask that specifies where to stop, and in particular,
8496 which of those four position arguments makes a difference. See the
8497 description of enum move_operation_enum.
8498
8499 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8500 screen line, this function will set IT to the next position that is
8501 displayed to the right of TO_CHARPOS on the screen. */
8502
8503 void
8504 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
8505 {
8506 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8507 int line_height, line_start_x = 0, reached = 0;
8508 void *backup_data = NULL;
8509
8510 for (;;)
8511 {
8512 if (op & MOVE_TO_VPOS)
8513 {
8514 /* If no TO_CHARPOS and no TO_X specified, stop at the
8515 start of the line TO_VPOS. */
8516 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8517 {
8518 if (it->vpos == to_vpos)
8519 {
8520 reached = 1;
8521 break;
8522 }
8523 else
8524 skip = move_it_in_display_line_to (it, -1, -1, 0);
8525 }
8526 else
8527 {
8528 /* TO_VPOS >= 0 means stop at TO_X in the line at
8529 TO_VPOS, or at TO_POS, whichever comes first. */
8530 if (it->vpos == to_vpos)
8531 {
8532 reached = 2;
8533 break;
8534 }
8535
8536 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8537
8538 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8539 {
8540 reached = 3;
8541 break;
8542 }
8543 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8544 {
8545 /* We have reached TO_X but not in the line we want. */
8546 skip = move_it_in_display_line_to (it, to_charpos,
8547 -1, MOVE_TO_POS);
8548 if (skip == MOVE_POS_MATCH_OR_ZV)
8549 {
8550 reached = 4;
8551 break;
8552 }
8553 }
8554 }
8555 }
8556 else if (op & MOVE_TO_Y)
8557 {
8558 struct it it_backup;
8559
8560 if (it->line_wrap == WORD_WRAP)
8561 SAVE_IT (it_backup, *it, backup_data);
8562
8563 /* TO_Y specified means stop at TO_X in the line containing
8564 TO_Y---or at TO_CHARPOS if this is reached first. The
8565 problem is that we can't really tell whether the line
8566 contains TO_Y before we have completely scanned it, and
8567 this may skip past TO_X. What we do is to first scan to
8568 TO_X.
8569
8570 If TO_X is not specified, use a TO_X of zero. The reason
8571 is to make the outcome of this function more predictable.
8572 If we didn't use TO_X == 0, we would stop at the end of
8573 the line which is probably not what a caller would expect
8574 to happen. */
8575 skip = move_it_in_display_line_to
8576 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8577 (MOVE_TO_X | (op & MOVE_TO_POS)));
8578
8579 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8580 if (skip == MOVE_POS_MATCH_OR_ZV)
8581 reached = 5;
8582 else if (skip == MOVE_X_REACHED)
8583 {
8584 /* If TO_X was reached, we want to know whether TO_Y is
8585 in the line. We know this is the case if the already
8586 scanned glyphs make the line tall enough. Otherwise,
8587 we must check by scanning the rest of the line. */
8588 line_height = it->max_ascent + it->max_descent;
8589 if (to_y >= it->current_y
8590 && to_y < it->current_y + line_height)
8591 {
8592 reached = 6;
8593 break;
8594 }
8595 SAVE_IT (it_backup, *it, backup_data);
8596 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8597 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8598 op & MOVE_TO_POS);
8599 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8600 line_height = it->max_ascent + it->max_descent;
8601 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8602
8603 if (to_y >= it->current_y
8604 && to_y < it->current_y + line_height)
8605 {
8606 /* If TO_Y is in this line and TO_X was reached
8607 above, we scanned too far. We have to restore
8608 IT's settings to the ones before skipping. */
8609 RESTORE_IT (it, &it_backup, backup_data);
8610 reached = 6;
8611 }
8612 else
8613 {
8614 skip = skip2;
8615 if (skip == MOVE_POS_MATCH_OR_ZV)
8616 reached = 7;
8617 }
8618 }
8619 else
8620 {
8621 /* Check whether TO_Y is in this line. */
8622 line_height = it->max_ascent + it->max_descent;
8623 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8624
8625 if (to_y >= it->current_y
8626 && to_y < it->current_y + line_height)
8627 {
8628 /* When word-wrap is on, TO_X may lie past the end
8629 of a wrapped line. Then it->current is the
8630 character on the next line, so backtrack to the
8631 space before the wrap point. */
8632 if (skip == MOVE_LINE_CONTINUED
8633 && it->line_wrap == WORD_WRAP)
8634 {
8635 int prev_x = max (it->current_x - 1, 0);
8636 RESTORE_IT (it, &it_backup, backup_data);
8637 skip = move_it_in_display_line_to
8638 (it, -1, prev_x, MOVE_TO_X);
8639 }
8640 reached = 6;
8641 }
8642 }
8643
8644 if (reached)
8645 break;
8646 }
8647 else if (BUFFERP (it->object)
8648 && (it->method == GET_FROM_BUFFER
8649 || it->method == GET_FROM_STRETCH)
8650 && IT_CHARPOS (*it) >= to_charpos
8651 /* Under bidi iteration, a call to set_iterator_to_next
8652 can scan far beyond to_charpos if the initial
8653 portion of the next line needs to be reordered. In
8654 that case, give move_it_in_display_line_to another
8655 chance below. */
8656 && !(it->bidi_p
8657 && it->bidi_it.scan_dir == -1))
8658 skip = MOVE_POS_MATCH_OR_ZV;
8659 else
8660 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8661
8662 switch (skip)
8663 {
8664 case MOVE_POS_MATCH_OR_ZV:
8665 reached = 8;
8666 goto out;
8667
8668 case MOVE_NEWLINE_OR_CR:
8669 set_iterator_to_next (it, 1);
8670 it->continuation_lines_width = 0;
8671 break;
8672
8673 case MOVE_LINE_TRUNCATED:
8674 it->continuation_lines_width = 0;
8675 reseat_at_next_visible_line_start (it, 0);
8676 if ((op & MOVE_TO_POS) != 0
8677 && IT_CHARPOS (*it) > to_charpos)
8678 {
8679 reached = 9;
8680 goto out;
8681 }
8682 break;
8683
8684 case MOVE_LINE_CONTINUED:
8685 /* For continued lines ending in a tab, some of the glyphs
8686 associated with the tab are displayed on the current
8687 line. Since it->current_x does not include these glyphs,
8688 we use it->last_visible_x instead. */
8689 if (it->c == '\t')
8690 {
8691 it->continuation_lines_width += it->last_visible_x;
8692 /* When moving by vpos, ensure that the iterator really
8693 advances to the next line (bug#847, bug#969). Fixme:
8694 do we need to do this in other circumstances? */
8695 if (it->current_x != it->last_visible_x
8696 && (op & MOVE_TO_VPOS)
8697 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8698 {
8699 line_start_x = it->current_x + it->pixel_width
8700 - it->last_visible_x;
8701 set_iterator_to_next (it, 0);
8702 }
8703 }
8704 else
8705 it->continuation_lines_width += it->current_x;
8706 break;
8707
8708 default:
8709 abort ();
8710 }
8711
8712 /* Reset/increment for the next run. */
8713 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8714 it->current_x = line_start_x;
8715 line_start_x = 0;
8716 it->hpos = 0;
8717 it->current_y += it->max_ascent + it->max_descent;
8718 ++it->vpos;
8719 last_height = it->max_ascent + it->max_descent;
8720 last_max_ascent = it->max_ascent;
8721 it->max_ascent = it->max_descent = 0;
8722 }
8723
8724 out:
8725
8726 /* On text terminals, we may stop at the end of a line in the middle
8727 of a multi-character glyph. If the glyph itself is continued,
8728 i.e. it is actually displayed on the next line, don't treat this
8729 stopping point as valid; move to the next line instead (unless
8730 that brings us offscreen). */
8731 if (!FRAME_WINDOW_P (it->f)
8732 && op & MOVE_TO_POS
8733 && IT_CHARPOS (*it) == to_charpos
8734 && it->what == IT_CHARACTER
8735 && it->nglyphs > 1
8736 && it->line_wrap == WINDOW_WRAP
8737 && it->current_x == it->last_visible_x - 1
8738 && it->c != '\n'
8739 && it->c != '\t'
8740 && it->vpos < XFASTINT (it->w->window_end_vpos))
8741 {
8742 it->continuation_lines_width += it->current_x;
8743 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8744 it->current_y += it->max_ascent + it->max_descent;
8745 ++it->vpos;
8746 last_height = it->max_ascent + it->max_descent;
8747 last_max_ascent = it->max_ascent;
8748 }
8749
8750 if (backup_data)
8751 bidi_unshelve_cache (backup_data, 1);
8752
8753 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8754 }
8755
8756
8757 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8758
8759 If DY > 0, move IT backward at least that many pixels. DY = 0
8760 means move IT backward to the preceding line start or BEGV. This
8761 function may move over more than DY pixels if IT->current_y - DY
8762 ends up in the middle of a line; in this case IT->current_y will be
8763 set to the top of the line moved to. */
8764
8765 void
8766 move_it_vertically_backward (struct it *it, int dy)
8767 {
8768 int nlines, h;
8769 struct it it2, it3;
8770 void *it2data = NULL, *it3data = NULL;
8771 EMACS_INT start_pos;
8772
8773 move_further_back:
8774 xassert (dy >= 0);
8775
8776 start_pos = IT_CHARPOS (*it);
8777
8778 /* Estimate how many newlines we must move back. */
8779 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8780
8781 /* Set the iterator's position that many lines back. */
8782 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8783 back_to_previous_visible_line_start (it);
8784
8785 /* Reseat the iterator here. When moving backward, we don't want
8786 reseat to skip forward over invisible text, set up the iterator
8787 to deliver from overlay strings at the new position etc. So,
8788 use reseat_1 here. */
8789 reseat_1 (it, it->current.pos, 1);
8790
8791 /* We are now surely at a line start. */
8792 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8793 reordering is in effect. */
8794 it->continuation_lines_width = 0;
8795
8796 /* Move forward and see what y-distance we moved. First move to the
8797 start of the next line so that we get its height. We need this
8798 height to be able to tell whether we reached the specified
8799 y-distance. */
8800 SAVE_IT (it2, *it, it2data);
8801 it2.max_ascent = it2.max_descent = 0;
8802 do
8803 {
8804 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8805 MOVE_TO_POS | MOVE_TO_VPOS);
8806 }
8807 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8808 /* If we are in a display string which starts at START_POS,
8809 and that display string includes a newline, and we are
8810 right after that newline (i.e. at the beginning of a
8811 display line), exit the loop, because otherwise we will
8812 infloop, since move_it_to will see that it is already at
8813 START_POS and will not move. */
8814 || (it2.method == GET_FROM_STRING
8815 && IT_CHARPOS (it2) == start_pos
8816 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8817 xassert (IT_CHARPOS (*it) >= BEGV);
8818 SAVE_IT (it3, it2, it3data);
8819
8820 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8821 xassert (IT_CHARPOS (*it) >= BEGV);
8822 /* H is the actual vertical distance from the position in *IT
8823 and the starting position. */
8824 h = it2.current_y - it->current_y;
8825 /* NLINES is the distance in number of lines. */
8826 nlines = it2.vpos - it->vpos;
8827
8828 /* Correct IT's y and vpos position
8829 so that they are relative to the starting point. */
8830 it->vpos -= nlines;
8831 it->current_y -= h;
8832
8833 if (dy == 0)
8834 {
8835 /* DY == 0 means move to the start of the screen line. The
8836 value of nlines is > 0 if continuation lines were involved,
8837 or if the original IT position was at start of a line. */
8838 RESTORE_IT (it, it, it2data);
8839 if (nlines > 0)
8840 move_it_by_lines (it, nlines);
8841 /* The above code moves us to some position NLINES down,
8842 usually to its first glyph (leftmost in an L2R line), but
8843 that's not necessarily the start of the line, under bidi
8844 reordering. We want to get to the character position
8845 that is immediately after the newline of the previous
8846 line. */
8847 if (it->bidi_p
8848 && !it->continuation_lines_width
8849 && !STRINGP (it->string)
8850 && IT_CHARPOS (*it) > BEGV
8851 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8852 {
8853 EMACS_INT nl_pos =
8854 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8855
8856 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8857 }
8858 bidi_unshelve_cache (it3data, 1);
8859 }
8860 else
8861 {
8862 /* The y-position we try to reach, relative to *IT.
8863 Note that H has been subtracted in front of the if-statement. */
8864 int target_y = it->current_y + h - dy;
8865 int y0 = it3.current_y;
8866 int y1;
8867 int line_height;
8868
8869 RESTORE_IT (&it3, &it3, it3data);
8870 y1 = line_bottom_y (&it3);
8871 line_height = y1 - y0;
8872 RESTORE_IT (it, it, it2data);
8873 /* If we did not reach target_y, try to move further backward if
8874 we can. If we moved too far backward, try to move forward. */
8875 if (target_y < it->current_y
8876 /* This is heuristic. In a window that's 3 lines high, with
8877 a line height of 13 pixels each, recentering with point
8878 on the bottom line will try to move -39/2 = 19 pixels
8879 backward. Try to avoid moving into the first line. */
8880 && (it->current_y - target_y
8881 > min (window_box_height (it->w), line_height * 2 / 3))
8882 && IT_CHARPOS (*it) > BEGV)
8883 {
8884 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8885 target_y - it->current_y));
8886 dy = it->current_y - target_y;
8887 goto move_further_back;
8888 }
8889 else if (target_y >= it->current_y + line_height
8890 && IT_CHARPOS (*it) < ZV)
8891 {
8892 /* Should move forward by at least one line, maybe more.
8893
8894 Note: Calling move_it_by_lines can be expensive on
8895 terminal frames, where compute_motion is used (via
8896 vmotion) to do the job, when there are very long lines
8897 and truncate-lines is nil. That's the reason for
8898 treating terminal frames specially here. */
8899
8900 if (!FRAME_WINDOW_P (it->f))
8901 move_it_vertically (it, target_y - (it->current_y + line_height));
8902 else
8903 {
8904 do
8905 {
8906 move_it_by_lines (it, 1);
8907 }
8908 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8909 }
8910 }
8911 }
8912 }
8913
8914
8915 /* Move IT by a specified amount of pixel lines DY. DY negative means
8916 move backwards. DY = 0 means move to start of screen line. At the
8917 end, IT will be on the start of a screen line. */
8918
8919 void
8920 move_it_vertically (struct it *it, int dy)
8921 {
8922 if (dy <= 0)
8923 move_it_vertically_backward (it, -dy);
8924 else
8925 {
8926 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8927 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8928 MOVE_TO_POS | MOVE_TO_Y);
8929 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8930
8931 /* If buffer ends in ZV without a newline, move to the start of
8932 the line to satisfy the post-condition. */
8933 if (IT_CHARPOS (*it) == ZV
8934 && ZV > BEGV
8935 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8936 move_it_by_lines (it, 0);
8937 }
8938 }
8939
8940
8941 /* Move iterator IT past the end of the text line it is in. */
8942
8943 void
8944 move_it_past_eol (struct it *it)
8945 {
8946 enum move_it_result rc;
8947
8948 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8949 if (rc == MOVE_NEWLINE_OR_CR)
8950 set_iterator_to_next (it, 0);
8951 }
8952
8953
8954 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8955 negative means move up. DVPOS == 0 means move to the start of the
8956 screen line.
8957
8958 Optimization idea: If we would know that IT->f doesn't use
8959 a face with proportional font, we could be faster for
8960 truncate-lines nil. */
8961
8962 void
8963 move_it_by_lines (struct it *it, int dvpos)
8964 {
8965
8966 /* The commented-out optimization uses vmotion on terminals. This
8967 gives bad results, because elements like it->what, on which
8968 callers such as pos_visible_p rely, aren't updated. */
8969 /* struct position pos;
8970 if (!FRAME_WINDOW_P (it->f))
8971 {
8972 struct text_pos textpos;
8973
8974 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8975 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8976 reseat (it, textpos, 1);
8977 it->vpos += pos.vpos;
8978 it->current_y += pos.vpos;
8979 }
8980 else */
8981
8982 if (dvpos == 0)
8983 {
8984 /* DVPOS == 0 means move to the start of the screen line. */
8985 move_it_vertically_backward (it, 0);
8986 /* Let next call to line_bottom_y calculate real line height */
8987 last_height = 0;
8988 }
8989 else if (dvpos > 0)
8990 {
8991 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
8992 if (!IT_POS_VALID_AFTER_MOVE_P (it))
8993 {
8994 /* Only move to the next buffer position if we ended up in a
8995 string from display property, not in an overlay string
8996 (before-string or after-string). That is because the
8997 latter don't conceal the underlying buffer position, so
8998 we can ask to move the iterator to the exact position we
8999 are interested in. Note that, even if we are already at
9000 IT_CHARPOS (*it), the call below is not a no-op, as it
9001 will detect that we are at the end of the string, pop the
9002 iterator, and compute it->current_x and it->hpos
9003 correctly. */
9004 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9005 -1, -1, -1, MOVE_TO_POS);
9006 }
9007 }
9008 else
9009 {
9010 struct it it2;
9011 void *it2data = NULL;
9012 EMACS_INT start_charpos, i;
9013
9014 /* Start at the beginning of the screen line containing IT's
9015 position. This may actually move vertically backwards,
9016 in case of overlays, so adjust dvpos accordingly. */
9017 dvpos += it->vpos;
9018 move_it_vertically_backward (it, 0);
9019 dvpos -= it->vpos;
9020
9021 /* Go back -DVPOS visible lines and reseat the iterator there. */
9022 start_charpos = IT_CHARPOS (*it);
9023 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9024 back_to_previous_visible_line_start (it);
9025 reseat (it, it->current.pos, 1);
9026
9027 /* Move further back if we end up in a string or an image. */
9028 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9029 {
9030 /* First try to move to start of display line. */
9031 dvpos += it->vpos;
9032 move_it_vertically_backward (it, 0);
9033 dvpos -= it->vpos;
9034 if (IT_POS_VALID_AFTER_MOVE_P (it))
9035 break;
9036 /* If start of line is still in string or image,
9037 move further back. */
9038 back_to_previous_visible_line_start (it);
9039 reseat (it, it->current.pos, 1);
9040 dvpos--;
9041 }
9042
9043 it->current_x = it->hpos = 0;
9044
9045 /* Above call may have moved too far if continuation lines
9046 are involved. Scan forward and see if it did. */
9047 SAVE_IT (it2, *it, it2data);
9048 it2.vpos = it2.current_y = 0;
9049 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9050 it->vpos -= it2.vpos;
9051 it->current_y -= it2.current_y;
9052 it->current_x = it->hpos = 0;
9053
9054 /* If we moved too far back, move IT some lines forward. */
9055 if (it2.vpos > -dvpos)
9056 {
9057 int delta = it2.vpos + dvpos;
9058
9059 RESTORE_IT (&it2, &it2, it2data);
9060 SAVE_IT (it2, *it, it2data);
9061 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9062 /* Move back again if we got too far ahead. */
9063 if (IT_CHARPOS (*it) >= start_charpos)
9064 RESTORE_IT (it, &it2, it2data);
9065 else
9066 bidi_unshelve_cache (it2data, 1);
9067 }
9068 else
9069 RESTORE_IT (it, it, it2data);
9070 }
9071 }
9072
9073 /* Return 1 if IT points into the middle of a display vector. */
9074
9075 int
9076 in_display_vector_p (struct it *it)
9077 {
9078 return (it->method == GET_FROM_DISPLAY_VECTOR
9079 && it->current.dpvec_index > 0
9080 && it->dpvec + it->current.dpvec_index != it->dpend);
9081 }
9082
9083 \f
9084 /***********************************************************************
9085 Messages
9086 ***********************************************************************/
9087
9088
9089 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9090 to *Messages*. */
9091
9092 void
9093 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9094 {
9095 Lisp_Object args[3];
9096 Lisp_Object msg, fmt;
9097 char *buffer;
9098 EMACS_INT len;
9099 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9100 USE_SAFE_ALLOCA;
9101
9102 /* Do nothing if called asynchronously. Inserting text into
9103 a buffer may call after-change-functions and alike and
9104 that would means running Lisp asynchronously. */
9105 if (handling_signal)
9106 return;
9107
9108 fmt = msg = Qnil;
9109 GCPRO4 (fmt, msg, arg1, arg2);
9110
9111 args[0] = fmt = build_string (format);
9112 args[1] = arg1;
9113 args[2] = arg2;
9114 msg = Fformat (3, args);
9115
9116 len = SBYTES (msg) + 1;
9117 SAFE_ALLOCA (buffer, char *, len);
9118 memcpy (buffer, SDATA (msg), len);
9119
9120 message_dolog (buffer, len - 1, 1, 0);
9121 SAFE_FREE ();
9122
9123 UNGCPRO;
9124 }
9125
9126
9127 /* Output a newline in the *Messages* buffer if "needs" one. */
9128
9129 void
9130 message_log_maybe_newline (void)
9131 {
9132 if (message_log_need_newline)
9133 message_dolog ("", 0, 1, 0);
9134 }
9135
9136
9137 /* Add a string M of length NBYTES to the message log, optionally
9138 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9139 nonzero, means interpret the contents of M as multibyte. This
9140 function calls low-level routines in order to bypass text property
9141 hooks, etc. which might not be safe to run.
9142
9143 This may GC (insert may run before/after change hooks),
9144 so the buffer M must NOT point to a Lisp string. */
9145
9146 void
9147 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
9148 {
9149 const unsigned char *msg = (const unsigned char *) m;
9150
9151 if (!NILP (Vmemory_full))
9152 return;
9153
9154 if (!NILP (Vmessage_log_max))
9155 {
9156 struct buffer *oldbuf;
9157 Lisp_Object oldpoint, oldbegv, oldzv;
9158 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9159 EMACS_INT point_at_end = 0;
9160 EMACS_INT zv_at_end = 0;
9161 Lisp_Object old_deactivate_mark, tem;
9162 struct gcpro gcpro1;
9163
9164 old_deactivate_mark = Vdeactivate_mark;
9165 oldbuf = current_buffer;
9166 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9167 BVAR (current_buffer, undo_list) = Qt;
9168
9169 oldpoint = message_dolog_marker1;
9170 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9171 oldbegv = message_dolog_marker2;
9172 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9173 oldzv = message_dolog_marker3;
9174 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9175 GCPRO1 (old_deactivate_mark);
9176
9177 if (PT == Z)
9178 point_at_end = 1;
9179 if (ZV == Z)
9180 zv_at_end = 1;
9181
9182 BEGV = BEG;
9183 BEGV_BYTE = BEG_BYTE;
9184 ZV = Z;
9185 ZV_BYTE = Z_BYTE;
9186 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9187
9188 /* Insert the string--maybe converting multibyte to single byte
9189 or vice versa, so that all the text fits the buffer. */
9190 if (multibyte
9191 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9192 {
9193 EMACS_INT i;
9194 int c, char_bytes;
9195 char work[1];
9196
9197 /* Convert a multibyte string to single-byte
9198 for the *Message* buffer. */
9199 for (i = 0; i < nbytes; i += char_bytes)
9200 {
9201 c = string_char_and_length (msg + i, &char_bytes);
9202 work[0] = (ASCII_CHAR_P (c)
9203 ? c
9204 : multibyte_char_to_unibyte (c));
9205 insert_1_both (work, 1, 1, 1, 0, 0);
9206 }
9207 }
9208 else if (! multibyte
9209 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9210 {
9211 EMACS_INT i;
9212 int c, char_bytes;
9213 unsigned char str[MAX_MULTIBYTE_LENGTH];
9214 /* Convert a single-byte string to multibyte
9215 for the *Message* buffer. */
9216 for (i = 0; i < nbytes; i++)
9217 {
9218 c = msg[i];
9219 MAKE_CHAR_MULTIBYTE (c);
9220 char_bytes = CHAR_STRING (c, str);
9221 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9222 }
9223 }
9224 else if (nbytes)
9225 insert_1 (m, nbytes, 1, 0, 0);
9226
9227 if (nlflag)
9228 {
9229 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9230 printmax_t dups;
9231 insert_1 ("\n", 1, 1, 0, 0);
9232
9233 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9234 this_bol = PT;
9235 this_bol_byte = PT_BYTE;
9236
9237 /* See if this line duplicates the previous one.
9238 If so, combine duplicates. */
9239 if (this_bol > BEG)
9240 {
9241 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9242 prev_bol = PT;
9243 prev_bol_byte = PT_BYTE;
9244
9245 dups = message_log_check_duplicate (prev_bol_byte,
9246 this_bol_byte);
9247 if (dups)
9248 {
9249 del_range_both (prev_bol, prev_bol_byte,
9250 this_bol, this_bol_byte, 0);
9251 if (dups > 1)
9252 {
9253 char dupstr[sizeof " [ times]"
9254 + INT_STRLEN_BOUND (printmax_t)];
9255 int duplen;
9256
9257 /* If you change this format, don't forget to also
9258 change message_log_check_duplicate. */
9259 sprintf (dupstr, " [%"pMd" times]", dups);
9260 duplen = strlen (dupstr);
9261 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9262 insert_1 (dupstr, duplen, 1, 0, 1);
9263 }
9264 }
9265 }
9266
9267 /* If we have more than the desired maximum number of lines
9268 in the *Messages* buffer now, delete the oldest ones.
9269 This is safe because we don't have undo in this buffer. */
9270
9271 if (NATNUMP (Vmessage_log_max))
9272 {
9273 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9274 -XFASTINT (Vmessage_log_max) - 1, 0);
9275 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9276 }
9277 }
9278 BEGV = XMARKER (oldbegv)->charpos;
9279 BEGV_BYTE = marker_byte_position (oldbegv);
9280
9281 if (zv_at_end)
9282 {
9283 ZV = Z;
9284 ZV_BYTE = Z_BYTE;
9285 }
9286 else
9287 {
9288 ZV = XMARKER (oldzv)->charpos;
9289 ZV_BYTE = marker_byte_position (oldzv);
9290 }
9291
9292 if (point_at_end)
9293 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9294 else
9295 /* We can't do Fgoto_char (oldpoint) because it will run some
9296 Lisp code. */
9297 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9298 XMARKER (oldpoint)->bytepos);
9299
9300 UNGCPRO;
9301 unchain_marker (XMARKER (oldpoint));
9302 unchain_marker (XMARKER (oldbegv));
9303 unchain_marker (XMARKER (oldzv));
9304
9305 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9306 set_buffer_internal (oldbuf);
9307 if (NILP (tem))
9308 windows_or_buffers_changed = old_windows_or_buffers_changed;
9309 message_log_need_newline = !nlflag;
9310 Vdeactivate_mark = old_deactivate_mark;
9311 }
9312 }
9313
9314
9315 /* We are at the end of the buffer after just having inserted a newline.
9316 (Note: We depend on the fact we won't be crossing the gap.)
9317 Check to see if the most recent message looks a lot like the previous one.
9318 Return 0 if different, 1 if the new one should just replace it, or a
9319 value N > 1 if we should also append " [N times]". */
9320
9321 static intmax_t
9322 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
9323 {
9324 EMACS_INT i;
9325 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
9326 int seen_dots = 0;
9327 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9328 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9329
9330 for (i = 0; i < len; i++)
9331 {
9332 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9333 seen_dots = 1;
9334 if (p1[i] != p2[i])
9335 return seen_dots;
9336 }
9337 p1 += len;
9338 if (*p1 == '\n')
9339 return 2;
9340 if (*p1++ == ' ' && *p1++ == '[')
9341 {
9342 char *pend;
9343 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9344 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9345 return n+1;
9346 }
9347 return 0;
9348 }
9349 \f
9350
9351 /* Display an echo area message M with a specified length of NBYTES
9352 bytes. The string may include null characters. If M is 0, clear
9353 out any existing message, and let the mini-buffer text show
9354 through.
9355
9356 This may GC, so the buffer M must NOT point to a Lisp string. */
9357
9358 void
9359 message2 (const char *m, EMACS_INT nbytes, int multibyte)
9360 {
9361 /* First flush out any partial line written with print. */
9362 message_log_maybe_newline ();
9363 if (m)
9364 message_dolog (m, nbytes, 1, multibyte);
9365 message2_nolog (m, nbytes, multibyte);
9366 }
9367
9368
9369 /* The non-logging counterpart of message2. */
9370
9371 void
9372 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
9373 {
9374 struct frame *sf = SELECTED_FRAME ();
9375 message_enable_multibyte = multibyte;
9376
9377 if (FRAME_INITIAL_P (sf))
9378 {
9379 if (noninteractive_need_newline)
9380 putc ('\n', stderr);
9381 noninteractive_need_newline = 0;
9382 if (m)
9383 fwrite (m, nbytes, 1, stderr);
9384 if (cursor_in_echo_area == 0)
9385 fprintf (stderr, "\n");
9386 fflush (stderr);
9387 }
9388 /* A null message buffer means that the frame hasn't really been
9389 initialized yet. Error messages get reported properly by
9390 cmd_error, so this must be just an informative message; toss it. */
9391 else if (INTERACTIVE
9392 && sf->glyphs_initialized_p
9393 && FRAME_MESSAGE_BUF (sf))
9394 {
9395 Lisp_Object mini_window;
9396 struct frame *f;
9397
9398 /* Get the frame containing the mini-buffer
9399 that the selected frame is using. */
9400 mini_window = FRAME_MINIBUF_WINDOW (sf);
9401 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9402
9403 FRAME_SAMPLE_VISIBILITY (f);
9404 if (FRAME_VISIBLE_P (sf)
9405 && ! FRAME_VISIBLE_P (f))
9406 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9407
9408 if (m)
9409 {
9410 set_message (m, Qnil, nbytes, multibyte);
9411 if (minibuffer_auto_raise)
9412 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9413 }
9414 else
9415 clear_message (1, 1);
9416
9417 do_pending_window_change (0);
9418 echo_area_display (1);
9419 do_pending_window_change (0);
9420 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9421 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9422 }
9423 }
9424
9425
9426 /* Display an echo area message M with a specified length of NBYTES
9427 bytes. The string may include null characters. If M is not a
9428 string, clear out any existing message, and let the mini-buffer
9429 text show through.
9430
9431 This function cancels echoing. */
9432
9433 void
9434 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9435 {
9436 struct gcpro gcpro1;
9437
9438 GCPRO1 (m);
9439 clear_message (1,1);
9440 cancel_echoing ();
9441
9442 /* First flush out any partial line written with print. */
9443 message_log_maybe_newline ();
9444 if (STRINGP (m))
9445 {
9446 char *buffer;
9447 USE_SAFE_ALLOCA;
9448
9449 SAFE_ALLOCA (buffer, char *, nbytes);
9450 memcpy (buffer, SDATA (m), nbytes);
9451 message_dolog (buffer, nbytes, 1, multibyte);
9452 SAFE_FREE ();
9453 }
9454 message3_nolog (m, nbytes, multibyte);
9455
9456 UNGCPRO;
9457 }
9458
9459
9460 /* The non-logging version of message3.
9461 This does not cancel echoing, because it is used for echoing.
9462 Perhaps we need to make a separate function for echoing
9463 and make this cancel echoing. */
9464
9465 void
9466 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
9467 {
9468 struct frame *sf = SELECTED_FRAME ();
9469 message_enable_multibyte = multibyte;
9470
9471 if (FRAME_INITIAL_P (sf))
9472 {
9473 if (noninteractive_need_newline)
9474 putc ('\n', stderr);
9475 noninteractive_need_newline = 0;
9476 if (STRINGP (m))
9477 fwrite (SDATA (m), nbytes, 1, stderr);
9478 if (cursor_in_echo_area == 0)
9479 fprintf (stderr, "\n");
9480 fflush (stderr);
9481 }
9482 /* A null message buffer means that the frame hasn't really been
9483 initialized yet. Error messages get reported properly by
9484 cmd_error, so this must be just an informative message; toss it. */
9485 else if (INTERACTIVE
9486 && sf->glyphs_initialized_p
9487 && FRAME_MESSAGE_BUF (sf))
9488 {
9489 Lisp_Object mini_window;
9490 Lisp_Object frame;
9491 struct frame *f;
9492
9493 /* Get the frame containing the mini-buffer
9494 that the selected frame is using. */
9495 mini_window = FRAME_MINIBUF_WINDOW (sf);
9496 frame = XWINDOW (mini_window)->frame;
9497 f = XFRAME (frame);
9498
9499 FRAME_SAMPLE_VISIBILITY (f);
9500 if (FRAME_VISIBLE_P (sf)
9501 && !FRAME_VISIBLE_P (f))
9502 Fmake_frame_visible (frame);
9503
9504 if (STRINGP (m) && SCHARS (m) > 0)
9505 {
9506 set_message (NULL, m, nbytes, multibyte);
9507 if (minibuffer_auto_raise)
9508 Fraise_frame (frame);
9509 /* Assume we are not echoing.
9510 (If we are, echo_now will override this.) */
9511 echo_message_buffer = Qnil;
9512 }
9513 else
9514 clear_message (1, 1);
9515
9516 do_pending_window_change (0);
9517 echo_area_display (1);
9518 do_pending_window_change (0);
9519 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9520 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9521 }
9522 }
9523
9524
9525 /* Display a null-terminated echo area message M. If M is 0, clear
9526 out any existing message, and let the mini-buffer text show through.
9527
9528 The buffer M must continue to exist until after the echo area gets
9529 cleared or some other message gets displayed there. Do not pass
9530 text that is stored in a Lisp string. Do not pass text in a buffer
9531 that was alloca'd. */
9532
9533 void
9534 message1 (const char *m)
9535 {
9536 message2 (m, (m ? strlen (m) : 0), 0);
9537 }
9538
9539
9540 /* The non-logging counterpart of message1. */
9541
9542 void
9543 message1_nolog (const char *m)
9544 {
9545 message2_nolog (m, (m ? strlen (m) : 0), 0);
9546 }
9547
9548 /* Display a message M which contains a single %s
9549 which gets replaced with STRING. */
9550
9551 void
9552 message_with_string (const char *m, Lisp_Object string, int log)
9553 {
9554 CHECK_STRING (string);
9555
9556 if (noninteractive)
9557 {
9558 if (m)
9559 {
9560 if (noninteractive_need_newline)
9561 putc ('\n', stderr);
9562 noninteractive_need_newline = 0;
9563 fprintf (stderr, m, SDATA (string));
9564 if (!cursor_in_echo_area)
9565 fprintf (stderr, "\n");
9566 fflush (stderr);
9567 }
9568 }
9569 else if (INTERACTIVE)
9570 {
9571 /* The frame whose minibuffer we're going to display the message on.
9572 It may be larger than the selected frame, so we need
9573 to use its buffer, not the selected frame's buffer. */
9574 Lisp_Object mini_window;
9575 struct frame *f, *sf = SELECTED_FRAME ();
9576
9577 /* Get the frame containing the minibuffer
9578 that the selected frame is using. */
9579 mini_window = FRAME_MINIBUF_WINDOW (sf);
9580 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9581
9582 /* A null message buffer means that the frame hasn't really been
9583 initialized yet. Error messages get reported properly by
9584 cmd_error, so this must be just an informative message; toss it. */
9585 if (FRAME_MESSAGE_BUF (f))
9586 {
9587 Lisp_Object args[2], msg;
9588 struct gcpro gcpro1, gcpro2;
9589
9590 args[0] = build_string (m);
9591 args[1] = msg = string;
9592 GCPRO2 (args[0], msg);
9593 gcpro1.nvars = 2;
9594
9595 msg = Fformat (2, args);
9596
9597 if (log)
9598 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9599 else
9600 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9601
9602 UNGCPRO;
9603
9604 /* Print should start at the beginning of the message
9605 buffer next time. */
9606 message_buf_print = 0;
9607 }
9608 }
9609 }
9610
9611
9612 /* Dump an informative message to the minibuf. If M is 0, clear out
9613 any existing message, and let the mini-buffer text show through. */
9614
9615 static void
9616 vmessage (const char *m, va_list ap)
9617 {
9618 if (noninteractive)
9619 {
9620 if (m)
9621 {
9622 if (noninteractive_need_newline)
9623 putc ('\n', stderr);
9624 noninteractive_need_newline = 0;
9625 vfprintf (stderr, m, ap);
9626 if (cursor_in_echo_area == 0)
9627 fprintf (stderr, "\n");
9628 fflush (stderr);
9629 }
9630 }
9631 else if (INTERACTIVE)
9632 {
9633 /* The frame whose mini-buffer we're going to display the message
9634 on. It may be larger than the selected frame, so we need to
9635 use its buffer, not the selected frame's buffer. */
9636 Lisp_Object mini_window;
9637 struct frame *f, *sf = SELECTED_FRAME ();
9638
9639 /* Get the frame containing the mini-buffer
9640 that the selected frame is using. */
9641 mini_window = FRAME_MINIBUF_WINDOW (sf);
9642 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9643
9644 /* A null message buffer means that the frame hasn't really been
9645 initialized yet. Error messages get reported properly by
9646 cmd_error, so this must be just an informative message; toss
9647 it. */
9648 if (FRAME_MESSAGE_BUF (f))
9649 {
9650 if (m)
9651 {
9652 ptrdiff_t len;
9653
9654 len = doprnt (FRAME_MESSAGE_BUF (f),
9655 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9656
9657 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9658 }
9659 else
9660 message1 (0);
9661
9662 /* Print should start at the beginning of the message
9663 buffer next time. */
9664 message_buf_print = 0;
9665 }
9666 }
9667 }
9668
9669 void
9670 message (const char *m, ...)
9671 {
9672 va_list ap;
9673 va_start (ap, m);
9674 vmessage (m, ap);
9675 va_end (ap);
9676 }
9677
9678
9679 #if 0
9680 /* The non-logging version of message. */
9681
9682 void
9683 message_nolog (const char *m, ...)
9684 {
9685 Lisp_Object old_log_max;
9686 va_list ap;
9687 va_start (ap, m);
9688 old_log_max = Vmessage_log_max;
9689 Vmessage_log_max = Qnil;
9690 vmessage (m, ap);
9691 Vmessage_log_max = old_log_max;
9692 va_end (ap);
9693 }
9694 #endif
9695
9696
9697 /* Display the current message in the current mini-buffer. This is
9698 only called from error handlers in process.c, and is not time
9699 critical. */
9700
9701 void
9702 update_echo_area (void)
9703 {
9704 if (!NILP (echo_area_buffer[0]))
9705 {
9706 Lisp_Object string;
9707 string = Fcurrent_message ();
9708 message3 (string, SBYTES (string),
9709 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9710 }
9711 }
9712
9713
9714 /* Make sure echo area buffers in `echo_buffers' are live.
9715 If they aren't, make new ones. */
9716
9717 static void
9718 ensure_echo_area_buffers (void)
9719 {
9720 int i;
9721
9722 for (i = 0; i < 2; ++i)
9723 if (!BUFFERP (echo_buffer[i])
9724 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9725 {
9726 char name[30];
9727 Lisp_Object old_buffer;
9728 int j;
9729
9730 old_buffer = echo_buffer[i];
9731 sprintf (name, " *Echo Area %d*", i);
9732 echo_buffer[i] = Fget_buffer_create (build_string (name));
9733 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9734 /* to force word wrap in echo area -
9735 it was decided to postpone this*/
9736 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9737
9738 for (j = 0; j < 2; ++j)
9739 if (EQ (old_buffer, echo_area_buffer[j]))
9740 echo_area_buffer[j] = echo_buffer[i];
9741 }
9742 }
9743
9744
9745 /* Call FN with args A1..A4 with either the current or last displayed
9746 echo_area_buffer as current buffer.
9747
9748 WHICH zero means use the current message buffer
9749 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9750 from echo_buffer[] and clear it.
9751
9752 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9753 suitable buffer from echo_buffer[] and clear it.
9754
9755 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9756 that the current message becomes the last displayed one, make
9757 choose a suitable buffer for echo_area_buffer[0], and clear it.
9758
9759 Value is what FN returns. */
9760
9761 static int
9762 with_echo_area_buffer (struct window *w, int which,
9763 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9764 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9765 {
9766 Lisp_Object buffer;
9767 int this_one, the_other, clear_buffer_p, rc;
9768 int count = SPECPDL_INDEX ();
9769
9770 /* If buffers aren't live, make new ones. */
9771 ensure_echo_area_buffers ();
9772
9773 clear_buffer_p = 0;
9774
9775 if (which == 0)
9776 this_one = 0, the_other = 1;
9777 else if (which > 0)
9778 this_one = 1, the_other = 0;
9779 else
9780 {
9781 this_one = 0, the_other = 1;
9782 clear_buffer_p = 1;
9783
9784 /* We need a fresh one in case the current echo buffer equals
9785 the one containing the last displayed echo area message. */
9786 if (!NILP (echo_area_buffer[this_one])
9787 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9788 echo_area_buffer[this_one] = Qnil;
9789 }
9790
9791 /* Choose a suitable buffer from echo_buffer[] is we don't
9792 have one. */
9793 if (NILP (echo_area_buffer[this_one]))
9794 {
9795 echo_area_buffer[this_one]
9796 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9797 ? echo_buffer[the_other]
9798 : echo_buffer[this_one]);
9799 clear_buffer_p = 1;
9800 }
9801
9802 buffer = echo_area_buffer[this_one];
9803
9804 /* Don't get confused by reusing the buffer used for echoing
9805 for a different purpose. */
9806 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9807 cancel_echoing ();
9808
9809 record_unwind_protect (unwind_with_echo_area_buffer,
9810 with_echo_area_buffer_unwind_data (w));
9811
9812 /* Make the echo area buffer current. Note that for display
9813 purposes, it is not necessary that the displayed window's buffer
9814 == current_buffer, except for text property lookup. So, let's
9815 only set that buffer temporarily here without doing a full
9816 Fset_window_buffer. We must also change w->pointm, though,
9817 because otherwise an assertions in unshow_buffer fails, and Emacs
9818 aborts. */
9819 set_buffer_internal_1 (XBUFFER (buffer));
9820 if (w)
9821 {
9822 w->buffer = buffer;
9823 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9824 }
9825
9826 BVAR (current_buffer, undo_list) = Qt;
9827 BVAR (current_buffer, read_only) = Qnil;
9828 specbind (Qinhibit_read_only, Qt);
9829 specbind (Qinhibit_modification_hooks, Qt);
9830
9831 if (clear_buffer_p && Z > BEG)
9832 del_range (BEG, Z);
9833
9834 xassert (BEGV >= BEG);
9835 xassert (ZV <= Z && ZV >= BEGV);
9836
9837 rc = fn (a1, a2, a3, a4);
9838
9839 xassert (BEGV >= BEG);
9840 xassert (ZV <= Z && ZV >= BEGV);
9841
9842 unbind_to (count, Qnil);
9843 return rc;
9844 }
9845
9846
9847 /* Save state that should be preserved around the call to the function
9848 FN called in with_echo_area_buffer. */
9849
9850 static Lisp_Object
9851 with_echo_area_buffer_unwind_data (struct window *w)
9852 {
9853 int i = 0;
9854 Lisp_Object vector, tmp;
9855
9856 /* Reduce consing by keeping one vector in
9857 Vwith_echo_area_save_vector. */
9858 vector = Vwith_echo_area_save_vector;
9859 Vwith_echo_area_save_vector = Qnil;
9860
9861 if (NILP (vector))
9862 vector = Fmake_vector (make_number (7), Qnil);
9863
9864 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9865 ASET (vector, i, Vdeactivate_mark); ++i;
9866 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9867
9868 if (w)
9869 {
9870 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9871 ASET (vector, i, w->buffer); ++i;
9872 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9873 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9874 }
9875 else
9876 {
9877 int end = i + 4;
9878 for (; i < end; ++i)
9879 ASET (vector, i, Qnil);
9880 }
9881
9882 xassert (i == ASIZE (vector));
9883 return vector;
9884 }
9885
9886
9887 /* Restore global state from VECTOR which was created by
9888 with_echo_area_buffer_unwind_data. */
9889
9890 static Lisp_Object
9891 unwind_with_echo_area_buffer (Lisp_Object vector)
9892 {
9893 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9894 Vdeactivate_mark = AREF (vector, 1);
9895 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9896
9897 if (WINDOWP (AREF (vector, 3)))
9898 {
9899 struct window *w;
9900 Lisp_Object buffer, charpos, bytepos;
9901
9902 w = XWINDOW (AREF (vector, 3));
9903 buffer = AREF (vector, 4);
9904 charpos = AREF (vector, 5);
9905 bytepos = AREF (vector, 6);
9906
9907 w->buffer = buffer;
9908 set_marker_both (w->pointm, buffer,
9909 XFASTINT (charpos), XFASTINT (bytepos));
9910 }
9911
9912 Vwith_echo_area_save_vector = vector;
9913 return Qnil;
9914 }
9915
9916
9917 /* Set up the echo area for use by print functions. MULTIBYTE_P
9918 non-zero means we will print multibyte. */
9919
9920 void
9921 setup_echo_area_for_printing (int multibyte_p)
9922 {
9923 /* If we can't find an echo area any more, exit. */
9924 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9925 Fkill_emacs (Qnil);
9926
9927 ensure_echo_area_buffers ();
9928
9929 if (!message_buf_print)
9930 {
9931 /* A message has been output since the last time we printed.
9932 Choose a fresh echo area buffer. */
9933 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9934 echo_area_buffer[0] = echo_buffer[1];
9935 else
9936 echo_area_buffer[0] = echo_buffer[0];
9937
9938 /* Switch to that buffer and clear it. */
9939 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9940 BVAR (current_buffer, truncate_lines) = Qnil;
9941
9942 if (Z > BEG)
9943 {
9944 int count = SPECPDL_INDEX ();
9945 specbind (Qinhibit_read_only, Qt);
9946 /* Note that undo recording is always disabled. */
9947 del_range (BEG, Z);
9948 unbind_to (count, Qnil);
9949 }
9950 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9951
9952 /* Set up the buffer for the multibyteness we need. */
9953 if (multibyte_p
9954 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9955 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9956
9957 /* Raise the frame containing the echo area. */
9958 if (minibuffer_auto_raise)
9959 {
9960 struct frame *sf = SELECTED_FRAME ();
9961 Lisp_Object mini_window;
9962 mini_window = FRAME_MINIBUF_WINDOW (sf);
9963 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9964 }
9965
9966 message_log_maybe_newline ();
9967 message_buf_print = 1;
9968 }
9969 else
9970 {
9971 if (NILP (echo_area_buffer[0]))
9972 {
9973 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9974 echo_area_buffer[0] = echo_buffer[1];
9975 else
9976 echo_area_buffer[0] = echo_buffer[0];
9977 }
9978
9979 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9980 {
9981 /* Someone switched buffers between print requests. */
9982 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9983 BVAR (current_buffer, truncate_lines) = Qnil;
9984 }
9985 }
9986 }
9987
9988
9989 /* Display an echo area message in window W. Value is non-zero if W's
9990 height is changed. If display_last_displayed_message_p is
9991 non-zero, display the message that was last displayed, otherwise
9992 display the current message. */
9993
9994 static int
9995 display_echo_area (struct window *w)
9996 {
9997 int i, no_message_p, window_height_changed_p, count;
9998
9999 /* Temporarily disable garbage collections while displaying the echo
10000 area. This is done because a GC can print a message itself.
10001 That message would modify the echo area buffer's contents while a
10002 redisplay of the buffer is going on, and seriously confuse
10003 redisplay. */
10004 count = inhibit_garbage_collection ();
10005
10006 /* If there is no message, we must call display_echo_area_1
10007 nevertheless because it resizes the window. But we will have to
10008 reset the echo_area_buffer in question to nil at the end because
10009 with_echo_area_buffer will sets it to an empty buffer. */
10010 i = display_last_displayed_message_p ? 1 : 0;
10011 no_message_p = NILP (echo_area_buffer[i]);
10012
10013 window_height_changed_p
10014 = with_echo_area_buffer (w, display_last_displayed_message_p,
10015 display_echo_area_1,
10016 (intptr_t) w, Qnil, 0, 0);
10017
10018 if (no_message_p)
10019 echo_area_buffer[i] = Qnil;
10020
10021 unbind_to (count, Qnil);
10022 return window_height_changed_p;
10023 }
10024
10025
10026 /* Helper for display_echo_area. Display the current buffer which
10027 contains the current echo area message in window W, a mini-window,
10028 a pointer to which is passed in A1. A2..A4 are currently not used.
10029 Change the height of W so that all of the message is displayed.
10030 Value is non-zero if height of W was changed. */
10031
10032 static int
10033 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10034 {
10035 intptr_t i1 = a1;
10036 struct window *w = (struct window *) i1;
10037 Lisp_Object window;
10038 struct text_pos start;
10039 int window_height_changed_p = 0;
10040
10041 /* Do this before displaying, so that we have a large enough glyph
10042 matrix for the display. If we can't get enough space for the
10043 whole text, display the last N lines. That works by setting w->start. */
10044 window_height_changed_p = resize_mini_window (w, 0);
10045
10046 /* Use the starting position chosen by resize_mini_window. */
10047 SET_TEXT_POS_FROM_MARKER (start, w->start);
10048
10049 /* Display. */
10050 clear_glyph_matrix (w->desired_matrix);
10051 XSETWINDOW (window, w);
10052 try_window (window, start, 0);
10053
10054 return window_height_changed_p;
10055 }
10056
10057
10058 /* Resize the echo area window to exactly the size needed for the
10059 currently displayed message, if there is one. If a mini-buffer
10060 is active, don't shrink it. */
10061
10062 void
10063 resize_echo_area_exactly (void)
10064 {
10065 if (BUFFERP (echo_area_buffer[0])
10066 && WINDOWP (echo_area_window))
10067 {
10068 struct window *w = XWINDOW (echo_area_window);
10069 int resized_p;
10070 Lisp_Object resize_exactly;
10071
10072 if (minibuf_level == 0)
10073 resize_exactly = Qt;
10074 else
10075 resize_exactly = Qnil;
10076
10077 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10078 (intptr_t) w, resize_exactly,
10079 0, 0);
10080 if (resized_p)
10081 {
10082 ++windows_or_buffers_changed;
10083 ++update_mode_lines;
10084 redisplay_internal ();
10085 }
10086 }
10087 }
10088
10089
10090 /* Callback function for with_echo_area_buffer, when used from
10091 resize_echo_area_exactly. A1 contains a pointer to the window to
10092 resize, EXACTLY non-nil means resize the mini-window exactly to the
10093 size of the text displayed. A3 and A4 are not used. Value is what
10094 resize_mini_window returns. */
10095
10096 static int
10097 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
10098 {
10099 intptr_t i1 = a1;
10100 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10101 }
10102
10103
10104 /* Resize mini-window W to fit the size of its contents. EXACT_P
10105 means size the window exactly to the size needed. Otherwise, it's
10106 only enlarged until W's buffer is empty.
10107
10108 Set W->start to the right place to begin display. If the whole
10109 contents fit, start at the beginning. Otherwise, start so as
10110 to make the end of the contents appear. This is particularly
10111 important for y-or-n-p, but seems desirable generally.
10112
10113 Value is non-zero if the window height has been changed. */
10114
10115 int
10116 resize_mini_window (struct window *w, int exact_p)
10117 {
10118 struct frame *f = XFRAME (w->frame);
10119 int window_height_changed_p = 0;
10120
10121 xassert (MINI_WINDOW_P (w));
10122
10123 /* By default, start display at the beginning. */
10124 set_marker_both (w->start, w->buffer,
10125 BUF_BEGV (XBUFFER (w->buffer)),
10126 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10127
10128 /* Don't resize windows while redisplaying a window; it would
10129 confuse redisplay functions when the size of the window they are
10130 displaying changes from under them. Such a resizing can happen,
10131 for instance, when which-func prints a long message while
10132 we are running fontification-functions. We're running these
10133 functions with safe_call which binds inhibit-redisplay to t. */
10134 if (!NILP (Vinhibit_redisplay))
10135 return 0;
10136
10137 /* Nil means don't try to resize. */
10138 if (NILP (Vresize_mini_windows)
10139 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10140 return 0;
10141
10142 if (!FRAME_MINIBUF_ONLY_P (f))
10143 {
10144 struct it it;
10145 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10146 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10147 int height, max_height;
10148 int unit = FRAME_LINE_HEIGHT (f);
10149 struct text_pos start;
10150 struct buffer *old_current_buffer = NULL;
10151
10152 if (current_buffer != XBUFFER (w->buffer))
10153 {
10154 old_current_buffer = current_buffer;
10155 set_buffer_internal (XBUFFER (w->buffer));
10156 }
10157
10158 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10159
10160 /* Compute the max. number of lines specified by the user. */
10161 if (FLOATP (Vmax_mini_window_height))
10162 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10163 else if (INTEGERP (Vmax_mini_window_height))
10164 max_height = XINT (Vmax_mini_window_height);
10165 else
10166 max_height = total_height / 4;
10167
10168 /* Correct that max. height if it's bogus. */
10169 max_height = max (1, max_height);
10170 max_height = min (total_height, max_height);
10171
10172 /* Find out the height of the text in the window. */
10173 if (it.line_wrap == TRUNCATE)
10174 height = 1;
10175 else
10176 {
10177 last_height = 0;
10178 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10179 if (it.max_ascent == 0 && it.max_descent == 0)
10180 height = it.current_y + last_height;
10181 else
10182 height = it.current_y + it.max_ascent + it.max_descent;
10183 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10184 height = (height + unit - 1) / unit;
10185 }
10186
10187 /* Compute a suitable window start. */
10188 if (height > max_height)
10189 {
10190 height = max_height;
10191 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10192 move_it_vertically_backward (&it, (height - 1) * unit);
10193 start = it.current.pos;
10194 }
10195 else
10196 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10197 SET_MARKER_FROM_TEXT_POS (w->start, start);
10198
10199 if (EQ (Vresize_mini_windows, Qgrow_only))
10200 {
10201 /* Let it grow only, until we display an empty message, in which
10202 case the window shrinks again. */
10203 if (height > WINDOW_TOTAL_LINES (w))
10204 {
10205 int old_height = WINDOW_TOTAL_LINES (w);
10206 freeze_window_starts (f, 1);
10207 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10208 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10209 }
10210 else if (height < WINDOW_TOTAL_LINES (w)
10211 && (exact_p || BEGV == ZV))
10212 {
10213 int old_height = WINDOW_TOTAL_LINES (w);
10214 freeze_window_starts (f, 0);
10215 shrink_mini_window (w);
10216 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10217 }
10218 }
10219 else
10220 {
10221 /* Always resize to exact size needed. */
10222 if (height > WINDOW_TOTAL_LINES (w))
10223 {
10224 int old_height = WINDOW_TOTAL_LINES (w);
10225 freeze_window_starts (f, 1);
10226 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10227 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10228 }
10229 else if (height < WINDOW_TOTAL_LINES (w))
10230 {
10231 int old_height = WINDOW_TOTAL_LINES (w);
10232 freeze_window_starts (f, 0);
10233 shrink_mini_window (w);
10234
10235 if (height)
10236 {
10237 freeze_window_starts (f, 1);
10238 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10239 }
10240
10241 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10242 }
10243 }
10244
10245 if (old_current_buffer)
10246 set_buffer_internal (old_current_buffer);
10247 }
10248
10249 return window_height_changed_p;
10250 }
10251
10252
10253 /* Value is the current message, a string, or nil if there is no
10254 current message. */
10255
10256 Lisp_Object
10257 current_message (void)
10258 {
10259 Lisp_Object msg;
10260
10261 if (!BUFFERP (echo_area_buffer[0]))
10262 msg = Qnil;
10263 else
10264 {
10265 with_echo_area_buffer (0, 0, current_message_1,
10266 (intptr_t) &msg, Qnil, 0, 0);
10267 if (NILP (msg))
10268 echo_area_buffer[0] = Qnil;
10269 }
10270
10271 return msg;
10272 }
10273
10274
10275 static int
10276 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10277 {
10278 intptr_t i1 = a1;
10279 Lisp_Object *msg = (Lisp_Object *) i1;
10280
10281 if (Z > BEG)
10282 *msg = make_buffer_string (BEG, Z, 1);
10283 else
10284 *msg = Qnil;
10285 return 0;
10286 }
10287
10288
10289 /* Push the current message on Vmessage_stack for later restoration
10290 by restore_message. Value is non-zero if the current message isn't
10291 empty. This is a relatively infrequent operation, so it's not
10292 worth optimizing. */
10293
10294 int
10295 push_message (void)
10296 {
10297 Lisp_Object msg;
10298 msg = current_message ();
10299 Vmessage_stack = Fcons (msg, Vmessage_stack);
10300 return STRINGP (msg);
10301 }
10302
10303
10304 /* Restore message display from the top of Vmessage_stack. */
10305
10306 void
10307 restore_message (void)
10308 {
10309 Lisp_Object msg;
10310
10311 xassert (CONSP (Vmessage_stack));
10312 msg = XCAR (Vmessage_stack);
10313 if (STRINGP (msg))
10314 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10315 else
10316 message3_nolog (msg, 0, 0);
10317 }
10318
10319
10320 /* Handler for record_unwind_protect calling pop_message. */
10321
10322 Lisp_Object
10323 pop_message_unwind (Lisp_Object dummy)
10324 {
10325 pop_message ();
10326 return Qnil;
10327 }
10328
10329 /* Pop the top-most entry off Vmessage_stack. */
10330
10331 static void
10332 pop_message (void)
10333 {
10334 xassert (CONSP (Vmessage_stack));
10335 Vmessage_stack = XCDR (Vmessage_stack);
10336 }
10337
10338
10339 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10340 exits. If the stack is not empty, we have a missing pop_message
10341 somewhere. */
10342
10343 void
10344 check_message_stack (void)
10345 {
10346 if (!NILP (Vmessage_stack))
10347 abort ();
10348 }
10349
10350
10351 /* Truncate to NCHARS what will be displayed in the echo area the next
10352 time we display it---but don't redisplay it now. */
10353
10354 void
10355 truncate_echo_area (EMACS_INT nchars)
10356 {
10357 if (nchars == 0)
10358 echo_area_buffer[0] = Qnil;
10359 /* A null message buffer means that the frame hasn't really been
10360 initialized yet. Error messages get reported properly by
10361 cmd_error, so this must be just an informative message; toss it. */
10362 else if (!noninteractive
10363 && INTERACTIVE
10364 && !NILP (echo_area_buffer[0]))
10365 {
10366 struct frame *sf = SELECTED_FRAME ();
10367 if (FRAME_MESSAGE_BUF (sf))
10368 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10369 }
10370 }
10371
10372
10373 /* Helper function for truncate_echo_area. Truncate the current
10374 message to at most NCHARS characters. */
10375
10376 static int
10377 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
10378 {
10379 if (BEG + nchars < Z)
10380 del_range (BEG + nchars, Z);
10381 if (Z == BEG)
10382 echo_area_buffer[0] = Qnil;
10383 return 0;
10384 }
10385
10386
10387 /* Set the current message to a substring of S or STRING.
10388
10389 If STRING is a Lisp string, set the message to the first NBYTES
10390 bytes from STRING. NBYTES zero means use the whole string. If
10391 STRING is multibyte, the message will be displayed multibyte.
10392
10393 If S is not null, set the message to the first LEN bytes of S. LEN
10394 zero means use the whole string. MULTIBYTE_P non-zero means S is
10395 multibyte. Display the message multibyte in that case.
10396
10397 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10398 to t before calling set_message_1 (which calls insert).
10399 */
10400
10401 static void
10402 set_message (const char *s, Lisp_Object string,
10403 EMACS_INT nbytes, int multibyte_p)
10404 {
10405 message_enable_multibyte
10406 = ((s && multibyte_p)
10407 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10408
10409 with_echo_area_buffer (0, -1, set_message_1,
10410 (intptr_t) s, string, nbytes, multibyte_p);
10411 message_buf_print = 0;
10412 help_echo_showing_p = 0;
10413 }
10414
10415
10416 /* Helper function for set_message. Arguments have the same meaning
10417 as there, with A1 corresponding to S and A2 corresponding to STRING
10418 This function is called with the echo area buffer being
10419 current. */
10420
10421 static int
10422 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
10423 {
10424 intptr_t i1 = a1;
10425 const char *s = (const char *) i1;
10426 const unsigned char *msg = (const unsigned char *) s;
10427 Lisp_Object string = a2;
10428
10429 /* Change multibyteness of the echo buffer appropriately. */
10430 if (message_enable_multibyte
10431 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10432 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10433
10434 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10435 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10436 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10437
10438 /* Insert new message at BEG. */
10439 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10440
10441 if (STRINGP (string))
10442 {
10443 EMACS_INT nchars;
10444
10445 if (nbytes == 0)
10446 nbytes = SBYTES (string);
10447 nchars = string_byte_to_char (string, nbytes);
10448
10449 /* This function takes care of single/multibyte conversion. We
10450 just have to ensure that the echo area buffer has the right
10451 setting of enable_multibyte_characters. */
10452 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10453 }
10454 else if (s)
10455 {
10456 if (nbytes == 0)
10457 nbytes = strlen (s);
10458
10459 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10460 {
10461 /* Convert from multi-byte to single-byte. */
10462 EMACS_INT i;
10463 int c, n;
10464 char work[1];
10465
10466 /* Convert a multibyte string to single-byte. */
10467 for (i = 0; i < nbytes; i += n)
10468 {
10469 c = string_char_and_length (msg + i, &n);
10470 work[0] = (ASCII_CHAR_P (c)
10471 ? c
10472 : multibyte_char_to_unibyte (c));
10473 insert_1_both (work, 1, 1, 1, 0, 0);
10474 }
10475 }
10476 else if (!multibyte_p
10477 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10478 {
10479 /* Convert from single-byte to multi-byte. */
10480 EMACS_INT i;
10481 int c, n;
10482 unsigned char str[MAX_MULTIBYTE_LENGTH];
10483
10484 /* Convert a single-byte string to multibyte. */
10485 for (i = 0; i < nbytes; i++)
10486 {
10487 c = msg[i];
10488 MAKE_CHAR_MULTIBYTE (c);
10489 n = CHAR_STRING (c, str);
10490 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10491 }
10492 }
10493 else
10494 insert_1 (s, nbytes, 1, 0, 0);
10495 }
10496
10497 return 0;
10498 }
10499
10500
10501 /* Clear messages. CURRENT_P non-zero means clear the current
10502 message. LAST_DISPLAYED_P non-zero means clear the message
10503 last displayed. */
10504
10505 void
10506 clear_message (int current_p, int last_displayed_p)
10507 {
10508 if (current_p)
10509 {
10510 echo_area_buffer[0] = Qnil;
10511 message_cleared_p = 1;
10512 }
10513
10514 if (last_displayed_p)
10515 echo_area_buffer[1] = Qnil;
10516
10517 message_buf_print = 0;
10518 }
10519
10520 /* Clear garbaged frames.
10521
10522 This function is used where the old redisplay called
10523 redraw_garbaged_frames which in turn called redraw_frame which in
10524 turn called clear_frame. The call to clear_frame was a source of
10525 flickering. I believe a clear_frame is not necessary. It should
10526 suffice in the new redisplay to invalidate all current matrices,
10527 and ensure a complete redisplay of all windows. */
10528
10529 static void
10530 clear_garbaged_frames (void)
10531 {
10532 if (frame_garbaged)
10533 {
10534 Lisp_Object tail, frame;
10535 int changed_count = 0;
10536
10537 FOR_EACH_FRAME (tail, frame)
10538 {
10539 struct frame *f = XFRAME (frame);
10540
10541 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10542 {
10543 if (f->resized_p)
10544 {
10545 Fredraw_frame (frame);
10546 f->force_flush_display_p = 1;
10547 }
10548 clear_current_matrices (f);
10549 changed_count++;
10550 f->garbaged = 0;
10551 f->resized_p = 0;
10552 }
10553 }
10554
10555 frame_garbaged = 0;
10556 if (changed_count)
10557 ++windows_or_buffers_changed;
10558 }
10559 }
10560
10561
10562 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10563 is non-zero update selected_frame. Value is non-zero if the
10564 mini-windows height has been changed. */
10565
10566 static int
10567 echo_area_display (int update_frame_p)
10568 {
10569 Lisp_Object mini_window;
10570 struct window *w;
10571 struct frame *f;
10572 int window_height_changed_p = 0;
10573 struct frame *sf = SELECTED_FRAME ();
10574
10575 mini_window = FRAME_MINIBUF_WINDOW (sf);
10576 w = XWINDOW (mini_window);
10577 f = XFRAME (WINDOW_FRAME (w));
10578
10579 /* Don't display if frame is invisible or not yet initialized. */
10580 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10581 return 0;
10582
10583 #ifdef HAVE_WINDOW_SYSTEM
10584 /* When Emacs starts, selected_frame may be the initial terminal
10585 frame. If we let this through, a message would be displayed on
10586 the terminal. */
10587 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10588 return 0;
10589 #endif /* HAVE_WINDOW_SYSTEM */
10590
10591 /* Redraw garbaged frames. */
10592 if (frame_garbaged)
10593 clear_garbaged_frames ();
10594
10595 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10596 {
10597 echo_area_window = mini_window;
10598 window_height_changed_p = display_echo_area (w);
10599 w->must_be_updated_p = 1;
10600
10601 /* Update the display, unless called from redisplay_internal.
10602 Also don't update the screen during redisplay itself. The
10603 update will happen at the end of redisplay, and an update
10604 here could cause confusion. */
10605 if (update_frame_p && !redisplaying_p)
10606 {
10607 int n = 0;
10608
10609 /* If the display update has been interrupted by pending
10610 input, update mode lines in the frame. Due to the
10611 pending input, it might have been that redisplay hasn't
10612 been called, so that mode lines above the echo area are
10613 garbaged. This looks odd, so we prevent it here. */
10614 if (!display_completed)
10615 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10616
10617 if (window_height_changed_p
10618 /* Don't do this if Emacs is shutting down. Redisplay
10619 needs to run hooks. */
10620 && !NILP (Vrun_hooks))
10621 {
10622 /* Must update other windows. Likewise as in other
10623 cases, don't let this update be interrupted by
10624 pending input. */
10625 int count = SPECPDL_INDEX ();
10626 specbind (Qredisplay_dont_pause, Qt);
10627 windows_or_buffers_changed = 1;
10628 redisplay_internal ();
10629 unbind_to (count, Qnil);
10630 }
10631 else if (FRAME_WINDOW_P (f) && n == 0)
10632 {
10633 /* Window configuration is the same as before.
10634 Can do with a display update of the echo area,
10635 unless we displayed some mode lines. */
10636 update_single_window (w, 1);
10637 FRAME_RIF (f)->flush_display (f);
10638 }
10639 else
10640 update_frame (f, 1, 1);
10641
10642 /* If cursor is in the echo area, make sure that the next
10643 redisplay displays the minibuffer, so that the cursor will
10644 be replaced with what the minibuffer wants. */
10645 if (cursor_in_echo_area)
10646 ++windows_or_buffers_changed;
10647 }
10648 }
10649 else if (!EQ (mini_window, selected_window))
10650 windows_or_buffers_changed++;
10651
10652 /* Last displayed message is now the current message. */
10653 echo_area_buffer[1] = echo_area_buffer[0];
10654 /* Inform read_char that we're not echoing. */
10655 echo_message_buffer = Qnil;
10656
10657 /* Prevent redisplay optimization in redisplay_internal by resetting
10658 this_line_start_pos. This is done because the mini-buffer now
10659 displays the message instead of its buffer text. */
10660 if (EQ (mini_window, selected_window))
10661 CHARPOS (this_line_start_pos) = 0;
10662
10663 return window_height_changed_p;
10664 }
10665
10666
10667 \f
10668 /***********************************************************************
10669 Mode Lines and Frame Titles
10670 ***********************************************************************/
10671
10672 /* A buffer for constructing non-propertized mode-line strings and
10673 frame titles in it; allocated from the heap in init_xdisp and
10674 resized as needed in store_mode_line_noprop_char. */
10675
10676 static char *mode_line_noprop_buf;
10677
10678 /* The buffer's end, and a current output position in it. */
10679
10680 static char *mode_line_noprop_buf_end;
10681 static char *mode_line_noprop_ptr;
10682
10683 #define MODE_LINE_NOPROP_LEN(start) \
10684 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10685
10686 static enum {
10687 MODE_LINE_DISPLAY = 0,
10688 MODE_LINE_TITLE,
10689 MODE_LINE_NOPROP,
10690 MODE_LINE_STRING
10691 } mode_line_target;
10692
10693 /* Alist that caches the results of :propertize.
10694 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10695 static Lisp_Object mode_line_proptrans_alist;
10696
10697 /* List of strings making up the mode-line. */
10698 static Lisp_Object mode_line_string_list;
10699
10700 /* Base face property when building propertized mode line string. */
10701 static Lisp_Object mode_line_string_face;
10702 static Lisp_Object mode_line_string_face_prop;
10703
10704
10705 /* Unwind data for mode line strings */
10706
10707 static Lisp_Object Vmode_line_unwind_vector;
10708
10709 static Lisp_Object
10710 format_mode_line_unwind_data (struct buffer *obuf,
10711 Lisp_Object owin,
10712 int save_proptrans)
10713 {
10714 Lisp_Object vector, tmp;
10715
10716 /* Reduce consing by keeping one vector in
10717 Vwith_echo_area_save_vector. */
10718 vector = Vmode_line_unwind_vector;
10719 Vmode_line_unwind_vector = Qnil;
10720
10721 if (NILP (vector))
10722 vector = Fmake_vector (make_number (8), Qnil);
10723
10724 ASET (vector, 0, make_number (mode_line_target));
10725 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10726 ASET (vector, 2, mode_line_string_list);
10727 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10728 ASET (vector, 4, mode_line_string_face);
10729 ASET (vector, 5, mode_line_string_face_prop);
10730
10731 if (obuf)
10732 XSETBUFFER (tmp, obuf);
10733 else
10734 tmp = Qnil;
10735 ASET (vector, 6, tmp);
10736 ASET (vector, 7, owin);
10737
10738 return vector;
10739 }
10740
10741 static Lisp_Object
10742 unwind_format_mode_line (Lisp_Object vector)
10743 {
10744 mode_line_target = XINT (AREF (vector, 0));
10745 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10746 mode_line_string_list = AREF (vector, 2);
10747 if (! EQ (AREF (vector, 3), Qt))
10748 mode_line_proptrans_alist = AREF (vector, 3);
10749 mode_line_string_face = AREF (vector, 4);
10750 mode_line_string_face_prop = AREF (vector, 5);
10751
10752 if (!NILP (AREF (vector, 7)))
10753 /* Select window before buffer, since it may change the buffer. */
10754 Fselect_window (AREF (vector, 7), Qt);
10755
10756 if (!NILP (AREF (vector, 6)))
10757 {
10758 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10759 ASET (vector, 6, Qnil);
10760 }
10761
10762 Vmode_line_unwind_vector = vector;
10763 return Qnil;
10764 }
10765
10766
10767 /* Store a single character C for the frame title in mode_line_noprop_buf.
10768 Re-allocate mode_line_noprop_buf if necessary. */
10769
10770 static void
10771 store_mode_line_noprop_char (char c)
10772 {
10773 /* If output position has reached the end of the allocated buffer,
10774 increase the buffer's size. */
10775 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10776 {
10777 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10778 ptrdiff_t size = len;
10779 mode_line_noprop_buf =
10780 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10781 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10782 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10783 }
10784
10785 *mode_line_noprop_ptr++ = c;
10786 }
10787
10788
10789 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10790 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10791 characters that yield more columns than PRECISION; PRECISION <= 0
10792 means copy the whole string. Pad with spaces until FIELD_WIDTH
10793 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10794 pad. Called from display_mode_element when it is used to build a
10795 frame title. */
10796
10797 static int
10798 store_mode_line_noprop (const char *string, int field_width, int precision)
10799 {
10800 const unsigned char *str = (const unsigned char *) string;
10801 int n = 0;
10802 EMACS_INT dummy, nbytes;
10803
10804 /* Copy at most PRECISION chars from STR. */
10805 nbytes = strlen (string);
10806 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10807 while (nbytes--)
10808 store_mode_line_noprop_char (*str++);
10809
10810 /* Fill up with spaces until FIELD_WIDTH reached. */
10811 while (field_width > 0
10812 && n < field_width)
10813 {
10814 store_mode_line_noprop_char (' ');
10815 ++n;
10816 }
10817
10818 return n;
10819 }
10820
10821 /***********************************************************************
10822 Frame Titles
10823 ***********************************************************************/
10824
10825 #ifdef HAVE_WINDOW_SYSTEM
10826
10827 /* Set the title of FRAME, if it has changed. The title format is
10828 Vicon_title_format if FRAME is iconified, otherwise it is
10829 frame_title_format. */
10830
10831 static void
10832 x_consider_frame_title (Lisp_Object frame)
10833 {
10834 struct frame *f = XFRAME (frame);
10835
10836 if (FRAME_WINDOW_P (f)
10837 || FRAME_MINIBUF_ONLY_P (f)
10838 || f->explicit_name)
10839 {
10840 /* Do we have more than one visible frame on this X display? */
10841 Lisp_Object tail;
10842 Lisp_Object fmt;
10843 ptrdiff_t title_start;
10844 char *title;
10845 ptrdiff_t len;
10846 struct it it;
10847 int count = SPECPDL_INDEX ();
10848
10849 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10850 {
10851 Lisp_Object other_frame = XCAR (tail);
10852 struct frame *tf = XFRAME (other_frame);
10853
10854 if (tf != f
10855 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10856 && !FRAME_MINIBUF_ONLY_P (tf)
10857 && !EQ (other_frame, tip_frame)
10858 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10859 break;
10860 }
10861
10862 /* Set global variable indicating that multiple frames exist. */
10863 multiple_frames = CONSP (tail);
10864
10865 /* Switch to the buffer of selected window of the frame. Set up
10866 mode_line_target so that display_mode_element will output into
10867 mode_line_noprop_buf; then display the title. */
10868 record_unwind_protect (unwind_format_mode_line,
10869 format_mode_line_unwind_data
10870 (current_buffer, selected_window, 0));
10871
10872 Fselect_window (f->selected_window, Qt);
10873 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10874 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10875
10876 mode_line_target = MODE_LINE_TITLE;
10877 title_start = MODE_LINE_NOPROP_LEN (0);
10878 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10879 NULL, DEFAULT_FACE_ID);
10880 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10881 len = MODE_LINE_NOPROP_LEN (title_start);
10882 title = mode_line_noprop_buf + title_start;
10883 unbind_to (count, Qnil);
10884
10885 /* Set the title only if it's changed. This avoids consing in
10886 the common case where it hasn't. (If it turns out that we've
10887 already wasted too much time by walking through the list with
10888 display_mode_element, then we might need to optimize at a
10889 higher level than this.) */
10890 if (! STRINGP (f->name)
10891 || SBYTES (f->name) != len
10892 || memcmp (title, SDATA (f->name), len) != 0)
10893 x_implicitly_set_name (f, make_string (title, len), Qnil);
10894 }
10895 }
10896
10897 #endif /* not HAVE_WINDOW_SYSTEM */
10898
10899
10900
10901 \f
10902 /***********************************************************************
10903 Menu Bars
10904 ***********************************************************************/
10905
10906
10907 /* Prepare for redisplay by updating menu-bar item lists when
10908 appropriate. This can call eval. */
10909
10910 void
10911 prepare_menu_bars (void)
10912 {
10913 int all_windows;
10914 struct gcpro gcpro1, gcpro2;
10915 struct frame *f;
10916 Lisp_Object tooltip_frame;
10917
10918 #ifdef HAVE_WINDOW_SYSTEM
10919 tooltip_frame = tip_frame;
10920 #else
10921 tooltip_frame = Qnil;
10922 #endif
10923
10924 /* Update all frame titles based on their buffer names, etc. We do
10925 this before the menu bars so that the buffer-menu will show the
10926 up-to-date frame titles. */
10927 #ifdef HAVE_WINDOW_SYSTEM
10928 if (windows_or_buffers_changed || update_mode_lines)
10929 {
10930 Lisp_Object tail, frame;
10931
10932 FOR_EACH_FRAME (tail, frame)
10933 {
10934 f = XFRAME (frame);
10935 if (!EQ (frame, tooltip_frame)
10936 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10937 x_consider_frame_title (frame);
10938 }
10939 }
10940 #endif /* HAVE_WINDOW_SYSTEM */
10941
10942 /* Update the menu bar item lists, if appropriate. This has to be
10943 done before any actual redisplay or generation of display lines. */
10944 all_windows = (update_mode_lines
10945 || buffer_shared > 1
10946 || windows_or_buffers_changed);
10947 if (all_windows)
10948 {
10949 Lisp_Object tail, frame;
10950 int count = SPECPDL_INDEX ();
10951 /* 1 means that update_menu_bar has run its hooks
10952 so any further calls to update_menu_bar shouldn't do so again. */
10953 int menu_bar_hooks_run = 0;
10954
10955 record_unwind_save_match_data ();
10956
10957 FOR_EACH_FRAME (tail, frame)
10958 {
10959 f = XFRAME (frame);
10960
10961 /* Ignore tooltip frame. */
10962 if (EQ (frame, tooltip_frame))
10963 continue;
10964
10965 /* If a window on this frame changed size, report that to
10966 the user and clear the size-change flag. */
10967 if (FRAME_WINDOW_SIZES_CHANGED (f))
10968 {
10969 Lisp_Object functions;
10970
10971 /* Clear flag first in case we get an error below. */
10972 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10973 functions = Vwindow_size_change_functions;
10974 GCPRO2 (tail, functions);
10975
10976 while (CONSP (functions))
10977 {
10978 if (!EQ (XCAR (functions), Qt))
10979 call1 (XCAR (functions), frame);
10980 functions = XCDR (functions);
10981 }
10982 UNGCPRO;
10983 }
10984
10985 GCPRO1 (tail);
10986 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10987 #ifdef HAVE_WINDOW_SYSTEM
10988 update_tool_bar (f, 0);
10989 #endif
10990 #ifdef HAVE_NS
10991 if (windows_or_buffers_changed
10992 && FRAME_NS_P (f))
10993 ns_set_doc_edited (f, Fbuffer_modified_p
10994 (XWINDOW (f->selected_window)->buffer));
10995 #endif
10996 UNGCPRO;
10997 }
10998
10999 unbind_to (count, Qnil);
11000 }
11001 else
11002 {
11003 struct frame *sf = SELECTED_FRAME ();
11004 update_menu_bar (sf, 1, 0);
11005 #ifdef HAVE_WINDOW_SYSTEM
11006 update_tool_bar (sf, 1);
11007 #endif
11008 }
11009 }
11010
11011
11012 /* Update the menu bar item list for frame F. This has to be done
11013 before we start to fill in any display lines, because it can call
11014 eval.
11015
11016 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11017
11018 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11019 already ran the menu bar hooks for this redisplay, so there
11020 is no need to run them again. The return value is the
11021 updated value of this flag, to pass to the next call. */
11022
11023 static int
11024 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11025 {
11026 Lisp_Object window;
11027 register struct window *w;
11028
11029 /* If called recursively during a menu update, do nothing. This can
11030 happen when, for instance, an activate-menubar-hook causes a
11031 redisplay. */
11032 if (inhibit_menubar_update)
11033 return hooks_run;
11034
11035 window = FRAME_SELECTED_WINDOW (f);
11036 w = XWINDOW (window);
11037
11038 if (FRAME_WINDOW_P (f)
11039 ?
11040 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11041 || defined (HAVE_NS) || defined (USE_GTK)
11042 FRAME_EXTERNAL_MENU_BAR (f)
11043 #else
11044 FRAME_MENU_BAR_LINES (f) > 0
11045 #endif
11046 : FRAME_MENU_BAR_LINES (f) > 0)
11047 {
11048 /* If the user has switched buffers or windows, we need to
11049 recompute to reflect the new bindings. But we'll
11050 recompute when update_mode_lines is set too; that means
11051 that people can use force-mode-line-update to request
11052 that the menu bar be recomputed. The adverse effect on
11053 the rest of the redisplay algorithm is about the same as
11054 windows_or_buffers_changed anyway. */
11055 if (windows_or_buffers_changed
11056 /* This used to test w->update_mode_line, but we believe
11057 there is no need to recompute the menu in that case. */
11058 || update_mode_lines
11059 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11060 < BUF_MODIFF (XBUFFER (w->buffer)))
11061 != !NILP (w->last_had_star))
11062 || ((!NILP (Vtransient_mark_mode)
11063 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11064 != !NILP (w->region_showing)))
11065 {
11066 struct buffer *prev = current_buffer;
11067 int count = SPECPDL_INDEX ();
11068
11069 specbind (Qinhibit_menubar_update, Qt);
11070
11071 set_buffer_internal_1 (XBUFFER (w->buffer));
11072 if (save_match_data)
11073 record_unwind_save_match_data ();
11074 if (NILP (Voverriding_local_map_menu_flag))
11075 {
11076 specbind (Qoverriding_terminal_local_map, Qnil);
11077 specbind (Qoverriding_local_map, Qnil);
11078 }
11079
11080 if (!hooks_run)
11081 {
11082 /* Run the Lucid hook. */
11083 safe_run_hooks (Qactivate_menubar_hook);
11084
11085 /* If it has changed current-menubar from previous value,
11086 really recompute the menu-bar from the value. */
11087 if (! NILP (Vlucid_menu_bar_dirty_flag))
11088 call0 (Qrecompute_lucid_menubar);
11089
11090 safe_run_hooks (Qmenu_bar_update_hook);
11091
11092 hooks_run = 1;
11093 }
11094
11095 XSETFRAME (Vmenu_updating_frame, f);
11096 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11097
11098 /* Redisplay the menu bar in case we changed it. */
11099 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11100 || defined (HAVE_NS) || defined (USE_GTK)
11101 if (FRAME_WINDOW_P (f))
11102 {
11103 #if defined (HAVE_NS)
11104 /* All frames on Mac OS share the same menubar. So only
11105 the selected frame should be allowed to set it. */
11106 if (f == SELECTED_FRAME ())
11107 #endif
11108 set_frame_menubar (f, 0, 0);
11109 }
11110 else
11111 /* On a terminal screen, the menu bar is an ordinary screen
11112 line, and this makes it get updated. */
11113 w->update_mode_line = Qt;
11114 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11115 /* In the non-toolkit version, the menu bar is an ordinary screen
11116 line, and this makes it get updated. */
11117 w->update_mode_line = Qt;
11118 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11119
11120 unbind_to (count, Qnil);
11121 set_buffer_internal_1 (prev);
11122 }
11123 }
11124
11125 return hooks_run;
11126 }
11127
11128
11129 \f
11130 /***********************************************************************
11131 Output Cursor
11132 ***********************************************************************/
11133
11134 #ifdef HAVE_WINDOW_SYSTEM
11135
11136 /* EXPORT:
11137 Nominal cursor position -- where to draw output.
11138 HPOS and VPOS are window relative glyph matrix coordinates.
11139 X and Y are window relative pixel coordinates. */
11140
11141 struct cursor_pos output_cursor;
11142
11143
11144 /* EXPORT:
11145 Set the global variable output_cursor to CURSOR. All cursor
11146 positions are relative to updated_window. */
11147
11148 void
11149 set_output_cursor (struct cursor_pos *cursor)
11150 {
11151 output_cursor.hpos = cursor->hpos;
11152 output_cursor.vpos = cursor->vpos;
11153 output_cursor.x = cursor->x;
11154 output_cursor.y = cursor->y;
11155 }
11156
11157
11158 /* EXPORT for RIF:
11159 Set a nominal cursor position.
11160
11161 HPOS and VPOS are column/row positions in a window glyph matrix. X
11162 and Y are window text area relative pixel positions.
11163
11164 If this is done during an update, updated_window will contain the
11165 window that is being updated and the position is the future output
11166 cursor position for that window. If updated_window is null, use
11167 selected_window and display the cursor at the given position. */
11168
11169 void
11170 x_cursor_to (int vpos, int hpos, int y, int x)
11171 {
11172 struct window *w;
11173
11174 /* If updated_window is not set, work on selected_window. */
11175 if (updated_window)
11176 w = updated_window;
11177 else
11178 w = XWINDOW (selected_window);
11179
11180 /* Set the output cursor. */
11181 output_cursor.hpos = hpos;
11182 output_cursor.vpos = vpos;
11183 output_cursor.x = x;
11184 output_cursor.y = y;
11185
11186 /* If not called as part of an update, really display the cursor.
11187 This will also set the cursor position of W. */
11188 if (updated_window == NULL)
11189 {
11190 BLOCK_INPUT;
11191 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11192 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11193 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11194 UNBLOCK_INPUT;
11195 }
11196 }
11197
11198 #endif /* HAVE_WINDOW_SYSTEM */
11199
11200 \f
11201 /***********************************************************************
11202 Tool-bars
11203 ***********************************************************************/
11204
11205 #ifdef HAVE_WINDOW_SYSTEM
11206
11207 /* Where the mouse was last time we reported a mouse event. */
11208
11209 FRAME_PTR last_mouse_frame;
11210
11211 /* Tool-bar item index of the item on which a mouse button was pressed
11212 or -1. */
11213
11214 int last_tool_bar_item;
11215
11216
11217 static Lisp_Object
11218 update_tool_bar_unwind (Lisp_Object frame)
11219 {
11220 selected_frame = frame;
11221 return Qnil;
11222 }
11223
11224 /* Update the tool-bar item list for frame F. This has to be done
11225 before we start to fill in any display lines. Called from
11226 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11227 and restore it here. */
11228
11229 static void
11230 update_tool_bar (struct frame *f, int save_match_data)
11231 {
11232 #if defined (USE_GTK) || defined (HAVE_NS)
11233 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11234 #else
11235 int do_update = WINDOWP (f->tool_bar_window)
11236 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11237 #endif
11238
11239 if (do_update)
11240 {
11241 Lisp_Object window;
11242 struct window *w;
11243
11244 window = FRAME_SELECTED_WINDOW (f);
11245 w = XWINDOW (window);
11246
11247 /* If the user has switched buffers or windows, we need to
11248 recompute to reflect the new bindings. But we'll
11249 recompute when update_mode_lines is set too; that means
11250 that people can use force-mode-line-update to request
11251 that the menu bar be recomputed. The adverse effect on
11252 the rest of the redisplay algorithm is about the same as
11253 windows_or_buffers_changed anyway. */
11254 if (windows_or_buffers_changed
11255 || !NILP (w->update_mode_line)
11256 || update_mode_lines
11257 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11258 < BUF_MODIFF (XBUFFER (w->buffer)))
11259 != !NILP (w->last_had_star))
11260 || ((!NILP (Vtransient_mark_mode)
11261 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11262 != !NILP (w->region_showing)))
11263 {
11264 struct buffer *prev = current_buffer;
11265 int count = SPECPDL_INDEX ();
11266 Lisp_Object frame, new_tool_bar;
11267 int new_n_tool_bar;
11268 struct gcpro gcpro1;
11269
11270 /* Set current_buffer to the buffer of the selected
11271 window of the frame, so that we get the right local
11272 keymaps. */
11273 set_buffer_internal_1 (XBUFFER (w->buffer));
11274
11275 /* Save match data, if we must. */
11276 if (save_match_data)
11277 record_unwind_save_match_data ();
11278
11279 /* Make sure that we don't accidentally use bogus keymaps. */
11280 if (NILP (Voverriding_local_map_menu_flag))
11281 {
11282 specbind (Qoverriding_terminal_local_map, Qnil);
11283 specbind (Qoverriding_local_map, Qnil);
11284 }
11285
11286 GCPRO1 (new_tool_bar);
11287
11288 /* We must temporarily set the selected frame to this frame
11289 before calling tool_bar_items, because the calculation of
11290 the tool-bar keymap uses the selected frame (see
11291 `tool-bar-make-keymap' in tool-bar.el). */
11292 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11293 XSETFRAME (frame, f);
11294 selected_frame = frame;
11295
11296 /* Build desired tool-bar items from keymaps. */
11297 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11298 &new_n_tool_bar);
11299
11300 /* Redisplay the tool-bar if we changed it. */
11301 if (new_n_tool_bar != f->n_tool_bar_items
11302 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11303 {
11304 /* Redisplay that happens asynchronously due to an expose event
11305 may access f->tool_bar_items. Make sure we update both
11306 variables within BLOCK_INPUT so no such event interrupts. */
11307 BLOCK_INPUT;
11308 f->tool_bar_items = new_tool_bar;
11309 f->n_tool_bar_items = new_n_tool_bar;
11310 w->update_mode_line = Qt;
11311 UNBLOCK_INPUT;
11312 }
11313
11314 UNGCPRO;
11315
11316 unbind_to (count, Qnil);
11317 set_buffer_internal_1 (prev);
11318 }
11319 }
11320 }
11321
11322
11323 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11324 F's desired tool-bar contents. F->tool_bar_items must have
11325 been set up previously by calling prepare_menu_bars. */
11326
11327 static void
11328 build_desired_tool_bar_string (struct frame *f)
11329 {
11330 int i, size, size_needed;
11331 struct gcpro gcpro1, gcpro2, gcpro3;
11332 Lisp_Object image, plist, props;
11333
11334 image = plist = props = Qnil;
11335 GCPRO3 (image, plist, props);
11336
11337 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11338 Otherwise, make a new string. */
11339
11340 /* The size of the string we might be able to reuse. */
11341 size = (STRINGP (f->desired_tool_bar_string)
11342 ? SCHARS (f->desired_tool_bar_string)
11343 : 0);
11344
11345 /* We need one space in the string for each image. */
11346 size_needed = f->n_tool_bar_items;
11347
11348 /* Reuse f->desired_tool_bar_string, if possible. */
11349 if (size < size_needed || NILP (f->desired_tool_bar_string))
11350 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11351 make_number (' '));
11352 else
11353 {
11354 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11355 Fremove_text_properties (make_number (0), make_number (size),
11356 props, f->desired_tool_bar_string);
11357 }
11358
11359 /* Put a `display' property on the string for the images to display,
11360 put a `menu_item' property on tool-bar items with a value that
11361 is the index of the item in F's tool-bar item vector. */
11362 for (i = 0; i < f->n_tool_bar_items; ++i)
11363 {
11364 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11365
11366 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11367 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11368 int hmargin, vmargin, relief, idx, end;
11369
11370 /* If image is a vector, choose the image according to the
11371 button state. */
11372 image = PROP (TOOL_BAR_ITEM_IMAGES);
11373 if (VECTORP (image))
11374 {
11375 if (enabled_p)
11376 idx = (selected_p
11377 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11378 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11379 else
11380 idx = (selected_p
11381 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11382 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11383
11384 xassert (ASIZE (image) >= idx);
11385 image = AREF (image, idx);
11386 }
11387 else
11388 idx = -1;
11389
11390 /* Ignore invalid image specifications. */
11391 if (!valid_image_p (image))
11392 continue;
11393
11394 /* Display the tool-bar button pressed, or depressed. */
11395 plist = Fcopy_sequence (XCDR (image));
11396
11397 /* Compute margin and relief to draw. */
11398 relief = (tool_bar_button_relief >= 0
11399 ? tool_bar_button_relief
11400 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11401 hmargin = vmargin = relief;
11402
11403 if (INTEGERP (Vtool_bar_button_margin)
11404 && XINT (Vtool_bar_button_margin) > 0)
11405 {
11406 hmargin += XFASTINT (Vtool_bar_button_margin);
11407 vmargin += XFASTINT (Vtool_bar_button_margin);
11408 }
11409 else if (CONSP (Vtool_bar_button_margin))
11410 {
11411 if (INTEGERP (XCAR (Vtool_bar_button_margin))
11412 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
11413 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11414
11415 if (INTEGERP (XCDR (Vtool_bar_button_margin))
11416 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
11417 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11418 }
11419
11420 if (auto_raise_tool_bar_buttons_p)
11421 {
11422 /* Add a `:relief' property to the image spec if the item is
11423 selected. */
11424 if (selected_p)
11425 {
11426 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11427 hmargin -= relief;
11428 vmargin -= relief;
11429 }
11430 }
11431 else
11432 {
11433 /* If image is selected, display it pressed, i.e. with a
11434 negative relief. If it's not selected, display it with a
11435 raised relief. */
11436 plist = Fplist_put (plist, QCrelief,
11437 (selected_p
11438 ? make_number (-relief)
11439 : make_number (relief)));
11440 hmargin -= relief;
11441 vmargin -= relief;
11442 }
11443
11444 /* Put a margin around the image. */
11445 if (hmargin || vmargin)
11446 {
11447 if (hmargin == vmargin)
11448 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11449 else
11450 plist = Fplist_put (plist, QCmargin,
11451 Fcons (make_number (hmargin),
11452 make_number (vmargin)));
11453 }
11454
11455 /* If button is not enabled, and we don't have special images
11456 for the disabled state, make the image appear disabled by
11457 applying an appropriate algorithm to it. */
11458 if (!enabled_p && idx < 0)
11459 plist = Fplist_put (plist, QCconversion, Qdisabled);
11460
11461 /* Put a `display' text property on the string for the image to
11462 display. Put a `menu-item' property on the string that gives
11463 the start of this item's properties in the tool-bar items
11464 vector. */
11465 image = Fcons (Qimage, plist);
11466 props = list4 (Qdisplay, image,
11467 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11468
11469 /* Let the last image hide all remaining spaces in the tool bar
11470 string. The string can be longer than needed when we reuse a
11471 previous string. */
11472 if (i + 1 == f->n_tool_bar_items)
11473 end = SCHARS (f->desired_tool_bar_string);
11474 else
11475 end = i + 1;
11476 Fadd_text_properties (make_number (i), make_number (end),
11477 props, f->desired_tool_bar_string);
11478 #undef PROP
11479 }
11480
11481 UNGCPRO;
11482 }
11483
11484
11485 /* Display one line of the tool-bar of frame IT->f.
11486
11487 HEIGHT specifies the desired height of the tool-bar line.
11488 If the actual height of the glyph row is less than HEIGHT, the
11489 row's height is increased to HEIGHT, and the icons are centered
11490 vertically in the new height.
11491
11492 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11493 count a final empty row in case the tool-bar width exactly matches
11494 the window width.
11495 */
11496
11497 static void
11498 display_tool_bar_line (struct it *it, int height)
11499 {
11500 struct glyph_row *row = it->glyph_row;
11501 int max_x = it->last_visible_x;
11502 struct glyph *last;
11503
11504 prepare_desired_row (row);
11505 row->y = it->current_y;
11506
11507 /* Note that this isn't made use of if the face hasn't a box,
11508 so there's no need to check the face here. */
11509 it->start_of_box_run_p = 1;
11510
11511 while (it->current_x < max_x)
11512 {
11513 int x, n_glyphs_before, i, nglyphs;
11514 struct it it_before;
11515
11516 /* Get the next display element. */
11517 if (!get_next_display_element (it))
11518 {
11519 /* Don't count empty row if we are counting needed tool-bar lines. */
11520 if (height < 0 && !it->hpos)
11521 return;
11522 break;
11523 }
11524
11525 /* Produce glyphs. */
11526 n_glyphs_before = row->used[TEXT_AREA];
11527 it_before = *it;
11528
11529 PRODUCE_GLYPHS (it);
11530
11531 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11532 i = 0;
11533 x = it_before.current_x;
11534 while (i < nglyphs)
11535 {
11536 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11537
11538 if (x + glyph->pixel_width > max_x)
11539 {
11540 /* Glyph doesn't fit on line. Backtrack. */
11541 row->used[TEXT_AREA] = n_glyphs_before;
11542 *it = it_before;
11543 /* If this is the only glyph on this line, it will never fit on the
11544 tool-bar, so skip it. But ensure there is at least one glyph,
11545 so we don't accidentally disable the tool-bar. */
11546 if (n_glyphs_before == 0
11547 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11548 break;
11549 goto out;
11550 }
11551
11552 ++it->hpos;
11553 x += glyph->pixel_width;
11554 ++i;
11555 }
11556
11557 /* Stop at line end. */
11558 if (ITERATOR_AT_END_OF_LINE_P (it))
11559 break;
11560
11561 set_iterator_to_next (it, 1);
11562 }
11563
11564 out:;
11565
11566 row->displays_text_p = row->used[TEXT_AREA] != 0;
11567
11568 /* Use default face for the border below the tool bar.
11569
11570 FIXME: When auto-resize-tool-bars is grow-only, there is
11571 no additional border below the possibly empty tool-bar lines.
11572 So to make the extra empty lines look "normal", we have to
11573 use the tool-bar face for the border too. */
11574 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11575 it->face_id = DEFAULT_FACE_ID;
11576
11577 extend_face_to_end_of_line (it);
11578 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11579 last->right_box_line_p = 1;
11580 if (last == row->glyphs[TEXT_AREA])
11581 last->left_box_line_p = 1;
11582
11583 /* Make line the desired height and center it vertically. */
11584 if ((height -= it->max_ascent + it->max_descent) > 0)
11585 {
11586 /* Don't add more than one line height. */
11587 height %= FRAME_LINE_HEIGHT (it->f);
11588 it->max_ascent += height / 2;
11589 it->max_descent += (height + 1) / 2;
11590 }
11591
11592 compute_line_metrics (it);
11593
11594 /* If line is empty, make it occupy the rest of the tool-bar. */
11595 if (!row->displays_text_p)
11596 {
11597 row->height = row->phys_height = it->last_visible_y - row->y;
11598 row->visible_height = row->height;
11599 row->ascent = row->phys_ascent = 0;
11600 row->extra_line_spacing = 0;
11601 }
11602
11603 row->full_width_p = 1;
11604 row->continued_p = 0;
11605 row->truncated_on_left_p = 0;
11606 row->truncated_on_right_p = 0;
11607
11608 it->current_x = it->hpos = 0;
11609 it->current_y += row->height;
11610 ++it->vpos;
11611 ++it->glyph_row;
11612 }
11613
11614
11615 /* Max tool-bar height. */
11616
11617 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11618 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11619
11620 /* Value is the number of screen lines needed to make all tool-bar
11621 items of frame F visible. The number of actual rows needed is
11622 returned in *N_ROWS if non-NULL. */
11623
11624 static int
11625 tool_bar_lines_needed (struct frame *f, int *n_rows)
11626 {
11627 struct window *w = XWINDOW (f->tool_bar_window);
11628 struct it it;
11629 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11630 the desired matrix, so use (unused) mode-line row as temporary row to
11631 avoid destroying the first tool-bar row. */
11632 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11633
11634 /* Initialize an iterator for iteration over
11635 F->desired_tool_bar_string in the tool-bar window of frame F. */
11636 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11637 it.first_visible_x = 0;
11638 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11639 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11640 it.paragraph_embedding = L2R;
11641
11642 while (!ITERATOR_AT_END_P (&it))
11643 {
11644 clear_glyph_row (temp_row);
11645 it.glyph_row = temp_row;
11646 display_tool_bar_line (&it, -1);
11647 }
11648 clear_glyph_row (temp_row);
11649
11650 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11651 if (n_rows)
11652 *n_rows = it.vpos > 0 ? it.vpos : -1;
11653
11654 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11655 }
11656
11657
11658 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11659 0, 1, 0,
11660 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11661 (Lisp_Object frame)
11662 {
11663 struct frame *f;
11664 struct window *w;
11665 int nlines = 0;
11666
11667 if (NILP (frame))
11668 frame = selected_frame;
11669 else
11670 CHECK_FRAME (frame);
11671 f = XFRAME (frame);
11672
11673 if (WINDOWP (f->tool_bar_window)
11674 && (w = XWINDOW (f->tool_bar_window),
11675 WINDOW_TOTAL_LINES (w) > 0))
11676 {
11677 update_tool_bar (f, 1);
11678 if (f->n_tool_bar_items)
11679 {
11680 build_desired_tool_bar_string (f);
11681 nlines = tool_bar_lines_needed (f, NULL);
11682 }
11683 }
11684
11685 return make_number (nlines);
11686 }
11687
11688
11689 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11690 height should be changed. */
11691
11692 static int
11693 redisplay_tool_bar (struct frame *f)
11694 {
11695 struct window *w;
11696 struct it it;
11697 struct glyph_row *row;
11698
11699 #if defined (USE_GTK) || defined (HAVE_NS)
11700 if (FRAME_EXTERNAL_TOOL_BAR (f))
11701 update_frame_tool_bar (f);
11702 return 0;
11703 #endif
11704
11705 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11706 do anything. This means you must start with tool-bar-lines
11707 non-zero to get the auto-sizing effect. Or in other words, you
11708 can turn off tool-bars by specifying tool-bar-lines zero. */
11709 if (!WINDOWP (f->tool_bar_window)
11710 || (w = XWINDOW (f->tool_bar_window),
11711 WINDOW_TOTAL_LINES (w) == 0))
11712 return 0;
11713
11714 /* Set up an iterator for the tool-bar window. */
11715 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11716 it.first_visible_x = 0;
11717 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11718 row = it.glyph_row;
11719
11720 /* Build a string that represents the contents of the tool-bar. */
11721 build_desired_tool_bar_string (f);
11722 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11723 /* FIXME: This should be controlled by a user option. But it
11724 doesn't make sense to have an R2L tool bar if the menu bar cannot
11725 be drawn also R2L, and making the menu bar R2L is tricky due
11726 toolkit-specific code that implements it. If an R2L tool bar is
11727 ever supported, display_tool_bar_line should also be augmented to
11728 call unproduce_glyphs like display_line and display_string
11729 do. */
11730 it.paragraph_embedding = L2R;
11731
11732 if (f->n_tool_bar_rows == 0)
11733 {
11734 int nlines;
11735
11736 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11737 nlines != WINDOW_TOTAL_LINES (w)))
11738 {
11739 Lisp_Object frame;
11740 int old_height = WINDOW_TOTAL_LINES (w);
11741
11742 XSETFRAME (frame, f);
11743 Fmodify_frame_parameters (frame,
11744 Fcons (Fcons (Qtool_bar_lines,
11745 make_number (nlines)),
11746 Qnil));
11747 if (WINDOW_TOTAL_LINES (w) != old_height)
11748 {
11749 clear_glyph_matrix (w->desired_matrix);
11750 fonts_changed_p = 1;
11751 return 1;
11752 }
11753 }
11754 }
11755
11756 /* Display as many lines as needed to display all tool-bar items. */
11757
11758 if (f->n_tool_bar_rows > 0)
11759 {
11760 int border, rows, height, extra;
11761
11762 if (INTEGERP (Vtool_bar_border))
11763 border = XINT (Vtool_bar_border);
11764 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11765 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11766 else if (EQ (Vtool_bar_border, Qborder_width))
11767 border = f->border_width;
11768 else
11769 border = 0;
11770 if (border < 0)
11771 border = 0;
11772
11773 rows = f->n_tool_bar_rows;
11774 height = max (1, (it.last_visible_y - border) / rows);
11775 extra = it.last_visible_y - border - height * rows;
11776
11777 while (it.current_y < it.last_visible_y)
11778 {
11779 int h = 0;
11780 if (extra > 0 && rows-- > 0)
11781 {
11782 h = (extra + rows - 1) / rows;
11783 extra -= h;
11784 }
11785 display_tool_bar_line (&it, height + h);
11786 }
11787 }
11788 else
11789 {
11790 while (it.current_y < it.last_visible_y)
11791 display_tool_bar_line (&it, 0);
11792 }
11793
11794 /* It doesn't make much sense to try scrolling in the tool-bar
11795 window, so don't do it. */
11796 w->desired_matrix->no_scrolling_p = 1;
11797 w->must_be_updated_p = 1;
11798
11799 if (!NILP (Vauto_resize_tool_bars))
11800 {
11801 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11802 int change_height_p = 0;
11803
11804 /* If we couldn't display everything, change the tool-bar's
11805 height if there is room for more. */
11806 if (IT_STRING_CHARPOS (it) < it.end_charpos
11807 && it.current_y < max_tool_bar_height)
11808 change_height_p = 1;
11809
11810 row = it.glyph_row - 1;
11811
11812 /* If there are blank lines at the end, except for a partially
11813 visible blank line at the end that is smaller than
11814 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11815 if (!row->displays_text_p
11816 && row->height >= FRAME_LINE_HEIGHT (f))
11817 change_height_p = 1;
11818
11819 /* If row displays tool-bar items, but is partially visible,
11820 change the tool-bar's height. */
11821 if (row->displays_text_p
11822 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11823 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11824 change_height_p = 1;
11825
11826 /* Resize windows as needed by changing the `tool-bar-lines'
11827 frame parameter. */
11828 if (change_height_p)
11829 {
11830 Lisp_Object frame;
11831 int old_height = WINDOW_TOTAL_LINES (w);
11832 int nrows;
11833 int nlines = tool_bar_lines_needed (f, &nrows);
11834
11835 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11836 && !f->minimize_tool_bar_window_p)
11837 ? (nlines > old_height)
11838 : (nlines != old_height));
11839 f->minimize_tool_bar_window_p = 0;
11840
11841 if (change_height_p)
11842 {
11843 XSETFRAME (frame, f);
11844 Fmodify_frame_parameters (frame,
11845 Fcons (Fcons (Qtool_bar_lines,
11846 make_number (nlines)),
11847 Qnil));
11848 if (WINDOW_TOTAL_LINES (w) != old_height)
11849 {
11850 clear_glyph_matrix (w->desired_matrix);
11851 f->n_tool_bar_rows = nrows;
11852 fonts_changed_p = 1;
11853 return 1;
11854 }
11855 }
11856 }
11857 }
11858
11859 f->minimize_tool_bar_window_p = 0;
11860 return 0;
11861 }
11862
11863
11864 /* Get information about the tool-bar item which is displayed in GLYPH
11865 on frame F. Return in *PROP_IDX the index where tool-bar item
11866 properties start in F->tool_bar_items. Value is zero if
11867 GLYPH doesn't display a tool-bar item. */
11868
11869 static int
11870 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11871 {
11872 Lisp_Object prop;
11873 int success_p;
11874 int charpos;
11875
11876 /* This function can be called asynchronously, which means we must
11877 exclude any possibility that Fget_text_property signals an
11878 error. */
11879 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11880 charpos = max (0, charpos);
11881
11882 /* Get the text property `menu-item' at pos. The value of that
11883 property is the start index of this item's properties in
11884 F->tool_bar_items. */
11885 prop = Fget_text_property (make_number (charpos),
11886 Qmenu_item, f->current_tool_bar_string);
11887 if (INTEGERP (prop))
11888 {
11889 *prop_idx = XINT (prop);
11890 success_p = 1;
11891 }
11892 else
11893 success_p = 0;
11894
11895 return success_p;
11896 }
11897
11898 \f
11899 /* Get information about the tool-bar item at position X/Y on frame F.
11900 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11901 the current matrix of the tool-bar window of F, or NULL if not
11902 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11903 item in F->tool_bar_items. Value is
11904
11905 -1 if X/Y is not on a tool-bar item
11906 0 if X/Y is on the same item that was highlighted before.
11907 1 otherwise. */
11908
11909 static int
11910 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11911 int *hpos, int *vpos, int *prop_idx)
11912 {
11913 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11914 struct window *w = XWINDOW (f->tool_bar_window);
11915 int area;
11916
11917 /* Find the glyph under X/Y. */
11918 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11919 if (*glyph == NULL)
11920 return -1;
11921
11922 /* Get the start of this tool-bar item's properties in
11923 f->tool_bar_items. */
11924 if (!tool_bar_item_info (f, *glyph, prop_idx))
11925 return -1;
11926
11927 /* Is mouse on the highlighted item? */
11928 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11929 && *vpos >= hlinfo->mouse_face_beg_row
11930 && *vpos <= hlinfo->mouse_face_end_row
11931 && (*vpos > hlinfo->mouse_face_beg_row
11932 || *hpos >= hlinfo->mouse_face_beg_col)
11933 && (*vpos < hlinfo->mouse_face_end_row
11934 || *hpos < hlinfo->mouse_face_end_col
11935 || hlinfo->mouse_face_past_end))
11936 return 0;
11937
11938 return 1;
11939 }
11940
11941
11942 /* EXPORT:
11943 Handle mouse button event on the tool-bar of frame F, at
11944 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11945 0 for button release. MODIFIERS is event modifiers for button
11946 release. */
11947
11948 void
11949 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11950 unsigned int modifiers)
11951 {
11952 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11953 struct window *w = XWINDOW (f->tool_bar_window);
11954 int hpos, vpos, prop_idx;
11955 struct glyph *glyph;
11956 Lisp_Object enabled_p;
11957
11958 /* If not on the highlighted tool-bar item, return. */
11959 frame_to_window_pixel_xy (w, &x, &y);
11960 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11961 return;
11962
11963 /* If item is disabled, do nothing. */
11964 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11965 if (NILP (enabled_p))
11966 return;
11967
11968 if (down_p)
11969 {
11970 /* Show item in pressed state. */
11971 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11972 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11973 last_tool_bar_item = prop_idx;
11974 }
11975 else
11976 {
11977 Lisp_Object key, frame;
11978 struct input_event event;
11979 EVENT_INIT (event);
11980
11981 /* Show item in released state. */
11982 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11983 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11984
11985 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11986
11987 XSETFRAME (frame, f);
11988 event.kind = TOOL_BAR_EVENT;
11989 event.frame_or_window = frame;
11990 event.arg = frame;
11991 kbd_buffer_store_event (&event);
11992
11993 event.kind = TOOL_BAR_EVENT;
11994 event.frame_or_window = frame;
11995 event.arg = key;
11996 event.modifiers = modifiers;
11997 kbd_buffer_store_event (&event);
11998 last_tool_bar_item = -1;
11999 }
12000 }
12001
12002
12003 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12004 tool-bar window-relative coordinates X/Y. Called from
12005 note_mouse_highlight. */
12006
12007 static void
12008 note_tool_bar_highlight (struct frame *f, int x, int y)
12009 {
12010 Lisp_Object window = f->tool_bar_window;
12011 struct window *w = XWINDOW (window);
12012 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12013 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12014 int hpos, vpos;
12015 struct glyph *glyph;
12016 struct glyph_row *row;
12017 int i;
12018 Lisp_Object enabled_p;
12019 int prop_idx;
12020 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12021 int mouse_down_p, rc;
12022
12023 /* Function note_mouse_highlight is called with negative X/Y
12024 values when mouse moves outside of the frame. */
12025 if (x <= 0 || y <= 0)
12026 {
12027 clear_mouse_face (hlinfo);
12028 return;
12029 }
12030
12031 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12032 if (rc < 0)
12033 {
12034 /* Not on tool-bar item. */
12035 clear_mouse_face (hlinfo);
12036 return;
12037 }
12038 else if (rc == 0)
12039 /* On same tool-bar item as before. */
12040 goto set_help_echo;
12041
12042 clear_mouse_face (hlinfo);
12043
12044 /* Mouse is down, but on different tool-bar item? */
12045 mouse_down_p = (dpyinfo->grabbed
12046 && f == last_mouse_frame
12047 && FRAME_LIVE_P (f));
12048 if (mouse_down_p
12049 && last_tool_bar_item != prop_idx)
12050 return;
12051
12052 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12053 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12054
12055 /* If tool-bar item is not enabled, don't highlight it. */
12056 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12057 if (!NILP (enabled_p))
12058 {
12059 /* Compute the x-position of the glyph. In front and past the
12060 image is a space. We include this in the highlighted area. */
12061 row = MATRIX_ROW (w->current_matrix, vpos);
12062 for (i = x = 0; i < hpos; ++i)
12063 x += row->glyphs[TEXT_AREA][i].pixel_width;
12064
12065 /* Record this as the current active region. */
12066 hlinfo->mouse_face_beg_col = hpos;
12067 hlinfo->mouse_face_beg_row = vpos;
12068 hlinfo->mouse_face_beg_x = x;
12069 hlinfo->mouse_face_beg_y = row->y;
12070 hlinfo->mouse_face_past_end = 0;
12071
12072 hlinfo->mouse_face_end_col = hpos + 1;
12073 hlinfo->mouse_face_end_row = vpos;
12074 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12075 hlinfo->mouse_face_end_y = row->y;
12076 hlinfo->mouse_face_window = window;
12077 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12078
12079 /* Display it as active. */
12080 show_mouse_face (hlinfo, draw);
12081 hlinfo->mouse_face_image_state = draw;
12082 }
12083
12084 set_help_echo:
12085
12086 /* Set help_echo_string to a help string to display for this tool-bar item.
12087 XTread_socket does the rest. */
12088 help_echo_object = help_echo_window = Qnil;
12089 help_echo_pos = -1;
12090 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12091 if (NILP (help_echo_string))
12092 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12093 }
12094
12095 #endif /* HAVE_WINDOW_SYSTEM */
12096
12097
12098 \f
12099 /************************************************************************
12100 Horizontal scrolling
12101 ************************************************************************/
12102
12103 static int hscroll_window_tree (Lisp_Object);
12104 static int hscroll_windows (Lisp_Object);
12105
12106 /* For all leaf windows in the window tree rooted at WINDOW, set their
12107 hscroll value so that PT is (i) visible in the window, and (ii) so
12108 that it is not within a certain margin at the window's left and
12109 right border. Value is non-zero if any window's hscroll has been
12110 changed. */
12111
12112 static int
12113 hscroll_window_tree (Lisp_Object window)
12114 {
12115 int hscrolled_p = 0;
12116 int hscroll_relative_p = FLOATP (Vhscroll_step);
12117 int hscroll_step_abs = 0;
12118 double hscroll_step_rel = 0;
12119
12120 if (hscroll_relative_p)
12121 {
12122 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12123 if (hscroll_step_rel < 0)
12124 {
12125 hscroll_relative_p = 0;
12126 hscroll_step_abs = 0;
12127 }
12128 }
12129 else if (INTEGERP (Vhscroll_step))
12130 {
12131 hscroll_step_abs = XINT (Vhscroll_step);
12132 if (hscroll_step_abs < 0)
12133 hscroll_step_abs = 0;
12134 }
12135 else
12136 hscroll_step_abs = 0;
12137
12138 while (WINDOWP (window))
12139 {
12140 struct window *w = XWINDOW (window);
12141
12142 if (WINDOWP (w->hchild))
12143 hscrolled_p |= hscroll_window_tree (w->hchild);
12144 else if (WINDOWP (w->vchild))
12145 hscrolled_p |= hscroll_window_tree (w->vchild);
12146 else if (w->cursor.vpos >= 0)
12147 {
12148 int h_margin;
12149 int text_area_width;
12150 struct glyph_row *current_cursor_row
12151 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12152 struct glyph_row *desired_cursor_row
12153 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12154 struct glyph_row *cursor_row
12155 = (desired_cursor_row->enabled_p
12156 ? desired_cursor_row
12157 : current_cursor_row);
12158 int row_r2l_p = cursor_row->reversed_p;
12159
12160 text_area_width = window_box_width (w, TEXT_AREA);
12161
12162 /* Scroll when cursor is inside this scroll margin. */
12163 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12164
12165 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12166 /* For left-to-right rows, hscroll when cursor is either
12167 (i) inside the right hscroll margin, or (ii) if it is
12168 inside the left margin and the window is already
12169 hscrolled. */
12170 && ((!row_r2l_p
12171 && ((XFASTINT (w->hscroll)
12172 && w->cursor.x <= h_margin)
12173 || (cursor_row->enabled_p
12174 && cursor_row->truncated_on_right_p
12175 && (w->cursor.x >= text_area_width - h_margin))))
12176 /* For right-to-left rows, the logic is similar,
12177 except that rules for scrolling to left and right
12178 are reversed. E.g., if cursor.x <= h_margin, we
12179 need to hscroll "to the right" unconditionally,
12180 and that will scroll the screen to the left so as
12181 to reveal the next portion of the row. */
12182 || (row_r2l_p
12183 && ((cursor_row->enabled_p
12184 /* FIXME: It is confusing to set the
12185 truncated_on_right_p flag when R2L rows
12186 are actually truncated on the left. */
12187 && cursor_row->truncated_on_right_p
12188 && w->cursor.x <= h_margin)
12189 || (XFASTINT (w->hscroll)
12190 && (w->cursor.x >= text_area_width - h_margin))))))
12191 {
12192 struct it it;
12193 int hscroll;
12194 struct buffer *saved_current_buffer;
12195 EMACS_INT pt;
12196 int wanted_x;
12197
12198 /* Find point in a display of infinite width. */
12199 saved_current_buffer = current_buffer;
12200 current_buffer = XBUFFER (w->buffer);
12201
12202 if (w == XWINDOW (selected_window))
12203 pt = PT;
12204 else
12205 {
12206 pt = marker_position (w->pointm);
12207 pt = max (BEGV, pt);
12208 pt = min (ZV, pt);
12209 }
12210
12211 /* Move iterator to pt starting at cursor_row->start in
12212 a line with infinite width. */
12213 init_to_row_start (&it, w, cursor_row);
12214 it.last_visible_x = INFINITY;
12215 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12216 current_buffer = saved_current_buffer;
12217
12218 /* Position cursor in window. */
12219 if (!hscroll_relative_p && hscroll_step_abs == 0)
12220 hscroll = max (0, (it.current_x
12221 - (ITERATOR_AT_END_OF_LINE_P (&it)
12222 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12223 : (text_area_width / 2))))
12224 / FRAME_COLUMN_WIDTH (it.f);
12225 else if ((!row_r2l_p
12226 && w->cursor.x >= text_area_width - h_margin)
12227 || (row_r2l_p && w->cursor.x <= h_margin))
12228 {
12229 if (hscroll_relative_p)
12230 wanted_x = text_area_width * (1 - hscroll_step_rel)
12231 - h_margin;
12232 else
12233 wanted_x = text_area_width
12234 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12235 - h_margin;
12236 hscroll
12237 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12238 }
12239 else
12240 {
12241 if (hscroll_relative_p)
12242 wanted_x = text_area_width * hscroll_step_rel
12243 + h_margin;
12244 else
12245 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12246 + h_margin;
12247 hscroll
12248 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12249 }
12250 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
12251
12252 /* Don't prevent redisplay optimizations if hscroll
12253 hasn't changed, as it will unnecessarily slow down
12254 redisplay. */
12255 if (XFASTINT (w->hscroll) != hscroll)
12256 {
12257 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12258 w->hscroll = make_number (hscroll);
12259 hscrolled_p = 1;
12260 }
12261 }
12262 }
12263
12264 window = w->next;
12265 }
12266
12267 /* Value is non-zero if hscroll of any leaf window has been changed. */
12268 return hscrolled_p;
12269 }
12270
12271
12272 /* Set hscroll so that cursor is visible and not inside horizontal
12273 scroll margins for all windows in the tree rooted at WINDOW. See
12274 also hscroll_window_tree above. Value is non-zero if any window's
12275 hscroll has been changed. If it has, desired matrices on the frame
12276 of WINDOW are cleared. */
12277
12278 static int
12279 hscroll_windows (Lisp_Object window)
12280 {
12281 int hscrolled_p = hscroll_window_tree (window);
12282 if (hscrolled_p)
12283 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12284 return hscrolled_p;
12285 }
12286
12287
12288 \f
12289 /************************************************************************
12290 Redisplay
12291 ************************************************************************/
12292
12293 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12294 to a non-zero value. This is sometimes handy to have in a debugger
12295 session. */
12296
12297 #if GLYPH_DEBUG
12298
12299 /* First and last unchanged row for try_window_id. */
12300
12301 static int debug_first_unchanged_at_end_vpos;
12302 static int debug_last_unchanged_at_beg_vpos;
12303
12304 /* Delta vpos and y. */
12305
12306 static int debug_dvpos, debug_dy;
12307
12308 /* Delta in characters and bytes for try_window_id. */
12309
12310 static EMACS_INT debug_delta, debug_delta_bytes;
12311
12312 /* Values of window_end_pos and window_end_vpos at the end of
12313 try_window_id. */
12314
12315 static EMACS_INT debug_end_vpos;
12316
12317 /* Append a string to W->desired_matrix->method. FMT is a printf
12318 format string. If trace_redisplay_p is non-zero also printf the
12319 resulting string to stderr. */
12320
12321 static void debug_method_add (struct window *, char const *, ...)
12322 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12323
12324 static void
12325 debug_method_add (struct window *w, char const *fmt, ...)
12326 {
12327 char buffer[512];
12328 char *method = w->desired_matrix->method;
12329 int len = strlen (method);
12330 int size = sizeof w->desired_matrix->method;
12331 int remaining = size - len - 1;
12332 va_list ap;
12333
12334 va_start (ap, fmt);
12335 vsprintf (buffer, fmt, ap);
12336 va_end (ap);
12337 if (len && remaining)
12338 {
12339 method[len] = '|';
12340 --remaining, ++len;
12341 }
12342
12343 strncpy (method + len, buffer, remaining);
12344
12345 if (trace_redisplay_p)
12346 fprintf (stderr, "%p (%s): %s\n",
12347 w,
12348 ((BUFFERP (w->buffer)
12349 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12350 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12351 : "no buffer"),
12352 buffer);
12353 }
12354
12355 #endif /* GLYPH_DEBUG */
12356
12357
12358 /* Value is non-zero if all changes in window W, which displays
12359 current_buffer, are in the text between START and END. START is a
12360 buffer position, END is given as a distance from Z. Used in
12361 redisplay_internal for display optimization. */
12362
12363 static inline int
12364 text_outside_line_unchanged_p (struct window *w,
12365 EMACS_INT start, EMACS_INT end)
12366 {
12367 int unchanged_p = 1;
12368
12369 /* If text or overlays have changed, see where. */
12370 if (XFASTINT (w->last_modified) < MODIFF
12371 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12372 {
12373 /* Gap in the line? */
12374 if (GPT < start || Z - GPT < end)
12375 unchanged_p = 0;
12376
12377 /* Changes start in front of the line, or end after it? */
12378 if (unchanged_p
12379 && (BEG_UNCHANGED < start - 1
12380 || END_UNCHANGED < end))
12381 unchanged_p = 0;
12382
12383 /* If selective display, can't optimize if changes start at the
12384 beginning of the line. */
12385 if (unchanged_p
12386 && INTEGERP (BVAR (current_buffer, selective_display))
12387 && XINT (BVAR (current_buffer, selective_display)) > 0
12388 && (BEG_UNCHANGED < start || GPT <= start))
12389 unchanged_p = 0;
12390
12391 /* If there are overlays at the start or end of the line, these
12392 may have overlay strings with newlines in them. A change at
12393 START, for instance, may actually concern the display of such
12394 overlay strings as well, and they are displayed on different
12395 lines. So, quickly rule out this case. (For the future, it
12396 might be desirable to implement something more telling than
12397 just BEG/END_UNCHANGED.) */
12398 if (unchanged_p)
12399 {
12400 if (BEG + BEG_UNCHANGED == start
12401 && overlay_touches_p (start))
12402 unchanged_p = 0;
12403 if (END_UNCHANGED == end
12404 && overlay_touches_p (Z - end))
12405 unchanged_p = 0;
12406 }
12407
12408 /* Under bidi reordering, adding or deleting a character in the
12409 beginning of a paragraph, before the first strong directional
12410 character, can change the base direction of the paragraph (unless
12411 the buffer specifies a fixed paragraph direction), which will
12412 require to redisplay the whole paragraph. It might be worthwhile
12413 to find the paragraph limits and widen the range of redisplayed
12414 lines to that, but for now just give up this optimization. */
12415 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12416 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12417 unchanged_p = 0;
12418 }
12419
12420 return unchanged_p;
12421 }
12422
12423
12424 /* Do a frame update, taking possible shortcuts into account. This is
12425 the main external entry point for redisplay.
12426
12427 If the last redisplay displayed an echo area message and that message
12428 is no longer requested, we clear the echo area or bring back the
12429 mini-buffer if that is in use. */
12430
12431 void
12432 redisplay (void)
12433 {
12434 redisplay_internal ();
12435 }
12436
12437
12438 static Lisp_Object
12439 overlay_arrow_string_or_property (Lisp_Object var)
12440 {
12441 Lisp_Object val;
12442
12443 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12444 return val;
12445
12446 return Voverlay_arrow_string;
12447 }
12448
12449 /* Return 1 if there are any overlay-arrows in current_buffer. */
12450 static int
12451 overlay_arrow_in_current_buffer_p (void)
12452 {
12453 Lisp_Object vlist;
12454
12455 for (vlist = Voverlay_arrow_variable_list;
12456 CONSP (vlist);
12457 vlist = XCDR (vlist))
12458 {
12459 Lisp_Object var = XCAR (vlist);
12460 Lisp_Object val;
12461
12462 if (!SYMBOLP (var))
12463 continue;
12464 val = find_symbol_value (var);
12465 if (MARKERP (val)
12466 && current_buffer == XMARKER (val)->buffer)
12467 return 1;
12468 }
12469 return 0;
12470 }
12471
12472
12473 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12474 has changed. */
12475
12476 static int
12477 overlay_arrows_changed_p (void)
12478 {
12479 Lisp_Object vlist;
12480
12481 for (vlist = Voverlay_arrow_variable_list;
12482 CONSP (vlist);
12483 vlist = XCDR (vlist))
12484 {
12485 Lisp_Object var = XCAR (vlist);
12486 Lisp_Object val, pstr;
12487
12488 if (!SYMBOLP (var))
12489 continue;
12490 val = find_symbol_value (var);
12491 if (!MARKERP (val))
12492 continue;
12493 if (! EQ (COERCE_MARKER (val),
12494 Fget (var, Qlast_arrow_position))
12495 || ! (pstr = overlay_arrow_string_or_property (var),
12496 EQ (pstr, Fget (var, Qlast_arrow_string))))
12497 return 1;
12498 }
12499 return 0;
12500 }
12501
12502 /* Mark overlay arrows to be updated on next redisplay. */
12503
12504 static void
12505 update_overlay_arrows (int up_to_date)
12506 {
12507 Lisp_Object vlist;
12508
12509 for (vlist = Voverlay_arrow_variable_list;
12510 CONSP (vlist);
12511 vlist = XCDR (vlist))
12512 {
12513 Lisp_Object var = XCAR (vlist);
12514
12515 if (!SYMBOLP (var))
12516 continue;
12517
12518 if (up_to_date > 0)
12519 {
12520 Lisp_Object val = find_symbol_value (var);
12521 Fput (var, Qlast_arrow_position,
12522 COERCE_MARKER (val));
12523 Fput (var, Qlast_arrow_string,
12524 overlay_arrow_string_or_property (var));
12525 }
12526 else if (up_to_date < 0
12527 || !NILP (Fget (var, Qlast_arrow_position)))
12528 {
12529 Fput (var, Qlast_arrow_position, Qt);
12530 Fput (var, Qlast_arrow_string, Qt);
12531 }
12532 }
12533 }
12534
12535
12536 /* Return overlay arrow string to display at row.
12537 Return integer (bitmap number) for arrow bitmap in left fringe.
12538 Return nil if no overlay arrow. */
12539
12540 static Lisp_Object
12541 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12542 {
12543 Lisp_Object vlist;
12544
12545 for (vlist = Voverlay_arrow_variable_list;
12546 CONSP (vlist);
12547 vlist = XCDR (vlist))
12548 {
12549 Lisp_Object var = XCAR (vlist);
12550 Lisp_Object val;
12551
12552 if (!SYMBOLP (var))
12553 continue;
12554
12555 val = find_symbol_value (var);
12556
12557 if (MARKERP (val)
12558 && current_buffer == XMARKER (val)->buffer
12559 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12560 {
12561 if (FRAME_WINDOW_P (it->f)
12562 /* FIXME: if ROW->reversed_p is set, this should test
12563 the right fringe, not the left one. */
12564 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12565 {
12566 #ifdef HAVE_WINDOW_SYSTEM
12567 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12568 {
12569 int fringe_bitmap;
12570 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12571 return make_number (fringe_bitmap);
12572 }
12573 #endif
12574 return make_number (-1); /* Use default arrow bitmap */
12575 }
12576 return overlay_arrow_string_or_property (var);
12577 }
12578 }
12579
12580 return Qnil;
12581 }
12582
12583 /* Return 1 if point moved out of or into a composition. Otherwise
12584 return 0. PREV_BUF and PREV_PT are the last point buffer and
12585 position. BUF and PT are the current point buffer and position. */
12586
12587 static int
12588 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
12589 struct buffer *buf, EMACS_INT pt)
12590 {
12591 EMACS_INT start, end;
12592 Lisp_Object prop;
12593 Lisp_Object buffer;
12594
12595 XSETBUFFER (buffer, buf);
12596 /* Check a composition at the last point if point moved within the
12597 same buffer. */
12598 if (prev_buf == buf)
12599 {
12600 if (prev_pt == pt)
12601 /* Point didn't move. */
12602 return 0;
12603
12604 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12605 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12606 && COMPOSITION_VALID_P (start, end, prop)
12607 && start < prev_pt && end > prev_pt)
12608 /* The last point was within the composition. Return 1 iff
12609 point moved out of the composition. */
12610 return (pt <= start || pt >= end);
12611 }
12612
12613 /* Check a composition at the current point. */
12614 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12615 && find_composition (pt, -1, &start, &end, &prop, buffer)
12616 && COMPOSITION_VALID_P (start, end, prop)
12617 && start < pt && end > pt);
12618 }
12619
12620
12621 /* Reconsider the setting of B->clip_changed which is displayed
12622 in window W. */
12623
12624 static inline void
12625 reconsider_clip_changes (struct window *w, struct buffer *b)
12626 {
12627 if (b->clip_changed
12628 && !NILP (w->window_end_valid)
12629 && w->current_matrix->buffer == b
12630 && w->current_matrix->zv == BUF_ZV (b)
12631 && w->current_matrix->begv == BUF_BEGV (b))
12632 b->clip_changed = 0;
12633
12634 /* If display wasn't paused, and W is not a tool bar window, see if
12635 point has been moved into or out of a composition. In that case,
12636 we set b->clip_changed to 1 to force updating the screen. If
12637 b->clip_changed has already been set to 1, we can skip this
12638 check. */
12639 if (!b->clip_changed
12640 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12641 {
12642 EMACS_INT pt;
12643
12644 if (w == XWINDOW (selected_window))
12645 pt = PT;
12646 else
12647 pt = marker_position (w->pointm);
12648
12649 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12650 || pt != XINT (w->last_point))
12651 && check_point_in_composition (w->current_matrix->buffer,
12652 XINT (w->last_point),
12653 XBUFFER (w->buffer), pt))
12654 b->clip_changed = 1;
12655 }
12656 }
12657 \f
12658
12659 /* Select FRAME to forward the values of frame-local variables into C
12660 variables so that the redisplay routines can access those values
12661 directly. */
12662
12663 static void
12664 select_frame_for_redisplay (Lisp_Object frame)
12665 {
12666 Lisp_Object tail, tem;
12667 Lisp_Object old = selected_frame;
12668 struct Lisp_Symbol *sym;
12669
12670 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12671
12672 selected_frame = frame;
12673
12674 do {
12675 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12676 if (CONSP (XCAR (tail))
12677 && (tem = XCAR (XCAR (tail)),
12678 SYMBOLP (tem))
12679 && (sym = indirect_variable (XSYMBOL (tem)),
12680 sym->redirect == SYMBOL_LOCALIZED)
12681 && sym->val.blv->frame_local)
12682 /* Use find_symbol_value rather than Fsymbol_value
12683 to avoid an error if it is void. */
12684 find_symbol_value (tem);
12685 } while (!EQ (frame, old) && (frame = old, 1));
12686 }
12687
12688
12689 #define STOP_POLLING \
12690 do { if (! polling_stopped_here) stop_polling (); \
12691 polling_stopped_here = 1; } while (0)
12692
12693 #define RESUME_POLLING \
12694 do { if (polling_stopped_here) start_polling (); \
12695 polling_stopped_here = 0; } while (0)
12696
12697
12698 /* Perhaps in the future avoid recentering windows if it
12699 is not necessary; currently that causes some problems. */
12700
12701 static void
12702 redisplay_internal (void)
12703 {
12704 struct window *w = XWINDOW (selected_window);
12705 struct window *sw;
12706 struct frame *fr;
12707 int pending;
12708 int must_finish = 0;
12709 struct text_pos tlbufpos, tlendpos;
12710 int number_of_visible_frames;
12711 int count, count1;
12712 struct frame *sf;
12713 int polling_stopped_here = 0;
12714 Lisp_Object old_frame = selected_frame;
12715
12716 /* Non-zero means redisplay has to consider all windows on all
12717 frames. Zero means, only selected_window is considered. */
12718 int consider_all_windows_p;
12719
12720 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12721
12722 /* No redisplay if running in batch mode or frame is not yet fully
12723 initialized, or redisplay is explicitly turned off by setting
12724 Vinhibit_redisplay. */
12725 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12726 || !NILP (Vinhibit_redisplay))
12727 return;
12728
12729 /* Don't examine these until after testing Vinhibit_redisplay.
12730 When Emacs is shutting down, perhaps because its connection to
12731 X has dropped, we should not look at them at all. */
12732 fr = XFRAME (w->frame);
12733 sf = SELECTED_FRAME ();
12734
12735 if (!fr->glyphs_initialized_p)
12736 return;
12737
12738 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12739 if (popup_activated ())
12740 return;
12741 #endif
12742
12743 /* I don't think this happens but let's be paranoid. */
12744 if (redisplaying_p)
12745 return;
12746
12747 /* Record a function that resets redisplaying_p to its old value
12748 when we leave this function. */
12749 count = SPECPDL_INDEX ();
12750 record_unwind_protect (unwind_redisplay,
12751 Fcons (make_number (redisplaying_p), selected_frame));
12752 ++redisplaying_p;
12753 specbind (Qinhibit_free_realized_faces, Qnil);
12754
12755 {
12756 Lisp_Object tail, frame;
12757
12758 FOR_EACH_FRAME (tail, frame)
12759 {
12760 struct frame *f = XFRAME (frame);
12761 f->already_hscrolled_p = 0;
12762 }
12763 }
12764
12765 retry:
12766 /* Remember the currently selected window. */
12767 sw = w;
12768
12769 if (!EQ (old_frame, selected_frame)
12770 && FRAME_LIVE_P (XFRAME (old_frame)))
12771 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12772 selected_frame and selected_window to be temporarily out-of-sync so
12773 when we come back here via `goto retry', we need to resync because we
12774 may need to run Elisp code (via prepare_menu_bars). */
12775 select_frame_for_redisplay (old_frame);
12776
12777 pending = 0;
12778 reconsider_clip_changes (w, current_buffer);
12779 last_escape_glyph_frame = NULL;
12780 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12781 last_glyphless_glyph_frame = NULL;
12782 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12783
12784 /* If new fonts have been loaded that make a glyph matrix adjustment
12785 necessary, do it. */
12786 if (fonts_changed_p)
12787 {
12788 adjust_glyphs (NULL);
12789 ++windows_or_buffers_changed;
12790 fonts_changed_p = 0;
12791 }
12792
12793 /* If face_change_count is non-zero, init_iterator will free all
12794 realized faces, which includes the faces referenced from current
12795 matrices. So, we can't reuse current matrices in this case. */
12796 if (face_change_count)
12797 ++windows_or_buffers_changed;
12798
12799 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12800 && FRAME_TTY (sf)->previous_frame != sf)
12801 {
12802 /* Since frames on a single ASCII terminal share the same
12803 display area, displaying a different frame means redisplay
12804 the whole thing. */
12805 windows_or_buffers_changed++;
12806 SET_FRAME_GARBAGED (sf);
12807 #ifndef DOS_NT
12808 set_tty_color_mode (FRAME_TTY (sf), sf);
12809 #endif
12810 FRAME_TTY (sf)->previous_frame = sf;
12811 }
12812
12813 /* Set the visible flags for all frames. Do this before checking
12814 for resized or garbaged frames; they want to know if their frames
12815 are visible. See the comment in frame.h for
12816 FRAME_SAMPLE_VISIBILITY. */
12817 {
12818 Lisp_Object tail, frame;
12819
12820 number_of_visible_frames = 0;
12821
12822 FOR_EACH_FRAME (tail, frame)
12823 {
12824 struct frame *f = XFRAME (frame);
12825
12826 FRAME_SAMPLE_VISIBILITY (f);
12827 if (FRAME_VISIBLE_P (f))
12828 ++number_of_visible_frames;
12829 clear_desired_matrices (f);
12830 }
12831 }
12832
12833 /* Notice any pending interrupt request to change frame size. */
12834 do_pending_window_change (1);
12835
12836 /* do_pending_window_change could change the selected_window due to
12837 frame resizing which makes the selected window too small. */
12838 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12839 {
12840 sw = w;
12841 reconsider_clip_changes (w, current_buffer);
12842 }
12843
12844 /* Clear frames marked as garbaged. */
12845 if (frame_garbaged)
12846 clear_garbaged_frames ();
12847
12848 /* Build menubar and tool-bar items. */
12849 if (NILP (Vmemory_full))
12850 prepare_menu_bars ();
12851
12852 if (windows_or_buffers_changed)
12853 update_mode_lines++;
12854
12855 /* Detect case that we need to write or remove a star in the mode line. */
12856 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12857 {
12858 w->update_mode_line = Qt;
12859 if (buffer_shared > 1)
12860 update_mode_lines++;
12861 }
12862
12863 /* Avoid invocation of point motion hooks by `current_column' below. */
12864 count1 = SPECPDL_INDEX ();
12865 specbind (Qinhibit_point_motion_hooks, Qt);
12866
12867 /* If %c is in the mode line, update it if needed. */
12868 if (!NILP (w->column_number_displayed)
12869 /* This alternative quickly identifies a common case
12870 where no change is needed. */
12871 && !(PT == XFASTINT (w->last_point)
12872 && XFASTINT (w->last_modified) >= MODIFF
12873 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12874 && (XFASTINT (w->column_number_displayed) != current_column ()))
12875 w->update_mode_line = Qt;
12876
12877 unbind_to (count1, Qnil);
12878
12879 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12880
12881 /* The variable buffer_shared is set in redisplay_window and
12882 indicates that we redisplay a buffer in different windows. See
12883 there. */
12884 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12885 || cursor_type_changed);
12886
12887 /* If specs for an arrow have changed, do thorough redisplay
12888 to ensure we remove any arrow that should no longer exist. */
12889 if (overlay_arrows_changed_p ())
12890 consider_all_windows_p = windows_or_buffers_changed = 1;
12891
12892 /* Normally the message* functions will have already displayed and
12893 updated the echo area, but the frame may have been trashed, or
12894 the update may have been preempted, so display the echo area
12895 again here. Checking message_cleared_p captures the case that
12896 the echo area should be cleared. */
12897 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12898 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12899 || (message_cleared_p
12900 && minibuf_level == 0
12901 /* If the mini-window is currently selected, this means the
12902 echo-area doesn't show through. */
12903 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12904 {
12905 int window_height_changed_p = echo_area_display (0);
12906 must_finish = 1;
12907
12908 /* If we don't display the current message, don't clear the
12909 message_cleared_p flag, because, if we did, we wouldn't clear
12910 the echo area in the next redisplay which doesn't preserve
12911 the echo area. */
12912 if (!display_last_displayed_message_p)
12913 message_cleared_p = 0;
12914
12915 if (fonts_changed_p)
12916 goto retry;
12917 else if (window_height_changed_p)
12918 {
12919 consider_all_windows_p = 1;
12920 ++update_mode_lines;
12921 ++windows_or_buffers_changed;
12922
12923 /* If window configuration was changed, frames may have been
12924 marked garbaged. Clear them or we will experience
12925 surprises wrt scrolling. */
12926 if (frame_garbaged)
12927 clear_garbaged_frames ();
12928 }
12929 }
12930 else if (EQ (selected_window, minibuf_window)
12931 && (current_buffer->clip_changed
12932 || XFASTINT (w->last_modified) < MODIFF
12933 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12934 && resize_mini_window (w, 0))
12935 {
12936 /* Resized active mini-window to fit the size of what it is
12937 showing if its contents might have changed. */
12938 must_finish = 1;
12939 /* FIXME: this causes all frames to be updated, which seems unnecessary
12940 since only the current frame needs to be considered. This function needs
12941 to be rewritten with two variables, consider_all_windows and
12942 consider_all_frames. */
12943 consider_all_windows_p = 1;
12944 ++windows_or_buffers_changed;
12945 ++update_mode_lines;
12946
12947 /* If window configuration was changed, frames may have been
12948 marked garbaged. Clear them or we will experience
12949 surprises wrt scrolling. */
12950 if (frame_garbaged)
12951 clear_garbaged_frames ();
12952 }
12953
12954
12955 /* If showing the region, and mark has changed, we must redisplay
12956 the whole window. The assignment to this_line_start_pos prevents
12957 the optimization directly below this if-statement. */
12958 if (((!NILP (Vtransient_mark_mode)
12959 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12960 != !NILP (w->region_showing))
12961 || (!NILP (w->region_showing)
12962 && !EQ (w->region_showing,
12963 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12964 CHARPOS (this_line_start_pos) = 0;
12965
12966 /* Optimize the case that only the line containing the cursor in the
12967 selected window has changed. Variables starting with this_ are
12968 set in display_line and record information about the line
12969 containing the cursor. */
12970 tlbufpos = this_line_start_pos;
12971 tlendpos = this_line_end_pos;
12972 if (!consider_all_windows_p
12973 && CHARPOS (tlbufpos) > 0
12974 && NILP (w->update_mode_line)
12975 && !current_buffer->clip_changed
12976 && !current_buffer->prevent_redisplay_optimizations_p
12977 && FRAME_VISIBLE_P (XFRAME (w->frame))
12978 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12979 /* Make sure recorded data applies to current buffer, etc. */
12980 && this_line_buffer == current_buffer
12981 && current_buffer == XBUFFER (w->buffer)
12982 && NILP (w->force_start)
12983 && NILP (w->optional_new_start)
12984 /* Point must be on the line that we have info recorded about. */
12985 && PT >= CHARPOS (tlbufpos)
12986 && PT <= Z - CHARPOS (tlendpos)
12987 /* All text outside that line, including its final newline,
12988 must be unchanged. */
12989 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
12990 CHARPOS (tlendpos)))
12991 {
12992 if (CHARPOS (tlbufpos) > BEGV
12993 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
12994 && (CHARPOS (tlbufpos) == ZV
12995 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
12996 /* Former continuation line has disappeared by becoming empty. */
12997 goto cancel;
12998 else if (XFASTINT (w->last_modified) < MODIFF
12999 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
13000 || MINI_WINDOW_P (w))
13001 {
13002 /* We have to handle the case of continuation around a
13003 wide-column character (see the comment in indent.c around
13004 line 1340).
13005
13006 For instance, in the following case:
13007
13008 -------- Insert --------
13009 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13010 J_I_ ==> J_I_ `^^' are cursors.
13011 ^^ ^^
13012 -------- --------
13013
13014 As we have to redraw the line above, we cannot use this
13015 optimization. */
13016
13017 struct it it;
13018 int line_height_before = this_line_pixel_height;
13019
13020 /* Note that start_display will handle the case that the
13021 line starting at tlbufpos is a continuation line. */
13022 start_display (&it, w, tlbufpos);
13023
13024 /* Implementation note: It this still necessary? */
13025 if (it.current_x != this_line_start_x)
13026 goto cancel;
13027
13028 TRACE ((stderr, "trying display optimization 1\n"));
13029 w->cursor.vpos = -1;
13030 overlay_arrow_seen = 0;
13031 it.vpos = this_line_vpos;
13032 it.current_y = this_line_y;
13033 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13034 display_line (&it);
13035
13036 /* If line contains point, is not continued,
13037 and ends at same distance from eob as before, we win. */
13038 if (w->cursor.vpos >= 0
13039 /* Line is not continued, otherwise this_line_start_pos
13040 would have been set to 0 in display_line. */
13041 && CHARPOS (this_line_start_pos)
13042 /* Line ends as before. */
13043 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13044 /* Line has same height as before. Otherwise other lines
13045 would have to be shifted up or down. */
13046 && this_line_pixel_height == line_height_before)
13047 {
13048 /* If this is not the window's last line, we must adjust
13049 the charstarts of the lines below. */
13050 if (it.current_y < it.last_visible_y)
13051 {
13052 struct glyph_row *row
13053 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13054 EMACS_INT delta, delta_bytes;
13055
13056 /* We used to distinguish between two cases here,
13057 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13058 when the line ends in a newline or the end of the
13059 buffer's accessible portion. But both cases did
13060 the same, so they were collapsed. */
13061 delta = (Z
13062 - CHARPOS (tlendpos)
13063 - MATRIX_ROW_START_CHARPOS (row));
13064 delta_bytes = (Z_BYTE
13065 - BYTEPOS (tlendpos)
13066 - MATRIX_ROW_START_BYTEPOS (row));
13067
13068 increment_matrix_positions (w->current_matrix,
13069 this_line_vpos + 1,
13070 w->current_matrix->nrows,
13071 delta, delta_bytes);
13072 }
13073
13074 /* If this row displays text now but previously didn't,
13075 or vice versa, w->window_end_vpos may have to be
13076 adjusted. */
13077 if ((it.glyph_row - 1)->displays_text_p)
13078 {
13079 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13080 XSETINT (w->window_end_vpos, this_line_vpos);
13081 }
13082 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13083 && this_line_vpos > 0)
13084 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13085 w->window_end_valid = Qnil;
13086
13087 /* Update hint: No need to try to scroll in update_window. */
13088 w->desired_matrix->no_scrolling_p = 1;
13089
13090 #if GLYPH_DEBUG
13091 *w->desired_matrix->method = 0;
13092 debug_method_add (w, "optimization 1");
13093 #endif
13094 #ifdef HAVE_WINDOW_SYSTEM
13095 update_window_fringes (w, 0);
13096 #endif
13097 goto update;
13098 }
13099 else
13100 goto cancel;
13101 }
13102 else if (/* Cursor position hasn't changed. */
13103 PT == XFASTINT (w->last_point)
13104 /* Make sure the cursor was last displayed
13105 in this window. Otherwise we have to reposition it. */
13106 && 0 <= w->cursor.vpos
13107 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13108 {
13109 if (!must_finish)
13110 {
13111 do_pending_window_change (1);
13112 /* If selected_window changed, redisplay again. */
13113 if (WINDOWP (selected_window)
13114 && (w = XWINDOW (selected_window)) != sw)
13115 goto retry;
13116
13117 /* We used to always goto end_of_redisplay here, but this
13118 isn't enough if we have a blinking cursor. */
13119 if (w->cursor_off_p == w->last_cursor_off_p)
13120 goto end_of_redisplay;
13121 }
13122 goto update;
13123 }
13124 /* If highlighting the region, or if the cursor is in the echo area,
13125 then we can't just move the cursor. */
13126 else if (! (!NILP (Vtransient_mark_mode)
13127 && !NILP (BVAR (current_buffer, mark_active)))
13128 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
13129 || highlight_nonselected_windows)
13130 && NILP (w->region_showing)
13131 && NILP (Vshow_trailing_whitespace)
13132 && !cursor_in_echo_area)
13133 {
13134 struct it it;
13135 struct glyph_row *row;
13136
13137 /* Skip from tlbufpos to PT and see where it is. Note that
13138 PT may be in invisible text. If so, we will end at the
13139 next visible position. */
13140 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13141 NULL, DEFAULT_FACE_ID);
13142 it.current_x = this_line_start_x;
13143 it.current_y = this_line_y;
13144 it.vpos = this_line_vpos;
13145
13146 /* The call to move_it_to stops in front of PT, but
13147 moves over before-strings. */
13148 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13149
13150 if (it.vpos == this_line_vpos
13151 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13152 row->enabled_p))
13153 {
13154 xassert (this_line_vpos == it.vpos);
13155 xassert (this_line_y == it.current_y);
13156 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13157 #if GLYPH_DEBUG
13158 *w->desired_matrix->method = 0;
13159 debug_method_add (w, "optimization 3");
13160 #endif
13161 goto update;
13162 }
13163 else
13164 goto cancel;
13165 }
13166
13167 cancel:
13168 /* Text changed drastically or point moved off of line. */
13169 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13170 }
13171
13172 CHARPOS (this_line_start_pos) = 0;
13173 consider_all_windows_p |= buffer_shared > 1;
13174 ++clear_face_cache_count;
13175 #ifdef HAVE_WINDOW_SYSTEM
13176 ++clear_image_cache_count;
13177 #endif
13178
13179 /* Build desired matrices, and update the display. If
13180 consider_all_windows_p is non-zero, do it for all windows on all
13181 frames. Otherwise do it for selected_window, only. */
13182
13183 if (consider_all_windows_p)
13184 {
13185 Lisp_Object tail, frame;
13186
13187 FOR_EACH_FRAME (tail, frame)
13188 XFRAME (frame)->updated_p = 0;
13189
13190 /* Recompute # windows showing selected buffer. This will be
13191 incremented each time such a window is displayed. */
13192 buffer_shared = 0;
13193
13194 FOR_EACH_FRAME (tail, frame)
13195 {
13196 struct frame *f = XFRAME (frame);
13197
13198 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13199 {
13200 if (! EQ (frame, selected_frame))
13201 /* Select the frame, for the sake of frame-local
13202 variables. */
13203 select_frame_for_redisplay (frame);
13204
13205 /* Mark all the scroll bars to be removed; we'll redeem
13206 the ones we want when we redisplay their windows. */
13207 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13208 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13209
13210 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13211 redisplay_windows (FRAME_ROOT_WINDOW (f));
13212
13213 /* The X error handler may have deleted that frame. */
13214 if (!FRAME_LIVE_P (f))
13215 continue;
13216
13217 /* Any scroll bars which redisplay_windows should have
13218 nuked should now go away. */
13219 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13220 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13221
13222 /* If fonts changed, display again. */
13223 /* ??? rms: I suspect it is a mistake to jump all the way
13224 back to retry here. It should just retry this frame. */
13225 if (fonts_changed_p)
13226 goto retry;
13227
13228 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13229 {
13230 /* See if we have to hscroll. */
13231 if (!f->already_hscrolled_p)
13232 {
13233 f->already_hscrolled_p = 1;
13234 if (hscroll_windows (f->root_window))
13235 goto retry;
13236 }
13237
13238 /* Prevent various kinds of signals during display
13239 update. stdio is not robust about handling
13240 signals, which can cause an apparent I/O
13241 error. */
13242 if (interrupt_input)
13243 unrequest_sigio ();
13244 STOP_POLLING;
13245
13246 /* Update the display. */
13247 set_window_update_flags (XWINDOW (f->root_window), 1);
13248 pending |= update_frame (f, 0, 0);
13249 f->updated_p = 1;
13250 }
13251 }
13252 }
13253
13254 if (!EQ (old_frame, selected_frame)
13255 && FRAME_LIVE_P (XFRAME (old_frame)))
13256 /* We played a bit fast-and-loose above and allowed selected_frame
13257 and selected_window to be temporarily out-of-sync but let's make
13258 sure this stays contained. */
13259 select_frame_for_redisplay (old_frame);
13260 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13261
13262 if (!pending)
13263 {
13264 /* Do the mark_window_display_accurate after all windows have
13265 been redisplayed because this call resets flags in buffers
13266 which are needed for proper redisplay. */
13267 FOR_EACH_FRAME (tail, frame)
13268 {
13269 struct frame *f = XFRAME (frame);
13270 if (f->updated_p)
13271 {
13272 mark_window_display_accurate (f->root_window, 1);
13273 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13274 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13275 }
13276 }
13277 }
13278 }
13279 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13280 {
13281 Lisp_Object mini_window;
13282 struct frame *mini_frame;
13283
13284 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13285 /* Use list_of_error, not Qerror, so that
13286 we catch only errors and don't run the debugger. */
13287 internal_condition_case_1 (redisplay_window_1, selected_window,
13288 list_of_error,
13289 redisplay_window_error);
13290
13291 /* Compare desired and current matrices, perform output. */
13292
13293 update:
13294 /* If fonts changed, display again. */
13295 if (fonts_changed_p)
13296 goto retry;
13297
13298 /* Prevent various kinds of signals during display update.
13299 stdio is not robust about handling signals,
13300 which can cause an apparent I/O error. */
13301 if (interrupt_input)
13302 unrequest_sigio ();
13303 STOP_POLLING;
13304
13305 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13306 {
13307 if (hscroll_windows (selected_window))
13308 goto retry;
13309
13310 XWINDOW (selected_window)->must_be_updated_p = 1;
13311 pending = update_frame (sf, 0, 0);
13312 }
13313
13314 /* We may have called echo_area_display at the top of this
13315 function. If the echo area is on another frame, that may
13316 have put text on a frame other than the selected one, so the
13317 above call to update_frame would not have caught it. Catch
13318 it here. */
13319 mini_window = FRAME_MINIBUF_WINDOW (sf);
13320 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13321
13322 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13323 {
13324 XWINDOW (mini_window)->must_be_updated_p = 1;
13325 pending |= update_frame (mini_frame, 0, 0);
13326 if (!pending && hscroll_windows (mini_window))
13327 goto retry;
13328 }
13329 }
13330
13331 /* If display was paused because of pending input, make sure we do a
13332 thorough update the next time. */
13333 if (pending)
13334 {
13335 /* Prevent the optimization at the beginning of
13336 redisplay_internal that tries a single-line update of the
13337 line containing the cursor in the selected window. */
13338 CHARPOS (this_line_start_pos) = 0;
13339
13340 /* Let the overlay arrow be updated the next time. */
13341 update_overlay_arrows (0);
13342
13343 /* If we pause after scrolling, some rows in the current
13344 matrices of some windows are not valid. */
13345 if (!WINDOW_FULL_WIDTH_P (w)
13346 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13347 update_mode_lines = 1;
13348 }
13349 else
13350 {
13351 if (!consider_all_windows_p)
13352 {
13353 /* This has already been done above if
13354 consider_all_windows_p is set. */
13355 mark_window_display_accurate_1 (w, 1);
13356
13357 /* Say overlay arrows are up to date. */
13358 update_overlay_arrows (1);
13359
13360 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13361 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13362 }
13363
13364 update_mode_lines = 0;
13365 windows_or_buffers_changed = 0;
13366 cursor_type_changed = 0;
13367 }
13368
13369 /* Start SIGIO interrupts coming again. Having them off during the
13370 code above makes it less likely one will discard output, but not
13371 impossible, since there might be stuff in the system buffer here.
13372 But it is much hairier to try to do anything about that. */
13373 if (interrupt_input)
13374 request_sigio ();
13375 RESUME_POLLING;
13376
13377 /* If a frame has become visible which was not before, redisplay
13378 again, so that we display it. Expose events for such a frame
13379 (which it gets when becoming visible) don't call the parts of
13380 redisplay constructing glyphs, so simply exposing a frame won't
13381 display anything in this case. So, we have to display these
13382 frames here explicitly. */
13383 if (!pending)
13384 {
13385 Lisp_Object tail, frame;
13386 int new_count = 0;
13387
13388 FOR_EACH_FRAME (tail, frame)
13389 {
13390 int this_is_visible = 0;
13391
13392 if (XFRAME (frame)->visible)
13393 this_is_visible = 1;
13394 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13395 if (XFRAME (frame)->visible)
13396 this_is_visible = 1;
13397
13398 if (this_is_visible)
13399 new_count++;
13400 }
13401
13402 if (new_count != number_of_visible_frames)
13403 windows_or_buffers_changed++;
13404 }
13405
13406 /* Change frame size now if a change is pending. */
13407 do_pending_window_change (1);
13408
13409 /* If we just did a pending size change, or have additional
13410 visible frames, or selected_window changed, redisplay again. */
13411 if ((windows_or_buffers_changed && !pending)
13412 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13413 goto retry;
13414
13415 /* Clear the face and image caches.
13416
13417 We used to do this only if consider_all_windows_p. But the cache
13418 needs to be cleared if a timer creates images in the current
13419 buffer (e.g. the test case in Bug#6230). */
13420
13421 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13422 {
13423 clear_face_cache (0);
13424 clear_face_cache_count = 0;
13425 }
13426
13427 #ifdef HAVE_WINDOW_SYSTEM
13428 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13429 {
13430 clear_image_caches (Qnil);
13431 clear_image_cache_count = 0;
13432 }
13433 #endif /* HAVE_WINDOW_SYSTEM */
13434
13435 end_of_redisplay:
13436 unbind_to (count, Qnil);
13437 RESUME_POLLING;
13438 }
13439
13440
13441 /* Redisplay, but leave alone any recent echo area message unless
13442 another message has been requested in its place.
13443
13444 This is useful in situations where you need to redisplay but no
13445 user action has occurred, making it inappropriate for the message
13446 area to be cleared. See tracking_off and
13447 wait_reading_process_output for examples of these situations.
13448
13449 FROM_WHERE is an integer saying from where this function was
13450 called. This is useful for debugging. */
13451
13452 void
13453 redisplay_preserve_echo_area (int from_where)
13454 {
13455 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13456
13457 if (!NILP (echo_area_buffer[1]))
13458 {
13459 /* We have a previously displayed message, but no current
13460 message. Redisplay the previous message. */
13461 display_last_displayed_message_p = 1;
13462 redisplay_internal ();
13463 display_last_displayed_message_p = 0;
13464 }
13465 else
13466 redisplay_internal ();
13467
13468 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13469 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13470 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13471 }
13472
13473
13474 /* Function registered with record_unwind_protect in
13475 redisplay_internal. Reset redisplaying_p to the value it had
13476 before redisplay_internal was called, and clear
13477 prevent_freeing_realized_faces_p. It also selects the previously
13478 selected frame, unless it has been deleted (by an X connection
13479 failure during redisplay, for example). */
13480
13481 static Lisp_Object
13482 unwind_redisplay (Lisp_Object val)
13483 {
13484 Lisp_Object old_redisplaying_p, old_frame;
13485
13486 old_redisplaying_p = XCAR (val);
13487 redisplaying_p = XFASTINT (old_redisplaying_p);
13488 old_frame = XCDR (val);
13489 if (! EQ (old_frame, selected_frame)
13490 && FRAME_LIVE_P (XFRAME (old_frame)))
13491 select_frame_for_redisplay (old_frame);
13492 return Qnil;
13493 }
13494
13495
13496 /* Mark the display of window W as accurate or inaccurate. If
13497 ACCURATE_P is non-zero mark display of W as accurate. If
13498 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13499 redisplay_internal is called. */
13500
13501 static void
13502 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13503 {
13504 if (BUFFERP (w->buffer))
13505 {
13506 struct buffer *b = XBUFFER (w->buffer);
13507
13508 w->last_modified
13509 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13510 w->last_overlay_modified
13511 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13512 w->last_had_star
13513 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
13514
13515 if (accurate_p)
13516 {
13517 b->clip_changed = 0;
13518 b->prevent_redisplay_optimizations_p = 0;
13519
13520 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13521 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13522 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13523 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13524
13525 w->current_matrix->buffer = b;
13526 w->current_matrix->begv = BUF_BEGV (b);
13527 w->current_matrix->zv = BUF_ZV (b);
13528
13529 w->last_cursor = w->cursor;
13530 w->last_cursor_off_p = w->cursor_off_p;
13531
13532 if (w == XWINDOW (selected_window))
13533 w->last_point = make_number (BUF_PT (b));
13534 else
13535 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13536 }
13537 }
13538
13539 if (accurate_p)
13540 {
13541 w->window_end_valid = w->buffer;
13542 w->update_mode_line = Qnil;
13543 }
13544 }
13545
13546
13547 /* Mark the display of windows in the window tree rooted at WINDOW as
13548 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13549 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13550 be redisplayed the next time redisplay_internal is called. */
13551
13552 void
13553 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13554 {
13555 struct window *w;
13556
13557 for (; !NILP (window); window = w->next)
13558 {
13559 w = XWINDOW (window);
13560 mark_window_display_accurate_1 (w, accurate_p);
13561
13562 if (!NILP (w->vchild))
13563 mark_window_display_accurate (w->vchild, accurate_p);
13564 if (!NILP (w->hchild))
13565 mark_window_display_accurate (w->hchild, accurate_p);
13566 }
13567
13568 if (accurate_p)
13569 {
13570 update_overlay_arrows (1);
13571 }
13572 else
13573 {
13574 /* Force a thorough redisplay the next time by setting
13575 last_arrow_position and last_arrow_string to t, which is
13576 unequal to any useful value of Voverlay_arrow_... */
13577 update_overlay_arrows (-1);
13578 }
13579 }
13580
13581
13582 /* Return value in display table DP (Lisp_Char_Table *) for character
13583 C. Since a display table doesn't have any parent, we don't have to
13584 follow parent. Do not call this function directly but use the
13585 macro DISP_CHAR_VECTOR. */
13586
13587 Lisp_Object
13588 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13589 {
13590 Lisp_Object val;
13591
13592 if (ASCII_CHAR_P (c))
13593 {
13594 val = dp->ascii;
13595 if (SUB_CHAR_TABLE_P (val))
13596 val = XSUB_CHAR_TABLE (val)->contents[c];
13597 }
13598 else
13599 {
13600 Lisp_Object table;
13601
13602 XSETCHAR_TABLE (table, dp);
13603 val = char_table_ref (table, c);
13604 }
13605 if (NILP (val))
13606 val = dp->defalt;
13607 return val;
13608 }
13609
13610
13611 \f
13612 /***********************************************************************
13613 Window Redisplay
13614 ***********************************************************************/
13615
13616 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13617
13618 static void
13619 redisplay_windows (Lisp_Object window)
13620 {
13621 while (!NILP (window))
13622 {
13623 struct window *w = XWINDOW (window);
13624
13625 if (!NILP (w->hchild))
13626 redisplay_windows (w->hchild);
13627 else if (!NILP (w->vchild))
13628 redisplay_windows (w->vchild);
13629 else if (!NILP (w->buffer))
13630 {
13631 displayed_buffer = XBUFFER (w->buffer);
13632 /* Use list_of_error, not Qerror, so that
13633 we catch only errors and don't run the debugger. */
13634 internal_condition_case_1 (redisplay_window_0, window,
13635 list_of_error,
13636 redisplay_window_error);
13637 }
13638
13639 window = w->next;
13640 }
13641 }
13642
13643 static Lisp_Object
13644 redisplay_window_error (Lisp_Object ignore)
13645 {
13646 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13647 return Qnil;
13648 }
13649
13650 static Lisp_Object
13651 redisplay_window_0 (Lisp_Object window)
13652 {
13653 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13654 redisplay_window (window, 0);
13655 return Qnil;
13656 }
13657
13658 static Lisp_Object
13659 redisplay_window_1 (Lisp_Object window)
13660 {
13661 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13662 redisplay_window (window, 1);
13663 return Qnil;
13664 }
13665 \f
13666
13667 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13668 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13669 which positions recorded in ROW differ from current buffer
13670 positions.
13671
13672 Return 0 if cursor is not on this row, 1 otherwise. */
13673
13674 static int
13675 set_cursor_from_row (struct window *w, struct glyph_row *row,
13676 struct glyph_matrix *matrix,
13677 EMACS_INT delta, EMACS_INT delta_bytes,
13678 int dy, int dvpos)
13679 {
13680 struct glyph *glyph = row->glyphs[TEXT_AREA];
13681 struct glyph *end = glyph + row->used[TEXT_AREA];
13682 struct glyph *cursor = NULL;
13683 /* The last known character position in row. */
13684 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13685 int x = row->x;
13686 EMACS_INT pt_old = PT - delta;
13687 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13688 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13689 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13690 /* A glyph beyond the edge of TEXT_AREA which we should never
13691 touch. */
13692 struct glyph *glyphs_end = end;
13693 /* Non-zero means we've found a match for cursor position, but that
13694 glyph has the avoid_cursor_p flag set. */
13695 int match_with_avoid_cursor = 0;
13696 /* Non-zero means we've seen at least one glyph that came from a
13697 display string. */
13698 int string_seen = 0;
13699 /* Largest and smallest buffer positions seen so far during scan of
13700 glyph row. */
13701 EMACS_INT bpos_max = pos_before;
13702 EMACS_INT bpos_min = pos_after;
13703 /* Last buffer position covered by an overlay string with an integer
13704 `cursor' property. */
13705 EMACS_INT bpos_covered = 0;
13706 /* Non-zero means the display string on which to display the cursor
13707 comes from a text property, not from an overlay. */
13708 int string_from_text_prop = 0;
13709
13710 /* Don't even try doing anything if called for a mode-line or
13711 header-line row, since the rest of the code isn't prepared to
13712 deal with such calamities. */
13713 xassert (!row->mode_line_p);
13714 if (row->mode_line_p)
13715 return 0;
13716
13717 /* Skip over glyphs not having an object at the start and the end of
13718 the row. These are special glyphs like truncation marks on
13719 terminal frames. */
13720 if (row->displays_text_p)
13721 {
13722 if (!row->reversed_p)
13723 {
13724 while (glyph < end
13725 && INTEGERP (glyph->object)
13726 && glyph->charpos < 0)
13727 {
13728 x += glyph->pixel_width;
13729 ++glyph;
13730 }
13731 while (end > glyph
13732 && INTEGERP ((end - 1)->object)
13733 /* CHARPOS is zero for blanks and stretch glyphs
13734 inserted by extend_face_to_end_of_line. */
13735 && (end - 1)->charpos <= 0)
13736 --end;
13737 glyph_before = glyph - 1;
13738 glyph_after = end;
13739 }
13740 else
13741 {
13742 struct glyph *g;
13743
13744 /* If the glyph row is reversed, we need to process it from back
13745 to front, so swap the edge pointers. */
13746 glyphs_end = end = glyph - 1;
13747 glyph += row->used[TEXT_AREA] - 1;
13748
13749 while (glyph > end + 1
13750 && INTEGERP (glyph->object)
13751 && glyph->charpos < 0)
13752 {
13753 --glyph;
13754 x -= glyph->pixel_width;
13755 }
13756 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13757 --glyph;
13758 /* By default, in reversed rows we put the cursor on the
13759 rightmost (first in the reading order) glyph. */
13760 for (g = end + 1; g < glyph; g++)
13761 x += g->pixel_width;
13762 while (end < glyph
13763 && INTEGERP ((end + 1)->object)
13764 && (end + 1)->charpos <= 0)
13765 ++end;
13766 glyph_before = glyph + 1;
13767 glyph_after = end;
13768 }
13769 }
13770 else if (row->reversed_p)
13771 {
13772 /* In R2L rows that don't display text, put the cursor on the
13773 rightmost glyph. Case in point: an empty last line that is
13774 part of an R2L paragraph. */
13775 cursor = end - 1;
13776 /* Avoid placing the cursor on the last glyph of the row, where
13777 on terminal frames we hold the vertical border between
13778 adjacent windows. */
13779 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13780 && !WINDOW_RIGHTMOST_P (w)
13781 && cursor == row->glyphs[LAST_AREA] - 1)
13782 cursor--;
13783 x = -1; /* will be computed below, at label compute_x */
13784 }
13785
13786 /* Step 1: Try to find the glyph whose character position
13787 corresponds to point. If that's not possible, find 2 glyphs
13788 whose character positions are the closest to point, one before
13789 point, the other after it. */
13790 if (!row->reversed_p)
13791 while (/* not marched to end of glyph row */
13792 glyph < end
13793 /* glyph was not inserted by redisplay for internal purposes */
13794 && !INTEGERP (glyph->object))
13795 {
13796 if (BUFFERP (glyph->object))
13797 {
13798 EMACS_INT dpos = glyph->charpos - pt_old;
13799
13800 if (glyph->charpos > bpos_max)
13801 bpos_max = glyph->charpos;
13802 if (glyph->charpos < bpos_min)
13803 bpos_min = glyph->charpos;
13804 if (!glyph->avoid_cursor_p)
13805 {
13806 /* If we hit point, we've found the glyph on which to
13807 display the cursor. */
13808 if (dpos == 0)
13809 {
13810 match_with_avoid_cursor = 0;
13811 break;
13812 }
13813 /* See if we've found a better approximation to
13814 POS_BEFORE or to POS_AFTER. Note that we want the
13815 first (leftmost) glyph of all those that are the
13816 closest from below, and the last (rightmost) of all
13817 those from above. */
13818 if (0 > dpos && dpos > pos_before - pt_old)
13819 {
13820 pos_before = glyph->charpos;
13821 glyph_before = glyph;
13822 }
13823 else if (0 < dpos && dpos <= pos_after - pt_old)
13824 {
13825 pos_after = glyph->charpos;
13826 glyph_after = glyph;
13827 }
13828 }
13829 else if (dpos == 0)
13830 match_with_avoid_cursor = 1;
13831 }
13832 else if (STRINGP (glyph->object))
13833 {
13834 Lisp_Object chprop;
13835 EMACS_INT glyph_pos = glyph->charpos;
13836
13837 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13838 glyph->object);
13839 if (!NILP (chprop))
13840 {
13841 /* If the string came from a `display' text property,
13842 look up the buffer position of that property and
13843 use that position to update bpos_max, as if we
13844 actually saw such a position in one of the row's
13845 glyphs. This helps with supporting integer values
13846 of `cursor' property on the display string in
13847 situations where most or all of the row's buffer
13848 text is completely covered by display properties,
13849 so that no glyph with valid buffer positions is
13850 ever seen in the row. */
13851 EMACS_INT prop_pos =
13852 string_buffer_position_lim (glyph->object, pos_before,
13853 pos_after, 0);
13854
13855 if (prop_pos >= pos_before)
13856 bpos_max = prop_pos - 1;
13857 }
13858 if (INTEGERP (chprop))
13859 {
13860 bpos_covered = bpos_max + XINT (chprop);
13861 /* If the `cursor' property covers buffer positions up
13862 to and including point, we should display cursor on
13863 this glyph. Note that, if a `cursor' property on one
13864 of the string's characters has an integer value, we
13865 will break out of the loop below _before_ we get to
13866 the position match above. IOW, integer values of
13867 the `cursor' property override the "exact match for
13868 point" strategy of positioning the cursor. */
13869 /* Implementation note: bpos_max == pt_old when, e.g.,
13870 we are in an empty line, where bpos_max is set to
13871 MATRIX_ROW_START_CHARPOS, see above. */
13872 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13873 {
13874 cursor = glyph;
13875 break;
13876 }
13877 }
13878
13879 string_seen = 1;
13880 }
13881 x += glyph->pixel_width;
13882 ++glyph;
13883 }
13884 else if (glyph > end) /* row is reversed */
13885 while (!INTEGERP (glyph->object))
13886 {
13887 if (BUFFERP (glyph->object))
13888 {
13889 EMACS_INT dpos = glyph->charpos - pt_old;
13890
13891 if (glyph->charpos > bpos_max)
13892 bpos_max = glyph->charpos;
13893 if (glyph->charpos < bpos_min)
13894 bpos_min = glyph->charpos;
13895 if (!glyph->avoid_cursor_p)
13896 {
13897 if (dpos == 0)
13898 {
13899 match_with_avoid_cursor = 0;
13900 break;
13901 }
13902 if (0 > dpos && dpos > pos_before - pt_old)
13903 {
13904 pos_before = glyph->charpos;
13905 glyph_before = glyph;
13906 }
13907 else if (0 < dpos && dpos <= pos_after - pt_old)
13908 {
13909 pos_after = glyph->charpos;
13910 glyph_after = glyph;
13911 }
13912 }
13913 else if (dpos == 0)
13914 match_with_avoid_cursor = 1;
13915 }
13916 else if (STRINGP (glyph->object))
13917 {
13918 Lisp_Object chprop;
13919 EMACS_INT glyph_pos = glyph->charpos;
13920
13921 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13922 glyph->object);
13923 if (!NILP (chprop))
13924 {
13925 EMACS_INT prop_pos =
13926 string_buffer_position_lim (glyph->object, pos_before,
13927 pos_after, 0);
13928
13929 if (prop_pos >= pos_before)
13930 bpos_max = prop_pos - 1;
13931 }
13932 if (INTEGERP (chprop))
13933 {
13934 bpos_covered = bpos_max + XINT (chprop);
13935 /* If the `cursor' property covers buffer positions up
13936 to and including point, we should display cursor on
13937 this glyph. */
13938 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13939 {
13940 cursor = glyph;
13941 break;
13942 }
13943 }
13944 string_seen = 1;
13945 }
13946 --glyph;
13947 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13948 {
13949 x--; /* can't use any pixel_width */
13950 break;
13951 }
13952 x -= glyph->pixel_width;
13953 }
13954
13955 /* Step 2: If we didn't find an exact match for point, we need to
13956 look for a proper place to put the cursor among glyphs between
13957 GLYPH_BEFORE and GLYPH_AFTER. */
13958 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13959 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13960 && bpos_covered < pt_old)
13961 {
13962 /* An empty line has a single glyph whose OBJECT is zero and
13963 whose CHARPOS is the position of a newline on that line.
13964 Note that on a TTY, there are more glyphs after that, which
13965 were produced by extend_face_to_end_of_line, but their
13966 CHARPOS is zero or negative. */
13967 int empty_line_p =
13968 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13969 && INTEGERP (glyph->object) && glyph->charpos > 0;
13970
13971 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13972 {
13973 EMACS_INT ellipsis_pos;
13974
13975 /* Scan back over the ellipsis glyphs. */
13976 if (!row->reversed_p)
13977 {
13978 ellipsis_pos = (glyph - 1)->charpos;
13979 while (glyph > row->glyphs[TEXT_AREA]
13980 && (glyph - 1)->charpos == ellipsis_pos)
13981 glyph--, x -= glyph->pixel_width;
13982 /* That loop always goes one position too far, including
13983 the glyph before the ellipsis. So scan forward over
13984 that one. */
13985 x += glyph->pixel_width;
13986 glyph++;
13987 }
13988 else /* row is reversed */
13989 {
13990 ellipsis_pos = (glyph + 1)->charpos;
13991 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
13992 && (glyph + 1)->charpos == ellipsis_pos)
13993 glyph++, x += glyph->pixel_width;
13994 x -= glyph->pixel_width;
13995 glyph--;
13996 }
13997 }
13998 else if (match_with_avoid_cursor)
13999 {
14000 cursor = glyph_after;
14001 x = -1;
14002 }
14003 else if (string_seen)
14004 {
14005 int incr = row->reversed_p ? -1 : +1;
14006
14007 /* Need to find the glyph that came out of a string which is
14008 present at point. That glyph is somewhere between
14009 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14010 positioned between POS_BEFORE and POS_AFTER in the
14011 buffer. */
14012 struct glyph *start, *stop;
14013 EMACS_INT pos = pos_before;
14014
14015 x = -1;
14016
14017 /* If the row ends in a newline from a display string,
14018 reordering could have moved the glyphs belonging to the
14019 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14020 in this case we extend the search to the last glyph in
14021 the row that was not inserted by redisplay. */
14022 if (row->ends_in_newline_from_string_p)
14023 {
14024 glyph_after = end;
14025 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14026 }
14027
14028 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14029 correspond to POS_BEFORE and POS_AFTER, respectively. We
14030 need START and STOP in the order that corresponds to the
14031 row's direction as given by its reversed_p flag. If the
14032 directionality of characters between POS_BEFORE and
14033 POS_AFTER is the opposite of the row's base direction,
14034 these characters will have been reordered for display,
14035 and we need to reverse START and STOP. */
14036 if (!row->reversed_p)
14037 {
14038 start = min (glyph_before, glyph_after);
14039 stop = max (glyph_before, glyph_after);
14040 }
14041 else
14042 {
14043 start = max (glyph_before, glyph_after);
14044 stop = min (glyph_before, glyph_after);
14045 }
14046 for (glyph = start + incr;
14047 row->reversed_p ? glyph > stop : glyph < stop; )
14048 {
14049
14050 /* Any glyphs that come from the buffer are here because
14051 of bidi reordering. Skip them, and only pay
14052 attention to glyphs that came from some string. */
14053 if (STRINGP (glyph->object))
14054 {
14055 Lisp_Object str;
14056 EMACS_INT tem;
14057 /* If the display property covers the newline, we
14058 need to search for it one position farther. */
14059 EMACS_INT lim = pos_after
14060 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14061
14062 string_from_text_prop = 0;
14063 str = glyph->object;
14064 tem = string_buffer_position_lim (str, pos, lim, 0);
14065 if (tem == 0 /* from overlay */
14066 || pos <= tem)
14067 {
14068 /* If the string from which this glyph came is
14069 found in the buffer at point, then we've
14070 found the glyph we've been looking for. If
14071 it comes from an overlay (tem == 0), and it
14072 has the `cursor' property on one of its
14073 glyphs, record that glyph as a candidate for
14074 displaying the cursor. (As in the
14075 unidirectional version, we will display the
14076 cursor on the last candidate we find.) */
14077 if (tem == 0 || tem == pt_old)
14078 {
14079 /* The glyphs from this string could have
14080 been reordered. Find the one with the
14081 smallest string position. Or there could
14082 be a character in the string with the
14083 `cursor' property, which means display
14084 cursor on that character's glyph. */
14085 EMACS_INT strpos = glyph->charpos;
14086
14087 if (tem)
14088 {
14089 cursor = glyph;
14090 string_from_text_prop = 1;
14091 }
14092 for ( ;
14093 (row->reversed_p ? glyph > stop : glyph < stop)
14094 && EQ (glyph->object, str);
14095 glyph += incr)
14096 {
14097 Lisp_Object cprop;
14098 EMACS_INT gpos = glyph->charpos;
14099
14100 cprop = Fget_char_property (make_number (gpos),
14101 Qcursor,
14102 glyph->object);
14103 if (!NILP (cprop))
14104 {
14105 cursor = glyph;
14106 break;
14107 }
14108 if (tem && glyph->charpos < strpos)
14109 {
14110 strpos = glyph->charpos;
14111 cursor = glyph;
14112 }
14113 }
14114
14115 if (tem == pt_old)
14116 goto compute_x;
14117 }
14118 if (tem)
14119 pos = tem + 1; /* don't find previous instances */
14120 }
14121 /* This string is not what we want; skip all of the
14122 glyphs that came from it. */
14123 while ((row->reversed_p ? glyph > stop : glyph < stop)
14124 && EQ (glyph->object, str))
14125 glyph += incr;
14126 }
14127 else
14128 glyph += incr;
14129 }
14130
14131 /* If we reached the end of the line, and END was from a string,
14132 the cursor is not on this line. */
14133 if (cursor == NULL
14134 && (row->reversed_p ? glyph <= end : glyph >= end)
14135 && STRINGP (end->object)
14136 && row->continued_p)
14137 return 0;
14138 }
14139 /* A truncated row may not include PT among its character positions.
14140 Setting the cursor inside the scroll margin will trigger
14141 recalculation of hscroll in hscroll_window_tree. But if a
14142 display string covers point, defer to the string-handling
14143 code below to figure this out. */
14144 else if (row->truncated_on_left_p && pt_old < bpos_min)
14145 {
14146 cursor = glyph_before;
14147 x = -1;
14148 }
14149 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14150 /* Zero-width characters produce no glyphs. */
14151 || (!empty_line_p
14152 && (row->reversed_p
14153 ? glyph_after > glyphs_end
14154 : glyph_after < glyphs_end)))
14155 {
14156 cursor = glyph_after;
14157 x = -1;
14158 }
14159 }
14160
14161 compute_x:
14162 if (cursor != NULL)
14163 glyph = cursor;
14164 if (x < 0)
14165 {
14166 struct glyph *g;
14167
14168 /* Need to compute x that corresponds to GLYPH. */
14169 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14170 {
14171 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14172 abort ();
14173 x += g->pixel_width;
14174 }
14175 }
14176
14177 /* ROW could be part of a continued line, which, under bidi
14178 reordering, might have other rows whose start and end charpos
14179 occlude point. Only set w->cursor if we found a better
14180 approximation to the cursor position than we have from previously
14181 examined candidate rows belonging to the same continued line. */
14182 if (/* we already have a candidate row */
14183 w->cursor.vpos >= 0
14184 /* that candidate is not the row we are processing */
14185 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14186 /* Make sure cursor.vpos specifies a row whose start and end
14187 charpos occlude point, and it is valid candidate for being a
14188 cursor-row. This is because some callers of this function
14189 leave cursor.vpos at the row where the cursor was displayed
14190 during the last redisplay cycle. */
14191 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14192 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14193 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14194 {
14195 struct glyph *g1 =
14196 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14197
14198 /* Don't consider glyphs that are outside TEXT_AREA. */
14199 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14200 return 0;
14201 /* Keep the candidate whose buffer position is the closest to
14202 point or has the `cursor' property. */
14203 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14204 w->cursor.hpos >= 0
14205 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14206 && ((BUFFERP (g1->object)
14207 && (g1->charpos == pt_old /* an exact match always wins */
14208 || (BUFFERP (glyph->object)
14209 && eabs (g1->charpos - pt_old)
14210 < eabs (glyph->charpos - pt_old))))
14211 /* previous candidate is a glyph from a string that has
14212 a non-nil `cursor' property */
14213 || (STRINGP (g1->object)
14214 && (!NILP (Fget_char_property (make_number (g1->charpos),
14215 Qcursor, g1->object))
14216 /* previous candidate is from the same display
14217 string as this one, and the display string
14218 came from a text property */
14219 || (EQ (g1->object, glyph->object)
14220 && string_from_text_prop)
14221 /* this candidate is from newline and its
14222 position is not an exact match */
14223 || (INTEGERP (glyph->object)
14224 && glyph->charpos != pt_old)))))
14225 return 0;
14226 /* If this candidate gives an exact match, use that. */
14227 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14228 /* If this candidate is a glyph created for the
14229 terminating newline of a line, and point is on that
14230 newline, it wins because it's an exact match. */
14231 || (!row->continued_p
14232 && INTEGERP (glyph->object)
14233 && glyph->charpos == 0
14234 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14235 /* Otherwise, keep the candidate that comes from a row
14236 spanning less buffer positions. This may win when one or
14237 both candidate positions are on glyphs that came from
14238 display strings, for which we cannot compare buffer
14239 positions. */
14240 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14241 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14242 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14243 return 0;
14244 }
14245 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14246 w->cursor.x = x;
14247 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14248 w->cursor.y = row->y + dy;
14249
14250 if (w == XWINDOW (selected_window))
14251 {
14252 if (!row->continued_p
14253 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14254 && row->x == 0)
14255 {
14256 this_line_buffer = XBUFFER (w->buffer);
14257
14258 CHARPOS (this_line_start_pos)
14259 = MATRIX_ROW_START_CHARPOS (row) + delta;
14260 BYTEPOS (this_line_start_pos)
14261 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14262
14263 CHARPOS (this_line_end_pos)
14264 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14265 BYTEPOS (this_line_end_pos)
14266 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14267
14268 this_line_y = w->cursor.y;
14269 this_line_pixel_height = row->height;
14270 this_line_vpos = w->cursor.vpos;
14271 this_line_start_x = row->x;
14272 }
14273 else
14274 CHARPOS (this_line_start_pos) = 0;
14275 }
14276
14277 return 1;
14278 }
14279
14280
14281 /* Run window scroll functions, if any, for WINDOW with new window
14282 start STARTP. Sets the window start of WINDOW to that position.
14283
14284 We assume that the window's buffer is really current. */
14285
14286 static inline struct text_pos
14287 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14288 {
14289 struct window *w = XWINDOW (window);
14290 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14291
14292 if (current_buffer != XBUFFER (w->buffer))
14293 abort ();
14294
14295 if (!NILP (Vwindow_scroll_functions))
14296 {
14297 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14298 make_number (CHARPOS (startp)));
14299 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14300 /* In case the hook functions switch buffers. */
14301 if (current_buffer != XBUFFER (w->buffer))
14302 set_buffer_internal_1 (XBUFFER (w->buffer));
14303 }
14304
14305 return startp;
14306 }
14307
14308
14309 /* Make sure the line containing the cursor is fully visible.
14310 A value of 1 means there is nothing to be done.
14311 (Either the line is fully visible, or it cannot be made so,
14312 or we cannot tell.)
14313
14314 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14315 is higher than window.
14316
14317 A value of 0 means the caller should do scrolling
14318 as if point had gone off the screen. */
14319
14320 static int
14321 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14322 {
14323 struct glyph_matrix *matrix;
14324 struct glyph_row *row;
14325 int window_height;
14326
14327 if (!make_cursor_line_fully_visible_p)
14328 return 1;
14329
14330 /* It's not always possible to find the cursor, e.g, when a window
14331 is full of overlay strings. Don't do anything in that case. */
14332 if (w->cursor.vpos < 0)
14333 return 1;
14334
14335 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14336 row = MATRIX_ROW (matrix, w->cursor.vpos);
14337
14338 /* If the cursor row is not partially visible, there's nothing to do. */
14339 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14340 return 1;
14341
14342 /* If the row the cursor is in is taller than the window's height,
14343 it's not clear what to do, so do nothing. */
14344 window_height = window_box_height (w);
14345 if (row->height >= window_height)
14346 {
14347 if (!force_p || MINI_WINDOW_P (w)
14348 || w->vscroll || w->cursor.vpos == 0)
14349 return 1;
14350 }
14351 return 0;
14352 }
14353
14354
14355 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14356 non-zero means only WINDOW is redisplayed in redisplay_internal.
14357 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14358 in redisplay_window to bring a partially visible line into view in
14359 the case that only the cursor has moved.
14360
14361 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14362 last screen line's vertical height extends past the end of the screen.
14363
14364 Value is
14365
14366 1 if scrolling succeeded
14367
14368 0 if scrolling didn't find point.
14369
14370 -1 if new fonts have been loaded so that we must interrupt
14371 redisplay, adjust glyph matrices, and try again. */
14372
14373 enum
14374 {
14375 SCROLLING_SUCCESS,
14376 SCROLLING_FAILED,
14377 SCROLLING_NEED_LARGER_MATRICES
14378 };
14379
14380 /* If scroll-conservatively is more than this, never recenter.
14381
14382 If you change this, don't forget to update the doc string of
14383 `scroll-conservatively' and the Emacs manual. */
14384 #define SCROLL_LIMIT 100
14385
14386 static int
14387 try_scrolling (Lisp_Object window, int just_this_one_p,
14388 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
14389 int temp_scroll_step, int last_line_misfit)
14390 {
14391 struct window *w = XWINDOW (window);
14392 struct frame *f = XFRAME (w->frame);
14393 struct text_pos pos, startp;
14394 struct it it;
14395 int this_scroll_margin, scroll_max, rc, height;
14396 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14397 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14398 Lisp_Object aggressive;
14399 /* We will never try scrolling more than this number of lines. */
14400 int scroll_limit = SCROLL_LIMIT;
14401
14402 #if GLYPH_DEBUG
14403 debug_method_add (w, "try_scrolling");
14404 #endif
14405
14406 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14407
14408 /* Compute scroll margin height in pixels. We scroll when point is
14409 within this distance from the top or bottom of the window. */
14410 if (scroll_margin > 0)
14411 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14412 * FRAME_LINE_HEIGHT (f);
14413 else
14414 this_scroll_margin = 0;
14415
14416 /* Force arg_scroll_conservatively to have a reasonable value, to
14417 avoid scrolling too far away with slow move_it_* functions. Note
14418 that the user can supply scroll-conservatively equal to
14419 `most-positive-fixnum', which can be larger than INT_MAX. */
14420 if (arg_scroll_conservatively > scroll_limit)
14421 {
14422 arg_scroll_conservatively = scroll_limit + 1;
14423 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14424 }
14425 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14426 /* Compute how much we should try to scroll maximally to bring
14427 point into view. */
14428 scroll_max = (max (scroll_step,
14429 max (arg_scroll_conservatively, temp_scroll_step))
14430 * FRAME_LINE_HEIGHT (f));
14431 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14432 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14433 /* We're trying to scroll because of aggressive scrolling but no
14434 scroll_step is set. Choose an arbitrary one. */
14435 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14436 else
14437 scroll_max = 0;
14438
14439 too_near_end:
14440
14441 /* Decide whether to scroll down. */
14442 if (PT > CHARPOS (startp))
14443 {
14444 int scroll_margin_y;
14445
14446 /* Compute the pixel ypos of the scroll margin, then move IT to
14447 either that ypos or PT, whichever comes first. */
14448 start_display (&it, w, startp);
14449 scroll_margin_y = it.last_visible_y - this_scroll_margin
14450 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14451 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14452 (MOVE_TO_POS | MOVE_TO_Y));
14453
14454 if (PT > CHARPOS (it.current.pos))
14455 {
14456 int y0 = line_bottom_y (&it);
14457 /* Compute how many pixels below window bottom to stop searching
14458 for PT. This avoids costly search for PT that is far away if
14459 the user limited scrolling by a small number of lines, but
14460 always finds PT if scroll_conservatively is set to a large
14461 number, such as most-positive-fixnum. */
14462 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14463 int y_to_move = it.last_visible_y + slack;
14464
14465 /* Compute the distance from the scroll margin to PT or to
14466 the scroll limit, whichever comes first. This should
14467 include the height of the cursor line, to make that line
14468 fully visible. */
14469 move_it_to (&it, PT, -1, y_to_move,
14470 -1, MOVE_TO_POS | MOVE_TO_Y);
14471 dy = line_bottom_y (&it) - y0;
14472
14473 if (dy > scroll_max)
14474 return SCROLLING_FAILED;
14475
14476 if (dy > 0)
14477 scroll_down_p = 1;
14478 }
14479 }
14480
14481 if (scroll_down_p)
14482 {
14483 /* Point is in or below the bottom scroll margin, so move the
14484 window start down. If scrolling conservatively, move it just
14485 enough down to make point visible. If scroll_step is set,
14486 move it down by scroll_step. */
14487 if (arg_scroll_conservatively)
14488 amount_to_scroll
14489 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14490 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14491 else if (scroll_step || temp_scroll_step)
14492 amount_to_scroll = scroll_max;
14493 else
14494 {
14495 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14496 height = WINDOW_BOX_TEXT_HEIGHT (w);
14497 if (NUMBERP (aggressive))
14498 {
14499 double float_amount = XFLOATINT (aggressive) * height;
14500 amount_to_scroll = float_amount;
14501 if (amount_to_scroll == 0 && float_amount > 0)
14502 amount_to_scroll = 1;
14503 /* Don't let point enter the scroll margin near top of
14504 the window. */
14505 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14506 amount_to_scroll = height - 2*this_scroll_margin + dy;
14507 }
14508 }
14509
14510 if (amount_to_scroll <= 0)
14511 return SCROLLING_FAILED;
14512
14513 start_display (&it, w, startp);
14514 if (arg_scroll_conservatively <= scroll_limit)
14515 move_it_vertically (&it, amount_to_scroll);
14516 else
14517 {
14518 /* Extra precision for users who set scroll-conservatively
14519 to a large number: make sure the amount we scroll
14520 the window start is never less than amount_to_scroll,
14521 which was computed as distance from window bottom to
14522 point. This matters when lines at window top and lines
14523 below window bottom have different height. */
14524 struct it it1;
14525 void *it1data = NULL;
14526 /* We use a temporary it1 because line_bottom_y can modify
14527 its argument, if it moves one line down; see there. */
14528 int start_y;
14529
14530 SAVE_IT (it1, it, it1data);
14531 start_y = line_bottom_y (&it1);
14532 do {
14533 RESTORE_IT (&it, &it, it1data);
14534 move_it_by_lines (&it, 1);
14535 SAVE_IT (it1, it, it1data);
14536 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14537 }
14538
14539 /* If STARTP is unchanged, move it down another screen line. */
14540 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14541 move_it_by_lines (&it, 1);
14542 startp = it.current.pos;
14543 }
14544 else
14545 {
14546 struct text_pos scroll_margin_pos = startp;
14547
14548 /* See if point is inside the scroll margin at the top of the
14549 window. */
14550 if (this_scroll_margin)
14551 {
14552 start_display (&it, w, startp);
14553 move_it_vertically (&it, this_scroll_margin);
14554 scroll_margin_pos = it.current.pos;
14555 }
14556
14557 if (PT < CHARPOS (scroll_margin_pos))
14558 {
14559 /* Point is in the scroll margin at the top of the window or
14560 above what is displayed in the window. */
14561 int y0, y_to_move;
14562
14563 /* Compute the vertical distance from PT to the scroll
14564 margin position. Move as far as scroll_max allows, or
14565 one screenful, or 10 screen lines, whichever is largest.
14566 Give up if distance is greater than scroll_max. */
14567 SET_TEXT_POS (pos, PT, PT_BYTE);
14568 start_display (&it, w, pos);
14569 y0 = it.current_y;
14570 y_to_move = max (it.last_visible_y,
14571 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14572 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14573 y_to_move, -1,
14574 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14575 dy = it.current_y - y0;
14576 if (dy > scroll_max)
14577 return SCROLLING_FAILED;
14578
14579 /* Compute new window start. */
14580 start_display (&it, w, startp);
14581
14582 if (arg_scroll_conservatively)
14583 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14584 max (scroll_step, temp_scroll_step));
14585 else if (scroll_step || temp_scroll_step)
14586 amount_to_scroll = scroll_max;
14587 else
14588 {
14589 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14590 height = WINDOW_BOX_TEXT_HEIGHT (w);
14591 if (NUMBERP (aggressive))
14592 {
14593 double float_amount = XFLOATINT (aggressive) * height;
14594 amount_to_scroll = float_amount;
14595 if (amount_to_scroll == 0 && float_amount > 0)
14596 amount_to_scroll = 1;
14597 amount_to_scroll -=
14598 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14599 /* Don't let point enter the scroll margin near
14600 bottom of the window. */
14601 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14602 amount_to_scroll = height - 2*this_scroll_margin + dy;
14603 }
14604 }
14605
14606 if (amount_to_scroll <= 0)
14607 return SCROLLING_FAILED;
14608
14609 move_it_vertically_backward (&it, amount_to_scroll);
14610 startp = it.current.pos;
14611 }
14612 }
14613
14614 /* Run window scroll functions. */
14615 startp = run_window_scroll_functions (window, startp);
14616
14617 /* Display the window. Give up if new fonts are loaded, or if point
14618 doesn't appear. */
14619 if (!try_window (window, startp, 0))
14620 rc = SCROLLING_NEED_LARGER_MATRICES;
14621 else if (w->cursor.vpos < 0)
14622 {
14623 clear_glyph_matrix (w->desired_matrix);
14624 rc = SCROLLING_FAILED;
14625 }
14626 else
14627 {
14628 /* Maybe forget recorded base line for line number display. */
14629 if (!just_this_one_p
14630 || current_buffer->clip_changed
14631 || BEG_UNCHANGED < CHARPOS (startp))
14632 w->base_line_number = Qnil;
14633
14634 /* If cursor ends up on a partially visible line,
14635 treat that as being off the bottom of the screen. */
14636 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14637 /* It's possible that the cursor is on the first line of the
14638 buffer, which is partially obscured due to a vscroll
14639 (Bug#7537). In that case, avoid looping forever . */
14640 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14641 {
14642 clear_glyph_matrix (w->desired_matrix);
14643 ++extra_scroll_margin_lines;
14644 goto too_near_end;
14645 }
14646 rc = SCROLLING_SUCCESS;
14647 }
14648
14649 return rc;
14650 }
14651
14652
14653 /* Compute a suitable window start for window W if display of W starts
14654 on a continuation line. Value is non-zero if a new window start
14655 was computed.
14656
14657 The new window start will be computed, based on W's width, starting
14658 from the start of the continued line. It is the start of the
14659 screen line with the minimum distance from the old start W->start. */
14660
14661 static int
14662 compute_window_start_on_continuation_line (struct window *w)
14663 {
14664 struct text_pos pos, start_pos;
14665 int window_start_changed_p = 0;
14666
14667 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14668
14669 /* If window start is on a continuation line... Window start may be
14670 < BEGV in case there's invisible text at the start of the
14671 buffer (M-x rmail, for example). */
14672 if (CHARPOS (start_pos) > BEGV
14673 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14674 {
14675 struct it it;
14676 struct glyph_row *row;
14677
14678 /* Handle the case that the window start is out of range. */
14679 if (CHARPOS (start_pos) < BEGV)
14680 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14681 else if (CHARPOS (start_pos) > ZV)
14682 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14683
14684 /* Find the start of the continued line. This should be fast
14685 because scan_buffer is fast (newline cache). */
14686 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14687 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14688 row, DEFAULT_FACE_ID);
14689 reseat_at_previous_visible_line_start (&it);
14690
14691 /* If the line start is "too far" away from the window start,
14692 say it takes too much time to compute a new window start. */
14693 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14694 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14695 {
14696 int min_distance, distance;
14697
14698 /* Move forward by display lines to find the new window
14699 start. If window width was enlarged, the new start can
14700 be expected to be > the old start. If window width was
14701 decreased, the new window start will be < the old start.
14702 So, we're looking for the display line start with the
14703 minimum distance from the old window start. */
14704 pos = it.current.pos;
14705 min_distance = INFINITY;
14706 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14707 distance < min_distance)
14708 {
14709 min_distance = distance;
14710 pos = it.current.pos;
14711 move_it_by_lines (&it, 1);
14712 }
14713
14714 /* Set the window start there. */
14715 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14716 window_start_changed_p = 1;
14717 }
14718 }
14719
14720 return window_start_changed_p;
14721 }
14722
14723
14724 /* Try cursor movement in case text has not changed in window WINDOW,
14725 with window start STARTP. Value is
14726
14727 CURSOR_MOVEMENT_SUCCESS if successful
14728
14729 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14730
14731 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14732 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14733 we want to scroll as if scroll-step were set to 1. See the code.
14734
14735 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14736 which case we have to abort this redisplay, and adjust matrices
14737 first. */
14738
14739 enum
14740 {
14741 CURSOR_MOVEMENT_SUCCESS,
14742 CURSOR_MOVEMENT_CANNOT_BE_USED,
14743 CURSOR_MOVEMENT_MUST_SCROLL,
14744 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14745 };
14746
14747 static int
14748 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14749 {
14750 struct window *w = XWINDOW (window);
14751 struct frame *f = XFRAME (w->frame);
14752 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14753
14754 #if GLYPH_DEBUG
14755 if (inhibit_try_cursor_movement)
14756 return rc;
14757 #endif
14758
14759 /* Handle case where text has not changed, only point, and it has
14760 not moved off the frame. */
14761 if (/* Point may be in this window. */
14762 PT >= CHARPOS (startp)
14763 /* Selective display hasn't changed. */
14764 && !current_buffer->clip_changed
14765 /* Function force-mode-line-update is used to force a thorough
14766 redisplay. It sets either windows_or_buffers_changed or
14767 update_mode_lines. So don't take a shortcut here for these
14768 cases. */
14769 && !update_mode_lines
14770 && !windows_or_buffers_changed
14771 && !cursor_type_changed
14772 /* Can't use this case if highlighting a region. When a
14773 region exists, cursor movement has to do more than just
14774 set the cursor. */
14775 && !(!NILP (Vtransient_mark_mode)
14776 && !NILP (BVAR (current_buffer, mark_active)))
14777 && NILP (w->region_showing)
14778 && NILP (Vshow_trailing_whitespace)
14779 /* Right after splitting windows, last_point may be nil. */
14780 && INTEGERP (w->last_point)
14781 /* This code is not used for mini-buffer for the sake of the case
14782 of redisplaying to replace an echo area message; since in
14783 that case the mini-buffer contents per se are usually
14784 unchanged. This code is of no real use in the mini-buffer
14785 since the handling of this_line_start_pos, etc., in redisplay
14786 handles the same cases. */
14787 && !EQ (window, minibuf_window)
14788 /* When splitting windows or for new windows, it happens that
14789 redisplay is called with a nil window_end_vpos or one being
14790 larger than the window. This should really be fixed in
14791 window.c. I don't have this on my list, now, so we do
14792 approximately the same as the old redisplay code. --gerd. */
14793 && INTEGERP (w->window_end_vpos)
14794 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14795 && (FRAME_WINDOW_P (f)
14796 || !overlay_arrow_in_current_buffer_p ()))
14797 {
14798 int this_scroll_margin, top_scroll_margin;
14799 struct glyph_row *row = NULL;
14800
14801 #if GLYPH_DEBUG
14802 debug_method_add (w, "cursor movement");
14803 #endif
14804
14805 /* Scroll if point within this distance from the top or bottom
14806 of the window. This is a pixel value. */
14807 if (scroll_margin > 0)
14808 {
14809 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14810 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14811 }
14812 else
14813 this_scroll_margin = 0;
14814
14815 top_scroll_margin = this_scroll_margin;
14816 if (WINDOW_WANTS_HEADER_LINE_P (w))
14817 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14818
14819 /* Start with the row the cursor was displayed during the last
14820 not paused redisplay. Give up if that row is not valid. */
14821 if (w->last_cursor.vpos < 0
14822 || w->last_cursor.vpos >= w->current_matrix->nrows)
14823 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14824 else
14825 {
14826 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14827 if (row->mode_line_p)
14828 ++row;
14829 if (!row->enabled_p)
14830 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14831 }
14832
14833 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14834 {
14835 int scroll_p = 0, must_scroll = 0;
14836 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14837
14838 if (PT > XFASTINT (w->last_point))
14839 {
14840 /* Point has moved forward. */
14841 while (MATRIX_ROW_END_CHARPOS (row) < PT
14842 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14843 {
14844 xassert (row->enabled_p);
14845 ++row;
14846 }
14847
14848 /* If the end position of a row equals the start
14849 position of the next row, and PT is at that position,
14850 we would rather display cursor in the next line. */
14851 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14852 && MATRIX_ROW_END_CHARPOS (row) == PT
14853 && row < w->current_matrix->rows
14854 + w->current_matrix->nrows - 1
14855 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14856 && !cursor_row_p (row))
14857 ++row;
14858
14859 /* If within the scroll margin, scroll. Note that
14860 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14861 the next line would be drawn, and that
14862 this_scroll_margin can be zero. */
14863 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14864 || PT > MATRIX_ROW_END_CHARPOS (row)
14865 /* Line is completely visible last line in window
14866 and PT is to be set in the next line. */
14867 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14868 && PT == MATRIX_ROW_END_CHARPOS (row)
14869 && !row->ends_at_zv_p
14870 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14871 scroll_p = 1;
14872 }
14873 else if (PT < XFASTINT (w->last_point))
14874 {
14875 /* Cursor has to be moved backward. Note that PT >=
14876 CHARPOS (startp) because of the outer if-statement. */
14877 while (!row->mode_line_p
14878 && (MATRIX_ROW_START_CHARPOS (row) > PT
14879 || (MATRIX_ROW_START_CHARPOS (row) == PT
14880 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14881 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14882 row > w->current_matrix->rows
14883 && (row-1)->ends_in_newline_from_string_p))))
14884 && (row->y > top_scroll_margin
14885 || CHARPOS (startp) == BEGV))
14886 {
14887 xassert (row->enabled_p);
14888 --row;
14889 }
14890
14891 /* Consider the following case: Window starts at BEGV,
14892 there is invisible, intangible text at BEGV, so that
14893 display starts at some point START > BEGV. It can
14894 happen that we are called with PT somewhere between
14895 BEGV and START. Try to handle that case. */
14896 if (row < w->current_matrix->rows
14897 || row->mode_line_p)
14898 {
14899 row = w->current_matrix->rows;
14900 if (row->mode_line_p)
14901 ++row;
14902 }
14903
14904 /* Due to newlines in overlay strings, we may have to
14905 skip forward over overlay strings. */
14906 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14907 && MATRIX_ROW_END_CHARPOS (row) == PT
14908 && !cursor_row_p (row))
14909 ++row;
14910
14911 /* If within the scroll margin, scroll. */
14912 if (row->y < top_scroll_margin
14913 && CHARPOS (startp) != BEGV)
14914 scroll_p = 1;
14915 }
14916 else
14917 {
14918 /* Cursor did not move. So don't scroll even if cursor line
14919 is partially visible, as it was so before. */
14920 rc = CURSOR_MOVEMENT_SUCCESS;
14921 }
14922
14923 if (PT < MATRIX_ROW_START_CHARPOS (row)
14924 || PT > MATRIX_ROW_END_CHARPOS (row))
14925 {
14926 /* if PT is not in the glyph row, give up. */
14927 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14928 must_scroll = 1;
14929 }
14930 else if (rc != CURSOR_MOVEMENT_SUCCESS
14931 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14932 {
14933 struct glyph_row *row1;
14934
14935 /* If rows are bidi-reordered and point moved, back up
14936 until we find a row that does not belong to a
14937 continuation line. This is because we must consider
14938 all rows of a continued line as candidates for the
14939 new cursor positioning, since row start and end
14940 positions change non-linearly with vertical position
14941 in such rows. */
14942 /* FIXME: Revisit this when glyph ``spilling'' in
14943 continuation lines' rows is implemented for
14944 bidi-reordered rows. */
14945 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
14946 MATRIX_ROW_CONTINUATION_LINE_P (row);
14947 --row)
14948 {
14949 /* If we hit the beginning of the displayed portion
14950 without finding the first row of a continued
14951 line, give up. */
14952 if (row <= row1)
14953 {
14954 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14955 break;
14956 }
14957 xassert (row->enabled_p);
14958 }
14959 }
14960 if (must_scroll)
14961 ;
14962 else if (rc != CURSOR_MOVEMENT_SUCCESS
14963 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
14964 /* Make sure this isn't a header line by any chance, since
14965 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
14966 && !row->mode_line_p
14967 && make_cursor_line_fully_visible_p)
14968 {
14969 if (PT == MATRIX_ROW_END_CHARPOS (row)
14970 && !row->ends_at_zv_p
14971 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14972 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14973 else if (row->height > window_box_height (w))
14974 {
14975 /* If we end up in a partially visible line, let's
14976 make it fully visible, except when it's taller
14977 than the window, in which case we can't do much
14978 about it. */
14979 *scroll_step = 1;
14980 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14981 }
14982 else
14983 {
14984 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14985 if (!cursor_row_fully_visible_p (w, 0, 1))
14986 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14987 else
14988 rc = CURSOR_MOVEMENT_SUCCESS;
14989 }
14990 }
14991 else if (scroll_p)
14992 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14993 else if (rc != CURSOR_MOVEMENT_SUCCESS
14994 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14995 {
14996 /* With bidi-reordered rows, there could be more than
14997 one candidate row whose start and end positions
14998 occlude point. We need to let set_cursor_from_row
14999 find the best candidate. */
15000 /* FIXME: Revisit this when glyph ``spilling'' in
15001 continuation lines' rows is implemented for
15002 bidi-reordered rows. */
15003 int rv = 0;
15004
15005 do
15006 {
15007 int at_zv_p = 0, exact_match_p = 0;
15008
15009 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15010 && PT <= MATRIX_ROW_END_CHARPOS (row)
15011 && cursor_row_p (row))
15012 rv |= set_cursor_from_row (w, row, w->current_matrix,
15013 0, 0, 0, 0);
15014 /* As soon as we've found the exact match for point,
15015 or the first suitable row whose ends_at_zv_p flag
15016 is set, we are done. */
15017 at_zv_p =
15018 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15019 if (rv && !at_zv_p
15020 && w->cursor.hpos >= 0
15021 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15022 w->cursor.vpos))
15023 {
15024 struct glyph_row *candidate =
15025 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15026 struct glyph *g =
15027 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15028 EMACS_INT endpos = MATRIX_ROW_END_CHARPOS (candidate);
15029
15030 exact_match_p =
15031 (BUFFERP (g->object) && g->charpos == PT)
15032 || (INTEGERP (g->object)
15033 && (g->charpos == PT
15034 || (g->charpos == 0 && endpos - 1 == PT)));
15035 }
15036 if (rv && (at_zv_p || exact_match_p))
15037 {
15038 rc = CURSOR_MOVEMENT_SUCCESS;
15039 break;
15040 }
15041 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15042 break;
15043 ++row;
15044 }
15045 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15046 || row->continued_p)
15047 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15048 || (MATRIX_ROW_START_CHARPOS (row) == PT
15049 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15050 /* If we didn't find any candidate rows, or exited the
15051 loop before all the candidates were examined, signal
15052 to the caller that this method failed. */
15053 if (rc != CURSOR_MOVEMENT_SUCCESS
15054 && !(rv
15055 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15056 && !row->continued_p))
15057 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15058 else if (rv)
15059 rc = CURSOR_MOVEMENT_SUCCESS;
15060 }
15061 else
15062 {
15063 do
15064 {
15065 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15066 {
15067 rc = CURSOR_MOVEMENT_SUCCESS;
15068 break;
15069 }
15070 ++row;
15071 }
15072 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15073 && MATRIX_ROW_START_CHARPOS (row) == PT
15074 && cursor_row_p (row));
15075 }
15076 }
15077 }
15078
15079 return rc;
15080 }
15081
15082 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15083 static
15084 #endif
15085 void
15086 set_vertical_scroll_bar (struct window *w)
15087 {
15088 EMACS_INT start, end, whole;
15089
15090 /* Calculate the start and end positions for the current window.
15091 At some point, it would be nice to choose between scrollbars
15092 which reflect the whole buffer size, with special markers
15093 indicating narrowing, and scrollbars which reflect only the
15094 visible region.
15095
15096 Note that mini-buffers sometimes aren't displaying any text. */
15097 if (!MINI_WINDOW_P (w)
15098 || (w == XWINDOW (minibuf_window)
15099 && NILP (echo_area_buffer[0])))
15100 {
15101 struct buffer *buf = XBUFFER (w->buffer);
15102 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15103 start = marker_position (w->start) - BUF_BEGV (buf);
15104 /* I don't think this is guaranteed to be right. For the
15105 moment, we'll pretend it is. */
15106 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15107
15108 if (end < start)
15109 end = start;
15110 if (whole < (end - start))
15111 whole = end - start;
15112 }
15113 else
15114 start = end = whole = 0;
15115
15116 /* Indicate what this scroll bar ought to be displaying now. */
15117 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15118 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15119 (w, end - start, whole, start);
15120 }
15121
15122
15123 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15124 selected_window is redisplayed.
15125
15126 We can return without actually redisplaying the window if
15127 fonts_changed_p is nonzero. In that case, redisplay_internal will
15128 retry. */
15129
15130 static void
15131 redisplay_window (Lisp_Object window, int just_this_one_p)
15132 {
15133 struct window *w = XWINDOW (window);
15134 struct frame *f = XFRAME (w->frame);
15135 struct buffer *buffer = XBUFFER (w->buffer);
15136 struct buffer *old = current_buffer;
15137 struct text_pos lpoint, opoint, startp;
15138 int update_mode_line;
15139 int tem;
15140 struct it it;
15141 /* Record it now because it's overwritten. */
15142 int current_matrix_up_to_date_p = 0;
15143 int used_current_matrix_p = 0;
15144 /* This is less strict than current_matrix_up_to_date_p.
15145 It indicates that the buffer contents and narrowing are unchanged. */
15146 int buffer_unchanged_p = 0;
15147 int temp_scroll_step = 0;
15148 int count = SPECPDL_INDEX ();
15149 int rc;
15150 int centering_position = -1;
15151 int last_line_misfit = 0;
15152 EMACS_INT beg_unchanged, end_unchanged;
15153
15154 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15155 opoint = lpoint;
15156
15157 /* W must be a leaf window here. */
15158 xassert (!NILP (w->buffer));
15159 #if GLYPH_DEBUG
15160 *w->desired_matrix->method = 0;
15161 #endif
15162
15163 restart:
15164 reconsider_clip_changes (w, buffer);
15165
15166 /* Has the mode line to be updated? */
15167 update_mode_line = (!NILP (w->update_mode_line)
15168 || update_mode_lines
15169 || buffer->clip_changed
15170 || buffer->prevent_redisplay_optimizations_p);
15171
15172 if (MINI_WINDOW_P (w))
15173 {
15174 if (w == XWINDOW (echo_area_window)
15175 && !NILP (echo_area_buffer[0]))
15176 {
15177 if (update_mode_line)
15178 /* We may have to update a tty frame's menu bar or a
15179 tool-bar. Example `M-x C-h C-h C-g'. */
15180 goto finish_menu_bars;
15181 else
15182 /* We've already displayed the echo area glyphs in this window. */
15183 goto finish_scroll_bars;
15184 }
15185 else if ((w != XWINDOW (minibuf_window)
15186 || minibuf_level == 0)
15187 /* When buffer is nonempty, redisplay window normally. */
15188 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15189 /* Quail displays non-mini buffers in minibuffer window.
15190 In that case, redisplay the window normally. */
15191 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15192 {
15193 /* W is a mini-buffer window, but it's not active, so clear
15194 it. */
15195 int yb = window_text_bottom_y (w);
15196 struct glyph_row *row;
15197 int y;
15198
15199 for (y = 0, row = w->desired_matrix->rows;
15200 y < yb;
15201 y += row->height, ++row)
15202 blank_row (w, row, y);
15203 goto finish_scroll_bars;
15204 }
15205
15206 clear_glyph_matrix (w->desired_matrix);
15207 }
15208
15209 /* Otherwise set up data on this window; select its buffer and point
15210 value. */
15211 /* Really select the buffer, for the sake of buffer-local
15212 variables. */
15213 set_buffer_internal_1 (XBUFFER (w->buffer));
15214
15215 current_matrix_up_to_date_p
15216 = (!NILP (w->window_end_valid)
15217 && !current_buffer->clip_changed
15218 && !current_buffer->prevent_redisplay_optimizations_p
15219 && XFASTINT (w->last_modified) >= MODIFF
15220 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15221
15222 /* Run the window-bottom-change-functions
15223 if it is possible that the text on the screen has changed
15224 (either due to modification of the text, or any other reason). */
15225 if (!current_matrix_up_to_date_p
15226 && !NILP (Vwindow_text_change_functions))
15227 {
15228 safe_run_hooks (Qwindow_text_change_functions);
15229 goto restart;
15230 }
15231
15232 beg_unchanged = BEG_UNCHANGED;
15233 end_unchanged = END_UNCHANGED;
15234
15235 SET_TEXT_POS (opoint, PT, PT_BYTE);
15236
15237 specbind (Qinhibit_point_motion_hooks, Qt);
15238
15239 buffer_unchanged_p
15240 = (!NILP (w->window_end_valid)
15241 && !current_buffer->clip_changed
15242 && XFASTINT (w->last_modified) >= MODIFF
15243 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15244
15245 /* When windows_or_buffers_changed is non-zero, we can't rely on
15246 the window end being valid, so set it to nil there. */
15247 if (windows_or_buffers_changed)
15248 {
15249 /* If window starts on a continuation line, maybe adjust the
15250 window start in case the window's width changed. */
15251 if (XMARKER (w->start)->buffer == current_buffer)
15252 compute_window_start_on_continuation_line (w);
15253
15254 w->window_end_valid = Qnil;
15255 }
15256
15257 /* Some sanity checks. */
15258 CHECK_WINDOW_END (w);
15259 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15260 abort ();
15261 if (BYTEPOS (opoint) < CHARPOS (opoint))
15262 abort ();
15263
15264 /* If %c is in mode line, update it if needed. */
15265 if (!NILP (w->column_number_displayed)
15266 /* This alternative quickly identifies a common case
15267 where no change is needed. */
15268 && !(PT == XFASTINT (w->last_point)
15269 && XFASTINT (w->last_modified) >= MODIFF
15270 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
15271 && (XFASTINT (w->column_number_displayed) != current_column ()))
15272 update_mode_line = 1;
15273
15274 /* Count number of windows showing the selected buffer. An indirect
15275 buffer counts as its base buffer. */
15276 if (!just_this_one_p)
15277 {
15278 struct buffer *current_base, *window_base;
15279 current_base = current_buffer;
15280 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15281 if (current_base->base_buffer)
15282 current_base = current_base->base_buffer;
15283 if (window_base->base_buffer)
15284 window_base = window_base->base_buffer;
15285 if (current_base == window_base)
15286 buffer_shared++;
15287 }
15288
15289 /* Point refers normally to the selected window. For any other
15290 window, set up appropriate value. */
15291 if (!EQ (window, selected_window))
15292 {
15293 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
15294 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
15295 if (new_pt < BEGV)
15296 {
15297 new_pt = BEGV;
15298 new_pt_byte = BEGV_BYTE;
15299 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15300 }
15301 else if (new_pt > (ZV - 1))
15302 {
15303 new_pt = ZV;
15304 new_pt_byte = ZV_BYTE;
15305 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15306 }
15307
15308 /* We don't use SET_PT so that the point-motion hooks don't run. */
15309 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15310 }
15311
15312 /* If any of the character widths specified in the display table
15313 have changed, invalidate the width run cache. It's true that
15314 this may be a bit late to catch such changes, but the rest of
15315 redisplay goes (non-fatally) haywire when the display table is
15316 changed, so why should we worry about doing any better? */
15317 if (current_buffer->width_run_cache)
15318 {
15319 struct Lisp_Char_Table *disptab = buffer_display_table ();
15320
15321 if (! disptab_matches_widthtab (disptab,
15322 XVECTOR (BVAR (current_buffer, width_table))))
15323 {
15324 invalidate_region_cache (current_buffer,
15325 current_buffer->width_run_cache,
15326 BEG, Z);
15327 recompute_width_table (current_buffer, disptab);
15328 }
15329 }
15330
15331 /* If window-start is screwed up, choose a new one. */
15332 if (XMARKER (w->start)->buffer != current_buffer)
15333 goto recenter;
15334
15335 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15336
15337 /* If someone specified a new starting point but did not insist,
15338 check whether it can be used. */
15339 if (!NILP (w->optional_new_start)
15340 && CHARPOS (startp) >= BEGV
15341 && CHARPOS (startp) <= ZV)
15342 {
15343 w->optional_new_start = Qnil;
15344 start_display (&it, w, startp);
15345 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15346 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15347 if (IT_CHARPOS (it) == PT)
15348 w->force_start = Qt;
15349 /* IT may overshoot PT if text at PT is invisible. */
15350 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15351 w->force_start = Qt;
15352 }
15353
15354 force_start:
15355
15356 /* Handle case where place to start displaying has been specified,
15357 unless the specified location is outside the accessible range. */
15358 if (!NILP (w->force_start)
15359 || w->frozen_window_start_p)
15360 {
15361 /* We set this later on if we have to adjust point. */
15362 int new_vpos = -1;
15363
15364 w->force_start = Qnil;
15365 w->vscroll = 0;
15366 w->window_end_valid = Qnil;
15367
15368 /* Forget any recorded base line for line number display. */
15369 if (!buffer_unchanged_p)
15370 w->base_line_number = Qnil;
15371
15372 /* Redisplay the mode line. Select the buffer properly for that.
15373 Also, run the hook window-scroll-functions
15374 because we have scrolled. */
15375 /* Note, we do this after clearing force_start because
15376 if there's an error, it is better to forget about force_start
15377 than to get into an infinite loop calling the hook functions
15378 and having them get more errors. */
15379 if (!update_mode_line
15380 || ! NILP (Vwindow_scroll_functions))
15381 {
15382 update_mode_line = 1;
15383 w->update_mode_line = Qt;
15384 startp = run_window_scroll_functions (window, startp);
15385 }
15386
15387 w->last_modified = make_number (0);
15388 w->last_overlay_modified = make_number (0);
15389 if (CHARPOS (startp) < BEGV)
15390 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15391 else if (CHARPOS (startp) > ZV)
15392 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15393
15394 /* Redisplay, then check if cursor has been set during the
15395 redisplay. Give up if new fonts were loaded. */
15396 /* We used to issue a CHECK_MARGINS argument to try_window here,
15397 but this causes scrolling to fail when point begins inside
15398 the scroll margin (bug#148) -- cyd */
15399 if (!try_window (window, startp, 0))
15400 {
15401 w->force_start = Qt;
15402 clear_glyph_matrix (w->desired_matrix);
15403 goto need_larger_matrices;
15404 }
15405
15406 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15407 {
15408 /* If point does not appear, try to move point so it does
15409 appear. The desired matrix has been built above, so we
15410 can use it here. */
15411 new_vpos = window_box_height (w) / 2;
15412 }
15413
15414 if (!cursor_row_fully_visible_p (w, 0, 0))
15415 {
15416 /* Point does appear, but on a line partly visible at end of window.
15417 Move it back to a fully-visible line. */
15418 new_vpos = window_box_height (w);
15419 }
15420
15421 /* If we need to move point for either of the above reasons,
15422 now actually do it. */
15423 if (new_vpos >= 0)
15424 {
15425 struct glyph_row *row;
15426
15427 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15428 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15429 ++row;
15430
15431 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15432 MATRIX_ROW_START_BYTEPOS (row));
15433
15434 if (w != XWINDOW (selected_window))
15435 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15436 else if (current_buffer == old)
15437 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15438
15439 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15440
15441 /* If we are highlighting the region, then we just changed
15442 the region, so redisplay to show it. */
15443 if (!NILP (Vtransient_mark_mode)
15444 && !NILP (BVAR (current_buffer, mark_active)))
15445 {
15446 clear_glyph_matrix (w->desired_matrix);
15447 if (!try_window (window, startp, 0))
15448 goto need_larger_matrices;
15449 }
15450 }
15451
15452 #if GLYPH_DEBUG
15453 debug_method_add (w, "forced window start");
15454 #endif
15455 goto done;
15456 }
15457
15458 /* Handle case where text has not changed, only point, and it has
15459 not moved off the frame, and we are not retrying after hscroll.
15460 (current_matrix_up_to_date_p is nonzero when retrying.) */
15461 if (current_matrix_up_to_date_p
15462 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15463 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15464 {
15465 switch (rc)
15466 {
15467 case CURSOR_MOVEMENT_SUCCESS:
15468 used_current_matrix_p = 1;
15469 goto done;
15470
15471 case CURSOR_MOVEMENT_MUST_SCROLL:
15472 goto try_to_scroll;
15473
15474 default:
15475 abort ();
15476 }
15477 }
15478 /* If current starting point was originally the beginning of a line
15479 but no longer is, find a new starting point. */
15480 else if (!NILP (w->start_at_line_beg)
15481 && !(CHARPOS (startp) <= BEGV
15482 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15483 {
15484 #if GLYPH_DEBUG
15485 debug_method_add (w, "recenter 1");
15486 #endif
15487 goto recenter;
15488 }
15489
15490 /* Try scrolling with try_window_id. Value is > 0 if update has
15491 been done, it is -1 if we know that the same window start will
15492 not work. It is 0 if unsuccessful for some other reason. */
15493 else if ((tem = try_window_id (w)) != 0)
15494 {
15495 #if GLYPH_DEBUG
15496 debug_method_add (w, "try_window_id %d", tem);
15497 #endif
15498
15499 if (fonts_changed_p)
15500 goto need_larger_matrices;
15501 if (tem > 0)
15502 goto done;
15503
15504 /* Otherwise try_window_id has returned -1 which means that we
15505 don't want the alternative below this comment to execute. */
15506 }
15507 else if (CHARPOS (startp) >= BEGV
15508 && CHARPOS (startp) <= ZV
15509 && PT >= CHARPOS (startp)
15510 && (CHARPOS (startp) < ZV
15511 /* Avoid starting at end of buffer. */
15512 || CHARPOS (startp) == BEGV
15513 || (XFASTINT (w->last_modified) >= MODIFF
15514 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15515 {
15516 int d1, d2, d3, d4, d5, d6;
15517
15518 /* If first window line is a continuation line, and window start
15519 is inside the modified region, but the first change is before
15520 current window start, we must select a new window start.
15521
15522 However, if this is the result of a down-mouse event (e.g. by
15523 extending the mouse-drag-overlay), we don't want to select a
15524 new window start, since that would change the position under
15525 the mouse, resulting in an unwanted mouse-movement rather
15526 than a simple mouse-click. */
15527 if (NILP (w->start_at_line_beg)
15528 && NILP (do_mouse_tracking)
15529 && CHARPOS (startp) > BEGV
15530 && CHARPOS (startp) > BEG + beg_unchanged
15531 && CHARPOS (startp) <= Z - end_unchanged
15532 /* Even if w->start_at_line_beg is nil, a new window may
15533 start at a line_beg, since that's how set_buffer_window
15534 sets it. So, we need to check the return value of
15535 compute_window_start_on_continuation_line. (See also
15536 bug#197). */
15537 && XMARKER (w->start)->buffer == current_buffer
15538 && compute_window_start_on_continuation_line (w)
15539 /* It doesn't make sense to force the window start like we
15540 do at label force_start if it is already known that point
15541 will not be visible in the resulting window, because
15542 doing so will move point from its correct position
15543 instead of scrolling the window to bring point into view.
15544 See bug#9324. */
15545 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15546 {
15547 w->force_start = Qt;
15548 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15549 goto force_start;
15550 }
15551
15552 #if GLYPH_DEBUG
15553 debug_method_add (w, "same window start");
15554 #endif
15555
15556 /* Try to redisplay starting at same place as before.
15557 If point has not moved off frame, accept the results. */
15558 if (!current_matrix_up_to_date_p
15559 /* Don't use try_window_reusing_current_matrix in this case
15560 because a window scroll function can have changed the
15561 buffer. */
15562 || !NILP (Vwindow_scroll_functions)
15563 || MINI_WINDOW_P (w)
15564 || !(used_current_matrix_p
15565 = try_window_reusing_current_matrix (w)))
15566 {
15567 IF_DEBUG (debug_method_add (w, "1"));
15568 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15569 /* -1 means we need to scroll.
15570 0 means we need new matrices, but fonts_changed_p
15571 is set in that case, so we will detect it below. */
15572 goto try_to_scroll;
15573 }
15574
15575 if (fonts_changed_p)
15576 goto need_larger_matrices;
15577
15578 if (w->cursor.vpos >= 0)
15579 {
15580 if (!just_this_one_p
15581 || current_buffer->clip_changed
15582 || BEG_UNCHANGED < CHARPOS (startp))
15583 /* Forget any recorded base line for line number display. */
15584 w->base_line_number = Qnil;
15585
15586 if (!cursor_row_fully_visible_p (w, 1, 0))
15587 {
15588 clear_glyph_matrix (w->desired_matrix);
15589 last_line_misfit = 1;
15590 }
15591 /* Drop through and scroll. */
15592 else
15593 goto done;
15594 }
15595 else
15596 clear_glyph_matrix (w->desired_matrix);
15597 }
15598
15599 try_to_scroll:
15600
15601 w->last_modified = make_number (0);
15602 w->last_overlay_modified = make_number (0);
15603
15604 /* Redisplay the mode line. Select the buffer properly for that. */
15605 if (!update_mode_line)
15606 {
15607 update_mode_line = 1;
15608 w->update_mode_line = Qt;
15609 }
15610
15611 /* Try to scroll by specified few lines. */
15612 if ((scroll_conservatively
15613 || emacs_scroll_step
15614 || temp_scroll_step
15615 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15616 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15617 && CHARPOS (startp) >= BEGV
15618 && CHARPOS (startp) <= ZV)
15619 {
15620 /* The function returns -1 if new fonts were loaded, 1 if
15621 successful, 0 if not successful. */
15622 int ss = try_scrolling (window, just_this_one_p,
15623 scroll_conservatively,
15624 emacs_scroll_step,
15625 temp_scroll_step, last_line_misfit);
15626 switch (ss)
15627 {
15628 case SCROLLING_SUCCESS:
15629 goto done;
15630
15631 case SCROLLING_NEED_LARGER_MATRICES:
15632 goto need_larger_matrices;
15633
15634 case SCROLLING_FAILED:
15635 break;
15636
15637 default:
15638 abort ();
15639 }
15640 }
15641
15642 /* Finally, just choose a place to start which positions point
15643 according to user preferences. */
15644
15645 recenter:
15646
15647 #if GLYPH_DEBUG
15648 debug_method_add (w, "recenter");
15649 #endif
15650
15651 /* w->vscroll = 0; */
15652
15653 /* Forget any previously recorded base line for line number display. */
15654 if (!buffer_unchanged_p)
15655 w->base_line_number = Qnil;
15656
15657 /* Determine the window start relative to point. */
15658 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15659 it.current_y = it.last_visible_y;
15660 if (centering_position < 0)
15661 {
15662 int margin =
15663 scroll_margin > 0
15664 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15665 : 0;
15666 EMACS_INT margin_pos = CHARPOS (startp);
15667 Lisp_Object aggressive;
15668 int scrolling_up;
15669
15670 /* If there is a scroll margin at the top of the window, find
15671 its character position. */
15672 if (margin
15673 /* Cannot call start_display if startp is not in the
15674 accessible region of the buffer. This can happen when we
15675 have just switched to a different buffer and/or changed
15676 its restriction. In that case, startp is initialized to
15677 the character position 1 (BEGV) because we did not yet
15678 have chance to display the buffer even once. */
15679 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15680 {
15681 struct it it1;
15682 void *it1data = NULL;
15683
15684 SAVE_IT (it1, it, it1data);
15685 start_display (&it1, w, startp);
15686 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15687 margin_pos = IT_CHARPOS (it1);
15688 RESTORE_IT (&it, &it, it1data);
15689 }
15690 scrolling_up = PT > margin_pos;
15691 aggressive =
15692 scrolling_up
15693 ? BVAR (current_buffer, scroll_up_aggressively)
15694 : BVAR (current_buffer, scroll_down_aggressively);
15695
15696 if (!MINI_WINDOW_P (w)
15697 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15698 {
15699 int pt_offset = 0;
15700
15701 /* Setting scroll-conservatively overrides
15702 scroll-*-aggressively. */
15703 if (!scroll_conservatively && NUMBERP (aggressive))
15704 {
15705 double float_amount = XFLOATINT (aggressive);
15706
15707 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15708 if (pt_offset == 0 && float_amount > 0)
15709 pt_offset = 1;
15710 if (pt_offset && margin > 0)
15711 margin -= 1;
15712 }
15713 /* Compute how much to move the window start backward from
15714 point so that point will be displayed where the user
15715 wants it. */
15716 if (scrolling_up)
15717 {
15718 centering_position = it.last_visible_y;
15719 if (pt_offset)
15720 centering_position -= pt_offset;
15721 centering_position -=
15722 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15723 + WINDOW_HEADER_LINE_HEIGHT (w);
15724 /* Don't let point enter the scroll margin near top of
15725 the window. */
15726 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15727 centering_position = margin * FRAME_LINE_HEIGHT (f);
15728 }
15729 else
15730 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15731 }
15732 else
15733 /* Set the window start half the height of the window backward
15734 from point. */
15735 centering_position = window_box_height (w) / 2;
15736 }
15737 move_it_vertically_backward (&it, centering_position);
15738
15739 xassert (IT_CHARPOS (it) >= BEGV);
15740
15741 /* The function move_it_vertically_backward may move over more
15742 than the specified y-distance. If it->w is small, e.g. a
15743 mini-buffer window, we may end up in front of the window's
15744 display area. Start displaying at the start of the line
15745 containing PT in this case. */
15746 if (it.current_y <= 0)
15747 {
15748 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15749 move_it_vertically_backward (&it, 0);
15750 it.current_y = 0;
15751 }
15752
15753 it.current_x = it.hpos = 0;
15754
15755 /* Set the window start position here explicitly, to avoid an
15756 infinite loop in case the functions in window-scroll-functions
15757 get errors. */
15758 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15759
15760 /* Run scroll hooks. */
15761 startp = run_window_scroll_functions (window, it.current.pos);
15762
15763 /* Redisplay the window. */
15764 if (!current_matrix_up_to_date_p
15765 || windows_or_buffers_changed
15766 || cursor_type_changed
15767 /* Don't use try_window_reusing_current_matrix in this case
15768 because it can have changed the buffer. */
15769 || !NILP (Vwindow_scroll_functions)
15770 || !just_this_one_p
15771 || MINI_WINDOW_P (w)
15772 || !(used_current_matrix_p
15773 = try_window_reusing_current_matrix (w)))
15774 try_window (window, startp, 0);
15775
15776 /* If new fonts have been loaded (due to fontsets), give up. We
15777 have to start a new redisplay since we need to re-adjust glyph
15778 matrices. */
15779 if (fonts_changed_p)
15780 goto need_larger_matrices;
15781
15782 /* If cursor did not appear assume that the middle of the window is
15783 in the first line of the window. Do it again with the next line.
15784 (Imagine a window of height 100, displaying two lines of height
15785 60. Moving back 50 from it->last_visible_y will end in the first
15786 line.) */
15787 if (w->cursor.vpos < 0)
15788 {
15789 if (!NILP (w->window_end_valid)
15790 && PT >= Z - XFASTINT (w->window_end_pos))
15791 {
15792 clear_glyph_matrix (w->desired_matrix);
15793 move_it_by_lines (&it, 1);
15794 try_window (window, it.current.pos, 0);
15795 }
15796 else if (PT < IT_CHARPOS (it))
15797 {
15798 clear_glyph_matrix (w->desired_matrix);
15799 move_it_by_lines (&it, -1);
15800 try_window (window, it.current.pos, 0);
15801 }
15802 else
15803 {
15804 /* Not much we can do about it. */
15805 }
15806 }
15807
15808 /* Consider the following case: Window starts at BEGV, there is
15809 invisible, intangible text at BEGV, so that display starts at
15810 some point START > BEGV. It can happen that we are called with
15811 PT somewhere between BEGV and START. Try to handle that case. */
15812 if (w->cursor.vpos < 0)
15813 {
15814 struct glyph_row *row = w->current_matrix->rows;
15815 if (row->mode_line_p)
15816 ++row;
15817 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15818 }
15819
15820 if (!cursor_row_fully_visible_p (w, 0, 0))
15821 {
15822 /* If vscroll is enabled, disable it and try again. */
15823 if (w->vscroll)
15824 {
15825 w->vscroll = 0;
15826 clear_glyph_matrix (w->desired_matrix);
15827 goto recenter;
15828 }
15829
15830 /* Users who set scroll-conservatively to a large number want
15831 point just above/below the scroll margin. If we ended up
15832 with point's row partially visible, move the window start to
15833 make that row fully visible and out of the margin. */
15834 if (scroll_conservatively > SCROLL_LIMIT)
15835 {
15836 int margin =
15837 scroll_margin > 0
15838 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15839 : 0;
15840 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15841
15842 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15843 clear_glyph_matrix (w->desired_matrix);
15844 if (1 == try_window (window, it.current.pos,
15845 TRY_WINDOW_CHECK_MARGINS))
15846 goto done;
15847 }
15848
15849 /* If centering point failed to make the whole line visible,
15850 put point at the top instead. That has to make the whole line
15851 visible, if it can be done. */
15852 if (centering_position == 0)
15853 goto done;
15854
15855 clear_glyph_matrix (w->desired_matrix);
15856 centering_position = 0;
15857 goto recenter;
15858 }
15859
15860 done:
15861
15862 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15863 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15864 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15865 ? Qt : Qnil);
15866
15867 /* Display the mode line, if we must. */
15868 if ((update_mode_line
15869 /* If window not full width, must redo its mode line
15870 if (a) the window to its side is being redone and
15871 (b) we do a frame-based redisplay. This is a consequence
15872 of how inverted lines are drawn in frame-based redisplay. */
15873 || (!just_this_one_p
15874 && !FRAME_WINDOW_P (f)
15875 && !WINDOW_FULL_WIDTH_P (w))
15876 /* Line number to display. */
15877 || INTEGERP (w->base_line_pos)
15878 /* Column number is displayed and different from the one displayed. */
15879 || (!NILP (w->column_number_displayed)
15880 && (XFASTINT (w->column_number_displayed) != current_column ())))
15881 /* This means that the window has a mode line. */
15882 && (WINDOW_WANTS_MODELINE_P (w)
15883 || WINDOW_WANTS_HEADER_LINE_P (w)))
15884 {
15885 display_mode_lines (w);
15886
15887 /* If mode line height has changed, arrange for a thorough
15888 immediate redisplay using the correct mode line height. */
15889 if (WINDOW_WANTS_MODELINE_P (w)
15890 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15891 {
15892 fonts_changed_p = 1;
15893 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15894 = DESIRED_MODE_LINE_HEIGHT (w);
15895 }
15896
15897 /* If header line height has changed, arrange for a thorough
15898 immediate redisplay using the correct header line height. */
15899 if (WINDOW_WANTS_HEADER_LINE_P (w)
15900 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15901 {
15902 fonts_changed_p = 1;
15903 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15904 = DESIRED_HEADER_LINE_HEIGHT (w);
15905 }
15906
15907 if (fonts_changed_p)
15908 goto need_larger_matrices;
15909 }
15910
15911 if (!line_number_displayed
15912 && !BUFFERP (w->base_line_pos))
15913 {
15914 w->base_line_pos = Qnil;
15915 w->base_line_number = Qnil;
15916 }
15917
15918 finish_menu_bars:
15919
15920 /* When we reach a frame's selected window, redo the frame's menu bar. */
15921 if (update_mode_line
15922 && EQ (FRAME_SELECTED_WINDOW (f), window))
15923 {
15924 int redisplay_menu_p = 0;
15925
15926 if (FRAME_WINDOW_P (f))
15927 {
15928 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15929 || defined (HAVE_NS) || defined (USE_GTK)
15930 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15931 #else
15932 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15933 #endif
15934 }
15935 else
15936 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15937
15938 if (redisplay_menu_p)
15939 display_menu_bar (w);
15940
15941 #ifdef HAVE_WINDOW_SYSTEM
15942 if (FRAME_WINDOW_P (f))
15943 {
15944 #if defined (USE_GTK) || defined (HAVE_NS)
15945 if (FRAME_EXTERNAL_TOOL_BAR (f))
15946 redisplay_tool_bar (f);
15947 #else
15948 if (WINDOWP (f->tool_bar_window)
15949 && (FRAME_TOOL_BAR_LINES (f) > 0
15950 || !NILP (Vauto_resize_tool_bars))
15951 && redisplay_tool_bar (f))
15952 ignore_mouse_drag_p = 1;
15953 #endif
15954 }
15955 #endif
15956 }
15957
15958 #ifdef HAVE_WINDOW_SYSTEM
15959 if (FRAME_WINDOW_P (f)
15960 && update_window_fringes (w, (just_this_one_p
15961 || (!used_current_matrix_p && !overlay_arrow_seen)
15962 || w->pseudo_window_p)))
15963 {
15964 update_begin (f);
15965 BLOCK_INPUT;
15966 if (draw_window_fringes (w, 1))
15967 x_draw_vertical_border (w);
15968 UNBLOCK_INPUT;
15969 update_end (f);
15970 }
15971 #endif /* HAVE_WINDOW_SYSTEM */
15972
15973 /* We go to this label, with fonts_changed_p nonzero,
15974 if it is necessary to try again using larger glyph matrices.
15975 We have to redeem the scroll bar even in this case,
15976 because the loop in redisplay_internal expects that. */
15977 need_larger_matrices:
15978 ;
15979 finish_scroll_bars:
15980
15981 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
15982 {
15983 /* Set the thumb's position and size. */
15984 set_vertical_scroll_bar (w);
15985
15986 /* Note that we actually used the scroll bar attached to this
15987 window, so it shouldn't be deleted at the end of redisplay. */
15988 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
15989 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
15990 }
15991
15992 /* Restore current_buffer and value of point in it. The window
15993 update may have changed the buffer, so first make sure `opoint'
15994 is still valid (Bug#6177). */
15995 if (CHARPOS (opoint) < BEGV)
15996 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15997 else if (CHARPOS (opoint) > ZV)
15998 TEMP_SET_PT_BOTH (Z, Z_BYTE);
15999 else
16000 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16001
16002 set_buffer_internal_1 (old);
16003 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16004 shorter. This can be caused by log truncation in *Messages*. */
16005 if (CHARPOS (lpoint) <= ZV)
16006 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16007
16008 unbind_to (count, Qnil);
16009 }
16010
16011
16012 /* Build the complete desired matrix of WINDOW with a window start
16013 buffer position POS.
16014
16015 Value is 1 if successful. It is zero if fonts were loaded during
16016 redisplay which makes re-adjusting glyph matrices necessary, and -1
16017 if point would appear in the scroll margins.
16018 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16019 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16020 set in FLAGS.) */
16021
16022 int
16023 try_window (Lisp_Object window, struct text_pos pos, int flags)
16024 {
16025 struct window *w = XWINDOW (window);
16026 struct it it;
16027 struct glyph_row *last_text_row = NULL;
16028 struct frame *f = XFRAME (w->frame);
16029
16030 /* Make POS the new window start. */
16031 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16032
16033 /* Mark cursor position as unknown. No overlay arrow seen. */
16034 w->cursor.vpos = -1;
16035 overlay_arrow_seen = 0;
16036
16037 /* Initialize iterator and info to start at POS. */
16038 start_display (&it, w, pos);
16039
16040 /* Display all lines of W. */
16041 while (it.current_y < it.last_visible_y)
16042 {
16043 if (display_line (&it))
16044 last_text_row = it.glyph_row - 1;
16045 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16046 return 0;
16047 }
16048
16049 /* Don't let the cursor end in the scroll margins. */
16050 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16051 && !MINI_WINDOW_P (w))
16052 {
16053 int this_scroll_margin;
16054
16055 if (scroll_margin > 0)
16056 {
16057 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16058 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16059 }
16060 else
16061 this_scroll_margin = 0;
16062
16063 if ((w->cursor.y >= 0 /* not vscrolled */
16064 && w->cursor.y < this_scroll_margin
16065 && CHARPOS (pos) > BEGV
16066 && IT_CHARPOS (it) < ZV)
16067 /* rms: considering make_cursor_line_fully_visible_p here
16068 seems to give wrong results. We don't want to recenter
16069 when the last line is partly visible, we want to allow
16070 that case to be handled in the usual way. */
16071 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16072 {
16073 w->cursor.vpos = -1;
16074 clear_glyph_matrix (w->desired_matrix);
16075 return -1;
16076 }
16077 }
16078
16079 /* If bottom moved off end of frame, change mode line percentage. */
16080 if (XFASTINT (w->window_end_pos) <= 0
16081 && Z != IT_CHARPOS (it))
16082 w->update_mode_line = Qt;
16083
16084 /* Set window_end_pos to the offset of the last character displayed
16085 on the window from the end of current_buffer. Set
16086 window_end_vpos to its row number. */
16087 if (last_text_row)
16088 {
16089 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16090 w->window_end_bytepos
16091 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16092 w->window_end_pos
16093 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16094 w->window_end_vpos
16095 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16096 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16097 ->displays_text_p);
16098 }
16099 else
16100 {
16101 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16102 w->window_end_pos = make_number (Z - ZV);
16103 w->window_end_vpos = make_number (0);
16104 }
16105
16106 /* But that is not valid info until redisplay finishes. */
16107 w->window_end_valid = Qnil;
16108 return 1;
16109 }
16110
16111
16112 \f
16113 /************************************************************************
16114 Window redisplay reusing current matrix when buffer has not changed
16115 ************************************************************************/
16116
16117 /* Try redisplay of window W showing an unchanged buffer with a
16118 different window start than the last time it was displayed by
16119 reusing its current matrix. Value is non-zero if successful.
16120 W->start is the new window start. */
16121
16122 static int
16123 try_window_reusing_current_matrix (struct window *w)
16124 {
16125 struct frame *f = XFRAME (w->frame);
16126 struct glyph_row *bottom_row;
16127 struct it it;
16128 struct run run;
16129 struct text_pos start, new_start;
16130 int nrows_scrolled, i;
16131 struct glyph_row *last_text_row;
16132 struct glyph_row *last_reused_text_row;
16133 struct glyph_row *start_row;
16134 int start_vpos, min_y, max_y;
16135
16136 #if GLYPH_DEBUG
16137 if (inhibit_try_window_reusing)
16138 return 0;
16139 #endif
16140
16141 if (/* This function doesn't handle terminal frames. */
16142 !FRAME_WINDOW_P (f)
16143 /* Don't try to reuse the display if windows have been split
16144 or such. */
16145 || windows_or_buffers_changed
16146 || cursor_type_changed)
16147 return 0;
16148
16149 /* Can't do this if region may have changed. */
16150 if ((!NILP (Vtransient_mark_mode)
16151 && !NILP (BVAR (current_buffer, mark_active)))
16152 || !NILP (w->region_showing)
16153 || !NILP (Vshow_trailing_whitespace))
16154 return 0;
16155
16156 /* If top-line visibility has changed, give up. */
16157 if (WINDOW_WANTS_HEADER_LINE_P (w)
16158 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16159 return 0;
16160
16161 /* Give up if old or new display is scrolled vertically. We could
16162 make this function handle this, but right now it doesn't. */
16163 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16164 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16165 return 0;
16166
16167 /* The variable new_start now holds the new window start. The old
16168 start `start' can be determined from the current matrix. */
16169 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16170 start = start_row->minpos;
16171 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16172
16173 /* Clear the desired matrix for the display below. */
16174 clear_glyph_matrix (w->desired_matrix);
16175
16176 if (CHARPOS (new_start) <= CHARPOS (start))
16177 {
16178 /* Don't use this method if the display starts with an ellipsis
16179 displayed for invisible text. It's not easy to handle that case
16180 below, and it's certainly not worth the effort since this is
16181 not a frequent case. */
16182 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16183 return 0;
16184
16185 IF_DEBUG (debug_method_add (w, "twu1"));
16186
16187 /* Display up to a row that can be reused. The variable
16188 last_text_row is set to the last row displayed that displays
16189 text. Note that it.vpos == 0 if or if not there is a
16190 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16191 start_display (&it, w, new_start);
16192 w->cursor.vpos = -1;
16193 last_text_row = last_reused_text_row = NULL;
16194
16195 while (it.current_y < it.last_visible_y
16196 && !fonts_changed_p)
16197 {
16198 /* If we have reached into the characters in the START row,
16199 that means the line boundaries have changed. So we
16200 can't start copying with the row START. Maybe it will
16201 work to start copying with the following row. */
16202 while (IT_CHARPOS (it) > CHARPOS (start))
16203 {
16204 /* Advance to the next row as the "start". */
16205 start_row++;
16206 start = start_row->minpos;
16207 /* If there are no more rows to try, or just one, give up. */
16208 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16209 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16210 || CHARPOS (start) == ZV)
16211 {
16212 clear_glyph_matrix (w->desired_matrix);
16213 return 0;
16214 }
16215
16216 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16217 }
16218 /* If we have reached alignment, we can copy the rest of the
16219 rows. */
16220 if (IT_CHARPOS (it) == CHARPOS (start)
16221 /* Don't accept "alignment" inside a display vector,
16222 since start_row could have started in the middle of
16223 that same display vector (thus their character
16224 positions match), and we have no way of telling if
16225 that is the case. */
16226 && it.current.dpvec_index < 0)
16227 break;
16228
16229 if (display_line (&it))
16230 last_text_row = it.glyph_row - 1;
16231
16232 }
16233
16234 /* A value of current_y < last_visible_y means that we stopped
16235 at the previous window start, which in turn means that we
16236 have at least one reusable row. */
16237 if (it.current_y < it.last_visible_y)
16238 {
16239 struct glyph_row *row;
16240
16241 /* IT.vpos always starts from 0; it counts text lines. */
16242 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16243
16244 /* Find PT if not already found in the lines displayed. */
16245 if (w->cursor.vpos < 0)
16246 {
16247 int dy = it.current_y - start_row->y;
16248
16249 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16250 row = row_containing_pos (w, PT, row, NULL, dy);
16251 if (row)
16252 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16253 dy, nrows_scrolled);
16254 else
16255 {
16256 clear_glyph_matrix (w->desired_matrix);
16257 return 0;
16258 }
16259 }
16260
16261 /* Scroll the display. Do it before the current matrix is
16262 changed. The problem here is that update has not yet
16263 run, i.e. part of the current matrix is not up to date.
16264 scroll_run_hook will clear the cursor, and use the
16265 current matrix to get the height of the row the cursor is
16266 in. */
16267 run.current_y = start_row->y;
16268 run.desired_y = it.current_y;
16269 run.height = it.last_visible_y - it.current_y;
16270
16271 if (run.height > 0 && run.current_y != run.desired_y)
16272 {
16273 update_begin (f);
16274 FRAME_RIF (f)->update_window_begin_hook (w);
16275 FRAME_RIF (f)->clear_window_mouse_face (w);
16276 FRAME_RIF (f)->scroll_run_hook (w, &run);
16277 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16278 update_end (f);
16279 }
16280
16281 /* Shift current matrix down by nrows_scrolled lines. */
16282 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16283 rotate_matrix (w->current_matrix,
16284 start_vpos,
16285 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16286 nrows_scrolled);
16287
16288 /* Disable lines that must be updated. */
16289 for (i = 0; i < nrows_scrolled; ++i)
16290 (start_row + i)->enabled_p = 0;
16291
16292 /* Re-compute Y positions. */
16293 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16294 max_y = it.last_visible_y;
16295 for (row = start_row + nrows_scrolled;
16296 row < bottom_row;
16297 ++row)
16298 {
16299 row->y = it.current_y;
16300 row->visible_height = row->height;
16301
16302 if (row->y < min_y)
16303 row->visible_height -= min_y - row->y;
16304 if (row->y + row->height > max_y)
16305 row->visible_height -= row->y + row->height - max_y;
16306 if (row->fringe_bitmap_periodic_p)
16307 row->redraw_fringe_bitmaps_p = 1;
16308
16309 it.current_y += row->height;
16310
16311 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16312 last_reused_text_row = row;
16313 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16314 break;
16315 }
16316
16317 /* Disable lines in the current matrix which are now
16318 below the window. */
16319 for (++row; row < bottom_row; ++row)
16320 row->enabled_p = row->mode_line_p = 0;
16321 }
16322
16323 /* Update window_end_pos etc.; last_reused_text_row is the last
16324 reused row from the current matrix containing text, if any.
16325 The value of last_text_row is the last displayed line
16326 containing text. */
16327 if (last_reused_text_row)
16328 {
16329 w->window_end_bytepos
16330 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16331 w->window_end_pos
16332 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16333 w->window_end_vpos
16334 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16335 w->current_matrix));
16336 }
16337 else if (last_text_row)
16338 {
16339 w->window_end_bytepos
16340 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16341 w->window_end_pos
16342 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16343 w->window_end_vpos
16344 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16345 }
16346 else
16347 {
16348 /* This window must be completely empty. */
16349 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16350 w->window_end_pos = make_number (Z - ZV);
16351 w->window_end_vpos = make_number (0);
16352 }
16353 w->window_end_valid = Qnil;
16354
16355 /* Update hint: don't try scrolling again in update_window. */
16356 w->desired_matrix->no_scrolling_p = 1;
16357
16358 #if GLYPH_DEBUG
16359 debug_method_add (w, "try_window_reusing_current_matrix 1");
16360 #endif
16361 return 1;
16362 }
16363 else if (CHARPOS (new_start) > CHARPOS (start))
16364 {
16365 struct glyph_row *pt_row, *row;
16366 struct glyph_row *first_reusable_row;
16367 struct glyph_row *first_row_to_display;
16368 int dy;
16369 int yb = window_text_bottom_y (w);
16370
16371 /* Find the row starting at new_start, if there is one. Don't
16372 reuse a partially visible line at the end. */
16373 first_reusable_row = start_row;
16374 while (first_reusable_row->enabled_p
16375 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16376 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16377 < CHARPOS (new_start)))
16378 ++first_reusable_row;
16379
16380 /* Give up if there is no row to reuse. */
16381 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16382 || !first_reusable_row->enabled_p
16383 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16384 != CHARPOS (new_start)))
16385 return 0;
16386
16387 /* We can reuse fully visible rows beginning with
16388 first_reusable_row to the end of the window. Set
16389 first_row_to_display to the first row that cannot be reused.
16390 Set pt_row to the row containing point, if there is any. */
16391 pt_row = NULL;
16392 for (first_row_to_display = first_reusable_row;
16393 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16394 ++first_row_to_display)
16395 {
16396 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16397 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16398 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16399 && first_row_to_display->ends_at_zv_p
16400 && pt_row == NULL)))
16401 pt_row = first_row_to_display;
16402 }
16403
16404 /* Start displaying at the start of first_row_to_display. */
16405 xassert (first_row_to_display->y < yb);
16406 init_to_row_start (&it, w, first_row_to_display);
16407
16408 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16409 - start_vpos);
16410 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16411 - nrows_scrolled);
16412 it.current_y = (first_row_to_display->y - first_reusable_row->y
16413 + WINDOW_HEADER_LINE_HEIGHT (w));
16414
16415 /* Display lines beginning with first_row_to_display in the
16416 desired matrix. Set last_text_row to the last row displayed
16417 that displays text. */
16418 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16419 if (pt_row == NULL)
16420 w->cursor.vpos = -1;
16421 last_text_row = NULL;
16422 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16423 if (display_line (&it))
16424 last_text_row = it.glyph_row - 1;
16425
16426 /* If point is in a reused row, adjust y and vpos of the cursor
16427 position. */
16428 if (pt_row)
16429 {
16430 w->cursor.vpos -= nrows_scrolled;
16431 w->cursor.y -= first_reusable_row->y - start_row->y;
16432 }
16433
16434 /* Give up if point isn't in a row displayed or reused. (This
16435 also handles the case where w->cursor.vpos < nrows_scrolled
16436 after the calls to display_line, which can happen with scroll
16437 margins. See bug#1295.) */
16438 if (w->cursor.vpos < 0)
16439 {
16440 clear_glyph_matrix (w->desired_matrix);
16441 return 0;
16442 }
16443
16444 /* Scroll the display. */
16445 run.current_y = first_reusable_row->y;
16446 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16447 run.height = it.last_visible_y - run.current_y;
16448 dy = run.current_y - run.desired_y;
16449
16450 if (run.height)
16451 {
16452 update_begin (f);
16453 FRAME_RIF (f)->update_window_begin_hook (w);
16454 FRAME_RIF (f)->clear_window_mouse_face (w);
16455 FRAME_RIF (f)->scroll_run_hook (w, &run);
16456 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16457 update_end (f);
16458 }
16459
16460 /* Adjust Y positions of reused rows. */
16461 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16462 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16463 max_y = it.last_visible_y;
16464 for (row = first_reusable_row; row < first_row_to_display; ++row)
16465 {
16466 row->y -= dy;
16467 row->visible_height = row->height;
16468 if (row->y < min_y)
16469 row->visible_height -= min_y - row->y;
16470 if (row->y + row->height > max_y)
16471 row->visible_height -= row->y + row->height - max_y;
16472 if (row->fringe_bitmap_periodic_p)
16473 row->redraw_fringe_bitmaps_p = 1;
16474 }
16475
16476 /* Scroll the current matrix. */
16477 xassert (nrows_scrolled > 0);
16478 rotate_matrix (w->current_matrix,
16479 start_vpos,
16480 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16481 -nrows_scrolled);
16482
16483 /* Disable rows not reused. */
16484 for (row -= nrows_scrolled; row < bottom_row; ++row)
16485 row->enabled_p = 0;
16486
16487 /* Point may have moved to a different line, so we cannot assume that
16488 the previous cursor position is valid; locate the correct row. */
16489 if (pt_row)
16490 {
16491 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16492 row < bottom_row
16493 && PT >= MATRIX_ROW_END_CHARPOS (row)
16494 && !row->ends_at_zv_p;
16495 row++)
16496 {
16497 w->cursor.vpos++;
16498 w->cursor.y = row->y;
16499 }
16500 if (row < bottom_row)
16501 {
16502 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16503 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16504
16505 /* Can't use this optimization with bidi-reordered glyph
16506 rows, unless cursor is already at point. */
16507 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16508 {
16509 if (!(w->cursor.hpos >= 0
16510 && w->cursor.hpos < row->used[TEXT_AREA]
16511 && BUFFERP (glyph->object)
16512 && glyph->charpos == PT))
16513 return 0;
16514 }
16515 else
16516 for (; glyph < end
16517 && (!BUFFERP (glyph->object)
16518 || glyph->charpos < PT);
16519 glyph++)
16520 {
16521 w->cursor.hpos++;
16522 w->cursor.x += glyph->pixel_width;
16523 }
16524 }
16525 }
16526
16527 /* Adjust window end. A null value of last_text_row means that
16528 the window end is in reused rows which in turn means that
16529 only its vpos can have changed. */
16530 if (last_text_row)
16531 {
16532 w->window_end_bytepos
16533 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16534 w->window_end_pos
16535 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16536 w->window_end_vpos
16537 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16538 }
16539 else
16540 {
16541 w->window_end_vpos
16542 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16543 }
16544
16545 w->window_end_valid = Qnil;
16546 w->desired_matrix->no_scrolling_p = 1;
16547
16548 #if GLYPH_DEBUG
16549 debug_method_add (w, "try_window_reusing_current_matrix 2");
16550 #endif
16551 return 1;
16552 }
16553
16554 return 0;
16555 }
16556
16557
16558 \f
16559 /************************************************************************
16560 Window redisplay reusing current matrix when buffer has changed
16561 ************************************************************************/
16562
16563 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16564 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16565 EMACS_INT *, EMACS_INT *);
16566 static struct glyph_row *
16567 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16568 struct glyph_row *);
16569
16570
16571 /* Return the last row in MATRIX displaying text. If row START is
16572 non-null, start searching with that row. IT gives the dimensions
16573 of the display. Value is null if matrix is empty; otherwise it is
16574 a pointer to the row found. */
16575
16576 static struct glyph_row *
16577 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16578 struct glyph_row *start)
16579 {
16580 struct glyph_row *row, *row_found;
16581
16582 /* Set row_found to the last row in IT->w's current matrix
16583 displaying text. The loop looks funny but think of partially
16584 visible lines. */
16585 row_found = NULL;
16586 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16587 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16588 {
16589 xassert (row->enabled_p);
16590 row_found = row;
16591 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16592 break;
16593 ++row;
16594 }
16595
16596 return row_found;
16597 }
16598
16599
16600 /* Return the last row in the current matrix of W that is not affected
16601 by changes at the start of current_buffer that occurred since W's
16602 current matrix was built. Value is null if no such row exists.
16603
16604 BEG_UNCHANGED us the number of characters unchanged at the start of
16605 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16606 first changed character in current_buffer. Characters at positions <
16607 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16608 when the current matrix was built. */
16609
16610 static struct glyph_row *
16611 find_last_unchanged_at_beg_row (struct window *w)
16612 {
16613 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
16614 struct glyph_row *row;
16615 struct glyph_row *row_found = NULL;
16616 int yb = window_text_bottom_y (w);
16617
16618 /* Find the last row displaying unchanged text. */
16619 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16620 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16621 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16622 ++row)
16623 {
16624 if (/* If row ends before first_changed_pos, it is unchanged,
16625 except in some case. */
16626 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16627 /* When row ends in ZV and we write at ZV it is not
16628 unchanged. */
16629 && !row->ends_at_zv_p
16630 /* When first_changed_pos is the end of a continued line,
16631 row is not unchanged because it may be no longer
16632 continued. */
16633 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16634 && (row->continued_p
16635 || row->exact_window_width_line_p))
16636 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16637 needs to be recomputed, so don't consider this row as
16638 unchanged. This happens when the last line was
16639 bidi-reordered and was killed immediately before this
16640 redisplay cycle. In that case, ROW->end stores the
16641 buffer position of the first visual-order character of
16642 the killed text, which is now beyond ZV. */
16643 && CHARPOS (row->end.pos) <= ZV)
16644 row_found = row;
16645
16646 /* Stop if last visible row. */
16647 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16648 break;
16649 }
16650
16651 return row_found;
16652 }
16653
16654
16655 /* Find the first glyph row in the current matrix of W that is not
16656 affected by changes at the end of current_buffer since the
16657 time W's current matrix was built.
16658
16659 Return in *DELTA the number of chars by which buffer positions in
16660 unchanged text at the end of current_buffer must be adjusted.
16661
16662 Return in *DELTA_BYTES the corresponding number of bytes.
16663
16664 Value is null if no such row exists, i.e. all rows are affected by
16665 changes. */
16666
16667 static struct glyph_row *
16668 find_first_unchanged_at_end_row (struct window *w,
16669 EMACS_INT *delta, EMACS_INT *delta_bytes)
16670 {
16671 struct glyph_row *row;
16672 struct glyph_row *row_found = NULL;
16673
16674 *delta = *delta_bytes = 0;
16675
16676 /* Display must not have been paused, otherwise the current matrix
16677 is not up to date. */
16678 eassert (!NILP (w->window_end_valid));
16679
16680 /* A value of window_end_pos >= END_UNCHANGED means that the window
16681 end is in the range of changed text. If so, there is no
16682 unchanged row at the end of W's current matrix. */
16683 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16684 return NULL;
16685
16686 /* Set row to the last row in W's current matrix displaying text. */
16687 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16688
16689 /* If matrix is entirely empty, no unchanged row exists. */
16690 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16691 {
16692 /* The value of row is the last glyph row in the matrix having a
16693 meaningful buffer position in it. The end position of row
16694 corresponds to window_end_pos. This allows us to translate
16695 buffer positions in the current matrix to current buffer
16696 positions for characters not in changed text. */
16697 EMACS_INT Z_old =
16698 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16699 EMACS_INT Z_BYTE_old =
16700 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16701 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
16702 struct glyph_row *first_text_row
16703 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16704
16705 *delta = Z - Z_old;
16706 *delta_bytes = Z_BYTE - Z_BYTE_old;
16707
16708 /* Set last_unchanged_pos to the buffer position of the last
16709 character in the buffer that has not been changed. Z is the
16710 index + 1 of the last character in current_buffer, i.e. by
16711 subtracting END_UNCHANGED we get the index of the last
16712 unchanged character, and we have to add BEG to get its buffer
16713 position. */
16714 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16715 last_unchanged_pos_old = last_unchanged_pos - *delta;
16716
16717 /* Search backward from ROW for a row displaying a line that
16718 starts at a minimum position >= last_unchanged_pos_old. */
16719 for (; row > first_text_row; --row)
16720 {
16721 /* This used to abort, but it can happen.
16722 It is ok to just stop the search instead here. KFS. */
16723 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16724 break;
16725
16726 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16727 row_found = row;
16728 }
16729 }
16730
16731 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16732
16733 return row_found;
16734 }
16735
16736
16737 /* Make sure that glyph rows in the current matrix of window W
16738 reference the same glyph memory as corresponding rows in the
16739 frame's frame matrix. This function is called after scrolling W's
16740 current matrix on a terminal frame in try_window_id and
16741 try_window_reusing_current_matrix. */
16742
16743 static void
16744 sync_frame_with_window_matrix_rows (struct window *w)
16745 {
16746 struct frame *f = XFRAME (w->frame);
16747 struct glyph_row *window_row, *window_row_end, *frame_row;
16748
16749 /* Preconditions: W must be a leaf window and full-width. Its frame
16750 must have a frame matrix. */
16751 xassert (NILP (w->hchild) && NILP (w->vchild));
16752 xassert (WINDOW_FULL_WIDTH_P (w));
16753 xassert (!FRAME_WINDOW_P (f));
16754
16755 /* If W is a full-width window, glyph pointers in W's current matrix
16756 have, by definition, to be the same as glyph pointers in the
16757 corresponding frame matrix. Note that frame matrices have no
16758 marginal areas (see build_frame_matrix). */
16759 window_row = w->current_matrix->rows;
16760 window_row_end = window_row + w->current_matrix->nrows;
16761 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16762 while (window_row < window_row_end)
16763 {
16764 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16765 struct glyph *end = window_row->glyphs[LAST_AREA];
16766
16767 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16768 frame_row->glyphs[TEXT_AREA] = start;
16769 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16770 frame_row->glyphs[LAST_AREA] = end;
16771
16772 /* Disable frame rows whose corresponding window rows have
16773 been disabled in try_window_id. */
16774 if (!window_row->enabled_p)
16775 frame_row->enabled_p = 0;
16776
16777 ++window_row, ++frame_row;
16778 }
16779 }
16780
16781
16782 /* Find the glyph row in window W containing CHARPOS. Consider all
16783 rows between START and END (not inclusive). END null means search
16784 all rows to the end of the display area of W. Value is the row
16785 containing CHARPOS or null. */
16786
16787 struct glyph_row *
16788 row_containing_pos (struct window *w, EMACS_INT charpos,
16789 struct glyph_row *start, struct glyph_row *end, int dy)
16790 {
16791 struct glyph_row *row = start;
16792 struct glyph_row *best_row = NULL;
16793 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16794 int last_y;
16795
16796 /* If we happen to start on a header-line, skip that. */
16797 if (row->mode_line_p)
16798 ++row;
16799
16800 if ((end && row >= end) || !row->enabled_p)
16801 return NULL;
16802
16803 last_y = window_text_bottom_y (w) - dy;
16804
16805 while (1)
16806 {
16807 /* Give up if we have gone too far. */
16808 if (end && row >= end)
16809 return NULL;
16810 /* This formerly returned if they were equal.
16811 I think that both quantities are of a "last plus one" type;
16812 if so, when they are equal, the row is within the screen. -- rms. */
16813 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16814 return NULL;
16815
16816 /* If it is in this row, return this row. */
16817 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16818 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16819 /* The end position of a row equals the start
16820 position of the next row. If CHARPOS is there, we
16821 would rather display it in the next line, except
16822 when this line ends in ZV. */
16823 && !row->ends_at_zv_p
16824 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16825 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16826 {
16827 struct glyph *g;
16828
16829 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16830 || (!best_row && !row->continued_p))
16831 return row;
16832 /* In bidi-reordered rows, there could be several rows
16833 occluding point, all of them belonging to the same
16834 continued line. We need to find the row which fits
16835 CHARPOS the best. */
16836 for (g = row->glyphs[TEXT_AREA];
16837 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16838 g++)
16839 {
16840 if (!STRINGP (g->object))
16841 {
16842 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16843 {
16844 mindif = eabs (g->charpos - charpos);
16845 best_row = row;
16846 /* Exact match always wins. */
16847 if (mindif == 0)
16848 return best_row;
16849 }
16850 }
16851 }
16852 }
16853 else if (best_row && !row->continued_p)
16854 return best_row;
16855 ++row;
16856 }
16857 }
16858
16859
16860 /* Try to redisplay window W by reusing its existing display. W's
16861 current matrix must be up to date when this function is called,
16862 i.e. window_end_valid must not be nil.
16863
16864 Value is
16865
16866 1 if display has been updated
16867 0 if otherwise unsuccessful
16868 -1 if redisplay with same window start is known not to succeed
16869
16870 The following steps are performed:
16871
16872 1. Find the last row in the current matrix of W that is not
16873 affected by changes at the start of current_buffer. If no such row
16874 is found, give up.
16875
16876 2. Find the first row in W's current matrix that is not affected by
16877 changes at the end of current_buffer. Maybe there is no such row.
16878
16879 3. Display lines beginning with the row + 1 found in step 1 to the
16880 row found in step 2 or, if step 2 didn't find a row, to the end of
16881 the window.
16882
16883 4. If cursor is not known to appear on the window, give up.
16884
16885 5. If display stopped at the row found in step 2, scroll the
16886 display and current matrix as needed.
16887
16888 6. Maybe display some lines at the end of W, if we must. This can
16889 happen under various circumstances, like a partially visible line
16890 becoming fully visible, or because newly displayed lines are displayed
16891 in smaller font sizes.
16892
16893 7. Update W's window end information. */
16894
16895 static int
16896 try_window_id (struct window *w)
16897 {
16898 struct frame *f = XFRAME (w->frame);
16899 struct glyph_matrix *current_matrix = w->current_matrix;
16900 struct glyph_matrix *desired_matrix = w->desired_matrix;
16901 struct glyph_row *last_unchanged_at_beg_row;
16902 struct glyph_row *first_unchanged_at_end_row;
16903 struct glyph_row *row;
16904 struct glyph_row *bottom_row;
16905 int bottom_vpos;
16906 struct it it;
16907 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
16908 int dvpos, dy;
16909 struct text_pos start_pos;
16910 struct run run;
16911 int first_unchanged_at_end_vpos = 0;
16912 struct glyph_row *last_text_row, *last_text_row_at_end;
16913 struct text_pos start;
16914 EMACS_INT first_changed_charpos, last_changed_charpos;
16915
16916 #if GLYPH_DEBUG
16917 if (inhibit_try_window_id)
16918 return 0;
16919 #endif
16920
16921 /* This is handy for debugging. */
16922 #if 0
16923 #define GIVE_UP(X) \
16924 do { \
16925 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16926 return 0; \
16927 } while (0)
16928 #else
16929 #define GIVE_UP(X) return 0
16930 #endif
16931
16932 SET_TEXT_POS_FROM_MARKER (start, w->start);
16933
16934 /* Don't use this for mini-windows because these can show
16935 messages and mini-buffers, and we don't handle that here. */
16936 if (MINI_WINDOW_P (w))
16937 GIVE_UP (1);
16938
16939 /* This flag is used to prevent redisplay optimizations. */
16940 if (windows_or_buffers_changed || cursor_type_changed)
16941 GIVE_UP (2);
16942
16943 /* Verify that narrowing has not changed.
16944 Also verify that we were not told to prevent redisplay optimizations.
16945 It would be nice to further
16946 reduce the number of cases where this prevents try_window_id. */
16947 if (current_buffer->clip_changed
16948 || current_buffer->prevent_redisplay_optimizations_p)
16949 GIVE_UP (3);
16950
16951 /* Window must either use window-based redisplay or be full width. */
16952 if (!FRAME_WINDOW_P (f)
16953 && (!FRAME_LINE_INS_DEL_OK (f)
16954 || !WINDOW_FULL_WIDTH_P (w)))
16955 GIVE_UP (4);
16956
16957 /* Give up if point is known NOT to appear in W. */
16958 if (PT < CHARPOS (start))
16959 GIVE_UP (5);
16960
16961 /* Another way to prevent redisplay optimizations. */
16962 if (XFASTINT (w->last_modified) == 0)
16963 GIVE_UP (6);
16964
16965 /* Verify that window is not hscrolled. */
16966 if (XFASTINT (w->hscroll) != 0)
16967 GIVE_UP (7);
16968
16969 /* Verify that display wasn't paused. */
16970 if (NILP (w->window_end_valid))
16971 GIVE_UP (8);
16972
16973 /* Can't use this if highlighting a region because a cursor movement
16974 will do more than just set the cursor. */
16975 if (!NILP (Vtransient_mark_mode)
16976 && !NILP (BVAR (current_buffer, mark_active)))
16977 GIVE_UP (9);
16978
16979 /* Likewise if highlighting trailing whitespace. */
16980 if (!NILP (Vshow_trailing_whitespace))
16981 GIVE_UP (11);
16982
16983 /* Likewise if showing a region. */
16984 if (!NILP (w->region_showing))
16985 GIVE_UP (10);
16986
16987 /* Can't use this if overlay arrow position and/or string have
16988 changed. */
16989 if (overlay_arrows_changed_p ())
16990 GIVE_UP (12);
16991
16992 /* When word-wrap is on, adding a space to the first word of a
16993 wrapped line can change the wrap position, altering the line
16994 above it. It might be worthwhile to handle this more
16995 intelligently, but for now just redisplay from scratch. */
16996 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
16997 GIVE_UP (21);
16998
16999 /* Under bidi reordering, adding or deleting a character in the
17000 beginning of a paragraph, before the first strong directional
17001 character, can change the base direction of the paragraph (unless
17002 the buffer specifies a fixed paragraph direction), which will
17003 require to redisplay the whole paragraph. It might be worthwhile
17004 to find the paragraph limits and widen the range of redisplayed
17005 lines to that, but for now just give up this optimization and
17006 redisplay from scratch. */
17007 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17008 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17009 GIVE_UP (22);
17010
17011 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17012 only if buffer has really changed. The reason is that the gap is
17013 initially at Z for freshly visited files. The code below would
17014 set end_unchanged to 0 in that case. */
17015 if (MODIFF > SAVE_MODIFF
17016 /* This seems to happen sometimes after saving a buffer. */
17017 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17018 {
17019 if (GPT - BEG < BEG_UNCHANGED)
17020 BEG_UNCHANGED = GPT - BEG;
17021 if (Z - GPT < END_UNCHANGED)
17022 END_UNCHANGED = Z - GPT;
17023 }
17024
17025 /* The position of the first and last character that has been changed. */
17026 first_changed_charpos = BEG + BEG_UNCHANGED;
17027 last_changed_charpos = Z - END_UNCHANGED;
17028
17029 /* If window starts after a line end, and the last change is in
17030 front of that newline, then changes don't affect the display.
17031 This case happens with stealth-fontification. Note that although
17032 the display is unchanged, glyph positions in the matrix have to
17033 be adjusted, of course. */
17034 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17035 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17036 && ((last_changed_charpos < CHARPOS (start)
17037 && CHARPOS (start) == BEGV)
17038 || (last_changed_charpos < CHARPOS (start) - 1
17039 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17040 {
17041 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17042 struct glyph_row *r0;
17043
17044 /* Compute how many chars/bytes have been added to or removed
17045 from the buffer. */
17046 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17047 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17048 Z_delta = Z - Z_old;
17049 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17050
17051 /* Give up if PT is not in the window. Note that it already has
17052 been checked at the start of try_window_id that PT is not in
17053 front of the window start. */
17054 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17055 GIVE_UP (13);
17056
17057 /* If window start is unchanged, we can reuse the whole matrix
17058 as is, after adjusting glyph positions. No need to compute
17059 the window end again, since its offset from Z hasn't changed. */
17060 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17061 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17062 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17063 /* PT must not be in a partially visible line. */
17064 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17065 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17066 {
17067 /* Adjust positions in the glyph matrix. */
17068 if (Z_delta || Z_delta_bytes)
17069 {
17070 struct glyph_row *r1
17071 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17072 increment_matrix_positions (w->current_matrix,
17073 MATRIX_ROW_VPOS (r0, current_matrix),
17074 MATRIX_ROW_VPOS (r1, current_matrix),
17075 Z_delta, Z_delta_bytes);
17076 }
17077
17078 /* Set the cursor. */
17079 row = row_containing_pos (w, PT, r0, NULL, 0);
17080 if (row)
17081 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17082 else
17083 abort ();
17084 return 1;
17085 }
17086 }
17087
17088 /* Handle the case that changes are all below what is displayed in
17089 the window, and that PT is in the window. This shortcut cannot
17090 be taken if ZV is visible in the window, and text has been added
17091 there that is visible in the window. */
17092 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17093 /* ZV is not visible in the window, or there are no
17094 changes at ZV, actually. */
17095 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17096 || first_changed_charpos == last_changed_charpos))
17097 {
17098 struct glyph_row *r0;
17099
17100 /* Give up if PT is not in the window. Note that it already has
17101 been checked at the start of try_window_id that PT is not in
17102 front of the window start. */
17103 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17104 GIVE_UP (14);
17105
17106 /* If window start is unchanged, we can reuse the whole matrix
17107 as is, without changing glyph positions since no text has
17108 been added/removed in front of the window end. */
17109 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17110 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17111 /* PT must not be in a partially visible line. */
17112 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17113 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17114 {
17115 /* We have to compute the window end anew since text
17116 could have been added/removed after it. */
17117 w->window_end_pos
17118 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17119 w->window_end_bytepos
17120 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17121
17122 /* Set the cursor. */
17123 row = row_containing_pos (w, PT, r0, NULL, 0);
17124 if (row)
17125 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17126 else
17127 abort ();
17128 return 2;
17129 }
17130 }
17131
17132 /* Give up if window start is in the changed area.
17133
17134 The condition used to read
17135
17136 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17137
17138 but why that was tested escapes me at the moment. */
17139 if (CHARPOS (start) >= first_changed_charpos
17140 && CHARPOS (start) <= last_changed_charpos)
17141 GIVE_UP (15);
17142
17143 /* Check that window start agrees with the start of the first glyph
17144 row in its current matrix. Check this after we know the window
17145 start is not in changed text, otherwise positions would not be
17146 comparable. */
17147 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17148 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17149 GIVE_UP (16);
17150
17151 /* Give up if the window ends in strings. Overlay strings
17152 at the end are difficult to handle, so don't try. */
17153 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17154 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17155 GIVE_UP (20);
17156
17157 /* Compute the position at which we have to start displaying new
17158 lines. Some of the lines at the top of the window might be
17159 reusable because they are not displaying changed text. Find the
17160 last row in W's current matrix not affected by changes at the
17161 start of current_buffer. Value is null if changes start in the
17162 first line of window. */
17163 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17164 if (last_unchanged_at_beg_row)
17165 {
17166 /* Avoid starting to display in the middle of a character, a TAB
17167 for instance. This is easier than to set up the iterator
17168 exactly, and it's not a frequent case, so the additional
17169 effort wouldn't really pay off. */
17170 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17171 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17172 && last_unchanged_at_beg_row > w->current_matrix->rows)
17173 --last_unchanged_at_beg_row;
17174
17175 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17176 GIVE_UP (17);
17177
17178 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17179 GIVE_UP (18);
17180 start_pos = it.current.pos;
17181
17182 /* Start displaying new lines in the desired matrix at the same
17183 vpos we would use in the current matrix, i.e. below
17184 last_unchanged_at_beg_row. */
17185 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17186 current_matrix);
17187 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17188 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17189
17190 xassert (it.hpos == 0 && it.current_x == 0);
17191 }
17192 else
17193 {
17194 /* There are no reusable lines at the start of the window.
17195 Start displaying in the first text line. */
17196 start_display (&it, w, start);
17197 it.vpos = it.first_vpos;
17198 start_pos = it.current.pos;
17199 }
17200
17201 /* Find the first row that is not affected by changes at the end of
17202 the buffer. Value will be null if there is no unchanged row, in
17203 which case we must redisplay to the end of the window. delta
17204 will be set to the value by which buffer positions beginning with
17205 first_unchanged_at_end_row have to be adjusted due to text
17206 changes. */
17207 first_unchanged_at_end_row
17208 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17209 IF_DEBUG (debug_delta = delta);
17210 IF_DEBUG (debug_delta_bytes = delta_bytes);
17211
17212 /* Set stop_pos to the buffer position up to which we will have to
17213 display new lines. If first_unchanged_at_end_row != NULL, this
17214 is the buffer position of the start of the line displayed in that
17215 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17216 that we don't stop at a buffer position. */
17217 stop_pos = 0;
17218 if (first_unchanged_at_end_row)
17219 {
17220 xassert (last_unchanged_at_beg_row == NULL
17221 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17222
17223 /* If this is a continuation line, move forward to the next one
17224 that isn't. Changes in lines above affect this line.
17225 Caution: this may move first_unchanged_at_end_row to a row
17226 not displaying text. */
17227 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17228 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17229 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17230 < it.last_visible_y))
17231 ++first_unchanged_at_end_row;
17232
17233 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17234 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17235 >= it.last_visible_y))
17236 first_unchanged_at_end_row = NULL;
17237 else
17238 {
17239 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17240 + delta);
17241 first_unchanged_at_end_vpos
17242 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17243 xassert (stop_pos >= Z - END_UNCHANGED);
17244 }
17245 }
17246 else if (last_unchanged_at_beg_row == NULL)
17247 GIVE_UP (19);
17248
17249
17250 #if GLYPH_DEBUG
17251
17252 /* Either there is no unchanged row at the end, or the one we have
17253 now displays text. This is a necessary condition for the window
17254 end pos calculation at the end of this function. */
17255 xassert (first_unchanged_at_end_row == NULL
17256 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17257
17258 debug_last_unchanged_at_beg_vpos
17259 = (last_unchanged_at_beg_row
17260 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17261 : -1);
17262 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17263
17264 #endif /* GLYPH_DEBUG != 0 */
17265
17266
17267 /* Display new lines. Set last_text_row to the last new line
17268 displayed which has text on it, i.e. might end up as being the
17269 line where the window_end_vpos is. */
17270 w->cursor.vpos = -1;
17271 last_text_row = NULL;
17272 overlay_arrow_seen = 0;
17273 while (it.current_y < it.last_visible_y
17274 && !fonts_changed_p
17275 && (first_unchanged_at_end_row == NULL
17276 || IT_CHARPOS (it) < stop_pos))
17277 {
17278 if (display_line (&it))
17279 last_text_row = it.glyph_row - 1;
17280 }
17281
17282 if (fonts_changed_p)
17283 return -1;
17284
17285
17286 /* Compute differences in buffer positions, y-positions etc. for
17287 lines reused at the bottom of the window. Compute what we can
17288 scroll. */
17289 if (first_unchanged_at_end_row
17290 /* No lines reused because we displayed everything up to the
17291 bottom of the window. */
17292 && it.current_y < it.last_visible_y)
17293 {
17294 dvpos = (it.vpos
17295 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17296 current_matrix));
17297 dy = it.current_y - first_unchanged_at_end_row->y;
17298 run.current_y = first_unchanged_at_end_row->y;
17299 run.desired_y = run.current_y + dy;
17300 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17301 }
17302 else
17303 {
17304 delta = delta_bytes = dvpos = dy
17305 = run.current_y = run.desired_y = run.height = 0;
17306 first_unchanged_at_end_row = NULL;
17307 }
17308 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17309
17310
17311 /* Find the cursor if not already found. We have to decide whether
17312 PT will appear on this window (it sometimes doesn't, but this is
17313 not a very frequent case.) This decision has to be made before
17314 the current matrix is altered. A value of cursor.vpos < 0 means
17315 that PT is either in one of the lines beginning at
17316 first_unchanged_at_end_row or below the window. Don't care for
17317 lines that might be displayed later at the window end; as
17318 mentioned, this is not a frequent case. */
17319 if (w->cursor.vpos < 0)
17320 {
17321 /* Cursor in unchanged rows at the top? */
17322 if (PT < CHARPOS (start_pos)
17323 && last_unchanged_at_beg_row)
17324 {
17325 row = row_containing_pos (w, PT,
17326 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17327 last_unchanged_at_beg_row + 1, 0);
17328 if (row)
17329 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17330 }
17331
17332 /* Start from first_unchanged_at_end_row looking for PT. */
17333 else if (first_unchanged_at_end_row)
17334 {
17335 row = row_containing_pos (w, PT - delta,
17336 first_unchanged_at_end_row, NULL, 0);
17337 if (row)
17338 set_cursor_from_row (w, row, w->current_matrix, delta,
17339 delta_bytes, dy, dvpos);
17340 }
17341
17342 /* Give up if cursor was not found. */
17343 if (w->cursor.vpos < 0)
17344 {
17345 clear_glyph_matrix (w->desired_matrix);
17346 return -1;
17347 }
17348 }
17349
17350 /* Don't let the cursor end in the scroll margins. */
17351 {
17352 int this_scroll_margin, cursor_height;
17353
17354 this_scroll_margin =
17355 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17356 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17357 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17358
17359 if ((w->cursor.y < this_scroll_margin
17360 && CHARPOS (start) > BEGV)
17361 /* Old redisplay didn't take scroll margin into account at the bottom,
17362 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17363 || (w->cursor.y + (make_cursor_line_fully_visible_p
17364 ? cursor_height + this_scroll_margin
17365 : 1)) > it.last_visible_y)
17366 {
17367 w->cursor.vpos = -1;
17368 clear_glyph_matrix (w->desired_matrix);
17369 return -1;
17370 }
17371 }
17372
17373 /* Scroll the display. Do it before changing the current matrix so
17374 that xterm.c doesn't get confused about where the cursor glyph is
17375 found. */
17376 if (dy && run.height)
17377 {
17378 update_begin (f);
17379
17380 if (FRAME_WINDOW_P (f))
17381 {
17382 FRAME_RIF (f)->update_window_begin_hook (w);
17383 FRAME_RIF (f)->clear_window_mouse_face (w);
17384 FRAME_RIF (f)->scroll_run_hook (w, &run);
17385 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17386 }
17387 else
17388 {
17389 /* Terminal frame. In this case, dvpos gives the number of
17390 lines to scroll by; dvpos < 0 means scroll up. */
17391 int from_vpos
17392 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17393 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17394 int end = (WINDOW_TOP_EDGE_LINE (w)
17395 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17396 + window_internal_height (w));
17397
17398 #if defined (HAVE_GPM) || defined (MSDOS)
17399 x_clear_window_mouse_face (w);
17400 #endif
17401 /* Perform the operation on the screen. */
17402 if (dvpos > 0)
17403 {
17404 /* Scroll last_unchanged_at_beg_row to the end of the
17405 window down dvpos lines. */
17406 set_terminal_window (f, end);
17407
17408 /* On dumb terminals delete dvpos lines at the end
17409 before inserting dvpos empty lines. */
17410 if (!FRAME_SCROLL_REGION_OK (f))
17411 ins_del_lines (f, end - dvpos, -dvpos);
17412
17413 /* Insert dvpos empty lines in front of
17414 last_unchanged_at_beg_row. */
17415 ins_del_lines (f, from, dvpos);
17416 }
17417 else if (dvpos < 0)
17418 {
17419 /* Scroll up last_unchanged_at_beg_vpos to the end of
17420 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17421 set_terminal_window (f, end);
17422
17423 /* Delete dvpos lines in front of
17424 last_unchanged_at_beg_vpos. ins_del_lines will set
17425 the cursor to the given vpos and emit |dvpos| delete
17426 line sequences. */
17427 ins_del_lines (f, from + dvpos, dvpos);
17428
17429 /* On a dumb terminal insert dvpos empty lines at the
17430 end. */
17431 if (!FRAME_SCROLL_REGION_OK (f))
17432 ins_del_lines (f, end + dvpos, -dvpos);
17433 }
17434
17435 set_terminal_window (f, 0);
17436 }
17437
17438 update_end (f);
17439 }
17440
17441 /* Shift reused rows of the current matrix to the right position.
17442 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17443 text. */
17444 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17445 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17446 if (dvpos < 0)
17447 {
17448 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17449 bottom_vpos, dvpos);
17450 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17451 bottom_vpos, 0);
17452 }
17453 else if (dvpos > 0)
17454 {
17455 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17456 bottom_vpos, dvpos);
17457 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17458 first_unchanged_at_end_vpos + dvpos, 0);
17459 }
17460
17461 /* For frame-based redisplay, make sure that current frame and window
17462 matrix are in sync with respect to glyph memory. */
17463 if (!FRAME_WINDOW_P (f))
17464 sync_frame_with_window_matrix_rows (w);
17465
17466 /* Adjust buffer positions in reused rows. */
17467 if (delta || delta_bytes)
17468 increment_matrix_positions (current_matrix,
17469 first_unchanged_at_end_vpos + dvpos,
17470 bottom_vpos, delta, delta_bytes);
17471
17472 /* Adjust Y positions. */
17473 if (dy)
17474 shift_glyph_matrix (w, current_matrix,
17475 first_unchanged_at_end_vpos + dvpos,
17476 bottom_vpos, dy);
17477
17478 if (first_unchanged_at_end_row)
17479 {
17480 first_unchanged_at_end_row += dvpos;
17481 if (first_unchanged_at_end_row->y >= it.last_visible_y
17482 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17483 first_unchanged_at_end_row = NULL;
17484 }
17485
17486 /* If scrolling up, there may be some lines to display at the end of
17487 the window. */
17488 last_text_row_at_end = NULL;
17489 if (dy < 0)
17490 {
17491 /* Scrolling up can leave for example a partially visible line
17492 at the end of the window to be redisplayed. */
17493 /* Set last_row to the glyph row in the current matrix where the
17494 window end line is found. It has been moved up or down in
17495 the matrix by dvpos. */
17496 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17497 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17498
17499 /* If last_row is the window end line, it should display text. */
17500 xassert (last_row->displays_text_p);
17501
17502 /* If window end line was partially visible before, begin
17503 displaying at that line. Otherwise begin displaying with the
17504 line following it. */
17505 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17506 {
17507 init_to_row_start (&it, w, last_row);
17508 it.vpos = last_vpos;
17509 it.current_y = last_row->y;
17510 }
17511 else
17512 {
17513 init_to_row_end (&it, w, last_row);
17514 it.vpos = 1 + last_vpos;
17515 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17516 ++last_row;
17517 }
17518
17519 /* We may start in a continuation line. If so, we have to
17520 get the right continuation_lines_width and current_x. */
17521 it.continuation_lines_width = last_row->continuation_lines_width;
17522 it.hpos = it.current_x = 0;
17523
17524 /* Display the rest of the lines at the window end. */
17525 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17526 while (it.current_y < it.last_visible_y
17527 && !fonts_changed_p)
17528 {
17529 /* Is it always sure that the display agrees with lines in
17530 the current matrix? I don't think so, so we mark rows
17531 displayed invalid in the current matrix by setting their
17532 enabled_p flag to zero. */
17533 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17534 if (display_line (&it))
17535 last_text_row_at_end = it.glyph_row - 1;
17536 }
17537 }
17538
17539 /* Update window_end_pos and window_end_vpos. */
17540 if (first_unchanged_at_end_row
17541 && !last_text_row_at_end)
17542 {
17543 /* Window end line if one of the preserved rows from the current
17544 matrix. Set row to the last row displaying text in current
17545 matrix starting at first_unchanged_at_end_row, after
17546 scrolling. */
17547 xassert (first_unchanged_at_end_row->displays_text_p);
17548 row = find_last_row_displaying_text (w->current_matrix, &it,
17549 first_unchanged_at_end_row);
17550 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17551
17552 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17553 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17554 w->window_end_vpos
17555 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17556 xassert (w->window_end_bytepos >= 0);
17557 IF_DEBUG (debug_method_add (w, "A"));
17558 }
17559 else if (last_text_row_at_end)
17560 {
17561 w->window_end_pos
17562 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17563 w->window_end_bytepos
17564 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17565 w->window_end_vpos
17566 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17567 xassert (w->window_end_bytepos >= 0);
17568 IF_DEBUG (debug_method_add (w, "B"));
17569 }
17570 else if (last_text_row)
17571 {
17572 /* We have displayed either to the end of the window or at the
17573 end of the window, i.e. the last row with text is to be found
17574 in the desired matrix. */
17575 w->window_end_pos
17576 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17577 w->window_end_bytepos
17578 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17579 w->window_end_vpos
17580 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17581 xassert (w->window_end_bytepos >= 0);
17582 }
17583 else if (first_unchanged_at_end_row == NULL
17584 && last_text_row == NULL
17585 && last_text_row_at_end == NULL)
17586 {
17587 /* Displayed to end of window, but no line containing text was
17588 displayed. Lines were deleted at the end of the window. */
17589 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17590 int vpos = XFASTINT (w->window_end_vpos);
17591 struct glyph_row *current_row = current_matrix->rows + vpos;
17592 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17593
17594 for (row = NULL;
17595 row == NULL && vpos >= first_vpos;
17596 --vpos, --current_row, --desired_row)
17597 {
17598 if (desired_row->enabled_p)
17599 {
17600 if (desired_row->displays_text_p)
17601 row = desired_row;
17602 }
17603 else if (current_row->displays_text_p)
17604 row = current_row;
17605 }
17606
17607 xassert (row != NULL);
17608 w->window_end_vpos = make_number (vpos + 1);
17609 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17610 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17611 xassert (w->window_end_bytepos >= 0);
17612 IF_DEBUG (debug_method_add (w, "C"));
17613 }
17614 else
17615 abort ();
17616
17617 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17618 debug_end_vpos = XFASTINT (w->window_end_vpos));
17619
17620 /* Record that display has not been completed. */
17621 w->window_end_valid = Qnil;
17622 w->desired_matrix->no_scrolling_p = 1;
17623 return 3;
17624
17625 #undef GIVE_UP
17626 }
17627
17628
17629 \f
17630 /***********************************************************************
17631 More debugging support
17632 ***********************************************************************/
17633
17634 #if GLYPH_DEBUG
17635
17636 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17637 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17638 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17639
17640
17641 /* Dump the contents of glyph matrix MATRIX on stderr.
17642
17643 GLYPHS 0 means don't show glyph contents.
17644 GLYPHS 1 means show glyphs in short form
17645 GLYPHS > 1 means show glyphs in long form. */
17646
17647 void
17648 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17649 {
17650 int i;
17651 for (i = 0; i < matrix->nrows; ++i)
17652 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17653 }
17654
17655
17656 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17657 the glyph row and area where the glyph comes from. */
17658
17659 void
17660 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17661 {
17662 if (glyph->type == CHAR_GLYPH)
17663 {
17664 fprintf (stderr,
17665 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17666 glyph - row->glyphs[TEXT_AREA],
17667 'C',
17668 glyph->charpos,
17669 (BUFFERP (glyph->object)
17670 ? 'B'
17671 : (STRINGP (glyph->object)
17672 ? 'S'
17673 : '-')),
17674 glyph->pixel_width,
17675 glyph->u.ch,
17676 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17677 ? glyph->u.ch
17678 : '.'),
17679 glyph->face_id,
17680 glyph->left_box_line_p,
17681 glyph->right_box_line_p);
17682 }
17683 else if (glyph->type == STRETCH_GLYPH)
17684 {
17685 fprintf (stderr,
17686 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17687 glyph - row->glyphs[TEXT_AREA],
17688 'S',
17689 glyph->charpos,
17690 (BUFFERP (glyph->object)
17691 ? 'B'
17692 : (STRINGP (glyph->object)
17693 ? 'S'
17694 : '-')),
17695 glyph->pixel_width,
17696 0,
17697 '.',
17698 glyph->face_id,
17699 glyph->left_box_line_p,
17700 glyph->right_box_line_p);
17701 }
17702 else if (glyph->type == IMAGE_GLYPH)
17703 {
17704 fprintf (stderr,
17705 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17706 glyph - row->glyphs[TEXT_AREA],
17707 'I',
17708 glyph->charpos,
17709 (BUFFERP (glyph->object)
17710 ? 'B'
17711 : (STRINGP (glyph->object)
17712 ? 'S'
17713 : '-')),
17714 glyph->pixel_width,
17715 glyph->u.img_id,
17716 '.',
17717 glyph->face_id,
17718 glyph->left_box_line_p,
17719 glyph->right_box_line_p);
17720 }
17721 else if (glyph->type == COMPOSITE_GLYPH)
17722 {
17723 fprintf (stderr,
17724 " %5td %4c %6"pI"d %c %3d 0x%05x",
17725 glyph - row->glyphs[TEXT_AREA],
17726 '+',
17727 glyph->charpos,
17728 (BUFFERP (glyph->object)
17729 ? 'B'
17730 : (STRINGP (glyph->object)
17731 ? 'S'
17732 : '-')),
17733 glyph->pixel_width,
17734 glyph->u.cmp.id);
17735 if (glyph->u.cmp.automatic)
17736 fprintf (stderr,
17737 "[%d-%d]",
17738 glyph->slice.cmp.from, glyph->slice.cmp.to);
17739 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17740 glyph->face_id,
17741 glyph->left_box_line_p,
17742 glyph->right_box_line_p);
17743 }
17744 }
17745
17746
17747 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17748 GLYPHS 0 means don't show glyph contents.
17749 GLYPHS 1 means show glyphs in short form
17750 GLYPHS > 1 means show glyphs in long form. */
17751
17752 void
17753 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17754 {
17755 if (glyphs != 1)
17756 {
17757 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17758 fprintf (stderr, "======================================================================\n");
17759
17760 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17761 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17762 vpos,
17763 MATRIX_ROW_START_CHARPOS (row),
17764 MATRIX_ROW_END_CHARPOS (row),
17765 row->used[TEXT_AREA],
17766 row->contains_overlapping_glyphs_p,
17767 row->enabled_p,
17768 row->truncated_on_left_p,
17769 row->truncated_on_right_p,
17770 row->continued_p,
17771 MATRIX_ROW_CONTINUATION_LINE_P (row),
17772 row->displays_text_p,
17773 row->ends_at_zv_p,
17774 row->fill_line_p,
17775 row->ends_in_middle_of_char_p,
17776 row->starts_in_middle_of_char_p,
17777 row->mouse_face_p,
17778 row->x,
17779 row->y,
17780 row->pixel_width,
17781 row->height,
17782 row->visible_height,
17783 row->ascent,
17784 row->phys_ascent);
17785 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
17786 row->end.overlay_string_index,
17787 row->continuation_lines_width);
17788 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17789 CHARPOS (row->start.string_pos),
17790 CHARPOS (row->end.string_pos));
17791 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17792 row->end.dpvec_index);
17793 }
17794
17795 if (glyphs > 1)
17796 {
17797 int area;
17798
17799 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17800 {
17801 struct glyph *glyph = row->glyphs[area];
17802 struct glyph *glyph_end = glyph + row->used[area];
17803
17804 /* Glyph for a line end in text. */
17805 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17806 ++glyph_end;
17807
17808 if (glyph < glyph_end)
17809 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17810
17811 for (; glyph < glyph_end; ++glyph)
17812 dump_glyph (row, glyph, area);
17813 }
17814 }
17815 else if (glyphs == 1)
17816 {
17817 int area;
17818
17819 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17820 {
17821 char *s = (char *) alloca (row->used[area] + 1);
17822 int i;
17823
17824 for (i = 0; i < row->used[area]; ++i)
17825 {
17826 struct glyph *glyph = row->glyphs[area] + i;
17827 if (glyph->type == CHAR_GLYPH
17828 && glyph->u.ch < 0x80
17829 && glyph->u.ch >= ' ')
17830 s[i] = glyph->u.ch;
17831 else
17832 s[i] = '.';
17833 }
17834
17835 s[i] = '\0';
17836 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17837 }
17838 }
17839 }
17840
17841
17842 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17843 Sdump_glyph_matrix, 0, 1, "p",
17844 doc: /* Dump the current matrix of the selected window to stderr.
17845 Shows contents of glyph row structures. With non-nil
17846 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17847 glyphs in short form, otherwise show glyphs in long form. */)
17848 (Lisp_Object glyphs)
17849 {
17850 struct window *w = XWINDOW (selected_window);
17851 struct buffer *buffer = XBUFFER (w->buffer);
17852
17853 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17854 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17855 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17856 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17857 fprintf (stderr, "=============================================\n");
17858 dump_glyph_matrix (w->current_matrix,
17859 NILP (glyphs) ? 0 : XINT (glyphs));
17860 return Qnil;
17861 }
17862
17863
17864 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17865 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17866 (void)
17867 {
17868 struct frame *f = XFRAME (selected_frame);
17869 dump_glyph_matrix (f->current_matrix, 1);
17870 return Qnil;
17871 }
17872
17873
17874 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17875 doc: /* Dump glyph row ROW to stderr.
17876 GLYPH 0 means don't dump glyphs.
17877 GLYPH 1 means dump glyphs in short form.
17878 GLYPH > 1 or omitted means dump glyphs in long form. */)
17879 (Lisp_Object row, Lisp_Object glyphs)
17880 {
17881 struct glyph_matrix *matrix;
17882 int vpos;
17883
17884 CHECK_NUMBER (row);
17885 matrix = XWINDOW (selected_window)->current_matrix;
17886 vpos = XINT (row);
17887 if (vpos >= 0 && vpos < matrix->nrows)
17888 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17889 vpos,
17890 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17891 return Qnil;
17892 }
17893
17894
17895 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17896 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17897 GLYPH 0 means don't dump glyphs.
17898 GLYPH 1 means dump glyphs in short form.
17899 GLYPH > 1 or omitted means dump glyphs in long form. */)
17900 (Lisp_Object row, Lisp_Object glyphs)
17901 {
17902 struct frame *sf = SELECTED_FRAME ();
17903 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17904 int vpos;
17905
17906 CHECK_NUMBER (row);
17907 vpos = XINT (row);
17908 if (vpos >= 0 && vpos < m->nrows)
17909 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17910 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17911 return Qnil;
17912 }
17913
17914
17915 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
17916 doc: /* Toggle tracing of redisplay.
17917 With ARG, turn tracing on if and only if ARG is positive. */)
17918 (Lisp_Object arg)
17919 {
17920 if (NILP (arg))
17921 trace_redisplay_p = !trace_redisplay_p;
17922 else
17923 {
17924 arg = Fprefix_numeric_value (arg);
17925 trace_redisplay_p = XINT (arg) > 0;
17926 }
17927
17928 return Qnil;
17929 }
17930
17931
17932 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
17933 doc: /* Like `format', but print result to stderr.
17934 usage: (trace-to-stderr STRING &rest OBJECTS) */)
17935 (ptrdiff_t nargs, Lisp_Object *args)
17936 {
17937 Lisp_Object s = Fformat (nargs, args);
17938 fprintf (stderr, "%s", SDATA (s));
17939 return Qnil;
17940 }
17941
17942 #endif /* GLYPH_DEBUG */
17943
17944
17945 \f
17946 /***********************************************************************
17947 Building Desired Matrix Rows
17948 ***********************************************************************/
17949
17950 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
17951 Used for non-window-redisplay windows, and for windows w/o left fringe. */
17952
17953 static struct glyph_row *
17954 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
17955 {
17956 struct frame *f = XFRAME (WINDOW_FRAME (w));
17957 struct buffer *buffer = XBUFFER (w->buffer);
17958 struct buffer *old = current_buffer;
17959 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
17960 int arrow_len = SCHARS (overlay_arrow_string);
17961 const unsigned char *arrow_end = arrow_string + arrow_len;
17962 const unsigned char *p;
17963 struct it it;
17964 int multibyte_p;
17965 int n_glyphs_before;
17966
17967 set_buffer_temp (buffer);
17968 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
17969 it.glyph_row->used[TEXT_AREA] = 0;
17970 SET_TEXT_POS (it.position, 0, 0);
17971
17972 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
17973 p = arrow_string;
17974 while (p < arrow_end)
17975 {
17976 Lisp_Object face, ilisp;
17977
17978 /* Get the next character. */
17979 if (multibyte_p)
17980 it.c = it.char_to_display = string_char_and_length (p, &it.len);
17981 else
17982 {
17983 it.c = it.char_to_display = *p, it.len = 1;
17984 if (! ASCII_CHAR_P (it.c))
17985 it.char_to_display = BYTE8_TO_CHAR (it.c);
17986 }
17987 p += it.len;
17988
17989 /* Get its face. */
17990 ilisp = make_number (p - arrow_string);
17991 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
17992 it.face_id = compute_char_face (f, it.char_to_display, face);
17993
17994 /* Compute its width, get its glyphs. */
17995 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
17996 SET_TEXT_POS (it.position, -1, -1);
17997 PRODUCE_GLYPHS (&it);
17998
17999 /* If this character doesn't fit any more in the line, we have
18000 to remove some glyphs. */
18001 if (it.current_x > it.last_visible_x)
18002 {
18003 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18004 break;
18005 }
18006 }
18007
18008 set_buffer_temp (old);
18009 return it.glyph_row;
18010 }
18011
18012
18013 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
18014 glyphs are only inserted for terminal frames since we can't really
18015 win with truncation glyphs when partially visible glyphs are
18016 involved. Which glyphs to insert is determined by
18017 produce_special_glyphs. */
18018
18019 static void
18020 insert_left_trunc_glyphs (struct it *it)
18021 {
18022 struct it truncate_it;
18023 struct glyph *from, *end, *to, *toend;
18024
18025 xassert (!FRAME_WINDOW_P (it->f));
18026
18027 /* Get the truncation glyphs. */
18028 truncate_it = *it;
18029 truncate_it.current_x = 0;
18030 truncate_it.face_id = DEFAULT_FACE_ID;
18031 truncate_it.glyph_row = &scratch_glyph_row;
18032 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18033 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18034 truncate_it.object = make_number (0);
18035 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18036
18037 /* Overwrite glyphs from IT with truncation glyphs. */
18038 if (!it->glyph_row->reversed_p)
18039 {
18040 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18041 end = from + truncate_it.glyph_row->used[TEXT_AREA];
18042 to = it->glyph_row->glyphs[TEXT_AREA];
18043 toend = to + it->glyph_row->used[TEXT_AREA];
18044
18045 while (from < end)
18046 *to++ = *from++;
18047
18048 /* There may be padding glyphs left over. Overwrite them too. */
18049 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18050 {
18051 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18052 while (from < end)
18053 *to++ = *from++;
18054 }
18055
18056 if (to > toend)
18057 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18058 }
18059 else
18060 {
18061 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18062 that back to front. */
18063 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18064 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18065 toend = it->glyph_row->glyphs[TEXT_AREA];
18066 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18067
18068 while (from >= end && to >= toend)
18069 *to-- = *from--;
18070 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18071 {
18072 from =
18073 truncate_it.glyph_row->glyphs[TEXT_AREA]
18074 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18075 while (from >= end && to >= toend)
18076 *to-- = *from--;
18077 }
18078 if (from >= end)
18079 {
18080 /* Need to free some room before prepending additional
18081 glyphs. */
18082 int move_by = from - end + 1;
18083 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18084 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18085
18086 for ( ; g >= g0; g--)
18087 g[move_by] = *g;
18088 while (from >= end)
18089 *to-- = *from--;
18090 it->glyph_row->used[TEXT_AREA] += move_by;
18091 }
18092 }
18093 }
18094
18095 /* Compute the hash code for ROW. */
18096 unsigned
18097 row_hash (struct glyph_row *row)
18098 {
18099 int area, k;
18100 unsigned hashval = 0;
18101
18102 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18103 for (k = 0; k < row->used[area]; ++k)
18104 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18105 + row->glyphs[area][k].u.val
18106 + row->glyphs[area][k].face_id
18107 + row->glyphs[area][k].padding_p
18108 + (row->glyphs[area][k].type << 2));
18109
18110 return hashval;
18111 }
18112
18113 /* Compute the pixel height and width of IT->glyph_row.
18114
18115 Most of the time, ascent and height of a display line will be equal
18116 to the max_ascent and max_height values of the display iterator
18117 structure. This is not the case if
18118
18119 1. We hit ZV without displaying anything. In this case, max_ascent
18120 and max_height will be zero.
18121
18122 2. We have some glyphs that don't contribute to the line height.
18123 (The glyph row flag contributes_to_line_height_p is for future
18124 pixmap extensions).
18125
18126 The first case is easily covered by using default values because in
18127 these cases, the line height does not really matter, except that it
18128 must not be zero. */
18129
18130 static void
18131 compute_line_metrics (struct it *it)
18132 {
18133 struct glyph_row *row = it->glyph_row;
18134
18135 if (FRAME_WINDOW_P (it->f))
18136 {
18137 int i, min_y, max_y;
18138
18139 /* The line may consist of one space only, that was added to
18140 place the cursor on it. If so, the row's height hasn't been
18141 computed yet. */
18142 if (row->height == 0)
18143 {
18144 if (it->max_ascent + it->max_descent == 0)
18145 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18146 row->ascent = it->max_ascent;
18147 row->height = it->max_ascent + it->max_descent;
18148 row->phys_ascent = it->max_phys_ascent;
18149 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18150 row->extra_line_spacing = it->max_extra_line_spacing;
18151 }
18152
18153 /* Compute the width of this line. */
18154 row->pixel_width = row->x;
18155 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18156 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18157
18158 xassert (row->pixel_width >= 0);
18159 xassert (row->ascent >= 0 && row->height > 0);
18160
18161 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18162 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18163
18164 /* If first line's physical ascent is larger than its logical
18165 ascent, use the physical ascent, and make the row taller.
18166 This makes accented characters fully visible. */
18167 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18168 && row->phys_ascent > row->ascent)
18169 {
18170 row->height += row->phys_ascent - row->ascent;
18171 row->ascent = row->phys_ascent;
18172 }
18173
18174 /* Compute how much of the line is visible. */
18175 row->visible_height = row->height;
18176
18177 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18178 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18179
18180 if (row->y < min_y)
18181 row->visible_height -= min_y - row->y;
18182 if (row->y + row->height > max_y)
18183 row->visible_height -= row->y + row->height - max_y;
18184 }
18185 else
18186 {
18187 row->pixel_width = row->used[TEXT_AREA];
18188 if (row->continued_p)
18189 row->pixel_width -= it->continuation_pixel_width;
18190 else if (row->truncated_on_right_p)
18191 row->pixel_width -= it->truncation_pixel_width;
18192 row->ascent = row->phys_ascent = 0;
18193 row->height = row->phys_height = row->visible_height = 1;
18194 row->extra_line_spacing = 0;
18195 }
18196
18197 /* Compute a hash code for this row. */
18198 row->hash = row_hash (row);
18199
18200 it->max_ascent = it->max_descent = 0;
18201 it->max_phys_ascent = it->max_phys_descent = 0;
18202 }
18203
18204
18205 /* Append one space to the glyph row of iterator IT if doing a
18206 window-based redisplay. The space has the same face as
18207 IT->face_id. Value is non-zero if a space was added.
18208
18209 This function is called to make sure that there is always one glyph
18210 at the end of a glyph row that the cursor can be set on under
18211 window-systems. (If there weren't such a glyph we would not know
18212 how wide and tall a box cursor should be displayed).
18213
18214 At the same time this space let's a nicely handle clearing to the
18215 end of the line if the row ends in italic text. */
18216
18217 static int
18218 append_space_for_newline (struct it *it, int default_face_p)
18219 {
18220 if (FRAME_WINDOW_P (it->f))
18221 {
18222 int n = it->glyph_row->used[TEXT_AREA];
18223
18224 if (it->glyph_row->glyphs[TEXT_AREA] + n
18225 < it->glyph_row->glyphs[1 + TEXT_AREA])
18226 {
18227 /* Save some values that must not be changed.
18228 Must save IT->c and IT->len because otherwise
18229 ITERATOR_AT_END_P wouldn't work anymore after
18230 append_space_for_newline has been called. */
18231 enum display_element_type saved_what = it->what;
18232 int saved_c = it->c, saved_len = it->len;
18233 int saved_char_to_display = it->char_to_display;
18234 int saved_x = it->current_x;
18235 int saved_face_id = it->face_id;
18236 struct text_pos saved_pos;
18237 Lisp_Object saved_object;
18238 struct face *face;
18239
18240 saved_object = it->object;
18241 saved_pos = it->position;
18242
18243 it->what = IT_CHARACTER;
18244 memset (&it->position, 0, sizeof it->position);
18245 it->object = make_number (0);
18246 it->c = it->char_to_display = ' ';
18247 it->len = 1;
18248
18249 /* If the default face was remapped, be sure to use the
18250 remapped face for the appended newline. */
18251 if (default_face_p)
18252 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18253 else if (it->face_before_selective_p)
18254 it->face_id = it->saved_face_id;
18255 face = FACE_FROM_ID (it->f, it->face_id);
18256 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18257
18258 PRODUCE_GLYPHS (it);
18259
18260 it->override_ascent = -1;
18261 it->constrain_row_ascent_descent_p = 0;
18262 it->current_x = saved_x;
18263 it->object = saved_object;
18264 it->position = saved_pos;
18265 it->what = saved_what;
18266 it->face_id = saved_face_id;
18267 it->len = saved_len;
18268 it->c = saved_c;
18269 it->char_to_display = saved_char_to_display;
18270 return 1;
18271 }
18272 }
18273
18274 return 0;
18275 }
18276
18277
18278 /* Extend the face of the last glyph in the text area of IT->glyph_row
18279 to the end of the display line. Called from display_line. If the
18280 glyph row is empty, add a space glyph to it so that we know the
18281 face to draw. Set the glyph row flag fill_line_p. If the glyph
18282 row is R2L, prepend a stretch glyph to cover the empty space to the
18283 left of the leftmost glyph. */
18284
18285 static void
18286 extend_face_to_end_of_line (struct it *it)
18287 {
18288 struct face *face, *default_face;
18289 struct frame *f = it->f;
18290
18291 /* If line is already filled, do nothing. Non window-system frames
18292 get a grace of one more ``pixel'' because their characters are
18293 1-``pixel'' wide, so they hit the equality too early. This grace
18294 is needed only for R2L rows that are not continued, to produce
18295 one extra blank where we could display the cursor. */
18296 if (it->current_x >= it->last_visible_x
18297 + (!FRAME_WINDOW_P (f)
18298 && it->glyph_row->reversed_p
18299 && !it->glyph_row->continued_p))
18300 return;
18301
18302 /* The default face, possibly remapped. */
18303 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18304
18305 /* Face extension extends the background and box of IT->face_id
18306 to the end of the line. If the background equals the background
18307 of the frame, we don't have to do anything. */
18308 if (it->face_before_selective_p)
18309 face = FACE_FROM_ID (f, it->saved_face_id);
18310 else
18311 face = FACE_FROM_ID (f, it->face_id);
18312
18313 if (FRAME_WINDOW_P (f)
18314 && it->glyph_row->displays_text_p
18315 && face->box == FACE_NO_BOX
18316 && face->background == FRAME_BACKGROUND_PIXEL (f)
18317 && !face->stipple
18318 && !it->glyph_row->reversed_p)
18319 return;
18320
18321 /* Set the glyph row flag indicating that the face of the last glyph
18322 in the text area has to be drawn to the end of the text area. */
18323 it->glyph_row->fill_line_p = 1;
18324
18325 /* If current character of IT is not ASCII, make sure we have the
18326 ASCII face. This will be automatically undone the next time
18327 get_next_display_element returns a multibyte character. Note
18328 that the character will always be single byte in unibyte
18329 text. */
18330 if (!ASCII_CHAR_P (it->c))
18331 {
18332 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18333 }
18334
18335 if (FRAME_WINDOW_P (f))
18336 {
18337 /* If the row is empty, add a space with the current face of IT,
18338 so that we know which face to draw. */
18339 if (it->glyph_row->used[TEXT_AREA] == 0)
18340 {
18341 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18342 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18343 it->glyph_row->used[TEXT_AREA] = 1;
18344 }
18345 #ifdef HAVE_WINDOW_SYSTEM
18346 if (it->glyph_row->reversed_p)
18347 {
18348 /* Prepend a stretch glyph to the row, such that the
18349 rightmost glyph will be drawn flushed all the way to the
18350 right margin of the window. The stretch glyph that will
18351 occupy the empty space, if any, to the left of the
18352 glyphs. */
18353 struct font *font = face->font ? face->font : FRAME_FONT (f);
18354 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18355 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18356 struct glyph *g;
18357 int row_width, stretch_ascent, stretch_width;
18358 struct text_pos saved_pos;
18359 int saved_face_id, saved_avoid_cursor;
18360
18361 for (row_width = 0, g = row_start; g < row_end; g++)
18362 row_width += g->pixel_width;
18363 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18364 if (stretch_width > 0)
18365 {
18366 stretch_ascent =
18367 (((it->ascent + it->descent)
18368 * FONT_BASE (font)) / FONT_HEIGHT (font));
18369 saved_pos = it->position;
18370 memset (&it->position, 0, sizeof it->position);
18371 saved_avoid_cursor = it->avoid_cursor_p;
18372 it->avoid_cursor_p = 1;
18373 saved_face_id = it->face_id;
18374 /* The last row's stretch glyph should get the default
18375 face, to avoid painting the rest of the window with
18376 the region face, if the region ends at ZV. */
18377 if (it->glyph_row->ends_at_zv_p)
18378 it->face_id = default_face->id;
18379 else
18380 it->face_id = face->id;
18381 append_stretch_glyph (it, make_number (0), stretch_width,
18382 it->ascent + it->descent, stretch_ascent);
18383 it->position = saved_pos;
18384 it->avoid_cursor_p = saved_avoid_cursor;
18385 it->face_id = saved_face_id;
18386 }
18387 }
18388 #endif /* HAVE_WINDOW_SYSTEM */
18389 }
18390 else
18391 {
18392 /* Save some values that must not be changed. */
18393 int saved_x = it->current_x;
18394 struct text_pos saved_pos;
18395 Lisp_Object saved_object;
18396 enum display_element_type saved_what = it->what;
18397 int saved_face_id = it->face_id;
18398
18399 saved_object = it->object;
18400 saved_pos = it->position;
18401
18402 it->what = IT_CHARACTER;
18403 memset (&it->position, 0, sizeof it->position);
18404 it->object = make_number (0);
18405 it->c = it->char_to_display = ' ';
18406 it->len = 1;
18407 /* The last row's blank glyphs should get the default face, to
18408 avoid painting the rest of the window with the region face,
18409 if the region ends at ZV. */
18410 if (it->glyph_row->ends_at_zv_p)
18411 it->face_id = default_face->id;
18412 else
18413 it->face_id = face->id;
18414
18415 PRODUCE_GLYPHS (it);
18416
18417 while (it->current_x <= it->last_visible_x)
18418 PRODUCE_GLYPHS (it);
18419
18420 /* Don't count these blanks really. It would let us insert a left
18421 truncation glyph below and make us set the cursor on them, maybe. */
18422 it->current_x = saved_x;
18423 it->object = saved_object;
18424 it->position = saved_pos;
18425 it->what = saved_what;
18426 it->face_id = saved_face_id;
18427 }
18428 }
18429
18430
18431 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18432 trailing whitespace. */
18433
18434 static int
18435 trailing_whitespace_p (EMACS_INT charpos)
18436 {
18437 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
18438 int c = 0;
18439
18440 while (bytepos < ZV_BYTE
18441 && (c = FETCH_CHAR (bytepos),
18442 c == ' ' || c == '\t'))
18443 ++bytepos;
18444
18445 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18446 {
18447 if (bytepos != PT_BYTE)
18448 return 1;
18449 }
18450 return 0;
18451 }
18452
18453
18454 /* Highlight trailing whitespace, if any, in ROW. */
18455
18456 static void
18457 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18458 {
18459 int used = row->used[TEXT_AREA];
18460
18461 if (used)
18462 {
18463 struct glyph *start = row->glyphs[TEXT_AREA];
18464 struct glyph *glyph = start + used - 1;
18465
18466 if (row->reversed_p)
18467 {
18468 /* Right-to-left rows need to be processed in the opposite
18469 direction, so swap the edge pointers. */
18470 glyph = start;
18471 start = row->glyphs[TEXT_AREA] + used - 1;
18472 }
18473
18474 /* Skip over glyphs inserted to display the cursor at the
18475 end of a line, for extending the face of the last glyph
18476 to the end of the line on terminals, and for truncation
18477 and continuation glyphs. */
18478 if (!row->reversed_p)
18479 {
18480 while (glyph >= start
18481 && glyph->type == CHAR_GLYPH
18482 && INTEGERP (glyph->object))
18483 --glyph;
18484 }
18485 else
18486 {
18487 while (glyph <= start
18488 && glyph->type == CHAR_GLYPH
18489 && INTEGERP (glyph->object))
18490 ++glyph;
18491 }
18492
18493 /* If last glyph is a space or stretch, and it's trailing
18494 whitespace, set the face of all trailing whitespace glyphs in
18495 IT->glyph_row to `trailing-whitespace'. */
18496 if ((row->reversed_p ? glyph <= start : glyph >= start)
18497 && BUFFERP (glyph->object)
18498 && (glyph->type == STRETCH_GLYPH
18499 || (glyph->type == CHAR_GLYPH
18500 && glyph->u.ch == ' '))
18501 && trailing_whitespace_p (glyph->charpos))
18502 {
18503 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18504 if (face_id < 0)
18505 return;
18506
18507 if (!row->reversed_p)
18508 {
18509 while (glyph >= start
18510 && BUFFERP (glyph->object)
18511 && (glyph->type == STRETCH_GLYPH
18512 || (glyph->type == CHAR_GLYPH
18513 && glyph->u.ch == ' ')))
18514 (glyph--)->face_id = face_id;
18515 }
18516 else
18517 {
18518 while (glyph <= start
18519 && BUFFERP (glyph->object)
18520 && (glyph->type == STRETCH_GLYPH
18521 || (glyph->type == CHAR_GLYPH
18522 && glyph->u.ch == ' ')))
18523 (glyph++)->face_id = face_id;
18524 }
18525 }
18526 }
18527 }
18528
18529
18530 /* Value is non-zero if glyph row ROW should be
18531 used to hold the cursor. */
18532
18533 static int
18534 cursor_row_p (struct glyph_row *row)
18535 {
18536 int result = 1;
18537
18538 if (PT == CHARPOS (row->end.pos)
18539 || PT == MATRIX_ROW_END_CHARPOS (row))
18540 {
18541 /* Suppose the row ends on a string.
18542 Unless the row is continued, that means it ends on a newline
18543 in the string. If it's anything other than a display string
18544 (e.g., a before-string from an overlay), we don't want the
18545 cursor there. (This heuristic seems to give the optimal
18546 behavior for the various types of multi-line strings.)
18547 One exception: if the string has `cursor' property on one of
18548 its characters, we _do_ want the cursor there. */
18549 if (CHARPOS (row->end.string_pos) >= 0)
18550 {
18551 if (row->continued_p)
18552 result = 1;
18553 else
18554 {
18555 /* Check for `display' property. */
18556 struct glyph *beg = row->glyphs[TEXT_AREA];
18557 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18558 struct glyph *glyph;
18559
18560 result = 0;
18561 for (glyph = end; glyph >= beg; --glyph)
18562 if (STRINGP (glyph->object))
18563 {
18564 Lisp_Object prop
18565 = Fget_char_property (make_number (PT),
18566 Qdisplay, Qnil);
18567 result =
18568 (!NILP (prop)
18569 && display_prop_string_p (prop, glyph->object));
18570 /* If there's a `cursor' property on one of the
18571 string's characters, this row is a cursor row,
18572 even though this is not a display string. */
18573 if (!result)
18574 {
18575 Lisp_Object s = glyph->object;
18576
18577 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18578 {
18579 EMACS_INT gpos = glyph->charpos;
18580
18581 if (!NILP (Fget_char_property (make_number (gpos),
18582 Qcursor, s)))
18583 {
18584 result = 1;
18585 break;
18586 }
18587 }
18588 }
18589 break;
18590 }
18591 }
18592 }
18593 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18594 {
18595 /* If the row ends in middle of a real character,
18596 and the line is continued, we want the cursor here.
18597 That's because CHARPOS (ROW->end.pos) would equal
18598 PT if PT is before the character. */
18599 if (!row->ends_in_ellipsis_p)
18600 result = row->continued_p;
18601 else
18602 /* If the row ends in an ellipsis, then
18603 CHARPOS (ROW->end.pos) will equal point after the
18604 invisible text. We want that position to be displayed
18605 after the ellipsis. */
18606 result = 0;
18607 }
18608 /* If the row ends at ZV, display the cursor at the end of that
18609 row instead of at the start of the row below. */
18610 else if (row->ends_at_zv_p)
18611 result = 1;
18612 else
18613 result = 0;
18614 }
18615
18616 return result;
18617 }
18618
18619 \f
18620
18621 /* Push the property PROP so that it will be rendered at the current
18622 position in IT. Return 1 if PROP was successfully pushed, 0
18623 otherwise. Called from handle_line_prefix to handle the
18624 `line-prefix' and `wrap-prefix' properties. */
18625
18626 static int
18627 push_prefix_prop (struct it *it, Lisp_Object prop)
18628 {
18629 struct text_pos pos =
18630 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18631
18632 xassert (it->method == GET_FROM_BUFFER
18633 || it->method == GET_FROM_DISPLAY_VECTOR
18634 || it->method == GET_FROM_STRING);
18635
18636 /* We need to save the current buffer/string position, so it will be
18637 restored by pop_it, because iterate_out_of_display_property
18638 depends on that being set correctly, but some situations leave
18639 it->position not yet set when this function is called. */
18640 push_it (it, &pos);
18641
18642 if (STRINGP (prop))
18643 {
18644 if (SCHARS (prop) == 0)
18645 {
18646 pop_it (it);
18647 return 0;
18648 }
18649
18650 it->string = prop;
18651 it->string_from_prefix_prop_p = 1;
18652 it->multibyte_p = STRING_MULTIBYTE (it->string);
18653 it->current.overlay_string_index = -1;
18654 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18655 it->end_charpos = it->string_nchars = SCHARS (it->string);
18656 it->method = GET_FROM_STRING;
18657 it->stop_charpos = 0;
18658 it->prev_stop = 0;
18659 it->base_level_stop = 0;
18660
18661 /* Force paragraph direction to be that of the parent
18662 buffer/string. */
18663 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18664 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18665 else
18666 it->paragraph_embedding = L2R;
18667
18668 /* Set up the bidi iterator for this display string. */
18669 if (it->bidi_p)
18670 {
18671 it->bidi_it.string.lstring = it->string;
18672 it->bidi_it.string.s = NULL;
18673 it->bidi_it.string.schars = it->end_charpos;
18674 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18675 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18676 it->bidi_it.string.unibyte = !it->multibyte_p;
18677 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18678 }
18679 }
18680 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18681 {
18682 it->method = GET_FROM_STRETCH;
18683 it->object = prop;
18684 }
18685 #ifdef HAVE_WINDOW_SYSTEM
18686 else if (IMAGEP (prop))
18687 {
18688 it->what = IT_IMAGE;
18689 it->image_id = lookup_image (it->f, prop);
18690 it->method = GET_FROM_IMAGE;
18691 }
18692 #endif /* HAVE_WINDOW_SYSTEM */
18693 else
18694 {
18695 pop_it (it); /* bogus display property, give up */
18696 return 0;
18697 }
18698
18699 return 1;
18700 }
18701
18702 /* Return the character-property PROP at the current position in IT. */
18703
18704 static Lisp_Object
18705 get_it_property (struct it *it, Lisp_Object prop)
18706 {
18707 Lisp_Object position;
18708
18709 if (STRINGP (it->object))
18710 position = make_number (IT_STRING_CHARPOS (*it));
18711 else if (BUFFERP (it->object))
18712 position = make_number (IT_CHARPOS (*it));
18713 else
18714 return Qnil;
18715
18716 return Fget_char_property (position, prop, it->object);
18717 }
18718
18719 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18720
18721 static void
18722 handle_line_prefix (struct it *it)
18723 {
18724 Lisp_Object prefix;
18725
18726 if (it->continuation_lines_width > 0)
18727 {
18728 prefix = get_it_property (it, Qwrap_prefix);
18729 if (NILP (prefix))
18730 prefix = Vwrap_prefix;
18731 }
18732 else
18733 {
18734 prefix = get_it_property (it, Qline_prefix);
18735 if (NILP (prefix))
18736 prefix = Vline_prefix;
18737 }
18738 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18739 {
18740 /* If the prefix is wider than the window, and we try to wrap
18741 it, it would acquire its own wrap prefix, and so on till the
18742 iterator stack overflows. So, don't wrap the prefix. */
18743 it->line_wrap = TRUNCATE;
18744 it->avoid_cursor_p = 1;
18745 }
18746 }
18747
18748 \f
18749
18750 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18751 only for R2L lines from display_line and display_string, when they
18752 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18753 the line/string needs to be continued on the next glyph row. */
18754 static void
18755 unproduce_glyphs (struct it *it, int n)
18756 {
18757 struct glyph *glyph, *end;
18758
18759 xassert (it->glyph_row);
18760 xassert (it->glyph_row->reversed_p);
18761 xassert (it->area == TEXT_AREA);
18762 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18763
18764 if (n > it->glyph_row->used[TEXT_AREA])
18765 n = it->glyph_row->used[TEXT_AREA];
18766 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18767 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18768 for ( ; glyph < end; glyph++)
18769 glyph[-n] = *glyph;
18770 }
18771
18772 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18773 and ROW->maxpos. */
18774 static void
18775 find_row_edges (struct it *it, struct glyph_row *row,
18776 EMACS_INT min_pos, EMACS_INT min_bpos,
18777 EMACS_INT max_pos, EMACS_INT max_bpos)
18778 {
18779 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18780 lines' rows is implemented for bidi-reordered rows. */
18781
18782 /* ROW->minpos is the value of min_pos, the minimal buffer position
18783 we have in ROW, or ROW->start.pos if that is smaller. */
18784 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18785 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18786 else
18787 /* We didn't find buffer positions smaller than ROW->start, or
18788 didn't find _any_ valid buffer positions in any of the glyphs,
18789 so we must trust the iterator's computed positions. */
18790 row->minpos = row->start.pos;
18791 if (max_pos <= 0)
18792 {
18793 max_pos = CHARPOS (it->current.pos);
18794 max_bpos = BYTEPOS (it->current.pos);
18795 }
18796
18797 /* Here are the various use-cases for ending the row, and the
18798 corresponding values for ROW->maxpos:
18799
18800 Line ends in a newline from buffer eol_pos + 1
18801 Line is continued from buffer max_pos + 1
18802 Line is truncated on right it->current.pos
18803 Line ends in a newline from string max_pos + 1(*)
18804 (*) + 1 only when line ends in a forward scan
18805 Line is continued from string max_pos
18806 Line is continued from display vector max_pos
18807 Line is entirely from a string min_pos == max_pos
18808 Line is entirely from a display vector min_pos == max_pos
18809 Line that ends at ZV ZV
18810
18811 If you discover other use-cases, please add them here as
18812 appropriate. */
18813 if (row->ends_at_zv_p)
18814 row->maxpos = it->current.pos;
18815 else if (row->used[TEXT_AREA])
18816 {
18817 int seen_this_string = 0;
18818 struct glyph_row *r1 = row - 1;
18819
18820 /* Did we see the same display string on the previous row? */
18821 if (STRINGP (it->object)
18822 /* this is not the first row */
18823 && row > it->w->desired_matrix->rows
18824 /* previous row is not the header line */
18825 && !r1->mode_line_p
18826 /* previous row also ends in a newline from a string */
18827 && r1->ends_in_newline_from_string_p)
18828 {
18829 struct glyph *start, *end;
18830
18831 /* Search for the last glyph of the previous row that came
18832 from buffer or string. Depending on whether the row is
18833 L2R or R2L, we need to process it front to back or the
18834 other way round. */
18835 if (!r1->reversed_p)
18836 {
18837 start = r1->glyphs[TEXT_AREA];
18838 end = start + r1->used[TEXT_AREA];
18839 /* Glyphs inserted by redisplay have an integer (zero)
18840 as their object. */
18841 while (end > start
18842 && INTEGERP ((end - 1)->object)
18843 && (end - 1)->charpos <= 0)
18844 --end;
18845 if (end > start)
18846 {
18847 if (EQ ((end - 1)->object, it->object))
18848 seen_this_string = 1;
18849 }
18850 else
18851 /* If all the glyphs of the previous row were inserted
18852 by redisplay, it means the previous row was
18853 produced from a single newline, which is only
18854 possible if that newline came from the same string
18855 as the one which produced this ROW. */
18856 seen_this_string = 1;
18857 }
18858 else
18859 {
18860 end = r1->glyphs[TEXT_AREA] - 1;
18861 start = end + r1->used[TEXT_AREA];
18862 while (end < start
18863 && INTEGERP ((end + 1)->object)
18864 && (end + 1)->charpos <= 0)
18865 ++end;
18866 if (end < start)
18867 {
18868 if (EQ ((end + 1)->object, it->object))
18869 seen_this_string = 1;
18870 }
18871 else
18872 seen_this_string = 1;
18873 }
18874 }
18875 /* Take note of each display string that covers a newline only
18876 once, the first time we see it. This is for when a display
18877 string includes more than one newline in it. */
18878 if (row->ends_in_newline_from_string_p && !seen_this_string)
18879 {
18880 /* If we were scanning the buffer forward when we displayed
18881 the string, we want to account for at least one buffer
18882 position that belongs to this row (position covered by
18883 the display string), so that cursor positioning will
18884 consider this row as a candidate when point is at the end
18885 of the visual line represented by this row. This is not
18886 required when scanning back, because max_pos will already
18887 have a much larger value. */
18888 if (CHARPOS (row->end.pos) > max_pos)
18889 INC_BOTH (max_pos, max_bpos);
18890 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18891 }
18892 else if (CHARPOS (it->eol_pos) > 0)
18893 SET_TEXT_POS (row->maxpos,
18894 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
18895 else if (row->continued_p)
18896 {
18897 /* If max_pos is different from IT's current position, it
18898 means IT->method does not belong to the display element
18899 at max_pos. However, it also means that the display
18900 element at max_pos was displayed in its entirety on this
18901 line, which is equivalent to saying that the next line
18902 starts at the next buffer position. */
18903 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
18904 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18905 else
18906 {
18907 INC_BOTH (max_pos, max_bpos);
18908 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18909 }
18910 }
18911 else if (row->truncated_on_right_p)
18912 /* display_line already called reseat_at_next_visible_line_start,
18913 which puts the iterator at the beginning of the next line, in
18914 the logical order. */
18915 row->maxpos = it->current.pos;
18916 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
18917 /* A line that is entirely from a string/image/stretch... */
18918 row->maxpos = row->minpos;
18919 else
18920 abort ();
18921 }
18922 else
18923 row->maxpos = it->current.pos;
18924 }
18925
18926 /* Construct the glyph row IT->glyph_row in the desired matrix of
18927 IT->w from text at the current position of IT. See dispextern.h
18928 for an overview of struct it. Value is non-zero if
18929 IT->glyph_row displays text, as opposed to a line displaying ZV
18930 only. */
18931
18932 static int
18933 display_line (struct it *it)
18934 {
18935 struct glyph_row *row = it->glyph_row;
18936 Lisp_Object overlay_arrow_string;
18937 struct it wrap_it;
18938 void *wrap_data = NULL;
18939 int may_wrap = 0, wrap_x IF_LINT (= 0);
18940 int wrap_row_used = -1;
18941 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
18942 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
18943 int wrap_row_extra_line_spacing IF_LINT (= 0);
18944 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
18945 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
18946 int cvpos;
18947 EMACS_INT min_pos = ZV + 1, max_pos = 0;
18948 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
18949
18950 /* We always start displaying at hpos zero even if hscrolled. */
18951 xassert (it->hpos == 0 && it->current_x == 0);
18952
18953 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
18954 >= it->w->desired_matrix->nrows)
18955 {
18956 it->w->nrows_scale_factor++;
18957 fonts_changed_p = 1;
18958 return 0;
18959 }
18960
18961 /* Is IT->w showing the region? */
18962 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
18963
18964 /* Clear the result glyph row and enable it. */
18965 prepare_desired_row (row);
18966
18967 row->y = it->current_y;
18968 row->start = it->start;
18969 row->continuation_lines_width = it->continuation_lines_width;
18970 row->displays_text_p = 1;
18971 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
18972 it->starts_in_middle_of_char_p = 0;
18973
18974 /* Arrange the overlays nicely for our purposes. Usually, we call
18975 display_line on only one line at a time, in which case this
18976 can't really hurt too much, or we call it on lines which appear
18977 one after another in the buffer, in which case all calls to
18978 recenter_overlay_lists but the first will be pretty cheap. */
18979 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
18980
18981 /* Move over display elements that are not visible because we are
18982 hscrolled. This may stop at an x-position < IT->first_visible_x
18983 if the first glyph is partially visible or if we hit a line end. */
18984 if (it->current_x < it->first_visible_x)
18985 {
18986 this_line_min_pos = row->start.pos;
18987 move_it_in_display_line_to (it, ZV, it->first_visible_x,
18988 MOVE_TO_POS | MOVE_TO_X);
18989 /* Record the smallest positions seen while we moved over
18990 display elements that are not visible. This is needed by
18991 redisplay_internal for optimizing the case where the cursor
18992 stays inside the same line. The rest of this function only
18993 considers positions that are actually displayed, so
18994 RECORD_MAX_MIN_POS will not otherwise record positions that
18995 are hscrolled to the left of the left edge of the window. */
18996 min_pos = CHARPOS (this_line_min_pos);
18997 min_bpos = BYTEPOS (this_line_min_pos);
18998 }
18999 else
19000 {
19001 /* We only do this when not calling `move_it_in_display_line_to'
19002 above, because move_it_in_display_line_to calls
19003 handle_line_prefix itself. */
19004 handle_line_prefix (it);
19005 }
19006
19007 /* Get the initial row height. This is either the height of the
19008 text hscrolled, if there is any, or zero. */
19009 row->ascent = it->max_ascent;
19010 row->height = it->max_ascent + it->max_descent;
19011 row->phys_ascent = it->max_phys_ascent;
19012 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19013 row->extra_line_spacing = it->max_extra_line_spacing;
19014
19015 /* Utility macro to record max and min buffer positions seen until now. */
19016 #define RECORD_MAX_MIN_POS(IT) \
19017 do \
19018 { \
19019 int composition_p = !STRINGP ((IT)->string) \
19020 && ((IT)->what == IT_COMPOSITION); \
19021 EMACS_INT current_pos = \
19022 composition_p ? (IT)->cmp_it.charpos \
19023 : IT_CHARPOS (*(IT)); \
19024 EMACS_INT current_bpos = \
19025 composition_p ? CHAR_TO_BYTE (current_pos) \
19026 : IT_BYTEPOS (*(IT)); \
19027 if (current_pos < min_pos) \
19028 { \
19029 min_pos = current_pos; \
19030 min_bpos = current_bpos; \
19031 } \
19032 if (IT_CHARPOS (*it) > max_pos) \
19033 { \
19034 max_pos = IT_CHARPOS (*it); \
19035 max_bpos = IT_BYTEPOS (*it); \
19036 } \
19037 } \
19038 while (0)
19039
19040 /* Loop generating characters. The loop is left with IT on the next
19041 character to display. */
19042 while (1)
19043 {
19044 int n_glyphs_before, hpos_before, x_before;
19045 int x, nglyphs;
19046 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19047
19048 /* Retrieve the next thing to display. Value is zero if end of
19049 buffer reached. */
19050 if (!get_next_display_element (it))
19051 {
19052 /* Maybe add a space at the end of this line that is used to
19053 display the cursor there under X. Set the charpos of the
19054 first glyph of blank lines not corresponding to any text
19055 to -1. */
19056 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19057 row->exact_window_width_line_p = 1;
19058 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19059 || row->used[TEXT_AREA] == 0)
19060 {
19061 row->glyphs[TEXT_AREA]->charpos = -1;
19062 row->displays_text_p = 0;
19063
19064 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19065 && (!MINI_WINDOW_P (it->w)
19066 || (minibuf_level && EQ (it->window, minibuf_window))))
19067 row->indicate_empty_line_p = 1;
19068 }
19069
19070 it->continuation_lines_width = 0;
19071 row->ends_at_zv_p = 1;
19072 /* A row that displays right-to-left text must always have
19073 its last face extended all the way to the end of line,
19074 even if this row ends in ZV, because we still write to
19075 the screen left to right. We also need to extend the
19076 last face if the default face is remapped to some
19077 different face, otherwise the functions that clear
19078 portions of the screen will clear with the default face's
19079 background color. */
19080 if (row->reversed_p
19081 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19082 extend_face_to_end_of_line (it);
19083 break;
19084 }
19085
19086 /* Now, get the metrics of what we want to display. This also
19087 generates glyphs in `row' (which is IT->glyph_row). */
19088 n_glyphs_before = row->used[TEXT_AREA];
19089 x = it->current_x;
19090
19091 /* Remember the line height so far in case the next element doesn't
19092 fit on the line. */
19093 if (it->line_wrap != TRUNCATE)
19094 {
19095 ascent = it->max_ascent;
19096 descent = it->max_descent;
19097 phys_ascent = it->max_phys_ascent;
19098 phys_descent = it->max_phys_descent;
19099
19100 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19101 {
19102 if (IT_DISPLAYING_WHITESPACE (it))
19103 may_wrap = 1;
19104 else if (may_wrap)
19105 {
19106 SAVE_IT (wrap_it, *it, wrap_data);
19107 wrap_x = x;
19108 wrap_row_used = row->used[TEXT_AREA];
19109 wrap_row_ascent = row->ascent;
19110 wrap_row_height = row->height;
19111 wrap_row_phys_ascent = row->phys_ascent;
19112 wrap_row_phys_height = row->phys_height;
19113 wrap_row_extra_line_spacing = row->extra_line_spacing;
19114 wrap_row_min_pos = min_pos;
19115 wrap_row_min_bpos = min_bpos;
19116 wrap_row_max_pos = max_pos;
19117 wrap_row_max_bpos = max_bpos;
19118 may_wrap = 0;
19119 }
19120 }
19121 }
19122
19123 PRODUCE_GLYPHS (it);
19124
19125 /* If this display element was in marginal areas, continue with
19126 the next one. */
19127 if (it->area != TEXT_AREA)
19128 {
19129 row->ascent = max (row->ascent, it->max_ascent);
19130 row->height = max (row->height, it->max_ascent + it->max_descent);
19131 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19132 row->phys_height = max (row->phys_height,
19133 it->max_phys_ascent + it->max_phys_descent);
19134 row->extra_line_spacing = max (row->extra_line_spacing,
19135 it->max_extra_line_spacing);
19136 set_iterator_to_next (it, 1);
19137 continue;
19138 }
19139
19140 /* Does the display element fit on the line? If we truncate
19141 lines, we should draw past the right edge of the window. If
19142 we don't truncate, we want to stop so that we can display the
19143 continuation glyph before the right margin. If lines are
19144 continued, there are two possible strategies for characters
19145 resulting in more than 1 glyph (e.g. tabs): Display as many
19146 glyphs as possible in this line and leave the rest for the
19147 continuation line, or display the whole element in the next
19148 line. Original redisplay did the former, so we do it also. */
19149 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19150 hpos_before = it->hpos;
19151 x_before = x;
19152
19153 if (/* Not a newline. */
19154 nglyphs > 0
19155 /* Glyphs produced fit entirely in the line. */
19156 && it->current_x < it->last_visible_x)
19157 {
19158 it->hpos += nglyphs;
19159 row->ascent = max (row->ascent, it->max_ascent);
19160 row->height = max (row->height, it->max_ascent + it->max_descent);
19161 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19162 row->phys_height = max (row->phys_height,
19163 it->max_phys_ascent + it->max_phys_descent);
19164 row->extra_line_spacing = max (row->extra_line_spacing,
19165 it->max_extra_line_spacing);
19166 if (it->current_x - it->pixel_width < it->first_visible_x)
19167 row->x = x - it->first_visible_x;
19168 /* Record the maximum and minimum buffer positions seen so
19169 far in glyphs that will be displayed by this row. */
19170 if (it->bidi_p)
19171 RECORD_MAX_MIN_POS (it);
19172 }
19173 else
19174 {
19175 int i, new_x;
19176 struct glyph *glyph;
19177
19178 for (i = 0; i < nglyphs; ++i, x = new_x)
19179 {
19180 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19181 new_x = x + glyph->pixel_width;
19182
19183 if (/* Lines are continued. */
19184 it->line_wrap != TRUNCATE
19185 && (/* Glyph doesn't fit on the line. */
19186 new_x > it->last_visible_x
19187 /* Or it fits exactly on a window system frame. */
19188 || (new_x == it->last_visible_x
19189 && FRAME_WINDOW_P (it->f))))
19190 {
19191 /* End of a continued line. */
19192
19193 if (it->hpos == 0
19194 || (new_x == it->last_visible_x
19195 && FRAME_WINDOW_P (it->f)))
19196 {
19197 /* Current glyph is the only one on the line or
19198 fits exactly on the line. We must continue
19199 the line because we can't draw the cursor
19200 after the glyph. */
19201 row->continued_p = 1;
19202 it->current_x = new_x;
19203 it->continuation_lines_width += new_x;
19204 ++it->hpos;
19205 if (i == nglyphs - 1)
19206 {
19207 /* If line-wrap is on, check if a previous
19208 wrap point was found. */
19209 if (wrap_row_used > 0
19210 /* Even if there is a previous wrap
19211 point, continue the line here as
19212 usual, if (i) the previous character
19213 was a space or tab AND (ii) the
19214 current character is not. */
19215 && (!may_wrap
19216 || IT_DISPLAYING_WHITESPACE (it)))
19217 goto back_to_wrap;
19218
19219 /* Record the maximum and minimum buffer
19220 positions seen so far in glyphs that will be
19221 displayed by this row. */
19222 if (it->bidi_p)
19223 RECORD_MAX_MIN_POS (it);
19224 set_iterator_to_next (it, 1);
19225 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19226 {
19227 if (!get_next_display_element (it))
19228 {
19229 row->exact_window_width_line_p = 1;
19230 it->continuation_lines_width = 0;
19231 row->continued_p = 0;
19232 row->ends_at_zv_p = 1;
19233 }
19234 else if (ITERATOR_AT_END_OF_LINE_P (it))
19235 {
19236 row->continued_p = 0;
19237 row->exact_window_width_line_p = 1;
19238 }
19239 }
19240 }
19241 else if (it->bidi_p)
19242 RECORD_MAX_MIN_POS (it);
19243 }
19244 else if (CHAR_GLYPH_PADDING_P (*glyph)
19245 && !FRAME_WINDOW_P (it->f))
19246 {
19247 /* A padding glyph that doesn't fit on this line.
19248 This means the whole character doesn't fit
19249 on the line. */
19250 if (row->reversed_p)
19251 unproduce_glyphs (it, row->used[TEXT_AREA]
19252 - n_glyphs_before);
19253 row->used[TEXT_AREA] = n_glyphs_before;
19254
19255 /* Fill the rest of the row with continuation
19256 glyphs like in 20.x. */
19257 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19258 < row->glyphs[1 + TEXT_AREA])
19259 produce_special_glyphs (it, IT_CONTINUATION);
19260
19261 row->continued_p = 1;
19262 it->current_x = x_before;
19263 it->continuation_lines_width += x_before;
19264
19265 /* Restore the height to what it was before the
19266 element not fitting on the line. */
19267 it->max_ascent = ascent;
19268 it->max_descent = descent;
19269 it->max_phys_ascent = phys_ascent;
19270 it->max_phys_descent = phys_descent;
19271 }
19272 else if (wrap_row_used > 0)
19273 {
19274 back_to_wrap:
19275 if (row->reversed_p)
19276 unproduce_glyphs (it,
19277 row->used[TEXT_AREA] - wrap_row_used);
19278 RESTORE_IT (it, &wrap_it, wrap_data);
19279 it->continuation_lines_width += wrap_x;
19280 row->used[TEXT_AREA] = wrap_row_used;
19281 row->ascent = wrap_row_ascent;
19282 row->height = wrap_row_height;
19283 row->phys_ascent = wrap_row_phys_ascent;
19284 row->phys_height = wrap_row_phys_height;
19285 row->extra_line_spacing = wrap_row_extra_line_spacing;
19286 min_pos = wrap_row_min_pos;
19287 min_bpos = wrap_row_min_bpos;
19288 max_pos = wrap_row_max_pos;
19289 max_bpos = wrap_row_max_bpos;
19290 row->continued_p = 1;
19291 row->ends_at_zv_p = 0;
19292 row->exact_window_width_line_p = 0;
19293 it->continuation_lines_width += x;
19294
19295 /* Make sure that a non-default face is extended
19296 up to the right margin of the window. */
19297 extend_face_to_end_of_line (it);
19298 }
19299 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19300 {
19301 /* A TAB that extends past the right edge of the
19302 window. This produces a single glyph on
19303 window system frames. We leave the glyph in
19304 this row and let it fill the row, but don't
19305 consume the TAB. */
19306 it->continuation_lines_width += it->last_visible_x;
19307 row->ends_in_middle_of_char_p = 1;
19308 row->continued_p = 1;
19309 glyph->pixel_width = it->last_visible_x - x;
19310 it->starts_in_middle_of_char_p = 1;
19311 }
19312 else
19313 {
19314 /* Something other than a TAB that draws past
19315 the right edge of the window. Restore
19316 positions to values before the element. */
19317 if (row->reversed_p)
19318 unproduce_glyphs (it, row->used[TEXT_AREA]
19319 - (n_glyphs_before + i));
19320 row->used[TEXT_AREA] = n_glyphs_before + i;
19321
19322 /* Display continuation glyphs. */
19323 if (!FRAME_WINDOW_P (it->f))
19324 produce_special_glyphs (it, IT_CONTINUATION);
19325 row->continued_p = 1;
19326
19327 it->current_x = x_before;
19328 it->continuation_lines_width += x;
19329 extend_face_to_end_of_line (it);
19330
19331 if (nglyphs > 1 && i > 0)
19332 {
19333 row->ends_in_middle_of_char_p = 1;
19334 it->starts_in_middle_of_char_p = 1;
19335 }
19336
19337 /* Restore the height to what it was before the
19338 element not fitting on the line. */
19339 it->max_ascent = ascent;
19340 it->max_descent = descent;
19341 it->max_phys_ascent = phys_ascent;
19342 it->max_phys_descent = phys_descent;
19343 }
19344
19345 break;
19346 }
19347 else if (new_x > it->first_visible_x)
19348 {
19349 /* Increment number of glyphs actually displayed. */
19350 ++it->hpos;
19351
19352 /* Record the maximum and minimum buffer positions
19353 seen so far in glyphs that will be displayed by
19354 this row. */
19355 if (it->bidi_p)
19356 RECORD_MAX_MIN_POS (it);
19357
19358 if (x < it->first_visible_x)
19359 /* Glyph is partially visible, i.e. row starts at
19360 negative X position. */
19361 row->x = x - it->first_visible_x;
19362 }
19363 else
19364 {
19365 /* Glyph is completely off the left margin of the
19366 window. This should not happen because of the
19367 move_it_in_display_line at the start of this
19368 function, unless the text display area of the
19369 window is empty. */
19370 xassert (it->first_visible_x <= it->last_visible_x);
19371 }
19372 }
19373 /* Even if this display element produced no glyphs at all,
19374 we want to record its position. */
19375 if (it->bidi_p && nglyphs == 0)
19376 RECORD_MAX_MIN_POS (it);
19377
19378 row->ascent = max (row->ascent, it->max_ascent);
19379 row->height = max (row->height, it->max_ascent + it->max_descent);
19380 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19381 row->phys_height = max (row->phys_height,
19382 it->max_phys_ascent + it->max_phys_descent);
19383 row->extra_line_spacing = max (row->extra_line_spacing,
19384 it->max_extra_line_spacing);
19385
19386 /* End of this display line if row is continued. */
19387 if (row->continued_p || row->ends_at_zv_p)
19388 break;
19389 }
19390
19391 at_end_of_line:
19392 /* Is this a line end? If yes, we're also done, after making
19393 sure that a non-default face is extended up to the right
19394 margin of the window. */
19395 if (ITERATOR_AT_END_OF_LINE_P (it))
19396 {
19397 int used_before = row->used[TEXT_AREA];
19398
19399 row->ends_in_newline_from_string_p = STRINGP (it->object);
19400
19401 /* Add a space at the end of the line that is used to
19402 display the cursor there. */
19403 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19404 append_space_for_newline (it, 0);
19405
19406 /* Extend the face to the end of the line. */
19407 extend_face_to_end_of_line (it);
19408
19409 /* Make sure we have the position. */
19410 if (used_before == 0)
19411 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19412
19413 /* Record the position of the newline, for use in
19414 find_row_edges. */
19415 it->eol_pos = it->current.pos;
19416
19417 /* Consume the line end. This skips over invisible lines. */
19418 set_iterator_to_next (it, 1);
19419 it->continuation_lines_width = 0;
19420 break;
19421 }
19422
19423 /* Proceed with next display element. Note that this skips
19424 over lines invisible because of selective display. */
19425 set_iterator_to_next (it, 1);
19426
19427 /* If we truncate lines, we are done when the last displayed
19428 glyphs reach past the right margin of the window. */
19429 if (it->line_wrap == TRUNCATE
19430 && (FRAME_WINDOW_P (it->f)
19431 ? (it->current_x >= it->last_visible_x)
19432 : (it->current_x > it->last_visible_x)))
19433 {
19434 /* Maybe add truncation glyphs. */
19435 if (!FRAME_WINDOW_P (it->f))
19436 {
19437 int i, n;
19438
19439 if (!row->reversed_p)
19440 {
19441 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19442 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19443 break;
19444 }
19445 else
19446 {
19447 for (i = 0; i < row->used[TEXT_AREA]; i++)
19448 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19449 break;
19450 /* Remove any padding glyphs at the front of ROW, to
19451 make room for the truncation glyphs we will be
19452 adding below. The loop below always inserts at
19453 least one truncation glyph, so also remove the
19454 last glyph added to ROW. */
19455 unproduce_glyphs (it, i + 1);
19456 /* Adjust i for the loop below. */
19457 i = row->used[TEXT_AREA] - (i + 1);
19458 }
19459
19460 for (n = row->used[TEXT_AREA]; i < n; ++i)
19461 {
19462 row->used[TEXT_AREA] = i;
19463 produce_special_glyphs (it, IT_TRUNCATION);
19464 }
19465 }
19466 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19467 {
19468 /* Don't truncate if we can overflow newline into fringe. */
19469 if (!get_next_display_element (it))
19470 {
19471 it->continuation_lines_width = 0;
19472 row->ends_at_zv_p = 1;
19473 row->exact_window_width_line_p = 1;
19474 break;
19475 }
19476 if (ITERATOR_AT_END_OF_LINE_P (it))
19477 {
19478 row->exact_window_width_line_p = 1;
19479 goto at_end_of_line;
19480 }
19481 }
19482
19483 row->truncated_on_right_p = 1;
19484 it->continuation_lines_width = 0;
19485 reseat_at_next_visible_line_start (it, 0);
19486 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19487 it->hpos = hpos_before;
19488 it->current_x = x_before;
19489 break;
19490 }
19491 }
19492
19493 if (wrap_data)
19494 bidi_unshelve_cache (wrap_data, 1);
19495
19496 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19497 at the left window margin. */
19498 if (it->first_visible_x
19499 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19500 {
19501 if (!FRAME_WINDOW_P (it->f))
19502 insert_left_trunc_glyphs (it);
19503 row->truncated_on_left_p = 1;
19504 }
19505
19506 /* Remember the position at which this line ends.
19507
19508 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19509 cannot be before the call to find_row_edges below, since that is
19510 where these positions are determined. */
19511 row->end = it->current;
19512 if (!it->bidi_p)
19513 {
19514 row->minpos = row->start.pos;
19515 row->maxpos = row->end.pos;
19516 }
19517 else
19518 {
19519 /* ROW->minpos and ROW->maxpos must be the smallest and
19520 `1 + the largest' buffer positions in ROW. But if ROW was
19521 bidi-reordered, these two positions can be anywhere in the
19522 row, so we must determine them now. */
19523 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19524 }
19525
19526 /* If the start of this line is the overlay arrow-position, then
19527 mark this glyph row as the one containing the overlay arrow.
19528 This is clearly a mess with variable size fonts. It would be
19529 better to let it be displayed like cursors under X. */
19530 if ((row->displays_text_p || !overlay_arrow_seen)
19531 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19532 !NILP (overlay_arrow_string)))
19533 {
19534 /* Overlay arrow in window redisplay is a fringe bitmap. */
19535 if (STRINGP (overlay_arrow_string))
19536 {
19537 struct glyph_row *arrow_row
19538 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19539 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19540 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19541 struct glyph *p = row->glyphs[TEXT_AREA];
19542 struct glyph *p2, *end;
19543
19544 /* Copy the arrow glyphs. */
19545 while (glyph < arrow_end)
19546 *p++ = *glyph++;
19547
19548 /* Throw away padding glyphs. */
19549 p2 = p;
19550 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19551 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19552 ++p2;
19553 if (p2 > p)
19554 {
19555 while (p2 < end)
19556 *p++ = *p2++;
19557 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19558 }
19559 }
19560 else
19561 {
19562 xassert (INTEGERP (overlay_arrow_string));
19563 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19564 }
19565 overlay_arrow_seen = 1;
19566 }
19567
19568 /* Highlight trailing whitespace. */
19569 if (!NILP (Vshow_trailing_whitespace))
19570 highlight_trailing_whitespace (it->f, it->glyph_row);
19571
19572 /* Compute pixel dimensions of this line. */
19573 compute_line_metrics (it);
19574
19575 /* Implementation note: No changes in the glyphs of ROW or in their
19576 faces can be done past this point, because compute_line_metrics
19577 computes ROW's hash value and stores it within the glyph_row
19578 structure. */
19579
19580 /* Record whether this row ends inside an ellipsis. */
19581 row->ends_in_ellipsis_p
19582 = (it->method == GET_FROM_DISPLAY_VECTOR
19583 && it->ellipsis_p);
19584
19585 /* Save fringe bitmaps in this row. */
19586 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19587 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19588 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19589 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19590
19591 it->left_user_fringe_bitmap = 0;
19592 it->left_user_fringe_face_id = 0;
19593 it->right_user_fringe_bitmap = 0;
19594 it->right_user_fringe_face_id = 0;
19595
19596 /* Maybe set the cursor. */
19597 cvpos = it->w->cursor.vpos;
19598 if ((cvpos < 0
19599 /* In bidi-reordered rows, keep checking for proper cursor
19600 position even if one has been found already, because buffer
19601 positions in such rows change non-linearly with ROW->VPOS,
19602 when a line is continued. One exception: when we are at ZV,
19603 display cursor on the first suitable glyph row, since all
19604 the empty rows after that also have their position set to ZV. */
19605 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19606 lines' rows is implemented for bidi-reordered rows. */
19607 || (it->bidi_p
19608 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19609 && PT >= MATRIX_ROW_START_CHARPOS (row)
19610 && PT <= MATRIX_ROW_END_CHARPOS (row)
19611 && cursor_row_p (row))
19612 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19613
19614 /* Prepare for the next line. This line starts horizontally at (X
19615 HPOS) = (0 0). Vertical positions are incremented. As a
19616 convenience for the caller, IT->glyph_row is set to the next
19617 row to be used. */
19618 it->current_x = it->hpos = 0;
19619 it->current_y += row->height;
19620 SET_TEXT_POS (it->eol_pos, 0, 0);
19621 ++it->vpos;
19622 ++it->glyph_row;
19623 /* The next row should by default use the same value of the
19624 reversed_p flag as this one. set_iterator_to_next decides when
19625 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19626 the flag accordingly. */
19627 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19628 it->glyph_row->reversed_p = row->reversed_p;
19629 it->start = row->end;
19630 return row->displays_text_p;
19631
19632 #undef RECORD_MAX_MIN_POS
19633 }
19634
19635 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19636 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19637 doc: /* Return paragraph direction at point in BUFFER.
19638 Value is either `left-to-right' or `right-to-left'.
19639 If BUFFER is omitted or nil, it defaults to the current buffer.
19640
19641 Paragraph direction determines how the text in the paragraph is displayed.
19642 In left-to-right paragraphs, text begins at the left margin of the window
19643 and the reading direction is generally left to right. In right-to-left
19644 paragraphs, text begins at the right margin and is read from right to left.
19645
19646 See also `bidi-paragraph-direction'. */)
19647 (Lisp_Object buffer)
19648 {
19649 struct buffer *buf = current_buffer;
19650 struct buffer *old = buf;
19651
19652 if (! NILP (buffer))
19653 {
19654 CHECK_BUFFER (buffer);
19655 buf = XBUFFER (buffer);
19656 }
19657
19658 if (NILP (BVAR (buf, bidi_display_reordering))
19659 || NILP (BVAR (buf, enable_multibyte_characters))
19660 /* When we are loading loadup.el, the character property tables
19661 needed for bidi iteration are not yet available. */
19662 || !NILP (Vpurify_flag))
19663 return Qleft_to_right;
19664 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19665 return BVAR (buf, bidi_paragraph_direction);
19666 else
19667 {
19668 /* Determine the direction from buffer text. We could try to
19669 use current_matrix if it is up to date, but this seems fast
19670 enough as it is. */
19671 struct bidi_it itb;
19672 EMACS_INT pos = BUF_PT (buf);
19673 EMACS_INT bytepos = BUF_PT_BYTE (buf);
19674 int c;
19675 void *itb_data = bidi_shelve_cache ();
19676
19677 set_buffer_temp (buf);
19678 /* bidi_paragraph_init finds the base direction of the paragraph
19679 by searching forward from paragraph start. We need the base
19680 direction of the current or _previous_ paragraph, so we need
19681 to make sure we are within that paragraph. To that end, find
19682 the previous non-empty line. */
19683 if (pos >= ZV && pos > BEGV)
19684 {
19685 pos--;
19686 bytepos = CHAR_TO_BYTE (pos);
19687 }
19688 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19689 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19690 {
19691 while ((c = FETCH_BYTE (bytepos)) == '\n'
19692 || c == ' ' || c == '\t' || c == '\f')
19693 {
19694 if (bytepos <= BEGV_BYTE)
19695 break;
19696 bytepos--;
19697 pos--;
19698 }
19699 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19700 bytepos--;
19701 }
19702 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19703 itb.paragraph_dir = NEUTRAL_DIR;
19704 itb.string.s = NULL;
19705 itb.string.lstring = Qnil;
19706 itb.string.bufpos = 0;
19707 itb.string.unibyte = 0;
19708 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19709 bidi_unshelve_cache (itb_data, 0);
19710 set_buffer_temp (old);
19711 switch (itb.paragraph_dir)
19712 {
19713 case L2R:
19714 return Qleft_to_right;
19715 break;
19716 case R2L:
19717 return Qright_to_left;
19718 break;
19719 default:
19720 abort ();
19721 }
19722 }
19723 }
19724
19725
19726 \f
19727 /***********************************************************************
19728 Menu Bar
19729 ***********************************************************************/
19730
19731 /* Redisplay the menu bar in the frame for window W.
19732
19733 The menu bar of X frames that don't have X toolkit support is
19734 displayed in a special window W->frame->menu_bar_window.
19735
19736 The menu bar of terminal frames is treated specially as far as
19737 glyph matrices are concerned. Menu bar lines are not part of
19738 windows, so the update is done directly on the frame matrix rows
19739 for the menu bar. */
19740
19741 static void
19742 display_menu_bar (struct window *w)
19743 {
19744 struct frame *f = XFRAME (WINDOW_FRAME (w));
19745 struct it it;
19746 Lisp_Object items;
19747 int i;
19748
19749 /* Don't do all this for graphical frames. */
19750 #ifdef HAVE_NTGUI
19751 if (FRAME_W32_P (f))
19752 return;
19753 #endif
19754 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19755 if (FRAME_X_P (f))
19756 return;
19757 #endif
19758
19759 #ifdef HAVE_NS
19760 if (FRAME_NS_P (f))
19761 return;
19762 #endif /* HAVE_NS */
19763
19764 #ifdef USE_X_TOOLKIT
19765 xassert (!FRAME_WINDOW_P (f));
19766 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19767 it.first_visible_x = 0;
19768 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19769 #else /* not USE_X_TOOLKIT */
19770 if (FRAME_WINDOW_P (f))
19771 {
19772 /* Menu bar lines are displayed in the desired matrix of the
19773 dummy window menu_bar_window. */
19774 struct window *menu_w;
19775 xassert (WINDOWP (f->menu_bar_window));
19776 menu_w = XWINDOW (f->menu_bar_window);
19777 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19778 MENU_FACE_ID);
19779 it.first_visible_x = 0;
19780 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19781 }
19782 else
19783 {
19784 /* This is a TTY frame, i.e. character hpos/vpos are used as
19785 pixel x/y. */
19786 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19787 MENU_FACE_ID);
19788 it.first_visible_x = 0;
19789 it.last_visible_x = FRAME_COLS (f);
19790 }
19791 #endif /* not USE_X_TOOLKIT */
19792
19793 /* FIXME: This should be controlled by a user option. See the
19794 comments in redisplay_tool_bar and display_mode_line about
19795 this. */
19796 it.paragraph_embedding = L2R;
19797
19798 if (! mode_line_inverse_video)
19799 /* Force the menu-bar to be displayed in the default face. */
19800 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19801
19802 /* Clear all rows of the menu bar. */
19803 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19804 {
19805 struct glyph_row *row = it.glyph_row + i;
19806 clear_glyph_row (row);
19807 row->enabled_p = 1;
19808 row->full_width_p = 1;
19809 }
19810
19811 /* Display all items of the menu bar. */
19812 items = FRAME_MENU_BAR_ITEMS (it.f);
19813 for (i = 0; i < ASIZE (items); i += 4)
19814 {
19815 Lisp_Object string;
19816
19817 /* Stop at nil string. */
19818 string = AREF (items, i + 1);
19819 if (NILP (string))
19820 break;
19821
19822 /* Remember where item was displayed. */
19823 ASET (items, i + 3, make_number (it.hpos));
19824
19825 /* Display the item, pad with one space. */
19826 if (it.current_x < it.last_visible_x)
19827 display_string (NULL, string, Qnil, 0, 0, &it,
19828 SCHARS (string) + 1, 0, 0, -1);
19829 }
19830
19831 /* Fill out the line with spaces. */
19832 if (it.current_x < it.last_visible_x)
19833 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19834
19835 /* Compute the total height of the lines. */
19836 compute_line_metrics (&it);
19837 }
19838
19839
19840 \f
19841 /***********************************************************************
19842 Mode Line
19843 ***********************************************************************/
19844
19845 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19846 FORCE is non-zero, redisplay mode lines unconditionally.
19847 Otherwise, redisplay only mode lines that are garbaged. Value is
19848 the number of windows whose mode lines were redisplayed. */
19849
19850 static int
19851 redisplay_mode_lines (Lisp_Object window, int force)
19852 {
19853 int nwindows = 0;
19854
19855 while (!NILP (window))
19856 {
19857 struct window *w = XWINDOW (window);
19858
19859 if (WINDOWP (w->hchild))
19860 nwindows += redisplay_mode_lines (w->hchild, force);
19861 else if (WINDOWP (w->vchild))
19862 nwindows += redisplay_mode_lines (w->vchild, force);
19863 else if (force
19864 || FRAME_GARBAGED_P (XFRAME (w->frame))
19865 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
19866 {
19867 struct text_pos lpoint;
19868 struct buffer *old = current_buffer;
19869
19870 /* Set the window's buffer for the mode line display. */
19871 SET_TEXT_POS (lpoint, PT, PT_BYTE);
19872 set_buffer_internal_1 (XBUFFER (w->buffer));
19873
19874 /* Point refers normally to the selected window. For any
19875 other window, set up appropriate value. */
19876 if (!EQ (window, selected_window))
19877 {
19878 struct text_pos pt;
19879
19880 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
19881 if (CHARPOS (pt) < BEGV)
19882 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
19883 else if (CHARPOS (pt) > (ZV - 1))
19884 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
19885 else
19886 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
19887 }
19888
19889 /* Display mode lines. */
19890 clear_glyph_matrix (w->desired_matrix);
19891 if (display_mode_lines (w))
19892 {
19893 ++nwindows;
19894 w->must_be_updated_p = 1;
19895 }
19896
19897 /* Restore old settings. */
19898 set_buffer_internal_1 (old);
19899 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
19900 }
19901
19902 window = w->next;
19903 }
19904
19905 return nwindows;
19906 }
19907
19908
19909 /* Display the mode and/or header line of window W. Value is the
19910 sum number of mode lines and header lines displayed. */
19911
19912 static int
19913 display_mode_lines (struct window *w)
19914 {
19915 Lisp_Object old_selected_window, old_selected_frame;
19916 int n = 0;
19917
19918 old_selected_frame = selected_frame;
19919 selected_frame = w->frame;
19920 old_selected_window = selected_window;
19921 XSETWINDOW (selected_window, w);
19922
19923 /* These will be set while the mode line specs are processed. */
19924 line_number_displayed = 0;
19925 w->column_number_displayed = Qnil;
19926
19927 if (WINDOW_WANTS_MODELINE_P (w))
19928 {
19929 struct window *sel_w = XWINDOW (old_selected_window);
19930
19931 /* Select mode line face based on the real selected window. */
19932 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
19933 BVAR (current_buffer, mode_line_format));
19934 ++n;
19935 }
19936
19937 if (WINDOW_WANTS_HEADER_LINE_P (w))
19938 {
19939 display_mode_line (w, HEADER_LINE_FACE_ID,
19940 BVAR (current_buffer, header_line_format));
19941 ++n;
19942 }
19943
19944 selected_frame = old_selected_frame;
19945 selected_window = old_selected_window;
19946 return n;
19947 }
19948
19949
19950 /* Display mode or header line of window W. FACE_ID specifies which
19951 line to display; it is either MODE_LINE_FACE_ID or
19952 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
19953 display. Value is the pixel height of the mode/header line
19954 displayed. */
19955
19956 static int
19957 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
19958 {
19959 struct it it;
19960 struct face *face;
19961 int count = SPECPDL_INDEX ();
19962
19963 init_iterator (&it, w, -1, -1, NULL, face_id);
19964 /* Don't extend on a previously drawn mode-line.
19965 This may happen if called from pos_visible_p. */
19966 it.glyph_row->enabled_p = 0;
19967 prepare_desired_row (it.glyph_row);
19968
19969 it.glyph_row->mode_line_p = 1;
19970
19971 if (! mode_line_inverse_video)
19972 /* Force the mode-line to be displayed in the default face. */
19973 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19974
19975 /* FIXME: This should be controlled by a user option. But
19976 supporting such an option is not trivial, since the mode line is
19977 made up of many separate strings. */
19978 it.paragraph_embedding = L2R;
19979
19980 record_unwind_protect (unwind_format_mode_line,
19981 format_mode_line_unwind_data (NULL, Qnil, 0));
19982
19983 mode_line_target = MODE_LINE_DISPLAY;
19984
19985 /* Temporarily make frame's keyboard the current kboard so that
19986 kboard-local variables in the mode_line_format will get the right
19987 values. */
19988 push_kboard (FRAME_KBOARD (it.f));
19989 record_unwind_save_match_data ();
19990 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19991 pop_kboard ();
19992
19993 unbind_to (count, Qnil);
19994
19995 /* Fill up with spaces. */
19996 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
19997
19998 compute_line_metrics (&it);
19999 it.glyph_row->full_width_p = 1;
20000 it.glyph_row->continued_p = 0;
20001 it.glyph_row->truncated_on_left_p = 0;
20002 it.glyph_row->truncated_on_right_p = 0;
20003
20004 /* Make a 3D mode-line have a shadow at its right end. */
20005 face = FACE_FROM_ID (it.f, face_id);
20006 extend_face_to_end_of_line (&it);
20007 if (face->box != FACE_NO_BOX)
20008 {
20009 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20010 + it.glyph_row->used[TEXT_AREA] - 1);
20011 last->right_box_line_p = 1;
20012 }
20013
20014 return it.glyph_row->height;
20015 }
20016
20017 /* Move element ELT in LIST to the front of LIST.
20018 Return the updated list. */
20019
20020 static Lisp_Object
20021 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20022 {
20023 register Lisp_Object tail, prev;
20024 register Lisp_Object tem;
20025
20026 tail = list;
20027 prev = Qnil;
20028 while (CONSP (tail))
20029 {
20030 tem = XCAR (tail);
20031
20032 if (EQ (elt, tem))
20033 {
20034 /* Splice out the link TAIL. */
20035 if (NILP (prev))
20036 list = XCDR (tail);
20037 else
20038 Fsetcdr (prev, XCDR (tail));
20039
20040 /* Now make it the first. */
20041 Fsetcdr (tail, list);
20042 return tail;
20043 }
20044 else
20045 prev = tail;
20046 tail = XCDR (tail);
20047 QUIT;
20048 }
20049
20050 /* Not found--return unchanged LIST. */
20051 return list;
20052 }
20053
20054 /* Contribute ELT to the mode line for window IT->w. How it
20055 translates into text depends on its data type.
20056
20057 IT describes the display environment in which we display, as usual.
20058
20059 DEPTH is the depth in recursion. It is used to prevent
20060 infinite recursion here.
20061
20062 FIELD_WIDTH is the number of characters the display of ELT should
20063 occupy in the mode line, and PRECISION is the maximum number of
20064 characters to display from ELT's representation. See
20065 display_string for details.
20066
20067 Returns the hpos of the end of the text generated by ELT.
20068
20069 PROPS is a property list to add to any string we encounter.
20070
20071 If RISKY is nonzero, remove (disregard) any properties in any string
20072 we encounter, and ignore :eval and :propertize.
20073
20074 The global variable `mode_line_target' determines whether the
20075 output is passed to `store_mode_line_noprop',
20076 `store_mode_line_string', or `display_string'. */
20077
20078 static int
20079 display_mode_element (struct it *it, int depth, int field_width, int precision,
20080 Lisp_Object elt, Lisp_Object props, int risky)
20081 {
20082 int n = 0, field, prec;
20083 int literal = 0;
20084
20085 tail_recurse:
20086 if (depth > 100)
20087 elt = build_string ("*too-deep*");
20088
20089 depth++;
20090
20091 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20092 {
20093 case Lisp_String:
20094 {
20095 /* A string: output it and check for %-constructs within it. */
20096 unsigned char c;
20097 EMACS_INT offset = 0;
20098
20099 if (SCHARS (elt) > 0
20100 && (!NILP (props) || risky))
20101 {
20102 Lisp_Object oprops, aelt;
20103 oprops = Ftext_properties_at (make_number (0), elt);
20104
20105 /* If the starting string's properties are not what
20106 we want, translate the string. Also, if the string
20107 is risky, do that anyway. */
20108
20109 if (NILP (Fequal (props, oprops)) || risky)
20110 {
20111 /* If the starting string has properties,
20112 merge the specified ones onto the existing ones. */
20113 if (! NILP (oprops) && !risky)
20114 {
20115 Lisp_Object tem;
20116
20117 oprops = Fcopy_sequence (oprops);
20118 tem = props;
20119 while (CONSP (tem))
20120 {
20121 oprops = Fplist_put (oprops, XCAR (tem),
20122 XCAR (XCDR (tem)));
20123 tem = XCDR (XCDR (tem));
20124 }
20125 props = oprops;
20126 }
20127
20128 aelt = Fassoc (elt, mode_line_proptrans_alist);
20129 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20130 {
20131 /* AELT is what we want. Move it to the front
20132 without consing. */
20133 elt = XCAR (aelt);
20134 mode_line_proptrans_alist
20135 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20136 }
20137 else
20138 {
20139 Lisp_Object tem;
20140
20141 /* If AELT has the wrong props, it is useless.
20142 so get rid of it. */
20143 if (! NILP (aelt))
20144 mode_line_proptrans_alist
20145 = Fdelq (aelt, mode_line_proptrans_alist);
20146
20147 elt = Fcopy_sequence (elt);
20148 Fset_text_properties (make_number (0), Flength (elt),
20149 props, elt);
20150 /* Add this item to mode_line_proptrans_alist. */
20151 mode_line_proptrans_alist
20152 = Fcons (Fcons (elt, props),
20153 mode_line_proptrans_alist);
20154 /* Truncate mode_line_proptrans_alist
20155 to at most 50 elements. */
20156 tem = Fnthcdr (make_number (50),
20157 mode_line_proptrans_alist);
20158 if (! NILP (tem))
20159 XSETCDR (tem, Qnil);
20160 }
20161 }
20162 }
20163
20164 offset = 0;
20165
20166 if (literal)
20167 {
20168 prec = precision - n;
20169 switch (mode_line_target)
20170 {
20171 case MODE_LINE_NOPROP:
20172 case MODE_LINE_TITLE:
20173 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20174 break;
20175 case MODE_LINE_STRING:
20176 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20177 break;
20178 case MODE_LINE_DISPLAY:
20179 n += display_string (NULL, elt, Qnil, 0, 0, it,
20180 0, prec, 0, STRING_MULTIBYTE (elt));
20181 break;
20182 }
20183
20184 break;
20185 }
20186
20187 /* Handle the non-literal case. */
20188
20189 while ((precision <= 0 || n < precision)
20190 && SREF (elt, offset) != 0
20191 && (mode_line_target != MODE_LINE_DISPLAY
20192 || it->current_x < it->last_visible_x))
20193 {
20194 EMACS_INT last_offset = offset;
20195
20196 /* Advance to end of string or next format specifier. */
20197 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20198 ;
20199
20200 if (offset - 1 != last_offset)
20201 {
20202 EMACS_INT nchars, nbytes;
20203
20204 /* Output to end of string or up to '%'. Field width
20205 is length of string. Don't output more than
20206 PRECISION allows us. */
20207 offset--;
20208
20209 prec = c_string_width (SDATA (elt) + last_offset,
20210 offset - last_offset, precision - n,
20211 &nchars, &nbytes);
20212
20213 switch (mode_line_target)
20214 {
20215 case MODE_LINE_NOPROP:
20216 case MODE_LINE_TITLE:
20217 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20218 break;
20219 case MODE_LINE_STRING:
20220 {
20221 EMACS_INT bytepos = last_offset;
20222 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
20223 EMACS_INT endpos = (precision <= 0
20224 ? string_byte_to_char (elt, offset)
20225 : charpos + nchars);
20226
20227 n += store_mode_line_string (NULL,
20228 Fsubstring (elt, make_number (charpos),
20229 make_number (endpos)),
20230 0, 0, 0, Qnil);
20231 }
20232 break;
20233 case MODE_LINE_DISPLAY:
20234 {
20235 EMACS_INT bytepos = last_offset;
20236 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
20237
20238 if (precision <= 0)
20239 nchars = string_byte_to_char (elt, offset) - charpos;
20240 n += display_string (NULL, elt, Qnil, 0, charpos,
20241 it, 0, nchars, 0,
20242 STRING_MULTIBYTE (elt));
20243 }
20244 break;
20245 }
20246 }
20247 else /* c == '%' */
20248 {
20249 EMACS_INT percent_position = offset;
20250
20251 /* Get the specified minimum width. Zero means
20252 don't pad. */
20253 field = 0;
20254 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20255 field = field * 10 + c - '0';
20256
20257 /* Don't pad beyond the total padding allowed. */
20258 if (field_width - n > 0 && field > field_width - n)
20259 field = field_width - n;
20260
20261 /* Note that either PRECISION <= 0 or N < PRECISION. */
20262 prec = precision - n;
20263
20264 if (c == 'M')
20265 n += display_mode_element (it, depth, field, prec,
20266 Vglobal_mode_string, props,
20267 risky);
20268 else if (c != 0)
20269 {
20270 int multibyte;
20271 EMACS_INT bytepos, charpos;
20272 const char *spec;
20273 Lisp_Object string;
20274
20275 bytepos = percent_position;
20276 charpos = (STRING_MULTIBYTE (elt)
20277 ? string_byte_to_char (elt, bytepos)
20278 : bytepos);
20279 spec = decode_mode_spec (it->w, c, field, &string);
20280 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20281
20282 switch (mode_line_target)
20283 {
20284 case MODE_LINE_NOPROP:
20285 case MODE_LINE_TITLE:
20286 n += store_mode_line_noprop (spec, field, prec);
20287 break;
20288 case MODE_LINE_STRING:
20289 {
20290 Lisp_Object tem = build_string (spec);
20291 props = Ftext_properties_at (make_number (charpos), elt);
20292 /* Should only keep face property in props */
20293 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20294 }
20295 break;
20296 case MODE_LINE_DISPLAY:
20297 {
20298 int nglyphs_before, nwritten;
20299
20300 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20301 nwritten = display_string (spec, string, elt,
20302 charpos, 0, it,
20303 field, prec, 0,
20304 multibyte);
20305
20306 /* Assign to the glyphs written above the
20307 string where the `%x' came from, position
20308 of the `%'. */
20309 if (nwritten > 0)
20310 {
20311 struct glyph *glyph
20312 = (it->glyph_row->glyphs[TEXT_AREA]
20313 + nglyphs_before);
20314 int i;
20315
20316 for (i = 0; i < nwritten; ++i)
20317 {
20318 glyph[i].object = elt;
20319 glyph[i].charpos = charpos;
20320 }
20321
20322 n += nwritten;
20323 }
20324 }
20325 break;
20326 }
20327 }
20328 else /* c == 0 */
20329 break;
20330 }
20331 }
20332 }
20333 break;
20334
20335 case Lisp_Symbol:
20336 /* A symbol: process the value of the symbol recursively
20337 as if it appeared here directly. Avoid error if symbol void.
20338 Special case: if value of symbol is a string, output the string
20339 literally. */
20340 {
20341 register Lisp_Object tem;
20342
20343 /* If the variable is not marked as risky to set
20344 then its contents are risky to use. */
20345 if (NILP (Fget (elt, Qrisky_local_variable)))
20346 risky = 1;
20347
20348 tem = Fboundp (elt);
20349 if (!NILP (tem))
20350 {
20351 tem = Fsymbol_value (elt);
20352 /* If value is a string, output that string literally:
20353 don't check for % within it. */
20354 if (STRINGP (tem))
20355 literal = 1;
20356
20357 if (!EQ (tem, elt))
20358 {
20359 /* Give up right away for nil or t. */
20360 elt = tem;
20361 goto tail_recurse;
20362 }
20363 }
20364 }
20365 break;
20366
20367 case Lisp_Cons:
20368 {
20369 register Lisp_Object car, tem;
20370
20371 /* A cons cell: five distinct cases.
20372 If first element is :eval or :propertize, do something special.
20373 If first element is a string or a cons, process all the elements
20374 and effectively concatenate them.
20375 If first element is a negative number, truncate displaying cdr to
20376 at most that many characters. If positive, pad (with spaces)
20377 to at least that many characters.
20378 If first element is a symbol, process the cadr or caddr recursively
20379 according to whether the symbol's value is non-nil or nil. */
20380 car = XCAR (elt);
20381 if (EQ (car, QCeval))
20382 {
20383 /* An element of the form (:eval FORM) means evaluate FORM
20384 and use the result as mode line elements. */
20385
20386 if (risky)
20387 break;
20388
20389 if (CONSP (XCDR (elt)))
20390 {
20391 Lisp_Object spec;
20392 spec = safe_eval (XCAR (XCDR (elt)));
20393 n += display_mode_element (it, depth, field_width - n,
20394 precision - n, spec, props,
20395 risky);
20396 }
20397 }
20398 else if (EQ (car, QCpropertize))
20399 {
20400 /* An element of the form (:propertize ELT PROPS...)
20401 means display ELT but applying properties PROPS. */
20402
20403 if (risky)
20404 break;
20405
20406 if (CONSP (XCDR (elt)))
20407 n += display_mode_element (it, depth, field_width - n,
20408 precision - n, XCAR (XCDR (elt)),
20409 XCDR (XCDR (elt)), risky);
20410 }
20411 else if (SYMBOLP (car))
20412 {
20413 tem = Fboundp (car);
20414 elt = XCDR (elt);
20415 if (!CONSP (elt))
20416 goto invalid;
20417 /* elt is now the cdr, and we know it is a cons cell.
20418 Use its car if CAR has a non-nil value. */
20419 if (!NILP (tem))
20420 {
20421 tem = Fsymbol_value (car);
20422 if (!NILP (tem))
20423 {
20424 elt = XCAR (elt);
20425 goto tail_recurse;
20426 }
20427 }
20428 /* Symbol's value is nil (or symbol is unbound)
20429 Get the cddr of the original list
20430 and if possible find the caddr and use that. */
20431 elt = XCDR (elt);
20432 if (NILP (elt))
20433 break;
20434 else if (!CONSP (elt))
20435 goto invalid;
20436 elt = XCAR (elt);
20437 goto tail_recurse;
20438 }
20439 else if (INTEGERP (car))
20440 {
20441 register int lim = XINT (car);
20442 elt = XCDR (elt);
20443 if (lim < 0)
20444 {
20445 /* Negative int means reduce maximum width. */
20446 if (precision <= 0)
20447 precision = -lim;
20448 else
20449 precision = min (precision, -lim);
20450 }
20451 else if (lim > 0)
20452 {
20453 /* Padding specified. Don't let it be more than
20454 current maximum. */
20455 if (precision > 0)
20456 lim = min (precision, lim);
20457
20458 /* If that's more padding than already wanted, queue it.
20459 But don't reduce padding already specified even if
20460 that is beyond the current truncation point. */
20461 field_width = max (lim, field_width);
20462 }
20463 goto tail_recurse;
20464 }
20465 else if (STRINGP (car) || CONSP (car))
20466 {
20467 Lisp_Object halftail = elt;
20468 int len = 0;
20469
20470 while (CONSP (elt)
20471 && (precision <= 0 || n < precision))
20472 {
20473 n += display_mode_element (it, depth,
20474 /* Do padding only after the last
20475 element in the list. */
20476 (! CONSP (XCDR (elt))
20477 ? field_width - n
20478 : 0),
20479 precision - n, XCAR (elt),
20480 props, risky);
20481 elt = XCDR (elt);
20482 len++;
20483 if ((len & 1) == 0)
20484 halftail = XCDR (halftail);
20485 /* Check for cycle. */
20486 if (EQ (halftail, elt))
20487 break;
20488 }
20489 }
20490 }
20491 break;
20492
20493 default:
20494 invalid:
20495 elt = build_string ("*invalid*");
20496 goto tail_recurse;
20497 }
20498
20499 /* Pad to FIELD_WIDTH. */
20500 if (field_width > 0 && n < field_width)
20501 {
20502 switch (mode_line_target)
20503 {
20504 case MODE_LINE_NOPROP:
20505 case MODE_LINE_TITLE:
20506 n += store_mode_line_noprop ("", field_width - n, 0);
20507 break;
20508 case MODE_LINE_STRING:
20509 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20510 break;
20511 case MODE_LINE_DISPLAY:
20512 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20513 0, 0, 0);
20514 break;
20515 }
20516 }
20517
20518 return n;
20519 }
20520
20521 /* Store a mode-line string element in mode_line_string_list.
20522
20523 If STRING is non-null, display that C string. Otherwise, the Lisp
20524 string LISP_STRING is displayed.
20525
20526 FIELD_WIDTH is the minimum number of output glyphs to produce.
20527 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20528 with spaces. FIELD_WIDTH <= 0 means don't pad.
20529
20530 PRECISION is the maximum number of characters to output from
20531 STRING. PRECISION <= 0 means don't truncate the string.
20532
20533 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20534 properties to the string.
20535
20536 PROPS are the properties to add to the string.
20537 The mode_line_string_face face property is always added to the string.
20538 */
20539
20540 static int
20541 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20542 int field_width, int precision, Lisp_Object props)
20543 {
20544 EMACS_INT len;
20545 int n = 0;
20546
20547 if (string != NULL)
20548 {
20549 len = strlen (string);
20550 if (precision > 0 && len > precision)
20551 len = precision;
20552 lisp_string = make_string (string, len);
20553 if (NILP (props))
20554 props = mode_line_string_face_prop;
20555 else if (!NILP (mode_line_string_face))
20556 {
20557 Lisp_Object face = Fplist_get (props, Qface);
20558 props = Fcopy_sequence (props);
20559 if (NILP (face))
20560 face = mode_line_string_face;
20561 else
20562 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20563 props = Fplist_put (props, Qface, face);
20564 }
20565 Fadd_text_properties (make_number (0), make_number (len),
20566 props, lisp_string);
20567 }
20568 else
20569 {
20570 len = XFASTINT (Flength (lisp_string));
20571 if (precision > 0 && len > precision)
20572 {
20573 len = precision;
20574 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20575 precision = -1;
20576 }
20577 if (!NILP (mode_line_string_face))
20578 {
20579 Lisp_Object face;
20580 if (NILP (props))
20581 props = Ftext_properties_at (make_number (0), lisp_string);
20582 face = Fplist_get (props, Qface);
20583 if (NILP (face))
20584 face = mode_line_string_face;
20585 else
20586 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20587 props = Fcons (Qface, Fcons (face, Qnil));
20588 if (copy_string)
20589 lisp_string = Fcopy_sequence (lisp_string);
20590 }
20591 if (!NILP (props))
20592 Fadd_text_properties (make_number (0), make_number (len),
20593 props, lisp_string);
20594 }
20595
20596 if (len > 0)
20597 {
20598 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20599 n += len;
20600 }
20601
20602 if (field_width > len)
20603 {
20604 field_width -= len;
20605 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20606 if (!NILP (props))
20607 Fadd_text_properties (make_number (0), make_number (field_width),
20608 props, lisp_string);
20609 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20610 n += field_width;
20611 }
20612
20613 return n;
20614 }
20615
20616
20617 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20618 1, 4, 0,
20619 doc: /* Format a string out of a mode line format specification.
20620 First arg FORMAT specifies the mode line format (see `mode-line-format'
20621 for details) to use.
20622
20623 By default, the format is evaluated for the currently selected window.
20624
20625 Optional second arg FACE specifies the face property to put on all
20626 characters for which no face is specified. The value nil means the
20627 default face. The value t means whatever face the window's mode line
20628 currently uses (either `mode-line' or `mode-line-inactive',
20629 depending on whether the window is the selected window or not).
20630 An integer value means the value string has no text
20631 properties.
20632
20633 Optional third and fourth args WINDOW and BUFFER specify the window
20634 and buffer to use as the context for the formatting (defaults
20635 are the selected window and the WINDOW's buffer). */)
20636 (Lisp_Object format, Lisp_Object face,
20637 Lisp_Object window, Lisp_Object buffer)
20638 {
20639 struct it it;
20640 int len;
20641 struct window *w;
20642 struct buffer *old_buffer = NULL;
20643 int face_id;
20644 int no_props = INTEGERP (face);
20645 int count = SPECPDL_INDEX ();
20646 Lisp_Object str;
20647 int string_start = 0;
20648
20649 if (NILP (window))
20650 window = selected_window;
20651 CHECK_WINDOW (window);
20652 w = XWINDOW (window);
20653
20654 if (NILP (buffer))
20655 buffer = w->buffer;
20656 CHECK_BUFFER (buffer);
20657
20658 /* Make formatting the modeline a non-op when noninteractive, otherwise
20659 there will be problems later caused by a partially initialized frame. */
20660 if (NILP (format) || noninteractive)
20661 return empty_unibyte_string;
20662
20663 if (no_props)
20664 face = Qnil;
20665
20666 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20667 : EQ (face, Qt) ? (EQ (window, selected_window)
20668 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20669 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20670 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20671 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20672 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20673 : DEFAULT_FACE_ID;
20674
20675 if (XBUFFER (buffer) != current_buffer)
20676 old_buffer = current_buffer;
20677
20678 /* Save things including mode_line_proptrans_alist,
20679 and set that to nil so that we don't alter the outer value. */
20680 record_unwind_protect (unwind_format_mode_line,
20681 format_mode_line_unwind_data
20682 (old_buffer, selected_window, 1));
20683 mode_line_proptrans_alist = Qnil;
20684
20685 Fselect_window (window, Qt);
20686 if (old_buffer)
20687 set_buffer_internal_1 (XBUFFER (buffer));
20688
20689 init_iterator (&it, w, -1, -1, NULL, face_id);
20690
20691 if (no_props)
20692 {
20693 mode_line_target = MODE_LINE_NOPROP;
20694 mode_line_string_face_prop = Qnil;
20695 mode_line_string_list = Qnil;
20696 string_start = MODE_LINE_NOPROP_LEN (0);
20697 }
20698 else
20699 {
20700 mode_line_target = MODE_LINE_STRING;
20701 mode_line_string_list = Qnil;
20702 mode_line_string_face = face;
20703 mode_line_string_face_prop
20704 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20705 }
20706
20707 push_kboard (FRAME_KBOARD (it.f));
20708 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20709 pop_kboard ();
20710
20711 if (no_props)
20712 {
20713 len = MODE_LINE_NOPROP_LEN (string_start);
20714 str = make_string (mode_line_noprop_buf + string_start, len);
20715 }
20716 else
20717 {
20718 mode_line_string_list = Fnreverse (mode_line_string_list);
20719 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20720 empty_unibyte_string);
20721 }
20722
20723 unbind_to (count, Qnil);
20724 return str;
20725 }
20726
20727 /* Write a null-terminated, right justified decimal representation of
20728 the positive integer D to BUF using a minimal field width WIDTH. */
20729
20730 static void
20731 pint2str (register char *buf, register int width, register EMACS_INT d)
20732 {
20733 register char *p = buf;
20734
20735 if (d <= 0)
20736 *p++ = '0';
20737 else
20738 {
20739 while (d > 0)
20740 {
20741 *p++ = d % 10 + '0';
20742 d /= 10;
20743 }
20744 }
20745
20746 for (width -= (int) (p - buf); width > 0; --width)
20747 *p++ = ' ';
20748 *p-- = '\0';
20749 while (p > buf)
20750 {
20751 d = *buf;
20752 *buf++ = *p;
20753 *p-- = d;
20754 }
20755 }
20756
20757 /* Write a null-terminated, right justified decimal and "human
20758 readable" representation of the nonnegative integer D to BUF using
20759 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20760
20761 static const char power_letter[] =
20762 {
20763 0, /* no letter */
20764 'k', /* kilo */
20765 'M', /* mega */
20766 'G', /* giga */
20767 'T', /* tera */
20768 'P', /* peta */
20769 'E', /* exa */
20770 'Z', /* zetta */
20771 'Y' /* yotta */
20772 };
20773
20774 static void
20775 pint2hrstr (char *buf, int width, EMACS_INT d)
20776 {
20777 /* We aim to represent the nonnegative integer D as
20778 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20779 EMACS_INT quotient = d;
20780 int remainder = 0;
20781 /* -1 means: do not use TENTHS. */
20782 int tenths = -1;
20783 int exponent = 0;
20784
20785 /* Length of QUOTIENT.TENTHS as a string. */
20786 int length;
20787
20788 char * psuffix;
20789 char * p;
20790
20791 if (1000 <= quotient)
20792 {
20793 /* Scale to the appropriate EXPONENT. */
20794 do
20795 {
20796 remainder = quotient % 1000;
20797 quotient /= 1000;
20798 exponent++;
20799 }
20800 while (1000 <= quotient);
20801
20802 /* Round to nearest and decide whether to use TENTHS or not. */
20803 if (quotient <= 9)
20804 {
20805 tenths = remainder / 100;
20806 if (50 <= remainder % 100)
20807 {
20808 if (tenths < 9)
20809 tenths++;
20810 else
20811 {
20812 quotient++;
20813 if (quotient == 10)
20814 tenths = -1;
20815 else
20816 tenths = 0;
20817 }
20818 }
20819 }
20820 else
20821 if (500 <= remainder)
20822 {
20823 if (quotient < 999)
20824 quotient++;
20825 else
20826 {
20827 quotient = 1;
20828 exponent++;
20829 tenths = 0;
20830 }
20831 }
20832 }
20833
20834 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20835 if (tenths == -1 && quotient <= 99)
20836 if (quotient <= 9)
20837 length = 1;
20838 else
20839 length = 2;
20840 else
20841 length = 3;
20842 p = psuffix = buf + max (width, length);
20843
20844 /* Print EXPONENT. */
20845 *psuffix++ = power_letter[exponent];
20846 *psuffix = '\0';
20847
20848 /* Print TENTHS. */
20849 if (tenths >= 0)
20850 {
20851 *--p = '0' + tenths;
20852 *--p = '.';
20853 }
20854
20855 /* Print QUOTIENT. */
20856 do
20857 {
20858 int digit = quotient % 10;
20859 *--p = '0' + digit;
20860 }
20861 while ((quotient /= 10) != 0);
20862
20863 /* Print leading spaces. */
20864 while (buf < p)
20865 *--p = ' ';
20866 }
20867
20868 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
20869 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
20870 type of CODING_SYSTEM. Return updated pointer into BUF. */
20871
20872 static unsigned char invalid_eol_type[] = "(*invalid*)";
20873
20874 static char *
20875 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
20876 {
20877 Lisp_Object val;
20878 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
20879 const unsigned char *eol_str;
20880 int eol_str_len;
20881 /* The EOL conversion we are using. */
20882 Lisp_Object eoltype;
20883
20884 val = CODING_SYSTEM_SPEC (coding_system);
20885 eoltype = Qnil;
20886
20887 if (!VECTORP (val)) /* Not yet decided. */
20888 {
20889 if (multibyte)
20890 *buf++ = '-';
20891 if (eol_flag)
20892 eoltype = eol_mnemonic_undecided;
20893 /* Don't mention EOL conversion if it isn't decided. */
20894 }
20895 else
20896 {
20897 Lisp_Object attrs;
20898 Lisp_Object eolvalue;
20899
20900 attrs = AREF (val, 0);
20901 eolvalue = AREF (val, 2);
20902
20903 if (multibyte)
20904 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
20905
20906 if (eol_flag)
20907 {
20908 /* The EOL conversion that is normal on this system. */
20909
20910 if (NILP (eolvalue)) /* Not yet decided. */
20911 eoltype = eol_mnemonic_undecided;
20912 else if (VECTORP (eolvalue)) /* Not yet decided. */
20913 eoltype = eol_mnemonic_undecided;
20914 else /* eolvalue is Qunix, Qdos, or Qmac. */
20915 eoltype = (EQ (eolvalue, Qunix)
20916 ? eol_mnemonic_unix
20917 : (EQ (eolvalue, Qdos) == 1
20918 ? eol_mnemonic_dos : eol_mnemonic_mac));
20919 }
20920 }
20921
20922 if (eol_flag)
20923 {
20924 /* Mention the EOL conversion if it is not the usual one. */
20925 if (STRINGP (eoltype))
20926 {
20927 eol_str = SDATA (eoltype);
20928 eol_str_len = SBYTES (eoltype);
20929 }
20930 else if (CHARACTERP (eoltype))
20931 {
20932 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
20933 int c = XFASTINT (eoltype);
20934 eol_str_len = CHAR_STRING (c, tmp);
20935 eol_str = tmp;
20936 }
20937 else
20938 {
20939 eol_str = invalid_eol_type;
20940 eol_str_len = sizeof (invalid_eol_type) - 1;
20941 }
20942 memcpy (buf, eol_str, eol_str_len);
20943 buf += eol_str_len;
20944 }
20945
20946 return buf;
20947 }
20948
20949 /* Return a string for the output of a mode line %-spec for window W,
20950 generated by character C. FIELD_WIDTH > 0 means pad the string
20951 returned with spaces to that value. Return a Lisp string in
20952 *STRING if the resulting string is taken from that Lisp string.
20953
20954 Note we operate on the current buffer for most purposes,
20955 the exception being w->base_line_pos. */
20956
20957 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
20958
20959 static const char *
20960 decode_mode_spec (struct window *w, register int c, int field_width,
20961 Lisp_Object *string)
20962 {
20963 Lisp_Object obj;
20964 struct frame *f = XFRAME (WINDOW_FRAME (w));
20965 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
20966 struct buffer *b = current_buffer;
20967
20968 obj = Qnil;
20969 *string = Qnil;
20970
20971 switch (c)
20972 {
20973 case '*':
20974 if (!NILP (BVAR (b, read_only)))
20975 return "%";
20976 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20977 return "*";
20978 return "-";
20979
20980 case '+':
20981 /* This differs from %* only for a modified read-only buffer. */
20982 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20983 return "*";
20984 if (!NILP (BVAR (b, read_only)))
20985 return "%";
20986 return "-";
20987
20988 case '&':
20989 /* This differs from %* in ignoring read-only-ness. */
20990 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20991 return "*";
20992 return "-";
20993
20994 case '%':
20995 return "%";
20996
20997 case '[':
20998 {
20999 int i;
21000 char *p;
21001
21002 if (command_loop_level > 5)
21003 return "[[[... ";
21004 p = decode_mode_spec_buf;
21005 for (i = 0; i < command_loop_level; i++)
21006 *p++ = '[';
21007 *p = 0;
21008 return decode_mode_spec_buf;
21009 }
21010
21011 case ']':
21012 {
21013 int i;
21014 char *p;
21015
21016 if (command_loop_level > 5)
21017 return " ...]]]";
21018 p = decode_mode_spec_buf;
21019 for (i = 0; i < command_loop_level; i++)
21020 *p++ = ']';
21021 *p = 0;
21022 return decode_mode_spec_buf;
21023 }
21024
21025 case '-':
21026 {
21027 register int i;
21028
21029 /* Let lots_of_dashes be a string of infinite length. */
21030 if (mode_line_target == MODE_LINE_NOPROP ||
21031 mode_line_target == MODE_LINE_STRING)
21032 return "--";
21033 if (field_width <= 0
21034 || field_width > sizeof (lots_of_dashes))
21035 {
21036 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21037 decode_mode_spec_buf[i] = '-';
21038 decode_mode_spec_buf[i] = '\0';
21039 return decode_mode_spec_buf;
21040 }
21041 else
21042 return lots_of_dashes;
21043 }
21044
21045 case 'b':
21046 obj = BVAR (b, name);
21047 break;
21048
21049 case 'c':
21050 /* %c and %l are ignored in `frame-title-format'.
21051 (In redisplay_internal, the frame title is drawn _before_ the
21052 windows are updated, so the stuff which depends on actual
21053 window contents (such as %l) may fail to render properly, or
21054 even crash emacs.) */
21055 if (mode_line_target == MODE_LINE_TITLE)
21056 return "";
21057 else
21058 {
21059 EMACS_INT col = current_column ();
21060 w->column_number_displayed = make_number (col);
21061 pint2str (decode_mode_spec_buf, field_width, col);
21062 return decode_mode_spec_buf;
21063 }
21064
21065 case 'e':
21066 #ifndef SYSTEM_MALLOC
21067 {
21068 if (NILP (Vmemory_full))
21069 return "";
21070 else
21071 return "!MEM FULL! ";
21072 }
21073 #else
21074 return "";
21075 #endif
21076
21077 case 'F':
21078 /* %F displays the frame name. */
21079 if (!NILP (f->title))
21080 return SSDATA (f->title);
21081 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21082 return SSDATA (f->name);
21083 return "Emacs";
21084
21085 case 'f':
21086 obj = BVAR (b, filename);
21087 break;
21088
21089 case 'i':
21090 {
21091 EMACS_INT size = ZV - BEGV;
21092 pint2str (decode_mode_spec_buf, field_width, size);
21093 return decode_mode_spec_buf;
21094 }
21095
21096 case 'I':
21097 {
21098 EMACS_INT size = ZV - BEGV;
21099 pint2hrstr (decode_mode_spec_buf, field_width, size);
21100 return decode_mode_spec_buf;
21101 }
21102
21103 case 'l':
21104 {
21105 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
21106 EMACS_INT topline, nlines, height;
21107 EMACS_INT junk;
21108
21109 /* %c and %l are ignored in `frame-title-format'. */
21110 if (mode_line_target == MODE_LINE_TITLE)
21111 return "";
21112
21113 startpos = XMARKER (w->start)->charpos;
21114 startpos_byte = marker_byte_position (w->start);
21115 height = WINDOW_TOTAL_LINES (w);
21116
21117 /* If we decided that this buffer isn't suitable for line numbers,
21118 don't forget that too fast. */
21119 if (EQ (w->base_line_pos, w->buffer))
21120 goto no_value;
21121 /* But do forget it, if the window shows a different buffer now. */
21122 else if (BUFFERP (w->base_line_pos))
21123 w->base_line_pos = Qnil;
21124
21125 /* If the buffer is very big, don't waste time. */
21126 if (INTEGERP (Vline_number_display_limit)
21127 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21128 {
21129 w->base_line_pos = Qnil;
21130 w->base_line_number = Qnil;
21131 goto no_value;
21132 }
21133
21134 if (INTEGERP (w->base_line_number)
21135 && INTEGERP (w->base_line_pos)
21136 && XFASTINT (w->base_line_pos) <= startpos)
21137 {
21138 line = XFASTINT (w->base_line_number);
21139 linepos = XFASTINT (w->base_line_pos);
21140 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21141 }
21142 else
21143 {
21144 line = 1;
21145 linepos = BUF_BEGV (b);
21146 linepos_byte = BUF_BEGV_BYTE (b);
21147 }
21148
21149 /* Count lines from base line to window start position. */
21150 nlines = display_count_lines (linepos_byte,
21151 startpos_byte,
21152 startpos, &junk);
21153
21154 topline = nlines + line;
21155
21156 /* Determine a new base line, if the old one is too close
21157 or too far away, or if we did not have one.
21158 "Too close" means it's plausible a scroll-down would
21159 go back past it. */
21160 if (startpos == BUF_BEGV (b))
21161 {
21162 w->base_line_number = make_number (topline);
21163 w->base_line_pos = make_number (BUF_BEGV (b));
21164 }
21165 else if (nlines < height + 25 || nlines > height * 3 + 50
21166 || linepos == BUF_BEGV (b))
21167 {
21168 EMACS_INT limit = BUF_BEGV (b);
21169 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
21170 EMACS_INT position;
21171 EMACS_INT distance =
21172 (height * 2 + 30) * line_number_display_limit_width;
21173
21174 if (startpos - distance > limit)
21175 {
21176 limit = startpos - distance;
21177 limit_byte = CHAR_TO_BYTE (limit);
21178 }
21179
21180 nlines = display_count_lines (startpos_byte,
21181 limit_byte,
21182 - (height * 2 + 30),
21183 &position);
21184 /* If we couldn't find the lines we wanted within
21185 line_number_display_limit_width chars per line,
21186 give up on line numbers for this window. */
21187 if (position == limit_byte && limit == startpos - distance)
21188 {
21189 w->base_line_pos = w->buffer;
21190 w->base_line_number = Qnil;
21191 goto no_value;
21192 }
21193
21194 w->base_line_number = make_number (topline - nlines);
21195 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21196 }
21197
21198 /* Now count lines from the start pos to point. */
21199 nlines = display_count_lines (startpos_byte,
21200 PT_BYTE, PT, &junk);
21201
21202 /* Record that we did display the line number. */
21203 line_number_displayed = 1;
21204
21205 /* Make the string to show. */
21206 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21207 return decode_mode_spec_buf;
21208 no_value:
21209 {
21210 char* p = decode_mode_spec_buf;
21211 int pad = field_width - 2;
21212 while (pad-- > 0)
21213 *p++ = ' ';
21214 *p++ = '?';
21215 *p++ = '?';
21216 *p = '\0';
21217 return decode_mode_spec_buf;
21218 }
21219 }
21220 break;
21221
21222 case 'm':
21223 obj = BVAR (b, mode_name);
21224 break;
21225
21226 case 'n':
21227 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21228 return " Narrow";
21229 break;
21230
21231 case 'p':
21232 {
21233 EMACS_INT pos = marker_position (w->start);
21234 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
21235
21236 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21237 {
21238 if (pos <= BUF_BEGV (b))
21239 return "All";
21240 else
21241 return "Bottom";
21242 }
21243 else if (pos <= BUF_BEGV (b))
21244 return "Top";
21245 else
21246 {
21247 if (total > 1000000)
21248 /* Do it differently for a large value, to avoid overflow. */
21249 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21250 else
21251 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21252 /* We can't normally display a 3-digit number,
21253 so get us a 2-digit number that is close. */
21254 if (total == 100)
21255 total = 99;
21256 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
21257 return decode_mode_spec_buf;
21258 }
21259 }
21260
21261 /* Display percentage of size above the bottom of the screen. */
21262 case 'P':
21263 {
21264 EMACS_INT toppos = marker_position (w->start);
21265 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21266 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
21267
21268 if (botpos >= BUF_ZV (b))
21269 {
21270 if (toppos <= BUF_BEGV (b))
21271 return "All";
21272 else
21273 return "Bottom";
21274 }
21275 else
21276 {
21277 if (total > 1000000)
21278 /* Do it differently for a large value, to avoid overflow. */
21279 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21280 else
21281 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21282 /* We can't normally display a 3-digit number,
21283 so get us a 2-digit number that is close. */
21284 if (total == 100)
21285 total = 99;
21286 if (toppos <= BUF_BEGV (b))
21287 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
21288 else
21289 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
21290 return decode_mode_spec_buf;
21291 }
21292 }
21293
21294 case 's':
21295 /* status of process */
21296 obj = Fget_buffer_process (Fcurrent_buffer ());
21297 if (NILP (obj))
21298 return "no process";
21299 #ifndef MSDOS
21300 obj = Fsymbol_name (Fprocess_status (obj));
21301 #endif
21302 break;
21303
21304 case '@':
21305 {
21306 int count = inhibit_garbage_collection ();
21307 Lisp_Object val = call1 (intern ("file-remote-p"),
21308 BVAR (current_buffer, directory));
21309 unbind_to (count, Qnil);
21310
21311 if (NILP (val))
21312 return "-";
21313 else
21314 return "@";
21315 }
21316
21317 case 't': /* indicate TEXT or BINARY */
21318 return "T";
21319
21320 case 'z':
21321 /* coding-system (not including end-of-line format) */
21322 case 'Z':
21323 /* coding-system (including end-of-line type) */
21324 {
21325 int eol_flag = (c == 'Z');
21326 char *p = decode_mode_spec_buf;
21327
21328 if (! FRAME_WINDOW_P (f))
21329 {
21330 /* No need to mention EOL here--the terminal never needs
21331 to do EOL conversion. */
21332 p = decode_mode_spec_coding (CODING_ID_NAME
21333 (FRAME_KEYBOARD_CODING (f)->id),
21334 p, 0);
21335 p = decode_mode_spec_coding (CODING_ID_NAME
21336 (FRAME_TERMINAL_CODING (f)->id),
21337 p, 0);
21338 }
21339 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21340 p, eol_flag);
21341
21342 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21343 #ifdef subprocesses
21344 obj = Fget_buffer_process (Fcurrent_buffer ());
21345 if (PROCESSP (obj))
21346 {
21347 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21348 p, eol_flag);
21349 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21350 p, eol_flag);
21351 }
21352 #endif /* subprocesses */
21353 #endif /* 0 */
21354 *p = 0;
21355 return decode_mode_spec_buf;
21356 }
21357 }
21358
21359 if (STRINGP (obj))
21360 {
21361 *string = obj;
21362 return SSDATA (obj);
21363 }
21364 else
21365 return "";
21366 }
21367
21368
21369 /* Count up to COUNT lines starting from START_BYTE.
21370 But don't go beyond LIMIT_BYTE.
21371 Return the number of lines thus found (always nonnegative).
21372
21373 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21374
21375 static EMACS_INT
21376 display_count_lines (EMACS_INT start_byte,
21377 EMACS_INT limit_byte, EMACS_INT count,
21378 EMACS_INT *byte_pos_ptr)
21379 {
21380 register unsigned char *cursor;
21381 unsigned char *base;
21382
21383 register EMACS_INT ceiling;
21384 register unsigned char *ceiling_addr;
21385 EMACS_INT orig_count = count;
21386
21387 /* If we are not in selective display mode,
21388 check only for newlines. */
21389 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21390 && !INTEGERP (BVAR (current_buffer, selective_display)));
21391
21392 if (count > 0)
21393 {
21394 while (start_byte < limit_byte)
21395 {
21396 ceiling = BUFFER_CEILING_OF (start_byte);
21397 ceiling = min (limit_byte - 1, ceiling);
21398 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21399 base = (cursor = BYTE_POS_ADDR (start_byte));
21400 while (1)
21401 {
21402 if (selective_display)
21403 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21404 ;
21405 else
21406 while (*cursor != '\n' && ++cursor != ceiling_addr)
21407 ;
21408
21409 if (cursor != ceiling_addr)
21410 {
21411 if (--count == 0)
21412 {
21413 start_byte += cursor - base + 1;
21414 *byte_pos_ptr = start_byte;
21415 return orig_count;
21416 }
21417 else
21418 if (++cursor == ceiling_addr)
21419 break;
21420 }
21421 else
21422 break;
21423 }
21424 start_byte += cursor - base;
21425 }
21426 }
21427 else
21428 {
21429 while (start_byte > limit_byte)
21430 {
21431 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21432 ceiling = max (limit_byte, ceiling);
21433 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21434 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21435 while (1)
21436 {
21437 if (selective_display)
21438 while (--cursor != ceiling_addr
21439 && *cursor != '\n' && *cursor != 015)
21440 ;
21441 else
21442 while (--cursor != ceiling_addr && *cursor != '\n')
21443 ;
21444
21445 if (cursor != ceiling_addr)
21446 {
21447 if (++count == 0)
21448 {
21449 start_byte += cursor - base + 1;
21450 *byte_pos_ptr = start_byte;
21451 /* When scanning backwards, we should
21452 not count the newline posterior to which we stop. */
21453 return - orig_count - 1;
21454 }
21455 }
21456 else
21457 break;
21458 }
21459 /* Here we add 1 to compensate for the last decrement
21460 of CURSOR, which took it past the valid range. */
21461 start_byte += cursor - base + 1;
21462 }
21463 }
21464
21465 *byte_pos_ptr = limit_byte;
21466
21467 if (count < 0)
21468 return - orig_count + count;
21469 return orig_count - count;
21470
21471 }
21472
21473
21474 \f
21475 /***********************************************************************
21476 Displaying strings
21477 ***********************************************************************/
21478
21479 /* Display a NUL-terminated string, starting with index START.
21480
21481 If STRING is non-null, display that C string. Otherwise, the Lisp
21482 string LISP_STRING is displayed. There's a case that STRING is
21483 non-null and LISP_STRING is not nil. It means STRING is a string
21484 data of LISP_STRING. In that case, we display LISP_STRING while
21485 ignoring its text properties.
21486
21487 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21488 FACE_STRING. Display STRING or LISP_STRING with the face at
21489 FACE_STRING_POS in FACE_STRING:
21490
21491 Display the string in the environment given by IT, but use the
21492 standard display table, temporarily.
21493
21494 FIELD_WIDTH is the minimum number of output glyphs to produce.
21495 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21496 with spaces. If STRING has more characters, more than FIELD_WIDTH
21497 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21498
21499 PRECISION is the maximum number of characters to output from
21500 STRING. PRECISION < 0 means don't truncate the string.
21501
21502 This is roughly equivalent to printf format specifiers:
21503
21504 FIELD_WIDTH PRECISION PRINTF
21505 ----------------------------------------
21506 -1 -1 %s
21507 -1 10 %.10s
21508 10 -1 %10s
21509 20 10 %20.10s
21510
21511 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21512 display them, and < 0 means obey the current buffer's value of
21513 enable_multibyte_characters.
21514
21515 Value is the number of columns displayed. */
21516
21517 static int
21518 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21519 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
21520 int field_width, int precision, int max_x, int multibyte)
21521 {
21522 int hpos_at_start = it->hpos;
21523 int saved_face_id = it->face_id;
21524 struct glyph_row *row = it->glyph_row;
21525 EMACS_INT it_charpos;
21526
21527 /* Initialize the iterator IT for iteration over STRING beginning
21528 with index START. */
21529 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21530 precision, field_width, multibyte);
21531 if (string && STRINGP (lisp_string))
21532 /* LISP_STRING is the one returned by decode_mode_spec. We should
21533 ignore its text properties. */
21534 it->stop_charpos = it->end_charpos;
21535
21536 /* If displaying STRING, set up the face of the iterator from
21537 FACE_STRING, if that's given. */
21538 if (STRINGP (face_string))
21539 {
21540 EMACS_INT endptr;
21541 struct face *face;
21542
21543 it->face_id
21544 = face_at_string_position (it->w, face_string, face_string_pos,
21545 0, it->region_beg_charpos,
21546 it->region_end_charpos,
21547 &endptr, it->base_face_id, 0);
21548 face = FACE_FROM_ID (it->f, it->face_id);
21549 it->face_box_p = face->box != FACE_NO_BOX;
21550 }
21551
21552 /* Set max_x to the maximum allowed X position. Don't let it go
21553 beyond the right edge of the window. */
21554 if (max_x <= 0)
21555 max_x = it->last_visible_x;
21556 else
21557 max_x = min (max_x, it->last_visible_x);
21558
21559 /* Skip over display elements that are not visible. because IT->w is
21560 hscrolled. */
21561 if (it->current_x < it->first_visible_x)
21562 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21563 MOVE_TO_POS | MOVE_TO_X);
21564
21565 row->ascent = it->max_ascent;
21566 row->height = it->max_ascent + it->max_descent;
21567 row->phys_ascent = it->max_phys_ascent;
21568 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21569 row->extra_line_spacing = it->max_extra_line_spacing;
21570
21571 if (STRINGP (it->string))
21572 it_charpos = IT_STRING_CHARPOS (*it);
21573 else
21574 it_charpos = IT_CHARPOS (*it);
21575
21576 /* This condition is for the case that we are called with current_x
21577 past last_visible_x. */
21578 while (it->current_x < max_x)
21579 {
21580 int x_before, x, n_glyphs_before, i, nglyphs;
21581
21582 /* Get the next display element. */
21583 if (!get_next_display_element (it))
21584 break;
21585
21586 /* Produce glyphs. */
21587 x_before = it->current_x;
21588 n_glyphs_before = row->used[TEXT_AREA];
21589 PRODUCE_GLYPHS (it);
21590
21591 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21592 i = 0;
21593 x = x_before;
21594 while (i < nglyphs)
21595 {
21596 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21597
21598 if (it->line_wrap != TRUNCATE
21599 && x + glyph->pixel_width > max_x)
21600 {
21601 /* End of continued line or max_x reached. */
21602 if (CHAR_GLYPH_PADDING_P (*glyph))
21603 {
21604 /* A wide character is unbreakable. */
21605 if (row->reversed_p)
21606 unproduce_glyphs (it, row->used[TEXT_AREA]
21607 - n_glyphs_before);
21608 row->used[TEXT_AREA] = n_glyphs_before;
21609 it->current_x = x_before;
21610 }
21611 else
21612 {
21613 if (row->reversed_p)
21614 unproduce_glyphs (it, row->used[TEXT_AREA]
21615 - (n_glyphs_before + i));
21616 row->used[TEXT_AREA] = n_glyphs_before + i;
21617 it->current_x = x;
21618 }
21619 break;
21620 }
21621 else if (x + glyph->pixel_width >= it->first_visible_x)
21622 {
21623 /* Glyph is at least partially visible. */
21624 ++it->hpos;
21625 if (x < it->first_visible_x)
21626 row->x = x - it->first_visible_x;
21627 }
21628 else
21629 {
21630 /* Glyph is off the left margin of the display area.
21631 Should not happen. */
21632 abort ();
21633 }
21634
21635 row->ascent = max (row->ascent, it->max_ascent);
21636 row->height = max (row->height, it->max_ascent + it->max_descent);
21637 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21638 row->phys_height = max (row->phys_height,
21639 it->max_phys_ascent + it->max_phys_descent);
21640 row->extra_line_spacing = max (row->extra_line_spacing,
21641 it->max_extra_line_spacing);
21642 x += glyph->pixel_width;
21643 ++i;
21644 }
21645
21646 /* Stop if max_x reached. */
21647 if (i < nglyphs)
21648 break;
21649
21650 /* Stop at line ends. */
21651 if (ITERATOR_AT_END_OF_LINE_P (it))
21652 {
21653 it->continuation_lines_width = 0;
21654 break;
21655 }
21656
21657 set_iterator_to_next (it, 1);
21658 if (STRINGP (it->string))
21659 it_charpos = IT_STRING_CHARPOS (*it);
21660 else
21661 it_charpos = IT_CHARPOS (*it);
21662
21663 /* Stop if truncating at the right edge. */
21664 if (it->line_wrap == TRUNCATE
21665 && it->current_x >= it->last_visible_x)
21666 {
21667 /* Add truncation mark, but don't do it if the line is
21668 truncated at a padding space. */
21669 if (it_charpos < it->string_nchars)
21670 {
21671 if (!FRAME_WINDOW_P (it->f))
21672 {
21673 int ii, n;
21674
21675 if (it->current_x > it->last_visible_x)
21676 {
21677 if (!row->reversed_p)
21678 {
21679 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21680 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21681 break;
21682 }
21683 else
21684 {
21685 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21686 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21687 break;
21688 unproduce_glyphs (it, ii + 1);
21689 ii = row->used[TEXT_AREA] - (ii + 1);
21690 }
21691 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21692 {
21693 row->used[TEXT_AREA] = ii;
21694 produce_special_glyphs (it, IT_TRUNCATION);
21695 }
21696 }
21697 produce_special_glyphs (it, IT_TRUNCATION);
21698 }
21699 row->truncated_on_right_p = 1;
21700 }
21701 break;
21702 }
21703 }
21704
21705 /* Maybe insert a truncation at the left. */
21706 if (it->first_visible_x
21707 && it_charpos > 0)
21708 {
21709 if (!FRAME_WINDOW_P (it->f))
21710 insert_left_trunc_glyphs (it);
21711 row->truncated_on_left_p = 1;
21712 }
21713
21714 it->face_id = saved_face_id;
21715
21716 /* Value is number of columns displayed. */
21717 return it->hpos - hpos_at_start;
21718 }
21719
21720
21721 \f
21722 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21723 appears as an element of LIST or as the car of an element of LIST.
21724 If PROPVAL is a list, compare each element against LIST in that
21725 way, and return 1/2 if any element of PROPVAL is found in LIST.
21726 Otherwise return 0. This function cannot quit.
21727 The return value is 2 if the text is invisible but with an ellipsis
21728 and 1 if it's invisible and without an ellipsis. */
21729
21730 int
21731 invisible_p (register Lisp_Object propval, Lisp_Object list)
21732 {
21733 register Lisp_Object tail, proptail;
21734
21735 for (tail = list; CONSP (tail); tail = XCDR (tail))
21736 {
21737 register Lisp_Object tem;
21738 tem = XCAR (tail);
21739 if (EQ (propval, tem))
21740 return 1;
21741 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21742 return NILP (XCDR (tem)) ? 1 : 2;
21743 }
21744
21745 if (CONSP (propval))
21746 {
21747 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21748 {
21749 Lisp_Object propelt;
21750 propelt = XCAR (proptail);
21751 for (tail = list; CONSP (tail); tail = XCDR (tail))
21752 {
21753 register Lisp_Object tem;
21754 tem = XCAR (tail);
21755 if (EQ (propelt, tem))
21756 return 1;
21757 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21758 return NILP (XCDR (tem)) ? 1 : 2;
21759 }
21760 }
21761 }
21762
21763 return 0;
21764 }
21765
21766 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21767 doc: /* Non-nil if the property makes the text invisible.
21768 POS-OR-PROP can be a marker or number, in which case it is taken to be
21769 a position in the current buffer and the value of the `invisible' property
21770 is checked; or it can be some other value, which is then presumed to be the
21771 value of the `invisible' property of the text of interest.
21772 The non-nil value returned can be t for truly invisible text or something
21773 else if the text is replaced by an ellipsis. */)
21774 (Lisp_Object pos_or_prop)
21775 {
21776 Lisp_Object prop
21777 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21778 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21779 : pos_or_prop);
21780 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21781 return (invis == 0 ? Qnil
21782 : invis == 1 ? Qt
21783 : make_number (invis));
21784 }
21785
21786 /* Calculate a width or height in pixels from a specification using
21787 the following elements:
21788
21789 SPEC ::=
21790 NUM - a (fractional) multiple of the default font width/height
21791 (NUM) - specifies exactly NUM pixels
21792 UNIT - a fixed number of pixels, see below.
21793 ELEMENT - size of a display element in pixels, see below.
21794 (NUM . SPEC) - equals NUM * SPEC
21795 (+ SPEC SPEC ...) - add pixel values
21796 (- SPEC SPEC ...) - subtract pixel values
21797 (- SPEC) - negate pixel value
21798
21799 NUM ::=
21800 INT or FLOAT - a number constant
21801 SYMBOL - use symbol's (buffer local) variable binding.
21802
21803 UNIT ::=
21804 in - pixels per inch *)
21805 mm - pixels per 1/1000 meter *)
21806 cm - pixels per 1/100 meter *)
21807 width - width of current font in pixels.
21808 height - height of current font in pixels.
21809
21810 *) using the ratio(s) defined in display-pixels-per-inch.
21811
21812 ELEMENT ::=
21813
21814 left-fringe - left fringe width in pixels
21815 right-fringe - right fringe width in pixels
21816
21817 left-margin - left margin width in pixels
21818 right-margin - right margin width in pixels
21819
21820 scroll-bar - scroll-bar area width in pixels
21821
21822 Examples:
21823
21824 Pixels corresponding to 5 inches:
21825 (5 . in)
21826
21827 Total width of non-text areas on left side of window (if scroll-bar is on left):
21828 '(space :width (+ left-fringe left-margin scroll-bar))
21829
21830 Align to first text column (in header line):
21831 '(space :align-to 0)
21832
21833 Align to middle of text area minus half the width of variable `my-image'
21834 containing a loaded image:
21835 '(space :align-to (0.5 . (- text my-image)))
21836
21837 Width of left margin minus width of 1 character in the default font:
21838 '(space :width (- left-margin 1))
21839
21840 Width of left margin minus width of 2 characters in the current font:
21841 '(space :width (- left-margin (2 . width)))
21842
21843 Center 1 character over left-margin (in header line):
21844 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21845
21846 Different ways to express width of left fringe plus left margin minus one pixel:
21847 '(space :width (- (+ left-fringe left-margin) (1)))
21848 '(space :width (+ left-fringe left-margin (- (1))))
21849 '(space :width (+ left-fringe left-margin (-1)))
21850
21851 */
21852
21853 #define NUMVAL(X) \
21854 ((INTEGERP (X) || FLOATP (X)) \
21855 ? XFLOATINT (X) \
21856 : - 1)
21857
21858 static int
21859 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
21860 struct font *font, int width_p, int *align_to)
21861 {
21862 double pixels;
21863
21864 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
21865 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
21866
21867 if (NILP (prop))
21868 return OK_PIXELS (0);
21869
21870 xassert (FRAME_LIVE_P (it->f));
21871
21872 if (SYMBOLP (prop))
21873 {
21874 if (SCHARS (SYMBOL_NAME (prop)) == 2)
21875 {
21876 char *unit = SSDATA (SYMBOL_NAME (prop));
21877
21878 if (unit[0] == 'i' && unit[1] == 'n')
21879 pixels = 1.0;
21880 else if (unit[0] == 'm' && unit[1] == 'm')
21881 pixels = 25.4;
21882 else if (unit[0] == 'c' && unit[1] == 'm')
21883 pixels = 2.54;
21884 else
21885 pixels = 0;
21886 if (pixels > 0)
21887 {
21888 double ppi;
21889 #ifdef HAVE_WINDOW_SYSTEM
21890 if (FRAME_WINDOW_P (it->f)
21891 && (ppi = (width_p
21892 ? FRAME_X_DISPLAY_INFO (it->f)->resx
21893 : FRAME_X_DISPLAY_INFO (it->f)->resy),
21894 ppi > 0))
21895 return OK_PIXELS (ppi / pixels);
21896 #endif
21897
21898 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
21899 || (CONSP (Vdisplay_pixels_per_inch)
21900 && (ppi = (width_p
21901 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
21902 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
21903 ppi > 0)))
21904 return OK_PIXELS (ppi / pixels);
21905
21906 return 0;
21907 }
21908 }
21909
21910 #ifdef HAVE_WINDOW_SYSTEM
21911 if (EQ (prop, Qheight))
21912 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
21913 if (EQ (prop, Qwidth))
21914 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
21915 #else
21916 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
21917 return OK_PIXELS (1);
21918 #endif
21919
21920 if (EQ (prop, Qtext))
21921 return OK_PIXELS (width_p
21922 ? window_box_width (it->w, TEXT_AREA)
21923 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
21924
21925 if (align_to && *align_to < 0)
21926 {
21927 *res = 0;
21928 if (EQ (prop, Qleft))
21929 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
21930 if (EQ (prop, Qright))
21931 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
21932 if (EQ (prop, Qcenter))
21933 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
21934 + window_box_width (it->w, TEXT_AREA) / 2);
21935 if (EQ (prop, Qleft_fringe))
21936 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21937 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
21938 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
21939 if (EQ (prop, Qright_fringe))
21940 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21941 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21942 : window_box_right_offset (it->w, TEXT_AREA));
21943 if (EQ (prop, Qleft_margin))
21944 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
21945 if (EQ (prop, Qright_margin))
21946 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
21947 if (EQ (prop, Qscroll_bar))
21948 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
21949 ? 0
21950 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21951 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21952 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21953 : 0)));
21954 }
21955 else
21956 {
21957 if (EQ (prop, Qleft_fringe))
21958 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
21959 if (EQ (prop, Qright_fringe))
21960 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
21961 if (EQ (prop, Qleft_margin))
21962 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
21963 if (EQ (prop, Qright_margin))
21964 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
21965 if (EQ (prop, Qscroll_bar))
21966 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
21967 }
21968
21969 prop = Fbuffer_local_value (prop, it->w->buffer);
21970 }
21971
21972 if (INTEGERP (prop) || FLOATP (prop))
21973 {
21974 int base_unit = (width_p
21975 ? FRAME_COLUMN_WIDTH (it->f)
21976 : FRAME_LINE_HEIGHT (it->f));
21977 return OK_PIXELS (XFLOATINT (prop) * base_unit);
21978 }
21979
21980 if (CONSP (prop))
21981 {
21982 Lisp_Object car = XCAR (prop);
21983 Lisp_Object cdr = XCDR (prop);
21984
21985 if (SYMBOLP (car))
21986 {
21987 #ifdef HAVE_WINDOW_SYSTEM
21988 if (FRAME_WINDOW_P (it->f)
21989 && valid_image_p (prop))
21990 {
21991 ptrdiff_t id = lookup_image (it->f, prop);
21992 struct image *img = IMAGE_FROM_ID (it->f, id);
21993
21994 return OK_PIXELS (width_p ? img->width : img->height);
21995 }
21996 #endif
21997 if (EQ (car, Qplus) || EQ (car, Qminus))
21998 {
21999 int first = 1;
22000 double px;
22001
22002 pixels = 0;
22003 while (CONSP (cdr))
22004 {
22005 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22006 font, width_p, align_to))
22007 return 0;
22008 if (first)
22009 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22010 else
22011 pixels += px;
22012 cdr = XCDR (cdr);
22013 }
22014 if (EQ (car, Qminus))
22015 pixels = -pixels;
22016 return OK_PIXELS (pixels);
22017 }
22018
22019 car = Fbuffer_local_value (car, it->w->buffer);
22020 }
22021
22022 if (INTEGERP (car) || FLOATP (car))
22023 {
22024 double fact;
22025 pixels = XFLOATINT (car);
22026 if (NILP (cdr))
22027 return OK_PIXELS (pixels);
22028 if (calc_pixel_width_or_height (&fact, it, cdr,
22029 font, width_p, align_to))
22030 return OK_PIXELS (pixels * fact);
22031 return 0;
22032 }
22033
22034 return 0;
22035 }
22036
22037 return 0;
22038 }
22039
22040 \f
22041 /***********************************************************************
22042 Glyph Display
22043 ***********************************************************************/
22044
22045 #ifdef HAVE_WINDOW_SYSTEM
22046
22047 #if GLYPH_DEBUG
22048
22049 void
22050 dump_glyph_string (struct glyph_string *s)
22051 {
22052 fprintf (stderr, "glyph string\n");
22053 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22054 s->x, s->y, s->width, s->height);
22055 fprintf (stderr, " ybase = %d\n", s->ybase);
22056 fprintf (stderr, " hl = %d\n", s->hl);
22057 fprintf (stderr, " left overhang = %d, right = %d\n",
22058 s->left_overhang, s->right_overhang);
22059 fprintf (stderr, " nchars = %d\n", s->nchars);
22060 fprintf (stderr, " extends to end of line = %d\n",
22061 s->extends_to_end_of_line_p);
22062 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22063 fprintf (stderr, " bg width = %d\n", s->background_width);
22064 }
22065
22066 #endif /* GLYPH_DEBUG */
22067
22068 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22069 of XChar2b structures for S; it can't be allocated in
22070 init_glyph_string because it must be allocated via `alloca'. W
22071 is the window on which S is drawn. ROW and AREA are the glyph row
22072 and area within the row from which S is constructed. START is the
22073 index of the first glyph structure covered by S. HL is a
22074 face-override for drawing S. */
22075
22076 #ifdef HAVE_NTGUI
22077 #define OPTIONAL_HDC(hdc) HDC hdc,
22078 #define DECLARE_HDC(hdc) HDC hdc;
22079 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22080 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22081 #endif
22082
22083 #ifndef OPTIONAL_HDC
22084 #define OPTIONAL_HDC(hdc)
22085 #define DECLARE_HDC(hdc)
22086 #define ALLOCATE_HDC(hdc, f)
22087 #define RELEASE_HDC(hdc, f)
22088 #endif
22089
22090 static void
22091 init_glyph_string (struct glyph_string *s,
22092 OPTIONAL_HDC (hdc)
22093 XChar2b *char2b, struct window *w, struct glyph_row *row,
22094 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22095 {
22096 memset (s, 0, sizeof *s);
22097 s->w = w;
22098 s->f = XFRAME (w->frame);
22099 #ifdef HAVE_NTGUI
22100 s->hdc = hdc;
22101 #endif
22102 s->display = FRAME_X_DISPLAY (s->f);
22103 s->window = FRAME_X_WINDOW (s->f);
22104 s->char2b = char2b;
22105 s->hl = hl;
22106 s->row = row;
22107 s->area = area;
22108 s->first_glyph = row->glyphs[area] + start;
22109 s->height = row->height;
22110 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22111 s->ybase = s->y + row->ascent;
22112 }
22113
22114
22115 /* Append the list of glyph strings with head H and tail T to the list
22116 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22117
22118 static inline void
22119 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22120 struct glyph_string *h, struct glyph_string *t)
22121 {
22122 if (h)
22123 {
22124 if (*head)
22125 (*tail)->next = h;
22126 else
22127 *head = h;
22128 h->prev = *tail;
22129 *tail = t;
22130 }
22131 }
22132
22133
22134 /* Prepend the list of glyph strings with head H and tail T to the
22135 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22136 result. */
22137
22138 static inline void
22139 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22140 struct glyph_string *h, struct glyph_string *t)
22141 {
22142 if (h)
22143 {
22144 if (*head)
22145 (*head)->prev = t;
22146 else
22147 *tail = t;
22148 t->next = *head;
22149 *head = h;
22150 }
22151 }
22152
22153
22154 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22155 Set *HEAD and *TAIL to the resulting list. */
22156
22157 static inline void
22158 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22159 struct glyph_string *s)
22160 {
22161 s->next = s->prev = NULL;
22162 append_glyph_string_lists (head, tail, s, s);
22163 }
22164
22165
22166 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22167 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22168 make sure that X resources for the face returned are allocated.
22169 Value is a pointer to a realized face that is ready for display if
22170 DISPLAY_P is non-zero. */
22171
22172 static inline struct face *
22173 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22174 XChar2b *char2b, int display_p)
22175 {
22176 struct face *face = FACE_FROM_ID (f, face_id);
22177
22178 if (face->font)
22179 {
22180 unsigned code = face->font->driver->encode_char (face->font, c);
22181
22182 if (code != FONT_INVALID_CODE)
22183 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22184 else
22185 STORE_XCHAR2B (char2b, 0, 0);
22186 }
22187
22188 /* Make sure X resources of the face are allocated. */
22189 #ifdef HAVE_X_WINDOWS
22190 if (display_p)
22191 #endif
22192 {
22193 xassert (face != NULL);
22194 PREPARE_FACE_FOR_DISPLAY (f, face);
22195 }
22196
22197 return face;
22198 }
22199
22200
22201 /* Get face and two-byte form of character glyph GLYPH on frame F.
22202 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22203 a pointer to a realized face that is ready for display. */
22204
22205 static inline struct face *
22206 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22207 XChar2b *char2b, int *two_byte_p)
22208 {
22209 struct face *face;
22210
22211 xassert (glyph->type == CHAR_GLYPH);
22212 face = FACE_FROM_ID (f, glyph->face_id);
22213
22214 if (two_byte_p)
22215 *two_byte_p = 0;
22216
22217 if (face->font)
22218 {
22219 unsigned code;
22220
22221 if (CHAR_BYTE8_P (glyph->u.ch))
22222 code = CHAR_TO_BYTE8 (glyph->u.ch);
22223 else
22224 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22225
22226 if (code != FONT_INVALID_CODE)
22227 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22228 else
22229 STORE_XCHAR2B (char2b, 0, 0);
22230 }
22231
22232 /* Make sure X resources of the face are allocated. */
22233 xassert (face != NULL);
22234 PREPARE_FACE_FOR_DISPLAY (f, face);
22235 return face;
22236 }
22237
22238
22239 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22240 Return 1 if FONT has a glyph for C, otherwise return 0. */
22241
22242 static inline int
22243 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22244 {
22245 unsigned code;
22246
22247 if (CHAR_BYTE8_P (c))
22248 code = CHAR_TO_BYTE8 (c);
22249 else
22250 code = font->driver->encode_char (font, c);
22251
22252 if (code == FONT_INVALID_CODE)
22253 return 0;
22254 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22255 return 1;
22256 }
22257
22258
22259 /* Fill glyph string S with composition components specified by S->cmp.
22260
22261 BASE_FACE is the base face of the composition.
22262 S->cmp_from is the index of the first component for S.
22263
22264 OVERLAPS non-zero means S should draw the foreground only, and use
22265 its physical height for clipping. See also draw_glyphs.
22266
22267 Value is the index of a component not in S. */
22268
22269 static int
22270 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22271 int overlaps)
22272 {
22273 int i;
22274 /* For all glyphs of this composition, starting at the offset
22275 S->cmp_from, until we reach the end of the definition or encounter a
22276 glyph that requires the different face, add it to S. */
22277 struct face *face;
22278
22279 xassert (s);
22280
22281 s->for_overlaps = overlaps;
22282 s->face = NULL;
22283 s->font = NULL;
22284 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22285 {
22286 int c = COMPOSITION_GLYPH (s->cmp, i);
22287
22288 /* TAB in a composition means display glyphs with padding space
22289 on the left or right. */
22290 if (c != '\t')
22291 {
22292 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22293 -1, Qnil);
22294
22295 face = get_char_face_and_encoding (s->f, c, face_id,
22296 s->char2b + i, 1);
22297 if (face)
22298 {
22299 if (! s->face)
22300 {
22301 s->face = face;
22302 s->font = s->face->font;
22303 }
22304 else if (s->face != face)
22305 break;
22306 }
22307 }
22308 ++s->nchars;
22309 }
22310 s->cmp_to = i;
22311
22312 if (s->face == NULL)
22313 {
22314 s->face = base_face->ascii_face;
22315 s->font = s->face->font;
22316 }
22317
22318 /* All glyph strings for the same composition has the same width,
22319 i.e. the width set for the first component of the composition. */
22320 s->width = s->first_glyph->pixel_width;
22321
22322 /* If the specified font could not be loaded, use the frame's
22323 default font, but record the fact that we couldn't load it in
22324 the glyph string so that we can draw rectangles for the
22325 characters of the glyph string. */
22326 if (s->font == NULL)
22327 {
22328 s->font_not_found_p = 1;
22329 s->font = FRAME_FONT (s->f);
22330 }
22331
22332 /* Adjust base line for subscript/superscript text. */
22333 s->ybase += s->first_glyph->voffset;
22334
22335 /* This glyph string must always be drawn with 16-bit functions. */
22336 s->two_byte_p = 1;
22337
22338 return s->cmp_to;
22339 }
22340
22341 static int
22342 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22343 int start, int end, int overlaps)
22344 {
22345 struct glyph *glyph, *last;
22346 Lisp_Object lgstring;
22347 int i;
22348
22349 s->for_overlaps = overlaps;
22350 glyph = s->row->glyphs[s->area] + start;
22351 last = s->row->glyphs[s->area] + end;
22352 s->cmp_id = glyph->u.cmp.id;
22353 s->cmp_from = glyph->slice.cmp.from;
22354 s->cmp_to = glyph->slice.cmp.to + 1;
22355 s->face = FACE_FROM_ID (s->f, face_id);
22356 lgstring = composition_gstring_from_id (s->cmp_id);
22357 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22358 glyph++;
22359 while (glyph < last
22360 && glyph->u.cmp.automatic
22361 && glyph->u.cmp.id == s->cmp_id
22362 && s->cmp_to == glyph->slice.cmp.from)
22363 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22364
22365 for (i = s->cmp_from; i < s->cmp_to; i++)
22366 {
22367 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22368 unsigned code = LGLYPH_CODE (lglyph);
22369
22370 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22371 }
22372 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22373 return glyph - s->row->glyphs[s->area];
22374 }
22375
22376
22377 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22378 See the comment of fill_glyph_string for arguments.
22379 Value is the index of the first glyph not in S. */
22380
22381
22382 static int
22383 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22384 int start, int end, int overlaps)
22385 {
22386 struct glyph *glyph, *last;
22387 int voffset;
22388
22389 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22390 s->for_overlaps = overlaps;
22391 glyph = s->row->glyphs[s->area] + start;
22392 last = s->row->glyphs[s->area] + end;
22393 voffset = glyph->voffset;
22394 s->face = FACE_FROM_ID (s->f, face_id);
22395 s->font = s->face->font;
22396 s->nchars = 1;
22397 s->width = glyph->pixel_width;
22398 glyph++;
22399 while (glyph < last
22400 && glyph->type == GLYPHLESS_GLYPH
22401 && glyph->voffset == voffset
22402 && glyph->face_id == face_id)
22403 {
22404 s->nchars++;
22405 s->width += glyph->pixel_width;
22406 glyph++;
22407 }
22408 s->ybase += voffset;
22409 return glyph - s->row->glyphs[s->area];
22410 }
22411
22412
22413 /* Fill glyph string S from a sequence of character glyphs.
22414
22415 FACE_ID is the face id of the string. START is the index of the
22416 first glyph to consider, END is the index of the last + 1.
22417 OVERLAPS non-zero means S should draw the foreground only, and use
22418 its physical height for clipping. See also draw_glyphs.
22419
22420 Value is the index of the first glyph not in S. */
22421
22422 static int
22423 fill_glyph_string (struct glyph_string *s, int face_id,
22424 int start, int end, int overlaps)
22425 {
22426 struct glyph *glyph, *last;
22427 int voffset;
22428 int glyph_not_available_p;
22429
22430 xassert (s->f == XFRAME (s->w->frame));
22431 xassert (s->nchars == 0);
22432 xassert (start >= 0 && end > start);
22433
22434 s->for_overlaps = overlaps;
22435 glyph = s->row->glyphs[s->area] + start;
22436 last = s->row->glyphs[s->area] + end;
22437 voffset = glyph->voffset;
22438 s->padding_p = glyph->padding_p;
22439 glyph_not_available_p = glyph->glyph_not_available_p;
22440
22441 while (glyph < last
22442 && glyph->type == CHAR_GLYPH
22443 && glyph->voffset == voffset
22444 /* Same face id implies same font, nowadays. */
22445 && glyph->face_id == face_id
22446 && glyph->glyph_not_available_p == glyph_not_available_p)
22447 {
22448 int two_byte_p;
22449
22450 s->face = get_glyph_face_and_encoding (s->f, glyph,
22451 s->char2b + s->nchars,
22452 &two_byte_p);
22453 s->two_byte_p = two_byte_p;
22454 ++s->nchars;
22455 xassert (s->nchars <= end - start);
22456 s->width += glyph->pixel_width;
22457 if (glyph++->padding_p != s->padding_p)
22458 break;
22459 }
22460
22461 s->font = s->face->font;
22462
22463 /* If the specified font could not be loaded, use the frame's font,
22464 but record the fact that we couldn't load it in
22465 S->font_not_found_p so that we can draw rectangles for the
22466 characters of the glyph string. */
22467 if (s->font == NULL || glyph_not_available_p)
22468 {
22469 s->font_not_found_p = 1;
22470 s->font = FRAME_FONT (s->f);
22471 }
22472
22473 /* Adjust base line for subscript/superscript text. */
22474 s->ybase += voffset;
22475
22476 xassert (s->face && s->face->gc);
22477 return glyph - s->row->glyphs[s->area];
22478 }
22479
22480
22481 /* Fill glyph string S from image glyph S->first_glyph. */
22482
22483 static void
22484 fill_image_glyph_string (struct glyph_string *s)
22485 {
22486 xassert (s->first_glyph->type == IMAGE_GLYPH);
22487 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22488 xassert (s->img);
22489 s->slice = s->first_glyph->slice.img;
22490 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22491 s->font = s->face->font;
22492 s->width = s->first_glyph->pixel_width;
22493
22494 /* Adjust base line for subscript/superscript text. */
22495 s->ybase += s->first_glyph->voffset;
22496 }
22497
22498
22499 /* Fill glyph string S from a sequence of stretch glyphs.
22500
22501 START is the index of the first glyph to consider,
22502 END is the index of the last + 1.
22503
22504 Value is the index of the first glyph not in S. */
22505
22506 static int
22507 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22508 {
22509 struct glyph *glyph, *last;
22510 int voffset, face_id;
22511
22512 xassert (s->first_glyph->type == STRETCH_GLYPH);
22513
22514 glyph = s->row->glyphs[s->area] + start;
22515 last = s->row->glyphs[s->area] + end;
22516 face_id = glyph->face_id;
22517 s->face = FACE_FROM_ID (s->f, face_id);
22518 s->font = s->face->font;
22519 s->width = glyph->pixel_width;
22520 s->nchars = 1;
22521 voffset = glyph->voffset;
22522
22523 for (++glyph;
22524 (glyph < last
22525 && glyph->type == STRETCH_GLYPH
22526 && glyph->voffset == voffset
22527 && glyph->face_id == face_id);
22528 ++glyph)
22529 s->width += glyph->pixel_width;
22530
22531 /* Adjust base line for subscript/superscript text. */
22532 s->ybase += voffset;
22533
22534 /* The case that face->gc == 0 is handled when drawing the glyph
22535 string by calling PREPARE_FACE_FOR_DISPLAY. */
22536 xassert (s->face);
22537 return glyph - s->row->glyphs[s->area];
22538 }
22539
22540 static struct font_metrics *
22541 get_per_char_metric (struct font *font, XChar2b *char2b)
22542 {
22543 static struct font_metrics metrics;
22544 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22545
22546 if (! font || code == FONT_INVALID_CODE)
22547 return NULL;
22548 font->driver->text_extents (font, &code, 1, &metrics);
22549 return &metrics;
22550 }
22551
22552 /* EXPORT for RIF:
22553 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22554 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22555 assumed to be zero. */
22556
22557 void
22558 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22559 {
22560 *left = *right = 0;
22561
22562 if (glyph->type == CHAR_GLYPH)
22563 {
22564 struct face *face;
22565 XChar2b char2b;
22566 struct font_metrics *pcm;
22567
22568 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22569 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22570 {
22571 if (pcm->rbearing > pcm->width)
22572 *right = pcm->rbearing - pcm->width;
22573 if (pcm->lbearing < 0)
22574 *left = -pcm->lbearing;
22575 }
22576 }
22577 else if (glyph->type == COMPOSITE_GLYPH)
22578 {
22579 if (! glyph->u.cmp.automatic)
22580 {
22581 struct composition *cmp = composition_table[glyph->u.cmp.id];
22582
22583 if (cmp->rbearing > cmp->pixel_width)
22584 *right = cmp->rbearing - cmp->pixel_width;
22585 if (cmp->lbearing < 0)
22586 *left = - cmp->lbearing;
22587 }
22588 else
22589 {
22590 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22591 struct font_metrics metrics;
22592
22593 composition_gstring_width (gstring, glyph->slice.cmp.from,
22594 glyph->slice.cmp.to + 1, &metrics);
22595 if (metrics.rbearing > metrics.width)
22596 *right = metrics.rbearing - metrics.width;
22597 if (metrics.lbearing < 0)
22598 *left = - metrics.lbearing;
22599 }
22600 }
22601 }
22602
22603
22604 /* Return the index of the first glyph preceding glyph string S that
22605 is overwritten by S because of S's left overhang. Value is -1
22606 if no glyphs are overwritten. */
22607
22608 static int
22609 left_overwritten (struct glyph_string *s)
22610 {
22611 int k;
22612
22613 if (s->left_overhang)
22614 {
22615 int x = 0, i;
22616 struct glyph *glyphs = s->row->glyphs[s->area];
22617 int first = s->first_glyph - glyphs;
22618
22619 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22620 x -= glyphs[i].pixel_width;
22621
22622 k = i + 1;
22623 }
22624 else
22625 k = -1;
22626
22627 return k;
22628 }
22629
22630
22631 /* Return the index of the first glyph preceding glyph string S that
22632 is overwriting S because of its right overhang. Value is -1 if no
22633 glyph in front of S overwrites S. */
22634
22635 static int
22636 left_overwriting (struct glyph_string *s)
22637 {
22638 int i, k, x;
22639 struct glyph *glyphs = s->row->glyphs[s->area];
22640 int first = s->first_glyph - glyphs;
22641
22642 k = -1;
22643 x = 0;
22644 for (i = first - 1; i >= 0; --i)
22645 {
22646 int left, right;
22647 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22648 if (x + right > 0)
22649 k = i;
22650 x -= glyphs[i].pixel_width;
22651 }
22652
22653 return k;
22654 }
22655
22656
22657 /* Return the index of the last glyph following glyph string S that is
22658 overwritten by S because of S's right overhang. Value is -1 if
22659 no such glyph is found. */
22660
22661 static int
22662 right_overwritten (struct glyph_string *s)
22663 {
22664 int k = -1;
22665
22666 if (s->right_overhang)
22667 {
22668 int x = 0, i;
22669 struct glyph *glyphs = s->row->glyphs[s->area];
22670 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22671 int end = s->row->used[s->area];
22672
22673 for (i = first; i < end && s->right_overhang > x; ++i)
22674 x += glyphs[i].pixel_width;
22675
22676 k = i;
22677 }
22678
22679 return k;
22680 }
22681
22682
22683 /* Return the index of the last glyph following glyph string S that
22684 overwrites S because of its left overhang. Value is negative
22685 if no such glyph is found. */
22686
22687 static int
22688 right_overwriting (struct glyph_string *s)
22689 {
22690 int i, k, x;
22691 int end = s->row->used[s->area];
22692 struct glyph *glyphs = s->row->glyphs[s->area];
22693 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22694
22695 k = -1;
22696 x = 0;
22697 for (i = first; i < end; ++i)
22698 {
22699 int left, right;
22700 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22701 if (x - left < 0)
22702 k = i;
22703 x += glyphs[i].pixel_width;
22704 }
22705
22706 return k;
22707 }
22708
22709
22710 /* Set background width of glyph string S. START is the index of the
22711 first glyph following S. LAST_X is the right-most x-position + 1
22712 in the drawing area. */
22713
22714 static inline void
22715 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22716 {
22717 /* If the face of this glyph string has to be drawn to the end of
22718 the drawing area, set S->extends_to_end_of_line_p. */
22719
22720 if (start == s->row->used[s->area]
22721 && s->area == TEXT_AREA
22722 && ((s->row->fill_line_p
22723 && (s->hl == DRAW_NORMAL_TEXT
22724 || s->hl == DRAW_IMAGE_RAISED
22725 || s->hl == DRAW_IMAGE_SUNKEN))
22726 || s->hl == DRAW_MOUSE_FACE))
22727 s->extends_to_end_of_line_p = 1;
22728
22729 /* If S extends its face to the end of the line, set its
22730 background_width to the distance to the right edge of the drawing
22731 area. */
22732 if (s->extends_to_end_of_line_p)
22733 s->background_width = last_x - s->x + 1;
22734 else
22735 s->background_width = s->width;
22736 }
22737
22738
22739 /* Compute overhangs and x-positions for glyph string S and its
22740 predecessors, or successors. X is the starting x-position for S.
22741 BACKWARD_P non-zero means process predecessors. */
22742
22743 static void
22744 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22745 {
22746 if (backward_p)
22747 {
22748 while (s)
22749 {
22750 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22751 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22752 x -= s->width;
22753 s->x = x;
22754 s = s->prev;
22755 }
22756 }
22757 else
22758 {
22759 while (s)
22760 {
22761 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22762 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22763 s->x = x;
22764 x += s->width;
22765 s = s->next;
22766 }
22767 }
22768 }
22769
22770
22771
22772 /* The following macros are only called from draw_glyphs below.
22773 They reference the following parameters of that function directly:
22774 `w', `row', `area', and `overlap_p'
22775 as well as the following local variables:
22776 `s', `f', and `hdc' (in W32) */
22777
22778 #ifdef HAVE_NTGUI
22779 /* On W32, silently add local `hdc' variable to argument list of
22780 init_glyph_string. */
22781 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22782 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22783 #else
22784 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22785 init_glyph_string (s, char2b, w, row, area, start, hl)
22786 #endif
22787
22788 /* Add a glyph string for a stretch glyph to the list of strings
22789 between HEAD and TAIL. START is the index of the stretch glyph in
22790 row area AREA of glyph row ROW. END is the index of the last glyph
22791 in that glyph row area. X is the current output position assigned
22792 to the new glyph string constructed. HL overrides that face of the
22793 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22794 is the right-most x-position of the drawing area. */
22795
22796 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22797 and below -- keep them on one line. */
22798 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22799 do \
22800 { \
22801 s = (struct glyph_string *) alloca (sizeof *s); \
22802 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22803 START = fill_stretch_glyph_string (s, START, END); \
22804 append_glyph_string (&HEAD, &TAIL, s); \
22805 s->x = (X); \
22806 } \
22807 while (0)
22808
22809
22810 /* Add a glyph string for an image glyph to the list of strings
22811 between HEAD and TAIL. START is the index of the image glyph in
22812 row area AREA of glyph row ROW. END is the index of the last glyph
22813 in that glyph row area. X is the current output position assigned
22814 to the new glyph string constructed. HL overrides that face of the
22815 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22816 is the right-most x-position of the drawing area. */
22817
22818 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22819 do \
22820 { \
22821 s = (struct glyph_string *) alloca (sizeof *s); \
22822 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22823 fill_image_glyph_string (s); \
22824 append_glyph_string (&HEAD, &TAIL, s); \
22825 ++START; \
22826 s->x = (X); \
22827 } \
22828 while (0)
22829
22830
22831 /* Add a glyph string for a sequence of character glyphs to the list
22832 of strings between HEAD and TAIL. START is the index of the first
22833 glyph in row area AREA of glyph row ROW that is part of the new
22834 glyph string. END is the index of the last glyph in that glyph row
22835 area. X is the current output position assigned to the new glyph
22836 string constructed. HL overrides that face of the glyph; e.g. it
22837 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22838 right-most x-position of the drawing area. */
22839
22840 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22841 do \
22842 { \
22843 int face_id; \
22844 XChar2b *char2b; \
22845 \
22846 face_id = (row)->glyphs[area][START].face_id; \
22847 \
22848 s = (struct glyph_string *) alloca (sizeof *s); \
22849 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22850 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22851 append_glyph_string (&HEAD, &TAIL, s); \
22852 s->x = (X); \
22853 START = fill_glyph_string (s, face_id, START, END, overlaps); \
22854 } \
22855 while (0)
22856
22857
22858 /* Add a glyph string for a composite sequence to the list of strings
22859 between HEAD and TAIL. START is the index of the first glyph in
22860 row area AREA of glyph row ROW that is part of the new glyph
22861 string. END is the index of the last glyph in that glyph row area.
22862 X is the current output position assigned to the new glyph string
22863 constructed. HL overrides that face of the glyph; e.g. it is
22864 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
22865 x-position of the drawing area. */
22866
22867 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22868 do { \
22869 int face_id = (row)->glyphs[area][START].face_id; \
22870 struct face *base_face = FACE_FROM_ID (f, face_id); \
22871 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
22872 struct composition *cmp = composition_table[cmp_id]; \
22873 XChar2b *char2b; \
22874 struct glyph_string *first_s = NULL; \
22875 int n; \
22876 \
22877 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
22878 \
22879 /* Make glyph_strings for each glyph sequence that is drawable by \
22880 the same face, and append them to HEAD/TAIL. */ \
22881 for (n = 0; n < cmp->glyph_len;) \
22882 { \
22883 s = (struct glyph_string *) alloca (sizeof *s); \
22884 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22885 append_glyph_string (&(HEAD), &(TAIL), s); \
22886 s->cmp = cmp; \
22887 s->cmp_from = n; \
22888 s->x = (X); \
22889 if (n == 0) \
22890 first_s = s; \
22891 n = fill_composite_glyph_string (s, base_face, overlaps); \
22892 } \
22893 \
22894 ++START; \
22895 s = first_s; \
22896 } while (0)
22897
22898
22899 /* Add a glyph string for a glyph-string sequence to the list of strings
22900 between HEAD and TAIL. */
22901
22902 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22903 do { \
22904 int face_id; \
22905 XChar2b *char2b; \
22906 Lisp_Object gstring; \
22907 \
22908 face_id = (row)->glyphs[area][START].face_id; \
22909 gstring = (composition_gstring_from_id \
22910 ((row)->glyphs[area][START].u.cmp.id)); \
22911 s = (struct glyph_string *) alloca (sizeof *s); \
22912 char2b = (XChar2b *) alloca ((sizeof *char2b) \
22913 * LGSTRING_GLYPH_LEN (gstring)); \
22914 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22915 append_glyph_string (&(HEAD), &(TAIL), s); \
22916 s->x = (X); \
22917 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
22918 } while (0)
22919
22920
22921 /* Add a glyph string for a sequence of glyphless character's glyphs
22922 to the list of strings between HEAD and TAIL. The meanings of
22923 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
22924
22925 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22926 do \
22927 { \
22928 int face_id; \
22929 \
22930 face_id = (row)->glyphs[area][START].face_id; \
22931 \
22932 s = (struct glyph_string *) alloca (sizeof *s); \
22933 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22934 append_glyph_string (&HEAD, &TAIL, s); \
22935 s->x = (X); \
22936 START = fill_glyphless_glyph_string (s, face_id, START, END, \
22937 overlaps); \
22938 } \
22939 while (0)
22940
22941
22942 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
22943 of AREA of glyph row ROW on window W between indices START and END.
22944 HL overrides the face for drawing glyph strings, e.g. it is
22945 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
22946 x-positions of the drawing area.
22947
22948 This is an ugly monster macro construct because we must use alloca
22949 to allocate glyph strings (because draw_glyphs can be called
22950 asynchronously). */
22951
22952 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22953 do \
22954 { \
22955 HEAD = TAIL = NULL; \
22956 while (START < END) \
22957 { \
22958 struct glyph *first_glyph = (row)->glyphs[area] + START; \
22959 switch (first_glyph->type) \
22960 { \
22961 case CHAR_GLYPH: \
22962 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
22963 HL, X, LAST_X); \
22964 break; \
22965 \
22966 case COMPOSITE_GLYPH: \
22967 if (first_glyph->u.cmp.automatic) \
22968 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
22969 HL, X, LAST_X); \
22970 else \
22971 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
22972 HL, X, LAST_X); \
22973 break; \
22974 \
22975 case STRETCH_GLYPH: \
22976 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
22977 HL, X, LAST_X); \
22978 break; \
22979 \
22980 case IMAGE_GLYPH: \
22981 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
22982 HL, X, LAST_X); \
22983 break; \
22984 \
22985 case GLYPHLESS_GLYPH: \
22986 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
22987 HL, X, LAST_X); \
22988 break; \
22989 \
22990 default: \
22991 abort (); \
22992 } \
22993 \
22994 if (s) \
22995 { \
22996 set_glyph_string_background_width (s, START, LAST_X); \
22997 (X) += s->width; \
22998 } \
22999 } \
23000 } while (0)
23001
23002
23003 /* Draw glyphs between START and END in AREA of ROW on window W,
23004 starting at x-position X. X is relative to AREA in W. HL is a
23005 face-override with the following meaning:
23006
23007 DRAW_NORMAL_TEXT draw normally
23008 DRAW_CURSOR draw in cursor face
23009 DRAW_MOUSE_FACE draw in mouse face.
23010 DRAW_INVERSE_VIDEO draw in mode line face
23011 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23012 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23013
23014 If OVERLAPS is non-zero, draw only the foreground of characters and
23015 clip to the physical height of ROW. Non-zero value also defines
23016 the overlapping part to be drawn:
23017
23018 OVERLAPS_PRED overlap with preceding rows
23019 OVERLAPS_SUCC overlap with succeeding rows
23020 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23021 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23022
23023 Value is the x-position reached, relative to AREA of W. */
23024
23025 static int
23026 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23027 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
23028 enum draw_glyphs_face hl, int overlaps)
23029 {
23030 struct glyph_string *head, *tail;
23031 struct glyph_string *s;
23032 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23033 int i, j, x_reached, last_x, area_left = 0;
23034 struct frame *f = XFRAME (WINDOW_FRAME (w));
23035 DECLARE_HDC (hdc);
23036
23037 ALLOCATE_HDC (hdc, f);
23038
23039 /* Let's rather be paranoid than getting a SEGV. */
23040 end = min (end, row->used[area]);
23041 start = max (0, start);
23042 start = min (end, start);
23043
23044 /* Translate X to frame coordinates. Set last_x to the right
23045 end of the drawing area. */
23046 if (row->full_width_p)
23047 {
23048 /* X is relative to the left edge of W, without scroll bars
23049 or fringes. */
23050 area_left = WINDOW_LEFT_EDGE_X (w);
23051 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23052 }
23053 else
23054 {
23055 area_left = window_box_left (w, area);
23056 last_x = area_left + window_box_width (w, area);
23057 }
23058 x += area_left;
23059
23060 /* Build a doubly-linked list of glyph_string structures between
23061 head and tail from what we have to draw. Note that the macro
23062 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23063 the reason we use a separate variable `i'. */
23064 i = start;
23065 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23066 if (tail)
23067 x_reached = tail->x + tail->background_width;
23068 else
23069 x_reached = x;
23070
23071 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23072 the row, redraw some glyphs in front or following the glyph
23073 strings built above. */
23074 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23075 {
23076 struct glyph_string *h, *t;
23077 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23078 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23079 int check_mouse_face = 0;
23080 int dummy_x = 0;
23081
23082 /* If mouse highlighting is on, we may need to draw adjacent
23083 glyphs using mouse-face highlighting. */
23084 if (area == TEXT_AREA && row->mouse_face_p)
23085 {
23086 struct glyph_row *mouse_beg_row, *mouse_end_row;
23087
23088 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23089 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23090
23091 if (row >= mouse_beg_row && row <= mouse_end_row)
23092 {
23093 check_mouse_face = 1;
23094 mouse_beg_col = (row == mouse_beg_row)
23095 ? hlinfo->mouse_face_beg_col : 0;
23096 mouse_end_col = (row == mouse_end_row)
23097 ? hlinfo->mouse_face_end_col
23098 : row->used[TEXT_AREA];
23099 }
23100 }
23101
23102 /* Compute overhangs for all glyph strings. */
23103 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23104 for (s = head; s; s = s->next)
23105 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23106
23107 /* Prepend glyph strings for glyphs in front of the first glyph
23108 string that are overwritten because of the first glyph
23109 string's left overhang. The background of all strings
23110 prepended must be drawn because the first glyph string
23111 draws over it. */
23112 i = left_overwritten (head);
23113 if (i >= 0)
23114 {
23115 enum draw_glyphs_face overlap_hl;
23116
23117 /* If this row contains mouse highlighting, attempt to draw
23118 the overlapped glyphs with the correct highlight. This
23119 code fails if the overlap encompasses more than one glyph
23120 and mouse-highlight spans only some of these glyphs.
23121 However, making it work perfectly involves a lot more
23122 code, and I don't know if the pathological case occurs in
23123 practice, so we'll stick to this for now. --- cyd */
23124 if (check_mouse_face
23125 && mouse_beg_col < start && mouse_end_col > i)
23126 overlap_hl = DRAW_MOUSE_FACE;
23127 else
23128 overlap_hl = DRAW_NORMAL_TEXT;
23129
23130 j = i;
23131 BUILD_GLYPH_STRINGS (j, start, h, t,
23132 overlap_hl, dummy_x, last_x);
23133 start = i;
23134 compute_overhangs_and_x (t, head->x, 1);
23135 prepend_glyph_string_lists (&head, &tail, h, t);
23136 clip_head = head;
23137 }
23138
23139 /* Prepend glyph strings for glyphs in front of the first glyph
23140 string that overwrite that glyph string because of their
23141 right overhang. For these strings, only the foreground must
23142 be drawn, because it draws over the glyph string at `head'.
23143 The background must not be drawn because this would overwrite
23144 right overhangs of preceding glyphs for which no glyph
23145 strings exist. */
23146 i = left_overwriting (head);
23147 if (i >= 0)
23148 {
23149 enum draw_glyphs_face overlap_hl;
23150
23151 if (check_mouse_face
23152 && mouse_beg_col < start && mouse_end_col > i)
23153 overlap_hl = DRAW_MOUSE_FACE;
23154 else
23155 overlap_hl = DRAW_NORMAL_TEXT;
23156
23157 clip_head = head;
23158 BUILD_GLYPH_STRINGS (i, start, h, t,
23159 overlap_hl, dummy_x, last_x);
23160 for (s = h; s; s = s->next)
23161 s->background_filled_p = 1;
23162 compute_overhangs_and_x (t, head->x, 1);
23163 prepend_glyph_string_lists (&head, &tail, h, t);
23164 }
23165
23166 /* Append glyphs strings for glyphs following the last glyph
23167 string tail that are overwritten by tail. The background of
23168 these strings has to be drawn because tail's foreground draws
23169 over it. */
23170 i = right_overwritten (tail);
23171 if (i >= 0)
23172 {
23173 enum draw_glyphs_face overlap_hl;
23174
23175 if (check_mouse_face
23176 && mouse_beg_col < i && mouse_end_col > end)
23177 overlap_hl = DRAW_MOUSE_FACE;
23178 else
23179 overlap_hl = DRAW_NORMAL_TEXT;
23180
23181 BUILD_GLYPH_STRINGS (end, i, h, t,
23182 overlap_hl, x, last_x);
23183 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23184 we don't have `end = i;' here. */
23185 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23186 append_glyph_string_lists (&head, &tail, h, t);
23187 clip_tail = tail;
23188 }
23189
23190 /* Append glyph strings for glyphs following the last glyph
23191 string tail that overwrite tail. The foreground of such
23192 glyphs has to be drawn because it writes into the background
23193 of tail. The background must not be drawn because it could
23194 paint over the foreground of following glyphs. */
23195 i = right_overwriting (tail);
23196 if (i >= 0)
23197 {
23198 enum draw_glyphs_face overlap_hl;
23199 if (check_mouse_face
23200 && mouse_beg_col < i && mouse_end_col > end)
23201 overlap_hl = DRAW_MOUSE_FACE;
23202 else
23203 overlap_hl = DRAW_NORMAL_TEXT;
23204
23205 clip_tail = tail;
23206 i++; /* We must include the Ith glyph. */
23207 BUILD_GLYPH_STRINGS (end, i, h, t,
23208 overlap_hl, x, last_x);
23209 for (s = h; s; s = s->next)
23210 s->background_filled_p = 1;
23211 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23212 append_glyph_string_lists (&head, &tail, h, t);
23213 }
23214 if (clip_head || clip_tail)
23215 for (s = head; s; s = s->next)
23216 {
23217 s->clip_head = clip_head;
23218 s->clip_tail = clip_tail;
23219 }
23220 }
23221
23222 /* Draw all strings. */
23223 for (s = head; s; s = s->next)
23224 FRAME_RIF (f)->draw_glyph_string (s);
23225
23226 #ifndef HAVE_NS
23227 /* When focus a sole frame and move horizontally, this sets on_p to 0
23228 causing a failure to erase prev cursor position. */
23229 if (area == TEXT_AREA
23230 && !row->full_width_p
23231 /* When drawing overlapping rows, only the glyph strings'
23232 foreground is drawn, which doesn't erase a cursor
23233 completely. */
23234 && !overlaps)
23235 {
23236 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23237 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23238 : (tail ? tail->x + tail->background_width : x));
23239 x0 -= area_left;
23240 x1 -= area_left;
23241
23242 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23243 row->y, MATRIX_ROW_BOTTOM_Y (row));
23244 }
23245 #endif
23246
23247 /* Value is the x-position up to which drawn, relative to AREA of W.
23248 This doesn't include parts drawn because of overhangs. */
23249 if (row->full_width_p)
23250 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23251 else
23252 x_reached -= area_left;
23253
23254 RELEASE_HDC (hdc, f);
23255
23256 return x_reached;
23257 }
23258
23259 /* Expand row matrix if too narrow. Don't expand if area
23260 is not present. */
23261
23262 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23263 { \
23264 if (!fonts_changed_p \
23265 && (it->glyph_row->glyphs[area] \
23266 < it->glyph_row->glyphs[area + 1])) \
23267 { \
23268 it->w->ncols_scale_factor++; \
23269 fonts_changed_p = 1; \
23270 } \
23271 }
23272
23273 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23274 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23275
23276 static inline void
23277 append_glyph (struct it *it)
23278 {
23279 struct glyph *glyph;
23280 enum glyph_row_area area = it->area;
23281
23282 xassert (it->glyph_row);
23283 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23284
23285 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23286 if (glyph < it->glyph_row->glyphs[area + 1])
23287 {
23288 /* If the glyph row is reversed, we need to prepend the glyph
23289 rather than append it. */
23290 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23291 {
23292 struct glyph *g;
23293
23294 /* Make room for the additional glyph. */
23295 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23296 g[1] = *g;
23297 glyph = it->glyph_row->glyphs[area];
23298 }
23299 glyph->charpos = CHARPOS (it->position);
23300 glyph->object = it->object;
23301 if (it->pixel_width > 0)
23302 {
23303 glyph->pixel_width = it->pixel_width;
23304 glyph->padding_p = 0;
23305 }
23306 else
23307 {
23308 /* Assure at least 1-pixel width. Otherwise, cursor can't
23309 be displayed correctly. */
23310 glyph->pixel_width = 1;
23311 glyph->padding_p = 1;
23312 }
23313 glyph->ascent = it->ascent;
23314 glyph->descent = it->descent;
23315 glyph->voffset = it->voffset;
23316 glyph->type = CHAR_GLYPH;
23317 glyph->avoid_cursor_p = it->avoid_cursor_p;
23318 glyph->multibyte_p = it->multibyte_p;
23319 glyph->left_box_line_p = it->start_of_box_run_p;
23320 glyph->right_box_line_p = it->end_of_box_run_p;
23321 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23322 || it->phys_descent > it->descent);
23323 glyph->glyph_not_available_p = it->glyph_not_available_p;
23324 glyph->face_id = it->face_id;
23325 glyph->u.ch = it->char_to_display;
23326 glyph->slice.img = null_glyph_slice;
23327 glyph->font_type = FONT_TYPE_UNKNOWN;
23328 if (it->bidi_p)
23329 {
23330 glyph->resolved_level = it->bidi_it.resolved_level;
23331 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23332 abort ();
23333 glyph->bidi_type = it->bidi_it.type;
23334 }
23335 else
23336 {
23337 glyph->resolved_level = 0;
23338 glyph->bidi_type = UNKNOWN_BT;
23339 }
23340 ++it->glyph_row->used[area];
23341 }
23342 else
23343 IT_EXPAND_MATRIX_WIDTH (it, area);
23344 }
23345
23346 /* Store one glyph for the composition IT->cmp_it.id in
23347 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23348 non-null. */
23349
23350 static inline void
23351 append_composite_glyph (struct it *it)
23352 {
23353 struct glyph *glyph;
23354 enum glyph_row_area area = it->area;
23355
23356 xassert (it->glyph_row);
23357
23358 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23359 if (glyph < it->glyph_row->glyphs[area + 1])
23360 {
23361 /* If the glyph row is reversed, we need to prepend the glyph
23362 rather than append it. */
23363 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23364 {
23365 struct glyph *g;
23366
23367 /* Make room for the new glyph. */
23368 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23369 g[1] = *g;
23370 glyph = it->glyph_row->glyphs[it->area];
23371 }
23372 glyph->charpos = it->cmp_it.charpos;
23373 glyph->object = it->object;
23374 glyph->pixel_width = it->pixel_width;
23375 glyph->ascent = it->ascent;
23376 glyph->descent = it->descent;
23377 glyph->voffset = it->voffset;
23378 glyph->type = COMPOSITE_GLYPH;
23379 if (it->cmp_it.ch < 0)
23380 {
23381 glyph->u.cmp.automatic = 0;
23382 glyph->u.cmp.id = it->cmp_it.id;
23383 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23384 }
23385 else
23386 {
23387 glyph->u.cmp.automatic = 1;
23388 glyph->u.cmp.id = it->cmp_it.id;
23389 glyph->slice.cmp.from = it->cmp_it.from;
23390 glyph->slice.cmp.to = it->cmp_it.to - 1;
23391 }
23392 glyph->avoid_cursor_p = it->avoid_cursor_p;
23393 glyph->multibyte_p = it->multibyte_p;
23394 glyph->left_box_line_p = it->start_of_box_run_p;
23395 glyph->right_box_line_p = it->end_of_box_run_p;
23396 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23397 || it->phys_descent > it->descent);
23398 glyph->padding_p = 0;
23399 glyph->glyph_not_available_p = 0;
23400 glyph->face_id = it->face_id;
23401 glyph->font_type = FONT_TYPE_UNKNOWN;
23402 if (it->bidi_p)
23403 {
23404 glyph->resolved_level = it->bidi_it.resolved_level;
23405 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23406 abort ();
23407 glyph->bidi_type = it->bidi_it.type;
23408 }
23409 ++it->glyph_row->used[area];
23410 }
23411 else
23412 IT_EXPAND_MATRIX_WIDTH (it, area);
23413 }
23414
23415
23416 /* Change IT->ascent and IT->height according to the setting of
23417 IT->voffset. */
23418
23419 static inline void
23420 take_vertical_position_into_account (struct it *it)
23421 {
23422 if (it->voffset)
23423 {
23424 if (it->voffset < 0)
23425 /* Increase the ascent so that we can display the text higher
23426 in the line. */
23427 it->ascent -= it->voffset;
23428 else
23429 /* Increase the descent so that we can display the text lower
23430 in the line. */
23431 it->descent += it->voffset;
23432 }
23433 }
23434
23435
23436 /* Produce glyphs/get display metrics for the image IT is loaded with.
23437 See the description of struct display_iterator in dispextern.h for
23438 an overview of struct display_iterator. */
23439
23440 static void
23441 produce_image_glyph (struct it *it)
23442 {
23443 struct image *img;
23444 struct face *face;
23445 int glyph_ascent, crop;
23446 struct glyph_slice slice;
23447
23448 xassert (it->what == IT_IMAGE);
23449
23450 face = FACE_FROM_ID (it->f, it->face_id);
23451 xassert (face);
23452 /* Make sure X resources of the face is loaded. */
23453 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23454
23455 if (it->image_id < 0)
23456 {
23457 /* Fringe bitmap. */
23458 it->ascent = it->phys_ascent = 0;
23459 it->descent = it->phys_descent = 0;
23460 it->pixel_width = 0;
23461 it->nglyphs = 0;
23462 return;
23463 }
23464
23465 img = IMAGE_FROM_ID (it->f, it->image_id);
23466 xassert (img);
23467 /* Make sure X resources of the image is loaded. */
23468 prepare_image_for_display (it->f, img);
23469
23470 slice.x = slice.y = 0;
23471 slice.width = img->width;
23472 slice.height = img->height;
23473
23474 if (INTEGERP (it->slice.x))
23475 slice.x = XINT (it->slice.x);
23476 else if (FLOATP (it->slice.x))
23477 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23478
23479 if (INTEGERP (it->slice.y))
23480 slice.y = XINT (it->slice.y);
23481 else if (FLOATP (it->slice.y))
23482 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23483
23484 if (INTEGERP (it->slice.width))
23485 slice.width = XINT (it->slice.width);
23486 else if (FLOATP (it->slice.width))
23487 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23488
23489 if (INTEGERP (it->slice.height))
23490 slice.height = XINT (it->slice.height);
23491 else if (FLOATP (it->slice.height))
23492 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23493
23494 if (slice.x >= img->width)
23495 slice.x = img->width;
23496 if (slice.y >= img->height)
23497 slice.y = img->height;
23498 if (slice.x + slice.width >= img->width)
23499 slice.width = img->width - slice.x;
23500 if (slice.y + slice.height > img->height)
23501 slice.height = img->height - slice.y;
23502
23503 if (slice.width == 0 || slice.height == 0)
23504 return;
23505
23506 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23507
23508 it->descent = slice.height - glyph_ascent;
23509 if (slice.y == 0)
23510 it->descent += img->vmargin;
23511 if (slice.y + slice.height == img->height)
23512 it->descent += img->vmargin;
23513 it->phys_descent = it->descent;
23514
23515 it->pixel_width = slice.width;
23516 if (slice.x == 0)
23517 it->pixel_width += img->hmargin;
23518 if (slice.x + slice.width == img->width)
23519 it->pixel_width += img->hmargin;
23520
23521 /* It's quite possible for images to have an ascent greater than
23522 their height, so don't get confused in that case. */
23523 if (it->descent < 0)
23524 it->descent = 0;
23525
23526 it->nglyphs = 1;
23527
23528 if (face->box != FACE_NO_BOX)
23529 {
23530 if (face->box_line_width > 0)
23531 {
23532 if (slice.y == 0)
23533 it->ascent += face->box_line_width;
23534 if (slice.y + slice.height == img->height)
23535 it->descent += face->box_line_width;
23536 }
23537
23538 if (it->start_of_box_run_p && slice.x == 0)
23539 it->pixel_width += eabs (face->box_line_width);
23540 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23541 it->pixel_width += eabs (face->box_line_width);
23542 }
23543
23544 take_vertical_position_into_account (it);
23545
23546 /* Automatically crop wide image glyphs at right edge so we can
23547 draw the cursor on same display row. */
23548 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23549 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23550 {
23551 it->pixel_width -= crop;
23552 slice.width -= crop;
23553 }
23554
23555 if (it->glyph_row)
23556 {
23557 struct glyph *glyph;
23558 enum glyph_row_area area = it->area;
23559
23560 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23561 if (glyph < it->glyph_row->glyphs[area + 1])
23562 {
23563 glyph->charpos = CHARPOS (it->position);
23564 glyph->object = it->object;
23565 glyph->pixel_width = it->pixel_width;
23566 glyph->ascent = glyph_ascent;
23567 glyph->descent = it->descent;
23568 glyph->voffset = it->voffset;
23569 glyph->type = IMAGE_GLYPH;
23570 glyph->avoid_cursor_p = it->avoid_cursor_p;
23571 glyph->multibyte_p = it->multibyte_p;
23572 glyph->left_box_line_p = it->start_of_box_run_p;
23573 glyph->right_box_line_p = it->end_of_box_run_p;
23574 glyph->overlaps_vertically_p = 0;
23575 glyph->padding_p = 0;
23576 glyph->glyph_not_available_p = 0;
23577 glyph->face_id = it->face_id;
23578 glyph->u.img_id = img->id;
23579 glyph->slice.img = slice;
23580 glyph->font_type = FONT_TYPE_UNKNOWN;
23581 if (it->bidi_p)
23582 {
23583 glyph->resolved_level = it->bidi_it.resolved_level;
23584 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23585 abort ();
23586 glyph->bidi_type = it->bidi_it.type;
23587 }
23588 ++it->glyph_row->used[area];
23589 }
23590 else
23591 IT_EXPAND_MATRIX_WIDTH (it, area);
23592 }
23593 }
23594
23595
23596 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23597 of the glyph, WIDTH and HEIGHT are the width and height of the
23598 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23599
23600 static void
23601 append_stretch_glyph (struct it *it, Lisp_Object object,
23602 int width, int height, int ascent)
23603 {
23604 struct glyph *glyph;
23605 enum glyph_row_area area = it->area;
23606
23607 xassert (ascent >= 0 && ascent <= height);
23608
23609 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23610 if (glyph < it->glyph_row->glyphs[area + 1])
23611 {
23612 /* If the glyph row is reversed, we need to prepend the glyph
23613 rather than append it. */
23614 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23615 {
23616 struct glyph *g;
23617
23618 /* Make room for the additional glyph. */
23619 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23620 g[1] = *g;
23621 glyph = it->glyph_row->glyphs[area];
23622 }
23623 glyph->charpos = CHARPOS (it->position);
23624 glyph->object = object;
23625 glyph->pixel_width = width;
23626 glyph->ascent = ascent;
23627 glyph->descent = height - ascent;
23628 glyph->voffset = it->voffset;
23629 glyph->type = STRETCH_GLYPH;
23630 glyph->avoid_cursor_p = it->avoid_cursor_p;
23631 glyph->multibyte_p = it->multibyte_p;
23632 glyph->left_box_line_p = it->start_of_box_run_p;
23633 glyph->right_box_line_p = it->end_of_box_run_p;
23634 glyph->overlaps_vertically_p = 0;
23635 glyph->padding_p = 0;
23636 glyph->glyph_not_available_p = 0;
23637 glyph->face_id = it->face_id;
23638 glyph->u.stretch.ascent = ascent;
23639 glyph->u.stretch.height = height;
23640 glyph->slice.img = null_glyph_slice;
23641 glyph->font_type = FONT_TYPE_UNKNOWN;
23642 if (it->bidi_p)
23643 {
23644 glyph->resolved_level = it->bidi_it.resolved_level;
23645 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23646 abort ();
23647 glyph->bidi_type = it->bidi_it.type;
23648 }
23649 else
23650 {
23651 glyph->resolved_level = 0;
23652 glyph->bidi_type = UNKNOWN_BT;
23653 }
23654 ++it->glyph_row->used[area];
23655 }
23656 else
23657 IT_EXPAND_MATRIX_WIDTH (it, area);
23658 }
23659
23660 #endif /* HAVE_WINDOW_SYSTEM */
23661
23662 /* Produce a stretch glyph for iterator IT. IT->object is the value
23663 of the glyph property displayed. The value must be a list
23664 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23665 being recognized:
23666
23667 1. `:width WIDTH' specifies that the space should be WIDTH *
23668 canonical char width wide. WIDTH may be an integer or floating
23669 point number.
23670
23671 2. `:relative-width FACTOR' specifies that the width of the stretch
23672 should be computed from the width of the first character having the
23673 `glyph' property, and should be FACTOR times that width.
23674
23675 3. `:align-to HPOS' specifies that the space should be wide enough
23676 to reach HPOS, a value in canonical character units.
23677
23678 Exactly one of the above pairs must be present.
23679
23680 4. `:height HEIGHT' specifies that the height of the stretch produced
23681 should be HEIGHT, measured in canonical character units.
23682
23683 5. `:relative-height FACTOR' specifies that the height of the
23684 stretch should be FACTOR times the height of the characters having
23685 the glyph property.
23686
23687 Either none or exactly one of 4 or 5 must be present.
23688
23689 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23690 of the stretch should be used for the ascent of the stretch.
23691 ASCENT must be in the range 0 <= ASCENT <= 100. */
23692
23693 void
23694 produce_stretch_glyph (struct it *it)
23695 {
23696 /* (space :width WIDTH :height HEIGHT ...) */
23697 Lisp_Object prop, plist;
23698 int width = 0, height = 0, align_to = -1;
23699 int zero_width_ok_p = 0;
23700 int ascent = 0;
23701 double tem;
23702 struct face *face = NULL;
23703 struct font *font = NULL;
23704
23705 #ifdef HAVE_WINDOW_SYSTEM
23706 int zero_height_ok_p = 0;
23707
23708 if (FRAME_WINDOW_P (it->f))
23709 {
23710 face = FACE_FROM_ID (it->f, it->face_id);
23711 font = face->font ? face->font : FRAME_FONT (it->f);
23712 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23713 }
23714 #endif
23715
23716 /* List should start with `space'. */
23717 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23718 plist = XCDR (it->object);
23719
23720 /* Compute the width of the stretch. */
23721 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23722 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23723 {
23724 /* Absolute width `:width WIDTH' specified and valid. */
23725 zero_width_ok_p = 1;
23726 width = (int)tem;
23727 }
23728 #ifdef HAVE_WINDOW_SYSTEM
23729 else if (FRAME_WINDOW_P (it->f)
23730 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23731 {
23732 /* Relative width `:relative-width FACTOR' specified and valid.
23733 Compute the width of the characters having the `glyph'
23734 property. */
23735 struct it it2;
23736 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23737
23738 it2 = *it;
23739 if (it->multibyte_p)
23740 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23741 else
23742 {
23743 it2.c = it2.char_to_display = *p, it2.len = 1;
23744 if (! ASCII_CHAR_P (it2.c))
23745 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23746 }
23747
23748 it2.glyph_row = NULL;
23749 it2.what = IT_CHARACTER;
23750 x_produce_glyphs (&it2);
23751 width = NUMVAL (prop) * it2.pixel_width;
23752 }
23753 #endif /* HAVE_WINDOW_SYSTEM */
23754 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23755 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23756 {
23757 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23758 align_to = (align_to < 0
23759 ? 0
23760 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23761 else if (align_to < 0)
23762 align_to = window_box_left_offset (it->w, TEXT_AREA);
23763 width = max (0, (int)tem + align_to - it->current_x);
23764 zero_width_ok_p = 1;
23765 }
23766 else
23767 /* Nothing specified -> width defaults to canonical char width. */
23768 width = FRAME_COLUMN_WIDTH (it->f);
23769
23770 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23771 width = 1;
23772
23773 #ifdef HAVE_WINDOW_SYSTEM
23774 /* Compute height. */
23775 if (FRAME_WINDOW_P (it->f))
23776 {
23777 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23778 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23779 {
23780 height = (int)tem;
23781 zero_height_ok_p = 1;
23782 }
23783 else if (prop = Fplist_get (plist, QCrelative_height),
23784 NUMVAL (prop) > 0)
23785 height = FONT_HEIGHT (font) * NUMVAL (prop);
23786 else
23787 height = FONT_HEIGHT (font);
23788
23789 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23790 height = 1;
23791
23792 /* Compute percentage of height used for ascent. If
23793 `:ascent ASCENT' is present and valid, use that. Otherwise,
23794 derive the ascent from the font in use. */
23795 if (prop = Fplist_get (plist, QCascent),
23796 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23797 ascent = height * NUMVAL (prop) / 100.0;
23798 else if (!NILP (prop)
23799 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23800 ascent = min (max (0, (int)tem), height);
23801 else
23802 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23803 }
23804 else
23805 #endif /* HAVE_WINDOW_SYSTEM */
23806 height = 1;
23807
23808 if (width > 0 && it->line_wrap != TRUNCATE
23809 && it->current_x + width > it->last_visible_x)
23810 {
23811 width = it->last_visible_x - it->current_x;
23812 #ifdef HAVE_WINDOW_SYSTEM
23813 /* Subtract one more pixel from the stretch width, but only on
23814 GUI frames, since on a TTY each glyph is one "pixel" wide. */
23815 width -= FRAME_WINDOW_P (it->f);
23816 #endif
23817 }
23818
23819 if (width > 0 && height > 0 && it->glyph_row)
23820 {
23821 Lisp_Object o_object = it->object;
23822 Lisp_Object object = it->stack[it->sp - 1].string;
23823 int n = width;
23824
23825 if (!STRINGP (object))
23826 object = it->w->buffer;
23827 #ifdef HAVE_WINDOW_SYSTEM
23828 if (FRAME_WINDOW_P (it->f))
23829 append_stretch_glyph (it, object, width, height, ascent);
23830 else
23831 #endif
23832 {
23833 it->object = object;
23834 it->char_to_display = ' ';
23835 it->pixel_width = it->len = 1;
23836 while (n--)
23837 tty_append_glyph (it);
23838 it->object = o_object;
23839 }
23840 }
23841
23842 it->pixel_width = width;
23843 #ifdef HAVE_WINDOW_SYSTEM
23844 if (FRAME_WINDOW_P (it->f))
23845 {
23846 it->ascent = it->phys_ascent = ascent;
23847 it->descent = it->phys_descent = height - it->ascent;
23848 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23849 take_vertical_position_into_account (it);
23850 }
23851 else
23852 #endif
23853 it->nglyphs = width;
23854 }
23855
23856 #ifdef HAVE_WINDOW_SYSTEM
23857
23858 /* Calculate line-height and line-spacing properties.
23859 An integer value specifies explicit pixel value.
23860 A float value specifies relative value to current face height.
23861 A cons (float . face-name) specifies relative value to
23862 height of specified face font.
23863
23864 Returns height in pixels, or nil. */
23865
23866
23867 static Lisp_Object
23868 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
23869 int boff, int override)
23870 {
23871 Lisp_Object face_name = Qnil;
23872 int ascent, descent, height;
23873
23874 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
23875 return val;
23876
23877 if (CONSP (val))
23878 {
23879 face_name = XCAR (val);
23880 val = XCDR (val);
23881 if (!NUMBERP (val))
23882 val = make_number (1);
23883 if (NILP (face_name))
23884 {
23885 height = it->ascent + it->descent;
23886 goto scale;
23887 }
23888 }
23889
23890 if (NILP (face_name))
23891 {
23892 font = FRAME_FONT (it->f);
23893 boff = FRAME_BASELINE_OFFSET (it->f);
23894 }
23895 else if (EQ (face_name, Qt))
23896 {
23897 override = 0;
23898 }
23899 else
23900 {
23901 int face_id;
23902 struct face *face;
23903
23904 face_id = lookup_named_face (it->f, face_name, 0);
23905 if (face_id < 0)
23906 return make_number (-1);
23907
23908 face = FACE_FROM_ID (it->f, face_id);
23909 font = face->font;
23910 if (font == NULL)
23911 return make_number (-1);
23912 boff = font->baseline_offset;
23913 if (font->vertical_centering)
23914 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23915 }
23916
23917 ascent = FONT_BASE (font) + boff;
23918 descent = FONT_DESCENT (font) - boff;
23919
23920 if (override)
23921 {
23922 it->override_ascent = ascent;
23923 it->override_descent = descent;
23924 it->override_boff = boff;
23925 }
23926
23927 height = ascent + descent;
23928
23929 scale:
23930 if (FLOATP (val))
23931 height = (int)(XFLOAT_DATA (val) * height);
23932 else if (INTEGERP (val))
23933 height *= XINT (val);
23934
23935 return make_number (height);
23936 }
23937
23938
23939 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
23940 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
23941 and only if this is for a character for which no font was found.
23942
23943 If the display method (it->glyphless_method) is
23944 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
23945 length of the acronym or the hexadecimal string, UPPER_XOFF and
23946 UPPER_YOFF are pixel offsets for the upper part of the string,
23947 LOWER_XOFF and LOWER_YOFF are for the lower part.
23948
23949 For the other display methods, LEN through LOWER_YOFF are zero. */
23950
23951 static void
23952 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
23953 short upper_xoff, short upper_yoff,
23954 short lower_xoff, short lower_yoff)
23955 {
23956 struct glyph *glyph;
23957 enum glyph_row_area area = it->area;
23958
23959 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23960 if (glyph < it->glyph_row->glyphs[area + 1])
23961 {
23962 /* If the glyph row is reversed, we need to prepend the glyph
23963 rather than append it. */
23964 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23965 {
23966 struct glyph *g;
23967
23968 /* Make room for the additional glyph. */
23969 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23970 g[1] = *g;
23971 glyph = it->glyph_row->glyphs[area];
23972 }
23973 glyph->charpos = CHARPOS (it->position);
23974 glyph->object = it->object;
23975 glyph->pixel_width = it->pixel_width;
23976 glyph->ascent = it->ascent;
23977 glyph->descent = it->descent;
23978 glyph->voffset = it->voffset;
23979 glyph->type = GLYPHLESS_GLYPH;
23980 glyph->u.glyphless.method = it->glyphless_method;
23981 glyph->u.glyphless.for_no_font = for_no_font;
23982 glyph->u.glyphless.len = len;
23983 glyph->u.glyphless.ch = it->c;
23984 glyph->slice.glyphless.upper_xoff = upper_xoff;
23985 glyph->slice.glyphless.upper_yoff = upper_yoff;
23986 glyph->slice.glyphless.lower_xoff = lower_xoff;
23987 glyph->slice.glyphless.lower_yoff = lower_yoff;
23988 glyph->avoid_cursor_p = it->avoid_cursor_p;
23989 glyph->multibyte_p = it->multibyte_p;
23990 glyph->left_box_line_p = it->start_of_box_run_p;
23991 glyph->right_box_line_p = it->end_of_box_run_p;
23992 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23993 || it->phys_descent > it->descent);
23994 glyph->padding_p = 0;
23995 glyph->glyph_not_available_p = 0;
23996 glyph->face_id = face_id;
23997 glyph->font_type = FONT_TYPE_UNKNOWN;
23998 if (it->bidi_p)
23999 {
24000 glyph->resolved_level = it->bidi_it.resolved_level;
24001 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24002 abort ();
24003 glyph->bidi_type = it->bidi_it.type;
24004 }
24005 ++it->glyph_row->used[area];
24006 }
24007 else
24008 IT_EXPAND_MATRIX_WIDTH (it, area);
24009 }
24010
24011
24012 /* Produce a glyph for a glyphless character for iterator IT.
24013 IT->glyphless_method specifies which method to use for displaying
24014 the character. See the description of enum
24015 glyphless_display_method in dispextern.h for the detail.
24016
24017 FOR_NO_FONT is nonzero if and only if this is for a character for
24018 which no font was found. ACRONYM, if non-nil, is an acronym string
24019 for the character. */
24020
24021 static void
24022 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24023 {
24024 int face_id;
24025 struct face *face;
24026 struct font *font;
24027 int base_width, base_height, width, height;
24028 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24029 int len;
24030
24031 /* Get the metrics of the base font. We always refer to the current
24032 ASCII face. */
24033 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24034 font = face->font ? face->font : FRAME_FONT (it->f);
24035 it->ascent = FONT_BASE (font) + font->baseline_offset;
24036 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24037 base_height = it->ascent + it->descent;
24038 base_width = font->average_width;
24039
24040 /* Get a face ID for the glyph by utilizing a cache (the same way as
24041 done for `escape-glyph' in get_next_display_element). */
24042 if (it->f == last_glyphless_glyph_frame
24043 && it->face_id == last_glyphless_glyph_face_id)
24044 {
24045 face_id = last_glyphless_glyph_merged_face_id;
24046 }
24047 else
24048 {
24049 /* Merge the `glyphless-char' face into the current face. */
24050 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24051 last_glyphless_glyph_frame = it->f;
24052 last_glyphless_glyph_face_id = it->face_id;
24053 last_glyphless_glyph_merged_face_id = face_id;
24054 }
24055
24056 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24057 {
24058 it->pixel_width = THIN_SPACE_WIDTH;
24059 len = 0;
24060 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24061 }
24062 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24063 {
24064 width = CHAR_WIDTH (it->c);
24065 if (width == 0)
24066 width = 1;
24067 else if (width > 4)
24068 width = 4;
24069 it->pixel_width = base_width * width;
24070 len = 0;
24071 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24072 }
24073 else
24074 {
24075 char buf[7];
24076 const char *str;
24077 unsigned int code[6];
24078 int upper_len;
24079 int ascent, descent;
24080 struct font_metrics metrics_upper, metrics_lower;
24081
24082 face = FACE_FROM_ID (it->f, face_id);
24083 font = face->font ? face->font : FRAME_FONT (it->f);
24084 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24085
24086 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24087 {
24088 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24089 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24090 if (CONSP (acronym))
24091 acronym = XCAR (acronym);
24092 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24093 }
24094 else
24095 {
24096 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24097 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24098 str = buf;
24099 }
24100 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24101 code[len] = font->driver->encode_char (font, str[len]);
24102 upper_len = (len + 1) / 2;
24103 font->driver->text_extents (font, code, upper_len,
24104 &metrics_upper);
24105 font->driver->text_extents (font, code + upper_len, len - upper_len,
24106 &metrics_lower);
24107
24108
24109
24110 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24111 width = max (metrics_upper.width, metrics_lower.width) + 4;
24112 upper_xoff = upper_yoff = 2; /* the typical case */
24113 if (base_width >= width)
24114 {
24115 /* Align the upper to the left, the lower to the right. */
24116 it->pixel_width = base_width;
24117 lower_xoff = base_width - 2 - metrics_lower.width;
24118 }
24119 else
24120 {
24121 /* Center the shorter one. */
24122 it->pixel_width = width;
24123 if (metrics_upper.width >= metrics_lower.width)
24124 lower_xoff = (width - metrics_lower.width) / 2;
24125 else
24126 {
24127 /* FIXME: This code doesn't look right. It formerly was
24128 missing the "lower_xoff = 0;", which couldn't have
24129 been right since it left lower_xoff uninitialized. */
24130 lower_xoff = 0;
24131 upper_xoff = (width - metrics_upper.width) / 2;
24132 }
24133 }
24134
24135 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24136 top, bottom, and between upper and lower strings. */
24137 height = (metrics_upper.ascent + metrics_upper.descent
24138 + metrics_lower.ascent + metrics_lower.descent) + 5;
24139 /* Center vertically.
24140 H:base_height, D:base_descent
24141 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24142
24143 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24144 descent = D - H/2 + h/2;
24145 lower_yoff = descent - 2 - ld;
24146 upper_yoff = lower_yoff - la - 1 - ud; */
24147 ascent = - (it->descent - (base_height + height + 1) / 2);
24148 descent = it->descent - (base_height - height) / 2;
24149 lower_yoff = descent - 2 - metrics_lower.descent;
24150 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24151 - metrics_upper.descent);
24152 /* Don't make the height shorter than the base height. */
24153 if (height > base_height)
24154 {
24155 it->ascent = ascent;
24156 it->descent = descent;
24157 }
24158 }
24159
24160 it->phys_ascent = it->ascent;
24161 it->phys_descent = it->descent;
24162 if (it->glyph_row)
24163 append_glyphless_glyph (it, face_id, for_no_font, len,
24164 upper_xoff, upper_yoff,
24165 lower_xoff, lower_yoff);
24166 it->nglyphs = 1;
24167 take_vertical_position_into_account (it);
24168 }
24169
24170
24171 /* RIF:
24172 Produce glyphs/get display metrics for the display element IT is
24173 loaded with. See the description of struct it in dispextern.h
24174 for an overview of struct it. */
24175
24176 void
24177 x_produce_glyphs (struct it *it)
24178 {
24179 int extra_line_spacing = it->extra_line_spacing;
24180
24181 it->glyph_not_available_p = 0;
24182
24183 if (it->what == IT_CHARACTER)
24184 {
24185 XChar2b char2b;
24186 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24187 struct font *font = face->font;
24188 struct font_metrics *pcm = NULL;
24189 int boff; /* baseline offset */
24190
24191 if (font == NULL)
24192 {
24193 /* When no suitable font is found, display this character by
24194 the method specified in the first extra slot of
24195 Vglyphless_char_display. */
24196 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24197
24198 xassert (it->what == IT_GLYPHLESS);
24199 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24200 goto done;
24201 }
24202
24203 boff = font->baseline_offset;
24204 if (font->vertical_centering)
24205 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24206
24207 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24208 {
24209 int stretched_p;
24210
24211 it->nglyphs = 1;
24212
24213 if (it->override_ascent >= 0)
24214 {
24215 it->ascent = it->override_ascent;
24216 it->descent = it->override_descent;
24217 boff = it->override_boff;
24218 }
24219 else
24220 {
24221 it->ascent = FONT_BASE (font) + boff;
24222 it->descent = FONT_DESCENT (font) - boff;
24223 }
24224
24225 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24226 {
24227 pcm = get_per_char_metric (font, &char2b);
24228 if (pcm->width == 0
24229 && pcm->rbearing == 0 && pcm->lbearing == 0)
24230 pcm = NULL;
24231 }
24232
24233 if (pcm)
24234 {
24235 it->phys_ascent = pcm->ascent + boff;
24236 it->phys_descent = pcm->descent - boff;
24237 it->pixel_width = pcm->width;
24238 }
24239 else
24240 {
24241 it->glyph_not_available_p = 1;
24242 it->phys_ascent = it->ascent;
24243 it->phys_descent = it->descent;
24244 it->pixel_width = font->space_width;
24245 }
24246
24247 if (it->constrain_row_ascent_descent_p)
24248 {
24249 if (it->descent > it->max_descent)
24250 {
24251 it->ascent += it->descent - it->max_descent;
24252 it->descent = it->max_descent;
24253 }
24254 if (it->ascent > it->max_ascent)
24255 {
24256 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24257 it->ascent = it->max_ascent;
24258 }
24259 it->phys_ascent = min (it->phys_ascent, it->ascent);
24260 it->phys_descent = min (it->phys_descent, it->descent);
24261 extra_line_spacing = 0;
24262 }
24263
24264 /* If this is a space inside a region of text with
24265 `space-width' property, change its width. */
24266 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24267 if (stretched_p)
24268 it->pixel_width *= XFLOATINT (it->space_width);
24269
24270 /* If face has a box, add the box thickness to the character
24271 height. If character has a box line to the left and/or
24272 right, add the box line width to the character's width. */
24273 if (face->box != FACE_NO_BOX)
24274 {
24275 int thick = face->box_line_width;
24276
24277 if (thick > 0)
24278 {
24279 it->ascent += thick;
24280 it->descent += thick;
24281 }
24282 else
24283 thick = -thick;
24284
24285 if (it->start_of_box_run_p)
24286 it->pixel_width += thick;
24287 if (it->end_of_box_run_p)
24288 it->pixel_width += thick;
24289 }
24290
24291 /* If face has an overline, add the height of the overline
24292 (1 pixel) and a 1 pixel margin to the character height. */
24293 if (face->overline_p)
24294 it->ascent += overline_margin;
24295
24296 if (it->constrain_row_ascent_descent_p)
24297 {
24298 if (it->ascent > it->max_ascent)
24299 it->ascent = it->max_ascent;
24300 if (it->descent > it->max_descent)
24301 it->descent = it->max_descent;
24302 }
24303
24304 take_vertical_position_into_account (it);
24305
24306 /* If we have to actually produce glyphs, do it. */
24307 if (it->glyph_row)
24308 {
24309 if (stretched_p)
24310 {
24311 /* Translate a space with a `space-width' property
24312 into a stretch glyph. */
24313 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24314 / FONT_HEIGHT (font));
24315 append_stretch_glyph (it, it->object, it->pixel_width,
24316 it->ascent + it->descent, ascent);
24317 }
24318 else
24319 append_glyph (it);
24320
24321 /* If characters with lbearing or rbearing are displayed
24322 in this line, record that fact in a flag of the
24323 glyph row. This is used to optimize X output code. */
24324 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24325 it->glyph_row->contains_overlapping_glyphs_p = 1;
24326 }
24327 if (! stretched_p && it->pixel_width == 0)
24328 /* We assure that all visible glyphs have at least 1-pixel
24329 width. */
24330 it->pixel_width = 1;
24331 }
24332 else if (it->char_to_display == '\n')
24333 {
24334 /* A newline has no width, but we need the height of the
24335 line. But if previous part of the line sets a height,
24336 don't increase that height */
24337
24338 Lisp_Object height;
24339 Lisp_Object total_height = Qnil;
24340
24341 it->override_ascent = -1;
24342 it->pixel_width = 0;
24343 it->nglyphs = 0;
24344
24345 height = get_it_property (it, Qline_height);
24346 /* Split (line-height total-height) list */
24347 if (CONSP (height)
24348 && CONSP (XCDR (height))
24349 && NILP (XCDR (XCDR (height))))
24350 {
24351 total_height = XCAR (XCDR (height));
24352 height = XCAR (height);
24353 }
24354 height = calc_line_height_property (it, height, font, boff, 1);
24355
24356 if (it->override_ascent >= 0)
24357 {
24358 it->ascent = it->override_ascent;
24359 it->descent = it->override_descent;
24360 boff = it->override_boff;
24361 }
24362 else
24363 {
24364 it->ascent = FONT_BASE (font) + boff;
24365 it->descent = FONT_DESCENT (font) - boff;
24366 }
24367
24368 if (EQ (height, Qt))
24369 {
24370 if (it->descent > it->max_descent)
24371 {
24372 it->ascent += it->descent - it->max_descent;
24373 it->descent = it->max_descent;
24374 }
24375 if (it->ascent > it->max_ascent)
24376 {
24377 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24378 it->ascent = it->max_ascent;
24379 }
24380 it->phys_ascent = min (it->phys_ascent, it->ascent);
24381 it->phys_descent = min (it->phys_descent, it->descent);
24382 it->constrain_row_ascent_descent_p = 1;
24383 extra_line_spacing = 0;
24384 }
24385 else
24386 {
24387 Lisp_Object spacing;
24388
24389 it->phys_ascent = it->ascent;
24390 it->phys_descent = it->descent;
24391
24392 if ((it->max_ascent > 0 || it->max_descent > 0)
24393 && face->box != FACE_NO_BOX
24394 && face->box_line_width > 0)
24395 {
24396 it->ascent += face->box_line_width;
24397 it->descent += face->box_line_width;
24398 }
24399 if (!NILP (height)
24400 && XINT (height) > it->ascent + it->descent)
24401 it->ascent = XINT (height) - it->descent;
24402
24403 if (!NILP (total_height))
24404 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24405 else
24406 {
24407 spacing = get_it_property (it, Qline_spacing);
24408 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24409 }
24410 if (INTEGERP (spacing))
24411 {
24412 extra_line_spacing = XINT (spacing);
24413 if (!NILP (total_height))
24414 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24415 }
24416 }
24417 }
24418 else /* i.e. (it->char_to_display == '\t') */
24419 {
24420 if (font->space_width > 0)
24421 {
24422 int tab_width = it->tab_width * font->space_width;
24423 int x = it->current_x + it->continuation_lines_width;
24424 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24425
24426 /* If the distance from the current position to the next tab
24427 stop is less than a space character width, use the
24428 tab stop after that. */
24429 if (next_tab_x - x < font->space_width)
24430 next_tab_x += tab_width;
24431
24432 it->pixel_width = next_tab_x - x;
24433 it->nglyphs = 1;
24434 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24435 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24436
24437 if (it->glyph_row)
24438 {
24439 append_stretch_glyph (it, it->object, it->pixel_width,
24440 it->ascent + it->descent, it->ascent);
24441 }
24442 }
24443 else
24444 {
24445 it->pixel_width = 0;
24446 it->nglyphs = 1;
24447 }
24448 }
24449 }
24450 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24451 {
24452 /* A static composition.
24453
24454 Note: A composition is represented as one glyph in the
24455 glyph matrix. There are no padding glyphs.
24456
24457 Important note: pixel_width, ascent, and descent are the
24458 values of what is drawn by draw_glyphs (i.e. the values of
24459 the overall glyphs composed). */
24460 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24461 int boff; /* baseline offset */
24462 struct composition *cmp = composition_table[it->cmp_it.id];
24463 int glyph_len = cmp->glyph_len;
24464 struct font *font = face->font;
24465
24466 it->nglyphs = 1;
24467
24468 /* If we have not yet calculated pixel size data of glyphs of
24469 the composition for the current face font, calculate them
24470 now. Theoretically, we have to check all fonts for the
24471 glyphs, but that requires much time and memory space. So,
24472 here we check only the font of the first glyph. This may
24473 lead to incorrect display, but it's very rare, and C-l
24474 (recenter-top-bottom) can correct the display anyway. */
24475 if (! cmp->font || cmp->font != font)
24476 {
24477 /* Ascent and descent of the font of the first character
24478 of this composition (adjusted by baseline offset).
24479 Ascent and descent of overall glyphs should not be less
24480 than these, respectively. */
24481 int font_ascent, font_descent, font_height;
24482 /* Bounding box of the overall glyphs. */
24483 int leftmost, rightmost, lowest, highest;
24484 int lbearing, rbearing;
24485 int i, width, ascent, descent;
24486 int left_padded = 0, right_padded = 0;
24487 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24488 XChar2b char2b;
24489 struct font_metrics *pcm;
24490 int font_not_found_p;
24491 EMACS_INT pos;
24492
24493 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24494 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24495 break;
24496 if (glyph_len < cmp->glyph_len)
24497 right_padded = 1;
24498 for (i = 0; i < glyph_len; i++)
24499 {
24500 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24501 break;
24502 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24503 }
24504 if (i > 0)
24505 left_padded = 1;
24506
24507 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24508 : IT_CHARPOS (*it));
24509 /* If no suitable font is found, use the default font. */
24510 font_not_found_p = font == NULL;
24511 if (font_not_found_p)
24512 {
24513 face = face->ascii_face;
24514 font = face->font;
24515 }
24516 boff = font->baseline_offset;
24517 if (font->vertical_centering)
24518 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24519 font_ascent = FONT_BASE (font) + boff;
24520 font_descent = FONT_DESCENT (font) - boff;
24521 font_height = FONT_HEIGHT (font);
24522
24523 cmp->font = (void *) font;
24524
24525 pcm = NULL;
24526 if (! font_not_found_p)
24527 {
24528 get_char_face_and_encoding (it->f, c, it->face_id,
24529 &char2b, 0);
24530 pcm = get_per_char_metric (font, &char2b);
24531 }
24532
24533 /* Initialize the bounding box. */
24534 if (pcm)
24535 {
24536 width = cmp->glyph_len > 0 ? pcm->width : 0;
24537 ascent = pcm->ascent;
24538 descent = pcm->descent;
24539 lbearing = pcm->lbearing;
24540 rbearing = pcm->rbearing;
24541 }
24542 else
24543 {
24544 width = cmp->glyph_len > 0 ? font->space_width : 0;
24545 ascent = FONT_BASE (font);
24546 descent = FONT_DESCENT (font);
24547 lbearing = 0;
24548 rbearing = width;
24549 }
24550
24551 rightmost = width;
24552 leftmost = 0;
24553 lowest = - descent + boff;
24554 highest = ascent + boff;
24555
24556 if (! font_not_found_p
24557 && font->default_ascent
24558 && CHAR_TABLE_P (Vuse_default_ascent)
24559 && !NILP (Faref (Vuse_default_ascent,
24560 make_number (it->char_to_display))))
24561 highest = font->default_ascent + boff;
24562
24563 /* Draw the first glyph at the normal position. It may be
24564 shifted to right later if some other glyphs are drawn
24565 at the left. */
24566 cmp->offsets[i * 2] = 0;
24567 cmp->offsets[i * 2 + 1] = boff;
24568 cmp->lbearing = lbearing;
24569 cmp->rbearing = rbearing;
24570
24571 /* Set cmp->offsets for the remaining glyphs. */
24572 for (i++; i < glyph_len; i++)
24573 {
24574 int left, right, btm, top;
24575 int ch = COMPOSITION_GLYPH (cmp, i);
24576 int face_id;
24577 struct face *this_face;
24578
24579 if (ch == '\t')
24580 ch = ' ';
24581 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24582 this_face = FACE_FROM_ID (it->f, face_id);
24583 font = this_face->font;
24584
24585 if (font == NULL)
24586 pcm = NULL;
24587 else
24588 {
24589 get_char_face_and_encoding (it->f, ch, face_id,
24590 &char2b, 0);
24591 pcm = get_per_char_metric (font, &char2b);
24592 }
24593 if (! pcm)
24594 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24595 else
24596 {
24597 width = pcm->width;
24598 ascent = pcm->ascent;
24599 descent = pcm->descent;
24600 lbearing = pcm->lbearing;
24601 rbearing = pcm->rbearing;
24602 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24603 {
24604 /* Relative composition with or without
24605 alternate chars. */
24606 left = (leftmost + rightmost - width) / 2;
24607 btm = - descent + boff;
24608 if (font->relative_compose
24609 && (! CHAR_TABLE_P (Vignore_relative_composition)
24610 || NILP (Faref (Vignore_relative_composition,
24611 make_number (ch)))))
24612 {
24613
24614 if (- descent >= font->relative_compose)
24615 /* One extra pixel between two glyphs. */
24616 btm = highest + 1;
24617 else if (ascent <= 0)
24618 /* One extra pixel between two glyphs. */
24619 btm = lowest - 1 - ascent - descent;
24620 }
24621 }
24622 else
24623 {
24624 /* A composition rule is specified by an integer
24625 value that encodes global and new reference
24626 points (GREF and NREF). GREF and NREF are
24627 specified by numbers as below:
24628
24629 0---1---2 -- ascent
24630 | |
24631 | |
24632 | |
24633 9--10--11 -- center
24634 | |
24635 ---3---4---5--- baseline
24636 | |
24637 6---7---8 -- descent
24638 */
24639 int rule = COMPOSITION_RULE (cmp, i);
24640 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24641
24642 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24643 grefx = gref % 3, nrefx = nref % 3;
24644 grefy = gref / 3, nrefy = nref / 3;
24645 if (xoff)
24646 xoff = font_height * (xoff - 128) / 256;
24647 if (yoff)
24648 yoff = font_height * (yoff - 128) / 256;
24649
24650 left = (leftmost
24651 + grefx * (rightmost - leftmost) / 2
24652 - nrefx * width / 2
24653 + xoff);
24654
24655 btm = ((grefy == 0 ? highest
24656 : grefy == 1 ? 0
24657 : grefy == 2 ? lowest
24658 : (highest + lowest) / 2)
24659 - (nrefy == 0 ? ascent + descent
24660 : nrefy == 1 ? descent - boff
24661 : nrefy == 2 ? 0
24662 : (ascent + descent) / 2)
24663 + yoff);
24664 }
24665
24666 cmp->offsets[i * 2] = left;
24667 cmp->offsets[i * 2 + 1] = btm + descent;
24668
24669 /* Update the bounding box of the overall glyphs. */
24670 if (width > 0)
24671 {
24672 right = left + width;
24673 if (left < leftmost)
24674 leftmost = left;
24675 if (right > rightmost)
24676 rightmost = right;
24677 }
24678 top = btm + descent + ascent;
24679 if (top > highest)
24680 highest = top;
24681 if (btm < lowest)
24682 lowest = btm;
24683
24684 if (cmp->lbearing > left + lbearing)
24685 cmp->lbearing = left + lbearing;
24686 if (cmp->rbearing < left + rbearing)
24687 cmp->rbearing = left + rbearing;
24688 }
24689 }
24690
24691 /* If there are glyphs whose x-offsets are negative,
24692 shift all glyphs to the right and make all x-offsets
24693 non-negative. */
24694 if (leftmost < 0)
24695 {
24696 for (i = 0; i < cmp->glyph_len; i++)
24697 cmp->offsets[i * 2] -= leftmost;
24698 rightmost -= leftmost;
24699 cmp->lbearing -= leftmost;
24700 cmp->rbearing -= leftmost;
24701 }
24702
24703 if (left_padded && cmp->lbearing < 0)
24704 {
24705 for (i = 0; i < cmp->glyph_len; i++)
24706 cmp->offsets[i * 2] -= cmp->lbearing;
24707 rightmost -= cmp->lbearing;
24708 cmp->rbearing -= cmp->lbearing;
24709 cmp->lbearing = 0;
24710 }
24711 if (right_padded && rightmost < cmp->rbearing)
24712 {
24713 rightmost = cmp->rbearing;
24714 }
24715
24716 cmp->pixel_width = rightmost;
24717 cmp->ascent = highest;
24718 cmp->descent = - lowest;
24719 if (cmp->ascent < font_ascent)
24720 cmp->ascent = font_ascent;
24721 if (cmp->descent < font_descent)
24722 cmp->descent = font_descent;
24723 }
24724
24725 if (it->glyph_row
24726 && (cmp->lbearing < 0
24727 || cmp->rbearing > cmp->pixel_width))
24728 it->glyph_row->contains_overlapping_glyphs_p = 1;
24729
24730 it->pixel_width = cmp->pixel_width;
24731 it->ascent = it->phys_ascent = cmp->ascent;
24732 it->descent = it->phys_descent = cmp->descent;
24733 if (face->box != FACE_NO_BOX)
24734 {
24735 int thick = face->box_line_width;
24736
24737 if (thick > 0)
24738 {
24739 it->ascent += thick;
24740 it->descent += thick;
24741 }
24742 else
24743 thick = - thick;
24744
24745 if (it->start_of_box_run_p)
24746 it->pixel_width += thick;
24747 if (it->end_of_box_run_p)
24748 it->pixel_width += thick;
24749 }
24750
24751 /* If face has an overline, add the height of the overline
24752 (1 pixel) and a 1 pixel margin to the character height. */
24753 if (face->overline_p)
24754 it->ascent += overline_margin;
24755
24756 take_vertical_position_into_account (it);
24757 if (it->ascent < 0)
24758 it->ascent = 0;
24759 if (it->descent < 0)
24760 it->descent = 0;
24761
24762 if (it->glyph_row && cmp->glyph_len > 0)
24763 append_composite_glyph (it);
24764 }
24765 else if (it->what == IT_COMPOSITION)
24766 {
24767 /* A dynamic (automatic) composition. */
24768 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24769 Lisp_Object gstring;
24770 struct font_metrics metrics;
24771
24772 it->nglyphs = 1;
24773
24774 gstring = composition_gstring_from_id (it->cmp_it.id);
24775 it->pixel_width
24776 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24777 &metrics);
24778 if (it->glyph_row
24779 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24780 it->glyph_row->contains_overlapping_glyphs_p = 1;
24781 it->ascent = it->phys_ascent = metrics.ascent;
24782 it->descent = it->phys_descent = metrics.descent;
24783 if (face->box != FACE_NO_BOX)
24784 {
24785 int thick = face->box_line_width;
24786
24787 if (thick > 0)
24788 {
24789 it->ascent += thick;
24790 it->descent += thick;
24791 }
24792 else
24793 thick = - thick;
24794
24795 if (it->start_of_box_run_p)
24796 it->pixel_width += thick;
24797 if (it->end_of_box_run_p)
24798 it->pixel_width += thick;
24799 }
24800 /* If face has an overline, add the height of the overline
24801 (1 pixel) and a 1 pixel margin to the character height. */
24802 if (face->overline_p)
24803 it->ascent += overline_margin;
24804 take_vertical_position_into_account (it);
24805 if (it->ascent < 0)
24806 it->ascent = 0;
24807 if (it->descent < 0)
24808 it->descent = 0;
24809
24810 if (it->glyph_row)
24811 append_composite_glyph (it);
24812 }
24813 else if (it->what == IT_GLYPHLESS)
24814 produce_glyphless_glyph (it, 0, Qnil);
24815 else if (it->what == IT_IMAGE)
24816 produce_image_glyph (it);
24817 else if (it->what == IT_STRETCH)
24818 produce_stretch_glyph (it);
24819
24820 done:
24821 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24822 because this isn't true for images with `:ascent 100'. */
24823 xassert (it->ascent >= 0 && it->descent >= 0);
24824 if (it->area == TEXT_AREA)
24825 it->current_x += it->pixel_width;
24826
24827 if (extra_line_spacing > 0)
24828 {
24829 it->descent += extra_line_spacing;
24830 if (extra_line_spacing > it->max_extra_line_spacing)
24831 it->max_extra_line_spacing = extra_line_spacing;
24832 }
24833
24834 it->max_ascent = max (it->max_ascent, it->ascent);
24835 it->max_descent = max (it->max_descent, it->descent);
24836 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24837 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24838 }
24839
24840 /* EXPORT for RIF:
24841 Output LEN glyphs starting at START at the nominal cursor position.
24842 Advance the nominal cursor over the text. The global variable
24843 updated_window contains the window being updated, updated_row is
24844 the glyph row being updated, and updated_area is the area of that
24845 row being updated. */
24846
24847 void
24848 x_write_glyphs (struct glyph *start, int len)
24849 {
24850 int x, hpos, chpos = updated_window->phys_cursor.hpos;
24851
24852 xassert (updated_window && updated_row);
24853 /* When the window is hscrolled, cursor hpos can legitimately be out
24854 of bounds, but we draw the cursor at the corresponding window
24855 margin in that case. */
24856 if (!updated_row->reversed_p && chpos < 0)
24857 chpos = 0;
24858 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
24859 chpos = updated_row->used[TEXT_AREA] - 1;
24860
24861 BLOCK_INPUT;
24862
24863 /* Write glyphs. */
24864
24865 hpos = start - updated_row->glyphs[updated_area];
24866 x = draw_glyphs (updated_window, output_cursor.x,
24867 updated_row, updated_area,
24868 hpos, hpos + len,
24869 DRAW_NORMAL_TEXT, 0);
24870
24871 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
24872 if (updated_area == TEXT_AREA
24873 && updated_window->phys_cursor_on_p
24874 && updated_window->phys_cursor.vpos == output_cursor.vpos
24875 && chpos >= hpos
24876 && chpos < hpos + len)
24877 updated_window->phys_cursor_on_p = 0;
24878
24879 UNBLOCK_INPUT;
24880
24881 /* Advance the output cursor. */
24882 output_cursor.hpos += len;
24883 output_cursor.x = x;
24884 }
24885
24886
24887 /* EXPORT for RIF:
24888 Insert LEN glyphs from START at the nominal cursor position. */
24889
24890 void
24891 x_insert_glyphs (struct glyph *start, int len)
24892 {
24893 struct frame *f;
24894 struct window *w;
24895 int line_height, shift_by_width, shifted_region_width;
24896 struct glyph_row *row;
24897 struct glyph *glyph;
24898 int frame_x, frame_y;
24899 EMACS_INT hpos;
24900
24901 xassert (updated_window && updated_row);
24902 BLOCK_INPUT;
24903 w = updated_window;
24904 f = XFRAME (WINDOW_FRAME (w));
24905
24906 /* Get the height of the line we are in. */
24907 row = updated_row;
24908 line_height = row->height;
24909
24910 /* Get the width of the glyphs to insert. */
24911 shift_by_width = 0;
24912 for (glyph = start; glyph < start + len; ++glyph)
24913 shift_by_width += glyph->pixel_width;
24914
24915 /* Get the width of the region to shift right. */
24916 shifted_region_width = (window_box_width (w, updated_area)
24917 - output_cursor.x
24918 - shift_by_width);
24919
24920 /* Shift right. */
24921 frame_x = window_box_left (w, updated_area) + output_cursor.x;
24922 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
24923
24924 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
24925 line_height, shift_by_width);
24926
24927 /* Write the glyphs. */
24928 hpos = start - row->glyphs[updated_area];
24929 draw_glyphs (w, output_cursor.x, row, updated_area,
24930 hpos, hpos + len,
24931 DRAW_NORMAL_TEXT, 0);
24932
24933 /* Advance the output cursor. */
24934 output_cursor.hpos += len;
24935 output_cursor.x += shift_by_width;
24936 UNBLOCK_INPUT;
24937 }
24938
24939
24940 /* EXPORT for RIF:
24941 Erase the current text line from the nominal cursor position
24942 (inclusive) to pixel column TO_X (exclusive). The idea is that
24943 everything from TO_X onward is already erased.
24944
24945 TO_X is a pixel position relative to updated_area of
24946 updated_window. TO_X == -1 means clear to the end of this area. */
24947
24948 void
24949 x_clear_end_of_line (int to_x)
24950 {
24951 struct frame *f;
24952 struct window *w = updated_window;
24953 int max_x, min_y, max_y;
24954 int from_x, from_y, to_y;
24955
24956 xassert (updated_window && updated_row);
24957 f = XFRAME (w->frame);
24958
24959 if (updated_row->full_width_p)
24960 max_x = WINDOW_TOTAL_WIDTH (w);
24961 else
24962 max_x = window_box_width (w, updated_area);
24963 max_y = window_text_bottom_y (w);
24964
24965 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
24966 of window. For TO_X > 0, truncate to end of drawing area. */
24967 if (to_x == 0)
24968 return;
24969 else if (to_x < 0)
24970 to_x = max_x;
24971 else
24972 to_x = min (to_x, max_x);
24973
24974 to_y = min (max_y, output_cursor.y + updated_row->height);
24975
24976 /* Notice if the cursor will be cleared by this operation. */
24977 if (!updated_row->full_width_p)
24978 notice_overwritten_cursor (w, updated_area,
24979 output_cursor.x, -1,
24980 updated_row->y,
24981 MATRIX_ROW_BOTTOM_Y (updated_row));
24982
24983 from_x = output_cursor.x;
24984
24985 /* Translate to frame coordinates. */
24986 if (updated_row->full_width_p)
24987 {
24988 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
24989 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
24990 }
24991 else
24992 {
24993 int area_left = window_box_left (w, updated_area);
24994 from_x += area_left;
24995 to_x += area_left;
24996 }
24997
24998 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
24999 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25000 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25001
25002 /* Prevent inadvertently clearing to end of the X window. */
25003 if (to_x > from_x && to_y > from_y)
25004 {
25005 BLOCK_INPUT;
25006 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25007 to_x - from_x, to_y - from_y);
25008 UNBLOCK_INPUT;
25009 }
25010 }
25011
25012 #endif /* HAVE_WINDOW_SYSTEM */
25013
25014
25015 \f
25016 /***********************************************************************
25017 Cursor types
25018 ***********************************************************************/
25019
25020 /* Value is the internal representation of the specified cursor type
25021 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25022 of the bar cursor. */
25023
25024 static enum text_cursor_kinds
25025 get_specified_cursor_type (Lisp_Object arg, int *width)
25026 {
25027 enum text_cursor_kinds type;
25028
25029 if (NILP (arg))
25030 return NO_CURSOR;
25031
25032 if (EQ (arg, Qbox))
25033 return FILLED_BOX_CURSOR;
25034
25035 if (EQ (arg, Qhollow))
25036 return HOLLOW_BOX_CURSOR;
25037
25038 if (EQ (arg, Qbar))
25039 {
25040 *width = 2;
25041 return BAR_CURSOR;
25042 }
25043
25044 if (CONSP (arg)
25045 && EQ (XCAR (arg), Qbar)
25046 && INTEGERP (XCDR (arg))
25047 && XINT (XCDR (arg)) >= 0)
25048 {
25049 *width = XINT (XCDR (arg));
25050 return BAR_CURSOR;
25051 }
25052
25053 if (EQ (arg, Qhbar))
25054 {
25055 *width = 2;
25056 return HBAR_CURSOR;
25057 }
25058
25059 if (CONSP (arg)
25060 && EQ (XCAR (arg), Qhbar)
25061 && INTEGERP (XCDR (arg))
25062 && XINT (XCDR (arg)) >= 0)
25063 {
25064 *width = XINT (XCDR (arg));
25065 return HBAR_CURSOR;
25066 }
25067
25068 /* Treat anything unknown as "hollow box cursor".
25069 It was bad to signal an error; people have trouble fixing
25070 .Xdefaults with Emacs, when it has something bad in it. */
25071 type = HOLLOW_BOX_CURSOR;
25072
25073 return type;
25074 }
25075
25076 /* Set the default cursor types for specified frame. */
25077 void
25078 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25079 {
25080 int width = 1;
25081 Lisp_Object tem;
25082
25083 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25084 FRAME_CURSOR_WIDTH (f) = width;
25085
25086 /* By default, set up the blink-off state depending on the on-state. */
25087
25088 tem = Fassoc (arg, Vblink_cursor_alist);
25089 if (!NILP (tem))
25090 {
25091 FRAME_BLINK_OFF_CURSOR (f)
25092 = get_specified_cursor_type (XCDR (tem), &width);
25093 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25094 }
25095 else
25096 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25097 }
25098
25099
25100 #ifdef HAVE_WINDOW_SYSTEM
25101
25102 /* Return the cursor we want to be displayed in window W. Return
25103 width of bar/hbar cursor through WIDTH arg. Return with
25104 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25105 (i.e. if the `system caret' should track this cursor).
25106
25107 In a mini-buffer window, we want the cursor only to appear if we
25108 are reading input from this window. For the selected window, we
25109 want the cursor type given by the frame parameter or buffer local
25110 setting of cursor-type. If explicitly marked off, draw no cursor.
25111 In all other cases, we want a hollow box cursor. */
25112
25113 static enum text_cursor_kinds
25114 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25115 int *active_cursor)
25116 {
25117 struct frame *f = XFRAME (w->frame);
25118 struct buffer *b = XBUFFER (w->buffer);
25119 int cursor_type = DEFAULT_CURSOR;
25120 Lisp_Object alt_cursor;
25121 int non_selected = 0;
25122
25123 *active_cursor = 1;
25124
25125 /* Echo area */
25126 if (cursor_in_echo_area
25127 && FRAME_HAS_MINIBUF_P (f)
25128 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25129 {
25130 if (w == XWINDOW (echo_area_window))
25131 {
25132 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25133 {
25134 *width = FRAME_CURSOR_WIDTH (f);
25135 return FRAME_DESIRED_CURSOR (f);
25136 }
25137 else
25138 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25139 }
25140
25141 *active_cursor = 0;
25142 non_selected = 1;
25143 }
25144
25145 /* Detect a nonselected window or nonselected frame. */
25146 else if (w != XWINDOW (f->selected_window)
25147 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25148 {
25149 *active_cursor = 0;
25150
25151 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25152 return NO_CURSOR;
25153
25154 non_selected = 1;
25155 }
25156
25157 /* Never display a cursor in a window in which cursor-type is nil. */
25158 if (NILP (BVAR (b, cursor_type)))
25159 return NO_CURSOR;
25160
25161 /* Get the normal cursor type for this window. */
25162 if (EQ (BVAR (b, cursor_type), Qt))
25163 {
25164 cursor_type = FRAME_DESIRED_CURSOR (f);
25165 *width = FRAME_CURSOR_WIDTH (f);
25166 }
25167 else
25168 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25169
25170 /* Use cursor-in-non-selected-windows instead
25171 for non-selected window or frame. */
25172 if (non_selected)
25173 {
25174 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25175 if (!EQ (Qt, alt_cursor))
25176 return get_specified_cursor_type (alt_cursor, width);
25177 /* t means modify the normal cursor type. */
25178 if (cursor_type == FILLED_BOX_CURSOR)
25179 cursor_type = HOLLOW_BOX_CURSOR;
25180 else if (cursor_type == BAR_CURSOR && *width > 1)
25181 --*width;
25182 return cursor_type;
25183 }
25184
25185 /* Use normal cursor if not blinked off. */
25186 if (!w->cursor_off_p)
25187 {
25188 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25189 {
25190 if (cursor_type == FILLED_BOX_CURSOR)
25191 {
25192 /* Using a block cursor on large images can be very annoying.
25193 So use a hollow cursor for "large" images.
25194 If image is not transparent (no mask), also use hollow cursor. */
25195 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25196 if (img != NULL && IMAGEP (img->spec))
25197 {
25198 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25199 where N = size of default frame font size.
25200 This should cover most of the "tiny" icons people may use. */
25201 if (!img->mask
25202 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25203 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25204 cursor_type = HOLLOW_BOX_CURSOR;
25205 }
25206 }
25207 else if (cursor_type != NO_CURSOR)
25208 {
25209 /* Display current only supports BOX and HOLLOW cursors for images.
25210 So for now, unconditionally use a HOLLOW cursor when cursor is
25211 not a solid box cursor. */
25212 cursor_type = HOLLOW_BOX_CURSOR;
25213 }
25214 }
25215 return cursor_type;
25216 }
25217
25218 /* Cursor is blinked off, so determine how to "toggle" it. */
25219
25220 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25221 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25222 return get_specified_cursor_type (XCDR (alt_cursor), width);
25223
25224 /* Then see if frame has specified a specific blink off cursor type. */
25225 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25226 {
25227 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25228 return FRAME_BLINK_OFF_CURSOR (f);
25229 }
25230
25231 #if 0
25232 /* Some people liked having a permanently visible blinking cursor,
25233 while others had very strong opinions against it. So it was
25234 decided to remove it. KFS 2003-09-03 */
25235
25236 /* Finally perform built-in cursor blinking:
25237 filled box <-> hollow box
25238 wide [h]bar <-> narrow [h]bar
25239 narrow [h]bar <-> no cursor
25240 other type <-> no cursor */
25241
25242 if (cursor_type == FILLED_BOX_CURSOR)
25243 return HOLLOW_BOX_CURSOR;
25244
25245 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25246 {
25247 *width = 1;
25248 return cursor_type;
25249 }
25250 #endif
25251
25252 return NO_CURSOR;
25253 }
25254
25255
25256 /* Notice when the text cursor of window W has been completely
25257 overwritten by a drawing operation that outputs glyphs in AREA
25258 starting at X0 and ending at X1 in the line starting at Y0 and
25259 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25260 the rest of the line after X0 has been written. Y coordinates
25261 are window-relative. */
25262
25263 static void
25264 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25265 int x0, int x1, int y0, int y1)
25266 {
25267 int cx0, cx1, cy0, cy1;
25268 struct glyph_row *row;
25269
25270 if (!w->phys_cursor_on_p)
25271 return;
25272 if (area != TEXT_AREA)
25273 return;
25274
25275 if (w->phys_cursor.vpos < 0
25276 || w->phys_cursor.vpos >= w->current_matrix->nrows
25277 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25278 !(row->enabled_p && row->displays_text_p)))
25279 return;
25280
25281 if (row->cursor_in_fringe_p)
25282 {
25283 row->cursor_in_fringe_p = 0;
25284 draw_fringe_bitmap (w, row, row->reversed_p);
25285 w->phys_cursor_on_p = 0;
25286 return;
25287 }
25288
25289 cx0 = w->phys_cursor.x;
25290 cx1 = cx0 + w->phys_cursor_width;
25291 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25292 return;
25293
25294 /* The cursor image will be completely removed from the
25295 screen if the output area intersects the cursor area in
25296 y-direction. When we draw in [y0 y1[, and some part of
25297 the cursor is at y < y0, that part must have been drawn
25298 before. When scrolling, the cursor is erased before
25299 actually scrolling, so we don't come here. When not
25300 scrolling, the rows above the old cursor row must have
25301 changed, and in this case these rows must have written
25302 over the cursor image.
25303
25304 Likewise if part of the cursor is below y1, with the
25305 exception of the cursor being in the first blank row at
25306 the buffer and window end because update_text_area
25307 doesn't draw that row. (Except when it does, but
25308 that's handled in update_text_area.) */
25309
25310 cy0 = w->phys_cursor.y;
25311 cy1 = cy0 + w->phys_cursor_height;
25312 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25313 return;
25314
25315 w->phys_cursor_on_p = 0;
25316 }
25317
25318 #endif /* HAVE_WINDOW_SYSTEM */
25319
25320 \f
25321 /************************************************************************
25322 Mouse Face
25323 ************************************************************************/
25324
25325 #ifdef HAVE_WINDOW_SYSTEM
25326
25327 /* EXPORT for RIF:
25328 Fix the display of area AREA of overlapping row ROW in window W
25329 with respect to the overlapping part OVERLAPS. */
25330
25331 void
25332 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25333 enum glyph_row_area area, int overlaps)
25334 {
25335 int i, x;
25336
25337 BLOCK_INPUT;
25338
25339 x = 0;
25340 for (i = 0; i < row->used[area];)
25341 {
25342 if (row->glyphs[area][i].overlaps_vertically_p)
25343 {
25344 int start = i, start_x = x;
25345
25346 do
25347 {
25348 x += row->glyphs[area][i].pixel_width;
25349 ++i;
25350 }
25351 while (i < row->used[area]
25352 && row->glyphs[area][i].overlaps_vertically_p);
25353
25354 draw_glyphs (w, start_x, row, area,
25355 start, i,
25356 DRAW_NORMAL_TEXT, overlaps);
25357 }
25358 else
25359 {
25360 x += row->glyphs[area][i].pixel_width;
25361 ++i;
25362 }
25363 }
25364
25365 UNBLOCK_INPUT;
25366 }
25367
25368
25369 /* EXPORT:
25370 Draw the cursor glyph of window W in glyph row ROW. See the
25371 comment of draw_glyphs for the meaning of HL. */
25372
25373 void
25374 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25375 enum draw_glyphs_face hl)
25376 {
25377 /* If cursor hpos is out of bounds, don't draw garbage. This can
25378 happen in mini-buffer windows when switching between echo area
25379 glyphs and mini-buffer. */
25380 if ((row->reversed_p
25381 ? (w->phys_cursor.hpos >= 0)
25382 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25383 {
25384 int on_p = w->phys_cursor_on_p;
25385 int x1;
25386 int hpos = w->phys_cursor.hpos;
25387
25388 /* When the window is hscrolled, cursor hpos can legitimately be
25389 out of bounds, but we draw the cursor at the corresponding
25390 window margin in that case. */
25391 if (!row->reversed_p && hpos < 0)
25392 hpos = 0;
25393 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25394 hpos = row->used[TEXT_AREA] - 1;
25395
25396 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25397 hl, 0);
25398 w->phys_cursor_on_p = on_p;
25399
25400 if (hl == DRAW_CURSOR)
25401 w->phys_cursor_width = x1 - w->phys_cursor.x;
25402 /* When we erase the cursor, and ROW is overlapped by other
25403 rows, make sure that these overlapping parts of other rows
25404 are redrawn. */
25405 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25406 {
25407 w->phys_cursor_width = x1 - w->phys_cursor.x;
25408
25409 if (row > w->current_matrix->rows
25410 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25411 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25412 OVERLAPS_ERASED_CURSOR);
25413
25414 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25415 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25416 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25417 OVERLAPS_ERASED_CURSOR);
25418 }
25419 }
25420 }
25421
25422
25423 /* EXPORT:
25424 Erase the image of a cursor of window W from the screen. */
25425
25426 void
25427 erase_phys_cursor (struct window *w)
25428 {
25429 struct frame *f = XFRAME (w->frame);
25430 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25431 int hpos = w->phys_cursor.hpos;
25432 int vpos = w->phys_cursor.vpos;
25433 int mouse_face_here_p = 0;
25434 struct glyph_matrix *active_glyphs = w->current_matrix;
25435 struct glyph_row *cursor_row;
25436 struct glyph *cursor_glyph;
25437 enum draw_glyphs_face hl;
25438
25439 /* No cursor displayed or row invalidated => nothing to do on the
25440 screen. */
25441 if (w->phys_cursor_type == NO_CURSOR)
25442 goto mark_cursor_off;
25443
25444 /* VPOS >= active_glyphs->nrows means that window has been resized.
25445 Don't bother to erase the cursor. */
25446 if (vpos >= active_glyphs->nrows)
25447 goto mark_cursor_off;
25448
25449 /* If row containing cursor is marked invalid, there is nothing we
25450 can do. */
25451 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25452 if (!cursor_row->enabled_p)
25453 goto mark_cursor_off;
25454
25455 /* If line spacing is > 0, old cursor may only be partially visible in
25456 window after split-window. So adjust visible height. */
25457 cursor_row->visible_height = min (cursor_row->visible_height,
25458 window_text_bottom_y (w) - cursor_row->y);
25459
25460 /* If row is completely invisible, don't attempt to delete a cursor which
25461 isn't there. This can happen if cursor is at top of a window, and
25462 we switch to a buffer with a header line in that window. */
25463 if (cursor_row->visible_height <= 0)
25464 goto mark_cursor_off;
25465
25466 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25467 if (cursor_row->cursor_in_fringe_p)
25468 {
25469 cursor_row->cursor_in_fringe_p = 0;
25470 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25471 goto mark_cursor_off;
25472 }
25473
25474 /* This can happen when the new row is shorter than the old one.
25475 In this case, either draw_glyphs or clear_end_of_line
25476 should have cleared the cursor. Note that we wouldn't be
25477 able to erase the cursor in this case because we don't have a
25478 cursor glyph at hand. */
25479 if ((cursor_row->reversed_p
25480 ? (w->phys_cursor.hpos < 0)
25481 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25482 goto mark_cursor_off;
25483
25484 /* When the window is hscrolled, cursor hpos can legitimately be out
25485 of bounds, but we draw the cursor at the corresponding window
25486 margin in that case. */
25487 if (!cursor_row->reversed_p && hpos < 0)
25488 hpos = 0;
25489 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25490 hpos = cursor_row->used[TEXT_AREA] - 1;
25491
25492 /* If the cursor is in the mouse face area, redisplay that when
25493 we clear the cursor. */
25494 if (! NILP (hlinfo->mouse_face_window)
25495 && coords_in_mouse_face_p (w, hpos, vpos)
25496 /* Don't redraw the cursor's spot in mouse face if it is at the
25497 end of a line (on a newline). The cursor appears there, but
25498 mouse highlighting does not. */
25499 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25500 mouse_face_here_p = 1;
25501
25502 /* Maybe clear the display under the cursor. */
25503 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25504 {
25505 int x, y, left_x;
25506 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25507 int width;
25508
25509 cursor_glyph = get_phys_cursor_glyph (w);
25510 if (cursor_glyph == NULL)
25511 goto mark_cursor_off;
25512
25513 width = cursor_glyph->pixel_width;
25514 left_x = window_box_left_offset (w, TEXT_AREA);
25515 x = w->phys_cursor.x;
25516 if (x < left_x)
25517 width -= left_x - x;
25518 width = min (width, window_box_width (w, TEXT_AREA) - x);
25519 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25520 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25521
25522 if (width > 0)
25523 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25524 }
25525
25526 /* Erase the cursor by redrawing the character underneath it. */
25527 if (mouse_face_here_p)
25528 hl = DRAW_MOUSE_FACE;
25529 else
25530 hl = DRAW_NORMAL_TEXT;
25531 draw_phys_cursor_glyph (w, cursor_row, hl);
25532
25533 mark_cursor_off:
25534 w->phys_cursor_on_p = 0;
25535 w->phys_cursor_type = NO_CURSOR;
25536 }
25537
25538
25539 /* EXPORT:
25540 Display or clear cursor of window W. If ON is zero, clear the
25541 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25542 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25543
25544 void
25545 display_and_set_cursor (struct window *w, int on,
25546 int hpos, int vpos, int x, int y)
25547 {
25548 struct frame *f = XFRAME (w->frame);
25549 int new_cursor_type;
25550 int new_cursor_width;
25551 int active_cursor;
25552 struct glyph_row *glyph_row;
25553 struct glyph *glyph;
25554
25555 /* This is pointless on invisible frames, and dangerous on garbaged
25556 windows and frames; in the latter case, the frame or window may
25557 be in the midst of changing its size, and x and y may be off the
25558 window. */
25559 if (! FRAME_VISIBLE_P (f)
25560 || FRAME_GARBAGED_P (f)
25561 || vpos >= w->current_matrix->nrows
25562 || hpos >= w->current_matrix->matrix_w)
25563 return;
25564
25565 /* If cursor is off and we want it off, return quickly. */
25566 if (!on && !w->phys_cursor_on_p)
25567 return;
25568
25569 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25570 /* If cursor row is not enabled, we don't really know where to
25571 display the cursor. */
25572 if (!glyph_row->enabled_p)
25573 {
25574 w->phys_cursor_on_p = 0;
25575 return;
25576 }
25577
25578 glyph = NULL;
25579 if (!glyph_row->exact_window_width_line_p
25580 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25581 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25582
25583 xassert (interrupt_input_blocked);
25584
25585 /* Set new_cursor_type to the cursor we want to be displayed. */
25586 new_cursor_type = get_window_cursor_type (w, glyph,
25587 &new_cursor_width, &active_cursor);
25588
25589 /* If cursor is currently being shown and we don't want it to be or
25590 it is in the wrong place, or the cursor type is not what we want,
25591 erase it. */
25592 if (w->phys_cursor_on_p
25593 && (!on
25594 || w->phys_cursor.x != x
25595 || w->phys_cursor.y != y
25596 || new_cursor_type != w->phys_cursor_type
25597 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25598 && new_cursor_width != w->phys_cursor_width)))
25599 erase_phys_cursor (w);
25600
25601 /* Don't check phys_cursor_on_p here because that flag is only set
25602 to zero in some cases where we know that the cursor has been
25603 completely erased, to avoid the extra work of erasing the cursor
25604 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25605 still not be visible, or it has only been partly erased. */
25606 if (on)
25607 {
25608 w->phys_cursor_ascent = glyph_row->ascent;
25609 w->phys_cursor_height = glyph_row->height;
25610
25611 /* Set phys_cursor_.* before x_draw_.* is called because some
25612 of them may need the information. */
25613 w->phys_cursor.x = x;
25614 w->phys_cursor.y = glyph_row->y;
25615 w->phys_cursor.hpos = hpos;
25616 w->phys_cursor.vpos = vpos;
25617 }
25618
25619 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25620 new_cursor_type, new_cursor_width,
25621 on, active_cursor);
25622 }
25623
25624
25625 /* Switch the display of W's cursor on or off, according to the value
25626 of ON. */
25627
25628 static void
25629 update_window_cursor (struct window *w, int on)
25630 {
25631 /* Don't update cursor in windows whose frame is in the process
25632 of being deleted. */
25633 if (w->current_matrix)
25634 {
25635 int hpos = w->phys_cursor.hpos;
25636 int vpos = w->phys_cursor.vpos;
25637 struct glyph_row *row;
25638
25639 if (vpos >= w->current_matrix->nrows
25640 || hpos >= w->current_matrix->matrix_w)
25641 return;
25642
25643 row = MATRIX_ROW (w->current_matrix, vpos);
25644
25645 /* When the window is hscrolled, cursor hpos can legitimately be
25646 out of bounds, but we draw the cursor at the corresponding
25647 window margin in that case. */
25648 if (!row->reversed_p && hpos < 0)
25649 hpos = 0;
25650 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25651 hpos = row->used[TEXT_AREA] - 1;
25652
25653 BLOCK_INPUT;
25654 display_and_set_cursor (w, on, hpos, vpos,
25655 w->phys_cursor.x, w->phys_cursor.y);
25656 UNBLOCK_INPUT;
25657 }
25658 }
25659
25660
25661 /* Call update_window_cursor with parameter ON_P on all leaf windows
25662 in the window tree rooted at W. */
25663
25664 static void
25665 update_cursor_in_window_tree (struct window *w, int on_p)
25666 {
25667 while (w)
25668 {
25669 if (!NILP (w->hchild))
25670 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25671 else if (!NILP (w->vchild))
25672 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25673 else
25674 update_window_cursor (w, on_p);
25675
25676 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25677 }
25678 }
25679
25680
25681 /* EXPORT:
25682 Display the cursor on window W, or clear it, according to ON_P.
25683 Don't change the cursor's position. */
25684
25685 void
25686 x_update_cursor (struct frame *f, int on_p)
25687 {
25688 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25689 }
25690
25691
25692 /* EXPORT:
25693 Clear the cursor of window W to background color, and mark the
25694 cursor as not shown. This is used when the text where the cursor
25695 is about to be rewritten. */
25696
25697 void
25698 x_clear_cursor (struct window *w)
25699 {
25700 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25701 update_window_cursor (w, 0);
25702 }
25703
25704 #endif /* HAVE_WINDOW_SYSTEM */
25705
25706 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25707 and MSDOS. */
25708 static void
25709 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25710 int start_hpos, int end_hpos,
25711 enum draw_glyphs_face draw)
25712 {
25713 #ifdef HAVE_WINDOW_SYSTEM
25714 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25715 {
25716 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25717 return;
25718 }
25719 #endif
25720 #if defined (HAVE_GPM) || defined (MSDOS)
25721 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25722 #endif
25723 }
25724
25725 /* Display the active region described by mouse_face_* according to DRAW. */
25726
25727 static void
25728 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25729 {
25730 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25731 struct frame *f = XFRAME (WINDOW_FRAME (w));
25732
25733 if (/* If window is in the process of being destroyed, don't bother
25734 to do anything. */
25735 w->current_matrix != NULL
25736 /* Don't update mouse highlight if hidden */
25737 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25738 /* Recognize when we are called to operate on rows that don't exist
25739 anymore. This can happen when a window is split. */
25740 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25741 {
25742 int phys_cursor_on_p = w->phys_cursor_on_p;
25743 struct glyph_row *row, *first, *last;
25744
25745 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25746 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25747
25748 for (row = first; row <= last && row->enabled_p; ++row)
25749 {
25750 int start_hpos, end_hpos, start_x;
25751
25752 /* For all but the first row, the highlight starts at column 0. */
25753 if (row == first)
25754 {
25755 /* R2L rows have BEG and END in reversed order, but the
25756 screen drawing geometry is always left to right. So
25757 we need to mirror the beginning and end of the
25758 highlighted area in R2L rows. */
25759 if (!row->reversed_p)
25760 {
25761 start_hpos = hlinfo->mouse_face_beg_col;
25762 start_x = hlinfo->mouse_face_beg_x;
25763 }
25764 else if (row == last)
25765 {
25766 start_hpos = hlinfo->mouse_face_end_col;
25767 start_x = hlinfo->mouse_face_end_x;
25768 }
25769 else
25770 {
25771 start_hpos = 0;
25772 start_x = 0;
25773 }
25774 }
25775 else if (row->reversed_p && row == last)
25776 {
25777 start_hpos = hlinfo->mouse_face_end_col;
25778 start_x = hlinfo->mouse_face_end_x;
25779 }
25780 else
25781 {
25782 start_hpos = 0;
25783 start_x = 0;
25784 }
25785
25786 if (row == last)
25787 {
25788 if (!row->reversed_p)
25789 end_hpos = hlinfo->mouse_face_end_col;
25790 else if (row == first)
25791 end_hpos = hlinfo->mouse_face_beg_col;
25792 else
25793 {
25794 end_hpos = row->used[TEXT_AREA];
25795 if (draw == DRAW_NORMAL_TEXT)
25796 row->fill_line_p = 1; /* Clear to end of line */
25797 }
25798 }
25799 else if (row->reversed_p && row == first)
25800 end_hpos = hlinfo->mouse_face_beg_col;
25801 else
25802 {
25803 end_hpos = row->used[TEXT_AREA];
25804 if (draw == DRAW_NORMAL_TEXT)
25805 row->fill_line_p = 1; /* Clear to end of line */
25806 }
25807
25808 if (end_hpos > start_hpos)
25809 {
25810 draw_row_with_mouse_face (w, start_x, row,
25811 start_hpos, end_hpos, draw);
25812
25813 row->mouse_face_p
25814 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25815 }
25816 }
25817
25818 #ifdef HAVE_WINDOW_SYSTEM
25819 /* When we've written over the cursor, arrange for it to
25820 be displayed again. */
25821 if (FRAME_WINDOW_P (f)
25822 && phys_cursor_on_p && !w->phys_cursor_on_p)
25823 {
25824 int hpos = w->phys_cursor.hpos;
25825
25826 /* When the window is hscrolled, cursor hpos can legitimately be
25827 out of bounds, but we draw the cursor at the corresponding
25828 window margin in that case. */
25829 if (!row->reversed_p && hpos < 0)
25830 hpos = 0;
25831 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25832 hpos = row->used[TEXT_AREA] - 1;
25833
25834 BLOCK_INPUT;
25835 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
25836 w->phys_cursor.x, w->phys_cursor.y);
25837 UNBLOCK_INPUT;
25838 }
25839 #endif /* HAVE_WINDOW_SYSTEM */
25840 }
25841
25842 #ifdef HAVE_WINDOW_SYSTEM
25843 /* Change the mouse cursor. */
25844 if (FRAME_WINDOW_P (f))
25845 {
25846 if (draw == DRAW_NORMAL_TEXT
25847 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25848 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25849 else if (draw == DRAW_MOUSE_FACE)
25850 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25851 else
25852 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25853 }
25854 #endif /* HAVE_WINDOW_SYSTEM */
25855 }
25856
25857 /* EXPORT:
25858 Clear out the mouse-highlighted active region.
25859 Redraw it un-highlighted first. Value is non-zero if mouse
25860 face was actually drawn unhighlighted. */
25861
25862 int
25863 clear_mouse_face (Mouse_HLInfo *hlinfo)
25864 {
25865 int cleared = 0;
25866
25867 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
25868 {
25869 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
25870 cleared = 1;
25871 }
25872
25873 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25874 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25875 hlinfo->mouse_face_window = Qnil;
25876 hlinfo->mouse_face_overlay = Qnil;
25877 return cleared;
25878 }
25879
25880 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
25881 within the mouse face on that window. */
25882 static int
25883 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
25884 {
25885 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25886
25887 /* Quickly resolve the easy cases. */
25888 if (!(WINDOWP (hlinfo->mouse_face_window)
25889 && XWINDOW (hlinfo->mouse_face_window) == w))
25890 return 0;
25891 if (vpos < hlinfo->mouse_face_beg_row
25892 || vpos > hlinfo->mouse_face_end_row)
25893 return 0;
25894 if (vpos > hlinfo->mouse_face_beg_row
25895 && vpos < hlinfo->mouse_face_end_row)
25896 return 1;
25897
25898 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
25899 {
25900 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25901 {
25902 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
25903 return 1;
25904 }
25905 else if ((vpos == hlinfo->mouse_face_beg_row
25906 && hpos >= hlinfo->mouse_face_beg_col)
25907 || (vpos == hlinfo->mouse_face_end_row
25908 && hpos < hlinfo->mouse_face_end_col))
25909 return 1;
25910 }
25911 else
25912 {
25913 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25914 {
25915 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
25916 return 1;
25917 }
25918 else if ((vpos == hlinfo->mouse_face_beg_row
25919 && hpos <= hlinfo->mouse_face_beg_col)
25920 || (vpos == hlinfo->mouse_face_end_row
25921 && hpos > hlinfo->mouse_face_end_col))
25922 return 1;
25923 }
25924 return 0;
25925 }
25926
25927
25928 /* EXPORT:
25929 Non-zero if physical cursor of window W is within mouse face. */
25930
25931 int
25932 cursor_in_mouse_face_p (struct window *w)
25933 {
25934 int hpos = w->phys_cursor.hpos;
25935 int vpos = w->phys_cursor.vpos;
25936 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
25937
25938 /* When the window is hscrolled, cursor hpos can legitimately be out
25939 of bounds, but we draw the cursor at the corresponding window
25940 margin in that case. */
25941 if (!row->reversed_p && hpos < 0)
25942 hpos = 0;
25943 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25944 hpos = row->used[TEXT_AREA] - 1;
25945
25946 return coords_in_mouse_face_p (w, hpos, vpos);
25947 }
25948
25949
25950 \f
25951 /* Find the glyph rows START_ROW and END_ROW of window W that display
25952 characters between buffer positions START_CHARPOS and END_CHARPOS
25953 (excluding END_CHARPOS). DISP_STRING is a display string that
25954 covers these buffer positions. This is similar to
25955 row_containing_pos, but is more accurate when bidi reordering makes
25956 buffer positions change non-linearly with glyph rows. */
25957 static void
25958 rows_from_pos_range (struct window *w,
25959 EMACS_INT start_charpos, EMACS_INT end_charpos,
25960 Lisp_Object disp_string,
25961 struct glyph_row **start, struct glyph_row **end)
25962 {
25963 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25964 int last_y = window_text_bottom_y (w);
25965 struct glyph_row *row;
25966
25967 *start = NULL;
25968 *end = NULL;
25969
25970 while (!first->enabled_p
25971 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
25972 first++;
25973
25974 /* Find the START row. */
25975 for (row = first;
25976 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
25977 row++)
25978 {
25979 /* A row can potentially be the START row if the range of the
25980 characters it displays intersects the range
25981 [START_CHARPOS..END_CHARPOS). */
25982 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
25983 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
25984 /* See the commentary in row_containing_pos, for the
25985 explanation of the complicated way to check whether
25986 some position is beyond the end of the characters
25987 displayed by a row. */
25988 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
25989 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
25990 && !row->ends_at_zv_p
25991 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
25992 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
25993 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
25994 && !row->ends_at_zv_p
25995 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
25996 {
25997 /* Found a candidate row. Now make sure at least one of the
25998 glyphs it displays has a charpos from the range
25999 [START_CHARPOS..END_CHARPOS).
26000
26001 This is not obvious because bidi reordering could make
26002 buffer positions of a row be 1,2,3,102,101,100, and if we
26003 want to highlight characters in [50..60), we don't want
26004 this row, even though [50..60) does intersect [1..103),
26005 the range of character positions given by the row's start
26006 and end positions. */
26007 struct glyph *g = row->glyphs[TEXT_AREA];
26008 struct glyph *e = g + row->used[TEXT_AREA];
26009
26010 while (g < e)
26011 {
26012 if (((BUFFERP (g->object) || INTEGERP (g->object))
26013 && start_charpos <= g->charpos && g->charpos < end_charpos)
26014 /* A glyph that comes from DISP_STRING is by
26015 definition to be highlighted. */
26016 || EQ (g->object, disp_string))
26017 *start = row;
26018 g++;
26019 }
26020 if (*start)
26021 break;
26022 }
26023 }
26024
26025 /* Find the END row. */
26026 if (!*start
26027 /* If the last row is partially visible, start looking for END
26028 from that row, instead of starting from FIRST. */
26029 && !(row->enabled_p
26030 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26031 row = first;
26032 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26033 {
26034 struct glyph_row *next = row + 1;
26035 EMACS_INT next_start = MATRIX_ROW_START_CHARPOS (next);
26036
26037 if (!next->enabled_p
26038 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26039 /* The first row >= START whose range of displayed characters
26040 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26041 is the row END + 1. */
26042 || (start_charpos < next_start
26043 && end_charpos < next_start)
26044 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26045 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26046 && !next->ends_at_zv_p
26047 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26048 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26049 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26050 && !next->ends_at_zv_p
26051 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26052 {
26053 *end = row;
26054 break;
26055 }
26056 else
26057 {
26058 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26059 but none of the characters it displays are in the range, it is
26060 also END + 1. */
26061 struct glyph *g = next->glyphs[TEXT_AREA];
26062 struct glyph *s = g;
26063 struct glyph *e = g + next->used[TEXT_AREA];
26064
26065 while (g < e)
26066 {
26067 if (((BUFFERP (g->object) || INTEGERP (g->object))
26068 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26069 /* If the buffer position of the first glyph in
26070 the row is equal to END_CHARPOS, it means
26071 the last character to be highlighted is the
26072 newline of ROW, and we must consider NEXT as
26073 END, not END+1. */
26074 || (((!next->reversed_p && g == s)
26075 || (next->reversed_p && g == e - 1))
26076 && (g->charpos == end_charpos
26077 /* Special case for when NEXT is an
26078 empty line at ZV. */
26079 || (g->charpos == -1
26080 && !row->ends_at_zv_p
26081 && next_start == end_charpos)))))
26082 /* A glyph that comes from DISP_STRING is by
26083 definition to be highlighted. */
26084 || EQ (g->object, disp_string))
26085 break;
26086 g++;
26087 }
26088 if (g == e)
26089 {
26090 *end = row;
26091 break;
26092 }
26093 /* The first row that ends at ZV must be the last to be
26094 highlighted. */
26095 else if (next->ends_at_zv_p)
26096 {
26097 *end = next;
26098 break;
26099 }
26100 }
26101 }
26102 }
26103
26104 /* This function sets the mouse_face_* elements of HLINFO, assuming
26105 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26106 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26107 for the overlay or run of text properties specifying the mouse
26108 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26109 before-string and after-string that must also be highlighted.
26110 DISP_STRING, if non-nil, is a display string that may cover some
26111 or all of the highlighted text. */
26112
26113 static void
26114 mouse_face_from_buffer_pos (Lisp_Object window,
26115 Mouse_HLInfo *hlinfo,
26116 EMACS_INT mouse_charpos,
26117 EMACS_INT start_charpos,
26118 EMACS_INT end_charpos,
26119 Lisp_Object before_string,
26120 Lisp_Object after_string,
26121 Lisp_Object disp_string)
26122 {
26123 struct window *w = XWINDOW (window);
26124 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26125 struct glyph_row *r1, *r2;
26126 struct glyph *glyph, *end;
26127 EMACS_INT ignore, pos;
26128 int x;
26129
26130 xassert (NILP (disp_string) || STRINGP (disp_string));
26131 xassert (NILP (before_string) || STRINGP (before_string));
26132 xassert (NILP (after_string) || STRINGP (after_string));
26133
26134 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26135 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26136 if (r1 == NULL)
26137 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26138 /* If the before-string or display-string contains newlines,
26139 rows_from_pos_range skips to its last row. Move back. */
26140 if (!NILP (before_string) || !NILP (disp_string))
26141 {
26142 struct glyph_row *prev;
26143 while ((prev = r1 - 1, prev >= first)
26144 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26145 && prev->used[TEXT_AREA] > 0)
26146 {
26147 struct glyph *beg = prev->glyphs[TEXT_AREA];
26148 glyph = beg + prev->used[TEXT_AREA];
26149 while (--glyph >= beg && INTEGERP (glyph->object));
26150 if (glyph < beg
26151 || !(EQ (glyph->object, before_string)
26152 || EQ (glyph->object, disp_string)))
26153 break;
26154 r1 = prev;
26155 }
26156 }
26157 if (r2 == NULL)
26158 {
26159 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26160 hlinfo->mouse_face_past_end = 1;
26161 }
26162 else if (!NILP (after_string))
26163 {
26164 /* If the after-string has newlines, advance to its last row. */
26165 struct glyph_row *next;
26166 struct glyph_row *last
26167 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26168
26169 for (next = r2 + 1;
26170 next <= last
26171 && next->used[TEXT_AREA] > 0
26172 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26173 ++next)
26174 r2 = next;
26175 }
26176 /* The rest of the display engine assumes that mouse_face_beg_row is
26177 either above mouse_face_end_row or identical to it. But with
26178 bidi-reordered continued lines, the row for START_CHARPOS could
26179 be below the row for END_CHARPOS. If so, swap the rows and store
26180 them in correct order. */
26181 if (r1->y > r2->y)
26182 {
26183 struct glyph_row *tem = r2;
26184
26185 r2 = r1;
26186 r1 = tem;
26187 }
26188
26189 hlinfo->mouse_face_beg_y = r1->y;
26190 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26191 hlinfo->mouse_face_end_y = r2->y;
26192 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26193
26194 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26195 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26196 could be anywhere in the row and in any order. The strategy
26197 below is to find the leftmost and the rightmost glyph that
26198 belongs to either of these 3 strings, or whose position is
26199 between START_CHARPOS and END_CHARPOS, and highlight all the
26200 glyphs between those two. This may cover more than just the text
26201 between START_CHARPOS and END_CHARPOS if the range of characters
26202 strides the bidi level boundary, e.g. if the beginning is in R2L
26203 text while the end is in L2R text or vice versa. */
26204 if (!r1->reversed_p)
26205 {
26206 /* This row is in a left to right paragraph. Scan it left to
26207 right. */
26208 glyph = r1->glyphs[TEXT_AREA];
26209 end = glyph + r1->used[TEXT_AREA];
26210 x = r1->x;
26211
26212 /* Skip truncation glyphs at the start of the glyph row. */
26213 if (r1->displays_text_p)
26214 for (; glyph < end
26215 && INTEGERP (glyph->object)
26216 && glyph->charpos < 0;
26217 ++glyph)
26218 x += glyph->pixel_width;
26219
26220 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26221 or DISP_STRING, and the first glyph from buffer whose
26222 position is between START_CHARPOS and END_CHARPOS. */
26223 for (; glyph < end
26224 && !INTEGERP (glyph->object)
26225 && !EQ (glyph->object, disp_string)
26226 && !(BUFFERP (glyph->object)
26227 && (glyph->charpos >= start_charpos
26228 && glyph->charpos < end_charpos));
26229 ++glyph)
26230 {
26231 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26232 are present at buffer positions between START_CHARPOS and
26233 END_CHARPOS, or if they come from an overlay. */
26234 if (EQ (glyph->object, before_string))
26235 {
26236 pos = string_buffer_position (before_string,
26237 start_charpos);
26238 /* If pos == 0, it means before_string came from an
26239 overlay, not from a buffer position. */
26240 if (!pos || (pos >= start_charpos && pos < end_charpos))
26241 break;
26242 }
26243 else if (EQ (glyph->object, after_string))
26244 {
26245 pos = string_buffer_position (after_string, end_charpos);
26246 if (!pos || (pos >= start_charpos && pos < end_charpos))
26247 break;
26248 }
26249 x += glyph->pixel_width;
26250 }
26251 hlinfo->mouse_face_beg_x = x;
26252 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26253 }
26254 else
26255 {
26256 /* This row is in a right to left paragraph. Scan it right to
26257 left. */
26258 struct glyph *g;
26259
26260 end = r1->glyphs[TEXT_AREA] - 1;
26261 glyph = end + r1->used[TEXT_AREA];
26262
26263 /* Skip truncation glyphs at the start of the glyph row. */
26264 if (r1->displays_text_p)
26265 for (; glyph > end
26266 && INTEGERP (glyph->object)
26267 && glyph->charpos < 0;
26268 --glyph)
26269 ;
26270
26271 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26272 or DISP_STRING, and the first glyph from buffer whose
26273 position is between START_CHARPOS and END_CHARPOS. */
26274 for (; glyph > end
26275 && !INTEGERP (glyph->object)
26276 && !EQ (glyph->object, disp_string)
26277 && !(BUFFERP (glyph->object)
26278 && (glyph->charpos >= start_charpos
26279 && glyph->charpos < end_charpos));
26280 --glyph)
26281 {
26282 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26283 are present at buffer positions between START_CHARPOS and
26284 END_CHARPOS, or if they come from an overlay. */
26285 if (EQ (glyph->object, before_string))
26286 {
26287 pos = string_buffer_position (before_string, start_charpos);
26288 /* If pos == 0, it means before_string came from an
26289 overlay, not from a buffer position. */
26290 if (!pos || (pos >= start_charpos && pos < end_charpos))
26291 break;
26292 }
26293 else if (EQ (glyph->object, after_string))
26294 {
26295 pos = string_buffer_position (after_string, end_charpos);
26296 if (!pos || (pos >= start_charpos && pos < end_charpos))
26297 break;
26298 }
26299 }
26300
26301 glyph++; /* first glyph to the right of the highlighted area */
26302 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26303 x += g->pixel_width;
26304 hlinfo->mouse_face_beg_x = x;
26305 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26306 }
26307
26308 /* If the highlight ends in a different row, compute GLYPH and END
26309 for the end row. Otherwise, reuse the values computed above for
26310 the row where the highlight begins. */
26311 if (r2 != r1)
26312 {
26313 if (!r2->reversed_p)
26314 {
26315 glyph = r2->glyphs[TEXT_AREA];
26316 end = glyph + r2->used[TEXT_AREA];
26317 x = r2->x;
26318 }
26319 else
26320 {
26321 end = r2->glyphs[TEXT_AREA] - 1;
26322 glyph = end + r2->used[TEXT_AREA];
26323 }
26324 }
26325
26326 if (!r2->reversed_p)
26327 {
26328 /* Skip truncation and continuation glyphs near the end of the
26329 row, and also blanks and stretch glyphs inserted by
26330 extend_face_to_end_of_line. */
26331 while (end > glyph
26332 && INTEGERP ((end - 1)->object))
26333 --end;
26334 /* Scan the rest of the glyph row from the end, looking for the
26335 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26336 DISP_STRING, or whose position is between START_CHARPOS
26337 and END_CHARPOS */
26338 for (--end;
26339 end > glyph
26340 && !INTEGERP (end->object)
26341 && !EQ (end->object, disp_string)
26342 && !(BUFFERP (end->object)
26343 && (end->charpos >= start_charpos
26344 && end->charpos < end_charpos));
26345 --end)
26346 {
26347 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26348 are present at buffer positions between START_CHARPOS and
26349 END_CHARPOS, or if they come from an overlay. */
26350 if (EQ (end->object, before_string))
26351 {
26352 pos = string_buffer_position (before_string, start_charpos);
26353 if (!pos || (pos >= start_charpos && pos < end_charpos))
26354 break;
26355 }
26356 else if (EQ (end->object, after_string))
26357 {
26358 pos = string_buffer_position (after_string, end_charpos);
26359 if (!pos || (pos >= start_charpos && pos < end_charpos))
26360 break;
26361 }
26362 }
26363 /* Find the X coordinate of the last glyph to be highlighted. */
26364 for (; glyph <= end; ++glyph)
26365 x += glyph->pixel_width;
26366
26367 hlinfo->mouse_face_end_x = x;
26368 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26369 }
26370 else
26371 {
26372 /* Skip truncation and continuation glyphs near the end of the
26373 row, and also blanks and stretch glyphs inserted by
26374 extend_face_to_end_of_line. */
26375 x = r2->x;
26376 end++;
26377 while (end < glyph
26378 && INTEGERP (end->object))
26379 {
26380 x += end->pixel_width;
26381 ++end;
26382 }
26383 /* Scan the rest of the glyph row from the end, looking for the
26384 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26385 DISP_STRING, or whose position is between START_CHARPOS
26386 and END_CHARPOS */
26387 for ( ;
26388 end < glyph
26389 && !INTEGERP (end->object)
26390 && !EQ (end->object, disp_string)
26391 && !(BUFFERP (end->object)
26392 && (end->charpos >= start_charpos
26393 && end->charpos < end_charpos));
26394 ++end)
26395 {
26396 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26397 are present at buffer positions between START_CHARPOS and
26398 END_CHARPOS, or if they come from an overlay. */
26399 if (EQ (end->object, before_string))
26400 {
26401 pos = string_buffer_position (before_string, start_charpos);
26402 if (!pos || (pos >= start_charpos && pos < end_charpos))
26403 break;
26404 }
26405 else if (EQ (end->object, after_string))
26406 {
26407 pos = string_buffer_position (after_string, end_charpos);
26408 if (!pos || (pos >= start_charpos && pos < end_charpos))
26409 break;
26410 }
26411 x += end->pixel_width;
26412 }
26413 /* If we exited the above loop because we arrived at the last
26414 glyph of the row, and its buffer position is still not in
26415 range, it means the last character in range is the preceding
26416 newline. Bump the end column and x values to get past the
26417 last glyph. */
26418 if (end == glyph
26419 && BUFFERP (end->object)
26420 && (end->charpos < start_charpos
26421 || end->charpos >= end_charpos))
26422 {
26423 x += end->pixel_width;
26424 ++end;
26425 }
26426 hlinfo->mouse_face_end_x = x;
26427 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26428 }
26429
26430 hlinfo->mouse_face_window = window;
26431 hlinfo->mouse_face_face_id
26432 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26433 mouse_charpos + 1,
26434 !hlinfo->mouse_face_hidden, -1);
26435 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26436 }
26437
26438 /* The following function is not used anymore (replaced with
26439 mouse_face_from_string_pos), but I leave it here for the time
26440 being, in case someone would. */
26441
26442 #if 0 /* not used */
26443
26444 /* Find the position of the glyph for position POS in OBJECT in
26445 window W's current matrix, and return in *X, *Y the pixel
26446 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26447
26448 RIGHT_P non-zero means return the position of the right edge of the
26449 glyph, RIGHT_P zero means return the left edge position.
26450
26451 If no glyph for POS exists in the matrix, return the position of
26452 the glyph with the next smaller position that is in the matrix, if
26453 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26454 exists in the matrix, return the position of the glyph with the
26455 next larger position in OBJECT.
26456
26457 Value is non-zero if a glyph was found. */
26458
26459 static int
26460 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
26461 int *hpos, int *vpos, int *x, int *y, int right_p)
26462 {
26463 int yb = window_text_bottom_y (w);
26464 struct glyph_row *r;
26465 struct glyph *best_glyph = NULL;
26466 struct glyph_row *best_row = NULL;
26467 int best_x = 0;
26468
26469 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26470 r->enabled_p && r->y < yb;
26471 ++r)
26472 {
26473 struct glyph *g = r->glyphs[TEXT_AREA];
26474 struct glyph *e = g + r->used[TEXT_AREA];
26475 int gx;
26476
26477 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26478 if (EQ (g->object, object))
26479 {
26480 if (g->charpos == pos)
26481 {
26482 best_glyph = g;
26483 best_x = gx;
26484 best_row = r;
26485 goto found;
26486 }
26487 else if (best_glyph == NULL
26488 || ((eabs (g->charpos - pos)
26489 < eabs (best_glyph->charpos - pos))
26490 && (right_p
26491 ? g->charpos < pos
26492 : g->charpos > pos)))
26493 {
26494 best_glyph = g;
26495 best_x = gx;
26496 best_row = r;
26497 }
26498 }
26499 }
26500
26501 found:
26502
26503 if (best_glyph)
26504 {
26505 *x = best_x;
26506 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26507
26508 if (right_p)
26509 {
26510 *x += best_glyph->pixel_width;
26511 ++*hpos;
26512 }
26513
26514 *y = best_row->y;
26515 *vpos = best_row - w->current_matrix->rows;
26516 }
26517
26518 return best_glyph != NULL;
26519 }
26520 #endif /* not used */
26521
26522 /* Find the positions of the first and the last glyphs in window W's
26523 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26524 (assumed to be a string), and return in HLINFO's mouse_face_*
26525 members the pixel and column/row coordinates of those glyphs. */
26526
26527 static void
26528 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26529 Lisp_Object object,
26530 EMACS_INT startpos, EMACS_INT endpos)
26531 {
26532 int yb = window_text_bottom_y (w);
26533 struct glyph_row *r;
26534 struct glyph *g, *e;
26535 int gx;
26536 int found = 0;
26537
26538 /* Find the glyph row with at least one position in the range
26539 [STARTPOS..ENDPOS], and the first glyph in that row whose
26540 position belongs to that range. */
26541 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26542 r->enabled_p && r->y < yb;
26543 ++r)
26544 {
26545 if (!r->reversed_p)
26546 {
26547 g = r->glyphs[TEXT_AREA];
26548 e = g + r->used[TEXT_AREA];
26549 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26550 if (EQ (g->object, object)
26551 && startpos <= g->charpos && g->charpos <= endpos)
26552 {
26553 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26554 hlinfo->mouse_face_beg_y = r->y;
26555 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26556 hlinfo->mouse_face_beg_x = gx;
26557 found = 1;
26558 break;
26559 }
26560 }
26561 else
26562 {
26563 struct glyph *g1;
26564
26565 e = r->glyphs[TEXT_AREA];
26566 g = e + r->used[TEXT_AREA];
26567 for ( ; g > e; --g)
26568 if (EQ ((g-1)->object, object)
26569 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26570 {
26571 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26572 hlinfo->mouse_face_beg_y = r->y;
26573 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26574 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26575 gx += g1->pixel_width;
26576 hlinfo->mouse_face_beg_x = gx;
26577 found = 1;
26578 break;
26579 }
26580 }
26581 if (found)
26582 break;
26583 }
26584
26585 if (!found)
26586 return;
26587
26588 /* Starting with the next row, look for the first row which does NOT
26589 include any glyphs whose positions are in the range. */
26590 for (++r; r->enabled_p && r->y < yb; ++r)
26591 {
26592 g = r->glyphs[TEXT_AREA];
26593 e = g + r->used[TEXT_AREA];
26594 found = 0;
26595 for ( ; g < e; ++g)
26596 if (EQ (g->object, object)
26597 && startpos <= g->charpos && g->charpos <= endpos)
26598 {
26599 found = 1;
26600 break;
26601 }
26602 if (!found)
26603 break;
26604 }
26605
26606 /* The highlighted region ends on the previous row. */
26607 r--;
26608
26609 /* Set the end row and its vertical pixel coordinate. */
26610 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26611 hlinfo->mouse_face_end_y = r->y;
26612
26613 /* Compute and set the end column and the end column's horizontal
26614 pixel coordinate. */
26615 if (!r->reversed_p)
26616 {
26617 g = r->glyphs[TEXT_AREA];
26618 e = g + r->used[TEXT_AREA];
26619 for ( ; e > g; --e)
26620 if (EQ ((e-1)->object, object)
26621 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26622 break;
26623 hlinfo->mouse_face_end_col = e - g;
26624
26625 for (gx = r->x; g < e; ++g)
26626 gx += g->pixel_width;
26627 hlinfo->mouse_face_end_x = gx;
26628 }
26629 else
26630 {
26631 e = r->glyphs[TEXT_AREA];
26632 g = e + r->used[TEXT_AREA];
26633 for (gx = r->x ; e < g; ++e)
26634 {
26635 if (EQ (e->object, object)
26636 && startpos <= e->charpos && e->charpos <= endpos)
26637 break;
26638 gx += e->pixel_width;
26639 }
26640 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26641 hlinfo->mouse_face_end_x = gx;
26642 }
26643 }
26644
26645 #ifdef HAVE_WINDOW_SYSTEM
26646
26647 /* See if position X, Y is within a hot-spot of an image. */
26648
26649 static int
26650 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26651 {
26652 if (!CONSP (hot_spot))
26653 return 0;
26654
26655 if (EQ (XCAR (hot_spot), Qrect))
26656 {
26657 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26658 Lisp_Object rect = XCDR (hot_spot);
26659 Lisp_Object tem;
26660 if (!CONSP (rect))
26661 return 0;
26662 if (!CONSP (XCAR (rect)))
26663 return 0;
26664 if (!CONSP (XCDR (rect)))
26665 return 0;
26666 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26667 return 0;
26668 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26669 return 0;
26670 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26671 return 0;
26672 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26673 return 0;
26674 return 1;
26675 }
26676 else if (EQ (XCAR (hot_spot), Qcircle))
26677 {
26678 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26679 Lisp_Object circ = XCDR (hot_spot);
26680 Lisp_Object lr, lx0, ly0;
26681 if (CONSP (circ)
26682 && CONSP (XCAR (circ))
26683 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26684 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26685 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26686 {
26687 double r = XFLOATINT (lr);
26688 double dx = XINT (lx0) - x;
26689 double dy = XINT (ly0) - y;
26690 return (dx * dx + dy * dy <= r * r);
26691 }
26692 }
26693 else if (EQ (XCAR (hot_spot), Qpoly))
26694 {
26695 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26696 if (VECTORP (XCDR (hot_spot)))
26697 {
26698 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26699 Lisp_Object *poly = v->contents;
26700 int n = v->header.size;
26701 int i;
26702 int inside = 0;
26703 Lisp_Object lx, ly;
26704 int x0, y0;
26705
26706 /* Need an even number of coordinates, and at least 3 edges. */
26707 if (n < 6 || n & 1)
26708 return 0;
26709
26710 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26711 If count is odd, we are inside polygon. Pixels on edges
26712 may or may not be included depending on actual geometry of the
26713 polygon. */
26714 if ((lx = poly[n-2], !INTEGERP (lx))
26715 || (ly = poly[n-1], !INTEGERP (lx)))
26716 return 0;
26717 x0 = XINT (lx), y0 = XINT (ly);
26718 for (i = 0; i < n; i += 2)
26719 {
26720 int x1 = x0, y1 = y0;
26721 if ((lx = poly[i], !INTEGERP (lx))
26722 || (ly = poly[i+1], !INTEGERP (ly)))
26723 return 0;
26724 x0 = XINT (lx), y0 = XINT (ly);
26725
26726 /* Does this segment cross the X line? */
26727 if (x0 >= x)
26728 {
26729 if (x1 >= x)
26730 continue;
26731 }
26732 else if (x1 < x)
26733 continue;
26734 if (y > y0 && y > y1)
26735 continue;
26736 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26737 inside = !inside;
26738 }
26739 return inside;
26740 }
26741 }
26742 return 0;
26743 }
26744
26745 Lisp_Object
26746 find_hot_spot (Lisp_Object map, int x, int y)
26747 {
26748 while (CONSP (map))
26749 {
26750 if (CONSP (XCAR (map))
26751 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26752 return XCAR (map);
26753 map = XCDR (map);
26754 }
26755
26756 return Qnil;
26757 }
26758
26759 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26760 3, 3, 0,
26761 doc: /* Lookup in image map MAP coordinates X and Y.
26762 An image map is an alist where each element has the format (AREA ID PLIST).
26763 An AREA is specified as either a rectangle, a circle, or a polygon:
26764 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26765 pixel coordinates of the upper left and bottom right corners.
26766 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26767 and the radius of the circle; r may be a float or integer.
26768 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26769 vector describes one corner in the polygon.
26770 Returns the alist element for the first matching AREA in MAP. */)
26771 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26772 {
26773 if (NILP (map))
26774 return Qnil;
26775
26776 CHECK_NUMBER (x);
26777 CHECK_NUMBER (y);
26778
26779 return find_hot_spot (map, XINT (x), XINT (y));
26780 }
26781
26782
26783 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26784 static void
26785 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26786 {
26787 /* Do not change cursor shape while dragging mouse. */
26788 if (!NILP (do_mouse_tracking))
26789 return;
26790
26791 if (!NILP (pointer))
26792 {
26793 if (EQ (pointer, Qarrow))
26794 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26795 else if (EQ (pointer, Qhand))
26796 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
26797 else if (EQ (pointer, Qtext))
26798 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26799 else if (EQ (pointer, intern ("hdrag")))
26800 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26801 #ifdef HAVE_X_WINDOWS
26802 else if (EQ (pointer, intern ("vdrag")))
26803 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
26804 #endif
26805 else if (EQ (pointer, intern ("hourglass")))
26806 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
26807 else if (EQ (pointer, Qmodeline))
26808 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
26809 else
26810 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26811 }
26812
26813 if (cursor != No_Cursor)
26814 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26815 }
26816
26817 #endif /* HAVE_WINDOW_SYSTEM */
26818
26819 /* Take proper action when mouse has moved to the mode or header line
26820 or marginal area AREA of window W, x-position X and y-position Y.
26821 X is relative to the start of the text display area of W, so the
26822 width of bitmap areas and scroll bars must be subtracted to get a
26823 position relative to the start of the mode line. */
26824
26825 static void
26826 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
26827 enum window_part area)
26828 {
26829 struct window *w = XWINDOW (window);
26830 struct frame *f = XFRAME (w->frame);
26831 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26832 #ifdef HAVE_WINDOW_SYSTEM
26833 Display_Info *dpyinfo;
26834 #endif
26835 Cursor cursor = No_Cursor;
26836 Lisp_Object pointer = Qnil;
26837 int dx, dy, width, height;
26838 EMACS_INT charpos;
26839 Lisp_Object string, object = Qnil;
26840 Lisp_Object pos, help;
26841
26842 Lisp_Object mouse_face;
26843 int original_x_pixel = x;
26844 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26845 struct glyph_row *row;
26846
26847 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26848 {
26849 int x0;
26850 struct glyph *end;
26851
26852 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26853 returns them in row/column units! */
26854 string = mode_line_string (w, area, &x, &y, &charpos,
26855 &object, &dx, &dy, &width, &height);
26856
26857 row = (area == ON_MODE_LINE
26858 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
26859 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
26860
26861 /* Find the glyph under the mouse pointer. */
26862 if (row->mode_line_p && row->enabled_p)
26863 {
26864 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
26865 end = glyph + row->used[TEXT_AREA];
26866
26867 for (x0 = original_x_pixel;
26868 glyph < end && x0 >= glyph->pixel_width;
26869 ++glyph)
26870 x0 -= glyph->pixel_width;
26871
26872 if (glyph >= end)
26873 glyph = NULL;
26874 }
26875 }
26876 else
26877 {
26878 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
26879 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
26880 returns them in row/column units! */
26881 string = marginal_area_string (w, area, &x, &y, &charpos,
26882 &object, &dx, &dy, &width, &height);
26883 }
26884
26885 help = Qnil;
26886
26887 #ifdef HAVE_WINDOW_SYSTEM
26888 if (IMAGEP (object))
26889 {
26890 Lisp_Object image_map, hotspot;
26891 if ((image_map = Fplist_get (XCDR (object), QCmap),
26892 !NILP (image_map))
26893 && (hotspot = find_hot_spot (image_map, dx, dy),
26894 CONSP (hotspot))
26895 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26896 {
26897 Lisp_Object plist;
26898
26899 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
26900 If so, we could look for mouse-enter, mouse-leave
26901 properties in PLIST (and do something...). */
26902 hotspot = XCDR (hotspot);
26903 if (CONSP (hotspot)
26904 && (plist = XCAR (hotspot), CONSP (plist)))
26905 {
26906 pointer = Fplist_get (plist, Qpointer);
26907 if (NILP (pointer))
26908 pointer = Qhand;
26909 help = Fplist_get (plist, Qhelp_echo);
26910 if (!NILP (help))
26911 {
26912 help_echo_string = help;
26913 /* Is this correct? ++kfs */
26914 XSETWINDOW (help_echo_window, w);
26915 help_echo_object = w->buffer;
26916 help_echo_pos = charpos;
26917 }
26918 }
26919 }
26920 if (NILP (pointer))
26921 pointer = Fplist_get (XCDR (object), QCpointer);
26922 }
26923 #endif /* HAVE_WINDOW_SYSTEM */
26924
26925 if (STRINGP (string))
26926 {
26927 pos = make_number (charpos);
26928 /* If we're on a string with `help-echo' text property, arrange
26929 for the help to be displayed. This is done by setting the
26930 global variable help_echo_string to the help string. */
26931 if (NILP (help))
26932 {
26933 help = Fget_text_property (pos, Qhelp_echo, string);
26934 if (!NILP (help))
26935 {
26936 help_echo_string = help;
26937 XSETWINDOW (help_echo_window, w);
26938 help_echo_object = string;
26939 help_echo_pos = charpos;
26940 }
26941 }
26942
26943 #ifdef HAVE_WINDOW_SYSTEM
26944 if (FRAME_WINDOW_P (f))
26945 {
26946 dpyinfo = FRAME_X_DISPLAY_INFO (f);
26947 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26948 if (NILP (pointer))
26949 pointer = Fget_text_property (pos, Qpointer, string);
26950
26951 /* Change the mouse pointer according to what is under X/Y. */
26952 if (NILP (pointer)
26953 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
26954 {
26955 Lisp_Object map;
26956 map = Fget_text_property (pos, Qlocal_map, string);
26957 if (!KEYMAPP (map))
26958 map = Fget_text_property (pos, Qkeymap, string);
26959 if (!KEYMAPP (map))
26960 cursor = dpyinfo->vertical_scroll_bar_cursor;
26961 }
26962 }
26963 #endif
26964
26965 /* Change the mouse face according to what is under X/Y. */
26966 mouse_face = Fget_text_property (pos, Qmouse_face, string);
26967 if (!NILP (mouse_face)
26968 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26969 && glyph)
26970 {
26971 Lisp_Object b, e;
26972
26973 struct glyph * tmp_glyph;
26974
26975 int gpos;
26976 int gseq_length;
26977 int total_pixel_width;
26978 EMACS_INT begpos, endpos, ignore;
26979
26980 int vpos, hpos;
26981
26982 b = Fprevious_single_property_change (make_number (charpos + 1),
26983 Qmouse_face, string, Qnil);
26984 if (NILP (b))
26985 begpos = 0;
26986 else
26987 begpos = XINT (b);
26988
26989 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
26990 if (NILP (e))
26991 endpos = SCHARS (string);
26992 else
26993 endpos = XINT (e);
26994
26995 /* Calculate the glyph position GPOS of GLYPH in the
26996 displayed string, relative to the beginning of the
26997 highlighted part of the string.
26998
26999 Note: GPOS is different from CHARPOS. CHARPOS is the
27000 position of GLYPH in the internal string object. A mode
27001 line string format has structures which are converted to
27002 a flattened string by the Emacs Lisp interpreter. The
27003 internal string is an element of those structures. The
27004 displayed string is the flattened string. */
27005 tmp_glyph = row_start_glyph;
27006 while (tmp_glyph < glyph
27007 && (!(EQ (tmp_glyph->object, glyph->object)
27008 && begpos <= tmp_glyph->charpos
27009 && tmp_glyph->charpos < endpos)))
27010 tmp_glyph++;
27011 gpos = glyph - tmp_glyph;
27012
27013 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27014 the highlighted part of the displayed string to which
27015 GLYPH belongs. Note: GSEQ_LENGTH is different from
27016 SCHARS (STRING), because the latter returns the length of
27017 the internal string. */
27018 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27019 tmp_glyph > glyph
27020 && (!(EQ (tmp_glyph->object, glyph->object)
27021 && begpos <= tmp_glyph->charpos
27022 && tmp_glyph->charpos < endpos));
27023 tmp_glyph--)
27024 ;
27025 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27026
27027 /* Calculate the total pixel width of all the glyphs between
27028 the beginning of the highlighted area and GLYPH. */
27029 total_pixel_width = 0;
27030 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27031 total_pixel_width += tmp_glyph->pixel_width;
27032
27033 /* Pre calculation of re-rendering position. Note: X is in
27034 column units here, after the call to mode_line_string or
27035 marginal_area_string. */
27036 hpos = x - gpos;
27037 vpos = (area == ON_MODE_LINE
27038 ? (w->current_matrix)->nrows - 1
27039 : 0);
27040
27041 /* If GLYPH's position is included in the region that is
27042 already drawn in mouse face, we have nothing to do. */
27043 if ( EQ (window, hlinfo->mouse_face_window)
27044 && (!row->reversed_p
27045 ? (hlinfo->mouse_face_beg_col <= hpos
27046 && hpos < hlinfo->mouse_face_end_col)
27047 /* In R2L rows we swap BEG and END, see below. */
27048 : (hlinfo->mouse_face_end_col <= hpos
27049 && hpos < hlinfo->mouse_face_beg_col))
27050 && hlinfo->mouse_face_beg_row == vpos )
27051 return;
27052
27053 if (clear_mouse_face (hlinfo))
27054 cursor = No_Cursor;
27055
27056 if (!row->reversed_p)
27057 {
27058 hlinfo->mouse_face_beg_col = hpos;
27059 hlinfo->mouse_face_beg_x = original_x_pixel
27060 - (total_pixel_width + dx);
27061 hlinfo->mouse_face_end_col = hpos + gseq_length;
27062 hlinfo->mouse_face_end_x = 0;
27063 }
27064 else
27065 {
27066 /* In R2L rows, show_mouse_face expects BEG and END
27067 coordinates to be swapped. */
27068 hlinfo->mouse_face_end_col = hpos;
27069 hlinfo->mouse_face_end_x = original_x_pixel
27070 - (total_pixel_width + dx);
27071 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27072 hlinfo->mouse_face_beg_x = 0;
27073 }
27074
27075 hlinfo->mouse_face_beg_row = vpos;
27076 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27077 hlinfo->mouse_face_beg_y = 0;
27078 hlinfo->mouse_face_end_y = 0;
27079 hlinfo->mouse_face_past_end = 0;
27080 hlinfo->mouse_face_window = window;
27081
27082 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27083 charpos,
27084 0, 0, 0,
27085 &ignore,
27086 glyph->face_id,
27087 1);
27088 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27089
27090 if (NILP (pointer))
27091 pointer = Qhand;
27092 }
27093 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27094 clear_mouse_face (hlinfo);
27095 }
27096 #ifdef HAVE_WINDOW_SYSTEM
27097 if (FRAME_WINDOW_P (f))
27098 define_frame_cursor1 (f, cursor, pointer);
27099 #endif
27100 }
27101
27102
27103 /* EXPORT:
27104 Take proper action when the mouse has moved to position X, Y on
27105 frame F as regards highlighting characters that have mouse-face
27106 properties. Also de-highlighting chars where the mouse was before.
27107 X and Y can be negative or out of range. */
27108
27109 void
27110 note_mouse_highlight (struct frame *f, int x, int y)
27111 {
27112 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27113 enum window_part part = ON_NOTHING;
27114 Lisp_Object window;
27115 struct window *w;
27116 Cursor cursor = No_Cursor;
27117 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27118 struct buffer *b;
27119
27120 /* When a menu is active, don't highlight because this looks odd. */
27121 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27122 if (popup_activated ())
27123 return;
27124 #endif
27125
27126 if (NILP (Vmouse_highlight)
27127 || !f->glyphs_initialized_p
27128 || f->pointer_invisible)
27129 return;
27130
27131 hlinfo->mouse_face_mouse_x = x;
27132 hlinfo->mouse_face_mouse_y = y;
27133 hlinfo->mouse_face_mouse_frame = f;
27134
27135 if (hlinfo->mouse_face_defer)
27136 return;
27137
27138 if (gc_in_progress)
27139 {
27140 hlinfo->mouse_face_deferred_gc = 1;
27141 return;
27142 }
27143
27144 /* Which window is that in? */
27145 window = window_from_coordinates (f, x, y, &part, 1);
27146
27147 /* If displaying active text in another window, clear that. */
27148 if (! EQ (window, hlinfo->mouse_face_window)
27149 /* Also clear if we move out of text area in same window. */
27150 || (!NILP (hlinfo->mouse_face_window)
27151 && !NILP (window)
27152 && part != ON_TEXT
27153 && part != ON_MODE_LINE
27154 && part != ON_HEADER_LINE))
27155 clear_mouse_face (hlinfo);
27156
27157 /* Not on a window -> return. */
27158 if (!WINDOWP (window))
27159 return;
27160
27161 /* Reset help_echo_string. It will get recomputed below. */
27162 help_echo_string = Qnil;
27163
27164 /* Convert to window-relative pixel coordinates. */
27165 w = XWINDOW (window);
27166 frame_to_window_pixel_xy (w, &x, &y);
27167
27168 #ifdef HAVE_WINDOW_SYSTEM
27169 /* Handle tool-bar window differently since it doesn't display a
27170 buffer. */
27171 if (EQ (window, f->tool_bar_window))
27172 {
27173 note_tool_bar_highlight (f, x, y);
27174 return;
27175 }
27176 #endif
27177
27178 /* Mouse is on the mode, header line or margin? */
27179 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27180 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27181 {
27182 note_mode_line_or_margin_highlight (window, x, y, part);
27183 return;
27184 }
27185
27186 #ifdef HAVE_WINDOW_SYSTEM
27187 if (part == ON_VERTICAL_BORDER)
27188 {
27189 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27190 help_echo_string = build_string ("drag-mouse-1: resize");
27191 }
27192 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27193 || part == ON_SCROLL_BAR)
27194 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27195 else
27196 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27197 #endif
27198
27199 /* Are we in a window whose display is up to date?
27200 And verify the buffer's text has not changed. */
27201 b = XBUFFER (w->buffer);
27202 if (part == ON_TEXT
27203 && EQ (w->window_end_valid, w->buffer)
27204 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
27205 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
27206 {
27207 int hpos, vpos, dx, dy, area = LAST_AREA;
27208 EMACS_INT pos;
27209 struct glyph *glyph;
27210 Lisp_Object object;
27211 Lisp_Object mouse_face = Qnil, position;
27212 Lisp_Object *overlay_vec = NULL;
27213 ptrdiff_t i, noverlays;
27214 struct buffer *obuf;
27215 EMACS_INT obegv, ozv;
27216 int same_region;
27217
27218 /* Find the glyph under X/Y. */
27219 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27220
27221 #ifdef HAVE_WINDOW_SYSTEM
27222 /* Look for :pointer property on image. */
27223 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27224 {
27225 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27226 if (img != NULL && IMAGEP (img->spec))
27227 {
27228 Lisp_Object image_map, hotspot;
27229 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27230 !NILP (image_map))
27231 && (hotspot = find_hot_spot (image_map,
27232 glyph->slice.img.x + dx,
27233 glyph->slice.img.y + dy),
27234 CONSP (hotspot))
27235 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27236 {
27237 Lisp_Object plist;
27238
27239 /* Could check XCAR (hotspot) to see if we enter/leave
27240 this hot-spot.
27241 If so, we could look for mouse-enter, mouse-leave
27242 properties in PLIST (and do something...). */
27243 hotspot = XCDR (hotspot);
27244 if (CONSP (hotspot)
27245 && (plist = XCAR (hotspot), CONSP (plist)))
27246 {
27247 pointer = Fplist_get (plist, Qpointer);
27248 if (NILP (pointer))
27249 pointer = Qhand;
27250 help_echo_string = Fplist_get (plist, Qhelp_echo);
27251 if (!NILP (help_echo_string))
27252 {
27253 help_echo_window = window;
27254 help_echo_object = glyph->object;
27255 help_echo_pos = glyph->charpos;
27256 }
27257 }
27258 }
27259 if (NILP (pointer))
27260 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27261 }
27262 }
27263 #endif /* HAVE_WINDOW_SYSTEM */
27264
27265 /* Clear mouse face if X/Y not over text. */
27266 if (glyph == NULL
27267 || area != TEXT_AREA
27268 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27269 /* Glyph's OBJECT is an integer for glyphs inserted by the
27270 display engine for its internal purposes, like truncation
27271 and continuation glyphs and blanks beyond the end of
27272 line's text on text terminals. If we are over such a
27273 glyph, we are not over any text. */
27274 || INTEGERP (glyph->object)
27275 /* R2L rows have a stretch glyph at their front, which
27276 stands for no text, whereas L2R rows have no glyphs at
27277 all beyond the end of text. Treat such stretch glyphs
27278 like we do with NULL glyphs in L2R rows. */
27279 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27280 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27281 && glyph->type == STRETCH_GLYPH
27282 && glyph->avoid_cursor_p))
27283 {
27284 if (clear_mouse_face (hlinfo))
27285 cursor = No_Cursor;
27286 #ifdef HAVE_WINDOW_SYSTEM
27287 if (FRAME_WINDOW_P (f) && NILP (pointer))
27288 {
27289 if (area != TEXT_AREA)
27290 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27291 else
27292 pointer = Vvoid_text_area_pointer;
27293 }
27294 #endif
27295 goto set_cursor;
27296 }
27297
27298 pos = glyph->charpos;
27299 object = glyph->object;
27300 if (!STRINGP (object) && !BUFFERP (object))
27301 goto set_cursor;
27302
27303 /* If we get an out-of-range value, return now; avoid an error. */
27304 if (BUFFERP (object) && pos > BUF_Z (b))
27305 goto set_cursor;
27306
27307 /* Make the window's buffer temporarily current for
27308 overlays_at and compute_char_face. */
27309 obuf = current_buffer;
27310 current_buffer = b;
27311 obegv = BEGV;
27312 ozv = ZV;
27313 BEGV = BEG;
27314 ZV = Z;
27315
27316 /* Is this char mouse-active or does it have help-echo? */
27317 position = make_number (pos);
27318
27319 if (BUFFERP (object))
27320 {
27321 /* Put all the overlays we want in a vector in overlay_vec. */
27322 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27323 /* Sort overlays into increasing priority order. */
27324 noverlays = sort_overlays (overlay_vec, noverlays, w);
27325 }
27326 else
27327 noverlays = 0;
27328
27329 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27330
27331 if (same_region)
27332 cursor = No_Cursor;
27333
27334 /* Check mouse-face highlighting. */
27335 if (! same_region
27336 /* If there exists an overlay with mouse-face overlapping
27337 the one we are currently highlighting, we have to
27338 check if we enter the overlapping overlay, and then
27339 highlight only that. */
27340 || (OVERLAYP (hlinfo->mouse_face_overlay)
27341 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27342 {
27343 /* Find the highest priority overlay with a mouse-face. */
27344 Lisp_Object overlay = Qnil;
27345 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27346 {
27347 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27348 if (!NILP (mouse_face))
27349 overlay = overlay_vec[i];
27350 }
27351
27352 /* If we're highlighting the same overlay as before, there's
27353 no need to do that again. */
27354 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27355 goto check_help_echo;
27356 hlinfo->mouse_face_overlay = overlay;
27357
27358 /* Clear the display of the old active region, if any. */
27359 if (clear_mouse_face (hlinfo))
27360 cursor = No_Cursor;
27361
27362 /* If no overlay applies, get a text property. */
27363 if (NILP (overlay))
27364 mouse_face = Fget_text_property (position, Qmouse_face, object);
27365
27366 /* Next, compute the bounds of the mouse highlighting and
27367 display it. */
27368 if (!NILP (mouse_face) && STRINGP (object))
27369 {
27370 /* The mouse-highlighting comes from a display string
27371 with a mouse-face. */
27372 Lisp_Object s, e;
27373 EMACS_INT ignore;
27374
27375 s = Fprevious_single_property_change
27376 (make_number (pos + 1), Qmouse_face, object, Qnil);
27377 e = Fnext_single_property_change
27378 (position, Qmouse_face, object, Qnil);
27379 if (NILP (s))
27380 s = make_number (0);
27381 if (NILP (e))
27382 e = make_number (SCHARS (object) - 1);
27383 mouse_face_from_string_pos (w, hlinfo, object,
27384 XINT (s), XINT (e));
27385 hlinfo->mouse_face_past_end = 0;
27386 hlinfo->mouse_face_window = window;
27387 hlinfo->mouse_face_face_id
27388 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27389 glyph->face_id, 1);
27390 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27391 cursor = No_Cursor;
27392 }
27393 else
27394 {
27395 /* The mouse-highlighting, if any, comes from an overlay
27396 or text property in the buffer. */
27397 Lisp_Object buffer IF_LINT (= Qnil);
27398 Lisp_Object disp_string IF_LINT (= Qnil);
27399
27400 if (STRINGP (object))
27401 {
27402 /* If we are on a display string with no mouse-face,
27403 check if the text under it has one. */
27404 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27405 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27406 pos = string_buffer_position (object, start);
27407 if (pos > 0)
27408 {
27409 mouse_face = get_char_property_and_overlay
27410 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27411 buffer = w->buffer;
27412 disp_string = object;
27413 }
27414 }
27415 else
27416 {
27417 buffer = object;
27418 disp_string = Qnil;
27419 }
27420
27421 if (!NILP (mouse_face))
27422 {
27423 Lisp_Object before, after;
27424 Lisp_Object before_string, after_string;
27425 /* To correctly find the limits of mouse highlight
27426 in a bidi-reordered buffer, we must not use the
27427 optimization of limiting the search in
27428 previous-single-property-change and
27429 next-single-property-change, because
27430 rows_from_pos_range needs the real start and end
27431 positions to DTRT in this case. That's because
27432 the first row visible in a window does not
27433 necessarily display the character whose position
27434 is the smallest. */
27435 Lisp_Object lim1 =
27436 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27437 ? Fmarker_position (w->start)
27438 : Qnil;
27439 Lisp_Object lim2 =
27440 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27441 ? make_number (BUF_Z (XBUFFER (buffer))
27442 - XFASTINT (w->window_end_pos))
27443 : Qnil;
27444
27445 if (NILP (overlay))
27446 {
27447 /* Handle the text property case. */
27448 before = Fprevious_single_property_change
27449 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27450 after = Fnext_single_property_change
27451 (make_number (pos), Qmouse_face, buffer, lim2);
27452 before_string = after_string = Qnil;
27453 }
27454 else
27455 {
27456 /* Handle the overlay case. */
27457 before = Foverlay_start (overlay);
27458 after = Foverlay_end (overlay);
27459 before_string = Foverlay_get (overlay, Qbefore_string);
27460 after_string = Foverlay_get (overlay, Qafter_string);
27461
27462 if (!STRINGP (before_string)) before_string = Qnil;
27463 if (!STRINGP (after_string)) after_string = Qnil;
27464 }
27465
27466 mouse_face_from_buffer_pos (window, hlinfo, pos,
27467 NILP (before)
27468 ? 1
27469 : XFASTINT (before),
27470 NILP (after)
27471 ? BUF_Z (XBUFFER (buffer))
27472 : XFASTINT (after),
27473 before_string, after_string,
27474 disp_string);
27475 cursor = No_Cursor;
27476 }
27477 }
27478 }
27479
27480 check_help_echo:
27481
27482 /* Look for a `help-echo' property. */
27483 if (NILP (help_echo_string)) {
27484 Lisp_Object help, overlay;
27485
27486 /* Check overlays first. */
27487 help = overlay = Qnil;
27488 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27489 {
27490 overlay = overlay_vec[i];
27491 help = Foverlay_get (overlay, Qhelp_echo);
27492 }
27493
27494 if (!NILP (help))
27495 {
27496 help_echo_string = help;
27497 help_echo_window = window;
27498 help_echo_object = overlay;
27499 help_echo_pos = pos;
27500 }
27501 else
27502 {
27503 Lisp_Object obj = glyph->object;
27504 EMACS_INT charpos = glyph->charpos;
27505
27506 /* Try text properties. */
27507 if (STRINGP (obj)
27508 && charpos >= 0
27509 && charpos < SCHARS (obj))
27510 {
27511 help = Fget_text_property (make_number (charpos),
27512 Qhelp_echo, obj);
27513 if (NILP (help))
27514 {
27515 /* If the string itself doesn't specify a help-echo,
27516 see if the buffer text ``under'' it does. */
27517 struct glyph_row *r
27518 = MATRIX_ROW (w->current_matrix, vpos);
27519 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27520 EMACS_INT p = string_buffer_position (obj, start);
27521 if (p > 0)
27522 {
27523 help = Fget_char_property (make_number (p),
27524 Qhelp_echo, w->buffer);
27525 if (!NILP (help))
27526 {
27527 charpos = p;
27528 obj = w->buffer;
27529 }
27530 }
27531 }
27532 }
27533 else if (BUFFERP (obj)
27534 && charpos >= BEGV
27535 && charpos < ZV)
27536 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27537 obj);
27538
27539 if (!NILP (help))
27540 {
27541 help_echo_string = help;
27542 help_echo_window = window;
27543 help_echo_object = obj;
27544 help_echo_pos = charpos;
27545 }
27546 }
27547 }
27548
27549 #ifdef HAVE_WINDOW_SYSTEM
27550 /* Look for a `pointer' property. */
27551 if (FRAME_WINDOW_P (f) && NILP (pointer))
27552 {
27553 /* Check overlays first. */
27554 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27555 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27556
27557 if (NILP (pointer))
27558 {
27559 Lisp_Object obj = glyph->object;
27560 EMACS_INT charpos = glyph->charpos;
27561
27562 /* Try text properties. */
27563 if (STRINGP (obj)
27564 && charpos >= 0
27565 && charpos < SCHARS (obj))
27566 {
27567 pointer = Fget_text_property (make_number (charpos),
27568 Qpointer, obj);
27569 if (NILP (pointer))
27570 {
27571 /* If the string itself doesn't specify a pointer,
27572 see if the buffer text ``under'' it does. */
27573 struct glyph_row *r
27574 = MATRIX_ROW (w->current_matrix, vpos);
27575 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
27576 EMACS_INT p = string_buffer_position (obj, start);
27577 if (p > 0)
27578 pointer = Fget_char_property (make_number (p),
27579 Qpointer, w->buffer);
27580 }
27581 }
27582 else if (BUFFERP (obj)
27583 && charpos >= BEGV
27584 && charpos < ZV)
27585 pointer = Fget_text_property (make_number (charpos),
27586 Qpointer, obj);
27587 }
27588 }
27589 #endif /* HAVE_WINDOW_SYSTEM */
27590
27591 BEGV = obegv;
27592 ZV = ozv;
27593 current_buffer = obuf;
27594 }
27595
27596 set_cursor:
27597
27598 #ifdef HAVE_WINDOW_SYSTEM
27599 if (FRAME_WINDOW_P (f))
27600 define_frame_cursor1 (f, cursor, pointer);
27601 #else
27602 /* This is here to prevent a compiler error, about "label at end of
27603 compound statement". */
27604 return;
27605 #endif
27606 }
27607
27608
27609 /* EXPORT for RIF:
27610 Clear any mouse-face on window W. This function is part of the
27611 redisplay interface, and is called from try_window_id and similar
27612 functions to ensure the mouse-highlight is off. */
27613
27614 void
27615 x_clear_window_mouse_face (struct window *w)
27616 {
27617 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27618 Lisp_Object window;
27619
27620 BLOCK_INPUT;
27621 XSETWINDOW (window, w);
27622 if (EQ (window, hlinfo->mouse_face_window))
27623 clear_mouse_face (hlinfo);
27624 UNBLOCK_INPUT;
27625 }
27626
27627
27628 /* EXPORT:
27629 Just discard the mouse face information for frame F, if any.
27630 This is used when the size of F is changed. */
27631
27632 void
27633 cancel_mouse_face (struct frame *f)
27634 {
27635 Lisp_Object window;
27636 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27637
27638 window = hlinfo->mouse_face_window;
27639 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27640 {
27641 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27642 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27643 hlinfo->mouse_face_window = Qnil;
27644 }
27645 }
27646
27647
27648 \f
27649 /***********************************************************************
27650 Exposure Events
27651 ***********************************************************************/
27652
27653 #ifdef HAVE_WINDOW_SYSTEM
27654
27655 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27656 which intersects rectangle R. R is in window-relative coordinates. */
27657
27658 static void
27659 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27660 enum glyph_row_area area)
27661 {
27662 struct glyph *first = row->glyphs[area];
27663 struct glyph *end = row->glyphs[area] + row->used[area];
27664 struct glyph *last;
27665 int first_x, start_x, x;
27666
27667 if (area == TEXT_AREA && row->fill_line_p)
27668 /* If row extends face to end of line write the whole line. */
27669 draw_glyphs (w, 0, row, area,
27670 0, row->used[area],
27671 DRAW_NORMAL_TEXT, 0);
27672 else
27673 {
27674 /* Set START_X to the window-relative start position for drawing glyphs of
27675 AREA. The first glyph of the text area can be partially visible.
27676 The first glyphs of other areas cannot. */
27677 start_x = window_box_left_offset (w, area);
27678 x = start_x;
27679 if (area == TEXT_AREA)
27680 x += row->x;
27681
27682 /* Find the first glyph that must be redrawn. */
27683 while (first < end
27684 && x + first->pixel_width < r->x)
27685 {
27686 x += first->pixel_width;
27687 ++first;
27688 }
27689
27690 /* Find the last one. */
27691 last = first;
27692 first_x = x;
27693 while (last < end
27694 && x < r->x + r->width)
27695 {
27696 x += last->pixel_width;
27697 ++last;
27698 }
27699
27700 /* Repaint. */
27701 if (last > first)
27702 draw_glyphs (w, first_x - start_x, row, area,
27703 first - row->glyphs[area], last - row->glyphs[area],
27704 DRAW_NORMAL_TEXT, 0);
27705 }
27706 }
27707
27708
27709 /* Redraw the parts of the glyph row ROW on window W intersecting
27710 rectangle R. R is in window-relative coordinates. Value is
27711 non-zero if mouse-face was overwritten. */
27712
27713 static int
27714 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27715 {
27716 xassert (row->enabled_p);
27717
27718 if (row->mode_line_p || w->pseudo_window_p)
27719 draw_glyphs (w, 0, row, TEXT_AREA,
27720 0, row->used[TEXT_AREA],
27721 DRAW_NORMAL_TEXT, 0);
27722 else
27723 {
27724 if (row->used[LEFT_MARGIN_AREA])
27725 expose_area (w, row, r, LEFT_MARGIN_AREA);
27726 if (row->used[TEXT_AREA])
27727 expose_area (w, row, r, TEXT_AREA);
27728 if (row->used[RIGHT_MARGIN_AREA])
27729 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27730 draw_row_fringe_bitmaps (w, row);
27731 }
27732
27733 return row->mouse_face_p;
27734 }
27735
27736
27737 /* Redraw those parts of glyphs rows during expose event handling that
27738 overlap other rows. Redrawing of an exposed line writes over parts
27739 of lines overlapping that exposed line; this function fixes that.
27740
27741 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27742 row in W's current matrix that is exposed and overlaps other rows.
27743 LAST_OVERLAPPING_ROW is the last such row. */
27744
27745 static void
27746 expose_overlaps (struct window *w,
27747 struct glyph_row *first_overlapping_row,
27748 struct glyph_row *last_overlapping_row,
27749 XRectangle *r)
27750 {
27751 struct glyph_row *row;
27752
27753 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27754 if (row->overlapping_p)
27755 {
27756 xassert (row->enabled_p && !row->mode_line_p);
27757
27758 row->clip = r;
27759 if (row->used[LEFT_MARGIN_AREA])
27760 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
27761
27762 if (row->used[TEXT_AREA])
27763 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
27764
27765 if (row->used[RIGHT_MARGIN_AREA])
27766 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
27767 row->clip = NULL;
27768 }
27769 }
27770
27771
27772 /* Return non-zero if W's cursor intersects rectangle R. */
27773
27774 static int
27775 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
27776 {
27777 XRectangle cr, result;
27778 struct glyph *cursor_glyph;
27779 struct glyph_row *row;
27780
27781 if (w->phys_cursor.vpos >= 0
27782 && w->phys_cursor.vpos < w->current_matrix->nrows
27783 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
27784 row->enabled_p)
27785 && row->cursor_in_fringe_p)
27786 {
27787 /* Cursor is in the fringe. */
27788 cr.x = window_box_right_offset (w,
27789 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
27790 ? RIGHT_MARGIN_AREA
27791 : TEXT_AREA));
27792 cr.y = row->y;
27793 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
27794 cr.height = row->height;
27795 return x_intersect_rectangles (&cr, r, &result);
27796 }
27797
27798 cursor_glyph = get_phys_cursor_glyph (w);
27799 if (cursor_glyph)
27800 {
27801 /* r is relative to W's box, but w->phys_cursor.x is relative
27802 to left edge of W's TEXT area. Adjust it. */
27803 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
27804 cr.y = w->phys_cursor.y;
27805 cr.width = cursor_glyph->pixel_width;
27806 cr.height = w->phys_cursor_height;
27807 /* ++KFS: W32 version used W32-specific IntersectRect here, but
27808 I assume the effect is the same -- and this is portable. */
27809 return x_intersect_rectangles (&cr, r, &result);
27810 }
27811 /* If we don't understand the format, pretend we're not in the hot-spot. */
27812 return 0;
27813 }
27814
27815
27816 /* EXPORT:
27817 Draw a vertical window border to the right of window W if W doesn't
27818 have vertical scroll bars. */
27819
27820 void
27821 x_draw_vertical_border (struct window *w)
27822 {
27823 struct frame *f = XFRAME (WINDOW_FRAME (w));
27824
27825 /* We could do better, if we knew what type of scroll-bar the adjacent
27826 windows (on either side) have... But we don't :-(
27827 However, I think this works ok. ++KFS 2003-04-25 */
27828
27829 /* Redraw borders between horizontally adjacent windows. Don't
27830 do it for frames with vertical scroll bars because either the
27831 right scroll bar of a window, or the left scroll bar of its
27832 neighbor will suffice as a border. */
27833 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
27834 return;
27835
27836 if (!WINDOW_RIGHTMOST_P (w)
27837 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
27838 {
27839 int x0, x1, y0, y1;
27840
27841 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27842 y1 -= 1;
27843
27844 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27845 x1 -= 1;
27846
27847 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
27848 }
27849 else if (!WINDOW_LEFTMOST_P (w)
27850 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
27851 {
27852 int x0, x1, y0, y1;
27853
27854 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27855 y1 -= 1;
27856
27857 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27858 x0 -= 1;
27859
27860 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
27861 }
27862 }
27863
27864
27865 /* Redraw the part of window W intersection rectangle FR. Pixel
27866 coordinates in FR are frame-relative. Call this function with
27867 input blocked. Value is non-zero if the exposure overwrites
27868 mouse-face. */
27869
27870 static int
27871 expose_window (struct window *w, XRectangle *fr)
27872 {
27873 struct frame *f = XFRAME (w->frame);
27874 XRectangle wr, r;
27875 int mouse_face_overwritten_p = 0;
27876
27877 /* If window is not yet fully initialized, do nothing. This can
27878 happen when toolkit scroll bars are used and a window is split.
27879 Reconfiguring the scroll bar will generate an expose for a newly
27880 created window. */
27881 if (w->current_matrix == NULL)
27882 return 0;
27883
27884 /* When we're currently updating the window, display and current
27885 matrix usually don't agree. Arrange for a thorough display
27886 later. */
27887 if (w == updated_window)
27888 {
27889 SET_FRAME_GARBAGED (f);
27890 return 0;
27891 }
27892
27893 /* Frame-relative pixel rectangle of W. */
27894 wr.x = WINDOW_LEFT_EDGE_X (w);
27895 wr.y = WINDOW_TOP_EDGE_Y (w);
27896 wr.width = WINDOW_TOTAL_WIDTH (w);
27897 wr.height = WINDOW_TOTAL_HEIGHT (w);
27898
27899 if (x_intersect_rectangles (fr, &wr, &r))
27900 {
27901 int yb = window_text_bottom_y (w);
27902 struct glyph_row *row;
27903 int cursor_cleared_p, phys_cursor_on_p;
27904 struct glyph_row *first_overlapping_row, *last_overlapping_row;
27905
27906 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
27907 r.x, r.y, r.width, r.height));
27908
27909 /* Convert to window coordinates. */
27910 r.x -= WINDOW_LEFT_EDGE_X (w);
27911 r.y -= WINDOW_TOP_EDGE_Y (w);
27912
27913 /* Turn off the cursor. */
27914 if (!w->pseudo_window_p
27915 && phys_cursor_in_rect_p (w, &r))
27916 {
27917 x_clear_cursor (w);
27918 cursor_cleared_p = 1;
27919 }
27920 else
27921 cursor_cleared_p = 0;
27922
27923 /* If the row containing the cursor extends face to end of line,
27924 then expose_area might overwrite the cursor outside the
27925 rectangle and thus notice_overwritten_cursor might clear
27926 w->phys_cursor_on_p. We remember the original value and
27927 check later if it is changed. */
27928 phys_cursor_on_p = w->phys_cursor_on_p;
27929
27930 /* Update lines intersecting rectangle R. */
27931 first_overlapping_row = last_overlapping_row = NULL;
27932 for (row = w->current_matrix->rows;
27933 row->enabled_p;
27934 ++row)
27935 {
27936 int y0 = row->y;
27937 int y1 = MATRIX_ROW_BOTTOM_Y (row);
27938
27939 if ((y0 >= r.y && y0 < r.y + r.height)
27940 || (y1 > r.y && y1 < r.y + r.height)
27941 || (r.y >= y0 && r.y < y1)
27942 || (r.y + r.height > y0 && r.y + r.height < y1))
27943 {
27944 /* A header line may be overlapping, but there is no need
27945 to fix overlapping areas for them. KFS 2005-02-12 */
27946 if (row->overlapping_p && !row->mode_line_p)
27947 {
27948 if (first_overlapping_row == NULL)
27949 first_overlapping_row = row;
27950 last_overlapping_row = row;
27951 }
27952
27953 row->clip = fr;
27954 if (expose_line (w, row, &r))
27955 mouse_face_overwritten_p = 1;
27956 row->clip = NULL;
27957 }
27958 else if (row->overlapping_p)
27959 {
27960 /* We must redraw a row overlapping the exposed area. */
27961 if (y0 < r.y
27962 ? y0 + row->phys_height > r.y
27963 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
27964 {
27965 if (first_overlapping_row == NULL)
27966 first_overlapping_row = row;
27967 last_overlapping_row = row;
27968 }
27969 }
27970
27971 if (y1 >= yb)
27972 break;
27973 }
27974
27975 /* Display the mode line if there is one. */
27976 if (WINDOW_WANTS_MODELINE_P (w)
27977 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
27978 row->enabled_p)
27979 && row->y < r.y + r.height)
27980 {
27981 if (expose_line (w, row, &r))
27982 mouse_face_overwritten_p = 1;
27983 }
27984
27985 if (!w->pseudo_window_p)
27986 {
27987 /* Fix the display of overlapping rows. */
27988 if (first_overlapping_row)
27989 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
27990 fr);
27991
27992 /* Draw border between windows. */
27993 x_draw_vertical_border (w);
27994
27995 /* Turn the cursor on again. */
27996 if (cursor_cleared_p
27997 || (phys_cursor_on_p && !w->phys_cursor_on_p))
27998 update_window_cursor (w, 1);
27999 }
28000 }
28001
28002 return mouse_face_overwritten_p;
28003 }
28004
28005
28006
28007 /* Redraw (parts) of all windows in the window tree rooted at W that
28008 intersect R. R contains frame pixel coordinates. Value is
28009 non-zero if the exposure overwrites mouse-face. */
28010
28011 static int
28012 expose_window_tree (struct window *w, XRectangle *r)
28013 {
28014 struct frame *f = XFRAME (w->frame);
28015 int mouse_face_overwritten_p = 0;
28016
28017 while (w && !FRAME_GARBAGED_P (f))
28018 {
28019 if (!NILP (w->hchild))
28020 mouse_face_overwritten_p
28021 |= expose_window_tree (XWINDOW (w->hchild), r);
28022 else if (!NILP (w->vchild))
28023 mouse_face_overwritten_p
28024 |= expose_window_tree (XWINDOW (w->vchild), r);
28025 else
28026 mouse_face_overwritten_p |= expose_window (w, r);
28027
28028 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28029 }
28030
28031 return mouse_face_overwritten_p;
28032 }
28033
28034
28035 /* EXPORT:
28036 Redisplay an exposed area of frame F. X and Y are the upper-left
28037 corner of the exposed rectangle. W and H are width and height of
28038 the exposed area. All are pixel values. W or H zero means redraw
28039 the entire frame. */
28040
28041 void
28042 expose_frame (struct frame *f, int x, int y, int w, int h)
28043 {
28044 XRectangle r;
28045 int mouse_face_overwritten_p = 0;
28046
28047 TRACE ((stderr, "expose_frame "));
28048
28049 /* No need to redraw if frame will be redrawn soon. */
28050 if (FRAME_GARBAGED_P (f))
28051 {
28052 TRACE ((stderr, " garbaged\n"));
28053 return;
28054 }
28055
28056 /* If basic faces haven't been realized yet, there is no point in
28057 trying to redraw anything. This can happen when we get an expose
28058 event while Emacs is starting, e.g. by moving another window. */
28059 if (FRAME_FACE_CACHE (f) == NULL
28060 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28061 {
28062 TRACE ((stderr, " no faces\n"));
28063 return;
28064 }
28065
28066 if (w == 0 || h == 0)
28067 {
28068 r.x = r.y = 0;
28069 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28070 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28071 }
28072 else
28073 {
28074 r.x = x;
28075 r.y = y;
28076 r.width = w;
28077 r.height = h;
28078 }
28079
28080 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28081 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28082
28083 if (WINDOWP (f->tool_bar_window))
28084 mouse_face_overwritten_p
28085 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28086
28087 #ifdef HAVE_X_WINDOWS
28088 #ifndef MSDOS
28089 #ifndef USE_X_TOOLKIT
28090 if (WINDOWP (f->menu_bar_window))
28091 mouse_face_overwritten_p
28092 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28093 #endif /* not USE_X_TOOLKIT */
28094 #endif
28095 #endif
28096
28097 /* Some window managers support a focus-follows-mouse style with
28098 delayed raising of frames. Imagine a partially obscured frame,
28099 and moving the mouse into partially obscured mouse-face on that
28100 frame. The visible part of the mouse-face will be highlighted,
28101 then the WM raises the obscured frame. With at least one WM, KDE
28102 2.1, Emacs is not getting any event for the raising of the frame
28103 (even tried with SubstructureRedirectMask), only Expose events.
28104 These expose events will draw text normally, i.e. not
28105 highlighted. Which means we must redo the highlight here.
28106 Subsume it under ``we love X''. --gerd 2001-08-15 */
28107 /* Included in Windows version because Windows most likely does not
28108 do the right thing if any third party tool offers
28109 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28110 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28111 {
28112 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28113 if (f == hlinfo->mouse_face_mouse_frame)
28114 {
28115 int mouse_x = hlinfo->mouse_face_mouse_x;
28116 int mouse_y = hlinfo->mouse_face_mouse_y;
28117 clear_mouse_face (hlinfo);
28118 note_mouse_highlight (f, mouse_x, mouse_y);
28119 }
28120 }
28121 }
28122
28123
28124 /* EXPORT:
28125 Determine the intersection of two rectangles R1 and R2. Return
28126 the intersection in *RESULT. Value is non-zero if RESULT is not
28127 empty. */
28128
28129 int
28130 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28131 {
28132 XRectangle *left, *right;
28133 XRectangle *upper, *lower;
28134 int intersection_p = 0;
28135
28136 /* Rearrange so that R1 is the left-most rectangle. */
28137 if (r1->x < r2->x)
28138 left = r1, right = r2;
28139 else
28140 left = r2, right = r1;
28141
28142 /* X0 of the intersection is right.x0, if this is inside R1,
28143 otherwise there is no intersection. */
28144 if (right->x <= left->x + left->width)
28145 {
28146 result->x = right->x;
28147
28148 /* The right end of the intersection is the minimum of
28149 the right ends of left and right. */
28150 result->width = (min (left->x + left->width, right->x + right->width)
28151 - result->x);
28152
28153 /* Same game for Y. */
28154 if (r1->y < r2->y)
28155 upper = r1, lower = r2;
28156 else
28157 upper = r2, lower = r1;
28158
28159 /* The upper end of the intersection is lower.y0, if this is inside
28160 of upper. Otherwise, there is no intersection. */
28161 if (lower->y <= upper->y + upper->height)
28162 {
28163 result->y = lower->y;
28164
28165 /* The lower end of the intersection is the minimum of the lower
28166 ends of upper and lower. */
28167 result->height = (min (lower->y + lower->height,
28168 upper->y + upper->height)
28169 - result->y);
28170 intersection_p = 1;
28171 }
28172 }
28173
28174 return intersection_p;
28175 }
28176
28177 #endif /* HAVE_WINDOW_SYSTEM */
28178
28179 \f
28180 /***********************************************************************
28181 Initialization
28182 ***********************************************************************/
28183
28184 void
28185 syms_of_xdisp (void)
28186 {
28187 Vwith_echo_area_save_vector = Qnil;
28188 staticpro (&Vwith_echo_area_save_vector);
28189
28190 Vmessage_stack = Qnil;
28191 staticpro (&Vmessage_stack);
28192
28193 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28194
28195 message_dolog_marker1 = Fmake_marker ();
28196 staticpro (&message_dolog_marker1);
28197 message_dolog_marker2 = Fmake_marker ();
28198 staticpro (&message_dolog_marker2);
28199 message_dolog_marker3 = Fmake_marker ();
28200 staticpro (&message_dolog_marker3);
28201
28202 #if GLYPH_DEBUG
28203 defsubr (&Sdump_frame_glyph_matrix);
28204 defsubr (&Sdump_glyph_matrix);
28205 defsubr (&Sdump_glyph_row);
28206 defsubr (&Sdump_tool_bar_row);
28207 defsubr (&Strace_redisplay);
28208 defsubr (&Strace_to_stderr);
28209 #endif
28210 #ifdef HAVE_WINDOW_SYSTEM
28211 defsubr (&Stool_bar_lines_needed);
28212 defsubr (&Slookup_image_map);
28213 #endif
28214 defsubr (&Sformat_mode_line);
28215 defsubr (&Sinvisible_p);
28216 defsubr (&Scurrent_bidi_paragraph_direction);
28217
28218 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28219 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28220 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28221 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28222 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28223 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28224 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28225 DEFSYM (Qeval, "eval");
28226 DEFSYM (QCdata, ":data");
28227 DEFSYM (Qdisplay, "display");
28228 DEFSYM (Qspace_width, "space-width");
28229 DEFSYM (Qraise, "raise");
28230 DEFSYM (Qslice, "slice");
28231 DEFSYM (Qspace, "space");
28232 DEFSYM (Qmargin, "margin");
28233 DEFSYM (Qpointer, "pointer");
28234 DEFSYM (Qleft_margin, "left-margin");
28235 DEFSYM (Qright_margin, "right-margin");
28236 DEFSYM (Qcenter, "center");
28237 DEFSYM (Qline_height, "line-height");
28238 DEFSYM (QCalign_to, ":align-to");
28239 DEFSYM (QCrelative_width, ":relative-width");
28240 DEFSYM (QCrelative_height, ":relative-height");
28241 DEFSYM (QCeval, ":eval");
28242 DEFSYM (QCpropertize, ":propertize");
28243 DEFSYM (QCfile, ":file");
28244 DEFSYM (Qfontified, "fontified");
28245 DEFSYM (Qfontification_functions, "fontification-functions");
28246 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28247 DEFSYM (Qescape_glyph, "escape-glyph");
28248 DEFSYM (Qnobreak_space, "nobreak-space");
28249 DEFSYM (Qimage, "image");
28250 DEFSYM (Qtext, "text");
28251 DEFSYM (Qboth, "both");
28252 DEFSYM (Qboth_horiz, "both-horiz");
28253 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28254 DEFSYM (QCmap, ":map");
28255 DEFSYM (QCpointer, ":pointer");
28256 DEFSYM (Qrect, "rect");
28257 DEFSYM (Qcircle, "circle");
28258 DEFSYM (Qpoly, "poly");
28259 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28260 DEFSYM (Qgrow_only, "grow-only");
28261 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28262 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28263 DEFSYM (Qposition, "position");
28264 DEFSYM (Qbuffer_position, "buffer-position");
28265 DEFSYM (Qobject, "object");
28266 DEFSYM (Qbar, "bar");
28267 DEFSYM (Qhbar, "hbar");
28268 DEFSYM (Qbox, "box");
28269 DEFSYM (Qhollow, "hollow");
28270 DEFSYM (Qhand, "hand");
28271 DEFSYM (Qarrow, "arrow");
28272 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28273
28274 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28275 Fcons (intern_c_string ("void-variable"), Qnil)),
28276 Qnil);
28277 staticpro (&list_of_error);
28278
28279 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28280 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28281 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28282 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28283
28284 echo_buffer[0] = echo_buffer[1] = Qnil;
28285 staticpro (&echo_buffer[0]);
28286 staticpro (&echo_buffer[1]);
28287
28288 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28289 staticpro (&echo_area_buffer[0]);
28290 staticpro (&echo_area_buffer[1]);
28291
28292 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
28293 staticpro (&Vmessages_buffer_name);
28294
28295 mode_line_proptrans_alist = Qnil;
28296 staticpro (&mode_line_proptrans_alist);
28297 mode_line_string_list = Qnil;
28298 staticpro (&mode_line_string_list);
28299 mode_line_string_face = Qnil;
28300 staticpro (&mode_line_string_face);
28301 mode_line_string_face_prop = Qnil;
28302 staticpro (&mode_line_string_face_prop);
28303 Vmode_line_unwind_vector = Qnil;
28304 staticpro (&Vmode_line_unwind_vector);
28305
28306 help_echo_string = Qnil;
28307 staticpro (&help_echo_string);
28308 help_echo_object = Qnil;
28309 staticpro (&help_echo_object);
28310 help_echo_window = Qnil;
28311 staticpro (&help_echo_window);
28312 previous_help_echo_string = Qnil;
28313 staticpro (&previous_help_echo_string);
28314 help_echo_pos = -1;
28315
28316 DEFSYM (Qright_to_left, "right-to-left");
28317 DEFSYM (Qleft_to_right, "left-to-right");
28318
28319 #ifdef HAVE_WINDOW_SYSTEM
28320 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28321 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
28322 For example, if a block cursor is over a tab, it will be drawn as
28323 wide as that tab on the display. */);
28324 x_stretch_cursor_p = 0;
28325 #endif
28326
28327 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28328 doc: /* *Non-nil means highlight trailing whitespace.
28329 The face used for trailing whitespace is `trailing-whitespace'. */);
28330 Vshow_trailing_whitespace = Qnil;
28331
28332 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28333 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28334 If the value is t, Emacs highlights non-ASCII chars which have the
28335 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28336 or `escape-glyph' face respectively.
28337
28338 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28339 U+2011 (non-breaking hyphen) are affected.
28340
28341 Any other non-nil value means to display these characters as a escape
28342 glyph followed by an ordinary space or hyphen.
28343
28344 A value of nil means no special handling of these characters. */);
28345 Vnobreak_char_display = Qt;
28346
28347 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28348 doc: /* *The pointer shape to show in void text areas.
28349 A value of nil means to show the text pointer. Other options are `arrow',
28350 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28351 Vvoid_text_area_pointer = Qarrow;
28352
28353 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28354 doc: /* Non-nil means don't actually do any redisplay.
28355 This is used for internal purposes. */);
28356 Vinhibit_redisplay = Qnil;
28357
28358 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28359 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28360 Vglobal_mode_string = Qnil;
28361
28362 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28363 doc: /* Marker for where to display an arrow on top of the buffer text.
28364 This must be the beginning of a line in order to work.
28365 See also `overlay-arrow-string'. */);
28366 Voverlay_arrow_position = Qnil;
28367
28368 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28369 doc: /* String to display as an arrow in non-window frames.
28370 See also `overlay-arrow-position'. */);
28371 Voverlay_arrow_string = make_pure_c_string ("=>");
28372
28373 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28374 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28375 The symbols on this list are examined during redisplay to determine
28376 where to display overlay arrows. */);
28377 Voverlay_arrow_variable_list
28378 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28379
28380 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28381 doc: /* *The number of lines to try scrolling a window by when point moves out.
28382 If that fails to bring point back on frame, point is centered instead.
28383 If this is zero, point is always centered after it moves off frame.
28384 If you want scrolling to always be a line at a time, you should set
28385 `scroll-conservatively' to a large value rather than set this to 1. */);
28386
28387 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28388 doc: /* *Scroll up to this many lines, to bring point back on screen.
28389 If point moves off-screen, redisplay will scroll by up to
28390 `scroll-conservatively' lines in order to bring point just barely
28391 onto the screen again. If that cannot be done, then redisplay
28392 recenters point as usual.
28393
28394 If the value is greater than 100, redisplay will never recenter point,
28395 but will always scroll just enough text to bring point into view, even
28396 if you move far away.
28397
28398 A value of zero means always recenter point if it moves off screen. */);
28399 scroll_conservatively = 0;
28400
28401 DEFVAR_INT ("scroll-margin", scroll_margin,
28402 doc: /* *Number of lines of margin at the top and bottom of a window.
28403 Recenter the window whenever point gets within this many lines
28404 of the top or bottom of the window. */);
28405 scroll_margin = 0;
28406
28407 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28408 doc: /* Pixels per inch value for non-window system displays.
28409 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28410 Vdisplay_pixels_per_inch = make_float (72.0);
28411
28412 #if GLYPH_DEBUG
28413 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28414 #endif
28415
28416 DEFVAR_LISP ("truncate-partial-width-windows",
28417 Vtruncate_partial_width_windows,
28418 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28419 For an integer value, truncate lines in each window narrower than the
28420 full frame width, provided the window width is less than that integer;
28421 otherwise, respect the value of `truncate-lines'.
28422
28423 For any other non-nil value, truncate lines in all windows that do
28424 not span the full frame width.
28425
28426 A value of nil means to respect the value of `truncate-lines'.
28427
28428 If `word-wrap' is enabled, you might want to reduce this. */);
28429 Vtruncate_partial_width_windows = make_number (50);
28430
28431 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28432 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28433 Any other value means to use the appropriate face, `mode-line',
28434 `header-line', or `menu' respectively. */);
28435 mode_line_inverse_video = 1;
28436
28437 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28438 doc: /* *Maximum buffer size for which line number should be displayed.
28439 If the buffer is bigger than this, the line number does not appear
28440 in the mode line. A value of nil means no limit. */);
28441 Vline_number_display_limit = Qnil;
28442
28443 DEFVAR_INT ("line-number-display-limit-width",
28444 line_number_display_limit_width,
28445 doc: /* *Maximum line width (in characters) for line number display.
28446 If the average length of the lines near point is bigger than this, then the
28447 line number may be omitted from the mode line. */);
28448 line_number_display_limit_width = 200;
28449
28450 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28451 doc: /* *Non-nil means highlight region even in nonselected windows. */);
28452 highlight_nonselected_windows = 0;
28453
28454 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28455 doc: /* Non-nil if more than one frame is visible on this display.
28456 Minibuffer-only frames don't count, but iconified frames do.
28457 This variable is not guaranteed to be accurate except while processing
28458 `frame-title-format' and `icon-title-format'. */);
28459
28460 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28461 doc: /* Template for displaying the title bar of visible frames.
28462 \(Assuming the window manager supports this feature.)
28463
28464 This variable has the same structure as `mode-line-format', except that
28465 the %c and %l constructs are ignored. It is used only on frames for
28466 which no explicit name has been set \(see `modify-frame-parameters'). */);
28467
28468 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28469 doc: /* Template for displaying the title bar of an iconified frame.
28470 \(Assuming the window manager supports this feature.)
28471 This variable has the same structure as `mode-line-format' (which see),
28472 and is used only on frames for which no explicit name has been set
28473 \(see `modify-frame-parameters'). */);
28474 Vicon_title_format
28475 = Vframe_title_format
28476 = pure_cons (intern_c_string ("multiple-frames"),
28477 pure_cons (make_pure_c_string ("%b"),
28478 pure_cons (pure_cons (empty_unibyte_string,
28479 pure_cons (intern_c_string ("invocation-name"),
28480 pure_cons (make_pure_c_string ("@"),
28481 pure_cons (intern_c_string ("system-name"),
28482 Qnil)))),
28483 Qnil)));
28484
28485 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28486 doc: /* Maximum number of lines to keep in the message log buffer.
28487 If nil, disable message logging. If t, log messages but don't truncate
28488 the buffer when it becomes large. */);
28489 Vmessage_log_max = make_number (100);
28490
28491 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28492 doc: /* Functions called before redisplay, if window sizes have changed.
28493 The value should be a list of functions that take one argument.
28494 Just before redisplay, for each frame, if any of its windows have changed
28495 size since the last redisplay, or have been split or deleted,
28496 all the functions in the list are called, with the frame as argument. */);
28497 Vwindow_size_change_functions = Qnil;
28498
28499 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28500 doc: /* List of functions to call before redisplaying a window with scrolling.
28501 Each function is called with two arguments, the window and its new
28502 display-start position. Note that these functions are also called by
28503 `set-window-buffer'. Also note that the value of `window-end' is not
28504 valid when these functions are called.
28505
28506 Warning: Do not use this feature to alter the way the window
28507 is scrolled. It is not designed for that, and such use probably won't
28508 work. */);
28509 Vwindow_scroll_functions = Qnil;
28510
28511 DEFVAR_LISP ("window-text-change-functions",
28512 Vwindow_text_change_functions,
28513 doc: /* Functions to call in redisplay when text in the window might change. */);
28514 Vwindow_text_change_functions = Qnil;
28515
28516 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28517 doc: /* Functions called when redisplay of a window reaches the end trigger.
28518 Each function is called with two arguments, the window and the end trigger value.
28519 See `set-window-redisplay-end-trigger'. */);
28520 Vredisplay_end_trigger_functions = Qnil;
28521
28522 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28523 doc: /* *Non-nil means autoselect window with mouse pointer.
28524 If nil, do not autoselect windows.
28525 A positive number means delay autoselection by that many seconds: a
28526 window is autoselected only after the mouse has remained in that
28527 window for the duration of the delay.
28528 A negative number has a similar effect, but causes windows to be
28529 autoselected only after the mouse has stopped moving. \(Because of
28530 the way Emacs compares mouse events, you will occasionally wait twice
28531 that time before the window gets selected.\)
28532 Any other value means to autoselect window instantaneously when the
28533 mouse pointer enters it.
28534
28535 Autoselection selects the minibuffer only if it is active, and never
28536 unselects the minibuffer if it is active.
28537
28538 When customizing this variable make sure that the actual value of
28539 `focus-follows-mouse' matches the behavior of your window manager. */);
28540 Vmouse_autoselect_window = Qnil;
28541
28542 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28543 doc: /* *Non-nil means automatically resize tool-bars.
28544 This dynamically changes the tool-bar's height to the minimum height
28545 that is needed to make all tool-bar items visible.
28546 If value is `grow-only', the tool-bar's height is only increased
28547 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28548 Vauto_resize_tool_bars = Qt;
28549
28550 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28551 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28552 auto_raise_tool_bar_buttons_p = 1;
28553
28554 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28555 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28556 make_cursor_line_fully_visible_p = 1;
28557
28558 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28559 doc: /* *Border below tool-bar in pixels.
28560 If an integer, use it as the height of the border.
28561 If it is one of `internal-border-width' or `border-width', use the
28562 value of the corresponding frame parameter.
28563 Otherwise, no border is added below the tool-bar. */);
28564 Vtool_bar_border = Qinternal_border_width;
28565
28566 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28567 doc: /* *Margin around tool-bar buttons in pixels.
28568 If an integer, use that for both horizontal and vertical margins.
28569 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28570 HORZ specifying the horizontal margin, and VERT specifying the
28571 vertical margin. */);
28572 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28573
28574 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28575 doc: /* *Relief thickness of tool-bar buttons. */);
28576 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28577
28578 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28579 doc: /* Tool bar style to use.
28580 It can be one of
28581 image - show images only
28582 text - show text only
28583 both - show both, text below image
28584 both-horiz - show text to the right of the image
28585 text-image-horiz - show text to the left of the image
28586 any other - use system default or image if no system default. */);
28587 Vtool_bar_style = Qnil;
28588
28589 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28590 doc: /* *Maximum number of characters a label can have to be shown.
28591 The tool bar style must also show labels for this to have any effect, see
28592 `tool-bar-style'. */);
28593 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28594
28595 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28596 doc: /* List of functions to call to fontify regions of text.
28597 Each function is called with one argument POS. Functions must
28598 fontify a region starting at POS in the current buffer, and give
28599 fontified regions the property `fontified'. */);
28600 Vfontification_functions = Qnil;
28601 Fmake_variable_buffer_local (Qfontification_functions);
28602
28603 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28604 unibyte_display_via_language_environment,
28605 doc: /* *Non-nil means display unibyte text according to language environment.
28606 Specifically, this means that raw bytes in the range 160-255 decimal
28607 are displayed by converting them to the equivalent multibyte characters
28608 according to the current language environment. As a result, they are
28609 displayed according to the current fontset.
28610
28611 Note that this variable affects only how these bytes are displayed,
28612 but does not change the fact they are interpreted as raw bytes. */);
28613 unibyte_display_via_language_environment = 0;
28614
28615 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28616 doc: /* *Maximum height for resizing mini-windows (the minibuffer and the echo area).
28617 If a float, it specifies a fraction of the mini-window frame's height.
28618 If an integer, it specifies a number of lines. */);
28619 Vmax_mini_window_height = make_float (0.25);
28620
28621 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28622 doc: /* How to resize mini-windows (the minibuffer and the echo area).
28623 A value of nil means don't automatically resize mini-windows.
28624 A value of t means resize them to fit the text displayed in them.
28625 A value of `grow-only', the default, means let mini-windows grow only;
28626 they return to their normal size when the minibuffer is closed, or the
28627 echo area becomes empty. */);
28628 Vresize_mini_windows = Qgrow_only;
28629
28630 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28631 doc: /* Alist specifying how to blink the cursor off.
28632 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28633 `cursor-type' frame-parameter or variable equals ON-STATE,
28634 comparing using `equal', Emacs uses OFF-STATE to specify
28635 how to blink it off. ON-STATE and OFF-STATE are values for
28636 the `cursor-type' frame parameter.
28637
28638 If a frame's ON-STATE has no entry in this list,
28639 the frame's other specifications determine how to blink the cursor off. */);
28640 Vblink_cursor_alist = Qnil;
28641
28642 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28643 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28644 If non-nil, windows are automatically scrolled horizontally to make
28645 point visible. */);
28646 automatic_hscrolling_p = 1;
28647 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28648
28649 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28650 doc: /* *How many columns away from the window edge point is allowed to get
28651 before automatic hscrolling will horizontally scroll the window. */);
28652 hscroll_margin = 5;
28653
28654 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28655 doc: /* *How many columns to scroll the window when point gets too close to the edge.
28656 When point is less than `hscroll-margin' columns from the window
28657 edge, automatic hscrolling will scroll the window by the amount of columns
28658 determined by this variable. If its value is a positive integer, scroll that
28659 many columns. If it's a positive floating-point number, it specifies the
28660 fraction of the window's width to scroll. If it's nil or zero, point will be
28661 centered horizontally after the scroll. Any other value, including negative
28662 numbers, are treated as if the value were zero.
28663
28664 Automatic hscrolling always moves point outside the scroll margin, so if
28665 point was more than scroll step columns inside the margin, the window will
28666 scroll more than the value given by the scroll step.
28667
28668 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28669 and `scroll-right' overrides this variable's effect. */);
28670 Vhscroll_step = make_number (0);
28671
28672 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28673 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28674 Bind this around calls to `message' to let it take effect. */);
28675 message_truncate_lines = 0;
28676
28677 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28678 doc: /* Normal hook run to update the menu bar definitions.
28679 Redisplay runs this hook before it redisplays the menu bar.
28680 This is used to update submenus such as Buffers,
28681 whose contents depend on various data. */);
28682 Vmenu_bar_update_hook = Qnil;
28683
28684 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28685 doc: /* Frame for which we are updating a menu.
28686 The enable predicate for a menu binding should check this variable. */);
28687 Vmenu_updating_frame = Qnil;
28688
28689 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28690 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28691 inhibit_menubar_update = 0;
28692
28693 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28694 doc: /* Prefix prepended to all continuation lines at display time.
28695 The value may be a string, an image, or a stretch-glyph; it is
28696 interpreted in the same way as the value of a `display' text property.
28697
28698 This variable is overridden by any `wrap-prefix' text or overlay
28699 property.
28700
28701 To add a prefix to non-continuation lines, use `line-prefix'. */);
28702 Vwrap_prefix = Qnil;
28703 DEFSYM (Qwrap_prefix, "wrap-prefix");
28704 Fmake_variable_buffer_local (Qwrap_prefix);
28705
28706 DEFVAR_LISP ("line-prefix", Vline_prefix,
28707 doc: /* Prefix prepended to all non-continuation lines at display time.
28708 The value may be a string, an image, or a stretch-glyph; it is
28709 interpreted in the same way as the value of a `display' text property.
28710
28711 This variable is overridden by any `line-prefix' text or overlay
28712 property.
28713
28714 To add a prefix to continuation lines, use `wrap-prefix'. */);
28715 Vline_prefix = Qnil;
28716 DEFSYM (Qline_prefix, "line-prefix");
28717 Fmake_variable_buffer_local (Qline_prefix);
28718
28719 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28720 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28721 inhibit_eval_during_redisplay = 0;
28722
28723 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28724 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28725 inhibit_free_realized_faces = 0;
28726
28727 #if GLYPH_DEBUG
28728 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28729 doc: /* Inhibit try_window_id display optimization. */);
28730 inhibit_try_window_id = 0;
28731
28732 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28733 doc: /* Inhibit try_window_reusing display optimization. */);
28734 inhibit_try_window_reusing = 0;
28735
28736 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28737 doc: /* Inhibit try_cursor_movement display optimization. */);
28738 inhibit_try_cursor_movement = 0;
28739 #endif /* GLYPH_DEBUG */
28740
28741 DEFVAR_INT ("overline-margin", overline_margin,
28742 doc: /* *Space between overline and text, in pixels.
28743 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28744 margin to the character height. */);
28745 overline_margin = 2;
28746
28747 DEFVAR_INT ("underline-minimum-offset",
28748 underline_minimum_offset,
28749 doc: /* Minimum distance between baseline and underline.
28750 This can improve legibility of underlined text at small font sizes,
28751 particularly when using variable `x-use-underline-position-properties'
28752 with fonts that specify an UNDERLINE_POSITION relatively close to the
28753 baseline. The default value is 1. */);
28754 underline_minimum_offset = 1;
28755
28756 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
28757 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
28758 This feature only works when on a window system that can change
28759 cursor shapes. */);
28760 display_hourglass_p = 1;
28761
28762 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
28763 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
28764 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
28765
28766 hourglass_atimer = NULL;
28767 hourglass_shown_p = 0;
28768
28769 DEFSYM (Qglyphless_char, "glyphless-char");
28770 DEFSYM (Qhex_code, "hex-code");
28771 DEFSYM (Qempty_box, "empty-box");
28772 DEFSYM (Qthin_space, "thin-space");
28773 DEFSYM (Qzero_width, "zero-width");
28774
28775 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
28776 /* Intern this now in case it isn't already done.
28777 Setting this variable twice is harmless.
28778 But don't staticpro it here--that is done in alloc.c. */
28779 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
28780 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
28781
28782 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
28783 doc: /* Char-table defining glyphless characters.
28784 Each element, if non-nil, should be one of the following:
28785 an ASCII acronym string: display this string in a box
28786 `hex-code': display the hexadecimal code of a character in a box
28787 `empty-box': display as an empty box
28788 `thin-space': display as 1-pixel width space
28789 `zero-width': don't display
28790 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
28791 display method for graphical terminals and text terminals respectively.
28792 GRAPHICAL and TEXT should each have one of the values listed above.
28793
28794 The char-table has one extra slot to control the display of a character for
28795 which no font is found. This slot only takes effect on graphical terminals.
28796 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
28797 `thin-space'. The default is `empty-box'. */);
28798 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
28799 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
28800 Qempty_box);
28801 }
28802
28803
28804 /* Initialize this module when Emacs starts. */
28805
28806 void
28807 init_xdisp (void)
28808 {
28809 current_header_line_height = current_mode_line_height = -1;
28810
28811 CHARPOS (this_line_start_pos) = 0;
28812
28813 if (!noninteractive)
28814 {
28815 struct window *m = XWINDOW (minibuf_window);
28816 Lisp_Object frame = m->frame;
28817 struct frame *f = XFRAME (frame);
28818 Lisp_Object root = FRAME_ROOT_WINDOW (f);
28819 struct window *r = XWINDOW (root);
28820 int i;
28821
28822 echo_area_window = minibuf_window;
28823
28824 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
28825 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
28826 XSETFASTINT (r->total_cols, FRAME_COLS (f));
28827 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
28828 XSETFASTINT (m->total_lines, 1);
28829 XSETFASTINT (m->total_cols, FRAME_COLS (f));
28830
28831 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
28832 scratch_glyph_row.glyphs[TEXT_AREA + 1]
28833 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
28834
28835 /* The default ellipsis glyphs `...'. */
28836 for (i = 0; i < 3; ++i)
28837 default_invis_vector[i] = make_number ('.');
28838 }
28839
28840 {
28841 /* Allocate the buffer for frame titles.
28842 Also used for `format-mode-line'. */
28843 int size = 100;
28844 mode_line_noprop_buf = (char *) xmalloc (size);
28845 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
28846 mode_line_noprop_ptr = mode_line_noprop_buf;
28847 mode_line_target = MODE_LINE_DISPLAY;
28848 }
28849
28850 help_echo_showing_p = 0;
28851 }
28852
28853 /* Since w32 does not support atimers, it defines its own implementation of
28854 the following three functions in w32fns.c. */
28855 #ifndef WINDOWSNT
28856
28857 /* Platform-independent portion of hourglass implementation. */
28858
28859 /* Return non-zero if hourglass timer has been started or hourglass is
28860 shown. */
28861 int
28862 hourglass_started (void)
28863 {
28864 return hourglass_shown_p || hourglass_atimer != NULL;
28865 }
28866
28867 /* Cancel a currently active hourglass timer, and start a new one. */
28868 void
28869 start_hourglass (void)
28870 {
28871 #if defined (HAVE_WINDOW_SYSTEM)
28872 EMACS_TIME delay;
28873 int secs, usecs = 0;
28874
28875 cancel_hourglass ();
28876
28877 if (INTEGERP (Vhourglass_delay)
28878 && XINT (Vhourglass_delay) > 0)
28879 secs = XFASTINT (Vhourglass_delay);
28880 else if (FLOATP (Vhourglass_delay)
28881 && XFLOAT_DATA (Vhourglass_delay) > 0)
28882 {
28883 Lisp_Object tem;
28884 tem = Ftruncate (Vhourglass_delay, Qnil);
28885 secs = XFASTINT (tem);
28886 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
28887 }
28888 else
28889 secs = DEFAULT_HOURGLASS_DELAY;
28890
28891 EMACS_SET_SECS_USECS (delay, secs, usecs);
28892 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
28893 show_hourglass, NULL);
28894 #endif
28895 }
28896
28897
28898 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
28899 shown. */
28900 void
28901 cancel_hourglass (void)
28902 {
28903 #if defined (HAVE_WINDOW_SYSTEM)
28904 if (hourglass_atimer)
28905 {
28906 cancel_atimer (hourglass_atimer);
28907 hourglass_atimer = NULL;
28908 }
28909
28910 if (hourglass_shown_p)
28911 hide_hourglass ();
28912 #endif
28913 }
28914 #endif /* ! WINDOWSNT */