]> code.delx.au - gnu-emacs/blob - src/xdisp.c
Merge from trunk.
[gnu-emacs] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2011 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276 #include <setjmp.h>
277
278 #include "lisp.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "buffer.h"
285 #include "character.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301
302 #ifdef HAVE_X_WINDOWS
303 #include "xterm.h"
304 #endif
305 #ifdef WINDOWSNT
306 #include "w32term.h"
307 #endif
308 #ifdef HAVE_NS
309 #include "nsterm.h"
310 #endif
311 #ifdef USE_GTK
312 #include "gtkutil.h"
313 #endif
314
315 #include "font.h"
316
317 #ifndef FRAME_X_OUTPUT
318 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
319 #endif
320
321 #define INFINITY 10000000
322
323 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
324 Lisp_Object Qwindow_scroll_functions;
325 static Lisp_Object Qwindow_text_change_functions;
326 static Lisp_Object Qredisplay_end_trigger_functions;
327 Lisp_Object Qinhibit_point_motion_hooks;
328 static Lisp_Object QCeval, QCpropertize;
329 Lisp_Object QCfile, QCdata;
330 static Lisp_Object Qfontified;
331 static Lisp_Object Qgrow_only;
332 static Lisp_Object Qinhibit_eval_during_redisplay;
333 static Lisp_Object Qbuffer_position, Qposition, Qobject;
334 static Lisp_Object Qright_to_left, Qleft_to_right;
335
336 /* Cursor shapes */
337 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
338
339 /* Pointer shapes */
340 static Lisp_Object Qarrow, Qhand;
341 Lisp_Object Qtext;
342
343 /* Holds the list (error). */
344 static Lisp_Object list_of_error;
345
346 static Lisp_Object Qfontification_functions;
347
348 static Lisp_Object Qwrap_prefix;
349 static Lisp_Object Qline_prefix;
350
351 /* Non-nil means don't actually do any redisplay. */
352
353 Lisp_Object Qinhibit_redisplay;
354
355 /* Names of text properties relevant for redisplay. */
356
357 Lisp_Object Qdisplay;
358
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
367
368 #ifdef HAVE_WINDOW_SYSTEM
369
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
372
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x \
380 && (IT)->line_wrap != WORD_WRAP)
381
382 #else /* !HAVE_WINDOW_SYSTEM */
383 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
384 #endif /* HAVE_WINDOW_SYSTEM */
385
386 /* Test if the display element loaded in IT is a space or tab
387 character. This is used to determine word wrapping. */
388
389 #define IT_DISPLAYING_WHITESPACE(it) \
390 (it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t'))
391
392 /* Name of the face used to highlight trailing whitespace. */
393
394 static Lisp_Object Qtrailing_whitespace;
395
396 /* Name and number of the face used to highlight escape glyphs. */
397
398 static Lisp_Object Qescape_glyph;
399
400 /* Name and number of the face used to highlight non-breaking spaces. */
401
402 static Lisp_Object Qnobreak_space;
403
404 /* The symbol `image' which is the car of the lists used to represent
405 images in Lisp. Also a tool bar style. */
406
407 Lisp_Object Qimage;
408
409 /* The image map types. */
410 Lisp_Object QCmap;
411 static Lisp_Object QCpointer;
412 static Lisp_Object Qrect, Qcircle, Qpoly;
413
414 /* Tool bar styles */
415 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
416
417 /* Non-zero means print newline to stdout before next mini-buffer
418 message. */
419
420 int noninteractive_need_newline;
421
422 /* Non-zero means print newline to message log before next message. */
423
424 static int message_log_need_newline;
425
426 /* Three markers that message_dolog uses.
427 It could allocate them itself, but that causes trouble
428 in handling memory-full errors. */
429 static Lisp_Object message_dolog_marker1;
430 static Lisp_Object message_dolog_marker2;
431 static Lisp_Object message_dolog_marker3;
432 \f
433 /* The buffer position of the first character appearing entirely or
434 partially on the line of the selected window which contains the
435 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
436 redisplay optimization in redisplay_internal. */
437
438 static struct text_pos this_line_start_pos;
439
440 /* Number of characters past the end of the line above, including the
441 terminating newline. */
442
443 static struct text_pos this_line_end_pos;
444
445 /* The vertical positions and the height of this line. */
446
447 static int this_line_vpos;
448 static int this_line_y;
449 static int this_line_pixel_height;
450
451 /* X position at which this display line starts. Usually zero;
452 negative if first character is partially visible. */
453
454 static int this_line_start_x;
455
456 /* The smallest character position seen by move_it_* functions as they
457 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
458 hscrolled lines, see display_line. */
459
460 static struct text_pos this_line_min_pos;
461
462 /* Buffer that this_line_.* variables are referring to. */
463
464 static struct buffer *this_line_buffer;
465
466
467 /* Values of those variables at last redisplay are stored as
468 properties on `overlay-arrow-position' symbol. However, if
469 Voverlay_arrow_position is a marker, last-arrow-position is its
470 numerical position. */
471
472 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
473
474 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
475 properties on a symbol in overlay-arrow-variable-list. */
476
477 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
478
479 Lisp_Object Qmenu_bar_update_hook;
480
481 /* Nonzero if an overlay arrow has been displayed in this window. */
482
483 static int overlay_arrow_seen;
484
485 /* Number of windows showing the buffer of the selected window (or
486 another buffer with the same base buffer). keyboard.c refers to
487 this. */
488
489 int buffer_shared;
490
491 /* Vector containing glyphs for an ellipsis `...'. */
492
493 static Lisp_Object default_invis_vector[3];
494
495 /* This is the window where the echo area message was displayed. It
496 is always a mini-buffer window, but it may not be the same window
497 currently active as a mini-buffer. */
498
499 Lisp_Object echo_area_window;
500
501 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
502 pushes the current message and the value of
503 message_enable_multibyte on the stack, the function restore_message
504 pops the stack and displays MESSAGE again. */
505
506 static Lisp_Object Vmessage_stack;
507
508 /* Nonzero means multibyte characters were enabled when the echo area
509 message was specified. */
510
511 static int message_enable_multibyte;
512
513 /* Nonzero if we should redraw the mode lines on the next redisplay. */
514
515 int update_mode_lines;
516
517 /* Nonzero if window sizes or contents have changed since last
518 redisplay that finished. */
519
520 int windows_or_buffers_changed;
521
522 /* Nonzero means a frame's cursor type has been changed. */
523
524 int cursor_type_changed;
525
526 /* Nonzero after display_mode_line if %l was used and it displayed a
527 line number. */
528
529 static int line_number_displayed;
530
531 /* The name of the *Messages* buffer, a string. */
532
533 static Lisp_Object Vmessages_buffer_name;
534
535 /* Current, index 0, and last displayed echo area message. Either
536 buffers from echo_buffers, or nil to indicate no message. */
537
538 Lisp_Object echo_area_buffer[2];
539
540 /* The buffers referenced from echo_area_buffer. */
541
542 static Lisp_Object echo_buffer[2];
543
544 /* A vector saved used in with_area_buffer to reduce consing. */
545
546 static Lisp_Object Vwith_echo_area_save_vector;
547
548 /* Non-zero means display_echo_area should display the last echo area
549 message again. Set by redisplay_preserve_echo_area. */
550
551 static int display_last_displayed_message_p;
552
553 /* Nonzero if echo area is being used by print; zero if being used by
554 message. */
555
556 static int message_buf_print;
557
558 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
559
560 static Lisp_Object Qinhibit_menubar_update;
561 static Lisp_Object Qmessage_truncate_lines;
562
563 /* Set to 1 in clear_message to make redisplay_internal aware
564 of an emptied echo area. */
565
566 static int message_cleared_p;
567
568 /* A scratch glyph row with contents used for generating truncation
569 glyphs. Also used in direct_output_for_insert. */
570
571 #define MAX_SCRATCH_GLYPHS 100
572 static struct glyph_row scratch_glyph_row;
573 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
574
575 /* Ascent and height of the last line processed by move_it_to. */
576
577 static int last_max_ascent, last_height;
578
579 /* Non-zero if there's a help-echo in the echo area. */
580
581 int help_echo_showing_p;
582
583 /* If >= 0, computed, exact values of mode-line and header-line height
584 to use in the macros CURRENT_MODE_LINE_HEIGHT and
585 CURRENT_HEADER_LINE_HEIGHT. */
586
587 int current_mode_line_height, current_header_line_height;
588
589 /* The maximum distance to look ahead for text properties. Values
590 that are too small let us call compute_char_face and similar
591 functions too often which is expensive. Values that are too large
592 let us call compute_char_face and alike too often because we
593 might not be interested in text properties that far away. */
594
595 #define TEXT_PROP_DISTANCE_LIMIT 100
596
597 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
598 iterator state and later restore it. This is needed because the
599 bidi iterator on bidi.c keeps a stacked cache of its states, which
600 is really a singleton. When we use scratch iterator objects to
601 move around the buffer, we can cause the bidi cache to be pushed or
602 popped, and therefore we need to restore the cache state when we
603 return to the original iterator. */
604 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
605 do { \
606 if (CACHE) \
607 bidi_unshelve_cache (CACHE, 1); \
608 ITCOPY = ITORIG; \
609 CACHE = bidi_shelve_cache (); \
610 } while (0)
611
612 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
613 do { \
614 if (pITORIG != pITCOPY) \
615 *(pITORIG) = *(pITCOPY); \
616 bidi_unshelve_cache (CACHE, 0); \
617 CACHE = NULL; \
618 } while (0)
619
620 #if GLYPH_DEBUG
621
622 /* Non-zero means print traces of redisplay if compiled with
623 GLYPH_DEBUG != 0. */
624
625 int trace_redisplay_p;
626
627 #endif /* GLYPH_DEBUG */
628
629 #ifdef DEBUG_TRACE_MOVE
630 /* Non-zero means trace with TRACE_MOVE to stderr. */
631 int trace_move;
632
633 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
634 #else
635 #define TRACE_MOVE(x) (void) 0
636 #endif
637
638 static Lisp_Object Qauto_hscroll_mode;
639
640 /* Buffer being redisplayed -- for redisplay_window_error. */
641
642 static struct buffer *displayed_buffer;
643
644 /* Value returned from text property handlers (see below). */
645
646 enum prop_handled
647 {
648 HANDLED_NORMALLY,
649 HANDLED_RECOMPUTE_PROPS,
650 HANDLED_OVERLAY_STRING_CONSUMED,
651 HANDLED_RETURN
652 };
653
654 /* A description of text properties that redisplay is interested
655 in. */
656
657 struct props
658 {
659 /* The name of the property. */
660 Lisp_Object *name;
661
662 /* A unique index for the property. */
663 enum prop_idx idx;
664
665 /* A handler function called to set up iterator IT from the property
666 at IT's current position. Value is used to steer handle_stop. */
667 enum prop_handled (*handler) (struct it *it);
668 };
669
670 static enum prop_handled handle_face_prop (struct it *);
671 static enum prop_handled handle_invisible_prop (struct it *);
672 static enum prop_handled handle_display_prop (struct it *);
673 static enum prop_handled handle_composition_prop (struct it *);
674 static enum prop_handled handle_overlay_change (struct it *);
675 static enum prop_handled handle_fontified_prop (struct it *);
676
677 /* Properties handled by iterators. */
678
679 static struct props it_props[] =
680 {
681 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
682 /* Handle `face' before `display' because some sub-properties of
683 `display' need to know the face. */
684 {&Qface, FACE_PROP_IDX, handle_face_prop},
685 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
686 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
687 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
688 {NULL, 0, NULL}
689 };
690
691 /* Value is the position described by X. If X is a marker, value is
692 the marker_position of X. Otherwise, value is X. */
693
694 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
695
696 /* Enumeration returned by some move_it_.* functions internally. */
697
698 enum move_it_result
699 {
700 /* Not used. Undefined value. */
701 MOVE_UNDEFINED,
702
703 /* Move ended at the requested buffer position or ZV. */
704 MOVE_POS_MATCH_OR_ZV,
705
706 /* Move ended at the requested X pixel position. */
707 MOVE_X_REACHED,
708
709 /* Move within a line ended at the end of a line that must be
710 continued. */
711 MOVE_LINE_CONTINUED,
712
713 /* Move within a line ended at the end of a line that would
714 be displayed truncated. */
715 MOVE_LINE_TRUNCATED,
716
717 /* Move within a line ended at a line end. */
718 MOVE_NEWLINE_OR_CR
719 };
720
721 /* This counter is used to clear the face cache every once in a while
722 in redisplay_internal. It is incremented for each redisplay.
723 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
724 cleared. */
725
726 #define CLEAR_FACE_CACHE_COUNT 500
727 static int clear_face_cache_count;
728
729 /* Similarly for the image cache. */
730
731 #ifdef HAVE_WINDOW_SYSTEM
732 #define CLEAR_IMAGE_CACHE_COUNT 101
733 static int clear_image_cache_count;
734
735 /* Null glyph slice */
736 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
737 #endif
738
739 /* Non-zero while redisplay_internal is in progress. */
740
741 int redisplaying_p;
742
743 static Lisp_Object Qinhibit_free_realized_faces;
744
745 /* If a string, XTread_socket generates an event to display that string.
746 (The display is done in read_char.) */
747
748 Lisp_Object help_echo_string;
749 Lisp_Object help_echo_window;
750 Lisp_Object help_echo_object;
751 ptrdiff_t help_echo_pos;
752
753 /* Temporary variable for XTread_socket. */
754
755 Lisp_Object previous_help_echo_string;
756
757 /* Platform-independent portion of hourglass implementation. */
758
759 /* Non-zero means an hourglass cursor is currently shown. */
760 int hourglass_shown_p;
761
762 /* If non-null, an asynchronous timer that, when it expires, displays
763 an hourglass cursor on all frames. */
764 struct atimer *hourglass_atimer;
765
766 /* Name of the face used to display glyphless characters. */
767 Lisp_Object Qglyphless_char;
768
769 /* Symbol for the purpose of Vglyphless_char_display. */
770 static Lisp_Object Qglyphless_char_display;
771
772 /* Method symbols for Vglyphless_char_display. */
773 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
774
775 /* Default pixel width of `thin-space' display method. */
776 #define THIN_SPACE_WIDTH 1
777
778 /* Default number of seconds to wait before displaying an hourglass
779 cursor. */
780 #define DEFAULT_HOURGLASS_DELAY 1
781
782 \f
783 /* Function prototypes. */
784
785 static void setup_for_ellipsis (struct it *, int);
786 static void set_iterator_to_next (struct it *, int);
787 static void mark_window_display_accurate_1 (struct window *, int);
788 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
789 static int display_prop_string_p (Lisp_Object, Lisp_Object);
790 static int cursor_row_p (struct glyph_row *);
791 static int redisplay_mode_lines (Lisp_Object, int);
792 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
793
794 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
795
796 static void handle_line_prefix (struct it *);
797
798 static void pint2str (char *, int, ptrdiff_t);
799 static void pint2hrstr (char *, int, ptrdiff_t);
800 static struct text_pos run_window_scroll_functions (Lisp_Object,
801 struct text_pos);
802 static void reconsider_clip_changes (struct window *, struct buffer *);
803 static int text_outside_line_unchanged_p (struct window *,
804 ptrdiff_t, ptrdiff_t);
805 static void store_mode_line_noprop_char (char);
806 static int store_mode_line_noprop (const char *, int, int);
807 static void handle_stop (struct it *);
808 static void handle_stop_backwards (struct it *, ptrdiff_t);
809 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
810 static void ensure_echo_area_buffers (void);
811 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
812 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
813 static int with_echo_area_buffer (struct window *, int,
814 int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
815 ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
816 static void clear_garbaged_frames (void);
817 static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
818 static void pop_message (void);
819 static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
820 static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
821 static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
822 static int display_echo_area (struct window *);
823 static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
824 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
825 static Lisp_Object unwind_redisplay (Lisp_Object);
826 static int string_char_and_length (const unsigned char *, int *);
827 static struct text_pos display_prop_end (struct it *, Lisp_Object,
828 struct text_pos);
829 static int compute_window_start_on_continuation_line (struct window *);
830 static Lisp_Object safe_eval_handler (Lisp_Object);
831 static void insert_left_trunc_glyphs (struct it *);
832 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
833 Lisp_Object);
834 static void extend_face_to_end_of_line (struct it *);
835 static int append_space_for_newline (struct it *, int);
836 static int cursor_row_fully_visible_p (struct window *, int, int);
837 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
838 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
839 static int trailing_whitespace_p (ptrdiff_t);
840 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
841 static void push_it (struct it *, struct text_pos *);
842 static void pop_it (struct it *);
843 static void sync_frame_with_window_matrix_rows (struct window *);
844 static void select_frame_for_redisplay (Lisp_Object);
845 static void redisplay_internal (void);
846 static int echo_area_display (int);
847 static void redisplay_windows (Lisp_Object);
848 static void redisplay_window (Lisp_Object, int);
849 static Lisp_Object redisplay_window_error (Lisp_Object);
850 static Lisp_Object redisplay_window_0 (Lisp_Object);
851 static Lisp_Object redisplay_window_1 (Lisp_Object);
852 static int set_cursor_from_row (struct window *, struct glyph_row *,
853 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
854 int, int);
855 static int update_menu_bar (struct frame *, int, int);
856 static int try_window_reusing_current_matrix (struct window *);
857 static int try_window_id (struct window *);
858 static int display_line (struct it *);
859 static int display_mode_lines (struct window *);
860 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
861 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
862 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
863 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
864 static void display_menu_bar (struct window *);
865 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
866 ptrdiff_t *);
867 static int display_string (const char *, Lisp_Object, Lisp_Object,
868 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
869 static void compute_line_metrics (struct it *);
870 static void run_redisplay_end_trigger_hook (struct it *);
871 static int get_overlay_strings (struct it *, ptrdiff_t);
872 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
873 static void next_overlay_string (struct it *);
874 static void reseat (struct it *, struct text_pos, int);
875 static void reseat_1 (struct it *, struct text_pos, int);
876 static void back_to_previous_visible_line_start (struct it *);
877 void reseat_at_previous_visible_line_start (struct it *);
878 static void reseat_at_next_visible_line_start (struct it *, int);
879 static int next_element_from_ellipsis (struct it *);
880 static int next_element_from_display_vector (struct it *);
881 static int next_element_from_string (struct it *);
882 static int next_element_from_c_string (struct it *);
883 static int next_element_from_buffer (struct it *);
884 static int next_element_from_composition (struct it *);
885 static int next_element_from_image (struct it *);
886 static int next_element_from_stretch (struct it *);
887 static void load_overlay_strings (struct it *, ptrdiff_t);
888 static int init_from_display_pos (struct it *, struct window *,
889 struct display_pos *);
890 static void reseat_to_string (struct it *, const char *,
891 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
892 static int get_next_display_element (struct it *);
893 static enum move_it_result
894 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
895 enum move_operation_enum);
896 void move_it_vertically_backward (struct it *, int);
897 static void init_to_row_start (struct it *, struct window *,
898 struct glyph_row *);
899 static int init_to_row_end (struct it *, struct window *,
900 struct glyph_row *);
901 static void back_to_previous_line_start (struct it *);
902 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
903 static struct text_pos string_pos_nchars_ahead (struct text_pos,
904 Lisp_Object, ptrdiff_t);
905 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
906 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
907 static ptrdiff_t number_of_chars (const char *, int);
908 static void compute_stop_pos (struct it *);
909 static void compute_string_pos (struct text_pos *, struct text_pos,
910 Lisp_Object);
911 static int face_before_or_after_it_pos (struct it *, int);
912 static ptrdiff_t next_overlay_change (ptrdiff_t);
913 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
914 Lisp_Object, struct text_pos *, ptrdiff_t, int);
915 static int handle_single_display_spec (struct it *, Lisp_Object,
916 Lisp_Object, Lisp_Object,
917 struct text_pos *, ptrdiff_t, int, int);
918 static int underlying_face_id (struct it *);
919 static int in_ellipses_for_invisible_text_p (struct display_pos *,
920 struct window *);
921
922 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
923 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
924
925 #ifdef HAVE_WINDOW_SYSTEM
926
927 static void x_consider_frame_title (Lisp_Object);
928 static int tool_bar_lines_needed (struct frame *, int *);
929 static void update_tool_bar (struct frame *, int);
930 static void build_desired_tool_bar_string (struct frame *f);
931 static int redisplay_tool_bar (struct frame *);
932 static void display_tool_bar_line (struct it *, int);
933 static void notice_overwritten_cursor (struct window *,
934 enum glyph_row_area,
935 int, int, int, int);
936 static void append_stretch_glyph (struct it *, Lisp_Object,
937 int, int, int);
938
939
940 #endif /* HAVE_WINDOW_SYSTEM */
941
942 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
943 static int coords_in_mouse_face_p (struct window *, int, int);
944
945
946 \f
947 /***********************************************************************
948 Window display dimensions
949 ***********************************************************************/
950
951 /* Return the bottom boundary y-position for text lines in window W.
952 This is the first y position at which a line cannot start.
953 It is relative to the top of the window.
954
955 This is the height of W minus the height of a mode line, if any. */
956
957 int
958 window_text_bottom_y (struct window *w)
959 {
960 int height = WINDOW_TOTAL_HEIGHT (w);
961
962 if (WINDOW_WANTS_MODELINE_P (w))
963 height -= CURRENT_MODE_LINE_HEIGHT (w);
964 return height;
965 }
966
967 /* Return the pixel width of display area AREA of window W. AREA < 0
968 means return the total width of W, not including fringes to
969 the left and right of the window. */
970
971 int
972 window_box_width (struct window *w, int area)
973 {
974 int cols = XFASTINT (w->total_cols);
975 int pixels = 0;
976
977 if (!w->pseudo_window_p)
978 {
979 cols -= WINDOW_SCROLL_BAR_COLS (w);
980
981 if (area == TEXT_AREA)
982 {
983 if (INTEGERP (w->left_margin_cols))
984 cols -= XFASTINT (w->left_margin_cols);
985 if (INTEGERP (w->right_margin_cols))
986 cols -= XFASTINT (w->right_margin_cols);
987 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
988 }
989 else if (area == LEFT_MARGIN_AREA)
990 {
991 cols = (INTEGERP (w->left_margin_cols)
992 ? XFASTINT (w->left_margin_cols) : 0);
993 pixels = 0;
994 }
995 else if (area == RIGHT_MARGIN_AREA)
996 {
997 cols = (INTEGERP (w->right_margin_cols)
998 ? XFASTINT (w->right_margin_cols) : 0);
999 pixels = 0;
1000 }
1001 }
1002
1003 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1004 }
1005
1006
1007 /* Return the pixel height of the display area of window W, not
1008 including mode lines of W, if any. */
1009
1010 int
1011 window_box_height (struct window *w)
1012 {
1013 struct frame *f = XFRAME (w->frame);
1014 int height = WINDOW_TOTAL_HEIGHT (w);
1015
1016 xassert (height >= 0);
1017
1018 /* Note: the code below that determines the mode-line/header-line
1019 height is essentially the same as that contained in the macro
1020 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1021 the appropriate glyph row has its `mode_line_p' flag set,
1022 and if it doesn't, uses estimate_mode_line_height instead. */
1023
1024 if (WINDOW_WANTS_MODELINE_P (w))
1025 {
1026 struct glyph_row *ml_row
1027 = (w->current_matrix && w->current_matrix->rows
1028 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1029 : 0);
1030 if (ml_row && ml_row->mode_line_p)
1031 height -= ml_row->height;
1032 else
1033 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1034 }
1035
1036 if (WINDOW_WANTS_HEADER_LINE_P (w))
1037 {
1038 struct glyph_row *hl_row
1039 = (w->current_matrix && w->current_matrix->rows
1040 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1041 : 0);
1042 if (hl_row && hl_row->mode_line_p)
1043 height -= hl_row->height;
1044 else
1045 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1046 }
1047
1048 /* With a very small font and a mode-line that's taller than
1049 default, we might end up with a negative height. */
1050 return max (0, height);
1051 }
1052
1053 /* Return the window-relative coordinate of the left edge of display
1054 area AREA of window W. AREA < 0 means return the left edge of the
1055 whole window, to the right of the left fringe of W. */
1056
1057 int
1058 window_box_left_offset (struct window *w, int area)
1059 {
1060 int x;
1061
1062 if (w->pseudo_window_p)
1063 return 0;
1064
1065 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1066
1067 if (area == TEXT_AREA)
1068 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1069 + window_box_width (w, LEFT_MARGIN_AREA));
1070 else if (area == RIGHT_MARGIN_AREA)
1071 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1072 + window_box_width (w, LEFT_MARGIN_AREA)
1073 + window_box_width (w, TEXT_AREA)
1074 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1075 ? 0
1076 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1077 else if (area == LEFT_MARGIN_AREA
1078 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1079 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1080
1081 return x;
1082 }
1083
1084
1085 /* Return the window-relative coordinate of the right edge of display
1086 area AREA of window W. AREA < 0 means return the right edge of the
1087 whole window, to the left of the right fringe of W. */
1088
1089 int
1090 window_box_right_offset (struct window *w, int area)
1091 {
1092 return window_box_left_offset (w, area) + window_box_width (w, area);
1093 }
1094
1095 /* Return the frame-relative coordinate of the left edge of display
1096 area AREA of window W. AREA < 0 means return the left edge of the
1097 whole window, to the right of the left fringe of W. */
1098
1099 int
1100 window_box_left (struct window *w, int area)
1101 {
1102 struct frame *f = XFRAME (w->frame);
1103 int x;
1104
1105 if (w->pseudo_window_p)
1106 return FRAME_INTERNAL_BORDER_WIDTH (f);
1107
1108 x = (WINDOW_LEFT_EDGE_X (w)
1109 + window_box_left_offset (w, area));
1110
1111 return x;
1112 }
1113
1114
1115 /* Return the frame-relative coordinate of the right edge of display
1116 area AREA of window W. AREA < 0 means return the right edge of the
1117 whole window, to the left of the right fringe of W. */
1118
1119 int
1120 window_box_right (struct window *w, int area)
1121 {
1122 return window_box_left (w, area) + window_box_width (w, area);
1123 }
1124
1125 /* Get the bounding box of the display area AREA of window W, without
1126 mode lines, in frame-relative coordinates. AREA < 0 means the
1127 whole window, not including the left and right fringes of
1128 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1129 coordinates of the upper-left corner of the box. Return in
1130 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1131
1132 void
1133 window_box (struct window *w, int area, int *box_x, int *box_y,
1134 int *box_width, int *box_height)
1135 {
1136 if (box_width)
1137 *box_width = window_box_width (w, area);
1138 if (box_height)
1139 *box_height = window_box_height (w);
1140 if (box_x)
1141 *box_x = window_box_left (w, area);
1142 if (box_y)
1143 {
1144 *box_y = WINDOW_TOP_EDGE_Y (w);
1145 if (WINDOW_WANTS_HEADER_LINE_P (w))
1146 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1147 }
1148 }
1149
1150
1151 /* Get the bounding box of the display area AREA of window W, without
1152 mode lines. AREA < 0 means the whole window, not including the
1153 left and right fringe of the window. Return in *TOP_LEFT_X
1154 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1155 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1156 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1157 box. */
1158
1159 static inline void
1160 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1161 int *bottom_right_x, int *bottom_right_y)
1162 {
1163 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1164 bottom_right_y);
1165 *bottom_right_x += *top_left_x;
1166 *bottom_right_y += *top_left_y;
1167 }
1168
1169
1170 \f
1171 /***********************************************************************
1172 Utilities
1173 ***********************************************************************/
1174
1175 /* Return the bottom y-position of the line the iterator IT is in.
1176 This can modify IT's settings. */
1177
1178 int
1179 line_bottom_y (struct it *it)
1180 {
1181 int line_height = it->max_ascent + it->max_descent;
1182 int line_top_y = it->current_y;
1183
1184 if (line_height == 0)
1185 {
1186 if (last_height)
1187 line_height = last_height;
1188 else if (IT_CHARPOS (*it) < ZV)
1189 {
1190 move_it_by_lines (it, 1);
1191 line_height = (it->max_ascent || it->max_descent
1192 ? it->max_ascent + it->max_descent
1193 : last_height);
1194 }
1195 else
1196 {
1197 struct glyph_row *row = it->glyph_row;
1198
1199 /* Use the default character height. */
1200 it->glyph_row = NULL;
1201 it->what = IT_CHARACTER;
1202 it->c = ' ';
1203 it->len = 1;
1204 PRODUCE_GLYPHS (it);
1205 line_height = it->ascent + it->descent;
1206 it->glyph_row = row;
1207 }
1208 }
1209
1210 return line_top_y + line_height;
1211 }
1212
1213 /* Subroutine of pos_visible_p below. Extracts a display string, if
1214 any, from the display spec given as its argument. */
1215 static Lisp_Object
1216 string_from_display_spec (Lisp_Object spec)
1217 {
1218 if (CONSP (spec))
1219 {
1220 while (CONSP (spec))
1221 {
1222 if (STRINGP (XCAR (spec)))
1223 return XCAR (spec);
1224 spec = XCDR (spec);
1225 }
1226 }
1227 else if (VECTORP (spec))
1228 {
1229 ptrdiff_t i;
1230
1231 for (i = 0; i < ASIZE (spec); i++)
1232 {
1233 if (STRINGP (AREF (spec, i)))
1234 return AREF (spec, i);
1235 }
1236 return Qnil;
1237 }
1238
1239 return spec;
1240 }
1241
1242 /* Return 1 if position CHARPOS is visible in window W.
1243 CHARPOS < 0 means return info about WINDOW_END position.
1244 If visible, set *X and *Y to pixel coordinates of top left corner.
1245 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1246 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1247
1248 int
1249 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1250 int *rtop, int *rbot, int *rowh, int *vpos)
1251 {
1252 struct it it;
1253 void *itdata = bidi_shelve_cache ();
1254 struct text_pos top;
1255 int visible_p = 0;
1256 struct buffer *old_buffer = NULL;
1257
1258 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1259 return visible_p;
1260
1261 if (XBUFFER (w->buffer) != current_buffer)
1262 {
1263 old_buffer = current_buffer;
1264 set_buffer_internal_1 (XBUFFER (w->buffer));
1265 }
1266
1267 SET_TEXT_POS_FROM_MARKER (top, w->start);
1268
1269 /* Compute exact mode line heights. */
1270 if (WINDOW_WANTS_MODELINE_P (w))
1271 current_mode_line_height
1272 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1273 BVAR (current_buffer, mode_line_format));
1274
1275 if (WINDOW_WANTS_HEADER_LINE_P (w))
1276 current_header_line_height
1277 = display_mode_line (w, HEADER_LINE_FACE_ID,
1278 BVAR (current_buffer, header_line_format));
1279
1280 start_display (&it, w, top);
1281 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1282 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1283
1284 if (charpos >= 0
1285 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1286 && IT_CHARPOS (it) >= charpos)
1287 /* When scanning backwards under bidi iteration, move_it_to
1288 stops at or _before_ CHARPOS, because it stops at or to
1289 the _right_ of the character at CHARPOS. */
1290 || (it.bidi_p && it.bidi_it.scan_dir == -1
1291 && IT_CHARPOS (it) <= charpos)))
1292 {
1293 /* We have reached CHARPOS, or passed it. How the call to
1294 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1295 or covered by a display property, move_it_to stops at the end
1296 of the invisible text, to the right of CHARPOS. (ii) If
1297 CHARPOS is in a display vector, move_it_to stops on its last
1298 glyph. */
1299 int top_x = it.current_x;
1300 int top_y = it.current_y;
1301 enum it_method it_method = it.method;
1302 /* Calling line_bottom_y may change it.method, it.position, etc. */
1303 int bottom_y = (last_height = 0, line_bottom_y (&it));
1304 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1305
1306 if (top_y < window_top_y)
1307 visible_p = bottom_y > window_top_y;
1308 else if (top_y < it.last_visible_y)
1309 visible_p = 1;
1310 if (visible_p)
1311 {
1312 if (it_method == GET_FROM_DISPLAY_VECTOR)
1313 {
1314 /* We stopped on the last glyph of a display vector.
1315 Try and recompute. Hack alert! */
1316 if (charpos < 2 || top.charpos >= charpos)
1317 top_x = it.glyph_row->x;
1318 else
1319 {
1320 struct it it2;
1321 start_display (&it2, w, top);
1322 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1323 get_next_display_element (&it2);
1324 PRODUCE_GLYPHS (&it2);
1325 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1326 || it2.current_x > it2.last_visible_x)
1327 top_x = it.glyph_row->x;
1328 else
1329 {
1330 top_x = it2.current_x;
1331 top_y = it2.current_y;
1332 }
1333 }
1334 }
1335 else if (IT_CHARPOS (it) != charpos)
1336 {
1337 Lisp_Object cpos = make_number (charpos);
1338 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1339 Lisp_Object string = string_from_display_spec (spec);
1340 int newline_in_string = 0;
1341
1342 if (STRINGP (string))
1343 {
1344 const char *s = SSDATA (string);
1345 const char *e = s + SBYTES (string);
1346 while (s < e)
1347 {
1348 if (*s++ == '\n')
1349 {
1350 newline_in_string = 1;
1351 break;
1352 }
1353 }
1354 }
1355 /* The tricky code below is needed because there's a
1356 discrepancy between move_it_to and how we set cursor
1357 when the display line ends in a newline from a
1358 display string. move_it_to will stop _after_ such
1359 display strings, whereas set_cursor_from_row
1360 conspires with cursor_row_p to place the cursor on
1361 the first glyph produced from the display string. */
1362
1363 /* We have overshoot PT because it is covered by a
1364 display property whose value is a string. If the
1365 string includes embedded newlines, we are also in the
1366 wrong display line. Backtrack to the correct line,
1367 where the display string begins. */
1368 if (newline_in_string)
1369 {
1370 Lisp_Object startpos, endpos;
1371 EMACS_INT start, end;
1372 struct it it3;
1373
1374 /* Find the first and the last buffer positions
1375 covered by the display string. */
1376 endpos =
1377 Fnext_single_char_property_change (cpos, Qdisplay,
1378 Qnil, Qnil);
1379 startpos =
1380 Fprevious_single_char_property_change (endpos, Qdisplay,
1381 Qnil, Qnil);
1382 start = XFASTINT (startpos);
1383 end = XFASTINT (endpos);
1384 /* Move to the last buffer position before the
1385 display property. */
1386 start_display (&it3, w, top);
1387 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1388 /* Move forward one more line if the position before
1389 the display string is a newline or if it is the
1390 rightmost character on a line that is
1391 continued or word-wrapped. */
1392 if (it3.method == GET_FROM_BUFFER
1393 && it3.c == '\n')
1394 move_it_by_lines (&it3, 1);
1395 else if (move_it_in_display_line_to (&it3, -1,
1396 it3.current_x
1397 + it3.pixel_width,
1398 MOVE_TO_X)
1399 == MOVE_LINE_CONTINUED)
1400 {
1401 move_it_by_lines (&it3, 1);
1402 /* When we are under word-wrap, the #$@%!
1403 move_it_by_lines moves 2 lines, so we need to
1404 fix that up. */
1405 if (it3.line_wrap == WORD_WRAP)
1406 move_it_by_lines (&it3, -1);
1407 }
1408
1409 /* Record the vertical coordinate of the display
1410 line where we wound up. */
1411 top_y = it3.current_y;
1412 if (it3.bidi_p)
1413 {
1414 /* When characters are reordered for display,
1415 the character displayed to the left of the
1416 display string could be _after_ the display
1417 property in the logical order. Use the
1418 smallest vertical position of these two. */
1419 start_display (&it3, w, top);
1420 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1421 if (it3.current_y < top_y)
1422 top_y = it3.current_y;
1423 }
1424 /* Move from the top of the window to the beginning
1425 of the display line where the display string
1426 begins. */
1427 start_display (&it3, w, top);
1428 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1429 /* Finally, advance the iterator until we hit the
1430 first display element whose character position is
1431 CHARPOS, or until the first newline from the
1432 display string, which signals the end of the
1433 display line. */
1434 while (get_next_display_element (&it3))
1435 {
1436 PRODUCE_GLYPHS (&it3);
1437 if (IT_CHARPOS (it3) == charpos
1438 || ITERATOR_AT_END_OF_LINE_P (&it3))
1439 break;
1440 set_iterator_to_next (&it3, 0);
1441 }
1442 top_x = it3.current_x - it3.pixel_width;
1443 /* Normally, we would exit the above loop because we
1444 found the display element whose character
1445 position is CHARPOS. For the contingency that we
1446 didn't, and stopped at the first newline from the
1447 display string, move back over the glyphs
1448 produced from the string, until we find the
1449 rightmost glyph not from the string. */
1450 if (IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1451 {
1452 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1453 + it3.glyph_row->used[TEXT_AREA];
1454
1455 while (EQ ((g - 1)->object, string))
1456 {
1457 --g;
1458 top_x -= g->pixel_width;
1459 }
1460 xassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1461 + it3.glyph_row->used[TEXT_AREA]);
1462 }
1463 }
1464 }
1465
1466 *x = top_x;
1467 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1468 *rtop = max (0, window_top_y - top_y);
1469 *rbot = max (0, bottom_y - it.last_visible_y);
1470 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1471 - max (top_y, window_top_y)));
1472 *vpos = it.vpos;
1473 }
1474 }
1475 else
1476 {
1477 /* We were asked to provide info about WINDOW_END. */
1478 struct it it2;
1479 void *it2data = NULL;
1480
1481 SAVE_IT (it2, it, it2data);
1482 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1483 move_it_by_lines (&it, 1);
1484 if (charpos < IT_CHARPOS (it)
1485 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1486 {
1487 visible_p = 1;
1488 RESTORE_IT (&it2, &it2, it2data);
1489 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1490 *x = it2.current_x;
1491 *y = it2.current_y + it2.max_ascent - it2.ascent;
1492 *rtop = max (0, -it2.current_y);
1493 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1494 - it.last_visible_y));
1495 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1496 it.last_visible_y)
1497 - max (it2.current_y,
1498 WINDOW_HEADER_LINE_HEIGHT (w))));
1499 *vpos = it2.vpos;
1500 }
1501 else
1502 bidi_unshelve_cache (it2data, 1);
1503 }
1504 bidi_unshelve_cache (itdata, 0);
1505
1506 if (old_buffer)
1507 set_buffer_internal_1 (old_buffer);
1508
1509 current_header_line_height = current_mode_line_height = -1;
1510
1511 if (visible_p && XFASTINT (w->hscroll) > 0)
1512 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1513
1514 #if 0
1515 /* Debugging code. */
1516 if (visible_p)
1517 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1518 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1519 else
1520 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1521 #endif
1522
1523 return visible_p;
1524 }
1525
1526
1527 /* Return the next character from STR. Return in *LEN the length of
1528 the character. This is like STRING_CHAR_AND_LENGTH but never
1529 returns an invalid character. If we find one, we return a `?', but
1530 with the length of the invalid character. */
1531
1532 static inline int
1533 string_char_and_length (const unsigned char *str, int *len)
1534 {
1535 int c;
1536
1537 c = STRING_CHAR_AND_LENGTH (str, *len);
1538 if (!CHAR_VALID_P (c))
1539 /* We may not change the length here because other places in Emacs
1540 don't use this function, i.e. they silently accept invalid
1541 characters. */
1542 c = '?';
1543
1544 return c;
1545 }
1546
1547
1548
1549 /* Given a position POS containing a valid character and byte position
1550 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1551
1552 static struct text_pos
1553 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1554 {
1555 xassert (STRINGP (string) && nchars >= 0);
1556
1557 if (STRING_MULTIBYTE (string))
1558 {
1559 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1560 int len;
1561
1562 while (nchars--)
1563 {
1564 string_char_and_length (p, &len);
1565 p += len;
1566 CHARPOS (pos) += 1;
1567 BYTEPOS (pos) += len;
1568 }
1569 }
1570 else
1571 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1572
1573 return pos;
1574 }
1575
1576
1577 /* Value is the text position, i.e. character and byte position,
1578 for character position CHARPOS in STRING. */
1579
1580 static inline struct text_pos
1581 string_pos (ptrdiff_t charpos, Lisp_Object string)
1582 {
1583 struct text_pos pos;
1584 xassert (STRINGP (string));
1585 xassert (charpos >= 0);
1586 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1587 return pos;
1588 }
1589
1590
1591 /* Value is a text position, i.e. character and byte position, for
1592 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1593 means recognize multibyte characters. */
1594
1595 static struct text_pos
1596 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1597 {
1598 struct text_pos pos;
1599
1600 xassert (s != NULL);
1601 xassert (charpos >= 0);
1602
1603 if (multibyte_p)
1604 {
1605 int len;
1606
1607 SET_TEXT_POS (pos, 0, 0);
1608 while (charpos--)
1609 {
1610 string_char_and_length ((const unsigned char *) s, &len);
1611 s += len;
1612 CHARPOS (pos) += 1;
1613 BYTEPOS (pos) += len;
1614 }
1615 }
1616 else
1617 SET_TEXT_POS (pos, charpos, charpos);
1618
1619 return pos;
1620 }
1621
1622
1623 /* Value is the number of characters in C string S. MULTIBYTE_P
1624 non-zero means recognize multibyte characters. */
1625
1626 static ptrdiff_t
1627 number_of_chars (const char *s, int multibyte_p)
1628 {
1629 ptrdiff_t nchars;
1630
1631 if (multibyte_p)
1632 {
1633 ptrdiff_t rest = strlen (s);
1634 int len;
1635 const unsigned char *p = (const unsigned char *) s;
1636
1637 for (nchars = 0; rest > 0; ++nchars)
1638 {
1639 string_char_and_length (p, &len);
1640 rest -= len, p += len;
1641 }
1642 }
1643 else
1644 nchars = strlen (s);
1645
1646 return nchars;
1647 }
1648
1649
1650 /* Compute byte position NEWPOS->bytepos corresponding to
1651 NEWPOS->charpos. POS is a known position in string STRING.
1652 NEWPOS->charpos must be >= POS.charpos. */
1653
1654 static void
1655 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1656 {
1657 xassert (STRINGP (string));
1658 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1659
1660 if (STRING_MULTIBYTE (string))
1661 *newpos = string_pos_nchars_ahead (pos, string,
1662 CHARPOS (*newpos) - CHARPOS (pos));
1663 else
1664 BYTEPOS (*newpos) = CHARPOS (*newpos);
1665 }
1666
1667 /* EXPORT:
1668 Return an estimation of the pixel height of mode or header lines on
1669 frame F. FACE_ID specifies what line's height to estimate. */
1670
1671 int
1672 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1673 {
1674 #ifdef HAVE_WINDOW_SYSTEM
1675 if (FRAME_WINDOW_P (f))
1676 {
1677 int height = FONT_HEIGHT (FRAME_FONT (f));
1678
1679 /* This function is called so early when Emacs starts that the face
1680 cache and mode line face are not yet initialized. */
1681 if (FRAME_FACE_CACHE (f))
1682 {
1683 struct face *face = FACE_FROM_ID (f, face_id);
1684 if (face)
1685 {
1686 if (face->font)
1687 height = FONT_HEIGHT (face->font);
1688 if (face->box_line_width > 0)
1689 height += 2 * face->box_line_width;
1690 }
1691 }
1692
1693 return height;
1694 }
1695 #endif
1696
1697 return 1;
1698 }
1699
1700 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1701 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1702 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1703 not force the value into range. */
1704
1705 void
1706 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1707 int *x, int *y, NativeRectangle *bounds, int noclip)
1708 {
1709
1710 #ifdef HAVE_WINDOW_SYSTEM
1711 if (FRAME_WINDOW_P (f))
1712 {
1713 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1714 even for negative values. */
1715 if (pix_x < 0)
1716 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1717 if (pix_y < 0)
1718 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1719
1720 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1721 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1722
1723 if (bounds)
1724 STORE_NATIVE_RECT (*bounds,
1725 FRAME_COL_TO_PIXEL_X (f, pix_x),
1726 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1727 FRAME_COLUMN_WIDTH (f) - 1,
1728 FRAME_LINE_HEIGHT (f) - 1);
1729
1730 if (!noclip)
1731 {
1732 if (pix_x < 0)
1733 pix_x = 0;
1734 else if (pix_x > FRAME_TOTAL_COLS (f))
1735 pix_x = FRAME_TOTAL_COLS (f);
1736
1737 if (pix_y < 0)
1738 pix_y = 0;
1739 else if (pix_y > FRAME_LINES (f))
1740 pix_y = FRAME_LINES (f);
1741 }
1742 }
1743 #endif
1744
1745 *x = pix_x;
1746 *y = pix_y;
1747 }
1748
1749
1750 /* Find the glyph under window-relative coordinates X/Y in window W.
1751 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1752 strings. Return in *HPOS and *VPOS the row and column number of
1753 the glyph found. Return in *AREA the glyph area containing X.
1754 Value is a pointer to the glyph found or null if X/Y is not on
1755 text, or we can't tell because W's current matrix is not up to
1756 date. */
1757
1758 static
1759 struct glyph *
1760 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1761 int *dx, int *dy, int *area)
1762 {
1763 struct glyph *glyph, *end;
1764 struct glyph_row *row = NULL;
1765 int x0, i;
1766
1767 /* Find row containing Y. Give up if some row is not enabled. */
1768 for (i = 0; i < w->current_matrix->nrows; ++i)
1769 {
1770 row = MATRIX_ROW (w->current_matrix, i);
1771 if (!row->enabled_p)
1772 return NULL;
1773 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1774 break;
1775 }
1776
1777 *vpos = i;
1778 *hpos = 0;
1779
1780 /* Give up if Y is not in the window. */
1781 if (i == w->current_matrix->nrows)
1782 return NULL;
1783
1784 /* Get the glyph area containing X. */
1785 if (w->pseudo_window_p)
1786 {
1787 *area = TEXT_AREA;
1788 x0 = 0;
1789 }
1790 else
1791 {
1792 if (x < window_box_left_offset (w, TEXT_AREA))
1793 {
1794 *area = LEFT_MARGIN_AREA;
1795 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1796 }
1797 else if (x < window_box_right_offset (w, TEXT_AREA))
1798 {
1799 *area = TEXT_AREA;
1800 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1801 }
1802 else
1803 {
1804 *area = RIGHT_MARGIN_AREA;
1805 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1806 }
1807 }
1808
1809 /* Find glyph containing X. */
1810 glyph = row->glyphs[*area];
1811 end = glyph + row->used[*area];
1812 x -= x0;
1813 while (glyph < end && x >= glyph->pixel_width)
1814 {
1815 x -= glyph->pixel_width;
1816 ++glyph;
1817 }
1818
1819 if (glyph == end)
1820 return NULL;
1821
1822 if (dx)
1823 {
1824 *dx = x;
1825 *dy = y - (row->y + row->ascent - glyph->ascent);
1826 }
1827
1828 *hpos = glyph - row->glyphs[*area];
1829 return glyph;
1830 }
1831
1832 /* Convert frame-relative x/y to coordinates relative to window W.
1833 Takes pseudo-windows into account. */
1834
1835 static void
1836 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1837 {
1838 if (w->pseudo_window_p)
1839 {
1840 /* A pseudo-window is always full-width, and starts at the
1841 left edge of the frame, plus a frame border. */
1842 struct frame *f = XFRAME (w->frame);
1843 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1844 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1845 }
1846 else
1847 {
1848 *x -= WINDOW_LEFT_EDGE_X (w);
1849 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1850 }
1851 }
1852
1853 #ifdef HAVE_WINDOW_SYSTEM
1854
1855 /* EXPORT:
1856 Return in RECTS[] at most N clipping rectangles for glyph string S.
1857 Return the number of stored rectangles. */
1858
1859 int
1860 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1861 {
1862 XRectangle r;
1863
1864 if (n <= 0)
1865 return 0;
1866
1867 if (s->row->full_width_p)
1868 {
1869 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1870 r.x = WINDOW_LEFT_EDGE_X (s->w);
1871 r.width = WINDOW_TOTAL_WIDTH (s->w);
1872
1873 /* Unless displaying a mode or menu bar line, which are always
1874 fully visible, clip to the visible part of the row. */
1875 if (s->w->pseudo_window_p)
1876 r.height = s->row->visible_height;
1877 else
1878 r.height = s->height;
1879 }
1880 else
1881 {
1882 /* This is a text line that may be partially visible. */
1883 r.x = window_box_left (s->w, s->area);
1884 r.width = window_box_width (s->w, s->area);
1885 r.height = s->row->visible_height;
1886 }
1887
1888 if (s->clip_head)
1889 if (r.x < s->clip_head->x)
1890 {
1891 if (r.width >= s->clip_head->x - r.x)
1892 r.width -= s->clip_head->x - r.x;
1893 else
1894 r.width = 0;
1895 r.x = s->clip_head->x;
1896 }
1897 if (s->clip_tail)
1898 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1899 {
1900 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1901 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1902 else
1903 r.width = 0;
1904 }
1905
1906 /* If S draws overlapping rows, it's sufficient to use the top and
1907 bottom of the window for clipping because this glyph string
1908 intentionally draws over other lines. */
1909 if (s->for_overlaps)
1910 {
1911 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1912 r.height = window_text_bottom_y (s->w) - r.y;
1913
1914 /* Alas, the above simple strategy does not work for the
1915 environments with anti-aliased text: if the same text is
1916 drawn onto the same place multiple times, it gets thicker.
1917 If the overlap we are processing is for the erased cursor, we
1918 take the intersection with the rectangle of the cursor. */
1919 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1920 {
1921 XRectangle rc, r_save = r;
1922
1923 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1924 rc.y = s->w->phys_cursor.y;
1925 rc.width = s->w->phys_cursor_width;
1926 rc.height = s->w->phys_cursor_height;
1927
1928 x_intersect_rectangles (&r_save, &rc, &r);
1929 }
1930 }
1931 else
1932 {
1933 /* Don't use S->y for clipping because it doesn't take partially
1934 visible lines into account. For example, it can be negative for
1935 partially visible lines at the top of a window. */
1936 if (!s->row->full_width_p
1937 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1938 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1939 else
1940 r.y = max (0, s->row->y);
1941 }
1942
1943 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1944
1945 /* If drawing the cursor, don't let glyph draw outside its
1946 advertised boundaries. Cleartype does this under some circumstances. */
1947 if (s->hl == DRAW_CURSOR)
1948 {
1949 struct glyph *glyph = s->first_glyph;
1950 int height, max_y;
1951
1952 if (s->x > r.x)
1953 {
1954 r.width -= s->x - r.x;
1955 r.x = s->x;
1956 }
1957 r.width = min (r.width, glyph->pixel_width);
1958
1959 /* If r.y is below window bottom, ensure that we still see a cursor. */
1960 height = min (glyph->ascent + glyph->descent,
1961 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
1962 max_y = window_text_bottom_y (s->w) - height;
1963 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
1964 if (s->ybase - glyph->ascent > max_y)
1965 {
1966 r.y = max_y;
1967 r.height = height;
1968 }
1969 else
1970 {
1971 /* Don't draw cursor glyph taller than our actual glyph. */
1972 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
1973 if (height < r.height)
1974 {
1975 max_y = r.y + r.height;
1976 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
1977 r.height = min (max_y - r.y, height);
1978 }
1979 }
1980 }
1981
1982 if (s->row->clip)
1983 {
1984 XRectangle r_save = r;
1985
1986 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
1987 r.width = 0;
1988 }
1989
1990 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
1991 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
1992 {
1993 #ifdef CONVERT_FROM_XRECT
1994 CONVERT_FROM_XRECT (r, *rects);
1995 #else
1996 *rects = r;
1997 #endif
1998 return 1;
1999 }
2000 else
2001 {
2002 /* If we are processing overlapping and allowed to return
2003 multiple clipping rectangles, we exclude the row of the glyph
2004 string from the clipping rectangle. This is to avoid drawing
2005 the same text on the environment with anti-aliasing. */
2006 #ifdef CONVERT_FROM_XRECT
2007 XRectangle rs[2];
2008 #else
2009 XRectangle *rs = rects;
2010 #endif
2011 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2012
2013 if (s->for_overlaps & OVERLAPS_PRED)
2014 {
2015 rs[i] = r;
2016 if (r.y + r.height > row_y)
2017 {
2018 if (r.y < row_y)
2019 rs[i].height = row_y - r.y;
2020 else
2021 rs[i].height = 0;
2022 }
2023 i++;
2024 }
2025 if (s->for_overlaps & OVERLAPS_SUCC)
2026 {
2027 rs[i] = r;
2028 if (r.y < row_y + s->row->visible_height)
2029 {
2030 if (r.y + r.height > row_y + s->row->visible_height)
2031 {
2032 rs[i].y = row_y + s->row->visible_height;
2033 rs[i].height = r.y + r.height - rs[i].y;
2034 }
2035 else
2036 rs[i].height = 0;
2037 }
2038 i++;
2039 }
2040
2041 n = i;
2042 #ifdef CONVERT_FROM_XRECT
2043 for (i = 0; i < n; i++)
2044 CONVERT_FROM_XRECT (rs[i], rects[i]);
2045 #endif
2046 return n;
2047 }
2048 }
2049
2050 /* EXPORT:
2051 Return in *NR the clipping rectangle for glyph string S. */
2052
2053 void
2054 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2055 {
2056 get_glyph_string_clip_rects (s, nr, 1);
2057 }
2058
2059
2060 /* EXPORT:
2061 Return the position and height of the phys cursor in window W.
2062 Set w->phys_cursor_width to width of phys cursor.
2063 */
2064
2065 void
2066 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2067 struct glyph *glyph, int *xp, int *yp, int *heightp)
2068 {
2069 struct frame *f = XFRAME (WINDOW_FRAME (w));
2070 int x, y, wd, h, h0, y0;
2071
2072 /* Compute the width of the rectangle to draw. If on a stretch
2073 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2074 rectangle as wide as the glyph, but use a canonical character
2075 width instead. */
2076 wd = glyph->pixel_width - 1;
2077 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2078 wd++; /* Why? */
2079 #endif
2080
2081 x = w->phys_cursor.x;
2082 if (x < 0)
2083 {
2084 wd += x;
2085 x = 0;
2086 }
2087
2088 if (glyph->type == STRETCH_GLYPH
2089 && !x_stretch_cursor_p)
2090 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2091 w->phys_cursor_width = wd;
2092
2093 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2094
2095 /* If y is below window bottom, ensure that we still see a cursor. */
2096 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2097
2098 h = max (h0, glyph->ascent + glyph->descent);
2099 h0 = min (h0, glyph->ascent + glyph->descent);
2100
2101 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2102 if (y < y0)
2103 {
2104 h = max (h - (y0 - y) + 1, h0);
2105 y = y0 - 1;
2106 }
2107 else
2108 {
2109 y0 = window_text_bottom_y (w) - h0;
2110 if (y > y0)
2111 {
2112 h += y - y0;
2113 y = y0;
2114 }
2115 }
2116
2117 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2118 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2119 *heightp = h;
2120 }
2121
2122 /*
2123 * Remember which glyph the mouse is over.
2124 */
2125
2126 void
2127 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2128 {
2129 Lisp_Object window;
2130 struct window *w;
2131 struct glyph_row *r, *gr, *end_row;
2132 enum window_part part;
2133 enum glyph_row_area area;
2134 int x, y, width, height;
2135
2136 /* Try to determine frame pixel position and size of the glyph under
2137 frame pixel coordinates X/Y on frame F. */
2138
2139 if (!f->glyphs_initialized_p
2140 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2141 NILP (window)))
2142 {
2143 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2144 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2145 goto virtual_glyph;
2146 }
2147
2148 w = XWINDOW (window);
2149 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2150 height = WINDOW_FRAME_LINE_HEIGHT (w);
2151
2152 x = window_relative_x_coord (w, part, gx);
2153 y = gy - WINDOW_TOP_EDGE_Y (w);
2154
2155 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2156 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2157
2158 if (w->pseudo_window_p)
2159 {
2160 area = TEXT_AREA;
2161 part = ON_MODE_LINE; /* Don't adjust margin. */
2162 goto text_glyph;
2163 }
2164
2165 switch (part)
2166 {
2167 case ON_LEFT_MARGIN:
2168 area = LEFT_MARGIN_AREA;
2169 goto text_glyph;
2170
2171 case ON_RIGHT_MARGIN:
2172 area = RIGHT_MARGIN_AREA;
2173 goto text_glyph;
2174
2175 case ON_HEADER_LINE:
2176 case ON_MODE_LINE:
2177 gr = (part == ON_HEADER_LINE
2178 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2179 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2180 gy = gr->y;
2181 area = TEXT_AREA;
2182 goto text_glyph_row_found;
2183
2184 case ON_TEXT:
2185 area = TEXT_AREA;
2186
2187 text_glyph:
2188 gr = 0; gy = 0;
2189 for (; r <= end_row && r->enabled_p; ++r)
2190 if (r->y + r->height > y)
2191 {
2192 gr = r; gy = r->y;
2193 break;
2194 }
2195
2196 text_glyph_row_found:
2197 if (gr && gy <= y)
2198 {
2199 struct glyph *g = gr->glyphs[area];
2200 struct glyph *end = g + gr->used[area];
2201
2202 height = gr->height;
2203 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2204 if (gx + g->pixel_width > x)
2205 break;
2206
2207 if (g < end)
2208 {
2209 if (g->type == IMAGE_GLYPH)
2210 {
2211 /* Don't remember when mouse is over image, as
2212 image may have hot-spots. */
2213 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2214 return;
2215 }
2216 width = g->pixel_width;
2217 }
2218 else
2219 {
2220 /* Use nominal char spacing at end of line. */
2221 x -= gx;
2222 gx += (x / width) * width;
2223 }
2224
2225 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2226 gx += window_box_left_offset (w, area);
2227 }
2228 else
2229 {
2230 /* Use nominal line height at end of window. */
2231 gx = (x / width) * width;
2232 y -= gy;
2233 gy += (y / height) * height;
2234 }
2235 break;
2236
2237 case ON_LEFT_FRINGE:
2238 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2239 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2240 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2241 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2242 goto row_glyph;
2243
2244 case ON_RIGHT_FRINGE:
2245 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2246 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2247 : window_box_right_offset (w, TEXT_AREA));
2248 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2249 goto row_glyph;
2250
2251 case ON_SCROLL_BAR:
2252 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2253 ? 0
2254 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2255 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2256 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2257 : 0)));
2258 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2259
2260 row_glyph:
2261 gr = 0, gy = 0;
2262 for (; r <= end_row && r->enabled_p; ++r)
2263 if (r->y + r->height > y)
2264 {
2265 gr = r; gy = r->y;
2266 break;
2267 }
2268
2269 if (gr && gy <= y)
2270 height = gr->height;
2271 else
2272 {
2273 /* Use nominal line height at end of window. */
2274 y -= gy;
2275 gy += (y / height) * height;
2276 }
2277 break;
2278
2279 default:
2280 ;
2281 virtual_glyph:
2282 /* If there is no glyph under the mouse, then we divide the screen
2283 into a grid of the smallest glyph in the frame, and use that
2284 as our "glyph". */
2285
2286 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2287 round down even for negative values. */
2288 if (gx < 0)
2289 gx -= width - 1;
2290 if (gy < 0)
2291 gy -= height - 1;
2292
2293 gx = (gx / width) * width;
2294 gy = (gy / height) * height;
2295
2296 goto store_rect;
2297 }
2298
2299 gx += WINDOW_LEFT_EDGE_X (w);
2300 gy += WINDOW_TOP_EDGE_Y (w);
2301
2302 store_rect:
2303 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2304
2305 /* Visible feedback for debugging. */
2306 #if 0
2307 #if HAVE_X_WINDOWS
2308 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2309 f->output_data.x->normal_gc,
2310 gx, gy, width, height);
2311 #endif
2312 #endif
2313 }
2314
2315
2316 #endif /* HAVE_WINDOW_SYSTEM */
2317
2318 \f
2319 /***********************************************************************
2320 Lisp form evaluation
2321 ***********************************************************************/
2322
2323 /* Error handler for safe_eval and safe_call. */
2324
2325 static Lisp_Object
2326 safe_eval_handler (Lisp_Object arg)
2327 {
2328 add_to_log ("Error during redisplay: %S", arg, Qnil);
2329 return Qnil;
2330 }
2331
2332
2333 /* Evaluate SEXPR and return the result, or nil if something went
2334 wrong. Prevent redisplay during the evaluation. */
2335
2336 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2337 Return the result, or nil if something went wrong. Prevent
2338 redisplay during the evaluation. */
2339
2340 Lisp_Object
2341 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2342 {
2343 Lisp_Object val;
2344
2345 if (inhibit_eval_during_redisplay)
2346 val = Qnil;
2347 else
2348 {
2349 ptrdiff_t count = SPECPDL_INDEX ();
2350 struct gcpro gcpro1;
2351
2352 GCPRO1 (args[0]);
2353 gcpro1.nvars = nargs;
2354 specbind (Qinhibit_redisplay, Qt);
2355 /* Use Qt to ensure debugger does not run,
2356 so there is no possibility of wanting to redisplay. */
2357 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2358 safe_eval_handler);
2359 UNGCPRO;
2360 val = unbind_to (count, val);
2361 }
2362
2363 return val;
2364 }
2365
2366
2367 /* Call function FN with one argument ARG.
2368 Return the result, or nil if something went wrong. */
2369
2370 Lisp_Object
2371 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2372 {
2373 Lisp_Object args[2];
2374 args[0] = fn;
2375 args[1] = arg;
2376 return safe_call (2, args);
2377 }
2378
2379 static Lisp_Object Qeval;
2380
2381 Lisp_Object
2382 safe_eval (Lisp_Object sexpr)
2383 {
2384 return safe_call1 (Qeval, sexpr);
2385 }
2386
2387 /* Call function FN with one argument ARG.
2388 Return the result, or nil if something went wrong. */
2389
2390 Lisp_Object
2391 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2392 {
2393 Lisp_Object args[3];
2394 args[0] = fn;
2395 args[1] = arg1;
2396 args[2] = arg2;
2397 return safe_call (3, args);
2398 }
2399
2400
2401 \f
2402 /***********************************************************************
2403 Debugging
2404 ***********************************************************************/
2405
2406 #if 0
2407
2408 /* Define CHECK_IT to perform sanity checks on iterators.
2409 This is for debugging. It is too slow to do unconditionally. */
2410
2411 static void
2412 check_it (struct it *it)
2413 {
2414 if (it->method == GET_FROM_STRING)
2415 {
2416 xassert (STRINGP (it->string));
2417 xassert (IT_STRING_CHARPOS (*it) >= 0);
2418 }
2419 else
2420 {
2421 xassert (IT_STRING_CHARPOS (*it) < 0);
2422 if (it->method == GET_FROM_BUFFER)
2423 {
2424 /* Check that character and byte positions agree. */
2425 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2426 }
2427 }
2428
2429 if (it->dpvec)
2430 xassert (it->current.dpvec_index >= 0);
2431 else
2432 xassert (it->current.dpvec_index < 0);
2433 }
2434
2435 #define CHECK_IT(IT) check_it ((IT))
2436
2437 #else /* not 0 */
2438
2439 #define CHECK_IT(IT) (void) 0
2440
2441 #endif /* not 0 */
2442
2443
2444 #if GLYPH_DEBUG && XASSERTS
2445
2446 /* Check that the window end of window W is what we expect it
2447 to be---the last row in the current matrix displaying text. */
2448
2449 static void
2450 check_window_end (struct window *w)
2451 {
2452 if (!MINI_WINDOW_P (w)
2453 && !NILP (w->window_end_valid))
2454 {
2455 struct glyph_row *row;
2456 xassert ((row = MATRIX_ROW (w->current_matrix,
2457 XFASTINT (w->window_end_vpos)),
2458 !row->enabled_p
2459 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2460 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2461 }
2462 }
2463
2464 #define CHECK_WINDOW_END(W) check_window_end ((W))
2465
2466 #else
2467
2468 #define CHECK_WINDOW_END(W) (void) 0
2469
2470 #endif
2471
2472
2473 \f
2474 /***********************************************************************
2475 Iterator initialization
2476 ***********************************************************************/
2477
2478 /* Initialize IT for displaying current_buffer in window W, starting
2479 at character position CHARPOS. CHARPOS < 0 means that no buffer
2480 position is specified which is useful when the iterator is assigned
2481 a position later. BYTEPOS is the byte position corresponding to
2482 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2483
2484 If ROW is not null, calls to produce_glyphs with IT as parameter
2485 will produce glyphs in that row.
2486
2487 BASE_FACE_ID is the id of a base face to use. It must be one of
2488 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2489 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2490 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2491
2492 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2493 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2494 will be initialized to use the corresponding mode line glyph row of
2495 the desired matrix of W. */
2496
2497 void
2498 init_iterator (struct it *it, struct window *w,
2499 ptrdiff_t charpos, ptrdiff_t bytepos,
2500 struct glyph_row *row, enum face_id base_face_id)
2501 {
2502 int highlight_region_p;
2503 enum face_id remapped_base_face_id = base_face_id;
2504
2505 /* Some precondition checks. */
2506 xassert (w != NULL && it != NULL);
2507 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2508 && charpos <= ZV));
2509
2510 /* If face attributes have been changed since the last redisplay,
2511 free realized faces now because they depend on face definitions
2512 that might have changed. Don't free faces while there might be
2513 desired matrices pending which reference these faces. */
2514 if (face_change_count && !inhibit_free_realized_faces)
2515 {
2516 face_change_count = 0;
2517 free_all_realized_faces (Qnil);
2518 }
2519
2520 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2521 if (! NILP (Vface_remapping_alist))
2522 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2523
2524 /* Use one of the mode line rows of W's desired matrix if
2525 appropriate. */
2526 if (row == NULL)
2527 {
2528 if (base_face_id == MODE_LINE_FACE_ID
2529 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2530 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2531 else if (base_face_id == HEADER_LINE_FACE_ID)
2532 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2533 }
2534
2535 /* Clear IT. */
2536 memset (it, 0, sizeof *it);
2537 it->current.overlay_string_index = -1;
2538 it->current.dpvec_index = -1;
2539 it->base_face_id = remapped_base_face_id;
2540 it->string = Qnil;
2541 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2542 it->paragraph_embedding = L2R;
2543 it->bidi_it.string.lstring = Qnil;
2544 it->bidi_it.string.s = NULL;
2545 it->bidi_it.string.bufpos = 0;
2546
2547 /* The window in which we iterate over current_buffer: */
2548 XSETWINDOW (it->window, w);
2549 it->w = w;
2550 it->f = XFRAME (w->frame);
2551
2552 it->cmp_it.id = -1;
2553
2554 /* Extra space between lines (on window systems only). */
2555 if (base_face_id == DEFAULT_FACE_ID
2556 && FRAME_WINDOW_P (it->f))
2557 {
2558 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2559 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2560 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2561 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2562 * FRAME_LINE_HEIGHT (it->f));
2563 else if (it->f->extra_line_spacing > 0)
2564 it->extra_line_spacing = it->f->extra_line_spacing;
2565 it->max_extra_line_spacing = 0;
2566 }
2567
2568 /* If realized faces have been removed, e.g. because of face
2569 attribute changes of named faces, recompute them. When running
2570 in batch mode, the face cache of the initial frame is null. If
2571 we happen to get called, make a dummy face cache. */
2572 if (FRAME_FACE_CACHE (it->f) == NULL)
2573 init_frame_faces (it->f);
2574 if (FRAME_FACE_CACHE (it->f)->used == 0)
2575 recompute_basic_faces (it->f);
2576
2577 /* Current value of the `slice', `space-width', and 'height' properties. */
2578 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2579 it->space_width = Qnil;
2580 it->font_height = Qnil;
2581 it->override_ascent = -1;
2582
2583 /* Are control characters displayed as `^C'? */
2584 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2585
2586 /* -1 means everything between a CR and the following line end
2587 is invisible. >0 means lines indented more than this value are
2588 invisible. */
2589 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2590 ? clip_to_bounds (-1, XINT (BVAR (current_buffer,
2591 selective_display)),
2592 PTRDIFF_MAX)
2593 : (!NILP (BVAR (current_buffer, selective_display))
2594 ? -1 : 0));
2595 it->selective_display_ellipsis_p
2596 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2597
2598 /* Display table to use. */
2599 it->dp = window_display_table (w);
2600
2601 /* Are multibyte characters enabled in current_buffer? */
2602 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2603
2604 /* Non-zero if we should highlight the region. */
2605 highlight_region_p
2606 = (!NILP (Vtransient_mark_mode)
2607 && !NILP (BVAR (current_buffer, mark_active))
2608 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2609
2610 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2611 start and end of a visible region in window IT->w. Set both to
2612 -1 to indicate no region. */
2613 if (highlight_region_p
2614 /* Maybe highlight only in selected window. */
2615 && (/* Either show region everywhere. */
2616 highlight_nonselected_windows
2617 /* Or show region in the selected window. */
2618 || w == XWINDOW (selected_window)
2619 /* Or show the region if we are in the mini-buffer and W is
2620 the window the mini-buffer refers to. */
2621 || (MINI_WINDOW_P (XWINDOW (selected_window))
2622 && WINDOWP (minibuf_selected_window)
2623 && w == XWINDOW (minibuf_selected_window))))
2624 {
2625 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
2626 it->region_beg_charpos = min (PT, markpos);
2627 it->region_end_charpos = max (PT, markpos);
2628 }
2629 else
2630 it->region_beg_charpos = it->region_end_charpos = -1;
2631
2632 /* Get the position at which the redisplay_end_trigger hook should
2633 be run, if it is to be run at all. */
2634 if (MARKERP (w->redisplay_end_trigger)
2635 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2636 it->redisplay_end_trigger_charpos
2637 = marker_position (w->redisplay_end_trigger);
2638 else if (INTEGERP (w->redisplay_end_trigger))
2639 it->redisplay_end_trigger_charpos =
2640 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2641
2642 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2643
2644 /* Are lines in the display truncated? */
2645 if (base_face_id != DEFAULT_FACE_ID
2646 || XINT (it->w->hscroll)
2647 || (! WINDOW_FULL_WIDTH_P (it->w)
2648 && ((!NILP (Vtruncate_partial_width_windows)
2649 && !INTEGERP (Vtruncate_partial_width_windows))
2650 || (INTEGERP (Vtruncate_partial_width_windows)
2651 && (WINDOW_TOTAL_COLS (it->w)
2652 < XINT (Vtruncate_partial_width_windows))))))
2653 it->line_wrap = TRUNCATE;
2654 else if (NILP (BVAR (current_buffer, truncate_lines)))
2655 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2656 ? WINDOW_WRAP : WORD_WRAP;
2657 else
2658 it->line_wrap = TRUNCATE;
2659
2660 /* Get dimensions of truncation and continuation glyphs. These are
2661 displayed as fringe bitmaps under X, so we don't need them for such
2662 frames. */
2663 if (!FRAME_WINDOW_P (it->f))
2664 {
2665 if (it->line_wrap == TRUNCATE)
2666 {
2667 /* We will need the truncation glyph. */
2668 xassert (it->glyph_row == NULL);
2669 produce_special_glyphs (it, IT_TRUNCATION);
2670 it->truncation_pixel_width = it->pixel_width;
2671 }
2672 else
2673 {
2674 /* We will need the continuation glyph. */
2675 xassert (it->glyph_row == NULL);
2676 produce_special_glyphs (it, IT_CONTINUATION);
2677 it->continuation_pixel_width = it->pixel_width;
2678 }
2679
2680 /* Reset these values to zero because the produce_special_glyphs
2681 above has changed them. */
2682 it->pixel_width = it->ascent = it->descent = 0;
2683 it->phys_ascent = it->phys_descent = 0;
2684 }
2685
2686 /* Set this after getting the dimensions of truncation and
2687 continuation glyphs, so that we don't produce glyphs when calling
2688 produce_special_glyphs, above. */
2689 it->glyph_row = row;
2690 it->area = TEXT_AREA;
2691
2692 /* Forget any previous info about this row being reversed. */
2693 if (it->glyph_row)
2694 it->glyph_row->reversed_p = 0;
2695
2696 /* Get the dimensions of the display area. The display area
2697 consists of the visible window area plus a horizontally scrolled
2698 part to the left of the window. All x-values are relative to the
2699 start of this total display area. */
2700 if (base_face_id != DEFAULT_FACE_ID)
2701 {
2702 /* Mode lines, menu bar in terminal frames. */
2703 it->first_visible_x = 0;
2704 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2705 }
2706 else
2707 {
2708 it->first_visible_x
2709 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2710 it->last_visible_x = (it->first_visible_x
2711 + window_box_width (w, TEXT_AREA));
2712
2713 /* If we truncate lines, leave room for the truncator glyph(s) at
2714 the right margin. Otherwise, leave room for the continuation
2715 glyph(s). Truncation and continuation glyphs are not inserted
2716 for window-based redisplay. */
2717 if (!FRAME_WINDOW_P (it->f))
2718 {
2719 if (it->line_wrap == TRUNCATE)
2720 it->last_visible_x -= it->truncation_pixel_width;
2721 else
2722 it->last_visible_x -= it->continuation_pixel_width;
2723 }
2724
2725 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2726 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2727 }
2728
2729 /* Leave room for a border glyph. */
2730 if (!FRAME_WINDOW_P (it->f)
2731 && !WINDOW_RIGHTMOST_P (it->w))
2732 it->last_visible_x -= 1;
2733
2734 it->last_visible_y = window_text_bottom_y (w);
2735
2736 /* For mode lines and alike, arrange for the first glyph having a
2737 left box line if the face specifies a box. */
2738 if (base_face_id != DEFAULT_FACE_ID)
2739 {
2740 struct face *face;
2741
2742 it->face_id = remapped_base_face_id;
2743
2744 /* If we have a boxed mode line, make the first character appear
2745 with a left box line. */
2746 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2747 if (face->box != FACE_NO_BOX)
2748 it->start_of_box_run_p = 1;
2749 }
2750
2751 /* If a buffer position was specified, set the iterator there,
2752 getting overlays and face properties from that position. */
2753 if (charpos >= BUF_BEG (current_buffer))
2754 {
2755 it->end_charpos = ZV;
2756 it->face_id = -1;
2757 IT_CHARPOS (*it) = charpos;
2758
2759 /* Compute byte position if not specified. */
2760 if (bytepos < charpos)
2761 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2762 else
2763 IT_BYTEPOS (*it) = bytepos;
2764
2765 it->start = it->current;
2766 /* Do we need to reorder bidirectional text? Not if this is a
2767 unibyte buffer: by definition, none of the single-byte
2768 characters are strong R2L, so no reordering is needed. And
2769 bidi.c doesn't support unibyte buffers anyway. Also, don't
2770 reorder while we are loading loadup.el, since the tables of
2771 character properties needed for reordering are not yet
2772 available. */
2773 it->bidi_p =
2774 NILP (Vpurify_flag)
2775 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2776 && it->multibyte_p;
2777
2778 /* If we are to reorder bidirectional text, init the bidi
2779 iterator. */
2780 if (it->bidi_p)
2781 {
2782 /* Note the paragraph direction that this buffer wants to
2783 use. */
2784 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2785 Qleft_to_right))
2786 it->paragraph_embedding = L2R;
2787 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2788 Qright_to_left))
2789 it->paragraph_embedding = R2L;
2790 else
2791 it->paragraph_embedding = NEUTRAL_DIR;
2792 bidi_unshelve_cache (NULL, 0);
2793 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2794 &it->bidi_it);
2795 }
2796
2797 /* Compute faces etc. */
2798 reseat (it, it->current.pos, 1);
2799 }
2800
2801 CHECK_IT (it);
2802 }
2803
2804
2805 /* Initialize IT for the display of window W with window start POS. */
2806
2807 void
2808 start_display (struct it *it, struct window *w, struct text_pos pos)
2809 {
2810 struct glyph_row *row;
2811 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2812
2813 row = w->desired_matrix->rows + first_vpos;
2814 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2815 it->first_vpos = first_vpos;
2816
2817 /* Don't reseat to previous visible line start if current start
2818 position is in a string or image. */
2819 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2820 {
2821 int start_at_line_beg_p;
2822 int first_y = it->current_y;
2823
2824 /* If window start is not at a line start, skip forward to POS to
2825 get the correct continuation lines width. */
2826 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2827 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2828 if (!start_at_line_beg_p)
2829 {
2830 int new_x;
2831
2832 reseat_at_previous_visible_line_start (it);
2833 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2834
2835 new_x = it->current_x + it->pixel_width;
2836
2837 /* If lines are continued, this line may end in the middle
2838 of a multi-glyph character (e.g. a control character
2839 displayed as \003, or in the middle of an overlay
2840 string). In this case move_it_to above will not have
2841 taken us to the start of the continuation line but to the
2842 end of the continued line. */
2843 if (it->current_x > 0
2844 && it->line_wrap != TRUNCATE /* Lines are continued. */
2845 && (/* And glyph doesn't fit on the line. */
2846 new_x > it->last_visible_x
2847 /* Or it fits exactly and we're on a window
2848 system frame. */
2849 || (new_x == it->last_visible_x
2850 && FRAME_WINDOW_P (it->f))))
2851 {
2852 if ((it->current.dpvec_index >= 0
2853 || it->current.overlay_string_index >= 0)
2854 /* If we are on a newline from a display vector or
2855 overlay string, then we are already at the end of
2856 a screen line; no need to go to the next line in
2857 that case, as this line is not really continued.
2858 (If we do go to the next line, C-e will not DTRT.) */
2859 && it->c != '\n')
2860 {
2861 set_iterator_to_next (it, 1);
2862 move_it_in_display_line_to (it, -1, -1, 0);
2863 }
2864
2865 it->continuation_lines_width += it->current_x;
2866 }
2867 /* If the character at POS is displayed via a display
2868 vector, move_it_to above stops at the final glyph of
2869 IT->dpvec. To make the caller redisplay that character
2870 again (a.k.a. start at POS), we need to reset the
2871 dpvec_index to the beginning of IT->dpvec. */
2872 else if (it->current.dpvec_index >= 0)
2873 it->current.dpvec_index = 0;
2874
2875 /* We're starting a new display line, not affected by the
2876 height of the continued line, so clear the appropriate
2877 fields in the iterator structure. */
2878 it->max_ascent = it->max_descent = 0;
2879 it->max_phys_ascent = it->max_phys_descent = 0;
2880
2881 it->current_y = first_y;
2882 it->vpos = 0;
2883 it->current_x = it->hpos = 0;
2884 }
2885 }
2886 }
2887
2888
2889 /* Return 1 if POS is a position in ellipses displayed for invisible
2890 text. W is the window we display, for text property lookup. */
2891
2892 static int
2893 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2894 {
2895 Lisp_Object prop, window;
2896 int ellipses_p = 0;
2897 ptrdiff_t charpos = CHARPOS (pos->pos);
2898
2899 /* If POS specifies a position in a display vector, this might
2900 be for an ellipsis displayed for invisible text. We won't
2901 get the iterator set up for delivering that ellipsis unless
2902 we make sure that it gets aware of the invisible text. */
2903 if (pos->dpvec_index >= 0
2904 && pos->overlay_string_index < 0
2905 && CHARPOS (pos->string_pos) < 0
2906 && charpos > BEGV
2907 && (XSETWINDOW (window, w),
2908 prop = Fget_char_property (make_number (charpos),
2909 Qinvisible, window),
2910 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2911 {
2912 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2913 window);
2914 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2915 }
2916
2917 return ellipses_p;
2918 }
2919
2920
2921 /* Initialize IT for stepping through current_buffer in window W,
2922 starting at position POS that includes overlay string and display
2923 vector/ control character translation position information. Value
2924 is zero if there are overlay strings with newlines at POS. */
2925
2926 static int
2927 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2928 {
2929 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2930 int i, overlay_strings_with_newlines = 0;
2931
2932 /* If POS specifies a position in a display vector, this might
2933 be for an ellipsis displayed for invisible text. We won't
2934 get the iterator set up for delivering that ellipsis unless
2935 we make sure that it gets aware of the invisible text. */
2936 if (in_ellipses_for_invisible_text_p (pos, w))
2937 {
2938 --charpos;
2939 bytepos = 0;
2940 }
2941
2942 /* Keep in mind: the call to reseat in init_iterator skips invisible
2943 text, so we might end up at a position different from POS. This
2944 is only a problem when POS is a row start after a newline and an
2945 overlay starts there with an after-string, and the overlay has an
2946 invisible property. Since we don't skip invisible text in
2947 display_line and elsewhere immediately after consuming the
2948 newline before the row start, such a POS will not be in a string,
2949 but the call to init_iterator below will move us to the
2950 after-string. */
2951 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
2952
2953 /* This only scans the current chunk -- it should scan all chunks.
2954 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
2955 to 16 in 22.1 to make this a lesser problem. */
2956 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
2957 {
2958 const char *s = SSDATA (it->overlay_strings[i]);
2959 const char *e = s + SBYTES (it->overlay_strings[i]);
2960
2961 while (s < e && *s != '\n')
2962 ++s;
2963
2964 if (s < e)
2965 {
2966 overlay_strings_with_newlines = 1;
2967 break;
2968 }
2969 }
2970
2971 /* If position is within an overlay string, set up IT to the right
2972 overlay string. */
2973 if (pos->overlay_string_index >= 0)
2974 {
2975 int relative_index;
2976
2977 /* If the first overlay string happens to have a `display'
2978 property for an image, the iterator will be set up for that
2979 image, and we have to undo that setup first before we can
2980 correct the overlay string index. */
2981 if (it->method == GET_FROM_IMAGE)
2982 pop_it (it);
2983
2984 /* We already have the first chunk of overlay strings in
2985 IT->overlay_strings. Load more until the one for
2986 pos->overlay_string_index is in IT->overlay_strings. */
2987 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
2988 {
2989 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
2990 it->current.overlay_string_index = 0;
2991 while (n--)
2992 {
2993 load_overlay_strings (it, 0);
2994 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
2995 }
2996 }
2997
2998 it->current.overlay_string_index = pos->overlay_string_index;
2999 relative_index = (it->current.overlay_string_index
3000 % OVERLAY_STRING_CHUNK_SIZE);
3001 it->string = it->overlay_strings[relative_index];
3002 xassert (STRINGP (it->string));
3003 it->current.string_pos = pos->string_pos;
3004 it->method = GET_FROM_STRING;
3005 }
3006
3007 if (CHARPOS (pos->string_pos) >= 0)
3008 {
3009 /* Recorded position is not in an overlay string, but in another
3010 string. This can only be a string from a `display' property.
3011 IT should already be filled with that string. */
3012 it->current.string_pos = pos->string_pos;
3013 xassert (STRINGP (it->string));
3014 }
3015
3016 /* Restore position in display vector translations, control
3017 character translations or ellipses. */
3018 if (pos->dpvec_index >= 0)
3019 {
3020 if (it->dpvec == NULL)
3021 get_next_display_element (it);
3022 xassert (it->dpvec && it->current.dpvec_index == 0);
3023 it->current.dpvec_index = pos->dpvec_index;
3024 }
3025
3026 CHECK_IT (it);
3027 return !overlay_strings_with_newlines;
3028 }
3029
3030
3031 /* Initialize IT for stepping through current_buffer in window W
3032 starting at ROW->start. */
3033
3034 static void
3035 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3036 {
3037 init_from_display_pos (it, w, &row->start);
3038 it->start = row->start;
3039 it->continuation_lines_width = row->continuation_lines_width;
3040 CHECK_IT (it);
3041 }
3042
3043
3044 /* Initialize IT for stepping through current_buffer in window W
3045 starting in the line following ROW, i.e. starting at ROW->end.
3046 Value is zero if there are overlay strings with newlines at ROW's
3047 end position. */
3048
3049 static int
3050 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3051 {
3052 int success = 0;
3053
3054 if (init_from_display_pos (it, w, &row->end))
3055 {
3056 if (row->continued_p)
3057 it->continuation_lines_width
3058 = row->continuation_lines_width + row->pixel_width;
3059 CHECK_IT (it);
3060 success = 1;
3061 }
3062
3063 return success;
3064 }
3065
3066
3067
3068 \f
3069 /***********************************************************************
3070 Text properties
3071 ***********************************************************************/
3072
3073 /* Called when IT reaches IT->stop_charpos. Handle text property and
3074 overlay changes. Set IT->stop_charpos to the next position where
3075 to stop. */
3076
3077 static void
3078 handle_stop (struct it *it)
3079 {
3080 enum prop_handled handled;
3081 int handle_overlay_change_p;
3082 struct props *p;
3083
3084 it->dpvec = NULL;
3085 it->current.dpvec_index = -1;
3086 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3087 it->ignore_overlay_strings_at_pos_p = 0;
3088 it->ellipsis_p = 0;
3089
3090 /* Use face of preceding text for ellipsis (if invisible) */
3091 if (it->selective_display_ellipsis_p)
3092 it->saved_face_id = it->face_id;
3093
3094 do
3095 {
3096 handled = HANDLED_NORMALLY;
3097
3098 /* Call text property handlers. */
3099 for (p = it_props; p->handler; ++p)
3100 {
3101 handled = p->handler (it);
3102
3103 if (handled == HANDLED_RECOMPUTE_PROPS)
3104 break;
3105 else if (handled == HANDLED_RETURN)
3106 {
3107 /* We still want to show before and after strings from
3108 overlays even if the actual buffer text is replaced. */
3109 if (!handle_overlay_change_p
3110 || it->sp > 1
3111 || !get_overlay_strings_1 (it, 0, 0))
3112 {
3113 if (it->ellipsis_p)
3114 setup_for_ellipsis (it, 0);
3115 /* When handling a display spec, we might load an
3116 empty string. In that case, discard it here. We
3117 used to discard it in handle_single_display_spec,
3118 but that causes get_overlay_strings_1, above, to
3119 ignore overlay strings that we must check. */
3120 if (STRINGP (it->string) && !SCHARS (it->string))
3121 pop_it (it);
3122 return;
3123 }
3124 else if (STRINGP (it->string) && !SCHARS (it->string))
3125 pop_it (it);
3126 else
3127 {
3128 it->ignore_overlay_strings_at_pos_p = 1;
3129 it->string_from_display_prop_p = 0;
3130 it->from_disp_prop_p = 0;
3131 handle_overlay_change_p = 0;
3132 }
3133 handled = HANDLED_RECOMPUTE_PROPS;
3134 break;
3135 }
3136 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3137 handle_overlay_change_p = 0;
3138 }
3139
3140 if (handled != HANDLED_RECOMPUTE_PROPS)
3141 {
3142 /* Don't check for overlay strings below when set to deliver
3143 characters from a display vector. */
3144 if (it->method == GET_FROM_DISPLAY_VECTOR)
3145 handle_overlay_change_p = 0;
3146
3147 /* Handle overlay changes.
3148 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3149 if it finds overlays. */
3150 if (handle_overlay_change_p)
3151 handled = handle_overlay_change (it);
3152 }
3153
3154 if (it->ellipsis_p)
3155 {
3156 setup_for_ellipsis (it, 0);
3157 break;
3158 }
3159 }
3160 while (handled == HANDLED_RECOMPUTE_PROPS);
3161
3162 /* Determine where to stop next. */
3163 if (handled == HANDLED_NORMALLY)
3164 compute_stop_pos (it);
3165 }
3166
3167
3168 /* Compute IT->stop_charpos from text property and overlay change
3169 information for IT's current position. */
3170
3171 static void
3172 compute_stop_pos (struct it *it)
3173 {
3174 register INTERVAL iv, next_iv;
3175 Lisp_Object object, limit, position;
3176 ptrdiff_t charpos, bytepos;
3177
3178 if (STRINGP (it->string))
3179 {
3180 /* Strings are usually short, so don't limit the search for
3181 properties. */
3182 it->stop_charpos = it->end_charpos;
3183 object = it->string;
3184 limit = Qnil;
3185 charpos = IT_STRING_CHARPOS (*it);
3186 bytepos = IT_STRING_BYTEPOS (*it);
3187 }
3188 else
3189 {
3190 ptrdiff_t pos;
3191
3192 /* If end_charpos is out of range for some reason, such as a
3193 misbehaving display function, rationalize it (Bug#5984). */
3194 if (it->end_charpos > ZV)
3195 it->end_charpos = ZV;
3196 it->stop_charpos = it->end_charpos;
3197
3198 /* If next overlay change is in front of the current stop pos
3199 (which is IT->end_charpos), stop there. Note: value of
3200 next_overlay_change is point-max if no overlay change
3201 follows. */
3202 charpos = IT_CHARPOS (*it);
3203 bytepos = IT_BYTEPOS (*it);
3204 pos = next_overlay_change (charpos);
3205 if (pos < it->stop_charpos)
3206 it->stop_charpos = pos;
3207
3208 /* If showing the region, we have to stop at the region
3209 start or end because the face might change there. */
3210 if (it->region_beg_charpos > 0)
3211 {
3212 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3213 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3214 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3215 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3216 }
3217
3218 /* Set up variables for computing the stop position from text
3219 property changes. */
3220 XSETBUFFER (object, current_buffer);
3221 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3222 }
3223
3224 /* Get the interval containing IT's position. Value is a null
3225 interval if there isn't such an interval. */
3226 position = make_number (charpos);
3227 iv = validate_interval_range (object, &position, &position, 0);
3228 if (!NULL_INTERVAL_P (iv))
3229 {
3230 Lisp_Object values_here[LAST_PROP_IDX];
3231 struct props *p;
3232
3233 /* Get properties here. */
3234 for (p = it_props; p->handler; ++p)
3235 values_here[p->idx] = textget (iv->plist, *p->name);
3236
3237 /* Look for an interval following iv that has different
3238 properties. */
3239 for (next_iv = next_interval (iv);
3240 (!NULL_INTERVAL_P (next_iv)
3241 && (NILP (limit)
3242 || XFASTINT (limit) > next_iv->position));
3243 next_iv = next_interval (next_iv))
3244 {
3245 for (p = it_props; p->handler; ++p)
3246 {
3247 Lisp_Object new_value;
3248
3249 new_value = textget (next_iv->plist, *p->name);
3250 if (!EQ (values_here[p->idx], new_value))
3251 break;
3252 }
3253
3254 if (p->handler)
3255 break;
3256 }
3257
3258 if (!NULL_INTERVAL_P (next_iv))
3259 {
3260 if (INTEGERP (limit)
3261 && next_iv->position >= XFASTINT (limit))
3262 /* No text property change up to limit. */
3263 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3264 else
3265 /* Text properties change in next_iv. */
3266 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3267 }
3268 }
3269
3270 if (it->cmp_it.id < 0)
3271 {
3272 ptrdiff_t stoppos = it->end_charpos;
3273
3274 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3275 stoppos = -1;
3276 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3277 stoppos, it->string);
3278 }
3279
3280 xassert (STRINGP (it->string)
3281 || (it->stop_charpos >= BEGV
3282 && it->stop_charpos >= IT_CHARPOS (*it)));
3283 }
3284
3285
3286 /* Return the position of the next overlay change after POS in
3287 current_buffer. Value is point-max if no overlay change
3288 follows. This is like `next-overlay-change' but doesn't use
3289 xmalloc. */
3290
3291 static ptrdiff_t
3292 next_overlay_change (ptrdiff_t pos)
3293 {
3294 ptrdiff_t i, noverlays;
3295 ptrdiff_t endpos;
3296 Lisp_Object *overlays;
3297
3298 /* Get all overlays at the given position. */
3299 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3300
3301 /* If any of these overlays ends before endpos,
3302 use its ending point instead. */
3303 for (i = 0; i < noverlays; ++i)
3304 {
3305 Lisp_Object oend;
3306 ptrdiff_t oendpos;
3307
3308 oend = OVERLAY_END (overlays[i]);
3309 oendpos = OVERLAY_POSITION (oend);
3310 endpos = min (endpos, oendpos);
3311 }
3312
3313 return endpos;
3314 }
3315
3316 /* How many characters forward to search for a display property or
3317 display string. Searching too far forward makes the bidi display
3318 sluggish, especially in small windows. */
3319 #define MAX_DISP_SCAN 250
3320
3321 /* Return the character position of a display string at or after
3322 position specified by POSITION. If no display string exists at or
3323 after POSITION, return ZV. A display string is either an overlay
3324 with `display' property whose value is a string, or a `display'
3325 text property whose value is a string. STRING is data about the
3326 string to iterate; if STRING->lstring is nil, we are iterating a
3327 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3328 on a GUI frame. DISP_PROP is set to zero if we searched
3329 MAX_DISP_SCAN characters forward without finding any display
3330 strings, non-zero otherwise. It is set to 2 if the display string
3331 uses any kind of `(space ...)' spec that will produce a stretch of
3332 white space in the text area. */
3333 ptrdiff_t
3334 compute_display_string_pos (struct text_pos *position,
3335 struct bidi_string_data *string,
3336 int frame_window_p, int *disp_prop)
3337 {
3338 /* OBJECT = nil means current buffer. */
3339 Lisp_Object object =
3340 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3341 Lisp_Object pos, spec, limpos;
3342 int string_p = (string && (STRINGP (string->lstring) || string->s));
3343 ptrdiff_t eob = string_p ? string->schars : ZV;
3344 ptrdiff_t begb = string_p ? 0 : BEGV;
3345 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3346 ptrdiff_t lim =
3347 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3348 struct text_pos tpos;
3349 int rv = 0;
3350
3351 *disp_prop = 1;
3352
3353 if (charpos >= eob
3354 /* We don't support display properties whose values are strings
3355 that have display string properties. */
3356 || string->from_disp_str
3357 /* C strings cannot have display properties. */
3358 || (string->s && !STRINGP (object)))
3359 {
3360 *disp_prop = 0;
3361 return eob;
3362 }
3363
3364 /* If the character at CHARPOS is where the display string begins,
3365 return CHARPOS. */
3366 pos = make_number (charpos);
3367 if (STRINGP (object))
3368 bufpos = string->bufpos;
3369 else
3370 bufpos = charpos;
3371 tpos = *position;
3372 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3373 && (charpos <= begb
3374 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3375 object),
3376 spec))
3377 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3378 frame_window_p)))
3379 {
3380 if (rv == 2)
3381 *disp_prop = 2;
3382 return charpos;
3383 }
3384
3385 /* Look forward for the first character with a `display' property
3386 that will replace the underlying text when displayed. */
3387 limpos = make_number (lim);
3388 do {
3389 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3390 CHARPOS (tpos) = XFASTINT (pos);
3391 if (CHARPOS (tpos) >= lim)
3392 {
3393 *disp_prop = 0;
3394 break;
3395 }
3396 if (STRINGP (object))
3397 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3398 else
3399 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3400 spec = Fget_char_property (pos, Qdisplay, object);
3401 if (!STRINGP (object))
3402 bufpos = CHARPOS (tpos);
3403 } while (NILP (spec)
3404 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3405 bufpos, frame_window_p)));
3406 if (rv == 2)
3407 *disp_prop = 2;
3408
3409 return CHARPOS (tpos);
3410 }
3411
3412 /* Return the character position of the end of the display string that
3413 started at CHARPOS. If there's no display string at CHARPOS,
3414 return -1. A display string is either an overlay with `display'
3415 property whose value is a string or a `display' text property whose
3416 value is a string. */
3417 ptrdiff_t
3418 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3419 {
3420 /* OBJECT = nil means current buffer. */
3421 Lisp_Object object =
3422 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3423 Lisp_Object pos = make_number (charpos);
3424 ptrdiff_t eob =
3425 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3426
3427 if (charpos >= eob || (string->s && !STRINGP (object)))
3428 return eob;
3429
3430 /* It could happen that the display property or overlay was removed
3431 since we found it in compute_display_string_pos above. One way
3432 this can happen is if JIT font-lock was called (through
3433 handle_fontified_prop), and jit-lock-functions remove text
3434 properties or overlays from the portion of buffer that includes
3435 CHARPOS. Muse mode is known to do that, for example. In this
3436 case, we return -1 to the caller, to signal that no display
3437 string is actually present at CHARPOS. See bidi_fetch_char for
3438 how this is handled.
3439
3440 An alternative would be to never look for display properties past
3441 it->stop_charpos. But neither compute_display_string_pos nor
3442 bidi_fetch_char that calls it know or care where the next
3443 stop_charpos is. */
3444 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3445 return -1;
3446
3447 /* Look forward for the first character where the `display' property
3448 changes. */
3449 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3450
3451 return XFASTINT (pos);
3452 }
3453
3454
3455 \f
3456 /***********************************************************************
3457 Fontification
3458 ***********************************************************************/
3459
3460 /* Handle changes in the `fontified' property of the current buffer by
3461 calling hook functions from Qfontification_functions to fontify
3462 regions of text. */
3463
3464 static enum prop_handled
3465 handle_fontified_prop (struct it *it)
3466 {
3467 Lisp_Object prop, pos;
3468 enum prop_handled handled = HANDLED_NORMALLY;
3469
3470 if (!NILP (Vmemory_full))
3471 return handled;
3472
3473 /* Get the value of the `fontified' property at IT's current buffer
3474 position. (The `fontified' property doesn't have a special
3475 meaning in strings.) If the value is nil, call functions from
3476 Qfontification_functions. */
3477 if (!STRINGP (it->string)
3478 && it->s == NULL
3479 && !NILP (Vfontification_functions)
3480 && !NILP (Vrun_hooks)
3481 && (pos = make_number (IT_CHARPOS (*it)),
3482 prop = Fget_char_property (pos, Qfontified, Qnil),
3483 /* Ignore the special cased nil value always present at EOB since
3484 no amount of fontifying will be able to change it. */
3485 NILP (prop) && IT_CHARPOS (*it) < Z))
3486 {
3487 ptrdiff_t count = SPECPDL_INDEX ();
3488 Lisp_Object val;
3489 struct buffer *obuf = current_buffer;
3490 int begv = BEGV, zv = ZV;
3491 int old_clip_changed = current_buffer->clip_changed;
3492
3493 val = Vfontification_functions;
3494 specbind (Qfontification_functions, Qnil);
3495
3496 xassert (it->end_charpos == ZV);
3497
3498 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3499 safe_call1 (val, pos);
3500 else
3501 {
3502 Lisp_Object fns, fn;
3503 struct gcpro gcpro1, gcpro2;
3504
3505 fns = Qnil;
3506 GCPRO2 (val, fns);
3507
3508 for (; CONSP (val); val = XCDR (val))
3509 {
3510 fn = XCAR (val);
3511
3512 if (EQ (fn, Qt))
3513 {
3514 /* A value of t indicates this hook has a local
3515 binding; it means to run the global binding too.
3516 In a global value, t should not occur. If it
3517 does, we must ignore it to avoid an endless
3518 loop. */
3519 for (fns = Fdefault_value (Qfontification_functions);
3520 CONSP (fns);
3521 fns = XCDR (fns))
3522 {
3523 fn = XCAR (fns);
3524 if (!EQ (fn, Qt))
3525 safe_call1 (fn, pos);
3526 }
3527 }
3528 else
3529 safe_call1 (fn, pos);
3530 }
3531
3532 UNGCPRO;
3533 }
3534
3535 unbind_to (count, Qnil);
3536
3537 /* Fontification functions routinely call `save-restriction'.
3538 Normally, this tags clip_changed, which can confuse redisplay
3539 (see discussion in Bug#6671). Since we don't perform any
3540 special handling of fontification changes in the case where
3541 `save-restriction' isn't called, there's no point doing so in
3542 this case either. So, if the buffer's restrictions are
3543 actually left unchanged, reset clip_changed. */
3544 if (obuf == current_buffer)
3545 {
3546 if (begv == BEGV && zv == ZV)
3547 current_buffer->clip_changed = old_clip_changed;
3548 }
3549 /* There isn't much we can reasonably do to protect against
3550 misbehaving fontification, but here's a fig leaf. */
3551 else if (!NILP (BVAR (obuf, name)))
3552 set_buffer_internal_1 (obuf);
3553
3554 /* The fontification code may have added/removed text.
3555 It could do even a lot worse, but let's at least protect against
3556 the most obvious case where only the text past `pos' gets changed',
3557 as is/was done in grep.el where some escapes sequences are turned
3558 into face properties (bug#7876). */
3559 it->end_charpos = ZV;
3560
3561 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3562 something. This avoids an endless loop if they failed to
3563 fontify the text for which reason ever. */
3564 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3565 handled = HANDLED_RECOMPUTE_PROPS;
3566 }
3567
3568 return handled;
3569 }
3570
3571
3572 \f
3573 /***********************************************************************
3574 Faces
3575 ***********************************************************************/
3576
3577 /* Set up iterator IT from face properties at its current position.
3578 Called from handle_stop. */
3579
3580 static enum prop_handled
3581 handle_face_prop (struct it *it)
3582 {
3583 int new_face_id;
3584 ptrdiff_t next_stop;
3585
3586 if (!STRINGP (it->string))
3587 {
3588 new_face_id
3589 = face_at_buffer_position (it->w,
3590 IT_CHARPOS (*it),
3591 it->region_beg_charpos,
3592 it->region_end_charpos,
3593 &next_stop,
3594 (IT_CHARPOS (*it)
3595 + TEXT_PROP_DISTANCE_LIMIT),
3596 0, it->base_face_id);
3597
3598 /* Is this a start of a run of characters with box face?
3599 Caveat: this can be called for a freshly initialized
3600 iterator; face_id is -1 in this case. We know that the new
3601 face will not change until limit, i.e. if the new face has a
3602 box, all characters up to limit will have one. But, as
3603 usual, we don't know whether limit is really the end. */
3604 if (new_face_id != it->face_id)
3605 {
3606 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3607
3608 /* If new face has a box but old face has not, this is
3609 the start of a run of characters with box, i.e. it has
3610 a shadow on the left side. The value of face_id of the
3611 iterator will be -1 if this is the initial call that gets
3612 the face. In this case, we have to look in front of IT's
3613 position and see whether there is a face != new_face_id. */
3614 it->start_of_box_run_p
3615 = (new_face->box != FACE_NO_BOX
3616 && (it->face_id >= 0
3617 || IT_CHARPOS (*it) == BEG
3618 || new_face_id != face_before_it_pos (it)));
3619 it->face_box_p = new_face->box != FACE_NO_BOX;
3620 }
3621 }
3622 else
3623 {
3624 int base_face_id;
3625 ptrdiff_t bufpos;
3626 int i;
3627 Lisp_Object from_overlay
3628 = (it->current.overlay_string_index >= 0
3629 ? it->string_overlays[it->current.overlay_string_index]
3630 : Qnil);
3631
3632 /* See if we got to this string directly or indirectly from
3633 an overlay property. That includes the before-string or
3634 after-string of an overlay, strings in display properties
3635 provided by an overlay, their text properties, etc.
3636
3637 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3638 if (! NILP (from_overlay))
3639 for (i = it->sp - 1; i >= 0; i--)
3640 {
3641 if (it->stack[i].current.overlay_string_index >= 0)
3642 from_overlay
3643 = it->string_overlays[it->stack[i].current.overlay_string_index];
3644 else if (! NILP (it->stack[i].from_overlay))
3645 from_overlay = it->stack[i].from_overlay;
3646
3647 if (!NILP (from_overlay))
3648 break;
3649 }
3650
3651 if (! NILP (from_overlay))
3652 {
3653 bufpos = IT_CHARPOS (*it);
3654 /* For a string from an overlay, the base face depends
3655 only on text properties and ignores overlays. */
3656 base_face_id
3657 = face_for_overlay_string (it->w,
3658 IT_CHARPOS (*it),
3659 it->region_beg_charpos,
3660 it->region_end_charpos,
3661 &next_stop,
3662 (IT_CHARPOS (*it)
3663 + TEXT_PROP_DISTANCE_LIMIT),
3664 0,
3665 from_overlay);
3666 }
3667 else
3668 {
3669 bufpos = 0;
3670
3671 /* For strings from a `display' property, use the face at
3672 IT's current buffer position as the base face to merge
3673 with, so that overlay strings appear in the same face as
3674 surrounding text, unless they specify their own
3675 faces. */
3676 base_face_id = underlying_face_id (it);
3677 }
3678
3679 new_face_id = face_at_string_position (it->w,
3680 it->string,
3681 IT_STRING_CHARPOS (*it),
3682 bufpos,
3683 it->region_beg_charpos,
3684 it->region_end_charpos,
3685 &next_stop,
3686 base_face_id, 0);
3687
3688 /* Is this a start of a run of characters with box? Caveat:
3689 this can be called for a freshly allocated iterator; face_id
3690 is -1 is this case. We know that the new face will not
3691 change until the next check pos, i.e. if the new face has a
3692 box, all characters up to that position will have a
3693 box. But, as usual, we don't know whether that position
3694 is really the end. */
3695 if (new_face_id != it->face_id)
3696 {
3697 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3698 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3699
3700 /* If new face has a box but old face hasn't, this is the
3701 start of a run of characters with box, i.e. it has a
3702 shadow on the left side. */
3703 it->start_of_box_run_p
3704 = new_face->box && (old_face == NULL || !old_face->box);
3705 it->face_box_p = new_face->box != FACE_NO_BOX;
3706 }
3707 }
3708
3709 it->face_id = new_face_id;
3710 return HANDLED_NORMALLY;
3711 }
3712
3713
3714 /* Return the ID of the face ``underlying'' IT's current position,
3715 which is in a string. If the iterator is associated with a
3716 buffer, return the face at IT's current buffer position.
3717 Otherwise, use the iterator's base_face_id. */
3718
3719 static int
3720 underlying_face_id (struct it *it)
3721 {
3722 int face_id = it->base_face_id, i;
3723
3724 xassert (STRINGP (it->string));
3725
3726 for (i = it->sp - 1; i >= 0; --i)
3727 if (NILP (it->stack[i].string))
3728 face_id = it->stack[i].face_id;
3729
3730 return face_id;
3731 }
3732
3733
3734 /* Compute the face one character before or after the current position
3735 of IT, in the visual order. BEFORE_P non-zero means get the face
3736 in front (to the left in L2R paragraphs, to the right in R2L
3737 paragraphs) of IT's screen position. Value is the ID of the face. */
3738
3739 static int
3740 face_before_or_after_it_pos (struct it *it, int before_p)
3741 {
3742 int face_id, limit;
3743 ptrdiff_t next_check_charpos;
3744 struct it it_copy;
3745 void *it_copy_data = NULL;
3746
3747 xassert (it->s == NULL);
3748
3749 if (STRINGP (it->string))
3750 {
3751 ptrdiff_t bufpos, charpos;
3752 int base_face_id;
3753
3754 /* No face change past the end of the string (for the case
3755 we are padding with spaces). No face change before the
3756 string start. */
3757 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3758 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3759 return it->face_id;
3760
3761 if (!it->bidi_p)
3762 {
3763 /* Set charpos to the position before or after IT's current
3764 position, in the logical order, which in the non-bidi
3765 case is the same as the visual order. */
3766 if (before_p)
3767 charpos = IT_STRING_CHARPOS (*it) - 1;
3768 else if (it->what == IT_COMPOSITION)
3769 /* For composition, we must check the character after the
3770 composition. */
3771 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3772 else
3773 charpos = IT_STRING_CHARPOS (*it) + 1;
3774 }
3775 else
3776 {
3777 if (before_p)
3778 {
3779 /* With bidi iteration, the character before the current
3780 in the visual order cannot be found by simple
3781 iteration, because "reverse" reordering is not
3782 supported. Instead, we need to use the move_it_*
3783 family of functions. */
3784 /* Ignore face changes before the first visible
3785 character on this display line. */
3786 if (it->current_x <= it->first_visible_x)
3787 return it->face_id;
3788 SAVE_IT (it_copy, *it, it_copy_data);
3789 /* Implementation note: Since move_it_in_display_line
3790 works in the iterator geometry, and thinks the first
3791 character is always the leftmost, even in R2L lines,
3792 we don't need to distinguish between the R2L and L2R
3793 cases here. */
3794 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3795 it_copy.current_x - 1, MOVE_TO_X);
3796 charpos = IT_STRING_CHARPOS (it_copy);
3797 RESTORE_IT (it, it, it_copy_data);
3798 }
3799 else
3800 {
3801 /* Set charpos to the string position of the character
3802 that comes after IT's current position in the visual
3803 order. */
3804 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3805
3806 it_copy = *it;
3807 while (n--)
3808 bidi_move_to_visually_next (&it_copy.bidi_it);
3809
3810 charpos = it_copy.bidi_it.charpos;
3811 }
3812 }
3813 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3814
3815 if (it->current.overlay_string_index >= 0)
3816 bufpos = IT_CHARPOS (*it);
3817 else
3818 bufpos = 0;
3819
3820 base_face_id = underlying_face_id (it);
3821
3822 /* Get the face for ASCII, or unibyte. */
3823 face_id = face_at_string_position (it->w,
3824 it->string,
3825 charpos,
3826 bufpos,
3827 it->region_beg_charpos,
3828 it->region_end_charpos,
3829 &next_check_charpos,
3830 base_face_id, 0);
3831
3832 /* Correct the face for charsets different from ASCII. Do it
3833 for the multibyte case only. The face returned above is
3834 suitable for unibyte text if IT->string is unibyte. */
3835 if (STRING_MULTIBYTE (it->string))
3836 {
3837 struct text_pos pos1 = string_pos (charpos, it->string);
3838 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3839 int c, len;
3840 struct face *face = FACE_FROM_ID (it->f, face_id);
3841
3842 c = string_char_and_length (p, &len);
3843 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3844 }
3845 }
3846 else
3847 {
3848 struct text_pos pos;
3849
3850 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3851 || (IT_CHARPOS (*it) <= BEGV && before_p))
3852 return it->face_id;
3853
3854 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3855 pos = it->current.pos;
3856
3857 if (!it->bidi_p)
3858 {
3859 if (before_p)
3860 DEC_TEXT_POS (pos, it->multibyte_p);
3861 else
3862 {
3863 if (it->what == IT_COMPOSITION)
3864 {
3865 /* For composition, we must check the position after
3866 the composition. */
3867 pos.charpos += it->cmp_it.nchars;
3868 pos.bytepos += it->len;
3869 }
3870 else
3871 INC_TEXT_POS (pos, it->multibyte_p);
3872 }
3873 }
3874 else
3875 {
3876 if (before_p)
3877 {
3878 /* With bidi iteration, the character before the current
3879 in the visual order cannot be found by simple
3880 iteration, because "reverse" reordering is not
3881 supported. Instead, we need to use the move_it_*
3882 family of functions. */
3883 /* Ignore face changes before the first visible
3884 character on this display line. */
3885 if (it->current_x <= it->first_visible_x)
3886 return it->face_id;
3887 SAVE_IT (it_copy, *it, it_copy_data);
3888 /* Implementation note: Since move_it_in_display_line
3889 works in the iterator geometry, and thinks the first
3890 character is always the leftmost, even in R2L lines,
3891 we don't need to distinguish between the R2L and L2R
3892 cases here. */
3893 move_it_in_display_line (&it_copy, ZV,
3894 it_copy.current_x - 1, MOVE_TO_X);
3895 pos = it_copy.current.pos;
3896 RESTORE_IT (it, it, it_copy_data);
3897 }
3898 else
3899 {
3900 /* Set charpos to the buffer position of the character
3901 that comes after IT's current position in the visual
3902 order. */
3903 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3904
3905 it_copy = *it;
3906 while (n--)
3907 bidi_move_to_visually_next (&it_copy.bidi_it);
3908
3909 SET_TEXT_POS (pos,
3910 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3911 }
3912 }
3913 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3914
3915 /* Determine face for CHARSET_ASCII, or unibyte. */
3916 face_id = face_at_buffer_position (it->w,
3917 CHARPOS (pos),
3918 it->region_beg_charpos,
3919 it->region_end_charpos,
3920 &next_check_charpos,
3921 limit, 0, -1);
3922
3923 /* Correct the face for charsets different from ASCII. Do it
3924 for the multibyte case only. The face returned above is
3925 suitable for unibyte text if current_buffer is unibyte. */
3926 if (it->multibyte_p)
3927 {
3928 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3929 struct face *face = FACE_FROM_ID (it->f, face_id);
3930 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3931 }
3932 }
3933
3934 return face_id;
3935 }
3936
3937
3938 \f
3939 /***********************************************************************
3940 Invisible text
3941 ***********************************************************************/
3942
3943 /* Set up iterator IT from invisible properties at its current
3944 position. Called from handle_stop. */
3945
3946 static enum prop_handled
3947 handle_invisible_prop (struct it *it)
3948 {
3949 enum prop_handled handled = HANDLED_NORMALLY;
3950
3951 if (STRINGP (it->string))
3952 {
3953 Lisp_Object prop, end_charpos, limit, charpos;
3954
3955 /* Get the value of the invisible text property at the
3956 current position. Value will be nil if there is no such
3957 property. */
3958 charpos = make_number (IT_STRING_CHARPOS (*it));
3959 prop = Fget_text_property (charpos, Qinvisible, it->string);
3960
3961 if (!NILP (prop)
3962 && IT_STRING_CHARPOS (*it) < it->end_charpos)
3963 {
3964 ptrdiff_t endpos;
3965
3966 handled = HANDLED_RECOMPUTE_PROPS;
3967
3968 /* Get the position at which the next change of the
3969 invisible text property can be found in IT->string.
3970 Value will be nil if the property value is the same for
3971 all the rest of IT->string. */
3972 XSETINT (limit, SCHARS (it->string));
3973 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
3974 it->string, limit);
3975
3976 /* Text at current position is invisible. The next
3977 change in the property is at position end_charpos.
3978 Move IT's current position to that position. */
3979 if (INTEGERP (end_charpos)
3980 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
3981 {
3982 struct text_pos old;
3983 ptrdiff_t oldpos;
3984
3985 old = it->current.string_pos;
3986 oldpos = CHARPOS (old);
3987 if (it->bidi_p)
3988 {
3989 if (it->bidi_it.first_elt
3990 && it->bidi_it.charpos < SCHARS (it->string))
3991 bidi_paragraph_init (it->paragraph_embedding,
3992 &it->bidi_it, 1);
3993 /* Bidi-iterate out of the invisible text. */
3994 do
3995 {
3996 bidi_move_to_visually_next (&it->bidi_it);
3997 }
3998 while (oldpos <= it->bidi_it.charpos
3999 && it->bidi_it.charpos < endpos);
4000
4001 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4002 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4003 if (IT_CHARPOS (*it) >= endpos)
4004 it->prev_stop = endpos;
4005 }
4006 else
4007 {
4008 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4009 compute_string_pos (&it->current.string_pos, old, it->string);
4010 }
4011 }
4012 else
4013 {
4014 /* The rest of the string is invisible. If this is an
4015 overlay string, proceed with the next overlay string
4016 or whatever comes and return a character from there. */
4017 if (it->current.overlay_string_index >= 0)
4018 {
4019 next_overlay_string (it);
4020 /* Don't check for overlay strings when we just
4021 finished processing them. */
4022 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4023 }
4024 else
4025 {
4026 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4027 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4028 }
4029 }
4030 }
4031 }
4032 else
4033 {
4034 int invis_p;
4035 ptrdiff_t newpos, next_stop, start_charpos, tem;
4036 Lisp_Object pos, prop, overlay;
4037
4038 /* First of all, is there invisible text at this position? */
4039 tem = start_charpos = IT_CHARPOS (*it);
4040 pos = make_number (tem);
4041 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4042 &overlay);
4043 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4044
4045 /* If we are on invisible text, skip over it. */
4046 if (invis_p && start_charpos < it->end_charpos)
4047 {
4048 /* Record whether we have to display an ellipsis for the
4049 invisible text. */
4050 int display_ellipsis_p = invis_p == 2;
4051
4052 handled = HANDLED_RECOMPUTE_PROPS;
4053
4054 /* Loop skipping over invisible text. The loop is left at
4055 ZV or with IT on the first char being visible again. */
4056 do
4057 {
4058 /* Try to skip some invisible text. Return value is the
4059 position reached which can be equal to where we start
4060 if there is nothing invisible there. This skips both
4061 over invisible text properties and overlays with
4062 invisible property. */
4063 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4064
4065 /* If we skipped nothing at all we weren't at invisible
4066 text in the first place. If everything to the end of
4067 the buffer was skipped, end the loop. */
4068 if (newpos == tem || newpos >= ZV)
4069 invis_p = 0;
4070 else
4071 {
4072 /* We skipped some characters but not necessarily
4073 all there are. Check if we ended up on visible
4074 text. Fget_char_property returns the property of
4075 the char before the given position, i.e. if we
4076 get invis_p = 0, this means that the char at
4077 newpos is visible. */
4078 pos = make_number (newpos);
4079 prop = Fget_char_property (pos, Qinvisible, it->window);
4080 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4081 }
4082
4083 /* If we ended up on invisible text, proceed to
4084 skip starting with next_stop. */
4085 if (invis_p)
4086 tem = next_stop;
4087
4088 /* If there are adjacent invisible texts, don't lose the
4089 second one's ellipsis. */
4090 if (invis_p == 2)
4091 display_ellipsis_p = 1;
4092 }
4093 while (invis_p);
4094
4095 /* The position newpos is now either ZV or on visible text. */
4096 if (it->bidi_p && newpos < ZV)
4097 {
4098 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4099 int on_newline = FETCH_BYTE (bpos) == '\n';
4100 int after_newline =
4101 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4102
4103 /* If the invisible text ends on a newline or on a
4104 character after a newline, we can avoid the costly,
4105 character by character, bidi iteration to NEWPOS, and
4106 instead simply reseat the iterator there. That's
4107 because all bidi reordering information is tossed at
4108 the newline. This is a big win for modes that hide
4109 complete lines, like Outline, Org, etc. */
4110 if (on_newline || after_newline)
4111 {
4112 struct text_pos tpos;
4113 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4114
4115 SET_TEXT_POS (tpos, newpos, bpos);
4116 reseat_1 (it, tpos, 0);
4117 /* If we reseat on a newline, we need to prep the
4118 bidi iterator for advancing to the next character
4119 after the newline, keeping the current paragraph
4120 direction (so that PRODUCE_GLYPHS does TRT wrt
4121 prepending/appending glyphs to a glyph row). */
4122 if (on_newline)
4123 {
4124 it->bidi_it.first_elt = 0;
4125 it->bidi_it.paragraph_dir = pdir;
4126 it->bidi_it.ch = '\n';
4127 it->bidi_it.nchars = 1;
4128 it->bidi_it.ch_len = 1;
4129 }
4130 }
4131 else /* Must use the slow method. */
4132 {
4133 /* With bidi iteration, the region of invisible text
4134 could start and/or end in the middle of a
4135 non-base embedding level. Therefore, we need to
4136 skip invisible text using the bidi iterator,
4137 starting at IT's current position, until we find
4138 ourselves outside of the invisible text.
4139 Skipping invisible text _after_ bidi iteration
4140 avoids affecting the visual order of the
4141 displayed text when invisible properties are
4142 added or removed. */
4143 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4144 {
4145 /* If we were `reseat'ed to a new paragraph,
4146 determine the paragraph base direction. We
4147 need to do it now because
4148 next_element_from_buffer may not have a
4149 chance to do it, if we are going to skip any
4150 text at the beginning, which resets the
4151 FIRST_ELT flag. */
4152 bidi_paragraph_init (it->paragraph_embedding,
4153 &it->bidi_it, 1);
4154 }
4155 do
4156 {
4157 bidi_move_to_visually_next (&it->bidi_it);
4158 }
4159 while (it->stop_charpos <= it->bidi_it.charpos
4160 && it->bidi_it.charpos < newpos);
4161 IT_CHARPOS (*it) = it->bidi_it.charpos;
4162 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4163 /* If we overstepped NEWPOS, record its position in
4164 the iterator, so that we skip invisible text if
4165 later the bidi iteration lands us in the
4166 invisible region again. */
4167 if (IT_CHARPOS (*it) >= newpos)
4168 it->prev_stop = newpos;
4169 }
4170 }
4171 else
4172 {
4173 IT_CHARPOS (*it) = newpos;
4174 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4175 }
4176
4177 /* If there are before-strings at the start of invisible
4178 text, and the text is invisible because of a text
4179 property, arrange to show before-strings because 20.x did
4180 it that way. (If the text is invisible because of an
4181 overlay property instead of a text property, this is
4182 already handled in the overlay code.) */
4183 if (NILP (overlay)
4184 && get_overlay_strings (it, it->stop_charpos))
4185 {
4186 handled = HANDLED_RECOMPUTE_PROPS;
4187 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4188 }
4189 else if (display_ellipsis_p)
4190 {
4191 /* Make sure that the glyphs of the ellipsis will get
4192 correct `charpos' values. If we would not update
4193 it->position here, the glyphs would belong to the
4194 last visible character _before_ the invisible
4195 text, which confuses `set_cursor_from_row'.
4196
4197 We use the last invisible position instead of the
4198 first because this way the cursor is always drawn on
4199 the first "." of the ellipsis, whenever PT is inside
4200 the invisible text. Otherwise the cursor would be
4201 placed _after_ the ellipsis when the point is after the
4202 first invisible character. */
4203 if (!STRINGP (it->object))
4204 {
4205 it->position.charpos = newpos - 1;
4206 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4207 }
4208 it->ellipsis_p = 1;
4209 /* Let the ellipsis display before
4210 considering any properties of the following char.
4211 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4212 handled = HANDLED_RETURN;
4213 }
4214 }
4215 }
4216
4217 return handled;
4218 }
4219
4220
4221 /* Make iterator IT return `...' next.
4222 Replaces LEN characters from buffer. */
4223
4224 static void
4225 setup_for_ellipsis (struct it *it, int len)
4226 {
4227 /* Use the display table definition for `...'. Invalid glyphs
4228 will be handled by the method returning elements from dpvec. */
4229 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4230 {
4231 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4232 it->dpvec = v->contents;
4233 it->dpend = v->contents + v->header.size;
4234 }
4235 else
4236 {
4237 /* Default `...'. */
4238 it->dpvec = default_invis_vector;
4239 it->dpend = default_invis_vector + 3;
4240 }
4241
4242 it->dpvec_char_len = len;
4243 it->current.dpvec_index = 0;
4244 it->dpvec_face_id = -1;
4245
4246 /* Remember the current face id in case glyphs specify faces.
4247 IT's face is restored in set_iterator_to_next.
4248 saved_face_id was set to preceding char's face in handle_stop. */
4249 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4250 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4251
4252 it->method = GET_FROM_DISPLAY_VECTOR;
4253 it->ellipsis_p = 1;
4254 }
4255
4256
4257 \f
4258 /***********************************************************************
4259 'display' property
4260 ***********************************************************************/
4261
4262 /* Set up iterator IT from `display' property at its current position.
4263 Called from handle_stop.
4264 We return HANDLED_RETURN if some part of the display property
4265 overrides the display of the buffer text itself.
4266 Otherwise we return HANDLED_NORMALLY. */
4267
4268 static enum prop_handled
4269 handle_display_prop (struct it *it)
4270 {
4271 Lisp_Object propval, object, overlay;
4272 struct text_pos *position;
4273 ptrdiff_t bufpos;
4274 /* Nonzero if some property replaces the display of the text itself. */
4275 int display_replaced_p = 0;
4276
4277 if (STRINGP (it->string))
4278 {
4279 object = it->string;
4280 position = &it->current.string_pos;
4281 bufpos = CHARPOS (it->current.pos);
4282 }
4283 else
4284 {
4285 XSETWINDOW (object, it->w);
4286 position = &it->current.pos;
4287 bufpos = CHARPOS (*position);
4288 }
4289
4290 /* Reset those iterator values set from display property values. */
4291 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4292 it->space_width = Qnil;
4293 it->font_height = Qnil;
4294 it->voffset = 0;
4295
4296 /* We don't support recursive `display' properties, i.e. string
4297 values that have a string `display' property, that have a string
4298 `display' property etc. */
4299 if (!it->string_from_display_prop_p)
4300 it->area = TEXT_AREA;
4301
4302 propval = get_char_property_and_overlay (make_number (position->charpos),
4303 Qdisplay, object, &overlay);
4304 if (NILP (propval))
4305 return HANDLED_NORMALLY;
4306 /* Now OVERLAY is the overlay that gave us this property, or nil
4307 if it was a text property. */
4308
4309 if (!STRINGP (it->string))
4310 object = it->w->buffer;
4311
4312 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4313 position, bufpos,
4314 FRAME_WINDOW_P (it->f));
4315
4316 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4317 }
4318
4319 /* Subroutine of handle_display_prop. Returns non-zero if the display
4320 specification in SPEC is a replacing specification, i.e. it would
4321 replace the text covered by `display' property with something else,
4322 such as an image or a display string. If SPEC includes any kind or
4323 `(space ...) specification, the value is 2; this is used by
4324 compute_display_string_pos, which see.
4325
4326 See handle_single_display_spec for documentation of arguments.
4327 frame_window_p is non-zero if the window being redisplayed is on a
4328 GUI frame; this argument is used only if IT is NULL, see below.
4329
4330 IT can be NULL, if this is called by the bidi reordering code
4331 through compute_display_string_pos, which see. In that case, this
4332 function only examines SPEC, but does not otherwise "handle" it, in
4333 the sense that it doesn't set up members of IT from the display
4334 spec. */
4335 static int
4336 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4337 Lisp_Object overlay, struct text_pos *position,
4338 ptrdiff_t bufpos, int frame_window_p)
4339 {
4340 int replacing_p = 0;
4341 int rv;
4342
4343 if (CONSP (spec)
4344 /* Simple specerties. */
4345 && !EQ (XCAR (spec), Qimage)
4346 && !EQ (XCAR (spec), Qspace)
4347 && !EQ (XCAR (spec), Qwhen)
4348 && !EQ (XCAR (spec), Qslice)
4349 && !EQ (XCAR (spec), Qspace_width)
4350 && !EQ (XCAR (spec), Qheight)
4351 && !EQ (XCAR (spec), Qraise)
4352 /* Marginal area specifications. */
4353 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4354 && !EQ (XCAR (spec), Qleft_fringe)
4355 && !EQ (XCAR (spec), Qright_fringe)
4356 && !NILP (XCAR (spec)))
4357 {
4358 for (; CONSP (spec); spec = XCDR (spec))
4359 {
4360 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4361 overlay, position, bufpos,
4362 replacing_p, frame_window_p)))
4363 {
4364 replacing_p = rv;
4365 /* If some text in a string is replaced, `position' no
4366 longer points to the position of `object'. */
4367 if (!it || STRINGP (object))
4368 break;
4369 }
4370 }
4371 }
4372 else if (VECTORP (spec))
4373 {
4374 ptrdiff_t i;
4375 for (i = 0; i < ASIZE (spec); ++i)
4376 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4377 overlay, position, bufpos,
4378 replacing_p, frame_window_p)))
4379 {
4380 replacing_p = rv;
4381 /* If some text in a string is replaced, `position' no
4382 longer points to the position of `object'. */
4383 if (!it || STRINGP (object))
4384 break;
4385 }
4386 }
4387 else
4388 {
4389 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4390 position, bufpos, 0,
4391 frame_window_p)))
4392 replacing_p = rv;
4393 }
4394
4395 return replacing_p;
4396 }
4397
4398 /* Value is the position of the end of the `display' property starting
4399 at START_POS in OBJECT. */
4400
4401 static struct text_pos
4402 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4403 {
4404 Lisp_Object end;
4405 struct text_pos end_pos;
4406
4407 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4408 Qdisplay, object, Qnil);
4409 CHARPOS (end_pos) = XFASTINT (end);
4410 if (STRINGP (object))
4411 compute_string_pos (&end_pos, start_pos, it->string);
4412 else
4413 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4414
4415 return end_pos;
4416 }
4417
4418
4419 /* Set up IT from a single `display' property specification SPEC. OBJECT
4420 is the object in which the `display' property was found. *POSITION
4421 is the position in OBJECT at which the `display' property was found.
4422 BUFPOS is the buffer position of OBJECT (different from POSITION if
4423 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4424 previously saw a display specification which already replaced text
4425 display with something else, for example an image; we ignore such
4426 properties after the first one has been processed.
4427
4428 OVERLAY is the overlay this `display' property came from,
4429 or nil if it was a text property.
4430
4431 If SPEC is a `space' or `image' specification, and in some other
4432 cases too, set *POSITION to the position where the `display'
4433 property ends.
4434
4435 If IT is NULL, only examine the property specification in SPEC, but
4436 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4437 is intended to be displayed in a window on a GUI frame.
4438
4439 Value is non-zero if something was found which replaces the display
4440 of buffer or string text. */
4441
4442 static int
4443 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4444 Lisp_Object overlay, struct text_pos *position,
4445 ptrdiff_t bufpos, int display_replaced_p,
4446 int frame_window_p)
4447 {
4448 Lisp_Object form;
4449 Lisp_Object location, value;
4450 struct text_pos start_pos = *position;
4451 int valid_p;
4452
4453 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4454 If the result is non-nil, use VALUE instead of SPEC. */
4455 form = Qt;
4456 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4457 {
4458 spec = XCDR (spec);
4459 if (!CONSP (spec))
4460 return 0;
4461 form = XCAR (spec);
4462 spec = XCDR (spec);
4463 }
4464
4465 if (!NILP (form) && !EQ (form, Qt))
4466 {
4467 ptrdiff_t count = SPECPDL_INDEX ();
4468 struct gcpro gcpro1;
4469
4470 /* Bind `object' to the object having the `display' property, a
4471 buffer or string. Bind `position' to the position in the
4472 object where the property was found, and `buffer-position'
4473 to the current position in the buffer. */
4474
4475 if (NILP (object))
4476 XSETBUFFER (object, current_buffer);
4477 specbind (Qobject, object);
4478 specbind (Qposition, make_number (CHARPOS (*position)));
4479 specbind (Qbuffer_position, make_number (bufpos));
4480 GCPRO1 (form);
4481 form = safe_eval (form);
4482 UNGCPRO;
4483 unbind_to (count, Qnil);
4484 }
4485
4486 if (NILP (form))
4487 return 0;
4488
4489 /* Handle `(height HEIGHT)' specifications. */
4490 if (CONSP (spec)
4491 && EQ (XCAR (spec), Qheight)
4492 && CONSP (XCDR (spec)))
4493 {
4494 if (it)
4495 {
4496 if (!FRAME_WINDOW_P (it->f))
4497 return 0;
4498
4499 it->font_height = XCAR (XCDR (spec));
4500 if (!NILP (it->font_height))
4501 {
4502 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4503 int new_height = -1;
4504
4505 if (CONSP (it->font_height)
4506 && (EQ (XCAR (it->font_height), Qplus)
4507 || EQ (XCAR (it->font_height), Qminus))
4508 && CONSP (XCDR (it->font_height))
4509 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4510 {
4511 /* `(+ N)' or `(- N)' where N is an integer. */
4512 int steps = XINT (XCAR (XCDR (it->font_height)));
4513 if (EQ (XCAR (it->font_height), Qplus))
4514 steps = - steps;
4515 it->face_id = smaller_face (it->f, it->face_id, steps);
4516 }
4517 else if (FUNCTIONP (it->font_height))
4518 {
4519 /* Call function with current height as argument.
4520 Value is the new height. */
4521 Lisp_Object height;
4522 height = safe_call1 (it->font_height,
4523 face->lface[LFACE_HEIGHT_INDEX]);
4524 if (NUMBERP (height))
4525 new_height = XFLOATINT (height);
4526 }
4527 else if (NUMBERP (it->font_height))
4528 {
4529 /* Value is a multiple of the canonical char height. */
4530 struct face *f;
4531
4532 f = FACE_FROM_ID (it->f,
4533 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4534 new_height = (XFLOATINT (it->font_height)
4535 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4536 }
4537 else
4538 {
4539 /* Evaluate IT->font_height with `height' bound to the
4540 current specified height to get the new height. */
4541 ptrdiff_t count = SPECPDL_INDEX ();
4542
4543 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4544 value = safe_eval (it->font_height);
4545 unbind_to (count, Qnil);
4546
4547 if (NUMBERP (value))
4548 new_height = XFLOATINT (value);
4549 }
4550
4551 if (new_height > 0)
4552 it->face_id = face_with_height (it->f, it->face_id, new_height);
4553 }
4554 }
4555
4556 return 0;
4557 }
4558
4559 /* Handle `(space-width WIDTH)'. */
4560 if (CONSP (spec)
4561 && EQ (XCAR (spec), Qspace_width)
4562 && CONSP (XCDR (spec)))
4563 {
4564 if (it)
4565 {
4566 if (!FRAME_WINDOW_P (it->f))
4567 return 0;
4568
4569 value = XCAR (XCDR (spec));
4570 if (NUMBERP (value) && XFLOATINT (value) > 0)
4571 it->space_width = value;
4572 }
4573
4574 return 0;
4575 }
4576
4577 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4578 if (CONSP (spec)
4579 && EQ (XCAR (spec), Qslice))
4580 {
4581 Lisp_Object tem;
4582
4583 if (it)
4584 {
4585 if (!FRAME_WINDOW_P (it->f))
4586 return 0;
4587
4588 if (tem = XCDR (spec), CONSP (tem))
4589 {
4590 it->slice.x = XCAR (tem);
4591 if (tem = XCDR (tem), CONSP (tem))
4592 {
4593 it->slice.y = XCAR (tem);
4594 if (tem = XCDR (tem), CONSP (tem))
4595 {
4596 it->slice.width = XCAR (tem);
4597 if (tem = XCDR (tem), CONSP (tem))
4598 it->slice.height = XCAR (tem);
4599 }
4600 }
4601 }
4602 }
4603
4604 return 0;
4605 }
4606
4607 /* Handle `(raise FACTOR)'. */
4608 if (CONSP (spec)
4609 && EQ (XCAR (spec), Qraise)
4610 && CONSP (XCDR (spec)))
4611 {
4612 if (it)
4613 {
4614 if (!FRAME_WINDOW_P (it->f))
4615 return 0;
4616
4617 #ifdef HAVE_WINDOW_SYSTEM
4618 value = XCAR (XCDR (spec));
4619 if (NUMBERP (value))
4620 {
4621 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4622 it->voffset = - (XFLOATINT (value)
4623 * (FONT_HEIGHT (face->font)));
4624 }
4625 #endif /* HAVE_WINDOW_SYSTEM */
4626 }
4627
4628 return 0;
4629 }
4630
4631 /* Don't handle the other kinds of display specifications
4632 inside a string that we got from a `display' property. */
4633 if (it && it->string_from_display_prop_p)
4634 return 0;
4635
4636 /* Characters having this form of property are not displayed, so
4637 we have to find the end of the property. */
4638 if (it)
4639 {
4640 start_pos = *position;
4641 *position = display_prop_end (it, object, start_pos);
4642 }
4643 value = Qnil;
4644
4645 /* Stop the scan at that end position--we assume that all
4646 text properties change there. */
4647 if (it)
4648 it->stop_charpos = position->charpos;
4649
4650 /* Handle `(left-fringe BITMAP [FACE])'
4651 and `(right-fringe BITMAP [FACE])'. */
4652 if (CONSP (spec)
4653 && (EQ (XCAR (spec), Qleft_fringe)
4654 || EQ (XCAR (spec), Qright_fringe))
4655 && CONSP (XCDR (spec)))
4656 {
4657 int fringe_bitmap;
4658
4659 if (it)
4660 {
4661 if (!FRAME_WINDOW_P (it->f))
4662 /* If we return here, POSITION has been advanced
4663 across the text with this property. */
4664 return 0;
4665 }
4666 else if (!frame_window_p)
4667 return 0;
4668
4669 #ifdef HAVE_WINDOW_SYSTEM
4670 value = XCAR (XCDR (spec));
4671 if (!SYMBOLP (value)
4672 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4673 /* If we return here, POSITION has been advanced
4674 across the text with this property. */
4675 return 0;
4676
4677 if (it)
4678 {
4679 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4680
4681 if (CONSP (XCDR (XCDR (spec))))
4682 {
4683 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4684 int face_id2 = lookup_derived_face (it->f, face_name,
4685 FRINGE_FACE_ID, 0);
4686 if (face_id2 >= 0)
4687 face_id = face_id2;
4688 }
4689
4690 /* Save current settings of IT so that we can restore them
4691 when we are finished with the glyph property value. */
4692 push_it (it, position);
4693
4694 it->area = TEXT_AREA;
4695 it->what = IT_IMAGE;
4696 it->image_id = -1; /* no image */
4697 it->position = start_pos;
4698 it->object = NILP (object) ? it->w->buffer : object;
4699 it->method = GET_FROM_IMAGE;
4700 it->from_overlay = Qnil;
4701 it->face_id = face_id;
4702 it->from_disp_prop_p = 1;
4703
4704 /* Say that we haven't consumed the characters with
4705 `display' property yet. The call to pop_it in
4706 set_iterator_to_next will clean this up. */
4707 *position = start_pos;
4708
4709 if (EQ (XCAR (spec), Qleft_fringe))
4710 {
4711 it->left_user_fringe_bitmap = fringe_bitmap;
4712 it->left_user_fringe_face_id = face_id;
4713 }
4714 else
4715 {
4716 it->right_user_fringe_bitmap = fringe_bitmap;
4717 it->right_user_fringe_face_id = face_id;
4718 }
4719 }
4720 #endif /* HAVE_WINDOW_SYSTEM */
4721 return 1;
4722 }
4723
4724 /* Prepare to handle `((margin left-margin) ...)',
4725 `((margin right-margin) ...)' and `((margin nil) ...)'
4726 prefixes for display specifications. */
4727 location = Qunbound;
4728 if (CONSP (spec) && CONSP (XCAR (spec)))
4729 {
4730 Lisp_Object tem;
4731
4732 value = XCDR (spec);
4733 if (CONSP (value))
4734 value = XCAR (value);
4735
4736 tem = XCAR (spec);
4737 if (EQ (XCAR (tem), Qmargin)
4738 && (tem = XCDR (tem),
4739 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4740 (NILP (tem)
4741 || EQ (tem, Qleft_margin)
4742 || EQ (tem, Qright_margin))))
4743 location = tem;
4744 }
4745
4746 if (EQ (location, Qunbound))
4747 {
4748 location = Qnil;
4749 value = spec;
4750 }
4751
4752 /* After this point, VALUE is the property after any
4753 margin prefix has been stripped. It must be a string,
4754 an image specification, or `(space ...)'.
4755
4756 LOCATION specifies where to display: `left-margin',
4757 `right-margin' or nil. */
4758
4759 valid_p = (STRINGP (value)
4760 #ifdef HAVE_WINDOW_SYSTEM
4761 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4762 && valid_image_p (value))
4763 #endif /* not HAVE_WINDOW_SYSTEM */
4764 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4765
4766 if (valid_p && !display_replaced_p)
4767 {
4768 int retval = 1;
4769
4770 if (!it)
4771 {
4772 /* Callers need to know whether the display spec is any kind
4773 of `(space ...)' spec that is about to affect text-area
4774 display. */
4775 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4776 retval = 2;
4777 return retval;
4778 }
4779
4780 /* Save current settings of IT so that we can restore them
4781 when we are finished with the glyph property value. */
4782 push_it (it, position);
4783 it->from_overlay = overlay;
4784 it->from_disp_prop_p = 1;
4785
4786 if (NILP (location))
4787 it->area = TEXT_AREA;
4788 else if (EQ (location, Qleft_margin))
4789 it->area = LEFT_MARGIN_AREA;
4790 else
4791 it->area = RIGHT_MARGIN_AREA;
4792
4793 if (STRINGP (value))
4794 {
4795 it->string = value;
4796 it->multibyte_p = STRING_MULTIBYTE (it->string);
4797 it->current.overlay_string_index = -1;
4798 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4799 it->end_charpos = it->string_nchars = SCHARS (it->string);
4800 it->method = GET_FROM_STRING;
4801 it->stop_charpos = 0;
4802 it->prev_stop = 0;
4803 it->base_level_stop = 0;
4804 it->string_from_display_prop_p = 1;
4805 /* Say that we haven't consumed the characters with
4806 `display' property yet. The call to pop_it in
4807 set_iterator_to_next will clean this up. */
4808 if (BUFFERP (object))
4809 *position = start_pos;
4810
4811 /* Force paragraph direction to be that of the parent
4812 object. If the parent object's paragraph direction is
4813 not yet determined, default to L2R. */
4814 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4815 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4816 else
4817 it->paragraph_embedding = L2R;
4818
4819 /* Set up the bidi iterator for this display string. */
4820 if (it->bidi_p)
4821 {
4822 it->bidi_it.string.lstring = it->string;
4823 it->bidi_it.string.s = NULL;
4824 it->bidi_it.string.schars = it->end_charpos;
4825 it->bidi_it.string.bufpos = bufpos;
4826 it->bidi_it.string.from_disp_str = 1;
4827 it->bidi_it.string.unibyte = !it->multibyte_p;
4828 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4829 }
4830 }
4831 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4832 {
4833 it->method = GET_FROM_STRETCH;
4834 it->object = value;
4835 *position = it->position = start_pos;
4836 retval = 1 + (it->area == TEXT_AREA);
4837 }
4838 #ifdef HAVE_WINDOW_SYSTEM
4839 else
4840 {
4841 it->what = IT_IMAGE;
4842 it->image_id = lookup_image (it->f, value);
4843 it->position = start_pos;
4844 it->object = NILP (object) ? it->w->buffer : object;
4845 it->method = GET_FROM_IMAGE;
4846
4847 /* Say that we haven't consumed the characters with
4848 `display' property yet. The call to pop_it in
4849 set_iterator_to_next will clean this up. */
4850 *position = start_pos;
4851 }
4852 #endif /* HAVE_WINDOW_SYSTEM */
4853
4854 return retval;
4855 }
4856
4857 /* Invalid property or property not supported. Restore
4858 POSITION to what it was before. */
4859 *position = start_pos;
4860 return 0;
4861 }
4862
4863 /* Check if PROP is a display property value whose text should be
4864 treated as intangible. OVERLAY is the overlay from which PROP
4865 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4866 specify the buffer position covered by PROP. */
4867
4868 int
4869 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4870 ptrdiff_t charpos, ptrdiff_t bytepos)
4871 {
4872 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4873 struct text_pos position;
4874
4875 SET_TEXT_POS (position, charpos, bytepos);
4876 return handle_display_spec (NULL, prop, Qnil, overlay,
4877 &position, charpos, frame_window_p);
4878 }
4879
4880
4881 /* Return 1 if PROP is a display sub-property value containing STRING.
4882
4883 Implementation note: this and the following function are really
4884 special cases of handle_display_spec and
4885 handle_single_display_spec, and should ideally use the same code.
4886 Until they do, these two pairs must be consistent and must be
4887 modified in sync. */
4888
4889 static int
4890 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4891 {
4892 if (EQ (string, prop))
4893 return 1;
4894
4895 /* Skip over `when FORM'. */
4896 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4897 {
4898 prop = XCDR (prop);
4899 if (!CONSP (prop))
4900 return 0;
4901 /* Actually, the condition following `when' should be eval'ed,
4902 like handle_single_display_spec does, and we should return
4903 zero if it evaluates to nil. However, this function is
4904 called only when the buffer was already displayed and some
4905 glyph in the glyph matrix was found to come from a display
4906 string. Therefore, the condition was already evaluated, and
4907 the result was non-nil, otherwise the display string wouldn't
4908 have been displayed and we would have never been called for
4909 this property. Thus, we can skip the evaluation and assume
4910 its result is non-nil. */
4911 prop = XCDR (prop);
4912 }
4913
4914 if (CONSP (prop))
4915 /* Skip over `margin LOCATION'. */
4916 if (EQ (XCAR (prop), Qmargin))
4917 {
4918 prop = XCDR (prop);
4919 if (!CONSP (prop))
4920 return 0;
4921
4922 prop = XCDR (prop);
4923 if (!CONSP (prop))
4924 return 0;
4925 }
4926
4927 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
4928 }
4929
4930
4931 /* Return 1 if STRING appears in the `display' property PROP. */
4932
4933 static int
4934 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
4935 {
4936 if (CONSP (prop)
4937 && !EQ (XCAR (prop), Qwhen)
4938 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
4939 {
4940 /* A list of sub-properties. */
4941 while (CONSP (prop))
4942 {
4943 if (single_display_spec_string_p (XCAR (prop), string))
4944 return 1;
4945 prop = XCDR (prop);
4946 }
4947 }
4948 else if (VECTORP (prop))
4949 {
4950 /* A vector of sub-properties. */
4951 ptrdiff_t i;
4952 for (i = 0; i < ASIZE (prop); ++i)
4953 if (single_display_spec_string_p (AREF (prop, i), string))
4954 return 1;
4955 }
4956 else
4957 return single_display_spec_string_p (prop, string);
4958
4959 return 0;
4960 }
4961
4962 /* Look for STRING in overlays and text properties in the current
4963 buffer, between character positions FROM and TO (excluding TO).
4964 BACK_P non-zero means look back (in this case, TO is supposed to be
4965 less than FROM).
4966 Value is the first character position where STRING was found, or
4967 zero if it wasn't found before hitting TO.
4968
4969 This function may only use code that doesn't eval because it is
4970 called asynchronously from note_mouse_highlight. */
4971
4972 static ptrdiff_t
4973 string_buffer_position_lim (Lisp_Object string,
4974 ptrdiff_t from, ptrdiff_t to, int back_p)
4975 {
4976 Lisp_Object limit, prop, pos;
4977 int found = 0;
4978
4979 pos = make_number (from);
4980
4981 if (!back_p) /* looking forward */
4982 {
4983 limit = make_number (min (to, ZV));
4984 while (!found && !EQ (pos, limit))
4985 {
4986 prop = Fget_char_property (pos, Qdisplay, Qnil);
4987 if (!NILP (prop) && display_prop_string_p (prop, string))
4988 found = 1;
4989 else
4990 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
4991 limit);
4992 }
4993 }
4994 else /* looking back */
4995 {
4996 limit = make_number (max (to, BEGV));
4997 while (!found && !EQ (pos, limit))
4998 {
4999 prop = Fget_char_property (pos, Qdisplay, Qnil);
5000 if (!NILP (prop) && display_prop_string_p (prop, string))
5001 found = 1;
5002 else
5003 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5004 limit);
5005 }
5006 }
5007
5008 return found ? XINT (pos) : 0;
5009 }
5010
5011 /* Determine which buffer position in current buffer STRING comes from.
5012 AROUND_CHARPOS is an approximate position where it could come from.
5013 Value is the buffer position or 0 if it couldn't be determined.
5014
5015 This function is necessary because we don't record buffer positions
5016 in glyphs generated from strings (to keep struct glyph small).
5017 This function may only use code that doesn't eval because it is
5018 called asynchronously from note_mouse_highlight. */
5019
5020 static ptrdiff_t
5021 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5022 {
5023 const int MAX_DISTANCE = 1000;
5024 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5025 around_charpos + MAX_DISTANCE,
5026 0);
5027
5028 if (!found)
5029 found = string_buffer_position_lim (string, around_charpos,
5030 around_charpos - MAX_DISTANCE, 1);
5031 return found;
5032 }
5033
5034
5035 \f
5036 /***********************************************************************
5037 `composition' property
5038 ***********************************************************************/
5039
5040 /* Set up iterator IT from `composition' property at its current
5041 position. Called from handle_stop. */
5042
5043 static enum prop_handled
5044 handle_composition_prop (struct it *it)
5045 {
5046 Lisp_Object prop, string;
5047 ptrdiff_t pos, pos_byte, start, end;
5048
5049 if (STRINGP (it->string))
5050 {
5051 unsigned char *s;
5052
5053 pos = IT_STRING_CHARPOS (*it);
5054 pos_byte = IT_STRING_BYTEPOS (*it);
5055 string = it->string;
5056 s = SDATA (string) + pos_byte;
5057 it->c = STRING_CHAR (s);
5058 }
5059 else
5060 {
5061 pos = IT_CHARPOS (*it);
5062 pos_byte = IT_BYTEPOS (*it);
5063 string = Qnil;
5064 it->c = FETCH_CHAR (pos_byte);
5065 }
5066
5067 /* If there's a valid composition and point is not inside of the
5068 composition (in the case that the composition is from the current
5069 buffer), draw a glyph composed from the composition components. */
5070 if (find_composition (pos, -1, &start, &end, &prop, string)
5071 && COMPOSITION_VALID_P (start, end, prop)
5072 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5073 {
5074 if (start < pos)
5075 /* As we can't handle this situation (perhaps font-lock added
5076 a new composition), we just return here hoping that next
5077 redisplay will detect this composition much earlier. */
5078 return HANDLED_NORMALLY;
5079 if (start != pos)
5080 {
5081 if (STRINGP (it->string))
5082 pos_byte = string_char_to_byte (it->string, start);
5083 else
5084 pos_byte = CHAR_TO_BYTE (start);
5085 }
5086 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5087 prop, string);
5088
5089 if (it->cmp_it.id >= 0)
5090 {
5091 it->cmp_it.ch = -1;
5092 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5093 it->cmp_it.nglyphs = -1;
5094 }
5095 }
5096
5097 return HANDLED_NORMALLY;
5098 }
5099
5100
5101 \f
5102 /***********************************************************************
5103 Overlay strings
5104 ***********************************************************************/
5105
5106 /* The following structure is used to record overlay strings for
5107 later sorting in load_overlay_strings. */
5108
5109 struct overlay_entry
5110 {
5111 Lisp_Object overlay;
5112 Lisp_Object string;
5113 EMACS_INT priority;
5114 int after_string_p;
5115 };
5116
5117
5118 /* Set up iterator IT from overlay strings at its current position.
5119 Called from handle_stop. */
5120
5121 static enum prop_handled
5122 handle_overlay_change (struct it *it)
5123 {
5124 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5125 return HANDLED_RECOMPUTE_PROPS;
5126 else
5127 return HANDLED_NORMALLY;
5128 }
5129
5130
5131 /* Set up the next overlay string for delivery by IT, if there is an
5132 overlay string to deliver. Called by set_iterator_to_next when the
5133 end of the current overlay string is reached. If there are more
5134 overlay strings to display, IT->string and
5135 IT->current.overlay_string_index are set appropriately here.
5136 Otherwise IT->string is set to nil. */
5137
5138 static void
5139 next_overlay_string (struct it *it)
5140 {
5141 ++it->current.overlay_string_index;
5142 if (it->current.overlay_string_index == it->n_overlay_strings)
5143 {
5144 /* No more overlay strings. Restore IT's settings to what
5145 they were before overlay strings were processed, and
5146 continue to deliver from current_buffer. */
5147
5148 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5149 pop_it (it);
5150 xassert (it->sp > 0
5151 || (NILP (it->string)
5152 && it->method == GET_FROM_BUFFER
5153 && it->stop_charpos >= BEGV
5154 && it->stop_charpos <= it->end_charpos));
5155 it->current.overlay_string_index = -1;
5156 it->n_overlay_strings = 0;
5157 it->overlay_strings_charpos = -1;
5158
5159 /* If we're at the end of the buffer, record that we have
5160 processed the overlay strings there already, so that
5161 next_element_from_buffer doesn't try it again. */
5162 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5163 it->overlay_strings_at_end_processed_p = 1;
5164 }
5165 else
5166 {
5167 /* There are more overlay strings to process. If
5168 IT->current.overlay_string_index has advanced to a position
5169 where we must load IT->overlay_strings with more strings, do
5170 it. We must load at the IT->overlay_strings_charpos where
5171 IT->n_overlay_strings was originally computed; when invisible
5172 text is present, this might not be IT_CHARPOS (Bug#7016). */
5173 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5174
5175 if (it->current.overlay_string_index && i == 0)
5176 load_overlay_strings (it, it->overlay_strings_charpos);
5177
5178 /* Initialize IT to deliver display elements from the overlay
5179 string. */
5180 it->string = it->overlay_strings[i];
5181 it->multibyte_p = STRING_MULTIBYTE (it->string);
5182 SET_TEXT_POS (it->current.string_pos, 0, 0);
5183 it->method = GET_FROM_STRING;
5184 it->stop_charpos = 0;
5185 if (it->cmp_it.stop_pos >= 0)
5186 it->cmp_it.stop_pos = 0;
5187 it->prev_stop = 0;
5188 it->base_level_stop = 0;
5189
5190 /* Set up the bidi iterator for this overlay string. */
5191 if (it->bidi_p)
5192 {
5193 it->bidi_it.string.lstring = it->string;
5194 it->bidi_it.string.s = NULL;
5195 it->bidi_it.string.schars = SCHARS (it->string);
5196 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5197 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5198 it->bidi_it.string.unibyte = !it->multibyte_p;
5199 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5200 }
5201 }
5202
5203 CHECK_IT (it);
5204 }
5205
5206
5207 /* Compare two overlay_entry structures E1 and E2. Used as a
5208 comparison function for qsort in load_overlay_strings. Overlay
5209 strings for the same position are sorted so that
5210
5211 1. All after-strings come in front of before-strings, except
5212 when they come from the same overlay.
5213
5214 2. Within after-strings, strings are sorted so that overlay strings
5215 from overlays with higher priorities come first.
5216
5217 2. Within before-strings, strings are sorted so that overlay
5218 strings from overlays with higher priorities come last.
5219
5220 Value is analogous to strcmp. */
5221
5222
5223 static int
5224 compare_overlay_entries (const void *e1, const void *e2)
5225 {
5226 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5227 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5228 int result;
5229
5230 if (entry1->after_string_p != entry2->after_string_p)
5231 {
5232 /* Let after-strings appear in front of before-strings if
5233 they come from different overlays. */
5234 if (EQ (entry1->overlay, entry2->overlay))
5235 result = entry1->after_string_p ? 1 : -1;
5236 else
5237 result = entry1->after_string_p ? -1 : 1;
5238 }
5239 else if (entry1->priority != entry2->priority)
5240 {
5241 if (entry1->after_string_p)
5242 /* After-strings sorted in order of decreasing priority. */
5243 result = entry2->priority < entry1->priority ? -1 : 1;
5244 else
5245 /* Before-strings sorted in order of increasing priority. */
5246 result = entry1->priority < entry2->priority ? -1 : 1;
5247 }
5248 else
5249 result = 0;
5250
5251 return result;
5252 }
5253
5254
5255 /* Load the vector IT->overlay_strings with overlay strings from IT's
5256 current buffer position, or from CHARPOS if that is > 0. Set
5257 IT->n_overlays to the total number of overlay strings found.
5258
5259 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5260 a time. On entry into load_overlay_strings,
5261 IT->current.overlay_string_index gives the number of overlay
5262 strings that have already been loaded by previous calls to this
5263 function.
5264
5265 IT->add_overlay_start contains an additional overlay start
5266 position to consider for taking overlay strings from, if non-zero.
5267 This position comes into play when the overlay has an `invisible'
5268 property, and both before and after-strings. When we've skipped to
5269 the end of the overlay, because of its `invisible' property, we
5270 nevertheless want its before-string to appear.
5271 IT->add_overlay_start will contain the overlay start position
5272 in this case.
5273
5274 Overlay strings are sorted so that after-string strings come in
5275 front of before-string strings. Within before and after-strings,
5276 strings are sorted by overlay priority. See also function
5277 compare_overlay_entries. */
5278
5279 static void
5280 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5281 {
5282 Lisp_Object overlay, window, str, invisible;
5283 struct Lisp_Overlay *ov;
5284 ptrdiff_t start, end;
5285 ptrdiff_t size = 20;
5286 ptrdiff_t n = 0, i, j;
5287 int invis_p;
5288 struct overlay_entry *entries
5289 = (struct overlay_entry *) alloca (size * sizeof *entries);
5290 USE_SAFE_ALLOCA;
5291
5292 if (charpos <= 0)
5293 charpos = IT_CHARPOS (*it);
5294
5295 /* Append the overlay string STRING of overlay OVERLAY to vector
5296 `entries' which has size `size' and currently contains `n'
5297 elements. AFTER_P non-zero means STRING is an after-string of
5298 OVERLAY. */
5299 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5300 do \
5301 { \
5302 Lisp_Object priority; \
5303 \
5304 if (n == size) \
5305 { \
5306 struct overlay_entry *old = entries; \
5307 SAFE_NALLOCA (entries, 2, size); \
5308 memcpy (entries, old, size * sizeof *entries); \
5309 size *= 2; \
5310 } \
5311 \
5312 entries[n].string = (STRING); \
5313 entries[n].overlay = (OVERLAY); \
5314 priority = Foverlay_get ((OVERLAY), Qpriority); \
5315 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5316 entries[n].after_string_p = (AFTER_P); \
5317 ++n; \
5318 } \
5319 while (0)
5320
5321 /* Process overlay before the overlay center. */
5322 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5323 {
5324 XSETMISC (overlay, ov);
5325 xassert (OVERLAYP (overlay));
5326 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5327 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5328
5329 if (end < charpos)
5330 break;
5331
5332 /* Skip this overlay if it doesn't start or end at IT's current
5333 position. */
5334 if (end != charpos && start != charpos)
5335 continue;
5336
5337 /* Skip this overlay if it doesn't apply to IT->w. */
5338 window = Foverlay_get (overlay, Qwindow);
5339 if (WINDOWP (window) && XWINDOW (window) != it->w)
5340 continue;
5341
5342 /* If the text ``under'' the overlay is invisible, both before-
5343 and after-strings from this overlay are visible; start and
5344 end position are indistinguishable. */
5345 invisible = Foverlay_get (overlay, Qinvisible);
5346 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5347
5348 /* If overlay has a non-empty before-string, record it. */
5349 if ((start == charpos || (end == charpos && invis_p))
5350 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5351 && SCHARS (str))
5352 RECORD_OVERLAY_STRING (overlay, str, 0);
5353
5354 /* If overlay has a non-empty after-string, record it. */
5355 if ((end == charpos || (start == charpos && invis_p))
5356 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5357 && SCHARS (str))
5358 RECORD_OVERLAY_STRING (overlay, str, 1);
5359 }
5360
5361 /* Process overlays after the overlay center. */
5362 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5363 {
5364 XSETMISC (overlay, ov);
5365 xassert (OVERLAYP (overlay));
5366 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5367 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5368
5369 if (start > charpos)
5370 break;
5371
5372 /* Skip this overlay if it doesn't start or end at IT's current
5373 position. */
5374 if (end != charpos && start != charpos)
5375 continue;
5376
5377 /* Skip this overlay if it doesn't apply to IT->w. */
5378 window = Foverlay_get (overlay, Qwindow);
5379 if (WINDOWP (window) && XWINDOW (window) != it->w)
5380 continue;
5381
5382 /* If the text ``under'' the overlay is invisible, it has a zero
5383 dimension, and both before- and after-strings apply. */
5384 invisible = Foverlay_get (overlay, Qinvisible);
5385 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5386
5387 /* If overlay has a non-empty before-string, record it. */
5388 if ((start == charpos || (end == charpos && invis_p))
5389 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5390 && SCHARS (str))
5391 RECORD_OVERLAY_STRING (overlay, str, 0);
5392
5393 /* If overlay has a non-empty after-string, record it. */
5394 if ((end == charpos || (start == charpos && invis_p))
5395 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5396 && SCHARS (str))
5397 RECORD_OVERLAY_STRING (overlay, str, 1);
5398 }
5399
5400 #undef RECORD_OVERLAY_STRING
5401
5402 /* Sort entries. */
5403 if (n > 1)
5404 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5405
5406 /* Record number of overlay strings, and where we computed it. */
5407 it->n_overlay_strings = n;
5408 it->overlay_strings_charpos = charpos;
5409
5410 /* IT->current.overlay_string_index is the number of overlay strings
5411 that have already been consumed by IT. Copy some of the
5412 remaining overlay strings to IT->overlay_strings. */
5413 i = 0;
5414 j = it->current.overlay_string_index;
5415 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5416 {
5417 it->overlay_strings[i] = entries[j].string;
5418 it->string_overlays[i++] = entries[j++].overlay;
5419 }
5420
5421 CHECK_IT (it);
5422 SAFE_FREE ();
5423 }
5424
5425
5426 /* Get the first chunk of overlay strings at IT's current buffer
5427 position, or at CHARPOS if that is > 0. Value is non-zero if at
5428 least one overlay string was found. */
5429
5430 static int
5431 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5432 {
5433 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5434 process. This fills IT->overlay_strings with strings, and sets
5435 IT->n_overlay_strings to the total number of strings to process.
5436 IT->pos.overlay_string_index has to be set temporarily to zero
5437 because load_overlay_strings needs this; it must be set to -1
5438 when no overlay strings are found because a zero value would
5439 indicate a position in the first overlay string. */
5440 it->current.overlay_string_index = 0;
5441 load_overlay_strings (it, charpos);
5442
5443 /* If we found overlay strings, set up IT to deliver display
5444 elements from the first one. Otherwise set up IT to deliver
5445 from current_buffer. */
5446 if (it->n_overlay_strings)
5447 {
5448 /* Make sure we know settings in current_buffer, so that we can
5449 restore meaningful values when we're done with the overlay
5450 strings. */
5451 if (compute_stop_p)
5452 compute_stop_pos (it);
5453 xassert (it->face_id >= 0);
5454
5455 /* Save IT's settings. They are restored after all overlay
5456 strings have been processed. */
5457 xassert (!compute_stop_p || it->sp == 0);
5458
5459 /* When called from handle_stop, there might be an empty display
5460 string loaded. In that case, don't bother saving it. */
5461 if (!STRINGP (it->string) || SCHARS (it->string))
5462 push_it (it, NULL);
5463
5464 /* Set up IT to deliver display elements from the first overlay
5465 string. */
5466 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5467 it->string = it->overlay_strings[0];
5468 it->from_overlay = Qnil;
5469 it->stop_charpos = 0;
5470 xassert (STRINGP (it->string));
5471 it->end_charpos = SCHARS (it->string);
5472 it->prev_stop = 0;
5473 it->base_level_stop = 0;
5474 it->multibyte_p = STRING_MULTIBYTE (it->string);
5475 it->method = GET_FROM_STRING;
5476 it->from_disp_prop_p = 0;
5477
5478 /* Force paragraph direction to be that of the parent
5479 buffer. */
5480 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5481 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5482 else
5483 it->paragraph_embedding = L2R;
5484
5485 /* Set up the bidi iterator for this overlay string. */
5486 if (it->bidi_p)
5487 {
5488 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5489
5490 it->bidi_it.string.lstring = it->string;
5491 it->bidi_it.string.s = NULL;
5492 it->bidi_it.string.schars = SCHARS (it->string);
5493 it->bidi_it.string.bufpos = pos;
5494 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5495 it->bidi_it.string.unibyte = !it->multibyte_p;
5496 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5497 }
5498 return 1;
5499 }
5500
5501 it->current.overlay_string_index = -1;
5502 return 0;
5503 }
5504
5505 static int
5506 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5507 {
5508 it->string = Qnil;
5509 it->method = GET_FROM_BUFFER;
5510
5511 (void) get_overlay_strings_1 (it, charpos, 1);
5512
5513 CHECK_IT (it);
5514
5515 /* Value is non-zero if we found at least one overlay string. */
5516 return STRINGP (it->string);
5517 }
5518
5519
5520 \f
5521 /***********************************************************************
5522 Saving and restoring state
5523 ***********************************************************************/
5524
5525 /* Save current settings of IT on IT->stack. Called, for example,
5526 before setting up IT for an overlay string, to be able to restore
5527 IT's settings to what they were after the overlay string has been
5528 processed. If POSITION is non-NULL, it is the position to save on
5529 the stack instead of IT->position. */
5530
5531 static void
5532 push_it (struct it *it, struct text_pos *position)
5533 {
5534 struct iterator_stack_entry *p;
5535
5536 xassert (it->sp < IT_STACK_SIZE);
5537 p = it->stack + it->sp;
5538
5539 p->stop_charpos = it->stop_charpos;
5540 p->prev_stop = it->prev_stop;
5541 p->base_level_stop = it->base_level_stop;
5542 p->cmp_it = it->cmp_it;
5543 xassert (it->face_id >= 0);
5544 p->face_id = it->face_id;
5545 p->string = it->string;
5546 p->method = it->method;
5547 p->from_overlay = it->from_overlay;
5548 switch (p->method)
5549 {
5550 case GET_FROM_IMAGE:
5551 p->u.image.object = it->object;
5552 p->u.image.image_id = it->image_id;
5553 p->u.image.slice = it->slice;
5554 break;
5555 case GET_FROM_STRETCH:
5556 p->u.stretch.object = it->object;
5557 break;
5558 }
5559 p->position = position ? *position : it->position;
5560 p->current = it->current;
5561 p->end_charpos = it->end_charpos;
5562 p->string_nchars = it->string_nchars;
5563 p->area = it->area;
5564 p->multibyte_p = it->multibyte_p;
5565 p->avoid_cursor_p = it->avoid_cursor_p;
5566 p->space_width = it->space_width;
5567 p->font_height = it->font_height;
5568 p->voffset = it->voffset;
5569 p->string_from_display_prop_p = it->string_from_display_prop_p;
5570 p->display_ellipsis_p = 0;
5571 p->line_wrap = it->line_wrap;
5572 p->bidi_p = it->bidi_p;
5573 p->paragraph_embedding = it->paragraph_embedding;
5574 p->from_disp_prop_p = it->from_disp_prop_p;
5575 ++it->sp;
5576
5577 /* Save the state of the bidi iterator as well. */
5578 if (it->bidi_p)
5579 bidi_push_it (&it->bidi_it);
5580 }
5581
5582 static void
5583 iterate_out_of_display_property (struct it *it)
5584 {
5585 int buffer_p = BUFFERP (it->object);
5586 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5587 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5588
5589 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5590
5591 /* Maybe initialize paragraph direction. If we are at the beginning
5592 of a new paragraph, next_element_from_buffer may not have a
5593 chance to do that. */
5594 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5595 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5596 /* prev_stop can be zero, so check against BEGV as well. */
5597 while (it->bidi_it.charpos >= bob
5598 && it->prev_stop <= it->bidi_it.charpos
5599 && it->bidi_it.charpos < CHARPOS (it->position)
5600 && it->bidi_it.charpos < eob)
5601 bidi_move_to_visually_next (&it->bidi_it);
5602 /* Record the stop_pos we just crossed, for when we cross it
5603 back, maybe. */
5604 if (it->bidi_it.charpos > CHARPOS (it->position))
5605 it->prev_stop = CHARPOS (it->position);
5606 /* If we ended up not where pop_it put us, resync IT's
5607 positional members with the bidi iterator. */
5608 if (it->bidi_it.charpos != CHARPOS (it->position))
5609 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5610 if (buffer_p)
5611 it->current.pos = it->position;
5612 else
5613 it->current.string_pos = it->position;
5614 }
5615
5616 /* Restore IT's settings from IT->stack. Called, for example, when no
5617 more overlay strings must be processed, and we return to delivering
5618 display elements from a buffer, or when the end of a string from a
5619 `display' property is reached and we return to delivering display
5620 elements from an overlay string, or from a buffer. */
5621
5622 static void
5623 pop_it (struct it *it)
5624 {
5625 struct iterator_stack_entry *p;
5626 int from_display_prop = it->from_disp_prop_p;
5627
5628 xassert (it->sp > 0);
5629 --it->sp;
5630 p = it->stack + it->sp;
5631 it->stop_charpos = p->stop_charpos;
5632 it->prev_stop = p->prev_stop;
5633 it->base_level_stop = p->base_level_stop;
5634 it->cmp_it = p->cmp_it;
5635 it->face_id = p->face_id;
5636 it->current = p->current;
5637 it->position = p->position;
5638 it->string = p->string;
5639 it->from_overlay = p->from_overlay;
5640 if (NILP (it->string))
5641 SET_TEXT_POS (it->current.string_pos, -1, -1);
5642 it->method = p->method;
5643 switch (it->method)
5644 {
5645 case GET_FROM_IMAGE:
5646 it->image_id = p->u.image.image_id;
5647 it->object = p->u.image.object;
5648 it->slice = p->u.image.slice;
5649 break;
5650 case GET_FROM_STRETCH:
5651 it->object = p->u.stretch.object;
5652 break;
5653 case GET_FROM_BUFFER:
5654 it->object = it->w->buffer;
5655 break;
5656 case GET_FROM_STRING:
5657 it->object = it->string;
5658 break;
5659 case GET_FROM_DISPLAY_VECTOR:
5660 if (it->s)
5661 it->method = GET_FROM_C_STRING;
5662 else if (STRINGP (it->string))
5663 it->method = GET_FROM_STRING;
5664 else
5665 {
5666 it->method = GET_FROM_BUFFER;
5667 it->object = it->w->buffer;
5668 }
5669 }
5670 it->end_charpos = p->end_charpos;
5671 it->string_nchars = p->string_nchars;
5672 it->area = p->area;
5673 it->multibyte_p = p->multibyte_p;
5674 it->avoid_cursor_p = p->avoid_cursor_p;
5675 it->space_width = p->space_width;
5676 it->font_height = p->font_height;
5677 it->voffset = p->voffset;
5678 it->string_from_display_prop_p = p->string_from_display_prop_p;
5679 it->line_wrap = p->line_wrap;
5680 it->bidi_p = p->bidi_p;
5681 it->paragraph_embedding = p->paragraph_embedding;
5682 it->from_disp_prop_p = p->from_disp_prop_p;
5683 if (it->bidi_p)
5684 {
5685 bidi_pop_it (&it->bidi_it);
5686 /* Bidi-iterate until we get out of the portion of text, if any,
5687 covered by a `display' text property or by an overlay with
5688 `display' property. (We cannot just jump there, because the
5689 internal coherency of the bidi iterator state can not be
5690 preserved across such jumps.) We also must determine the
5691 paragraph base direction if the overlay we just processed is
5692 at the beginning of a new paragraph. */
5693 if (from_display_prop
5694 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5695 iterate_out_of_display_property (it);
5696
5697 xassert ((BUFFERP (it->object)
5698 && IT_CHARPOS (*it) == it->bidi_it.charpos
5699 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5700 || (STRINGP (it->object)
5701 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5702 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5703 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5704 }
5705 }
5706
5707
5708 \f
5709 /***********************************************************************
5710 Moving over lines
5711 ***********************************************************************/
5712
5713 /* Set IT's current position to the previous line start. */
5714
5715 static void
5716 back_to_previous_line_start (struct it *it)
5717 {
5718 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5719 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5720 }
5721
5722
5723 /* Move IT to the next line start.
5724
5725 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5726 we skipped over part of the text (as opposed to moving the iterator
5727 continuously over the text). Otherwise, don't change the value
5728 of *SKIPPED_P.
5729
5730 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5731 iterator on the newline, if it was found.
5732
5733 Newlines may come from buffer text, overlay strings, or strings
5734 displayed via the `display' property. That's the reason we can't
5735 simply use find_next_newline_no_quit.
5736
5737 Note that this function may not skip over invisible text that is so
5738 because of text properties and immediately follows a newline. If
5739 it would, function reseat_at_next_visible_line_start, when called
5740 from set_iterator_to_next, would effectively make invisible
5741 characters following a newline part of the wrong glyph row, which
5742 leads to wrong cursor motion. */
5743
5744 static int
5745 forward_to_next_line_start (struct it *it, int *skipped_p,
5746 struct bidi_it *bidi_it_prev)
5747 {
5748 ptrdiff_t old_selective;
5749 int newline_found_p, n;
5750 const int MAX_NEWLINE_DISTANCE = 500;
5751
5752 /* If already on a newline, just consume it to avoid unintended
5753 skipping over invisible text below. */
5754 if (it->what == IT_CHARACTER
5755 && it->c == '\n'
5756 && CHARPOS (it->position) == IT_CHARPOS (*it))
5757 {
5758 if (it->bidi_p && bidi_it_prev)
5759 *bidi_it_prev = it->bidi_it;
5760 set_iterator_to_next (it, 0);
5761 it->c = 0;
5762 return 1;
5763 }
5764
5765 /* Don't handle selective display in the following. It's (a)
5766 unnecessary because it's done by the caller, and (b) leads to an
5767 infinite recursion because next_element_from_ellipsis indirectly
5768 calls this function. */
5769 old_selective = it->selective;
5770 it->selective = 0;
5771
5772 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5773 from buffer text. */
5774 for (n = newline_found_p = 0;
5775 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5776 n += STRINGP (it->string) ? 0 : 1)
5777 {
5778 if (!get_next_display_element (it))
5779 return 0;
5780 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5781 if (newline_found_p && it->bidi_p && bidi_it_prev)
5782 *bidi_it_prev = it->bidi_it;
5783 set_iterator_to_next (it, 0);
5784 }
5785
5786 /* If we didn't find a newline near enough, see if we can use a
5787 short-cut. */
5788 if (!newline_found_p)
5789 {
5790 ptrdiff_t start = IT_CHARPOS (*it);
5791 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5792 Lisp_Object pos;
5793
5794 xassert (!STRINGP (it->string));
5795
5796 /* If there isn't any `display' property in sight, and no
5797 overlays, we can just use the position of the newline in
5798 buffer text. */
5799 if (it->stop_charpos >= limit
5800 || ((pos = Fnext_single_property_change (make_number (start),
5801 Qdisplay, Qnil,
5802 make_number (limit)),
5803 NILP (pos))
5804 && next_overlay_change (start) == ZV))
5805 {
5806 if (!it->bidi_p)
5807 {
5808 IT_CHARPOS (*it) = limit;
5809 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5810 }
5811 else
5812 {
5813 struct bidi_it bprev;
5814
5815 /* Help bidi.c avoid expensive searches for display
5816 properties and overlays, by telling it that there are
5817 none up to `limit'. */
5818 if (it->bidi_it.disp_pos < limit)
5819 {
5820 it->bidi_it.disp_pos = limit;
5821 it->bidi_it.disp_prop = 0;
5822 }
5823 do {
5824 bprev = it->bidi_it;
5825 bidi_move_to_visually_next (&it->bidi_it);
5826 } while (it->bidi_it.charpos != limit);
5827 IT_CHARPOS (*it) = limit;
5828 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5829 if (bidi_it_prev)
5830 *bidi_it_prev = bprev;
5831 }
5832 *skipped_p = newline_found_p = 1;
5833 }
5834 else
5835 {
5836 while (get_next_display_element (it)
5837 && !newline_found_p)
5838 {
5839 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5840 if (newline_found_p && it->bidi_p && bidi_it_prev)
5841 *bidi_it_prev = it->bidi_it;
5842 set_iterator_to_next (it, 0);
5843 }
5844 }
5845 }
5846
5847 it->selective = old_selective;
5848 return newline_found_p;
5849 }
5850
5851
5852 /* Set IT's current position to the previous visible line start. Skip
5853 invisible text that is so either due to text properties or due to
5854 selective display. Caution: this does not change IT->current_x and
5855 IT->hpos. */
5856
5857 static void
5858 back_to_previous_visible_line_start (struct it *it)
5859 {
5860 while (IT_CHARPOS (*it) > BEGV)
5861 {
5862 back_to_previous_line_start (it);
5863
5864 if (IT_CHARPOS (*it) <= BEGV)
5865 break;
5866
5867 /* If selective > 0, then lines indented more than its value are
5868 invisible. */
5869 if (it->selective > 0
5870 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5871 it->selective))
5872 continue;
5873
5874 /* Check the newline before point for invisibility. */
5875 {
5876 Lisp_Object prop;
5877 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5878 Qinvisible, it->window);
5879 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5880 continue;
5881 }
5882
5883 if (IT_CHARPOS (*it) <= BEGV)
5884 break;
5885
5886 {
5887 struct it it2;
5888 void *it2data = NULL;
5889 ptrdiff_t pos;
5890 ptrdiff_t beg, end;
5891 Lisp_Object val, overlay;
5892
5893 SAVE_IT (it2, *it, it2data);
5894
5895 /* If newline is part of a composition, continue from start of composition */
5896 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
5897 && beg < IT_CHARPOS (*it))
5898 goto replaced;
5899
5900 /* If newline is replaced by a display property, find start of overlay
5901 or interval and continue search from that point. */
5902 pos = --IT_CHARPOS (it2);
5903 --IT_BYTEPOS (it2);
5904 it2.sp = 0;
5905 bidi_unshelve_cache (NULL, 0);
5906 it2.string_from_display_prop_p = 0;
5907 it2.from_disp_prop_p = 0;
5908 if (handle_display_prop (&it2) == HANDLED_RETURN
5909 && !NILP (val = get_char_property_and_overlay
5910 (make_number (pos), Qdisplay, Qnil, &overlay))
5911 && (OVERLAYP (overlay)
5912 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
5913 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
5914 {
5915 RESTORE_IT (it, it, it2data);
5916 goto replaced;
5917 }
5918
5919 /* Newline is not replaced by anything -- so we are done. */
5920 RESTORE_IT (it, it, it2data);
5921 break;
5922
5923 replaced:
5924 if (beg < BEGV)
5925 beg = BEGV;
5926 IT_CHARPOS (*it) = beg;
5927 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
5928 }
5929 }
5930
5931 it->continuation_lines_width = 0;
5932
5933 xassert (IT_CHARPOS (*it) >= BEGV);
5934 xassert (IT_CHARPOS (*it) == BEGV
5935 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5936 CHECK_IT (it);
5937 }
5938
5939
5940 /* Reseat iterator IT at the previous visible line start. Skip
5941 invisible text that is so either due to text properties or due to
5942 selective display. At the end, update IT's overlay information,
5943 face information etc. */
5944
5945 void
5946 reseat_at_previous_visible_line_start (struct it *it)
5947 {
5948 back_to_previous_visible_line_start (it);
5949 reseat (it, it->current.pos, 1);
5950 CHECK_IT (it);
5951 }
5952
5953
5954 /* Reseat iterator IT on the next visible line start in the current
5955 buffer. ON_NEWLINE_P non-zero means position IT on the newline
5956 preceding the line start. Skip over invisible text that is so
5957 because of selective display. Compute faces, overlays etc at the
5958 new position. Note that this function does not skip over text that
5959 is invisible because of text properties. */
5960
5961 static void
5962 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
5963 {
5964 int newline_found_p, skipped_p = 0;
5965 struct bidi_it bidi_it_prev;
5966
5967 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5968
5969 /* Skip over lines that are invisible because they are indented
5970 more than the value of IT->selective. */
5971 if (it->selective > 0)
5972 while (IT_CHARPOS (*it) < ZV
5973 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5974 it->selective))
5975 {
5976 xassert (IT_BYTEPOS (*it) == BEGV
5977 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
5978 newline_found_p =
5979 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
5980 }
5981
5982 /* Position on the newline if that's what's requested. */
5983 if (on_newline_p && newline_found_p)
5984 {
5985 if (STRINGP (it->string))
5986 {
5987 if (IT_STRING_CHARPOS (*it) > 0)
5988 {
5989 if (!it->bidi_p)
5990 {
5991 --IT_STRING_CHARPOS (*it);
5992 --IT_STRING_BYTEPOS (*it);
5993 }
5994 else
5995 {
5996 /* We need to restore the bidi iterator to the state
5997 it had on the newline, and resync the IT's
5998 position with that. */
5999 it->bidi_it = bidi_it_prev;
6000 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6001 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6002 }
6003 }
6004 }
6005 else if (IT_CHARPOS (*it) > BEGV)
6006 {
6007 if (!it->bidi_p)
6008 {
6009 --IT_CHARPOS (*it);
6010 --IT_BYTEPOS (*it);
6011 }
6012 else
6013 {
6014 /* We need to restore the bidi iterator to the state it
6015 had on the newline and resync IT with that. */
6016 it->bidi_it = bidi_it_prev;
6017 IT_CHARPOS (*it) = it->bidi_it.charpos;
6018 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6019 }
6020 reseat (it, it->current.pos, 0);
6021 }
6022 }
6023 else if (skipped_p)
6024 reseat (it, it->current.pos, 0);
6025
6026 CHECK_IT (it);
6027 }
6028
6029
6030 \f
6031 /***********************************************************************
6032 Changing an iterator's position
6033 ***********************************************************************/
6034
6035 /* Change IT's current position to POS in current_buffer. If FORCE_P
6036 is non-zero, always check for text properties at the new position.
6037 Otherwise, text properties are only looked up if POS >=
6038 IT->check_charpos of a property. */
6039
6040 static void
6041 reseat (struct it *it, struct text_pos pos, int force_p)
6042 {
6043 ptrdiff_t original_pos = IT_CHARPOS (*it);
6044
6045 reseat_1 (it, pos, 0);
6046
6047 /* Determine where to check text properties. Avoid doing it
6048 where possible because text property lookup is very expensive. */
6049 if (force_p
6050 || CHARPOS (pos) > it->stop_charpos
6051 || CHARPOS (pos) < original_pos)
6052 {
6053 if (it->bidi_p)
6054 {
6055 /* For bidi iteration, we need to prime prev_stop and
6056 base_level_stop with our best estimations. */
6057 /* Implementation note: Of course, POS is not necessarily a
6058 stop position, so assigning prev_pos to it is a lie; we
6059 should have called compute_stop_backwards. However, if
6060 the current buffer does not include any R2L characters,
6061 that call would be a waste of cycles, because the
6062 iterator will never move back, and thus never cross this
6063 "fake" stop position. So we delay that backward search
6064 until the time we really need it, in next_element_from_buffer. */
6065 if (CHARPOS (pos) != it->prev_stop)
6066 it->prev_stop = CHARPOS (pos);
6067 if (CHARPOS (pos) < it->base_level_stop)
6068 it->base_level_stop = 0; /* meaning it's unknown */
6069 handle_stop (it);
6070 }
6071 else
6072 {
6073 handle_stop (it);
6074 it->prev_stop = it->base_level_stop = 0;
6075 }
6076
6077 }
6078
6079 CHECK_IT (it);
6080 }
6081
6082
6083 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6084 IT->stop_pos to POS, also. */
6085
6086 static void
6087 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6088 {
6089 /* Don't call this function when scanning a C string. */
6090 xassert (it->s == NULL);
6091
6092 /* POS must be a reasonable value. */
6093 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6094
6095 it->current.pos = it->position = pos;
6096 it->end_charpos = ZV;
6097 it->dpvec = NULL;
6098 it->current.dpvec_index = -1;
6099 it->current.overlay_string_index = -1;
6100 IT_STRING_CHARPOS (*it) = -1;
6101 IT_STRING_BYTEPOS (*it) = -1;
6102 it->string = Qnil;
6103 it->method = GET_FROM_BUFFER;
6104 it->object = it->w->buffer;
6105 it->area = TEXT_AREA;
6106 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6107 it->sp = 0;
6108 it->string_from_display_prop_p = 0;
6109 it->from_disp_prop_p = 0;
6110 it->face_before_selective_p = 0;
6111 if (it->bidi_p)
6112 {
6113 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6114 &it->bidi_it);
6115 bidi_unshelve_cache (NULL, 0);
6116 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6117 it->bidi_it.string.s = NULL;
6118 it->bidi_it.string.lstring = Qnil;
6119 it->bidi_it.string.bufpos = 0;
6120 it->bidi_it.string.unibyte = 0;
6121 }
6122
6123 if (set_stop_p)
6124 {
6125 it->stop_charpos = CHARPOS (pos);
6126 it->base_level_stop = CHARPOS (pos);
6127 }
6128 }
6129
6130
6131 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6132 If S is non-null, it is a C string to iterate over. Otherwise,
6133 STRING gives a Lisp string to iterate over.
6134
6135 If PRECISION > 0, don't return more then PRECISION number of
6136 characters from the string.
6137
6138 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6139 characters have been returned. FIELD_WIDTH < 0 means an infinite
6140 field width.
6141
6142 MULTIBYTE = 0 means disable processing of multibyte characters,
6143 MULTIBYTE > 0 means enable it,
6144 MULTIBYTE < 0 means use IT->multibyte_p.
6145
6146 IT must be initialized via a prior call to init_iterator before
6147 calling this function. */
6148
6149 static void
6150 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6151 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6152 int multibyte)
6153 {
6154 /* No region in strings. */
6155 it->region_beg_charpos = it->region_end_charpos = -1;
6156
6157 /* No text property checks performed by default, but see below. */
6158 it->stop_charpos = -1;
6159
6160 /* Set iterator position and end position. */
6161 memset (&it->current, 0, sizeof it->current);
6162 it->current.overlay_string_index = -1;
6163 it->current.dpvec_index = -1;
6164 xassert (charpos >= 0);
6165
6166 /* If STRING is specified, use its multibyteness, otherwise use the
6167 setting of MULTIBYTE, if specified. */
6168 if (multibyte >= 0)
6169 it->multibyte_p = multibyte > 0;
6170
6171 /* Bidirectional reordering of strings is controlled by the default
6172 value of bidi-display-reordering. Don't try to reorder while
6173 loading loadup.el, as the necessary character property tables are
6174 not yet available. */
6175 it->bidi_p =
6176 NILP (Vpurify_flag)
6177 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6178
6179 if (s == NULL)
6180 {
6181 xassert (STRINGP (string));
6182 it->string = string;
6183 it->s = NULL;
6184 it->end_charpos = it->string_nchars = SCHARS (string);
6185 it->method = GET_FROM_STRING;
6186 it->current.string_pos = string_pos (charpos, string);
6187
6188 if (it->bidi_p)
6189 {
6190 it->bidi_it.string.lstring = string;
6191 it->bidi_it.string.s = NULL;
6192 it->bidi_it.string.schars = it->end_charpos;
6193 it->bidi_it.string.bufpos = 0;
6194 it->bidi_it.string.from_disp_str = 0;
6195 it->bidi_it.string.unibyte = !it->multibyte_p;
6196 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6197 FRAME_WINDOW_P (it->f), &it->bidi_it);
6198 }
6199 }
6200 else
6201 {
6202 it->s = (const unsigned char *) s;
6203 it->string = Qnil;
6204
6205 /* Note that we use IT->current.pos, not it->current.string_pos,
6206 for displaying C strings. */
6207 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6208 if (it->multibyte_p)
6209 {
6210 it->current.pos = c_string_pos (charpos, s, 1);
6211 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6212 }
6213 else
6214 {
6215 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6216 it->end_charpos = it->string_nchars = strlen (s);
6217 }
6218
6219 if (it->bidi_p)
6220 {
6221 it->bidi_it.string.lstring = Qnil;
6222 it->bidi_it.string.s = (const unsigned char *) s;
6223 it->bidi_it.string.schars = it->end_charpos;
6224 it->bidi_it.string.bufpos = 0;
6225 it->bidi_it.string.from_disp_str = 0;
6226 it->bidi_it.string.unibyte = !it->multibyte_p;
6227 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6228 &it->bidi_it);
6229 }
6230 it->method = GET_FROM_C_STRING;
6231 }
6232
6233 /* PRECISION > 0 means don't return more than PRECISION characters
6234 from the string. */
6235 if (precision > 0 && it->end_charpos - charpos > precision)
6236 {
6237 it->end_charpos = it->string_nchars = charpos + precision;
6238 if (it->bidi_p)
6239 it->bidi_it.string.schars = it->end_charpos;
6240 }
6241
6242 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6243 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6244 FIELD_WIDTH < 0 means infinite field width. This is useful for
6245 padding with `-' at the end of a mode line. */
6246 if (field_width < 0)
6247 field_width = INFINITY;
6248 /* Implementation note: We deliberately don't enlarge
6249 it->bidi_it.string.schars here to fit it->end_charpos, because
6250 the bidi iterator cannot produce characters out of thin air. */
6251 if (field_width > it->end_charpos - charpos)
6252 it->end_charpos = charpos + field_width;
6253
6254 /* Use the standard display table for displaying strings. */
6255 if (DISP_TABLE_P (Vstandard_display_table))
6256 it->dp = XCHAR_TABLE (Vstandard_display_table);
6257
6258 it->stop_charpos = charpos;
6259 it->prev_stop = charpos;
6260 it->base_level_stop = 0;
6261 if (it->bidi_p)
6262 {
6263 it->bidi_it.first_elt = 1;
6264 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6265 it->bidi_it.disp_pos = -1;
6266 }
6267 if (s == NULL && it->multibyte_p)
6268 {
6269 ptrdiff_t endpos = SCHARS (it->string);
6270 if (endpos > it->end_charpos)
6271 endpos = it->end_charpos;
6272 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6273 it->string);
6274 }
6275 CHECK_IT (it);
6276 }
6277
6278
6279 \f
6280 /***********************************************************************
6281 Iteration
6282 ***********************************************************************/
6283
6284 /* Map enum it_method value to corresponding next_element_from_* function. */
6285
6286 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6287 {
6288 next_element_from_buffer,
6289 next_element_from_display_vector,
6290 next_element_from_string,
6291 next_element_from_c_string,
6292 next_element_from_image,
6293 next_element_from_stretch
6294 };
6295
6296 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6297
6298
6299 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6300 (possibly with the following characters). */
6301
6302 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6303 ((IT)->cmp_it.id >= 0 \
6304 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6305 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6306 END_CHARPOS, (IT)->w, \
6307 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6308 (IT)->string)))
6309
6310
6311 /* Lookup the char-table Vglyphless_char_display for character C (-1
6312 if we want information for no-font case), and return the display
6313 method symbol. By side-effect, update it->what and
6314 it->glyphless_method. This function is called from
6315 get_next_display_element for each character element, and from
6316 x_produce_glyphs when no suitable font was found. */
6317
6318 Lisp_Object
6319 lookup_glyphless_char_display (int c, struct it *it)
6320 {
6321 Lisp_Object glyphless_method = Qnil;
6322
6323 if (CHAR_TABLE_P (Vglyphless_char_display)
6324 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6325 {
6326 if (c >= 0)
6327 {
6328 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6329 if (CONSP (glyphless_method))
6330 glyphless_method = FRAME_WINDOW_P (it->f)
6331 ? XCAR (glyphless_method)
6332 : XCDR (glyphless_method);
6333 }
6334 else
6335 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6336 }
6337
6338 retry:
6339 if (NILP (glyphless_method))
6340 {
6341 if (c >= 0)
6342 /* The default is to display the character by a proper font. */
6343 return Qnil;
6344 /* The default for the no-font case is to display an empty box. */
6345 glyphless_method = Qempty_box;
6346 }
6347 if (EQ (glyphless_method, Qzero_width))
6348 {
6349 if (c >= 0)
6350 return glyphless_method;
6351 /* This method can't be used for the no-font case. */
6352 glyphless_method = Qempty_box;
6353 }
6354 if (EQ (glyphless_method, Qthin_space))
6355 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6356 else if (EQ (glyphless_method, Qempty_box))
6357 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6358 else if (EQ (glyphless_method, Qhex_code))
6359 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6360 else if (STRINGP (glyphless_method))
6361 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6362 else
6363 {
6364 /* Invalid value. We use the default method. */
6365 glyphless_method = Qnil;
6366 goto retry;
6367 }
6368 it->what = IT_GLYPHLESS;
6369 return glyphless_method;
6370 }
6371
6372 /* Load IT's display element fields with information about the next
6373 display element from the current position of IT. Value is zero if
6374 end of buffer (or C string) is reached. */
6375
6376 static struct frame *last_escape_glyph_frame = NULL;
6377 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6378 static int last_escape_glyph_merged_face_id = 0;
6379
6380 struct frame *last_glyphless_glyph_frame = NULL;
6381 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6382 int last_glyphless_glyph_merged_face_id = 0;
6383
6384 static int
6385 get_next_display_element (struct it *it)
6386 {
6387 /* Non-zero means that we found a display element. Zero means that
6388 we hit the end of what we iterate over. Performance note: the
6389 function pointer `method' used here turns out to be faster than
6390 using a sequence of if-statements. */
6391 int success_p;
6392
6393 get_next:
6394 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6395
6396 if (it->what == IT_CHARACTER)
6397 {
6398 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6399 and only if (a) the resolved directionality of that character
6400 is R..." */
6401 /* FIXME: Do we need an exception for characters from display
6402 tables? */
6403 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6404 it->c = bidi_mirror_char (it->c);
6405 /* Map via display table or translate control characters.
6406 IT->c, IT->len etc. have been set to the next character by
6407 the function call above. If we have a display table, and it
6408 contains an entry for IT->c, translate it. Don't do this if
6409 IT->c itself comes from a display table, otherwise we could
6410 end up in an infinite recursion. (An alternative could be to
6411 count the recursion depth of this function and signal an
6412 error when a certain maximum depth is reached.) Is it worth
6413 it? */
6414 if (success_p && it->dpvec == NULL)
6415 {
6416 Lisp_Object dv;
6417 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6418 int nonascii_space_p = 0;
6419 int nonascii_hyphen_p = 0;
6420 int c = it->c; /* This is the character to display. */
6421
6422 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6423 {
6424 xassert (SINGLE_BYTE_CHAR_P (c));
6425 if (unibyte_display_via_language_environment)
6426 {
6427 c = DECODE_CHAR (unibyte, c);
6428 if (c < 0)
6429 c = BYTE8_TO_CHAR (it->c);
6430 }
6431 else
6432 c = BYTE8_TO_CHAR (it->c);
6433 }
6434
6435 if (it->dp
6436 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6437 VECTORP (dv)))
6438 {
6439 struct Lisp_Vector *v = XVECTOR (dv);
6440
6441 /* Return the first character from the display table
6442 entry, if not empty. If empty, don't display the
6443 current character. */
6444 if (v->header.size)
6445 {
6446 it->dpvec_char_len = it->len;
6447 it->dpvec = v->contents;
6448 it->dpend = v->contents + v->header.size;
6449 it->current.dpvec_index = 0;
6450 it->dpvec_face_id = -1;
6451 it->saved_face_id = it->face_id;
6452 it->method = GET_FROM_DISPLAY_VECTOR;
6453 it->ellipsis_p = 0;
6454 }
6455 else
6456 {
6457 set_iterator_to_next (it, 0);
6458 }
6459 goto get_next;
6460 }
6461
6462 if (! NILP (lookup_glyphless_char_display (c, it)))
6463 {
6464 if (it->what == IT_GLYPHLESS)
6465 goto done;
6466 /* Don't display this character. */
6467 set_iterator_to_next (it, 0);
6468 goto get_next;
6469 }
6470
6471 /* If `nobreak-char-display' is non-nil, we display
6472 non-ASCII spaces and hyphens specially. */
6473 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6474 {
6475 if (c == 0xA0)
6476 nonascii_space_p = 1;
6477 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6478 nonascii_hyphen_p = 1;
6479 }
6480
6481 /* Translate control characters into `\003' or `^C' form.
6482 Control characters coming from a display table entry are
6483 currently not translated because we use IT->dpvec to hold
6484 the translation. This could easily be changed but I
6485 don't believe that it is worth doing.
6486
6487 The characters handled by `nobreak-char-display' must be
6488 translated too.
6489
6490 Non-printable characters and raw-byte characters are also
6491 translated to octal form. */
6492 if (((c < ' ' || c == 127) /* ASCII control chars */
6493 ? (it->area != TEXT_AREA
6494 /* In mode line, treat \n, \t like other crl chars. */
6495 || (c != '\t'
6496 && it->glyph_row
6497 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6498 || (c != '\n' && c != '\t'))
6499 : (nonascii_space_p
6500 || nonascii_hyphen_p
6501 || CHAR_BYTE8_P (c)
6502 || ! CHAR_PRINTABLE_P (c))))
6503 {
6504 /* C is a control character, non-ASCII space/hyphen,
6505 raw-byte, or a non-printable character which must be
6506 displayed either as '\003' or as `^C' where the '\\'
6507 and '^' can be defined in the display table. Fill
6508 IT->ctl_chars with glyphs for what we have to
6509 display. Then, set IT->dpvec to these glyphs. */
6510 Lisp_Object gc;
6511 int ctl_len;
6512 int face_id;
6513 int lface_id = 0;
6514 int escape_glyph;
6515
6516 /* Handle control characters with ^. */
6517
6518 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6519 {
6520 int g;
6521
6522 g = '^'; /* default glyph for Control */
6523 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6524 if (it->dp
6525 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6526 {
6527 g = GLYPH_CODE_CHAR (gc);
6528 lface_id = GLYPH_CODE_FACE (gc);
6529 }
6530 if (lface_id)
6531 {
6532 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6533 }
6534 else if (it->f == last_escape_glyph_frame
6535 && it->face_id == last_escape_glyph_face_id)
6536 {
6537 face_id = last_escape_glyph_merged_face_id;
6538 }
6539 else
6540 {
6541 /* Merge the escape-glyph face into the current face. */
6542 face_id = merge_faces (it->f, Qescape_glyph, 0,
6543 it->face_id);
6544 last_escape_glyph_frame = it->f;
6545 last_escape_glyph_face_id = it->face_id;
6546 last_escape_glyph_merged_face_id = face_id;
6547 }
6548
6549 XSETINT (it->ctl_chars[0], g);
6550 XSETINT (it->ctl_chars[1], c ^ 0100);
6551 ctl_len = 2;
6552 goto display_control;
6553 }
6554
6555 /* Handle non-ascii space in the mode where it only gets
6556 highlighting. */
6557
6558 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6559 {
6560 /* Merge `nobreak-space' into the current face. */
6561 face_id = merge_faces (it->f, Qnobreak_space, 0,
6562 it->face_id);
6563 XSETINT (it->ctl_chars[0], ' ');
6564 ctl_len = 1;
6565 goto display_control;
6566 }
6567
6568 /* Handle sequences that start with the "escape glyph". */
6569
6570 /* the default escape glyph is \. */
6571 escape_glyph = '\\';
6572
6573 if (it->dp
6574 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6575 {
6576 escape_glyph = GLYPH_CODE_CHAR (gc);
6577 lface_id = GLYPH_CODE_FACE (gc);
6578 }
6579 if (lface_id)
6580 {
6581 /* The display table specified a face.
6582 Merge it into face_id and also into escape_glyph. */
6583 face_id = merge_faces (it->f, Qt, lface_id,
6584 it->face_id);
6585 }
6586 else if (it->f == last_escape_glyph_frame
6587 && it->face_id == last_escape_glyph_face_id)
6588 {
6589 face_id = last_escape_glyph_merged_face_id;
6590 }
6591 else
6592 {
6593 /* Merge the escape-glyph face into the current face. */
6594 face_id = merge_faces (it->f, Qescape_glyph, 0,
6595 it->face_id);
6596 last_escape_glyph_frame = it->f;
6597 last_escape_glyph_face_id = it->face_id;
6598 last_escape_glyph_merged_face_id = face_id;
6599 }
6600
6601 /* Draw non-ASCII hyphen with just highlighting: */
6602
6603 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6604 {
6605 XSETINT (it->ctl_chars[0], '-');
6606 ctl_len = 1;
6607 goto display_control;
6608 }
6609
6610 /* Draw non-ASCII space/hyphen with escape glyph: */
6611
6612 if (nonascii_space_p || nonascii_hyphen_p)
6613 {
6614 XSETINT (it->ctl_chars[0], escape_glyph);
6615 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6616 ctl_len = 2;
6617 goto display_control;
6618 }
6619
6620 {
6621 char str[10];
6622 int len, i;
6623
6624 if (CHAR_BYTE8_P (c))
6625 /* Display \200 instead of \17777600. */
6626 c = CHAR_TO_BYTE8 (c);
6627 len = sprintf (str, "%03o", c);
6628
6629 XSETINT (it->ctl_chars[0], escape_glyph);
6630 for (i = 0; i < len; i++)
6631 XSETINT (it->ctl_chars[i + 1], str[i]);
6632 ctl_len = len + 1;
6633 }
6634
6635 display_control:
6636 /* Set up IT->dpvec and return first character from it. */
6637 it->dpvec_char_len = it->len;
6638 it->dpvec = it->ctl_chars;
6639 it->dpend = it->dpvec + ctl_len;
6640 it->current.dpvec_index = 0;
6641 it->dpvec_face_id = face_id;
6642 it->saved_face_id = it->face_id;
6643 it->method = GET_FROM_DISPLAY_VECTOR;
6644 it->ellipsis_p = 0;
6645 goto get_next;
6646 }
6647 it->char_to_display = c;
6648 }
6649 else if (success_p)
6650 {
6651 it->char_to_display = it->c;
6652 }
6653 }
6654
6655 /* Adjust face id for a multibyte character. There are no multibyte
6656 character in unibyte text. */
6657 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6658 && it->multibyte_p
6659 && success_p
6660 && FRAME_WINDOW_P (it->f))
6661 {
6662 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6663
6664 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6665 {
6666 /* Automatic composition with glyph-string. */
6667 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6668
6669 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6670 }
6671 else
6672 {
6673 ptrdiff_t pos = (it->s ? -1
6674 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6675 : IT_CHARPOS (*it));
6676 int c;
6677
6678 if (it->what == IT_CHARACTER)
6679 c = it->char_to_display;
6680 else
6681 {
6682 struct composition *cmp = composition_table[it->cmp_it.id];
6683 int i;
6684
6685 c = ' ';
6686 for (i = 0; i < cmp->glyph_len; i++)
6687 /* TAB in a composition means display glyphs with
6688 padding space on the left or right. */
6689 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6690 break;
6691 }
6692 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6693 }
6694 }
6695
6696 done:
6697 /* Is this character the last one of a run of characters with
6698 box? If yes, set IT->end_of_box_run_p to 1. */
6699 if (it->face_box_p
6700 && it->s == NULL)
6701 {
6702 if (it->method == GET_FROM_STRING && it->sp)
6703 {
6704 int face_id = underlying_face_id (it);
6705 struct face *face = FACE_FROM_ID (it->f, face_id);
6706
6707 if (face)
6708 {
6709 if (face->box == FACE_NO_BOX)
6710 {
6711 /* If the box comes from face properties in a
6712 display string, check faces in that string. */
6713 int string_face_id = face_after_it_pos (it);
6714 it->end_of_box_run_p
6715 = (FACE_FROM_ID (it->f, string_face_id)->box
6716 == FACE_NO_BOX);
6717 }
6718 /* Otherwise, the box comes from the underlying face.
6719 If this is the last string character displayed, check
6720 the next buffer location. */
6721 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6722 && (it->current.overlay_string_index
6723 == it->n_overlay_strings - 1))
6724 {
6725 ptrdiff_t ignore;
6726 int next_face_id;
6727 struct text_pos pos = it->current.pos;
6728 INC_TEXT_POS (pos, it->multibyte_p);
6729
6730 next_face_id = face_at_buffer_position
6731 (it->w, CHARPOS (pos), it->region_beg_charpos,
6732 it->region_end_charpos, &ignore,
6733 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6734 -1);
6735 it->end_of_box_run_p
6736 = (FACE_FROM_ID (it->f, next_face_id)->box
6737 == FACE_NO_BOX);
6738 }
6739 }
6740 }
6741 else
6742 {
6743 int face_id = face_after_it_pos (it);
6744 it->end_of_box_run_p
6745 = (face_id != it->face_id
6746 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6747 }
6748 }
6749
6750 /* Value is 0 if end of buffer or string reached. */
6751 return success_p;
6752 }
6753
6754
6755 /* Move IT to the next display element.
6756
6757 RESEAT_P non-zero means if called on a newline in buffer text,
6758 skip to the next visible line start.
6759
6760 Functions get_next_display_element and set_iterator_to_next are
6761 separate because I find this arrangement easier to handle than a
6762 get_next_display_element function that also increments IT's
6763 position. The way it is we can first look at an iterator's current
6764 display element, decide whether it fits on a line, and if it does,
6765 increment the iterator position. The other way around we probably
6766 would either need a flag indicating whether the iterator has to be
6767 incremented the next time, or we would have to implement a
6768 decrement position function which would not be easy to write. */
6769
6770 void
6771 set_iterator_to_next (struct it *it, int reseat_p)
6772 {
6773 /* Reset flags indicating start and end of a sequence of characters
6774 with box. Reset them at the start of this function because
6775 moving the iterator to a new position might set them. */
6776 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6777
6778 switch (it->method)
6779 {
6780 case GET_FROM_BUFFER:
6781 /* The current display element of IT is a character from
6782 current_buffer. Advance in the buffer, and maybe skip over
6783 invisible lines that are so because of selective display. */
6784 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6785 reseat_at_next_visible_line_start (it, 0);
6786 else if (it->cmp_it.id >= 0)
6787 {
6788 /* We are currently getting glyphs from a composition. */
6789 int i;
6790
6791 if (! it->bidi_p)
6792 {
6793 IT_CHARPOS (*it) += it->cmp_it.nchars;
6794 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6795 if (it->cmp_it.to < it->cmp_it.nglyphs)
6796 {
6797 it->cmp_it.from = it->cmp_it.to;
6798 }
6799 else
6800 {
6801 it->cmp_it.id = -1;
6802 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6803 IT_BYTEPOS (*it),
6804 it->end_charpos, Qnil);
6805 }
6806 }
6807 else if (! it->cmp_it.reversed_p)
6808 {
6809 /* Composition created while scanning forward. */
6810 /* Update IT's char/byte positions to point to the first
6811 character of the next grapheme cluster, or to the
6812 character visually after the current composition. */
6813 for (i = 0; i < it->cmp_it.nchars; i++)
6814 bidi_move_to_visually_next (&it->bidi_it);
6815 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6816 IT_CHARPOS (*it) = it->bidi_it.charpos;
6817
6818 if (it->cmp_it.to < it->cmp_it.nglyphs)
6819 {
6820 /* Proceed to the next grapheme cluster. */
6821 it->cmp_it.from = it->cmp_it.to;
6822 }
6823 else
6824 {
6825 /* No more grapheme clusters in this composition.
6826 Find the next stop position. */
6827 ptrdiff_t stop = it->end_charpos;
6828 if (it->bidi_it.scan_dir < 0)
6829 /* Now we are scanning backward and don't know
6830 where to stop. */
6831 stop = -1;
6832 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6833 IT_BYTEPOS (*it), stop, Qnil);
6834 }
6835 }
6836 else
6837 {
6838 /* Composition created while scanning backward. */
6839 /* Update IT's char/byte positions to point to the last
6840 character of the previous grapheme cluster, or the
6841 character visually after the current composition. */
6842 for (i = 0; i < it->cmp_it.nchars; i++)
6843 bidi_move_to_visually_next (&it->bidi_it);
6844 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6845 IT_CHARPOS (*it) = it->bidi_it.charpos;
6846 if (it->cmp_it.from > 0)
6847 {
6848 /* Proceed to the previous grapheme cluster. */
6849 it->cmp_it.to = it->cmp_it.from;
6850 }
6851 else
6852 {
6853 /* No more grapheme clusters in this composition.
6854 Find the next stop position. */
6855 ptrdiff_t stop = it->end_charpos;
6856 if (it->bidi_it.scan_dir < 0)
6857 /* Now we are scanning backward and don't know
6858 where to stop. */
6859 stop = -1;
6860 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6861 IT_BYTEPOS (*it), stop, Qnil);
6862 }
6863 }
6864 }
6865 else
6866 {
6867 xassert (it->len != 0);
6868
6869 if (!it->bidi_p)
6870 {
6871 IT_BYTEPOS (*it) += it->len;
6872 IT_CHARPOS (*it) += 1;
6873 }
6874 else
6875 {
6876 int prev_scan_dir = it->bidi_it.scan_dir;
6877 /* If this is a new paragraph, determine its base
6878 direction (a.k.a. its base embedding level). */
6879 if (it->bidi_it.new_paragraph)
6880 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6881 bidi_move_to_visually_next (&it->bidi_it);
6882 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6883 IT_CHARPOS (*it) = it->bidi_it.charpos;
6884 if (prev_scan_dir != it->bidi_it.scan_dir)
6885 {
6886 /* As the scan direction was changed, we must
6887 re-compute the stop position for composition. */
6888 ptrdiff_t stop = it->end_charpos;
6889 if (it->bidi_it.scan_dir < 0)
6890 stop = -1;
6891 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6892 IT_BYTEPOS (*it), stop, Qnil);
6893 }
6894 }
6895 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
6896 }
6897 break;
6898
6899 case GET_FROM_C_STRING:
6900 /* Current display element of IT is from a C string. */
6901 if (!it->bidi_p
6902 /* If the string position is beyond string's end, it means
6903 next_element_from_c_string is padding the string with
6904 blanks, in which case we bypass the bidi iterator,
6905 because it cannot deal with such virtual characters. */
6906 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
6907 {
6908 IT_BYTEPOS (*it) += it->len;
6909 IT_CHARPOS (*it) += 1;
6910 }
6911 else
6912 {
6913 bidi_move_to_visually_next (&it->bidi_it);
6914 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6915 IT_CHARPOS (*it) = it->bidi_it.charpos;
6916 }
6917 break;
6918
6919 case GET_FROM_DISPLAY_VECTOR:
6920 /* Current display element of IT is from a display table entry.
6921 Advance in the display table definition. Reset it to null if
6922 end reached, and continue with characters from buffers/
6923 strings. */
6924 ++it->current.dpvec_index;
6925
6926 /* Restore face of the iterator to what they were before the
6927 display vector entry (these entries may contain faces). */
6928 it->face_id = it->saved_face_id;
6929
6930 if (it->dpvec + it->current.dpvec_index == it->dpend)
6931 {
6932 int recheck_faces = it->ellipsis_p;
6933
6934 if (it->s)
6935 it->method = GET_FROM_C_STRING;
6936 else if (STRINGP (it->string))
6937 it->method = GET_FROM_STRING;
6938 else
6939 {
6940 it->method = GET_FROM_BUFFER;
6941 it->object = it->w->buffer;
6942 }
6943
6944 it->dpvec = NULL;
6945 it->current.dpvec_index = -1;
6946
6947 /* Skip over characters which were displayed via IT->dpvec. */
6948 if (it->dpvec_char_len < 0)
6949 reseat_at_next_visible_line_start (it, 1);
6950 else if (it->dpvec_char_len > 0)
6951 {
6952 if (it->method == GET_FROM_STRING
6953 && it->n_overlay_strings > 0)
6954 it->ignore_overlay_strings_at_pos_p = 1;
6955 it->len = it->dpvec_char_len;
6956 set_iterator_to_next (it, reseat_p);
6957 }
6958
6959 /* Maybe recheck faces after display vector */
6960 if (recheck_faces)
6961 it->stop_charpos = IT_CHARPOS (*it);
6962 }
6963 break;
6964
6965 case GET_FROM_STRING:
6966 /* Current display element is a character from a Lisp string. */
6967 xassert (it->s == NULL && STRINGP (it->string));
6968 if (it->cmp_it.id >= 0)
6969 {
6970 int i;
6971
6972 if (! it->bidi_p)
6973 {
6974 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
6975 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
6976 if (it->cmp_it.to < it->cmp_it.nglyphs)
6977 it->cmp_it.from = it->cmp_it.to;
6978 else
6979 {
6980 it->cmp_it.id = -1;
6981 composition_compute_stop_pos (&it->cmp_it,
6982 IT_STRING_CHARPOS (*it),
6983 IT_STRING_BYTEPOS (*it),
6984 it->end_charpos, it->string);
6985 }
6986 }
6987 else if (! it->cmp_it.reversed_p)
6988 {
6989 for (i = 0; i < it->cmp_it.nchars; i++)
6990 bidi_move_to_visually_next (&it->bidi_it);
6991 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6992 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6993
6994 if (it->cmp_it.to < it->cmp_it.nglyphs)
6995 it->cmp_it.from = it->cmp_it.to;
6996 else
6997 {
6998 ptrdiff_t stop = it->end_charpos;
6999 if (it->bidi_it.scan_dir < 0)
7000 stop = -1;
7001 composition_compute_stop_pos (&it->cmp_it,
7002 IT_STRING_CHARPOS (*it),
7003 IT_STRING_BYTEPOS (*it), stop,
7004 it->string);
7005 }
7006 }
7007 else
7008 {
7009 for (i = 0; i < it->cmp_it.nchars; i++)
7010 bidi_move_to_visually_next (&it->bidi_it);
7011 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7012 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7013 if (it->cmp_it.from > 0)
7014 it->cmp_it.to = it->cmp_it.from;
7015 else
7016 {
7017 ptrdiff_t stop = it->end_charpos;
7018 if (it->bidi_it.scan_dir < 0)
7019 stop = -1;
7020 composition_compute_stop_pos (&it->cmp_it,
7021 IT_STRING_CHARPOS (*it),
7022 IT_STRING_BYTEPOS (*it), stop,
7023 it->string);
7024 }
7025 }
7026 }
7027 else
7028 {
7029 if (!it->bidi_p
7030 /* If the string position is beyond string's end, it
7031 means next_element_from_string is padding the string
7032 with blanks, in which case we bypass the bidi
7033 iterator, because it cannot deal with such virtual
7034 characters. */
7035 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7036 {
7037 IT_STRING_BYTEPOS (*it) += it->len;
7038 IT_STRING_CHARPOS (*it) += 1;
7039 }
7040 else
7041 {
7042 int prev_scan_dir = it->bidi_it.scan_dir;
7043
7044 bidi_move_to_visually_next (&it->bidi_it);
7045 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7046 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7047 if (prev_scan_dir != it->bidi_it.scan_dir)
7048 {
7049 ptrdiff_t stop = it->end_charpos;
7050
7051 if (it->bidi_it.scan_dir < 0)
7052 stop = -1;
7053 composition_compute_stop_pos (&it->cmp_it,
7054 IT_STRING_CHARPOS (*it),
7055 IT_STRING_BYTEPOS (*it), stop,
7056 it->string);
7057 }
7058 }
7059 }
7060
7061 consider_string_end:
7062
7063 if (it->current.overlay_string_index >= 0)
7064 {
7065 /* IT->string is an overlay string. Advance to the
7066 next, if there is one. */
7067 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7068 {
7069 it->ellipsis_p = 0;
7070 next_overlay_string (it);
7071 if (it->ellipsis_p)
7072 setup_for_ellipsis (it, 0);
7073 }
7074 }
7075 else
7076 {
7077 /* IT->string is not an overlay string. If we reached
7078 its end, and there is something on IT->stack, proceed
7079 with what is on the stack. This can be either another
7080 string, this time an overlay string, or a buffer. */
7081 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7082 && it->sp > 0)
7083 {
7084 pop_it (it);
7085 if (it->method == GET_FROM_STRING)
7086 goto consider_string_end;
7087 }
7088 }
7089 break;
7090
7091 case GET_FROM_IMAGE:
7092 case GET_FROM_STRETCH:
7093 /* The position etc with which we have to proceed are on
7094 the stack. The position may be at the end of a string,
7095 if the `display' property takes up the whole string. */
7096 xassert (it->sp > 0);
7097 pop_it (it);
7098 if (it->method == GET_FROM_STRING)
7099 goto consider_string_end;
7100 break;
7101
7102 default:
7103 /* There are no other methods defined, so this should be a bug. */
7104 abort ();
7105 }
7106
7107 xassert (it->method != GET_FROM_STRING
7108 || (STRINGP (it->string)
7109 && IT_STRING_CHARPOS (*it) >= 0));
7110 }
7111
7112 /* Load IT's display element fields with information about the next
7113 display element which comes from a display table entry or from the
7114 result of translating a control character to one of the forms `^C'
7115 or `\003'.
7116
7117 IT->dpvec holds the glyphs to return as characters.
7118 IT->saved_face_id holds the face id before the display vector--it
7119 is restored into IT->face_id in set_iterator_to_next. */
7120
7121 static int
7122 next_element_from_display_vector (struct it *it)
7123 {
7124 Lisp_Object gc;
7125
7126 /* Precondition. */
7127 xassert (it->dpvec && it->current.dpvec_index >= 0);
7128
7129 it->face_id = it->saved_face_id;
7130
7131 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7132 That seemed totally bogus - so I changed it... */
7133 gc = it->dpvec[it->current.dpvec_index];
7134
7135 if (GLYPH_CODE_P (gc))
7136 {
7137 it->c = GLYPH_CODE_CHAR (gc);
7138 it->len = CHAR_BYTES (it->c);
7139
7140 /* The entry may contain a face id to use. Such a face id is
7141 the id of a Lisp face, not a realized face. A face id of
7142 zero means no face is specified. */
7143 if (it->dpvec_face_id >= 0)
7144 it->face_id = it->dpvec_face_id;
7145 else
7146 {
7147 int lface_id = GLYPH_CODE_FACE (gc);
7148 if (lface_id > 0)
7149 it->face_id = merge_faces (it->f, Qt, lface_id,
7150 it->saved_face_id);
7151 }
7152 }
7153 else
7154 /* Display table entry is invalid. Return a space. */
7155 it->c = ' ', it->len = 1;
7156
7157 /* Don't change position and object of the iterator here. They are
7158 still the values of the character that had this display table
7159 entry or was translated, and that's what we want. */
7160 it->what = IT_CHARACTER;
7161 return 1;
7162 }
7163
7164 /* Get the first element of string/buffer in the visual order, after
7165 being reseated to a new position in a string or a buffer. */
7166 static void
7167 get_visually_first_element (struct it *it)
7168 {
7169 int string_p = STRINGP (it->string) || it->s;
7170 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7171 ptrdiff_t bob = (string_p ? 0 : BEGV);
7172
7173 if (STRINGP (it->string))
7174 {
7175 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7176 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7177 }
7178 else
7179 {
7180 it->bidi_it.charpos = IT_CHARPOS (*it);
7181 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7182 }
7183
7184 if (it->bidi_it.charpos == eob)
7185 {
7186 /* Nothing to do, but reset the FIRST_ELT flag, like
7187 bidi_paragraph_init does, because we are not going to
7188 call it. */
7189 it->bidi_it.first_elt = 0;
7190 }
7191 else if (it->bidi_it.charpos == bob
7192 || (!string_p
7193 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7194 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7195 {
7196 /* If we are at the beginning of a line/string, we can produce
7197 the next element right away. */
7198 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7199 bidi_move_to_visually_next (&it->bidi_it);
7200 }
7201 else
7202 {
7203 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7204
7205 /* We need to prime the bidi iterator starting at the line's or
7206 string's beginning, before we will be able to produce the
7207 next element. */
7208 if (string_p)
7209 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7210 else
7211 {
7212 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7213 -1);
7214 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7215 }
7216 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7217 do
7218 {
7219 /* Now return to buffer/string position where we were asked
7220 to get the next display element, and produce that. */
7221 bidi_move_to_visually_next (&it->bidi_it);
7222 }
7223 while (it->bidi_it.bytepos != orig_bytepos
7224 && it->bidi_it.charpos < eob);
7225 }
7226
7227 /* Adjust IT's position information to where we ended up. */
7228 if (STRINGP (it->string))
7229 {
7230 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7231 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7232 }
7233 else
7234 {
7235 IT_CHARPOS (*it) = it->bidi_it.charpos;
7236 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7237 }
7238
7239 if (STRINGP (it->string) || !it->s)
7240 {
7241 ptrdiff_t stop, charpos, bytepos;
7242
7243 if (STRINGP (it->string))
7244 {
7245 xassert (!it->s);
7246 stop = SCHARS (it->string);
7247 if (stop > it->end_charpos)
7248 stop = it->end_charpos;
7249 charpos = IT_STRING_CHARPOS (*it);
7250 bytepos = IT_STRING_BYTEPOS (*it);
7251 }
7252 else
7253 {
7254 stop = it->end_charpos;
7255 charpos = IT_CHARPOS (*it);
7256 bytepos = IT_BYTEPOS (*it);
7257 }
7258 if (it->bidi_it.scan_dir < 0)
7259 stop = -1;
7260 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7261 it->string);
7262 }
7263 }
7264
7265 /* Load IT with the next display element from Lisp string IT->string.
7266 IT->current.string_pos is the current position within the string.
7267 If IT->current.overlay_string_index >= 0, the Lisp string is an
7268 overlay string. */
7269
7270 static int
7271 next_element_from_string (struct it *it)
7272 {
7273 struct text_pos position;
7274
7275 xassert (STRINGP (it->string));
7276 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7277 xassert (IT_STRING_CHARPOS (*it) >= 0);
7278 position = it->current.string_pos;
7279
7280 /* With bidi reordering, the character to display might not be the
7281 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7282 that we were reseat()ed to a new string, whose paragraph
7283 direction is not known. */
7284 if (it->bidi_p && it->bidi_it.first_elt)
7285 {
7286 get_visually_first_element (it);
7287 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7288 }
7289
7290 /* Time to check for invisible text? */
7291 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7292 {
7293 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7294 {
7295 if (!(!it->bidi_p
7296 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7297 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7298 {
7299 /* With bidi non-linear iteration, we could find
7300 ourselves far beyond the last computed stop_charpos,
7301 with several other stop positions in between that we
7302 missed. Scan them all now, in buffer's logical
7303 order, until we find and handle the last stop_charpos
7304 that precedes our current position. */
7305 handle_stop_backwards (it, it->stop_charpos);
7306 return GET_NEXT_DISPLAY_ELEMENT (it);
7307 }
7308 else
7309 {
7310 if (it->bidi_p)
7311 {
7312 /* Take note of the stop position we just moved
7313 across, for when we will move back across it. */
7314 it->prev_stop = it->stop_charpos;
7315 /* If we are at base paragraph embedding level, take
7316 note of the last stop position seen at this
7317 level. */
7318 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7319 it->base_level_stop = it->stop_charpos;
7320 }
7321 handle_stop (it);
7322
7323 /* Since a handler may have changed IT->method, we must
7324 recurse here. */
7325 return GET_NEXT_DISPLAY_ELEMENT (it);
7326 }
7327 }
7328 else if (it->bidi_p
7329 /* If we are before prev_stop, we may have overstepped
7330 on our way backwards a stop_pos, and if so, we need
7331 to handle that stop_pos. */
7332 && IT_STRING_CHARPOS (*it) < it->prev_stop
7333 /* We can sometimes back up for reasons that have nothing
7334 to do with bidi reordering. E.g., compositions. The
7335 code below is only needed when we are above the base
7336 embedding level, so test for that explicitly. */
7337 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7338 {
7339 /* If we lost track of base_level_stop, we have no better
7340 place for handle_stop_backwards to start from than string
7341 beginning. This happens, e.g., when we were reseated to
7342 the previous screenful of text by vertical-motion. */
7343 if (it->base_level_stop <= 0
7344 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7345 it->base_level_stop = 0;
7346 handle_stop_backwards (it, it->base_level_stop);
7347 return GET_NEXT_DISPLAY_ELEMENT (it);
7348 }
7349 }
7350
7351 if (it->current.overlay_string_index >= 0)
7352 {
7353 /* Get the next character from an overlay string. In overlay
7354 strings, There is no field width or padding with spaces to
7355 do. */
7356 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7357 {
7358 it->what = IT_EOB;
7359 return 0;
7360 }
7361 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7362 IT_STRING_BYTEPOS (*it),
7363 it->bidi_it.scan_dir < 0
7364 ? -1
7365 : SCHARS (it->string))
7366 && next_element_from_composition (it))
7367 {
7368 return 1;
7369 }
7370 else if (STRING_MULTIBYTE (it->string))
7371 {
7372 const unsigned char *s = (SDATA (it->string)
7373 + IT_STRING_BYTEPOS (*it));
7374 it->c = string_char_and_length (s, &it->len);
7375 }
7376 else
7377 {
7378 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7379 it->len = 1;
7380 }
7381 }
7382 else
7383 {
7384 /* Get the next character from a Lisp string that is not an
7385 overlay string. Such strings come from the mode line, for
7386 example. We may have to pad with spaces, or truncate the
7387 string. See also next_element_from_c_string. */
7388 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7389 {
7390 it->what = IT_EOB;
7391 return 0;
7392 }
7393 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7394 {
7395 /* Pad with spaces. */
7396 it->c = ' ', it->len = 1;
7397 CHARPOS (position) = BYTEPOS (position) = -1;
7398 }
7399 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7400 IT_STRING_BYTEPOS (*it),
7401 it->bidi_it.scan_dir < 0
7402 ? -1
7403 : it->string_nchars)
7404 && next_element_from_composition (it))
7405 {
7406 return 1;
7407 }
7408 else if (STRING_MULTIBYTE (it->string))
7409 {
7410 const unsigned char *s = (SDATA (it->string)
7411 + IT_STRING_BYTEPOS (*it));
7412 it->c = string_char_and_length (s, &it->len);
7413 }
7414 else
7415 {
7416 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7417 it->len = 1;
7418 }
7419 }
7420
7421 /* Record what we have and where it came from. */
7422 it->what = IT_CHARACTER;
7423 it->object = it->string;
7424 it->position = position;
7425 return 1;
7426 }
7427
7428
7429 /* Load IT with next display element from C string IT->s.
7430 IT->string_nchars is the maximum number of characters to return
7431 from the string. IT->end_charpos may be greater than
7432 IT->string_nchars when this function is called, in which case we
7433 may have to return padding spaces. Value is zero if end of string
7434 reached, including padding spaces. */
7435
7436 static int
7437 next_element_from_c_string (struct it *it)
7438 {
7439 int success_p = 1;
7440
7441 xassert (it->s);
7442 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7443 it->what = IT_CHARACTER;
7444 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7445 it->object = Qnil;
7446
7447 /* With bidi reordering, the character to display might not be the
7448 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7449 we were reseated to a new string, whose paragraph direction is
7450 not known. */
7451 if (it->bidi_p && it->bidi_it.first_elt)
7452 get_visually_first_element (it);
7453
7454 /* IT's position can be greater than IT->string_nchars in case a
7455 field width or precision has been specified when the iterator was
7456 initialized. */
7457 if (IT_CHARPOS (*it) >= it->end_charpos)
7458 {
7459 /* End of the game. */
7460 it->what = IT_EOB;
7461 success_p = 0;
7462 }
7463 else if (IT_CHARPOS (*it) >= it->string_nchars)
7464 {
7465 /* Pad with spaces. */
7466 it->c = ' ', it->len = 1;
7467 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7468 }
7469 else if (it->multibyte_p)
7470 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7471 else
7472 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7473
7474 return success_p;
7475 }
7476
7477
7478 /* Set up IT to return characters from an ellipsis, if appropriate.
7479 The definition of the ellipsis glyphs may come from a display table
7480 entry. This function fills IT with the first glyph from the
7481 ellipsis if an ellipsis is to be displayed. */
7482
7483 static int
7484 next_element_from_ellipsis (struct it *it)
7485 {
7486 if (it->selective_display_ellipsis_p)
7487 setup_for_ellipsis (it, it->len);
7488 else
7489 {
7490 /* The face at the current position may be different from the
7491 face we find after the invisible text. Remember what it
7492 was in IT->saved_face_id, and signal that it's there by
7493 setting face_before_selective_p. */
7494 it->saved_face_id = it->face_id;
7495 it->method = GET_FROM_BUFFER;
7496 it->object = it->w->buffer;
7497 reseat_at_next_visible_line_start (it, 1);
7498 it->face_before_selective_p = 1;
7499 }
7500
7501 return GET_NEXT_DISPLAY_ELEMENT (it);
7502 }
7503
7504
7505 /* Deliver an image display element. The iterator IT is already
7506 filled with image information (done in handle_display_prop). Value
7507 is always 1. */
7508
7509
7510 static int
7511 next_element_from_image (struct it *it)
7512 {
7513 it->what = IT_IMAGE;
7514 it->ignore_overlay_strings_at_pos_p = 0;
7515 return 1;
7516 }
7517
7518
7519 /* Fill iterator IT with next display element from a stretch glyph
7520 property. IT->object is the value of the text property. Value is
7521 always 1. */
7522
7523 static int
7524 next_element_from_stretch (struct it *it)
7525 {
7526 it->what = IT_STRETCH;
7527 return 1;
7528 }
7529
7530 /* Scan backwards from IT's current position until we find a stop
7531 position, or until BEGV. This is called when we find ourself
7532 before both the last known prev_stop and base_level_stop while
7533 reordering bidirectional text. */
7534
7535 static void
7536 compute_stop_pos_backwards (struct it *it)
7537 {
7538 const int SCAN_BACK_LIMIT = 1000;
7539 struct text_pos pos;
7540 struct display_pos save_current = it->current;
7541 struct text_pos save_position = it->position;
7542 ptrdiff_t charpos = IT_CHARPOS (*it);
7543 ptrdiff_t where_we_are = charpos;
7544 ptrdiff_t save_stop_pos = it->stop_charpos;
7545 ptrdiff_t save_end_pos = it->end_charpos;
7546
7547 xassert (NILP (it->string) && !it->s);
7548 xassert (it->bidi_p);
7549 it->bidi_p = 0;
7550 do
7551 {
7552 it->end_charpos = min (charpos + 1, ZV);
7553 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7554 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7555 reseat_1 (it, pos, 0);
7556 compute_stop_pos (it);
7557 /* We must advance forward, right? */
7558 if (it->stop_charpos <= charpos)
7559 abort ();
7560 }
7561 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7562
7563 if (it->stop_charpos <= where_we_are)
7564 it->prev_stop = it->stop_charpos;
7565 else
7566 it->prev_stop = BEGV;
7567 it->bidi_p = 1;
7568 it->current = save_current;
7569 it->position = save_position;
7570 it->stop_charpos = save_stop_pos;
7571 it->end_charpos = save_end_pos;
7572 }
7573
7574 /* Scan forward from CHARPOS in the current buffer/string, until we
7575 find a stop position > current IT's position. Then handle the stop
7576 position before that. This is called when we bump into a stop
7577 position while reordering bidirectional text. CHARPOS should be
7578 the last previously processed stop_pos (or BEGV/0, if none were
7579 processed yet) whose position is less that IT's current
7580 position. */
7581
7582 static void
7583 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7584 {
7585 int bufp = !STRINGP (it->string);
7586 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7587 struct display_pos save_current = it->current;
7588 struct text_pos save_position = it->position;
7589 struct text_pos pos1;
7590 ptrdiff_t next_stop;
7591
7592 /* Scan in strict logical order. */
7593 xassert (it->bidi_p);
7594 it->bidi_p = 0;
7595 do
7596 {
7597 it->prev_stop = charpos;
7598 if (bufp)
7599 {
7600 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7601 reseat_1 (it, pos1, 0);
7602 }
7603 else
7604 it->current.string_pos = string_pos (charpos, it->string);
7605 compute_stop_pos (it);
7606 /* We must advance forward, right? */
7607 if (it->stop_charpos <= it->prev_stop)
7608 abort ();
7609 charpos = it->stop_charpos;
7610 }
7611 while (charpos <= where_we_are);
7612
7613 it->bidi_p = 1;
7614 it->current = save_current;
7615 it->position = save_position;
7616 next_stop = it->stop_charpos;
7617 it->stop_charpos = it->prev_stop;
7618 handle_stop (it);
7619 it->stop_charpos = next_stop;
7620 }
7621
7622 /* Load IT with the next display element from current_buffer. Value
7623 is zero if end of buffer reached. IT->stop_charpos is the next
7624 position at which to stop and check for text properties or buffer
7625 end. */
7626
7627 static int
7628 next_element_from_buffer (struct it *it)
7629 {
7630 int success_p = 1;
7631
7632 xassert (IT_CHARPOS (*it) >= BEGV);
7633 xassert (NILP (it->string) && !it->s);
7634 xassert (!it->bidi_p
7635 || (EQ (it->bidi_it.string.lstring, Qnil)
7636 && it->bidi_it.string.s == NULL));
7637
7638 /* With bidi reordering, the character to display might not be the
7639 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7640 we were reseat()ed to a new buffer position, which is potentially
7641 a different paragraph. */
7642 if (it->bidi_p && it->bidi_it.first_elt)
7643 {
7644 get_visually_first_element (it);
7645 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7646 }
7647
7648 if (IT_CHARPOS (*it) >= it->stop_charpos)
7649 {
7650 if (IT_CHARPOS (*it) >= it->end_charpos)
7651 {
7652 int overlay_strings_follow_p;
7653
7654 /* End of the game, except when overlay strings follow that
7655 haven't been returned yet. */
7656 if (it->overlay_strings_at_end_processed_p)
7657 overlay_strings_follow_p = 0;
7658 else
7659 {
7660 it->overlay_strings_at_end_processed_p = 1;
7661 overlay_strings_follow_p = get_overlay_strings (it, 0);
7662 }
7663
7664 if (overlay_strings_follow_p)
7665 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7666 else
7667 {
7668 it->what = IT_EOB;
7669 it->position = it->current.pos;
7670 success_p = 0;
7671 }
7672 }
7673 else if (!(!it->bidi_p
7674 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7675 || IT_CHARPOS (*it) == it->stop_charpos))
7676 {
7677 /* With bidi non-linear iteration, we could find ourselves
7678 far beyond the last computed stop_charpos, with several
7679 other stop positions in between that we missed. Scan
7680 them all now, in buffer's logical order, until we find
7681 and handle the last stop_charpos that precedes our
7682 current position. */
7683 handle_stop_backwards (it, it->stop_charpos);
7684 return GET_NEXT_DISPLAY_ELEMENT (it);
7685 }
7686 else
7687 {
7688 if (it->bidi_p)
7689 {
7690 /* Take note of the stop position we just moved across,
7691 for when we will move back across it. */
7692 it->prev_stop = it->stop_charpos;
7693 /* If we are at base paragraph embedding level, take
7694 note of the last stop position seen at this
7695 level. */
7696 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7697 it->base_level_stop = it->stop_charpos;
7698 }
7699 handle_stop (it);
7700 return GET_NEXT_DISPLAY_ELEMENT (it);
7701 }
7702 }
7703 else if (it->bidi_p
7704 /* If we are before prev_stop, we may have overstepped on
7705 our way backwards a stop_pos, and if so, we need to
7706 handle that stop_pos. */
7707 && IT_CHARPOS (*it) < it->prev_stop
7708 /* We can sometimes back up for reasons that have nothing
7709 to do with bidi reordering. E.g., compositions. The
7710 code below is only needed when we are above the base
7711 embedding level, so test for that explicitly. */
7712 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7713 {
7714 if (it->base_level_stop <= 0
7715 || IT_CHARPOS (*it) < it->base_level_stop)
7716 {
7717 /* If we lost track of base_level_stop, we need to find
7718 prev_stop by looking backwards. This happens, e.g., when
7719 we were reseated to the previous screenful of text by
7720 vertical-motion. */
7721 it->base_level_stop = BEGV;
7722 compute_stop_pos_backwards (it);
7723 handle_stop_backwards (it, it->prev_stop);
7724 }
7725 else
7726 handle_stop_backwards (it, it->base_level_stop);
7727 return GET_NEXT_DISPLAY_ELEMENT (it);
7728 }
7729 else
7730 {
7731 /* No face changes, overlays etc. in sight, so just return a
7732 character from current_buffer. */
7733 unsigned char *p;
7734 ptrdiff_t stop;
7735
7736 /* Maybe run the redisplay end trigger hook. Performance note:
7737 This doesn't seem to cost measurable time. */
7738 if (it->redisplay_end_trigger_charpos
7739 && it->glyph_row
7740 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7741 run_redisplay_end_trigger_hook (it);
7742
7743 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7744 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7745 stop)
7746 && next_element_from_composition (it))
7747 {
7748 return 1;
7749 }
7750
7751 /* Get the next character, maybe multibyte. */
7752 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7753 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7754 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7755 else
7756 it->c = *p, it->len = 1;
7757
7758 /* Record what we have and where it came from. */
7759 it->what = IT_CHARACTER;
7760 it->object = it->w->buffer;
7761 it->position = it->current.pos;
7762
7763 /* Normally we return the character found above, except when we
7764 really want to return an ellipsis for selective display. */
7765 if (it->selective)
7766 {
7767 if (it->c == '\n')
7768 {
7769 /* A value of selective > 0 means hide lines indented more
7770 than that number of columns. */
7771 if (it->selective > 0
7772 && IT_CHARPOS (*it) + 1 < ZV
7773 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7774 IT_BYTEPOS (*it) + 1,
7775 it->selective))
7776 {
7777 success_p = next_element_from_ellipsis (it);
7778 it->dpvec_char_len = -1;
7779 }
7780 }
7781 else if (it->c == '\r' && it->selective == -1)
7782 {
7783 /* A value of selective == -1 means that everything from the
7784 CR to the end of the line is invisible, with maybe an
7785 ellipsis displayed for it. */
7786 success_p = next_element_from_ellipsis (it);
7787 it->dpvec_char_len = -1;
7788 }
7789 }
7790 }
7791
7792 /* Value is zero if end of buffer reached. */
7793 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7794 return success_p;
7795 }
7796
7797
7798 /* Run the redisplay end trigger hook for IT. */
7799
7800 static void
7801 run_redisplay_end_trigger_hook (struct it *it)
7802 {
7803 Lisp_Object args[3];
7804
7805 /* IT->glyph_row should be non-null, i.e. we should be actually
7806 displaying something, or otherwise we should not run the hook. */
7807 xassert (it->glyph_row);
7808
7809 /* Set up hook arguments. */
7810 args[0] = Qredisplay_end_trigger_functions;
7811 args[1] = it->window;
7812 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7813 it->redisplay_end_trigger_charpos = 0;
7814
7815 /* Since we are *trying* to run these functions, don't try to run
7816 them again, even if they get an error. */
7817 it->w->redisplay_end_trigger = Qnil;
7818 Frun_hook_with_args (3, args);
7819
7820 /* Notice if it changed the face of the character we are on. */
7821 handle_face_prop (it);
7822 }
7823
7824
7825 /* Deliver a composition display element. Unlike the other
7826 next_element_from_XXX, this function is not registered in the array
7827 get_next_element[]. It is called from next_element_from_buffer and
7828 next_element_from_string when necessary. */
7829
7830 static int
7831 next_element_from_composition (struct it *it)
7832 {
7833 it->what = IT_COMPOSITION;
7834 it->len = it->cmp_it.nbytes;
7835 if (STRINGP (it->string))
7836 {
7837 if (it->c < 0)
7838 {
7839 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7840 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7841 return 0;
7842 }
7843 it->position = it->current.string_pos;
7844 it->object = it->string;
7845 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7846 IT_STRING_BYTEPOS (*it), it->string);
7847 }
7848 else
7849 {
7850 if (it->c < 0)
7851 {
7852 IT_CHARPOS (*it) += it->cmp_it.nchars;
7853 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7854 if (it->bidi_p)
7855 {
7856 if (it->bidi_it.new_paragraph)
7857 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7858 /* Resync the bidi iterator with IT's new position.
7859 FIXME: this doesn't support bidirectional text. */
7860 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7861 bidi_move_to_visually_next (&it->bidi_it);
7862 }
7863 return 0;
7864 }
7865 it->position = it->current.pos;
7866 it->object = it->w->buffer;
7867 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
7868 IT_BYTEPOS (*it), Qnil);
7869 }
7870 return 1;
7871 }
7872
7873
7874 \f
7875 /***********************************************************************
7876 Moving an iterator without producing glyphs
7877 ***********************************************************************/
7878
7879 /* Check if iterator is at a position corresponding to a valid buffer
7880 position after some move_it_ call. */
7881
7882 #define IT_POS_VALID_AFTER_MOVE_P(it) \
7883 ((it)->method == GET_FROM_STRING \
7884 ? IT_STRING_CHARPOS (*it) == 0 \
7885 : 1)
7886
7887
7888 /* Move iterator IT to a specified buffer or X position within one
7889 line on the display without producing glyphs.
7890
7891 OP should be a bit mask including some or all of these bits:
7892 MOVE_TO_X: Stop upon reaching x-position TO_X.
7893 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
7894 Regardless of OP's value, stop upon reaching the end of the display line.
7895
7896 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
7897 This means, in particular, that TO_X includes window's horizontal
7898 scroll amount.
7899
7900 The return value has several possible values that
7901 say what condition caused the scan to stop:
7902
7903 MOVE_POS_MATCH_OR_ZV
7904 - when TO_POS or ZV was reached.
7905
7906 MOVE_X_REACHED
7907 -when TO_X was reached before TO_POS or ZV were reached.
7908
7909 MOVE_LINE_CONTINUED
7910 - when we reached the end of the display area and the line must
7911 be continued.
7912
7913 MOVE_LINE_TRUNCATED
7914 - when we reached the end of the display area and the line is
7915 truncated.
7916
7917 MOVE_NEWLINE_OR_CR
7918 - when we stopped at a line end, i.e. a newline or a CR and selective
7919 display is on. */
7920
7921 static enum move_it_result
7922 move_it_in_display_line_to (struct it *it,
7923 ptrdiff_t to_charpos, int to_x,
7924 enum move_operation_enum op)
7925 {
7926 enum move_it_result result = MOVE_UNDEFINED;
7927 struct glyph_row *saved_glyph_row;
7928 struct it wrap_it, atpos_it, atx_it, ppos_it;
7929 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
7930 void *ppos_data = NULL;
7931 int may_wrap = 0;
7932 enum it_method prev_method = it->method;
7933 ptrdiff_t prev_pos = IT_CHARPOS (*it);
7934 int saw_smaller_pos = prev_pos < to_charpos;
7935
7936 /* Don't produce glyphs in produce_glyphs. */
7937 saved_glyph_row = it->glyph_row;
7938 it->glyph_row = NULL;
7939
7940 /* Use wrap_it to save a copy of IT wherever a word wrap could
7941 occur. Use atpos_it to save a copy of IT at the desired buffer
7942 position, if found, so that we can scan ahead and check if the
7943 word later overshoots the window edge. Use atx_it similarly, for
7944 pixel positions. */
7945 wrap_it.sp = -1;
7946 atpos_it.sp = -1;
7947 atx_it.sp = -1;
7948
7949 /* Use ppos_it under bidi reordering to save a copy of IT for the
7950 position > CHARPOS that is the closest to CHARPOS. We restore
7951 that position in IT when we have scanned the entire display line
7952 without finding a match for CHARPOS and all the character
7953 positions are greater than CHARPOS. */
7954 if (it->bidi_p)
7955 {
7956 SAVE_IT (ppos_it, *it, ppos_data);
7957 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
7958 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
7959 SAVE_IT (ppos_it, *it, ppos_data);
7960 }
7961
7962 #define BUFFER_POS_REACHED_P() \
7963 ((op & MOVE_TO_POS) != 0 \
7964 && BUFFERP (it->object) \
7965 && (IT_CHARPOS (*it) == to_charpos \
7966 || ((!it->bidi_p \
7967 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
7968 && IT_CHARPOS (*it) > to_charpos) \
7969 || (it->what == IT_COMPOSITION \
7970 && ((IT_CHARPOS (*it) > to_charpos \
7971 && to_charpos >= it->cmp_it.charpos) \
7972 || (IT_CHARPOS (*it) < to_charpos \
7973 && to_charpos <= it->cmp_it.charpos)))) \
7974 && (it->method == GET_FROM_BUFFER \
7975 || (it->method == GET_FROM_DISPLAY_VECTOR \
7976 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
7977
7978 /* If there's a line-/wrap-prefix, handle it. */
7979 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
7980 && it->current_y < it->last_visible_y)
7981 handle_line_prefix (it);
7982
7983 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
7984 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7985
7986 while (1)
7987 {
7988 int x, i, ascent = 0, descent = 0;
7989
7990 /* Utility macro to reset an iterator with x, ascent, and descent. */
7991 #define IT_RESET_X_ASCENT_DESCENT(IT) \
7992 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
7993 (IT)->max_descent = descent)
7994
7995 /* Stop if we move beyond TO_CHARPOS (after an image or a
7996 display string or stretch glyph). */
7997 if ((op & MOVE_TO_POS) != 0
7998 && BUFFERP (it->object)
7999 && it->method == GET_FROM_BUFFER
8000 && (((!it->bidi_p
8001 /* When the iterator is at base embedding level, we
8002 are guaranteed that characters are delivered for
8003 display in strictly increasing order of their
8004 buffer positions. */
8005 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8006 && IT_CHARPOS (*it) > to_charpos)
8007 || (it->bidi_p
8008 && (prev_method == GET_FROM_IMAGE
8009 || prev_method == GET_FROM_STRETCH
8010 || prev_method == GET_FROM_STRING)
8011 /* Passed TO_CHARPOS from left to right. */
8012 && ((prev_pos < to_charpos
8013 && IT_CHARPOS (*it) > to_charpos)
8014 /* Passed TO_CHARPOS from right to left. */
8015 || (prev_pos > to_charpos
8016 && IT_CHARPOS (*it) < to_charpos)))))
8017 {
8018 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8019 {
8020 result = MOVE_POS_MATCH_OR_ZV;
8021 break;
8022 }
8023 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8024 /* If wrap_it is valid, the current position might be in a
8025 word that is wrapped. So, save the iterator in
8026 atpos_it and continue to see if wrapping happens. */
8027 SAVE_IT (atpos_it, *it, atpos_data);
8028 }
8029
8030 /* Stop when ZV reached.
8031 We used to stop here when TO_CHARPOS reached as well, but that is
8032 too soon if this glyph does not fit on this line. So we handle it
8033 explicitly below. */
8034 if (!get_next_display_element (it))
8035 {
8036 result = MOVE_POS_MATCH_OR_ZV;
8037 break;
8038 }
8039
8040 if (it->line_wrap == TRUNCATE)
8041 {
8042 if (BUFFER_POS_REACHED_P ())
8043 {
8044 result = MOVE_POS_MATCH_OR_ZV;
8045 break;
8046 }
8047 }
8048 else
8049 {
8050 if (it->line_wrap == WORD_WRAP)
8051 {
8052 if (IT_DISPLAYING_WHITESPACE (it))
8053 may_wrap = 1;
8054 else if (may_wrap)
8055 {
8056 /* We have reached a glyph that follows one or more
8057 whitespace characters. If the position is
8058 already found, we are done. */
8059 if (atpos_it.sp >= 0)
8060 {
8061 RESTORE_IT (it, &atpos_it, atpos_data);
8062 result = MOVE_POS_MATCH_OR_ZV;
8063 goto done;
8064 }
8065 if (atx_it.sp >= 0)
8066 {
8067 RESTORE_IT (it, &atx_it, atx_data);
8068 result = MOVE_X_REACHED;
8069 goto done;
8070 }
8071 /* Otherwise, we can wrap here. */
8072 SAVE_IT (wrap_it, *it, wrap_data);
8073 may_wrap = 0;
8074 }
8075 }
8076 }
8077
8078 /* Remember the line height for the current line, in case
8079 the next element doesn't fit on the line. */
8080 ascent = it->max_ascent;
8081 descent = it->max_descent;
8082
8083 /* The call to produce_glyphs will get the metrics of the
8084 display element IT is loaded with. Record the x-position
8085 before this display element, in case it doesn't fit on the
8086 line. */
8087 x = it->current_x;
8088
8089 PRODUCE_GLYPHS (it);
8090
8091 if (it->area != TEXT_AREA)
8092 {
8093 prev_method = it->method;
8094 if (it->method == GET_FROM_BUFFER)
8095 prev_pos = IT_CHARPOS (*it);
8096 set_iterator_to_next (it, 1);
8097 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8098 SET_TEXT_POS (this_line_min_pos,
8099 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8100 if (it->bidi_p
8101 && (op & MOVE_TO_POS)
8102 && IT_CHARPOS (*it) > to_charpos
8103 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8104 SAVE_IT (ppos_it, *it, ppos_data);
8105 continue;
8106 }
8107
8108 /* The number of glyphs we get back in IT->nglyphs will normally
8109 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8110 character on a terminal frame, or (iii) a line end. For the
8111 second case, IT->nglyphs - 1 padding glyphs will be present.
8112 (On X frames, there is only one glyph produced for a
8113 composite character.)
8114
8115 The behavior implemented below means, for continuation lines,
8116 that as many spaces of a TAB as fit on the current line are
8117 displayed there. For terminal frames, as many glyphs of a
8118 multi-glyph character are displayed in the current line, too.
8119 This is what the old redisplay code did, and we keep it that
8120 way. Under X, the whole shape of a complex character must
8121 fit on the line or it will be completely displayed in the
8122 next line.
8123
8124 Note that both for tabs and padding glyphs, all glyphs have
8125 the same width. */
8126 if (it->nglyphs)
8127 {
8128 /* More than one glyph or glyph doesn't fit on line. All
8129 glyphs have the same width. */
8130 int single_glyph_width = it->pixel_width / it->nglyphs;
8131 int new_x;
8132 int x_before_this_char = x;
8133 int hpos_before_this_char = it->hpos;
8134
8135 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8136 {
8137 new_x = x + single_glyph_width;
8138
8139 /* We want to leave anything reaching TO_X to the caller. */
8140 if ((op & MOVE_TO_X) && new_x > to_x)
8141 {
8142 if (BUFFER_POS_REACHED_P ())
8143 {
8144 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8145 goto buffer_pos_reached;
8146 if (atpos_it.sp < 0)
8147 {
8148 SAVE_IT (atpos_it, *it, atpos_data);
8149 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8150 }
8151 }
8152 else
8153 {
8154 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8155 {
8156 it->current_x = x;
8157 result = MOVE_X_REACHED;
8158 break;
8159 }
8160 if (atx_it.sp < 0)
8161 {
8162 SAVE_IT (atx_it, *it, atx_data);
8163 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8164 }
8165 }
8166 }
8167
8168 if (/* Lines are continued. */
8169 it->line_wrap != TRUNCATE
8170 && (/* And glyph doesn't fit on the line. */
8171 new_x > it->last_visible_x
8172 /* Or it fits exactly and we're on a window
8173 system frame. */
8174 || (new_x == it->last_visible_x
8175 && FRAME_WINDOW_P (it->f))))
8176 {
8177 if (/* IT->hpos == 0 means the very first glyph
8178 doesn't fit on the line, e.g. a wide image. */
8179 it->hpos == 0
8180 || (new_x == it->last_visible_x
8181 && FRAME_WINDOW_P (it->f)))
8182 {
8183 ++it->hpos;
8184 it->current_x = new_x;
8185
8186 /* The character's last glyph just barely fits
8187 in this row. */
8188 if (i == it->nglyphs - 1)
8189 {
8190 /* If this is the destination position,
8191 return a position *before* it in this row,
8192 now that we know it fits in this row. */
8193 if (BUFFER_POS_REACHED_P ())
8194 {
8195 if (it->line_wrap != WORD_WRAP
8196 || wrap_it.sp < 0)
8197 {
8198 it->hpos = hpos_before_this_char;
8199 it->current_x = x_before_this_char;
8200 result = MOVE_POS_MATCH_OR_ZV;
8201 break;
8202 }
8203 if (it->line_wrap == WORD_WRAP
8204 && atpos_it.sp < 0)
8205 {
8206 SAVE_IT (atpos_it, *it, atpos_data);
8207 atpos_it.current_x = x_before_this_char;
8208 atpos_it.hpos = hpos_before_this_char;
8209 }
8210 }
8211
8212 prev_method = it->method;
8213 if (it->method == GET_FROM_BUFFER)
8214 prev_pos = IT_CHARPOS (*it);
8215 set_iterator_to_next (it, 1);
8216 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8217 SET_TEXT_POS (this_line_min_pos,
8218 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8219 /* On graphical terminals, newlines may
8220 "overflow" into the fringe if
8221 overflow-newline-into-fringe is non-nil.
8222 On text-only terminals, newlines may
8223 overflow into the last glyph on the
8224 display line.*/
8225 if (!FRAME_WINDOW_P (it->f)
8226 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8227 {
8228 if (!get_next_display_element (it))
8229 {
8230 result = MOVE_POS_MATCH_OR_ZV;
8231 break;
8232 }
8233 if (BUFFER_POS_REACHED_P ())
8234 {
8235 if (ITERATOR_AT_END_OF_LINE_P (it))
8236 result = MOVE_POS_MATCH_OR_ZV;
8237 else
8238 result = MOVE_LINE_CONTINUED;
8239 break;
8240 }
8241 if (ITERATOR_AT_END_OF_LINE_P (it))
8242 {
8243 result = MOVE_NEWLINE_OR_CR;
8244 break;
8245 }
8246 }
8247 }
8248 }
8249 else
8250 IT_RESET_X_ASCENT_DESCENT (it);
8251
8252 if (wrap_it.sp >= 0)
8253 {
8254 RESTORE_IT (it, &wrap_it, wrap_data);
8255 atpos_it.sp = -1;
8256 atx_it.sp = -1;
8257 }
8258
8259 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8260 IT_CHARPOS (*it)));
8261 result = MOVE_LINE_CONTINUED;
8262 break;
8263 }
8264
8265 if (BUFFER_POS_REACHED_P ())
8266 {
8267 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8268 goto buffer_pos_reached;
8269 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8270 {
8271 SAVE_IT (atpos_it, *it, atpos_data);
8272 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8273 }
8274 }
8275
8276 if (new_x > it->first_visible_x)
8277 {
8278 /* Glyph is visible. Increment number of glyphs that
8279 would be displayed. */
8280 ++it->hpos;
8281 }
8282 }
8283
8284 if (result != MOVE_UNDEFINED)
8285 break;
8286 }
8287 else if (BUFFER_POS_REACHED_P ())
8288 {
8289 buffer_pos_reached:
8290 IT_RESET_X_ASCENT_DESCENT (it);
8291 result = MOVE_POS_MATCH_OR_ZV;
8292 break;
8293 }
8294 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8295 {
8296 /* Stop when TO_X specified and reached. This check is
8297 necessary here because of lines consisting of a line end,
8298 only. The line end will not produce any glyphs and we
8299 would never get MOVE_X_REACHED. */
8300 xassert (it->nglyphs == 0);
8301 result = MOVE_X_REACHED;
8302 break;
8303 }
8304
8305 /* Is this a line end? If yes, we're done. */
8306 if (ITERATOR_AT_END_OF_LINE_P (it))
8307 {
8308 /* If we are past TO_CHARPOS, but never saw any character
8309 positions smaller than TO_CHARPOS, return
8310 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8311 did. */
8312 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8313 {
8314 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8315 {
8316 if (IT_CHARPOS (ppos_it) < ZV)
8317 {
8318 RESTORE_IT (it, &ppos_it, ppos_data);
8319 result = MOVE_POS_MATCH_OR_ZV;
8320 }
8321 else
8322 goto buffer_pos_reached;
8323 }
8324 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8325 && IT_CHARPOS (*it) > to_charpos)
8326 goto buffer_pos_reached;
8327 else
8328 result = MOVE_NEWLINE_OR_CR;
8329 }
8330 else
8331 result = MOVE_NEWLINE_OR_CR;
8332 break;
8333 }
8334
8335 prev_method = it->method;
8336 if (it->method == GET_FROM_BUFFER)
8337 prev_pos = IT_CHARPOS (*it);
8338 /* The current display element has been consumed. Advance
8339 to the next. */
8340 set_iterator_to_next (it, 1);
8341 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8342 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8343 if (IT_CHARPOS (*it) < to_charpos)
8344 saw_smaller_pos = 1;
8345 if (it->bidi_p
8346 && (op & MOVE_TO_POS)
8347 && IT_CHARPOS (*it) >= to_charpos
8348 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8349 SAVE_IT (ppos_it, *it, ppos_data);
8350
8351 /* Stop if lines are truncated and IT's current x-position is
8352 past the right edge of the window now. */
8353 if (it->line_wrap == TRUNCATE
8354 && it->current_x >= it->last_visible_x)
8355 {
8356 if (!FRAME_WINDOW_P (it->f)
8357 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8358 {
8359 int at_eob_p = 0;
8360
8361 if ((at_eob_p = !get_next_display_element (it))
8362 || BUFFER_POS_REACHED_P ()
8363 /* If we are past TO_CHARPOS, but never saw any
8364 character positions smaller than TO_CHARPOS,
8365 return MOVE_POS_MATCH_OR_ZV, like the
8366 unidirectional display did. */
8367 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8368 && !saw_smaller_pos
8369 && IT_CHARPOS (*it) > to_charpos))
8370 {
8371 if (it->bidi_p
8372 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8373 RESTORE_IT (it, &ppos_it, ppos_data);
8374 result = MOVE_POS_MATCH_OR_ZV;
8375 break;
8376 }
8377 if (ITERATOR_AT_END_OF_LINE_P (it))
8378 {
8379 result = MOVE_NEWLINE_OR_CR;
8380 break;
8381 }
8382 }
8383 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8384 && !saw_smaller_pos
8385 && IT_CHARPOS (*it) > to_charpos)
8386 {
8387 if (IT_CHARPOS (ppos_it) < ZV)
8388 RESTORE_IT (it, &ppos_it, ppos_data);
8389 result = MOVE_POS_MATCH_OR_ZV;
8390 break;
8391 }
8392 result = MOVE_LINE_TRUNCATED;
8393 break;
8394 }
8395 #undef IT_RESET_X_ASCENT_DESCENT
8396 }
8397
8398 #undef BUFFER_POS_REACHED_P
8399
8400 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8401 restore the saved iterator. */
8402 if (atpos_it.sp >= 0)
8403 RESTORE_IT (it, &atpos_it, atpos_data);
8404 else if (atx_it.sp >= 0)
8405 RESTORE_IT (it, &atx_it, atx_data);
8406
8407 done:
8408
8409 if (atpos_data)
8410 bidi_unshelve_cache (atpos_data, 1);
8411 if (atx_data)
8412 bidi_unshelve_cache (atx_data, 1);
8413 if (wrap_data)
8414 bidi_unshelve_cache (wrap_data, 1);
8415 if (ppos_data)
8416 bidi_unshelve_cache (ppos_data, 1);
8417
8418 /* Restore the iterator settings altered at the beginning of this
8419 function. */
8420 it->glyph_row = saved_glyph_row;
8421 return result;
8422 }
8423
8424 /* For external use. */
8425 void
8426 move_it_in_display_line (struct it *it,
8427 ptrdiff_t to_charpos, int to_x,
8428 enum move_operation_enum op)
8429 {
8430 if (it->line_wrap == WORD_WRAP
8431 && (op & MOVE_TO_X))
8432 {
8433 struct it save_it;
8434 void *save_data = NULL;
8435 int skip;
8436
8437 SAVE_IT (save_it, *it, save_data);
8438 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8439 /* When word-wrap is on, TO_X may lie past the end
8440 of a wrapped line. Then it->current is the
8441 character on the next line, so backtrack to the
8442 space before the wrap point. */
8443 if (skip == MOVE_LINE_CONTINUED)
8444 {
8445 int prev_x = max (it->current_x - 1, 0);
8446 RESTORE_IT (it, &save_it, save_data);
8447 move_it_in_display_line_to
8448 (it, -1, prev_x, MOVE_TO_X);
8449 }
8450 else
8451 bidi_unshelve_cache (save_data, 1);
8452 }
8453 else
8454 move_it_in_display_line_to (it, to_charpos, to_x, op);
8455 }
8456
8457
8458 /* Move IT forward until it satisfies one or more of the criteria in
8459 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8460
8461 OP is a bit-mask that specifies where to stop, and in particular,
8462 which of those four position arguments makes a difference. See the
8463 description of enum move_operation_enum.
8464
8465 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8466 screen line, this function will set IT to the next position that is
8467 displayed to the right of TO_CHARPOS on the screen. */
8468
8469 void
8470 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8471 {
8472 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8473 int line_height, line_start_x = 0, reached = 0;
8474 void *backup_data = NULL;
8475
8476 for (;;)
8477 {
8478 if (op & MOVE_TO_VPOS)
8479 {
8480 /* If no TO_CHARPOS and no TO_X specified, stop at the
8481 start of the line TO_VPOS. */
8482 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8483 {
8484 if (it->vpos == to_vpos)
8485 {
8486 reached = 1;
8487 break;
8488 }
8489 else
8490 skip = move_it_in_display_line_to (it, -1, -1, 0);
8491 }
8492 else
8493 {
8494 /* TO_VPOS >= 0 means stop at TO_X in the line at
8495 TO_VPOS, or at TO_POS, whichever comes first. */
8496 if (it->vpos == to_vpos)
8497 {
8498 reached = 2;
8499 break;
8500 }
8501
8502 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8503
8504 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8505 {
8506 reached = 3;
8507 break;
8508 }
8509 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8510 {
8511 /* We have reached TO_X but not in the line we want. */
8512 skip = move_it_in_display_line_to (it, to_charpos,
8513 -1, MOVE_TO_POS);
8514 if (skip == MOVE_POS_MATCH_OR_ZV)
8515 {
8516 reached = 4;
8517 break;
8518 }
8519 }
8520 }
8521 }
8522 else if (op & MOVE_TO_Y)
8523 {
8524 struct it it_backup;
8525
8526 if (it->line_wrap == WORD_WRAP)
8527 SAVE_IT (it_backup, *it, backup_data);
8528
8529 /* TO_Y specified means stop at TO_X in the line containing
8530 TO_Y---or at TO_CHARPOS if this is reached first. The
8531 problem is that we can't really tell whether the line
8532 contains TO_Y before we have completely scanned it, and
8533 this may skip past TO_X. What we do is to first scan to
8534 TO_X.
8535
8536 If TO_X is not specified, use a TO_X of zero. The reason
8537 is to make the outcome of this function more predictable.
8538 If we didn't use TO_X == 0, we would stop at the end of
8539 the line which is probably not what a caller would expect
8540 to happen. */
8541 skip = move_it_in_display_line_to
8542 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8543 (MOVE_TO_X | (op & MOVE_TO_POS)));
8544
8545 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8546 if (skip == MOVE_POS_MATCH_OR_ZV)
8547 reached = 5;
8548 else if (skip == MOVE_X_REACHED)
8549 {
8550 /* If TO_X was reached, we want to know whether TO_Y is
8551 in the line. We know this is the case if the already
8552 scanned glyphs make the line tall enough. Otherwise,
8553 we must check by scanning the rest of the line. */
8554 line_height = it->max_ascent + it->max_descent;
8555 if (to_y >= it->current_y
8556 && to_y < it->current_y + line_height)
8557 {
8558 reached = 6;
8559 break;
8560 }
8561 SAVE_IT (it_backup, *it, backup_data);
8562 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8563 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8564 op & MOVE_TO_POS);
8565 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8566 line_height = it->max_ascent + it->max_descent;
8567 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8568
8569 if (to_y >= it->current_y
8570 && to_y < it->current_y + line_height)
8571 {
8572 /* If TO_Y is in this line and TO_X was reached
8573 above, we scanned too far. We have to restore
8574 IT's settings to the ones before skipping. */
8575 RESTORE_IT (it, &it_backup, backup_data);
8576 reached = 6;
8577 }
8578 else
8579 {
8580 skip = skip2;
8581 if (skip == MOVE_POS_MATCH_OR_ZV)
8582 reached = 7;
8583 }
8584 }
8585 else
8586 {
8587 /* Check whether TO_Y is in this line. */
8588 line_height = it->max_ascent + it->max_descent;
8589 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8590
8591 if (to_y >= it->current_y
8592 && to_y < it->current_y + line_height)
8593 {
8594 /* When word-wrap is on, TO_X may lie past the end
8595 of a wrapped line. Then it->current is the
8596 character on the next line, so backtrack to the
8597 space before the wrap point. */
8598 if (skip == MOVE_LINE_CONTINUED
8599 && it->line_wrap == WORD_WRAP)
8600 {
8601 int prev_x = max (it->current_x - 1, 0);
8602 RESTORE_IT (it, &it_backup, backup_data);
8603 skip = move_it_in_display_line_to
8604 (it, -1, prev_x, MOVE_TO_X);
8605 }
8606 reached = 6;
8607 }
8608 }
8609
8610 if (reached)
8611 break;
8612 }
8613 else if (BUFFERP (it->object)
8614 && (it->method == GET_FROM_BUFFER
8615 || it->method == GET_FROM_STRETCH)
8616 && IT_CHARPOS (*it) >= to_charpos
8617 /* Under bidi iteration, a call to set_iterator_to_next
8618 can scan far beyond to_charpos if the initial
8619 portion of the next line needs to be reordered. In
8620 that case, give move_it_in_display_line_to another
8621 chance below. */
8622 && !(it->bidi_p
8623 && it->bidi_it.scan_dir == -1))
8624 skip = MOVE_POS_MATCH_OR_ZV;
8625 else
8626 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8627
8628 switch (skip)
8629 {
8630 case MOVE_POS_MATCH_OR_ZV:
8631 reached = 8;
8632 goto out;
8633
8634 case MOVE_NEWLINE_OR_CR:
8635 set_iterator_to_next (it, 1);
8636 it->continuation_lines_width = 0;
8637 break;
8638
8639 case MOVE_LINE_TRUNCATED:
8640 it->continuation_lines_width = 0;
8641 reseat_at_next_visible_line_start (it, 0);
8642 if ((op & MOVE_TO_POS) != 0
8643 && IT_CHARPOS (*it) > to_charpos)
8644 {
8645 reached = 9;
8646 goto out;
8647 }
8648 break;
8649
8650 case MOVE_LINE_CONTINUED:
8651 /* For continued lines ending in a tab, some of the glyphs
8652 associated with the tab are displayed on the current
8653 line. Since it->current_x does not include these glyphs,
8654 we use it->last_visible_x instead. */
8655 if (it->c == '\t')
8656 {
8657 it->continuation_lines_width += it->last_visible_x;
8658 /* When moving by vpos, ensure that the iterator really
8659 advances to the next line (bug#847, bug#969). Fixme:
8660 do we need to do this in other circumstances? */
8661 if (it->current_x != it->last_visible_x
8662 && (op & MOVE_TO_VPOS)
8663 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8664 {
8665 line_start_x = it->current_x + it->pixel_width
8666 - it->last_visible_x;
8667 set_iterator_to_next (it, 0);
8668 }
8669 }
8670 else
8671 it->continuation_lines_width += it->current_x;
8672 break;
8673
8674 default:
8675 abort ();
8676 }
8677
8678 /* Reset/increment for the next run. */
8679 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8680 it->current_x = line_start_x;
8681 line_start_x = 0;
8682 it->hpos = 0;
8683 it->current_y += it->max_ascent + it->max_descent;
8684 ++it->vpos;
8685 last_height = it->max_ascent + it->max_descent;
8686 last_max_ascent = it->max_ascent;
8687 it->max_ascent = it->max_descent = 0;
8688 }
8689
8690 out:
8691
8692 /* On text terminals, we may stop at the end of a line in the middle
8693 of a multi-character glyph. If the glyph itself is continued,
8694 i.e. it is actually displayed on the next line, don't treat this
8695 stopping point as valid; move to the next line instead (unless
8696 that brings us offscreen). */
8697 if (!FRAME_WINDOW_P (it->f)
8698 && op & MOVE_TO_POS
8699 && IT_CHARPOS (*it) == to_charpos
8700 && it->what == IT_CHARACTER
8701 && it->nglyphs > 1
8702 && it->line_wrap == WINDOW_WRAP
8703 && it->current_x == it->last_visible_x - 1
8704 && it->c != '\n'
8705 && it->c != '\t'
8706 && it->vpos < XFASTINT (it->w->window_end_vpos))
8707 {
8708 it->continuation_lines_width += it->current_x;
8709 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8710 it->current_y += it->max_ascent + it->max_descent;
8711 ++it->vpos;
8712 last_height = it->max_ascent + it->max_descent;
8713 last_max_ascent = it->max_ascent;
8714 }
8715
8716 if (backup_data)
8717 bidi_unshelve_cache (backup_data, 1);
8718
8719 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8720 }
8721
8722
8723 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8724
8725 If DY > 0, move IT backward at least that many pixels. DY = 0
8726 means move IT backward to the preceding line start or BEGV. This
8727 function may move over more than DY pixels if IT->current_y - DY
8728 ends up in the middle of a line; in this case IT->current_y will be
8729 set to the top of the line moved to. */
8730
8731 void
8732 move_it_vertically_backward (struct it *it, int dy)
8733 {
8734 int nlines, h;
8735 struct it it2, it3;
8736 void *it2data = NULL, *it3data = NULL;
8737 ptrdiff_t start_pos;
8738
8739 move_further_back:
8740 xassert (dy >= 0);
8741
8742 start_pos = IT_CHARPOS (*it);
8743
8744 /* Estimate how many newlines we must move back. */
8745 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8746
8747 /* Set the iterator's position that many lines back. */
8748 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8749 back_to_previous_visible_line_start (it);
8750
8751 /* Reseat the iterator here. When moving backward, we don't want
8752 reseat to skip forward over invisible text, set up the iterator
8753 to deliver from overlay strings at the new position etc. So,
8754 use reseat_1 here. */
8755 reseat_1 (it, it->current.pos, 1);
8756
8757 /* We are now surely at a line start. */
8758 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8759 reordering is in effect. */
8760 it->continuation_lines_width = 0;
8761
8762 /* Move forward and see what y-distance we moved. First move to the
8763 start of the next line so that we get its height. We need this
8764 height to be able to tell whether we reached the specified
8765 y-distance. */
8766 SAVE_IT (it2, *it, it2data);
8767 it2.max_ascent = it2.max_descent = 0;
8768 do
8769 {
8770 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8771 MOVE_TO_POS | MOVE_TO_VPOS);
8772 }
8773 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8774 /* If we are in a display string which starts at START_POS,
8775 and that display string includes a newline, and we are
8776 right after that newline (i.e. at the beginning of a
8777 display line), exit the loop, because otherwise we will
8778 infloop, since move_it_to will see that it is already at
8779 START_POS and will not move. */
8780 || (it2.method == GET_FROM_STRING
8781 && IT_CHARPOS (it2) == start_pos
8782 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8783 xassert (IT_CHARPOS (*it) >= BEGV);
8784 SAVE_IT (it3, it2, it3data);
8785
8786 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8787 xassert (IT_CHARPOS (*it) >= BEGV);
8788 /* H is the actual vertical distance from the position in *IT
8789 and the starting position. */
8790 h = it2.current_y - it->current_y;
8791 /* NLINES is the distance in number of lines. */
8792 nlines = it2.vpos - it->vpos;
8793
8794 /* Correct IT's y and vpos position
8795 so that they are relative to the starting point. */
8796 it->vpos -= nlines;
8797 it->current_y -= h;
8798
8799 if (dy == 0)
8800 {
8801 /* DY == 0 means move to the start of the screen line. The
8802 value of nlines is > 0 if continuation lines were involved,
8803 or if the original IT position was at start of a line. */
8804 RESTORE_IT (it, it, it2data);
8805 if (nlines > 0)
8806 move_it_by_lines (it, nlines);
8807 /* The above code moves us to some position NLINES down,
8808 usually to its first glyph (leftmost in an L2R line), but
8809 that's not necessarily the start of the line, under bidi
8810 reordering. We want to get to the character position
8811 that is immediately after the newline of the previous
8812 line. */
8813 if (it->bidi_p
8814 && !it->continuation_lines_width
8815 && !STRINGP (it->string)
8816 && IT_CHARPOS (*it) > BEGV
8817 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8818 {
8819 ptrdiff_t nl_pos =
8820 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8821
8822 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8823 }
8824 bidi_unshelve_cache (it3data, 1);
8825 }
8826 else
8827 {
8828 /* The y-position we try to reach, relative to *IT.
8829 Note that H has been subtracted in front of the if-statement. */
8830 int target_y = it->current_y + h - dy;
8831 int y0 = it3.current_y;
8832 int y1;
8833 int line_height;
8834
8835 RESTORE_IT (&it3, &it3, it3data);
8836 y1 = line_bottom_y (&it3);
8837 line_height = y1 - y0;
8838 RESTORE_IT (it, it, it2data);
8839 /* If we did not reach target_y, try to move further backward if
8840 we can. If we moved too far backward, try to move forward. */
8841 if (target_y < it->current_y
8842 /* This is heuristic. In a window that's 3 lines high, with
8843 a line height of 13 pixels each, recentering with point
8844 on the bottom line will try to move -39/2 = 19 pixels
8845 backward. Try to avoid moving into the first line. */
8846 && (it->current_y - target_y
8847 > min (window_box_height (it->w), line_height * 2 / 3))
8848 && IT_CHARPOS (*it) > BEGV)
8849 {
8850 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8851 target_y - it->current_y));
8852 dy = it->current_y - target_y;
8853 goto move_further_back;
8854 }
8855 else if (target_y >= it->current_y + line_height
8856 && IT_CHARPOS (*it) < ZV)
8857 {
8858 /* Should move forward by at least one line, maybe more.
8859
8860 Note: Calling move_it_by_lines can be expensive on
8861 terminal frames, where compute_motion is used (via
8862 vmotion) to do the job, when there are very long lines
8863 and truncate-lines is nil. That's the reason for
8864 treating terminal frames specially here. */
8865
8866 if (!FRAME_WINDOW_P (it->f))
8867 move_it_vertically (it, target_y - (it->current_y + line_height));
8868 else
8869 {
8870 do
8871 {
8872 move_it_by_lines (it, 1);
8873 }
8874 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
8875 }
8876 }
8877 }
8878 }
8879
8880
8881 /* Move IT by a specified amount of pixel lines DY. DY negative means
8882 move backwards. DY = 0 means move to start of screen line. At the
8883 end, IT will be on the start of a screen line. */
8884
8885 void
8886 move_it_vertically (struct it *it, int dy)
8887 {
8888 if (dy <= 0)
8889 move_it_vertically_backward (it, -dy);
8890 else
8891 {
8892 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
8893 move_it_to (it, ZV, -1, it->current_y + dy, -1,
8894 MOVE_TO_POS | MOVE_TO_Y);
8895 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
8896
8897 /* If buffer ends in ZV without a newline, move to the start of
8898 the line to satisfy the post-condition. */
8899 if (IT_CHARPOS (*it) == ZV
8900 && ZV > BEGV
8901 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8902 move_it_by_lines (it, 0);
8903 }
8904 }
8905
8906
8907 /* Move iterator IT past the end of the text line it is in. */
8908
8909 void
8910 move_it_past_eol (struct it *it)
8911 {
8912 enum move_it_result rc;
8913
8914 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
8915 if (rc == MOVE_NEWLINE_OR_CR)
8916 set_iterator_to_next (it, 0);
8917 }
8918
8919
8920 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
8921 negative means move up. DVPOS == 0 means move to the start of the
8922 screen line.
8923
8924 Optimization idea: If we would know that IT->f doesn't use
8925 a face with proportional font, we could be faster for
8926 truncate-lines nil. */
8927
8928 void
8929 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
8930 {
8931
8932 /* The commented-out optimization uses vmotion on terminals. This
8933 gives bad results, because elements like it->what, on which
8934 callers such as pos_visible_p rely, aren't updated. */
8935 /* struct position pos;
8936 if (!FRAME_WINDOW_P (it->f))
8937 {
8938 struct text_pos textpos;
8939
8940 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
8941 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
8942 reseat (it, textpos, 1);
8943 it->vpos += pos.vpos;
8944 it->current_y += pos.vpos;
8945 }
8946 else */
8947
8948 if (dvpos == 0)
8949 {
8950 /* DVPOS == 0 means move to the start of the screen line. */
8951 move_it_vertically_backward (it, 0);
8952 xassert (it->current_x == 0 && it->hpos == 0);
8953 /* Let next call to line_bottom_y calculate real line height */
8954 last_height = 0;
8955 }
8956 else if (dvpos > 0)
8957 {
8958 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
8959 if (!IT_POS_VALID_AFTER_MOVE_P (it))
8960 move_it_to (it, IT_CHARPOS (*it) + 1, -1, -1, -1, MOVE_TO_POS);
8961 }
8962 else
8963 {
8964 struct it it2;
8965 void *it2data = NULL;
8966 ptrdiff_t start_charpos, i;
8967
8968 /* Start at the beginning of the screen line containing IT's
8969 position. This may actually move vertically backwards,
8970 in case of overlays, so adjust dvpos accordingly. */
8971 dvpos += it->vpos;
8972 move_it_vertically_backward (it, 0);
8973 dvpos -= it->vpos;
8974
8975 /* Go back -DVPOS visible lines and reseat the iterator there. */
8976 start_charpos = IT_CHARPOS (*it);
8977 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
8978 back_to_previous_visible_line_start (it);
8979 reseat (it, it->current.pos, 1);
8980
8981 /* Move further back if we end up in a string or an image. */
8982 while (!IT_POS_VALID_AFTER_MOVE_P (it))
8983 {
8984 /* First try to move to start of display line. */
8985 dvpos += it->vpos;
8986 move_it_vertically_backward (it, 0);
8987 dvpos -= it->vpos;
8988 if (IT_POS_VALID_AFTER_MOVE_P (it))
8989 break;
8990 /* If start of line is still in string or image,
8991 move further back. */
8992 back_to_previous_visible_line_start (it);
8993 reseat (it, it->current.pos, 1);
8994 dvpos--;
8995 }
8996
8997 it->current_x = it->hpos = 0;
8998
8999 /* Above call may have moved too far if continuation lines
9000 are involved. Scan forward and see if it did. */
9001 SAVE_IT (it2, *it, it2data);
9002 it2.vpos = it2.current_y = 0;
9003 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9004 it->vpos -= it2.vpos;
9005 it->current_y -= it2.current_y;
9006 it->current_x = it->hpos = 0;
9007
9008 /* If we moved too far back, move IT some lines forward. */
9009 if (it2.vpos > -dvpos)
9010 {
9011 int delta = it2.vpos + dvpos;
9012
9013 RESTORE_IT (&it2, &it2, it2data);
9014 SAVE_IT (it2, *it, it2data);
9015 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9016 /* Move back again if we got too far ahead. */
9017 if (IT_CHARPOS (*it) >= start_charpos)
9018 RESTORE_IT (it, &it2, it2data);
9019 else
9020 bidi_unshelve_cache (it2data, 1);
9021 }
9022 else
9023 RESTORE_IT (it, it, it2data);
9024 }
9025 }
9026
9027 /* Return 1 if IT points into the middle of a display vector. */
9028
9029 int
9030 in_display_vector_p (struct it *it)
9031 {
9032 return (it->method == GET_FROM_DISPLAY_VECTOR
9033 && it->current.dpvec_index > 0
9034 && it->dpvec + it->current.dpvec_index != it->dpend);
9035 }
9036
9037 \f
9038 /***********************************************************************
9039 Messages
9040 ***********************************************************************/
9041
9042
9043 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9044 to *Messages*. */
9045
9046 void
9047 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9048 {
9049 Lisp_Object args[3];
9050 Lisp_Object msg, fmt;
9051 char *buffer;
9052 ptrdiff_t len;
9053 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9054 USE_SAFE_ALLOCA;
9055
9056 /* Do nothing if called asynchronously. Inserting text into
9057 a buffer may call after-change-functions and alike and
9058 that would means running Lisp asynchronously. */
9059 if (handling_signal)
9060 return;
9061
9062 fmt = msg = Qnil;
9063 GCPRO4 (fmt, msg, arg1, arg2);
9064
9065 args[0] = fmt = build_string (format);
9066 args[1] = arg1;
9067 args[2] = arg2;
9068 msg = Fformat (3, args);
9069
9070 len = SBYTES (msg) + 1;
9071 SAFE_ALLOCA (buffer, char *, len);
9072 memcpy (buffer, SDATA (msg), len);
9073
9074 message_dolog (buffer, len - 1, 1, 0);
9075 SAFE_FREE ();
9076
9077 UNGCPRO;
9078 }
9079
9080
9081 /* Output a newline in the *Messages* buffer if "needs" one. */
9082
9083 void
9084 message_log_maybe_newline (void)
9085 {
9086 if (message_log_need_newline)
9087 message_dolog ("", 0, 1, 0);
9088 }
9089
9090
9091 /* Add a string M of length NBYTES to the message log, optionally
9092 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9093 nonzero, means interpret the contents of M as multibyte. This
9094 function calls low-level routines in order to bypass text property
9095 hooks, etc. which might not be safe to run.
9096
9097 This may GC (insert may run before/after change hooks),
9098 so the buffer M must NOT point to a Lisp string. */
9099
9100 void
9101 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9102 {
9103 const unsigned char *msg = (const unsigned char *) m;
9104
9105 if (!NILP (Vmemory_full))
9106 return;
9107
9108 if (!NILP (Vmessage_log_max))
9109 {
9110 struct buffer *oldbuf;
9111 Lisp_Object oldpoint, oldbegv, oldzv;
9112 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9113 ptrdiff_t point_at_end = 0;
9114 ptrdiff_t zv_at_end = 0;
9115 Lisp_Object old_deactivate_mark, tem;
9116 struct gcpro gcpro1;
9117
9118 old_deactivate_mark = Vdeactivate_mark;
9119 oldbuf = current_buffer;
9120 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9121 BVAR (current_buffer, undo_list) = Qt;
9122
9123 oldpoint = message_dolog_marker1;
9124 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9125 oldbegv = message_dolog_marker2;
9126 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9127 oldzv = message_dolog_marker3;
9128 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9129 GCPRO1 (old_deactivate_mark);
9130
9131 if (PT == Z)
9132 point_at_end = 1;
9133 if (ZV == Z)
9134 zv_at_end = 1;
9135
9136 BEGV = BEG;
9137 BEGV_BYTE = BEG_BYTE;
9138 ZV = Z;
9139 ZV_BYTE = Z_BYTE;
9140 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9141
9142 /* Insert the string--maybe converting multibyte to single byte
9143 or vice versa, so that all the text fits the buffer. */
9144 if (multibyte
9145 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9146 {
9147 ptrdiff_t i;
9148 int c, char_bytes;
9149 char work[1];
9150
9151 /* Convert a multibyte string to single-byte
9152 for the *Message* buffer. */
9153 for (i = 0; i < nbytes; i += char_bytes)
9154 {
9155 c = string_char_and_length (msg + i, &char_bytes);
9156 work[0] = (ASCII_CHAR_P (c)
9157 ? c
9158 : multibyte_char_to_unibyte (c));
9159 insert_1_both (work, 1, 1, 1, 0, 0);
9160 }
9161 }
9162 else if (! multibyte
9163 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9164 {
9165 ptrdiff_t i;
9166 int c, char_bytes;
9167 unsigned char str[MAX_MULTIBYTE_LENGTH];
9168 /* Convert a single-byte string to multibyte
9169 for the *Message* buffer. */
9170 for (i = 0; i < nbytes; i++)
9171 {
9172 c = msg[i];
9173 MAKE_CHAR_MULTIBYTE (c);
9174 char_bytes = CHAR_STRING (c, str);
9175 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9176 }
9177 }
9178 else if (nbytes)
9179 insert_1 (m, nbytes, 1, 0, 0);
9180
9181 if (nlflag)
9182 {
9183 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9184 printmax_t dups;
9185 insert_1 ("\n", 1, 1, 0, 0);
9186
9187 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9188 this_bol = PT;
9189 this_bol_byte = PT_BYTE;
9190
9191 /* See if this line duplicates the previous one.
9192 If so, combine duplicates. */
9193 if (this_bol > BEG)
9194 {
9195 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9196 prev_bol = PT;
9197 prev_bol_byte = PT_BYTE;
9198
9199 dups = message_log_check_duplicate (prev_bol_byte,
9200 this_bol_byte);
9201 if (dups)
9202 {
9203 del_range_both (prev_bol, prev_bol_byte,
9204 this_bol, this_bol_byte, 0);
9205 if (dups > 1)
9206 {
9207 char dupstr[sizeof " [ times]"
9208 + INT_STRLEN_BOUND (printmax_t)];
9209 int duplen;
9210
9211 /* If you change this format, don't forget to also
9212 change message_log_check_duplicate. */
9213 sprintf (dupstr, " [%"pMd" times]", dups);
9214 duplen = strlen (dupstr);
9215 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9216 insert_1 (dupstr, duplen, 1, 0, 1);
9217 }
9218 }
9219 }
9220
9221 /* If we have more than the desired maximum number of lines
9222 in the *Messages* buffer now, delete the oldest ones.
9223 This is safe because we don't have undo in this buffer. */
9224
9225 if (NATNUMP (Vmessage_log_max))
9226 {
9227 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9228 -XFASTINT (Vmessage_log_max) - 1, 0);
9229 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9230 }
9231 }
9232 BEGV = XMARKER (oldbegv)->charpos;
9233 BEGV_BYTE = marker_byte_position (oldbegv);
9234
9235 if (zv_at_end)
9236 {
9237 ZV = Z;
9238 ZV_BYTE = Z_BYTE;
9239 }
9240 else
9241 {
9242 ZV = XMARKER (oldzv)->charpos;
9243 ZV_BYTE = marker_byte_position (oldzv);
9244 }
9245
9246 if (point_at_end)
9247 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9248 else
9249 /* We can't do Fgoto_char (oldpoint) because it will run some
9250 Lisp code. */
9251 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9252 XMARKER (oldpoint)->bytepos);
9253
9254 UNGCPRO;
9255 unchain_marker (XMARKER (oldpoint));
9256 unchain_marker (XMARKER (oldbegv));
9257 unchain_marker (XMARKER (oldzv));
9258
9259 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9260 set_buffer_internal (oldbuf);
9261 if (NILP (tem))
9262 windows_or_buffers_changed = old_windows_or_buffers_changed;
9263 message_log_need_newline = !nlflag;
9264 Vdeactivate_mark = old_deactivate_mark;
9265 }
9266 }
9267
9268
9269 /* We are at the end of the buffer after just having inserted a newline.
9270 (Note: We depend on the fact we won't be crossing the gap.)
9271 Check to see if the most recent message looks a lot like the previous one.
9272 Return 0 if different, 1 if the new one should just replace it, or a
9273 value N > 1 if we should also append " [N times]". */
9274
9275 static intmax_t
9276 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9277 {
9278 ptrdiff_t i;
9279 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9280 int seen_dots = 0;
9281 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9282 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9283
9284 for (i = 0; i < len; i++)
9285 {
9286 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9287 seen_dots = 1;
9288 if (p1[i] != p2[i])
9289 return seen_dots;
9290 }
9291 p1 += len;
9292 if (*p1 == '\n')
9293 return 2;
9294 if (*p1++ == ' ' && *p1++ == '[')
9295 {
9296 char *pend;
9297 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9298 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9299 return n+1;
9300 }
9301 return 0;
9302 }
9303 \f
9304
9305 /* Display an echo area message M with a specified length of NBYTES
9306 bytes. The string may include null characters. If M is 0, clear
9307 out any existing message, and let the mini-buffer text show
9308 through.
9309
9310 This may GC, so the buffer M must NOT point to a Lisp string. */
9311
9312 void
9313 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9314 {
9315 /* First flush out any partial line written with print. */
9316 message_log_maybe_newline ();
9317 if (m)
9318 message_dolog (m, nbytes, 1, multibyte);
9319 message2_nolog (m, nbytes, multibyte);
9320 }
9321
9322
9323 /* The non-logging counterpart of message2. */
9324
9325 void
9326 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9327 {
9328 struct frame *sf = SELECTED_FRAME ();
9329 message_enable_multibyte = multibyte;
9330
9331 if (FRAME_INITIAL_P (sf))
9332 {
9333 if (noninteractive_need_newline)
9334 putc ('\n', stderr);
9335 noninteractive_need_newline = 0;
9336 if (m)
9337 fwrite (m, nbytes, 1, stderr);
9338 if (cursor_in_echo_area == 0)
9339 fprintf (stderr, "\n");
9340 fflush (stderr);
9341 }
9342 /* A null message buffer means that the frame hasn't really been
9343 initialized yet. Error messages get reported properly by
9344 cmd_error, so this must be just an informative message; toss it. */
9345 else if (INTERACTIVE
9346 && sf->glyphs_initialized_p
9347 && FRAME_MESSAGE_BUF (sf))
9348 {
9349 Lisp_Object mini_window;
9350 struct frame *f;
9351
9352 /* Get the frame containing the mini-buffer
9353 that the selected frame is using. */
9354 mini_window = FRAME_MINIBUF_WINDOW (sf);
9355 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9356
9357 FRAME_SAMPLE_VISIBILITY (f);
9358 if (FRAME_VISIBLE_P (sf)
9359 && ! FRAME_VISIBLE_P (f))
9360 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9361
9362 if (m)
9363 {
9364 set_message (m, Qnil, nbytes, multibyte);
9365 if (minibuffer_auto_raise)
9366 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9367 }
9368 else
9369 clear_message (1, 1);
9370
9371 do_pending_window_change (0);
9372 echo_area_display (1);
9373 do_pending_window_change (0);
9374 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9375 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9376 }
9377 }
9378
9379
9380 /* Display an echo area message M with a specified length of NBYTES
9381 bytes. The string may include null characters. If M is not a
9382 string, clear out any existing message, and let the mini-buffer
9383 text show through.
9384
9385 This function cancels echoing. */
9386
9387 void
9388 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9389 {
9390 struct gcpro gcpro1;
9391
9392 GCPRO1 (m);
9393 clear_message (1,1);
9394 cancel_echoing ();
9395
9396 /* First flush out any partial line written with print. */
9397 message_log_maybe_newline ();
9398 if (STRINGP (m))
9399 {
9400 char *buffer;
9401 USE_SAFE_ALLOCA;
9402
9403 SAFE_ALLOCA (buffer, char *, nbytes);
9404 memcpy (buffer, SDATA (m), nbytes);
9405 message_dolog (buffer, nbytes, 1, multibyte);
9406 SAFE_FREE ();
9407 }
9408 message3_nolog (m, nbytes, multibyte);
9409
9410 UNGCPRO;
9411 }
9412
9413
9414 /* The non-logging version of message3.
9415 This does not cancel echoing, because it is used for echoing.
9416 Perhaps we need to make a separate function for echoing
9417 and make this cancel echoing. */
9418
9419 void
9420 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9421 {
9422 struct frame *sf = SELECTED_FRAME ();
9423 message_enable_multibyte = multibyte;
9424
9425 if (FRAME_INITIAL_P (sf))
9426 {
9427 if (noninteractive_need_newline)
9428 putc ('\n', stderr);
9429 noninteractive_need_newline = 0;
9430 if (STRINGP (m))
9431 fwrite (SDATA (m), nbytes, 1, stderr);
9432 if (cursor_in_echo_area == 0)
9433 fprintf (stderr, "\n");
9434 fflush (stderr);
9435 }
9436 /* A null message buffer means that the frame hasn't really been
9437 initialized yet. Error messages get reported properly by
9438 cmd_error, so this must be just an informative message; toss it. */
9439 else if (INTERACTIVE
9440 && sf->glyphs_initialized_p
9441 && FRAME_MESSAGE_BUF (sf))
9442 {
9443 Lisp_Object mini_window;
9444 Lisp_Object frame;
9445 struct frame *f;
9446
9447 /* Get the frame containing the mini-buffer
9448 that the selected frame is using. */
9449 mini_window = FRAME_MINIBUF_WINDOW (sf);
9450 frame = XWINDOW (mini_window)->frame;
9451 f = XFRAME (frame);
9452
9453 FRAME_SAMPLE_VISIBILITY (f);
9454 if (FRAME_VISIBLE_P (sf)
9455 && !FRAME_VISIBLE_P (f))
9456 Fmake_frame_visible (frame);
9457
9458 if (STRINGP (m) && SCHARS (m) > 0)
9459 {
9460 set_message (NULL, m, nbytes, multibyte);
9461 if (minibuffer_auto_raise)
9462 Fraise_frame (frame);
9463 /* Assume we are not echoing.
9464 (If we are, echo_now will override this.) */
9465 echo_message_buffer = Qnil;
9466 }
9467 else
9468 clear_message (1, 1);
9469
9470 do_pending_window_change (0);
9471 echo_area_display (1);
9472 do_pending_window_change (0);
9473 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9474 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9475 }
9476 }
9477
9478
9479 /* Display a null-terminated echo area message M. If M is 0, clear
9480 out any existing message, and let the mini-buffer text show through.
9481
9482 The buffer M must continue to exist until after the echo area gets
9483 cleared or some other message gets displayed there. Do not pass
9484 text that is stored in a Lisp string. Do not pass text in a buffer
9485 that was alloca'd. */
9486
9487 void
9488 message1 (const char *m)
9489 {
9490 message2 (m, (m ? strlen (m) : 0), 0);
9491 }
9492
9493
9494 /* The non-logging counterpart of message1. */
9495
9496 void
9497 message1_nolog (const char *m)
9498 {
9499 message2_nolog (m, (m ? strlen (m) : 0), 0);
9500 }
9501
9502 /* Display a message M which contains a single %s
9503 which gets replaced with STRING. */
9504
9505 void
9506 message_with_string (const char *m, Lisp_Object string, int log)
9507 {
9508 CHECK_STRING (string);
9509
9510 if (noninteractive)
9511 {
9512 if (m)
9513 {
9514 if (noninteractive_need_newline)
9515 putc ('\n', stderr);
9516 noninteractive_need_newline = 0;
9517 fprintf (stderr, m, SDATA (string));
9518 if (!cursor_in_echo_area)
9519 fprintf (stderr, "\n");
9520 fflush (stderr);
9521 }
9522 }
9523 else if (INTERACTIVE)
9524 {
9525 /* The frame whose minibuffer we're going to display the message on.
9526 It may be larger than the selected frame, so we need
9527 to use its buffer, not the selected frame's buffer. */
9528 Lisp_Object mini_window;
9529 struct frame *f, *sf = SELECTED_FRAME ();
9530
9531 /* Get the frame containing the minibuffer
9532 that the selected frame is using. */
9533 mini_window = FRAME_MINIBUF_WINDOW (sf);
9534 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9535
9536 /* A null message buffer means that the frame hasn't really been
9537 initialized yet. Error messages get reported properly by
9538 cmd_error, so this must be just an informative message; toss it. */
9539 if (FRAME_MESSAGE_BUF (f))
9540 {
9541 Lisp_Object args[2], msg;
9542 struct gcpro gcpro1, gcpro2;
9543
9544 args[0] = build_string (m);
9545 args[1] = msg = string;
9546 GCPRO2 (args[0], msg);
9547 gcpro1.nvars = 2;
9548
9549 msg = Fformat (2, args);
9550
9551 if (log)
9552 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9553 else
9554 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9555
9556 UNGCPRO;
9557
9558 /* Print should start at the beginning of the message
9559 buffer next time. */
9560 message_buf_print = 0;
9561 }
9562 }
9563 }
9564
9565
9566 /* Dump an informative message to the minibuf. If M is 0, clear out
9567 any existing message, and let the mini-buffer text show through. */
9568
9569 static void
9570 vmessage (const char *m, va_list ap)
9571 {
9572 if (noninteractive)
9573 {
9574 if (m)
9575 {
9576 if (noninteractive_need_newline)
9577 putc ('\n', stderr);
9578 noninteractive_need_newline = 0;
9579 vfprintf (stderr, m, ap);
9580 if (cursor_in_echo_area == 0)
9581 fprintf (stderr, "\n");
9582 fflush (stderr);
9583 }
9584 }
9585 else if (INTERACTIVE)
9586 {
9587 /* The frame whose mini-buffer we're going to display the message
9588 on. It may be larger than the selected frame, so we need to
9589 use its buffer, not the selected frame's buffer. */
9590 Lisp_Object mini_window;
9591 struct frame *f, *sf = SELECTED_FRAME ();
9592
9593 /* Get the frame containing the mini-buffer
9594 that the selected frame is using. */
9595 mini_window = FRAME_MINIBUF_WINDOW (sf);
9596 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9597
9598 /* A null message buffer means that the frame hasn't really been
9599 initialized yet. Error messages get reported properly by
9600 cmd_error, so this must be just an informative message; toss
9601 it. */
9602 if (FRAME_MESSAGE_BUF (f))
9603 {
9604 if (m)
9605 {
9606 ptrdiff_t len;
9607
9608 len = doprnt (FRAME_MESSAGE_BUF (f),
9609 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9610
9611 message2 (FRAME_MESSAGE_BUF (f), len, 0);
9612 }
9613 else
9614 message1 (0);
9615
9616 /* Print should start at the beginning of the message
9617 buffer next time. */
9618 message_buf_print = 0;
9619 }
9620 }
9621 }
9622
9623 void
9624 message (const char *m, ...)
9625 {
9626 va_list ap;
9627 va_start (ap, m);
9628 vmessage (m, ap);
9629 va_end (ap);
9630 }
9631
9632
9633 #if 0
9634 /* The non-logging version of message. */
9635
9636 void
9637 message_nolog (const char *m, ...)
9638 {
9639 Lisp_Object old_log_max;
9640 va_list ap;
9641 va_start (ap, m);
9642 old_log_max = Vmessage_log_max;
9643 Vmessage_log_max = Qnil;
9644 vmessage (m, ap);
9645 Vmessage_log_max = old_log_max;
9646 va_end (ap);
9647 }
9648 #endif
9649
9650
9651 /* Display the current message in the current mini-buffer. This is
9652 only called from error handlers in process.c, and is not time
9653 critical. */
9654
9655 void
9656 update_echo_area (void)
9657 {
9658 if (!NILP (echo_area_buffer[0]))
9659 {
9660 Lisp_Object string;
9661 string = Fcurrent_message ();
9662 message3 (string, SBYTES (string),
9663 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9664 }
9665 }
9666
9667
9668 /* Make sure echo area buffers in `echo_buffers' are live.
9669 If they aren't, make new ones. */
9670
9671 static void
9672 ensure_echo_area_buffers (void)
9673 {
9674 int i;
9675
9676 for (i = 0; i < 2; ++i)
9677 if (!BUFFERP (echo_buffer[i])
9678 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9679 {
9680 char name[30];
9681 Lisp_Object old_buffer;
9682 int j;
9683
9684 old_buffer = echo_buffer[i];
9685 sprintf (name, " *Echo Area %d*", i);
9686 echo_buffer[i] = Fget_buffer_create (build_string (name));
9687 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9688 /* to force word wrap in echo area -
9689 it was decided to postpone this*/
9690 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9691
9692 for (j = 0; j < 2; ++j)
9693 if (EQ (old_buffer, echo_area_buffer[j]))
9694 echo_area_buffer[j] = echo_buffer[i];
9695 }
9696 }
9697
9698
9699 /* Call FN with args A1..A4 with either the current or last displayed
9700 echo_area_buffer as current buffer.
9701
9702 WHICH zero means use the current message buffer
9703 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9704 from echo_buffer[] and clear it.
9705
9706 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9707 suitable buffer from echo_buffer[] and clear it.
9708
9709 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9710 that the current message becomes the last displayed one, make
9711 choose a suitable buffer for echo_area_buffer[0], and clear it.
9712
9713 Value is what FN returns. */
9714
9715 static int
9716 with_echo_area_buffer (struct window *w, int which,
9717 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9718 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9719 {
9720 Lisp_Object buffer;
9721 int this_one, the_other, clear_buffer_p, rc;
9722 ptrdiff_t count = SPECPDL_INDEX ();
9723
9724 /* If buffers aren't live, make new ones. */
9725 ensure_echo_area_buffers ();
9726
9727 clear_buffer_p = 0;
9728
9729 if (which == 0)
9730 this_one = 0, the_other = 1;
9731 else if (which > 0)
9732 this_one = 1, the_other = 0;
9733 else
9734 {
9735 this_one = 0, the_other = 1;
9736 clear_buffer_p = 1;
9737
9738 /* We need a fresh one in case the current echo buffer equals
9739 the one containing the last displayed echo area message. */
9740 if (!NILP (echo_area_buffer[this_one])
9741 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9742 echo_area_buffer[this_one] = Qnil;
9743 }
9744
9745 /* Choose a suitable buffer from echo_buffer[] is we don't
9746 have one. */
9747 if (NILP (echo_area_buffer[this_one]))
9748 {
9749 echo_area_buffer[this_one]
9750 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9751 ? echo_buffer[the_other]
9752 : echo_buffer[this_one]);
9753 clear_buffer_p = 1;
9754 }
9755
9756 buffer = echo_area_buffer[this_one];
9757
9758 /* Don't get confused by reusing the buffer used for echoing
9759 for a different purpose. */
9760 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9761 cancel_echoing ();
9762
9763 record_unwind_protect (unwind_with_echo_area_buffer,
9764 with_echo_area_buffer_unwind_data (w));
9765
9766 /* Make the echo area buffer current. Note that for display
9767 purposes, it is not necessary that the displayed window's buffer
9768 == current_buffer, except for text property lookup. So, let's
9769 only set that buffer temporarily here without doing a full
9770 Fset_window_buffer. We must also change w->pointm, though,
9771 because otherwise an assertions in unshow_buffer fails, and Emacs
9772 aborts. */
9773 set_buffer_internal_1 (XBUFFER (buffer));
9774 if (w)
9775 {
9776 w->buffer = buffer;
9777 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9778 }
9779
9780 BVAR (current_buffer, undo_list) = Qt;
9781 BVAR (current_buffer, read_only) = Qnil;
9782 specbind (Qinhibit_read_only, Qt);
9783 specbind (Qinhibit_modification_hooks, Qt);
9784
9785 if (clear_buffer_p && Z > BEG)
9786 del_range (BEG, Z);
9787
9788 xassert (BEGV >= BEG);
9789 xassert (ZV <= Z && ZV >= BEGV);
9790
9791 rc = fn (a1, a2, a3, a4);
9792
9793 xassert (BEGV >= BEG);
9794 xassert (ZV <= Z && ZV >= BEGV);
9795
9796 unbind_to (count, Qnil);
9797 return rc;
9798 }
9799
9800
9801 /* Save state that should be preserved around the call to the function
9802 FN called in with_echo_area_buffer. */
9803
9804 static Lisp_Object
9805 with_echo_area_buffer_unwind_data (struct window *w)
9806 {
9807 int i = 0;
9808 Lisp_Object vector, tmp;
9809
9810 /* Reduce consing by keeping one vector in
9811 Vwith_echo_area_save_vector. */
9812 vector = Vwith_echo_area_save_vector;
9813 Vwith_echo_area_save_vector = Qnil;
9814
9815 if (NILP (vector))
9816 vector = Fmake_vector (make_number (7), Qnil);
9817
9818 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9819 ASET (vector, i, Vdeactivate_mark); ++i;
9820 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9821
9822 if (w)
9823 {
9824 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9825 ASET (vector, i, w->buffer); ++i;
9826 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9827 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9828 }
9829 else
9830 {
9831 int end = i + 4;
9832 for (; i < end; ++i)
9833 ASET (vector, i, Qnil);
9834 }
9835
9836 xassert (i == ASIZE (vector));
9837 return vector;
9838 }
9839
9840
9841 /* Restore global state from VECTOR which was created by
9842 with_echo_area_buffer_unwind_data. */
9843
9844 static Lisp_Object
9845 unwind_with_echo_area_buffer (Lisp_Object vector)
9846 {
9847 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
9848 Vdeactivate_mark = AREF (vector, 1);
9849 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
9850
9851 if (WINDOWP (AREF (vector, 3)))
9852 {
9853 struct window *w;
9854 Lisp_Object buffer, charpos, bytepos;
9855
9856 w = XWINDOW (AREF (vector, 3));
9857 buffer = AREF (vector, 4);
9858 charpos = AREF (vector, 5);
9859 bytepos = AREF (vector, 6);
9860
9861 w->buffer = buffer;
9862 set_marker_both (w->pointm, buffer,
9863 XFASTINT (charpos), XFASTINT (bytepos));
9864 }
9865
9866 Vwith_echo_area_save_vector = vector;
9867 return Qnil;
9868 }
9869
9870
9871 /* Set up the echo area for use by print functions. MULTIBYTE_P
9872 non-zero means we will print multibyte. */
9873
9874 void
9875 setup_echo_area_for_printing (int multibyte_p)
9876 {
9877 /* If we can't find an echo area any more, exit. */
9878 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
9879 Fkill_emacs (Qnil);
9880
9881 ensure_echo_area_buffers ();
9882
9883 if (!message_buf_print)
9884 {
9885 /* A message has been output since the last time we printed.
9886 Choose a fresh echo area buffer. */
9887 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9888 echo_area_buffer[0] = echo_buffer[1];
9889 else
9890 echo_area_buffer[0] = echo_buffer[0];
9891
9892 /* Switch to that buffer and clear it. */
9893 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9894 BVAR (current_buffer, truncate_lines) = Qnil;
9895
9896 if (Z > BEG)
9897 {
9898 ptrdiff_t count = SPECPDL_INDEX ();
9899 specbind (Qinhibit_read_only, Qt);
9900 /* Note that undo recording is always disabled. */
9901 del_range (BEG, Z);
9902 unbind_to (count, Qnil);
9903 }
9904 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
9905
9906 /* Set up the buffer for the multibyteness we need. */
9907 if (multibyte_p
9908 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
9909 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
9910
9911 /* Raise the frame containing the echo area. */
9912 if (minibuffer_auto_raise)
9913 {
9914 struct frame *sf = SELECTED_FRAME ();
9915 Lisp_Object mini_window;
9916 mini_window = FRAME_MINIBUF_WINDOW (sf);
9917 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9918 }
9919
9920 message_log_maybe_newline ();
9921 message_buf_print = 1;
9922 }
9923 else
9924 {
9925 if (NILP (echo_area_buffer[0]))
9926 {
9927 if (EQ (echo_area_buffer[1], echo_buffer[0]))
9928 echo_area_buffer[0] = echo_buffer[1];
9929 else
9930 echo_area_buffer[0] = echo_buffer[0];
9931 }
9932
9933 if (current_buffer != XBUFFER (echo_area_buffer[0]))
9934 {
9935 /* Someone switched buffers between print requests. */
9936 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
9937 BVAR (current_buffer, truncate_lines) = Qnil;
9938 }
9939 }
9940 }
9941
9942
9943 /* Display an echo area message in window W. Value is non-zero if W's
9944 height is changed. If display_last_displayed_message_p is
9945 non-zero, display the message that was last displayed, otherwise
9946 display the current message. */
9947
9948 static int
9949 display_echo_area (struct window *w)
9950 {
9951 int i, no_message_p, window_height_changed_p;
9952
9953 /* Temporarily disable garbage collections while displaying the echo
9954 area. This is done because a GC can print a message itself.
9955 That message would modify the echo area buffer's contents while a
9956 redisplay of the buffer is going on, and seriously confuse
9957 redisplay. */
9958 ptrdiff_t count = inhibit_garbage_collection ();
9959
9960 /* If there is no message, we must call display_echo_area_1
9961 nevertheless because it resizes the window. But we will have to
9962 reset the echo_area_buffer in question to nil at the end because
9963 with_echo_area_buffer will sets it to an empty buffer. */
9964 i = display_last_displayed_message_p ? 1 : 0;
9965 no_message_p = NILP (echo_area_buffer[i]);
9966
9967 window_height_changed_p
9968 = with_echo_area_buffer (w, display_last_displayed_message_p,
9969 display_echo_area_1,
9970 (intptr_t) w, Qnil, 0, 0);
9971
9972 if (no_message_p)
9973 echo_area_buffer[i] = Qnil;
9974
9975 unbind_to (count, Qnil);
9976 return window_height_changed_p;
9977 }
9978
9979
9980 /* Helper for display_echo_area. Display the current buffer which
9981 contains the current echo area message in window W, a mini-window,
9982 a pointer to which is passed in A1. A2..A4 are currently not used.
9983 Change the height of W so that all of the message is displayed.
9984 Value is non-zero if height of W was changed. */
9985
9986 static int
9987 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9988 {
9989 intptr_t i1 = a1;
9990 struct window *w = (struct window *) i1;
9991 Lisp_Object window;
9992 struct text_pos start;
9993 int window_height_changed_p = 0;
9994
9995 /* Do this before displaying, so that we have a large enough glyph
9996 matrix for the display. If we can't get enough space for the
9997 whole text, display the last N lines. That works by setting w->start. */
9998 window_height_changed_p = resize_mini_window (w, 0);
9999
10000 /* Use the starting position chosen by resize_mini_window. */
10001 SET_TEXT_POS_FROM_MARKER (start, w->start);
10002
10003 /* Display. */
10004 clear_glyph_matrix (w->desired_matrix);
10005 XSETWINDOW (window, w);
10006 try_window (window, start, 0);
10007
10008 return window_height_changed_p;
10009 }
10010
10011
10012 /* Resize the echo area window to exactly the size needed for the
10013 currently displayed message, if there is one. If a mini-buffer
10014 is active, don't shrink it. */
10015
10016 void
10017 resize_echo_area_exactly (void)
10018 {
10019 if (BUFFERP (echo_area_buffer[0])
10020 && WINDOWP (echo_area_window))
10021 {
10022 struct window *w = XWINDOW (echo_area_window);
10023 int resized_p;
10024 Lisp_Object resize_exactly;
10025
10026 if (minibuf_level == 0)
10027 resize_exactly = Qt;
10028 else
10029 resize_exactly = Qnil;
10030
10031 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10032 (intptr_t) w, resize_exactly,
10033 0, 0);
10034 if (resized_p)
10035 {
10036 ++windows_or_buffers_changed;
10037 ++update_mode_lines;
10038 redisplay_internal ();
10039 }
10040 }
10041 }
10042
10043
10044 /* Callback function for with_echo_area_buffer, when used from
10045 resize_echo_area_exactly. A1 contains a pointer to the window to
10046 resize, EXACTLY non-nil means resize the mini-window exactly to the
10047 size of the text displayed. A3 and A4 are not used. Value is what
10048 resize_mini_window returns. */
10049
10050 static int
10051 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10052 {
10053 intptr_t i1 = a1;
10054 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10055 }
10056
10057
10058 /* Resize mini-window W to fit the size of its contents. EXACT_P
10059 means size the window exactly to the size needed. Otherwise, it's
10060 only enlarged until W's buffer is empty.
10061
10062 Set W->start to the right place to begin display. If the whole
10063 contents fit, start at the beginning. Otherwise, start so as
10064 to make the end of the contents appear. This is particularly
10065 important for y-or-n-p, but seems desirable generally.
10066
10067 Value is non-zero if the window height has been changed. */
10068
10069 int
10070 resize_mini_window (struct window *w, int exact_p)
10071 {
10072 struct frame *f = XFRAME (w->frame);
10073 int window_height_changed_p = 0;
10074
10075 xassert (MINI_WINDOW_P (w));
10076
10077 /* By default, start display at the beginning. */
10078 set_marker_both (w->start, w->buffer,
10079 BUF_BEGV (XBUFFER (w->buffer)),
10080 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10081
10082 /* Don't resize windows while redisplaying a window; it would
10083 confuse redisplay functions when the size of the window they are
10084 displaying changes from under them. Such a resizing can happen,
10085 for instance, when which-func prints a long message while
10086 we are running fontification-functions. We're running these
10087 functions with safe_call which binds inhibit-redisplay to t. */
10088 if (!NILP (Vinhibit_redisplay))
10089 return 0;
10090
10091 /* Nil means don't try to resize. */
10092 if (NILP (Vresize_mini_windows)
10093 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10094 return 0;
10095
10096 if (!FRAME_MINIBUF_ONLY_P (f))
10097 {
10098 struct it it;
10099 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10100 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10101 int height;
10102 EMACS_INT max_height;
10103 int unit = FRAME_LINE_HEIGHT (f);
10104 struct text_pos start;
10105 struct buffer *old_current_buffer = NULL;
10106
10107 if (current_buffer != XBUFFER (w->buffer))
10108 {
10109 old_current_buffer = current_buffer;
10110 set_buffer_internal (XBUFFER (w->buffer));
10111 }
10112
10113 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10114
10115 /* Compute the max. number of lines specified by the user. */
10116 if (FLOATP (Vmax_mini_window_height))
10117 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10118 else if (INTEGERP (Vmax_mini_window_height))
10119 max_height = XINT (Vmax_mini_window_height);
10120 else
10121 max_height = total_height / 4;
10122
10123 /* Correct that max. height if it's bogus. */
10124 max_height = max (1, max_height);
10125 max_height = min (total_height, max_height);
10126
10127 /* Find out the height of the text in the window. */
10128 if (it.line_wrap == TRUNCATE)
10129 height = 1;
10130 else
10131 {
10132 last_height = 0;
10133 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10134 if (it.max_ascent == 0 && it.max_descent == 0)
10135 height = it.current_y + last_height;
10136 else
10137 height = it.current_y + it.max_ascent + it.max_descent;
10138 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10139 height = (height + unit - 1) / unit;
10140 }
10141
10142 /* Compute a suitable window start. */
10143 if (height > max_height)
10144 {
10145 height = max_height;
10146 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10147 move_it_vertically_backward (&it, (height - 1) * unit);
10148 start = it.current.pos;
10149 }
10150 else
10151 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10152 SET_MARKER_FROM_TEXT_POS (w->start, start);
10153
10154 if (EQ (Vresize_mini_windows, Qgrow_only))
10155 {
10156 /* Let it grow only, until we display an empty message, in which
10157 case the window shrinks again. */
10158 if (height > WINDOW_TOTAL_LINES (w))
10159 {
10160 int old_height = WINDOW_TOTAL_LINES (w);
10161 freeze_window_starts (f, 1);
10162 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10163 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10164 }
10165 else if (height < WINDOW_TOTAL_LINES (w)
10166 && (exact_p || BEGV == ZV))
10167 {
10168 int old_height = WINDOW_TOTAL_LINES (w);
10169 freeze_window_starts (f, 0);
10170 shrink_mini_window (w);
10171 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10172 }
10173 }
10174 else
10175 {
10176 /* Always resize to exact size needed. */
10177 if (height > WINDOW_TOTAL_LINES (w))
10178 {
10179 int old_height = WINDOW_TOTAL_LINES (w);
10180 freeze_window_starts (f, 1);
10181 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10182 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10183 }
10184 else if (height < WINDOW_TOTAL_LINES (w))
10185 {
10186 int old_height = WINDOW_TOTAL_LINES (w);
10187 freeze_window_starts (f, 0);
10188 shrink_mini_window (w);
10189
10190 if (height)
10191 {
10192 freeze_window_starts (f, 1);
10193 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10194 }
10195
10196 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10197 }
10198 }
10199
10200 if (old_current_buffer)
10201 set_buffer_internal (old_current_buffer);
10202 }
10203
10204 return window_height_changed_p;
10205 }
10206
10207
10208 /* Value is the current message, a string, or nil if there is no
10209 current message. */
10210
10211 Lisp_Object
10212 current_message (void)
10213 {
10214 Lisp_Object msg;
10215
10216 if (!BUFFERP (echo_area_buffer[0]))
10217 msg = Qnil;
10218 else
10219 {
10220 with_echo_area_buffer (0, 0, current_message_1,
10221 (intptr_t) &msg, Qnil, 0, 0);
10222 if (NILP (msg))
10223 echo_area_buffer[0] = Qnil;
10224 }
10225
10226 return msg;
10227 }
10228
10229
10230 static int
10231 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10232 {
10233 intptr_t i1 = a1;
10234 Lisp_Object *msg = (Lisp_Object *) i1;
10235
10236 if (Z > BEG)
10237 *msg = make_buffer_string (BEG, Z, 1);
10238 else
10239 *msg = Qnil;
10240 return 0;
10241 }
10242
10243
10244 /* Push the current message on Vmessage_stack for later restoration
10245 by restore_message. Value is non-zero if the current message isn't
10246 empty. This is a relatively infrequent operation, so it's not
10247 worth optimizing. */
10248
10249 int
10250 push_message (void)
10251 {
10252 Lisp_Object msg;
10253 msg = current_message ();
10254 Vmessage_stack = Fcons (msg, Vmessage_stack);
10255 return STRINGP (msg);
10256 }
10257
10258
10259 /* Restore message display from the top of Vmessage_stack. */
10260
10261 void
10262 restore_message (void)
10263 {
10264 Lisp_Object msg;
10265
10266 xassert (CONSP (Vmessage_stack));
10267 msg = XCAR (Vmessage_stack);
10268 if (STRINGP (msg))
10269 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10270 else
10271 message3_nolog (msg, 0, 0);
10272 }
10273
10274
10275 /* Handler for record_unwind_protect calling pop_message. */
10276
10277 Lisp_Object
10278 pop_message_unwind (Lisp_Object dummy)
10279 {
10280 pop_message ();
10281 return Qnil;
10282 }
10283
10284 /* Pop the top-most entry off Vmessage_stack. */
10285
10286 static void
10287 pop_message (void)
10288 {
10289 xassert (CONSP (Vmessage_stack));
10290 Vmessage_stack = XCDR (Vmessage_stack);
10291 }
10292
10293
10294 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10295 exits. If the stack is not empty, we have a missing pop_message
10296 somewhere. */
10297
10298 void
10299 check_message_stack (void)
10300 {
10301 if (!NILP (Vmessage_stack))
10302 abort ();
10303 }
10304
10305
10306 /* Truncate to NCHARS what will be displayed in the echo area the next
10307 time we display it---but don't redisplay it now. */
10308
10309 void
10310 truncate_echo_area (ptrdiff_t nchars)
10311 {
10312 if (nchars == 0)
10313 echo_area_buffer[0] = Qnil;
10314 /* A null message buffer means that the frame hasn't really been
10315 initialized yet. Error messages get reported properly by
10316 cmd_error, so this must be just an informative message; toss it. */
10317 else if (!noninteractive
10318 && INTERACTIVE
10319 && !NILP (echo_area_buffer[0]))
10320 {
10321 struct frame *sf = SELECTED_FRAME ();
10322 if (FRAME_MESSAGE_BUF (sf))
10323 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10324 }
10325 }
10326
10327
10328 /* Helper function for truncate_echo_area. Truncate the current
10329 message to at most NCHARS characters. */
10330
10331 static int
10332 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10333 {
10334 if (BEG + nchars < Z)
10335 del_range (BEG + nchars, Z);
10336 if (Z == BEG)
10337 echo_area_buffer[0] = Qnil;
10338 return 0;
10339 }
10340
10341
10342 /* Set the current message to a substring of S or STRING.
10343
10344 If STRING is a Lisp string, set the message to the first NBYTES
10345 bytes from STRING. NBYTES zero means use the whole string. If
10346 STRING is multibyte, the message will be displayed multibyte.
10347
10348 If S is not null, set the message to the first LEN bytes of S. LEN
10349 zero means use the whole string. MULTIBYTE_P non-zero means S is
10350 multibyte. Display the message multibyte in that case.
10351
10352 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10353 to t before calling set_message_1 (which calls insert).
10354 */
10355
10356 static void
10357 set_message (const char *s, Lisp_Object string,
10358 ptrdiff_t nbytes, int multibyte_p)
10359 {
10360 message_enable_multibyte
10361 = ((s && multibyte_p)
10362 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10363
10364 with_echo_area_buffer (0, -1, set_message_1,
10365 (intptr_t) s, string, nbytes, multibyte_p);
10366 message_buf_print = 0;
10367 help_echo_showing_p = 0;
10368 }
10369
10370
10371 /* Helper function for set_message. Arguments have the same meaning
10372 as there, with A1 corresponding to S and A2 corresponding to STRING
10373 This function is called with the echo area buffer being
10374 current. */
10375
10376 static int
10377 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10378 {
10379 intptr_t i1 = a1;
10380 const char *s = (const char *) i1;
10381 const unsigned char *msg = (const unsigned char *) s;
10382 Lisp_Object string = a2;
10383
10384 /* Change multibyteness of the echo buffer appropriately. */
10385 if (message_enable_multibyte
10386 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10387 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10388
10389 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10390 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10391 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10392
10393 /* Insert new message at BEG. */
10394 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10395
10396 if (STRINGP (string))
10397 {
10398 ptrdiff_t nchars;
10399
10400 if (nbytes == 0)
10401 nbytes = SBYTES (string);
10402 nchars = string_byte_to_char (string, nbytes);
10403
10404 /* This function takes care of single/multibyte conversion. We
10405 just have to ensure that the echo area buffer has the right
10406 setting of enable_multibyte_characters. */
10407 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10408 }
10409 else if (s)
10410 {
10411 if (nbytes == 0)
10412 nbytes = strlen (s);
10413
10414 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10415 {
10416 /* Convert from multi-byte to single-byte. */
10417 ptrdiff_t i;
10418 int c, n;
10419 char work[1];
10420
10421 /* Convert a multibyte string to single-byte. */
10422 for (i = 0; i < nbytes; i += n)
10423 {
10424 c = string_char_and_length (msg + i, &n);
10425 work[0] = (ASCII_CHAR_P (c)
10426 ? c
10427 : multibyte_char_to_unibyte (c));
10428 insert_1_both (work, 1, 1, 1, 0, 0);
10429 }
10430 }
10431 else if (!multibyte_p
10432 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10433 {
10434 /* Convert from single-byte to multi-byte. */
10435 ptrdiff_t i;
10436 int c, n;
10437 unsigned char str[MAX_MULTIBYTE_LENGTH];
10438
10439 /* Convert a single-byte string to multibyte. */
10440 for (i = 0; i < nbytes; i++)
10441 {
10442 c = msg[i];
10443 MAKE_CHAR_MULTIBYTE (c);
10444 n = CHAR_STRING (c, str);
10445 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10446 }
10447 }
10448 else
10449 insert_1 (s, nbytes, 1, 0, 0);
10450 }
10451
10452 return 0;
10453 }
10454
10455
10456 /* Clear messages. CURRENT_P non-zero means clear the current
10457 message. LAST_DISPLAYED_P non-zero means clear the message
10458 last displayed. */
10459
10460 void
10461 clear_message (int current_p, int last_displayed_p)
10462 {
10463 if (current_p)
10464 {
10465 echo_area_buffer[0] = Qnil;
10466 message_cleared_p = 1;
10467 }
10468
10469 if (last_displayed_p)
10470 echo_area_buffer[1] = Qnil;
10471
10472 message_buf_print = 0;
10473 }
10474
10475 /* Clear garbaged frames.
10476
10477 This function is used where the old redisplay called
10478 redraw_garbaged_frames which in turn called redraw_frame which in
10479 turn called clear_frame. The call to clear_frame was a source of
10480 flickering. I believe a clear_frame is not necessary. It should
10481 suffice in the new redisplay to invalidate all current matrices,
10482 and ensure a complete redisplay of all windows. */
10483
10484 static void
10485 clear_garbaged_frames (void)
10486 {
10487 if (frame_garbaged)
10488 {
10489 Lisp_Object tail, frame;
10490 int changed_count = 0;
10491
10492 FOR_EACH_FRAME (tail, frame)
10493 {
10494 struct frame *f = XFRAME (frame);
10495
10496 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10497 {
10498 if (f->resized_p)
10499 {
10500 Fredraw_frame (frame);
10501 f->force_flush_display_p = 1;
10502 }
10503 clear_current_matrices (f);
10504 changed_count++;
10505 f->garbaged = 0;
10506 f->resized_p = 0;
10507 }
10508 }
10509
10510 frame_garbaged = 0;
10511 if (changed_count)
10512 ++windows_or_buffers_changed;
10513 }
10514 }
10515
10516
10517 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10518 is non-zero update selected_frame. Value is non-zero if the
10519 mini-windows height has been changed. */
10520
10521 static int
10522 echo_area_display (int update_frame_p)
10523 {
10524 Lisp_Object mini_window;
10525 struct window *w;
10526 struct frame *f;
10527 int window_height_changed_p = 0;
10528 struct frame *sf = SELECTED_FRAME ();
10529
10530 mini_window = FRAME_MINIBUF_WINDOW (sf);
10531 w = XWINDOW (mini_window);
10532 f = XFRAME (WINDOW_FRAME (w));
10533
10534 /* Don't display if frame is invisible or not yet initialized. */
10535 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10536 return 0;
10537
10538 #ifdef HAVE_WINDOW_SYSTEM
10539 /* When Emacs starts, selected_frame may be the initial terminal
10540 frame. If we let this through, a message would be displayed on
10541 the terminal. */
10542 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10543 return 0;
10544 #endif /* HAVE_WINDOW_SYSTEM */
10545
10546 /* Redraw garbaged frames. */
10547 if (frame_garbaged)
10548 clear_garbaged_frames ();
10549
10550 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10551 {
10552 echo_area_window = mini_window;
10553 window_height_changed_p = display_echo_area (w);
10554 w->must_be_updated_p = 1;
10555
10556 /* Update the display, unless called from redisplay_internal.
10557 Also don't update the screen during redisplay itself. The
10558 update will happen at the end of redisplay, and an update
10559 here could cause confusion. */
10560 if (update_frame_p && !redisplaying_p)
10561 {
10562 int n = 0;
10563
10564 /* If the display update has been interrupted by pending
10565 input, update mode lines in the frame. Due to the
10566 pending input, it might have been that redisplay hasn't
10567 been called, so that mode lines above the echo area are
10568 garbaged. This looks odd, so we prevent it here. */
10569 if (!display_completed)
10570 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10571
10572 if (window_height_changed_p
10573 /* Don't do this if Emacs is shutting down. Redisplay
10574 needs to run hooks. */
10575 && !NILP (Vrun_hooks))
10576 {
10577 /* Must update other windows. Likewise as in other
10578 cases, don't let this update be interrupted by
10579 pending input. */
10580 ptrdiff_t count = SPECPDL_INDEX ();
10581 specbind (Qredisplay_dont_pause, Qt);
10582 windows_or_buffers_changed = 1;
10583 redisplay_internal ();
10584 unbind_to (count, Qnil);
10585 }
10586 else if (FRAME_WINDOW_P (f) && n == 0)
10587 {
10588 /* Window configuration is the same as before.
10589 Can do with a display update of the echo area,
10590 unless we displayed some mode lines. */
10591 update_single_window (w, 1);
10592 FRAME_RIF (f)->flush_display (f);
10593 }
10594 else
10595 update_frame (f, 1, 1);
10596
10597 /* If cursor is in the echo area, make sure that the next
10598 redisplay displays the minibuffer, so that the cursor will
10599 be replaced with what the minibuffer wants. */
10600 if (cursor_in_echo_area)
10601 ++windows_or_buffers_changed;
10602 }
10603 }
10604 else if (!EQ (mini_window, selected_window))
10605 windows_or_buffers_changed++;
10606
10607 /* Last displayed message is now the current message. */
10608 echo_area_buffer[1] = echo_area_buffer[0];
10609 /* Inform read_char that we're not echoing. */
10610 echo_message_buffer = Qnil;
10611
10612 /* Prevent redisplay optimization in redisplay_internal by resetting
10613 this_line_start_pos. This is done because the mini-buffer now
10614 displays the message instead of its buffer text. */
10615 if (EQ (mini_window, selected_window))
10616 CHARPOS (this_line_start_pos) = 0;
10617
10618 return window_height_changed_p;
10619 }
10620
10621
10622 \f
10623 /***********************************************************************
10624 Mode Lines and Frame Titles
10625 ***********************************************************************/
10626
10627 /* A buffer for constructing non-propertized mode-line strings and
10628 frame titles in it; allocated from the heap in init_xdisp and
10629 resized as needed in store_mode_line_noprop_char. */
10630
10631 static char *mode_line_noprop_buf;
10632
10633 /* The buffer's end, and a current output position in it. */
10634
10635 static char *mode_line_noprop_buf_end;
10636 static char *mode_line_noprop_ptr;
10637
10638 #define MODE_LINE_NOPROP_LEN(start) \
10639 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10640
10641 static enum {
10642 MODE_LINE_DISPLAY = 0,
10643 MODE_LINE_TITLE,
10644 MODE_LINE_NOPROP,
10645 MODE_LINE_STRING
10646 } mode_line_target;
10647
10648 /* Alist that caches the results of :propertize.
10649 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10650 static Lisp_Object mode_line_proptrans_alist;
10651
10652 /* List of strings making up the mode-line. */
10653 static Lisp_Object mode_line_string_list;
10654
10655 /* Base face property when building propertized mode line string. */
10656 static Lisp_Object mode_line_string_face;
10657 static Lisp_Object mode_line_string_face_prop;
10658
10659
10660 /* Unwind data for mode line strings */
10661
10662 static Lisp_Object Vmode_line_unwind_vector;
10663
10664 static Lisp_Object
10665 format_mode_line_unwind_data (struct buffer *obuf,
10666 Lisp_Object owin,
10667 int save_proptrans)
10668 {
10669 Lisp_Object vector, tmp;
10670
10671 /* Reduce consing by keeping one vector in
10672 Vwith_echo_area_save_vector. */
10673 vector = Vmode_line_unwind_vector;
10674 Vmode_line_unwind_vector = Qnil;
10675
10676 if (NILP (vector))
10677 vector = Fmake_vector (make_number (8), Qnil);
10678
10679 ASET (vector, 0, make_number (mode_line_target));
10680 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10681 ASET (vector, 2, mode_line_string_list);
10682 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10683 ASET (vector, 4, mode_line_string_face);
10684 ASET (vector, 5, mode_line_string_face_prop);
10685
10686 if (obuf)
10687 XSETBUFFER (tmp, obuf);
10688 else
10689 tmp = Qnil;
10690 ASET (vector, 6, tmp);
10691 ASET (vector, 7, owin);
10692
10693 return vector;
10694 }
10695
10696 static Lisp_Object
10697 unwind_format_mode_line (Lisp_Object vector)
10698 {
10699 mode_line_target = XINT (AREF (vector, 0));
10700 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10701 mode_line_string_list = AREF (vector, 2);
10702 if (! EQ (AREF (vector, 3), Qt))
10703 mode_line_proptrans_alist = AREF (vector, 3);
10704 mode_line_string_face = AREF (vector, 4);
10705 mode_line_string_face_prop = AREF (vector, 5);
10706
10707 if (!NILP (AREF (vector, 7)))
10708 /* Select window before buffer, since it may change the buffer. */
10709 Fselect_window (AREF (vector, 7), Qt);
10710
10711 if (!NILP (AREF (vector, 6)))
10712 {
10713 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10714 ASET (vector, 6, Qnil);
10715 }
10716
10717 Vmode_line_unwind_vector = vector;
10718 return Qnil;
10719 }
10720
10721
10722 /* Store a single character C for the frame title in mode_line_noprop_buf.
10723 Re-allocate mode_line_noprop_buf if necessary. */
10724
10725 static void
10726 store_mode_line_noprop_char (char c)
10727 {
10728 /* If output position has reached the end of the allocated buffer,
10729 increase the buffer's size. */
10730 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10731 {
10732 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10733 ptrdiff_t size = len;
10734 mode_line_noprop_buf =
10735 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10736 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10737 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10738 }
10739
10740 *mode_line_noprop_ptr++ = c;
10741 }
10742
10743
10744 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10745 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10746 characters that yield more columns than PRECISION; PRECISION <= 0
10747 means copy the whole string. Pad with spaces until FIELD_WIDTH
10748 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10749 pad. Called from display_mode_element when it is used to build a
10750 frame title. */
10751
10752 static int
10753 store_mode_line_noprop (const char *string, int field_width, int precision)
10754 {
10755 const unsigned char *str = (const unsigned char *) string;
10756 int n = 0;
10757 ptrdiff_t dummy, nbytes;
10758
10759 /* Copy at most PRECISION chars from STR. */
10760 nbytes = strlen (string);
10761 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10762 while (nbytes--)
10763 store_mode_line_noprop_char (*str++);
10764
10765 /* Fill up with spaces until FIELD_WIDTH reached. */
10766 while (field_width > 0
10767 && n < field_width)
10768 {
10769 store_mode_line_noprop_char (' ');
10770 ++n;
10771 }
10772
10773 return n;
10774 }
10775
10776 /***********************************************************************
10777 Frame Titles
10778 ***********************************************************************/
10779
10780 #ifdef HAVE_WINDOW_SYSTEM
10781
10782 /* Set the title of FRAME, if it has changed. The title format is
10783 Vicon_title_format if FRAME is iconified, otherwise it is
10784 frame_title_format. */
10785
10786 static void
10787 x_consider_frame_title (Lisp_Object frame)
10788 {
10789 struct frame *f = XFRAME (frame);
10790
10791 if (FRAME_WINDOW_P (f)
10792 || FRAME_MINIBUF_ONLY_P (f)
10793 || f->explicit_name)
10794 {
10795 /* Do we have more than one visible frame on this X display? */
10796 Lisp_Object tail;
10797 Lisp_Object fmt;
10798 ptrdiff_t title_start;
10799 char *title;
10800 ptrdiff_t len;
10801 struct it it;
10802 ptrdiff_t count = SPECPDL_INDEX ();
10803
10804 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10805 {
10806 Lisp_Object other_frame = XCAR (tail);
10807 struct frame *tf = XFRAME (other_frame);
10808
10809 if (tf != f
10810 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10811 && !FRAME_MINIBUF_ONLY_P (tf)
10812 && !EQ (other_frame, tip_frame)
10813 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10814 break;
10815 }
10816
10817 /* Set global variable indicating that multiple frames exist. */
10818 multiple_frames = CONSP (tail);
10819
10820 /* Switch to the buffer of selected window of the frame. Set up
10821 mode_line_target so that display_mode_element will output into
10822 mode_line_noprop_buf; then display the title. */
10823 record_unwind_protect (unwind_format_mode_line,
10824 format_mode_line_unwind_data
10825 (current_buffer, selected_window, 0));
10826
10827 Fselect_window (f->selected_window, Qt);
10828 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10829 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10830
10831 mode_line_target = MODE_LINE_TITLE;
10832 title_start = MODE_LINE_NOPROP_LEN (0);
10833 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10834 NULL, DEFAULT_FACE_ID);
10835 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10836 len = MODE_LINE_NOPROP_LEN (title_start);
10837 title = mode_line_noprop_buf + title_start;
10838 unbind_to (count, Qnil);
10839
10840 /* Set the title only if it's changed. This avoids consing in
10841 the common case where it hasn't. (If it turns out that we've
10842 already wasted too much time by walking through the list with
10843 display_mode_element, then we might need to optimize at a
10844 higher level than this.) */
10845 if (! STRINGP (f->name)
10846 || SBYTES (f->name) != len
10847 || memcmp (title, SDATA (f->name), len) != 0)
10848 x_implicitly_set_name (f, make_string (title, len), Qnil);
10849 }
10850 }
10851
10852 #endif /* not HAVE_WINDOW_SYSTEM */
10853
10854
10855
10856 \f
10857 /***********************************************************************
10858 Menu Bars
10859 ***********************************************************************/
10860
10861
10862 /* Prepare for redisplay by updating menu-bar item lists when
10863 appropriate. This can call eval. */
10864
10865 void
10866 prepare_menu_bars (void)
10867 {
10868 int all_windows;
10869 struct gcpro gcpro1, gcpro2;
10870 struct frame *f;
10871 Lisp_Object tooltip_frame;
10872
10873 #ifdef HAVE_WINDOW_SYSTEM
10874 tooltip_frame = tip_frame;
10875 #else
10876 tooltip_frame = Qnil;
10877 #endif
10878
10879 /* Update all frame titles based on their buffer names, etc. We do
10880 this before the menu bars so that the buffer-menu will show the
10881 up-to-date frame titles. */
10882 #ifdef HAVE_WINDOW_SYSTEM
10883 if (windows_or_buffers_changed || update_mode_lines)
10884 {
10885 Lisp_Object tail, frame;
10886
10887 FOR_EACH_FRAME (tail, frame)
10888 {
10889 f = XFRAME (frame);
10890 if (!EQ (frame, tooltip_frame)
10891 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
10892 x_consider_frame_title (frame);
10893 }
10894 }
10895 #endif /* HAVE_WINDOW_SYSTEM */
10896
10897 /* Update the menu bar item lists, if appropriate. This has to be
10898 done before any actual redisplay or generation of display lines. */
10899 all_windows = (update_mode_lines
10900 || buffer_shared > 1
10901 || windows_or_buffers_changed);
10902 if (all_windows)
10903 {
10904 Lisp_Object tail, frame;
10905 ptrdiff_t count = SPECPDL_INDEX ();
10906 /* 1 means that update_menu_bar has run its hooks
10907 so any further calls to update_menu_bar shouldn't do so again. */
10908 int menu_bar_hooks_run = 0;
10909
10910 record_unwind_save_match_data ();
10911
10912 FOR_EACH_FRAME (tail, frame)
10913 {
10914 f = XFRAME (frame);
10915
10916 /* Ignore tooltip frame. */
10917 if (EQ (frame, tooltip_frame))
10918 continue;
10919
10920 /* If a window on this frame changed size, report that to
10921 the user and clear the size-change flag. */
10922 if (FRAME_WINDOW_SIZES_CHANGED (f))
10923 {
10924 Lisp_Object functions;
10925
10926 /* Clear flag first in case we get an error below. */
10927 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
10928 functions = Vwindow_size_change_functions;
10929 GCPRO2 (tail, functions);
10930
10931 while (CONSP (functions))
10932 {
10933 if (!EQ (XCAR (functions), Qt))
10934 call1 (XCAR (functions), frame);
10935 functions = XCDR (functions);
10936 }
10937 UNGCPRO;
10938 }
10939
10940 GCPRO1 (tail);
10941 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
10942 #ifdef HAVE_WINDOW_SYSTEM
10943 update_tool_bar (f, 0);
10944 #endif
10945 #ifdef HAVE_NS
10946 if (windows_or_buffers_changed
10947 && FRAME_NS_P (f))
10948 ns_set_doc_edited (f, Fbuffer_modified_p
10949 (XWINDOW (f->selected_window)->buffer));
10950 #endif
10951 UNGCPRO;
10952 }
10953
10954 unbind_to (count, Qnil);
10955 }
10956 else
10957 {
10958 struct frame *sf = SELECTED_FRAME ();
10959 update_menu_bar (sf, 1, 0);
10960 #ifdef HAVE_WINDOW_SYSTEM
10961 update_tool_bar (sf, 1);
10962 #endif
10963 }
10964 }
10965
10966
10967 /* Update the menu bar item list for frame F. This has to be done
10968 before we start to fill in any display lines, because it can call
10969 eval.
10970
10971 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
10972
10973 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
10974 already ran the menu bar hooks for this redisplay, so there
10975 is no need to run them again. The return value is the
10976 updated value of this flag, to pass to the next call. */
10977
10978 static int
10979 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
10980 {
10981 Lisp_Object window;
10982 register struct window *w;
10983
10984 /* If called recursively during a menu update, do nothing. This can
10985 happen when, for instance, an activate-menubar-hook causes a
10986 redisplay. */
10987 if (inhibit_menubar_update)
10988 return hooks_run;
10989
10990 window = FRAME_SELECTED_WINDOW (f);
10991 w = XWINDOW (window);
10992
10993 if (FRAME_WINDOW_P (f)
10994 ?
10995 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
10996 || defined (HAVE_NS) || defined (USE_GTK)
10997 FRAME_EXTERNAL_MENU_BAR (f)
10998 #else
10999 FRAME_MENU_BAR_LINES (f) > 0
11000 #endif
11001 : FRAME_MENU_BAR_LINES (f) > 0)
11002 {
11003 /* If the user has switched buffers or windows, we need to
11004 recompute to reflect the new bindings. But we'll
11005 recompute when update_mode_lines is set too; that means
11006 that people can use force-mode-line-update to request
11007 that the menu bar be recomputed. The adverse effect on
11008 the rest of the redisplay algorithm is about the same as
11009 windows_or_buffers_changed anyway. */
11010 if (windows_or_buffers_changed
11011 /* This used to test w->update_mode_line, but we believe
11012 there is no need to recompute the menu in that case. */
11013 || update_mode_lines
11014 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11015 < BUF_MODIFF (XBUFFER (w->buffer)))
11016 != !NILP (w->last_had_star))
11017 || ((!NILP (Vtransient_mark_mode)
11018 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11019 != !NILP (w->region_showing)))
11020 {
11021 struct buffer *prev = current_buffer;
11022 ptrdiff_t count = SPECPDL_INDEX ();
11023
11024 specbind (Qinhibit_menubar_update, Qt);
11025
11026 set_buffer_internal_1 (XBUFFER (w->buffer));
11027 if (save_match_data)
11028 record_unwind_save_match_data ();
11029 if (NILP (Voverriding_local_map_menu_flag))
11030 {
11031 specbind (Qoverriding_terminal_local_map, Qnil);
11032 specbind (Qoverriding_local_map, Qnil);
11033 }
11034
11035 if (!hooks_run)
11036 {
11037 /* Run the Lucid hook. */
11038 safe_run_hooks (Qactivate_menubar_hook);
11039
11040 /* If it has changed current-menubar from previous value,
11041 really recompute the menu-bar from the value. */
11042 if (! NILP (Vlucid_menu_bar_dirty_flag))
11043 call0 (Qrecompute_lucid_menubar);
11044
11045 safe_run_hooks (Qmenu_bar_update_hook);
11046
11047 hooks_run = 1;
11048 }
11049
11050 XSETFRAME (Vmenu_updating_frame, f);
11051 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11052
11053 /* Redisplay the menu bar in case we changed it. */
11054 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11055 || defined (HAVE_NS) || defined (USE_GTK)
11056 if (FRAME_WINDOW_P (f))
11057 {
11058 #if defined (HAVE_NS)
11059 /* All frames on Mac OS share the same menubar. So only
11060 the selected frame should be allowed to set it. */
11061 if (f == SELECTED_FRAME ())
11062 #endif
11063 set_frame_menubar (f, 0, 0);
11064 }
11065 else
11066 /* On a terminal screen, the menu bar is an ordinary screen
11067 line, and this makes it get updated. */
11068 w->update_mode_line = Qt;
11069 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11070 /* In the non-toolkit version, the menu bar is an ordinary screen
11071 line, and this makes it get updated. */
11072 w->update_mode_line = Qt;
11073 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11074
11075 unbind_to (count, Qnil);
11076 set_buffer_internal_1 (prev);
11077 }
11078 }
11079
11080 return hooks_run;
11081 }
11082
11083
11084 \f
11085 /***********************************************************************
11086 Output Cursor
11087 ***********************************************************************/
11088
11089 #ifdef HAVE_WINDOW_SYSTEM
11090
11091 /* EXPORT:
11092 Nominal cursor position -- where to draw output.
11093 HPOS and VPOS are window relative glyph matrix coordinates.
11094 X and Y are window relative pixel coordinates. */
11095
11096 struct cursor_pos output_cursor;
11097
11098
11099 /* EXPORT:
11100 Set the global variable output_cursor to CURSOR. All cursor
11101 positions are relative to updated_window. */
11102
11103 void
11104 set_output_cursor (struct cursor_pos *cursor)
11105 {
11106 output_cursor.hpos = cursor->hpos;
11107 output_cursor.vpos = cursor->vpos;
11108 output_cursor.x = cursor->x;
11109 output_cursor.y = cursor->y;
11110 }
11111
11112
11113 /* EXPORT for RIF:
11114 Set a nominal cursor position.
11115
11116 HPOS and VPOS are column/row positions in a window glyph matrix. X
11117 and Y are window text area relative pixel positions.
11118
11119 If this is done during an update, updated_window will contain the
11120 window that is being updated and the position is the future output
11121 cursor position for that window. If updated_window is null, use
11122 selected_window and display the cursor at the given position. */
11123
11124 void
11125 x_cursor_to (int vpos, int hpos, int y, int x)
11126 {
11127 struct window *w;
11128
11129 /* If updated_window is not set, work on selected_window. */
11130 if (updated_window)
11131 w = updated_window;
11132 else
11133 w = XWINDOW (selected_window);
11134
11135 /* Set the output cursor. */
11136 output_cursor.hpos = hpos;
11137 output_cursor.vpos = vpos;
11138 output_cursor.x = x;
11139 output_cursor.y = y;
11140
11141 /* If not called as part of an update, really display the cursor.
11142 This will also set the cursor position of W. */
11143 if (updated_window == NULL)
11144 {
11145 BLOCK_INPUT;
11146 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11147 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11148 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11149 UNBLOCK_INPUT;
11150 }
11151 }
11152
11153 #endif /* HAVE_WINDOW_SYSTEM */
11154
11155 \f
11156 /***********************************************************************
11157 Tool-bars
11158 ***********************************************************************/
11159
11160 #ifdef HAVE_WINDOW_SYSTEM
11161
11162 /* Where the mouse was last time we reported a mouse event. */
11163
11164 FRAME_PTR last_mouse_frame;
11165
11166 /* Tool-bar item index of the item on which a mouse button was pressed
11167 or -1. */
11168
11169 int last_tool_bar_item;
11170
11171
11172 static Lisp_Object
11173 update_tool_bar_unwind (Lisp_Object frame)
11174 {
11175 selected_frame = frame;
11176 return Qnil;
11177 }
11178
11179 /* Update the tool-bar item list for frame F. This has to be done
11180 before we start to fill in any display lines. Called from
11181 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11182 and restore it here. */
11183
11184 static void
11185 update_tool_bar (struct frame *f, int save_match_data)
11186 {
11187 #if defined (USE_GTK) || defined (HAVE_NS)
11188 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11189 #else
11190 int do_update = WINDOWP (f->tool_bar_window)
11191 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11192 #endif
11193
11194 if (do_update)
11195 {
11196 Lisp_Object window;
11197 struct window *w;
11198
11199 window = FRAME_SELECTED_WINDOW (f);
11200 w = XWINDOW (window);
11201
11202 /* If the user has switched buffers or windows, we need to
11203 recompute to reflect the new bindings. But we'll
11204 recompute when update_mode_lines is set too; that means
11205 that people can use force-mode-line-update to request
11206 that the menu bar be recomputed. The adverse effect on
11207 the rest of the redisplay algorithm is about the same as
11208 windows_or_buffers_changed anyway. */
11209 if (windows_or_buffers_changed
11210 || !NILP (w->update_mode_line)
11211 || update_mode_lines
11212 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11213 < BUF_MODIFF (XBUFFER (w->buffer)))
11214 != !NILP (w->last_had_star))
11215 || ((!NILP (Vtransient_mark_mode)
11216 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11217 != !NILP (w->region_showing)))
11218 {
11219 struct buffer *prev = current_buffer;
11220 ptrdiff_t count = SPECPDL_INDEX ();
11221 Lisp_Object frame, new_tool_bar;
11222 int new_n_tool_bar;
11223 struct gcpro gcpro1;
11224
11225 /* Set current_buffer to the buffer of the selected
11226 window of the frame, so that we get the right local
11227 keymaps. */
11228 set_buffer_internal_1 (XBUFFER (w->buffer));
11229
11230 /* Save match data, if we must. */
11231 if (save_match_data)
11232 record_unwind_save_match_data ();
11233
11234 /* Make sure that we don't accidentally use bogus keymaps. */
11235 if (NILP (Voverriding_local_map_menu_flag))
11236 {
11237 specbind (Qoverriding_terminal_local_map, Qnil);
11238 specbind (Qoverriding_local_map, Qnil);
11239 }
11240
11241 GCPRO1 (new_tool_bar);
11242
11243 /* We must temporarily set the selected frame to this frame
11244 before calling tool_bar_items, because the calculation of
11245 the tool-bar keymap uses the selected frame (see
11246 `tool-bar-make-keymap' in tool-bar.el). */
11247 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11248 XSETFRAME (frame, f);
11249 selected_frame = frame;
11250
11251 /* Build desired tool-bar items from keymaps. */
11252 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11253 &new_n_tool_bar);
11254
11255 /* Redisplay the tool-bar if we changed it. */
11256 if (new_n_tool_bar != f->n_tool_bar_items
11257 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11258 {
11259 /* Redisplay that happens asynchronously due to an expose event
11260 may access f->tool_bar_items. Make sure we update both
11261 variables within BLOCK_INPUT so no such event interrupts. */
11262 BLOCK_INPUT;
11263 f->tool_bar_items = new_tool_bar;
11264 f->n_tool_bar_items = new_n_tool_bar;
11265 w->update_mode_line = Qt;
11266 UNBLOCK_INPUT;
11267 }
11268
11269 UNGCPRO;
11270
11271 unbind_to (count, Qnil);
11272 set_buffer_internal_1 (prev);
11273 }
11274 }
11275 }
11276
11277
11278 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11279 F's desired tool-bar contents. F->tool_bar_items must have
11280 been set up previously by calling prepare_menu_bars. */
11281
11282 static void
11283 build_desired_tool_bar_string (struct frame *f)
11284 {
11285 int i, size, size_needed;
11286 struct gcpro gcpro1, gcpro2, gcpro3;
11287 Lisp_Object image, plist, props;
11288
11289 image = plist = props = Qnil;
11290 GCPRO3 (image, plist, props);
11291
11292 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11293 Otherwise, make a new string. */
11294
11295 /* The size of the string we might be able to reuse. */
11296 size = (STRINGP (f->desired_tool_bar_string)
11297 ? SCHARS (f->desired_tool_bar_string)
11298 : 0);
11299
11300 /* We need one space in the string for each image. */
11301 size_needed = f->n_tool_bar_items;
11302
11303 /* Reuse f->desired_tool_bar_string, if possible. */
11304 if (size < size_needed || NILP (f->desired_tool_bar_string))
11305 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11306 make_number (' '));
11307 else
11308 {
11309 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11310 Fremove_text_properties (make_number (0), make_number (size),
11311 props, f->desired_tool_bar_string);
11312 }
11313
11314 /* Put a `display' property on the string for the images to display,
11315 put a `menu_item' property on tool-bar items with a value that
11316 is the index of the item in F's tool-bar item vector. */
11317 for (i = 0; i < f->n_tool_bar_items; ++i)
11318 {
11319 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11320
11321 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11322 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11323 int hmargin, vmargin, relief, idx, end;
11324
11325 /* If image is a vector, choose the image according to the
11326 button state. */
11327 image = PROP (TOOL_BAR_ITEM_IMAGES);
11328 if (VECTORP (image))
11329 {
11330 if (enabled_p)
11331 idx = (selected_p
11332 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11333 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11334 else
11335 idx = (selected_p
11336 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11337 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11338
11339 xassert (ASIZE (image) >= idx);
11340 image = AREF (image, idx);
11341 }
11342 else
11343 idx = -1;
11344
11345 /* Ignore invalid image specifications. */
11346 if (!valid_image_p (image))
11347 continue;
11348
11349 /* Display the tool-bar button pressed, or depressed. */
11350 plist = Fcopy_sequence (XCDR (image));
11351
11352 /* Compute margin and relief to draw. */
11353 relief = (tool_bar_button_relief >= 0
11354 ? tool_bar_button_relief
11355 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11356 hmargin = vmargin = relief;
11357
11358 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11359 INT_MAX - max (hmargin, vmargin)))
11360 {
11361 hmargin += XFASTINT (Vtool_bar_button_margin);
11362 vmargin += XFASTINT (Vtool_bar_button_margin);
11363 }
11364 else if (CONSP (Vtool_bar_button_margin))
11365 {
11366 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11367 INT_MAX - hmargin))
11368 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11369
11370 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11371 INT_MAX - vmargin))
11372 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11373 }
11374
11375 if (auto_raise_tool_bar_buttons_p)
11376 {
11377 /* Add a `:relief' property to the image spec if the item is
11378 selected. */
11379 if (selected_p)
11380 {
11381 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11382 hmargin -= relief;
11383 vmargin -= relief;
11384 }
11385 }
11386 else
11387 {
11388 /* If image is selected, display it pressed, i.e. with a
11389 negative relief. If it's not selected, display it with a
11390 raised relief. */
11391 plist = Fplist_put (plist, QCrelief,
11392 (selected_p
11393 ? make_number (-relief)
11394 : make_number (relief)));
11395 hmargin -= relief;
11396 vmargin -= relief;
11397 }
11398
11399 /* Put a margin around the image. */
11400 if (hmargin || vmargin)
11401 {
11402 if (hmargin == vmargin)
11403 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11404 else
11405 plist = Fplist_put (plist, QCmargin,
11406 Fcons (make_number (hmargin),
11407 make_number (vmargin)));
11408 }
11409
11410 /* If button is not enabled, and we don't have special images
11411 for the disabled state, make the image appear disabled by
11412 applying an appropriate algorithm to it. */
11413 if (!enabled_p && idx < 0)
11414 plist = Fplist_put (plist, QCconversion, Qdisabled);
11415
11416 /* Put a `display' text property on the string for the image to
11417 display. Put a `menu-item' property on the string that gives
11418 the start of this item's properties in the tool-bar items
11419 vector. */
11420 image = Fcons (Qimage, plist);
11421 props = list4 (Qdisplay, image,
11422 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11423
11424 /* Let the last image hide all remaining spaces in the tool bar
11425 string. The string can be longer than needed when we reuse a
11426 previous string. */
11427 if (i + 1 == f->n_tool_bar_items)
11428 end = SCHARS (f->desired_tool_bar_string);
11429 else
11430 end = i + 1;
11431 Fadd_text_properties (make_number (i), make_number (end),
11432 props, f->desired_tool_bar_string);
11433 #undef PROP
11434 }
11435
11436 UNGCPRO;
11437 }
11438
11439
11440 /* Display one line of the tool-bar of frame IT->f.
11441
11442 HEIGHT specifies the desired height of the tool-bar line.
11443 If the actual height of the glyph row is less than HEIGHT, the
11444 row's height is increased to HEIGHT, and the icons are centered
11445 vertically in the new height.
11446
11447 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11448 count a final empty row in case the tool-bar width exactly matches
11449 the window width.
11450 */
11451
11452 static void
11453 display_tool_bar_line (struct it *it, int height)
11454 {
11455 struct glyph_row *row = it->glyph_row;
11456 int max_x = it->last_visible_x;
11457 struct glyph *last;
11458
11459 prepare_desired_row (row);
11460 row->y = it->current_y;
11461
11462 /* Note that this isn't made use of if the face hasn't a box,
11463 so there's no need to check the face here. */
11464 it->start_of_box_run_p = 1;
11465
11466 while (it->current_x < max_x)
11467 {
11468 int x, n_glyphs_before, i, nglyphs;
11469 struct it it_before;
11470
11471 /* Get the next display element. */
11472 if (!get_next_display_element (it))
11473 {
11474 /* Don't count empty row if we are counting needed tool-bar lines. */
11475 if (height < 0 && !it->hpos)
11476 return;
11477 break;
11478 }
11479
11480 /* Produce glyphs. */
11481 n_glyphs_before = row->used[TEXT_AREA];
11482 it_before = *it;
11483
11484 PRODUCE_GLYPHS (it);
11485
11486 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11487 i = 0;
11488 x = it_before.current_x;
11489 while (i < nglyphs)
11490 {
11491 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11492
11493 if (x + glyph->pixel_width > max_x)
11494 {
11495 /* Glyph doesn't fit on line. Backtrack. */
11496 row->used[TEXT_AREA] = n_glyphs_before;
11497 *it = it_before;
11498 /* If this is the only glyph on this line, it will never fit on the
11499 tool-bar, so skip it. But ensure there is at least one glyph,
11500 so we don't accidentally disable the tool-bar. */
11501 if (n_glyphs_before == 0
11502 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11503 break;
11504 goto out;
11505 }
11506
11507 ++it->hpos;
11508 x += glyph->pixel_width;
11509 ++i;
11510 }
11511
11512 /* Stop at line end. */
11513 if (ITERATOR_AT_END_OF_LINE_P (it))
11514 break;
11515
11516 set_iterator_to_next (it, 1);
11517 }
11518
11519 out:;
11520
11521 row->displays_text_p = row->used[TEXT_AREA] != 0;
11522
11523 /* Use default face for the border below the tool bar.
11524
11525 FIXME: When auto-resize-tool-bars is grow-only, there is
11526 no additional border below the possibly empty tool-bar lines.
11527 So to make the extra empty lines look "normal", we have to
11528 use the tool-bar face for the border too. */
11529 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11530 it->face_id = DEFAULT_FACE_ID;
11531
11532 extend_face_to_end_of_line (it);
11533 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11534 last->right_box_line_p = 1;
11535 if (last == row->glyphs[TEXT_AREA])
11536 last->left_box_line_p = 1;
11537
11538 /* Make line the desired height and center it vertically. */
11539 if ((height -= it->max_ascent + it->max_descent) > 0)
11540 {
11541 /* Don't add more than one line height. */
11542 height %= FRAME_LINE_HEIGHT (it->f);
11543 it->max_ascent += height / 2;
11544 it->max_descent += (height + 1) / 2;
11545 }
11546
11547 compute_line_metrics (it);
11548
11549 /* If line is empty, make it occupy the rest of the tool-bar. */
11550 if (!row->displays_text_p)
11551 {
11552 row->height = row->phys_height = it->last_visible_y - row->y;
11553 row->visible_height = row->height;
11554 row->ascent = row->phys_ascent = 0;
11555 row->extra_line_spacing = 0;
11556 }
11557
11558 row->full_width_p = 1;
11559 row->continued_p = 0;
11560 row->truncated_on_left_p = 0;
11561 row->truncated_on_right_p = 0;
11562
11563 it->current_x = it->hpos = 0;
11564 it->current_y += row->height;
11565 ++it->vpos;
11566 ++it->glyph_row;
11567 }
11568
11569
11570 /* Max tool-bar height. */
11571
11572 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11573 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11574
11575 /* Value is the number of screen lines needed to make all tool-bar
11576 items of frame F visible. The number of actual rows needed is
11577 returned in *N_ROWS if non-NULL. */
11578
11579 static int
11580 tool_bar_lines_needed (struct frame *f, int *n_rows)
11581 {
11582 struct window *w = XWINDOW (f->tool_bar_window);
11583 struct it it;
11584 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11585 the desired matrix, so use (unused) mode-line row as temporary row to
11586 avoid destroying the first tool-bar row. */
11587 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11588
11589 /* Initialize an iterator for iteration over
11590 F->desired_tool_bar_string in the tool-bar window of frame F. */
11591 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11592 it.first_visible_x = 0;
11593 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11594 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11595 it.paragraph_embedding = L2R;
11596
11597 while (!ITERATOR_AT_END_P (&it))
11598 {
11599 clear_glyph_row (temp_row);
11600 it.glyph_row = temp_row;
11601 display_tool_bar_line (&it, -1);
11602 }
11603 clear_glyph_row (temp_row);
11604
11605 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11606 if (n_rows)
11607 *n_rows = it.vpos > 0 ? it.vpos : -1;
11608
11609 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11610 }
11611
11612
11613 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11614 0, 1, 0,
11615 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11616 (Lisp_Object frame)
11617 {
11618 struct frame *f;
11619 struct window *w;
11620 int nlines = 0;
11621
11622 if (NILP (frame))
11623 frame = selected_frame;
11624 else
11625 CHECK_FRAME (frame);
11626 f = XFRAME (frame);
11627
11628 if (WINDOWP (f->tool_bar_window)
11629 && (w = XWINDOW (f->tool_bar_window),
11630 WINDOW_TOTAL_LINES (w) > 0))
11631 {
11632 update_tool_bar (f, 1);
11633 if (f->n_tool_bar_items)
11634 {
11635 build_desired_tool_bar_string (f);
11636 nlines = tool_bar_lines_needed (f, NULL);
11637 }
11638 }
11639
11640 return make_number (nlines);
11641 }
11642
11643
11644 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11645 height should be changed. */
11646
11647 static int
11648 redisplay_tool_bar (struct frame *f)
11649 {
11650 struct window *w;
11651 struct it it;
11652 struct glyph_row *row;
11653
11654 #if defined (USE_GTK) || defined (HAVE_NS)
11655 if (FRAME_EXTERNAL_TOOL_BAR (f))
11656 update_frame_tool_bar (f);
11657 return 0;
11658 #endif
11659
11660 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11661 do anything. This means you must start with tool-bar-lines
11662 non-zero to get the auto-sizing effect. Or in other words, you
11663 can turn off tool-bars by specifying tool-bar-lines zero. */
11664 if (!WINDOWP (f->tool_bar_window)
11665 || (w = XWINDOW (f->tool_bar_window),
11666 WINDOW_TOTAL_LINES (w) == 0))
11667 return 0;
11668
11669 /* Set up an iterator for the tool-bar window. */
11670 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11671 it.first_visible_x = 0;
11672 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11673 row = it.glyph_row;
11674
11675 /* Build a string that represents the contents of the tool-bar. */
11676 build_desired_tool_bar_string (f);
11677 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11678 /* FIXME: This should be controlled by a user option. But it
11679 doesn't make sense to have an R2L tool bar if the menu bar cannot
11680 be drawn also R2L, and making the menu bar R2L is tricky due
11681 toolkit-specific code that implements it. If an R2L tool bar is
11682 ever supported, display_tool_bar_line should also be augmented to
11683 call unproduce_glyphs like display_line and display_string
11684 do. */
11685 it.paragraph_embedding = L2R;
11686
11687 if (f->n_tool_bar_rows == 0)
11688 {
11689 int nlines;
11690
11691 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11692 nlines != WINDOW_TOTAL_LINES (w)))
11693 {
11694 Lisp_Object frame;
11695 int old_height = WINDOW_TOTAL_LINES (w);
11696
11697 XSETFRAME (frame, f);
11698 Fmodify_frame_parameters (frame,
11699 Fcons (Fcons (Qtool_bar_lines,
11700 make_number (nlines)),
11701 Qnil));
11702 if (WINDOW_TOTAL_LINES (w) != old_height)
11703 {
11704 clear_glyph_matrix (w->desired_matrix);
11705 fonts_changed_p = 1;
11706 return 1;
11707 }
11708 }
11709 }
11710
11711 /* Display as many lines as needed to display all tool-bar items. */
11712
11713 if (f->n_tool_bar_rows > 0)
11714 {
11715 int border, rows, height, extra;
11716
11717 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11718 border = XINT (Vtool_bar_border);
11719 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11720 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11721 else if (EQ (Vtool_bar_border, Qborder_width))
11722 border = f->border_width;
11723 else
11724 border = 0;
11725 if (border < 0)
11726 border = 0;
11727
11728 rows = f->n_tool_bar_rows;
11729 height = max (1, (it.last_visible_y - border) / rows);
11730 extra = it.last_visible_y - border - height * rows;
11731
11732 while (it.current_y < it.last_visible_y)
11733 {
11734 int h = 0;
11735 if (extra > 0 && rows-- > 0)
11736 {
11737 h = (extra + rows - 1) / rows;
11738 extra -= h;
11739 }
11740 display_tool_bar_line (&it, height + h);
11741 }
11742 }
11743 else
11744 {
11745 while (it.current_y < it.last_visible_y)
11746 display_tool_bar_line (&it, 0);
11747 }
11748
11749 /* It doesn't make much sense to try scrolling in the tool-bar
11750 window, so don't do it. */
11751 w->desired_matrix->no_scrolling_p = 1;
11752 w->must_be_updated_p = 1;
11753
11754 if (!NILP (Vauto_resize_tool_bars))
11755 {
11756 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11757 int change_height_p = 0;
11758
11759 /* If we couldn't display everything, change the tool-bar's
11760 height if there is room for more. */
11761 if (IT_STRING_CHARPOS (it) < it.end_charpos
11762 && it.current_y < max_tool_bar_height)
11763 change_height_p = 1;
11764
11765 row = it.glyph_row - 1;
11766
11767 /* If there are blank lines at the end, except for a partially
11768 visible blank line at the end that is smaller than
11769 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11770 if (!row->displays_text_p
11771 && row->height >= FRAME_LINE_HEIGHT (f))
11772 change_height_p = 1;
11773
11774 /* If row displays tool-bar items, but is partially visible,
11775 change the tool-bar's height. */
11776 if (row->displays_text_p
11777 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11778 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11779 change_height_p = 1;
11780
11781 /* Resize windows as needed by changing the `tool-bar-lines'
11782 frame parameter. */
11783 if (change_height_p)
11784 {
11785 Lisp_Object frame;
11786 int old_height = WINDOW_TOTAL_LINES (w);
11787 int nrows;
11788 int nlines = tool_bar_lines_needed (f, &nrows);
11789
11790 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11791 && !f->minimize_tool_bar_window_p)
11792 ? (nlines > old_height)
11793 : (nlines != old_height));
11794 f->minimize_tool_bar_window_p = 0;
11795
11796 if (change_height_p)
11797 {
11798 XSETFRAME (frame, f);
11799 Fmodify_frame_parameters (frame,
11800 Fcons (Fcons (Qtool_bar_lines,
11801 make_number (nlines)),
11802 Qnil));
11803 if (WINDOW_TOTAL_LINES (w) != old_height)
11804 {
11805 clear_glyph_matrix (w->desired_matrix);
11806 f->n_tool_bar_rows = nrows;
11807 fonts_changed_p = 1;
11808 return 1;
11809 }
11810 }
11811 }
11812 }
11813
11814 f->minimize_tool_bar_window_p = 0;
11815 return 0;
11816 }
11817
11818
11819 /* Get information about the tool-bar item which is displayed in GLYPH
11820 on frame F. Return in *PROP_IDX the index where tool-bar item
11821 properties start in F->tool_bar_items. Value is zero if
11822 GLYPH doesn't display a tool-bar item. */
11823
11824 static int
11825 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11826 {
11827 Lisp_Object prop;
11828 int success_p;
11829 int charpos;
11830
11831 /* This function can be called asynchronously, which means we must
11832 exclude any possibility that Fget_text_property signals an
11833 error. */
11834 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11835 charpos = max (0, charpos);
11836
11837 /* Get the text property `menu-item' at pos. The value of that
11838 property is the start index of this item's properties in
11839 F->tool_bar_items. */
11840 prop = Fget_text_property (make_number (charpos),
11841 Qmenu_item, f->current_tool_bar_string);
11842 if (INTEGERP (prop))
11843 {
11844 *prop_idx = XINT (prop);
11845 success_p = 1;
11846 }
11847 else
11848 success_p = 0;
11849
11850 return success_p;
11851 }
11852
11853 \f
11854 /* Get information about the tool-bar item at position X/Y on frame F.
11855 Return in *GLYPH a pointer to the glyph of the tool-bar item in
11856 the current matrix of the tool-bar window of F, or NULL if not
11857 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
11858 item in F->tool_bar_items. Value is
11859
11860 -1 if X/Y is not on a tool-bar item
11861 0 if X/Y is on the same item that was highlighted before.
11862 1 otherwise. */
11863
11864 static int
11865 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
11866 int *hpos, int *vpos, int *prop_idx)
11867 {
11868 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11869 struct window *w = XWINDOW (f->tool_bar_window);
11870 int area;
11871
11872 /* Find the glyph under X/Y. */
11873 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
11874 if (*glyph == NULL)
11875 return -1;
11876
11877 /* Get the start of this tool-bar item's properties in
11878 f->tool_bar_items. */
11879 if (!tool_bar_item_info (f, *glyph, prop_idx))
11880 return -1;
11881
11882 /* Is mouse on the highlighted item? */
11883 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
11884 && *vpos >= hlinfo->mouse_face_beg_row
11885 && *vpos <= hlinfo->mouse_face_end_row
11886 && (*vpos > hlinfo->mouse_face_beg_row
11887 || *hpos >= hlinfo->mouse_face_beg_col)
11888 && (*vpos < hlinfo->mouse_face_end_row
11889 || *hpos < hlinfo->mouse_face_end_col
11890 || hlinfo->mouse_face_past_end))
11891 return 0;
11892
11893 return 1;
11894 }
11895
11896
11897 /* EXPORT:
11898 Handle mouse button event on the tool-bar of frame F, at
11899 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
11900 0 for button release. MODIFIERS is event modifiers for button
11901 release. */
11902
11903 void
11904 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
11905 int modifiers)
11906 {
11907 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11908 struct window *w = XWINDOW (f->tool_bar_window);
11909 int hpos, vpos, prop_idx;
11910 struct glyph *glyph;
11911 Lisp_Object enabled_p;
11912
11913 /* If not on the highlighted tool-bar item, return. */
11914 frame_to_window_pixel_xy (w, &x, &y);
11915 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
11916 return;
11917
11918 /* If item is disabled, do nothing. */
11919 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
11920 if (NILP (enabled_p))
11921 return;
11922
11923 if (down_p)
11924 {
11925 /* Show item in pressed state. */
11926 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
11927 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
11928 last_tool_bar_item = prop_idx;
11929 }
11930 else
11931 {
11932 Lisp_Object key, frame;
11933 struct input_event event;
11934 EVENT_INIT (event);
11935
11936 /* Show item in released state. */
11937 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
11938 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
11939
11940 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
11941
11942 XSETFRAME (frame, f);
11943 event.kind = TOOL_BAR_EVENT;
11944 event.frame_or_window = frame;
11945 event.arg = frame;
11946 kbd_buffer_store_event (&event);
11947
11948 event.kind = TOOL_BAR_EVENT;
11949 event.frame_or_window = frame;
11950 event.arg = key;
11951 event.modifiers = modifiers;
11952 kbd_buffer_store_event (&event);
11953 last_tool_bar_item = -1;
11954 }
11955 }
11956
11957
11958 /* Possibly highlight a tool-bar item on frame F when mouse moves to
11959 tool-bar window-relative coordinates X/Y. Called from
11960 note_mouse_highlight. */
11961
11962 static void
11963 note_tool_bar_highlight (struct frame *f, int x, int y)
11964 {
11965 Lisp_Object window = f->tool_bar_window;
11966 struct window *w = XWINDOW (window);
11967 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
11968 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
11969 int hpos, vpos;
11970 struct glyph *glyph;
11971 struct glyph_row *row;
11972 int i;
11973 Lisp_Object enabled_p;
11974 int prop_idx;
11975 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
11976 int mouse_down_p, rc;
11977
11978 /* Function note_mouse_highlight is called with negative X/Y
11979 values when mouse moves outside of the frame. */
11980 if (x <= 0 || y <= 0)
11981 {
11982 clear_mouse_face (hlinfo);
11983 return;
11984 }
11985
11986 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
11987 if (rc < 0)
11988 {
11989 /* Not on tool-bar item. */
11990 clear_mouse_face (hlinfo);
11991 return;
11992 }
11993 else if (rc == 0)
11994 /* On same tool-bar item as before. */
11995 goto set_help_echo;
11996
11997 clear_mouse_face (hlinfo);
11998
11999 /* Mouse is down, but on different tool-bar item? */
12000 mouse_down_p = (dpyinfo->grabbed
12001 && f == last_mouse_frame
12002 && FRAME_LIVE_P (f));
12003 if (mouse_down_p
12004 && last_tool_bar_item != prop_idx)
12005 return;
12006
12007 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12008 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12009
12010 /* If tool-bar item is not enabled, don't highlight it. */
12011 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12012 if (!NILP (enabled_p))
12013 {
12014 /* Compute the x-position of the glyph. In front and past the
12015 image is a space. We include this in the highlighted area. */
12016 row = MATRIX_ROW (w->current_matrix, vpos);
12017 for (i = x = 0; i < hpos; ++i)
12018 x += row->glyphs[TEXT_AREA][i].pixel_width;
12019
12020 /* Record this as the current active region. */
12021 hlinfo->mouse_face_beg_col = hpos;
12022 hlinfo->mouse_face_beg_row = vpos;
12023 hlinfo->mouse_face_beg_x = x;
12024 hlinfo->mouse_face_beg_y = row->y;
12025 hlinfo->mouse_face_past_end = 0;
12026
12027 hlinfo->mouse_face_end_col = hpos + 1;
12028 hlinfo->mouse_face_end_row = vpos;
12029 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12030 hlinfo->mouse_face_end_y = row->y;
12031 hlinfo->mouse_face_window = window;
12032 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12033
12034 /* Display it as active. */
12035 show_mouse_face (hlinfo, draw);
12036 hlinfo->mouse_face_image_state = draw;
12037 }
12038
12039 set_help_echo:
12040
12041 /* Set help_echo_string to a help string to display for this tool-bar item.
12042 XTread_socket does the rest. */
12043 help_echo_object = help_echo_window = Qnil;
12044 help_echo_pos = -1;
12045 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12046 if (NILP (help_echo_string))
12047 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12048 }
12049
12050 #endif /* HAVE_WINDOW_SYSTEM */
12051
12052
12053 \f
12054 /************************************************************************
12055 Horizontal scrolling
12056 ************************************************************************/
12057
12058 static int hscroll_window_tree (Lisp_Object);
12059 static int hscroll_windows (Lisp_Object);
12060
12061 /* For all leaf windows in the window tree rooted at WINDOW, set their
12062 hscroll value so that PT is (i) visible in the window, and (ii) so
12063 that it is not within a certain margin at the window's left and
12064 right border. Value is non-zero if any window's hscroll has been
12065 changed. */
12066
12067 static int
12068 hscroll_window_tree (Lisp_Object window)
12069 {
12070 int hscrolled_p = 0;
12071 int hscroll_relative_p = FLOATP (Vhscroll_step);
12072 int hscroll_step_abs = 0;
12073 double hscroll_step_rel = 0;
12074
12075 if (hscroll_relative_p)
12076 {
12077 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12078 if (hscroll_step_rel < 0)
12079 {
12080 hscroll_relative_p = 0;
12081 hscroll_step_abs = 0;
12082 }
12083 }
12084 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12085 {
12086 hscroll_step_abs = XINT (Vhscroll_step);
12087 if (hscroll_step_abs < 0)
12088 hscroll_step_abs = 0;
12089 }
12090 else
12091 hscroll_step_abs = 0;
12092
12093 while (WINDOWP (window))
12094 {
12095 struct window *w = XWINDOW (window);
12096
12097 if (WINDOWP (w->hchild))
12098 hscrolled_p |= hscroll_window_tree (w->hchild);
12099 else if (WINDOWP (w->vchild))
12100 hscrolled_p |= hscroll_window_tree (w->vchild);
12101 else if (w->cursor.vpos >= 0)
12102 {
12103 int h_margin;
12104 int text_area_width;
12105 struct glyph_row *current_cursor_row
12106 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12107 struct glyph_row *desired_cursor_row
12108 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12109 struct glyph_row *cursor_row
12110 = (desired_cursor_row->enabled_p
12111 ? desired_cursor_row
12112 : current_cursor_row);
12113 int row_r2l_p = cursor_row->reversed_p;
12114
12115 text_area_width = window_box_width (w, TEXT_AREA);
12116
12117 /* Scroll when cursor is inside this scroll margin. */
12118 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12119
12120 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12121 /* For left-to-right rows, hscroll when cursor is either
12122 (i) inside the right hscroll margin, or (ii) if it is
12123 inside the left margin and the window is already
12124 hscrolled. */
12125 && ((!row_r2l_p
12126 && ((XFASTINT (w->hscroll)
12127 && w->cursor.x <= h_margin)
12128 || (cursor_row->enabled_p
12129 && cursor_row->truncated_on_right_p
12130 && (w->cursor.x >= text_area_width - h_margin))))
12131 /* For right-to-left rows, the logic is similar,
12132 except that rules for scrolling to left and right
12133 are reversed. E.g., if cursor.x <= h_margin, we
12134 need to hscroll "to the right" unconditionally,
12135 and that will scroll the screen to the left so as
12136 to reveal the next portion of the row. */
12137 || (row_r2l_p
12138 && ((cursor_row->enabled_p
12139 /* FIXME: It is confusing to set the
12140 truncated_on_right_p flag when R2L rows
12141 are actually truncated on the left. */
12142 && cursor_row->truncated_on_right_p
12143 && w->cursor.x <= h_margin)
12144 || (XFASTINT (w->hscroll)
12145 && (w->cursor.x >= text_area_width - h_margin))))))
12146 {
12147 struct it it;
12148 ptrdiff_t hscroll;
12149 struct buffer *saved_current_buffer;
12150 ptrdiff_t pt;
12151 int wanted_x;
12152
12153 /* Find point in a display of infinite width. */
12154 saved_current_buffer = current_buffer;
12155 current_buffer = XBUFFER (w->buffer);
12156
12157 if (w == XWINDOW (selected_window))
12158 pt = PT;
12159 else
12160 {
12161 pt = marker_position (w->pointm);
12162 pt = max (BEGV, pt);
12163 pt = min (ZV, pt);
12164 }
12165
12166 /* Move iterator to pt starting at cursor_row->start in
12167 a line with infinite width. */
12168 init_to_row_start (&it, w, cursor_row);
12169 it.last_visible_x = INFINITY;
12170 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12171 current_buffer = saved_current_buffer;
12172
12173 /* Position cursor in window. */
12174 if (!hscroll_relative_p && hscroll_step_abs == 0)
12175 hscroll = max (0, (it.current_x
12176 - (ITERATOR_AT_END_OF_LINE_P (&it)
12177 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12178 : (text_area_width / 2))))
12179 / FRAME_COLUMN_WIDTH (it.f);
12180 else if ((!row_r2l_p
12181 && w->cursor.x >= text_area_width - h_margin)
12182 || (row_r2l_p && w->cursor.x <= h_margin))
12183 {
12184 if (hscroll_relative_p)
12185 wanted_x = text_area_width * (1 - hscroll_step_rel)
12186 - h_margin;
12187 else
12188 wanted_x = text_area_width
12189 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12190 - h_margin;
12191 hscroll
12192 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12193 }
12194 else
12195 {
12196 if (hscroll_relative_p)
12197 wanted_x = text_area_width * hscroll_step_rel
12198 + h_margin;
12199 else
12200 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12201 + h_margin;
12202 hscroll
12203 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12204 }
12205 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
12206
12207 /* Don't prevent redisplay optimizations if hscroll
12208 hasn't changed, as it will unnecessarily slow down
12209 redisplay. */
12210 if (XFASTINT (w->hscroll) != hscroll)
12211 {
12212 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12213 w->hscroll = make_number (hscroll);
12214 hscrolled_p = 1;
12215 }
12216 }
12217 }
12218
12219 window = w->next;
12220 }
12221
12222 /* Value is non-zero if hscroll of any leaf window has been changed. */
12223 return hscrolled_p;
12224 }
12225
12226
12227 /* Set hscroll so that cursor is visible and not inside horizontal
12228 scroll margins for all windows in the tree rooted at WINDOW. See
12229 also hscroll_window_tree above. Value is non-zero if any window's
12230 hscroll has been changed. If it has, desired matrices on the frame
12231 of WINDOW are cleared. */
12232
12233 static int
12234 hscroll_windows (Lisp_Object window)
12235 {
12236 int hscrolled_p = hscroll_window_tree (window);
12237 if (hscrolled_p)
12238 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12239 return hscrolled_p;
12240 }
12241
12242
12243 \f
12244 /************************************************************************
12245 Redisplay
12246 ************************************************************************/
12247
12248 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12249 to a non-zero value. This is sometimes handy to have in a debugger
12250 session. */
12251
12252 #if GLYPH_DEBUG
12253
12254 /* First and last unchanged row for try_window_id. */
12255
12256 static int debug_first_unchanged_at_end_vpos;
12257 static int debug_last_unchanged_at_beg_vpos;
12258
12259 /* Delta vpos and y. */
12260
12261 static int debug_dvpos, debug_dy;
12262
12263 /* Delta in characters and bytes for try_window_id. */
12264
12265 static ptrdiff_t debug_delta, debug_delta_bytes;
12266
12267 /* Values of window_end_pos and window_end_vpos at the end of
12268 try_window_id. */
12269
12270 static ptrdiff_t debug_end_vpos;
12271
12272 /* Append a string to W->desired_matrix->method. FMT is a printf
12273 format string. If trace_redisplay_p is non-zero also printf the
12274 resulting string to stderr. */
12275
12276 static void debug_method_add (struct window *, char const *, ...)
12277 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12278
12279 static void
12280 debug_method_add (struct window *w, char const *fmt, ...)
12281 {
12282 char buffer[512];
12283 char *method = w->desired_matrix->method;
12284 int len = strlen (method);
12285 int size = sizeof w->desired_matrix->method;
12286 int remaining = size - len - 1;
12287 va_list ap;
12288
12289 va_start (ap, fmt);
12290 vsprintf (buffer, fmt, ap);
12291 va_end (ap);
12292 if (len && remaining)
12293 {
12294 method[len] = '|';
12295 --remaining, ++len;
12296 }
12297
12298 strncpy (method + len, buffer, remaining);
12299
12300 if (trace_redisplay_p)
12301 fprintf (stderr, "%p (%s): %s\n",
12302 w,
12303 ((BUFFERP (w->buffer)
12304 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12305 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12306 : "no buffer"),
12307 buffer);
12308 }
12309
12310 #endif /* GLYPH_DEBUG */
12311
12312
12313 /* Value is non-zero if all changes in window W, which displays
12314 current_buffer, are in the text between START and END. START is a
12315 buffer position, END is given as a distance from Z. Used in
12316 redisplay_internal for display optimization. */
12317
12318 static inline int
12319 text_outside_line_unchanged_p (struct window *w,
12320 ptrdiff_t start, ptrdiff_t end)
12321 {
12322 int unchanged_p = 1;
12323
12324 /* If text or overlays have changed, see where. */
12325 if (XFASTINT (w->last_modified) < MODIFF
12326 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12327 {
12328 /* Gap in the line? */
12329 if (GPT < start || Z - GPT < end)
12330 unchanged_p = 0;
12331
12332 /* Changes start in front of the line, or end after it? */
12333 if (unchanged_p
12334 && (BEG_UNCHANGED < start - 1
12335 || END_UNCHANGED < end))
12336 unchanged_p = 0;
12337
12338 /* If selective display, can't optimize if changes start at the
12339 beginning of the line. */
12340 if (unchanged_p
12341 && INTEGERP (BVAR (current_buffer, selective_display))
12342 && XINT (BVAR (current_buffer, selective_display)) > 0
12343 && (BEG_UNCHANGED < start || GPT <= start))
12344 unchanged_p = 0;
12345
12346 /* If there are overlays at the start or end of the line, these
12347 may have overlay strings with newlines in them. A change at
12348 START, for instance, may actually concern the display of such
12349 overlay strings as well, and they are displayed on different
12350 lines. So, quickly rule out this case. (For the future, it
12351 might be desirable to implement something more telling than
12352 just BEG/END_UNCHANGED.) */
12353 if (unchanged_p)
12354 {
12355 if (BEG + BEG_UNCHANGED == start
12356 && overlay_touches_p (start))
12357 unchanged_p = 0;
12358 if (END_UNCHANGED == end
12359 && overlay_touches_p (Z - end))
12360 unchanged_p = 0;
12361 }
12362
12363 /* Under bidi reordering, adding or deleting a character in the
12364 beginning of a paragraph, before the first strong directional
12365 character, can change the base direction of the paragraph (unless
12366 the buffer specifies a fixed paragraph direction), which will
12367 require to redisplay the whole paragraph. It might be worthwhile
12368 to find the paragraph limits and widen the range of redisplayed
12369 lines to that, but for now just give up this optimization. */
12370 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12371 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12372 unchanged_p = 0;
12373 }
12374
12375 return unchanged_p;
12376 }
12377
12378
12379 /* Do a frame update, taking possible shortcuts into account. This is
12380 the main external entry point for redisplay.
12381
12382 If the last redisplay displayed an echo area message and that message
12383 is no longer requested, we clear the echo area or bring back the
12384 mini-buffer if that is in use. */
12385
12386 void
12387 redisplay (void)
12388 {
12389 redisplay_internal ();
12390 }
12391
12392
12393 static Lisp_Object
12394 overlay_arrow_string_or_property (Lisp_Object var)
12395 {
12396 Lisp_Object val;
12397
12398 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12399 return val;
12400
12401 return Voverlay_arrow_string;
12402 }
12403
12404 /* Return 1 if there are any overlay-arrows in current_buffer. */
12405 static int
12406 overlay_arrow_in_current_buffer_p (void)
12407 {
12408 Lisp_Object vlist;
12409
12410 for (vlist = Voverlay_arrow_variable_list;
12411 CONSP (vlist);
12412 vlist = XCDR (vlist))
12413 {
12414 Lisp_Object var = XCAR (vlist);
12415 Lisp_Object val;
12416
12417 if (!SYMBOLP (var))
12418 continue;
12419 val = find_symbol_value (var);
12420 if (MARKERP (val)
12421 && current_buffer == XMARKER (val)->buffer)
12422 return 1;
12423 }
12424 return 0;
12425 }
12426
12427
12428 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12429 has changed. */
12430
12431 static int
12432 overlay_arrows_changed_p (void)
12433 {
12434 Lisp_Object vlist;
12435
12436 for (vlist = Voverlay_arrow_variable_list;
12437 CONSP (vlist);
12438 vlist = XCDR (vlist))
12439 {
12440 Lisp_Object var = XCAR (vlist);
12441 Lisp_Object val, pstr;
12442
12443 if (!SYMBOLP (var))
12444 continue;
12445 val = find_symbol_value (var);
12446 if (!MARKERP (val))
12447 continue;
12448 if (! EQ (COERCE_MARKER (val),
12449 Fget (var, Qlast_arrow_position))
12450 || ! (pstr = overlay_arrow_string_or_property (var),
12451 EQ (pstr, Fget (var, Qlast_arrow_string))))
12452 return 1;
12453 }
12454 return 0;
12455 }
12456
12457 /* Mark overlay arrows to be updated on next redisplay. */
12458
12459 static void
12460 update_overlay_arrows (int up_to_date)
12461 {
12462 Lisp_Object vlist;
12463
12464 for (vlist = Voverlay_arrow_variable_list;
12465 CONSP (vlist);
12466 vlist = XCDR (vlist))
12467 {
12468 Lisp_Object var = XCAR (vlist);
12469
12470 if (!SYMBOLP (var))
12471 continue;
12472
12473 if (up_to_date > 0)
12474 {
12475 Lisp_Object val = find_symbol_value (var);
12476 Fput (var, Qlast_arrow_position,
12477 COERCE_MARKER (val));
12478 Fput (var, Qlast_arrow_string,
12479 overlay_arrow_string_or_property (var));
12480 }
12481 else if (up_to_date < 0
12482 || !NILP (Fget (var, Qlast_arrow_position)))
12483 {
12484 Fput (var, Qlast_arrow_position, Qt);
12485 Fput (var, Qlast_arrow_string, Qt);
12486 }
12487 }
12488 }
12489
12490
12491 /* Return overlay arrow string to display at row.
12492 Return integer (bitmap number) for arrow bitmap in left fringe.
12493 Return nil if no overlay arrow. */
12494
12495 static Lisp_Object
12496 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12497 {
12498 Lisp_Object vlist;
12499
12500 for (vlist = Voverlay_arrow_variable_list;
12501 CONSP (vlist);
12502 vlist = XCDR (vlist))
12503 {
12504 Lisp_Object var = XCAR (vlist);
12505 Lisp_Object val;
12506
12507 if (!SYMBOLP (var))
12508 continue;
12509
12510 val = find_symbol_value (var);
12511
12512 if (MARKERP (val)
12513 && current_buffer == XMARKER (val)->buffer
12514 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12515 {
12516 if (FRAME_WINDOW_P (it->f)
12517 /* FIXME: if ROW->reversed_p is set, this should test
12518 the right fringe, not the left one. */
12519 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12520 {
12521 #ifdef HAVE_WINDOW_SYSTEM
12522 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12523 {
12524 int fringe_bitmap;
12525 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12526 return make_number (fringe_bitmap);
12527 }
12528 #endif
12529 return make_number (-1); /* Use default arrow bitmap */
12530 }
12531 return overlay_arrow_string_or_property (var);
12532 }
12533 }
12534
12535 return Qnil;
12536 }
12537
12538 /* Return 1 if point moved out of or into a composition. Otherwise
12539 return 0. PREV_BUF and PREV_PT are the last point buffer and
12540 position. BUF and PT are the current point buffer and position. */
12541
12542 static int
12543 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12544 struct buffer *buf, ptrdiff_t pt)
12545 {
12546 ptrdiff_t start, end;
12547 Lisp_Object prop;
12548 Lisp_Object buffer;
12549
12550 XSETBUFFER (buffer, buf);
12551 /* Check a composition at the last point if point moved within the
12552 same buffer. */
12553 if (prev_buf == buf)
12554 {
12555 if (prev_pt == pt)
12556 /* Point didn't move. */
12557 return 0;
12558
12559 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12560 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12561 && COMPOSITION_VALID_P (start, end, prop)
12562 && start < prev_pt && end > prev_pt)
12563 /* The last point was within the composition. Return 1 iff
12564 point moved out of the composition. */
12565 return (pt <= start || pt >= end);
12566 }
12567
12568 /* Check a composition at the current point. */
12569 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12570 && find_composition (pt, -1, &start, &end, &prop, buffer)
12571 && COMPOSITION_VALID_P (start, end, prop)
12572 && start < pt && end > pt);
12573 }
12574
12575
12576 /* Reconsider the setting of B->clip_changed which is displayed
12577 in window W. */
12578
12579 static inline void
12580 reconsider_clip_changes (struct window *w, struct buffer *b)
12581 {
12582 if (b->clip_changed
12583 && !NILP (w->window_end_valid)
12584 && w->current_matrix->buffer == b
12585 && w->current_matrix->zv == BUF_ZV (b)
12586 && w->current_matrix->begv == BUF_BEGV (b))
12587 b->clip_changed = 0;
12588
12589 /* If display wasn't paused, and W is not a tool bar window, see if
12590 point has been moved into or out of a composition. In that case,
12591 we set b->clip_changed to 1 to force updating the screen. If
12592 b->clip_changed has already been set to 1, we can skip this
12593 check. */
12594 if (!b->clip_changed
12595 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12596 {
12597 ptrdiff_t pt;
12598
12599 if (w == XWINDOW (selected_window))
12600 pt = PT;
12601 else
12602 pt = marker_position (w->pointm);
12603
12604 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12605 || pt != XINT (w->last_point))
12606 && check_point_in_composition (w->current_matrix->buffer,
12607 XINT (w->last_point),
12608 XBUFFER (w->buffer), pt))
12609 b->clip_changed = 1;
12610 }
12611 }
12612 \f
12613
12614 /* Select FRAME to forward the values of frame-local variables into C
12615 variables so that the redisplay routines can access those values
12616 directly. */
12617
12618 static void
12619 select_frame_for_redisplay (Lisp_Object frame)
12620 {
12621 Lisp_Object tail, tem;
12622 Lisp_Object old = selected_frame;
12623 struct Lisp_Symbol *sym;
12624
12625 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12626
12627 selected_frame = frame;
12628
12629 do {
12630 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12631 if (CONSP (XCAR (tail))
12632 && (tem = XCAR (XCAR (tail)),
12633 SYMBOLP (tem))
12634 && (sym = indirect_variable (XSYMBOL (tem)),
12635 sym->redirect == SYMBOL_LOCALIZED)
12636 && sym->val.blv->frame_local)
12637 /* Use find_symbol_value rather than Fsymbol_value
12638 to avoid an error if it is void. */
12639 find_symbol_value (tem);
12640 } while (!EQ (frame, old) && (frame = old, 1));
12641 }
12642
12643
12644 #define STOP_POLLING \
12645 do { if (! polling_stopped_here) stop_polling (); \
12646 polling_stopped_here = 1; } while (0)
12647
12648 #define RESUME_POLLING \
12649 do { if (polling_stopped_here) start_polling (); \
12650 polling_stopped_here = 0; } while (0)
12651
12652
12653 /* Perhaps in the future avoid recentering windows if it
12654 is not necessary; currently that causes some problems. */
12655
12656 static void
12657 redisplay_internal (void)
12658 {
12659 struct window *w = XWINDOW (selected_window);
12660 struct window *sw;
12661 struct frame *fr;
12662 int pending;
12663 int must_finish = 0;
12664 struct text_pos tlbufpos, tlendpos;
12665 int number_of_visible_frames;
12666 ptrdiff_t count, count1;
12667 struct frame *sf;
12668 int polling_stopped_here = 0;
12669 Lisp_Object old_frame = selected_frame;
12670
12671 /* Non-zero means redisplay has to consider all windows on all
12672 frames. Zero means, only selected_window is considered. */
12673 int consider_all_windows_p;
12674
12675 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12676
12677 /* No redisplay if running in batch mode or frame is not yet fully
12678 initialized, or redisplay is explicitly turned off by setting
12679 Vinhibit_redisplay. */
12680 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12681 || !NILP (Vinhibit_redisplay))
12682 return;
12683
12684 /* Don't examine these until after testing Vinhibit_redisplay.
12685 When Emacs is shutting down, perhaps because its connection to
12686 X has dropped, we should not look at them at all. */
12687 fr = XFRAME (w->frame);
12688 sf = SELECTED_FRAME ();
12689
12690 if (!fr->glyphs_initialized_p)
12691 return;
12692
12693 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12694 if (popup_activated ())
12695 return;
12696 #endif
12697
12698 /* I don't think this happens but let's be paranoid. */
12699 if (redisplaying_p)
12700 return;
12701
12702 /* Record a function that resets redisplaying_p to its old value
12703 when we leave this function. */
12704 count = SPECPDL_INDEX ();
12705 record_unwind_protect (unwind_redisplay,
12706 Fcons (make_number (redisplaying_p), selected_frame));
12707 ++redisplaying_p;
12708 specbind (Qinhibit_free_realized_faces, Qnil);
12709
12710 {
12711 Lisp_Object tail, frame;
12712
12713 FOR_EACH_FRAME (tail, frame)
12714 {
12715 struct frame *f = XFRAME (frame);
12716 f->already_hscrolled_p = 0;
12717 }
12718 }
12719
12720 retry:
12721 /* Remember the currently selected window. */
12722 sw = w;
12723
12724 if (!EQ (old_frame, selected_frame)
12725 && FRAME_LIVE_P (XFRAME (old_frame)))
12726 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12727 selected_frame and selected_window to be temporarily out-of-sync so
12728 when we come back here via `goto retry', we need to resync because we
12729 may need to run Elisp code (via prepare_menu_bars). */
12730 select_frame_for_redisplay (old_frame);
12731
12732 pending = 0;
12733 reconsider_clip_changes (w, current_buffer);
12734 last_escape_glyph_frame = NULL;
12735 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12736 last_glyphless_glyph_frame = NULL;
12737 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12738
12739 /* If new fonts have been loaded that make a glyph matrix adjustment
12740 necessary, do it. */
12741 if (fonts_changed_p)
12742 {
12743 adjust_glyphs (NULL);
12744 ++windows_or_buffers_changed;
12745 fonts_changed_p = 0;
12746 }
12747
12748 /* If face_change_count is non-zero, init_iterator will free all
12749 realized faces, which includes the faces referenced from current
12750 matrices. So, we can't reuse current matrices in this case. */
12751 if (face_change_count)
12752 ++windows_or_buffers_changed;
12753
12754 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12755 && FRAME_TTY (sf)->previous_frame != sf)
12756 {
12757 /* Since frames on a single ASCII terminal share the same
12758 display area, displaying a different frame means redisplay
12759 the whole thing. */
12760 windows_or_buffers_changed++;
12761 SET_FRAME_GARBAGED (sf);
12762 #ifndef DOS_NT
12763 set_tty_color_mode (FRAME_TTY (sf), sf);
12764 #endif
12765 FRAME_TTY (sf)->previous_frame = sf;
12766 }
12767
12768 /* Set the visible flags for all frames. Do this before checking
12769 for resized or garbaged frames; they want to know if their frames
12770 are visible. See the comment in frame.h for
12771 FRAME_SAMPLE_VISIBILITY. */
12772 {
12773 Lisp_Object tail, frame;
12774
12775 number_of_visible_frames = 0;
12776
12777 FOR_EACH_FRAME (tail, frame)
12778 {
12779 struct frame *f = XFRAME (frame);
12780
12781 FRAME_SAMPLE_VISIBILITY (f);
12782 if (FRAME_VISIBLE_P (f))
12783 ++number_of_visible_frames;
12784 clear_desired_matrices (f);
12785 }
12786 }
12787
12788 /* Notice any pending interrupt request to change frame size. */
12789 do_pending_window_change (1);
12790
12791 /* do_pending_window_change could change the selected_window due to
12792 frame resizing which makes the selected window too small. */
12793 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12794 {
12795 sw = w;
12796 reconsider_clip_changes (w, current_buffer);
12797 }
12798
12799 /* Clear frames marked as garbaged. */
12800 if (frame_garbaged)
12801 clear_garbaged_frames ();
12802
12803 /* Build menubar and tool-bar items. */
12804 if (NILP (Vmemory_full))
12805 prepare_menu_bars ();
12806
12807 if (windows_or_buffers_changed)
12808 update_mode_lines++;
12809
12810 /* Detect case that we need to write or remove a star in the mode line. */
12811 if ((SAVE_MODIFF < MODIFF) != !NILP (w->last_had_star))
12812 {
12813 w->update_mode_line = Qt;
12814 if (buffer_shared > 1)
12815 update_mode_lines++;
12816 }
12817
12818 /* Avoid invocation of point motion hooks by `current_column' below. */
12819 count1 = SPECPDL_INDEX ();
12820 specbind (Qinhibit_point_motion_hooks, Qt);
12821
12822 /* If %c is in the mode line, update it if needed. */
12823 if (!NILP (w->column_number_displayed)
12824 /* This alternative quickly identifies a common case
12825 where no change is needed. */
12826 && !(PT == XFASTINT (w->last_point)
12827 && XFASTINT (w->last_modified) >= MODIFF
12828 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12829 && (XFASTINT (w->column_number_displayed) != current_column ()))
12830 w->update_mode_line = Qt;
12831
12832 unbind_to (count1, Qnil);
12833
12834 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12835
12836 /* The variable buffer_shared is set in redisplay_window and
12837 indicates that we redisplay a buffer in different windows. See
12838 there. */
12839 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
12840 || cursor_type_changed);
12841
12842 /* If specs for an arrow have changed, do thorough redisplay
12843 to ensure we remove any arrow that should no longer exist. */
12844 if (overlay_arrows_changed_p ())
12845 consider_all_windows_p = windows_or_buffers_changed = 1;
12846
12847 /* Normally the message* functions will have already displayed and
12848 updated the echo area, but the frame may have been trashed, or
12849 the update may have been preempted, so display the echo area
12850 again here. Checking message_cleared_p captures the case that
12851 the echo area should be cleared. */
12852 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12853 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12854 || (message_cleared_p
12855 && minibuf_level == 0
12856 /* If the mini-window is currently selected, this means the
12857 echo-area doesn't show through. */
12858 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12859 {
12860 int window_height_changed_p = echo_area_display (0);
12861 must_finish = 1;
12862
12863 /* If we don't display the current message, don't clear the
12864 message_cleared_p flag, because, if we did, we wouldn't clear
12865 the echo area in the next redisplay which doesn't preserve
12866 the echo area. */
12867 if (!display_last_displayed_message_p)
12868 message_cleared_p = 0;
12869
12870 if (fonts_changed_p)
12871 goto retry;
12872 else if (window_height_changed_p)
12873 {
12874 consider_all_windows_p = 1;
12875 ++update_mode_lines;
12876 ++windows_or_buffers_changed;
12877
12878 /* If window configuration was changed, frames may have been
12879 marked garbaged. Clear them or we will experience
12880 surprises wrt scrolling. */
12881 if (frame_garbaged)
12882 clear_garbaged_frames ();
12883 }
12884 }
12885 else if (EQ (selected_window, minibuf_window)
12886 && (current_buffer->clip_changed
12887 || XFASTINT (w->last_modified) < MODIFF
12888 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12889 && resize_mini_window (w, 0))
12890 {
12891 /* Resized active mini-window to fit the size of what it is
12892 showing if its contents might have changed. */
12893 must_finish = 1;
12894 /* FIXME: this causes all frames to be updated, which seems unnecessary
12895 since only the current frame needs to be considered. This function needs
12896 to be rewritten with two variables, consider_all_windows and
12897 consider_all_frames. */
12898 consider_all_windows_p = 1;
12899 ++windows_or_buffers_changed;
12900 ++update_mode_lines;
12901
12902 /* If window configuration was changed, frames may have been
12903 marked garbaged. Clear them or we will experience
12904 surprises wrt scrolling. */
12905 if (frame_garbaged)
12906 clear_garbaged_frames ();
12907 }
12908
12909
12910 /* If showing the region, and mark has changed, we must redisplay
12911 the whole window. The assignment to this_line_start_pos prevents
12912 the optimization directly below this if-statement. */
12913 if (((!NILP (Vtransient_mark_mode)
12914 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
12915 != !NILP (w->region_showing))
12916 || (!NILP (w->region_showing)
12917 && !EQ (w->region_showing,
12918 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
12919 CHARPOS (this_line_start_pos) = 0;
12920
12921 /* Optimize the case that only the line containing the cursor in the
12922 selected window has changed. Variables starting with this_ are
12923 set in display_line and record information about the line
12924 containing the cursor. */
12925 tlbufpos = this_line_start_pos;
12926 tlendpos = this_line_end_pos;
12927 if (!consider_all_windows_p
12928 && CHARPOS (tlbufpos) > 0
12929 && NILP (w->update_mode_line)
12930 && !current_buffer->clip_changed
12931 && !current_buffer->prevent_redisplay_optimizations_p
12932 && FRAME_VISIBLE_P (XFRAME (w->frame))
12933 && !FRAME_OBSCURED_P (XFRAME (w->frame))
12934 /* Make sure recorded data applies to current buffer, etc. */
12935 && this_line_buffer == current_buffer
12936 && current_buffer == XBUFFER (w->buffer)
12937 && NILP (w->force_start)
12938 && NILP (w->optional_new_start)
12939 /* Point must be on the line that we have info recorded about. */
12940 && PT >= CHARPOS (tlbufpos)
12941 && PT <= Z - CHARPOS (tlendpos)
12942 /* All text outside that line, including its final newline,
12943 must be unchanged. */
12944 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
12945 CHARPOS (tlendpos)))
12946 {
12947 if (CHARPOS (tlbufpos) > BEGV
12948 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
12949 && (CHARPOS (tlbufpos) == ZV
12950 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
12951 /* Former continuation line has disappeared by becoming empty. */
12952 goto cancel;
12953 else if (XFASTINT (w->last_modified) < MODIFF
12954 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
12955 || MINI_WINDOW_P (w))
12956 {
12957 /* We have to handle the case of continuation around a
12958 wide-column character (see the comment in indent.c around
12959 line 1340).
12960
12961 For instance, in the following case:
12962
12963 -------- Insert --------
12964 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
12965 J_I_ ==> J_I_ `^^' are cursors.
12966 ^^ ^^
12967 -------- --------
12968
12969 As we have to redraw the line above, we cannot use this
12970 optimization. */
12971
12972 struct it it;
12973 int line_height_before = this_line_pixel_height;
12974
12975 /* Note that start_display will handle the case that the
12976 line starting at tlbufpos is a continuation line. */
12977 start_display (&it, w, tlbufpos);
12978
12979 /* Implementation note: It this still necessary? */
12980 if (it.current_x != this_line_start_x)
12981 goto cancel;
12982
12983 TRACE ((stderr, "trying display optimization 1\n"));
12984 w->cursor.vpos = -1;
12985 overlay_arrow_seen = 0;
12986 it.vpos = this_line_vpos;
12987 it.current_y = this_line_y;
12988 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
12989 display_line (&it);
12990
12991 /* If line contains point, is not continued,
12992 and ends at same distance from eob as before, we win. */
12993 if (w->cursor.vpos >= 0
12994 /* Line is not continued, otherwise this_line_start_pos
12995 would have been set to 0 in display_line. */
12996 && CHARPOS (this_line_start_pos)
12997 /* Line ends as before. */
12998 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
12999 /* Line has same height as before. Otherwise other lines
13000 would have to be shifted up or down. */
13001 && this_line_pixel_height == line_height_before)
13002 {
13003 /* If this is not the window's last line, we must adjust
13004 the charstarts of the lines below. */
13005 if (it.current_y < it.last_visible_y)
13006 {
13007 struct glyph_row *row
13008 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13009 ptrdiff_t delta, delta_bytes;
13010
13011 /* We used to distinguish between two cases here,
13012 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13013 when the line ends in a newline or the end of the
13014 buffer's accessible portion. But both cases did
13015 the same, so they were collapsed. */
13016 delta = (Z
13017 - CHARPOS (tlendpos)
13018 - MATRIX_ROW_START_CHARPOS (row));
13019 delta_bytes = (Z_BYTE
13020 - BYTEPOS (tlendpos)
13021 - MATRIX_ROW_START_BYTEPOS (row));
13022
13023 increment_matrix_positions (w->current_matrix,
13024 this_line_vpos + 1,
13025 w->current_matrix->nrows,
13026 delta, delta_bytes);
13027 }
13028
13029 /* If this row displays text now but previously didn't,
13030 or vice versa, w->window_end_vpos may have to be
13031 adjusted. */
13032 if ((it.glyph_row - 1)->displays_text_p)
13033 {
13034 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13035 XSETINT (w->window_end_vpos, this_line_vpos);
13036 }
13037 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13038 && this_line_vpos > 0)
13039 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13040 w->window_end_valid = Qnil;
13041
13042 /* Update hint: No need to try to scroll in update_window. */
13043 w->desired_matrix->no_scrolling_p = 1;
13044
13045 #if GLYPH_DEBUG
13046 *w->desired_matrix->method = 0;
13047 debug_method_add (w, "optimization 1");
13048 #endif
13049 #ifdef HAVE_WINDOW_SYSTEM
13050 update_window_fringes (w, 0);
13051 #endif
13052 goto update;
13053 }
13054 else
13055 goto cancel;
13056 }
13057 else if (/* Cursor position hasn't changed. */
13058 PT == XFASTINT (w->last_point)
13059 /* Make sure the cursor was last displayed
13060 in this window. Otherwise we have to reposition it. */
13061 && 0 <= w->cursor.vpos
13062 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13063 {
13064 if (!must_finish)
13065 {
13066 do_pending_window_change (1);
13067 /* If selected_window changed, redisplay again. */
13068 if (WINDOWP (selected_window)
13069 && (w = XWINDOW (selected_window)) != sw)
13070 goto retry;
13071
13072 /* We used to always goto end_of_redisplay here, but this
13073 isn't enough if we have a blinking cursor. */
13074 if (w->cursor_off_p == w->last_cursor_off_p)
13075 goto end_of_redisplay;
13076 }
13077 goto update;
13078 }
13079 /* If highlighting the region, or if the cursor is in the echo area,
13080 then we can't just move the cursor. */
13081 else if (! (!NILP (Vtransient_mark_mode)
13082 && !NILP (BVAR (current_buffer, mark_active)))
13083 && (EQ (selected_window, BVAR (current_buffer, last_selected_window))
13084 || highlight_nonselected_windows)
13085 && NILP (w->region_showing)
13086 && NILP (Vshow_trailing_whitespace)
13087 && !cursor_in_echo_area)
13088 {
13089 struct it it;
13090 struct glyph_row *row;
13091
13092 /* Skip from tlbufpos to PT and see where it is. Note that
13093 PT may be in invisible text. If so, we will end at the
13094 next visible position. */
13095 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13096 NULL, DEFAULT_FACE_ID);
13097 it.current_x = this_line_start_x;
13098 it.current_y = this_line_y;
13099 it.vpos = this_line_vpos;
13100
13101 /* The call to move_it_to stops in front of PT, but
13102 moves over before-strings. */
13103 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13104
13105 if (it.vpos == this_line_vpos
13106 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13107 row->enabled_p))
13108 {
13109 xassert (this_line_vpos == it.vpos);
13110 xassert (this_line_y == it.current_y);
13111 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13112 #if GLYPH_DEBUG
13113 *w->desired_matrix->method = 0;
13114 debug_method_add (w, "optimization 3");
13115 #endif
13116 goto update;
13117 }
13118 else
13119 goto cancel;
13120 }
13121
13122 cancel:
13123 /* Text changed drastically or point moved off of line. */
13124 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13125 }
13126
13127 CHARPOS (this_line_start_pos) = 0;
13128 consider_all_windows_p |= buffer_shared > 1;
13129 ++clear_face_cache_count;
13130 #ifdef HAVE_WINDOW_SYSTEM
13131 ++clear_image_cache_count;
13132 #endif
13133
13134 /* Build desired matrices, and update the display. If
13135 consider_all_windows_p is non-zero, do it for all windows on all
13136 frames. Otherwise do it for selected_window, only. */
13137
13138 if (consider_all_windows_p)
13139 {
13140 Lisp_Object tail, frame;
13141
13142 FOR_EACH_FRAME (tail, frame)
13143 XFRAME (frame)->updated_p = 0;
13144
13145 /* Recompute # windows showing selected buffer. This will be
13146 incremented each time such a window is displayed. */
13147 buffer_shared = 0;
13148
13149 FOR_EACH_FRAME (tail, frame)
13150 {
13151 struct frame *f = XFRAME (frame);
13152
13153 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13154 {
13155 if (! EQ (frame, selected_frame))
13156 /* Select the frame, for the sake of frame-local
13157 variables. */
13158 select_frame_for_redisplay (frame);
13159
13160 /* Mark all the scroll bars to be removed; we'll redeem
13161 the ones we want when we redisplay their windows. */
13162 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13163 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13164
13165 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13166 redisplay_windows (FRAME_ROOT_WINDOW (f));
13167
13168 /* The X error handler may have deleted that frame. */
13169 if (!FRAME_LIVE_P (f))
13170 continue;
13171
13172 /* Any scroll bars which redisplay_windows should have
13173 nuked should now go away. */
13174 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13175 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13176
13177 /* If fonts changed, display again. */
13178 /* ??? rms: I suspect it is a mistake to jump all the way
13179 back to retry here. It should just retry this frame. */
13180 if (fonts_changed_p)
13181 goto retry;
13182
13183 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13184 {
13185 /* See if we have to hscroll. */
13186 if (!f->already_hscrolled_p)
13187 {
13188 f->already_hscrolled_p = 1;
13189 if (hscroll_windows (f->root_window))
13190 goto retry;
13191 }
13192
13193 /* Prevent various kinds of signals during display
13194 update. stdio is not robust about handling
13195 signals, which can cause an apparent I/O
13196 error. */
13197 if (interrupt_input)
13198 unrequest_sigio ();
13199 STOP_POLLING;
13200
13201 /* Update the display. */
13202 set_window_update_flags (XWINDOW (f->root_window), 1);
13203 pending |= update_frame (f, 0, 0);
13204 f->updated_p = 1;
13205 }
13206 }
13207 }
13208
13209 if (!EQ (old_frame, selected_frame)
13210 && FRAME_LIVE_P (XFRAME (old_frame)))
13211 /* We played a bit fast-and-loose above and allowed selected_frame
13212 and selected_window to be temporarily out-of-sync but let's make
13213 sure this stays contained. */
13214 select_frame_for_redisplay (old_frame);
13215 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13216
13217 if (!pending)
13218 {
13219 /* Do the mark_window_display_accurate after all windows have
13220 been redisplayed because this call resets flags in buffers
13221 which are needed for proper redisplay. */
13222 FOR_EACH_FRAME (tail, frame)
13223 {
13224 struct frame *f = XFRAME (frame);
13225 if (f->updated_p)
13226 {
13227 mark_window_display_accurate (f->root_window, 1);
13228 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13229 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13230 }
13231 }
13232 }
13233 }
13234 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13235 {
13236 Lisp_Object mini_window;
13237 struct frame *mini_frame;
13238
13239 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13240 /* Use list_of_error, not Qerror, so that
13241 we catch only errors and don't run the debugger. */
13242 internal_condition_case_1 (redisplay_window_1, selected_window,
13243 list_of_error,
13244 redisplay_window_error);
13245
13246 /* Compare desired and current matrices, perform output. */
13247
13248 update:
13249 /* If fonts changed, display again. */
13250 if (fonts_changed_p)
13251 goto retry;
13252
13253 /* Prevent various kinds of signals during display update.
13254 stdio is not robust about handling signals,
13255 which can cause an apparent I/O error. */
13256 if (interrupt_input)
13257 unrequest_sigio ();
13258 STOP_POLLING;
13259
13260 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13261 {
13262 if (hscroll_windows (selected_window))
13263 goto retry;
13264
13265 XWINDOW (selected_window)->must_be_updated_p = 1;
13266 pending = update_frame (sf, 0, 0);
13267 }
13268
13269 /* We may have called echo_area_display at the top of this
13270 function. If the echo area is on another frame, that may
13271 have put text on a frame other than the selected one, so the
13272 above call to update_frame would not have caught it. Catch
13273 it here. */
13274 mini_window = FRAME_MINIBUF_WINDOW (sf);
13275 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13276
13277 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13278 {
13279 XWINDOW (mini_window)->must_be_updated_p = 1;
13280 pending |= update_frame (mini_frame, 0, 0);
13281 if (!pending && hscroll_windows (mini_window))
13282 goto retry;
13283 }
13284 }
13285
13286 /* If display was paused because of pending input, make sure we do a
13287 thorough update the next time. */
13288 if (pending)
13289 {
13290 /* Prevent the optimization at the beginning of
13291 redisplay_internal that tries a single-line update of the
13292 line containing the cursor in the selected window. */
13293 CHARPOS (this_line_start_pos) = 0;
13294
13295 /* Let the overlay arrow be updated the next time. */
13296 update_overlay_arrows (0);
13297
13298 /* If we pause after scrolling, some rows in the current
13299 matrices of some windows are not valid. */
13300 if (!WINDOW_FULL_WIDTH_P (w)
13301 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13302 update_mode_lines = 1;
13303 }
13304 else
13305 {
13306 if (!consider_all_windows_p)
13307 {
13308 /* This has already been done above if
13309 consider_all_windows_p is set. */
13310 mark_window_display_accurate_1 (w, 1);
13311
13312 /* Say overlay arrows are up to date. */
13313 update_overlay_arrows (1);
13314
13315 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13316 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13317 }
13318
13319 update_mode_lines = 0;
13320 windows_or_buffers_changed = 0;
13321 cursor_type_changed = 0;
13322 }
13323
13324 /* Start SIGIO interrupts coming again. Having them off during the
13325 code above makes it less likely one will discard output, but not
13326 impossible, since there might be stuff in the system buffer here.
13327 But it is much hairier to try to do anything about that. */
13328 if (interrupt_input)
13329 request_sigio ();
13330 RESUME_POLLING;
13331
13332 /* If a frame has become visible which was not before, redisplay
13333 again, so that we display it. Expose events for such a frame
13334 (which it gets when becoming visible) don't call the parts of
13335 redisplay constructing glyphs, so simply exposing a frame won't
13336 display anything in this case. So, we have to display these
13337 frames here explicitly. */
13338 if (!pending)
13339 {
13340 Lisp_Object tail, frame;
13341 int new_count = 0;
13342
13343 FOR_EACH_FRAME (tail, frame)
13344 {
13345 int this_is_visible = 0;
13346
13347 if (XFRAME (frame)->visible)
13348 this_is_visible = 1;
13349 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13350 if (XFRAME (frame)->visible)
13351 this_is_visible = 1;
13352
13353 if (this_is_visible)
13354 new_count++;
13355 }
13356
13357 if (new_count != number_of_visible_frames)
13358 windows_or_buffers_changed++;
13359 }
13360
13361 /* Change frame size now if a change is pending. */
13362 do_pending_window_change (1);
13363
13364 /* If we just did a pending size change, or have additional
13365 visible frames, or selected_window changed, redisplay again. */
13366 if ((windows_or_buffers_changed && !pending)
13367 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13368 goto retry;
13369
13370 /* Clear the face and image caches.
13371
13372 We used to do this only if consider_all_windows_p. But the cache
13373 needs to be cleared if a timer creates images in the current
13374 buffer (e.g. the test case in Bug#6230). */
13375
13376 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13377 {
13378 clear_face_cache (0);
13379 clear_face_cache_count = 0;
13380 }
13381
13382 #ifdef HAVE_WINDOW_SYSTEM
13383 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13384 {
13385 clear_image_caches (Qnil);
13386 clear_image_cache_count = 0;
13387 }
13388 #endif /* HAVE_WINDOW_SYSTEM */
13389
13390 end_of_redisplay:
13391 unbind_to (count, Qnil);
13392 RESUME_POLLING;
13393 }
13394
13395
13396 /* Redisplay, but leave alone any recent echo area message unless
13397 another message has been requested in its place.
13398
13399 This is useful in situations where you need to redisplay but no
13400 user action has occurred, making it inappropriate for the message
13401 area to be cleared. See tracking_off and
13402 wait_reading_process_output for examples of these situations.
13403
13404 FROM_WHERE is an integer saying from where this function was
13405 called. This is useful for debugging. */
13406
13407 void
13408 redisplay_preserve_echo_area (int from_where)
13409 {
13410 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13411
13412 if (!NILP (echo_area_buffer[1]))
13413 {
13414 /* We have a previously displayed message, but no current
13415 message. Redisplay the previous message. */
13416 display_last_displayed_message_p = 1;
13417 redisplay_internal ();
13418 display_last_displayed_message_p = 0;
13419 }
13420 else
13421 redisplay_internal ();
13422
13423 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13424 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13425 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13426 }
13427
13428
13429 /* Function registered with record_unwind_protect in
13430 redisplay_internal. Reset redisplaying_p to the value it had
13431 before redisplay_internal was called, and clear
13432 prevent_freeing_realized_faces_p. It also selects the previously
13433 selected frame, unless it has been deleted (by an X connection
13434 failure during redisplay, for example). */
13435
13436 static Lisp_Object
13437 unwind_redisplay (Lisp_Object val)
13438 {
13439 Lisp_Object old_redisplaying_p, old_frame;
13440
13441 old_redisplaying_p = XCAR (val);
13442 redisplaying_p = XFASTINT (old_redisplaying_p);
13443 old_frame = XCDR (val);
13444 if (! EQ (old_frame, selected_frame)
13445 && FRAME_LIVE_P (XFRAME (old_frame)))
13446 select_frame_for_redisplay (old_frame);
13447 return Qnil;
13448 }
13449
13450
13451 /* Mark the display of window W as accurate or inaccurate. If
13452 ACCURATE_P is non-zero mark display of W as accurate. If
13453 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13454 redisplay_internal is called. */
13455
13456 static void
13457 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13458 {
13459 if (BUFFERP (w->buffer))
13460 {
13461 struct buffer *b = XBUFFER (w->buffer);
13462
13463 w->last_modified
13464 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13465 w->last_overlay_modified
13466 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13467 w->last_had_star
13468 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b) ? Qt : Qnil;
13469
13470 if (accurate_p)
13471 {
13472 b->clip_changed = 0;
13473 b->prevent_redisplay_optimizations_p = 0;
13474
13475 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13476 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13477 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13478 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13479
13480 w->current_matrix->buffer = b;
13481 w->current_matrix->begv = BUF_BEGV (b);
13482 w->current_matrix->zv = BUF_ZV (b);
13483
13484 w->last_cursor = w->cursor;
13485 w->last_cursor_off_p = w->cursor_off_p;
13486
13487 if (w == XWINDOW (selected_window))
13488 w->last_point = make_number (BUF_PT (b));
13489 else
13490 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13491 }
13492 }
13493
13494 if (accurate_p)
13495 {
13496 w->window_end_valid = w->buffer;
13497 w->update_mode_line = Qnil;
13498 }
13499 }
13500
13501
13502 /* Mark the display of windows in the window tree rooted at WINDOW as
13503 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13504 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13505 be redisplayed the next time redisplay_internal is called. */
13506
13507 void
13508 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13509 {
13510 struct window *w;
13511
13512 for (; !NILP (window); window = w->next)
13513 {
13514 w = XWINDOW (window);
13515 mark_window_display_accurate_1 (w, accurate_p);
13516
13517 if (!NILP (w->vchild))
13518 mark_window_display_accurate (w->vchild, accurate_p);
13519 if (!NILP (w->hchild))
13520 mark_window_display_accurate (w->hchild, accurate_p);
13521 }
13522
13523 if (accurate_p)
13524 {
13525 update_overlay_arrows (1);
13526 }
13527 else
13528 {
13529 /* Force a thorough redisplay the next time by setting
13530 last_arrow_position and last_arrow_string to t, which is
13531 unequal to any useful value of Voverlay_arrow_... */
13532 update_overlay_arrows (-1);
13533 }
13534 }
13535
13536
13537 /* Return value in display table DP (Lisp_Char_Table *) for character
13538 C. Since a display table doesn't have any parent, we don't have to
13539 follow parent. Do not call this function directly but use the
13540 macro DISP_CHAR_VECTOR. */
13541
13542 Lisp_Object
13543 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13544 {
13545 Lisp_Object val;
13546
13547 if (ASCII_CHAR_P (c))
13548 {
13549 val = dp->ascii;
13550 if (SUB_CHAR_TABLE_P (val))
13551 val = XSUB_CHAR_TABLE (val)->contents[c];
13552 }
13553 else
13554 {
13555 Lisp_Object table;
13556
13557 XSETCHAR_TABLE (table, dp);
13558 val = char_table_ref (table, c);
13559 }
13560 if (NILP (val))
13561 val = dp->defalt;
13562 return val;
13563 }
13564
13565
13566 \f
13567 /***********************************************************************
13568 Window Redisplay
13569 ***********************************************************************/
13570
13571 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13572
13573 static void
13574 redisplay_windows (Lisp_Object window)
13575 {
13576 while (!NILP (window))
13577 {
13578 struct window *w = XWINDOW (window);
13579
13580 if (!NILP (w->hchild))
13581 redisplay_windows (w->hchild);
13582 else if (!NILP (w->vchild))
13583 redisplay_windows (w->vchild);
13584 else if (!NILP (w->buffer))
13585 {
13586 displayed_buffer = XBUFFER (w->buffer);
13587 /* Use list_of_error, not Qerror, so that
13588 we catch only errors and don't run the debugger. */
13589 internal_condition_case_1 (redisplay_window_0, window,
13590 list_of_error,
13591 redisplay_window_error);
13592 }
13593
13594 window = w->next;
13595 }
13596 }
13597
13598 static Lisp_Object
13599 redisplay_window_error (Lisp_Object ignore)
13600 {
13601 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13602 return Qnil;
13603 }
13604
13605 static Lisp_Object
13606 redisplay_window_0 (Lisp_Object window)
13607 {
13608 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13609 redisplay_window (window, 0);
13610 return Qnil;
13611 }
13612
13613 static Lisp_Object
13614 redisplay_window_1 (Lisp_Object window)
13615 {
13616 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13617 redisplay_window (window, 1);
13618 return Qnil;
13619 }
13620 \f
13621
13622 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13623 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13624 which positions recorded in ROW differ from current buffer
13625 positions.
13626
13627 Return 0 if cursor is not on this row, 1 otherwise. */
13628
13629 static int
13630 set_cursor_from_row (struct window *w, struct glyph_row *row,
13631 struct glyph_matrix *matrix,
13632 ptrdiff_t delta, ptrdiff_t delta_bytes,
13633 int dy, int dvpos)
13634 {
13635 struct glyph *glyph = row->glyphs[TEXT_AREA];
13636 struct glyph *end = glyph + row->used[TEXT_AREA];
13637 struct glyph *cursor = NULL;
13638 /* The last known character position in row. */
13639 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13640 int x = row->x;
13641 ptrdiff_t pt_old = PT - delta;
13642 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13643 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13644 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13645 /* A glyph beyond the edge of TEXT_AREA which we should never
13646 touch. */
13647 struct glyph *glyphs_end = end;
13648 /* Non-zero means we've found a match for cursor position, but that
13649 glyph has the avoid_cursor_p flag set. */
13650 int match_with_avoid_cursor = 0;
13651 /* Non-zero means we've seen at least one glyph that came from a
13652 display string. */
13653 int string_seen = 0;
13654 /* Largest and smalles buffer positions seen so far during scan of
13655 glyph row. */
13656 ptrdiff_t bpos_max = pos_before;
13657 ptrdiff_t bpos_min = pos_after;
13658 /* Last buffer position covered by an overlay string with an integer
13659 `cursor' property. */
13660 ptrdiff_t bpos_covered = 0;
13661 /* Non-zero means the display string on which to display the cursor
13662 comes from a text property, not from an overlay. */
13663 int string_from_text_prop = 0;
13664
13665 /* Skip over glyphs not having an object at the start and the end of
13666 the row. These are special glyphs like truncation marks on
13667 terminal frames. */
13668 if (row->displays_text_p)
13669 {
13670 if (!row->reversed_p)
13671 {
13672 while (glyph < end
13673 && INTEGERP (glyph->object)
13674 && glyph->charpos < 0)
13675 {
13676 x += glyph->pixel_width;
13677 ++glyph;
13678 }
13679 while (end > glyph
13680 && INTEGERP ((end - 1)->object)
13681 /* CHARPOS is zero for blanks and stretch glyphs
13682 inserted by extend_face_to_end_of_line. */
13683 && (end - 1)->charpos <= 0)
13684 --end;
13685 glyph_before = glyph - 1;
13686 glyph_after = end;
13687 }
13688 else
13689 {
13690 struct glyph *g;
13691
13692 /* If the glyph row is reversed, we need to process it from back
13693 to front, so swap the edge pointers. */
13694 glyphs_end = end = glyph - 1;
13695 glyph += row->used[TEXT_AREA] - 1;
13696
13697 while (glyph > end + 1
13698 && INTEGERP (glyph->object)
13699 && glyph->charpos < 0)
13700 {
13701 --glyph;
13702 x -= glyph->pixel_width;
13703 }
13704 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13705 --glyph;
13706 /* By default, in reversed rows we put the cursor on the
13707 rightmost (first in the reading order) glyph. */
13708 for (g = end + 1; g < glyph; g++)
13709 x += g->pixel_width;
13710 while (end < glyph
13711 && INTEGERP ((end + 1)->object)
13712 && (end + 1)->charpos <= 0)
13713 ++end;
13714 glyph_before = glyph + 1;
13715 glyph_after = end;
13716 }
13717 }
13718 else if (row->reversed_p)
13719 {
13720 /* In R2L rows that don't display text, put the cursor on the
13721 rightmost glyph. Case in point: an empty last line that is
13722 part of an R2L paragraph. */
13723 cursor = end - 1;
13724 /* Avoid placing the cursor on the last glyph of the row, where
13725 on terminal frames we hold the vertical border between
13726 adjacent windows. */
13727 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13728 && !WINDOW_RIGHTMOST_P (w)
13729 && cursor == row->glyphs[LAST_AREA] - 1)
13730 cursor--;
13731 x = -1; /* will be computed below, at label compute_x */
13732 }
13733
13734 /* Step 1: Try to find the glyph whose character position
13735 corresponds to point. If that's not possible, find 2 glyphs
13736 whose character positions are the closest to point, one before
13737 point, the other after it. */
13738 if (!row->reversed_p)
13739 while (/* not marched to end of glyph row */
13740 glyph < end
13741 /* glyph was not inserted by redisplay for internal purposes */
13742 && !INTEGERP (glyph->object))
13743 {
13744 if (BUFFERP (glyph->object))
13745 {
13746 ptrdiff_t dpos = glyph->charpos - pt_old;
13747
13748 if (glyph->charpos > bpos_max)
13749 bpos_max = glyph->charpos;
13750 if (glyph->charpos < bpos_min)
13751 bpos_min = glyph->charpos;
13752 if (!glyph->avoid_cursor_p)
13753 {
13754 /* If we hit point, we've found the glyph on which to
13755 display the cursor. */
13756 if (dpos == 0)
13757 {
13758 match_with_avoid_cursor = 0;
13759 break;
13760 }
13761 /* See if we've found a better approximation to
13762 POS_BEFORE or to POS_AFTER. Note that we want the
13763 first (leftmost) glyph of all those that are the
13764 closest from below, and the last (rightmost) of all
13765 those from above. */
13766 if (0 > dpos && dpos > pos_before - pt_old)
13767 {
13768 pos_before = glyph->charpos;
13769 glyph_before = glyph;
13770 }
13771 else if (0 < dpos && dpos <= pos_after - pt_old)
13772 {
13773 pos_after = glyph->charpos;
13774 glyph_after = glyph;
13775 }
13776 }
13777 else if (dpos == 0)
13778 match_with_avoid_cursor = 1;
13779 }
13780 else if (STRINGP (glyph->object))
13781 {
13782 Lisp_Object chprop;
13783 ptrdiff_t glyph_pos = glyph->charpos;
13784
13785 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13786 glyph->object);
13787 if (INTEGERP (chprop))
13788 {
13789 bpos_covered = bpos_max + XINT (chprop);
13790 /* If the `cursor' property covers buffer positions up
13791 to and including point, we should display cursor on
13792 this glyph. Note that overlays and text properties
13793 with string values stop bidi reordering, so every
13794 buffer position to the left of the string is always
13795 smaller than any position to the right of the
13796 string. Therefore, if a `cursor' property on one
13797 of the string's characters has an integer value, we
13798 will break out of the loop below _before_ we get to
13799 the position match above. IOW, integer values of
13800 the `cursor' property override the "exact match for
13801 point" strategy of positioning the cursor. */
13802 /* Implementation note: bpos_max == pt_old when, e.g.,
13803 we are in an empty line, where bpos_max is set to
13804 MATRIX_ROW_START_CHARPOS, see above. */
13805 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13806 {
13807 cursor = glyph;
13808 break;
13809 }
13810 }
13811
13812 string_seen = 1;
13813 }
13814 x += glyph->pixel_width;
13815 ++glyph;
13816 }
13817 else if (glyph > end) /* row is reversed */
13818 while (!INTEGERP (glyph->object))
13819 {
13820 if (BUFFERP (glyph->object))
13821 {
13822 ptrdiff_t dpos = glyph->charpos - pt_old;
13823
13824 if (glyph->charpos > bpos_max)
13825 bpos_max = glyph->charpos;
13826 if (glyph->charpos < bpos_min)
13827 bpos_min = glyph->charpos;
13828 if (!glyph->avoid_cursor_p)
13829 {
13830 if (dpos == 0)
13831 {
13832 match_with_avoid_cursor = 0;
13833 break;
13834 }
13835 if (0 > dpos && dpos > pos_before - pt_old)
13836 {
13837 pos_before = glyph->charpos;
13838 glyph_before = glyph;
13839 }
13840 else if (0 < dpos && dpos <= pos_after - pt_old)
13841 {
13842 pos_after = glyph->charpos;
13843 glyph_after = glyph;
13844 }
13845 }
13846 else if (dpos == 0)
13847 match_with_avoid_cursor = 1;
13848 }
13849 else if (STRINGP (glyph->object))
13850 {
13851 Lisp_Object chprop;
13852 ptrdiff_t glyph_pos = glyph->charpos;
13853
13854 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13855 glyph->object);
13856 if (INTEGERP (chprop))
13857 {
13858 bpos_covered = bpos_max + XINT (chprop);
13859 /* If the `cursor' property covers buffer positions up
13860 to and including point, we should display cursor on
13861 this glyph. */
13862 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13863 {
13864 cursor = glyph;
13865 break;
13866 }
13867 }
13868 string_seen = 1;
13869 }
13870 --glyph;
13871 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13872 {
13873 x--; /* can't use any pixel_width */
13874 break;
13875 }
13876 x -= glyph->pixel_width;
13877 }
13878
13879 /* Step 2: If we didn't find an exact match for point, we need to
13880 look for a proper place to put the cursor among glyphs between
13881 GLYPH_BEFORE and GLYPH_AFTER. */
13882 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13883 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13884 && bpos_covered < pt_old)
13885 {
13886 /* An empty line has a single glyph whose OBJECT is zero and
13887 whose CHARPOS is the position of a newline on that line.
13888 Note that on a TTY, there are more glyphs after that, which
13889 were produced by extend_face_to_end_of_line, but their
13890 CHARPOS is zero or negative. */
13891 int empty_line_p =
13892 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13893 && INTEGERP (glyph->object) && glyph->charpos > 0;
13894
13895 if (row->ends_in_ellipsis_p && pos_after == last_pos)
13896 {
13897 ptrdiff_t ellipsis_pos;
13898
13899 /* Scan back over the ellipsis glyphs. */
13900 if (!row->reversed_p)
13901 {
13902 ellipsis_pos = (glyph - 1)->charpos;
13903 while (glyph > row->glyphs[TEXT_AREA]
13904 && (glyph - 1)->charpos == ellipsis_pos)
13905 glyph--, x -= glyph->pixel_width;
13906 /* That loop always goes one position too far, including
13907 the glyph before the ellipsis. So scan forward over
13908 that one. */
13909 x += glyph->pixel_width;
13910 glyph++;
13911 }
13912 else /* row is reversed */
13913 {
13914 ellipsis_pos = (glyph + 1)->charpos;
13915 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
13916 && (glyph + 1)->charpos == ellipsis_pos)
13917 glyph++, x += glyph->pixel_width;
13918 x -= glyph->pixel_width;
13919 glyph--;
13920 }
13921 }
13922 else if (match_with_avoid_cursor)
13923 {
13924 cursor = glyph_after;
13925 x = -1;
13926 }
13927 else if (string_seen)
13928 {
13929 int incr = row->reversed_p ? -1 : +1;
13930
13931 /* Need to find the glyph that came out of a string which is
13932 present at point. That glyph is somewhere between
13933 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
13934 positioned between POS_BEFORE and POS_AFTER in the
13935 buffer. */
13936 struct glyph *start, *stop;
13937 ptrdiff_t pos = pos_before;
13938
13939 x = -1;
13940
13941 /* If the row ends in a newline from a display string,
13942 reordering could have moved the glyphs belonging to the
13943 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
13944 in this case we extend the search to the last glyph in
13945 the row that was not inserted by redisplay. */
13946 if (row->ends_in_newline_from_string_p)
13947 {
13948 glyph_after = end;
13949 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13950 }
13951
13952 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
13953 correspond to POS_BEFORE and POS_AFTER, respectively. We
13954 need START and STOP in the order that corresponds to the
13955 row's direction as given by its reversed_p flag. If the
13956 directionality of characters between POS_BEFORE and
13957 POS_AFTER is the opposite of the row's base direction,
13958 these characters will have been reordered for display,
13959 and we need to reverse START and STOP. */
13960 if (!row->reversed_p)
13961 {
13962 start = min (glyph_before, glyph_after);
13963 stop = max (glyph_before, glyph_after);
13964 }
13965 else
13966 {
13967 start = max (glyph_before, glyph_after);
13968 stop = min (glyph_before, glyph_after);
13969 }
13970 for (glyph = start + incr;
13971 row->reversed_p ? glyph > stop : glyph < stop; )
13972 {
13973
13974 /* Any glyphs that come from the buffer are here because
13975 of bidi reordering. Skip them, and only pay
13976 attention to glyphs that came from some string. */
13977 if (STRINGP (glyph->object))
13978 {
13979 Lisp_Object str;
13980 ptrdiff_t tem;
13981 /* If the display property covers the newline, we
13982 need to search for it one position farther. */
13983 ptrdiff_t lim = pos_after
13984 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
13985
13986 string_from_text_prop = 0;
13987 str = glyph->object;
13988 tem = string_buffer_position_lim (str, pos, lim, 0);
13989 if (tem == 0 /* from overlay */
13990 || pos <= tem)
13991 {
13992 /* If the string from which this glyph came is
13993 found in the buffer at point, then we've
13994 found the glyph we've been looking for. If
13995 it comes from an overlay (tem == 0), and it
13996 has the `cursor' property on one of its
13997 glyphs, record that glyph as a candidate for
13998 displaying the cursor. (As in the
13999 unidirectional version, we will display the
14000 cursor on the last candidate we find.) */
14001 if (tem == 0 || tem == pt_old)
14002 {
14003 /* The glyphs from this string could have
14004 been reordered. Find the one with the
14005 smallest string position. Or there could
14006 be a character in the string with the
14007 `cursor' property, which means display
14008 cursor on that character's glyph. */
14009 ptrdiff_t strpos = glyph->charpos;
14010
14011 if (tem)
14012 {
14013 cursor = glyph;
14014 string_from_text_prop = 1;
14015 }
14016 for ( ;
14017 (row->reversed_p ? glyph > stop : glyph < stop)
14018 && EQ (glyph->object, str);
14019 glyph += incr)
14020 {
14021 Lisp_Object cprop;
14022 ptrdiff_t gpos = glyph->charpos;
14023
14024 cprop = Fget_char_property (make_number (gpos),
14025 Qcursor,
14026 glyph->object);
14027 if (!NILP (cprop))
14028 {
14029 cursor = glyph;
14030 break;
14031 }
14032 if (tem && glyph->charpos < strpos)
14033 {
14034 strpos = glyph->charpos;
14035 cursor = glyph;
14036 }
14037 }
14038
14039 if (tem == pt_old)
14040 goto compute_x;
14041 }
14042 if (tem)
14043 pos = tem + 1; /* don't find previous instances */
14044 }
14045 /* This string is not what we want; skip all of the
14046 glyphs that came from it. */
14047 while ((row->reversed_p ? glyph > stop : glyph < stop)
14048 && EQ (glyph->object, str))
14049 glyph += incr;
14050 }
14051 else
14052 glyph += incr;
14053 }
14054
14055 /* If we reached the end of the line, and END was from a string,
14056 the cursor is not on this line. */
14057 if (cursor == NULL
14058 && (row->reversed_p ? glyph <= end : glyph >= end)
14059 && STRINGP (end->object)
14060 && row->continued_p)
14061 return 0;
14062 }
14063 /* A truncated row may not include PT among its character positions.
14064 Setting the cursor inside the scroll margin will trigger
14065 recalculation of hscroll in hscroll_window_tree. But if a
14066 display string covers point, defer to the string-handling
14067 code below to figure this out. */
14068 else if (row->truncated_on_left_p && pt_old < bpos_min)
14069 {
14070 cursor = glyph_before;
14071 x = -1;
14072 }
14073 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14074 /* Zero-width characters produce no glyphs. */
14075 || (!empty_line_p
14076 && (row->reversed_p
14077 ? glyph_after > glyphs_end
14078 : glyph_after < glyphs_end)))
14079 {
14080 cursor = glyph_after;
14081 x = -1;
14082 }
14083 }
14084
14085 compute_x:
14086 if (cursor != NULL)
14087 glyph = cursor;
14088 if (x < 0)
14089 {
14090 struct glyph *g;
14091
14092 /* Need to compute x that corresponds to GLYPH. */
14093 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14094 {
14095 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14096 abort ();
14097 x += g->pixel_width;
14098 }
14099 }
14100
14101 /* ROW could be part of a continued line, which, under bidi
14102 reordering, might have other rows whose start and end charpos
14103 occlude point. Only set w->cursor if we found a better
14104 approximation to the cursor position than we have from previously
14105 examined candidate rows belonging to the same continued line. */
14106 if (/* we already have a candidate row */
14107 w->cursor.vpos >= 0
14108 /* that candidate is not the row we are processing */
14109 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14110 /* Make sure cursor.vpos specifies a row whose start and end
14111 charpos occlude point, and it is valid candidate for being a
14112 cursor-row. This is because some callers of this function
14113 leave cursor.vpos at the row where the cursor was displayed
14114 during the last redisplay cycle. */
14115 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14116 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14117 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14118 {
14119 struct glyph *g1 =
14120 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14121
14122 /* Don't consider glyphs that are outside TEXT_AREA. */
14123 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14124 return 0;
14125 /* Keep the candidate whose buffer position is the closest to
14126 point or has the `cursor' property. */
14127 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14128 w->cursor.hpos >= 0
14129 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14130 && ((BUFFERP (g1->object)
14131 && (g1->charpos == pt_old /* an exact match always wins */
14132 || (BUFFERP (glyph->object)
14133 && eabs (g1->charpos - pt_old)
14134 < eabs (glyph->charpos - pt_old))))
14135 /* previous candidate is a glyph from a string that has
14136 a non-nil `cursor' property */
14137 || (STRINGP (g1->object)
14138 && (!NILP (Fget_char_property (make_number (g1->charpos),
14139 Qcursor, g1->object))
14140 /* previous candidate is from the same display
14141 string as this one, and the display string
14142 came from a text property */
14143 || (EQ (g1->object, glyph->object)
14144 && string_from_text_prop)
14145 /* this candidate is from newline and its
14146 position is not an exact match */
14147 || (INTEGERP (glyph->object)
14148 && glyph->charpos != pt_old)))))
14149 return 0;
14150 /* If this candidate gives an exact match, use that. */
14151 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14152 /* If this candidate is a glyph created for the
14153 terminating newline of a line, and point is on that
14154 newline, it wins because it's an exact match. */
14155 || (!row->continued_p
14156 && INTEGERP (glyph->object)
14157 && glyph->charpos == 0
14158 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14159 /* Otherwise, keep the candidate that comes from a row
14160 spanning less buffer positions. This may win when one or
14161 both candidate positions are on glyphs that came from
14162 display strings, for which we cannot compare buffer
14163 positions. */
14164 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14165 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14166 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14167 return 0;
14168 }
14169 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14170 w->cursor.x = x;
14171 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14172 w->cursor.y = row->y + dy;
14173
14174 if (w == XWINDOW (selected_window))
14175 {
14176 if (!row->continued_p
14177 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14178 && row->x == 0)
14179 {
14180 this_line_buffer = XBUFFER (w->buffer);
14181
14182 CHARPOS (this_line_start_pos)
14183 = MATRIX_ROW_START_CHARPOS (row) + delta;
14184 BYTEPOS (this_line_start_pos)
14185 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14186
14187 CHARPOS (this_line_end_pos)
14188 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14189 BYTEPOS (this_line_end_pos)
14190 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14191
14192 this_line_y = w->cursor.y;
14193 this_line_pixel_height = row->height;
14194 this_line_vpos = w->cursor.vpos;
14195 this_line_start_x = row->x;
14196 }
14197 else
14198 CHARPOS (this_line_start_pos) = 0;
14199 }
14200
14201 return 1;
14202 }
14203
14204
14205 /* Run window scroll functions, if any, for WINDOW with new window
14206 start STARTP. Sets the window start of WINDOW to that position.
14207
14208 We assume that the window's buffer is really current. */
14209
14210 static inline struct text_pos
14211 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14212 {
14213 struct window *w = XWINDOW (window);
14214 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14215
14216 if (current_buffer != XBUFFER (w->buffer))
14217 abort ();
14218
14219 if (!NILP (Vwindow_scroll_functions))
14220 {
14221 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14222 make_number (CHARPOS (startp)));
14223 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14224 /* In case the hook functions switch buffers. */
14225 if (current_buffer != XBUFFER (w->buffer))
14226 set_buffer_internal_1 (XBUFFER (w->buffer));
14227 }
14228
14229 return startp;
14230 }
14231
14232
14233 /* Make sure the line containing the cursor is fully visible.
14234 A value of 1 means there is nothing to be done.
14235 (Either the line is fully visible, or it cannot be made so,
14236 or we cannot tell.)
14237
14238 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14239 is higher than window.
14240
14241 A value of 0 means the caller should do scrolling
14242 as if point had gone off the screen. */
14243
14244 static int
14245 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14246 {
14247 struct glyph_matrix *matrix;
14248 struct glyph_row *row;
14249 int window_height;
14250
14251 if (!make_cursor_line_fully_visible_p)
14252 return 1;
14253
14254 /* It's not always possible to find the cursor, e.g, when a window
14255 is full of overlay strings. Don't do anything in that case. */
14256 if (w->cursor.vpos < 0)
14257 return 1;
14258
14259 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14260 row = MATRIX_ROW (matrix, w->cursor.vpos);
14261
14262 /* If the cursor row is not partially visible, there's nothing to do. */
14263 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14264 return 1;
14265
14266 /* If the row the cursor is in is taller than the window's height,
14267 it's not clear what to do, so do nothing. */
14268 window_height = window_box_height (w);
14269 if (row->height >= window_height)
14270 {
14271 if (!force_p || MINI_WINDOW_P (w)
14272 || w->vscroll || w->cursor.vpos == 0)
14273 return 1;
14274 }
14275 return 0;
14276 }
14277
14278
14279 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14280 non-zero means only WINDOW is redisplayed in redisplay_internal.
14281 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14282 in redisplay_window to bring a partially visible line into view in
14283 the case that only the cursor has moved.
14284
14285 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14286 last screen line's vertical height extends past the end of the screen.
14287
14288 Value is
14289
14290 1 if scrolling succeeded
14291
14292 0 if scrolling didn't find point.
14293
14294 -1 if new fonts have been loaded so that we must interrupt
14295 redisplay, adjust glyph matrices, and try again. */
14296
14297 enum
14298 {
14299 SCROLLING_SUCCESS,
14300 SCROLLING_FAILED,
14301 SCROLLING_NEED_LARGER_MATRICES
14302 };
14303
14304 /* If scroll-conservatively is more than this, never recenter.
14305
14306 If you change this, don't forget to update the doc string of
14307 `scroll-conservatively' and the Emacs manual. */
14308 #define SCROLL_LIMIT 100
14309
14310 static int
14311 try_scrolling (Lisp_Object window, int just_this_one_p,
14312 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14313 int temp_scroll_step, int last_line_misfit)
14314 {
14315 struct window *w = XWINDOW (window);
14316 struct frame *f = XFRAME (w->frame);
14317 struct text_pos pos, startp;
14318 struct it it;
14319 int this_scroll_margin, scroll_max, rc, height;
14320 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14321 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14322 Lisp_Object aggressive;
14323 /* We will never try scrolling more than this number of lines. */
14324 int scroll_limit = SCROLL_LIMIT;
14325
14326 #if GLYPH_DEBUG
14327 debug_method_add (w, "try_scrolling");
14328 #endif
14329
14330 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14331
14332 /* Compute scroll margin height in pixels. We scroll when point is
14333 within this distance from the top or bottom of the window. */
14334 if (scroll_margin > 0)
14335 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14336 * FRAME_LINE_HEIGHT (f);
14337 else
14338 this_scroll_margin = 0;
14339
14340 /* Force arg_scroll_conservatively to have a reasonable value, to
14341 avoid scrolling too far away with slow move_it_* functions. Note
14342 that the user can supply scroll-conservatively equal to
14343 `most-positive-fixnum', which can be larger than INT_MAX. */
14344 if (arg_scroll_conservatively > scroll_limit)
14345 {
14346 arg_scroll_conservatively = scroll_limit + 1;
14347 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14348 }
14349 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14350 /* Compute how much we should try to scroll maximally to bring
14351 point into view. */
14352 scroll_max = (max (scroll_step,
14353 max (arg_scroll_conservatively, temp_scroll_step))
14354 * FRAME_LINE_HEIGHT (f));
14355 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14356 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14357 /* We're trying to scroll because of aggressive scrolling but no
14358 scroll_step is set. Choose an arbitrary one. */
14359 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14360 else
14361 scroll_max = 0;
14362
14363 too_near_end:
14364
14365 /* Decide whether to scroll down. */
14366 if (PT > CHARPOS (startp))
14367 {
14368 int scroll_margin_y;
14369
14370 /* Compute the pixel ypos of the scroll margin, then move IT to
14371 either that ypos or PT, whichever comes first. */
14372 start_display (&it, w, startp);
14373 scroll_margin_y = it.last_visible_y - this_scroll_margin
14374 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14375 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14376 (MOVE_TO_POS | MOVE_TO_Y));
14377
14378 if (PT > CHARPOS (it.current.pos))
14379 {
14380 int y0 = line_bottom_y (&it);
14381 /* Compute how many pixels below window bottom to stop searching
14382 for PT. This avoids costly search for PT that is far away if
14383 the user limited scrolling by a small number of lines, but
14384 always finds PT if scroll_conservatively is set to a large
14385 number, such as most-positive-fixnum. */
14386 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14387 int y_to_move = it.last_visible_y + slack;
14388
14389 /* Compute the distance from the scroll margin to PT or to
14390 the scroll limit, whichever comes first. This should
14391 include the height of the cursor line, to make that line
14392 fully visible. */
14393 move_it_to (&it, PT, -1, y_to_move,
14394 -1, MOVE_TO_POS | MOVE_TO_Y);
14395 dy = line_bottom_y (&it) - y0;
14396
14397 if (dy > scroll_max)
14398 return SCROLLING_FAILED;
14399
14400 if (dy > 0)
14401 scroll_down_p = 1;
14402 }
14403 }
14404
14405 if (scroll_down_p)
14406 {
14407 /* Point is in or below the bottom scroll margin, so move the
14408 window start down. If scrolling conservatively, move it just
14409 enough down to make point visible. If scroll_step is set,
14410 move it down by scroll_step. */
14411 if (arg_scroll_conservatively)
14412 amount_to_scroll
14413 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14414 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14415 else if (scroll_step || temp_scroll_step)
14416 amount_to_scroll = scroll_max;
14417 else
14418 {
14419 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14420 height = WINDOW_BOX_TEXT_HEIGHT (w);
14421 if (NUMBERP (aggressive))
14422 {
14423 double float_amount = XFLOATINT (aggressive) * height;
14424 amount_to_scroll = float_amount;
14425 if (amount_to_scroll == 0 && float_amount > 0)
14426 amount_to_scroll = 1;
14427 /* Don't let point enter the scroll margin near top of
14428 the window. */
14429 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14430 amount_to_scroll = height - 2*this_scroll_margin + dy;
14431 }
14432 }
14433
14434 if (amount_to_scroll <= 0)
14435 return SCROLLING_FAILED;
14436
14437 start_display (&it, w, startp);
14438 if (arg_scroll_conservatively <= scroll_limit)
14439 move_it_vertically (&it, amount_to_scroll);
14440 else
14441 {
14442 /* Extra precision for users who set scroll-conservatively
14443 to a large number: make sure the amount we scroll
14444 the window start is never less than amount_to_scroll,
14445 which was computed as distance from window bottom to
14446 point. This matters when lines at window top and lines
14447 below window bottom have different height. */
14448 struct it it1;
14449 void *it1data = NULL;
14450 /* We use a temporary it1 because line_bottom_y can modify
14451 its argument, if it moves one line down; see there. */
14452 int start_y;
14453
14454 SAVE_IT (it1, it, it1data);
14455 start_y = line_bottom_y (&it1);
14456 do {
14457 RESTORE_IT (&it, &it, it1data);
14458 move_it_by_lines (&it, 1);
14459 SAVE_IT (it1, it, it1data);
14460 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14461 }
14462
14463 /* If STARTP is unchanged, move it down another screen line. */
14464 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14465 move_it_by_lines (&it, 1);
14466 startp = it.current.pos;
14467 }
14468 else
14469 {
14470 struct text_pos scroll_margin_pos = startp;
14471
14472 /* See if point is inside the scroll margin at the top of the
14473 window. */
14474 if (this_scroll_margin)
14475 {
14476 start_display (&it, w, startp);
14477 move_it_vertically (&it, this_scroll_margin);
14478 scroll_margin_pos = it.current.pos;
14479 }
14480
14481 if (PT < CHARPOS (scroll_margin_pos))
14482 {
14483 /* Point is in the scroll margin at the top of the window or
14484 above what is displayed in the window. */
14485 int y0, y_to_move;
14486
14487 /* Compute the vertical distance from PT to the scroll
14488 margin position. Move as far as scroll_max allows, or
14489 one screenful, or 10 screen lines, whichever is largest.
14490 Give up if distance is greater than scroll_max. */
14491 SET_TEXT_POS (pos, PT, PT_BYTE);
14492 start_display (&it, w, pos);
14493 y0 = it.current_y;
14494 y_to_move = max (it.last_visible_y,
14495 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14496 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14497 y_to_move, -1,
14498 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14499 dy = it.current_y - y0;
14500 if (dy > scroll_max)
14501 return SCROLLING_FAILED;
14502
14503 /* Compute new window start. */
14504 start_display (&it, w, startp);
14505
14506 if (arg_scroll_conservatively)
14507 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14508 max (scroll_step, temp_scroll_step));
14509 else if (scroll_step || temp_scroll_step)
14510 amount_to_scroll = scroll_max;
14511 else
14512 {
14513 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14514 height = WINDOW_BOX_TEXT_HEIGHT (w);
14515 if (NUMBERP (aggressive))
14516 {
14517 double float_amount = XFLOATINT (aggressive) * height;
14518 amount_to_scroll = float_amount;
14519 if (amount_to_scroll == 0 && float_amount > 0)
14520 amount_to_scroll = 1;
14521 amount_to_scroll -=
14522 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14523 /* Don't let point enter the scroll margin near
14524 bottom of the window. */
14525 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14526 amount_to_scroll = height - 2*this_scroll_margin + dy;
14527 }
14528 }
14529
14530 if (amount_to_scroll <= 0)
14531 return SCROLLING_FAILED;
14532
14533 move_it_vertically_backward (&it, amount_to_scroll);
14534 startp = it.current.pos;
14535 }
14536 }
14537
14538 /* Run window scroll functions. */
14539 startp = run_window_scroll_functions (window, startp);
14540
14541 /* Display the window. Give up if new fonts are loaded, or if point
14542 doesn't appear. */
14543 if (!try_window (window, startp, 0))
14544 rc = SCROLLING_NEED_LARGER_MATRICES;
14545 else if (w->cursor.vpos < 0)
14546 {
14547 clear_glyph_matrix (w->desired_matrix);
14548 rc = SCROLLING_FAILED;
14549 }
14550 else
14551 {
14552 /* Maybe forget recorded base line for line number display. */
14553 if (!just_this_one_p
14554 || current_buffer->clip_changed
14555 || BEG_UNCHANGED < CHARPOS (startp))
14556 w->base_line_number = Qnil;
14557
14558 /* If cursor ends up on a partially visible line,
14559 treat that as being off the bottom of the screen. */
14560 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14561 /* It's possible that the cursor is on the first line of the
14562 buffer, which is partially obscured due to a vscroll
14563 (Bug#7537). In that case, avoid looping forever . */
14564 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14565 {
14566 clear_glyph_matrix (w->desired_matrix);
14567 ++extra_scroll_margin_lines;
14568 goto too_near_end;
14569 }
14570 rc = SCROLLING_SUCCESS;
14571 }
14572
14573 return rc;
14574 }
14575
14576
14577 /* Compute a suitable window start for window W if display of W starts
14578 on a continuation line. Value is non-zero if a new window start
14579 was computed.
14580
14581 The new window start will be computed, based on W's width, starting
14582 from the start of the continued line. It is the start of the
14583 screen line with the minimum distance from the old start W->start. */
14584
14585 static int
14586 compute_window_start_on_continuation_line (struct window *w)
14587 {
14588 struct text_pos pos, start_pos;
14589 int window_start_changed_p = 0;
14590
14591 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14592
14593 /* If window start is on a continuation line... Window start may be
14594 < BEGV in case there's invisible text at the start of the
14595 buffer (M-x rmail, for example). */
14596 if (CHARPOS (start_pos) > BEGV
14597 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14598 {
14599 struct it it;
14600 struct glyph_row *row;
14601
14602 /* Handle the case that the window start is out of range. */
14603 if (CHARPOS (start_pos) < BEGV)
14604 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14605 else if (CHARPOS (start_pos) > ZV)
14606 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14607
14608 /* Find the start of the continued line. This should be fast
14609 because scan_buffer is fast (newline cache). */
14610 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14611 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14612 row, DEFAULT_FACE_ID);
14613 reseat_at_previous_visible_line_start (&it);
14614
14615 /* If the line start is "too far" away from the window start,
14616 say it takes too much time to compute a new window start. */
14617 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14618 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14619 {
14620 int min_distance, distance;
14621
14622 /* Move forward by display lines to find the new window
14623 start. If window width was enlarged, the new start can
14624 be expected to be > the old start. If window width was
14625 decreased, the new window start will be < the old start.
14626 So, we're looking for the display line start with the
14627 minimum distance from the old window start. */
14628 pos = it.current.pos;
14629 min_distance = INFINITY;
14630 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14631 distance < min_distance)
14632 {
14633 min_distance = distance;
14634 pos = it.current.pos;
14635 move_it_by_lines (&it, 1);
14636 }
14637
14638 /* Set the window start there. */
14639 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14640 window_start_changed_p = 1;
14641 }
14642 }
14643
14644 return window_start_changed_p;
14645 }
14646
14647
14648 /* Try cursor movement in case text has not changed in window WINDOW,
14649 with window start STARTP. Value is
14650
14651 CURSOR_MOVEMENT_SUCCESS if successful
14652
14653 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14654
14655 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14656 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14657 we want to scroll as if scroll-step were set to 1. See the code.
14658
14659 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14660 which case we have to abort this redisplay, and adjust matrices
14661 first. */
14662
14663 enum
14664 {
14665 CURSOR_MOVEMENT_SUCCESS,
14666 CURSOR_MOVEMENT_CANNOT_BE_USED,
14667 CURSOR_MOVEMENT_MUST_SCROLL,
14668 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14669 };
14670
14671 static int
14672 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14673 {
14674 struct window *w = XWINDOW (window);
14675 struct frame *f = XFRAME (w->frame);
14676 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14677
14678 #if GLYPH_DEBUG
14679 if (inhibit_try_cursor_movement)
14680 return rc;
14681 #endif
14682
14683 /* Handle case where text has not changed, only point, and it has
14684 not moved off the frame. */
14685 if (/* Point may be in this window. */
14686 PT >= CHARPOS (startp)
14687 /* Selective display hasn't changed. */
14688 && !current_buffer->clip_changed
14689 /* Function force-mode-line-update is used to force a thorough
14690 redisplay. It sets either windows_or_buffers_changed or
14691 update_mode_lines. So don't take a shortcut here for these
14692 cases. */
14693 && !update_mode_lines
14694 && !windows_or_buffers_changed
14695 && !cursor_type_changed
14696 /* Can't use this case if highlighting a region. When a
14697 region exists, cursor movement has to do more than just
14698 set the cursor. */
14699 && !(!NILP (Vtransient_mark_mode)
14700 && !NILP (BVAR (current_buffer, mark_active)))
14701 && NILP (w->region_showing)
14702 && NILP (Vshow_trailing_whitespace)
14703 /* Right after splitting windows, last_point may be nil. */
14704 && INTEGERP (w->last_point)
14705 /* This code is not used for mini-buffer for the sake of the case
14706 of redisplaying to replace an echo area message; since in
14707 that case the mini-buffer contents per se are usually
14708 unchanged. This code is of no real use in the mini-buffer
14709 since the handling of this_line_start_pos, etc., in redisplay
14710 handles the same cases. */
14711 && !EQ (window, minibuf_window)
14712 /* When splitting windows or for new windows, it happens that
14713 redisplay is called with a nil window_end_vpos or one being
14714 larger than the window. This should really be fixed in
14715 window.c. I don't have this on my list, now, so we do
14716 approximately the same as the old redisplay code. --gerd. */
14717 && INTEGERP (w->window_end_vpos)
14718 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14719 && (FRAME_WINDOW_P (f)
14720 || !overlay_arrow_in_current_buffer_p ()))
14721 {
14722 int this_scroll_margin, top_scroll_margin;
14723 struct glyph_row *row = NULL;
14724
14725 #if GLYPH_DEBUG
14726 debug_method_add (w, "cursor movement");
14727 #endif
14728
14729 /* Scroll if point within this distance from the top or bottom
14730 of the window. This is a pixel value. */
14731 if (scroll_margin > 0)
14732 {
14733 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14734 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14735 }
14736 else
14737 this_scroll_margin = 0;
14738
14739 top_scroll_margin = this_scroll_margin;
14740 if (WINDOW_WANTS_HEADER_LINE_P (w))
14741 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14742
14743 /* Start with the row the cursor was displayed during the last
14744 not paused redisplay. Give up if that row is not valid. */
14745 if (w->last_cursor.vpos < 0
14746 || w->last_cursor.vpos >= w->current_matrix->nrows)
14747 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14748 else
14749 {
14750 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14751 if (row->mode_line_p)
14752 ++row;
14753 if (!row->enabled_p)
14754 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14755 }
14756
14757 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14758 {
14759 int scroll_p = 0, must_scroll = 0;
14760 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14761
14762 if (PT > XFASTINT (w->last_point))
14763 {
14764 /* Point has moved forward. */
14765 while (MATRIX_ROW_END_CHARPOS (row) < PT
14766 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14767 {
14768 xassert (row->enabled_p);
14769 ++row;
14770 }
14771
14772 /* If the end position of a row equals the start
14773 position of the next row, and PT is at that position,
14774 we would rather display cursor in the next line. */
14775 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14776 && MATRIX_ROW_END_CHARPOS (row) == PT
14777 && row < w->current_matrix->rows
14778 + w->current_matrix->nrows - 1
14779 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14780 && !cursor_row_p (row))
14781 ++row;
14782
14783 /* If within the scroll margin, scroll. Note that
14784 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14785 the next line would be drawn, and that
14786 this_scroll_margin can be zero. */
14787 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14788 || PT > MATRIX_ROW_END_CHARPOS (row)
14789 /* Line is completely visible last line in window
14790 and PT is to be set in the next line. */
14791 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14792 && PT == MATRIX_ROW_END_CHARPOS (row)
14793 && !row->ends_at_zv_p
14794 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14795 scroll_p = 1;
14796 }
14797 else if (PT < XFASTINT (w->last_point))
14798 {
14799 /* Cursor has to be moved backward. Note that PT >=
14800 CHARPOS (startp) because of the outer if-statement. */
14801 while (!row->mode_line_p
14802 && (MATRIX_ROW_START_CHARPOS (row) > PT
14803 || (MATRIX_ROW_START_CHARPOS (row) == PT
14804 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14805 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14806 row > w->current_matrix->rows
14807 && (row-1)->ends_in_newline_from_string_p))))
14808 && (row->y > top_scroll_margin
14809 || CHARPOS (startp) == BEGV))
14810 {
14811 xassert (row->enabled_p);
14812 --row;
14813 }
14814
14815 /* Consider the following case: Window starts at BEGV,
14816 there is invisible, intangible text at BEGV, so that
14817 display starts at some point START > BEGV. It can
14818 happen that we are called with PT somewhere between
14819 BEGV and START. Try to handle that case. */
14820 if (row < w->current_matrix->rows
14821 || row->mode_line_p)
14822 {
14823 row = w->current_matrix->rows;
14824 if (row->mode_line_p)
14825 ++row;
14826 }
14827
14828 /* Due to newlines in overlay strings, we may have to
14829 skip forward over overlay strings. */
14830 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14831 && MATRIX_ROW_END_CHARPOS (row) == PT
14832 && !cursor_row_p (row))
14833 ++row;
14834
14835 /* If within the scroll margin, scroll. */
14836 if (row->y < top_scroll_margin
14837 && CHARPOS (startp) != BEGV)
14838 scroll_p = 1;
14839 }
14840 else
14841 {
14842 /* Cursor did not move. So don't scroll even if cursor line
14843 is partially visible, as it was so before. */
14844 rc = CURSOR_MOVEMENT_SUCCESS;
14845 }
14846
14847 if (PT < MATRIX_ROW_START_CHARPOS (row)
14848 || PT > MATRIX_ROW_END_CHARPOS (row))
14849 {
14850 /* if PT is not in the glyph row, give up. */
14851 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14852 must_scroll = 1;
14853 }
14854 else if (rc != CURSOR_MOVEMENT_SUCCESS
14855 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14856 {
14857 /* If rows are bidi-reordered and point moved, back up
14858 until we find a row that does not belong to a
14859 continuation line. This is because we must consider
14860 all rows of a continued line as candidates for the
14861 new cursor positioning, since row start and end
14862 positions change non-linearly with vertical position
14863 in such rows. */
14864 /* FIXME: Revisit this when glyph ``spilling'' in
14865 continuation lines' rows is implemented for
14866 bidi-reordered rows. */
14867 while (MATRIX_ROW_CONTINUATION_LINE_P (row))
14868 {
14869 /* If we hit the beginning of the displayed portion
14870 without finding the first row of a continued
14871 line, give up. */
14872 if (row <= w->current_matrix->rows)
14873 {
14874 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14875 break;
14876 }
14877 xassert (row->enabled_p);
14878 --row;
14879 }
14880 }
14881 if (must_scroll)
14882 ;
14883 else if (rc != CURSOR_MOVEMENT_SUCCESS
14884 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
14885 && make_cursor_line_fully_visible_p)
14886 {
14887 if (PT == MATRIX_ROW_END_CHARPOS (row)
14888 && !row->ends_at_zv_p
14889 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
14890 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14891 else if (row->height > window_box_height (w))
14892 {
14893 /* If we end up in a partially visible line, let's
14894 make it fully visible, except when it's taller
14895 than the window, in which case we can't do much
14896 about it. */
14897 *scroll_step = 1;
14898 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14899 }
14900 else
14901 {
14902 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
14903 if (!cursor_row_fully_visible_p (w, 0, 1))
14904 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14905 else
14906 rc = CURSOR_MOVEMENT_SUCCESS;
14907 }
14908 }
14909 else if (scroll_p)
14910 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14911 else if (rc != CURSOR_MOVEMENT_SUCCESS
14912 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
14913 {
14914 /* With bidi-reordered rows, there could be more than
14915 one candidate row whose start and end positions
14916 occlude point. We need to let set_cursor_from_row
14917 find the best candidate. */
14918 /* FIXME: Revisit this when glyph ``spilling'' in
14919 continuation lines' rows is implemented for
14920 bidi-reordered rows. */
14921 int rv = 0;
14922
14923 do
14924 {
14925 int at_zv_p = 0, exact_match_p = 0;
14926
14927 if (MATRIX_ROW_START_CHARPOS (row) <= PT
14928 && PT <= MATRIX_ROW_END_CHARPOS (row)
14929 && cursor_row_p (row))
14930 rv |= set_cursor_from_row (w, row, w->current_matrix,
14931 0, 0, 0, 0);
14932 /* As soon as we've found the exact match for point,
14933 or the first suitable row whose ends_at_zv_p flag
14934 is set, we are done. */
14935 at_zv_p =
14936 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
14937 if (rv && !at_zv_p
14938 && w->cursor.hpos >= 0
14939 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
14940 w->cursor.vpos))
14941 {
14942 struct glyph_row *candidate =
14943 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
14944 struct glyph *g =
14945 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
14946 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
14947
14948 exact_match_p =
14949 (BUFFERP (g->object) && g->charpos == PT)
14950 || (INTEGERP (g->object)
14951 && (g->charpos == PT
14952 || (g->charpos == 0 && endpos - 1 == PT)));
14953 }
14954 if (rv && (at_zv_p || exact_match_p))
14955 {
14956 rc = CURSOR_MOVEMENT_SUCCESS;
14957 break;
14958 }
14959 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
14960 break;
14961 ++row;
14962 }
14963 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
14964 || row->continued_p)
14965 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
14966 || (MATRIX_ROW_START_CHARPOS (row) == PT
14967 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
14968 /* If we didn't find any candidate rows, or exited the
14969 loop before all the candidates were examined, signal
14970 to the caller that this method failed. */
14971 if (rc != CURSOR_MOVEMENT_SUCCESS
14972 && !(rv
14973 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14974 && !row->continued_p))
14975 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14976 else if (rv)
14977 rc = CURSOR_MOVEMENT_SUCCESS;
14978 }
14979 else
14980 {
14981 do
14982 {
14983 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
14984 {
14985 rc = CURSOR_MOVEMENT_SUCCESS;
14986 break;
14987 }
14988 ++row;
14989 }
14990 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14991 && MATRIX_ROW_START_CHARPOS (row) == PT
14992 && cursor_row_p (row));
14993 }
14994 }
14995 }
14996
14997 return rc;
14998 }
14999
15000 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15001 static
15002 #endif
15003 void
15004 set_vertical_scroll_bar (struct window *w)
15005 {
15006 ptrdiff_t start, end, whole;
15007
15008 /* Calculate the start and end positions for the current window.
15009 At some point, it would be nice to choose between scrollbars
15010 which reflect the whole buffer size, with special markers
15011 indicating narrowing, and scrollbars which reflect only the
15012 visible region.
15013
15014 Note that mini-buffers sometimes aren't displaying any text. */
15015 if (!MINI_WINDOW_P (w)
15016 || (w == XWINDOW (minibuf_window)
15017 && NILP (echo_area_buffer[0])))
15018 {
15019 struct buffer *buf = XBUFFER (w->buffer);
15020 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15021 start = marker_position (w->start) - BUF_BEGV (buf);
15022 /* I don't think this is guaranteed to be right. For the
15023 moment, we'll pretend it is. */
15024 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15025
15026 if (end < start)
15027 end = start;
15028 if (whole < (end - start))
15029 whole = end - start;
15030 }
15031 else
15032 start = end = whole = 0;
15033
15034 /* Indicate what this scroll bar ought to be displaying now. */
15035 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15036 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15037 (w, end - start, whole, start);
15038 }
15039
15040
15041 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15042 selected_window is redisplayed.
15043
15044 We can return without actually redisplaying the window if
15045 fonts_changed_p is nonzero. In that case, redisplay_internal will
15046 retry. */
15047
15048 static void
15049 redisplay_window (Lisp_Object window, int just_this_one_p)
15050 {
15051 struct window *w = XWINDOW (window);
15052 struct frame *f = XFRAME (w->frame);
15053 struct buffer *buffer = XBUFFER (w->buffer);
15054 struct buffer *old = current_buffer;
15055 struct text_pos lpoint, opoint, startp;
15056 int update_mode_line;
15057 int tem;
15058 struct it it;
15059 /* Record it now because it's overwritten. */
15060 int current_matrix_up_to_date_p = 0;
15061 int used_current_matrix_p = 0;
15062 /* This is less strict than current_matrix_up_to_date_p.
15063 It indicates that the buffer contents and narrowing are unchanged. */
15064 int buffer_unchanged_p = 0;
15065 int temp_scroll_step = 0;
15066 ptrdiff_t count = SPECPDL_INDEX ();
15067 int rc;
15068 int centering_position = -1;
15069 int last_line_misfit = 0;
15070 ptrdiff_t beg_unchanged, end_unchanged;
15071
15072 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15073 opoint = lpoint;
15074
15075 /* W must be a leaf window here. */
15076 xassert (!NILP (w->buffer));
15077 #if GLYPH_DEBUG
15078 *w->desired_matrix->method = 0;
15079 #endif
15080
15081 restart:
15082 reconsider_clip_changes (w, buffer);
15083
15084 /* Has the mode line to be updated? */
15085 update_mode_line = (!NILP (w->update_mode_line)
15086 || update_mode_lines
15087 || buffer->clip_changed
15088 || buffer->prevent_redisplay_optimizations_p);
15089
15090 if (MINI_WINDOW_P (w))
15091 {
15092 if (w == XWINDOW (echo_area_window)
15093 && !NILP (echo_area_buffer[0]))
15094 {
15095 if (update_mode_line)
15096 /* We may have to update a tty frame's menu bar or a
15097 tool-bar. Example `M-x C-h C-h C-g'. */
15098 goto finish_menu_bars;
15099 else
15100 /* We've already displayed the echo area glyphs in this window. */
15101 goto finish_scroll_bars;
15102 }
15103 else if ((w != XWINDOW (minibuf_window)
15104 || minibuf_level == 0)
15105 /* When buffer is nonempty, redisplay window normally. */
15106 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15107 /* Quail displays non-mini buffers in minibuffer window.
15108 In that case, redisplay the window normally. */
15109 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15110 {
15111 /* W is a mini-buffer window, but it's not active, so clear
15112 it. */
15113 int yb = window_text_bottom_y (w);
15114 struct glyph_row *row;
15115 int y;
15116
15117 for (y = 0, row = w->desired_matrix->rows;
15118 y < yb;
15119 y += row->height, ++row)
15120 blank_row (w, row, y);
15121 goto finish_scroll_bars;
15122 }
15123
15124 clear_glyph_matrix (w->desired_matrix);
15125 }
15126
15127 /* Otherwise set up data on this window; select its buffer and point
15128 value. */
15129 /* Really select the buffer, for the sake of buffer-local
15130 variables. */
15131 set_buffer_internal_1 (XBUFFER (w->buffer));
15132
15133 current_matrix_up_to_date_p
15134 = (!NILP (w->window_end_valid)
15135 && !current_buffer->clip_changed
15136 && !current_buffer->prevent_redisplay_optimizations_p
15137 && XFASTINT (w->last_modified) >= MODIFF
15138 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15139
15140 /* Run the window-bottom-change-functions
15141 if it is possible that the text on the screen has changed
15142 (either due to modification of the text, or any other reason). */
15143 if (!current_matrix_up_to_date_p
15144 && !NILP (Vwindow_text_change_functions))
15145 {
15146 safe_run_hooks (Qwindow_text_change_functions);
15147 goto restart;
15148 }
15149
15150 beg_unchanged = BEG_UNCHANGED;
15151 end_unchanged = END_UNCHANGED;
15152
15153 SET_TEXT_POS (opoint, PT, PT_BYTE);
15154
15155 specbind (Qinhibit_point_motion_hooks, Qt);
15156
15157 buffer_unchanged_p
15158 = (!NILP (w->window_end_valid)
15159 && !current_buffer->clip_changed
15160 && XFASTINT (w->last_modified) >= MODIFF
15161 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15162
15163 /* When windows_or_buffers_changed is non-zero, we can't rely on
15164 the window end being valid, so set it to nil there. */
15165 if (windows_or_buffers_changed)
15166 {
15167 /* If window starts on a continuation line, maybe adjust the
15168 window start in case the window's width changed. */
15169 if (XMARKER (w->start)->buffer == current_buffer)
15170 compute_window_start_on_continuation_line (w);
15171
15172 w->window_end_valid = Qnil;
15173 }
15174
15175 /* Some sanity checks. */
15176 CHECK_WINDOW_END (w);
15177 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15178 abort ();
15179 if (BYTEPOS (opoint) < CHARPOS (opoint))
15180 abort ();
15181
15182 /* If %c is in mode line, update it if needed. */
15183 if (!NILP (w->column_number_displayed)
15184 /* This alternative quickly identifies a common case
15185 where no change is needed. */
15186 && !(PT == XFASTINT (w->last_point)
15187 && XFASTINT (w->last_modified) >= MODIFF
15188 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
15189 && (XFASTINT (w->column_number_displayed) != current_column ()))
15190 update_mode_line = 1;
15191
15192 /* Count number of windows showing the selected buffer. An indirect
15193 buffer counts as its base buffer. */
15194 if (!just_this_one_p)
15195 {
15196 struct buffer *current_base, *window_base;
15197 current_base = current_buffer;
15198 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15199 if (current_base->base_buffer)
15200 current_base = current_base->base_buffer;
15201 if (window_base->base_buffer)
15202 window_base = window_base->base_buffer;
15203 if (current_base == window_base)
15204 buffer_shared++;
15205 }
15206
15207 /* Point refers normally to the selected window. For any other
15208 window, set up appropriate value. */
15209 if (!EQ (window, selected_window))
15210 {
15211 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15212 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15213 if (new_pt < BEGV)
15214 {
15215 new_pt = BEGV;
15216 new_pt_byte = BEGV_BYTE;
15217 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15218 }
15219 else if (new_pt > (ZV - 1))
15220 {
15221 new_pt = ZV;
15222 new_pt_byte = ZV_BYTE;
15223 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15224 }
15225
15226 /* We don't use SET_PT so that the point-motion hooks don't run. */
15227 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15228 }
15229
15230 /* If any of the character widths specified in the display table
15231 have changed, invalidate the width run cache. It's true that
15232 this may be a bit late to catch such changes, but the rest of
15233 redisplay goes (non-fatally) haywire when the display table is
15234 changed, so why should we worry about doing any better? */
15235 if (current_buffer->width_run_cache)
15236 {
15237 struct Lisp_Char_Table *disptab = buffer_display_table ();
15238
15239 if (! disptab_matches_widthtab (disptab,
15240 XVECTOR (BVAR (current_buffer, width_table))))
15241 {
15242 invalidate_region_cache (current_buffer,
15243 current_buffer->width_run_cache,
15244 BEG, Z);
15245 recompute_width_table (current_buffer, disptab);
15246 }
15247 }
15248
15249 /* If window-start is screwed up, choose a new one. */
15250 if (XMARKER (w->start)->buffer != current_buffer)
15251 goto recenter;
15252
15253 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15254
15255 /* If someone specified a new starting point but did not insist,
15256 check whether it can be used. */
15257 if (!NILP (w->optional_new_start)
15258 && CHARPOS (startp) >= BEGV
15259 && CHARPOS (startp) <= ZV)
15260 {
15261 w->optional_new_start = Qnil;
15262 start_display (&it, w, startp);
15263 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15264 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15265 if (IT_CHARPOS (it) == PT)
15266 w->force_start = Qt;
15267 /* IT may overshoot PT if text at PT is invisible. */
15268 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15269 w->force_start = Qt;
15270 }
15271
15272 force_start:
15273
15274 /* Handle case where place to start displaying has been specified,
15275 unless the specified location is outside the accessible range. */
15276 if (!NILP (w->force_start)
15277 || w->frozen_window_start_p)
15278 {
15279 /* We set this later on if we have to adjust point. */
15280 int new_vpos = -1;
15281
15282 w->force_start = Qnil;
15283 w->vscroll = 0;
15284 w->window_end_valid = Qnil;
15285
15286 /* Forget any recorded base line for line number display. */
15287 if (!buffer_unchanged_p)
15288 w->base_line_number = Qnil;
15289
15290 /* Redisplay the mode line. Select the buffer properly for that.
15291 Also, run the hook window-scroll-functions
15292 because we have scrolled. */
15293 /* Note, we do this after clearing force_start because
15294 if there's an error, it is better to forget about force_start
15295 than to get into an infinite loop calling the hook functions
15296 and having them get more errors. */
15297 if (!update_mode_line
15298 || ! NILP (Vwindow_scroll_functions))
15299 {
15300 update_mode_line = 1;
15301 w->update_mode_line = Qt;
15302 startp = run_window_scroll_functions (window, startp);
15303 }
15304
15305 w->last_modified = make_number (0);
15306 w->last_overlay_modified = make_number (0);
15307 if (CHARPOS (startp) < BEGV)
15308 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15309 else if (CHARPOS (startp) > ZV)
15310 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15311
15312 /* Redisplay, then check if cursor has been set during the
15313 redisplay. Give up if new fonts were loaded. */
15314 /* We used to issue a CHECK_MARGINS argument to try_window here,
15315 but this causes scrolling to fail when point begins inside
15316 the scroll margin (bug#148) -- cyd */
15317 if (!try_window (window, startp, 0))
15318 {
15319 w->force_start = Qt;
15320 clear_glyph_matrix (w->desired_matrix);
15321 goto need_larger_matrices;
15322 }
15323
15324 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15325 {
15326 /* If point does not appear, try to move point so it does
15327 appear. The desired matrix has been built above, so we
15328 can use it here. */
15329 new_vpos = window_box_height (w) / 2;
15330 }
15331
15332 if (!cursor_row_fully_visible_p (w, 0, 0))
15333 {
15334 /* Point does appear, but on a line partly visible at end of window.
15335 Move it back to a fully-visible line. */
15336 new_vpos = window_box_height (w);
15337 }
15338
15339 /* If we need to move point for either of the above reasons,
15340 now actually do it. */
15341 if (new_vpos >= 0)
15342 {
15343 struct glyph_row *row;
15344
15345 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15346 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15347 ++row;
15348
15349 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15350 MATRIX_ROW_START_BYTEPOS (row));
15351
15352 if (w != XWINDOW (selected_window))
15353 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15354 else if (current_buffer == old)
15355 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15356
15357 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15358
15359 /* If we are highlighting the region, then we just changed
15360 the region, so redisplay to show it. */
15361 if (!NILP (Vtransient_mark_mode)
15362 && !NILP (BVAR (current_buffer, mark_active)))
15363 {
15364 clear_glyph_matrix (w->desired_matrix);
15365 if (!try_window (window, startp, 0))
15366 goto need_larger_matrices;
15367 }
15368 }
15369
15370 #if GLYPH_DEBUG
15371 debug_method_add (w, "forced window start");
15372 #endif
15373 goto done;
15374 }
15375
15376 /* Handle case where text has not changed, only point, and it has
15377 not moved off the frame, and we are not retrying after hscroll.
15378 (current_matrix_up_to_date_p is nonzero when retrying.) */
15379 if (current_matrix_up_to_date_p
15380 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15381 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15382 {
15383 switch (rc)
15384 {
15385 case CURSOR_MOVEMENT_SUCCESS:
15386 used_current_matrix_p = 1;
15387 goto done;
15388
15389 case CURSOR_MOVEMENT_MUST_SCROLL:
15390 goto try_to_scroll;
15391
15392 default:
15393 abort ();
15394 }
15395 }
15396 /* If current starting point was originally the beginning of a line
15397 but no longer is, find a new starting point. */
15398 else if (!NILP (w->start_at_line_beg)
15399 && !(CHARPOS (startp) <= BEGV
15400 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15401 {
15402 #if GLYPH_DEBUG
15403 debug_method_add (w, "recenter 1");
15404 #endif
15405 goto recenter;
15406 }
15407
15408 /* Try scrolling with try_window_id. Value is > 0 if update has
15409 been done, it is -1 if we know that the same window start will
15410 not work. It is 0 if unsuccessful for some other reason. */
15411 else if ((tem = try_window_id (w)) != 0)
15412 {
15413 #if GLYPH_DEBUG
15414 debug_method_add (w, "try_window_id %d", tem);
15415 #endif
15416
15417 if (fonts_changed_p)
15418 goto need_larger_matrices;
15419 if (tem > 0)
15420 goto done;
15421
15422 /* Otherwise try_window_id has returned -1 which means that we
15423 don't want the alternative below this comment to execute. */
15424 }
15425 else if (CHARPOS (startp) >= BEGV
15426 && CHARPOS (startp) <= ZV
15427 && PT >= CHARPOS (startp)
15428 && (CHARPOS (startp) < ZV
15429 /* Avoid starting at end of buffer. */
15430 || CHARPOS (startp) == BEGV
15431 || (XFASTINT (w->last_modified) >= MODIFF
15432 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15433 {
15434 int d1, d2, d3, d4, d5, d6;
15435
15436 /* If first window line is a continuation line, and window start
15437 is inside the modified region, but the first change is before
15438 current window start, we must select a new window start.
15439
15440 However, if this is the result of a down-mouse event (e.g. by
15441 extending the mouse-drag-overlay), we don't want to select a
15442 new window start, since that would change the position under
15443 the mouse, resulting in an unwanted mouse-movement rather
15444 than a simple mouse-click. */
15445 if (NILP (w->start_at_line_beg)
15446 && NILP (do_mouse_tracking)
15447 && CHARPOS (startp) > BEGV
15448 && CHARPOS (startp) > BEG + beg_unchanged
15449 && CHARPOS (startp) <= Z - end_unchanged
15450 /* Even if w->start_at_line_beg is nil, a new window may
15451 start at a line_beg, since that's how set_buffer_window
15452 sets it. So, we need to check the return value of
15453 compute_window_start_on_continuation_line. (See also
15454 bug#197). */
15455 && XMARKER (w->start)->buffer == current_buffer
15456 && compute_window_start_on_continuation_line (w)
15457 /* It doesn't make sense to force the window start like we
15458 do at label force_start if it is already known that point
15459 will not be visible in the resulting window, because
15460 doing so will move point from its correct position
15461 instead of scrolling the window to bring point into view.
15462 See bug#9324. */
15463 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15464 {
15465 w->force_start = Qt;
15466 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15467 goto force_start;
15468 }
15469
15470 #if GLYPH_DEBUG
15471 debug_method_add (w, "same window start");
15472 #endif
15473
15474 /* Try to redisplay starting at same place as before.
15475 If point has not moved off frame, accept the results. */
15476 if (!current_matrix_up_to_date_p
15477 /* Don't use try_window_reusing_current_matrix in this case
15478 because a window scroll function can have changed the
15479 buffer. */
15480 || !NILP (Vwindow_scroll_functions)
15481 || MINI_WINDOW_P (w)
15482 || !(used_current_matrix_p
15483 = try_window_reusing_current_matrix (w)))
15484 {
15485 IF_DEBUG (debug_method_add (w, "1"));
15486 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15487 /* -1 means we need to scroll.
15488 0 means we need new matrices, but fonts_changed_p
15489 is set in that case, so we will detect it below. */
15490 goto try_to_scroll;
15491 }
15492
15493 if (fonts_changed_p)
15494 goto need_larger_matrices;
15495
15496 if (w->cursor.vpos >= 0)
15497 {
15498 if (!just_this_one_p
15499 || current_buffer->clip_changed
15500 || BEG_UNCHANGED < CHARPOS (startp))
15501 /* Forget any recorded base line for line number display. */
15502 w->base_line_number = Qnil;
15503
15504 if (!cursor_row_fully_visible_p (w, 1, 0))
15505 {
15506 clear_glyph_matrix (w->desired_matrix);
15507 last_line_misfit = 1;
15508 }
15509 /* Drop through and scroll. */
15510 else
15511 goto done;
15512 }
15513 else
15514 clear_glyph_matrix (w->desired_matrix);
15515 }
15516
15517 try_to_scroll:
15518
15519 w->last_modified = make_number (0);
15520 w->last_overlay_modified = make_number (0);
15521
15522 /* Redisplay the mode line. Select the buffer properly for that. */
15523 if (!update_mode_line)
15524 {
15525 update_mode_line = 1;
15526 w->update_mode_line = Qt;
15527 }
15528
15529 /* Try to scroll by specified few lines. */
15530 if ((scroll_conservatively
15531 || emacs_scroll_step
15532 || temp_scroll_step
15533 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15534 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15535 && CHARPOS (startp) >= BEGV
15536 && CHARPOS (startp) <= ZV)
15537 {
15538 /* The function returns -1 if new fonts were loaded, 1 if
15539 successful, 0 if not successful. */
15540 int ss = try_scrolling (window, just_this_one_p,
15541 scroll_conservatively,
15542 emacs_scroll_step,
15543 temp_scroll_step, last_line_misfit);
15544 switch (ss)
15545 {
15546 case SCROLLING_SUCCESS:
15547 goto done;
15548
15549 case SCROLLING_NEED_LARGER_MATRICES:
15550 goto need_larger_matrices;
15551
15552 case SCROLLING_FAILED:
15553 break;
15554
15555 default:
15556 abort ();
15557 }
15558 }
15559
15560 /* Finally, just choose a place to start which positions point
15561 according to user preferences. */
15562
15563 recenter:
15564
15565 #if GLYPH_DEBUG
15566 debug_method_add (w, "recenter");
15567 #endif
15568
15569 /* w->vscroll = 0; */
15570
15571 /* Forget any previously recorded base line for line number display. */
15572 if (!buffer_unchanged_p)
15573 w->base_line_number = Qnil;
15574
15575 /* Determine the window start relative to point. */
15576 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15577 it.current_y = it.last_visible_y;
15578 if (centering_position < 0)
15579 {
15580 int margin =
15581 scroll_margin > 0
15582 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15583 : 0;
15584 ptrdiff_t margin_pos = CHARPOS (startp);
15585 Lisp_Object aggressive;
15586 int scrolling_up;
15587
15588 /* If there is a scroll margin at the top of the window, find
15589 its character position. */
15590 if (margin
15591 /* Cannot call start_display if startp is not in the
15592 accessible region of the buffer. This can happen when we
15593 have just switched to a different buffer and/or changed
15594 its restriction. In that case, startp is initialized to
15595 the character position 1 (BEG) because we did not yet
15596 have chance to display the buffer even once. */
15597 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15598 {
15599 struct it it1;
15600 void *it1data = NULL;
15601
15602 SAVE_IT (it1, it, it1data);
15603 start_display (&it1, w, startp);
15604 move_it_vertically (&it1, margin);
15605 margin_pos = IT_CHARPOS (it1);
15606 RESTORE_IT (&it, &it, it1data);
15607 }
15608 scrolling_up = PT > margin_pos;
15609 aggressive =
15610 scrolling_up
15611 ? BVAR (current_buffer, scroll_up_aggressively)
15612 : BVAR (current_buffer, scroll_down_aggressively);
15613
15614 if (!MINI_WINDOW_P (w)
15615 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15616 {
15617 int pt_offset = 0;
15618
15619 /* Setting scroll-conservatively overrides
15620 scroll-*-aggressively. */
15621 if (!scroll_conservatively && NUMBERP (aggressive))
15622 {
15623 double float_amount = XFLOATINT (aggressive);
15624
15625 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15626 if (pt_offset == 0 && float_amount > 0)
15627 pt_offset = 1;
15628 if (pt_offset && margin > 0)
15629 margin -= 1;
15630 }
15631 /* Compute how much to move the window start backward from
15632 point so that point will be displayed where the user
15633 wants it. */
15634 if (scrolling_up)
15635 {
15636 centering_position = it.last_visible_y;
15637 if (pt_offset)
15638 centering_position -= pt_offset;
15639 centering_position -=
15640 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15641 + WINDOW_HEADER_LINE_HEIGHT (w);
15642 /* Don't let point enter the scroll margin near top of
15643 the window. */
15644 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15645 centering_position = margin * FRAME_LINE_HEIGHT (f);
15646 }
15647 else
15648 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15649 }
15650 else
15651 /* Set the window start half the height of the window backward
15652 from point. */
15653 centering_position = window_box_height (w) / 2;
15654 }
15655 move_it_vertically_backward (&it, centering_position);
15656
15657 xassert (IT_CHARPOS (it) >= BEGV);
15658
15659 /* The function move_it_vertically_backward may move over more
15660 than the specified y-distance. If it->w is small, e.g. a
15661 mini-buffer window, we may end up in front of the window's
15662 display area. Start displaying at the start of the line
15663 containing PT in this case. */
15664 if (it.current_y <= 0)
15665 {
15666 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15667 move_it_vertically_backward (&it, 0);
15668 it.current_y = 0;
15669 }
15670
15671 it.current_x = it.hpos = 0;
15672
15673 /* Set the window start position here explicitly, to avoid an
15674 infinite loop in case the functions in window-scroll-functions
15675 get errors. */
15676 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15677
15678 /* Run scroll hooks. */
15679 startp = run_window_scroll_functions (window, it.current.pos);
15680
15681 /* Redisplay the window. */
15682 if (!current_matrix_up_to_date_p
15683 || windows_or_buffers_changed
15684 || cursor_type_changed
15685 /* Don't use try_window_reusing_current_matrix in this case
15686 because it can have changed the buffer. */
15687 || !NILP (Vwindow_scroll_functions)
15688 || !just_this_one_p
15689 || MINI_WINDOW_P (w)
15690 || !(used_current_matrix_p
15691 = try_window_reusing_current_matrix (w)))
15692 try_window (window, startp, 0);
15693
15694 /* If new fonts have been loaded (due to fontsets), give up. We
15695 have to start a new redisplay since we need to re-adjust glyph
15696 matrices. */
15697 if (fonts_changed_p)
15698 goto need_larger_matrices;
15699
15700 /* If cursor did not appear assume that the middle of the window is
15701 in the first line of the window. Do it again with the next line.
15702 (Imagine a window of height 100, displaying two lines of height
15703 60. Moving back 50 from it->last_visible_y will end in the first
15704 line.) */
15705 if (w->cursor.vpos < 0)
15706 {
15707 if (!NILP (w->window_end_valid)
15708 && PT >= Z - XFASTINT (w->window_end_pos))
15709 {
15710 clear_glyph_matrix (w->desired_matrix);
15711 move_it_by_lines (&it, 1);
15712 try_window (window, it.current.pos, 0);
15713 }
15714 else if (PT < IT_CHARPOS (it))
15715 {
15716 clear_glyph_matrix (w->desired_matrix);
15717 move_it_by_lines (&it, -1);
15718 try_window (window, it.current.pos, 0);
15719 }
15720 else
15721 {
15722 /* Not much we can do about it. */
15723 }
15724 }
15725
15726 /* Consider the following case: Window starts at BEGV, there is
15727 invisible, intangible text at BEGV, so that display starts at
15728 some point START > BEGV. It can happen that we are called with
15729 PT somewhere between BEGV and START. Try to handle that case. */
15730 if (w->cursor.vpos < 0)
15731 {
15732 struct glyph_row *row = w->current_matrix->rows;
15733 if (row->mode_line_p)
15734 ++row;
15735 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15736 }
15737
15738 if (!cursor_row_fully_visible_p (w, 0, 0))
15739 {
15740 /* If vscroll is enabled, disable it and try again. */
15741 if (w->vscroll)
15742 {
15743 w->vscroll = 0;
15744 clear_glyph_matrix (w->desired_matrix);
15745 goto recenter;
15746 }
15747
15748 /* Users who set scroll-conservatively to a large number want
15749 point just above/below the scroll margin. If we ended up
15750 with point's row partially visible, move the window start to
15751 make that row fully visible and out of the margin. */
15752 if (scroll_conservatively > SCROLL_LIMIT)
15753 {
15754 int margin =
15755 scroll_margin > 0
15756 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15757 : 0;
15758 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15759
15760 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15761 clear_glyph_matrix (w->desired_matrix);
15762 if (1 == try_window (window, it.current.pos,
15763 TRY_WINDOW_CHECK_MARGINS))
15764 goto done;
15765 }
15766
15767 /* If centering point failed to make the whole line visible,
15768 put point at the top instead. That has to make the whole line
15769 visible, if it can be done. */
15770 if (centering_position == 0)
15771 goto done;
15772
15773 clear_glyph_matrix (w->desired_matrix);
15774 centering_position = 0;
15775 goto recenter;
15776 }
15777
15778 done:
15779
15780 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15781 w->start_at_line_beg = ((CHARPOS (startp) == BEGV
15782 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n')
15783 ? Qt : Qnil);
15784
15785 /* Display the mode line, if we must. */
15786 if ((update_mode_line
15787 /* If window not full width, must redo its mode line
15788 if (a) the window to its side is being redone and
15789 (b) we do a frame-based redisplay. This is a consequence
15790 of how inverted lines are drawn in frame-based redisplay. */
15791 || (!just_this_one_p
15792 && !FRAME_WINDOW_P (f)
15793 && !WINDOW_FULL_WIDTH_P (w))
15794 /* Line number to display. */
15795 || INTEGERP (w->base_line_pos)
15796 /* Column number is displayed and different from the one displayed. */
15797 || (!NILP (w->column_number_displayed)
15798 && (XFASTINT (w->column_number_displayed) != current_column ())))
15799 /* This means that the window has a mode line. */
15800 && (WINDOW_WANTS_MODELINE_P (w)
15801 || WINDOW_WANTS_HEADER_LINE_P (w)))
15802 {
15803 display_mode_lines (w);
15804
15805 /* If mode line height has changed, arrange for a thorough
15806 immediate redisplay using the correct mode line height. */
15807 if (WINDOW_WANTS_MODELINE_P (w)
15808 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15809 {
15810 fonts_changed_p = 1;
15811 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15812 = DESIRED_MODE_LINE_HEIGHT (w);
15813 }
15814
15815 /* If header line height has changed, arrange for a thorough
15816 immediate redisplay using the correct header line height. */
15817 if (WINDOW_WANTS_HEADER_LINE_P (w)
15818 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15819 {
15820 fonts_changed_p = 1;
15821 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15822 = DESIRED_HEADER_LINE_HEIGHT (w);
15823 }
15824
15825 if (fonts_changed_p)
15826 goto need_larger_matrices;
15827 }
15828
15829 if (!line_number_displayed
15830 && !BUFFERP (w->base_line_pos))
15831 {
15832 w->base_line_pos = Qnil;
15833 w->base_line_number = Qnil;
15834 }
15835
15836 finish_menu_bars:
15837
15838 /* When we reach a frame's selected window, redo the frame's menu bar. */
15839 if (update_mode_line
15840 && EQ (FRAME_SELECTED_WINDOW (f), window))
15841 {
15842 int redisplay_menu_p = 0;
15843
15844 if (FRAME_WINDOW_P (f))
15845 {
15846 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15847 || defined (HAVE_NS) || defined (USE_GTK)
15848 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
15849 #else
15850 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15851 #endif
15852 }
15853 else
15854 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
15855
15856 if (redisplay_menu_p)
15857 display_menu_bar (w);
15858
15859 #ifdef HAVE_WINDOW_SYSTEM
15860 if (FRAME_WINDOW_P (f))
15861 {
15862 #if defined (USE_GTK) || defined (HAVE_NS)
15863 if (FRAME_EXTERNAL_TOOL_BAR (f))
15864 redisplay_tool_bar (f);
15865 #else
15866 if (WINDOWP (f->tool_bar_window)
15867 && (FRAME_TOOL_BAR_LINES (f) > 0
15868 || !NILP (Vauto_resize_tool_bars))
15869 && redisplay_tool_bar (f))
15870 ignore_mouse_drag_p = 1;
15871 #endif
15872 }
15873 #endif
15874 }
15875
15876 #ifdef HAVE_WINDOW_SYSTEM
15877 if (FRAME_WINDOW_P (f)
15878 && update_window_fringes (w, (just_this_one_p
15879 || (!used_current_matrix_p && !overlay_arrow_seen)
15880 || w->pseudo_window_p)))
15881 {
15882 update_begin (f);
15883 BLOCK_INPUT;
15884 if (draw_window_fringes (w, 1))
15885 x_draw_vertical_border (w);
15886 UNBLOCK_INPUT;
15887 update_end (f);
15888 }
15889 #endif /* HAVE_WINDOW_SYSTEM */
15890
15891 /* We go to this label, with fonts_changed_p nonzero,
15892 if it is necessary to try again using larger glyph matrices.
15893 We have to redeem the scroll bar even in this case,
15894 because the loop in redisplay_internal expects that. */
15895 need_larger_matrices:
15896 ;
15897 finish_scroll_bars:
15898
15899 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
15900 {
15901 /* Set the thumb's position and size. */
15902 set_vertical_scroll_bar (w);
15903
15904 /* Note that we actually used the scroll bar attached to this
15905 window, so it shouldn't be deleted at the end of redisplay. */
15906 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
15907 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
15908 }
15909
15910 /* Restore current_buffer and value of point in it. The window
15911 update may have changed the buffer, so first make sure `opoint'
15912 is still valid (Bug#6177). */
15913 if (CHARPOS (opoint) < BEGV)
15914 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
15915 else if (CHARPOS (opoint) > ZV)
15916 TEMP_SET_PT_BOTH (Z, Z_BYTE);
15917 else
15918 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
15919
15920 set_buffer_internal_1 (old);
15921 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
15922 shorter. This can be caused by log truncation in *Messages*. */
15923 if (CHARPOS (lpoint) <= ZV)
15924 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
15925
15926 unbind_to (count, Qnil);
15927 }
15928
15929
15930 /* Build the complete desired matrix of WINDOW with a window start
15931 buffer position POS.
15932
15933 Value is 1 if successful. It is zero if fonts were loaded during
15934 redisplay which makes re-adjusting glyph matrices necessary, and -1
15935 if point would appear in the scroll margins.
15936 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
15937 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
15938 set in FLAGS.) */
15939
15940 int
15941 try_window (Lisp_Object window, struct text_pos pos, int flags)
15942 {
15943 struct window *w = XWINDOW (window);
15944 struct it it;
15945 struct glyph_row *last_text_row = NULL;
15946 struct frame *f = XFRAME (w->frame);
15947
15948 /* Make POS the new window start. */
15949 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
15950
15951 /* Mark cursor position as unknown. No overlay arrow seen. */
15952 w->cursor.vpos = -1;
15953 overlay_arrow_seen = 0;
15954
15955 /* Initialize iterator and info to start at POS. */
15956 start_display (&it, w, pos);
15957
15958 /* Display all lines of W. */
15959 while (it.current_y < it.last_visible_y)
15960 {
15961 if (display_line (&it))
15962 last_text_row = it.glyph_row - 1;
15963 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
15964 return 0;
15965 }
15966
15967 /* Don't let the cursor end in the scroll margins. */
15968 if ((flags & TRY_WINDOW_CHECK_MARGINS)
15969 && !MINI_WINDOW_P (w))
15970 {
15971 int this_scroll_margin;
15972
15973 if (scroll_margin > 0)
15974 {
15975 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15976 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15977 }
15978 else
15979 this_scroll_margin = 0;
15980
15981 if ((w->cursor.y >= 0 /* not vscrolled */
15982 && w->cursor.y < this_scroll_margin
15983 && CHARPOS (pos) > BEGV
15984 && IT_CHARPOS (it) < ZV)
15985 /* rms: considering make_cursor_line_fully_visible_p here
15986 seems to give wrong results. We don't want to recenter
15987 when the last line is partly visible, we want to allow
15988 that case to be handled in the usual way. */
15989 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
15990 {
15991 w->cursor.vpos = -1;
15992 clear_glyph_matrix (w->desired_matrix);
15993 return -1;
15994 }
15995 }
15996
15997 /* If bottom moved off end of frame, change mode line percentage. */
15998 if (XFASTINT (w->window_end_pos) <= 0
15999 && Z != IT_CHARPOS (it))
16000 w->update_mode_line = Qt;
16001
16002 /* Set window_end_pos to the offset of the last character displayed
16003 on the window from the end of current_buffer. Set
16004 window_end_vpos to its row number. */
16005 if (last_text_row)
16006 {
16007 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16008 w->window_end_bytepos
16009 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16010 w->window_end_pos
16011 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16012 w->window_end_vpos
16013 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16014 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16015 ->displays_text_p);
16016 }
16017 else
16018 {
16019 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16020 w->window_end_pos = make_number (Z - ZV);
16021 w->window_end_vpos = make_number (0);
16022 }
16023
16024 /* But that is not valid info until redisplay finishes. */
16025 w->window_end_valid = Qnil;
16026 return 1;
16027 }
16028
16029
16030 \f
16031 /************************************************************************
16032 Window redisplay reusing current matrix when buffer has not changed
16033 ************************************************************************/
16034
16035 /* Try redisplay of window W showing an unchanged buffer with a
16036 different window start than the last time it was displayed by
16037 reusing its current matrix. Value is non-zero if successful.
16038 W->start is the new window start. */
16039
16040 static int
16041 try_window_reusing_current_matrix (struct window *w)
16042 {
16043 struct frame *f = XFRAME (w->frame);
16044 struct glyph_row *bottom_row;
16045 struct it it;
16046 struct run run;
16047 struct text_pos start, new_start;
16048 int nrows_scrolled, i;
16049 struct glyph_row *last_text_row;
16050 struct glyph_row *last_reused_text_row;
16051 struct glyph_row *start_row;
16052 int start_vpos, min_y, max_y;
16053
16054 #if GLYPH_DEBUG
16055 if (inhibit_try_window_reusing)
16056 return 0;
16057 #endif
16058
16059 if (/* This function doesn't handle terminal frames. */
16060 !FRAME_WINDOW_P (f)
16061 /* Don't try to reuse the display if windows have been split
16062 or such. */
16063 || windows_or_buffers_changed
16064 || cursor_type_changed)
16065 return 0;
16066
16067 /* Can't do this if region may have changed. */
16068 if ((!NILP (Vtransient_mark_mode)
16069 && !NILP (BVAR (current_buffer, mark_active)))
16070 || !NILP (w->region_showing)
16071 || !NILP (Vshow_trailing_whitespace))
16072 return 0;
16073
16074 /* If top-line visibility has changed, give up. */
16075 if (WINDOW_WANTS_HEADER_LINE_P (w)
16076 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16077 return 0;
16078
16079 /* Give up if old or new display is scrolled vertically. We could
16080 make this function handle this, but right now it doesn't. */
16081 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16082 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16083 return 0;
16084
16085 /* The variable new_start now holds the new window start. The old
16086 start `start' can be determined from the current matrix. */
16087 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16088 start = start_row->minpos;
16089 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16090
16091 /* Clear the desired matrix for the display below. */
16092 clear_glyph_matrix (w->desired_matrix);
16093
16094 if (CHARPOS (new_start) <= CHARPOS (start))
16095 {
16096 /* Don't use this method if the display starts with an ellipsis
16097 displayed for invisible text. It's not easy to handle that case
16098 below, and it's certainly not worth the effort since this is
16099 not a frequent case. */
16100 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16101 return 0;
16102
16103 IF_DEBUG (debug_method_add (w, "twu1"));
16104
16105 /* Display up to a row that can be reused. The variable
16106 last_text_row is set to the last row displayed that displays
16107 text. Note that it.vpos == 0 if or if not there is a
16108 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16109 start_display (&it, w, new_start);
16110 w->cursor.vpos = -1;
16111 last_text_row = last_reused_text_row = NULL;
16112
16113 while (it.current_y < it.last_visible_y
16114 && !fonts_changed_p)
16115 {
16116 /* If we have reached into the characters in the START row,
16117 that means the line boundaries have changed. So we
16118 can't start copying with the row START. Maybe it will
16119 work to start copying with the following row. */
16120 while (IT_CHARPOS (it) > CHARPOS (start))
16121 {
16122 /* Advance to the next row as the "start". */
16123 start_row++;
16124 start = start_row->minpos;
16125 /* If there are no more rows to try, or just one, give up. */
16126 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16127 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16128 || CHARPOS (start) == ZV)
16129 {
16130 clear_glyph_matrix (w->desired_matrix);
16131 return 0;
16132 }
16133
16134 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16135 }
16136 /* If we have reached alignment, we can copy the rest of the
16137 rows. */
16138 if (IT_CHARPOS (it) == CHARPOS (start)
16139 /* Don't accept "alignment" inside a display vector,
16140 since start_row could have started in the middle of
16141 that same display vector (thus their character
16142 positions match), and we have no way of telling if
16143 that is the case. */
16144 && it.current.dpvec_index < 0)
16145 break;
16146
16147 if (display_line (&it))
16148 last_text_row = it.glyph_row - 1;
16149
16150 }
16151
16152 /* A value of current_y < last_visible_y means that we stopped
16153 at the previous window start, which in turn means that we
16154 have at least one reusable row. */
16155 if (it.current_y < it.last_visible_y)
16156 {
16157 struct glyph_row *row;
16158
16159 /* IT.vpos always starts from 0; it counts text lines. */
16160 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16161
16162 /* Find PT if not already found in the lines displayed. */
16163 if (w->cursor.vpos < 0)
16164 {
16165 int dy = it.current_y - start_row->y;
16166
16167 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16168 row = row_containing_pos (w, PT, row, NULL, dy);
16169 if (row)
16170 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16171 dy, nrows_scrolled);
16172 else
16173 {
16174 clear_glyph_matrix (w->desired_matrix);
16175 return 0;
16176 }
16177 }
16178
16179 /* Scroll the display. Do it before the current matrix is
16180 changed. The problem here is that update has not yet
16181 run, i.e. part of the current matrix is not up to date.
16182 scroll_run_hook will clear the cursor, and use the
16183 current matrix to get the height of the row the cursor is
16184 in. */
16185 run.current_y = start_row->y;
16186 run.desired_y = it.current_y;
16187 run.height = it.last_visible_y - it.current_y;
16188
16189 if (run.height > 0 && run.current_y != run.desired_y)
16190 {
16191 update_begin (f);
16192 FRAME_RIF (f)->update_window_begin_hook (w);
16193 FRAME_RIF (f)->clear_window_mouse_face (w);
16194 FRAME_RIF (f)->scroll_run_hook (w, &run);
16195 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16196 update_end (f);
16197 }
16198
16199 /* Shift current matrix down by nrows_scrolled lines. */
16200 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16201 rotate_matrix (w->current_matrix,
16202 start_vpos,
16203 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16204 nrows_scrolled);
16205
16206 /* Disable lines that must be updated. */
16207 for (i = 0; i < nrows_scrolled; ++i)
16208 (start_row + i)->enabled_p = 0;
16209
16210 /* Re-compute Y positions. */
16211 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16212 max_y = it.last_visible_y;
16213 for (row = start_row + nrows_scrolled;
16214 row < bottom_row;
16215 ++row)
16216 {
16217 row->y = it.current_y;
16218 row->visible_height = row->height;
16219
16220 if (row->y < min_y)
16221 row->visible_height -= min_y - row->y;
16222 if (row->y + row->height > max_y)
16223 row->visible_height -= row->y + row->height - max_y;
16224 if (row->fringe_bitmap_periodic_p)
16225 row->redraw_fringe_bitmaps_p = 1;
16226
16227 it.current_y += row->height;
16228
16229 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16230 last_reused_text_row = row;
16231 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16232 break;
16233 }
16234
16235 /* Disable lines in the current matrix which are now
16236 below the window. */
16237 for (++row; row < bottom_row; ++row)
16238 row->enabled_p = row->mode_line_p = 0;
16239 }
16240
16241 /* Update window_end_pos etc.; last_reused_text_row is the last
16242 reused row from the current matrix containing text, if any.
16243 The value of last_text_row is the last displayed line
16244 containing text. */
16245 if (last_reused_text_row)
16246 {
16247 w->window_end_bytepos
16248 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16249 w->window_end_pos
16250 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16251 w->window_end_vpos
16252 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16253 w->current_matrix));
16254 }
16255 else if (last_text_row)
16256 {
16257 w->window_end_bytepos
16258 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16259 w->window_end_pos
16260 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16261 w->window_end_vpos
16262 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16263 }
16264 else
16265 {
16266 /* This window must be completely empty. */
16267 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16268 w->window_end_pos = make_number (Z - ZV);
16269 w->window_end_vpos = make_number (0);
16270 }
16271 w->window_end_valid = Qnil;
16272
16273 /* Update hint: don't try scrolling again in update_window. */
16274 w->desired_matrix->no_scrolling_p = 1;
16275
16276 #if GLYPH_DEBUG
16277 debug_method_add (w, "try_window_reusing_current_matrix 1");
16278 #endif
16279 return 1;
16280 }
16281 else if (CHARPOS (new_start) > CHARPOS (start))
16282 {
16283 struct glyph_row *pt_row, *row;
16284 struct glyph_row *first_reusable_row;
16285 struct glyph_row *first_row_to_display;
16286 int dy;
16287 int yb = window_text_bottom_y (w);
16288
16289 /* Find the row starting at new_start, if there is one. Don't
16290 reuse a partially visible line at the end. */
16291 first_reusable_row = start_row;
16292 while (first_reusable_row->enabled_p
16293 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16294 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16295 < CHARPOS (new_start)))
16296 ++first_reusable_row;
16297
16298 /* Give up if there is no row to reuse. */
16299 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16300 || !first_reusable_row->enabled_p
16301 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16302 != CHARPOS (new_start)))
16303 return 0;
16304
16305 /* We can reuse fully visible rows beginning with
16306 first_reusable_row to the end of the window. Set
16307 first_row_to_display to the first row that cannot be reused.
16308 Set pt_row to the row containing point, if there is any. */
16309 pt_row = NULL;
16310 for (first_row_to_display = first_reusable_row;
16311 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16312 ++first_row_to_display)
16313 {
16314 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16315 && PT < MATRIX_ROW_END_CHARPOS (first_row_to_display))
16316 pt_row = first_row_to_display;
16317 }
16318
16319 /* Start displaying at the start of first_row_to_display. */
16320 xassert (first_row_to_display->y < yb);
16321 init_to_row_start (&it, w, first_row_to_display);
16322
16323 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16324 - start_vpos);
16325 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16326 - nrows_scrolled);
16327 it.current_y = (first_row_to_display->y - first_reusable_row->y
16328 + WINDOW_HEADER_LINE_HEIGHT (w));
16329
16330 /* Display lines beginning with first_row_to_display in the
16331 desired matrix. Set last_text_row to the last row displayed
16332 that displays text. */
16333 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16334 if (pt_row == NULL)
16335 w->cursor.vpos = -1;
16336 last_text_row = NULL;
16337 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16338 if (display_line (&it))
16339 last_text_row = it.glyph_row - 1;
16340
16341 /* If point is in a reused row, adjust y and vpos of the cursor
16342 position. */
16343 if (pt_row)
16344 {
16345 w->cursor.vpos -= nrows_scrolled;
16346 w->cursor.y -= first_reusable_row->y - start_row->y;
16347 }
16348
16349 /* Give up if point isn't in a row displayed or reused. (This
16350 also handles the case where w->cursor.vpos < nrows_scrolled
16351 after the calls to display_line, which can happen with scroll
16352 margins. See bug#1295.) */
16353 if (w->cursor.vpos < 0)
16354 {
16355 clear_glyph_matrix (w->desired_matrix);
16356 return 0;
16357 }
16358
16359 /* Scroll the display. */
16360 run.current_y = first_reusable_row->y;
16361 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16362 run.height = it.last_visible_y - run.current_y;
16363 dy = run.current_y - run.desired_y;
16364
16365 if (run.height)
16366 {
16367 update_begin (f);
16368 FRAME_RIF (f)->update_window_begin_hook (w);
16369 FRAME_RIF (f)->clear_window_mouse_face (w);
16370 FRAME_RIF (f)->scroll_run_hook (w, &run);
16371 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16372 update_end (f);
16373 }
16374
16375 /* Adjust Y positions of reused rows. */
16376 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16377 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16378 max_y = it.last_visible_y;
16379 for (row = first_reusable_row; row < first_row_to_display; ++row)
16380 {
16381 row->y -= dy;
16382 row->visible_height = row->height;
16383 if (row->y < min_y)
16384 row->visible_height -= min_y - row->y;
16385 if (row->y + row->height > max_y)
16386 row->visible_height -= row->y + row->height - max_y;
16387 if (row->fringe_bitmap_periodic_p)
16388 row->redraw_fringe_bitmaps_p = 1;
16389 }
16390
16391 /* Scroll the current matrix. */
16392 xassert (nrows_scrolled > 0);
16393 rotate_matrix (w->current_matrix,
16394 start_vpos,
16395 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16396 -nrows_scrolled);
16397
16398 /* Disable rows not reused. */
16399 for (row -= nrows_scrolled; row < bottom_row; ++row)
16400 row->enabled_p = 0;
16401
16402 /* Point may have moved to a different line, so we cannot assume that
16403 the previous cursor position is valid; locate the correct row. */
16404 if (pt_row)
16405 {
16406 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16407 row < bottom_row && PT >= MATRIX_ROW_END_CHARPOS (row);
16408 row++)
16409 {
16410 w->cursor.vpos++;
16411 w->cursor.y = row->y;
16412 }
16413 if (row < bottom_row)
16414 {
16415 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16416 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16417
16418 /* Can't use this optimization with bidi-reordered glyph
16419 rows, unless cursor is already at point. */
16420 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16421 {
16422 if (!(w->cursor.hpos >= 0
16423 && w->cursor.hpos < row->used[TEXT_AREA]
16424 && BUFFERP (glyph->object)
16425 && glyph->charpos == PT))
16426 return 0;
16427 }
16428 else
16429 for (; glyph < end
16430 && (!BUFFERP (glyph->object)
16431 || glyph->charpos < PT);
16432 glyph++)
16433 {
16434 w->cursor.hpos++;
16435 w->cursor.x += glyph->pixel_width;
16436 }
16437 }
16438 }
16439
16440 /* Adjust window end. A null value of last_text_row means that
16441 the window end is in reused rows which in turn means that
16442 only its vpos can have changed. */
16443 if (last_text_row)
16444 {
16445 w->window_end_bytepos
16446 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16447 w->window_end_pos
16448 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16449 w->window_end_vpos
16450 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16451 }
16452 else
16453 {
16454 w->window_end_vpos
16455 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16456 }
16457
16458 w->window_end_valid = Qnil;
16459 w->desired_matrix->no_scrolling_p = 1;
16460
16461 #if GLYPH_DEBUG
16462 debug_method_add (w, "try_window_reusing_current_matrix 2");
16463 #endif
16464 return 1;
16465 }
16466
16467 return 0;
16468 }
16469
16470
16471 \f
16472 /************************************************************************
16473 Window redisplay reusing current matrix when buffer has changed
16474 ************************************************************************/
16475
16476 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16477 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16478 ptrdiff_t *, ptrdiff_t *);
16479 static struct glyph_row *
16480 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16481 struct glyph_row *);
16482
16483
16484 /* Return the last row in MATRIX displaying text. If row START is
16485 non-null, start searching with that row. IT gives the dimensions
16486 of the display. Value is null if matrix is empty; otherwise it is
16487 a pointer to the row found. */
16488
16489 static struct glyph_row *
16490 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16491 struct glyph_row *start)
16492 {
16493 struct glyph_row *row, *row_found;
16494
16495 /* Set row_found to the last row in IT->w's current matrix
16496 displaying text. The loop looks funny but think of partially
16497 visible lines. */
16498 row_found = NULL;
16499 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16500 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16501 {
16502 xassert (row->enabled_p);
16503 row_found = row;
16504 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16505 break;
16506 ++row;
16507 }
16508
16509 return row_found;
16510 }
16511
16512
16513 /* Return the last row in the current matrix of W that is not affected
16514 by changes at the start of current_buffer that occurred since W's
16515 current matrix was built. Value is null if no such row exists.
16516
16517 BEG_UNCHANGED us the number of characters unchanged at the start of
16518 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16519 first changed character in current_buffer. Characters at positions <
16520 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16521 when the current matrix was built. */
16522
16523 static struct glyph_row *
16524 find_last_unchanged_at_beg_row (struct window *w)
16525 {
16526 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16527 struct glyph_row *row;
16528 struct glyph_row *row_found = NULL;
16529 int yb = window_text_bottom_y (w);
16530
16531 /* Find the last row displaying unchanged text. */
16532 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16533 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16534 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16535 ++row)
16536 {
16537 if (/* If row ends before first_changed_pos, it is unchanged,
16538 except in some case. */
16539 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16540 /* When row ends in ZV and we write at ZV it is not
16541 unchanged. */
16542 && !row->ends_at_zv_p
16543 /* When first_changed_pos is the end of a continued line,
16544 row is not unchanged because it may be no longer
16545 continued. */
16546 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16547 && (row->continued_p
16548 || row->exact_window_width_line_p)))
16549 row_found = row;
16550
16551 /* Stop if last visible row. */
16552 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16553 break;
16554 }
16555
16556 return row_found;
16557 }
16558
16559
16560 /* Find the first glyph row in the current matrix of W that is not
16561 affected by changes at the end of current_buffer since the
16562 time W's current matrix was built.
16563
16564 Return in *DELTA the number of chars by which buffer positions in
16565 unchanged text at the end of current_buffer must be adjusted.
16566
16567 Return in *DELTA_BYTES the corresponding number of bytes.
16568
16569 Value is null if no such row exists, i.e. all rows are affected by
16570 changes. */
16571
16572 static struct glyph_row *
16573 find_first_unchanged_at_end_row (struct window *w,
16574 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16575 {
16576 struct glyph_row *row;
16577 struct glyph_row *row_found = NULL;
16578
16579 *delta = *delta_bytes = 0;
16580
16581 /* Display must not have been paused, otherwise the current matrix
16582 is not up to date. */
16583 eassert (!NILP (w->window_end_valid));
16584
16585 /* A value of window_end_pos >= END_UNCHANGED means that the window
16586 end is in the range of changed text. If so, there is no
16587 unchanged row at the end of W's current matrix. */
16588 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16589 return NULL;
16590
16591 /* Set row to the last row in W's current matrix displaying text. */
16592 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16593
16594 /* If matrix is entirely empty, no unchanged row exists. */
16595 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16596 {
16597 /* The value of row is the last glyph row in the matrix having a
16598 meaningful buffer position in it. The end position of row
16599 corresponds to window_end_pos. This allows us to translate
16600 buffer positions in the current matrix to current buffer
16601 positions for characters not in changed text. */
16602 ptrdiff_t Z_old =
16603 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16604 ptrdiff_t Z_BYTE_old =
16605 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16606 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16607 struct glyph_row *first_text_row
16608 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16609
16610 *delta = Z - Z_old;
16611 *delta_bytes = Z_BYTE - Z_BYTE_old;
16612
16613 /* Set last_unchanged_pos to the buffer position of the last
16614 character in the buffer that has not been changed. Z is the
16615 index + 1 of the last character in current_buffer, i.e. by
16616 subtracting END_UNCHANGED we get the index of the last
16617 unchanged character, and we have to add BEG to get its buffer
16618 position. */
16619 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16620 last_unchanged_pos_old = last_unchanged_pos - *delta;
16621
16622 /* Search backward from ROW for a row displaying a line that
16623 starts at a minimum position >= last_unchanged_pos_old. */
16624 for (; row > first_text_row; --row)
16625 {
16626 /* This used to abort, but it can happen.
16627 It is ok to just stop the search instead here. KFS. */
16628 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16629 break;
16630
16631 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16632 row_found = row;
16633 }
16634 }
16635
16636 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16637
16638 return row_found;
16639 }
16640
16641
16642 /* Make sure that glyph rows in the current matrix of window W
16643 reference the same glyph memory as corresponding rows in the
16644 frame's frame matrix. This function is called after scrolling W's
16645 current matrix on a terminal frame in try_window_id and
16646 try_window_reusing_current_matrix. */
16647
16648 static void
16649 sync_frame_with_window_matrix_rows (struct window *w)
16650 {
16651 struct frame *f = XFRAME (w->frame);
16652 struct glyph_row *window_row, *window_row_end, *frame_row;
16653
16654 /* Preconditions: W must be a leaf window and full-width. Its frame
16655 must have a frame matrix. */
16656 xassert (NILP (w->hchild) && NILP (w->vchild));
16657 xassert (WINDOW_FULL_WIDTH_P (w));
16658 xassert (!FRAME_WINDOW_P (f));
16659
16660 /* If W is a full-width window, glyph pointers in W's current matrix
16661 have, by definition, to be the same as glyph pointers in the
16662 corresponding frame matrix. Note that frame matrices have no
16663 marginal areas (see build_frame_matrix). */
16664 window_row = w->current_matrix->rows;
16665 window_row_end = window_row + w->current_matrix->nrows;
16666 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16667 while (window_row < window_row_end)
16668 {
16669 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16670 struct glyph *end = window_row->glyphs[LAST_AREA];
16671
16672 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16673 frame_row->glyphs[TEXT_AREA] = start;
16674 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16675 frame_row->glyphs[LAST_AREA] = end;
16676
16677 /* Disable frame rows whose corresponding window rows have
16678 been disabled in try_window_id. */
16679 if (!window_row->enabled_p)
16680 frame_row->enabled_p = 0;
16681
16682 ++window_row, ++frame_row;
16683 }
16684 }
16685
16686
16687 /* Find the glyph row in window W containing CHARPOS. Consider all
16688 rows between START and END (not inclusive). END null means search
16689 all rows to the end of the display area of W. Value is the row
16690 containing CHARPOS or null. */
16691
16692 struct glyph_row *
16693 row_containing_pos (struct window *w, ptrdiff_t charpos,
16694 struct glyph_row *start, struct glyph_row *end, int dy)
16695 {
16696 struct glyph_row *row = start;
16697 struct glyph_row *best_row = NULL;
16698 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16699 int last_y;
16700
16701 /* If we happen to start on a header-line, skip that. */
16702 if (row->mode_line_p)
16703 ++row;
16704
16705 if ((end && row >= end) || !row->enabled_p)
16706 return NULL;
16707
16708 last_y = window_text_bottom_y (w) - dy;
16709
16710 while (1)
16711 {
16712 /* Give up if we have gone too far. */
16713 if (end && row >= end)
16714 return NULL;
16715 /* This formerly returned if they were equal.
16716 I think that both quantities are of a "last plus one" type;
16717 if so, when they are equal, the row is within the screen. -- rms. */
16718 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16719 return NULL;
16720
16721 /* If it is in this row, return this row. */
16722 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16723 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16724 /* The end position of a row equals the start
16725 position of the next row. If CHARPOS is there, we
16726 would rather display it in the next line, except
16727 when this line ends in ZV. */
16728 && !row->ends_at_zv_p
16729 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16730 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16731 {
16732 struct glyph *g;
16733
16734 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16735 || (!best_row && !row->continued_p))
16736 return row;
16737 /* In bidi-reordered rows, there could be several rows
16738 occluding point, all of them belonging to the same
16739 continued line. We need to find the row which fits
16740 CHARPOS the best. */
16741 for (g = row->glyphs[TEXT_AREA];
16742 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16743 g++)
16744 {
16745 if (!STRINGP (g->object))
16746 {
16747 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16748 {
16749 mindif = eabs (g->charpos - charpos);
16750 best_row = row;
16751 /* Exact match always wins. */
16752 if (mindif == 0)
16753 return best_row;
16754 }
16755 }
16756 }
16757 }
16758 else if (best_row && !row->continued_p)
16759 return best_row;
16760 ++row;
16761 }
16762 }
16763
16764
16765 /* Try to redisplay window W by reusing its existing display. W's
16766 current matrix must be up to date when this function is called,
16767 i.e. window_end_valid must not be nil.
16768
16769 Value is
16770
16771 1 if display has been updated
16772 0 if otherwise unsuccessful
16773 -1 if redisplay with same window start is known not to succeed
16774
16775 The following steps are performed:
16776
16777 1. Find the last row in the current matrix of W that is not
16778 affected by changes at the start of current_buffer. If no such row
16779 is found, give up.
16780
16781 2. Find the first row in W's current matrix that is not affected by
16782 changes at the end of current_buffer. Maybe there is no such row.
16783
16784 3. Display lines beginning with the row + 1 found in step 1 to the
16785 row found in step 2 or, if step 2 didn't find a row, to the end of
16786 the window.
16787
16788 4. If cursor is not known to appear on the window, give up.
16789
16790 5. If display stopped at the row found in step 2, scroll the
16791 display and current matrix as needed.
16792
16793 6. Maybe display some lines at the end of W, if we must. This can
16794 happen under various circumstances, like a partially visible line
16795 becoming fully visible, or because newly displayed lines are displayed
16796 in smaller font sizes.
16797
16798 7. Update W's window end information. */
16799
16800 static int
16801 try_window_id (struct window *w)
16802 {
16803 struct frame *f = XFRAME (w->frame);
16804 struct glyph_matrix *current_matrix = w->current_matrix;
16805 struct glyph_matrix *desired_matrix = w->desired_matrix;
16806 struct glyph_row *last_unchanged_at_beg_row;
16807 struct glyph_row *first_unchanged_at_end_row;
16808 struct glyph_row *row;
16809 struct glyph_row *bottom_row;
16810 int bottom_vpos;
16811 struct it it;
16812 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
16813 int dvpos, dy;
16814 struct text_pos start_pos;
16815 struct run run;
16816 int first_unchanged_at_end_vpos = 0;
16817 struct glyph_row *last_text_row, *last_text_row_at_end;
16818 struct text_pos start;
16819 ptrdiff_t first_changed_charpos, last_changed_charpos;
16820
16821 #if GLYPH_DEBUG
16822 if (inhibit_try_window_id)
16823 return 0;
16824 #endif
16825
16826 /* This is handy for debugging. */
16827 #if 0
16828 #define GIVE_UP(X) \
16829 do { \
16830 fprintf (stderr, "try_window_id give up %d\n", (X)); \
16831 return 0; \
16832 } while (0)
16833 #else
16834 #define GIVE_UP(X) return 0
16835 #endif
16836
16837 SET_TEXT_POS_FROM_MARKER (start, w->start);
16838
16839 /* Don't use this for mini-windows because these can show
16840 messages and mini-buffers, and we don't handle that here. */
16841 if (MINI_WINDOW_P (w))
16842 GIVE_UP (1);
16843
16844 /* This flag is used to prevent redisplay optimizations. */
16845 if (windows_or_buffers_changed || cursor_type_changed)
16846 GIVE_UP (2);
16847
16848 /* Verify that narrowing has not changed.
16849 Also verify that we were not told to prevent redisplay optimizations.
16850 It would be nice to further
16851 reduce the number of cases where this prevents try_window_id. */
16852 if (current_buffer->clip_changed
16853 || current_buffer->prevent_redisplay_optimizations_p)
16854 GIVE_UP (3);
16855
16856 /* Window must either use window-based redisplay or be full width. */
16857 if (!FRAME_WINDOW_P (f)
16858 && (!FRAME_LINE_INS_DEL_OK (f)
16859 || !WINDOW_FULL_WIDTH_P (w)))
16860 GIVE_UP (4);
16861
16862 /* Give up if point is known NOT to appear in W. */
16863 if (PT < CHARPOS (start))
16864 GIVE_UP (5);
16865
16866 /* Another way to prevent redisplay optimizations. */
16867 if (XFASTINT (w->last_modified) == 0)
16868 GIVE_UP (6);
16869
16870 /* Verify that window is not hscrolled. */
16871 if (XFASTINT (w->hscroll) != 0)
16872 GIVE_UP (7);
16873
16874 /* Verify that display wasn't paused. */
16875 if (NILP (w->window_end_valid))
16876 GIVE_UP (8);
16877
16878 /* Can't use this if highlighting a region because a cursor movement
16879 will do more than just set the cursor. */
16880 if (!NILP (Vtransient_mark_mode)
16881 && !NILP (BVAR (current_buffer, mark_active)))
16882 GIVE_UP (9);
16883
16884 /* Likewise if highlighting trailing whitespace. */
16885 if (!NILP (Vshow_trailing_whitespace))
16886 GIVE_UP (11);
16887
16888 /* Likewise if showing a region. */
16889 if (!NILP (w->region_showing))
16890 GIVE_UP (10);
16891
16892 /* Can't use this if overlay arrow position and/or string have
16893 changed. */
16894 if (overlay_arrows_changed_p ())
16895 GIVE_UP (12);
16896
16897 /* When word-wrap is on, adding a space to the first word of a
16898 wrapped line can change the wrap position, altering the line
16899 above it. It might be worthwhile to handle this more
16900 intelligently, but for now just redisplay from scratch. */
16901 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
16902 GIVE_UP (21);
16903
16904 /* Under bidi reordering, adding or deleting a character in the
16905 beginning of a paragraph, before the first strong directional
16906 character, can change the base direction of the paragraph (unless
16907 the buffer specifies a fixed paragraph direction), which will
16908 require to redisplay the whole paragraph. It might be worthwhile
16909 to find the paragraph limits and widen the range of redisplayed
16910 lines to that, but for now just give up this optimization and
16911 redisplay from scratch. */
16912 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16913 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
16914 GIVE_UP (22);
16915
16916 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
16917 only if buffer has really changed. The reason is that the gap is
16918 initially at Z for freshly visited files. The code below would
16919 set end_unchanged to 0 in that case. */
16920 if (MODIFF > SAVE_MODIFF
16921 /* This seems to happen sometimes after saving a buffer. */
16922 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
16923 {
16924 if (GPT - BEG < BEG_UNCHANGED)
16925 BEG_UNCHANGED = GPT - BEG;
16926 if (Z - GPT < END_UNCHANGED)
16927 END_UNCHANGED = Z - GPT;
16928 }
16929
16930 /* The position of the first and last character that has been changed. */
16931 first_changed_charpos = BEG + BEG_UNCHANGED;
16932 last_changed_charpos = Z - END_UNCHANGED;
16933
16934 /* If window starts after a line end, and the last change is in
16935 front of that newline, then changes don't affect the display.
16936 This case happens with stealth-fontification. Note that although
16937 the display is unchanged, glyph positions in the matrix have to
16938 be adjusted, of course. */
16939 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16940 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
16941 && ((last_changed_charpos < CHARPOS (start)
16942 && CHARPOS (start) == BEGV)
16943 || (last_changed_charpos < CHARPOS (start) - 1
16944 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
16945 {
16946 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
16947 struct glyph_row *r0;
16948
16949 /* Compute how many chars/bytes have been added to or removed
16950 from the buffer. */
16951 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16952 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16953 Z_delta = Z - Z_old;
16954 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
16955
16956 /* Give up if PT is not in the window. Note that it already has
16957 been checked at the start of try_window_id that PT is not in
16958 front of the window start. */
16959 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
16960 GIVE_UP (13);
16961
16962 /* If window start is unchanged, we can reuse the whole matrix
16963 as is, after adjusting glyph positions. No need to compute
16964 the window end again, since its offset from Z hasn't changed. */
16965 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
16966 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
16967 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
16968 /* PT must not be in a partially visible line. */
16969 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
16970 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
16971 {
16972 /* Adjust positions in the glyph matrix. */
16973 if (Z_delta || Z_delta_bytes)
16974 {
16975 struct glyph_row *r1
16976 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
16977 increment_matrix_positions (w->current_matrix,
16978 MATRIX_ROW_VPOS (r0, current_matrix),
16979 MATRIX_ROW_VPOS (r1, current_matrix),
16980 Z_delta, Z_delta_bytes);
16981 }
16982
16983 /* Set the cursor. */
16984 row = row_containing_pos (w, PT, r0, NULL, 0);
16985 if (row)
16986 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
16987 else
16988 abort ();
16989 return 1;
16990 }
16991 }
16992
16993 /* Handle the case that changes are all below what is displayed in
16994 the window, and that PT is in the window. This shortcut cannot
16995 be taken if ZV is visible in the window, and text has been added
16996 there that is visible in the window. */
16997 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
16998 /* ZV is not visible in the window, or there are no
16999 changes at ZV, actually. */
17000 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17001 || first_changed_charpos == last_changed_charpos))
17002 {
17003 struct glyph_row *r0;
17004
17005 /* Give up if PT is not in the window. Note that it already has
17006 been checked at the start of try_window_id that PT is not in
17007 front of the window start. */
17008 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17009 GIVE_UP (14);
17010
17011 /* If window start is unchanged, we can reuse the whole matrix
17012 as is, without changing glyph positions since no text has
17013 been added/removed in front of the window end. */
17014 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17015 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17016 /* PT must not be in a partially visible line. */
17017 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17018 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17019 {
17020 /* We have to compute the window end anew since text
17021 could have been added/removed after it. */
17022 w->window_end_pos
17023 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17024 w->window_end_bytepos
17025 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17026
17027 /* Set the cursor. */
17028 row = row_containing_pos (w, PT, r0, NULL, 0);
17029 if (row)
17030 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17031 else
17032 abort ();
17033 return 2;
17034 }
17035 }
17036
17037 /* Give up if window start is in the changed area.
17038
17039 The condition used to read
17040
17041 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17042
17043 but why that was tested escapes me at the moment. */
17044 if (CHARPOS (start) >= first_changed_charpos
17045 && CHARPOS (start) <= last_changed_charpos)
17046 GIVE_UP (15);
17047
17048 /* Check that window start agrees with the start of the first glyph
17049 row in its current matrix. Check this after we know the window
17050 start is not in changed text, otherwise positions would not be
17051 comparable. */
17052 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17053 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17054 GIVE_UP (16);
17055
17056 /* Give up if the window ends in strings. Overlay strings
17057 at the end are difficult to handle, so don't try. */
17058 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17059 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17060 GIVE_UP (20);
17061
17062 /* Compute the position at which we have to start displaying new
17063 lines. Some of the lines at the top of the window might be
17064 reusable because they are not displaying changed text. Find the
17065 last row in W's current matrix not affected by changes at the
17066 start of current_buffer. Value is null if changes start in the
17067 first line of window. */
17068 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17069 if (last_unchanged_at_beg_row)
17070 {
17071 /* Avoid starting to display in the middle of a character, a TAB
17072 for instance. This is easier than to set up the iterator
17073 exactly, and it's not a frequent case, so the additional
17074 effort wouldn't really pay off. */
17075 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17076 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17077 && last_unchanged_at_beg_row > w->current_matrix->rows)
17078 --last_unchanged_at_beg_row;
17079
17080 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17081 GIVE_UP (17);
17082
17083 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17084 GIVE_UP (18);
17085 start_pos = it.current.pos;
17086
17087 /* Start displaying new lines in the desired matrix at the same
17088 vpos we would use in the current matrix, i.e. below
17089 last_unchanged_at_beg_row. */
17090 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17091 current_matrix);
17092 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17093 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17094
17095 xassert (it.hpos == 0 && it.current_x == 0);
17096 }
17097 else
17098 {
17099 /* There are no reusable lines at the start of the window.
17100 Start displaying in the first text line. */
17101 start_display (&it, w, start);
17102 it.vpos = it.first_vpos;
17103 start_pos = it.current.pos;
17104 }
17105
17106 /* Find the first row that is not affected by changes at the end of
17107 the buffer. Value will be null if there is no unchanged row, in
17108 which case we must redisplay to the end of the window. delta
17109 will be set to the value by which buffer positions beginning with
17110 first_unchanged_at_end_row have to be adjusted due to text
17111 changes. */
17112 first_unchanged_at_end_row
17113 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17114 IF_DEBUG (debug_delta = delta);
17115 IF_DEBUG (debug_delta_bytes = delta_bytes);
17116
17117 /* Set stop_pos to the buffer position up to which we will have to
17118 display new lines. If first_unchanged_at_end_row != NULL, this
17119 is the buffer position of the start of the line displayed in that
17120 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17121 that we don't stop at a buffer position. */
17122 stop_pos = 0;
17123 if (first_unchanged_at_end_row)
17124 {
17125 xassert (last_unchanged_at_beg_row == NULL
17126 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17127
17128 /* If this is a continuation line, move forward to the next one
17129 that isn't. Changes in lines above affect this line.
17130 Caution: this may move first_unchanged_at_end_row to a row
17131 not displaying text. */
17132 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17133 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17134 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17135 < it.last_visible_y))
17136 ++first_unchanged_at_end_row;
17137
17138 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17139 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17140 >= it.last_visible_y))
17141 first_unchanged_at_end_row = NULL;
17142 else
17143 {
17144 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17145 + delta);
17146 first_unchanged_at_end_vpos
17147 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17148 xassert (stop_pos >= Z - END_UNCHANGED);
17149 }
17150 }
17151 else if (last_unchanged_at_beg_row == NULL)
17152 GIVE_UP (19);
17153
17154
17155 #if GLYPH_DEBUG
17156
17157 /* Either there is no unchanged row at the end, or the one we have
17158 now displays text. This is a necessary condition for the window
17159 end pos calculation at the end of this function. */
17160 xassert (first_unchanged_at_end_row == NULL
17161 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17162
17163 debug_last_unchanged_at_beg_vpos
17164 = (last_unchanged_at_beg_row
17165 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17166 : -1);
17167 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17168
17169 #endif /* GLYPH_DEBUG != 0 */
17170
17171
17172 /* Display new lines. Set last_text_row to the last new line
17173 displayed which has text on it, i.e. might end up as being the
17174 line where the window_end_vpos is. */
17175 w->cursor.vpos = -1;
17176 last_text_row = NULL;
17177 overlay_arrow_seen = 0;
17178 while (it.current_y < it.last_visible_y
17179 && !fonts_changed_p
17180 && (first_unchanged_at_end_row == NULL
17181 || IT_CHARPOS (it) < stop_pos))
17182 {
17183 if (display_line (&it))
17184 last_text_row = it.glyph_row - 1;
17185 }
17186
17187 if (fonts_changed_p)
17188 return -1;
17189
17190
17191 /* Compute differences in buffer positions, y-positions etc. for
17192 lines reused at the bottom of the window. Compute what we can
17193 scroll. */
17194 if (first_unchanged_at_end_row
17195 /* No lines reused because we displayed everything up to the
17196 bottom of the window. */
17197 && it.current_y < it.last_visible_y)
17198 {
17199 dvpos = (it.vpos
17200 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17201 current_matrix));
17202 dy = it.current_y - first_unchanged_at_end_row->y;
17203 run.current_y = first_unchanged_at_end_row->y;
17204 run.desired_y = run.current_y + dy;
17205 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17206 }
17207 else
17208 {
17209 delta = delta_bytes = dvpos = dy
17210 = run.current_y = run.desired_y = run.height = 0;
17211 first_unchanged_at_end_row = NULL;
17212 }
17213 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17214
17215
17216 /* Find the cursor if not already found. We have to decide whether
17217 PT will appear on this window (it sometimes doesn't, but this is
17218 not a very frequent case.) This decision has to be made before
17219 the current matrix is altered. A value of cursor.vpos < 0 means
17220 that PT is either in one of the lines beginning at
17221 first_unchanged_at_end_row or below the window. Don't care for
17222 lines that might be displayed later at the window end; as
17223 mentioned, this is not a frequent case. */
17224 if (w->cursor.vpos < 0)
17225 {
17226 /* Cursor in unchanged rows at the top? */
17227 if (PT < CHARPOS (start_pos)
17228 && last_unchanged_at_beg_row)
17229 {
17230 row = row_containing_pos (w, PT,
17231 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17232 last_unchanged_at_beg_row + 1, 0);
17233 if (row)
17234 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17235 }
17236
17237 /* Start from first_unchanged_at_end_row looking for PT. */
17238 else if (first_unchanged_at_end_row)
17239 {
17240 row = row_containing_pos (w, PT - delta,
17241 first_unchanged_at_end_row, NULL, 0);
17242 if (row)
17243 set_cursor_from_row (w, row, w->current_matrix, delta,
17244 delta_bytes, dy, dvpos);
17245 }
17246
17247 /* Give up if cursor was not found. */
17248 if (w->cursor.vpos < 0)
17249 {
17250 clear_glyph_matrix (w->desired_matrix);
17251 return -1;
17252 }
17253 }
17254
17255 /* Don't let the cursor end in the scroll margins. */
17256 {
17257 int this_scroll_margin, cursor_height;
17258
17259 this_scroll_margin =
17260 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17261 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17262 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17263
17264 if ((w->cursor.y < this_scroll_margin
17265 && CHARPOS (start) > BEGV)
17266 /* Old redisplay didn't take scroll margin into account at the bottom,
17267 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17268 || (w->cursor.y + (make_cursor_line_fully_visible_p
17269 ? cursor_height + this_scroll_margin
17270 : 1)) > it.last_visible_y)
17271 {
17272 w->cursor.vpos = -1;
17273 clear_glyph_matrix (w->desired_matrix);
17274 return -1;
17275 }
17276 }
17277
17278 /* Scroll the display. Do it before changing the current matrix so
17279 that xterm.c doesn't get confused about where the cursor glyph is
17280 found. */
17281 if (dy && run.height)
17282 {
17283 update_begin (f);
17284
17285 if (FRAME_WINDOW_P (f))
17286 {
17287 FRAME_RIF (f)->update_window_begin_hook (w);
17288 FRAME_RIF (f)->clear_window_mouse_face (w);
17289 FRAME_RIF (f)->scroll_run_hook (w, &run);
17290 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17291 }
17292 else
17293 {
17294 /* Terminal frame. In this case, dvpos gives the number of
17295 lines to scroll by; dvpos < 0 means scroll up. */
17296 int from_vpos
17297 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17298 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17299 int end = (WINDOW_TOP_EDGE_LINE (w)
17300 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17301 + window_internal_height (w));
17302
17303 #if defined (HAVE_GPM) || defined (MSDOS)
17304 x_clear_window_mouse_face (w);
17305 #endif
17306 /* Perform the operation on the screen. */
17307 if (dvpos > 0)
17308 {
17309 /* Scroll last_unchanged_at_beg_row to the end of the
17310 window down dvpos lines. */
17311 set_terminal_window (f, end);
17312
17313 /* On dumb terminals delete dvpos lines at the end
17314 before inserting dvpos empty lines. */
17315 if (!FRAME_SCROLL_REGION_OK (f))
17316 ins_del_lines (f, end - dvpos, -dvpos);
17317
17318 /* Insert dvpos empty lines in front of
17319 last_unchanged_at_beg_row. */
17320 ins_del_lines (f, from, dvpos);
17321 }
17322 else if (dvpos < 0)
17323 {
17324 /* Scroll up last_unchanged_at_beg_vpos to the end of
17325 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17326 set_terminal_window (f, end);
17327
17328 /* Delete dvpos lines in front of
17329 last_unchanged_at_beg_vpos. ins_del_lines will set
17330 the cursor to the given vpos and emit |dvpos| delete
17331 line sequences. */
17332 ins_del_lines (f, from + dvpos, dvpos);
17333
17334 /* On a dumb terminal insert dvpos empty lines at the
17335 end. */
17336 if (!FRAME_SCROLL_REGION_OK (f))
17337 ins_del_lines (f, end + dvpos, -dvpos);
17338 }
17339
17340 set_terminal_window (f, 0);
17341 }
17342
17343 update_end (f);
17344 }
17345
17346 /* Shift reused rows of the current matrix to the right position.
17347 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17348 text. */
17349 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17350 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17351 if (dvpos < 0)
17352 {
17353 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17354 bottom_vpos, dvpos);
17355 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17356 bottom_vpos, 0);
17357 }
17358 else if (dvpos > 0)
17359 {
17360 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17361 bottom_vpos, dvpos);
17362 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17363 first_unchanged_at_end_vpos + dvpos, 0);
17364 }
17365
17366 /* For frame-based redisplay, make sure that current frame and window
17367 matrix are in sync with respect to glyph memory. */
17368 if (!FRAME_WINDOW_P (f))
17369 sync_frame_with_window_matrix_rows (w);
17370
17371 /* Adjust buffer positions in reused rows. */
17372 if (delta || delta_bytes)
17373 increment_matrix_positions (current_matrix,
17374 first_unchanged_at_end_vpos + dvpos,
17375 bottom_vpos, delta, delta_bytes);
17376
17377 /* Adjust Y positions. */
17378 if (dy)
17379 shift_glyph_matrix (w, current_matrix,
17380 first_unchanged_at_end_vpos + dvpos,
17381 bottom_vpos, dy);
17382
17383 if (first_unchanged_at_end_row)
17384 {
17385 first_unchanged_at_end_row += dvpos;
17386 if (first_unchanged_at_end_row->y >= it.last_visible_y
17387 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17388 first_unchanged_at_end_row = NULL;
17389 }
17390
17391 /* If scrolling up, there may be some lines to display at the end of
17392 the window. */
17393 last_text_row_at_end = NULL;
17394 if (dy < 0)
17395 {
17396 /* Scrolling up can leave for example a partially visible line
17397 at the end of the window to be redisplayed. */
17398 /* Set last_row to the glyph row in the current matrix where the
17399 window end line is found. It has been moved up or down in
17400 the matrix by dvpos. */
17401 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17402 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17403
17404 /* If last_row is the window end line, it should display text. */
17405 xassert (last_row->displays_text_p);
17406
17407 /* If window end line was partially visible before, begin
17408 displaying at that line. Otherwise begin displaying with the
17409 line following it. */
17410 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17411 {
17412 init_to_row_start (&it, w, last_row);
17413 it.vpos = last_vpos;
17414 it.current_y = last_row->y;
17415 }
17416 else
17417 {
17418 init_to_row_end (&it, w, last_row);
17419 it.vpos = 1 + last_vpos;
17420 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17421 ++last_row;
17422 }
17423
17424 /* We may start in a continuation line. If so, we have to
17425 get the right continuation_lines_width and current_x. */
17426 it.continuation_lines_width = last_row->continuation_lines_width;
17427 it.hpos = it.current_x = 0;
17428
17429 /* Display the rest of the lines at the window end. */
17430 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17431 while (it.current_y < it.last_visible_y
17432 && !fonts_changed_p)
17433 {
17434 /* Is it always sure that the display agrees with lines in
17435 the current matrix? I don't think so, so we mark rows
17436 displayed invalid in the current matrix by setting their
17437 enabled_p flag to zero. */
17438 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17439 if (display_line (&it))
17440 last_text_row_at_end = it.glyph_row - 1;
17441 }
17442 }
17443
17444 /* Update window_end_pos and window_end_vpos. */
17445 if (first_unchanged_at_end_row
17446 && !last_text_row_at_end)
17447 {
17448 /* Window end line if one of the preserved rows from the current
17449 matrix. Set row to the last row displaying text in current
17450 matrix starting at first_unchanged_at_end_row, after
17451 scrolling. */
17452 xassert (first_unchanged_at_end_row->displays_text_p);
17453 row = find_last_row_displaying_text (w->current_matrix, &it,
17454 first_unchanged_at_end_row);
17455 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17456
17457 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17458 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17459 w->window_end_vpos
17460 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17461 xassert (w->window_end_bytepos >= 0);
17462 IF_DEBUG (debug_method_add (w, "A"));
17463 }
17464 else if (last_text_row_at_end)
17465 {
17466 w->window_end_pos
17467 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17468 w->window_end_bytepos
17469 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17470 w->window_end_vpos
17471 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17472 xassert (w->window_end_bytepos >= 0);
17473 IF_DEBUG (debug_method_add (w, "B"));
17474 }
17475 else if (last_text_row)
17476 {
17477 /* We have displayed either to the end of the window or at the
17478 end of the window, i.e. the last row with text is to be found
17479 in the desired matrix. */
17480 w->window_end_pos
17481 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17482 w->window_end_bytepos
17483 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17484 w->window_end_vpos
17485 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17486 xassert (w->window_end_bytepos >= 0);
17487 }
17488 else if (first_unchanged_at_end_row == NULL
17489 && last_text_row == NULL
17490 && last_text_row_at_end == NULL)
17491 {
17492 /* Displayed to end of window, but no line containing text was
17493 displayed. Lines were deleted at the end of the window. */
17494 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17495 int vpos = XFASTINT (w->window_end_vpos);
17496 struct glyph_row *current_row = current_matrix->rows + vpos;
17497 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17498
17499 for (row = NULL;
17500 row == NULL && vpos >= first_vpos;
17501 --vpos, --current_row, --desired_row)
17502 {
17503 if (desired_row->enabled_p)
17504 {
17505 if (desired_row->displays_text_p)
17506 row = desired_row;
17507 }
17508 else if (current_row->displays_text_p)
17509 row = current_row;
17510 }
17511
17512 xassert (row != NULL);
17513 w->window_end_vpos = make_number (vpos + 1);
17514 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17515 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17516 xassert (w->window_end_bytepos >= 0);
17517 IF_DEBUG (debug_method_add (w, "C"));
17518 }
17519 else
17520 abort ();
17521
17522 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17523 debug_end_vpos = XFASTINT (w->window_end_vpos));
17524
17525 /* Record that display has not been completed. */
17526 w->window_end_valid = Qnil;
17527 w->desired_matrix->no_scrolling_p = 1;
17528 return 3;
17529
17530 #undef GIVE_UP
17531 }
17532
17533
17534 \f
17535 /***********************************************************************
17536 More debugging support
17537 ***********************************************************************/
17538
17539 #if GLYPH_DEBUG
17540
17541 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17542 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17543 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17544
17545
17546 /* Dump the contents of glyph matrix MATRIX on stderr.
17547
17548 GLYPHS 0 means don't show glyph contents.
17549 GLYPHS 1 means show glyphs in short form
17550 GLYPHS > 1 means show glyphs in long form. */
17551
17552 void
17553 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17554 {
17555 int i;
17556 for (i = 0; i < matrix->nrows; ++i)
17557 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17558 }
17559
17560
17561 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17562 the glyph row and area where the glyph comes from. */
17563
17564 void
17565 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17566 {
17567 if (glyph->type == CHAR_GLYPH)
17568 {
17569 fprintf (stderr,
17570 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17571 glyph - row->glyphs[TEXT_AREA],
17572 'C',
17573 glyph->charpos,
17574 (BUFFERP (glyph->object)
17575 ? 'B'
17576 : (STRINGP (glyph->object)
17577 ? 'S'
17578 : '-')),
17579 glyph->pixel_width,
17580 glyph->u.ch,
17581 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17582 ? glyph->u.ch
17583 : '.'),
17584 glyph->face_id,
17585 glyph->left_box_line_p,
17586 glyph->right_box_line_p);
17587 }
17588 else if (glyph->type == STRETCH_GLYPH)
17589 {
17590 fprintf (stderr,
17591 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17592 glyph - row->glyphs[TEXT_AREA],
17593 'S',
17594 glyph->charpos,
17595 (BUFFERP (glyph->object)
17596 ? 'B'
17597 : (STRINGP (glyph->object)
17598 ? 'S'
17599 : '-')),
17600 glyph->pixel_width,
17601 0,
17602 '.',
17603 glyph->face_id,
17604 glyph->left_box_line_p,
17605 glyph->right_box_line_p);
17606 }
17607 else if (glyph->type == IMAGE_GLYPH)
17608 {
17609 fprintf (stderr,
17610 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17611 glyph - row->glyphs[TEXT_AREA],
17612 'I',
17613 glyph->charpos,
17614 (BUFFERP (glyph->object)
17615 ? 'B'
17616 : (STRINGP (glyph->object)
17617 ? 'S'
17618 : '-')),
17619 glyph->pixel_width,
17620 glyph->u.img_id,
17621 '.',
17622 glyph->face_id,
17623 glyph->left_box_line_p,
17624 glyph->right_box_line_p);
17625 }
17626 else if (glyph->type == COMPOSITE_GLYPH)
17627 {
17628 fprintf (stderr,
17629 " %5td %4c %6"pI"d %c %3d 0x%05x",
17630 glyph - row->glyphs[TEXT_AREA],
17631 '+',
17632 glyph->charpos,
17633 (BUFFERP (glyph->object)
17634 ? 'B'
17635 : (STRINGP (glyph->object)
17636 ? 'S'
17637 : '-')),
17638 glyph->pixel_width,
17639 glyph->u.cmp.id);
17640 if (glyph->u.cmp.automatic)
17641 fprintf (stderr,
17642 "[%d-%d]",
17643 glyph->slice.cmp.from, glyph->slice.cmp.to);
17644 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17645 glyph->face_id,
17646 glyph->left_box_line_p,
17647 glyph->right_box_line_p);
17648 }
17649 }
17650
17651
17652 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17653 GLYPHS 0 means don't show glyph contents.
17654 GLYPHS 1 means show glyphs in short form
17655 GLYPHS > 1 means show glyphs in long form. */
17656
17657 void
17658 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17659 {
17660 if (glyphs != 1)
17661 {
17662 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17663 fprintf (stderr, "======================================================================\n");
17664
17665 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17666 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17667 vpos,
17668 MATRIX_ROW_START_CHARPOS (row),
17669 MATRIX_ROW_END_CHARPOS (row),
17670 row->used[TEXT_AREA],
17671 row->contains_overlapping_glyphs_p,
17672 row->enabled_p,
17673 row->truncated_on_left_p,
17674 row->truncated_on_right_p,
17675 row->continued_p,
17676 MATRIX_ROW_CONTINUATION_LINE_P (row),
17677 row->displays_text_p,
17678 row->ends_at_zv_p,
17679 row->fill_line_p,
17680 row->ends_in_middle_of_char_p,
17681 row->starts_in_middle_of_char_p,
17682 row->mouse_face_p,
17683 row->x,
17684 row->y,
17685 row->pixel_width,
17686 row->height,
17687 row->visible_height,
17688 row->ascent,
17689 row->phys_ascent);
17690 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
17691 row->end.overlay_string_index,
17692 row->continuation_lines_width);
17693 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17694 CHARPOS (row->start.string_pos),
17695 CHARPOS (row->end.string_pos));
17696 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17697 row->end.dpvec_index);
17698 }
17699
17700 if (glyphs > 1)
17701 {
17702 int area;
17703
17704 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17705 {
17706 struct glyph *glyph = row->glyphs[area];
17707 struct glyph *glyph_end = glyph + row->used[area];
17708
17709 /* Glyph for a line end in text. */
17710 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17711 ++glyph_end;
17712
17713 if (glyph < glyph_end)
17714 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17715
17716 for (; glyph < glyph_end; ++glyph)
17717 dump_glyph (row, glyph, area);
17718 }
17719 }
17720 else if (glyphs == 1)
17721 {
17722 int area;
17723
17724 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17725 {
17726 char *s = (char *) alloca (row->used[area] + 1);
17727 int i;
17728
17729 for (i = 0; i < row->used[area]; ++i)
17730 {
17731 struct glyph *glyph = row->glyphs[area] + i;
17732 if (glyph->type == CHAR_GLYPH
17733 && glyph->u.ch < 0x80
17734 && glyph->u.ch >= ' ')
17735 s[i] = glyph->u.ch;
17736 else
17737 s[i] = '.';
17738 }
17739
17740 s[i] = '\0';
17741 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17742 }
17743 }
17744 }
17745
17746
17747 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17748 Sdump_glyph_matrix, 0, 1, "p",
17749 doc: /* Dump the current matrix of the selected window to stderr.
17750 Shows contents of glyph row structures. With non-nil
17751 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17752 glyphs in short form, otherwise show glyphs in long form. */)
17753 (Lisp_Object glyphs)
17754 {
17755 struct window *w = XWINDOW (selected_window);
17756 struct buffer *buffer = XBUFFER (w->buffer);
17757
17758 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17759 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17760 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17761 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17762 fprintf (stderr, "=============================================\n");
17763 dump_glyph_matrix (w->current_matrix,
17764 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
17765 return Qnil;
17766 }
17767
17768
17769 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17770 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17771 (void)
17772 {
17773 struct frame *f = XFRAME (selected_frame);
17774 dump_glyph_matrix (f->current_matrix, 1);
17775 return Qnil;
17776 }
17777
17778
17779 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17780 doc: /* Dump glyph row ROW to stderr.
17781 GLYPH 0 means don't dump glyphs.
17782 GLYPH 1 means dump glyphs in short form.
17783 GLYPH > 1 or omitted means dump glyphs in long form. */)
17784 (Lisp_Object row, Lisp_Object glyphs)
17785 {
17786 struct glyph_matrix *matrix;
17787 EMACS_INT vpos;
17788
17789 CHECK_NUMBER (row);
17790 matrix = XWINDOW (selected_window)->current_matrix;
17791 vpos = XINT (row);
17792 if (vpos >= 0 && vpos < matrix->nrows)
17793 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17794 vpos,
17795 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
17796 return Qnil;
17797 }
17798
17799
17800 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17801 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17802 GLYPH 0 means don't dump glyphs.
17803 GLYPH 1 means dump glyphs in short form.
17804 GLYPH > 1 or omitted means dump glyphs in long form. */)
17805 (Lisp_Object row, Lisp_Object glyphs)
17806 {
17807 struct frame *sf = SELECTED_FRAME ();
17808 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
17809 EMACS_INT vpos;
17810
17811 CHECK_NUMBER (row);
17812 vpos = XINT (row);
17813 if (vpos >= 0 && vpos < m->nrows)
17814 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
17815 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
17816 return Qnil;
17817 }
17818
17819
17820 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
17821 doc: /* Toggle tracing of redisplay.
17822 With ARG, turn tracing on if and only if ARG is positive. */)
17823 (Lisp_Object arg)
17824 {
17825 if (NILP (arg))
17826 trace_redisplay_p = !trace_redisplay_p;
17827 else
17828 {
17829 arg = Fprefix_numeric_value (arg);
17830 trace_redisplay_p = XINT (arg) > 0;
17831 }
17832
17833 return Qnil;
17834 }
17835
17836
17837 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
17838 doc: /* Like `format', but print result to stderr.
17839 usage: (trace-to-stderr STRING &rest OBJECTS) */)
17840 (ptrdiff_t nargs, Lisp_Object *args)
17841 {
17842 Lisp_Object s = Fformat (nargs, args);
17843 fprintf (stderr, "%s", SDATA (s));
17844 return Qnil;
17845 }
17846
17847 #endif /* GLYPH_DEBUG */
17848
17849
17850 \f
17851 /***********************************************************************
17852 Building Desired Matrix Rows
17853 ***********************************************************************/
17854
17855 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
17856 Used for non-window-redisplay windows, and for windows w/o left fringe. */
17857
17858 static struct glyph_row *
17859 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
17860 {
17861 struct frame *f = XFRAME (WINDOW_FRAME (w));
17862 struct buffer *buffer = XBUFFER (w->buffer);
17863 struct buffer *old = current_buffer;
17864 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
17865 int arrow_len = SCHARS (overlay_arrow_string);
17866 const unsigned char *arrow_end = arrow_string + arrow_len;
17867 const unsigned char *p;
17868 struct it it;
17869 int multibyte_p;
17870 int n_glyphs_before;
17871
17872 set_buffer_temp (buffer);
17873 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
17874 it.glyph_row->used[TEXT_AREA] = 0;
17875 SET_TEXT_POS (it.position, 0, 0);
17876
17877 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
17878 p = arrow_string;
17879 while (p < arrow_end)
17880 {
17881 Lisp_Object face, ilisp;
17882
17883 /* Get the next character. */
17884 if (multibyte_p)
17885 it.c = it.char_to_display = string_char_and_length (p, &it.len);
17886 else
17887 {
17888 it.c = it.char_to_display = *p, it.len = 1;
17889 if (! ASCII_CHAR_P (it.c))
17890 it.char_to_display = BYTE8_TO_CHAR (it.c);
17891 }
17892 p += it.len;
17893
17894 /* Get its face. */
17895 ilisp = make_number (p - arrow_string);
17896 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
17897 it.face_id = compute_char_face (f, it.char_to_display, face);
17898
17899 /* Compute its width, get its glyphs. */
17900 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
17901 SET_TEXT_POS (it.position, -1, -1);
17902 PRODUCE_GLYPHS (&it);
17903
17904 /* If this character doesn't fit any more in the line, we have
17905 to remove some glyphs. */
17906 if (it.current_x > it.last_visible_x)
17907 {
17908 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
17909 break;
17910 }
17911 }
17912
17913 set_buffer_temp (old);
17914 return it.glyph_row;
17915 }
17916
17917
17918 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
17919 glyphs are only inserted for terminal frames since we can't really
17920 win with truncation glyphs when partially visible glyphs are
17921 involved. Which glyphs to insert is determined by
17922 produce_special_glyphs. */
17923
17924 static void
17925 insert_left_trunc_glyphs (struct it *it)
17926 {
17927 struct it truncate_it;
17928 struct glyph *from, *end, *to, *toend;
17929
17930 xassert (!FRAME_WINDOW_P (it->f));
17931
17932 /* Get the truncation glyphs. */
17933 truncate_it = *it;
17934 truncate_it.current_x = 0;
17935 truncate_it.face_id = DEFAULT_FACE_ID;
17936 truncate_it.glyph_row = &scratch_glyph_row;
17937 truncate_it.glyph_row->used[TEXT_AREA] = 0;
17938 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
17939 truncate_it.object = make_number (0);
17940 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
17941
17942 /* Overwrite glyphs from IT with truncation glyphs. */
17943 if (!it->glyph_row->reversed_p)
17944 {
17945 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17946 end = from + truncate_it.glyph_row->used[TEXT_AREA];
17947 to = it->glyph_row->glyphs[TEXT_AREA];
17948 toend = to + it->glyph_row->used[TEXT_AREA];
17949
17950 while (from < end)
17951 *to++ = *from++;
17952
17953 /* There may be padding glyphs left over. Overwrite them too. */
17954 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
17955 {
17956 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
17957 while (from < end)
17958 *to++ = *from++;
17959 }
17960
17961 if (to > toend)
17962 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
17963 }
17964 else
17965 {
17966 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
17967 that back to front. */
17968 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
17969 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17970 toend = it->glyph_row->glyphs[TEXT_AREA];
17971 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
17972
17973 while (from >= end && to >= toend)
17974 *to-- = *from--;
17975 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
17976 {
17977 from =
17978 truncate_it.glyph_row->glyphs[TEXT_AREA]
17979 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
17980 while (from >= end && to >= toend)
17981 *to-- = *from--;
17982 }
17983 if (from >= end)
17984 {
17985 /* Need to free some room before prepending additional
17986 glyphs. */
17987 int move_by = from - end + 1;
17988 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
17989 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
17990
17991 for ( ; g >= g0; g--)
17992 g[move_by] = *g;
17993 while (from >= end)
17994 *to-- = *from--;
17995 it->glyph_row->used[TEXT_AREA] += move_by;
17996 }
17997 }
17998 }
17999
18000 /* Compute the hash code for ROW. */
18001 unsigned
18002 row_hash (struct glyph_row *row)
18003 {
18004 int area, k;
18005 unsigned hashval = 0;
18006
18007 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18008 for (k = 0; k < row->used[area]; ++k)
18009 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18010 + row->glyphs[area][k].u.val
18011 + row->glyphs[area][k].face_id
18012 + row->glyphs[area][k].padding_p
18013 + (row->glyphs[area][k].type << 2));
18014
18015 return hashval;
18016 }
18017
18018 /* Compute the pixel height and width of IT->glyph_row.
18019
18020 Most of the time, ascent and height of a display line will be equal
18021 to the max_ascent and max_height values of the display iterator
18022 structure. This is not the case if
18023
18024 1. We hit ZV without displaying anything. In this case, max_ascent
18025 and max_height will be zero.
18026
18027 2. We have some glyphs that don't contribute to the line height.
18028 (The glyph row flag contributes_to_line_height_p is for future
18029 pixmap extensions).
18030
18031 The first case is easily covered by using default values because in
18032 these cases, the line height does not really matter, except that it
18033 must not be zero. */
18034
18035 static void
18036 compute_line_metrics (struct it *it)
18037 {
18038 struct glyph_row *row = it->glyph_row;
18039
18040 if (FRAME_WINDOW_P (it->f))
18041 {
18042 int i, min_y, max_y;
18043
18044 /* The line may consist of one space only, that was added to
18045 place the cursor on it. If so, the row's height hasn't been
18046 computed yet. */
18047 if (row->height == 0)
18048 {
18049 if (it->max_ascent + it->max_descent == 0)
18050 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18051 row->ascent = it->max_ascent;
18052 row->height = it->max_ascent + it->max_descent;
18053 row->phys_ascent = it->max_phys_ascent;
18054 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18055 row->extra_line_spacing = it->max_extra_line_spacing;
18056 }
18057
18058 /* Compute the width of this line. */
18059 row->pixel_width = row->x;
18060 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18061 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18062
18063 xassert (row->pixel_width >= 0);
18064 xassert (row->ascent >= 0 && row->height > 0);
18065
18066 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18067 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18068
18069 /* If first line's physical ascent is larger than its logical
18070 ascent, use the physical ascent, and make the row taller.
18071 This makes accented characters fully visible. */
18072 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18073 && row->phys_ascent > row->ascent)
18074 {
18075 row->height += row->phys_ascent - row->ascent;
18076 row->ascent = row->phys_ascent;
18077 }
18078
18079 /* Compute how much of the line is visible. */
18080 row->visible_height = row->height;
18081
18082 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18083 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18084
18085 if (row->y < min_y)
18086 row->visible_height -= min_y - row->y;
18087 if (row->y + row->height > max_y)
18088 row->visible_height -= row->y + row->height - max_y;
18089 }
18090 else
18091 {
18092 row->pixel_width = row->used[TEXT_AREA];
18093 if (row->continued_p)
18094 row->pixel_width -= it->continuation_pixel_width;
18095 else if (row->truncated_on_right_p)
18096 row->pixel_width -= it->truncation_pixel_width;
18097 row->ascent = row->phys_ascent = 0;
18098 row->height = row->phys_height = row->visible_height = 1;
18099 row->extra_line_spacing = 0;
18100 }
18101
18102 /* Compute a hash code for this row. */
18103 row->hash = row_hash (row);
18104
18105 it->max_ascent = it->max_descent = 0;
18106 it->max_phys_ascent = it->max_phys_descent = 0;
18107 }
18108
18109
18110 /* Append one space to the glyph row of iterator IT if doing a
18111 window-based redisplay. The space has the same face as
18112 IT->face_id. Value is non-zero if a space was added.
18113
18114 This function is called to make sure that there is always one glyph
18115 at the end of a glyph row that the cursor can be set on under
18116 window-systems. (If there weren't such a glyph we would not know
18117 how wide and tall a box cursor should be displayed).
18118
18119 At the same time this space let's a nicely handle clearing to the
18120 end of the line if the row ends in italic text. */
18121
18122 static int
18123 append_space_for_newline (struct it *it, int default_face_p)
18124 {
18125 if (FRAME_WINDOW_P (it->f))
18126 {
18127 int n = it->glyph_row->used[TEXT_AREA];
18128
18129 if (it->glyph_row->glyphs[TEXT_AREA] + n
18130 < it->glyph_row->glyphs[1 + TEXT_AREA])
18131 {
18132 /* Save some values that must not be changed.
18133 Must save IT->c and IT->len because otherwise
18134 ITERATOR_AT_END_P wouldn't work anymore after
18135 append_space_for_newline has been called. */
18136 enum display_element_type saved_what = it->what;
18137 int saved_c = it->c, saved_len = it->len;
18138 int saved_char_to_display = it->char_to_display;
18139 int saved_x = it->current_x;
18140 int saved_face_id = it->face_id;
18141 struct text_pos saved_pos;
18142 Lisp_Object saved_object;
18143 struct face *face;
18144
18145 saved_object = it->object;
18146 saved_pos = it->position;
18147
18148 it->what = IT_CHARACTER;
18149 memset (&it->position, 0, sizeof it->position);
18150 it->object = make_number (0);
18151 it->c = it->char_to_display = ' ';
18152 it->len = 1;
18153
18154 if (default_face_p)
18155 it->face_id = DEFAULT_FACE_ID;
18156 else if (it->face_before_selective_p)
18157 it->face_id = it->saved_face_id;
18158 face = FACE_FROM_ID (it->f, it->face_id);
18159 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18160
18161 PRODUCE_GLYPHS (it);
18162
18163 it->override_ascent = -1;
18164 it->constrain_row_ascent_descent_p = 0;
18165 it->current_x = saved_x;
18166 it->object = saved_object;
18167 it->position = saved_pos;
18168 it->what = saved_what;
18169 it->face_id = saved_face_id;
18170 it->len = saved_len;
18171 it->c = saved_c;
18172 it->char_to_display = saved_char_to_display;
18173 return 1;
18174 }
18175 }
18176
18177 return 0;
18178 }
18179
18180
18181 /* Extend the face of the last glyph in the text area of IT->glyph_row
18182 to the end of the display line. Called from display_line. If the
18183 glyph row is empty, add a space glyph to it so that we know the
18184 face to draw. Set the glyph row flag fill_line_p. If the glyph
18185 row is R2L, prepend a stretch glyph to cover the empty space to the
18186 left of the leftmost glyph. */
18187
18188 static void
18189 extend_face_to_end_of_line (struct it *it)
18190 {
18191 struct face *face;
18192 struct frame *f = it->f;
18193
18194 /* If line is already filled, do nothing. Non window-system frames
18195 get a grace of one more ``pixel'' because their characters are
18196 1-``pixel'' wide, so they hit the equality too early. This grace
18197 is needed only for R2L rows that are not continued, to produce
18198 one extra blank where we could display the cursor. */
18199 if (it->current_x >= it->last_visible_x
18200 + (!FRAME_WINDOW_P (f)
18201 && it->glyph_row->reversed_p
18202 && !it->glyph_row->continued_p))
18203 return;
18204
18205 /* Face extension extends the background and box of IT->face_id
18206 to the end of the line. If the background equals the background
18207 of the frame, we don't have to do anything. */
18208 if (it->face_before_selective_p)
18209 face = FACE_FROM_ID (f, it->saved_face_id);
18210 else
18211 face = FACE_FROM_ID (f, it->face_id);
18212
18213 if (FRAME_WINDOW_P (f)
18214 && it->glyph_row->displays_text_p
18215 && face->box == FACE_NO_BOX
18216 && face->background == FRAME_BACKGROUND_PIXEL (f)
18217 && !face->stipple
18218 && !it->glyph_row->reversed_p)
18219 return;
18220
18221 /* Set the glyph row flag indicating that the face of the last glyph
18222 in the text area has to be drawn to the end of the text area. */
18223 it->glyph_row->fill_line_p = 1;
18224
18225 /* If current character of IT is not ASCII, make sure we have the
18226 ASCII face. This will be automatically undone the next time
18227 get_next_display_element returns a multibyte character. Note
18228 that the character will always be single byte in unibyte
18229 text. */
18230 if (!ASCII_CHAR_P (it->c))
18231 {
18232 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18233 }
18234
18235 if (FRAME_WINDOW_P (f))
18236 {
18237 /* If the row is empty, add a space with the current face of IT,
18238 so that we know which face to draw. */
18239 if (it->glyph_row->used[TEXT_AREA] == 0)
18240 {
18241 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18242 it->glyph_row->glyphs[TEXT_AREA][0].face_id = it->face_id;
18243 it->glyph_row->used[TEXT_AREA] = 1;
18244 }
18245 #ifdef HAVE_WINDOW_SYSTEM
18246 if (it->glyph_row->reversed_p)
18247 {
18248 /* Prepend a stretch glyph to the row, such that the
18249 rightmost glyph will be drawn flushed all the way to the
18250 right margin of the window. The stretch glyph that will
18251 occupy the empty space, if any, to the left of the
18252 glyphs. */
18253 struct font *font = face->font ? face->font : FRAME_FONT (f);
18254 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18255 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18256 struct glyph *g;
18257 int row_width, stretch_ascent, stretch_width;
18258 struct text_pos saved_pos;
18259 int saved_face_id, saved_avoid_cursor;
18260
18261 for (row_width = 0, g = row_start; g < row_end; g++)
18262 row_width += g->pixel_width;
18263 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18264 if (stretch_width > 0)
18265 {
18266 stretch_ascent =
18267 (((it->ascent + it->descent)
18268 * FONT_BASE (font)) / FONT_HEIGHT (font));
18269 saved_pos = it->position;
18270 memset (&it->position, 0, sizeof it->position);
18271 saved_avoid_cursor = it->avoid_cursor_p;
18272 it->avoid_cursor_p = 1;
18273 saved_face_id = it->face_id;
18274 /* The last row's stretch glyph should get the default
18275 face, to avoid painting the rest of the window with
18276 the region face, if the region ends at ZV. */
18277 if (it->glyph_row->ends_at_zv_p)
18278 it->face_id = DEFAULT_FACE_ID;
18279 else
18280 it->face_id = face->id;
18281 append_stretch_glyph (it, make_number (0), stretch_width,
18282 it->ascent + it->descent, stretch_ascent);
18283 it->position = saved_pos;
18284 it->avoid_cursor_p = saved_avoid_cursor;
18285 it->face_id = saved_face_id;
18286 }
18287 }
18288 #endif /* HAVE_WINDOW_SYSTEM */
18289 }
18290 else
18291 {
18292 /* Save some values that must not be changed. */
18293 int saved_x = it->current_x;
18294 struct text_pos saved_pos;
18295 Lisp_Object saved_object;
18296 enum display_element_type saved_what = it->what;
18297 int saved_face_id = it->face_id;
18298
18299 saved_object = it->object;
18300 saved_pos = it->position;
18301
18302 it->what = IT_CHARACTER;
18303 memset (&it->position, 0, sizeof it->position);
18304 it->object = make_number (0);
18305 it->c = it->char_to_display = ' ';
18306 it->len = 1;
18307 /* The last row's blank glyphs should get the default face, to
18308 avoid painting the rest of the window with the region face,
18309 if the region ends at ZV. */
18310 if (it->glyph_row->ends_at_zv_p)
18311 it->face_id = DEFAULT_FACE_ID;
18312 else
18313 it->face_id = face->id;
18314
18315 PRODUCE_GLYPHS (it);
18316
18317 while (it->current_x <= it->last_visible_x)
18318 PRODUCE_GLYPHS (it);
18319
18320 /* Don't count these blanks really. It would let us insert a left
18321 truncation glyph below and make us set the cursor on them, maybe. */
18322 it->current_x = saved_x;
18323 it->object = saved_object;
18324 it->position = saved_pos;
18325 it->what = saved_what;
18326 it->face_id = saved_face_id;
18327 }
18328 }
18329
18330
18331 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18332 trailing whitespace. */
18333
18334 static int
18335 trailing_whitespace_p (ptrdiff_t charpos)
18336 {
18337 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18338 int c = 0;
18339
18340 while (bytepos < ZV_BYTE
18341 && (c = FETCH_CHAR (bytepos),
18342 c == ' ' || c == '\t'))
18343 ++bytepos;
18344
18345 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18346 {
18347 if (bytepos != PT_BYTE)
18348 return 1;
18349 }
18350 return 0;
18351 }
18352
18353
18354 /* Highlight trailing whitespace, if any, in ROW. */
18355
18356 static void
18357 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18358 {
18359 int used = row->used[TEXT_AREA];
18360
18361 if (used)
18362 {
18363 struct glyph *start = row->glyphs[TEXT_AREA];
18364 struct glyph *glyph = start + used - 1;
18365
18366 if (row->reversed_p)
18367 {
18368 /* Right-to-left rows need to be processed in the opposite
18369 direction, so swap the edge pointers. */
18370 glyph = start;
18371 start = row->glyphs[TEXT_AREA] + used - 1;
18372 }
18373
18374 /* Skip over glyphs inserted to display the cursor at the
18375 end of a line, for extending the face of the last glyph
18376 to the end of the line on terminals, and for truncation
18377 and continuation glyphs. */
18378 if (!row->reversed_p)
18379 {
18380 while (glyph >= start
18381 && glyph->type == CHAR_GLYPH
18382 && INTEGERP (glyph->object))
18383 --glyph;
18384 }
18385 else
18386 {
18387 while (glyph <= start
18388 && glyph->type == CHAR_GLYPH
18389 && INTEGERP (glyph->object))
18390 ++glyph;
18391 }
18392
18393 /* If last glyph is a space or stretch, and it's trailing
18394 whitespace, set the face of all trailing whitespace glyphs in
18395 IT->glyph_row to `trailing-whitespace'. */
18396 if ((row->reversed_p ? glyph <= start : glyph >= start)
18397 && BUFFERP (glyph->object)
18398 && (glyph->type == STRETCH_GLYPH
18399 || (glyph->type == CHAR_GLYPH
18400 && glyph->u.ch == ' '))
18401 && trailing_whitespace_p (glyph->charpos))
18402 {
18403 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18404 if (face_id < 0)
18405 return;
18406
18407 if (!row->reversed_p)
18408 {
18409 while (glyph >= start
18410 && BUFFERP (glyph->object)
18411 && (glyph->type == STRETCH_GLYPH
18412 || (glyph->type == CHAR_GLYPH
18413 && glyph->u.ch == ' ')))
18414 (glyph--)->face_id = face_id;
18415 }
18416 else
18417 {
18418 while (glyph <= start
18419 && BUFFERP (glyph->object)
18420 && (glyph->type == STRETCH_GLYPH
18421 || (glyph->type == CHAR_GLYPH
18422 && glyph->u.ch == ' ')))
18423 (glyph++)->face_id = face_id;
18424 }
18425 }
18426 }
18427 }
18428
18429
18430 /* Value is non-zero if glyph row ROW should be
18431 used to hold the cursor. */
18432
18433 static int
18434 cursor_row_p (struct glyph_row *row)
18435 {
18436 int result = 1;
18437
18438 if (PT == CHARPOS (row->end.pos)
18439 || PT == MATRIX_ROW_END_CHARPOS (row))
18440 {
18441 /* Suppose the row ends on a string.
18442 Unless the row is continued, that means it ends on a newline
18443 in the string. If it's anything other than a display string
18444 (e.g. a before-string from an overlay), we don't want the
18445 cursor there. (This heuristic seems to give the optimal
18446 behavior for the various types of multi-line strings.) */
18447 if (CHARPOS (row->end.string_pos) >= 0)
18448 {
18449 if (row->continued_p)
18450 result = 1;
18451 else
18452 {
18453 /* Check for `display' property. */
18454 struct glyph *beg = row->glyphs[TEXT_AREA];
18455 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18456 struct glyph *glyph;
18457
18458 result = 0;
18459 for (glyph = end; glyph >= beg; --glyph)
18460 if (STRINGP (glyph->object))
18461 {
18462 Lisp_Object prop
18463 = Fget_char_property (make_number (PT),
18464 Qdisplay, Qnil);
18465 result =
18466 (!NILP (prop)
18467 && display_prop_string_p (prop, glyph->object));
18468 break;
18469 }
18470 }
18471 }
18472 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18473 {
18474 /* If the row ends in middle of a real character,
18475 and the line is continued, we want the cursor here.
18476 That's because CHARPOS (ROW->end.pos) would equal
18477 PT if PT is before the character. */
18478 if (!row->ends_in_ellipsis_p)
18479 result = row->continued_p;
18480 else
18481 /* If the row ends in an ellipsis, then
18482 CHARPOS (ROW->end.pos) will equal point after the
18483 invisible text. We want that position to be displayed
18484 after the ellipsis. */
18485 result = 0;
18486 }
18487 /* If the row ends at ZV, display the cursor at the end of that
18488 row instead of at the start of the row below. */
18489 else if (row->ends_at_zv_p)
18490 result = 1;
18491 else
18492 result = 0;
18493 }
18494
18495 return result;
18496 }
18497
18498 \f
18499
18500 /* Push the property PROP so that it will be rendered at the current
18501 position in IT. Return 1 if PROP was successfully pushed, 0
18502 otherwise. Called from handle_line_prefix to handle the
18503 `line-prefix' and `wrap-prefix' properties. */
18504
18505 static int
18506 push_display_prop (struct it *it, Lisp_Object prop)
18507 {
18508 struct text_pos pos =
18509 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18510
18511 xassert (it->method == GET_FROM_BUFFER
18512 || it->method == GET_FROM_DISPLAY_VECTOR
18513 || it->method == GET_FROM_STRING);
18514
18515 /* We need to save the current buffer/string position, so it will be
18516 restored by pop_it, because iterate_out_of_display_property
18517 depends on that being set correctly, but some situations leave
18518 it->position not yet set when this function is called. */
18519 push_it (it, &pos);
18520
18521 if (STRINGP (prop))
18522 {
18523 if (SCHARS (prop) == 0)
18524 {
18525 pop_it (it);
18526 return 0;
18527 }
18528
18529 it->string = prop;
18530 it->multibyte_p = STRING_MULTIBYTE (it->string);
18531 it->current.overlay_string_index = -1;
18532 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18533 it->end_charpos = it->string_nchars = SCHARS (it->string);
18534 it->method = GET_FROM_STRING;
18535 it->stop_charpos = 0;
18536 it->prev_stop = 0;
18537 it->base_level_stop = 0;
18538
18539 /* Force paragraph direction to be that of the parent
18540 buffer/string. */
18541 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18542 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18543 else
18544 it->paragraph_embedding = L2R;
18545
18546 /* Set up the bidi iterator for this display string. */
18547 if (it->bidi_p)
18548 {
18549 it->bidi_it.string.lstring = it->string;
18550 it->bidi_it.string.s = NULL;
18551 it->bidi_it.string.schars = it->end_charpos;
18552 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18553 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18554 it->bidi_it.string.unibyte = !it->multibyte_p;
18555 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18556 }
18557 }
18558 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18559 {
18560 it->method = GET_FROM_STRETCH;
18561 it->object = prop;
18562 }
18563 #ifdef HAVE_WINDOW_SYSTEM
18564 else if (IMAGEP (prop))
18565 {
18566 it->what = IT_IMAGE;
18567 it->image_id = lookup_image (it->f, prop);
18568 it->method = GET_FROM_IMAGE;
18569 }
18570 #endif /* HAVE_WINDOW_SYSTEM */
18571 else
18572 {
18573 pop_it (it); /* bogus display property, give up */
18574 return 0;
18575 }
18576
18577 return 1;
18578 }
18579
18580 /* Return the character-property PROP at the current position in IT. */
18581
18582 static Lisp_Object
18583 get_it_property (struct it *it, Lisp_Object prop)
18584 {
18585 Lisp_Object position;
18586
18587 if (STRINGP (it->object))
18588 position = make_number (IT_STRING_CHARPOS (*it));
18589 else if (BUFFERP (it->object))
18590 position = make_number (IT_CHARPOS (*it));
18591 else
18592 return Qnil;
18593
18594 return Fget_char_property (position, prop, it->object);
18595 }
18596
18597 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18598
18599 static void
18600 handle_line_prefix (struct it *it)
18601 {
18602 Lisp_Object prefix;
18603
18604 if (it->continuation_lines_width > 0)
18605 {
18606 prefix = get_it_property (it, Qwrap_prefix);
18607 if (NILP (prefix))
18608 prefix = Vwrap_prefix;
18609 }
18610 else
18611 {
18612 prefix = get_it_property (it, Qline_prefix);
18613 if (NILP (prefix))
18614 prefix = Vline_prefix;
18615 }
18616 if (! NILP (prefix) && push_display_prop (it, prefix))
18617 {
18618 /* If the prefix is wider than the window, and we try to wrap
18619 it, it would acquire its own wrap prefix, and so on till the
18620 iterator stack overflows. So, don't wrap the prefix. */
18621 it->line_wrap = TRUNCATE;
18622 it->avoid_cursor_p = 1;
18623 }
18624 }
18625
18626 \f
18627
18628 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18629 only for R2L lines from display_line and display_string, when they
18630 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18631 the line/string needs to be continued on the next glyph row. */
18632 static void
18633 unproduce_glyphs (struct it *it, int n)
18634 {
18635 struct glyph *glyph, *end;
18636
18637 xassert (it->glyph_row);
18638 xassert (it->glyph_row->reversed_p);
18639 xassert (it->area == TEXT_AREA);
18640 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18641
18642 if (n > it->glyph_row->used[TEXT_AREA])
18643 n = it->glyph_row->used[TEXT_AREA];
18644 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18645 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18646 for ( ; glyph < end; glyph++)
18647 glyph[-n] = *glyph;
18648 }
18649
18650 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18651 and ROW->maxpos. */
18652 static void
18653 find_row_edges (struct it *it, struct glyph_row *row,
18654 ptrdiff_t min_pos, ptrdiff_t min_bpos,
18655 ptrdiff_t max_pos, ptrdiff_t max_bpos)
18656 {
18657 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18658 lines' rows is implemented for bidi-reordered rows. */
18659
18660 /* ROW->minpos is the value of min_pos, the minimal buffer position
18661 we have in ROW, or ROW->start.pos if that is smaller. */
18662 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18663 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18664 else
18665 /* We didn't find buffer positions smaller than ROW->start, or
18666 didn't find _any_ valid buffer positions in any of the glyphs,
18667 so we must trust the iterator's computed positions. */
18668 row->minpos = row->start.pos;
18669 if (max_pos <= 0)
18670 {
18671 max_pos = CHARPOS (it->current.pos);
18672 max_bpos = BYTEPOS (it->current.pos);
18673 }
18674
18675 /* Here are the various use-cases for ending the row, and the
18676 corresponding values for ROW->maxpos:
18677
18678 Line ends in a newline from buffer eol_pos + 1
18679 Line is continued from buffer max_pos + 1
18680 Line is truncated on right it->current.pos
18681 Line ends in a newline from string max_pos + 1(*)
18682 (*) + 1 only when line ends in a forward scan
18683 Line is continued from string max_pos
18684 Line is continued from display vector max_pos
18685 Line is entirely from a string min_pos == max_pos
18686 Line is entirely from a display vector min_pos == max_pos
18687 Line that ends at ZV ZV
18688
18689 If you discover other use-cases, please add them here as
18690 appropriate. */
18691 if (row->ends_at_zv_p)
18692 row->maxpos = it->current.pos;
18693 else if (row->used[TEXT_AREA])
18694 {
18695 int seen_this_string = 0;
18696 struct glyph_row *r1 = row - 1;
18697
18698 /* Did we see the same display string on the previous row? */
18699 if (STRINGP (it->object)
18700 /* this is not the first row */
18701 && row > it->w->desired_matrix->rows
18702 /* previous row is not the header line */
18703 && !r1->mode_line_p
18704 /* previous row also ends in a newline from a string */
18705 && r1->ends_in_newline_from_string_p)
18706 {
18707 struct glyph *start, *end;
18708
18709 /* Search for the last glyph of the previous row that came
18710 from buffer or string. Depending on whether the row is
18711 L2R or R2L, we need to process it front to back or the
18712 other way round. */
18713 if (!r1->reversed_p)
18714 {
18715 start = r1->glyphs[TEXT_AREA];
18716 end = start + r1->used[TEXT_AREA];
18717 /* Glyphs inserted by redisplay have an integer (zero)
18718 as their object. */
18719 while (end > start
18720 && INTEGERP ((end - 1)->object)
18721 && (end - 1)->charpos <= 0)
18722 --end;
18723 if (end > start)
18724 {
18725 if (EQ ((end - 1)->object, it->object))
18726 seen_this_string = 1;
18727 }
18728 else
18729 /* If all the glyphs of the previous row were inserted
18730 by redisplay, it means the previous row was
18731 produced from a single newline, which is only
18732 possible if that newline came from the same string
18733 as the one which produced this ROW. */
18734 seen_this_string = 1;
18735 }
18736 else
18737 {
18738 end = r1->glyphs[TEXT_AREA] - 1;
18739 start = end + r1->used[TEXT_AREA];
18740 while (end < start
18741 && INTEGERP ((end + 1)->object)
18742 && (end + 1)->charpos <= 0)
18743 ++end;
18744 if (end < start)
18745 {
18746 if (EQ ((end + 1)->object, it->object))
18747 seen_this_string = 1;
18748 }
18749 else
18750 seen_this_string = 1;
18751 }
18752 }
18753 /* Take note of each display string that covers a newline only
18754 once, the first time we see it. This is for when a display
18755 string includes more than one newline in it. */
18756 if (row->ends_in_newline_from_string_p && !seen_this_string)
18757 {
18758 /* If we were scanning the buffer forward when we displayed
18759 the string, we want to account for at least one buffer
18760 position that belongs to this row (position covered by
18761 the display string), so that cursor positioning will
18762 consider this row as a candidate when point is at the end
18763 of the visual line represented by this row. This is not
18764 required when scanning back, because max_pos will already
18765 have a much larger value. */
18766 if (CHARPOS (row->end.pos) > max_pos)
18767 INC_BOTH (max_pos, max_bpos);
18768 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18769 }
18770 else if (CHARPOS (it->eol_pos) > 0)
18771 SET_TEXT_POS (row->maxpos,
18772 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
18773 else if (row->continued_p)
18774 {
18775 /* If max_pos is different from IT's current position, it
18776 means IT->method does not belong to the display element
18777 at max_pos. However, it also means that the display
18778 element at max_pos was displayed in its entirety on this
18779 line, which is equivalent to saying that the next line
18780 starts at the next buffer position. */
18781 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
18782 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18783 else
18784 {
18785 INC_BOTH (max_pos, max_bpos);
18786 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
18787 }
18788 }
18789 else if (row->truncated_on_right_p)
18790 /* display_line already called reseat_at_next_visible_line_start,
18791 which puts the iterator at the beginning of the next line, in
18792 the logical order. */
18793 row->maxpos = it->current.pos;
18794 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
18795 /* A line that is entirely from a string/image/stretch... */
18796 row->maxpos = row->minpos;
18797 else
18798 abort ();
18799 }
18800 else
18801 row->maxpos = it->current.pos;
18802 }
18803
18804 /* Construct the glyph row IT->glyph_row in the desired matrix of
18805 IT->w from text at the current position of IT. See dispextern.h
18806 for an overview of struct it. Value is non-zero if
18807 IT->glyph_row displays text, as opposed to a line displaying ZV
18808 only. */
18809
18810 static int
18811 display_line (struct it *it)
18812 {
18813 struct glyph_row *row = it->glyph_row;
18814 Lisp_Object overlay_arrow_string;
18815 struct it wrap_it;
18816 void *wrap_data = NULL;
18817 int may_wrap = 0, wrap_x IF_LINT (= 0);
18818 int wrap_row_used = -1;
18819 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
18820 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
18821 int wrap_row_extra_line_spacing IF_LINT (= 0);
18822 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
18823 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
18824 int cvpos;
18825 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
18826 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
18827
18828 /* We always start displaying at hpos zero even if hscrolled. */
18829 xassert (it->hpos == 0 && it->current_x == 0);
18830
18831 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
18832 >= it->w->desired_matrix->nrows)
18833 {
18834 it->w->nrows_scale_factor++;
18835 fonts_changed_p = 1;
18836 return 0;
18837 }
18838
18839 /* Is IT->w showing the region? */
18840 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
18841
18842 /* Clear the result glyph row and enable it. */
18843 prepare_desired_row (row);
18844
18845 row->y = it->current_y;
18846 row->start = it->start;
18847 row->continuation_lines_width = it->continuation_lines_width;
18848 row->displays_text_p = 1;
18849 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
18850 it->starts_in_middle_of_char_p = 0;
18851
18852 /* Arrange the overlays nicely for our purposes. Usually, we call
18853 display_line on only one line at a time, in which case this
18854 can't really hurt too much, or we call it on lines which appear
18855 one after another in the buffer, in which case all calls to
18856 recenter_overlay_lists but the first will be pretty cheap. */
18857 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
18858
18859 /* Move over display elements that are not visible because we are
18860 hscrolled. This may stop at an x-position < IT->first_visible_x
18861 if the first glyph is partially visible or if we hit a line end. */
18862 if (it->current_x < it->first_visible_x)
18863 {
18864 this_line_min_pos = row->start.pos;
18865 move_it_in_display_line_to (it, ZV, it->first_visible_x,
18866 MOVE_TO_POS | MOVE_TO_X);
18867 /* Record the smallest positions seen while we moved over
18868 display elements that are not visible. This is needed by
18869 redisplay_internal for optimizing the case where the cursor
18870 stays inside the same line. The rest of this function only
18871 considers positions that are actually displayed, so
18872 RECORD_MAX_MIN_POS will not otherwise record positions that
18873 are hscrolled to the left of the left edge of the window. */
18874 min_pos = CHARPOS (this_line_min_pos);
18875 min_bpos = BYTEPOS (this_line_min_pos);
18876 }
18877 else
18878 {
18879 /* We only do this when not calling `move_it_in_display_line_to'
18880 above, because move_it_in_display_line_to calls
18881 handle_line_prefix itself. */
18882 handle_line_prefix (it);
18883 }
18884
18885 /* Get the initial row height. This is either the height of the
18886 text hscrolled, if there is any, or zero. */
18887 row->ascent = it->max_ascent;
18888 row->height = it->max_ascent + it->max_descent;
18889 row->phys_ascent = it->max_phys_ascent;
18890 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18891 row->extra_line_spacing = it->max_extra_line_spacing;
18892
18893 /* Utility macro to record max and min buffer positions seen until now. */
18894 #define RECORD_MAX_MIN_POS(IT) \
18895 do \
18896 { \
18897 int composition_p = !STRINGP ((IT)->string) \
18898 && ((IT)->what == IT_COMPOSITION); \
18899 ptrdiff_t current_pos = \
18900 composition_p ? (IT)->cmp_it.charpos \
18901 : IT_CHARPOS (*(IT)); \
18902 ptrdiff_t current_bpos = \
18903 composition_p ? CHAR_TO_BYTE (current_pos) \
18904 : IT_BYTEPOS (*(IT)); \
18905 if (current_pos < min_pos) \
18906 { \
18907 min_pos = current_pos; \
18908 min_bpos = current_bpos; \
18909 } \
18910 if (IT_CHARPOS (*it) > max_pos) \
18911 { \
18912 max_pos = IT_CHARPOS (*it); \
18913 max_bpos = IT_BYTEPOS (*it); \
18914 } \
18915 } \
18916 while (0)
18917
18918 /* Loop generating characters. The loop is left with IT on the next
18919 character to display. */
18920 while (1)
18921 {
18922 int n_glyphs_before, hpos_before, x_before;
18923 int x, nglyphs;
18924 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
18925
18926 /* Retrieve the next thing to display. Value is zero if end of
18927 buffer reached. */
18928 if (!get_next_display_element (it))
18929 {
18930 /* Maybe add a space at the end of this line that is used to
18931 display the cursor there under X. Set the charpos of the
18932 first glyph of blank lines not corresponding to any text
18933 to -1. */
18934 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
18935 row->exact_window_width_line_p = 1;
18936 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
18937 || row->used[TEXT_AREA] == 0)
18938 {
18939 row->glyphs[TEXT_AREA]->charpos = -1;
18940 row->displays_text_p = 0;
18941
18942 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
18943 && (!MINI_WINDOW_P (it->w)
18944 || (minibuf_level && EQ (it->window, minibuf_window))))
18945 row->indicate_empty_line_p = 1;
18946 }
18947
18948 it->continuation_lines_width = 0;
18949 row->ends_at_zv_p = 1;
18950 /* A row that displays right-to-left text must always have
18951 its last face extended all the way to the end of line,
18952 even if this row ends in ZV, because we still write to
18953 the screen left to right. */
18954 if (row->reversed_p)
18955 extend_face_to_end_of_line (it);
18956 break;
18957 }
18958
18959 /* Now, get the metrics of what we want to display. This also
18960 generates glyphs in `row' (which is IT->glyph_row). */
18961 n_glyphs_before = row->used[TEXT_AREA];
18962 x = it->current_x;
18963
18964 /* Remember the line height so far in case the next element doesn't
18965 fit on the line. */
18966 if (it->line_wrap != TRUNCATE)
18967 {
18968 ascent = it->max_ascent;
18969 descent = it->max_descent;
18970 phys_ascent = it->max_phys_ascent;
18971 phys_descent = it->max_phys_descent;
18972
18973 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
18974 {
18975 if (IT_DISPLAYING_WHITESPACE (it))
18976 may_wrap = 1;
18977 else if (may_wrap)
18978 {
18979 SAVE_IT (wrap_it, *it, wrap_data);
18980 wrap_x = x;
18981 wrap_row_used = row->used[TEXT_AREA];
18982 wrap_row_ascent = row->ascent;
18983 wrap_row_height = row->height;
18984 wrap_row_phys_ascent = row->phys_ascent;
18985 wrap_row_phys_height = row->phys_height;
18986 wrap_row_extra_line_spacing = row->extra_line_spacing;
18987 wrap_row_min_pos = min_pos;
18988 wrap_row_min_bpos = min_bpos;
18989 wrap_row_max_pos = max_pos;
18990 wrap_row_max_bpos = max_bpos;
18991 may_wrap = 0;
18992 }
18993 }
18994 }
18995
18996 PRODUCE_GLYPHS (it);
18997
18998 /* If this display element was in marginal areas, continue with
18999 the next one. */
19000 if (it->area != TEXT_AREA)
19001 {
19002 row->ascent = max (row->ascent, it->max_ascent);
19003 row->height = max (row->height, it->max_ascent + it->max_descent);
19004 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19005 row->phys_height = max (row->phys_height,
19006 it->max_phys_ascent + it->max_phys_descent);
19007 row->extra_line_spacing = max (row->extra_line_spacing,
19008 it->max_extra_line_spacing);
19009 set_iterator_to_next (it, 1);
19010 continue;
19011 }
19012
19013 /* Does the display element fit on the line? If we truncate
19014 lines, we should draw past the right edge of the window. If
19015 we don't truncate, we want to stop so that we can display the
19016 continuation glyph before the right margin. If lines are
19017 continued, there are two possible strategies for characters
19018 resulting in more than 1 glyph (e.g. tabs): Display as many
19019 glyphs as possible in this line and leave the rest for the
19020 continuation line, or display the whole element in the next
19021 line. Original redisplay did the former, so we do it also. */
19022 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19023 hpos_before = it->hpos;
19024 x_before = x;
19025
19026 if (/* Not a newline. */
19027 nglyphs > 0
19028 /* Glyphs produced fit entirely in the line. */
19029 && it->current_x < it->last_visible_x)
19030 {
19031 it->hpos += nglyphs;
19032 row->ascent = max (row->ascent, it->max_ascent);
19033 row->height = max (row->height, it->max_ascent + it->max_descent);
19034 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19035 row->phys_height = max (row->phys_height,
19036 it->max_phys_ascent + it->max_phys_descent);
19037 row->extra_line_spacing = max (row->extra_line_spacing,
19038 it->max_extra_line_spacing);
19039 if (it->current_x - it->pixel_width < it->first_visible_x)
19040 row->x = x - it->first_visible_x;
19041 /* Record the maximum and minimum buffer positions seen so
19042 far in glyphs that will be displayed by this row. */
19043 if (it->bidi_p)
19044 RECORD_MAX_MIN_POS (it);
19045 }
19046 else
19047 {
19048 int i, new_x;
19049 struct glyph *glyph;
19050
19051 for (i = 0; i < nglyphs; ++i, x = new_x)
19052 {
19053 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19054 new_x = x + glyph->pixel_width;
19055
19056 if (/* Lines are continued. */
19057 it->line_wrap != TRUNCATE
19058 && (/* Glyph doesn't fit on the line. */
19059 new_x > it->last_visible_x
19060 /* Or it fits exactly on a window system frame. */
19061 || (new_x == it->last_visible_x
19062 && FRAME_WINDOW_P (it->f))))
19063 {
19064 /* End of a continued line. */
19065
19066 if (it->hpos == 0
19067 || (new_x == it->last_visible_x
19068 && FRAME_WINDOW_P (it->f)))
19069 {
19070 /* Current glyph is the only one on the line or
19071 fits exactly on the line. We must continue
19072 the line because we can't draw the cursor
19073 after the glyph. */
19074 row->continued_p = 1;
19075 it->current_x = new_x;
19076 it->continuation_lines_width += new_x;
19077 ++it->hpos;
19078 if (i == nglyphs - 1)
19079 {
19080 /* If line-wrap is on, check if a previous
19081 wrap point was found. */
19082 if (wrap_row_used > 0
19083 /* Even if there is a previous wrap
19084 point, continue the line here as
19085 usual, if (i) the previous character
19086 was a space or tab AND (ii) the
19087 current character is not. */
19088 && (!may_wrap
19089 || IT_DISPLAYING_WHITESPACE (it)))
19090 goto back_to_wrap;
19091
19092 /* Record the maximum and minimum buffer
19093 positions seen so far in glyphs that will be
19094 displayed by this row. */
19095 if (it->bidi_p)
19096 RECORD_MAX_MIN_POS (it);
19097 set_iterator_to_next (it, 1);
19098 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19099 {
19100 if (!get_next_display_element (it))
19101 {
19102 row->exact_window_width_line_p = 1;
19103 it->continuation_lines_width = 0;
19104 row->continued_p = 0;
19105 row->ends_at_zv_p = 1;
19106 }
19107 else if (ITERATOR_AT_END_OF_LINE_P (it))
19108 {
19109 row->continued_p = 0;
19110 row->exact_window_width_line_p = 1;
19111 }
19112 }
19113 }
19114 else if (it->bidi_p)
19115 RECORD_MAX_MIN_POS (it);
19116 }
19117 else if (CHAR_GLYPH_PADDING_P (*glyph)
19118 && !FRAME_WINDOW_P (it->f))
19119 {
19120 /* A padding glyph that doesn't fit on this line.
19121 This means the whole character doesn't fit
19122 on the line. */
19123 if (row->reversed_p)
19124 unproduce_glyphs (it, row->used[TEXT_AREA]
19125 - n_glyphs_before);
19126 row->used[TEXT_AREA] = n_glyphs_before;
19127
19128 /* Fill the rest of the row with continuation
19129 glyphs like in 20.x. */
19130 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19131 < row->glyphs[1 + TEXT_AREA])
19132 produce_special_glyphs (it, IT_CONTINUATION);
19133
19134 row->continued_p = 1;
19135 it->current_x = x_before;
19136 it->continuation_lines_width += x_before;
19137
19138 /* Restore the height to what it was before the
19139 element not fitting on the line. */
19140 it->max_ascent = ascent;
19141 it->max_descent = descent;
19142 it->max_phys_ascent = phys_ascent;
19143 it->max_phys_descent = phys_descent;
19144 }
19145 else if (wrap_row_used > 0)
19146 {
19147 back_to_wrap:
19148 if (row->reversed_p)
19149 unproduce_glyphs (it,
19150 row->used[TEXT_AREA] - wrap_row_used);
19151 RESTORE_IT (it, &wrap_it, wrap_data);
19152 it->continuation_lines_width += wrap_x;
19153 row->used[TEXT_AREA] = wrap_row_used;
19154 row->ascent = wrap_row_ascent;
19155 row->height = wrap_row_height;
19156 row->phys_ascent = wrap_row_phys_ascent;
19157 row->phys_height = wrap_row_phys_height;
19158 row->extra_line_spacing = wrap_row_extra_line_spacing;
19159 min_pos = wrap_row_min_pos;
19160 min_bpos = wrap_row_min_bpos;
19161 max_pos = wrap_row_max_pos;
19162 max_bpos = wrap_row_max_bpos;
19163 row->continued_p = 1;
19164 row->ends_at_zv_p = 0;
19165 row->exact_window_width_line_p = 0;
19166 it->continuation_lines_width += x;
19167
19168 /* Make sure that a non-default face is extended
19169 up to the right margin of the window. */
19170 extend_face_to_end_of_line (it);
19171 }
19172 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19173 {
19174 /* A TAB that extends past the right edge of the
19175 window. This produces a single glyph on
19176 window system frames. We leave the glyph in
19177 this row and let it fill the row, but don't
19178 consume the TAB. */
19179 it->continuation_lines_width += it->last_visible_x;
19180 row->ends_in_middle_of_char_p = 1;
19181 row->continued_p = 1;
19182 glyph->pixel_width = it->last_visible_x - x;
19183 it->starts_in_middle_of_char_p = 1;
19184 }
19185 else
19186 {
19187 /* Something other than a TAB that draws past
19188 the right edge of the window. Restore
19189 positions to values before the element. */
19190 if (row->reversed_p)
19191 unproduce_glyphs (it, row->used[TEXT_AREA]
19192 - (n_glyphs_before + i));
19193 row->used[TEXT_AREA] = n_glyphs_before + i;
19194
19195 /* Display continuation glyphs. */
19196 if (!FRAME_WINDOW_P (it->f))
19197 produce_special_glyphs (it, IT_CONTINUATION);
19198 row->continued_p = 1;
19199
19200 it->current_x = x_before;
19201 it->continuation_lines_width += x;
19202 extend_face_to_end_of_line (it);
19203
19204 if (nglyphs > 1 && i > 0)
19205 {
19206 row->ends_in_middle_of_char_p = 1;
19207 it->starts_in_middle_of_char_p = 1;
19208 }
19209
19210 /* Restore the height to what it was before the
19211 element not fitting on the line. */
19212 it->max_ascent = ascent;
19213 it->max_descent = descent;
19214 it->max_phys_ascent = phys_ascent;
19215 it->max_phys_descent = phys_descent;
19216 }
19217
19218 break;
19219 }
19220 else if (new_x > it->first_visible_x)
19221 {
19222 /* Increment number of glyphs actually displayed. */
19223 ++it->hpos;
19224
19225 /* Record the maximum and minimum buffer positions
19226 seen so far in glyphs that will be displayed by
19227 this row. */
19228 if (it->bidi_p)
19229 RECORD_MAX_MIN_POS (it);
19230
19231 if (x < it->first_visible_x)
19232 /* Glyph is partially visible, i.e. row starts at
19233 negative X position. */
19234 row->x = x - it->first_visible_x;
19235 }
19236 else
19237 {
19238 /* Glyph is completely off the left margin of the
19239 window. This should not happen because of the
19240 move_it_in_display_line at the start of this
19241 function, unless the text display area of the
19242 window is empty. */
19243 xassert (it->first_visible_x <= it->last_visible_x);
19244 }
19245 }
19246 /* Even if this display element produced no glyphs at all,
19247 we want to record its position. */
19248 if (it->bidi_p && nglyphs == 0)
19249 RECORD_MAX_MIN_POS (it);
19250
19251 row->ascent = max (row->ascent, it->max_ascent);
19252 row->height = max (row->height, it->max_ascent + it->max_descent);
19253 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19254 row->phys_height = max (row->phys_height,
19255 it->max_phys_ascent + it->max_phys_descent);
19256 row->extra_line_spacing = max (row->extra_line_spacing,
19257 it->max_extra_line_spacing);
19258
19259 /* End of this display line if row is continued. */
19260 if (row->continued_p || row->ends_at_zv_p)
19261 break;
19262 }
19263
19264 at_end_of_line:
19265 /* Is this a line end? If yes, we're also done, after making
19266 sure that a non-default face is extended up to the right
19267 margin of the window. */
19268 if (ITERATOR_AT_END_OF_LINE_P (it))
19269 {
19270 int used_before = row->used[TEXT_AREA];
19271
19272 row->ends_in_newline_from_string_p = STRINGP (it->object);
19273
19274 /* Add a space at the end of the line that is used to
19275 display the cursor there. */
19276 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19277 append_space_for_newline (it, 0);
19278
19279 /* Extend the face to the end of the line. */
19280 extend_face_to_end_of_line (it);
19281
19282 /* Make sure we have the position. */
19283 if (used_before == 0)
19284 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19285
19286 /* Record the position of the newline, for use in
19287 find_row_edges. */
19288 it->eol_pos = it->current.pos;
19289
19290 /* Consume the line end. This skips over invisible lines. */
19291 set_iterator_to_next (it, 1);
19292 it->continuation_lines_width = 0;
19293 break;
19294 }
19295
19296 /* Proceed with next display element. Note that this skips
19297 over lines invisible because of selective display. */
19298 set_iterator_to_next (it, 1);
19299
19300 /* If we truncate lines, we are done when the last displayed
19301 glyphs reach past the right margin of the window. */
19302 if (it->line_wrap == TRUNCATE
19303 && (FRAME_WINDOW_P (it->f)
19304 ? (it->current_x >= it->last_visible_x)
19305 : (it->current_x > it->last_visible_x)))
19306 {
19307 /* Maybe add truncation glyphs. */
19308 if (!FRAME_WINDOW_P (it->f))
19309 {
19310 int i, n;
19311
19312 if (!row->reversed_p)
19313 {
19314 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19315 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19316 break;
19317 }
19318 else
19319 {
19320 for (i = 0; i < row->used[TEXT_AREA]; i++)
19321 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19322 break;
19323 /* Remove any padding glyphs at the front of ROW, to
19324 make room for the truncation glyphs we will be
19325 adding below. The loop below always inserts at
19326 least one truncation glyph, so also remove the
19327 last glyph added to ROW. */
19328 unproduce_glyphs (it, i + 1);
19329 /* Adjust i for the loop below. */
19330 i = row->used[TEXT_AREA] - (i + 1);
19331 }
19332
19333 for (n = row->used[TEXT_AREA]; i < n; ++i)
19334 {
19335 row->used[TEXT_AREA] = i;
19336 produce_special_glyphs (it, IT_TRUNCATION);
19337 }
19338 }
19339 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19340 {
19341 /* Don't truncate if we can overflow newline into fringe. */
19342 if (!get_next_display_element (it))
19343 {
19344 it->continuation_lines_width = 0;
19345 row->ends_at_zv_p = 1;
19346 row->exact_window_width_line_p = 1;
19347 break;
19348 }
19349 if (ITERATOR_AT_END_OF_LINE_P (it))
19350 {
19351 row->exact_window_width_line_p = 1;
19352 goto at_end_of_line;
19353 }
19354 }
19355
19356 row->truncated_on_right_p = 1;
19357 it->continuation_lines_width = 0;
19358 reseat_at_next_visible_line_start (it, 0);
19359 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19360 it->hpos = hpos_before;
19361 it->current_x = x_before;
19362 break;
19363 }
19364 }
19365
19366 if (wrap_data)
19367 bidi_unshelve_cache (wrap_data, 1);
19368
19369 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19370 at the left window margin. */
19371 if (it->first_visible_x
19372 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19373 {
19374 if (!FRAME_WINDOW_P (it->f))
19375 insert_left_trunc_glyphs (it);
19376 row->truncated_on_left_p = 1;
19377 }
19378
19379 /* Remember the position at which this line ends.
19380
19381 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19382 cannot be before the call to find_row_edges below, since that is
19383 where these positions are determined. */
19384 row->end = it->current;
19385 if (!it->bidi_p)
19386 {
19387 row->minpos = row->start.pos;
19388 row->maxpos = row->end.pos;
19389 }
19390 else
19391 {
19392 /* ROW->minpos and ROW->maxpos must be the smallest and
19393 `1 + the largest' buffer positions in ROW. But if ROW was
19394 bidi-reordered, these two positions can be anywhere in the
19395 row, so we must determine them now. */
19396 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19397 }
19398
19399 /* If the start of this line is the overlay arrow-position, then
19400 mark this glyph row as the one containing the overlay arrow.
19401 This is clearly a mess with variable size fonts. It would be
19402 better to let it be displayed like cursors under X. */
19403 if ((row->displays_text_p || !overlay_arrow_seen)
19404 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19405 !NILP (overlay_arrow_string)))
19406 {
19407 /* Overlay arrow in window redisplay is a fringe bitmap. */
19408 if (STRINGP (overlay_arrow_string))
19409 {
19410 struct glyph_row *arrow_row
19411 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19412 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19413 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19414 struct glyph *p = row->glyphs[TEXT_AREA];
19415 struct glyph *p2, *end;
19416
19417 /* Copy the arrow glyphs. */
19418 while (glyph < arrow_end)
19419 *p++ = *glyph++;
19420
19421 /* Throw away padding glyphs. */
19422 p2 = p;
19423 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19424 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19425 ++p2;
19426 if (p2 > p)
19427 {
19428 while (p2 < end)
19429 *p++ = *p2++;
19430 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19431 }
19432 }
19433 else
19434 {
19435 xassert (INTEGERP (overlay_arrow_string));
19436 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19437 }
19438 overlay_arrow_seen = 1;
19439 }
19440
19441 /* Highlight trailing whitespace. */
19442 if (!NILP (Vshow_trailing_whitespace))
19443 highlight_trailing_whitespace (it->f, it->glyph_row);
19444
19445 /* Compute pixel dimensions of this line. */
19446 compute_line_metrics (it);
19447
19448 /* Implementation note: No changes in the glyphs of ROW or in their
19449 faces can be done past this point, because compute_line_metrics
19450 computes ROW's hash value and stores it within the glyph_row
19451 structure. */
19452
19453 /* Record whether this row ends inside an ellipsis. */
19454 row->ends_in_ellipsis_p
19455 = (it->method == GET_FROM_DISPLAY_VECTOR
19456 && it->ellipsis_p);
19457
19458 /* Save fringe bitmaps in this row. */
19459 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19460 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19461 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19462 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19463
19464 it->left_user_fringe_bitmap = 0;
19465 it->left_user_fringe_face_id = 0;
19466 it->right_user_fringe_bitmap = 0;
19467 it->right_user_fringe_face_id = 0;
19468
19469 /* Maybe set the cursor. */
19470 cvpos = it->w->cursor.vpos;
19471 if ((cvpos < 0
19472 /* In bidi-reordered rows, keep checking for proper cursor
19473 position even if one has been found already, because buffer
19474 positions in such rows change non-linearly with ROW->VPOS,
19475 when a line is continued. One exception: when we are at ZV,
19476 display cursor on the first suitable glyph row, since all
19477 the empty rows after that also have their position set to ZV. */
19478 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19479 lines' rows is implemented for bidi-reordered rows. */
19480 || (it->bidi_p
19481 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19482 && PT >= MATRIX_ROW_START_CHARPOS (row)
19483 && PT <= MATRIX_ROW_END_CHARPOS (row)
19484 && cursor_row_p (row))
19485 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19486
19487 /* Prepare for the next line. This line starts horizontally at (X
19488 HPOS) = (0 0). Vertical positions are incremented. As a
19489 convenience for the caller, IT->glyph_row is set to the next
19490 row to be used. */
19491 it->current_x = it->hpos = 0;
19492 it->current_y += row->height;
19493 SET_TEXT_POS (it->eol_pos, 0, 0);
19494 ++it->vpos;
19495 ++it->glyph_row;
19496 /* The next row should by default use the same value of the
19497 reversed_p flag as this one. set_iterator_to_next decides when
19498 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19499 the flag accordingly. */
19500 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19501 it->glyph_row->reversed_p = row->reversed_p;
19502 it->start = row->end;
19503 return row->displays_text_p;
19504
19505 #undef RECORD_MAX_MIN_POS
19506 }
19507
19508 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19509 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19510 doc: /* Return paragraph direction at point in BUFFER.
19511 Value is either `left-to-right' or `right-to-left'.
19512 If BUFFER is omitted or nil, it defaults to the current buffer.
19513
19514 Paragraph direction determines how the text in the paragraph is displayed.
19515 In left-to-right paragraphs, text begins at the left margin of the window
19516 and the reading direction is generally left to right. In right-to-left
19517 paragraphs, text begins at the right margin and is read from right to left.
19518
19519 See also `bidi-paragraph-direction'. */)
19520 (Lisp_Object buffer)
19521 {
19522 struct buffer *buf = current_buffer;
19523 struct buffer *old = buf;
19524
19525 if (! NILP (buffer))
19526 {
19527 CHECK_BUFFER (buffer);
19528 buf = XBUFFER (buffer);
19529 }
19530
19531 if (NILP (BVAR (buf, bidi_display_reordering))
19532 || NILP (BVAR (buf, enable_multibyte_characters))
19533 /* When we are loading loadup.el, the character property tables
19534 needed for bidi iteration are not yet available. */
19535 || !NILP (Vpurify_flag))
19536 return Qleft_to_right;
19537 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19538 return BVAR (buf, bidi_paragraph_direction);
19539 else
19540 {
19541 /* Determine the direction from buffer text. We could try to
19542 use current_matrix if it is up to date, but this seems fast
19543 enough as it is. */
19544 struct bidi_it itb;
19545 ptrdiff_t pos = BUF_PT (buf);
19546 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19547 int c;
19548 void *itb_data = bidi_shelve_cache ();
19549
19550 set_buffer_temp (buf);
19551 /* bidi_paragraph_init finds the base direction of the paragraph
19552 by searching forward from paragraph start. We need the base
19553 direction of the current or _previous_ paragraph, so we need
19554 to make sure we are within that paragraph. To that end, find
19555 the previous non-empty line. */
19556 if (pos >= ZV && pos > BEGV)
19557 {
19558 pos--;
19559 bytepos = CHAR_TO_BYTE (pos);
19560 }
19561 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19562 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19563 {
19564 while ((c = FETCH_BYTE (bytepos)) == '\n'
19565 || c == ' ' || c == '\t' || c == '\f')
19566 {
19567 if (bytepos <= BEGV_BYTE)
19568 break;
19569 bytepos--;
19570 pos--;
19571 }
19572 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19573 bytepos--;
19574 }
19575 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19576 itb.paragraph_dir = NEUTRAL_DIR;
19577 itb.string.s = NULL;
19578 itb.string.lstring = Qnil;
19579 itb.string.bufpos = 0;
19580 itb.string.unibyte = 0;
19581 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19582 bidi_unshelve_cache (itb_data, 0);
19583 set_buffer_temp (old);
19584 switch (itb.paragraph_dir)
19585 {
19586 case L2R:
19587 return Qleft_to_right;
19588 break;
19589 case R2L:
19590 return Qright_to_left;
19591 break;
19592 default:
19593 abort ();
19594 }
19595 }
19596 }
19597
19598
19599 \f
19600 /***********************************************************************
19601 Menu Bar
19602 ***********************************************************************/
19603
19604 /* Redisplay the menu bar in the frame for window W.
19605
19606 The menu bar of X frames that don't have X toolkit support is
19607 displayed in a special window W->frame->menu_bar_window.
19608
19609 The menu bar of terminal frames is treated specially as far as
19610 glyph matrices are concerned. Menu bar lines are not part of
19611 windows, so the update is done directly on the frame matrix rows
19612 for the menu bar. */
19613
19614 static void
19615 display_menu_bar (struct window *w)
19616 {
19617 struct frame *f = XFRAME (WINDOW_FRAME (w));
19618 struct it it;
19619 Lisp_Object items;
19620 int i;
19621
19622 /* Don't do all this for graphical frames. */
19623 #ifdef HAVE_NTGUI
19624 if (FRAME_W32_P (f))
19625 return;
19626 #endif
19627 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19628 if (FRAME_X_P (f))
19629 return;
19630 #endif
19631
19632 #ifdef HAVE_NS
19633 if (FRAME_NS_P (f))
19634 return;
19635 #endif /* HAVE_NS */
19636
19637 #ifdef USE_X_TOOLKIT
19638 xassert (!FRAME_WINDOW_P (f));
19639 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19640 it.first_visible_x = 0;
19641 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19642 #else /* not USE_X_TOOLKIT */
19643 if (FRAME_WINDOW_P (f))
19644 {
19645 /* Menu bar lines are displayed in the desired matrix of the
19646 dummy window menu_bar_window. */
19647 struct window *menu_w;
19648 xassert (WINDOWP (f->menu_bar_window));
19649 menu_w = XWINDOW (f->menu_bar_window);
19650 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19651 MENU_FACE_ID);
19652 it.first_visible_x = 0;
19653 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19654 }
19655 else
19656 {
19657 /* This is a TTY frame, i.e. character hpos/vpos are used as
19658 pixel x/y. */
19659 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19660 MENU_FACE_ID);
19661 it.first_visible_x = 0;
19662 it.last_visible_x = FRAME_COLS (f);
19663 }
19664 #endif /* not USE_X_TOOLKIT */
19665
19666 /* FIXME: This should be controlled by a user option. See the
19667 comments in redisplay_tool_bar and display_mode_line about
19668 this. */
19669 it.paragraph_embedding = L2R;
19670
19671 if (! mode_line_inverse_video)
19672 /* Force the menu-bar to be displayed in the default face. */
19673 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19674
19675 /* Clear all rows of the menu bar. */
19676 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19677 {
19678 struct glyph_row *row = it.glyph_row + i;
19679 clear_glyph_row (row);
19680 row->enabled_p = 1;
19681 row->full_width_p = 1;
19682 }
19683
19684 /* Display all items of the menu bar. */
19685 items = FRAME_MENU_BAR_ITEMS (it.f);
19686 for (i = 0; i < ASIZE (items); i += 4)
19687 {
19688 Lisp_Object string;
19689
19690 /* Stop at nil string. */
19691 string = AREF (items, i + 1);
19692 if (NILP (string))
19693 break;
19694
19695 /* Remember where item was displayed. */
19696 ASET (items, i + 3, make_number (it.hpos));
19697
19698 /* Display the item, pad with one space. */
19699 if (it.current_x < it.last_visible_x)
19700 display_string (NULL, string, Qnil, 0, 0, &it,
19701 SCHARS (string) + 1, 0, 0, -1);
19702 }
19703
19704 /* Fill out the line with spaces. */
19705 if (it.current_x < it.last_visible_x)
19706 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19707
19708 /* Compute the total height of the lines. */
19709 compute_line_metrics (&it);
19710 }
19711
19712
19713 \f
19714 /***********************************************************************
19715 Mode Line
19716 ***********************************************************************/
19717
19718 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19719 FORCE is non-zero, redisplay mode lines unconditionally.
19720 Otherwise, redisplay only mode lines that are garbaged. Value is
19721 the number of windows whose mode lines were redisplayed. */
19722
19723 static int
19724 redisplay_mode_lines (Lisp_Object window, int force)
19725 {
19726 int nwindows = 0;
19727
19728 while (!NILP (window))
19729 {
19730 struct window *w = XWINDOW (window);
19731
19732 if (WINDOWP (w->hchild))
19733 nwindows += redisplay_mode_lines (w->hchild, force);
19734 else if (WINDOWP (w->vchild))
19735 nwindows += redisplay_mode_lines (w->vchild, force);
19736 else if (force
19737 || FRAME_GARBAGED_P (XFRAME (w->frame))
19738 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
19739 {
19740 struct text_pos lpoint;
19741 struct buffer *old = current_buffer;
19742
19743 /* Set the window's buffer for the mode line display. */
19744 SET_TEXT_POS (lpoint, PT, PT_BYTE);
19745 set_buffer_internal_1 (XBUFFER (w->buffer));
19746
19747 /* Point refers normally to the selected window. For any
19748 other window, set up appropriate value. */
19749 if (!EQ (window, selected_window))
19750 {
19751 struct text_pos pt;
19752
19753 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
19754 if (CHARPOS (pt) < BEGV)
19755 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
19756 else if (CHARPOS (pt) > (ZV - 1))
19757 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
19758 else
19759 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
19760 }
19761
19762 /* Display mode lines. */
19763 clear_glyph_matrix (w->desired_matrix);
19764 if (display_mode_lines (w))
19765 {
19766 ++nwindows;
19767 w->must_be_updated_p = 1;
19768 }
19769
19770 /* Restore old settings. */
19771 set_buffer_internal_1 (old);
19772 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
19773 }
19774
19775 window = w->next;
19776 }
19777
19778 return nwindows;
19779 }
19780
19781
19782 /* Display the mode and/or header line of window W. Value is the
19783 sum number of mode lines and header lines displayed. */
19784
19785 static int
19786 display_mode_lines (struct window *w)
19787 {
19788 Lisp_Object old_selected_window, old_selected_frame;
19789 int n = 0;
19790
19791 old_selected_frame = selected_frame;
19792 selected_frame = w->frame;
19793 old_selected_window = selected_window;
19794 XSETWINDOW (selected_window, w);
19795
19796 /* These will be set while the mode line specs are processed. */
19797 line_number_displayed = 0;
19798 w->column_number_displayed = Qnil;
19799
19800 if (WINDOW_WANTS_MODELINE_P (w))
19801 {
19802 struct window *sel_w = XWINDOW (old_selected_window);
19803
19804 /* Select mode line face based on the real selected window. */
19805 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
19806 BVAR (current_buffer, mode_line_format));
19807 ++n;
19808 }
19809
19810 if (WINDOW_WANTS_HEADER_LINE_P (w))
19811 {
19812 display_mode_line (w, HEADER_LINE_FACE_ID,
19813 BVAR (current_buffer, header_line_format));
19814 ++n;
19815 }
19816
19817 selected_frame = old_selected_frame;
19818 selected_window = old_selected_window;
19819 return n;
19820 }
19821
19822
19823 /* Display mode or header line of window W. FACE_ID specifies which
19824 line to display; it is either MODE_LINE_FACE_ID or
19825 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
19826 display. Value is the pixel height of the mode/header line
19827 displayed. */
19828
19829 static int
19830 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
19831 {
19832 struct it it;
19833 struct face *face;
19834 ptrdiff_t count = SPECPDL_INDEX ();
19835
19836 init_iterator (&it, w, -1, -1, NULL, face_id);
19837 /* Don't extend on a previously drawn mode-line.
19838 This may happen if called from pos_visible_p. */
19839 it.glyph_row->enabled_p = 0;
19840 prepare_desired_row (it.glyph_row);
19841
19842 it.glyph_row->mode_line_p = 1;
19843
19844 if (! mode_line_inverse_video)
19845 /* Force the mode-line to be displayed in the default face. */
19846 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19847
19848 /* FIXME: This should be controlled by a user option. But
19849 supporting such an option is not trivial, since the mode line is
19850 made up of many separate strings. */
19851 it.paragraph_embedding = L2R;
19852
19853 record_unwind_protect (unwind_format_mode_line,
19854 format_mode_line_unwind_data (NULL, Qnil, 0));
19855
19856 mode_line_target = MODE_LINE_DISPLAY;
19857
19858 /* Temporarily make frame's keyboard the current kboard so that
19859 kboard-local variables in the mode_line_format will get the right
19860 values. */
19861 push_kboard (FRAME_KBOARD (it.f));
19862 record_unwind_save_match_data ();
19863 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
19864 pop_kboard ();
19865
19866 unbind_to (count, Qnil);
19867
19868 /* Fill up with spaces. */
19869 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
19870
19871 compute_line_metrics (&it);
19872 it.glyph_row->full_width_p = 1;
19873 it.glyph_row->continued_p = 0;
19874 it.glyph_row->truncated_on_left_p = 0;
19875 it.glyph_row->truncated_on_right_p = 0;
19876
19877 /* Make a 3D mode-line have a shadow at its right end. */
19878 face = FACE_FROM_ID (it.f, face_id);
19879 extend_face_to_end_of_line (&it);
19880 if (face->box != FACE_NO_BOX)
19881 {
19882 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
19883 + it.glyph_row->used[TEXT_AREA] - 1);
19884 last->right_box_line_p = 1;
19885 }
19886
19887 return it.glyph_row->height;
19888 }
19889
19890 /* Move element ELT in LIST to the front of LIST.
19891 Return the updated list. */
19892
19893 static Lisp_Object
19894 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
19895 {
19896 register Lisp_Object tail, prev;
19897 register Lisp_Object tem;
19898
19899 tail = list;
19900 prev = Qnil;
19901 while (CONSP (tail))
19902 {
19903 tem = XCAR (tail);
19904
19905 if (EQ (elt, tem))
19906 {
19907 /* Splice out the link TAIL. */
19908 if (NILP (prev))
19909 list = XCDR (tail);
19910 else
19911 Fsetcdr (prev, XCDR (tail));
19912
19913 /* Now make it the first. */
19914 Fsetcdr (tail, list);
19915 return tail;
19916 }
19917 else
19918 prev = tail;
19919 tail = XCDR (tail);
19920 QUIT;
19921 }
19922
19923 /* Not found--return unchanged LIST. */
19924 return list;
19925 }
19926
19927 /* Contribute ELT to the mode line for window IT->w. How it
19928 translates into text depends on its data type.
19929
19930 IT describes the display environment in which we display, as usual.
19931
19932 DEPTH is the depth in recursion. It is used to prevent
19933 infinite recursion here.
19934
19935 FIELD_WIDTH is the number of characters the display of ELT should
19936 occupy in the mode line, and PRECISION is the maximum number of
19937 characters to display from ELT's representation. See
19938 display_string for details.
19939
19940 Returns the hpos of the end of the text generated by ELT.
19941
19942 PROPS is a property list to add to any string we encounter.
19943
19944 If RISKY is nonzero, remove (disregard) any properties in any string
19945 we encounter, and ignore :eval and :propertize.
19946
19947 The global variable `mode_line_target' determines whether the
19948 output is passed to `store_mode_line_noprop',
19949 `store_mode_line_string', or `display_string'. */
19950
19951 static int
19952 display_mode_element (struct it *it, int depth, int field_width, int precision,
19953 Lisp_Object elt, Lisp_Object props, int risky)
19954 {
19955 int n = 0, field, prec;
19956 int literal = 0;
19957
19958 tail_recurse:
19959 if (depth > 100)
19960 elt = build_string ("*too-deep*");
19961
19962 depth++;
19963
19964 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
19965 {
19966 case Lisp_String:
19967 {
19968 /* A string: output it and check for %-constructs within it. */
19969 unsigned char c;
19970 ptrdiff_t offset = 0;
19971
19972 if (SCHARS (elt) > 0
19973 && (!NILP (props) || risky))
19974 {
19975 Lisp_Object oprops, aelt;
19976 oprops = Ftext_properties_at (make_number (0), elt);
19977
19978 /* If the starting string's properties are not what
19979 we want, translate the string. Also, if the string
19980 is risky, do that anyway. */
19981
19982 if (NILP (Fequal (props, oprops)) || risky)
19983 {
19984 /* If the starting string has properties,
19985 merge the specified ones onto the existing ones. */
19986 if (! NILP (oprops) && !risky)
19987 {
19988 Lisp_Object tem;
19989
19990 oprops = Fcopy_sequence (oprops);
19991 tem = props;
19992 while (CONSP (tem))
19993 {
19994 oprops = Fplist_put (oprops, XCAR (tem),
19995 XCAR (XCDR (tem)));
19996 tem = XCDR (XCDR (tem));
19997 }
19998 props = oprops;
19999 }
20000
20001 aelt = Fassoc (elt, mode_line_proptrans_alist);
20002 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20003 {
20004 /* AELT is what we want. Move it to the front
20005 without consing. */
20006 elt = XCAR (aelt);
20007 mode_line_proptrans_alist
20008 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20009 }
20010 else
20011 {
20012 Lisp_Object tem;
20013
20014 /* If AELT has the wrong props, it is useless.
20015 so get rid of it. */
20016 if (! NILP (aelt))
20017 mode_line_proptrans_alist
20018 = Fdelq (aelt, mode_line_proptrans_alist);
20019
20020 elt = Fcopy_sequence (elt);
20021 Fset_text_properties (make_number (0), Flength (elt),
20022 props, elt);
20023 /* Add this item to mode_line_proptrans_alist. */
20024 mode_line_proptrans_alist
20025 = Fcons (Fcons (elt, props),
20026 mode_line_proptrans_alist);
20027 /* Truncate mode_line_proptrans_alist
20028 to at most 50 elements. */
20029 tem = Fnthcdr (make_number (50),
20030 mode_line_proptrans_alist);
20031 if (! NILP (tem))
20032 XSETCDR (tem, Qnil);
20033 }
20034 }
20035 }
20036
20037 offset = 0;
20038
20039 if (literal)
20040 {
20041 prec = precision - n;
20042 switch (mode_line_target)
20043 {
20044 case MODE_LINE_NOPROP:
20045 case MODE_LINE_TITLE:
20046 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20047 break;
20048 case MODE_LINE_STRING:
20049 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20050 break;
20051 case MODE_LINE_DISPLAY:
20052 n += display_string (NULL, elt, Qnil, 0, 0, it,
20053 0, prec, 0, STRING_MULTIBYTE (elt));
20054 break;
20055 }
20056
20057 break;
20058 }
20059
20060 /* Handle the non-literal case. */
20061
20062 while ((precision <= 0 || n < precision)
20063 && SREF (elt, offset) != 0
20064 && (mode_line_target != MODE_LINE_DISPLAY
20065 || it->current_x < it->last_visible_x))
20066 {
20067 ptrdiff_t last_offset = offset;
20068
20069 /* Advance to end of string or next format specifier. */
20070 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20071 ;
20072
20073 if (offset - 1 != last_offset)
20074 {
20075 ptrdiff_t nchars, nbytes;
20076
20077 /* Output to end of string or up to '%'. Field width
20078 is length of string. Don't output more than
20079 PRECISION allows us. */
20080 offset--;
20081
20082 prec = c_string_width (SDATA (elt) + last_offset,
20083 offset - last_offset, precision - n,
20084 &nchars, &nbytes);
20085
20086 switch (mode_line_target)
20087 {
20088 case MODE_LINE_NOPROP:
20089 case MODE_LINE_TITLE:
20090 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20091 break;
20092 case MODE_LINE_STRING:
20093 {
20094 ptrdiff_t bytepos = last_offset;
20095 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20096 ptrdiff_t endpos = (precision <= 0
20097 ? string_byte_to_char (elt, offset)
20098 : charpos + nchars);
20099
20100 n += store_mode_line_string (NULL,
20101 Fsubstring (elt, make_number (charpos),
20102 make_number (endpos)),
20103 0, 0, 0, Qnil);
20104 }
20105 break;
20106 case MODE_LINE_DISPLAY:
20107 {
20108 ptrdiff_t bytepos = last_offset;
20109 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20110
20111 if (precision <= 0)
20112 nchars = string_byte_to_char (elt, offset) - charpos;
20113 n += display_string (NULL, elt, Qnil, 0, charpos,
20114 it, 0, nchars, 0,
20115 STRING_MULTIBYTE (elt));
20116 }
20117 break;
20118 }
20119 }
20120 else /* c == '%' */
20121 {
20122 ptrdiff_t percent_position = offset;
20123
20124 /* Get the specified minimum width. Zero means
20125 don't pad. */
20126 field = 0;
20127 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20128 field = field * 10 + c - '0';
20129
20130 /* Don't pad beyond the total padding allowed. */
20131 if (field_width - n > 0 && field > field_width - n)
20132 field = field_width - n;
20133
20134 /* Note that either PRECISION <= 0 or N < PRECISION. */
20135 prec = precision - n;
20136
20137 if (c == 'M')
20138 n += display_mode_element (it, depth, field, prec,
20139 Vglobal_mode_string, props,
20140 risky);
20141 else if (c != 0)
20142 {
20143 int multibyte;
20144 ptrdiff_t bytepos, charpos;
20145 const char *spec;
20146 Lisp_Object string;
20147
20148 bytepos = percent_position;
20149 charpos = (STRING_MULTIBYTE (elt)
20150 ? string_byte_to_char (elt, bytepos)
20151 : bytepos);
20152 spec = decode_mode_spec (it->w, c, field, &string);
20153 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20154
20155 switch (mode_line_target)
20156 {
20157 case MODE_LINE_NOPROP:
20158 case MODE_LINE_TITLE:
20159 n += store_mode_line_noprop (spec, field, prec);
20160 break;
20161 case MODE_LINE_STRING:
20162 {
20163 Lisp_Object tem = build_string (spec);
20164 props = Ftext_properties_at (make_number (charpos), elt);
20165 /* Should only keep face property in props */
20166 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20167 }
20168 break;
20169 case MODE_LINE_DISPLAY:
20170 {
20171 int nglyphs_before, nwritten;
20172
20173 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20174 nwritten = display_string (spec, string, elt,
20175 charpos, 0, it,
20176 field, prec, 0,
20177 multibyte);
20178
20179 /* Assign to the glyphs written above the
20180 string where the `%x' came from, position
20181 of the `%'. */
20182 if (nwritten > 0)
20183 {
20184 struct glyph *glyph
20185 = (it->glyph_row->glyphs[TEXT_AREA]
20186 + nglyphs_before);
20187 int i;
20188
20189 for (i = 0; i < nwritten; ++i)
20190 {
20191 glyph[i].object = elt;
20192 glyph[i].charpos = charpos;
20193 }
20194
20195 n += nwritten;
20196 }
20197 }
20198 break;
20199 }
20200 }
20201 else /* c == 0 */
20202 break;
20203 }
20204 }
20205 }
20206 break;
20207
20208 case Lisp_Symbol:
20209 /* A symbol: process the value of the symbol recursively
20210 as if it appeared here directly. Avoid error if symbol void.
20211 Special case: if value of symbol is a string, output the string
20212 literally. */
20213 {
20214 register Lisp_Object tem;
20215
20216 /* If the variable is not marked as risky to set
20217 then its contents are risky to use. */
20218 if (NILP (Fget (elt, Qrisky_local_variable)))
20219 risky = 1;
20220
20221 tem = Fboundp (elt);
20222 if (!NILP (tem))
20223 {
20224 tem = Fsymbol_value (elt);
20225 /* If value is a string, output that string literally:
20226 don't check for % within it. */
20227 if (STRINGP (tem))
20228 literal = 1;
20229
20230 if (!EQ (tem, elt))
20231 {
20232 /* Give up right away for nil or t. */
20233 elt = tem;
20234 goto tail_recurse;
20235 }
20236 }
20237 }
20238 break;
20239
20240 case Lisp_Cons:
20241 {
20242 register Lisp_Object car, tem;
20243
20244 /* A cons cell: five distinct cases.
20245 If first element is :eval or :propertize, do something special.
20246 If first element is a string or a cons, process all the elements
20247 and effectively concatenate them.
20248 If first element is a negative number, truncate displaying cdr to
20249 at most that many characters. If positive, pad (with spaces)
20250 to at least that many characters.
20251 If first element is a symbol, process the cadr or caddr recursively
20252 according to whether the symbol's value is non-nil or nil. */
20253 car = XCAR (elt);
20254 if (EQ (car, QCeval))
20255 {
20256 /* An element of the form (:eval FORM) means evaluate FORM
20257 and use the result as mode line elements. */
20258
20259 if (risky)
20260 break;
20261
20262 if (CONSP (XCDR (elt)))
20263 {
20264 Lisp_Object spec;
20265 spec = safe_eval (XCAR (XCDR (elt)));
20266 n += display_mode_element (it, depth, field_width - n,
20267 precision - n, spec, props,
20268 risky);
20269 }
20270 }
20271 else if (EQ (car, QCpropertize))
20272 {
20273 /* An element of the form (:propertize ELT PROPS...)
20274 means display ELT but applying properties PROPS. */
20275
20276 if (risky)
20277 break;
20278
20279 if (CONSP (XCDR (elt)))
20280 n += display_mode_element (it, depth, field_width - n,
20281 precision - n, XCAR (XCDR (elt)),
20282 XCDR (XCDR (elt)), risky);
20283 }
20284 else if (SYMBOLP (car))
20285 {
20286 tem = Fboundp (car);
20287 elt = XCDR (elt);
20288 if (!CONSP (elt))
20289 goto invalid;
20290 /* elt is now the cdr, and we know it is a cons cell.
20291 Use its car if CAR has a non-nil value. */
20292 if (!NILP (tem))
20293 {
20294 tem = Fsymbol_value (car);
20295 if (!NILP (tem))
20296 {
20297 elt = XCAR (elt);
20298 goto tail_recurse;
20299 }
20300 }
20301 /* Symbol's value is nil (or symbol is unbound)
20302 Get the cddr of the original list
20303 and if possible find the caddr and use that. */
20304 elt = XCDR (elt);
20305 if (NILP (elt))
20306 break;
20307 else if (!CONSP (elt))
20308 goto invalid;
20309 elt = XCAR (elt);
20310 goto tail_recurse;
20311 }
20312 else if (INTEGERP (car))
20313 {
20314 register int lim = XINT (car);
20315 elt = XCDR (elt);
20316 if (lim < 0)
20317 {
20318 /* Negative int means reduce maximum width. */
20319 if (precision <= 0)
20320 precision = -lim;
20321 else
20322 precision = min (precision, -lim);
20323 }
20324 else if (lim > 0)
20325 {
20326 /* Padding specified. Don't let it be more than
20327 current maximum. */
20328 if (precision > 0)
20329 lim = min (precision, lim);
20330
20331 /* If that's more padding than already wanted, queue it.
20332 But don't reduce padding already specified even if
20333 that is beyond the current truncation point. */
20334 field_width = max (lim, field_width);
20335 }
20336 goto tail_recurse;
20337 }
20338 else if (STRINGP (car) || CONSP (car))
20339 {
20340 Lisp_Object halftail = elt;
20341 int len = 0;
20342
20343 while (CONSP (elt)
20344 && (precision <= 0 || n < precision))
20345 {
20346 n += display_mode_element (it, depth,
20347 /* Do padding only after the last
20348 element in the list. */
20349 (! CONSP (XCDR (elt))
20350 ? field_width - n
20351 : 0),
20352 precision - n, XCAR (elt),
20353 props, risky);
20354 elt = XCDR (elt);
20355 len++;
20356 if ((len & 1) == 0)
20357 halftail = XCDR (halftail);
20358 /* Check for cycle. */
20359 if (EQ (halftail, elt))
20360 break;
20361 }
20362 }
20363 }
20364 break;
20365
20366 default:
20367 invalid:
20368 elt = build_string ("*invalid*");
20369 goto tail_recurse;
20370 }
20371
20372 /* Pad to FIELD_WIDTH. */
20373 if (field_width > 0 && n < field_width)
20374 {
20375 switch (mode_line_target)
20376 {
20377 case MODE_LINE_NOPROP:
20378 case MODE_LINE_TITLE:
20379 n += store_mode_line_noprop ("", field_width - n, 0);
20380 break;
20381 case MODE_LINE_STRING:
20382 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20383 break;
20384 case MODE_LINE_DISPLAY:
20385 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20386 0, 0, 0);
20387 break;
20388 }
20389 }
20390
20391 return n;
20392 }
20393
20394 /* Store a mode-line string element in mode_line_string_list.
20395
20396 If STRING is non-null, display that C string. Otherwise, the Lisp
20397 string LISP_STRING is displayed.
20398
20399 FIELD_WIDTH is the minimum number of output glyphs to produce.
20400 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20401 with spaces. FIELD_WIDTH <= 0 means don't pad.
20402
20403 PRECISION is the maximum number of characters to output from
20404 STRING. PRECISION <= 0 means don't truncate the string.
20405
20406 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20407 properties to the string.
20408
20409 PROPS are the properties to add to the string.
20410 The mode_line_string_face face property is always added to the string.
20411 */
20412
20413 static int
20414 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20415 int field_width, int precision, Lisp_Object props)
20416 {
20417 ptrdiff_t len;
20418 int n = 0;
20419
20420 if (string != NULL)
20421 {
20422 len = strlen (string);
20423 if (precision > 0 && len > precision)
20424 len = precision;
20425 lisp_string = make_string (string, len);
20426 if (NILP (props))
20427 props = mode_line_string_face_prop;
20428 else if (!NILP (mode_line_string_face))
20429 {
20430 Lisp_Object face = Fplist_get (props, Qface);
20431 props = Fcopy_sequence (props);
20432 if (NILP (face))
20433 face = mode_line_string_face;
20434 else
20435 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20436 props = Fplist_put (props, Qface, face);
20437 }
20438 Fadd_text_properties (make_number (0), make_number (len),
20439 props, lisp_string);
20440 }
20441 else
20442 {
20443 len = XFASTINT (Flength (lisp_string));
20444 if (precision > 0 && len > precision)
20445 {
20446 len = precision;
20447 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20448 precision = -1;
20449 }
20450 if (!NILP (mode_line_string_face))
20451 {
20452 Lisp_Object face;
20453 if (NILP (props))
20454 props = Ftext_properties_at (make_number (0), lisp_string);
20455 face = Fplist_get (props, Qface);
20456 if (NILP (face))
20457 face = mode_line_string_face;
20458 else
20459 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20460 props = Fcons (Qface, Fcons (face, Qnil));
20461 if (copy_string)
20462 lisp_string = Fcopy_sequence (lisp_string);
20463 }
20464 if (!NILP (props))
20465 Fadd_text_properties (make_number (0), make_number (len),
20466 props, lisp_string);
20467 }
20468
20469 if (len > 0)
20470 {
20471 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20472 n += len;
20473 }
20474
20475 if (field_width > len)
20476 {
20477 field_width -= len;
20478 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20479 if (!NILP (props))
20480 Fadd_text_properties (make_number (0), make_number (field_width),
20481 props, lisp_string);
20482 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20483 n += field_width;
20484 }
20485
20486 return n;
20487 }
20488
20489
20490 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20491 1, 4, 0,
20492 doc: /* Format a string out of a mode line format specification.
20493 First arg FORMAT specifies the mode line format (see `mode-line-format'
20494 for details) to use.
20495
20496 By default, the format is evaluated for the currently selected window.
20497
20498 Optional second arg FACE specifies the face property to put on all
20499 characters for which no face is specified. The value nil means the
20500 default face. The value t means whatever face the window's mode line
20501 currently uses (either `mode-line' or `mode-line-inactive',
20502 depending on whether the window is the selected window or not).
20503 An integer value means the value string has no text
20504 properties.
20505
20506 Optional third and fourth args WINDOW and BUFFER specify the window
20507 and buffer to use as the context for the formatting (defaults
20508 are the selected window and the WINDOW's buffer). */)
20509 (Lisp_Object format, Lisp_Object face,
20510 Lisp_Object window, Lisp_Object buffer)
20511 {
20512 struct it it;
20513 int len;
20514 struct window *w;
20515 struct buffer *old_buffer = NULL;
20516 int face_id;
20517 int no_props = INTEGERP (face);
20518 ptrdiff_t count = SPECPDL_INDEX ();
20519 Lisp_Object str;
20520 int string_start = 0;
20521
20522 if (NILP (window))
20523 window = selected_window;
20524 CHECK_WINDOW (window);
20525 w = XWINDOW (window);
20526
20527 if (NILP (buffer))
20528 buffer = w->buffer;
20529 CHECK_BUFFER (buffer);
20530
20531 /* Make formatting the modeline a non-op when noninteractive, otherwise
20532 there will be problems later caused by a partially initialized frame. */
20533 if (NILP (format) || noninteractive)
20534 return empty_unibyte_string;
20535
20536 if (no_props)
20537 face = Qnil;
20538
20539 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20540 : EQ (face, Qt) ? (EQ (window, selected_window)
20541 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20542 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20543 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20544 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20545 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20546 : DEFAULT_FACE_ID;
20547
20548 if (XBUFFER (buffer) != current_buffer)
20549 old_buffer = current_buffer;
20550
20551 /* Save things including mode_line_proptrans_alist,
20552 and set that to nil so that we don't alter the outer value. */
20553 record_unwind_protect (unwind_format_mode_line,
20554 format_mode_line_unwind_data
20555 (old_buffer, selected_window, 1));
20556 mode_line_proptrans_alist = Qnil;
20557
20558 Fselect_window (window, Qt);
20559 if (old_buffer)
20560 set_buffer_internal_1 (XBUFFER (buffer));
20561
20562 init_iterator (&it, w, -1, -1, NULL, face_id);
20563
20564 if (no_props)
20565 {
20566 mode_line_target = MODE_LINE_NOPROP;
20567 mode_line_string_face_prop = Qnil;
20568 mode_line_string_list = Qnil;
20569 string_start = MODE_LINE_NOPROP_LEN (0);
20570 }
20571 else
20572 {
20573 mode_line_target = MODE_LINE_STRING;
20574 mode_line_string_list = Qnil;
20575 mode_line_string_face = face;
20576 mode_line_string_face_prop
20577 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20578 }
20579
20580 push_kboard (FRAME_KBOARD (it.f));
20581 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20582 pop_kboard ();
20583
20584 if (no_props)
20585 {
20586 len = MODE_LINE_NOPROP_LEN (string_start);
20587 str = make_string (mode_line_noprop_buf + string_start, len);
20588 }
20589 else
20590 {
20591 mode_line_string_list = Fnreverse (mode_line_string_list);
20592 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20593 empty_unibyte_string);
20594 }
20595
20596 unbind_to (count, Qnil);
20597 return str;
20598 }
20599
20600 /* Write a null-terminated, right justified decimal representation of
20601 the positive integer D to BUF using a minimal field width WIDTH. */
20602
20603 static void
20604 pint2str (register char *buf, register int width, register ptrdiff_t d)
20605 {
20606 register char *p = buf;
20607
20608 if (d <= 0)
20609 *p++ = '0';
20610 else
20611 {
20612 while (d > 0)
20613 {
20614 *p++ = d % 10 + '0';
20615 d /= 10;
20616 }
20617 }
20618
20619 for (width -= (int) (p - buf); width > 0; --width)
20620 *p++ = ' ';
20621 *p-- = '\0';
20622 while (p > buf)
20623 {
20624 d = *buf;
20625 *buf++ = *p;
20626 *p-- = d;
20627 }
20628 }
20629
20630 /* Write a null-terminated, right justified decimal and "human
20631 readable" representation of the nonnegative integer D to BUF using
20632 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20633
20634 static const char power_letter[] =
20635 {
20636 0, /* no letter */
20637 'k', /* kilo */
20638 'M', /* mega */
20639 'G', /* giga */
20640 'T', /* tera */
20641 'P', /* peta */
20642 'E', /* exa */
20643 'Z', /* zetta */
20644 'Y' /* yotta */
20645 };
20646
20647 static void
20648 pint2hrstr (char *buf, int width, ptrdiff_t d)
20649 {
20650 /* We aim to represent the nonnegative integer D as
20651 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20652 ptrdiff_t quotient = d;
20653 int remainder = 0;
20654 /* -1 means: do not use TENTHS. */
20655 int tenths = -1;
20656 int exponent = 0;
20657
20658 /* Length of QUOTIENT.TENTHS as a string. */
20659 int length;
20660
20661 char * psuffix;
20662 char * p;
20663
20664 if (1000 <= quotient)
20665 {
20666 /* Scale to the appropriate EXPONENT. */
20667 do
20668 {
20669 remainder = quotient % 1000;
20670 quotient /= 1000;
20671 exponent++;
20672 }
20673 while (1000 <= quotient);
20674
20675 /* Round to nearest and decide whether to use TENTHS or not. */
20676 if (quotient <= 9)
20677 {
20678 tenths = remainder / 100;
20679 if (50 <= remainder % 100)
20680 {
20681 if (tenths < 9)
20682 tenths++;
20683 else
20684 {
20685 quotient++;
20686 if (quotient == 10)
20687 tenths = -1;
20688 else
20689 tenths = 0;
20690 }
20691 }
20692 }
20693 else
20694 if (500 <= remainder)
20695 {
20696 if (quotient < 999)
20697 quotient++;
20698 else
20699 {
20700 quotient = 1;
20701 exponent++;
20702 tenths = 0;
20703 }
20704 }
20705 }
20706
20707 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20708 if (tenths == -1 && quotient <= 99)
20709 if (quotient <= 9)
20710 length = 1;
20711 else
20712 length = 2;
20713 else
20714 length = 3;
20715 p = psuffix = buf + max (width, length);
20716
20717 /* Print EXPONENT. */
20718 *psuffix++ = power_letter[exponent];
20719 *psuffix = '\0';
20720
20721 /* Print TENTHS. */
20722 if (tenths >= 0)
20723 {
20724 *--p = '0' + tenths;
20725 *--p = '.';
20726 }
20727
20728 /* Print QUOTIENT. */
20729 do
20730 {
20731 int digit = quotient % 10;
20732 *--p = '0' + digit;
20733 }
20734 while ((quotient /= 10) != 0);
20735
20736 /* Print leading spaces. */
20737 while (buf < p)
20738 *--p = ' ';
20739 }
20740
20741 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
20742 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
20743 type of CODING_SYSTEM. Return updated pointer into BUF. */
20744
20745 static unsigned char invalid_eol_type[] = "(*invalid*)";
20746
20747 static char *
20748 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
20749 {
20750 Lisp_Object val;
20751 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
20752 const unsigned char *eol_str;
20753 int eol_str_len;
20754 /* The EOL conversion we are using. */
20755 Lisp_Object eoltype;
20756
20757 val = CODING_SYSTEM_SPEC (coding_system);
20758 eoltype = Qnil;
20759
20760 if (!VECTORP (val)) /* Not yet decided. */
20761 {
20762 if (multibyte)
20763 *buf++ = '-';
20764 if (eol_flag)
20765 eoltype = eol_mnemonic_undecided;
20766 /* Don't mention EOL conversion if it isn't decided. */
20767 }
20768 else
20769 {
20770 Lisp_Object attrs;
20771 Lisp_Object eolvalue;
20772
20773 attrs = AREF (val, 0);
20774 eolvalue = AREF (val, 2);
20775
20776 if (multibyte)
20777 *buf++ = XFASTINT (CODING_ATTR_MNEMONIC (attrs));
20778
20779 if (eol_flag)
20780 {
20781 /* The EOL conversion that is normal on this system. */
20782
20783 if (NILP (eolvalue)) /* Not yet decided. */
20784 eoltype = eol_mnemonic_undecided;
20785 else if (VECTORP (eolvalue)) /* Not yet decided. */
20786 eoltype = eol_mnemonic_undecided;
20787 else /* eolvalue is Qunix, Qdos, or Qmac. */
20788 eoltype = (EQ (eolvalue, Qunix)
20789 ? eol_mnemonic_unix
20790 : (EQ (eolvalue, Qdos) == 1
20791 ? eol_mnemonic_dos : eol_mnemonic_mac));
20792 }
20793 }
20794
20795 if (eol_flag)
20796 {
20797 /* Mention the EOL conversion if it is not the usual one. */
20798 if (STRINGP (eoltype))
20799 {
20800 eol_str = SDATA (eoltype);
20801 eol_str_len = SBYTES (eoltype);
20802 }
20803 else if (CHARACTERP (eoltype))
20804 {
20805 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
20806 int c = XFASTINT (eoltype);
20807 eol_str_len = CHAR_STRING (c, tmp);
20808 eol_str = tmp;
20809 }
20810 else
20811 {
20812 eol_str = invalid_eol_type;
20813 eol_str_len = sizeof (invalid_eol_type) - 1;
20814 }
20815 memcpy (buf, eol_str, eol_str_len);
20816 buf += eol_str_len;
20817 }
20818
20819 return buf;
20820 }
20821
20822 /* Return a string for the output of a mode line %-spec for window W,
20823 generated by character C. FIELD_WIDTH > 0 means pad the string
20824 returned with spaces to that value. Return a Lisp string in
20825 *STRING if the resulting string is taken from that Lisp string.
20826
20827 Note we operate on the current buffer for most purposes,
20828 the exception being w->base_line_pos. */
20829
20830 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
20831
20832 static const char *
20833 decode_mode_spec (struct window *w, register int c, int field_width,
20834 Lisp_Object *string)
20835 {
20836 Lisp_Object obj;
20837 struct frame *f = XFRAME (WINDOW_FRAME (w));
20838 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
20839 struct buffer *b = current_buffer;
20840
20841 obj = Qnil;
20842 *string = Qnil;
20843
20844 switch (c)
20845 {
20846 case '*':
20847 if (!NILP (BVAR (b, read_only)))
20848 return "%";
20849 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20850 return "*";
20851 return "-";
20852
20853 case '+':
20854 /* This differs from %* only for a modified read-only buffer. */
20855 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20856 return "*";
20857 if (!NILP (BVAR (b, read_only)))
20858 return "%";
20859 return "-";
20860
20861 case '&':
20862 /* This differs from %* in ignoring read-only-ness. */
20863 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
20864 return "*";
20865 return "-";
20866
20867 case '%':
20868 return "%";
20869
20870 case '[':
20871 {
20872 int i;
20873 char *p;
20874
20875 if (command_loop_level > 5)
20876 return "[[[... ";
20877 p = decode_mode_spec_buf;
20878 for (i = 0; i < command_loop_level; i++)
20879 *p++ = '[';
20880 *p = 0;
20881 return decode_mode_spec_buf;
20882 }
20883
20884 case ']':
20885 {
20886 int i;
20887 char *p;
20888
20889 if (command_loop_level > 5)
20890 return " ...]]]";
20891 p = decode_mode_spec_buf;
20892 for (i = 0; i < command_loop_level; i++)
20893 *p++ = ']';
20894 *p = 0;
20895 return decode_mode_spec_buf;
20896 }
20897
20898 case '-':
20899 {
20900 register int i;
20901
20902 /* Let lots_of_dashes be a string of infinite length. */
20903 if (mode_line_target == MODE_LINE_NOPROP ||
20904 mode_line_target == MODE_LINE_STRING)
20905 return "--";
20906 if (field_width <= 0
20907 || field_width > sizeof (lots_of_dashes))
20908 {
20909 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
20910 decode_mode_spec_buf[i] = '-';
20911 decode_mode_spec_buf[i] = '\0';
20912 return decode_mode_spec_buf;
20913 }
20914 else
20915 return lots_of_dashes;
20916 }
20917
20918 case 'b':
20919 obj = BVAR (b, name);
20920 break;
20921
20922 case 'c':
20923 /* %c and %l are ignored in `frame-title-format'.
20924 (In redisplay_internal, the frame title is drawn _before_ the
20925 windows are updated, so the stuff which depends on actual
20926 window contents (such as %l) may fail to render properly, or
20927 even crash emacs.) */
20928 if (mode_line_target == MODE_LINE_TITLE)
20929 return "";
20930 else
20931 {
20932 ptrdiff_t col = current_column ();
20933 w->column_number_displayed = make_number (col);
20934 pint2str (decode_mode_spec_buf, field_width, col);
20935 return decode_mode_spec_buf;
20936 }
20937
20938 case 'e':
20939 #ifndef SYSTEM_MALLOC
20940 {
20941 if (NILP (Vmemory_full))
20942 return "";
20943 else
20944 return "!MEM FULL! ";
20945 }
20946 #else
20947 return "";
20948 #endif
20949
20950 case 'F':
20951 /* %F displays the frame name. */
20952 if (!NILP (f->title))
20953 return SSDATA (f->title);
20954 if (f->explicit_name || ! FRAME_WINDOW_P (f))
20955 return SSDATA (f->name);
20956 return "Emacs";
20957
20958 case 'f':
20959 obj = BVAR (b, filename);
20960 break;
20961
20962 case 'i':
20963 {
20964 ptrdiff_t size = ZV - BEGV;
20965 pint2str (decode_mode_spec_buf, field_width, size);
20966 return decode_mode_spec_buf;
20967 }
20968
20969 case 'I':
20970 {
20971 ptrdiff_t size = ZV - BEGV;
20972 pint2hrstr (decode_mode_spec_buf, field_width, size);
20973 return decode_mode_spec_buf;
20974 }
20975
20976 case 'l':
20977 {
20978 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
20979 ptrdiff_t topline, nlines, height;
20980 ptrdiff_t junk;
20981
20982 /* %c and %l are ignored in `frame-title-format'. */
20983 if (mode_line_target == MODE_LINE_TITLE)
20984 return "";
20985
20986 startpos = XMARKER (w->start)->charpos;
20987 startpos_byte = marker_byte_position (w->start);
20988 height = WINDOW_TOTAL_LINES (w);
20989
20990 /* If we decided that this buffer isn't suitable for line numbers,
20991 don't forget that too fast. */
20992 if (EQ (w->base_line_pos, w->buffer))
20993 goto no_value;
20994 /* But do forget it, if the window shows a different buffer now. */
20995 else if (BUFFERP (w->base_line_pos))
20996 w->base_line_pos = Qnil;
20997
20998 /* If the buffer is very big, don't waste time. */
20999 if (INTEGERP (Vline_number_display_limit)
21000 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21001 {
21002 w->base_line_pos = Qnil;
21003 w->base_line_number = Qnil;
21004 goto no_value;
21005 }
21006
21007 if (INTEGERP (w->base_line_number)
21008 && INTEGERP (w->base_line_pos)
21009 && XFASTINT (w->base_line_pos) <= startpos)
21010 {
21011 line = XFASTINT (w->base_line_number);
21012 linepos = XFASTINT (w->base_line_pos);
21013 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21014 }
21015 else
21016 {
21017 line = 1;
21018 linepos = BUF_BEGV (b);
21019 linepos_byte = BUF_BEGV_BYTE (b);
21020 }
21021
21022 /* Count lines from base line to window start position. */
21023 nlines = display_count_lines (linepos_byte,
21024 startpos_byte,
21025 startpos, &junk);
21026
21027 topline = nlines + line;
21028
21029 /* Determine a new base line, if the old one is too close
21030 or too far away, or if we did not have one.
21031 "Too close" means it's plausible a scroll-down would
21032 go back past it. */
21033 if (startpos == BUF_BEGV (b))
21034 {
21035 w->base_line_number = make_number (topline);
21036 w->base_line_pos = make_number (BUF_BEGV (b));
21037 }
21038 else if (nlines < height + 25 || nlines > height * 3 + 50
21039 || linepos == BUF_BEGV (b))
21040 {
21041 ptrdiff_t limit = BUF_BEGV (b);
21042 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21043 ptrdiff_t position;
21044 ptrdiff_t distance =
21045 (height * 2 + 30) * line_number_display_limit_width;
21046
21047 if (startpos - distance > limit)
21048 {
21049 limit = startpos - distance;
21050 limit_byte = CHAR_TO_BYTE (limit);
21051 }
21052
21053 nlines = display_count_lines (startpos_byte,
21054 limit_byte,
21055 - (height * 2 + 30),
21056 &position);
21057 /* If we couldn't find the lines we wanted within
21058 line_number_display_limit_width chars per line,
21059 give up on line numbers for this window. */
21060 if (position == limit_byte && limit == startpos - distance)
21061 {
21062 w->base_line_pos = w->buffer;
21063 w->base_line_number = Qnil;
21064 goto no_value;
21065 }
21066
21067 w->base_line_number = make_number (topline - nlines);
21068 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21069 }
21070
21071 /* Now count lines from the start pos to point. */
21072 nlines = display_count_lines (startpos_byte,
21073 PT_BYTE, PT, &junk);
21074
21075 /* Record that we did display the line number. */
21076 line_number_displayed = 1;
21077
21078 /* Make the string to show. */
21079 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21080 return decode_mode_spec_buf;
21081 no_value:
21082 {
21083 char* p = decode_mode_spec_buf;
21084 int pad = field_width - 2;
21085 while (pad-- > 0)
21086 *p++ = ' ';
21087 *p++ = '?';
21088 *p++ = '?';
21089 *p = '\0';
21090 return decode_mode_spec_buf;
21091 }
21092 }
21093 break;
21094
21095 case 'm':
21096 obj = BVAR (b, mode_name);
21097 break;
21098
21099 case 'n':
21100 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21101 return " Narrow";
21102 break;
21103
21104 case 'p':
21105 {
21106 ptrdiff_t pos = marker_position (w->start);
21107 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21108
21109 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21110 {
21111 if (pos <= BUF_BEGV (b))
21112 return "All";
21113 else
21114 return "Bottom";
21115 }
21116 else if (pos <= BUF_BEGV (b))
21117 return "Top";
21118 else
21119 {
21120 if (total > 1000000)
21121 /* Do it differently for a large value, to avoid overflow. */
21122 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21123 else
21124 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21125 /* We can't normally display a 3-digit number,
21126 so get us a 2-digit number that is close. */
21127 if (total == 100)
21128 total = 99;
21129 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21130 return decode_mode_spec_buf;
21131 }
21132 }
21133
21134 /* Display percentage of size above the bottom of the screen. */
21135 case 'P':
21136 {
21137 ptrdiff_t toppos = marker_position (w->start);
21138 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21139 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21140
21141 if (botpos >= BUF_ZV (b))
21142 {
21143 if (toppos <= BUF_BEGV (b))
21144 return "All";
21145 else
21146 return "Bottom";
21147 }
21148 else
21149 {
21150 if (total > 1000000)
21151 /* Do it differently for a large value, to avoid overflow. */
21152 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21153 else
21154 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21155 /* We can't normally display a 3-digit number,
21156 so get us a 2-digit number that is close. */
21157 if (total == 100)
21158 total = 99;
21159 if (toppos <= BUF_BEGV (b))
21160 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21161 else
21162 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21163 return decode_mode_spec_buf;
21164 }
21165 }
21166
21167 case 's':
21168 /* status of process */
21169 obj = Fget_buffer_process (Fcurrent_buffer ());
21170 if (NILP (obj))
21171 return "no process";
21172 #ifndef MSDOS
21173 obj = Fsymbol_name (Fprocess_status (obj));
21174 #endif
21175 break;
21176
21177 case '@':
21178 {
21179 ptrdiff_t count = inhibit_garbage_collection ();
21180 Lisp_Object val = call1 (intern ("file-remote-p"),
21181 BVAR (current_buffer, directory));
21182 unbind_to (count, Qnil);
21183
21184 if (NILP (val))
21185 return "-";
21186 else
21187 return "@";
21188 }
21189
21190 case 't': /* indicate TEXT or BINARY */
21191 return "T";
21192
21193 case 'z':
21194 /* coding-system (not including end-of-line format) */
21195 case 'Z':
21196 /* coding-system (including end-of-line type) */
21197 {
21198 int eol_flag = (c == 'Z');
21199 char *p = decode_mode_spec_buf;
21200
21201 if (! FRAME_WINDOW_P (f))
21202 {
21203 /* No need to mention EOL here--the terminal never needs
21204 to do EOL conversion. */
21205 p = decode_mode_spec_coding (CODING_ID_NAME
21206 (FRAME_KEYBOARD_CODING (f)->id),
21207 p, 0);
21208 p = decode_mode_spec_coding (CODING_ID_NAME
21209 (FRAME_TERMINAL_CODING (f)->id),
21210 p, 0);
21211 }
21212 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21213 p, eol_flag);
21214
21215 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21216 #ifdef subprocesses
21217 obj = Fget_buffer_process (Fcurrent_buffer ());
21218 if (PROCESSP (obj))
21219 {
21220 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21221 p, eol_flag);
21222 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21223 p, eol_flag);
21224 }
21225 #endif /* subprocesses */
21226 #endif /* 0 */
21227 *p = 0;
21228 return decode_mode_spec_buf;
21229 }
21230 }
21231
21232 if (STRINGP (obj))
21233 {
21234 *string = obj;
21235 return SSDATA (obj);
21236 }
21237 else
21238 return "";
21239 }
21240
21241
21242 /* Count up to COUNT lines starting from START_BYTE.
21243 But don't go beyond LIMIT_BYTE.
21244 Return the number of lines thus found (always nonnegative).
21245
21246 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21247
21248 static ptrdiff_t
21249 display_count_lines (ptrdiff_t start_byte,
21250 ptrdiff_t limit_byte, ptrdiff_t count,
21251 ptrdiff_t *byte_pos_ptr)
21252 {
21253 register unsigned char *cursor;
21254 unsigned char *base;
21255
21256 register ptrdiff_t ceiling;
21257 register unsigned char *ceiling_addr;
21258 ptrdiff_t orig_count = count;
21259
21260 /* If we are not in selective display mode,
21261 check only for newlines. */
21262 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21263 && !INTEGERP (BVAR (current_buffer, selective_display)));
21264
21265 if (count > 0)
21266 {
21267 while (start_byte < limit_byte)
21268 {
21269 ceiling = BUFFER_CEILING_OF (start_byte);
21270 ceiling = min (limit_byte - 1, ceiling);
21271 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21272 base = (cursor = BYTE_POS_ADDR (start_byte));
21273 while (1)
21274 {
21275 if (selective_display)
21276 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21277 ;
21278 else
21279 while (*cursor != '\n' && ++cursor != ceiling_addr)
21280 ;
21281
21282 if (cursor != ceiling_addr)
21283 {
21284 if (--count == 0)
21285 {
21286 start_byte += cursor - base + 1;
21287 *byte_pos_ptr = start_byte;
21288 return orig_count;
21289 }
21290 else
21291 if (++cursor == ceiling_addr)
21292 break;
21293 }
21294 else
21295 break;
21296 }
21297 start_byte += cursor - base;
21298 }
21299 }
21300 else
21301 {
21302 while (start_byte > limit_byte)
21303 {
21304 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21305 ceiling = max (limit_byte, ceiling);
21306 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21307 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21308 while (1)
21309 {
21310 if (selective_display)
21311 while (--cursor != ceiling_addr
21312 && *cursor != '\n' && *cursor != 015)
21313 ;
21314 else
21315 while (--cursor != ceiling_addr && *cursor != '\n')
21316 ;
21317
21318 if (cursor != ceiling_addr)
21319 {
21320 if (++count == 0)
21321 {
21322 start_byte += cursor - base + 1;
21323 *byte_pos_ptr = start_byte;
21324 /* When scanning backwards, we should
21325 not count the newline posterior to which we stop. */
21326 return - orig_count - 1;
21327 }
21328 }
21329 else
21330 break;
21331 }
21332 /* Here we add 1 to compensate for the last decrement
21333 of CURSOR, which took it past the valid range. */
21334 start_byte += cursor - base + 1;
21335 }
21336 }
21337
21338 *byte_pos_ptr = limit_byte;
21339
21340 if (count < 0)
21341 return - orig_count + count;
21342 return orig_count - count;
21343
21344 }
21345
21346
21347 \f
21348 /***********************************************************************
21349 Displaying strings
21350 ***********************************************************************/
21351
21352 /* Display a NUL-terminated string, starting with index START.
21353
21354 If STRING is non-null, display that C string. Otherwise, the Lisp
21355 string LISP_STRING is displayed. There's a case that STRING is
21356 non-null and LISP_STRING is not nil. It means STRING is a string
21357 data of LISP_STRING. In that case, we display LISP_STRING while
21358 ignoring its text properties.
21359
21360 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21361 FACE_STRING. Display STRING or LISP_STRING with the face at
21362 FACE_STRING_POS in FACE_STRING:
21363
21364 Display the string in the environment given by IT, but use the
21365 standard display table, temporarily.
21366
21367 FIELD_WIDTH is the minimum number of output glyphs to produce.
21368 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21369 with spaces. If STRING has more characters, more than FIELD_WIDTH
21370 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21371
21372 PRECISION is the maximum number of characters to output from
21373 STRING. PRECISION < 0 means don't truncate the string.
21374
21375 This is roughly equivalent to printf format specifiers:
21376
21377 FIELD_WIDTH PRECISION PRINTF
21378 ----------------------------------------
21379 -1 -1 %s
21380 -1 10 %.10s
21381 10 -1 %10s
21382 20 10 %20.10s
21383
21384 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21385 display them, and < 0 means obey the current buffer's value of
21386 enable_multibyte_characters.
21387
21388 Value is the number of columns displayed. */
21389
21390 static int
21391 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21392 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21393 int field_width, int precision, int max_x, int multibyte)
21394 {
21395 int hpos_at_start = it->hpos;
21396 int saved_face_id = it->face_id;
21397 struct glyph_row *row = it->glyph_row;
21398 ptrdiff_t it_charpos;
21399
21400 /* Initialize the iterator IT for iteration over STRING beginning
21401 with index START. */
21402 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21403 precision, field_width, multibyte);
21404 if (string && STRINGP (lisp_string))
21405 /* LISP_STRING is the one returned by decode_mode_spec. We should
21406 ignore its text properties. */
21407 it->stop_charpos = it->end_charpos;
21408
21409 /* If displaying STRING, set up the face of the iterator from
21410 FACE_STRING, if that's given. */
21411 if (STRINGP (face_string))
21412 {
21413 ptrdiff_t endptr;
21414 struct face *face;
21415
21416 it->face_id
21417 = face_at_string_position (it->w, face_string, face_string_pos,
21418 0, it->region_beg_charpos,
21419 it->region_end_charpos,
21420 &endptr, it->base_face_id, 0);
21421 face = FACE_FROM_ID (it->f, it->face_id);
21422 it->face_box_p = face->box != FACE_NO_BOX;
21423 }
21424
21425 /* Set max_x to the maximum allowed X position. Don't let it go
21426 beyond the right edge of the window. */
21427 if (max_x <= 0)
21428 max_x = it->last_visible_x;
21429 else
21430 max_x = min (max_x, it->last_visible_x);
21431
21432 /* Skip over display elements that are not visible. because IT->w is
21433 hscrolled. */
21434 if (it->current_x < it->first_visible_x)
21435 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21436 MOVE_TO_POS | MOVE_TO_X);
21437
21438 row->ascent = it->max_ascent;
21439 row->height = it->max_ascent + it->max_descent;
21440 row->phys_ascent = it->max_phys_ascent;
21441 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21442 row->extra_line_spacing = it->max_extra_line_spacing;
21443
21444 if (STRINGP (it->string))
21445 it_charpos = IT_STRING_CHARPOS (*it);
21446 else
21447 it_charpos = IT_CHARPOS (*it);
21448
21449 /* This condition is for the case that we are called with current_x
21450 past last_visible_x. */
21451 while (it->current_x < max_x)
21452 {
21453 int x_before, x, n_glyphs_before, i, nglyphs;
21454
21455 /* Get the next display element. */
21456 if (!get_next_display_element (it))
21457 break;
21458
21459 /* Produce glyphs. */
21460 x_before = it->current_x;
21461 n_glyphs_before = row->used[TEXT_AREA];
21462 PRODUCE_GLYPHS (it);
21463
21464 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21465 i = 0;
21466 x = x_before;
21467 while (i < nglyphs)
21468 {
21469 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21470
21471 if (it->line_wrap != TRUNCATE
21472 && x + glyph->pixel_width > max_x)
21473 {
21474 /* End of continued line or max_x reached. */
21475 if (CHAR_GLYPH_PADDING_P (*glyph))
21476 {
21477 /* A wide character is unbreakable. */
21478 if (row->reversed_p)
21479 unproduce_glyphs (it, row->used[TEXT_AREA]
21480 - n_glyphs_before);
21481 row->used[TEXT_AREA] = n_glyphs_before;
21482 it->current_x = x_before;
21483 }
21484 else
21485 {
21486 if (row->reversed_p)
21487 unproduce_glyphs (it, row->used[TEXT_AREA]
21488 - (n_glyphs_before + i));
21489 row->used[TEXT_AREA] = n_glyphs_before + i;
21490 it->current_x = x;
21491 }
21492 break;
21493 }
21494 else if (x + glyph->pixel_width >= it->first_visible_x)
21495 {
21496 /* Glyph is at least partially visible. */
21497 ++it->hpos;
21498 if (x < it->first_visible_x)
21499 row->x = x - it->first_visible_x;
21500 }
21501 else
21502 {
21503 /* Glyph is off the left margin of the display area.
21504 Should not happen. */
21505 abort ();
21506 }
21507
21508 row->ascent = max (row->ascent, it->max_ascent);
21509 row->height = max (row->height, it->max_ascent + it->max_descent);
21510 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21511 row->phys_height = max (row->phys_height,
21512 it->max_phys_ascent + it->max_phys_descent);
21513 row->extra_line_spacing = max (row->extra_line_spacing,
21514 it->max_extra_line_spacing);
21515 x += glyph->pixel_width;
21516 ++i;
21517 }
21518
21519 /* Stop if max_x reached. */
21520 if (i < nglyphs)
21521 break;
21522
21523 /* Stop at line ends. */
21524 if (ITERATOR_AT_END_OF_LINE_P (it))
21525 {
21526 it->continuation_lines_width = 0;
21527 break;
21528 }
21529
21530 set_iterator_to_next (it, 1);
21531 if (STRINGP (it->string))
21532 it_charpos = IT_STRING_CHARPOS (*it);
21533 else
21534 it_charpos = IT_CHARPOS (*it);
21535
21536 /* Stop if truncating at the right edge. */
21537 if (it->line_wrap == TRUNCATE
21538 && it->current_x >= it->last_visible_x)
21539 {
21540 /* Add truncation mark, but don't do it if the line is
21541 truncated at a padding space. */
21542 if (it_charpos < it->string_nchars)
21543 {
21544 if (!FRAME_WINDOW_P (it->f))
21545 {
21546 int ii, n;
21547
21548 if (it->current_x > it->last_visible_x)
21549 {
21550 if (!row->reversed_p)
21551 {
21552 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21553 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21554 break;
21555 }
21556 else
21557 {
21558 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21559 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21560 break;
21561 unproduce_glyphs (it, ii + 1);
21562 ii = row->used[TEXT_AREA] - (ii + 1);
21563 }
21564 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21565 {
21566 row->used[TEXT_AREA] = ii;
21567 produce_special_glyphs (it, IT_TRUNCATION);
21568 }
21569 }
21570 produce_special_glyphs (it, IT_TRUNCATION);
21571 }
21572 row->truncated_on_right_p = 1;
21573 }
21574 break;
21575 }
21576 }
21577
21578 /* Maybe insert a truncation at the left. */
21579 if (it->first_visible_x
21580 && it_charpos > 0)
21581 {
21582 if (!FRAME_WINDOW_P (it->f))
21583 insert_left_trunc_glyphs (it);
21584 row->truncated_on_left_p = 1;
21585 }
21586
21587 it->face_id = saved_face_id;
21588
21589 /* Value is number of columns displayed. */
21590 return it->hpos - hpos_at_start;
21591 }
21592
21593
21594 \f
21595 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21596 appears as an element of LIST or as the car of an element of LIST.
21597 If PROPVAL is a list, compare each element against LIST in that
21598 way, and return 1/2 if any element of PROPVAL is found in LIST.
21599 Otherwise return 0. This function cannot quit.
21600 The return value is 2 if the text is invisible but with an ellipsis
21601 and 1 if it's invisible and without an ellipsis. */
21602
21603 int
21604 invisible_p (register Lisp_Object propval, Lisp_Object list)
21605 {
21606 register Lisp_Object tail, proptail;
21607
21608 for (tail = list; CONSP (tail); tail = XCDR (tail))
21609 {
21610 register Lisp_Object tem;
21611 tem = XCAR (tail);
21612 if (EQ (propval, tem))
21613 return 1;
21614 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21615 return NILP (XCDR (tem)) ? 1 : 2;
21616 }
21617
21618 if (CONSP (propval))
21619 {
21620 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21621 {
21622 Lisp_Object propelt;
21623 propelt = XCAR (proptail);
21624 for (tail = list; CONSP (tail); tail = XCDR (tail))
21625 {
21626 register Lisp_Object tem;
21627 tem = XCAR (tail);
21628 if (EQ (propelt, tem))
21629 return 1;
21630 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21631 return NILP (XCDR (tem)) ? 1 : 2;
21632 }
21633 }
21634 }
21635
21636 return 0;
21637 }
21638
21639 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21640 doc: /* Non-nil if the property makes the text invisible.
21641 POS-OR-PROP can be a marker or number, in which case it is taken to be
21642 a position in the current buffer and the value of the `invisible' property
21643 is checked; or it can be some other value, which is then presumed to be the
21644 value of the `invisible' property of the text of interest.
21645 The non-nil value returned can be t for truly invisible text or something
21646 else if the text is replaced by an ellipsis. */)
21647 (Lisp_Object pos_or_prop)
21648 {
21649 Lisp_Object prop
21650 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21651 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21652 : pos_or_prop);
21653 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21654 return (invis == 0 ? Qnil
21655 : invis == 1 ? Qt
21656 : make_number (invis));
21657 }
21658
21659 /* Calculate a width or height in pixels from a specification using
21660 the following elements:
21661
21662 SPEC ::=
21663 NUM - a (fractional) multiple of the default font width/height
21664 (NUM) - specifies exactly NUM pixels
21665 UNIT - a fixed number of pixels, see below.
21666 ELEMENT - size of a display element in pixels, see below.
21667 (NUM . SPEC) - equals NUM * SPEC
21668 (+ SPEC SPEC ...) - add pixel values
21669 (- SPEC SPEC ...) - subtract pixel values
21670 (- SPEC) - negate pixel value
21671
21672 NUM ::=
21673 INT or FLOAT - a number constant
21674 SYMBOL - use symbol's (buffer local) variable binding.
21675
21676 UNIT ::=
21677 in - pixels per inch *)
21678 mm - pixels per 1/1000 meter *)
21679 cm - pixels per 1/100 meter *)
21680 width - width of current font in pixels.
21681 height - height of current font in pixels.
21682
21683 *) using the ratio(s) defined in display-pixels-per-inch.
21684
21685 ELEMENT ::=
21686
21687 left-fringe - left fringe width in pixels
21688 right-fringe - right fringe width in pixels
21689
21690 left-margin - left margin width in pixels
21691 right-margin - right margin width in pixels
21692
21693 scroll-bar - scroll-bar area width in pixels
21694
21695 Examples:
21696
21697 Pixels corresponding to 5 inches:
21698 (5 . in)
21699
21700 Total width of non-text areas on left side of window (if scroll-bar is on left):
21701 '(space :width (+ left-fringe left-margin scroll-bar))
21702
21703 Align to first text column (in header line):
21704 '(space :align-to 0)
21705
21706 Align to middle of text area minus half the width of variable `my-image'
21707 containing a loaded image:
21708 '(space :align-to (0.5 . (- text my-image)))
21709
21710 Width of left margin minus width of 1 character in the default font:
21711 '(space :width (- left-margin 1))
21712
21713 Width of left margin minus width of 2 characters in the current font:
21714 '(space :width (- left-margin (2 . width)))
21715
21716 Center 1 character over left-margin (in header line):
21717 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21718
21719 Different ways to express width of left fringe plus left margin minus one pixel:
21720 '(space :width (- (+ left-fringe left-margin) (1)))
21721 '(space :width (+ left-fringe left-margin (- (1))))
21722 '(space :width (+ left-fringe left-margin (-1)))
21723
21724 */
21725
21726 #define NUMVAL(X) \
21727 ((INTEGERP (X) || FLOATP (X)) \
21728 ? XFLOATINT (X) \
21729 : - 1)
21730
21731 static int
21732 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
21733 struct font *font, int width_p, int *align_to)
21734 {
21735 double pixels;
21736
21737 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
21738 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
21739
21740 if (NILP (prop))
21741 return OK_PIXELS (0);
21742
21743 xassert (FRAME_LIVE_P (it->f));
21744
21745 if (SYMBOLP (prop))
21746 {
21747 if (SCHARS (SYMBOL_NAME (prop)) == 2)
21748 {
21749 char *unit = SSDATA (SYMBOL_NAME (prop));
21750
21751 if (unit[0] == 'i' && unit[1] == 'n')
21752 pixels = 1.0;
21753 else if (unit[0] == 'm' && unit[1] == 'm')
21754 pixels = 25.4;
21755 else if (unit[0] == 'c' && unit[1] == 'm')
21756 pixels = 2.54;
21757 else
21758 pixels = 0;
21759 if (pixels > 0)
21760 {
21761 double ppi;
21762 #ifdef HAVE_WINDOW_SYSTEM
21763 if (FRAME_WINDOW_P (it->f)
21764 && (ppi = (width_p
21765 ? FRAME_X_DISPLAY_INFO (it->f)->resx
21766 : FRAME_X_DISPLAY_INFO (it->f)->resy),
21767 ppi > 0))
21768 return OK_PIXELS (ppi / pixels);
21769 #endif
21770
21771 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
21772 || (CONSP (Vdisplay_pixels_per_inch)
21773 && (ppi = (width_p
21774 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
21775 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
21776 ppi > 0)))
21777 return OK_PIXELS (ppi / pixels);
21778
21779 return 0;
21780 }
21781 }
21782
21783 #ifdef HAVE_WINDOW_SYSTEM
21784 if (EQ (prop, Qheight))
21785 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
21786 if (EQ (prop, Qwidth))
21787 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
21788 #else
21789 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
21790 return OK_PIXELS (1);
21791 #endif
21792
21793 if (EQ (prop, Qtext))
21794 return OK_PIXELS (width_p
21795 ? window_box_width (it->w, TEXT_AREA)
21796 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
21797
21798 if (align_to && *align_to < 0)
21799 {
21800 *res = 0;
21801 if (EQ (prop, Qleft))
21802 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
21803 if (EQ (prop, Qright))
21804 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
21805 if (EQ (prop, Qcenter))
21806 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
21807 + window_box_width (it->w, TEXT_AREA) / 2);
21808 if (EQ (prop, Qleft_fringe))
21809 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21810 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
21811 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
21812 if (EQ (prop, Qright_fringe))
21813 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21814 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21815 : window_box_right_offset (it->w, TEXT_AREA));
21816 if (EQ (prop, Qleft_margin))
21817 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
21818 if (EQ (prop, Qright_margin))
21819 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
21820 if (EQ (prop, Qscroll_bar))
21821 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
21822 ? 0
21823 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
21824 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
21825 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21826 : 0)));
21827 }
21828 else
21829 {
21830 if (EQ (prop, Qleft_fringe))
21831 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
21832 if (EQ (prop, Qright_fringe))
21833 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
21834 if (EQ (prop, Qleft_margin))
21835 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
21836 if (EQ (prop, Qright_margin))
21837 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
21838 if (EQ (prop, Qscroll_bar))
21839 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
21840 }
21841
21842 prop = Fbuffer_local_value (prop, it->w->buffer);
21843 }
21844
21845 if (INTEGERP (prop) || FLOATP (prop))
21846 {
21847 int base_unit = (width_p
21848 ? FRAME_COLUMN_WIDTH (it->f)
21849 : FRAME_LINE_HEIGHT (it->f));
21850 return OK_PIXELS (XFLOATINT (prop) * base_unit);
21851 }
21852
21853 if (CONSP (prop))
21854 {
21855 Lisp_Object car = XCAR (prop);
21856 Lisp_Object cdr = XCDR (prop);
21857
21858 if (SYMBOLP (car))
21859 {
21860 #ifdef HAVE_WINDOW_SYSTEM
21861 if (FRAME_WINDOW_P (it->f)
21862 && valid_image_p (prop))
21863 {
21864 ptrdiff_t id = lookup_image (it->f, prop);
21865 struct image *img = IMAGE_FROM_ID (it->f, id);
21866
21867 return OK_PIXELS (width_p ? img->width : img->height);
21868 }
21869 #endif
21870 if (EQ (car, Qplus) || EQ (car, Qminus))
21871 {
21872 int first = 1;
21873 double px;
21874
21875 pixels = 0;
21876 while (CONSP (cdr))
21877 {
21878 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
21879 font, width_p, align_to))
21880 return 0;
21881 if (first)
21882 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
21883 else
21884 pixels += px;
21885 cdr = XCDR (cdr);
21886 }
21887 if (EQ (car, Qminus))
21888 pixels = -pixels;
21889 return OK_PIXELS (pixels);
21890 }
21891
21892 car = Fbuffer_local_value (car, it->w->buffer);
21893 }
21894
21895 if (INTEGERP (car) || FLOATP (car))
21896 {
21897 double fact;
21898 pixels = XFLOATINT (car);
21899 if (NILP (cdr))
21900 return OK_PIXELS (pixels);
21901 if (calc_pixel_width_or_height (&fact, it, cdr,
21902 font, width_p, align_to))
21903 return OK_PIXELS (pixels * fact);
21904 return 0;
21905 }
21906
21907 return 0;
21908 }
21909
21910 return 0;
21911 }
21912
21913 \f
21914 /***********************************************************************
21915 Glyph Display
21916 ***********************************************************************/
21917
21918 #ifdef HAVE_WINDOW_SYSTEM
21919
21920 #if GLYPH_DEBUG
21921
21922 void
21923 dump_glyph_string (struct glyph_string *s)
21924 {
21925 fprintf (stderr, "glyph string\n");
21926 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
21927 s->x, s->y, s->width, s->height);
21928 fprintf (stderr, " ybase = %d\n", s->ybase);
21929 fprintf (stderr, " hl = %d\n", s->hl);
21930 fprintf (stderr, " left overhang = %d, right = %d\n",
21931 s->left_overhang, s->right_overhang);
21932 fprintf (stderr, " nchars = %d\n", s->nchars);
21933 fprintf (stderr, " extends to end of line = %d\n",
21934 s->extends_to_end_of_line_p);
21935 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
21936 fprintf (stderr, " bg width = %d\n", s->background_width);
21937 }
21938
21939 #endif /* GLYPH_DEBUG */
21940
21941 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
21942 of XChar2b structures for S; it can't be allocated in
21943 init_glyph_string because it must be allocated via `alloca'. W
21944 is the window on which S is drawn. ROW and AREA are the glyph row
21945 and area within the row from which S is constructed. START is the
21946 index of the first glyph structure covered by S. HL is a
21947 face-override for drawing S. */
21948
21949 #ifdef HAVE_NTGUI
21950 #define OPTIONAL_HDC(hdc) HDC hdc,
21951 #define DECLARE_HDC(hdc) HDC hdc;
21952 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
21953 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
21954 #endif
21955
21956 #ifndef OPTIONAL_HDC
21957 #define OPTIONAL_HDC(hdc)
21958 #define DECLARE_HDC(hdc)
21959 #define ALLOCATE_HDC(hdc, f)
21960 #define RELEASE_HDC(hdc, f)
21961 #endif
21962
21963 static void
21964 init_glyph_string (struct glyph_string *s,
21965 OPTIONAL_HDC (hdc)
21966 XChar2b *char2b, struct window *w, struct glyph_row *row,
21967 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
21968 {
21969 memset (s, 0, sizeof *s);
21970 s->w = w;
21971 s->f = XFRAME (w->frame);
21972 #ifdef HAVE_NTGUI
21973 s->hdc = hdc;
21974 #endif
21975 s->display = FRAME_X_DISPLAY (s->f);
21976 s->window = FRAME_X_WINDOW (s->f);
21977 s->char2b = char2b;
21978 s->hl = hl;
21979 s->row = row;
21980 s->area = area;
21981 s->first_glyph = row->glyphs[area] + start;
21982 s->height = row->height;
21983 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
21984 s->ybase = s->y + row->ascent;
21985 }
21986
21987
21988 /* Append the list of glyph strings with head H and tail T to the list
21989 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
21990
21991 static inline void
21992 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
21993 struct glyph_string *h, struct glyph_string *t)
21994 {
21995 if (h)
21996 {
21997 if (*head)
21998 (*tail)->next = h;
21999 else
22000 *head = h;
22001 h->prev = *tail;
22002 *tail = t;
22003 }
22004 }
22005
22006
22007 /* Prepend the list of glyph strings with head H and tail T to the
22008 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22009 result. */
22010
22011 static inline void
22012 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22013 struct glyph_string *h, struct glyph_string *t)
22014 {
22015 if (h)
22016 {
22017 if (*head)
22018 (*head)->prev = t;
22019 else
22020 *tail = t;
22021 t->next = *head;
22022 *head = h;
22023 }
22024 }
22025
22026
22027 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22028 Set *HEAD and *TAIL to the resulting list. */
22029
22030 static inline void
22031 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22032 struct glyph_string *s)
22033 {
22034 s->next = s->prev = NULL;
22035 append_glyph_string_lists (head, tail, s, s);
22036 }
22037
22038
22039 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22040 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22041 make sure that X resources for the face returned are allocated.
22042 Value is a pointer to a realized face that is ready for display if
22043 DISPLAY_P is non-zero. */
22044
22045 static inline struct face *
22046 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22047 XChar2b *char2b, int display_p)
22048 {
22049 struct face *face = FACE_FROM_ID (f, face_id);
22050
22051 if (face->font)
22052 {
22053 unsigned code = face->font->driver->encode_char (face->font, c);
22054
22055 if (code != FONT_INVALID_CODE)
22056 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22057 else
22058 STORE_XCHAR2B (char2b, 0, 0);
22059 }
22060
22061 /* Make sure X resources of the face are allocated. */
22062 #ifdef HAVE_X_WINDOWS
22063 if (display_p)
22064 #endif
22065 {
22066 xassert (face != NULL);
22067 PREPARE_FACE_FOR_DISPLAY (f, face);
22068 }
22069
22070 return face;
22071 }
22072
22073
22074 /* Get face and two-byte form of character glyph GLYPH on frame F.
22075 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22076 a pointer to a realized face that is ready for display. */
22077
22078 static inline struct face *
22079 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22080 XChar2b *char2b, int *two_byte_p)
22081 {
22082 struct face *face;
22083
22084 xassert (glyph->type == CHAR_GLYPH);
22085 face = FACE_FROM_ID (f, glyph->face_id);
22086
22087 if (two_byte_p)
22088 *two_byte_p = 0;
22089
22090 if (face->font)
22091 {
22092 unsigned code;
22093
22094 if (CHAR_BYTE8_P (glyph->u.ch))
22095 code = CHAR_TO_BYTE8 (glyph->u.ch);
22096 else
22097 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22098
22099 if (code != FONT_INVALID_CODE)
22100 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22101 else
22102 STORE_XCHAR2B (char2b, 0, 0);
22103 }
22104
22105 /* Make sure X resources of the face are allocated. */
22106 xassert (face != NULL);
22107 PREPARE_FACE_FOR_DISPLAY (f, face);
22108 return face;
22109 }
22110
22111
22112 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22113 Return 1 if FONT has a glyph for C, otherwise return 0. */
22114
22115 static inline int
22116 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22117 {
22118 unsigned code;
22119
22120 if (CHAR_BYTE8_P (c))
22121 code = CHAR_TO_BYTE8 (c);
22122 else
22123 code = font->driver->encode_char (font, c);
22124
22125 if (code == FONT_INVALID_CODE)
22126 return 0;
22127 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22128 return 1;
22129 }
22130
22131
22132 /* Fill glyph string S with composition components specified by S->cmp.
22133
22134 BASE_FACE is the base face of the composition.
22135 S->cmp_from is the index of the first component for S.
22136
22137 OVERLAPS non-zero means S should draw the foreground only, and use
22138 its physical height for clipping. See also draw_glyphs.
22139
22140 Value is the index of a component not in S. */
22141
22142 static int
22143 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22144 int overlaps)
22145 {
22146 int i;
22147 /* For all glyphs of this composition, starting at the offset
22148 S->cmp_from, until we reach the end of the definition or encounter a
22149 glyph that requires the different face, add it to S. */
22150 struct face *face;
22151
22152 xassert (s);
22153
22154 s->for_overlaps = overlaps;
22155 s->face = NULL;
22156 s->font = NULL;
22157 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22158 {
22159 int c = COMPOSITION_GLYPH (s->cmp, i);
22160
22161 /* TAB in a composition means display glyphs with padding space
22162 on the left or right. */
22163 if (c != '\t')
22164 {
22165 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22166 -1, Qnil);
22167
22168 face = get_char_face_and_encoding (s->f, c, face_id,
22169 s->char2b + i, 1);
22170 if (face)
22171 {
22172 if (! s->face)
22173 {
22174 s->face = face;
22175 s->font = s->face->font;
22176 }
22177 else if (s->face != face)
22178 break;
22179 }
22180 }
22181 ++s->nchars;
22182 }
22183 s->cmp_to = i;
22184
22185 if (s->face == NULL)
22186 {
22187 s->face = base_face->ascii_face;
22188 s->font = s->face->font;
22189 }
22190
22191 /* All glyph strings for the same composition has the same width,
22192 i.e. the width set for the first component of the composition. */
22193 s->width = s->first_glyph->pixel_width;
22194
22195 /* If the specified font could not be loaded, use the frame's
22196 default font, but record the fact that we couldn't load it in
22197 the glyph string so that we can draw rectangles for the
22198 characters of the glyph string. */
22199 if (s->font == NULL)
22200 {
22201 s->font_not_found_p = 1;
22202 s->font = FRAME_FONT (s->f);
22203 }
22204
22205 /* Adjust base line for subscript/superscript text. */
22206 s->ybase += s->first_glyph->voffset;
22207
22208 /* This glyph string must always be drawn with 16-bit functions. */
22209 s->two_byte_p = 1;
22210
22211 return s->cmp_to;
22212 }
22213
22214 static int
22215 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22216 int start, int end, int overlaps)
22217 {
22218 struct glyph *glyph, *last;
22219 Lisp_Object lgstring;
22220 int i;
22221
22222 s->for_overlaps = overlaps;
22223 glyph = s->row->glyphs[s->area] + start;
22224 last = s->row->glyphs[s->area] + end;
22225 s->cmp_id = glyph->u.cmp.id;
22226 s->cmp_from = glyph->slice.cmp.from;
22227 s->cmp_to = glyph->slice.cmp.to + 1;
22228 s->face = FACE_FROM_ID (s->f, face_id);
22229 lgstring = composition_gstring_from_id (s->cmp_id);
22230 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22231 glyph++;
22232 while (glyph < last
22233 && glyph->u.cmp.automatic
22234 && glyph->u.cmp.id == s->cmp_id
22235 && s->cmp_to == glyph->slice.cmp.from)
22236 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22237
22238 for (i = s->cmp_from; i < s->cmp_to; i++)
22239 {
22240 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22241 unsigned code = LGLYPH_CODE (lglyph);
22242
22243 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22244 }
22245 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22246 return glyph - s->row->glyphs[s->area];
22247 }
22248
22249
22250 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22251 See the comment of fill_glyph_string for arguments.
22252 Value is the index of the first glyph not in S. */
22253
22254
22255 static int
22256 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22257 int start, int end, int overlaps)
22258 {
22259 struct glyph *glyph, *last;
22260 int voffset;
22261
22262 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22263 s->for_overlaps = overlaps;
22264 glyph = s->row->glyphs[s->area] + start;
22265 last = s->row->glyphs[s->area] + end;
22266 voffset = glyph->voffset;
22267 s->face = FACE_FROM_ID (s->f, face_id);
22268 s->font = s->face->font;
22269 s->nchars = 1;
22270 s->width = glyph->pixel_width;
22271 glyph++;
22272 while (glyph < last
22273 && glyph->type == GLYPHLESS_GLYPH
22274 && glyph->voffset == voffset
22275 && glyph->face_id == face_id)
22276 {
22277 s->nchars++;
22278 s->width += glyph->pixel_width;
22279 glyph++;
22280 }
22281 s->ybase += voffset;
22282 return glyph - s->row->glyphs[s->area];
22283 }
22284
22285
22286 /* Fill glyph string S from a sequence of character glyphs.
22287
22288 FACE_ID is the face id of the string. START is the index of the
22289 first glyph to consider, END is the index of the last + 1.
22290 OVERLAPS non-zero means S should draw the foreground only, and use
22291 its physical height for clipping. See also draw_glyphs.
22292
22293 Value is the index of the first glyph not in S. */
22294
22295 static int
22296 fill_glyph_string (struct glyph_string *s, int face_id,
22297 int start, int end, int overlaps)
22298 {
22299 struct glyph *glyph, *last;
22300 int voffset;
22301 int glyph_not_available_p;
22302
22303 xassert (s->f == XFRAME (s->w->frame));
22304 xassert (s->nchars == 0);
22305 xassert (start >= 0 && end > start);
22306
22307 s->for_overlaps = overlaps;
22308 glyph = s->row->glyphs[s->area] + start;
22309 last = s->row->glyphs[s->area] + end;
22310 voffset = glyph->voffset;
22311 s->padding_p = glyph->padding_p;
22312 glyph_not_available_p = glyph->glyph_not_available_p;
22313
22314 while (glyph < last
22315 && glyph->type == CHAR_GLYPH
22316 && glyph->voffset == voffset
22317 /* Same face id implies same font, nowadays. */
22318 && glyph->face_id == face_id
22319 && glyph->glyph_not_available_p == glyph_not_available_p)
22320 {
22321 int two_byte_p;
22322
22323 s->face = get_glyph_face_and_encoding (s->f, glyph,
22324 s->char2b + s->nchars,
22325 &two_byte_p);
22326 s->two_byte_p = two_byte_p;
22327 ++s->nchars;
22328 xassert (s->nchars <= end - start);
22329 s->width += glyph->pixel_width;
22330 if (glyph++->padding_p != s->padding_p)
22331 break;
22332 }
22333
22334 s->font = s->face->font;
22335
22336 /* If the specified font could not be loaded, use the frame's font,
22337 but record the fact that we couldn't load it in
22338 S->font_not_found_p so that we can draw rectangles for the
22339 characters of the glyph string. */
22340 if (s->font == NULL || glyph_not_available_p)
22341 {
22342 s->font_not_found_p = 1;
22343 s->font = FRAME_FONT (s->f);
22344 }
22345
22346 /* Adjust base line for subscript/superscript text. */
22347 s->ybase += voffset;
22348
22349 xassert (s->face && s->face->gc);
22350 return glyph - s->row->glyphs[s->area];
22351 }
22352
22353
22354 /* Fill glyph string S from image glyph S->first_glyph. */
22355
22356 static void
22357 fill_image_glyph_string (struct glyph_string *s)
22358 {
22359 xassert (s->first_glyph->type == IMAGE_GLYPH);
22360 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22361 xassert (s->img);
22362 s->slice = s->first_glyph->slice.img;
22363 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22364 s->font = s->face->font;
22365 s->width = s->first_glyph->pixel_width;
22366
22367 /* Adjust base line for subscript/superscript text. */
22368 s->ybase += s->first_glyph->voffset;
22369 }
22370
22371
22372 /* Fill glyph string S from a sequence of stretch glyphs.
22373
22374 START is the index of the first glyph to consider,
22375 END is the index of the last + 1.
22376
22377 Value is the index of the first glyph not in S. */
22378
22379 static int
22380 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22381 {
22382 struct glyph *glyph, *last;
22383 int voffset, face_id;
22384
22385 xassert (s->first_glyph->type == STRETCH_GLYPH);
22386
22387 glyph = s->row->glyphs[s->area] + start;
22388 last = s->row->glyphs[s->area] + end;
22389 face_id = glyph->face_id;
22390 s->face = FACE_FROM_ID (s->f, face_id);
22391 s->font = s->face->font;
22392 s->width = glyph->pixel_width;
22393 s->nchars = 1;
22394 voffset = glyph->voffset;
22395
22396 for (++glyph;
22397 (glyph < last
22398 && glyph->type == STRETCH_GLYPH
22399 && glyph->voffset == voffset
22400 && glyph->face_id == face_id);
22401 ++glyph)
22402 s->width += glyph->pixel_width;
22403
22404 /* Adjust base line for subscript/superscript text. */
22405 s->ybase += voffset;
22406
22407 /* The case that face->gc == 0 is handled when drawing the glyph
22408 string by calling PREPARE_FACE_FOR_DISPLAY. */
22409 xassert (s->face);
22410 return glyph - s->row->glyphs[s->area];
22411 }
22412
22413 static struct font_metrics *
22414 get_per_char_metric (struct font *font, XChar2b *char2b)
22415 {
22416 static struct font_metrics metrics;
22417 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22418
22419 if (! font || code == FONT_INVALID_CODE)
22420 return NULL;
22421 font->driver->text_extents (font, &code, 1, &metrics);
22422 return &metrics;
22423 }
22424
22425 /* EXPORT for RIF:
22426 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22427 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22428 assumed to be zero. */
22429
22430 void
22431 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22432 {
22433 *left = *right = 0;
22434
22435 if (glyph->type == CHAR_GLYPH)
22436 {
22437 struct face *face;
22438 XChar2b char2b;
22439 struct font_metrics *pcm;
22440
22441 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22442 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22443 {
22444 if (pcm->rbearing > pcm->width)
22445 *right = pcm->rbearing - pcm->width;
22446 if (pcm->lbearing < 0)
22447 *left = -pcm->lbearing;
22448 }
22449 }
22450 else if (glyph->type == COMPOSITE_GLYPH)
22451 {
22452 if (! glyph->u.cmp.automatic)
22453 {
22454 struct composition *cmp = composition_table[glyph->u.cmp.id];
22455
22456 if (cmp->rbearing > cmp->pixel_width)
22457 *right = cmp->rbearing - cmp->pixel_width;
22458 if (cmp->lbearing < 0)
22459 *left = - cmp->lbearing;
22460 }
22461 else
22462 {
22463 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22464 struct font_metrics metrics;
22465
22466 composition_gstring_width (gstring, glyph->slice.cmp.from,
22467 glyph->slice.cmp.to + 1, &metrics);
22468 if (metrics.rbearing > metrics.width)
22469 *right = metrics.rbearing - metrics.width;
22470 if (metrics.lbearing < 0)
22471 *left = - metrics.lbearing;
22472 }
22473 }
22474 }
22475
22476
22477 /* Return the index of the first glyph preceding glyph string S that
22478 is overwritten by S because of S's left overhang. Value is -1
22479 if no glyphs are overwritten. */
22480
22481 static int
22482 left_overwritten (struct glyph_string *s)
22483 {
22484 int k;
22485
22486 if (s->left_overhang)
22487 {
22488 int x = 0, i;
22489 struct glyph *glyphs = s->row->glyphs[s->area];
22490 int first = s->first_glyph - glyphs;
22491
22492 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22493 x -= glyphs[i].pixel_width;
22494
22495 k = i + 1;
22496 }
22497 else
22498 k = -1;
22499
22500 return k;
22501 }
22502
22503
22504 /* Return the index of the first glyph preceding glyph string S that
22505 is overwriting S because of its right overhang. Value is -1 if no
22506 glyph in front of S overwrites S. */
22507
22508 static int
22509 left_overwriting (struct glyph_string *s)
22510 {
22511 int i, k, x;
22512 struct glyph *glyphs = s->row->glyphs[s->area];
22513 int first = s->first_glyph - glyphs;
22514
22515 k = -1;
22516 x = 0;
22517 for (i = first - 1; i >= 0; --i)
22518 {
22519 int left, right;
22520 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22521 if (x + right > 0)
22522 k = i;
22523 x -= glyphs[i].pixel_width;
22524 }
22525
22526 return k;
22527 }
22528
22529
22530 /* Return the index of the last glyph following glyph string S that is
22531 overwritten by S because of S's right overhang. Value is -1 if
22532 no such glyph is found. */
22533
22534 static int
22535 right_overwritten (struct glyph_string *s)
22536 {
22537 int k = -1;
22538
22539 if (s->right_overhang)
22540 {
22541 int x = 0, i;
22542 struct glyph *glyphs = s->row->glyphs[s->area];
22543 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22544 int end = s->row->used[s->area];
22545
22546 for (i = first; i < end && s->right_overhang > x; ++i)
22547 x += glyphs[i].pixel_width;
22548
22549 k = i;
22550 }
22551
22552 return k;
22553 }
22554
22555
22556 /* Return the index of the last glyph following glyph string S that
22557 overwrites S because of its left overhang. Value is negative
22558 if no such glyph is found. */
22559
22560 static int
22561 right_overwriting (struct glyph_string *s)
22562 {
22563 int i, k, x;
22564 int end = s->row->used[s->area];
22565 struct glyph *glyphs = s->row->glyphs[s->area];
22566 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22567
22568 k = -1;
22569 x = 0;
22570 for (i = first; i < end; ++i)
22571 {
22572 int left, right;
22573 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22574 if (x - left < 0)
22575 k = i;
22576 x += glyphs[i].pixel_width;
22577 }
22578
22579 return k;
22580 }
22581
22582
22583 /* Set background width of glyph string S. START is the index of the
22584 first glyph following S. LAST_X is the right-most x-position + 1
22585 in the drawing area. */
22586
22587 static inline void
22588 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22589 {
22590 /* If the face of this glyph string has to be drawn to the end of
22591 the drawing area, set S->extends_to_end_of_line_p. */
22592
22593 if (start == s->row->used[s->area]
22594 && s->area == TEXT_AREA
22595 && ((s->row->fill_line_p
22596 && (s->hl == DRAW_NORMAL_TEXT
22597 || s->hl == DRAW_IMAGE_RAISED
22598 || s->hl == DRAW_IMAGE_SUNKEN))
22599 || s->hl == DRAW_MOUSE_FACE))
22600 s->extends_to_end_of_line_p = 1;
22601
22602 /* If S extends its face to the end of the line, set its
22603 background_width to the distance to the right edge of the drawing
22604 area. */
22605 if (s->extends_to_end_of_line_p)
22606 s->background_width = last_x - s->x + 1;
22607 else
22608 s->background_width = s->width;
22609 }
22610
22611
22612 /* Compute overhangs and x-positions for glyph string S and its
22613 predecessors, or successors. X is the starting x-position for S.
22614 BACKWARD_P non-zero means process predecessors. */
22615
22616 static void
22617 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22618 {
22619 if (backward_p)
22620 {
22621 while (s)
22622 {
22623 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22624 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22625 x -= s->width;
22626 s->x = x;
22627 s = s->prev;
22628 }
22629 }
22630 else
22631 {
22632 while (s)
22633 {
22634 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22635 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22636 s->x = x;
22637 x += s->width;
22638 s = s->next;
22639 }
22640 }
22641 }
22642
22643
22644
22645 /* The following macros are only called from draw_glyphs below.
22646 They reference the following parameters of that function directly:
22647 `w', `row', `area', and `overlap_p'
22648 as well as the following local variables:
22649 `s', `f', and `hdc' (in W32) */
22650
22651 #ifdef HAVE_NTGUI
22652 /* On W32, silently add local `hdc' variable to argument list of
22653 init_glyph_string. */
22654 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22655 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22656 #else
22657 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22658 init_glyph_string (s, char2b, w, row, area, start, hl)
22659 #endif
22660
22661 /* Add a glyph string for a stretch glyph to the list of strings
22662 between HEAD and TAIL. START is the index of the stretch glyph in
22663 row area AREA of glyph row ROW. END is the index of the last glyph
22664 in that glyph row area. X is the current output position assigned
22665 to the new glyph string constructed. HL overrides that face of the
22666 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22667 is the right-most x-position of the drawing area. */
22668
22669 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22670 and below -- keep them on one line. */
22671 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22672 do \
22673 { \
22674 s = (struct glyph_string *) alloca (sizeof *s); \
22675 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22676 START = fill_stretch_glyph_string (s, START, END); \
22677 append_glyph_string (&HEAD, &TAIL, s); \
22678 s->x = (X); \
22679 } \
22680 while (0)
22681
22682
22683 /* Add a glyph string for an image glyph to the list of strings
22684 between HEAD and TAIL. START is the index of the image glyph in
22685 row area AREA of glyph row ROW. END is the index of the last glyph
22686 in that glyph row area. X is the current output position assigned
22687 to the new glyph string constructed. HL overrides that face of the
22688 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22689 is the right-most x-position of the drawing area. */
22690
22691 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22692 do \
22693 { \
22694 s = (struct glyph_string *) alloca (sizeof *s); \
22695 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22696 fill_image_glyph_string (s); \
22697 append_glyph_string (&HEAD, &TAIL, s); \
22698 ++START; \
22699 s->x = (X); \
22700 } \
22701 while (0)
22702
22703
22704 /* Add a glyph string for a sequence of character glyphs to the list
22705 of strings between HEAD and TAIL. START is the index of the first
22706 glyph in row area AREA of glyph row ROW that is part of the new
22707 glyph string. END is the index of the last glyph in that glyph row
22708 area. X is the current output position assigned to the new glyph
22709 string constructed. HL overrides that face of the glyph; e.g. it
22710 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22711 right-most x-position of the drawing area. */
22712
22713 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22714 do \
22715 { \
22716 int face_id; \
22717 XChar2b *char2b; \
22718 \
22719 face_id = (row)->glyphs[area][START].face_id; \
22720 \
22721 s = (struct glyph_string *) alloca (sizeof *s); \
22722 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22723 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22724 append_glyph_string (&HEAD, &TAIL, s); \
22725 s->x = (X); \
22726 START = fill_glyph_string (s, face_id, START, END, overlaps); \
22727 } \
22728 while (0)
22729
22730
22731 /* Add a glyph string for a composite sequence to the list of strings
22732 between HEAD and TAIL. START is the index of the first glyph in
22733 row area AREA of glyph row ROW that is part of the new glyph
22734 string. END is the index of the last glyph in that glyph row area.
22735 X is the current output position assigned to the new glyph string
22736 constructed. HL overrides that face of the glyph; e.g. it is
22737 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
22738 x-position of the drawing area. */
22739
22740 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22741 do { \
22742 int face_id = (row)->glyphs[area][START].face_id; \
22743 struct face *base_face = FACE_FROM_ID (f, face_id); \
22744 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
22745 struct composition *cmp = composition_table[cmp_id]; \
22746 XChar2b *char2b; \
22747 struct glyph_string *first_s IF_LINT (= NULL); \
22748 int n; \
22749 \
22750 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
22751 \
22752 /* Make glyph_strings for each glyph sequence that is drawable by \
22753 the same face, and append them to HEAD/TAIL. */ \
22754 for (n = 0; n < cmp->glyph_len;) \
22755 { \
22756 s = (struct glyph_string *) alloca (sizeof *s); \
22757 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22758 append_glyph_string (&(HEAD), &(TAIL), s); \
22759 s->cmp = cmp; \
22760 s->cmp_from = n; \
22761 s->x = (X); \
22762 if (n == 0) \
22763 first_s = s; \
22764 n = fill_composite_glyph_string (s, base_face, overlaps); \
22765 } \
22766 \
22767 ++START; \
22768 s = first_s; \
22769 } while (0)
22770
22771
22772 /* Add a glyph string for a glyph-string sequence to the list of strings
22773 between HEAD and TAIL. */
22774
22775 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22776 do { \
22777 int face_id; \
22778 XChar2b *char2b; \
22779 Lisp_Object gstring; \
22780 \
22781 face_id = (row)->glyphs[area][START].face_id; \
22782 gstring = (composition_gstring_from_id \
22783 ((row)->glyphs[area][START].u.cmp.id)); \
22784 s = (struct glyph_string *) alloca (sizeof *s); \
22785 char2b = (XChar2b *) alloca ((sizeof *char2b) \
22786 * LGSTRING_GLYPH_LEN (gstring)); \
22787 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22788 append_glyph_string (&(HEAD), &(TAIL), s); \
22789 s->x = (X); \
22790 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
22791 } while (0)
22792
22793
22794 /* Add a glyph string for a sequence of glyphless character's glyphs
22795 to the list of strings between HEAD and TAIL. The meanings of
22796 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
22797
22798 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22799 do \
22800 { \
22801 int face_id; \
22802 \
22803 face_id = (row)->glyphs[area][START].face_id; \
22804 \
22805 s = (struct glyph_string *) alloca (sizeof *s); \
22806 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22807 append_glyph_string (&HEAD, &TAIL, s); \
22808 s->x = (X); \
22809 START = fill_glyphless_glyph_string (s, face_id, START, END, \
22810 overlaps); \
22811 } \
22812 while (0)
22813
22814
22815 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
22816 of AREA of glyph row ROW on window W between indices START and END.
22817 HL overrides the face for drawing glyph strings, e.g. it is
22818 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
22819 x-positions of the drawing area.
22820
22821 This is an ugly monster macro construct because we must use alloca
22822 to allocate glyph strings (because draw_glyphs can be called
22823 asynchronously). */
22824
22825 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22826 do \
22827 { \
22828 HEAD = TAIL = NULL; \
22829 while (START < END) \
22830 { \
22831 struct glyph *first_glyph = (row)->glyphs[area] + START; \
22832 switch (first_glyph->type) \
22833 { \
22834 case CHAR_GLYPH: \
22835 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
22836 HL, X, LAST_X); \
22837 break; \
22838 \
22839 case COMPOSITE_GLYPH: \
22840 if (first_glyph->u.cmp.automatic) \
22841 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
22842 HL, X, LAST_X); \
22843 else \
22844 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
22845 HL, X, LAST_X); \
22846 break; \
22847 \
22848 case STRETCH_GLYPH: \
22849 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
22850 HL, X, LAST_X); \
22851 break; \
22852 \
22853 case IMAGE_GLYPH: \
22854 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
22855 HL, X, LAST_X); \
22856 break; \
22857 \
22858 case GLYPHLESS_GLYPH: \
22859 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
22860 HL, X, LAST_X); \
22861 break; \
22862 \
22863 default: \
22864 abort (); \
22865 } \
22866 \
22867 if (s) \
22868 { \
22869 set_glyph_string_background_width (s, START, LAST_X); \
22870 (X) += s->width; \
22871 } \
22872 } \
22873 } while (0)
22874
22875
22876 /* Draw glyphs between START and END in AREA of ROW on window W,
22877 starting at x-position X. X is relative to AREA in W. HL is a
22878 face-override with the following meaning:
22879
22880 DRAW_NORMAL_TEXT draw normally
22881 DRAW_CURSOR draw in cursor face
22882 DRAW_MOUSE_FACE draw in mouse face.
22883 DRAW_INVERSE_VIDEO draw in mode line face
22884 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
22885 DRAW_IMAGE_RAISED draw an image with a raised relief around it
22886
22887 If OVERLAPS is non-zero, draw only the foreground of characters and
22888 clip to the physical height of ROW. Non-zero value also defines
22889 the overlapping part to be drawn:
22890
22891 OVERLAPS_PRED overlap with preceding rows
22892 OVERLAPS_SUCC overlap with succeeding rows
22893 OVERLAPS_BOTH overlap with both preceding/succeeding rows
22894 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
22895
22896 Value is the x-position reached, relative to AREA of W. */
22897
22898 static int
22899 draw_glyphs (struct window *w, int x, struct glyph_row *row,
22900 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
22901 enum draw_glyphs_face hl, int overlaps)
22902 {
22903 struct glyph_string *head, *tail;
22904 struct glyph_string *s;
22905 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
22906 int i, j, x_reached, last_x, area_left = 0;
22907 struct frame *f = XFRAME (WINDOW_FRAME (w));
22908 DECLARE_HDC (hdc);
22909
22910 ALLOCATE_HDC (hdc, f);
22911
22912 /* Let's rather be paranoid than getting a SEGV. */
22913 end = min (end, row->used[area]);
22914 start = max (0, start);
22915 start = min (end, start);
22916
22917 /* Translate X to frame coordinates. Set last_x to the right
22918 end of the drawing area. */
22919 if (row->full_width_p)
22920 {
22921 /* X is relative to the left edge of W, without scroll bars
22922 or fringes. */
22923 area_left = WINDOW_LEFT_EDGE_X (w);
22924 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
22925 }
22926 else
22927 {
22928 area_left = window_box_left (w, area);
22929 last_x = area_left + window_box_width (w, area);
22930 }
22931 x += area_left;
22932
22933 /* Build a doubly-linked list of glyph_string structures between
22934 head and tail from what we have to draw. Note that the macro
22935 BUILD_GLYPH_STRINGS will modify its start parameter. That's
22936 the reason we use a separate variable `i'. */
22937 i = start;
22938 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
22939 if (tail)
22940 x_reached = tail->x + tail->background_width;
22941 else
22942 x_reached = x;
22943
22944 /* If there are any glyphs with lbearing < 0 or rbearing > width in
22945 the row, redraw some glyphs in front or following the glyph
22946 strings built above. */
22947 if (head && !overlaps && row->contains_overlapping_glyphs_p)
22948 {
22949 struct glyph_string *h, *t;
22950 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
22951 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
22952 int check_mouse_face = 0;
22953 int dummy_x = 0;
22954
22955 /* If mouse highlighting is on, we may need to draw adjacent
22956 glyphs using mouse-face highlighting. */
22957 if (area == TEXT_AREA && row->mouse_face_p)
22958 {
22959 struct glyph_row *mouse_beg_row, *mouse_end_row;
22960
22961 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
22962 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
22963
22964 if (row >= mouse_beg_row && row <= mouse_end_row)
22965 {
22966 check_mouse_face = 1;
22967 mouse_beg_col = (row == mouse_beg_row)
22968 ? hlinfo->mouse_face_beg_col : 0;
22969 mouse_end_col = (row == mouse_end_row)
22970 ? hlinfo->mouse_face_end_col
22971 : row->used[TEXT_AREA];
22972 }
22973 }
22974
22975 /* Compute overhangs for all glyph strings. */
22976 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
22977 for (s = head; s; s = s->next)
22978 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
22979
22980 /* Prepend glyph strings for glyphs in front of the first glyph
22981 string that are overwritten because of the first glyph
22982 string's left overhang. The background of all strings
22983 prepended must be drawn because the first glyph string
22984 draws over it. */
22985 i = left_overwritten (head);
22986 if (i >= 0)
22987 {
22988 enum draw_glyphs_face overlap_hl;
22989
22990 /* If this row contains mouse highlighting, attempt to draw
22991 the overlapped glyphs with the correct highlight. This
22992 code fails if the overlap encompasses more than one glyph
22993 and mouse-highlight spans only some of these glyphs.
22994 However, making it work perfectly involves a lot more
22995 code, and I don't know if the pathological case occurs in
22996 practice, so we'll stick to this for now. --- cyd */
22997 if (check_mouse_face
22998 && mouse_beg_col < start && mouse_end_col > i)
22999 overlap_hl = DRAW_MOUSE_FACE;
23000 else
23001 overlap_hl = DRAW_NORMAL_TEXT;
23002
23003 j = i;
23004 BUILD_GLYPH_STRINGS (j, start, h, t,
23005 overlap_hl, dummy_x, last_x);
23006 start = i;
23007 compute_overhangs_and_x (t, head->x, 1);
23008 prepend_glyph_string_lists (&head, &tail, h, t);
23009 clip_head = head;
23010 }
23011
23012 /* Prepend glyph strings for glyphs in front of the first glyph
23013 string that overwrite that glyph string because of their
23014 right overhang. For these strings, only the foreground must
23015 be drawn, because it draws over the glyph string at `head'.
23016 The background must not be drawn because this would overwrite
23017 right overhangs of preceding glyphs for which no glyph
23018 strings exist. */
23019 i = left_overwriting (head);
23020 if (i >= 0)
23021 {
23022 enum draw_glyphs_face overlap_hl;
23023
23024 if (check_mouse_face
23025 && mouse_beg_col < start && mouse_end_col > i)
23026 overlap_hl = DRAW_MOUSE_FACE;
23027 else
23028 overlap_hl = DRAW_NORMAL_TEXT;
23029
23030 clip_head = head;
23031 BUILD_GLYPH_STRINGS (i, start, h, t,
23032 overlap_hl, dummy_x, last_x);
23033 for (s = h; s; s = s->next)
23034 s->background_filled_p = 1;
23035 compute_overhangs_and_x (t, head->x, 1);
23036 prepend_glyph_string_lists (&head, &tail, h, t);
23037 }
23038
23039 /* Append glyphs strings for glyphs following the last glyph
23040 string tail that are overwritten by tail. The background of
23041 these strings has to be drawn because tail's foreground draws
23042 over it. */
23043 i = right_overwritten (tail);
23044 if (i >= 0)
23045 {
23046 enum draw_glyphs_face overlap_hl;
23047
23048 if (check_mouse_face
23049 && mouse_beg_col < i && mouse_end_col > end)
23050 overlap_hl = DRAW_MOUSE_FACE;
23051 else
23052 overlap_hl = DRAW_NORMAL_TEXT;
23053
23054 BUILD_GLYPH_STRINGS (end, i, h, t,
23055 overlap_hl, x, last_x);
23056 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23057 we don't have `end = i;' here. */
23058 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23059 append_glyph_string_lists (&head, &tail, h, t);
23060 clip_tail = tail;
23061 }
23062
23063 /* Append glyph strings for glyphs following the last glyph
23064 string tail that overwrite tail. The foreground of such
23065 glyphs has to be drawn because it writes into the background
23066 of tail. The background must not be drawn because it could
23067 paint over the foreground of following glyphs. */
23068 i = right_overwriting (tail);
23069 if (i >= 0)
23070 {
23071 enum draw_glyphs_face overlap_hl;
23072 if (check_mouse_face
23073 && mouse_beg_col < i && mouse_end_col > end)
23074 overlap_hl = DRAW_MOUSE_FACE;
23075 else
23076 overlap_hl = DRAW_NORMAL_TEXT;
23077
23078 clip_tail = tail;
23079 i++; /* We must include the Ith glyph. */
23080 BUILD_GLYPH_STRINGS (end, i, h, t,
23081 overlap_hl, x, last_x);
23082 for (s = h; s; s = s->next)
23083 s->background_filled_p = 1;
23084 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23085 append_glyph_string_lists (&head, &tail, h, t);
23086 }
23087 if (clip_head || clip_tail)
23088 for (s = head; s; s = s->next)
23089 {
23090 s->clip_head = clip_head;
23091 s->clip_tail = clip_tail;
23092 }
23093 }
23094
23095 /* Draw all strings. */
23096 for (s = head; s; s = s->next)
23097 FRAME_RIF (f)->draw_glyph_string (s);
23098
23099 #ifndef HAVE_NS
23100 /* When focus a sole frame and move horizontally, this sets on_p to 0
23101 causing a failure to erase prev cursor position. */
23102 if (area == TEXT_AREA
23103 && !row->full_width_p
23104 /* When drawing overlapping rows, only the glyph strings'
23105 foreground is drawn, which doesn't erase a cursor
23106 completely. */
23107 && !overlaps)
23108 {
23109 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23110 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23111 : (tail ? tail->x + tail->background_width : x));
23112 x0 -= area_left;
23113 x1 -= area_left;
23114
23115 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23116 row->y, MATRIX_ROW_BOTTOM_Y (row));
23117 }
23118 #endif
23119
23120 /* Value is the x-position up to which drawn, relative to AREA of W.
23121 This doesn't include parts drawn because of overhangs. */
23122 if (row->full_width_p)
23123 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23124 else
23125 x_reached -= area_left;
23126
23127 RELEASE_HDC (hdc, f);
23128
23129 return x_reached;
23130 }
23131
23132 /* Expand row matrix if too narrow. Don't expand if area
23133 is not present. */
23134
23135 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23136 { \
23137 if (!fonts_changed_p \
23138 && (it->glyph_row->glyphs[area] \
23139 < it->glyph_row->glyphs[area + 1])) \
23140 { \
23141 it->w->ncols_scale_factor++; \
23142 fonts_changed_p = 1; \
23143 } \
23144 }
23145
23146 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23147 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23148
23149 static inline void
23150 append_glyph (struct it *it)
23151 {
23152 struct glyph *glyph;
23153 enum glyph_row_area area = it->area;
23154
23155 xassert (it->glyph_row);
23156 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23157
23158 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23159 if (glyph < it->glyph_row->glyphs[area + 1])
23160 {
23161 /* If the glyph row is reversed, we need to prepend the glyph
23162 rather than append it. */
23163 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23164 {
23165 struct glyph *g;
23166
23167 /* Make room for the additional glyph. */
23168 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23169 g[1] = *g;
23170 glyph = it->glyph_row->glyphs[area];
23171 }
23172 glyph->charpos = CHARPOS (it->position);
23173 glyph->object = it->object;
23174 if (it->pixel_width > 0)
23175 {
23176 glyph->pixel_width = it->pixel_width;
23177 glyph->padding_p = 0;
23178 }
23179 else
23180 {
23181 /* Assure at least 1-pixel width. Otherwise, cursor can't
23182 be displayed correctly. */
23183 glyph->pixel_width = 1;
23184 glyph->padding_p = 1;
23185 }
23186 glyph->ascent = it->ascent;
23187 glyph->descent = it->descent;
23188 glyph->voffset = it->voffset;
23189 glyph->type = CHAR_GLYPH;
23190 glyph->avoid_cursor_p = it->avoid_cursor_p;
23191 glyph->multibyte_p = it->multibyte_p;
23192 glyph->left_box_line_p = it->start_of_box_run_p;
23193 glyph->right_box_line_p = it->end_of_box_run_p;
23194 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23195 || it->phys_descent > it->descent);
23196 glyph->glyph_not_available_p = it->glyph_not_available_p;
23197 glyph->face_id = it->face_id;
23198 glyph->u.ch = it->char_to_display;
23199 glyph->slice.img = null_glyph_slice;
23200 glyph->font_type = FONT_TYPE_UNKNOWN;
23201 if (it->bidi_p)
23202 {
23203 glyph->resolved_level = it->bidi_it.resolved_level;
23204 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23205 abort ();
23206 glyph->bidi_type = it->bidi_it.type;
23207 }
23208 else
23209 {
23210 glyph->resolved_level = 0;
23211 glyph->bidi_type = UNKNOWN_BT;
23212 }
23213 ++it->glyph_row->used[area];
23214 }
23215 else
23216 IT_EXPAND_MATRIX_WIDTH (it, area);
23217 }
23218
23219 /* Store one glyph for the composition IT->cmp_it.id in
23220 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23221 non-null. */
23222
23223 static inline void
23224 append_composite_glyph (struct it *it)
23225 {
23226 struct glyph *glyph;
23227 enum glyph_row_area area = it->area;
23228
23229 xassert (it->glyph_row);
23230
23231 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23232 if (glyph < it->glyph_row->glyphs[area + 1])
23233 {
23234 /* If the glyph row is reversed, we need to prepend the glyph
23235 rather than append it. */
23236 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23237 {
23238 struct glyph *g;
23239
23240 /* Make room for the new glyph. */
23241 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23242 g[1] = *g;
23243 glyph = it->glyph_row->glyphs[it->area];
23244 }
23245 glyph->charpos = it->cmp_it.charpos;
23246 glyph->object = it->object;
23247 glyph->pixel_width = it->pixel_width;
23248 glyph->ascent = it->ascent;
23249 glyph->descent = it->descent;
23250 glyph->voffset = it->voffset;
23251 glyph->type = COMPOSITE_GLYPH;
23252 if (it->cmp_it.ch < 0)
23253 {
23254 glyph->u.cmp.automatic = 0;
23255 glyph->u.cmp.id = it->cmp_it.id;
23256 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23257 }
23258 else
23259 {
23260 glyph->u.cmp.automatic = 1;
23261 glyph->u.cmp.id = it->cmp_it.id;
23262 glyph->slice.cmp.from = it->cmp_it.from;
23263 glyph->slice.cmp.to = it->cmp_it.to - 1;
23264 }
23265 glyph->avoid_cursor_p = it->avoid_cursor_p;
23266 glyph->multibyte_p = it->multibyte_p;
23267 glyph->left_box_line_p = it->start_of_box_run_p;
23268 glyph->right_box_line_p = it->end_of_box_run_p;
23269 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23270 || it->phys_descent > it->descent);
23271 glyph->padding_p = 0;
23272 glyph->glyph_not_available_p = 0;
23273 glyph->face_id = it->face_id;
23274 glyph->font_type = FONT_TYPE_UNKNOWN;
23275 if (it->bidi_p)
23276 {
23277 glyph->resolved_level = it->bidi_it.resolved_level;
23278 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23279 abort ();
23280 glyph->bidi_type = it->bidi_it.type;
23281 }
23282 ++it->glyph_row->used[area];
23283 }
23284 else
23285 IT_EXPAND_MATRIX_WIDTH (it, area);
23286 }
23287
23288
23289 /* Change IT->ascent and IT->height according to the setting of
23290 IT->voffset. */
23291
23292 static inline void
23293 take_vertical_position_into_account (struct it *it)
23294 {
23295 if (it->voffset)
23296 {
23297 if (it->voffset < 0)
23298 /* Increase the ascent so that we can display the text higher
23299 in the line. */
23300 it->ascent -= it->voffset;
23301 else
23302 /* Increase the descent so that we can display the text lower
23303 in the line. */
23304 it->descent += it->voffset;
23305 }
23306 }
23307
23308
23309 /* Produce glyphs/get display metrics for the image IT is loaded with.
23310 See the description of struct display_iterator in dispextern.h for
23311 an overview of struct display_iterator. */
23312
23313 static void
23314 produce_image_glyph (struct it *it)
23315 {
23316 struct image *img;
23317 struct face *face;
23318 int glyph_ascent, crop;
23319 struct glyph_slice slice;
23320
23321 xassert (it->what == IT_IMAGE);
23322
23323 face = FACE_FROM_ID (it->f, it->face_id);
23324 xassert (face);
23325 /* Make sure X resources of the face is loaded. */
23326 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23327
23328 if (it->image_id < 0)
23329 {
23330 /* Fringe bitmap. */
23331 it->ascent = it->phys_ascent = 0;
23332 it->descent = it->phys_descent = 0;
23333 it->pixel_width = 0;
23334 it->nglyphs = 0;
23335 return;
23336 }
23337
23338 img = IMAGE_FROM_ID (it->f, it->image_id);
23339 xassert (img);
23340 /* Make sure X resources of the image is loaded. */
23341 prepare_image_for_display (it->f, img);
23342
23343 slice.x = slice.y = 0;
23344 slice.width = img->width;
23345 slice.height = img->height;
23346
23347 if (INTEGERP (it->slice.x))
23348 slice.x = XINT (it->slice.x);
23349 else if (FLOATP (it->slice.x))
23350 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23351
23352 if (INTEGERP (it->slice.y))
23353 slice.y = XINT (it->slice.y);
23354 else if (FLOATP (it->slice.y))
23355 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23356
23357 if (INTEGERP (it->slice.width))
23358 slice.width = XINT (it->slice.width);
23359 else if (FLOATP (it->slice.width))
23360 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23361
23362 if (INTEGERP (it->slice.height))
23363 slice.height = XINT (it->slice.height);
23364 else if (FLOATP (it->slice.height))
23365 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23366
23367 if (slice.x >= img->width)
23368 slice.x = img->width;
23369 if (slice.y >= img->height)
23370 slice.y = img->height;
23371 if (slice.x + slice.width >= img->width)
23372 slice.width = img->width - slice.x;
23373 if (slice.y + slice.height > img->height)
23374 slice.height = img->height - slice.y;
23375
23376 if (slice.width == 0 || slice.height == 0)
23377 return;
23378
23379 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23380
23381 it->descent = slice.height - glyph_ascent;
23382 if (slice.y == 0)
23383 it->descent += img->vmargin;
23384 if (slice.y + slice.height == img->height)
23385 it->descent += img->vmargin;
23386 it->phys_descent = it->descent;
23387
23388 it->pixel_width = slice.width;
23389 if (slice.x == 0)
23390 it->pixel_width += img->hmargin;
23391 if (slice.x + slice.width == img->width)
23392 it->pixel_width += img->hmargin;
23393
23394 /* It's quite possible for images to have an ascent greater than
23395 their height, so don't get confused in that case. */
23396 if (it->descent < 0)
23397 it->descent = 0;
23398
23399 it->nglyphs = 1;
23400
23401 if (face->box != FACE_NO_BOX)
23402 {
23403 if (face->box_line_width > 0)
23404 {
23405 if (slice.y == 0)
23406 it->ascent += face->box_line_width;
23407 if (slice.y + slice.height == img->height)
23408 it->descent += face->box_line_width;
23409 }
23410
23411 if (it->start_of_box_run_p && slice.x == 0)
23412 it->pixel_width += eabs (face->box_line_width);
23413 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23414 it->pixel_width += eabs (face->box_line_width);
23415 }
23416
23417 take_vertical_position_into_account (it);
23418
23419 /* Automatically crop wide image glyphs at right edge so we can
23420 draw the cursor on same display row. */
23421 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23422 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23423 {
23424 it->pixel_width -= crop;
23425 slice.width -= crop;
23426 }
23427
23428 if (it->glyph_row)
23429 {
23430 struct glyph *glyph;
23431 enum glyph_row_area area = it->area;
23432
23433 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23434 if (glyph < it->glyph_row->glyphs[area + 1])
23435 {
23436 glyph->charpos = CHARPOS (it->position);
23437 glyph->object = it->object;
23438 glyph->pixel_width = it->pixel_width;
23439 glyph->ascent = glyph_ascent;
23440 glyph->descent = it->descent;
23441 glyph->voffset = it->voffset;
23442 glyph->type = IMAGE_GLYPH;
23443 glyph->avoid_cursor_p = it->avoid_cursor_p;
23444 glyph->multibyte_p = it->multibyte_p;
23445 glyph->left_box_line_p = it->start_of_box_run_p;
23446 glyph->right_box_line_p = it->end_of_box_run_p;
23447 glyph->overlaps_vertically_p = 0;
23448 glyph->padding_p = 0;
23449 glyph->glyph_not_available_p = 0;
23450 glyph->face_id = it->face_id;
23451 glyph->u.img_id = img->id;
23452 glyph->slice.img = slice;
23453 glyph->font_type = FONT_TYPE_UNKNOWN;
23454 if (it->bidi_p)
23455 {
23456 glyph->resolved_level = it->bidi_it.resolved_level;
23457 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23458 abort ();
23459 glyph->bidi_type = it->bidi_it.type;
23460 }
23461 ++it->glyph_row->used[area];
23462 }
23463 else
23464 IT_EXPAND_MATRIX_WIDTH (it, area);
23465 }
23466 }
23467
23468
23469 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23470 of the glyph, WIDTH and HEIGHT are the width and height of the
23471 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23472
23473 static void
23474 append_stretch_glyph (struct it *it, Lisp_Object object,
23475 int width, int height, int ascent)
23476 {
23477 struct glyph *glyph;
23478 enum glyph_row_area area = it->area;
23479
23480 xassert (ascent >= 0 && ascent <= height);
23481
23482 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23483 if (glyph < it->glyph_row->glyphs[area + 1])
23484 {
23485 /* If the glyph row is reversed, we need to prepend the glyph
23486 rather than append it. */
23487 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23488 {
23489 struct glyph *g;
23490
23491 /* Make room for the additional glyph. */
23492 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23493 g[1] = *g;
23494 glyph = it->glyph_row->glyphs[area];
23495 }
23496 glyph->charpos = CHARPOS (it->position);
23497 glyph->object = object;
23498 glyph->pixel_width = width;
23499 glyph->ascent = ascent;
23500 glyph->descent = height - ascent;
23501 glyph->voffset = it->voffset;
23502 glyph->type = STRETCH_GLYPH;
23503 glyph->avoid_cursor_p = it->avoid_cursor_p;
23504 glyph->multibyte_p = it->multibyte_p;
23505 glyph->left_box_line_p = it->start_of_box_run_p;
23506 glyph->right_box_line_p = it->end_of_box_run_p;
23507 glyph->overlaps_vertically_p = 0;
23508 glyph->padding_p = 0;
23509 glyph->glyph_not_available_p = 0;
23510 glyph->face_id = it->face_id;
23511 glyph->u.stretch.ascent = ascent;
23512 glyph->u.stretch.height = height;
23513 glyph->slice.img = null_glyph_slice;
23514 glyph->font_type = FONT_TYPE_UNKNOWN;
23515 if (it->bidi_p)
23516 {
23517 glyph->resolved_level = it->bidi_it.resolved_level;
23518 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23519 abort ();
23520 glyph->bidi_type = it->bidi_it.type;
23521 }
23522 else
23523 {
23524 glyph->resolved_level = 0;
23525 glyph->bidi_type = UNKNOWN_BT;
23526 }
23527 ++it->glyph_row->used[area];
23528 }
23529 else
23530 IT_EXPAND_MATRIX_WIDTH (it, area);
23531 }
23532
23533 #endif /* HAVE_WINDOW_SYSTEM */
23534
23535 /* Produce a stretch glyph for iterator IT. IT->object is the value
23536 of the glyph property displayed. The value must be a list
23537 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23538 being recognized:
23539
23540 1. `:width WIDTH' specifies that the space should be WIDTH *
23541 canonical char width wide. WIDTH may be an integer or floating
23542 point number.
23543
23544 2. `:relative-width FACTOR' specifies that the width of the stretch
23545 should be computed from the width of the first character having the
23546 `glyph' property, and should be FACTOR times that width.
23547
23548 3. `:align-to HPOS' specifies that the space should be wide enough
23549 to reach HPOS, a value in canonical character units.
23550
23551 Exactly one of the above pairs must be present.
23552
23553 4. `:height HEIGHT' specifies that the height of the stretch produced
23554 should be HEIGHT, measured in canonical character units.
23555
23556 5. `:relative-height FACTOR' specifies that the height of the
23557 stretch should be FACTOR times the height of the characters having
23558 the glyph property.
23559
23560 Either none or exactly one of 4 or 5 must be present.
23561
23562 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23563 of the stretch should be used for the ascent of the stretch.
23564 ASCENT must be in the range 0 <= ASCENT <= 100. */
23565
23566 void
23567 produce_stretch_glyph (struct it *it)
23568 {
23569 /* (space :width WIDTH :height HEIGHT ...) */
23570 Lisp_Object prop, plist;
23571 int width = 0, height = 0, align_to = -1;
23572 int zero_width_ok_p = 0;
23573 int ascent = 0;
23574 double tem;
23575 struct face *face = NULL;
23576 struct font *font = NULL;
23577
23578 #ifdef HAVE_WINDOW_SYSTEM
23579 int zero_height_ok_p = 0;
23580
23581 if (FRAME_WINDOW_P (it->f))
23582 {
23583 face = FACE_FROM_ID (it->f, it->face_id);
23584 font = face->font ? face->font : FRAME_FONT (it->f);
23585 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23586 }
23587 #endif
23588
23589 /* List should start with `space'. */
23590 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23591 plist = XCDR (it->object);
23592
23593 /* Compute the width of the stretch. */
23594 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23595 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23596 {
23597 /* Absolute width `:width WIDTH' specified and valid. */
23598 zero_width_ok_p = 1;
23599 width = (int)tem;
23600 }
23601 #ifdef HAVE_WINDOW_SYSTEM
23602 else if (FRAME_WINDOW_P (it->f)
23603 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23604 {
23605 /* Relative width `:relative-width FACTOR' specified and valid.
23606 Compute the width of the characters having the `glyph'
23607 property. */
23608 struct it it2;
23609 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23610
23611 it2 = *it;
23612 if (it->multibyte_p)
23613 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23614 else
23615 {
23616 it2.c = it2.char_to_display = *p, it2.len = 1;
23617 if (! ASCII_CHAR_P (it2.c))
23618 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23619 }
23620
23621 it2.glyph_row = NULL;
23622 it2.what = IT_CHARACTER;
23623 x_produce_glyphs (&it2);
23624 width = NUMVAL (prop) * it2.pixel_width;
23625 }
23626 #endif /* HAVE_WINDOW_SYSTEM */
23627 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23628 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23629 {
23630 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23631 align_to = (align_to < 0
23632 ? 0
23633 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23634 else if (align_to < 0)
23635 align_to = window_box_left_offset (it->w, TEXT_AREA);
23636 width = max (0, (int)tem + align_to - it->current_x);
23637 zero_width_ok_p = 1;
23638 }
23639 else
23640 /* Nothing specified -> width defaults to canonical char width. */
23641 width = FRAME_COLUMN_WIDTH (it->f);
23642
23643 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23644 width = 1;
23645
23646 #ifdef HAVE_WINDOW_SYSTEM
23647 /* Compute height. */
23648 if (FRAME_WINDOW_P (it->f))
23649 {
23650 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23651 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23652 {
23653 height = (int)tem;
23654 zero_height_ok_p = 1;
23655 }
23656 else if (prop = Fplist_get (plist, QCrelative_height),
23657 NUMVAL (prop) > 0)
23658 height = FONT_HEIGHT (font) * NUMVAL (prop);
23659 else
23660 height = FONT_HEIGHT (font);
23661
23662 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23663 height = 1;
23664
23665 /* Compute percentage of height used for ascent. If
23666 `:ascent ASCENT' is present and valid, use that. Otherwise,
23667 derive the ascent from the font in use. */
23668 if (prop = Fplist_get (plist, QCascent),
23669 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23670 ascent = height * NUMVAL (prop) / 100.0;
23671 else if (!NILP (prop)
23672 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23673 ascent = min (max (0, (int)tem), height);
23674 else
23675 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23676 }
23677 else
23678 #endif /* HAVE_WINDOW_SYSTEM */
23679 height = 1;
23680
23681 if (width > 0 && it->line_wrap != TRUNCATE
23682 && it->current_x + width > it->last_visible_x)
23683 {
23684 width = it->last_visible_x - it->current_x;
23685 #ifdef HAVE_WINDOW_SYSTEM
23686 /* Subtract one more pixel from the stretch width, but only on
23687 GUI frames, since on a TTY each glyph is one "pixel" wide. */
23688 width -= FRAME_WINDOW_P (it->f);
23689 #endif
23690 }
23691
23692 if (width > 0 && height > 0 && it->glyph_row)
23693 {
23694 Lisp_Object o_object = it->object;
23695 Lisp_Object object = it->stack[it->sp - 1].string;
23696 int n = width;
23697
23698 if (!STRINGP (object))
23699 object = it->w->buffer;
23700 #ifdef HAVE_WINDOW_SYSTEM
23701 if (FRAME_WINDOW_P (it->f))
23702 append_stretch_glyph (it, object, width, height, ascent);
23703 else
23704 #endif
23705 {
23706 it->object = object;
23707 it->char_to_display = ' ';
23708 it->pixel_width = it->len = 1;
23709 while (n--)
23710 tty_append_glyph (it);
23711 it->object = o_object;
23712 }
23713 }
23714
23715 it->pixel_width = width;
23716 #ifdef HAVE_WINDOW_SYSTEM
23717 if (FRAME_WINDOW_P (it->f))
23718 {
23719 it->ascent = it->phys_ascent = ascent;
23720 it->descent = it->phys_descent = height - it->ascent;
23721 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23722 take_vertical_position_into_account (it);
23723 }
23724 else
23725 #endif
23726 it->nglyphs = width;
23727 }
23728
23729 #ifdef HAVE_WINDOW_SYSTEM
23730
23731 /* Calculate line-height and line-spacing properties.
23732 An integer value specifies explicit pixel value.
23733 A float value specifies relative value to current face height.
23734 A cons (float . face-name) specifies relative value to
23735 height of specified face font.
23736
23737 Returns height in pixels, or nil. */
23738
23739
23740 static Lisp_Object
23741 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
23742 int boff, int override)
23743 {
23744 Lisp_Object face_name = Qnil;
23745 int ascent, descent, height;
23746
23747 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
23748 return val;
23749
23750 if (CONSP (val))
23751 {
23752 face_name = XCAR (val);
23753 val = XCDR (val);
23754 if (!NUMBERP (val))
23755 val = make_number (1);
23756 if (NILP (face_name))
23757 {
23758 height = it->ascent + it->descent;
23759 goto scale;
23760 }
23761 }
23762
23763 if (NILP (face_name))
23764 {
23765 font = FRAME_FONT (it->f);
23766 boff = FRAME_BASELINE_OFFSET (it->f);
23767 }
23768 else if (EQ (face_name, Qt))
23769 {
23770 override = 0;
23771 }
23772 else
23773 {
23774 int face_id;
23775 struct face *face;
23776
23777 face_id = lookup_named_face (it->f, face_name, 0);
23778 if (face_id < 0)
23779 return make_number (-1);
23780
23781 face = FACE_FROM_ID (it->f, face_id);
23782 font = face->font;
23783 if (font == NULL)
23784 return make_number (-1);
23785 boff = font->baseline_offset;
23786 if (font->vertical_centering)
23787 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
23788 }
23789
23790 ascent = FONT_BASE (font) + boff;
23791 descent = FONT_DESCENT (font) - boff;
23792
23793 if (override)
23794 {
23795 it->override_ascent = ascent;
23796 it->override_descent = descent;
23797 it->override_boff = boff;
23798 }
23799
23800 height = ascent + descent;
23801
23802 scale:
23803 if (FLOATP (val))
23804 height = (int)(XFLOAT_DATA (val) * height);
23805 else if (INTEGERP (val))
23806 height *= XINT (val);
23807
23808 return make_number (height);
23809 }
23810
23811
23812 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
23813 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
23814 and only if this is for a character for which no font was found.
23815
23816 If the display method (it->glyphless_method) is
23817 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
23818 length of the acronym or the hexadecimal string, UPPER_XOFF and
23819 UPPER_YOFF are pixel offsets for the upper part of the string,
23820 LOWER_XOFF and LOWER_YOFF are for the lower part.
23821
23822 For the other display methods, LEN through LOWER_YOFF are zero. */
23823
23824 static void
23825 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
23826 short upper_xoff, short upper_yoff,
23827 short lower_xoff, short lower_yoff)
23828 {
23829 struct glyph *glyph;
23830 enum glyph_row_area area = it->area;
23831
23832 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23833 if (glyph < it->glyph_row->glyphs[area + 1])
23834 {
23835 /* If the glyph row is reversed, we need to prepend the glyph
23836 rather than append it. */
23837 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23838 {
23839 struct glyph *g;
23840
23841 /* Make room for the additional glyph. */
23842 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23843 g[1] = *g;
23844 glyph = it->glyph_row->glyphs[area];
23845 }
23846 glyph->charpos = CHARPOS (it->position);
23847 glyph->object = it->object;
23848 glyph->pixel_width = it->pixel_width;
23849 glyph->ascent = it->ascent;
23850 glyph->descent = it->descent;
23851 glyph->voffset = it->voffset;
23852 glyph->type = GLYPHLESS_GLYPH;
23853 glyph->u.glyphless.method = it->glyphless_method;
23854 glyph->u.glyphless.for_no_font = for_no_font;
23855 glyph->u.glyphless.len = len;
23856 glyph->u.glyphless.ch = it->c;
23857 glyph->slice.glyphless.upper_xoff = upper_xoff;
23858 glyph->slice.glyphless.upper_yoff = upper_yoff;
23859 glyph->slice.glyphless.lower_xoff = lower_xoff;
23860 glyph->slice.glyphless.lower_yoff = lower_yoff;
23861 glyph->avoid_cursor_p = it->avoid_cursor_p;
23862 glyph->multibyte_p = it->multibyte_p;
23863 glyph->left_box_line_p = it->start_of_box_run_p;
23864 glyph->right_box_line_p = it->end_of_box_run_p;
23865 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23866 || it->phys_descent > it->descent);
23867 glyph->padding_p = 0;
23868 glyph->glyph_not_available_p = 0;
23869 glyph->face_id = face_id;
23870 glyph->font_type = FONT_TYPE_UNKNOWN;
23871 if (it->bidi_p)
23872 {
23873 glyph->resolved_level = it->bidi_it.resolved_level;
23874 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23875 abort ();
23876 glyph->bidi_type = it->bidi_it.type;
23877 }
23878 ++it->glyph_row->used[area];
23879 }
23880 else
23881 IT_EXPAND_MATRIX_WIDTH (it, area);
23882 }
23883
23884
23885 /* Produce a glyph for a glyphless character for iterator IT.
23886 IT->glyphless_method specifies which method to use for displaying
23887 the character. See the description of enum
23888 glyphless_display_method in dispextern.h for the detail.
23889
23890 FOR_NO_FONT is nonzero if and only if this is for a character for
23891 which no font was found. ACRONYM, if non-nil, is an acronym string
23892 for the character. */
23893
23894 static void
23895 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
23896 {
23897 int face_id;
23898 struct face *face;
23899 struct font *font;
23900 int base_width, base_height, width, height;
23901 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
23902 int len;
23903
23904 /* Get the metrics of the base font. We always refer to the current
23905 ASCII face. */
23906 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
23907 font = face->font ? face->font : FRAME_FONT (it->f);
23908 it->ascent = FONT_BASE (font) + font->baseline_offset;
23909 it->descent = FONT_DESCENT (font) - font->baseline_offset;
23910 base_height = it->ascent + it->descent;
23911 base_width = font->average_width;
23912
23913 /* Get a face ID for the glyph by utilizing a cache (the same way as
23914 done for `escape-glyph' in get_next_display_element). */
23915 if (it->f == last_glyphless_glyph_frame
23916 && it->face_id == last_glyphless_glyph_face_id)
23917 {
23918 face_id = last_glyphless_glyph_merged_face_id;
23919 }
23920 else
23921 {
23922 /* Merge the `glyphless-char' face into the current face. */
23923 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
23924 last_glyphless_glyph_frame = it->f;
23925 last_glyphless_glyph_face_id = it->face_id;
23926 last_glyphless_glyph_merged_face_id = face_id;
23927 }
23928
23929 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
23930 {
23931 it->pixel_width = THIN_SPACE_WIDTH;
23932 len = 0;
23933 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23934 }
23935 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
23936 {
23937 width = CHAR_WIDTH (it->c);
23938 if (width == 0)
23939 width = 1;
23940 else if (width > 4)
23941 width = 4;
23942 it->pixel_width = base_width * width;
23943 len = 0;
23944 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
23945 }
23946 else
23947 {
23948 char buf[7];
23949 const char *str;
23950 unsigned int code[6];
23951 int upper_len;
23952 int ascent, descent;
23953 struct font_metrics metrics_upper, metrics_lower;
23954
23955 face = FACE_FROM_ID (it->f, face_id);
23956 font = face->font ? face->font : FRAME_FONT (it->f);
23957 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23958
23959 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
23960 {
23961 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
23962 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
23963 if (CONSP (acronym))
23964 acronym = XCAR (acronym);
23965 str = STRINGP (acronym) ? SSDATA (acronym) : "";
23966 }
23967 else
23968 {
23969 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
23970 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
23971 str = buf;
23972 }
23973 for (len = 0; str[len] && ASCII_BYTE_P (str[len]); len++)
23974 code[len] = font->driver->encode_char (font, str[len]);
23975 upper_len = (len + 1) / 2;
23976 font->driver->text_extents (font, code, upper_len,
23977 &metrics_upper);
23978 font->driver->text_extents (font, code + upper_len, len - upper_len,
23979 &metrics_lower);
23980
23981
23982
23983 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
23984 width = max (metrics_upper.width, metrics_lower.width) + 4;
23985 upper_xoff = upper_yoff = 2; /* the typical case */
23986 if (base_width >= width)
23987 {
23988 /* Align the upper to the left, the lower to the right. */
23989 it->pixel_width = base_width;
23990 lower_xoff = base_width - 2 - metrics_lower.width;
23991 }
23992 else
23993 {
23994 /* Center the shorter one. */
23995 it->pixel_width = width;
23996 if (metrics_upper.width >= metrics_lower.width)
23997 lower_xoff = (width - metrics_lower.width) / 2;
23998 else
23999 {
24000 /* FIXME: This code doesn't look right. It formerly was
24001 missing the "lower_xoff = 0;", which couldn't have
24002 been right since it left lower_xoff uninitialized. */
24003 lower_xoff = 0;
24004 upper_xoff = (width - metrics_upper.width) / 2;
24005 }
24006 }
24007
24008 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24009 top, bottom, and between upper and lower strings. */
24010 height = (metrics_upper.ascent + metrics_upper.descent
24011 + metrics_lower.ascent + metrics_lower.descent) + 5;
24012 /* Center vertically.
24013 H:base_height, D:base_descent
24014 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24015
24016 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24017 descent = D - H/2 + h/2;
24018 lower_yoff = descent - 2 - ld;
24019 upper_yoff = lower_yoff - la - 1 - ud; */
24020 ascent = - (it->descent - (base_height + height + 1) / 2);
24021 descent = it->descent - (base_height - height) / 2;
24022 lower_yoff = descent - 2 - metrics_lower.descent;
24023 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24024 - metrics_upper.descent);
24025 /* Don't make the height shorter than the base height. */
24026 if (height > base_height)
24027 {
24028 it->ascent = ascent;
24029 it->descent = descent;
24030 }
24031 }
24032
24033 it->phys_ascent = it->ascent;
24034 it->phys_descent = it->descent;
24035 if (it->glyph_row)
24036 append_glyphless_glyph (it, face_id, for_no_font, len,
24037 upper_xoff, upper_yoff,
24038 lower_xoff, lower_yoff);
24039 it->nglyphs = 1;
24040 take_vertical_position_into_account (it);
24041 }
24042
24043
24044 /* RIF:
24045 Produce glyphs/get display metrics for the display element IT is
24046 loaded with. See the description of struct it in dispextern.h
24047 for an overview of struct it. */
24048
24049 void
24050 x_produce_glyphs (struct it *it)
24051 {
24052 int extra_line_spacing = it->extra_line_spacing;
24053
24054 it->glyph_not_available_p = 0;
24055
24056 if (it->what == IT_CHARACTER)
24057 {
24058 XChar2b char2b;
24059 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24060 struct font *font = face->font;
24061 struct font_metrics *pcm = NULL;
24062 int boff; /* baseline offset */
24063
24064 if (font == NULL)
24065 {
24066 /* When no suitable font is found, display this character by
24067 the method specified in the first extra slot of
24068 Vglyphless_char_display. */
24069 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24070
24071 xassert (it->what == IT_GLYPHLESS);
24072 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24073 goto done;
24074 }
24075
24076 boff = font->baseline_offset;
24077 if (font->vertical_centering)
24078 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24079
24080 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24081 {
24082 int stretched_p;
24083
24084 it->nglyphs = 1;
24085
24086 if (it->override_ascent >= 0)
24087 {
24088 it->ascent = it->override_ascent;
24089 it->descent = it->override_descent;
24090 boff = it->override_boff;
24091 }
24092 else
24093 {
24094 it->ascent = FONT_BASE (font) + boff;
24095 it->descent = FONT_DESCENT (font) - boff;
24096 }
24097
24098 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24099 {
24100 pcm = get_per_char_metric (font, &char2b);
24101 if (pcm->width == 0
24102 && pcm->rbearing == 0 && pcm->lbearing == 0)
24103 pcm = NULL;
24104 }
24105
24106 if (pcm)
24107 {
24108 it->phys_ascent = pcm->ascent + boff;
24109 it->phys_descent = pcm->descent - boff;
24110 it->pixel_width = pcm->width;
24111 }
24112 else
24113 {
24114 it->glyph_not_available_p = 1;
24115 it->phys_ascent = it->ascent;
24116 it->phys_descent = it->descent;
24117 it->pixel_width = font->space_width;
24118 }
24119
24120 if (it->constrain_row_ascent_descent_p)
24121 {
24122 if (it->descent > it->max_descent)
24123 {
24124 it->ascent += it->descent - it->max_descent;
24125 it->descent = it->max_descent;
24126 }
24127 if (it->ascent > it->max_ascent)
24128 {
24129 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24130 it->ascent = it->max_ascent;
24131 }
24132 it->phys_ascent = min (it->phys_ascent, it->ascent);
24133 it->phys_descent = min (it->phys_descent, it->descent);
24134 extra_line_spacing = 0;
24135 }
24136
24137 /* If this is a space inside a region of text with
24138 `space-width' property, change its width. */
24139 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24140 if (stretched_p)
24141 it->pixel_width *= XFLOATINT (it->space_width);
24142
24143 /* If face has a box, add the box thickness to the character
24144 height. If character has a box line to the left and/or
24145 right, add the box line width to the character's width. */
24146 if (face->box != FACE_NO_BOX)
24147 {
24148 int thick = face->box_line_width;
24149
24150 if (thick > 0)
24151 {
24152 it->ascent += thick;
24153 it->descent += thick;
24154 }
24155 else
24156 thick = -thick;
24157
24158 if (it->start_of_box_run_p)
24159 it->pixel_width += thick;
24160 if (it->end_of_box_run_p)
24161 it->pixel_width += thick;
24162 }
24163
24164 /* If face has an overline, add the height of the overline
24165 (1 pixel) and a 1 pixel margin to the character height. */
24166 if (face->overline_p)
24167 it->ascent += overline_margin;
24168
24169 if (it->constrain_row_ascent_descent_p)
24170 {
24171 if (it->ascent > it->max_ascent)
24172 it->ascent = it->max_ascent;
24173 if (it->descent > it->max_descent)
24174 it->descent = it->max_descent;
24175 }
24176
24177 take_vertical_position_into_account (it);
24178
24179 /* If we have to actually produce glyphs, do it. */
24180 if (it->glyph_row)
24181 {
24182 if (stretched_p)
24183 {
24184 /* Translate a space with a `space-width' property
24185 into a stretch glyph. */
24186 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24187 / FONT_HEIGHT (font));
24188 append_stretch_glyph (it, it->object, it->pixel_width,
24189 it->ascent + it->descent, ascent);
24190 }
24191 else
24192 append_glyph (it);
24193
24194 /* If characters with lbearing or rbearing are displayed
24195 in this line, record that fact in a flag of the
24196 glyph row. This is used to optimize X output code. */
24197 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24198 it->glyph_row->contains_overlapping_glyphs_p = 1;
24199 }
24200 if (! stretched_p && it->pixel_width == 0)
24201 /* We assure that all visible glyphs have at least 1-pixel
24202 width. */
24203 it->pixel_width = 1;
24204 }
24205 else if (it->char_to_display == '\n')
24206 {
24207 /* A newline has no width, but we need the height of the
24208 line. But if previous part of the line sets a height,
24209 don't increase that height */
24210
24211 Lisp_Object height;
24212 Lisp_Object total_height = Qnil;
24213
24214 it->override_ascent = -1;
24215 it->pixel_width = 0;
24216 it->nglyphs = 0;
24217
24218 height = get_it_property (it, Qline_height);
24219 /* Split (line-height total-height) list */
24220 if (CONSP (height)
24221 && CONSP (XCDR (height))
24222 && NILP (XCDR (XCDR (height))))
24223 {
24224 total_height = XCAR (XCDR (height));
24225 height = XCAR (height);
24226 }
24227 height = calc_line_height_property (it, height, font, boff, 1);
24228
24229 if (it->override_ascent >= 0)
24230 {
24231 it->ascent = it->override_ascent;
24232 it->descent = it->override_descent;
24233 boff = it->override_boff;
24234 }
24235 else
24236 {
24237 it->ascent = FONT_BASE (font) + boff;
24238 it->descent = FONT_DESCENT (font) - boff;
24239 }
24240
24241 if (EQ (height, Qt))
24242 {
24243 if (it->descent > it->max_descent)
24244 {
24245 it->ascent += it->descent - it->max_descent;
24246 it->descent = it->max_descent;
24247 }
24248 if (it->ascent > it->max_ascent)
24249 {
24250 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24251 it->ascent = it->max_ascent;
24252 }
24253 it->phys_ascent = min (it->phys_ascent, it->ascent);
24254 it->phys_descent = min (it->phys_descent, it->descent);
24255 it->constrain_row_ascent_descent_p = 1;
24256 extra_line_spacing = 0;
24257 }
24258 else
24259 {
24260 Lisp_Object spacing;
24261
24262 it->phys_ascent = it->ascent;
24263 it->phys_descent = it->descent;
24264
24265 if ((it->max_ascent > 0 || it->max_descent > 0)
24266 && face->box != FACE_NO_BOX
24267 && face->box_line_width > 0)
24268 {
24269 it->ascent += face->box_line_width;
24270 it->descent += face->box_line_width;
24271 }
24272 if (!NILP (height)
24273 && XINT (height) > it->ascent + it->descent)
24274 it->ascent = XINT (height) - it->descent;
24275
24276 if (!NILP (total_height))
24277 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24278 else
24279 {
24280 spacing = get_it_property (it, Qline_spacing);
24281 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24282 }
24283 if (INTEGERP (spacing))
24284 {
24285 extra_line_spacing = XINT (spacing);
24286 if (!NILP (total_height))
24287 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24288 }
24289 }
24290 }
24291 else /* i.e. (it->char_to_display == '\t') */
24292 {
24293 if (font->space_width > 0)
24294 {
24295 int tab_width = it->tab_width * font->space_width;
24296 int x = it->current_x + it->continuation_lines_width;
24297 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24298
24299 /* If the distance from the current position to the next tab
24300 stop is less than a space character width, use the
24301 tab stop after that. */
24302 if (next_tab_x - x < font->space_width)
24303 next_tab_x += tab_width;
24304
24305 it->pixel_width = next_tab_x - x;
24306 it->nglyphs = 1;
24307 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24308 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24309
24310 if (it->glyph_row)
24311 {
24312 append_stretch_glyph (it, it->object, it->pixel_width,
24313 it->ascent + it->descent, it->ascent);
24314 }
24315 }
24316 else
24317 {
24318 it->pixel_width = 0;
24319 it->nglyphs = 1;
24320 }
24321 }
24322 }
24323 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24324 {
24325 /* A static composition.
24326
24327 Note: A composition is represented as one glyph in the
24328 glyph matrix. There are no padding glyphs.
24329
24330 Important note: pixel_width, ascent, and descent are the
24331 values of what is drawn by draw_glyphs (i.e. the values of
24332 the overall glyphs composed). */
24333 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24334 int boff; /* baseline offset */
24335 struct composition *cmp = composition_table[it->cmp_it.id];
24336 int glyph_len = cmp->glyph_len;
24337 struct font *font = face->font;
24338
24339 it->nglyphs = 1;
24340
24341 /* If we have not yet calculated pixel size data of glyphs of
24342 the composition for the current face font, calculate them
24343 now. Theoretically, we have to check all fonts for the
24344 glyphs, but that requires much time and memory space. So,
24345 here we check only the font of the first glyph. This may
24346 lead to incorrect display, but it's very rare, and C-l
24347 (recenter-top-bottom) can correct the display anyway. */
24348 if (! cmp->font || cmp->font != font)
24349 {
24350 /* Ascent and descent of the font of the first character
24351 of this composition (adjusted by baseline offset).
24352 Ascent and descent of overall glyphs should not be less
24353 than these, respectively. */
24354 int font_ascent, font_descent, font_height;
24355 /* Bounding box of the overall glyphs. */
24356 int leftmost, rightmost, lowest, highest;
24357 int lbearing, rbearing;
24358 int i, width, ascent, descent;
24359 int left_padded = 0, right_padded = 0;
24360 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24361 XChar2b char2b;
24362 struct font_metrics *pcm;
24363 int font_not_found_p;
24364 ptrdiff_t pos;
24365
24366 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24367 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24368 break;
24369 if (glyph_len < cmp->glyph_len)
24370 right_padded = 1;
24371 for (i = 0; i < glyph_len; i++)
24372 {
24373 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24374 break;
24375 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24376 }
24377 if (i > 0)
24378 left_padded = 1;
24379
24380 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24381 : IT_CHARPOS (*it));
24382 /* If no suitable font is found, use the default font. */
24383 font_not_found_p = font == NULL;
24384 if (font_not_found_p)
24385 {
24386 face = face->ascii_face;
24387 font = face->font;
24388 }
24389 boff = font->baseline_offset;
24390 if (font->vertical_centering)
24391 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24392 font_ascent = FONT_BASE (font) + boff;
24393 font_descent = FONT_DESCENT (font) - boff;
24394 font_height = FONT_HEIGHT (font);
24395
24396 cmp->font = (void *) font;
24397
24398 pcm = NULL;
24399 if (! font_not_found_p)
24400 {
24401 get_char_face_and_encoding (it->f, c, it->face_id,
24402 &char2b, 0);
24403 pcm = get_per_char_metric (font, &char2b);
24404 }
24405
24406 /* Initialize the bounding box. */
24407 if (pcm)
24408 {
24409 width = pcm->width;
24410 ascent = pcm->ascent;
24411 descent = pcm->descent;
24412 lbearing = pcm->lbearing;
24413 rbearing = pcm->rbearing;
24414 }
24415 else
24416 {
24417 width = font->space_width;
24418 ascent = FONT_BASE (font);
24419 descent = FONT_DESCENT (font);
24420 lbearing = 0;
24421 rbearing = width;
24422 }
24423
24424 rightmost = width;
24425 leftmost = 0;
24426 lowest = - descent + boff;
24427 highest = ascent + boff;
24428
24429 if (! font_not_found_p
24430 && font->default_ascent
24431 && CHAR_TABLE_P (Vuse_default_ascent)
24432 && !NILP (Faref (Vuse_default_ascent,
24433 make_number (it->char_to_display))))
24434 highest = font->default_ascent + boff;
24435
24436 /* Draw the first glyph at the normal position. It may be
24437 shifted to right later if some other glyphs are drawn
24438 at the left. */
24439 cmp->offsets[i * 2] = 0;
24440 cmp->offsets[i * 2 + 1] = boff;
24441 cmp->lbearing = lbearing;
24442 cmp->rbearing = rbearing;
24443
24444 /* Set cmp->offsets for the remaining glyphs. */
24445 for (i++; i < glyph_len; i++)
24446 {
24447 int left, right, btm, top;
24448 int ch = COMPOSITION_GLYPH (cmp, i);
24449 int face_id;
24450 struct face *this_face;
24451
24452 if (ch == '\t')
24453 ch = ' ';
24454 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24455 this_face = FACE_FROM_ID (it->f, face_id);
24456 font = this_face->font;
24457
24458 if (font == NULL)
24459 pcm = NULL;
24460 else
24461 {
24462 get_char_face_and_encoding (it->f, ch, face_id,
24463 &char2b, 0);
24464 pcm = get_per_char_metric (font, &char2b);
24465 }
24466 if (! pcm)
24467 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24468 else
24469 {
24470 width = pcm->width;
24471 ascent = pcm->ascent;
24472 descent = pcm->descent;
24473 lbearing = pcm->lbearing;
24474 rbearing = pcm->rbearing;
24475 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24476 {
24477 /* Relative composition with or without
24478 alternate chars. */
24479 left = (leftmost + rightmost - width) / 2;
24480 btm = - descent + boff;
24481 if (font->relative_compose
24482 && (! CHAR_TABLE_P (Vignore_relative_composition)
24483 || NILP (Faref (Vignore_relative_composition,
24484 make_number (ch)))))
24485 {
24486
24487 if (- descent >= font->relative_compose)
24488 /* One extra pixel between two glyphs. */
24489 btm = highest + 1;
24490 else if (ascent <= 0)
24491 /* One extra pixel between two glyphs. */
24492 btm = lowest - 1 - ascent - descent;
24493 }
24494 }
24495 else
24496 {
24497 /* A composition rule is specified by an integer
24498 value that encodes global and new reference
24499 points (GREF and NREF). GREF and NREF are
24500 specified by numbers as below:
24501
24502 0---1---2 -- ascent
24503 | |
24504 | |
24505 | |
24506 9--10--11 -- center
24507 | |
24508 ---3---4---5--- baseline
24509 | |
24510 6---7---8 -- descent
24511 */
24512 int rule = COMPOSITION_RULE (cmp, i);
24513 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24514
24515 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24516 grefx = gref % 3, nrefx = nref % 3;
24517 grefy = gref / 3, nrefy = nref / 3;
24518 if (xoff)
24519 xoff = font_height * (xoff - 128) / 256;
24520 if (yoff)
24521 yoff = font_height * (yoff - 128) / 256;
24522
24523 left = (leftmost
24524 + grefx * (rightmost - leftmost) / 2
24525 - nrefx * width / 2
24526 + xoff);
24527
24528 btm = ((grefy == 0 ? highest
24529 : grefy == 1 ? 0
24530 : grefy == 2 ? lowest
24531 : (highest + lowest) / 2)
24532 - (nrefy == 0 ? ascent + descent
24533 : nrefy == 1 ? descent - boff
24534 : nrefy == 2 ? 0
24535 : (ascent + descent) / 2)
24536 + yoff);
24537 }
24538
24539 cmp->offsets[i * 2] = left;
24540 cmp->offsets[i * 2 + 1] = btm + descent;
24541
24542 /* Update the bounding box of the overall glyphs. */
24543 if (width > 0)
24544 {
24545 right = left + width;
24546 if (left < leftmost)
24547 leftmost = left;
24548 if (right > rightmost)
24549 rightmost = right;
24550 }
24551 top = btm + descent + ascent;
24552 if (top > highest)
24553 highest = top;
24554 if (btm < lowest)
24555 lowest = btm;
24556
24557 if (cmp->lbearing > left + lbearing)
24558 cmp->lbearing = left + lbearing;
24559 if (cmp->rbearing < left + rbearing)
24560 cmp->rbearing = left + rbearing;
24561 }
24562 }
24563
24564 /* If there are glyphs whose x-offsets are negative,
24565 shift all glyphs to the right and make all x-offsets
24566 non-negative. */
24567 if (leftmost < 0)
24568 {
24569 for (i = 0; i < cmp->glyph_len; i++)
24570 cmp->offsets[i * 2] -= leftmost;
24571 rightmost -= leftmost;
24572 cmp->lbearing -= leftmost;
24573 cmp->rbearing -= leftmost;
24574 }
24575
24576 if (left_padded && cmp->lbearing < 0)
24577 {
24578 for (i = 0; i < cmp->glyph_len; i++)
24579 cmp->offsets[i * 2] -= cmp->lbearing;
24580 rightmost -= cmp->lbearing;
24581 cmp->rbearing -= cmp->lbearing;
24582 cmp->lbearing = 0;
24583 }
24584 if (right_padded && rightmost < cmp->rbearing)
24585 {
24586 rightmost = cmp->rbearing;
24587 }
24588
24589 cmp->pixel_width = rightmost;
24590 cmp->ascent = highest;
24591 cmp->descent = - lowest;
24592 if (cmp->ascent < font_ascent)
24593 cmp->ascent = font_ascent;
24594 if (cmp->descent < font_descent)
24595 cmp->descent = font_descent;
24596 }
24597
24598 if (it->glyph_row
24599 && (cmp->lbearing < 0
24600 || cmp->rbearing > cmp->pixel_width))
24601 it->glyph_row->contains_overlapping_glyphs_p = 1;
24602
24603 it->pixel_width = cmp->pixel_width;
24604 it->ascent = it->phys_ascent = cmp->ascent;
24605 it->descent = it->phys_descent = cmp->descent;
24606 if (face->box != FACE_NO_BOX)
24607 {
24608 int thick = face->box_line_width;
24609
24610 if (thick > 0)
24611 {
24612 it->ascent += thick;
24613 it->descent += thick;
24614 }
24615 else
24616 thick = - thick;
24617
24618 if (it->start_of_box_run_p)
24619 it->pixel_width += thick;
24620 if (it->end_of_box_run_p)
24621 it->pixel_width += thick;
24622 }
24623
24624 /* If face has an overline, add the height of the overline
24625 (1 pixel) and a 1 pixel margin to the character height. */
24626 if (face->overline_p)
24627 it->ascent += overline_margin;
24628
24629 take_vertical_position_into_account (it);
24630 if (it->ascent < 0)
24631 it->ascent = 0;
24632 if (it->descent < 0)
24633 it->descent = 0;
24634
24635 if (it->glyph_row)
24636 append_composite_glyph (it);
24637 }
24638 else if (it->what == IT_COMPOSITION)
24639 {
24640 /* A dynamic (automatic) composition. */
24641 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24642 Lisp_Object gstring;
24643 struct font_metrics metrics;
24644
24645 it->nglyphs = 1;
24646
24647 gstring = composition_gstring_from_id (it->cmp_it.id);
24648 it->pixel_width
24649 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24650 &metrics);
24651 if (it->glyph_row
24652 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24653 it->glyph_row->contains_overlapping_glyphs_p = 1;
24654 it->ascent = it->phys_ascent = metrics.ascent;
24655 it->descent = it->phys_descent = metrics.descent;
24656 if (face->box != FACE_NO_BOX)
24657 {
24658 int thick = face->box_line_width;
24659
24660 if (thick > 0)
24661 {
24662 it->ascent += thick;
24663 it->descent += thick;
24664 }
24665 else
24666 thick = - thick;
24667
24668 if (it->start_of_box_run_p)
24669 it->pixel_width += thick;
24670 if (it->end_of_box_run_p)
24671 it->pixel_width += thick;
24672 }
24673 /* If face has an overline, add the height of the overline
24674 (1 pixel) and a 1 pixel margin to the character height. */
24675 if (face->overline_p)
24676 it->ascent += overline_margin;
24677 take_vertical_position_into_account (it);
24678 if (it->ascent < 0)
24679 it->ascent = 0;
24680 if (it->descent < 0)
24681 it->descent = 0;
24682
24683 if (it->glyph_row)
24684 append_composite_glyph (it);
24685 }
24686 else if (it->what == IT_GLYPHLESS)
24687 produce_glyphless_glyph (it, 0, Qnil);
24688 else if (it->what == IT_IMAGE)
24689 produce_image_glyph (it);
24690 else if (it->what == IT_STRETCH)
24691 produce_stretch_glyph (it);
24692
24693 done:
24694 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24695 because this isn't true for images with `:ascent 100'. */
24696 xassert (it->ascent >= 0 && it->descent >= 0);
24697 if (it->area == TEXT_AREA)
24698 it->current_x += it->pixel_width;
24699
24700 if (extra_line_spacing > 0)
24701 {
24702 it->descent += extra_line_spacing;
24703 if (extra_line_spacing > it->max_extra_line_spacing)
24704 it->max_extra_line_spacing = extra_line_spacing;
24705 }
24706
24707 it->max_ascent = max (it->max_ascent, it->ascent);
24708 it->max_descent = max (it->max_descent, it->descent);
24709 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24710 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24711 }
24712
24713 /* EXPORT for RIF:
24714 Output LEN glyphs starting at START at the nominal cursor position.
24715 Advance the nominal cursor over the text. The global variable
24716 updated_window contains the window being updated, updated_row is
24717 the glyph row being updated, and updated_area is the area of that
24718 row being updated. */
24719
24720 void
24721 x_write_glyphs (struct glyph *start, int len)
24722 {
24723 int x, hpos, chpos = updated_window->phys_cursor.hpos;
24724
24725 xassert (updated_window && updated_row);
24726 /* When the window is hscrolled, cursor hpos can legitimately be out
24727 of bounds, but we draw the cursor at the corresponding window
24728 margin in that case. */
24729 if (!updated_row->reversed_p && chpos < 0)
24730 chpos = 0;
24731 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
24732 chpos = updated_row->used[TEXT_AREA] - 1;
24733
24734 BLOCK_INPUT;
24735
24736 /* Write glyphs. */
24737
24738 hpos = start - updated_row->glyphs[updated_area];
24739 x = draw_glyphs (updated_window, output_cursor.x,
24740 updated_row, updated_area,
24741 hpos, hpos + len,
24742 DRAW_NORMAL_TEXT, 0);
24743
24744 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
24745 if (updated_area == TEXT_AREA
24746 && updated_window->phys_cursor_on_p
24747 && updated_window->phys_cursor.vpos == output_cursor.vpos
24748 && chpos >= hpos
24749 && chpos < hpos + len)
24750 updated_window->phys_cursor_on_p = 0;
24751
24752 UNBLOCK_INPUT;
24753
24754 /* Advance the output cursor. */
24755 output_cursor.hpos += len;
24756 output_cursor.x = x;
24757 }
24758
24759
24760 /* EXPORT for RIF:
24761 Insert LEN glyphs from START at the nominal cursor position. */
24762
24763 void
24764 x_insert_glyphs (struct glyph *start, int len)
24765 {
24766 struct frame *f;
24767 struct window *w;
24768 int line_height, shift_by_width, shifted_region_width;
24769 struct glyph_row *row;
24770 struct glyph *glyph;
24771 int frame_x, frame_y;
24772 ptrdiff_t hpos;
24773
24774 xassert (updated_window && updated_row);
24775 BLOCK_INPUT;
24776 w = updated_window;
24777 f = XFRAME (WINDOW_FRAME (w));
24778
24779 /* Get the height of the line we are in. */
24780 row = updated_row;
24781 line_height = row->height;
24782
24783 /* Get the width of the glyphs to insert. */
24784 shift_by_width = 0;
24785 for (glyph = start; glyph < start + len; ++glyph)
24786 shift_by_width += glyph->pixel_width;
24787
24788 /* Get the width of the region to shift right. */
24789 shifted_region_width = (window_box_width (w, updated_area)
24790 - output_cursor.x
24791 - shift_by_width);
24792
24793 /* Shift right. */
24794 frame_x = window_box_left (w, updated_area) + output_cursor.x;
24795 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
24796
24797 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
24798 line_height, shift_by_width);
24799
24800 /* Write the glyphs. */
24801 hpos = start - row->glyphs[updated_area];
24802 draw_glyphs (w, output_cursor.x, row, updated_area,
24803 hpos, hpos + len,
24804 DRAW_NORMAL_TEXT, 0);
24805
24806 /* Advance the output cursor. */
24807 output_cursor.hpos += len;
24808 output_cursor.x += shift_by_width;
24809 UNBLOCK_INPUT;
24810 }
24811
24812
24813 /* EXPORT for RIF:
24814 Erase the current text line from the nominal cursor position
24815 (inclusive) to pixel column TO_X (exclusive). The idea is that
24816 everything from TO_X onward is already erased.
24817
24818 TO_X is a pixel position relative to updated_area of
24819 updated_window. TO_X == -1 means clear to the end of this area. */
24820
24821 void
24822 x_clear_end_of_line (int to_x)
24823 {
24824 struct frame *f;
24825 struct window *w = updated_window;
24826 int max_x, min_y, max_y;
24827 int from_x, from_y, to_y;
24828
24829 xassert (updated_window && updated_row);
24830 f = XFRAME (w->frame);
24831
24832 if (updated_row->full_width_p)
24833 max_x = WINDOW_TOTAL_WIDTH (w);
24834 else
24835 max_x = window_box_width (w, updated_area);
24836 max_y = window_text_bottom_y (w);
24837
24838 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
24839 of window. For TO_X > 0, truncate to end of drawing area. */
24840 if (to_x == 0)
24841 return;
24842 else if (to_x < 0)
24843 to_x = max_x;
24844 else
24845 to_x = min (to_x, max_x);
24846
24847 to_y = min (max_y, output_cursor.y + updated_row->height);
24848
24849 /* Notice if the cursor will be cleared by this operation. */
24850 if (!updated_row->full_width_p)
24851 notice_overwritten_cursor (w, updated_area,
24852 output_cursor.x, -1,
24853 updated_row->y,
24854 MATRIX_ROW_BOTTOM_Y (updated_row));
24855
24856 from_x = output_cursor.x;
24857
24858 /* Translate to frame coordinates. */
24859 if (updated_row->full_width_p)
24860 {
24861 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
24862 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
24863 }
24864 else
24865 {
24866 int area_left = window_box_left (w, updated_area);
24867 from_x += area_left;
24868 to_x += area_left;
24869 }
24870
24871 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
24872 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
24873 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
24874
24875 /* Prevent inadvertently clearing to end of the X window. */
24876 if (to_x > from_x && to_y > from_y)
24877 {
24878 BLOCK_INPUT;
24879 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
24880 to_x - from_x, to_y - from_y);
24881 UNBLOCK_INPUT;
24882 }
24883 }
24884
24885 #endif /* HAVE_WINDOW_SYSTEM */
24886
24887
24888 \f
24889 /***********************************************************************
24890 Cursor types
24891 ***********************************************************************/
24892
24893 /* Value is the internal representation of the specified cursor type
24894 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
24895 of the bar cursor. */
24896
24897 static enum text_cursor_kinds
24898 get_specified_cursor_type (Lisp_Object arg, int *width)
24899 {
24900 enum text_cursor_kinds type;
24901
24902 if (NILP (arg))
24903 return NO_CURSOR;
24904
24905 if (EQ (arg, Qbox))
24906 return FILLED_BOX_CURSOR;
24907
24908 if (EQ (arg, Qhollow))
24909 return HOLLOW_BOX_CURSOR;
24910
24911 if (EQ (arg, Qbar))
24912 {
24913 *width = 2;
24914 return BAR_CURSOR;
24915 }
24916
24917 if (CONSP (arg)
24918 && EQ (XCAR (arg), Qbar)
24919 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
24920 {
24921 *width = XINT (XCDR (arg));
24922 return BAR_CURSOR;
24923 }
24924
24925 if (EQ (arg, Qhbar))
24926 {
24927 *width = 2;
24928 return HBAR_CURSOR;
24929 }
24930
24931 if (CONSP (arg)
24932 && EQ (XCAR (arg), Qhbar)
24933 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
24934 {
24935 *width = XINT (XCDR (arg));
24936 return HBAR_CURSOR;
24937 }
24938
24939 /* Treat anything unknown as "hollow box cursor".
24940 It was bad to signal an error; people have trouble fixing
24941 .Xdefaults with Emacs, when it has something bad in it. */
24942 type = HOLLOW_BOX_CURSOR;
24943
24944 return type;
24945 }
24946
24947 /* Set the default cursor types for specified frame. */
24948 void
24949 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
24950 {
24951 int width = 1;
24952 Lisp_Object tem;
24953
24954 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
24955 FRAME_CURSOR_WIDTH (f) = width;
24956
24957 /* By default, set up the blink-off state depending on the on-state. */
24958
24959 tem = Fassoc (arg, Vblink_cursor_alist);
24960 if (!NILP (tem))
24961 {
24962 FRAME_BLINK_OFF_CURSOR (f)
24963 = get_specified_cursor_type (XCDR (tem), &width);
24964 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
24965 }
24966 else
24967 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
24968 }
24969
24970
24971 #ifdef HAVE_WINDOW_SYSTEM
24972
24973 /* Return the cursor we want to be displayed in window W. Return
24974 width of bar/hbar cursor through WIDTH arg. Return with
24975 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
24976 (i.e. if the `system caret' should track this cursor).
24977
24978 In a mini-buffer window, we want the cursor only to appear if we
24979 are reading input from this window. For the selected window, we
24980 want the cursor type given by the frame parameter or buffer local
24981 setting of cursor-type. If explicitly marked off, draw no cursor.
24982 In all other cases, we want a hollow box cursor. */
24983
24984 static enum text_cursor_kinds
24985 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
24986 int *active_cursor)
24987 {
24988 struct frame *f = XFRAME (w->frame);
24989 struct buffer *b = XBUFFER (w->buffer);
24990 int cursor_type = DEFAULT_CURSOR;
24991 Lisp_Object alt_cursor;
24992 int non_selected = 0;
24993
24994 *active_cursor = 1;
24995
24996 /* Echo area */
24997 if (cursor_in_echo_area
24998 && FRAME_HAS_MINIBUF_P (f)
24999 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25000 {
25001 if (w == XWINDOW (echo_area_window))
25002 {
25003 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25004 {
25005 *width = FRAME_CURSOR_WIDTH (f);
25006 return FRAME_DESIRED_CURSOR (f);
25007 }
25008 else
25009 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25010 }
25011
25012 *active_cursor = 0;
25013 non_selected = 1;
25014 }
25015
25016 /* Detect a nonselected window or nonselected frame. */
25017 else if (w != XWINDOW (f->selected_window)
25018 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25019 {
25020 *active_cursor = 0;
25021
25022 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25023 return NO_CURSOR;
25024
25025 non_selected = 1;
25026 }
25027
25028 /* Never display a cursor in a window in which cursor-type is nil. */
25029 if (NILP (BVAR (b, cursor_type)))
25030 return NO_CURSOR;
25031
25032 /* Get the normal cursor type for this window. */
25033 if (EQ (BVAR (b, cursor_type), Qt))
25034 {
25035 cursor_type = FRAME_DESIRED_CURSOR (f);
25036 *width = FRAME_CURSOR_WIDTH (f);
25037 }
25038 else
25039 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25040
25041 /* Use cursor-in-non-selected-windows instead
25042 for non-selected window or frame. */
25043 if (non_selected)
25044 {
25045 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25046 if (!EQ (Qt, alt_cursor))
25047 return get_specified_cursor_type (alt_cursor, width);
25048 /* t means modify the normal cursor type. */
25049 if (cursor_type == FILLED_BOX_CURSOR)
25050 cursor_type = HOLLOW_BOX_CURSOR;
25051 else if (cursor_type == BAR_CURSOR && *width > 1)
25052 --*width;
25053 return cursor_type;
25054 }
25055
25056 /* Use normal cursor if not blinked off. */
25057 if (!w->cursor_off_p)
25058 {
25059 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25060 {
25061 if (cursor_type == FILLED_BOX_CURSOR)
25062 {
25063 /* Using a block cursor on large images can be very annoying.
25064 So use a hollow cursor for "large" images.
25065 If image is not transparent (no mask), also use hollow cursor. */
25066 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25067 if (img != NULL && IMAGEP (img->spec))
25068 {
25069 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25070 where N = size of default frame font size.
25071 This should cover most of the "tiny" icons people may use. */
25072 if (!img->mask
25073 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25074 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25075 cursor_type = HOLLOW_BOX_CURSOR;
25076 }
25077 }
25078 else if (cursor_type != NO_CURSOR)
25079 {
25080 /* Display current only supports BOX and HOLLOW cursors for images.
25081 So for now, unconditionally use a HOLLOW cursor when cursor is
25082 not a solid box cursor. */
25083 cursor_type = HOLLOW_BOX_CURSOR;
25084 }
25085 }
25086 return cursor_type;
25087 }
25088
25089 /* Cursor is blinked off, so determine how to "toggle" it. */
25090
25091 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25092 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25093 return get_specified_cursor_type (XCDR (alt_cursor), width);
25094
25095 /* Then see if frame has specified a specific blink off cursor type. */
25096 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25097 {
25098 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25099 return FRAME_BLINK_OFF_CURSOR (f);
25100 }
25101
25102 #if 0
25103 /* Some people liked having a permanently visible blinking cursor,
25104 while others had very strong opinions against it. So it was
25105 decided to remove it. KFS 2003-09-03 */
25106
25107 /* Finally perform built-in cursor blinking:
25108 filled box <-> hollow box
25109 wide [h]bar <-> narrow [h]bar
25110 narrow [h]bar <-> no cursor
25111 other type <-> no cursor */
25112
25113 if (cursor_type == FILLED_BOX_CURSOR)
25114 return HOLLOW_BOX_CURSOR;
25115
25116 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25117 {
25118 *width = 1;
25119 return cursor_type;
25120 }
25121 #endif
25122
25123 return NO_CURSOR;
25124 }
25125
25126
25127 /* Notice when the text cursor of window W has been completely
25128 overwritten by a drawing operation that outputs glyphs in AREA
25129 starting at X0 and ending at X1 in the line starting at Y0 and
25130 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25131 the rest of the line after X0 has been written. Y coordinates
25132 are window-relative. */
25133
25134 static void
25135 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25136 int x0, int x1, int y0, int y1)
25137 {
25138 int cx0, cx1, cy0, cy1;
25139 struct glyph_row *row;
25140
25141 if (!w->phys_cursor_on_p)
25142 return;
25143 if (area != TEXT_AREA)
25144 return;
25145
25146 if (w->phys_cursor.vpos < 0
25147 || w->phys_cursor.vpos >= w->current_matrix->nrows
25148 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25149 !(row->enabled_p && row->displays_text_p)))
25150 return;
25151
25152 if (row->cursor_in_fringe_p)
25153 {
25154 row->cursor_in_fringe_p = 0;
25155 draw_fringe_bitmap (w, row, row->reversed_p);
25156 w->phys_cursor_on_p = 0;
25157 return;
25158 }
25159
25160 cx0 = w->phys_cursor.x;
25161 cx1 = cx0 + w->phys_cursor_width;
25162 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25163 return;
25164
25165 /* The cursor image will be completely removed from the
25166 screen if the output area intersects the cursor area in
25167 y-direction. When we draw in [y0 y1[, and some part of
25168 the cursor is at y < y0, that part must have been drawn
25169 before. When scrolling, the cursor is erased before
25170 actually scrolling, so we don't come here. When not
25171 scrolling, the rows above the old cursor row must have
25172 changed, and in this case these rows must have written
25173 over the cursor image.
25174
25175 Likewise if part of the cursor is below y1, with the
25176 exception of the cursor being in the first blank row at
25177 the buffer and window end because update_text_area
25178 doesn't draw that row. (Except when it does, but
25179 that's handled in update_text_area.) */
25180
25181 cy0 = w->phys_cursor.y;
25182 cy1 = cy0 + w->phys_cursor_height;
25183 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25184 return;
25185
25186 w->phys_cursor_on_p = 0;
25187 }
25188
25189 #endif /* HAVE_WINDOW_SYSTEM */
25190
25191 \f
25192 /************************************************************************
25193 Mouse Face
25194 ************************************************************************/
25195
25196 #ifdef HAVE_WINDOW_SYSTEM
25197
25198 /* EXPORT for RIF:
25199 Fix the display of area AREA of overlapping row ROW in window W
25200 with respect to the overlapping part OVERLAPS. */
25201
25202 void
25203 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25204 enum glyph_row_area area, int overlaps)
25205 {
25206 int i, x;
25207
25208 BLOCK_INPUT;
25209
25210 x = 0;
25211 for (i = 0; i < row->used[area];)
25212 {
25213 if (row->glyphs[area][i].overlaps_vertically_p)
25214 {
25215 int start = i, start_x = x;
25216
25217 do
25218 {
25219 x += row->glyphs[area][i].pixel_width;
25220 ++i;
25221 }
25222 while (i < row->used[area]
25223 && row->glyphs[area][i].overlaps_vertically_p);
25224
25225 draw_glyphs (w, start_x, row, area,
25226 start, i,
25227 DRAW_NORMAL_TEXT, overlaps);
25228 }
25229 else
25230 {
25231 x += row->glyphs[area][i].pixel_width;
25232 ++i;
25233 }
25234 }
25235
25236 UNBLOCK_INPUT;
25237 }
25238
25239
25240 /* EXPORT:
25241 Draw the cursor glyph of window W in glyph row ROW. See the
25242 comment of draw_glyphs for the meaning of HL. */
25243
25244 void
25245 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25246 enum draw_glyphs_face hl)
25247 {
25248 /* If cursor hpos is out of bounds, don't draw garbage. This can
25249 happen in mini-buffer windows when switching between echo area
25250 glyphs and mini-buffer. */
25251 if ((row->reversed_p
25252 ? (w->phys_cursor.hpos >= 0)
25253 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25254 {
25255 int on_p = w->phys_cursor_on_p;
25256 int x1;
25257 int hpos = w->phys_cursor.hpos;
25258
25259 /* When the window is hscrolled, cursor hpos can legitimately be
25260 out of bounds, but we draw the cursor at the corresponding
25261 window margin in that case. */
25262 if (!row->reversed_p && hpos < 0)
25263 hpos = 0;
25264 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25265 hpos = row->used[TEXT_AREA] - 1;
25266
25267 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25268 hl, 0);
25269 w->phys_cursor_on_p = on_p;
25270
25271 if (hl == DRAW_CURSOR)
25272 w->phys_cursor_width = x1 - w->phys_cursor.x;
25273 /* When we erase the cursor, and ROW is overlapped by other
25274 rows, make sure that these overlapping parts of other rows
25275 are redrawn. */
25276 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25277 {
25278 w->phys_cursor_width = x1 - w->phys_cursor.x;
25279
25280 if (row > w->current_matrix->rows
25281 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25282 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25283 OVERLAPS_ERASED_CURSOR);
25284
25285 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25286 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25287 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25288 OVERLAPS_ERASED_CURSOR);
25289 }
25290 }
25291 }
25292
25293
25294 /* EXPORT:
25295 Erase the image of a cursor of window W from the screen. */
25296
25297 void
25298 erase_phys_cursor (struct window *w)
25299 {
25300 struct frame *f = XFRAME (w->frame);
25301 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25302 int hpos = w->phys_cursor.hpos;
25303 int vpos = w->phys_cursor.vpos;
25304 int mouse_face_here_p = 0;
25305 struct glyph_matrix *active_glyphs = w->current_matrix;
25306 struct glyph_row *cursor_row;
25307 struct glyph *cursor_glyph;
25308 enum draw_glyphs_face hl;
25309
25310 /* No cursor displayed or row invalidated => nothing to do on the
25311 screen. */
25312 if (w->phys_cursor_type == NO_CURSOR)
25313 goto mark_cursor_off;
25314
25315 /* VPOS >= active_glyphs->nrows means that window has been resized.
25316 Don't bother to erase the cursor. */
25317 if (vpos >= active_glyphs->nrows)
25318 goto mark_cursor_off;
25319
25320 /* If row containing cursor is marked invalid, there is nothing we
25321 can do. */
25322 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25323 if (!cursor_row->enabled_p)
25324 goto mark_cursor_off;
25325
25326 /* If line spacing is > 0, old cursor may only be partially visible in
25327 window after split-window. So adjust visible height. */
25328 cursor_row->visible_height = min (cursor_row->visible_height,
25329 window_text_bottom_y (w) - cursor_row->y);
25330
25331 /* If row is completely invisible, don't attempt to delete a cursor which
25332 isn't there. This can happen if cursor is at top of a window, and
25333 we switch to a buffer with a header line in that window. */
25334 if (cursor_row->visible_height <= 0)
25335 goto mark_cursor_off;
25336
25337 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25338 if (cursor_row->cursor_in_fringe_p)
25339 {
25340 cursor_row->cursor_in_fringe_p = 0;
25341 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25342 goto mark_cursor_off;
25343 }
25344
25345 /* This can happen when the new row is shorter than the old one.
25346 In this case, either draw_glyphs or clear_end_of_line
25347 should have cleared the cursor. Note that we wouldn't be
25348 able to erase the cursor in this case because we don't have a
25349 cursor glyph at hand. */
25350 if ((cursor_row->reversed_p
25351 ? (w->phys_cursor.hpos < 0)
25352 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25353 goto mark_cursor_off;
25354
25355 /* When the window is hscrolled, cursor hpos can legitimately be out
25356 of bounds, but we draw the cursor at the corresponding window
25357 margin in that case. */
25358 if (!cursor_row->reversed_p && hpos < 0)
25359 hpos = 0;
25360 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25361 hpos = cursor_row->used[TEXT_AREA] - 1;
25362
25363 /* If the cursor is in the mouse face area, redisplay that when
25364 we clear the cursor. */
25365 if (! NILP (hlinfo->mouse_face_window)
25366 && coords_in_mouse_face_p (w, hpos, vpos)
25367 /* Don't redraw the cursor's spot in mouse face if it is at the
25368 end of a line (on a newline). The cursor appears there, but
25369 mouse highlighting does not. */
25370 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25371 mouse_face_here_p = 1;
25372
25373 /* Maybe clear the display under the cursor. */
25374 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25375 {
25376 int x, y, left_x;
25377 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25378 int width;
25379
25380 cursor_glyph = get_phys_cursor_glyph (w);
25381 if (cursor_glyph == NULL)
25382 goto mark_cursor_off;
25383
25384 width = cursor_glyph->pixel_width;
25385 left_x = window_box_left_offset (w, TEXT_AREA);
25386 x = w->phys_cursor.x;
25387 if (x < left_x)
25388 width -= left_x - x;
25389 width = min (width, window_box_width (w, TEXT_AREA) - x);
25390 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25391 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25392
25393 if (width > 0)
25394 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25395 }
25396
25397 /* Erase the cursor by redrawing the character underneath it. */
25398 if (mouse_face_here_p)
25399 hl = DRAW_MOUSE_FACE;
25400 else
25401 hl = DRAW_NORMAL_TEXT;
25402 draw_phys_cursor_glyph (w, cursor_row, hl);
25403
25404 mark_cursor_off:
25405 w->phys_cursor_on_p = 0;
25406 w->phys_cursor_type = NO_CURSOR;
25407 }
25408
25409
25410 /* EXPORT:
25411 Display or clear cursor of window W. If ON is zero, clear the
25412 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25413 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25414
25415 void
25416 display_and_set_cursor (struct window *w, int on,
25417 int hpos, int vpos, int x, int y)
25418 {
25419 struct frame *f = XFRAME (w->frame);
25420 int new_cursor_type;
25421 int new_cursor_width;
25422 int active_cursor;
25423 struct glyph_row *glyph_row;
25424 struct glyph *glyph;
25425
25426 /* This is pointless on invisible frames, and dangerous on garbaged
25427 windows and frames; in the latter case, the frame or window may
25428 be in the midst of changing its size, and x and y may be off the
25429 window. */
25430 if (! FRAME_VISIBLE_P (f)
25431 || FRAME_GARBAGED_P (f)
25432 || vpos >= w->current_matrix->nrows
25433 || hpos >= w->current_matrix->matrix_w)
25434 return;
25435
25436 /* If cursor is off and we want it off, return quickly. */
25437 if (!on && !w->phys_cursor_on_p)
25438 return;
25439
25440 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25441 /* If cursor row is not enabled, we don't really know where to
25442 display the cursor. */
25443 if (!glyph_row->enabled_p)
25444 {
25445 w->phys_cursor_on_p = 0;
25446 return;
25447 }
25448
25449 glyph = NULL;
25450 if (!glyph_row->exact_window_width_line_p
25451 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25452 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25453
25454 xassert (interrupt_input_blocked);
25455
25456 /* Set new_cursor_type to the cursor we want to be displayed. */
25457 new_cursor_type = get_window_cursor_type (w, glyph,
25458 &new_cursor_width, &active_cursor);
25459
25460 /* If cursor is currently being shown and we don't want it to be or
25461 it is in the wrong place, or the cursor type is not what we want,
25462 erase it. */
25463 if (w->phys_cursor_on_p
25464 && (!on
25465 || w->phys_cursor.x != x
25466 || w->phys_cursor.y != y
25467 || new_cursor_type != w->phys_cursor_type
25468 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25469 && new_cursor_width != w->phys_cursor_width)))
25470 erase_phys_cursor (w);
25471
25472 /* Don't check phys_cursor_on_p here because that flag is only set
25473 to zero in some cases where we know that the cursor has been
25474 completely erased, to avoid the extra work of erasing the cursor
25475 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25476 still not be visible, or it has only been partly erased. */
25477 if (on)
25478 {
25479 w->phys_cursor_ascent = glyph_row->ascent;
25480 w->phys_cursor_height = glyph_row->height;
25481
25482 /* Set phys_cursor_.* before x_draw_.* is called because some
25483 of them may need the information. */
25484 w->phys_cursor.x = x;
25485 w->phys_cursor.y = glyph_row->y;
25486 w->phys_cursor.hpos = hpos;
25487 w->phys_cursor.vpos = vpos;
25488 }
25489
25490 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25491 new_cursor_type, new_cursor_width,
25492 on, active_cursor);
25493 }
25494
25495
25496 /* Switch the display of W's cursor on or off, according to the value
25497 of ON. */
25498
25499 static void
25500 update_window_cursor (struct window *w, int on)
25501 {
25502 /* Don't update cursor in windows whose frame is in the process
25503 of being deleted. */
25504 if (w->current_matrix)
25505 {
25506 int hpos = w->phys_cursor.hpos;
25507 int vpos = w->phys_cursor.vpos;
25508 struct glyph_row *row;
25509
25510 if (vpos >= w->current_matrix->nrows
25511 || hpos >= w->current_matrix->matrix_w)
25512 return;
25513
25514 row = MATRIX_ROW (w->current_matrix, vpos);
25515
25516 /* When the window is hscrolled, cursor hpos can legitimately be
25517 out of bounds, but we draw the cursor at the corresponding
25518 window margin in that case. */
25519 if (!row->reversed_p && hpos < 0)
25520 hpos = 0;
25521 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25522 hpos = row->used[TEXT_AREA] - 1;
25523
25524 BLOCK_INPUT;
25525 display_and_set_cursor (w, on, hpos, vpos,
25526 w->phys_cursor.x, w->phys_cursor.y);
25527 UNBLOCK_INPUT;
25528 }
25529 }
25530
25531
25532 /* Call update_window_cursor with parameter ON_P on all leaf windows
25533 in the window tree rooted at W. */
25534
25535 static void
25536 update_cursor_in_window_tree (struct window *w, int on_p)
25537 {
25538 while (w)
25539 {
25540 if (!NILP (w->hchild))
25541 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25542 else if (!NILP (w->vchild))
25543 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25544 else
25545 update_window_cursor (w, on_p);
25546
25547 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25548 }
25549 }
25550
25551
25552 /* EXPORT:
25553 Display the cursor on window W, or clear it, according to ON_P.
25554 Don't change the cursor's position. */
25555
25556 void
25557 x_update_cursor (struct frame *f, int on_p)
25558 {
25559 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25560 }
25561
25562
25563 /* EXPORT:
25564 Clear the cursor of window W to background color, and mark the
25565 cursor as not shown. This is used when the text where the cursor
25566 is about to be rewritten. */
25567
25568 void
25569 x_clear_cursor (struct window *w)
25570 {
25571 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25572 update_window_cursor (w, 0);
25573 }
25574
25575 #endif /* HAVE_WINDOW_SYSTEM */
25576
25577 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25578 and MSDOS. */
25579 static void
25580 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25581 int start_hpos, int end_hpos,
25582 enum draw_glyphs_face draw)
25583 {
25584 #ifdef HAVE_WINDOW_SYSTEM
25585 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25586 {
25587 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25588 return;
25589 }
25590 #endif
25591 #if defined (HAVE_GPM) || defined (MSDOS)
25592 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25593 #endif
25594 }
25595
25596 /* Display the active region described by mouse_face_* according to DRAW. */
25597
25598 static void
25599 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25600 {
25601 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25602 struct frame *f = XFRAME (WINDOW_FRAME (w));
25603
25604 if (/* If window is in the process of being destroyed, don't bother
25605 to do anything. */
25606 w->current_matrix != NULL
25607 /* Don't update mouse highlight if hidden */
25608 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25609 /* Recognize when we are called to operate on rows that don't exist
25610 anymore. This can happen when a window is split. */
25611 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25612 {
25613 int phys_cursor_on_p = w->phys_cursor_on_p;
25614 struct glyph_row *row, *first, *last;
25615
25616 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25617 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25618
25619 for (row = first; row <= last && row->enabled_p; ++row)
25620 {
25621 int start_hpos, end_hpos, start_x;
25622
25623 /* For all but the first row, the highlight starts at column 0. */
25624 if (row == first)
25625 {
25626 /* R2L rows have BEG and END in reversed order, but the
25627 screen drawing geometry is always left to right. So
25628 we need to mirror the beginning and end of the
25629 highlighted area in R2L rows. */
25630 if (!row->reversed_p)
25631 {
25632 start_hpos = hlinfo->mouse_face_beg_col;
25633 start_x = hlinfo->mouse_face_beg_x;
25634 }
25635 else if (row == last)
25636 {
25637 start_hpos = hlinfo->mouse_face_end_col;
25638 start_x = hlinfo->mouse_face_end_x;
25639 }
25640 else
25641 {
25642 start_hpos = 0;
25643 start_x = 0;
25644 }
25645 }
25646 else if (row->reversed_p && row == last)
25647 {
25648 start_hpos = hlinfo->mouse_face_end_col;
25649 start_x = hlinfo->mouse_face_end_x;
25650 }
25651 else
25652 {
25653 start_hpos = 0;
25654 start_x = 0;
25655 }
25656
25657 if (row == last)
25658 {
25659 if (!row->reversed_p)
25660 end_hpos = hlinfo->mouse_face_end_col;
25661 else if (row == first)
25662 end_hpos = hlinfo->mouse_face_beg_col;
25663 else
25664 {
25665 end_hpos = row->used[TEXT_AREA];
25666 if (draw == DRAW_NORMAL_TEXT)
25667 row->fill_line_p = 1; /* Clear to end of line */
25668 }
25669 }
25670 else if (row->reversed_p && row == first)
25671 end_hpos = hlinfo->mouse_face_beg_col;
25672 else
25673 {
25674 end_hpos = row->used[TEXT_AREA];
25675 if (draw == DRAW_NORMAL_TEXT)
25676 row->fill_line_p = 1; /* Clear to end of line */
25677 }
25678
25679 if (end_hpos > start_hpos)
25680 {
25681 draw_row_with_mouse_face (w, start_x, row,
25682 start_hpos, end_hpos, draw);
25683
25684 row->mouse_face_p
25685 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25686 }
25687 }
25688
25689 #ifdef HAVE_WINDOW_SYSTEM
25690 /* When we've written over the cursor, arrange for it to
25691 be displayed again. */
25692 if (FRAME_WINDOW_P (f)
25693 && phys_cursor_on_p && !w->phys_cursor_on_p)
25694 {
25695 int hpos = w->phys_cursor.hpos;
25696
25697 /* When the window is hscrolled, cursor hpos can legitimately be
25698 out of bounds, but we draw the cursor at the corresponding
25699 window margin in that case. */
25700 if (!row->reversed_p && hpos < 0)
25701 hpos = 0;
25702 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25703 hpos = row->used[TEXT_AREA] - 1;
25704
25705 BLOCK_INPUT;
25706 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
25707 w->phys_cursor.x, w->phys_cursor.y);
25708 UNBLOCK_INPUT;
25709 }
25710 #endif /* HAVE_WINDOW_SYSTEM */
25711 }
25712
25713 #ifdef HAVE_WINDOW_SYSTEM
25714 /* Change the mouse cursor. */
25715 if (FRAME_WINDOW_P (f))
25716 {
25717 if (draw == DRAW_NORMAL_TEXT
25718 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25719 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25720 else if (draw == DRAW_MOUSE_FACE)
25721 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25722 else
25723 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25724 }
25725 #endif /* HAVE_WINDOW_SYSTEM */
25726 }
25727
25728 /* EXPORT:
25729 Clear out the mouse-highlighted active region.
25730 Redraw it un-highlighted first. Value is non-zero if mouse
25731 face was actually drawn unhighlighted. */
25732
25733 int
25734 clear_mouse_face (Mouse_HLInfo *hlinfo)
25735 {
25736 int cleared = 0;
25737
25738 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
25739 {
25740 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
25741 cleared = 1;
25742 }
25743
25744 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
25745 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
25746 hlinfo->mouse_face_window = Qnil;
25747 hlinfo->mouse_face_overlay = Qnil;
25748 return cleared;
25749 }
25750
25751 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
25752 within the mouse face on that window. */
25753 static int
25754 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
25755 {
25756 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
25757
25758 /* Quickly resolve the easy cases. */
25759 if (!(WINDOWP (hlinfo->mouse_face_window)
25760 && XWINDOW (hlinfo->mouse_face_window) == w))
25761 return 0;
25762 if (vpos < hlinfo->mouse_face_beg_row
25763 || vpos > hlinfo->mouse_face_end_row)
25764 return 0;
25765 if (vpos > hlinfo->mouse_face_beg_row
25766 && vpos < hlinfo->mouse_face_end_row)
25767 return 1;
25768
25769 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
25770 {
25771 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25772 {
25773 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
25774 return 1;
25775 }
25776 else if ((vpos == hlinfo->mouse_face_beg_row
25777 && hpos >= hlinfo->mouse_face_beg_col)
25778 || (vpos == hlinfo->mouse_face_end_row
25779 && hpos < hlinfo->mouse_face_end_col))
25780 return 1;
25781 }
25782 else
25783 {
25784 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
25785 {
25786 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
25787 return 1;
25788 }
25789 else if ((vpos == hlinfo->mouse_face_beg_row
25790 && hpos <= hlinfo->mouse_face_beg_col)
25791 || (vpos == hlinfo->mouse_face_end_row
25792 && hpos > hlinfo->mouse_face_end_col))
25793 return 1;
25794 }
25795 return 0;
25796 }
25797
25798
25799 /* EXPORT:
25800 Non-zero if physical cursor of window W is within mouse face. */
25801
25802 int
25803 cursor_in_mouse_face_p (struct window *w)
25804 {
25805 int hpos = w->phys_cursor.hpos;
25806 int vpos = w->phys_cursor.vpos;
25807 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
25808
25809 /* When the window is hscrolled, cursor hpos can legitimately be out
25810 of bounds, but we draw the cursor at the corresponding window
25811 margin in that case. */
25812 if (!row->reversed_p && hpos < 0)
25813 hpos = 0;
25814 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25815 hpos = row->used[TEXT_AREA] - 1;
25816
25817 return coords_in_mouse_face_p (w, hpos, vpos);
25818 }
25819
25820
25821 \f
25822 /* Find the glyph rows START_ROW and END_ROW of window W that display
25823 characters between buffer positions START_CHARPOS and END_CHARPOS
25824 (excluding END_CHARPOS). This is similar to row_containing_pos,
25825 but is more accurate when bidi reordering makes buffer positions
25826 change non-linearly with glyph rows. */
25827 static void
25828 rows_from_pos_range (struct window *w,
25829 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
25830 struct glyph_row **start, struct glyph_row **end)
25831 {
25832 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25833 int last_y = window_text_bottom_y (w);
25834 struct glyph_row *row;
25835
25836 *start = NULL;
25837 *end = NULL;
25838
25839 while (!first->enabled_p
25840 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
25841 first++;
25842
25843 /* Find the START row. */
25844 for (row = first;
25845 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
25846 row++)
25847 {
25848 /* A row can potentially be the START row if the range of the
25849 characters it displays intersects the range
25850 [START_CHARPOS..END_CHARPOS). */
25851 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
25852 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
25853 /* See the commentary in row_containing_pos, for the
25854 explanation of the complicated way to check whether
25855 some position is beyond the end of the characters
25856 displayed by a row. */
25857 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
25858 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
25859 && !row->ends_at_zv_p
25860 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
25861 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
25862 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
25863 && !row->ends_at_zv_p
25864 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
25865 {
25866 /* Found a candidate row. Now make sure at least one of the
25867 glyphs it displays has a charpos from the range
25868 [START_CHARPOS..END_CHARPOS).
25869
25870 This is not obvious because bidi reordering could make
25871 buffer positions of a row be 1,2,3,102,101,100, and if we
25872 want to highlight characters in [50..60), we don't want
25873 this row, even though [50..60) does intersect [1..103),
25874 the range of character positions given by the row's start
25875 and end positions. */
25876 struct glyph *g = row->glyphs[TEXT_AREA];
25877 struct glyph *e = g + row->used[TEXT_AREA];
25878
25879 while (g < e)
25880 {
25881 if ((BUFFERP (g->object) || INTEGERP (g->object))
25882 && start_charpos <= g->charpos && g->charpos < end_charpos)
25883 *start = row;
25884 g++;
25885 }
25886 if (*start)
25887 break;
25888 }
25889 }
25890
25891 /* Find the END row. */
25892 if (!*start
25893 /* If the last row is partially visible, start looking for END
25894 from that row, instead of starting from FIRST. */
25895 && !(row->enabled_p
25896 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
25897 row = first;
25898 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
25899 {
25900 struct glyph_row *next = row + 1;
25901
25902 if (!next->enabled_p
25903 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
25904 /* The first row >= START whose range of displayed characters
25905 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
25906 is the row END + 1. */
25907 || (start_charpos < MATRIX_ROW_START_CHARPOS (next)
25908 && end_charpos < MATRIX_ROW_START_CHARPOS (next))
25909 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
25910 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
25911 && !next->ends_at_zv_p
25912 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
25913 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
25914 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
25915 && !next->ends_at_zv_p
25916 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
25917 {
25918 *end = row;
25919 break;
25920 }
25921 else
25922 {
25923 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
25924 but none of the characters it displays are in the range, it is
25925 also END + 1. */
25926 struct glyph *g = next->glyphs[TEXT_AREA];
25927 struct glyph *e = g + next->used[TEXT_AREA];
25928
25929 while (g < e)
25930 {
25931 if ((BUFFERP (g->object) || INTEGERP (g->object))
25932 && start_charpos <= g->charpos && g->charpos < end_charpos)
25933 break;
25934 g++;
25935 }
25936 if (g == e)
25937 {
25938 *end = row;
25939 break;
25940 }
25941 }
25942 }
25943 }
25944
25945 /* This function sets the mouse_face_* elements of HLINFO, assuming
25946 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
25947 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
25948 for the overlay or run of text properties specifying the mouse
25949 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
25950 before-string and after-string that must also be highlighted.
25951 DISP_STRING, if non-nil, is a display string that may cover some
25952 or all of the highlighted text. */
25953
25954 static void
25955 mouse_face_from_buffer_pos (Lisp_Object window,
25956 Mouse_HLInfo *hlinfo,
25957 ptrdiff_t mouse_charpos,
25958 ptrdiff_t start_charpos,
25959 ptrdiff_t end_charpos,
25960 Lisp_Object before_string,
25961 Lisp_Object after_string,
25962 Lisp_Object disp_string)
25963 {
25964 struct window *w = XWINDOW (window);
25965 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
25966 struct glyph_row *r1, *r2;
25967 struct glyph *glyph, *end;
25968 ptrdiff_t ignore, pos;
25969 int x;
25970
25971 xassert (NILP (disp_string) || STRINGP (disp_string));
25972 xassert (NILP (before_string) || STRINGP (before_string));
25973 xassert (NILP (after_string) || STRINGP (after_string));
25974
25975 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
25976 rows_from_pos_range (w, start_charpos, end_charpos, &r1, &r2);
25977 if (r1 == NULL)
25978 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
25979 /* If the before-string or display-string contains newlines,
25980 rows_from_pos_range skips to its last row. Move back. */
25981 if (!NILP (before_string) || !NILP (disp_string))
25982 {
25983 struct glyph_row *prev;
25984 while ((prev = r1 - 1, prev >= first)
25985 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
25986 && prev->used[TEXT_AREA] > 0)
25987 {
25988 struct glyph *beg = prev->glyphs[TEXT_AREA];
25989 glyph = beg + prev->used[TEXT_AREA];
25990 while (--glyph >= beg && INTEGERP (glyph->object));
25991 if (glyph < beg
25992 || !(EQ (glyph->object, before_string)
25993 || EQ (glyph->object, disp_string)))
25994 break;
25995 r1 = prev;
25996 }
25997 }
25998 if (r2 == NULL)
25999 {
26000 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26001 hlinfo->mouse_face_past_end = 1;
26002 }
26003 else if (!NILP (after_string))
26004 {
26005 /* If the after-string has newlines, advance to its last row. */
26006 struct glyph_row *next;
26007 struct glyph_row *last
26008 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26009
26010 for (next = r2 + 1;
26011 next <= last
26012 && next->used[TEXT_AREA] > 0
26013 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26014 ++next)
26015 r2 = next;
26016 }
26017 /* The rest of the display engine assumes that mouse_face_beg_row is
26018 either above mouse_face_end_row or identical to it. But with
26019 bidi-reordered continued lines, the row for START_CHARPOS could
26020 be below the row for END_CHARPOS. If so, swap the rows and store
26021 them in correct order. */
26022 if (r1->y > r2->y)
26023 {
26024 struct glyph_row *tem = r2;
26025
26026 r2 = r1;
26027 r1 = tem;
26028 }
26029
26030 hlinfo->mouse_face_beg_y = r1->y;
26031 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26032 hlinfo->mouse_face_end_y = r2->y;
26033 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26034
26035 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26036 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26037 could be anywhere in the row and in any order. The strategy
26038 below is to find the leftmost and the rightmost glyph that
26039 belongs to either of these 3 strings, or whose position is
26040 between START_CHARPOS and END_CHARPOS, and highlight all the
26041 glyphs between those two. This may cover more than just the text
26042 between START_CHARPOS and END_CHARPOS if the range of characters
26043 strides the bidi level boundary, e.g. if the beginning is in R2L
26044 text while the end is in L2R text or vice versa. */
26045 if (!r1->reversed_p)
26046 {
26047 /* This row is in a left to right paragraph. Scan it left to
26048 right. */
26049 glyph = r1->glyphs[TEXT_AREA];
26050 end = glyph + r1->used[TEXT_AREA];
26051 x = r1->x;
26052
26053 /* Skip truncation glyphs at the start of the glyph row. */
26054 if (r1->displays_text_p)
26055 for (; glyph < end
26056 && INTEGERP (glyph->object)
26057 && glyph->charpos < 0;
26058 ++glyph)
26059 x += glyph->pixel_width;
26060
26061 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26062 or DISP_STRING, and the first glyph from buffer whose
26063 position is between START_CHARPOS and END_CHARPOS. */
26064 for (; glyph < end
26065 && !INTEGERP (glyph->object)
26066 && !EQ (glyph->object, disp_string)
26067 && !(BUFFERP (glyph->object)
26068 && (glyph->charpos >= start_charpos
26069 && glyph->charpos < end_charpos));
26070 ++glyph)
26071 {
26072 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26073 are present at buffer positions between START_CHARPOS and
26074 END_CHARPOS, or if they come from an overlay. */
26075 if (EQ (glyph->object, before_string))
26076 {
26077 pos = string_buffer_position (before_string,
26078 start_charpos);
26079 /* If pos == 0, it means before_string came from an
26080 overlay, not from a buffer position. */
26081 if (!pos || (pos >= start_charpos && pos < end_charpos))
26082 break;
26083 }
26084 else if (EQ (glyph->object, after_string))
26085 {
26086 pos = string_buffer_position (after_string, end_charpos);
26087 if (!pos || (pos >= start_charpos && pos < end_charpos))
26088 break;
26089 }
26090 x += glyph->pixel_width;
26091 }
26092 hlinfo->mouse_face_beg_x = x;
26093 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26094 }
26095 else
26096 {
26097 /* This row is in a right to left paragraph. Scan it right to
26098 left. */
26099 struct glyph *g;
26100
26101 end = r1->glyphs[TEXT_AREA] - 1;
26102 glyph = end + r1->used[TEXT_AREA];
26103
26104 /* Skip truncation glyphs at the start of the glyph row. */
26105 if (r1->displays_text_p)
26106 for (; glyph > end
26107 && INTEGERP (glyph->object)
26108 && glyph->charpos < 0;
26109 --glyph)
26110 ;
26111
26112 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26113 or DISP_STRING, and the first glyph from buffer whose
26114 position is between START_CHARPOS and END_CHARPOS. */
26115 for (; glyph > end
26116 && !INTEGERP (glyph->object)
26117 && !EQ (glyph->object, disp_string)
26118 && !(BUFFERP (glyph->object)
26119 && (glyph->charpos >= start_charpos
26120 && glyph->charpos < end_charpos));
26121 --glyph)
26122 {
26123 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26124 are present at buffer positions between START_CHARPOS and
26125 END_CHARPOS, or if they come from an overlay. */
26126 if (EQ (glyph->object, before_string))
26127 {
26128 pos = string_buffer_position (before_string, start_charpos);
26129 /* If pos == 0, it means before_string came from an
26130 overlay, not from a buffer position. */
26131 if (!pos || (pos >= start_charpos && pos < end_charpos))
26132 break;
26133 }
26134 else if (EQ (glyph->object, after_string))
26135 {
26136 pos = string_buffer_position (after_string, end_charpos);
26137 if (!pos || (pos >= start_charpos && pos < end_charpos))
26138 break;
26139 }
26140 }
26141
26142 glyph++; /* first glyph to the right of the highlighted area */
26143 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26144 x += g->pixel_width;
26145 hlinfo->mouse_face_beg_x = x;
26146 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26147 }
26148
26149 /* If the highlight ends in a different row, compute GLYPH and END
26150 for the end row. Otherwise, reuse the values computed above for
26151 the row where the highlight begins. */
26152 if (r2 != r1)
26153 {
26154 if (!r2->reversed_p)
26155 {
26156 glyph = r2->glyphs[TEXT_AREA];
26157 end = glyph + r2->used[TEXT_AREA];
26158 x = r2->x;
26159 }
26160 else
26161 {
26162 end = r2->glyphs[TEXT_AREA] - 1;
26163 glyph = end + r2->used[TEXT_AREA];
26164 }
26165 }
26166
26167 if (!r2->reversed_p)
26168 {
26169 /* Skip truncation and continuation glyphs near the end of the
26170 row, and also blanks and stretch glyphs inserted by
26171 extend_face_to_end_of_line. */
26172 while (end > glyph
26173 && INTEGERP ((end - 1)->object))
26174 --end;
26175 /* Scan the rest of the glyph row from the end, looking for the
26176 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26177 DISP_STRING, or whose position is between START_CHARPOS
26178 and END_CHARPOS */
26179 for (--end;
26180 end > glyph
26181 && !INTEGERP (end->object)
26182 && !EQ (end->object, disp_string)
26183 && !(BUFFERP (end->object)
26184 && (end->charpos >= start_charpos
26185 && end->charpos < end_charpos));
26186 --end)
26187 {
26188 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26189 are present at buffer positions between START_CHARPOS and
26190 END_CHARPOS, or if they come from an overlay. */
26191 if (EQ (end->object, before_string))
26192 {
26193 pos = string_buffer_position (before_string, start_charpos);
26194 if (!pos || (pos >= start_charpos && pos < end_charpos))
26195 break;
26196 }
26197 else if (EQ (end->object, after_string))
26198 {
26199 pos = string_buffer_position (after_string, end_charpos);
26200 if (!pos || (pos >= start_charpos && pos < end_charpos))
26201 break;
26202 }
26203 }
26204 /* Find the X coordinate of the last glyph to be highlighted. */
26205 for (; glyph <= end; ++glyph)
26206 x += glyph->pixel_width;
26207
26208 hlinfo->mouse_face_end_x = x;
26209 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26210 }
26211 else
26212 {
26213 /* Skip truncation and continuation glyphs near the end of the
26214 row, and also blanks and stretch glyphs inserted by
26215 extend_face_to_end_of_line. */
26216 x = r2->x;
26217 end++;
26218 while (end < glyph
26219 && INTEGERP (end->object))
26220 {
26221 x += end->pixel_width;
26222 ++end;
26223 }
26224 /* Scan the rest of the glyph row from the end, looking for the
26225 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26226 DISP_STRING, or whose position is between START_CHARPOS
26227 and END_CHARPOS */
26228 for ( ;
26229 end < glyph
26230 && !INTEGERP (end->object)
26231 && !EQ (end->object, disp_string)
26232 && !(BUFFERP (end->object)
26233 && (end->charpos >= start_charpos
26234 && end->charpos < end_charpos));
26235 ++end)
26236 {
26237 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26238 are present at buffer positions between START_CHARPOS and
26239 END_CHARPOS, or if they come from an overlay. */
26240 if (EQ (end->object, before_string))
26241 {
26242 pos = string_buffer_position (before_string, start_charpos);
26243 if (!pos || (pos >= start_charpos && pos < end_charpos))
26244 break;
26245 }
26246 else if (EQ (end->object, after_string))
26247 {
26248 pos = string_buffer_position (after_string, end_charpos);
26249 if (!pos || (pos >= start_charpos && pos < end_charpos))
26250 break;
26251 }
26252 x += end->pixel_width;
26253 }
26254 hlinfo->mouse_face_end_x = x;
26255 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26256 }
26257
26258 hlinfo->mouse_face_window = window;
26259 hlinfo->mouse_face_face_id
26260 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26261 mouse_charpos + 1,
26262 !hlinfo->mouse_face_hidden, -1);
26263 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26264 }
26265
26266 /* The following function is not used anymore (replaced with
26267 mouse_face_from_string_pos), but I leave it here for the time
26268 being, in case someone would. */
26269
26270 #if 0 /* not used */
26271
26272 /* Find the position of the glyph for position POS in OBJECT in
26273 window W's current matrix, and return in *X, *Y the pixel
26274 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26275
26276 RIGHT_P non-zero means return the position of the right edge of the
26277 glyph, RIGHT_P zero means return the left edge position.
26278
26279 If no glyph for POS exists in the matrix, return the position of
26280 the glyph with the next smaller position that is in the matrix, if
26281 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26282 exists in the matrix, return the position of the glyph with the
26283 next larger position in OBJECT.
26284
26285 Value is non-zero if a glyph was found. */
26286
26287 static int
26288 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26289 int *hpos, int *vpos, int *x, int *y, int right_p)
26290 {
26291 int yb = window_text_bottom_y (w);
26292 struct glyph_row *r;
26293 struct glyph *best_glyph = NULL;
26294 struct glyph_row *best_row = NULL;
26295 int best_x = 0;
26296
26297 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26298 r->enabled_p && r->y < yb;
26299 ++r)
26300 {
26301 struct glyph *g = r->glyphs[TEXT_AREA];
26302 struct glyph *e = g + r->used[TEXT_AREA];
26303 int gx;
26304
26305 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26306 if (EQ (g->object, object))
26307 {
26308 if (g->charpos == pos)
26309 {
26310 best_glyph = g;
26311 best_x = gx;
26312 best_row = r;
26313 goto found;
26314 }
26315 else if (best_glyph == NULL
26316 || ((eabs (g->charpos - pos)
26317 < eabs (best_glyph->charpos - pos))
26318 && (right_p
26319 ? g->charpos < pos
26320 : g->charpos > pos)))
26321 {
26322 best_glyph = g;
26323 best_x = gx;
26324 best_row = r;
26325 }
26326 }
26327 }
26328
26329 found:
26330
26331 if (best_glyph)
26332 {
26333 *x = best_x;
26334 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26335
26336 if (right_p)
26337 {
26338 *x += best_glyph->pixel_width;
26339 ++*hpos;
26340 }
26341
26342 *y = best_row->y;
26343 *vpos = best_row - w->current_matrix->rows;
26344 }
26345
26346 return best_glyph != NULL;
26347 }
26348 #endif /* not used */
26349
26350 /* Find the positions of the first and the last glyphs in window W's
26351 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26352 (assumed to be a string), and return in HLINFO's mouse_face_*
26353 members the pixel and column/row coordinates of those glyphs. */
26354
26355 static void
26356 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26357 Lisp_Object object,
26358 ptrdiff_t startpos, ptrdiff_t endpos)
26359 {
26360 int yb = window_text_bottom_y (w);
26361 struct glyph_row *r;
26362 struct glyph *g, *e;
26363 int gx;
26364 int found = 0;
26365
26366 /* Find the glyph row with at least one position in the range
26367 [STARTPOS..ENDPOS], and the first glyph in that row whose
26368 position belongs to that range. */
26369 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26370 r->enabled_p && r->y < yb;
26371 ++r)
26372 {
26373 if (!r->reversed_p)
26374 {
26375 g = r->glyphs[TEXT_AREA];
26376 e = g + r->used[TEXT_AREA];
26377 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26378 if (EQ (g->object, object)
26379 && startpos <= g->charpos && g->charpos <= endpos)
26380 {
26381 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26382 hlinfo->mouse_face_beg_y = r->y;
26383 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26384 hlinfo->mouse_face_beg_x = gx;
26385 found = 1;
26386 break;
26387 }
26388 }
26389 else
26390 {
26391 struct glyph *g1;
26392
26393 e = r->glyphs[TEXT_AREA];
26394 g = e + r->used[TEXT_AREA];
26395 for ( ; g > e; --g)
26396 if (EQ ((g-1)->object, object)
26397 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26398 {
26399 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26400 hlinfo->mouse_face_beg_y = r->y;
26401 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26402 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26403 gx += g1->pixel_width;
26404 hlinfo->mouse_face_beg_x = gx;
26405 found = 1;
26406 break;
26407 }
26408 }
26409 if (found)
26410 break;
26411 }
26412
26413 if (!found)
26414 return;
26415
26416 /* Starting with the next row, look for the first row which does NOT
26417 include any glyphs whose positions are in the range. */
26418 for (++r; r->enabled_p && r->y < yb; ++r)
26419 {
26420 g = r->glyphs[TEXT_AREA];
26421 e = g + r->used[TEXT_AREA];
26422 found = 0;
26423 for ( ; g < e; ++g)
26424 if (EQ (g->object, object)
26425 && startpos <= g->charpos && g->charpos <= endpos)
26426 {
26427 found = 1;
26428 break;
26429 }
26430 if (!found)
26431 break;
26432 }
26433
26434 /* The highlighted region ends on the previous row. */
26435 r--;
26436
26437 /* Set the end row and its vertical pixel coordinate. */
26438 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26439 hlinfo->mouse_face_end_y = r->y;
26440
26441 /* Compute and set the end column and the end column's horizontal
26442 pixel coordinate. */
26443 if (!r->reversed_p)
26444 {
26445 g = r->glyphs[TEXT_AREA];
26446 e = g + r->used[TEXT_AREA];
26447 for ( ; e > g; --e)
26448 if (EQ ((e-1)->object, object)
26449 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26450 break;
26451 hlinfo->mouse_face_end_col = e - g;
26452
26453 for (gx = r->x; g < e; ++g)
26454 gx += g->pixel_width;
26455 hlinfo->mouse_face_end_x = gx;
26456 }
26457 else
26458 {
26459 e = r->glyphs[TEXT_AREA];
26460 g = e + r->used[TEXT_AREA];
26461 for (gx = r->x ; e < g; ++e)
26462 {
26463 if (EQ (e->object, object)
26464 && startpos <= e->charpos && e->charpos <= endpos)
26465 break;
26466 gx += e->pixel_width;
26467 }
26468 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26469 hlinfo->mouse_face_end_x = gx;
26470 }
26471 }
26472
26473 #ifdef HAVE_WINDOW_SYSTEM
26474
26475 /* See if position X, Y is within a hot-spot of an image. */
26476
26477 static int
26478 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26479 {
26480 if (!CONSP (hot_spot))
26481 return 0;
26482
26483 if (EQ (XCAR (hot_spot), Qrect))
26484 {
26485 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26486 Lisp_Object rect = XCDR (hot_spot);
26487 Lisp_Object tem;
26488 if (!CONSP (rect))
26489 return 0;
26490 if (!CONSP (XCAR (rect)))
26491 return 0;
26492 if (!CONSP (XCDR (rect)))
26493 return 0;
26494 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26495 return 0;
26496 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26497 return 0;
26498 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26499 return 0;
26500 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26501 return 0;
26502 return 1;
26503 }
26504 else if (EQ (XCAR (hot_spot), Qcircle))
26505 {
26506 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26507 Lisp_Object circ = XCDR (hot_spot);
26508 Lisp_Object lr, lx0, ly0;
26509 if (CONSP (circ)
26510 && CONSP (XCAR (circ))
26511 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26512 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26513 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26514 {
26515 double r = XFLOATINT (lr);
26516 double dx = XINT (lx0) - x;
26517 double dy = XINT (ly0) - y;
26518 return (dx * dx + dy * dy <= r * r);
26519 }
26520 }
26521 else if (EQ (XCAR (hot_spot), Qpoly))
26522 {
26523 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26524 if (VECTORP (XCDR (hot_spot)))
26525 {
26526 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26527 Lisp_Object *poly = v->contents;
26528 ptrdiff_t n = v->header.size;
26529 ptrdiff_t i;
26530 int inside = 0;
26531 Lisp_Object lx, ly;
26532 int x0, y0;
26533
26534 /* Need an even number of coordinates, and at least 3 edges. */
26535 if (n < 6 || n & 1)
26536 return 0;
26537
26538 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26539 If count is odd, we are inside polygon. Pixels on edges
26540 may or may not be included depending on actual geometry of the
26541 polygon. */
26542 if ((lx = poly[n-2], !INTEGERP (lx))
26543 || (ly = poly[n-1], !INTEGERP (lx)))
26544 return 0;
26545 x0 = XINT (lx), y0 = XINT (ly);
26546 for (i = 0; i < n; i += 2)
26547 {
26548 int x1 = x0, y1 = y0;
26549 if ((lx = poly[i], !INTEGERP (lx))
26550 || (ly = poly[i+1], !INTEGERP (ly)))
26551 return 0;
26552 x0 = XINT (lx), y0 = XINT (ly);
26553
26554 /* Does this segment cross the X line? */
26555 if (x0 >= x)
26556 {
26557 if (x1 >= x)
26558 continue;
26559 }
26560 else if (x1 < x)
26561 continue;
26562 if (y > y0 && y > y1)
26563 continue;
26564 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26565 inside = !inside;
26566 }
26567 return inside;
26568 }
26569 }
26570 return 0;
26571 }
26572
26573 Lisp_Object
26574 find_hot_spot (Lisp_Object map, int x, int y)
26575 {
26576 while (CONSP (map))
26577 {
26578 if (CONSP (XCAR (map))
26579 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26580 return XCAR (map);
26581 map = XCDR (map);
26582 }
26583
26584 return Qnil;
26585 }
26586
26587 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26588 3, 3, 0,
26589 doc: /* Lookup in image map MAP coordinates X and Y.
26590 An image map is an alist where each element has the format (AREA ID PLIST).
26591 An AREA is specified as either a rectangle, a circle, or a polygon:
26592 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26593 pixel coordinates of the upper left and bottom right corners.
26594 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26595 and the radius of the circle; r may be a float or integer.
26596 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26597 vector describes one corner in the polygon.
26598 Returns the alist element for the first matching AREA in MAP. */)
26599 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26600 {
26601 if (NILP (map))
26602 return Qnil;
26603
26604 CHECK_NUMBER (x);
26605 CHECK_NUMBER (y);
26606
26607 return find_hot_spot (map,
26608 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
26609 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
26610 }
26611
26612
26613 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26614 static void
26615 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26616 {
26617 /* Do not change cursor shape while dragging mouse. */
26618 if (!NILP (do_mouse_tracking))
26619 return;
26620
26621 if (!NILP (pointer))
26622 {
26623 if (EQ (pointer, Qarrow))
26624 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26625 else if (EQ (pointer, Qhand))
26626 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
26627 else if (EQ (pointer, Qtext))
26628 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26629 else if (EQ (pointer, intern ("hdrag")))
26630 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26631 #ifdef HAVE_X_WINDOWS
26632 else if (EQ (pointer, intern ("vdrag")))
26633 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
26634 #endif
26635 else if (EQ (pointer, intern ("hourglass")))
26636 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
26637 else if (EQ (pointer, Qmodeline))
26638 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
26639 else
26640 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26641 }
26642
26643 if (cursor != No_Cursor)
26644 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26645 }
26646
26647 #endif /* HAVE_WINDOW_SYSTEM */
26648
26649 /* Take proper action when mouse has moved to the mode or header line
26650 or marginal area AREA of window W, x-position X and y-position Y.
26651 X is relative to the start of the text display area of W, so the
26652 width of bitmap areas and scroll bars must be subtracted to get a
26653 position relative to the start of the mode line. */
26654
26655 static void
26656 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
26657 enum window_part area)
26658 {
26659 struct window *w = XWINDOW (window);
26660 struct frame *f = XFRAME (w->frame);
26661 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26662 #ifdef HAVE_WINDOW_SYSTEM
26663 Display_Info *dpyinfo;
26664 #endif
26665 Cursor cursor = No_Cursor;
26666 Lisp_Object pointer = Qnil;
26667 int dx, dy, width, height;
26668 ptrdiff_t charpos;
26669 Lisp_Object string, object = Qnil;
26670 Lisp_Object pos, help;
26671
26672 Lisp_Object mouse_face;
26673 int original_x_pixel = x;
26674 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26675 struct glyph_row *row;
26676
26677 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26678 {
26679 int x0;
26680 struct glyph *end;
26681
26682 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26683 returns them in row/column units! */
26684 string = mode_line_string (w, area, &x, &y, &charpos,
26685 &object, &dx, &dy, &width, &height);
26686
26687 row = (area == ON_MODE_LINE
26688 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
26689 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
26690
26691 /* Find the glyph under the mouse pointer. */
26692 if (row->mode_line_p && row->enabled_p)
26693 {
26694 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
26695 end = glyph + row->used[TEXT_AREA];
26696
26697 for (x0 = original_x_pixel;
26698 glyph < end && x0 >= glyph->pixel_width;
26699 ++glyph)
26700 x0 -= glyph->pixel_width;
26701
26702 if (glyph >= end)
26703 glyph = NULL;
26704 }
26705 }
26706 else
26707 {
26708 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
26709 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
26710 returns them in row/column units! */
26711 string = marginal_area_string (w, area, &x, &y, &charpos,
26712 &object, &dx, &dy, &width, &height);
26713 }
26714
26715 help = Qnil;
26716
26717 #ifdef HAVE_WINDOW_SYSTEM
26718 if (IMAGEP (object))
26719 {
26720 Lisp_Object image_map, hotspot;
26721 if ((image_map = Fplist_get (XCDR (object), QCmap),
26722 !NILP (image_map))
26723 && (hotspot = find_hot_spot (image_map, dx, dy),
26724 CONSP (hotspot))
26725 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
26726 {
26727 Lisp_Object plist;
26728
26729 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
26730 If so, we could look for mouse-enter, mouse-leave
26731 properties in PLIST (and do something...). */
26732 hotspot = XCDR (hotspot);
26733 if (CONSP (hotspot)
26734 && (plist = XCAR (hotspot), CONSP (plist)))
26735 {
26736 pointer = Fplist_get (plist, Qpointer);
26737 if (NILP (pointer))
26738 pointer = Qhand;
26739 help = Fplist_get (plist, Qhelp_echo);
26740 if (!NILP (help))
26741 {
26742 help_echo_string = help;
26743 /* Is this correct? ++kfs */
26744 XSETWINDOW (help_echo_window, w);
26745 help_echo_object = w->buffer;
26746 help_echo_pos = charpos;
26747 }
26748 }
26749 }
26750 if (NILP (pointer))
26751 pointer = Fplist_get (XCDR (object), QCpointer);
26752 }
26753 #endif /* HAVE_WINDOW_SYSTEM */
26754
26755 if (STRINGP (string))
26756 {
26757 pos = make_number (charpos);
26758 /* If we're on a string with `help-echo' text property, arrange
26759 for the help to be displayed. This is done by setting the
26760 global variable help_echo_string to the help string. */
26761 if (NILP (help))
26762 {
26763 help = Fget_text_property (pos, Qhelp_echo, string);
26764 if (!NILP (help))
26765 {
26766 help_echo_string = help;
26767 XSETWINDOW (help_echo_window, w);
26768 help_echo_object = string;
26769 help_echo_pos = charpos;
26770 }
26771 }
26772
26773 #ifdef HAVE_WINDOW_SYSTEM
26774 if (FRAME_WINDOW_P (f))
26775 {
26776 dpyinfo = FRAME_X_DISPLAY_INFO (f);
26777 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26778 if (NILP (pointer))
26779 pointer = Fget_text_property (pos, Qpointer, string);
26780
26781 /* Change the mouse pointer according to what is under X/Y. */
26782 if (NILP (pointer)
26783 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
26784 {
26785 Lisp_Object map;
26786 map = Fget_text_property (pos, Qlocal_map, string);
26787 if (!KEYMAPP (map))
26788 map = Fget_text_property (pos, Qkeymap, string);
26789 if (!KEYMAPP (map))
26790 cursor = dpyinfo->vertical_scroll_bar_cursor;
26791 }
26792 }
26793 #endif
26794
26795 /* Change the mouse face according to what is under X/Y. */
26796 mouse_face = Fget_text_property (pos, Qmouse_face, string);
26797 if (!NILP (mouse_face)
26798 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26799 && glyph)
26800 {
26801 Lisp_Object b, e;
26802
26803 struct glyph * tmp_glyph;
26804
26805 int gpos;
26806 int gseq_length;
26807 int total_pixel_width;
26808 ptrdiff_t begpos, endpos, ignore;
26809
26810 int vpos, hpos;
26811
26812 b = Fprevious_single_property_change (make_number (charpos + 1),
26813 Qmouse_face, string, Qnil);
26814 if (NILP (b))
26815 begpos = 0;
26816 else
26817 begpos = XINT (b);
26818
26819 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
26820 if (NILP (e))
26821 endpos = SCHARS (string);
26822 else
26823 endpos = XINT (e);
26824
26825 /* Calculate the glyph position GPOS of GLYPH in the
26826 displayed string, relative to the beginning of the
26827 highlighted part of the string.
26828
26829 Note: GPOS is different from CHARPOS. CHARPOS is the
26830 position of GLYPH in the internal string object. A mode
26831 line string format has structures which are converted to
26832 a flattened string by the Emacs Lisp interpreter. The
26833 internal string is an element of those structures. The
26834 displayed string is the flattened string. */
26835 tmp_glyph = row_start_glyph;
26836 while (tmp_glyph < glyph
26837 && (!(EQ (tmp_glyph->object, glyph->object)
26838 && begpos <= tmp_glyph->charpos
26839 && tmp_glyph->charpos < endpos)))
26840 tmp_glyph++;
26841 gpos = glyph - tmp_glyph;
26842
26843 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
26844 the highlighted part of the displayed string to which
26845 GLYPH belongs. Note: GSEQ_LENGTH is different from
26846 SCHARS (STRING), because the latter returns the length of
26847 the internal string. */
26848 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
26849 tmp_glyph > glyph
26850 && (!(EQ (tmp_glyph->object, glyph->object)
26851 && begpos <= tmp_glyph->charpos
26852 && tmp_glyph->charpos < endpos));
26853 tmp_glyph--)
26854 ;
26855 gseq_length = gpos + (tmp_glyph - glyph) + 1;
26856
26857 /* Calculate the total pixel width of all the glyphs between
26858 the beginning of the highlighted area and GLYPH. */
26859 total_pixel_width = 0;
26860 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
26861 total_pixel_width += tmp_glyph->pixel_width;
26862
26863 /* Pre calculation of re-rendering position. Note: X is in
26864 column units here, after the call to mode_line_string or
26865 marginal_area_string. */
26866 hpos = x - gpos;
26867 vpos = (area == ON_MODE_LINE
26868 ? (w->current_matrix)->nrows - 1
26869 : 0);
26870
26871 /* If GLYPH's position is included in the region that is
26872 already drawn in mouse face, we have nothing to do. */
26873 if ( EQ (window, hlinfo->mouse_face_window)
26874 && (!row->reversed_p
26875 ? (hlinfo->mouse_face_beg_col <= hpos
26876 && hpos < hlinfo->mouse_face_end_col)
26877 /* In R2L rows we swap BEG and END, see below. */
26878 : (hlinfo->mouse_face_end_col <= hpos
26879 && hpos < hlinfo->mouse_face_beg_col))
26880 && hlinfo->mouse_face_beg_row == vpos )
26881 return;
26882
26883 if (clear_mouse_face (hlinfo))
26884 cursor = No_Cursor;
26885
26886 if (!row->reversed_p)
26887 {
26888 hlinfo->mouse_face_beg_col = hpos;
26889 hlinfo->mouse_face_beg_x = original_x_pixel
26890 - (total_pixel_width + dx);
26891 hlinfo->mouse_face_end_col = hpos + gseq_length;
26892 hlinfo->mouse_face_end_x = 0;
26893 }
26894 else
26895 {
26896 /* In R2L rows, show_mouse_face expects BEG and END
26897 coordinates to be swapped. */
26898 hlinfo->mouse_face_end_col = hpos;
26899 hlinfo->mouse_face_end_x = original_x_pixel
26900 - (total_pixel_width + dx);
26901 hlinfo->mouse_face_beg_col = hpos + gseq_length;
26902 hlinfo->mouse_face_beg_x = 0;
26903 }
26904
26905 hlinfo->mouse_face_beg_row = vpos;
26906 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
26907 hlinfo->mouse_face_beg_y = 0;
26908 hlinfo->mouse_face_end_y = 0;
26909 hlinfo->mouse_face_past_end = 0;
26910 hlinfo->mouse_face_window = window;
26911
26912 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
26913 charpos,
26914 0, 0, 0,
26915 &ignore,
26916 glyph->face_id,
26917 1);
26918 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26919
26920 if (NILP (pointer))
26921 pointer = Qhand;
26922 }
26923 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
26924 clear_mouse_face (hlinfo);
26925 }
26926 #ifdef HAVE_WINDOW_SYSTEM
26927 if (FRAME_WINDOW_P (f))
26928 define_frame_cursor1 (f, cursor, pointer);
26929 #endif
26930 }
26931
26932
26933 /* EXPORT:
26934 Take proper action when the mouse has moved to position X, Y on
26935 frame F as regards highlighting characters that have mouse-face
26936 properties. Also de-highlighting chars where the mouse was before.
26937 X and Y can be negative or out of range. */
26938
26939 void
26940 note_mouse_highlight (struct frame *f, int x, int y)
26941 {
26942 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26943 enum window_part part = ON_NOTHING;
26944 Lisp_Object window;
26945 struct window *w;
26946 Cursor cursor = No_Cursor;
26947 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
26948 struct buffer *b;
26949
26950 /* When a menu is active, don't highlight because this looks odd. */
26951 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
26952 if (popup_activated ())
26953 return;
26954 #endif
26955
26956 if (NILP (Vmouse_highlight)
26957 || !f->glyphs_initialized_p
26958 || f->pointer_invisible)
26959 return;
26960
26961 hlinfo->mouse_face_mouse_x = x;
26962 hlinfo->mouse_face_mouse_y = y;
26963 hlinfo->mouse_face_mouse_frame = f;
26964
26965 if (hlinfo->mouse_face_defer)
26966 return;
26967
26968 if (gc_in_progress)
26969 {
26970 hlinfo->mouse_face_deferred_gc = 1;
26971 return;
26972 }
26973
26974 /* Which window is that in? */
26975 window = window_from_coordinates (f, x, y, &part, 1);
26976
26977 /* If displaying active text in another window, clear that. */
26978 if (! EQ (window, hlinfo->mouse_face_window)
26979 /* Also clear if we move out of text area in same window. */
26980 || (!NILP (hlinfo->mouse_face_window)
26981 && !NILP (window)
26982 && part != ON_TEXT
26983 && part != ON_MODE_LINE
26984 && part != ON_HEADER_LINE))
26985 clear_mouse_face (hlinfo);
26986
26987 /* Not on a window -> return. */
26988 if (!WINDOWP (window))
26989 return;
26990
26991 /* Reset help_echo_string. It will get recomputed below. */
26992 help_echo_string = Qnil;
26993
26994 /* Convert to window-relative pixel coordinates. */
26995 w = XWINDOW (window);
26996 frame_to_window_pixel_xy (w, &x, &y);
26997
26998 #ifdef HAVE_WINDOW_SYSTEM
26999 /* Handle tool-bar window differently since it doesn't display a
27000 buffer. */
27001 if (EQ (window, f->tool_bar_window))
27002 {
27003 note_tool_bar_highlight (f, x, y);
27004 return;
27005 }
27006 #endif
27007
27008 /* Mouse is on the mode, header line or margin? */
27009 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27010 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27011 {
27012 note_mode_line_or_margin_highlight (window, x, y, part);
27013 return;
27014 }
27015
27016 #ifdef HAVE_WINDOW_SYSTEM
27017 if (part == ON_VERTICAL_BORDER)
27018 {
27019 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27020 help_echo_string = build_string ("drag-mouse-1: resize");
27021 }
27022 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27023 || part == ON_SCROLL_BAR)
27024 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27025 else
27026 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27027 #endif
27028
27029 /* Are we in a window whose display is up to date?
27030 And verify the buffer's text has not changed. */
27031 b = XBUFFER (w->buffer);
27032 if (part == ON_TEXT
27033 && EQ (w->window_end_valid, w->buffer)
27034 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
27035 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
27036 {
27037 int hpos, vpos, dx, dy, area = LAST_AREA;
27038 ptrdiff_t pos;
27039 struct glyph *glyph;
27040 Lisp_Object object;
27041 Lisp_Object mouse_face = Qnil, position;
27042 Lisp_Object *overlay_vec = NULL;
27043 ptrdiff_t i, noverlays;
27044 struct buffer *obuf;
27045 ptrdiff_t obegv, ozv;
27046 int same_region;
27047
27048 /* Find the glyph under X/Y. */
27049 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27050
27051 #ifdef HAVE_WINDOW_SYSTEM
27052 /* Look for :pointer property on image. */
27053 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27054 {
27055 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27056 if (img != NULL && IMAGEP (img->spec))
27057 {
27058 Lisp_Object image_map, hotspot;
27059 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27060 !NILP (image_map))
27061 && (hotspot = find_hot_spot (image_map,
27062 glyph->slice.img.x + dx,
27063 glyph->slice.img.y + dy),
27064 CONSP (hotspot))
27065 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27066 {
27067 Lisp_Object plist;
27068
27069 /* Could check XCAR (hotspot) to see if we enter/leave
27070 this hot-spot.
27071 If so, we could look for mouse-enter, mouse-leave
27072 properties in PLIST (and do something...). */
27073 hotspot = XCDR (hotspot);
27074 if (CONSP (hotspot)
27075 && (plist = XCAR (hotspot), CONSP (plist)))
27076 {
27077 pointer = Fplist_get (plist, Qpointer);
27078 if (NILP (pointer))
27079 pointer = Qhand;
27080 help_echo_string = Fplist_get (plist, Qhelp_echo);
27081 if (!NILP (help_echo_string))
27082 {
27083 help_echo_window = window;
27084 help_echo_object = glyph->object;
27085 help_echo_pos = glyph->charpos;
27086 }
27087 }
27088 }
27089 if (NILP (pointer))
27090 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27091 }
27092 }
27093 #endif /* HAVE_WINDOW_SYSTEM */
27094
27095 /* Clear mouse face if X/Y not over text. */
27096 if (glyph == NULL
27097 || area != TEXT_AREA
27098 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27099 /* Glyph's OBJECT is an integer for glyphs inserted by the
27100 display engine for its internal purposes, like truncation
27101 and continuation glyphs and blanks beyond the end of
27102 line's text on text terminals. If we are over such a
27103 glyph, we are not over any text. */
27104 || INTEGERP (glyph->object)
27105 /* R2L rows have a stretch glyph at their front, which
27106 stands for no text, whereas L2R rows have no glyphs at
27107 all beyond the end of text. Treat such stretch glyphs
27108 like we do with NULL glyphs in L2R rows. */
27109 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27110 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27111 && glyph->type == STRETCH_GLYPH
27112 && glyph->avoid_cursor_p))
27113 {
27114 if (clear_mouse_face (hlinfo))
27115 cursor = No_Cursor;
27116 #ifdef HAVE_WINDOW_SYSTEM
27117 if (FRAME_WINDOW_P (f) && NILP (pointer))
27118 {
27119 if (area != TEXT_AREA)
27120 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27121 else
27122 pointer = Vvoid_text_area_pointer;
27123 }
27124 #endif
27125 goto set_cursor;
27126 }
27127
27128 pos = glyph->charpos;
27129 object = glyph->object;
27130 if (!STRINGP (object) && !BUFFERP (object))
27131 goto set_cursor;
27132
27133 /* If we get an out-of-range value, return now; avoid an error. */
27134 if (BUFFERP (object) && pos > BUF_Z (b))
27135 goto set_cursor;
27136
27137 /* Make the window's buffer temporarily current for
27138 overlays_at and compute_char_face. */
27139 obuf = current_buffer;
27140 current_buffer = b;
27141 obegv = BEGV;
27142 ozv = ZV;
27143 BEGV = BEG;
27144 ZV = Z;
27145
27146 /* Is this char mouse-active or does it have help-echo? */
27147 position = make_number (pos);
27148
27149 if (BUFFERP (object))
27150 {
27151 /* Put all the overlays we want in a vector in overlay_vec. */
27152 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27153 /* Sort overlays into increasing priority order. */
27154 noverlays = sort_overlays (overlay_vec, noverlays, w);
27155 }
27156 else
27157 noverlays = 0;
27158
27159 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27160
27161 if (same_region)
27162 cursor = No_Cursor;
27163
27164 /* Check mouse-face highlighting. */
27165 if (! same_region
27166 /* If there exists an overlay with mouse-face overlapping
27167 the one we are currently highlighting, we have to
27168 check if we enter the overlapping overlay, and then
27169 highlight only that. */
27170 || (OVERLAYP (hlinfo->mouse_face_overlay)
27171 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27172 {
27173 /* Find the highest priority overlay with a mouse-face. */
27174 Lisp_Object overlay = Qnil;
27175 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27176 {
27177 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27178 if (!NILP (mouse_face))
27179 overlay = overlay_vec[i];
27180 }
27181
27182 /* If we're highlighting the same overlay as before, there's
27183 no need to do that again. */
27184 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27185 goto check_help_echo;
27186 hlinfo->mouse_face_overlay = overlay;
27187
27188 /* Clear the display of the old active region, if any. */
27189 if (clear_mouse_face (hlinfo))
27190 cursor = No_Cursor;
27191
27192 /* If no overlay applies, get a text property. */
27193 if (NILP (overlay))
27194 mouse_face = Fget_text_property (position, Qmouse_face, object);
27195
27196 /* Next, compute the bounds of the mouse highlighting and
27197 display it. */
27198 if (!NILP (mouse_face) && STRINGP (object))
27199 {
27200 /* The mouse-highlighting comes from a display string
27201 with a mouse-face. */
27202 Lisp_Object s, e;
27203 ptrdiff_t ignore;
27204
27205 s = Fprevious_single_property_change
27206 (make_number (pos + 1), Qmouse_face, object, Qnil);
27207 e = Fnext_single_property_change
27208 (position, Qmouse_face, object, Qnil);
27209 if (NILP (s))
27210 s = make_number (0);
27211 if (NILP (e))
27212 e = make_number (SCHARS (object) - 1);
27213 mouse_face_from_string_pos (w, hlinfo, object,
27214 XINT (s), XINT (e));
27215 hlinfo->mouse_face_past_end = 0;
27216 hlinfo->mouse_face_window = window;
27217 hlinfo->mouse_face_face_id
27218 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27219 glyph->face_id, 1);
27220 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27221 cursor = No_Cursor;
27222 }
27223 else
27224 {
27225 /* The mouse-highlighting, if any, comes from an overlay
27226 or text property in the buffer. */
27227 Lisp_Object buffer IF_LINT (= Qnil);
27228 Lisp_Object disp_string IF_LINT (= Qnil);
27229
27230 if (STRINGP (object))
27231 {
27232 /* If we are on a display string with no mouse-face,
27233 check if the text under it has one. */
27234 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27235 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27236 pos = string_buffer_position (object, start);
27237 if (pos > 0)
27238 {
27239 mouse_face = get_char_property_and_overlay
27240 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27241 buffer = w->buffer;
27242 disp_string = object;
27243 }
27244 }
27245 else
27246 {
27247 buffer = object;
27248 disp_string = Qnil;
27249 }
27250
27251 if (!NILP (mouse_face))
27252 {
27253 Lisp_Object before, after;
27254 Lisp_Object before_string, after_string;
27255 /* To correctly find the limits of mouse highlight
27256 in a bidi-reordered buffer, we must not use the
27257 optimization of limiting the search in
27258 previous-single-property-change and
27259 next-single-property-change, because
27260 rows_from_pos_range needs the real start and end
27261 positions to DTRT in this case. That's because
27262 the first row visible in a window does not
27263 necessarily display the character whose position
27264 is the smallest. */
27265 Lisp_Object lim1 =
27266 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27267 ? Fmarker_position (w->start)
27268 : Qnil;
27269 Lisp_Object lim2 =
27270 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27271 ? make_number (BUF_Z (XBUFFER (buffer))
27272 - XFASTINT (w->window_end_pos))
27273 : Qnil;
27274
27275 if (NILP (overlay))
27276 {
27277 /* Handle the text property case. */
27278 before = Fprevious_single_property_change
27279 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27280 after = Fnext_single_property_change
27281 (make_number (pos), Qmouse_face, buffer, lim2);
27282 before_string = after_string = Qnil;
27283 }
27284 else
27285 {
27286 /* Handle the overlay case. */
27287 before = Foverlay_start (overlay);
27288 after = Foverlay_end (overlay);
27289 before_string = Foverlay_get (overlay, Qbefore_string);
27290 after_string = Foverlay_get (overlay, Qafter_string);
27291
27292 if (!STRINGP (before_string)) before_string = Qnil;
27293 if (!STRINGP (after_string)) after_string = Qnil;
27294 }
27295
27296 mouse_face_from_buffer_pos (window, hlinfo, pos,
27297 NILP (before)
27298 ? 1
27299 : XFASTINT (before),
27300 NILP (after)
27301 ? BUF_Z (XBUFFER (buffer))
27302 : XFASTINT (after),
27303 before_string, after_string,
27304 disp_string);
27305 cursor = No_Cursor;
27306 }
27307 }
27308 }
27309
27310 check_help_echo:
27311
27312 /* Look for a `help-echo' property. */
27313 if (NILP (help_echo_string)) {
27314 Lisp_Object help, overlay;
27315
27316 /* Check overlays first. */
27317 help = overlay = Qnil;
27318 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27319 {
27320 overlay = overlay_vec[i];
27321 help = Foverlay_get (overlay, Qhelp_echo);
27322 }
27323
27324 if (!NILP (help))
27325 {
27326 help_echo_string = help;
27327 help_echo_window = window;
27328 help_echo_object = overlay;
27329 help_echo_pos = pos;
27330 }
27331 else
27332 {
27333 Lisp_Object obj = glyph->object;
27334 ptrdiff_t charpos = glyph->charpos;
27335
27336 /* Try text properties. */
27337 if (STRINGP (obj)
27338 && charpos >= 0
27339 && charpos < SCHARS (obj))
27340 {
27341 help = Fget_text_property (make_number (charpos),
27342 Qhelp_echo, obj);
27343 if (NILP (help))
27344 {
27345 /* If the string itself doesn't specify a help-echo,
27346 see if the buffer text ``under'' it does. */
27347 struct glyph_row *r
27348 = MATRIX_ROW (w->current_matrix, vpos);
27349 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27350 ptrdiff_t p = string_buffer_position (obj, start);
27351 if (p > 0)
27352 {
27353 help = Fget_char_property (make_number (p),
27354 Qhelp_echo, w->buffer);
27355 if (!NILP (help))
27356 {
27357 charpos = p;
27358 obj = w->buffer;
27359 }
27360 }
27361 }
27362 }
27363 else if (BUFFERP (obj)
27364 && charpos >= BEGV
27365 && charpos < ZV)
27366 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27367 obj);
27368
27369 if (!NILP (help))
27370 {
27371 help_echo_string = help;
27372 help_echo_window = window;
27373 help_echo_object = obj;
27374 help_echo_pos = charpos;
27375 }
27376 }
27377 }
27378
27379 #ifdef HAVE_WINDOW_SYSTEM
27380 /* Look for a `pointer' property. */
27381 if (FRAME_WINDOW_P (f) && NILP (pointer))
27382 {
27383 /* Check overlays first. */
27384 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27385 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27386
27387 if (NILP (pointer))
27388 {
27389 Lisp_Object obj = glyph->object;
27390 ptrdiff_t charpos = glyph->charpos;
27391
27392 /* Try text properties. */
27393 if (STRINGP (obj)
27394 && charpos >= 0
27395 && charpos < SCHARS (obj))
27396 {
27397 pointer = Fget_text_property (make_number (charpos),
27398 Qpointer, obj);
27399 if (NILP (pointer))
27400 {
27401 /* If the string itself doesn't specify a pointer,
27402 see if the buffer text ``under'' it does. */
27403 struct glyph_row *r
27404 = MATRIX_ROW (w->current_matrix, vpos);
27405 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27406 ptrdiff_t p = string_buffer_position (obj, start);
27407 if (p > 0)
27408 pointer = Fget_char_property (make_number (p),
27409 Qpointer, w->buffer);
27410 }
27411 }
27412 else if (BUFFERP (obj)
27413 && charpos >= BEGV
27414 && charpos < ZV)
27415 pointer = Fget_text_property (make_number (charpos),
27416 Qpointer, obj);
27417 }
27418 }
27419 #endif /* HAVE_WINDOW_SYSTEM */
27420
27421 BEGV = obegv;
27422 ZV = ozv;
27423 current_buffer = obuf;
27424 }
27425
27426 set_cursor:
27427
27428 #ifdef HAVE_WINDOW_SYSTEM
27429 if (FRAME_WINDOW_P (f))
27430 define_frame_cursor1 (f, cursor, pointer);
27431 #else
27432 /* This is here to prevent a compiler error, about "label at end of
27433 compound statement". */
27434 return;
27435 #endif
27436 }
27437
27438
27439 /* EXPORT for RIF:
27440 Clear any mouse-face on window W. This function is part of the
27441 redisplay interface, and is called from try_window_id and similar
27442 functions to ensure the mouse-highlight is off. */
27443
27444 void
27445 x_clear_window_mouse_face (struct window *w)
27446 {
27447 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27448 Lisp_Object window;
27449
27450 BLOCK_INPUT;
27451 XSETWINDOW (window, w);
27452 if (EQ (window, hlinfo->mouse_face_window))
27453 clear_mouse_face (hlinfo);
27454 UNBLOCK_INPUT;
27455 }
27456
27457
27458 /* EXPORT:
27459 Just discard the mouse face information for frame F, if any.
27460 This is used when the size of F is changed. */
27461
27462 void
27463 cancel_mouse_face (struct frame *f)
27464 {
27465 Lisp_Object window;
27466 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27467
27468 window = hlinfo->mouse_face_window;
27469 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27470 {
27471 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27472 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27473 hlinfo->mouse_face_window = Qnil;
27474 }
27475 }
27476
27477
27478 \f
27479 /***********************************************************************
27480 Exposure Events
27481 ***********************************************************************/
27482
27483 #ifdef HAVE_WINDOW_SYSTEM
27484
27485 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27486 which intersects rectangle R. R is in window-relative coordinates. */
27487
27488 static void
27489 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27490 enum glyph_row_area area)
27491 {
27492 struct glyph *first = row->glyphs[area];
27493 struct glyph *end = row->glyphs[area] + row->used[area];
27494 struct glyph *last;
27495 int first_x, start_x, x;
27496
27497 if (area == TEXT_AREA && row->fill_line_p)
27498 /* If row extends face to end of line write the whole line. */
27499 draw_glyphs (w, 0, row, area,
27500 0, row->used[area],
27501 DRAW_NORMAL_TEXT, 0);
27502 else
27503 {
27504 /* Set START_X to the window-relative start position for drawing glyphs of
27505 AREA. The first glyph of the text area can be partially visible.
27506 The first glyphs of other areas cannot. */
27507 start_x = window_box_left_offset (w, area);
27508 x = start_x;
27509 if (area == TEXT_AREA)
27510 x += row->x;
27511
27512 /* Find the first glyph that must be redrawn. */
27513 while (first < end
27514 && x + first->pixel_width < r->x)
27515 {
27516 x += first->pixel_width;
27517 ++first;
27518 }
27519
27520 /* Find the last one. */
27521 last = first;
27522 first_x = x;
27523 while (last < end
27524 && x < r->x + r->width)
27525 {
27526 x += last->pixel_width;
27527 ++last;
27528 }
27529
27530 /* Repaint. */
27531 if (last > first)
27532 draw_glyphs (w, first_x - start_x, row, area,
27533 first - row->glyphs[area], last - row->glyphs[area],
27534 DRAW_NORMAL_TEXT, 0);
27535 }
27536 }
27537
27538
27539 /* Redraw the parts of the glyph row ROW on window W intersecting
27540 rectangle R. R is in window-relative coordinates. Value is
27541 non-zero if mouse-face was overwritten. */
27542
27543 static int
27544 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27545 {
27546 xassert (row->enabled_p);
27547
27548 if (row->mode_line_p || w->pseudo_window_p)
27549 draw_glyphs (w, 0, row, TEXT_AREA,
27550 0, row->used[TEXT_AREA],
27551 DRAW_NORMAL_TEXT, 0);
27552 else
27553 {
27554 if (row->used[LEFT_MARGIN_AREA])
27555 expose_area (w, row, r, LEFT_MARGIN_AREA);
27556 if (row->used[TEXT_AREA])
27557 expose_area (w, row, r, TEXT_AREA);
27558 if (row->used[RIGHT_MARGIN_AREA])
27559 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27560 draw_row_fringe_bitmaps (w, row);
27561 }
27562
27563 return row->mouse_face_p;
27564 }
27565
27566
27567 /* Redraw those parts of glyphs rows during expose event handling that
27568 overlap other rows. Redrawing of an exposed line writes over parts
27569 of lines overlapping that exposed line; this function fixes that.
27570
27571 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27572 row in W's current matrix that is exposed and overlaps other rows.
27573 LAST_OVERLAPPING_ROW is the last such row. */
27574
27575 static void
27576 expose_overlaps (struct window *w,
27577 struct glyph_row *first_overlapping_row,
27578 struct glyph_row *last_overlapping_row,
27579 XRectangle *r)
27580 {
27581 struct glyph_row *row;
27582
27583 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27584 if (row->overlapping_p)
27585 {
27586 xassert (row->enabled_p && !row->mode_line_p);
27587
27588 row->clip = r;
27589 if (row->used[LEFT_MARGIN_AREA])
27590 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
27591
27592 if (row->used[TEXT_AREA])
27593 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
27594
27595 if (row->used[RIGHT_MARGIN_AREA])
27596 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
27597 row->clip = NULL;
27598 }
27599 }
27600
27601
27602 /* Return non-zero if W's cursor intersects rectangle R. */
27603
27604 static int
27605 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
27606 {
27607 XRectangle cr, result;
27608 struct glyph *cursor_glyph;
27609 struct glyph_row *row;
27610
27611 if (w->phys_cursor.vpos >= 0
27612 && w->phys_cursor.vpos < w->current_matrix->nrows
27613 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
27614 row->enabled_p)
27615 && row->cursor_in_fringe_p)
27616 {
27617 /* Cursor is in the fringe. */
27618 cr.x = window_box_right_offset (w,
27619 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
27620 ? RIGHT_MARGIN_AREA
27621 : TEXT_AREA));
27622 cr.y = row->y;
27623 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
27624 cr.height = row->height;
27625 return x_intersect_rectangles (&cr, r, &result);
27626 }
27627
27628 cursor_glyph = get_phys_cursor_glyph (w);
27629 if (cursor_glyph)
27630 {
27631 /* r is relative to W's box, but w->phys_cursor.x is relative
27632 to left edge of W's TEXT area. Adjust it. */
27633 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
27634 cr.y = w->phys_cursor.y;
27635 cr.width = cursor_glyph->pixel_width;
27636 cr.height = w->phys_cursor_height;
27637 /* ++KFS: W32 version used W32-specific IntersectRect here, but
27638 I assume the effect is the same -- and this is portable. */
27639 return x_intersect_rectangles (&cr, r, &result);
27640 }
27641 /* If we don't understand the format, pretend we're not in the hot-spot. */
27642 return 0;
27643 }
27644
27645
27646 /* EXPORT:
27647 Draw a vertical window border to the right of window W if W doesn't
27648 have vertical scroll bars. */
27649
27650 void
27651 x_draw_vertical_border (struct window *w)
27652 {
27653 struct frame *f = XFRAME (WINDOW_FRAME (w));
27654
27655 /* We could do better, if we knew what type of scroll-bar the adjacent
27656 windows (on either side) have... But we don't :-(
27657 However, I think this works ok. ++KFS 2003-04-25 */
27658
27659 /* Redraw borders between horizontally adjacent windows. Don't
27660 do it for frames with vertical scroll bars because either the
27661 right scroll bar of a window, or the left scroll bar of its
27662 neighbor will suffice as a border. */
27663 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
27664 return;
27665
27666 if (!WINDOW_RIGHTMOST_P (w)
27667 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
27668 {
27669 int x0, x1, y0, y1;
27670
27671 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27672 y1 -= 1;
27673
27674 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27675 x1 -= 1;
27676
27677 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
27678 }
27679 else if (!WINDOW_LEFTMOST_P (w)
27680 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
27681 {
27682 int x0, x1, y0, y1;
27683
27684 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
27685 y1 -= 1;
27686
27687 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
27688 x0 -= 1;
27689
27690 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
27691 }
27692 }
27693
27694
27695 /* Redraw the part of window W intersection rectangle FR. Pixel
27696 coordinates in FR are frame-relative. Call this function with
27697 input blocked. Value is non-zero if the exposure overwrites
27698 mouse-face. */
27699
27700 static int
27701 expose_window (struct window *w, XRectangle *fr)
27702 {
27703 struct frame *f = XFRAME (w->frame);
27704 XRectangle wr, r;
27705 int mouse_face_overwritten_p = 0;
27706
27707 /* If window is not yet fully initialized, do nothing. This can
27708 happen when toolkit scroll bars are used and a window is split.
27709 Reconfiguring the scroll bar will generate an expose for a newly
27710 created window. */
27711 if (w->current_matrix == NULL)
27712 return 0;
27713
27714 /* When we're currently updating the window, display and current
27715 matrix usually don't agree. Arrange for a thorough display
27716 later. */
27717 if (w == updated_window)
27718 {
27719 SET_FRAME_GARBAGED (f);
27720 return 0;
27721 }
27722
27723 /* Frame-relative pixel rectangle of W. */
27724 wr.x = WINDOW_LEFT_EDGE_X (w);
27725 wr.y = WINDOW_TOP_EDGE_Y (w);
27726 wr.width = WINDOW_TOTAL_WIDTH (w);
27727 wr.height = WINDOW_TOTAL_HEIGHT (w);
27728
27729 if (x_intersect_rectangles (fr, &wr, &r))
27730 {
27731 int yb = window_text_bottom_y (w);
27732 struct glyph_row *row;
27733 int cursor_cleared_p, phys_cursor_on_p;
27734 struct glyph_row *first_overlapping_row, *last_overlapping_row;
27735
27736 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
27737 r.x, r.y, r.width, r.height));
27738
27739 /* Convert to window coordinates. */
27740 r.x -= WINDOW_LEFT_EDGE_X (w);
27741 r.y -= WINDOW_TOP_EDGE_Y (w);
27742
27743 /* Turn off the cursor. */
27744 if (!w->pseudo_window_p
27745 && phys_cursor_in_rect_p (w, &r))
27746 {
27747 x_clear_cursor (w);
27748 cursor_cleared_p = 1;
27749 }
27750 else
27751 cursor_cleared_p = 0;
27752
27753 /* If the row containing the cursor extends face to end of line,
27754 then expose_area might overwrite the cursor outside the
27755 rectangle and thus notice_overwritten_cursor might clear
27756 w->phys_cursor_on_p. We remember the original value and
27757 check later if it is changed. */
27758 phys_cursor_on_p = w->phys_cursor_on_p;
27759
27760 /* Update lines intersecting rectangle R. */
27761 first_overlapping_row = last_overlapping_row = NULL;
27762 for (row = w->current_matrix->rows;
27763 row->enabled_p;
27764 ++row)
27765 {
27766 int y0 = row->y;
27767 int y1 = MATRIX_ROW_BOTTOM_Y (row);
27768
27769 if ((y0 >= r.y && y0 < r.y + r.height)
27770 || (y1 > r.y && y1 < r.y + r.height)
27771 || (r.y >= y0 && r.y < y1)
27772 || (r.y + r.height > y0 && r.y + r.height < y1))
27773 {
27774 /* A header line may be overlapping, but there is no need
27775 to fix overlapping areas for them. KFS 2005-02-12 */
27776 if (row->overlapping_p && !row->mode_line_p)
27777 {
27778 if (first_overlapping_row == NULL)
27779 first_overlapping_row = row;
27780 last_overlapping_row = row;
27781 }
27782
27783 row->clip = fr;
27784 if (expose_line (w, row, &r))
27785 mouse_face_overwritten_p = 1;
27786 row->clip = NULL;
27787 }
27788 else if (row->overlapping_p)
27789 {
27790 /* We must redraw a row overlapping the exposed area. */
27791 if (y0 < r.y
27792 ? y0 + row->phys_height > r.y
27793 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
27794 {
27795 if (first_overlapping_row == NULL)
27796 first_overlapping_row = row;
27797 last_overlapping_row = row;
27798 }
27799 }
27800
27801 if (y1 >= yb)
27802 break;
27803 }
27804
27805 /* Display the mode line if there is one. */
27806 if (WINDOW_WANTS_MODELINE_P (w)
27807 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
27808 row->enabled_p)
27809 && row->y < r.y + r.height)
27810 {
27811 if (expose_line (w, row, &r))
27812 mouse_face_overwritten_p = 1;
27813 }
27814
27815 if (!w->pseudo_window_p)
27816 {
27817 /* Fix the display of overlapping rows. */
27818 if (first_overlapping_row)
27819 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
27820 fr);
27821
27822 /* Draw border between windows. */
27823 x_draw_vertical_border (w);
27824
27825 /* Turn the cursor on again. */
27826 if (cursor_cleared_p
27827 || (phys_cursor_on_p && !w->phys_cursor_on_p))
27828 update_window_cursor (w, 1);
27829 }
27830 }
27831
27832 return mouse_face_overwritten_p;
27833 }
27834
27835
27836
27837 /* Redraw (parts) of all windows in the window tree rooted at W that
27838 intersect R. R contains frame pixel coordinates. Value is
27839 non-zero if the exposure overwrites mouse-face. */
27840
27841 static int
27842 expose_window_tree (struct window *w, XRectangle *r)
27843 {
27844 struct frame *f = XFRAME (w->frame);
27845 int mouse_face_overwritten_p = 0;
27846
27847 while (w && !FRAME_GARBAGED_P (f))
27848 {
27849 if (!NILP (w->hchild))
27850 mouse_face_overwritten_p
27851 |= expose_window_tree (XWINDOW (w->hchild), r);
27852 else if (!NILP (w->vchild))
27853 mouse_face_overwritten_p
27854 |= expose_window_tree (XWINDOW (w->vchild), r);
27855 else
27856 mouse_face_overwritten_p |= expose_window (w, r);
27857
27858 w = NILP (w->next) ? NULL : XWINDOW (w->next);
27859 }
27860
27861 return mouse_face_overwritten_p;
27862 }
27863
27864
27865 /* EXPORT:
27866 Redisplay an exposed area of frame F. X and Y are the upper-left
27867 corner of the exposed rectangle. W and H are width and height of
27868 the exposed area. All are pixel values. W or H zero means redraw
27869 the entire frame. */
27870
27871 void
27872 expose_frame (struct frame *f, int x, int y, int w, int h)
27873 {
27874 XRectangle r;
27875 int mouse_face_overwritten_p = 0;
27876
27877 TRACE ((stderr, "expose_frame "));
27878
27879 /* No need to redraw if frame will be redrawn soon. */
27880 if (FRAME_GARBAGED_P (f))
27881 {
27882 TRACE ((stderr, " garbaged\n"));
27883 return;
27884 }
27885
27886 /* If basic faces haven't been realized yet, there is no point in
27887 trying to redraw anything. This can happen when we get an expose
27888 event while Emacs is starting, e.g. by moving another window. */
27889 if (FRAME_FACE_CACHE (f) == NULL
27890 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
27891 {
27892 TRACE ((stderr, " no faces\n"));
27893 return;
27894 }
27895
27896 if (w == 0 || h == 0)
27897 {
27898 r.x = r.y = 0;
27899 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
27900 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
27901 }
27902 else
27903 {
27904 r.x = x;
27905 r.y = y;
27906 r.width = w;
27907 r.height = h;
27908 }
27909
27910 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
27911 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
27912
27913 if (WINDOWP (f->tool_bar_window))
27914 mouse_face_overwritten_p
27915 |= expose_window (XWINDOW (f->tool_bar_window), &r);
27916
27917 #ifdef HAVE_X_WINDOWS
27918 #ifndef MSDOS
27919 #ifndef USE_X_TOOLKIT
27920 if (WINDOWP (f->menu_bar_window))
27921 mouse_face_overwritten_p
27922 |= expose_window (XWINDOW (f->menu_bar_window), &r);
27923 #endif /* not USE_X_TOOLKIT */
27924 #endif
27925 #endif
27926
27927 /* Some window managers support a focus-follows-mouse style with
27928 delayed raising of frames. Imagine a partially obscured frame,
27929 and moving the mouse into partially obscured mouse-face on that
27930 frame. The visible part of the mouse-face will be highlighted,
27931 then the WM raises the obscured frame. With at least one WM, KDE
27932 2.1, Emacs is not getting any event for the raising of the frame
27933 (even tried with SubstructureRedirectMask), only Expose events.
27934 These expose events will draw text normally, i.e. not
27935 highlighted. Which means we must redo the highlight here.
27936 Subsume it under ``we love X''. --gerd 2001-08-15 */
27937 /* Included in Windows version because Windows most likely does not
27938 do the right thing if any third party tool offers
27939 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
27940 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
27941 {
27942 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27943 if (f == hlinfo->mouse_face_mouse_frame)
27944 {
27945 int mouse_x = hlinfo->mouse_face_mouse_x;
27946 int mouse_y = hlinfo->mouse_face_mouse_y;
27947 clear_mouse_face (hlinfo);
27948 note_mouse_highlight (f, mouse_x, mouse_y);
27949 }
27950 }
27951 }
27952
27953
27954 /* EXPORT:
27955 Determine the intersection of two rectangles R1 and R2. Return
27956 the intersection in *RESULT. Value is non-zero if RESULT is not
27957 empty. */
27958
27959 int
27960 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
27961 {
27962 XRectangle *left, *right;
27963 XRectangle *upper, *lower;
27964 int intersection_p = 0;
27965
27966 /* Rearrange so that R1 is the left-most rectangle. */
27967 if (r1->x < r2->x)
27968 left = r1, right = r2;
27969 else
27970 left = r2, right = r1;
27971
27972 /* X0 of the intersection is right.x0, if this is inside R1,
27973 otherwise there is no intersection. */
27974 if (right->x <= left->x + left->width)
27975 {
27976 result->x = right->x;
27977
27978 /* The right end of the intersection is the minimum of
27979 the right ends of left and right. */
27980 result->width = (min (left->x + left->width, right->x + right->width)
27981 - result->x);
27982
27983 /* Same game for Y. */
27984 if (r1->y < r2->y)
27985 upper = r1, lower = r2;
27986 else
27987 upper = r2, lower = r1;
27988
27989 /* The upper end of the intersection is lower.y0, if this is inside
27990 of upper. Otherwise, there is no intersection. */
27991 if (lower->y <= upper->y + upper->height)
27992 {
27993 result->y = lower->y;
27994
27995 /* The lower end of the intersection is the minimum of the lower
27996 ends of upper and lower. */
27997 result->height = (min (lower->y + lower->height,
27998 upper->y + upper->height)
27999 - result->y);
28000 intersection_p = 1;
28001 }
28002 }
28003
28004 return intersection_p;
28005 }
28006
28007 #endif /* HAVE_WINDOW_SYSTEM */
28008
28009 \f
28010 /***********************************************************************
28011 Initialization
28012 ***********************************************************************/
28013
28014 void
28015 syms_of_xdisp (void)
28016 {
28017 Vwith_echo_area_save_vector = Qnil;
28018 staticpro (&Vwith_echo_area_save_vector);
28019
28020 Vmessage_stack = Qnil;
28021 staticpro (&Vmessage_stack);
28022
28023 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28024
28025 message_dolog_marker1 = Fmake_marker ();
28026 staticpro (&message_dolog_marker1);
28027 message_dolog_marker2 = Fmake_marker ();
28028 staticpro (&message_dolog_marker2);
28029 message_dolog_marker3 = Fmake_marker ();
28030 staticpro (&message_dolog_marker3);
28031
28032 #if GLYPH_DEBUG
28033 defsubr (&Sdump_frame_glyph_matrix);
28034 defsubr (&Sdump_glyph_matrix);
28035 defsubr (&Sdump_glyph_row);
28036 defsubr (&Sdump_tool_bar_row);
28037 defsubr (&Strace_redisplay);
28038 defsubr (&Strace_to_stderr);
28039 #endif
28040 #ifdef HAVE_WINDOW_SYSTEM
28041 defsubr (&Stool_bar_lines_needed);
28042 defsubr (&Slookup_image_map);
28043 #endif
28044 defsubr (&Sformat_mode_line);
28045 defsubr (&Sinvisible_p);
28046 defsubr (&Scurrent_bidi_paragraph_direction);
28047
28048 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28049 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28050 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28051 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28052 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28053 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28054 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28055 DEFSYM (Qeval, "eval");
28056 DEFSYM (QCdata, ":data");
28057 DEFSYM (Qdisplay, "display");
28058 DEFSYM (Qspace_width, "space-width");
28059 DEFSYM (Qraise, "raise");
28060 DEFSYM (Qslice, "slice");
28061 DEFSYM (Qspace, "space");
28062 DEFSYM (Qmargin, "margin");
28063 DEFSYM (Qpointer, "pointer");
28064 DEFSYM (Qleft_margin, "left-margin");
28065 DEFSYM (Qright_margin, "right-margin");
28066 DEFSYM (Qcenter, "center");
28067 DEFSYM (Qline_height, "line-height");
28068 DEFSYM (QCalign_to, ":align-to");
28069 DEFSYM (QCrelative_width, ":relative-width");
28070 DEFSYM (QCrelative_height, ":relative-height");
28071 DEFSYM (QCeval, ":eval");
28072 DEFSYM (QCpropertize, ":propertize");
28073 DEFSYM (QCfile, ":file");
28074 DEFSYM (Qfontified, "fontified");
28075 DEFSYM (Qfontification_functions, "fontification-functions");
28076 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28077 DEFSYM (Qescape_glyph, "escape-glyph");
28078 DEFSYM (Qnobreak_space, "nobreak-space");
28079 DEFSYM (Qimage, "image");
28080 DEFSYM (Qtext, "text");
28081 DEFSYM (Qboth, "both");
28082 DEFSYM (Qboth_horiz, "both-horiz");
28083 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28084 DEFSYM (QCmap, ":map");
28085 DEFSYM (QCpointer, ":pointer");
28086 DEFSYM (Qrect, "rect");
28087 DEFSYM (Qcircle, "circle");
28088 DEFSYM (Qpoly, "poly");
28089 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28090 DEFSYM (Qgrow_only, "grow-only");
28091 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28092 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28093 DEFSYM (Qposition, "position");
28094 DEFSYM (Qbuffer_position, "buffer-position");
28095 DEFSYM (Qobject, "object");
28096 DEFSYM (Qbar, "bar");
28097 DEFSYM (Qhbar, "hbar");
28098 DEFSYM (Qbox, "box");
28099 DEFSYM (Qhollow, "hollow");
28100 DEFSYM (Qhand, "hand");
28101 DEFSYM (Qarrow, "arrow");
28102 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28103
28104 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28105 Fcons (intern_c_string ("void-variable"), Qnil)),
28106 Qnil);
28107 staticpro (&list_of_error);
28108
28109 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28110 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28111 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28112 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28113
28114 echo_buffer[0] = echo_buffer[1] = Qnil;
28115 staticpro (&echo_buffer[0]);
28116 staticpro (&echo_buffer[1]);
28117
28118 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28119 staticpro (&echo_area_buffer[0]);
28120 staticpro (&echo_area_buffer[1]);
28121
28122 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
28123 staticpro (&Vmessages_buffer_name);
28124
28125 mode_line_proptrans_alist = Qnil;
28126 staticpro (&mode_line_proptrans_alist);
28127 mode_line_string_list = Qnil;
28128 staticpro (&mode_line_string_list);
28129 mode_line_string_face = Qnil;
28130 staticpro (&mode_line_string_face);
28131 mode_line_string_face_prop = Qnil;
28132 staticpro (&mode_line_string_face_prop);
28133 Vmode_line_unwind_vector = Qnil;
28134 staticpro (&Vmode_line_unwind_vector);
28135
28136 help_echo_string = Qnil;
28137 staticpro (&help_echo_string);
28138 help_echo_object = Qnil;
28139 staticpro (&help_echo_object);
28140 help_echo_window = Qnil;
28141 staticpro (&help_echo_window);
28142 previous_help_echo_string = Qnil;
28143 staticpro (&previous_help_echo_string);
28144 help_echo_pos = -1;
28145
28146 DEFSYM (Qright_to_left, "right-to-left");
28147 DEFSYM (Qleft_to_right, "left-to-right");
28148
28149 #ifdef HAVE_WINDOW_SYSTEM
28150 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28151 doc: /* *Non-nil means draw block cursor as wide as the glyph under it.
28152 For example, if a block cursor is over a tab, it will be drawn as
28153 wide as that tab on the display. */);
28154 x_stretch_cursor_p = 0;
28155 #endif
28156
28157 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28158 doc: /* *Non-nil means highlight trailing whitespace.
28159 The face used for trailing whitespace is `trailing-whitespace'. */);
28160 Vshow_trailing_whitespace = Qnil;
28161
28162 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28163 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28164 If the value is t, Emacs highlights non-ASCII chars which have the
28165 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28166 or `escape-glyph' face respectively.
28167
28168 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28169 U+2011 (non-breaking hyphen) are affected.
28170
28171 Any other non-nil value means to display these characters as a escape
28172 glyph followed by an ordinary space or hyphen.
28173
28174 A value of nil means no special handling of these characters. */);
28175 Vnobreak_char_display = Qt;
28176
28177 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28178 doc: /* *The pointer shape to show in void text areas.
28179 A value of nil means to show the text pointer. Other options are `arrow',
28180 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28181 Vvoid_text_area_pointer = Qarrow;
28182
28183 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28184 doc: /* Non-nil means don't actually do any redisplay.
28185 This is used for internal purposes. */);
28186 Vinhibit_redisplay = Qnil;
28187
28188 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28189 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28190 Vglobal_mode_string = Qnil;
28191
28192 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28193 doc: /* Marker for where to display an arrow on top of the buffer text.
28194 This must be the beginning of a line in order to work.
28195 See also `overlay-arrow-string'. */);
28196 Voverlay_arrow_position = Qnil;
28197
28198 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28199 doc: /* String to display as an arrow in non-window frames.
28200 See also `overlay-arrow-position'. */);
28201 Voverlay_arrow_string = make_pure_c_string ("=>");
28202
28203 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28204 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28205 The symbols on this list are examined during redisplay to determine
28206 where to display overlay arrows. */);
28207 Voverlay_arrow_variable_list
28208 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28209
28210 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28211 doc: /* *The number of lines to try scrolling a window by when point moves out.
28212 If that fails to bring point back on frame, point is centered instead.
28213 If this is zero, point is always centered after it moves off frame.
28214 If you want scrolling to always be a line at a time, you should set
28215 `scroll-conservatively' to a large value rather than set this to 1. */);
28216
28217 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28218 doc: /* *Scroll up to this many lines, to bring point back on screen.
28219 If point moves off-screen, redisplay will scroll by up to
28220 `scroll-conservatively' lines in order to bring point just barely
28221 onto the screen again. If that cannot be done, then redisplay
28222 recenters point as usual.
28223
28224 If the value is greater than 100, redisplay will never recenter point,
28225 but will always scroll just enough text to bring point into view, even
28226 if you move far away.
28227
28228 A value of zero means always recenter point if it moves off screen. */);
28229 scroll_conservatively = 0;
28230
28231 DEFVAR_INT ("scroll-margin", scroll_margin,
28232 doc: /* *Number of lines of margin at the top and bottom of a window.
28233 Recenter the window whenever point gets within this many lines
28234 of the top or bottom of the window. */);
28235 scroll_margin = 0;
28236
28237 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28238 doc: /* Pixels per inch value for non-window system displays.
28239 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28240 Vdisplay_pixels_per_inch = make_float (72.0);
28241
28242 #if GLYPH_DEBUG
28243 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28244 #endif
28245
28246 DEFVAR_LISP ("truncate-partial-width-windows",
28247 Vtruncate_partial_width_windows,
28248 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28249 For an integer value, truncate lines in each window narrower than the
28250 full frame width, provided the window width is less than that integer;
28251 otherwise, respect the value of `truncate-lines'.
28252
28253 For any other non-nil value, truncate lines in all windows that do
28254 not span the full frame width.
28255
28256 A value of nil means to respect the value of `truncate-lines'.
28257
28258 If `word-wrap' is enabled, you might want to reduce this. */);
28259 Vtruncate_partial_width_windows = make_number (50);
28260
28261 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28262 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28263 Any other value means to use the appropriate face, `mode-line',
28264 `header-line', or `menu' respectively. */);
28265 mode_line_inverse_video = 1;
28266
28267 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28268 doc: /* *Maximum buffer size for which line number should be displayed.
28269 If the buffer is bigger than this, the line number does not appear
28270 in the mode line. A value of nil means no limit. */);
28271 Vline_number_display_limit = Qnil;
28272
28273 DEFVAR_INT ("line-number-display-limit-width",
28274 line_number_display_limit_width,
28275 doc: /* *Maximum line width (in characters) for line number display.
28276 If the average length of the lines near point is bigger than this, then the
28277 line number may be omitted from the mode line. */);
28278 line_number_display_limit_width = 200;
28279
28280 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28281 doc: /* *Non-nil means highlight region even in nonselected windows. */);
28282 highlight_nonselected_windows = 0;
28283
28284 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28285 doc: /* Non-nil if more than one frame is visible on this display.
28286 Minibuffer-only frames don't count, but iconified frames do.
28287 This variable is not guaranteed to be accurate except while processing
28288 `frame-title-format' and `icon-title-format'. */);
28289
28290 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28291 doc: /* Template for displaying the title bar of visible frames.
28292 \(Assuming the window manager supports this feature.)
28293
28294 This variable has the same structure as `mode-line-format', except that
28295 the %c and %l constructs are ignored. It is used only on frames for
28296 which no explicit name has been set \(see `modify-frame-parameters'). */);
28297
28298 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28299 doc: /* Template for displaying the title bar of an iconified frame.
28300 \(Assuming the window manager supports this feature.)
28301 This variable has the same structure as `mode-line-format' (which see),
28302 and is used only on frames for which no explicit name has been set
28303 \(see `modify-frame-parameters'). */);
28304 Vicon_title_format
28305 = Vframe_title_format
28306 = pure_cons (intern_c_string ("multiple-frames"),
28307 pure_cons (make_pure_c_string ("%b"),
28308 pure_cons (pure_cons (empty_unibyte_string,
28309 pure_cons (intern_c_string ("invocation-name"),
28310 pure_cons (make_pure_c_string ("@"),
28311 pure_cons (intern_c_string ("system-name"),
28312 Qnil)))),
28313 Qnil)));
28314
28315 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28316 doc: /* Maximum number of lines to keep in the message log buffer.
28317 If nil, disable message logging. If t, log messages but don't truncate
28318 the buffer when it becomes large. */);
28319 Vmessage_log_max = make_number (100);
28320
28321 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28322 doc: /* Functions called before redisplay, if window sizes have changed.
28323 The value should be a list of functions that take one argument.
28324 Just before redisplay, for each frame, if any of its windows have changed
28325 size since the last redisplay, or have been split or deleted,
28326 all the functions in the list are called, with the frame as argument. */);
28327 Vwindow_size_change_functions = Qnil;
28328
28329 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28330 doc: /* List of functions to call before redisplaying a window with scrolling.
28331 Each function is called with two arguments, the window and its new
28332 display-start position. Note that these functions are also called by
28333 `set-window-buffer'. Also note that the value of `window-end' is not
28334 valid when these functions are called. */);
28335 Vwindow_scroll_functions = Qnil;
28336
28337 DEFVAR_LISP ("window-text-change-functions",
28338 Vwindow_text_change_functions,
28339 doc: /* Functions to call in redisplay when text in the window might change. */);
28340 Vwindow_text_change_functions = Qnil;
28341
28342 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28343 doc: /* Functions called when redisplay of a window reaches the end trigger.
28344 Each function is called with two arguments, the window and the end trigger value.
28345 See `set-window-redisplay-end-trigger'. */);
28346 Vredisplay_end_trigger_functions = Qnil;
28347
28348 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28349 doc: /* *Non-nil means autoselect window with mouse pointer.
28350 If nil, do not autoselect windows.
28351 A positive number means delay autoselection by that many seconds: a
28352 window is autoselected only after the mouse has remained in that
28353 window for the duration of the delay.
28354 A negative number has a similar effect, but causes windows to be
28355 autoselected only after the mouse has stopped moving. \(Because of
28356 the way Emacs compares mouse events, you will occasionally wait twice
28357 that time before the window gets selected.\)
28358 Any other value means to autoselect window instantaneously when the
28359 mouse pointer enters it.
28360
28361 Autoselection selects the minibuffer only if it is active, and never
28362 unselects the minibuffer if it is active.
28363
28364 When customizing this variable make sure that the actual value of
28365 `focus-follows-mouse' matches the behavior of your window manager. */);
28366 Vmouse_autoselect_window = Qnil;
28367
28368 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28369 doc: /* *Non-nil means automatically resize tool-bars.
28370 This dynamically changes the tool-bar's height to the minimum height
28371 that is needed to make all tool-bar items visible.
28372 If value is `grow-only', the tool-bar's height is only increased
28373 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28374 Vauto_resize_tool_bars = Qt;
28375
28376 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28377 doc: /* *Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28378 auto_raise_tool_bar_buttons_p = 1;
28379
28380 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28381 doc: /* *Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28382 make_cursor_line_fully_visible_p = 1;
28383
28384 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28385 doc: /* *Border below tool-bar in pixels.
28386 If an integer, use it as the height of the border.
28387 If it is one of `internal-border-width' or `border-width', use the
28388 value of the corresponding frame parameter.
28389 Otherwise, no border is added below the tool-bar. */);
28390 Vtool_bar_border = Qinternal_border_width;
28391
28392 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28393 doc: /* *Margin around tool-bar buttons in pixels.
28394 If an integer, use that for both horizontal and vertical margins.
28395 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28396 HORZ specifying the horizontal margin, and VERT specifying the
28397 vertical margin. */);
28398 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28399
28400 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28401 doc: /* *Relief thickness of tool-bar buttons. */);
28402 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28403
28404 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28405 doc: /* Tool bar style to use.
28406 It can be one of
28407 image - show images only
28408 text - show text only
28409 both - show both, text below image
28410 both-horiz - show text to the right of the image
28411 text-image-horiz - show text to the left of the image
28412 any other - use system default or image if no system default. */);
28413 Vtool_bar_style = Qnil;
28414
28415 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28416 doc: /* *Maximum number of characters a label can have to be shown.
28417 The tool bar style must also show labels for this to have any effect, see
28418 `tool-bar-style'. */);
28419 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28420
28421 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28422 doc: /* List of functions to call to fontify regions of text.
28423 Each function is called with one argument POS. Functions must
28424 fontify a region starting at POS in the current buffer, and give
28425 fontified regions the property `fontified'. */);
28426 Vfontification_functions = Qnil;
28427 Fmake_variable_buffer_local (Qfontification_functions);
28428
28429 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28430 unibyte_display_via_language_environment,
28431 doc: /* *Non-nil means display unibyte text according to language environment.
28432 Specifically, this means that raw bytes in the range 160-255 decimal
28433 are displayed by converting them to the equivalent multibyte characters
28434 according to the current language environment. As a result, they are
28435 displayed according to the current fontset.
28436
28437 Note that this variable affects only how these bytes are displayed,
28438 but does not change the fact they are interpreted as raw bytes. */);
28439 unibyte_display_via_language_environment = 0;
28440
28441 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28442 doc: /* *Maximum height for resizing mini-windows (the minibuffer and the echo area).
28443 If a float, it specifies a fraction of the mini-window frame's height.
28444 If an integer, it specifies a number of lines. */);
28445 Vmax_mini_window_height = make_float (0.25);
28446
28447 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28448 doc: /* How to resize mini-windows (the minibuffer and the echo area).
28449 A value of nil means don't automatically resize mini-windows.
28450 A value of t means resize them to fit the text displayed in them.
28451 A value of `grow-only', the default, means let mini-windows grow only;
28452 they return to their normal size when the minibuffer is closed, or the
28453 echo area becomes empty. */);
28454 Vresize_mini_windows = Qgrow_only;
28455
28456 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28457 doc: /* Alist specifying how to blink the cursor off.
28458 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28459 `cursor-type' frame-parameter or variable equals ON-STATE,
28460 comparing using `equal', Emacs uses OFF-STATE to specify
28461 how to blink it off. ON-STATE and OFF-STATE are values for
28462 the `cursor-type' frame parameter.
28463
28464 If a frame's ON-STATE has no entry in this list,
28465 the frame's other specifications determine how to blink the cursor off. */);
28466 Vblink_cursor_alist = Qnil;
28467
28468 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28469 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28470 If non-nil, windows are automatically scrolled horizontally to make
28471 point visible. */);
28472 automatic_hscrolling_p = 1;
28473 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28474
28475 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28476 doc: /* *How many columns away from the window edge point is allowed to get
28477 before automatic hscrolling will horizontally scroll the window. */);
28478 hscroll_margin = 5;
28479
28480 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28481 doc: /* *How many columns to scroll the window when point gets too close to the edge.
28482 When point is less than `hscroll-margin' columns from the window
28483 edge, automatic hscrolling will scroll the window by the amount of columns
28484 determined by this variable. If its value is a positive integer, scroll that
28485 many columns. If it's a positive floating-point number, it specifies the
28486 fraction of the window's width to scroll. If it's nil or zero, point will be
28487 centered horizontally after the scroll. Any other value, including negative
28488 numbers, are treated as if the value were zero.
28489
28490 Automatic hscrolling always moves point outside the scroll margin, so if
28491 point was more than scroll step columns inside the margin, the window will
28492 scroll more than the value given by the scroll step.
28493
28494 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28495 and `scroll-right' overrides this variable's effect. */);
28496 Vhscroll_step = make_number (0);
28497
28498 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28499 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28500 Bind this around calls to `message' to let it take effect. */);
28501 message_truncate_lines = 0;
28502
28503 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28504 doc: /* Normal hook run to update the menu bar definitions.
28505 Redisplay runs this hook before it redisplays the menu bar.
28506 This is used to update submenus such as Buffers,
28507 whose contents depend on various data. */);
28508 Vmenu_bar_update_hook = Qnil;
28509
28510 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28511 doc: /* Frame for which we are updating a menu.
28512 The enable predicate for a menu binding should check this variable. */);
28513 Vmenu_updating_frame = Qnil;
28514
28515 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28516 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28517 inhibit_menubar_update = 0;
28518
28519 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28520 doc: /* Prefix prepended to all continuation lines at display time.
28521 The value may be a string, an image, or a stretch-glyph; it is
28522 interpreted in the same way as the value of a `display' text property.
28523
28524 This variable is overridden by any `wrap-prefix' text or overlay
28525 property.
28526
28527 To add a prefix to non-continuation lines, use `line-prefix'. */);
28528 Vwrap_prefix = Qnil;
28529 DEFSYM (Qwrap_prefix, "wrap-prefix");
28530 Fmake_variable_buffer_local (Qwrap_prefix);
28531
28532 DEFVAR_LISP ("line-prefix", Vline_prefix,
28533 doc: /* Prefix prepended to all non-continuation lines at display time.
28534 The value may be a string, an image, or a stretch-glyph; it is
28535 interpreted in the same way as the value of a `display' text property.
28536
28537 This variable is overridden by any `line-prefix' text or overlay
28538 property.
28539
28540 To add a prefix to continuation lines, use `wrap-prefix'. */);
28541 Vline_prefix = Qnil;
28542 DEFSYM (Qline_prefix, "line-prefix");
28543 Fmake_variable_buffer_local (Qline_prefix);
28544
28545 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28546 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28547 inhibit_eval_during_redisplay = 0;
28548
28549 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28550 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28551 inhibit_free_realized_faces = 0;
28552
28553 #if GLYPH_DEBUG
28554 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28555 doc: /* Inhibit try_window_id display optimization. */);
28556 inhibit_try_window_id = 0;
28557
28558 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28559 doc: /* Inhibit try_window_reusing display optimization. */);
28560 inhibit_try_window_reusing = 0;
28561
28562 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28563 doc: /* Inhibit try_cursor_movement display optimization. */);
28564 inhibit_try_cursor_movement = 0;
28565 #endif /* GLYPH_DEBUG */
28566
28567 DEFVAR_INT ("overline-margin", overline_margin,
28568 doc: /* *Space between overline and text, in pixels.
28569 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28570 margin to the character height. */);
28571 overline_margin = 2;
28572
28573 DEFVAR_INT ("underline-minimum-offset",
28574 underline_minimum_offset,
28575 doc: /* Minimum distance between baseline and underline.
28576 This can improve legibility of underlined text at small font sizes,
28577 particularly when using variable `x-use-underline-position-properties'
28578 with fonts that specify an UNDERLINE_POSITION relatively close to the
28579 baseline. The default value is 1. */);
28580 underline_minimum_offset = 1;
28581
28582 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
28583 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
28584 This feature only works when on a window system that can change
28585 cursor shapes. */);
28586 display_hourglass_p = 1;
28587
28588 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
28589 doc: /* *Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
28590 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
28591
28592 hourglass_atimer = NULL;
28593 hourglass_shown_p = 0;
28594
28595 DEFSYM (Qglyphless_char, "glyphless-char");
28596 DEFSYM (Qhex_code, "hex-code");
28597 DEFSYM (Qempty_box, "empty-box");
28598 DEFSYM (Qthin_space, "thin-space");
28599 DEFSYM (Qzero_width, "zero-width");
28600
28601 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
28602 /* Intern this now in case it isn't already done.
28603 Setting this variable twice is harmless.
28604 But don't staticpro it here--that is done in alloc.c. */
28605 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
28606 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
28607
28608 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
28609 doc: /* Char-table defining glyphless characters.
28610 Each element, if non-nil, should be one of the following:
28611 an ASCII acronym string: display this string in a box
28612 `hex-code': display the hexadecimal code of a character in a box
28613 `empty-box': display as an empty box
28614 `thin-space': display as 1-pixel width space
28615 `zero-width': don't display
28616 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
28617 display method for graphical terminals and text terminals respectively.
28618 GRAPHICAL and TEXT should each have one of the values listed above.
28619
28620 The char-table has one extra slot to control the display of a character for
28621 which no font is found. This slot only takes effect on graphical terminals.
28622 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
28623 `thin-space'. The default is `empty-box'. */);
28624 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
28625 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
28626 Qempty_box);
28627 }
28628
28629
28630 /* Initialize this module when Emacs starts. */
28631
28632 void
28633 init_xdisp (void)
28634 {
28635 current_header_line_height = current_mode_line_height = -1;
28636
28637 CHARPOS (this_line_start_pos) = 0;
28638
28639 if (!noninteractive)
28640 {
28641 struct window *m = XWINDOW (minibuf_window);
28642 Lisp_Object frame = m->frame;
28643 struct frame *f = XFRAME (frame);
28644 Lisp_Object root = FRAME_ROOT_WINDOW (f);
28645 struct window *r = XWINDOW (root);
28646 int i;
28647
28648 echo_area_window = minibuf_window;
28649
28650 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
28651 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
28652 XSETFASTINT (r->total_cols, FRAME_COLS (f));
28653 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
28654 XSETFASTINT (m->total_lines, 1);
28655 XSETFASTINT (m->total_cols, FRAME_COLS (f));
28656
28657 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
28658 scratch_glyph_row.glyphs[TEXT_AREA + 1]
28659 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
28660
28661 /* The default ellipsis glyphs `...'. */
28662 for (i = 0; i < 3; ++i)
28663 default_invis_vector[i] = make_number ('.');
28664 }
28665
28666 {
28667 /* Allocate the buffer for frame titles.
28668 Also used for `format-mode-line'. */
28669 int size = 100;
28670 mode_line_noprop_buf = (char *) xmalloc (size);
28671 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
28672 mode_line_noprop_ptr = mode_line_noprop_buf;
28673 mode_line_target = MODE_LINE_DISPLAY;
28674 }
28675
28676 help_echo_showing_p = 0;
28677 }
28678
28679 /* Since w32 does not support atimers, it defines its own implementation of
28680 the following three functions in w32fns.c. */
28681 #ifndef WINDOWSNT
28682
28683 /* Platform-independent portion of hourglass implementation. */
28684
28685 /* Return non-zero if hourglass timer has been started or hourglass is
28686 shown. */
28687 int
28688 hourglass_started (void)
28689 {
28690 return hourglass_shown_p || hourglass_atimer != NULL;
28691 }
28692
28693 /* Cancel a currently active hourglass timer, and start a new one. */
28694 void
28695 start_hourglass (void)
28696 {
28697 #if defined (HAVE_WINDOW_SYSTEM)
28698 EMACS_TIME delay;
28699 int secs = DEFAULT_HOURGLASS_DELAY, usecs = 0;
28700
28701 cancel_hourglass ();
28702
28703 if (NUMBERP (Vhourglass_delay))
28704 {
28705 double duration = extract_float (Vhourglass_delay);
28706 if (0 < duration)
28707 duration_to_sec_usec (duration, &secs, &usecs);
28708 }
28709
28710 EMACS_SET_SECS_USECS (delay, secs, usecs);
28711 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
28712 show_hourglass, NULL);
28713 #endif
28714 }
28715
28716
28717 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
28718 shown. */
28719 void
28720 cancel_hourglass (void)
28721 {
28722 #if defined (HAVE_WINDOW_SYSTEM)
28723 if (hourglass_atimer)
28724 {
28725 cancel_atimer (hourglass_atimer);
28726 hourglass_atimer = NULL;
28727 }
28728
28729 if (hourglass_shown_p)
28730 hide_hourglass ();
28731 #endif
28732 }
28733 #endif /* ! WINDOWSNT */