Fix various problems with cursor positioning around display properties.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2011 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the 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 xfree (CACHE); \
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); \
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 unsigned long int 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 *);
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 INLINE int
958 window_text_bottom_y (struct window *w)
959 {
960 int height = WINDOW_TOTAL_HEIGHT (w);
961
962 if (WINDOW_WANTS_MODELINE_P (w))
963 height -= CURRENT_MODE_LINE_HEIGHT (w);
964 return height;
965 }
966
967 /* Return the pixel width of display area AREA of window W. AREA < 0
968 means return the total width of W, not including fringes to
969 the left and right of the window. */
970
971 INLINE int
972 window_box_width (struct window *w, int area)
973 {
974 int cols = XFASTINT (w->total_cols);
975 int pixels = 0;
976
977 if (!w->pseudo_window_p)
978 {
979 cols -= WINDOW_SCROLL_BAR_COLS (w);
980
981 if (area == TEXT_AREA)
982 {
983 if (INTEGERP (w->left_margin_cols))
984 cols -= XFASTINT (w->left_margin_cols);
985 if (INTEGERP (w->right_margin_cols))
986 cols -= XFASTINT (w->right_margin_cols);
987 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
988 }
989 else if (area == LEFT_MARGIN_AREA)
990 {
991 cols = (INTEGERP (w->left_margin_cols)
992 ? XFASTINT (w->left_margin_cols) : 0);
993 pixels = 0;
994 }
995 else if (area == RIGHT_MARGIN_AREA)
996 {
997 cols = (INTEGERP (w->right_margin_cols)
998 ? XFASTINT (w->right_margin_cols) : 0);
999 pixels = 0;
1000 }
1001 }
1002
1003 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1004 }
1005
1006
1007 /* Return the pixel height of the display area of window W, not
1008 including mode lines of W, if any. */
1009
1010 INLINE int
1011 window_box_height (struct window *w)
1012 {
1013 struct frame *f = XFRAME (w->frame);
1014 int height = WINDOW_TOTAL_HEIGHT (w);
1015
1016 xassert (height >= 0);
1017
1018 /* Note: the code below that determines the mode-line/header-line
1019 height is essentially the same as that contained in the macro
1020 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1021 the appropriate glyph row has its `mode_line_p' flag set,
1022 and if it doesn't, uses estimate_mode_line_height instead. */
1023
1024 if (WINDOW_WANTS_MODELINE_P (w))
1025 {
1026 struct glyph_row *ml_row
1027 = (w->current_matrix && w->current_matrix->rows
1028 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1029 : 0);
1030 if (ml_row && ml_row->mode_line_p)
1031 height -= ml_row->height;
1032 else
1033 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1034 }
1035
1036 if (WINDOW_WANTS_HEADER_LINE_P (w))
1037 {
1038 struct glyph_row *hl_row
1039 = (w->current_matrix && w->current_matrix->rows
1040 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1041 : 0);
1042 if (hl_row && hl_row->mode_line_p)
1043 height -= hl_row->height;
1044 else
1045 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1046 }
1047
1048 /* With a very small font and a mode-line that's taller than
1049 default, we might end up with a negative height. */
1050 return max (0, height);
1051 }
1052
1053 /* Return the window-relative coordinate of the left edge of display
1054 area AREA of window W. AREA < 0 means return the left edge of the
1055 whole window, to the right of the left fringe of W. */
1056
1057 INLINE int
1058 window_box_left_offset (struct window *w, int area)
1059 {
1060 int x;
1061
1062 if (w->pseudo_window_p)
1063 return 0;
1064
1065 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1066
1067 if (area == TEXT_AREA)
1068 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1069 + window_box_width (w, LEFT_MARGIN_AREA));
1070 else if (area == RIGHT_MARGIN_AREA)
1071 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1072 + window_box_width (w, LEFT_MARGIN_AREA)
1073 + window_box_width (w, TEXT_AREA)
1074 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1075 ? 0
1076 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1077 else if (area == LEFT_MARGIN_AREA
1078 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1079 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1080
1081 return x;
1082 }
1083
1084
1085 /* Return the window-relative coordinate of the right edge of display
1086 area AREA of window W. AREA < 0 means return the right edge of the
1087 whole window, to the left of the right fringe of W. */
1088
1089 INLINE int
1090 window_box_right_offset (struct window *w, int area)
1091 {
1092 return window_box_left_offset (w, area) + window_box_width (w, area);
1093 }
1094
1095 /* Return the frame-relative coordinate of the left edge of display
1096 area AREA of window W. AREA < 0 means return the left edge of the
1097 whole window, to the right of the left fringe of W. */
1098
1099 INLINE int
1100 window_box_left (struct window *w, int area)
1101 {
1102 struct frame *f = XFRAME (w->frame);
1103 int x;
1104
1105 if (w->pseudo_window_p)
1106 return FRAME_INTERNAL_BORDER_WIDTH (f);
1107
1108 x = (WINDOW_LEFT_EDGE_X (w)
1109 + window_box_left_offset (w, area));
1110
1111 return x;
1112 }
1113
1114
1115 /* Return the frame-relative coordinate of the right edge of display
1116 area AREA of window W. AREA < 0 means return the right edge of the
1117 whole window, to the left of the right fringe of W. */
1118
1119 INLINE int
1120 window_box_right (struct window *w, int area)
1121 {
1122 return window_box_left (w, area) + window_box_width (w, area);
1123 }
1124
1125 /* Get the bounding box of the display area AREA of window W, without
1126 mode lines, in frame-relative coordinates. AREA < 0 means the
1127 whole window, not including the left and right fringes of
1128 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1129 coordinates of the upper-left corner of the box. Return in
1130 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1131
1132 INLINE void
1133 window_box (struct window *w, int area, int *box_x, int *box_y,
1134 int *box_width, int *box_height)
1135 {
1136 if (box_width)
1137 *box_width = window_box_width (w, area);
1138 if (box_height)
1139 *box_height = window_box_height (w);
1140 if (box_x)
1141 *box_x = window_box_left (w, area);
1142 if (box_y)
1143 {
1144 *box_y = WINDOW_TOP_EDGE_Y (w);
1145 if (WINDOW_WANTS_HEADER_LINE_P (w))
1146 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1147 }
1148 }
1149
1150
1151 /* Get the bounding box of the display area AREA of window W, without
1152 mode lines. AREA < 0 means the whole window, not including the
1153 left and right fringe of the window. Return in *TOP_LEFT_X
1154 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1155 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1156 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1157 box. */
1158
1159 static INLINE void
1160 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1161 int *bottom_right_x, int *bottom_right_y)
1162 {
1163 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1164 bottom_right_y);
1165 *bottom_right_x += *top_left_x;
1166 *bottom_right_y += *top_left_y;
1167 }
1168
1169
1170 \f
1171 /***********************************************************************
1172 Utilities
1173 ***********************************************************************/
1174
1175 /* Return the bottom y-position of the line the iterator IT is in.
1176 This can modify IT's settings. */
1177
1178 int
1179 line_bottom_y (struct it *it)
1180 {
1181 int line_height = it->max_ascent + it->max_descent;
1182 int line_top_y = it->current_y;
1183
1184 if (line_height == 0)
1185 {
1186 if (last_height)
1187 line_height = last_height;
1188 else if (IT_CHARPOS (*it) < ZV)
1189 {
1190 move_it_by_lines (it, 1);
1191 line_height = (it->max_ascent || it->max_descent
1192 ? it->max_ascent + it->max_descent
1193 : last_height);
1194 }
1195 else
1196 {
1197 struct glyph_row *row = it->glyph_row;
1198
1199 /* Use the default character height. */
1200 it->glyph_row = NULL;
1201 it->what = IT_CHARACTER;
1202 it->c = ' ';
1203 it->len = 1;
1204 PRODUCE_GLYPHS (it);
1205 line_height = it->ascent + it->descent;
1206 it->glyph_row = row;
1207 }
1208 }
1209
1210 return line_top_y + line_height;
1211 }
1212
1213
1214 /* Return 1 if position CHARPOS is visible in window W.
1215 CHARPOS < 0 means return info about WINDOW_END position.
1216 If visible, set *X and *Y to pixel coordinates of top left corner.
1217 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1218 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1219
1220 int
1221 pos_visible_p (struct window *w, EMACS_INT charpos, int *x, int *y,
1222 int *rtop, int *rbot, int *rowh, int *vpos)
1223 {
1224 struct it it;
1225 void *itdata = bidi_shelve_cache ();
1226 struct text_pos top;
1227 int visible_p = 0;
1228 struct buffer *old_buffer = NULL;
1229
1230 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1231 return visible_p;
1232
1233 if (XBUFFER (w->buffer) != current_buffer)
1234 {
1235 old_buffer = current_buffer;
1236 set_buffer_internal_1 (XBUFFER (w->buffer));
1237 }
1238
1239 SET_TEXT_POS_FROM_MARKER (top, w->start);
1240
1241 /* Compute exact mode line heights. */
1242 if (WINDOW_WANTS_MODELINE_P (w))
1243 current_mode_line_height
1244 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1245 BVAR (current_buffer, mode_line_format));
1246
1247 if (WINDOW_WANTS_HEADER_LINE_P (w))
1248 current_header_line_height
1249 = display_mode_line (w, HEADER_LINE_FACE_ID,
1250 BVAR (current_buffer, header_line_format));
1251
1252 start_display (&it, w, top);
1253 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1254 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1255
1256 if (charpos >= 0
1257 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1258 && IT_CHARPOS (it) >= charpos)
1259 /* When scanning backwards under bidi iteration, move_it_to
1260 stops at or _before_ CHARPOS, because it stops at or to
1261 the _right_ of the character at CHARPOS. */
1262 || (it.bidi_p && it.bidi_it.scan_dir == -1
1263 && IT_CHARPOS (it) <= charpos)))
1264 {
1265 /* We have reached CHARPOS, or passed it. How the call to
1266 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1267 or covered by a display property, move_it_to stops at the end
1268 of the invisible text, to the right of CHARPOS. (ii) If
1269 CHARPOS is in a display vector, move_it_to stops on its last
1270 glyph. */
1271 int top_x = it.current_x;
1272 int top_y = it.current_y;
1273 enum it_method it_method = it.method;
1274 /* Calling line_bottom_y may change it.method, it.position, etc. */
1275 int bottom_y = (last_height = 0, line_bottom_y (&it));
1276 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1277
1278 if (top_y < window_top_y)
1279 visible_p = bottom_y > window_top_y;
1280 else if (top_y < it.last_visible_y)
1281 visible_p = 1;
1282 if (visible_p)
1283 {
1284 if (it_method == GET_FROM_DISPLAY_VECTOR)
1285 {
1286 /* We stopped on the last glyph of a display vector.
1287 Try and recompute. Hack alert! */
1288 if (charpos < 2 || top.charpos >= charpos)
1289 top_x = it.glyph_row->x;
1290 else
1291 {
1292 struct it it2;
1293 start_display (&it2, w, top);
1294 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1295 get_next_display_element (&it2);
1296 PRODUCE_GLYPHS (&it2);
1297 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1298 || it2.current_x > it2.last_visible_x)
1299 top_x = it.glyph_row->x;
1300 else
1301 {
1302 top_x = it2.current_x;
1303 top_y = it2.current_y;
1304 }
1305 }
1306 }
1307
1308 *x = top_x;
1309 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1310 *rtop = max (0, window_top_y - top_y);
1311 *rbot = max (0, bottom_y - it.last_visible_y);
1312 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1313 - max (top_y, window_top_y)));
1314 *vpos = it.vpos;
1315 }
1316 }
1317 else
1318 {
1319 /* We were asked to provide info about WINDOW_END. */
1320 struct it it2;
1321 void *it2data = NULL;
1322
1323 SAVE_IT (it2, it, it2data);
1324 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1325 move_it_by_lines (&it, 1);
1326 if (charpos < IT_CHARPOS (it)
1327 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1328 {
1329 visible_p = 1;
1330 RESTORE_IT (&it2, &it2, it2data);
1331 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1332 *x = it2.current_x;
1333 *y = it2.current_y + it2.max_ascent - it2.ascent;
1334 *rtop = max (0, -it2.current_y);
1335 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1336 - it.last_visible_y));
1337 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1338 it.last_visible_y)
1339 - max (it2.current_y,
1340 WINDOW_HEADER_LINE_HEIGHT (w))));
1341 *vpos = it2.vpos;
1342 }
1343 else
1344 xfree (it2data);
1345 }
1346 bidi_unshelve_cache (itdata);
1347
1348 if (old_buffer)
1349 set_buffer_internal_1 (old_buffer);
1350
1351 current_header_line_height = current_mode_line_height = -1;
1352
1353 if (visible_p && XFASTINT (w->hscroll) > 0)
1354 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1355
1356 #if 0
1357 /* Debugging code. */
1358 if (visible_p)
1359 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1360 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1361 else
1362 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1363 #endif
1364
1365 return visible_p;
1366 }
1367
1368
1369 /* Return the next character from STR. Return in *LEN the length of
1370 the character. This is like STRING_CHAR_AND_LENGTH but never
1371 returns an invalid character. If we find one, we return a `?', but
1372 with the length of the invalid character. */
1373
1374 static INLINE int
1375 string_char_and_length (const unsigned char *str, int *len)
1376 {
1377 int c;
1378
1379 c = STRING_CHAR_AND_LENGTH (str, *len);
1380 if (!CHAR_VALID_P (c, 1))
1381 /* We may not change the length here because other places in Emacs
1382 don't use this function, i.e. they silently accept invalid
1383 characters. */
1384 c = '?';
1385
1386 return c;
1387 }
1388
1389
1390
1391 /* Given a position POS containing a valid character and byte position
1392 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1393
1394 static struct text_pos
1395 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, EMACS_INT nchars)
1396 {
1397 xassert (STRINGP (string) && nchars >= 0);
1398
1399 if (STRING_MULTIBYTE (string))
1400 {
1401 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1402 int len;
1403
1404 while (nchars--)
1405 {
1406 string_char_and_length (p, &len);
1407 p += len;
1408 CHARPOS (pos) += 1;
1409 BYTEPOS (pos) += len;
1410 }
1411 }
1412 else
1413 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1414
1415 return pos;
1416 }
1417
1418
1419 /* Value is the text position, i.e. character and byte position,
1420 for character position CHARPOS in STRING. */
1421
1422 static INLINE struct text_pos
1423 string_pos (EMACS_INT charpos, Lisp_Object string)
1424 {
1425 struct text_pos pos;
1426 xassert (STRINGP (string));
1427 xassert (charpos >= 0);
1428 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1429 return pos;
1430 }
1431
1432
1433 /* Value is a text position, i.e. character and byte position, for
1434 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1435 means recognize multibyte characters. */
1436
1437 static struct text_pos
1438 c_string_pos (EMACS_INT charpos, const char *s, int multibyte_p)
1439 {
1440 struct text_pos pos;
1441
1442 xassert (s != NULL);
1443 xassert (charpos >= 0);
1444
1445 if (multibyte_p)
1446 {
1447 int len;
1448
1449 SET_TEXT_POS (pos, 0, 0);
1450 while (charpos--)
1451 {
1452 string_char_and_length ((const unsigned char *) s, &len);
1453 s += len;
1454 CHARPOS (pos) += 1;
1455 BYTEPOS (pos) += len;
1456 }
1457 }
1458 else
1459 SET_TEXT_POS (pos, charpos, charpos);
1460
1461 return pos;
1462 }
1463
1464
1465 /* Value is the number of characters in C string S. MULTIBYTE_P
1466 non-zero means recognize multibyte characters. */
1467
1468 static EMACS_INT
1469 number_of_chars (const char *s, int multibyte_p)
1470 {
1471 EMACS_INT nchars;
1472
1473 if (multibyte_p)
1474 {
1475 EMACS_INT rest = strlen (s);
1476 int len;
1477 const unsigned char *p = (const unsigned char *) s;
1478
1479 for (nchars = 0; rest > 0; ++nchars)
1480 {
1481 string_char_and_length (p, &len);
1482 rest -= len, p += len;
1483 }
1484 }
1485 else
1486 nchars = strlen (s);
1487
1488 return nchars;
1489 }
1490
1491
1492 /* Compute byte position NEWPOS->bytepos corresponding to
1493 NEWPOS->charpos. POS is a known position in string STRING.
1494 NEWPOS->charpos must be >= POS.charpos. */
1495
1496 static void
1497 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1498 {
1499 xassert (STRINGP (string));
1500 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1501
1502 if (STRING_MULTIBYTE (string))
1503 *newpos = string_pos_nchars_ahead (pos, string,
1504 CHARPOS (*newpos) - CHARPOS (pos));
1505 else
1506 BYTEPOS (*newpos) = CHARPOS (*newpos);
1507 }
1508
1509 /* EXPORT:
1510 Return an estimation of the pixel height of mode or header lines on
1511 frame F. FACE_ID specifies what line's height to estimate. */
1512
1513 int
1514 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1515 {
1516 #ifdef HAVE_WINDOW_SYSTEM
1517 if (FRAME_WINDOW_P (f))
1518 {
1519 int height = FONT_HEIGHT (FRAME_FONT (f));
1520
1521 /* This function is called so early when Emacs starts that the face
1522 cache and mode line face are not yet initialized. */
1523 if (FRAME_FACE_CACHE (f))
1524 {
1525 struct face *face = FACE_FROM_ID (f, face_id);
1526 if (face)
1527 {
1528 if (face->font)
1529 height = FONT_HEIGHT (face->font);
1530 if (face->box_line_width > 0)
1531 height += 2 * face->box_line_width;
1532 }
1533 }
1534
1535 return height;
1536 }
1537 #endif
1538
1539 return 1;
1540 }
1541
1542 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1543 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1544 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1545 not force the value into range. */
1546
1547 void
1548 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1549 int *x, int *y, NativeRectangle *bounds, int noclip)
1550 {
1551
1552 #ifdef HAVE_WINDOW_SYSTEM
1553 if (FRAME_WINDOW_P (f))
1554 {
1555 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1556 even for negative values. */
1557 if (pix_x < 0)
1558 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1559 if (pix_y < 0)
1560 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1561
1562 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1563 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1564
1565 if (bounds)
1566 STORE_NATIVE_RECT (*bounds,
1567 FRAME_COL_TO_PIXEL_X (f, pix_x),
1568 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1569 FRAME_COLUMN_WIDTH (f) - 1,
1570 FRAME_LINE_HEIGHT (f) - 1);
1571
1572 if (!noclip)
1573 {
1574 if (pix_x < 0)
1575 pix_x = 0;
1576 else if (pix_x > FRAME_TOTAL_COLS (f))
1577 pix_x = FRAME_TOTAL_COLS (f);
1578
1579 if (pix_y < 0)
1580 pix_y = 0;
1581 else if (pix_y > FRAME_LINES (f))
1582 pix_y = FRAME_LINES (f);
1583 }
1584 }
1585 #endif
1586
1587 *x = pix_x;
1588 *y = pix_y;
1589 }
1590
1591
1592 /* Find the glyph under window-relative coordinates X/Y in window W.
1593 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1594 strings. Return in *HPOS and *VPOS the row and column number of
1595 the glyph found. Return in *AREA the glyph area containing X.
1596 Value is a pointer to the glyph found or null if X/Y is not on
1597 text, or we can't tell because W's current matrix is not up to
1598 date. */
1599
1600 static
1601 struct glyph *
1602 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1603 int *dx, int *dy, int *area)
1604 {
1605 struct glyph *glyph, *end;
1606 struct glyph_row *row = NULL;
1607 int x0, i;
1608
1609 /* Find row containing Y. Give up if some row is not enabled. */
1610 for (i = 0; i < w->current_matrix->nrows; ++i)
1611 {
1612 row = MATRIX_ROW (w->current_matrix, i);
1613 if (!row->enabled_p)
1614 return NULL;
1615 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1616 break;
1617 }
1618
1619 *vpos = i;
1620 *hpos = 0;
1621
1622 /* Give up if Y is not in the window. */
1623 if (i == w->current_matrix->nrows)
1624 return NULL;
1625
1626 /* Get the glyph area containing X. */
1627 if (w->pseudo_window_p)
1628 {
1629 *area = TEXT_AREA;
1630 x0 = 0;
1631 }
1632 else
1633 {
1634 if (x < window_box_left_offset (w, TEXT_AREA))
1635 {
1636 *area = LEFT_MARGIN_AREA;
1637 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1638 }
1639 else if (x < window_box_right_offset (w, TEXT_AREA))
1640 {
1641 *area = TEXT_AREA;
1642 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1643 }
1644 else
1645 {
1646 *area = RIGHT_MARGIN_AREA;
1647 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1648 }
1649 }
1650
1651 /* Find glyph containing X. */
1652 glyph = row->glyphs[*area];
1653 end = glyph + row->used[*area];
1654 x -= x0;
1655 while (glyph < end && x >= glyph->pixel_width)
1656 {
1657 x -= glyph->pixel_width;
1658 ++glyph;
1659 }
1660
1661 if (glyph == end)
1662 return NULL;
1663
1664 if (dx)
1665 {
1666 *dx = x;
1667 *dy = y - (row->y + row->ascent - glyph->ascent);
1668 }
1669
1670 *hpos = glyph - row->glyphs[*area];
1671 return glyph;
1672 }
1673
1674 /* Convert frame-relative x/y to coordinates relative to window W.
1675 Takes pseudo-windows into account. */
1676
1677 static void
1678 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1679 {
1680 if (w->pseudo_window_p)
1681 {
1682 /* A pseudo-window is always full-width, and starts at the
1683 left edge of the frame, plus a frame border. */
1684 struct frame *f = XFRAME (w->frame);
1685 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1686 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1687 }
1688 else
1689 {
1690 *x -= WINDOW_LEFT_EDGE_X (w);
1691 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1692 }
1693 }
1694
1695 #ifdef HAVE_WINDOW_SYSTEM
1696
1697 /* EXPORT:
1698 Return in RECTS[] at most N clipping rectangles for glyph string S.
1699 Return the number of stored rectangles. */
1700
1701 int
1702 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1703 {
1704 XRectangle r;
1705
1706 if (n <= 0)
1707 return 0;
1708
1709 if (s->row->full_width_p)
1710 {
1711 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1712 r.x = WINDOW_LEFT_EDGE_X (s->w);
1713 r.width = WINDOW_TOTAL_WIDTH (s->w);
1714
1715 /* Unless displaying a mode or menu bar line, which are always
1716 fully visible, clip to the visible part of the row. */
1717 if (s->w->pseudo_window_p)
1718 r.height = s->row->visible_height;
1719 else
1720 r.height = s->height;
1721 }
1722 else
1723 {
1724 /* This is a text line that may be partially visible. */
1725 r.x = window_box_left (s->w, s->area);
1726 r.width = window_box_width (s->w, s->area);
1727 r.height = s->row->visible_height;
1728 }
1729
1730 if (s->clip_head)
1731 if (r.x < s->clip_head->x)
1732 {
1733 if (r.width >= s->clip_head->x - r.x)
1734 r.width -= s->clip_head->x - r.x;
1735 else
1736 r.width = 0;
1737 r.x = s->clip_head->x;
1738 }
1739 if (s->clip_tail)
1740 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1741 {
1742 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1743 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1744 else
1745 r.width = 0;
1746 }
1747
1748 /* If S draws overlapping rows, it's sufficient to use the top and
1749 bottom of the window for clipping because this glyph string
1750 intentionally draws over other lines. */
1751 if (s->for_overlaps)
1752 {
1753 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1754 r.height = window_text_bottom_y (s->w) - r.y;
1755
1756 /* Alas, the above simple strategy does not work for the
1757 environments with anti-aliased text: if the same text is
1758 drawn onto the same place multiple times, it gets thicker.
1759 If the overlap we are processing is for the erased cursor, we
1760 take the intersection with the rectagle of the cursor. */
1761 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1762 {
1763 XRectangle rc, r_save = r;
1764
1765 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1766 rc.y = s->w->phys_cursor.y;
1767 rc.width = s->w->phys_cursor_width;
1768 rc.height = s->w->phys_cursor_height;
1769
1770 x_intersect_rectangles (&r_save, &rc, &r);
1771 }
1772 }
1773 else
1774 {
1775 /* Don't use S->y for clipping because it doesn't take partially
1776 visible lines into account. For example, it can be negative for
1777 partially visible lines at the top of a window. */
1778 if (!s->row->full_width_p
1779 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1780 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1781 else
1782 r.y = max (0, s->row->y);
1783 }
1784
1785 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1786
1787 /* If drawing the cursor, don't let glyph draw outside its
1788 advertised boundaries. Cleartype does this under some circumstances. */
1789 if (s->hl == DRAW_CURSOR)
1790 {
1791 struct glyph *glyph = s->first_glyph;
1792 int height, max_y;
1793
1794 if (s->x > r.x)
1795 {
1796 r.width -= s->x - r.x;
1797 r.x = s->x;
1798 }
1799 r.width = min (r.width, glyph->pixel_width);
1800
1801 /* If r.y is below window bottom, ensure that we still see a cursor. */
1802 height = min (glyph->ascent + glyph->descent,
1803 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1804 max_y = window_text_bottom_y (s->w) - height;
1805 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1806 if (s->ybase - glyph->ascent > max_y)
1807 {
1808 r.y = max_y;
1809 r.height = height;
1810 }
1811 else
1812 {
1813 /* Don't draw cursor glyph taller than our actual glyph. */
1814 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1815 if (height < r.height)
1816 {
1817 max_y = r.y + r.height;
1818 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1819 r.height = min (max_y - r.y, height);
1820 }
1821 }
1822 }
1823
1824 if (s->row->clip)
1825 {
1826 XRectangle r_save = r;
1827
1828 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1829 r.width = 0;
1830 }
1831
1832 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1833 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1834 {
1835 #ifdef CONVERT_FROM_XRECT
1836 CONVERT_FROM_XRECT (r, *rects);
1837 #else
1838 *rects = r;
1839 #endif
1840 return 1;
1841 }
1842 else
1843 {
1844 /* If we are processing overlapping and allowed to return
1845 multiple clipping rectangles, we exclude the row of the glyph
1846 string from the clipping rectangle. This is to avoid drawing
1847 the same text on the environment with anti-aliasing. */
1848 #ifdef CONVERT_FROM_XRECT
1849 XRectangle rs[2];
1850 #else
1851 XRectangle *rs = rects;
1852 #endif
1853 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
1854
1855 if (s->for_overlaps & OVERLAPS_PRED)
1856 {
1857 rs[i] = r;
1858 if (r.y + r.height > row_y)
1859 {
1860 if (r.y < row_y)
1861 rs[i].height = row_y - r.y;
1862 else
1863 rs[i].height = 0;
1864 }
1865 i++;
1866 }
1867 if (s->for_overlaps & OVERLAPS_SUCC)
1868 {
1869 rs[i] = r;
1870 if (r.y < row_y + s->row->visible_height)
1871 {
1872 if (r.y + r.height > row_y + s->row->visible_height)
1873 {
1874 rs[i].y = row_y + s->row->visible_height;
1875 rs[i].height = r.y + r.height - rs[i].y;
1876 }
1877 else
1878 rs[i].height = 0;
1879 }
1880 i++;
1881 }
1882
1883 n = i;
1884 #ifdef CONVERT_FROM_XRECT
1885 for (i = 0; i < n; i++)
1886 CONVERT_FROM_XRECT (rs[i], rects[i]);
1887 #endif
1888 return n;
1889 }
1890 }
1891
1892 /* EXPORT:
1893 Return in *NR the clipping rectangle for glyph string S. */
1894
1895 void
1896 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
1897 {
1898 get_glyph_string_clip_rects (s, nr, 1);
1899 }
1900
1901
1902 /* EXPORT:
1903 Return the position and height of the phys cursor in window W.
1904 Set w->phys_cursor_width to width of phys cursor.
1905 */
1906
1907 void
1908 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
1909 struct glyph *glyph, int *xp, int *yp, int *heightp)
1910 {
1911 struct frame *f = XFRAME (WINDOW_FRAME (w));
1912 int x, y, wd, h, h0, y0;
1913
1914 /* Compute the width of the rectangle to draw. If on a stretch
1915 glyph, and `x-stretch-block-cursor' is nil, don't draw a
1916 rectangle as wide as the glyph, but use a canonical character
1917 width instead. */
1918 wd = glyph->pixel_width - 1;
1919 #if defined(HAVE_NTGUI) || defined(HAVE_NS)
1920 wd++; /* Why? */
1921 #endif
1922
1923 x = w->phys_cursor.x;
1924 if (x < 0)
1925 {
1926 wd += x;
1927 x = 0;
1928 }
1929
1930 if (glyph->type == STRETCH_GLYPH
1931 && !x_stretch_cursor_p)
1932 wd = min (FRAME_COLUMN_WIDTH (f), wd);
1933 w->phys_cursor_width = wd;
1934
1935 y = w->phys_cursor.y + row->ascent - glyph->ascent;
1936
1937 /* If y is below window bottom, ensure that we still see a cursor. */
1938 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
1939
1940 h = max (h0, glyph->ascent + glyph->descent);
1941 h0 = min (h0, glyph->ascent + glyph->descent);
1942
1943 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
1944 if (y < y0)
1945 {
1946 h = max (h - (y0 - y) + 1, h0);
1947 y = y0 - 1;
1948 }
1949 else
1950 {
1951 y0 = window_text_bottom_y (w) - h0;
1952 if (y > y0)
1953 {
1954 h += y - y0;
1955 y = y0;
1956 }
1957 }
1958
1959 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
1960 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
1961 *heightp = h;
1962 }
1963
1964 /*
1965 * Remember which glyph the mouse is over.
1966 */
1967
1968 void
1969 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
1970 {
1971 Lisp_Object window;
1972 struct window *w;
1973 struct glyph_row *r, *gr, *end_row;
1974 enum window_part part;
1975 enum glyph_row_area area;
1976 int x, y, width, height;
1977
1978 /* Try to determine frame pixel position and size of the glyph under
1979 frame pixel coordinates X/Y on frame F. */
1980
1981 if (!f->glyphs_initialized_p
1982 || (window = window_from_coordinates (f, gx, gy, &part, 0),
1983 NILP (window)))
1984 {
1985 width = FRAME_SMALLEST_CHAR_WIDTH (f);
1986 height = FRAME_SMALLEST_FONT_HEIGHT (f);
1987 goto virtual_glyph;
1988 }
1989
1990 w = XWINDOW (window);
1991 width = WINDOW_FRAME_COLUMN_WIDTH (w);
1992 height = WINDOW_FRAME_LINE_HEIGHT (w);
1993
1994 x = window_relative_x_coord (w, part, gx);
1995 y = gy - WINDOW_TOP_EDGE_Y (w);
1996
1997 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
1998 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
1999
2000 if (w->pseudo_window_p)
2001 {
2002 area = TEXT_AREA;
2003 part = ON_MODE_LINE; /* Don't adjust margin. */
2004 goto text_glyph;
2005 }
2006
2007 switch (part)
2008 {
2009 case ON_LEFT_MARGIN:
2010 area = LEFT_MARGIN_AREA;
2011 goto text_glyph;
2012
2013 case ON_RIGHT_MARGIN:
2014 area = RIGHT_MARGIN_AREA;
2015 goto text_glyph;
2016
2017 case ON_HEADER_LINE:
2018 case ON_MODE_LINE:
2019 gr = (part == ON_HEADER_LINE
2020 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2021 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2022 gy = gr->y;
2023 area = TEXT_AREA;
2024 goto text_glyph_row_found;
2025
2026 case ON_TEXT:
2027 area = TEXT_AREA;
2028
2029 text_glyph:
2030 gr = 0; gy = 0;
2031 for (; r <= end_row && r->enabled_p; ++r)
2032 if (r->y + r->height > y)
2033 {
2034 gr = r; gy = r->y;
2035 break;
2036 }
2037
2038 text_glyph_row_found:
2039 if (gr && gy <= y)
2040 {
2041 struct glyph *g = gr->glyphs[area];
2042 struct glyph *end = g + gr->used[area];
2043
2044 height = gr->height;
2045 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2046 if (gx + g->pixel_width > x)
2047 break;
2048
2049 if (g < end)
2050 {
2051 if (g->type == IMAGE_GLYPH)
2052 {
2053 /* Don't remember when mouse is over image, as
2054 image may have hot-spots. */
2055 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2056 return;
2057 }
2058 width = g->pixel_width;
2059 }
2060 else
2061 {
2062 /* Use nominal char spacing at end of line. */
2063 x -= gx;
2064 gx += (x / width) * width;
2065 }
2066
2067 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2068 gx += window_box_left_offset (w, area);
2069 }
2070 else
2071 {
2072 /* Use nominal line height at end of window. */
2073 gx = (x / width) * width;
2074 y -= gy;
2075 gy += (y / height) * height;
2076 }
2077 break;
2078
2079 case ON_LEFT_FRINGE:
2080 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2081 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2082 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2083 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2084 goto row_glyph;
2085
2086 case ON_RIGHT_FRINGE:
2087 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2088 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2089 : window_box_right_offset (w, TEXT_AREA));
2090 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2091 goto row_glyph;
2092
2093 case ON_SCROLL_BAR:
2094 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2095 ? 0
2096 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2097 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2098 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2099 : 0)));
2100 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2101
2102 row_glyph:
2103 gr = 0, gy = 0;
2104 for (; r <= end_row && r->enabled_p; ++r)
2105 if (r->y + r->height > y)
2106 {
2107 gr = r; gy = r->y;
2108 break;
2109 }
2110
2111 if (gr && gy <= y)
2112 height = gr->height;
2113 else
2114 {
2115 /* Use nominal line height at end of window. */
2116 y -= gy;
2117 gy += (y / height) * height;
2118 }
2119 break;
2120
2121 default:
2122 ;
2123 virtual_glyph:
2124 /* If there is no glyph under the mouse, then we divide the screen
2125 into a grid of the smallest glyph in the frame, and use that
2126 as our "glyph". */
2127
2128 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2129 round down even for negative values. */
2130 if (gx < 0)
2131 gx -= width - 1;
2132 if (gy < 0)
2133 gy -= height - 1;
2134
2135 gx = (gx / width) * width;
2136 gy = (gy / height) * height;
2137
2138 goto store_rect;
2139 }
2140
2141 gx += WINDOW_LEFT_EDGE_X (w);
2142 gy += WINDOW_TOP_EDGE_Y (w);
2143
2144 store_rect:
2145 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2146
2147 /* Visible feedback for debugging. */
2148 #if 0
2149 #if HAVE_X_WINDOWS
2150 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2151 f->output_data.x->normal_gc,
2152 gx, gy, width, height);
2153 #endif
2154 #endif
2155 }
2156
2157
2158 #endif /* HAVE_WINDOW_SYSTEM */
2159
2160 \f
2161 /***********************************************************************
2162 Lisp form evaluation
2163 ***********************************************************************/
2164
2165 /* Error handler for safe_eval and safe_call. */
2166
2167 static Lisp_Object
2168 safe_eval_handler (Lisp_Object arg)
2169 {
2170 add_to_log ("Error during redisplay: %S", arg, Qnil);
2171 return Qnil;
2172 }
2173
2174
2175 /* Evaluate SEXPR and return the result, or nil if something went
2176 wrong. Prevent redisplay during the evaluation. */
2177
2178 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2179 Return the result, or nil if something went wrong. Prevent
2180 redisplay during the evaluation. */
2181
2182 Lisp_Object
2183 safe_call (size_t nargs, Lisp_Object *args)
2184 {
2185 Lisp_Object val;
2186
2187 if (inhibit_eval_during_redisplay)
2188 val = Qnil;
2189 else
2190 {
2191 int count = SPECPDL_INDEX ();
2192 struct gcpro gcpro1;
2193
2194 GCPRO1 (args[0]);
2195 gcpro1.nvars = nargs;
2196 specbind (Qinhibit_redisplay, Qt);
2197 /* Use Qt to ensure debugger does not run,
2198 so there is no possibility of wanting to redisplay. */
2199 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2200 safe_eval_handler);
2201 UNGCPRO;
2202 val = unbind_to (count, val);
2203 }
2204
2205 return val;
2206 }
2207
2208
2209 /* Call function FN with one argument ARG.
2210 Return the result, or nil if something went wrong. */
2211
2212 Lisp_Object
2213 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2214 {
2215 Lisp_Object args[2];
2216 args[0] = fn;
2217 args[1] = arg;
2218 return safe_call (2, args);
2219 }
2220
2221 static Lisp_Object Qeval;
2222
2223 Lisp_Object
2224 safe_eval (Lisp_Object sexpr)
2225 {
2226 return safe_call1 (Qeval, sexpr);
2227 }
2228
2229 /* Call function FN with one argument ARG.
2230 Return the result, or nil if something went wrong. */
2231
2232 Lisp_Object
2233 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2234 {
2235 Lisp_Object args[3];
2236 args[0] = fn;
2237 args[1] = arg1;
2238 args[2] = arg2;
2239 return safe_call (3, args);
2240 }
2241
2242
2243 \f
2244 /***********************************************************************
2245 Debugging
2246 ***********************************************************************/
2247
2248 #if 0
2249
2250 /* Define CHECK_IT to perform sanity checks on iterators.
2251 This is for debugging. It is too slow to do unconditionally. */
2252
2253 static void
2254 check_it (it)
2255 struct it *it;
2256 {
2257 if (it->method == GET_FROM_STRING)
2258 {
2259 xassert (STRINGP (it->string));
2260 xassert (IT_STRING_CHARPOS (*it) >= 0);
2261 }
2262 else
2263 {
2264 xassert (IT_STRING_CHARPOS (*it) < 0);
2265 if (it->method == GET_FROM_BUFFER)
2266 {
2267 /* Check that character and byte positions agree. */
2268 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2269 }
2270 }
2271
2272 if (it->dpvec)
2273 xassert (it->current.dpvec_index >= 0);
2274 else
2275 xassert (it->current.dpvec_index < 0);
2276 }
2277
2278 #define CHECK_IT(IT) check_it ((IT))
2279
2280 #else /* not 0 */
2281
2282 #define CHECK_IT(IT) (void) 0
2283
2284 #endif /* not 0 */
2285
2286
2287 #if GLYPH_DEBUG
2288
2289 /* Check that the window end of window W is what we expect it
2290 to be---the last row in the current matrix displaying text. */
2291
2292 static void
2293 check_window_end (w)
2294 struct window *w;
2295 {
2296 if (!MINI_WINDOW_P (w)
2297 && !NILP (w->window_end_valid))
2298 {
2299 struct glyph_row *row;
2300 xassert ((row = MATRIX_ROW (w->current_matrix,
2301 XFASTINT (w->window_end_vpos)),
2302 !row->enabled_p
2303 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2304 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2305 }
2306 }
2307
2308 #define CHECK_WINDOW_END(W) check_window_end ((W))
2309
2310 #else /* not GLYPH_DEBUG */
2311
2312 #define CHECK_WINDOW_END(W) (void) 0
2313
2314 #endif /* not GLYPH_DEBUG */
2315
2316
2317 \f
2318 /***********************************************************************
2319 Iterator initialization
2320 ***********************************************************************/
2321
2322 /* Initialize IT for displaying current_buffer in window W, starting
2323 at character position CHARPOS. CHARPOS < 0 means that no buffer
2324 position is specified which is useful when the iterator is assigned
2325 a position later. BYTEPOS is the byte position corresponding to
2326 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2327
2328 If ROW is not null, calls to produce_glyphs with IT as parameter
2329 will produce glyphs in that row.
2330
2331 BASE_FACE_ID is the id of a base face to use. It must be one of
2332 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2333 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2334 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2335
2336 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2337 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2338 will be initialized to use the corresponding mode line glyph row of
2339 the desired matrix of W. */
2340
2341 void
2342 init_iterator (struct it *it, struct window *w,
2343 EMACS_INT charpos, EMACS_INT bytepos,
2344 struct glyph_row *row, enum face_id base_face_id)
2345 {
2346 int highlight_region_p;
2347 enum face_id remapped_base_face_id = base_face_id;
2348
2349 /* Some precondition checks. */
2350 xassert (w != NULL && it != NULL);
2351 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2352 && charpos <= ZV));
2353
2354 /* If face attributes have been changed since the last redisplay,
2355 free realized faces now because they depend on face definitions
2356 that might have changed. Don't free faces while there might be
2357 desired matrices pending which reference these faces. */
2358 if (face_change_count && !inhibit_free_realized_faces)
2359 {
2360 face_change_count = 0;
2361 free_all_realized_faces (Qnil);
2362 }
2363
2364 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2365 if (! NILP (Vface_remapping_alist))
2366 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2367
2368 /* Use one of the mode line rows of W's desired matrix if
2369 appropriate. */
2370 if (row == NULL)
2371 {
2372 if (base_face_id == MODE_LINE_FACE_ID
2373 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2374 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2375 else if (base_face_id == HEADER_LINE_FACE_ID)
2376 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2377 }
2378
2379 /* Clear IT. */
2380 memset (it, 0, sizeof *it);
2381 it->current.overlay_string_index = -1;
2382 it->current.dpvec_index = -1;
2383 it->base_face_id = remapped_base_face_id;
2384 it->string = Qnil;
2385 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2386 it->paragraph_embedding = L2R;
2387 it->bidi_it.string.lstring = Qnil;
2388 it->bidi_it.string.s = NULL;
2389 it->bidi_it.string.bufpos = 0;
2390
2391 /* The window in which we iterate over current_buffer: */
2392 XSETWINDOW (it->window, w);
2393 it->w = w;
2394 it->f = XFRAME (w->frame);
2395
2396 it->cmp_it.id = -1;
2397
2398 /* Extra space between lines (on window systems only). */
2399 if (base_face_id == DEFAULT_FACE_ID
2400 && FRAME_WINDOW_P (it->f))
2401 {
2402 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2403 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2404 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2405 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2406 * FRAME_LINE_HEIGHT (it->f));
2407 else if (it->f->extra_line_spacing > 0)
2408 it->extra_line_spacing = it->f->extra_line_spacing;
2409 it->max_extra_line_spacing = 0;
2410 }
2411
2412 /* If realized faces have been removed, e.g. because of face
2413 attribute changes of named faces, recompute them. When running
2414 in batch mode, the face cache of the initial frame is null. If
2415 we happen to get called, make a dummy face cache. */
2416 if (FRAME_FACE_CACHE (it->f) == NULL)
2417 init_frame_faces (it->f);
2418 if (FRAME_FACE_CACHE (it->f)->used == 0)
2419 recompute_basic_faces (it->f);
2420
2421 /* Current value of the `slice', `space-width', and 'height' properties. */
2422 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2423 it->space_width = Qnil;
2424 it->font_height = Qnil;
2425 it->override_ascent = -1;
2426
2427 /* Are control characters displayed as `^C'? */
2428 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2429
2430 /* -1 means everything between a CR and the following line end
2431 is invisible. >0 means lines indented more than this value are
2432 invisible. */
2433 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2434 ? XFASTINT (BVAR (current_buffer, selective_display))
2435 : (!NILP (BVAR (current_buffer, selective_display))
2436 ? -1 : 0));
2437 it->selective_display_ellipsis_p
2438 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2439
2440 /* Display table to use. */
2441 it->dp = window_display_table (w);
2442
2443 /* Are multibyte characters enabled in current_buffer? */
2444 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2445
2446 /* Non-zero if we should highlight the region. */
2447 highlight_region_p
2448 = (!NILP (Vtransient_mark_mode)
2449 && !NILP (BVAR (current_buffer, mark_active))
2450 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2451
2452 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2453 start and end of a visible region in window IT->w. Set both to
2454 -1 to indicate no region. */
2455 if (highlight_region_p
2456 /* Maybe highlight only in selected window. */
2457 && (/* Either show region everywhere. */
2458 highlight_nonselected_windows
2459 /* Or show region in the selected window. */
2460 || w == XWINDOW (selected_window)
2461 /* Or show the region if we are in the mini-buffer and W is
2462 the window the mini-buffer refers to. */
2463 || (MINI_WINDOW_P (XWINDOW (selected_window))
2464 && WINDOWP (minibuf_selected_window)
2465 && w == XWINDOW (minibuf_selected_window))))
2466 {
2467 EMACS_INT markpos = marker_position (BVAR (current_buffer, mark));
2468 it->region_beg_charpos = min (PT, markpos);
2469 it->region_end_charpos = max (PT, markpos);
2470 }
2471 else
2472 it->region_beg_charpos = it->region_end_charpos = -1;
2473
2474 /* Get the position at which the redisplay_end_trigger hook should
2475 be run, if it is to be run at all. */
2476 if (MARKERP (w->redisplay_end_trigger)
2477 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2478 it->redisplay_end_trigger_charpos
2479 = marker_position (w->redisplay_end_trigger);
2480 else if (INTEGERP (w->redisplay_end_trigger))
2481 it->redisplay_end_trigger_charpos = XINT (w->redisplay_end_trigger);
2482
2483 /* Correct bogus values of tab_width. */
2484 it->tab_width = XINT (BVAR (current_buffer, tab_width));
2485 if (it->tab_width <= 0 || it->tab_width > 1000)
2486 it->tab_width = 8;
2487
2488 /* Are lines in the display truncated? */
2489 if (base_face_id != DEFAULT_FACE_ID
2490 || XINT (it->w->hscroll)
2491 || (! WINDOW_FULL_WIDTH_P (it->w)
2492 && ((!NILP (Vtruncate_partial_width_windows)
2493 && !INTEGERP (Vtruncate_partial_width_windows))
2494 || (INTEGERP (Vtruncate_partial_width_windows)
2495 && (WINDOW_TOTAL_COLS (it->w)
2496 < XINT (Vtruncate_partial_width_windows))))))
2497 it->line_wrap = TRUNCATE;
2498 else if (NILP (BVAR (current_buffer, truncate_lines)))
2499 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2500 ? WINDOW_WRAP : WORD_WRAP;
2501 else
2502 it->line_wrap = TRUNCATE;
2503
2504 /* Get dimensions of truncation and continuation glyphs. These are
2505 displayed as fringe bitmaps under X, so we don't need them for such
2506 frames. */
2507 if (!FRAME_WINDOW_P (it->f))
2508 {
2509 if (it->line_wrap == TRUNCATE)
2510 {
2511 /* We will need the truncation glyph. */
2512 xassert (it->glyph_row == NULL);
2513 produce_special_glyphs (it, IT_TRUNCATION);
2514 it->truncation_pixel_width = it->pixel_width;
2515 }
2516 else
2517 {
2518 /* We will need the continuation glyph. */
2519 xassert (it->glyph_row == NULL);
2520 produce_special_glyphs (it, IT_CONTINUATION);
2521 it->continuation_pixel_width = it->pixel_width;
2522 }
2523
2524 /* Reset these values to zero because the produce_special_glyphs
2525 above has changed them. */
2526 it->pixel_width = it->ascent = it->descent = 0;
2527 it->phys_ascent = it->phys_descent = 0;
2528 }
2529
2530 /* Set this after getting the dimensions of truncation and
2531 continuation glyphs, so that we don't produce glyphs when calling
2532 produce_special_glyphs, above. */
2533 it->glyph_row = row;
2534 it->area = TEXT_AREA;
2535
2536 /* Forget any previous info about this row being reversed. */
2537 if (it->glyph_row)
2538 it->glyph_row->reversed_p = 0;
2539
2540 /* Get the dimensions of the display area. The display area
2541 consists of the visible window area plus a horizontally scrolled
2542 part to the left of the window. All x-values are relative to the
2543 start of this total display area. */
2544 if (base_face_id != DEFAULT_FACE_ID)
2545 {
2546 /* Mode lines, menu bar in terminal frames. */
2547 it->first_visible_x = 0;
2548 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2549 }
2550 else
2551 {
2552 it->first_visible_x
2553 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2554 it->last_visible_x = (it->first_visible_x
2555 + window_box_width (w, TEXT_AREA));
2556
2557 /* If we truncate lines, leave room for the truncator glyph(s) at
2558 the right margin. Otherwise, leave room for the continuation
2559 glyph(s). Truncation and continuation glyphs are not inserted
2560 for window-based redisplay. */
2561 if (!FRAME_WINDOW_P (it->f))
2562 {
2563 if (it->line_wrap == TRUNCATE)
2564 it->last_visible_x -= it->truncation_pixel_width;
2565 else
2566 it->last_visible_x -= it->continuation_pixel_width;
2567 }
2568
2569 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2570 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2571 }
2572
2573 /* Leave room for a border glyph. */
2574 if (!FRAME_WINDOW_P (it->f)
2575 && !WINDOW_RIGHTMOST_P (it->w))
2576 it->last_visible_x -= 1;
2577
2578 it->last_visible_y = window_text_bottom_y (w);
2579
2580 /* For mode lines and alike, arrange for the first glyph having a
2581 left box line if the face specifies a box. */
2582 if (base_face_id != DEFAULT_FACE_ID)
2583 {
2584 struct face *face;
2585
2586 it->face_id = remapped_base_face_id;
2587
2588 /* If we have a boxed mode line, make the first character appear
2589 with a left box line. */
2590 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2591 if (face->box != FACE_NO_BOX)
2592 it->start_of_box_run_p = 1;
2593 }
2594
2595 /* If a buffer position was specified, set the iterator there,
2596 getting overlays and face properties from that position. */
2597 if (charpos >= BUF_BEG (current_buffer))
2598 {
2599 it->end_charpos = ZV;
2600 it->face_id = -1;
2601 IT_CHARPOS (*it) = charpos;
2602
2603 /* Compute byte position if not specified. */
2604 if (bytepos < charpos)
2605 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2606 else
2607 IT_BYTEPOS (*it) = bytepos;
2608
2609 it->start = it->current;
2610 /* Do we need to reorder bidirectional text? Not if this is a
2611 unibyte buffer: by definition, none of the single-byte
2612 characters are strong R2L, so no reordering is needed. And
2613 bidi.c doesn't support unibyte buffers anyway. */
2614 it->bidi_p =
2615 !NILP (BVAR (current_buffer, bidi_display_reordering))
2616 && it->multibyte_p;
2617
2618 /* If we are to reorder bidirectional text, init the bidi
2619 iterator. */
2620 if (it->bidi_p)
2621 {
2622 /* Note the paragraph direction that this buffer wants to
2623 use. */
2624 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2625 Qleft_to_right))
2626 it->paragraph_embedding = L2R;
2627 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2628 Qright_to_left))
2629 it->paragraph_embedding = R2L;
2630 else
2631 it->paragraph_embedding = NEUTRAL_DIR;
2632 bidi_unshelve_cache (NULL);
2633 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2634 &it->bidi_it);
2635 }
2636
2637 /* Compute faces etc. */
2638 reseat (it, it->current.pos, 1);
2639 }
2640
2641 CHECK_IT (it);
2642 }
2643
2644
2645 /* Initialize IT for the display of window W with window start POS. */
2646
2647 void
2648 start_display (struct it *it, struct window *w, struct text_pos pos)
2649 {
2650 struct glyph_row *row;
2651 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2652
2653 row = w->desired_matrix->rows + first_vpos;
2654 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2655 it->first_vpos = first_vpos;
2656
2657 /* Don't reseat to previous visible line start if current start
2658 position is in a string or image. */
2659 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2660 {
2661 int start_at_line_beg_p;
2662 int first_y = it->current_y;
2663
2664 /* If window start is not at a line start, skip forward to POS to
2665 get the correct continuation lines width. */
2666 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2667 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2668 if (!start_at_line_beg_p)
2669 {
2670 int new_x;
2671
2672 reseat_at_previous_visible_line_start (it);
2673 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2674
2675 new_x = it->current_x + it->pixel_width;
2676
2677 /* If lines are continued, this line may end in the middle
2678 of a multi-glyph character (e.g. a control character
2679 displayed as \003, or in the middle of an overlay
2680 string). In this case move_it_to above will not have
2681 taken us to the start of the continuation line but to the
2682 end of the continued line. */
2683 if (it->current_x > 0
2684 && it->line_wrap != TRUNCATE /* Lines are continued. */
2685 && (/* And glyph doesn't fit on the line. */
2686 new_x > it->last_visible_x
2687 /* Or it fits exactly and we're on a window
2688 system frame. */
2689 || (new_x == it->last_visible_x
2690 && FRAME_WINDOW_P (it->f))))
2691 {
2692 if (it->current.dpvec_index >= 0
2693 || it->current.overlay_string_index >= 0)
2694 {
2695 set_iterator_to_next (it, 1);
2696 move_it_in_display_line_to (it, -1, -1, 0);
2697 }
2698
2699 it->continuation_lines_width += it->current_x;
2700 }
2701
2702 /* We're starting a new display line, not affected by the
2703 height of the continued line, so clear the appropriate
2704 fields in the iterator structure. */
2705 it->max_ascent = it->max_descent = 0;
2706 it->max_phys_ascent = it->max_phys_descent = 0;
2707
2708 it->current_y = first_y;
2709 it->vpos = 0;
2710 it->current_x = it->hpos = 0;
2711 }
2712 }
2713 }
2714
2715
2716 /* Return 1 if POS is a position in ellipses displayed for invisible
2717 text. W is the window we display, for text property lookup. */
2718
2719 static int
2720 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2721 {
2722 Lisp_Object prop, window;
2723 int ellipses_p = 0;
2724 EMACS_INT charpos = CHARPOS (pos->pos);
2725
2726 /* If POS specifies a position in a display vector, this might
2727 be for an ellipsis displayed for invisible text. We won't
2728 get the iterator set up for delivering that ellipsis unless
2729 we make sure that it gets aware of the invisible text. */
2730 if (pos->dpvec_index >= 0
2731 && pos->overlay_string_index < 0
2732 && CHARPOS (pos->string_pos) < 0
2733 && charpos > BEGV
2734 && (XSETWINDOW (window, w),
2735 prop = Fget_char_property (make_number (charpos),
2736 Qinvisible, window),
2737 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2738 {
2739 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2740 window);
2741 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2742 }
2743
2744 return ellipses_p;
2745 }
2746
2747
2748 /* Initialize IT for stepping through current_buffer in window W,
2749 starting at position POS that includes overlay string and display
2750 vector/ control character translation position information. Value
2751 is zero if there are overlay strings with newlines at POS. */
2752
2753 static int
2754 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2755 {
2756 EMACS_INT charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2757 int i, overlay_strings_with_newlines = 0;
2758
2759 /* If POS specifies a position in a display vector, this might
2760 be for an ellipsis displayed for invisible text. We won't
2761 get the iterator set up for delivering that ellipsis unless
2762 we make sure that it gets aware of the invisible text. */
2763 if (in_ellipses_for_invisible_text_p (pos, w))
2764 {
2765 --charpos;
2766 bytepos = 0;
2767 }
2768
2769 /* Keep in mind: the call to reseat in init_iterator skips invisible
2770 text, so we might end up at a position different from POS. This
2771 is only a problem when POS is a row start after a newline and an
2772 overlay starts there with an after-string, and the overlay has an
2773 invisible property. Since we don't skip invisible text in
2774 display_line and elsewhere immediately after consuming the
2775 newline before the row start, such a POS will not be in a string,
2776 but the call to init_iterator below will move us to the
2777 after-string. */
2778 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2779
2780 /* This only scans the current chunk -- it should scan all chunks.
2781 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2782 to 16 in 22.1 to make this a lesser problem. */
2783 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2784 {
2785 const char *s = SSDATA (it->overlay_strings[i]);
2786 const char *e = s + SBYTES (it->overlay_strings[i]);
2787
2788 while (s < e && *s != '\n')
2789 ++s;
2790
2791 if (s < e)
2792 {
2793 overlay_strings_with_newlines = 1;
2794 break;
2795 }
2796 }
2797
2798 /* If position is within an overlay string, set up IT to the right
2799 overlay string. */
2800 if (pos->overlay_string_index >= 0)
2801 {
2802 int relative_index;
2803
2804 /* If the first overlay string happens to have a `display'
2805 property for an image, the iterator will be set up for that
2806 image, and we have to undo that setup first before we can
2807 correct the overlay string index. */
2808 if (it->method == GET_FROM_IMAGE)
2809 pop_it (it);
2810
2811 /* We already have the first chunk of overlay strings in
2812 IT->overlay_strings. Load more until the one for
2813 pos->overlay_string_index is in IT->overlay_strings. */
2814 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2815 {
2816 int n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2817 it->current.overlay_string_index = 0;
2818 while (n--)
2819 {
2820 load_overlay_strings (it, 0);
2821 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2822 }
2823 }
2824
2825 it->current.overlay_string_index = pos->overlay_string_index;
2826 relative_index = (it->current.overlay_string_index
2827 % OVERLAY_STRING_CHUNK_SIZE);
2828 it->string = it->overlay_strings[relative_index];
2829 xassert (STRINGP (it->string));
2830 it->current.string_pos = pos->string_pos;
2831 it->method = GET_FROM_STRING;
2832 }
2833
2834 if (CHARPOS (pos->string_pos) >= 0)
2835 {
2836 /* Recorded position is not in an overlay string, but in another
2837 string. This can only be a string from a `display' property.
2838 IT should already be filled with that string. */
2839 it->current.string_pos = pos->string_pos;
2840 xassert (STRINGP (it->string));
2841 }
2842
2843 /* Restore position in display vector translations, control
2844 character translations or ellipses. */
2845 if (pos->dpvec_index >= 0)
2846 {
2847 if (it->dpvec == NULL)
2848 get_next_display_element (it);
2849 xassert (it->dpvec && it->current.dpvec_index == 0);
2850 it->current.dpvec_index = pos->dpvec_index;
2851 }
2852
2853 CHECK_IT (it);
2854 return !overlay_strings_with_newlines;
2855 }
2856
2857
2858 /* Initialize IT for stepping through current_buffer in window W
2859 starting at ROW->start. */
2860
2861 static void
2862 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
2863 {
2864 init_from_display_pos (it, w, &row->start);
2865 it->start = row->start;
2866 it->continuation_lines_width = row->continuation_lines_width;
2867 CHECK_IT (it);
2868 }
2869
2870
2871 /* Initialize IT for stepping through current_buffer in window W
2872 starting in the line following ROW, i.e. starting at ROW->end.
2873 Value is zero if there are overlay strings with newlines at ROW's
2874 end position. */
2875
2876 static int
2877 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
2878 {
2879 int success = 0;
2880
2881 if (init_from_display_pos (it, w, &row->end))
2882 {
2883 if (row->continued_p)
2884 it->continuation_lines_width
2885 = row->continuation_lines_width + row->pixel_width;
2886 CHECK_IT (it);
2887 success = 1;
2888 }
2889
2890 return success;
2891 }
2892
2893
2894
2895 \f
2896 /***********************************************************************
2897 Text properties
2898 ***********************************************************************/
2899
2900 /* Called when IT reaches IT->stop_charpos. Handle text property and
2901 overlay changes. Set IT->stop_charpos to the next position where
2902 to stop. */
2903
2904 static void
2905 handle_stop (struct it *it)
2906 {
2907 enum prop_handled handled;
2908 int handle_overlay_change_p;
2909 struct props *p;
2910
2911 it->dpvec = NULL;
2912 it->current.dpvec_index = -1;
2913 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
2914 it->ignore_overlay_strings_at_pos_p = 0;
2915 it->ellipsis_p = 0;
2916
2917 /* Use face of preceding text for ellipsis (if invisible) */
2918 if (it->selective_display_ellipsis_p)
2919 it->saved_face_id = it->face_id;
2920
2921 do
2922 {
2923 handled = HANDLED_NORMALLY;
2924
2925 /* Call text property handlers. */
2926 for (p = it_props; p->handler; ++p)
2927 {
2928 handled = p->handler (it);
2929
2930 if (handled == HANDLED_RECOMPUTE_PROPS)
2931 break;
2932 else if (handled == HANDLED_RETURN)
2933 {
2934 /* We still want to show before and after strings from
2935 overlays even if the actual buffer text is replaced. */
2936 if (!handle_overlay_change_p
2937 || it->sp > 1
2938 || !get_overlay_strings_1 (it, 0, 0))
2939 {
2940 if (it->ellipsis_p)
2941 setup_for_ellipsis (it, 0);
2942 /* When handling a display spec, we might load an
2943 empty string. In that case, discard it here. We
2944 used to discard it in handle_single_display_spec,
2945 but that causes get_overlay_strings_1, above, to
2946 ignore overlay strings that we must check. */
2947 if (STRINGP (it->string) && !SCHARS (it->string))
2948 pop_it (it);
2949 return;
2950 }
2951 else if (STRINGP (it->string) && !SCHARS (it->string))
2952 pop_it (it);
2953 else
2954 {
2955 it->ignore_overlay_strings_at_pos_p = 1;
2956 it->string_from_display_prop_p = 0;
2957 it->from_disp_prop_p = 0;
2958 handle_overlay_change_p = 0;
2959 }
2960 handled = HANDLED_RECOMPUTE_PROPS;
2961 break;
2962 }
2963 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
2964 handle_overlay_change_p = 0;
2965 }
2966
2967 if (handled != HANDLED_RECOMPUTE_PROPS)
2968 {
2969 /* Don't check for overlay strings below when set to deliver
2970 characters from a display vector. */
2971 if (it->method == GET_FROM_DISPLAY_VECTOR)
2972 handle_overlay_change_p = 0;
2973
2974 /* Handle overlay changes.
2975 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
2976 if it finds overlays. */
2977 if (handle_overlay_change_p)
2978 handled = handle_overlay_change (it);
2979 }
2980
2981 if (it->ellipsis_p)
2982 {
2983 setup_for_ellipsis (it, 0);
2984 break;
2985 }
2986 }
2987 while (handled == HANDLED_RECOMPUTE_PROPS);
2988
2989 /* Determine where to stop next. */
2990 if (handled == HANDLED_NORMALLY)
2991 compute_stop_pos (it);
2992 }
2993
2994
2995 /* Compute IT->stop_charpos from text property and overlay change
2996 information for IT's current position. */
2997
2998 static void
2999 compute_stop_pos (struct it *it)
3000 {
3001 register INTERVAL iv, next_iv;
3002 Lisp_Object object, limit, position;
3003 EMACS_INT charpos, bytepos;
3004
3005 /* If nowhere else, stop at the end. */
3006 it->stop_charpos = it->end_charpos;
3007
3008 if (STRINGP (it->string))
3009 {
3010 /* Strings are usually short, so don't limit the search for
3011 properties. */
3012 object = it->string;
3013 limit = Qnil;
3014 charpos = IT_STRING_CHARPOS (*it);
3015 bytepos = IT_STRING_BYTEPOS (*it);
3016 }
3017 else
3018 {
3019 EMACS_INT pos;
3020
3021 /* If next overlay change is in front of the current stop pos
3022 (which is IT->end_charpos), stop there. Note: value of
3023 next_overlay_change is point-max if no overlay change
3024 follows. */
3025 charpos = IT_CHARPOS (*it);
3026 bytepos = IT_BYTEPOS (*it);
3027 pos = next_overlay_change (charpos);
3028 if (pos < it->stop_charpos)
3029 it->stop_charpos = pos;
3030
3031 /* If showing the region, we have to stop at the region
3032 start or end because the face might change there. */
3033 if (it->region_beg_charpos > 0)
3034 {
3035 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3036 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3037 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3038 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3039 }
3040
3041 /* Set up variables for computing the stop position from text
3042 property changes. */
3043 XSETBUFFER (object, current_buffer);
3044 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3045 }
3046
3047 /* Get the interval containing IT's position. Value is a null
3048 interval if there isn't such an interval. */
3049 position = make_number (charpos);
3050 iv = validate_interval_range (object, &position, &position, 0);
3051 if (!NULL_INTERVAL_P (iv))
3052 {
3053 Lisp_Object values_here[LAST_PROP_IDX];
3054 struct props *p;
3055
3056 /* Get properties here. */
3057 for (p = it_props; p->handler; ++p)
3058 values_here[p->idx] = textget (iv->plist, *p->name);
3059
3060 /* Look for an interval following iv that has different
3061 properties. */
3062 for (next_iv = next_interval (iv);
3063 (!NULL_INTERVAL_P (next_iv)
3064 && (NILP (limit)
3065 || XFASTINT (limit) > next_iv->position));
3066 next_iv = next_interval (next_iv))
3067 {
3068 for (p = it_props; p->handler; ++p)
3069 {
3070 Lisp_Object new_value;
3071
3072 new_value = textget (next_iv->plist, *p->name);
3073 if (!EQ (values_here[p->idx], new_value))
3074 break;
3075 }
3076
3077 if (p->handler)
3078 break;
3079 }
3080
3081 if (!NULL_INTERVAL_P (next_iv))
3082 {
3083 if (INTEGERP (limit)
3084 && next_iv->position >= XFASTINT (limit))
3085 /* No text property change up to limit. */
3086 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3087 else
3088 /* Text properties change in next_iv. */
3089 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3090 }
3091 }
3092
3093 if (it->cmp_it.id < 0)
3094 {
3095 EMACS_INT stoppos = it->end_charpos;
3096
3097 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3098 stoppos = -1;
3099 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3100 stoppos, it->string);
3101 }
3102
3103 xassert (STRINGP (it->string)
3104 || (it->stop_charpos >= BEGV
3105 && it->stop_charpos >= IT_CHARPOS (*it)));
3106 }
3107
3108
3109 /* Return the position of the next overlay change after POS in
3110 current_buffer. Value is point-max if no overlay change
3111 follows. This is like `next-overlay-change' but doesn't use
3112 xmalloc. */
3113
3114 static EMACS_INT
3115 next_overlay_change (EMACS_INT pos)
3116 {
3117 int noverlays;
3118 EMACS_INT endpos;
3119 Lisp_Object *overlays;
3120 int i;
3121
3122 /* Get all overlays at the given position. */
3123 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3124
3125 /* If any of these overlays ends before endpos,
3126 use its ending point instead. */
3127 for (i = 0; i < noverlays; ++i)
3128 {
3129 Lisp_Object oend;
3130 EMACS_INT oendpos;
3131
3132 oend = OVERLAY_END (overlays[i]);
3133 oendpos = OVERLAY_POSITION (oend);
3134 endpos = min (endpos, oendpos);
3135 }
3136
3137 return endpos;
3138 }
3139
3140 /* Return the character position of a display string at or after
3141 position specified by POSITION. If no display string exists at or
3142 after POSITION, return ZV. A display string is either an overlay
3143 with `display' property whose value is a string, or a `display'
3144 text property whose value is a string. STRING is data about the
3145 string to iterate; if STRING->lstring is nil, we are iterating a
3146 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3147 on a GUI frame. */
3148 EMACS_INT
3149 compute_display_string_pos (struct text_pos *position,
3150 struct bidi_string_data *string, int frame_window_p)
3151 {
3152 /* OBJECT = nil means current buffer. */
3153 Lisp_Object object =
3154 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3155 Lisp_Object pos, spec;
3156 int string_p = (string && (STRINGP (string->lstring) || string->s));
3157 EMACS_INT eob = string_p ? string->schars : ZV;
3158 EMACS_INT begb = string_p ? 0 : BEGV;
3159 EMACS_INT bufpos, charpos = CHARPOS (*position);
3160 struct text_pos tpos;
3161
3162 if (charpos >= eob
3163 /* We don't support display properties whose values are strings
3164 that have display string properties. */
3165 || string->from_disp_str
3166 /* C strings cannot have display properties. */
3167 || (string->s && !STRINGP (object)))
3168 return eob;
3169
3170 /* If the character at CHARPOS is where the display string begins,
3171 return CHARPOS. */
3172 pos = make_number (charpos);
3173 if (STRINGP (object))
3174 bufpos = string->bufpos;
3175 else
3176 bufpos = charpos;
3177 tpos = *position;
3178 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3179 && (charpos <= begb
3180 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3181 object),
3182 spec))
3183 && handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3184 frame_window_p))
3185 return charpos;
3186
3187 /* Look forward for the first character with a `display' property
3188 that will replace the underlying text when displayed. */
3189 do {
3190 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3191 CHARPOS (tpos) = XFASTINT (pos);
3192 if (STRINGP (object))
3193 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3194 else
3195 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3196 if (CHARPOS (tpos) >= eob)
3197 break;
3198 spec = Fget_char_property (pos, Qdisplay, object);
3199 if (!STRINGP (object))
3200 bufpos = CHARPOS (tpos);
3201 } while (NILP (spec)
3202 || !handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3203 frame_window_p));
3204
3205 return CHARPOS (tpos);
3206 }
3207
3208 /* Return the character position of the end of the display string that
3209 started at CHARPOS. A display string is either an overlay with
3210 `display' property whose value is a string or a `display' text
3211 property whose value is a string. */
3212 EMACS_INT
3213 compute_display_string_end (EMACS_INT charpos, struct bidi_string_data *string)
3214 {
3215 /* OBJECT = nil means current buffer. */
3216 Lisp_Object object =
3217 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3218 Lisp_Object pos = make_number (charpos);
3219 EMACS_INT eob =
3220 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3221
3222 if (charpos >= eob || (string->s && !STRINGP (object)))
3223 return eob;
3224
3225 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3226 abort ();
3227
3228 /* Look forward for the first character where the `display' property
3229 changes. */
3230 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3231
3232 return XFASTINT (pos);
3233 }
3234
3235
3236 \f
3237 /***********************************************************************
3238 Fontification
3239 ***********************************************************************/
3240
3241 /* Handle changes in the `fontified' property of the current buffer by
3242 calling hook functions from Qfontification_functions to fontify
3243 regions of text. */
3244
3245 static enum prop_handled
3246 handle_fontified_prop (struct it *it)
3247 {
3248 Lisp_Object prop, pos;
3249 enum prop_handled handled = HANDLED_NORMALLY;
3250
3251 if (!NILP (Vmemory_full))
3252 return handled;
3253
3254 /* Get the value of the `fontified' property at IT's current buffer
3255 position. (The `fontified' property doesn't have a special
3256 meaning in strings.) If the value is nil, call functions from
3257 Qfontification_functions. */
3258 if (!STRINGP (it->string)
3259 && it->s == NULL
3260 && !NILP (Vfontification_functions)
3261 && !NILP (Vrun_hooks)
3262 && (pos = make_number (IT_CHARPOS (*it)),
3263 prop = Fget_char_property (pos, Qfontified, Qnil),
3264 /* Ignore the special cased nil value always present at EOB since
3265 no amount of fontifying will be able to change it. */
3266 NILP (prop) && IT_CHARPOS (*it) < Z))
3267 {
3268 int count = SPECPDL_INDEX ();
3269 Lisp_Object val;
3270 struct buffer *obuf = current_buffer;
3271 int begv = BEGV, zv = ZV;
3272 int old_clip_changed = current_buffer->clip_changed;
3273
3274 val = Vfontification_functions;
3275 specbind (Qfontification_functions, Qnil);
3276
3277 xassert (it->end_charpos == ZV);
3278
3279 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3280 safe_call1 (val, pos);
3281 else
3282 {
3283 Lisp_Object fns, fn;
3284 struct gcpro gcpro1, gcpro2;
3285
3286 fns = Qnil;
3287 GCPRO2 (val, fns);
3288
3289 for (; CONSP (val); val = XCDR (val))
3290 {
3291 fn = XCAR (val);
3292
3293 if (EQ (fn, Qt))
3294 {
3295 /* A value of t indicates this hook has a local
3296 binding; it means to run the global binding too.
3297 In a global value, t should not occur. If it
3298 does, we must ignore it to avoid an endless
3299 loop. */
3300 for (fns = Fdefault_value (Qfontification_functions);
3301 CONSP (fns);
3302 fns = XCDR (fns))
3303 {
3304 fn = XCAR (fns);
3305 if (!EQ (fn, Qt))
3306 safe_call1 (fn, pos);
3307 }
3308 }
3309 else
3310 safe_call1 (fn, pos);
3311 }
3312
3313 UNGCPRO;
3314 }
3315
3316 unbind_to (count, Qnil);
3317
3318 /* Fontification functions routinely call `save-restriction'.
3319 Normally, this tags clip_changed, which can confuse redisplay
3320 (see discussion in Bug#6671). Since we don't perform any
3321 special handling of fontification changes in the case where
3322 `save-restriction' isn't called, there's no point doing so in
3323 this case either. So, if the buffer's restrictions are
3324 actually left unchanged, reset clip_changed. */
3325 if (obuf == current_buffer)
3326 {
3327 if (begv == BEGV && zv == ZV)
3328 current_buffer->clip_changed = old_clip_changed;
3329 }
3330 /* There isn't much we can reasonably do to protect against
3331 misbehaving fontification, but here's a fig leaf. */
3332 else if (!NILP (BVAR (obuf, name)))
3333 set_buffer_internal_1 (obuf);
3334
3335 /* The fontification code may have added/removed text.
3336 It could do even a lot worse, but let's at least protect against
3337 the most obvious case where only the text past `pos' gets changed',
3338 as is/was done in grep.el where some escapes sequences are turned
3339 into face properties (bug#7876). */
3340 it->end_charpos = ZV;
3341
3342 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3343 something. This avoids an endless loop if they failed to
3344 fontify the text for which reason ever. */
3345 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3346 handled = HANDLED_RECOMPUTE_PROPS;
3347 }
3348
3349 return handled;
3350 }
3351
3352
3353 \f
3354 /***********************************************************************
3355 Faces
3356 ***********************************************************************/
3357
3358 /* Set up iterator IT from face properties at its current position.
3359 Called from handle_stop. */
3360
3361 static enum prop_handled
3362 handle_face_prop (struct it *it)
3363 {
3364 int new_face_id;
3365 EMACS_INT next_stop;
3366
3367 if (!STRINGP (it->string))
3368 {
3369 new_face_id
3370 = face_at_buffer_position (it->w,
3371 IT_CHARPOS (*it),
3372 it->region_beg_charpos,
3373 it->region_end_charpos,
3374 &next_stop,
3375 (IT_CHARPOS (*it)
3376 + TEXT_PROP_DISTANCE_LIMIT),
3377 0, it->base_face_id);
3378
3379 /* Is this a start of a run of characters with box face?
3380 Caveat: this can be called for a freshly initialized
3381 iterator; face_id is -1 in this case. We know that the new
3382 face will not change until limit, i.e. if the new face has a
3383 box, all characters up to limit will have one. But, as
3384 usual, we don't know whether limit is really the end. */
3385 if (new_face_id != it->face_id)
3386 {
3387 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3388
3389 /* If new face has a box but old face has not, this is
3390 the start of a run of characters with box, i.e. it has
3391 a shadow on the left side. The value of face_id of the
3392 iterator will be -1 if this is the initial call that gets
3393 the face. In this case, we have to look in front of IT's
3394 position and see whether there is a face != new_face_id. */
3395 it->start_of_box_run_p
3396 = (new_face->box != FACE_NO_BOX
3397 && (it->face_id >= 0
3398 || IT_CHARPOS (*it) == BEG
3399 || new_face_id != face_before_it_pos (it)));
3400 it->face_box_p = new_face->box != FACE_NO_BOX;
3401 }
3402 }
3403 else
3404 {
3405 int base_face_id;
3406 EMACS_INT bufpos;
3407 int i;
3408 Lisp_Object from_overlay
3409 = (it->current.overlay_string_index >= 0
3410 ? it->string_overlays[it->current.overlay_string_index]
3411 : Qnil);
3412
3413 /* See if we got to this string directly or indirectly from
3414 an overlay property. That includes the before-string or
3415 after-string of an overlay, strings in display properties
3416 provided by an overlay, their text properties, etc.
3417
3418 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3419 if (! NILP (from_overlay))
3420 for (i = it->sp - 1; i >= 0; i--)
3421 {
3422 if (it->stack[i].current.overlay_string_index >= 0)
3423 from_overlay
3424 = it->string_overlays[it->stack[i].current.overlay_string_index];
3425 else if (! NILP (it->stack[i].from_overlay))
3426 from_overlay = it->stack[i].from_overlay;
3427
3428 if (!NILP (from_overlay))
3429 break;
3430 }
3431
3432 if (! NILP (from_overlay))
3433 {
3434 bufpos = IT_CHARPOS (*it);
3435 /* For a string from an overlay, the base face depends
3436 only on text properties and ignores overlays. */
3437 base_face_id
3438 = face_for_overlay_string (it->w,
3439 IT_CHARPOS (*it),
3440 it->region_beg_charpos,
3441 it->region_end_charpos,
3442 &next_stop,
3443 (IT_CHARPOS (*it)
3444 + TEXT_PROP_DISTANCE_LIMIT),
3445 0,
3446 from_overlay);
3447 }
3448 else
3449 {
3450 bufpos = 0;
3451
3452 /* For strings from a `display' property, use the face at
3453 IT's current buffer position as the base face to merge
3454 with, so that overlay strings appear in the same face as
3455 surrounding text, unless they specify their own
3456 faces. */
3457 base_face_id = underlying_face_id (it);
3458 }
3459
3460 new_face_id = face_at_string_position (it->w,
3461 it->string,
3462 IT_STRING_CHARPOS (*it),
3463 bufpos,
3464 it->region_beg_charpos,
3465 it->region_end_charpos,
3466 &next_stop,
3467 base_face_id, 0);
3468
3469 /* Is this a start of a run of characters with box? Caveat:
3470 this can be called for a freshly allocated iterator; face_id
3471 is -1 is this case. We know that the new face will not
3472 change until the next check pos, i.e. if the new face has a
3473 box, all characters up to that position will have a
3474 box. But, as usual, we don't know whether that position
3475 is really the end. */
3476 if (new_face_id != it->face_id)
3477 {
3478 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3479 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3480
3481 /* If new face has a box but old face hasn't, this is the
3482 start of a run of characters with box, i.e. it has a
3483 shadow on the left side. */
3484 it->start_of_box_run_p
3485 = new_face->box && (old_face == NULL || !old_face->box);
3486 it->face_box_p = new_face->box != FACE_NO_BOX;
3487 }
3488 }
3489
3490 it->face_id = new_face_id;
3491 return HANDLED_NORMALLY;
3492 }
3493
3494
3495 /* Return the ID of the face ``underlying'' IT's current position,
3496 which is in a string. If the iterator is associated with a
3497 buffer, return the face at IT's current buffer position.
3498 Otherwise, use the iterator's base_face_id. */
3499
3500 static int
3501 underlying_face_id (struct it *it)
3502 {
3503 int face_id = it->base_face_id, i;
3504
3505 xassert (STRINGP (it->string));
3506
3507 for (i = it->sp - 1; i >= 0; --i)
3508 if (NILP (it->stack[i].string))
3509 face_id = it->stack[i].face_id;
3510
3511 return face_id;
3512 }
3513
3514
3515 /* Compute the face one character before or after the current position
3516 of IT, in the visual order. BEFORE_P non-zero means get the face
3517 in front (to the left in L2R paragraphs, to the right in R2L
3518 paragraphs) of IT's screen position. Value is the ID of the face. */
3519
3520 static int
3521 face_before_or_after_it_pos (struct it *it, int before_p)
3522 {
3523 int face_id, limit;
3524 EMACS_INT next_check_charpos;
3525 struct it it_copy;
3526 void *it_copy_data = NULL;
3527
3528 xassert (it->s == NULL);
3529
3530 if (STRINGP (it->string))
3531 {
3532 EMACS_INT bufpos, charpos;
3533 int base_face_id;
3534
3535 /* No face change past the end of the string (for the case
3536 we are padding with spaces). No face change before the
3537 string start. */
3538 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3539 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3540 return it->face_id;
3541
3542 if (!it->bidi_p)
3543 {
3544 /* Set charpos to the position before or after IT's current
3545 position, in the logical order, which in the non-bidi
3546 case is the same as the visual order. */
3547 if (before_p)
3548 charpos = IT_STRING_CHARPOS (*it) - 1;
3549 else if (it->what == IT_COMPOSITION)
3550 /* For composition, we must check the character after the
3551 composition. */
3552 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3553 else
3554 charpos = IT_STRING_CHARPOS (*it) + 1;
3555 }
3556 else
3557 {
3558 if (before_p)
3559 {
3560 /* With bidi iteration, the character before the current
3561 in the visual order cannot be found by simple
3562 iteration, because "reverse" reordering is not
3563 supported. Instead, we need to use the move_it_*
3564 family of functions. */
3565 /* Ignore face changes before the first visible
3566 character on this display line. */
3567 if (it->current_x <= it->first_visible_x)
3568 return it->face_id;
3569 SAVE_IT (it_copy, *it, it_copy_data);
3570 /* Implementation note: Since move_it_in_display_line
3571 works in the iterator geometry, and thinks the first
3572 character is always the leftmost, even in R2L lines,
3573 we don't need to distinguish between the R2L and L2R
3574 cases here. */
3575 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3576 it_copy.current_x - 1, MOVE_TO_X);
3577 charpos = IT_STRING_CHARPOS (it_copy);
3578 RESTORE_IT (it, it, it_copy_data);
3579 }
3580 else
3581 {
3582 /* Set charpos to the string position of the character
3583 that comes after IT's current position in the visual
3584 order. */
3585 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3586
3587 it_copy = *it;
3588 while (n--)
3589 bidi_move_to_visually_next (&it_copy.bidi_it);
3590
3591 charpos = it_copy.bidi_it.charpos;
3592 }
3593 }
3594 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3595
3596 if (it->current.overlay_string_index >= 0)
3597 bufpos = IT_CHARPOS (*it);
3598 else
3599 bufpos = 0;
3600
3601 base_face_id = underlying_face_id (it);
3602
3603 /* Get the face for ASCII, or unibyte. */
3604 face_id = face_at_string_position (it->w,
3605 it->string,
3606 charpos,
3607 bufpos,
3608 it->region_beg_charpos,
3609 it->region_end_charpos,
3610 &next_check_charpos,
3611 base_face_id, 0);
3612
3613 /* Correct the face for charsets different from ASCII. Do it
3614 for the multibyte case only. The face returned above is
3615 suitable for unibyte text if IT->string is unibyte. */
3616 if (STRING_MULTIBYTE (it->string))
3617 {
3618 struct text_pos pos1 = string_pos (charpos, it->string);
3619 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3620 int c, len;
3621 struct face *face = FACE_FROM_ID (it->f, face_id);
3622
3623 c = string_char_and_length (p, &len);
3624 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3625 }
3626 }
3627 else
3628 {
3629 struct text_pos pos;
3630
3631 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3632 || (IT_CHARPOS (*it) <= BEGV && before_p))
3633 return it->face_id;
3634
3635 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3636 pos = it->current.pos;
3637
3638 if (!it->bidi_p)
3639 {
3640 if (before_p)
3641 DEC_TEXT_POS (pos, it->multibyte_p);
3642 else
3643 {
3644 if (it->what == IT_COMPOSITION)
3645 {
3646 /* For composition, we must check the position after
3647 the composition. */
3648 pos.charpos += it->cmp_it.nchars;
3649 pos.bytepos += it->len;
3650 }
3651 else
3652 INC_TEXT_POS (pos, it->multibyte_p);
3653 }
3654 }
3655 else
3656 {
3657 if (before_p)
3658 {
3659 /* With bidi iteration, the character before the current
3660 in the visual order cannot be found by simple
3661 iteration, because "reverse" reordering is not
3662 supported. Instead, we need to use the move_it_*
3663 family of functions. */
3664 /* Ignore face changes before the first visible
3665 character on this display line. */
3666 if (it->current_x <= it->first_visible_x)
3667 return it->face_id;
3668 SAVE_IT (it_copy, *it, it_copy_data);
3669 /* Implementation note: Since move_it_in_display_line
3670 works in the iterator geometry, and thinks the first
3671 character is always the leftmost, even in R2L lines,
3672 we don't need to distinguish between the R2L and L2R
3673 cases here. */
3674 move_it_in_display_line (&it_copy, ZV,
3675 it_copy.current_x - 1, MOVE_TO_X);
3676 pos = it_copy.current.pos;
3677 RESTORE_IT (it, it, it_copy_data);
3678 }
3679 else
3680 {
3681 /* Set charpos to the buffer position of the character
3682 that comes after IT's current position in the visual
3683 order. */
3684 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3685
3686 it_copy = *it;
3687 while (n--)
3688 bidi_move_to_visually_next (&it_copy.bidi_it);
3689
3690 SET_TEXT_POS (pos,
3691 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3692 }
3693 }
3694 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3695
3696 /* Determine face for CHARSET_ASCII, or unibyte. */
3697 face_id = face_at_buffer_position (it->w,
3698 CHARPOS (pos),
3699 it->region_beg_charpos,
3700 it->region_end_charpos,
3701 &next_check_charpos,
3702 limit, 0, -1);
3703
3704 /* Correct the face for charsets different from ASCII. Do it
3705 for the multibyte case only. The face returned above is
3706 suitable for unibyte text if current_buffer is unibyte. */
3707 if (it->multibyte_p)
3708 {
3709 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3710 struct face *face = FACE_FROM_ID (it->f, face_id);
3711 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3712 }
3713 }
3714
3715 return face_id;
3716 }
3717
3718
3719 \f
3720 /***********************************************************************
3721 Invisible text
3722 ***********************************************************************/
3723
3724 /* Set up iterator IT from invisible properties at its current
3725 position. Called from handle_stop. */
3726
3727 static enum prop_handled
3728 handle_invisible_prop (struct it *it)
3729 {
3730 enum prop_handled handled = HANDLED_NORMALLY;
3731
3732 if (STRINGP (it->string))
3733 {
3734 Lisp_Object prop, end_charpos, limit, charpos;
3735
3736 /* Get the value of the invisible text property at the
3737 current position. Value will be nil if there is no such
3738 property. */
3739 charpos = make_number (IT_STRING_CHARPOS (*it));
3740 prop = Fget_text_property (charpos, Qinvisible, it->string);
3741
3742 if (!NILP (prop)
3743 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3744 {
3745 EMACS_INT endpos;
3746
3747 handled = HANDLED_RECOMPUTE_PROPS;
3748
3749 /* Get the position at which the next change of the
3750 invisible text property can be found in IT->string.
3751 Value will be nil if the property value is the same for
3752 all the rest of IT->string. */
3753 XSETINT (limit, SCHARS (it->string));
3754 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3755 it->string, limit);
3756
3757 /* Text at current position is invisible. The next
3758 change in the property is at position end_charpos.
3759 Move IT's current position to that position. */
3760 if (INTEGERP (end_charpos)
3761 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
3762 {
3763 struct text_pos old;
3764 EMACS_INT oldpos;
3765
3766 old = it->current.string_pos;
3767 oldpos = CHARPOS (old);
3768 if (it->bidi_p)
3769 {
3770 if (it->bidi_it.first_elt
3771 && it->bidi_it.charpos < SCHARS (it->string))
3772 bidi_paragraph_init (it->paragraph_embedding,
3773 &it->bidi_it, 1);
3774 /* Bidi-iterate out of the invisible text. */
3775 do
3776 {
3777 bidi_move_to_visually_next (&it->bidi_it);
3778 }
3779 while (oldpos <= it->bidi_it.charpos
3780 && it->bidi_it.charpos < endpos);
3781
3782 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
3783 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
3784 if (IT_CHARPOS (*it) >= endpos)
3785 it->prev_stop = endpos;
3786 }
3787 else
3788 {
3789 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
3790 compute_string_pos (&it->current.string_pos, old, it->string);
3791 }
3792 }
3793 else
3794 {
3795 /* The rest of the string is invisible. If this is an
3796 overlay string, proceed with the next overlay string
3797 or whatever comes and return a character from there. */
3798 if (it->current.overlay_string_index >= 0)
3799 {
3800 next_overlay_string (it);
3801 /* Don't check for overlay strings when we just
3802 finished processing them. */
3803 handled = HANDLED_OVERLAY_STRING_CONSUMED;
3804 }
3805 else
3806 {
3807 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
3808 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
3809 }
3810 }
3811 }
3812 }
3813 else
3814 {
3815 int invis_p;
3816 EMACS_INT newpos, next_stop, start_charpos, tem;
3817 Lisp_Object pos, prop, overlay;
3818
3819 /* First of all, is there invisible text at this position? */
3820 tem = start_charpos = IT_CHARPOS (*it);
3821 pos = make_number (tem);
3822 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
3823 &overlay);
3824 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3825
3826 /* If we are on invisible text, skip over it. */
3827 if (invis_p && start_charpos < it->end_charpos)
3828 {
3829 /* Record whether we have to display an ellipsis for the
3830 invisible text. */
3831 int display_ellipsis_p = invis_p == 2;
3832
3833 handled = HANDLED_RECOMPUTE_PROPS;
3834
3835 /* Loop skipping over invisible text. The loop is left at
3836 ZV or with IT on the first char being visible again. */
3837 do
3838 {
3839 /* Try to skip some invisible text. Return value is the
3840 position reached which can be equal to where we start
3841 if there is nothing invisible there. This skips both
3842 over invisible text properties and overlays with
3843 invisible property. */
3844 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
3845
3846 /* If we skipped nothing at all we weren't at invisible
3847 text in the first place. If everything to the end of
3848 the buffer was skipped, end the loop. */
3849 if (newpos == tem || newpos >= ZV)
3850 invis_p = 0;
3851 else
3852 {
3853 /* We skipped some characters but not necessarily
3854 all there are. Check if we ended up on visible
3855 text. Fget_char_property returns the property of
3856 the char before the given position, i.e. if we
3857 get invis_p = 0, this means that the char at
3858 newpos is visible. */
3859 pos = make_number (newpos);
3860 prop = Fget_char_property (pos, Qinvisible, it->window);
3861 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
3862 }
3863
3864 /* If we ended up on invisible text, proceed to
3865 skip starting with next_stop. */
3866 if (invis_p)
3867 tem = next_stop;
3868
3869 /* If there are adjacent invisible texts, don't lose the
3870 second one's ellipsis. */
3871 if (invis_p == 2)
3872 display_ellipsis_p = 1;
3873 }
3874 while (invis_p);
3875
3876 /* The position newpos is now either ZV or on visible text. */
3877 if (it->bidi_p && newpos < ZV)
3878 {
3879 /* With bidi iteration, the region of invisible text
3880 could start and/or end in the middle of a non-base
3881 embedding level. Therefore, we need to skip
3882 invisible text using the bidi iterator, starting at
3883 IT's current position, until we find ourselves
3884 outside the invisible text. Skipping invisible text
3885 _after_ bidi iteration avoids affecting the visual
3886 order of the displayed text when invisible properties
3887 are added or removed. */
3888 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
3889 {
3890 /* If we were `reseat'ed to a new paragraph,
3891 determine the paragraph base direction. We need
3892 to do it now because next_element_from_buffer may
3893 not have a chance to do it, if we are going to
3894 skip any text at the beginning, which resets the
3895 FIRST_ELT flag. */
3896 bidi_paragraph_init (it->paragraph_embedding,
3897 &it->bidi_it, 1);
3898 }
3899 do
3900 {
3901 bidi_move_to_visually_next (&it->bidi_it);
3902 }
3903 while (it->stop_charpos <= it->bidi_it.charpos
3904 && it->bidi_it.charpos < newpos);
3905 IT_CHARPOS (*it) = it->bidi_it.charpos;
3906 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
3907 /* If we overstepped NEWPOS, record its position in the
3908 iterator, so that we skip invisible text if later the
3909 bidi iteration lands us in the invisible region
3910 again. */
3911 if (IT_CHARPOS (*it) >= newpos)
3912 it->prev_stop = newpos;
3913 }
3914 else
3915 {
3916 IT_CHARPOS (*it) = newpos;
3917 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
3918 }
3919
3920 /* If there are before-strings at the start of invisible
3921 text, and the text is invisible because of a text
3922 property, arrange to show before-strings because 20.x did
3923 it that way. (If the text is invisible because of an
3924 overlay property instead of a text property, this is
3925 already handled in the overlay code.) */
3926 if (NILP (overlay)
3927 && get_overlay_strings (it, it->stop_charpos))
3928 {
3929 handled = HANDLED_RECOMPUTE_PROPS;
3930 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
3931 }
3932 else if (display_ellipsis_p)
3933 {
3934 /* Make sure that the glyphs of the ellipsis will get
3935 correct `charpos' values. If we would not update
3936 it->position here, the glyphs would belong to the
3937 last visible character _before_ the invisible
3938 text, which confuses `set_cursor_from_row'.
3939
3940 We use the last invisible position instead of the
3941 first because this way the cursor is always drawn on
3942 the first "." of the ellipsis, whenever PT is inside
3943 the invisible text. Otherwise the cursor would be
3944 placed _after_ the ellipsis when the point is after the
3945 first invisible character. */
3946 if (!STRINGP (it->object))
3947 {
3948 it->position.charpos = newpos - 1;
3949 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
3950 }
3951 it->ellipsis_p = 1;
3952 /* Let the ellipsis display before
3953 considering any properties of the following char.
3954 Fixes jasonr@gnu.org 01 Oct 07 bug. */
3955 handled = HANDLED_RETURN;
3956 }
3957 }
3958 }
3959
3960 return handled;
3961 }
3962
3963
3964 /* Make iterator IT return `...' next.
3965 Replaces LEN characters from buffer. */
3966
3967 static void
3968 setup_for_ellipsis (struct it *it, int len)
3969 {
3970 /* Use the display table definition for `...'. Invalid glyphs
3971 will be handled by the method returning elements from dpvec. */
3972 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
3973 {
3974 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
3975 it->dpvec = v->contents;
3976 it->dpend = v->contents + v->header.size;
3977 }
3978 else
3979 {
3980 /* Default `...'. */
3981 it->dpvec = default_invis_vector;
3982 it->dpend = default_invis_vector + 3;
3983 }
3984
3985 it->dpvec_char_len = len;
3986 it->current.dpvec_index = 0;
3987 it->dpvec_face_id = -1;
3988
3989 /* Remember the current face id in case glyphs specify faces.
3990 IT's face is restored in set_iterator_to_next.
3991 saved_face_id was set to preceding char's face in handle_stop. */
3992 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
3993 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
3994
3995 it->method = GET_FROM_DISPLAY_VECTOR;
3996 it->ellipsis_p = 1;
3997 }
3998
3999
4000 \f
4001 /***********************************************************************
4002 'display' property
4003 ***********************************************************************/
4004
4005 /* Set up iterator IT from `display' property at its current position.
4006 Called from handle_stop.
4007 We return HANDLED_RETURN if some part of the display property
4008 overrides the display of the buffer text itself.
4009 Otherwise we return HANDLED_NORMALLY. */
4010
4011 static enum prop_handled
4012 handle_display_prop (struct it *it)
4013 {
4014 Lisp_Object propval, object, overlay;
4015 struct text_pos *position;
4016 EMACS_INT bufpos;
4017 /* Nonzero if some property replaces the display of the text itself. */
4018 int display_replaced_p = 0;
4019
4020 if (STRINGP (it->string))
4021 {
4022 object = it->string;
4023 position = &it->current.string_pos;
4024 bufpos = CHARPOS (it->current.pos);
4025 }
4026 else
4027 {
4028 XSETWINDOW (object, it->w);
4029 position = &it->current.pos;
4030 bufpos = CHARPOS (*position);
4031 }
4032
4033 /* Reset those iterator values set from display property values. */
4034 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4035 it->space_width = Qnil;
4036 it->font_height = Qnil;
4037 it->voffset = 0;
4038
4039 /* We don't support recursive `display' properties, i.e. string
4040 values that have a string `display' property, that have a string
4041 `display' property etc. */
4042 if (!it->string_from_display_prop_p)
4043 it->area = TEXT_AREA;
4044
4045 propval = get_char_property_and_overlay (make_number (position->charpos),
4046 Qdisplay, object, &overlay);
4047 if (NILP (propval))
4048 return HANDLED_NORMALLY;
4049 /* Now OVERLAY is the overlay that gave us this property, or nil
4050 if it was a text property. */
4051
4052 if (!STRINGP (it->string))
4053 object = it->w->buffer;
4054
4055 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4056 position, bufpos,
4057 FRAME_WINDOW_P (it->f));
4058
4059 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4060 }
4061
4062 /* Subroutine of handle_display_prop. Returns non-zero if the display
4063 specification in SPEC is a replacing specification, i.e. it would
4064 replace the text covered by `display' property with something else,
4065 such as an image or a display string.
4066
4067 See handle_single_display_spec for documentation of arguments.
4068 frame_window_p is non-zero if the window being redisplayed is on a
4069 GUI frame; this argument is used only if IT is NULL, see below.
4070
4071 IT can be NULL, if this is called by the bidi reordering code
4072 through compute_display_string_pos, which see. In that case, this
4073 function only examines SPEC, but does not otherwise "handle" it, in
4074 the sense that it doesn't set up members of IT from the display
4075 spec. */
4076 static int
4077 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4078 Lisp_Object overlay, struct text_pos *position,
4079 EMACS_INT bufpos, int frame_window_p)
4080 {
4081 int replacing_p = 0;
4082
4083 if (CONSP (spec)
4084 /* Simple specerties. */
4085 && !EQ (XCAR (spec), Qimage)
4086 && !EQ (XCAR (spec), Qspace)
4087 && !EQ (XCAR (spec), Qwhen)
4088 && !EQ (XCAR (spec), Qslice)
4089 && !EQ (XCAR (spec), Qspace_width)
4090 && !EQ (XCAR (spec), Qheight)
4091 && !EQ (XCAR (spec), Qraise)
4092 /* Marginal area specifications. */
4093 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4094 && !EQ (XCAR (spec), Qleft_fringe)
4095 && !EQ (XCAR (spec), Qright_fringe)
4096 && !NILP (XCAR (spec)))
4097 {
4098 for (; CONSP (spec); spec = XCDR (spec))
4099 {
4100 if (handle_single_display_spec (it, XCAR (spec), object, overlay,
4101 position, bufpos, replacing_p,
4102 frame_window_p))
4103 {
4104 replacing_p = 1;
4105 /* If some text in a string is replaced, `position' no
4106 longer points to the position of `object'. */
4107 if (!it || STRINGP (object))
4108 break;
4109 }
4110 }
4111 }
4112 else if (VECTORP (spec))
4113 {
4114 int i;
4115 for (i = 0; i < ASIZE (spec); ++i)
4116 if (handle_single_display_spec (it, AREF (spec, i), object, overlay,
4117 position, bufpos, replacing_p,
4118 frame_window_p))
4119 {
4120 replacing_p = 1;
4121 /* If some text in a string is replaced, `position' no
4122 longer points to the position of `object'. */
4123 if (!it || STRINGP (object))
4124 break;
4125 }
4126 }
4127 else
4128 {
4129 if (handle_single_display_spec (it, spec, object, overlay,
4130 position, bufpos, 0, frame_window_p))
4131 replacing_p = 1;
4132 }
4133
4134 return replacing_p;
4135 }
4136
4137 /* Value is the position of the end of the `display' property starting
4138 at START_POS in OBJECT. */
4139
4140 static struct text_pos
4141 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4142 {
4143 Lisp_Object end;
4144 struct text_pos end_pos;
4145
4146 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4147 Qdisplay, object, Qnil);
4148 CHARPOS (end_pos) = XFASTINT (end);
4149 if (STRINGP (object))
4150 compute_string_pos (&end_pos, start_pos, it->string);
4151 else
4152 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4153
4154 return end_pos;
4155 }
4156
4157
4158 /* Set up IT from a single `display' property specification SPEC. OBJECT
4159 is the object in which the `display' property was found. *POSITION
4160 is the position in OBJECT at which the `display' property was found.
4161 BUFPOS is the buffer position of OBJECT (different from POSITION if
4162 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4163 previously saw a display specification which already replaced text
4164 display with something else, for example an image; we ignore such
4165 properties after the first one has been processed.
4166
4167 OVERLAY is the overlay this `display' property came from,
4168 or nil if it was a text property.
4169
4170 If SPEC is a `space' or `image' specification, and in some other
4171 cases too, set *POSITION to the position where the `display'
4172 property ends.
4173
4174 If IT is NULL, only examine the property specification in SPEC, but
4175 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4176 is intended to be displayed in a window on a GUI frame.
4177
4178 Value is non-zero if something was found which replaces the display
4179 of buffer or string text. */
4180
4181 static int
4182 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4183 Lisp_Object overlay, struct text_pos *position,
4184 EMACS_INT bufpos, int display_replaced_p,
4185 int frame_window_p)
4186 {
4187 Lisp_Object form;
4188 Lisp_Object location, value;
4189 struct text_pos start_pos = *position;
4190 int valid_p;
4191
4192 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4193 If the result is non-nil, use VALUE instead of SPEC. */
4194 form = Qt;
4195 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4196 {
4197 spec = XCDR (spec);
4198 if (!CONSP (spec))
4199 return 0;
4200 form = XCAR (spec);
4201 spec = XCDR (spec);
4202 }
4203
4204 if (!NILP (form) && !EQ (form, Qt))
4205 {
4206 int count = SPECPDL_INDEX ();
4207 struct gcpro gcpro1;
4208
4209 /* Bind `object' to the object having the `display' property, a
4210 buffer or string. Bind `position' to the position in the
4211 object where the property was found, and `buffer-position'
4212 to the current position in the buffer. */
4213
4214 if (NILP (object))
4215 XSETBUFFER (object, current_buffer);
4216 specbind (Qobject, object);
4217 specbind (Qposition, make_number (CHARPOS (*position)));
4218 specbind (Qbuffer_position, make_number (bufpos));
4219 GCPRO1 (form);
4220 form = safe_eval (form);
4221 UNGCPRO;
4222 unbind_to (count, Qnil);
4223 }
4224
4225 if (NILP (form))
4226 return 0;
4227
4228 /* Handle `(height HEIGHT)' specifications. */
4229 if (CONSP (spec)
4230 && EQ (XCAR (spec), Qheight)
4231 && CONSP (XCDR (spec)))
4232 {
4233 if (it)
4234 {
4235 if (!FRAME_WINDOW_P (it->f))
4236 return 0;
4237
4238 it->font_height = XCAR (XCDR (spec));
4239 if (!NILP (it->font_height))
4240 {
4241 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4242 int new_height = -1;
4243
4244 if (CONSP (it->font_height)
4245 && (EQ (XCAR (it->font_height), Qplus)
4246 || EQ (XCAR (it->font_height), Qminus))
4247 && CONSP (XCDR (it->font_height))
4248 && INTEGERP (XCAR (XCDR (it->font_height))))
4249 {
4250 /* `(+ N)' or `(- N)' where N is an integer. */
4251 int steps = XINT (XCAR (XCDR (it->font_height)));
4252 if (EQ (XCAR (it->font_height), Qplus))
4253 steps = - steps;
4254 it->face_id = smaller_face (it->f, it->face_id, steps);
4255 }
4256 else if (FUNCTIONP (it->font_height))
4257 {
4258 /* Call function with current height as argument.
4259 Value is the new height. */
4260 Lisp_Object height;
4261 height = safe_call1 (it->font_height,
4262 face->lface[LFACE_HEIGHT_INDEX]);
4263 if (NUMBERP (height))
4264 new_height = XFLOATINT (height);
4265 }
4266 else if (NUMBERP (it->font_height))
4267 {
4268 /* Value is a multiple of the canonical char height. */
4269 struct face *f;
4270
4271 f = FACE_FROM_ID (it->f,
4272 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4273 new_height = (XFLOATINT (it->font_height)
4274 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4275 }
4276 else
4277 {
4278 /* Evaluate IT->font_height with `height' bound to the
4279 current specified height to get the new height. */
4280 int count = SPECPDL_INDEX ();
4281
4282 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4283 value = safe_eval (it->font_height);
4284 unbind_to (count, Qnil);
4285
4286 if (NUMBERP (value))
4287 new_height = XFLOATINT (value);
4288 }
4289
4290 if (new_height > 0)
4291 it->face_id = face_with_height (it->f, it->face_id, new_height);
4292 }
4293 }
4294
4295 return 0;
4296 }
4297
4298 /* Handle `(space-width WIDTH)'. */
4299 if (CONSP (spec)
4300 && EQ (XCAR (spec), Qspace_width)
4301 && CONSP (XCDR (spec)))
4302 {
4303 if (it)
4304 {
4305 if (!FRAME_WINDOW_P (it->f))
4306 return 0;
4307
4308 value = XCAR (XCDR (spec));
4309 if (NUMBERP (value) && XFLOATINT (value) > 0)
4310 it->space_width = value;
4311 }
4312
4313 return 0;
4314 }
4315
4316 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4317 if (CONSP (spec)
4318 && EQ (XCAR (spec), Qslice))
4319 {
4320 Lisp_Object tem;
4321
4322 if (it)
4323 {
4324 if (!FRAME_WINDOW_P (it->f))
4325 return 0;
4326
4327 if (tem = XCDR (spec), CONSP (tem))
4328 {
4329 it->slice.x = XCAR (tem);
4330 if (tem = XCDR (tem), CONSP (tem))
4331 {
4332 it->slice.y = XCAR (tem);
4333 if (tem = XCDR (tem), CONSP (tem))
4334 {
4335 it->slice.width = XCAR (tem);
4336 if (tem = XCDR (tem), CONSP (tem))
4337 it->slice.height = XCAR (tem);
4338 }
4339 }
4340 }
4341 }
4342
4343 return 0;
4344 }
4345
4346 /* Handle `(raise FACTOR)'. */
4347 if (CONSP (spec)
4348 && EQ (XCAR (spec), Qraise)
4349 && CONSP (XCDR (spec)))
4350 {
4351 if (it)
4352 {
4353 if (!FRAME_WINDOW_P (it->f))
4354 return 0;
4355
4356 #ifdef HAVE_WINDOW_SYSTEM
4357 value = XCAR (XCDR (spec));
4358 if (NUMBERP (value))
4359 {
4360 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4361 it->voffset = - (XFLOATINT (value)
4362 * (FONT_HEIGHT (face->font)));
4363 }
4364 #endif /* HAVE_WINDOW_SYSTEM */
4365 }
4366
4367 return 0;
4368 }
4369
4370 /* Don't handle the other kinds of display specifications
4371 inside a string that we got from a `display' property. */
4372 if (it && it->string_from_display_prop_p)
4373 return 0;
4374
4375 /* Characters having this form of property are not displayed, so
4376 we have to find the end of the property. */
4377 if (it)
4378 {
4379 start_pos = *position;
4380 *position = display_prop_end (it, object, start_pos);
4381 }
4382 value = Qnil;
4383
4384 /* Stop the scan at that end position--we assume that all
4385 text properties change there. */
4386 if (it)
4387 it->stop_charpos = position->charpos;
4388
4389 /* Handle `(left-fringe BITMAP [FACE])'
4390 and `(right-fringe BITMAP [FACE])'. */
4391 if (CONSP (spec)
4392 && (EQ (XCAR (spec), Qleft_fringe)
4393 || EQ (XCAR (spec), Qright_fringe))
4394 && CONSP (XCDR (spec)))
4395 {
4396 int fringe_bitmap;
4397
4398 if (it)
4399 {
4400 if (!FRAME_WINDOW_P (it->f))
4401 /* If we return here, POSITION has been advanced
4402 across the text with this property. */
4403 return 0;
4404 }
4405 else if (!frame_window_p)
4406 return 0;
4407
4408 #ifdef HAVE_WINDOW_SYSTEM
4409 value = XCAR (XCDR (spec));
4410 if (!SYMBOLP (value)
4411 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4412 /* If we return here, POSITION has been advanced
4413 across the text with this property. */
4414 return 0;
4415
4416 if (it)
4417 {
4418 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4419
4420 if (CONSP (XCDR (XCDR (spec))))
4421 {
4422 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4423 int face_id2 = lookup_derived_face (it->f, face_name,
4424 FRINGE_FACE_ID, 0);
4425 if (face_id2 >= 0)
4426 face_id = face_id2;
4427 }
4428
4429 /* Save current settings of IT so that we can restore them
4430 when we are finished with the glyph property value. */
4431 push_it (it, position);
4432
4433 it->area = TEXT_AREA;
4434 it->what = IT_IMAGE;
4435 it->image_id = -1; /* no image */
4436 it->position = start_pos;
4437 it->object = NILP (object) ? it->w->buffer : object;
4438 it->method = GET_FROM_IMAGE;
4439 it->from_overlay = Qnil;
4440 it->face_id = face_id;
4441 it->from_disp_prop_p = 1;
4442
4443 /* Say that we haven't consumed the characters with
4444 `display' property yet. The call to pop_it in
4445 set_iterator_to_next will clean this up. */
4446 *position = start_pos;
4447
4448 if (EQ (XCAR (spec), Qleft_fringe))
4449 {
4450 it->left_user_fringe_bitmap = fringe_bitmap;
4451 it->left_user_fringe_face_id = face_id;
4452 }
4453 else
4454 {
4455 it->right_user_fringe_bitmap = fringe_bitmap;
4456 it->right_user_fringe_face_id = face_id;
4457 }
4458 }
4459 #endif /* HAVE_WINDOW_SYSTEM */
4460 return 1;
4461 }
4462
4463 /* Prepare to handle `((margin left-margin) ...)',
4464 `((margin right-margin) ...)' and `((margin nil) ...)'
4465 prefixes for display specifications. */
4466 location = Qunbound;
4467 if (CONSP (spec) && CONSP (XCAR (spec)))
4468 {
4469 Lisp_Object tem;
4470
4471 value = XCDR (spec);
4472 if (CONSP (value))
4473 value = XCAR (value);
4474
4475 tem = XCAR (spec);
4476 if (EQ (XCAR (tem), Qmargin)
4477 && (tem = XCDR (tem),
4478 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4479 (NILP (tem)
4480 || EQ (tem, Qleft_margin)
4481 || EQ (tem, Qright_margin))))
4482 location = tem;
4483 }
4484
4485 if (EQ (location, Qunbound))
4486 {
4487 location = Qnil;
4488 value = spec;
4489 }
4490
4491 /* After this point, VALUE is the property after any
4492 margin prefix has been stripped. It must be a string,
4493 an image specification, or `(space ...)'.
4494
4495 LOCATION specifies where to display: `left-margin',
4496 `right-margin' or nil. */
4497
4498 valid_p = (STRINGP (value)
4499 #ifdef HAVE_WINDOW_SYSTEM
4500 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4501 && valid_image_p (value))
4502 #endif /* not HAVE_WINDOW_SYSTEM */
4503 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4504
4505 if (valid_p && !display_replaced_p)
4506 {
4507 if (!it)
4508 return 1;
4509
4510 /* Save current settings of IT so that we can restore them
4511 when we are finished with the glyph property value. */
4512 push_it (it, position);
4513 it->from_overlay = overlay;
4514 it->from_disp_prop_p = 1;
4515
4516 if (NILP (location))
4517 it->area = TEXT_AREA;
4518 else if (EQ (location, Qleft_margin))
4519 it->area = LEFT_MARGIN_AREA;
4520 else
4521 it->area = RIGHT_MARGIN_AREA;
4522
4523 if (STRINGP (value))
4524 {
4525 it->string = value;
4526 it->multibyte_p = STRING_MULTIBYTE (it->string);
4527 it->current.overlay_string_index = -1;
4528 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4529 it->end_charpos = it->string_nchars = SCHARS (it->string);
4530 it->method = GET_FROM_STRING;
4531 it->stop_charpos = 0;
4532 it->prev_stop = 0;
4533 it->base_level_stop = 0;
4534 it->string_from_display_prop_p = 1;
4535 /* Say that we haven't consumed the characters with
4536 `display' property yet. The call to pop_it in
4537 set_iterator_to_next will clean this up. */
4538 if (BUFFERP (object))
4539 *position = start_pos;
4540
4541 /* Force paragraph direction to be that of the parent
4542 object. If the parent object's paragraph direction is
4543 not yet determined, default to L2R. */
4544 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4545 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4546 else
4547 it->paragraph_embedding = L2R;
4548
4549 /* Set up the bidi iterator for this display string. */
4550 if (it->bidi_p)
4551 {
4552 it->bidi_it.string.lstring = it->string;
4553 it->bidi_it.string.s = NULL;
4554 it->bidi_it.string.schars = it->end_charpos;
4555 it->bidi_it.string.bufpos = bufpos;
4556 it->bidi_it.string.from_disp_str = 1;
4557 it->bidi_it.string.unibyte = !it->multibyte_p;
4558 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4559 }
4560 }
4561 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4562 {
4563 it->method = GET_FROM_STRETCH;
4564 it->object = value;
4565 *position = it->position = start_pos;
4566 }
4567 #ifdef HAVE_WINDOW_SYSTEM
4568 else
4569 {
4570 it->what = IT_IMAGE;
4571 it->image_id = lookup_image (it->f, value);
4572 it->position = start_pos;
4573 it->object = NILP (object) ? it->w->buffer : object;
4574 it->method = GET_FROM_IMAGE;
4575
4576 /* Say that we haven't consumed the characters with
4577 `display' property yet. The call to pop_it in
4578 set_iterator_to_next will clean this up. */
4579 *position = start_pos;
4580 }
4581 #endif /* HAVE_WINDOW_SYSTEM */
4582
4583 return 1;
4584 }
4585
4586 /* Invalid property or property not supported. Restore
4587 POSITION to what it was before. */
4588 *position = start_pos;
4589 return 0;
4590 }
4591
4592 /* Check if PROP is a display property value whose text should be
4593 treated as intangible. OVERLAY is the overlay from which PROP
4594 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4595 specify the buffer position covered by PROP. */
4596
4597 int
4598 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4599 EMACS_INT charpos, EMACS_INT bytepos)
4600 {
4601 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4602 struct text_pos position;
4603
4604 SET_TEXT_POS (position, charpos, bytepos);
4605 return handle_display_spec (NULL, prop, Qnil, overlay,
4606 &position, charpos, frame_window_p);
4607 }
4608
4609
4610 /* Return 1 if PROP is a display sub-property value containing STRING.
4611
4612 Implementation note: this and the following function are really
4613 special cases of handle_display_spec and
4614 handle_single_display_spec, and should ideally use the same code.
4615 Until they do, these two pairs must be consistent and must be
4616 modified in sync. */
4617
4618 static int
4619 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4620 {
4621 if (EQ (string, prop))
4622 return 1;
4623
4624 /* Skip over `when FORM'. */
4625 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4626 {
4627 prop = XCDR (prop);
4628 if (!CONSP (prop))
4629 return 0;
4630 /* Actually, the condition following `when' should be eval'ed,
4631 like handle_single_display_spec does, and we should return
4632 zero if it evaluates to nil. However, this function is
4633 called only when the buffer was already displayed and some
4634 glyph in the glyph matrix was found to come from a display
4635 string. Therefore, the condition was already evaluated, and
4636 the result was non-nil, otherwise the display string wouldn't
4637 have been displayed and we would have never been called for
4638 this property. Thus, we can skip the evaluation and assume
4639 its result is non-nil. */
4640 prop = XCDR (prop);
4641 }
4642
4643 if (CONSP (prop))
4644 /* Skip over `margin LOCATION'. */
4645 if (EQ (XCAR (prop), Qmargin))
4646 {
4647 prop = XCDR (prop);
4648 if (!CONSP (prop))
4649 return 0;
4650
4651 prop = XCDR (prop);
4652 if (!CONSP (prop))
4653 return 0;
4654 }
4655
4656 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4657 }
4658
4659
4660 /* Return 1 if STRING appears in the `display' property PROP. */
4661
4662 static int
4663 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4664 {
4665 if (CONSP (prop)
4666 && !EQ (XCAR (prop), Qwhen)
4667 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4668 {
4669 /* A list of sub-properties. */
4670 while (CONSP (prop))
4671 {
4672 if (single_display_spec_string_p (XCAR (prop), string))
4673 return 1;
4674 prop = XCDR (prop);
4675 }
4676 }
4677 else if (VECTORP (prop))
4678 {
4679 /* A vector of sub-properties. */
4680 int i;
4681 for (i = 0; i < ASIZE (prop); ++i)
4682 if (single_display_spec_string_p (AREF (prop, i), string))
4683 return 1;
4684 }
4685 else
4686 return single_display_spec_string_p (prop, string);
4687
4688 return 0;
4689 }
4690
4691 /* Look for STRING in overlays and text properties in the current
4692 buffer, between character positions FROM and TO (excluding TO).
4693 BACK_P non-zero means look back (in this case, TO is supposed to be
4694 less than FROM).
4695 Value is the first character position where STRING was found, or
4696 zero if it wasn't found before hitting TO.
4697
4698 This function may only use code that doesn't eval because it is
4699 called asynchronously from note_mouse_highlight. */
4700
4701 static EMACS_INT
4702 string_buffer_position_lim (Lisp_Object string,
4703 EMACS_INT from, EMACS_INT to, int back_p)
4704 {
4705 Lisp_Object limit, prop, pos;
4706 int found = 0;
4707
4708 pos = make_number (from);
4709
4710 if (!back_p) /* looking forward */
4711 {
4712 limit = make_number (min (to, ZV));
4713 while (!found && !EQ (pos, limit))
4714 {
4715 prop = Fget_char_property (pos, Qdisplay, Qnil);
4716 if (!NILP (prop) && display_prop_string_p (prop, string))
4717 found = 1;
4718 else
4719 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4720 limit);
4721 }
4722 }
4723 else /* looking back */
4724 {
4725 limit = make_number (max (to, BEGV));
4726 while (!found && !EQ (pos, limit))
4727 {
4728 prop = Fget_char_property (pos, Qdisplay, Qnil);
4729 if (!NILP (prop) && display_prop_string_p (prop, string))
4730 found = 1;
4731 else
4732 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
4733 limit);
4734 }
4735 }
4736
4737 return found ? XINT (pos) : 0;
4738 }
4739
4740 /* Determine which buffer position in current buffer STRING comes from.
4741 AROUND_CHARPOS is an approximate position where it could come from.
4742 Value is the buffer position or 0 if it couldn't be determined.
4743
4744 This function is necessary because we don't record buffer positions
4745 in glyphs generated from strings (to keep struct glyph small).
4746 This function may only use code that doesn't eval because it is
4747 called asynchronously from note_mouse_highlight. */
4748
4749 static EMACS_INT
4750 string_buffer_position (Lisp_Object string, EMACS_INT around_charpos)
4751 {
4752 const int MAX_DISTANCE = 1000;
4753 EMACS_INT found = string_buffer_position_lim (string, around_charpos,
4754 around_charpos + MAX_DISTANCE,
4755 0);
4756
4757 if (!found)
4758 found = string_buffer_position_lim (string, around_charpos,
4759 around_charpos - MAX_DISTANCE, 1);
4760 return found;
4761 }
4762
4763
4764 \f
4765 /***********************************************************************
4766 `composition' property
4767 ***********************************************************************/
4768
4769 /* Set up iterator IT from `composition' property at its current
4770 position. Called from handle_stop. */
4771
4772 static enum prop_handled
4773 handle_composition_prop (struct it *it)
4774 {
4775 Lisp_Object prop, string;
4776 EMACS_INT pos, pos_byte, start, end;
4777
4778 if (STRINGP (it->string))
4779 {
4780 unsigned char *s;
4781
4782 pos = IT_STRING_CHARPOS (*it);
4783 pos_byte = IT_STRING_BYTEPOS (*it);
4784 string = it->string;
4785 s = SDATA (string) + pos_byte;
4786 it->c = STRING_CHAR (s);
4787 }
4788 else
4789 {
4790 pos = IT_CHARPOS (*it);
4791 pos_byte = IT_BYTEPOS (*it);
4792 string = Qnil;
4793 it->c = FETCH_CHAR (pos_byte);
4794 }
4795
4796 /* If there's a valid composition and point is not inside of the
4797 composition (in the case that the composition is from the current
4798 buffer), draw a glyph composed from the composition components. */
4799 if (find_composition (pos, -1, &start, &end, &prop, string)
4800 && COMPOSITION_VALID_P (start, end, prop)
4801 && (STRINGP (it->string) || (PT <= start || PT >= end)))
4802 {
4803 if (start != pos)
4804 {
4805 if (STRINGP (it->string))
4806 pos_byte = string_char_to_byte (it->string, start);
4807 else
4808 pos_byte = CHAR_TO_BYTE (start);
4809 }
4810 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
4811 prop, string);
4812
4813 if (it->cmp_it.id >= 0)
4814 {
4815 it->cmp_it.ch = -1;
4816 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
4817 it->cmp_it.nglyphs = -1;
4818 }
4819 }
4820
4821 return HANDLED_NORMALLY;
4822 }
4823
4824
4825 \f
4826 /***********************************************************************
4827 Overlay strings
4828 ***********************************************************************/
4829
4830 /* The following structure is used to record overlay strings for
4831 later sorting in load_overlay_strings. */
4832
4833 struct overlay_entry
4834 {
4835 Lisp_Object overlay;
4836 Lisp_Object string;
4837 int priority;
4838 int after_string_p;
4839 };
4840
4841
4842 /* Set up iterator IT from overlay strings at its current position.
4843 Called from handle_stop. */
4844
4845 static enum prop_handled
4846 handle_overlay_change (struct it *it)
4847 {
4848 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
4849 return HANDLED_RECOMPUTE_PROPS;
4850 else
4851 return HANDLED_NORMALLY;
4852 }
4853
4854
4855 /* Set up the next overlay string for delivery by IT, if there is an
4856 overlay string to deliver. Called by set_iterator_to_next when the
4857 end of the current overlay string is reached. If there are more
4858 overlay strings to display, IT->string and
4859 IT->current.overlay_string_index are set appropriately here.
4860 Otherwise IT->string is set to nil. */
4861
4862 static void
4863 next_overlay_string (struct it *it)
4864 {
4865 ++it->current.overlay_string_index;
4866 if (it->current.overlay_string_index == it->n_overlay_strings)
4867 {
4868 /* No more overlay strings. Restore IT's settings to what
4869 they were before overlay strings were processed, and
4870 continue to deliver from current_buffer. */
4871
4872 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
4873 pop_it (it);
4874 xassert (it->sp > 0
4875 || (NILP (it->string)
4876 && it->method == GET_FROM_BUFFER
4877 && it->stop_charpos >= BEGV
4878 && it->stop_charpos <= it->end_charpos));
4879 it->current.overlay_string_index = -1;
4880 it->n_overlay_strings = 0;
4881 it->overlay_strings_charpos = -1;
4882
4883 /* If we're at the end of the buffer, record that we have
4884 processed the overlay strings there already, so that
4885 next_element_from_buffer doesn't try it again. */
4886 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
4887 it->overlay_strings_at_end_processed_p = 1;
4888 }
4889 else
4890 {
4891 /* There are more overlay strings to process. If
4892 IT->current.overlay_string_index has advanced to a position
4893 where we must load IT->overlay_strings with more strings, do
4894 it. We must load at the IT->overlay_strings_charpos where
4895 IT->n_overlay_strings was originally computed; when invisible
4896 text is present, this might not be IT_CHARPOS (Bug#7016). */
4897 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
4898
4899 if (it->current.overlay_string_index && i == 0)
4900 load_overlay_strings (it, it->overlay_strings_charpos);
4901
4902 /* Initialize IT to deliver display elements from the overlay
4903 string. */
4904 it->string = it->overlay_strings[i];
4905 it->multibyte_p = STRING_MULTIBYTE (it->string);
4906 SET_TEXT_POS (it->current.string_pos, 0, 0);
4907 it->method = GET_FROM_STRING;
4908 it->stop_charpos = 0;
4909 if (it->cmp_it.stop_pos >= 0)
4910 it->cmp_it.stop_pos = 0;
4911 it->prev_stop = 0;
4912 it->base_level_stop = 0;
4913
4914 /* Set up the bidi iterator for this overlay string. */
4915 if (it->bidi_p)
4916 {
4917 it->bidi_it.string.lstring = it->string;
4918 it->bidi_it.string.s = NULL;
4919 it->bidi_it.string.schars = SCHARS (it->string);
4920 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
4921 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
4922 it->bidi_it.string.unibyte = !it->multibyte_p;
4923 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4924 }
4925 }
4926
4927 CHECK_IT (it);
4928 }
4929
4930
4931 /* Compare two overlay_entry structures E1 and E2. Used as a
4932 comparison function for qsort in load_overlay_strings. Overlay
4933 strings for the same position are sorted so that
4934
4935 1. All after-strings come in front of before-strings, except
4936 when they come from the same overlay.
4937
4938 2. Within after-strings, strings are sorted so that overlay strings
4939 from overlays with higher priorities come first.
4940
4941 2. Within before-strings, strings are sorted so that overlay
4942 strings from overlays with higher priorities come last.
4943
4944 Value is analogous to strcmp. */
4945
4946
4947 static int
4948 compare_overlay_entries (const void *e1, const void *e2)
4949 {
4950 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
4951 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
4952 int result;
4953
4954 if (entry1->after_string_p != entry2->after_string_p)
4955 {
4956 /* Let after-strings appear in front of before-strings if
4957 they come from different overlays. */
4958 if (EQ (entry1->overlay, entry2->overlay))
4959 result = entry1->after_string_p ? 1 : -1;
4960 else
4961 result = entry1->after_string_p ? -1 : 1;
4962 }
4963 else if (entry1->after_string_p)
4964 /* After-strings sorted in order of decreasing priority. */
4965 result = entry2->priority - entry1->priority;
4966 else
4967 /* Before-strings sorted in order of increasing priority. */
4968 result = entry1->priority - entry2->priority;
4969
4970 return result;
4971 }
4972
4973
4974 /* Load the vector IT->overlay_strings with overlay strings from IT's
4975 current buffer position, or from CHARPOS if that is > 0. Set
4976 IT->n_overlays to the total number of overlay strings found.
4977
4978 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
4979 a time. On entry into load_overlay_strings,
4980 IT->current.overlay_string_index gives the number of overlay
4981 strings that have already been loaded by previous calls to this
4982 function.
4983
4984 IT->add_overlay_start contains an additional overlay start
4985 position to consider for taking overlay strings from, if non-zero.
4986 This position comes into play when the overlay has an `invisible'
4987 property, and both before and after-strings. When we've skipped to
4988 the end of the overlay, because of its `invisible' property, we
4989 nevertheless want its before-string to appear.
4990 IT->add_overlay_start will contain the overlay start position
4991 in this case.
4992
4993 Overlay strings are sorted so that after-string strings come in
4994 front of before-string strings. Within before and after-strings,
4995 strings are sorted by overlay priority. See also function
4996 compare_overlay_entries. */
4997
4998 static void
4999 load_overlay_strings (struct it *it, EMACS_INT charpos)
5000 {
5001 Lisp_Object overlay, window, str, invisible;
5002 struct Lisp_Overlay *ov;
5003 EMACS_INT start, end;
5004 int size = 20;
5005 int n = 0, i, j, invis_p;
5006 struct overlay_entry *entries
5007 = (struct overlay_entry *) alloca (size * sizeof *entries);
5008
5009 if (charpos <= 0)
5010 charpos = IT_CHARPOS (*it);
5011
5012 /* Append the overlay string STRING of overlay OVERLAY to vector
5013 `entries' which has size `size' and currently contains `n'
5014 elements. AFTER_P non-zero means STRING is an after-string of
5015 OVERLAY. */
5016 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5017 do \
5018 { \
5019 Lisp_Object priority; \
5020 \
5021 if (n == size) \
5022 { \
5023 int new_size = 2 * size; \
5024 struct overlay_entry *old = entries; \
5025 entries = \
5026 (struct overlay_entry *) alloca (new_size \
5027 * sizeof *entries); \
5028 memcpy (entries, old, size * sizeof *entries); \
5029 size = new_size; \
5030 } \
5031 \
5032 entries[n].string = (STRING); \
5033 entries[n].overlay = (OVERLAY); \
5034 priority = Foverlay_get ((OVERLAY), Qpriority); \
5035 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5036 entries[n].after_string_p = (AFTER_P); \
5037 ++n; \
5038 } \
5039 while (0)
5040
5041 /* Process overlay before the overlay center. */
5042 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5043 {
5044 XSETMISC (overlay, ov);
5045 xassert (OVERLAYP (overlay));
5046 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5047 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5048
5049 if (end < charpos)
5050 break;
5051
5052 /* Skip this overlay if it doesn't start or end at IT's current
5053 position. */
5054 if (end != charpos && start != charpos)
5055 continue;
5056
5057 /* Skip this overlay if it doesn't apply to IT->w. */
5058 window = Foverlay_get (overlay, Qwindow);
5059 if (WINDOWP (window) && XWINDOW (window) != it->w)
5060 continue;
5061
5062 /* If the text ``under'' the overlay is invisible, both before-
5063 and after-strings from this overlay are visible; start and
5064 end position are indistinguishable. */
5065 invisible = Foverlay_get (overlay, Qinvisible);
5066 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5067
5068 /* If overlay has a non-empty before-string, record it. */
5069 if ((start == charpos || (end == charpos && invis_p))
5070 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5071 && SCHARS (str))
5072 RECORD_OVERLAY_STRING (overlay, str, 0);
5073
5074 /* If overlay has a non-empty after-string, record it. */
5075 if ((end == charpos || (start == charpos && invis_p))
5076 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5077 && SCHARS (str))
5078 RECORD_OVERLAY_STRING (overlay, str, 1);
5079 }
5080
5081 /* Process overlays after the overlay center. */
5082 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5083 {
5084 XSETMISC (overlay, ov);
5085 xassert (OVERLAYP (overlay));
5086 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5087 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5088
5089 if (start > charpos)
5090 break;
5091
5092 /* Skip this overlay if it doesn't start or end at IT's current
5093 position. */
5094 if (end != charpos && start != charpos)
5095 continue;
5096
5097 /* Skip this overlay if it doesn't apply to IT->w. */
5098 window = Foverlay_get (overlay, Qwindow);
5099 if (WINDOWP (window) && XWINDOW (window) != it->w)
5100 continue;
5101
5102 /* If the text ``under'' the overlay is invisible, it has a zero
5103 dimension, and both before- and after-strings apply. */
5104 invisible = Foverlay_get (overlay, Qinvisible);
5105 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5106
5107 /* If overlay has a non-empty before-string, record it. */
5108 if ((start == charpos || (end == charpos && invis_p))
5109 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5110 && SCHARS (str))
5111 RECORD_OVERLAY_STRING (overlay, str, 0);
5112
5113 /* If overlay has a non-empty after-string, record it. */
5114 if ((end == charpos || (start == charpos && invis_p))
5115 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5116 && SCHARS (str))
5117 RECORD_OVERLAY_STRING (overlay, str, 1);
5118 }
5119
5120 #undef RECORD_OVERLAY_STRING
5121
5122 /* Sort entries. */
5123 if (n > 1)
5124 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5125
5126 /* Record number of overlay strings, and where we computed it. */
5127 it->n_overlay_strings = n;
5128 it->overlay_strings_charpos = charpos;
5129
5130 /* IT->current.overlay_string_index is the number of overlay strings
5131 that have already been consumed by IT. Copy some of the
5132 remaining overlay strings to IT->overlay_strings. */
5133 i = 0;
5134 j = it->current.overlay_string_index;
5135 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5136 {
5137 it->overlay_strings[i] = entries[j].string;
5138 it->string_overlays[i++] = entries[j++].overlay;
5139 }
5140
5141 CHECK_IT (it);
5142 }
5143
5144
5145 /* Get the first chunk of overlay strings at IT's current buffer
5146 position, or at CHARPOS if that is > 0. Value is non-zero if at
5147 least one overlay string was found. */
5148
5149 static int
5150 get_overlay_strings_1 (struct it *it, EMACS_INT charpos, int compute_stop_p)
5151 {
5152 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5153 process. This fills IT->overlay_strings with strings, and sets
5154 IT->n_overlay_strings to the total number of strings to process.
5155 IT->pos.overlay_string_index has to be set temporarily to zero
5156 because load_overlay_strings needs this; it must be set to -1
5157 when no overlay strings are found because a zero value would
5158 indicate a position in the first overlay string. */
5159 it->current.overlay_string_index = 0;
5160 load_overlay_strings (it, charpos);
5161
5162 /* If we found overlay strings, set up IT to deliver display
5163 elements from the first one. Otherwise set up IT to deliver
5164 from current_buffer. */
5165 if (it->n_overlay_strings)
5166 {
5167 /* Make sure we know settings in current_buffer, so that we can
5168 restore meaningful values when we're done with the overlay
5169 strings. */
5170 if (compute_stop_p)
5171 compute_stop_pos (it);
5172 xassert (it->face_id >= 0);
5173
5174 /* Save IT's settings. They are restored after all overlay
5175 strings have been processed. */
5176 xassert (!compute_stop_p || it->sp == 0);
5177
5178 /* When called from handle_stop, there might be an empty display
5179 string loaded. In that case, don't bother saving it. */
5180 if (!STRINGP (it->string) || SCHARS (it->string))
5181 push_it (it, NULL);
5182
5183 /* Set up IT to deliver display elements from the first overlay
5184 string. */
5185 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5186 it->string = it->overlay_strings[0];
5187 it->from_overlay = Qnil;
5188 it->stop_charpos = 0;
5189 xassert (STRINGP (it->string));
5190 it->end_charpos = SCHARS (it->string);
5191 it->prev_stop = 0;
5192 it->base_level_stop = 0;
5193 it->multibyte_p = STRING_MULTIBYTE (it->string);
5194 it->method = GET_FROM_STRING;
5195 it->from_disp_prop_p = 0;
5196
5197 /* Force paragraph direction to be that of the parent
5198 buffer. */
5199 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5200 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5201 else
5202 it->paragraph_embedding = L2R;
5203
5204 /* Set up the bidi iterator for this overlay string. */
5205 if (it->bidi_p)
5206 {
5207 EMACS_INT pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5208
5209 it->bidi_it.string.lstring = it->string;
5210 it->bidi_it.string.s = NULL;
5211 it->bidi_it.string.schars = SCHARS (it->string);
5212 it->bidi_it.string.bufpos = pos;
5213 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5214 it->bidi_it.string.unibyte = !it->multibyte_p;
5215 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5216 }
5217 return 1;
5218 }
5219
5220 it->current.overlay_string_index = -1;
5221 return 0;
5222 }
5223
5224 static int
5225 get_overlay_strings (struct it *it, EMACS_INT charpos)
5226 {
5227 it->string = Qnil;
5228 it->method = GET_FROM_BUFFER;
5229
5230 (void) get_overlay_strings_1 (it, charpos, 1);
5231
5232 CHECK_IT (it);
5233
5234 /* Value is non-zero if we found at least one overlay string. */
5235 return STRINGP (it->string);
5236 }
5237
5238
5239 \f
5240 /***********************************************************************
5241 Saving and restoring state
5242 ***********************************************************************/
5243
5244 /* Save current settings of IT on IT->stack. Called, for example,
5245 before setting up IT for an overlay string, to be able to restore
5246 IT's settings to what they were after the overlay string has been
5247 processed. If POSITION is non-NULL, it is the position to save on
5248 the stack instead of IT->position. */
5249
5250 static void
5251 push_it (struct it *it, struct text_pos *position)
5252 {
5253 struct iterator_stack_entry *p;
5254
5255 xassert (it->sp < IT_STACK_SIZE);
5256 p = it->stack + it->sp;
5257
5258 p->stop_charpos = it->stop_charpos;
5259 p->prev_stop = it->prev_stop;
5260 p->base_level_stop = it->base_level_stop;
5261 p->cmp_it = it->cmp_it;
5262 xassert (it->face_id >= 0);
5263 p->face_id = it->face_id;
5264 p->string = it->string;
5265 p->method = it->method;
5266 p->from_overlay = it->from_overlay;
5267 switch (p->method)
5268 {
5269 case GET_FROM_IMAGE:
5270 p->u.image.object = it->object;
5271 p->u.image.image_id = it->image_id;
5272 p->u.image.slice = it->slice;
5273 break;
5274 case GET_FROM_STRETCH:
5275 p->u.stretch.object = it->object;
5276 break;
5277 }
5278 p->position = position ? *position : it->position;
5279 p->current = it->current;
5280 p->end_charpos = it->end_charpos;
5281 p->string_nchars = it->string_nchars;
5282 p->area = it->area;
5283 p->multibyte_p = it->multibyte_p;
5284 p->avoid_cursor_p = it->avoid_cursor_p;
5285 p->space_width = it->space_width;
5286 p->font_height = it->font_height;
5287 p->voffset = it->voffset;
5288 p->string_from_display_prop_p = it->string_from_display_prop_p;
5289 p->display_ellipsis_p = 0;
5290 p->line_wrap = it->line_wrap;
5291 p->bidi_p = it->bidi_p;
5292 p->paragraph_embedding = it->paragraph_embedding;
5293 p->from_disp_prop_p = it->from_disp_prop_p;
5294 ++it->sp;
5295
5296 /* Save the state of the bidi iterator as well. */
5297 if (it->bidi_p)
5298 bidi_push_it (&it->bidi_it);
5299 }
5300
5301 static void
5302 iterate_out_of_display_property (struct it *it)
5303 {
5304 int buffer_p = BUFFERP (it->object);
5305 EMACS_INT eob = (buffer_p ? ZV : it->end_charpos);
5306 EMACS_INT bob = (buffer_p ? BEGV : 0);
5307
5308 /* Maybe initialize paragraph direction. If we are at the beginning
5309 of a new paragraph, next_element_from_buffer may not have a
5310 chance to do that. */
5311 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5312 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5313 /* prev_stop can be zero, so check against BEGV as well. */
5314 while (it->bidi_it.charpos >= bob
5315 && it->prev_stop <= it->bidi_it.charpos
5316 && it->bidi_it.charpos < CHARPOS (it->position))
5317 bidi_move_to_visually_next (&it->bidi_it);
5318 /* Record the stop_pos we just crossed, for when we cross it
5319 back, maybe. */
5320 if (it->bidi_it.charpos > CHARPOS (it->position))
5321 it->prev_stop = CHARPOS (it->position);
5322 /* If we ended up not where pop_it put us, resync IT's
5323 positional members with the bidi iterator. */
5324 if (it->bidi_it.charpos != CHARPOS (it->position))
5325 {
5326 SET_TEXT_POS (it->position,
5327 it->bidi_it.charpos, it->bidi_it.bytepos);
5328 if (buffer_p)
5329 it->current.pos = it->position;
5330 else
5331 it->current.string_pos = it->position;
5332 }
5333 }
5334
5335 /* Restore IT's settings from IT->stack. Called, for example, when no
5336 more overlay strings must be processed, and we return to delivering
5337 display elements from a buffer, or when the end of a string from a
5338 `display' property is reached and we return to delivering display
5339 elements from an overlay string, or from a buffer. */
5340
5341 static void
5342 pop_it (struct it *it)
5343 {
5344 struct iterator_stack_entry *p;
5345 int from_display_prop = it->from_disp_prop_p;
5346
5347 xassert (it->sp > 0);
5348 --it->sp;
5349 p = it->stack + it->sp;
5350 it->stop_charpos = p->stop_charpos;
5351 it->prev_stop = p->prev_stop;
5352 it->base_level_stop = p->base_level_stop;
5353 it->cmp_it = p->cmp_it;
5354 it->face_id = p->face_id;
5355 it->current = p->current;
5356 it->position = p->position;
5357 it->string = p->string;
5358 it->from_overlay = p->from_overlay;
5359 if (NILP (it->string))
5360 SET_TEXT_POS (it->current.string_pos, -1, -1);
5361 it->method = p->method;
5362 switch (it->method)
5363 {
5364 case GET_FROM_IMAGE:
5365 it->image_id = p->u.image.image_id;
5366 it->object = p->u.image.object;
5367 it->slice = p->u.image.slice;
5368 break;
5369 case GET_FROM_STRETCH:
5370 it->object = p->u.stretch.object;
5371 break;
5372 case GET_FROM_BUFFER:
5373 it->object = it->w->buffer;
5374 break;
5375 case GET_FROM_STRING:
5376 it->object = it->string;
5377 break;
5378 case GET_FROM_DISPLAY_VECTOR:
5379 if (it->s)
5380 it->method = GET_FROM_C_STRING;
5381 else if (STRINGP (it->string))
5382 it->method = GET_FROM_STRING;
5383 else
5384 {
5385 it->method = GET_FROM_BUFFER;
5386 it->object = it->w->buffer;
5387 }
5388 }
5389 it->end_charpos = p->end_charpos;
5390 it->string_nchars = p->string_nchars;
5391 it->area = p->area;
5392 it->multibyte_p = p->multibyte_p;
5393 it->avoid_cursor_p = p->avoid_cursor_p;
5394 it->space_width = p->space_width;
5395 it->font_height = p->font_height;
5396 it->voffset = p->voffset;
5397 it->string_from_display_prop_p = p->string_from_display_prop_p;
5398 it->line_wrap = p->line_wrap;
5399 it->bidi_p = p->bidi_p;
5400 it->paragraph_embedding = p->paragraph_embedding;
5401 it->from_disp_prop_p = p->from_disp_prop_p;
5402 if (it->bidi_p)
5403 {
5404 bidi_pop_it (&it->bidi_it);
5405 /* Bidi-iterate until we get out of the portion of text, if any,
5406 covered by a `display' text property or by an overlay with
5407 `display' property. (We cannot just jump there, because the
5408 internal coherency of the bidi iterator state can not be
5409 preserved across such jumps.) We also must determine the
5410 paragraph base direction if the overlay we just processed is
5411 at the beginning of a new paragraph. */
5412 if (from_display_prop
5413 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5414 iterate_out_of_display_property (it);
5415
5416 xassert ((BUFFERP (it->object)
5417 && IT_CHARPOS (*it) == it->bidi_it.charpos
5418 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5419 || (STRINGP (it->object)
5420 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5421 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos));
5422 }
5423 }
5424
5425
5426 \f
5427 /***********************************************************************
5428 Moving over lines
5429 ***********************************************************************/
5430
5431 /* Set IT's current position to the previous line start. */
5432
5433 static void
5434 back_to_previous_line_start (struct it *it)
5435 {
5436 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5437 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5438 }
5439
5440
5441 /* Move IT to the next line start.
5442
5443 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5444 we skipped over part of the text (as opposed to moving the iterator
5445 continuously over the text). Otherwise, don't change the value
5446 of *SKIPPED_P.
5447
5448 Newlines may come from buffer text, overlay strings, or strings
5449 displayed via the `display' property. That's the reason we can't
5450 simply use find_next_newline_no_quit.
5451
5452 Note that this function may not skip over invisible text that is so
5453 because of text properties and immediately follows a newline. If
5454 it would, function reseat_at_next_visible_line_start, when called
5455 from set_iterator_to_next, would effectively make invisible
5456 characters following a newline part of the wrong glyph row, which
5457 leads to wrong cursor motion. */
5458
5459 static int
5460 forward_to_next_line_start (struct it *it, int *skipped_p)
5461 {
5462 int old_selective, newline_found_p, n;
5463 const int MAX_NEWLINE_DISTANCE = 500;
5464
5465 /* If already on a newline, just consume it to avoid unintended
5466 skipping over invisible text below. */
5467 if (it->what == IT_CHARACTER
5468 && it->c == '\n'
5469 && CHARPOS (it->position) == IT_CHARPOS (*it))
5470 {
5471 set_iterator_to_next (it, 0);
5472 it->c = 0;
5473 return 1;
5474 }
5475
5476 /* Don't handle selective display in the following. It's (a)
5477 unnecessary because it's done by the caller, and (b) leads to an
5478 infinite recursion because next_element_from_ellipsis indirectly
5479 calls this function. */
5480 old_selective = it->selective;
5481 it->selective = 0;
5482
5483 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5484 from buffer text. */
5485 for (n = newline_found_p = 0;
5486 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5487 n += STRINGP (it->string) ? 0 : 1)
5488 {
5489 if (!get_next_display_element (it))
5490 return 0;
5491 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5492 set_iterator_to_next (it, 0);
5493 }
5494
5495 /* If we didn't find a newline near enough, see if we can use a
5496 short-cut. */
5497 if (!newline_found_p)
5498 {
5499 EMACS_INT start = IT_CHARPOS (*it);
5500 EMACS_INT limit = find_next_newline_no_quit (start, 1);
5501 Lisp_Object pos;
5502
5503 xassert (!STRINGP (it->string));
5504
5505 /* If we are not bidi-reordering, and there isn't any `display'
5506 property in sight, and no overlays, we can just use the
5507 position of the newline in buffer text. */
5508 if (!it->bidi_p
5509 && (it->stop_charpos >= limit
5510 || ((pos = Fnext_single_property_change (make_number (start),
5511 Qdisplay, Qnil,
5512 make_number (limit)),
5513 NILP (pos))
5514 && next_overlay_change (start) == ZV)))
5515 {
5516 IT_CHARPOS (*it) = limit;
5517 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5518 *skipped_p = newline_found_p = 1;
5519 }
5520 else
5521 {
5522 while (get_next_display_element (it)
5523 && !newline_found_p)
5524 {
5525 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5526 set_iterator_to_next (it, 0);
5527 }
5528 }
5529 }
5530
5531 it->selective = old_selective;
5532 return newline_found_p;
5533 }
5534
5535
5536 /* Set IT's current position to the previous visible line start. Skip
5537 invisible text that is so either due to text properties or due to
5538 selective display. Caution: this does not change IT->current_x and
5539 IT->hpos. */
5540
5541 static void
5542 back_to_previous_visible_line_start (struct it *it)
5543 {
5544 while (IT_CHARPOS (*it) > BEGV)
5545 {
5546 back_to_previous_line_start (it);
5547
5548 if (IT_CHARPOS (*it) <= BEGV)
5549 break;
5550
5551 /* If selective > 0, then lines indented more than its value are
5552 invisible. */
5553 if (it->selective > 0
5554 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5555 (double) it->selective)) /* iftc */
5556 continue;
5557
5558 /* Check the newline before point for invisibility. */
5559 {
5560 Lisp_Object prop;
5561 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5562 Qinvisible, it->window);
5563 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5564 continue;
5565 }
5566
5567 if (IT_CHARPOS (*it) <= BEGV)
5568 break;
5569
5570 {
5571 struct it it2;
5572 void *it2data = NULL;
5573 EMACS_INT pos;
5574 EMACS_INT beg, end;
5575 Lisp_Object val, overlay;
5576
5577 SAVE_IT (it2, *it, it2data);
5578
5579 /* If newline is part of a composition, continue from start of composition */
5580 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5581 && beg < IT_CHARPOS (*it))
5582 goto replaced;
5583
5584 /* If newline is replaced by a display property, find start of overlay
5585 or interval and continue search from that point. */
5586 pos = --IT_CHARPOS (it2);
5587 --IT_BYTEPOS (it2);
5588 it2.sp = 0;
5589 bidi_unshelve_cache (NULL);
5590 it2.string_from_display_prop_p = 0;
5591 it2.from_disp_prop_p = 0;
5592 if (handle_display_prop (&it2) == HANDLED_RETURN
5593 && !NILP (val = get_char_property_and_overlay
5594 (make_number (pos), Qdisplay, Qnil, &overlay))
5595 && (OVERLAYP (overlay)
5596 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5597 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5598 {
5599 RESTORE_IT (it, it, it2data);
5600 goto replaced;
5601 }
5602
5603 /* Newline is not replaced by anything -- so we are done. */
5604 RESTORE_IT (it, it, it2data);
5605 break;
5606
5607 replaced:
5608 if (beg < BEGV)
5609 beg = BEGV;
5610 IT_CHARPOS (*it) = beg;
5611 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5612 }
5613 }
5614
5615 it->continuation_lines_width = 0;
5616
5617 xassert (IT_CHARPOS (*it) >= BEGV);
5618 xassert (IT_CHARPOS (*it) == BEGV
5619 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5620 CHECK_IT (it);
5621 }
5622
5623
5624 /* Reseat iterator IT at the previous visible line start. Skip
5625 invisible text that is so either due to text properties or due to
5626 selective display. At the end, update IT's overlay information,
5627 face information etc. */
5628
5629 void
5630 reseat_at_previous_visible_line_start (struct it *it)
5631 {
5632 back_to_previous_visible_line_start (it);
5633 reseat (it, it->current.pos, 1);
5634 CHECK_IT (it);
5635 }
5636
5637
5638 /* Reseat iterator IT on the next visible line start in the current
5639 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5640 preceding the line start. Skip over invisible text that is so
5641 because of selective display. Compute faces, overlays etc at the
5642 new position. Note that this function does not skip over text that
5643 is invisible because of text properties. */
5644
5645 static void
5646 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5647 {
5648 int newline_found_p, skipped_p = 0;
5649
5650 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5651
5652 /* Skip over lines that are invisible because they are indented
5653 more than the value of IT->selective. */
5654 if (it->selective > 0)
5655 while (IT_CHARPOS (*it) < ZV
5656 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5657 (double) it->selective)) /* iftc */
5658 {
5659 xassert (IT_BYTEPOS (*it) == BEGV
5660 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5661 newline_found_p = forward_to_next_line_start (it, &skipped_p);
5662 }
5663
5664 /* Position on the newline if that's what's requested. */
5665 if (on_newline_p && newline_found_p)
5666 {
5667 if (STRINGP (it->string))
5668 {
5669 if (IT_STRING_CHARPOS (*it) > 0)
5670 {
5671 if (!it->bidi_p)
5672 {
5673 --IT_STRING_CHARPOS (*it);
5674 --IT_STRING_BYTEPOS (*it);
5675 }
5676 else
5677 /* Setting this flag will cause
5678 bidi_move_to_visually_next not to advance, but
5679 instead deliver the current character (newline),
5680 which is what the ON_NEWLINE_P flag wants. */
5681 it->bidi_it.first_elt = 1;
5682 }
5683 }
5684 else if (IT_CHARPOS (*it) > BEGV)
5685 {
5686 if (!it->bidi_p)
5687 {
5688 --IT_CHARPOS (*it);
5689 --IT_BYTEPOS (*it);
5690 }
5691 /* With bidi iteration, the call to `reseat' will cause
5692 bidi_move_to_visually_next deliver the current character,
5693 the newline, instead of advancing. */
5694 reseat (it, it->current.pos, 0);
5695 }
5696 }
5697 else if (skipped_p)
5698 reseat (it, it->current.pos, 0);
5699
5700 CHECK_IT (it);
5701 }
5702
5703
5704 \f
5705 /***********************************************************************
5706 Changing an iterator's position
5707 ***********************************************************************/
5708
5709 /* Change IT's current position to POS in current_buffer. If FORCE_P
5710 is non-zero, always check for text properties at the new position.
5711 Otherwise, text properties are only looked up if POS >=
5712 IT->check_charpos of a property. */
5713
5714 static void
5715 reseat (struct it *it, struct text_pos pos, int force_p)
5716 {
5717 EMACS_INT original_pos = IT_CHARPOS (*it);
5718
5719 reseat_1 (it, pos, 0);
5720
5721 /* Determine where to check text properties. Avoid doing it
5722 where possible because text property lookup is very expensive. */
5723 if (force_p
5724 || CHARPOS (pos) > it->stop_charpos
5725 || CHARPOS (pos) < original_pos)
5726 {
5727 if (it->bidi_p)
5728 {
5729 /* For bidi iteration, we need to prime prev_stop and
5730 base_level_stop with our best estimations. */
5731 if (CHARPOS (pos) < it->prev_stop)
5732 {
5733 handle_stop_backwards (it, BEGV);
5734 if (CHARPOS (pos) < it->base_level_stop)
5735 it->base_level_stop = 0;
5736 }
5737 else if (CHARPOS (pos) > it->stop_charpos
5738 && it->stop_charpos >= BEGV)
5739 handle_stop_backwards (it, it->stop_charpos);
5740 else /* force_p */
5741 handle_stop (it);
5742 }
5743 else
5744 {
5745 handle_stop (it);
5746 it->prev_stop = it->base_level_stop = 0;
5747 }
5748
5749 }
5750
5751 CHECK_IT (it);
5752 }
5753
5754
5755 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
5756 IT->stop_pos to POS, also. */
5757
5758 static void
5759 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
5760 {
5761 /* Don't call this function when scanning a C string. */
5762 xassert (it->s == NULL);
5763
5764 /* POS must be a reasonable value. */
5765 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
5766
5767 it->current.pos = it->position = pos;
5768 it->end_charpos = ZV;
5769 it->dpvec = NULL;
5770 it->current.dpvec_index = -1;
5771 it->current.overlay_string_index = -1;
5772 IT_STRING_CHARPOS (*it) = -1;
5773 IT_STRING_BYTEPOS (*it) = -1;
5774 it->string = Qnil;
5775 it->method = GET_FROM_BUFFER;
5776 it->object = it->w->buffer;
5777 it->area = TEXT_AREA;
5778 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
5779 it->sp = 0;
5780 it->string_from_display_prop_p = 0;
5781 it->from_disp_prop_p = 0;
5782 it->face_before_selective_p = 0;
5783 if (it->bidi_p)
5784 {
5785 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
5786 &it->bidi_it);
5787 bidi_unshelve_cache (NULL);
5788 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5789 it->bidi_it.string.s = NULL;
5790 it->bidi_it.string.lstring = Qnil;
5791 it->bidi_it.string.bufpos = 0;
5792 it->bidi_it.string.unibyte = 0;
5793 }
5794
5795 if (set_stop_p)
5796 {
5797 it->stop_charpos = CHARPOS (pos);
5798 it->base_level_stop = CHARPOS (pos);
5799 }
5800 }
5801
5802
5803 /* Set up IT for displaying a string, starting at CHARPOS in window W.
5804 If S is non-null, it is a C string to iterate over. Otherwise,
5805 STRING gives a Lisp string to iterate over.
5806
5807 If PRECISION > 0, don't return more then PRECISION number of
5808 characters from the string.
5809
5810 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
5811 characters have been returned. FIELD_WIDTH < 0 means an infinite
5812 field width.
5813
5814 MULTIBYTE = 0 means disable processing of multibyte characters,
5815 MULTIBYTE > 0 means enable it,
5816 MULTIBYTE < 0 means use IT->multibyte_p.
5817
5818 IT must be initialized via a prior call to init_iterator before
5819 calling this function. */
5820
5821 static void
5822 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
5823 EMACS_INT charpos, EMACS_INT precision, int field_width,
5824 int multibyte)
5825 {
5826 /* No region in strings. */
5827 it->region_beg_charpos = it->region_end_charpos = -1;
5828
5829 /* No text property checks performed by default, but see below. */
5830 it->stop_charpos = -1;
5831
5832 /* Set iterator position and end position. */
5833 memset (&it->current, 0, sizeof it->current);
5834 it->current.overlay_string_index = -1;
5835 it->current.dpvec_index = -1;
5836 xassert (charpos >= 0);
5837
5838 /* If STRING is specified, use its multibyteness, otherwise use the
5839 setting of MULTIBYTE, if specified. */
5840 if (multibyte >= 0)
5841 it->multibyte_p = multibyte > 0;
5842
5843 /* Bidirectional reordering of strings is controlled by the default
5844 value of bidi-display-reordering. */
5845 it->bidi_p = !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
5846
5847 if (s == NULL)
5848 {
5849 xassert (STRINGP (string));
5850 it->string = string;
5851 it->s = NULL;
5852 it->end_charpos = it->string_nchars = SCHARS (string);
5853 it->method = GET_FROM_STRING;
5854 it->current.string_pos = string_pos (charpos, string);
5855
5856 if (it->bidi_p)
5857 {
5858 it->bidi_it.string.lstring = string;
5859 it->bidi_it.string.s = NULL;
5860 it->bidi_it.string.schars = it->end_charpos;
5861 it->bidi_it.string.bufpos = 0;
5862 it->bidi_it.string.from_disp_str = 0;
5863 it->bidi_it.string.unibyte = !it->multibyte_p;
5864 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
5865 FRAME_WINDOW_P (it->f), &it->bidi_it);
5866 }
5867 }
5868 else
5869 {
5870 it->s = (const unsigned char *) s;
5871 it->string = Qnil;
5872
5873 /* Note that we use IT->current.pos, not it->current.string_pos,
5874 for displaying C strings. */
5875 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
5876 if (it->multibyte_p)
5877 {
5878 it->current.pos = c_string_pos (charpos, s, 1);
5879 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
5880 }
5881 else
5882 {
5883 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
5884 it->end_charpos = it->string_nchars = strlen (s);
5885 }
5886
5887 if (it->bidi_p)
5888 {
5889 it->bidi_it.string.lstring = Qnil;
5890 it->bidi_it.string.s = s;
5891 it->bidi_it.string.schars = it->end_charpos;
5892 it->bidi_it.string.bufpos = 0;
5893 it->bidi_it.string.from_disp_str = 0;
5894 it->bidi_it.string.unibyte = !it->multibyte_p;
5895 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
5896 &it->bidi_it);
5897 }
5898 it->method = GET_FROM_C_STRING;
5899 }
5900
5901 /* PRECISION > 0 means don't return more than PRECISION characters
5902 from the string. */
5903 if (precision > 0 && it->end_charpos - charpos > precision)
5904 {
5905 it->end_charpos = it->string_nchars = charpos + precision;
5906 if (it->bidi_p)
5907 it->bidi_it.string.schars = it->end_charpos;
5908 }
5909
5910 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
5911 characters have been returned. FIELD_WIDTH == 0 means don't pad,
5912 FIELD_WIDTH < 0 means infinite field width. This is useful for
5913 padding with `-' at the end of a mode line. */
5914 if (field_width < 0)
5915 field_width = INFINITY;
5916 /* Implementation note: We deliberately don't enlarge
5917 it->bidi_it.string.schars here to fit it->end_charpos, because
5918 the bidi iterator cannot produce characters out of thin air. */
5919 if (field_width > it->end_charpos - charpos)
5920 it->end_charpos = charpos + field_width;
5921
5922 /* Use the standard display table for displaying strings. */
5923 if (DISP_TABLE_P (Vstandard_display_table))
5924 it->dp = XCHAR_TABLE (Vstandard_display_table);
5925
5926 it->stop_charpos = charpos;
5927 it->prev_stop = charpos;
5928 it->base_level_stop = 0;
5929 if (it->bidi_p)
5930 {
5931 it->bidi_it.first_elt = 1;
5932 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
5933 it->bidi_it.disp_pos = -1;
5934 }
5935 if (s == NULL && it->multibyte_p)
5936 {
5937 EMACS_INT endpos = SCHARS (it->string);
5938 if (endpos > it->end_charpos)
5939 endpos = it->end_charpos;
5940 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
5941 it->string);
5942 }
5943 CHECK_IT (it);
5944 }
5945
5946
5947 \f
5948 /***********************************************************************
5949 Iteration
5950 ***********************************************************************/
5951
5952 /* Map enum it_method value to corresponding next_element_from_* function. */
5953
5954 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
5955 {
5956 next_element_from_buffer,
5957 next_element_from_display_vector,
5958 next_element_from_string,
5959 next_element_from_c_string,
5960 next_element_from_image,
5961 next_element_from_stretch
5962 };
5963
5964 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
5965
5966
5967 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
5968 (possibly with the following characters). */
5969
5970 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
5971 ((IT)->cmp_it.id >= 0 \
5972 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
5973 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
5974 END_CHARPOS, (IT)->w, \
5975 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
5976 (IT)->string)))
5977
5978
5979 /* Lookup the char-table Vglyphless_char_display for character C (-1
5980 if we want information for no-font case), and return the display
5981 method symbol. By side-effect, update it->what and
5982 it->glyphless_method. This function is called from
5983 get_next_display_element for each character element, and from
5984 x_produce_glyphs when no suitable font was found. */
5985
5986 Lisp_Object
5987 lookup_glyphless_char_display (int c, struct it *it)
5988 {
5989 Lisp_Object glyphless_method = Qnil;
5990
5991 if (CHAR_TABLE_P (Vglyphless_char_display)
5992 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
5993 {
5994 if (c >= 0)
5995 {
5996 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
5997 if (CONSP (glyphless_method))
5998 glyphless_method = FRAME_WINDOW_P (it->f)
5999 ? XCAR (glyphless_method)
6000 : XCDR (glyphless_method);
6001 }
6002 else
6003 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6004 }
6005
6006 retry:
6007 if (NILP (glyphless_method))
6008 {
6009 if (c >= 0)
6010 /* The default is to display the character by a proper font. */
6011 return Qnil;
6012 /* The default for the no-font case is to display an empty box. */
6013 glyphless_method = Qempty_box;
6014 }
6015 if (EQ (glyphless_method, Qzero_width))
6016 {
6017 if (c >= 0)
6018 return glyphless_method;
6019 /* This method can't be used for the no-font case. */
6020 glyphless_method = Qempty_box;
6021 }
6022 if (EQ (glyphless_method, Qthin_space))
6023 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6024 else if (EQ (glyphless_method, Qempty_box))
6025 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6026 else if (EQ (glyphless_method, Qhex_code))
6027 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6028 else if (STRINGP (glyphless_method))
6029 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6030 else
6031 {
6032 /* Invalid value. We use the default method. */
6033 glyphless_method = Qnil;
6034 goto retry;
6035 }
6036 it->what = IT_GLYPHLESS;
6037 return glyphless_method;
6038 }
6039
6040 /* Load IT's display element fields with information about the next
6041 display element from the current position of IT. Value is zero if
6042 end of buffer (or C string) is reached. */
6043
6044 static struct frame *last_escape_glyph_frame = NULL;
6045 static unsigned last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6046 static int last_escape_glyph_merged_face_id = 0;
6047
6048 struct frame *last_glyphless_glyph_frame = NULL;
6049 unsigned last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6050 int last_glyphless_glyph_merged_face_id = 0;
6051
6052 static int
6053 get_next_display_element (struct it *it)
6054 {
6055 /* Non-zero means that we found a display element. Zero means that
6056 we hit the end of what we iterate over. Performance note: the
6057 function pointer `method' used here turns out to be faster than
6058 using a sequence of if-statements. */
6059 int success_p;
6060
6061 get_next:
6062 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6063
6064 if (it->what == IT_CHARACTER)
6065 {
6066 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6067 and only if (a) the resolved directionality of that character
6068 is R..." */
6069 /* FIXME: Do we need an exception for characters from display
6070 tables? */
6071 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6072 it->c = bidi_mirror_char (it->c);
6073 /* Map via display table or translate control characters.
6074 IT->c, IT->len etc. have been set to the next character by
6075 the function call above. If we have a display table, and it
6076 contains an entry for IT->c, translate it. Don't do this if
6077 IT->c itself comes from a display table, otherwise we could
6078 end up in an infinite recursion. (An alternative could be to
6079 count the recursion depth of this function and signal an
6080 error when a certain maximum depth is reached.) Is it worth
6081 it? */
6082 if (success_p && it->dpvec == NULL)
6083 {
6084 Lisp_Object dv;
6085 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6086 enum { char_is_other = 0, char_is_nbsp, char_is_soft_hyphen }
6087 nbsp_or_shy = char_is_other;
6088 int c = it->c; /* This is the character to display. */
6089
6090 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6091 {
6092 xassert (SINGLE_BYTE_CHAR_P (c));
6093 if (unibyte_display_via_language_environment)
6094 {
6095 c = DECODE_CHAR (unibyte, c);
6096 if (c < 0)
6097 c = BYTE8_TO_CHAR (it->c);
6098 }
6099 else
6100 c = BYTE8_TO_CHAR (it->c);
6101 }
6102
6103 if (it->dp
6104 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6105 VECTORP (dv)))
6106 {
6107 struct Lisp_Vector *v = XVECTOR (dv);
6108
6109 /* Return the first character from the display table
6110 entry, if not empty. If empty, don't display the
6111 current character. */
6112 if (v->header.size)
6113 {
6114 it->dpvec_char_len = it->len;
6115 it->dpvec = v->contents;
6116 it->dpend = v->contents + v->header.size;
6117 it->current.dpvec_index = 0;
6118 it->dpvec_face_id = -1;
6119 it->saved_face_id = it->face_id;
6120 it->method = GET_FROM_DISPLAY_VECTOR;
6121 it->ellipsis_p = 0;
6122 }
6123 else
6124 {
6125 set_iterator_to_next (it, 0);
6126 }
6127 goto get_next;
6128 }
6129
6130 if (! NILP (lookup_glyphless_char_display (c, it)))
6131 {
6132 if (it->what == IT_GLYPHLESS)
6133 goto done;
6134 /* Don't display this character. */
6135 set_iterator_to_next (it, 0);
6136 goto get_next;
6137 }
6138
6139 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6140 nbsp_or_shy = (c == 0xA0 ? char_is_nbsp
6141 : c == 0xAD ? char_is_soft_hyphen
6142 : char_is_other);
6143
6144 /* Translate control characters into `\003' or `^C' form.
6145 Control characters coming from a display table entry are
6146 currently not translated because we use IT->dpvec to hold
6147 the translation. This could easily be changed but I
6148 don't believe that it is worth doing.
6149
6150 NBSP and SOFT-HYPEN are property translated too.
6151
6152 Non-printable characters and raw-byte characters are also
6153 translated to octal form. */
6154 if (((c < ' ' || c == 127) /* ASCII control chars */
6155 ? (it->area != TEXT_AREA
6156 /* In mode line, treat \n, \t like other crl chars. */
6157 || (c != '\t'
6158 && it->glyph_row
6159 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6160 || (c != '\n' && c != '\t'))
6161 : (nbsp_or_shy
6162 || CHAR_BYTE8_P (c)
6163 || ! CHAR_PRINTABLE_P (c))))
6164 {
6165 /* C is a control character, NBSP, SOFT-HYPEN, raw-byte,
6166 or a non-printable character which must be displayed
6167 either as '\003' or as `^C' where the '\\' and '^'
6168 can be defined in the display table. Fill
6169 IT->ctl_chars with glyphs for what we have to
6170 display. Then, set IT->dpvec to these glyphs. */
6171 Lisp_Object gc;
6172 int ctl_len;
6173 int face_id, lface_id = 0 ;
6174 int escape_glyph;
6175
6176 /* Handle control characters with ^. */
6177
6178 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6179 {
6180 int g;
6181
6182 g = '^'; /* default glyph for Control */
6183 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6184 if (it->dp
6185 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc))
6186 && GLYPH_CODE_CHAR_VALID_P (gc))
6187 {
6188 g = GLYPH_CODE_CHAR (gc);
6189 lface_id = GLYPH_CODE_FACE (gc);
6190 }
6191 if (lface_id)
6192 {
6193 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6194 }
6195 else if (it->f == last_escape_glyph_frame
6196 && it->face_id == last_escape_glyph_face_id)
6197 {
6198 face_id = last_escape_glyph_merged_face_id;
6199 }
6200 else
6201 {
6202 /* Merge the escape-glyph face into the current face. */
6203 face_id = merge_faces (it->f, Qescape_glyph, 0,
6204 it->face_id);
6205 last_escape_glyph_frame = it->f;
6206 last_escape_glyph_face_id = it->face_id;
6207 last_escape_glyph_merged_face_id = face_id;
6208 }
6209
6210 XSETINT (it->ctl_chars[0], g);
6211 XSETINT (it->ctl_chars[1], c ^ 0100);
6212 ctl_len = 2;
6213 goto display_control;
6214 }
6215
6216 /* Handle non-break space in the mode where it only gets
6217 highlighting. */
6218
6219 if (EQ (Vnobreak_char_display, Qt)
6220 && nbsp_or_shy == char_is_nbsp)
6221 {
6222 /* Merge the no-break-space face into the current face. */
6223 face_id = merge_faces (it->f, Qnobreak_space, 0,
6224 it->face_id);
6225
6226 c = ' ';
6227 XSETINT (it->ctl_chars[0], ' ');
6228 ctl_len = 1;
6229 goto display_control;
6230 }
6231
6232 /* Handle sequences that start with the "escape glyph". */
6233
6234 /* the default escape glyph is \. */
6235 escape_glyph = '\\';
6236
6237 if (it->dp
6238 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc))
6239 && GLYPH_CODE_CHAR_VALID_P (gc))
6240 {
6241 escape_glyph = GLYPH_CODE_CHAR (gc);
6242 lface_id = GLYPH_CODE_FACE (gc);
6243 }
6244 if (lface_id)
6245 {
6246 /* The display table specified a face.
6247 Merge it into face_id and also into escape_glyph. */
6248 face_id = merge_faces (it->f, Qt, lface_id,
6249 it->face_id);
6250 }
6251 else if (it->f == last_escape_glyph_frame
6252 && it->face_id == last_escape_glyph_face_id)
6253 {
6254 face_id = last_escape_glyph_merged_face_id;
6255 }
6256 else
6257 {
6258 /* Merge the escape-glyph face into the current face. */
6259 face_id = merge_faces (it->f, Qescape_glyph, 0,
6260 it->face_id);
6261 last_escape_glyph_frame = it->f;
6262 last_escape_glyph_face_id = it->face_id;
6263 last_escape_glyph_merged_face_id = face_id;
6264 }
6265
6266 /* Handle soft hyphens in the mode where they only get
6267 highlighting. */
6268
6269 if (EQ (Vnobreak_char_display, Qt)
6270 && nbsp_or_shy == char_is_soft_hyphen)
6271 {
6272 XSETINT (it->ctl_chars[0], '-');
6273 ctl_len = 1;
6274 goto display_control;
6275 }
6276
6277 /* Handle non-break space and soft hyphen
6278 with the escape glyph. */
6279
6280 if (nbsp_or_shy)
6281 {
6282 XSETINT (it->ctl_chars[0], escape_glyph);
6283 c = (nbsp_or_shy == char_is_nbsp ? ' ' : '-');
6284 XSETINT (it->ctl_chars[1], c);
6285 ctl_len = 2;
6286 goto display_control;
6287 }
6288
6289 {
6290 char str[10];
6291 int len, i;
6292
6293 if (CHAR_BYTE8_P (c))
6294 /* Display \200 instead of \17777600. */
6295 c = CHAR_TO_BYTE8 (c);
6296 len = sprintf (str, "%03o", c);
6297
6298 XSETINT (it->ctl_chars[0], escape_glyph);
6299 for (i = 0; i < len; i++)
6300 XSETINT (it->ctl_chars[i + 1], str[i]);
6301 ctl_len = len + 1;
6302 }
6303
6304 display_control:
6305 /* Set up IT->dpvec and return first character from it. */
6306 it->dpvec_char_len = it->len;
6307 it->dpvec = it->ctl_chars;
6308 it->dpend = it->dpvec + ctl_len;
6309 it->current.dpvec_index = 0;
6310 it->dpvec_face_id = face_id;
6311 it->saved_face_id = it->face_id;
6312 it->method = GET_FROM_DISPLAY_VECTOR;
6313 it->ellipsis_p = 0;
6314 goto get_next;
6315 }
6316 it->char_to_display = c;
6317 }
6318 else if (success_p)
6319 {
6320 it->char_to_display = it->c;
6321 }
6322 }
6323
6324 /* Adjust face id for a multibyte character. There are no multibyte
6325 character in unibyte text. */
6326 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6327 && it->multibyte_p
6328 && success_p
6329 && FRAME_WINDOW_P (it->f))
6330 {
6331 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6332
6333 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6334 {
6335 /* Automatic composition with glyph-string. */
6336 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6337
6338 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6339 }
6340 else
6341 {
6342 EMACS_INT pos = (it->s ? -1
6343 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6344 : IT_CHARPOS (*it));
6345
6346 it->face_id = FACE_FOR_CHAR (it->f, face, it->char_to_display, pos,
6347 it->string);
6348 }
6349 }
6350
6351 done:
6352 /* Is this character the last one of a run of characters with
6353 box? If yes, set IT->end_of_box_run_p to 1. */
6354 if (it->face_box_p
6355 && it->s == NULL)
6356 {
6357 if (it->method == GET_FROM_STRING && it->sp)
6358 {
6359 int face_id = underlying_face_id (it);
6360 struct face *face = FACE_FROM_ID (it->f, face_id);
6361
6362 if (face)
6363 {
6364 if (face->box == FACE_NO_BOX)
6365 {
6366 /* If the box comes from face properties in a
6367 display string, check faces in that string. */
6368 int string_face_id = face_after_it_pos (it);
6369 it->end_of_box_run_p
6370 = (FACE_FROM_ID (it->f, string_face_id)->box
6371 == FACE_NO_BOX);
6372 }
6373 /* Otherwise, the box comes from the underlying face.
6374 If this is the last string character displayed, check
6375 the next buffer location. */
6376 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6377 && (it->current.overlay_string_index
6378 == it->n_overlay_strings - 1))
6379 {
6380 EMACS_INT ignore;
6381 int next_face_id;
6382 struct text_pos pos = it->current.pos;
6383 INC_TEXT_POS (pos, it->multibyte_p);
6384
6385 next_face_id = face_at_buffer_position
6386 (it->w, CHARPOS (pos), it->region_beg_charpos,
6387 it->region_end_charpos, &ignore,
6388 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6389 -1);
6390 it->end_of_box_run_p
6391 = (FACE_FROM_ID (it->f, next_face_id)->box
6392 == FACE_NO_BOX);
6393 }
6394 }
6395 }
6396 else
6397 {
6398 int face_id = face_after_it_pos (it);
6399 it->end_of_box_run_p
6400 = (face_id != it->face_id
6401 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6402 }
6403 }
6404
6405 /* Value is 0 if end of buffer or string reached. */
6406 return success_p;
6407 }
6408
6409
6410 /* Move IT to the next display element.
6411
6412 RESEAT_P non-zero means if called on a newline in buffer text,
6413 skip to the next visible line start.
6414
6415 Functions get_next_display_element and set_iterator_to_next are
6416 separate because I find this arrangement easier to handle than a
6417 get_next_display_element function that also increments IT's
6418 position. The way it is we can first look at an iterator's current
6419 display element, decide whether it fits on a line, and if it does,
6420 increment the iterator position. The other way around we probably
6421 would either need a flag indicating whether the iterator has to be
6422 incremented the next time, or we would have to implement a
6423 decrement position function which would not be easy to write. */
6424
6425 void
6426 set_iterator_to_next (struct it *it, int reseat_p)
6427 {
6428 /* Reset flags indicating start and end of a sequence of characters
6429 with box. Reset them at the start of this function because
6430 moving the iterator to a new position might set them. */
6431 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6432
6433 switch (it->method)
6434 {
6435 case GET_FROM_BUFFER:
6436 /* The current display element of IT is a character from
6437 current_buffer. Advance in the buffer, and maybe skip over
6438 invisible lines that are so because of selective display. */
6439 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6440 reseat_at_next_visible_line_start (it, 0);
6441 else if (it->cmp_it.id >= 0)
6442 {
6443 /* We are currently getting glyphs from a composition. */
6444 int i;
6445
6446 if (! it->bidi_p)
6447 {
6448 IT_CHARPOS (*it) += it->cmp_it.nchars;
6449 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6450 if (it->cmp_it.to < it->cmp_it.nglyphs)
6451 {
6452 it->cmp_it.from = it->cmp_it.to;
6453 }
6454 else
6455 {
6456 it->cmp_it.id = -1;
6457 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6458 IT_BYTEPOS (*it),
6459 it->end_charpos, Qnil);
6460 }
6461 }
6462 else if (! it->cmp_it.reversed_p)
6463 {
6464 /* Composition created while scanning forward. */
6465 /* Update IT's char/byte positions to point to the first
6466 character of the next grapheme cluster, or to the
6467 character visually after the current composition. */
6468 for (i = 0; i < it->cmp_it.nchars; i++)
6469 bidi_move_to_visually_next (&it->bidi_it);
6470 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6471 IT_CHARPOS (*it) = it->bidi_it.charpos;
6472
6473 if (it->cmp_it.to < it->cmp_it.nglyphs)
6474 {
6475 /* Proceed to the next grapheme cluster. */
6476 it->cmp_it.from = it->cmp_it.to;
6477 }
6478 else
6479 {
6480 /* No more grapheme clusters in this composition.
6481 Find the next stop position. */
6482 EMACS_INT stop = it->end_charpos;
6483 if (it->bidi_it.scan_dir < 0)
6484 /* Now we are scanning backward and don't know
6485 where to stop. */
6486 stop = -1;
6487 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6488 IT_BYTEPOS (*it), stop, Qnil);
6489 }
6490 }
6491 else
6492 {
6493 /* Composition created while scanning backward. */
6494 /* Update IT's char/byte positions to point to the last
6495 character of the previous grapheme cluster, or the
6496 character visually after the current composition. */
6497 for (i = 0; i < it->cmp_it.nchars; i++)
6498 bidi_move_to_visually_next (&it->bidi_it);
6499 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6500 IT_CHARPOS (*it) = it->bidi_it.charpos;
6501 if (it->cmp_it.from > 0)
6502 {
6503 /* Proceed to the previous grapheme cluster. */
6504 it->cmp_it.to = it->cmp_it.from;
6505 }
6506 else
6507 {
6508 /* No more grapheme clusters in this composition.
6509 Find the next stop position. */
6510 EMACS_INT stop = it->end_charpos;
6511 if (it->bidi_it.scan_dir < 0)
6512 /* Now we are scanning backward and don't know
6513 where to stop. */
6514 stop = -1;
6515 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6516 IT_BYTEPOS (*it), stop, Qnil);
6517 }
6518 }
6519 }
6520 else
6521 {
6522 xassert (it->len != 0);
6523
6524 if (!it->bidi_p)
6525 {
6526 IT_BYTEPOS (*it) += it->len;
6527 IT_CHARPOS (*it) += 1;
6528 }
6529 else
6530 {
6531 int prev_scan_dir = it->bidi_it.scan_dir;
6532 /* If this is a new paragraph, determine its base
6533 direction (a.k.a. its base embedding level). */
6534 if (it->bidi_it.new_paragraph)
6535 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6536 bidi_move_to_visually_next (&it->bidi_it);
6537 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6538 IT_CHARPOS (*it) = it->bidi_it.charpos;
6539 if (prev_scan_dir != it->bidi_it.scan_dir)
6540 {
6541 /* As the scan direction was changed, we must
6542 re-compute the stop position for composition. */
6543 EMACS_INT stop = it->end_charpos;
6544 if (it->bidi_it.scan_dir < 0)
6545 stop = -1;
6546 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6547 IT_BYTEPOS (*it), stop, Qnil);
6548 }
6549 }
6550 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6551 }
6552 break;
6553
6554 case GET_FROM_C_STRING:
6555 /* Current display element of IT is from a C string. */
6556 if (!it->bidi_p
6557 /* If the string position is beyond string's end, it means
6558 next_element_from_c_string is padding the string with
6559 blanks, in which case we bypass the bidi iterator,
6560 because it cannot deal with such virtual characters. */
6561 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
6562 {
6563 IT_BYTEPOS (*it) += it->len;
6564 IT_CHARPOS (*it) += 1;
6565 }
6566 else
6567 {
6568 bidi_move_to_visually_next (&it->bidi_it);
6569 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6570 IT_CHARPOS (*it) = it->bidi_it.charpos;
6571 }
6572 break;
6573
6574 case GET_FROM_DISPLAY_VECTOR:
6575 /* Current display element of IT is from a display table entry.
6576 Advance in the display table definition. Reset it to null if
6577 end reached, and continue with characters from buffers/
6578 strings. */
6579 ++it->current.dpvec_index;
6580
6581 /* Restore face of the iterator to what they were before the
6582 display vector entry (these entries may contain faces). */
6583 it->face_id = it->saved_face_id;
6584
6585 if (it->dpvec + it->current.dpvec_index == it->dpend)
6586 {
6587 int recheck_faces = it->ellipsis_p;
6588
6589 if (it->s)
6590 it->method = GET_FROM_C_STRING;
6591 else if (STRINGP (it->string))
6592 it->method = GET_FROM_STRING;
6593 else
6594 {
6595 it->method = GET_FROM_BUFFER;
6596 it->object = it->w->buffer;
6597 }
6598
6599 it->dpvec = NULL;
6600 it->current.dpvec_index = -1;
6601
6602 /* Skip over characters which were displayed via IT->dpvec. */
6603 if (it->dpvec_char_len < 0)
6604 reseat_at_next_visible_line_start (it, 1);
6605 else if (it->dpvec_char_len > 0)
6606 {
6607 if (it->method == GET_FROM_STRING
6608 && it->n_overlay_strings > 0)
6609 it->ignore_overlay_strings_at_pos_p = 1;
6610 it->len = it->dpvec_char_len;
6611 set_iterator_to_next (it, reseat_p);
6612 }
6613
6614 /* Maybe recheck faces after display vector */
6615 if (recheck_faces)
6616 it->stop_charpos = IT_CHARPOS (*it);
6617 }
6618 break;
6619
6620 case GET_FROM_STRING:
6621 /* Current display element is a character from a Lisp string. */
6622 xassert (it->s == NULL && STRINGP (it->string));
6623 if (it->cmp_it.id >= 0)
6624 {
6625 int i;
6626
6627 if (! it->bidi_p)
6628 {
6629 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6630 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6631 if (it->cmp_it.to < it->cmp_it.nglyphs)
6632 it->cmp_it.from = it->cmp_it.to;
6633 else
6634 {
6635 it->cmp_it.id = -1;
6636 composition_compute_stop_pos (&it->cmp_it,
6637 IT_STRING_CHARPOS (*it),
6638 IT_STRING_BYTEPOS (*it),
6639 it->end_charpos, it->string);
6640 }
6641 }
6642 else if (! it->cmp_it.reversed_p)
6643 {
6644 for (i = 0; i < it->cmp_it.nchars; i++)
6645 bidi_move_to_visually_next (&it->bidi_it);
6646 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6647 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6648
6649 if (it->cmp_it.to < it->cmp_it.nglyphs)
6650 it->cmp_it.from = it->cmp_it.to;
6651 else
6652 {
6653 EMACS_INT stop = it->end_charpos;
6654 if (it->bidi_it.scan_dir < 0)
6655 stop = -1;
6656 composition_compute_stop_pos (&it->cmp_it,
6657 IT_STRING_CHARPOS (*it),
6658 IT_STRING_BYTEPOS (*it), stop,
6659 it->string);
6660 }
6661 }
6662 else
6663 {
6664 for (i = 0; i < it->cmp_it.nchars; i++)
6665 bidi_move_to_visually_next (&it->bidi_it);
6666 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6667 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6668 if (it->cmp_it.from > 0)
6669 it->cmp_it.to = it->cmp_it.from;
6670 else
6671 {
6672 EMACS_INT stop = it->end_charpos;
6673 if (it->bidi_it.scan_dir < 0)
6674 stop = -1;
6675 composition_compute_stop_pos (&it->cmp_it,
6676 IT_STRING_CHARPOS (*it),
6677 IT_STRING_BYTEPOS (*it), stop,
6678 it->string);
6679 }
6680 }
6681 }
6682 else
6683 {
6684 if (!it->bidi_p
6685 /* If the string position is beyond string's end, it
6686 means next_element_from_string is padding the string
6687 with blanks, in which case we bypass the bidi
6688 iterator, because it cannot deal with such virtual
6689 characters. */
6690 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
6691 {
6692 IT_STRING_BYTEPOS (*it) += it->len;
6693 IT_STRING_CHARPOS (*it) += 1;
6694 }
6695 else
6696 {
6697 int prev_scan_dir = it->bidi_it.scan_dir;
6698
6699 bidi_move_to_visually_next (&it->bidi_it);
6700 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6701 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6702 if (prev_scan_dir != it->bidi_it.scan_dir)
6703 {
6704 EMACS_INT stop = it->end_charpos;
6705
6706 if (it->bidi_it.scan_dir < 0)
6707 stop = -1;
6708 composition_compute_stop_pos (&it->cmp_it,
6709 IT_STRING_CHARPOS (*it),
6710 IT_STRING_BYTEPOS (*it), stop,
6711 it->string);
6712 }
6713 }
6714 }
6715
6716 consider_string_end:
6717
6718 if (it->current.overlay_string_index >= 0)
6719 {
6720 /* IT->string is an overlay string. Advance to the
6721 next, if there is one. */
6722 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
6723 {
6724 it->ellipsis_p = 0;
6725 next_overlay_string (it);
6726 if (it->ellipsis_p)
6727 setup_for_ellipsis (it, 0);
6728 }
6729 }
6730 else
6731 {
6732 /* IT->string is not an overlay string. If we reached
6733 its end, and there is something on IT->stack, proceed
6734 with what is on the stack. This can be either another
6735 string, this time an overlay string, or a buffer. */
6736 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
6737 && it->sp > 0)
6738 {
6739 pop_it (it);
6740 if (it->method == GET_FROM_STRING)
6741 goto consider_string_end;
6742 }
6743 }
6744 break;
6745
6746 case GET_FROM_IMAGE:
6747 case GET_FROM_STRETCH:
6748 /* The position etc with which we have to proceed are on
6749 the stack. The position may be at the end of a string,
6750 if the `display' property takes up the whole string. */
6751 xassert (it->sp > 0);
6752 pop_it (it);
6753 if (it->method == GET_FROM_STRING)
6754 goto consider_string_end;
6755 break;
6756
6757 default:
6758 /* There are no other methods defined, so this should be a bug. */
6759 abort ();
6760 }
6761
6762 xassert (it->method != GET_FROM_STRING
6763 || (STRINGP (it->string)
6764 && IT_STRING_CHARPOS (*it) >= 0));
6765 }
6766
6767 /* Load IT's display element fields with information about the next
6768 display element which comes from a display table entry or from the
6769 result of translating a control character to one of the forms `^C'
6770 or `\003'.
6771
6772 IT->dpvec holds the glyphs to return as characters.
6773 IT->saved_face_id holds the face id before the display vector--it
6774 is restored into IT->face_id in set_iterator_to_next. */
6775
6776 static int
6777 next_element_from_display_vector (struct it *it)
6778 {
6779 Lisp_Object gc;
6780
6781 /* Precondition. */
6782 xassert (it->dpvec && it->current.dpvec_index >= 0);
6783
6784 it->face_id = it->saved_face_id;
6785
6786 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
6787 That seemed totally bogus - so I changed it... */
6788 gc = it->dpvec[it->current.dpvec_index];
6789
6790 if (GLYPH_CODE_P (gc) && GLYPH_CODE_CHAR_VALID_P (gc))
6791 {
6792 it->c = GLYPH_CODE_CHAR (gc);
6793 it->len = CHAR_BYTES (it->c);
6794
6795 /* The entry may contain a face id to use. Such a face id is
6796 the id of a Lisp face, not a realized face. A face id of
6797 zero means no face is specified. */
6798 if (it->dpvec_face_id >= 0)
6799 it->face_id = it->dpvec_face_id;
6800 else
6801 {
6802 int lface_id = GLYPH_CODE_FACE (gc);
6803 if (lface_id > 0)
6804 it->face_id = merge_faces (it->f, Qt, lface_id,
6805 it->saved_face_id);
6806 }
6807 }
6808 else
6809 /* Display table entry is invalid. Return a space. */
6810 it->c = ' ', it->len = 1;
6811
6812 /* Don't change position and object of the iterator here. They are
6813 still the values of the character that had this display table
6814 entry or was translated, and that's what we want. */
6815 it->what = IT_CHARACTER;
6816 return 1;
6817 }
6818
6819 /* Get the first element of string/buffer in the visual order, after
6820 being reseated to a new position in a string or a buffer. */
6821 static void
6822 get_visually_first_element (struct it *it)
6823 {
6824 int string_p = STRINGP (it->string) || it->s;
6825 EMACS_INT eob = (string_p ? it->bidi_it.string.schars : ZV);
6826 EMACS_INT bob = (string_p ? 0 : BEGV);
6827
6828 if (STRINGP (it->string))
6829 {
6830 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
6831 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
6832 }
6833 else
6834 {
6835 it->bidi_it.charpos = IT_CHARPOS (*it);
6836 it->bidi_it.bytepos = IT_BYTEPOS (*it);
6837 }
6838
6839 if (it->bidi_it.charpos == eob)
6840 {
6841 /* Nothing to do, but reset the FIRST_ELT flag, like
6842 bidi_paragraph_init does, because we are not going to
6843 call it. */
6844 it->bidi_it.first_elt = 0;
6845 }
6846 else if (it->bidi_it.charpos == bob
6847 || (!string_p
6848 /* FIXME: Should support all Unicode line separators. */
6849 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
6850 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
6851 {
6852 /* If we are at the beginning of a line/string, we can produce
6853 the next element right away. */
6854 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6855 bidi_move_to_visually_next (&it->bidi_it);
6856 }
6857 else
6858 {
6859 EMACS_INT orig_bytepos = it->bidi_it.bytepos;
6860
6861 /* We need to prime the bidi iterator starting at the line's or
6862 string's beginning, before we will be able to produce the
6863 next element. */
6864 if (string_p)
6865 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
6866 else
6867 {
6868 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
6869 -1);
6870 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
6871 }
6872 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
6873 do
6874 {
6875 /* Now return to buffer/string position where we were asked
6876 to get the next display element, and produce that. */
6877 bidi_move_to_visually_next (&it->bidi_it);
6878 }
6879 while (it->bidi_it.bytepos != orig_bytepos
6880 && it->bidi_it.charpos < eob);
6881 }
6882
6883 /* Adjust IT's position information to where we ended up. */
6884 if (STRINGP (it->string))
6885 {
6886 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6887 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6888 }
6889 else
6890 {
6891 IT_CHARPOS (*it) = it->bidi_it.charpos;
6892 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6893 }
6894
6895 if (STRINGP (it->string) || !it->s)
6896 {
6897 EMACS_INT stop, charpos, bytepos;
6898
6899 if (STRINGP (it->string))
6900 {
6901 xassert (!it->s);
6902 stop = SCHARS (it->string);
6903 if (stop > it->end_charpos)
6904 stop = it->end_charpos;
6905 charpos = IT_STRING_CHARPOS (*it);
6906 bytepos = IT_STRING_BYTEPOS (*it);
6907 }
6908 else
6909 {
6910 stop = it->end_charpos;
6911 charpos = IT_CHARPOS (*it);
6912 bytepos = IT_BYTEPOS (*it);
6913 }
6914 if (it->bidi_it.scan_dir < 0)
6915 stop = -1;
6916 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
6917 it->string);
6918 }
6919 }
6920
6921 /* Load IT with the next display element from Lisp string IT->string.
6922 IT->current.string_pos is the current position within the string.
6923 If IT->current.overlay_string_index >= 0, the Lisp string is an
6924 overlay string. */
6925
6926 static int
6927 next_element_from_string (struct it *it)
6928 {
6929 struct text_pos position;
6930
6931 xassert (STRINGP (it->string));
6932 xassert (!it->bidi_p || it->string == it->bidi_it.string.lstring);
6933 xassert (IT_STRING_CHARPOS (*it) >= 0);
6934 position = it->current.string_pos;
6935
6936 /* With bidi reordering, the character to display might not be the
6937 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
6938 that we were reseat()ed to a new string, whose paragraph
6939 direction is not known. */
6940 if (it->bidi_p && it->bidi_it.first_elt)
6941 {
6942 get_visually_first_element (it);
6943 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
6944 }
6945
6946 /* Time to check for invisible text? */
6947 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
6948 {
6949 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
6950 {
6951 if (!(!it->bidi_p
6952 || BIDI_AT_BASE_LEVEL (it->bidi_it)
6953 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
6954 {
6955 /* With bidi non-linear iteration, we could find
6956 ourselves far beyond the last computed stop_charpos,
6957 with several other stop positions in between that we
6958 missed. Scan them all now, in buffer's logical
6959 order, until we find and handle the last stop_charpos
6960 that precedes our current position. */
6961 handle_stop_backwards (it, it->stop_charpos);
6962 return GET_NEXT_DISPLAY_ELEMENT (it);
6963 }
6964 else
6965 {
6966 if (it->bidi_p)
6967 {
6968 /* Take note of the stop position we just moved
6969 across, for when we will move back across it. */
6970 it->prev_stop = it->stop_charpos;
6971 /* If we are at base paragraph embedding level, take
6972 note of the last stop position seen at this
6973 level. */
6974 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
6975 it->base_level_stop = it->stop_charpos;
6976 }
6977 handle_stop (it);
6978
6979 /* Since a handler may have changed IT->method, we must
6980 recurse here. */
6981 return GET_NEXT_DISPLAY_ELEMENT (it);
6982 }
6983 }
6984 else if (it->bidi_p
6985 /* If we are before prev_stop, we may have overstepped
6986 on our way backwards a stop_pos, and if so, we need
6987 to handle that stop_pos. */
6988 && IT_STRING_CHARPOS (*it) < it->prev_stop
6989 /* We can sometimes back up for reasons that have nothing
6990 to do with bidi reordering. E.g., compositions. The
6991 code below is only needed when we are above the base
6992 embedding level, so test for that explicitly. */
6993 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
6994 {
6995 /* If we lost track of base_level_stop, we have no better place
6996 for handle_stop_backwards to start from than BEGV. This
6997 happens, e.g., when we were reseated to the previous
6998 screenful of text by vertical-motion. */
6999 if (it->base_level_stop <= 0
7000 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7001 it->base_level_stop = 0;
7002 handle_stop_backwards (it, it->base_level_stop);
7003 return GET_NEXT_DISPLAY_ELEMENT (it);
7004 }
7005 }
7006
7007 if (it->current.overlay_string_index >= 0)
7008 {
7009 /* Get the next character from an overlay string. In overlay
7010 strings, There is no field width or padding with spaces to
7011 do. */
7012 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7013 {
7014 it->what = IT_EOB;
7015 return 0;
7016 }
7017 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7018 IT_STRING_BYTEPOS (*it),
7019 it->bidi_it.scan_dir < 0
7020 ? -1
7021 : SCHARS (it->string))
7022 && next_element_from_composition (it))
7023 {
7024 return 1;
7025 }
7026 else if (STRING_MULTIBYTE (it->string))
7027 {
7028 const unsigned char *s = (SDATA (it->string)
7029 + IT_STRING_BYTEPOS (*it));
7030 it->c = string_char_and_length (s, &it->len);
7031 }
7032 else
7033 {
7034 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7035 it->len = 1;
7036 }
7037 }
7038 else
7039 {
7040 /* Get the next character from a Lisp string that is not an
7041 overlay string. Such strings come from the mode line, for
7042 example. We may have to pad with spaces, or truncate the
7043 string. See also next_element_from_c_string. */
7044 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7045 {
7046 it->what = IT_EOB;
7047 return 0;
7048 }
7049 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7050 {
7051 /* Pad with spaces. */
7052 it->c = ' ', it->len = 1;
7053 CHARPOS (position) = BYTEPOS (position) = -1;
7054 }
7055 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7056 IT_STRING_BYTEPOS (*it),
7057 it->bidi_it.scan_dir < 0
7058 ? -1
7059 : it->string_nchars)
7060 && next_element_from_composition (it))
7061 {
7062 return 1;
7063 }
7064 else if (STRING_MULTIBYTE (it->string))
7065 {
7066 const unsigned char *s = (SDATA (it->string)
7067 + IT_STRING_BYTEPOS (*it));
7068 it->c = string_char_and_length (s, &it->len);
7069 }
7070 else
7071 {
7072 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7073 it->len = 1;
7074 }
7075 }
7076
7077 /* Record what we have and where it came from. */
7078 it->what = IT_CHARACTER;
7079 it->object = it->string;
7080 it->position = position;
7081 return 1;
7082 }
7083
7084
7085 /* Load IT with next display element from C string IT->s.
7086 IT->string_nchars is the maximum number of characters to return
7087 from the string. IT->end_charpos may be greater than
7088 IT->string_nchars when this function is called, in which case we
7089 may have to return padding spaces. Value is zero if end of string
7090 reached, including padding spaces. */
7091
7092 static int
7093 next_element_from_c_string (struct it *it)
7094 {
7095 int success_p = 1;
7096
7097 xassert (it->s);
7098 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7099 it->what = IT_CHARACTER;
7100 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7101 it->object = Qnil;
7102
7103 /* With bidi reordering, the character to display might not be the
7104 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7105 we were reseated to a new string, whose paragraph direction is
7106 not known. */
7107 if (it->bidi_p && it->bidi_it.first_elt)
7108 get_visually_first_element (it);
7109
7110 /* IT's position can be greater than IT->string_nchars in case a
7111 field width or precision has been specified when the iterator was
7112 initialized. */
7113 if (IT_CHARPOS (*it) >= it->end_charpos)
7114 {
7115 /* End of the game. */
7116 it->what = IT_EOB;
7117 success_p = 0;
7118 }
7119 else if (IT_CHARPOS (*it) >= it->string_nchars)
7120 {
7121 /* Pad with spaces. */
7122 it->c = ' ', it->len = 1;
7123 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7124 }
7125 else if (it->multibyte_p)
7126 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7127 else
7128 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7129
7130 return success_p;
7131 }
7132
7133
7134 /* Set up IT to return characters from an ellipsis, if appropriate.
7135 The definition of the ellipsis glyphs may come from a display table
7136 entry. This function fills IT with the first glyph from the
7137 ellipsis if an ellipsis is to be displayed. */
7138
7139 static int
7140 next_element_from_ellipsis (struct it *it)
7141 {
7142 if (it->selective_display_ellipsis_p)
7143 setup_for_ellipsis (it, it->len);
7144 else
7145 {
7146 /* The face at the current position may be different from the
7147 face we find after the invisible text. Remember what it
7148 was in IT->saved_face_id, and signal that it's there by
7149 setting face_before_selective_p. */
7150 it->saved_face_id = it->face_id;
7151 it->method = GET_FROM_BUFFER;
7152 it->object = it->w->buffer;
7153 reseat_at_next_visible_line_start (it, 1);
7154 it->face_before_selective_p = 1;
7155 }
7156
7157 return GET_NEXT_DISPLAY_ELEMENT (it);
7158 }
7159
7160
7161 /* Deliver an image display element. The iterator IT is already
7162 filled with image information (done in handle_display_prop). Value
7163 is always 1. */
7164
7165
7166 static int
7167 next_element_from_image (struct it *it)
7168 {
7169 it->what = IT_IMAGE;
7170 it->ignore_overlay_strings_at_pos_p = 0;
7171 return 1;
7172 }
7173
7174
7175 /* Fill iterator IT with next display element from a stretch glyph
7176 property. IT->object is the value of the text property. Value is
7177 always 1. */
7178
7179 static int
7180 next_element_from_stretch (struct it *it)
7181 {
7182 it->what = IT_STRETCH;
7183 return 1;
7184 }
7185
7186 /* Scan forward from CHARPOS in the current buffer/string, until we
7187 find a stop position > current IT's position. Then handle the stop
7188 position before that. This is called when we bump into a stop
7189 position while reordering bidirectional text. CHARPOS should be
7190 the last previously processed stop_pos (or BEGV/0, if none were
7191 processed yet) whose position is less that IT's current
7192 position. */
7193
7194 static void
7195 handle_stop_backwards (struct it *it, EMACS_INT charpos)
7196 {
7197 int bufp = !STRINGP (it->string);
7198 EMACS_INT where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7199 struct display_pos save_current = it->current;
7200 struct text_pos save_position = it->position;
7201 struct text_pos pos1;
7202 EMACS_INT next_stop;
7203
7204 /* Scan in strict logical order. */
7205 it->bidi_p = 0;
7206 do
7207 {
7208 it->prev_stop = charpos;
7209 if (bufp)
7210 {
7211 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7212 reseat_1 (it, pos1, 0);
7213 }
7214 else
7215 it->current.string_pos = string_pos (charpos, it->string);
7216 compute_stop_pos (it);
7217 /* We must advance forward, right? */
7218 if (it->stop_charpos <= it->prev_stop)
7219 abort ();
7220 charpos = it->stop_charpos;
7221 }
7222 while (charpos <= where_we_are);
7223
7224 next_stop = it->stop_charpos;
7225 it->stop_charpos = it->prev_stop;
7226 it->bidi_p = 1;
7227 it->current = save_current;
7228 it->position = save_position;
7229 handle_stop (it);
7230 it->stop_charpos = next_stop;
7231 }
7232
7233 /* Load IT with the next display element from current_buffer. Value
7234 is zero if end of buffer reached. IT->stop_charpos is the next
7235 position at which to stop and check for text properties or buffer
7236 end. */
7237
7238 static int
7239 next_element_from_buffer (struct it *it)
7240 {
7241 int success_p = 1;
7242
7243 xassert (IT_CHARPOS (*it) >= BEGV);
7244 xassert (NILP (it->string) && !it->s);
7245 xassert (!it->bidi_p
7246 || (it->bidi_it.string.lstring == Qnil
7247 && it->bidi_it.string.s == NULL));
7248
7249 /* With bidi reordering, the character to display might not be the
7250 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7251 we were reseat()ed to a new buffer position, which is potentially
7252 a different paragraph. */
7253 if (it->bidi_p && it->bidi_it.first_elt)
7254 {
7255 get_visually_first_element (it);
7256 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7257 }
7258
7259 if (IT_CHARPOS (*it) >= it->stop_charpos)
7260 {
7261 if (IT_CHARPOS (*it) >= it->end_charpos)
7262 {
7263 int overlay_strings_follow_p;
7264
7265 /* End of the game, except when overlay strings follow that
7266 haven't been returned yet. */
7267 if (it->overlay_strings_at_end_processed_p)
7268 overlay_strings_follow_p = 0;
7269 else
7270 {
7271 it->overlay_strings_at_end_processed_p = 1;
7272 overlay_strings_follow_p = get_overlay_strings (it, 0);
7273 }
7274
7275 if (overlay_strings_follow_p)
7276 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7277 else
7278 {
7279 it->what = IT_EOB;
7280 it->position = it->current.pos;
7281 success_p = 0;
7282 }
7283 }
7284 else if (!(!it->bidi_p
7285 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7286 || IT_CHARPOS (*it) == it->stop_charpos))
7287 {
7288 /* With bidi non-linear iteration, we could find ourselves
7289 far beyond the last computed stop_charpos, with several
7290 other stop positions in between that we missed. Scan
7291 them all now, in buffer's logical order, until we find
7292 and handle the last stop_charpos that precedes our
7293 current position. */
7294 handle_stop_backwards (it, it->stop_charpos);
7295 return GET_NEXT_DISPLAY_ELEMENT (it);
7296 }
7297 else
7298 {
7299 if (it->bidi_p)
7300 {
7301 /* Take note of the stop position we just moved across,
7302 for when we will move back across it. */
7303 it->prev_stop = it->stop_charpos;
7304 /* If we are at base paragraph embedding level, take
7305 note of the last stop position seen at this
7306 level. */
7307 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7308 it->base_level_stop = it->stop_charpos;
7309 }
7310 handle_stop (it);
7311 return GET_NEXT_DISPLAY_ELEMENT (it);
7312 }
7313 }
7314 else if (it->bidi_p
7315 /* If we are before prev_stop, we may have overstepped on
7316 our way backwards a stop_pos, and if so, we need to
7317 handle that stop_pos. */
7318 && IT_CHARPOS (*it) < it->prev_stop
7319 /* We can sometimes back up for reasons that have nothing
7320 to do with bidi reordering. E.g., compositions. The
7321 code below is only needed when we are above the base
7322 embedding level, so test for that explicitly. */
7323 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7324 {
7325 /* If we lost track of base_level_stop, we have no better place
7326 for handle_stop_backwards to start from than BEGV. This
7327 happens, e.g., when we were reseated to the previous
7328 screenful of text by vertical-motion. */
7329 if (it->base_level_stop <= 0
7330 || IT_CHARPOS (*it) < it->base_level_stop)
7331 it->base_level_stop = BEGV;
7332 handle_stop_backwards (it, it->base_level_stop);
7333 return GET_NEXT_DISPLAY_ELEMENT (it);
7334 }
7335 else
7336 {
7337 /* No face changes, overlays etc. in sight, so just return a
7338 character from current_buffer. */
7339 unsigned char *p;
7340 EMACS_INT stop;
7341
7342 /* Maybe run the redisplay end trigger hook. Performance note:
7343 This doesn't seem to cost measurable time. */
7344 if (it->redisplay_end_trigger_charpos
7345 && it->glyph_row
7346 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7347 run_redisplay_end_trigger_hook (it);
7348
7349 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7350 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7351 stop)
7352 && next_element_from_composition (it))
7353 {
7354 return 1;
7355 }
7356
7357 /* Get the next character, maybe multibyte. */
7358 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7359 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7360 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7361 else
7362 it->c = *p, it->len = 1;
7363
7364 /* Record what we have and where it came from. */
7365 it->what = IT_CHARACTER;
7366 it->object = it->w->buffer;
7367 it->position = it->current.pos;
7368
7369 /* Normally we return the character found above, except when we
7370 really want to return an ellipsis for selective display. */
7371 if (it->selective)
7372 {
7373 if (it->c == '\n')
7374 {
7375 /* A value of selective > 0 means hide lines indented more
7376 than that number of columns. */
7377 if (it->selective > 0
7378 && IT_CHARPOS (*it) + 1 < ZV
7379 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7380 IT_BYTEPOS (*it) + 1,
7381 (double) it->selective)) /* iftc */
7382 {
7383 success_p = next_element_from_ellipsis (it);
7384 it->dpvec_char_len = -1;
7385 }
7386 }
7387 else if (it->c == '\r' && it->selective == -1)
7388 {
7389 /* A value of selective == -1 means that everything from the
7390 CR to the end of the line is invisible, with maybe an
7391 ellipsis displayed for it. */
7392 success_p = next_element_from_ellipsis (it);
7393 it->dpvec_char_len = -1;
7394 }
7395 }
7396 }
7397
7398 /* Value is zero if end of buffer reached. */
7399 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7400 return success_p;
7401 }
7402
7403
7404 /* Run the redisplay end trigger hook for IT. */
7405
7406 static void
7407 run_redisplay_end_trigger_hook (struct it *it)
7408 {
7409 Lisp_Object args[3];
7410
7411 /* IT->glyph_row should be non-null, i.e. we should be actually
7412 displaying something, or otherwise we should not run the hook. */
7413 xassert (it->glyph_row);
7414
7415 /* Set up hook arguments. */
7416 args[0] = Qredisplay_end_trigger_functions;
7417 args[1] = it->window;
7418 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7419 it->redisplay_end_trigger_charpos = 0;
7420
7421 /* Since we are *trying* to run these functions, don't try to run
7422 them again, even if they get an error. */
7423 it->w->redisplay_end_trigger = Qnil;
7424 Frun_hook_with_args (3, args);
7425
7426 /* Notice if it changed the face of the character we are on. */
7427 handle_face_prop (it);
7428 }
7429
7430
7431 /* Deliver a composition display element. Unlike the other
7432 next_element_from_XXX, this function is not registered in the array
7433 get_next_element[]. It is called from next_element_from_buffer and
7434 next_element_from_string when necessary. */
7435
7436 static int
7437 next_element_from_composition (struct it *it)
7438 {
7439 it->what = IT_COMPOSITION;
7440 it->len = it->cmp_it.nbytes;
7441 if (STRINGP (it->string))
7442 {
7443 if (it->c < 0)
7444 {
7445 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7446 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7447 return 0;
7448 }
7449 it->position = it->current.string_pos;
7450 it->object = it->string;
7451 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7452 IT_STRING_BYTEPOS (*it), it->string);
7453 }
7454 else
7455 {
7456 if (it->c < 0)
7457 {
7458 IT_CHARPOS (*it) += it->cmp_it.nchars;
7459 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7460 if (it->bidi_p)
7461 {
7462 if (it->bidi_it.new_paragraph)
7463 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7464 /* Resync the bidi iterator with IT's new position.
7465 FIXME: this doesn't support bidirectional text. */
7466 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7467 bidi_move_to_visually_next (&it->bidi_it);
7468 }
7469 return 0;
7470 }
7471 it->position = it->current.pos;
7472 it->object = it->w->buffer;
7473 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7474 IT_BYTEPOS (*it), Qnil);
7475 }
7476 return 1;
7477 }
7478
7479
7480 \f
7481 /***********************************************************************
7482 Moving an iterator without producing glyphs
7483 ***********************************************************************/
7484
7485 /* Check if iterator is at a position corresponding to a valid buffer
7486 position after some move_it_ call. */
7487
7488 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7489 ((it)->method == GET_FROM_STRING \
7490 ? IT_STRING_CHARPOS (*it) == 0 \
7491 : 1)
7492
7493
7494 /* Move iterator IT to a specified buffer or X position within one
7495 line on the display without producing glyphs.
7496
7497 OP should be a bit mask including some or all of these bits:
7498 MOVE_TO_X: Stop upon reaching x-position TO_X.
7499 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7500 Regardless of OP's value, stop upon reaching the end of the display line.
7501
7502 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7503 This means, in particular, that TO_X includes window's horizontal
7504 scroll amount.
7505
7506 The return value has several possible values that
7507 say what condition caused the scan to stop:
7508
7509 MOVE_POS_MATCH_OR_ZV
7510 - when TO_POS or ZV was reached.
7511
7512 MOVE_X_REACHED
7513 -when TO_X was reached before TO_POS or ZV were reached.
7514
7515 MOVE_LINE_CONTINUED
7516 - when we reached the end of the display area and the line must
7517 be continued.
7518
7519 MOVE_LINE_TRUNCATED
7520 - when we reached the end of the display area and the line is
7521 truncated.
7522
7523 MOVE_NEWLINE_OR_CR
7524 - when we stopped at a line end, i.e. a newline or a CR and selective
7525 display is on. */
7526
7527 static enum move_it_result
7528 move_it_in_display_line_to (struct it *it,
7529 EMACS_INT to_charpos, int to_x,
7530 enum move_operation_enum op)
7531 {
7532 enum move_it_result result = MOVE_UNDEFINED;
7533 struct glyph_row *saved_glyph_row;
7534 struct it wrap_it, atpos_it, atx_it;
7535 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
7536 int may_wrap = 0;
7537 enum it_method prev_method = it->method;
7538 EMACS_INT prev_pos = IT_CHARPOS (*it);
7539 int saw_smaller_pos = prev_pos < to_charpos;
7540
7541 /* Don't produce glyphs in produce_glyphs. */
7542 saved_glyph_row = it->glyph_row;
7543 it->glyph_row = NULL;
7544
7545 /* Use wrap_it to save a copy of IT wherever a word wrap could
7546 occur. Use atpos_it to save a copy of IT at the desired buffer
7547 position, if found, so that we can scan ahead and check if the
7548 word later overshoots the window edge. Use atx_it similarly, for
7549 pixel positions. */
7550 wrap_it.sp = -1;
7551 atpos_it.sp = -1;
7552 atx_it.sp = -1;
7553
7554 #define BUFFER_POS_REACHED_P() \
7555 ((op & MOVE_TO_POS) != 0 \
7556 && BUFFERP (it->object) \
7557 && (IT_CHARPOS (*it) == to_charpos \
7558 || (!it->bidi_p && IT_CHARPOS (*it) > to_charpos)) \
7559 && (it->method == GET_FROM_BUFFER \
7560 || (it->method == GET_FROM_DISPLAY_VECTOR \
7561 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7562
7563 /* If there's a line-/wrap-prefix, handle it. */
7564 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7565 && it->current_y < it->last_visible_y)
7566 handle_line_prefix (it);
7567
7568 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7569 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7570
7571 while (1)
7572 {
7573 int x, i, ascent = 0, descent = 0;
7574
7575 /* Utility macro to reset an iterator with x, ascent, and descent. */
7576 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7577 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7578 (IT)->max_descent = descent)
7579
7580 /* Stop if we move beyond TO_CHARPOS (after an image or a
7581 display string or stretch glyph). */
7582 if ((op & MOVE_TO_POS) != 0
7583 && BUFFERP (it->object)
7584 && it->method == GET_FROM_BUFFER
7585 && ((!it->bidi_p && IT_CHARPOS (*it) > to_charpos)
7586 || (it->bidi_p
7587 && (prev_method == GET_FROM_IMAGE
7588 || prev_method == GET_FROM_STRETCH
7589 || prev_method == GET_FROM_STRING)
7590 /* Passed TO_CHARPOS from left to right. */
7591 && ((prev_pos < to_charpos
7592 && IT_CHARPOS (*it) > to_charpos)
7593 /* Passed TO_CHARPOS from right to left. */
7594 || (prev_pos > to_charpos
7595 && IT_CHARPOS (*it) < to_charpos)))))
7596 {
7597 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7598 {
7599 result = MOVE_POS_MATCH_OR_ZV;
7600 break;
7601 }
7602 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7603 /* If wrap_it is valid, the current position might be in a
7604 word that is wrapped. So, save the iterator in
7605 atpos_it and continue to see if wrapping happens. */
7606 SAVE_IT (atpos_it, *it, atpos_data);
7607 }
7608
7609 /* Stop when ZV reached.
7610 We used to stop here when TO_CHARPOS reached as well, but that is
7611 too soon if this glyph does not fit on this line. So we handle it
7612 explicitly below. */
7613 if (!get_next_display_element (it))
7614 {
7615 result = MOVE_POS_MATCH_OR_ZV;
7616 break;
7617 }
7618
7619 if (it->line_wrap == TRUNCATE)
7620 {
7621 if (BUFFER_POS_REACHED_P ())
7622 {
7623 result = MOVE_POS_MATCH_OR_ZV;
7624 break;
7625 }
7626 }
7627 else
7628 {
7629 if (it->line_wrap == WORD_WRAP)
7630 {
7631 if (IT_DISPLAYING_WHITESPACE (it))
7632 may_wrap = 1;
7633 else if (may_wrap)
7634 {
7635 /* We have reached a glyph that follows one or more
7636 whitespace characters. If the position is
7637 already found, we are done. */
7638 if (atpos_it.sp >= 0)
7639 {
7640 RESTORE_IT (it, &atpos_it, atpos_data);
7641 result = MOVE_POS_MATCH_OR_ZV;
7642 goto done;
7643 }
7644 if (atx_it.sp >= 0)
7645 {
7646 RESTORE_IT (it, &atx_it, atx_data);
7647 result = MOVE_X_REACHED;
7648 goto done;
7649 }
7650 /* Otherwise, we can wrap here. */
7651 SAVE_IT (wrap_it, *it, wrap_data);
7652 may_wrap = 0;
7653 }
7654 }
7655 }
7656
7657 /* Remember the line height for the current line, in case
7658 the next element doesn't fit on the line. */
7659 ascent = it->max_ascent;
7660 descent = it->max_descent;
7661
7662 /* The call to produce_glyphs will get the metrics of the
7663 display element IT is loaded with. Record the x-position
7664 before this display element, in case it doesn't fit on the
7665 line. */
7666 x = it->current_x;
7667
7668 PRODUCE_GLYPHS (it);
7669
7670 if (it->area != TEXT_AREA)
7671 {
7672 prev_method = it->method;
7673 if (it->method == GET_FROM_BUFFER)
7674 prev_pos = IT_CHARPOS (*it);
7675 set_iterator_to_next (it, 1);
7676 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7677 SET_TEXT_POS (this_line_min_pos,
7678 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7679 continue;
7680 }
7681
7682 /* The number of glyphs we get back in IT->nglyphs will normally
7683 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
7684 character on a terminal frame, or (iii) a line end. For the
7685 second case, IT->nglyphs - 1 padding glyphs will be present.
7686 (On X frames, there is only one glyph produced for a
7687 composite character.)
7688
7689 The behavior implemented below means, for continuation lines,
7690 that as many spaces of a TAB as fit on the current line are
7691 displayed there. For terminal frames, as many glyphs of a
7692 multi-glyph character are displayed in the current line, too.
7693 This is what the old redisplay code did, and we keep it that
7694 way. Under X, the whole shape of a complex character must
7695 fit on the line or it will be completely displayed in the
7696 next line.
7697
7698 Note that both for tabs and padding glyphs, all glyphs have
7699 the same width. */
7700 if (it->nglyphs)
7701 {
7702 /* More than one glyph or glyph doesn't fit on line. All
7703 glyphs have the same width. */
7704 int single_glyph_width = it->pixel_width / it->nglyphs;
7705 int new_x;
7706 int x_before_this_char = x;
7707 int hpos_before_this_char = it->hpos;
7708
7709 for (i = 0; i < it->nglyphs; ++i, x = new_x)
7710 {
7711 new_x = x + single_glyph_width;
7712
7713 /* We want to leave anything reaching TO_X to the caller. */
7714 if ((op & MOVE_TO_X) && new_x > to_x)
7715 {
7716 if (BUFFER_POS_REACHED_P ())
7717 {
7718 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7719 goto buffer_pos_reached;
7720 if (atpos_it.sp < 0)
7721 {
7722 SAVE_IT (atpos_it, *it, atpos_data);
7723 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7724 }
7725 }
7726 else
7727 {
7728 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7729 {
7730 it->current_x = x;
7731 result = MOVE_X_REACHED;
7732 break;
7733 }
7734 if (atx_it.sp < 0)
7735 {
7736 SAVE_IT (atx_it, *it, atx_data);
7737 IT_RESET_X_ASCENT_DESCENT (&atx_it);
7738 }
7739 }
7740 }
7741
7742 if (/* Lines are continued. */
7743 it->line_wrap != TRUNCATE
7744 && (/* And glyph doesn't fit on the line. */
7745 new_x > it->last_visible_x
7746 /* Or it fits exactly and we're on a window
7747 system frame. */
7748 || (new_x == it->last_visible_x
7749 && FRAME_WINDOW_P (it->f))))
7750 {
7751 if (/* IT->hpos == 0 means the very first glyph
7752 doesn't fit on the line, e.g. a wide image. */
7753 it->hpos == 0
7754 || (new_x == it->last_visible_x
7755 && FRAME_WINDOW_P (it->f)))
7756 {
7757 ++it->hpos;
7758 it->current_x = new_x;
7759
7760 /* The character's last glyph just barely fits
7761 in this row. */
7762 if (i == it->nglyphs - 1)
7763 {
7764 /* If this is the destination position,
7765 return a position *before* it in this row,
7766 now that we know it fits in this row. */
7767 if (BUFFER_POS_REACHED_P ())
7768 {
7769 if (it->line_wrap != WORD_WRAP
7770 || wrap_it.sp < 0)
7771 {
7772 it->hpos = hpos_before_this_char;
7773 it->current_x = x_before_this_char;
7774 result = MOVE_POS_MATCH_OR_ZV;
7775 break;
7776 }
7777 if (it->line_wrap == WORD_WRAP
7778 && atpos_it.sp < 0)
7779 {
7780 SAVE_IT (atpos_it, *it, atpos_data);
7781 atpos_it.current_x = x_before_this_char;
7782 atpos_it.hpos = hpos_before_this_char;
7783 }
7784 }
7785
7786 prev_method = it->method;
7787 if (it->method == GET_FROM_BUFFER)
7788 prev_pos = IT_CHARPOS (*it);
7789 set_iterator_to_next (it, 1);
7790 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7791 SET_TEXT_POS (this_line_min_pos,
7792 IT_CHARPOS (*it), IT_BYTEPOS (*it));
7793 /* On graphical terminals, newlines may
7794 "overflow" into the fringe if
7795 overflow-newline-into-fringe is non-nil.
7796 On text-only terminals, newlines may
7797 overflow into the last glyph on the
7798 display line.*/
7799 if (!FRAME_WINDOW_P (it->f)
7800 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7801 {
7802 if (!get_next_display_element (it))
7803 {
7804 result = MOVE_POS_MATCH_OR_ZV;
7805 break;
7806 }
7807 if (BUFFER_POS_REACHED_P ())
7808 {
7809 if (ITERATOR_AT_END_OF_LINE_P (it))
7810 result = MOVE_POS_MATCH_OR_ZV;
7811 else
7812 result = MOVE_LINE_CONTINUED;
7813 break;
7814 }
7815 if (ITERATOR_AT_END_OF_LINE_P (it))
7816 {
7817 result = MOVE_NEWLINE_OR_CR;
7818 break;
7819 }
7820 }
7821 }
7822 }
7823 else
7824 IT_RESET_X_ASCENT_DESCENT (it);
7825
7826 if (wrap_it.sp >= 0)
7827 {
7828 RESTORE_IT (it, &wrap_it, wrap_data);
7829 atpos_it.sp = -1;
7830 atx_it.sp = -1;
7831 }
7832
7833 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
7834 IT_CHARPOS (*it)));
7835 result = MOVE_LINE_CONTINUED;
7836 break;
7837 }
7838
7839 if (BUFFER_POS_REACHED_P ())
7840 {
7841 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
7842 goto buffer_pos_reached;
7843 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
7844 {
7845 SAVE_IT (atpos_it, *it, atpos_data);
7846 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
7847 }
7848 }
7849
7850 if (new_x > it->first_visible_x)
7851 {
7852 /* Glyph is visible. Increment number of glyphs that
7853 would be displayed. */
7854 ++it->hpos;
7855 }
7856 }
7857
7858 if (result != MOVE_UNDEFINED)
7859 break;
7860 }
7861 else if (BUFFER_POS_REACHED_P ())
7862 {
7863 buffer_pos_reached:
7864 IT_RESET_X_ASCENT_DESCENT (it);
7865 result = MOVE_POS_MATCH_OR_ZV;
7866 break;
7867 }
7868 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
7869 {
7870 /* Stop when TO_X specified and reached. This check is
7871 necessary here because of lines consisting of a line end,
7872 only. The line end will not produce any glyphs and we
7873 would never get MOVE_X_REACHED. */
7874 xassert (it->nglyphs == 0);
7875 result = MOVE_X_REACHED;
7876 break;
7877 }
7878
7879 /* Is this a line end? If yes, we're done. */
7880 if (ITERATOR_AT_END_OF_LINE_P (it))
7881 {
7882 /* If we are past TO_CHARPOS, but never saw any character
7883 positions smaller than TO_CHARPOS, return
7884 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
7885 did. */
7886 if ((op & MOVE_TO_POS) != 0
7887 && !saw_smaller_pos
7888 && IT_CHARPOS (*it) > to_charpos)
7889 result = MOVE_POS_MATCH_OR_ZV;
7890 else
7891 result = MOVE_NEWLINE_OR_CR;
7892 break;
7893 }
7894
7895 prev_method = it->method;
7896 if (it->method == GET_FROM_BUFFER)
7897 prev_pos = IT_CHARPOS (*it);
7898 /* The current display element has been consumed. Advance
7899 to the next. */
7900 set_iterator_to_next (it, 1);
7901 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7902 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7903 if (IT_CHARPOS (*it) < to_charpos)
7904 saw_smaller_pos = 1;
7905
7906 /* Stop if lines are truncated and IT's current x-position is
7907 past the right edge of the window now. */
7908 if (it->line_wrap == TRUNCATE
7909 && it->current_x >= it->last_visible_x)
7910 {
7911 if (!FRAME_WINDOW_P (it->f)
7912 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
7913 {
7914 if (!get_next_display_element (it)
7915 || BUFFER_POS_REACHED_P ())
7916 {
7917 result = MOVE_POS_MATCH_OR_ZV;
7918 break;
7919 }
7920 if (ITERATOR_AT_END_OF_LINE_P (it))
7921 {
7922 result = MOVE_NEWLINE_OR_CR;
7923 break;
7924 }
7925 }
7926 result = MOVE_LINE_TRUNCATED;
7927 break;
7928 }
7929 #undef IT_RESET_X_ASCENT_DESCENT
7930 }
7931
7932 #undef BUFFER_POS_REACHED_P
7933
7934 /* If we scanned beyond to_pos and didn't find a point to wrap at,
7935 restore the saved iterator. */
7936 if (atpos_it.sp >= 0)
7937 RESTORE_IT (it, &atpos_it, atpos_data);
7938 else if (atx_it.sp >= 0)
7939 RESTORE_IT (it, &atx_it, atx_data);
7940
7941 done:
7942
7943 if (atpos_data)
7944 xfree (atpos_data);
7945 if (atx_data)
7946 xfree (atx_data);
7947 if (wrap_data)
7948 xfree (wrap_data);
7949
7950 /* Restore the iterator settings altered at the beginning of this
7951 function. */
7952 it->glyph_row = saved_glyph_row;
7953 return result;
7954 }
7955
7956 /* For external use. */
7957 void
7958 move_it_in_display_line (struct it *it,
7959 EMACS_INT to_charpos, int to_x,
7960 enum move_operation_enum op)
7961 {
7962 if (it->line_wrap == WORD_WRAP
7963 && (op & MOVE_TO_X))
7964 {
7965 struct it save_it;
7966 void *save_data = NULL;
7967 int skip;
7968
7969 SAVE_IT (save_it, *it, save_data);
7970 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
7971 /* When word-wrap is on, TO_X may lie past the end
7972 of a wrapped line. Then it->current is the
7973 character on the next line, so backtrack to the
7974 space before the wrap point. */
7975 if (skip == MOVE_LINE_CONTINUED)
7976 {
7977 int prev_x = max (it->current_x - 1, 0);
7978 RESTORE_IT (it, &save_it, save_data);
7979 move_it_in_display_line_to
7980 (it, -1, prev_x, MOVE_TO_X);
7981 }
7982 else
7983 xfree (save_data);
7984 }
7985 else
7986 move_it_in_display_line_to (it, to_charpos, to_x, op);
7987 }
7988
7989
7990 /* Move IT forward until it satisfies one or more of the criteria in
7991 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
7992
7993 OP is a bit-mask that specifies where to stop, and in particular,
7994 which of those four position arguments makes a difference. See the
7995 description of enum move_operation_enum.
7996
7997 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
7998 screen line, this function will set IT to the next position that is
7999 displayed to the right of TO_CHARPOS on the screen. */
8000
8001 void
8002 move_it_to (struct it *it, EMACS_INT to_charpos, int to_x, int to_y, int to_vpos, int op)
8003 {
8004 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8005 int line_height, line_start_x = 0, reached = 0;
8006 void *backup_data = NULL;
8007
8008 for (;;)
8009 {
8010 if (op & MOVE_TO_VPOS)
8011 {
8012 /* If no TO_CHARPOS and no TO_X specified, stop at the
8013 start of the line TO_VPOS. */
8014 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8015 {
8016 if (it->vpos == to_vpos)
8017 {
8018 reached = 1;
8019 break;
8020 }
8021 else
8022 skip = move_it_in_display_line_to (it, -1, -1, 0);
8023 }
8024 else
8025 {
8026 /* TO_VPOS >= 0 means stop at TO_X in the line at
8027 TO_VPOS, or at TO_POS, whichever comes first. */
8028 if (it->vpos == to_vpos)
8029 {
8030 reached = 2;
8031 break;
8032 }
8033
8034 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8035
8036 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8037 {
8038 reached = 3;
8039 break;
8040 }
8041 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8042 {
8043 /* We have reached TO_X but not in the line we want. */
8044 skip = move_it_in_display_line_to (it, to_charpos,
8045 -1, MOVE_TO_POS);
8046 if (skip == MOVE_POS_MATCH_OR_ZV)
8047 {
8048 reached = 4;
8049 break;
8050 }
8051 }
8052 }
8053 }
8054 else if (op & MOVE_TO_Y)
8055 {
8056 struct it it_backup;
8057
8058 if (it->line_wrap == WORD_WRAP)
8059 SAVE_IT (it_backup, *it, backup_data);
8060
8061 /* TO_Y specified means stop at TO_X in the line containing
8062 TO_Y---or at TO_CHARPOS if this is reached first. The
8063 problem is that we can't really tell whether the line
8064 contains TO_Y before we have completely scanned it, and
8065 this may skip past TO_X. What we do is to first scan to
8066 TO_X.
8067
8068 If TO_X is not specified, use a TO_X of zero. The reason
8069 is to make the outcome of this function more predictable.
8070 If we didn't use TO_X == 0, we would stop at the end of
8071 the line which is probably not what a caller would expect
8072 to happen. */
8073 skip = move_it_in_display_line_to
8074 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8075 (MOVE_TO_X | (op & MOVE_TO_POS)));
8076
8077 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8078 if (skip == MOVE_POS_MATCH_OR_ZV)
8079 reached = 5;
8080 else if (skip == MOVE_X_REACHED)
8081 {
8082 /* If TO_X was reached, we want to know whether TO_Y is
8083 in the line. We know this is the case if the already
8084 scanned glyphs make the line tall enough. Otherwise,
8085 we must check by scanning the rest of the line. */
8086 line_height = it->max_ascent + it->max_descent;
8087 if (to_y >= it->current_y
8088 && to_y < it->current_y + line_height)
8089 {
8090 reached = 6;
8091 break;
8092 }
8093 SAVE_IT (it_backup, *it, backup_data);
8094 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8095 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8096 op & MOVE_TO_POS);
8097 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8098 line_height = it->max_ascent + it->max_descent;
8099 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8100
8101 if (to_y >= it->current_y
8102 && to_y < it->current_y + line_height)
8103 {
8104 /* If TO_Y is in this line and TO_X was reached
8105 above, we scanned too far. We have to restore
8106 IT's settings to the ones before skipping. */
8107 RESTORE_IT (it, &it_backup, backup_data);
8108 reached = 6;
8109 }
8110 else
8111 {
8112 skip = skip2;
8113 if (skip == MOVE_POS_MATCH_OR_ZV)
8114 reached = 7;
8115 }
8116 }
8117 else
8118 {
8119 /* Check whether TO_Y is in this line. */
8120 line_height = it->max_ascent + it->max_descent;
8121 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8122
8123 if (to_y >= it->current_y
8124 && to_y < it->current_y + line_height)
8125 {
8126 /* When word-wrap is on, TO_X may lie past the end
8127 of a wrapped line. Then it->current is the
8128 character on the next line, so backtrack to the
8129 space before the wrap point. */
8130 if (skip == MOVE_LINE_CONTINUED
8131 && it->line_wrap == WORD_WRAP)
8132 {
8133 int prev_x = max (it->current_x - 1, 0);
8134 RESTORE_IT (it, &it_backup, backup_data);
8135 skip = move_it_in_display_line_to
8136 (it, -1, prev_x, MOVE_TO_X);
8137 }
8138 reached = 6;
8139 }
8140 }
8141
8142 if (reached)
8143 break;
8144 }
8145 else if (BUFFERP (it->object)
8146 && (it->method == GET_FROM_BUFFER
8147 || it->method == GET_FROM_STRETCH)
8148 && IT_CHARPOS (*it) >= to_charpos)
8149 skip = MOVE_POS_MATCH_OR_ZV;
8150 else
8151 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8152
8153 switch (skip)
8154 {
8155 case MOVE_POS_MATCH_OR_ZV:
8156 reached = 8;
8157 goto out;
8158
8159 case MOVE_NEWLINE_OR_CR:
8160 set_iterator_to_next (it, 1);
8161 it->continuation_lines_width = 0;
8162 break;
8163
8164 case MOVE_LINE_TRUNCATED:
8165 it->continuation_lines_width = 0;
8166 reseat_at_next_visible_line_start (it, 0);
8167 if ((op & MOVE_TO_POS) != 0
8168 && IT_CHARPOS (*it) > to_charpos)
8169 {
8170 reached = 9;
8171 goto out;
8172 }
8173 break;
8174
8175 case MOVE_LINE_CONTINUED:
8176 /* For continued lines ending in a tab, some of the glyphs
8177 associated with the tab are displayed on the current
8178 line. Since it->current_x does not include these glyphs,
8179 we use it->last_visible_x instead. */
8180 if (it->c == '\t')
8181 {
8182 it->continuation_lines_width += it->last_visible_x;
8183 /* When moving by vpos, ensure that the iterator really
8184 advances to the next line (bug#847, bug#969). Fixme:
8185 do we need to do this in other circumstances? */
8186 if (it->current_x != it->last_visible_x
8187 && (op & MOVE_TO_VPOS)
8188 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8189 {
8190 line_start_x = it->current_x + it->pixel_width
8191 - it->last_visible_x;
8192 set_iterator_to_next (it, 0);
8193 }
8194 }
8195 else
8196 it->continuation_lines_width += it->current_x;
8197 break;
8198
8199 default:
8200 abort ();
8201 }
8202
8203 /* Reset/increment for the next run. */
8204 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8205 it->current_x = line_start_x;
8206 line_start_x = 0;
8207 it->hpos = 0;
8208 it->current_y += it->max_ascent + it->max_descent;
8209 ++it->vpos;
8210 last_height = it->max_ascent + it->max_descent;
8211 last_max_ascent = it->max_ascent;
8212 it->max_ascent = it->max_descent = 0;
8213 }
8214
8215 out:
8216
8217 /* On text terminals, we may stop at the end of a line in the middle
8218 of a multi-character glyph. If the glyph itself is continued,
8219 i.e. it is actually displayed on the next line, don't treat this
8220 stopping point as valid; move to the next line instead (unless
8221 that brings us offscreen). */
8222 if (!FRAME_WINDOW_P (it->f)
8223 && op & MOVE_TO_POS
8224 && IT_CHARPOS (*it) == to_charpos
8225 && it->what == IT_CHARACTER
8226 && it->nglyphs > 1
8227 && it->line_wrap == WINDOW_WRAP
8228 && it->current_x == it->last_visible_x - 1
8229 && it->c != '\n'
8230 && it->c != '\t'
8231 && it->vpos < XFASTINT (it->w->window_end_vpos))
8232 {
8233 it->continuation_lines_width += it->current_x;
8234 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8235 it->current_y += it->max_ascent + it->max_descent;
8236 ++it->vpos;
8237 last_height = it->max_ascent + it->max_descent;
8238 last_max_ascent = it->max_ascent;
8239 }
8240
8241 if (backup_data)
8242 xfree (backup_data);
8243
8244 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8245 }
8246
8247
8248 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8249
8250 If DY > 0, move IT backward at least that many pixels. DY = 0
8251 means move IT backward to the preceding line start or BEGV. This
8252 function may move over more than DY pixels if IT->current_y - DY
8253 ends up in the middle of a line; in this case IT->current_y will be
8254 set to the top of the line moved to. */
8255
8256 void
8257 move_it_vertically_backward (struct it *it, int dy)
8258 {
8259 int nlines, h;
8260 struct it it2, it3;
8261 void *it2data = NULL, *it3data = NULL;
8262 EMACS_INT start_pos;
8263
8264 move_further_back:
8265 xassert (dy >= 0);
8266
8267 start_pos = IT_CHARPOS (*it);
8268
8269 /* Estimate how many newlines we must move back. */
8270 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8271
8272 /* Set the iterator's position that many lines back. */
8273 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8274 back_to_previous_visible_line_start (it);
8275
8276 /* Reseat the iterator here. When moving backward, we don't want
8277 reseat to skip forward over invisible text, set up the iterator
8278 to deliver from overlay strings at the new position etc. So,
8279 use reseat_1 here. */
8280 reseat_1 (it, it->current.pos, 1);
8281
8282 /* We are now surely at a line start. */
8283 it->current_x = it->hpos = 0;
8284 it->continuation_lines_width = 0;
8285
8286 /* Move forward and see what y-distance we moved. First move to the
8287 start of the next line so that we get its height. We need this
8288 height to be able to tell whether we reached the specified
8289 y-distance. */
8290 SAVE_IT (it2, *it, it2data);
8291 it2.max_ascent = it2.max_descent = 0;
8292 do
8293 {
8294 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8295 MOVE_TO_POS | MOVE_TO_VPOS);
8296 }
8297 while (!IT_POS_VALID_AFTER_MOVE_P (&it2));
8298 xassert (IT_CHARPOS (*it) >= BEGV);
8299 SAVE_IT (it3, it2, it3data);
8300
8301 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8302 xassert (IT_CHARPOS (*it) >= BEGV);
8303 /* H is the actual vertical distance from the position in *IT
8304 and the starting position. */
8305 h = it2.current_y - it->current_y;
8306 /* NLINES is the distance in number of lines. */
8307 nlines = it2.vpos - it->vpos;
8308
8309 /* Correct IT's y and vpos position
8310 so that they are relative to the starting point. */
8311 it->vpos -= nlines;
8312 it->current_y -= h;
8313
8314 if (dy == 0)
8315 {
8316 /* DY == 0 means move to the start of the screen line. The
8317 value of nlines is > 0 if continuation lines were involved. */
8318 RESTORE_IT (it, it, it2data);
8319 if (nlines > 0)
8320 move_it_by_lines (it, nlines);
8321 xfree (it3data);
8322 }
8323 else
8324 {
8325 /* The y-position we try to reach, relative to *IT.
8326 Note that H has been subtracted in front of the if-statement. */
8327 int target_y = it->current_y + h - dy;
8328 int y0 = it3.current_y;
8329 int y1;
8330 int line_height;
8331
8332 RESTORE_IT (&it3, &it3, it3data);
8333 y1 = line_bottom_y (&it3);
8334 line_height = y1 - y0;
8335 RESTORE_IT (it, it, it2data);
8336 /* If we did not reach target_y, try to move further backward if
8337 we can. If we moved too far backward, try to move forward. */
8338 if (target_y < it->current_y
8339 /* This is heuristic. In a window that's 3 lines high, with
8340 a line height of 13 pixels each, recentering with point
8341 on the bottom line will try to move -39/2 = 19 pixels
8342 backward. Try to avoid moving into the first line. */
8343 && (it->current_y - target_y
8344 > min (window_box_height (it->w), line_height * 2 / 3))
8345 && IT_CHARPOS (*it) > BEGV)
8346 {
8347 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8348 target_y - it->current_y));
8349 dy = it->current_y - target_y;
8350 goto move_further_back;
8351 }
8352 else if (target_y >= it->current_y + line_height
8353 && IT_CHARPOS (*it) < ZV)
8354 {
8355 /* Should move forward by at least one line, maybe more.
8356
8357 Note: Calling move_it_by_lines can be expensive on
8358 terminal frames, where compute_motion is used (via
8359 vmotion) to do the job, when there are very long lines
8360 and truncate-lines is nil. That's the reason for
8361 treating terminal frames specially here. */
8362
8363 if (!FRAME_WINDOW_P (it->f))
8364 move_it_vertically (it, target_y - (it->current_y + line_height));
8365 else
8366 {
8367 do
8368 {
8369 move_it_by_lines (it, 1);
8370 }
8371 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8372 }
8373 }
8374 }
8375 }
8376
8377
8378 /* Move IT by a specified amount of pixel lines DY. DY negative means
8379 move backwards. DY = 0 means move to start of screen line. At the
8380 end, IT will be on the start of a screen line. */
8381
8382 void
8383 move_it_vertically (struct it *it, int dy)
8384 {
8385 if (dy <= 0)
8386 move_it_vertically_backward (it, -dy);
8387 else
8388 {
8389 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8390 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8391 MOVE_TO_POS | MOVE_TO_Y);
8392 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8393
8394 /* If buffer ends in ZV without a newline, move to the start of
8395 the line to satisfy the post-condition. */
8396 if (IT_CHARPOS (*it) == ZV
8397 && ZV > BEGV
8398 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8399 move_it_by_lines (it, 0);
8400 }
8401 }
8402
8403
8404 /* Move iterator IT past the end of the text line it is in. */
8405
8406 void
8407 move_it_past_eol (struct it *it)
8408 {
8409 enum move_it_result rc;
8410
8411 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8412 if (rc == MOVE_NEWLINE_OR_CR)
8413 set_iterator_to_next (it, 0);
8414 }
8415
8416
8417 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8418 negative means move up. DVPOS == 0 means move to the start of the
8419 screen line.
8420
8421 Optimization idea: If we would know that IT->f doesn't use
8422 a face with proportional font, we could be faster for
8423 truncate-lines nil. */
8424
8425 void
8426 move_it_by_lines (struct it *it, int dvpos)
8427 {
8428
8429 /* The commented-out optimization uses vmotion on terminals. This
8430 gives bad results, because elements like it->what, on which
8431 callers such as pos_visible_p rely, aren't updated. */
8432 /* struct position pos;
8433 if (!FRAME_WINDOW_P (it->f))
8434 {
8435 struct text_pos textpos;
8436
8437 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8438 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8439 reseat (it, textpos, 1);
8440 it->vpos += pos.vpos;
8441 it->current_y += pos.vpos;
8442 }
8443 else */
8444
8445 if (dvpos == 0)
8446 {
8447 /* DVPOS == 0 means move to the start of the screen line. */
8448 move_it_vertically_backward (it, 0);
8449 xassert (it->current_x == 0 && it->hpos == 0);
8450 /* Let next call to line_bottom_y calculate real line height */
8451 last_height = 0;
8452 }
8453 else if (dvpos > 0)
8454 {
8455 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
8456 if (!IT_POS_VALID_AFTER_MOVE_P (it))
8457 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
8458 }
8459 else
8460 {
8461 struct it it2;
8462 void *it2data = NULL;
8463 EMACS_INT start_charpos, i;
8464
8465 /* Start at the beginning of the screen line containing IT's
8466 position. This may actually move vertically backwards,
8467 in case of overlays, so adjust dvpos accordingly. */
8468 dvpos += it->vpos;
8469 move_it_vertically_backward (it, 0);
8470 dvpos -= it->vpos;
8471
8472 /* Go back -DVPOS visible lines and reseat the iterator there. */
8473 start_charpos = IT_CHARPOS (*it);
8474 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
8475 back_to_previous_visible_line_start (it);
8476 reseat (it, it->current.pos, 1);
8477
8478 /* Move further back if we end up in a string or an image. */
8479 while (!IT_POS_VALID_AFTER_MOVE_P (it))
8480 {
8481 /* First try to move to start of display line. */
8482 dvpos += it->vpos;
8483 move_it_vertically_backward (it, 0);
8484 dvpos -= it->vpos;
8485 if (IT_POS_VALID_AFTER_MOVE_P (it))
8486 break;
8487 /* If start of line is still in string or image,
8488 move further back. */
8489 back_to_previous_visible_line_start (it);
8490 reseat (it, it->current.pos, 1);
8491 dvpos--;
8492 }
8493
8494 it->current_x = it->hpos = 0;
8495
8496 /* Above call may have moved too far if continuation lines
8497 are involved. Scan forward and see if it did. */
8498 SAVE_IT (it2, *it, it2data);
8499 it2.vpos = it2.current_y = 0;
8500 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
8501 it->vpos -= it2.vpos;
8502 it->current_y -= it2.current_y;
8503 it->current_x = it->hpos = 0;
8504
8505 /* If we moved too far back, move IT some lines forward. */
8506 if (it2.vpos > -dvpos)
8507 {
8508 int delta = it2.vpos + dvpos;
8509
8510 RESTORE_IT (&it2, &it2, it2data);
8511 SAVE_IT (it2, *it, it2data);
8512 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
8513 /* Move back again if we got too far ahead. */
8514 if (IT_CHARPOS (*it) >= start_charpos)
8515 RESTORE_IT (it, &it2, it2data);
8516 else
8517 xfree (it2data);
8518 }
8519 else
8520 RESTORE_IT (it, it, it2data);
8521 }
8522 }
8523
8524 /* Return 1 if IT points into the middle of a display vector. */
8525
8526 int
8527 in_display_vector_p (struct it *it)
8528 {
8529 return (it->method == GET_FROM_DISPLAY_VECTOR
8530 && it->current.dpvec_index > 0
8531 && it->dpvec + it->current.dpvec_index != it->dpend);
8532 }
8533
8534 \f
8535 /***********************************************************************
8536 Messages
8537 ***********************************************************************/
8538
8539
8540 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
8541 to *Messages*. */
8542
8543 void
8544 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
8545 {
8546 Lisp_Object args[3];
8547 Lisp_Object msg, fmt;
8548 char *buffer;
8549 EMACS_INT len;
8550 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
8551 USE_SAFE_ALLOCA;
8552
8553 /* Do nothing if called asynchronously. Inserting text into
8554 a buffer may call after-change-functions and alike and
8555 that would means running Lisp asynchronously. */
8556 if (handling_signal)
8557 return;
8558
8559 fmt = msg = Qnil;
8560 GCPRO4 (fmt, msg, arg1, arg2);
8561
8562 args[0] = fmt = build_string (format);
8563 args[1] = arg1;
8564 args[2] = arg2;
8565 msg = Fformat (3, args);
8566
8567 len = SBYTES (msg) + 1;
8568 SAFE_ALLOCA (buffer, char *, len);
8569 memcpy (buffer, SDATA (msg), len);
8570
8571 message_dolog (buffer, len - 1, 1, 0);
8572 SAFE_FREE ();
8573
8574 UNGCPRO;
8575 }
8576
8577
8578 /* Output a newline in the *Messages* buffer if "needs" one. */
8579
8580 void
8581 message_log_maybe_newline (void)
8582 {
8583 if (message_log_need_newline)
8584 message_dolog ("", 0, 1, 0);
8585 }
8586
8587
8588 /* Add a string M of length NBYTES to the message log, optionally
8589 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
8590 nonzero, means interpret the contents of M as multibyte. This
8591 function calls low-level routines in order to bypass text property
8592 hooks, etc. which might not be safe to run.
8593
8594 This may GC (insert may run before/after change hooks),
8595 so the buffer M must NOT point to a Lisp string. */
8596
8597 void
8598 message_dolog (const char *m, EMACS_INT nbytes, int nlflag, int multibyte)
8599 {
8600 const unsigned char *msg = (const unsigned char *) m;
8601
8602 if (!NILP (Vmemory_full))
8603 return;
8604
8605 if (!NILP (Vmessage_log_max))
8606 {
8607 struct buffer *oldbuf;
8608 Lisp_Object oldpoint, oldbegv, oldzv;
8609 int old_windows_or_buffers_changed = windows_or_buffers_changed;
8610 EMACS_INT point_at_end = 0;
8611 EMACS_INT zv_at_end = 0;
8612 Lisp_Object old_deactivate_mark, tem;
8613 struct gcpro gcpro1;
8614
8615 old_deactivate_mark = Vdeactivate_mark;
8616 oldbuf = current_buffer;
8617 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
8618 BVAR (current_buffer, undo_list) = Qt;
8619
8620 oldpoint = message_dolog_marker1;
8621 set_marker_restricted (oldpoint, make_number (PT), Qnil);
8622 oldbegv = message_dolog_marker2;
8623 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
8624 oldzv = message_dolog_marker3;
8625 set_marker_restricted (oldzv, make_number (ZV), Qnil);
8626 GCPRO1 (old_deactivate_mark);
8627
8628 if (PT == Z)
8629 point_at_end = 1;
8630 if (ZV == Z)
8631 zv_at_end = 1;
8632
8633 BEGV = BEG;
8634 BEGV_BYTE = BEG_BYTE;
8635 ZV = Z;
8636 ZV_BYTE = Z_BYTE;
8637 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8638
8639 /* Insert the string--maybe converting multibyte to single byte
8640 or vice versa, so that all the text fits the buffer. */
8641 if (multibyte
8642 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
8643 {
8644 EMACS_INT i;
8645 int c, char_bytes;
8646 char work[1];
8647
8648 /* Convert a multibyte string to single-byte
8649 for the *Message* buffer. */
8650 for (i = 0; i < nbytes; i += char_bytes)
8651 {
8652 c = string_char_and_length (msg + i, &char_bytes);
8653 work[0] = (ASCII_CHAR_P (c)
8654 ? c
8655 : multibyte_char_to_unibyte (c));
8656 insert_1_both (work, 1, 1, 1, 0, 0);
8657 }
8658 }
8659 else if (! multibyte
8660 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
8661 {
8662 EMACS_INT i;
8663 int c, char_bytes;
8664 unsigned char str[MAX_MULTIBYTE_LENGTH];
8665 /* Convert a single-byte string to multibyte
8666 for the *Message* buffer. */
8667 for (i = 0; i < nbytes; i++)
8668 {
8669 c = msg[i];
8670 MAKE_CHAR_MULTIBYTE (c);
8671 char_bytes = CHAR_STRING (c, str);
8672 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
8673 }
8674 }
8675 else if (nbytes)
8676 insert_1 (m, nbytes, 1, 0, 0);
8677
8678 if (nlflag)
8679 {
8680 EMACS_INT this_bol, this_bol_byte, prev_bol, prev_bol_byte;
8681 unsigned long int dups;
8682 insert_1 ("\n", 1, 1, 0, 0);
8683
8684 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
8685 this_bol = PT;
8686 this_bol_byte = PT_BYTE;
8687
8688 /* See if this line duplicates the previous one.
8689 If so, combine duplicates. */
8690 if (this_bol > BEG)
8691 {
8692 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
8693 prev_bol = PT;
8694 prev_bol_byte = PT_BYTE;
8695
8696 dups = message_log_check_duplicate (prev_bol_byte,
8697 this_bol_byte);
8698 if (dups)
8699 {
8700 del_range_both (prev_bol, prev_bol_byte,
8701 this_bol, this_bol_byte, 0);
8702 if (dups > 1)
8703 {
8704 char dupstr[40];
8705 int duplen;
8706
8707 /* If you change this format, don't forget to also
8708 change message_log_check_duplicate. */
8709 sprintf (dupstr, " [%lu times]", dups);
8710 duplen = strlen (dupstr);
8711 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
8712 insert_1 (dupstr, duplen, 1, 0, 1);
8713 }
8714 }
8715 }
8716
8717 /* If we have more than the desired maximum number of lines
8718 in the *Messages* buffer now, delete the oldest ones.
8719 This is safe because we don't have undo in this buffer. */
8720
8721 if (NATNUMP (Vmessage_log_max))
8722 {
8723 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
8724 -XFASTINT (Vmessage_log_max) - 1, 0);
8725 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
8726 }
8727 }
8728 BEGV = XMARKER (oldbegv)->charpos;
8729 BEGV_BYTE = marker_byte_position (oldbegv);
8730
8731 if (zv_at_end)
8732 {
8733 ZV = Z;
8734 ZV_BYTE = Z_BYTE;
8735 }
8736 else
8737 {
8738 ZV = XMARKER (oldzv)->charpos;
8739 ZV_BYTE = marker_byte_position (oldzv);
8740 }
8741
8742 if (point_at_end)
8743 TEMP_SET_PT_BOTH (Z, Z_BYTE);
8744 else
8745 /* We can't do Fgoto_char (oldpoint) because it will run some
8746 Lisp code. */
8747 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
8748 XMARKER (oldpoint)->bytepos);
8749
8750 UNGCPRO;
8751 unchain_marker (XMARKER (oldpoint));
8752 unchain_marker (XMARKER (oldbegv));
8753 unchain_marker (XMARKER (oldzv));
8754
8755 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
8756 set_buffer_internal (oldbuf);
8757 if (NILP (tem))
8758 windows_or_buffers_changed = old_windows_or_buffers_changed;
8759 message_log_need_newline = !nlflag;
8760 Vdeactivate_mark = old_deactivate_mark;
8761 }
8762 }
8763
8764
8765 /* We are at the end of the buffer after just having inserted a newline.
8766 (Note: We depend on the fact we won't be crossing the gap.)
8767 Check to see if the most recent message looks a lot like the previous one.
8768 Return 0 if different, 1 if the new one should just replace it, or a
8769 value N > 1 if we should also append " [N times]". */
8770
8771 static unsigned long int
8772 message_log_check_duplicate (EMACS_INT prev_bol_byte, EMACS_INT this_bol_byte)
8773 {
8774 EMACS_INT i;
8775 EMACS_INT len = Z_BYTE - 1 - this_bol_byte;
8776 int seen_dots = 0;
8777 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
8778 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
8779
8780 for (i = 0; i < len; i++)
8781 {
8782 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
8783 seen_dots = 1;
8784 if (p1[i] != p2[i])
8785 return seen_dots;
8786 }
8787 p1 += len;
8788 if (*p1 == '\n')
8789 return 2;
8790 if (*p1++ == ' ' && *p1++ == '[')
8791 {
8792 char *pend;
8793 unsigned long int n = strtoul ((char *) p1, &pend, 10);
8794 if (strncmp (pend, " times]\n", 8) == 0)
8795 return n+1;
8796 }
8797 return 0;
8798 }
8799 \f
8800
8801 /* Display an echo area message M with a specified length of NBYTES
8802 bytes. The string may include null characters. If M is 0, clear
8803 out any existing message, and let the mini-buffer text show
8804 through.
8805
8806 This may GC, so the buffer M must NOT point to a Lisp string. */
8807
8808 void
8809 message2 (const char *m, EMACS_INT nbytes, int multibyte)
8810 {
8811 /* First flush out any partial line written with print. */
8812 message_log_maybe_newline ();
8813 if (m)
8814 message_dolog (m, nbytes, 1, multibyte);
8815 message2_nolog (m, nbytes, multibyte);
8816 }
8817
8818
8819 /* The non-logging counterpart of message2. */
8820
8821 void
8822 message2_nolog (const char *m, EMACS_INT nbytes, int multibyte)
8823 {
8824 struct frame *sf = SELECTED_FRAME ();
8825 message_enable_multibyte = multibyte;
8826
8827 if (FRAME_INITIAL_P (sf))
8828 {
8829 if (noninteractive_need_newline)
8830 putc ('\n', stderr);
8831 noninteractive_need_newline = 0;
8832 if (m)
8833 fwrite (m, nbytes, 1, stderr);
8834 if (cursor_in_echo_area == 0)
8835 fprintf (stderr, "\n");
8836 fflush (stderr);
8837 }
8838 /* A null message buffer means that the frame hasn't really been
8839 initialized yet. Error messages get reported properly by
8840 cmd_error, so this must be just an informative message; toss it. */
8841 else if (INTERACTIVE
8842 && sf->glyphs_initialized_p
8843 && FRAME_MESSAGE_BUF (sf))
8844 {
8845 Lisp_Object mini_window;
8846 struct frame *f;
8847
8848 /* Get the frame containing the mini-buffer
8849 that the selected frame is using. */
8850 mini_window = FRAME_MINIBUF_WINDOW (sf);
8851 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
8852
8853 FRAME_SAMPLE_VISIBILITY (f);
8854 if (FRAME_VISIBLE_P (sf)
8855 && ! FRAME_VISIBLE_P (f))
8856 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
8857
8858 if (m)
8859 {
8860 set_message (m, Qnil, nbytes, multibyte);
8861 if (minibuffer_auto_raise)
8862 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
8863 }
8864 else
8865 clear_message (1, 1);
8866
8867 do_pending_window_change (0);
8868 echo_area_display (1);
8869 do_pending_window_change (0);
8870 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8871 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8872 }
8873 }
8874
8875
8876 /* Display an echo area message M with a specified length of NBYTES
8877 bytes. The string may include null characters. If M is not a
8878 string, clear out any existing message, and let the mini-buffer
8879 text show through.
8880
8881 This function cancels echoing. */
8882
8883 void
8884 message3 (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8885 {
8886 struct gcpro gcpro1;
8887
8888 GCPRO1 (m);
8889 clear_message (1,1);
8890 cancel_echoing ();
8891
8892 /* First flush out any partial line written with print. */
8893 message_log_maybe_newline ();
8894 if (STRINGP (m))
8895 {
8896 char *buffer;
8897 USE_SAFE_ALLOCA;
8898
8899 SAFE_ALLOCA (buffer, char *, nbytes);
8900 memcpy (buffer, SDATA (m), nbytes);
8901 message_dolog (buffer, nbytes, 1, multibyte);
8902 SAFE_FREE ();
8903 }
8904 message3_nolog (m, nbytes, multibyte);
8905
8906 UNGCPRO;
8907 }
8908
8909
8910 /* The non-logging version of message3.
8911 This does not cancel echoing, because it is used for echoing.
8912 Perhaps we need to make a separate function for echoing
8913 and make this cancel echoing. */
8914
8915 void
8916 message3_nolog (Lisp_Object m, EMACS_INT nbytes, int multibyte)
8917 {
8918 struct frame *sf = SELECTED_FRAME ();
8919 message_enable_multibyte = multibyte;
8920
8921 if (FRAME_INITIAL_P (sf))
8922 {
8923 if (noninteractive_need_newline)
8924 putc ('\n', stderr);
8925 noninteractive_need_newline = 0;
8926 if (STRINGP (m))
8927 fwrite (SDATA (m), nbytes, 1, stderr);
8928 if (cursor_in_echo_area == 0)
8929 fprintf (stderr, "\n");
8930 fflush (stderr);
8931 }
8932 /* A null message buffer means that the frame hasn't really been
8933 initialized yet. Error messages get reported properly by
8934 cmd_error, so this must be just an informative message; toss it. */
8935 else if (INTERACTIVE
8936 && sf->glyphs_initialized_p
8937 && FRAME_MESSAGE_BUF (sf))
8938 {
8939 Lisp_Object mini_window;
8940 Lisp_Object frame;
8941 struct frame *f;
8942
8943 /* Get the frame containing the mini-buffer
8944 that the selected frame is using. */
8945 mini_window = FRAME_MINIBUF_WINDOW (sf);
8946 frame = XWINDOW (mini_window)->frame;
8947 f = XFRAME (frame);
8948
8949 FRAME_SAMPLE_VISIBILITY (f);
8950 if (FRAME_VISIBLE_P (sf)
8951 && !FRAME_VISIBLE_P (f))
8952 Fmake_frame_visible (frame);
8953
8954 if (STRINGP (m) && SCHARS (m) > 0)
8955 {
8956 set_message (NULL, m, nbytes, multibyte);
8957 if (minibuffer_auto_raise)
8958 Fraise_frame (frame);
8959 /* Assume we are not echoing.
8960 (If we are, echo_now will override this.) */
8961 echo_message_buffer = Qnil;
8962 }
8963 else
8964 clear_message (1, 1);
8965
8966 do_pending_window_change (0);
8967 echo_area_display (1);
8968 do_pending_window_change (0);
8969 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
8970 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
8971 }
8972 }
8973
8974
8975 /* Display a null-terminated echo area message M. If M is 0, clear
8976 out any existing message, and let the mini-buffer text show through.
8977
8978 The buffer M must continue to exist until after the echo area gets
8979 cleared or some other message gets displayed there. Do not pass
8980 text that is stored in a Lisp string. Do not pass text in a buffer
8981 that was alloca'd. */
8982
8983 void
8984 message1 (const char *m)
8985 {
8986 message2 (m, (m ? strlen (m) : 0), 0);
8987 }
8988
8989
8990 /* The non-logging counterpart of message1. */
8991
8992 void
8993 message1_nolog (const char *m)
8994 {
8995 message2_nolog (m, (m ? strlen (m) : 0), 0);
8996 }
8997
8998 /* Display a message M which contains a single %s
8999 which gets replaced with STRING. */
9000
9001 void
9002 message_with_string (const char *m, Lisp_Object string, int log)
9003 {
9004 CHECK_STRING (string);
9005
9006 if (noninteractive)
9007 {
9008 if (m)
9009 {
9010 if (noninteractive_need_newline)
9011 putc ('\n', stderr);
9012 noninteractive_need_newline = 0;
9013 fprintf (stderr, m, SDATA (string));
9014 if (!cursor_in_echo_area)
9015 fprintf (stderr, "\n");
9016 fflush (stderr);
9017 }
9018 }
9019 else if (INTERACTIVE)
9020 {
9021 /* The frame whose minibuffer we're going to display the message on.
9022 It may be larger than the selected frame, so we need
9023 to use its buffer, not the selected frame's buffer. */
9024 Lisp_Object mini_window;
9025 struct frame *f, *sf = SELECTED_FRAME ();
9026
9027 /* Get the frame containing the minibuffer
9028 that the selected frame is using. */
9029 mini_window = FRAME_MINIBUF_WINDOW (sf);
9030 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9031
9032 /* A null message buffer means that the frame hasn't really been
9033 initialized yet. Error messages get reported properly by
9034 cmd_error, so this must be just an informative message; toss it. */
9035 if (FRAME_MESSAGE_BUF (f))
9036 {
9037 Lisp_Object args[2], msg;
9038 struct gcpro gcpro1, gcpro2;
9039
9040 args[0] = build_string (m);
9041 args[1] = msg = string;
9042 GCPRO2 (args[0], msg);
9043 gcpro1.nvars = 2;
9044
9045 msg = Fformat (2, args);
9046
9047 if (log)
9048 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9049 else
9050 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9051
9052 UNGCPRO;
9053
9054 /* Print should start at the beginning of the message
9055 buffer next time. */
9056 message_buf_print = 0;
9057 }
9058 }
9059 }
9060
9061
9062 /* Dump an informative message to the minibuf. If M is 0, clear out
9063 any existing message, and let the mini-buffer text show through. */
9064
9065 static void
9066 vmessage (const char *m, va_list ap)
9067 {
9068 if (noninteractive)
9069 {
9070 if (m)
9071 {
9072 if (noninteractive_need_newline)
9073 putc ('\n', stderr);
9074 noninteractive_need_newline = 0;
9075 vfprintf (stderr, m, ap);
9076 if (cursor_in_echo_area == 0)
9077 fprintf (stderr, "\n");
9078 fflush (stderr);
9079 }
9080 }
9081 else if (INTERACTIVE)
9082 {
9083 /* The frame whose mini-buffer we're going to display the message
9084 on. It may be larger than the selected frame, so we need to
9085 use its buffer, not the selected frame's buffer. */
9086 Lisp_Object mini_window;
9087 struct frame *f, *sf = SELECTED_FRAME ();
9088
9089 /* Get the frame containing the mini-buffer
9090 that the selected frame is using. */
9091 mini_window = FRAME_MINIBUF_WINDOW (sf);
9092 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9093
9094 /* A null message buffer means that the frame hasn't really been
9095 initialized yet. Error messages get reported properly by
9096 cmd_error, so this must be just an informative message; toss
9097 it. */
9098 if (FRAME_MESSAGE_BUF (f))
9099 {
9100 if (m)
9101 {
9102 size_t len;
9103
9104 len = doprnt (FRAME_MESSAGE_BUF (f),
9105 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9106
9107 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9108 }
9109 else
9110 message1 (0);
9111
9112 /* Print should start at the beginning of the message
9113 buffer next time. */
9114 message_buf_print = 0;
9115 }
9116 }
9117 }
9118
9119 void
9120 message (const char *m, ...)
9121 {
9122 va_list ap;
9123 va_start (ap, m);
9124 vmessage (m, ap);
9125 va_end (ap);
9126 }
9127
9128
9129 #if 0
9130 /* The non-logging version of message. */
9131
9132 void
9133 message_nolog (const char *m, ...)
9134 {
9135 Lisp_Object old_log_max;
9136 va_list ap;
9137 va_start (ap, m);
9138 old_log_max = Vmessage_log_max;
9139 Vmessage_log_max = Qnil;
9140 vmessage (m, ap);
9141 Vmessage_log_max = old_log_max;
9142 va_end (ap);
9143 }
9144 #endif
9145
9146
9147 /* Display the current message in the current mini-buffer. This is
9148 only called from error handlers in process.c, and is not time
9149 critical. */
9150
9151 void
9152 update_echo_area (void)
9153 {
9154 if (!NILP (echo_area_buffer[0]))
9155 {
9156 Lisp_Object string;
9157 string = Fcurrent_message ();
9158 message3 (string, SBYTES (string),
9159 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9160 }
9161 }
9162
9163
9164 /* Make sure echo area buffers in `echo_buffers' are live.
9165 If they aren't, make new ones. */
9166
9167 static void
9168 ensure_echo_area_buffers (void)
9169 {
9170 int i;
9171
9172 for (i = 0; i < 2; ++i)
9173 if (!BUFFERP (echo_buffer[i])
9174 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9175 {
9176 char name[30];
9177 Lisp_Object old_buffer;
9178 int j;
9179
9180 old_buffer = echo_buffer[i];
9181 sprintf (name, " *Echo Area %d*", i);
9182 echo_buffer[i] = Fget_buffer_create (build_string (name));
9183 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9184 /* to force word wrap in echo area -
9185 it was decided to postpone this*/
9186 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9187
9188 for (j = 0; j < 2; ++j)
9189 if (EQ (old_buffer, echo_area_buffer[j]))
9190 echo_area_buffer[j] = echo_buffer[i];
9191 }
9192 }
9193
9194
9195 /* Call FN with args A1..A4 with either the current or last displayed
9196 echo_area_buffer as current buffer.
9197
9198 WHICH zero means use the current message buffer
9199 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9200 from echo_buffer[] and clear it.
9201
9202 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9203 suitable buffer from echo_buffer[] and clear it.
9204
9205 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9206 that the current message becomes the last displayed one, make
9207 choose a suitable buffer for echo_area_buffer[0], and clear it.
9208
9209 Value is what FN returns. */
9210
9211 static int
9212 with_echo_area_buffer (struct window *w, int which,
9213 int (*fn) (EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT),
9214 EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9215 {
9216 Lisp_Object buffer;
9217 int this_one, the_other, clear_buffer_p, rc;
9218 int count = SPECPDL_INDEX ();
9219
9220 /* If buffers aren't live, make new ones. */
9221 ensure_echo_area_buffers ();
9222
9223 clear_buffer_p = 0;
9224
9225 if (which == 0)
9226 this_one = 0, the_other = 1;
9227 else if (which > 0)
9228 this_one = 1, the_other = 0;
9229 else
9230 {
9231 this_one = 0, the_other = 1;
9232 clear_buffer_p = 1;
9233
9234 /* We need a fresh one in case the current echo buffer equals
9235 the one containing the last displayed echo area message. */
9236 if (!NILP (echo_area_buffer[this_one])
9237 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9238 echo_area_buffer[this_one] = Qnil;
9239 }
9240
9241 /* Choose a suitable buffer from echo_buffer[] is we don't
9242 have one. */
9243 if (NILP (echo_area_buffer[this_one]))
9244 {
9245 echo_area_buffer[this_one]
9246 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9247 ? echo_buffer[the_other]
9248 : echo_buffer[this_one]);
9249 clear_buffer_p = 1;
9250 }
9251
9252 buffer = echo_area_buffer[this_one];
9253
9254 /* Don't get confused by reusing the buffer used for echoing
9255 for a different purpose. */
9256 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9257 cancel_echoing ();
9258
9259 record_unwind_protect (unwind_with_echo_area_buffer,
9260 with_echo_area_buffer_unwind_data (w));
9261
9262 /* Make the echo area buffer current. Note that for display
9263 purposes, it is not necessary that the displayed window's buffer
9264 == current_buffer, except for text property lookup. So, let's
9265 only set that buffer temporarily here without doing a full
9266 Fset_window_buffer. We must also change w->pointm, though,
9267 because otherwise an assertions in unshow_buffer fails, and Emacs
9268 aborts. */
9269 set_buffer_internal_1 (XBUFFER (buffer));
9270 if (w)
9271 {
9272 w->buffer = buffer;
9273 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9274 }
9275
9276 BVAR (current_buffer, undo_list) = Qt;
9277 BVAR (current_buffer, read_only) = Qnil;
9278 specbind (Qinhibit_read_only, Qt);
9279 specbind (Qinhibit_modification_hooks, Qt);
9280
9281 if (clear_buffer_p && Z > BEG)
9282 del_range (BEG, Z);
9283
9284 xassert (BEGV >= BEG);
9285 xassert (ZV <= Z && ZV >= BEGV);
9286
9287 rc = fn (a1, a2, a3, a4);
9288
9289 xassert (BEGV >= BEG);
9290 xassert (ZV <= Z && ZV >= BEGV);
9291
9292 unbind_to (count, Qnil);
9293 return rc;
9294 }
9295
9296
9297 /* Save state that should be preserved around the call to the function
9298 FN called in with_echo_area_buffer. */
9299
9300 static Lisp_Object
9301 with_echo_area_buffer_unwind_data (struct window *w)
9302 {
9303 int i = 0;
9304 Lisp_Object vector, tmp;
9305
9306 /* Reduce consing by keeping one vector in
9307 Vwith_echo_area_save_vector. */
9308 vector = Vwith_echo_area_save_vector;
9309 Vwith_echo_area_save_vector = Qnil;
9310
9311 if (NILP (vector))
9312 vector = Fmake_vector (make_number (7), Qnil);
9313
9314 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9315 ASET (vector, i, Vdeactivate_mark); ++i;
9316 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9317
9318 if (w)
9319 {
9320 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9321 ASET (vector, i, w->buffer); ++i;
9322 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9323 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9324 }
9325 else
9326 {
9327 int end = i + 4;
9328 for (; i < end; ++i)
9329 ASET (vector, i, Qnil);
9330 }
9331
9332 xassert (i == ASIZE (vector));
9333 return vector;
9334 }
9335
9336
9337 /* Restore global state from VECTOR which was created by
9338 with_echo_area_buffer_unwind_data. */
9339
9340 static Lisp_Object
9341 unwind_with_echo_area_buffer (Lisp_Object vector)
9342 {
9343 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9344 Vdeactivate_mark = AREF (vector, 1);
9345 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9346
9347 if (WINDOWP (AREF (vector, 3)))
9348 {
9349 struct window *w;
9350 Lisp_Object buffer, charpos, bytepos;
9351
9352 w = XWINDOW (AREF (vector, 3));
9353 buffer = AREF (vector, 4);
9354 charpos = AREF (vector, 5);
9355 bytepos = AREF (vector, 6);
9356
9357 w->buffer = buffer;
9358 set_marker_both (w->pointm, buffer,
9359 XFASTINT (charpos), XFASTINT (bytepos));
9360 }
9361
9362 Vwith_echo_area_save_vector = vector;
9363 return Qnil;
9364 }
9365
9366
9367 /* Set up the echo area for use by print functions. MULTIBYTE_P
9368 non-zero means we will print multibyte. */
9369
9370 void
9371 setup_echo_area_for_printing (int multibyte_p)
9372 {
9373 /* If we can't find an echo area any more, exit. */
9374 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9375 Fkill_emacs (Qnil);
9376
9377 ensure_echo_area_buffers ();
9378
9379 if (!message_buf_print)
9380 {
9381 /* A message has been output since the last time we printed.
9382 Choose a fresh echo area buffer. */
9383 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9384 echo_area_buffer[0] = echo_buffer[1];
9385 else
9386 echo_area_buffer[0] = echo_buffer[0];
9387
9388 /* Switch to that buffer and clear it. */
9389 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9390 BVAR (current_buffer, truncate_lines) = Qnil;
9391
9392 if (Z > BEG)
9393 {
9394 int count = SPECPDL_INDEX ();
9395 specbind (Qinhibit_read_only, Qt);
9396 /* Note that undo recording is always disabled. */
9397 del_range (BEG, Z);
9398 unbind_to (count, Qnil);
9399 }
9400 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9401
9402 /* Set up the buffer for the multibyteness we need. */
9403 if (multibyte_p
9404 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9405 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9406
9407 /* Raise the frame containing the echo area. */
9408 if (minibuffer_auto_raise)
9409 {
9410 struct frame *sf = SELECTED_FRAME ();
9411 Lisp_Object mini_window;
9412 mini_window = FRAME_MINIBUF_WINDOW (sf);
9413 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9414 }
9415
9416 message_log_maybe_newline ();
9417 message_buf_print = 1;
9418 }
9419 else
9420 {
9421 if (NILP (echo_area_buffer[0]))
9422 {
9423 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9424 echo_area_buffer[0] = echo_buffer[1];
9425 else
9426 echo_area_buffer[0] = echo_buffer[0];
9427 }
9428
9429 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9430 {
9431 /* Someone switched buffers between print requests. */
9432 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9433 BVAR (current_buffer, truncate_lines) = Qnil;
9434 }
9435 }
9436 }
9437
9438
9439 /* Display an echo area message in window W. Value is non-zero if W's
9440 height is changed. If display_last_displayed_message_p is
9441 non-zero, display the message that was last displayed, otherwise
9442 display the current message. */
9443
9444 static int
9445 display_echo_area (struct window *w)
9446 {
9447 int i, no_message_p, window_height_changed_p, count;
9448
9449 /* Temporarily disable garbage collections while displaying the echo
9450 area. This is done because a GC can print a message itself.
9451 That message would modify the echo area buffer's contents while a
9452 redisplay of the buffer is going on, and seriously confuse
9453 redisplay. */
9454 count = inhibit_garbage_collection ();
9455
9456 /* If there is no message, we must call display_echo_area_1
9457 nevertheless because it resizes the window. But we will have to
9458 reset the echo_area_buffer in question to nil at the end because
9459 with_echo_area_buffer will sets it to an empty buffer. */
9460 i = display_last_displayed_message_p ? 1 : 0;
9461 no_message_p = NILP (echo_area_buffer[i]);
9462
9463 window_height_changed_p
9464 = with_echo_area_buffer (w, display_last_displayed_message_p,
9465 display_echo_area_1,
9466 (intptr_t) w, Qnil, 0, 0);
9467
9468 if (no_message_p)
9469 echo_area_buffer[i] = Qnil;
9470
9471 unbind_to (count, Qnil);
9472 return window_height_changed_p;
9473 }
9474
9475
9476 /* Helper for display_echo_area. Display the current buffer which
9477 contains the current echo area message in window W, a mini-window,
9478 a pointer to which is passed in A1. A2..A4 are currently not used.
9479 Change the height of W so that all of the message is displayed.
9480 Value is non-zero if height of W was changed. */
9481
9482 static int
9483 display_echo_area_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9484 {
9485 intptr_t i1 = a1;
9486 struct window *w = (struct window *) i1;
9487 Lisp_Object window;
9488 struct text_pos start;
9489 int window_height_changed_p = 0;
9490
9491 /* Do this before displaying, so that we have a large enough glyph
9492 matrix for the display. If we can't get enough space for the
9493 whole text, display the last N lines. That works by setting w->start. */
9494 window_height_changed_p = resize_mini_window (w, 0);
9495
9496 /* Use the starting position chosen by resize_mini_window. */
9497 SET_TEXT_POS_FROM_MARKER (start, w->start);
9498
9499 /* Display. */
9500 clear_glyph_matrix (w->desired_matrix);
9501 XSETWINDOW (window, w);
9502 try_window (window, start, 0);
9503
9504 return window_height_changed_p;
9505 }
9506
9507
9508 /* Resize the echo area window to exactly the size needed for the
9509 currently displayed message, if there is one. If a mini-buffer
9510 is active, don't shrink it. */
9511
9512 void
9513 resize_echo_area_exactly (void)
9514 {
9515 if (BUFFERP (echo_area_buffer[0])
9516 && WINDOWP (echo_area_window))
9517 {
9518 struct window *w = XWINDOW (echo_area_window);
9519 int resized_p;
9520 Lisp_Object resize_exactly;
9521
9522 if (minibuf_level == 0)
9523 resize_exactly = Qt;
9524 else
9525 resize_exactly = Qnil;
9526
9527 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
9528 (intptr_t) w, resize_exactly,
9529 0, 0);
9530 if (resized_p)
9531 {
9532 ++windows_or_buffers_changed;
9533 ++update_mode_lines;
9534 redisplay_internal ();
9535 }
9536 }
9537 }
9538
9539
9540 /* Callback function for with_echo_area_buffer, when used from
9541 resize_echo_area_exactly. A1 contains a pointer to the window to
9542 resize, EXACTLY non-nil means resize the mini-window exactly to the
9543 size of the text displayed. A3 and A4 are not used. Value is what
9544 resize_mini_window returns. */
9545
9546 static int
9547 resize_mini_window_1 (EMACS_INT a1, Lisp_Object exactly, EMACS_INT a3, EMACS_INT a4)
9548 {
9549 intptr_t i1 = a1;
9550 return resize_mini_window ((struct window *) i1, !NILP (exactly));
9551 }
9552
9553
9554 /* Resize mini-window W to fit the size of its contents. EXACT_P
9555 means size the window exactly to the size needed. Otherwise, it's
9556 only enlarged until W's buffer is empty.
9557
9558 Set W->start to the right place to begin display. If the whole
9559 contents fit, start at the beginning. Otherwise, start so as
9560 to make the end of the contents appear. This is particularly
9561 important for y-or-n-p, but seems desirable generally.
9562
9563 Value is non-zero if the window height has been changed. */
9564
9565 int
9566 resize_mini_window (struct window *w, int exact_p)
9567 {
9568 struct frame *f = XFRAME (w->frame);
9569 int window_height_changed_p = 0;
9570
9571 xassert (MINI_WINDOW_P (w));
9572
9573 /* By default, start display at the beginning. */
9574 set_marker_both (w->start, w->buffer,
9575 BUF_BEGV (XBUFFER (w->buffer)),
9576 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
9577
9578 /* Don't resize windows while redisplaying a window; it would
9579 confuse redisplay functions when the size of the window they are
9580 displaying changes from under them. Such a resizing can happen,
9581 for instance, when which-func prints a long message while
9582 we are running fontification-functions. We're running these
9583 functions with safe_call which binds inhibit-redisplay to t. */
9584 if (!NILP (Vinhibit_redisplay))
9585 return 0;
9586
9587 /* Nil means don't try to resize. */
9588 if (NILP (Vresize_mini_windows)
9589 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
9590 return 0;
9591
9592 if (!FRAME_MINIBUF_ONLY_P (f))
9593 {
9594 struct it it;
9595 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
9596 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
9597 int height, max_height;
9598 int unit = FRAME_LINE_HEIGHT (f);
9599 struct text_pos start;
9600 struct buffer *old_current_buffer = NULL;
9601
9602 if (current_buffer != XBUFFER (w->buffer))
9603 {
9604 old_current_buffer = current_buffer;
9605 set_buffer_internal (XBUFFER (w->buffer));
9606 }
9607
9608 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
9609
9610 /* Compute the max. number of lines specified by the user. */
9611 if (FLOATP (Vmax_mini_window_height))
9612 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
9613 else if (INTEGERP (Vmax_mini_window_height))
9614 max_height = XINT (Vmax_mini_window_height);
9615 else
9616 max_height = total_height / 4;
9617
9618 /* Correct that max. height if it's bogus. */
9619 max_height = max (1, max_height);
9620 max_height = min (total_height, max_height);
9621
9622 /* Find out the height of the text in the window. */
9623 if (it.line_wrap == TRUNCATE)
9624 height = 1;
9625 else
9626 {
9627 last_height = 0;
9628 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
9629 if (it.max_ascent == 0 && it.max_descent == 0)
9630 height = it.current_y + last_height;
9631 else
9632 height = it.current_y + it.max_ascent + it.max_descent;
9633 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
9634 height = (height + unit - 1) / unit;
9635 }
9636
9637 /* Compute a suitable window start. */
9638 if (height > max_height)
9639 {
9640 height = max_height;
9641 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
9642 move_it_vertically_backward (&it, (height - 1) * unit);
9643 start = it.current.pos;
9644 }
9645 else
9646 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
9647 SET_MARKER_FROM_TEXT_POS (w->start, start);
9648
9649 if (EQ (Vresize_mini_windows, Qgrow_only))
9650 {
9651 /* Let it grow only, until we display an empty message, in which
9652 case the window shrinks again. */
9653 if (height > WINDOW_TOTAL_LINES (w))
9654 {
9655 int old_height = WINDOW_TOTAL_LINES (w);
9656 freeze_window_starts (f, 1);
9657 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9658 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9659 }
9660 else if (height < WINDOW_TOTAL_LINES (w)
9661 && (exact_p || BEGV == ZV))
9662 {
9663 int old_height = WINDOW_TOTAL_LINES (w);
9664 freeze_window_starts (f, 0);
9665 shrink_mini_window (w);
9666 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9667 }
9668 }
9669 else
9670 {
9671 /* Always resize to exact size needed. */
9672 if (height > WINDOW_TOTAL_LINES (w))
9673 {
9674 int old_height = WINDOW_TOTAL_LINES (w);
9675 freeze_window_starts (f, 1);
9676 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9677 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9678 }
9679 else if (height < WINDOW_TOTAL_LINES (w))
9680 {
9681 int old_height = WINDOW_TOTAL_LINES (w);
9682 freeze_window_starts (f, 0);
9683 shrink_mini_window (w);
9684
9685 if (height)
9686 {
9687 freeze_window_starts (f, 1);
9688 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
9689 }
9690
9691 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
9692 }
9693 }
9694
9695 if (old_current_buffer)
9696 set_buffer_internal (old_current_buffer);
9697 }
9698
9699 return window_height_changed_p;
9700 }
9701
9702
9703 /* Value is the current message, a string, or nil if there is no
9704 current message. */
9705
9706 Lisp_Object
9707 current_message (void)
9708 {
9709 Lisp_Object msg;
9710
9711 if (!BUFFERP (echo_area_buffer[0]))
9712 msg = Qnil;
9713 else
9714 {
9715 with_echo_area_buffer (0, 0, current_message_1,
9716 (intptr_t) &msg, Qnil, 0, 0);
9717 if (NILP (msg))
9718 echo_area_buffer[0] = Qnil;
9719 }
9720
9721 return msg;
9722 }
9723
9724
9725 static int
9726 current_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9727 {
9728 intptr_t i1 = a1;
9729 Lisp_Object *msg = (Lisp_Object *) i1;
9730
9731 if (Z > BEG)
9732 *msg = make_buffer_string (BEG, Z, 1);
9733 else
9734 *msg = Qnil;
9735 return 0;
9736 }
9737
9738
9739 /* Push the current message on Vmessage_stack for later restauration
9740 by restore_message. Value is non-zero if the current message isn't
9741 empty. This is a relatively infrequent operation, so it's not
9742 worth optimizing. */
9743
9744 int
9745 push_message (void)
9746 {
9747 Lisp_Object msg;
9748 msg = current_message ();
9749 Vmessage_stack = Fcons (msg, Vmessage_stack);
9750 return STRINGP (msg);
9751 }
9752
9753
9754 /* Restore message display from the top of Vmessage_stack. */
9755
9756 void
9757 restore_message (void)
9758 {
9759 Lisp_Object msg;
9760
9761 xassert (CONSP (Vmessage_stack));
9762 msg = XCAR (Vmessage_stack);
9763 if (STRINGP (msg))
9764 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9765 else
9766 message3_nolog (msg, 0, 0);
9767 }
9768
9769
9770 /* Handler for record_unwind_protect calling pop_message. */
9771
9772 Lisp_Object
9773 pop_message_unwind (Lisp_Object dummy)
9774 {
9775 pop_message ();
9776 return Qnil;
9777 }
9778
9779 /* Pop the top-most entry off Vmessage_stack. */
9780
9781 static void
9782 pop_message (void)
9783 {
9784 xassert (CONSP (Vmessage_stack));
9785 Vmessage_stack = XCDR (Vmessage_stack);
9786 }
9787
9788
9789 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
9790 exits. If the stack is not empty, we have a missing pop_message
9791 somewhere. */
9792
9793 void
9794 check_message_stack (void)
9795 {
9796 if (!NILP (Vmessage_stack))
9797 abort ();
9798 }
9799
9800
9801 /* Truncate to NCHARS what will be displayed in the echo area the next
9802 time we display it---but don't redisplay it now. */
9803
9804 void
9805 truncate_echo_area (EMACS_INT nchars)
9806 {
9807 if (nchars == 0)
9808 echo_area_buffer[0] = Qnil;
9809 /* A null message buffer means that the frame hasn't really been
9810 initialized yet. Error messages get reported properly by
9811 cmd_error, so this must be just an informative message; toss it. */
9812 else if (!noninteractive
9813 && INTERACTIVE
9814 && !NILP (echo_area_buffer[0]))
9815 {
9816 struct frame *sf = SELECTED_FRAME ();
9817 if (FRAME_MESSAGE_BUF (sf))
9818 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
9819 }
9820 }
9821
9822
9823 /* Helper function for truncate_echo_area. Truncate the current
9824 message to at most NCHARS characters. */
9825
9826 static int
9827 truncate_message_1 (EMACS_INT nchars, Lisp_Object a2, EMACS_INT a3, EMACS_INT a4)
9828 {
9829 if (BEG + nchars < Z)
9830 del_range (BEG + nchars, Z);
9831 if (Z == BEG)
9832 echo_area_buffer[0] = Qnil;
9833 return 0;
9834 }
9835
9836
9837 /* Set the current message to a substring of S or STRING.
9838
9839 If STRING is a Lisp string, set the message to the first NBYTES
9840 bytes from STRING. NBYTES zero means use the whole string. If
9841 STRING is multibyte, the message will be displayed multibyte.
9842
9843 If S is not null, set the message to the first LEN bytes of S. LEN
9844 zero means use the whole string. MULTIBYTE_P non-zero means S is
9845 multibyte. Display the message multibyte in that case.
9846
9847 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
9848 to t before calling set_message_1 (which calls insert).
9849 */
9850
9851 static void
9852 set_message (const char *s, Lisp_Object string,
9853 EMACS_INT nbytes, int multibyte_p)
9854 {
9855 message_enable_multibyte
9856 = ((s && multibyte_p)
9857 || (STRINGP (string) && STRING_MULTIBYTE (string)));
9858
9859 with_echo_area_buffer (0, -1, set_message_1,
9860 (intptr_t) s, string, nbytes, multibyte_p);
9861 message_buf_print = 0;
9862 help_echo_showing_p = 0;
9863 }
9864
9865
9866 /* Helper function for set_message. Arguments have the same meaning
9867 as there, with A1 corresponding to S and A2 corresponding to STRING
9868 This function is called with the echo area buffer being
9869 current. */
9870
9871 static int
9872 set_message_1 (EMACS_INT a1, Lisp_Object a2, EMACS_INT nbytes, EMACS_INT multibyte_p)
9873 {
9874 intptr_t i1 = a1;
9875 const char *s = (const char *) i1;
9876 const unsigned char *msg = (const unsigned char *) s;
9877 Lisp_Object string = a2;
9878
9879 /* Change multibyteness of the echo buffer appropriately. */
9880 if (message_enable_multibyte
9881 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9882 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
9883
9884 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
9885 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
9886 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
9887
9888 /* Insert new message at BEG. */
9889 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9890
9891 if (STRINGP (string))
9892 {
9893 EMACS_INT nchars;
9894
9895 if (nbytes == 0)
9896 nbytes = SBYTES (string);
9897 nchars = string_byte_to_char (string, nbytes);
9898
9899 /* This function takes care of single/multibyte conversion. We
9900 just have to ensure that the echo area buffer has the right
9901 setting of enable_multibyte_characters. */
9902 insert_from_string (string, 0, 0, nchars, nbytes, 1);
9903 }
9904 else if (s)
9905 {
9906 if (nbytes == 0)
9907 nbytes = strlen (s);
9908
9909 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9910 {
9911 /* Convert from multi-byte to single-byte. */
9912 EMACS_INT i;
9913 int c, n;
9914 char work[1];
9915
9916 /* Convert a multibyte string to single-byte. */
9917 for (i = 0; i < nbytes; i += n)
9918 {
9919 c = string_char_and_length (msg + i, &n);
9920 work[0] = (ASCII_CHAR_P (c)
9921 ? c
9922 : multibyte_char_to_unibyte (c));
9923 insert_1_both (work, 1, 1, 1, 0, 0);
9924 }
9925 }
9926 else if (!multibyte_p
9927 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9928 {
9929 /* Convert from single-byte to multi-byte. */
9930 EMACS_INT i;
9931 int c, n;
9932 unsigned char str[MAX_MULTIBYTE_LENGTH];
9933
9934 /* Convert a single-byte string to multibyte. */
9935 for (i = 0; i < nbytes; i++)
9936 {
9937 c = msg[i];
9938 MAKE_CHAR_MULTIBYTE (c);
9939 n = CHAR_STRING (c, str);
9940 insert_1_both ((char *) str, 1, n, 1, 0, 0);
9941 }
9942 }
9943 else
9944 insert_1 (s, nbytes, 1, 0, 0);
9945 }
9946
9947 return 0;
9948 }
9949
9950
9951 /* Clear messages. CURRENT_P non-zero means clear the current
9952 message. LAST_DISPLAYED_P non-zero means clear the message
9953 last displayed. */
9954
9955 void
9956 clear_message (int current_p, int last_displayed_p)
9957 {
9958 if (current_p)
9959 {
9960 echo_area_buffer[0] = Qnil;
9961 message_cleared_p = 1;
9962 }
9963
9964 if (last_displayed_p)
9965 echo_area_buffer[1] = Qnil;
9966
9967 message_buf_print = 0;
9968 }
9969
9970 /* Clear garbaged frames.
9971
9972 This function is used where the old redisplay called
9973 redraw_garbaged_frames which in turn called redraw_frame which in
9974 turn called clear_frame. The call to clear_frame was a source of
9975 flickering. I believe a clear_frame is not necessary. It should
9976 suffice in the new redisplay to invalidate all current matrices,
9977 and ensure a complete redisplay of all windows. */
9978
9979 static void
9980 clear_garbaged_frames (void)
9981 {
9982 if (frame_garbaged)
9983 {
9984 Lisp_Object tail, frame;
9985 int changed_count = 0;
9986
9987 FOR_EACH_FRAME (tail, frame)
9988 {
9989 struct frame *f = XFRAME (frame);
9990
9991 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
9992 {
9993 if (f->resized_p)
9994 {
9995 Fredraw_frame (frame);
9996 f->force_flush_display_p = 1;
9997 }
9998 clear_current_matrices (f);
9999 changed_count++;
10000 f->garbaged = 0;
10001 f->resized_p = 0;
10002 }
10003 }
10004
10005 frame_garbaged = 0;
10006 if (changed_count)
10007 ++windows_or_buffers_changed;
10008 }
10009 }
10010
10011
10012 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10013 is non-zero update selected_frame. Value is non-zero if the
10014 mini-windows height has been changed. */
10015
10016 static int
10017 echo_area_display (int update_frame_p)
10018 {
10019 Lisp_Object mini_window;
10020 struct window *w;
10021 struct frame *f;
10022 int window_height_changed_p = 0;
10023 struct frame *sf = SELECTED_FRAME ();
10024
10025 mini_window = FRAME_MINIBUF_WINDOW (sf);
10026 w = XWINDOW (mini_window);
10027 f = XFRAME (WINDOW_FRAME (w));
10028
10029 /* Don't display if frame is invisible or not yet initialized. */
10030 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10031 return 0;
10032
10033 #ifdef HAVE_WINDOW_SYSTEM
10034 /* When Emacs starts, selected_frame may be the initial terminal
10035 frame. If we let this through, a message would be displayed on
10036 the terminal. */
10037 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10038 return 0;
10039 #endif /* HAVE_WINDOW_SYSTEM */
10040
10041 /* Redraw garbaged frames. */
10042 if (frame_garbaged)
10043 clear_garbaged_frames ();
10044
10045 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10046 {
10047 echo_area_window = mini_window;
10048 window_height_changed_p = display_echo_area (w);
10049 w->must_be_updated_p = 1;
10050
10051 /* Update the display, unless called from redisplay_internal.
10052 Also don't update the screen during redisplay itself. The
10053 update will happen at the end of redisplay, and an update
10054 here could cause confusion. */
10055 if (update_frame_p && !redisplaying_p)
10056 {
10057 int n = 0;
10058
10059 /* If the display update has been interrupted by pending
10060 input, update mode lines in the frame. Due to the
10061 pending input, it might have been that redisplay hasn't
10062 been called, so that mode lines above the echo area are
10063 garbaged. This looks odd, so we prevent it here. */
10064 if (!display_completed)
10065 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10066
10067 if (window_height_changed_p
10068 /* Don't do this if Emacs is shutting down. Redisplay
10069 needs to run hooks. */
10070 && !NILP (Vrun_hooks))
10071 {
10072 /* Must update other windows. Likewise as in other
10073 cases, don't let this update be interrupted by
10074 pending input. */
10075 int count = SPECPDL_INDEX ();
10076 specbind (Qredisplay_dont_pause, Qt);
10077 windows_or_buffers_changed = 1;
10078 redisplay_internal ();
10079 unbind_to (count, Qnil);
10080 }
10081 else if (FRAME_WINDOW_P (f) && n == 0)
10082 {
10083 /* Window configuration is the same as before.
10084 Can do with a display update of the echo area,
10085 unless we displayed some mode lines. */
10086 update_single_window (w, 1);
10087 FRAME_RIF (f)->flush_display (f);
10088 }
10089 else
10090 update_frame (f, 1, 1);
10091
10092 /* If cursor is in the echo area, make sure that the next
10093 redisplay displays the minibuffer, so that the cursor will
10094 be replaced with what the minibuffer wants. */
10095 if (cursor_in_echo_area)
10096 ++windows_or_buffers_changed;
10097 }
10098 }
10099 else if (!EQ (mini_window, selected_window))
10100 windows_or_buffers_changed++;
10101
10102 /* Last displayed message is now the current message. */
10103 echo_area_buffer[1] = echo_area_buffer[0];
10104 /* Inform read_char that we're not echoing. */
10105 echo_message_buffer = Qnil;
10106
10107 /* Prevent redisplay optimization in redisplay_internal by resetting
10108 this_line_start_pos. This is done because the mini-buffer now
10109 displays the message instead of its buffer text. */
10110 if (EQ (mini_window, selected_window))
10111 CHARPOS (this_line_start_pos) = 0;
10112
10113 return window_height_changed_p;
10114 }
10115
10116
10117 \f
10118 /***********************************************************************
10119 Mode Lines and Frame Titles
10120 ***********************************************************************/
10121
10122 /* A buffer for constructing non-propertized mode-line strings and
10123 frame titles in it; allocated from the heap in init_xdisp and
10124 resized as needed in store_mode_line_noprop_char. */
10125
10126 static char *mode_line_noprop_buf;
10127
10128 /* The buffer's end, and a current output position in it. */
10129
10130 static char *mode_line_noprop_buf_end;
10131 static char *mode_line_noprop_ptr;
10132
10133 #define MODE_LINE_NOPROP_LEN(start) \
10134 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10135
10136 static enum {
10137 MODE_LINE_DISPLAY = 0,
10138 MODE_LINE_TITLE,
10139 MODE_LINE_NOPROP,
10140 MODE_LINE_STRING
10141 } mode_line_target;
10142
10143 /* Alist that caches the results of :propertize.
10144 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10145 static Lisp_Object mode_line_proptrans_alist;
10146
10147 /* List of strings making up the mode-line. */
10148 static Lisp_Object mode_line_string_list;
10149
10150 /* Base face property when building propertized mode line string. */
10151 static Lisp_Object mode_line_string_face;
10152 static Lisp_Object mode_line_string_face_prop;
10153
10154
10155 /* Unwind data for mode line strings */
10156
10157 static Lisp_Object Vmode_line_unwind_vector;
10158
10159 static Lisp_Object
10160 format_mode_line_unwind_data (struct buffer *obuf,
10161 Lisp_Object owin,
10162 int save_proptrans)
10163 {
10164 Lisp_Object vector, tmp;
10165
10166 /* Reduce consing by keeping one vector in
10167 Vwith_echo_area_save_vector. */
10168 vector = Vmode_line_unwind_vector;
10169 Vmode_line_unwind_vector = Qnil;
10170
10171 if (NILP (vector))
10172 vector = Fmake_vector (make_number (8), Qnil);
10173
10174 ASET (vector, 0, make_number (mode_line_target));
10175 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10176 ASET (vector, 2, mode_line_string_list);
10177 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10178 ASET (vector, 4, mode_line_string_face);
10179 ASET (vector, 5, mode_line_string_face_prop);
10180
10181 if (obuf)
10182 XSETBUFFER (tmp, obuf);
10183 else
10184 tmp = Qnil;
10185 ASET (vector, 6, tmp);
10186 ASET (vector, 7, owin);
10187
10188 return vector;
10189 }
10190
10191 static Lisp_Object
10192 unwind_format_mode_line (Lisp_Object vector)
10193 {
10194 mode_line_target = XINT (AREF (vector, 0));
10195 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10196 mode_line_string_list = AREF (vector, 2);
10197 if (! EQ (AREF (vector, 3), Qt))
10198 mode_line_proptrans_alist = AREF (vector, 3);
10199 mode_line_string_face = AREF (vector, 4);
10200 mode_line_string_face_prop = AREF (vector, 5);
10201
10202 if (!NILP (AREF (vector, 7)))
10203 /* Select window before buffer, since it may change the buffer. */
10204 Fselect_window (AREF (vector, 7), Qt);
10205
10206 if (!NILP (AREF (vector, 6)))
10207 {
10208 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10209 ASET (vector, 6, Qnil);
10210 }
10211
10212 Vmode_line_unwind_vector = vector;
10213 return Qnil;
10214 }
10215
10216
10217 /* Store a single character C for the frame title in mode_line_noprop_buf.
10218 Re-allocate mode_line_noprop_buf if necessary. */
10219
10220 static void
10221 store_mode_line_noprop_char (char c)
10222 {
10223 /* If output position has reached the end of the allocated buffer,
10224 double the buffer's size. */
10225 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10226 {
10227 int len = MODE_LINE_NOPROP_LEN (0);
10228 int new_size = 2 * len * sizeof *mode_line_noprop_buf;
10229 mode_line_noprop_buf = (char *) xrealloc (mode_line_noprop_buf, new_size);
10230 mode_line_noprop_buf_end = mode_line_noprop_buf + new_size;
10231 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10232 }
10233
10234 *mode_line_noprop_ptr++ = c;
10235 }
10236
10237
10238 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10239 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10240 characters that yield more columns than PRECISION; PRECISION <= 0
10241 means copy the whole string. Pad with spaces until FIELD_WIDTH
10242 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10243 pad. Called from display_mode_element when it is used to build a
10244 frame title. */
10245
10246 static int
10247 store_mode_line_noprop (const char *string, int field_width, int precision)
10248 {
10249 const unsigned char *str = (const unsigned char *) string;
10250 int n = 0;
10251 EMACS_INT dummy, nbytes;
10252
10253 /* Copy at most PRECISION chars from STR. */
10254 nbytes = strlen (string);
10255 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10256 while (nbytes--)
10257 store_mode_line_noprop_char (*str++);
10258
10259 /* Fill up with spaces until FIELD_WIDTH reached. */
10260 while (field_width > 0
10261 && n < field_width)
10262 {
10263 store_mode_line_noprop_char (' ');
10264 ++n;
10265 }
10266
10267 return n;
10268 }
10269
10270 /***********************************************************************
10271 Frame Titles
10272 ***********************************************************************/
10273
10274 #ifdef HAVE_WINDOW_SYSTEM
10275
10276 /* Set the title of FRAME, if it has changed. The title format is
10277 Vicon_title_format if FRAME is iconified, otherwise it is
10278 frame_title_format. */
10279
10280 static void
10281 x_consider_frame_title (Lisp_Object frame)
10282 {
10283 struct frame *f = XFRAME (frame);
10284
10285 if (FRAME_WINDOW_P (f)
10286 || FRAME_MINIBUF_ONLY_P (f)
10287 || f->explicit_name)
10288 {
10289 /* Do we have more than one visible frame on this X display? */
10290 Lisp_Object tail;
10291 Lisp_Object fmt;
10292 int title_start;
10293 char *title;
10294 int len;
10295 struct it it;
10296 int count = SPECPDL_INDEX ();
10297
10298 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10299 {
10300 Lisp_Object other_frame = XCAR (tail);
10301 struct frame *tf = XFRAME (other_frame);
10302
10303 if (tf != f
10304 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10305 && !FRAME_MINIBUF_ONLY_P (tf)
10306 && !EQ (other_frame, tip_frame)
10307 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10308 break;
10309 }
10310
10311 /* Set global variable indicating that multiple frames exist. */
10312 multiple_frames = CONSP (tail);
10313
10314 /* Switch to the buffer of selected window of the frame. Set up
10315 mode_line_target so that display_mode_element will output into
10316 mode_line_noprop_buf; then display the title. */
10317 record_unwind_protect (unwind_format_mode_line,
10318 format_mode_line_unwind_data
10319 (current_buffer, selected_window, 0));
10320
10321 Fselect_window (f->selected_window, Qt);
10322 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10323 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10324
10325 mode_line_target = MODE_LINE_TITLE;
10326 title_start = MODE_LINE_NOPROP_LEN (0);
10327 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10328 NULL, DEFAULT_FACE_ID);
10329 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10330 len = MODE_LINE_NOPROP_LEN (title_start);
10331 title = mode_line_noprop_buf + title_start;
10332 unbind_to (count, Qnil);
10333
10334 /* Set the title only if it's changed. This avoids consing in
10335 the common case where it hasn't. (If it turns out that we've
10336 already wasted too much time by walking through the list with
10337 display_mode_element, then we might need to optimize at a
10338 higher level than this.) */
10339 if (! STRINGP (f->name)
10340 || SBYTES (f->name) != len
10341 || memcmp (title, SDATA (f->name), len) != 0)
10342 x_implicitly_set_name (f, make_string (title, len), Qnil);
10343 }
10344 }
10345
10346 #endif /* not HAVE_WINDOW_SYSTEM */
10347
10348
10349
10350 \f
10351 /***********************************************************************
10352 Menu Bars
10353 ***********************************************************************/
10354
10355
10356 /* Prepare for redisplay by updating menu-bar item lists when
10357 appropriate. This can call eval. */
10358
10359 void
10360 prepare_menu_bars (void)
10361 {
10362 int all_windows;
10363 struct gcpro gcpro1, gcpro2;
10364 struct frame *f;
10365 Lisp_Object tooltip_frame;
10366
10367 #ifdef HAVE_WINDOW_SYSTEM
10368 tooltip_frame = tip_frame;
10369 #else
10370 tooltip_frame = Qnil;
10371 #endif
10372
10373 /* Update all frame titles based on their buffer names, etc. We do
10374 this before the menu bars so that the buffer-menu will show the
10375 up-to-date frame titles. */
10376 #ifdef HAVE_WINDOW_SYSTEM
10377 if (windows_or_buffers_changed || update_mode_lines)
10378 {
10379 Lisp_Object tail, frame;
10380
10381 FOR_EACH_FRAME (tail, frame)
10382 {
10383 f = XFRAME (frame);
10384 if (!EQ (frame, tooltip_frame)
10385 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10386 x_consider_frame_title (frame);
10387 }
10388 }
10389 #endif /* HAVE_WINDOW_SYSTEM */
10390
10391 /* Update the menu bar item lists, if appropriate. This has to be
10392 done before any actual redisplay or generation of display lines. */
10393 all_windows = (update_mode_lines
10394 || buffer_shared > 1
10395 || windows_or_buffers_changed);
10396 if (all_windows)
10397 {
10398 Lisp_Object tail, frame;
10399 int count = SPECPDL_INDEX ();
10400 /* 1 means that update_menu_bar has run its hooks
10401 so any further calls to update_menu_bar shouldn't do so again. */
10402 int menu_bar_hooks_run = 0;
10403
10404 record_unwind_save_match_data ();
10405
10406 FOR_EACH_FRAME (tail, frame)
10407 {
10408 f = XFRAME (frame);
10409
10410 /* Ignore tooltip frame. */
10411 if (EQ (frame, tooltip_frame))
10412 continue;
10413
10414 /* If a window on this frame changed size, report that to
10415 the user and clear the size-change flag. */
10416 if (FRAME_WINDOW_SIZES_CHANGED (f))
10417 {
10418 Lisp_Object functions;
10419
10420 /* Clear flag first in case we get an error below. */
10421 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10422 functions = Vwindow_size_change_functions;
10423 GCPRO2 (tail, functions);
10424
10425 while (CONSP (functions))
10426 {
10427 if (!EQ (XCAR (functions), Qt))
10428 call1 (XCAR (functions), frame);
10429 functions = XCDR (functions);
10430 }
10431 UNGCPRO;
10432 }
10433
10434 GCPRO1 (tail);
10435 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10436 #ifdef HAVE_WINDOW_SYSTEM
10437 update_tool_bar (f, 0);
10438 #endif
10439 #ifdef HAVE_NS
10440 if (windows_or_buffers_changed
10441 && FRAME_NS_P (f))
10442 ns_set_doc_edited (f, Fbuffer_modified_p
10443 (XWINDOW (f->selected_window)->buffer));
10444 #endif
10445 UNGCPRO;
10446 }
10447
10448 unbind_to (count, Qnil);
10449 }
10450 else
10451 {
10452 struct frame *sf = SELECTED_FRAME ();
10453 update_menu_bar (sf, 1, 0);
10454 #ifdef HAVE_WINDOW_SYSTEM
10455 update_tool_bar (sf, 1);
10456 #endif
10457 }
10458 }
10459
10460
10461 /* Update the menu bar item list for frame F. This has to be done
10462 before we start to fill in any display lines, because it can call
10463 eval.
10464
10465 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
10466
10467 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
10468 already ran the menu bar hooks for this redisplay, so there
10469 is no need to run them again. The return value is the
10470 updated value of this flag, to pass to the next call. */
10471
10472 static int
10473 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
10474 {
10475 Lisp_Object window;
10476 register struct window *w;
10477
10478 /* If called recursively during a menu update, do nothing. This can
10479 happen when, for instance, an activate-menubar-hook causes a
10480 redisplay. */
10481 if (inhibit_menubar_update)
10482 return hooks_run;
10483
10484 window = FRAME_SELECTED_WINDOW (f);
10485 w = XWINDOW (window);
10486
10487 if (FRAME_WINDOW_P (f)
10488 ?
10489 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10490 || defined (HAVE_NS) || defined (USE_GTK)
10491 FRAME_EXTERNAL_MENU_BAR (f)
10492 #else
10493 FRAME_MENU_BAR_LINES (f) > 0
10494 #endif
10495 : FRAME_MENU_BAR_LINES (f) > 0)
10496 {
10497 /* If the user has switched buffers or windows, we need to
10498 recompute to reflect the new bindings. But we'll
10499 recompute when update_mode_lines is set too; that means
10500 that people can use force-mode-line-update to request
10501 that the menu bar be recomputed. The adverse effect on
10502 the rest of the redisplay algorithm is about the same as
10503 windows_or_buffers_changed anyway. */
10504 if (windows_or_buffers_changed
10505 /* This used to test w->update_mode_line, but we believe
10506 there is no need to recompute the menu in that case. */
10507 || update_mode_lines
10508 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10509 < BUF_MODIFF (XBUFFER (w->buffer)))
10510 != !NILP (w->last_had_star))
10511 || ((!NILP (Vtransient_mark_mode)
10512 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10513 != !NILP (w->region_showing)))
10514 {
10515 struct buffer *prev = current_buffer;
10516 int count = SPECPDL_INDEX ();
10517
10518 specbind (Qinhibit_menubar_update, Qt);
10519
10520 set_buffer_internal_1 (XBUFFER (w->buffer));
10521 if (save_match_data)
10522 record_unwind_save_match_data ();
10523 if (NILP (Voverriding_local_map_menu_flag))
10524 {
10525 specbind (Qoverriding_terminal_local_map, Qnil);
10526 specbind (Qoverriding_local_map, Qnil);
10527 }
10528
10529 if (!hooks_run)
10530 {
10531 /* Run the Lucid hook. */
10532 safe_run_hooks (Qactivate_menubar_hook);
10533
10534 /* If it has changed current-menubar from previous value,
10535 really recompute the menu-bar from the value. */
10536 if (! NILP (Vlucid_menu_bar_dirty_flag))
10537 call0 (Qrecompute_lucid_menubar);
10538
10539 safe_run_hooks (Qmenu_bar_update_hook);
10540
10541 hooks_run = 1;
10542 }
10543
10544 XSETFRAME (Vmenu_updating_frame, f);
10545 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
10546
10547 /* Redisplay the menu bar in case we changed it. */
10548 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10549 || defined (HAVE_NS) || defined (USE_GTK)
10550 if (FRAME_WINDOW_P (f))
10551 {
10552 #if defined (HAVE_NS)
10553 /* All frames on Mac OS share the same menubar. So only
10554 the selected frame should be allowed to set it. */
10555 if (f == SELECTED_FRAME ())
10556 #endif
10557 set_frame_menubar (f, 0, 0);
10558 }
10559 else
10560 /* On a terminal screen, the menu bar is an ordinary screen
10561 line, and this makes it get updated. */
10562 w->update_mode_line = Qt;
10563 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10564 /* In the non-toolkit version, the menu bar is an ordinary screen
10565 line, and this makes it get updated. */
10566 w->update_mode_line = Qt;
10567 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
10568
10569 unbind_to (count, Qnil);
10570 set_buffer_internal_1 (prev);
10571 }
10572 }
10573
10574 return hooks_run;
10575 }
10576
10577
10578 \f
10579 /***********************************************************************
10580 Output Cursor
10581 ***********************************************************************/
10582
10583 #ifdef HAVE_WINDOW_SYSTEM
10584
10585 /* EXPORT:
10586 Nominal cursor position -- where to draw output.
10587 HPOS and VPOS are window relative glyph matrix coordinates.
10588 X and Y are window relative pixel coordinates. */
10589
10590 struct cursor_pos output_cursor;
10591
10592
10593 /* EXPORT:
10594 Set the global variable output_cursor to CURSOR. All cursor
10595 positions are relative to updated_window. */
10596
10597 void
10598 set_output_cursor (struct cursor_pos *cursor)
10599 {
10600 output_cursor.hpos = cursor->hpos;
10601 output_cursor.vpos = cursor->vpos;
10602 output_cursor.x = cursor->x;
10603 output_cursor.y = cursor->y;
10604 }
10605
10606
10607 /* EXPORT for RIF:
10608 Set a nominal cursor position.
10609
10610 HPOS and VPOS are column/row positions in a window glyph matrix. X
10611 and Y are window text area relative pixel positions.
10612
10613 If this is done during an update, updated_window will contain the
10614 window that is being updated and the position is the future output
10615 cursor position for that window. If updated_window is null, use
10616 selected_window and display the cursor at the given position. */
10617
10618 void
10619 x_cursor_to (int vpos, int hpos, int y, int x)
10620 {
10621 struct window *w;
10622
10623 /* If updated_window is not set, work on selected_window. */
10624 if (updated_window)
10625 w = updated_window;
10626 else
10627 w = XWINDOW (selected_window);
10628
10629 /* Set the output cursor. */
10630 output_cursor.hpos = hpos;
10631 output_cursor.vpos = vpos;
10632 output_cursor.x = x;
10633 output_cursor.y = y;
10634
10635 /* If not called as part of an update, really display the cursor.
10636 This will also set the cursor position of W. */
10637 if (updated_window == NULL)
10638 {
10639 BLOCK_INPUT;
10640 display_and_set_cursor (w, 1, hpos, vpos, x, y);
10641 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
10642 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
10643 UNBLOCK_INPUT;
10644 }
10645 }
10646
10647 #endif /* HAVE_WINDOW_SYSTEM */
10648
10649 \f
10650 /***********************************************************************
10651 Tool-bars
10652 ***********************************************************************/
10653
10654 #ifdef HAVE_WINDOW_SYSTEM
10655
10656 /* Where the mouse was last time we reported a mouse event. */
10657
10658 FRAME_PTR last_mouse_frame;
10659
10660 /* Tool-bar item index of the item on which a mouse button was pressed
10661 or -1. */
10662
10663 int last_tool_bar_item;
10664
10665
10666 static Lisp_Object
10667 update_tool_bar_unwind (Lisp_Object frame)
10668 {
10669 selected_frame = frame;
10670 return Qnil;
10671 }
10672
10673 /* Update the tool-bar item list for frame F. This has to be done
10674 before we start to fill in any display lines. Called from
10675 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
10676 and restore it here. */
10677
10678 static void
10679 update_tool_bar (struct frame *f, int save_match_data)
10680 {
10681 #if defined (USE_GTK) || defined (HAVE_NS)
10682 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
10683 #else
10684 int do_update = WINDOWP (f->tool_bar_window)
10685 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
10686 #endif
10687
10688 if (do_update)
10689 {
10690 Lisp_Object window;
10691 struct window *w;
10692
10693 window = FRAME_SELECTED_WINDOW (f);
10694 w = XWINDOW (window);
10695
10696 /* If the user has switched buffers or windows, we need to
10697 recompute to reflect the new bindings. But we'll
10698 recompute when update_mode_lines is set too; that means
10699 that people can use force-mode-line-update to request
10700 that the menu bar be recomputed. The adverse effect on
10701 the rest of the redisplay algorithm is about the same as
10702 windows_or_buffers_changed anyway. */
10703 if (windows_or_buffers_changed
10704 || !NILP (w->update_mode_line)
10705 || update_mode_lines
10706 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
10707 < BUF_MODIFF (XBUFFER (w->buffer)))
10708 != !NILP (w->last_had_star))
10709 || ((!NILP (Vtransient_mark_mode)
10710 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
10711 != !NILP (w->region_showing)))
10712 {
10713 struct buffer *prev = current_buffer;
10714 int count = SPECPDL_INDEX ();
10715 Lisp_Object frame, new_tool_bar;
10716 int new_n_tool_bar;
10717 struct gcpro gcpro1;
10718
10719 /* Set current_buffer to the buffer of the selected
10720 window of the frame, so that we get the right local
10721 keymaps. */
10722 set_buffer_internal_1 (XBUFFER (w->buffer));
10723
10724 /* Save match data, if we must. */
10725 if (save_match_data)
10726 record_unwind_save_match_data ();
10727
10728 /* Make sure that we don't accidentally use bogus keymaps. */
10729 if (NILP (Voverriding_local_map_menu_flag))
10730 {
10731 specbind (Qoverriding_terminal_local_map, Qnil);
10732 specbind (Qoverriding_local_map, Qnil);
10733 }
10734
10735 GCPRO1 (new_tool_bar);
10736
10737 /* We must temporarily set the selected frame to this frame
10738 before calling tool_bar_items, because the calculation of
10739 the tool-bar keymap uses the selected frame (see
10740 `tool-bar-make-keymap' in tool-bar.el). */
10741 record_unwind_protect (update_tool_bar_unwind, selected_frame);
10742 XSETFRAME (frame, f);
10743 selected_frame = frame;
10744
10745 /* Build desired tool-bar items from keymaps. */
10746 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
10747 &new_n_tool_bar);
10748
10749 /* Redisplay the tool-bar if we changed it. */
10750 if (new_n_tool_bar != f->n_tool_bar_items
10751 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
10752 {
10753 /* Redisplay that happens asynchronously due to an expose event
10754 may access f->tool_bar_items. Make sure we update both
10755 variables within BLOCK_INPUT so no such event interrupts. */
10756 BLOCK_INPUT;
10757 f->tool_bar_items = new_tool_bar;
10758 f->n_tool_bar_items = new_n_tool_bar;
10759 w->update_mode_line = Qt;
10760 UNBLOCK_INPUT;
10761 }
10762
10763 UNGCPRO;
10764
10765 unbind_to (count, Qnil);
10766 set_buffer_internal_1 (prev);
10767 }
10768 }
10769 }
10770
10771
10772 /* Set F->desired_tool_bar_string to a Lisp string representing frame
10773 F's desired tool-bar contents. F->tool_bar_items must have
10774 been set up previously by calling prepare_menu_bars. */
10775
10776 static void
10777 build_desired_tool_bar_string (struct frame *f)
10778 {
10779 int i, size, size_needed;
10780 struct gcpro gcpro1, gcpro2, gcpro3;
10781 Lisp_Object image, plist, props;
10782
10783 image = plist = props = Qnil;
10784 GCPRO3 (image, plist, props);
10785
10786 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
10787 Otherwise, make a new string. */
10788
10789 /* The size of the string we might be able to reuse. */
10790 size = (STRINGP (f->desired_tool_bar_string)
10791 ? SCHARS (f->desired_tool_bar_string)
10792 : 0);
10793
10794 /* We need one space in the string for each image. */
10795 size_needed = f->n_tool_bar_items;
10796
10797 /* Reuse f->desired_tool_bar_string, if possible. */
10798 if (size < size_needed || NILP (f->desired_tool_bar_string))
10799 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
10800 make_number (' '));
10801 else
10802 {
10803 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
10804 Fremove_text_properties (make_number (0), make_number (size),
10805 props, f->desired_tool_bar_string);
10806 }
10807
10808 /* Put a `display' property on the string for the images to display,
10809 put a `menu_item' property on tool-bar items with a value that
10810 is the index of the item in F's tool-bar item vector. */
10811 for (i = 0; i < f->n_tool_bar_items; ++i)
10812 {
10813 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
10814
10815 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
10816 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
10817 int hmargin, vmargin, relief, idx, end;
10818
10819 /* If image is a vector, choose the image according to the
10820 button state. */
10821 image = PROP (TOOL_BAR_ITEM_IMAGES);
10822 if (VECTORP (image))
10823 {
10824 if (enabled_p)
10825 idx = (selected_p
10826 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
10827 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
10828 else
10829 idx = (selected_p
10830 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
10831 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
10832
10833 xassert (ASIZE (image) >= idx);
10834 image = AREF (image, idx);
10835 }
10836 else
10837 idx = -1;
10838
10839 /* Ignore invalid image specifications. */
10840 if (!valid_image_p (image))
10841 continue;
10842
10843 /* Display the tool-bar button pressed, or depressed. */
10844 plist = Fcopy_sequence (XCDR (image));
10845
10846 /* Compute margin and relief to draw. */
10847 relief = (tool_bar_button_relief >= 0
10848 ? tool_bar_button_relief
10849 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
10850 hmargin = vmargin = relief;
10851
10852 if (INTEGERP (Vtool_bar_button_margin)
10853 && XINT (Vtool_bar_button_margin) > 0)
10854 {
10855 hmargin += XFASTINT (Vtool_bar_button_margin);
10856 vmargin += XFASTINT (Vtool_bar_button_margin);
10857 }
10858 else if (CONSP (Vtool_bar_button_margin))
10859 {
10860 if (INTEGERP (XCAR (Vtool_bar_button_margin))
10861 && XINT (XCAR (Vtool_bar_button_margin)) > 0)
10862 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
10863
10864 if (INTEGERP (XCDR (Vtool_bar_button_margin))
10865 && XINT (XCDR (Vtool_bar_button_margin)) > 0)
10866 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
10867 }
10868
10869 if (auto_raise_tool_bar_buttons_p)
10870 {
10871 /* Add a `:relief' property to the image spec if the item is
10872 selected. */
10873 if (selected_p)
10874 {
10875 plist = Fplist_put (plist, QCrelief, make_number (-relief));
10876 hmargin -= relief;
10877 vmargin -= relief;
10878 }
10879 }
10880 else
10881 {
10882 /* If image is selected, display it pressed, i.e. with a
10883 negative relief. If it's not selected, display it with a
10884 raised relief. */
10885 plist = Fplist_put (plist, QCrelief,
10886 (selected_p
10887 ? make_number (-relief)
10888 : make_number (relief)));
10889 hmargin -= relief;
10890 vmargin -= relief;
10891 }
10892
10893 /* Put a margin around the image. */
10894 if (hmargin || vmargin)
10895 {
10896 if (hmargin == vmargin)
10897 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
10898 else
10899 plist = Fplist_put (plist, QCmargin,
10900 Fcons (make_number (hmargin),
10901 make_number (vmargin)));
10902 }
10903
10904 /* If button is not enabled, and we don't have special images
10905 for the disabled state, make the image appear disabled by
10906 applying an appropriate algorithm to it. */
10907 if (!enabled_p && idx < 0)
10908 plist = Fplist_put (plist, QCconversion, Qdisabled);
10909
10910 /* Put a `display' text property on the string for the image to
10911 display. Put a `menu-item' property on the string that gives
10912 the start of this item's properties in the tool-bar items
10913 vector. */
10914 image = Fcons (Qimage, plist);
10915 props = list4 (Qdisplay, image,
10916 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
10917
10918 /* Let the last image hide all remaining spaces in the tool bar
10919 string. The string can be longer than needed when we reuse a
10920 previous string. */
10921 if (i + 1 == f->n_tool_bar_items)
10922 end = SCHARS (f->desired_tool_bar_string);
10923 else
10924 end = i + 1;
10925 Fadd_text_properties (make_number (i), make_number (end),
10926 props, f->desired_tool_bar_string);
10927 #undef PROP
10928 }
10929
10930 UNGCPRO;
10931 }
10932
10933
10934 /* Display one line of the tool-bar of frame IT->f.
10935
10936 HEIGHT specifies the desired height of the tool-bar line.
10937 If the actual height of the glyph row is less than HEIGHT, the
10938 row's height is increased to HEIGHT, and the icons are centered
10939 vertically in the new height.
10940
10941 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
10942 count a final empty row in case the tool-bar width exactly matches
10943 the window width.
10944 */
10945
10946 static void
10947 display_tool_bar_line (struct it *it, int height)
10948 {
10949 struct glyph_row *row = it->glyph_row;
10950 int max_x = it->last_visible_x;
10951 struct glyph *last;
10952
10953 prepare_desired_row (row);
10954 row->y = it->current_y;
10955
10956 /* Note that this isn't made use of if the face hasn't a box,
10957 so there's no need to check the face here. */
10958 it->start_of_box_run_p = 1;
10959
10960 while (it->current_x < max_x)
10961 {
10962 int x, n_glyphs_before, i, nglyphs;
10963 struct it it_before;
10964
10965 /* Get the next display element. */
10966 if (!get_next_display_element (it))
10967 {
10968 /* Don't count empty row if we are counting needed tool-bar lines. */
10969 if (height < 0 && !it->hpos)
10970 return;
10971 break;
10972 }
10973
10974 /* Produce glyphs. */
10975 n_glyphs_before = row->used[TEXT_AREA];
10976 it_before = *it;
10977
10978 PRODUCE_GLYPHS (it);
10979
10980 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
10981 i = 0;
10982 x = it_before.current_x;
10983 while (i < nglyphs)
10984 {
10985 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
10986
10987 if (x + glyph->pixel_width > max_x)
10988 {
10989 /* Glyph doesn't fit on line. Backtrack. */
10990 row->used[TEXT_AREA] = n_glyphs_before;
10991 *it = it_before;
10992 /* If this is the only glyph on this line, it will never fit on the
10993 tool-bar, so skip it. But ensure there is at least one glyph,
10994 so we don't accidentally disable the tool-bar. */
10995 if (n_glyphs_before == 0
10996 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
10997 break;
10998 goto out;
10999 }
11000
11001 ++it->hpos;
11002 x += glyph->pixel_width;
11003 ++i;
11004 }
11005
11006 /* Stop at line end. */
11007 if (ITERATOR_AT_END_OF_LINE_P (it))
11008 break;
11009
11010 set_iterator_to_next (it, 1);
11011 }
11012
11013 out:;
11014
11015 row->displays_text_p = row->used[TEXT_AREA] != 0;
11016
11017 /* Use default face for the border below the tool bar.
11018
11019 FIXME: When auto-resize-tool-bars is grow-only, there is
11020 no additional border below the possibly empty tool-bar lines.
11021 So to make the extra empty lines look "normal", we have to
11022 use the tool-bar face for the border too. */
11023 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11024 it->face_id = DEFAULT_FACE_ID;
11025
11026 extend_face_to_end_of_line (it);
11027 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11028 last->right_box_line_p = 1;
11029 if (last == row->glyphs[TEXT_AREA])
11030 last->left_box_line_p = 1;
11031
11032 /* Make line the desired height and center it vertically. */
11033 if ((height -= it->max_ascent + it->max_descent) > 0)
11034 {
11035 /* Don't add more than one line height. */
11036 height %= FRAME_LINE_HEIGHT (it->f);
11037 it->max_ascent += height / 2;
11038 it->max_descent += (height + 1) / 2;
11039 }
11040
11041 compute_line_metrics (it);
11042
11043 /* If line is empty, make it occupy the rest of the tool-bar. */
11044 if (!row->displays_text_p)
11045 {
11046 row->height = row->phys_height = it->last_visible_y - row->y;
11047 row->visible_height = row->height;
11048 row->ascent = row->phys_ascent = 0;
11049 row->extra_line_spacing = 0;
11050 }
11051
11052 row->full_width_p = 1;
11053 row->continued_p = 0;
11054 row->truncated_on_left_p = 0;
11055 row->truncated_on_right_p = 0;
11056
11057 it->current_x = it->hpos = 0;
11058 it->current_y += row->height;
11059 ++it->vpos;
11060 ++it->glyph_row;
11061 }
11062
11063
11064 /* Max tool-bar height. */
11065
11066 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11067 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11068
11069 /* Value is the number of screen lines needed to make all tool-bar
11070 items of frame F visible. The number of actual rows needed is
11071 returned in *N_ROWS if non-NULL. */
11072
11073 static int
11074 tool_bar_lines_needed (struct frame *f, int *n_rows)
11075 {
11076 struct window *w = XWINDOW (f->tool_bar_window);
11077 struct it it;
11078 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11079 the desired matrix, so use (unused) mode-line row as temporary row to
11080 avoid destroying the first tool-bar row. */
11081 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11082
11083 /* Initialize an iterator for iteration over
11084 F->desired_tool_bar_string in the tool-bar window of frame F. */
11085 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11086 it.first_visible_x = 0;
11087 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11088 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11089 it.paragraph_embedding = L2R;
11090
11091 while (!ITERATOR_AT_END_P (&it))
11092 {
11093 clear_glyph_row (temp_row);
11094 it.glyph_row = temp_row;
11095 display_tool_bar_line (&it, -1);
11096 }
11097 clear_glyph_row (temp_row);
11098
11099 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11100 if (n_rows)
11101 *n_rows = it.vpos > 0 ? it.vpos : -1;
11102
11103 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11104 }
11105
11106
11107 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11108 0, 1, 0,
11109 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11110 (Lisp_Object frame)
11111 {
11112 struct frame *f;
11113 struct window *w;
11114 int nlines = 0;
11115
11116 if (NILP (frame))
11117 frame = selected_frame;
11118 else
11119 CHECK_FRAME (frame);
11120 f = XFRAME (frame);
11121
11122 if (WINDOWP (f->tool_bar_window)
11123 || (w = XWINDOW (f->tool_bar_window),
11124 WINDOW_TOTAL_LINES (w) > 0))
11125 {
11126 update_tool_bar (f, 1);
11127 if (f->n_tool_bar_items)
11128 {
11129 build_desired_tool_bar_string (f);
11130 nlines = tool_bar_lines_needed (f, NULL);
11131 }
11132 }
11133
11134 return make_number (nlines);
11135 }
11136
11137
11138 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11139 height should be changed. */
11140
11141 static int
11142 redisplay_tool_bar (struct frame *f)
11143 {
11144 struct window *w;
11145 struct it it;
11146 struct glyph_row *row;
11147
11148 #if defined (USE_GTK) || defined (HAVE_NS)
11149 if (FRAME_EXTERNAL_TOOL_BAR (f))
11150 update_frame_tool_bar (f);
11151 return 0;
11152 #endif
11153
11154 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11155 do anything. This means you must start with tool-bar-lines
11156 non-zero to get the auto-sizing effect. Or in other words, you
11157 can turn off tool-bars by specifying tool-bar-lines zero. */
11158 if (!WINDOWP (f->tool_bar_window)
11159 || (w = XWINDOW (f->tool_bar_window),
11160 WINDOW_TOTAL_LINES (w) == 0))
11161 return 0;
11162
11163 /* Set up an iterator for the tool-bar window. */
11164 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11165 it.first_visible_x = 0;
11166 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11167 row = it.glyph_row;
11168
11169 /* Build a string that represents the contents of the tool-bar. */
11170 build_desired_tool_bar_string (f);
11171 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11172 /* FIXME: This should be controlled by a user option. But it
11173 doesn't make sense to have an R2L tool bar if the menu bar cannot
11174 be drawn also R2L, and making the menu bar R2L is tricky due
11175 toolkit-specific code that implements it. If an R2L tool bar is
11176 ever supported, display_tool_bar_line should also be augmented to
11177 call unproduce_glyphs like display_line and display_string
11178 do. */
11179 it.paragraph_embedding = L2R;
11180
11181 if (f->n_tool_bar_rows == 0)
11182 {
11183 int nlines;
11184
11185 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11186 nlines != WINDOW_TOTAL_LINES (w)))
11187 {
11188 Lisp_Object frame;
11189 int old_height = WINDOW_TOTAL_LINES (w);
11190
11191 XSETFRAME (frame, f);
11192 Fmodify_frame_parameters (frame,
11193 Fcons (Fcons (Qtool_bar_lines,
11194 make_number (nlines)),
11195 Qnil));
11196 if (WINDOW_TOTAL_LINES (w) != old_height)
11197 {
11198 clear_glyph_matrix (w->desired_matrix);
11199 fonts_changed_p = 1;
11200 return 1;
11201 }
11202 }
11203 }
11204
11205 /* Display as many lines as needed to display all tool-bar items. */
11206
11207 if (f->n_tool_bar_rows > 0)
11208 {
11209 int border, rows, height, extra;
11210
11211 if (INTEGERP (Vtool_bar_border))
11212 border = XINT (Vtool_bar_border);
11213 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11214 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11215 else if (EQ (Vtool_bar_border, Qborder_width))
11216 border = f->border_width;
11217 else
11218 border = 0;
11219 if (border < 0)
11220 border = 0;
11221
11222 rows = f->n_tool_bar_rows;
11223 height = max (1, (it.last_visible_y - border) / rows);
11224 extra = it.last_visible_y - border - height * rows;
11225
11226 while (it.current_y < it.last_visible_y)
11227 {
11228 int h = 0;
11229 if (extra > 0 && rows-- > 0)
11230 {
11231 h = (extra + rows - 1) / rows;
11232 extra -= h;
11233 }
11234 display_tool_bar_line (&it, height + h);
11235 }
11236 }
11237 else
11238 {
11239 while (it.current_y < it.last_visible_y)
11240 display_tool_bar_line (&it, 0);
11241 }
11242
11243 /* It doesn't make much sense to try scrolling in the tool-bar
11244 window, so don't do it. */
11245 w->desired_matrix->no_scrolling_p = 1;
11246 w->must_be_updated_p = 1;
11247
11248 if (!NILP (Vauto_resize_tool_bars))
11249 {
11250 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11251 int change_height_p = 0;
11252
11253 /* If we couldn't display everything, change the tool-bar's
11254 height if there is room for more. */
11255 if (IT_STRING_CHARPOS (it) < it.end_charpos
11256 && it.current_y < max_tool_bar_height)
11257 change_height_p = 1;
11258
11259 row = it.glyph_row - 1;
11260
11261 /* If there are blank lines at the end, except for a partially
11262 visible blank line at the end that is smaller than
11263 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11264 if (!row->displays_text_p
11265 && row->height >= FRAME_LINE_HEIGHT (f))
11266 change_height_p = 1;
11267
11268 /* If row displays tool-bar items, but is partially visible,
11269 change the tool-bar's height. */
11270 if (row->displays_text_p
11271 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11272 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11273 change_height_p = 1;
11274
11275 /* Resize windows as needed by changing the `tool-bar-lines'
11276 frame parameter. */
11277 if (change_height_p)
11278 {
11279 Lisp_Object frame;
11280 int old_height = WINDOW_TOTAL_LINES (w);
11281 int nrows;
11282 int nlines = tool_bar_lines_needed (f, &nrows);
11283
11284 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11285 && !f->minimize_tool_bar_window_p)
11286 ? (nlines > old_height)
11287 : (nlines != old_height));
11288 f->minimize_tool_bar_window_p = 0;
11289
11290 if (change_height_p)
11291 {
11292 XSETFRAME (frame, f);
11293 Fmodify_frame_parameters (frame,
11294 Fcons (Fcons (Qtool_bar_lines,
11295 make_number (nlines)),
11296 Qnil));
11297 if (WINDOW_TOTAL_LINES (w) != old_height)
11298 {
11299 clear_glyph_matrix (w->desired_matrix);
11300 f->n_tool_bar_rows = nrows;
11301 fonts_changed_p = 1;
11302 return 1;
11303 }
11304 }
11305 }
11306 }
11307
11308 f->minimize_tool_bar_window_p = 0;
11309 return 0;
11310 }
11311
11312
11313 /* Get information about the tool-bar item which is displayed in GLYPH
11314 on frame F. Return in *PROP_IDX the index where tool-bar item
11315 properties start in F->tool_bar_items. Value is zero if
11316 GLYPH doesn't display a tool-bar item. */
11317
11318 static int
11319 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11320 {
11321 Lisp_Object prop;
11322 int success_p;
11323 int charpos;
11324
11325 /* This function can be called asynchronously, which means we must
11326 exclude any possibility that Fget_text_property signals an
11327 error. */
11328 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11329 charpos = max (0, charpos);
11330
11331 /* Get the text property `menu-item' at pos. The value of that
11332 property is the start index of this item's properties in
11333 F->tool_bar_items. */
11334 prop = Fget_text_property (make_number (charpos),
11335 Qmenu_item, f->current_tool_bar_string);
11336 if (INTEGERP (prop))
11337 {
11338 *prop_idx = XINT (prop);
11339 success_p = 1;
11340 }
11341 else
11342 success_p = 0;
11343
11344 return success_p;
11345 }
11346
11347 \f
11348 /* Get information about the tool-bar item at position X/Y on frame F.
11349 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11350 the current matrix of the tool-bar window of F, or NULL if not
11351 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11352 item in F->tool_bar_items. Value is
11353
11354 -1 if X/Y is not on a tool-bar item
11355 0 if X/Y is on the same item that was highlighted before.
11356 1 otherwise. */
11357
11358 static int
11359 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11360 int *hpos, int *vpos, int *prop_idx)
11361 {
11362 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11363 struct window *w = XWINDOW (f->tool_bar_window);
11364 int area;
11365
11366 /* Find the glyph under X/Y. */
11367 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11368 if (*glyph == NULL)
11369 return -1;
11370
11371 /* Get the start of this tool-bar item's properties in
11372 f->tool_bar_items. */
11373 if (!tool_bar_item_info (f, *glyph, prop_idx))
11374 return -1;
11375
11376 /* Is mouse on the highlighted item? */
11377 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11378 && *vpos >= hlinfo->mouse_face_beg_row
11379 && *vpos <= hlinfo->mouse_face_end_row
11380 && (*vpos > hlinfo->mouse_face_beg_row
11381 || *hpos >= hlinfo->mouse_face_beg_col)
11382 && (*vpos < hlinfo->mouse_face_end_row
11383 || *hpos < hlinfo->mouse_face_end_col
11384 || hlinfo->mouse_face_past_end))
11385 return 0;
11386
11387 return 1;
11388 }
11389
11390
11391 /* EXPORT:
11392 Handle mouse button event on the tool-bar of frame F, at
11393 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11394 0 for button release. MODIFIERS is event modifiers for button
11395 release. */
11396
11397 void
11398 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11399 unsigned int modifiers)
11400 {
11401 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11402 struct window *w = XWINDOW (f->tool_bar_window);
11403 int hpos, vpos, prop_idx;
11404 struct glyph *glyph;
11405 Lisp_Object enabled_p;
11406
11407 /* If not on the highlighted tool-bar item, return. */
11408 frame_to_window_pixel_xy (w, &x, &y);
11409 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11410 return;
11411
11412 /* If item is disabled, do nothing. */
11413 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11414 if (NILP (enabled_p))
11415 return;
11416
11417 if (down_p)
11418 {
11419 /* Show item in pressed state. */
11420 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11421 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11422 last_tool_bar_item = prop_idx;
11423 }
11424 else
11425 {
11426 Lisp_Object key, frame;
11427 struct input_event event;
11428 EVENT_INIT (event);
11429
11430 /* Show item in released state. */
11431 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11432 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11433
11434 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11435
11436 XSETFRAME (frame, f);
11437 event.kind = TOOL_BAR_EVENT;
11438 event.frame_or_window = frame;
11439 event.arg = frame;
11440 kbd_buffer_store_event (&event);
11441
11442 event.kind = TOOL_BAR_EVENT;
11443 event.frame_or_window = frame;
11444 event.arg = key;
11445 event.modifiers = modifiers;
11446 kbd_buffer_store_event (&event);
11447 last_tool_bar_item = -1;
11448 }
11449 }
11450
11451
11452 /* Possibly highlight a tool-bar item on frame F when mouse moves to
11453 tool-bar window-relative coordinates X/Y. Called from
11454 note_mouse_highlight. */
11455
11456 static void
11457 note_tool_bar_highlight (struct frame *f, int x, int y)
11458 {
11459 Lisp_Object window = f->tool_bar_window;
11460 struct window *w = XWINDOW (window);
11461 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11462 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11463 int hpos, vpos;
11464 struct glyph *glyph;
11465 struct glyph_row *row;
11466 int i;
11467 Lisp_Object enabled_p;
11468 int prop_idx;
11469 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
11470 int mouse_down_p, rc;
11471
11472 /* Function note_mouse_highlight is called with negative X/Y
11473 values when mouse moves outside of the frame. */
11474 if (x <= 0 || y <= 0)
11475 {
11476 clear_mouse_face (hlinfo);
11477 return;
11478 }
11479
11480 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
11481 if (rc < 0)
11482 {
11483 /* Not on tool-bar item. */
11484 clear_mouse_face (hlinfo);
11485 return;
11486 }
11487 else if (rc == 0)
11488 /* On same tool-bar item as before. */
11489 goto set_help_echo;
11490
11491 clear_mouse_face (hlinfo);
11492
11493 /* Mouse is down, but on different tool-bar item? */
11494 mouse_down_p = (dpyinfo->grabbed
11495 && f == last_mouse_frame
11496 && FRAME_LIVE_P (f));
11497 if (mouse_down_p
11498 && last_tool_bar_item != prop_idx)
11499 return;
11500
11501 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
11502 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
11503
11504 /* If tool-bar item is not enabled, don't highlight it. */
11505 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11506 if (!NILP (enabled_p))
11507 {
11508 /* Compute the x-position of the glyph. In front and past the
11509 image is a space. We include this in the highlighted area. */
11510 row = MATRIX_ROW (w->current_matrix, vpos);
11511 for (i = x = 0; i < hpos; ++i)
11512 x += row->glyphs[TEXT_AREA][i].pixel_width;
11513
11514 /* Record this as the current active region. */
11515 hlinfo->mouse_face_beg_col = hpos;
11516 hlinfo->mouse_face_beg_row = vpos;
11517 hlinfo->mouse_face_beg_x = x;
11518 hlinfo->mouse_face_beg_y = row->y;
11519 hlinfo->mouse_face_past_end = 0;
11520
11521 hlinfo->mouse_face_end_col = hpos + 1;
11522 hlinfo->mouse_face_end_row = vpos;
11523 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
11524 hlinfo->mouse_face_end_y = row->y;
11525 hlinfo->mouse_face_window = window;
11526 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
11527
11528 /* Display it as active. */
11529 show_mouse_face (hlinfo, draw);
11530 hlinfo->mouse_face_image_state = draw;
11531 }
11532
11533 set_help_echo:
11534
11535 /* Set help_echo_string to a help string to display for this tool-bar item.
11536 XTread_socket does the rest. */
11537 help_echo_object = help_echo_window = Qnil;
11538 help_echo_pos = -1;
11539 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
11540 if (NILP (help_echo_string))
11541 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
11542 }
11543
11544 #endif /* HAVE_WINDOW_SYSTEM */
11545
11546
11547 \f
11548 /************************************************************************
11549 Horizontal scrolling
11550 ************************************************************************/
11551
11552 static int hscroll_window_tree (Lisp_Object);
11553 static int hscroll_windows (Lisp_Object);
11554
11555 /* For all leaf windows in the window tree rooted at WINDOW, set their
11556 hscroll value so that PT is (i) visible in the window, and (ii) so
11557 that it is not within a certain margin at the window's left and
11558 right border. Value is non-zero if any window's hscroll has been
11559 changed. */
11560
11561 static int
11562 hscroll_window_tree (Lisp_Object window)
11563 {
11564 int hscrolled_p = 0;
11565 int hscroll_relative_p = FLOATP (Vhscroll_step);
11566 int hscroll_step_abs = 0;
11567 double hscroll_step_rel = 0;
11568
11569 if (hscroll_relative_p)
11570 {
11571 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
11572 if (hscroll_step_rel < 0)
11573 {
11574 hscroll_relative_p = 0;
11575 hscroll_step_abs = 0;
11576 }
11577 }
11578 else if (INTEGERP (Vhscroll_step))
11579 {
11580 hscroll_step_abs = XINT (Vhscroll_step);
11581 if (hscroll_step_abs < 0)
11582 hscroll_step_abs = 0;
11583 }
11584 else
11585 hscroll_step_abs = 0;
11586
11587 while (WINDOWP (window))
11588 {
11589 struct window *w = XWINDOW (window);
11590
11591 if (WINDOWP (w->hchild))
11592 hscrolled_p |= hscroll_window_tree (w->hchild);
11593 else if (WINDOWP (w->vchild))
11594 hscrolled_p |= hscroll_window_tree (w->vchild);
11595 else if (w->cursor.vpos >= 0)
11596 {
11597 int h_margin;
11598 int text_area_width;
11599 struct glyph_row *current_cursor_row
11600 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
11601 struct glyph_row *desired_cursor_row
11602 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
11603 struct glyph_row *cursor_row
11604 = (desired_cursor_row->enabled_p
11605 ? desired_cursor_row
11606 : current_cursor_row);
11607
11608 text_area_width = window_box_width (w, TEXT_AREA);
11609
11610 /* Scroll when cursor is inside this scroll margin. */
11611 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
11612
11613 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
11614 && ((XFASTINT (w->hscroll)
11615 && w->cursor.x <= h_margin)
11616 || (cursor_row->enabled_p
11617 && cursor_row->truncated_on_right_p
11618 && (w->cursor.x >= text_area_width - h_margin))))
11619 {
11620 struct it it;
11621 int hscroll;
11622 struct buffer *saved_current_buffer;
11623 EMACS_INT pt;
11624 int wanted_x;
11625
11626 /* Find point in a display of infinite width. */
11627 saved_current_buffer = current_buffer;
11628 current_buffer = XBUFFER (w->buffer);
11629
11630 if (w == XWINDOW (selected_window))
11631 pt = PT;
11632 else
11633 {
11634 pt = marker_position (w->pointm);
11635 pt = max (BEGV, pt);
11636 pt = min (ZV, pt);
11637 }
11638
11639 /* Move iterator to pt starting at cursor_row->start in
11640 a line with infinite width. */
11641 init_to_row_start (&it, w, cursor_row);
11642 it.last_visible_x = INFINITY;
11643 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
11644 current_buffer = saved_current_buffer;
11645
11646 /* Position cursor in window. */
11647 if (!hscroll_relative_p && hscroll_step_abs == 0)
11648 hscroll = max (0, (it.current_x
11649 - (ITERATOR_AT_END_OF_LINE_P (&it)
11650 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
11651 : (text_area_width / 2))))
11652 / FRAME_COLUMN_WIDTH (it.f);
11653 else if (w->cursor.x >= text_area_width - h_margin)
11654 {
11655 if (hscroll_relative_p)
11656 wanted_x = text_area_width * (1 - hscroll_step_rel)
11657 - h_margin;
11658 else
11659 wanted_x = text_area_width
11660 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11661 - h_margin;
11662 hscroll
11663 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11664 }
11665 else
11666 {
11667 if (hscroll_relative_p)
11668 wanted_x = text_area_width * hscroll_step_rel
11669 + h_margin;
11670 else
11671 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
11672 + h_margin;
11673 hscroll
11674 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
11675 }
11676 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
11677
11678 /* Don't call Fset_window_hscroll if value hasn't
11679 changed because it will prevent redisplay
11680 optimizations. */
11681 if (XFASTINT (w->hscroll) != hscroll)
11682 {
11683 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
11684 w->hscroll = make_number (hscroll);
11685 hscrolled_p = 1;
11686 }
11687 }
11688 }
11689
11690 window = w->next;
11691 }
11692
11693 /* Value is non-zero if hscroll of any leaf window has been changed. */
11694 return hscrolled_p;
11695 }
11696
11697
11698 /* Set hscroll so that cursor is visible and not inside horizontal
11699 scroll margins for all windows in the tree rooted at WINDOW. See
11700 also hscroll_window_tree above. Value is non-zero if any window's
11701 hscroll has been changed. If it has, desired matrices on the frame
11702 of WINDOW are cleared. */
11703
11704 static int
11705 hscroll_windows (Lisp_Object window)
11706 {
11707 int hscrolled_p = hscroll_window_tree (window);
11708 if (hscrolled_p)
11709 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
11710 return hscrolled_p;
11711 }
11712
11713
11714 \f
11715 /************************************************************************
11716 Redisplay
11717 ************************************************************************/
11718
11719 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
11720 to a non-zero value. This is sometimes handy to have in a debugger
11721 session. */
11722
11723 #if GLYPH_DEBUG
11724
11725 /* First and last unchanged row for try_window_id. */
11726
11727 int debug_first_unchanged_at_end_vpos;
11728 int debug_last_unchanged_at_beg_vpos;
11729
11730 /* Delta vpos and y. */
11731
11732 int debug_dvpos, debug_dy;
11733
11734 /* Delta in characters and bytes for try_window_id. */
11735
11736 EMACS_INT debug_delta, debug_delta_bytes;
11737
11738 /* Values of window_end_pos and window_end_vpos at the end of
11739 try_window_id. */
11740
11741 EMACS_INT debug_end_vpos;
11742
11743 /* Append a string to W->desired_matrix->method. FMT is a printf
11744 format string. A1...A9 are a supplement for a variable-length
11745 argument list. If trace_redisplay_p is non-zero also printf the
11746 resulting string to stderr. */
11747
11748 static void
11749 debug_method_add (w, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9)
11750 struct window *w;
11751 char *fmt;
11752 int a1, a2, a3, a4, a5, a6, a7, a8, a9;
11753 {
11754 char buffer[512];
11755 char *method = w->desired_matrix->method;
11756 int len = strlen (method);
11757 int size = sizeof w->desired_matrix->method;
11758 int remaining = size - len - 1;
11759
11760 sprintf (buffer, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9);
11761 if (len && remaining)
11762 {
11763 method[len] = '|';
11764 --remaining, ++len;
11765 }
11766
11767 strncpy (method + len, buffer, remaining);
11768
11769 if (trace_redisplay_p)
11770 fprintf (stderr, "%p (%s): %s\n",
11771 w,
11772 ((BUFFERP (w->buffer)
11773 && STRINGP (XBUFFER (w->buffer)->name))
11774 ? SSDATA (XBUFFER (w->buffer)->name)
11775 : "no buffer"),
11776 buffer);
11777 }
11778
11779 #endif /* GLYPH_DEBUG */
11780
11781
11782 /* Value is non-zero if all changes in window W, which displays
11783 current_buffer, are in the text between START and END. START is a
11784 buffer position, END is given as a distance from Z. Used in
11785 redisplay_internal for display optimization. */
11786
11787 static INLINE int
11788 text_outside_line_unchanged_p (struct window *w,
11789 EMACS_INT start, EMACS_INT end)
11790 {
11791 int unchanged_p = 1;
11792
11793 /* If text or overlays have changed, see where. */
11794 if (XFASTINT (w->last_modified) < MODIFF
11795 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
11796 {
11797 /* Gap in the line? */
11798 if (GPT < start || Z - GPT < end)
11799 unchanged_p = 0;
11800
11801 /* Changes start in front of the line, or end after it? */
11802 if (unchanged_p
11803 && (BEG_UNCHANGED < start - 1
11804 || END_UNCHANGED < end))
11805 unchanged_p = 0;
11806
11807 /* If selective display, can't optimize if changes start at the
11808 beginning of the line. */
11809 if (unchanged_p
11810 && INTEGERP (BVAR (current_buffer, selective_display))
11811 && XINT (BVAR (current_buffer, selective_display)) > 0
11812 && (BEG_UNCHANGED < start || GPT <= start))
11813 unchanged_p = 0;
11814
11815 /* If there are overlays at the start or end of the line, these
11816 may have overlay strings with newlines in them. A change at
11817 START, for instance, may actually concern the display of such
11818 overlay strings as well, and they are displayed on different
11819 lines. So, quickly rule out this case. (For the future, it
11820 might be desirable to implement something more telling than
11821 just BEG/END_UNCHANGED.) */
11822 if (unchanged_p)
11823 {
11824 if (BEG + BEG_UNCHANGED == start
11825 && overlay_touches_p (start))
11826 unchanged_p = 0;
11827 if (END_UNCHANGED == end
11828 && overlay_touches_p (Z - end))
11829 unchanged_p = 0;
11830 }
11831
11832 /* Under bidi reordering, adding or deleting a character in the
11833 beginning of a paragraph, before the first strong directional
11834 character, can change the base direction of the paragraph (unless
11835 the buffer specifies a fixed paragraph direction), which will
11836 require to redisplay the whole paragraph. It might be worthwhile
11837 to find the paragraph limits and widen the range of redisplayed
11838 lines to that, but for now just give up this optimization. */
11839 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
11840 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
11841 unchanged_p = 0;
11842 }
11843
11844 return unchanged_p;
11845 }
11846
11847
11848 /* Do a frame update, taking possible shortcuts into account. This is
11849 the main external entry point for redisplay.
11850
11851 If the last redisplay displayed an echo area message and that message
11852 is no longer requested, we clear the echo area or bring back the
11853 mini-buffer if that is in use. */
11854
11855 void
11856 redisplay (void)
11857 {
11858 redisplay_internal ();
11859 }
11860
11861
11862 static Lisp_Object
11863 overlay_arrow_string_or_property (Lisp_Object var)
11864 {
11865 Lisp_Object val;
11866
11867 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
11868 return val;
11869
11870 return Voverlay_arrow_string;
11871 }
11872
11873 /* Return 1 if there are any overlay-arrows in current_buffer. */
11874 static int
11875 overlay_arrow_in_current_buffer_p (void)
11876 {
11877 Lisp_Object vlist;
11878
11879 for (vlist = Voverlay_arrow_variable_list;
11880 CONSP (vlist);
11881 vlist = XCDR (vlist))
11882 {
11883 Lisp_Object var = XCAR (vlist);
11884 Lisp_Object val;
11885
11886 if (!SYMBOLP (var))
11887 continue;
11888 val = find_symbol_value (var);
11889 if (MARKERP (val)
11890 && current_buffer == XMARKER (val)->buffer)
11891 return 1;
11892 }
11893 return 0;
11894 }
11895
11896
11897 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
11898 has changed. */
11899
11900 static int
11901 overlay_arrows_changed_p (void)
11902 {
11903 Lisp_Object vlist;
11904
11905 for (vlist = Voverlay_arrow_variable_list;
11906 CONSP (vlist);
11907 vlist = XCDR (vlist))
11908 {
11909 Lisp_Object var = XCAR (vlist);
11910 Lisp_Object val, pstr;
11911
11912 if (!SYMBOLP (var))
11913 continue;
11914 val = find_symbol_value (var);
11915 if (!MARKERP (val))
11916 continue;
11917 if (! EQ (COERCE_MARKER (val),
11918 Fget (var, Qlast_arrow_position))
11919 || ! (pstr = overlay_arrow_string_or_property (var),
11920 EQ (pstr, Fget (var, Qlast_arrow_string))))
11921 return 1;
11922 }
11923 return 0;
11924 }
11925
11926 /* Mark overlay arrows to be updated on next redisplay. */
11927
11928 static void
11929 update_overlay_arrows (int up_to_date)
11930 {
11931 Lisp_Object vlist;
11932
11933 for (vlist = Voverlay_arrow_variable_list;
11934 CONSP (vlist);
11935 vlist = XCDR (vlist))
11936 {
11937 Lisp_Object var = XCAR (vlist);
11938
11939 if (!SYMBOLP (var))
11940 continue;
11941
11942 if (up_to_date > 0)
11943 {
11944 Lisp_Object val = find_symbol_value (var);
11945 Fput (var, Qlast_arrow_position,
11946 COERCE_MARKER (val));
11947 Fput (var, Qlast_arrow_string,
11948 overlay_arrow_string_or_property (var));
11949 }
11950 else if (up_to_date < 0
11951 || !NILP (Fget (var, Qlast_arrow_position)))
11952 {
11953 Fput (var, Qlast_arrow_position, Qt);
11954 Fput (var, Qlast_arrow_string, Qt);
11955 }
11956 }
11957 }
11958
11959
11960 /* Return overlay arrow string to display at row.
11961 Return integer (bitmap number) for arrow bitmap in left fringe.
11962 Return nil if no overlay arrow. */
11963
11964 static Lisp_Object
11965 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
11966 {
11967 Lisp_Object vlist;
11968
11969 for (vlist = Voverlay_arrow_variable_list;
11970 CONSP (vlist);
11971 vlist = XCDR (vlist))
11972 {
11973 Lisp_Object var = XCAR (vlist);
11974 Lisp_Object val;
11975
11976 if (!SYMBOLP (var))
11977 continue;
11978
11979 val = find_symbol_value (var);
11980
11981 if (MARKERP (val)
11982 && current_buffer == XMARKER (val)->buffer
11983 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
11984 {
11985 if (FRAME_WINDOW_P (it->f)
11986 /* FIXME: if ROW->reversed_p is set, this should test
11987 the right fringe, not the left one. */
11988 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
11989 {
11990 #ifdef HAVE_WINDOW_SYSTEM
11991 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
11992 {
11993 int fringe_bitmap;
11994 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
11995 return make_number (fringe_bitmap);
11996 }
11997 #endif
11998 return make_number (-1); /* Use default arrow bitmap */
11999 }
12000 return overlay_arrow_string_or_property (var);
12001 }
12002 }
12003
12004 return Qnil;
12005 }
12006
12007 /* Return 1 if point moved out of or into a composition. Otherwise
12008 return 0. PREV_BUF and PREV_PT are the last point buffer and
12009 position. BUF and PT are the current point buffer and position. */
12010
12011 static int
12012 check_point_in_composition (struct buffer *prev_buf, EMACS_INT prev_pt,
12013 struct buffer *buf, EMACS_INT pt)
12014 {
12015 EMACS_INT start, end;
12016 Lisp_Object prop;
12017 Lisp_Object buffer;
12018
12019 XSETBUFFER (buffer, buf);
12020 /* Check a composition at the last point if point moved within the
12021 same buffer. */
12022 if (prev_buf == buf)
12023 {
12024 if (prev_pt == pt)
12025 /* Point didn't move. */
12026 return 0;
12027
12028 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12029 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12030 && COMPOSITION_VALID_P (start, end, prop)
12031 && start < prev_pt && end > prev_pt)
12032 /* The last point was within the composition. Return 1 iff
12033 point moved out of the composition. */
12034 return (pt <= start || pt >= end);
12035 }
12036
12037 /* Check a composition at the current point. */
12038 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12039 && find_composition (pt, -1, &start, &end, &prop, buffer)
12040 && COMPOSITION_VALID_P (start, end, prop)
12041 && start < pt && end > pt);
12042 }
12043
12044
12045 /* Reconsider the setting of B->clip_changed which is displayed
12046 in window W. */
12047
12048 static INLINE void
12049 reconsider_clip_changes (struct window *w, struct buffer *b)
12050 {
12051 if (b->clip_changed
12052 && !NILP (w->window_end_valid)
12053 && w->current_matrix->buffer == b
12054 && w->current_matrix->zv == BUF_ZV (b)
12055 && w->current_matrix->begv == BUF_BEGV (b))
12056 b->clip_changed = 0;
12057
12058 /* If display wasn't paused, and W is not a tool bar window, see if
12059 point has been moved into or out of a composition. In that case,
12060 we set b->clip_changed to 1 to force updating the screen. If
12061 b->clip_changed has already been set to 1, we can skip this
12062 check. */
12063 if (!b->clip_changed
12064 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12065 {
12066 EMACS_INT pt;
12067
12068 if (w == XWINDOW (selected_window))
12069 pt = PT;
12070 else
12071 pt = marker_position (w->pointm);
12072
12073 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12074 || pt != XINT (w->last_point))
12075 && check_point_in_composition (w->current_matrix->buffer,
12076 XINT (w->last_point),
12077 XBUFFER (w->buffer), pt))
12078 b->clip_changed = 1;
12079 }
12080 }
12081 \f
12082
12083 /* Select FRAME to forward the values of frame-local variables into C
12084 variables so that the redisplay routines can access those values
12085 directly. */
12086
12087 static void
12088 select_frame_for_redisplay (Lisp_Object frame)
12089 {
12090 Lisp_Object tail, tem;
12091 Lisp_Object old = selected_frame;
12092 struct Lisp_Symbol *sym;
12093
12094 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12095
12096 selected_frame = frame;
12097
12098 do {
12099 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12100 if (CONSP (XCAR (tail))
12101 && (tem = XCAR (XCAR (tail)),
12102 SYMBOLP (tem))
12103 && (sym = indirect_variable (XSYMBOL (tem)),
12104 sym->redirect == SYMBOL_LOCALIZED)
12105 && sym->val.blv->frame_local)
12106 /* Use find_symbol_value rather than Fsymbol_value
12107 to avoid an error if it is void. */
12108 find_symbol_value (tem);
12109 } while (!EQ (frame, old) && (frame = old, 1));
12110 }
12111
12112
12113 #define STOP_POLLING \
12114 do { if (! polling_stopped_here) stop_polling (); \
12115 polling_stopped_here = 1; } while (0)
12116
12117 #define RESUME_POLLING \
12118 do { if (polling_stopped_here) start_polling (); \
12119 polling_stopped_here = 0; } while (0)
12120
12121
12122 /* Perhaps in the future avoid recentering windows if it
12123 is not necessary; currently that causes some problems. */
12124
12125 static void
12126 redisplay_internal (void)
12127 {
12128 struct window *w = XWINDOW (selected_window);
12129 struct window *sw;
12130 struct frame *fr;
12131 int pending;
12132 int must_finish = 0;
12133 struct text_pos tlbufpos, tlendpos;
12134 int number_of_visible_frames;
12135 int count, count1;
12136 struct frame *sf;
12137 int polling_stopped_here = 0;
12138 Lisp_Object old_frame = selected_frame;
12139
12140 /* Non-zero means redisplay has to consider all windows on all
12141 frames. Zero means, only selected_window is considered. */
12142 int consider_all_windows_p;
12143
12144 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12145
12146 /* No redisplay if running in batch mode or frame is not yet fully
12147 initialized, or redisplay is explicitly turned off by setting
12148 Vinhibit_redisplay. */
12149 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12150 || !NILP (Vinhibit_redisplay))
12151 return;
12152
12153 /* Don't examine these until after testing Vinhibit_redisplay.
12154 When Emacs is shutting down, perhaps because its connection to
12155 X has dropped, we should not look at them at all. */
12156 fr = XFRAME (w->frame);
12157 sf = SELECTED_FRAME ();
12158
12159 if (!fr->glyphs_initialized_p)
12160 return;
12161
12162 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12163 if (popup_activated ())
12164 return;
12165 #endif
12166
12167 /* I don't think this happens but let's be paranoid. */
12168 if (redisplaying_p)
12169 return;
12170
12171 /* Record a function that resets redisplaying_p to its old value
12172 when we leave this function. */
12173 count = SPECPDL_INDEX ();
12174 record_unwind_protect (unwind_redisplay,
12175 Fcons (make_number (redisplaying_p), selected_frame));
12176 ++redisplaying_p;
12177 specbind (Qinhibit_free_realized_faces, Qnil);
12178
12179 {
12180 Lisp_Object tail, frame;
12181
12182 FOR_EACH_FRAME (tail, frame)
12183 {
12184 struct frame *f = XFRAME (frame);
12185 f->already_hscrolled_p = 0;
12186 }
12187 }
12188
12189 retry:
12190 /* Remember the currently selected window. */
12191 sw = w;
12192
12193 if (!EQ (old_frame, selected_frame)
12194 && FRAME_LIVE_P (XFRAME (old_frame)))
12195 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12196 selected_frame and selected_window to be temporarily out-of-sync so
12197 when we come back here via `goto retry', we need to resync because we
12198 may need to run Elisp code (via prepare_menu_bars). */
12199 select_frame_for_redisplay (old_frame);
12200
12201 pending = 0;
12202 reconsider_clip_changes (w, current_buffer);
12203 last_escape_glyph_frame = NULL;
12204 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12205 last_glyphless_glyph_frame = NULL;
12206 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12207
12208 /* If new fonts have been loaded that make a glyph matrix adjustment
12209 necessary, do it. */
12210 if (fonts_changed_p)
12211 {
12212 adjust_glyphs (NULL);
12213 ++windows_or_buffers_changed;
12214 fonts_changed_p = 0;
12215 }
12216
12217 /* If face_change_count is non-zero, init_iterator will free all
12218 realized faces, which includes the faces referenced from current
12219 matrices. So, we can't reuse current matrices in this case. */
12220 if (face_change_count)
12221 ++windows_or_buffers_changed;
12222
12223 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12224 && FRAME_TTY (sf)->previous_frame != sf)
12225 {
12226 /* Since frames on a single ASCII terminal share the same
12227 display area, displaying a different frame means redisplay
12228 the whole thing. */
12229 windows_or_buffers_changed++;
12230 SET_FRAME_GARBAGED (sf);
12231 #ifndef DOS_NT
12232 set_tty_color_mode (FRAME_TTY (sf), sf);
12233 #endif
12234 FRAME_TTY (sf)->previous_frame = sf;
12235 }
12236
12237 /* Set the visible flags for all frames. Do this before checking
12238 for resized or garbaged frames; they want to know if their frames
12239 are visible. See the comment in frame.h for
12240 FRAME_SAMPLE_VISIBILITY. */
12241 {
12242 Lisp_Object tail, frame;
12243
12244 number_of_visible_frames = 0;
12245
12246 FOR_EACH_FRAME (tail, frame)
12247 {
12248 struct frame *f = XFRAME (frame);
12249
12250 FRAME_SAMPLE_VISIBILITY (f);
12251 if (FRAME_VISIBLE_P (f))
12252 ++number_of_visible_frames;
12253 clear_desired_matrices (f);
12254 }
12255 }
12256
12257 /* Notice any pending interrupt request to change frame size. */
12258 do_pending_window_change (1);
12259
12260 /* do_pending_window_change could change the selected_window due to
12261 frame resizing which makes the selected window too small. */
12262 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12263 {
12264 sw = w;
12265 reconsider_clip_changes (w, current_buffer);
12266 }
12267
12268 /* Clear frames marked as garbaged. */
12269 if (frame_garbaged)
12270 clear_garbaged_frames ();
12271
12272 /* Build menubar and tool-bar items. */
12273 if (NILP (Vmemory_full))
12274 prepare_menu_bars ();
12275
12276 if (windows_or_buffers_changed)
12277 update_mode_lines++;
12278
12279 /* Detect case that we need to write or remove a star in the mode line. */
12280 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12281 {
12282 w->update_mode_line = Qt;
12283 if (buffer_shared > 1)
12284 update_mode_lines++;
12285 }
12286
12287 /* Avoid invocation of point motion hooks by `current_column' below. */
12288 count1 = SPECPDL_INDEX ();
12289 specbind (Qinhibit_point_motion_hooks, Qt);
12290
12291 /* If %c is in the mode line, update it if needed. */
12292 if (!NILP (w->column_number_displayed)
12293 /* This alternative quickly identifies a common case
12294 where no change is needed. */
12295 && !(PT == XFASTINT (w->last_point)
12296 && XFASTINT (w->last_modified) >= MODIFF
12297 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12298 && (XFASTINT (w->column_number_displayed) != current_column ()))
12299 w->update_mode_line = Qt;
12300
12301 unbind_to (count1, Qnil);
12302
12303 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12304
12305 /* The variable buffer_shared is set in redisplay_window and
12306 indicates that we redisplay a buffer in different windows. See
12307 there. */
12308 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12309 || cursor_type_changed);
12310
12311 /* If specs for an arrow have changed, do thorough redisplay
12312 to ensure we remove any arrow that should no longer exist. */
12313 if (overlay_arrows_changed_p ())
12314 consider_all_windows_p = windows_or_buffers_changed = 1;
12315
12316 /* Normally the message* functions will have already displayed and
12317 updated the echo area, but the frame may have been trashed, or
12318 the update may have been preempted, so display the echo area
12319 again here. Checking message_cleared_p captures the case that
12320 the echo area should be cleared. */
12321 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12322 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12323 || (message_cleared_p
12324 && minibuf_level == 0
12325 /* If the mini-window is currently selected, this means the
12326 echo-area doesn't show through. */
12327 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12328 {
12329 int window_height_changed_p = echo_area_display (0);
12330 must_finish = 1;
12331
12332 /* If we don't display the current message, don't clear the
12333 message_cleared_p flag, because, if we did, we wouldn't clear
12334 the echo area in the next redisplay which doesn't preserve
12335 the echo area. */
12336 if (!display_last_displayed_message_p)
12337 message_cleared_p = 0;
12338
12339 if (fonts_changed_p)
12340 goto retry;
12341 else if (window_height_changed_p)
12342 {
12343 consider_all_windows_p = 1;
12344 ++update_mode_lines;
12345 ++windows_or_buffers_changed;
12346
12347 /* If window configuration was changed, frames may have been
12348 marked garbaged. Clear them or we will experience
12349 surprises wrt scrolling. */
12350 if (frame_garbaged)
12351 clear_garbaged_frames ();
12352 }
12353 }
12354 else if (EQ (selected_window, minibuf_window)
12355 && (current_buffer->clip_changed
12356 || XFASTINT (w->last_modified) < MODIFF
12357 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12358 && resize_mini_window (w, 0))
12359 {
12360 /* Resized active mini-window to fit the size of what it is
12361 showing if its contents might have changed. */
12362 must_finish = 1;
12363 /* FIXME: this causes all frames to be updated, which seems unnecessary
12364 since only the current frame needs to be considered. This function needs
12365 to be rewritten with two variables, consider_all_windows and
12366 consider_all_frames. */
12367 consider_all_windows_p = 1;
12368 ++windows_or_buffers_changed;
12369 ++update_mode_lines;
12370
12371 /* If window configuration was changed, frames may have been
12372 marked garbaged. Clear them or we will experience
12373 surprises wrt scrolling. */
12374 if (frame_garbaged)
12375 clear_garbaged_frames ();
12376 }
12377
12378
12379 /* If showing the region, and mark has changed, we must redisplay
12380 the whole window. The assignment to this_line_start_pos prevents
12381 the optimization directly below this if-statement. */
12382 if (((!NILP (Vtransient_mark_mode)
12383 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12384 != !NILP (w->region_showing))
12385 || (!NILP (w->region_showing)
12386 && !EQ (w->region_showing,
12387 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12388 CHARPOS (this_line_start_pos) = 0;
12389
12390 /* Optimize the case that only the line containing the cursor in the
12391 selected window has changed. Variables starting with this_ are
12392 set in display_line and record information about the line
12393 containing the cursor. */
12394 tlbufpos = this_line_start_pos;
12395 tlendpos = this_line_end_pos;
12396 if (!consider_all_windows_p
12397 && CHARPOS (tlbufpos) > 0
12398 && NILP (w->update_mode_line)
12399 && !current_buffer->clip_changed
12400 && !current_buffer->prevent_redisplay_optimizations_p
12401 && FRAME_VISIBLE_P (XFRAME (w->frame))
12402 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12403 /* Make sure recorded data applies to current buffer, etc. */
12404 && this_line_buffer == current_buffer
12405 && current_buffer == XBUFFER (w->buffer)
12406 && NILP (w->force_start)
12407 && NILP (w->optional_new_start)
12408 /* Point must be on the line that we have info recorded about. */
12409 && PT >= CHARPOS (tlbufpos)
12410 && PT <= Z - CHARPOS (tlendpos)
12411 /* All text outside that line, including its final newline,
12412 must be unchanged. */
12413 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
12414 CHARPOS (tlendpos)))
12415 {
12416 if (CHARPOS (tlbufpos) > BEGV
12417 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
12418 && (CHARPOS (tlbufpos) == ZV
12419 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
12420 /* Former continuation line has disappeared by becoming empty. */
12421 goto cancel;
12422 else if (XFASTINT (w->last_modified) < MODIFF
12423 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
12424 || MINI_WINDOW_P (w))
12425 {
12426 /* We have to handle the case of continuation around a
12427 wide-column character (see the comment in indent.c around
12428 line 1340).
12429
12430 For instance, in the following case:
12431
12432 -------- Insert --------
12433 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
12434 J_I_ ==> J_I_ `^^' are cursors.
12435 ^^ ^^
12436 -------- --------
12437
12438 As we have to redraw the line above, we cannot use this
12439 optimization. */
12440
12441 struct it it;
12442 int line_height_before = this_line_pixel_height;
12443
12444 /* Note that start_display will handle the case that the
12445 line starting at tlbufpos is a continuation line. */
12446 start_display (&it, w, tlbufpos);
12447
12448 /* Implementation note: It this still necessary? */
12449 if (it.current_x != this_line_start_x)
12450 goto cancel;
12451
12452 TRACE ((stderr, "trying display optimization 1\n"));
12453 w->cursor.vpos = -1;
12454 overlay_arrow_seen = 0;
12455 it.vpos = this_line_vpos;
12456 it.current_y = this_line_y;
12457 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
12458 display_line (&it);
12459
12460 /* If line contains point, is not continued,
12461 and ends at same distance from eob as before, we win. */
12462 if (w->cursor.vpos >= 0
12463 /* Line is not continued, otherwise this_line_start_pos
12464 would have been set to 0 in display_line. */
12465 && CHARPOS (this_line_start_pos)
12466 /* Line ends as before. */
12467 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
12468 /* Line has same height as before. Otherwise other lines
12469 would have to be shifted up or down. */
12470 && this_line_pixel_height == line_height_before)
12471 {
12472 /* If this is not the window's last line, we must adjust
12473 the charstarts of the lines below. */
12474 if (it.current_y < it.last_visible_y)
12475 {
12476 struct glyph_row *row
12477 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
12478 EMACS_INT delta, delta_bytes;
12479
12480 /* We used to distinguish between two cases here,
12481 conditioned by Z - CHARPOS (tlendpos) == ZV, for
12482 when the line ends in a newline or the end of the
12483 buffer's accessible portion. But both cases did
12484 the same, so they were collapsed. */
12485 delta = (Z
12486 - CHARPOS (tlendpos)
12487 - MATRIX_ROW_START_CHARPOS (row));
12488 delta_bytes = (Z_BYTE
12489 - BYTEPOS (tlendpos)
12490 - MATRIX_ROW_START_BYTEPOS (row));
12491
12492 increment_matrix_positions (w->current_matrix,
12493 this_line_vpos + 1,
12494 w->current_matrix->nrows,
12495 delta, delta_bytes);
12496 }
12497
12498 /* If this row displays text now but previously didn't,
12499 or vice versa, w->window_end_vpos may have to be
12500 adjusted. */
12501 if ((it.glyph_row - 1)->displays_text_p)
12502 {
12503 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
12504 XSETINT (w->window_end_vpos, this_line_vpos);
12505 }
12506 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
12507 && this_line_vpos > 0)
12508 XSETINT (w->window_end_vpos, this_line_vpos - 1);
12509 w->window_end_valid = Qnil;
12510
12511 /* Update hint: No need to try to scroll in update_window. */
12512 w->desired_matrix->no_scrolling_p = 1;
12513
12514 #if GLYPH_DEBUG
12515 *w->desired_matrix->method = 0;
12516 debug_method_add (w, "optimization 1");
12517 #endif
12518 #ifdef HAVE_WINDOW_SYSTEM
12519 update_window_fringes (w, 0);
12520 #endif
12521 goto update;
12522 }
12523 else
12524 goto cancel;
12525 }
12526 else if (/* Cursor position hasn't changed. */
12527 PT == XFASTINT (w->last_point)
12528 /* Make sure the cursor was last displayed
12529 in this window. Otherwise we have to reposition it. */
12530 && 0 <= w->cursor.vpos
12531 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
12532 {
12533 if (!must_finish)
12534 {
12535 do_pending_window_change (1);
12536 /* If selected_window changed, redisplay again. */
12537 if (WINDOWP (selected_window)
12538 && (w = XWINDOW (selected_window)) != sw)
12539 goto retry;
12540
12541 /* We used to always goto end_of_redisplay here, but this
12542 isn't enough if we have a blinking cursor. */
12543 if (w->cursor_off_p == w->last_cursor_off_p)
12544 goto end_of_redisplay;
12545 }
12546 goto update;
12547 }
12548 /* If highlighting the region, or if the cursor is in the echo area,
12549 then we can't just move the cursor. */
12550 else if (! (!NILP (Vtransient_mark_mode)
12551 && !NILP (BVAR (current_buffer, mark_active)))
12552 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
12553 || highlight_nonselected_windows)
12554 && NILP (w->region_showing)
12555 && NILP (Vshow_trailing_whitespace)
12556 && !cursor_in_echo_area)
12557 {
12558 struct it it;
12559 struct glyph_row *row;
12560
12561 /* Skip from tlbufpos to PT and see where it is. Note that
12562 PT may be in invisible text. If so, we will end at the
12563 next visible position. */
12564 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
12565 NULL, DEFAULT_FACE_ID);
12566 it.current_x = this_line_start_x;
12567 it.current_y = this_line_y;
12568 it.vpos = this_line_vpos;
12569
12570 /* The call to move_it_to stops in front of PT, but
12571 moves over before-strings. */
12572 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
12573
12574 if (it.vpos == this_line_vpos
12575 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
12576 row->enabled_p))
12577 {
12578 xassert (this_line_vpos == it.vpos);
12579 xassert (this_line_y == it.current_y);
12580 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
12581 #if GLYPH_DEBUG
12582 *w->desired_matrix->method = 0;
12583 debug_method_add (w, "optimization 3");
12584 #endif
12585 goto update;
12586 }
12587 else
12588 goto cancel;
12589 }
12590
12591 cancel:
12592 /* Text changed drastically or point moved off of line. */
12593 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
12594 }
12595
12596 CHARPOS (this_line_start_pos) = 0;
12597 consider_all_windows_p |= buffer_shared > 1;
12598 ++clear_face_cache_count;
12599 #ifdef HAVE_WINDOW_SYSTEM
12600 ++clear_image_cache_count;
12601 #endif
12602
12603 /* Build desired matrices, and update the display. If
12604 consider_all_windows_p is non-zero, do it for all windows on all
12605 frames. Otherwise do it for selected_window, only. */
12606
12607 if (consider_all_windows_p)
12608 {
12609 Lisp_Object tail, frame;
12610
12611 FOR_EACH_FRAME (tail, frame)
12612 XFRAME (frame)->updated_p = 0;
12613
12614 /* Recompute # windows showing selected buffer. This will be
12615 incremented each time such a window is displayed. */
12616 buffer_shared = 0;
12617
12618 FOR_EACH_FRAME (tail, frame)
12619 {
12620 struct frame *f = XFRAME (frame);
12621
12622 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
12623 {
12624 if (! EQ (frame, selected_frame))
12625 /* Select the frame, for the sake of frame-local
12626 variables. */
12627 select_frame_for_redisplay (frame);
12628
12629 /* Mark all the scroll bars to be removed; we'll redeem
12630 the ones we want when we redisplay their windows. */
12631 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
12632 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
12633
12634 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12635 redisplay_windows (FRAME_ROOT_WINDOW (f));
12636
12637 /* The X error handler may have deleted that frame. */
12638 if (!FRAME_LIVE_P (f))
12639 continue;
12640
12641 /* Any scroll bars which redisplay_windows should have
12642 nuked should now go away. */
12643 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
12644 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
12645
12646 /* If fonts changed, display again. */
12647 /* ??? rms: I suspect it is a mistake to jump all the way
12648 back to retry here. It should just retry this frame. */
12649 if (fonts_changed_p)
12650 goto retry;
12651
12652 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
12653 {
12654 /* See if we have to hscroll. */
12655 if (!f->already_hscrolled_p)
12656 {
12657 f->already_hscrolled_p = 1;
12658 if (hscroll_windows (f->root_window))
12659 goto retry;
12660 }
12661
12662 /* Prevent various kinds of signals during display
12663 update. stdio is not robust about handling
12664 signals, which can cause an apparent I/O
12665 error. */
12666 if (interrupt_input)
12667 unrequest_sigio ();
12668 STOP_POLLING;
12669
12670 /* Update the display. */
12671 set_window_update_flags (XWINDOW (f->root_window), 1);
12672 pending |= update_frame (f, 0, 0);
12673 f->updated_p = 1;
12674 }
12675 }
12676 }
12677
12678 if (!EQ (old_frame, selected_frame)
12679 && FRAME_LIVE_P (XFRAME (old_frame)))
12680 /* We played a bit fast-and-loose above and allowed selected_frame
12681 and selected_window to be temporarily out-of-sync but let's make
12682 sure this stays contained. */
12683 select_frame_for_redisplay (old_frame);
12684 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
12685
12686 if (!pending)
12687 {
12688 /* Do the mark_window_display_accurate after all windows have
12689 been redisplayed because this call resets flags in buffers
12690 which are needed for proper redisplay. */
12691 FOR_EACH_FRAME (tail, frame)
12692 {
12693 struct frame *f = XFRAME (frame);
12694 if (f->updated_p)
12695 {
12696 mark_window_display_accurate (f->root_window, 1);
12697 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
12698 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
12699 }
12700 }
12701 }
12702 }
12703 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12704 {
12705 Lisp_Object mini_window;
12706 struct frame *mini_frame;
12707
12708 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
12709 /* Use list_of_error, not Qerror, so that
12710 we catch only errors and don't run the debugger. */
12711 internal_condition_case_1 (redisplay_window_1, selected_window,
12712 list_of_error,
12713 redisplay_window_error);
12714
12715 /* Compare desired and current matrices, perform output. */
12716
12717 update:
12718 /* If fonts changed, display again. */
12719 if (fonts_changed_p)
12720 goto retry;
12721
12722 /* Prevent various kinds of signals during display update.
12723 stdio is not robust about handling signals,
12724 which can cause an apparent I/O error. */
12725 if (interrupt_input)
12726 unrequest_sigio ();
12727 STOP_POLLING;
12728
12729 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
12730 {
12731 if (hscroll_windows (selected_window))
12732 goto retry;
12733
12734 XWINDOW (selected_window)->must_be_updated_p = 1;
12735 pending = update_frame (sf, 0, 0);
12736 }
12737
12738 /* We may have called echo_area_display at the top of this
12739 function. If the echo area is on another frame, that may
12740 have put text on a frame other than the selected one, so the
12741 above call to update_frame would not have caught it. Catch
12742 it here. */
12743 mini_window = FRAME_MINIBUF_WINDOW (sf);
12744 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
12745
12746 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
12747 {
12748 XWINDOW (mini_window)->must_be_updated_p = 1;
12749 pending |= update_frame (mini_frame, 0, 0);
12750 if (!pending && hscroll_windows (mini_window))
12751 goto retry;
12752 }
12753 }
12754
12755 /* If display was paused because of pending input, make sure we do a
12756 thorough update the next time. */
12757 if (pending)
12758 {
12759 /* Prevent the optimization at the beginning of
12760 redisplay_internal that tries a single-line update of the
12761 line containing the cursor in the selected window. */
12762 CHARPOS (this_line_start_pos) = 0;
12763
12764 /* Let the overlay arrow be updated the next time. */
12765 update_overlay_arrows (0);
12766
12767 /* If we pause after scrolling, some rows in the current
12768 matrices of some windows are not valid. */
12769 if (!WINDOW_FULL_WIDTH_P (w)
12770 && !FRAME_WINDOW_P (XFRAME (w->frame)))
12771 update_mode_lines = 1;
12772 }
12773 else
12774 {
12775 if (!consider_all_windows_p)
12776 {
12777 /* This has already been done above if
12778 consider_all_windows_p is set. */
12779 mark_window_display_accurate_1 (w, 1);
12780
12781 /* Say overlay arrows are up to date. */
12782 update_overlay_arrows (1);
12783
12784 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
12785 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
12786 }
12787
12788 update_mode_lines = 0;
12789 windows_or_buffers_changed = 0;
12790 cursor_type_changed = 0;
12791 }
12792
12793 /* Start SIGIO interrupts coming again. Having them off during the
12794 code above makes it less likely one will discard output, but not
12795 impossible, since there might be stuff in the system buffer here.
12796 But it is much hairier to try to do anything about that. */
12797 if (interrupt_input)
12798 request_sigio ();
12799 RESUME_POLLING;
12800
12801 /* If a frame has become visible which was not before, redisplay
12802 again, so that we display it. Expose events for such a frame
12803 (which it gets when becoming visible) don't call the parts of
12804 redisplay constructing glyphs, so simply exposing a frame won't
12805 display anything in this case. So, we have to display these
12806 frames here explicitly. */
12807 if (!pending)
12808 {
12809 Lisp_Object tail, frame;
12810 int new_count = 0;
12811
12812 FOR_EACH_FRAME (tail, frame)
12813 {
12814 int this_is_visible = 0;
12815
12816 if (XFRAME (frame)->visible)
12817 this_is_visible = 1;
12818 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
12819 if (XFRAME (frame)->visible)
12820 this_is_visible = 1;
12821
12822 if (this_is_visible)
12823 new_count++;
12824 }
12825
12826 if (new_count != number_of_visible_frames)
12827 windows_or_buffers_changed++;
12828 }
12829
12830 /* Change frame size now if a change is pending. */
12831 do_pending_window_change (1);
12832
12833 /* If we just did a pending size change, or have additional
12834 visible frames, or selected_window changed, redisplay again. */
12835 if ((windows_or_buffers_changed && !pending)
12836 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
12837 goto retry;
12838
12839 /* Clear the face and image caches.
12840
12841 We used to do this only if consider_all_windows_p. But the cache
12842 needs to be cleared if a timer creates images in the current
12843 buffer (e.g. the test case in Bug#6230). */
12844
12845 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
12846 {
12847 clear_face_cache (0);
12848 clear_face_cache_count = 0;
12849 }
12850
12851 #ifdef HAVE_WINDOW_SYSTEM
12852 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
12853 {
12854 clear_image_caches (Qnil);
12855 clear_image_cache_count = 0;
12856 }
12857 #endif /* HAVE_WINDOW_SYSTEM */
12858
12859 end_of_redisplay:
12860 unbind_to (count, Qnil);
12861 RESUME_POLLING;
12862 }
12863
12864
12865 /* Redisplay, but leave alone any recent echo area message unless
12866 another message has been requested in its place.
12867
12868 This is useful in situations where you need to redisplay but no
12869 user action has occurred, making it inappropriate for the message
12870 area to be cleared. See tracking_off and
12871 wait_reading_process_output for examples of these situations.
12872
12873 FROM_WHERE is an integer saying from where this function was
12874 called. This is useful for debugging. */
12875
12876 void
12877 redisplay_preserve_echo_area (int from_where)
12878 {
12879 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
12880
12881 if (!NILP (echo_area_buffer[1]))
12882 {
12883 /* We have a previously displayed message, but no current
12884 message. Redisplay the previous message. */
12885 display_last_displayed_message_p = 1;
12886 redisplay_internal ();
12887 display_last_displayed_message_p = 0;
12888 }
12889 else
12890 redisplay_internal ();
12891
12892 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
12893 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
12894 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
12895 }
12896
12897
12898 /* Function registered with record_unwind_protect in
12899 redisplay_internal. Reset redisplaying_p to the value it had
12900 before redisplay_internal was called, and clear
12901 prevent_freeing_realized_faces_p. It also selects the previously
12902 selected frame, unless it has been deleted (by an X connection
12903 failure during redisplay, for example). */
12904
12905 static Lisp_Object
12906 unwind_redisplay (Lisp_Object val)
12907 {
12908 Lisp_Object old_redisplaying_p, old_frame;
12909
12910 old_redisplaying_p = XCAR (val);
12911 redisplaying_p = XFASTINT (old_redisplaying_p);
12912 old_frame = XCDR (val);
12913 if (! EQ (old_frame, selected_frame)
12914 && FRAME_LIVE_P (XFRAME (old_frame)))
12915 select_frame_for_redisplay (old_frame);
12916 return Qnil;
12917 }
12918
12919
12920 /* Mark the display of window W as accurate or inaccurate. If
12921 ACCURATE_P is non-zero mark display of W as accurate. If
12922 ACCURATE_P is zero, arrange for W to be redisplayed the next time
12923 redisplay_internal is called. */
12924
12925 static void
12926 mark_window_display_accurate_1 (struct window *w, int accurate_p)
12927 {
12928 if (BUFFERP (w->buffer))
12929 {
12930 struct buffer *b = XBUFFER (w->buffer);
12931
12932 w->last_modified
12933 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
12934 w->last_overlay_modified
12935 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
12936 w->last_had_star
12937 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
12938
12939 if (accurate_p)
12940 {
12941 b->clip_changed = 0;
12942 b->prevent_redisplay_optimizations_p = 0;
12943
12944 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
12945 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
12946 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
12947 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
12948
12949 w->current_matrix->buffer = b;
12950 w->current_matrix->begv = BUF_BEGV (b);
12951 w->current_matrix->zv = BUF_ZV (b);
12952
12953 w->last_cursor = w->cursor;
12954 w->last_cursor_off_p = w->cursor_off_p;
12955
12956 if (w == XWINDOW (selected_window))
12957 w->last_point = make_number (BUF_PT (b));
12958 else
12959 w->last_point = make_number (XMARKER (w->pointm)->charpos);
12960 }
12961 }
12962
12963 if (accurate_p)
12964 {
12965 w->window_end_valid = w->buffer;
12966 w->update_mode_line = Qnil;
12967 }
12968 }
12969
12970
12971 /* Mark the display of windows in the window tree rooted at WINDOW as
12972 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
12973 windows as accurate. If ACCURATE_P is zero, arrange for windows to
12974 be redisplayed the next time redisplay_internal is called. */
12975
12976 void
12977 mark_window_display_accurate (Lisp_Object window, int accurate_p)
12978 {
12979 struct window *w;
12980
12981 for (; !NILP (window); window = w->next)
12982 {
12983 w = XWINDOW (window);
12984 mark_window_display_accurate_1 (w, accurate_p);
12985
12986 if (!NILP (w->vchild))
12987 mark_window_display_accurate (w->vchild, accurate_p);
12988 if (!NILP (w->hchild))
12989 mark_window_display_accurate (w->hchild, accurate_p);
12990 }
12991
12992 if (accurate_p)
12993 {
12994 update_overlay_arrows (1);
12995 }
12996 else
12997 {
12998 /* Force a thorough redisplay the next time by setting
12999 last_arrow_position and last_arrow_string to t, which is
13000 unequal to any useful value of Voverlay_arrow_... */
13001 update_overlay_arrows (-1);
13002 }
13003 }
13004
13005
13006 /* Return value in display table DP (Lisp_Char_Table *) for character
13007 C. Since a display table doesn't have any parent, we don't have to
13008 follow parent. Do not call this function directly but use the
13009 macro DISP_CHAR_VECTOR. */
13010
13011 Lisp_Object
13012 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13013 {
13014 Lisp_Object val;
13015
13016 if (ASCII_CHAR_P (c))
13017 {
13018 val = dp->ascii;
13019 if (SUB_CHAR_TABLE_P (val))
13020 val = XSUB_CHAR_TABLE (val)->contents[c];
13021 }
13022 else
13023 {
13024 Lisp_Object table;
13025
13026 XSETCHAR_TABLE (table, dp);
13027 val = char_table_ref (table, c);
13028 }
13029 if (NILP (val))
13030 val = dp->defalt;
13031 return val;
13032 }
13033
13034
13035 \f
13036 /***********************************************************************
13037 Window Redisplay
13038 ***********************************************************************/
13039
13040 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13041
13042 static void
13043 redisplay_windows (Lisp_Object window)
13044 {
13045 while (!NILP (window))
13046 {
13047 struct window *w = XWINDOW (window);
13048
13049 if (!NILP (w->hchild))
13050 redisplay_windows (w->hchild);
13051 else if (!NILP (w->vchild))
13052 redisplay_windows (w->vchild);
13053 else if (!NILP (w->buffer))
13054 {
13055 displayed_buffer = XBUFFER (w->buffer);
13056 /* Use list_of_error, not Qerror, so that
13057 we catch only errors and don't run the debugger. */
13058 internal_condition_case_1 (redisplay_window_0, window,
13059 list_of_error,
13060 redisplay_window_error);
13061 }
13062
13063 window = w->next;
13064 }
13065 }
13066
13067 static Lisp_Object
13068 redisplay_window_error (Lisp_Object ignore)
13069 {
13070 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13071 return Qnil;
13072 }
13073
13074 static Lisp_Object
13075 redisplay_window_0 (Lisp_Object window)
13076 {
13077 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13078 redisplay_window (window, 0);
13079 return Qnil;
13080 }
13081
13082 static Lisp_Object
13083 redisplay_window_1 (Lisp_Object window)
13084 {
13085 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13086 redisplay_window (window, 1);
13087 return Qnil;
13088 }
13089 \f
13090
13091 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13092 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13093 which positions recorded in ROW differ from current buffer
13094 positions.
13095
13096 Return 0 if cursor is not on this row, 1 otherwise. */
13097
13098 static int
13099 set_cursor_from_row (struct window *w, struct glyph_row *row,
13100 struct glyph_matrix *matrix,
13101 EMACS_INT delta, EMACS_INT delta_bytes,
13102 int dy, int dvpos)
13103 {
13104 struct glyph *glyph = row->glyphs[TEXT_AREA];
13105 struct glyph *end = glyph + row->used[TEXT_AREA];
13106 struct glyph *cursor = NULL;
13107 /* The last known character position in row. */
13108 EMACS_INT last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13109 int x = row->x;
13110 EMACS_INT pt_old = PT - delta;
13111 EMACS_INT pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13112 EMACS_INT pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13113 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13114 /* A glyph beyond the edge of TEXT_AREA which we should never
13115 touch. */
13116 struct glyph *glyphs_end = end;
13117 /* Non-zero means we've found a match for cursor position, but that
13118 glyph has the avoid_cursor_p flag set. */
13119 int match_with_avoid_cursor = 0;
13120 /* Non-zero means we've seen at least one glyph that came from a
13121 display string. */
13122 int string_seen = 0;
13123 /* Largest and smalles buffer positions seen so far during scan of
13124 glyph row. */
13125 EMACS_INT bpos_max = pos_before;
13126 EMACS_INT bpos_min = pos_after;
13127 /* Last buffer position covered by an overlay string with an integer
13128 `cursor' property. */
13129 EMACS_INT bpos_covered = 0;
13130
13131 /* Skip over glyphs not having an object at the start and the end of
13132 the row. These are special glyphs like truncation marks on
13133 terminal frames. */
13134 if (row->displays_text_p)
13135 {
13136 if (!row->reversed_p)
13137 {
13138 while (glyph < end
13139 && INTEGERP (glyph->object)
13140 && glyph->charpos < 0)
13141 {
13142 x += glyph->pixel_width;
13143 ++glyph;
13144 }
13145 while (end > glyph
13146 && INTEGERP ((end - 1)->object)
13147 /* CHARPOS is zero for blanks and stretch glyphs
13148 inserted by extend_face_to_end_of_line. */
13149 && (end - 1)->charpos <= 0)
13150 --end;
13151 glyph_before = glyph - 1;
13152 glyph_after = end;
13153 }
13154 else
13155 {
13156 struct glyph *g;
13157
13158 /* If the glyph row is reversed, we need to process it from back
13159 to front, so swap the edge pointers. */
13160 glyphs_end = end = glyph - 1;
13161 glyph += row->used[TEXT_AREA] - 1;
13162
13163 while (glyph > end + 1
13164 && INTEGERP (glyph->object)
13165 && glyph->charpos < 0)
13166 {
13167 --glyph;
13168 x -= glyph->pixel_width;
13169 }
13170 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13171 --glyph;
13172 /* By default, in reversed rows we put the cursor on the
13173 rightmost (first in the reading order) glyph. */
13174 for (g = end + 1; g < glyph; g++)
13175 x += g->pixel_width;
13176 while (end < glyph
13177 && INTEGERP ((end + 1)->object)
13178 && (end + 1)->charpos <= 0)
13179 ++end;
13180 glyph_before = glyph + 1;
13181 glyph_after = end;
13182 }
13183 }
13184 else if (row->reversed_p)
13185 {
13186 /* In R2L rows that don't display text, put the cursor on the
13187 rightmost glyph. Case in point: an empty last line that is
13188 part of an R2L paragraph. */
13189 cursor = end - 1;
13190 /* Avoid placing the cursor on the last glyph of the row, where
13191 on terminal frames we hold the vertical border between
13192 adjacent windows. */
13193 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13194 && !WINDOW_RIGHTMOST_P (w)
13195 && cursor == row->glyphs[LAST_AREA] - 1)
13196 cursor--;
13197 x = -1; /* will be computed below, at label compute_x */
13198 }
13199
13200 /* Step 1: Try to find the glyph whose character position
13201 corresponds to point. If that's not possible, find 2 glyphs
13202 whose character positions are the closest to point, one before
13203 point, the other after it. */
13204 if (!row->reversed_p)
13205 while (/* not marched to end of glyph row */
13206 glyph < end
13207 /* glyph was not inserted by redisplay for internal purposes */
13208 && !INTEGERP (glyph->object))
13209 {
13210 if (BUFFERP (glyph->object))
13211 {
13212 EMACS_INT dpos = glyph->charpos - pt_old;
13213
13214 if (glyph->charpos > bpos_max)
13215 bpos_max = glyph->charpos;
13216 if (glyph->charpos < bpos_min)
13217 bpos_min = glyph->charpos;
13218 if (!glyph->avoid_cursor_p)
13219 {
13220 /* If we hit point, we've found the glyph on which to
13221 display the cursor. */
13222 if (dpos == 0)
13223 {
13224 match_with_avoid_cursor = 0;
13225 break;
13226 }
13227 /* See if we've found a better approximation to
13228 POS_BEFORE or to POS_AFTER. Note that we want the
13229 first (leftmost) glyph of all those that are the
13230 closest from below, and the last (rightmost) of all
13231 those from above. */
13232 if (0 > dpos && dpos > pos_before - pt_old)
13233 {
13234 pos_before = glyph->charpos;
13235 glyph_before = glyph;
13236 }
13237 else if (0 < dpos && dpos <= pos_after - pt_old)
13238 {
13239 pos_after = glyph->charpos;
13240 glyph_after = glyph;
13241 }
13242 }
13243 else if (dpos == 0)
13244 match_with_avoid_cursor = 1;
13245 }
13246 else if (STRINGP (glyph->object))
13247 {
13248 Lisp_Object chprop;
13249 EMACS_INT glyph_pos = glyph->charpos;
13250
13251 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13252 glyph->object);
13253 if (INTEGERP (chprop))
13254 {
13255 bpos_covered = bpos_max + XINT (chprop);
13256 /* If the `cursor' property covers buffer positions up
13257 to and including point, we should display cursor on
13258 this glyph. Note that overlays and text properties
13259 with string values stop bidi reordering, so every
13260 buffer position to the left of the string is always
13261 smaller than any position to the right of the
13262 string. Therefore, if a `cursor' property on one
13263 of the string's characters has an integer value, we
13264 will break out of the loop below _before_ we get to
13265 the position match above. IOW, integer values of
13266 the `cursor' property override the "exact match for
13267 point" strategy of positioning the cursor. */
13268 /* Implementation note: bpos_max == pt_old when, e.g.,
13269 we are in an empty line, where bpos_max is set to
13270 MATRIX_ROW_START_CHARPOS, see above. */
13271 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13272 {
13273 cursor = glyph;
13274 break;
13275 }
13276 }
13277
13278 string_seen = 1;
13279 }
13280 x += glyph->pixel_width;
13281 ++glyph;
13282 }
13283 else if (glyph > end) /* row is reversed */
13284 while (!INTEGERP (glyph->object))
13285 {
13286 if (BUFFERP (glyph->object))
13287 {
13288 EMACS_INT dpos = glyph->charpos - pt_old;
13289
13290 if (glyph->charpos > bpos_max)
13291 bpos_max = glyph->charpos;
13292 if (glyph->charpos < bpos_min)
13293 bpos_min = glyph->charpos;
13294 if (!glyph->avoid_cursor_p)
13295 {
13296 if (dpos == 0)
13297 {
13298 match_with_avoid_cursor = 0;
13299 break;
13300 }
13301 if (0 > dpos && dpos > pos_before - pt_old)
13302 {
13303 pos_before = glyph->charpos;
13304 glyph_before = glyph;
13305 }
13306 else if (0 < dpos && dpos <= pos_after - pt_old)
13307 {
13308 pos_after = glyph->charpos;
13309 glyph_after = glyph;
13310 }
13311 }
13312 else if (dpos == 0)
13313 match_with_avoid_cursor = 1;
13314 }
13315 else if (STRINGP (glyph->object))
13316 {
13317 Lisp_Object chprop;
13318 EMACS_INT glyph_pos = glyph->charpos;
13319
13320 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13321 glyph->object);
13322 if (INTEGERP (chprop))
13323 {
13324 bpos_covered = bpos_max + XINT (chprop);
13325 /* If the `cursor' property covers buffer positions up
13326 to and including point, we should display cursor on
13327 this glyph. */
13328 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13329 {
13330 cursor = glyph;
13331 break;
13332 }
13333 }
13334 string_seen = 1;
13335 }
13336 --glyph;
13337 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13338 {
13339 x--; /* can't use any pixel_width */
13340 break;
13341 }
13342 x -= glyph->pixel_width;
13343 }
13344
13345 /* Step 2: If we didn't find an exact match for point, we need to
13346 look for a proper place to put the cursor among glyphs between
13347 GLYPH_BEFORE and GLYPH_AFTER. */
13348 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13349 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13350 && bpos_covered < pt_old)
13351 {
13352 /* An empty line has a single glyph whose OBJECT is zero and
13353 whose CHARPOS is the position of a newline on that line.
13354 Note that on a TTY, there are more glyphs after that, which
13355 were produced by extend_face_to_end_of_line, but their
13356 CHARPOS is zero or negative. */
13357 int empty_line_p =
13358 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13359 && INTEGERP (glyph->object) && glyph->charpos > 0;
13360
13361 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13362 {
13363 EMACS_INT ellipsis_pos;
13364
13365 /* Scan back over the ellipsis glyphs. */
13366 if (!row->reversed_p)
13367 {
13368 ellipsis_pos = (glyph - 1)->charpos;
13369 while (glyph > row->glyphs[TEXT_AREA]
13370 && (glyph - 1)->charpos == ellipsis_pos)
13371 glyph--, x -= glyph->pixel_width;
13372 /* That loop always goes one position too far, including
13373 the glyph before the ellipsis. So scan forward over
13374 that one. */
13375 x += glyph->pixel_width;
13376 glyph++;
13377 }
13378 else /* row is reversed */
13379 {
13380 ellipsis_pos = (glyph + 1)->charpos;
13381 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
13382 && (glyph + 1)->charpos == ellipsis_pos)
13383 glyph++, x += glyph->pixel_width;
13384 x -= glyph->pixel_width;
13385 glyph--;
13386 }
13387 }
13388 else if (match_with_avoid_cursor
13389 /* A truncated row may not include PT among its
13390 character positions. Setting the cursor inside the
13391 scroll margin will trigger recalculation of hscroll
13392 in hscroll_window_tree. */
13393 || (row->truncated_on_left_p && pt_old < bpos_min)
13394 || (row->truncated_on_right_p && pt_old > bpos_max)
13395 /* Zero-width characters produce no glyphs. */
13396 || (!string_seen
13397 && !empty_line_p
13398 && (row->reversed_p
13399 ? glyph_after > glyphs_end
13400 : glyph_after < glyphs_end)))
13401 {
13402 cursor = glyph_after;
13403 x = -1;
13404 }
13405 else if (string_seen)
13406 {
13407 int incr = row->reversed_p ? -1 : +1;
13408
13409 /* Need to find the glyph that came out of a string which is
13410 present at point. That glyph is somewhere between
13411 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
13412 positioned between POS_BEFORE and POS_AFTER in the
13413 buffer. */
13414 struct glyph *start, *stop;
13415 EMACS_INT pos = pos_before;
13416
13417 x = -1;
13418
13419 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
13420 correspond to POS_BEFORE and POS_AFTER, respectively. We
13421 need START and STOP in the order that corresponds to the
13422 row's direction as given by its reversed_p flag. If the
13423 directionality of characters between POS_BEFORE and
13424 POS_AFTER is the opposite of the row's base direction,
13425 these characters will have been reordered for display,
13426 and we need to reverse START and STOP. */
13427 if (!row->reversed_p)
13428 {
13429 start = min (glyph_before, glyph_after);
13430 stop = max (glyph_before, glyph_after);
13431 }
13432 else
13433 {
13434 start = max (glyph_before, glyph_after);
13435 stop = min (glyph_before, glyph_after);
13436 }
13437 for (glyph = start + incr;
13438 row->reversed_p ? glyph > stop : glyph < stop; )
13439 {
13440
13441 /* Any glyphs that come from the buffer are here because
13442 of bidi reordering. Skip them, and only pay
13443 attention to glyphs that came from some string. */
13444 if (STRINGP (glyph->object))
13445 {
13446 Lisp_Object str;
13447 EMACS_INT tem;
13448
13449 str = glyph->object;
13450 tem = string_buffer_position_lim (str, pos, pos_after, 0);
13451 if (tem == 0 /* from overlay */
13452 || pos <= tem)
13453 {
13454 /* If the string from which this glyph came is
13455 found in the buffer at point, then we've
13456 found the glyph we've been looking for. If
13457 it comes from an overlay (tem == 0), and it
13458 has the `cursor' property on one of its
13459 glyphs, record that glyph as a candidate for
13460 displaying the cursor. (As in the
13461 unidirectional version, we will display the
13462 cursor on the last candidate we find.) */
13463 if (tem == 0 || tem == pt_old)
13464 {
13465 /* The glyphs from this string could have
13466 been reordered. Find the one with the
13467 smallest string position. Or there could
13468 be a character in the string with the
13469 `cursor' property, which means display
13470 cursor on that character's glyph. */
13471 EMACS_INT strpos = glyph->charpos;
13472
13473 if (tem)
13474 cursor = glyph;
13475 for ( ;
13476 (row->reversed_p ? glyph > stop : glyph < stop)
13477 && EQ (glyph->object, str);
13478 glyph += incr)
13479 {
13480 Lisp_Object cprop;
13481 EMACS_INT gpos = glyph->charpos;
13482
13483 cprop = Fget_char_property (make_number (gpos),
13484 Qcursor,
13485 glyph->object);
13486 if (!NILP (cprop))
13487 {
13488 cursor = glyph;
13489 break;
13490 }
13491 if (tem && glyph->charpos < strpos)
13492 {
13493 strpos = glyph->charpos;
13494 cursor = glyph;
13495 }
13496 }
13497
13498 if (tem == pt_old)
13499 goto compute_x;
13500 }
13501 if (tem)
13502 pos = tem + 1; /* don't find previous instances */
13503 }
13504 /* This string is not what we want; skip all of the
13505 glyphs that came from it. */
13506 while ((row->reversed_p ? glyph > stop : glyph < stop)
13507 && EQ (glyph->object, str))
13508 glyph += incr;
13509 }
13510 else
13511 glyph += incr;
13512 }
13513
13514 /* If we reached the end of the line, and END was from a string,
13515 the cursor is not on this line. */
13516 if (cursor == NULL
13517 && (row->reversed_p ? glyph <= end : glyph >= end)
13518 && STRINGP (end->object)
13519 && row->continued_p)
13520 return 0;
13521 }
13522 }
13523
13524 compute_x:
13525 if (cursor != NULL)
13526 glyph = cursor;
13527 if (x < 0)
13528 {
13529 struct glyph *g;
13530
13531 /* Need to compute x that corresponds to GLYPH. */
13532 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
13533 {
13534 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
13535 abort ();
13536 x += g->pixel_width;
13537 }
13538 }
13539
13540 /* ROW could be part of a continued line, which, under bidi
13541 reordering, might have other rows whose start and end charpos
13542 occlude point. Only set w->cursor if we found a better
13543 approximation to the cursor position than we have from previously
13544 examined candidate rows belonging to the same continued line. */
13545 if (/* we already have a candidate row */
13546 w->cursor.vpos >= 0
13547 /* that candidate is not the row we are processing */
13548 && MATRIX_ROW (matrix, w->cursor.vpos) != row
13549 /* the row we are processing is part of a continued line */
13550 && (row->continued_p || MATRIX_ROW_CONTINUATION_LINE_P (row))
13551 /* Make sure cursor.vpos specifies a row whose start and end
13552 charpos occlude point. This is because some callers of this
13553 function leave cursor.vpos at the row where the cursor was
13554 displayed during the last redisplay cycle. */
13555 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
13556 && pt_old < MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)))
13557 {
13558 struct glyph *g1 =
13559 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
13560
13561 /* Don't consider glyphs that are outside TEXT_AREA. */
13562 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
13563 return 0;
13564 /* Keep the candidate whose buffer position is the closest to
13565 point. */
13566 if (/* previous candidate is a glyph in TEXT_AREA of that row */
13567 w->cursor.hpos >= 0
13568 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
13569 && BUFFERP (g1->object)
13570 && (g1->charpos == pt_old /* an exact match always wins */
13571 || (BUFFERP (glyph->object)
13572 && eabs (g1->charpos - pt_old)
13573 < eabs (glyph->charpos - pt_old))))
13574 return 0;
13575 /* If this candidate gives an exact match, use that. */
13576 if (!(BUFFERP (glyph->object) && glyph->charpos == pt_old)
13577 /* Otherwise, keep the candidate that comes from a row
13578 spanning less buffer positions. This may win when one or
13579 both candidate positions are on glyphs that came from
13580 display strings, for which we cannot compare buffer
13581 positions. */
13582 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13583 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
13584 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
13585 return 0;
13586 }
13587 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
13588 w->cursor.x = x;
13589 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
13590 w->cursor.y = row->y + dy;
13591
13592 if (w == XWINDOW (selected_window))
13593 {
13594 if (!row->continued_p
13595 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
13596 && row->x == 0)
13597 {
13598 this_line_buffer = XBUFFER (w->buffer);
13599
13600 CHARPOS (this_line_start_pos)
13601 = MATRIX_ROW_START_CHARPOS (row) + delta;
13602 BYTEPOS (this_line_start_pos)
13603 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
13604
13605 CHARPOS (this_line_end_pos)
13606 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
13607 BYTEPOS (this_line_end_pos)
13608 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
13609
13610 this_line_y = w->cursor.y;
13611 this_line_pixel_height = row->height;
13612 this_line_vpos = w->cursor.vpos;
13613 this_line_start_x = row->x;
13614 }
13615 else
13616 CHARPOS (this_line_start_pos) = 0;
13617 }
13618
13619 return 1;
13620 }
13621
13622
13623 /* Run window scroll functions, if any, for WINDOW with new window
13624 start STARTP. Sets the window start of WINDOW to that position.
13625
13626 We assume that the window's buffer is really current. */
13627
13628 static INLINE struct text_pos
13629 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
13630 {
13631 struct window *w = XWINDOW (window);
13632 SET_MARKER_FROM_TEXT_POS (w->start, startp);
13633
13634 if (current_buffer != XBUFFER (w->buffer))
13635 abort ();
13636
13637 if (!NILP (Vwindow_scroll_functions))
13638 {
13639 run_hook_with_args_2 (Qwindow_scroll_functions, window,
13640 make_number (CHARPOS (startp)));
13641 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13642 /* In case the hook functions switch buffers. */
13643 if (current_buffer != XBUFFER (w->buffer))
13644 set_buffer_internal_1 (XBUFFER (w->buffer));
13645 }
13646
13647 return startp;
13648 }
13649
13650
13651 /* Make sure the line containing the cursor is fully visible.
13652 A value of 1 means there is nothing to be done.
13653 (Either the line is fully visible, or it cannot be made so,
13654 or we cannot tell.)
13655
13656 If FORCE_P is non-zero, return 0 even if partial visible cursor row
13657 is higher than window.
13658
13659 A value of 0 means the caller should do scrolling
13660 as if point had gone off the screen. */
13661
13662 static int
13663 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
13664 {
13665 struct glyph_matrix *matrix;
13666 struct glyph_row *row;
13667 int window_height;
13668
13669 if (!make_cursor_line_fully_visible_p)
13670 return 1;
13671
13672 /* It's not always possible to find the cursor, e.g, when a window
13673 is full of overlay strings. Don't do anything in that case. */
13674 if (w->cursor.vpos < 0)
13675 return 1;
13676
13677 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
13678 row = MATRIX_ROW (matrix, w->cursor.vpos);
13679
13680 /* If the cursor row is not partially visible, there's nothing to do. */
13681 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
13682 return 1;
13683
13684 /* If the row the cursor is in is taller than the window's height,
13685 it's not clear what to do, so do nothing. */
13686 window_height = window_box_height (w);
13687 if (row->height >= window_height)
13688 {
13689 if (!force_p || MINI_WINDOW_P (w)
13690 || w->vscroll || w->cursor.vpos == 0)
13691 return 1;
13692 }
13693 return 0;
13694 }
13695
13696
13697 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
13698 non-zero means only WINDOW is redisplayed in redisplay_internal.
13699 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
13700 in redisplay_window to bring a partially visible line into view in
13701 the case that only the cursor has moved.
13702
13703 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
13704 last screen line's vertical height extends past the end of the screen.
13705
13706 Value is
13707
13708 1 if scrolling succeeded
13709
13710 0 if scrolling didn't find point.
13711
13712 -1 if new fonts have been loaded so that we must interrupt
13713 redisplay, adjust glyph matrices, and try again. */
13714
13715 enum
13716 {
13717 SCROLLING_SUCCESS,
13718 SCROLLING_FAILED,
13719 SCROLLING_NEED_LARGER_MATRICES
13720 };
13721
13722 /* If scroll-conservatively is more than this, never recenter.
13723
13724 If you change this, don't forget to update the doc string of
13725 `scroll-conservatively' and the Emacs manual. */
13726 #define SCROLL_LIMIT 100
13727
13728 static int
13729 try_scrolling (Lisp_Object window, int just_this_one_p,
13730 EMACS_INT arg_scroll_conservatively, EMACS_INT scroll_step,
13731 int temp_scroll_step, int last_line_misfit)
13732 {
13733 struct window *w = XWINDOW (window);
13734 struct frame *f = XFRAME (w->frame);
13735 struct text_pos pos, startp;
13736 struct it it;
13737 int this_scroll_margin, scroll_max, rc, height;
13738 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
13739 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
13740 Lisp_Object aggressive;
13741 /* We will never try scrolling more than this number of lines. */
13742 int scroll_limit = SCROLL_LIMIT;
13743
13744 #if GLYPH_DEBUG
13745 debug_method_add (w, "try_scrolling");
13746 #endif
13747
13748 SET_TEXT_POS_FROM_MARKER (startp, w->start);
13749
13750 /* Compute scroll margin height in pixels. We scroll when point is
13751 within this distance from the top or bottom of the window. */
13752 if (scroll_margin > 0)
13753 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
13754 * FRAME_LINE_HEIGHT (f);
13755 else
13756 this_scroll_margin = 0;
13757
13758 /* Force arg_scroll_conservatively to have a reasonable value, to
13759 avoid scrolling too far away with slow move_it_* functions. Note
13760 that the user can supply scroll-conservatively equal to
13761 `most-positive-fixnum', which can be larger than INT_MAX. */
13762 if (arg_scroll_conservatively > scroll_limit)
13763 {
13764 arg_scroll_conservatively = scroll_limit + 1;
13765 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
13766 }
13767 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
13768 /* Compute how much we should try to scroll maximally to bring
13769 point into view. */
13770 scroll_max = (max (scroll_step,
13771 max (arg_scroll_conservatively, temp_scroll_step))
13772 * FRAME_LINE_HEIGHT (f));
13773 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
13774 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
13775 /* We're trying to scroll because of aggressive scrolling but no
13776 scroll_step is set. Choose an arbitrary one. */
13777 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
13778 else
13779 scroll_max = 0;
13780
13781 too_near_end:
13782
13783 /* Decide whether to scroll down. */
13784 if (PT > CHARPOS (startp))
13785 {
13786 int scroll_margin_y;
13787
13788 /* Compute the pixel ypos of the scroll margin, then move it to
13789 either that ypos or PT, whichever comes first. */
13790 start_display (&it, w, startp);
13791 scroll_margin_y = it.last_visible_y - this_scroll_margin
13792 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
13793 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
13794 (MOVE_TO_POS | MOVE_TO_Y));
13795
13796 if (PT > CHARPOS (it.current.pos))
13797 {
13798 int y0 = line_bottom_y (&it);
13799 /* Compute how many pixels below window bottom to stop searching
13800 for PT. This avoids costly search for PT that is far away if
13801 the user limited scrolling by a small number of lines, but
13802 always finds PT if scroll_conservatively is set to a large
13803 number, such as most-positive-fixnum. */
13804 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
13805 int y_to_move = it.last_visible_y + slack;
13806
13807 /* Compute the distance from the scroll margin to PT or to
13808 the scroll limit, whichever comes first. This should
13809 include the height of the cursor line, to make that line
13810 fully visible. */
13811 move_it_to (&it, PT, -1, y_to_move,
13812 -1, MOVE_TO_POS | MOVE_TO_Y);
13813 dy = line_bottom_y (&it) - y0;
13814
13815 if (dy > scroll_max)
13816 return SCROLLING_FAILED;
13817
13818 scroll_down_p = 1;
13819 }
13820 }
13821
13822 if (scroll_down_p)
13823 {
13824 /* Point is in or below the bottom scroll margin, so move the
13825 window start down. If scrolling conservatively, move it just
13826 enough down to make point visible. If scroll_step is set,
13827 move it down by scroll_step. */
13828 if (arg_scroll_conservatively)
13829 amount_to_scroll
13830 = min (max (dy, FRAME_LINE_HEIGHT (f)),
13831 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
13832 else if (scroll_step || temp_scroll_step)
13833 amount_to_scroll = scroll_max;
13834 else
13835 {
13836 aggressive = BVAR (current_buffer, scroll_up_aggressively);
13837 height = WINDOW_BOX_TEXT_HEIGHT (w);
13838 if (NUMBERP (aggressive))
13839 {
13840 double float_amount = XFLOATINT (aggressive) * height;
13841 amount_to_scroll = float_amount;
13842 if (amount_to_scroll == 0 && float_amount > 0)
13843 amount_to_scroll = 1;
13844 /* Don't let point enter the scroll margin near top of
13845 the window. */
13846 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13847 amount_to_scroll = height - 2*this_scroll_margin + dy;
13848 }
13849 }
13850
13851 if (amount_to_scroll <= 0)
13852 return SCROLLING_FAILED;
13853
13854 start_display (&it, w, startp);
13855 if (arg_scroll_conservatively <= scroll_limit)
13856 move_it_vertically (&it, amount_to_scroll);
13857 else
13858 {
13859 /* Extra precision for users who set scroll-conservatively
13860 to a large number: make sure the amount we scroll
13861 the window start is never less than amount_to_scroll,
13862 which was computed as distance from window bottom to
13863 point. This matters when lines at window top and lines
13864 below window bottom have different height. */
13865 struct it it1;
13866 void *it1data = NULL;
13867 /* We use a temporary it1 because line_bottom_y can modify
13868 its argument, if it moves one line down; see there. */
13869 int start_y;
13870
13871 SAVE_IT (it1, it, it1data);
13872 start_y = line_bottom_y (&it1);
13873 do {
13874 RESTORE_IT (&it, &it, it1data);
13875 move_it_by_lines (&it, 1);
13876 SAVE_IT (it1, it, it1data);
13877 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
13878 }
13879
13880 /* If STARTP is unchanged, move it down another screen line. */
13881 if (CHARPOS (it.current.pos) == CHARPOS (startp))
13882 move_it_by_lines (&it, 1);
13883 startp = it.current.pos;
13884 }
13885 else
13886 {
13887 struct text_pos scroll_margin_pos = startp;
13888
13889 /* See if point is inside the scroll margin at the top of the
13890 window. */
13891 if (this_scroll_margin)
13892 {
13893 start_display (&it, w, startp);
13894 move_it_vertically (&it, this_scroll_margin);
13895 scroll_margin_pos = it.current.pos;
13896 }
13897
13898 if (PT < CHARPOS (scroll_margin_pos))
13899 {
13900 /* Point is in the scroll margin at the top of the window or
13901 above what is displayed in the window. */
13902 int y0, y_to_move;
13903
13904 /* Compute the vertical distance from PT to the scroll
13905 margin position. Move as far as scroll_max allows, or
13906 one screenful, or 10 screen lines, whichever is largest.
13907 Give up if distance is greater than scroll_max. */
13908 SET_TEXT_POS (pos, PT, PT_BYTE);
13909 start_display (&it, w, pos);
13910 y0 = it.current_y;
13911 y_to_move = max (it.last_visible_y,
13912 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
13913 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
13914 y_to_move, -1,
13915 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
13916 dy = it.current_y - y0;
13917 if (dy > scroll_max)
13918 return SCROLLING_FAILED;
13919
13920 /* Compute new window start. */
13921 start_display (&it, w, startp);
13922
13923 if (arg_scroll_conservatively)
13924 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
13925 max (scroll_step, temp_scroll_step));
13926 else if (scroll_step || temp_scroll_step)
13927 amount_to_scroll = scroll_max;
13928 else
13929 {
13930 aggressive = BVAR (current_buffer, scroll_down_aggressively);
13931 height = WINDOW_BOX_TEXT_HEIGHT (w);
13932 if (NUMBERP (aggressive))
13933 {
13934 double float_amount = XFLOATINT (aggressive) * height;
13935 amount_to_scroll = float_amount;
13936 if (amount_to_scroll == 0 && float_amount > 0)
13937 amount_to_scroll = 1;
13938 amount_to_scroll -=
13939 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
13940 /* Don't let point enter the scroll margin near
13941 bottom of the window. */
13942 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
13943 amount_to_scroll = height - 2*this_scroll_margin + dy;
13944 }
13945 }
13946
13947 if (amount_to_scroll <= 0)
13948 return SCROLLING_FAILED;
13949
13950 move_it_vertically_backward (&it, amount_to_scroll);
13951 startp = it.current.pos;
13952 }
13953 }
13954
13955 /* Run window scroll functions. */
13956 startp = run_window_scroll_functions (window, startp);
13957
13958 /* Display the window. Give up if new fonts are loaded, or if point
13959 doesn't appear. */
13960 if (!try_window (window, startp, 0))
13961 rc = SCROLLING_NEED_LARGER_MATRICES;
13962 else if (w->cursor.vpos < 0)
13963 {
13964 clear_glyph_matrix (w->desired_matrix);
13965 rc = SCROLLING_FAILED;
13966 }
13967 else
13968 {
13969 /* Maybe forget recorded base line for line number display. */
13970 if (!just_this_one_p
13971 || current_buffer->clip_changed
13972 || BEG_UNCHANGED < CHARPOS (startp))
13973 w->base_line_number = Qnil;
13974
13975 /* If cursor ends up on a partially visible line,
13976 treat that as being off the bottom of the screen. */
13977 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
13978 /* It's possible that the cursor is on the first line of the
13979 buffer, which is partially obscured due to a vscroll
13980 (Bug#7537). In that case, avoid looping forever . */
13981 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
13982 {
13983 clear_glyph_matrix (w->desired_matrix);
13984 ++extra_scroll_margin_lines;
13985 goto too_near_end;
13986 }
13987 rc = SCROLLING_SUCCESS;
13988 }
13989
13990 return rc;
13991 }
13992
13993
13994 /* Compute a suitable window start for window W if display of W starts
13995 on a continuation line. Value is non-zero if a new window start
13996 was computed.
13997
13998 The new window start will be computed, based on W's width, starting
13999 from the start of the continued line. It is the start of the
14000 screen line with the minimum distance from the old start W->start. */
14001
14002 static int
14003 compute_window_start_on_continuation_line (struct window *w)
14004 {
14005 struct text_pos pos, start_pos;
14006 int window_start_changed_p = 0;
14007
14008 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14009
14010 /* If window start is on a continuation line... Window start may be
14011 < BEGV in case there's invisible text at the start of the
14012 buffer (M-x rmail, for example). */
14013 if (CHARPOS (start_pos) > BEGV
14014 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14015 {
14016 struct it it;
14017 struct glyph_row *row;
14018
14019 /* Handle the case that the window start is out of range. */
14020 if (CHARPOS (start_pos) < BEGV)
14021 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14022 else if (CHARPOS (start_pos) > ZV)
14023 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14024
14025 /* Find the start of the continued line. This should be fast
14026 because scan_buffer is fast (newline cache). */
14027 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14028 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14029 row, DEFAULT_FACE_ID);
14030 reseat_at_previous_visible_line_start (&it);
14031
14032 /* If the line start is "too far" away from the window start,
14033 say it takes too much time to compute a new window start. */
14034 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14035 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14036 {
14037 int min_distance, distance;
14038
14039 /* Move forward by display lines to find the new window
14040 start. If window width was enlarged, the new start can
14041 be expected to be > the old start. If window width was
14042 decreased, the new window start will be < the old start.
14043 So, we're looking for the display line start with the
14044 minimum distance from the old window start. */
14045 pos = it.current.pos;
14046 min_distance = INFINITY;
14047 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14048 distance < min_distance)
14049 {
14050 min_distance = distance;
14051 pos = it.current.pos;
14052 move_it_by_lines (&it, 1);
14053 }
14054
14055 /* Set the window start there. */
14056 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14057 window_start_changed_p = 1;
14058 }
14059 }
14060
14061 return window_start_changed_p;
14062 }
14063
14064
14065 /* Try cursor movement in case text has not changed in window WINDOW,
14066 with window start STARTP. Value is
14067
14068 CURSOR_MOVEMENT_SUCCESS if successful
14069
14070 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14071
14072 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14073 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14074 we want to scroll as if scroll-step were set to 1. See the code.
14075
14076 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14077 which case we have to abort this redisplay, and adjust matrices
14078 first. */
14079
14080 enum
14081 {
14082 CURSOR_MOVEMENT_SUCCESS,
14083 CURSOR_MOVEMENT_CANNOT_BE_USED,
14084 CURSOR_MOVEMENT_MUST_SCROLL,
14085 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14086 };
14087
14088 static int
14089 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14090 {
14091 struct window *w = XWINDOW (window);
14092 struct frame *f = XFRAME (w->frame);
14093 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14094
14095 #if GLYPH_DEBUG
14096 if (inhibit_try_cursor_movement)
14097 return rc;
14098 #endif
14099
14100 /* Handle case where text has not changed, only point, and it has
14101 not moved off the frame. */
14102 if (/* Point may be in this window. */
14103 PT >= CHARPOS (startp)
14104 /* Selective display hasn't changed. */
14105 && !current_buffer->clip_changed
14106 /* Function force-mode-line-update is used to force a thorough
14107 redisplay. It sets either windows_or_buffers_changed or
14108 update_mode_lines. So don't take a shortcut here for these
14109 cases. */
14110 && !update_mode_lines
14111 && !windows_or_buffers_changed
14112 && !cursor_type_changed
14113 /* Can't use this case if highlighting a region. When a
14114 region exists, cursor movement has to do more than just
14115 set the cursor. */
14116 && !(!NILP (Vtransient_mark_mode)
14117 && !NILP (BVAR (current_buffer, mark_active)))
14118 && NILP (w->region_showing)
14119 && NILP (Vshow_trailing_whitespace)
14120 /* Right after splitting windows, last_point may be nil. */
14121 && INTEGERP (w->last_point)
14122 /* This code is not used for mini-buffer for the sake of the case
14123 of redisplaying to replace an echo area message; since in
14124 that case the mini-buffer contents per se are usually
14125 unchanged. This code is of no real use in the mini-buffer
14126 since the handling of this_line_start_pos, etc., in redisplay
14127 handles the same cases. */
14128 && !EQ (window, minibuf_window)
14129 /* When splitting windows or for new windows, it happens that
14130 redisplay is called with a nil window_end_vpos or one being
14131 larger than the window. This should really be fixed in
14132 window.c. I don't have this on my list, now, so we do
14133 approximately the same as the old redisplay code. --gerd. */
14134 && INTEGERP (w->window_end_vpos)
14135 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14136 && (FRAME_WINDOW_P (f)
14137 || !overlay_arrow_in_current_buffer_p ()))
14138 {
14139 int this_scroll_margin, top_scroll_margin;
14140 struct glyph_row *row = NULL;
14141
14142 #if GLYPH_DEBUG
14143 debug_method_add (w, "cursor movement");
14144 #endif
14145
14146 /* Scroll if point within this distance from the top or bottom
14147 of the window. This is a pixel value. */
14148 if (scroll_margin > 0)
14149 {
14150 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14151 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14152 }
14153 else
14154 this_scroll_margin = 0;
14155
14156 top_scroll_margin = this_scroll_margin;
14157 if (WINDOW_WANTS_HEADER_LINE_P (w))
14158 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14159
14160 /* Start with the row the cursor was displayed during the last
14161 not paused redisplay. Give up if that row is not valid. */
14162 if (w->last_cursor.vpos < 0
14163 || w->last_cursor.vpos >= w->current_matrix->nrows)
14164 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14165 else
14166 {
14167 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14168 if (row->mode_line_p)
14169 ++row;
14170 if (!row->enabled_p)
14171 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14172 }
14173
14174 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14175 {
14176 int scroll_p = 0, must_scroll = 0;
14177 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14178
14179 if (PT > XFASTINT (w->last_point))
14180 {
14181 /* Point has moved forward. */
14182 while (MATRIX_ROW_END_CHARPOS (row) < PT
14183 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14184 {
14185 xassert (row->enabled_p);
14186 ++row;
14187 }
14188
14189 /* If the end position of a row equals the start
14190 position of the next row, and PT is at that position,
14191 we would rather display cursor in the next line. */
14192 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14193 && MATRIX_ROW_END_CHARPOS (row) == PT
14194 && row < w->current_matrix->rows
14195 + w->current_matrix->nrows - 1
14196 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14197 && !cursor_row_p (row))
14198 ++row;
14199
14200 /* If within the scroll margin, scroll. Note that
14201 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14202 the next line would be drawn, and that
14203 this_scroll_margin can be zero. */
14204 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14205 || PT > MATRIX_ROW_END_CHARPOS (row)
14206 /* Line is completely visible last line in window
14207 and PT is to be set in the next line. */
14208 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14209 && PT == MATRIX_ROW_END_CHARPOS (row)
14210 && !row->ends_at_zv_p
14211 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14212 scroll_p = 1;
14213 }
14214 else if (PT < XFASTINT (w->last_point))
14215 {
14216 /* Cursor has to be moved backward. Note that PT >=
14217 CHARPOS (startp) because of the outer if-statement. */
14218 while (!row->mode_line_p
14219 && (MATRIX_ROW_START_CHARPOS (row) > PT
14220 || (MATRIX_ROW_START_CHARPOS (row) == PT
14221 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14222 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14223 row > w->current_matrix->rows
14224 && (row-1)->ends_in_newline_from_string_p))))
14225 && (row->y > top_scroll_margin
14226 || CHARPOS (startp) == BEGV))
14227 {
14228 xassert (row->enabled_p);
14229 --row;
14230 }
14231
14232 /* Consider the following case: Window starts at BEGV,
14233 there is invisible, intangible text at BEGV, so that
14234 display starts at some point START > BEGV. It can
14235 happen that we are called with PT somewhere between
14236 BEGV and START. Try to handle that case. */
14237 if (row < w->current_matrix->rows
14238 || row->mode_line_p)
14239 {
14240 row = w->current_matrix->rows;
14241 if (row->mode_line_p)
14242 ++row;
14243 }
14244
14245 /* Due to newlines in overlay strings, we may have to
14246 skip forward over overlay strings. */
14247 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14248 && MATRIX_ROW_END_CHARPOS (row) == PT
14249 && !cursor_row_p (row))
14250 ++row;
14251
14252 /* If within the scroll margin, scroll. */
14253 if (row->y < top_scroll_margin
14254 && CHARPOS (startp) != BEGV)
14255 scroll_p = 1;
14256 }
14257 else
14258 {
14259 /* Cursor did not move. So don't scroll even if cursor line
14260 is partially visible, as it was so before. */
14261 rc = CURSOR_MOVEMENT_SUCCESS;
14262 }
14263
14264 if (PT < MATRIX_ROW_START_CHARPOS (row)
14265 || PT > MATRIX_ROW_END_CHARPOS (row))
14266 {
14267 /* if PT is not in the glyph row, give up. */
14268 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14269 must_scroll = 1;
14270 }
14271 else if (rc != CURSOR_MOVEMENT_SUCCESS
14272 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14273 {
14274 /* If rows are bidi-reordered and point moved, back up
14275 until we find a row that does not belong to a
14276 continuation line. This is because we must consider
14277 all rows of a continued line as candidates for the
14278 new cursor positioning, since row start and end
14279 positions change non-linearly with vertical position
14280 in such rows. */
14281 /* FIXME: Revisit this when glyph ``spilling'' in
14282 continuation lines' rows is implemented for
14283 bidi-reordered rows. */
14284 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
14285 {
14286 xassert (row->enabled_p);
14287 --row;
14288 /* If we hit the beginning of the displayed portion
14289 without finding the first row of a continued
14290 line, give up. */
14291 if (row <= w->current_matrix->rows)
14292 {
14293 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14294 break;
14295 }
14296
14297 }
14298 }
14299 if (must_scroll)
14300 ;
14301 else if (rc != CURSOR_MOVEMENT_SUCCESS
14302 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
14303 && make_cursor_line_fully_visible_p)
14304 {
14305 if (PT == MATRIX_ROW_END_CHARPOS (row)
14306 && !row->ends_at_zv_p
14307 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14308 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14309 else if (row->height > window_box_height (w))
14310 {
14311 /* If we end up in a partially visible line, let's
14312 make it fully visible, except when it's taller
14313 than the window, in which case we can't do much
14314 about it. */
14315 *scroll_step = 1;
14316 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14317 }
14318 else
14319 {
14320 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14321 if (!cursor_row_fully_visible_p (w, 0, 1))
14322 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14323 else
14324 rc = CURSOR_MOVEMENT_SUCCESS;
14325 }
14326 }
14327 else if (scroll_p)
14328 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14329 else if (rc != CURSOR_MOVEMENT_SUCCESS
14330 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14331 {
14332 /* With bidi-reordered rows, there could be more than
14333 one candidate row whose start and end positions
14334 occlude point. We need to let set_cursor_from_row
14335 find the best candidate. */
14336 /* FIXME: Revisit this when glyph ``spilling'' in
14337 continuation lines' rows is implemented for
14338 bidi-reordered rows. */
14339 int rv = 0;
14340
14341 do
14342 {
14343 if (MATRIX_ROW_START_CHARPOS (row) <= PT
14344 && PT <= MATRIX_ROW_END_CHARPOS (row)
14345 && cursor_row_p (row))
14346 rv |= set_cursor_from_row (w, row, w->current_matrix,
14347 0, 0, 0, 0);
14348 /* As soon as we've found the first suitable row
14349 whose ends_at_zv_p flag is set, we are done. */
14350 if (rv
14351 && MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p)
14352 {
14353 rc = CURSOR_MOVEMENT_SUCCESS;
14354 break;
14355 }
14356 ++row;
14357 }
14358 while ((MATRIX_ROW_CONTINUATION_LINE_P (row)
14359 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
14360 || (MATRIX_ROW_START_CHARPOS (row) == PT
14361 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
14362 /* If we didn't find any candidate rows, or exited the
14363 loop before all the candidates were examined, signal
14364 to the caller that this method failed. */
14365 if (rc != CURSOR_MOVEMENT_SUCCESS
14366 && (!rv || MATRIX_ROW_CONTINUATION_LINE_P (row)))
14367 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14368 else if (rv)
14369 rc = CURSOR_MOVEMENT_SUCCESS;
14370 }
14371 else
14372 {
14373 do
14374 {
14375 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
14376 {
14377 rc = CURSOR_MOVEMENT_SUCCESS;
14378 break;
14379 }
14380 ++row;
14381 }
14382 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14383 && MATRIX_ROW_START_CHARPOS (row) == PT
14384 && cursor_row_p (row));
14385 }
14386 }
14387 }
14388
14389 return rc;
14390 }
14391
14392 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
14393 static
14394 #endif
14395 void
14396 set_vertical_scroll_bar (struct window *w)
14397 {
14398 EMACS_INT start, end, whole;
14399
14400 /* Calculate the start and end positions for the current window.
14401 At some point, it would be nice to choose between scrollbars
14402 which reflect the whole buffer size, with special markers
14403 indicating narrowing, and scrollbars which reflect only the
14404 visible region.
14405
14406 Note that mini-buffers sometimes aren't displaying any text. */
14407 if (!MINI_WINDOW_P (w)
14408 || (w == XWINDOW (minibuf_window)
14409 && NILP (echo_area_buffer[0])))
14410 {
14411 struct buffer *buf = XBUFFER (w->buffer);
14412 whole = BUF_ZV (buf) - BUF_BEGV (buf);
14413 start = marker_position (w->start) - BUF_BEGV (buf);
14414 /* I don't think this is guaranteed to be right. For the
14415 moment, we'll pretend it is. */
14416 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
14417
14418 if (end < start)
14419 end = start;
14420 if (whole < (end - start))
14421 whole = end - start;
14422 }
14423 else
14424 start = end = whole = 0;
14425
14426 /* Indicate what this scroll bar ought to be displaying now. */
14427 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14428 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
14429 (w, end - start, whole, start);
14430 }
14431
14432
14433 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
14434 selected_window is redisplayed.
14435
14436 We can return without actually redisplaying the window if
14437 fonts_changed_p is nonzero. In that case, redisplay_internal will
14438 retry. */
14439
14440 static void
14441 redisplay_window (Lisp_Object window, int just_this_one_p)
14442 {
14443 struct window *w = XWINDOW (window);
14444 struct frame *f = XFRAME (w->frame);
14445 struct buffer *buffer = XBUFFER (w->buffer);
14446 struct buffer *old = current_buffer;
14447 struct text_pos lpoint, opoint, startp;
14448 int update_mode_line;
14449 int tem;
14450 struct it it;
14451 /* Record it now because it's overwritten. */
14452 int current_matrix_up_to_date_p = 0;
14453 int used_current_matrix_p = 0;
14454 /* This is less strict than current_matrix_up_to_date_p.
14455 It indictes that the buffer contents and narrowing are unchanged. */
14456 int buffer_unchanged_p = 0;
14457 int temp_scroll_step = 0;
14458 int count = SPECPDL_INDEX ();
14459 int rc;
14460 int centering_position = -1;
14461 int last_line_misfit = 0;
14462 EMACS_INT beg_unchanged, end_unchanged;
14463
14464 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14465 opoint = lpoint;
14466
14467 /* W must be a leaf window here. */
14468 xassert (!NILP (w->buffer));
14469 #if GLYPH_DEBUG
14470 *w->desired_matrix->method = 0;
14471 #endif
14472
14473 restart:
14474 reconsider_clip_changes (w, buffer);
14475
14476 /* Has the mode line to be updated? */
14477 update_mode_line = (!NILP (w->update_mode_line)
14478 || update_mode_lines
14479 || buffer->clip_changed
14480 || buffer->prevent_redisplay_optimizations_p);
14481
14482 if (MINI_WINDOW_P (w))
14483 {
14484 if (w == XWINDOW (echo_area_window)
14485 && !NILP (echo_area_buffer[0]))
14486 {
14487 if (update_mode_line)
14488 /* We may have to update a tty frame's menu bar or a
14489 tool-bar. Example `M-x C-h C-h C-g'. */
14490 goto finish_menu_bars;
14491 else
14492 /* We've already displayed the echo area glyphs in this window. */
14493 goto finish_scroll_bars;
14494 }
14495 else if ((w != XWINDOW (minibuf_window)
14496 || minibuf_level == 0)
14497 /* When buffer is nonempty, redisplay window normally. */
14498 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
14499 /* Quail displays non-mini buffers in minibuffer window.
14500 In that case, redisplay the window normally. */
14501 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
14502 {
14503 /* W is a mini-buffer window, but it's not active, so clear
14504 it. */
14505 int yb = window_text_bottom_y (w);
14506 struct glyph_row *row;
14507 int y;
14508
14509 for (y = 0, row = w->desired_matrix->rows;
14510 y < yb;
14511 y += row->height, ++row)
14512 blank_row (w, row, y);
14513 goto finish_scroll_bars;
14514 }
14515
14516 clear_glyph_matrix (w->desired_matrix);
14517 }
14518
14519 /* Otherwise set up data on this window; select its buffer and point
14520 value. */
14521 /* Really select the buffer, for the sake of buffer-local
14522 variables. */
14523 set_buffer_internal_1 (XBUFFER (w->buffer));
14524
14525 current_matrix_up_to_date_p
14526 = (!NILP (w->window_end_valid)
14527 && !current_buffer->clip_changed
14528 && !current_buffer->prevent_redisplay_optimizations_p
14529 && XFASTINT (w->last_modified) >= MODIFF
14530 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14531
14532 /* Run the window-bottom-change-functions
14533 if it is possible that the text on the screen has changed
14534 (either due to modification of the text, or any other reason). */
14535 if (!current_matrix_up_to_date_p
14536 && !NILP (Vwindow_text_change_functions))
14537 {
14538 safe_run_hooks (Qwindow_text_change_functions);
14539 goto restart;
14540 }
14541
14542 beg_unchanged = BEG_UNCHANGED;
14543 end_unchanged = END_UNCHANGED;
14544
14545 SET_TEXT_POS (opoint, PT, PT_BYTE);
14546
14547 specbind (Qinhibit_point_motion_hooks, Qt);
14548
14549 buffer_unchanged_p
14550 = (!NILP (w->window_end_valid)
14551 && !current_buffer->clip_changed
14552 && XFASTINT (w->last_modified) >= MODIFF
14553 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
14554
14555 /* When windows_or_buffers_changed is non-zero, we can't rely on
14556 the window end being valid, so set it to nil there. */
14557 if (windows_or_buffers_changed)
14558 {
14559 /* If window starts on a continuation line, maybe adjust the
14560 window start in case the window's width changed. */
14561 if (XMARKER (w->start)->buffer == current_buffer)
14562 compute_window_start_on_continuation_line (w);
14563
14564 w->window_end_valid = Qnil;
14565 }
14566
14567 /* Some sanity checks. */
14568 CHECK_WINDOW_END (w);
14569 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
14570 abort ();
14571 if (BYTEPOS (opoint) < CHARPOS (opoint))
14572 abort ();
14573
14574 /* If %c is in mode line, update it if needed. */
14575 if (!NILP (w->column_number_displayed)
14576 /* This alternative quickly identifies a common case
14577 where no change is needed. */
14578 && !(PT == XFASTINT (w->last_point)
14579 && XFASTINT (w->last_modified) >= MODIFF
14580 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
14581 && (XFASTINT (w->column_number_displayed) != current_column ()))
14582 update_mode_line = 1;
14583
14584 /* Count number of windows showing the selected buffer. An indirect
14585 buffer counts as its base buffer. */
14586 if (!just_this_one_p)
14587 {
14588 struct buffer *current_base, *window_base;
14589 current_base = current_buffer;
14590 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
14591 if (current_base->base_buffer)
14592 current_base = current_base->base_buffer;
14593 if (window_base->base_buffer)
14594 window_base = window_base->base_buffer;
14595 if (current_base == window_base)
14596 buffer_shared++;
14597 }
14598
14599 /* Point refers normally to the selected window. For any other
14600 window, set up appropriate value. */
14601 if (!EQ (window, selected_window))
14602 {
14603 EMACS_INT new_pt = XMARKER (w->pointm)->charpos;
14604 EMACS_INT new_pt_byte = marker_byte_position (w->pointm);
14605 if (new_pt < BEGV)
14606 {
14607 new_pt = BEGV;
14608 new_pt_byte = BEGV_BYTE;
14609 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
14610 }
14611 else if (new_pt > (ZV - 1))
14612 {
14613 new_pt = ZV;
14614 new_pt_byte = ZV_BYTE;
14615 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
14616 }
14617
14618 /* We don't use SET_PT so that the point-motion hooks don't run. */
14619 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
14620 }
14621
14622 /* If any of the character widths specified in the display table
14623 have changed, invalidate the width run cache. It's true that
14624 this may be a bit late to catch such changes, but the rest of
14625 redisplay goes (non-fatally) haywire when the display table is
14626 changed, so why should we worry about doing any better? */
14627 if (current_buffer->width_run_cache)
14628 {
14629 struct Lisp_Char_Table *disptab = buffer_display_table ();
14630
14631 if (! disptab_matches_widthtab (disptab,
14632 XVECTOR (BVAR (current_buffer, width_table))))
14633 {
14634 invalidate_region_cache (current_buffer,
14635 current_buffer->width_run_cache,
14636 BEG, Z);
14637 recompute_width_table (current_buffer, disptab);
14638 }
14639 }
14640
14641 /* If window-start is screwed up, choose a new one. */
14642 if (XMARKER (w->start)->buffer != current_buffer)
14643 goto recenter;
14644
14645 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14646
14647 /* If someone specified a new starting point but did not insist,
14648 check whether it can be used. */
14649 if (!NILP (w->optional_new_start)
14650 && CHARPOS (startp) >= BEGV
14651 && CHARPOS (startp) <= ZV)
14652 {
14653 w->optional_new_start = Qnil;
14654 start_display (&it, w, startp);
14655 move_it_to (&it, PT, 0, it.last_visible_y, -1,
14656 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14657 if (IT_CHARPOS (it) == PT)
14658 w->force_start = Qt;
14659 /* IT may overshoot PT if text at PT is invisible. */
14660 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
14661 w->force_start = Qt;
14662 }
14663
14664 force_start:
14665
14666 /* Handle case where place to start displaying has been specified,
14667 unless the specified location is outside the accessible range. */
14668 if (!NILP (w->force_start)
14669 || w->frozen_window_start_p)
14670 {
14671 /* We set this later on if we have to adjust point. */
14672 int new_vpos = -1;
14673
14674 w->force_start = Qnil;
14675 w->vscroll = 0;
14676 w->window_end_valid = Qnil;
14677
14678 /* Forget any recorded base line for line number display. */
14679 if (!buffer_unchanged_p)
14680 w->base_line_number = Qnil;
14681
14682 /* Redisplay the mode line. Select the buffer properly for that.
14683 Also, run the hook window-scroll-functions
14684 because we have scrolled. */
14685 /* Note, we do this after clearing force_start because
14686 if there's an error, it is better to forget about force_start
14687 than to get into an infinite loop calling the hook functions
14688 and having them get more errors. */
14689 if (!update_mode_line
14690 || ! NILP (Vwindow_scroll_functions))
14691 {
14692 update_mode_line = 1;
14693 w->update_mode_line = Qt;
14694 startp = run_window_scroll_functions (window, startp);
14695 }
14696
14697 w->last_modified = make_number (0);
14698 w->last_overlay_modified = make_number (0);
14699 if (CHARPOS (startp) < BEGV)
14700 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
14701 else if (CHARPOS (startp) > ZV)
14702 SET_TEXT_POS (startp, ZV, ZV_BYTE);
14703
14704 /* Redisplay, then check if cursor has been set during the
14705 redisplay. Give up if new fonts were loaded. */
14706 /* We used to issue a CHECK_MARGINS argument to try_window here,
14707 but this causes scrolling to fail when point begins inside
14708 the scroll margin (bug#148) -- cyd */
14709 if (!try_window (window, startp, 0))
14710 {
14711 w->force_start = Qt;
14712 clear_glyph_matrix (w->desired_matrix);
14713 goto need_larger_matrices;
14714 }
14715
14716 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
14717 {
14718 /* If point does not appear, try to move point so it does
14719 appear. The desired matrix has been built above, so we
14720 can use it here. */
14721 new_vpos = window_box_height (w) / 2;
14722 }
14723
14724 if (!cursor_row_fully_visible_p (w, 0, 0))
14725 {
14726 /* Point does appear, but on a line partly visible at end of window.
14727 Move it back to a fully-visible line. */
14728 new_vpos = window_box_height (w);
14729 }
14730
14731 /* If we need to move point for either of the above reasons,
14732 now actually do it. */
14733 if (new_vpos >= 0)
14734 {
14735 struct glyph_row *row;
14736
14737 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
14738 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
14739 ++row;
14740
14741 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
14742 MATRIX_ROW_START_BYTEPOS (row));
14743
14744 if (w != XWINDOW (selected_window))
14745 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
14746 else if (current_buffer == old)
14747 SET_TEXT_POS (lpoint, PT, PT_BYTE);
14748
14749 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
14750
14751 /* If we are highlighting the region, then we just changed
14752 the region, so redisplay to show it. */
14753 if (!NILP (Vtransient_mark_mode)
14754 && !NILP (BVAR (current_buffer, mark_active)))
14755 {
14756 clear_glyph_matrix (w->desired_matrix);
14757 if (!try_window (window, startp, 0))
14758 goto need_larger_matrices;
14759 }
14760 }
14761
14762 #if GLYPH_DEBUG
14763 debug_method_add (w, "forced window start");
14764 #endif
14765 goto done;
14766 }
14767
14768 /* Handle case where text has not changed, only point, and it has
14769 not moved off the frame, and we are not retrying after hscroll.
14770 (current_matrix_up_to_date_p is nonzero when retrying.) */
14771 if (current_matrix_up_to_date_p
14772 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
14773 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
14774 {
14775 switch (rc)
14776 {
14777 case CURSOR_MOVEMENT_SUCCESS:
14778 used_current_matrix_p = 1;
14779 goto done;
14780
14781 case CURSOR_MOVEMENT_MUST_SCROLL:
14782 goto try_to_scroll;
14783
14784 default:
14785 abort ();
14786 }
14787 }
14788 /* If current starting point was originally the beginning of a line
14789 but no longer is, find a new starting point. */
14790 else if (!NILP (w->start_at_line_beg)
14791 && !(CHARPOS (startp) <= BEGV
14792 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
14793 {
14794 #if GLYPH_DEBUG
14795 debug_method_add (w, "recenter 1");
14796 #endif
14797 goto recenter;
14798 }
14799
14800 /* Try scrolling with try_window_id. Value is > 0 if update has
14801 been done, it is -1 if we know that the same window start will
14802 not work. It is 0 if unsuccessful for some other reason. */
14803 else if ((tem = try_window_id (w)) != 0)
14804 {
14805 #if GLYPH_DEBUG
14806 debug_method_add (w, "try_window_id %d", tem);
14807 #endif
14808
14809 if (fonts_changed_p)
14810 goto need_larger_matrices;
14811 if (tem > 0)
14812 goto done;
14813
14814 /* Otherwise try_window_id has returned -1 which means that we
14815 don't want the alternative below this comment to execute. */
14816 }
14817 else if (CHARPOS (startp) >= BEGV
14818 && CHARPOS (startp) <= ZV
14819 && PT >= CHARPOS (startp)
14820 && (CHARPOS (startp) < ZV
14821 /* Avoid starting at end of buffer. */
14822 || CHARPOS (startp) == BEGV
14823 || (XFASTINT (w->last_modified) >= MODIFF
14824 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
14825 {
14826
14827 /* If first window line is a continuation line, and window start
14828 is inside the modified region, but the first change is before
14829 current window start, we must select a new window start.
14830
14831 However, if this is the result of a down-mouse event (e.g. by
14832 extending the mouse-drag-overlay), we don't want to select a
14833 new window start, since that would change the position under
14834 the mouse, resulting in an unwanted mouse-movement rather
14835 than a simple mouse-click. */
14836 if (NILP (w->start_at_line_beg)
14837 && NILP (do_mouse_tracking)
14838 && CHARPOS (startp) > BEGV
14839 && CHARPOS (startp) > BEG + beg_unchanged
14840 && CHARPOS (startp) <= Z - end_unchanged
14841 /* Even if w->start_at_line_beg is nil, a new window may
14842 start at a line_beg, since that's how set_buffer_window
14843 sets it. So, we need to check the return value of
14844 compute_window_start_on_continuation_line. (See also
14845 bug#197). */
14846 && XMARKER (w->start)->buffer == current_buffer
14847 && compute_window_start_on_continuation_line (w))
14848 {
14849 w->force_start = Qt;
14850 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14851 goto force_start;
14852 }
14853
14854 #if GLYPH_DEBUG
14855 debug_method_add (w, "same window start");
14856 #endif
14857
14858 /* Try to redisplay starting at same place as before.
14859 If point has not moved off frame, accept the results. */
14860 if (!current_matrix_up_to_date_p
14861 /* Don't use try_window_reusing_current_matrix in this case
14862 because a window scroll function can have changed the
14863 buffer. */
14864 || !NILP (Vwindow_scroll_functions)
14865 || MINI_WINDOW_P (w)
14866 || !(used_current_matrix_p
14867 = try_window_reusing_current_matrix (w)))
14868 {
14869 IF_DEBUG (debug_method_add (w, "1"));
14870 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
14871 /* -1 means we need to scroll.
14872 0 means we need new matrices, but fonts_changed_p
14873 is set in that case, so we will detect it below. */
14874 goto try_to_scroll;
14875 }
14876
14877 if (fonts_changed_p)
14878 goto need_larger_matrices;
14879
14880 if (w->cursor.vpos >= 0)
14881 {
14882 if (!just_this_one_p
14883 || current_buffer->clip_changed
14884 || BEG_UNCHANGED < CHARPOS (startp))
14885 /* Forget any recorded base line for line number display. */
14886 w->base_line_number = Qnil;
14887
14888 if (!cursor_row_fully_visible_p (w, 1, 0))
14889 {
14890 clear_glyph_matrix (w->desired_matrix);
14891 last_line_misfit = 1;
14892 }
14893 /* Drop through and scroll. */
14894 else
14895 goto done;
14896 }
14897 else
14898 clear_glyph_matrix (w->desired_matrix);
14899 }
14900
14901 try_to_scroll:
14902
14903 w->last_modified = make_number (0);
14904 w->last_overlay_modified = make_number (0);
14905
14906 /* Redisplay the mode line. Select the buffer properly for that. */
14907 if (!update_mode_line)
14908 {
14909 update_mode_line = 1;
14910 w->update_mode_line = Qt;
14911 }
14912
14913 /* Try to scroll by specified few lines. */
14914 if ((scroll_conservatively
14915 || emacs_scroll_step
14916 || temp_scroll_step
14917 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
14918 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
14919 && CHARPOS (startp) >= BEGV
14920 && CHARPOS (startp) <= ZV)
14921 {
14922 /* The function returns -1 if new fonts were loaded, 1 if
14923 successful, 0 if not successful. */
14924 int ss = try_scrolling (window, just_this_one_p,
14925 scroll_conservatively,
14926 emacs_scroll_step,
14927 temp_scroll_step, last_line_misfit);
14928 switch (ss)
14929 {
14930 case SCROLLING_SUCCESS:
14931 goto done;
14932
14933 case SCROLLING_NEED_LARGER_MATRICES:
14934 goto need_larger_matrices;
14935
14936 case SCROLLING_FAILED:
14937 break;
14938
14939 default:
14940 abort ();
14941 }
14942 }
14943
14944 /* Finally, just choose a place to start which positions point
14945 according to user preferences. */
14946
14947 recenter:
14948
14949 #if GLYPH_DEBUG
14950 debug_method_add (w, "recenter");
14951 #endif
14952
14953 /* w->vscroll = 0; */
14954
14955 /* Forget any previously recorded base line for line number display. */
14956 if (!buffer_unchanged_p)
14957 w->base_line_number = Qnil;
14958
14959 /* Determine the window start relative to point. */
14960 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
14961 it.current_y = it.last_visible_y;
14962 if (centering_position < 0)
14963 {
14964 int margin =
14965 scroll_margin > 0
14966 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14967 : 0;
14968 EMACS_INT margin_pos = CHARPOS (startp);
14969 int scrolling_up;
14970 Lisp_Object aggressive;
14971
14972 /* If there is a scroll margin at the top of the window, find
14973 its character position. */
14974 if (margin
14975 /* Cannot call start_display if startp is not in the
14976 accessible region of the buffer. This can happen when we
14977 have just switched to a different buffer and/or changed
14978 its restriction. In that case, startp is initialized to
14979 the character position 1 (BEG) because we did not yet
14980 have chance to display the buffer even once. */
14981 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
14982 {
14983 struct it it1;
14984 void *it1data = NULL;
14985
14986 SAVE_IT (it1, it, it1data);
14987 start_display (&it1, w, startp);
14988 move_it_vertically (&it1, margin);
14989 margin_pos = IT_CHARPOS (it1);
14990 RESTORE_IT (&it, &it, it1data);
14991 }
14992 scrolling_up = PT > margin_pos;
14993 aggressive =
14994 scrolling_up
14995 ? BVAR (current_buffer, scroll_up_aggressively)
14996 : BVAR (current_buffer, scroll_down_aggressively);
14997
14998 if (!MINI_WINDOW_P (w)
14999 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15000 {
15001 int pt_offset = 0;
15002
15003 /* Setting scroll-conservatively overrides
15004 scroll-*-aggressively. */
15005 if (!scroll_conservatively && NUMBERP (aggressive))
15006 {
15007 double float_amount = XFLOATINT (aggressive);
15008
15009 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15010 if (pt_offset == 0 && float_amount > 0)
15011 pt_offset = 1;
15012 if (pt_offset)
15013 margin -= 1;
15014 }
15015 /* Compute how much to move the window start backward from
15016 point so that point will be displayed where the user
15017 wants it. */
15018 if (scrolling_up)
15019 {
15020 centering_position = it.last_visible_y;
15021 if (pt_offset)
15022 centering_position -= pt_offset;
15023 centering_position -=
15024 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0));
15025 /* Don't let point enter the scroll margin near top of
15026 the window. */
15027 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15028 centering_position = margin * FRAME_LINE_HEIGHT (f);
15029 }
15030 else
15031 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15032 }
15033 else
15034 /* Set the window start half the height of the window backward
15035 from point. */
15036 centering_position = window_box_height (w) / 2;
15037 }
15038 move_it_vertically_backward (&it, centering_position);
15039
15040 xassert (IT_CHARPOS (it) >= BEGV);
15041
15042 /* The function move_it_vertically_backward may move over more
15043 than the specified y-distance. If it->w is small, e.g. a
15044 mini-buffer window, we may end up in front of the window's
15045 display area. Start displaying at the start of the line
15046 containing PT in this case. */
15047 if (it.current_y <= 0)
15048 {
15049 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15050 move_it_vertically_backward (&it, 0);
15051 it.current_y = 0;
15052 }
15053
15054 it.current_x = it.hpos = 0;
15055
15056 /* Set the window start position here explicitly, to avoid an
15057 infinite loop in case the functions in window-scroll-functions
15058 get errors. */
15059 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15060
15061 /* Run scroll hooks. */
15062 startp = run_window_scroll_functions (window, it.current.pos);
15063
15064 /* Redisplay the window. */
15065 if (!current_matrix_up_to_date_p
15066 || windows_or_buffers_changed
15067 || cursor_type_changed
15068 /* Don't use try_window_reusing_current_matrix in this case
15069 because it can have changed the buffer. */
15070 || !NILP (Vwindow_scroll_functions)
15071 || !just_this_one_p
15072 || MINI_WINDOW_P (w)
15073 || !(used_current_matrix_p
15074 = try_window_reusing_current_matrix (w)))
15075 try_window (window, startp, 0);
15076
15077 /* If new fonts have been loaded (due to fontsets), give up. We
15078 have to start a new redisplay since we need to re-adjust glyph
15079 matrices. */
15080 if (fonts_changed_p)
15081 goto need_larger_matrices;
15082
15083 /* If cursor did not appear assume that the middle of the window is
15084 in the first line of the window. Do it again with the next line.
15085 (Imagine a window of height 100, displaying two lines of height
15086 60. Moving back 50 from it->last_visible_y will end in the first
15087 line.) */
15088 if (w->cursor.vpos < 0)
15089 {
15090 if (!NILP (w->window_end_valid)
15091 && PT >= Z - XFASTINT (w->window_end_pos))
15092 {
15093 clear_glyph_matrix (w->desired_matrix);
15094 move_it_by_lines (&it, 1);
15095 try_window (window, it.current.pos, 0);
15096 }
15097 else if (PT < IT_CHARPOS (it))
15098 {
15099 clear_glyph_matrix (w->desired_matrix);
15100 move_it_by_lines (&it, -1);
15101 try_window (window, it.current.pos, 0);
15102 }
15103 else
15104 {
15105 /* Not much we can do about it. */
15106 }
15107 }
15108
15109 /* Consider the following case: Window starts at BEGV, there is
15110 invisible, intangible text at BEGV, so that display starts at
15111 some point START > BEGV. It can happen that we are called with
15112 PT somewhere between BEGV and START. Try to handle that case. */
15113 if (w->cursor.vpos < 0)
15114 {
15115 struct glyph_row *row = w->current_matrix->rows;
15116 if (row->mode_line_p)
15117 ++row;
15118 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15119 }
15120
15121 if (!cursor_row_fully_visible_p (w, 0, 0))
15122 {
15123 /* If vscroll is enabled, disable it and try again. */
15124 if (w->vscroll)
15125 {
15126 w->vscroll = 0;
15127 clear_glyph_matrix (w->desired_matrix);
15128 goto recenter;
15129 }
15130
15131 /* If centering point failed to make the whole line visible,
15132 put point at the top instead. That has to make the whole line
15133 visible, if it can be done. */
15134 if (centering_position == 0)
15135 goto done;
15136
15137 clear_glyph_matrix (w->desired_matrix);
15138 centering_position = 0;
15139 goto recenter;
15140 }
15141
15142 done:
15143
15144 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15145 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15146 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15147 ? Qt : Qnil);
15148
15149 /* Display the mode line, if we must. */
15150 if ((update_mode_line
15151 /* If window not full width, must redo its mode line
15152 if (a) the window to its side is being redone and
15153 (b) we do a frame-based redisplay. This is a consequence
15154 of how inverted lines are drawn in frame-based redisplay. */
15155 || (!just_this_one_p
15156 && !FRAME_WINDOW_P (f)
15157 && !WINDOW_FULL_WIDTH_P (w))
15158 /* Line number to display. */
15159 || INTEGERP (w->base_line_pos)
15160 /* Column number is displayed and different from the one displayed. */
15161 || (!NILP (w->column_number_displayed)
15162 && (XFASTINT (w->column_number_displayed) != current_column ())))
15163 /* This means that the window has a mode line. */
15164 && (WINDOW_WANTS_MODELINE_P (w)
15165 || WINDOW_WANTS_HEADER_LINE_P (w)))
15166 {
15167 display_mode_lines (w);
15168
15169 /* If mode line height has changed, arrange for a thorough
15170 immediate redisplay using the correct mode line height. */
15171 if (WINDOW_WANTS_MODELINE_P (w)
15172 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15173 {
15174 fonts_changed_p = 1;
15175 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15176 = DESIRED_MODE_LINE_HEIGHT (w);
15177 }
15178
15179 /* If header line height has changed, arrange for a thorough
15180 immediate redisplay using the correct header line height. */
15181 if (WINDOW_WANTS_HEADER_LINE_P (w)
15182 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15183 {
15184 fonts_changed_p = 1;
15185 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15186 = DESIRED_HEADER_LINE_HEIGHT (w);
15187 }
15188
15189 if (fonts_changed_p)
15190 goto need_larger_matrices;
15191 }
15192
15193 if (!line_number_displayed
15194 && !BUFFERP (w->base_line_pos))
15195 {
15196 w->base_line_pos = Qnil;
15197 w->base_line_number = Qnil;
15198 }
15199
15200 finish_menu_bars:
15201
15202 /* When we reach a frame's selected window, redo the frame's menu bar. */
15203 if (update_mode_line
15204 && EQ (FRAME_SELECTED_WINDOW (f), window))
15205 {
15206 int redisplay_menu_p = 0;
15207
15208 if (FRAME_WINDOW_P (f))
15209 {
15210 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15211 || defined (HAVE_NS) || defined (USE_GTK)
15212 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15213 #else
15214 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15215 #endif
15216 }
15217 else
15218 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15219
15220 if (redisplay_menu_p)
15221 display_menu_bar (w);
15222
15223 #ifdef HAVE_WINDOW_SYSTEM
15224 if (FRAME_WINDOW_P (f))
15225 {
15226 #if defined (USE_GTK) || defined (HAVE_NS)
15227 if (FRAME_EXTERNAL_TOOL_BAR (f))
15228 redisplay_tool_bar (f);
15229 #else
15230 if (WINDOWP (f->tool_bar_window)
15231 && (FRAME_TOOL_BAR_LINES (f) > 0
15232 || !NILP (Vauto_resize_tool_bars))
15233 && redisplay_tool_bar (f))
15234 ignore_mouse_drag_p = 1;
15235 #endif
15236 }
15237 #endif
15238 }
15239
15240 #ifdef HAVE_WINDOW_SYSTEM
15241 if (FRAME_WINDOW_P (f)
15242 && update_window_fringes (w, (just_this_one_p
15243 || (!used_current_matrix_p && !overlay_arrow_seen)
15244 || w->pseudo_window_p)))
15245 {
15246 update_begin (f);
15247 BLOCK_INPUT;
15248 if (draw_window_fringes (w, 1))
15249 x_draw_vertical_border (w);
15250 UNBLOCK_INPUT;
15251 update_end (f);
15252 }
15253 #endif /* HAVE_WINDOW_SYSTEM */
15254
15255 /* We go to this label, with fonts_changed_p nonzero,
15256 if it is necessary to try again using larger glyph matrices.
15257 We have to redeem the scroll bar even in this case,
15258 because the loop in redisplay_internal expects that. */
15259 need_larger_matrices:
15260 ;
15261 finish_scroll_bars:
15262
15263 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
15264 {
15265 /* Set the thumb's position and size. */
15266 set_vertical_scroll_bar (w);
15267
15268 /* Note that we actually used the scroll bar attached to this
15269 window, so it shouldn't be deleted at the end of redisplay. */
15270 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
15271 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
15272 }
15273
15274 /* Restore current_buffer and value of point in it. The window
15275 update may have changed the buffer, so first make sure `opoint'
15276 is still valid (Bug#6177). */
15277 if (CHARPOS (opoint) < BEGV)
15278 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15279 else if (CHARPOS (opoint) > ZV)
15280 TEMP_SET_PT_BOTH (Z, Z_BYTE);
15281 else
15282 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
15283
15284 set_buffer_internal_1 (old);
15285 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
15286 shorter. This can be caused by log truncation in *Messages*. */
15287 if (CHARPOS (lpoint) <= ZV)
15288 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15289
15290 unbind_to (count, Qnil);
15291 }
15292
15293
15294 /* Build the complete desired matrix of WINDOW with a window start
15295 buffer position POS.
15296
15297 Value is 1 if successful. It is zero if fonts were loaded during
15298 redisplay which makes re-adjusting glyph matrices necessary, and -1
15299 if point would appear in the scroll margins.
15300 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
15301 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
15302 set in FLAGS.) */
15303
15304 int
15305 try_window (Lisp_Object window, struct text_pos pos, int flags)
15306 {
15307 struct window *w = XWINDOW (window);
15308 struct it it;
15309 struct glyph_row *last_text_row = NULL;
15310 struct frame *f = XFRAME (w->frame);
15311
15312 /* Make POS the new window start. */
15313 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
15314
15315 /* Mark cursor position as unknown. No overlay arrow seen. */
15316 w->cursor.vpos = -1;
15317 overlay_arrow_seen = 0;
15318
15319 /* Initialize iterator and info to start at POS. */
15320 start_display (&it, w, pos);
15321
15322 /* Display all lines of W. */
15323 while (it.current_y < it.last_visible_y)
15324 {
15325 if (display_line (&it))
15326 last_text_row = it.glyph_row - 1;
15327 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
15328 return 0;
15329 }
15330
15331 /* Don't let the cursor end in the scroll margins. */
15332 if ((flags & TRY_WINDOW_CHECK_MARGINS)
15333 && !MINI_WINDOW_P (w))
15334 {
15335 int this_scroll_margin;
15336
15337 if (scroll_margin > 0)
15338 {
15339 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15340 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15341 }
15342 else
15343 this_scroll_margin = 0;
15344
15345 if ((w->cursor.y >= 0 /* not vscrolled */
15346 && w->cursor.y < this_scroll_margin
15347 && CHARPOS (pos) > BEGV
15348 && IT_CHARPOS (it) < ZV)
15349 /* rms: considering make_cursor_line_fully_visible_p here
15350 seems to give wrong results. We don't want to recenter
15351 when the last line is partly visible, we want to allow
15352 that case to be handled in the usual way. */
15353 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
15354 {
15355 w->cursor.vpos = -1;
15356 clear_glyph_matrix (w->desired_matrix);
15357 return -1;
15358 }
15359 }
15360
15361 /* If bottom moved off end of frame, change mode line percentage. */
15362 if (XFASTINT (w->window_end_pos) <= 0
15363 && Z != IT_CHARPOS (it))
15364 w->update_mode_line = Qt;
15365
15366 /* Set window_end_pos to the offset of the last character displayed
15367 on the window from the end of current_buffer. Set
15368 window_end_vpos to its row number. */
15369 if (last_text_row)
15370 {
15371 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
15372 w->window_end_bytepos
15373 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15374 w->window_end_pos
15375 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15376 w->window_end_vpos
15377 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15378 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
15379 ->displays_text_p);
15380 }
15381 else
15382 {
15383 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15384 w->window_end_pos = make_number (Z - ZV);
15385 w->window_end_vpos = make_number (0);
15386 }
15387
15388 /* But that is not valid info until redisplay finishes. */
15389 w->window_end_valid = Qnil;
15390 return 1;
15391 }
15392
15393
15394 \f
15395 /************************************************************************
15396 Window redisplay reusing current matrix when buffer has not changed
15397 ************************************************************************/
15398
15399 /* Try redisplay of window W showing an unchanged buffer with a
15400 different window start than the last time it was displayed by
15401 reusing its current matrix. Value is non-zero if successful.
15402 W->start is the new window start. */
15403
15404 static int
15405 try_window_reusing_current_matrix (struct window *w)
15406 {
15407 struct frame *f = XFRAME (w->frame);
15408 struct glyph_row *bottom_row;
15409 struct it it;
15410 struct run run;
15411 struct text_pos start, new_start;
15412 int nrows_scrolled, i;
15413 struct glyph_row *last_text_row;
15414 struct glyph_row *last_reused_text_row;
15415 struct glyph_row *start_row;
15416 int start_vpos, min_y, max_y;
15417
15418 #if GLYPH_DEBUG
15419 if (inhibit_try_window_reusing)
15420 return 0;
15421 #endif
15422
15423 if (/* This function doesn't handle terminal frames. */
15424 !FRAME_WINDOW_P (f)
15425 /* Don't try to reuse the display if windows have been split
15426 or such. */
15427 || windows_or_buffers_changed
15428 || cursor_type_changed)
15429 return 0;
15430
15431 /* Can't do this if region may have changed. */
15432 if ((!NILP (Vtransient_mark_mode)
15433 && !NILP (BVAR (current_buffer, mark_active)))
15434 || !NILP (w->region_showing)
15435 || !NILP (Vshow_trailing_whitespace))
15436 return 0;
15437
15438 /* If top-line visibility has changed, give up. */
15439 if (WINDOW_WANTS_HEADER_LINE_P (w)
15440 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
15441 return 0;
15442
15443 /* Give up if old or new display is scrolled vertically. We could
15444 make this function handle this, but right now it doesn't. */
15445 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15446 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
15447 return 0;
15448
15449 /* The variable new_start now holds the new window start. The old
15450 start `start' can be determined from the current matrix. */
15451 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
15452 start = start_row->minpos;
15453 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15454
15455 /* Clear the desired matrix for the display below. */
15456 clear_glyph_matrix (w->desired_matrix);
15457
15458 if (CHARPOS (new_start) <= CHARPOS (start))
15459 {
15460 /* Don't use this method if the display starts with an ellipsis
15461 displayed for invisible text. It's not easy to handle that case
15462 below, and it's certainly not worth the effort since this is
15463 not a frequent case. */
15464 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
15465 return 0;
15466
15467 IF_DEBUG (debug_method_add (w, "twu1"));
15468
15469 /* Display up to a row that can be reused. The variable
15470 last_text_row is set to the last row displayed that displays
15471 text. Note that it.vpos == 0 if or if not there is a
15472 header-line; it's not the same as the MATRIX_ROW_VPOS! */
15473 start_display (&it, w, new_start);
15474 w->cursor.vpos = -1;
15475 last_text_row = last_reused_text_row = NULL;
15476
15477 while (it.current_y < it.last_visible_y
15478 && !fonts_changed_p)
15479 {
15480 /* If we have reached into the characters in the START row,
15481 that means the line boundaries have changed. So we
15482 can't start copying with the row START. Maybe it will
15483 work to start copying with the following row. */
15484 while (IT_CHARPOS (it) > CHARPOS (start))
15485 {
15486 /* Advance to the next row as the "start". */
15487 start_row++;
15488 start = start_row->minpos;
15489 /* If there are no more rows to try, or just one, give up. */
15490 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
15491 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
15492 || CHARPOS (start) == ZV)
15493 {
15494 clear_glyph_matrix (w->desired_matrix);
15495 return 0;
15496 }
15497
15498 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
15499 }
15500 /* If we have reached alignment,
15501 we can copy the rest of the rows. */
15502 if (IT_CHARPOS (it) == CHARPOS (start))
15503 break;
15504
15505 if (display_line (&it))
15506 last_text_row = it.glyph_row - 1;
15507 }
15508
15509 /* A value of current_y < last_visible_y means that we stopped
15510 at the previous window start, which in turn means that we
15511 have at least one reusable row. */
15512 if (it.current_y < it.last_visible_y)
15513 {
15514 struct glyph_row *row;
15515
15516 /* IT.vpos always starts from 0; it counts text lines. */
15517 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
15518
15519 /* Find PT if not already found in the lines displayed. */
15520 if (w->cursor.vpos < 0)
15521 {
15522 int dy = it.current_y - start_row->y;
15523
15524 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15525 row = row_containing_pos (w, PT, row, NULL, dy);
15526 if (row)
15527 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
15528 dy, nrows_scrolled);
15529 else
15530 {
15531 clear_glyph_matrix (w->desired_matrix);
15532 return 0;
15533 }
15534 }
15535
15536 /* Scroll the display. Do it before the current matrix is
15537 changed. The problem here is that update has not yet
15538 run, i.e. part of the current matrix is not up to date.
15539 scroll_run_hook will clear the cursor, and use the
15540 current matrix to get the height of the row the cursor is
15541 in. */
15542 run.current_y = start_row->y;
15543 run.desired_y = it.current_y;
15544 run.height = it.last_visible_y - it.current_y;
15545
15546 if (run.height > 0 && run.current_y != run.desired_y)
15547 {
15548 update_begin (f);
15549 FRAME_RIF (f)->update_window_begin_hook (w);
15550 FRAME_RIF (f)->clear_window_mouse_face (w);
15551 FRAME_RIF (f)->scroll_run_hook (w, &run);
15552 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15553 update_end (f);
15554 }
15555
15556 /* Shift current matrix down by nrows_scrolled lines. */
15557 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15558 rotate_matrix (w->current_matrix,
15559 start_vpos,
15560 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15561 nrows_scrolled);
15562
15563 /* Disable lines that must be updated. */
15564 for (i = 0; i < nrows_scrolled; ++i)
15565 (start_row + i)->enabled_p = 0;
15566
15567 /* Re-compute Y positions. */
15568 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15569 max_y = it.last_visible_y;
15570 for (row = start_row + nrows_scrolled;
15571 row < bottom_row;
15572 ++row)
15573 {
15574 row->y = it.current_y;
15575 row->visible_height = row->height;
15576
15577 if (row->y < min_y)
15578 row->visible_height -= min_y - row->y;
15579 if (row->y + row->height > max_y)
15580 row->visible_height -= row->y + row->height - max_y;
15581 row->redraw_fringe_bitmaps_p = 1;
15582
15583 it.current_y += row->height;
15584
15585 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15586 last_reused_text_row = row;
15587 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
15588 break;
15589 }
15590
15591 /* Disable lines in the current matrix which are now
15592 below the window. */
15593 for (++row; row < bottom_row; ++row)
15594 row->enabled_p = row->mode_line_p = 0;
15595 }
15596
15597 /* Update window_end_pos etc.; last_reused_text_row is the last
15598 reused row from the current matrix containing text, if any.
15599 The value of last_text_row is the last displayed line
15600 containing text. */
15601 if (last_reused_text_row)
15602 {
15603 w->window_end_bytepos
15604 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
15605 w->window_end_pos
15606 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
15607 w->window_end_vpos
15608 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
15609 w->current_matrix));
15610 }
15611 else if (last_text_row)
15612 {
15613 w->window_end_bytepos
15614 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15615 w->window_end_pos
15616 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15617 w->window_end_vpos
15618 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15619 }
15620 else
15621 {
15622 /* This window must be completely empty. */
15623 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
15624 w->window_end_pos = make_number (Z - ZV);
15625 w->window_end_vpos = make_number (0);
15626 }
15627 w->window_end_valid = Qnil;
15628
15629 /* Update hint: don't try scrolling again in update_window. */
15630 w->desired_matrix->no_scrolling_p = 1;
15631
15632 #if GLYPH_DEBUG
15633 debug_method_add (w, "try_window_reusing_current_matrix 1");
15634 #endif
15635 return 1;
15636 }
15637 else if (CHARPOS (new_start) > CHARPOS (start))
15638 {
15639 struct glyph_row *pt_row, *row;
15640 struct glyph_row *first_reusable_row;
15641 struct glyph_row *first_row_to_display;
15642 int dy;
15643 int yb = window_text_bottom_y (w);
15644
15645 /* Find the row starting at new_start, if there is one. Don't
15646 reuse a partially visible line at the end. */
15647 first_reusable_row = start_row;
15648 while (first_reusable_row->enabled_p
15649 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
15650 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15651 < CHARPOS (new_start)))
15652 ++first_reusable_row;
15653
15654 /* Give up if there is no row to reuse. */
15655 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
15656 || !first_reusable_row->enabled_p
15657 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
15658 != CHARPOS (new_start)))
15659 return 0;
15660
15661 /* We can reuse fully visible rows beginning with
15662 first_reusable_row to the end of the window. Set
15663 first_row_to_display to the first row that cannot be reused.
15664 Set pt_row to the row containing point, if there is any. */
15665 pt_row = NULL;
15666 for (first_row_to_display = first_reusable_row;
15667 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
15668 ++first_row_to_display)
15669 {
15670 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
15671 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
15672 pt_row = first_row_to_display;
15673 }
15674
15675 /* Start displaying at the start of first_row_to_display. */
15676 xassert (first_row_to_display->y < yb);
15677 init_to_row_start (&it, w, first_row_to_display);
15678
15679 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
15680 - start_vpos);
15681 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
15682 - nrows_scrolled);
15683 it.current_y = (first_row_to_display->y - first_reusable_row->y
15684 + WINDOW_HEADER_LINE_HEIGHT (w));
15685
15686 /* Display lines beginning with first_row_to_display in the
15687 desired matrix. Set last_text_row to the last row displayed
15688 that displays text. */
15689 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
15690 if (pt_row == NULL)
15691 w->cursor.vpos = -1;
15692 last_text_row = NULL;
15693 while (it.current_y < it.last_visible_y && !fonts_changed_p)
15694 if (display_line (&it))
15695 last_text_row = it.glyph_row - 1;
15696
15697 /* If point is in a reused row, adjust y and vpos of the cursor
15698 position. */
15699 if (pt_row)
15700 {
15701 w->cursor.vpos -= nrows_scrolled;
15702 w->cursor.y -= first_reusable_row->y - start_row->y;
15703 }
15704
15705 /* Give up if point isn't in a row displayed or reused. (This
15706 also handles the case where w->cursor.vpos < nrows_scrolled
15707 after the calls to display_line, which can happen with scroll
15708 margins. See bug#1295.) */
15709 if (w->cursor.vpos < 0)
15710 {
15711 clear_glyph_matrix (w->desired_matrix);
15712 return 0;
15713 }
15714
15715 /* Scroll the display. */
15716 run.current_y = first_reusable_row->y;
15717 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
15718 run.height = it.last_visible_y - run.current_y;
15719 dy = run.current_y - run.desired_y;
15720
15721 if (run.height)
15722 {
15723 update_begin (f);
15724 FRAME_RIF (f)->update_window_begin_hook (w);
15725 FRAME_RIF (f)->clear_window_mouse_face (w);
15726 FRAME_RIF (f)->scroll_run_hook (w, &run);
15727 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
15728 update_end (f);
15729 }
15730
15731 /* Adjust Y positions of reused rows. */
15732 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
15733 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
15734 max_y = it.last_visible_y;
15735 for (row = first_reusable_row; row < first_row_to_display; ++row)
15736 {
15737 row->y -= dy;
15738 row->visible_height = row->height;
15739 if (row->y < min_y)
15740 row->visible_height -= min_y - row->y;
15741 if (row->y + row->height > max_y)
15742 row->visible_height -= row->y + row->height - max_y;
15743 row->redraw_fringe_bitmaps_p = 1;
15744 }
15745
15746 /* Scroll the current matrix. */
15747 xassert (nrows_scrolled > 0);
15748 rotate_matrix (w->current_matrix,
15749 start_vpos,
15750 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
15751 -nrows_scrolled);
15752
15753 /* Disable rows not reused. */
15754 for (row -= nrows_scrolled; row < bottom_row; ++row)
15755 row->enabled_p = 0;
15756
15757 /* Point may have moved to a different line, so we cannot assume that
15758 the previous cursor position is valid; locate the correct row. */
15759 if (pt_row)
15760 {
15761 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15762 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
15763 row++)
15764 {
15765 w->cursor.vpos++;
15766 w->cursor.y = row->y;
15767 }
15768 if (row < bottom_row)
15769 {
15770 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
15771 struct glyph *end = glyph + row->used[TEXT_AREA];
15772
15773 /* Can't use this optimization with bidi-reordered glyph
15774 rows, unless cursor is already at point. */
15775 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15776 {
15777 if (!(w->cursor.hpos >= 0
15778 && w->cursor.hpos < row->used[TEXT_AREA]
15779 && BUFFERP (glyph->object)
15780 && glyph->charpos == PT))
15781 return 0;
15782 }
15783 else
15784 for (; glyph < end
15785 && (!BUFFERP (glyph->object)
15786 || glyph->charpos < PT);
15787 glyph++)
15788 {
15789 w->cursor.hpos++;
15790 w->cursor.x += glyph->pixel_width;
15791 }
15792 }
15793 }
15794
15795 /* Adjust window end. A null value of last_text_row means that
15796 the window end is in reused rows which in turn means that
15797 only its vpos can have changed. */
15798 if (last_text_row)
15799 {
15800 w->window_end_bytepos
15801 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
15802 w->window_end_pos
15803 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
15804 w->window_end_vpos
15805 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
15806 }
15807 else
15808 {
15809 w->window_end_vpos
15810 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
15811 }
15812
15813 w->window_end_valid = Qnil;
15814 w->desired_matrix->no_scrolling_p = 1;
15815
15816 #if GLYPH_DEBUG
15817 debug_method_add (w, "try_window_reusing_current_matrix 2");
15818 #endif
15819 return 1;
15820 }
15821
15822 return 0;
15823 }
15824
15825
15826 \f
15827 /************************************************************************
15828 Window redisplay reusing current matrix when buffer has changed
15829 ************************************************************************/
15830
15831 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
15832 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
15833 EMACS_INT *, EMACS_INT *);
15834 static struct glyph_row *
15835 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
15836 struct glyph_row *);
15837
15838
15839 /* Return the last row in MATRIX displaying text. If row START is
15840 non-null, start searching with that row. IT gives the dimensions
15841 of the display. Value is null if matrix is empty; otherwise it is
15842 a pointer to the row found. */
15843
15844 static struct glyph_row *
15845 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
15846 struct glyph_row *start)
15847 {
15848 struct glyph_row *row, *row_found;
15849
15850 /* Set row_found to the last row in IT->w's current matrix
15851 displaying text. The loop looks funny but think of partially
15852 visible lines. */
15853 row_found = NULL;
15854 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
15855 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15856 {
15857 xassert (row->enabled_p);
15858 row_found = row;
15859 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
15860 break;
15861 ++row;
15862 }
15863
15864 return row_found;
15865 }
15866
15867
15868 /* Return the last row in the current matrix of W that is not affected
15869 by changes at the start of current_buffer that occurred since W's
15870 current matrix was built. Value is null if no such row exists.
15871
15872 BEG_UNCHANGED us the number of characters unchanged at the start of
15873 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
15874 first changed character in current_buffer. Characters at positions <
15875 BEG + BEG_UNCHANGED are at the same buffer positions as they were
15876 when the current matrix was built. */
15877
15878 static struct glyph_row *
15879 find_last_unchanged_at_beg_row (struct window *w)
15880 {
15881 EMACS_INT first_changed_pos = BEG + BEG_UNCHANGED;
15882 struct glyph_row *row;
15883 struct glyph_row *row_found = NULL;
15884 int yb = window_text_bottom_y (w);
15885
15886 /* Find the last row displaying unchanged text. */
15887 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15888 MATRIX_ROW_DISPLAYS_TEXT_P (row)
15889 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
15890 ++row)
15891 {
15892 if (/* If row ends before first_changed_pos, it is unchanged,
15893 except in some case. */
15894 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
15895 /* When row ends in ZV and we write at ZV it is not
15896 unchanged. */
15897 && !row->ends_at_zv_p
15898 /* When first_changed_pos is the end of a continued line,
15899 row is not unchanged because it may be no longer
15900 continued. */
15901 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
15902 && (row->continued_p
15903 || row->exact_window_width_line_p)))
15904 row_found = row;
15905
15906 /* Stop if last visible row. */
15907 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
15908 break;
15909 }
15910
15911 return row_found;
15912 }
15913
15914
15915 /* Find the first glyph row in the current matrix of W that is not
15916 affected by changes at the end of current_buffer since the
15917 time W's current matrix was built.
15918
15919 Return in *DELTA the number of chars by which buffer positions in
15920 unchanged text at the end of current_buffer must be adjusted.
15921
15922 Return in *DELTA_BYTES the corresponding number of bytes.
15923
15924 Value is null if no such row exists, i.e. all rows are affected by
15925 changes. */
15926
15927 static struct glyph_row *
15928 find_first_unchanged_at_end_row (struct window *w,
15929 EMACS_INT *delta, EMACS_INT *delta_bytes)
15930 {
15931 struct glyph_row *row;
15932 struct glyph_row *row_found = NULL;
15933
15934 *delta = *delta_bytes = 0;
15935
15936 /* Display must not have been paused, otherwise the current matrix
15937 is not up to date. */
15938 eassert (!NILP (w->window_end_valid));
15939
15940 /* A value of window_end_pos >= END_UNCHANGED means that the window
15941 end is in the range of changed text. If so, there is no
15942 unchanged row at the end of W's current matrix. */
15943 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
15944 return NULL;
15945
15946 /* Set row to the last row in W's current matrix displaying text. */
15947 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
15948
15949 /* If matrix is entirely empty, no unchanged row exists. */
15950 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
15951 {
15952 /* The value of row is the last glyph row in the matrix having a
15953 meaningful buffer position in it. The end position of row
15954 corresponds to window_end_pos. This allows us to translate
15955 buffer positions in the current matrix to current buffer
15956 positions for characters not in changed text. */
15957 EMACS_INT Z_old =
15958 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
15959 EMACS_INT Z_BYTE_old =
15960 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
15961 EMACS_INT last_unchanged_pos, last_unchanged_pos_old;
15962 struct glyph_row *first_text_row
15963 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15964
15965 *delta = Z - Z_old;
15966 *delta_bytes = Z_BYTE - Z_BYTE_old;
15967
15968 /* Set last_unchanged_pos to the buffer position of the last
15969 character in the buffer that has not been changed. Z is the
15970 index + 1 of the last character in current_buffer, i.e. by
15971 subtracting END_UNCHANGED we get the index of the last
15972 unchanged character, and we have to add BEG to get its buffer
15973 position. */
15974 last_unchanged_pos = Z - END_UNCHANGED + BEG;
15975 last_unchanged_pos_old = last_unchanged_pos - *delta;
15976
15977 /* Search backward from ROW for a row displaying a line that
15978 starts at a minimum position >= last_unchanged_pos_old. */
15979 for (; row > first_text_row; --row)
15980 {
15981 /* This used to abort, but it can happen.
15982 It is ok to just stop the search instead here. KFS. */
15983 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
15984 break;
15985
15986 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
15987 row_found = row;
15988 }
15989 }
15990
15991 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
15992
15993 return row_found;
15994 }
15995
15996
15997 /* Make sure that glyph rows in the current matrix of window W
15998 reference the same glyph memory as corresponding rows in the
15999 frame's frame matrix. This function is called after scrolling W's
16000 current matrix on a terminal frame in try_window_id and
16001 try_window_reusing_current_matrix. */
16002
16003 static void
16004 sync_frame_with_window_matrix_rows (struct window *w)
16005 {
16006 struct frame *f = XFRAME (w->frame);
16007 struct glyph_row *window_row, *window_row_end, *frame_row;
16008
16009 /* Preconditions: W must be a leaf window and full-width. Its frame
16010 must have a frame matrix. */
16011 xassert (NILP (w->hchild) && NILP (w->vchild));
16012 xassert (WINDOW_FULL_WIDTH_P (w));
16013 xassert (!FRAME_WINDOW_P (f));
16014
16015 /* If W is a full-width window, glyph pointers in W's current matrix
16016 have, by definition, to be the same as glyph pointers in the
16017 corresponding frame matrix. Note that frame matrices have no
16018 marginal areas (see build_frame_matrix). */
16019 window_row = w->current_matrix->rows;
16020 window_row_end = window_row + w->current_matrix->nrows;
16021 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16022 while (window_row < window_row_end)
16023 {
16024 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16025 struct glyph *end = window_row->glyphs[LAST_AREA];
16026
16027 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16028 frame_row->glyphs[TEXT_AREA] = start;
16029 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16030 frame_row->glyphs[LAST_AREA] = end;
16031
16032 /* Disable frame rows whose corresponding window rows have
16033 been disabled in try_window_id. */
16034 if (!window_row->enabled_p)
16035 frame_row->enabled_p = 0;
16036
16037 ++window_row, ++frame_row;
16038 }
16039 }
16040
16041
16042 /* Find the glyph row in window W containing CHARPOS. Consider all
16043 rows between START and END (not inclusive). END null means search
16044 all rows to the end of the display area of W. Value is the row
16045 containing CHARPOS or null. */
16046
16047 struct glyph_row *
16048 row_containing_pos (struct window *w, EMACS_INT charpos,
16049 struct glyph_row *start, struct glyph_row *end, int dy)
16050 {
16051 struct glyph_row *row = start;
16052 struct glyph_row *best_row = NULL;
16053 EMACS_INT mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16054 int last_y;
16055
16056 /* If we happen to start on a header-line, skip that. */
16057 if (row->mode_line_p)
16058 ++row;
16059
16060 if ((end && row >= end) || !row->enabled_p)
16061 return NULL;
16062
16063 last_y = window_text_bottom_y (w) - dy;
16064
16065 while (1)
16066 {
16067 /* Give up if we have gone too far. */
16068 if (end && row >= end)
16069 return NULL;
16070 /* This formerly returned if they were equal.
16071 I think that both quantities are of a "last plus one" type;
16072 if so, when they are equal, the row is within the screen. -- rms. */
16073 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16074 return NULL;
16075
16076 /* If it is in this row, return this row. */
16077 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16078 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16079 /* The end position of a row equals the start
16080 position of the next row. If CHARPOS is there, we
16081 would rather display it in the next line, except
16082 when this line ends in ZV. */
16083 && !row->ends_at_zv_p
16084 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16085 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16086 {
16087 struct glyph *g;
16088
16089 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16090 || (!best_row && !row->continued_p))
16091 return row;
16092 /* In bidi-reordered rows, there could be several rows
16093 occluding point, all of them belonging to the same
16094 continued line. We need to find the row which fits
16095 CHARPOS the best. */
16096 for (g = row->glyphs[TEXT_AREA];
16097 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16098 g++)
16099 {
16100 if (!STRINGP (g->object))
16101 {
16102 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16103 {
16104 mindif = eabs (g->charpos - charpos);
16105 best_row = row;
16106 /* Exact match always wins. */
16107 if (mindif == 0)
16108 return best_row;
16109 }
16110 }
16111 }
16112 }
16113 else if (best_row && !row->continued_p)
16114 return best_row;
16115 ++row;
16116 }
16117 }
16118
16119
16120 /* Try to redisplay window W by reusing its existing display. W's
16121 current matrix must be up to date when this function is called,
16122 i.e. window_end_valid must not be nil.
16123
16124 Value is
16125
16126 1 if display has been updated
16127 0 if otherwise unsuccessful
16128 -1 if redisplay with same window start is known not to succeed
16129
16130 The following steps are performed:
16131
16132 1. Find the last row in the current matrix of W that is not
16133 affected by changes at the start of current_buffer. If no such row
16134 is found, give up.
16135
16136 2. Find the first row in W's current matrix that is not affected by
16137 changes at the end of current_buffer. Maybe there is no such row.
16138
16139 3. Display lines beginning with the row + 1 found in step 1 to the
16140 row found in step 2 or, if step 2 didn't find a row, to the end of
16141 the window.
16142
16143 4. If cursor is not known to appear on the window, give up.
16144
16145 5. If display stopped at the row found in step 2, scroll the
16146 display and current matrix as needed.
16147
16148 6. Maybe display some lines at the end of W, if we must. This can
16149 happen under various circumstances, like a partially visible line
16150 becoming fully visible, or because newly displayed lines are displayed
16151 in smaller font sizes.
16152
16153 7. Update W's window end information. */
16154
16155 static int
16156 try_window_id (struct window *w)
16157 {
16158 struct frame *f = XFRAME (w->frame);
16159 struct glyph_matrix *current_matrix = w->current_matrix;
16160 struct glyph_matrix *desired_matrix = w->desired_matrix;
16161 struct glyph_row *last_unchanged_at_beg_row;
16162 struct glyph_row *first_unchanged_at_end_row;
16163 struct glyph_row *row;
16164 struct glyph_row *bottom_row;
16165 int bottom_vpos;
16166 struct it it;
16167 EMACS_INT delta = 0, delta_bytes = 0, stop_pos;
16168 int dvpos, dy;
16169 struct text_pos start_pos;
16170 struct run run;
16171 int first_unchanged_at_end_vpos = 0;
16172 struct glyph_row *last_text_row, *last_text_row_at_end;
16173 struct text_pos start;
16174 EMACS_INT first_changed_charpos, last_changed_charpos;
16175
16176 #if GLYPH_DEBUG
16177 if (inhibit_try_window_id)
16178 return 0;
16179 #endif
16180
16181 /* This is handy for debugging. */
16182 #if 0
16183 #define GIVE_UP(X) \
16184 do { \
16185 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16186 return 0; \
16187 } while (0)
16188 #else
16189 #define GIVE_UP(X) return 0
16190 #endif
16191
16192 SET_TEXT_POS_FROM_MARKER (start, w->start);
16193
16194 /* Don't use this for mini-windows because these can show
16195 messages and mini-buffers, and we don't handle that here. */
16196 if (MINI_WINDOW_P (w))
16197 GIVE_UP (1);
16198
16199 /* This flag is used to prevent redisplay optimizations. */
16200 if (windows_or_buffers_changed || cursor_type_changed)
16201 GIVE_UP (2);
16202
16203 /* Verify that narrowing has not changed.
16204 Also verify that we were not told to prevent redisplay optimizations.
16205 It would be nice to further
16206 reduce the number of cases where this prevents try_window_id. */
16207 if (current_buffer->clip_changed
16208 || current_buffer->prevent_redisplay_optimizations_p)
16209 GIVE_UP (3);
16210
16211 /* Window must either use window-based redisplay or be full width. */
16212 if (!FRAME_WINDOW_P (f)
16213 && (!FRAME_LINE_INS_DEL_OK (f)
16214 || !WINDOW_FULL_WIDTH_P (w)))
16215 GIVE_UP (4);
16216
16217 /* Give up if point is known NOT to appear in W. */
16218 if (PT < CHARPOS (start))
16219 GIVE_UP (5);
16220
16221 /* Another way to prevent redisplay optimizations. */
16222 if (XFASTINT (w->last_modified) == 0)
16223 GIVE_UP (6);
16224
16225 /* Verify that window is not hscrolled. */
16226 if (XFASTINT (w->hscroll) != 0)
16227 GIVE_UP (7);
16228
16229 /* Verify that display wasn't paused. */
16230 if (NILP (w->window_end_valid))
16231 GIVE_UP (8);
16232
16233 /* Can't use this if highlighting a region because a cursor movement
16234 will do more than just set the cursor. */
16235 if (!NILP (Vtransient_mark_mode)
16236 && !NILP (BVAR (current_buffer, mark_active)))
16237 GIVE_UP (9);
16238
16239 /* Likewise if highlighting trailing whitespace. */
16240 if (!NILP (Vshow_trailing_whitespace))
16241 GIVE_UP (11);
16242
16243 /* Likewise if showing a region. */
16244 if (!NILP (w->region_showing))
16245 GIVE_UP (10);
16246
16247 /* Can't use this if overlay arrow position and/or string have
16248 changed. */
16249 if (overlay_arrows_changed_p ())
16250 GIVE_UP (12);
16251
16252 /* When word-wrap is on, adding a space to the first word of a
16253 wrapped line can change the wrap position, altering the line
16254 above it. It might be worthwhile to handle this more
16255 intelligently, but for now just redisplay from scratch. */
16256 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
16257 GIVE_UP (21);
16258
16259 /* Under bidi reordering, adding or deleting a character in the
16260 beginning of a paragraph, before the first strong directional
16261 character, can change the base direction of the paragraph (unless
16262 the buffer specifies a fixed paragraph direction), which will
16263 require to redisplay the whole paragraph. It might be worthwhile
16264 to find the paragraph limits and widen the range of redisplayed
16265 lines to that, but for now just give up this optimization and
16266 redisplay from scratch. */
16267 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16268 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
16269 GIVE_UP (22);
16270
16271 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
16272 only if buffer has really changed. The reason is that the gap is
16273 initially at Z for freshly visited files. The code below would
16274 set end_unchanged to 0 in that case. */
16275 if (MODIFF > SAVE_MODIFF
16276 /* This seems to happen sometimes after saving a buffer. */
16277 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
16278 {
16279 if (GPT - BEG < BEG_UNCHANGED)
16280 BEG_UNCHANGED = GPT - BEG;
16281 if (Z - GPT < END_UNCHANGED)
16282 END_UNCHANGED = Z - GPT;
16283 }
16284
16285 /* The position of the first and last character that has been changed. */
16286 first_changed_charpos = BEG + BEG_UNCHANGED;
16287 last_changed_charpos = Z - END_UNCHANGED;
16288
16289 /* If window starts after a line end, and the last change is in
16290 front of that newline, then changes don't affect the display.
16291 This case happens with stealth-fontification. Note that although
16292 the display is unchanged, glyph positions in the matrix have to
16293 be adjusted, of course. */
16294 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16295 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
16296 && ((last_changed_charpos < CHARPOS (start)
16297 && CHARPOS (start) == BEGV)
16298 || (last_changed_charpos < CHARPOS (start) - 1
16299 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
16300 {
16301 EMACS_INT Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
16302 struct glyph_row *r0;
16303
16304 /* Compute how many chars/bytes have been added to or removed
16305 from the buffer. */
16306 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16307 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16308 Z_delta = Z - Z_old;
16309 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
16310
16311 /* Give up if PT is not in the window. Note that it already has
16312 been checked at the start of try_window_id that PT is not in
16313 front of the window start. */
16314 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
16315 GIVE_UP (13);
16316
16317 /* If window start is unchanged, we can reuse the whole matrix
16318 as is, after adjusting glyph positions. No need to compute
16319 the window end again, since its offset from Z hasn't changed. */
16320 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16321 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
16322 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
16323 /* PT must not be in a partially visible line. */
16324 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
16325 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16326 {
16327 /* Adjust positions in the glyph matrix. */
16328 if (Z_delta || Z_delta_bytes)
16329 {
16330 struct glyph_row *r1
16331 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16332 increment_matrix_positions (w->current_matrix,
16333 MATRIX_ROW_VPOS (r0, current_matrix),
16334 MATRIX_ROW_VPOS (r1, current_matrix),
16335 Z_delta, Z_delta_bytes);
16336 }
16337
16338 /* Set the cursor. */
16339 row = row_containing_pos (w, PT, r0, NULL, 0);
16340 if (row)
16341 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16342 else
16343 abort ();
16344 return 1;
16345 }
16346 }
16347
16348 /* Handle the case that changes are all below what is displayed in
16349 the window, and that PT is in the window. This shortcut cannot
16350 be taken if ZV is visible in the window, and text has been added
16351 there that is visible in the window. */
16352 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
16353 /* ZV is not visible in the window, or there are no
16354 changes at ZV, actually. */
16355 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
16356 || first_changed_charpos == last_changed_charpos))
16357 {
16358 struct glyph_row *r0;
16359
16360 /* Give up if PT is not in the window. Note that it already has
16361 been checked at the start of try_window_id that PT is not in
16362 front of the window start. */
16363 if (PT >= MATRIX_ROW_END_CHARPOS (row))
16364 GIVE_UP (14);
16365
16366 /* If window start is unchanged, we can reuse the whole matrix
16367 as is, without changing glyph positions since no text has
16368 been added/removed in front of the window end. */
16369 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16370 if (TEXT_POS_EQUAL_P (start, r0->minpos)
16371 /* PT must not be in a partially visible line. */
16372 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
16373 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16374 {
16375 /* We have to compute the window end anew since text
16376 could have been added/removed after it. */
16377 w->window_end_pos
16378 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16379 w->window_end_bytepos
16380 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16381
16382 /* Set the cursor. */
16383 row = row_containing_pos (w, PT, r0, NULL, 0);
16384 if (row)
16385 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16386 else
16387 abort ();
16388 return 2;
16389 }
16390 }
16391
16392 /* Give up if window start is in the changed area.
16393
16394 The condition used to read
16395
16396 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
16397
16398 but why that was tested escapes me at the moment. */
16399 if (CHARPOS (start) >= first_changed_charpos
16400 && CHARPOS (start) <= last_changed_charpos)
16401 GIVE_UP (15);
16402
16403 /* Check that window start agrees with the start of the first glyph
16404 row in its current matrix. Check this after we know the window
16405 start is not in changed text, otherwise positions would not be
16406 comparable. */
16407 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
16408 if (!TEXT_POS_EQUAL_P (start, row->minpos))
16409 GIVE_UP (16);
16410
16411 /* Give up if the window ends in strings. Overlay strings
16412 at the end are difficult to handle, so don't try. */
16413 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
16414 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
16415 GIVE_UP (20);
16416
16417 /* Compute the position at which we have to start displaying new
16418 lines. Some of the lines at the top of the window might be
16419 reusable because they are not displaying changed text. Find the
16420 last row in W's current matrix not affected by changes at the
16421 start of current_buffer. Value is null if changes start in the
16422 first line of window. */
16423 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
16424 if (last_unchanged_at_beg_row)
16425 {
16426 /* Avoid starting to display in the moddle of a character, a TAB
16427 for instance. This is easier than to set up the iterator
16428 exactly, and it's not a frequent case, so the additional
16429 effort wouldn't really pay off. */
16430 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
16431 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
16432 && last_unchanged_at_beg_row > w->current_matrix->rows)
16433 --last_unchanged_at_beg_row;
16434
16435 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
16436 GIVE_UP (17);
16437
16438 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
16439 GIVE_UP (18);
16440 start_pos = it.current.pos;
16441
16442 /* Start displaying new lines in the desired matrix at the same
16443 vpos we would use in the current matrix, i.e. below
16444 last_unchanged_at_beg_row. */
16445 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
16446 current_matrix);
16447 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16448 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
16449
16450 xassert (it.hpos == 0 && it.current_x == 0);
16451 }
16452 else
16453 {
16454 /* There are no reusable lines at the start of the window.
16455 Start displaying in the first text line. */
16456 start_display (&it, w, start);
16457 it.vpos = it.first_vpos;
16458 start_pos = it.current.pos;
16459 }
16460
16461 /* Find the first row that is not affected by changes at the end of
16462 the buffer. Value will be null if there is no unchanged row, in
16463 which case we must redisplay to the end of the window. delta
16464 will be set to the value by which buffer positions beginning with
16465 first_unchanged_at_end_row have to be adjusted due to text
16466 changes. */
16467 first_unchanged_at_end_row
16468 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
16469 IF_DEBUG (debug_delta = delta);
16470 IF_DEBUG (debug_delta_bytes = delta_bytes);
16471
16472 /* Set stop_pos to the buffer position up to which we will have to
16473 display new lines. If first_unchanged_at_end_row != NULL, this
16474 is the buffer position of the start of the line displayed in that
16475 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
16476 that we don't stop at a buffer position. */
16477 stop_pos = 0;
16478 if (first_unchanged_at_end_row)
16479 {
16480 xassert (last_unchanged_at_beg_row == NULL
16481 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
16482
16483 /* If this is a continuation line, move forward to the next one
16484 that isn't. Changes in lines above affect this line.
16485 Caution: this may move first_unchanged_at_end_row to a row
16486 not displaying text. */
16487 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
16488 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16489 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16490 < it.last_visible_y))
16491 ++first_unchanged_at_end_row;
16492
16493 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
16494 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
16495 >= it.last_visible_y))
16496 first_unchanged_at_end_row = NULL;
16497 else
16498 {
16499 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
16500 + delta);
16501 first_unchanged_at_end_vpos
16502 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
16503 xassert (stop_pos >= Z - END_UNCHANGED);
16504 }
16505 }
16506 else if (last_unchanged_at_beg_row == NULL)
16507 GIVE_UP (19);
16508
16509
16510 #if GLYPH_DEBUG
16511
16512 /* Either there is no unchanged row at the end, or the one we have
16513 now displays text. This is a necessary condition for the window
16514 end pos calculation at the end of this function. */
16515 xassert (first_unchanged_at_end_row == NULL
16516 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
16517
16518 debug_last_unchanged_at_beg_vpos
16519 = (last_unchanged_at_beg_row
16520 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
16521 : -1);
16522 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
16523
16524 #endif /* GLYPH_DEBUG != 0 */
16525
16526
16527 /* Display new lines. Set last_text_row to the last new line
16528 displayed which has text on it, i.e. might end up as being the
16529 line where the window_end_vpos is. */
16530 w->cursor.vpos = -1;
16531 last_text_row = NULL;
16532 overlay_arrow_seen = 0;
16533 while (it.current_y < it.last_visible_y
16534 && !fonts_changed_p
16535 && (first_unchanged_at_end_row == NULL
16536 || IT_CHARPOS (it) < stop_pos))
16537 {
16538 if (display_line (&it))
16539 last_text_row = it.glyph_row - 1;
16540 }
16541
16542 if (fonts_changed_p)
16543 return -1;
16544
16545
16546 /* Compute differences in buffer positions, y-positions etc. for
16547 lines reused at the bottom of the window. Compute what we can
16548 scroll. */
16549 if (first_unchanged_at_end_row
16550 /* No lines reused because we displayed everything up to the
16551 bottom of the window. */
16552 && it.current_y < it.last_visible_y)
16553 {
16554 dvpos = (it.vpos
16555 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
16556 current_matrix));
16557 dy = it.current_y - first_unchanged_at_end_row->y;
16558 run.current_y = first_unchanged_at_end_row->y;
16559 run.desired_y = run.current_y + dy;
16560 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
16561 }
16562 else
16563 {
16564 delta = delta_bytes = dvpos = dy
16565 = run.current_y = run.desired_y = run.height = 0;
16566 first_unchanged_at_end_row = NULL;
16567 }
16568 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
16569
16570
16571 /* Find the cursor if not already found. We have to decide whether
16572 PT will appear on this window (it sometimes doesn't, but this is
16573 not a very frequent case.) This decision has to be made before
16574 the current matrix is altered. A value of cursor.vpos < 0 means
16575 that PT is either in one of the lines beginning at
16576 first_unchanged_at_end_row or below the window. Don't care for
16577 lines that might be displayed later at the window end; as
16578 mentioned, this is not a frequent case. */
16579 if (w->cursor.vpos < 0)
16580 {
16581 /* Cursor in unchanged rows at the top? */
16582 if (PT < CHARPOS (start_pos)
16583 && last_unchanged_at_beg_row)
16584 {
16585 row = row_containing_pos (w, PT,
16586 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
16587 last_unchanged_at_beg_row + 1, 0);
16588 if (row)
16589 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16590 }
16591
16592 /* Start from first_unchanged_at_end_row looking for PT. */
16593 else if (first_unchanged_at_end_row)
16594 {
16595 row = row_containing_pos (w, PT - delta,
16596 first_unchanged_at_end_row, NULL, 0);
16597 if (row)
16598 set_cursor_from_row (w, row, w->current_matrix, delta,
16599 delta_bytes, dy, dvpos);
16600 }
16601
16602 /* Give up if cursor was not found. */
16603 if (w->cursor.vpos < 0)
16604 {
16605 clear_glyph_matrix (w->desired_matrix);
16606 return -1;
16607 }
16608 }
16609
16610 /* Don't let the cursor end in the scroll margins. */
16611 {
16612 int this_scroll_margin, cursor_height;
16613
16614 this_scroll_margin = max (0, scroll_margin);
16615 this_scroll_margin = min (this_scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16616 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
16617 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
16618
16619 if ((w->cursor.y < this_scroll_margin
16620 && CHARPOS (start) > BEGV)
16621 /* Old redisplay didn't take scroll margin into account at the bottom,
16622 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
16623 || (w->cursor.y + (make_cursor_line_fully_visible_p
16624 ? cursor_height + this_scroll_margin
16625 : 1)) > it.last_visible_y)
16626 {
16627 w->cursor.vpos = -1;
16628 clear_glyph_matrix (w->desired_matrix);
16629 return -1;
16630 }
16631 }
16632
16633 /* Scroll the display. Do it before changing the current matrix so
16634 that xterm.c doesn't get confused about where the cursor glyph is
16635 found. */
16636 if (dy && run.height)
16637 {
16638 update_begin (f);
16639
16640 if (FRAME_WINDOW_P (f))
16641 {
16642 FRAME_RIF (f)->update_window_begin_hook (w);
16643 FRAME_RIF (f)->clear_window_mouse_face (w);
16644 FRAME_RIF (f)->scroll_run_hook (w, &run);
16645 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16646 }
16647 else
16648 {
16649 /* Terminal frame. In this case, dvpos gives the number of
16650 lines to scroll by; dvpos < 0 means scroll up. */
16651 int from_vpos
16652 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
16653 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
16654 int end = (WINDOW_TOP_EDGE_LINE (w)
16655 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
16656 + window_internal_height (w));
16657
16658 #if defined (HAVE_GPM) || defined (MSDOS)
16659 x_clear_window_mouse_face (w);
16660 #endif
16661 /* Perform the operation on the screen. */
16662 if (dvpos > 0)
16663 {
16664 /* Scroll last_unchanged_at_beg_row to the end of the
16665 window down dvpos lines. */
16666 set_terminal_window (f, end);
16667
16668 /* On dumb terminals delete dvpos lines at the end
16669 before inserting dvpos empty lines. */
16670 if (!FRAME_SCROLL_REGION_OK (f))
16671 ins_del_lines (f, end - dvpos, -dvpos);
16672
16673 /* Insert dvpos empty lines in front of
16674 last_unchanged_at_beg_row. */
16675 ins_del_lines (f, from, dvpos);
16676 }
16677 else if (dvpos < 0)
16678 {
16679 /* Scroll up last_unchanged_at_beg_vpos to the end of
16680 the window to last_unchanged_at_beg_vpos - |dvpos|. */
16681 set_terminal_window (f, end);
16682
16683 /* Delete dvpos lines in front of
16684 last_unchanged_at_beg_vpos. ins_del_lines will set
16685 the cursor to the given vpos and emit |dvpos| delete
16686 line sequences. */
16687 ins_del_lines (f, from + dvpos, dvpos);
16688
16689 /* On a dumb terminal insert dvpos empty lines at the
16690 end. */
16691 if (!FRAME_SCROLL_REGION_OK (f))
16692 ins_del_lines (f, end + dvpos, -dvpos);
16693 }
16694
16695 set_terminal_window (f, 0);
16696 }
16697
16698 update_end (f);
16699 }
16700
16701 /* Shift reused rows of the current matrix to the right position.
16702 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
16703 text. */
16704 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16705 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
16706 if (dvpos < 0)
16707 {
16708 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
16709 bottom_vpos, dvpos);
16710 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
16711 bottom_vpos, 0);
16712 }
16713 else if (dvpos > 0)
16714 {
16715 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
16716 bottom_vpos, dvpos);
16717 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
16718 first_unchanged_at_end_vpos + dvpos, 0);
16719 }
16720
16721 /* For frame-based redisplay, make sure that current frame and window
16722 matrix are in sync with respect to glyph memory. */
16723 if (!FRAME_WINDOW_P (f))
16724 sync_frame_with_window_matrix_rows (w);
16725
16726 /* Adjust buffer positions in reused rows. */
16727 if (delta || delta_bytes)
16728 increment_matrix_positions (current_matrix,
16729 first_unchanged_at_end_vpos + dvpos,
16730 bottom_vpos, delta, delta_bytes);
16731
16732 /* Adjust Y positions. */
16733 if (dy)
16734 shift_glyph_matrix (w, current_matrix,
16735 first_unchanged_at_end_vpos + dvpos,
16736 bottom_vpos, dy);
16737
16738 if (first_unchanged_at_end_row)
16739 {
16740 first_unchanged_at_end_row += dvpos;
16741 if (first_unchanged_at_end_row->y >= it.last_visible_y
16742 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
16743 first_unchanged_at_end_row = NULL;
16744 }
16745
16746 /* If scrolling up, there may be some lines to display at the end of
16747 the window. */
16748 last_text_row_at_end = NULL;
16749 if (dy < 0)
16750 {
16751 /* Scrolling up can leave for example a partially visible line
16752 at the end of the window to be redisplayed. */
16753 /* Set last_row to the glyph row in the current matrix where the
16754 window end line is found. It has been moved up or down in
16755 the matrix by dvpos. */
16756 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
16757 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
16758
16759 /* If last_row is the window end line, it should display text. */
16760 xassert (last_row->displays_text_p);
16761
16762 /* If window end line was partially visible before, begin
16763 displaying at that line. Otherwise begin displaying with the
16764 line following it. */
16765 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
16766 {
16767 init_to_row_start (&it, w, last_row);
16768 it.vpos = last_vpos;
16769 it.current_y = last_row->y;
16770 }
16771 else
16772 {
16773 init_to_row_end (&it, w, last_row);
16774 it.vpos = 1 + last_vpos;
16775 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
16776 ++last_row;
16777 }
16778
16779 /* We may start in a continuation line. If so, we have to
16780 get the right continuation_lines_width and current_x. */
16781 it.continuation_lines_width = last_row->continuation_lines_width;
16782 it.hpos = it.current_x = 0;
16783
16784 /* Display the rest of the lines at the window end. */
16785 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
16786 while (it.current_y < it.last_visible_y
16787 && !fonts_changed_p)
16788 {
16789 /* Is it always sure that the display agrees with lines in
16790 the current matrix? I don't think so, so we mark rows
16791 displayed invalid in the current matrix by setting their
16792 enabled_p flag to zero. */
16793 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
16794 if (display_line (&it))
16795 last_text_row_at_end = it.glyph_row - 1;
16796 }
16797 }
16798
16799 /* Update window_end_pos and window_end_vpos. */
16800 if (first_unchanged_at_end_row
16801 && !last_text_row_at_end)
16802 {
16803 /* Window end line if one of the preserved rows from the current
16804 matrix. Set row to the last row displaying text in current
16805 matrix starting at first_unchanged_at_end_row, after
16806 scrolling. */
16807 xassert (first_unchanged_at_end_row->displays_text_p);
16808 row = find_last_row_displaying_text (w->current_matrix, &it,
16809 first_unchanged_at_end_row);
16810 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
16811
16812 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16813 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16814 w->window_end_vpos
16815 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
16816 xassert (w->window_end_bytepos >= 0);
16817 IF_DEBUG (debug_method_add (w, "A"));
16818 }
16819 else if (last_text_row_at_end)
16820 {
16821 w->window_end_pos
16822 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
16823 w->window_end_bytepos
16824 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
16825 w->window_end_vpos
16826 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
16827 xassert (w->window_end_bytepos >= 0);
16828 IF_DEBUG (debug_method_add (w, "B"));
16829 }
16830 else if (last_text_row)
16831 {
16832 /* We have displayed either to the end of the window or at the
16833 end of the window, i.e. the last row with text is to be found
16834 in the desired matrix. */
16835 w->window_end_pos
16836 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16837 w->window_end_bytepos
16838 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16839 w->window_end_vpos
16840 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
16841 xassert (w->window_end_bytepos >= 0);
16842 }
16843 else if (first_unchanged_at_end_row == NULL
16844 && last_text_row == NULL
16845 && last_text_row_at_end == NULL)
16846 {
16847 /* Displayed to end of window, but no line containing text was
16848 displayed. Lines were deleted at the end of the window. */
16849 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
16850 int vpos = XFASTINT (w->window_end_vpos);
16851 struct glyph_row *current_row = current_matrix->rows + vpos;
16852 struct glyph_row *desired_row = desired_matrix->rows + vpos;
16853
16854 for (row = NULL;
16855 row == NULL && vpos >= first_vpos;
16856 --vpos, --current_row, --desired_row)
16857 {
16858 if (desired_row->enabled_p)
16859 {
16860 if (desired_row->displays_text_p)
16861 row = desired_row;
16862 }
16863 else if (current_row->displays_text_p)
16864 row = current_row;
16865 }
16866
16867 xassert (row != NULL);
16868 w->window_end_vpos = make_number (vpos + 1);
16869 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
16870 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
16871 xassert (w->window_end_bytepos >= 0);
16872 IF_DEBUG (debug_method_add (w, "C"));
16873 }
16874 else
16875 abort ();
16876
16877 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
16878 debug_end_vpos = XFASTINT (w->window_end_vpos));
16879
16880 /* Record that display has not been completed. */
16881 w->window_end_valid = Qnil;
16882 w->desired_matrix->no_scrolling_p = 1;
16883 return 3;
16884
16885 #undef GIVE_UP
16886 }
16887
16888
16889 \f
16890 /***********************************************************************
16891 More debugging support
16892 ***********************************************************************/
16893
16894 #if GLYPH_DEBUG
16895
16896 void dump_glyph_row (struct glyph_row *, int, int);
16897 void dump_glyph_matrix (struct glyph_matrix *, int);
16898 void dump_glyph (struct glyph_row *, struct glyph *, int);
16899
16900
16901 /* Dump the contents of glyph matrix MATRIX on stderr.
16902
16903 GLYPHS 0 means don't show glyph contents.
16904 GLYPHS 1 means show glyphs in short form
16905 GLYPHS > 1 means show glyphs in long form. */
16906
16907 void
16908 dump_glyph_matrix (matrix, glyphs)
16909 struct glyph_matrix *matrix;
16910 int glyphs;
16911 {
16912 int i;
16913 for (i = 0; i < matrix->nrows; ++i)
16914 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
16915 }
16916
16917
16918 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
16919 the glyph row and area where the glyph comes from. */
16920
16921 void
16922 dump_glyph (row, glyph, area)
16923 struct glyph_row *row;
16924 struct glyph *glyph;
16925 int area;
16926 {
16927 if (glyph->type == CHAR_GLYPH)
16928 {
16929 fprintf (stderr,
16930 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16931 glyph - row->glyphs[TEXT_AREA],
16932 'C',
16933 glyph->charpos,
16934 (BUFFERP (glyph->object)
16935 ? 'B'
16936 : (STRINGP (glyph->object)
16937 ? 'S'
16938 : '-')),
16939 glyph->pixel_width,
16940 glyph->u.ch,
16941 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
16942 ? glyph->u.ch
16943 : '.'),
16944 glyph->face_id,
16945 glyph->left_box_line_p,
16946 glyph->right_box_line_p);
16947 }
16948 else if (glyph->type == STRETCH_GLYPH)
16949 {
16950 fprintf (stderr,
16951 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16952 glyph - row->glyphs[TEXT_AREA],
16953 'S',
16954 glyph->charpos,
16955 (BUFFERP (glyph->object)
16956 ? 'B'
16957 : (STRINGP (glyph->object)
16958 ? 'S'
16959 : '-')),
16960 glyph->pixel_width,
16961 0,
16962 '.',
16963 glyph->face_id,
16964 glyph->left_box_line_p,
16965 glyph->right_box_line_p);
16966 }
16967 else if (glyph->type == IMAGE_GLYPH)
16968 {
16969 fprintf (stderr,
16970 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
16971 glyph - row->glyphs[TEXT_AREA],
16972 'I',
16973 glyph->charpos,
16974 (BUFFERP (glyph->object)
16975 ? 'B'
16976 : (STRINGP (glyph->object)
16977 ? 'S'
16978 : '-')),
16979 glyph->pixel_width,
16980 glyph->u.img_id,
16981 '.',
16982 glyph->face_id,
16983 glyph->left_box_line_p,
16984 glyph->right_box_line_p);
16985 }
16986 else if (glyph->type == COMPOSITE_GLYPH)
16987 {
16988 fprintf (stderr,
16989 " %5d %4c %6d %c %3d 0x%05x",
16990 glyph - row->glyphs[TEXT_AREA],
16991 '+',
16992 glyph->charpos,
16993 (BUFFERP (glyph->object)
16994 ? 'B'
16995 : (STRINGP (glyph->object)
16996 ? 'S'
16997 : '-')),
16998 glyph->pixel_width,
16999 glyph->u.cmp.id);
17000 if (glyph->u.cmp.automatic)
17001 fprintf (stderr,
17002 "[%d-%d]",
17003 glyph->slice.cmp.from, glyph->slice.cmp.to);
17004 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17005 glyph->face_id,
17006 glyph->left_box_line_p,
17007 glyph->right_box_line_p);
17008 }
17009 }
17010
17011
17012 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17013 GLYPHS 0 means don't show glyph contents.
17014 GLYPHS 1 means show glyphs in short form
17015 GLYPHS > 1 means show glyphs in long form. */
17016
17017 void
17018 dump_glyph_row (row, vpos, glyphs)
17019 struct glyph_row *row;
17020 int vpos, glyphs;
17021 {
17022 if (glyphs != 1)
17023 {
17024 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17025 fprintf (stderr, "======================================================================\n");
17026
17027 fprintf (stderr, "%3d %5d %5d %4d %1.1d%1.1d%1.1d%1.1d\
17028 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17029 vpos,
17030 MATRIX_ROW_START_CHARPOS (row),
17031 MATRIX_ROW_END_CHARPOS (row),
17032 row->used[TEXT_AREA],
17033 row->contains_overlapping_glyphs_p,
17034 row->enabled_p,
17035 row->truncated_on_left_p,
17036 row->truncated_on_right_p,
17037 row->continued_p,
17038 MATRIX_ROW_CONTINUATION_LINE_P (row),
17039 row->displays_text_p,
17040 row->ends_at_zv_p,
17041 row->fill_line_p,
17042 row->ends_in_middle_of_char_p,
17043 row->starts_in_middle_of_char_p,
17044 row->mouse_face_p,
17045 row->x,
17046 row->y,
17047 row->pixel_width,
17048 row->height,
17049 row->visible_height,
17050 row->ascent,
17051 row->phys_ascent);
17052 fprintf (stderr, "%9d %5d\t%5d\n", row->start.overlay_string_index,
17053 row->end.overlay_string_index,
17054 row->continuation_lines_width);
17055 fprintf (stderr, "%9d %5d\n",
17056 CHARPOS (row->start.string_pos),
17057 CHARPOS (row->end.string_pos));
17058 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17059 row->end.dpvec_index);
17060 }
17061
17062 if (glyphs > 1)
17063 {
17064 int area;
17065
17066 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17067 {
17068 struct glyph *glyph = row->glyphs[area];
17069 struct glyph *glyph_end = glyph + row->used[area];
17070
17071 /* Glyph for a line end in text. */
17072 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17073 ++glyph_end;
17074
17075 if (glyph < glyph_end)
17076 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17077
17078 for (; glyph < glyph_end; ++glyph)
17079 dump_glyph (row, glyph, area);
17080 }
17081 }
17082 else if (glyphs == 1)
17083 {
17084 int area;
17085
17086 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17087 {
17088 char *s = (char *) alloca (row->used[area] + 1);
17089 int i;
17090
17091 for (i = 0; i < row->used[area]; ++i)
17092 {
17093 struct glyph *glyph = row->glyphs[area] + i;
17094 if (glyph->type == CHAR_GLYPH
17095 && glyph->u.ch < 0x80
17096 && glyph->u.ch >= ' ')
17097 s[i] = glyph->u.ch;
17098 else
17099 s[i] = '.';
17100 }
17101
17102 s[i] = '\0';
17103 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17104 }
17105 }
17106 }
17107
17108
17109 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17110 Sdump_glyph_matrix, 0, 1, "p",
17111 doc: /* Dump the current matrix of the selected window to stderr.
17112 Shows contents of glyph row structures. With non-nil
17113 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17114 glyphs in short form, otherwise show glyphs in long form. */)
17115 (Lisp_Object glyphs)
17116 {
17117 struct window *w = XWINDOW (selected_window);
17118 struct buffer *buffer = XBUFFER (w->buffer);
17119
17120 fprintf (stderr, "PT = %d, BEGV = %d. ZV = %d\n",
17121 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17122 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17123 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17124 fprintf (stderr, "=============================================\n");
17125 dump_glyph_matrix (w->current_matrix,
17126 NILP (glyphs) ? 0 : XINT (glyphs));
17127 return Qnil;
17128 }
17129
17130
17131 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17132 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17133 (void)
17134 {
17135 struct frame *f = XFRAME (selected_frame);
17136 dump_glyph_matrix (f->current_matrix, 1);
17137 return Qnil;
17138 }
17139
17140
17141 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17142 doc: /* Dump glyph row ROW to stderr.
17143 GLYPH 0 means don't dump glyphs.
17144 GLYPH 1 means dump glyphs in short form.
17145 GLYPH > 1 or omitted means dump glyphs in long form. */)
17146 (Lisp_Object row, Lisp_Object glyphs)
17147 {
17148 struct glyph_matrix *matrix;
17149 int vpos;
17150
17151 CHECK_NUMBER (row);
17152 matrix = XWINDOW (selected_window)->current_matrix;
17153 vpos = XINT (row);
17154 if (vpos >= 0 && vpos < matrix->nrows)
17155 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17156 vpos,
17157 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17158 return Qnil;
17159 }
17160
17161
17162 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17163 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17164 GLYPH 0 means don't dump glyphs.
17165 GLYPH 1 means dump glyphs in short form.
17166 GLYPH > 1 or omitted means dump glyphs in long form. */)
17167 (Lisp_Object row, Lisp_Object glyphs)
17168 {
17169 struct frame *sf = SELECTED_FRAME ();
17170 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17171 int vpos;
17172
17173 CHECK_NUMBER (row);
17174 vpos = XINT (row);
17175 if (vpos >= 0 && vpos < m->nrows)
17176 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17177 INTEGERP (glyphs) ? XINT (glyphs) : 2);
17178 return Qnil;
17179 }
17180
17181
17182 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
17183 doc: /* Toggle tracing of redisplay.
17184 With ARG, turn tracing on if and only if ARG is positive. */)
17185 (Lisp_Object arg)
17186 {
17187 if (NILP (arg))
17188 trace_redisplay_p = !trace_redisplay_p;
17189 else
17190 {
17191 arg = Fprefix_numeric_value (arg);
17192 trace_redisplay_p = XINT (arg) > 0;
17193 }
17194
17195 return Qnil;
17196 }
17197
17198
17199 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
17200 doc: /* Like `format', but print result to stderr.
17201 usage: (trace-to-stderr STRING &rest OBJECTS) */)
17202 (size_t nargs, Lisp_Object *args)
17203 {
17204 Lisp_Object s = Fformat (nargs, args);
17205 fprintf (stderr, "%s", SDATA (s));
17206 return Qnil;
17207 }
17208
17209 #endif /* GLYPH_DEBUG */
17210
17211
17212 \f
17213 /***********************************************************************
17214 Building Desired Matrix Rows
17215 ***********************************************************************/
17216
17217 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
17218 Used for non-window-redisplay windows, and for windows w/o left fringe. */
17219
17220 static struct glyph_row *
17221 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
17222 {
17223 struct frame *f = XFRAME (WINDOW_FRAME (w));
17224 struct buffer *buffer = XBUFFER (w->buffer);
17225 struct buffer *old = current_buffer;
17226 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
17227 int arrow_len = SCHARS (overlay_arrow_string);
17228 const unsigned char *arrow_end = arrow_string + arrow_len;
17229 const unsigned char *p;
17230 struct it it;
17231 int multibyte_p;
17232 int n_glyphs_before;
17233
17234 set_buffer_temp (buffer);
17235 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
17236 it.glyph_row->used[TEXT_AREA] = 0;
17237 SET_TEXT_POS (it.position, 0, 0);
17238
17239 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
17240 p = arrow_string;
17241 while (p < arrow_end)
17242 {
17243 Lisp_Object face, ilisp;
17244
17245 /* Get the next character. */
17246 if (multibyte_p)
17247 it.c = it.char_to_display = string_char_and_length (p, &it.len);
17248 else
17249 {
17250 it.c = it.char_to_display = *p, it.len = 1;
17251 if (! ASCII_CHAR_P (it.c))
17252 it.char_to_display = BYTE8_TO_CHAR (it.c);
17253 }
17254 p += it.len;
17255
17256 /* Get its face. */
17257 ilisp = make_number (p - arrow_string);
17258 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
17259 it.face_id = compute_char_face (f, it.char_to_display, face);
17260
17261 /* Compute its width, get its glyphs. */
17262 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
17263 SET_TEXT_POS (it.position, -1, -1);
17264 PRODUCE_GLYPHS (&it);
17265
17266 /* If this character doesn't fit any more in the line, we have
17267 to remove some glyphs. */
17268 if (it.current_x > it.last_visible_x)
17269 {
17270 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
17271 break;
17272 }
17273 }
17274
17275 set_buffer_temp (old);
17276 return it.glyph_row;
17277 }
17278
17279
17280 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
17281 glyphs are only inserted for terminal frames since we can't really
17282 win with truncation glyphs when partially visible glyphs are
17283 involved. Which glyphs to insert is determined by
17284 produce_special_glyphs. */
17285
17286 static void
17287 insert_left_trunc_glyphs (struct it *it)
17288 {
17289 struct it truncate_it;
17290 struct glyph *from, *end, *to, *toend;
17291
17292 xassert (!FRAME_WINDOW_P (it->f));
17293
17294 /* Get the truncation glyphs. */
17295 truncate_it = *it;
17296 truncate_it.current_x = 0;
17297 truncate_it.face_id = DEFAULT_FACE_ID;
17298 truncate_it.glyph_row = &scratch_glyph_row;
17299 truncate_it.glyph_row->used[TEXT_AREA] = 0;
17300 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
17301 truncate_it.object = make_number (0);
17302 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
17303
17304 /* Overwrite glyphs from IT with truncation glyphs. */
17305 if (!it->glyph_row->reversed_p)
17306 {
17307 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17308 end = from + truncate_it.glyph_row->used[TEXT_AREA];
17309 to = it->glyph_row->glyphs[TEXT_AREA];
17310 toend = to + it->glyph_row->used[TEXT_AREA];
17311
17312 while (from < end)
17313 *to++ = *from++;
17314
17315 /* There may be padding glyphs left over. Overwrite them too. */
17316 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
17317 {
17318 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17319 while (from < end)
17320 *to++ = *from++;
17321 }
17322
17323 if (to > toend)
17324 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
17325 }
17326 else
17327 {
17328 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
17329 that back to front. */
17330 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
17331 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17332 toend = it->glyph_row->glyphs[TEXT_AREA];
17333 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
17334
17335 while (from >= end && to >= toend)
17336 *to-- = *from--;
17337 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
17338 {
17339 from =
17340 truncate_it.glyph_row->glyphs[TEXT_AREA]
17341 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17342 while (from >= end && to >= toend)
17343 *to-- = *from--;
17344 }
17345 if (from >= end)
17346 {
17347 /* Need to free some room before prepending additional
17348 glyphs. */
17349 int move_by = from - end + 1;
17350 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
17351 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
17352
17353 for ( ; g >= g0; g--)
17354 g[move_by] = *g;
17355 while (from >= end)
17356 *to-- = *from--;
17357 it->glyph_row->used[TEXT_AREA] += move_by;
17358 }
17359 }
17360 }
17361
17362
17363 /* Compute the pixel height and width of IT->glyph_row.
17364
17365 Most of the time, ascent and height of a display line will be equal
17366 to the max_ascent and max_height values of the display iterator
17367 structure. This is not the case if
17368
17369 1. We hit ZV without displaying anything. In this case, max_ascent
17370 and max_height will be zero.
17371
17372 2. We have some glyphs that don't contribute to the line height.
17373 (The glyph row flag contributes_to_line_height_p is for future
17374 pixmap extensions).
17375
17376 The first case is easily covered by using default values because in
17377 these cases, the line height does not really matter, except that it
17378 must not be zero. */
17379
17380 static void
17381 compute_line_metrics (struct it *it)
17382 {
17383 struct glyph_row *row = it->glyph_row;
17384
17385 if (FRAME_WINDOW_P (it->f))
17386 {
17387 int i, min_y, max_y;
17388
17389 /* The line may consist of one space only, that was added to
17390 place the cursor on it. If so, the row's height hasn't been
17391 computed yet. */
17392 if (row->height == 0)
17393 {
17394 if (it->max_ascent + it->max_descent == 0)
17395 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
17396 row->ascent = it->max_ascent;
17397 row->height = it->max_ascent + it->max_descent;
17398 row->phys_ascent = it->max_phys_ascent;
17399 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
17400 row->extra_line_spacing = it->max_extra_line_spacing;
17401 }
17402
17403 /* Compute the width of this line. */
17404 row->pixel_width = row->x;
17405 for (i = 0; i < row->used[TEXT_AREA]; ++i)
17406 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
17407
17408 xassert (row->pixel_width >= 0);
17409 xassert (row->ascent >= 0 && row->height > 0);
17410
17411 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
17412 || MATRIX_ROW_OVERLAPS_PRED_P (row));
17413
17414 /* If first line's physical ascent is larger than its logical
17415 ascent, use the physical ascent, and make the row taller.
17416 This makes accented characters fully visible. */
17417 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
17418 && row->phys_ascent > row->ascent)
17419 {
17420 row->height += row->phys_ascent - row->ascent;
17421 row->ascent = row->phys_ascent;
17422 }
17423
17424 /* Compute how much of the line is visible. */
17425 row->visible_height = row->height;
17426
17427 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
17428 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
17429
17430 if (row->y < min_y)
17431 row->visible_height -= min_y - row->y;
17432 if (row->y + row->height > max_y)
17433 row->visible_height -= row->y + row->height - max_y;
17434 }
17435 else
17436 {
17437 row->pixel_width = row->used[TEXT_AREA];
17438 if (row->continued_p)
17439 row->pixel_width -= it->continuation_pixel_width;
17440 else if (row->truncated_on_right_p)
17441 row->pixel_width -= it->truncation_pixel_width;
17442 row->ascent = row->phys_ascent = 0;
17443 row->height = row->phys_height = row->visible_height = 1;
17444 row->extra_line_spacing = 0;
17445 }
17446
17447 /* Compute a hash code for this row. */
17448 {
17449 int area, i;
17450 row->hash = 0;
17451 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17452 for (i = 0; i < row->used[area]; ++i)
17453 row->hash = ((((row->hash << 4) + (row->hash >> 24)) & 0x0fffffff)
17454 + row->glyphs[area][i].u.val
17455 + row->glyphs[area][i].face_id
17456 + row->glyphs[area][i].padding_p
17457 + (row->glyphs[area][i].type << 2));
17458 }
17459
17460 it->max_ascent = it->max_descent = 0;
17461 it->max_phys_ascent = it->max_phys_descent = 0;
17462 }
17463
17464
17465 /* Append one space to the glyph row of iterator IT if doing a
17466 window-based redisplay. The space has the same face as
17467 IT->face_id. Value is non-zero if a space was added.
17468
17469 This function is called to make sure that there is always one glyph
17470 at the end of a glyph row that the cursor can be set on under
17471 window-systems. (If there weren't such a glyph we would not know
17472 how wide and tall a box cursor should be displayed).
17473
17474 At the same time this space let's a nicely handle clearing to the
17475 end of the line if the row ends in italic text. */
17476
17477 static int
17478 append_space_for_newline (struct it *it, int default_face_p)
17479 {
17480 if (FRAME_WINDOW_P (it->f))
17481 {
17482 int n = it->glyph_row->used[TEXT_AREA];
17483
17484 if (it->glyph_row->glyphs[TEXT_AREA] + n
17485 < it->glyph_row->glyphs[1 + TEXT_AREA])
17486 {
17487 /* Save some values that must not be changed.
17488 Must save IT->c and IT->len because otherwise
17489 ITERATOR_AT_END_P wouldn't work anymore after
17490 append_space_for_newline has been called. */
17491 enum display_element_type saved_what = it->what;
17492 int saved_c = it->c, saved_len = it->len;
17493 int saved_char_to_display = it->char_to_display;
17494 int saved_x = it->current_x;
17495 int saved_face_id = it->face_id;
17496 struct text_pos saved_pos;
17497 Lisp_Object saved_object;
17498 struct face *face;
17499
17500 saved_object = it->object;
17501 saved_pos = it->position;
17502
17503 it->what = IT_CHARACTER;
17504 memset (&it->position, 0, sizeof it->position);
17505 it->object = make_number (0);
17506 it->c = it->char_to_display = ' ';
17507 it->len = 1;
17508
17509 if (default_face_p)
17510 it->face_id = DEFAULT_FACE_ID;
17511 else if (it->face_before_selective_p)
17512 it->face_id = it->saved_face_id;
17513 face = FACE_FROM_ID (it->f, it->face_id);
17514 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
17515
17516 PRODUCE_GLYPHS (it);
17517
17518 it->override_ascent = -1;
17519 it->constrain_row_ascent_descent_p = 0;
17520 it->current_x = saved_x;
17521 it->object = saved_object;
17522 it->position = saved_pos;
17523 it->what = saved_what;
17524 it->face_id = saved_face_id;
17525 it->len = saved_len;
17526 it->c = saved_c;
17527 it->char_to_display = saved_char_to_display;
17528 return 1;
17529 }
17530 }
17531
17532 return 0;
17533 }
17534
17535
17536 /* Extend the face of the last glyph in the text area of IT->glyph_row
17537 to the end of the display line. Called from display_line. If the
17538 glyph row is empty, add a space glyph to it so that we know the
17539 face to draw. Set the glyph row flag fill_line_p. If the glyph
17540 row is R2L, prepend a stretch glyph to cover the empty space to the
17541 left of the leftmost glyph. */
17542
17543 static void
17544 extend_face_to_end_of_line (struct it *it)
17545 {
17546 struct face *face;
17547 struct frame *f = it->f;
17548
17549 /* If line is already filled, do nothing. Non window-system frames
17550 get a grace of one more ``pixel'' because their characters are
17551 1-``pixel'' wide, so they hit the equality too early. This grace
17552 is needed only for R2L rows that are not continued, to produce
17553 one extra blank where we could display the cursor. */
17554 if (it->current_x >= it->last_visible_x
17555 + (!FRAME_WINDOW_P (f)
17556 && it->glyph_row->reversed_p
17557 && !it->glyph_row->continued_p))
17558 return;
17559
17560 /* Face extension extends the background and box of IT->face_id
17561 to the end of the line. If the background equals the background
17562 of the frame, we don't have to do anything. */
17563 if (it->face_before_selective_p)
17564 face = FACE_FROM_ID (f, it->saved_face_id);
17565 else
17566 face = FACE_FROM_ID (f, it->face_id);
17567
17568 if (FRAME_WINDOW_P (f)
17569 && it->glyph_row->displays_text_p
17570 && face->box == FACE_NO_BOX
17571 && face->background == FRAME_BACKGROUND_PIXEL (f)
17572 && !face->stipple
17573 && !it->glyph_row->reversed_p)
17574 return;
17575
17576 /* Set the glyph row flag indicating that the face of the last glyph
17577 in the text area has to be drawn to the end of the text area. */
17578 it->glyph_row->fill_line_p = 1;
17579
17580 /* If current character of IT is not ASCII, make sure we have the
17581 ASCII face. This will be automatically undone the next time
17582 get_next_display_element returns a multibyte character. Note
17583 that the character will always be single byte in unibyte
17584 text. */
17585 if (!ASCII_CHAR_P (it->c))
17586 {
17587 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
17588 }
17589
17590 if (FRAME_WINDOW_P (f))
17591 {
17592 /* If the row is empty, add a space with the current face of IT,
17593 so that we know which face to draw. */
17594 if (it->glyph_row->used[TEXT_AREA] == 0)
17595 {
17596 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
17597 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
17598 it->glyph_row->used[TEXT_AREA] = 1;
17599 }
17600 #ifdef HAVE_WINDOW_SYSTEM
17601 if (it->glyph_row->reversed_p)
17602 {
17603 /* Prepend a stretch glyph to the row, such that the
17604 rightmost glyph will be drawn flushed all the way to the
17605 right margin of the window. The stretch glyph that will
17606 occupy the empty space, if any, to the left of the
17607 glyphs. */
17608 struct font *font = face->font ? face->font : FRAME_FONT (f);
17609 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
17610 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
17611 struct glyph *g;
17612 int row_width, stretch_ascent, stretch_width;
17613 struct text_pos saved_pos;
17614 int saved_face_id, saved_avoid_cursor;
17615
17616 for (row_width = 0, g = row_start; g < row_end; g++)
17617 row_width += g->pixel_width;
17618 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
17619 if (stretch_width > 0)
17620 {
17621 stretch_ascent =
17622 (((it->ascent + it->descent)
17623 * FONT_BASE (font)) / FONT_HEIGHT (font));
17624 saved_pos = it->position;
17625 memset (&it->position, 0, sizeof it->position);
17626 saved_avoid_cursor = it->avoid_cursor_p;
17627 it->avoid_cursor_p = 1;
17628 saved_face_id = it->face_id;
17629 /* The last row's stretch glyph should get the default
17630 face, to avoid painting the rest of the window with
17631 the region face, if the region ends at ZV. */
17632 if (it->glyph_row->ends_at_zv_p)
17633 it->face_id = DEFAULT_FACE_ID;
17634 else
17635 it->face_id = face->id;
17636 append_stretch_glyph (it, make_number (0), stretch_width,
17637 it->ascent + it->descent, stretch_ascent);
17638 it->position = saved_pos;
17639 it->avoid_cursor_p = saved_avoid_cursor;
17640 it->face_id = saved_face_id;
17641 }
17642 }
17643 #endif /* HAVE_WINDOW_SYSTEM */
17644 }
17645 else
17646 {
17647 /* Save some values that must not be changed. */
17648 int saved_x = it->current_x;
17649 struct text_pos saved_pos;
17650 Lisp_Object saved_object;
17651 enum display_element_type saved_what = it->what;
17652 int saved_face_id = it->face_id;
17653
17654 saved_object = it->object;
17655 saved_pos = it->position;
17656
17657 it->what = IT_CHARACTER;
17658 memset (&it->position, 0, sizeof it->position);
17659 it->object = make_number (0);
17660 it->c = it->char_to_display = ' ';
17661 it->len = 1;
17662 /* The last row's blank glyphs should get the default face, to
17663 avoid painting the rest of the window with the region face,
17664 if the region ends at ZV. */
17665 if (it->glyph_row->ends_at_zv_p)
17666 it->face_id = DEFAULT_FACE_ID;
17667 else
17668 it->face_id = face->id;
17669
17670 PRODUCE_GLYPHS (it);
17671
17672 while (it->current_x <= it->last_visible_x)
17673 PRODUCE_GLYPHS (it);
17674
17675 /* Don't count these blanks really. It would let us insert a left
17676 truncation glyph below and make us set the cursor on them, maybe. */
17677 it->current_x = saved_x;
17678 it->object = saved_object;
17679 it->position = saved_pos;
17680 it->what = saved_what;
17681 it->face_id = saved_face_id;
17682 }
17683 }
17684
17685
17686 /* Value is non-zero if text starting at CHARPOS in current_buffer is
17687 trailing whitespace. */
17688
17689 static int
17690 trailing_whitespace_p (EMACS_INT charpos)
17691 {
17692 EMACS_INT bytepos = CHAR_TO_BYTE (charpos);
17693 int c = 0;
17694
17695 while (bytepos < ZV_BYTE
17696 && (c = FETCH_CHAR (bytepos),
17697 c == ' ' || c == '\t'))
17698 ++bytepos;
17699
17700 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
17701 {
17702 if (bytepos != PT_BYTE)
17703 return 1;
17704 }
17705 return 0;
17706 }
17707
17708
17709 /* Highlight trailing whitespace, if any, in ROW. */
17710
17711 static void
17712 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
17713 {
17714 int used = row->used[TEXT_AREA];
17715
17716 if (used)
17717 {
17718 struct glyph *start = row->glyphs[TEXT_AREA];
17719 struct glyph *glyph = start + used - 1;
17720
17721 if (row->reversed_p)
17722 {
17723 /* Right-to-left rows need to be processed in the opposite
17724 direction, so swap the edge pointers. */
17725 glyph = start;
17726 start = row->glyphs[TEXT_AREA] + used - 1;
17727 }
17728
17729 /* Skip over glyphs inserted to display the cursor at the
17730 end of a line, for extending the face of the last glyph
17731 to the end of the line on terminals, and for truncation
17732 and continuation glyphs. */
17733 if (!row->reversed_p)
17734 {
17735 while (glyph >= start
17736 && glyph->type == CHAR_GLYPH
17737 && INTEGERP (glyph->object))
17738 --glyph;
17739 }
17740 else
17741 {
17742 while (glyph <= start
17743 && glyph->type == CHAR_GLYPH
17744 && INTEGERP (glyph->object))
17745 ++glyph;
17746 }
17747
17748 /* If last glyph is a space or stretch, and it's trailing
17749 whitespace, set the face of all trailing whitespace glyphs in
17750 IT->glyph_row to `trailing-whitespace'. */
17751 if ((row->reversed_p ? glyph <= start : glyph >= start)
17752 && BUFFERP (glyph->object)
17753 && (glyph->type == STRETCH_GLYPH
17754 || (glyph->type == CHAR_GLYPH
17755 && glyph->u.ch == ' '))
17756 && trailing_whitespace_p (glyph->charpos))
17757 {
17758 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
17759 if (face_id < 0)
17760 return;
17761
17762 if (!row->reversed_p)
17763 {
17764 while (glyph >= start
17765 && BUFFERP (glyph->object)
17766 && (glyph->type == STRETCH_GLYPH
17767 || (glyph->type == CHAR_GLYPH
17768 && glyph->u.ch == ' ')))
17769 (glyph--)->face_id = face_id;
17770 }
17771 else
17772 {
17773 while (glyph <= start
17774 && BUFFERP (glyph->object)
17775 && (glyph->type == STRETCH_GLYPH
17776 || (glyph->type == CHAR_GLYPH
17777 && glyph->u.ch == ' ')))
17778 (glyph++)->face_id = face_id;
17779 }
17780 }
17781 }
17782 }
17783
17784
17785 /* Value is non-zero if glyph row ROW should be
17786 used to hold the cursor. */
17787
17788 static int
17789 cursor_row_p (struct glyph_row *row)
17790 {
17791 int result = 1;
17792
17793 if (PT == CHARPOS (row->end.pos))
17794 {
17795 /* Suppose the row ends on a string.
17796 Unless the row is continued, that means it ends on a newline
17797 in the string. If it's anything other than a display string
17798 (e.g. a before-string from an overlay), we don't want the
17799 cursor there. (This heuristic seems to give the optimal
17800 behavior for the various types of multi-line strings.) */
17801 if (CHARPOS (row->end.string_pos) >= 0)
17802 {
17803 if (row->continued_p)
17804 result = 1;
17805 else
17806 {
17807 /* Check for `display' property. */
17808 struct glyph *beg = row->glyphs[TEXT_AREA];
17809 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
17810 struct glyph *glyph;
17811
17812 result = 0;
17813 for (glyph = end; glyph >= beg; --glyph)
17814 if (STRINGP (glyph->object))
17815 {
17816 Lisp_Object prop
17817 = Fget_char_property (make_number (PT),
17818 Qdisplay, Qnil);
17819 result =
17820 (!NILP (prop)
17821 && display_prop_string_p (prop, glyph->object));
17822 break;
17823 }
17824 }
17825 }
17826 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
17827 {
17828 /* If the row ends in middle of a real character,
17829 and the line is continued, we want the cursor here.
17830 That's because CHARPOS (ROW->end.pos) would equal
17831 PT if PT is before the character. */
17832 if (!row->ends_in_ellipsis_p)
17833 result = row->continued_p;
17834 else
17835 /* If the row ends in an ellipsis, then
17836 CHARPOS (ROW->end.pos) will equal point after the
17837 invisible text. We want that position to be displayed
17838 after the ellipsis. */
17839 result = 0;
17840 }
17841 /* If the row ends at ZV, display the cursor at the end of that
17842 row instead of at the start of the row below. */
17843 else if (row->ends_at_zv_p)
17844 result = 1;
17845 else
17846 result = 0;
17847 }
17848
17849 return result;
17850 }
17851
17852 \f
17853
17854 /* Push the display property PROP so that it will be rendered at the
17855 current position in IT. Return 1 if PROP was successfully pushed,
17856 0 otherwise. */
17857
17858 static int
17859 push_display_prop (struct it *it, Lisp_Object prop)
17860 {
17861 xassert (it->method == GET_FROM_BUFFER);
17862
17863 push_it (it, NULL);
17864
17865 if (STRINGP (prop))
17866 {
17867 if (SCHARS (prop) == 0)
17868 {
17869 pop_it (it);
17870 return 0;
17871 }
17872
17873 it->string = prop;
17874 it->multibyte_p = STRING_MULTIBYTE (it->string);
17875 it->current.overlay_string_index = -1;
17876 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
17877 it->end_charpos = it->string_nchars = SCHARS (it->string);
17878 it->method = GET_FROM_STRING;
17879 it->stop_charpos = 0;
17880 it->prev_stop = 0;
17881 it->base_level_stop = 0;
17882 it->string_from_display_prop_p = 1;
17883 it->from_disp_prop_p = 1;
17884
17885 /* Force paragraph direction to be that of the parent
17886 buffer. */
17887 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
17888 it->paragraph_embedding = it->bidi_it.paragraph_dir;
17889 else
17890 it->paragraph_embedding = L2R;
17891
17892 /* Set up the bidi iterator for this display string. */
17893 if (it->bidi_p)
17894 {
17895 it->bidi_it.string.lstring = it->string;
17896 it->bidi_it.string.s = NULL;
17897 it->bidi_it.string.schars = it->end_charpos;
17898 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
17899 it->bidi_it.string.from_disp_str = 1;
17900 it->bidi_it.string.unibyte = !it->multibyte_p;
17901 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
17902 }
17903 }
17904 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
17905 {
17906 it->method = GET_FROM_STRETCH;
17907 it->object = prop;
17908 }
17909 #ifdef HAVE_WINDOW_SYSTEM
17910 else if (IMAGEP (prop))
17911 {
17912 it->what = IT_IMAGE;
17913 it->image_id = lookup_image (it->f, prop);
17914 it->method = GET_FROM_IMAGE;
17915 }
17916 #endif /* HAVE_WINDOW_SYSTEM */
17917 else
17918 {
17919 pop_it (it); /* bogus display property, give up */
17920 return 0;
17921 }
17922
17923 return 1;
17924 }
17925
17926 /* Return the character-property PROP at the current position in IT. */
17927
17928 static Lisp_Object
17929 get_it_property (struct it *it, Lisp_Object prop)
17930 {
17931 Lisp_Object position;
17932
17933 if (STRINGP (it->object))
17934 position = make_number (IT_STRING_CHARPOS (*it));
17935 else if (BUFFERP (it->object))
17936 position = make_number (IT_CHARPOS (*it));
17937 else
17938 return Qnil;
17939
17940 return Fget_char_property (position, prop, it->object);
17941 }
17942
17943 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
17944
17945 static void
17946 handle_line_prefix (struct it *it)
17947 {
17948 Lisp_Object prefix;
17949
17950 if (it->continuation_lines_width > 0)
17951 {
17952 prefix = get_it_property (it, Qwrap_prefix);
17953 if (NILP (prefix))
17954 prefix = Vwrap_prefix;
17955 }
17956 else
17957 {
17958 prefix = get_it_property (it, Qline_prefix);
17959 if (NILP (prefix))
17960 prefix = Vline_prefix;
17961 }
17962 if (! NILP (prefix) && push_display_prop (it, prefix))
17963 {
17964 /* If the prefix is wider than the window, and we try to wrap
17965 it, it would acquire its own wrap prefix, and so on till the
17966 iterator stack overflows. So, don't wrap the prefix. */
17967 it->line_wrap = TRUNCATE;
17968 it->avoid_cursor_p = 1;
17969 }
17970 }
17971
17972 \f
17973
17974 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
17975 only for R2L lines from display_line and display_string, when they
17976 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
17977 the line/string needs to be continued on the next glyph row. */
17978 static void
17979 unproduce_glyphs (struct it *it, int n)
17980 {
17981 struct glyph *glyph, *end;
17982
17983 xassert (it->glyph_row);
17984 xassert (it->glyph_row->reversed_p);
17985 xassert (it->area == TEXT_AREA);
17986 xassert (n <= it->glyph_row->used[TEXT_AREA]);
17987
17988 if (n > it->glyph_row->used[TEXT_AREA])
17989 n = it->glyph_row->used[TEXT_AREA];
17990 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
17991 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
17992 for ( ; glyph < end; glyph++)
17993 glyph[-n] = *glyph;
17994 }
17995
17996 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
17997 and ROW->maxpos. */
17998 static void
17999 find_row_edges (struct it *it, struct glyph_row *row,
18000 EMACS_INT min_pos, EMACS_INT min_bpos,
18001 EMACS_INT max_pos, EMACS_INT max_bpos)
18002 {
18003 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18004 lines' rows is implemented for bidi-reordered rows. */
18005
18006 /* ROW->minpos is the value of min_pos, the minimal buffer position
18007 we have in ROW, or ROW->start.pos if that is smaller. */
18008 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18009 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18010 else
18011 /* We didn't find buffer positions smaller than ROW->start, or
18012 didn't find _any_ valid buffer positions in any of the glyphs,
18013 so we must trust the iterator's computed positions. */
18014 row->minpos = row->start.pos;
18015 if (max_pos <= 0)
18016 {
18017 max_pos = CHARPOS (it->current.pos);
18018 max_bpos = BYTEPOS (it->current.pos);
18019 }
18020
18021 /* Here are the various use-cases for ending the row, and the
18022 corresponding values for ROW->maxpos:
18023
18024 Line ends in a newline from buffer eol_pos + 1
18025 Line is continued from buffer max_pos + 1
18026 Line is truncated on right it->current.pos
18027 Line ends in a newline from string max_pos
18028 Line is continued from string max_pos
18029 Line is continued from display vector max_pos
18030 Line is entirely from a string min_pos == max_pos
18031 Line is entirely from a display vector min_pos == max_pos
18032 Line that ends at ZV ZV
18033
18034 If you discover other use-cases, please add them here as
18035 appropriate. */
18036 if (row->ends_at_zv_p)
18037 row->maxpos = it->current.pos;
18038 else if (row->used[TEXT_AREA])
18039 {
18040 if (row->ends_in_newline_from_string_p)
18041 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18042 else if (CHARPOS (it->eol_pos) > 0)
18043 SET_TEXT_POS (row->maxpos,
18044 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
18045 else if (row->continued_p)
18046 {
18047 /* If max_pos is different from IT's current position, it
18048 means IT->method does not belong to the display element
18049 at max_pos. However, it also means that the display
18050 element at max_pos was displayed in its entirety on this
18051 line, which is equivalent to saying that the next line
18052 starts at the next buffer position. */
18053 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
18054 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18055 else
18056 {
18057 INC_BOTH (max_pos, max_bpos);
18058 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18059 }
18060 }
18061 else if (row->truncated_on_right_p)
18062 /* display_line already called reseat_at_next_visible_line_start,
18063 which puts the iterator at the beginning of the next line, in
18064 the logical order. */
18065 row->maxpos = it->current.pos;
18066 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
18067 /* A line that is entirely from a string/image/stretch... */
18068 row->maxpos = row->minpos;
18069 else
18070 abort ();
18071 }
18072 else
18073 row->maxpos = it->current.pos;
18074 }
18075
18076 /* Construct the glyph row IT->glyph_row in the desired matrix of
18077 IT->w from text at the current position of IT. See dispextern.h
18078 for an overview of struct it. Value is non-zero if
18079 IT->glyph_row displays text, as opposed to a line displaying ZV
18080 only. */
18081
18082 static int
18083 display_line (struct it *it)
18084 {
18085 struct glyph_row *row = it->glyph_row;
18086 Lisp_Object overlay_arrow_string;
18087 struct it wrap_it;
18088 void *wrap_data = NULL;
18089 int may_wrap = 0, wrap_x IF_LINT (= 0);
18090 int wrap_row_used = -1;
18091 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
18092 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
18093 int wrap_row_extra_line_spacing IF_LINT (= 0);
18094 EMACS_INT wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
18095 EMACS_INT wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
18096 int cvpos;
18097 EMACS_INT min_pos = ZV + 1, max_pos = 0;
18098 EMACS_INT min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
18099
18100 /* We always start displaying at hpos zero even if hscrolled. */
18101 xassert (it->hpos == 0 && it->current_x == 0);
18102
18103 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
18104 >= it->w->desired_matrix->nrows)
18105 {
18106 it->w->nrows_scale_factor++;
18107 fonts_changed_p = 1;
18108 return 0;
18109 }
18110
18111 /* Is IT->w showing the region? */
18112 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
18113
18114 /* Clear the result glyph row and enable it. */
18115 prepare_desired_row (row);
18116
18117 row->y = it->current_y;
18118 row->start = it->start;
18119 row->continuation_lines_width = it->continuation_lines_width;
18120 row->displays_text_p = 1;
18121 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
18122 it->starts_in_middle_of_char_p = 0;
18123
18124 /* Arrange the overlays nicely for our purposes. Usually, we call
18125 display_line on only one line at a time, in which case this
18126 can't really hurt too much, or we call it on lines which appear
18127 one after another in the buffer, in which case all calls to
18128 recenter_overlay_lists but the first will be pretty cheap. */
18129 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
18130
18131 /* Move over display elements that are not visible because we are
18132 hscrolled. This may stop at an x-position < IT->first_visible_x
18133 if the first glyph is partially visible or if we hit a line end. */
18134 if (it->current_x < it->first_visible_x)
18135 {
18136 this_line_min_pos = row->start.pos;
18137 move_it_in_display_line_to (it, ZV, it->first_visible_x,
18138 MOVE_TO_POS | MOVE_TO_X);
18139 /* Record the smallest positions seen while we moved over
18140 display elements that are not visible. This is needed by
18141 redisplay_internal for optimizing the case where the cursor
18142 stays inside the same line. The rest of this function only
18143 considers positions that are actually displayed, so
18144 RECORD_MAX_MIN_POS will not otherwise record positions that
18145 are hscrolled to the left of the left edge of the window. */
18146 min_pos = CHARPOS (this_line_min_pos);
18147 min_bpos = BYTEPOS (this_line_min_pos);
18148 }
18149 else
18150 {
18151 /* We only do this when not calling `move_it_in_display_line_to'
18152 above, because move_it_in_display_line_to calls
18153 handle_line_prefix itself. */
18154 handle_line_prefix (it);
18155 }
18156
18157 /* Get the initial row height. This is either the height of the
18158 text hscrolled, if there is any, or zero. */
18159 row->ascent = it->max_ascent;
18160 row->height = it->max_ascent + it->max_descent;
18161 row->phys_ascent = it->max_phys_ascent;
18162 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18163 row->extra_line_spacing = it->max_extra_line_spacing;
18164
18165 /* Utility macro to record max and min buffer positions seen until now. */
18166 #define RECORD_MAX_MIN_POS(IT) \
18167 do \
18168 { \
18169 if (IT_CHARPOS (*(IT)) < min_pos) \
18170 { \
18171 min_pos = IT_CHARPOS (*(IT)); \
18172 min_bpos = IT_BYTEPOS (*(IT)); \
18173 } \
18174 if (IT_CHARPOS (*(IT)) > max_pos) \
18175 { \
18176 max_pos = IT_CHARPOS (*(IT)); \
18177 max_bpos = IT_BYTEPOS (*(IT)); \
18178 } \
18179 } \
18180 while (0)
18181
18182 /* Loop generating characters. The loop is left with IT on the next
18183 character to display. */
18184 while (1)
18185 {
18186 int n_glyphs_before, hpos_before, x_before;
18187 int x, nglyphs;
18188 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
18189
18190 /* Retrieve the next thing to display. Value is zero if end of
18191 buffer reached. */
18192 if (!get_next_display_element (it))
18193 {
18194 /* Maybe add a space at the end of this line that is used to
18195 display the cursor there under X. Set the charpos of the
18196 first glyph of blank lines not corresponding to any text
18197 to -1. */
18198 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18199 row->exact_window_width_line_p = 1;
18200 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
18201 || row->used[TEXT_AREA] == 0)
18202 {
18203 row->glyphs[TEXT_AREA]->charpos = -1;
18204 row->displays_text_p = 0;
18205
18206 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
18207 && (!MINI_WINDOW_P (it->w)
18208 || (minibuf_level && EQ (it->window, minibuf_window))))
18209 row->indicate_empty_line_p = 1;
18210 }
18211
18212 it->continuation_lines_width = 0;
18213 row->ends_at_zv_p = 1;
18214 /* A row that displays right-to-left text must always have
18215 its last face extended all the way to the end of line,
18216 even if this row ends in ZV, because we still write to
18217 the screen left to right. */
18218 if (row->reversed_p)
18219 extend_face_to_end_of_line (it);
18220 break;
18221 }
18222
18223 /* Now, get the metrics of what we want to display. This also
18224 generates glyphs in `row' (which is IT->glyph_row). */
18225 n_glyphs_before = row->used[TEXT_AREA];
18226 x = it->current_x;
18227
18228 /* Remember the line height so far in case the next element doesn't
18229 fit on the line. */
18230 if (it->line_wrap != TRUNCATE)
18231 {
18232 ascent = it->max_ascent;
18233 descent = it->max_descent;
18234 phys_ascent = it->max_phys_ascent;
18235 phys_descent = it->max_phys_descent;
18236
18237 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
18238 {
18239 if (IT_DISPLAYING_WHITESPACE (it))
18240 may_wrap = 1;
18241 else if (may_wrap)
18242 {
18243 SAVE_IT (wrap_it, *it, wrap_data);
18244 wrap_x = x;
18245 wrap_row_used = row->used[TEXT_AREA];
18246 wrap_row_ascent = row->ascent;
18247 wrap_row_height = row->height;
18248 wrap_row_phys_ascent = row->phys_ascent;
18249 wrap_row_phys_height = row->phys_height;
18250 wrap_row_extra_line_spacing = row->extra_line_spacing;
18251 wrap_row_min_pos = min_pos;
18252 wrap_row_min_bpos = min_bpos;
18253 wrap_row_max_pos = max_pos;
18254 wrap_row_max_bpos = max_bpos;
18255 may_wrap = 0;
18256 }
18257 }
18258 }
18259
18260 PRODUCE_GLYPHS (it);
18261
18262 /* If this display element was in marginal areas, continue with
18263 the next one. */
18264 if (it->area != TEXT_AREA)
18265 {
18266 row->ascent = max (row->ascent, it->max_ascent);
18267 row->height = max (row->height, it->max_ascent + it->max_descent);
18268 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18269 row->phys_height = max (row->phys_height,
18270 it->max_phys_ascent + it->max_phys_descent);
18271 row->extra_line_spacing = max (row->extra_line_spacing,
18272 it->max_extra_line_spacing);
18273 set_iterator_to_next (it, 1);
18274 continue;
18275 }
18276
18277 /* Does the display element fit on the line? If we truncate
18278 lines, we should draw past the right edge of the window. If
18279 we don't truncate, we want to stop so that we can display the
18280 continuation glyph before the right margin. If lines are
18281 continued, there are two possible strategies for characters
18282 resulting in more than 1 glyph (e.g. tabs): Display as many
18283 glyphs as possible in this line and leave the rest for the
18284 continuation line, or display the whole element in the next
18285 line. Original redisplay did the former, so we do it also. */
18286 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
18287 hpos_before = it->hpos;
18288 x_before = x;
18289
18290 if (/* Not a newline. */
18291 nglyphs > 0
18292 /* Glyphs produced fit entirely in the line. */
18293 && it->current_x < it->last_visible_x)
18294 {
18295 it->hpos += nglyphs;
18296 row->ascent = max (row->ascent, it->max_ascent);
18297 row->height = max (row->height, it->max_ascent + it->max_descent);
18298 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18299 row->phys_height = max (row->phys_height,
18300 it->max_phys_ascent + it->max_phys_descent);
18301 row->extra_line_spacing = max (row->extra_line_spacing,
18302 it->max_extra_line_spacing);
18303 if (it->current_x - it->pixel_width < it->first_visible_x)
18304 row->x = x - it->first_visible_x;
18305 /* Record the maximum and minimum buffer positions seen so
18306 far in glyphs that will be displayed by this row. */
18307 if (it->bidi_p)
18308 RECORD_MAX_MIN_POS (it);
18309 }
18310 else
18311 {
18312 int i, new_x;
18313 struct glyph *glyph;
18314
18315 for (i = 0; i < nglyphs; ++i, x = new_x)
18316 {
18317 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
18318 new_x = x + glyph->pixel_width;
18319
18320 if (/* Lines are continued. */
18321 it->line_wrap != TRUNCATE
18322 && (/* Glyph doesn't fit on the line. */
18323 new_x > it->last_visible_x
18324 /* Or it fits exactly on a window system frame. */
18325 || (new_x == it->last_visible_x
18326 && FRAME_WINDOW_P (it->f))))
18327 {
18328 /* End of a continued line. */
18329
18330 if (it->hpos == 0
18331 || (new_x == it->last_visible_x
18332 && FRAME_WINDOW_P (it->f)))
18333 {
18334 /* Current glyph is the only one on the line or
18335 fits exactly on the line. We must continue
18336 the line because we can't draw the cursor
18337 after the glyph. */
18338 row->continued_p = 1;
18339 it->current_x = new_x;
18340 it->continuation_lines_width += new_x;
18341 ++it->hpos;
18342 /* Record the maximum and minimum buffer
18343 positions seen so far in glyphs that will be
18344 displayed by this row. */
18345 if (it->bidi_p)
18346 RECORD_MAX_MIN_POS (it);
18347 if (i == nglyphs - 1)
18348 {
18349 /* If line-wrap is on, check if a previous
18350 wrap point was found. */
18351 if (wrap_row_used > 0
18352 /* Even if there is a previous wrap
18353 point, continue the line here as
18354 usual, if (i) the previous character
18355 was a space or tab AND (ii) the
18356 current character is not. */
18357 && (!may_wrap
18358 || IT_DISPLAYING_WHITESPACE (it)))
18359 goto back_to_wrap;
18360
18361 set_iterator_to_next (it, 1);
18362 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18363 {
18364 if (!get_next_display_element (it))
18365 {
18366 row->exact_window_width_line_p = 1;
18367 it->continuation_lines_width = 0;
18368 row->continued_p = 0;
18369 row->ends_at_zv_p = 1;
18370 }
18371 else if (ITERATOR_AT_END_OF_LINE_P (it))
18372 {
18373 row->continued_p = 0;
18374 row->exact_window_width_line_p = 1;
18375 }
18376 }
18377 }
18378 }
18379 else if (CHAR_GLYPH_PADDING_P (*glyph)
18380 && !FRAME_WINDOW_P (it->f))
18381 {
18382 /* A padding glyph that doesn't fit on this line.
18383 This means the whole character doesn't fit
18384 on the line. */
18385 if (row->reversed_p)
18386 unproduce_glyphs (it, row->used[TEXT_AREA]
18387 - n_glyphs_before);
18388 row->used[TEXT_AREA] = n_glyphs_before;
18389
18390 /* Fill the rest of the row with continuation
18391 glyphs like in 20.x. */
18392 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
18393 < row->glyphs[1 + TEXT_AREA])
18394 produce_special_glyphs (it, IT_CONTINUATION);
18395
18396 row->continued_p = 1;
18397 it->current_x = x_before;
18398 it->continuation_lines_width += x_before;
18399
18400 /* Restore the height to what it was before the
18401 element not fitting on the line. */
18402 it->max_ascent = ascent;
18403 it->max_descent = descent;
18404 it->max_phys_ascent = phys_ascent;
18405 it->max_phys_descent = phys_descent;
18406 }
18407 else if (wrap_row_used > 0)
18408 {
18409 back_to_wrap:
18410 if (row->reversed_p)
18411 unproduce_glyphs (it,
18412 row->used[TEXT_AREA] - wrap_row_used);
18413 RESTORE_IT (it, &wrap_it, wrap_data);
18414 it->continuation_lines_width += wrap_x;
18415 row->used[TEXT_AREA] = wrap_row_used;
18416 row->ascent = wrap_row_ascent;
18417 row->height = wrap_row_height;
18418 row->phys_ascent = wrap_row_phys_ascent;
18419 row->phys_height = wrap_row_phys_height;
18420 row->extra_line_spacing = wrap_row_extra_line_spacing;
18421 min_pos = wrap_row_min_pos;
18422 min_bpos = wrap_row_min_bpos;
18423 max_pos = wrap_row_max_pos;
18424 max_bpos = wrap_row_max_bpos;
18425 row->continued_p = 1;
18426 row->ends_at_zv_p = 0;
18427 row->exact_window_width_line_p = 0;
18428 it->continuation_lines_width += x;
18429
18430 /* Make sure that a non-default face is extended
18431 up to the right margin of the window. */
18432 extend_face_to_end_of_line (it);
18433 }
18434 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
18435 {
18436 /* A TAB that extends past the right edge of the
18437 window. This produces a single glyph on
18438 window system frames. We leave the glyph in
18439 this row and let it fill the row, but don't
18440 consume the TAB. */
18441 it->continuation_lines_width += it->last_visible_x;
18442 row->ends_in_middle_of_char_p = 1;
18443 row->continued_p = 1;
18444 glyph->pixel_width = it->last_visible_x - x;
18445 it->starts_in_middle_of_char_p = 1;
18446 }
18447 else
18448 {
18449 /* Something other than a TAB that draws past
18450 the right edge of the window. Restore
18451 positions to values before the element. */
18452 if (row->reversed_p)
18453 unproduce_glyphs (it, row->used[TEXT_AREA]
18454 - (n_glyphs_before + i));
18455 row->used[TEXT_AREA] = n_glyphs_before + i;
18456
18457 /* Display continuation glyphs. */
18458 if (!FRAME_WINDOW_P (it->f))
18459 produce_special_glyphs (it, IT_CONTINUATION);
18460 row->continued_p = 1;
18461
18462 it->current_x = x_before;
18463 it->continuation_lines_width += x;
18464 extend_face_to_end_of_line (it);
18465
18466 if (nglyphs > 1 && i > 0)
18467 {
18468 row->ends_in_middle_of_char_p = 1;
18469 it->starts_in_middle_of_char_p = 1;
18470 }
18471
18472 /* Restore the height to what it was before the
18473 element not fitting on the line. */
18474 it->max_ascent = ascent;
18475 it->max_descent = descent;
18476 it->max_phys_ascent = phys_ascent;
18477 it->max_phys_descent = phys_descent;
18478 }
18479
18480 break;
18481 }
18482 else if (new_x > it->first_visible_x)
18483 {
18484 /* Increment number of glyphs actually displayed. */
18485 ++it->hpos;
18486
18487 /* Record the maximum and minimum buffer positions
18488 seen so far in glyphs that will be displayed by
18489 this row. */
18490 if (it->bidi_p)
18491 RECORD_MAX_MIN_POS (it);
18492
18493 if (x < it->first_visible_x)
18494 /* Glyph is partially visible, i.e. row starts at
18495 negative X position. */
18496 row->x = x - it->first_visible_x;
18497 }
18498 else
18499 {
18500 /* Glyph is completely off the left margin of the
18501 window. This should not happen because of the
18502 move_it_in_display_line at the start of this
18503 function, unless the text display area of the
18504 window is empty. */
18505 xassert (it->first_visible_x <= it->last_visible_x);
18506 }
18507 }
18508
18509 row->ascent = max (row->ascent, it->max_ascent);
18510 row->height = max (row->height, it->max_ascent + it->max_descent);
18511 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
18512 row->phys_height = max (row->phys_height,
18513 it->max_phys_ascent + it->max_phys_descent);
18514 row->extra_line_spacing = max (row->extra_line_spacing,
18515 it->max_extra_line_spacing);
18516
18517 /* End of this display line if row is continued. */
18518 if (row->continued_p || row->ends_at_zv_p)
18519 break;
18520 }
18521
18522 at_end_of_line:
18523 /* Is this a line end? If yes, we're also done, after making
18524 sure that a non-default face is extended up to the right
18525 margin of the window. */
18526 if (ITERATOR_AT_END_OF_LINE_P (it))
18527 {
18528 int used_before = row->used[TEXT_AREA];
18529
18530 row->ends_in_newline_from_string_p = STRINGP (it->object);
18531
18532 /* Add a space at the end of the line that is used to
18533 display the cursor there. */
18534 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18535 append_space_for_newline (it, 0);
18536
18537 /* Extend the face to the end of the line. */
18538 extend_face_to_end_of_line (it);
18539
18540 /* Make sure we have the position. */
18541 if (used_before == 0)
18542 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
18543
18544 /* Record the position of the newline, for use in
18545 find_row_edges. */
18546 it->eol_pos = it->current.pos;
18547
18548 /* Consume the line end. This skips over invisible lines. */
18549 set_iterator_to_next (it, 1);
18550 it->continuation_lines_width = 0;
18551 break;
18552 }
18553
18554 /* Proceed with next display element. Note that this skips
18555 over lines invisible because of selective display. */
18556 set_iterator_to_next (it, 1);
18557
18558 /* If we truncate lines, we are done when the last displayed
18559 glyphs reach past the right margin of the window. */
18560 if (it->line_wrap == TRUNCATE
18561 && (FRAME_WINDOW_P (it->f)
18562 ? (it->current_x >= it->last_visible_x)
18563 : (it->current_x > it->last_visible_x)))
18564 {
18565 /* Maybe add truncation glyphs. */
18566 if (!FRAME_WINDOW_P (it->f))
18567 {
18568 int i, n;
18569
18570 if (!row->reversed_p)
18571 {
18572 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
18573 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18574 break;
18575 }
18576 else
18577 {
18578 for (i = 0; i < row->used[TEXT_AREA]; i++)
18579 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
18580 break;
18581 /* Remove any padding glyphs at the front of ROW, to
18582 make room for the truncation glyphs we will be
18583 adding below. The loop below always inserts at
18584 least one truncation glyph, so also remove the
18585 last glyph added to ROW. */
18586 unproduce_glyphs (it, i + 1);
18587 /* Adjust i for the loop below. */
18588 i = row->used[TEXT_AREA] - (i + 1);
18589 }
18590
18591 for (n = row->used[TEXT_AREA]; i < n; ++i)
18592 {
18593 row->used[TEXT_AREA] = i;
18594 produce_special_glyphs (it, IT_TRUNCATION);
18595 }
18596 }
18597 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18598 {
18599 /* Don't truncate if we can overflow newline into fringe. */
18600 if (!get_next_display_element (it))
18601 {
18602 it->continuation_lines_width = 0;
18603 row->ends_at_zv_p = 1;
18604 row->exact_window_width_line_p = 1;
18605 break;
18606 }
18607 if (ITERATOR_AT_END_OF_LINE_P (it))
18608 {
18609 row->exact_window_width_line_p = 1;
18610 goto at_end_of_line;
18611 }
18612 }
18613
18614 row->truncated_on_right_p = 1;
18615 it->continuation_lines_width = 0;
18616 reseat_at_next_visible_line_start (it, 0);
18617 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
18618 it->hpos = hpos_before;
18619 it->current_x = x_before;
18620 break;
18621 }
18622 }
18623
18624 /* If line is not empty and hscrolled, maybe insert truncation glyphs
18625 at the left window margin. */
18626 if (it->first_visible_x
18627 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
18628 {
18629 if (!FRAME_WINDOW_P (it->f))
18630 insert_left_trunc_glyphs (it);
18631 row->truncated_on_left_p = 1;
18632 }
18633
18634 /* Remember the position at which this line ends.
18635
18636 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
18637 cannot be before the call to find_row_edges below, since that is
18638 where these positions are determined. */
18639 row->end = it->current;
18640 if (!it->bidi_p)
18641 {
18642 row->minpos = row->start.pos;
18643 row->maxpos = row->end.pos;
18644 }
18645 else
18646 {
18647 /* ROW->minpos and ROW->maxpos must be the smallest and
18648 `1 + the largest' buffer positions in ROW. But if ROW was
18649 bidi-reordered, these two positions can be anywhere in the
18650 row, so we must determine them now. */
18651 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
18652 }
18653
18654 /* If the start of this line is the overlay arrow-position, then
18655 mark this glyph row as the one containing the overlay arrow.
18656 This is clearly a mess with variable size fonts. It would be
18657 better to let it be displayed like cursors under X. */
18658 if ((row->displays_text_p || !overlay_arrow_seen)
18659 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
18660 !NILP (overlay_arrow_string)))
18661 {
18662 /* Overlay arrow in window redisplay is a fringe bitmap. */
18663 if (STRINGP (overlay_arrow_string))
18664 {
18665 struct glyph_row *arrow_row
18666 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
18667 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
18668 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
18669 struct glyph *p = row->glyphs[TEXT_AREA];
18670 struct glyph *p2, *end;
18671
18672 /* Copy the arrow glyphs. */
18673 while (glyph < arrow_end)
18674 *p++ = *glyph++;
18675
18676 /* Throw away padding glyphs. */
18677 p2 = p;
18678 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
18679 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
18680 ++p2;
18681 if (p2 > p)
18682 {
18683 while (p2 < end)
18684 *p++ = *p2++;
18685 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
18686 }
18687 }
18688 else
18689 {
18690 xassert (INTEGERP (overlay_arrow_string));
18691 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
18692 }
18693 overlay_arrow_seen = 1;
18694 }
18695
18696 /* Compute pixel dimensions of this line. */
18697 compute_line_metrics (it);
18698
18699 /* Record whether this row ends inside an ellipsis. */
18700 row->ends_in_ellipsis_p
18701 = (it->method == GET_FROM_DISPLAY_VECTOR
18702 && it->ellipsis_p);
18703
18704 /* Save fringe bitmaps in this row. */
18705 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
18706 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
18707 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
18708 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
18709
18710 it->left_user_fringe_bitmap = 0;
18711 it->left_user_fringe_face_id = 0;
18712 it->right_user_fringe_bitmap = 0;
18713 it->right_user_fringe_face_id = 0;
18714
18715 /* Maybe set the cursor. */
18716 cvpos = it->w->cursor.vpos;
18717 if ((cvpos < 0
18718 /* In bidi-reordered rows, keep checking for proper cursor
18719 position even if one has been found already, because buffer
18720 positions in such rows change non-linearly with ROW->VPOS,
18721 when a line is continued. One exception: when we are at ZV,
18722 display cursor on the first suitable glyph row, since all
18723 the empty rows after that also have their position set to ZV. */
18724 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18725 lines' rows is implemented for bidi-reordered rows. */
18726 || (it->bidi_p
18727 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
18728 && PT >= MATRIX_ROW_START_CHARPOS (row)
18729 && PT <= MATRIX_ROW_END_CHARPOS (row)
18730 && cursor_row_p (row))
18731 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
18732
18733 /* Highlight trailing whitespace. */
18734 if (!NILP (Vshow_trailing_whitespace))
18735 highlight_trailing_whitespace (it->f, it->glyph_row);
18736
18737 /* Prepare for the next line. This line starts horizontally at (X
18738 HPOS) = (0 0). Vertical positions are incremented. As a
18739 convenience for the caller, IT->glyph_row is set to the next
18740 row to be used. */
18741 it->current_x = it->hpos = 0;
18742 it->current_y += row->height;
18743 SET_TEXT_POS (it->eol_pos, 0, 0);
18744 ++it->vpos;
18745 ++it->glyph_row;
18746 /* The next row should by default use the same value of the
18747 reversed_p flag as this one. set_iterator_to_next decides when
18748 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
18749 the flag accordingly. */
18750 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
18751 it->glyph_row->reversed_p = row->reversed_p;
18752 it->start = row->end;
18753 return row->displays_text_p;
18754
18755 #undef RECORD_MAX_MIN_POS
18756 }
18757
18758 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
18759 Scurrent_bidi_paragraph_direction, 0, 1, 0,
18760 doc: /* Return paragraph direction at point in BUFFER.
18761 Value is either `left-to-right' or `right-to-left'.
18762 If BUFFER is omitted or nil, it defaults to the current buffer.
18763
18764 Paragraph direction determines how the text in the paragraph is displayed.
18765 In left-to-right paragraphs, text begins at the left margin of the window
18766 and the reading direction is generally left to right. In right-to-left
18767 paragraphs, text begins at the right margin and is read from right to left.
18768
18769 See also `bidi-paragraph-direction'. */)
18770 (Lisp_Object buffer)
18771 {
18772 struct buffer *buf = current_buffer;
18773 struct buffer *old = buf;
18774
18775 if (! NILP (buffer))
18776 {
18777 CHECK_BUFFER (buffer);
18778 buf = XBUFFER (buffer);
18779 }
18780
18781 if (NILP (BVAR (buf, bidi_display_reordering)))
18782 return Qleft_to_right;
18783 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
18784 return BVAR (buf, bidi_paragraph_direction);
18785 else
18786 {
18787 /* Determine the direction from buffer text. We could try to
18788 use current_matrix if it is up to date, but this seems fast
18789 enough as it is. */
18790 struct bidi_it itb;
18791 EMACS_INT pos = BUF_PT (buf);
18792 EMACS_INT bytepos = BUF_PT_BYTE (buf);
18793 int c;
18794
18795 set_buffer_temp (buf);
18796 /* bidi_paragraph_init finds the base direction of the paragraph
18797 by searching forward from paragraph start. We need the base
18798 direction of the current or _previous_ paragraph, so we need
18799 to make sure we are within that paragraph. To that end, find
18800 the previous non-empty line. */
18801 if (pos >= ZV && pos > BEGV)
18802 {
18803 pos--;
18804 bytepos = CHAR_TO_BYTE (pos);
18805 }
18806 while ((c = FETCH_BYTE (bytepos)) == '\n'
18807 || c == ' ' || c == '\t' || c == '\f')
18808 {
18809 if (bytepos <= BEGV_BYTE)
18810 break;
18811 bytepos--;
18812 pos--;
18813 }
18814 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
18815 bytepos--;
18816 itb.charpos = pos;
18817 itb.bytepos = bytepos;
18818 itb.nchars = -1;
18819 itb.string.s = NULL;
18820 itb.string.lstring = Qnil;
18821 itb.frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ()); /* guesswork */
18822 itb.first_elt = 1;
18823 itb.separator_limit = -1;
18824 itb.paragraph_dir = NEUTRAL_DIR;
18825
18826 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
18827 set_buffer_temp (old);
18828 switch (itb.paragraph_dir)
18829 {
18830 case L2R:
18831 return Qleft_to_right;
18832 break;
18833 case R2L:
18834 return Qright_to_left;
18835 break;
18836 default:
18837 abort ();
18838 }
18839 }
18840 }
18841
18842
18843 \f
18844 /***********************************************************************
18845 Menu Bar
18846 ***********************************************************************/
18847
18848 /* Redisplay the menu bar in the frame for window W.
18849
18850 The menu bar of X frames that don't have X toolkit support is
18851 displayed in a special window W->frame->menu_bar_window.
18852
18853 The menu bar of terminal frames is treated specially as far as
18854 glyph matrices are concerned. Menu bar lines are not part of
18855 windows, so the update is done directly on the frame matrix rows
18856 for the menu bar. */
18857
18858 static void
18859 display_menu_bar (struct window *w)
18860 {
18861 struct frame *f = XFRAME (WINDOW_FRAME (w));
18862 struct it it;
18863 Lisp_Object items;
18864 int i;
18865
18866 /* Don't do all this for graphical frames. */
18867 #ifdef HAVE_NTGUI
18868 if (FRAME_W32_P (f))
18869 return;
18870 #endif
18871 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
18872 if (FRAME_X_P (f))
18873 return;
18874 #endif
18875
18876 #ifdef HAVE_NS
18877 if (FRAME_NS_P (f))
18878 return;
18879 #endif /* HAVE_NS */
18880
18881 #ifdef USE_X_TOOLKIT
18882 xassert (!FRAME_WINDOW_P (f));
18883 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
18884 it.first_visible_x = 0;
18885 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18886 #else /* not USE_X_TOOLKIT */
18887 if (FRAME_WINDOW_P (f))
18888 {
18889 /* Menu bar lines are displayed in the desired matrix of the
18890 dummy window menu_bar_window. */
18891 struct window *menu_w;
18892 xassert (WINDOWP (f->menu_bar_window));
18893 menu_w = XWINDOW (f->menu_bar_window);
18894 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
18895 MENU_FACE_ID);
18896 it.first_visible_x = 0;
18897 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
18898 }
18899 else
18900 {
18901 /* This is a TTY frame, i.e. character hpos/vpos are used as
18902 pixel x/y. */
18903 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
18904 MENU_FACE_ID);
18905 it.first_visible_x = 0;
18906 it.last_visible_x = FRAME_COLS (f);
18907 }
18908 #endif /* not USE_X_TOOLKIT */
18909
18910 /* FIXME: This should be controlled by a user option. See the
18911 comments in redisplay_tool_bar and display_mode_line about
18912 this. */
18913 it.paragraph_embedding = L2R;
18914
18915 if (! mode_line_inverse_video)
18916 /* Force the menu-bar to be displayed in the default face. */
18917 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
18918
18919 /* Clear all rows of the menu bar. */
18920 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
18921 {
18922 struct glyph_row *row = it.glyph_row + i;
18923 clear_glyph_row (row);
18924 row->enabled_p = 1;
18925 row->full_width_p = 1;
18926 }
18927
18928 /* Display all items of the menu bar. */
18929 items = FRAME_MENU_BAR_ITEMS (it.f);
18930 for (i = 0; i < ASIZE (items); i += 4)
18931 {
18932 Lisp_Object string;
18933
18934 /* Stop at nil string. */
18935 string = AREF (items, i + 1);
18936 if (NILP (string))
18937 break;
18938
18939 /* Remember where item was displayed. */
18940 ASET (items, i + 3, make_number (it.hpos));
18941
18942 /* Display the item, pad with one space. */
18943 if (it.current_x < it.last_visible_x)
18944 display_string (NULL, string, Qnil, 0, 0, &it,
18945 SCHARS (string) + 1, 0, 0, -1);
18946 }
18947
18948 /* Fill out the line with spaces. */
18949 if (it.current_x < it.last_visible_x)
18950 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
18951
18952 /* Compute the total height of the lines. */
18953 compute_line_metrics (&it);
18954 }
18955
18956
18957 \f
18958 /***********************************************************************
18959 Mode Line
18960 ***********************************************************************/
18961
18962 /* Redisplay mode lines in the window tree whose root is WINDOW. If
18963 FORCE is non-zero, redisplay mode lines unconditionally.
18964 Otherwise, redisplay only mode lines that are garbaged. Value is
18965 the number of windows whose mode lines were redisplayed. */
18966
18967 static int
18968 redisplay_mode_lines (Lisp_Object window, int force)
18969 {
18970 int nwindows = 0;
18971
18972 while (!NILP (window))
18973 {
18974 struct window *w = XWINDOW (window);
18975
18976 if (WINDOWP (w->hchild))
18977 nwindows += redisplay_mode_lines (w->hchild, force);
18978 else if (WINDOWP (w->vchild))
18979 nwindows += redisplay_mode_lines (w->vchild, force);
18980 else if (force
18981 || FRAME_GARBAGED_P (XFRAME (w->frame))
18982 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
18983 {
18984 struct text_pos lpoint;
18985 struct buffer *old = current_buffer;
18986
18987 /* Set the window's buffer for the mode line display. */
18988 SET_TEXT_POS (lpoint, PT, PT_BYTE);
18989 set_buffer_internal_1 (XBUFFER (w->buffer));
18990
18991 /* Point refers normally to the selected window. For any
18992 other window, set up appropriate value. */
18993 if (!EQ (window, selected_window))
18994 {
18995 struct text_pos pt;
18996
18997 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
18998 if (CHARPOS (pt) < BEGV)
18999 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
19000 else if (CHARPOS (pt) > (ZV - 1))
19001 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
19002 else
19003 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
19004 }
19005
19006 /* Display mode lines. */
19007 clear_glyph_matrix (w->desired_matrix);
19008 if (display_mode_lines (w))
19009 {
19010 ++nwindows;
19011 w->must_be_updated_p = 1;
19012 }
19013
19014 /* Restore old settings. */
19015 set_buffer_internal_1 (old);
19016 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
19017 }
19018
19019 window = w->next;
19020 }
19021
19022 return nwindows;
19023 }
19024
19025
19026 /* Display the mode and/or header line of window W. Value is the
19027 sum number of mode lines and header lines displayed. */
19028
19029 static int
19030 display_mode_lines (struct window *w)
19031 {
19032 Lisp_Object old_selected_window, old_selected_frame;
19033 int n = 0;
19034
19035 old_selected_frame = selected_frame;
19036 selected_frame = w->frame;
19037 old_selected_window = selected_window;
19038 XSETWINDOW (selected_window, w);
19039
19040 /* These will be set while the mode line specs are processed. */
19041 line_number_displayed = 0;
19042 w->column_number_displayed = Qnil;
19043
19044 if (WINDOW_WANTS_MODELINE_P (w))
19045 {
19046 struct window *sel_w = XWINDOW (old_selected_window);
19047
19048 /* Select mode line face based on the real selected window. */
19049 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
19050 BVAR (current_buffer, mode_line_format));
19051 ++n;
19052 }
19053
19054 if (WINDOW_WANTS_HEADER_LINE_P (w))
19055 {
19056 display_mode_line (w, HEADER_LINE_FACE_ID,
19057 BVAR (current_buffer, header_line_format));
19058 ++n;
19059 }
19060
19061 selected_frame = old_selected_frame;
19062 selected_window = old_selected_window;
19063 return n;
19064 }
19065
19066
19067 /* Display mode or header line of window W. FACE_ID specifies which
19068 line to display; it is either MODE_LINE_FACE_ID or
19069 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
19070 display. Value is the pixel height of the mode/header line
19071 displayed. */
19072
19073 static int
19074 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
19075 {
19076 struct it it;
19077 struct face *face;
19078 int count = SPECPDL_INDEX ();
19079
19080 init_iterator (&it, w, -1, -1, NULL, face_id);
19081 /* Don't extend on a previously drawn mode-line.
19082 This may happen if called from pos_visible_p. */
19083 it.glyph_row->enabled_p = 0;
19084 prepare_desired_row (it.glyph_row);
19085
19086 it.glyph_row->mode_line_p = 1;
19087
19088 if (! mode_line_inverse_video)
19089 /* Force the mode-line to be displayed in the default face. */
19090 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19091
19092 /* FIXME: This should be controlled by a user option. But
19093 supporting such an option is not trivial, since the mode line is
19094 made up of many separate strings. */
19095 it.paragraph_embedding = L2R;
19096
19097 record_unwind_protect (unwind_format_mode_line,
19098 format_mode_line_unwind_data (NULL, Qnil, 0));
19099
19100 mode_line_target = MODE_LINE_DISPLAY;
19101
19102 /* Temporarily make frame's keyboard the current kboard so that
19103 kboard-local variables in the mode_line_format will get the right
19104 values. */
19105 push_kboard (FRAME_KBOARD (it.f));
19106 record_unwind_save_match_data ();
19107 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19108 pop_kboard ();
19109
19110 unbind_to (count, Qnil);
19111
19112 /* Fill up with spaces. */
19113 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
19114
19115 compute_line_metrics (&it);
19116 it.glyph_row->full_width_p = 1;
19117 it.glyph_row->continued_p = 0;
19118 it.glyph_row->truncated_on_left_p = 0;
19119 it.glyph_row->truncated_on_right_p = 0;
19120
19121 /* Make a 3D mode-line have a shadow at its right end. */
19122 face = FACE_FROM_ID (it.f, face_id);
19123 extend_face_to_end_of_line (&it);
19124 if (face->box != FACE_NO_BOX)
19125 {
19126 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
19127 + it.glyph_row->used[TEXT_AREA] - 1);
19128 last->right_box_line_p = 1;
19129 }
19130
19131 return it.glyph_row->height;
19132 }
19133
19134 /* Move element ELT in LIST to the front of LIST.
19135 Return the updated list. */
19136
19137 static Lisp_Object
19138 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
19139 {
19140 register Lisp_Object tail, prev;
19141 register Lisp_Object tem;
19142
19143 tail = list;
19144 prev = Qnil;
19145 while (CONSP (tail))
19146 {
19147 tem = XCAR (tail);
19148
19149 if (EQ (elt, tem))
19150 {
19151 /* Splice out the link TAIL. */
19152 if (NILP (prev))
19153 list = XCDR (tail);
19154 else
19155 Fsetcdr (prev, XCDR (tail));
19156
19157 /* Now make it the first. */
19158 Fsetcdr (tail, list);
19159 return tail;
19160 }
19161 else
19162 prev = tail;
19163 tail = XCDR (tail);
19164 QUIT;
19165 }
19166
19167 /* Not found--return unchanged LIST. */
19168 return list;
19169 }
19170
19171 /* Contribute ELT to the mode line for window IT->w. How it
19172 translates into text depends on its data type.
19173
19174 IT describes the display environment in which we display, as usual.
19175
19176 DEPTH is the depth in recursion. It is used to prevent
19177 infinite recursion here.
19178
19179 FIELD_WIDTH is the number of characters the display of ELT should
19180 occupy in the mode line, and PRECISION is the maximum number of
19181 characters to display from ELT's representation. See
19182 display_string for details.
19183
19184 Returns the hpos of the end of the text generated by ELT.
19185
19186 PROPS is a property list to add to any string we encounter.
19187
19188 If RISKY is nonzero, remove (disregard) any properties in any string
19189 we encounter, and ignore :eval and :propertize.
19190
19191 The global variable `mode_line_target' determines whether the
19192 output is passed to `store_mode_line_noprop',
19193 `store_mode_line_string', or `display_string'. */
19194
19195 static int
19196 display_mode_element (struct it *it, int depth, int field_width, int precision,
19197 Lisp_Object elt, Lisp_Object props, int risky)
19198 {
19199 int n = 0, field, prec;
19200 int literal = 0;
19201
19202 tail_recurse:
19203 if (depth > 100)
19204 elt = build_string ("*too-deep*");
19205
19206 depth++;
19207
19208 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
19209 {
19210 case Lisp_String:
19211 {
19212 /* A string: output it and check for %-constructs within it. */
19213 unsigned char c;
19214 EMACS_INT offset = 0;
19215
19216 if (SCHARS (elt) > 0
19217 && (!NILP (props) || risky))
19218 {
19219 Lisp_Object oprops, aelt;
19220 oprops = Ftext_properties_at (make_number (0), elt);
19221
19222 /* If the starting string's properties are not what
19223 we want, translate the string. Also, if the string
19224 is risky, do that anyway. */
19225
19226 if (NILP (Fequal (props, oprops)) || risky)
19227 {
19228 /* If the starting string has properties,
19229 merge the specified ones onto the existing ones. */
19230 if (! NILP (oprops) && !risky)
19231 {
19232 Lisp_Object tem;
19233
19234 oprops = Fcopy_sequence (oprops);
19235 tem = props;
19236 while (CONSP (tem))
19237 {
19238 oprops = Fplist_put (oprops, XCAR (tem),
19239 XCAR (XCDR (tem)));
19240 tem = XCDR (XCDR (tem));
19241 }
19242 props = oprops;
19243 }
19244
19245 aelt = Fassoc (elt, mode_line_proptrans_alist);
19246 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
19247 {
19248 /* AELT is what we want. Move it to the front
19249 without consing. */
19250 elt = XCAR (aelt);
19251 mode_line_proptrans_alist
19252 = move_elt_to_front (aelt, mode_line_proptrans_alist);
19253 }
19254 else
19255 {
19256 Lisp_Object tem;
19257
19258 /* If AELT has the wrong props, it is useless.
19259 so get rid of it. */
19260 if (! NILP (aelt))
19261 mode_line_proptrans_alist
19262 = Fdelq (aelt, mode_line_proptrans_alist);
19263
19264 elt = Fcopy_sequence (elt);
19265 Fset_text_properties (make_number (0), Flength (elt),
19266 props, elt);
19267 /* Add this item to mode_line_proptrans_alist. */
19268 mode_line_proptrans_alist
19269 = Fcons (Fcons (elt, props),
19270 mode_line_proptrans_alist);
19271 /* Truncate mode_line_proptrans_alist
19272 to at most 50 elements. */
19273 tem = Fnthcdr (make_number (50),
19274 mode_line_proptrans_alist);
19275 if (! NILP (tem))
19276 XSETCDR (tem, Qnil);
19277 }
19278 }
19279 }
19280
19281 offset = 0;
19282
19283 if (literal)
19284 {
19285 prec = precision - n;
19286 switch (mode_line_target)
19287 {
19288 case MODE_LINE_NOPROP:
19289 case MODE_LINE_TITLE:
19290 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
19291 break;
19292 case MODE_LINE_STRING:
19293 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
19294 break;
19295 case MODE_LINE_DISPLAY:
19296 n += display_string (NULL, elt, Qnil, 0, 0, it,
19297 0, prec, 0, STRING_MULTIBYTE (elt));
19298 break;
19299 }
19300
19301 break;
19302 }
19303
19304 /* Handle the non-literal case. */
19305
19306 while ((precision <= 0 || n < precision)
19307 && SREF (elt, offset) != 0
19308 && (mode_line_target != MODE_LINE_DISPLAY
19309 || it->current_x < it->last_visible_x))
19310 {
19311 EMACS_INT last_offset = offset;
19312
19313 /* Advance to end of string or next format specifier. */
19314 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
19315 ;
19316
19317 if (offset - 1 != last_offset)
19318 {
19319 EMACS_INT nchars, nbytes;
19320
19321 /* Output to end of string or up to '%'. Field width
19322 is length of string. Don't output more than
19323 PRECISION allows us. */
19324 offset--;
19325
19326 prec = c_string_width (SDATA (elt) + last_offset,
19327 offset - last_offset, precision - n,
19328 &nchars, &nbytes);
19329
19330 switch (mode_line_target)
19331 {
19332 case MODE_LINE_NOPROP:
19333 case MODE_LINE_TITLE:
19334 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
19335 break;
19336 case MODE_LINE_STRING:
19337 {
19338 EMACS_INT bytepos = last_offset;
19339 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19340 EMACS_INT endpos = (precision <= 0
19341 ? string_byte_to_char (elt, offset)
19342 : charpos + nchars);
19343
19344 n += store_mode_line_string (NULL,
19345 Fsubstring (elt, make_number (charpos),
19346 make_number (endpos)),
19347 0, 0, 0, Qnil);
19348 }
19349 break;
19350 case MODE_LINE_DISPLAY:
19351 {
19352 EMACS_INT bytepos = last_offset;
19353 EMACS_INT charpos = string_byte_to_char (elt, bytepos);
19354
19355 if (precision <= 0)
19356 nchars = string_byte_to_char (elt, offset) - charpos;
19357 n += display_string (NULL, elt, Qnil, 0, charpos,
19358 it, 0, nchars, 0,
19359 STRING_MULTIBYTE (elt));
19360 }
19361 break;
19362 }
19363 }
19364 else /* c == '%' */
19365 {
19366 EMACS_INT percent_position = offset;
19367
19368 /* Get the specified minimum width. Zero means
19369 don't pad. */
19370 field = 0;
19371 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
19372 field = field * 10 + c - '0';
19373
19374 /* Don't pad beyond the total padding allowed. */
19375 if (field_width - n > 0 && field > field_width - n)
19376 field = field_width - n;
19377
19378 /* Note that either PRECISION <= 0 or N < PRECISION. */
19379 prec = precision - n;
19380
19381 if (c == 'M')
19382 n += display_mode_element (it, depth, field, prec,
19383 Vglobal_mode_string, props,
19384 risky);
19385 else if (c != 0)
19386 {
19387 int multibyte;
19388 EMACS_INT bytepos, charpos;
19389 const char *spec;
19390 Lisp_Object string;
19391
19392 bytepos = percent_position;
19393 charpos = (STRING_MULTIBYTE (elt)
19394 ? string_byte_to_char (elt, bytepos)
19395 : bytepos);
19396 spec = decode_mode_spec (it->w, c, field, &string);
19397 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
19398
19399 switch (mode_line_target)
19400 {
19401 case MODE_LINE_NOPROP:
19402 case MODE_LINE_TITLE:
19403 n += store_mode_line_noprop (spec, field, prec);
19404 break;
19405 case MODE_LINE_STRING:
19406 {
19407 int len = strlen (spec);
19408 Lisp_Object tem = make_string (spec, len);
19409 props = Ftext_properties_at (make_number (charpos), elt);
19410 /* Should only keep face property in props */
19411 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
19412 }
19413 break;
19414 case MODE_LINE_DISPLAY:
19415 {
19416 int nglyphs_before, nwritten;
19417
19418 nglyphs_before = it->glyph_row->used[TEXT_AREA];
19419 nwritten = display_string (spec, string, elt,
19420 charpos, 0, it,
19421 field, prec, 0,
19422 multibyte);
19423
19424 /* Assign to the glyphs written above the
19425 string where the `%x' came from, position
19426 of the `%'. */
19427 if (nwritten > 0)
19428 {
19429 struct glyph *glyph
19430 = (it->glyph_row->glyphs[TEXT_AREA]
19431 + nglyphs_before);
19432 int i;
19433
19434 for (i = 0; i < nwritten; ++i)
19435 {
19436 glyph[i].object = elt;
19437 glyph[i].charpos = charpos;
19438 }
19439
19440 n += nwritten;
19441 }
19442 }
19443 break;
19444 }
19445 }
19446 else /* c == 0 */
19447 break;
19448 }
19449 }
19450 }
19451 break;
19452
19453 case Lisp_Symbol:
19454 /* A symbol: process the value of the symbol recursively
19455 as if it appeared here directly. Avoid error if symbol void.
19456 Special case: if value of symbol is a string, output the string
19457 literally. */
19458 {
19459 register Lisp_Object tem;
19460
19461 /* If the variable is not marked as risky to set
19462 then its contents are risky to use. */
19463 if (NILP (Fget (elt, Qrisky_local_variable)))
19464 risky = 1;
19465
19466 tem = Fboundp (elt);
19467 if (!NILP (tem))
19468 {
19469 tem = Fsymbol_value (elt);
19470 /* If value is a string, output that string literally:
19471 don't check for % within it. */
19472 if (STRINGP (tem))
19473 literal = 1;
19474
19475 if (!EQ (tem, elt))
19476 {
19477 /* Give up right away for nil or t. */
19478 elt = tem;
19479 goto tail_recurse;
19480 }
19481 }
19482 }
19483 break;
19484
19485 case Lisp_Cons:
19486 {
19487 register Lisp_Object car, tem;
19488
19489 /* A cons cell: five distinct cases.
19490 If first element is :eval or :propertize, do something special.
19491 If first element is a string or a cons, process all the elements
19492 and effectively concatenate them.
19493 If first element is a negative number, truncate displaying cdr to
19494 at most that many characters. If positive, pad (with spaces)
19495 to at least that many characters.
19496 If first element is a symbol, process the cadr or caddr recursively
19497 according to whether the symbol's value is non-nil or nil. */
19498 car = XCAR (elt);
19499 if (EQ (car, QCeval))
19500 {
19501 /* An element of the form (:eval FORM) means evaluate FORM
19502 and use the result as mode line elements. */
19503
19504 if (risky)
19505 break;
19506
19507 if (CONSP (XCDR (elt)))
19508 {
19509 Lisp_Object spec;
19510 spec = safe_eval (XCAR (XCDR (elt)));
19511 n += display_mode_element (it, depth, field_width - n,
19512 precision - n, spec, props,
19513 risky);
19514 }
19515 }
19516 else if (EQ (car, QCpropertize))
19517 {
19518 /* An element of the form (:propertize ELT PROPS...)
19519 means display ELT but applying properties PROPS. */
19520
19521 if (risky)
19522 break;
19523
19524 if (CONSP (XCDR (elt)))
19525 n += display_mode_element (it, depth, field_width - n,
19526 precision - n, XCAR (XCDR (elt)),
19527 XCDR (XCDR (elt)), risky);
19528 }
19529 else if (SYMBOLP (car))
19530 {
19531 tem = Fboundp (car);
19532 elt = XCDR (elt);
19533 if (!CONSP (elt))
19534 goto invalid;
19535 /* elt is now the cdr, and we know it is a cons cell.
19536 Use its car if CAR has a non-nil value. */
19537 if (!NILP (tem))
19538 {
19539 tem = Fsymbol_value (car);
19540 if (!NILP (tem))
19541 {
19542 elt = XCAR (elt);
19543 goto tail_recurse;
19544 }
19545 }
19546 /* Symbol's value is nil (or symbol is unbound)
19547 Get the cddr of the original list
19548 and if possible find the caddr and use that. */
19549 elt = XCDR (elt);
19550 if (NILP (elt))
19551 break;
19552 else if (!CONSP (elt))
19553 goto invalid;
19554 elt = XCAR (elt);
19555 goto tail_recurse;
19556 }
19557 else if (INTEGERP (car))
19558 {
19559 register int lim = XINT (car);
19560 elt = XCDR (elt);
19561 if (lim < 0)
19562 {
19563 /* Negative int means reduce maximum width. */
19564 if (precision <= 0)
19565 precision = -lim;
19566 else
19567 precision = min (precision, -lim);
19568 }
19569 else if (lim > 0)
19570 {
19571 /* Padding specified. Don't let it be more than
19572 current maximum. */
19573 if (precision > 0)
19574 lim = min (precision, lim);
19575
19576 /* If that's more padding than already wanted, queue it.
19577 But don't reduce padding already specified even if
19578 that is beyond the current truncation point. */
19579 field_width = max (lim, field_width);
19580 }
19581 goto tail_recurse;
19582 }
19583 else if (STRINGP (car) || CONSP (car))
19584 {
19585 Lisp_Object halftail = elt;
19586 int len = 0;
19587
19588 while (CONSP (elt)
19589 && (precision <= 0 || n < precision))
19590 {
19591 n += display_mode_element (it, depth,
19592 /* Do padding only after the last
19593 element in the list. */
19594 (! CONSP (XCDR (elt))
19595 ? field_width - n
19596 : 0),
19597 precision - n, XCAR (elt),
19598 props, risky);
19599 elt = XCDR (elt);
19600 len++;
19601 if ((len & 1) == 0)
19602 halftail = XCDR (halftail);
19603 /* Check for cycle. */
19604 if (EQ (halftail, elt))
19605 break;
19606 }
19607 }
19608 }
19609 break;
19610
19611 default:
19612 invalid:
19613 elt = build_string ("*invalid*");
19614 goto tail_recurse;
19615 }
19616
19617 /* Pad to FIELD_WIDTH. */
19618 if (field_width > 0 && n < field_width)
19619 {
19620 switch (mode_line_target)
19621 {
19622 case MODE_LINE_NOPROP:
19623 case MODE_LINE_TITLE:
19624 n += store_mode_line_noprop ("", field_width - n, 0);
19625 break;
19626 case MODE_LINE_STRING:
19627 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
19628 break;
19629 case MODE_LINE_DISPLAY:
19630 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
19631 0, 0, 0);
19632 break;
19633 }
19634 }
19635
19636 return n;
19637 }
19638
19639 /* Store a mode-line string element in mode_line_string_list.
19640
19641 If STRING is non-null, display that C string. Otherwise, the Lisp
19642 string LISP_STRING is displayed.
19643
19644 FIELD_WIDTH is the minimum number of output glyphs to produce.
19645 If STRING has fewer characters than FIELD_WIDTH, pad to the right
19646 with spaces. FIELD_WIDTH <= 0 means don't pad.
19647
19648 PRECISION is the maximum number of characters to output from
19649 STRING. PRECISION <= 0 means don't truncate the string.
19650
19651 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
19652 properties to the string.
19653
19654 PROPS are the properties to add to the string.
19655 The mode_line_string_face face property is always added to the string.
19656 */
19657
19658 static int
19659 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
19660 int field_width, int precision, Lisp_Object props)
19661 {
19662 EMACS_INT len;
19663 int n = 0;
19664
19665 if (string != NULL)
19666 {
19667 len = strlen (string);
19668 if (precision > 0 && len > precision)
19669 len = precision;
19670 lisp_string = make_string (string, len);
19671 if (NILP (props))
19672 props = mode_line_string_face_prop;
19673 else if (!NILP (mode_line_string_face))
19674 {
19675 Lisp_Object face = Fplist_get (props, Qface);
19676 props = Fcopy_sequence (props);
19677 if (NILP (face))
19678 face = mode_line_string_face;
19679 else
19680 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19681 props = Fplist_put (props, Qface, face);
19682 }
19683 Fadd_text_properties (make_number (0), make_number (len),
19684 props, lisp_string);
19685 }
19686 else
19687 {
19688 len = XFASTINT (Flength (lisp_string));
19689 if (precision > 0 && len > precision)
19690 {
19691 len = precision;
19692 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
19693 precision = -1;
19694 }
19695 if (!NILP (mode_line_string_face))
19696 {
19697 Lisp_Object face;
19698 if (NILP (props))
19699 props = Ftext_properties_at (make_number (0), lisp_string);
19700 face = Fplist_get (props, Qface);
19701 if (NILP (face))
19702 face = mode_line_string_face;
19703 else
19704 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
19705 props = Fcons (Qface, Fcons (face, Qnil));
19706 if (copy_string)
19707 lisp_string = Fcopy_sequence (lisp_string);
19708 }
19709 if (!NILP (props))
19710 Fadd_text_properties (make_number (0), make_number (len),
19711 props, lisp_string);
19712 }
19713
19714 if (len > 0)
19715 {
19716 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19717 n += len;
19718 }
19719
19720 if (field_width > len)
19721 {
19722 field_width -= len;
19723 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
19724 if (!NILP (props))
19725 Fadd_text_properties (make_number (0), make_number (field_width),
19726 props, lisp_string);
19727 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
19728 n += field_width;
19729 }
19730
19731 return n;
19732 }
19733
19734
19735 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
19736 1, 4, 0,
19737 doc: /* Format a string out of a mode line format specification.
19738 First arg FORMAT specifies the mode line format (see `mode-line-format'
19739 for details) to use.
19740
19741 By default, the format is evaluated for the currently selected window.
19742
19743 Optional second arg FACE specifies the face property to put on all
19744 characters for which no face is specified. The value nil means the
19745 default face. The value t means whatever face the window's mode line
19746 currently uses (either `mode-line' or `mode-line-inactive',
19747 depending on whether the window is the selected window or not).
19748 An integer value means the value string has no text
19749 properties.
19750
19751 Optional third and fourth args WINDOW and BUFFER specify the window
19752 and buffer to use as the context for the formatting (defaults
19753 are the selected window and the WINDOW's buffer). */)
19754 (Lisp_Object format, Lisp_Object face,
19755 Lisp_Object window, Lisp_Object buffer)
19756 {
19757 struct it it;
19758 int len;
19759 struct window *w;
19760 struct buffer *old_buffer = NULL;
19761 int face_id;
19762 int no_props = INTEGERP (face);
19763 int count = SPECPDL_INDEX ();
19764 Lisp_Object str;
19765 int string_start = 0;
19766
19767 if (NILP (window))
19768 window = selected_window;
19769 CHECK_WINDOW (window);
19770 w = XWINDOW (window);
19771
19772 if (NILP (buffer))
19773 buffer = w->buffer;
19774 CHECK_BUFFER (buffer);
19775
19776 /* Make formatting the modeline a non-op when noninteractive, otherwise
19777 there will be problems later caused by a partially initialized frame. */
19778 if (NILP (format) || noninteractive)
19779 return empty_unibyte_string;
19780
19781 if (no_props)
19782 face = Qnil;
19783
19784 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
19785 : EQ (face, Qt) ? (EQ (window, selected_window)
19786 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
19787 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
19788 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
19789 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
19790 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
19791 : DEFAULT_FACE_ID;
19792
19793 if (XBUFFER (buffer) != current_buffer)
19794 old_buffer = current_buffer;
19795
19796 /* Save things including mode_line_proptrans_alist,
19797 and set that to nil so that we don't alter the outer value. */
19798 record_unwind_protect (unwind_format_mode_line,
19799 format_mode_line_unwind_data
19800 (old_buffer, selected_window, 1));
19801 mode_line_proptrans_alist = Qnil;
19802
19803 Fselect_window (window, Qt);
19804 if (old_buffer)
19805 set_buffer_internal_1 (XBUFFER (buffer));
19806
19807 init_iterator (&it, w, -1, -1, NULL, face_id);
19808
19809 if (no_props)
19810 {
19811 mode_line_target = MODE_LINE_NOPROP;
19812 mode_line_string_face_prop = Qnil;
19813 mode_line_string_list = Qnil;
19814 string_start = MODE_LINE_NOPROP_LEN (0);
19815 }
19816 else
19817 {
19818 mode_line_target = MODE_LINE_STRING;
19819 mode_line_string_list = Qnil;
19820 mode_line_string_face = face;
19821 mode_line_string_face_prop
19822 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
19823 }
19824
19825 push_kboard (FRAME_KBOARD (it.f));
19826 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19827 pop_kboard ();
19828
19829 if (no_props)
19830 {
19831 len = MODE_LINE_NOPROP_LEN (string_start);
19832 str = make_string (mode_line_noprop_buf + string_start, len);
19833 }
19834 else
19835 {
19836 mode_line_string_list = Fnreverse (mode_line_string_list);
19837 str = Fmapconcat (intern ("identity"), mode_line_string_list,
19838 empty_unibyte_string);
19839 }
19840
19841 unbind_to (count, Qnil);
19842 return str;
19843 }
19844
19845 /* Write a null-terminated, right justified decimal representation of
19846 the positive integer D to BUF using a minimal field width WIDTH. */
19847
19848 static void
19849 pint2str (register char *buf, register int width, register EMACS_INT d)
19850 {
19851 register char *p = buf;
19852
19853 if (d <= 0)
19854 *p++ = '0';
19855 else
19856 {
19857 while (d > 0)
19858 {
19859 *p++ = d % 10 + '0';
19860 d /= 10;
19861 }
19862 }
19863
19864 for (width -= (int) (p - buf); width > 0; --width)
19865 *p++ = ' ';
19866 *p-- = '\0';
19867 while (p > buf)
19868 {
19869 d = *buf;
19870 *buf++ = *p;
19871 *p-- = d;
19872 }
19873 }
19874
19875 /* Write a null-terminated, right justified decimal and "human
19876 readable" representation of the nonnegative integer D to BUF using
19877 a minimal field width WIDTH. D should be smaller than 999.5e24. */
19878
19879 static const char power_letter[] =
19880 {
19881 0, /* no letter */
19882 'k', /* kilo */
19883 'M', /* mega */
19884 'G', /* giga */
19885 'T', /* tera */
19886 'P', /* peta */
19887 'E', /* exa */
19888 'Z', /* zetta */
19889 'Y' /* yotta */
19890 };
19891
19892 static void
19893 pint2hrstr (char *buf, int width, EMACS_INT d)
19894 {
19895 /* We aim to represent the nonnegative integer D as
19896 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
19897 EMACS_INT quotient = d;
19898 int remainder = 0;
19899 /* -1 means: do not use TENTHS. */
19900 int tenths = -1;
19901 int exponent = 0;
19902
19903 /* Length of QUOTIENT.TENTHS as a string. */
19904 int length;
19905
19906 char * psuffix;
19907 char * p;
19908
19909 if (1000 <= quotient)
19910 {
19911 /* Scale to the appropriate EXPONENT. */
19912 do
19913 {
19914 remainder = quotient % 1000;
19915 quotient /= 1000;
19916 exponent++;
19917 }
19918 while (1000 <= quotient);
19919
19920 /* Round to nearest and decide whether to use TENTHS or not. */
19921 if (quotient <= 9)
19922 {
19923 tenths = remainder / 100;
19924 if (50 <= remainder % 100)
19925 {
19926 if (tenths < 9)
19927 tenths++;
19928 else
19929 {
19930 quotient++;
19931 if (quotient == 10)
19932 tenths = -1;
19933 else
19934 tenths = 0;
19935 }
19936 }
19937 }
19938 else
19939 if (500 <= remainder)
19940 {
19941 if (quotient < 999)
19942 quotient++;
19943 else
19944 {
19945 quotient = 1;
19946 exponent++;
19947 tenths = 0;
19948 }
19949 }
19950 }
19951
19952 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
19953 if (tenths == -1 && quotient <= 99)
19954 if (quotient <= 9)
19955 length = 1;
19956 else
19957 length = 2;
19958 else
19959 length = 3;
19960 p = psuffix = buf + max (width, length);
19961
19962 /* Print EXPONENT. */
19963 *psuffix++ = power_letter[exponent];
19964 *psuffix = '\0';
19965
19966 /* Print TENTHS. */
19967 if (tenths >= 0)
19968 {
19969 *--p = '0' + tenths;
19970 *--p = '.';
19971 }
19972
19973 /* Print QUOTIENT. */
19974 do
19975 {
19976 int digit = quotient % 10;
19977 *--p = '0' + digit;
19978 }
19979 while ((quotient /= 10) != 0);
19980
19981 /* Print leading spaces. */
19982 while (buf < p)
19983 *--p = ' ';
19984 }
19985
19986 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
19987 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
19988 type of CODING_SYSTEM. Return updated pointer into BUF. */
19989
19990 static unsigned char invalid_eol_type[] = "(*invalid*)";
19991
19992 static char *
19993 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
19994 {
19995 Lisp_Object val;
19996 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
19997 const unsigned char *eol_str;
19998 int eol_str_len;
19999 /* The EOL conversion we are using. */
20000 Lisp_Object eoltype;
20001
20002 val = CODING_SYSTEM_SPEC (coding_system);
20003 eoltype = Qnil;
20004
20005 if (!VECTORP (val)) /* Not yet decided. */
20006 {
20007 if (multibyte)
20008 *buf++ = '-';
20009 if (eol_flag)
20010 eoltype = eol_mnemonic_undecided;
20011 /* Don't mention EOL conversion if it isn't decided. */
20012 }
20013 else
20014 {
20015 Lisp_Object attrs;
20016 Lisp_Object eolvalue;
20017
20018 attrs = AREF (val, 0);
20019 eolvalue = AREF (val, 2);
20020
20021 if (multibyte)
20022 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
20023
20024 if (eol_flag)
20025 {
20026 /* The EOL conversion that is normal on this system. */
20027
20028 if (NILP (eolvalue)) /* Not yet decided. */
20029 eoltype = eol_mnemonic_undecided;
20030 else if (VECTORP (eolvalue)) /* Not yet decided. */
20031 eoltype = eol_mnemonic_undecided;
20032 else /* eolvalue is Qunix, Qdos, or Qmac. */
20033 eoltype = (EQ (eolvalue, Qunix)
20034 ? eol_mnemonic_unix
20035 : (EQ (eolvalue, Qdos) == 1
20036 ? eol_mnemonic_dos : eol_mnemonic_mac));
20037 }
20038 }
20039
20040 if (eol_flag)
20041 {
20042 /* Mention the EOL conversion if it is not the usual one. */
20043 if (STRINGP (eoltype))
20044 {
20045 eol_str = SDATA (eoltype);
20046 eol_str_len = SBYTES (eoltype);
20047 }
20048 else if (CHARACTERP (eoltype))
20049 {
20050 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
20051 eol_str_len = CHAR_STRING (XINT (eoltype), tmp);
20052 eol_str = tmp;
20053 }
20054 else
20055 {
20056 eol_str = invalid_eol_type;
20057 eol_str_len = sizeof (invalid_eol_type) - 1;
20058 }
20059 memcpy (buf, eol_str, eol_str_len);
20060 buf += eol_str_len;
20061 }
20062
20063 return buf;
20064 }
20065
20066 /* Return a string for the output of a mode line %-spec for window W,
20067 generated by character C. FIELD_WIDTH > 0 means pad the string
20068 returned with spaces to that value. Return a Lisp string in
20069 *STRING if the resulting string is taken from that Lisp string.
20070
20071 Note we operate on the current buffer for most purposes,
20072 the exception being w->base_line_pos. */
20073
20074 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
20075
20076 static const char *
20077 decode_mode_spec (struct window *w, register int c, int field_width,
20078 Lisp_Object *string)
20079 {
20080 Lisp_Object obj;
20081 struct frame *f = XFRAME (WINDOW_FRAME (w));
20082 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
20083 struct buffer *b = current_buffer;
20084
20085 obj = Qnil;
20086 *string = Qnil;
20087
20088 switch (c)
20089 {
20090 case '*':
20091 if (!NILP (BVAR (b, read_only)))
20092 return "%";
20093 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20094 return "*";
20095 return "-";
20096
20097 case '+':
20098 /* This differs from %* only for a modified read-only buffer. */
20099 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20100 return "*";
20101 if (!NILP (BVAR (b, read_only)))
20102 return "%";
20103 return "-";
20104
20105 case '&':
20106 /* This differs from %* in ignoring read-only-ness. */
20107 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20108 return "*";
20109 return "-";
20110
20111 case '%':
20112 return "%";
20113
20114 case '[':
20115 {
20116 int i;
20117 char *p;
20118
20119 if (command_loop_level > 5)
20120 return "[[[... ";
20121 p = decode_mode_spec_buf;
20122 for (i = 0; i < command_loop_level; i++)
20123 *p++ = '[';
20124 *p = 0;
20125 return decode_mode_spec_buf;
20126 }
20127
20128 case ']':
20129 {
20130 int i;
20131 char *p;
20132
20133 if (command_loop_level > 5)
20134 return " ...]]]";
20135 p = decode_mode_spec_buf;
20136 for (i = 0; i < command_loop_level; i++)
20137 *p++ = ']';
20138 *p = 0;
20139 return decode_mode_spec_buf;
20140 }
20141
20142 case '-':
20143 {
20144 register int i;
20145
20146 /* Let lots_of_dashes be a string of infinite length. */
20147 if (mode_line_target == MODE_LINE_NOPROP ||
20148 mode_line_target == MODE_LINE_STRING)
20149 return "--";
20150 if (field_width <= 0
20151 || field_width > sizeof (lots_of_dashes))
20152 {
20153 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
20154 decode_mode_spec_buf[i] = '-';
20155 decode_mode_spec_buf[i] = '\0';
20156 return decode_mode_spec_buf;
20157 }
20158 else
20159 return lots_of_dashes;
20160 }
20161
20162 case 'b':
20163 obj = BVAR (b, name);
20164 break;
20165
20166 case 'c':
20167 /* %c and %l are ignored in `frame-title-format'.
20168 (In redisplay_internal, the frame title is drawn _before_ the
20169 windows are updated, so the stuff which depends on actual
20170 window contents (such as %l) may fail to render properly, or
20171 even crash emacs.) */
20172 if (mode_line_target == MODE_LINE_TITLE)
20173 return "";
20174 else
20175 {
20176 EMACS_INT col = current_column ();
20177 w->column_number_displayed = make_number (col);
20178 pint2str (decode_mode_spec_buf, field_width, col);
20179 return decode_mode_spec_buf;
20180 }
20181
20182 case 'e':
20183 #ifndef SYSTEM_MALLOC
20184 {
20185 if (NILP (Vmemory_full))
20186 return "";
20187 else
20188 return "!MEM FULL! ";
20189 }
20190 #else
20191 return "";
20192 #endif
20193
20194 case 'F':
20195 /* %F displays the frame name. */
20196 if (!NILP (f->title))
20197 return SSDATA (f->title);
20198 if (f->explicit_name || ! FRAME_WINDOW_P (f))
20199 return SSDATA (f->name);
20200 return "Emacs";
20201
20202 case 'f':
20203 obj = BVAR (b, filename);
20204 break;
20205
20206 case 'i':
20207 {
20208 EMACS_INT size = ZV - BEGV;
20209 pint2str (decode_mode_spec_buf, field_width, size);
20210 return decode_mode_spec_buf;
20211 }
20212
20213 case 'I':
20214 {
20215 EMACS_INT size = ZV - BEGV;
20216 pint2hrstr (decode_mode_spec_buf, field_width, size);
20217 return decode_mode_spec_buf;
20218 }
20219
20220 case 'l':
20221 {
20222 EMACS_INT startpos, startpos_byte, line, linepos, linepos_byte;
20223 EMACS_INT topline, nlines, height;
20224 EMACS_INT junk;
20225
20226 /* %c and %l are ignored in `frame-title-format'. */
20227 if (mode_line_target == MODE_LINE_TITLE)
20228 return "";
20229
20230 startpos = XMARKER (w->start)->charpos;
20231 startpos_byte = marker_byte_position (w->start);
20232 height = WINDOW_TOTAL_LINES (w);
20233
20234 /* If we decided that this buffer isn't suitable for line numbers,
20235 don't forget that too fast. */
20236 if (EQ (w->base_line_pos, w->buffer))
20237 goto no_value;
20238 /* But do forget it, if the window shows a different buffer now. */
20239 else if (BUFFERP (w->base_line_pos))
20240 w->base_line_pos = Qnil;
20241
20242 /* If the buffer is very big, don't waste time. */
20243 if (INTEGERP (Vline_number_display_limit)
20244 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
20245 {
20246 w->base_line_pos = Qnil;
20247 w->base_line_number = Qnil;
20248 goto no_value;
20249 }
20250
20251 if (INTEGERP (w->base_line_number)
20252 && INTEGERP (w->base_line_pos)
20253 && XFASTINT (w->base_line_pos) <= startpos)
20254 {
20255 line = XFASTINT (w->base_line_number);
20256 linepos = XFASTINT (w->base_line_pos);
20257 linepos_byte = buf_charpos_to_bytepos (b, linepos);
20258 }
20259 else
20260 {
20261 line = 1;
20262 linepos = BUF_BEGV (b);
20263 linepos_byte = BUF_BEGV_BYTE (b);
20264 }
20265
20266 /* Count lines from base line to window start position. */
20267 nlines = display_count_lines (linepos_byte,
20268 startpos_byte,
20269 startpos, &junk);
20270
20271 topline = nlines + line;
20272
20273 /* Determine a new base line, if the old one is too close
20274 or too far away, or if we did not have one.
20275 "Too close" means it's plausible a scroll-down would
20276 go back past it. */
20277 if (startpos == BUF_BEGV (b))
20278 {
20279 w->base_line_number = make_number (topline);
20280 w->base_line_pos = make_number (BUF_BEGV (b));
20281 }
20282 else if (nlines < height + 25 || nlines > height * 3 + 50
20283 || linepos == BUF_BEGV (b))
20284 {
20285 EMACS_INT limit = BUF_BEGV (b);
20286 EMACS_INT limit_byte = BUF_BEGV_BYTE (b);
20287 EMACS_INT position;
20288 EMACS_INT distance =
20289 (height * 2 + 30) * line_number_display_limit_width;
20290
20291 if (startpos - distance > limit)
20292 {
20293 limit = startpos - distance;
20294 limit_byte = CHAR_TO_BYTE (limit);
20295 }
20296
20297 nlines = display_count_lines (startpos_byte,
20298 limit_byte,
20299 - (height * 2 + 30),
20300 &position);
20301 /* If we couldn't find the lines we wanted within
20302 line_number_display_limit_width chars per line,
20303 give up on line numbers for this window. */
20304 if (position == limit_byte && limit == startpos - distance)
20305 {
20306 w->base_line_pos = w->buffer;
20307 w->base_line_number = Qnil;
20308 goto no_value;
20309 }
20310
20311 w->base_line_number = make_number (topline - nlines);
20312 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
20313 }
20314
20315 /* Now count lines from the start pos to point. */
20316 nlines = display_count_lines (startpos_byte,
20317 PT_BYTE, PT, &junk);
20318
20319 /* Record that we did display the line number. */
20320 line_number_displayed = 1;
20321
20322 /* Make the string to show. */
20323 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
20324 return decode_mode_spec_buf;
20325 no_value:
20326 {
20327 char* p = decode_mode_spec_buf;
20328 int pad = field_width - 2;
20329 while (pad-- > 0)
20330 *p++ = ' ';
20331 *p++ = '?';
20332 *p++ = '?';
20333 *p = '\0';
20334 return decode_mode_spec_buf;
20335 }
20336 }
20337 break;
20338
20339 case 'm':
20340 obj = BVAR (b, mode_name);
20341 break;
20342
20343 case 'n':
20344 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
20345 return " Narrow";
20346 break;
20347
20348 case 'p':
20349 {
20350 EMACS_INT pos = marker_position (w->start);
20351 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20352
20353 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
20354 {
20355 if (pos <= BUF_BEGV (b))
20356 return "All";
20357 else
20358 return "Bottom";
20359 }
20360 else if (pos <= BUF_BEGV (b))
20361 return "Top";
20362 else
20363 {
20364 if (total > 1000000)
20365 /* Do it differently for a large value, to avoid overflow. */
20366 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20367 else
20368 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
20369 /* We can't normally display a 3-digit number,
20370 so get us a 2-digit number that is close. */
20371 if (total == 100)
20372 total = 99;
20373 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20374 return decode_mode_spec_buf;
20375 }
20376 }
20377
20378 /* Display percentage of size above the bottom of the screen. */
20379 case 'P':
20380 {
20381 EMACS_INT toppos = marker_position (w->start);
20382 EMACS_INT botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
20383 EMACS_INT total = BUF_ZV (b) - BUF_BEGV (b);
20384
20385 if (botpos >= BUF_ZV (b))
20386 {
20387 if (toppos <= BUF_BEGV (b))
20388 return "All";
20389 else
20390 return "Bottom";
20391 }
20392 else
20393 {
20394 if (total > 1000000)
20395 /* Do it differently for a large value, to avoid overflow. */
20396 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
20397 else
20398 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
20399 /* We can't normally display a 3-digit number,
20400 so get us a 2-digit number that is close. */
20401 if (total == 100)
20402 total = 99;
20403 if (toppos <= BUF_BEGV (b))
20404 sprintf (decode_mode_spec_buf, "Top%2"pI"d%%", total);
20405 else
20406 sprintf (decode_mode_spec_buf, "%2"pI"d%%", total);
20407 return decode_mode_spec_buf;
20408 }
20409 }
20410
20411 case 's':
20412 /* status of process */
20413 obj = Fget_buffer_process (Fcurrent_buffer ());
20414 if (NILP (obj))
20415 return "no process";
20416 #ifndef MSDOS
20417 obj = Fsymbol_name (Fprocess_status (obj));
20418 #endif
20419 break;
20420
20421 case '@':
20422 {
20423 int count = inhibit_garbage_collection ();
20424 Lisp_Object val = call1 (intern ("file-remote-p"),
20425 BVAR (current_buffer, directory));
20426 unbind_to (count, Qnil);
20427
20428 if (NILP (val))
20429 return "-";
20430 else
20431 return "@";
20432 }
20433
20434 case 't': /* indicate TEXT or BINARY */
20435 return "T";
20436
20437 case 'z':
20438 /* coding-system (not including end-of-line format) */
20439 case 'Z':
20440 /* coding-system (including end-of-line type) */
20441 {
20442 int eol_flag = (c == 'Z');
20443 char *p = decode_mode_spec_buf;
20444
20445 if (! FRAME_WINDOW_P (f))
20446 {
20447 /* No need to mention EOL here--the terminal never needs
20448 to do EOL conversion. */
20449 p = decode_mode_spec_coding (CODING_ID_NAME
20450 (FRAME_KEYBOARD_CODING (f)->id),
20451 p, 0);
20452 p = decode_mode_spec_coding (CODING_ID_NAME
20453 (FRAME_TERMINAL_CODING (f)->id),
20454 p, 0);
20455 }
20456 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
20457 p, eol_flag);
20458
20459 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
20460 #ifdef subprocesses
20461 obj = Fget_buffer_process (Fcurrent_buffer ());
20462 if (PROCESSP (obj))
20463 {
20464 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
20465 p, eol_flag);
20466 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
20467 p, eol_flag);
20468 }
20469 #endif /* subprocesses */
20470 #endif /* 0 */
20471 *p = 0;
20472 return decode_mode_spec_buf;
20473 }
20474 }
20475
20476 if (STRINGP (obj))
20477 {
20478 *string = obj;
20479 return SSDATA (obj);
20480 }
20481 else
20482 return "";
20483 }
20484
20485
20486 /* Count up to COUNT lines starting from START_BYTE.
20487 But don't go beyond LIMIT_BYTE.
20488 Return the number of lines thus found (always nonnegative).
20489
20490 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
20491
20492 static EMACS_INT
20493 display_count_lines (EMACS_INT start_byte,
20494 EMACS_INT limit_byte, EMACS_INT count,
20495 EMACS_INT *byte_pos_ptr)
20496 {
20497 register unsigned char *cursor;
20498 unsigned char *base;
20499
20500 register EMACS_INT ceiling;
20501 register unsigned char *ceiling_addr;
20502 EMACS_INT orig_count = count;
20503
20504 /* If we are not in selective display mode,
20505 check only for newlines. */
20506 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
20507 && !INTEGERP (BVAR (current_buffer, selective_display)));
20508
20509 if (count > 0)
20510 {
20511 while (start_byte < limit_byte)
20512 {
20513 ceiling = BUFFER_CEILING_OF (start_byte);
20514 ceiling = min (limit_byte - 1, ceiling);
20515 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
20516 base = (cursor = BYTE_POS_ADDR (start_byte));
20517 while (1)
20518 {
20519 if (selective_display)
20520 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
20521 ;
20522 else
20523 while (*cursor != '\n' && ++cursor != ceiling_addr)
20524 ;
20525
20526 if (cursor != ceiling_addr)
20527 {
20528 if (--count == 0)
20529 {
20530 start_byte += cursor - base + 1;
20531 *byte_pos_ptr = start_byte;
20532 return orig_count;
20533 }
20534 else
20535 if (++cursor == ceiling_addr)
20536 break;
20537 }
20538 else
20539 break;
20540 }
20541 start_byte += cursor - base;
20542 }
20543 }
20544 else
20545 {
20546 while (start_byte > limit_byte)
20547 {
20548 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
20549 ceiling = max (limit_byte, ceiling);
20550 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
20551 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
20552 while (1)
20553 {
20554 if (selective_display)
20555 while (--cursor != ceiling_addr
20556 && *cursor != '\n' && *cursor != 015)
20557 ;
20558 else
20559 while (--cursor != ceiling_addr && *cursor != '\n')
20560 ;
20561
20562 if (cursor != ceiling_addr)
20563 {
20564 if (++count == 0)
20565 {
20566 start_byte += cursor - base + 1;
20567 *byte_pos_ptr = start_byte;
20568 /* When scanning backwards, we should
20569 not count the newline posterior to which we stop. */
20570 return - orig_count - 1;
20571 }
20572 }
20573 else
20574 break;
20575 }
20576 /* Here we add 1 to compensate for the last decrement
20577 of CURSOR, which took it past the valid range. */
20578 start_byte += cursor - base + 1;
20579 }
20580 }
20581
20582 *byte_pos_ptr = limit_byte;
20583
20584 if (count < 0)
20585 return - orig_count + count;
20586 return orig_count - count;
20587
20588 }
20589
20590
20591 \f
20592 /***********************************************************************
20593 Displaying strings
20594 ***********************************************************************/
20595
20596 /* Display a NUL-terminated string, starting with index START.
20597
20598 If STRING is non-null, display that C string. Otherwise, the Lisp
20599 string LISP_STRING is displayed. There's a case that STRING is
20600 non-null and LISP_STRING is not nil. It means STRING is a string
20601 data of LISP_STRING. In that case, we display LISP_STRING while
20602 ignoring its text properties.
20603
20604 If FACE_STRING is not nil, FACE_STRING_POS is a position in
20605 FACE_STRING. Display STRING or LISP_STRING with the face at
20606 FACE_STRING_POS in FACE_STRING:
20607
20608 Display the string in the environment given by IT, but use the
20609 standard display table, temporarily.
20610
20611 FIELD_WIDTH is the minimum number of output glyphs to produce.
20612 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20613 with spaces. If STRING has more characters, more than FIELD_WIDTH
20614 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
20615
20616 PRECISION is the maximum number of characters to output from
20617 STRING. PRECISION < 0 means don't truncate the string.
20618
20619 This is roughly equivalent to printf format specifiers:
20620
20621 FIELD_WIDTH PRECISION PRINTF
20622 ----------------------------------------
20623 -1 -1 %s
20624 -1 10 %.10s
20625 10 -1 %10s
20626 20 10 %20.10s
20627
20628 MULTIBYTE zero means do not display multibyte chars, > 0 means do
20629 display them, and < 0 means obey the current buffer's value of
20630 enable_multibyte_characters.
20631
20632 Value is the number of columns displayed. */
20633
20634 static int
20635 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
20636 EMACS_INT face_string_pos, EMACS_INT start, struct it *it,
20637 int field_width, int precision, int max_x, int multibyte)
20638 {
20639 int hpos_at_start = it->hpos;
20640 int saved_face_id = it->face_id;
20641 struct glyph_row *row = it->glyph_row;
20642 EMACS_INT it_charpos;
20643
20644 /* Initialize the iterator IT for iteration over STRING beginning
20645 with index START. */
20646 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
20647 precision, field_width, multibyte);
20648 if (string && STRINGP (lisp_string))
20649 /* LISP_STRING is the one returned by decode_mode_spec. We should
20650 ignore its text properties. */
20651 it->stop_charpos = it->end_charpos;
20652
20653 /* If displaying STRING, set up the face of the iterator from
20654 FACE_STRING, if that's given. */
20655 if (STRINGP (face_string))
20656 {
20657 EMACS_INT endptr;
20658 struct face *face;
20659
20660 it->face_id
20661 = face_at_string_position (it->w, face_string, face_string_pos,
20662 0, it->region_beg_charpos,
20663 it->region_end_charpos,
20664 &endptr, it->base_face_id, 0);
20665 face = FACE_FROM_ID (it->f, it->face_id);
20666 it->face_box_p = face->box != FACE_NO_BOX;
20667 }
20668
20669 /* Set max_x to the maximum allowed X position. Don't let it go
20670 beyond the right edge of the window. */
20671 if (max_x <= 0)
20672 max_x = it->last_visible_x;
20673 else
20674 max_x = min (max_x, it->last_visible_x);
20675
20676 /* Skip over display elements that are not visible. because IT->w is
20677 hscrolled. */
20678 if (it->current_x < it->first_visible_x)
20679 move_it_in_display_line_to (it, 100000, it->first_visible_x,
20680 MOVE_TO_POS | MOVE_TO_X);
20681
20682 row->ascent = it->max_ascent;
20683 row->height = it->max_ascent + it->max_descent;
20684 row->phys_ascent = it->max_phys_ascent;
20685 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
20686 row->extra_line_spacing = it->max_extra_line_spacing;
20687
20688 if (STRINGP (it->string))
20689 it_charpos = IT_STRING_CHARPOS (*it);
20690 else
20691 it_charpos = IT_CHARPOS (*it);
20692
20693 /* This condition is for the case that we are called with current_x
20694 past last_visible_x. */
20695 while (it->current_x < max_x)
20696 {
20697 int x_before, x, n_glyphs_before, i, nglyphs;
20698
20699 /* Get the next display element. */
20700 if (!get_next_display_element (it))
20701 break;
20702
20703 /* Produce glyphs. */
20704 x_before = it->current_x;
20705 n_glyphs_before = row->used[TEXT_AREA];
20706 PRODUCE_GLYPHS (it);
20707
20708 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
20709 i = 0;
20710 x = x_before;
20711 while (i < nglyphs)
20712 {
20713 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
20714
20715 if (it->line_wrap != TRUNCATE
20716 && x + glyph->pixel_width > max_x)
20717 {
20718 /* End of continued line or max_x reached. */
20719 if (CHAR_GLYPH_PADDING_P (*glyph))
20720 {
20721 /* A wide character is unbreakable. */
20722 if (row->reversed_p)
20723 unproduce_glyphs (it, row->used[TEXT_AREA]
20724 - n_glyphs_before);
20725 row->used[TEXT_AREA] = n_glyphs_before;
20726 it->current_x = x_before;
20727 }
20728 else
20729 {
20730 if (row->reversed_p)
20731 unproduce_glyphs (it, row->used[TEXT_AREA]
20732 - (n_glyphs_before + i));
20733 row->used[TEXT_AREA] = n_glyphs_before + i;
20734 it->current_x = x;
20735 }
20736 break;
20737 }
20738 else if (x + glyph->pixel_width >= it->first_visible_x)
20739 {
20740 /* Glyph is at least partially visible. */
20741 ++it->hpos;
20742 if (x < it->first_visible_x)
20743 row->x = x - it->first_visible_x;
20744 }
20745 else
20746 {
20747 /* Glyph is off the left margin of the display area.
20748 Should not happen. */
20749 abort ();
20750 }
20751
20752 row->ascent = max (row->ascent, it->max_ascent);
20753 row->height = max (row->height, it->max_ascent + it->max_descent);
20754 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
20755 row->phys_height = max (row->phys_height,
20756 it->max_phys_ascent + it->max_phys_descent);
20757 row->extra_line_spacing = max (row->extra_line_spacing,
20758 it->max_extra_line_spacing);
20759 x += glyph->pixel_width;
20760 ++i;
20761 }
20762
20763 /* Stop if max_x reached. */
20764 if (i < nglyphs)
20765 break;
20766
20767 /* Stop at line ends. */
20768 if (ITERATOR_AT_END_OF_LINE_P (it))
20769 {
20770 it->continuation_lines_width = 0;
20771 break;
20772 }
20773
20774 set_iterator_to_next (it, 1);
20775 if (STRINGP (it->string))
20776 it_charpos = IT_STRING_CHARPOS (*it);
20777 else
20778 it_charpos = IT_CHARPOS (*it);
20779
20780 /* Stop if truncating at the right edge. */
20781 if (it->line_wrap == TRUNCATE
20782 && it->current_x >= it->last_visible_x)
20783 {
20784 /* Add truncation mark, but don't do it if the line is
20785 truncated at a padding space. */
20786 if (it_charpos < it->string_nchars)
20787 {
20788 if (!FRAME_WINDOW_P (it->f))
20789 {
20790 int ii, n;
20791
20792 if (it->current_x > it->last_visible_x)
20793 {
20794 if (!row->reversed_p)
20795 {
20796 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
20797 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
20798 break;
20799 }
20800 else
20801 {
20802 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
20803 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
20804 break;
20805 unproduce_glyphs (it, ii + 1);
20806 ii = row->used[TEXT_AREA] - (ii + 1);
20807 }
20808 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
20809 {
20810 row->used[TEXT_AREA] = ii;
20811 produce_special_glyphs (it, IT_TRUNCATION);
20812 }
20813 }
20814 produce_special_glyphs (it, IT_TRUNCATION);
20815 }
20816 row->truncated_on_right_p = 1;
20817 }
20818 break;
20819 }
20820 }
20821
20822 /* Maybe insert a truncation at the left. */
20823 if (it->first_visible_x
20824 && it_charpos > 0)
20825 {
20826 if (!FRAME_WINDOW_P (it->f))
20827 insert_left_trunc_glyphs (it);
20828 row->truncated_on_left_p = 1;
20829 }
20830
20831 it->face_id = saved_face_id;
20832
20833 /* Value is number of columns displayed. */
20834 return it->hpos - hpos_at_start;
20835 }
20836
20837
20838 \f
20839 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
20840 appears as an element of LIST or as the car of an element of LIST.
20841 If PROPVAL is a list, compare each element against LIST in that
20842 way, and return 1/2 if any element of PROPVAL is found in LIST.
20843 Otherwise return 0. This function cannot quit.
20844 The return value is 2 if the text is invisible but with an ellipsis
20845 and 1 if it's invisible and without an ellipsis. */
20846
20847 int
20848 invisible_p (register Lisp_Object propval, Lisp_Object list)
20849 {
20850 register Lisp_Object tail, proptail;
20851
20852 for (tail = list; CONSP (tail); tail = XCDR (tail))
20853 {
20854 register Lisp_Object tem;
20855 tem = XCAR (tail);
20856 if (EQ (propval, tem))
20857 return 1;
20858 if (CONSP (tem) && EQ (propval, XCAR (tem)))
20859 return NILP (XCDR (tem)) ? 1 : 2;
20860 }
20861
20862 if (CONSP (propval))
20863 {
20864 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
20865 {
20866 Lisp_Object propelt;
20867 propelt = XCAR (proptail);
20868 for (tail = list; CONSP (tail); tail = XCDR (tail))
20869 {
20870 register Lisp_Object tem;
20871 tem = XCAR (tail);
20872 if (EQ (propelt, tem))
20873 return 1;
20874 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
20875 return NILP (XCDR (tem)) ? 1 : 2;
20876 }
20877 }
20878 }
20879
20880 return 0;
20881 }
20882
20883 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
20884 doc: /* Non-nil if the property makes the text invisible.
20885 POS-OR-PROP can be a marker or number, in which case it is taken to be
20886 a position in the current buffer and the value of the `invisible' property
20887 is checked; or it can be some other value, which is then presumed to be the
20888 value of the `invisible' property of the text of interest.
20889 The non-nil value returned can be t for truly invisible text or something
20890 else if the text is replaced by an ellipsis. */)
20891 (Lisp_Object pos_or_prop)
20892 {
20893 Lisp_Object prop
20894 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
20895 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
20896 : pos_or_prop);
20897 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
20898 return (invis == 0 ? Qnil
20899 : invis == 1 ? Qt
20900 : make_number (invis));
20901 }
20902
20903 /* Calculate a width or height in pixels from a specification using
20904 the following elements:
20905
20906 SPEC ::=
20907 NUM - a (fractional) multiple of the default font width/height
20908 (NUM) - specifies exactly NUM pixels
20909 UNIT - a fixed number of pixels, see below.
20910 ELEMENT - size of a display element in pixels, see below.
20911 (NUM . SPEC) - equals NUM * SPEC
20912 (+ SPEC SPEC ...) - add pixel values
20913 (- SPEC SPEC ...) - subtract pixel values
20914 (- SPEC) - negate pixel value
20915
20916 NUM ::=
20917 INT or FLOAT - a number constant
20918 SYMBOL - use symbol's (buffer local) variable binding.
20919
20920 UNIT ::=
20921 in - pixels per inch *)
20922 mm - pixels per 1/1000 meter *)
20923 cm - pixels per 1/100 meter *)
20924 width - width of current font in pixels.
20925 height - height of current font in pixels.
20926
20927 *) using the ratio(s) defined in display-pixels-per-inch.
20928
20929 ELEMENT ::=
20930
20931 left-fringe - left fringe width in pixels
20932 right-fringe - right fringe width in pixels
20933
20934 left-margin - left margin width in pixels
20935 right-margin - right margin width in pixels
20936
20937 scroll-bar - scroll-bar area width in pixels
20938
20939 Examples:
20940
20941 Pixels corresponding to 5 inches:
20942 (5 . in)
20943
20944 Total width of non-text areas on left side of window (if scroll-bar is on left):
20945 '(space :width (+ left-fringe left-margin scroll-bar))
20946
20947 Align to first text column (in header line):
20948 '(space :align-to 0)
20949
20950 Align to middle of text area minus half the width of variable `my-image'
20951 containing a loaded image:
20952 '(space :align-to (0.5 . (- text my-image)))
20953
20954 Width of left margin minus width of 1 character in the default font:
20955 '(space :width (- left-margin 1))
20956
20957 Width of left margin minus width of 2 characters in the current font:
20958 '(space :width (- left-margin (2 . width)))
20959
20960 Center 1 character over left-margin (in header line):
20961 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
20962
20963 Different ways to express width of left fringe plus left margin minus one pixel:
20964 '(space :width (- (+ left-fringe left-margin) (1)))
20965 '(space :width (+ left-fringe left-margin (- (1))))
20966 '(space :width (+ left-fringe left-margin (-1)))
20967
20968 */
20969
20970 #define NUMVAL(X) \
20971 ((INTEGERP (X) || FLOATP (X)) \
20972 ? XFLOATINT (X) \
20973 : - 1)
20974
20975 int
20976 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
20977 struct font *font, int width_p, int *align_to)
20978 {
20979 double pixels;
20980
20981 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
20982 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
20983
20984 if (NILP (prop))
20985 return OK_PIXELS (0);
20986
20987 xassert (FRAME_LIVE_P (it->f));
20988
20989 if (SYMBOLP (prop))
20990 {
20991 if (SCHARS (SYMBOL_NAME (prop)) == 2)
20992 {
20993 char *unit = SSDATA (SYMBOL_NAME (prop));
20994
20995 if (unit[0] == 'i' && unit[1] == 'n')
20996 pixels = 1.0;
20997 else if (unit[0] == 'm' && unit[1] == 'm')
20998 pixels = 25.4;
20999 else if (unit[0] == 'c' && unit[1] == 'm')
21000 pixels = 2.54;
21001 else
21002 pixels = 0;
21003 if (pixels > 0)
21004 {
21005 double ppi;
21006 #ifdef HAVE_WINDOW_SYSTEM
21007 if (FRAME_WINDOW_P (it->f)
21008 && (ppi = (width_p
21009 ? FRAME_X_DISPLAY_INFO (it->f)->resx
21010 : FRAME_X_DISPLAY_INFO (it->f)->resy),
21011 ppi > 0))
21012 return OK_PIXELS (ppi / pixels);
21013 #endif
21014
21015 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
21016 || (CONSP (Vdisplay_pixels_per_inch)
21017 && (ppi = (width_p
21018 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
21019 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
21020 ppi > 0)))
21021 return OK_PIXELS (ppi / pixels);
21022
21023 return 0;
21024 }
21025 }
21026
21027 #ifdef HAVE_WINDOW_SYSTEM
21028 if (EQ (prop, Qheight))
21029 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
21030 if (EQ (prop, Qwidth))
21031 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
21032 #else
21033 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
21034 return OK_PIXELS (1);
21035 #endif
21036
21037 if (EQ (prop, Qtext))
21038 return OK_PIXELS (width_p
21039 ? window_box_width (it->w, TEXT_AREA)
21040 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
21041
21042 if (align_to && *align_to < 0)
21043 {
21044 *res = 0;
21045 if (EQ (prop, Qleft))
21046 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
21047 if (EQ (prop, Qright))
21048 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
21049 if (EQ (prop, Qcenter))
21050 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
21051 + window_box_width (it->w, TEXT_AREA) / 2);
21052 if (EQ (prop, Qleft_fringe))
21053 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21054 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
21055 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
21056 if (EQ (prop, Qright_fringe))
21057 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21058 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21059 : window_box_right_offset (it->w, TEXT_AREA));
21060 if (EQ (prop, Qleft_margin))
21061 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
21062 if (EQ (prop, Qright_margin))
21063 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
21064 if (EQ (prop, Qscroll_bar))
21065 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
21066 ? 0
21067 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21068 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21069 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21070 : 0)));
21071 }
21072 else
21073 {
21074 if (EQ (prop, Qleft_fringe))
21075 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
21076 if (EQ (prop, Qright_fringe))
21077 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
21078 if (EQ (prop, Qleft_margin))
21079 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
21080 if (EQ (prop, Qright_margin))
21081 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
21082 if (EQ (prop, Qscroll_bar))
21083 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
21084 }
21085
21086 prop = Fbuffer_local_value (prop, it->w->buffer);
21087 }
21088
21089 if (INTEGERP (prop) || FLOATP (prop))
21090 {
21091 int base_unit = (width_p
21092 ? FRAME_COLUMN_WIDTH (it->f)
21093 : FRAME_LINE_HEIGHT (it->f));
21094 return OK_PIXELS (XFLOATINT (prop) * base_unit);
21095 }
21096
21097 if (CONSP (prop))
21098 {
21099 Lisp_Object car = XCAR (prop);
21100 Lisp_Object cdr = XCDR (prop);
21101
21102 if (SYMBOLP (car))
21103 {
21104 #ifdef HAVE_WINDOW_SYSTEM
21105 if (FRAME_WINDOW_P (it->f)
21106 && valid_image_p (prop))
21107 {
21108 int id = lookup_image (it->f, prop);
21109 struct image *img = IMAGE_FROM_ID (it->f, id);
21110
21111 return OK_PIXELS (width_p ? img->width : img->height);
21112 }
21113 #endif
21114 if (EQ (car, Qplus) || EQ (car, Qminus))
21115 {
21116 int first = 1;
21117 double px;
21118
21119 pixels = 0;
21120 while (CONSP (cdr))
21121 {
21122 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
21123 font, width_p, align_to))
21124 return 0;
21125 if (first)
21126 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
21127 else
21128 pixels += px;
21129 cdr = XCDR (cdr);
21130 }
21131 if (EQ (car, Qminus))
21132 pixels = -pixels;
21133 return OK_PIXELS (pixels);
21134 }
21135
21136 car = Fbuffer_local_value (car, it->w->buffer);
21137 }
21138
21139 if (INTEGERP (car) || FLOATP (car))
21140 {
21141 double fact;
21142 pixels = XFLOATINT (car);
21143 if (NILP (cdr))
21144 return OK_PIXELS (pixels);
21145 if (calc_pixel_width_or_height (&fact, it, cdr,
21146 font, width_p, align_to))
21147 return OK_PIXELS (pixels * fact);
21148 return 0;
21149 }
21150
21151 return 0;
21152 }
21153
21154 return 0;
21155 }
21156
21157 \f
21158 /***********************************************************************
21159 Glyph Display
21160 ***********************************************************************/
21161
21162 #ifdef HAVE_WINDOW_SYSTEM
21163
21164 #if GLYPH_DEBUG
21165
21166 void
21167 dump_glyph_string (s)
21168 struct glyph_string *s;
21169 {
21170 fprintf (stderr, "glyph string\n");
21171 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
21172 s->x, s->y, s->width, s->height);
21173 fprintf (stderr, " ybase = %d\n", s->ybase);
21174 fprintf (stderr, " hl = %d\n", s->hl);
21175 fprintf (stderr, " left overhang = %d, right = %d\n",
21176 s->left_overhang, s->right_overhang);
21177 fprintf (stderr, " nchars = %d\n", s->nchars);
21178 fprintf (stderr, " extends to end of line = %d\n",
21179 s->extends_to_end_of_line_p);
21180 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
21181 fprintf (stderr, " bg width = %d\n", s->background_width);
21182 }
21183
21184 #endif /* GLYPH_DEBUG */
21185
21186 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
21187 of XChar2b structures for S; it can't be allocated in
21188 init_glyph_string because it must be allocated via `alloca'. W
21189 is the window on which S is drawn. ROW and AREA are the glyph row
21190 and area within the row from which S is constructed. START is the
21191 index of the first glyph structure covered by S. HL is a
21192 face-override for drawing S. */
21193
21194 #ifdef HAVE_NTGUI
21195 #define OPTIONAL_HDC(hdc) HDC hdc,
21196 #define DECLARE_HDC(hdc) HDC hdc;
21197 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
21198 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
21199 #endif
21200
21201 #ifndef OPTIONAL_HDC
21202 #define OPTIONAL_HDC(hdc)
21203 #define DECLARE_HDC(hdc)
21204 #define ALLOCATE_HDC(hdc, f)
21205 #define RELEASE_HDC(hdc, f)
21206 #endif
21207
21208 static void
21209 init_glyph_string (struct glyph_string *s,
21210 OPTIONAL_HDC (hdc)
21211 XChar2b *char2b, struct window *w, struct glyph_row *row,
21212 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
21213 {
21214 memset (s, 0, sizeof *s);
21215 s->w = w;
21216 s->f = XFRAME (w->frame);
21217 #ifdef HAVE_NTGUI
21218 s->hdc = hdc;
21219 #endif
21220 s->display = FRAME_X_DISPLAY (s->f);
21221 s->window = FRAME_X_WINDOW (s->f);
21222 s->char2b = char2b;
21223 s->hl = hl;
21224 s->row = row;
21225 s->area = area;
21226 s->first_glyph = row->glyphs[area] + start;
21227 s->height = row->height;
21228 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
21229 s->ybase = s->y + row->ascent;
21230 }
21231
21232
21233 /* Append the list of glyph strings with head H and tail T to the list
21234 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
21235
21236 static INLINE void
21237 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21238 struct glyph_string *h, struct glyph_string *t)
21239 {
21240 if (h)
21241 {
21242 if (*head)
21243 (*tail)->next = h;
21244 else
21245 *head = h;
21246 h->prev = *tail;
21247 *tail = t;
21248 }
21249 }
21250
21251
21252 /* Prepend the list of glyph strings with head H and tail T to the
21253 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
21254 result. */
21255
21256 static INLINE void
21257 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21258 struct glyph_string *h, struct glyph_string *t)
21259 {
21260 if (h)
21261 {
21262 if (*head)
21263 (*head)->prev = t;
21264 else
21265 *tail = t;
21266 t->next = *head;
21267 *head = h;
21268 }
21269 }
21270
21271
21272 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
21273 Set *HEAD and *TAIL to the resulting list. */
21274
21275 static INLINE void
21276 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
21277 struct glyph_string *s)
21278 {
21279 s->next = s->prev = NULL;
21280 append_glyph_string_lists (head, tail, s, s);
21281 }
21282
21283
21284 /* Get face and two-byte form of character C in face FACE_ID on frame F.
21285 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
21286 make sure that X resources for the face returned are allocated.
21287 Value is a pointer to a realized face that is ready for display if
21288 DISPLAY_P is non-zero. */
21289
21290 static INLINE struct face *
21291 get_char_face_and_encoding (struct frame *f, int c, int face_id,
21292 XChar2b *char2b, int display_p)
21293 {
21294 struct face *face = FACE_FROM_ID (f, face_id);
21295
21296 if (face->font)
21297 {
21298 unsigned code = face->font->driver->encode_char (face->font, c);
21299
21300 if (code != FONT_INVALID_CODE)
21301 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21302 else
21303 STORE_XCHAR2B (char2b, 0, 0);
21304 }
21305
21306 /* Make sure X resources of the face are allocated. */
21307 #ifdef HAVE_X_WINDOWS
21308 if (display_p)
21309 #endif
21310 {
21311 xassert (face != NULL);
21312 PREPARE_FACE_FOR_DISPLAY (f, face);
21313 }
21314
21315 return face;
21316 }
21317
21318
21319 /* Get face and two-byte form of character glyph GLYPH on frame F.
21320 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
21321 a pointer to a realized face that is ready for display. */
21322
21323 static INLINE struct face *
21324 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
21325 XChar2b *char2b, int *two_byte_p)
21326 {
21327 struct face *face;
21328
21329 xassert (glyph->type == CHAR_GLYPH);
21330 face = FACE_FROM_ID (f, glyph->face_id);
21331
21332 if (two_byte_p)
21333 *two_byte_p = 0;
21334
21335 if (face->font)
21336 {
21337 unsigned code;
21338
21339 if (CHAR_BYTE8_P (glyph->u.ch))
21340 code = CHAR_TO_BYTE8 (glyph->u.ch);
21341 else
21342 code = face->font->driver->encode_char (face->font, glyph->u.ch);
21343
21344 if (code != FONT_INVALID_CODE)
21345 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21346 else
21347 STORE_XCHAR2B (char2b, 0, 0);
21348 }
21349
21350 /* Make sure X resources of the face are allocated. */
21351 xassert (face != NULL);
21352 PREPARE_FACE_FOR_DISPLAY (f, face);
21353 return face;
21354 }
21355
21356
21357 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
21358 Retunr 1 if FONT has a glyph for C, otherwise return 0. */
21359
21360 static INLINE int
21361 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
21362 {
21363 unsigned code;
21364
21365 if (CHAR_BYTE8_P (c))
21366 code = CHAR_TO_BYTE8 (c);
21367 else
21368 code = font->driver->encode_char (font, c);
21369
21370 if (code == FONT_INVALID_CODE)
21371 return 0;
21372 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
21373 return 1;
21374 }
21375
21376
21377 /* Fill glyph string S with composition components specified by S->cmp.
21378
21379 BASE_FACE is the base face of the composition.
21380 S->cmp_from is the index of the first component for S.
21381
21382 OVERLAPS non-zero means S should draw the foreground only, and use
21383 its physical height for clipping. See also draw_glyphs.
21384
21385 Value is the index of a component not in S. */
21386
21387 static int
21388 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
21389 int overlaps)
21390 {
21391 int i;
21392 /* For all glyphs of this composition, starting at the offset
21393 S->cmp_from, until we reach the end of the definition or encounter a
21394 glyph that requires the different face, add it to S. */
21395 struct face *face;
21396
21397 xassert (s);
21398
21399 s->for_overlaps = overlaps;
21400 s->face = NULL;
21401 s->font = NULL;
21402 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
21403 {
21404 int c = COMPOSITION_GLYPH (s->cmp, i);
21405
21406 if (c != '\t')
21407 {
21408 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
21409 -1, Qnil);
21410
21411 face = get_char_face_and_encoding (s->f, c, face_id,
21412 s->char2b + i, 1);
21413 if (face)
21414 {
21415 if (! s->face)
21416 {
21417 s->face = face;
21418 s->font = s->face->font;
21419 }
21420 else if (s->face != face)
21421 break;
21422 }
21423 }
21424 ++s->nchars;
21425 }
21426 s->cmp_to = i;
21427
21428 /* All glyph strings for the same composition has the same width,
21429 i.e. the width set for the first component of the composition. */
21430 s->width = s->first_glyph->pixel_width;
21431
21432 /* If the specified font could not be loaded, use the frame's
21433 default font, but record the fact that we couldn't load it in
21434 the glyph string so that we can draw rectangles for the
21435 characters of the glyph string. */
21436 if (s->font == NULL)
21437 {
21438 s->font_not_found_p = 1;
21439 s->font = FRAME_FONT (s->f);
21440 }
21441
21442 /* Adjust base line for subscript/superscript text. */
21443 s->ybase += s->first_glyph->voffset;
21444
21445 /* This glyph string must always be drawn with 16-bit functions. */
21446 s->two_byte_p = 1;
21447
21448 return s->cmp_to;
21449 }
21450
21451 static int
21452 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
21453 int start, int end, int overlaps)
21454 {
21455 struct glyph *glyph, *last;
21456 Lisp_Object lgstring;
21457 int i;
21458
21459 s->for_overlaps = overlaps;
21460 glyph = s->row->glyphs[s->area] + start;
21461 last = s->row->glyphs[s->area] + end;
21462 s->cmp_id = glyph->u.cmp.id;
21463 s->cmp_from = glyph->slice.cmp.from;
21464 s->cmp_to = glyph->slice.cmp.to + 1;
21465 s->face = FACE_FROM_ID (s->f, face_id);
21466 lgstring = composition_gstring_from_id (s->cmp_id);
21467 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
21468 glyph++;
21469 while (glyph < last
21470 && glyph->u.cmp.automatic
21471 && glyph->u.cmp.id == s->cmp_id
21472 && s->cmp_to == glyph->slice.cmp.from)
21473 s->cmp_to = (glyph++)->slice.cmp.to + 1;
21474
21475 for (i = s->cmp_from; i < s->cmp_to; i++)
21476 {
21477 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
21478 unsigned code = LGLYPH_CODE (lglyph);
21479
21480 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
21481 }
21482 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
21483 return glyph - s->row->glyphs[s->area];
21484 }
21485
21486
21487 /* Fill glyph string S from a sequence glyphs for glyphless characters.
21488 See the comment of fill_glyph_string for arguments.
21489 Value is the index of the first glyph not in S. */
21490
21491
21492 static int
21493 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
21494 int start, int end, int overlaps)
21495 {
21496 struct glyph *glyph, *last;
21497 int voffset;
21498
21499 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
21500 s->for_overlaps = overlaps;
21501 glyph = s->row->glyphs[s->area] + start;
21502 last = s->row->glyphs[s->area] + end;
21503 voffset = glyph->voffset;
21504 s->face = FACE_FROM_ID (s->f, face_id);
21505 s->font = s->face->font;
21506 s->nchars = 1;
21507 s->width = glyph->pixel_width;
21508 glyph++;
21509 while (glyph < last
21510 && glyph->type == GLYPHLESS_GLYPH
21511 && glyph->voffset == voffset
21512 && glyph->face_id == face_id)
21513 {
21514 s->nchars++;
21515 s->width += glyph->pixel_width;
21516 glyph++;
21517 }
21518 s->ybase += voffset;
21519 return glyph - s->row->glyphs[s->area];
21520 }
21521
21522
21523 /* Fill glyph string S from a sequence of character glyphs.
21524
21525 FACE_ID is the face id of the string. START is the index of the
21526 first glyph to consider, END is the index of the last + 1.
21527 OVERLAPS non-zero means S should draw the foreground only, and use
21528 its physical height for clipping. See also draw_glyphs.
21529
21530 Value is the index of the first glyph not in S. */
21531
21532 static int
21533 fill_glyph_string (struct glyph_string *s, int face_id,
21534 int start, int end, int overlaps)
21535 {
21536 struct glyph *glyph, *last;
21537 int voffset;
21538 int glyph_not_available_p;
21539
21540 xassert (s->f == XFRAME (s->w->frame));
21541 xassert (s->nchars == 0);
21542 xassert (start >= 0 && end > start);
21543
21544 s->for_overlaps = overlaps;
21545 glyph = s->row->glyphs[s->area] + start;
21546 last = s->row->glyphs[s->area] + end;
21547 voffset = glyph->voffset;
21548 s->padding_p = glyph->padding_p;
21549 glyph_not_available_p = glyph->glyph_not_available_p;
21550
21551 while (glyph < last
21552 && glyph->type == CHAR_GLYPH
21553 && glyph->voffset == voffset
21554 /* Same face id implies same font, nowadays. */
21555 && glyph->face_id == face_id
21556 && glyph->glyph_not_available_p == glyph_not_available_p)
21557 {
21558 int two_byte_p;
21559
21560 s->face = get_glyph_face_and_encoding (s->f, glyph,
21561 s->char2b + s->nchars,
21562 &two_byte_p);
21563 s->two_byte_p = two_byte_p;
21564 ++s->nchars;
21565 xassert (s->nchars <= end - start);
21566 s->width += glyph->pixel_width;
21567 if (glyph++->padding_p != s->padding_p)
21568 break;
21569 }
21570
21571 s->font = s->face->font;
21572
21573 /* If the specified font could not be loaded, use the frame's font,
21574 but record the fact that we couldn't load it in
21575 S->font_not_found_p so that we can draw rectangles for the
21576 characters of the glyph string. */
21577 if (s->font == NULL || glyph_not_available_p)
21578 {
21579 s->font_not_found_p = 1;
21580 s->font = FRAME_FONT (s->f);
21581 }
21582
21583 /* Adjust base line for subscript/superscript text. */
21584 s->ybase += voffset;
21585
21586 xassert (s->face && s->face->gc);
21587 return glyph - s->row->glyphs[s->area];
21588 }
21589
21590
21591 /* Fill glyph string S from image glyph S->first_glyph. */
21592
21593 static void
21594 fill_image_glyph_string (struct glyph_string *s)
21595 {
21596 xassert (s->first_glyph->type == IMAGE_GLYPH);
21597 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
21598 xassert (s->img);
21599 s->slice = s->first_glyph->slice.img;
21600 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
21601 s->font = s->face->font;
21602 s->width = s->first_glyph->pixel_width;
21603
21604 /* Adjust base line for subscript/superscript text. */
21605 s->ybase += s->first_glyph->voffset;
21606 }
21607
21608
21609 /* Fill glyph string S from a sequence of stretch glyphs.
21610
21611 START is the index of the first glyph to consider,
21612 END is the index of the last + 1.
21613
21614 Value is the index of the first glyph not in S. */
21615
21616 static int
21617 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
21618 {
21619 struct glyph *glyph, *last;
21620 int voffset, face_id;
21621
21622 xassert (s->first_glyph->type == STRETCH_GLYPH);
21623
21624 glyph = s->row->glyphs[s->area] + start;
21625 last = s->row->glyphs[s->area] + end;
21626 face_id = glyph->face_id;
21627 s->face = FACE_FROM_ID (s->f, face_id);
21628 s->font = s->face->font;
21629 s->width = glyph->pixel_width;
21630 s->nchars = 1;
21631 voffset = glyph->voffset;
21632
21633 for (++glyph;
21634 (glyph < last
21635 && glyph->type == STRETCH_GLYPH
21636 && glyph->voffset == voffset
21637 && glyph->face_id == face_id);
21638 ++glyph)
21639 s->width += glyph->pixel_width;
21640
21641 /* Adjust base line for subscript/superscript text. */
21642 s->ybase += voffset;
21643
21644 /* The case that face->gc == 0 is handled when drawing the glyph
21645 string by calling PREPARE_FACE_FOR_DISPLAY. */
21646 xassert (s->face);
21647 return glyph - s->row->glyphs[s->area];
21648 }
21649
21650 static struct font_metrics *
21651 get_per_char_metric (struct font *font, XChar2b *char2b)
21652 {
21653 static struct font_metrics metrics;
21654 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
21655
21656 if (! font || code == FONT_INVALID_CODE)
21657 return NULL;
21658 font->driver->text_extents (font, &code, 1, &metrics);
21659 return &metrics;
21660 }
21661
21662 /* EXPORT for RIF:
21663 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
21664 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
21665 assumed to be zero. */
21666
21667 void
21668 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
21669 {
21670 *left = *right = 0;
21671
21672 if (glyph->type == CHAR_GLYPH)
21673 {
21674 struct face *face;
21675 XChar2b char2b;
21676 struct font_metrics *pcm;
21677
21678 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
21679 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
21680 {
21681 if (pcm->rbearing > pcm->width)
21682 *right = pcm->rbearing - pcm->width;
21683 if (pcm->lbearing < 0)
21684 *left = -pcm->lbearing;
21685 }
21686 }
21687 else if (glyph->type == COMPOSITE_GLYPH)
21688 {
21689 if (! glyph->u.cmp.automatic)
21690 {
21691 struct composition *cmp = composition_table[glyph->u.cmp.id];
21692
21693 if (cmp->rbearing > cmp->pixel_width)
21694 *right = cmp->rbearing - cmp->pixel_width;
21695 if (cmp->lbearing < 0)
21696 *left = - cmp->lbearing;
21697 }
21698 else
21699 {
21700 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
21701 struct font_metrics metrics;
21702
21703 composition_gstring_width (gstring, glyph->slice.cmp.from,
21704 glyph->slice.cmp.to + 1, &metrics);
21705 if (metrics.rbearing > metrics.width)
21706 *right = metrics.rbearing - metrics.width;
21707 if (metrics.lbearing < 0)
21708 *left = - metrics.lbearing;
21709 }
21710 }
21711 }
21712
21713
21714 /* Return the index of the first glyph preceding glyph string S that
21715 is overwritten by S because of S's left overhang. Value is -1
21716 if no glyphs are overwritten. */
21717
21718 static int
21719 left_overwritten (struct glyph_string *s)
21720 {
21721 int k;
21722
21723 if (s->left_overhang)
21724 {
21725 int x = 0, i;
21726 struct glyph *glyphs = s->row->glyphs[s->area];
21727 int first = s->first_glyph - glyphs;
21728
21729 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
21730 x -= glyphs[i].pixel_width;
21731
21732 k = i + 1;
21733 }
21734 else
21735 k = -1;
21736
21737 return k;
21738 }
21739
21740
21741 /* Return the index of the first glyph preceding glyph string S that
21742 is overwriting S because of its right overhang. Value is -1 if no
21743 glyph in front of S overwrites S. */
21744
21745 static int
21746 left_overwriting (struct glyph_string *s)
21747 {
21748 int i, k, x;
21749 struct glyph *glyphs = s->row->glyphs[s->area];
21750 int first = s->first_glyph - glyphs;
21751
21752 k = -1;
21753 x = 0;
21754 for (i = first - 1; i >= 0; --i)
21755 {
21756 int left, right;
21757 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21758 if (x + right > 0)
21759 k = i;
21760 x -= glyphs[i].pixel_width;
21761 }
21762
21763 return k;
21764 }
21765
21766
21767 /* Return the index of the last glyph following glyph string S that is
21768 overwritten by S because of S's right overhang. Value is -1 if
21769 no such glyph is found. */
21770
21771 static int
21772 right_overwritten (struct glyph_string *s)
21773 {
21774 int k = -1;
21775
21776 if (s->right_overhang)
21777 {
21778 int x = 0, i;
21779 struct glyph *glyphs = s->row->glyphs[s->area];
21780 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21781 int end = s->row->used[s->area];
21782
21783 for (i = first; i < end && s->right_overhang > x; ++i)
21784 x += glyphs[i].pixel_width;
21785
21786 k = i;
21787 }
21788
21789 return k;
21790 }
21791
21792
21793 /* Return the index of the last glyph following glyph string S that
21794 overwrites S because of its left overhang. Value is negative
21795 if no such glyph is found. */
21796
21797 static int
21798 right_overwriting (struct glyph_string *s)
21799 {
21800 int i, k, x;
21801 int end = s->row->used[s->area];
21802 struct glyph *glyphs = s->row->glyphs[s->area];
21803 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
21804
21805 k = -1;
21806 x = 0;
21807 for (i = first; i < end; ++i)
21808 {
21809 int left, right;
21810 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
21811 if (x - left < 0)
21812 k = i;
21813 x += glyphs[i].pixel_width;
21814 }
21815
21816 return k;
21817 }
21818
21819
21820 /* Set background width of glyph string S. START is the index of the
21821 first glyph following S. LAST_X is the right-most x-position + 1
21822 in the drawing area. */
21823
21824 static INLINE void
21825 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
21826 {
21827 /* If the face of this glyph string has to be drawn to the end of
21828 the drawing area, set S->extends_to_end_of_line_p. */
21829
21830 if (start == s->row->used[s->area]
21831 && s->area == TEXT_AREA
21832 && ((s->row->fill_line_p
21833 && (s->hl == DRAW_NORMAL_TEXT
21834 || s->hl == DRAW_IMAGE_RAISED
21835 || s->hl == DRAW_IMAGE_SUNKEN))
21836 || s->hl == DRAW_MOUSE_FACE))
21837 s->extends_to_end_of_line_p = 1;
21838
21839 /* If S extends its face to the end of the line, set its
21840 background_width to the distance to the right edge of the drawing
21841 area. */
21842 if (s->extends_to_end_of_line_p)
21843 s->background_width = last_x - s->x + 1;
21844 else
21845 s->background_width = s->width;
21846 }
21847
21848
21849 /* Compute overhangs and x-positions for glyph string S and its
21850 predecessors, or successors. X is the starting x-position for S.
21851 BACKWARD_P non-zero means process predecessors. */
21852
21853 static void
21854 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
21855 {
21856 if (backward_p)
21857 {
21858 while (s)
21859 {
21860 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21861 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21862 x -= s->width;
21863 s->x = x;
21864 s = s->prev;
21865 }
21866 }
21867 else
21868 {
21869 while (s)
21870 {
21871 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
21872 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
21873 s->x = x;
21874 x += s->width;
21875 s = s->next;
21876 }
21877 }
21878 }
21879
21880
21881
21882 /* The following macros are only called from draw_glyphs below.
21883 They reference the following parameters of that function directly:
21884 `w', `row', `area', and `overlap_p'
21885 as well as the following local variables:
21886 `s', `f', and `hdc' (in W32) */
21887
21888 #ifdef HAVE_NTGUI
21889 /* On W32, silently add local `hdc' variable to argument list of
21890 init_glyph_string. */
21891 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21892 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
21893 #else
21894 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
21895 init_glyph_string (s, char2b, w, row, area, start, hl)
21896 #endif
21897
21898 /* Add a glyph string for a stretch glyph to the list of strings
21899 between HEAD and TAIL. START is the index of the stretch glyph in
21900 row area AREA of glyph row ROW. END is the index of the last glyph
21901 in that glyph row area. X is the current output position assigned
21902 to the new glyph string constructed. HL overrides that face of the
21903 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21904 is the right-most x-position of the drawing area. */
21905
21906 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
21907 and below -- keep them on one line. */
21908 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21909 do \
21910 { \
21911 s = (struct glyph_string *) alloca (sizeof *s); \
21912 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21913 START = fill_stretch_glyph_string (s, START, END); \
21914 append_glyph_string (&HEAD, &TAIL, s); \
21915 s->x = (X); \
21916 } \
21917 while (0)
21918
21919
21920 /* Add a glyph string for an image glyph to the list of strings
21921 between HEAD and TAIL. START is the index of the image glyph in
21922 row area AREA of glyph row ROW. END is the index of the last glyph
21923 in that glyph row area. X is the current output position assigned
21924 to the new glyph string constructed. HL overrides that face of the
21925 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
21926 is the right-most x-position of the drawing area. */
21927
21928 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21929 do \
21930 { \
21931 s = (struct glyph_string *) alloca (sizeof *s); \
21932 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
21933 fill_image_glyph_string (s); \
21934 append_glyph_string (&HEAD, &TAIL, s); \
21935 ++START; \
21936 s->x = (X); \
21937 } \
21938 while (0)
21939
21940
21941 /* Add a glyph string for a sequence of character glyphs to the list
21942 of strings between HEAD and TAIL. START is the index of the first
21943 glyph in row area AREA of glyph row ROW that is part of the new
21944 glyph string. END is the index of the last glyph in that glyph row
21945 area. X is the current output position assigned to the new glyph
21946 string constructed. HL overrides that face of the glyph; e.g. it
21947 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
21948 right-most x-position of the drawing area. */
21949
21950 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
21951 do \
21952 { \
21953 int face_id; \
21954 XChar2b *char2b; \
21955 \
21956 face_id = (row)->glyphs[area][START].face_id; \
21957 \
21958 s = (struct glyph_string *) alloca (sizeof *s); \
21959 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
21960 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21961 append_glyph_string (&HEAD, &TAIL, s); \
21962 s->x = (X); \
21963 START = fill_glyph_string (s, face_id, START, END, overlaps); \
21964 } \
21965 while (0)
21966
21967
21968 /* Add a glyph string for a composite sequence to the list of strings
21969 between HEAD and TAIL. START is the index of the first glyph in
21970 row area AREA of glyph row ROW that is part of the new glyph
21971 string. END is the index of the last glyph in that glyph row area.
21972 X is the current output position assigned to the new glyph string
21973 constructed. HL overrides that face of the glyph; e.g. it is
21974 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
21975 x-position of the drawing area. */
21976
21977 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
21978 do { \
21979 int face_id = (row)->glyphs[area][START].face_id; \
21980 struct face *base_face = FACE_FROM_ID (f, face_id); \
21981 int cmp_id = (row)->glyphs[area][START].u.cmp.id; \
21982 struct composition *cmp = composition_table[cmp_id]; \
21983 XChar2b *char2b; \
21984 struct glyph_string *first_s IF_LINT (= NULL); \
21985 int n; \
21986 \
21987 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
21988 \
21989 /* Make glyph_strings for each glyph sequence that is drawable by \
21990 the same face, and append them to HEAD/TAIL. */ \
21991 for (n = 0; n < cmp->glyph_len;) \
21992 { \
21993 s = (struct glyph_string *) alloca (sizeof *s); \
21994 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
21995 append_glyph_string (&(HEAD), &(TAIL), s); \
21996 s->cmp = cmp; \
21997 s->cmp_from = n; \
21998 s->x = (X); \
21999 if (n == 0) \
22000 first_s = s; \
22001 n = fill_composite_glyph_string (s, base_face, overlaps); \
22002 } \
22003 \
22004 ++START; \
22005 s = first_s; \
22006 } while (0)
22007
22008
22009 /* Add a glyph string for a glyph-string sequence to the list of strings
22010 between HEAD and TAIL. */
22011
22012 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22013 do { \
22014 int face_id; \
22015 XChar2b *char2b; \
22016 Lisp_Object gstring; \
22017 \
22018 face_id = (row)->glyphs[area][START].face_id; \
22019 gstring = (composition_gstring_from_id \
22020 ((row)->glyphs[area][START].u.cmp.id)); \
22021 s = (struct glyph_string *) alloca (sizeof *s); \
22022 char2b = (XChar2b *) alloca ((sizeof *char2b) \
22023 * LGSTRING_GLYPH_LEN (gstring)); \
22024 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22025 append_glyph_string (&(HEAD), &(TAIL), s); \
22026 s->x = (X); \
22027 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
22028 } while (0)
22029
22030
22031 /* Add a glyph string for a sequence of glyphless character's glyphs
22032 to the list of strings between HEAD and TAIL. The meanings of
22033 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
22034
22035 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22036 do \
22037 { \
22038 int face_id; \
22039 \
22040 face_id = (row)->glyphs[area][START].face_id; \
22041 \
22042 s = (struct glyph_string *) alloca (sizeof *s); \
22043 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22044 append_glyph_string (&HEAD, &TAIL, s); \
22045 s->x = (X); \
22046 START = fill_glyphless_glyph_string (s, face_id, START, END, \
22047 overlaps); \
22048 } \
22049 while (0)
22050
22051
22052 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
22053 of AREA of glyph row ROW on window W between indices START and END.
22054 HL overrides the face for drawing glyph strings, e.g. it is
22055 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
22056 x-positions of the drawing area.
22057
22058 This is an ugly monster macro construct because we must use alloca
22059 to allocate glyph strings (because draw_glyphs can be called
22060 asynchronously). */
22061
22062 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22063 do \
22064 { \
22065 HEAD = TAIL = NULL; \
22066 while (START < END) \
22067 { \
22068 struct glyph *first_glyph = (row)->glyphs[area] + START; \
22069 switch (first_glyph->type) \
22070 { \
22071 case CHAR_GLYPH: \
22072 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
22073 HL, X, LAST_X); \
22074 break; \
22075 \
22076 case COMPOSITE_GLYPH: \
22077 if (first_glyph->u.cmp.automatic) \
22078 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
22079 HL, X, LAST_X); \
22080 else \
22081 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
22082 HL, X, LAST_X); \
22083 break; \
22084 \
22085 case STRETCH_GLYPH: \
22086 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
22087 HL, X, LAST_X); \
22088 break; \
22089 \
22090 case IMAGE_GLYPH: \
22091 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
22092 HL, X, LAST_X); \
22093 break; \
22094 \
22095 case GLYPHLESS_GLYPH: \
22096 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
22097 HL, X, LAST_X); \
22098 break; \
22099 \
22100 default: \
22101 abort (); \
22102 } \
22103 \
22104 if (s) \
22105 { \
22106 set_glyph_string_background_width (s, START, LAST_X); \
22107 (X) += s->width; \
22108 } \
22109 } \
22110 } while (0)
22111
22112
22113 /* Draw glyphs between START and END in AREA of ROW on window W,
22114 starting at x-position X. X is relative to AREA in W. HL is a
22115 face-override with the following meaning:
22116
22117 DRAW_NORMAL_TEXT draw normally
22118 DRAW_CURSOR draw in cursor face
22119 DRAW_MOUSE_FACE draw in mouse face.
22120 DRAW_INVERSE_VIDEO draw in mode line face
22121 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
22122 DRAW_IMAGE_RAISED draw an image with a raised relief around it
22123
22124 If OVERLAPS is non-zero, draw only the foreground of characters and
22125 clip to the physical height of ROW. Non-zero value also defines
22126 the overlapping part to be drawn:
22127
22128 OVERLAPS_PRED overlap with preceding rows
22129 OVERLAPS_SUCC overlap with succeeding rows
22130 OVERLAPS_BOTH overlap with both preceding/succeeding rows
22131 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
22132
22133 Value is the x-position reached, relative to AREA of W. */
22134
22135 static int
22136 draw_glyphs (struct window *w, int x, struct glyph_row *row,
22137 enum glyph_row_area area, EMACS_INT start, EMACS_INT end,
22138 enum draw_glyphs_face hl, int overlaps)
22139 {
22140 struct glyph_string *head, *tail;
22141 struct glyph_string *s;
22142 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
22143 int i, j, x_reached, last_x, area_left = 0;
22144 struct frame *f = XFRAME (WINDOW_FRAME (w));
22145 DECLARE_HDC (hdc);
22146
22147 ALLOCATE_HDC (hdc, f);
22148
22149 /* Let's rather be paranoid than getting a SEGV. */
22150 end = min (end, row->used[area]);
22151 start = max (0, start);
22152 start = min (end, start);
22153
22154 /* Translate X to frame coordinates. Set last_x to the right
22155 end of the drawing area. */
22156 if (row->full_width_p)
22157 {
22158 /* X is relative to the left edge of W, without scroll bars
22159 or fringes. */
22160 area_left = WINDOW_LEFT_EDGE_X (w);
22161 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
22162 }
22163 else
22164 {
22165 area_left = window_box_left (w, area);
22166 last_x = area_left + window_box_width (w, area);
22167 }
22168 x += area_left;
22169
22170 /* Build a doubly-linked list of glyph_string structures between
22171 head and tail from what we have to draw. Note that the macro
22172 BUILD_GLYPH_STRINGS will modify its start parameter. That's
22173 the reason we use a separate variable `i'. */
22174 i = start;
22175 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
22176 if (tail)
22177 x_reached = tail->x + tail->background_width;
22178 else
22179 x_reached = x;
22180
22181 /* If there are any glyphs with lbearing < 0 or rbearing > width in
22182 the row, redraw some glyphs in front or following the glyph
22183 strings built above. */
22184 if (head && !overlaps && row->contains_overlapping_glyphs_p)
22185 {
22186 struct glyph_string *h, *t;
22187 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
22188 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
22189 int check_mouse_face = 0;
22190 int dummy_x = 0;
22191
22192 /* If mouse highlighting is on, we may need to draw adjacent
22193 glyphs using mouse-face highlighting. */
22194 if (area == TEXT_AREA && row->mouse_face_p)
22195 {
22196 struct glyph_row *mouse_beg_row, *mouse_end_row;
22197
22198 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
22199 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
22200
22201 if (row >= mouse_beg_row && row <= mouse_end_row)
22202 {
22203 check_mouse_face = 1;
22204 mouse_beg_col = (row == mouse_beg_row)
22205 ? hlinfo->mouse_face_beg_col : 0;
22206 mouse_end_col = (row == mouse_end_row)
22207 ? hlinfo->mouse_face_end_col
22208 : row->used[TEXT_AREA];
22209 }
22210 }
22211
22212 /* Compute overhangs for all glyph strings. */
22213 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
22214 for (s = head; s; s = s->next)
22215 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
22216
22217 /* Prepend glyph strings for glyphs in front of the first glyph
22218 string that are overwritten because of the first glyph
22219 string's left overhang. The background of all strings
22220 prepended must be drawn because the first glyph string
22221 draws over it. */
22222 i = left_overwritten (head);
22223 if (i >= 0)
22224 {
22225 enum draw_glyphs_face overlap_hl;
22226
22227 /* If this row contains mouse highlighting, attempt to draw
22228 the overlapped glyphs with the correct highlight. This
22229 code fails if the overlap encompasses more than one glyph
22230 and mouse-highlight spans only some of these glyphs.
22231 However, making it work perfectly involves a lot more
22232 code, and I don't know if the pathological case occurs in
22233 practice, so we'll stick to this for now. --- cyd */
22234 if (check_mouse_face
22235 && mouse_beg_col < start && mouse_end_col > i)
22236 overlap_hl = DRAW_MOUSE_FACE;
22237 else
22238 overlap_hl = DRAW_NORMAL_TEXT;
22239
22240 j = i;
22241 BUILD_GLYPH_STRINGS (j, start, h, t,
22242 overlap_hl, dummy_x, last_x);
22243 start = i;
22244 compute_overhangs_and_x (t, head->x, 1);
22245 prepend_glyph_string_lists (&head, &tail, h, t);
22246 clip_head = head;
22247 }
22248
22249 /* Prepend glyph strings for glyphs in front of the first glyph
22250 string that overwrite that glyph string because of their
22251 right overhang. For these strings, only the foreground must
22252 be drawn, because it draws over the glyph string at `head'.
22253 The background must not be drawn because this would overwrite
22254 right overhangs of preceding glyphs for which no glyph
22255 strings exist. */
22256 i = left_overwriting (head);
22257 if (i >= 0)
22258 {
22259 enum draw_glyphs_face overlap_hl;
22260
22261 if (check_mouse_face
22262 && mouse_beg_col < start && mouse_end_col > i)
22263 overlap_hl = DRAW_MOUSE_FACE;
22264 else
22265 overlap_hl = DRAW_NORMAL_TEXT;
22266
22267 clip_head = head;
22268 BUILD_GLYPH_STRINGS (i, start, h, t,
22269 overlap_hl, dummy_x, last_x);
22270 for (s = h; s; s = s->next)
22271 s->background_filled_p = 1;
22272 compute_overhangs_and_x (t, head->x, 1);
22273 prepend_glyph_string_lists (&head, &tail, h, t);
22274 }
22275
22276 /* Append glyphs strings for glyphs following the last glyph
22277 string tail that are overwritten by tail. The background of
22278 these strings has to be drawn because tail's foreground draws
22279 over it. */
22280 i = right_overwritten (tail);
22281 if (i >= 0)
22282 {
22283 enum draw_glyphs_face overlap_hl;
22284
22285 if (check_mouse_face
22286 && mouse_beg_col < i && mouse_end_col > end)
22287 overlap_hl = DRAW_MOUSE_FACE;
22288 else
22289 overlap_hl = DRAW_NORMAL_TEXT;
22290
22291 BUILD_GLYPH_STRINGS (end, i, h, t,
22292 overlap_hl, x, last_x);
22293 /* Because BUILD_GLYPH_STRINGS updates the first argument,
22294 we don't have `end = i;' here. */
22295 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22296 append_glyph_string_lists (&head, &tail, h, t);
22297 clip_tail = tail;
22298 }
22299
22300 /* Append glyph strings for glyphs following the last glyph
22301 string tail that overwrite tail. The foreground of such
22302 glyphs has to be drawn because it writes into the background
22303 of tail. The background must not be drawn because it could
22304 paint over the foreground of following glyphs. */
22305 i = right_overwriting (tail);
22306 if (i >= 0)
22307 {
22308 enum draw_glyphs_face overlap_hl;
22309 if (check_mouse_face
22310 && mouse_beg_col < i && mouse_end_col > end)
22311 overlap_hl = DRAW_MOUSE_FACE;
22312 else
22313 overlap_hl = DRAW_NORMAL_TEXT;
22314
22315 clip_tail = tail;
22316 i++; /* We must include the Ith glyph. */
22317 BUILD_GLYPH_STRINGS (end, i, h, t,
22318 overlap_hl, x, last_x);
22319 for (s = h; s; s = s->next)
22320 s->background_filled_p = 1;
22321 compute_overhangs_and_x (h, tail->x + tail->width, 0);
22322 append_glyph_string_lists (&head, &tail, h, t);
22323 }
22324 if (clip_head || clip_tail)
22325 for (s = head; s; s = s->next)
22326 {
22327 s->clip_head = clip_head;
22328 s->clip_tail = clip_tail;
22329 }
22330 }
22331
22332 /* Draw all strings. */
22333 for (s = head; s; s = s->next)
22334 FRAME_RIF (f)->draw_glyph_string (s);
22335
22336 #ifndef HAVE_NS
22337 /* When focus a sole frame and move horizontally, this sets on_p to 0
22338 causing a failure to erase prev cursor position. */
22339 if (area == TEXT_AREA
22340 && !row->full_width_p
22341 /* When drawing overlapping rows, only the glyph strings'
22342 foreground is drawn, which doesn't erase a cursor
22343 completely. */
22344 && !overlaps)
22345 {
22346 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
22347 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
22348 : (tail ? tail->x + tail->background_width : x));
22349 x0 -= area_left;
22350 x1 -= area_left;
22351
22352 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
22353 row->y, MATRIX_ROW_BOTTOM_Y (row));
22354 }
22355 #endif
22356
22357 /* Value is the x-position up to which drawn, relative to AREA of W.
22358 This doesn't include parts drawn because of overhangs. */
22359 if (row->full_width_p)
22360 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
22361 else
22362 x_reached -= area_left;
22363
22364 RELEASE_HDC (hdc, f);
22365
22366 return x_reached;
22367 }
22368
22369 /* Expand row matrix if too narrow. Don't expand if area
22370 is not present. */
22371
22372 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
22373 { \
22374 if (!fonts_changed_p \
22375 && (it->glyph_row->glyphs[area] \
22376 < it->glyph_row->glyphs[area + 1])) \
22377 { \
22378 it->w->ncols_scale_factor++; \
22379 fonts_changed_p = 1; \
22380 } \
22381 }
22382
22383 /* Store one glyph for IT->char_to_display in IT->glyph_row.
22384 Called from x_produce_glyphs when IT->glyph_row is non-null. */
22385
22386 static INLINE void
22387 append_glyph (struct it *it)
22388 {
22389 struct glyph *glyph;
22390 enum glyph_row_area area = it->area;
22391
22392 xassert (it->glyph_row);
22393 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
22394
22395 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22396 if (glyph < it->glyph_row->glyphs[area + 1])
22397 {
22398 /* If the glyph row is reversed, we need to prepend the glyph
22399 rather than append it. */
22400 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22401 {
22402 struct glyph *g;
22403
22404 /* Make room for the additional glyph. */
22405 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22406 g[1] = *g;
22407 glyph = it->glyph_row->glyphs[area];
22408 }
22409 glyph->charpos = CHARPOS (it->position);
22410 glyph->object = it->object;
22411 if (it->pixel_width > 0)
22412 {
22413 glyph->pixel_width = it->pixel_width;
22414 glyph->padding_p = 0;
22415 }
22416 else
22417 {
22418 /* Assure at least 1-pixel width. Otherwise, cursor can't
22419 be displayed correctly. */
22420 glyph->pixel_width = 1;
22421 glyph->padding_p = 1;
22422 }
22423 glyph->ascent = it->ascent;
22424 glyph->descent = it->descent;
22425 glyph->voffset = it->voffset;
22426 glyph->type = CHAR_GLYPH;
22427 glyph->avoid_cursor_p = it->avoid_cursor_p;
22428 glyph->multibyte_p = it->multibyte_p;
22429 glyph->left_box_line_p = it->start_of_box_run_p;
22430 glyph->right_box_line_p = it->end_of_box_run_p;
22431 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22432 || it->phys_descent > it->descent);
22433 glyph->glyph_not_available_p = it->glyph_not_available_p;
22434 glyph->face_id = it->face_id;
22435 glyph->u.ch = it->char_to_display;
22436 glyph->slice.img = null_glyph_slice;
22437 glyph->font_type = FONT_TYPE_UNKNOWN;
22438 if (it->bidi_p)
22439 {
22440 glyph->resolved_level = it->bidi_it.resolved_level;
22441 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22442 abort ();
22443 glyph->bidi_type = it->bidi_it.type;
22444 }
22445 else
22446 {
22447 glyph->resolved_level = 0;
22448 glyph->bidi_type = UNKNOWN_BT;
22449 }
22450 ++it->glyph_row->used[area];
22451 }
22452 else
22453 IT_EXPAND_MATRIX_WIDTH (it, area);
22454 }
22455
22456 /* Store one glyph for the composition IT->cmp_it.id in
22457 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
22458 non-null. */
22459
22460 static INLINE void
22461 append_composite_glyph (struct it *it)
22462 {
22463 struct glyph *glyph;
22464 enum glyph_row_area area = it->area;
22465
22466 xassert (it->glyph_row);
22467
22468 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22469 if (glyph < it->glyph_row->glyphs[area + 1])
22470 {
22471 /* If the glyph row is reversed, we need to prepend the glyph
22472 rather than append it. */
22473 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
22474 {
22475 struct glyph *g;
22476
22477 /* Make room for the new glyph. */
22478 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
22479 g[1] = *g;
22480 glyph = it->glyph_row->glyphs[it->area];
22481 }
22482 glyph->charpos = it->cmp_it.charpos;
22483 glyph->object = it->object;
22484 glyph->pixel_width = it->pixel_width;
22485 glyph->ascent = it->ascent;
22486 glyph->descent = it->descent;
22487 glyph->voffset = it->voffset;
22488 glyph->type = COMPOSITE_GLYPH;
22489 if (it->cmp_it.ch < 0)
22490 {
22491 glyph->u.cmp.automatic = 0;
22492 glyph->u.cmp.id = it->cmp_it.id;
22493 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
22494 }
22495 else
22496 {
22497 glyph->u.cmp.automatic = 1;
22498 glyph->u.cmp.id = it->cmp_it.id;
22499 glyph->slice.cmp.from = it->cmp_it.from;
22500 glyph->slice.cmp.to = it->cmp_it.to - 1;
22501 }
22502 glyph->avoid_cursor_p = it->avoid_cursor_p;
22503 glyph->multibyte_p = it->multibyte_p;
22504 glyph->left_box_line_p = it->start_of_box_run_p;
22505 glyph->right_box_line_p = it->end_of_box_run_p;
22506 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
22507 || it->phys_descent > it->descent);
22508 glyph->padding_p = 0;
22509 glyph->glyph_not_available_p = 0;
22510 glyph->face_id = it->face_id;
22511 glyph->font_type = FONT_TYPE_UNKNOWN;
22512 if (it->bidi_p)
22513 {
22514 glyph->resolved_level = it->bidi_it.resolved_level;
22515 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22516 abort ();
22517 glyph->bidi_type = it->bidi_it.type;
22518 }
22519 ++it->glyph_row->used[area];
22520 }
22521 else
22522 IT_EXPAND_MATRIX_WIDTH (it, area);
22523 }
22524
22525
22526 /* Change IT->ascent and IT->height according to the setting of
22527 IT->voffset. */
22528
22529 static INLINE void
22530 take_vertical_position_into_account (struct it *it)
22531 {
22532 if (it->voffset)
22533 {
22534 if (it->voffset < 0)
22535 /* Increase the ascent so that we can display the text higher
22536 in the line. */
22537 it->ascent -= it->voffset;
22538 else
22539 /* Increase the descent so that we can display the text lower
22540 in the line. */
22541 it->descent += it->voffset;
22542 }
22543 }
22544
22545
22546 /* Produce glyphs/get display metrics for the image IT is loaded with.
22547 See the description of struct display_iterator in dispextern.h for
22548 an overview of struct display_iterator. */
22549
22550 static void
22551 produce_image_glyph (struct it *it)
22552 {
22553 struct image *img;
22554 struct face *face;
22555 int glyph_ascent, crop;
22556 struct glyph_slice slice;
22557
22558 xassert (it->what == IT_IMAGE);
22559
22560 face = FACE_FROM_ID (it->f, it->face_id);
22561 xassert (face);
22562 /* Make sure X resources of the face is loaded. */
22563 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22564
22565 if (it->image_id < 0)
22566 {
22567 /* Fringe bitmap. */
22568 it->ascent = it->phys_ascent = 0;
22569 it->descent = it->phys_descent = 0;
22570 it->pixel_width = 0;
22571 it->nglyphs = 0;
22572 return;
22573 }
22574
22575 img = IMAGE_FROM_ID (it->f, it->image_id);
22576 xassert (img);
22577 /* Make sure X resources of the image is loaded. */
22578 prepare_image_for_display (it->f, img);
22579
22580 slice.x = slice.y = 0;
22581 slice.width = img->width;
22582 slice.height = img->height;
22583
22584 if (INTEGERP (it->slice.x))
22585 slice.x = XINT (it->slice.x);
22586 else if (FLOATP (it->slice.x))
22587 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
22588
22589 if (INTEGERP (it->slice.y))
22590 slice.y = XINT (it->slice.y);
22591 else if (FLOATP (it->slice.y))
22592 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
22593
22594 if (INTEGERP (it->slice.width))
22595 slice.width = XINT (it->slice.width);
22596 else if (FLOATP (it->slice.width))
22597 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
22598
22599 if (INTEGERP (it->slice.height))
22600 slice.height = XINT (it->slice.height);
22601 else if (FLOATP (it->slice.height))
22602 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
22603
22604 if (slice.x >= img->width)
22605 slice.x = img->width;
22606 if (slice.y >= img->height)
22607 slice.y = img->height;
22608 if (slice.x + slice.width >= img->width)
22609 slice.width = img->width - slice.x;
22610 if (slice.y + slice.height > img->height)
22611 slice.height = img->height - slice.y;
22612
22613 if (slice.width == 0 || slice.height == 0)
22614 return;
22615
22616 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
22617
22618 it->descent = slice.height - glyph_ascent;
22619 if (slice.y == 0)
22620 it->descent += img->vmargin;
22621 if (slice.y + slice.height == img->height)
22622 it->descent += img->vmargin;
22623 it->phys_descent = it->descent;
22624
22625 it->pixel_width = slice.width;
22626 if (slice.x == 0)
22627 it->pixel_width += img->hmargin;
22628 if (slice.x + slice.width == img->width)
22629 it->pixel_width += img->hmargin;
22630
22631 /* It's quite possible for images to have an ascent greater than
22632 their height, so don't get confused in that case. */
22633 if (it->descent < 0)
22634 it->descent = 0;
22635
22636 it->nglyphs = 1;
22637
22638 if (face->box != FACE_NO_BOX)
22639 {
22640 if (face->box_line_width > 0)
22641 {
22642 if (slice.y == 0)
22643 it->ascent += face->box_line_width;
22644 if (slice.y + slice.height == img->height)
22645 it->descent += face->box_line_width;
22646 }
22647
22648 if (it->start_of_box_run_p && slice.x == 0)
22649 it->pixel_width += eabs (face->box_line_width);
22650 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
22651 it->pixel_width += eabs (face->box_line_width);
22652 }
22653
22654 take_vertical_position_into_account (it);
22655
22656 /* Automatically crop wide image glyphs at right edge so we can
22657 draw the cursor on same display row. */
22658 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
22659 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
22660 {
22661 it->pixel_width -= crop;
22662 slice.width -= crop;
22663 }
22664
22665 if (it->glyph_row)
22666 {
22667 struct glyph *glyph;
22668 enum glyph_row_area area = it->area;
22669
22670 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22671 if (glyph < it->glyph_row->glyphs[area + 1])
22672 {
22673 glyph->charpos = CHARPOS (it->position);
22674 glyph->object = it->object;
22675 glyph->pixel_width = it->pixel_width;
22676 glyph->ascent = glyph_ascent;
22677 glyph->descent = it->descent;
22678 glyph->voffset = it->voffset;
22679 glyph->type = IMAGE_GLYPH;
22680 glyph->avoid_cursor_p = it->avoid_cursor_p;
22681 glyph->multibyte_p = it->multibyte_p;
22682 glyph->left_box_line_p = it->start_of_box_run_p;
22683 glyph->right_box_line_p = it->end_of_box_run_p;
22684 glyph->overlaps_vertically_p = 0;
22685 glyph->padding_p = 0;
22686 glyph->glyph_not_available_p = 0;
22687 glyph->face_id = it->face_id;
22688 glyph->u.img_id = img->id;
22689 glyph->slice.img = slice;
22690 glyph->font_type = FONT_TYPE_UNKNOWN;
22691 if (it->bidi_p)
22692 {
22693 glyph->resolved_level = it->bidi_it.resolved_level;
22694 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22695 abort ();
22696 glyph->bidi_type = it->bidi_it.type;
22697 }
22698 ++it->glyph_row->used[area];
22699 }
22700 else
22701 IT_EXPAND_MATRIX_WIDTH (it, area);
22702 }
22703 }
22704
22705
22706 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
22707 of the glyph, WIDTH and HEIGHT are the width and height of the
22708 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
22709
22710 static void
22711 append_stretch_glyph (struct it *it, Lisp_Object object,
22712 int width, int height, int ascent)
22713 {
22714 struct glyph *glyph;
22715 enum glyph_row_area area = it->area;
22716
22717 xassert (ascent >= 0 && ascent <= height);
22718
22719 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
22720 if (glyph < it->glyph_row->glyphs[area + 1])
22721 {
22722 /* If the glyph row is reversed, we need to prepend the glyph
22723 rather than append it. */
22724 if (it->glyph_row->reversed_p && area == TEXT_AREA)
22725 {
22726 struct glyph *g;
22727
22728 /* Make room for the additional glyph. */
22729 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
22730 g[1] = *g;
22731 glyph = it->glyph_row->glyphs[area];
22732 }
22733 glyph->charpos = CHARPOS (it->position);
22734 glyph->object = object;
22735 glyph->pixel_width = width;
22736 glyph->ascent = ascent;
22737 glyph->descent = height - ascent;
22738 glyph->voffset = it->voffset;
22739 glyph->type = STRETCH_GLYPH;
22740 glyph->avoid_cursor_p = it->avoid_cursor_p;
22741 glyph->multibyte_p = it->multibyte_p;
22742 glyph->left_box_line_p = it->start_of_box_run_p;
22743 glyph->right_box_line_p = it->end_of_box_run_p;
22744 glyph->overlaps_vertically_p = 0;
22745 glyph->padding_p = 0;
22746 glyph->glyph_not_available_p = 0;
22747 glyph->face_id = it->face_id;
22748 glyph->u.stretch.ascent = ascent;
22749 glyph->u.stretch.height = height;
22750 glyph->slice.img = null_glyph_slice;
22751 glyph->font_type = FONT_TYPE_UNKNOWN;
22752 if (it->bidi_p)
22753 {
22754 glyph->resolved_level = it->bidi_it.resolved_level;
22755 if ((it->bidi_it.type & 7) != it->bidi_it.type)
22756 abort ();
22757 glyph->bidi_type = it->bidi_it.type;
22758 }
22759 else
22760 {
22761 glyph->resolved_level = 0;
22762 glyph->bidi_type = UNKNOWN_BT;
22763 }
22764 ++it->glyph_row->used[area];
22765 }
22766 else
22767 IT_EXPAND_MATRIX_WIDTH (it, area);
22768 }
22769
22770
22771 /* Produce a stretch glyph for iterator IT. IT->object is the value
22772 of the glyph property displayed. The value must be a list
22773 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
22774 being recognized:
22775
22776 1. `:width WIDTH' specifies that the space should be WIDTH *
22777 canonical char width wide. WIDTH may be an integer or floating
22778 point number.
22779
22780 2. `:relative-width FACTOR' specifies that the width of the stretch
22781 should be computed from the width of the first character having the
22782 `glyph' property, and should be FACTOR times that width.
22783
22784 3. `:align-to HPOS' specifies that the space should be wide enough
22785 to reach HPOS, a value in canonical character units.
22786
22787 Exactly one of the above pairs must be present.
22788
22789 4. `:height HEIGHT' specifies that the height of the stretch produced
22790 should be HEIGHT, measured in canonical character units.
22791
22792 5. `:relative-height FACTOR' specifies that the height of the
22793 stretch should be FACTOR times the height of the characters having
22794 the glyph property.
22795
22796 Either none or exactly one of 4 or 5 must be present.
22797
22798 6. `:ascent ASCENT' specifies that ASCENT percent of the height
22799 of the stretch should be used for the ascent of the stretch.
22800 ASCENT must be in the range 0 <= ASCENT <= 100. */
22801
22802 static void
22803 produce_stretch_glyph (struct it *it)
22804 {
22805 /* (space :width WIDTH :height HEIGHT ...) */
22806 Lisp_Object prop, plist;
22807 int width = 0, height = 0, align_to = -1;
22808 int zero_width_ok_p = 0, zero_height_ok_p = 0;
22809 int ascent = 0;
22810 double tem;
22811 struct face *face = FACE_FROM_ID (it->f, it->face_id);
22812 struct font *font = face->font ? face->font : FRAME_FONT (it->f);
22813
22814 PREPARE_FACE_FOR_DISPLAY (it->f, face);
22815
22816 /* List should start with `space'. */
22817 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
22818 plist = XCDR (it->object);
22819
22820 /* Compute the width of the stretch. */
22821 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
22822 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
22823 {
22824 /* Absolute width `:width WIDTH' specified and valid. */
22825 zero_width_ok_p = 1;
22826 width = (int)tem;
22827 }
22828 else if (prop = Fplist_get (plist, QCrelative_width),
22829 NUMVAL (prop) > 0)
22830 {
22831 /* Relative width `:relative-width FACTOR' specified and valid.
22832 Compute the width of the characters having the `glyph'
22833 property. */
22834 struct it it2;
22835 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
22836
22837 it2 = *it;
22838 if (it->multibyte_p)
22839 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
22840 else
22841 {
22842 it2.c = it2.char_to_display = *p, it2.len = 1;
22843 if (! ASCII_CHAR_P (it2.c))
22844 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
22845 }
22846
22847 it2.glyph_row = NULL;
22848 it2.what = IT_CHARACTER;
22849 x_produce_glyphs (&it2);
22850 width = NUMVAL (prop) * it2.pixel_width;
22851 }
22852 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
22853 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
22854 {
22855 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
22856 align_to = (align_to < 0
22857 ? 0
22858 : align_to - window_box_left_offset (it->w, TEXT_AREA));
22859 else if (align_to < 0)
22860 align_to = window_box_left_offset (it->w, TEXT_AREA);
22861 width = max (0, (int)tem + align_to - it->current_x);
22862 zero_width_ok_p = 1;
22863 }
22864 else
22865 /* Nothing specified -> width defaults to canonical char width. */
22866 width = FRAME_COLUMN_WIDTH (it->f);
22867
22868 if (width <= 0 && (width < 0 || !zero_width_ok_p))
22869 width = 1;
22870
22871 /* Compute height. */
22872 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
22873 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22874 {
22875 height = (int)tem;
22876 zero_height_ok_p = 1;
22877 }
22878 else if (prop = Fplist_get (plist, QCrelative_height),
22879 NUMVAL (prop) > 0)
22880 height = FONT_HEIGHT (font) * NUMVAL (prop);
22881 else
22882 height = FONT_HEIGHT (font);
22883
22884 if (height <= 0 && (height < 0 || !zero_height_ok_p))
22885 height = 1;
22886
22887 /* Compute percentage of height used for ascent. If
22888 `:ascent ASCENT' is present and valid, use that. Otherwise,
22889 derive the ascent from the font in use. */
22890 if (prop = Fplist_get (plist, QCascent),
22891 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
22892 ascent = height * NUMVAL (prop) / 100.0;
22893 else if (!NILP (prop)
22894 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
22895 ascent = min (max (0, (int)tem), height);
22896 else
22897 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
22898
22899 if (width > 0 && it->line_wrap != TRUNCATE
22900 && it->current_x + width > it->last_visible_x)
22901 width = it->last_visible_x - it->current_x - 1;
22902
22903 if (width > 0 && height > 0 && it->glyph_row)
22904 {
22905 Lisp_Object object = it->stack[it->sp - 1].string;
22906 if (!STRINGP (object))
22907 object = it->w->buffer;
22908 append_stretch_glyph (it, object, width, height, ascent);
22909 }
22910
22911 it->pixel_width = width;
22912 it->ascent = it->phys_ascent = ascent;
22913 it->descent = it->phys_descent = height - it->ascent;
22914 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
22915
22916 take_vertical_position_into_account (it);
22917 }
22918
22919 /* Calculate line-height and line-spacing properties.
22920 An integer value specifies explicit pixel value.
22921 A float value specifies relative value to current face height.
22922 A cons (float . face-name) specifies relative value to
22923 height of specified face font.
22924
22925 Returns height in pixels, or nil. */
22926
22927
22928 static Lisp_Object
22929 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
22930 int boff, int override)
22931 {
22932 Lisp_Object face_name = Qnil;
22933 int ascent, descent, height;
22934
22935 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
22936 return val;
22937
22938 if (CONSP (val))
22939 {
22940 face_name = XCAR (val);
22941 val = XCDR (val);
22942 if (!NUMBERP (val))
22943 val = make_number (1);
22944 if (NILP (face_name))
22945 {
22946 height = it->ascent + it->descent;
22947 goto scale;
22948 }
22949 }
22950
22951 if (NILP (face_name))
22952 {
22953 font = FRAME_FONT (it->f);
22954 boff = FRAME_BASELINE_OFFSET (it->f);
22955 }
22956 else if (EQ (face_name, Qt))
22957 {
22958 override = 0;
22959 }
22960 else
22961 {
22962 int face_id;
22963 struct face *face;
22964
22965 face_id = lookup_named_face (it->f, face_name, 0);
22966 if (face_id < 0)
22967 return make_number (-1);
22968
22969 face = FACE_FROM_ID (it->f, face_id);
22970 font = face->font;
22971 if (font == NULL)
22972 return make_number (-1);
22973 boff = font->baseline_offset;
22974 if (font->vertical_centering)
22975 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
22976 }
22977
22978 ascent = FONT_BASE (font) + boff;
22979 descent = FONT_DESCENT (font) - boff;
22980
22981 if (override)
22982 {
22983 it->override_ascent = ascent;
22984 it->override_descent = descent;
22985 it->override_boff = boff;
22986 }
22987
22988 height = ascent + descent;
22989
22990 scale:
22991 if (FLOATP (val))
22992 height = (int)(XFLOAT_DATA (val) * height);
22993 else if (INTEGERP (val))
22994 height *= XINT (val);
22995
22996 return make_number (height);
22997 }
22998
22999
23000 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
23001 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
23002 and only if this is for a character for which no font was found.
23003
23004 If the display method (it->glyphless_method) is
23005 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
23006 length of the acronym or the hexadecimal string, UPPER_XOFF and
23007 UPPER_YOFF are pixel offsets for the upper part of the string,
23008 LOWER_XOFF and LOWER_YOFF are for the lower part.
23009
23010 For the other display methods, LEN through LOWER_YOFF are zero. */
23011
23012 static void
23013 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
23014 short upper_xoff, short upper_yoff,
23015 short lower_xoff, short lower_yoff)
23016 {
23017 struct glyph *glyph;
23018 enum glyph_row_area area = it->area;
23019
23020 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23021 if (glyph < it->glyph_row->glyphs[area + 1])
23022 {
23023 /* If the glyph row is reversed, we need to prepend the glyph
23024 rather than append it. */
23025 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23026 {
23027 struct glyph *g;
23028
23029 /* Make room for the additional glyph. */
23030 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23031 g[1] = *g;
23032 glyph = it->glyph_row->glyphs[area];
23033 }
23034 glyph->charpos = CHARPOS (it->position);
23035 glyph->object = it->object;
23036 glyph->pixel_width = it->pixel_width;
23037 glyph->ascent = it->ascent;
23038 glyph->descent = it->descent;
23039 glyph->voffset = it->voffset;
23040 glyph->type = GLYPHLESS_GLYPH;
23041 glyph->u.glyphless.method = it->glyphless_method;
23042 glyph->u.glyphless.for_no_font = for_no_font;
23043 glyph->u.glyphless.len = len;
23044 glyph->u.glyphless.ch = it->c;
23045 glyph->slice.glyphless.upper_xoff = upper_xoff;
23046 glyph->slice.glyphless.upper_yoff = upper_yoff;
23047 glyph->slice.glyphless.lower_xoff = lower_xoff;
23048 glyph->slice.glyphless.lower_yoff = lower_yoff;
23049 glyph->avoid_cursor_p = it->avoid_cursor_p;
23050 glyph->multibyte_p = it->multibyte_p;
23051 glyph->left_box_line_p = it->start_of_box_run_p;
23052 glyph->right_box_line_p = it->end_of_box_run_p;
23053 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23054 || it->phys_descent > it->descent);
23055 glyph->padding_p = 0;
23056 glyph->glyph_not_available_p = 0;
23057 glyph->face_id = face_id;
23058 glyph->font_type = FONT_TYPE_UNKNOWN;
23059 if (it->bidi_p)
23060 {
23061 glyph->resolved_level = it->bidi_it.resolved_level;
23062 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23063 abort ();
23064 glyph->bidi_type = it->bidi_it.type;
23065 }
23066 ++it->glyph_row->used[area];
23067 }
23068 else
23069 IT_EXPAND_MATRIX_WIDTH (it, area);
23070 }
23071
23072
23073 /* Produce a glyph for a glyphless character for iterator IT.
23074 IT->glyphless_method specifies which method to use for displaying
23075 the character. See the description of enum
23076 glyphless_display_method in dispextern.h for the detail.
23077
23078 FOR_NO_FONT is nonzero if and only if this is for a character for
23079 which no font was found. ACRONYM, if non-nil, is an acronym string
23080 for the character. */
23081
23082 static void
23083 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
23084 {
23085 int face_id;
23086 struct face *face;
23087 struct font *font;
23088 int base_width, base_height, width, height;
23089 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
23090 int len;
23091
23092 /* Get the metrics of the base font. We always refer to the current
23093 ASCII face. */
23094 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
23095 font = face->font ? face->font : FRAME_FONT (it->f);
23096 it->ascent = FONT_BASE (font) + font->baseline_offset;
23097 it->descent = FONT_DESCENT (font) - font->baseline_offset;
23098 base_height = it->ascent + it->descent;
23099 base_width = font->average_width;
23100
23101 /* Get a face ID for the glyph by utilizing a cache (the same way as
23102 doen for `escape-glyph' in get_next_display_element). */
23103 if (it->f == last_glyphless_glyph_frame
23104 && it->face_id == last_glyphless_glyph_face_id)
23105 {
23106 face_id = last_glyphless_glyph_merged_face_id;
23107 }
23108 else
23109 {
23110 /* Merge the `glyphless-char' face into the current face. */
23111 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
23112 last_glyphless_glyph_frame = it->f;
23113 last_glyphless_glyph_face_id = it->face_id;
23114 last_glyphless_glyph_merged_face_id = face_id;
23115 }
23116
23117 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
23118 {
23119 it->pixel_width = THIN_SPACE_WIDTH;
23120 len = 0;
23121 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23122 }
23123 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
23124 {
23125 width = CHAR_WIDTH (it->c);
23126 if (width == 0)
23127 width = 1;
23128 else if (width > 4)
23129 width = 4;
23130 it->pixel_width = base_width * width;
23131 len = 0;
23132 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23133 }
23134 else
23135 {
23136 char buf[7];
23137 const char *str;
23138 unsigned int code[6];
23139 int upper_len;
23140 int ascent, descent;
23141 struct font_metrics metrics_upper, metrics_lower;
23142
23143 face = FACE_FROM_ID (it->f, face_id);
23144 font = face->font ? face->font : FRAME_FONT (it->f);
23145 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23146
23147 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
23148 {
23149 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
23150 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
23151 if (CONSP (acronym))
23152 acronym = XCAR (acronym);
23153 str = STRINGP (acronym) ? SSDATA (acronym) : "";
23154 }
23155 else
23156 {
23157 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
23158 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
23159 str = buf;
23160 }
23161 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
23162 code[len] = font->driver->encode_char (font, str[len]);
23163 upper_len = (len + 1) / 2;
23164 font->driver->text_extents (font, code, upper_len,
23165 &metrics_upper);
23166 font->driver->text_extents (font, code + upper_len, len - upper_len,
23167 &metrics_lower);
23168
23169
23170
23171 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
23172 width = max (metrics_upper.width, metrics_lower.width) + 4;
23173 upper_xoff = upper_yoff = 2; /* the typical case */
23174 if (base_width >= width)
23175 {
23176 /* Align the upper to the left, the lower to the right. */
23177 it->pixel_width = base_width;
23178 lower_xoff = base_width - 2 - metrics_lower.width;
23179 }
23180 else
23181 {
23182 /* Center the shorter one. */
23183 it->pixel_width = width;
23184 if (metrics_upper.width >= metrics_lower.width)
23185 lower_xoff = (width - metrics_lower.width) / 2;
23186 else
23187 {
23188 /* FIXME: This code doesn't look right. It formerly was
23189 missing the "lower_xoff = 0;", which couldn't have
23190 been right since it left lower_xoff uninitialized. */
23191 lower_xoff = 0;
23192 upper_xoff = (width - metrics_upper.width) / 2;
23193 }
23194 }
23195
23196 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
23197 top, bottom, and between upper and lower strings. */
23198 height = (metrics_upper.ascent + metrics_upper.descent
23199 + metrics_lower.ascent + metrics_lower.descent) + 5;
23200 /* Center vertically.
23201 H:base_height, D:base_descent
23202 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
23203
23204 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
23205 descent = D - H/2 + h/2;
23206 lower_yoff = descent - 2 - ld;
23207 upper_yoff = lower_yoff - la - 1 - ud; */
23208 ascent = - (it->descent - (base_height + height + 1) / 2);
23209 descent = it->descent - (base_height - height) / 2;
23210 lower_yoff = descent - 2 - metrics_lower.descent;
23211 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
23212 - metrics_upper.descent);
23213 /* Don't make the height shorter than the base height. */
23214 if (height > base_height)
23215 {
23216 it->ascent = ascent;
23217 it->descent = descent;
23218 }
23219 }
23220
23221 it->phys_ascent = it->ascent;
23222 it->phys_descent = it->descent;
23223 if (it->glyph_row)
23224 append_glyphless_glyph (it, face_id, for_no_font, len,
23225 upper_xoff, upper_yoff,
23226 lower_xoff, lower_yoff);
23227 it->nglyphs = 1;
23228 take_vertical_position_into_account (it);
23229 }
23230
23231
23232 /* RIF:
23233 Produce glyphs/get display metrics for the display element IT is
23234 loaded with. See the description of struct it in dispextern.h
23235 for an overview of struct it. */
23236
23237 void
23238 x_produce_glyphs (struct it *it)
23239 {
23240 int extra_line_spacing = it->extra_line_spacing;
23241
23242 it->glyph_not_available_p = 0;
23243
23244 if (it->what == IT_CHARACTER)
23245 {
23246 XChar2b char2b;
23247 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23248 struct font *font = face->font;
23249 struct font_metrics *pcm = NULL;
23250 int boff; /* baseline offset */
23251
23252 if (font == NULL)
23253 {
23254 /* When no suitable font is found, display this character by
23255 the method specified in the first extra slot of
23256 Vglyphless_char_display. */
23257 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
23258
23259 xassert (it->what == IT_GLYPHLESS);
23260 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
23261 goto done;
23262 }
23263
23264 boff = font->baseline_offset;
23265 if (font->vertical_centering)
23266 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23267
23268 if (it->char_to_display != '\n' && it->char_to_display != '\t')
23269 {
23270 int stretched_p;
23271
23272 it->nglyphs = 1;
23273
23274 if (it->override_ascent >= 0)
23275 {
23276 it->ascent = it->override_ascent;
23277 it->descent = it->override_descent;
23278 boff = it->override_boff;
23279 }
23280 else
23281 {
23282 it->ascent = FONT_BASE (font) + boff;
23283 it->descent = FONT_DESCENT (font) - boff;
23284 }
23285
23286 if (get_char_glyph_code (it->char_to_display, font, &char2b))
23287 {
23288 pcm = get_per_char_metric (font, &char2b);
23289 if (pcm->width == 0
23290 && pcm->rbearing == 0 && pcm->lbearing == 0)
23291 pcm = NULL;
23292 }
23293
23294 if (pcm)
23295 {
23296 it->phys_ascent = pcm->ascent + boff;
23297 it->phys_descent = pcm->descent - boff;
23298 it->pixel_width = pcm->width;
23299 }
23300 else
23301 {
23302 it->glyph_not_available_p = 1;
23303 it->phys_ascent = it->ascent;
23304 it->phys_descent = it->descent;
23305 it->pixel_width = font->space_width;
23306 }
23307
23308 if (it->constrain_row_ascent_descent_p)
23309 {
23310 if (it->descent > it->max_descent)
23311 {
23312 it->ascent += it->descent - it->max_descent;
23313 it->descent = it->max_descent;
23314 }
23315 if (it->ascent > it->max_ascent)
23316 {
23317 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23318 it->ascent = it->max_ascent;
23319 }
23320 it->phys_ascent = min (it->phys_ascent, it->ascent);
23321 it->phys_descent = min (it->phys_descent, it->descent);
23322 extra_line_spacing = 0;
23323 }
23324
23325 /* If this is a space inside a region of text with
23326 `space-width' property, change its width. */
23327 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
23328 if (stretched_p)
23329 it->pixel_width *= XFLOATINT (it->space_width);
23330
23331 /* If face has a box, add the box thickness to the character
23332 height. If character has a box line to the left and/or
23333 right, add the box line width to the character's width. */
23334 if (face->box != FACE_NO_BOX)
23335 {
23336 int thick = face->box_line_width;
23337
23338 if (thick > 0)
23339 {
23340 it->ascent += thick;
23341 it->descent += thick;
23342 }
23343 else
23344 thick = -thick;
23345
23346 if (it->start_of_box_run_p)
23347 it->pixel_width += thick;
23348 if (it->end_of_box_run_p)
23349 it->pixel_width += thick;
23350 }
23351
23352 /* If face has an overline, add the height of the overline
23353 (1 pixel) and a 1 pixel margin to the character height. */
23354 if (face->overline_p)
23355 it->ascent += overline_margin;
23356
23357 if (it->constrain_row_ascent_descent_p)
23358 {
23359 if (it->ascent > it->max_ascent)
23360 it->ascent = it->max_ascent;
23361 if (it->descent > it->max_descent)
23362 it->descent = it->max_descent;
23363 }
23364
23365 take_vertical_position_into_account (it);
23366
23367 /* If we have to actually produce glyphs, do it. */
23368 if (it->glyph_row)
23369 {
23370 if (stretched_p)
23371 {
23372 /* Translate a space with a `space-width' property
23373 into a stretch glyph. */
23374 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
23375 / FONT_HEIGHT (font));
23376 append_stretch_glyph (it, it->object, it->pixel_width,
23377 it->ascent + it->descent, ascent);
23378 }
23379 else
23380 append_glyph (it);
23381
23382 /* If characters with lbearing or rbearing are displayed
23383 in this line, record that fact in a flag of the
23384 glyph row. This is used to optimize X output code. */
23385 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
23386 it->glyph_row->contains_overlapping_glyphs_p = 1;
23387 }
23388 if (! stretched_p && it->pixel_width == 0)
23389 /* We assure that all visible glyphs have at least 1-pixel
23390 width. */
23391 it->pixel_width = 1;
23392 }
23393 else if (it->char_to_display == '\n')
23394 {
23395 /* A newline has no width, but we need the height of the
23396 line. But if previous part of the line sets a height,
23397 don't increase that height */
23398
23399 Lisp_Object height;
23400 Lisp_Object total_height = Qnil;
23401
23402 it->override_ascent = -1;
23403 it->pixel_width = 0;
23404 it->nglyphs = 0;
23405
23406 height = get_it_property (it, Qline_height);
23407 /* Split (line-height total-height) list */
23408 if (CONSP (height)
23409 && CONSP (XCDR (height))
23410 && NILP (XCDR (XCDR (height))))
23411 {
23412 total_height = XCAR (XCDR (height));
23413 height = XCAR (height);
23414 }
23415 height = calc_line_height_property (it, height, font, boff, 1);
23416
23417 if (it->override_ascent >= 0)
23418 {
23419 it->ascent = it->override_ascent;
23420 it->descent = it->override_descent;
23421 boff = it->override_boff;
23422 }
23423 else
23424 {
23425 it->ascent = FONT_BASE (font) + boff;
23426 it->descent = FONT_DESCENT (font) - boff;
23427 }
23428
23429 if (EQ (height, Qt))
23430 {
23431 if (it->descent > it->max_descent)
23432 {
23433 it->ascent += it->descent - it->max_descent;
23434 it->descent = it->max_descent;
23435 }
23436 if (it->ascent > it->max_ascent)
23437 {
23438 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
23439 it->ascent = it->max_ascent;
23440 }
23441 it->phys_ascent = min (it->phys_ascent, it->ascent);
23442 it->phys_descent = min (it->phys_descent, it->descent);
23443 it->constrain_row_ascent_descent_p = 1;
23444 extra_line_spacing = 0;
23445 }
23446 else
23447 {
23448 Lisp_Object spacing;
23449
23450 it->phys_ascent = it->ascent;
23451 it->phys_descent = it->descent;
23452
23453 if ((it->max_ascent > 0 || it->max_descent > 0)
23454 && face->box != FACE_NO_BOX
23455 && face->box_line_width > 0)
23456 {
23457 it->ascent += face->box_line_width;
23458 it->descent += face->box_line_width;
23459 }
23460 if (!NILP (height)
23461 && XINT (height) > it->ascent + it->descent)
23462 it->ascent = XINT (height) - it->descent;
23463
23464 if (!NILP (total_height))
23465 spacing = calc_line_height_property (it, total_height, font, boff, 0);
23466 else
23467 {
23468 spacing = get_it_property (it, Qline_spacing);
23469 spacing = calc_line_height_property (it, spacing, font, boff, 0);
23470 }
23471 if (INTEGERP (spacing))
23472 {
23473 extra_line_spacing = XINT (spacing);
23474 if (!NILP (total_height))
23475 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
23476 }
23477 }
23478 }
23479 else /* i.e. (it->char_to_display == '\t') */
23480 {
23481 if (font->space_width > 0)
23482 {
23483 int tab_width = it->tab_width * font->space_width;
23484 int x = it->current_x + it->continuation_lines_width;
23485 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
23486
23487 /* If the distance from the current position to the next tab
23488 stop is less than a space character width, use the
23489 tab stop after that. */
23490 if (next_tab_x - x < font->space_width)
23491 next_tab_x += tab_width;
23492
23493 it->pixel_width = next_tab_x - x;
23494 it->nglyphs = 1;
23495 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
23496 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
23497
23498 if (it->glyph_row)
23499 {
23500 append_stretch_glyph (it, it->object, it->pixel_width,
23501 it->ascent + it->descent, it->ascent);
23502 }
23503 }
23504 else
23505 {
23506 it->pixel_width = 0;
23507 it->nglyphs = 1;
23508 }
23509 }
23510 }
23511 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
23512 {
23513 /* A static composition.
23514
23515 Note: A composition is represented as one glyph in the
23516 glyph matrix. There are no padding glyphs.
23517
23518 Important note: pixel_width, ascent, and descent are the
23519 values of what is drawn by draw_glyphs (i.e. the values of
23520 the overall glyphs composed). */
23521 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23522 int boff; /* baseline offset */
23523 struct composition *cmp = composition_table[it->cmp_it.id];
23524 int glyph_len = cmp->glyph_len;
23525 struct font *font = face->font;
23526
23527 it->nglyphs = 1;
23528
23529 /* If we have not yet calculated pixel size data of glyphs of
23530 the composition for the current face font, calculate them
23531 now. Theoretically, we have to check all fonts for the
23532 glyphs, but that requires much time and memory space. So,
23533 here we check only the font of the first glyph. This may
23534 lead to incorrect display, but it's very rare, and C-l
23535 (recenter-top-bottom) can correct the display anyway. */
23536 if (! cmp->font || cmp->font != font)
23537 {
23538 /* Ascent and descent of the font of the first character
23539 of this composition (adjusted by baseline offset).
23540 Ascent and descent of overall glyphs should not be less
23541 than these, respectively. */
23542 int font_ascent, font_descent, font_height;
23543 /* Bounding box of the overall glyphs. */
23544 int leftmost, rightmost, lowest, highest;
23545 int lbearing, rbearing;
23546 int i, width, ascent, descent;
23547 int left_padded = 0, right_padded = 0;
23548 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
23549 XChar2b char2b;
23550 struct font_metrics *pcm;
23551 int font_not_found_p;
23552 EMACS_INT pos;
23553
23554 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
23555 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
23556 break;
23557 if (glyph_len < cmp->glyph_len)
23558 right_padded = 1;
23559 for (i = 0; i < glyph_len; i++)
23560 {
23561 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
23562 break;
23563 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23564 }
23565 if (i > 0)
23566 left_padded = 1;
23567
23568 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
23569 : IT_CHARPOS (*it));
23570 /* If no suitable font is found, use the default font. */
23571 font_not_found_p = font == NULL;
23572 if (font_not_found_p)
23573 {
23574 face = face->ascii_face;
23575 font = face->font;
23576 }
23577 boff = font->baseline_offset;
23578 if (font->vertical_centering)
23579 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23580 font_ascent = FONT_BASE (font) + boff;
23581 font_descent = FONT_DESCENT (font) - boff;
23582 font_height = FONT_HEIGHT (font);
23583
23584 cmp->font = (void *) font;
23585
23586 pcm = NULL;
23587 if (! font_not_found_p)
23588 {
23589 get_char_face_and_encoding (it->f, c, it->face_id,
23590 &char2b, 0);
23591 pcm = get_per_char_metric (font, &char2b);
23592 }
23593
23594 /* Initialize the bounding box. */
23595 if (pcm)
23596 {
23597 width = pcm->width;
23598 ascent = pcm->ascent;
23599 descent = pcm->descent;
23600 lbearing = pcm->lbearing;
23601 rbearing = pcm->rbearing;
23602 }
23603 else
23604 {
23605 width = font->space_width;
23606 ascent = FONT_BASE (font);
23607 descent = FONT_DESCENT (font);
23608 lbearing = 0;
23609 rbearing = width;
23610 }
23611
23612 rightmost = width;
23613 leftmost = 0;
23614 lowest = - descent + boff;
23615 highest = ascent + boff;
23616
23617 if (! font_not_found_p
23618 && font->default_ascent
23619 && CHAR_TABLE_P (Vuse_default_ascent)
23620 && !NILP (Faref (Vuse_default_ascent,
23621 make_number (it->char_to_display))))
23622 highest = font->default_ascent + boff;
23623
23624 /* Draw the first glyph at the normal position. It may be
23625 shifted to right later if some other glyphs are drawn
23626 at the left. */
23627 cmp->offsets[i * 2] = 0;
23628 cmp->offsets[i * 2 + 1] = boff;
23629 cmp->lbearing = lbearing;
23630 cmp->rbearing = rbearing;
23631
23632 /* Set cmp->offsets for the remaining glyphs. */
23633 for (i++; i < glyph_len; i++)
23634 {
23635 int left, right, btm, top;
23636 int ch = COMPOSITION_GLYPH (cmp, i);
23637 int face_id;
23638 struct face *this_face;
23639
23640 if (ch == '\t')
23641 ch = ' ';
23642 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
23643 this_face = FACE_FROM_ID (it->f, face_id);
23644 font = this_face->font;
23645
23646 if (font == NULL)
23647 pcm = NULL;
23648 else
23649 {
23650 get_char_face_and_encoding (it->f, ch, face_id,
23651 &char2b, 0);
23652 pcm = get_per_char_metric (font, &char2b);
23653 }
23654 if (! pcm)
23655 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
23656 else
23657 {
23658 width = pcm->width;
23659 ascent = pcm->ascent;
23660 descent = pcm->descent;
23661 lbearing = pcm->lbearing;
23662 rbearing = pcm->rbearing;
23663 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
23664 {
23665 /* Relative composition with or without
23666 alternate chars. */
23667 left = (leftmost + rightmost - width) / 2;
23668 btm = - descent + boff;
23669 if (font->relative_compose
23670 && (! CHAR_TABLE_P (Vignore_relative_composition)
23671 || NILP (Faref (Vignore_relative_composition,
23672 make_number (ch)))))
23673 {
23674
23675 if (- descent >= font->relative_compose)
23676 /* One extra pixel between two glyphs. */
23677 btm = highest + 1;
23678 else if (ascent <= 0)
23679 /* One extra pixel between two glyphs. */
23680 btm = lowest - 1 - ascent - descent;
23681 }
23682 }
23683 else
23684 {
23685 /* A composition rule is specified by an integer
23686 value that encodes global and new reference
23687 points (GREF and NREF). GREF and NREF are
23688 specified by numbers as below:
23689
23690 0---1---2 -- ascent
23691 | |
23692 | |
23693 | |
23694 9--10--11 -- center
23695 | |
23696 ---3---4---5--- baseline
23697 | |
23698 6---7---8 -- descent
23699 */
23700 int rule = COMPOSITION_RULE (cmp, i);
23701 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
23702
23703 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
23704 grefx = gref % 3, nrefx = nref % 3;
23705 grefy = gref / 3, nrefy = nref / 3;
23706 if (xoff)
23707 xoff = font_height * (xoff - 128) / 256;
23708 if (yoff)
23709 yoff = font_height * (yoff - 128) / 256;
23710
23711 left = (leftmost
23712 + grefx * (rightmost - leftmost) / 2
23713 - nrefx * width / 2
23714 + xoff);
23715
23716 btm = ((grefy == 0 ? highest
23717 : grefy == 1 ? 0
23718 : grefy == 2 ? lowest
23719 : (highest + lowest) / 2)
23720 - (nrefy == 0 ? ascent + descent
23721 : nrefy == 1 ? descent - boff
23722 : nrefy == 2 ? 0
23723 : (ascent + descent) / 2)
23724 + yoff);
23725 }
23726
23727 cmp->offsets[i * 2] = left;
23728 cmp->offsets[i * 2 + 1] = btm + descent;
23729
23730 /* Update the bounding box of the overall glyphs. */
23731 if (width > 0)
23732 {
23733 right = left + width;
23734 if (left < leftmost)
23735 leftmost = left;
23736 if (right > rightmost)
23737 rightmost = right;
23738 }
23739 top = btm + descent + ascent;
23740 if (top > highest)
23741 highest = top;
23742 if (btm < lowest)
23743 lowest = btm;
23744
23745 if (cmp->lbearing > left + lbearing)
23746 cmp->lbearing = left + lbearing;
23747 if (cmp->rbearing < left + rbearing)
23748 cmp->rbearing = left + rbearing;
23749 }
23750 }
23751
23752 /* If there are glyphs whose x-offsets are negative,
23753 shift all glyphs to the right and make all x-offsets
23754 non-negative. */
23755 if (leftmost < 0)
23756 {
23757 for (i = 0; i < cmp->glyph_len; i++)
23758 cmp->offsets[i * 2] -= leftmost;
23759 rightmost -= leftmost;
23760 cmp->lbearing -= leftmost;
23761 cmp->rbearing -= leftmost;
23762 }
23763
23764 if (left_padded && cmp->lbearing < 0)
23765 {
23766 for (i = 0; i < cmp->glyph_len; i++)
23767 cmp->offsets[i * 2] -= cmp->lbearing;
23768 rightmost -= cmp->lbearing;
23769 cmp->rbearing -= cmp->lbearing;
23770 cmp->lbearing = 0;
23771 }
23772 if (right_padded && rightmost < cmp->rbearing)
23773 {
23774 rightmost = cmp->rbearing;
23775 }
23776
23777 cmp->pixel_width = rightmost;
23778 cmp->ascent = highest;
23779 cmp->descent = - lowest;
23780 if (cmp->ascent < font_ascent)
23781 cmp->ascent = font_ascent;
23782 if (cmp->descent < font_descent)
23783 cmp->descent = font_descent;
23784 }
23785
23786 if (it->glyph_row
23787 && (cmp->lbearing < 0
23788 || cmp->rbearing > cmp->pixel_width))
23789 it->glyph_row->contains_overlapping_glyphs_p = 1;
23790
23791 it->pixel_width = cmp->pixel_width;
23792 it->ascent = it->phys_ascent = cmp->ascent;
23793 it->descent = it->phys_descent = cmp->descent;
23794 if (face->box != FACE_NO_BOX)
23795 {
23796 int thick = face->box_line_width;
23797
23798 if (thick > 0)
23799 {
23800 it->ascent += thick;
23801 it->descent += thick;
23802 }
23803 else
23804 thick = - thick;
23805
23806 if (it->start_of_box_run_p)
23807 it->pixel_width += thick;
23808 if (it->end_of_box_run_p)
23809 it->pixel_width += thick;
23810 }
23811
23812 /* If face has an overline, add the height of the overline
23813 (1 pixel) and a 1 pixel margin to the character height. */
23814 if (face->overline_p)
23815 it->ascent += overline_margin;
23816
23817 take_vertical_position_into_account (it);
23818 if (it->ascent < 0)
23819 it->ascent = 0;
23820 if (it->descent < 0)
23821 it->descent = 0;
23822
23823 if (it->glyph_row)
23824 append_composite_glyph (it);
23825 }
23826 else if (it->what == IT_COMPOSITION)
23827 {
23828 /* A dynamic (automatic) composition. */
23829 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23830 Lisp_Object gstring;
23831 struct font_metrics metrics;
23832
23833 gstring = composition_gstring_from_id (it->cmp_it.id);
23834 it->pixel_width
23835 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
23836 &metrics);
23837 if (it->glyph_row
23838 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
23839 it->glyph_row->contains_overlapping_glyphs_p = 1;
23840 it->ascent = it->phys_ascent = metrics.ascent;
23841 it->descent = it->phys_descent = metrics.descent;
23842 if (face->box != FACE_NO_BOX)
23843 {
23844 int thick = face->box_line_width;
23845
23846 if (thick > 0)
23847 {
23848 it->ascent += thick;
23849 it->descent += thick;
23850 }
23851 else
23852 thick = - thick;
23853
23854 if (it->start_of_box_run_p)
23855 it->pixel_width += thick;
23856 if (it->end_of_box_run_p)
23857 it->pixel_width += thick;
23858 }
23859 /* If face has an overline, add the height of the overline
23860 (1 pixel) and a 1 pixel margin to the character height. */
23861 if (face->overline_p)
23862 it->ascent += overline_margin;
23863 take_vertical_position_into_account (it);
23864 if (it->ascent < 0)
23865 it->ascent = 0;
23866 if (it->descent < 0)
23867 it->descent = 0;
23868
23869 if (it->glyph_row)
23870 append_composite_glyph (it);
23871 }
23872 else if (it->what == IT_GLYPHLESS)
23873 produce_glyphless_glyph (it, 0, Qnil);
23874 else if (it->what == IT_IMAGE)
23875 produce_image_glyph (it);
23876 else if (it->what == IT_STRETCH)
23877 produce_stretch_glyph (it);
23878
23879 done:
23880 /* Accumulate dimensions. Note: can't assume that it->descent > 0
23881 because this isn't true for images with `:ascent 100'. */
23882 xassert (it->ascent >= 0 && it->descent >= 0);
23883 if (it->area == TEXT_AREA)
23884 it->current_x += it->pixel_width;
23885
23886 if (extra_line_spacing > 0)
23887 {
23888 it->descent += extra_line_spacing;
23889 if (extra_line_spacing > it->max_extra_line_spacing)
23890 it->max_extra_line_spacing = extra_line_spacing;
23891 }
23892
23893 it->max_ascent = max (it->max_ascent, it->ascent);
23894 it->max_descent = max (it->max_descent, it->descent);
23895 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
23896 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
23897 }
23898
23899 /* EXPORT for RIF:
23900 Output LEN glyphs starting at START at the nominal cursor position.
23901 Advance the nominal cursor over the text. The global variable
23902 updated_window contains the window being updated, updated_row is
23903 the glyph row being updated, and updated_area is the area of that
23904 row being updated. */
23905
23906 void
23907 x_write_glyphs (struct glyph *start, int len)
23908 {
23909 int x, hpos;
23910
23911 xassert (updated_window && updated_row);
23912 BLOCK_INPUT;
23913
23914 /* Write glyphs. */
23915
23916 hpos = start - updated_row->glyphs[updated_area];
23917 x = draw_glyphs (updated_window, output_cursor.x,
23918 updated_row, updated_area,
23919 hpos, hpos + len,
23920 DRAW_NORMAL_TEXT, 0);
23921
23922 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
23923 if (updated_area == TEXT_AREA
23924 && updated_window->phys_cursor_on_p
23925 && updated_window->phys_cursor.vpos == output_cursor.vpos
23926 && updated_window->phys_cursor.hpos >= hpos
23927 && updated_window->phys_cursor.hpos < hpos + len)
23928 updated_window->phys_cursor_on_p = 0;
23929
23930 UNBLOCK_INPUT;
23931
23932 /* Advance the output cursor. */
23933 output_cursor.hpos += len;
23934 output_cursor.x = x;
23935 }
23936
23937
23938 /* EXPORT for RIF:
23939 Insert LEN glyphs from START at the nominal cursor position. */
23940
23941 void
23942 x_insert_glyphs (struct glyph *start, int len)
23943 {
23944 struct frame *f;
23945 struct window *w;
23946 int line_height, shift_by_width, shifted_region_width;
23947 struct glyph_row *row;
23948 struct glyph *glyph;
23949 int frame_x, frame_y;
23950 EMACS_INT hpos;
23951
23952 xassert (updated_window && updated_row);
23953 BLOCK_INPUT;
23954 w = updated_window;
23955 f = XFRAME (WINDOW_FRAME (w));
23956
23957 /* Get the height of the line we are in. */
23958 row = updated_row;
23959 line_height = row->height;
23960
23961 /* Get the width of the glyphs to insert. */
23962 shift_by_width = 0;
23963 for (glyph = start; glyph < start + len; ++glyph)
23964 shift_by_width += glyph->pixel_width;
23965
23966 /* Get the width of the region to shift right. */
23967 shifted_region_width = (window_box_width (w, updated_area)
23968 - output_cursor.x
23969 - shift_by_width);
23970
23971 /* Shift right. */
23972 frame_x = window_box_left (w, updated_area) + output_cursor.x;
23973 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
23974
23975 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
23976 line_height, shift_by_width);
23977
23978 /* Write the glyphs. */
23979 hpos = start - row->glyphs[updated_area];
23980 draw_glyphs (w, output_cursor.x, row, updated_area,
23981 hpos, hpos + len,
23982 DRAW_NORMAL_TEXT, 0);
23983
23984 /* Advance the output cursor. */
23985 output_cursor.hpos += len;
23986 output_cursor.x += shift_by_width;
23987 UNBLOCK_INPUT;
23988 }
23989
23990
23991 /* EXPORT for RIF:
23992 Erase the current text line from the nominal cursor position
23993 (inclusive) to pixel column TO_X (exclusive). The idea is that
23994 everything from TO_X onward is already erased.
23995
23996 TO_X is a pixel position relative to updated_area of
23997 updated_window. TO_X == -1 means clear to the end of this area. */
23998
23999 void
24000 x_clear_end_of_line (int to_x)
24001 {
24002 struct frame *f;
24003 struct window *w = updated_window;
24004 int max_x, min_y, max_y;
24005 int from_x, from_y, to_y;
24006
24007 xassert (updated_window && updated_row);
24008 f = XFRAME (w->frame);
24009
24010 if (updated_row->full_width_p)
24011 max_x = WINDOW_TOTAL_WIDTH (w);
24012 else
24013 max_x = window_box_width (w, updated_area);
24014 max_y = window_text_bottom_y (w);
24015
24016 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
24017 of window. For TO_X > 0, truncate to end of drawing area. */
24018 if (to_x == 0)
24019 return;
24020 else if (to_x < 0)
24021 to_x = max_x;
24022 else
24023 to_x = min (to_x, max_x);
24024
24025 to_y = min (max_y, output_cursor.y + updated_row->height);
24026
24027 /* Notice if the cursor will be cleared by this operation. */
24028 if (!updated_row->full_width_p)
24029 notice_overwritten_cursor (w, updated_area,
24030 output_cursor.x, -1,
24031 updated_row->y,
24032 MATRIX_ROW_BOTTOM_Y (updated_row));
24033
24034 from_x = output_cursor.x;
24035
24036 /* Translate to frame coordinates. */
24037 if (updated_row->full_width_p)
24038 {
24039 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
24040 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
24041 }
24042 else
24043 {
24044 int area_left = window_box_left (w, updated_area);
24045 from_x += area_left;
24046 to_x += area_left;
24047 }
24048
24049 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
24050 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
24051 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
24052
24053 /* Prevent inadvertently clearing to end of the X window. */
24054 if (to_x > from_x && to_y > from_y)
24055 {
24056 BLOCK_INPUT;
24057 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
24058 to_x - from_x, to_y - from_y);
24059 UNBLOCK_INPUT;
24060 }
24061 }
24062
24063 #endif /* HAVE_WINDOW_SYSTEM */
24064
24065
24066 \f
24067 /***********************************************************************
24068 Cursor types
24069 ***********************************************************************/
24070
24071 /* Value is the internal representation of the specified cursor type
24072 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
24073 of the bar cursor. */
24074
24075 static enum text_cursor_kinds
24076 get_specified_cursor_type (Lisp_Object arg, int *width)
24077 {
24078 enum text_cursor_kinds type;
24079
24080 if (NILP (arg))
24081 return NO_CURSOR;
24082
24083 if (EQ (arg, Qbox))
24084 return FILLED_BOX_CURSOR;
24085
24086 if (EQ (arg, Qhollow))
24087 return HOLLOW_BOX_CURSOR;
24088
24089 if (EQ (arg, Qbar))
24090 {
24091 *width = 2;
24092 return BAR_CURSOR;
24093 }
24094
24095 if (CONSP (arg)
24096 && EQ (XCAR (arg), Qbar)
24097 && INTEGERP (XCDR (arg))
24098 && XINT (XCDR (arg)) >= 0)
24099 {
24100 *width = XINT (XCDR (arg));
24101 return BAR_CURSOR;
24102 }
24103
24104 if (EQ (arg, Qhbar))
24105 {
24106 *width = 2;
24107 return HBAR_CURSOR;
24108 }
24109
24110 if (CONSP (arg)
24111 && EQ (XCAR (arg), Qhbar)
24112 && INTEGERP (XCDR (arg))
24113 && XINT (XCDR (arg)) >= 0)
24114 {
24115 *width = XINT (XCDR (arg));
24116 return HBAR_CURSOR;
24117 }
24118
24119 /* Treat anything unknown as "hollow box cursor".
24120 It was bad to signal an error; people have trouble fixing
24121 .Xdefaults with Emacs, when it has something bad in it. */
24122 type = HOLLOW_BOX_CURSOR;
24123
24124 return type;
24125 }
24126
24127 /* Set the default cursor types for specified frame. */
24128 void
24129 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
24130 {
24131 int width = 1;
24132 Lisp_Object tem;
24133
24134 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
24135 FRAME_CURSOR_WIDTH (f) = width;
24136
24137 /* By default, set up the blink-off state depending on the on-state. */
24138
24139 tem = Fassoc (arg, Vblink_cursor_alist);
24140 if (!NILP (tem))
24141 {
24142 FRAME_BLINK_OFF_CURSOR (f)
24143 = get_specified_cursor_type (XCDR (tem), &width);
24144 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
24145 }
24146 else
24147 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
24148 }
24149
24150
24151 #ifdef HAVE_WINDOW_SYSTEM
24152
24153 /* Return the cursor we want to be displayed in window W. Return
24154 width of bar/hbar cursor through WIDTH arg. Return with
24155 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
24156 (i.e. if the `system caret' should track this cursor).
24157
24158 In a mini-buffer window, we want the cursor only to appear if we
24159 are reading input from this window. For the selected window, we
24160 want the cursor type given by the frame parameter or buffer local
24161 setting of cursor-type. If explicitly marked off, draw no cursor.
24162 In all other cases, we want a hollow box cursor. */
24163
24164 static enum text_cursor_kinds
24165 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
24166 int *active_cursor)
24167 {
24168 struct frame *f = XFRAME (w->frame);
24169 struct buffer *b = XBUFFER (w->buffer);
24170 int cursor_type = DEFAULT_CURSOR;
24171 Lisp_Object alt_cursor;
24172 int non_selected = 0;
24173
24174 *active_cursor = 1;
24175
24176 /* Echo area */
24177 if (cursor_in_echo_area
24178 && FRAME_HAS_MINIBUF_P (f)
24179 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
24180 {
24181 if (w == XWINDOW (echo_area_window))
24182 {
24183 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
24184 {
24185 *width = FRAME_CURSOR_WIDTH (f);
24186 return FRAME_DESIRED_CURSOR (f);
24187 }
24188 else
24189 return get_specified_cursor_type (BVAR (b, cursor_type), width);
24190 }
24191
24192 *active_cursor = 0;
24193 non_selected = 1;
24194 }
24195
24196 /* Detect a nonselected window or nonselected frame. */
24197 else if (w != XWINDOW (f->selected_window)
24198 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
24199 {
24200 *active_cursor = 0;
24201
24202 if (MINI_WINDOW_P (w) && minibuf_level == 0)
24203 return NO_CURSOR;
24204
24205 non_selected = 1;
24206 }
24207
24208 /* Never display a cursor in a window in which cursor-type is nil. */
24209 if (NILP (BVAR (b, cursor_type)))
24210 return NO_CURSOR;
24211
24212 /* Get the normal cursor type for this window. */
24213 if (EQ (BVAR (b, cursor_type), Qt))
24214 {
24215 cursor_type = FRAME_DESIRED_CURSOR (f);
24216 *width = FRAME_CURSOR_WIDTH (f);
24217 }
24218 else
24219 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
24220
24221 /* Use cursor-in-non-selected-windows instead
24222 for non-selected window or frame. */
24223 if (non_selected)
24224 {
24225 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
24226 if (!EQ (Qt, alt_cursor))
24227 return get_specified_cursor_type (alt_cursor, width);
24228 /* t means modify the normal cursor type. */
24229 if (cursor_type == FILLED_BOX_CURSOR)
24230 cursor_type = HOLLOW_BOX_CURSOR;
24231 else if (cursor_type == BAR_CURSOR && *width > 1)
24232 --*width;
24233 return cursor_type;
24234 }
24235
24236 /* Use normal cursor if not blinked off. */
24237 if (!w->cursor_off_p)
24238 {
24239 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
24240 {
24241 if (cursor_type == FILLED_BOX_CURSOR)
24242 {
24243 /* Using a block cursor on large images can be very annoying.
24244 So use a hollow cursor for "large" images.
24245 If image is not transparent (no mask), also use hollow cursor. */
24246 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
24247 if (img != NULL && IMAGEP (img->spec))
24248 {
24249 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
24250 where N = size of default frame font size.
24251 This should cover most of the "tiny" icons people may use. */
24252 if (!img->mask
24253 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
24254 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
24255 cursor_type = HOLLOW_BOX_CURSOR;
24256 }
24257 }
24258 else if (cursor_type != NO_CURSOR)
24259 {
24260 /* Display current only supports BOX and HOLLOW cursors for images.
24261 So for now, unconditionally use a HOLLOW cursor when cursor is
24262 not a solid box cursor. */
24263 cursor_type = HOLLOW_BOX_CURSOR;
24264 }
24265 }
24266 return cursor_type;
24267 }
24268
24269 /* Cursor is blinked off, so determine how to "toggle" it. */
24270
24271 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
24272 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
24273 return get_specified_cursor_type (XCDR (alt_cursor), width);
24274
24275 /* Then see if frame has specified a specific blink off cursor type. */
24276 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
24277 {
24278 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
24279 return FRAME_BLINK_OFF_CURSOR (f);
24280 }
24281
24282 #if 0
24283 /* Some people liked having a permanently visible blinking cursor,
24284 while others had very strong opinions against it. So it was
24285 decided to remove it. KFS 2003-09-03 */
24286
24287 /* Finally perform built-in cursor blinking:
24288 filled box <-> hollow box
24289 wide [h]bar <-> narrow [h]bar
24290 narrow [h]bar <-> no cursor
24291 other type <-> no cursor */
24292
24293 if (cursor_type == FILLED_BOX_CURSOR)
24294 return HOLLOW_BOX_CURSOR;
24295
24296 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
24297 {
24298 *width = 1;
24299 return cursor_type;
24300 }
24301 #endif
24302
24303 return NO_CURSOR;
24304 }
24305
24306
24307 /* Notice when the text cursor of window W has been completely
24308 overwritten by a drawing operation that outputs glyphs in AREA
24309 starting at X0 and ending at X1 in the line starting at Y0 and
24310 ending at Y1. X coordinates are area-relative. X1 < 0 means all
24311 the rest of the line after X0 has been written. Y coordinates
24312 are window-relative. */
24313
24314 static void
24315 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
24316 int x0, int x1, int y0, int y1)
24317 {
24318 int cx0, cx1, cy0, cy1;
24319 struct glyph_row *row;
24320
24321 if (!w->phys_cursor_on_p)
24322 return;
24323 if (area != TEXT_AREA)
24324 return;
24325
24326 if (w->phys_cursor.vpos < 0
24327 || w->phys_cursor.vpos >= w->current_matrix->nrows
24328 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
24329 !(row->enabled_p && row->displays_text_p)))
24330 return;
24331
24332 if (row->cursor_in_fringe_p)
24333 {
24334 row->cursor_in_fringe_p = 0;
24335 draw_fringe_bitmap (w, row, row->reversed_p);
24336 w->phys_cursor_on_p = 0;
24337 return;
24338 }
24339
24340 cx0 = w->phys_cursor.x;
24341 cx1 = cx0 + w->phys_cursor_width;
24342 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
24343 return;
24344
24345 /* The cursor image will be completely removed from the
24346 screen if the output area intersects the cursor area in
24347 y-direction. When we draw in [y0 y1[, and some part of
24348 the cursor is at y < y0, that part must have been drawn
24349 before. When scrolling, the cursor is erased before
24350 actually scrolling, so we don't come here. When not
24351 scrolling, the rows above the old cursor row must have
24352 changed, and in this case these rows must have written
24353 over the cursor image.
24354
24355 Likewise if part of the cursor is below y1, with the
24356 exception of the cursor being in the first blank row at
24357 the buffer and window end because update_text_area
24358 doesn't draw that row. (Except when it does, but
24359 that's handled in update_text_area.) */
24360
24361 cy0 = w->phys_cursor.y;
24362 cy1 = cy0 + w->phys_cursor_height;
24363 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
24364 return;
24365
24366 w->phys_cursor_on_p = 0;
24367 }
24368
24369 #endif /* HAVE_WINDOW_SYSTEM */
24370
24371 \f
24372 /************************************************************************
24373 Mouse Face
24374 ************************************************************************/
24375
24376 #ifdef HAVE_WINDOW_SYSTEM
24377
24378 /* EXPORT for RIF:
24379 Fix the display of area AREA of overlapping row ROW in window W
24380 with respect to the overlapping part OVERLAPS. */
24381
24382 void
24383 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
24384 enum glyph_row_area area, int overlaps)
24385 {
24386 int i, x;
24387
24388 BLOCK_INPUT;
24389
24390 x = 0;
24391 for (i = 0; i < row->used[area];)
24392 {
24393 if (row->glyphs[area][i].overlaps_vertically_p)
24394 {
24395 int start = i, start_x = x;
24396
24397 do
24398 {
24399 x += row->glyphs[area][i].pixel_width;
24400 ++i;
24401 }
24402 while (i < row->used[area]
24403 && row->glyphs[area][i].overlaps_vertically_p);
24404
24405 draw_glyphs (w, start_x, row, area,
24406 start, i,
24407 DRAW_NORMAL_TEXT, overlaps);
24408 }
24409 else
24410 {
24411 x += row->glyphs[area][i].pixel_width;
24412 ++i;
24413 }
24414 }
24415
24416 UNBLOCK_INPUT;
24417 }
24418
24419
24420 /* EXPORT:
24421 Draw the cursor glyph of window W in glyph row ROW. See the
24422 comment of draw_glyphs for the meaning of HL. */
24423
24424 void
24425 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
24426 enum draw_glyphs_face hl)
24427 {
24428 /* If cursor hpos is out of bounds, don't draw garbage. This can
24429 happen in mini-buffer windows when switching between echo area
24430 glyphs and mini-buffer. */
24431 if ((row->reversed_p
24432 ? (w->phys_cursor.hpos >= 0)
24433 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
24434 {
24435 int on_p = w->phys_cursor_on_p;
24436 int x1;
24437 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA,
24438 w->phys_cursor.hpos, w->phys_cursor.hpos + 1,
24439 hl, 0);
24440 w->phys_cursor_on_p = on_p;
24441
24442 if (hl == DRAW_CURSOR)
24443 w->phys_cursor_width = x1 - w->phys_cursor.x;
24444 /* When we erase the cursor, and ROW is overlapped by other
24445 rows, make sure that these overlapping parts of other rows
24446 are redrawn. */
24447 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
24448 {
24449 w->phys_cursor_width = x1 - w->phys_cursor.x;
24450
24451 if (row > w->current_matrix->rows
24452 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
24453 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
24454 OVERLAPS_ERASED_CURSOR);
24455
24456 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
24457 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
24458 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
24459 OVERLAPS_ERASED_CURSOR);
24460 }
24461 }
24462 }
24463
24464
24465 /* EXPORT:
24466 Erase the image of a cursor of window W from the screen. */
24467
24468 void
24469 erase_phys_cursor (struct window *w)
24470 {
24471 struct frame *f = XFRAME (w->frame);
24472 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
24473 int hpos = w->phys_cursor.hpos;
24474 int vpos = w->phys_cursor.vpos;
24475 int mouse_face_here_p = 0;
24476 struct glyph_matrix *active_glyphs = w->current_matrix;
24477 struct glyph_row *cursor_row;
24478 struct glyph *cursor_glyph;
24479 enum draw_glyphs_face hl;
24480
24481 /* No cursor displayed or row invalidated => nothing to do on the
24482 screen. */
24483 if (w->phys_cursor_type == NO_CURSOR)
24484 goto mark_cursor_off;
24485
24486 /* VPOS >= active_glyphs->nrows means that window has been resized.
24487 Don't bother to erase the cursor. */
24488 if (vpos >= active_glyphs->nrows)
24489 goto mark_cursor_off;
24490
24491 /* If row containing cursor is marked invalid, there is nothing we
24492 can do. */
24493 cursor_row = MATRIX_ROW (active_glyphs, vpos);
24494 if (!cursor_row->enabled_p)
24495 goto mark_cursor_off;
24496
24497 /* If line spacing is > 0, old cursor may only be partially visible in
24498 window after split-window. So adjust visible height. */
24499 cursor_row->visible_height = min (cursor_row->visible_height,
24500 window_text_bottom_y (w) - cursor_row->y);
24501
24502 /* If row is completely invisible, don't attempt to delete a cursor which
24503 isn't there. This can happen if cursor is at top of a window, and
24504 we switch to a buffer with a header line in that window. */
24505 if (cursor_row->visible_height <= 0)
24506 goto mark_cursor_off;
24507
24508 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
24509 if (cursor_row->cursor_in_fringe_p)
24510 {
24511 cursor_row->cursor_in_fringe_p = 0;
24512 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
24513 goto mark_cursor_off;
24514 }
24515
24516 /* This can happen when the new row is shorter than the old one.
24517 In this case, either draw_glyphs or clear_end_of_line
24518 should have cleared the cursor. Note that we wouldn't be
24519 able to erase the cursor in this case because we don't have a
24520 cursor glyph at hand. */
24521 if ((cursor_row->reversed_p
24522 ? (w->phys_cursor.hpos < 0)
24523 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
24524 goto mark_cursor_off;
24525
24526 /* If the cursor is in the mouse face area, redisplay that when
24527 we clear the cursor. */
24528 if (! NILP (hlinfo->mouse_face_window)
24529 && coords_in_mouse_face_p (w, hpos, vpos)
24530 /* Don't redraw the cursor's spot in mouse face if it is at the
24531 end of a line (on a newline). The cursor appears there, but
24532 mouse highlighting does not. */
24533 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
24534 mouse_face_here_p = 1;
24535
24536 /* Maybe clear the display under the cursor. */
24537 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
24538 {
24539 int x, y, left_x;
24540 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
24541 int width;
24542
24543 cursor_glyph = get_phys_cursor_glyph (w);
24544 if (cursor_glyph == NULL)
24545 goto mark_cursor_off;
24546
24547 width = cursor_glyph->pixel_width;
24548 left_x = window_box_left_offset (w, TEXT_AREA);
24549 x = w->phys_cursor.x;
24550 if (x < left_x)
24551 width -= left_x - x;
24552 width = min (width, window_box_width (w, TEXT_AREA) - x);
24553 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
24554 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
24555
24556 if (width > 0)
24557 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
24558 }
24559
24560 /* Erase the cursor by redrawing the character underneath it. */
24561 if (mouse_face_here_p)
24562 hl = DRAW_MOUSE_FACE;
24563 else
24564 hl = DRAW_NORMAL_TEXT;
24565 draw_phys_cursor_glyph (w, cursor_row, hl);
24566
24567 mark_cursor_off:
24568 w->phys_cursor_on_p = 0;
24569 w->phys_cursor_type = NO_CURSOR;
24570 }
24571
24572
24573 /* EXPORT:
24574 Display or clear cursor of window W. If ON is zero, clear the
24575 cursor. If it is non-zero, display the cursor. If ON is nonzero,
24576 where to put the cursor is specified by HPOS, VPOS, X and Y. */
24577
24578 void
24579 display_and_set_cursor (struct window *w, int on,
24580 int hpos, int vpos, int x, int y)
24581 {
24582 struct frame *f = XFRAME (w->frame);
24583 int new_cursor_type;
24584 int new_cursor_width;
24585 int active_cursor;
24586 struct glyph_row *glyph_row;
24587 struct glyph *glyph;
24588
24589 /* This is pointless on invisible frames, and dangerous on garbaged
24590 windows and frames; in the latter case, the frame or window may
24591 be in the midst of changing its size, and x and y may be off the
24592 window. */
24593 if (! FRAME_VISIBLE_P (f)
24594 || FRAME_GARBAGED_P (f)
24595 || vpos >= w->current_matrix->nrows
24596 || hpos >= w->current_matrix->matrix_w)
24597 return;
24598
24599 /* If cursor is off and we want it off, return quickly. */
24600 if (!on && !w->phys_cursor_on_p)
24601 return;
24602
24603 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
24604 /* If cursor row is not enabled, we don't really know where to
24605 display the cursor. */
24606 if (!glyph_row->enabled_p)
24607 {
24608 w->phys_cursor_on_p = 0;
24609 return;
24610 }
24611
24612 glyph = NULL;
24613 if (!glyph_row->exact_window_width_line_p
24614 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
24615 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
24616
24617 xassert (interrupt_input_blocked);
24618
24619 /* Set new_cursor_type to the cursor we want to be displayed. */
24620 new_cursor_type = get_window_cursor_type (w, glyph,
24621 &new_cursor_width, &active_cursor);
24622
24623 /* If cursor is currently being shown and we don't want it to be or
24624 it is in the wrong place, or the cursor type is not what we want,
24625 erase it. */
24626 if (w->phys_cursor_on_p
24627 && (!on
24628 || w->phys_cursor.x != x
24629 || w->phys_cursor.y != y
24630 || new_cursor_type != w->phys_cursor_type
24631 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
24632 && new_cursor_width != w->phys_cursor_width)))
24633 erase_phys_cursor (w);
24634
24635 /* Don't check phys_cursor_on_p here because that flag is only set
24636 to zero in some cases where we know that the cursor has been
24637 completely erased, to avoid the extra work of erasing the cursor
24638 twice. In other words, phys_cursor_on_p can be 1 and the cursor
24639 still not be visible, or it has only been partly erased. */
24640 if (on)
24641 {
24642 w->phys_cursor_ascent = glyph_row->ascent;
24643 w->phys_cursor_height = glyph_row->height;
24644
24645 /* Set phys_cursor_.* before x_draw_.* is called because some
24646 of them may need the information. */
24647 w->phys_cursor.x = x;
24648 w->phys_cursor.y = glyph_row->y;
24649 w->phys_cursor.hpos = hpos;
24650 w->phys_cursor.vpos = vpos;
24651 }
24652
24653 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
24654 new_cursor_type, new_cursor_width,
24655 on, active_cursor);
24656 }
24657
24658
24659 /* Switch the display of W's cursor on or off, according to the value
24660 of ON. */
24661
24662 static void
24663 update_window_cursor (struct window *w, int on)
24664 {
24665 /* Don't update cursor in windows whose frame is in the process
24666 of being deleted. */
24667 if (w->current_matrix)
24668 {
24669 BLOCK_INPUT;
24670 display_and_set_cursor (w, on, w->phys_cursor.hpos, w->phys_cursor.vpos,
24671 w->phys_cursor.x, w->phys_cursor.y);
24672 UNBLOCK_INPUT;
24673 }
24674 }
24675
24676
24677 /* Call update_window_cursor with parameter ON_P on all leaf windows
24678 in the window tree rooted at W. */
24679
24680 static void
24681 update_cursor_in_window_tree (struct window *w, int on_p)
24682 {
24683 while (w)
24684 {
24685 if (!NILP (w->hchild))
24686 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
24687 else if (!NILP (w->vchild))
24688 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
24689 else
24690 update_window_cursor (w, on_p);
24691
24692 w = NILP (w->next) ? 0 : XWINDOW (w->next);
24693 }
24694 }
24695
24696
24697 /* EXPORT:
24698 Display the cursor on window W, or clear it, according to ON_P.
24699 Don't change the cursor's position. */
24700
24701 void
24702 x_update_cursor (struct frame *f, int on_p)
24703 {
24704 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
24705 }
24706
24707
24708 /* EXPORT:
24709 Clear the cursor of window W to background color, and mark the
24710 cursor as not shown. This is used when the text where the cursor
24711 is about to be rewritten. */
24712
24713 void
24714 x_clear_cursor (struct window *w)
24715 {
24716 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
24717 update_window_cursor (w, 0);
24718 }
24719
24720 #endif /* HAVE_WINDOW_SYSTEM */
24721
24722 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
24723 and MSDOS. */
24724 static void
24725 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
24726 int start_hpos, int end_hpos,
24727 enum draw_glyphs_face draw)
24728 {
24729 #ifdef HAVE_WINDOW_SYSTEM
24730 if (FRAME_WINDOW_P (XFRAME (w->frame)))
24731 {
24732 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
24733 return;
24734 }
24735 #endif
24736 #if defined (HAVE_GPM) || defined (MSDOS)
24737 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
24738 #endif
24739 }
24740
24741 /* Display the active region described by mouse_face_* according to DRAW. */
24742
24743 static void
24744 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
24745 {
24746 struct window *w = XWINDOW (hlinfo->mouse_face_window);
24747 struct frame *f = XFRAME (WINDOW_FRAME (w));
24748
24749 if (/* If window is in the process of being destroyed, don't bother
24750 to do anything. */
24751 w->current_matrix != NULL
24752 /* Don't update mouse highlight if hidden */
24753 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
24754 /* Recognize when we are called to operate on rows that don't exist
24755 anymore. This can happen when a window is split. */
24756 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
24757 {
24758 int phys_cursor_on_p = w->phys_cursor_on_p;
24759 struct glyph_row *row, *first, *last;
24760
24761 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
24762 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
24763
24764 for (row = first; row <= last && row->enabled_p; ++row)
24765 {
24766 int start_hpos, end_hpos, start_x;
24767
24768 /* For all but the first row, the highlight starts at column 0. */
24769 if (row == first)
24770 {
24771 /* R2L rows have BEG and END in reversed order, but the
24772 screen drawing geometry is always left to right. So
24773 we need to mirror the beginning and end of the
24774 highlighted area in R2L rows. */
24775 if (!row->reversed_p)
24776 {
24777 start_hpos = hlinfo->mouse_face_beg_col;
24778 start_x = hlinfo->mouse_face_beg_x;
24779 }
24780 else if (row == last)
24781 {
24782 start_hpos = hlinfo->mouse_face_end_col;
24783 start_x = hlinfo->mouse_face_end_x;
24784 }
24785 else
24786 {
24787 start_hpos = 0;
24788 start_x = 0;
24789 }
24790 }
24791 else if (row->reversed_p && row == last)
24792 {
24793 start_hpos = hlinfo->mouse_face_end_col;
24794 start_x = hlinfo->mouse_face_end_x;
24795 }
24796 else
24797 {
24798 start_hpos = 0;
24799 start_x = 0;
24800 }
24801
24802 if (row == last)
24803 {
24804 if (!row->reversed_p)
24805 end_hpos = hlinfo->mouse_face_end_col;
24806 else if (row == first)
24807 end_hpos = hlinfo->mouse_face_beg_col;
24808 else
24809 {
24810 end_hpos = row->used[TEXT_AREA];
24811 if (draw == DRAW_NORMAL_TEXT)
24812 row->fill_line_p = 1; /* Clear to end of line */
24813 }
24814 }
24815 else if (row->reversed_p && row == first)
24816 end_hpos = hlinfo->mouse_face_beg_col;
24817 else
24818 {
24819 end_hpos = row->used[TEXT_AREA];
24820 if (draw == DRAW_NORMAL_TEXT)
24821 row->fill_line_p = 1; /* Clear to end of line */
24822 }
24823
24824 if (end_hpos > start_hpos)
24825 {
24826 draw_row_with_mouse_face (w, start_x, row,
24827 start_hpos, end_hpos, draw);
24828
24829 row->mouse_face_p
24830 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
24831 }
24832 }
24833
24834 #ifdef HAVE_WINDOW_SYSTEM
24835 /* When we've written over the cursor, arrange for it to
24836 be displayed again. */
24837 if (FRAME_WINDOW_P (f)
24838 && phys_cursor_on_p && !w->phys_cursor_on_p)
24839 {
24840 BLOCK_INPUT;
24841 display_and_set_cursor (w, 1,
24842 w->phys_cursor.hpos, w->phys_cursor.vpos,
24843 w->phys_cursor.x, w->phys_cursor.y);
24844 UNBLOCK_INPUT;
24845 }
24846 #endif /* HAVE_WINDOW_SYSTEM */
24847 }
24848
24849 #ifdef HAVE_WINDOW_SYSTEM
24850 /* Change the mouse cursor. */
24851 if (FRAME_WINDOW_P (f))
24852 {
24853 if (draw == DRAW_NORMAL_TEXT
24854 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
24855 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
24856 else if (draw == DRAW_MOUSE_FACE)
24857 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
24858 else
24859 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
24860 }
24861 #endif /* HAVE_WINDOW_SYSTEM */
24862 }
24863
24864 /* EXPORT:
24865 Clear out the mouse-highlighted active region.
24866 Redraw it un-highlighted first. Value is non-zero if mouse
24867 face was actually drawn unhighlighted. */
24868
24869 int
24870 clear_mouse_face (Mouse_HLInfo *hlinfo)
24871 {
24872 int cleared = 0;
24873
24874 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
24875 {
24876 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
24877 cleared = 1;
24878 }
24879
24880 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
24881 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
24882 hlinfo->mouse_face_window = Qnil;
24883 hlinfo->mouse_face_overlay = Qnil;
24884 return cleared;
24885 }
24886
24887 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
24888 within the mouse face on that window. */
24889 static int
24890 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
24891 {
24892 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
24893
24894 /* Quickly resolve the easy cases. */
24895 if (!(WINDOWP (hlinfo->mouse_face_window)
24896 && XWINDOW (hlinfo->mouse_face_window) == w))
24897 return 0;
24898 if (vpos < hlinfo->mouse_face_beg_row
24899 || vpos > hlinfo->mouse_face_end_row)
24900 return 0;
24901 if (vpos > hlinfo->mouse_face_beg_row
24902 && vpos < hlinfo->mouse_face_end_row)
24903 return 1;
24904
24905 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
24906 {
24907 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24908 {
24909 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
24910 return 1;
24911 }
24912 else if ((vpos == hlinfo->mouse_face_beg_row
24913 && hpos >= hlinfo->mouse_face_beg_col)
24914 || (vpos == hlinfo->mouse_face_end_row
24915 && hpos < hlinfo->mouse_face_end_col))
24916 return 1;
24917 }
24918 else
24919 {
24920 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
24921 {
24922 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
24923 return 1;
24924 }
24925 else if ((vpos == hlinfo->mouse_face_beg_row
24926 && hpos <= hlinfo->mouse_face_beg_col)
24927 || (vpos == hlinfo->mouse_face_end_row
24928 && hpos > hlinfo->mouse_face_end_col))
24929 return 1;
24930 }
24931 return 0;
24932 }
24933
24934
24935 /* EXPORT:
24936 Non-zero if physical cursor of window W is within mouse face. */
24937
24938 int
24939 cursor_in_mouse_face_p (struct window *w)
24940 {
24941 return coords_in_mouse_face_p (w, w->phys_cursor.hpos, w->phys_cursor.vpos);
24942 }
24943
24944
24945 \f
24946 /* Find the glyph rows START_ROW and END_ROW of window W that display
24947 characters between buffer positions START_CHARPOS and END_CHARPOS
24948 (excluding END_CHARPOS). This is similar to row_containing_pos,
24949 but is more accurate when bidi reordering makes buffer positions
24950 change non-linearly with glyph rows. */
24951 static void
24952 rows_from_pos_range (struct window *w,
24953 EMACS_INT start_charpos, EMACS_INT end_charpos,
24954 struct glyph_row **start, struct glyph_row **end)
24955 {
24956 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
24957 int last_y = window_text_bottom_y (w);
24958 struct glyph_row *row;
24959
24960 *start = NULL;
24961 *end = NULL;
24962
24963 while (!first->enabled_p
24964 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
24965 first++;
24966
24967 /* Find the START row. */
24968 for (row = first;
24969 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
24970 row++)
24971 {
24972 /* A row can potentially be the START row if the range of the
24973 characters it displays intersects the range
24974 [START_CHARPOS..END_CHARPOS). */
24975 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
24976 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
24977 /* See the commentary in row_containing_pos, for the
24978 explanation of the complicated way to check whether
24979 some position is beyond the end of the characters
24980 displayed by a row. */
24981 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
24982 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
24983 && !row->ends_at_zv_p
24984 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
24985 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
24986 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
24987 && !row->ends_at_zv_p
24988 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
24989 {
24990 /* Found a candidate row. Now make sure at least one of the
24991 glyphs it displays has a charpos from the range
24992 [START_CHARPOS..END_CHARPOS).
24993
24994 This is not obvious because bidi reordering could make
24995 buffer positions of a row be 1,2,3,102,101,100, and if we
24996 want to highlight characters in [50..60), we don't want
24997 this row, even though [50..60) does intersect [1..103),
24998 the range of character positions given by the row's start
24999 and end positions. */
25000 struct glyph *g = row->glyphs[TEXT_AREA];
25001 struct glyph *e = g + row->used[TEXT_AREA];
25002
25003 while (g < e)
25004 {
25005 if (BUFFERP (g->object)
25006 && start_charpos <= g->charpos && g->charpos < end_charpos)
25007 *start = row;
25008 g++;
25009 }
25010 if (*start)
25011 break;
25012 }
25013 }
25014
25015 /* Find the END row. */
25016 if (!*start
25017 /* If the last row is partially visible, start looking for END
25018 from that row, instead of starting from FIRST. */
25019 && !(row->enabled_p
25020 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
25021 row = first;
25022 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
25023 {
25024 struct glyph_row *next = row + 1;
25025
25026 if (!next->enabled_p
25027 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
25028 /* The first row >= START whose range of displayed characters
25029 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
25030 is the row END + 1. */
25031 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
25032 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
25033 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
25034 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
25035 && !next->ends_at_zv_p
25036 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
25037 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
25038 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
25039 && !next->ends_at_zv_p
25040 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
25041 {
25042 *end = row;
25043 break;
25044 }
25045 else
25046 {
25047 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
25048 but none of the characters it displays are in the range, it is
25049 also END + 1. */
25050 struct glyph *g = next->glyphs[TEXT_AREA];
25051 struct glyph *e = g + next->used[TEXT_AREA];
25052
25053 while (g < e)
25054 {
25055 if (BUFFERP (g->object)
25056 && start_charpos <= g->charpos && g->charpos < end_charpos)
25057 break;
25058 g++;
25059 }
25060 if (g == e)
25061 {
25062 *end = row;
25063 break;
25064 }
25065 }
25066 }
25067 }
25068
25069 /* This function sets the mouse_face_* elements of HLINFO, assuming
25070 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
25071 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
25072 for the overlay or run of text properties specifying the mouse
25073 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
25074 before-string and after-string that must also be highlighted.
25075 COVER_STRING, if non-nil, is a display string that may cover some
25076 or all of the highlighted text. */
25077
25078 static void
25079 mouse_face_from_buffer_pos (Lisp_Object window,
25080 Mouse_HLInfo *hlinfo,
25081 EMACS_INT mouse_charpos,
25082 EMACS_INT start_charpos,
25083 EMACS_INT end_charpos,
25084 Lisp_Object before_string,
25085 Lisp_Object after_string,
25086 Lisp_Object cover_string)
25087 {
25088 struct window *w = XWINDOW (window);
25089 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25090 struct glyph_row *r1, *r2;
25091 struct glyph *glyph, *end;
25092 EMACS_INT ignore, pos;
25093 int x;
25094
25095 xassert (NILP (cover_string) || STRINGP (cover_string));
25096 xassert (NILP (before_string) || STRINGP (before_string));
25097 xassert (NILP (after_string) || STRINGP (after_string));
25098
25099 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
25100 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
25101 if (r1 == NULL)
25102 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25103 /* If the before-string or display-string contains newlines,
25104 rows_from_pos_range skips to its last row. Move back. */
25105 if (!NILP (before_string) || !NILP (cover_string))
25106 {
25107 struct glyph_row *prev;
25108 while ((prev = r1 - 1, prev >= first)
25109 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
25110 && prev->used[TEXT_AREA] > 0)
25111 {
25112 struct glyph *beg = prev->glyphs[TEXT_AREA];
25113 glyph = beg + prev->used[TEXT_AREA];
25114 while (--glyph >= beg && INTEGERP (glyph->object));
25115 if (glyph < beg
25116 || !(EQ (glyph->object, before_string)
25117 || EQ (glyph->object, cover_string)))
25118 break;
25119 r1 = prev;
25120 }
25121 }
25122 if (r2 == NULL)
25123 {
25124 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25125 hlinfo->mouse_face_past_end = 1;
25126 }
25127 else if (!NILP (after_string))
25128 {
25129 /* If the after-string has newlines, advance to its last row. */
25130 struct glyph_row *next;
25131 struct glyph_row *last
25132 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25133
25134 for (next = r2 + 1;
25135 next <= last
25136 && next->used[TEXT_AREA] > 0
25137 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
25138 ++next)
25139 r2 = next;
25140 }
25141 /* The rest of the display engine assumes that mouse_face_beg_row is
25142 either above below mouse_face_end_row or identical to it. But
25143 with bidi-reordered continued lines, the row for START_CHARPOS
25144 could be below the row for END_CHARPOS. If so, swap the rows and
25145 store them in correct order. */
25146 if (r1->y > r2->y)
25147 {
25148 struct glyph_row *tem = r2;
25149
25150 r2 = r1;
25151 r1 = tem;
25152 }
25153
25154 hlinfo->mouse_face_beg_y = r1->y;
25155 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
25156 hlinfo->mouse_face_end_y = r2->y;
25157 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
25158
25159 /* For a bidi-reordered row, the positions of BEFORE_STRING,
25160 AFTER_STRING, COVER_STRING, START_CHARPOS, and END_CHARPOS
25161 could be anywhere in the row and in any order. The strategy
25162 below is to find the leftmost and the rightmost glyph that
25163 belongs to either of these 3 strings, or whose position is
25164 between START_CHARPOS and END_CHARPOS, and highlight all the
25165 glyphs between those two. This may cover more than just the text
25166 between START_CHARPOS and END_CHARPOS if the range of characters
25167 strides the bidi level boundary, e.g. if the beginning is in R2L
25168 text while the end is in L2R text or vice versa. */
25169 if (!r1->reversed_p)
25170 {
25171 /* This row is in a left to right paragraph. Scan it left to
25172 right. */
25173 glyph = r1->glyphs[TEXT_AREA];
25174 end = glyph + r1->used[TEXT_AREA];
25175 x = r1->x;
25176
25177 /* Skip truncation glyphs at the start of the glyph row. */
25178 if (r1->displays_text_p)
25179 for (; glyph < end
25180 && INTEGERP (glyph->object)
25181 && glyph->charpos < 0;
25182 ++glyph)
25183 x += glyph->pixel_width;
25184
25185 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25186 or COVER_STRING, and the first glyph from buffer whose
25187 position is between START_CHARPOS and END_CHARPOS. */
25188 for (; glyph < end
25189 && !INTEGERP (glyph->object)
25190 && !EQ (glyph->object, cover_string)
25191 && !(BUFFERP (glyph->object)
25192 && (glyph->charpos >= start_charpos
25193 && glyph->charpos < end_charpos));
25194 ++glyph)
25195 {
25196 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25197 are present at buffer positions between START_CHARPOS and
25198 END_CHARPOS, or if they come from an overlay. */
25199 if (EQ (glyph->object, before_string))
25200 {
25201 pos = string_buffer_position (before_string,
25202 start_charpos);
25203 /* If pos == 0, it means before_string came from an
25204 overlay, not from a buffer position. */
25205 if (!pos || (pos >= start_charpos && pos < end_charpos))
25206 break;
25207 }
25208 else if (EQ (glyph->object, after_string))
25209 {
25210 pos = string_buffer_position (after_string, end_charpos);
25211 if (!pos || (pos >= start_charpos && pos < end_charpos))
25212 break;
25213 }
25214 x += glyph->pixel_width;
25215 }
25216 hlinfo->mouse_face_beg_x = x;
25217 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25218 }
25219 else
25220 {
25221 /* This row is in a right to left paragraph. Scan it right to
25222 left. */
25223 struct glyph *g;
25224
25225 end = r1->glyphs[TEXT_AREA] - 1;
25226 glyph = end + r1->used[TEXT_AREA];
25227
25228 /* Skip truncation glyphs at the start of the glyph row. */
25229 if (r1->displays_text_p)
25230 for (; glyph > end
25231 && INTEGERP (glyph->object)
25232 && glyph->charpos < 0;
25233 --glyph)
25234 ;
25235
25236 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
25237 or COVER_STRING, and the first glyph from buffer whose
25238 position is between START_CHARPOS and END_CHARPOS. */
25239 for (; glyph > end
25240 && !INTEGERP (glyph->object)
25241 && !EQ (glyph->object, cover_string)
25242 && !(BUFFERP (glyph->object)
25243 && (glyph->charpos >= start_charpos
25244 && glyph->charpos < end_charpos));
25245 --glyph)
25246 {
25247 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25248 are present at buffer positions between START_CHARPOS and
25249 END_CHARPOS, or if they come from an overlay. */
25250 if (EQ (glyph->object, before_string))
25251 {
25252 pos = string_buffer_position (before_string, start_charpos);
25253 /* If pos == 0, it means before_string came from an
25254 overlay, not from a buffer position. */
25255 if (!pos || (pos >= start_charpos && pos < end_charpos))
25256 break;
25257 }
25258 else if (EQ (glyph->object, after_string))
25259 {
25260 pos = string_buffer_position (after_string, end_charpos);
25261 if (!pos || (pos >= start_charpos && pos < end_charpos))
25262 break;
25263 }
25264 }
25265
25266 glyph++; /* first glyph to the right of the highlighted area */
25267 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
25268 x += g->pixel_width;
25269 hlinfo->mouse_face_beg_x = x;
25270 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
25271 }
25272
25273 /* If the highlight ends in a different row, compute GLYPH and END
25274 for the end row. Otherwise, reuse the values computed above for
25275 the row where the highlight begins. */
25276 if (r2 != r1)
25277 {
25278 if (!r2->reversed_p)
25279 {
25280 glyph = r2->glyphs[TEXT_AREA];
25281 end = glyph + r2->used[TEXT_AREA];
25282 x = r2->x;
25283 }
25284 else
25285 {
25286 end = r2->glyphs[TEXT_AREA] - 1;
25287 glyph = end + r2->used[TEXT_AREA];
25288 }
25289 }
25290
25291 if (!r2->reversed_p)
25292 {
25293 /* Skip truncation and continuation glyphs near the end of the
25294 row, and also blanks and stretch glyphs inserted by
25295 extend_face_to_end_of_line. */
25296 while (end > glyph
25297 && INTEGERP ((end - 1)->object)
25298 && (end - 1)->charpos <= 0)
25299 --end;
25300 /* Scan the rest of the glyph row from the end, looking for the
25301 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25302 COVER_STRING, or whose position is between START_CHARPOS
25303 and END_CHARPOS */
25304 for (--end;
25305 end > glyph
25306 && !INTEGERP (end->object)
25307 && !EQ (end->object, cover_string)
25308 && !(BUFFERP (end->object)
25309 && (end->charpos >= start_charpos
25310 && end->charpos < end_charpos));
25311 --end)
25312 {
25313 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25314 are present at buffer positions between START_CHARPOS and
25315 END_CHARPOS, or if they come from an overlay. */
25316 if (EQ (end->object, before_string))
25317 {
25318 pos = string_buffer_position (before_string, start_charpos);
25319 if (!pos || (pos >= start_charpos && pos < end_charpos))
25320 break;
25321 }
25322 else if (EQ (end->object, after_string))
25323 {
25324 pos = string_buffer_position (after_string, end_charpos);
25325 if (!pos || (pos >= start_charpos && pos < end_charpos))
25326 break;
25327 }
25328 }
25329 /* Find the X coordinate of the last glyph to be highlighted. */
25330 for (; glyph <= end; ++glyph)
25331 x += glyph->pixel_width;
25332
25333 hlinfo->mouse_face_end_x = x;
25334 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
25335 }
25336 else
25337 {
25338 /* Skip truncation and continuation glyphs near the end of the
25339 row, and also blanks and stretch glyphs inserted by
25340 extend_face_to_end_of_line. */
25341 x = r2->x;
25342 end++;
25343 while (end < glyph
25344 && INTEGERP (end->object)
25345 && end->charpos <= 0)
25346 {
25347 x += end->pixel_width;
25348 ++end;
25349 }
25350 /* Scan the rest of the glyph row from the end, looking for the
25351 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
25352 COVER_STRING, or whose position is between START_CHARPOS
25353 and END_CHARPOS */
25354 for ( ;
25355 end < glyph
25356 && !INTEGERP (end->object)
25357 && !EQ (end->object, cover_string)
25358 && !(BUFFERP (end->object)
25359 && (end->charpos >= start_charpos
25360 && end->charpos < end_charpos));
25361 ++end)
25362 {
25363 /* BEFORE_STRING or AFTER_STRING are only relevant if they
25364 are present at buffer positions between START_CHARPOS and
25365 END_CHARPOS, or if they come from an overlay. */
25366 if (EQ (end->object, before_string))
25367 {
25368 pos = string_buffer_position (before_string, start_charpos);
25369 if (!pos || (pos >= start_charpos && pos < end_charpos))
25370 break;
25371 }
25372 else if (EQ (end->object, after_string))
25373 {
25374 pos = string_buffer_position (after_string, end_charpos);
25375 if (!pos || (pos >= start_charpos && pos < end_charpos))
25376 break;
25377 }
25378 x += end->pixel_width;
25379 }
25380 hlinfo->mouse_face_end_x = x;
25381 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
25382 }
25383
25384 hlinfo->mouse_face_window = window;
25385 hlinfo->mouse_face_face_id
25386 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
25387 mouse_charpos + 1,
25388 !hlinfo->mouse_face_hidden, -1);
25389 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
25390 }
25391
25392 /* The following function is not used anymore (replaced with
25393 mouse_face_from_string_pos), but I leave it here for the time
25394 being, in case someone would. */
25395
25396 #if 0 /* not used */
25397
25398 /* Find the position of the glyph for position POS in OBJECT in
25399 window W's current matrix, and return in *X, *Y the pixel
25400 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
25401
25402 RIGHT_P non-zero means return the position of the right edge of the
25403 glyph, RIGHT_P zero means return the left edge position.
25404
25405 If no glyph for POS exists in the matrix, return the position of
25406 the glyph with the next smaller position that is in the matrix, if
25407 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
25408 exists in the matrix, return the position of the glyph with the
25409 next larger position in OBJECT.
25410
25411 Value is non-zero if a glyph was found. */
25412
25413 static int
25414 fast_find_string_pos (struct window *w, EMACS_INT pos, Lisp_Object object,
25415 int *hpos, int *vpos, int *x, int *y, int right_p)
25416 {
25417 int yb = window_text_bottom_y (w);
25418 struct glyph_row *r;
25419 struct glyph *best_glyph = NULL;
25420 struct glyph_row *best_row = NULL;
25421 int best_x = 0;
25422
25423 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25424 r->enabled_p && r->y < yb;
25425 ++r)
25426 {
25427 struct glyph *g = r->glyphs[TEXT_AREA];
25428 struct glyph *e = g + r->used[TEXT_AREA];
25429 int gx;
25430
25431 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25432 if (EQ (g->object, object))
25433 {
25434 if (g->charpos == pos)
25435 {
25436 best_glyph = g;
25437 best_x = gx;
25438 best_row = r;
25439 goto found;
25440 }
25441 else if (best_glyph == NULL
25442 || ((eabs (g->charpos - pos)
25443 < eabs (best_glyph->charpos - pos))
25444 && (right_p
25445 ? g->charpos < pos
25446 : g->charpos > pos)))
25447 {
25448 best_glyph = g;
25449 best_x = gx;
25450 best_row = r;
25451 }
25452 }
25453 }
25454
25455 found:
25456
25457 if (best_glyph)
25458 {
25459 *x = best_x;
25460 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
25461
25462 if (right_p)
25463 {
25464 *x += best_glyph->pixel_width;
25465 ++*hpos;
25466 }
25467
25468 *y = best_row->y;
25469 *vpos = best_row - w->current_matrix->rows;
25470 }
25471
25472 return best_glyph != NULL;
25473 }
25474 #endif /* not used */
25475
25476 /* Find the positions of the first and the last glyphs in window W's
25477 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
25478 (assumed to be a string), and return in HLINFO's mouse_face_*
25479 members the pixel and column/row coordinates of those glyphs. */
25480
25481 static void
25482 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
25483 Lisp_Object object,
25484 EMACS_INT startpos, EMACS_INT endpos)
25485 {
25486 int yb = window_text_bottom_y (w);
25487 struct glyph_row *r;
25488 struct glyph *g, *e;
25489 int gx;
25490 int found = 0;
25491
25492 /* Find the glyph row with at least one position in the range
25493 [STARTPOS..ENDPOS], and the first glyph in that row whose
25494 position belongs to that range. */
25495 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25496 r->enabled_p && r->y < yb;
25497 ++r)
25498 {
25499 if (!r->reversed_p)
25500 {
25501 g = r->glyphs[TEXT_AREA];
25502 e = g + r->used[TEXT_AREA];
25503 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
25504 if (EQ (g->object, object)
25505 && startpos <= g->charpos && g->charpos <= endpos)
25506 {
25507 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25508 hlinfo->mouse_face_beg_y = r->y;
25509 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25510 hlinfo->mouse_face_beg_x = gx;
25511 found = 1;
25512 break;
25513 }
25514 }
25515 else
25516 {
25517 struct glyph *g1;
25518
25519 e = r->glyphs[TEXT_AREA];
25520 g = e + r->used[TEXT_AREA];
25521 for ( ; g > e; --g)
25522 if (EQ ((g-1)->object, object)
25523 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
25524 {
25525 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
25526 hlinfo->mouse_face_beg_y = r->y;
25527 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
25528 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
25529 gx += g1->pixel_width;
25530 hlinfo->mouse_face_beg_x = gx;
25531 found = 1;
25532 break;
25533 }
25534 }
25535 if (found)
25536 break;
25537 }
25538
25539 if (!found)
25540 return;
25541
25542 /* Starting with the next row, look for the first row which does NOT
25543 include any glyphs whose positions are in the range. */
25544 for (++r; r->enabled_p && r->y < yb; ++r)
25545 {
25546 g = r->glyphs[TEXT_AREA];
25547 e = g + r->used[TEXT_AREA];
25548 found = 0;
25549 for ( ; g < e; ++g)
25550 if (EQ (g->object, object)
25551 && startpos <= g->charpos && g->charpos <= endpos)
25552 {
25553 found = 1;
25554 break;
25555 }
25556 if (!found)
25557 break;
25558 }
25559
25560 /* The highlighted region ends on the previous row. */
25561 r--;
25562
25563 /* Set the end row and its vertical pixel coordinate. */
25564 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
25565 hlinfo->mouse_face_end_y = r->y;
25566
25567 /* Compute and set the end column and the end column's horizontal
25568 pixel coordinate. */
25569 if (!r->reversed_p)
25570 {
25571 g = r->glyphs[TEXT_AREA];
25572 e = g + r->used[TEXT_AREA];
25573 for ( ; e > g; --e)
25574 if (EQ ((e-1)->object, object)
25575 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
25576 break;
25577 hlinfo->mouse_face_end_col = e - g;
25578
25579 for (gx = r->x; g < e; ++g)
25580 gx += g->pixel_width;
25581 hlinfo->mouse_face_end_x = gx;
25582 }
25583 else
25584 {
25585 e = r->glyphs[TEXT_AREA];
25586 g = e + r->used[TEXT_AREA];
25587 for (gx = r->x ; e < g; ++e)
25588 {
25589 if (EQ (e->object, object)
25590 && startpos <= e->charpos && e->charpos <= endpos)
25591 break;
25592 gx += e->pixel_width;
25593 }
25594 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
25595 hlinfo->mouse_face_end_x = gx;
25596 }
25597 }
25598
25599 #ifdef HAVE_WINDOW_SYSTEM
25600
25601 /* See if position X, Y is within a hot-spot of an image. */
25602
25603 static int
25604 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
25605 {
25606 if (!CONSP (hot_spot))
25607 return 0;
25608
25609 if (EQ (XCAR (hot_spot), Qrect))
25610 {
25611 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
25612 Lisp_Object rect = XCDR (hot_spot);
25613 Lisp_Object tem;
25614 if (!CONSP (rect))
25615 return 0;
25616 if (!CONSP (XCAR (rect)))
25617 return 0;
25618 if (!CONSP (XCDR (rect)))
25619 return 0;
25620 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
25621 return 0;
25622 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
25623 return 0;
25624 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
25625 return 0;
25626 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
25627 return 0;
25628 return 1;
25629 }
25630 else if (EQ (XCAR (hot_spot), Qcircle))
25631 {
25632 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
25633 Lisp_Object circ = XCDR (hot_spot);
25634 Lisp_Object lr, lx0, ly0;
25635 if (CONSP (circ)
25636 && CONSP (XCAR (circ))
25637 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
25638 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
25639 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
25640 {
25641 double r = XFLOATINT (lr);
25642 double dx = XINT (lx0) - x;
25643 double dy = XINT (ly0) - y;
25644 return (dx * dx + dy * dy <= r * r);
25645 }
25646 }
25647 else if (EQ (XCAR (hot_spot), Qpoly))
25648 {
25649 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
25650 if (VECTORP (XCDR (hot_spot)))
25651 {
25652 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
25653 Lisp_Object *poly = v->contents;
25654 int n = v->header.size;
25655 int i;
25656 int inside = 0;
25657 Lisp_Object lx, ly;
25658 int x0, y0;
25659
25660 /* Need an even number of coordinates, and at least 3 edges. */
25661 if (n < 6 || n & 1)
25662 return 0;
25663
25664 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
25665 If count is odd, we are inside polygon. Pixels on edges
25666 may or may not be included depending on actual geometry of the
25667 polygon. */
25668 if ((lx = poly[n-2], !INTEGERP (lx))
25669 || (ly = poly[n-1], !INTEGERP (lx)))
25670 return 0;
25671 x0 = XINT (lx), y0 = XINT (ly);
25672 for (i = 0; i < n; i += 2)
25673 {
25674 int x1 = x0, y1 = y0;
25675 if ((lx = poly[i], !INTEGERP (lx))
25676 || (ly = poly[i+1], !INTEGERP (ly)))
25677 return 0;
25678 x0 = XINT (lx), y0 = XINT (ly);
25679
25680 /* Does this segment cross the X line? */
25681 if (x0 >= x)
25682 {
25683 if (x1 >= x)
25684 continue;
25685 }
25686 else if (x1 < x)
25687 continue;
25688 if (y > y0 && y > y1)
25689 continue;
25690 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
25691 inside = !inside;
25692 }
25693 return inside;
25694 }
25695 }
25696 return 0;
25697 }
25698
25699 Lisp_Object
25700 find_hot_spot (Lisp_Object map, int x, int y)
25701 {
25702 while (CONSP (map))
25703 {
25704 if (CONSP (XCAR (map))
25705 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
25706 return XCAR (map);
25707 map = XCDR (map);
25708 }
25709
25710 return Qnil;
25711 }
25712
25713 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
25714 3, 3, 0,
25715 doc: /* Lookup in image map MAP coordinates X and Y.
25716 An image map is an alist where each element has the format (AREA ID PLIST).
25717 An AREA is specified as either a rectangle, a circle, or a polygon:
25718 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
25719 pixel coordinates of the upper left and bottom right corners.
25720 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
25721 and the radius of the circle; r may be a float or integer.
25722 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
25723 vector describes one corner in the polygon.
25724 Returns the alist element for the first matching AREA in MAP. */)
25725 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
25726 {
25727 if (NILP (map))
25728 return Qnil;
25729
25730 CHECK_NUMBER (x);
25731 CHECK_NUMBER (y);
25732
25733 return find_hot_spot (map, XINT (x), XINT (y));
25734 }
25735
25736
25737 /* Display frame CURSOR, optionally using shape defined by POINTER. */
25738 static void
25739 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
25740 {
25741 /* Do not change cursor shape while dragging mouse. */
25742 if (!NILP (do_mouse_tracking))
25743 return;
25744
25745 if (!NILP (pointer))
25746 {
25747 if (EQ (pointer, Qarrow))
25748 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25749 else if (EQ (pointer, Qhand))
25750 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
25751 else if (EQ (pointer, Qtext))
25752 cursor = FRAME_X_OUTPUT (f)->text_cursor;
25753 else if (EQ (pointer, intern ("hdrag")))
25754 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
25755 #ifdef HAVE_X_WINDOWS
25756 else if (EQ (pointer, intern ("vdrag")))
25757 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
25758 #endif
25759 else if (EQ (pointer, intern ("hourglass")))
25760 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
25761 else if (EQ (pointer, Qmodeline))
25762 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
25763 else
25764 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25765 }
25766
25767 if (cursor != No_Cursor)
25768 FRAME_RIF (f)->define_frame_cursor (f, cursor);
25769 }
25770
25771 #endif /* HAVE_WINDOW_SYSTEM */
25772
25773 /* Take proper action when mouse has moved to the mode or header line
25774 or marginal area AREA of window W, x-position X and y-position Y.
25775 X is relative to the start of the text display area of W, so the
25776 width of bitmap areas and scroll bars must be subtracted to get a
25777 position relative to the start of the mode line. */
25778
25779 static void
25780 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
25781 enum window_part area)
25782 {
25783 struct window *w = XWINDOW (window);
25784 struct frame *f = XFRAME (w->frame);
25785 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25786 #ifdef HAVE_WINDOW_SYSTEM
25787 Display_Info *dpyinfo;
25788 #endif
25789 Cursor cursor = No_Cursor;
25790 Lisp_Object pointer = Qnil;
25791 int dx, dy, width, height;
25792 EMACS_INT charpos;
25793 Lisp_Object string, object = Qnil;
25794 Lisp_Object pos, help;
25795
25796 Lisp_Object mouse_face;
25797 int original_x_pixel = x;
25798 struct glyph * glyph = NULL, * row_start_glyph = NULL;
25799 struct glyph_row *row;
25800
25801 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
25802 {
25803 int x0;
25804 struct glyph *end;
25805
25806 /* Kludge alert: mode_line_string takes X/Y in pixels, but
25807 returns them in row/column units! */
25808 string = mode_line_string (w, area, &x, &y, &charpos,
25809 &object, &dx, &dy, &width, &height);
25810
25811 row = (area == ON_MODE_LINE
25812 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
25813 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
25814
25815 /* Find the glyph under the mouse pointer. */
25816 if (row->mode_line_p && row->enabled_p)
25817 {
25818 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
25819 end = glyph + row->used[TEXT_AREA];
25820
25821 for (x0 = original_x_pixel;
25822 glyph < end && x0 >= glyph->pixel_width;
25823 ++glyph)
25824 x0 -= glyph->pixel_width;
25825
25826 if (glyph >= end)
25827 glyph = NULL;
25828 }
25829 }
25830 else
25831 {
25832 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
25833 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
25834 returns them in row/column units! */
25835 string = marginal_area_string (w, area, &x, &y, &charpos,
25836 &object, &dx, &dy, &width, &height);
25837 }
25838
25839 help = Qnil;
25840
25841 #ifdef HAVE_WINDOW_SYSTEM
25842 if (IMAGEP (object))
25843 {
25844 Lisp_Object image_map, hotspot;
25845 if ((image_map = Fplist_get (XCDR (object), QCmap),
25846 !NILP (image_map))
25847 && (hotspot = find_hot_spot (image_map, dx, dy),
25848 CONSP (hotspot))
25849 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
25850 {
25851 Lisp_Object plist;
25852
25853 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
25854 If so, we could look for mouse-enter, mouse-leave
25855 properties in PLIST (and do something...). */
25856 hotspot = XCDR (hotspot);
25857 if (CONSP (hotspot)
25858 && (plist = XCAR (hotspot), CONSP (plist)))
25859 {
25860 pointer = Fplist_get (plist, Qpointer);
25861 if (NILP (pointer))
25862 pointer = Qhand;
25863 help = Fplist_get (plist, Qhelp_echo);
25864 if (!NILP (help))
25865 {
25866 help_echo_string = help;
25867 /* Is this correct? ++kfs */
25868 XSETWINDOW (help_echo_window, w);
25869 help_echo_object = w->buffer;
25870 help_echo_pos = charpos;
25871 }
25872 }
25873 }
25874 if (NILP (pointer))
25875 pointer = Fplist_get (XCDR (object), QCpointer);
25876 }
25877 #endif /* HAVE_WINDOW_SYSTEM */
25878
25879 if (STRINGP (string))
25880 {
25881 pos = make_number (charpos);
25882 /* If we're on a string with `help-echo' text property, arrange
25883 for the help to be displayed. This is done by setting the
25884 global variable help_echo_string to the help string. */
25885 if (NILP (help))
25886 {
25887 help = Fget_text_property (pos, Qhelp_echo, string);
25888 if (!NILP (help))
25889 {
25890 help_echo_string = help;
25891 XSETWINDOW (help_echo_window, w);
25892 help_echo_object = string;
25893 help_echo_pos = charpos;
25894 }
25895 }
25896
25897 #ifdef HAVE_WINDOW_SYSTEM
25898 if (FRAME_WINDOW_P (f))
25899 {
25900 dpyinfo = FRAME_X_DISPLAY_INFO (f);
25901 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
25902 if (NILP (pointer))
25903 pointer = Fget_text_property (pos, Qpointer, string);
25904
25905 /* Change the mouse pointer according to what is under X/Y. */
25906 if (NILP (pointer)
25907 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
25908 {
25909 Lisp_Object map;
25910 map = Fget_text_property (pos, Qlocal_map, string);
25911 if (!KEYMAPP (map))
25912 map = Fget_text_property (pos, Qkeymap, string);
25913 if (!KEYMAPP (map))
25914 cursor = dpyinfo->vertical_scroll_bar_cursor;
25915 }
25916 }
25917 #endif
25918
25919 /* Change the mouse face according to what is under X/Y. */
25920 mouse_face = Fget_text_property (pos, Qmouse_face, string);
25921 if (!NILP (mouse_face)
25922 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
25923 && glyph)
25924 {
25925 Lisp_Object b, e;
25926
25927 struct glyph * tmp_glyph;
25928
25929 int gpos;
25930 int gseq_length;
25931 int total_pixel_width;
25932 EMACS_INT begpos, endpos, ignore;
25933
25934 int vpos, hpos;
25935
25936 b = Fprevious_single_property_change (make_number (charpos + 1),
25937 Qmouse_face, string, Qnil);
25938 if (NILP (b))
25939 begpos = 0;
25940 else
25941 begpos = XINT (b);
25942
25943 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
25944 if (NILP (e))
25945 endpos = SCHARS (string);
25946 else
25947 endpos = XINT (e);
25948
25949 /* Calculate the glyph position GPOS of GLYPH in the
25950 displayed string, relative to the beginning of the
25951 highlighted part of the string.
25952
25953 Note: GPOS is different from CHARPOS. CHARPOS is the
25954 position of GLYPH in the internal string object. A mode
25955 line string format has structures which are converted to
25956 a flattened string by the Emacs Lisp interpreter. The
25957 internal string is an element of those structures. The
25958 displayed string is the flattened string. */
25959 tmp_glyph = row_start_glyph;
25960 while (tmp_glyph < glyph
25961 && (!(EQ (tmp_glyph->object, glyph->object)
25962 && begpos <= tmp_glyph->charpos
25963 && tmp_glyph->charpos < endpos)))
25964 tmp_glyph++;
25965 gpos = glyph - tmp_glyph;
25966
25967 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
25968 the highlighted part of the displayed string to which
25969 GLYPH belongs. Note: GSEQ_LENGTH is different from
25970 SCHARS (STRING), because the latter returns the length of
25971 the internal string. */
25972 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
25973 tmp_glyph > glyph
25974 && (!(EQ (tmp_glyph->object, glyph->object)
25975 && begpos <= tmp_glyph->charpos
25976 && tmp_glyph->charpos < endpos));
25977 tmp_glyph--)
25978 ;
25979 gseq_length = gpos + (tmp_glyph - glyph) + 1;
25980
25981 /* Calculate the total pixel width of all the glyphs between
25982 the beginning of the highlighted area and GLYPH. */
25983 total_pixel_width = 0;
25984 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
25985 total_pixel_width += tmp_glyph->pixel_width;
25986
25987 /* Pre calculation of re-rendering position. Note: X is in
25988 column units here, after the call to mode_line_string or
25989 marginal_area_string. */
25990 hpos = x - gpos;
25991 vpos = (area == ON_MODE_LINE
25992 ? (w->current_matrix)->nrows - 1
25993 : 0);
25994
25995 /* If GLYPH's position is included in the region that is
25996 already drawn in mouse face, we have nothing to do. */
25997 if ( EQ (window, hlinfo->mouse_face_window)
25998 && (!row->reversed_p
25999 ? (hlinfo->mouse_face_beg_col <= hpos
26000 && hpos < hlinfo->mouse_face_end_col)
26001 /* In R2L rows we swap BEG and END, see below. */
26002 : (hlinfo->mouse_face_end_col <= hpos
26003 && hpos < hlinfo->mouse_face_beg_col))
26004 && hlinfo->mouse_face_beg_row == vpos )
26005 return;
26006
26007 if (clear_mouse_face (hlinfo))
26008 cursor = No_Cursor;
26009
26010 if (!row->reversed_p)
26011 {
26012 hlinfo->mouse_face_beg_col = hpos;
26013 hlinfo->mouse_face_beg_x = original_x_pixel
26014 - (total_pixel_width + dx);
26015 hlinfo->mouse_face_end_col = hpos + gseq_length;
26016 hlinfo->mouse_face_end_x = 0;
26017 }
26018 else
26019 {
26020 /* In R2L rows, show_mouse_face expects BEG and END
26021 coordinates to be swapped. */
26022 hlinfo->mouse_face_end_col = hpos;
26023 hlinfo->mouse_face_end_x = original_x_pixel
26024 - (total_pixel_width + dx);
26025 hlinfo->mouse_face_beg_col = hpos + gseq_length;
26026 hlinfo->mouse_face_beg_x = 0;
26027 }
26028
26029 hlinfo->mouse_face_beg_row = vpos;
26030 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
26031 hlinfo->mouse_face_beg_y = 0;
26032 hlinfo->mouse_face_end_y = 0;
26033 hlinfo->mouse_face_past_end = 0;
26034 hlinfo->mouse_face_window = window;
26035
26036 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
26037 charpos,
26038 0, 0, 0,
26039 &ignore,
26040 glyph->face_id,
26041 1);
26042 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26043
26044 if (NILP (pointer))
26045 pointer = Qhand;
26046 }
26047 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26048 clear_mouse_face (hlinfo);
26049 }
26050 #ifdef HAVE_WINDOW_SYSTEM
26051 if (FRAME_WINDOW_P (f))
26052 define_frame_cursor1 (f, cursor, pointer);
26053 #endif
26054 }
26055
26056
26057 /* EXPORT:
26058 Take proper action when the mouse has moved to position X, Y on
26059 frame F as regards highlighting characters that have mouse-face
26060 properties. Also de-highlighting chars where the mouse was before.
26061 X and Y can be negative or out of range. */
26062
26063 void
26064 note_mouse_highlight (struct frame *f, int x, int y)
26065 {
26066 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26067 enum window_part part;
26068 Lisp_Object window;
26069 struct window *w;
26070 Cursor cursor = No_Cursor;
26071 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
26072 struct buffer *b;
26073
26074 /* When a menu is active, don't highlight because this looks odd. */
26075 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
26076 if (popup_activated ())
26077 return;
26078 #endif
26079
26080 if (NILP (Vmouse_highlight)
26081 || !f->glyphs_initialized_p
26082 || f->pointer_invisible)
26083 return;
26084
26085 hlinfo->mouse_face_mouse_x = x;
26086 hlinfo->mouse_face_mouse_y = y;
26087 hlinfo->mouse_face_mouse_frame = f;
26088
26089 if (hlinfo->mouse_face_defer)
26090 return;
26091
26092 if (gc_in_progress)
26093 {
26094 hlinfo->mouse_face_deferred_gc = 1;
26095 return;
26096 }
26097
26098 /* Which window is that in? */
26099 window = window_from_coordinates (f, x, y, &part, 1);
26100
26101 /* If we were displaying active text in another window, clear that.
26102 Also clear if we move out of text area in same window. */
26103 if (! EQ (window, hlinfo->mouse_face_window)
26104 || (part != ON_TEXT && part != ON_MODE_LINE && part != ON_HEADER_LINE
26105 && !NILP (hlinfo->mouse_face_window)))
26106 clear_mouse_face (hlinfo);
26107
26108 /* Not on a window -> return. */
26109 if (!WINDOWP (window))
26110 return;
26111
26112 /* Reset help_echo_string. It will get recomputed below. */
26113 help_echo_string = Qnil;
26114
26115 /* Convert to window-relative pixel coordinates. */
26116 w = XWINDOW (window);
26117 frame_to_window_pixel_xy (w, &x, &y);
26118
26119 #ifdef HAVE_WINDOW_SYSTEM
26120 /* Handle tool-bar window differently since it doesn't display a
26121 buffer. */
26122 if (EQ (window, f->tool_bar_window))
26123 {
26124 note_tool_bar_highlight (f, x, y);
26125 return;
26126 }
26127 #endif
26128
26129 /* Mouse is on the mode, header line or margin? */
26130 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
26131 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
26132 {
26133 note_mode_line_or_margin_highlight (window, x, y, part);
26134 return;
26135 }
26136
26137 #ifdef HAVE_WINDOW_SYSTEM
26138 if (part == ON_VERTICAL_BORDER)
26139 {
26140 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26141 help_echo_string = build_string ("drag-mouse-1: resize");
26142 }
26143 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
26144 || part == ON_SCROLL_BAR)
26145 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26146 else
26147 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26148 #endif
26149
26150 /* Are we in a window whose display is up to date?
26151 And verify the buffer's text has not changed. */
26152 b = XBUFFER (w->buffer);
26153 if (part == ON_TEXT
26154 && EQ (w->window_end_valid, w->buffer)
26155 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
26156 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
26157 {
26158 int hpos, vpos, i, dx, dy, area;
26159 EMACS_INT pos;
26160 struct glyph *glyph;
26161 Lisp_Object object;
26162 Lisp_Object mouse_face = Qnil, position;
26163 Lisp_Object *overlay_vec = NULL;
26164 int noverlays;
26165 struct buffer *obuf;
26166 EMACS_INT obegv, ozv;
26167 int same_region;
26168
26169 /* Find the glyph under X/Y. */
26170 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
26171
26172 #ifdef HAVE_WINDOW_SYSTEM
26173 /* Look for :pointer property on image. */
26174 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26175 {
26176 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26177 if (img != NULL && IMAGEP (img->spec))
26178 {
26179 Lisp_Object image_map, hotspot;
26180 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
26181 !NILP (image_map))
26182 && (hotspot = find_hot_spot (image_map,
26183 glyph->slice.img.x + dx,
26184 glyph->slice.img.y + dy),
26185 CONSP (hotspot))
26186 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26187 {
26188 Lisp_Object plist;
26189
26190 /* Could check XCAR (hotspot) to see if we enter/leave
26191 this hot-spot.
26192 If so, we could look for mouse-enter, mouse-leave
26193 properties in PLIST (and do something...). */
26194 hotspot = XCDR (hotspot);
26195 if (CONSP (hotspot)
26196 && (plist = XCAR (hotspot), CONSP (plist)))
26197 {
26198 pointer = Fplist_get (plist, Qpointer);
26199 if (NILP (pointer))
26200 pointer = Qhand;
26201 help_echo_string = Fplist_get (plist, Qhelp_echo);
26202 if (!NILP (help_echo_string))
26203 {
26204 help_echo_window = window;
26205 help_echo_object = glyph->object;
26206 help_echo_pos = glyph->charpos;
26207 }
26208 }
26209 }
26210 if (NILP (pointer))
26211 pointer = Fplist_get (XCDR (img->spec), QCpointer);
26212 }
26213 }
26214 #endif /* HAVE_WINDOW_SYSTEM */
26215
26216 /* Clear mouse face if X/Y not over text. */
26217 if (glyph == NULL
26218 || area != TEXT_AREA
26219 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
26220 /* Glyph's OBJECT is an integer for glyphs inserted by the
26221 display engine for its internal purposes, like truncation
26222 and continuation glyphs and blanks beyond the end of
26223 line's text on text terminals. If we are over such a
26224 glyph, we are not over any text. */
26225 || INTEGERP (glyph->object)
26226 /* R2L rows have a stretch glyph at their front, which
26227 stands for no text, whereas L2R rows have no glyphs at
26228 all beyond the end of text. Treat such stretch glyphs
26229 like we do with NULL glyphs in L2R rows. */
26230 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
26231 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
26232 && glyph->type == STRETCH_GLYPH
26233 && glyph->avoid_cursor_p))
26234 {
26235 if (clear_mouse_face (hlinfo))
26236 cursor = No_Cursor;
26237 #ifdef HAVE_WINDOW_SYSTEM
26238 if (FRAME_WINDOW_P (f) && NILP (pointer))
26239 {
26240 if (area != TEXT_AREA)
26241 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26242 else
26243 pointer = Vvoid_text_area_pointer;
26244 }
26245 #endif
26246 goto set_cursor;
26247 }
26248
26249 pos = glyph->charpos;
26250 object = glyph->object;
26251 if (!STRINGP (object) && !BUFFERP (object))
26252 goto set_cursor;
26253
26254 /* If we get an out-of-range value, return now; avoid an error. */
26255 if (BUFFERP (object) && pos > BUF_Z (b))
26256 goto set_cursor;
26257
26258 /* Make the window's buffer temporarily current for
26259 overlays_at and compute_char_face. */
26260 obuf = current_buffer;
26261 current_buffer = b;
26262 obegv = BEGV;
26263 ozv = ZV;
26264 BEGV = BEG;
26265 ZV = Z;
26266
26267 /* Is this char mouse-active or does it have help-echo? */
26268 position = make_number (pos);
26269
26270 if (BUFFERP (object))
26271 {
26272 /* Put all the overlays we want in a vector in overlay_vec. */
26273 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
26274 /* Sort overlays into increasing priority order. */
26275 noverlays = sort_overlays (overlay_vec, noverlays, w);
26276 }
26277 else
26278 noverlays = 0;
26279
26280 same_region = coords_in_mouse_face_p (w, hpos, vpos);
26281
26282 if (same_region)
26283 cursor = No_Cursor;
26284
26285 /* Check mouse-face highlighting. */
26286 if (! same_region
26287 /* If there exists an overlay with mouse-face overlapping
26288 the one we are currently highlighting, we have to
26289 check if we enter the overlapping overlay, and then
26290 highlight only that. */
26291 || (OVERLAYP (hlinfo->mouse_face_overlay)
26292 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
26293 {
26294 /* Find the highest priority overlay with a mouse-face. */
26295 Lisp_Object overlay = Qnil;
26296 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
26297 {
26298 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
26299 if (!NILP (mouse_face))
26300 overlay = overlay_vec[i];
26301 }
26302
26303 /* If we're highlighting the same overlay as before, there's
26304 no need to do that again. */
26305 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
26306 goto check_help_echo;
26307 hlinfo->mouse_face_overlay = overlay;
26308
26309 /* Clear the display of the old active region, if any. */
26310 if (clear_mouse_face (hlinfo))
26311 cursor = No_Cursor;
26312
26313 /* If no overlay applies, get a text property. */
26314 if (NILP (overlay))
26315 mouse_face = Fget_text_property (position, Qmouse_face, object);
26316
26317 /* Next, compute the bounds of the mouse highlighting and
26318 display it. */
26319 if (!NILP (mouse_face) && STRINGP (object))
26320 {
26321 /* The mouse-highlighting comes from a display string
26322 with a mouse-face. */
26323 Lisp_Object s, e;
26324 EMACS_INT ignore;
26325
26326 s = Fprevious_single_property_change
26327 (make_number (pos + 1), Qmouse_face, object, Qnil);
26328 e = Fnext_single_property_change
26329 (position, Qmouse_face, object, Qnil);
26330 if (NILP (s))
26331 s = make_number (0);
26332 if (NILP (e))
26333 e = make_number (SCHARS (object) - 1);
26334 mouse_face_from_string_pos (w, hlinfo, object,
26335 XINT (s), XINT (e));
26336 hlinfo->mouse_face_past_end = 0;
26337 hlinfo->mouse_face_window = window;
26338 hlinfo->mouse_face_face_id
26339 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
26340 glyph->face_id, 1);
26341 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26342 cursor = No_Cursor;
26343 }
26344 else
26345 {
26346 /* The mouse-highlighting, if any, comes from an overlay
26347 or text property in the buffer. */
26348 Lisp_Object buffer IF_LINT (= Qnil);
26349 Lisp_Object cover_string IF_LINT (= Qnil);
26350
26351 if (STRINGP (object))
26352 {
26353 /* If we are on a display string with no mouse-face,
26354 check if the text under it has one. */
26355 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
26356 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26357 pos = string_buffer_position (object, start);
26358 if (pos > 0)
26359 {
26360 mouse_face = get_char_property_and_overlay
26361 (make_number (pos), Qmouse_face, w->buffer, &overlay);
26362 buffer = w->buffer;
26363 cover_string = object;
26364 }
26365 }
26366 else
26367 {
26368 buffer = object;
26369 cover_string = Qnil;
26370 }
26371
26372 if (!NILP (mouse_face))
26373 {
26374 Lisp_Object before, after;
26375 Lisp_Object before_string, after_string;
26376 /* To correctly find the limits of mouse highlight
26377 in a bidi-reordered buffer, we must not use the
26378 optimization of limiting the search in
26379 previous-single-property-change and
26380 next-single-property-change, because
26381 rows_from_pos_range needs the real start and end
26382 positions to DTRT in this case. That's because
26383 the first row visible in a window does not
26384 necessarily display the character whose position
26385 is the smallest. */
26386 Lisp_Object lim1 =
26387 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26388 ? Fmarker_position (w->start)
26389 : Qnil;
26390 Lisp_Object lim2 =
26391 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
26392 ? make_number (BUF_Z (XBUFFER (buffer))
26393 - XFASTINT (w->window_end_pos))
26394 : Qnil;
26395
26396 if (NILP (overlay))
26397 {
26398 /* Handle the text property case. */
26399 before = Fprevious_single_property_change
26400 (make_number (pos + 1), Qmouse_face, buffer, lim1);
26401 after = Fnext_single_property_change
26402 (make_number (pos), Qmouse_face, buffer, lim2);
26403 before_string = after_string = Qnil;
26404 }
26405 else
26406 {
26407 /* Handle the overlay case. */
26408 before = Foverlay_start (overlay);
26409 after = Foverlay_end (overlay);
26410 before_string = Foverlay_get (overlay, Qbefore_string);
26411 after_string = Foverlay_get (overlay, Qafter_string);
26412
26413 if (!STRINGP (before_string)) before_string = Qnil;
26414 if (!STRINGP (after_string)) after_string = Qnil;
26415 }
26416
26417 mouse_face_from_buffer_pos (window, hlinfo, pos,
26418 XFASTINT (before),
26419 XFASTINT (after),
26420 before_string, after_string,
26421 cover_string);
26422 cursor = No_Cursor;
26423 }
26424 }
26425 }
26426
26427 check_help_echo:
26428
26429 /* Look for a `help-echo' property. */
26430 if (NILP (help_echo_string)) {
26431 Lisp_Object help, overlay;
26432
26433 /* Check overlays first. */
26434 help = overlay = Qnil;
26435 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
26436 {
26437 overlay = overlay_vec[i];
26438 help = Foverlay_get (overlay, Qhelp_echo);
26439 }
26440
26441 if (!NILP (help))
26442 {
26443 help_echo_string = help;
26444 help_echo_window = window;
26445 help_echo_object = overlay;
26446 help_echo_pos = pos;
26447 }
26448 else
26449 {
26450 Lisp_Object obj = glyph->object;
26451 EMACS_INT charpos = glyph->charpos;
26452
26453 /* Try text properties. */
26454 if (STRINGP (obj)
26455 && charpos >= 0
26456 && charpos < SCHARS (obj))
26457 {
26458 help = Fget_text_property (make_number (charpos),
26459 Qhelp_echo, obj);
26460 if (NILP (help))
26461 {
26462 /* If the string itself doesn't specify a help-echo,
26463 see if the buffer text ``under'' it does. */
26464 struct glyph_row *r
26465 = MATRIX_ROW (w->current_matrix, vpos);
26466 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26467 EMACS_INT p = string_buffer_position (obj, start);
26468 if (p > 0)
26469 {
26470 help = Fget_char_property (make_number (p),
26471 Qhelp_echo, w->buffer);
26472 if (!NILP (help))
26473 {
26474 charpos = p;
26475 obj = w->buffer;
26476 }
26477 }
26478 }
26479 }
26480 else if (BUFFERP (obj)
26481 && charpos >= BEGV
26482 && charpos < ZV)
26483 help = Fget_text_property (make_number (charpos), Qhelp_echo,
26484 obj);
26485
26486 if (!NILP (help))
26487 {
26488 help_echo_string = help;
26489 help_echo_window = window;
26490 help_echo_object = obj;
26491 help_echo_pos = charpos;
26492 }
26493 }
26494 }
26495
26496 #ifdef HAVE_WINDOW_SYSTEM
26497 /* Look for a `pointer' property. */
26498 if (FRAME_WINDOW_P (f) && NILP (pointer))
26499 {
26500 /* Check overlays first. */
26501 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
26502 pointer = Foverlay_get (overlay_vec[i], Qpointer);
26503
26504 if (NILP (pointer))
26505 {
26506 Lisp_Object obj = glyph->object;
26507 EMACS_INT charpos = glyph->charpos;
26508
26509 /* Try text properties. */
26510 if (STRINGP (obj)
26511 && charpos >= 0
26512 && charpos < SCHARS (obj))
26513 {
26514 pointer = Fget_text_property (make_number (charpos),
26515 Qpointer, obj);
26516 if (NILP (pointer))
26517 {
26518 /* If the string itself doesn't specify a pointer,
26519 see if the buffer text ``under'' it does. */
26520 struct glyph_row *r
26521 = MATRIX_ROW (w->current_matrix, vpos);
26522 EMACS_INT start = MATRIX_ROW_START_CHARPOS (r);
26523 EMACS_INT p = string_buffer_position (obj, start);
26524 if (p > 0)
26525 pointer = Fget_char_property (make_number (p),
26526 Qpointer, w->buffer);
26527 }
26528 }
26529 else if (BUFFERP (obj)
26530 && charpos >= BEGV
26531 && charpos < ZV)
26532 pointer = Fget_text_property (make_number (charpos),
26533 Qpointer, obj);
26534 }
26535 }
26536 #endif /* HAVE_WINDOW_SYSTEM */
26537
26538 BEGV = obegv;
26539 ZV = ozv;
26540 current_buffer = obuf;
26541 }
26542
26543 set_cursor:
26544
26545 #ifdef HAVE_WINDOW_SYSTEM
26546 if (FRAME_WINDOW_P (f))
26547 define_frame_cursor1 (f, cursor, pointer);
26548 #else
26549 /* This is here to prevent a compiler error, about "label at end of
26550 compound statement". */
26551 return;
26552 #endif
26553 }
26554
26555
26556 /* EXPORT for RIF:
26557 Clear any mouse-face on window W. This function is part of the
26558 redisplay interface, and is called from try_window_id and similar
26559 functions to ensure the mouse-highlight is off. */
26560
26561 void
26562 x_clear_window_mouse_face (struct window *w)
26563 {
26564 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26565 Lisp_Object window;
26566
26567 BLOCK_INPUT;
26568 XSETWINDOW (window, w);
26569 if (EQ (window, hlinfo->mouse_face_window))
26570 clear_mouse_face (hlinfo);
26571 UNBLOCK_INPUT;
26572 }
26573
26574
26575 /* EXPORT:
26576 Just discard the mouse face information for frame F, if any.
26577 This is used when the size of F is changed. */
26578
26579 void
26580 cancel_mouse_face (struct frame *f)
26581 {
26582 Lisp_Object window;
26583 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26584
26585 window = hlinfo->mouse_face_window;
26586 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
26587 {
26588 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26589 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26590 hlinfo->mouse_face_window = Qnil;
26591 }
26592 }
26593
26594
26595 \f
26596 /***********************************************************************
26597 Exposure Events
26598 ***********************************************************************/
26599
26600 #ifdef HAVE_WINDOW_SYSTEM
26601
26602 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
26603 which intersects rectangle R. R is in window-relative coordinates. */
26604
26605 static void
26606 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
26607 enum glyph_row_area area)
26608 {
26609 struct glyph *first = row->glyphs[area];
26610 struct glyph *end = row->glyphs[area] + row->used[area];
26611 struct glyph *last;
26612 int first_x, start_x, x;
26613
26614 if (area == TEXT_AREA && row->fill_line_p)
26615 /* If row extends face to end of line write the whole line. */
26616 draw_glyphs (w, 0, row, area,
26617 0, row->used[area],
26618 DRAW_NORMAL_TEXT, 0);
26619 else
26620 {
26621 /* Set START_X to the window-relative start position for drawing glyphs of
26622 AREA. The first glyph of the text area can be partially visible.
26623 The first glyphs of other areas cannot. */
26624 start_x = window_box_left_offset (w, area);
26625 x = start_x;
26626 if (area == TEXT_AREA)
26627 x += row->x;
26628
26629 /* Find the first glyph that must be redrawn. */
26630 while (first < end
26631 && x + first->pixel_width < r->x)
26632 {
26633 x += first->pixel_width;
26634 ++first;
26635 }
26636
26637 /* Find the last one. */
26638 last = first;
26639 first_x = x;
26640 while (last < end
26641 && x < r->x + r->width)
26642 {
26643 x += last->pixel_width;
26644 ++last;
26645 }
26646
26647 /* Repaint. */
26648 if (last > first)
26649 draw_glyphs (w, first_x - start_x, row, area,
26650 first - row->glyphs[area], last - row->glyphs[area],
26651 DRAW_NORMAL_TEXT, 0);
26652 }
26653 }
26654
26655
26656 /* Redraw the parts of the glyph row ROW on window W intersecting
26657 rectangle R. R is in window-relative coordinates. Value is
26658 non-zero if mouse-face was overwritten. */
26659
26660 static int
26661 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
26662 {
26663 xassert (row->enabled_p);
26664
26665 if (row->mode_line_p || w->pseudo_window_p)
26666 draw_glyphs (w, 0, row, TEXT_AREA,
26667 0, row->used[TEXT_AREA],
26668 DRAW_NORMAL_TEXT, 0);
26669 else
26670 {
26671 if (row->used[LEFT_MARGIN_AREA])
26672 expose_area (w, row, r, LEFT_MARGIN_AREA);
26673 if (row->used[TEXT_AREA])
26674 expose_area (w, row, r, TEXT_AREA);
26675 if (row->used[RIGHT_MARGIN_AREA])
26676 expose_area (w, row, r, RIGHT_MARGIN_AREA);
26677 draw_row_fringe_bitmaps (w, row);
26678 }
26679
26680 return row->mouse_face_p;
26681 }
26682
26683
26684 /* Redraw those parts of glyphs rows during expose event handling that
26685 overlap other rows. Redrawing of an exposed line writes over parts
26686 of lines overlapping that exposed line; this function fixes that.
26687
26688 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
26689 row in W's current matrix that is exposed and overlaps other rows.
26690 LAST_OVERLAPPING_ROW is the last such row. */
26691
26692 static void
26693 expose_overlaps (struct window *w,
26694 struct glyph_row *first_overlapping_row,
26695 struct glyph_row *last_overlapping_row,
26696 XRectangle *r)
26697 {
26698 struct glyph_row *row;
26699
26700 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
26701 if (row->overlapping_p)
26702 {
26703 xassert (row->enabled_p && !row->mode_line_p);
26704
26705 row->clip = r;
26706 if (row->used[LEFT_MARGIN_AREA])
26707 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
26708
26709 if (row->used[TEXT_AREA])
26710 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
26711
26712 if (row->used[RIGHT_MARGIN_AREA])
26713 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
26714 row->clip = NULL;
26715 }
26716 }
26717
26718
26719 /* Return non-zero if W's cursor intersects rectangle R. */
26720
26721 static int
26722 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
26723 {
26724 XRectangle cr, result;
26725 struct glyph *cursor_glyph;
26726 struct glyph_row *row;
26727
26728 if (w->phys_cursor.vpos >= 0
26729 && w->phys_cursor.vpos < w->current_matrix->nrows
26730 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
26731 row->enabled_p)
26732 && row->cursor_in_fringe_p)
26733 {
26734 /* Cursor is in the fringe. */
26735 cr.x = window_box_right_offset (w,
26736 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
26737 ? RIGHT_MARGIN_AREA
26738 : TEXT_AREA));
26739 cr.y = row->y;
26740 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
26741 cr.height = row->height;
26742 return x_intersect_rectangles (&cr, r, &result);
26743 }
26744
26745 cursor_glyph = get_phys_cursor_glyph (w);
26746 if (cursor_glyph)
26747 {
26748 /* r is relative to W's box, but w->phys_cursor.x is relative
26749 to left edge of W's TEXT area. Adjust it. */
26750 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
26751 cr.y = w->phys_cursor.y;
26752 cr.width = cursor_glyph->pixel_width;
26753 cr.height = w->phys_cursor_height;
26754 /* ++KFS: W32 version used W32-specific IntersectRect here, but
26755 I assume the effect is the same -- and this is portable. */
26756 return x_intersect_rectangles (&cr, r, &result);
26757 }
26758 /* If we don't understand the format, pretend we're not in the hot-spot. */
26759 return 0;
26760 }
26761
26762
26763 /* EXPORT:
26764 Draw a vertical window border to the right of window W if W doesn't
26765 have vertical scroll bars. */
26766
26767 void
26768 x_draw_vertical_border (struct window *w)
26769 {
26770 struct frame *f = XFRAME (WINDOW_FRAME (w));
26771
26772 /* We could do better, if we knew what type of scroll-bar the adjacent
26773 windows (on either side) have... But we don't :-(
26774 However, I think this works ok. ++KFS 2003-04-25 */
26775
26776 /* Redraw borders between horizontally adjacent windows. Don't
26777 do it for frames with vertical scroll bars because either the
26778 right scroll bar of a window, or the left scroll bar of its
26779 neighbor will suffice as a border. */
26780 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
26781 return;
26782
26783 if (!WINDOW_RIGHTMOST_P (w)
26784 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
26785 {
26786 int x0, x1, y0, y1;
26787
26788 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26789 y1 -= 1;
26790
26791 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26792 x1 -= 1;
26793
26794 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
26795 }
26796 else if (!WINDOW_LEFTMOST_P (w)
26797 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
26798 {
26799 int x0, x1, y0, y1;
26800
26801 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
26802 y1 -= 1;
26803
26804 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
26805 x0 -= 1;
26806
26807 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
26808 }
26809 }
26810
26811
26812 /* Redraw the part of window W intersection rectangle FR. Pixel
26813 coordinates in FR are frame-relative. Call this function with
26814 input blocked. Value is non-zero if the exposure overwrites
26815 mouse-face. */
26816
26817 static int
26818 expose_window (struct window *w, XRectangle *fr)
26819 {
26820 struct frame *f = XFRAME (w->frame);
26821 XRectangle wr, r;
26822 int mouse_face_overwritten_p = 0;
26823
26824 /* If window is not yet fully initialized, do nothing. This can
26825 happen when toolkit scroll bars are used and a window is split.
26826 Reconfiguring the scroll bar will generate an expose for a newly
26827 created window. */
26828 if (w->current_matrix == NULL)
26829 return 0;
26830
26831 /* When we're currently updating the window, display and current
26832 matrix usually don't agree. Arrange for a thorough display
26833 later. */
26834 if (w == updated_window)
26835 {
26836 SET_FRAME_GARBAGED (f);
26837 return 0;
26838 }
26839
26840 /* Frame-relative pixel rectangle of W. */
26841 wr.x = WINDOW_LEFT_EDGE_X (w);
26842 wr.y = WINDOW_TOP_EDGE_Y (w);
26843 wr.width = WINDOW_TOTAL_WIDTH (w);
26844 wr.height = WINDOW_TOTAL_HEIGHT (w);
26845
26846 if (x_intersect_rectangles (fr, &wr, &r))
26847 {
26848 int yb = window_text_bottom_y (w);
26849 struct glyph_row *row;
26850 int cursor_cleared_p;
26851 struct glyph_row *first_overlapping_row, *last_overlapping_row;
26852
26853 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
26854 r.x, r.y, r.width, r.height));
26855
26856 /* Convert to window coordinates. */
26857 r.x -= WINDOW_LEFT_EDGE_X (w);
26858 r.y -= WINDOW_TOP_EDGE_Y (w);
26859
26860 /* Turn off the cursor. */
26861 if (!w->pseudo_window_p
26862 && phys_cursor_in_rect_p (w, &r))
26863 {
26864 x_clear_cursor (w);
26865 cursor_cleared_p = 1;
26866 }
26867 else
26868 cursor_cleared_p = 0;
26869
26870 /* Update lines intersecting rectangle R. */
26871 first_overlapping_row = last_overlapping_row = NULL;
26872 for (row = w->current_matrix->rows;
26873 row->enabled_p;
26874 ++row)
26875 {
26876 int y0 = row->y;
26877 int y1 = MATRIX_ROW_BOTTOM_Y (row);
26878
26879 if ((y0 >= r.y && y0 < r.y + r.height)
26880 || (y1 > r.y && y1 < r.y + r.height)
26881 || (r.y >= y0 && r.y < y1)
26882 || (r.y + r.height > y0 && r.y + r.height < y1))
26883 {
26884 /* A header line may be overlapping, but there is no need
26885 to fix overlapping areas for them. KFS 2005-02-12 */
26886 if (row->overlapping_p && !row->mode_line_p)
26887 {
26888 if (first_overlapping_row == NULL)
26889 first_overlapping_row = row;
26890 last_overlapping_row = row;
26891 }
26892
26893 row->clip = fr;
26894 if (expose_line (w, row, &r))
26895 mouse_face_overwritten_p = 1;
26896 row->clip = NULL;
26897 }
26898 else if (row->overlapping_p)
26899 {
26900 /* We must redraw a row overlapping the exposed area. */
26901 if (y0 < r.y
26902 ? y0 + row->phys_height > r.y
26903 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
26904 {
26905 if (first_overlapping_row == NULL)
26906 first_overlapping_row = row;
26907 last_overlapping_row = row;
26908 }
26909 }
26910
26911 if (y1 >= yb)
26912 break;
26913 }
26914
26915 /* Display the mode line if there is one. */
26916 if (WINDOW_WANTS_MODELINE_P (w)
26917 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
26918 row->enabled_p)
26919 && row->y < r.y + r.height)
26920 {
26921 if (expose_line (w, row, &r))
26922 mouse_face_overwritten_p = 1;
26923 }
26924
26925 if (!w->pseudo_window_p)
26926 {
26927 /* Fix the display of overlapping rows. */
26928 if (first_overlapping_row)
26929 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
26930 fr);
26931
26932 /* Draw border between windows. */
26933 x_draw_vertical_border (w);
26934
26935 /* Turn the cursor on again. */
26936 if (cursor_cleared_p)
26937 update_window_cursor (w, 1);
26938 }
26939 }
26940
26941 return mouse_face_overwritten_p;
26942 }
26943
26944
26945
26946 /* Redraw (parts) of all windows in the window tree rooted at W that
26947 intersect R. R contains frame pixel coordinates. Value is
26948 non-zero if the exposure overwrites mouse-face. */
26949
26950 static int
26951 expose_window_tree (struct window *w, XRectangle *r)
26952 {
26953 struct frame *f = XFRAME (w->frame);
26954 int mouse_face_overwritten_p = 0;
26955
26956 while (w && !FRAME_GARBAGED_P (f))
26957 {
26958 if (!NILP (w->hchild))
26959 mouse_face_overwritten_p
26960 |= expose_window_tree (XWINDOW (w->hchild), r);
26961 else if (!NILP (w->vchild))
26962 mouse_face_overwritten_p
26963 |= expose_window_tree (XWINDOW (w->vchild), r);
26964 else
26965 mouse_face_overwritten_p |= expose_window (w, r);
26966
26967 w = NILP (w->next) ? NULL : XWINDOW (w->next);
26968 }
26969
26970 return mouse_face_overwritten_p;
26971 }
26972
26973
26974 /* EXPORT:
26975 Redisplay an exposed area of frame F. X and Y are the upper-left
26976 corner of the exposed rectangle. W and H are width and height of
26977 the exposed area. All are pixel values. W or H zero means redraw
26978 the entire frame. */
26979
26980 void
26981 expose_frame (struct frame *f, int x, int y, int w, int h)
26982 {
26983 XRectangle r;
26984 int mouse_face_overwritten_p = 0;
26985
26986 TRACE ((stderr, "expose_frame "));
26987
26988 /* No need to redraw if frame will be redrawn soon. */
26989 if (FRAME_GARBAGED_P (f))
26990 {
26991 TRACE ((stderr, " garbaged\n"));
26992 return;
26993 }
26994
26995 /* If basic faces haven't been realized yet, there is no point in
26996 trying to redraw anything. This can happen when we get an expose
26997 event while Emacs is starting, e.g. by moving another window. */
26998 if (FRAME_FACE_CACHE (f) == NULL
26999 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
27000 {
27001 TRACE ((stderr, " no faces\n"));
27002 return;
27003 }
27004
27005 if (w == 0 || h == 0)
27006 {
27007 r.x = r.y = 0;
27008 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
27009 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
27010 }
27011 else
27012 {
27013 r.x = x;
27014 r.y = y;
27015 r.width = w;
27016 r.height = h;
27017 }
27018
27019 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
27020 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
27021
27022 if (WINDOWP (f->tool_bar_window))
27023 mouse_face_overwritten_p
27024 |= expose_window (XWINDOW (f->tool_bar_window), &r);
27025
27026 #ifdef HAVE_X_WINDOWS
27027 #ifndef MSDOS
27028 #ifndef USE_X_TOOLKIT
27029 if (WINDOWP (f->menu_bar_window))
27030 mouse_face_overwritten_p
27031 |= expose_window (XWINDOW (f->menu_bar_window), &r);
27032 #endif /* not USE_X_TOOLKIT */
27033 #endif
27034 #endif
27035
27036 /* Some window managers support a focus-follows-mouse style with
27037 delayed raising of frames. Imagine a partially obscured frame,
27038 and moving the mouse into partially obscured mouse-face on that
27039 frame. The visible part of the mouse-face will be highlighted,
27040 then the WM raises the obscured frame. With at least one WM, KDE
27041 2.1, Emacs is not getting any event for the raising of the frame
27042 (even tried with SubstructureRedirectMask), only Expose events.
27043 These expose events will draw text normally, i.e. not
27044 highlighted. Which means we must redo the highlight here.
27045 Subsume it under ``we love X''. --gerd 2001-08-15 */
27046 /* Included in Windows version because Windows most likely does not
27047 do the right thing if any third party tool offers
27048 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
27049 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
27050 {
27051 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27052 if (f == hlinfo->mouse_face_mouse_frame)
27053 {
27054 int mouse_x = hlinfo->mouse_face_mouse_x;
27055 int mouse_y = hlinfo->mouse_face_mouse_y;
27056 clear_mouse_face (hlinfo);
27057 note_mouse_highlight (f, mouse_x, mouse_y);
27058 }
27059 }
27060 }
27061
27062
27063 /* EXPORT:
27064 Determine the intersection of two rectangles R1 and R2. Return
27065 the intersection in *RESULT. Value is non-zero if RESULT is not
27066 empty. */
27067
27068 int
27069 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
27070 {
27071 XRectangle *left, *right;
27072 XRectangle *upper, *lower;
27073 int intersection_p = 0;
27074
27075 /* Rearrange so that R1 is the left-most rectangle. */
27076 if (r1->x < r2->x)
27077 left = r1, right = r2;
27078 else
27079 left = r2, right = r1;
27080
27081 /* X0 of the intersection is right.x0, if this is inside R1,
27082 otherwise there is no intersection. */
27083 if (right->x <= left->x + left->width)
27084 {
27085 result->x = right->x;
27086
27087 /* The right end of the intersection is the minimum of the
27088 the right ends of left and right. */
27089 result->width = (min (left->x + left->width, right->x + right->width)
27090 - result->x);
27091
27092 /* Same game for Y. */
27093 if (r1->y < r2->y)
27094 upper = r1, lower = r2;
27095 else
27096 upper = r2, lower = r1;
27097
27098 /* The upper end of the intersection is lower.y0, if this is inside
27099 of upper. Otherwise, there is no intersection. */
27100 if (lower->y <= upper->y + upper->height)
27101 {
27102 result->y = lower->y;
27103
27104 /* The lower end of the intersection is the minimum of the lower
27105 ends of upper and lower. */
27106 result->height = (min (lower->y + lower->height,
27107 upper->y + upper->height)
27108 - result->y);
27109 intersection_p = 1;
27110 }
27111 }
27112
27113 return intersection_p;
27114 }
27115
27116 #endif /* HAVE_WINDOW_SYSTEM */
27117
27118 \f
27119 /***********************************************************************
27120 Initialization
27121 ***********************************************************************/
27122
27123 void
27124 syms_of_xdisp (void)
27125 {
27126 Vwith_echo_area_save_vector = Qnil;
27127 staticpro (&Vwith_echo_area_save_vector);
27128
27129 Vmessage_stack = Qnil;
27130 staticpro (&Vmessage_stack);
27131
27132 Qinhibit_redisplay = intern_c_string ("inhibit-redisplay");
27133 staticpro (&Qinhibit_redisplay);
27134
27135 message_dolog_marker1 = Fmake_marker ();
27136 staticpro (&message_dolog_marker1);
27137 message_dolog_marker2 = Fmake_marker ();
27138 staticpro (&message_dolog_marker2);
27139 message_dolog_marker3 = Fmake_marker ();
27140 staticpro (&message_dolog_marker3);
27141
27142 #if GLYPH_DEBUG
27143 defsubr (&Sdump_frame_glyph_matrix);
27144 defsubr (&Sdump_glyph_matrix);
27145 defsubr (&Sdump_glyph_row);
27146 defsubr (&Sdump_tool_bar_row);
27147 defsubr (&Strace_redisplay);
27148 defsubr (&Strace_to_stderr);
27149 #endif
27150 #ifdef HAVE_WINDOW_SYSTEM
27151 defsubr (&Stool_bar_lines_needed);
27152 defsubr (&Slookup_image_map);
27153 #endif
27154 defsubr (&Sformat_mode_line);
27155 defsubr (&Sinvisible_p);
27156 defsubr (&Scurrent_bidi_paragraph_direction);
27157
27158 staticpro (&Qmenu_bar_update_hook);
27159 Qmenu_bar_update_hook = intern_c_string ("menu-bar-update-hook");
27160
27161 staticpro (&Qoverriding_terminal_local_map);
27162 Qoverriding_terminal_local_map = intern_c_string ("overriding-terminal-local-map");
27163
27164 staticpro (&Qoverriding_local_map);
27165 Qoverriding_local_map = intern_c_string ("overriding-local-map");
27166
27167 staticpro (&Qwindow_scroll_functions);
27168 Qwindow_scroll_functions = intern_c_string ("window-scroll-functions");
27169
27170 staticpro (&Qwindow_text_change_functions);
27171 Qwindow_text_change_functions = intern_c_string ("window-text-change-functions");
27172
27173 staticpro (&Qredisplay_end_trigger_functions);
27174 Qredisplay_end_trigger_functions = intern_c_string ("redisplay-end-trigger-functions");
27175
27176 staticpro (&Qinhibit_point_motion_hooks);
27177 Qinhibit_point_motion_hooks = intern_c_string ("inhibit-point-motion-hooks");
27178
27179 Qeval = intern_c_string ("eval");
27180 staticpro (&Qeval);
27181
27182 QCdata = intern_c_string (":data");
27183 staticpro (&QCdata);
27184 Qdisplay = intern_c_string ("display");
27185 staticpro (&Qdisplay);
27186 Qspace_width = intern_c_string ("space-width");
27187 staticpro (&Qspace_width);
27188 Qraise = intern_c_string ("raise");
27189 staticpro (&Qraise);
27190 Qslice = intern_c_string ("slice");
27191 staticpro (&Qslice);
27192 Qspace = intern_c_string ("space");
27193 staticpro (&Qspace);
27194 Qmargin = intern_c_string ("margin");
27195 staticpro (&Qmargin);
27196 Qpointer = intern_c_string ("pointer");
27197 staticpro (&Qpointer);
27198 Qleft_margin = intern_c_string ("left-margin");
27199 staticpro (&Qleft_margin);
27200 Qright_margin = intern_c_string ("right-margin");
27201 staticpro (&Qright_margin);
27202 Qcenter = intern_c_string ("center");
27203 staticpro (&Qcenter);
27204 Qline_height = intern_c_string ("line-height");
27205 staticpro (&Qline_height);
27206 QCalign_to = intern_c_string (":align-to");
27207 staticpro (&QCalign_to);
27208 QCrelative_width = intern_c_string (":relative-width");
27209 staticpro (&QCrelative_width);
27210 QCrelative_height = intern_c_string (":relative-height");
27211 staticpro (&QCrelative_height);
27212 QCeval = intern_c_string (":eval");
27213 staticpro (&QCeval);
27214 QCpropertize = intern_c_string (":propertize");
27215 staticpro (&QCpropertize);
27216 QCfile = intern_c_string (":file");
27217 staticpro (&QCfile);
27218 Qfontified = intern_c_string ("fontified");
27219 staticpro (&Qfontified);
27220 Qfontification_functions = intern_c_string ("fontification-functions");
27221 staticpro (&Qfontification_functions);
27222 Qtrailing_whitespace = intern_c_string ("trailing-whitespace");
27223 staticpro (&Qtrailing_whitespace);
27224 Qescape_glyph = intern_c_string ("escape-glyph");
27225 staticpro (&Qescape_glyph);
27226 Qnobreak_space = intern_c_string ("nobreak-space");
27227 staticpro (&Qnobreak_space);
27228 Qimage = intern_c_string ("image");
27229 staticpro (&Qimage);
27230 Qtext = intern_c_string ("text");
27231 staticpro (&Qtext);
27232 Qboth = intern_c_string ("both");
27233 staticpro (&Qboth);
27234 Qboth_horiz = intern_c_string ("both-horiz");
27235 staticpro (&Qboth_horiz);
27236 Qtext_image_horiz = intern_c_string ("text-image-horiz");
27237 staticpro (&Qtext_image_horiz);
27238 QCmap = intern_c_string (":map");
27239 staticpro (&QCmap);
27240 QCpointer = intern_c_string (":pointer");
27241 staticpro (&QCpointer);
27242 Qrect = intern_c_string ("rect");
27243 staticpro (&Qrect);
27244 Qcircle = intern_c_string ("circle");
27245 staticpro (&Qcircle);
27246 Qpoly = intern_c_string ("poly");
27247 staticpro (&Qpoly);
27248 Qmessage_truncate_lines = intern_c_string ("message-truncate-lines");
27249 staticpro (&Qmessage_truncate_lines);
27250 Qgrow_only = intern_c_string ("grow-only");
27251 staticpro (&Qgrow_only);
27252 Qinhibit_menubar_update = intern_c_string ("inhibit-menubar-update");
27253 staticpro (&Qinhibit_menubar_update);
27254 Qinhibit_eval_during_redisplay = intern_c_string ("inhibit-eval-during-redisplay");
27255 staticpro (&Qinhibit_eval_during_redisplay);
27256 Qposition = intern_c_string ("position");
27257 staticpro (&Qposition);
27258 Qbuffer_position = intern_c_string ("buffer-position");
27259 staticpro (&Qbuffer_position);
27260 Qobject = intern_c_string ("object");
27261 staticpro (&Qobject);
27262 Qbar = intern_c_string ("bar");
27263 staticpro (&Qbar);
27264 Qhbar = intern_c_string ("hbar");
27265 staticpro (&Qhbar);
27266 Qbox = intern_c_string ("box");
27267 staticpro (&Qbox);
27268 Qhollow = intern_c_string ("hollow");
27269 staticpro (&Qhollow);
27270 Qhand = intern_c_string ("hand");
27271 staticpro (&Qhand);
27272 Qarrow = intern_c_string ("arrow");
27273 staticpro (&Qarrow);
27274 Qtext = intern_c_string ("text");
27275 staticpro (&Qtext);
27276 Qinhibit_free_realized_faces = intern_c_string ("inhibit-free-realized-faces");
27277 staticpro (&Qinhibit_free_realized_faces);
27278
27279 list_of_error = Fcons (Fcons (intern_c_string ("error"),
27280 Fcons (intern_c_string ("void-variable"), Qnil)),
27281 Qnil);
27282 staticpro (&list_of_error);
27283
27284 Qlast_arrow_position = intern_c_string ("last-arrow-position");
27285 staticpro (&Qlast_arrow_position);
27286 Qlast_arrow_string = intern_c_string ("last-arrow-string");
27287 staticpro (&Qlast_arrow_string);
27288
27289 Qoverlay_arrow_string = intern_c_string ("overlay-arrow-string");
27290 staticpro (&Qoverlay_arrow_string);
27291 Qoverlay_arrow_bitmap = intern_c_string ("overlay-arrow-bitmap");
27292 staticpro (&Qoverlay_arrow_bitmap);
27293
27294 echo_buffer[0] = echo_buffer[1] = Qnil;
27295 staticpro (&echo_buffer[0]);
27296 staticpro (&echo_buffer[1]);
27297
27298 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
27299 staticpro (&echo_area_buffer[0]);
27300 staticpro (&echo_area_buffer[1]);
27301
27302 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
27303 staticpro (&Vmessages_buffer_name);
27304
27305 mode_line_proptrans_alist = Qnil;
27306 staticpro (&mode_line_proptrans_alist);
27307 mode_line_string_list = Qnil;
27308 staticpro (&mode_line_string_list);
27309 mode_line_string_face = Qnil;
27310 staticpro (&mode_line_string_face);
27311 mode_line_string_face_prop = Qnil;
27312 staticpro (&mode_line_string_face_prop);
27313 Vmode_line_unwind_vector = Qnil;
27314 staticpro (&Vmode_line_unwind_vector);
27315
27316 help_echo_string = Qnil;
27317 staticpro (&help_echo_string);
27318 help_echo_object = Qnil;
27319 staticpro (&help_echo_object);
27320 help_echo_window = Qnil;
27321 staticpro (&help_echo_window);
27322 previous_help_echo_string = Qnil;
27323 staticpro (&previous_help_echo_string);
27324 help_echo_pos = -1;
27325
27326 Qright_to_left = intern_c_string ("right-to-left");
27327 staticpro (&Qright_to_left);
27328 Qleft_to_right = intern_c_string ("left-to-right");
27329 staticpro (&Qleft_to_right);
27330
27331 #ifdef HAVE_WINDOW_SYSTEM
27332 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
27333 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
27334 For example, if a block cursor is over a tab, it will be drawn as
27335 wide as that tab on the display. */);
27336 x_stretch_cursor_p = 0;
27337 #endif
27338
27339 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
27340 doc: /* *Non-nil means highlight trailing whitespace.
27341 The face used for trailing whitespace is `trailing-whitespace'. */);
27342 Vshow_trailing_whitespace = Qnil;
27343
27344 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
27345 doc: /* *Control highlighting of nobreak space and soft hyphen.
27346 A value of t means highlight the character itself (for nobreak space,
27347 use face `nobreak-space').
27348 A value of nil means no highlighting.
27349 Other values mean display the escape glyph followed by an ordinary
27350 space or ordinary hyphen. */);
27351 Vnobreak_char_display = Qt;
27352
27353 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
27354 doc: /* *The pointer shape to show in void text areas.
27355 A value of nil means to show the text pointer. Other options are `arrow',
27356 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
27357 Vvoid_text_area_pointer = Qarrow;
27358
27359 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
27360 doc: /* Non-nil means don't actually do any redisplay.
27361 This is used for internal purposes. */);
27362 Vinhibit_redisplay = Qnil;
27363
27364 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
27365 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
27366 Vglobal_mode_string = Qnil;
27367
27368 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
27369 doc: /* Marker for where to display an arrow on top of the buffer text.
27370 This must be the beginning of a line in order to work.
27371 See also `overlay-arrow-string'. */);
27372 Voverlay_arrow_position = Qnil;
27373
27374 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
27375 doc: /* String to display as an arrow in non-window frames.
27376 See also `overlay-arrow-position'. */);
27377 Voverlay_arrow_string = make_pure_c_string ("=>");
27378
27379 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
27380 doc: /* List of variables (symbols) which hold markers for overlay arrows.
27381 The symbols on this list are examined during redisplay to determine
27382 where to display overlay arrows. */);
27383 Voverlay_arrow_variable_list
27384 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
27385
27386 DEFVAR_INT ("scroll-step", emacs_scroll_step,
27387 doc: /* *The number of lines to try scrolling a window by when point moves out.
27388 If that fails to bring point back on frame, point is centered instead.
27389 If this is zero, point is always centered after it moves off frame.
27390 If you want scrolling to always be a line at a time, you should set
27391 `scroll-conservatively' to a large value rather than set this to 1. */);
27392
27393 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
27394 doc: /* *Scroll up to this many lines, to bring point back on screen.
27395 If point moves off-screen, redisplay will scroll by up to
27396 `scroll-conservatively' lines in order to bring point just barely
27397 onto the screen again. If that cannot be done, then redisplay
27398 recenters point as usual.
27399
27400 If the value is greater than 100, redisplay will never recenter point,
27401 but will always scroll just enough text to bring point into view, even
27402 if you move far away.
27403
27404 A value of zero means always recenter point if it moves off screen. */);
27405 scroll_conservatively = 0;
27406
27407 DEFVAR_INT ("scroll-margin", scroll_margin,
27408 doc: /* *Number of lines of margin at the top and bottom of a window.
27409 Recenter the window whenever point gets within this many lines
27410 of the top or bottom of the window. */);
27411 scroll_margin = 0;
27412
27413 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
27414 doc: /* Pixels per inch value for non-window system displays.
27415 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
27416 Vdisplay_pixels_per_inch = make_float (72.0);
27417
27418 #if GLYPH_DEBUG
27419 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
27420 #endif
27421
27422 DEFVAR_LISP ("truncate-partial-width-windows",
27423 Vtruncate_partial_width_windows,
27424 doc: /* Non-nil means truncate lines in windows narrower than the frame.
27425 For an integer value, truncate lines in each window narrower than the
27426 full frame width, provided the window width is less than that integer;
27427 otherwise, respect the value of `truncate-lines'.
27428
27429 For any other non-nil value, truncate lines in all windows that do
27430 not span the full frame width.
27431
27432 A value of nil means to respect the value of `truncate-lines'.
27433
27434 If `word-wrap' is enabled, you might want to reduce this. */);
27435 Vtruncate_partial_width_windows = make_number (50);
27436
27437 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
27438 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
27439 Any other value means to use the appropriate face, `mode-line',
27440 `header-line', or `menu' respectively. */);
27441 mode_line_inverse_video = 1;
27442
27443 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
27444 doc: /* *Maximum buffer size for which line number should be displayed.
27445 If the buffer is bigger than this, the line number does not appear
27446 in the mode line. A value of nil means no limit. */);
27447 Vline_number_display_limit = Qnil;
27448
27449 DEFVAR_INT ("line-number-display-limit-width",
27450 line_number_display_limit_width,
27451 doc: /* *Maximum line width (in characters) for line number display.
27452 If the average length of the lines near point is bigger than this, then the
27453 line number may be omitted from the mode line. */);
27454 line_number_display_limit_width = 200;
27455
27456 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
27457 doc: /* *Non-nil means highlight region even in nonselected windows. */);
27458 highlight_nonselected_windows = 0;
27459
27460 DEFVAR_BOOL ("multiple-frames", multiple_frames,
27461 doc: /* Non-nil if more than one frame is visible on this display.
27462 Minibuffer-only frames don't count, but iconified frames do.
27463 This variable is not guaranteed to be accurate except while processing
27464 `frame-title-format' and `icon-title-format'. */);
27465
27466 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
27467 doc: /* Template for displaying the title bar of visible frames.
27468 \(Assuming the window manager supports this feature.)
27469
27470 This variable has the same structure as `mode-line-format', except that
27471 the %c and %l constructs are ignored. It is used only on frames for
27472 which no explicit name has been set \(see `modify-frame-parameters'). */);
27473
27474 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
27475 doc: /* Template for displaying the title bar of an iconified frame.
27476 \(Assuming the window manager supports this feature.)
27477 This variable has the same structure as `mode-line-format' (which see),
27478 and is used only on frames for which no explicit name has been set
27479 \(see `modify-frame-parameters'). */);
27480 Vicon_title_format
27481 = Vframe_title_format
27482 = pure_cons (intern_c_string ("multiple-frames"),
27483 pure_cons (make_pure_c_string ("%b"),
27484 pure_cons (pure_cons (empty_unibyte_string,
27485 pure_cons (intern_c_string ("invocation-name"),
27486 pure_cons (make_pure_c_string ("@"),
27487 pure_cons (intern_c_string ("system-name"),
27488 Qnil)))),
27489 Qnil)));
27490
27491 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
27492 doc: /* Maximum number of lines to keep in the message log buffer.
27493 If nil, disable message logging. If t, log messages but don't truncate
27494 the buffer when it becomes large. */);
27495 Vmessage_log_max = make_number (100);
27496
27497 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
27498 doc: /* Functions called before redisplay, if window sizes have changed.
27499 The value should be a list of functions that take one argument.
27500 Just before redisplay, for each frame, if any of its windows have changed
27501 size since the last redisplay, or have been split or deleted,
27502 all the functions in the list are called, with the frame as argument. */);
27503 Vwindow_size_change_functions = Qnil;
27504
27505 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
27506 doc: /* List of functions to call before redisplaying a window with scrolling.
27507 Each function is called with two arguments, the window and its new
27508 display-start position. Note that these functions are also called by
27509 `set-window-buffer'. Also note that the value of `window-end' is not
27510 valid when these functions are called. */);
27511 Vwindow_scroll_functions = Qnil;
27512
27513 DEFVAR_LISP ("window-text-change-functions",
27514 Vwindow_text_change_functions,
27515 doc: /* Functions to call in redisplay when text in the window might change. */);
27516 Vwindow_text_change_functions = Qnil;
27517
27518 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
27519 doc: /* Functions called when redisplay of a window reaches the end trigger.
27520 Each function is called with two arguments, the window and the end trigger value.
27521 See `set-window-redisplay-end-trigger'. */);
27522 Vredisplay_end_trigger_functions = Qnil;
27523
27524 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
27525 doc: /* *Non-nil means autoselect window with mouse pointer.
27526 If nil, do not autoselect windows.
27527 A positive number means delay autoselection by that many seconds: a
27528 window is autoselected only after the mouse has remained in that
27529 window for the duration of the delay.
27530 A negative number has a similar effect, but causes windows to be
27531 autoselected only after the mouse has stopped moving. \(Because of
27532 the way Emacs compares mouse events, you will occasionally wait twice
27533 that time before the window gets selected.\)
27534 Any other value means to autoselect window instantaneously when the
27535 mouse pointer enters it.
27536
27537 Autoselection selects the minibuffer only if it is active, and never
27538 unselects the minibuffer if it is active.
27539
27540 When customizing this variable make sure that the actual value of
27541 `focus-follows-mouse' matches the behavior of your window manager. */);
27542 Vmouse_autoselect_window = Qnil;
27543
27544 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
27545 doc: /* *Non-nil means automatically resize tool-bars.
27546 This dynamically changes the tool-bar's height to the minimum height
27547 that is needed to make all tool-bar items visible.
27548 If value is `grow-only', the tool-bar's height is only increased
27549 automatically; to decrease the tool-bar height, use \\[recenter]. */);
27550 Vauto_resize_tool_bars = Qt;
27551
27552 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
27553 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
27554 auto_raise_tool_bar_buttons_p = 1;
27555
27556 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
27557 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
27558 make_cursor_line_fully_visible_p = 1;
27559
27560 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
27561 doc: /* *Border below tool-bar in pixels.
27562 If an integer, use it as the height of the border.
27563 If it is one of `internal-border-width' or `border-width', use the
27564 value of the corresponding frame parameter.
27565 Otherwise, no border is added below the tool-bar. */);
27566 Vtool_bar_border = Qinternal_border_width;
27567
27568 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
27569 doc: /* *Margin around tool-bar buttons in pixels.
27570 If an integer, use that for both horizontal and vertical margins.
27571 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
27572 HORZ specifying the horizontal margin, and VERT specifying the
27573 vertical margin. */);
27574 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
27575
27576 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
27577 doc: /* *Relief thickness of tool-bar buttons. */);
27578 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
27579
27580 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
27581 doc: /* Tool bar style to use.
27582 It can be one of
27583 image - show images only
27584 text - show text only
27585 both - show both, text below image
27586 both-horiz - show text to the right of the image
27587 text-image-horiz - show text to the left of the image
27588 any other - use system default or image if no system default. */);
27589 Vtool_bar_style = Qnil;
27590
27591 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
27592 doc: /* *Maximum number of characters a label can have to be shown.
27593 The tool bar style must also show labels for this to have any effect, see
27594 `tool-bar-style'. */);
27595 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
27596
27597 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
27598 doc: /* List of functions to call to fontify regions of text.
27599 Each function is called with one argument POS. Functions must
27600 fontify a region starting at POS in the current buffer, and give
27601 fontified regions the property `fontified'. */);
27602 Vfontification_functions = Qnil;
27603 Fmake_variable_buffer_local (Qfontification_functions);
27604
27605 DEFVAR_BOOL ("unibyte-display-via-language-environment",
27606 unibyte_display_via_language_environment,
27607 doc: /* *Non-nil means display unibyte text according to language environment.
27608 Specifically, this means that raw bytes in the range 160-255 decimal
27609 are displayed by converting them to the equivalent multibyte characters
27610 according to the current language environment. As a result, they are
27611 displayed according to the current fontset.
27612
27613 Note that this variable affects only how these bytes are displayed,
27614 but does not change the fact they are interpreted as raw bytes. */);
27615 unibyte_display_via_language_environment = 0;
27616
27617 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
27618 doc: /* *Maximum height for resizing mini-windows.
27619 If a float, it specifies a fraction of the mini-window frame's height.
27620 If an integer, it specifies a number of lines. */);
27621 Vmax_mini_window_height = make_float (0.25);
27622
27623 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
27624 doc: /* *How to resize mini-windows.
27625 A value of nil means don't automatically resize mini-windows.
27626 A value of t means resize them to fit the text displayed in them.
27627 A value of `grow-only', the default, means let mini-windows grow
27628 only, until their display becomes empty, at which point the windows
27629 go back to their normal size. */);
27630 Vresize_mini_windows = Qgrow_only;
27631
27632 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
27633 doc: /* Alist specifying how to blink the cursor off.
27634 Each element has the form (ON-STATE . OFF-STATE). Whenever the
27635 `cursor-type' frame-parameter or variable equals ON-STATE,
27636 comparing using `equal', Emacs uses OFF-STATE to specify
27637 how to blink it off. ON-STATE and OFF-STATE are values for
27638 the `cursor-type' frame parameter.
27639
27640 If a frame's ON-STATE has no entry in this list,
27641 the frame's other specifications determine how to blink the cursor off. */);
27642 Vblink_cursor_alist = Qnil;
27643
27644 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
27645 doc: /* Allow or disallow automatic horizontal scrolling of windows.
27646 If non-nil, windows are automatically scrolled horizontally to make
27647 point visible. */);
27648 automatic_hscrolling_p = 1;
27649 Qauto_hscroll_mode = intern_c_string ("auto-hscroll-mode");
27650 staticpro (&Qauto_hscroll_mode);
27651
27652 DEFVAR_INT ("hscroll-margin", hscroll_margin,
27653 doc: /* *How many columns away from the window edge point is allowed to get
27654 before automatic hscrolling will horizontally scroll the window. */);
27655 hscroll_margin = 5;
27656
27657 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
27658 doc: /* *How many columns to scroll the window when point gets too close to the edge.
27659 When point is less than `hscroll-margin' columns from the window
27660 edge, automatic hscrolling will scroll the window by the amount of columns
27661 determined by this variable. If its value is a positive integer, scroll that
27662 many columns. If it's a positive floating-point number, it specifies the
27663 fraction of the window's width to scroll. If it's nil or zero, point will be
27664 centered horizontally after the scroll. Any other value, including negative
27665 numbers, are treated as if the value were zero.
27666
27667 Automatic hscrolling always moves point outside the scroll margin, so if
27668 point was more than scroll step columns inside the margin, the window will
27669 scroll more than the value given by the scroll step.
27670
27671 Note that the lower bound for automatic hscrolling specified by `scroll-left'
27672 and `scroll-right' overrides this variable's effect. */);
27673 Vhscroll_step = make_number (0);
27674
27675 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
27676 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
27677 Bind this around calls to `message' to let it take effect. */);
27678 message_truncate_lines = 0;
27679
27680 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
27681 doc: /* Normal hook run to update the menu bar definitions.
27682 Redisplay runs this hook before it redisplays the menu bar.
27683 This is used to update submenus such as Buffers,
27684 whose contents depend on various data. */);
27685 Vmenu_bar_update_hook = Qnil;
27686
27687 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
27688 doc: /* Frame for which we are updating a menu.
27689 The enable predicate for a menu binding should check this variable. */);
27690 Vmenu_updating_frame = Qnil;
27691
27692 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
27693 doc: /* Non-nil means don't update menu bars. Internal use only. */);
27694 inhibit_menubar_update = 0;
27695
27696 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
27697 doc: /* Prefix prepended to all continuation lines at display time.
27698 The value may be a string, an image, or a stretch-glyph; it is
27699 interpreted in the same way as the value of a `display' text property.
27700
27701 This variable is overridden by any `wrap-prefix' text or overlay
27702 property.
27703
27704 To add a prefix to non-continuation lines, use `line-prefix'. */);
27705 Vwrap_prefix = Qnil;
27706 staticpro (&Qwrap_prefix);
27707 Qwrap_prefix = intern_c_string ("wrap-prefix");
27708 Fmake_variable_buffer_local (Qwrap_prefix);
27709
27710 DEFVAR_LISP ("line-prefix", Vline_prefix,
27711 doc: /* Prefix prepended to all non-continuation lines at display time.
27712 The value may be a string, an image, or a stretch-glyph; it is
27713 interpreted in the same way as the value of a `display' text property.
27714
27715 This variable is overridden by any `line-prefix' text or overlay
27716 property.
27717
27718 To add a prefix to continuation lines, use `wrap-prefix'. */);
27719 Vline_prefix = Qnil;
27720 staticpro (&Qline_prefix);
27721 Qline_prefix = intern_c_string ("line-prefix");
27722 Fmake_variable_buffer_local (Qline_prefix);
27723
27724 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
27725 doc: /* Non-nil means don't eval Lisp during redisplay. */);
27726 inhibit_eval_during_redisplay = 0;
27727
27728 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
27729 doc: /* Non-nil means don't free realized faces. Internal use only. */);
27730 inhibit_free_realized_faces = 0;
27731
27732 #if GLYPH_DEBUG
27733 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
27734 doc: /* Inhibit try_window_id display optimization. */);
27735 inhibit_try_window_id = 0;
27736
27737 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
27738 doc: /* Inhibit try_window_reusing display optimization. */);
27739 inhibit_try_window_reusing = 0;
27740
27741 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
27742 doc: /* Inhibit try_cursor_movement display optimization. */);
27743 inhibit_try_cursor_movement = 0;
27744 #endif /* GLYPH_DEBUG */
27745
27746 DEFVAR_INT ("overline-margin", overline_margin,
27747 doc: /* *Space between overline and text, in pixels.
27748 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
27749 margin to the caracter height. */);
27750 overline_margin = 2;
27751
27752 DEFVAR_INT ("underline-minimum-offset",
27753 underline_minimum_offset,
27754 doc: /* Minimum distance between baseline and underline.
27755 This can improve legibility of underlined text at small font sizes,
27756 particularly when using variable `x-use-underline-position-properties'
27757 with fonts that specify an UNDERLINE_POSITION relatively close to the
27758 baseline. The default value is 1. */);
27759 underline_minimum_offset = 1;
27760
27761 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
27762 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
27763 This feature only works when on a window system that can change
27764 cursor shapes. */);
27765 display_hourglass_p = 1;
27766
27767 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
27768 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
27769 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
27770
27771 hourglass_atimer = NULL;
27772 hourglass_shown_p = 0;
27773
27774 DEFSYM (Qglyphless_char, "glyphless-char");
27775 DEFSYM (Qhex_code, "hex-code");
27776 DEFSYM (Qempty_box, "empty-box");
27777 DEFSYM (Qthin_space, "thin-space");
27778 DEFSYM (Qzero_width, "zero-width");
27779
27780 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
27781 /* Intern this now in case it isn't already done.
27782 Setting this variable twice is harmless.
27783 But don't staticpro it here--that is done in alloc.c. */
27784 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
27785 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
27786
27787 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
27788 doc: /* Char-table defining glyphless characters.
27789 Each element, if non-nil, should be one of the following:
27790 an ASCII acronym string: display this string in a box
27791 `hex-code': display the hexadecimal code of a character in a box
27792 `empty-box': display as an empty box
27793 `thin-space': display as 1-pixel width space
27794 `zero-width': don't display
27795 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
27796 display method for graphical terminals and text terminals respectively.
27797 GRAPHICAL and TEXT should each have one of the values listed above.
27798
27799 The char-table has one extra slot to control the display of a character for
27800 which no font is found. This slot only takes effect on graphical terminals.
27801 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
27802 `thin-space'. The default is `empty-box'. */);
27803 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
27804 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
27805 Qempty_box);
27806 }
27807
27808
27809 /* Initialize this module when Emacs starts. */
27810
27811 void
27812 init_xdisp (void)
27813 {
27814 Lisp_Object root_window;
27815 struct window *mini_w;
27816
27817 current_header_line_height = current_mode_line_height = -1;
27818
27819 CHARPOS (this_line_start_pos) = 0;
27820
27821 mini_w = XWINDOW (minibuf_window);
27822 root_window = FRAME_ROOT_WINDOW (XFRAME (WINDOW_FRAME (mini_w)));
27823 echo_area_window = minibuf_window;
27824
27825 if (!noninteractive)
27826 {
27827 struct frame *f = XFRAME (WINDOW_FRAME (XWINDOW (root_window)));
27828 int i;
27829
27830 XWINDOW (root_window)->top_line = make_number (FRAME_TOP_MARGIN (f));
27831 set_window_height (root_window,
27832 FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f),
27833 0);
27834 mini_w->top_line = make_number (FRAME_LINES (f) - 1);
27835 set_window_height (minibuf_window, 1, 0);
27836
27837 XWINDOW (root_window)->total_cols = make_number (FRAME_COLS (f));
27838 mini_w->total_cols = make_number (FRAME_COLS (f));
27839
27840 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
27841 scratch_glyph_row.glyphs[TEXT_AREA + 1]
27842 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
27843
27844 /* The default ellipsis glyphs `...'. */
27845 for (i = 0; i < 3; ++i)
27846 default_invis_vector[i] = make_number ('.');
27847 }
27848
27849 {
27850 /* Allocate the buffer for frame titles.
27851 Also used for `format-mode-line'. */
27852 int size = 100;
27853 mode_line_noprop_buf = (char *) xmalloc (size);
27854 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
27855 mode_line_noprop_ptr = mode_line_noprop_buf;
27856 mode_line_target = MODE_LINE_DISPLAY;
27857 }
27858
27859 help_echo_showing_p = 0;
27860 }
27861
27862 /* Since w32 does not support atimers, it defines its own implementation of
27863 the following three functions in w32fns.c. */
27864 #ifndef WINDOWSNT
27865
27866 /* Platform-independent portion of hourglass implementation. */
27867
27868 /* Return non-zero if houglass timer has been started or hourglass is shown. */
27869 int
27870 hourglass_started (void)
27871 {
27872 return hourglass_shown_p || hourglass_atimer != NULL;
27873 }
27874
27875 /* Cancel a currently active hourglass timer, and start a new one. */
27876 void
27877 start_hourglass (void)
27878 {
27879 #if defined (HAVE_WINDOW_SYSTEM)
27880 EMACS_TIME delay;
27881 int secs, usecs = 0;
27882
27883 cancel_hourglass ();
27884
27885 if (INTEGERP (Vhourglass_delay)
27886 && XINT (Vhourglass_delay) > 0)
27887 secs = XFASTINT (Vhourglass_delay);
27888 else if (FLOATP (Vhourglass_delay)
27889 && XFLOAT_DATA (Vhourglass_delay) > 0)
27890 {
27891 Lisp_Object tem;
27892 tem = Ftruncate (Vhourglass_delay, Qnil);
27893 secs = XFASTINT (tem);
27894 usecs = (XFLOAT_DATA (Vhourglass_delay) - secs) * 1000000;
27895 }
27896 else
27897 secs = DEFAULT_HOURGLASS_DELAY;
27898
27899 EMACS_SET_SECS_USECS (delay, secs, usecs);
27900 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
27901 show_hourglass, NULL);
27902 #endif
27903 }
27904
27905
27906 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
27907 shown. */
27908 void
27909 cancel_hourglass (void)
27910 {
27911 #if defined (HAVE_WINDOW_SYSTEM)
27912 if (hourglass_atimer)
27913 {
27914 cancel_atimer (hourglass_atimer);
27915 hourglass_atimer = NULL;
27916 }
27917
27918 if (hourglass_shown_p)
27919 hide_hourglass ();
27920 #endif
27921 }
27922 #endif /* ! WINDOWSNT */