Fix bug #11653 with cursor positioning in a row that has only strings.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2012 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276 #include <setjmp.h>
277
278 #include "lisp.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "buffer.h"
285 #include "character.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301
302 #ifdef HAVE_X_WINDOWS
303 #include "xterm.h"
304 #endif
305 #ifdef WINDOWSNT
306 #include "w32term.h"
307 #endif
308 #ifdef HAVE_NS
309 #include "nsterm.h"
310 #endif
311 #ifdef USE_GTK
312 #include "gtkutil.h"
313 #endif
314
315 #include "font.h"
316
317 #ifndef FRAME_X_OUTPUT
318 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
319 #endif
320
321 #define INFINITY 10000000
322
323 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
324 Lisp_Object Qwindow_scroll_functions;
325 static Lisp_Object Qwindow_text_change_functions;
326 static Lisp_Object Qredisplay_end_trigger_functions;
327 Lisp_Object Qinhibit_point_motion_hooks;
328 static Lisp_Object QCeval, QCpropertize;
329 Lisp_Object QCfile, QCdata;
330 static Lisp_Object Qfontified;
331 static Lisp_Object Qgrow_only;
332 static Lisp_Object Qinhibit_eval_during_redisplay;
333 static Lisp_Object Qbuffer_position, Qposition, Qobject;
334 static Lisp_Object Qright_to_left, Qleft_to_right;
335
336 /* Cursor shapes */
337 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
338
339 /* Pointer shapes */
340 static Lisp_Object Qarrow, Qhand;
341 Lisp_Object Qtext;
342
343 /* Holds the list (error). */
344 static Lisp_Object list_of_error;
345
346 static Lisp_Object Qfontification_functions;
347
348 static Lisp_Object Qwrap_prefix;
349 static Lisp_Object Qline_prefix;
350
351 /* Non-nil means don't actually do any redisplay. */
352
353 Lisp_Object Qinhibit_redisplay;
354
355 /* Names of text properties relevant for redisplay. */
356
357 Lisp_Object Qdisplay;
358
359 Lisp_Object Qspace, QCalign_to;
360 static Lisp_Object QCrelative_width, QCrelative_height;
361 Lisp_Object Qleft_margin, Qright_margin;
362 static Lisp_Object Qspace_width, Qraise;
363 static Lisp_Object Qslice;
364 Lisp_Object Qcenter;
365 static Lisp_Object Qmargin, Qpointer;
366 static Lisp_Object Qline_height;
367
368 #ifdef HAVE_WINDOW_SYSTEM
369
370 /* Test if overflow newline into fringe. Called with iterator IT
371 at or past right window margin, and with IT->current_x set. */
372
373 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
374 (!NILP (Voverflow_newline_into_fringe) \
375 && FRAME_WINDOW_P ((IT)->f) \
376 && ((IT)->bidi_it.paragraph_dir == R2L \
377 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
378 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
379 && (IT)->current_x == (IT)->last_visible_x \
380 && (IT)->line_wrap != WORD_WRAP)
381
382 #else /* !HAVE_WINDOW_SYSTEM */
383 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
384 #endif /* HAVE_WINDOW_SYSTEM */
385
386 /* Test if the display element loaded in IT, or the underlying buffer
387 or string character, is a space or a TAB character. This is used
388 to determine where word wrapping can occur. */
389
390 #define IT_DISPLAYING_WHITESPACE(it) \
391 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
392 || ((STRINGP (it->string) \
393 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
394 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
395 || (it->s \
396 && (it->s[IT_BYTEPOS (*it)] == ' ' \
397 || it->s[IT_BYTEPOS (*it)] == '\t')) \
398 || (IT_BYTEPOS (*it) < ZV_BYTE \
399 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
400 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
401
402 /* Name of the face used to highlight trailing whitespace. */
403
404 static Lisp_Object Qtrailing_whitespace;
405
406 /* Name and number of the face used to highlight escape glyphs. */
407
408 static Lisp_Object Qescape_glyph;
409
410 /* Name and number of the face used to highlight non-breaking spaces. */
411
412 static Lisp_Object Qnobreak_space;
413
414 /* The symbol `image' which is the car of the lists used to represent
415 images in Lisp. Also a tool bar style. */
416
417 Lisp_Object Qimage;
418
419 /* The image map types. */
420 Lisp_Object QCmap;
421 static Lisp_Object QCpointer;
422 static Lisp_Object Qrect, Qcircle, Qpoly;
423
424 /* Tool bar styles */
425 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
426
427 /* Non-zero means print newline to stdout before next mini-buffer
428 message. */
429
430 int noninteractive_need_newline;
431
432 /* Non-zero means print newline to message log before next message. */
433
434 static int message_log_need_newline;
435
436 /* Three markers that message_dolog uses.
437 It could allocate them itself, but that causes trouble
438 in handling memory-full errors. */
439 static Lisp_Object message_dolog_marker1;
440 static Lisp_Object message_dolog_marker2;
441 static Lisp_Object message_dolog_marker3;
442 \f
443 /* The buffer position of the first character appearing entirely or
444 partially on the line of the selected window which contains the
445 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
446 redisplay optimization in redisplay_internal. */
447
448 static struct text_pos this_line_start_pos;
449
450 /* Number of characters past the end of the line above, including the
451 terminating newline. */
452
453 static struct text_pos this_line_end_pos;
454
455 /* The vertical positions and the height of this line. */
456
457 static int this_line_vpos;
458 static int this_line_y;
459 static int this_line_pixel_height;
460
461 /* X position at which this display line starts. Usually zero;
462 negative if first character is partially visible. */
463
464 static int this_line_start_x;
465
466 /* The smallest character position seen by move_it_* functions as they
467 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
468 hscrolled lines, see display_line. */
469
470 static struct text_pos this_line_min_pos;
471
472 /* Buffer that this_line_.* variables are referring to. */
473
474 static struct buffer *this_line_buffer;
475
476
477 /* Values of those variables at last redisplay are stored as
478 properties on `overlay-arrow-position' symbol. However, if
479 Voverlay_arrow_position is a marker, last-arrow-position is its
480 numerical position. */
481
482 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
483
484 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
485 properties on a symbol in overlay-arrow-variable-list. */
486
487 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
488
489 Lisp_Object Qmenu_bar_update_hook;
490
491 /* Nonzero if an overlay arrow has been displayed in this window. */
492
493 static int overlay_arrow_seen;
494
495 /* Number of windows showing the buffer of the selected window (or
496 another buffer with the same base buffer). keyboard.c refers to
497 this. */
498
499 int buffer_shared;
500
501 /* Vector containing glyphs for an ellipsis `...'. */
502
503 static Lisp_Object default_invis_vector[3];
504
505 /* This is the window where the echo area message was displayed. It
506 is always a mini-buffer window, but it may not be the same window
507 currently active as a mini-buffer. */
508
509 Lisp_Object echo_area_window;
510
511 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
512 pushes the current message and the value of
513 message_enable_multibyte on the stack, the function restore_message
514 pops the stack and displays MESSAGE again. */
515
516 static Lisp_Object Vmessage_stack;
517
518 /* Nonzero means multibyte characters were enabled when the echo area
519 message was specified. */
520
521 static int message_enable_multibyte;
522
523 /* Nonzero if we should redraw the mode lines on the next redisplay. */
524
525 int update_mode_lines;
526
527 /* Nonzero if window sizes or contents have changed since last
528 redisplay that finished. */
529
530 int windows_or_buffers_changed;
531
532 /* Nonzero means a frame's cursor type has been changed. */
533
534 int cursor_type_changed;
535
536 /* Nonzero after display_mode_line if %l was used and it displayed a
537 line number. */
538
539 static int line_number_displayed;
540
541 /* The name of the *Messages* buffer, a string. */
542
543 static Lisp_Object Vmessages_buffer_name;
544
545 /* Current, index 0, and last displayed echo area message. Either
546 buffers from echo_buffers, or nil to indicate no message. */
547
548 Lisp_Object echo_area_buffer[2];
549
550 /* The buffers referenced from echo_area_buffer. */
551
552 static Lisp_Object echo_buffer[2];
553
554 /* A vector saved used in with_area_buffer to reduce consing. */
555
556 static Lisp_Object Vwith_echo_area_save_vector;
557
558 /* Non-zero means display_echo_area should display the last echo area
559 message again. Set by redisplay_preserve_echo_area. */
560
561 static int display_last_displayed_message_p;
562
563 /* Nonzero if echo area is being used by print; zero if being used by
564 message. */
565
566 static int message_buf_print;
567
568 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
569
570 static Lisp_Object Qinhibit_menubar_update;
571 static Lisp_Object Qmessage_truncate_lines;
572
573 /* Set to 1 in clear_message to make redisplay_internal aware
574 of an emptied echo area. */
575
576 static int message_cleared_p;
577
578 /* A scratch glyph row with contents used for generating truncation
579 glyphs. Also used in direct_output_for_insert. */
580
581 #define MAX_SCRATCH_GLYPHS 100
582 static struct glyph_row scratch_glyph_row;
583 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
584
585 /* Ascent and height of the last line processed by move_it_to. */
586
587 static int last_max_ascent, last_height;
588
589 /* Non-zero if there's a help-echo in the echo area. */
590
591 int help_echo_showing_p;
592
593 /* If >= 0, computed, exact values of mode-line and header-line height
594 to use in the macros CURRENT_MODE_LINE_HEIGHT and
595 CURRENT_HEADER_LINE_HEIGHT. */
596
597 int current_mode_line_height, current_header_line_height;
598
599 /* The maximum distance to look ahead for text properties. Values
600 that are too small let us call compute_char_face and similar
601 functions too often which is expensive. Values that are too large
602 let us call compute_char_face and alike too often because we
603 might not be interested in text properties that far away. */
604
605 #define TEXT_PROP_DISTANCE_LIMIT 100
606
607 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
608 iterator state and later restore it. This is needed because the
609 bidi iterator on bidi.c keeps a stacked cache of its states, which
610 is really a singleton. When we use scratch iterator objects to
611 move around the buffer, we can cause the bidi cache to be pushed or
612 popped, and therefore we need to restore the cache state when we
613 return to the original iterator. */
614 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
615 do { \
616 if (CACHE) \
617 bidi_unshelve_cache (CACHE, 1); \
618 ITCOPY = ITORIG; \
619 CACHE = bidi_shelve_cache (); \
620 } while (0)
621
622 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
623 do { \
624 if (pITORIG != pITCOPY) \
625 *(pITORIG) = *(pITCOPY); \
626 bidi_unshelve_cache (CACHE, 0); \
627 CACHE = NULL; \
628 } while (0)
629
630 #if GLYPH_DEBUG
631
632 /* Non-zero means print traces of redisplay if compiled with
633 GLYPH_DEBUG != 0. */
634
635 int trace_redisplay_p;
636
637 #endif /* GLYPH_DEBUG */
638
639 #ifdef DEBUG_TRACE_MOVE
640 /* Non-zero means trace with TRACE_MOVE to stderr. */
641 int trace_move;
642
643 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
644 #else
645 #define TRACE_MOVE(x) (void) 0
646 #endif
647
648 static Lisp_Object Qauto_hscroll_mode;
649
650 /* Buffer being redisplayed -- for redisplay_window_error. */
651
652 static struct buffer *displayed_buffer;
653
654 /* Value returned from text property handlers (see below). */
655
656 enum prop_handled
657 {
658 HANDLED_NORMALLY,
659 HANDLED_RECOMPUTE_PROPS,
660 HANDLED_OVERLAY_STRING_CONSUMED,
661 HANDLED_RETURN
662 };
663
664 /* A description of text properties that redisplay is interested
665 in. */
666
667 struct props
668 {
669 /* The name of the property. */
670 Lisp_Object *name;
671
672 /* A unique index for the property. */
673 enum prop_idx idx;
674
675 /* A handler function called to set up iterator IT from the property
676 at IT's current position. Value is used to steer handle_stop. */
677 enum prop_handled (*handler) (struct it *it);
678 };
679
680 static enum prop_handled handle_face_prop (struct it *);
681 static enum prop_handled handle_invisible_prop (struct it *);
682 static enum prop_handled handle_display_prop (struct it *);
683 static enum prop_handled handle_composition_prop (struct it *);
684 static enum prop_handled handle_overlay_change (struct it *);
685 static enum prop_handled handle_fontified_prop (struct it *);
686
687 /* Properties handled by iterators. */
688
689 static struct props it_props[] =
690 {
691 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
692 /* Handle `face' before `display' because some sub-properties of
693 `display' need to know the face. */
694 {&Qface, FACE_PROP_IDX, handle_face_prop},
695 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
696 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
697 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
698 {NULL, 0, NULL}
699 };
700
701 /* Value is the position described by X. If X is a marker, value is
702 the marker_position of X. Otherwise, value is X. */
703
704 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
705
706 /* Enumeration returned by some move_it_.* functions internally. */
707
708 enum move_it_result
709 {
710 /* Not used. Undefined value. */
711 MOVE_UNDEFINED,
712
713 /* Move ended at the requested buffer position or ZV. */
714 MOVE_POS_MATCH_OR_ZV,
715
716 /* Move ended at the requested X pixel position. */
717 MOVE_X_REACHED,
718
719 /* Move within a line ended at the end of a line that must be
720 continued. */
721 MOVE_LINE_CONTINUED,
722
723 /* Move within a line ended at the end of a line that would
724 be displayed truncated. */
725 MOVE_LINE_TRUNCATED,
726
727 /* Move within a line ended at a line end. */
728 MOVE_NEWLINE_OR_CR
729 };
730
731 /* This counter is used to clear the face cache every once in a while
732 in redisplay_internal. It is incremented for each redisplay.
733 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
734 cleared. */
735
736 #define CLEAR_FACE_CACHE_COUNT 500
737 static int clear_face_cache_count;
738
739 /* Similarly for the image cache. */
740
741 #ifdef HAVE_WINDOW_SYSTEM
742 #define CLEAR_IMAGE_CACHE_COUNT 101
743 static int clear_image_cache_count;
744
745 /* Null glyph slice */
746 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
747 #endif
748
749 /* Non-zero while redisplay_internal is in progress. */
750
751 int redisplaying_p;
752
753 static Lisp_Object Qinhibit_free_realized_faces;
754 static Lisp_Object Qmode_line_default_help_echo;
755
756 /* If a string, XTread_socket generates an event to display that string.
757 (The display is done in read_char.) */
758
759 Lisp_Object help_echo_string;
760 Lisp_Object help_echo_window;
761 Lisp_Object help_echo_object;
762 ptrdiff_t help_echo_pos;
763
764 /* Temporary variable for XTread_socket. */
765
766 Lisp_Object previous_help_echo_string;
767
768 /* Platform-independent portion of hourglass implementation. */
769
770 /* Non-zero means an hourglass cursor is currently shown. */
771 int hourglass_shown_p;
772
773 /* If non-null, an asynchronous timer that, when it expires, displays
774 an hourglass cursor on all frames. */
775 struct atimer *hourglass_atimer;
776
777 /* Name of the face used to display glyphless characters. */
778 Lisp_Object Qglyphless_char;
779
780 /* Symbol for the purpose of Vglyphless_char_display. */
781 static Lisp_Object Qglyphless_char_display;
782
783 /* Method symbols for Vglyphless_char_display. */
784 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
785
786 /* Default pixel width of `thin-space' display method. */
787 #define THIN_SPACE_WIDTH 1
788
789 /* Default number of seconds to wait before displaying an hourglass
790 cursor. */
791 #define DEFAULT_HOURGLASS_DELAY 1
792
793 \f
794 /* Function prototypes. */
795
796 static void setup_for_ellipsis (struct it *, int);
797 static void set_iterator_to_next (struct it *, int);
798 static void mark_window_display_accurate_1 (struct window *, int);
799 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
800 static int display_prop_string_p (Lisp_Object, Lisp_Object);
801 static int cursor_row_p (struct glyph_row *);
802 static int redisplay_mode_lines (Lisp_Object, int);
803 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
804
805 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
806
807 static void handle_line_prefix (struct it *);
808
809 static void pint2str (char *, int, ptrdiff_t);
810 static void pint2hrstr (char *, int, ptrdiff_t);
811 static struct text_pos run_window_scroll_functions (Lisp_Object,
812 struct text_pos);
813 static void reconsider_clip_changes (struct window *, struct buffer *);
814 static int text_outside_line_unchanged_p (struct window *,
815 ptrdiff_t, ptrdiff_t);
816 static void store_mode_line_noprop_char (char);
817 static int store_mode_line_noprop (const char *, int, int);
818 static void handle_stop (struct it *);
819 static void handle_stop_backwards (struct it *, ptrdiff_t);
820 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
821 static void ensure_echo_area_buffers (void);
822 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
823 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
824 static int with_echo_area_buffer (struct window *, int,
825 int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
826 ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
827 static void clear_garbaged_frames (void);
828 static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
829 static void pop_message (void);
830 static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
831 static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
832 static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
833 static int display_echo_area (struct window *);
834 static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
835 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
836 static Lisp_Object unwind_redisplay (Lisp_Object);
837 static int string_char_and_length (const unsigned char *, int *);
838 static struct text_pos display_prop_end (struct it *, Lisp_Object,
839 struct text_pos);
840 static int compute_window_start_on_continuation_line (struct window *);
841 static Lisp_Object safe_eval_handler (Lisp_Object);
842 static void insert_left_trunc_glyphs (struct it *);
843 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
844 Lisp_Object);
845 static void extend_face_to_end_of_line (struct it *);
846 static int append_space_for_newline (struct it *, int);
847 static int cursor_row_fully_visible_p (struct window *, int, int);
848 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
849 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
850 static int trailing_whitespace_p (ptrdiff_t);
851 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
852 static void push_it (struct it *, struct text_pos *);
853 static void iterate_out_of_display_property (struct it *);
854 static void pop_it (struct it *);
855 static void sync_frame_with_window_matrix_rows (struct window *);
856 static void select_frame_for_redisplay (Lisp_Object);
857 static void redisplay_internal (void);
858 static int echo_area_display (int);
859 static void redisplay_windows (Lisp_Object);
860 static void redisplay_window (Lisp_Object, int);
861 static Lisp_Object redisplay_window_error (Lisp_Object);
862 static Lisp_Object redisplay_window_0 (Lisp_Object);
863 static Lisp_Object redisplay_window_1 (Lisp_Object);
864 static int set_cursor_from_row (struct window *, struct glyph_row *,
865 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
866 int, int);
867 static int update_menu_bar (struct frame *, int, int);
868 static int try_window_reusing_current_matrix (struct window *);
869 static int try_window_id (struct window *);
870 static int display_line (struct it *);
871 static int display_mode_lines (struct window *);
872 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
873 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
874 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
875 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
876 static void display_menu_bar (struct window *);
877 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
878 ptrdiff_t *);
879 static int display_string (const char *, Lisp_Object, Lisp_Object,
880 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
881 static void compute_line_metrics (struct it *);
882 static void run_redisplay_end_trigger_hook (struct it *);
883 static int get_overlay_strings (struct it *, ptrdiff_t);
884 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
885 static void next_overlay_string (struct it *);
886 static void reseat (struct it *, struct text_pos, int);
887 static void reseat_1 (struct it *, struct text_pos, int);
888 static void back_to_previous_visible_line_start (struct it *);
889 void reseat_at_previous_visible_line_start (struct it *);
890 static void reseat_at_next_visible_line_start (struct it *, int);
891 static int next_element_from_ellipsis (struct it *);
892 static int next_element_from_display_vector (struct it *);
893 static int next_element_from_string (struct it *);
894 static int next_element_from_c_string (struct it *);
895 static int next_element_from_buffer (struct it *);
896 static int next_element_from_composition (struct it *);
897 static int next_element_from_image (struct it *);
898 static int next_element_from_stretch (struct it *);
899 static void load_overlay_strings (struct it *, ptrdiff_t);
900 static int init_from_display_pos (struct it *, struct window *,
901 struct display_pos *);
902 static void reseat_to_string (struct it *, const char *,
903 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
904 static int get_next_display_element (struct it *);
905 static enum move_it_result
906 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
907 enum move_operation_enum);
908 void move_it_vertically_backward (struct it *, int);
909 static void init_to_row_start (struct it *, struct window *,
910 struct glyph_row *);
911 static int init_to_row_end (struct it *, struct window *,
912 struct glyph_row *);
913 static void back_to_previous_line_start (struct it *);
914 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
915 static struct text_pos string_pos_nchars_ahead (struct text_pos,
916 Lisp_Object, ptrdiff_t);
917 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
918 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
919 static ptrdiff_t number_of_chars (const char *, int);
920 static void compute_stop_pos (struct it *);
921 static void compute_string_pos (struct text_pos *, struct text_pos,
922 Lisp_Object);
923 static int face_before_or_after_it_pos (struct it *, int);
924 static ptrdiff_t next_overlay_change (ptrdiff_t);
925 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
926 Lisp_Object, struct text_pos *, ptrdiff_t, int);
927 static int handle_single_display_spec (struct it *, Lisp_Object,
928 Lisp_Object, Lisp_Object,
929 struct text_pos *, ptrdiff_t, int, int);
930 static int underlying_face_id (struct it *);
931 static int in_ellipses_for_invisible_text_p (struct display_pos *,
932 struct window *);
933
934 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
935 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
936
937 #ifdef HAVE_WINDOW_SYSTEM
938
939 static void x_consider_frame_title (Lisp_Object);
940 static int tool_bar_lines_needed (struct frame *, int *);
941 static void update_tool_bar (struct frame *, int);
942 static void build_desired_tool_bar_string (struct frame *f);
943 static int redisplay_tool_bar (struct frame *);
944 static void display_tool_bar_line (struct it *, int);
945 static void notice_overwritten_cursor (struct window *,
946 enum glyph_row_area,
947 int, int, int, int);
948 static void append_stretch_glyph (struct it *, Lisp_Object,
949 int, int, int);
950
951
952 #endif /* HAVE_WINDOW_SYSTEM */
953
954 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
955 static int coords_in_mouse_face_p (struct window *, int, int);
956
957
958 \f
959 /***********************************************************************
960 Window display dimensions
961 ***********************************************************************/
962
963 /* Return the bottom boundary y-position for text lines in window W.
964 This is the first y position at which a line cannot start.
965 It is relative to the top of the window.
966
967 This is the height of W minus the height of a mode line, if any. */
968
969 int
970 window_text_bottom_y (struct window *w)
971 {
972 int height = WINDOW_TOTAL_HEIGHT (w);
973
974 if (WINDOW_WANTS_MODELINE_P (w))
975 height -= CURRENT_MODE_LINE_HEIGHT (w);
976 return height;
977 }
978
979 /* Return the pixel width of display area AREA of window W. AREA < 0
980 means return the total width of W, not including fringes to
981 the left and right of the window. */
982
983 int
984 window_box_width (struct window *w, int area)
985 {
986 int cols = XFASTINT (w->total_cols);
987 int pixels = 0;
988
989 if (!w->pseudo_window_p)
990 {
991 cols -= WINDOW_SCROLL_BAR_COLS (w);
992
993 if (area == TEXT_AREA)
994 {
995 if (INTEGERP (w->left_margin_cols))
996 cols -= XFASTINT (w->left_margin_cols);
997 if (INTEGERP (w->right_margin_cols))
998 cols -= XFASTINT (w->right_margin_cols);
999 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1000 }
1001 else if (area == LEFT_MARGIN_AREA)
1002 {
1003 cols = (INTEGERP (w->left_margin_cols)
1004 ? XFASTINT (w->left_margin_cols) : 0);
1005 pixels = 0;
1006 }
1007 else if (area == RIGHT_MARGIN_AREA)
1008 {
1009 cols = (INTEGERP (w->right_margin_cols)
1010 ? XFASTINT (w->right_margin_cols) : 0);
1011 pixels = 0;
1012 }
1013 }
1014
1015 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1016 }
1017
1018
1019 /* Return the pixel height of the display area of window W, not
1020 including mode lines of W, if any. */
1021
1022 int
1023 window_box_height (struct window *w)
1024 {
1025 struct frame *f = XFRAME (w->frame);
1026 int height = WINDOW_TOTAL_HEIGHT (w);
1027
1028 xassert (height >= 0);
1029
1030 /* Note: the code below that determines the mode-line/header-line
1031 height is essentially the same as that contained in the macro
1032 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1033 the appropriate glyph row has its `mode_line_p' flag set,
1034 and if it doesn't, uses estimate_mode_line_height instead. */
1035
1036 if (WINDOW_WANTS_MODELINE_P (w))
1037 {
1038 struct glyph_row *ml_row
1039 = (w->current_matrix && w->current_matrix->rows
1040 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1041 : 0);
1042 if (ml_row && ml_row->mode_line_p)
1043 height -= ml_row->height;
1044 else
1045 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1046 }
1047
1048 if (WINDOW_WANTS_HEADER_LINE_P (w))
1049 {
1050 struct glyph_row *hl_row
1051 = (w->current_matrix && w->current_matrix->rows
1052 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1053 : 0);
1054 if (hl_row && hl_row->mode_line_p)
1055 height -= hl_row->height;
1056 else
1057 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1058 }
1059
1060 /* With a very small font and a mode-line that's taller than
1061 default, we might end up with a negative height. */
1062 return max (0, height);
1063 }
1064
1065 /* Return the window-relative coordinate of the left edge of display
1066 area AREA of window W. AREA < 0 means return the left edge of the
1067 whole window, to the right of the left fringe of W. */
1068
1069 int
1070 window_box_left_offset (struct window *w, int area)
1071 {
1072 int x;
1073
1074 if (w->pseudo_window_p)
1075 return 0;
1076
1077 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1078
1079 if (area == TEXT_AREA)
1080 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1081 + window_box_width (w, LEFT_MARGIN_AREA));
1082 else if (area == RIGHT_MARGIN_AREA)
1083 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1084 + window_box_width (w, LEFT_MARGIN_AREA)
1085 + window_box_width (w, TEXT_AREA)
1086 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1087 ? 0
1088 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1089 else if (area == LEFT_MARGIN_AREA
1090 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1091 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1092
1093 return x;
1094 }
1095
1096
1097 /* Return the window-relative coordinate of the right edge of display
1098 area AREA of window W. AREA < 0 means return the right edge of the
1099 whole window, to the left of the right fringe of W. */
1100
1101 int
1102 window_box_right_offset (struct window *w, int area)
1103 {
1104 return window_box_left_offset (w, area) + window_box_width (w, area);
1105 }
1106
1107 /* Return the frame-relative coordinate of the left edge of display
1108 area AREA of window W. AREA < 0 means return the left edge of the
1109 whole window, to the right of the left fringe of W. */
1110
1111 int
1112 window_box_left (struct window *w, int area)
1113 {
1114 struct frame *f = XFRAME (w->frame);
1115 int x;
1116
1117 if (w->pseudo_window_p)
1118 return FRAME_INTERNAL_BORDER_WIDTH (f);
1119
1120 x = (WINDOW_LEFT_EDGE_X (w)
1121 + window_box_left_offset (w, area));
1122
1123 return x;
1124 }
1125
1126
1127 /* Return the frame-relative coordinate of the right edge of display
1128 area AREA of window W. AREA < 0 means return the right edge of the
1129 whole window, to the left of the right fringe of W. */
1130
1131 int
1132 window_box_right (struct window *w, int area)
1133 {
1134 return window_box_left (w, area) + window_box_width (w, area);
1135 }
1136
1137 /* Get the bounding box of the display area AREA of window W, without
1138 mode lines, in frame-relative coordinates. AREA < 0 means the
1139 whole window, not including the left and right fringes of
1140 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1141 coordinates of the upper-left corner of the box. Return in
1142 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1143
1144 void
1145 window_box (struct window *w, int area, int *box_x, int *box_y,
1146 int *box_width, int *box_height)
1147 {
1148 if (box_width)
1149 *box_width = window_box_width (w, area);
1150 if (box_height)
1151 *box_height = window_box_height (w);
1152 if (box_x)
1153 *box_x = window_box_left (w, area);
1154 if (box_y)
1155 {
1156 *box_y = WINDOW_TOP_EDGE_Y (w);
1157 if (WINDOW_WANTS_HEADER_LINE_P (w))
1158 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1159 }
1160 }
1161
1162
1163 /* Get the bounding box of the display area AREA of window W, without
1164 mode lines. AREA < 0 means the whole window, not including the
1165 left and right fringe of the window. Return in *TOP_LEFT_X
1166 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1167 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1168 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1169 box. */
1170
1171 static inline void
1172 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1173 int *bottom_right_x, int *bottom_right_y)
1174 {
1175 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1176 bottom_right_y);
1177 *bottom_right_x += *top_left_x;
1178 *bottom_right_y += *top_left_y;
1179 }
1180
1181
1182 \f
1183 /***********************************************************************
1184 Utilities
1185 ***********************************************************************/
1186
1187 /* Return the bottom y-position of the line the iterator IT is in.
1188 This can modify IT's settings. */
1189
1190 int
1191 line_bottom_y (struct it *it)
1192 {
1193 int line_height = it->max_ascent + it->max_descent;
1194 int line_top_y = it->current_y;
1195
1196 if (line_height == 0)
1197 {
1198 if (last_height)
1199 line_height = last_height;
1200 else if (IT_CHARPOS (*it) < ZV)
1201 {
1202 move_it_by_lines (it, 1);
1203 line_height = (it->max_ascent || it->max_descent
1204 ? it->max_ascent + it->max_descent
1205 : last_height);
1206 }
1207 else
1208 {
1209 struct glyph_row *row = it->glyph_row;
1210
1211 /* Use the default character height. */
1212 it->glyph_row = NULL;
1213 it->what = IT_CHARACTER;
1214 it->c = ' ';
1215 it->len = 1;
1216 PRODUCE_GLYPHS (it);
1217 line_height = it->ascent + it->descent;
1218 it->glyph_row = row;
1219 }
1220 }
1221
1222 return line_top_y + line_height;
1223 }
1224
1225 /* Subroutine of pos_visible_p below. Extracts a display string, if
1226 any, from the display spec given as its argument. */
1227 static Lisp_Object
1228 string_from_display_spec (Lisp_Object spec)
1229 {
1230 if (CONSP (spec))
1231 {
1232 while (CONSP (spec))
1233 {
1234 if (STRINGP (XCAR (spec)))
1235 return XCAR (spec);
1236 spec = XCDR (spec);
1237 }
1238 }
1239 else if (VECTORP (spec))
1240 {
1241 ptrdiff_t i;
1242
1243 for (i = 0; i < ASIZE (spec); i++)
1244 {
1245 if (STRINGP (AREF (spec, i)))
1246 return AREF (spec, i);
1247 }
1248 return Qnil;
1249 }
1250
1251 return spec;
1252 }
1253
1254 /* Return 1 if position CHARPOS is visible in window W.
1255 CHARPOS < 0 means return info about WINDOW_END position.
1256 If visible, set *X and *Y to pixel coordinates of top left corner.
1257 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1258 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1259
1260 int
1261 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1262 int *rtop, int *rbot, int *rowh, int *vpos)
1263 {
1264 struct it it;
1265 void *itdata = bidi_shelve_cache ();
1266 struct text_pos top;
1267 int visible_p = 0;
1268 struct buffer *old_buffer = NULL;
1269
1270 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1271 return visible_p;
1272
1273 if (XBUFFER (w->buffer) != current_buffer)
1274 {
1275 old_buffer = current_buffer;
1276 set_buffer_internal_1 (XBUFFER (w->buffer));
1277 }
1278
1279 SET_TEXT_POS_FROM_MARKER (top, w->start);
1280 /* Scrolling a minibuffer window via scroll bar when the echo area
1281 shows long text sometimes resets the minibuffer contents behind
1282 our backs. */
1283 if (CHARPOS (top) > ZV)
1284 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1285
1286 /* Compute exact mode line heights. */
1287 if (WINDOW_WANTS_MODELINE_P (w))
1288 current_mode_line_height
1289 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1290 BVAR (current_buffer, mode_line_format));
1291
1292 if (WINDOW_WANTS_HEADER_LINE_P (w))
1293 current_header_line_height
1294 = display_mode_line (w, HEADER_LINE_FACE_ID,
1295 BVAR (current_buffer, header_line_format));
1296
1297 start_display (&it, w, top);
1298 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1299 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1300
1301 if (charpos >= 0
1302 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1303 && IT_CHARPOS (it) >= charpos)
1304 /* When scanning backwards under bidi iteration, move_it_to
1305 stops at or _before_ CHARPOS, because it stops at or to
1306 the _right_ of the character at CHARPOS. */
1307 || (it.bidi_p && it.bidi_it.scan_dir == -1
1308 && IT_CHARPOS (it) <= charpos)))
1309 {
1310 /* We have reached CHARPOS, or passed it. How the call to
1311 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1312 or covered by a display property, move_it_to stops at the end
1313 of the invisible text, to the right of CHARPOS. (ii) If
1314 CHARPOS is in a display vector, move_it_to stops on its last
1315 glyph. */
1316 int top_x = it.current_x;
1317 int top_y = it.current_y;
1318 /* Calling line_bottom_y may change it.method, it.position, etc. */
1319 enum it_method it_method = it.method;
1320 int bottom_y = (last_height = 0, line_bottom_y (&it));
1321 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1322
1323 if (top_y < window_top_y)
1324 visible_p = bottom_y > window_top_y;
1325 else if (top_y < it.last_visible_y)
1326 visible_p = 1;
1327 if (bottom_y >= it.last_visible_y
1328 && it.bidi_p && it.bidi_it.scan_dir == -1
1329 && IT_CHARPOS (it) < charpos)
1330 {
1331 /* When the last line of the window is scanned backwards
1332 under bidi iteration, we could be duped into thinking
1333 that we have passed CHARPOS, when in fact move_it_to
1334 simply stopped short of CHARPOS because it reached
1335 last_visible_y. To see if that's what happened, we call
1336 move_it_to again with a slightly larger vertical limit,
1337 and see if it actually moved vertically; if it did, we
1338 didn't really reach CHARPOS, which is beyond window end. */
1339 struct it save_it = it;
1340 /* Why 10? because we don't know how many canonical lines
1341 will the height of the next line(s) be. So we guess. */
1342 int ten_more_lines =
1343 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1344
1345 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1346 MOVE_TO_POS | MOVE_TO_Y);
1347 if (it.current_y > top_y)
1348 visible_p = 0;
1349
1350 it = save_it;
1351 }
1352 if (visible_p)
1353 {
1354 if (it_method == GET_FROM_DISPLAY_VECTOR)
1355 {
1356 /* We stopped on the last glyph of a display vector.
1357 Try and recompute. Hack alert! */
1358 if (charpos < 2 || top.charpos >= charpos)
1359 top_x = it.glyph_row->x;
1360 else
1361 {
1362 struct it it2;
1363 start_display (&it2, w, top);
1364 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1365 get_next_display_element (&it2);
1366 PRODUCE_GLYPHS (&it2);
1367 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1368 || it2.current_x > it2.last_visible_x)
1369 top_x = it.glyph_row->x;
1370 else
1371 {
1372 top_x = it2.current_x;
1373 top_y = it2.current_y;
1374 }
1375 }
1376 }
1377 else if (IT_CHARPOS (it) != charpos)
1378 {
1379 Lisp_Object cpos = make_number (charpos);
1380 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1381 Lisp_Object string = string_from_display_spec (spec);
1382 int newline_in_string = 0;
1383
1384 if (STRINGP (string))
1385 {
1386 const char *s = SSDATA (string);
1387 const char *e = s + SBYTES (string);
1388 while (s < e)
1389 {
1390 if (*s++ == '\n')
1391 {
1392 newline_in_string = 1;
1393 break;
1394 }
1395 }
1396 }
1397 /* The tricky code below is needed because there's a
1398 discrepancy between move_it_to and how we set cursor
1399 when the display line ends in a newline from a
1400 display string. move_it_to will stop _after_ such
1401 display strings, whereas set_cursor_from_row
1402 conspires with cursor_row_p to place the cursor on
1403 the first glyph produced from the display string. */
1404
1405 /* We have overshoot PT because it is covered by a
1406 display property whose value is a string. If the
1407 string includes embedded newlines, we are also in the
1408 wrong display line. Backtrack to the correct line,
1409 where the display string begins. */
1410 if (newline_in_string)
1411 {
1412 Lisp_Object startpos, endpos;
1413 EMACS_INT start, end;
1414 struct it it3;
1415 int it3_moved;
1416
1417 /* Find the first and the last buffer positions
1418 covered by the display string. */
1419 endpos =
1420 Fnext_single_char_property_change (cpos, Qdisplay,
1421 Qnil, Qnil);
1422 startpos =
1423 Fprevious_single_char_property_change (endpos, Qdisplay,
1424 Qnil, Qnil);
1425 start = XFASTINT (startpos);
1426 end = XFASTINT (endpos);
1427 /* Move to the last buffer position before the
1428 display property. */
1429 start_display (&it3, w, top);
1430 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1431 /* Move forward one more line if the position before
1432 the display string is a newline or if it is the
1433 rightmost character on a line that is
1434 continued or word-wrapped. */
1435 if (it3.method == GET_FROM_BUFFER
1436 && it3.c == '\n')
1437 move_it_by_lines (&it3, 1);
1438 else if (move_it_in_display_line_to (&it3, -1,
1439 it3.current_x
1440 + it3.pixel_width,
1441 MOVE_TO_X)
1442 == MOVE_LINE_CONTINUED)
1443 {
1444 move_it_by_lines (&it3, 1);
1445 /* When we are under word-wrap, the #$@%!
1446 move_it_by_lines moves 2 lines, so we need to
1447 fix that up. */
1448 if (it3.line_wrap == WORD_WRAP)
1449 move_it_by_lines (&it3, -1);
1450 }
1451
1452 /* Record the vertical coordinate of the display
1453 line where we wound up. */
1454 top_y = it3.current_y;
1455 if (it3.bidi_p)
1456 {
1457 /* When characters are reordered for display,
1458 the character displayed to the left of the
1459 display string could be _after_ the display
1460 property in the logical order. Use the
1461 smallest vertical position of these two. */
1462 start_display (&it3, w, top);
1463 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1464 if (it3.current_y < top_y)
1465 top_y = it3.current_y;
1466 }
1467 /* Move from the top of the window to the beginning
1468 of the display line where the display string
1469 begins. */
1470 start_display (&it3, w, top);
1471 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1472 /* If it3_moved stays zero after the 'while' loop
1473 below, that means we already were at a newline
1474 before the loop (e.g., the display string begins
1475 with a newline), so we don't need to (and cannot)
1476 inspect the glyphs of it3.glyph_row, because
1477 PRODUCE_GLYPHS will not produce anything for a
1478 newline, and thus it3.glyph_row stays at its
1479 stale content it got at top of the window. */
1480 it3_moved = 0;
1481 /* Finally, advance the iterator until we hit the
1482 first display element whose character position is
1483 CHARPOS, or until the first newline from the
1484 display string, which signals the end of the
1485 display line. */
1486 while (get_next_display_element (&it3))
1487 {
1488 PRODUCE_GLYPHS (&it3);
1489 if (IT_CHARPOS (it3) == charpos
1490 || ITERATOR_AT_END_OF_LINE_P (&it3))
1491 break;
1492 it3_moved = 1;
1493 set_iterator_to_next (&it3, 0);
1494 }
1495 top_x = it3.current_x - it3.pixel_width;
1496 /* Normally, we would exit the above loop because we
1497 found the display element whose character
1498 position is CHARPOS. For the contingency that we
1499 didn't, and stopped at the first newline from the
1500 display string, move back over the glyphs
1501 produced from the string, until we find the
1502 rightmost glyph not from the string. */
1503 if (it3_moved
1504 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1505 {
1506 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1507 + it3.glyph_row->used[TEXT_AREA];
1508
1509 while (EQ ((g - 1)->object, string))
1510 {
1511 --g;
1512 top_x -= g->pixel_width;
1513 }
1514 xassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1515 + it3.glyph_row->used[TEXT_AREA]);
1516 }
1517 }
1518 }
1519
1520 *x = top_x;
1521 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1522 *rtop = max (0, window_top_y - top_y);
1523 *rbot = max (0, bottom_y - it.last_visible_y);
1524 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1525 - max (top_y, window_top_y)));
1526 *vpos = it.vpos;
1527 }
1528 }
1529 else
1530 {
1531 /* We were asked to provide info about WINDOW_END. */
1532 struct it it2;
1533 void *it2data = NULL;
1534
1535 SAVE_IT (it2, it, it2data);
1536 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1537 move_it_by_lines (&it, 1);
1538 if (charpos < IT_CHARPOS (it)
1539 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1540 {
1541 visible_p = 1;
1542 RESTORE_IT (&it2, &it2, it2data);
1543 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1544 *x = it2.current_x;
1545 *y = it2.current_y + it2.max_ascent - it2.ascent;
1546 *rtop = max (0, -it2.current_y);
1547 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1548 - it.last_visible_y));
1549 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1550 it.last_visible_y)
1551 - max (it2.current_y,
1552 WINDOW_HEADER_LINE_HEIGHT (w))));
1553 *vpos = it2.vpos;
1554 }
1555 else
1556 bidi_unshelve_cache (it2data, 1);
1557 }
1558 bidi_unshelve_cache (itdata, 0);
1559
1560 if (old_buffer)
1561 set_buffer_internal_1 (old_buffer);
1562
1563 current_header_line_height = current_mode_line_height = -1;
1564
1565 if (visible_p && XFASTINT (w->hscroll) > 0)
1566 *x -= XFASTINT (w->hscroll) * WINDOW_FRAME_COLUMN_WIDTH (w);
1567
1568 #if 0
1569 /* Debugging code. */
1570 if (visible_p)
1571 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1572 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1573 else
1574 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1575 #endif
1576
1577 return visible_p;
1578 }
1579
1580
1581 /* Return the next character from STR. Return in *LEN the length of
1582 the character. This is like STRING_CHAR_AND_LENGTH but never
1583 returns an invalid character. If we find one, we return a `?', but
1584 with the length of the invalid character. */
1585
1586 static inline int
1587 string_char_and_length (const unsigned char *str, int *len)
1588 {
1589 int c;
1590
1591 c = STRING_CHAR_AND_LENGTH (str, *len);
1592 if (!CHAR_VALID_P (c))
1593 /* We may not change the length here because other places in Emacs
1594 don't use this function, i.e. they silently accept invalid
1595 characters. */
1596 c = '?';
1597
1598 return c;
1599 }
1600
1601
1602
1603 /* Given a position POS containing a valid character and byte position
1604 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1605
1606 static struct text_pos
1607 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1608 {
1609 xassert (STRINGP (string) && nchars >= 0);
1610
1611 if (STRING_MULTIBYTE (string))
1612 {
1613 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1614 int len;
1615
1616 while (nchars--)
1617 {
1618 string_char_and_length (p, &len);
1619 p += len;
1620 CHARPOS (pos) += 1;
1621 BYTEPOS (pos) += len;
1622 }
1623 }
1624 else
1625 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1626
1627 return pos;
1628 }
1629
1630
1631 /* Value is the text position, i.e. character and byte position,
1632 for character position CHARPOS in STRING. */
1633
1634 static inline struct text_pos
1635 string_pos (ptrdiff_t charpos, Lisp_Object string)
1636 {
1637 struct text_pos pos;
1638 xassert (STRINGP (string));
1639 xassert (charpos >= 0);
1640 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1641 return pos;
1642 }
1643
1644
1645 /* Value is a text position, i.e. character and byte position, for
1646 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1647 means recognize multibyte characters. */
1648
1649 static struct text_pos
1650 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1651 {
1652 struct text_pos pos;
1653
1654 xassert (s != NULL);
1655 xassert (charpos >= 0);
1656
1657 if (multibyte_p)
1658 {
1659 int len;
1660
1661 SET_TEXT_POS (pos, 0, 0);
1662 while (charpos--)
1663 {
1664 string_char_and_length ((const unsigned char *) s, &len);
1665 s += len;
1666 CHARPOS (pos) += 1;
1667 BYTEPOS (pos) += len;
1668 }
1669 }
1670 else
1671 SET_TEXT_POS (pos, charpos, charpos);
1672
1673 return pos;
1674 }
1675
1676
1677 /* Value is the number of characters in C string S. MULTIBYTE_P
1678 non-zero means recognize multibyte characters. */
1679
1680 static ptrdiff_t
1681 number_of_chars (const char *s, int multibyte_p)
1682 {
1683 ptrdiff_t nchars;
1684
1685 if (multibyte_p)
1686 {
1687 ptrdiff_t rest = strlen (s);
1688 int len;
1689 const unsigned char *p = (const unsigned char *) s;
1690
1691 for (nchars = 0; rest > 0; ++nchars)
1692 {
1693 string_char_and_length (p, &len);
1694 rest -= len, p += len;
1695 }
1696 }
1697 else
1698 nchars = strlen (s);
1699
1700 return nchars;
1701 }
1702
1703
1704 /* Compute byte position NEWPOS->bytepos corresponding to
1705 NEWPOS->charpos. POS is a known position in string STRING.
1706 NEWPOS->charpos must be >= POS.charpos. */
1707
1708 static void
1709 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1710 {
1711 xassert (STRINGP (string));
1712 xassert (CHARPOS (*newpos) >= CHARPOS (pos));
1713
1714 if (STRING_MULTIBYTE (string))
1715 *newpos = string_pos_nchars_ahead (pos, string,
1716 CHARPOS (*newpos) - CHARPOS (pos));
1717 else
1718 BYTEPOS (*newpos) = CHARPOS (*newpos);
1719 }
1720
1721 /* EXPORT:
1722 Return an estimation of the pixel height of mode or header lines on
1723 frame F. FACE_ID specifies what line's height to estimate. */
1724
1725 int
1726 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1727 {
1728 #ifdef HAVE_WINDOW_SYSTEM
1729 if (FRAME_WINDOW_P (f))
1730 {
1731 int height = FONT_HEIGHT (FRAME_FONT (f));
1732
1733 /* This function is called so early when Emacs starts that the face
1734 cache and mode line face are not yet initialized. */
1735 if (FRAME_FACE_CACHE (f))
1736 {
1737 struct face *face = FACE_FROM_ID (f, face_id);
1738 if (face)
1739 {
1740 if (face->font)
1741 height = FONT_HEIGHT (face->font);
1742 if (face->box_line_width > 0)
1743 height += 2 * face->box_line_width;
1744 }
1745 }
1746
1747 return height;
1748 }
1749 #endif
1750
1751 return 1;
1752 }
1753
1754 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1755 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1756 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1757 not force the value into range. */
1758
1759 void
1760 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1761 int *x, int *y, NativeRectangle *bounds, int noclip)
1762 {
1763
1764 #ifdef HAVE_WINDOW_SYSTEM
1765 if (FRAME_WINDOW_P (f))
1766 {
1767 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1768 even for negative values. */
1769 if (pix_x < 0)
1770 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1771 if (pix_y < 0)
1772 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1773
1774 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1775 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1776
1777 if (bounds)
1778 STORE_NATIVE_RECT (*bounds,
1779 FRAME_COL_TO_PIXEL_X (f, pix_x),
1780 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1781 FRAME_COLUMN_WIDTH (f) - 1,
1782 FRAME_LINE_HEIGHT (f) - 1);
1783
1784 if (!noclip)
1785 {
1786 if (pix_x < 0)
1787 pix_x = 0;
1788 else if (pix_x > FRAME_TOTAL_COLS (f))
1789 pix_x = FRAME_TOTAL_COLS (f);
1790
1791 if (pix_y < 0)
1792 pix_y = 0;
1793 else if (pix_y > FRAME_LINES (f))
1794 pix_y = FRAME_LINES (f);
1795 }
1796 }
1797 #endif
1798
1799 *x = pix_x;
1800 *y = pix_y;
1801 }
1802
1803
1804 /* Find the glyph under window-relative coordinates X/Y in window W.
1805 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1806 strings. Return in *HPOS and *VPOS the row and column number of
1807 the glyph found. Return in *AREA the glyph area containing X.
1808 Value is a pointer to the glyph found or null if X/Y is not on
1809 text, or we can't tell because W's current matrix is not up to
1810 date. */
1811
1812 static
1813 struct glyph *
1814 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1815 int *dx, int *dy, int *area)
1816 {
1817 struct glyph *glyph, *end;
1818 struct glyph_row *row = NULL;
1819 int x0, i;
1820
1821 /* Find row containing Y. Give up if some row is not enabled. */
1822 for (i = 0; i < w->current_matrix->nrows; ++i)
1823 {
1824 row = MATRIX_ROW (w->current_matrix, i);
1825 if (!row->enabled_p)
1826 return NULL;
1827 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1828 break;
1829 }
1830
1831 *vpos = i;
1832 *hpos = 0;
1833
1834 /* Give up if Y is not in the window. */
1835 if (i == w->current_matrix->nrows)
1836 return NULL;
1837
1838 /* Get the glyph area containing X. */
1839 if (w->pseudo_window_p)
1840 {
1841 *area = TEXT_AREA;
1842 x0 = 0;
1843 }
1844 else
1845 {
1846 if (x < window_box_left_offset (w, TEXT_AREA))
1847 {
1848 *area = LEFT_MARGIN_AREA;
1849 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1850 }
1851 else if (x < window_box_right_offset (w, TEXT_AREA))
1852 {
1853 *area = TEXT_AREA;
1854 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1855 }
1856 else
1857 {
1858 *area = RIGHT_MARGIN_AREA;
1859 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1860 }
1861 }
1862
1863 /* Find glyph containing X. */
1864 glyph = row->glyphs[*area];
1865 end = glyph + row->used[*area];
1866 x -= x0;
1867 while (glyph < end && x >= glyph->pixel_width)
1868 {
1869 x -= glyph->pixel_width;
1870 ++glyph;
1871 }
1872
1873 if (glyph == end)
1874 return NULL;
1875
1876 if (dx)
1877 {
1878 *dx = x;
1879 *dy = y - (row->y + row->ascent - glyph->ascent);
1880 }
1881
1882 *hpos = glyph - row->glyphs[*area];
1883 return glyph;
1884 }
1885
1886 /* Convert frame-relative x/y to coordinates relative to window W.
1887 Takes pseudo-windows into account. */
1888
1889 static void
1890 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1891 {
1892 if (w->pseudo_window_p)
1893 {
1894 /* A pseudo-window is always full-width, and starts at the
1895 left edge of the frame, plus a frame border. */
1896 struct frame *f = XFRAME (w->frame);
1897 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1898 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1899 }
1900 else
1901 {
1902 *x -= WINDOW_LEFT_EDGE_X (w);
1903 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1904 }
1905 }
1906
1907 #ifdef HAVE_WINDOW_SYSTEM
1908
1909 /* EXPORT:
1910 Return in RECTS[] at most N clipping rectangles for glyph string S.
1911 Return the number of stored rectangles. */
1912
1913 int
1914 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1915 {
1916 XRectangle r;
1917
1918 if (n <= 0)
1919 return 0;
1920
1921 if (s->row->full_width_p)
1922 {
1923 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1924 r.x = WINDOW_LEFT_EDGE_X (s->w);
1925 r.width = WINDOW_TOTAL_WIDTH (s->w);
1926
1927 /* Unless displaying a mode or menu bar line, which are always
1928 fully visible, clip to the visible part of the row. */
1929 if (s->w->pseudo_window_p)
1930 r.height = s->row->visible_height;
1931 else
1932 r.height = s->height;
1933 }
1934 else
1935 {
1936 /* This is a text line that may be partially visible. */
1937 r.x = window_box_left (s->w, s->area);
1938 r.width = window_box_width (s->w, s->area);
1939 r.height = s->row->visible_height;
1940 }
1941
1942 if (s->clip_head)
1943 if (r.x < s->clip_head->x)
1944 {
1945 if (r.width >= s->clip_head->x - r.x)
1946 r.width -= s->clip_head->x - r.x;
1947 else
1948 r.width = 0;
1949 r.x = s->clip_head->x;
1950 }
1951 if (s->clip_tail)
1952 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1953 {
1954 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1955 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1956 else
1957 r.width = 0;
1958 }
1959
1960 /* If S draws overlapping rows, it's sufficient to use the top and
1961 bottom of the window for clipping because this glyph string
1962 intentionally draws over other lines. */
1963 if (s->for_overlaps)
1964 {
1965 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1966 r.height = window_text_bottom_y (s->w) - r.y;
1967
1968 /* Alas, the above simple strategy does not work for the
1969 environments with anti-aliased text: if the same text is
1970 drawn onto the same place multiple times, it gets thicker.
1971 If the overlap we are processing is for the erased cursor, we
1972 take the intersection with the rectangle of the cursor. */
1973 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1974 {
1975 XRectangle rc, r_save = r;
1976
1977 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1978 rc.y = s->w->phys_cursor.y;
1979 rc.width = s->w->phys_cursor_width;
1980 rc.height = s->w->phys_cursor_height;
1981
1982 x_intersect_rectangles (&r_save, &rc, &r);
1983 }
1984 }
1985 else
1986 {
1987 /* Don't use S->y for clipping because it doesn't take partially
1988 visible lines into account. For example, it can be negative for
1989 partially visible lines at the top of a window. */
1990 if (!s->row->full_width_p
1991 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
1992 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1993 else
1994 r.y = max (0, s->row->y);
1995 }
1996
1997 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
1998
1999 /* If drawing the cursor, don't let glyph draw outside its
2000 advertised boundaries. Cleartype does this under some circumstances. */
2001 if (s->hl == DRAW_CURSOR)
2002 {
2003 struct glyph *glyph = s->first_glyph;
2004 int height, max_y;
2005
2006 if (s->x > r.x)
2007 {
2008 r.width -= s->x - r.x;
2009 r.x = s->x;
2010 }
2011 r.width = min (r.width, glyph->pixel_width);
2012
2013 /* If r.y is below window bottom, ensure that we still see a cursor. */
2014 height = min (glyph->ascent + glyph->descent,
2015 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2016 max_y = window_text_bottom_y (s->w) - height;
2017 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2018 if (s->ybase - glyph->ascent > max_y)
2019 {
2020 r.y = max_y;
2021 r.height = height;
2022 }
2023 else
2024 {
2025 /* Don't draw cursor glyph taller than our actual glyph. */
2026 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2027 if (height < r.height)
2028 {
2029 max_y = r.y + r.height;
2030 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2031 r.height = min (max_y - r.y, height);
2032 }
2033 }
2034 }
2035
2036 if (s->row->clip)
2037 {
2038 XRectangle r_save = r;
2039
2040 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2041 r.width = 0;
2042 }
2043
2044 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2045 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2046 {
2047 #ifdef CONVERT_FROM_XRECT
2048 CONVERT_FROM_XRECT (r, *rects);
2049 #else
2050 *rects = r;
2051 #endif
2052 return 1;
2053 }
2054 else
2055 {
2056 /* If we are processing overlapping and allowed to return
2057 multiple clipping rectangles, we exclude the row of the glyph
2058 string from the clipping rectangle. This is to avoid drawing
2059 the same text on the environment with anti-aliasing. */
2060 #ifdef CONVERT_FROM_XRECT
2061 XRectangle rs[2];
2062 #else
2063 XRectangle *rs = rects;
2064 #endif
2065 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2066
2067 if (s->for_overlaps & OVERLAPS_PRED)
2068 {
2069 rs[i] = r;
2070 if (r.y + r.height > row_y)
2071 {
2072 if (r.y < row_y)
2073 rs[i].height = row_y - r.y;
2074 else
2075 rs[i].height = 0;
2076 }
2077 i++;
2078 }
2079 if (s->for_overlaps & OVERLAPS_SUCC)
2080 {
2081 rs[i] = r;
2082 if (r.y < row_y + s->row->visible_height)
2083 {
2084 if (r.y + r.height > row_y + s->row->visible_height)
2085 {
2086 rs[i].y = row_y + s->row->visible_height;
2087 rs[i].height = r.y + r.height - rs[i].y;
2088 }
2089 else
2090 rs[i].height = 0;
2091 }
2092 i++;
2093 }
2094
2095 n = i;
2096 #ifdef CONVERT_FROM_XRECT
2097 for (i = 0; i < n; i++)
2098 CONVERT_FROM_XRECT (rs[i], rects[i]);
2099 #endif
2100 return n;
2101 }
2102 }
2103
2104 /* EXPORT:
2105 Return in *NR the clipping rectangle for glyph string S. */
2106
2107 void
2108 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2109 {
2110 get_glyph_string_clip_rects (s, nr, 1);
2111 }
2112
2113
2114 /* EXPORT:
2115 Return the position and height of the phys cursor in window W.
2116 Set w->phys_cursor_width to width of phys cursor.
2117 */
2118
2119 void
2120 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2121 struct glyph *glyph, int *xp, int *yp, int *heightp)
2122 {
2123 struct frame *f = XFRAME (WINDOW_FRAME (w));
2124 int x, y, wd, h, h0, y0;
2125
2126 /* Compute the width of the rectangle to draw. If on a stretch
2127 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2128 rectangle as wide as the glyph, but use a canonical character
2129 width instead. */
2130 wd = glyph->pixel_width - 1;
2131 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2132 wd++; /* Why? */
2133 #endif
2134
2135 x = w->phys_cursor.x;
2136 if (x < 0)
2137 {
2138 wd += x;
2139 x = 0;
2140 }
2141
2142 if (glyph->type == STRETCH_GLYPH
2143 && !x_stretch_cursor_p)
2144 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2145 w->phys_cursor_width = wd;
2146
2147 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2148
2149 /* If y is below window bottom, ensure that we still see a cursor. */
2150 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2151
2152 h = max (h0, glyph->ascent + glyph->descent);
2153 h0 = min (h0, glyph->ascent + glyph->descent);
2154
2155 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2156 if (y < y0)
2157 {
2158 h = max (h - (y0 - y) + 1, h0);
2159 y = y0 - 1;
2160 }
2161 else
2162 {
2163 y0 = window_text_bottom_y (w) - h0;
2164 if (y > y0)
2165 {
2166 h += y - y0;
2167 y = y0;
2168 }
2169 }
2170
2171 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2172 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2173 *heightp = h;
2174 }
2175
2176 /*
2177 * Remember which glyph the mouse is over.
2178 */
2179
2180 void
2181 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2182 {
2183 Lisp_Object window;
2184 struct window *w;
2185 struct glyph_row *r, *gr, *end_row;
2186 enum window_part part;
2187 enum glyph_row_area area;
2188 int x, y, width, height;
2189
2190 /* Try to determine frame pixel position and size of the glyph under
2191 frame pixel coordinates X/Y on frame F. */
2192
2193 if (!f->glyphs_initialized_p
2194 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2195 NILP (window)))
2196 {
2197 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2198 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2199 goto virtual_glyph;
2200 }
2201
2202 w = XWINDOW (window);
2203 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2204 height = WINDOW_FRAME_LINE_HEIGHT (w);
2205
2206 x = window_relative_x_coord (w, part, gx);
2207 y = gy - WINDOW_TOP_EDGE_Y (w);
2208
2209 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2210 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2211
2212 if (w->pseudo_window_p)
2213 {
2214 area = TEXT_AREA;
2215 part = ON_MODE_LINE; /* Don't adjust margin. */
2216 goto text_glyph;
2217 }
2218
2219 switch (part)
2220 {
2221 case ON_LEFT_MARGIN:
2222 area = LEFT_MARGIN_AREA;
2223 goto text_glyph;
2224
2225 case ON_RIGHT_MARGIN:
2226 area = RIGHT_MARGIN_AREA;
2227 goto text_glyph;
2228
2229 case ON_HEADER_LINE:
2230 case ON_MODE_LINE:
2231 gr = (part == ON_HEADER_LINE
2232 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2233 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2234 gy = gr->y;
2235 area = TEXT_AREA;
2236 goto text_glyph_row_found;
2237
2238 case ON_TEXT:
2239 area = TEXT_AREA;
2240
2241 text_glyph:
2242 gr = 0; gy = 0;
2243 for (; r <= end_row && r->enabled_p; ++r)
2244 if (r->y + r->height > y)
2245 {
2246 gr = r; gy = r->y;
2247 break;
2248 }
2249
2250 text_glyph_row_found:
2251 if (gr && gy <= y)
2252 {
2253 struct glyph *g = gr->glyphs[area];
2254 struct glyph *end = g + gr->used[area];
2255
2256 height = gr->height;
2257 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2258 if (gx + g->pixel_width > x)
2259 break;
2260
2261 if (g < end)
2262 {
2263 if (g->type == IMAGE_GLYPH)
2264 {
2265 /* Don't remember when mouse is over image, as
2266 image may have hot-spots. */
2267 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2268 return;
2269 }
2270 width = g->pixel_width;
2271 }
2272 else
2273 {
2274 /* Use nominal char spacing at end of line. */
2275 x -= gx;
2276 gx += (x / width) * width;
2277 }
2278
2279 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2280 gx += window_box_left_offset (w, area);
2281 }
2282 else
2283 {
2284 /* Use nominal line height at end of window. */
2285 gx = (x / width) * width;
2286 y -= gy;
2287 gy += (y / height) * height;
2288 }
2289 break;
2290
2291 case ON_LEFT_FRINGE:
2292 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2293 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2294 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2295 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2296 goto row_glyph;
2297
2298 case ON_RIGHT_FRINGE:
2299 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2300 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2301 : window_box_right_offset (w, TEXT_AREA));
2302 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2303 goto row_glyph;
2304
2305 case ON_SCROLL_BAR:
2306 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2307 ? 0
2308 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2309 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2310 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2311 : 0)));
2312 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2313
2314 row_glyph:
2315 gr = 0, gy = 0;
2316 for (; r <= end_row && r->enabled_p; ++r)
2317 if (r->y + r->height > y)
2318 {
2319 gr = r; gy = r->y;
2320 break;
2321 }
2322
2323 if (gr && gy <= y)
2324 height = gr->height;
2325 else
2326 {
2327 /* Use nominal line height at end of window. */
2328 y -= gy;
2329 gy += (y / height) * height;
2330 }
2331 break;
2332
2333 default:
2334 ;
2335 virtual_glyph:
2336 /* If there is no glyph under the mouse, then we divide the screen
2337 into a grid of the smallest glyph in the frame, and use that
2338 as our "glyph". */
2339
2340 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2341 round down even for negative values. */
2342 if (gx < 0)
2343 gx -= width - 1;
2344 if (gy < 0)
2345 gy -= height - 1;
2346
2347 gx = (gx / width) * width;
2348 gy = (gy / height) * height;
2349
2350 goto store_rect;
2351 }
2352
2353 gx += WINDOW_LEFT_EDGE_X (w);
2354 gy += WINDOW_TOP_EDGE_Y (w);
2355
2356 store_rect:
2357 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2358
2359 /* Visible feedback for debugging. */
2360 #if 0
2361 #if HAVE_X_WINDOWS
2362 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2363 f->output_data.x->normal_gc,
2364 gx, gy, width, height);
2365 #endif
2366 #endif
2367 }
2368
2369
2370 #endif /* HAVE_WINDOW_SYSTEM */
2371
2372 \f
2373 /***********************************************************************
2374 Lisp form evaluation
2375 ***********************************************************************/
2376
2377 /* Error handler for safe_eval and safe_call. */
2378
2379 static Lisp_Object
2380 safe_eval_handler (Lisp_Object arg)
2381 {
2382 add_to_log ("Error during redisplay: %S", arg, Qnil);
2383 return Qnil;
2384 }
2385
2386
2387 /* Evaluate SEXPR and return the result, or nil if something went
2388 wrong. Prevent redisplay during the evaluation. */
2389
2390 /* Call function ARGS[0] with arguments ARGS[1] to ARGS[NARGS - 1].
2391 Return the result, or nil if something went wrong. Prevent
2392 redisplay during the evaluation. */
2393
2394 Lisp_Object
2395 safe_call (ptrdiff_t nargs, Lisp_Object *args)
2396 {
2397 Lisp_Object val;
2398
2399 if (inhibit_eval_during_redisplay)
2400 val = Qnil;
2401 else
2402 {
2403 ptrdiff_t count = SPECPDL_INDEX ();
2404 struct gcpro gcpro1;
2405
2406 GCPRO1 (args[0]);
2407 gcpro1.nvars = nargs;
2408 specbind (Qinhibit_redisplay, Qt);
2409 /* Use Qt to ensure debugger does not run,
2410 so there is no possibility of wanting to redisplay. */
2411 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2412 safe_eval_handler);
2413 UNGCPRO;
2414 val = unbind_to (count, val);
2415 }
2416
2417 return val;
2418 }
2419
2420
2421 /* Call function FN with one argument ARG.
2422 Return the result, or nil if something went wrong. */
2423
2424 Lisp_Object
2425 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2426 {
2427 Lisp_Object args[2];
2428 args[0] = fn;
2429 args[1] = arg;
2430 return safe_call (2, args);
2431 }
2432
2433 static Lisp_Object Qeval;
2434
2435 Lisp_Object
2436 safe_eval (Lisp_Object sexpr)
2437 {
2438 return safe_call1 (Qeval, sexpr);
2439 }
2440
2441 /* Call function FN with one argument ARG.
2442 Return the result, or nil if something went wrong. */
2443
2444 Lisp_Object
2445 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2446 {
2447 Lisp_Object args[3];
2448 args[0] = fn;
2449 args[1] = arg1;
2450 args[2] = arg2;
2451 return safe_call (3, args);
2452 }
2453
2454
2455 \f
2456 /***********************************************************************
2457 Debugging
2458 ***********************************************************************/
2459
2460 #if 0
2461
2462 /* Define CHECK_IT to perform sanity checks on iterators.
2463 This is for debugging. It is too slow to do unconditionally. */
2464
2465 static void
2466 check_it (struct it *it)
2467 {
2468 if (it->method == GET_FROM_STRING)
2469 {
2470 xassert (STRINGP (it->string));
2471 xassert (IT_STRING_CHARPOS (*it) >= 0);
2472 }
2473 else
2474 {
2475 xassert (IT_STRING_CHARPOS (*it) < 0);
2476 if (it->method == GET_FROM_BUFFER)
2477 {
2478 /* Check that character and byte positions agree. */
2479 xassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2480 }
2481 }
2482
2483 if (it->dpvec)
2484 xassert (it->current.dpvec_index >= 0);
2485 else
2486 xassert (it->current.dpvec_index < 0);
2487 }
2488
2489 #define CHECK_IT(IT) check_it ((IT))
2490
2491 #else /* not 0 */
2492
2493 #define CHECK_IT(IT) (void) 0
2494
2495 #endif /* not 0 */
2496
2497
2498 #if GLYPH_DEBUG && XASSERTS
2499
2500 /* Check that the window end of window W is what we expect it
2501 to be---the last row in the current matrix displaying text. */
2502
2503 static void
2504 check_window_end (struct window *w)
2505 {
2506 if (!MINI_WINDOW_P (w)
2507 && !NILP (w->window_end_valid))
2508 {
2509 struct glyph_row *row;
2510 xassert ((row = MATRIX_ROW (w->current_matrix,
2511 XFASTINT (w->window_end_vpos)),
2512 !row->enabled_p
2513 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2514 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2515 }
2516 }
2517
2518 #define CHECK_WINDOW_END(W) check_window_end ((W))
2519
2520 #else
2521
2522 #define CHECK_WINDOW_END(W) (void) 0
2523
2524 #endif
2525
2526
2527 \f
2528 /***********************************************************************
2529 Iterator initialization
2530 ***********************************************************************/
2531
2532 /* Initialize IT for displaying current_buffer in window W, starting
2533 at character position CHARPOS. CHARPOS < 0 means that no buffer
2534 position is specified which is useful when the iterator is assigned
2535 a position later. BYTEPOS is the byte position corresponding to
2536 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2537
2538 If ROW is not null, calls to produce_glyphs with IT as parameter
2539 will produce glyphs in that row.
2540
2541 BASE_FACE_ID is the id of a base face to use. It must be one of
2542 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2543 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2544 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2545
2546 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2547 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2548 will be initialized to use the corresponding mode line glyph row of
2549 the desired matrix of W. */
2550
2551 void
2552 init_iterator (struct it *it, struct window *w,
2553 ptrdiff_t charpos, ptrdiff_t bytepos,
2554 struct glyph_row *row, enum face_id base_face_id)
2555 {
2556 int highlight_region_p;
2557 enum face_id remapped_base_face_id = base_face_id;
2558
2559 /* Some precondition checks. */
2560 xassert (w != NULL && it != NULL);
2561 xassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2562 && charpos <= ZV));
2563
2564 /* If face attributes have been changed since the last redisplay,
2565 free realized faces now because they depend on face definitions
2566 that might have changed. Don't free faces while there might be
2567 desired matrices pending which reference these faces. */
2568 if (face_change_count && !inhibit_free_realized_faces)
2569 {
2570 face_change_count = 0;
2571 free_all_realized_faces (Qnil);
2572 }
2573
2574 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2575 if (! NILP (Vface_remapping_alist))
2576 remapped_base_face_id = lookup_basic_face (XFRAME (w->frame), base_face_id);
2577
2578 /* Use one of the mode line rows of W's desired matrix if
2579 appropriate. */
2580 if (row == NULL)
2581 {
2582 if (base_face_id == MODE_LINE_FACE_ID
2583 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2584 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2585 else if (base_face_id == HEADER_LINE_FACE_ID)
2586 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2587 }
2588
2589 /* Clear IT. */
2590 memset (it, 0, sizeof *it);
2591 it->current.overlay_string_index = -1;
2592 it->current.dpvec_index = -1;
2593 it->base_face_id = remapped_base_face_id;
2594 it->string = Qnil;
2595 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2596 it->paragraph_embedding = L2R;
2597 it->bidi_it.string.lstring = Qnil;
2598 it->bidi_it.string.s = NULL;
2599 it->bidi_it.string.bufpos = 0;
2600
2601 /* The window in which we iterate over current_buffer: */
2602 XSETWINDOW (it->window, w);
2603 it->w = w;
2604 it->f = XFRAME (w->frame);
2605
2606 it->cmp_it.id = -1;
2607
2608 /* Extra space between lines (on window systems only). */
2609 if (base_face_id == DEFAULT_FACE_ID
2610 && FRAME_WINDOW_P (it->f))
2611 {
2612 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2613 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2614 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2615 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2616 * FRAME_LINE_HEIGHT (it->f));
2617 else if (it->f->extra_line_spacing > 0)
2618 it->extra_line_spacing = it->f->extra_line_spacing;
2619 it->max_extra_line_spacing = 0;
2620 }
2621
2622 /* If realized faces have been removed, e.g. because of face
2623 attribute changes of named faces, recompute them. When running
2624 in batch mode, the face cache of the initial frame is null. If
2625 we happen to get called, make a dummy face cache. */
2626 if (FRAME_FACE_CACHE (it->f) == NULL)
2627 init_frame_faces (it->f);
2628 if (FRAME_FACE_CACHE (it->f)->used == 0)
2629 recompute_basic_faces (it->f);
2630
2631 /* Current value of the `slice', `space-width', and 'height' properties. */
2632 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2633 it->space_width = Qnil;
2634 it->font_height = Qnil;
2635 it->override_ascent = -1;
2636
2637 /* Are control characters displayed as `^C'? */
2638 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2639
2640 /* -1 means everything between a CR and the following line end
2641 is invisible. >0 means lines indented more than this value are
2642 invisible. */
2643 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2644 ? clip_to_bounds (-1, XINT (BVAR (current_buffer,
2645 selective_display)),
2646 PTRDIFF_MAX)
2647 : (!NILP (BVAR (current_buffer, selective_display))
2648 ? -1 : 0));
2649 it->selective_display_ellipsis_p
2650 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2651
2652 /* Display table to use. */
2653 it->dp = window_display_table (w);
2654
2655 /* Are multibyte characters enabled in current_buffer? */
2656 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2657
2658 /* Non-zero if we should highlight the region. */
2659 highlight_region_p
2660 = (!NILP (Vtransient_mark_mode)
2661 && !NILP (BVAR (current_buffer, mark_active))
2662 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2663
2664 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2665 start and end of a visible region in window IT->w. Set both to
2666 -1 to indicate no region. */
2667 if (highlight_region_p
2668 /* Maybe highlight only in selected window. */
2669 && (/* Either show region everywhere. */
2670 highlight_nonselected_windows
2671 /* Or show region in the selected window. */
2672 || w == XWINDOW (selected_window)
2673 /* Or show the region if we are in the mini-buffer and W is
2674 the window the mini-buffer refers to. */
2675 || (MINI_WINDOW_P (XWINDOW (selected_window))
2676 && WINDOWP (minibuf_selected_window)
2677 && w == XWINDOW (minibuf_selected_window))))
2678 {
2679 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
2680 it->region_beg_charpos = min (PT, markpos);
2681 it->region_end_charpos = max (PT, markpos);
2682 }
2683 else
2684 it->region_beg_charpos = it->region_end_charpos = -1;
2685
2686 /* Get the position at which the redisplay_end_trigger hook should
2687 be run, if it is to be run at all. */
2688 if (MARKERP (w->redisplay_end_trigger)
2689 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2690 it->redisplay_end_trigger_charpos
2691 = marker_position (w->redisplay_end_trigger);
2692 else if (INTEGERP (w->redisplay_end_trigger))
2693 it->redisplay_end_trigger_charpos =
2694 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2695
2696 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2697
2698 /* Are lines in the display truncated? */
2699 if (base_face_id != DEFAULT_FACE_ID
2700 || XINT (it->w->hscroll)
2701 || (! WINDOW_FULL_WIDTH_P (it->w)
2702 && ((!NILP (Vtruncate_partial_width_windows)
2703 && !INTEGERP (Vtruncate_partial_width_windows))
2704 || (INTEGERP (Vtruncate_partial_width_windows)
2705 && (WINDOW_TOTAL_COLS (it->w)
2706 < XINT (Vtruncate_partial_width_windows))))))
2707 it->line_wrap = TRUNCATE;
2708 else if (NILP (BVAR (current_buffer, truncate_lines)))
2709 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2710 ? WINDOW_WRAP : WORD_WRAP;
2711 else
2712 it->line_wrap = TRUNCATE;
2713
2714 /* Get dimensions of truncation and continuation glyphs. These are
2715 displayed as fringe bitmaps under X, so we don't need them for such
2716 frames. */
2717 if (!FRAME_WINDOW_P (it->f))
2718 {
2719 if (it->line_wrap == TRUNCATE)
2720 {
2721 /* We will need the truncation glyph. */
2722 xassert (it->glyph_row == NULL);
2723 produce_special_glyphs (it, IT_TRUNCATION);
2724 it->truncation_pixel_width = it->pixel_width;
2725 }
2726 else
2727 {
2728 /* We will need the continuation glyph. */
2729 xassert (it->glyph_row == NULL);
2730 produce_special_glyphs (it, IT_CONTINUATION);
2731 it->continuation_pixel_width = it->pixel_width;
2732 }
2733
2734 /* Reset these values to zero because the produce_special_glyphs
2735 above has changed them. */
2736 it->pixel_width = it->ascent = it->descent = 0;
2737 it->phys_ascent = it->phys_descent = 0;
2738 }
2739
2740 /* Set this after getting the dimensions of truncation and
2741 continuation glyphs, so that we don't produce glyphs when calling
2742 produce_special_glyphs, above. */
2743 it->glyph_row = row;
2744 it->area = TEXT_AREA;
2745
2746 /* Forget any previous info about this row being reversed. */
2747 if (it->glyph_row)
2748 it->glyph_row->reversed_p = 0;
2749
2750 /* Get the dimensions of the display area. The display area
2751 consists of the visible window area plus a horizontally scrolled
2752 part to the left of the window. All x-values are relative to the
2753 start of this total display area. */
2754 if (base_face_id != DEFAULT_FACE_ID)
2755 {
2756 /* Mode lines, menu bar in terminal frames. */
2757 it->first_visible_x = 0;
2758 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2759 }
2760 else
2761 {
2762 it->first_visible_x
2763 = XFASTINT (it->w->hscroll) * FRAME_COLUMN_WIDTH (it->f);
2764 it->last_visible_x = (it->first_visible_x
2765 + window_box_width (w, TEXT_AREA));
2766
2767 /* If we truncate lines, leave room for the truncator glyph(s) at
2768 the right margin. Otherwise, leave room for the continuation
2769 glyph(s). Truncation and continuation glyphs are not inserted
2770 for window-based redisplay. */
2771 if (!FRAME_WINDOW_P (it->f))
2772 {
2773 if (it->line_wrap == TRUNCATE)
2774 it->last_visible_x -= it->truncation_pixel_width;
2775 else
2776 it->last_visible_x -= it->continuation_pixel_width;
2777 }
2778
2779 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2780 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2781 }
2782
2783 /* Leave room for a border glyph. */
2784 if (!FRAME_WINDOW_P (it->f)
2785 && !WINDOW_RIGHTMOST_P (it->w))
2786 it->last_visible_x -= 1;
2787
2788 it->last_visible_y = window_text_bottom_y (w);
2789
2790 /* For mode lines and alike, arrange for the first glyph having a
2791 left box line if the face specifies a box. */
2792 if (base_face_id != DEFAULT_FACE_ID)
2793 {
2794 struct face *face;
2795
2796 it->face_id = remapped_base_face_id;
2797
2798 /* If we have a boxed mode line, make the first character appear
2799 with a left box line. */
2800 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2801 if (face->box != FACE_NO_BOX)
2802 it->start_of_box_run_p = 1;
2803 }
2804
2805 /* If a buffer position was specified, set the iterator there,
2806 getting overlays and face properties from that position. */
2807 if (charpos >= BUF_BEG (current_buffer))
2808 {
2809 it->end_charpos = ZV;
2810 IT_CHARPOS (*it) = charpos;
2811
2812 /* We will rely on `reseat' to set this up properly, via
2813 handle_face_prop. */
2814 it->face_id = it->base_face_id;
2815
2816 /* Compute byte position if not specified. */
2817 if (bytepos < charpos)
2818 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2819 else
2820 IT_BYTEPOS (*it) = bytepos;
2821
2822 it->start = it->current;
2823 /* Do we need to reorder bidirectional text? Not if this is a
2824 unibyte buffer: by definition, none of the single-byte
2825 characters are strong R2L, so no reordering is needed. And
2826 bidi.c doesn't support unibyte buffers anyway. Also, don't
2827 reorder while we are loading loadup.el, since the tables of
2828 character properties needed for reordering are not yet
2829 available. */
2830 it->bidi_p =
2831 NILP (Vpurify_flag)
2832 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2833 && it->multibyte_p;
2834
2835 /* If we are to reorder bidirectional text, init the bidi
2836 iterator. */
2837 if (it->bidi_p)
2838 {
2839 /* Note the paragraph direction that this buffer wants to
2840 use. */
2841 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2842 Qleft_to_right))
2843 it->paragraph_embedding = L2R;
2844 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2845 Qright_to_left))
2846 it->paragraph_embedding = R2L;
2847 else
2848 it->paragraph_embedding = NEUTRAL_DIR;
2849 bidi_unshelve_cache (NULL, 0);
2850 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2851 &it->bidi_it);
2852 }
2853
2854 /* Compute faces etc. */
2855 reseat (it, it->current.pos, 1);
2856 }
2857
2858 CHECK_IT (it);
2859 }
2860
2861
2862 /* Initialize IT for the display of window W with window start POS. */
2863
2864 void
2865 start_display (struct it *it, struct window *w, struct text_pos pos)
2866 {
2867 struct glyph_row *row;
2868 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2869
2870 row = w->desired_matrix->rows + first_vpos;
2871 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2872 it->first_vpos = first_vpos;
2873
2874 /* Don't reseat to previous visible line start if current start
2875 position is in a string or image. */
2876 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2877 {
2878 int start_at_line_beg_p;
2879 int first_y = it->current_y;
2880
2881 /* If window start is not at a line start, skip forward to POS to
2882 get the correct continuation lines width. */
2883 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2884 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2885 if (!start_at_line_beg_p)
2886 {
2887 int new_x;
2888
2889 reseat_at_previous_visible_line_start (it);
2890 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2891
2892 new_x = it->current_x + it->pixel_width;
2893
2894 /* If lines are continued, this line may end in the middle
2895 of a multi-glyph character (e.g. a control character
2896 displayed as \003, or in the middle of an overlay
2897 string). In this case move_it_to above will not have
2898 taken us to the start of the continuation line but to the
2899 end of the continued line. */
2900 if (it->current_x > 0
2901 && it->line_wrap != TRUNCATE /* Lines are continued. */
2902 && (/* And glyph doesn't fit on the line. */
2903 new_x > it->last_visible_x
2904 /* Or it fits exactly and we're on a window
2905 system frame. */
2906 || (new_x == it->last_visible_x
2907 && FRAME_WINDOW_P (it->f))))
2908 {
2909 if ((it->current.dpvec_index >= 0
2910 || it->current.overlay_string_index >= 0)
2911 /* If we are on a newline from a display vector or
2912 overlay string, then we are already at the end of
2913 a screen line; no need to go to the next line in
2914 that case, as this line is not really continued.
2915 (If we do go to the next line, C-e will not DTRT.) */
2916 && it->c != '\n')
2917 {
2918 set_iterator_to_next (it, 1);
2919 move_it_in_display_line_to (it, -1, -1, 0);
2920 }
2921
2922 it->continuation_lines_width += it->current_x;
2923 }
2924 /* If the character at POS is displayed via a display
2925 vector, move_it_to above stops at the final glyph of
2926 IT->dpvec. To make the caller redisplay that character
2927 again (a.k.a. start at POS), we need to reset the
2928 dpvec_index to the beginning of IT->dpvec. */
2929 else if (it->current.dpvec_index >= 0)
2930 it->current.dpvec_index = 0;
2931
2932 /* We're starting a new display line, not affected by the
2933 height of the continued line, so clear the appropriate
2934 fields in the iterator structure. */
2935 it->max_ascent = it->max_descent = 0;
2936 it->max_phys_ascent = it->max_phys_descent = 0;
2937
2938 it->current_y = first_y;
2939 it->vpos = 0;
2940 it->current_x = it->hpos = 0;
2941 }
2942 }
2943 }
2944
2945
2946 /* Return 1 if POS is a position in ellipses displayed for invisible
2947 text. W is the window we display, for text property lookup. */
2948
2949 static int
2950 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2951 {
2952 Lisp_Object prop, window;
2953 int ellipses_p = 0;
2954 ptrdiff_t charpos = CHARPOS (pos->pos);
2955
2956 /* If POS specifies a position in a display vector, this might
2957 be for an ellipsis displayed for invisible text. We won't
2958 get the iterator set up for delivering that ellipsis unless
2959 we make sure that it gets aware of the invisible text. */
2960 if (pos->dpvec_index >= 0
2961 && pos->overlay_string_index < 0
2962 && CHARPOS (pos->string_pos) < 0
2963 && charpos > BEGV
2964 && (XSETWINDOW (window, w),
2965 prop = Fget_char_property (make_number (charpos),
2966 Qinvisible, window),
2967 !TEXT_PROP_MEANS_INVISIBLE (prop)))
2968 {
2969 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
2970 window);
2971 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
2972 }
2973
2974 return ellipses_p;
2975 }
2976
2977
2978 /* Initialize IT for stepping through current_buffer in window W,
2979 starting at position POS that includes overlay string and display
2980 vector/ control character translation position information. Value
2981 is zero if there are overlay strings with newlines at POS. */
2982
2983 static int
2984 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
2985 {
2986 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
2987 int i, overlay_strings_with_newlines = 0;
2988
2989 /* If POS specifies a position in a display vector, this might
2990 be for an ellipsis displayed for invisible text. We won't
2991 get the iterator set up for delivering that ellipsis unless
2992 we make sure that it gets aware of the invisible text. */
2993 if (in_ellipses_for_invisible_text_p (pos, w))
2994 {
2995 --charpos;
2996 bytepos = 0;
2997 }
2998
2999 /* Keep in mind: the call to reseat in init_iterator skips invisible
3000 text, so we might end up at a position different from POS. This
3001 is only a problem when POS is a row start after a newline and an
3002 overlay starts there with an after-string, and the overlay has an
3003 invisible property. Since we don't skip invisible text in
3004 display_line and elsewhere immediately after consuming the
3005 newline before the row start, such a POS will not be in a string,
3006 but the call to init_iterator below will move us to the
3007 after-string. */
3008 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3009
3010 /* This only scans the current chunk -- it should scan all chunks.
3011 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3012 to 16 in 22.1 to make this a lesser problem. */
3013 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3014 {
3015 const char *s = SSDATA (it->overlay_strings[i]);
3016 const char *e = s + SBYTES (it->overlay_strings[i]);
3017
3018 while (s < e && *s != '\n')
3019 ++s;
3020
3021 if (s < e)
3022 {
3023 overlay_strings_with_newlines = 1;
3024 break;
3025 }
3026 }
3027
3028 /* If position is within an overlay string, set up IT to the right
3029 overlay string. */
3030 if (pos->overlay_string_index >= 0)
3031 {
3032 int relative_index;
3033
3034 /* If the first overlay string happens to have a `display'
3035 property for an image, the iterator will be set up for that
3036 image, and we have to undo that setup first before we can
3037 correct the overlay string index. */
3038 if (it->method == GET_FROM_IMAGE)
3039 pop_it (it);
3040
3041 /* We already have the first chunk of overlay strings in
3042 IT->overlay_strings. Load more until the one for
3043 pos->overlay_string_index is in IT->overlay_strings. */
3044 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3045 {
3046 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3047 it->current.overlay_string_index = 0;
3048 while (n--)
3049 {
3050 load_overlay_strings (it, 0);
3051 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3052 }
3053 }
3054
3055 it->current.overlay_string_index = pos->overlay_string_index;
3056 relative_index = (it->current.overlay_string_index
3057 % OVERLAY_STRING_CHUNK_SIZE);
3058 it->string = it->overlay_strings[relative_index];
3059 xassert (STRINGP (it->string));
3060 it->current.string_pos = pos->string_pos;
3061 it->method = GET_FROM_STRING;
3062 }
3063
3064 if (CHARPOS (pos->string_pos) >= 0)
3065 {
3066 /* Recorded position is not in an overlay string, but in another
3067 string. This can only be a string from a `display' property.
3068 IT should already be filled with that string. */
3069 it->current.string_pos = pos->string_pos;
3070 xassert (STRINGP (it->string));
3071 }
3072
3073 /* Restore position in display vector translations, control
3074 character translations or ellipses. */
3075 if (pos->dpvec_index >= 0)
3076 {
3077 if (it->dpvec == NULL)
3078 get_next_display_element (it);
3079 xassert (it->dpvec && it->current.dpvec_index == 0);
3080 it->current.dpvec_index = pos->dpvec_index;
3081 }
3082
3083 CHECK_IT (it);
3084 return !overlay_strings_with_newlines;
3085 }
3086
3087
3088 /* Initialize IT for stepping through current_buffer in window W
3089 starting at ROW->start. */
3090
3091 static void
3092 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3093 {
3094 init_from_display_pos (it, w, &row->start);
3095 it->start = row->start;
3096 it->continuation_lines_width = row->continuation_lines_width;
3097 CHECK_IT (it);
3098 }
3099
3100
3101 /* Initialize IT for stepping through current_buffer in window W
3102 starting in the line following ROW, i.e. starting at ROW->end.
3103 Value is zero if there are overlay strings with newlines at ROW's
3104 end position. */
3105
3106 static int
3107 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3108 {
3109 int success = 0;
3110
3111 if (init_from_display_pos (it, w, &row->end))
3112 {
3113 if (row->continued_p)
3114 it->continuation_lines_width
3115 = row->continuation_lines_width + row->pixel_width;
3116 CHECK_IT (it);
3117 success = 1;
3118 }
3119
3120 return success;
3121 }
3122
3123
3124
3125 \f
3126 /***********************************************************************
3127 Text properties
3128 ***********************************************************************/
3129
3130 /* Called when IT reaches IT->stop_charpos. Handle text property and
3131 overlay changes. Set IT->stop_charpos to the next position where
3132 to stop. */
3133
3134 static void
3135 handle_stop (struct it *it)
3136 {
3137 enum prop_handled handled;
3138 int handle_overlay_change_p;
3139 struct props *p;
3140
3141 it->dpvec = NULL;
3142 it->current.dpvec_index = -1;
3143 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3144 it->ignore_overlay_strings_at_pos_p = 0;
3145 it->ellipsis_p = 0;
3146
3147 /* Use face of preceding text for ellipsis (if invisible) */
3148 if (it->selective_display_ellipsis_p)
3149 it->saved_face_id = it->face_id;
3150
3151 do
3152 {
3153 handled = HANDLED_NORMALLY;
3154
3155 /* Call text property handlers. */
3156 for (p = it_props; p->handler; ++p)
3157 {
3158 handled = p->handler (it);
3159
3160 if (handled == HANDLED_RECOMPUTE_PROPS)
3161 break;
3162 else if (handled == HANDLED_RETURN)
3163 {
3164 /* We still want to show before and after strings from
3165 overlays even if the actual buffer text is replaced. */
3166 if (!handle_overlay_change_p
3167 || it->sp > 1
3168 /* Don't call get_overlay_strings_1 if we already
3169 have overlay strings loaded, because doing so
3170 will load them again and push the iterator state
3171 onto the stack one more time, which is not
3172 expected by the rest of the code that processes
3173 overlay strings. */
3174 || (it->current.overlay_string_index < 0
3175 ? !get_overlay_strings_1 (it, 0, 0)
3176 : 0))
3177 {
3178 if (it->ellipsis_p)
3179 setup_for_ellipsis (it, 0);
3180 /* When handling a display spec, we might load an
3181 empty string. In that case, discard it here. We
3182 used to discard it in handle_single_display_spec,
3183 but that causes get_overlay_strings_1, above, to
3184 ignore overlay strings that we must check. */
3185 if (STRINGP (it->string) && !SCHARS (it->string))
3186 pop_it (it);
3187 return;
3188 }
3189 else if (STRINGP (it->string) && !SCHARS (it->string))
3190 pop_it (it);
3191 else
3192 {
3193 it->ignore_overlay_strings_at_pos_p = 1;
3194 it->string_from_display_prop_p = 0;
3195 it->from_disp_prop_p = 0;
3196 handle_overlay_change_p = 0;
3197 }
3198 handled = HANDLED_RECOMPUTE_PROPS;
3199 break;
3200 }
3201 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3202 handle_overlay_change_p = 0;
3203 }
3204
3205 if (handled != HANDLED_RECOMPUTE_PROPS)
3206 {
3207 /* Don't check for overlay strings below when set to deliver
3208 characters from a display vector. */
3209 if (it->method == GET_FROM_DISPLAY_VECTOR)
3210 handle_overlay_change_p = 0;
3211
3212 /* Handle overlay changes.
3213 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3214 if it finds overlays. */
3215 if (handle_overlay_change_p)
3216 handled = handle_overlay_change (it);
3217 }
3218
3219 if (it->ellipsis_p)
3220 {
3221 setup_for_ellipsis (it, 0);
3222 break;
3223 }
3224 }
3225 while (handled == HANDLED_RECOMPUTE_PROPS);
3226
3227 /* Determine where to stop next. */
3228 if (handled == HANDLED_NORMALLY)
3229 compute_stop_pos (it);
3230 }
3231
3232
3233 /* Compute IT->stop_charpos from text property and overlay change
3234 information for IT's current position. */
3235
3236 static void
3237 compute_stop_pos (struct it *it)
3238 {
3239 register INTERVAL iv, next_iv;
3240 Lisp_Object object, limit, position;
3241 ptrdiff_t charpos, bytepos;
3242
3243 if (STRINGP (it->string))
3244 {
3245 /* Strings are usually short, so don't limit the search for
3246 properties. */
3247 it->stop_charpos = it->end_charpos;
3248 object = it->string;
3249 limit = Qnil;
3250 charpos = IT_STRING_CHARPOS (*it);
3251 bytepos = IT_STRING_BYTEPOS (*it);
3252 }
3253 else
3254 {
3255 ptrdiff_t pos;
3256
3257 /* If end_charpos is out of range for some reason, such as a
3258 misbehaving display function, rationalize it (Bug#5984). */
3259 if (it->end_charpos > ZV)
3260 it->end_charpos = ZV;
3261 it->stop_charpos = it->end_charpos;
3262
3263 /* If next overlay change is in front of the current stop pos
3264 (which is IT->end_charpos), stop there. Note: value of
3265 next_overlay_change is point-max if no overlay change
3266 follows. */
3267 charpos = IT_CHARPOS (*it);
3268 bytepos = IT_BYTEPOS (*it);
3269 pos = next_overlay_change (charpos);
3270 if (pos < it->stop_charpos)
3271 it->stop_charpos = pos;
3272
3273 /* If showing the region, we have to stop at the region
3274 start or end because the face might change there. */
3275 if (it->region_beg_charpos > 0)
3276 {
3277 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3278 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3279 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3280 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3281 }
3282
3283 /* Set up variables for computing the stop position from text
3284 property changes. */
3285 XSETBUFFER (object, current_buffer);
3286 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3287 }
3288
3289 /* Get the interval containing IT's position. Value is a null
3290 interval if there isn't such an interval. */
3291 position = make_number (charpos);
3292 iv = validate_interval_range (object, &position, &position, 0);
3293 if (!NULL_INTERVAL_P (iv))
3294 {
3295 Lisp_Object values_here[LAST_PROP_IDX];
3296 struct props *p;
3297
3298 /* Get properties here. */
3299 for (p = it_props; p->handler; ++p)
3300 values_here[p->idx] = textget (iv->plist, *p->name);
3301
3302 /* Look for an interval following iv that has different
3303 properties. */
3304 for (next_iv = next_interval (iv);
3305 (!NULL_INTERVAL_P (next_iv)
3306 && (NILP (limit)
3307 || XFASTINT (limit) > next_iv->position));
3308 next_iv = next_interval (next_iv))
3309 {
3310 for (p = it_props; p->handler; ++p)
3311 {
3312 Lisp_Object new_value;
3313
3314 new_value = textget (next_iv->plist, *p->name);
3315 if (!EQ (values_here[p->idx], new_value))
3316 break;
3317 }
3318
3319 if (p->handler)
3320 break;
3321 }
3322
3323 if (!NULL_INTERVAL_P (next_iv))
3324 {
3325 if (INTEGERP (limit)
3326 && next_iv->position >= XFASTINT (limit))
3327 /* No text property change up to limit. */
3328 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3329 else
3330 /* Text properties change in next_iv. */
3331 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3332 }
3333 }
3334
3335 if (it->cmp_it.id < 0)
3336 {
3337 ptrdiff_t stoppos = it->end_charpos;
3338
3339 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3340 stoppos = -1;
3341 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3342 stoppos, it->string);
3343 }
3344
3345 xassert (STRINGP (it->string)
3346 || (it->stop_charpos >= BEGV
3347 && it->stop_charpos >= IT_CHARPOS (*it)));
3348 }
3349
3350
3351 /* Return the position of the next overlay change after POS in
3352 current_buffer. Value is point-max if no overlay change
3353 follows. This is like `next-overlay-change' but doesn't use
3354 xmalloc. */
3355
3356 static ptrdiff_t
3357 next_overlay_change (ptrdiff_t pos)
3358 {
3359 ptrdiff_t i, noverlays;
3360 ptrdiff_t endpos;
3361 Lisp_Object *overlays;
3362
3363 /* Get all overlays at the given position. */
3364 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3365
3366 /* If any of these overlays ends before endpos,
3367 use its ending point instead. */
3368 for (i = 0; i < noverlays; ++i)
3369 {
3370 Lisp_Object oend;
3371 ptrdiff_t oendpos;
3372
3373 oend = OVERLAY_END (overlays[i]);
3374 oendpos = OVERLAY_POSITION (oend);
3375 endpos = min (endpos, oendpos);
3376 }
3377
3378 return endpos;
3379 }
3380
3381 /* How many characters forward to search for a display property or
3382 display string. Searching too far forward makes the bidi display
3383 sluggish, especially in small windows. */
3384 #define MAX_DISP_SCAN 250
3385
3386 /* Return the character position of a display string at or after
3387 position specified by POSITION. If no display string exists at or
3388 after POSITION, return ZV. A display string is either an overlay
3389 with `display' property whose value is a string, or a `display'
3390 text property whose value is a string. STRING is data about the
3391 string to iterate; if STRING->lstring is nil, we are iterating a
3392 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3393 on a GUI frame. DISP_PROP is set to zero if we searched
3394 MAX_DISP_SCAN characters forward without finding any display
3395 strings, non-zero otherwise. It is set to 2 if the display string
3396 uses any kind of `(space ...)' spec that will produce a stretch of
3397 white space in the text area. */
3398 ptrdiff_t
3399 compute_display_string_pos (struct text_pos *position,
3400 struct bidi_string_data *string,
3401 int frame_window_p, int *disp_prop)
3402 {
3403 /* OBJECT = nil means current buffer. */
3404 Lisp_Object object =
3405 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3406 Lisp_Object pos, spec, limpos;
3407 int string_p = (string && (STRINGP (string->lstring) || string->s));
3408 ptrdiff_t eob = string_p ? string->schars : ZV;
3409 ptrdiff_t begb = string_p ? 0 : BEGV;
3410 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3411 ptrdiff_t lim =
3412 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3413 struct text_pos tpos;
3414 int rv = 0;
3415
3416 *disp_prop = 1;
3417
3418 if (charpos >= eob
3419 /* We don't support display properties whose values are strings
3420 that have display string properties. */
3421 || string->from_disp_str
3422 /* C strings cannot have display properties. */
3423 || (string->s && !STRINGP (object)))
3424 {
3425 *disp_prop = 0;
3426 return eob;
3427 }
3428
3429 /* If the character at CHARPOS is where the display string begins,
3430 return CHARPOS. */
3431 pos = make_number (charpos);
3432 if (STRINGP (object))
3433 bufpos = string->bufpos;
3434 else
3435 bufpos = charpos;
3436 tpos = *position;
3437 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3438 && (charpos <= begb
3439 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3440 object),
3441 spec))
3442 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3443 frame_window_p)))
3444 {
3445 if (rv == 2)
3446 *disp_prop = 2;
3447 return charpos;
3448 }
3449
3450 /* Look forward for the first character with a `display' property
3451 that will replace the underlying text when displayed. */
3452 limpos = make_number (lim);
3453 do {
3454 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3455 CHARPOS (tpos) = XFASTINT (pos);
3456 if (CHARPOS (tpos) >= lim)
3457 {
3458 *disp_prop = 0;
3459 break;
3460 }
3461 if (STRINGP (object))
3462 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3463 else
3464 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3465 spec = Fget_char_property (pos, Qdisplay, object);
3466 if (!STRINGP (object))
3467 bufpos = CHARPOS (tpos);
3468 } while (NILP (spec)
3469 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3470 bufpos, frame_window_p)));
3471 if (rv == 2)
3472 *disp_prop = 2;
3473
3474 return CHARPOS (tpos);
3475 }
3476
3477 /* Return the character position of the end of the display string that
3478 started at CHARPOS. If there's no display string at CHARPOS,
3479 return -1. A display string is either an overlay with `display'
3480 property whose value is a string or a `display' text property whose
3481 value is a string. */
3482 ptrdiff_t
3483 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3484 {
3485 /* OBJECT = nil means current buffer. */
3486 Lisp_Object object =
3487 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3488 Lisp_Object pos = make_number (charpos);
3489 ptrdiff_t eob =
3490 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3491
3492 if (charpos >= eob || (string->s && !STRINGP (object)))
3493 return eob;
3494
3495 /* It could happen that the display property or overlay was removed
3496 since we found it in compute_display_string_pos above. One way
3497 this can happen is if JIT font-lock was called (through
3498 handle_fontified_prop), and jit-lock-functions remove text
3499 properties or overlays from the portion of buffer that includes
3500 CHARPOS. Muse mode is known to do that, for example. In this
3501 case, we return -1 to the caller, to signal that no display
3502 string is actually present at CHARPOS. See bidi_fetch_char for
3503 how this is handled.
3504
3505 An alternative would be to never look for display properties past
3506 it->stop_charpos. But neither compute_display_string_pos nor
3507 bidi_fetch_char that calls it know or care where the next
3508 stop_charpos is. */
3509 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3510 return -1;
3511
3512 /* Look forward for the first character where the `display' property
3513 changes. */
3514 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3515
3516 return XFASTINT (pos);
3517 }
3518
3519
3520 \f
3521 /***********************************************************************
3522 Fontification
3523 ***********************************************************************/
3524
3525 /* Handle changes in the `fontified' property of the current buffer by
3526 calling hook functions from Qfontification_functions to fontify
3527 regions of text. */
3528
3529 static enum prop_handled
3530 handle_fontified_prop (struct it *it)
3531 {
3532 Lisp_Object prop, pos;
3533 enum prop_handled handled = HANDLED_NORMALLY;
3534
3535 if (!NILP (Vmemory_full))
3536 return handled;
3537
3538 /* Get the value of the `fontified' property at IT's current buffer
3539 position. (The `fontified' property doesn't have a special
3540 meaning in strings.) If the value is nil, call functions from
3541 Qfontification_functions. */
3542 if (!STRINGP (it->string)
3543 && it->s == NULL
3544 && !NILP (Vfontification_functions)
3545 && !NILP (Vrun_hooks)
3546 && (pos = make_number (IT_CHARPOS (*it)),
3547 prop = Fget_char_property (pos, Qfontified, Qnil),
3548 /* Ignore the special cased nil value always present at EOB since
3549 no amount of fontifying will be able to change it. */
3550 NILP (prop) && IT_CHARPOS (*it) < Z))
3551 {
3552 ptrdiff_t count = SPECPDL_INDEX ();
3553 Lisp_Object val;
3554 struct buffer *obuf = current_buffer;
3555 int begv = BEGV, zv = ZV;
3556 int old_clip_changed = current_buffer->clip_changed;
3557
3558 val = Vfontification_functions;
3559 specbind (Qfontification_functions, Qnil);
3560
3561 xassert (it->end_charpos == ZV);
3562
3563 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3564 safe_call1 (val, pos);
3565 else
3566 {
3567 Lisp_Object fns, fn;
3568 struct gcpro gcpro1, gcpro2;
3569
3570 fns = Qnil;
3571 GCPRO2 (val, fns);
3572
3573 for (; CONSP (val); val = XCDR (val))
3574 {
3575 fn = XCAR (val);
3576
3577 if (EQ (fn, Qt))
3578 {
3579 /* A value of t indicates this hook has a local
3580 binding; it means to run the global binding too.
3581 In a global value, t should not occur. If it
3582 does, we must ignore it to avoid an endless
3583 loop. */
3584 for (fns = Fdefault_value (Qfontification_functions);
3585 CONSP (fns);
3586 fns = XCDR (fns))
3587 {
3588 fn = XCAR (fns);
3589 if (!EQ (fn, Qt))
3590 safe_call1 (fn, pos);
3591 }
3592 }
3593 else
3594 safe_call1 (fn, pos);
3595 }
3596
3597 UNGCPRO;
3598 }
3599
3600 unbind_to (count, Qnil);
3601
3602 /* Fontification functions routinely call `save-restriction'.
3603 Normally, this tags clip_changed, which can confuse redisplay
3604 (see discussion in Bug#6671). Since we don't perform any
3605 special handling of fontification changes in the case where
3606 `save-restriction' isn't called, there's no point doing so in
3607 this case either. So, if the buffer's restrictions are
3608 actually left unchanged, reset clip_changed. */
3609 if (obuf == current_buffer)
3610 {
3611 if (begv == BEGV && zv == ZV)
3612 current_buffer->clip_changed = old_clip_changed;
3613 }
3614 /* There isn't much we can reasonably do to protect against
3615 misbehaving fontification, but here's a fig leaf. */
3616 else if (!NILP (BVAR (obuf, name)))
3617 set_buffer_internal_1 (obuf);
3618
3619 /* The fontification code may have added/removed text.
3620 It could do even a lot worse, but let's at least protect against
3621 the most obvious case where only the text past `pos' gets changed',
3622 as is/was done in grep.el where some escapes sequences are turned
3623 into face properties (bug#7876). */
3624 it->end_charpos = ZV;
3625
3626 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3627 something. This avoids an endless loop if they failed to
3628 fontify the text for which reason ever. */
3629 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3630 handled = HANDLED_RECOMPUTE_PROPS;
3631 }
3632
3633 return handled;
3634 }
3635
3636
3637 \f
3638 /***********************************************************************
3639 Faces
3640 ***********************************************************************/
3641
3642 /* Set up iterator IT from face properties at its current position.
3643 Called from handle_stop. */
3644
3645 static enum prop_handled
3646 handle_face_prop (struct it *it)
3647 {
3648 int new_face_id;
3649 ptrdiff_t next_stop;
3650
3651 if (!STRINGP (it->string))
3652 {
3653 new_face_id
3654 = face_at_buffer_position (it->w,
3655 IT_CHARPOS (*it),
3656 it->region_beg_charpos,
3657 it->region_end_charpos,
3658 &next_stop,
3659 (IT_CHARPOS (*it)
3660 + TEXT_PROP_DISTANCE_LIMIT),
3661 0, it->base_face_id);
3662
3663 /* Is this a start of a run of characters with box face?
3664 Caveat: this can be called for a freshly initialized
3665 iterator; face_id is -1 in this case. We know that the new
3666 face will not change until limit, i.e. if the new face has a
3667 box, all characters up to limit will have one. But, as
3668 usual, we don't know whether limit is really the end. */
3669 if (new_face_id != it->face_id)
3670 {
3671 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3672
3673 /* If new face has a box but old face has not, this is
3674 the start of a run of characters with box, i.e. it has
3675 a shadow on the left side. The value of face_id of the
3676 iterator will be -1 if this is the initial call that gets
3677 the face. In this case, we have to look in front of IT's
3678 position and see whether there is a face != new_face_id. */
3679 it->start_of_box_run_p
3680 = (new_face->box != FACE_NO_BOX
3681 && (it->face_id >= 0
3682 || IT_CHARPOS (*it) == BEG
3683 || new_face_id != face_before_it_pos (it)));
3684 it->face_box_p = new_face->box != FACE_NO_BOX;
3685 }
3686 }
3687 else
3688 {
3689 int base_face_id;
3690 ptrdiff_t bufpos;
3691 int i;
3692 Lisp_Object from_overlay
3693 = (it->current.overlay_string_index >= 0
3694 ? it->string_overlays[it->current.overlay_string_index]
3695 : Qnil);
3696
3697 /* See if we got to this string directly or indirectly from
3698 an overlay property. That includes the before-string or
3699 after-string of an overlay, strings in display properties
3700 provided by an overlay, their text properties, etc.
3701
3702 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3703 if (! NILP (from_overlay))
3704 for (i = it->sp - 1; i >= 0; i--)
3705 {
3706 if (it->stack[i].current.overlay_string_index >= 0)
3707 from_overlay
3708 = it->string_overlays[it->stack[i].current.overlay_string_index];
3709 else if (! NILP (it->stack[i].from_overlay))
3710 from_overlay = it->stack[i].from_overlay;
3711
3712 if (!NILP (from_overlay))
3713 break;
3714 }
3715
3716 if (! NILP (from_overlay))
3717 {
3718 bufpos = IT_CHARPOS (*it);
3719 /* For a string from an overlay, the base face depends
3720 only on text properties and ignores overlays. */
3721 base_face_id
3722 = face_for_overlay_string (it->w,
3723 IT_CHARPOS (*it),
3724 it->region_beg_charpos,
3725 it->region_end_charpos,
3726 &next_stop,
3727 (IT_CHARPOS (*it)
3728 + TEXT_PROP_DISTANCE_LIMIT),
3729 0,
3730 from_overlay);
3731 }
3732 else
3733 {
3734 bufpos = 0;
3735
3736 /* For strings from a `display' property, use the face at
3737 IT's current buffer position as the base face to merge
3738 with, so that overlay strings appear in the same face as
3739 surrounding text, unless they specify their own
3740 faces. */
3741 base_face_id = it->string_from_prefix_prop_p
3742 ? DEFAULT_FACE_ID
3743 : underlying_face_id (it);
3744 }
3745
3746 new_face_id = face_at_string_position (it->w,
3747 it->string,
3748 IT_STRING_CHARPOS (*it),
3749 bufpos,
3750 it->region_beg_charpos,
3751 it->region_end_charpos,
3752 &next_stop,
3753 base_face_id, 0);
3754
3755 /* Is this a start of a run of characters with box? Caveat:
3756 this can be called for a freshly allocated iterator; face_id
3757 is -1 is this case. We know that the new face will not
3758 change until the next check pos, i.e. if the new face has a
3759 box, all characters up to that position will have a
3760 box. But, as usual, we don't know whether that position
3761 is really the end. */
3762 if (new_face_id != it->face_id)
3763 {
3764 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3765 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3766
3767 /* If new face has a box but old face hasn't, this is the
3768 start of a run of characters with box, i.e. it has a
3769 shadow on the left side. */
3770 it->start_of_box_run_p
3771 = new_face->box && (old_face == NULL || !old_face->box);
3772 it->face_box_p = new_face->box != FACE_NO_BOX;
3773 }
3774 }
3775
3776 it->face_id = new_face_id;
3777 return HANDLED_NORMALLY;
3778 }
3779
3780
3781 /* Return the ID of the face ``underlying'' IT's current position,
3782 which is in a string. If the iterator is associated with a
3783 buffer, return the face at IT's current buffer position.
3784 Otherwise, use the iterator's base_face_id. */
3785
3786 static int
3787 underlying_face_id (struct it *it)
3788 {
3789 int face_id = it->base_face_id, i;
3790
3791 xassert (STRINGP (it->string));
3792
3793 for (i = it->sp - 1; i >= 0; --i)
3794 if (NILP (it->stack[i].string))
3795 face_id = it->stack[i].face_id;
3796
3797 return face_id;
3798 }
3799
3800
3801 /* Compute the face one character before or after the current position
3802 of IT, in the visual order. BEFORE_P non-zero means get the face
3803 in front (to the left in L2R paragraphs, to the right in R2L
3804 paragraphs) of IT's screen position. Value is the ID of the face. */
3805
3806 static int
3807 face_before_or_after_it_pos (struct it *it, int before_p)
3808 {
3809 int face_id, limit;
3810 ptrdiff_t next_check_charpos;
3811 struct it it_copy;
3812 void *it_copy_data = NULL;
3813
3814 xassert (it->s == NULL);
3815
3816 if (STRINGP (it->string))
3817 {
3818 ptrdiff_t bufpos, charpos;
3819 int base_face_id;
3820
3821 /* No face change past the end of the string (for the case
3822 we are padding with spaces). No face change before the
3823 string start. */
3824 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3825 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3826 return it->face_id;
3827
3828 if (!it->bidi_p)
3829 {
3830 /* Set charpos to the position before or after IT's current
3831 position, in the logical order, which in the non-bidi
3832 case is the same as the visual order. */
3833 if (before_p)
3834 charpos = IT_STRING_CHARPOS (*it) - 1;
3835 else if (it->what == IT_COMPOSITION)
3836 /* For composition, we must check the character after the
3837 composition. */
3838 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3839 else
3840 charpos = IT_STRING_CHARPOS (*it) + 1;
3841 }
3842 else
3843 {
3844 if (before_p)
3845 {
3846 /* With bidi iteration, the character before the current
3847 in the visual order cannot be found by simple
3848 iteration, because "reverse" reordering is not
3849 supported. Instead, we need to use the move_it_*
3850 family of functions. */
3851 /* Ignore face changes before the first visible
3852 character on this display line. */
3853 if (it->current_x <= it->first_visible_x)
3854 return it->face_id;
3855 SAVE_IT (it_copy, *it, it_copy_data);
3856 /* Implementation note: Since move_it_in_display_line
3857 works in the iterator geometry, and thinks the first
3858 character is always the leftmost, even in R2L lines,
3859 we don't need to distinguish between the R2L and L2R
3860 cases here. */
3861 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3862 it_copy.current_x - 1, MOVE_TO_X);
3863 charpos = IT_STRING_CHARPOS (it_copy);
3864 RESTORE_IT (it, it, it_copy_data);
3865 }
3866 else
3867 {
3868 /* Set charpos to the string position of the character
3869 that comes after IT's current position in the visual
3870 order. */
3871 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3872
3873 it_copy = *it;
3874 while (n--)
3875 bidi_move_to_visually_next (&it_copy.bidi_it);
3876
3877 charpos = it_copy.bidi_it.charpos;
3878 }
3879 }
3880 xassert (0 <= charpos && charpos <= SCHARS (it->string));
3881
3882 if (it->current.overlay_string_index >= 0)
3883 bufpos = IT_CHARPOS (*it);
3884 else
3885 bufpos = 0;
3886
3887 base_face_id = underlying_face_id (it);
3888
3889 /* Get the face for ASCII, or unibyte. */
3890 face_id = face_at_string_position (it->w,
3891 it->string,
3892 charpos,
3893 bufpos,
3894 it->region_beg_charpos,
3895 it->region_end_charpos,
3896 &next_check_charpos,
3897 base_face_id, 0);
3898
3899 /* Correct the face for charsets different from ASCII. Do it
3900 for the multibyte case only. The face returned above is
3901 suitable for unibyte text if IT->string is unibyte. */
3902 if (STRING_MULTIBYTE (it->string))
3903 {
3904 struct text_pos pos1 = string_pos (charpos, it->string);
3905 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3906 int c, len;
3907 struct face *face = FACE_FROM_ID (it->f, face_id);
3908
3909 c = string_char_and_length (p, &len);
3910 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3911 }
3912 }
3913 else
3914 {
3915 struct text_pos pos;
3916
3917 if ((IT_CHARPOS (*it) >= ZV && !before_p)
3918 || (IT_CHARPOS (*it) <= BEGV && before_p))
3919 return it->face_id;
3920
3921 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
3922 pos = it->current.pos;
3923
3924 if (!it->bidi_p)
3925 {
3926 if (before_p)
3927 DEC_TEXT_POS (pos, it->multibyte_p);
3928 else
3929 {
3930 if (it->what == IT_COMPOSITION)
3931 {
3932 /* For composition, we must check the position after
3933 the composition. */
3934 pos.charpos += it->cmp_it.nchars;
3935 pos.bytepos += it->len;
3936 }
3937 else
3938 INC_TEXT_POS (pos, it->multibyte_p);
3939 }
3940 }
3941 else
3942 {
3943 if (before_p)
3944 {
3945 /* With bidi iteration, the character before the current
3946 in the visual order cannot be found by simple
3947 iteration, because "reverse" reordering is not
3948 supported. Instead, we need to use the move_it_*
3949 family of functions. */
3950 /* Ignore face changes before the first visible
3951 character on this display line. */
3952 if (it->current_x <= it->first_visible_x)
3953 return it->face_id;
3954 SAVE_IT (it_copy, *it, it_copy_data);
3955 /* Implementation note: Since move_it_in_display_line
3956 works in the iterator geometry, and thinks the first
3957 character is always the leftmost, even in R2L lines,
3958 we don't need to distinguish between the R2L and L2R
3959 cases here. */
3960 move_it_in_display_line (&it_copy, ZV,
3961 it_copy.current_x - 1, MOVE_TO_X);
3962 pos = it_copy.current.pos;
3963 RESTORE_IT (it, it, it_copy_data);
3964 }
3965 else
3966 {
3967 /* Set charpos to the buffer position of the character
3968 that comes after IT's current position in the visual
3969 order. */
3970 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3971
3972 it_copy = *it;
3973 while (n--)
3974 bidi_move_to_visually_next (&it_copy.bidi_it);
3975
3976 SET_TEXT_POS (pos,
3977 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
3978 }
3979 }
3980 xassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
3981
3982 /* Determine face for CHARSET_ASCII, or unibyte. */
3983 face_id = face_at_buffer_position (it->w,
3984 CHARPOS (pos),
3985 it->region_beg_charpos,
3986 it->region_end_charpos,
3987 &next_check_charpos,
3988 limit, 0, -1);
3989
3990 /* Correct the face for charsets different from ASCII. Do it
3991 for the multibyte case only. The face returned above is
3992 suitable for unibyte text if current_buffer is unibyte. */
3993 if (it->multibyte_p)
3994 {
3995 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
3996 struct face *face = FACE_FROM_ID (it->f, face_id);
3997 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
3998 }
3999 }
4000
4001 return face_id;
4002 }
4003
4004
4005 \f
4006 /***********************************************************************
4007 Invisible text
4008 ***********************************************************************/
4009
4010 /* Set up iterator IT from invisible properties at its current
4011 position. Called from handle_stop. */
4012
4013 static enum prop_handled
4014 handle_invisible_prop (struct it *it)
4015 {
4016 enum prop_handled handled = HANDLED_NORMALLY;
4017
4018 if (STRINGP (it->string))
4019 {
4020 Lisp_Object prop, end_charpos, limit, charpos;
4021
4022 /* Get the value of the invisible text property at the
4023 current position. Value will be nil if there is no such
4024 property. */
4025 charpos = make_number (IT_STRING_CHARPOS (*it));
4026 prop = Fget_text_property (charpos, Qinvisible, it->string);
4027
4028 if (!NILP (prop)
4029 && IT_STRING_CHARPOS (*it) < it->end_charpos)
4030 {
4031 ptrdiff_t endpos;
4032
4033 handled = HANDLED_RECOMPUTE_PROPS;
4034
4035 /* Get the position at which the next change of the
4036 invisible text property can be found in IT->string.
4037 Value will be nil if the property value is the same for
4038 all the rest of IT->string. */
4039 XSETINT (limit, SCHARS (it->string));
4040 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4041 it->string, limit);
4042
4043 /* Text at current position is invisible. The next
4044 change in the property is at position end_charpos.
4045 Move IT's current position to that position. */
4046 if (INTEGERP (end_charpos)
4047 && (endpos = XFASTINT (end_charpos)) < XFASTINT (limit))
4048 {
4049 struct text_pos old;
4050 ptrdiff_t oldpos;
4051
4052 old = it->current.string_pos;
4053 oldpos = CHARPOS (old);
4054 if (it->bidi_p)
4055 {
4056 if (it->bidi_it.first_elt
4057 && it->bidi_it.charpos < SCHARS (it->string))
4058 bidi_paragraph_init (it->paragraph_embedding,
4059 &it->bidi_it, 1);
4060 /* Bidi-iterate out of the invisible text. */
4061 do
4062 {
4063 bidi_move_to_visually_next (&it->bidi_it);
4064 }
4065 while (oldpos <= it->bidi_it.charpos
4066 && it->bidi_it.charpos < endpos);
4067
4068 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4069 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4070 if (IT_CHARPOS (*it) >= endpos)
4071 it->prev_stop = endpos;
4072 }
4073 else
4074 {
4075 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4076 compute_string_pos (&it->current.string_pos, old, it->string);
4077 }
4078 }
4079 else
4080 {
4081 /* The rest of the string is invisible. If this is an
4082 overlay string, proceed with the next overlay string
4083 or whatever comes and return a character from there. */
4084 if (it->current.overlay_string_index >= 0)
4085 {
4086 next_overlay_string (it);
4087 /* Don't check for overlay strings when we just
4088 finished processing them. */
4089 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4090 }
4091 else
4092 {
4093 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4094 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4095 }
4096 }
4097 }
4098 }
4099 else
4100 {
4101 int invis_p;
4102 ptrdiff_t newpos, next_stop, start_charpos, tem;
4103 Lisp_Object pos, prop, overlay;
4104
4105 /* First of all, is there invisible text at this position? */
4106 tem = start_charpos = IT_CHARPOS (*it);
4107 pos = make_number (tem);
4108 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4109 &overlay);
4110 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4111
4112 /* If we are on invisible text, skip over it. */
4113 if (invis_p && start_charpos < it->end_charpos)
4114 {
4115 /* Record whether we have to display an ellipsis for the
4116 invisible text. */
4117 int display_ellipsis_p = invis_p == 2;
4118
4119 handled = HANDLED_RECOMPUTE_PROPS;
4120
4121 /* Loop skipping over invisible text. The loop is left at
4122 ZV or with IT on the first char being visible again. */
4123 do
4124 {
4125 /* Try to skip some invisible text. Return value is the
4126 position reached which can be equal to where we start
4127 if there is nothing invisible there. This skips both
4128 over invisible text properties and overlays with
4129 invisible property. */
4130 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4131
4132 /* If we skipped nothing at all we weren't at invisible
4133 text in the first place. If everything to the end of
4134 the buffer was skipped, end the loop. */
4135 if (newpos == tem || newpos >= ZV)
4136 invis_p = 0;
4137 else
4138 {
4139 /* We skipped some characters but not necessarily
4140 all there are. Check if we ended up on visible
4141 text. Fget_char_property returns the property of
4142 the char before the given position, i.e. if we
4143 get invis_p = 0, this means that the char at
4144 newpos is visible. */
4145 pos = make_number (newpos);
4146 prop = Fget_char_property (pos, Qinvisible, it->window);
4147 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4148 }
4149
4150 /* If we ended up on invisible text, proceed to
4151 skip starting with next_stop. */
4152 if (invis_p)
4153 tem = next_stop;
4154
4155 /* If there are adjacent invisible texts, don't lose the
4156 second one's ellipsis. */
4157 if (invis_p == 2)
4158 display_ellipsis_p = 1;
4159 }
4160 while (invis_p);
4161
4162 /* The position newpos is now either ZV or on visible text. */
4163 if (it->bidi_p)
4164 {
4165 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4166 int on_newline =
4167 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4168 int after_newline =
4169 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4170
4171 /* If the invisible text ends on a newline or on a
4172 character after a newline, we can avoid the costly,
4173 character by character, bidi iteration to NEWPOS, and
4174 instead simply reseat the iterator there. That's
4175 because all bidi reordering information is tossed at
4176 the newline. This is a big win for modes that hide
4177 complete lines, like Outline, Org, etc. */
4178 if (on_newline || after_newline)
4179 {
4180 struct text_pos tpos;
4181 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4182
4183 SET_TEXT_POS (tpos, newpos, bpos);
4184 reseat_1 (it, tpos, 0);
4185 /* If we reseat on a newline/ZV, we need to prep the
4186 bidi iterator for advancing to the next character
4187 after the newline/EOB, keeping the current paragraph
4188 direction (so that PRODUCE_GLYPHS does TRT wrt
4189 prepending/appending glyphs to a glyph row). */
4190 if (on_newline)
4191 {
4192 it->bidi_it.first_elt = 0;
4193 it->bidi_it.paragraph_dir = pdir;
4194 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4195 it->bidi_it.nchars = 1;
4196 it->bidi_it.ch_len = 1;
4197 }
4198 }
4199 else /* Must use the slow method. */
4200 {
4201 /* With bidi iteration, the region of invisible text
4202 could start and/or end in the middle of a
4203 non-base embedding level. Therefore, we need to
4204 skip invisible text using the bidi iterator,
4205 starting at IT's current position, until we find
4206 ourselves outside of the invisible text.
4207 Skipping invisible text _after_ bidi iteration
4208 avoids affecting the visual order of the
4209 displayed text when invisible properties are
4210 added or removed. */
4211 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4212 {
4213 /* If we were `reseat'ed to a new paragraph,
4214 determine the paragraph base direction. We
4215 need to do it now because
4216 next_element_from_buffer may not have a
4217 chance to do it, if we are going to skip any
4218 text at the beginning, which resets the
4219 FIRST_ELT flag. */
4220 bidi_paragraph_init (it->paragraph_embedding,
4221 &it->bidi_it, 1);
4222 }
4223 do
4224 {
4225 bidi_move_to_visually_next (&it->bidi_it);
4226 }
4227 while (it->stop_charpos <= it->bidi_it.charpos
4228 && it->bidi_it.charpos < newpos);
4229 IT_CHARPOS (*it) = it->bidi_it.charpos;
4230 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4231 /* If we overstepped NEWPOS, record its position in
4232 the iterator, so that we skip invisible text if
4233 later the bidi iteration lands us in the
4234 invisible region again. */
4235 if (IT_CHARPOS (*it) >= newpos)
4236 it->prev_stop = newpos;
4237 }
4238 }
4239 else
4240 {
4241 IT_CHARPOS (*it) = newpos;
4242 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4243 }
4244
4245 /* If there are before-strings at the start of invisible
4246 text, and the text is invisible because of a text
4247 property, arrange to show before-strings because 20.x did
4248 it that way. (If the text is invisible because of an
4249 overlay property instead of a text property, this is
4250 already handled in the overlay code.) */
4251 if (NILP (overlay)
4252 && get_overlay_strings (it, it->stop_charpos))
4253 {
4254 handled = HANDLED_RECOMPUTE_PROPS;
4255 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4256 }
4257 else if (display_ellipsis_p)
4258 {
4259 /* Make sure that the glyphs of the ellipsis will get
4260 correct `charpos' values. If we would not update
4261 it->position here, the glyphs would belong to the
4262 last visible character _before_ the invisible
4263 text, which confuses `set_cursor_from_row'.
4264
4265 We use the last invisible position instead of the
4266 first because this way the cursor is always drawn on
4267 the first "." of the ellipsis, whenever PT is inside
4268 the invisible text. Otherwise the cursor would be
4269 placed _after_ the ellipsis when the point is after the
4270 first invisible character. */
4271 if (!STRINGP (it->object))
4272 {
4273 it->position.charpos = newpos - 1;
4274 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4275 }
4276 it->ellipsis_p = 1;
4277 /* Let the ellipsis display before
4278 considering any properties of the following char.
4279 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4280 handled = HANDLED_RETURN;
4281 }
4282 }
4283 }
4284
4285 return handled;
4286 }
4287
4288
4289 /* Make iterator IT return `...' next.
4290 Replaces LEN characters from buffer. */
4291
4292 static void
4293 setup_for_ellipsis (struct it *it, int len)
4294 {
4295 /* Use the display table definition for `...'. Invalid glyphs
4296 will be handled by the method returning elements from dpvec. */
4297 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4298 {
4299 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4300 it->dpvec = v->contents;
4301 it->dpend = v->contents + v->header.size;
4302 }
4303 else
4304 {
4305 /* Default `...'. */
4306 it->dpvec = default_invis_vector;
4307 it->dpend = default_invis_vector + 3;
4308 }
4309
4310 it->dpvec_char_len = len;
4311 it->current.dpvec_index = 0;
4312 it->dpvec_face_id = -1;
4313
4314 /* Remember the current face id in case glyphs specify faces.
4315 IT's face is restored in set_iterator_to_next.
4316 saved_face_id was set to preceding char's face in handle_stop. */
4317 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4318 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4319
4320 it->method = GET_FROM_DISPLAY_VECTOR;
4321 it->ellipsis_p = 1;
4322 }
4323
4324
4325 \f
4326 /***********************************************************************
4327 'display' property
4328 ***********************************************************************/
4329
4330 /* Set up iterator IT from `display' property at its current position.
4331 Called from handle_stop.
4332 We return HANDLED_RETURN if some part of the display property
4333 overrides the display of the buffer text itself.
4334 Otherwise we return HANDLED_NORMALLY. */
4335
4336 static enum prop_handled
4337 handle_display_prop (struct it *it)
4338 {
4339 Lisp_Object propval, object, overlay;
4340 struct text_pos *position;
4341 ptrdiff_t bufpos;
4342 /* Nonzero if some property replaces the display of the text itself. */
4343 int display_replaced_p = 0;
4344
4345 if (STRINGP (it->string))
4346 {
4347 object = it->string;
4348 position = &it->current.string_pos;
4349 bufpos = CHARPOS (it->current.pos);
4350 }
4351 else
4352 {
4353 XSETWINDOW (object, it->w);
4354 position = &it->current.pos;
4355 bufpos = CHARPOS (*position);
4356 }
4357
4358 /* Reset those iterator values set from display property values. */
4359 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4360 it->space_width = Qnil;
4361 it->font_height = Qnil;
4362 it->voffset = 0;
4363
4364 /* We don't support recursive `display' properties, i.e. string
4365 values that have a string `display' property, that have a string
4366 `display' property etc. */
4367 if (!it->string_from_display_prop_p)
4368 it->area = TEXT_AREA;
4369
4370 propval = get_char_property_and_overlay (make_number (position->charpos),
4371 Qdisplay, object, &overlay);
4372 if (NILP (propval))
4373 return HANDLED_NORMALLY;
4374 /* Now OVERLAY is the overlay that gave us this property, or nil
4375 if it was a text property. */
4376
4377 if (!STRINGP (it->string))
4378 object = it->w->buffer;
4379
4380 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4381 position, bufpos,
4382 FRAME_WINDOW_P (it->f));
4383
4384 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4385 }
4386
4387 /* Subroutine of handle_display_prop. Returns non-zero if the display
4388 specification in SPEC is a replacing specification, i.e. it would
4389 replace the text covered by `display' property with something else,
4390 such as an image or a display string. If SPEC includes any kind or
4391 `(space ...) specification, the value is 2; this is used by
4392 compute_display_string_pos, which see.
4393
4394 See handle_single_display_spec for documentation of arguments.
4395 frame_window_p is non-zero if the window being redisplayed is on a
4396 GUI frame; this argument is used only if IT is NULL, see below.
4397
4398 IT can be NULL, if this is called by the bidi reordering code
4399 through compute_display_string_pos, which see. In that case, this
4400 function only examines SPEC, but does not otherwise "handle" it, in
4401 the sense that it doesn't set up members of IT from the display
4402 spec. */
4403 static int
4404 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4405 Lisp_Object overlay, struct text_pos *position,
4406 ptrdiff_t bufpos, int frame_window_p)
4407 {
4408 int replacing_p = 0;
4409 int rv;
4410
4411 if (CONSP (spec)
4412 /* Simple specifications. */
4413 && !EQ (XCAR (spec), Qimage)
4414 && !EQ (XCAR (spec), Qspace)
4415 && !EQ (XCAR (spec), Qwhen)
4416 && !EQ (XCAR (spec), Qslice)
4417 && !EQ (XCAR (spec), Qspace_width)
4418 && !EQ (XCAR (spec), Qheight)
4419 && !EQ (XCAR (spec), Qraise)
4420 /* Marginal area specifications. */
4421 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4422 && !EQ (XCAR (spec), Qleft_fringe)
4423 && !EQ (XCAR (spec), Qright_fringe)
4424 && !NILP (XCAR (spec)))
4425 {
4426 for (; CONSP (spec); spec = XCDR (spec))
4427 {
4428 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4429 overlay, position, bufpos,
4430 replacing_p, frame_window_p)))
4431 {
4432 replacing_p = rv;
4433 /* If some text in a string is replaced, `position' no
4434 longer points to the position of `object'. */
4435 if (!it || STRINGP (object))
4436 break;
4437 }
4438 }
4439 }
4440 else if (VECTORP (spec))
4441 {
4442 ptrdiff_t i;
4443 for (i = 0; i < ASIZE (spec); ++i)
4444 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4445 overlay, position, bufpos,
4446 replacing_p, frame_window_p)))
4447 {
4448 replacing_p = rv;
4449 /* If some text in a string is replaced, `position' no
4450 longer points to the position of `object'. */
4451 if (!it || STRINGP (object))
4452 break;
4453 }
4454 }
4455 else
4456 {
4457 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4458 position, bufpos, 0,
4459 frame_window_p)))
4460 replacing_p = rv;
4461 }
4462
4463 return replacing_p;
4464 }
4465
4466 /* Value is the position of the end of the `display' property starting
4467 at START_POS in OBJECT. */
4468
4469 static struct text_pos
4470 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4471 {
4472 Lisp_Object end;
4473 struct text_pos end_pos;
4474
4475 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4476 Qdisplay, object, Qnil);
4477 CHARPOS (end_pos) = XFASTINT (end);
4478 if (STRINGP (object))
4479 compute_string_pos (&end_pos, start_pos, it->string);
4480 else
4481 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4482
4483 return end_pos;
4484 }
4485
4486
4487 /* Set up IT from a single `display' property specification SPEC. OBJECT
4488 is the object in which the `display' property was found. *POSITION
4489 is the position in OBJECT at which the `display' property was found.
4490 BUFPOS is the buffer position of OBJECT (different from POSITION if
4491 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4492 previously saw a display specification which already replaced text
4493 display with something else, for example an image; we ignore such
4494 properties after the first one has been processed.
4495
4496 OVERLAY is the overlay this `display' property came from,
4497 or nil if it was a text property.
4498
4499 If SPEC is a `space' or `image' specification, and in some other
4500 cases too, set *POSITION to the position where the `display'
4501 property ends.
4502
4503 If IT is NULL, only examine the property specification in SPEC, but
4504 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4505 is intended to be displayed in a window on a GUI frame.
4506
4507 Value is non-zero if something was found which replaces the display
4508 of buffer or string text. */
4509
4510 static int
4511 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4512 Lisp_Object overlay, struct text_pos *position,
4513 ptrdiff_t bufpos, int display_replaced_p,
4514 int frame_window_p)
4515 {
4516 Lisp_Object form;
4517 Lisp_Object location, value;
4518 struct text_pos start_pos = *position;
4519 int valid_p;
4520
4521 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4522 If the result is non-nil, use VALUE instead of SPEC. */
4523 form = Qt;
4524 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4525 {
4526 spec = XCDR (spec);
4527 if (!CONSP (spec))
4528 return 0;
4529 form = XCAR (spec);
4530 spec = XCDR (spec);
4531 }
4532
4533 if (!NILP (form) && !EQ (form, Qt))
4534 {
4535 ptrdiff_t count = SPECPDL_INDEX ();
4536 struct gcpro gcpro1;
4537
4538 /* Bind `object' to the object having the `display' property, a
4539 buffer or string. Bind `position' to the position in the
4540 object where the property was found, and `buffer-position'
4541 to the current position in the buffer. */
4542
4543 if (NILP (object))
4544 XSETBUFFER (object, current_buffer);
4545 specbind (Qobject, object);
4546 specbind (Qposition, make_number (CHARPOS (*position)));
4547 specbind (Qbuffer_position, make_number (bufpos));
4548 GCPRO1 (form);
4549 form = safe_eval (form);
4550 UNGCPRO;
4551 unbind_to (count, Qnil);
4552 }
4553
4554 if (NILP (form))
4555 return 0;
4556
4557 /* Handle `(height HEIGHT)' specifications. */
4558 if (CONSP (spec)
4559 && EQ (XCAR (spec), Qheight)
4560 && CONSP (XCDR (spec)))
4561 {
4562 if (it)
4563 {
4564 if (!FRAME_WINDOW_P (it->f))
4565 return 0;
4566
4567 it->font_height = XCAR (XCDR (spec));
4568 if (!NILP (it->font_height))
4569 {
4570 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4571 int new_height = -1;
4572
4573 if (CONSP (it->font_height)
4574 && (EQ (XCAR (it->font_height), Qplus)
4575 || EQ (XCAR (it->font_height), Qminus))
4576 && CONSP (XCDR (it->font_height))
4577 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4578 {
4579 /* `(+ N)' or `(- N)' where N is an integer. */
4580 int steps = XINT (XCAR (XCDR (it->font_height)));
4581 if (EQ (XCAR (it->font_height), Qplus))
4582 steps = - steps;
4583 it->face_id = smaller_face (it->f, it->face_id, steps);
4584 }
4585 else if (FUNCTIONP (it->font_height))
4586 {
4587 /* Call function with current height as argument.
4588 Value is the new height. */
4589 Lisp_Object height;
4590 height = safe_call1 (it->font_height,
4591 face->lface[LFACE_HEIGHT_INDEX]);
4592 if (NUMBERP (height))
4593 new_height = XFLOATINT (height);
4594 }
4595 else if (NUMBERP (it->font_height))
4596 {
4597 /* Value is a multiple of the canonical char height. */
4598 struct face *f;
4599
4600 f = FACE_FROM_ID (it->f,
4601 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4602 new_height = (XFLOATINT (it->font_height)
4603 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4604 }
4605 else
4606 {
4607 /* Evaluate IT->font_height with `height' bound to the
4608 current specified height to get the new height. */
4609 ptrdiff_t count = SPECPDL_INDEX ();
4610
4611 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4612 value = safe_eval (it->font_height);
4613 unbind_to (count, Qnil);
4614
4615 if (NUMBERP (value))
4616 new_height = XFLOATINT (value);
4617 }
4618
4619 if (new_height > 0)
4620 it->face_id = face_with_height (it->f, it->face_id, new_height);
4621 }
4622 }
4623
4624 return 0;
4625 }
4626
4627 /* Handle `(space-width WIDTH)'. */
4628 if (CONSP (spec)
4629 && EQ (XCAR (spec), Qspace_width)
4630 && CONSP (XCDR (spec)))
4631 {
4632 if (it)
4633 {
4634 if (!FRAME_WINDOW_P (it->f))
4635 return 0;
4636
4637 value = XCAR (XCDR (spec));
4638 if (NUMBERP (value) && XFLOATINT (value) > 0)
4639 it->space_width = value;
4640 }
4641
4642 return 0;
4643 }
4644
4645 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4646 if (CONSP (spec)
4647 && EQ (XCAR (spec), Qslice))
4648 {
4649 Lisp_Object tem;
4650
4651 if (it)
4652 {
4653 if (!FRAME_WINDOW_P (it->f))
4654 return 0;
4655
4656 if (tem = XCDR (spec), CONSP (tem))
4657 {
4658 it->slice.x = XCAR (tem);
4659 if (tem = XCDR (tem), CONSP (tem))
4660 {
4661 it->slice.y = XCAR (tem);
4662 if (tem = XCDR (tem), CONSP (tem))
4663 {
4664 it->slice.width = XCAR (tem);
4665 if (tem = XCDR (tem), CONSP (tem))
4666 it->slice.height = XCAR (tem);
4667 }
4668 }
4669 }
4670 }
4671
4672 return 0;
4673 }
4674
4675 /* Handle `(raise FACTOR)'. */
4676 if (CONSP (spec)
4677 && EQ (XCAR (spec), Qraise)
4678 && CONSP (XCDR (spec)))
4679 {
4680 if (it)
4681 {
4682 if (!FRAME_WINDOW_P (it->f))
4683 return 0;
4684
4685 #ifdef HAVE_WINDOW_SYSTEM
4686 value = XCAR (XCDR (spec));
4687 if (NUMBERP (value))
4688 {
4689 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4690 it->voffset = - (XFLOATINT (value)
4691 * (FONT_HEIGHT (face->font)));
4692 }
4693 #endif /* HAVE_WINDOW_SYSTEM */
4694 }
4695
4696 return 0;
4697 }
4698
4699 /* Don't handle the other kinds of display specifications
4700 inside a string that we got from a `display' property. */
4701 if (it && it->string_from_display_prop_p)
4702 return 0;
4703
4704 /* Characters having this form of property are not displayed, so
4705 we have to find the end of the property. */
4706 if (it)
4707 {
4708 start_pos = *position;
4709 *position = display_prop_end (it, object, start_pos);
4710 }
4711 value = Qnil;
4712
4713 /* Stop the scan at that end position--we assume that all
4714 text properties change there. */
4715 if (it)
4716 it->stop_charpos = position->charpos;
4717
4718 /* Handle `(left-fringe BITMAP [FACE])'
4719 and `(right-fringe BITMAP [FACE])'. */
4720 if (CONSP (spec)
4721 && (EQ (XCAR (spec), Qleft_fringe)
4722 || EQ (XCAR (spec), Qright_fringe))
4723 && CONSP (XCDR (spec)))
4724 {
4725 int fringe_bitmap;
4726
4727 if (it)
4728 {
4729 if (!FRAME_WINDOW_P (it->f))
4730 /* If we return here, POSITION has been advanced
4731 across the text with this property. */
4732 {
4733 /* Synchronize the bidi iterator with POSITION. This is
4734 needed because we are not going to push the iterator
4735 on behalf of this display property, so there will be
4736 no pop_it call to do this synchronization for us. */
4737 if (it->bidi_p)
4738 {
4739 it->position = *position;
4740 iterate_out_of_display_property (it);
4741 *position = it->position;
4742 }
4743 return 1;
4744 }
4745 }
4746 else if (!frame_window_p)
4747 return 1;
4748
4749 #ifdef HAVE_WINDOW_SYSTEM
4750 value = XCAR (XCDR (spec));
4751 if (!SYMBOLP (value)
4752 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4753 /* If we return here, POSITION has been advanced
4754 across the text with this property. */
4755 {
4756 if (it && it->bidi_p)
4757 {
4758 it->position = *position;
4759 iterate_out_of_display_property (it);
4760 *position = it->position;
4761 }
4762 return 1;
4763 }
4764
4765 if (it)
4766 {
4767 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4768
4769 if (CONSP (XCDR (XCDR (spec))))
4770 {
4771 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4772 int face_id2 = lookup_derived_face (it->f, face_name,
4773 FRINGE_FACE_ID, 0);
4774 if (face_id2 >= 0)
4775 face_id = face_id2;
4776 }
4777
4778 /* Save current settings of IT so that we can restore them
4779 when we are finished with the glyph property value. */
4780 push_it (it, position);
4781
4782 it->area = TEXT_AREA;
4783 it->what = IT_IMAGE;
4784 it->image_id = -1; /* no image */
4785 it->position = start_pos;
4786 it->object = NILP (object) ? it->w->buffer : object;
4787 it->method = GET_FROM_IMAGE;
4788 it->from_overlay = Qnil;
4789 it->face_id = face_id;
4790 it->from_disp_prop_p = 1;
4791
4792 /* Say that we haven't consumed the characters with
4793 `display' property yet. The call to pop_it in
4794 set_iterator_to_next will clean this up. */
4795 *position = start_pos;
4796
4797 if (EQ (XCAR (spec), Qleft_fringe))
4798 {
4799 it->left_user_fringe_bitmap = fringe_bitmap;
4800 it->left_user_fringe_face_id = face_id;
4801 }
4802 else
4803 {
4804 it->right_user_fringe_bitmap = fringe_bitmap;
4805 it->right_user_fringe_face_id = face_id;
4806 }
4807 }
4808 #endif /* HAVE_WINDOW_SYSTEM */
4809 return 1;
4810 }
4811
4812 /* Prepare to handle `((margin left-margin) ...)',
4813 `((margin right-margin) ...)' and `((margin nil) ...)'
4814 prefixes for display specifications. */
4815 location = Qunbound;
4816 if (CONSP (spec) && CONSP (XCAR (spec)))
4817 {
4818 Lisp_Object tem;
4819
4820 value = XCDR (spec);
4821 if (CONSP (value))
4822 value = XCAR (value);
4823
4824 tem = XCAR (spec);
4825 if (EQ (XCAR (tem), Qmargin)
4826 && (tem = XCDR (tem),
4827 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4828 (NILP (tem)
4829 || EQ (tem, Qleft_margin)
4830 || EQ (tem, Qright_margin))))
4831 location = tem;
4832 }
4833
4834 if (EQ (location, Qunbound))
4835 {
4836 location = Qnil;
4837 value = spec;
4838 }
4839
4840 /* After this point, VALUE is the property after any
4841 margin prefix has been stripped. It must be a string,
4842 an image specification, or `(space ...)'.
4843
4844 LOCATION specifies where to display: `left-margin',
4845 `right-margin' or nil. */
4846
4847 valid_p = (STRINGP (value)
4848 #ifdef HAVE_WINDOW_SYSTEM
4849 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4850 && valid_image_p (value))
4851 #endif /* not HAVE_WINDOW_SYSTEM */
4852 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4853
4854 if (valid_p && !display_replaced_p)
4855 {
4856 int retval = 1;
4857
4858 if (!it)
4859 {
4860 /* Callers need to know whether the display spec is any kind
4861 of `(space ...)' spec that is about to affect text-area
4862 display. */
4863 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4864 retval = 2;
4865 return retval;
4866 }
4867
4868 /* Save current settings of IT so that we can restore them
4869 when we are finished with the glyph property value. */
4870 push_it (it, position);
4871 it->from_overlay = overlay;
4872 it->from_disp_prop_p = 1;
4873
4874 if (NILP (location))
4875 it->area = TEXT_AREA;
4876 else if (EQ (location, Qleft_margin))
4877 it->area = LEFT_MARGIN_AREA;
4878 else
4879 it->area = RIGHT_MARGIN_AREA;
4880
4881 if (STRINGP (value))
4882 {
4883 it->string = value;
4884 it->multibyte_p = STRING_MULTIBYTE (it->string);
4885 it->current.overlay_string_index = -1;
4886 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4887 it->end_charpos = it->string_nchars = SCHARS (it->string);
4888 it->method = GET_FROM_STRING;
4889 it->stop_charpos = 0;
4890 it->prev_stop = 0;
4891 it->base_level_stop = 0;
4892 it->string_from_display_prop_p = 1;
4893 /* Say that we haven't consumed the characters with
4894 `display' property yet. The call to pop_it in
4895 set_iterator_to_next will clean this up. */
4896 if (BUFFERP (object))
4897 *position = start_pos;
4898
4899 /* Force paragraph direction to be that of the parent
4900 object. If the parent object's paragraph direction is
4901 not yet determined, default to L2R. */
4902 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
4903 it->paragraph_embedding = it->bidi_it.paragraph_dir;
4904 else
4905 it->paragraph_embedding = L2R;
4906
4907 /* Set up the bidi iterator for this display string. */
4908 if (it->bidi_p)
4909 {
4910 it->bidi_it.string.lstring = it->string;
4911 it->bidi_it.string.s = NULL;
4912 it->bidi_it.string.schars = it->end_charpos;
4913 it->bidi_it.string.bufpos = bufpos;
4914 it->bidi_it.string.from_disp_str = 1;
4915 it->bidi_it.string.unibyte = !it->multibyte_p;
4916 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
4917 }
4918 }
4919 else if (CONSP (value) && EQ (XCAR (value), Qspace))
4920 {
4921 it->method = GET_FROM_STRETCH;
4922 it->object = value;
4923 *position = it->position = start_pos;
4924 retval = 1 + (it->area == TEXT_AREA);
4925 }
4926 #ifdef HAVE_WINDOW_SYSTEM
4927 else
4928 {
4929 it->what = IT_IMAGE;
4930 it->image_id = lookup_image (it->f, value);
4931 it->position = start_pos;
4932 it->object = NILP (object) ? it->w->buffer : object;
4933 it->method = GET_FROM_IMAGE;
4934
4935 /* Say that we haven't consumed the characters with
4936 `display' property yet. The call to pop_it in
4937 set_iterator_to_next will clean this up. */
4938 *position = start_pos;
4939 }
4940 #endif /* HAVE_WINDOW_SYSTEM */
4941
4942 return retval;
4943 }
4944
4945 /* Invalid property or property not supported. Restore
4946 POSITION to what it was before. */
4947 *position = start_pos;
4948 return 0;
4949 }
4950
4951 /* Check if PROP is a display property value whose text should be
4952 treated as intangible. OVERLAY is the overlay from which PROP
4953 came, or nil if it came from a text property. CHARPOS and BYTEPOS
4954 specify the buffer position covered by PROP. */
4955
4956 int
4957 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
4958 ptrdiff_t charpos, ptrdiff_t bytepos)
4959 {
4960 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
4961 struct text_pos position;
4962
4963 SET_TEXT_POS (position, charpos, bytepos);
4964 return handle_display_spec (NULL, prop, Qnil, overlay,
4965 &position, charpos, frame_window_p);
4966 }
4967
4968
4969 /* Return 1 if PROP is a display sub-property value containing STRING.
4970
4971 Implementation note: this and the following function are really
4972 special cases of handle_display_spec and
4973 handle_single_display_spec, and should ideally use the same code.
4974 Until they do, these two pairs must be consistent and must be
4975 modified in sync. */
4976
4977 static int
4978 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
4979 {
4980 if (EQ (string, prop))
4981 return 1;
4982
4983 /* Skip over `when FORM'. */
4984 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
4985 {
4986 prop = XCDR (prop);
4987 if (!CONSP (prop))
4988 return 0;
4989 /* Actually, the condition following `when' should be eval'ed,
4990 like handle_single_display_spec does, and we should return
4991 zero if it evaluates to nil. However, this function is
4992 called only when the buffer was already displayed and some
4993 glyph in the glyph matrix was found to come from a display
4994 string. Therefore, the condition was already evaluated, and
4995 the result was non-nil, otherwise the display string wouldn't
4996 have been displayed and we would have never been called for
4997 this property. Thus, we can skip the evaluation and assume
4998 its result is non-nil. */
4999 prop = XCDR (prop);
5000 }
5001
5002 if (CONSP (prop))
5003 /* Skip over `margin LOCATION'. */
5004 if (EQ (XCAR (prop), Qmargin))
5005 {
5006 prop = XCDR (prop);
5007 if (!CONSP (prop))
5008 return 0;
5009
5010 prop = XCDR (prop);
5011 if (!CONSP (prop))
5012 return 0;
5013 }
5014
5015 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5016 }
5017
5018
5019 /* Return 1 if STRING appears in the `display' property PROP. */
5020
5021 static int
5022 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5023 {
5024 if (CONSP (prop)
5025 && !EQ (XCAR (prop), Qwhen)
5026 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5027 {
5028 /* A list of sub-properties. */
5029 while (CONSP (prop))
5030 {
5031 if (single_display_spec_string_p (XCAR (prop), string))
5032 return 1;
5033 prop = XCDR (prop);
5034 }
5035 }
5036 else if (VECTORP (prop))
5037 {
5038 /* A vector of sub-properties. */
5039 ptrdiff_t i;
5040 for (i = 0; i < ASIZE (prop); ++i)
5041 if (single_display_spec_string_p (AREF (prop, i), string))
5042 return 1;
5043 }
5044 else
5045 return single_display_spec_string_p (prop, string);
5046
5047 return 0;
5048 }
5049
5050 /* Look for STRING in overlays and text properties in the current
5051 buffer, between character positions FROM and TO (excluding TO).
5052 BACK_P non-zero means look back (in this case, TO is supposed to be
5053 less than FROM).
5054 Value is the first character position where STRING was found, or
5055 zero if it wasn't found before hitting TO.
5056
5057 This function may only use code that doesn't eval because it is
5058 called asynchronously from note_mouse_highlight. */
5059
5060 static ptrdiff_t
5061 string_buffer_position_lim (Lisp_Object string,
5062 ptrdiff_t from, ptrdiff_t to, int back_p)
5063 {
5064 Lisp_Object limit, prop, pos;
5065 int found = 0;
5066
5067 pos = make_number (max (from, BEGV));
5068
5069 if (!back_p) /* looking forward */
5070 {
5071 limit = make_number (min (to, ZV));
5072 while (!found && !EQ (pos, limit))
5073 {
5074 prop = Fget_char_property (pos, Qdisplay, Qnil);
5075 if (!NILP (prop) && display_prop_string_p (prop, string))
5076 found = 1;
5077 else
5078 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5079 limit);
5080 }
5081 }
5082 else /* looking back */
5083 {
5084 limit = make_number (max (to, BEGV));
5085 while (!found && !EQ (pos, limit))
5086 {
5087 prop = Fget_char_property (pos, Qdisplay, Qnil);
5088 if (!NILP (prop) && display_prop_string_p (prop, string))
5089 found = 1;
5090 else
5091 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5092 limit);
5093 }
5094 }
5095
5096 return found ? XINT (pos) : 0;
5097 }
5098
5099 /* Determine which buffer position in current buffer STRING comes from.
5100 AROUND_CHARPOS is an approximate position where it could come from.
5101 Value is the buffer position or 0 if it couldn't be determined.
5102
5103 This function is necessary because we don't record buffer positions
5104 in glyphs generated from strings (to keep struct glyph small).
5105 This function may only use code that doesn't eval because it is
5106 called asynchronously from note_mouse_highlight. */
5107
5108 static ptrdiff_t
5109 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5110 {
5111 const int MAX_DISTANCE = 1000;
5112 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5113 around_charpos + MAX_DISTANCE,
5114 0);
5115
5116 if (!found)
5117 found = string_buffer_position_lim (string, around_charpos,
5118 around_charpos - MAX_DISTANCE, 1);
5119 return found;
5120 }
5121
5122
5123 \f
5124 /***********************************************************************
5125 `composition' property
5126 ***********************************************************************/
5127
5128 /* Set up iterator IT from `composition' property at its current
5129 position. Called from handle_stop. */
5130
5131 static enum prop_handled
5132 handle_composition_prop (struct it *it)
5133 {
5134 Lisp_Object prop, string;
5135 ptrdiff_t pos, pos_byte, start, end;
5136
5137 if (STRINGP (it->string))
5138 {
5139 unsigned char *s;
5140
5141 pos = IT_STRING_CHARPOS (*it);
5142 pos_byte = IT_STRING_BYTEPOS (*it);
5143 string = it->string;
5144 s = SDATA (string) + pos_byte;
5145 it->c = STRING_CHAR (s);
5146 }
5147 else
5148 {
5149 pos = IT_CHARPOS (*it);
5150 pos_byte = IT_BYTEPOS (*it);
5151 string = Qnil;
5152 it->c = FETCH_CHAR (pos_byte);
5153 }
5154
5155 /* If there's a valid composition and point is not inside of the
5156 composition (in the case that the composition is from the current
5157 buffer), draw a glyph composed from the composition components. */
5158 if (find_composition (pos, -1, &start, &end, &prop, string)
5159 && COMPOSITION_VALID_P (start, end, prop)
5160 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5161 {
5162 if (start < pos)
5163 /* As we can't handle this situation (perhaps font-lock added
5164 a new composition), we just return here hoping that next
5165 redisplay will detect this composition much earlier. */
5166 return HANDLED_NORMALLY;
5167 if (start != pos)
5168 {
5169 if (STRINGP (it->string))
5170 pos_byte = string_char_to_byte (it->string, start);
5171 else
5172 pos_byte = CHAR_TO_BYTE (start);
5173 }
5174 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5175 prop, string);
5176
5177 if (it->cmp_it.id >= 0)
5178 {
5179 it->cmp_it.ch = -1;
5180 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5181 it->cmp_it.nglyphs = -1;
5182 }
5183 }
5184
5185 return HANDLED_NORMALLY;
5186 }
5187
5188
5189 \f
5190 /***********************************************************************
5191 Overlay strings
5192 ***********************************************************************/
5193
5194 /* The following structure is used to record overlay strings for
5195 later sorting in load_overlay_strings. */
5196
5197 struct overlay_entry
5198 {
5199 Lisp_Object overlay;
5200 Lisp_Object string;
5201 EMACS_INT priority;
5202 int after_string_p;
5203 };
5204
5205
5206 /* Set up iterator IT from overlay strings at its current position.
5207 Called from handle_stop. */
5208
5209 static enum prop_handled
5210 handle_overlay_change (struct it *it)
5211 {
5212 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5213 return HANDLED_RECOMPUTE_PROPS;
5214 else
5215 return HANDLED_NORMALLY;
5216 }
5217
5218
5219 /* Set up the next overlay string for delivery by IT, if there is an
5220 overlay string to deliver. Called by set_iterator_to_next when the
5221 end of the current overlay string is reached. If there are more
5222 overlay strings to display, IT->string and
5223 IT->current.overlay_string_index are set appropriately here.
5224 Otherwise IT->string is set to nil. */
5225
5226 static void
5227 next_overlay_string (struct it *it)
5228 {
5229 ++it->current.overlay_string_index;
5230 if (it->current.overlay_string_index == it->n_overlay_strings)
5231 {
5232 /* No more overlay strings. Restore IT's settings to what
5233 they were before overlay strings were processed, and
5234 continue to deliver from current_buffer. */
5235
5236 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5237 pop_it (it);
5238 xassert (it->sp > 0
5239 || (NILP (it->string)
5240 && it->method == GET_FROM_BUFFER
5241 && it->stop_charpos >= BEGV
5242 && it->stop_charpos <= it->end_charpos));
5243 it->current.overlay_string_index = -1;
5244 it->n_overlay_strings = 0;
5245 it->overlay_strings_charpos = -1;
5246 /* If there's an empty display string on the stack, pop the
5247 stack, to resync the bidi iterator with IT's position. Such
5248 empty strings are pushed onto the stack in
5249 get_overlay_strings_1. */
5250 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5251 pop_it (it);
5252
5253 /* If we're at the end of the buffer, record that we have
5254 processed the overlay strings there already, so that
5255 next_element_from_buffer doesn't try it again. */
5256 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5257 it->overlay_strings_at_end_processed_p = 1;
5258 }
5259 else
5260 {
5261 /* There are more overlay strings to process. If
5262 IT->current.overlay_string_index has advanced to a position
5263 where we must load IT->overlay_strings with more strings, do
5264 it. We must load at the IT->overlay_strings_charpos where
5265 IT->n_overlay_strings was originally computed; when invisible
5266 text is present, this might not be IT_CHARPOS (Bug#7016). */
5267 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5268
5269 if (it->current.overlay_string_index && i == 0)
5270 load_overlay_strings (it, it->overlay_strings_charpos);
5271
5272 /* Initialize IT to deliver display elements from the overlay
5273 string. */
5274 it->string = it->overlay_strings[i];
5275 it->multibyte_p = STRING_MULTIBYTE (it->string);
5276 SET_TEXT_POS (it->current.string_pos, 0, 0);
5277 it->method = GET_FROM_STRING;
5278 it->stop_charpos = 0;
5279 if (it->cmp_it.stop_pos >= 0)
5280 it->cmp_it.stop_pos = 0;
5281 it->prev_stop = 0;
5282 it->base_level_stop = 0;
5283
5284 /* Set up the bidi iterator for this overlay string. */
5285 if (it->bidi_p)
5286 {
5287 it->bidi_it.string.lstring = it->string;
5288 it->bidi_it.string.s = NULL;
5289 it->bidi_it.string.schars = SCHARS (it->string);
5290 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5291 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5292 it->bidi_it.string.unibyte = !it->multibyte_p;
5293 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5294 }
5295 }
5296
5297 CHECK_IT (it);
5298 }
5299
5300
5301 /* Compare two overlay_entry structures E1 and E2. Used as a
5302 comparison function for qsort in load_overlay_strings. Overlay
5303 strings for the same position are sorted so that
5304
5305 1. All after-strings come in front of before-strings, except
5306 when they come from the same overlay.
5307
5308 2. Within after-strings, strings are sorted so that overlay strings
5309 from overlays with higher priorities come first.
5310
5311 2. Within before-strings, strings are sorted so that overlay
5312 strings from overlays with higher priorities come last.
5313
5314 Value is analogous to strcmp. */
5315
5316
5317 static int
5318 compare_overlay_entries (const void *e1, const void *e2)
5319 {
5320 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5321 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5322 int result;
5323
5324 if (entry1->after_string_p != entry2->after_string_p)
5325 {
5326 /* Let after-strings appear in front of before-strings if
5327 they come from different overlays. */
5328 if (EQ (entry1->overlay, entry2->overlay))
5329 result = entry1->after_string_p ? 1 : -1;
5330 else
5331 result = entry1->after_string_p ? -1 : 1;
5332 }
5333 else if (entry1->priority != entry2->priority)
5334 {
5335 if (entry1->after_string_p)
5336 /* After-strings sorted in order of decreasing priority. */
5337 result = entry2->priority < entry1->priority ? -1 : 1;
5338 else
5339 /* Before-strings sorted in order of increasing priority. */
5340 result = entry1->priority < entry2->priority ? -1 : 1;
5341 }
5342 else
5343 result = 0;
5344
5345 return result;
5346 }
5347
5348
5349 /* Load the vector IT->overlay_strings with overlay strings from IT's
5350 current buffer position, or from CHARPOS if that is > 0. Set
5351 IT->n_overlays to the total number of overlay strings found.
5352
5353 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5354 a time. On entry into load_overlay_strings,
5355 IT->current.overlay_string_index gives the number of overlay
5356 strings that have already been loaded by previous calls to this
5357 function.
5358
5359 IT->add_overlay_start contains an additional overlay start
5360 position to consider for taking overlay strings from, if non-zero.
5361 This position comes into play when the overlay has an `invisible'
5362 property, and both before and after-strings. When we've skipped to
5363 the end of the overlay, because of its `invisible' property, we
5364 nevertheless want its before-string to appear.
5365 IT->add_overlay_start will contain the overlay start position
5366 in this case.
5367
5368 Overlay strings are sorted so that after-string strings come in
5369 front of before-string strings. Within before and after-strings,
5370 strings are sorted by overlay priority. See also function
5371 compare_overlay_entries. */
5372
5373 static void
5374 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5375 {
5376 Lisp_Object overlay, window, str, invisible;
5377 struct Lisp_Overlay *ov;
5378 ptrdiff_t start, end;
5379 ptrdiff_t size = 20;
5380 ptrdiff_t n = 0, i, j;
5381 int invis_p;
5382 struct overlay_entry *entries
5383 = (struct overlay_entry *) alloca (size * sizeof *entries);
5384 USE_SAFE_ALLOCA;
5385
5386 if (charpos <= 0)
5387 charpos = IT_CHARPOS (*it);
5388
5389 /* Append the overlay string STRING of overlay OVERLAY to vector
5390 `entries' which has size `size' and currently contains `n'
5391 elements. AFTER_P non-zero means STRING is an after-string of
5392 OVERLAY. */
5393 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5394 do \
5395 { \
5396 Lisp_Object priority; \
5397 \
5398 if (n == size) \
5399 { \
5400 struct overlay_entry *old = entries; \
5401 SAFE_NALLOCA (entries, 2, size); \
5402 memcpy (entries, old, size * sizeof *entries); \
5403 size *= 2; \
5404 } \
5405 \
5406 entries[n].string = (STRING); \
5407 entries[n].overlay = (OVERLAY); \
5408 priority = Foverlay_get ((OVERLAY), Qpriority); \
5409 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5410 entries[n].after_string_p = (AFTER_P); \
5411 ++n; \
5412 } \
5413 while (0)
5414
5415 /* Process overlay before the overlay center. */
5416 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5417 {
5418 XSETMISC (overlay, ov);
5419 xassert (OVERLAYP (overlay));
5420 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5421 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5422
5423 if (end < charpos)
5424 break;
5425
5426 /* Skip this overlay if it doesn't start or end at IT's current
5427 position. */
5428 if (end != charpos && start != charpos)
5429 continue;
5430
5431 /* Skip this overlay if it doesn't apply to IT->w. */
5432 window = Foverlay_get (overlay, Qwindow);
5433 if (WINDOWP (window) && XWINDOW (window) != it->w)
5434 continue;
5435
5436 /* If the text ``under'' the overlay is invisible, both before-
5437 and after-strings from this overlay are visible; start and
5438 end position are indistinguishable. */
5439 invisible = Foverlay_get (overlay, Qinvisible);
5440 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5441
5442 /* If overlay has a non-empty before-string, record it. */
5443 if ((start == charpos || (end == charpos && invis_p))
5444 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5445 && SCHARS (str))
5446 RECORD_OVERLAY_STRING (overlay, str, 0);
5447
5448 /* If overlay has a non-empty after-string, record it. */
5449 if ((end == charpos || (start == charpos && invis_p))
5450 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5451 && SCHARS (str))
5452 RECORD_OVERLAY_STRING (overlay, str, 1);
5453 }
5454
5455 /* Process overlays after the overlay center. */
5456 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5457 {
5458 XSETMISC (overlay, ov);
5459 xassert (OVERLAYP (overlay));
5460 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5461 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5462
5463 if (start > charpos)
5464 break;
5465
5466 /* Skip this overlay if it doesn't start or end at IT's current
5467 position. */
5468 if (end != charpos && start != charpos)
5469 continue;
5470
5471 /* Skip this overlay if it doesn't apply to IT->w. */
5472 window = Foverlay_get (overlay, Qwindow);
5473 if (WINDOWP (window) && XWINDOW (window) != it->w)
5474 continue;
5475
5476 /* If the text ``under'' the overlay is invisible, it has a zero
5477 dimension, and both before- and after-strings apply. */
5478 invisible = Foverlay_get (overlay, Qinvisible);
5479 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5480
5481 /* If overlay has a non-empty before-string, record it. */
5482 if ((start == charpos || (end == charpos && invis_p))
5483 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5484 && SCHARS (str))
5485 RECORD_OVERLAY_STRING (overlay, str, 0);
5486
5487 /* If overlay has a non-empty after-string, record it. */
5488 if ((end == charpos || (start == charpos && invis_p))
5489 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5490 && SCHARS (str))
5491 RECORD_OVERLAY_STRING (overlay, str, 1);
5492 }
5493
5494 #undef RECORD_OVERLAY_STRING
5495
5496 /* Sort entries. */
5497 if (n > 1)
5498 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5499
5500 /* Record number of overlay strings, and where we computed it. */
5501 it->n_overlay_strings = n;
5502 it->overlay_strings_charpos = charpos;
5503
5504 /* IT->current.overlay_string_index is the number of overlay strings
5505 that have already been consumed by IT. Copy some of the
5506 remaining overlay strings to IT->overlay_strings. */
5507 i = 0;
5508 j = it->current.overlay_string_index;
5509 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5510 {
5511 it->overlay_strings[i] = entries[j].string;
5512 it->string_overlays[i++] = entries[j++].overlay;
5513 }
5514
5515 CHECK_IT (it);
5516 SAFE_FREE ();
5517 }
5518
5519
5520 /* Get the first chunk of overlay strings at IT's current buffer
5521 position, or at CHARPOS if that is > 0. Value is non-zero if at
5522 least one overlay string was found. */
5523
5524 static int
5525 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5526 {
5527 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5528 process. This fills IT->overlay_strings with strings, and sets
5529 IT->n_overlay_strings to the total number of strings to process.
5530 IT->pos.overlay_string_index has to be set temporarily to zero
5531 because load_overlay_strings needs this; it must be set to -1
5532 when no overlay strings are found because a zero value would
5533 indicate a position in the first overlay string. */
5534 it->current.overlay_string_index = 0;
5535 load_overlay_strings (it, charpos);
5536
5537 /* If we found overlay strings, set up IT to deliver display
5538 elements from the first one. Otherwise set up IT to deliver
5539 from current_buffer. */
5540 if (it->n_overlay_strings)
5541 {
5542 /* Make sure we know settings in current_buffer, so that we can
5543 restore meaningful values when we're done with the overlay
5544 strings. */
5545 if (compute_stop_p)
5546 compute_stop_pos (it);
5547 xassert (it->face_id >= 0);
5548
5549 /* Save IT's settings. They are restored after all overlay
5550 strings have been processed. */
5551 xassert (!compute_stop_p || it->sp == 0);
5552
5553 /* When called from handle_stop, there might be an empty display
5554 string loaded. In that case, don't bother saving it. But
5555 don't use this optimization with the bidi iterator, since we
5556 need the corresponding pop_it call to resync the bidi
5557 iterator's position with IT's position, after we are done
5558 with the overlay strings. (The corresponding call to pop_it
5559 in case of an empty display string is in
5560 next_overlay_string.) */
5561 if (!(!it->bidi_p
5562 && STRINGP (it->string) && !SCHARS (it->string)))
5563 push_it (it, NULL);
5564
5565 /* Set up IT to deliver display elements from the first overlay
5566 string. */
5567 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5568 it->string = it->overlay_strings[0];
5569 it->from_overlay = Qnil;
5570 it->stop_charpos = 0;
5571 xassert (STRINGP (it->string));
5572 it->end_charpos = SCHARS (it->string);
5573 it->prev_stop = 0;
5574 it->base_level_stop = 0;
5575 it->multibyte_p = STRING_MULTIBYTE (it->string);
5576 it->method = GET_FROM_STRING;
5577 it->from_disp_prop_p = 0;
5578
5579 /* Force paragraph direction to be that of the parent
5580 buffer. */
5581 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5582 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5583 else
5584 it->paragraph_embedding = L2R;
5585
5586 /* Set up the bidi iterator for this overlay string. */
5587 if (it->bidi_p)
5588 {
5589 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5590
5591 it->bidi_it.string.lstring = it->string;
5592 it->bidi_it.string.s = NULL;
5593 it->bidi_it.string.schars = SCHARS (it->string);
5594 it->bidi_it.string.bufpos = pos;
5595 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5596 it->bidi_it.string.unibyte = !it->multibyte_p;
5597 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5598 }
5599 return 1;
5600 }
5601
5602 it->current.overlay_string_index = -1;
5603 return 0;
5604 }
5605
5606 static int
5607 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5608 {
5609 it->string = Qnil;
5610 it->method = GET_FROM_BUFFER;
5611
5612 (void) get_overlay_strings_1 (it, charpos, 1);
5613
5614 CHECK_IT (it);
5615
5616 /* Value is non-zero if we found at least one overlay string. */
5617 return STRINGP (it->string);
5618 }
5619
5620
5621 \f
5622 /***********************************************************************
5623 Saving and restoring state
5624 ***********************************************************************/
5625
5626 /* Save current settings of IT on IT->stack. Called, for example,
5627 before setting up IT for an overlay string, to be able to restore
5628 IT's settings to what they were after the overlay string has been
5629 processed. If POSITION is non-NULL, it is the position to save on
5630 the stack instead of IT->position. */
5631
5632 static void
5633 push_it (struct it *it, struct text_pos *position)
5634 {
5635 struct iterator_stack_entry *p;
5636
5637 xassert (it->sp < IT_STACK_SIZE);
5638 p = it->stack + it->sp;
5639
5640 p->stop_charpos = it->stop_charpos;
5641 p->prev_stop = it->prev_stop;
5642 p->base_level_stop = it->base_level_stop;
5643 p->cmp_it = it->cmp_it;
5644 xassert (it->face_id >= 0);
5645 p->face_id = it->face_id;
5646 p->string = it->string;
5647 p->method = it->method;
5648 p->from_overlay = it->from_overlay;
5649 switch (p->method)
5650 {
5651 case GET_FROM_IMAGE:
5652 p->u.image.object = it->object;
5653 p->u.image.image_id = it->image_id;
5654 p->u.image.slice = it->slice;
5655 break;
5656 case GET_FROM_STRETCH:
5657 p->u.stretch.object = it->object;
5658 break;
5659 }
5660 p->position = position ? *position : it->position;
5661 p->current = it->current;
5662 p->end_charpos = it->end_charpos;
5663 p->string_nchars = it->string_nchars;
5664 p->area = it->area;
5665 p->multibyte_p = it->multibyte_p;
5666 p->avoid_cursor_p = it->avoid_cursor_p;
5667 p->space_width = it->space_width;
5668 p->font_height = it->font_height;
5669 p->voffset = it->voffset;
5670 p->string_from_display_prop_p = it->string_from_display_prop_p;
5671 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5672 p->display_ellipsis_p = 0;
5673 p->line_wrap = it->line_wrap;
5674 p->bidi_p = it->bidi_p;
5675 p->paragraph_embedding = it->paragraph_embedding;
5676 p->from_disp_prop_p = it->from_disp_prop_p;
5677 ++it->sp;
5678
5679 /* Save the state of the bidi iterator as well. */
5680 if (it->bidi_p)
5681 bidi_push_it (&it->bidi_it);
5682 }
5683
5684 static void
5685 iterate_out_of_display_property (struct it *it)
5686 {
5687 int buffer_p = !STRINGP (it->string);
5688 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5689 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5690
5691 xassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5692
5693 /* Maybe initialize paragraph direction. If we are at the beginning
5694 of a new paragraph, next_element_from_buffer may not have a
5695 chance to do that. */
5696 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5697 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5698 /* prev_stop can be zero, so check against BEGV as well. */
5699 while (it->bidi_it.charpos >= bob
5700 && it->prev_stop <= it->bidi_it.charpos
5701 && it->bidi_it.charpos < CHARPOS (it->position)
5702 && it->bidi_it.charpos < eob)
5703 bidi_move_to_visually_next (&it->bidi_it);
5704 /* Record the stop_pos we just crossed, for when we cross it
5705 back, maybe. */
5706 if (it->bidi_it.charpos > CHARPOS (it->position))
5707 it->prev_stop = CHARPOS (it->position);
5708 /* If we ended up not where pop_it put us, resync IT's
5709 positional members with the bidi iterator. */
5710 if (it->bidi_it.charpos != CHARPOS (it->position))
5711 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5712 if (buffer_p)
5713 it->current.pos = it->position;
5714 else
5715 it->current.string_pos = it->position;
5716 }
5717
5718 /* Restore IT's settings from IT->stack. Called, for example, when no
5719 more overlay strings must be processed, and we return to delivering
5720 display elements from a buffer, or when the end of a string from a
5721 `display' property is reached and we return to delivering display
5722 elements from an overlay string, or from a buffer. */
5723
5724 static void
5725 pop_it (struct it *it)
5726 {
5727 struct iterator_stack_entry *p;
5728 int from_display_prop = it->from_disp_prop_p;
5729
5730 xassert (it->sp > 0);
5731 --it->sp;
5732 p = it->stack + it->sp;
5733 it->stop_charpos = p->stop_charpos;
5734 it->prev_stop = p->prev_stop;
5735 it->base_level_stop = p->base_level_stop;
5736 it->cmp_it = p->cmp_it;
5737 it->face_id = p->face_id;
5738 it->current = p->current;
5739 it->position = p->position;
5740 it->string = p->string;
5741 it->from_overlay = p->from_overlay;
5742 if (NILP (it->string))
5743 SET_TEXT_POS (it->current.string_pos, -1, -1);
5744 it->method = p->method;
5745 switch (it->method)
5746 {
5747 case GET_FROM_IMAGE:
5748 it->image_id = p->u.image.image_id;
5749 it->object = p->u.image.object;
5750 it->slice = p->u.image.slice;
5751 break;
5752 case GET_FROM_STRETCH:
5753 it->object = p->u.stretch.object;
5754 break;
5755 case GET_FROM_BUFFER:
5756 it->object = it->w->buffer;
5757 break;
5758 case GET_FROM_STRING:
5759 it->object = it->string;
5760 break;
5761 case GET_FROM_DISPLAY_VECTOR:
5762 if (it->s)
5763 it->method = GET_FROM_C_STRING;
5764 else if (STRINGP (it->string))
5765 it->method = GET_FROM_STRING;
5766 else
5767 {
5768 it->method = GET_FROM_BUFFER;
5769 it->object = it->w->buffer;
5770 }
5771 }
5772 it->end_charpos = p->end_charpos;
5773 it->string_nchars = p->string_nchars;
5774 it->area = p->area;
5775 it->multibyte_p = p->multibyte_p;
5776 it->avoid_cursor_p = p->avoid_cursor_p;
5777 it->space_width = p->space_width;
5778 it->font_height = p->font_height;
5779 it->voffset = p->voffset;
5780 it->string_from_display_prop_p = p->string_from_display_prop_p;
5781 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5782 it->line_wrap = p->line_wrap;
5783 it->bidi_p = p->bidi_p;
5784 it->paragraph_embedding = p->paragraph_embedding;
5785 it->from_disp_prop_p = p->from_disp_prop_p;
5786 if (it->bidi_p)
5787 {
5788 bidi_pop_it (&it->bidi_it);
5789 /* Bidi-iterate until we get out of the portion of text, if any,
5790 covered by a `display' text property or by an overlay with
5791 `display' property. (We cannot just jump there, because the
5792 internal coherency of the bidi iterator state can not be
5793 preserved across such jumps.) We also must determine the
5794 paragraph base direction if the overlay we just processed is
5795 at the beginning of a new paragraph. */
5796 if (from_display_prop
5797 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5798 iterate_out_of_display_property (it);
5799
5800 xassert ((BUFFERP (it->object)
5801 && IT_CHARPOS (*it) == it->bidi_it.charpos
5802 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5803 || (STRINGP (it->object)
5804 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5805 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5806 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5807 }
5808 }
5809
5810
5811 \f
5812 /***********************************************************************
5813 Moving over lines
5814 ***********************************************************************/
5815
5816 /* Set IT's current position to the previous line start. */
5817
5818 static void
5819 back_to_previous_line_start (struct it *it)
5820 {
5821 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5822 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5823 }
5824
5825
5826 /* Move IT to the next line start.
5827
5828 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5829 we skipped over part of the text (as opposed to moving the iterator
5830 continuously over the text). Otherwise, don't change the value
5831 of *SKIPPED_P.
5832
5833 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5834 iterator on the newline, if it was found.
5835
5836 Newlines may come from buffer text, overlay strings, or strings
5837 displayed via the `display' property. That's the reason we can't
5838 simply use find_next_newline_no_quit.
5839
5840 Note that this function may not skip over invisible text that is so
5841 because of text properties and immediately follows a newline. If
5842 it would, function reseat_at_next_visible_line_start, when called
5843 from set_iterator_to_next, would effectively make invisible
5844 characters following a newline part of the wrong glyph row, which
5845 leads to wrong cursor motion. */
5846
5847 static int
5848 forward_to_next_line_start (struct it *it, int *skipped_p,
5849 struct bidi_it *bidi_it_prev)
5850 {
5851 ptrdiff_t old_selective;
5852 int newline_found_p, n;
5853 const int MAX_NEWLINE_DISTANCE = 500;
5854
5855 /* If already on a newline, just consume it to avoid unintended
5856 skipping over invisible text below. */
5857 if (it->what == IT_CHARACTER
5858 && it->c == '\n'
5859 && CHARPOS (it->position) == IT_CHARPOS (*it))
5860 {
5861 if (it->bidi_p && bidi_it_prev)
5862 *bidi_it_prev = it->bidi_it;
5863 set_iterator_to_next (it, 0);
5864 it->c = 0;
5865 return 1;
5866 }
5867
5868 /* Don't handle selective display in the following. It's (a)
5869 unnecessary because it's done by the caller, and (b) leads to an
5870 infinite recursion because next_element_from_ellipsis indirectly
5871 calls this function. */
5872 old_selective = it->selective;
5873 it->selective = 0;
5874
5875 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5876 from buffer text. */
5877 for (n = newline_found_p = 0;
5878 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5879 n += STRINGP (it->string) ? 0 : 1)
5880 {
5881 if (!get_next_display_element (it))
5882 return 0;
5883 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5884 if (newline_found_p && it->bidi_p && bidi_it_prev)
5885 *bidi_it_prev = it->bidi_it;
5886 set_iterator_to_next (it, 0);
5887 }
5888
5889 /* If we didn't find a newline near enough, see if we can use a
5890 short-cut. */
5891 if (!newline_found_p)
5892 {
5893 ptrdiff_t start = IT_CHARPOS (*it);
5894 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5895 Lisp_Object pos;
5896
5897 xassert (!STRINGP (it->string));
5898
5899 /* If there isn't any `display' property in sight, and no
5900 overlays, we can just use the position of the newline in
5901 buffer text. */
5902 if (it->stop_charpos >= limit
5903 || ((pos = Fnext_single_property_change (make_number (start),
5904 Qdisplay, Qnil,
5905 make_number (limit)),
5906 NILP (pos))
5907 && next_overlay_change (start) == ZV))
5908 {
5909 if (!it->bidi_p)
5910 {
5911 IT_CHARPOS (*it) = limit;
5912 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
5913 }
5914 else
5915 {
5916 struct bidi_it bprev;
5917
5918 /* Help bidi.c avoid expensive searches for display
5919 properties and overlays, by telling it that there are
5920 none up to `limit'. */
5921 if (it->bidi_it.disp_pos < limit)
5922 {
5923 it->bidi_it.disp_pos = limit;
5924 it->bidi_it.disp_prop = 0;
5925 }
5926 do {
5927 bprev = it->bidi_it;
5928 bidi_move_to_visually_next (&it->bidi_it);
5929 } while (it->bidi_it.charpos != limit);
5930 IT_CHARPOS (*it) = limit;
5931 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
5932 if (bidi_it_prev)
5933 *bidi_it_prev = bprev;
5934 }
5935 *skipped_p = newline_found_p = 1;
5936 }
5937 else
5938 {
5939 while (get_next_display_element (it)
5940 && !newline_found_p)
5941 {
5942 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
5943 if (newline_found_p && it->bidi_p && bidi_it_prev)
5944 *bidi_it_prev = it->bidi_it;
5945 set_iterator_to_next (it, 0);
5946 }
5947 }
5948 }
5949
5950 it->selective = old_selective;
5951 return newline_found_p;
5952 }
5953
5954
5955 /* Set IT's current position to the previous visible line start. Skip
5956 invisible text that is so either due to text properties or due to
5957 selective display. Caution: this does not change IT->current_x and
5958 IT->hpos. */
5959
5960 static void
5961 back_to_previous_visible_line_start (struct it *it)
5962 {
5963 while (IT_CHARPOS (*it) > BEGV)
5964 {
5965 back_to_previous_line_start (it);
5966
5967 if (IT_CHARPOS (*it) <= BEGV)
5968 break;
5969
5970 /* If selective > 0, then lines indented more than its value are
5971 invisible. */
5972 if (it->selective > 0
5973 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
5974 it->selective))
5975 continue;
5976
5977 /* Check the newline before point for invisibility. */
5978 {
5979 Lisp_Object prop;
5980 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
5981 Qinvisible, it->window);
5982 if (TEXT_PROP_MEANS_INVISIBLE (prop))
5983 continue;
5984 }
5985
5986 if (IT_CHARPOS (*it) <= BEGV)
5987 break;
5988
5989 {
5990 struct it it2;
5991 void *it2data = NULL;
5992 ptrdiff_t pos;
5993 ptrdiff_t beg, end;
5994 Lisp_Object val, overlay;
5995
5996 SAVE_IT (it2, *it, it2data);
5997
5998 /* If newline is part of a composition, continue from start of composition */
5999 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6000 && beg < IT_CHARPOS (*it))
6001 goto replaced;
6002
6003 /* If newline is replaced by a display property, find start of overlay
6004 or interval and continue search from that point. */
6005 pos = --IT_CHARPOS (it2);
6006 --IT_BYTEPOS (it2);
6007 it2.sp = 0;
6008 bidi_unshelve_cache (NULL, 0);
6009 it2.string_from_display_prop_p = 0;
6010 it2.from_disp_prop_p = 0;
6011 if (handle_display_prop (&it2) == HANDLED_RETURN
6012 && !NILP (val = get_char_property_and_overlay
6013 (make_number (pos), Qdisplay, Qnil, &overlay))
6014 && (OVERLAYP (overlay)
6015 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6016 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6017 {
6018 RESTORE_IT (it, it, it2data);
6019 goto replaced;
6020 }
6021
6022 /* Newline is not replaced by anything -- so we are done. */
6023 RESTORE_IT (it, it, it2data);
6024 break;
6025
6026 replaced:
6027 if (beg < BEGV)
6028 beg = BEGV;
6029 IT_CHARPOS (*it) = beg;
6030 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6031 }
6032 }
6033
6034 it->continuation_lines_width = 0;
6035
6036 xassert (IT_CHARPOS (*it) >= BEGV);
6037 xassert (IT_CHARPOS (*it) == BEGV
6038 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6039 CHECK_IT (it);
6040 }
6041
6042
6043 /* Reseat iterator IT at the previous visible line start. Skip
6044 invisible text that is so either due to text properties or due to
6045 selective display. At the end, update IT's overlay information,
6046 face information etc. */
6047
6048 void
6049 reseat_at_previous_visible_line_start (struct it *it)
6050 {
6051 back_to_previous_visible_line_start (it);
6052 reseat (it, it->current.pos, 1);
6053 CHECK_IT (it);
6054 }
6055
6056
6057 /* Reseat iterator IT on the next visible line start in the current
6058 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6059 preceding the line start. Skip over invisible text that is so
6060 because of selective display. Compute faces, overlays etc at the
6061 new position. Note that this function does not skip over text that
6062 is invisible because of text properties. */
6063
6064 static void
6065 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6066 {
6067 int newline_found_p, skipped_p = 0;
6068 struct bidi_it bidi_it_prev;
6069
6070 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6071
6072 /* Skip over lines that are invisible because they are indented
6073 more than the value of IT->selective. */
6074 if (it->selective > 0)
6075 while (IT_CHARPOS (*it) < ZV
6076 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6077 it->selective))
6078 {
6079 xassert (IT_BYTEPOS (*it) == BEGV
6080 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6081 newline_found_p =
6082 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6083 }
6084
6085 /* Position on the newline if that's what's requested. */
6086 if (on_newline_p && newline_found_p)
6087 {
6088 if (STRINGP (it->string))
6089 {
6090 if (IT_STRING_CHARPOS (*it) > 0)
6091 {
6092 if (!it->bidi_p)
6093 {
6094 --IT_STRING_CHARPOS (*it);
6095 --IT_STRING_BYTEPOS (*it);
6096 }
6097 else
6098 {
6099 /* We need to restore the bidi iterator to the state
6100 it had on the newline, and resync the IT's
6101 position with that. */
6102 it->bidi_it = bidi_it_prev;
6103 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6104 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6105 }
6106 }
6107 }
6108 else if (IT_CHARPOS (*it) > BEGV)
6109 {
6110 if (!it->bidi_p)
6111 {
6112 --IT_CHARPOS (*it);
6113 --IT_BYTEPOS (*it);
6114 }
6115 else
6116 {
6117 /* We need to restore the bidi iterator to the state it
6118 had on the newline and resync IT with that. */
6119 it->bidi_it = bidi_it_prev;
6120 IT_CHARPOS (*it) = it->bidi_it.charpos;
6121 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6122 }
6123 reseat (it, it->current.pos, 0);
6124 }
6125 }
6126 else if (skipped_p)
6127 reseat (it, it->current.pos, 0);
6128
6129 CHECK_IT (it);
6130 }
6131
6132
6133 \f
6134 /***********************************************************************
6135 Changing an iterator's position
6136 ***********************************************************************/
6137
6138 /* Change IT's current position to POS in current_buffer. If FORCE_P
6139 is non-zero, always check for text properties at the new position.
6140 Otherwise, text properties are only looked up if POS >=
6141 IT->check_charpos of a property. */
6142
6143 static void
6144 reseat (struct it *it, struct text_pos pos, int force_p)
6145 {
6146 ptrdiff_t original_pos = IT_CHARPOS (*it);
6147
6148 reseat_1 (it, pos, 0);
6149
6150 /* Determine where to check text properties. Avoid doing it
6151 where possible because text property lookup is very expensive. */
6152 if (force_p
6153 || CHARPOS (pos) > it->stop_charpos
6154 || CHARPOS (pos) < original_pos)
6155 {
6156 if (it->bidi_p)
6157 {
6158 /* For bidi iteration, we need to prime prev_stop and
6159 base_level_stop with our best estimations. */
6160 /* Implementation note: Of course, POS is not necessarily a
6161 stop position, so assigning prev_pos to it is a lie; we
6162 should have called compute_stop_backwards. However, if
6163 the current buffer does not include any R2L characters,
6164 that call would be a waste of cycles, because the
6165 iterator will never move back, and thus never cross this
6166 "fake" stop position. So we delay that backward search
6167 until the time we really need it, in next_element_from_buffer. */
6168 if (CHARPOS (pos) != it->prev_stop)
6169 it->prev_stop = CHARPOS (pos);
6170 if (CHARPOS (pos) < it->base_level_stop)
6171 it->base_level_stop = 0; /* meaning it's unknown */
6172 handle_stop (it);
6173 }
6174 else
6175 {
6176 handle_stop (it);
6177 it->prev_stop = it->base_level_stop = 0;
6178 }
6179
6180 }
6181
6182 CHECK_IT (it);
6183 }
6184
6185
6186 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6187 IT->stop_pos to POS, also. */
6188
6189 static void
6190 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6191 {
6192 /* Don't call this function when scanning a C string. */
6193 xassert (it->s == NULL);
6194
6195 /* POS must be a reasonable value. */
6196 xassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6197
6198 it->current.pos = it->position = pos;
6199 it->end_charpos = ZV;
6200 it->dpvec = NULL;
6201 it->current.dpvec_index = -1;
6202 it->current.overlay_string_index = -1;
6203 IT_STRING_CHARPOS (*it) = -1;
6204 IT_STRING_BYTEPOS (*it) = -1;
6205 it->string = Qnil;
6206 it->method = GET_FROM_BUFFER;
6207 it->object = it->w->buffer;
6208 it->area = TEXT_AREA;
6209 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6210 it->sp = 0;
6211 it->string_from_display_prop_p = 0;
6212 it->string_from_prefix_prop_p = 0;
6213
6214 it->from_disp_prop_p = 0;
6215 it->face_before_selective_p = 0;
6216 if (it->bidi_p)
6217 {
6218 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6219 &it->bidi_it);
6220 bidi_unshelve_cache (NULL, 0);
6221 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6222 it->bidi_it.string.s = NULL;
6223 it->bidi_it.string.lstring = Qnil;
6224 it->bidi_it.string.bufpos = 0;
6225 it->bidi_it.string.unibyte = 0;
6226 }
6227
6228 if (set_stop_p)
6229 {
6230 it->stop_charpos = CHARPOS (pos);
6231 it->base_level_stop = CHARPOS (pos);
6232 }
6233 }
6234
6235
6236 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6237 If S is non-null, it is a C string to iterate over. Otherwise,
6238 STRING gives a Lisp string to iterate over.
6239
6240 If PRECISION > 0, don't return more then PRECISION number of
6241 characters from the string.
6242
6243 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6244 characters have been returned. FIELD_WIDTH < 0 means an infinite
6245 field width.
6246
6247 MULTIBYTE = 0 means disable processing of multibyte characters,
6248 MULTIBYTE > 0 means enable it,
6249 MULTIBYTE < 0 means use IT->multibyte_p.
6250
6251 IT must be initialized via a prior call to init_iterator before
6252 calling this function. */
6253
6254 static void
6255 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6256 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6257 int multibyte)
6258 {
6259 /* No region in strings. */
6260 it->region_beg_charpos = it->region_end_charpos = -1;
6261
6262 /* No text property checks performed by default, but see below. */
6263 it->stop_charpos = -1;
6264
6265 /* Set iterator position and end position. */
6266 memset (&it->current, 0, sizeof it->current);
6267 it->current.overlay_string_index = -1;
6268 it->current.dpvec_index = -1;
6269 xassert (charpos >= 0);
6270
6271 /* If STRING is specified, use its multibyteness, otherwise use the
6272 setting of MULTIBYTE, if specified. */
6273 if (multibyte >= 0)
6274 it->multibyte_p = multibyte > 0;
6275
6276 /* Bidirectional reordering of strings is controlled by the default
6277 value of bidi-display-reordering. Don't try to reorder while
6278 loading loadup.el, as the necessary character property tables are
6279 not yet available. */
6280 it->bidi_p =
6281 NILP (Vpurify_flag)
6282 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6283
6284 if (s == NULL)
6285 {
6286 xassert (STRINGP (string));
6287 it->string = string;
6288 it->s = NULL;
6289 it->end_charpos = it->string_nchars = SCHARS (string);
6290 it->method = GET_FROM_STRING;
6291 it->current.string_pos = string_pos (charpos, string);
6292
6293 if (it->bidi_p)
6294 {
6295 it->bidi_it.string.lstring = string;
6296 it->bidi_it.string.s = NULL;
6297 it->bidi_it.string.schars = it->end_charpos;
6298 it->bidi_it.string.bufpos = 0;
6299 it->bidi_it.string.from_disp_str = 0;
6300 it->bidi_it.string.unibyte = !it->multibyte_p;
6301 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6302 FRAME_WINDOW_P (it->f), &it->bidi_it);
6303 }
6304 }
6305 else
6306 {
6307 it->s = (const unsigned char *) s;
6308 it->string = Qnil;
6309
6310 /* Note that we use IT->current.pos, not it->current.string_pos,
6311 for displaying C strings. */
6312 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6313 if (it->multibyte_p)
6314 {
6315 it->current.pos = c_string_pos (charpos, s, 1);
6316 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6317 }
6318 else
6319 {
6320 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6321 it->end_charpos = it->string_nchars = strlen (s);
6322 }
6323
6324 if (it->bidi_p)
6325 {
6326 it->bidi_it.string.lstring = Qnil;
6327 it->bidi_it.string.s = (const unsigned char *) s;
6328 it->bidi_it.string.schars = it->end_charpos;
6329 it->bidi_it.string.bufpos = 0;
6330 it->bidi_it.string.from_disp_str = 0;
6331 it->bidi_it.string.unibyte = !it->multibyte_p;
6332 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6333 &it->bidi_it);
6334 }
6335 it->method = GET_FROM_C_STRING;
6336 }
6337
6338 /* PRECISION > 0 means don't return more than PRECISION characters
6339 from the string. */
6340 if (precision > 0 && it->end_charpos - charpos > precision)
6341 {
6342 it->end_charpos = it->string_nchars = charpos + precision;
6343 if (it->bidi_p)
6344 it->bidi_it.string.schars = it->end_charpos;
6345 }
6346
6347 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6348 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6349 FIELD_WIDTH < 0 means infinite field width. This is useful for
6350 padding with `-' at the end of a mode line. */
6351 if (field_width < 0)
6352 field_width = INFINITY;
6353 /* Implementation note: We deliberately don't enlarge
6354 it->bidi_it.string.schars here to fit it->end_charpos, because
6355 the bidi iterator cannot produce characters out of thin air. */
6356 if (field_width > it->end_charpos - charpos)
6357 it->end_charpos = charpos + field_width;
6358
6359 /* Use the standard display table for displaying strings. */
6360 if (DISP_TABLE_P (Vstandard_display_table))
6361 it->dp = XCHAR_TABLE (Vstandard_display_table);
6362
6363 it->stop_charpos = charpos;
6364 it->prev_stop = charpos;
6365 it->base_level_stop = 0;
6366 if (it->bidi_p)
6367 {
6368 it->bidi_it.first_elt = 1;
6369 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6370 it->bidi_it.disp_pos = -1;
6371 }
6372 if (s == NULL && it->multibyte_p)
6373 {
6374 ptrdiff_t endpos = SCHARS (it->string);
6375 if (endpos > it->end_charpos)
6376 endpos = it->end_charpos;
6377 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6378 it->string);
6379 }
6380 CHECK_IT (it);
6381 }
6382
6383
6384 \f
6385 /***********************************************************************
6386 Iteration
6387 ***********************************************************************/
6388
6389 /* Map enum it_method value to corresponding next_element_from_* function. */
6390
6391 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6392 {
6393 next_element_from_buffer,
6394 next_element_from_display_vector,
6395 next_element_from_string,
6396 next_element_from_c_string,
6397 next_element_from_image,
6398 next_element_from_stretch
6399 };
6400
6401 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6402
6403
6404 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6405 (possibly with the following characters). */
6406
6407 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6408 ((IT)->cmp_it.id >= 0 \
6409 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6410 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6411 END_CHARPOS, (IT)->w, \
6412 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6413 (IT)->string)))
6414
6415
6416 /* Lookup the char-table Vglyphless_char_display for character C (-1
6417 if we want information for no-font case), and return the display
6418 method symbol. By side-effect, update it->what and
6419 it->glyphless_method. This function is called from
6420 get_next_display_element for each character element, and from
6421 x_produce_glyphs when no suitable font was found. */
6422
6423 Lisp_Object
6424 lookup_glyphless_char_display (int c, struct it *it)
6425 {
6426 Lisp_Object glyphless_method = Qnil;
6427
6428 if (CHAR_TABLE_P (Vglyphless_char_display)
6429 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6430 {
6431 if (c >= 0)
6432 {
6433 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6434 if (CONSP (glyphless_method))
6435 glyphless_method = FRAME_WINDOW_P (it->f)
6436 ? XCAR (glyphless_method)
6437 : XCDR (glyphless_method);
6438 }
6439 else
6440 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6441 }
6442
6443 retry:
6444 if (NILP (glyphless_method))
6445 {
6446 if (c >= 0)
6447 /* The default is to display the character by a proper font. */
6448 return Qnil;
6449 /* The default for the no-font case is to display an empty box. */
6450 glyphless_method = Qempty_box;
6451 }
6452 if (EQ (glyphless_method, Qzero_width))
6453 {
6454 if (c >= 0)
6455 return glyphless_method;
6456 /* This method can't be used for the no-font case. */
6457 glyphless_method = Qempty_box;
6458 }
6459 if (EQ (glyphless_method, Qthin_space))
6460 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6461 else if (EQ (glyphless_method, Qempty_box))
6462 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6463 else if (EQ (glyphless_method, Qhex_code))
6464 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6465 else if (STRINGP (glyphless_method))
6466 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6467 else
6468 {
6469 /* Invalid value. We use the default method. */
6470 glyphless_method = Qnil;
6471 goto retry;
6472 }
6473 it->what = IT_GLYPHLESS;
6474 return glyphless_method;
6475 }
6476
6477 /* Load IT's display element fields with information about the next
6478 display element from the current position of IT. Value is zero if
6479 end of buffer (or C string) is reached. */
6480
6481 static struct frame *last_escape_glyph_frame = NULL;
6482 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6483 static int last_escape_glyph_merged_face_id = 0;
6484
6485 struct frame *last_glyphless_glyph_frame = NULL;
6486 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6487 int last_glyphless_glyph_merged_face_id = 0;
6488
6489 static int
6490 get_next_display_element (struct it *it)
6491 {
6492 /* Non-zero means that we found a display element. Zero means that
6493 we hit the end of what we iterate over. Performance note: the
6494 function pointer `method' used here turns out to be faster than
6495 using a sequence of if-statements. */
6496 int success_p;
6497
6498 get_next:
6499 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6500
6501 if (it->what == IT_CHARACTER)
6502 {
6503 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6504 and only if (a) the resolved directionality of that character
6505 is R..." */
6506 /* FIXME: Do we need an exception for characters from display
6507 tables? */
6508 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6509 it->c = bidi_mirror_char (it->c);
6510 /* Map via display table or translate control characters.
6511 IT->c, IT->len etc. have been set to the next character by
6512 the function call above. If we have a display table, and it
6513 contains an entry for IT->c, translate it. Don't do this if
6514 IT->c itself comes from a display table, otherwise we could
6515 end up in an infinite recursion. (An alternative could be to
6516 count the recursion depth of this function and signal an
6517 error when a certain maximum depth is reached.) Is it worth
6518 it? */
6519 if (success_p && it->dpvec == NULL)
6520 {
6521 Lisp_Object dv;
6522 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6523 int nonascii_space_p = 0;
6524 int nonascii_hyphen_p = 0;
6525 int c = it->c; /* This is the character to display. */
6526
6527 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6528 {
6529 xassert (SINGLE_BYTE_CHAR_P (c));
6530 if (unibyte_display_via_language_environment)
6531 {
6532 c = DECODE_CHAR (unibyte, c);
6533 if (c < 0)
6534 c = BYTE8_TO_CHAR (it->c);
6535 }
6536 else
6537 c = BYTE8_TO_CHAR (it->c);
6538 }
6539
6540 if (it->dp
6541 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6542 VECTORP (dv)))
6543 {
6544 struct Lisp_Vector *v = XVECTOR (dv);
6545
6546 /* Return the first character from the display table
6547 entry, if not empty. If empty, don't display the
6548 current character. */
6549 if (v->header.size)
6550 {
6551 it->dpvec_char_len = it->len;
6552 it->dpvec = v->contents;
6553 it->dpend = v->contents + v->header.size;
6554 it->current.dpvec_index = 0;
6555 it->dpvec_face_id = -1;
6556 it->saved_face_id = it->face_id;
6557 it->method = GET_FROM_DISPLAY_VECTOR;
6558 it->ellipsis_p = 0;
6559 }
6560 else
6561 {
6562 set_iterator_to_next (it, 0);
6563 }
6564 goto get_next;
6565 }
6566
6567 if (! NILP (lookup_glyphless_char_display (c, it)))
6568 {
6569 if (it->what == IT_GLYPHLESS)
6570 goto done;
6571 /* Don't display this character. */
6572 set_iterator_to_next (it, 0);
6573 goto get_next;
6574 }
6575
6576 /* If `nobreak-char-display' is non-nil, we display
6577 non-ASCII spaces and hyphens specially. */
6578 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6579 {
6580 if (c == 0xA0)
6581 nonascii_space_p = 1;
6582 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6583 nonascii_hyphen_p = 1;
6584 }
6585
6586 /* Translate control characters into `\003' or `^C' form.
6587 Control characters coming from a display table entry are
6588 currently not translated because we use IT->dpvec to hold
6589 the translation. This could easily be changed but I
6590 don't believe that it is worth doing.
6591
6592 The characters handled by `nobreak-char-display' must be
6593 translated too.
6594
6595 Non-printable characters and raw-byte characters are also
6596 translated to octal form. */
6597 if (((c < ' ' || c == 127) /* ASCII control chars */
6598 ? (it->area != TEXT_AREA
6599 /* In mode line, treat \n, \t like other crl chars. */
6600 || (c != '\t'
6601 && it->glyph_row
6602 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6603 || (c != '\n' && c != '\t'))
6604 : (nonascii_space_p
6605 || nonascii_hyphen_p
6606 || CHAR_BYTE8_P (c)
6607 || ! CHAR_PRINTABLE_P (c))))
6608 {
6609 /* C is a control character, non-ASCII space/hyphen,
6610 raw-byte, or a non-printable character which must be
6611 displayed either as '\003' or as `^C' where the '\\'
6612 and '^' can be defined in the display table. Fill
6613 IT->ctl_chars with glyphs for what we have to
6614 display. Then, set IT->dpvec to these glyphs. */
6615 Lisp_Object gc;
6616 int ctl_len;
6617 int face_id;
6618 int lface_id = 0;
6619 int escape_glyph;
6620
6621 /* Handle control characters with ^. */
6622
6623 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6624 {
6625 int g;
6626
6627 g = '^'; /* default glyph for Control */
6628 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6629 if (it->dp
6630 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6631 {
6632 g = GLYPH_CODE_CHAR (gc);
6633 lface_id = GLYPH_CODE_FACE (gc);
6634 }
6635 if (lface_id)
6636 {
6637 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6638 }
6639 else if (it->f == last_escape_glyph_frame
6640 && it->face_id == last_escape_glyph_face_id)
6641 {
6642 face_id = last_escape_glyph_merged_face_id;
6643 }
6644 else
6645 {
6646 /* Merge the escape-glyph face into the current face. */
6647 face_id = merge_faces (it->f, Qescape_glyph, 0,
6648 it->face_id);
6649 last_escape_glyph_frame = it->f;
6650 last_escape_glyph_face_id = it->face_id;
6651 last_escape_glyph_merged_face_id = face_id;
6652 }
6653
6654 XSETINT (it->ctl_chars[0], g);
6655 XSETINT (it->ctl_chars[1], c ^ 0100);
6656 ctl_len = 2;
6657 goto display_control;
6658 }
6659
6660 /* Handle non-ascii space in the mode where it only gets
6661 highlighting. */
6662
6663 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6664 {
6665 /* Merge `nobreak-space' into the current face. */
6666 face_id = merge_faces (it->f, Qnobreak_space, 0,
6667 it->face_id);
6668 XSETINT (it->ctl_chars[0], ' ');
6669 ctl_len = 1;
6670 goto display_control;
6671 }
6672
6673 /* Handle sequences that start with the "escape glyph". */
6674
6675 /* the default escape glyph is \. */
6676 escape_glyph = '\\';
6677
6678 if (it->dp
6679 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6680 {
6681 escape_glyph = GLYPH_CODE_CHAR (gc);
6682 lface_id = GLYPH_CODE_FACE (gc);
6683 }
6684 if (lface_id)
6685 {
6686 /* The display table specified a face.
6687 Merge it into face_id and also into escape_glyph. */
6688 face_id = merge_faces (it->f, Qt, lface_id,
6689 it->face_id);
6690 }
6691 else if (it->f == last_escape_glyph_frame
6692 && it->face_id == last_escape_glyph_face_id)
6693 {
6694 face_id = last_escape_glyph_merged_face_id;
6695 }
6696 else
6697 {
6698 /* Merge the escape-glyph face into the current face. */
6699 face_id = merge_faces (it->f, Qescape_glyph, 0,
6700 it->face_id);
6701 last_escape_glyph_frame = it->f;
6702 last_escape_glyph_face_id = it->face_id;
6703 last_escape_glyph_merged_face_id = face_id;
6704 }
6705
6706 /* Draw non-ASCII hyphen with just highlighting: */
6707
6708 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6709 {
6710 XSETINT (it->ctl_chars[0], '-');
6711 ctl_len = 1;
6712 goto display_control;
6713 }
6714
6715 /* Draw non-ASCII space/hyphen with escape glyph: */
6716
6717 if (nonascii_space_p || nonascii_hyphen_p)
6718 {
6719 XSETINT (it->ctl_chars[0], escape_glyph);
6720 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6721 ctl_len = 2;
6722 goto display_control;
6723 }
6724
6725 {
6726 char str[10];
6727 int len, i;
6728
6729 if (CHAR_BYTE8_P (c))
6730 /* Display \200 instead of \17777600. */
6731 c = CHAR_TO_BYTE8 (c);
6732 len = sprintf (str, "%03o", c);
6733
6734 XSETINT (it->ctl_chars[0], escape_glyph);
6735 for (i = 0; i < len; i++)
6736 XSETINT (it->ctl_chars[i + 1], str[i]);
6737 ctl_len = len + 1;
6738 }
6739
6740 display_control:
6741 /* Set up IT->dpvec and return first character from it. */
6742 it->dpvec_char_len = it->len;
6743 it->dpvec = it->ctl_chars;
6744 it->dpend = it->dpvec + ctl_len;
6745 it->current.dpvec_index = 0;
6746 it->dpvec_face_id = face_id;
6747 it->saved_face_id = it->face_id;
6748 it->method = GET_FROM_DISPLAY_VECTOR;
6749 it->ellipsis_p = 0;
6750 goto get_next;
6751 }
6752 it->char_to_display = c;
6753 }
6754 else if (success_p)
6755 {
6756 it->char_to_display = it->c;
6757 }
6758 }
6759
6760 /* Adjust face id for a multibyte character. There are no multibyte
6761 character in unibyte text. */
6762 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6763 && it->multibyte_p
6764 && success_p
6765 && FRAME_WINDOW_P (it->f))
6766 {
6767 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6768
6769 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6770 {
6771 /* Automatic composition with glyph-string. */
6772 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6773
6774 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6775 }
6776 else
6777 {
6778 ptrdiff_t pos = (it->s ? -1
6779 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6780 : IT_CHARPOS (*it));
6781 int c;
6782
6783 if (it->what == IT_CHARACTER)
6784 c = it->char_to_display;
6785 else
6786 {
6787 struct composition *cmp = composition_table[it->cmp_it.id];
6788 int i;
6789
6790 c = ' ';
6791 for (i = 0; i < cmp->glyph_len; i++)
6792 /* TAB in a composition means display glyphs with
6793 padding space on the left or right. */
6794 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6795 break;
6796 }
6797 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6798 }
6799 }
6800
6801 done:
6802 /* Is this character the last one of a run of characters with
6803 box? If yes, set IT->end_of_box_run_p to 1. */
6804 if (it->face_box_p
6805 && it->s == NULL)
6806 {
6807 if (it->method == GET_FROM_STRING && it->sp)
6808 {
6809 int face_id = underlying_face_id (it);
6810 struct face *face = FACE_FROM_ID (it->f, face_id);
6811
6812 if (face)
6813 {
6814 if (face->box == FACE_NO_BOX)
6815 {
6816 /* If the box comes from face properties in a
6817 display string, check faces in that string. */
6818 int string_face_id = face_after_it_pos (it);
6819 it->end_of_box_run_p
6820 = (FACE_FROM_ID (it->f, string_face_id)->box
6821 == FACE_NO_BOX);
6822 }
6823 /* Otherwise, the box comes from the underlying face.
6824 If this is the last string character displayed, check
6825 the next buffer location. */
6826 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6827 && (it->current.overlay_string_index
6828 == it->n_overlay_strings - 1))
6829 {
6830 ptrdiff_t ignore;
6831 int next_face_id;
6832 struct text_pos pos = it->current.pos;
6833 INC_TEXT_POS (pos, it->multibyte_p);
6834
6835 next_face_id = face_at_buffer_position
6836 (it->w, CHARPOS (pos), it->region_beg_charpos,
6837 it->region_end_charpos, &ignore,
6838 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6839 -1);
6840 it->end_of_box_run_p
6841 = (FACE_FROM_ID (it->f, next_face_id)->box
6842 == FACE_NO_BOX);
6843 }
6844 }
6845 }
6846 else
6847 {
6848 int face_id = face_after_it_pos (it);
6849 it->end_of_box_run_p
6850 = (face_id != it->face_id
6851 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6852 }
6853 }
6854 /* If we reached the end of the object we've been iterating (e.g., a
6855 display string or an overlay string), and there's something on
6856 IT->stack, proceed with what's on the stack. It doesn't make
6857 sense to return zero if there's unprocessed stuff on the stack,
6858 because otherwise that stuff will never be displayed. */
6859 if (!success_p && it->sp > 0)
6860 {
6861 set_iterator_to_next (it, 0);
6862 success_p = get_next_display_element (it);
6863 }
6864
6865 /* Value is 0 if end of buffer or string reached. */
6866 return success_p;
6867 }
6868
6869
6870 /* Move IT to the next display element.
6871
6872 RESEAT_P non-zero means if called on a newline in buffer text,
6873 skip to the next visible line start.
6874
6875 Functions get_next_display_element and set_iterator_to_next are
6876 separate because I find this arrangement easier to handle than a
6877 get_next_display_element function that also increments IT's
6878 position. The way it is we can first look at an iterator's current
6879 display element, decide whether it fits on a line, and if it does,
6880 increment the iterator position. The other way around we probably
6881 would either need a flag indicating whether the iterator has to be
6882 incremented the next time, or we would have to implement a
6883 decrement position function which would not be easy to write. */
6884
6885 void
6886 set_iterator_to_next (struct it *it, int reseat_p)
6887 {
6888 /* Reset flags indicating start and end of a sequence of characters
6889 with box. Reset them at the start of this function because
6890 moving the iterator to a new position might set them. */
6891 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6892
6893 switch (it->method)
6894 {
6895 case GET_FROM_BUFFER:
6896 /* The current display element of IT is a character from
6897 current_buffer. Advance in the buffer, and maybe skip over
6898 invisible lines that are so because of selective display. */
6899 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
6900 reseat_at_next_visible_line_start (it, 0);
6901 else if (it->cmp_it.id >= 0)
6902 {
6903 /* We are currently getting glyphs from a composition. */
6904 int i;
6905
6906 if (! it->bidi_p)
6907 {
6908 IT_CHARPOS (*it) += it->cmp_it.nchars;
6909 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
6910 if (it->cmp_it.to < it->cmp_it.nglyphs)
6911 {
6912 it->cmp_it.from = it->cmp_it.to;
6913 }
6914 else
6915 {
6916 it->cmp_it.id = -1;
6917 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6918 IT_BYTEPOS (*it),
6919 it->end_charpos, Qnil);
6920 }
6921 }
6922 else if (! it->cmp_it.reversed_p)
6923 {
6924 /* Composition created while scanning forward. */
6925 /* Update IT's char/byte positions to point to the first
6926 character of the next grapheme cluster, or to the
6927 character visually after the current composition. */
6928 for (i = 0; i < it->cmp_it.nchars; i++)
6929 bidi_move_to_visually_next (&it->bidi_it);
6930 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6931 IT_CHARPOS (*it) = it->bidi_it.charpos;
6932
6933 if (it->cmp_it.to < it->cmp_it.nglyphs)
6934 {
6935 /* Proceed to the next grapheme cluster. */
6936 it->cmp_it.from = it->cmp_it.to;
6937 }
6938 else
6939 {
6940 /* No more grapheme clusters in this composition.
6941 Find the next stop position. */
6942 ptrdiff_t stop = it->end_charpos;
6943 if (it->bidi_it.scan_dir < 0)
6944 /* Now we are scanning backward and don't know
6945 where to stop. */
6946 stop = -1;
6947 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6948 IT_BYTEPOS (*it), stop, Qnil);
6949 }
6950 }
6951 else
6952 {
6953 /* Composition created while scanning backward. */
6954 /* Update IT's char/byte positions to point to the last
6955 character of the previous grapheme cluster, or the
6956 character visually after the current composition. */
6957 for (i = 0; i < it->cmp_it.nchars; i++)
6958 bidi_move_to_visually_next (&it->bidi_it);
6959 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6960 IT_CHARPOS (*it) = it->bidi_it.charpos;
6961 if (it->cmp_it.from > 0)
6962 {
6963 /* Proceed to the previous grapheme cluster. */
6964 it->cmp_it.to = it->cmp_it.from;
6965 }
6966 else
6967 {
6968 /* No more grapheme clusters in this composition.
6969 Find the next stop position. */
6970 ptrdiff_t stop = it->end_charpos;
6971 if (it->bidi_it.scan_dir < 0)
6972 /* Now we are scanning backward and don't know
6973 where to stop. */
6974 stop = -1;
6975 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
6976 IT_BYTEPOS (*it), stop, Qnil);
6977 }
6978 }
6979 }
6980 else
6981 {
6982 xassert (it->len != 0);
6983
6984 if (!it->bidi_p)
6985 {
6986 IT_BYTEPOS (*it) += it->len;
6987 IT_CHARPOS (*it) += 1;
6988 }
6989 else
6990 {
6991 int prev_scan_dir = it->bidi_it.scan_dir;
6992 /* If this is a new paragraph, determine its base
6993 direction (a.k.a. its base embedding level). */
6994 if (it->bidi_it.new_paragraph)
6995 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
6996 bidi_move_to_visually_next (&it->bidi_it);
6997 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6998 IT_CHARPOS (*it) = it->bidi_it.charpos;
6999 if (prev_scan_dir != it->bidi_it.scan_dir)
7000 {
7001 /* As the scan direction was changed, we must
7002 re-compute the stop position for composition. */
7003 ptrdiff_t stop = it->end_charpos;
7004 if (it->bidi_it.scan_dir < 0)
7005 stop = -1;
7006 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7007 IT_BYTEPOS (*it), stop, Qnil);
7008 }
7009 }
7010 xassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7011 }
7012 break;
7013
7014 case GET_FROM_C_STRING:
7015 /* Current display element of IT is from a C string. */
7016 if (!it->bidi_p
7017 /* If the string position is beyond string's end, it means
7018 next_element_from_c_string is padding the string with
7019 blanks, in which case we bypass the bidi iterator,
7020 because it cannot deal with such virtual characters. */
7021 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7022 {
7023 IT_BYTEPOS (*it) += it->len;
7024 IT_CHARPOS (*it) += 1;
7025 }
7026 else
7027 {
7028 bidi_move_to_visually_next (&it->bidi_it);
7029 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7030 IT_CHARPOS (*it) = it->bidi_it.charpos;
7031 }
7032 break;
7033
7034 case GET_FROM_DISPLAY_VECTOR:
7035 /* Current display element of IT is from a display table entry.
7036 Advance in the display table definition. Reset it to null if
7037 end reached, and continue with characters from buffers/
7038 strings. */
7039 ++it->current.dpvec_index;
7040
7041 /* Restore face of the iterator to what they were before the
7042 display vector entry (these entries may contain faces). */
7043 it->face_id = it->saved_face_id;
7044
7045 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7046 {
7047 int recheck_faces = it->ellipsis_p;
7048
7049 if (it->s)
7050 it->method = GET_FROM_C_STRING;
7051 else if (STRINGP (it->string))
7052 it->method = GET_FROM_STRING;
7053 else
7054 {
7055 it->method = GET_FROM_BUFFER;
7056 it->object = it->w->buffer;
7057 }
7058
7059 it->dpvec = NULL;
7060 it->current.dpvec_index = -1;
7061
7062 /* Skip over characters which were displayed via IT->dpvec. */
7063 if (it->dpvec_char_len < 0)
7064 reseat_at_next_visible_line_start (it, 1);
7065 else if (it->dpvec_char_len > 0)
7066 {
7067 if (it->method == GET_FROM_STRING
7068 && it->n_overlay_strings > 0)
7069 it->ignore_overlay_strings_at_pos_p = 1;
7070 it->len = it->dpvec_char_len;
7071 set_iterator_to_next (it, reseat_p);
7072 }
7073
7074 /* Maybe recheck faces after display vector */
7075 if (recheck_faces)
7076 it->stop_charpos = IT_CHARPOS (*it);
7077 }
7078 break;
7079
7080 case GET_FROM_STRING:
7081 /* Current display element is a character from a Lisp string. */
7082 xassert (it->s == NULL && STRINGP (it->string));
7083 /* Don't advance past string end. These conditions are true
7084 when set_iterator_to_next is called at the end of
7085 get_next_display_element, in which case the Lisp string is
7086 already exhausted, and all we want is pop the iterator
7087 stack. */
7088 if (it->current.overlay_string_index >= 0)
7089 {
7090 /* This is an overlay string, so there's no padding with
7091 spaces, and the number of characters in the string is
7092 where the string ends. */
7093 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7094 goto consider_string_end;
7095 }
7096 else
7097 {
7098 /* Not an overlay string. There could be padding, so test
7099 against it->end_charpos . */
7100 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7101 goto consider_string_end;
7102 }
7103 if (it->cmp_it.id >= 0)
7104 {
7105 int i;
7106
7107 if (! it->bidi_p)
7108 {
7109 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7110 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7111 if (it->cmp_it.to < it->cmp_it.nglyphs)
7112 it->cmp_it.from = it->cmp_it.to;
7113 else
7114 {
7115 it->cmp_it.id = -1;
7116 composition_compute_stop_pos (&it->cmp_it,
7117 IT_STRING_CHARPOS (*it),
7118 IT_STRING_BYTEPOS (*it),
7119 it->end_charpos, it->string);
7120 }
7121 }
7122 else if (! it->cmp_it.reversed_p)
7123 {
7124 for (i = 0; i < it->cmp_it.nchars; i++)
7125 bidi_move_to_visually_next (&it->bidi_it);
7126 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7127 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7128
7129 if (it->cmp_it.to < it->cmp_it.nglyphs)
7130 it->cmp_it.from = it->cmp_it.to;
7131 else
7132 {
7133 ptrdiff_t stop = it->end_charpos;
7134 if (it->bidi_it.scan_dir < 0)
7135 stop = -1;
7136 composition_compute_stop_pos (&it->cmp_it,
7137 IT_STRING_CHARPOS (*it),
7138 IT_STRING_BYTEPOS (*it), stop,
7139 it->string);
7140 }
7141 }
7142 else
7143 {
7144 for (i = 0; i < it->cmp_it.nchars; i++)
7145 bidi_move_to_visually_next (&it->bidi_it);
7146 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7147 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7148 if (it->cmp_it.from > 0)
7149 it->cmp_it.to = it->cmp_it.from;
7150 else
7151 {
7152 ptrdiff_t stop = it->end_charpos;
7153 if (it->bidi_it.scan_dir < 0)
7154 stop = -1;
7155 composition_compute_stop_pos (&it->cmp_it,
7156 IT_STRING_CHARPOS (*it),
7157 IT_STRING_BYTEPOS (*it), stop,
7158 it->string);
7159 }
7160 }
7161 }
7162 else
7163 {
7164 if (!it->bidi_p
7165 /* If the string position is beyond string's end, it
7166 means next_element_from_string is padding the string
7167 with blanks, in which case we bypass the bidi
7168 iterator, because it cannot deal with such virtual
7169 characters. */
7170 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7171 {
7172 IT_STRING_BYTEPOS (*it) += it->len;
7173 IT_STRING_CHARPOS (*it) += 1;
7174 }
7175 else
7176 {
7177 int prev_scan_dir = it->bidi_it.scan_dir;
7178
7179 bidi_move_to_visually_next (&it->bidi_it);
7180 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7181 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7182 if (prev_scan_dir != it->bidi_it.scan_dir)
7183 {
7184 ptrdiff_t stop = it->end_charpos;
7185
7186 if (it->bidi_it.scan_dir < 0)
7187 stop = -1;
7188 composition_compute_stop_pos (&it->cmp_it,
7189 IT_STRING_CHARPOS (*it),
7190 IT_STRING_BYTEPOS (*it), stop,
7191 it->string);
7192 }
7193 }
7194 }
7195
7196 consider_string_end:
7197
7198 if (it->current.overlay_string_index >= 0)
7199 {
7200 /* IT->string is an overlay string. Advance to the
7201 next, if there is one. */
7202 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7203 {
7204 it->ellipsis_p = 0;
7205 next_overlay_string (it);
7206 if (it->ellipsis_p)
7207 setup_for_ellipsis (it, 0);
7208 }
7209 }
7210 else
7211 {
7212 /* IT->string is not an overlay string. If we reached
7213 its end, and there is something on IT->stack, proceed
7214 with what is on the stack. This can be either another
7215 string, this time an overlay string, or a buffer. */
7216 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7217 && it->sp > 0)
7218 {
7219 pop_it (it);
7220 if (it->method == GET_FROM_STRING)
7221 goto consider_string_end;
7222 }
7223 }
7224 break;
7225
7226 case GET_FROM_IMAGE:
7227 case GET_FROM_STRETCH:
7228 /* The position etc with which we have to proceed are on
7229 the stack. The position may be at the end of a string,
7230 if the `display' property takes up the whole string. */
7231 xassert (it->sp > 0);
7232 pop_it (it);
7233 if (it->method == GET_FROM_STRING)
7234 goto consider_string_end;
7235 break;
7236
7237 default:
7238 /* There are no other methods defined, so this should be a bug. */
7239 abort ();
7240 }
7241
7242 xassert (it->method != GET_FROM_STRING
7243 || (STRINGP (it->string)
7244 && IT_STRING_CHARPOS (*it) >= 0));
7245 }
7246
7247 /* Load IT's display element fields with information about the next
7248 display element which comes from a display table entry or from the
7249 result of translating a control character to one of the forms `^C'
7250 or `\003'.
7251
7252 IT->dpvec holds the glyphs to return as characters.
7253 IT->saved_face_id holds the face id before the display vector--it
7254 is restored into IT->face_id in set_iterator_to_next. */
7255
7256 static int
7257 next_element_from_display_vector (struct it *it)
7258 {
7259 Lisp_Object gc;
7260
7261 /* Precondition. */
7262 xassert (it->dpvec && it->current.dpvec_index >= 0);
7263
7264 it->face_id = it->saved_face_id;
7265
7266 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7267 That seemed totally bogus - so I changed it... */
7268 gc = it->dpvec[it->current.dpvec_index];
7269
7270 if (GLYPH_CODE_P (gc))
7271 {
7272 it->c = GLYPH_CODE_CHAR (gc);
7273 it->len = CHAR_BYTES (it->c);
7274
7275 /* The entry may contain a face id to use. Such a face id is
7276 the id of a Lisp face, not a realized face. A face id of
7277 zero means no face is specified. */
7278 if (it->dpvec_face_id >= 0)
7279 it->face_id = it->dpvec_face_id;
7280 else
7281 {
7282 int lface_id = GLYPH_CODE_FACE (gc);
7283 if (lface_id > 0)
7284 it->face_id = merge_faces (it->f, Qt, lface_id,
7285 it->saved_face_id);
7286 }
7287 }
7288 else
7289 /* Display table entry is invalid. Return a space. */
7290 it->c = ' ', it->len = 1;
7291
7292 /* Don't change position and object of the iterator here. They are
7293 still the values of the character that had this display table
7294 entry or was translated, and that's what we want. */
7295 it->what = IT_CHARACTER;
7296 return 1;
7297 }
7298
7299 /* Get the first element of string/buffer in the visual order, after
7300 being reseated to a new position in a string or a buffer. */
7301 static void
7302 get_visually_first_element (struct it *it)
7303 {
7304 int string_p = STRINGP (it->string) || it->s;
7305 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7306 ptrdiff_t bob = (string_p ? 0 : BEGV);
7307
7308 if (STRINGP (it->string))
7309 {
7310 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7311 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7312 }
7313 else
7314 {
7315 it->bidi_it.charpos = IT_CHARPOS (*it);
7316 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7317 }
7318
7319 if (it->bidi_it.charpos == eob)
7320 {
7321 /* Nothing to do, but reset the FIRST_ELT flag, like
7322 bidi_paragraph_init does, because we are not going to
7323 call it. */
7324 it->bidi_it.first_elt = 0;
7325 }
7326 else if (it->bidi_it.charpos == bob
7327 || (!string_p
7328 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7329 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7330 {
7331 /* If we are at the beginning of a line/string, we can produce
7332 the next element right away. */
7333 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7334 bidi_move_to_visually_next (&it->bidi_it);
7335 }
7336 else
7337 {
7338 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7339
7340 /* We need to prime the bidi iterator starting at the line's or
7341 string's beginning, before we will be able to produce the
7342 next element. */
7343 if (string_p)
7344 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7345 else
7346 {
7347 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7348 -1);
7349 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7350 }
7351 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7352 do
7353 {
7354 /* Now return to buffer/string position where we were asked
7355 to get the next display element, and produce that. */
7356 bidi_move_to_visually_next (&it->bidi_it);
7357 }
7358 while (it->bidi_it.bytepos != orig_bytepos
7359 && it->bidi_it.charpos < eob);
7360 }
7361
7362 /* Adjust IT's position information to where we ended up. */
7363 if (STRINGP (it->string))
7364 {
7365 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7366 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7367 }
7368 else
7369 {
7370 IT_CHARPOS (*it) = it->bidi_it.charpos;
7371 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7372 }
7373
7374 if (STRINGP (it->string) || !it->s)
7375 {
7376 ptrdiff_t stop, charpos, bytepos;
7377
7378 if (STRINGP (it->string))
7379 {
7380 xassert (!it->s);
7381 stop = SCHARS (it->string);
7382 if (stop > it->end_charpos)
7383 stop = it->end_charpos;
7384 charpos = IT_STRING_CHARPOS (*it);
7385 bytepos = IT_STRING_BYTEPOS (*it);
7386 }
7387 else
7388 {
7389 stop = it->end_charpos;
7390 charpos = IT_CHARPOS (*it);
7391 bytepos = IT_BYTEPOS (*it);
7392 }
7393 if (it->bidi_it.scan_dir < 0)
7394 stop = -1;
7395 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7396 it->string);
7397 }
7398 }
7399
7400 /* Load IT with the next display element from Lisp string IT->string.
7401 IT->current.string_pos is the current position within the string.
7402 If IT->current.overlay_string_index >= 0, the Lisp string is an
7403 overlay string. */
7404
7405 static int
7406 next_element_from_string (struct it *it)
7407 {
7408 struct text_pos position;
7409
7410 xassert (STRINGP (it->string));
7411 xassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7412 xassert (IT_STRING_CHARPOS (*it) >= 0);
7413 position = it->current.string_pos;
7414
7415 /* With bidi reordering, the character to display might not be the
7416 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7417 that we were reseat()ed to a new string, whose paragraph
7418 direction is not known. */
7419 if (it->bidi_p && it->bidi_it.first_elt)
7420 {
7421 get_visually_first_element (it);
7422 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7423 }
7424
7425 /* Time to check for invisible text? */
7426 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7427 {
7428 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7429 {
7430 if (!(!it->bidi_p
7431 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7432 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7433 {
7434 /* With bidi non-linear iteration, we could find
7435 ourselves far beyond the last computed stop_charpos,
7436 with several other stop positions in between that we
7437 missed. Scan them all now, in buffer's logical
7438 order, until we find and handle the last stop_charpos
7439 that precedes our current position. */
7440 handle_stop_backwards (it, it->stop_charpos);
7441 return GET_NEXT_DISPLAY_ELEMENT (it);
7442 }
7443 else
7444 {
7445 if (it->bidi_p)
7446 {
7447 /* Take note of the stop position we just moved
7448 across, for when we will move back across it. */
7449 it->prev_stop = it->stop_charpos;
7450 /* If we are at base paragraph embedding level, take
7451 note of the last stop position seen at this
7452 level. */
7453 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7454 it->base_level_stop = it->stop_charpos;
7455 }
7456 handle_stop (it);
7457
7458 /* Since a handler may have changed IT->method, we must
7459 recurse here. */
7460 return GET_NEXT_DISPLAY_ELEMENT (it);
7461 }
7462 }
7463 else if (it->bidi_p
7464 /* If we are before prev_stop, we may have overstepped
7465 on our way backwards a stop_pos, and if so, we need
7466 to handle that stop_pos. */
7467 && IT_STRING_CHARPOS (*it) < it->prev_stop
7468 /* We can sometimes back up for reasons that have nothing
7469 to do with bidi reordering. E.g., compositions. The
7470 code below is only needed when we are above the base
7471 embedding level, so test for that explicitly. */
7472 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7473 {
7474 /* If we lost track of base_level_stop, we have no better
7475 place for handle_stop_backwards to start from than string
7476 beginning. This happens, e.g., when we were reseated to
7477 the previous screenful of text by vertical-motion. */
7478 if (it->base_level_stop <= 0
7479 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7480 it->base_level_stop = 0;
7481 handle_stop_backwards (it, it->base_level_stop);
7482 return GET_NEXT_DISPLAY_ELEMENT (it);
7483 }
7484 }
7485
7486 if (it->current.overlay_string_index >= 0)
7487 {
7488 /* Get the next character from an overlay string. In overlay
7489 strings, there is no field width or padding with spaces to
7490 do. */
7491 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7492 {
7493 it->what = IT_EOB;
7494 return 0;
7495 }
7496 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7497 IT_STRING_BYTEPOS (*it),
7498 it->bidi_it.scan_dir < 0
7499 ? -1
7500 : SCHARS (it->string))
7501 && next_element_from_composition (it))
7502 {
7503 return 1;
7504 }
7505 else if (STRING_MULTIBYTE (it->string))
7506 {
7507 const unsigned char *s = (SDATA (it->string)
7508 + IT_STRING_BYTEPOS (*it));
7509 it->c = string_char_and_length (s, &it->len);
7510 }
7511 else
7512 {
7513 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7514 it->len = 1;
7515 }
7516 }
7517 else
7518 {
7519 /* Get the next character from a Lisp string that is not an
7520 overlay string. Such strings come from the mode line, for
7521 example. We may have to pad with spaces, or truncate the
7522 string. See also next_element_from_c_string. */
7523 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7524 {
7525 it->what = IT_EOB;
7526 return 0;
7527 }
7528 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7529 {
7530 /* Pad with spaces. */
7531 it->c = ' ', it->len = 1;
7532 CHARPOS (position) = BYTEPOS (position) = -1;
7533 }
7534 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7535 IT_STRING_BYTEPOS (*it),
7536 it->bidi_it.scan_dir < 0
7537 ? -1
7538 : it->string_nchars)
7539 && next_element_from_composition (it))
7540 {
7541 return 1;
7542 }
7543 else if (STRING_MULTIBYTE (it->string))
7544 {
7545 const unsigned char *s = (SDATA (it->string)
7546 + IT_STRING_BYTEPOS (*it));
7547 it->c = string_char_and_length (s, &it->len);
7548 }
7549 else
7550 {
7551 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7552 it->len = 1;
7553 }
7554 }
7555
7556 /* Record what we have and where it came from. */
7557 it->what = IT_CHARACTER;
7558 it->object = it->string;
7559 it->position = position;
7560 return 1;
7561 }
7562
7563
7564 /* Load IT with next display element from C string IT->s.
7565 IT->string_nchars is the maximum number of characters to return
7566 from the string. IT->end_charpos may be greater than
7567 IT->string_nchars when this function is called, in which case we
7568 may have to return padding spaces. Value is zero if end of string
7569 reached, including padding spaces. */
7570
7571 static int
7572 next_element_from_c_string (struct it *it)
7573 {
7574 int success_p = 1;
7575
7576 xassert (it->s);
7577 xassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7578 it->what = IT_CHARACTER;
7579 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7580 it->object = Qnil;
7581
7582 /* With bidi reordering, the character to display might not be the
7583 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7584 we were reseated to a new string, whose paragraph direction is
7585 not known. */
7586 if (it->bidi_p && it->bidi_it.first_elt)
7587 get_visually_first_element (it);
7588
7589 /* IT's position can be greater than IT->string_nchars in case a
7590 field width or precision has been specified when the iterator was
7591 initialized. */
7592 if (IT_CHARPOS (*it) >= it->end_charpos)
7593 {
7594 /* End of the game. */
7595 it->what = IT_EOB;
7596 success_p = 0;
7597 }
7598 else if (IT_CHARPOS (*it) >= it->string_nchars)
7599 {
7600 /* Pad with spaces. */
7601 it->c = ' ', it->len = 1;
7602 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7603 }
7604 else if (it->multibyte_p)
7605 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7606 else
7607 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7608
7609 return success_p;
7610 }
7611
7612
7613 /* Set up IT to return characters from an ellipsis, if appropriate.
7614 The definition of the ellipsis glyphs may come from a display table
7615 entry. This function fills IT with the first glyph from the
7616 ellipsis if an ellipsis is to be displayed. */
7617
7618 static int
7619 next_element_from_ellipsis (struct it *it)
7620 {
7621 if (it->selective_display_ellipsis_p)
7622 setup_for_ellipsis (it, it->len);
7623 else
7624 {
7625 /* The face at the current position may be different from the
7626 face we find after the invisible text. Remember what it
7627 was in IT->saved_face_id, and signal that it's there by
7628 setting face_before_selective_p. */
7629 it->saved_face_id = it->face_id;
7630 it->method = GET_FROM_BUFFER;
7631 it->object = it->w->buffer;
7632 reseat_at_next_visible_line_start (it, 1);
7633 it->face_before_selective_p = 1;
7634 }
7635
7636 return GET_NEXT_DISPLAY_ELEMENT (it);
7637 }
7638
7639
7640 /* Deliver an image display element. The iterator IT is already
7641 filled with image information (done in handle_display_prop). Value
7642 is always 1. */
7643
7644
7645 static int
7646 next_element_from_image (struct it *it)
7647 {
7648 it->what = IT_IMAGE;
7649 it->ignore_overlay_strings_at_pos_p = 0;
7650 return 1;
7651 }
7652
7653
7654 /* Fill iterator IT with next display element from a stretch glyph
7655 property. IT->object is the value of the text property. Value is
7656 always 1. */
7657
7658 static int
7659 next_element_from_stretch (struct it *it)
7660 {
7661 it->what = IT_STRETCH;
7662 return 1;
7663 }
7664
7665 /* Scan backwards from IT's current position until we find a stop
7666 position, or until BEGV. This is called when we find ourself
7667 before both the last known prev_stop and base_level_stop while
7668 reordering bidirectional text. */
7669
7670 static void
7671 compute_stop_pos_backwards (struct it *it)
7672 {
7673 const int SCAN_BACK_LIMIT = 1000;
7674 struct text_pos pos;
7675 struct display_pos save_current = it->current;
7676 struct text_pos save_position = it->position;
7677 ptrdiff_t charpos = IT_CHARPOS (*it);
7678 ptrdiff_t where_we_are = charpos;
7679 ptrdiff_t save_stop_pos = it->stop_charpos;
7680 ptrdiff_t save_end_pos = it->end_charpos;
7681
7682 xassert (NILP (it->string) && !it->s);
7683 xassert (it->bidi_p);
7684 it->bidi_p = 0;
7685 do
7686 {
7687 it->end_charpos = min (charpos + 1, ZV);
7688 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7689 SET_TEXT_POS (pos, charpos, BYTE_TO_CHAR (charpos));
7690 reseat_1 (it, pos, 0);
7691 compute_stop_pos (it);
7692 /* We must advance forward, right? */
7693 if (it->stop_charpos <= charpos)
7694 abort ();
7695 }
7696 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7697
7698 if (it->stop_charpos <= where_we_are)
7699 it->prev_stop = it->stop_charpos;
7700 else
7701 it->prev_stop = BEGV;
7702 it->bidi_p = 1;
7703 it->current = save_current;
7704 it->position = save_position;
7705 it->stop_charpos = save_stop_pos;
7706 it->end_charpos = save_end_pos;
7707 }
7708
7709 /* Scan forward from CHARPOS in the current buffer/string, until we
7710 find a stop position > current IT's position. Then handle the stop
7711 position before that. This is called when we bump into a stop
7712 position while reordering bidirectional text. CHARPOS should be
7713 the last previously processed stop_pos (or BEGV/0, if none were
7714 processed yet) whose position is less that IT's current
7715 position. */
7716
7717 static void
7718 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7719 {
7720 int bufp = !STRINGP (it->string);
7721 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7722 struct display_pos save_current = it->current;
7723 struct text_pos save_position = it->position;
7724 struct text_pos pos1;
7725 ptrdiff_t next_stop;
7726
7727 /* Scan in strict logical order. */
7728 xassert (it->bidi_p);
7729 it->bidi_p = 0;
7730 do
7731 {
7732 it->prev_stop = charpos;
7733 if (bufp)
7734 {
7735 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7736 reseat_1 (it, pos1, 0);
7737 }
7738 else
7739 it->current.string_pos = string_pos (charpos, it->string);
7740 compute_stop_pos (it);
7741 /* We must advance forward, right? */
7742 if (it->stop_charpos <= it->prev_stop)
7743 abort ();
7744 charpos = it->stop_charpos;
7745 }
7746 while (charpos <= where_we_are);
7747
7748 it->bidi_p = 1;
7749 it->current = save_current;
7750 it->position = save_position;
7751 next_stop = it->stop_charpos;
7752 it->stop_charpos = it->prev_stop;
7753 handle_stop (it);
7754 it->stop_charpos = next_stop;
7755 }
7756
7757 /* Load IT with the next display element from current_buffer. Value
7758 is zero if end of buffer reached. IT->stop_charpos is the next
7759 position at which to stop and check for text properties or buffer
7760 end. */
7761
7762 static int
7763 next_element_from_buffer (struct it *it)
7764 {
7765 int success_p = 1;
7766
7767 xassert (IT_CHARPOS (*it) >= BEGV);
7768 xassert (NILP (it->string) && !it->s);
7769 xassert (!it->bidi_p
7770 || (EQ (it->bidi_it.string.lstring, Qnil)
7771 && it->bidi_it.string.s == NULL));
7772
7773 /* With bidi reordering, the character to display might not be the
7774 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7775 we were reseat()ed to a new buffer position, which is potentially
7776 a different paragraph. */
7777 if (it->bidi_p && it->bidi_it.first_elt)
7778 {
7779 get_visually_first_element (it);
7780 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7781 }
7782
7783 if (IT_CHARPOS (*it) >= it->stop_charpos)
7784 {
7785 if (IT_CHARPOS (*it) >= it->end_charpos)
7786 {
7787 int overlay_strings_follow_p;
7788
7789 /* End of the game, except when overlay strings follow that
7790 haven't been returned yet. */
7791 if (it->overlay_strings_at_end_processed_p)
7792 overlay_strings_follow_p = 0;
7793 else
7794 {
7795 it->overlay_strings_at_end_processed_p = 1;
7796 overlay_strings_follow_p = get_overlay_strings (it, 0);
7797 }
7798
7799 if (overlay_strings_follow_p)
7800 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7801 else
7802 {
7803 it->what = IT_EOB;
7804 it->position = it->current.pos;
7805 success_p = 0;
7806 }
7807 }
7808 else if (!(!it->bidi_p
7809 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7810 || IT_CHARPOS (*it) == it->stop_charpos))
7811 {
7812 /* With bidi non-linear iteration, we could find ourselves
7813 far beyond the last computed stop_charpos, with several
7814 other stop positions in between that we missed. Scan
7815 them all now, in buffer's logical order, until we find
7816 and handle the last stop_charpos that precedes our
7817 current position. */
7818 handle_stop_backwards (it, it->stop_charpos);
7819 return GET_NEXT_DISPLAY_ELEMENT (it);
7820 }
7821 else
7822 {
7823 if (it->bidi_p)
7824 {
7825 /* Take note of the stop position we just moved across,
7826 for when we will move back across it. */
7827 it->prev_stop = it->stop_charpos;
7828 /* If we are at base paragraph embedding level, take
7829 note of the last stop position seen at this
7830 level. */
7831 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7832 it->base_level_stop = it->stop_charpos;
7833 }
7834 handle_stop (it);
7835 return GET_NEXT_DISPLAY_ELEMENT (it);
7836 }
7837 }
7838 else if (it->bidi_p
7839 /* If we are before prev_stop, we may have overstepped on
7840 our way backwards a stop_pos, and if so, we need to
7841 handle that stop_pos. */
7842 && IT_CHARPOS (*it) < it->prev_stop
7843 /* We can sometimes back up for reasons that have nothing
7844 to do with bidi reordering. E.g., compositions. The
7845 code below is only needed when we are above the base
7846 embedding level, so test for that explicitly. */
7847 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7848 {
7849 if (it->base_level_stop <= 0
7850 || IT_CHARPOS (*it) < it->base_level_stop)
7851 {
7852 /* If we lost track of base_level_stop, we need to find
7853 prev_stop by looking backwards. This happens, e.g., when
7854 we were reseated to the previous screenful of text by
7855 vertical-motion. */
7856 it->base_level_stop = BEGV;
7857 compute_stop_pos_backwards (it);
7858 handle_stop_backwards (it, it->prev_stop);
7859 }
7860 else
7861 handle_stop_backwards (it, it->base_level_stop);
7862 return GET_NEXT_DISPLAY_ELEMENT (it);
7863 }
7864 else
7865 {
7866 /* No face changes, overlays etc. in sight, so just return a
7867 character from current_buffer. */
7868 unsigned char *p;
7869 ptrdiff_t stop;
7870
7871 /* Maybe run the redisplay end trigger hook. Performance note:
7872 This doesn't seem to cost measurable time. */
7873 if (it->redisplay_end_trigger_charpos
7874 && it->glyph_row
7875 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7876 run_redisplay_end_trigger_hook (it);
7877
7878 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7879 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7880 stop)
7881 && next_element_from_composition (it))
7882 {
7883 return 1;
7884 }
7885
7886 /* Get the next character, maybe multibyte. */
7887 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7888 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7889 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7890 else
7891 it->c = *p, it->len = 1;
7892
7893 /* Record what we have and where it came from. */
7894 it->what = IT_CHARACTER;
7895 it->object = it->w->buffer;
7896 it->position = it->current.pos;
7897
7898 /* Normally we return the character found above, except when we
7899 really want to return an ellipsis for selective display. */
7900 if (it->selective)
7901 {
7902 if (it->c == '\n')
7903 {
7904 /* A value of selective > 0 means hide lines indented more
7905 than that number of columns. */
7906 if (it->selective > 0
7907 && IT_CHARPOS (*it) + 1 < ZV
7908 && indented_beyond_p (IT_CHARPOS (*it) + 1,
7909 IT_BYTEPOS (*it) + 1,
7910 it->selective))
7911 {
7912 success_p = next_element_from_ellipsis (it);
7913 it->dpvec_char_len = -1;
7914 }
7915 }
7916 else if (it->c == '\r' && it->selective == -1)
7917 {
7918 /* A value of selective == -1 means that everything from the
7919 CR to the end of the line is invisible, with maybe an
7920 ellipsis displayed for it. */
7921 success_p = next_element_from_ellipsis (it);
7922 it->dpvec_char_len = -1;
7923 }
7924 }
7925 }
7926
7927 /* Value is zero if end of buffer reached. */
7928 xassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
7929 return success_p;
7930 }
7931
7932
7933 /* Run the redisplay end trigger hook for IT. */
7934
7935 static void
7936 run_redisplay_end_trigger_hook (struct it *it)
7937 {
7938 Lisp_Object args[3];
7939
7940 /* IT->glyph_row should be non-null, i.e. we should be actually
7941 displaying something, or otherwise we should not run the hook. */
7942 xassert (it->glyph_row);
7943
7944 /* Set up hook arguments. */
7945 args[0] = Qredisplay_end_trigger_functions;
7946 args[1] = it->window;
7947 XSETINT (args[2], it->redisplay_end_trigger_charpos);
7948 it->redisplay_end_trigger_charpos = 0;
7949
7950 /* Since we are *trying* to run these functions, don't try to run
7951 them again, even if they get an error. */
7952 it->w->redisplay_end_trigger = Qnil;
7953 Frun_hook_with_args (3, args);
7954
7955 /* Notice if it changed the face of the character we are on. */
7956 handle_face_prop (it);
7957 }
7958
7959
7960 /* Deliver a composition display element. Unlike the other
7961 next_element_from_XXX, this function is not registered in the array
7962 get_next_element[]. It is called from next_element_from_buffer and
7963 next_element_from_string when necessary. */
7964
7965 static int
7966 next_element_from_composition (struct it *it)
7967 {
7968 it->what = IT_COMPOSITION;
7969 it->len = it->cmp_it.nbytes;
7970 if (STRINGP (it->string))
7971 {
7972 if (it->c < 0)
7973 {
7974 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7975 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7976 return 0;
7977 }
7978 it->position = it->current.string_pos;
7979 it->object = it->string;
7980 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
7981 IT_STRING_BYTEPOS (*it), it->string);
7982 }
7983 else
7984 {
7985 if (it->c < 0)
7986 {
7987 IT_CHARPOS (*it) += it->cmp_it.nchars;
7988 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7989 if (it->bidi_p)
7990 {
7991 if (it->bidi_it.new_paragraph)
7992 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7993 /* Resync the bidi iterator with IT's new position.
7994 FIXME: this doesn't support bidirectional text. */
7995 while (it->bidi_it.charpos < IT_CHARPOS (*it))
7996 bidi_move_to_visually_next (&it->bidi_it);
7997 }
7998 return 0;
7999 }
8000 it->position = it->current.pos;
8001 it->object = it->w->buffer;
8002 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8003 IT_BYTEPOS (*it), Qnil);
8004 }
8005 return 1;
8006 }
8007
8008
8009 \f
8010 /***********************************************************************
8011 Moving an iterator without producing glyphs
8012 ***********************************************************************/
8013
8014 /* Check if iterator is at a position corresponding to a valid buffer
8015 position after some move_it_ call. */
8016
8017 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8018 ((it)->method == GET_FROM_STRING \
8019 ? IT_STRING_CHARPOS (*it) == 0 \
8020 : 1)
8021
8022
8023 /* Move iterator IT to a specified buffer or X position within one
8024 line on the display without producing glyphs.
8025
8026 OP should be a bit mask including some or all of these bits:
8027 MOVE_TO_X: Stop upon reaching x-position TO_X.
8028 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8029 Regardless of OP's value, stop upon reaching the end of the display line.
8030
8031 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8032 This means, in particular, that TO_X includes window's horizontal
8033 scroll amount.
8034
8035 The return value has several possible values that
8036 say what condition caused the scan to stop:
8037
8038 MOVE_POS_MATCH_OR_ZV
8039 - when TO_POS or ZV was reached.
8040
8041 MOVE_X_REACHED
8042 -when TO_X was reached before TO_POS or ZV were reached.
8043
8044 MOVE_LINE_CONTINUED
8045 - when we reached the end of the display area and the line must
8046 be continued.
8047
8048 MOVE_LINE_TRUNCATED
8049 - when we reached the end of the display area and the line is
8050 truncated.
8051
8052 MOVE_NEWLINE_OR_CR
8053 - when we stopped at a line end, i.e. a newline or a CR and selective
8054 display is on. */
8055
8056 static enum move_it_result
8057 move_it_in_display_line_to (struct it *it,
8058 ptrdiff_t to_charpos, int to_x,
8059 enum move_operation_enum op)
8060 {
8061 enum move_it_result result = MOVE_UNDEFINED;
8062 struct glyph_row *saved_glyph_row;
8063 struct it wrap_it, atpos_it, atx_it, ppos_it;
8064 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8065 void *ppos_data = NULL;
8066 int may_wrap = 0;
8067 enum it_method prev_method = it->method;
8068 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8069 int saw_smaller_pos = prev_pos < to_charpos;
8070
8071 /* Don't produce glyphs in produce_glyphs. */
8072 saved_glyph_row = it->glyph_row;
8073 it->glyph_row = NULL;
8074
8075 /* Use wrap_it to save a copy of IT wherever a word wrap could
8076 occur. Use atpos_it to save a copy of IT at the desired buffer
8077 position, if found, so that we can scan ahead and check if the
8078 word later overshoots the window edge. Use atx_it similarly, for
8079 pixel positions. */
8080 wrap_it.sp = -1;
8081 atpos_it.sp = -1;
8082 atx_it.sp = -1;
8083
8084 /* Use ppos_it under bidi reordering to save a copy of IT for the
8085 position > CHARPOS that is the closest to CHARPOS. We restore
8086 that position in IT when we have scanned the entire display line
8087 without finding a match for CHARPOS and all the character
8088 positions are greater than CHARPOS. */
8089 if (it->bidi_p)
8090 {
8091 SAVE_IT (ppos_it, *it, ppos_data);
8092 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8093 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8094 SAVE_IT (ppos_it, *it, ppos_data);
8095 }
8096
8097 #define BUFFER_POS_REACHED_P() \
8098 ((op & MOVE_TO_POS) != 0 \
8099 && BUFFERP (it->object) \
8100 && (IT_CHARPOS (*it) == to_charpos \
8101 || ((!it->bidi_p \
8102 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8103 && IT_CHARPOS (*it) > to_charpos) \
8104 || (it->what == IT_COMPOSITION \
8105 && ((IT_CHARPOS (*it) > to_charpos \
8106 && to_charpos >= it->cmp_it.charpos) \
8107 || (IT_CHARPOS (*it) < to_charpos \
8108 && to_charpos <= it->cmp_it.charpos)))) \
8109 && (it->method == GET_FROM_BUFFER \
8110 || (it->method == GET_FROM_DISPLAY_VECTOR \
8111 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8112
8113 /* If there's a line-/wrap-prefix, handle it. */
8114 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8115 && it->current_y < it->last_visible_y)
8116 handle_line_prefix (it);
8117
8118 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8119 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8120
8121 while (1)
8122 {
8123 int x, i, ascent = 0, descent = 0;
8124
8125 /* Utility macro to reset an iterator with x, ascent, and descent. */
8126 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8127 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8128 (IT)->max_descent = descent)
8129
8130 /* Stop if we move beyond TO_CHARPOS (after an image or a
8131 display string or stretch glyph). */
8132 if ((op & MOVE_TO_POS) != 0
8133 && BUFFERP (it->object)
8134 && it->method == GET_FROM_BUFFER
8135 && (((!it->bidi_p
8136 /* When the iterator is at base embedding level, we
8137 are guaranteed that characters are delivered for
8138 display in strictly increasing order of their
8139 buffer positions. */
8140 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8141 && IT_CHARPOS (*it) > to_charpos)
8142 || (it->bidi_p
8143 && (prev_method == GET_FROM_IMAGE
8144 || prev_method == GET_FROM_STRETCH
8145 || prev_method == GET_FROM_STRING)
8146 /* Passed TO_CHARPOS from left to right. */
8147 && ((prev_pos < to_charpos
8148 && IT_CHARPOS (*it) > to_charpos)
8149 /* Passed TO_CHARPOS from right to left. */
8150 || (prev_pos > to_charpos
8151 && IT_CHARPOS (*it) < to_charpos)))))
8152 {
8153 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8154 {
8155 result = MOVE_POS_MATCH_OR_ZV;
8156 break;
8157 }
8158 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8159 /* If wrap_it is valid, the current position might be in a
8160 word that is wrapped. So, save the iterator in
8161 atpos_it and continue to see if wrapping happens. */
8162 SAVE_IT (atpos_it, *it, atpos_data);
8163 }
8164
8165 /* Stop when ZV reached.
8166 We used to stop here when TO_CHARPOS reached as well, but that is
8167 too soon if this glyph does not fit on this line. So we handle it
8168 explicitly below. */
8169 if (!get_next_display_element (it))
8170 {
8171 result = MOVE_POS_MATCH_OR_ZV;
8172 break;
8173 }
8174
8175 if (it->line_wrap == TRUNCATE)
8176 {
8177 if (BUFFER_POS_REACHED_P ())
8178 {
8179 result = MOVE_POS_MATCH_OR_ZV;
8180 break;
8181 }
8182 }
8183 else
8184 {
8185 if (it->line_wrap == WORD_WRAP)
8186 {
8187 if (IT_DISPLAYING_WHITESPACE (it))
8188 may_wrap = 1;
8189 else if (may_wrap)
8190 {
8191 /* We have reached a glyph that follows one or more
8192 whitespace characters. If the position is
8193 already found, we are done. */
8194 if (atpos_it.sp >= 0)
8195 {
8196 RESTORE_IT (it, &atpos_it, atpos_data);
8197 result = MOVE_POS_MATCH_OR_ZV;
8198 goto done;
8199 }
8200 if (atx_it.sp >= 0)
8201 {
8202 RESTORE_IT (it, &atx_it, atx_data);
8203 result = MOVE_X_REACHED;
8204 goto done;
8205 }
8206 /* Otherwise, we can wrap here. */
8207 SAVE_IT (wrap_it, *it, wrap_data);
8208 may_wrap = 0;
8209 }
8210 }
8211 }
8212
8213 /* Remember the line height for the current line, in case
8214 the next element doesn't fit on the line. */
8215 ascent = it->max_ascent;
8216 descent = it->max_descent;
8217
8218 /* The call to produce_glyphs will get the metrics of the
8219 display element IT is loaded with. Record the x-position
8220 before this display element, in case it doesn't fit on the
8221 line. */
8222 x = it->current_x;
8223
8224 PRODUCE_GLYPHS (it);
8225
8226 if (it->area != TEXT_AREA)
8227 {
8228 prev_method = it->method;
8229 if (it->method == GET_FROM_BUFFER)
8230 prev_pos = IT_CHARPOS (*it);
8231 set_iterator_to_next (it, 1);
8232 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8233 SET_TEXT_POS (this_line_min_pos,
8234 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8235 if (it->bidi_p
8236 && (op & MOVE_TO_POS)
8237 && IT_CHARPOS (*it) > to_charpos
8238 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8239 SAVE_IT (ppos_it, *it, ppos_data);
8240 continue;
8241 }
8242
8243 /* The number of glyphs we get back in IT->nglyphs will normally
8244 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8245 character on a terminal frame, or (iii) a line end. For the
8246 second case, IT->nglyphs - 1 padding glyphs will be present.
8247 (On X frames, there is only one glyph produced for a
8248 composite character.)
8249
8250 The behavior implemented below means, for continuation lines,
8251 that as many spaces of a TAB as fit on the current line are
8252 displayed there. For terminal frames, as many glyphs of a
8253 multi-glyph character are displayed in the current line, too.
8254 This is what the old redisplay code did, and we keep it that
8255 way. Under X, the whole shape of a complex character must
8256 fit on the line or it will be completely displayed in the
8257 next line.
8258
8259 Note that both for tabs and padding glyphs, all glyphs have
8260 the same width. */
8261 if (it->nglyphs)
8262 {
8263 /* More than one glyph or glyph doesn't fit on line. All
8264 glyphs have the same width. */
8265 int single_glyph_width = it->pixel_width / it->nglyphs;
8266 int new_x;
8267 int x_before_this_char = x;
8268 int hpos_before_this_char = it->hpos;
8269
8270 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8271 {
8272 new_x = x + single_glyph_width;
8273
8274 /* We want to leave anything reaching TO_X to the caller. */
8275 if ((op & MOVE_TO_X) && new_x > to_x)
8276 {
8277 if (BUFFER_POS_REACHED_P ())
8278 {
8279 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8280 goto buffer_pos_reached;
8281 if (atpos_it.sp < 0)
8282 {
8283 SAVE_IT (atpos_it, *it, atpos_data);
8284 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8285 }
8286 }
8287 else
8288 {
8289 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8290 {
8291 it->current_x = x;
8292 result = MOVE_X_REACHED;
8293 break;
8294 }
8295 if (atx_it.sp < 0)
8296 {
8297 SAVE_IT (atx_it, *it, atx_data);
8298 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8299 }
8300 }
8301 }
8302
8303 if (/* Lines are continued. */
8304 it->line_wrap != TRUNCATE
8305 && (/* And glyph doesn't fit on the line. */
8306 new_x > it->last_visible_x
8307 /* Or it fits exactly and we're on a window
8308 system frame. */
8309 || (new_x == it->last_visible_x
8310 && FRAME_WINDOW_P (it->f))))
8311 {
8312 if (/* IT->hpos == 0 means the very first glyph
8313 doesn't fit on the line, e.g. a wide image. */
8314 it->hpos == 0
8315 || (new_x == it->last_visible_x
8316 && FRAME_WINDOW_P (it->f)))
8317 {
8318 ++it->hpos;
8319 it->current_x = new_x;
8320
8321 /* The character's last glyph just barely fits
8322 in this row. */
8323 if (i == it->nglyphs - 1)
8324 {
8325 /* If this is the destination position,
8326 return a position *before* it in this row,
8327 now that we know it fits in this row. */
8328 if (BUFFER_POS_REACHED_P ())
8329 {
8330 if (it->line_wrap != WORD_WRAP
8331 || wrap_it.sp < 0)
8332 {
8333 it->hpos = hpos_before_this_char;
8334 it->current_x = x_before_this_char;
8335 result = MOVE_POS_MATCH_OR_ZV;
8336 break;
8337 }
8338 if (it->line_wrap == WORD_WRAP
8339 && atpos_it.sp < 0)
8340 {
8341 SAVE_IT (atpos_it, *it, atpos_data);
8342 atpos_it.current_x = x_before_this_char;
8343 atpos_it.hpos = hpos_before_this_char;
8344 }
8345 }
8346
8347 prev_method = it->method;
8348 if (it->method == GET_FROM_BUFFER)
8349 prev_pos = IT_CHARPOS (*it);
8350 set_iterator_to_next (it, 1);
8351 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8352 SET_TEXT_POS (this_line_min_pos,
8353 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8354 /* On graphical terminals, newlines may
8355 "overflow" into the fringe if
8356 overflow-newline-into-fringe is non-nil.
8357 On text-only terminals, newlines may
8358 overflow into the last glyph on the
8359 display line.*/
8360 if (!FRAME_WINDOW_P (it->f)
8361 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8362 {
8363 if (!get_next_display_element (it))
8364 {
8365 result = MOVE_POS_MATCH_OR_ZV;
8366 break;
8367 }
8368 if (BUFFER_POS_REACHED_P ())
8369 {
8370 if (ITERATOR_AT_END_OF_LINE_P (it))
8371 result = MOVE_POS_MATCH_OR_ZV;
8372 else
8373 result = MOVE_LINE_CONTINUED;
8374 break;
8375 }
8376 if (ITERATOR_AT_END_OF_LINE_P (it))
8377 {
8378 result = MOVE_NEWLINE_OR_CR;
8379 break;
8380 }
8381 }
8382 }
8383 }
8384 else
8385 IT_RESET_X_ASCENT_DESCENT (it);
8386
8387 if (wrap_it.sp >= 0)
8388 {
8389 RESTORE_IT (it, &wrap_it, wrap_data);
8390 atpos_it.sp = -1;
8391 atx_it.sp = -1;
8392 }
8393
8394 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8395 IT_CHARPOS (*it)));
8396 result = MOVE_LINE_CONTINUED;
8397 break;
8398 }
8399
8400 if (BUFFER_POS_REACHED_P ())
8401 {
8402 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8403 goto buffer_pos_reached;
8404 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8405 {
8406 SAVE_IT (atpos_it, *it, atpos_data);
8407 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8408 }
8409 }
8410
8411 if (new_x > it->first_visible_x)
8412 {
8413 /* Glyph is visible. Increment number of glyphs that
8414 would be displayed. */
8415 ++it->hpos;
8416 }
8417 }
8418
8419 if (result != MOVE_UNDEFINED)
8420 break;
8421 }
8422 else if (BUFFER_POS_REACHED_P ())
8423 {
8424 buffer_pos_reached:
8425 IT_RESET_X_ASCENT_DESCENT (it);
8426 result = MOVE_POS_MATCH_OR_ZV;
8427 break;
8428 }
8429 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8430 {
8431 /* Stop when TO_X specified and reached. This check is
8432 necessary here because of lines consisting of a line end,
8433 only. The line end will not produce any glyphs and we
8434 would never get MOVE_X_REACHED. */
8435 xassert (it->nglyphs == 0);
8436 result = MOVE_X_REACHED;
8437 break;
8438 }
8439
8440 /* Is this a line end? If yes, we're done. */
8441 if (ITERATOR_AT_END_OF_LINE_P (it))
8442 {
8443 /* If we are past TO_CHARPOS, but never saw any character
8444 positions smaller than TO_CHARPOS, return
8445 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8446 did. */
8447 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8448 {
8449 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8450 {
8451 if (IT_CHARPOS (ppos_it) < ZV)
8452 {
8453 RESTORE_IT (it, &ppos_it, ppos_data);
8454 result = MOVE_POS_MATCH_OR_ZV;
8455 }
8456 else
8457 goto buffer_pos_reached;
8458 }
8459 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8460 && IT_CHARPOS (*it) > to_charpos)
8461 goto buffer_pos_reached;
8462 else
8463 result = MOVE_NEWLINE_OR_CR;
8464 }
8465 else
8466 result = MOVE_NEWLINE_OR_CR;
8467 break;
8468 }
8469
8470 prev_method = it->method;
8471 if (it->method == GET_FROM_BUFFER)
8472 prev_pos = IT_CHARPOS (*it);
8473 /* The current display element has been consumed. Advance
8474 to the next. */
8475 set_iterator_to_next (it, 1);
8476 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8477 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8478 if (IT_CHARPOS (*it) < to_charpos)
8479 saw_smaller_pos = 1;
8480 if (it->bidi_p
8481 && (op & MOVE_TO_POS)
8482 && IT_CHARPOS (*it) >= to_charpos
8483 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8484 SAVE_IT (ppos_it, *it, ppos_data);
8485
8486 /* Stop if lines are truncated and IT's current x-position is
8487 past the right edge of the window now. */
8488 if (it->line_wrap == TRUNCATE
8489 && it->current_x >= it->last_visible_x)
8490 {
8491 if (!FRAME_WINDOW_P (it->f)
8492 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8493 {
8494 int at_eob_p = 0;
8495
8496 if ((at_eob_p = !get_next_display_element (it))
8497 || BUFFER_POS_REACHED_P ()
8498 /* If we are past TO_CHARPOS, but never saw any
8499 character positions smaller than TO_CHARPOS,
8500 return MOVE_POS_MATCH_OR_ZV, like the
8501 unidirectional display did. */
8502 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8503 && !saw_smaller_pos
8504 && IT_CHARPOS (*it) > to_charpos))
8505 {
8506 if (it->bidi_p
8507 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8508 RESTORE_IT (it, &ppos_it, ppos_data);
8509 result = MOVE_POS_MATCH_OR_ZV;
8510 break;
8511 }
8512 if (ITERATOR_AT_END_OF_LINE_P (it))
8513 {
8514 result = MOVE_NEWLINE_OR_CR;
8515 break;
8516 }
8517 }
8518 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8519 && !saw_smaller_pos
8520 && IT_CHARPOS (*it) > to_charpos)
8521 {
8522 if (IT_CHARPOS (ppos_it) < ZV)
8523 RESTORE_IT (it, &ppos_it, ppos_data);
8524 result = MOVE_POS_MATCH_OR_ZV;
8525 break;
8526 }
8527 result = MOVE_LINE_TRUNCATED;
8528 break;
8529 }
8530 #undef IT_RESET_X_ASCENT_DESCENT
8531 }
8532
8533 #undef BUFFER_POS_REACHED_P
8534
8535 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8536 restore the saved iterator. */
8537 if (atpos_it.sp >= 0)
8538 RESTORE_IT (it, &atpos_it, atpos_data);
8539 else if (atx_it.sp >= 0)
8540 RESTORE_IT (it, &atx_it, atx_data);
8541
8542 done:
8543
8544 if (atpos_data)
8545 bidi_unshelve_cache (atpos_data, 1);
8546 if (atx_data)
8547 bidi_unshelve_cache (atx_data, 1);
8548 if (wrap_data)
8549 bidi_unshelve_cache (wrap_data, 1);
8550 if (ppos_data)
8551 bidi_unshelve_cache (ppos_data, 1);
8552
8553 /* Restore the iterator settings altered at the beginning of this
8554 function. */
8555 it->glyph_row = saved_glyph_row;
8556 return result;
8557 }
8558
8559 /* For external use. */
8560 void
8561 move_it_in_display_line (struct it *it,
8562 ptrdiff_t to_charpos, int to_x,
8563 enum move_operation_enum op)
8564 {
8565 if (it->line_wrap == WORD_WRAP
8566 && (op & MOVE_TO_X))
8567 {
8568 struct it save_it;
8569 void *save_data = NULL;
8570 int skip;
8571
8572 SAVE_IT (save_it, *it, save_data);
8573 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8574 /* When word-wrap is on, TO_X may lie past the end
8575 of a wrapped line. Then it->current is the
8576 character on the next line, so backtrack to the
8577 space before the wrap point. */
8578 if (skip == MOVE_LINE_CONTINUED)
8579 {
8580 int prev_x = max (it->current_x - 1, 0);
8581 RESTORE_IT (it, &save_it, save_data);
8582 move_it_in_display_line_to
8583 (it, -1, prev_x, MOVE_TO_X);
8584 }
8585 else
8586 bidi_unshelve_cache (save_data, 1);
8587 }
8588 else
8589 move_it_in_display_line_to (it, to_charpos, to_x, op);
8590 }
8591
8592
8593 /* Move IT forward until it satisfies one or more of the criteria in
8594 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8595
8596 OP is a bit-mask that specifies where to stop, and in particular,
8597 which of those four position arguments makes a difference. See the
8598 description of enum move_operation_enum.
8599
8600 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8601 screen line, this function will set IT to the next position that is
8602 displayed to the right of TO_CHARPOS on the screen. */
8603
8604 void
8605 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8606 {
8607 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8608 int line_height, line_start_x = 0, reached = 0;
8609 void *backup_data = NULL;
8610
8611 for (;;)
8612 {
8613 if (op & MOVE_TO_VPOS)
8614 {
8615 /* If no TO_CHARPOS and no TO_X specified, stop at the
8616 start of the line TO_VPOS. */
8617 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8618 {
8619 if (it->vpos == to_vpos)
8620 {
8621 reached = 1;
8622 break;
8623 }
8624 else
8625 skip = move_it_in_display_line_to (it, -1, -1, 0);
8626 }
8627 else
8628 {
8629 /* TO_VPOS >= 0 means stop at TO_X in the line at
8630 TO_VPOS, or at TO_POS, whichever comes first. */
8631 if (it->vpos == to_vpos)
8632 {
8633 reached = 2;
8634 break;
8635 }
8636
8637 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8638
8639 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8640 {
8641 reached = 3;
8642 break;
8643 }
8644 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8645 {
8646 /* We have reached TO_X but not in the line we want. */
8647 skip = move_it_in_display_line_to (it, to_charpos,
8648 -1, MOVE_TO_POS);
8649 if (skip == MOVE_POS_MATCH_OR_ZV)
8650 {
8651 reached = 4;
8652 break;
8653 }
8654 }
8655 }
8656 }
8657 else if (op & MOVE_TO_Y)
8658 {
8659 struct it it_backup;
8660
8661 if (it->line_wrap == WORD_WRAP)
8662 SAVE_IT (it_backup, *it, backup_data);
8663
8664 /* TO_Y specified means stop at TO_X in the line containing
8665 TO_Y---or at TO_CHARPOS if this is reached first. The
8666 problem is that we can't really tell whether the line
8667 contains TO_Y before we have completely scanned it, and
8668 this may skip past TO_X. What we do is to first scan to
8669 TO_X.
8670
8671 If TO_X is not specified, use a TO_X of zero. The reason
8672 is to make the outcome of this function more predictable.
8673 If we didn't use TO_X == 0, we would stop at the end of
8674 the line which is probably not what a caller would expect
8675 to happen. */
8676 skip = move_it_in_display_line_to
8677 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8678 (MOVE_TO_X | (op & MOVE_TO_POS)));
8679
8680 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8681 if (skip == MOVE_POS_MATCH_OR_ZV)
8682 reached = 5;
8683 else if (skip == MOVE_X_REACHED)
8684 {
8685 /* If TO_X was reached, we want to know whether TO_Y is
8686 in the line. We know this is the case if the already
8687 scanned glyphs make the line tall enough. Otherwise,
8688 we must check by scanning the rest of the line. */
8689 line_height = it->max_ascent + it->max_descent;
8690 if (to_y >= it->current_y
8691 && to_y < it->current_y + line_height)
8692 {
8693 reached = 6;
8694 break;
8695 }
8696 SAVE_IT (it_backup, *it, backup_data);
8697 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8698 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8699 op & MOVE_TO_POS);
8700 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8701 line_height = it->max_ascent + it->max_descent;
8702 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8703
8704 if (to_y >= it->current_y
8705 && to_y < it->current_y + line_height)
8706 {
8707 /* If TO_Y is in this line and TO_X was reached
8708 above, we scanned too far. We have to restore
8709 IT's settings to the ones before skipping. But
8710 keep the more accurate values of max_ascent and
8711 max_descent we've found while skipping the rest
8712 of the line, for the sake of callers, such as
8713 pos_visible_p, that need to know the line
8714 height. */
8715 int max_ascent = it->max_ascent;
8716 int max_descent = it->max_descent;
8717
8718 RESTORE_IT (it, &it_backup, backup_data);
8719 it->max_ascent = max_ascent;
8720 it->max_descent = max_descent;
8721 reached = 6;
8722 }
8723 else
8724 {
8725 skip = skip2;
8726 if (skip == MOVE_POS_MATCH_OR_ZV)
8727 reached = 7;
8728 }
8729 }
8730 else
8731 {
8732 /* Check whether TO_Y is in this line. */
8733 line_height = it->max_ascent + it->max_descent;
8734 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8735
8736 if (to_y >= it->current_y
8737 && to_y < it->current_y + line_height)
8738 {
8739 /* When word-wrap is on, TO_X may lie past the end
8740 of a wrapped line. Then it->current is the
8741 character on the next line, so backtrack to the
8742 space before the wrap point. */
8743 if (skip == MOVE_LINE_CONTINUED
8744 && it->line_wrap == WORD_WRAP)
8745 {
8746 int prev_x = max (it->current_x - 1, 0);
8747 RESTORE_IT (it, &it_backup, backup_data);
8748 skip = move_it_in_display_line_to
8749 (it, -1, prev_x, MOVE_TO_X);
8750 }
8751 reached = 6;
8752 }
8753 }
8754
8755 if (reached)
8756 break;
8757 }
8758 else if (BUFFERP (it->object)
8759 && (it->method == GET_FROM_BUFFER
8760 || it->method == GET_FROM_STRETCH)
8761 && IT_CHARPOS (*it) >= to_charpos
8762 /* Under bidi iteration, a call to set_iterator_to_next
8763 can scan far beyond to_charpos if the initial
8764 portion of the next line needs to be reordered. In
8765 that case, give move_it_in_display_line_to another
8766 chance below. */
8767 && !(it->bidi_p
8768 && it->bidi_it.scan_dir == -1))
8769 skip = MOVE_POS_MATCH_OR_ZV;
8770 else
8771 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8772
8773 switch (skip)
8774 {
8775 case MOVE_POS_MATCH_OR_ZV:
8776 reached = 8;
8777 goto out;
8778
8779 case MOVE_NEWLINE_OR_CR:
8780 set_iterator_to_next (it, 1);
8781 it->continuation_lines_width = 0;
8782 break;
8783
8784 case MOVE_LINE_TRUNCATED:
8785 it->continuation_lines_width = 0;
8786 reseat_at_next_visible_line_start (it, 0);
8787 if ((op & MOVE_TO_POS) != 0
8788 && IT_CHARPOS (*it) > to_charpos)
8789 {
8790 reached = 9;
8791 goto out;
8792 }
8793 break;
8794
8795 case MOVE_LINE_CONTINUED:
8796 /* For continued lines ending in a tab, some of the glyphs
8797 associated with the tab are displayed on the current
8798 line. Since it->current_x does not include these glyphs,
8799 we use it->last_visible_x instead. */
8800 if (it->c == '\t')
8801 {
8802 it->continuation_lines_width += it->last_visible_x;
8803 /* When moving by vpos, ensure that the iterator really
8804 advances to the next line (bug#847, bug#969). Fixme:
8805 do we need to do this in other circumstances? */
8806 if (it->current_x != it->last_visible_x
8807 && (op & MOVE_TO_VPOS)
8808 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8809 {
8810 line_start_x = it->current_x + it->pixel_width
8811 - it->last_visible_x;
8812 set_iterator_to_next (it, 0);
8813 }
8814 }
8815 else
8816 it->continuation_lines_width += it->current_x;
8817 break;
8818
8819 default:
8820 abort ();
8821 }
8822
8823 /* Reset/increment for the next run. */
8824 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8825 it->current_x = line_start_x;
8826 line_start_x = 0;
8827 it->hpos = 0;
8828 it->current_y += it->max_ascent + it->max_descent;
8829 ++it->vpos;
8830 last_height = it->max_ascent + it->max_descent;
8831 last_max_ascent = it->max_ascent;
8832 it->max_ascent = it->max_descent = 0;
8833 }
8834
8835 out:
8836
8837 /* On text terminals, we may stop at the end of a line in the middle
8838 of a multi-character glyph. If the glyph itself is continued,
8839 i.e. it is actually displayed on the next line, don't treat this
8840 stopping point as valid; move to the next line instead (unless
8841 that brings us offscreen). */
8842 if (!FRAME_WINDOW_P (it->f)
8843 && op & MOVE_TO_POS
8844 && IT_CHARPOS (*it) == to_charpos
8845 && it->what == IT_CHARACTER
8846 && it->nglyphs > 1
8847 && it->line_wrap == WINDOW_WRAP
8848 && it->current_x == it->last_visible_x - 1
8849 && it->c != '\n'
8850 && it->c != '\t'
8851 && it->vpos < XFASTINT (it->w->window_end_vpos))
8852 {
8853 it->continuation_lines_width += it->current_x;
8854 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8855 it->current_y += it->max_ascent + it->max_descent;
8856 ++it->vpos;
8857 last_height = it->max_ascent + it->max_descent;
8858 last_max_ascent = it->max_ascent;
8859 }
8860
8861 if (backup_data)
8862 bidi_unshelve_cache (backup_data, 1);
8863
8864 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8865 }
8866
8867
8868 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8869
8870 If DY > 0, move IT backward at least that many pixels. DY = 0
8871 means move IT backward to the preceding line start or BEGV. This
8872 function may move over more than DY pixels if IT->current_y - DY
8873 ends up in the middle of a line; in this case IT->current_y will be
8874 set to the top of the line moved to. */
8875
8876 void
8877 move_it_vertically_backward (struct it *it, int dy)
8878 {
8879 int nlines, h;
8880 struct it it2, it3;
8881 void *it2data = NULL, *it3data = NULL;
8882 ptrdiff_t start_pos;
8883
8884 move_further_back:
8885 xassert (dy >= 0);
8886
8887 start_pos = IT_CHARPOS (*it);
8888
8889 /* Estimate how many newlines we must move back. */
8890 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
8891
8892 /* Set the iterator's position that many lines back. */
8893 while (nlines-- && IT_CHARPOS (*it) > BEGV)
8894 back_to_previous_visible_line_start (it);
8895
8896 /* Reseat the iterator here. When moving backward, we don't want
8897 reseat to skip forward over invisible text, set up the iterator
8898 to deliver from overlay strings at the new position etc. So,
8899 use reseat_1 here. */
8900 reseat_1 (it, it->current.pos, 1);
8901
8902 /* We are now surely at a line start. */
8903 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
8904 reordering is in effect. */
8905 it->continuation_lines_width = 0;
8906
8907 /* Move forward and see what y-distance we moved. First move to the
8908 start of the next line so that we get its height. We need this
8909 height to be able to tell whether we reached the specified
8910 y-distance. */
8911 SAVE_IT (it2, *it, it2data);
8912 it2.max_ascent = it2.max_descent = 0;
8913 do
8914 {
8915 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
8916 MOVE_TO_POS | MOVE_TO_VPOS);
8917 }
8918 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
8919 /* If we are in a display string which starts at START_POS,
8920 and that display string includes a newline, and we are
8921 right after that newline (i.e. at the beginning of a
8922 display line), exit the loop, because otherwise we will
8923 infloop, since move_it_to will see that it is already at
8924 START_POS and will not move. */
8925 || (it2.method == GET_FROM_STRING
8926 && IT_CHARPOS (it2) == start_pos
8927 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
8928 xassert (IT_CHARPOS (*it) >= BEGV);
8929 SAVE_IT (it3, it2, it3data);
8930
8931 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
8932 xassert (IT_CHARPOS (*it) >= BEGV);
8933 /* H is the actual vertical distance from the position in *IT
8934 and the starting position. */
8935 h = it2.current_y - it->current_y;
8936 /* NLINES is the distance in number of lines. */
8937 nlines = it2.vpos - it->vpos;
8938
8939 /* Correct IT's y and vpos position
8940 so that they are relative to the starting point. */
8941 it->vpos -= nlines;
8942 it->current_y -= h;
8943
8944 if (dy == 0)
8945 {
8946 /* DY == 0 means move to the start of the screen line. The
8947 value of nlines is > 0 if continuation lines were involved,
8948 or if the original IT position was at start of a line. */
8949 RESTORE_IT (it, it, it2data);
8950 if (nlines > 0)
8951 move_it_by_lines (it, nlines);
8952 /* The above code moves us to some position NLINES down,
8953 usually to its first glyph (leftmost in an L2R line), but
8954 that's not necessarily the start of the line, under bidi
8955 reordering. We want to get to the character position
8956 that is immediately after the newline of the previous
8957 line. */
8958 if (it->bidi_p
8959 && !it->continuation_lines_width
8960 && !STRINGP (it->string)
8961 && IT_CHARPOS (*it) > BEGV
8962 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
8963 {
8964 ptrdiff_t nl_pos =
8965 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
8966
8967 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
8968 }
8969 bidi_unshelve_cache (it3data, 1);
8970 }
8971 else
8972 {
8973 /* The y-position we try to reach, relative to *IT.
8974 Note that H has been subtracted in front of the if-statement. */
8975 int target_y = it->current_y + h - dy;
8976 int y0 = it3.current_y;
8977 int y1;
8978 int line_height;
8979
8980 RESTORE_IT (&it3, &it3, it3data);
8981 y1 = line_bottom_y (&it3);
8982 line_height = y1 - y0;
8983 RESTORE_IT (it, it, it2data);
8984 /* If we did not reach target_y, try to move further backward if
8985 we can. If we moved too far backward, try to move forward. */
8986 if (target_y < it->current_y
8987 /* This is heuristic. In a window that's 3 lines high, with
8988 a line height of 13 pixels each, recentering with point
8989 on the bottom line will try to move -39/2 = 19 pixels
8990 backward. Try to avoid moving into the first line. */
8991 && (it->current_y - target_y
8992 > min (window_box_height (it->w), line_height * 2 / 3))
8993 && IT_CHARPOS (*it) > BEGV)
8994 {
8995 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
8996 target_y - it->current_y));
8997 dy = it->current_y - target_y;
8998 goto move_further_back;
8999 }
9000 else if (target_y >= it->current_y + line_height
9001 && IT_CHARPOS (*it) < ZV)
9002 {
9003 /* Should move forward by at least one line, maybe more.
9004
9005 Note: Calling move_it_by_lines can be expensive on
9006 terminal frames, where compute_motion is used (via
9007 vmotion) to do the job, when there are very long lines
9008 and truncate-lines is nil. That's the reason for
9009 treating terminal frames specially here. */
9010
9011 if (!FRAME_WINDOW_P (it->f))
9012 move_it_vertically (it, target_y - (it->current_y + line_height));
9013 else
9014 {
9015 do
9016 {
9017 move_it_by_lines (it, 1);
9018 }
9019 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9020 }
9021 }
9022 }
9023 }
9024
9025
9026 /* Move IT by a specified amount of pixel lines DY. DY negative means
9027 move backwards. DY = 0 means move to start of screen line. At the
9028 end, IT will be on the start of a screen line. */
9029
9030 void
9031 move_it_vertically (struct it *it, int dy)
9032 {
9033 if (dy <= 0)
9034 move_it_vertically_backward (it, -dy);
9035 else
9036 {
9037 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9038 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9039 MOVE_TO_POS | MOVE_TO_Y);
9040 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9041
9042 /* If buffer ends in ZV without a newline, move to the start of
9043 the line to satisfy the post-condition. */
9044 if (IT_CHARPOS (*it) == ZV
9045 && ZV > BEGV
9046 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9047 move_it_by_lines (it, 0);
9048 }
9049 }
9050
9051
9052 /* Move iterator IT past the end of the text line it is in. */
9053
9054 void
9055 move_it_past_eol (struct it *it)
9056 {
9057 enum move_it_result rc;
9058
9059 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9060 if (rc == MOVE_NEWLINE_OR_CR)
9061 set_iterator_to_next (it, 0);
9062 }
9063
9064
9065 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9066 negative means move up. DVPOS == 0 means move to the start of the
9067 screen line.
9068
9069 Optimization idea: If we would know that IT->f doesn't use
9070 a face with proportional font, we could be faster for
9071 truncate-lines nil. */
9072
9073 void
9074 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9075 {
9076
9077 /* The commented-out optimization uses vmotion on terminals. This
9078 gives bad results, because elements like it->what, on which
9079 callers such as pos_visible_p rely, aren't updated. */
9080 /* struct position pos;
9081 if (!FRAME_WINDOW_P (it->f))
9082 {
9083 struct text_pos textpos;
9084
9085 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9086 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9087 reseat (it, textpos, 1);
9088 it->vpos += pos.vpos;
9089 it->current_y += pos.vpos;
9090 }
9091 else */
9092
9093 if (dvpos == 0)
9094 {
9095 /* DVPOS == 0 means move to the start of the screen line. */
9096 move_it_vertically_backward (it, 0);
9097 /* Let next call to line_bottom_y calculate real line height */
9098 last_height = 0;
9099 }
9100 else if (dvpos > 0)
9101 {
9102 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9103 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9104 {
9105 /* Only move to the next buffer position if we ended up in a
9106 string from display property, not in an overlay string
9107 (before-string or after-string). That is because the
9108 latter don't conceal the underlying buffer position, so
9109 we can ask to move the iterator to the exact position we
9110 are interested in. Note that, even if we are already at
9111 IT_CHARPOS (*it), the call below is not a no-op, as it
9112 will detect that we are at the end of the string, pop the
9113 iterator, and compute it->current_x and it->hpos
9114 correctly. */
9115 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9116 -1, -1, -1, MOVE_TO_POS);
9117 }
9118 }
9119 else
9120 {
9121 struct it it2;
9122 void *it2data = NULL;
9123 ptrdiff_t start_charpos, i;
9124
9125 /* Start at the beginning of the screen line containing IT's
9126 position. This may actually move vertically backwards,
9127 in case of overlays, so adjust dvpos accordingly. */
9128 dvpos += it->vpos;
9129 move_it_vertically_backward (it, 0);
9130 dvpos -= it->vpos;
9131
9132 /* Go back -DVPOS visible lines and reseat the iterator there. */
9133 start_charpos = IT_CHARPOS (*it);
9134 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9135 back_to_previous_visible_line_start (it);
9136 reseat (it, it->current.pos, 1);
9137
9138 /* Move further back if we end up in a string or an image. */
9139 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9140 {
9141 /* First try to move to start of display line. */
9142 dvpos += it->vpos;
9143 move_it_vertically_backward (it, 0);
9144 dvpos -= it->vpos;
9145 if (IT_POS_VALID_AFTER_MOVE_P (it))
9146 break;
9147 /* If start of line is still in string or image,
9148 move further back. */
9149 back_to_previous_visible_line_start (it);
9150 reseat (it, it->current.pos, 1);
9151 dvpos--;
9152 }
9153
9154 it->current_x = it->hpos = 0;
9155
9156 /* Above call may have moved too far if continuation lines
9157 are involved. Scan forward and see if it did. */
9158 SAVE_IT (it2, *it, it2data);
9159 it2.vpos = it2.current_y = 0;
9160 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9161 it->vpos -= it2.vpos;
9162 it->current_y -= it2.current_y;
9163 it->current_x = it->hpos = 0;
9164
9165 /* If we moved too far back, move IT some lines forward. */
9166 if (it2.vpos > -dvpos)
9167 {
9168 int delta = it2.vpos + dvpos;
9169
9170 RESTORE_IT (&it2, &it2, it2data);
9171 SAVE_IT (it2, *it, it2data);
9172 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9173 /* Move back again if we got too far ahead. */
9174 if (IT_CHARPOS (*it) >= start_charpos)
9175 RESTORE_IT (it, &it2, it2data);
9176 else
9177 bidi_unshelve_cache (it2data, 1);
9178 }
9179 else
9180 RESTORE_IT (it, it, it2data);
9181 }
9182 }
9183
9184 /* Return 1 if IT points into the middle of a display vector. */
9185
9186 int
9187 in_display_vector_p (struct it *it)
9188 {
9189 return (it->method == GET_FROM_DISPLAY_VECTOR
9190 && it->current.dpvec_index > 0
9191 && it->dpvec + it->current.dpvec_index != it->dpend);
9192 }
9193
9194 \f
9195 /***********************************************************************
9196 Messages
9197 ***********************************************************************/
9198
9199
9200 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9201 to *Messages*. */
9202
9203 void
9204 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9205 {
9206 Lisp_Object args[3];
9207 Lisp_Object msg, fmt;
9208 char *buffer;
9209 ptrdiff_t len;
9210 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9211 USE_SAFE_ALLOCA;
9212
9213 /* Do nothing if called asynchronously. Inserting text into
9214 a buffer may call after-change-functions and alike and
9215 that would means running Lisp asynchronously. */
9216 if (handling_signal)
9217 return;
9218
9219 fmt = msg = Qnil;
9220 GCPRO4 (fmt, msg, arg1, arg2);
9221
9222 args[0] = fmt = build_string (format);
9223 args[1] = arg1;
9224 args[2] = arg2;
9225 msg = Fformat (3, args);
9226
9227 len = SBYTES (msg) + 1;
9228 SAFE_ALLOCA (buffer, char *, len);
9229 memcpy (buffer, SDATA (msg), len);
9230
9231 message_dolog (buffer, len - 1, 1, 0);
9232 SAFE_FREE ();
9233
9234 UNGCPRO;
9235 }
9236
9237
9238 /* Output a newline in the *Messages* buffer if "needs" one. */
9239
9240 void
9241 message_log_maybe_newline (void)
9242 {
9243 if (message_log_need_newline)
9244 message_dolog ("", 0, 1, 0);
9245 }
9246
9247
9248 /* Add a string M of length NBYTES to the message log, optionally
9249 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9250 nonzero, means interpret the contents of M as multibyte. This
9251 function calls low-level routines in order to bypass text property
9252 hooks, etc. which might not be safe to run.
9253
9254 This may GC (insert may run before/after change hooks),
9255 so the buffer M must NOT point to a Lisp string. */
9256
9257 void
9258 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9259 {
9260 const unsigned char *msg = (const unsigned char *) m;
9261
9262 if (!NILP (Vmemory_full))
9263 return;
9264
9265 if (!NILP (Vmessage_log_max))
9266 {
9267 struct buffer *oldbuf;
9268 Lisp_Object oldpoint, oldbegv, oldzv;
9269 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9270 ptrdiff_t point_at_end = 0;
9271 ptrdiff_t zv_at_end = 0;
9272 Lisp_Object old_deactivate_mark, tem;
9273 struct gcpro gcpro1;
9274
9275 old_deactivate_mark = Vdeactivate_mark;
9276 oldbuf = current_buffer;
9277 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9278 BVAR (current_buffer, undo_list) = Qt;
9279
9280 oldpoint = message_dolog_marker1;
9281 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9282 oldbegv = message_dolog_marker2;
9283 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9284 oldzv = message_dolog_marker3;
9285 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9286 GCPRO1 (old_deactivate_mark);
9287
9288 if (PT == Z)
9289 point_at_end = 1;
9290 if (ZV == Z)
9291 zv_at_end = 1;
9292
9293 BEGV = BEG;
9294 BEGV_BYTE = BEG_BYTE;
9295 ZV = Z;
9296 ZV_BYTE = Z_BYTE;
9297 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9298
9299 /* Insert the string--maybe converting multibyte to single byte
9300 or vice versa, so that all the text fits the buffer. */
9301 if (multibyte
9302 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9303 {
9304 ptrdiff_t i;
9305 int c, char_bytes;
9306 char work[1];
9307
9308 /* Convert a multibyte string to single-byte
9309 for the *Message* buffer. */
9310 for (i = 0; i < nbytes; i += char_bytes)
9311 {
9312 c = string_char_and_length (msg + i, &char_bytes);
9313 work[0] = (ASCII_CHAR_P (c)
9314 ? c
9315 : multibyte_char_to_unibyte (c));
9316 insert_1_both (work, 1, 1, 1, 0, 0);
9317 }
9318 }
9319 else if (! multibyte
9320 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9321 {
9322 ptrdiff_t i;
9323 int c, char_bytes;
9324 unsigned char str[MAX_MULTIBYTE_LENGTH];
9325 /* Convert a single-byte string to multibyte
9326 for the *Message* buffer. */
9327 for (i = 0; i < nbytes; i++)
9328 {
9329 c = msg[i];
9330 MAKE_CHAR_MULTIBYTE (c);
9331 char_bytes = CHAR_STRING (c, str);
9332 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9333 }
9334 }
9335 else if (nbytes)
9336 insert_1 (m, nbytes, 1, 0, 0);
9337
9338 if (nlflag)
9339 {
9340 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9341 printmax_t dups;
9342 insert_1 ("\n", 1, 1, 0, 0);
9343
9344 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9345 this_bol = PT;
9346 this_bol_byte = PT_BYTE;
9347
9348 /* See if this line duplicates the previous one.
9349 If so, combine duplicates. */
9350 if (this_bol > BEG)
9351 {
9352 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9353 prev_bol = PT;
9354 prev_bol_byte = PT_BYTE;
9355
9356 dups = message_log_check_duplicate (prev_bol_byte,
9357 this_bol_byte);
9358 if (dups)
9359 {
9360 del_range_both (prev_bol, prev_bol_byte,
9361 this_bol, this_bol_byte, 0);
9362 if (dups > 1)
9363 {
9364 char dupstr[sizeof " [ times]"
9365 + INT_STRLEN_BOUND (printmax_t)];
9366 int duplen;
9367
9368 /* If you change this format, don't forget to also
9369 change message_log_check_duplicate. */
9370 sprintf (dupstr, " [%"pMd" times]", dups);
9371 duplen = strlen (dupstr);
9372 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9373 insert_1 (dupstr, duplen, 1, 0, 1);
9374 }
9375 }
9376 }
9377
9378 /* If we have more than the desired maximum number of lines
9379 in the *Messages* buffer now, delete the oldest ones.
9380 This is safe because we don't have undo in this buffer. */
9381
9382 if (NATNUMP (Vmessage_log_max))
9383 {
9384 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9385 -XFASTINT (Vmessage_log_max) - 1, 0);
9386 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9387 }
9388 }
9389 BEGV = XMARKER (oldbegv)->charpos;
9390 BEGV_BYTE = marker_byte_position (oldbegv);
9391
9392 if (zv_at_end)
9393 {
9394 ZV = Z;
9395 ZV_BYTE = Z_BYTE;
9396 }
9397 else
9398 {
9399 ZV = XMARKER (oldzv)->charpos;
9400 ZV_BYTE = marker_byte_position (oldzv);
9401 }
9402
9403 if (point_at_end)
9404 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9405 else
9406 /* We can't do Fgoto_char (oldpoint) because it will run some
9407 Lisp code. */
9408 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9409 XMARKER (oldpoint)->bytepos);
9410
9411 UNGCPRO;
9412 unchain_marker (XMARKER (oldpoint));
9413 unchain_marker (XMARKER (oldbegv));
9414 unchain_marker (XMARKER (oldzv));
9415
9416 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9417 set_buffer_internal (oldbuf);
9418 if (NILP (tem))
9419 windows_or_buffers_changed = old_windows_or_buffers_changed;
9420 message_log_need_newline = !nlflag;
9421 Vdeactivate_mark = old_deactivate_mark;
9422 }
9423 }
9424
9425
9426 /* We are at the end of the buffer after just having inserted a newline.
9427 (Note: We depend on the fact we won't be crossing the gap.)
9428 Check to see if the most recent message looks a lot like the previous one.
9429 Return 0 if different, 1 if the new one should just replace it, or a
9430 value N > 1 if we should also append " [N times]". */
9431
9432 static intmax_t
9433 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9434 {
9435 ptrdiff_t i;
9436 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9437 int seen_dots = 0;
9438 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9439 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9440
9441 for (i = 0; i < len; i++)
9442 {
9443 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9444 seen_dots = 1;
9445 if (p1[i] != p2[i])
9446 return seen_dots;
9447 }
9448 p1 += len;
9449 if (*p1 == '\n')
9450 return 2;
9451 if (*p1++ == ' ' && *p1++ == '[')
9452 {
9453 char *pend;
9454 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9455 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9456 return n+1;
9457 }
9458 return 0;
9459 }
9460 \f
9461
9462 /* Display an echo area message M with a specified length of NBYTES
9463 bytes. The string may include null characters. If M is 0, clear
9464 out any existing message, and let the mini-buffer text show
9465 through.
9466
9467 This may GC, so the buffer M must NOT point to a Lisp string. */
9468
9469 void
9470 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9471 {
9472 /* First flush out any partial line written with print. */
9473 message_log_maybe_newline ();
9474 if (m)
9475 message_dolog (m, nbytes, 1, multibyte);
9476 message2_nolog (m, nbytes, multibyte);
9477 }
9478
9479
9480 /* The non-logging counterpart of message2. */
9481
9482 void
9483 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9484 {
9485 struct frame *sf = SELECTED_FRAME ();
9486 message_enable_multibyte = multibyte;
9487
9488 if (FRAME_INITIAL_P (sf))
9489 {
9490 if (noninteractive_need_newline)
9491 putc ('\n', stderr);
9492 noninteractive_need_newline = 0;
9493 if (m)
9494 fwrite (m, nbytes, 1, stderr);
9495 if (cursor_in_echo_area == 0)
9496 fprintf (stderr, "\n");
9497 fflush (stderr);
9498 }
9499 /* A null message buffer means that the frame hasn't really been
9500 initialized yet. Error messages get reported properly by
9501 cmd_error, so this must be just an informative message; toss it. */
9502 else if (INTERACTIVE
9503 && sf->glyphs_initialized_p
9504 && FRAME_MESSAGE_BUF (sf))
9505 {
9506 Lisp_Object mini_window;
9507 struct frame *f;
9508
9509 /* Get the frame containing the mini-buffer
9510 that the selected frame is using. */
9511 mini_window = FRAME_MINIBUF_WINDOW (sf);
9512 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9513
9514 FRAME_SAMPLE_VISIBILITY (f);
9515 if (FRAME_VISIBLE_P (sf)
9516 && ! FRAME_VISIBLE_P (f))
9517 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9518
9519 if (m)
9520 {
9521 set_message (m, Qnil, nbytes, multibyte);
9522 if (minibuffer_auto_raise)
9523 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9524 }
9525 else
9526 clear_message (1, 1);
9527
9528 do_pending_window_change (0);
9529 echo_area_display (1);
9530 do_pending_window_change (0);
9531 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9532 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9533 }
9534 }
9535
9536
9537 /* Display an echo area message M with a specified length of NBYTES
9538 bytes. The string may include null characters. If M is not a
9539 string, clear out any existing message, and let the mini-buffer
9540 text show through.
9541
9542 This function cancels echoing. */
9543
9544 void
9545 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9546 {
9547 struct gcpro gcpro1;
9548
9549 GCPRO1 (m);
9550 clear_message (1,1);
9551 cancel_echoing ();
9552
9553 /* First flush out any partial line written with print. */
9554 message_log_maybe_newline ();
9555 if (STRINGP (m))
9556 {
9557 char *buffer;
9558 USE_SAFE_ALLOCA;
9559
9560 SAFE_ALLOCA (buffer, char *, nbytes);
9561 memcpy (buffer, SDATA (m), nbytes);
9562 message_dolog (buffer, nbytes, 1, multibyte);
9563 SAFE_FREE ();
9564 }
9565 message3_nolog (m, nbytes, multibyte);
9566
9567 UNGCPRO;
9568 }
9569
9570
9571 /* The non-logging version of message3.
9572 This does not cancel echoing, because it is used for echoing.
9573 Perhaps we need to make a separate function for echoing
9574 and make this cancel echoing. */
9575
9576 void
9577 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9578 {
9579 struct frame *sf = SELECTED_FRAME ();
9580 message_enable_multibyte = multibyte;
9581
9582 if (FRAME_INITIAL_P (sf))
9583 {
9584 if (noninteractive_need_newline)
9585 putc ('\n', stderr);
9586 noninteractive_need_newline = 0;
9587 if (STRINGP (m))
9588 fwrite (SDATA (m), nbytes, 1, stderr);
9589 if (cursor_in_echo_area == 0)
9590 fprintf (stderr, "\n");
9591 fflush (stderr);
9592 }
9593 /* A null message buffer means that the frame hasn't really been
9594 initialized yet. Error messages get reported properly by
9595 cmd_error, so this must be just an informative message; toss it. */
9596 else if (INTERACTIVE
9597 && sf->glyphs_initialized_p
9598 && FRAME_MESSAGE_BUF (sf))
9599 {
9600 Lisp_Object mini_window;
9601 Lisp_Object frame;
9602 struct frame *f;
9603
9604 /* Get the frame containing the mini-buffer
9605 that the selected frame is using. */
9606 mini_window = FRAME_MINIBUF_WINDOW (sf);
9607 frame = XWINDOW (mini_window)->frame;
9608 f = XFRAME (frame);
9609
9610 FRAME_SAMPLE_VISIBILITY (f);
9611 if (FRAME_VISIBLE_P (sf)
9612 && !FRAME_VISIBLE_P (f))
9613 Fmake_frame_visible (frame);
9614
9615 if (STRINGP (m) && SCHARS (m) > 0)
9616 {
9617 set_message (NULL, m, nbytes, multibyte);
9618 if (minibuffer_auto_raise)
9619 Fraise_frame (frame);
9620 /* Assume we are not echoing.
9621 (If we are, echo_now will override this.) */
9622 echo_message_buffer = Qnil;
9623 }
9624 else
9625 clear_message (1, 1);
9626
9627 do_pending_window_change (0);
9628 echo_area_display (1);
9629 do_pending_window_change (0);
9630 if (FRAME_TERMINAL (f)->frame_up_to_date_hook != 0 && ! gc_in_progress)
9631 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9632 }
9633 }
9634
9635
9636 /* Display a null-terminated echo area message M. If M is 0, clear
9637 out any existing message, and let the mini-buffer text show through.
9638
9639 The buffer M must continue to exist until after the echo area gets
9640 cleared or some other message gets displayed there. Do not pass
9641 text that is stored in a Lisp string. Do not pass text in a buffer
9642 that was alloca'd. */
9643
9644 void
9645 message1 (const char *m)
9646 {
9647 message2 (m, (m ? strlen (m) : 0), 0);
9648 }
9649
9650
9651 /* The non-logging counterpart of message1. */
9652
9653 void
9654 message1_nolog (const char *m)
9655 {
9656 message2_nolog (m, (m ? strlen (m) : 0), 0);
9657 }
9658
9659 /* Display a message M which contains a single %s
9660 which gets replaced with STRING. */
9661
9662 void
9663 message_with_string (const char *m, Lisp_Object string, int log)
9664 {
9665 CHECK_STRING (string);
9666
9667 if (noninteractive)
9668 {
9669 if (m)
9670 {
9671 if (noninteractive_need_newline)
9672 putc ('\n', stderr);
9673 noninteractive_need_newline = 0;
9674 fprintf (stderr, m, SDATA (string));
9675 if (!cursor_in_echo_area)
9676 fprintf (stderr, "\n");
9677 fflush (stderr);
9678 }
9679 }
9680 else if (INTERACTIVE)
9681 {
9682 /* The frame whose minibuffer we're going to display the message on.
9683 It may be larger than the selected frame, so we need
9684 to use its buffer, not the selected frame's buffer. */
9685 Lisp_Object mini_window;
9686 struct frame *f, *sf = SELECTED_FRAME ();
9687
9688 /* Get the frame containing the minibuffer
9689 that the selected frame is using. */
9690 mini_window = FRAME_MINIBUF_WINDOW (sf);
9691 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9692
9693 /* A null message buffer means that the frame hasn't really been
9694 initialized yet. Error messages get reported properly by
9695 cmd_error, so this must be just an informative message; toss it. */
9696 if (FRAME_MESSAGE_BUF (f))
9697 {
9698 Lisp_Object args[2], msg;
9699 struct gcpro gcpro1, gcpro2;
9700
9701 args[0] = build_string (m);
9702 args[1] = msg = string;
9703 GCPRO2 (args[0], msg);
9704 gcpro1.nvars = 2;
9705
9706 msg = Fformat (2, args);
9707
9708 if (log)
9709 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9710 else
9711 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9712
9713 UNGCPRO;
9714
9715 /* Print should start at the beginning of the message
9716 buffer next time. */
9717 message_buf_print = 0;
9718 }
9719 }
9720 }
9721
9722
9723 /* Dump an informative message to the minibuf. If M is 0, clear out
9724 any existing message, and let the mini-buffer text show through. */
9725
9726 static void
9727 vmessage (const char *m, va_list ap)
9728 {
9729 if (noninteractive)
9730 {
9731 if (m)
9732 {
9733 if (noninteractive_need_newline)
9734 putc ('\n', stderr);
9735 noninteractive_need_newline = 0;
9736 vfprintf (stderr, m, ap);
9737 if (cursor_in_echo_area == 0)
9738 fprintf (stderr, "\n");
9739 fflush (stderr);
9740 }
9741 }
9742 else if (INTERACTIVE)
9743 {
9744 /* The frame whose mini-buffer we're going to display the message
9745 on. It may be larger than the selected frame, so we need to
9746 use its buffer, not the selected frame's buffer. */
9747 Lisp_Object mini_window;
9748 struct frame *f, *sf = SELECTED_FRAME ();
9749
9750 /* Get the frame containing the mini-buffer
9751 that the selected frame is using. */
9752 mini_window = FRAME_MINIBUF_WINDOW (sf);
9753 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9754
9755 /* A null message buffer means that the frame hasn't really been
9756 initialized yet. Error messages get reported properly by
9757 cmd_error, so this must be just an informative message; toss
9758 it. */
9759 if (FRAME_MESSAGE_BUF (f))
9760 {
9761 if (m)
9762 {
9763 ptrdiff_t len;
9764
9765 len = doprnt (FRAME_MESSAGE_BUF (f),
9766 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9767
9768 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9769 }
9770 else
9771 message1 (0);
9772
9773 /* Print should start at the beginning of the message
9774 buffer next time. */
9775 message_buf_print = 0;
9776 }
9777 }
9778 }
9779
9780 void
9781 message (const char *m, ...)
9782 {
9783 va_list ap;
9784 va_start (ap, m);
9785 vmessage (m, ap);
9786 va_end (ap);
9787 }
9788
9789
9790 #if 0
9791 /* The non-logging version of message. */
9792
9793 void
9794 message_nolog (const char *m, ...)
9795 {
9796 Lisp_Object old_log_max;
9797 va_list ap;
9798 va_start (ap, m);
9799 old_log_max = Vmessage_log_max;
9800 Vmessage_log_max = Qnil;
9801 vmessage (m, ap);
9802 Vmessage_log_max = old_log_max;
9803 va_end (ap);
9804 }
9805 #endif
9806
9807
9808 /* Display the current message in the current mini-buffer. This is
9809 only called from error handlers in process.c, and is not time
9810 critical. */
9811
9812 void
9813 update_echo_area (void)
9814 {
9815 if (!NILP (echo_area_buffer[0]))
9816 {
9817 Lisp_Object string;
9818 string = Fcurrent_message ();
9819 message3 (string, SBYTES (string),
9820 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9821 }
9822 }
9823
9824
9825 /* Make sure echo area buffers in `echo_buffers' are live.
9826 If they aren't, make new ones. */
9827
9828 static void
9829 ensure_echo_area_buffers (void)
9830 {
9831 int i;
9832
9833 for (i = 0; i < 2; ++i)
9834 if (!BUFFERP (echo_buffer[i])
9835 || NILP (BVAR (XBUFFER (echo_buffer[i]), name)))
9836 {
9837 char name[30];
9838 Lisp_Object old_buffer;
9839 int j;
9840
9841 old_buffer = echo_buffer[i];
9842 sprintf (name, " *Echo Area %d*", i);
9843 echo_buffer[i] = Fget_buffer_create (build_string (name));
9844 BVAR (XBUFFER (echo_buffer[i]), truncate_lines) = Qnil;
9845 /* to force word wrap in echo area -
9846 it was decided to postpone this*/
9847 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9848
9849 for (j = 0; j < 2; ++j)
9850 if (EQ (old_buffer, echo_area_buffer[j]))
9851 echo_area_buffer[j] = echo_buffer[i];
9852 }
9853 }
9854
9855
9856 /* Call FN with args A1..A4 with either the current or last displayed
9857 echo_area_buffer as current buffer.
9858
9859 WHICH zero means use the current message buffer
9860 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9861 from echo_buffer[] and clear it.
9862
9863 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9864 suitable buffer from echo_buffer[] and clear it.
9865
9866 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9867 that the current message becomes the last displayed one, make
9868 choose a suitable buffer for echo_area_buffer[0], and clear it.
9869
9870 Value is what FN returns. */
9871
9872 static int
9873 with_echo_area_buffer (struct window *w, int which,
9874 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
9875 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
9876 {
9877 Lisp_Object buffer;
9878 int this_one, the_other, clear_buffer_p, rc;
9879 ptrdiff_t count = SPECPDL_INDEX ();
9880
9881 /* If buffers aren't live, make new ones. */
9882 ensure_echo_area_buffers ();
9883
9884 clear_buffer_p = 0;
9885
9886 if (which == 0)
9887 this_one = 0, the_other = 1;
9888 else if (which > 0)
9889 this_one = 1, the_other = 0;
9890 else
9891 {
9892 this_one = 0, the_other = 1;
9893 clear_buffer_p = 1;
9894
9895 /* We need a fresh one in case the current echo buffer equals
9896 the one containing the last displayed echo area message. */
9897 if (!NILP (echo_area_buffer[this_one])
9898 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9899 echo_area_buffer[this_one] = Qnil;
9900 }
9901
9902 /* Choose a suitable buffer from echo_buffer[] is we don't
9903 have one. */
9904 if (NILP (echo_area_buffer[this_one]))
9905 {
9906 echo_area_buffer[this_one]
9907 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9908 ? echo_buffer[the_other]
9909 : echo_buffer[this_one]);
9910 clear_buffer_p = 1;
9911 }
9912
9913 buffer = echo_area_buffer[this_one];
9914
9915 /* Don't get confused by reusing the buffer used for echoing
9916 for a different purpose. */
9917 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9918 cancel_echoing ();
9919
9920 record_unwind_protect (unwind_with_echo_area_buffer,
9921 with_echo_area_buffer_unwind_data (w));
9922
9923 /* Make the echo area buffer current. Note that for display
9924 purposes, it is not necessary that the displayed window's buffer
9925 == current_buffer, except for text property lookup. So, let's
9926 only set that buffer temporarily here without doing a full
9927 Fset_window_buffer. We must also change w->pointm, though,
9928 because otherwise an assertions in unshow_buffer fails, and Emacs
9929 aborts. */
9930 set_buffer_internal_1 (XBUFFER (buffer));
9931 if (w)
9932 {
9933 w->buffer = buffer;
9934 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9935 }
9936
9937 BVAR (current_buffer, undo_list) = Qt;
9938 BVAR (current_buffer, read_only) = Qnil;
9939 specbind (Qinhibit_read_only, Qt);
9940 specbind (Qinhibit_modification_hooks, Qt);
9941
9942 if (clear_buffer_p && Z > BEG)
9943 del_range (BEG, Z);
9944
9945 xassert (BEGV >= BEG);
9946 xassert (ZV <= Z && ZV >= BEGV);
9947
9948 rc = fn (a1, a2, a3, a4);
9949
9950 xassert (BEGV >= BEG);
9951 xassert (ZV <= Z && ZV >= BEGV);
9952
9953 unbind_to (count, Qnil);
9954 return rc;
9955 }
9956
9957
9958 /* Save state that should be preserved around the call to the function
9959 FN called in with_echo_area_buffer. */
9960
9961 static Lisp_Object
9962 with_echo_area_buffer_unwind_data (struct window *w)
9963 {
9964 int i = 0;
9965 Lisp_Object vector, tmp;
9966
9967 /* Reduce consing by keeping one vector in
9968 Vwith_echo_area_save_vector. */
9969 vector = Vwith_echo_area_save_vector;
9970 Vwith_echo_area_save_vector = Qnil;
9971
9972 if (NILP (vector))
9973 vector = Fmake_vector (make_number (7), Qnil);
9974
9975 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
9976 ASET (vector, i, Vdeactivate_mark); ++i;
9977 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
9978
9979 if (w)
9980 {
9981 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
9982 ASET (vector, i, w->buffer); ++i;
9983 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
9984 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
9985 }
9986 else
9987 {
9988 int end = i + 4;
9989 for (; i < end; ++i)
9990 ASET (vector, i, Qnil);
9991 }
9992
9993 xassert (i == ASIZE (vector));
9994 return vector;
9995 }
9996
9997
9998 /* Restore global state from VECTOR which was created by
9999 with_echo_area_buffer_unwind_data. */
10000
10001 static Lisp_Object
10002 unwind_with_echo_area_buffer (Lisp_Object vector)
10003 {
10004 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10005 Vdeactivate_mark = AREF (vector, 1);
10006 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10007
10008 if (WINDOWP (AREF (vector, 3)))
10009 {
10010 struct window *w;
10011 Lisp_Object buffer, charpos, bytepos;
10012
10013 w = XWINDOW (AREF (vector, 3));
10014 buffer = AREF (vector, 4);
10015 charpos = AREF (vector, 5);
10016 bytepos = AREF (vector, 6);
10017
10018 w->buffer = buffer;
10019 set_marker_both (w->pointm, buffer,
10020 XFASTINT (charpos), XFASTINT (bytepos));
10021 }
10022
10023 Vwith_echo_area_save_vector = vector;
10024 return Qnil;
10025 }
10026
10027
10028 /* Set up the echo area for use by print functions. MULTIBYTE_P
10029 non-zero means we will print multibyte. */
10030
10031 void
10032 setup_echo_area_for_printing (int multibyte_p)
10033 {
10034 /* If we can't find an echo area any more, exit. */
10035 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10036 Fkill_emacs (Qnil);
10037
10038 ensure_echo_area_buffers ();
10039
10040 if (!message_buf_print)
10041 {
10042 /* A message has been output since the last time we printed.
10043 Choose a fresh echo area buffer. */
10044 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10045 echo_area_buffer[0] = echo_buffer[1];
10046 else
10047 echo_area_buffer[0] = echo_buffer[0];
10048
10049 /* Switch to that buffer and clear it. */
10050 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10051 BVAR (current_buffer, truncate_lines) = Qnil;
10052
10053 if (Z > BEG)
10054 {
10055 ptrdiff_t count = SPECPDL_INDEX ();
10056 specbind (Qinhibit_read_only, Qt);
10057 /* Note that undo recording is always disabled. */
10058 del_range (BEG, Z);
10059 unbind_to (count, Qnil);
10060 }
10061 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10062
10063 /* Set up the buffer for the multibyteness we need. */
10064 if (multibyte_p
10065 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10066 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10067
10068 /* Raise the frame containing the echo area. */
10069 if (minibuffer_auto_raise)
10070 {
10071 struct frame *sf = SELECTED_FRAME ();
10072 Lisp_Object mini_window;
10073 mini_window = FRAME_MINIBUF_WINDOW (sf);
10074 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10075 }
10076
10077 message_log_maybe_newline ();
10078 message_buf_print = 1;
10079 }
10080 else
10081 {
10082 if (NILP (echo_area_buffer[0]))
10083 {
10084 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10085 echo_area_buffer[0] = echo_buffer[1];
10086 else
10087 echo_area_buffer[0] = echo_buffer[0];
10088 }
10089
10090 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10091 {
10092 /* Someone switched buffers between print requests. */
10093 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10094 BVAR (current_buffer, truncate_lines) = Qnil;
10095 }
10096 }
10097 }
10098
10099
10100 /* Display an echo area message in window W. Value is non-zero if W's
10101 height is changed. If display_last_displayed_message_p is
10102 non-zero, display the message that was last displayed, otherwise
10103 display the current message. */
10104
10105 static int
10106 display_echo_area (struct window *w)
10107 {
10108 int i, no_message_p, window_height_changed_p;
10109
10110 /* Temporarily disable garbage collections while displaying the echo
10111 area. This is done because a GC can print a message itself.
10112 That message would modify the echo area buffer's contents while a
10113 redisplay of the buffer is going on, and seriously confuse
10114 redisplay. */
10115 ptrdiff_t count = inhibit_garbage_collection ();
10116
10117 /* If there is no message, we must call display_echo_area_1
10118 nevertheless because it resizes the window. But we will have to
10119 reset the echo_area_buffer in question to nil at the end because
10120 with_echo_area_buffer will sets it to an empty buffer. */
10121 i = display_last_displayed_message_p ? 1 : 0;
10122 no_message_p = NILP (echo_area_buffer[i]);
10123
10124 window_height_changed_p
10125 = with_echo_area_buffer (w, display_last_displayed_message_p,
10126 display_echo_area_1,
10127 (intptr_t) w, Qnil, 0, 0);
10128
10129 if (no_message_p)
10130 echo_area_buffer[i] = Qnil;
10131
10132 unbind_to (count, Qnil);
10133 return window_height_changed_p;
10134 }
10135
10136
10137 /* Helper for display_echo_area. Display the current buffer which
10138 contains the current echo area message in window W, a mini-window,
10139 a pointer to which is passed in A1. A2..A4 are currently not used.
10140 Change the height of W so that all of the message is displayed.
10141 Value is non-zero if height of W was changed. */
10142
10143 static int
10144 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10145 {
10146 intptr_t i1 = a1;
10147 struct window *w = (struct window *) i1;
10148 Lisp_Object window;
10149 struct text_pos start;
10150 int window_height_changed_p = 0;
10151
10152 /* Do this before displaying, so that we have a large enough glyph
10153 matrix for the display. If we can't get enough space for the
10154 whole text, display the last N lines. That works by setting w->start. */
10155 window_height_changed_p = resize_mini_window (w, 0);
10156
10157 /* Use the starting position chosen by resize_mini_window. */
10158 SET_TEXT_POS_FROM_MARKER (start, w->start);
10159
10160 /* Display. */
10161 clear_glyph_matrix (w->desired_matrix);
10162 XSETWINDOW (window, w);
10163 try_window (window, start, 0);
10164
10165 return window_height_changed_p;
10166 }
10167
10168
10169 /* Resize the echo area window to exactly the size needed for the
10170 currently displayed message, if there is one. If a mini-buffer
10171 is active, don't shrink it. */
10172
10173 void
10174 resize_echo_area_exactly (void)
10175 {
10176 if (BUFFERP (echo_area_buffer[0])
10177 && WINDOWP (echo_area_window))
10178 {
10179 struct window *w = XWINDOW (echo_area_window);
10180 int resized_p;
10181 Lisp_Object resize_exactly;
10182
10183 if (minibuf_level == 0)
10184 resize_exactly = Qt;
10185 else
10186 resize_exactly = Qnil;
10187
10188 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10189 (intptr_t) w, resize_exactly,
10190 0, 0);
10191 if (resized_p)
10192 {
10193 ++windows_or_buffers_changed;
10194 ++update_mode_lines;
10195 redisplay_internal ();
10196 }
10197 }
10198 }
10199
10200
10201 /* Callback function for with_echo_area_buffer, when used from
10202 resize_echo_area_exactly. A1 contains a pointer to the window to
10203 resize, EXACTLY non-nil means resize the mini-window exactly to the
10204 size of the text displayed. A3 and A4 are not used. Value is what
10205 resize_mini_window returns. */
10206
10207 static int
10208 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10209 {
10210 intptr_t i1 = a1;
10211 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10212 }
10213
10214
10215 /* Resize mini-window W to fit the size of its contents. EXACT_P
10216 means size the window exactly to the size needed. Otherwise, it's
10217 only enlarged until W's buffer is empty.
10218
10219 Set W->start to the right place to begin display. If the whole
10220 contents fit, start at the beginning. Otherwise, start so as
10221 to make the end of the contents appear. This is particularly
10222 important for y-or-n-p, but seems desirable generally.
10223
10224 Value is non-zero if the window height has been changed. */
10225
10226 int
10227 resize_mini_window (struct window *w, int exact_p)
10228 {
10229 struct frame *f = XFRAME (w->frame);
10230 int window_height_changed_p = 0;
10231
10232 xassert (MINI_WINDOW_P (w));
10233
10234 /* By default, start display at the beginning. */
10235 set_marker_both (w->start, w->buffer,
10236 BUF_BEGV (XBUFFER (w->buffer)),
10237 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10238
10239 /* Don't resize windows while redisplaying a window; it would
10240 confuse redisplay functions when the size of the window they are
10241 displaying changes from under them. Such a resizing can happen,
10242 for instance, when which-func prints a long message while
10243 we are running fontification-functions. We're running these
10244 functions with safe_call which binds inhibit-redisplay to t. */
10245 if (!NILP (Vinhibit_redisplay))
10246 return 0;
10247
10248 /* Nil means don't try to resize. */
10249 if (NILP (Vresize_mini_windows)
10250 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10251 return 0;
10252
10253 if (!FRAME_MINIBUF_ONLY_P (f))
10254 {
10255 struct it it;
10256 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10257 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10258 int height;
10259 EMACS_INT max_height;
10260 int unit = FRAME_LINE_HEIGHT (f);
10261 struct text_pos start;
10262 struct buffer *old_current_buffer = NULL;
10263
10264 if (current_buffer != XBUFFER (w->buffer))
10265 {
10266 old_current_buffer = current_buffer;
10267 set_buffer_internal (XBUFFER (w->buffer));
10268 }
10269
10270 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10271
10272 /* Compute the max. number of lines specified by the user. */
10273 if (FLOATP (Vmax_mini_window_height))
10274 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10275 else if (INTEGERP (Vmax_mini_window_height))
10276 max_height = XINT (Vmax_mini_window_height);
10277 else
10278 max_height = total_height / 4;
10279
10280 /* Correct that max. height if it's bogus. */
10281 max_height = max (1, max_height);
10282 max_height = min (total_height, max_height);
10283
10284 /* Find out the height of the text in the window. */
10285 if (it.line_wrap == TRUNCATE)
10286 height = 1;
10287 else
10288 {
10289 last_height = 0;
10290 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10291 if (it.max_ascent == 0 && it.max_descent == 0)
10292 height = it.current_y + last_height;
10293 else
10294 height = it.current_y + it.max_ascent + it.max_descent;
10295 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10296 height = (height + unit - 1) / unit;
10297 }
10298
10299 /* Compute a suitable window start. */
10300 if (height > max_height)
10301 {
10302 height = max_height;
10303 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10304 move_it_vertically_backward (&it, (height - 1) * unit);
10305 start = it.current.pos;
10306 }
10307 else
10308 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10309 SET_MARKER_FROM_TEXT_POS (w->start, start);
10310
10311 if (EQ (Vresize_mini_windows, Qgrow_only))
10312 {
10313 /* Let it grow only, until we display an empty message, in which
10314 case the window shrinks again. */
10315 if (height > WINDOW_TOTAL_LINES (w))
10316 {
10317 int old_height = WINDOW_TOTAL_LINES (w);
10318 freeze_window_starts (f, 1);
10319 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10320 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10321 }
10322 else if (height < WINDOW_TOTAL_LINES (w)
10323 && (exact_p || BEGV == ZV))
10324 {
10325 int old_height = WINDOW_TOTAL_LINES (w);
10326 freeze_window_starts (f, 0);
10327 shrink_mini_window (w);
10328 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10329 }
10330 }
10331 else
10332 {
10333 /* Always resize to exact size needed. */
10334 if (height > WINDOW_TOTAL_LINES (w))
10335 {
10336 int old_height = WINDOW_TOTAL_LINES (w);
10337 freeze_window_starts (f, 1);
10338 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10339 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10340 }
10341 else if (height < WINDOW_TOTAL_LINES (w))
10342 {
10343 int old_height = WINDOW_TOTAL_LINES (w);
10344 freeze_window_starts (f, 0);
10345 shrink_mini_window (w);
10346
10347 if (height)
10348 {
10349 freeze_window_starts (f, 1);
10350 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10351 }
10352
10353 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10354 }
10355 }
10356
10357 if (old_current_buffer)
10358 set_buffer_internal (old_current_buffer);
10359 }
10360
10361 return window_height_changed_p;
10362 }
10363
10364
10365 /* Value is the current message, a string, or nil if there is no
10366 current message. */
10367
10368 Lisp_Object
10369 current_message (void)
10370 {
10371 Lisp_Object msg;
10372
10373 if (!BUFFERP (echo_area_buffer[0]))
10374 msg = Qnil;
10375 else
10376 {
10377 with_echo_area_buffer (0, 0, current_message_1,
10378 (intptr_t) &msg, Qnil, 0, 0);
10379 if (NILP (msg))
10380 echo_area_buffer[0] = Qnil;
10381 }
10382
10383 return msg;
10384 }
10385
10386
10387 static int
10388 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10389 {
10390 intptr_t i1 = a1;
10391 Lisp_Object *msg = (Lisp_Object *) i1;
10392
10393 if (Z > BEG)
10394 *msg = make_buffer_string (BEG, Z, 1);
10395 else
10396 *msg = Qnil;
10397 return 0;
10398 }
10399
10400
10401 /* Push the current message on Vmessage_stack for later restoration
10402 by restore_message. Value is non-zero if the current message isn't
10403 empty. This is a relatively infrequent operation, so it's not
10404 worth optimizing. */
10405
10406 int
10407 push_message (void)
10408 {
10409 Lisp_Object msg;
10410 msg = current_message ();
10411 Vmessage_stack = Fcons (msg, Vmessage_stack);
10412 return STRINGP (msg);
10413 }
10414
10415
10416 /* Restore message display from the top of Vmessage_stack. */
10417
10418 void
10419 restore_message (void)
10420 {
10421 Lisp_Object msg;
10422
10423 xassert (CONSP (Vmessage_stack));
10424 msg = XCAR (Vmessage_stack);
10425 if (STRINGP (msg))
10426 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10427 else
10428 message3_nolog (msg, 0, 0);
10429 }
10430
10431
10432 /* Handler for record_unwind_protect calling pop_message. */
10433
10434 Lisp_Object
10435 pop_message_unwind (Lisp_Object dummy)
10436 {
10437 pop_message ();
10438 return Qnil;
10439 }
10440
10441 /* Pop the top-most entry off Vmessage_stack. */
10442
10443 static void
10444 pop_message (void)
10445 {
10446 xassert (CONSP (Vmessage_stack));
10447 Vmessage_stack = XCDR (Vmessage_stack);
10448 }
10449
10450
10451 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10452 exits. If the stack is not empty, we have a missing pop_message
10453 somewhere. */
10454
10455 void
10456 check_message_stack (void)
10457 {
10458 if (!NILP (Vmessage_stack))
10459 abort ();
10460 }
10461
10462
10463 /* Truncate to NCHARS what will be displayed in the echo area the next
10464 time we display it---but don't redisplay it now. */
10465
10466 void
10467 truncate_echo_area (ptrdiff_t nchars)
10468 {
10469 if (nchars == 0)
10470 echo_area_buffer[0] = Qnil;
10471 /* A null message buffer means that the frame hasn't really been
10472 initialized yet. Error messages get reported properly by
10473 cmd_error, so this must be just an informative message; toss it. */
10474 else if (!noninteractive
10475 && INTERACTIVE
10476 && !NILP (echo_area_buffer[0]))
10477 {
10478 struct frame *sf = SELECTED_FRAME ();
10479 if (FRAME_MESSAGE_BUF (sf))
10480 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10481 }
10482 }
10483
10484
10485 /* Helper function for truncate_echo_area. Truncate the current
10486 message to at most NCHARS characters. */
10487
10488 static int
10489 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10490 {
10491 if (BEG + nchars < Z)
10492 del_range (BEG + nchars, Z);
10493 if (Z == BEG)
10494 echo_area_buffer[0] = Qnil;
10495 return 0;
10496 }
10497
10498
10499 /* Set the current message to a substring of S or STRING.
10500
10501 If STRING is a Lisp string, set the message to the first NBYTES
10502 bytes from STRING. NBYTES zero means use the whole string. If
10503 STRING is multibyte, the message will be displayed multibyte.
10504
10505 If S is not null, set the message to the first LEN bytes of S. LEN
10506 zero means use the whole string. MULTIBYTE_P non-zero means S is
10507 multibyte. Display the message multibyte in that case.
10508
10509 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10510 to t before calling set_message_1 (which calls insert).
10511 */
10512
10513 static void
10514 set_message (const char *s, Lisp_Object string,
10515 ptrdiff_t nbytes, int multibyte_p)
10516 {
10517 message_enable_multibyte
10518 = ((s && multibyte_p)
10519 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10520
10521 with_echo_area_buffer (0, -1, set_message_1,
10522 (intptr_t) s, string, nbytes, multibyte_p);
10523 message_buf_print = 0;
10524 help_echo_showing_p = 0;
10525 }
10526
10527
10528 /* Helper function for set_message. Arguments have the same meaning
10529 as there, with A1 corresponding to S and A2 corresponding to STRING
10530 This function is called with the echo area buffer being
10531 current. */
10532
10533 static int
10534 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10535 {
10536 intptr_t i1 = a1;
10537 const char *s = (const char *) i1;
10538 const unsigned char *msg = (const unsigned char *) s;
10539 Lisp_Object string = a2;
10540
10541 /* Change multibyteness of the echo buffer appropriately. */
10542 if (message_enable_multibyte
10543 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10544 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10545
10546 BVAR (current_buffer, truncate_lines) = message_truncate_lines ? Qt : Qnil;
10547 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10548 BVAR (current_buffer, bidi_paragraph_direction) = Qleft_to_right;
10549
10550 /* Insert new message at BEG. */
10551 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10552
10553 if (STRINGP (string))
10554 {
10555 ptrdiff_t nchars;
10556
10557 if (nbytes == 0)
10558 nbytes = SBYTES (string);
10559 nchars = string_byte_to_char (string, nbytes);
10560
10561 /* This function takes care of single/multibyte conversion. We
10562 just have to ensure that the echo area buffer has the right
10563 setting of enable_multibyte_characters. */
10564 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10565 }
10566 else if (s)
10567 {
10568 if (nbytes == 0)
10569 nbytes = strlen (s);
10570
10571 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10572 {
10573 /* Convert from multi-byte to single-byte. */
10574 ptrdiff_t i;
10575 int c, n;
10576 char work[1];
10577
10578 /* Convert a multibyte string to single-byte. */
10579 for (i = 0; i < nbytes; i += n)
10580 {
10581 c = string_char_and_length (msg + i, &n);
10582 work[0] = (ASCII_CHAR_P (c)
10583 ? c
10584 : multibyte_char_to_unibyte (c));
10585 insert_1_both (work, 1, 1, 1, 0, 0);
10586 }
10587 }
10588 else if (!multibyte_p
10589 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10590 {
10591 /* Convert from single-byte to multi-byte. */
10592 ptrdiff_t i;
10593 int c, n;
10594 unsigned char str[MAX_MULTIBYTE_LENGTH];
10595
10596 /* Convert a single-byte string to multibyte. */
10597 for (i = 0; i < nbytes; i++)
10598 {
10599 c = msg[i];
10600 MAKE_CHAR_MULTIBYTE (c);
10601 n = CHAR_STRING (c, str);
10602 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10603 }
10604 }
10605 else
10606 insert_1 (s, nbytes, 1, 0, 0);
10607 }
10608
10609 return 0;
10610 }
10611
10612
10613 /* Clear messages. CURRENT_P non-zero means clear the current
10614 message. LAST_DISPLAYED_P non-zero means clear the message
10615 last displayed. */
10616
10617 void
10618 clear_message (int current_p, int last_displayed_p)
10619 {
10620 if (current_p)
10621 {
10622 echo_area_buffer[0] = Qnil;
10623 message_cleared_p = 1;
10624 }
10625
10626 if (last_displayed_p)
10627 echo_area_buffer[1] = Qnil;
10628
10629 message_buf_print = 0;
10630 }
10631
10632 /* Clear garbaged frames.
10633
10634 This function is used where the old redisplay called
10635 redraw_garbaged_frames which in turn called redraw_frame which in
10636 turn called clear_frame. The call to clear_frame was a source of
10637 flickering. I believe a clear_frame is not necessary. It should
10638 suffice in the new redisplay to invalidate all current matrices,
10639 and ensure a complete redisplay of all windows. */
10640
10641 static void
10642 clear_garbaged_frames (void)
10643 {
10644 if (frame_garbaged)
10645 {
10646 Lisp_Object tail, frame;
10647 int changed_count = 0;
10648
10649 FOR_EACH_FRAME (tail, frame)
10650 {
10651 struct frame *f = XFRAME (frame);
10652
10653 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10654 {
10655 if (f->resized_p)
10656 {
10657 Fredraw_frame (frame);
10658 f->force_flush_display_p = 1;
10659 }
10660 clear_current_matrices (f);
10661 changed_count++;
10662 f->garbaged = 0;
10663 f->resized_p = 0;
10664 }
10665 }
10666
10667 frame_garbaged = 0;
10668 if (changed_count)
10669 ++windows_or_buffers_changed;
10670 }
10671 }
10672
10673
10674 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10675 is non-zero update selected_frame. Value is non-zero if the
10676 mini-windows height has been changed. */
10677
10678 static int
10679 echo_area_display (int update_frame_p)
10680 {
10681 Lisp_Object mini_window;
10682 struct window *w;
10683 struct frame *f;
10684 int window_height_changed_p = 0;
10685 struct frame *sf = SELECTED_FRAME ();
10686
10687 mini_window = FRAME_MINIBUF_WINDOW (sf);
10688 w = XWINDOW (mini_window);
10689 f = XFRAME (WINDOW_FRAME (w));
10690
10691 /* Don't display if frame is invisible or not yet initialized. */
10692 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10693 return 0;
10694
10695 #ifdef HAVE_WINDOW_SYSTEM
10696 /* When Emacs starts, selected_frame may be the initial terminal
10697 frame. If we let this through, a message would be displayed on
10698 the terminal. */
10699 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10700 return 0;
10701 #endif /* HAVE_WINDOW_SYSTEM */
10702
10703 /* Redraw garbaged frames. */
10704 if (frame_garbaged)
10705 clear_garbaged_frames ();
10706
10707 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10708 {
10709 echo_area_window = mini_window;
10710 window_height_changed_p = display_echo_area (w);
10711 w->must_be_updated_p = 1;
10712
10713 /* Update the display, unless called from redisplay_internal.
10714 Also don't update the screen during redisplay itself. The
10715 update will happen at the end of redisplay, and an update
10716 here could cause confusion. */
10717 if (update_frame_p && !redisplaying_p)
10718 {
10719 int n = 0;
10720
10721 /* If the display update has been interrupted by pending
10722 input, update mode lines in the frame. Due to the
10723 pending input, it might have been that redisplay hasn't
10724 been called, so that mode lines above the echo area are
10725 garbaged. This looks odd, so we prevent it here. */
10726 if (!display_completed)
10727 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10728
10729 if (window_height_changed_p
10730 /* Don't do this if Emacs is shutting down. Redisplay
10731 needs to run hooks. */
10732 && !NILP (Vrun_hooks))
10733 {
10734 /* Must update other windows. Likewise as in other
10735 cases, don't let this update be interrupted by
10736 pending input. */
10737 ptrdiff_t count = SPECPDL_INDEX ();
10738 specbind (Qredisplay_dont_pause, Qt);
10739 windows_or_buffers_changed = 1;
10740 redisplay_internal ();
10741 unbind_to (count, Qnil);
10742 }
10743 else if (FRAME_WINDOW_P (f) && n == 0)
10744 {
10745 /* Window configuration is the same as before.
10746 Can do with a display update of the echo area,
10747 unless we displayed some mode lines. */
10748 update_single_window (w, 1);
10749 FRAME_RIF (f)->flush_display (f);
10750 }
10751 else
10752 update_frame (f, 1, 1);
10753
10754 /* If cursor is in the echo area, make sure that the next
10755 redisplay displays the minibuffer, so that the cursor will
10756 be replaced with what the minibuffer wants. */
10757 if (cursor_in_echo_area)
10758 ++windows_or_buffers_changed;
10759 }
10760 }
10761 else if (!EQ (mini_window, selected_window))
10762 windows_or_buffers_changed++;
10763
10764 /* Last displayed message is now the current message. */
10765 echo_area_buffer[1] = echo_area_buffer[0];
10766 /* Inform read_char that we're not echoing. */
10767 echo_message_buffer = Qnil;
10768
10769 /* Prevent redisplay optimization in redisplay_internal by resetting
10770 this_line_start_pos. This is done because the mini-buffer now
10771 displays the message instead of its buffer text. */
10772 if (EQ (mini_window, selected_window))
10773 CHARPOS (this_line_start_pos) = 0;
10774
10775 return window_height_changed_p;
10776 }
10777
10778
10779 \f
10780 /***********************************************************************
10781 Mode Lines and Frame Titles
10782 ***********************************************************************/
10783
10784 /* A buffer for constructing non-propertized mode-line strings and
10785 frame titles in it; allocated from the heap in init_xdisp and
10786 resized as needed in store_mode_line_noprop_char. */
10787
10788 static char *mode_line_noprop_buf;
10789
10790 /* The buffer's end, and a current output position in it. */
10791
10792 static char *mode_line_noprop_buf_end;
10793 static char *mode_line_noprop_ptr;
10794
10795 #define MODE_LINE_NOPROP_LEN(start) \
10796 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10797
10798 static enum {
10799 MODE_LINE_DISPLAY = 0,
10800 MODE_LINE_TITLE,
10801 MODE_LINE_NOPROP,
10802 MODE_LINE_STRING
10803 } mode_line_target;
10804
10805 /* Alist that caches the results of :propertize.
10806 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10807 static Lisp_Object mode_line_proptrans_alist;
10808
10809 /* List of strings making up the mode-line. */
10810 static Lisp_Object mode_line_string_list;
10811
10812 /* Base face property when building propertized mode line string. */
10813 static Lisp_Object mode_line_string_face;
10814 static Lisp_Object mode_line_string_face_prop;
10815
10816
10817 /* Unwind data for mode line strings */
10818
10819 static Lisp_Object Vmode_line_unwind_vector;
10820
10821 static Lisp_Object
10822 format_mode_line_unwind_data (struct buffer *obuf,
10823 Lisp_Object owin,
10824 int save_proptrans)
10825 {
10826 Lisp_Object vector, tmp;
10827
10828 /* Reduce consing by keeping one vector in
10829 Vwith_echo_area_save_vector. */
10830 vector = Vmode_line_unwind_vector;
10831 Vmode_line_unwind_vector = Qnil;
10832
10833 if (NILP (vector))
10834 vector = Fmake_vector (make_number (8), Qnil);
10835
10836 ASET (vector, 0, make_number (mode_line_target));
10837 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10838 ASET (vector, 2, mode_line_string_list);
10839 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10840 ASET (vector, 4, mode_line_string_face);
10841 ASET (vector, 5, mode_line_string_face_prop);
10842
10843 if (obuf)
10844 XSETBUFFER (tmp, obuf);
10845 else
10846 tmp = Qnil;
10847 ASET (vector, 6, tmp);
10848 ASET (vector, 7, owin);
10849
10850 return vector;
10851 }
10852
10853 static Lisp_Object
10854 unwind_format_mode_line (Lisp_Object vector)
10855 {
10856 mode_line_target = XINT (AREF (vector, 0));
10857 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10858 mode_line_string_list = AREF (vector, 2);
10859 if (! EQ (AREF (vector, 3), Qt))
10860 mode_line_proptrans_alist = AREF (vector, 3);
10861 mode_line_string_face = AREF (vector, 4);
10862 mode_line_string_face_prop = AREF (vector, 5);
10863
10864 if (!NILP (AREF (vector, 7)))
10865 /* Select window before buffer, since it may change the buffer. */
10866 Fselect_window (AREF (vector, 7), Qt);
10867
10868 if (!NILP (AREF (vector, 6)))
10869 {
10870 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10871 ASET (vector, 6, Qnil);
10872 }
10873
10874 Vmode_line_unwind_vector = vector;
10875 return Qnil;
10876 }
10877
10878
10879 /* Store a single character C for the frame title in mode_line_noprop_buf.
10880 Re-allocate mode_line_noprop_buf if necessary. */
10881
10882 static void
10883 store_mode_line_noprop_char (char c)
10884 {
10885 /* If output position has reached the end of the allocated buffer,
10886 increase the buffer's size. */
10887 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10888 {
10889 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10890 ptrdiff_t size = len;
10891 mode_line_noprop_buf =
10892 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10893 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10894 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10895 }
10896
10897 *mode_line_noprop_ptr++ = c;
10898 }
10899
10900
10901 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10902 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10903 characters that yield more columns than PRECISION; PRECISION <= 0
10904 means copy the whole string. Pad with spaces until FIELD_WIDTH
10905 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10906 pad. Called from display_mode_element when it is used to build a
10907 frame title. */
10908
10909 static int
10910 store_mode_line_noprop (const char *string, int field_width, int precision)
10911 {
10912 const unsigned char *str = (const unsigned char *) string;
10913 int n = 0;
10914 ptrdiff_t dummy, nbytes;
10915
10916 /* Copy at most PRECISION chars from STR. */
10917 nbytes = strlen (string);
10918 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10919 while (nbytes--)
10920 store_mode_line_noprop_char (*str++);
10921
10922 /* Fill up with spaces until FIELD_WIDTH reached. */
10923 while (field_width > 0
10924 && n < field_width)
10925 {
10926 store_mode_line_noprop_char (' ');
10927 ++n;
10928 }
10929
10930 return n;
10931 }
10932
10933 /***********************************************************************
10934 Frame Titles
10935 ***********************************************************************/
10936
10937 #ifdef HAVE_WINDOW_SYSTEM
10938
10939 /* Set the title of FRAME, if it has changed. The title format is
10940 Vicon_title_format if FRAME is iconified, otherwise it is
10941 frame_title_format. */
10942
10943 static void
10944 x_consider_frame_title (Lisp_Object frame)
10945 {
10946 struct frame *f = XFRAME (frame);
10947
10948 if (FRAME_WINDOW_P (f)
10949 || FRAME_MINIBUF_ONLY_P (f)
10950 || f->explicit_name)
10951 {
10952 /* Do we have more than one visible frame on this X display? */
10953 Lisp_Object tail;
10954 Lisp_Object fmt;
10955 ptrdiff_t title_start;
10956 char *title;
10957 ptrdiff_t len;
10958 struct it it;
10959 ptrdiff_t count = SPECPDL_INDEX ();
10960
10961 for (tail = Vframe_list; CONSP (tail); tail = XCDR (tail))
10962 {
10963 Lisp_Object other_frame = XCAR (tail);
10964 struct frame *tf = XFRAME (other_frame);
10965
10966 if (tf != f
10967 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10968 && !FRAME_MINIBUF_ONLY_P (tf)
10969 && !EQ (other_frame, tip_frame)
10970 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
10971 break;
10972 }
10973
10974 /* Set global variable indicating that multiple frames exist. */
10975 multiple_frames = CONSP (tail);
10976
10977 /* Switch to the buffer of selected window of the frame. Set up
10978 mode_line_target so that display_mode_element will output into
10979 mode_line_noprop_buf; then display the title. */
10980 record_unwind_protect (unwind_format_mode_line,
10981 format_mode_line_unwind_data
10982 (current_buffer, selected_window, 0));
10983
10984 Fselect_window (f->selected_window, Qt);
10985 set_buffer_internal_1 (XBUFFER (XWINDOW (f->selected_window)->buffer));
10986 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
10987
10988 mode_line_target = MODE_LINE_TITLE;
10989 title_start = MODE_LINE_NOPROP_LEN (0);
10990 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
10991 NULL, DEFAULT_FACE_ID);
10992 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
10993 len = MODE_LINE_NOPROP_LEN (title_start);
10994 title = mode_line_noprop_buf + title_start;
10995 unbind_to (count, Qnil);
10996
10997 /* Set the title only if it's changed. This avoids consing in
10998 the common case where it hasn't. (If it turns out that we've
10999 already wasted too much time by walking through the list with
11000 display_mode_element, then we might need to optimize at a
11001 higher level than this.) */
11002 if (! STRINGP (f->name)
11003 || SBYTES (f->name) != len
11004 || memcmp (title, SDATA (f->name), len) != 0)
11005 x_implicitly_set_name (f, make_string (title, len), Qnil);
11006 }
11007 }
11008
11009 #endif /* not HAVE_WINDOW_SYSTEM */
11010
11011
11012
11013 \f
11014 /***********************************************************************
11015 Menu Bars
11016 ***********************************************************************/
11017
11018
11019 /* Prepare for redisplay by updating menu-bar item lists when
11020 appropriate. This can call eval. */
11021
11022 void
11023 prepare_menu_bars (void)
11024 {
11025 int all_windows;
11026 struct gcpro gcpro1, gcpro2;
11027 struct frame *f;
11028 Lisp_Object tooltip_frame;
11029
11030 #ifdef HAVE_WINDOW_SYSTEM
11031 tooltip_frame = tip_frame;
11032 #else
11033 tooltip_frame = Qnil;
11034 #endif
11035
11036 /* Update all frame titles based on their buffer names, etc. We do
11037 this before the menu bars so that the buffer-menu will show the
11038 up-to-date frame titles. */
11039 #ifdef HAVE_WINDOW_SYSTEM
11040 if (windows_or_buffers_changed || update_mode_lines)
11041 {
11042 Lisp_Object tail, frame;
11043
11044 FOR_EACH_FRAME (tail, frame)
11045 {
11046 f = XFRAME (frame);
11047 if (!EQ (frame, tooltip_frame)
11048 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11049 x_consider_frame_title (frame);
11050 }
11051 }
11052 #endif /* HAVE_WINDOW_SYSTEM */
11053
11054 /* Update the menu bar item lists, if appropriate. This has to be
11055 done before any actual redisplay or generation of display lines. */
11056 all_windows = (update_mode_lines
11057 || buffer_shared > 1
11058 || windows_or_buffers_changed);
11059 if (all_windows)
11060 {
11061 Lisp_Object tail, frame;
11062 ptrdiff_t count = SPECPDL_INDEX ();
11063 /* 1 means that update_menu_bar has run its hooks
11064 so any further calls to update_menu_bar shouldn't do so again. */
11065 int menu_bar_hooks_run = 0;
11066
11067 record_unwind_save_match_data ();
11068
11069 FOR_EACH_FRAME (tail, frame)
11070 {
11071 f = XFRAME (frame);
11072
11073 /* Ignore tooltip frame. */
11074 if (EQ (frame, tooltip_frame))
11075 continue;
11076
11077 /* If a window on this frame changed size, report that to
11078 the user and clear the size-change flag. */
11079 if (FRAME_WINDOW_SIZES_CHANGED (f))
11080 {
11081 Lisp_Object functions;
11082
11083 /* Clear flag first in case we get an error below. */
11084 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11085 functions = Vwindow_size_change_functions;
11086 GCPRO2 (tail, functions);
11087
11088 while (CONSP (functions))
11089 {
11090 if (!EQ (XCAR (functions), Qt))
11091 call1 (XCAR (functions), frame);
11092 functions = XCDR (functions);
11093 }
11094 UNGCPRO;
11095 }
11096
11097 GCPRO1 (tail);
11098 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11099 #ifdef HAVE_WINDOW_SYSTEM
11100 update_tool_bar (f, 0);
11101 #endif
11102 #ifdef HAVE_NS
11103 if (windows_or_buffers_changed
11104 && FRAME_NS_P (f))
11105 ns_set_doc_edited (f, Fbuffer_modified_p
11106 (XWINDOW (f->selected_window)->buffer));
11107 #endif
11108 UNGCPRO;
11109 }
11110
11111 unbind_to (count, Qnil);
11112 }
11113 else
11114 {
11115 struct frame *sf = SELECTED_FRAME ();
11116 update_menu_bar (sf, 1, 0);
11117 #ifdef HAVE_WINDOW_SYSTEM
11118 update_tool_bar (sf, 1);
11119 #endif
11120 }
11121 }
11122
11123
11124 /* Update the menu bar item list for frame F. This has to be done
11125 before we start to fill in any display lines, because it can call
11126 eval.
11127
11128 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11129
11130 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11131 already ran the menu bar hooks for this redisplay, so there
11132 is no need to run them again. The return value is the
11133 updated value of this flag, to pass to the next call. */
11134
11135 static int
11136 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11137 {
11138 Lisp_Object window;
11139 register struct window *w;
11140
11141 /* If called recursively during a menu update, do nothing. This can
11142 happen when, for instance, an activate-menubar-hook causes a
11143 redisplay. */
11144 if (inhibit_menubar_update)
11145 return hooks_run;
11146
11147 window = FRAME_SELECTED_WINDOW (f);
11148 w = XWINDOW (window);
11149
11150 if (FRAME_WINDOW_P (f)
11151 ?
11152 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11153 || defined (HAVE_NS) || defined (USE_GTK)
11154 FRAME_EXTERNAL_MENU_BAR (f)
11155 #else
11156 FRAME_MENU_BAR_LINES (f) > 0
11157 #endif
11158 : FRAME_MENU_BAR_LINES (f) > 0)
11159 {
11160 /* If the user has switched buffers or windows, we need to
11161 recompute to reflect the new bindings. But we'll
11162 recompute when update_mode_lines is set too; that means
11163 that people can use force-mode-line-update to request
11164 that the menu bar be recomputed. The adverse effect on
11165 the rest of the redisplay algorithm is about the same as
11166 windows_or_buffers_changed anyway. */
11167 if (windows_or_buffers_changed
11168 /* This used to test w->update_mode_line, but we believe
11169 there is no need to recompute the menu in that case. */
11170 || update_mode_lines
11171 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11172 < BUF_MODIFF (XBUFFER (w->buffer)))
11173 != w->last_had_star)
11174 || ((!NILP (Vtransient_mark_mode)
11175 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11176 != !NILP (w->region_showing)))
11177 {
11178 struct buffer *prev = current_buffer;
11179 ptrdiff_t count = SPECPDL_INDEX ();
11180
11181 specbind (Qinhibit_menubar_update, Qt);
11182
11183 set_buffer_internal_1 (XBUFFER (w->buffer));
11184 if (save_match_data)
11185 record_unwind_save_match_data ();
11186 if (NILP (Voverriding_local_map_menu_flag))
11187 {
11188 specbind (Qoverriding_terminal_local_map, Qnil);
11189 specbind (Qoverriding_local_map, Qnil);
11190 }
11191
11192 if (!hooks_run)
11193 {
11194 /* Run the Lucid hook. */
11195 safe_run_hooks (Qactivate_menubar_hook);
11196
11197 /* If it has changed current-menubar from previous value,
11198 really recompute the menu-bar from the value. */
11199 if (! NILP (Vlucid_menu_bar_dirty_flag))
11200 call0 (Qrecompute_lucid_menubar);
11201
11202 safe_run_hooks (Qmenu_bar_update_hook);
11203
11204 hooks_run = 1;
11205 }
11206
11207 XSETFRAME (Vmenu_updating_frame, f);
11208 FRAME_MENU_BAR_ITEMS (f) = menu_bar_items (FRAME_MENU_BAR_ITEMS (f));
11209
11210 /* Redisplay the menu bar in case we changed it. */
11211 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11212 || defined (HAVE_NS) || defined (USE_GTK)
11213 if (FRAME_WINDOW_P (f))
11214 {
11215 #if defined (HAVE_NS)
11216 /* All frames on Mac OS share the same menubar. So only
11217 the selected frame should be allowed to set it. */
11218 if (f == SELECTED_FRAME ())
11219 #endif
11220 set_frame_menubar (f, 0, 0);
11221 }
11222 else
11223 /* On a terminal screen, the menu bar is an ordinary screen
11224 line, and this makes it get updated. */
11225 w->update_mode_line = 1;
11226 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11227 /* In the non-toolkit version, the menu bar is an ordinary screen
11228 line, and this makes it get updated. */
11229 w->update_mode_line = 1;
11230 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11231
11232 unbind_to (count, Qnil);
11233 set_buffer_internal_1 (prev);
11234 }
11235 }
11236
11237 return hooks_run;
11238 }
11239
11240
11241 \f
11242 /***********************************************************************
11243 Output Cursor
11244 ***********************************************************************/
11245
11246 #ifdef HAVE_WINDOW_SYSTEM
11247
11248 /* EXPORT:
11249 Nominal cursor position -- where to draw output.
11250 HPOS and VPOS are window relative glyph matrix coordinates.
11251 X and Y are window relative pixel coordinates. */
11252
11253 struct cursor_pos output_cursor;
11254
11255
11256 /* EXPORT:
11257 Set the global variable output_cursor to CURSOR. All cursor
11258 positions are relative to updated_window. */
11259
11260 void
11261 set_output_cursor (struct cursor_pos *cursor)
11262 {
11263 output_cursor.hpos = cursor->hpos;
11264 output_cursor.vpos = cursor->vpos;
11265 output_cursor.x = cursor->x;
11266 output_cursor.y = cursor->y;
11267 }
11268
11269
11270 /* EXPORT for RIF:
11271 Set a nominal cursor position.
11272
11273 HPOS and VPOS are column/row positions in a window glyph matrix. X
11274 and Y are window text area relative pixel positions.
11275
11276 If this is done during an update, updated_window will contain the
11277 window that is being updated and the position is the future output
11278 cursor position for that window. If updated_window is null, use
11279 selected_window and display the cursor at the given position. */
11280
11281 void
11282 x_cursor_to (int vpos, int hpos, int y, int x)
11283 {
11284 struct window *w;
11285
11286 /* If updated_window is not set, work on selected_window. */
11287 if (updated_window)
11288 w = updated_window;
11289 else
11290 w = XWINDOW (selected_window);
11291
11292 /* Set the output cursor. */
11293 output_cursor.hpos = hpos;
11294 output_cursor.vpos = vpos;
11295 output_cursor.x = x;
11296 output_cursor.y = y;
11297
11298 /* If not called as part of an update, really display the cursor.
11299 This will also set the cursor position of W. */
11300 if (updated_window == NULL)
11301 {
11302 BLOCK_INPUT;
11303 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11304 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11305 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11306 UNBLOCK_INPUT;
11307 }
11308 }
11309
11310 #endif /* HAVE_WINDOW_SYSTEM */
11311
11312 \f
11313 /***********************************************************************
11314 Tool-bars
11315 ***********************************************************************/
11316
11317 #ifdef HAVE_WINDOW_SYSTEM
11318
11319 /* Where the mouse was last time we reported a mouse event. */
11320
11321 FRAME_PTR last_mouse_frame;
11322
11323 /* Tool-bar item index of the item on which a mouse button was pressed
11324 or -1. */
11325
11326 int last_tool_bar_item;
11327
11328
11329 static Lisp_Object
11330 update_tool_bar_unwind (Lisp_Object frame)
11331 {
11332 selected_frame = frame;
11333 return Qnil;
11334 }
11335
11336 /* Update the tool-bar item list for frame F. This has to be done
11337 before we start to fill in any display lines. Called from
11338 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11339 and restore it here. */
11340
11341 static void
11342 update_tool_bar (struct frame *f, int save_match_data)
11343 {
11344 #if defined (USE_GTK) || defined (HAVE_NS)
11345 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11346 #else
11347 int do_update = WINDOWP (f->tool_bar_window)
11348 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11349 #endif
11350
11351 if (do_update)
11352 {
11353 Lisp_Object window;
11354 struct window *w;
11355
11356 window = FRAME_SELECTED_WINDOW (f);
11357 w = XWINDOW (window);
11358
11359 /* If the user has switched buffers or windows, we need to
11360 recompute to reflect the new bindings. But we'll
11361 recompute when update_mode_lines is set too; that means
11362 that people can use force-mode-line-update to request
11363 that the menu bar be recomputed. The adverse effect on
11364 the rest of the redisplay algorithm is about the same as
11365 windows_or_buffers_changed anyway. */
11366 if (windows_or_buffers_changed
11367 || w->update_mode_line
11368 || update_mode_lines
11369 || ((BUF_SAVE_MODIFF (XBUFFER (w->buffer))
11370 < BUF_MODIFF (XBUFFER (w->buffer)))
11371 != w->last_had_star)
11372 || ((!NILP (Vtransient_mark_mode)
11373 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
11374 != !NILP (w->region_showing)))
11375 {
11376 struct buffer *prev = current_buffer;
11377 ptrdiff_t count = SPECPDL_INDEX ();
11378 Lisp_Object frame, new_tool_bar;
11379 int new_n_tool_bar;
11380 struct gcpro gcpro1;
11381
11382 /* Set current_buffer to the buffer of the selected
11383 window of the frame, so that we get the right local
11384 keymaps. */
11385 set_buffer_internal_1 (XBUFFER (w->buffer));
11386
11387 /* Save match data, if we must. */
11388 if (save_match_data)
11389 record_unwind_save_match_data ();
11390
11391 /* Make sure that we don't accidentally use bogus keymaps. */
11392 if (NILP (Voverriding_local_map_menu_flag))
11393 {
11394 specbind (Qoverriding_terminal_local_map, Qnil);
11395 specbind (Qoverriding_local_map, Qnil);
11396 }
11397
11398 GCPRO1 (new_tool_bar);
11399
11400 /* We must temporarily set the selected frame to this frame
11401 before calling tool_bar_items, because the calculation of
11402 the tool-bar keymap uses the selected frame (see
11403 `tool-bar-make-keymap' in tool-bar.el). */
11404 record_unwind_protect (update_tool_bar_unwind, selected_frame);
11405 XSETFRAME (frame, f);
11406 selected_frame = frame;
11407
11408 /* Build desired tool-bar items from keymaps. */
11409 new_tool_bar = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11410 &new_n_tool_bar);
11411
11412 /* Redisplay the tool-bar if we changed it. */
11413 if (new_n_tool_bar != f->n_tool_bar_items
11414 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11415 {
11416 /* Redisplay that happens asynchronously due to an expose event
11417 may access f->tool_bar_items. Make sure we update both
11418 variables within BLOCK_INPUT so no such event interrupts. */
11419 BLOCK_INPUT;
11420 f->tool_bar_items = new_tool_bar;
11421 f->n_tool_bar_items = new_n_tool_bar;
11422 w->update_mode_line = 1;
11423 UNBLOCK_INPUT;
11424 }
11425
11426 UNGCPRO;
11427
11428 unbind_to (count, Qnil);
11429 set_buffer_internal_1 (prev);
11430 }
11431 }
11432 }
11433
11434
11435 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11436 F's desired tool-bar contents. F->tool_bar_items must have
11437 been set up previously by calling prepare_menu_bars. */
11438
11439 static void
11440 build_desired_tool_bar_string (struct frame *f)
11441 {
11442 int i, size, size_needed;
11443 struct gcpro gcpro1, gcpro2, gcpro3;
11444 Lisp_Object image, plist, props;
11445
11446 image = plist = props = Qnil;
11447 GCPRO3 (image, plist, props);
11448
11449 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11450 Otherwise, make a new string. */
11451
11452 /* The size of the string we might be able to reuse. */
11453 size = (STRINGP (f->desired_tool_bar_string)
11454 ? SCHARS (f->desired_tool_bar_string)
11455 : 0);
11456
11457 /* We need one space in the string for each image. */
11458 size_needed = f->n_tool_bar_items;
11459
11460 /* Reuse f->desired_tool_bar_string, if possible. */
11461 if (size < size_needed || NILP (f->desired_tool_bar_string))
11462 f->desired_tool_bar_string = Fmake_string (make_number (size_needed),
11463 make_number (' '));
11464 else
11465 {
11466 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11467 Fremove_text_properties (make_number (0), make_number (size),
11468 props, f->desired_tool_bar_string);
11469 }
11470
11471 /* Put a `display' property on the string for the images to display,
11472 put a `menu_item' property on tool-bar items with a value that
11473 is the index of the item in F's tool-bar item vector. */
11474 for (i = 0; i < f->n_tool_bar_items; ++i)
11475 {
11476 #define PROP(IDX) AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11477
11478 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11479 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11480 int hmargin, vmargin, relief, idx, end;
11481
11482 /* If image is a vector, choose the image according to the
11483 button state. */
11484 image = PROP (TOOL_BAR_ITEM_IMAGES);
11485 if (VECTORP (image))
11486 {
11487 if (enabled_p)
11488 idx = (selected_p
11489 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11490 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11491 else
11492 idx = (selected_p
11493 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11494 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11495
11496 xassert (ASIZE (image) >= idx);
11497 image = AREF (image, idx);
11498 }
11499 else
11500 idx = -1;
11501
11502 /* Ignore invalid image specifications. */
11503 if (!valid_image_p (image))
11504 continue;
11505
11506 /* Display the tool-bar button pressed, or depressed. */
11507 plist = Fcopy_sequence (XCDR (image));
11508
11509 /* Compute margin and relief to draw. */
11510 relief = (tool_bar_button_relief >= 0
11511 ? tool_bar_button_relief
11512 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11513 hmargin = vmargin = relief;
11514
11515 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11516 INT_MAX - max (hmargin, vmargin)))
11517 {
11518 hmargin += XFASTINT (Vtool_bar_button_margin);
11519 vmargin += XFASTINT (Vtool_bar_button_margin);
11520 }
11521 else if (CONSP (Vtool_bar_button_margin))
11522 {
11523 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11524 INT_MAX - hmargin))
11525 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11526
11527 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11528 INT_MAX - vmargin))
11529 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11530 }
11531
11532 if (auto_raise_tool_bar_buttons_p)
11533 {
11534 /* Add a `:relief' property to the image spec if the item is
11535 selected. */
11536 if (selected_p)
11537 {
11538 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11539 hmargin -= relief;
11540 vmargin -= relief;
11541 }
11542 }
11543 else
11544 {
11545 /* If image is selected, display it pressed, i.e. with a
11546 negative relief. If it's not selected, display it with a
11547 raised relief. */
11548 plist = Fplist_put (plist, QCrelief,
11549 (selected_p
11550 ? make_number (-relief)
11551 : make_number (relief)));
11552 hmargin -= relief;
11553 vmargin -= relief;
11554 }
11555
11556 /* Put a margin around the image. */
11557 if (hmargin || vmargin)
11558 {
11559 if (hmargin == vmargin)
11560 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11561 else
11562 plist = Fplist_put (plist, QCmargin,
11563 Fcons (make_number (hmargin),
11564 make_number (vmargin)));
11565 }
11566
11567 /* If button is not enabled, and we don't have special images
11568 for the disabled state, make the image appear disabled by
11569 applying an appropriate algorithm to it. */
11570 if (!enabled_p && idx < 0)
11571 plist = Fplist_put (plist, QCconversion, Qdisabled);
11572
11573 /* Put a `display' text property on the string for the image to
11574 display. Put a `menu-item' property on the string that gives
11575 the start of this item's properties in the tool-bar items
11576 vector. */
11577 image = Fcons (Qimage, plist);
11578 props = list4 (Qdisplay, image,
11579 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11580
11581 /* Let the last image hide all remaining spaces in the tool bar
11582 string. The string can be longer than needed when we reuse a
11583 previous string. */
11584 if (i + 1 == f->n_tool_bar_items)
11585 end = SCHARS (f->desired_tool_bar_string);
11586 else
11587 end = i + 1;
11588 Fadd_text_properties (make_number (i), make_number (end),
11589 props, f->desired_tool_bar_string);
11590 #undef PROP
11591 }
11592
11593 UNGCPRO;
11594 }
11595
11596
11597 /* Display one line of the tool-bar of frame IT->f.
11598
11599 HEIGHT specifies the desired height of the tool-bar line.
11600 If the actual height of the glyph row is less than HEIGHT, the
11601 row's height is increased to HEIGHT, and the icons are centered
11602 vertically in the new height.
11603
11604 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11605 count a final empty row in case the tool-bar width exactly matches
11606 the window width.
11607 */
11608
11609 static void
11610 display_tool_bar_line (struct it *it, int height)
11611 {
11612 struct glyph_row *row = it->glyph_row;
11613 int max_x = it->last_visible_x;
11614 struct glyph *last;
11615
11616 prepare_desired_row (row);
11617 row->y = it->current_y;
11618
11619 /* Note that this isn't made use of if the face hasn't a box,
11620 so there's no need to check the face here. */
11621 it->start_of_box_run_p = 1;
11622
11623 while (it->current_x < max_x)
11624 {
11625 int x, n_glyphs_before, i, nglyphs;
11626 struct it it_before;
11627
11628 /* Get the next display element. */
11629 if (!get_next_display_element (it))
11630 {
11631 /* Don't count empty row if we are counting needed tool-bar lines. */
11632 if (height < 0 && !it->hpos)
11633 return;
11634 break;
11635 }
11636
11637 /* Produce glyphs. */
11638 n_glyphs_before = row->used[TEXT_AREA];
11639 it_before = *it;
11640
11641 PRODUCE_GLYPHS (it);
11642
11643 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11644 i = 0;
11645 x = it_before.current_x;
11646 while (i < nglyphs)
11647 {
11648 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11649
11650 if (x + glyph->pixel_width > max_x)
11651 {
11652 /* Glyph doesn't fit on line. Backtrack. */
11653 row->used[TEXT_AREA] = n_glyphs_before;
11654 *it = it_before;
11655 /* If this is the only glyph on this line, it will never fit on the
11656 tool-bar, so skip it. But ensure there is at least one glyph,
11657 so we don't accidentally disable the tool-bar. */
11658 if (n_glyphs_before == 0
11659 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11660 break;
11661 goto out;
11662 }
11663
11664 ++it->hpos;
11665 x += glyph->pixel_width;
11666 ++i;
11667 }
11668
11669 /* Stop at line end. */
11670 if (ITERATOR_AT_END_OF_LINE_P (it))
11671 break;
11672
11673 set_iterator_to_next (it, 1);
11674 }
11675
11676 out:;
11677
11678 row->displays_text_p = row->used[TEXT_AREA] != 0;
11679
11680 /* Use default face for the border below the tool bar.
11681
11682 FIXME: When auto-resize-tool-bars is grow-only, there is
11683 no additional border below the possibly empty tool-bar lines.
11684 So to make the extra empty lines look "normal", we have to
11685 use the tool-bar face for the border too. */
11686 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11687 it->face_id = DEFAULT_FACE_ID;
11688
11689 extend_face_to_end_of_line (it);
11690 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11691 last->right_box_line_p = 1;
11692 if (last == row->glyphs[TEXT_AREA])
11693 last->left_box_line_p = 1;
11694
11695 /* Make line the desired height and center it vertically. */
11696 if ((height -= it->max_ascent + it->max_descent) > 0)
11697 {
11698 /* Don't add more than one line height. */
11699 height %= FRAME_LINE_HEIGHT (it->f);
11700 it->max_ascent += height / 2;
11701 it->max_descent += (height + 1) / 2;
11702 }
11703
11704 compute_line_metrics (it);
11705
11706 /* If line is empty, make it occupy the rest of the tool-bar. */
11707 if (!row->displays_text_p)
11708 {
11709 row->height = row->phys_height = it->last_visible_y - row->y;
11710 row->visible_height = row->height;
11711 row->ascent = row->phys_ascent = 0;
11712 row->extra_line_spacing = 0;
11713 }
11714
11715 row->full_width_p = 1;
11716 row->continued_p = 0;
11717 row->truncated_on_left_p = 0;
11718 row->truncated_on_right_p = 0;
11719
11720 it->current_x = it->hpos = 0;
11721 it->current_y += row->height;
11722 ++it->vpos;
11723 ++it->glyph_row;
11724 }
11725
11726
11727 /* Max tool-bar height. */
11728
11729 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11730 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11731
11732 /* Value is the number of screen lines needed to make all tool-bar
11733 items of frame F visible. The number of actual rows needed is
11734 returned in *N_ROWS if non-NULL. */
11735
11736 static int
11737 tool_bar_lines_needed (struct frame *f, int *n_rows)
11738 {
11739 struct window *w = XWINDOW (f->tool_bar_window);
11740 struct it it;
11741 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11742 the desired matrix, so use (unused) mode-line row as temporary row to
11743 avoid destroying the first tool-bar row. */
11744 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11745
11746 /* Initialize an iterator for iteration over
11747 F->desired_tool_bar_string in the tool-bar window of frame F. */
11748 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11749 it.first_visible_x = 0;
11750 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11751 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11752 it.paragraph_embedding = L2R;
11753
11754 while (!ITERATOR_AT_END_P (&it))
11755 {
11756 clear_glyph_row (temp_row);
11757 it.glyph_row = temp_row;
11758 display_tool_bar_line (&it, -1);
11759 }
11760 clear_glyph_row (temp_row);
11761
11762 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11763 if (n_rows)
11764 *n_rows = it.vpos > 0 ? it.vpos : -1;
11765
11766 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11767 }
11768
11769
11770 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11771 0, 1, 0,
11772 doc: /* Return the number of lines occupied by the tool bar of FRAME. */)
11773 (Lisp_Object frame)
11774 {
11775 struct frame *f;
11776 struct window *w;
11777 int nlines = 0;
11778
11779 if (NILP (frame))
11780 frame = selected_frame;
11781 else
11782 CHECK_FRAME (frame);
11783 f = XFRAME (frame);
11784
11785 if (WINDOWP (f->tool_bar_window)
11786 && (w = XWINDOW (f->tool_bar_window),
11787 WINDOW_TOTAL_LINES (w) > 0))
11788 {
11789 update_tool_bar (f, 1);
11790 if (f->n_tool_bar_items)
11791 {
11792 build_desired_tool_bar_string (f);
11793 nlines = tool_bar_lines_needed (f, NULL);
11794 }
11795 }
11796
11797 return make_number (nlines);
11798 }
11799
11800
11801 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11802 height should be changed. */
11803
11804 static int
11805 redisplay_tool_bar (struct frame *f)
11806 {
11807 struct window *w;
11808 struct it it;
11809 struct glyph_row *row;
11810
11811 #if defined (USE_GTK) || defined (HAVE_NS)
11812 if (FRAME_EXTERNAL_TOOL_BAR (f))
11813 update_frame_tool_bar (f);
11814 return 0;
11815 #endif
11816
11817 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11818 do anything. This means you must start with tool-bar-lines
11819 non-zero to get the auto-sizing effect. Or in other words, you
11820 can turn off tool-bars by specifying tool-bar-lines zero. */
11821 if (!WINDOWP (f->tool_bar_window)
11822 || (w = XWINDOW (f->tool_bar_window),
11823 WINDOW_TOTAL_LINES (w) == 0))
11824 return 0;
11825
11826 /* Set up an iterator for the tool-bar window. */
11827 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11828 it.first_visible_x = 0;
11829 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11830 row = it.glyph_row;
11831
11832 /* Build a string that represents the contents of the tool-bar. */
11833 build_desired_tool_bar_string (f);
11834 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11835 /* FIXME: This should be controlled by a user option. But it
11836 doesn't make sense to have an R2L tool bar if the menu bar cannot
11837 be drawn also R2L, and making the menu bar R2L is tricky due
11838 toolkit-specific code that implements it. If an R2L tool bar is
11839 ever supported, display_tool_bar_line should also be augmented to
11840 call unproduce_glyphs like display_line and display_string
11841 do. */
11842 it.paragraph_embedding = L2R;
11843
11844 if (f->n_tool_bar_rows == 0)
11845 {
11846 int nlines;
11847
11848 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11849 nlines != WINDOW_TOTAL_LINES (w)))
11850 {
11851 Lisp_Object frame;
11852 int old_height = WINDOW_TOTAL_LINES (w);
11853
11854 XSETFRAME (frame, f);
11855 Fmodify_frame_parameters (frame,
11856 Fcons (Fcons (Qtool_bar_lines,
11857 make_number (nlines)),
11858 Qnil));
11859 if (WINDOW_TOTAL_LINES (w) != old_height)
11860 {
11861 clear_glyph_matrix (w->desired_matrix);
11862 fonts_changed_p = 1;
11863 return 1;
11864 }
11865 }
11866 }
11867
11868 /* Display as many lines as needed to display all tool-bar items. */
11869
11870 if (f->n_tool_bar_rows > 0)
11871 {
11872 int border, rows, height, extra;
11873
11874 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11875 border = XINT (Vtool_bar_border);
11876 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11877 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11878 else if (EQ (Vtool_bar_border, Qborder_width))
11879 border = f->border_width;
11880 else
11881 border = 0;
11882 if (border < 0)
11883 border = 0;
11884
11885 rows = f->n_tool_bar_rows;
11886 height = max (1, (it.last_visible_y - border) / rows);
11887 extra = it.last_visible_y - border - height * rows;
11888
11889 while (it.current_y < it.last_visible_y)
11890 {
11891 int h = 0;
11892 if (extra > 0 && rows-- > 0)
11893 {
11894 h = (extra + rows - 1) / rows;
11895 extra -= h;
11896 }
11897 display_tool_bar_line (&it, height + h);
11898 }
11899 }
11900 else
11901 {
11902 while (it.current_y < it.last_visible_y)
11903 display_tool_bar_line (&it, 0);
11904 }
11905
11906 /* It doesn't make much sense to try scrolling in the tool-bar
11907 window, so don't do it. */
11908 w->desired_matrix->no_scrolling_p = 1;
11909 w->must_be_updated_p = 1;
11910
11911 if (!NILP (Vauto_resize_tool_bars))
11912 {
11913 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11914 int change_height_p = 0;
11915
11916 /* If we couldn't display everything, change the tool-bar's
11917 height if there is room for more. */
11918 if (IT_STRING_CHARPOS (it) < it.end_charpos
11919 && it.current_y < max_tool_bar_height)
11920 change_height_p = 1;
11921
11922 row = it.glyph_row - 1;
11923
11924 /* If there are blank lines at the end, except for a partially
11925 visible blank line at the end that is smaller than
11926 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11927 if (!row->displays_text_p
11928 && row->height >= FRAME_LINE_HEIGHT (f))
11929 change_height_p = 1;
11930
11931 /* If row displays tool-bar items, but is partially visible,
11932 change the tool-bar's height. */
11933 if (row->displays_text_p
11934 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11935 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11936 change_height_p = 1;
11937
11938 /* Resize windows as needed by changing the `tool-bar-lines'
11939 frame parameter. */
11940 if (change_height_p)
11941 {
11942 Lisp_Object frame;
11943 int old_height = WINDOW_TOTAL_LINES (w);
11944 int nrows;
11945 int nlines = tool_bar_lines_needed (f, &nrows);
11946
11947 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11948 && !f->minimize_tool_bar_window_p)
11949 ? (nlines > old_height)
11950 : (nlines != old_height));
11951 f->minimize_tool_bar_window_p = 0;
11952
11953 if (change_height_p)
11954 {
11955 XSETFRAME (frame, f);
11956 Fmodify_frame_parameters (frame,
11957 Fcons (Fcons (Qtool_bar_lines,
11958 make_number (nlines)),
11959 Qnil));
11960 if (WINDOW_TOTAL_LINES (w) != old_height)
11961 {
11962 clear_glyph_matrix (w->desired_matrix);
11963 f->n_tool_bar_rows = nrows;
11964 fonts_changed_p = 1;
11965 return 1;
11966 }
11967 }
11968 }
11969 }
11970
11971 f->minimize_tool_bar_window_p = 0;
11972 return 0;
11973 }
11974
11975
11976 /* Get information about the tool-bar item which is displayed in GLYPH
11977 on frame F. Return in *PROP_IDX the index where tool-bar item
11978 properties start in F->tool_bar_items. Value is zero if
11979 GLYPH doesn't display a tool-bar item. */
11980
11981 static int
11982 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
11983 {
11984 Lisp_Object prop;
11985 int success_p;
11986 int charpos;
11987
11988 /* This function can be called asynchronously, which means we must
11989 exclude any possibility that Fget_text_property signals an
11990 error. */
11991 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
11992 charpos = max (0, charpos);
11993
11994 /* Get the text property `menu-item' at pos. The value of that
11995 property is the start index of this item's properties in
11996 F->tool_bar_items. */
11997 prop = Fget_text_property (make_number (charpos),
11998 Qmenu_item, f->current_tool_bar_string);
11999 if (INTEGERP (prop))
12000 {
12001 *prop_idx = XINT (prop);
12002 success_p = 1;
12003 }
12004 else
12005 success_p = 0;
12006
12007 return success_p;
12008 }
12009
12010 \f
12011 /* Get information about the tool-bar item at position X/Y on frame F.
12012 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12013 the current matrix of the tool-bar window of F, or NULL if not
12014 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12015 item in F->tool_bar_items. Value is
12016
12017 -1 if X/Y is not on a tool-bar item
12018 0 if X/Y is on the same item that was highlighted before.
12019 1 otherwise. */
12020
12021 static int
12022 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12023 int *hpos, int *vpos, int *prop_idx)
12024 {
12025 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12026 struct window *w = XWINDOW (f->tool_bar_window);
12027 int area;
12028
12029 /* Find the glyph under X/Y. */
12030 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12031 if (*glyph == NULL)
12032 return -1;
12033
12034 /* Get the start of this tool-bar item's properties in
12035 f->tool_bar_items. */
12036 if (!tool_bar_item_info (f, *glyph, prop_idx))
12037 return -1;
12038
12039 /* Is mouse on the highlighted item? */
12040 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12041 && *vpos >= hlinfo->mouse_face_beg_row
12042 && *vpos <= hlinfo->mouse_face_end_row
12043 && (*vpos > hlinfo->mouse_face_beg_row
12044 || *hpos >= hlinfo->mouse_face_beg_col)
12045 && (*vpos < hlinfo->mouse_face_end_row
12046 || *hpos < hlinfo->mouse_face_end_col
12047 || hlinfo->mouse_face_past_end))
12048 return 0;
12049
12050 return 1;
12051 }
12052
12053
12054 /* EXPORT:
12055 Handle mouse button event on the tool-bar of frame F, at
12056 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12057 0 for button release. MODIFIERS is event modifiers for button
12058 release. */
12059
12060 void
12061 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12062 int modifiers)
12063 {
12064 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12065 struct window *w = XWINDOW (f->tool_bar_window);
12066 int hpos, vpos, prop_idx;
12067 struct glyph *glyph;
12068 Lisp_Object enabled_p;
12069
12070 /* If not on the highlighted tool-bar item, return. */
12071 frame_to_window_pixel_xy (w, &x, &y);
12072 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12073 return;
12074
12075 /* If item is disabled, do nothing. */
12076 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12077 if (NILP (enabled_p))
12078 return;
12079
12080 if (down_p)
12081 {
12082 /* Show item in pressed state. */
12083 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12084 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12085 last_tool_bar_item = prop_idx;
12086 }
12087 else
12088 {
12089 Lisp_Object key, frame;
12090 struct input_event event;
12091 EVENT_INIT (event);
12092
12093 /* Show item in released state. */
12094 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12095 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12096
12097 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12098
12099 XSETFRAME (frame, f);
12100 event.kind = TOOL_BAR_EVENT;
12101 event.frame_or_window = frame;
12102 event.arg = frame;
12103 kbd_buffer_store_event (&event);
12104
12105 event.kind = TOOL_BAR_EVENT;
12106 event.frame_or_window = frame;
12107 event.arg = key;
12108 event.modifiers = modifiers;
12109 kbd_buffer_store_event (&event);
12110 last_tool_bar_item = -1;
12111 }
12112 }
12113
12114
12115 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12116 tool-bar window-relative coordinates X/Y. Called from
12117 note_mouse_highlight. */
12118
12119 static void
12120 note_tool_bar_highlight (struct frame *f, int x, int y)
12121 {
12122 Lisp_Object window = f->tool_bar_window;
12123 struct window *w = XWINDOW (window);
12124 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12125 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12126 int hpos, vpos;
12127 struct glyph *glyph;
12128 struct glyph_row *row;
12129 int i;
12130 Lisp_Object enabled_p;
12131 int prop_idx;
12132 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12133 int mouse_down_p, rc;
12134
12135 /* Function note_mouse_highlight is called with negative X/Y
12136 values when mouse moves outside of the frame. */
12137 if (x <= 0 || y <= 0)
12138 {
12139 clear_mouse_face (hlinfo);
12140 return;
12141 }
12142
12143 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12144 if (rc < 0)
12145 {
12146 /* Not on tool-bar item. */
12147 clear_mouse_face (hlinfo);
12148 return;
12149 }
12150 else if (rc == 0)
12151 /* On same tool-bar item as before. */
12152 goto set_help_echo;
12153
12154 clear_mouse_face (hlinfo);
12155
12156 /* Mouse is down, but on different tool-bar item? */
12157 mouse_down_p = (dpyinfo->grabbed
12158 && f == last_mouse_frame
12159 && FRAME_LIVE_P (f));
12160 if (mouse_down_p
12161 && last_tool_bar_item != prop_idx)
12162 return;
12163
12164 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12165 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12166
12167 /* If tool-bar item is not enabled, don't highlight it. */
12168 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12169 if (!NILP (enabled_p))
12170 {
12171 /* Compute the x-position of the glyph. In front and past the
12172 image is a space. We include this in the highlighted area. */
12173 row = MATRIX_ROW (w->current_matrix, vpos);
12174 for (i = x = 0; i < hpos; ++i)
12175 x += row->glyphs[TEXT_AREA][i].pixel_width;
12176
12177 /* Record this as the current active region. */
12178 hlinfo->mouse_face_beg_col = hpos;
12179 hlinfo->mouse_face_beg_row = vpos;
12180 hlinfo->mouse_face_beg_x = x;
12181 hlinfo->mouse_face_beg_y = row->y;
12182 hlinfo->mouse_face_past_end = 0;
12183
12184 hlinfo->mouse_face_end_col = hpos + 1;
12185 hlinfo->mouse_face_end_row = vpos;
12186 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12187 hlinfo->mouse_face_end_y = row->y;
12188 hlinfo->mouse_face_window = window;
12189 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12190
12191 /* Display it as active. */
12192 show_mouse_face (hlinfo, draw);
12193 hlinfo->mouse_face_image_state = draw;
12194 }
12195
12196 set_help_echo:
12197
12198 /* Set help_echo_string to a help string to display for this tool-bar item.
12199 XTread_socket does the rest. */
12200 help_echo_object = help_echo_window = Qnil;
12201 help_echo_pos = -1;
12202 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12203 if (NILP (help_echo_string))
12204 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12205 }
12206
12207 #endif /* HAVE_WINDOW_SYSTEM */
12208
12209
12210 \f
12211 /************************************************************************
12212 Horizontal scrolling
12213 ************************************************************************/
12214
12215 static int hscroll_window_tree (Lisp_Object);
12216 static int hscroll_windows (Lisp_Object);
12217
12218 /* For all leaf windows in the window tree rooted at WINDOW, set their
12219 hscroll value so that PT is (i) visible in the window, and (ii) so
12220 that it is not within a certain margin at the window's left and
12221 right border. Value is non-zero if any window's hscroll has been
12222 changed. */
12223
12224 static int
12225 hscroll_window_tree (Lisp_Object window)
12226 {
12227 int hscrolled_p = 0;
12228 int hscroll_relative_p = FLOATP (Vhscroll_step);
12229 int hscroll_step_abs = 0;
12230 double hscroll_step_rel = 0;
12231
12232 if (hscroll_relative_p)
12233 {
12234 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12235 if (hscroll_step_rel < 0)
12236 {
12237 hscroll_relative_p = 0;
12238 hscroll_step_abs = 0;
12239 }
12240 }
12241 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12242 {
12243 hscroll_step_abs = XINT (Vhscroll_step);
12244 if (hscroll_step_abs < 0)
12245 hscroll_step_abs = 0;
12246 }
12247 else
12248 hscroll_step_abs = 0;
12249
12250 while (WINDOWP (window))
12251 {
12252 struct window *w = XWINDOW (window);
12253
12254 if (WINDOWP (w->hchild))
12255 hscrolled_p |= hscroll_window_tree (w->hchild);
12256 else if (WINDOWP (w->vchild))
12257 hscrolled_p |= hscroll_window_tree (w->vchild);
12258 else if (w->cursor.vpos >= 0)
12259 {
12260 int h_margin;
12261 int text_area_width;
12262 struct glyph_row *current_cursor_row
12263 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12264 struct glyph_row *desired_cursor_row
12265 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12266 struct glyph_row *cursor_row
12267 = (desired_cursor_row->enabled_p
12268 ? desired_cursor_row
12269 : current_cursor_row);
12270 int row_r2l_p = cursor_row->reversed_p;
12271
12272 text_area_width = window_box_width (w, TEXT_AREA);
12273
12274 /* Scroll when cursor is inside this scroll margin. */
12275 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12276
12277 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12278 /* For left-to-right rows, hscroll when cursor is either
12279 (i) inside the right hscroll margin, or (ii) if it is
12280 inside the left margin and the window is already
12281 hscrolled. */
12282 && ((!row_r2l_p
12283 && ((XFASTINT (w->hscroll)
12284 && w->cursor.x <= h_margin)
12285 || (cursor_row->enabled_p
12286 && cursor_row->truncated_on_right_p
12287 && (w->cursor.x >= text_area_width - h_margin))))
12288 /* For right-to-left rows, the logic is similar,
12289 except that rules for scrolling to left and right
12290 are reversed. E.g., if cursor.x <= h_margin, we
12291 need to hscroll "to the right" unconditionally,
12292 and that will scroll the screen to the left so as
12293 to reveal the next portion of the row. */
12294 || (row_r2l_p
12295 && ((cursor_row->enabled_p
12296 /* FIXME: It is confusing to set the
12297 truncated_on_right_p flag when R2L rows
12298 are actually truncated on the left. */
12299 && cursor_row->truncated_on_right_p
12300 && w->cursor.x <= h_margin)
12301 || (XFASTINT (w->hscroll)
12302 && (w->cursor.x >= text_area_width - h_margin))))))
12303 {
12304 struct it it;
12305 ptrdiff_t hscroll;
12306 struct buffer *saved_current_buffer;
12307 ptrdiff_t pt;
12308 int wanted_x;
12309
12310 /* Find point in a display of infinite width. */
12311 saved_current_buffer = current_buffer;
12312 current_buffer = XBUFFER (w->buffer);
12313
12314 if (w == XWINDOW (selected_window))
12315 pt = PT;
12316 else
12317 {
12318 pt = marker_position (w->pointm);
12319 pt = max (BEGV, pt);
12320 pt = min (ZV, pt);
12321 }
12322
12323 /* Move iterator to pt starting at cursor_row->start in
12324 a line with infinite width. */
12325 init_to_row_start (&it, w, cursor_row);
12326 it.last_visible_x = INFINITY;
12327 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12328 current_buffer = saved_current_buffer;
12329
12330 /* Position cursor in window. */
12331 if (!hscroll_relative_p && hscroll_step_abs == 0)
12332 hscroll = max (0, (it.current_x
12333 - (ITERATOR_AT_END_OF_LINE_P (&it)
12334 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12335 : (text_area_width / 2))))
12336 / FRAME_COLUMN_WIDTH (it.f);
12337 else if ((!row_r2l_p
12338 && w->cursor.x >= text_area_width - h_margin)
12339 || (row_r2l_p && w->cursor.x <= h_margin))
12340 {
12341 if (hscroll_relative_p)
12342 wanted_x = text_area_width * (1 - hscroll_step_rel)
12343 - h_margin;
12344 else
12345 wanted_x = text_area_width
12346 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12347 - h_margin;
12348 hscroll
12349 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12350 }
12351 else
12352 {
12353 if (hscroll_relative_p)
12354 wanted_x = text_area_width * hscroll_step_rel
12355 + h_margin;
12356 else
12357 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12358 + h_margin;
12359 hscroll
12360 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12361 }
12362 hscroll = max (hscroll, XFASTINT (w->min_hscroll));
12363
12364 /* Don't prevent redisplay optimizations if hscroll
12365 hasn't changed, as it will unnecessarily slow down
12366 redisplay. */
12367 if (XFASTINT (w->hscroll) != hscroll)
12368 {
12369 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12370 w->hscroll = make_number (hscroll);
12371 hscrolled_p = 1;
12372 }
12373 }
12374 }
12375
12376 window = w->next;
12377 }
12378
12379 /* Value is non-zero if hscroll of any leaf window has been changed. */
12380 return hscrolled_p;
12381 }
12382
12383
12384 /* Set hscroll so that cursor is visible and not inside horizontal
12385 scroll margins for all windows in the tree rooted at WINDOW. See
12386 also hscroll_window_tree above. Value is non-zero if any window's
12387 hscroll has been changed. If it has, desired matrices on the frame
12388 of WINDOW are cleared. */
12389
12390 static int
12391 hscroll_windows (Lisp_Object window)
12392 {
12393 int hscrolled_p = hscroll_window_tree (window);
12394 if (hscrolled_p)
12395 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12396 return hscrolled_p;
12397 }
12398
12399
12400 \f
12401 /************************************************************************
12402 Redisplay
12403 ************************************************************************/
12404
12405 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12406 to a non-zero value. This is sometimes handy to have in a debugger
12407 session. */
12408
12409 #if GLYPH_DEBUG
12410
12411 /* First and last unchanged row for try_window_id. */
12412
12413 static int debug_first_unchanged_at_end_vpos;
12414 static int debug_last_unchanged_at_beg_vpos;
12415
12416 /* Delta vpos and y. */
12417
12418 static int debug_dvpos, debug_dy;
12419
12420 /* Delta in characters and bytes for try_window_id. */
12421
12422 static ptrdiff_t debug_delta, debug_delta_bytes;
12423
12424 /* Values of window_end_pos and window_end_vpos at the end of
12425 try_window_id. */
12426
12427 static ptrdiff_t debug_end_vpos;
12428
12429 /* Append a string to W->desired_matrix->method. FMT is a printf
12430 format string. If trace_redisplay_p is non-zero also printf the
12431 resulting string to stderr. */
12432
12433 static void debug_method_add (struct window *, char const *, ...)
12434 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12435
12436 static void
12437 debug_method_add (struct window *w, char const *fmt, ...)
12438 {
12439 char buffer[512];
12440 char *method = w->desired_matrix->method;
12441 int len = strlen (method);
12442 int size = sizeof w->desired_matrix->method;
12443 int remaining = size - len - 1;
12444 va_list ap;
12445
12446 va_start (ap, fmt);
12447 vsprintf (buffer, fmt, ap);
12448 va_end (ap);
12449 if (len && remaining)
12450 {
12451 method[len] = '|';
12452 --remaining, ++len;
12453 }
12454
12455 strncpy (method + len, buffer, remaining);
12456
12457 if (trace_redisplay_p)
12458 fprintf (stderr, "%p (%s): %s\n",
12459 w,
12460 ((BUFFERP (w->buffer)
12461 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12462 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12463 : "no buffer"),
12464 buffer);
12465 }
12466
12467 #endif /* GLYPH_DEBUG */
12468
12469
12470 /* Value is non-zero if all changes in window W, which displays
12471 current_buffer, are in the text between START and END. START is a
12472 buffer position, END is given as a distance from Z. Used in
12473 redisplay_internal for display optimization. */
12474
12475 static inline int
12476 text_outside_line_unchanged_p (struct window *w,
12477 ptrdiff_t start, ptrdiff_t end)
12478 {
12479 int unchanged_p = 1;
12480
12481 /* If text or overlays have changed, see where. */
12482 if (XFASTINT (w->last_modified) < MODIFF
12483 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
12484 {
12485 /* Gap in the line? */
12486 if (GPT < start || Z - GPT < end)
12487 unchanged_p = 0;
12488
12489 /* Changes start in front of the line, or end after it? */
12490 if (unchanged_p
12491 && (BEG_UNCHANGED < start - 1
12492 || END_UNCHANGED < end))
12493 unchanged_p = 0;
12494
12495 /* If selective display, can't optimize if changes start at the
12496 beginning of the line. */
12497 if (unchanged_p
12498 && INTEGERP (BVAR (current_buffer, selective_display))
12499 && XINT (BVAR (current_buffer, selective_display)) > 0
12500 && (BEG_UNCHANGED < start || GPT <= start))
12501 unchanged_p = 0;
12502
12503 /* If there are overlays at the start or end of the line, these
12504 may have overlay strings with newlines in them. A change at
12505 START, for instance, may actually concern the display of such
12506 overlay strings as well, and they are displayed on different
12507 lines. So, quickly rule out this case. (For the future, it
12508 might be desirable to implement something more telling than
12509 just BEG/END_UNCHANGED.) */
12510 if (unchanged_p)
12511 {
12512 if (BEG + BEG_UNCHANGED == start
12513 && overlay_touches_p (start))
12514 unchanged_p = 0;
12515 if (END_UNCHANGED == end
12516 && overlay_touches_p (Z - end))
12517 unchanged_p = 0;
12518 }
12519
12520 /* Under bidi reordering, adding or deleting a character in the
12521 beginning of a paragraph, before the first strong directional
12522 character, can change the base direction of the paragraph (unless
12523 the buffer specifies a fixed paragraph direction), which will
12524 require to redisplay the whole paragraph. It might be worthwhile
12525 to find the paragraph limits and widen the range of redisplayed
12526 lines to that, but for now just give up this optimization. */
12527 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12528 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12529 unchanged_p = 0;
12530 }
12531
12532 return unchanged_p;
12533 }
12534
12535
12536 /* Do a frame update, taking possible shortcuts into account. This is
12537 the main external entry point for redisplay.
12538
12539 If the last redisplay displayed an echo area message and that message
12540 is no longer requested, we clear the echo area or bring back the
12541 mini-buffer if that is in use. */
12542
12543 void
12544 redisplay (void)
12545 {
12546 redisplay_internal ();
12547 }
12548
12549
12550 static Lisp_Object
12551 overlay_arrow_string_or_property (Lisp_Object var)
12552 {
12553 Lisp_Object val;
12554
12555 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12556 return val;
12557
12558 return Voverlay_arrow_string;
12559 }
12560
12561 /* Return 1 if there are any overlay-arrows in current_buffer. */
12562 static int
12563 overlay_arrow_in_current_buffer_p (void)
12564 {
12565 Lisp_Object vlist;
12566
12567 for (vlist = Voverlay_arrow_variable_list;
12568 CONSP (vlist);
12569 vlist = XCDR (vlist))
12570 {
12571 Lisp_Object var = XCAR (vlist);
12572 Lisp_Object val;
12573
12574 if (!SYMBOLP (var))
12575 continue;
12576 val = find_symbol_value (var);
12577 if (MARKERP (val)
12578 && current_buffer == XMARKER (val)->buffer)
12579 return 1;
12580 }
12581 return 0;
12582 }
12583
12584
12585 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12586 has changed. */
12587
12588 static int
12589 overlay_arrows_changed_p (void)
12590 {
12591 Lisp_Object vlist;
12592
12593 for (vlist = Voverlay_arrow_variable_list;
12594 CONSP (vlist);
12595 vlist = XCDR (vlist))
12596 {
12597 Lisp_Object var = XCAR (vlist);
12598 Lisp_Object val, pstr;
12599
12600 if (!SYMBOLP (var))
12601 continue;
12602 val = find_symbol_value (var);
12603 if (!MARKERP (val))
12604 continue;
12605 if (! EQ (COERCE_MARKER (val),
12606 Fget (var, Qlast_arrow_position))
12607 || ! (pstr = overlay_arrow_string_or_property (var),
12608 EQ (pstr, Fget (var, Qlast_arrow_string))))
12609 return 1;
12610 }
12611 return 0;
12612 }
12613
12614 /* Mark overlay arrows to be updated on next redisplay. */
12615
12616 static void
12617 update_overlay_arrows (int up_to_date)
12618 {
12619 Lisp_Object vlist;
12620
12621 for (vlist = Voverlay_arrow_variable_list;
12622 CONSP (vlist);
12623 vlist = XCDR (vlist))
12624 {
12625 Lisp_Object var = XCAR (vlist);
12626
12627 if (!SYMBOLP (var))
12628 continue;
12629
12630 if (up_to_date > 0)
12631 {
12632 Lisp_Object val = find_symbol_value (var);
12633 Fput (var, Qlast_arrow_position,
12634 COERCE_MARKER (val));
12635 Fput (var, Qlast_arrow_string,
12636 overlay_arrow_string_or_property (var));
12637 }
12638 else if (up_to_date < 0
12639 || !NILP (Fget (var, Qlast_arrow_position)))
12640 {
12641 Fput (var, Qlast_arrow_position, Qt);
12642 Fput (var, Qlast_arrow_string, Qt);
12643 }
12644 }
12645 }
12646
12647
12648 /* Return overlay arrow string to display at row.
12649 Return integer (bitmap number) for arrow bitmap in left fringe.
12650 Return nil if no overlay arrow. */
12651
12652 static Lisp_Object
12653 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12654 {
12655 Lisp_Object vlist;
12656
12657 for (vlist = Voverlay_arrow_variable_list;
12658 CONSP (vlist);
12659 vlist = XCDR (vlist))
12660 {
12661 Lisp_Object var = XCAR (vlist);
12662 Lisp_Object val;
12663
12664 if (!SYMBOLP (var))
12665 continue;
12666
12667 val = find_symbol_value (var);
12668
12669 if (MARKERP (val)
12670 && current_buffer == XMARKER (val)->buffer
12671 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12672 {
12673 if (FRAME_WINDOW_P (it->f)
12674 /* FIXME: if ROW->reversed_p is set, this should test
12675 the right fringe, not the left one. */
12676 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12677 {
12678 #ifdef HAVE_WINDOW_SYSTEM
12679 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12680 {
12681 int fringe_bitmap;
12682 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12683 return make_number (fringe_bitmap);
12684 }
12685 #endif
12686 return make_number (-1); /* Use default arrow bitmap */
12687 }
12688 return overlay_arrow_string_or_property (var);
12689 }
12690 }
12691
12692 return Qnil;
12693 }
12694
12695 /* Return 1 if point moved out of or into a composition. Otherwise
12696 return 0. PREV_BUF and PREV_PT are the last point buffer and
12697 position. BUF and PT are the current point buffer and position. */
12698
12699 static int
12700 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12701 struct buffer *buf, ptrdiff_t pt)
12702 {
12703 ptrdiff_t start, end;
12704 Lisp_Object prop;
12705 Lisp_Object buffer;
12706
12707 XSETBUFFER (buffer, buf);
12708 /* Check a composition at the last point if point moved within the
12709 same buffer. */
12710 if (prev_buf == buf)
12711 {
12712 if (prev_pt == pt)
12713 /* Point didn't move. */
12714 return 0;
12715
12716 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12717 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12718 && COMPOSITION_VALID_P (start, end, prop)
12719 && start < prev_pt && end > prev_pt)
12720 /* The last point was within the composition. Return 1 iff
12721 point moved out of the composition. */
12722 return (pt <= start || pt >= end);
12723 }
12724
12725 /* Check a composition at the current point. */
12726 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12727 && find_composition (pt, -1, &start, &end, &prop, buffer)
12728 && COMPOSITION_VALID_P (start, end, prop)
12729 && start < pt && end > pt);
12730 }
12731
12732
12733 /* Reconsider the setting of B->clip_changed which is displayed
12734 in window W. */
12735
12736 static inline void
12737 reconsider_clip_changes (struct window *w, struct buffer *b)
12738 {
12739 if (b->clip_changed
12740 && !NILP (w->window_end_valid)
12741 && w->current_matrix->buffer == b
12742 && w->current_matrix->zv == BUF_ZV (b)
12743 && w->current_matrix->begv == BUF_BEGV (b))
12744 b->clip_changed = 0;
12745
12746 /* If display wasn't paused, and W is not a tool bar window, see if
12747 point has been moved into or out of a composition. In that case,
12748 we set b->clip_changed to 1 to force updating the screen. If
12749 b->clip_changed has already been set to 1, we can skip this
12750 check. */
12751 if (!b->clip_changed
12752 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12753 {
12754 ptrdiff_t pt;
12755
12756 if (w == XWINDOW (selected_window))
12757 pt = PT;
12758 else
12759 pt = marker_position (w->pointm);
12760
12761 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12762 || pt != XINT (w->last_point))
12763 && check_point_in_composition (w->current_matrix->buffer,
12764 XINT (w->last_point),
12765 XBUFFER (w->buffer), pt))
12766 b->clip_changed = 1;
12767 }
12768 }
12769 \f
12770
12771 /* Select FRAME to forward the values of frame-local variables into C
12772 variables so that the redisplay routines can access those values
12773 directly. */
12774
12775 static void
12776 select_frame_for_redisplay (Lisp_Object frame)
12777 {
12778 Lisp_Object tail, tem;
12779 Lisp_Object old = selected_frame;
12780 struct Lisp_Symbol *sym;
12781
12782 xassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12783
12784 selected_frame = frame;
12785
12786 do {
12787 for (tail = XFRAME (frame)->param_alist; CONSP (tail); tail = XCDR (tail))
12788 if (CONSP (XCAR (tail))
12789 && (tem = XCAR (XCAR (tail)),
12790 SYMBOLP (tem))
12791 && (sym = indirect_variable (XSYMBOL (tem)),
12792 sym->redirect == SYMBOL_LOCALIZED)
12793 && sym->val.blv->frame_local)
12794 /* Use find_symbol_value rather than Fsymbol_value
12795 to avoid an error if it is void. */
12796 find_symbol_value (tem);
12797 } while (!EQ (frame, old) && (frame = old, 1));
12798 }
12799
12800
12801 #define STOP_POLLING \
12802 do { if (! polling_stopped_here) stop_polling (); \
12803 polling_stopped_here = 1; } while (0)
12804
12805 #define RESUME_POLLING \
12806 do { if (polling_stopped_here) start_polling (); \
12807 polling_stopped_here = 0; } while (0)
12808
12809
12810 /* Perhaps in the future avoid recentering windows if it
12811 is not necessary; currently that causes some problems. */
12812
12813 static void
12814 redisplay_internal (void)
12815 {
12816 struct window *w = XWINDOW (selected_window);
12817 struct window *sw;
12818 struct frame *fr;
12819 int pending;
12820 int must_finish = 0;
12821 struct text_pos tlbufpos, tlendpos;
12822 int number_of_visible_frames;
12823 ptrdiff_t count, count1;
12824 struct frame *sf;
12825 int polling_stopped_here = 0;
12826 Lisp_Object old_frame = selected_frame;
12827
12828 /* Non-zero means redisplay has to consider all windows on all
12829 frames. Zero means, only selected_window is considered. */
12830 int consider_all_windows_p;
12831
12832 /* Non-zero means redisplay has to redisplay the miniwindow */
12833 int update_miniwindow_p = 0;
12834
12835 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12836
12837 /* No redisplay if running in batch mode or frame is not yet fully
12838 initialized, or redisplay is explicitly turned off by setting
12839 Vinhibit_redisplay. */
12840 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12841 || !NILP (Vinhibit_redisplay))
12842 return;
12843
12844 /* Don't examine these until after testing Vinhibit_redisplay.
12845 When Emacs is shutting down, perhaps because its connection to
12846 X has dropped, we should not look at them at all. */
12847 fr = XFRAME (w->frame);
12848 sf = SELECTED_FRAME ();
12849
12850 if (!fr->glyphs_initialized_p)
12851 return;
12852
12853 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12854 if (popup_activated ())
12855 return;
12856 #endif
12857
12858 /* I don't think this happens but let's be paranoid. */
12859 if (redisplaying_p)
12860 return;
12861
12862 /* Record a function that resets redisplaying_p to its old value
12863 when we leave this function. */
12864 count = SPECPDL_INDEX ();
12865 record_unwind_protect (unwind_redisplay,
12866 Fcons (make_number (redisplaying_p), selected_frame));
12867 ++redisplaying_p;
12868 specbind (Qinhibit_free_realized_faces, Qnil);
12869
12870 {
12871 Lisp_Object tail, frame;
12872
12873 FOR_EACH_FRAME (tail, frame)
12874 {
12875 struct frame *f = XFRAME (frame);
12876 f->already_hscrolled_p = 0;
12877 }
12878 }
12879
12880 retry:
12881 /* Remember the currently selected window. */
12882 sw = w;
12883
12884 if (!EQ (old_frame, selected_frame)
12885 && FRAME_LIVE_P (XFRAME (old_frame)))
12886 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
12887 selected_frame and selected_window to be temporarily out-of-sync so
12888 when we come back here via `goto retry', we need to resync because we
12889 may need to run Elisp code (via prepare_menu_bars). */
12890 select_frame_for_redisplay (old_frame);
12891
12892 pending = 0;
12893 reconsider_clip_changes (w, current_buffer);
12894 last_escape_glyph_frame = NULL;
12895 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12896 last_glyphless_glyph_frame = NULL;
12897 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12898
12899 /* If new fonts have been loaded that make a glyph matrix adjustment
12900 necessary, do it. */
12901 if (fonts_changed_p)
12902 {
12903 adjust_glyphs (NULL);
12904 ++windows_or_buffers_changed;
12905 fonts_changed_p = 0;
12906 }
12907
12908 /* If face_change_count is non-zero, init_iterator will free all
12909 realized faces, which includes the faces referenced from current
12910 matrices. So, we can't reuse current matrices in this case. */
12911 if (face_change_count)
12912 ++windows_or_buffers_changed;
12913
12914 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12915 && FRAME_TTY (sf)->previous_frame != sf)
12916 {
12917 /* Since frames on a single ASCII terminal share the same
12918 display area, displaying a different frame means redisplay
12919 the whole thing. */
12920 windows_or_buffers_changed++;
12921 SET_FRAME_GARBAGED (sf);
12922 #ifndef DOS_NT
12923 set_tty_color_mode (FRAME_TTY (sf), sf);
12924 #endif
12925 FRAME_TTY (sf)->previous_frame = sf;
12926 }
12927
12928 /* Set the visible flags for all frames. Do this before checking
12929 for resized or garbaged frames; they want to know if their frames
12930 are visible. See the comment in frame.h for
12931 FRAME_SAMPLE_VISIBILITY. */
12932 {
12933 Lisp_Object tail, frame;
12934
12935 number_of_visible_frames = 0;
12936
12937 FOR_EACH_FRAME (tail, frame)
12938 {
12939 struct frame *f = XFRAME (frame);
12940
12941 FRAME_SAMPLE_VISIBILITY (f);
12942 if (FRAME_VISIBLE_P (f))
12943 ++number_of_visible_frames;
12944 clear_desired_matrices (f);
12945 }
12946 }
12947
12948 /* Notice any pending interrupt request to change frame size. */
12949 do_pending_window_change (1);
12950
12951 /* do_pending_window_change could change the selected_window due to
12952 frame resizing which makes the selected window too small. */
12953 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12954 {
12955 sw = w;
12956 reconsider_clip_changes (w, current_buffer);
12957 }
12958
12959 /* Clear frames marked as garbaged. */
12960 if (frame_garbaged)
12961 clear_garbaged_frames ();
12962
12963 /* Build menubar and tool-bar items. */
12964 if (NILP (Vmemory_full))
12965 prepare_menu_bars ();
12966
12967 if (windows_or_buffers_changed)
12968 update_mode_lines++;
12969
12970 /* Detect case that we need to write or remove a star in the mode line. */
12971 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
12972 {
12973 w->update_mode_line = 1;
12974 if (buffer_shared > 1)
12975 update_mode_lines++;
12976 }
12977
12978 /* Avoid invocation of point motion hooks by `current_column' below. */
12979 count1 = SPECPDL_INDEX ();
12980 specbind (Qinhibit_point_motion_hooks, Qt);
12981
12982 /* If %c is in the mode line, update it if needed. */
12983 if (!NILP (w->column_number_displayed)
12984 /* This alternative quickly identifies a common case
12985 where no change is needed. */
12986 && !(PT == XFASTINT (w->last_point)
12987 && XFASTINT (w->last_modified) >= MODIFF
12988 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
12989 && (XFASTINT (w->column_number_displayed) != current_column ()))
12990 w->update_mode_line = 1;
12991
12992 unbind_to (count1, Qnil);
12993
12994 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12995
12996 /* The variable buffer_shared is set in redisplay_window and
12997 indicates that we redisplay a buffer in different windows. See
12998 there. */
12999 consider_all_windows_p = (update_mode_lines || buffer_shared > 1
13000 || cursor_type_changed);
13001
13002 /* If specs for an arrow have changed, do thorough redisplay
13003 to ensure we remove any arrow that should no longer exist. */
13004 if (overlay_arrows_changed_p ())
13005 consider_all_windows_p = windows_or_buffers_changed = 1;
13006
13007 /* Normally the message* functions will have already displayed and
13008 updated the echo area, but the frame may have been trashed, or
13009 the update may have been preempted, so display the echo area
13010 again here. Checking message_cleared_p captures the case that
13011 the echo area should be cleared. */
13012 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13013 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13014 || (message_cleared_p
13015 && minibuf_level == 0
13016 /* If the mini-window is currently selected, this means the
13017 echo-area doesn't show through. */
13018 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13019 {
13020 int window_height_changed_p = echo_area_display (0);
13021
13022 if (message_cleared_p)
13023 update_miniwindow_p = 1;
13024
13025 must_finish = 1;
13026
13027 /* If we don't display the current message, don't clear the
13028 message_cleared_p flag, because, if we did, we wouldn't clear
13029 the echo area in the next redisplay which doesn't preserve
13030 the echo area. */
13031 if (!display_last_displayed_message_p)
13032 message_cleared_p = 0;
13033
13034 if (fonts_changed_p)
13035 goto retry;
13036 else if (window_height_changed_p)
13037 {
13038 consider_all_windows_p = 1;
13039 ++update_mode_lines;
13040 ++windows_or_buffers_changed;
13041
13042 /* If window configuration was changed, frames may have been
13043 marked garbaged. Clear them or we will experience
13044 surprises wrt scrolling. */
13045 if (frame_garbaged)
13046 clear_garbaged_frames ();
13047 }
13048 }
13049 else if (EQ (selected_window, minibuf_window)
13050 && (current_buffer->clip_changed
13051 || XFASTINT (w->last_modified) < MODIFF
13052 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF)
13053 && resize_mini_window (w, 0))
13054 {
13055 /* Resized active mini-window to fit the size of what it is
13056 showing if its contents might have changed. */
13057 must_finish = 1;
13058 /* FIXME: this causes all frames to be updated, which seems unnecessary
13059 since only the current frame needs to be considered. This function needs
13060 to be rewritten with two variables, consider_all_windows and
13061 consider_all_frames. */
13062 consider_all_windows_p = 1;
13063 ++windows_or_buffers_changed;
13064 ++update_mode_lines;
13065
13066 /* If window configuration was changed, frames may have been
13067 marked garbaged. Clear them or we will experience
13068 surprises wrt scrolling. */
13069 if (frame_garbaged)
13070 clear_garbaged_frames ();
13071 }
13072
13073
13074 /* If showing the region, and mark has changed, we must redisplay
13075 the whole window. The assignment to this_line_start_pos prevents
13076 the optimization directly below this if-statement. */
13077 if (((!NILP (Vtransient_mark_mode)
13078 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13079 != !NILP (w->region_showing))
13080 || (!NILP (w->region_showing)
13081 && !EQ (w->region_showing,
13082 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13083 CHARPOS (this_line_start_pos) = 0;
13084
13085 /* Optimize the case that only the line containing the cursor in the
13086 selected window has changed. Variables starting with this_ are
13087 set in display_line and record information about the line
13088 containing the cursor. */
13089 tlbufpos = this_line_start_pos;
13090 tlendpos = this_line_end_pos;
13091 if (!consider_all_windows_p
13092 && CHARPOS (tlbufpos) > 0
13093 && !w->update_mode_line
13094 && !current_buffer->clip_changed
13095 && !current_buffer->prevent_redisplay_optimizations_p
13096 && FRAME_VISIBLE_P (XFRAME (w->frame))
13097 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13098 /* Make sure recorded data applies to current buffer, etc. */
13099 && this_line_buffer == current_buffer
13100 && current_buffer == XBUFFER (w->buffer)
13101 && !w->force_start
13102 && !w->optional_new_start
13103 /* Point must be on the line that we have info recorded about. */
13104 && PT >= CHARPOS (tlbufpos)
13105 && PT <= Z - CHARPOS (tlendpos)
13106 /* All text outside that line, including its final newline,
13107 must be unchanged. */
13108 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13109 CHARPOS (tlendpos)))
13110 {
13111 if (CHARPOS (tlbufpos) > BEGV
13112 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13113 && (CHARPOS (tlbufpos) == ZV
13114 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13115 /* Former continuation line has disappeared by becoming empty. */
13116 goto cancel;
13117 else if (XFASTINT (w->last_modified) < MODIFF
13118 || XFASTINT (w->last_overlay_modified) < OVERLAY_MODIFF
13119 || MINI_WINDOW_P (w))
13120 {
13121 /* We have to handle the case of continuation around a
13122 wide-column character (see the comment in indent.c around
13123 line 1340).
13124
13125 For instance, in the following case:
13126
13127 -------- Insert --------
13128 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13129 J_I_ ==> J_I_ `^^' are cursors.
13130 ^^ ^^
13131 -------- --------
13132
13133 As we have to redraw the line above, we cannot use this
13134 optimization. */
13135
13136 struct it it;
13137 int line_height_before = this_line_pixel_height;
13138
13139 /* Note that start_display will handle the case that the
13140 line starting at tlbufpos is a continuation line. */
13141 start_display (&it, w, tlbufpos);
13142
13143 /* Implementation note: It this still necessary? */
13144 if (it.current_x != this_line_start_x)
13145 goto cancel;
13146
13147 TRACE ((stderr, "trying display optimization 1\n"));
13148 w->cursor.vpos = -1;
13149 overlay_arrow_seen = 0;
13150 it.vpos = this_line_vpos;
13151 it.current_y = this_line_y;
13152 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13153 display_line (&it);
13154
13155 /* If line contains point, is not continued,
13156 and ends at same distance from eob as before, we win. */
13157 if (w->cursor.vpos >= 0
13158 /* Line is not continued, otherwise this_line_start_pos
13159 would have been set to 0 in display_line. */
13160 && CHARPOS (this_line_start_pos)
13161 /* Line ends as before. */
13162 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13163 /* Line has same height as before. Otherwise other lines
13164 would have to be shifted up or down. */
13165 && this_line_pixel_height == line_height_before)
13166 {
13167 /* If this is not the window's last line, we must adjust
13168 the charstarts of the lines below. */
13169 if (it.current_y < it.last_visible_y)
13170 {
13171 struct glyph_row *row
13172 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13173 ptrdiff_t delta, delta_bytes;
13174
13175 /* We used to distinguish between two cases here,
13176 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13177 when the line ends in a newline or the end of the
13178 buffer's accessible portion. But both cases did
13179 the same, so they were collapsed. */
13180 delta = (Z
13181 - CHARPOS (tlendpos)
13182 - MATRIX_ROW_START_CHARPOS (row));
13183 delta_bytes = (Z_BYTE
13184 - BYTEPOS (tlendpos)
13185 - MATRIX_ROW_START_BYTEPOS (row));
13186
13187 increment_matrix_positions (w->current_matrix,
13188 this_line_vpos + 1,
13189 w->current_matrix->nrows,
13190 delta, delta_bytes);
13191 }
13192
13193 /* If this row displays text now but previously didn't,
13194 or vice versa, w->window_end_vpos may have to be
13195 adjusted. */
13196 if ((it.glyph_row - 1)->displays_text_p)
13197 {
13198 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13199 XSETINT (w->window_end_vpos, this_line_vpos);
13200 }
13201 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13202 && this_line_vpos > 0)
13203 XSETINT (w->window_end_vpos, this_line_vpos - 1);
13204 w->window_end_valid = Qnil;
13205
13206 /* Update hint: No need to try to scroll in update_window. */
13207 w->desired_matrix->no_scrolling_p = 1;
13208
13209 #if GLYPH_DEBUG
13210 *w->desired_matrix->method = 0;
13211 debug_method_add (w, "optimization 1");
13212 #endif
13213 #ifdef HAVE_WINDOW_SYSTEM
13214 update_window_fringes (w, 0);
13215 #endif
13216 goto update;
13217 }
13218 else
13219 goto cancel;
13220 }
13221 else if (/* Cursor position hasn't changed. */
13222 PT == XFASTINT (w->last_point)
13223 /* Make sure the cursor was last displayed
13224 in this window. Otherwise we have to reposition it. */
13225 && 0 <= w->cursor.vpos
13226 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13227 {
13228 if (!must_finish)
13229 {
13230 do_pending_window_change (1);
13231 /* If selected_window changed, redisplay again. */
13232 if (WINDOWP (selected_window)
13233 && (w = XWINDOW (selected_window)) != sw)
13234 goto retry;
13235
13236 /* We used to always goto end_of_redisplay here, but this
13237 isn't enough if we have a blinking cursor. */
13238 if (w->cursor_off_p == w->last_cursor_off_p)
13239 goto end_of_redisplay;
13240 }
13241 goto update;
13242 }
13243 /* If highlighting the region, or if the cursor is in the echo area,
13244 then we can't just move the cursor. */
13245 else if (! (!NILP (Vtransient_mark_mode)
13246 && !NILP (BVAR (current_buffer, mark_active)))
13247 && (EQ (selected_window,
13248 BVAR (current_buffer, last_selected_window))
13249 || highlight_nonselected_windows)
13250 && NILP (w->region_showing)
13251 && NILP (Vshow_trailing_whitespace)
13252 && !cursor_in_echo_area)
13253 {
13254 struct it it;
13255 struct glyph_row *row;
13256
13257 /* Skip from tlbufpos to PT and see where it is. Note that
13258 PT may be in invisible text. If so, we will end at the
13259 next visible position. */
13260 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13261 NULL, DEFAULT_FACE_ID);
13262 it.current_x = this_line_start_x;
13263 it.current_y = this_line_y;
13264 it.vpos = this_line_vpos;
13265
13266 /* The call to move_it_to stops in front of PT, but
13267 moves over before-strings. */
13268 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13269
13270 if (it.vpos == this_line_vpos
13271 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13272 row->enabled_p))
13273 {
13274 xassert (this_line_vpos == it.vpos);
13275 xassert (this_line_y == it.current_y);
13276 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13277 #if GLYPH_DEBUG
13278 *w->desired_matrix->method = 0;
13279 debug_method_add (w, "optimization 3");
13280 #endif
13281 goto update;
13282 }
13283 else
13284 goto cancel;
13285 }
13286
13287 cancel:
13288 /* Text changed drastically or point moved off of line. */
13289 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13290 }
13291
13292 CHARPOS (this_line_start_pos) = 0;
13293 consider_all_windows_p |= buffer_shared > 1;
13294 ++clear_face_cache_count;
13295 #ifdef HAVE_WINDOW_SYSTEM
13296 ++clear_image_cache_count;
13297 #endif
13298
13299 /* Build desired matrices, and update the display. If
13300 consider_all_windows_p is non-zero, do it for all windows on all
13301 frames. Otherwise do it for selected_window, only. */
13302
13303 if (consider_all_windows_p)
13304 {
13305 Lisp_Object tail, frame;
13306
13307 FOR_EACH_FRAME (tail, frame)
13308 XFRAME (frame)->updated_p = 0;
13309
13310 /* Recompute # windows showing selected buffer. This will be
13311 incremented each time such a window is displayed. */
13312 buffer_shared = 0;
13313
13314 FOR_EACH_FRAME (tail, frame)
13315 {
13316 struct frame *f = XFRAME (frame);
13317
13318 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13319 {
13320 if (! EQ (frame, selected_frame))
13321 /* Select the frame, for the sake of frame-local
13322 variables. */
13323 select_frame_for_redisplay (frame);
13324
13325 /* Mark all the scroll bars to be removed; we'll redeem
13326 the ones we want when we redisplay their windows. */
13327 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13328 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13329
13330 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13331 redisplay_windows (FRAME_ROOT_WINDOW (f));
13332
13333 /* The X error handler may have deleted that frame. */
13334 if (!FRAME_LIVE_P (f))
13335 continue;
13336
13337 /* Any scroll bars which redisplay_windows should have
13338 nuked should now go away. */
13339 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13340 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13341
13342 /* If fonts changed, display again. */
13343 /* ??? rms: I suspect it is a mistake to jump all the way
13344 back to retry here. It should just retry this frame. */
13345 if (fonts_changed_p)
13346 goto retry;
13347
13348 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13349 {
13350 /* See if we have to hscroll. */
13351 if (!f->already_hscrolled_p)
13352 {
13353 f->already_hscrolled_p = 1;
13354 if (hscroll_windows (f->root_window))
13355 goto retry;
13356 }
13357
13358 /* Prevent various kinds of signals during display
13359 update. stdio is not robust about handling
13360 signals, which can cause an apparent I/O
13361 error. */
13362 if (interrupt_input)
13363 unrequest_sigio ();
13364 STOP_POLLING;
13365
13366 /* Update the display. */
13367 set_window_update_flags (XWINDOW (f->root_window), 1);
13368 pending |= update_frame (f, 0, 0);
13369 f->updated_p = 1;
13370 }
13371 }
13372 }
13373
13374 if (!EQ (old_frame, selected_frame)
13375 && FRAME_LIVE_P (XFRAME (old_frame)))
13376 /* We played a bit fast-and-loose above and allowed selected_frame
13377 and selected_window to be temporarily out-of-sync but let's make
13378 sure this stays contained. */
13379 select_frame_for_redisplay (old_frame);
13380 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13381
13382 if (!pending)
13383 {
13384 /* Do the mark_window_display_accurate after all windows have
13385 been redisplayed because this call resets flags in buffers
13386 which are needed for proper redisplay. */
13387 FOR_EACH_FRAME (tail, frame)
13388 {
13389 struct frame *f = XFRAME (frame);
13390 if (f->updated_p)
13391 {
13392 mark_window_display_accurate (f->root_window, 1);
13393 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13394 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13395 }
13396 }
13397 }
13398 }
13399 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13400 {
13401 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13402 struct frame *mini_frame;
13403
13404 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13405 /* Use list_of_error, not Qerror, so that
13406 we catch only errors and don't run the debugger. */
13407 internal_condition_case_1 (redisplay_window_1, selected_window,
13408 list_of_error,
13409 redisplay_window_error);
13410 if (update_miniwindow_p)
13411 internal_condition_case_1 (redisplay_window_1, mini_window,
13412 list_of_error,
13413 redisplay_window_error);
13414
13415 /* Compare desired and current matrices, perform output. */
13416
13417 update:
13418 /* If fonts changed, display again. */
13419 if (fonts_changed_p)
13420 goto retry;
13421
13422 /* Prevent various kinds of signals during display update.
13423 stdio is not robust about handling signals,
13424 which can cause an apparent I/O error. */
13425 if (interrupt_input)
13426 unrequest_sigio ();
13427 STOP_POLLING;
13428
13429 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13430 {
13431 if (hscroll_windows (selected_window))
13432 goto retry;
13433
13434 XWINDOW (selected_window)->must_be_updated_p = 1;
13435 pending = update_frame (sf, 0, 0);
13436 }
13437
13438 /* We may have called echo_area_display at the top of this
13439 function. If the echo area is on another frame, that may
13440 have put text on a frame other than the selected one, so the
13441 above call to update_frame would not have caught it. Catch
13442 it here. */
13443 mini_window = FRAME_MINIBUF_WINDOW (sf);
13444 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13445
13446 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13447 {
13448 XWINDOW (mini_window)->must_be_updated_p = 1;
13449 pending |= update_frame (mini_frame, 0, 0);
13450 if (!pending && hscroll_windows (mini_window))
13451 goto retry;
13452 }
13453 }
13454
13455 /* If display was paused because of pending input, make sure we do a
13456 thorough update the next time. */
13457 if (pending)
13458 {
13459 /* Prevent the optimization at the beginning of
13460 redisplay_internal that tries a single-line update of the
13461 line containing the cursor in the selected window. */
13462 CHARPOS (this_line_start_pos) = 0;
13463
13464 /* Let the overlay arrow be updated the next time. */
13465 update_overlay_arrows (0);
13466
13467 /* If we pause after scrolling, some rows in the current
13468 matrices of some windows are not valid. */
13469 if (!WINDOW_FULL_WIDTH_P (w)
13470 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13471 update_mode_lines = 1;
13472 }
13473 else
13474 {
13475 if (!consider_all_windows_p)
13476 {
13477 /* This has already been done above if
13478 consider_all_windows_p is set. */
13479 mark_window_display_accurate_1 (w, 1);
13480
13481 /* Say overlay arrows are up to date. */
13482 update_overlay_arrows (1);
13483
13484 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13485 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13486 }
13487
13488 update_mode_lines = 0;
13489 windows_or_buffers_changed = 0;
13490 cursor_type_changed = 0;
13491 }
13492
13493 /* Start SIGIO interrupts coming again. Having them off during the
13494 code above makes it less likely one will discard output, but not
13495 impossible, since there might be stuff in the system buffer here.
13496 But it is much hairier to try to do anything about that. */
13497 if (interrupt_input)
13498 request_sigio ();
13499 RESUME_POLLING;
13500
13501 /* If a frame has become visible which was not before, redisplay
13502 again, so that we display it. Expose events for such a frame
13503 (which it gets when becoming visible) don't call the parts of
13504 redisplay constructing glyphs, so simply exposing a frame won't
13505 display anything in this case. So, we have to display these
13506 frames here explicitly. */
13507 if (!pending)
13508 {
13509 Lisp_Object tail, frame;
13510 int new_count = 0;
13511
13512 FOR_EACH_FRAME (tail, frame)
13513 {
13514 int this_is_visible = 0;
13515
13516 if (XFRAME (frame)->visible)
13517 this_is_visible = 1;
13518 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13519 if (XFRAME (frame)->visible)
13520 this_is_visible = 1;
13521
13522 if (this_is_visible)
13523 new_count++;
13524 }
13525
13526 if (new_count != number_of_visible_frames)
13527 windows_or_buffers_changed++;
13528 }
13529
13530 /* Change frame size now if a change is pending. */
13531 do_pending_window_change (1);
13532
13533 /* If we just did a pending size change, or have additional
13534 visible frames, or selected_window changed, redisplay again. */
13535 if ((windows_or_buffers_changed && !pending)
13536 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13537 goto retry;
13538
13539 /* Clear the face and image caches.
13540
13541 We used to do this only if consider_all_windows_p. But the cache
13542 needs to be cleared if a timer creates images in the current
13543 buffer (e.g. the test case in Bug#6230). */
13544
13545 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13546 {
13547 clear_face_cache (0);
13548 clear_face_cache_count = 0;
13549 }
13550
13551 #ifdef HAVE_WINDOW_SYSTEM
13552 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13553 {
13554 clear_image_caches (Qnil);
13555 clear_image_cache_count = 0;
13556 }
13557 #endif /* HAVE_WINDOW_SYSTEM */
13558
13559 end_of_redisplay:
13560 unbind_to (count, Qnil);
13561 RESUME_POLLING;
13562 }
13563
13564
13565 /* Redisplay, but leave alone any recent echo area message unless
13566 another message has been requested in its place.
13567
13568 This is useful in situations where you need to redisplay but no
13569 user action has occurred, making it inappropriate for the message
13570 area to be cleared. See tracking_off and
13571 wait_reading_process_output for examples of these situations.
13572
13573 FROM_WHERE is an integer saying from where this function was
13574 called. This is useful for debugging. */
13575
13576 void
13577 redisplay_preserve_echo_area (int from_where)
13578 {
13579 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13580
13581 if (!NILP (echo_area_buffer[1]))
13582 {
13583 /* We have a previously displayed message, but no current
13584 message. Redisplay the previous message. */
13585 display_last_displayed_message_p = 1;
13586 redisplay_internal ();
13587 display_last_displayed_message_p = 0;
13588 }
13589 else
13590 redisplay_internal ();
13591
13592 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13593 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13594 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13595 }
13596
13597
13598 /* Function registered with record_unwind_protect in
13599 redisplay_internal. Reset redisplaying_p to the value it had
13600 before redisplay_internal was called, and clear
13601 prevent_freeing_realized_faces_p. It also selects the previously
13602 selected frame, unless it has been deleted (by an X connection
13603 failure during redisplay, for example). */
13604
13605 static Lisp_Object
13606 unwind_redisplay (Lisp_Object val)
13607 {
13608 Lisp_Object old_redisplaying_p, old_frame;
13609
13610 old_redisplaying_p = XCAR (val);
13611 redisplaying_p = XFASTINT (old_redisplaying_p);
13612 old_frame = XCDR (val);
13613 if (! EQ (old_frame, selected_frame)
13614 && FRAME_LIVE_P (XFRAME (old_frame)))
13615 select_frame_for_redisplay (old_frame);
13616 return Qnil;
13617 }
13618
13619
13620 /* Mark the display of window W as accurate or inaccurate. If
13621 ACCURATE_P is non-zero mark display of W as accurate. If
13622 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13623 redisplay_internal is called. */
13624
13625 static void
13626 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13627 {
13628 if (BUFFERP (w->buffer))
13629 {
13630 struct buffer *b = XBUFFER (w->buffer);
13631
13632 w->last_modified
13633 = make_number (accurate_p ? BUF_MODIFF (b) : 0);
13634 w->last_overlay_modified
13635 = make_number (accurate_p ? BUF_OVERLAY_MODIFF (b) : 0);
13636 w->last_had_star
13637 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13638
13639 if (accurate_p)
13640 {
13641 b->clip_changed = 0;
13642 b->prevent_redisplay_optimizations_p = 0;
13643
13644 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13645 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13646 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13647 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13648
13649 w->current_matrix->buffer = b;
13650 w->current_matrix->begv = BUF_BEGV (b);
13651 w->current_matrix->zv = BUF_ZV (b);
13652
13653 w->last_cursor = w->cursor;
13654 w->last_cursor_off_p = w->cursor_off_p;
13655
13656 if (w == XWINDOW (selected_window))
13657 w->last_point = make_number (BUF_PT (b));
13658 else
13659 w->last_point = make_number (XMARKER (w->pointm)->charpos);
13660 }
13661 }
13662
13663 if (accurate_p)
13664 {
13665 w->window_end_valid = w->buffer;
13666 w->update_mode_line = 0;
13667 }
13668 }
13669
13670
13671 /* Mark the display of windows in the window tree rooted at WINDOW as
13672 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13673 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13674 be redisplayed the next time redisplay_internal is called. */
13675
13676 void
13677 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13678 {
13679 struct window *w;
13680
13681 for (; !NILP (window); window = w->next)
13682 {
13683 w = XWINDOW (window);
13684 mark_window_display_accurate_1 (w, accurate_p);
13685
13686 if (!NILP (w->vchild))
13687 mark_window_display_accurate (w->vchild, accurate_p);
13688 if (!NILP (w->hchild))
13689 mark_window_display_accurate (w->hchild, accurate_p);
13690 }
13691
13692 if (accurate_p)
13693 {
13694 update_overlay_arrows (1);
13695 }
13696 else
13697 {
13698 /* Force a thorough redisplay the next time by setting
13699 last_arrow_position and last_arrow_string to t, which is
13700 unequal to any useful value of Voverlay_arrow_... */
13701 update_overlay_arrows (-1);
13702 }
13703 }
13704
13705
13706 /* Return value in display table DP (Lisp_Char_Table *) for character
13707 C. Since a display table doesn't have any parent, we don't have to
13708 follow parent. Do not call this function directly but use the
13709 macro DISP_CHAR_VECTOR. */
13710
13711 Lisp_Object
13712 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13713 {
13714 Lisp_Object val;
13715
13716 if (ASCII_CHAR_P (c))
13717 {
13718 val = dp->ascii;
13719 if (SUB_CHAR_TABLE_P (val))
13720 val = XSUB_CHAR_TABLE (val)->contents[c];
13721 }
13722 else
13723 {
13724 Lisp_Object table;
13725
13726 XSETCHAR_TABLE (table, dp);
13727 val = char_table_ref (table, c);
13728 }
13729 if (NILP (val))
13730 val = dp->defalt;
13731 return val;
13732 }
13733
13734
13735 \f
13736 /***********************************************************************
13737 Window Redisplay
13738 ***********************************************************************/
13739
13740 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13741
13742 static void
13743 redisplay_windows (Lisp_Object window)
13744 {
13745 while (!NILP (window))
13746 {
13747 struct window *w = XWINDOW (window);
13748
13749 if (!NILP (w->hchild))
13750 redisplay_windows (w->hchild);
13751 else if (!NILP (w->vchild))
13752 redisplay_windows (w->vchild);
13753 else if (!NILP (w->buffer))
13754 {
13755 displayed_buffer = XBUFFER (w->buffer);
13756 /* Use list_of_error, not Qerror, so that
13757 we catch only errors and don't run the debugger. */
13758 internal_condition_case_1 (redisplay_window_0, window,
13759 list_of_error,
13760 redisplay_window_error);
13761 }
13762
13763 window = w->next;
13764 }
13765 }
13766
13767 static Lisp_Object
13768 redisplay_window_error (Lisp_Object ignore)
13769 {
13770 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13771 return Qnil;
13772 }
13773
13774 static Lisp_Object
13775 redisplay_window_0 (Lisp_Object window)
13776 {
13777 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13778 redisplay_window (window, 0);
13779 return Qnil;
13780 }
13781
13782 static Lisp_Object
13783 redisplay_window_1 (Lisp_Object window)
13784 {
13785 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13786 redisplay_window (window, 1);
13787 return Qnil;
13788 }
13789 \f
13790
13791 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13792 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13793 which positions recorded in ROW differ from current buffer
13794 positions.
13795
13796 Return 0 if cursor is not on this row, 1 otherwise. */
13797
13798 static int
13799 set_cursor_from_row (struct window *w, struct glyph_row *row,
13800 struct glyph_matrix *matrix,
13801 ptrdiff_t delta, ptrdiff_t delta_bytes,
13802 int dy, int dvpos)
13803 {
13804 struct glyph *glyph = row->glyphs[TEXT_AREA];
13805 struct glyph *end = glyph + row->used[TEXT_AREA];
13806 struct glyph *cursor = NULL;
13807 /* The last known character position in row. */
13808 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13809 int x = row->x;
13810 ptrdiff_t pt_old = PT - delta;
13811 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13812 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13813 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13814 /* A glyph beyond the edge of TEXT_AREA which we should never
13815 touch. */
13816 struct glyph *glyphs_end = end;
13817 /* Non-zero means we've found a match for cursor position, but that
13818 glyph has the avoid_cursor_p flag set. */
13819 int match_with_avoid_cursor = 0;
13820 /* Non-zero means we've seen at least one glyph that came from a
13821 display string. */
13822 int string_seen = 0;
13823 /* Largest and smallest buffer positions seen so far during scan of
13824 glyph row. */
13825 ptrdiff_t bpos_max = pos_before;
13826 ptrdiff_t bpos_min = pos_after;
13827 /* Last buffer position covered by an overlay string with an integer
13828 `cursor' property. */
13829 ptrdiff_t bpos_covered = 0;
13830 /* Non-zero means the display string on which to display the cursor
13831 comes from a text property, not from an overlay. */
13832 int string_from_text_prop = 0;
13833
13834 /* Don't even try doing anything if called for a mode-line or
13835 header-line row, since the rest of the code isn't prepared to
13836 deal with such calamities. */
13837 xassert (!row->mode_line_p);
13838 if (row->mode_line_p)
13839 return 0;
13840
13841 /* Skip over glyphs not having an object at the start and the end of
13842 the row. These are special glyphs like truncation marks on
13843 terminal frames. */
13844 if (row->displays_text_p)
13845 {
13846 if (!row->reversed_p)
13847 {
13848 while (glyph < end
13849 && INTEGERP (glyph->object)
13850 && glyph->charpos < 0)
13851 {
13852 x += glyph->pixel_width;
13853 ++glyph;
13854 }
13855 while (end > glyph
13856 && INTEGERP ((end - 1)->object)
13857 /* CHARPOS is zero for blanks and stretch glyphs
13858 inserted by extend_face_to_end_of_line. */
13859 && (end - 1)->charpos <= 0)
13860 --end;
13861 glyph_before = glyph - 1;
13862 glyph_after = end;
13863 }
13864 else
13865 {
13866 struct glyph *g;
13867
13868 /* If the glyph row is reversed, we need to process it from back
13869 to front, so swap the edge pointers. */
13870 glyphs_end = end = glyph - 1;
13871 glyph += row->used[TEXT_AREA] - 1;
13872
13873 while (glyph > end + 1
13874 && INTEGERP (glyph->object)
13875 && glyph->charpos < 0)
13876 {
13877 --glyph;
13878 x -= glyph->pixel_width;
13879 }
13880 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13881 --glyph;
13882 /* By default, in reversed rows we put the cursor on the
13883 rightmost (first in the reading order) glyph. */
13884 for (g = end + 1; g < glyph; g++)
13885 x += g->pixel_width;
13886 while (end < glyph
13887 && INTEGERP ((end + 1)->object)
13888 && (end + 1)->charpos <= 0)
13889 ++end;
13890 glyph_before = glyph + 1;
13891 glyph_after = end;
13892 }
13893 }
13894 else if (row->reversed_p)
13895 {
13896 /* In R2L rows that don't display text, put the cursor on the
13897 rightmost glyph. Case in point: an empty last line that is
13898 part of an R2L paragraph. */
13899 cursor = end - 1;
13900 /* Avoid placing the cursor on the last glyph of the row, where
13901 on terminal frames we hold the vertical border between
13902 adjacent windows. */
13903 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13904 && !WINDOW_RIGHTMOST_P (w)
13905 && cursor == row->glyphs[LAST_AREA] - 1)
13906 cursor--;
13907 x = -1; /* will be computed below, at label compute_x */
13908 }
13909
13910 /* Step 1: Try to find the glyph whose character position
13911 corresponds to point. If that's not possible, find 2 glyphs
13912 whose character positions are the closest to point, one before
13913 point, the other after it. */
13914 if (!row->reversed_p)
13915 while (/* not marched to end of glyph row */
13916 glyph < end
13917 /* glyph was not inserted by redisplay for internal purposes */
13918 && !INTEGERP (glyph->object))
13919 {
13920 if (BUFFERP (glyph->object))
13921 {
13922 ptrdiff_t dpos = glyph->charpos - pt_old;
13923
13924 if (glyph->charpos > bpos_max)
13925 bpos_max = glyph->charpos;
13926 if (glyph->charpos < bpos_min)
13927 bpos_min = glyph->charpos;
13928 if (!glyph->avoid_cursor_p)
13929 {
13930 /* If we hit point, we've found the glyph on which to
13931 display the cursor. */
13932 if (dpos == 0)
13933 {
13934 match_with_avoid_cursor = 0;
13935 break;
13936 }
13937 /* See if we've found a better approximation to
13938 POS_BEFORE or to POS_AFTER. Note that we want the
13939 first (leftmost) glyph of all those that are the
13940 closest from below, and the last (rightmost) of all
13941 those from above. */
13942 if (0 > dpos && dpos > pos_before - pt_old)
13943 {
13944 pos_before = glyph->charpos;
13945 glyph_before = glyph;
13946 }
13947 else if (0 < dpos && dpos <= pos_after - pt_old)
13948 {
13949 pos_after = glyph->charpos;
13950 glyph_after = glyph;
13951 }
13952 }
13953 else if (dpos == 0)
13954 match_with_avoid_cursor = 1;
13955 }
13956 else if (STRINGP (glyph->object))
13957 {
13958 Lisp_Object chprop;
13959 ptrdiff_t glyph_pos = glyph->charpos;
13960
13961 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13962 glyph->object);
13963 if (!NILP (chprop))
13964 {
13965 /* If the string came from a `display' text property,
13966 look up the buffer position of that property and
13967 use that position to update bpos_max, as if we
13968 actually saw such a position in one of the row's
13969 glyphs. This helps with supporting integer values
13970 of `cursor' property on the display string in
13971 situations where most or all of the row's buffer
13972 text is completely covered by display properties,
13973 so that no glyph with valid buffer positions is
13974 ever seen in the row. */
13975 ptrdiff_t prop_pos =
13976 string_buffer_position_lim (glyph->object, pos_before,
13977 pos_after, 0);
13978
13979 if (prop_pos >= pos_before)
13980 bpos_max = prop_pos - 1;
13981 }
13982 if (INTEGERP (chprop))
13983 {
13984 bpos_covered = bpos_max + XINT (chprop);
13985 /* If the `cursor' property covers buffer positions up
13986 to and including point, we should display cursor on
13987 this glyph. Note that, if a `cursor' property on one
13988 of the string's characters has an integer value, we
13989 will break out of the loop below _before_ we get to
13990 the position match above. IOW, integer values of
13991 the `cursor' property override the "exact match for
13992 point" strategy of positioning the cursor. */
13993 /* Implementation note: bpos_max == pt_old when, e.g.,
13994 we are in an empty line, where bpos_max is set to
13995 MATRIX_ROW_START_CHARPOS, see above. */
13996 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13997 {
13998 cursor = glyph;
13999 break;
14000 }
14001 }
14002
14003 string_seen = 1;
14004 }
14005 x += glyph->pixel_width;
14006 ++glyph;
14007 }
14008 else if (glyph > end) /* row is reversed */
14009 while (!INTEGERP (glyph->object))
14010 {
14011 if (BUFFERP (glyph->object))
14012 {
14013 ptrdiff_t dpos = glyph->charpos - pt_old;
14014
14015 if (glyph->charpos > bpos_max)
14016 bpos_max = glyph->charpos;
14017 if (glyph->charpos < bpos_min)
14018 bpos_min = glyph->charpos;
14019 if (!glyph->avoid_cursor_p)
14020 {
14021 if (dpos == 0)
14022 {
14023 match_with_avoid_cursor = 0;
14024 break;
14025 }
14026 if (0 > dpos && dpos > pos_before - pt_old)
14027 {
14028 pos_before = glyph->charpos;
14029 glyph_before = glyph;
14030 }
14031 else if (0 < dpos && dpos <= pos_after - pt_old)
14032 {
14033 pos_after = glyph->charpos;
14034 glyph_after = glyph;
14035 }
14036 }
14037 else if (dpos == 0)
14038 match_with_avoid_cursor = 1;
14039 }
14040 else if (STRINGP (glyph->object))
14041 {
14042 Lisp_Object chprop;
14043 ptrdiff_t glyph_pos = glyph->charpos;
14044
14045 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14046 glyph->object);
14047 if (!NILP (chprop))
14048 {
14049 ptrdiff_t prop_pos =
14050 string_buffer_position_lim (glyph->object, pos_before,
14051 pos_after, 0);
14052
14053 if (prop_pos >= pos_before)
14054 bpos_max = prop_pos - 1;
14055 }
14056 if (INTEGERP (chprop))
14057 {
14058 bpos_covered = bpos_max + XINT (chprop);
14059 /* If the `cursor' property covers buffer positions up
14060 to and including point, we should display cursor on
14061 this glyph. */
14062 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14063 {
14064 cursor = glyph;
14065 break;
14066 }
14067 }
14068 string_seen = 1;
14069 }
14070 --glyph;
14071 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14072 {
14073 x--; /* can't use any pixel_width */
14074 break;
14075 }
14076 x -= glyph->pixel_width;
14077 }
14078
14079 /* Step 2: If we didn't find an exact match for point, we need to
14080 look for a proper place to put the cursor among glyphs between
14081 GLYPH_BEFORE and GLYPH_AFTER. */
14082 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14083 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14084 && bpos_covered < pt_old)
14085 {
14086 /* An empty line has a single glyph whose OBJECT is zero and
14087 whose CHARPOS is the position of a newline on that line.
14088 Note that on a TTY, there are more glyphs after that, which
14089 were produced by extend_face_to_end_of_line, but their
14090 CHARPOS is zero or negative. */
14091 int empty_line_p =
14092 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14093 && INTEGERP (glyph->object) && glyph->charpos > 0;
14094
14095 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14096 {
14097 ptrdiff_t ellipsis_pos;
14098
14099 /* Scan back over the ellipsis glyphs. */
14100 if (!row->reversed_p)
14101 {
14102 ellipsis_pos = (glyph - 1)->charpos;
14103 while (glyph > row->glyphs[TEXT_AREA]
14104 && (glyph - 1)->charpos == ellipsis_pos)
14105 glyph--, x -= glyph->pixel_width;
14106 /* That loop always goes one position too far, including
14107 the glyph before the ellipsis. So scan forward over
14108 that one. */
14109 x += glyph->pixel_width;
14110 glyph++;
14111 }
14112 else /* row is reversed */
14113 {
14114 ellipsis_pos = (glyph + 1)->charpos;
14115 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14116 && (glyph + 1)->charpos == ellipsis_pos)
14117 glyph++, x += glyph->pixel_width;
14118 x -= glyph->pixel_width;
14119 glyph--;
14120 }
14121 }
14122 else if (match_with_avoid_cursor)
14123 {
14124 cursor = glyph_after;
14125 x = -1;
14126 }
14127 else if (string_seen)
14128 {
14129 int incr = row->reversed_p ? -1 : +1;
14130
14131 /* Need to find the glyph that came out of a string which is
14132 present at point. That glyph is somewhere between
14133 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14134 positioned between POS_BEFORE and POS_AFTER in the
14135 buffer. */
14136 struct glyph *start, *stop;
14137 ptrdiff_t pos = pos_before;
14138
14139 x = -1;
14140
14141 /* If the row ends in a newline from a display string,
14142 reordering could have moved the glyphs belonging to the
14143 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14144 in this case we extend the search to the last glyph in
14145 the row that was not inserted by redisplay. */
14146 if (row->ends_in_newline_from_string_p)
14147 {
14148 glyph_after = end;
14149 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14150 }
14151
14152 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14153 correspond to POS_BEFORE and POS_AFTER, respectively. We
14154 need START and STOP in the order that corresponds to the
14155 row's direction as given by its reversed_p flag. If the
14156 directionality of characters between POS_BEFORE and
14157 POS_AFTER is the opposite of the row's base direction,
14158 these characters will have been reordered for display,
14159 and we need to reverse START and STOP. */
14160 if (!row->reversed_p)
14161 {
14162 start = min (glyph_before, glyph_after);
14163 stop = max (glyph_before, glyph_after);
14164 }
14165 else
14166 {
14167 start = max (glyph_before, glyph_after);
14168 stop = min (glyph_before, glyph_after);
14169 }
14170 for (glyph = start + incr;
14171 row->reversed_p ? glyph > stop : glyph < stop; )
14172 {
14173
14174 /* Any glyphs that come from the buffer are here because
14175 of bidi reordering. Skip them, and only pay
14176 attention to glyphs that came from some string. */
14177 if (STRINGP (glyph->object))
14178 {
14179 Lisp_Object str;
14180 ptrdiff_t tem;
14181 /* If the display property covers the newline, we
14182 need to search for it one position farther. */
14183 ptrdiff_t lim = pos_after
14184 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14185
14186 string_from_text_prop = 0;
14187 str = glyph->object;
14188 tem = string_buffer_position_lim (str, pos, lim, 0);
14189 if (tem == 0 /* from overlay */
14190 || pos <= tem)
14191 {
14192 /* If the string from which this glyph came is
14193 found in the buffer at point, or at position
14194 that is closer to point than pos_after, then
14195 we've found the glyph we've been looking for.
14196 If it comes from an overlay (tem == 0), and
14197 it has the `cursor' property on one of its
14198 glyphs, record that glyph as a candidate for
14199 displaying the cursor. (As in the
14200 unidirectional version, we will display the
14201 cursor on the last candidate we find.) */
14202 if (tem == 0
14203 || tem == pt_old
14204 || (tem - pt_old > 0 && tem < pos_after))
14205 {
14206 /* The glyphs from this string could have
14207 been reordered. Find the one with the
14208 smallest string position. Or there could
14209 be a character in the string with the
14210 `cursor' property, which means display
14211 cursor on that character's glyph. */
14212 ptrdiff_t strpos = glyph->charpos;
14213
14214 if (tem)
14215 {
14216 cursor = glyph;
14217 string_from_text_prop = 1;
14218 }
14219 for ( ;
14220 (row->reversed_p ? glyph > stop : glyph < stop)
14221 && EQ (glyph->object, str);
14222 glyph += incr)
14223 {
14224 Lisp_Object cprop;
14225 ptrdiff_t gpos = glyph->charpos;
14226
14227 cprop = Fget_char_property (make_number (gpos),
14228 Qcursor,
14229 glyph->object);
14230 if (!NILP (cprop))
14231 {
14232 cursor = glyph;
14233 break;
14234 }
14235 if (tem && glyph->charpos < strpos)
14236 {
14237 strpos = glyph->charpos;
14238 cursor = glyph;
14239 }
14240 }
14241
14242 if (tem == pt_old
14243 || (tem - pt_old > 0 && tem < pos_after))
14244 goto compute_x;
14245 }
14246 if (tem)
14247 pos = tem + 1; /* don't find previous instances */
14248 }
14249 /* This string is not what we want; skip all of the
14250 glyphs that came from it. */
14251 while ((row->reversed_p ? glyph > stop : glyph < stop)
14252 && EQ (glyph->object, str))
14253 glyph += incr;
14254 }
14255 else
14256 glyph += incr;
14257 }
14258
14259 /* If we reached the end of the line, and END was from a string,
14260 the cursor is not on this line. */
14261 if (cursor == NULL
14262 && (row->reversed_p ? glyph <= end : glyph >= end)
14263 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14264 && STRINGP (end->object)
14265 && row->continued_p)
14266 return 0;
14267 }
14268 /* A truncated row may not include PT among its character positions.
14269 Setting the cursor inside the scroll margin will trigger
14270 recalculation of hscroll in hscroll_window_tree. But if a
14271 display string covers point, defer to the string-handling
14272 code below to figure this out. */
14273 else if (row->truncated_on_left_p && pt_old < bpos_min)
14274 {
14275 cursor = glyph_before;
14276 x = -1;
14277 }
14278 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14279 /* Zero-width characters produce no glyphs. */
14280 || (!empty_line_p
14281 && (row->reversed_p
14282 ? glyph_after > glyphs_end
14283 : glyph_after < glyphs_end)))
14284 {
14285 cursor = glyph_after;
14286 x = -1;
14287 }
14288 }
14289
14290 compute_x:
14291 if (cursor != NULL)
14292 glyph = cursor;
14293 else if (glyph == glyphs_end
14294 && pos_before == pos_after
14295 && STRINGP ((row->reversed_p
14296 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14297 : row->glyphs[TEXT_AREA])->object))
14298 {
14299 /* If all the glyphs of this row came from strings, put the
14300 cursor on the first glyph of the row. This avoids having the
14301 cursor outside of the text area in this very rare and hard
14302 use case. */
14303 glyph =
14304 row->reversed_p
14305 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14306 : row->glyphs[TEXT_AREA];
14307 }
14308 if (x < 0)
14309 {
14310 struct glyph *g;
14311
14312 /* Need to compute x that corresponds to GLYPH. */
14313 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14314 {
14315 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14316 abort ();
14317 x += g->pixel_width;
14318 }
14319 }
14320
14321 /* ROW could be part of a continued line, which, under bidi
14322 reordering, might have other rows whose start and end charpos
14323 occlude point. Only set w->cursor if we found a better
14324 approximation to the cursor position than we have from previously
14325 examined candidate rows belonging to the same continued line. */
14326 if (/* we already have a candidate row */
14327 w->cursor.vpos >= 0
14328 /* that candidate is not the row we are processing */
14329 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14330 /* Make sure cursor.vpos specifies a row whose start and end
14331 charpos occlude point, and it is valid candidate for being a
14332 cursor-row. This is because some callers of this function
14333 leave cursor.vpos at the row where the cursor was displayed
14334 during the last redisplay cycle. */
14335 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14336 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14337 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14338 {
14339 struct glyph *g1 =
14340 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14341
14342 /* Don't consider glyphs that are outside TEXT_AREA. */
14343 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14344 return 0;
14345 /* Keep the candidate whose buffer position is the closest to
14346 point or has the `cursor' property. */
14347 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14348 w->cursor.hpos >= 0
14349 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14350 && ((BUFFERP (g1->object)
14351 && (g1->charpos == pt_old /* an exact match always wins */
14352 || (BUFFERP (glyph->object)
14353 && eabs (g1->charpos - pt_old)
14354 < eabs (glyph->charpos - pt_old))))
14355 /* previous candidate is a glyph from a string that has
14356 a non-nil `cursor' property */
14357 || (STRINGP (g1->object)
14358 && (!NILP (Fget_char_property (make_number (g1->charpos),
14359 Qcursor, g1->object))
14360 /* previous candidate is from the same display
14361 string as this one, and the display string
14362 came from a text property */
14363 || (EQ (g1->object, glyph->object)
14364 && string_from_text_prop)
14365 /* this candidate is from newline and its
14366 position is not an exact match */
14367 || (INTEGERP (glyph->object)
14368 && glyph->charpos != pt_old)))))
14369 return 0;
14370 /* If this candidate gives an exact match, use that. */
14371 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14372 /* If this candidate is a glyph created for the
14373 terminating newline of a line, and point is on that
14374 newline, it wins because it's an exact match. */
14375 || (!row->continued_p
14376 && INTEGERP (glyph->object)
14377 && glyph->charpos == 0
14378 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14379 /* Otherwise, keep the candidate that comes from a row
14380 spanning less buffer positions. This may win when one or
14381 both candidate positions are on glyphs that came from
14382 display strings, for which we cannot compare buffer
14383 positions. */
14384 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14385 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14386 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14387 return 0;
14388 }
14389 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14390 w->cursor.x = x;
14391 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14392 w->cursor.y = row->y + dy;
14393
14394 if (w == XWINDOW (selected_window))
14395 {
14396 if (!row->continued_p
14397 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14398 && row->x == 0)
14399 {
14400 this_line_buffer = XBUFFER (w->buffer);
14401
14402 CHARPOS (this_line_start_pos)
14403 = MATRIX_ROW_START_CHARPOS (row) + delta;
14404 BYTEPOS (this_line_start_pos)
14405 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14406
14407 CHARPOS (this_line_end_pos)
14408 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14409 BYTEPOS (this_line_end_pos)
14410 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14411
14412 this_line_y = w->cursor.y;
14413 this_line_pixel_height = row->height;
14414 this_line_vpos = w->cursor.vpos;
14415 this_line_start_x = row->x;
14416 }
14417 else
14418 CHARPOS (this_line_start_pos) = 0;
14419 }
14420
14421 return 1;
14422 }
14423
14424
14425 /* Run window scroll functions, if any, for WINDOW with new window
14426 start STARTP. Sets the window start of WINDOW to that position.
14427
14428 We assume that the window's buffer is really current. */
14429
14430 static inline struct text_pos
14431 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14432 {
14433 struct window *w = XWINDOW (window);
14434 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14435
14436 if (current_buffer != XBUFFER (w->buffer))
14437 abort ();
14438
14439 if (!NILP (Vwindow_scroll_functions))
14440 {
14441 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14442 make_number (CHARPOS (startp)));
14443 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14444 /* In case the hook functions switch buffers. */
14445 if (current_buffer != XBUFFER (w->buffer))
14446 set_buffer_internal_1 (XBUFFER (w->buffer));
14447 }
14448
14449 return startp;
14450 }
14451
14452
14453 /* Make sure the line containing the cursor is fully visible.
14454 A value of 1 means there is nothing to be done.
14455 (Either the line is fully visible, or it cannot be made so,
14456 or we cannot tell.)
14457
14458 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14459 is higher than window.
14460
14461 A value of 0 means the caller should do scrolling
14462 as if point had gone off the screen. */
14463
14464 static int
14465 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14466 {
14467 struct glyph_matrix *matrix;
14468 struct glyph_row *row;
14469 int window_height;
14470
14471 if (!make_cursor_line_fully_visible_p)
14472 return 1;
14473
14474 /* It's not always possible to find the cursor, e.g, when a window
14475 is full of overlay strings. Don't do anything in that case. */
14476 if (w->cursor.vpos < 0)
14477 return 1;
14478
14479 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14480 row = MATRIX_ROW (matrix, w->cursor.vpos);
14481
14482 /* If the cursor row is not partially visible, there's nothing to do. */
14483 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14484 return 1;
14485
14486 /* If the row the cursor is in is taller than the window's height,
14487 it's not clear what to do, so do nothing. */
14488 window_height = window_box_height (w);
14489 if (row->height >= window_height)
14490 {
14491 if (!force_p || MINI_WINDOW_P (w)
14492 || w->vscroll || w->cursor.vpos == 0)
14493 return 1;
14494 }
14495 return 0;
14496 }
14497
14498
14499 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14500 non-zero means only WINDOW is redisplayed in redisplay_internal.
14501 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14502 in redisplay_window to bring a partially visible line into view in
14503 the case that only the cursor has moved.
14504
14505 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14506 last screen line's vertical height extends past the end of the screen.
14507
14508 Value is
14509
14510 1 if scrolling succeeded
14511
14512 0 if scrolling didn't find point.
14513
14514 -1 if new fonts have been loaded so that we must interrupt
14515 redisplay, adjust glyph matrices, and try again. */
14516
14517 enum
14518 {
14519 SCROLLING_SUCCESS,
14520 SCROLLING_FAILED,
14521 SCROLLING_NEED_LARGER_MATRICES
14522 };
14523
14524 /* If scroll-conservatively is more than this, never recenter.
14525
14526 If you change this, don't forget to update the doc string of
14527 `scroll-conservatively' and the Emacs manual. */
14528 #define SCROLL_LIMIT 100
14529
14530 static int
14531 try_scrolling (Lisp_Object window, int just_this_one_p,
14532 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14533 int temp_scroll_step, int last_line_misfit)
14534 {
14535 struct window *w = XWINDOW (window);
14536 struct frame *f = XFRAME (w->frame);
14537 struct text_pos pos, startp;
14538 struct it it;
14539 int this_scroll_margin, scroll_max, rc, height;
14540 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14541 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14542 Lisp_Object aggressive;
14543 /* We will never try scrolling more than this number of lines. */
14544 int scroll_limit = SCROLL_LIMIT;
14545
14546 #if GLYPH_DEBUG
14547 debug_method_add (w, "try_scrolling");
14548 #endif
14549
14550 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14551
14552 /* Compute scroll margin height in pixels. We scroll when point is
14553 within this distance from the top or bottom of the window. */
14554 if (scroll_margin > 0)
14555 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14556 * FRAME_LINE_HEIGHT (f);
14557 else
14558 this_scroll_margin = 0;
14559
14560 /* Force arg_scroll_conservatively to have a reasonable value, to
14561 avoid scrolling too far away with slow move_it_* functions. Note
14562 that the user can supply scroll-conservatively equal to
14563 `most-positive-fixnum', which can be larger than INT_MAX. */
14564 if (arg_scroll_conservatively > scroll_limit)
14565 {
14566 arg_scroll_conservatively = scroll_limit + 1;
14567 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14568 }
14569 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14570 /* Compute how much we should try to scroll maximally to bring
14571 point into view. */
14572 scroll_max = (max (scroll_step,
14573 max (arg_scroll_conservatively, temp_scroll_step))
14574 * FRAME_LINE_HEIGHT (f));
14575 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14576 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14577 /* We're trying to scroll because of aggressive scrolling but no
14578 scroll_step is set. Choose an arbitrary one. */
14579 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14580 else
14581 scroll_max = 0;
14582
14583 too_near_end:
14584
14585 /* Decide whether to scroll down. */
14586 if (PT > CHARPOS (startp))
14587 {
14588 int scroll_margin_y;
14589
14590 /* Compute the pixel ypos of the scroll margin, then move IT to
14591 either that ypos or PT, whichever comes first. */
14592 start_display (&it, w, startp);
14593 scroll_margin_y = it.last_visible_y - this_scroll_margin
14594 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14595 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14596 (MOVE_TO_POS | MOVE_TO_Y));
14597
14598 if (PT > CHARPOS (it.current.pos))
14599 {
14600 int y0 = line_bottom_y (&it);
14601 /* Compute how many pixels below window bottom to stop searching
14602 for PT. This avoids costly search for PT that is far away if
14603 the user limited scrolling by a small number of lines, but
14604 always finds PT if scroll_conservatively is set to a large
14605 number, such as most-positive-fixnum. */
14606 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14607 int y_to_move = it.last_visible_y + slack;
14608
14609 /* Compute the distance from the scroll margin to PT or to
14610 the scroll limit, whichever comes first. This should
14611 include the height of the cursor line, to make that line
14612 fully visible. */
14613 move_it_to (&it, PT, -1, y_to_move,
14614 -1, MOVE_TO_POS | MOVE_TO_Y);
14615 dy = line_bottom_y (&it) - y0;
14616
14617 if (dy > scroll_max)
14618 return SCROLLING_FAILED;
14619
14620 if (dy > 0)
14621 scroll_down_p = 1;
14622 }
14623 }
14624
14625 if (scroll_down_p)
14626 {
14627 /* Point is in or below the bottom scroll margin, so move the
14628 window start down. If scrolling conservatively, move it just
14629 enough down to make point visible. If scroll_step is set,
14630 move it down by scroll_step. */
14631 if (arg_scroll_conservatively)
14632 amount_to_scroll
14633 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14634 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14635 else if (scroll_step || temp_scroll_step)
14636 amount_to_scroll = scroll_max;
14637 else
14638 {
14639 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14640 height = WINDOW_BOX_TEXT_HEIGHT (w);
14641 if (NUMBERP (aggressive))
14642 {
14643 double float_amount = XFLOATINT (aggressive) * height;
14644 amount_to_scroll = float_amount;
14645 if (amount_to_scroll == 0 && float_amount > 0)
14646 amount_to_scroll = 1;
14647 /* Don't let point enter the scroll margin near top of
14648 the window. */
14649 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14650 amount_to_scroll = height - 2*this_scroll_margin + dy;
14651 }
14652 }
14653
14654 if (amount_to_scroll <= 0)
14655 return SCROLLING_FAILED;
14656
14657 start_display (&it, w, startp);
14658 if (arg_scroll_conservatively <= scroll_limit)
14659 move_it_vertically (&it, amount_to_scroll);
14660 else
14661 {
14662 /* Extra precision for users who set scroll-conservatively
14663 to a large number: make sure the amount we scroll
14664 the window start is never less than amount_to_scroll,
14665 which was computed as distance from window bottom to
14666 point. This matters when lines at window top and lines
14667 below window bottom have different height. */
14668 struct it it1;
14669 void *it1data = NULL;
14670 /* We use a temporary it1 because line_bottom_y can modify
14671 its argument, if it moves one line down; see there. */
14672 int start_y;
14673
14674 SAVE_IT (it1, it, it1data);
14675 start_y = line_bottom_y (&it1);
14676 do {
14677 RESTORE_IT (&it, &it, it1data);
14678 move_it_by_lines (&it, 1);
14679 SAVE_IT (it1, it, it1data);
14680 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14681 }
14682
14683 /* If STARTP is unchanged, move it down another screen line. */
14684 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14685 move_it_by_lines (&it, 1);
14686 startp = it.current.pos;
14687 }
14688 else
14689 {
14690 struct text_pos scroll_margin_pos = startp;
14691
14692 /* See if point is inside the scroll margin at the top of the
14693 window. */
14694 if (this_scroll_margin)
14695 {
14696 start_display (&it, w, startp);
14697 move_it_vertically (&it, this_scroll_margin);
14698 scroll_margin_pos = it.current.pos;
14699 }
14700
14701 if (PT < CHARPOS (scroll_margin_pos))
14702 {
14703 /* Point is in the scroll margin at the top of the window or
14704 above what is displayed in the window. */
14705 int y0, y_to_move;
14706
14707 /* Compute the vertical distance from PT to the scroll
14708 margin position. Move as far as scroll_max allows, or
14709 one screenful, or 10 screen lines, whichever is largest.
14710 Give up if distance is greater than scroll_max. */
14711 SET_TEXT_POS (pos, PT, PT_BYTE);
14712 start_display (&it, w, pos);
14713 y0 = it.current_y;
14714 y_to_move = max (it.last_visible_y,
14715 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14716 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14717 y_to_move, -1,
14718 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14719 dy = it.current_y - y0;
14720 if (dy > scroll_max)
14721 return SCROLLING_FAILED;
14722
14723 /* Compute new window start. */
14724 start_display (&it, w, startp);
14725
14726 if (arg_scroll_conservatively)
14727 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14728 max (scroll_step, temp_scroll_step));
14729 else if (scroll_step || temp_scroll_step)
14730 amount_to_scroll = scroll_max;
14731 else
14732 {
14733 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14734 height = WINDOW_BOX_TEXT_HEIGHT (w);
14735 if (NUMBERP (aggressive))
14736 {
14737 double float_amount = XFLOATINT (aggressive) * height;
14738 amount_to_scroll = float_amount;
14739 if (amount_to_scroll == 0 && float_amount > 0)
14740 amount_to_scroll = 1;
14741 amount_to_scroll -=
14742 this_scroll_margin - dy - FRAME_LINE_HEIGHT (f);
14743 /* Don't let point enter the scroll margin near
14744 bottom of the window. */
14745 if (amount_to_scroll > height - 2*this_scroll_margin + dy)
14746 amount_to_scroll = height - 2*this_scroll_margin + dy;
14747 }
14748 }
14749
14750 if (amount_to_scroll <= 0)
14751 return SCROLLING_FAILED;
14752
14753 move_it_vertically_backward (&it, amount_to_scroll);
14754 startp = it.current.pos;
14755 }
14756 }
14757
14758 /* Run window scroll functions. */
14759 startp = run_window_scroll_functions (window, startp);
14760
14761 /* Display the window. Give up if new fonts are loaded, or if point
14762 doesn't appear. */
14763 if (!try_window (window, startp, 0))
14764 rc = SCROLLING_NEED_LARGER_MATRICES;
14765 else if (w->cursor.vpos < 0)
14766 {
14767 clear_glyph_matrix (w->desired_matrix);
14768 rc = SCROLLING_FAILED;
14769 }
14770 else
14771 {
14772 /* Maybe forget recorded base line for line number display. */
14773 if (!just_this_one_p
14774 || current_buffer->clip_changed
14775 || BEG_UNCHANGED < CHARPOS (startp))
14776 w->base_line_number = Qnil;
14777
14778 /* If cursor ends up on a partially visible line,
14779 treat that as being off the bottom of the screen. */
14780 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14781 /* It's possible that the cursor is on the first line of the
14782 buffer, which is partially obscured due to a vscroll
14783 (Bug#7537). In that case, avoid looping forever . */
14784 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14785 {
14786 clear_glyph_matrix (w->desired_matrix);
14787 ++extra_scroll_margin_lines;
14788 goto too_near_end;
14789 }
14790 rc = SCROLLING_SUCCESS;
14791 }
14792
14793 return rc;
14794 }
14795
14796
14797 /* Compute a suitable window start for window W if display of W starts
14798 on a continuation line. Value is non-zero if a new window start
14799 was computed.
14800
14801 The new window start will be computed, based on W's width, starting
14802 from the start of the continued line. It is the start of the
14803 screen line with the minimum distance from the old start W->start. */
14804
14805 static int
14806 compute_window_start_on_continuation_line (struct window *w)
14807 {
14808 struct text_pos pos, start_pos;
14809 int window_start_changed_p = 0;
14810
14811 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14812
14813 /* If window start is on a continuation line... Window start may be
14814 < BEGV in case there's invisible text at the start of the
14815 buffer (M-x rmail, for example). */
14816 if (CHARPOS (start_pos) > BEGV
14817 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14818 {
14819 struct it it;
14820 struct glyph_row *row;
14821
14822 /* Handle the case that the window start is out of range. */
14823 if (CHARPOS (start_pos) < BEGV)
14824 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14825 else if (CHARPOS (start_pos) > ZV)
14826 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14827
14828 /* Find the start of the continued line. This should be fast
14829 because scan_buffer is fast (newline cache). */
14830 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14831 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14832 row, DEFAULT_FACE_ID);
14833 reseat_at_previous_visible_line_start (&it);
14834
14835 /* If the line start is "too far" away from the window start,
14836 say it takes too much time to compute a new window start. */
14837 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14838 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14839 {
14840 int min_distance, distance;
14841
14842 /* Move forward by display lines to find the new window
14843 start. If window width was enlarged, the new start can
14844 be expected to be > the old start. If window width was
14845 decreased, the new window start will be < the old start.
14846 So, we're looking for the display line start with the
14847 minimum distance from the old window start. */
14848 pos = it.current.pos;
14849 min_distance = INFINITY;
14850 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14851 distance < min_distance)
14852 {
14853 min_distance = distance;
14854 pos = it.current.pos;
14855 move_it_by_lines (&it, 1);
14856 }
14857
14858 /* Set the window start there. */
14859 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14860 window_start_changed_p = 1;
14861 }
14862 }
14863
14864 return window_start_changed_p;
14865 }
14866
14867
14868 /* Try cursor movement in case text has not changed in window WINDOW,
14869 with window start STARTP. Value is
14870
14871 CURSOR_MOVEMENT_SUCCESS if successful
14872
14873 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14874
14875 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14876 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14877 we want to scroll as if scroll-step were set to 1. See the code.
14878
14879 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14880 which case we have to abort this redisplay, and adjust matrices
14881 first. */
14882
14883 enum
14884 {
14885 CURSOR_MOVEMENT_SUCCESS,
14886 CURSOR_MOVEMENT_CANNOT_BE_USED,
14887 CURSOR_MOVEMENT_MUST_SCROLL,
14888 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14889 };
14890
14891 static int
14892 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14893 {
14894 struct window *w = XWINDOW (window);
14895 struct frame *f = XFRAME (w->frame);
14896 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14897
14898 #if GLYPH_DEBUG
14899 if (inhibit_try_cursor_movement)
14900 return rc;
14901 #endif
14902
14903 /* Handle case where text has not changed, only point, and it has
14904 not moved off the frame. */
14905 if (/* Point may be in this window. */
14906 PT >= CHARPOS (startp)
14907 /* Selective display hasn't changed. */
14908 && !current_buffer->clip_changed
14909 /* Function force-mode-line-update is used to force a thorough
14910 redisplay. It sets either windows_or_buffers_changed or
14911 update_mode_lines. So don't take a shortcut here for these
14912 cases. */
14913 && !update_mode_lines
14914 && !windows_or_buffers_changed
14915 && !cursor_type_changed
14916 /* Can't use this case if highlighting a region. When a
14917 region exists, cursor movement has to do more than just
14918 set the cursor. */
14919 && !(!NILP (Vtransient_mark_mode)
14920 && !NILP (BVAR (current_buffer, mark_active)))
14921 && NILP (w->region_showing)
14922 && NILP (Vshow_trailing_whitespace)
14923 /* Right after splitting windows, last_point may be nil. */
14924 && INTEGERP (w->last_point)
14925 /* This code is not used for mini-buffer for the sake of the case
14926 of redisplaying to replace an echo area message; since in
14927 that case the mini-buffer contents per se are usually
14928 unchanged. This code is of no real use in the mini-buffer
14929 since the handling of this_line_start_pos, etc., in redisplay
14930 handles the same cases. */
14931 && !EQ (window, minibuf_window)
14932 /* When splitting windows or for new windows, it happens that
14933 redisplay is called with a nil window_end_vpos or one being
14934 larger than the window. This should really be fixed in
14935 window.c. I don't have this on my list, now, so we do
14936 approximately the same as the old redisplay code. --gerd. */
14937 && INTEGERP (w->window_end_vpos)
14938 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14939 && (FRAME_WINDOW_P (f)
14940 || !overlay_arrow_in_current_buffer_p ()))
14941 {
14942 int this_scroll_margin, top_scroll_margin;
14943 struct glyph_row *row = NULL;
14944
14945 #if GLYPH_DEBUG
14946 debug_method_add (w, "cursor movement");
14947 #endif
14948
14949 /* Scroll if point within this distance from the top or bottom
14950 of the window. This is a pixel value. */
14951 if (scroll_margin > 0)
14952 {
14953 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14954 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14955 }
14956 else
14957 this_scroll_margin = 0;
14958
14959 top_scroll_margin = this_scroll_margin;
14960 if (WINDOW_WANTS_HEADER_LINE_P (w))
14961 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14962
14963 /* Start with the row the cursor was displayed during the last
14964 not paused redisplay. Give up if that row is not valid. */
14965 if (w->last_cursor.vpos < 0
14966 || w->last_cursor.vpos >= w->current_matrix->nrows)
14967 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14968 else
14969 {
14970 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14971 if (row->mode_line_p)
14972 ++row;
14973 if (!row->enabled_p)
14974 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14975 }
14976
14977 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14978 {
14979 int scroll_p = 0, must_scroll = 0;
14980 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14981
14982 if (PT > XFASTINT (w->last_point))
14983 {
14984 /* Point has moved forward. */
14985 while (MATRIX_ROW_END_CHARPOS (row) < PT
14986 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14987 {
14988 xassert (row->enabled_p);
14989 ++row;
14990 }
14991
14992 /* If the end position of a row equals the start
14993 position of the next row, and PT is at that position,
14994 we would rather display cursor in the next line. */
14995 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14996 && MATRIX_ROW_END_CHARPOS (row) == PT
14997 && row < w->current_matrix->rows
14998 + w->current_matrix->nrows - 1
14999 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15000 && !cursor_row_p (row))
15001 ++row;
15002
15003 /* If within the scroll margin, scroll. Note that
15004 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15005 the next line would be drawn, and that
15006 this_scroll_margin can be zero. */
15007 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15008 || PT > MATRIX_ROW_END_CHARPOS (row)
15009 /* Line is completely visible last line in window
15010 and PT is to be set in the next line. */
15011 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15012 && PT == MATRIX_ROW_END_CHARPOS (row)
15013 && !row->ends_at_zv_p
15014 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15015 scroll_p = 1;
15016 }
15017 else if (PT < XFASTINT (w->last_point))
15018 {
15019 /* Cursor has to be moved backward. Note that PT >=
15020 CHARPOS (startp) because of the outer if-statement. */
15021 while (!row->mode_line_p
15022 && (MATRIX_ROW_START_CHARPOS (row) > PT
15023 || (MATRIX_ROW_START_CHARPOS (row) == PT
15024 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15025 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15026 row > w->current_matrix->rows
15027 && (row-1)->ends_in_newline_from_string_p))))
15028 && (row->y > top_scroll_margin
15029 || CHARPOS (startp) == BEGV))
15030 {
15031 xassert (row->enabled_p);
15032 --row;
15033 }
15034
15035 /* Consider the following case: Window starts at BEGV,
15036 there is invisible, intangible text at BEGV, so that
15037 display starts at some point START > BEGV. It can
15038 happen that we are called with PT somewhere between
15039 BEGV and START. Try to handle that case. */
15040 if (row < w->current_matrix->rows
15041 || row->mode_line_p)
15042 {
15043 row = w->current_matrix->rows;
15044 if (row->mode_line_p)
15045 ++row;
15046 }
15047
15048 /* Due to newlines in overlay strings, we may have to
15049 skip forward over overlay strings. */
15050 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15051 && MATRIX_ROW_END_CHARPOS (row) == PT
15052 && !cursor_row_p (row))
15053 ++row;
15054
15055 /* If within the scroll margin, scroll. */
15056 if (row->y < top_scroll_margin
15057 && CHARPOS (startp) != BEGV)
15058 scroll_p = 1;
15059 }
15060 else
15061 {
15062 /* Cursor did not move. So don't scroll even if cursor line
15063 is partially visible, as it was so before. */
15064 rc = CURSOR_MOVEMENT_SUCCESS;
15065 }
15066
15067 if (PT < MATRIX_ROW_START_CHARPOS (row)
15068 || PT > MATRIX_ROW_END_CHARPOS (row))
15069 {
15070 /* if PT is not in the glyph row, give up. */
15071 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15072 must_scroll = 1;
15073 }
15074 else if (rc != CURSOR_MOVEMENT_SUCCESS
15075 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15076 {
15077 struct glyph_row *row1;
15078
15079 /* If rows are bidi-reordered and point moved, back up
15080 until we find a row that does not belong to a
15081 continuation line. This is because we must consider
15082 all rows of a continued line as candidates for the
15083 new cursor positioning, since row start and end
15084 positions change non-linearly with vertical position
15085 in such rows. */
15086 /* FIXME: Revisit this when glyph ``spilling'' in
15087 continuation lines' rows is implemented for
15088 bidi-reordered rows. */
15089 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15090 MATRIX_ROW_CONTINUATION_LINE_P (row);
15091 --row)
15092 {
15093 /* If we hit the beginning of the displayed portion
15094 without finding the first row of a continued
15095 line, give up. */
15096 if (row <= row1)
15097 {
15098 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15099 break;
15100 }
15101 xassert (row->enabled_p);
15102 }
15103 }
15104 if (must_scroll)
15105 ;
15106 else if (rc != CURSOR_MOVEMENT_SUCCESS
15107 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15108 /* Make sure this isn't a header line by any chance, since
15109 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15110 && !row->mode_line_p
15111 && make_cursor_line_fully_visible_p)
15112 {
15113 if (PT == MATRIX_ROW_END_CHARPOS (row)
15114 && !row->ends_at_zv_p
15115 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15116 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15117 else if (row->height > window_box_height (w))
15118 {
15119 /* If we end up in a partially visible line, let's
15120 make it fully visible, except when it's taller
15121 than the window, in which case we can't do much
15122 about it. */
15123 *scroll_step = 1;
15124 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15125 }
15126 else
15127 {
15128 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15129 if (!cursor_row_fully_visible_p (w, 0, 1))
15130 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15131 else
15132 rc = CURSOR_MOVEMENT_SUCCESS;
15133 }
15134 }
15135 else if (scroll_p)
15136 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15137 else if (rc != CURSOR_MOVEMENT_SUCCESS
15138 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15139 {
15140 /* With bidi-reordered rows, there could be more than
15141 one candidate row whose start and end positions
15142 occlude point. We need to let set_cursor_from_row
15143 find the best candidate. */
15144 /* FIXME: Revisit this when glyph ``spilling'' in
15145 continuation lines' rows is implemented for
15146 bidi-reordered rows. */
15147 int rv = 0;
15148
15149 do
15150 {
15151 int at_zv_p = 0, exact_match_p = 0;
15152
15153 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15154 && PT <= MATRIX_ROW_END_CHARPOS (row)
15155 && cursor_row_p (row))
15156 rv |= set_cursor_from_row (w, row, w->current_matrix,
15157 0, 0, 0, 0);
15158 /* As soon as we've found the exact match for point,
15159 or the first suitable row whose ends_at_zv_p flag
15160 is set, we are done. */
15161 at_zv_p =
15162 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15163 if (rv && !at_zv_p
15164 && w->cursor.hpos >= 0
15165 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15166 w->cursor.vpos))
15167 {
15168 struct glyph_row *candidate =
15169 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15170 struct glyph *g =
15171 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15172 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15173
15174 exact_match_p =
15175 (BUFFERP (g->object) && g->charpos == PT)
15176 || (INTEGERP (g->object)
15177 && (g->charpos == PT
15178 || (g->charpos == 0 && endpos - 1 == PT)));
15179 }
15180 if (rv && (at_zv_p || exact_match_p))
15181 {
15182 rc = CURSOR_MOVEMENT_SUCCESS;
15183 break;
15184 }
15185 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15186 break;
15187 ++row;
15188 }
15189 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15190 || row->continued_p)
15191 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15192 || (MATRIX_ROW_START_CHARPOS (row) == PT
15193 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15194 /* If we didn't find any candidate rows, or exited the
15195 loop before all the candidates were examined, signal
15196 to the caller that this method failed. */
15197 if (rc != CURSOR_MOVEMENT_SUCCESS
15198 && !(rv
15199 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15200 && !row->continued_p))
15201 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15202 else if (rv)
15203 rc = CURSOR_MOVEMENT_SUCCESS;
15204 }
15205 else
15206 {
15207 do
15208 {
15209 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15210 {
15211 rc = CURSOR_MOVEMENT_SUCCESS;
15212 break;
15213 }
15214 ++row;
15215 }
15216 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15217 && MATRIX_ROW_START_CHARPOS (row) == PT
15218 && cursor_row_p (row));
15219 }
15220 }
15221 }
15222
15223 return rc;
15224 }
15225
15226 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15227 static
15228 #endif
15229 void
15230 set_vertical_scroll_bar (struct window *w)
15231 {
15232 ptrdiff_t start, end, whole;
15233
15234 /* Calculate the start and end positions for the current window.
15235 At some point, it would be nice to choose between scrollbars
15236 which reflect the whole buffer size, with special markers
15237 indicating narrowing, and scrollbars which reflect only the
15238 visible region.
15239
15240 Note that mini-buffers sometimes aren't displaying any text. */
15241 if (!MINI_WINDOW_P (w)
15242 || (w == XWINDOW (minibuf_window)
15243 && NILP (echo_area_buffer[0])))
15244 {
15245 struct buffer *buf = XBUFFER (w->buffer);
15246 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15247 start = marker_position (w->start) - BUF_BEGV (buf);
15248 /* I don't think this is guaranteed to be right. For the
15249 moment, we'll pretend it is. */
15250 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15251
15252 if (end < start)
15253 end = start;
15254 if (whole < (end - start))
15255 whole = end - start;
15256 }
15257 else
15258 start = end = whole = 0;
15259
15260 /* Indicate what this scroll bar ought to be displaying now. */
15261 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15262 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15263 (w, end - start, whole, start);
15264 }
15265
15266
15267 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15268 selected_window is redisplayed.
15269
15270 We can return without actually redisplaying the window if
15271 fonts_changed_p is nonzero. In that case, redisplay_internal will
15272 retry. */
15273
15274 static void
15275 redisplay_window (Lisp_Object window, int just_this_one_p)
15276 {
15277 struct window *w = XWINDOW (window);
15278 struct frame *f = XFRAME (w->frame);
15279 struct buffer *buffer = XBUFFER (w->buffer);
15280 struct buffer *old = current_buffer;
15281 struct text_pos lpoint, opoint, startp;
15282 int update_mode_line;
15283 int tem;
15284 struct it it;
15285 /* Record it now because it's overwritten. */
15286 int current_matrix_up_to_date_p = 0;
15287 int used_current_matrix_p = 0;
15288 /* This is less strict than current_matrix_up_to_date_p.
15289 It indicates that the buffer contents and narrowing are unchanged. */
15290 int buffer_unchanged_p = 0;
15291 int temp_scroll_step = 0;
15292 ptrdiff_t count = SPECPDL_INDEX ();
15293 int rc;
15294 int centering_position = -1;
15295 int last_line_misfit = 0;
15296 ptrdiff_t beg_unchanged, end_unchanged;
15297
15298 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15299 opoint = lpoint;
15300
15301 /* W must be a leaf window here. */
15302 xassert (!NILP (w->buffer));
15303 #if GLYPH_DEBUG
15304 *w->desired_matrix->method = 0;
15305 #endif
15306
15307 restart:
15308 reconsider_clip_changes (w, buffer);
15309
15310 /* Has the mode line to be updated? */
15311 update_mode_line = (w->update_mode_line
15312 || update_mode_lines
15313 || buffer->clip_changed
15314 || buffer->prevent_redisplay_optimizations_p);
15315
15316 if (MINI_WINDOW_P (w))
15317 {
15318 if (w == XWINDOW (echo_area_window)
15319 && !NILP (echo_area_buffer[0]))
15320 {
15321 if (update_mode_line)
15322 /* We may have to update a tty frame's menu bar or a
15323 tool-bar. Example `M-x C-h C-h C-g'. */
15324 goto finish_menu_bars;
15325 else
15326 /* We've already displayed the echo area glyphs in this window. */
15327 goto finish_scroll_bars;
15328 }
15329 else if ((w != XWINDOW (minibuf_window)
15330 || minibuf_level == 0)
15331 /* When buffer is nonempty, redisplay window normally. */
15332 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15333 /* Quail displays non-mini buffers in minibuffer window.
15334 In that case, redisplay the window normally. */
15335 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15336 {
15337 /* W is a mini-buffer window, but it's not active, so clear
15338 it. */
15339 int yb = window_text_bottom_y (w);
15340 struct glyph_row *row;
15341 int y;
15342
15343 for (y = 0, row = w->desired_matrix->rows;
15344 y < yb;
15345 y += row->height, ++row)
15346 blank_row (w, row, y);
15347 goto finish_scroll_bars;
15348 }
15349
15350 clear_glyph_matrix (w->desired_matrix);
15351 }
15352
15353 /* Otherwise set up data on this window; select its buffer and point
15354 value. */
15355 /* Really select the buffer, for the sake of buffer-local
15356 variables. */
15357 set_buffer_internal_1 (XBUFFER (w->buffer));
15358
15359 current_matrix_up_to_date_p
15360 = (!NILP (w->window_end_valid)
15361 && !current_buffer->clip_changed
15362 && !current_buffer->prevent_redisplay_optimizations_p
15363 && XFASTINT (w->last_modified) >= MODIFF
15364 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15365
15366 /* Run the window-bottom-change-functions
15367 if it is possible that the text on the screen has changed
15368 (either due to modification of the text, or any other reason). */
15369 if (!current_matrix_up_to_date_p
15370 && !NILP (Vwindow_text_change_functions))
15371 {
15372 safe_run_hooks (Qwindow_text_change_functions);
15373 goto restart;
15374 }
15375
15376 beg_unchanged = BEG_UNCHANGED;
15377 end_unchanged = END_UNCHANGED;
15378
15379 SET_TEXT_POS (opoint, PT, PT_BYTE);
15380
15381 specbind (Qinhibit_point_motion_hooks, Qt);
15382
15383 buffer_unchanged_p
15384 = (!NILP (w->window_end_valid)
15385 && !current_buffer->clip_changed
15386 && XFASTINT (w->last_modified) >= MODIFF
15387 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF);
15388
15389 /* When windows_or_buffers_changed is non-zero, we can't rely on
15390 the window end being valid, so set it to nil there. */
15391 if (windows_or_buffers_changed)
15392 {
15393 /* If window starts on a continuation line, maybe adjust the
15394 window start in case the window's width changed. */
15395 if (XMARKER (w->start)->buffer == current_buffer)
15396 compute_window_start_on_continuation_line (w);
15397
15398 w->window_end_valid = Qnil;
15399 }
15400
15401 /* Some sanity checks. */
15402 CHECK_WINDOW_END (w);
15403 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15404 abort ();
15405 if (BYTEPOS (opoint) < CHARPOS (opoint))
15406 abort ();
15407
15408 /* If %c is in mode line, update it if needed. */
15409 if (!NILP (w->column_number_displayed)
15410 /* This alternative quickly identifies a common case
15411 where no change is needed. */
15412 && !(PT == XFASTINT (w->last_point)
15413 && XFASTINT (w->last_modified) >= MODIFF
15414 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)
15415 && (XFASTINT (w->column_number_displayed) != current_column ()))
15416 update_mode_line = 1;
15417
15418 /* Count number of windows showing the selected buffer. An indirect
15419 buffer counts as its base buffer. */
15420 if (!just_this_one_p)
15421 {
15422 struct buffer *current_base, *window_base;
15423 current_base = current_buffer;
15424 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15425 if (current_base->base_buffer)
15426 current_base = current_base->base_buffer;
15427 if (window_base->base_buffer)
15428 window_base = window_base->base_buffer;
15429 if (current_base == window_base)
15430 buffer_shared++;
15431 }
15432
15433 /* Point refers normally to the selected window. For any other
15434 window, set up appropriate value. */
15435 if (!EQ (window, selected_window))
15436 {
15437 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15438 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15439 if (new_pt < BEGV)
15440 {
15441 new_pt = BEGV;
15442 new_pt_byte = BEGV_BYTE;
15443 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15444 }
15445 else if (new_pt > (ZV - 1))
15446 {
15447 new_pt = ZV;
15448 new_pt_byte = ZV_BYTE;
15449 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15450 }
15451
15452 /* We don't use SET_PT so that the point-motion hooks don't run. */
15453 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15454 }
15455
15456 /* If any of the character widths specified in the display table
15457 have changed, invalidate the width run cache. It's true that
15458 this may be a bit late to catch such changes, but the rest of
15459 redisplay goes (non-fatally) haywire when the display table is
15460 changed, so why should we worry about doing any better? */
15461 if (current_buffer->width_run_cache)
15462 {
15463 struct Lisp_Char_Table *disptab = buffer_display_table ();
15464
15465 if (! disptab_matches_widthtab (disptab,
15466 XVECTOR (BVAR (current_buffer, width_table))))
15467 {
15468 invalidate_region_cache (current_buffer,
15469 current_buffer->width_run_cache,
15470 BEG, Z);
15471 recompute_width_table (current_buffer, disptab);
15472 }
15473 }
15474
15475 /* If window-start is screwed up, choose a new one. */
15476 if (XMARKER (w->start)->buffer != current_buffer)
15477 goto recenter;
15478
15479 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15480
15481 /* If someone specified a new starting point but did not insist,
15482 check whether it can be used. */
15483 if (w->optional_new_start
15484 && CHARPOS (startp) >= BEGV
15485 && CHARPOS (startp) <= ZV)
15486 {
15487 w->optional_new_start = 0;
15488 start_display (&it, w, startp);
15489 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15490 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15491 if (IT_CHARPOS (it) == PT)
15492 w->force_start = 1;
15493 /* IT may overshoot PT if text at PT is invisible. */
15494 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15495 w->force_start = 1;
15496 }
15497
15498 force_start:
15499
15500 /* Handle case where place to start displaying has been specified,
15501 unless the specified location is outside the accessible range. */
15502 if (w->force_start || w->frozen_window_start_p)
15503 {
15504 /* We set this later on if we have to adjust point. */
15505 int new_vpos = -1;
15506
15507 w->force_start = 0;
15508 w->vscroll = 0;
15509 w->window_end_valid = Qnil;
15510
15511 /* Forget any recorded base line for line number display. */
15512 if (!buffer_unchanged_p)
15513 w->base_line_number = Qnil;
15514
15515 /* Redisplay the mode line. Select the buffer properly for that.
15516 Also, run the hook window-scroll-functions
15517 because we have scrolled. */
15518 /* Note, we do this after clearing force_start because
15519 if there's an error, it is better to forget about force_start
15520 than to get into an infinite loop calling the hook functions
15521 and having them get more errors. */
15522 if (!update_mode_line
15523 || ! NILP (Vwindow_scroll_functions))
15524 {
15525 update_mode_line = 1;
15526 w->update_mode_line = 1;
15527 startp = run_window_scroll_functions (window, startp);
15528 }
15529
15530 w->last_modified = make_number (0);
15531 w->last_overlay_modified = make_number (0);
15532 if (CHARPOS (startp) < BEGV)
15533 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15534 else if (CHARPOS (startp) > ZV)
15535 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15536
15537 /* Redisplay, then check if cursor has been set during the
15538 redisplay. Give up if new fonts were loaded. */
15539 /* We used to issue a CHECK_MARGINS argument to try_window here,
15540 but this causes scrolling to fail when point begins inside
15541 the scroll margin (bug#148) -- cyd */
15542 if (!try_window (window, startp, 0))
15543 {
15544 w->force_start = 1;
15545 clear_glyph_matrix (w->desired_matrix);
15546 goto need_larger_matrices;
15547 }
15548
15549 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15550 {
15551 /* If point does not appear, try to move point so it does
15552 appear. The desired matrix has been built above, so we
15553 can use it here. */
15554 new_vpos = window_box_height (w) / 2;
15555 }
15556
15557 if (!cursor_row_fully_visible_p (w, 0, 0))
15558 {
15559 /* Point does appear, but on a line partly visible at end of window.
15560 Move it back to a fully-visible line. */
15561 new_vpos = window_box_height (w);
15562 }
15563
15564 /* If we need to move point for either of the above reasons,
15565 now actually do it. */
15566 if (new_vpos >= 0)
15567 {
15568 struct glyph_row *row;
15569
15570 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15571 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15572 ++row;
15573
15574 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15575 MATRIX_ROW_START_BYTEPOS (row));
15576
15577 if (w != XWINDOW (selected_window))
15578 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15579 else if (current_buffer == old)
15580 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15581
15582 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15583
15584 /* If we are highlighting the region, then we just changed
15585 the region, so redisplay to show it. */
15586 if (!NILP (Vtransient_mark_mode)
15587 && !NILP (BVAR (current_buffer, mark_active)))
15588 {
15589 clear_glyph_matrix (w->desired_matrix);
15590 if (!try_window (window, startp, 0))
15591 goto need_larger_matrices;
15592 }
15593 }
15594
15595 #if GLYPH_DEBUG
15596 debug_method_add (w, "forced window start");
15597 #endif
15598 goto done;
15599 }
15600
15601 /* Handle case where text has not changed, only point, and it has
15602 not moved off the frame, and we are not retrying after hscroll.
15603 (current_matrix_up_to_date_p is nonzero when retrying.) */
15604 if (current_matrix_up_to_date_p
15605 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15606 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15607 {
15608 switch (rc)
15609 {
15610 case CURSOR_MOVEMENT_SUCCESS:
15611 used_current_matrix_p = 1;
15612 goto done;
15613
15614 case CURSOR_MOVEMENT_MUST_SCROLL:
15615 goto try_to_scroll;
15616
15617 default:
15618 abort ();
15619 }
15620 }
15621 /* If current starting point was originally the beginning of a line
15622 but no longer is, find a new starting point. */
15623 else if (w->start_at_line_beg
15624 && !(CHARPOS (startp) <= BEGV
15625 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15626 {
15627 #if GLYPH_DEBUG
15628 debug_method_add (w, "recenter 1");
15629 #endif
15630 goto recenter;
15631 }
15632
15633 /* Try scrolling with try_window_id. Value is > 0 if update has
15634 been done, it is -1 if we know that the same window start will
15635 not work. It is 0 if unsuccessful for some other reason. */
15636 else if ((tem = try_window_id (w)) != 0)
15637 {
15638 #if GLYPH_DEBUG
15639 debug_method_add (w, "try_window_id %d", tem);
15640 #endif
15641
15642 if (fonts_changed_p)
15643 goto need_larger_matrices;
15644 if (tem > 0)
15645 goto done;
15646
15647 /* Otherwise try_window_id has returned -1 which means that we
15648 don't want the alternative below this comment to execute. */
15649 }
15650 else if (CHARPOS (startp) >= BEGV
15651 && CHARPOS (startp) <= ZV
15652 && PT >= CHARPOS (startp)
15653 && (CHARPOS (startp) < ZV
15654 /* Avoid starting at end of buffer. */
15655 || CHARPOS (startp) == BEGV
15656 || (XFASTINT (w->last_modified) >= MODIFF
15657 && XFASTINT (w->last_overlay_modified) >= OVERLAY_MODIFF)))
15658 {
15659 int d1, d2, d3, d4, d5, d6;
15660
15661 /* If first window line is a continuation line, and window start
15662 is inside the modified region, but the first change is before
15663 current window start, we must select a new window start.
15664
15665 However, if this is the result of a down-mouse event (e.g. by
15666 extending the mouse-drag-overlay), we don't want to select a
15667 new window start, since that would change the position under
15668 the mouse, resulting in an unwanted mouse-movement rather
15669 than a simple mouse-click. */
15670 if (!w->start_at_line_beg
15671 && NILP (do_mouse_tracking)
15672 && CHARPOS (startp) > BEGV
15673 && CHARPOS (startp) > BEG + beg_unchanged
15674 && CHARPOS (startp) <= Z - end_unchanged
15675 /* Even if w->start_at_line_beg is nil, a new window may
15676 start at a line_beg, since that's how set_buffer_window
15677 sets it. So, we need to check the return value of
15678 compute_window_start_on_continuation_line. (See also
15679 bug#197). */
15680 && XMARKER (w->start)->buffer == current_buffer
15681 && compute_window_start_on_continuation_line (w)
15682 /* It doesn't make sense to force the window start like we
15683 do at label force_start if it is already known that point
15684 will not be visible in the resulting window, because
15685 doing so will move point from its correct position
15686 instead of scrolling the window to bring point into view.
15687 See bug#9324. */
15688 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15689 {
15690 w->force_start = 1;
15691 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15692 goto force_start;
15693 }
15694
15695 #if GLYPH_DEBUG
15696 debug_method_add (w, "same window start");
15697 #endif
15698
15699 /* Try to redisplay starting at same place as before.
15700 If point has not moved off frame, accept the results. */
15701 if (!current_matrix_up_to_date_p
15702 /* Don't use try_window_reusing_current_matrix in this case
15703 because a window scroll function can have changed the
15704 buffer. */
15705 || !NILP (Vwindow_scroll_functions)
15706 || MINI_WINDOW_P (w)
15707 || !(used_current_matrix_p
15708 = try_window_reusing_current_matrix (w)))
15709 {
15710 IF_DEBUG (debug_method_add (w, "1"));
15711 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15712 /* -1 means we need to scroll.
15713 0 means we need new matrices, but fonts_changed_p
15714 is set in that case, so we will detect it below. */
15715 goto try_to_scroll;
15716 }
15717
15718 if (fonts_changed_p)
15719 goto need_larger_matrices;
15720
15721 if (w->cursor.vpos >= 0)
15722 {
15723 if (!just_this_one_p
15724 || current_buffer->clip_changed
15725 || BEG_UNCHANGED < CHARPOS (startp))
15726 /* Forget any recorded base line for line number display. */
15727 w->base_line_number = Qnil;
15728
15729 if (!cursor_row_fully_visible_p (w, 1, 0))
15730 {
15731 clear_glyph_matrix (w->desired_matrix);
15732 last_line_misfit = 1;
15733 }
15734 /* Drop through and scroll. */
15735 else
15736 goto done;
15737 }
15738 else
15739 clear_glyph_matrix (w->desired_matrix);
15740 }
15741
15742 try_to_scroll:
15743
15744 w->last_modified = make_number (0);
15745 w->last_overlay_modified = make_number (0);
15746
15747 /* Redisplay the mode line. Select the buffer properly for that. */
15748 if (!update_mode_line)
15749 {
15750 update_mode_line = 1;
15751 w->update_mode_line = 1;
15752 }
15753
15754 /* Try to scroll by specified few lines. */
15755 if ((scroll_conservatively
15756 || emacs_scroll_step
15757 || temp_scroll_step
15758 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15759 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15760 && CHARPOS (startp) >= BEGV
15761 && CHARPOS (startp) <= ZV)
15762 {
15763 /* The function returns -1 if new fonts were loaded, 1 if
15764 successful, 0 if not successful. */
15765 int ss = try_scrolling (window, just_this_one_p,
15766 scroll_conservatively,
15767 emacs_scroll_step,
15768 temp_scroll_step, last_line_misfit);
15769 switch (ss)
15770 {
15771 case SCROLLING_SUCCESS:
15772 goto done;
15773
15774 case SCROLLING_NEED_LARGER_MATRICES:
15775 goto need_larger_matrices;
15776
15777 case SCROLLING_FAILED:
15778 break;
15779
15780 default:
15781 abort ();
15782 }
15783 }
15784
15785 /* Finally, just choose a place to start which positions point
15786 according to user preferences. */
15787
15788 recenter:
15789
15790 #if GLYPH_DEBUG
15791 debug_method_add (w, "recenter");
15792 #endif
15793
15794 /* w->vscroll = 0; */
15795
15796 /* Forget any previously recorded base line for line number display. */
15797 if (!buffer_unchanged_p)
15798 w->base_line_number = Qnil;
15799
15800 /* Determine the window start relative to point. */
15801 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15802 it.current_y = it.last_visible_y;
15803 if (centering_position < 0)
15804 {
15805 int margin =
15806 scroll_margin > 0
15807 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15808 : 0;
15809 ptrdiff_t margin_pos = CHARPOS (startp);
15810 Lisp_Object aggressive;
15811 int scrolling_up;
15812
15813 /* If there is a scroll margin at the top of the window, find
15814 its character position. */
15815 if (margin
15816 /* Cannot call start_display if startp is not in the
15817 accessible region of the buffer. This can happen when we
15818 have just switched to a different buffer and/or changed
15819 its restriction. In that case, startp is initialized to
15820 the character position 1 (BEGV) because we did not yet
15821 have chance to display the buffer even once. */
15822 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15823 {
15824 struct it it1;
15825 void *it1data = NULL;
15826
15827 SAVE_IT (it1, it, it1data);
15828 start_display (&it1, w, startp);
15829 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15830 margin_pos = IT_CHARPOS (it1);
15831 RESTORE_IT (&it, &it, it1data);
15832 }
15833 scrolling_up = PT > margin_pos;
15834 aggressive =
15835 scrolling_up
15836 ? BVAR (current_buffer, scroll_up_aggressively)
15837 : BVAR (current_buffer, scroll_down_aggressively);
15838
15839 if (!MINI_WINDOW_P (w)
15840 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15841 {
15842 int pt_offset = 0;
15843
15844 /* Setting scroll-conservatively overrides
15845 scroll-*-aggressively. */
15846 if (!scroll_conservatively && NUMBERP (aggressive))
15847 {
15848 double float_amount = XFLOATINT (aggressive);
15849
15850 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15851 if (pt_offset == 0 && float_amount > 0)
15852 pt_offset = 1;
15853 if (pt_offset && margin > 0)
15854 margin -= 1;
15855 }
15856 /* Compute how much to move the window start backward from
15857 point so that point will be displayed where the user
15858 wants it. */
15859 if (scrolling_up)
15860 {
15861 centering_position = it.last_visible_y;
15862 if (pt_offset)
15863 centering_position -= pt_offset;
15864 centering_position -=
15865 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15866 + WINDOW_HEADER_LINE_HEIGHT (w);
15867 /* Don't let point enter the scroll margin near top of
15868 the window. */
15869 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15870 centering_position = margin * FRAME_LINE_HEIGHT (f);
15871 }
15872 else
15873 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15874 }
15875 else
15876 /* Set the window start half the height of the window backward
15877 from point. */
15878 centering_position = window_box_height (w) / 2;
15879 }
15880 move_it_vertically_backward (&it, centering_position);
15881
15882 xassert (IT_CHARPOS (it) >= BEGV);
15883
15884 /* The function move_it_vertically_backward may move over more
15885 than the specified y-distance. If it->w is small, e.g. a
15886 mini-buffer window, we may end up in front of the window's
15887 display area. Start displaying at the start of the line
15888 containing PT in this case. */
15889 if (it.current_y <= 0)
15890 {
15891 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15892 move_it_vertically_backward (&it, 0);
15893 it.current_y = 0;
15894 }
15895
15896 it.current_x = it.hpos = 0;
15897
15898 /* Set the window start position here explicitly, to avoid an
15899 infinite loop in case the functions in window-scroll-functions
15900 get errors. */
15901 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15902
15903 /* Run scroll hooks. */
15904 startp = run_window_scroll_functions (window, it.current.pos);
15905
15906 /* Redisplay the window. */
15907 if (!current_matrix_up_to_date_p
15908 || windows_or_buffers_changed
15909 || cursor_type_changed
15910 /* Don't use try_window_reusing_current_matrix in this case
15911 because it can have changed the buffer. */
15912 || !NILP (Vwindow_scroll_functions)
15913 || !just_this_one_p
15914 || MINI_WINDOW_P (w)
15915 || !(used_current_matrix_p
15916 = try_window_reusing_current_matrix (w)))
15917 try_window (window, startp, 0);
15918
15919 /* If new fonts have been loaded (due to fontsets), give up. We
15920 have to start a new redisplay since we need to re-adjust glyph
15921 matrices. */
15922 if (fonts_changed_p)
15923 goto need_larger_matrices;
15924
15925 /* If cursor did not appear assume that the middle of the window is
15926 in the first line of the window. Do it again with the next line.
15927 (Imagine a window of height 100, displaying two lines of height
15928 60. Moving back 50 from it->last_visible_y will end in the first
15929 line.) */
15930 if (w->cursor.vpos < 0)
15931 {
15932 if (!NILP (w->window_end_valid)
15933 && PT >= Z - XFASTINT (w->window_end_pos))
15934 {
15935 clear_glyph_matrix (w->desired_matrix);
15936 move_it_by_lines (&it, 1);
15937 try_window (window, it.current.pos, 0);
15938 }
15939 else if (PT < IT_CHARPOS (it))
15940 {
15941 clear_glyph_matrix (w->desired_matrix);
15942 move_it_by_lines (&it, -1);
15943 try_window (window, it.current.pos, 0);
15944 }
15945 else
15946 {
15947 /* Not much we can do about it. */
15948 }
15949 }
15950
15951 /* Consider the following case: Window starts at BEGV, there is
15952 invisible, intangible text at BEGV, so that display starts at
15953 some point START > BEGV. It can happen that we are called with
15954 PT somewhere between BEGV and START. Try to handle that case. */
15955 if (w->cursor.vpos < 0)
15956 {
15957 struct glyph_row *row = w->current_matrix->rows;
15958 if (row->mode_line_p)
15959 ++row;
15960 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15961 }
15962
15963 if (!cursor_row_fully_visible_p (w, 0, 0))
15964 {
15965 /* If vscroll is enabled, disable it and try again. */
15966 if (w->vscroll)
15967 {
15968 w->vscroll = 0;
15969 clear_glyph_matrix (w->desired_matrix);
15970 goto recenter;
15971 }
15972
15973 /* Users who set scroll-conservatively to a large number want
15974 point just above/below the scroll margin. If we ended up
15975 with point's row partially visible, move the window start to
15976 make that row fully visible and out of the margin. */
15977 if (scroll_conservatively > SCROLL_LIMIT)
15978 {
15979 int margin =
15980 scroll_margin > 0
15981 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15982 : 0;
15983 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15984
15985 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15986 clear_glyph_matrix (w->desired_matrix);
15987 if (1 == try_window (window, it.current.pos,
15988 TRY_WINDOW_CHECK_MARGINS))
15989 goto done;
15990 }
15991
15992 /* If centering point failed to make the whole line visible,
15993 put point at the top instead. That has to make the whole line
15994 visible, if it can be done. */
15995 if (centering_position == 0)
15996 goto done;
15997
15998 clear_glyph_matrix (w->desired_matrix);
15999 centering_position = 0;
16000 goto recenter;
16001 }
16002
16003 done:
16004
16005 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16006 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16007 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16008
16009 /* Display the mode line, if we must. */
16010 if ((update_mode_line
16011 /* If window not full width, must redo its mode line
16012 if (a) the window to its side is being redone and
16013 (b) we do a frame-based redisplay. This is a consequence
16014 of how inverted lines are drawn in frame-based redisplay. */
16015 || (!just_this_one_p
16016 && !FRAME_WINDOW_P (f)
16017 && !WINDOW_FULL_WIDTH_P (w))
16018 /* Line number to display. */
16019 || INTEGERP (w->base_line_pos)
16020 /* Column number is displayed and different from the one displayed. */
16021 || (!NILP (w->column_number_displayed)
16022 && (XFASTINT (w->column_number_displayed) != current_column ())))
16023 /* This means that the window has a mode line. */
16024 && (WINDOW_WANTS_MODELINE_P (w)
16025 || WINDOW_WANTS_HEADER_LINE_P (w)))
16026 {
16027 display_mode_lines (w);
16028
16029 /* If mode line height has changed, arrange for a thorough
16030 immediate redisplay using the correct mode line height. */
16031 if (WINDOW_WANTS_MODELINE_P (w)
16032 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16033 {
16034 fonts_changed_p = 1;
16035 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16036 = DESIRED_MODE_LINE_HEIGHT (w);
16037 }
16038
16039 /* If header line height has changed, arrange for a thorough
16040 immediate redisplay using the correct header line height. */
16041 if (WINDOW_WANTS_HEADER_LINE_P (w)
16042 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16043 {
16044 fonts_changed_p = 1;
16045 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16046 = DESIRED_HEADER_LINE_HEIGHT (w);
16047 }
16048
16049 if (fonts_changed_p)
16050 goto need_larger_matrices;
16051 }
16052
16053 if (!line_number_displayed
16054 && !BUFFERP (w->base_line_pos))
16055 {
16056 w->base_line_pos = Qnil;
16057 w->base_line_number = Qnil;
16058 }
16059
16060 finish_menu_bars:
16061
16062 /* When we reach a frame's selected window, redo the frame's menu bar. */
16063 if (update_mode_line
16064 && EQ (FRAME_SELECTED_WINDOW (f), window))
16065 {
16066 int redisplay_menu_p = 0;
16067
16068 if (FRAME_WINDOW_P (f))
16069 {
16070 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16071 || defined (HAVE_NS) || defined (USE_GTK)
16072 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16073 #else
16074 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16075 #endif
16076 }
16077 else
16078 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16079
16080 if (redisplay_menu_p)
16081 display_menu_bar (w);
16082
16083 #ifdef HAVE_WINDOW_SYSTEM
16084 if (FRAME_WINDOW_P (f))
16085 {
16086 #if defined (USE_GTK) || defined (HAVE_NS)
16087 if (FRAME_EXTERNAL_TOOL_BAR (f))
16088 redisplay_tool_bar (f);
16089 #else
16090 if (WINDOWP (f->tool_bar_window)
16091 && (FRAME_TOOL_BAR_LINES (f) > 0
16092 || !NILP (Vauto_resize_tool_bars))
16093 && redisplay_tool_bar (f))
16094 ignore_mouse_drag_p = 1;
16095 #endif
16096 }
16097 #endif
16098 }
16099
16100 #ifdef HAVE_WINDOW_SYSTEM
16101 if (FRAME_WINDOW_P (f)
16102 && update_window_fringes (w, (just_this_one_p
16103 || (!used_current_matrix_p && !overlay_arrow_seen)
16104 || w->pseudo_window_p)))
16105 {
16106 update_begin (f);
16107 BLOCK_INPUT;
16108 if (draw_window_fringes (w, 1))
16109 x_draw_vertical_border (w);
16110 UNBLOCK_INPUT;
16111 update_end (f);
16112 }
16113 #endif /* HAVE_WINDOW_SYSTEM */
16114
16115 /* We go to this label, with fonts_changed_p nonzero,
16116 if it is necessary to try again using larger glyph matrices.
16117 We have to redeem the scroll bar even in this case,
16118 because the loop in redisplay_internal expects that. */
16119 need_larger_matrices:
16120 ;
16121 finish_scroll_bars:
16122
16123 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16124 {
16125 /* Set the thumb's position and size. */
16126 set_vertical_scroll_bar (w);
16127
16128 /* Note that we actually used the scroll bar attached to this
16129 window, so it shouldn't be deleted at the end of redisplay. */
16130 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16131 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16132 }
16133
16134 /* Restore current_buffer and value of point in it. The window
16135 update may have changed the buffer, so first make sure `opoint'
16136 is still valid (Bug#6177). */
16137 if (CHARPOS (opoint) < BEGV)
16138 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16139 else if (CHARPOS (opoint) > ZV)
16140 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16141 else
16142 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16143
16144 set_buffer_internal_1 (old);
16145 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16146 shorter. This can be caused by log truncation in *Messages*. */
16147 if (CHARPOS (lpoint) <= ZV)
16148 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16149
16150 unbind_to (count, Qnil);
16151 }
16152
16153
16154 /* Build the complete desired matrix of WINDOW with a window start
16155 buffer position POS.
16156
16157 Value is 1 if successful. It is zero if fonts were loaded during
16158 redisplay which makes re-adjusting glyph matrices necessary, and -1
16159 if point would appear in the scroll margins.
16160 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16161 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16162 set in FLAGS.) */
16163
16164 int
16165 try_window (Lisp_Object window, struct text_pos pos, int flags)
16166 {
16167 struct window *w = XWINDOW (window);
16168 struct it it;
16169 struct glyph_row *last_text_row = NULL;
16170 struct frame *f = XFRAME (w->frame);
16171
16172 /* Make POS the new window start. */
16173 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16174
16175 /* Mark cursor position as unknown. No overlay arrow seen. */
16176 w->cursor.vpos = -1;
16177 overlay_arrow_seen = 0;
16178
16179 /* Initialize iterator and info to start at POS. */
16180 start_display (&it, w, pos);
16181
16182 /* Display all lines of W. */
16183 while (it.current_y < it.last_visible_y)
16184 {
16185 if (display_line (&it))
16186 last_text_row = it.glyph_row - 1;
16187 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16188 return 0;
16189 }
16190
16191 /* Don't let the cursor end in the scroll margins. */
16192 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16193 && !MINI_WINDOW_P (w))
16194 {
16195 int this_scroll_margin;
16196
16197 if (scroll_margin > 0)
16198 {
16199 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16200 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16201 }
16202 else
16203 this_scroll_margin = 0;
16204
16205 if ((w->cursor.y >= 0 /* not vscrolled */
16206 && w->cursor.y < this_scroll_margin
16207 && CHARPOS (pos) > BEGV
16208 && IT_CHARPOS (it) < ZV)
16209 /* rms: considering make_cursor_line_fully_visible_p here
16210 seems to give wrong results. We don't want to recenter
16211 when the last line is partly visible, we want to allow
16212 that case to be handled in the usual way. */
16213 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16214 {
16215 w->cursor.vpos = -1;
16216 clear_glyph_matrix (w->desired_matrix);
16217 return -1;
16218 }
16219 }
16220
16221 /* If bottom moved off end of frame, change mode line percentage. */
16222 if (XFASTINT (w->window_end_pos) <= 0
16223 && Z != IT_CHARPOS (it))
16224 w->update_mode_line = 1;
16225
16226 /* Set window_end_pos to the offset of the last character displayed
16227 on the window from the end of current_buffer. Set
16228 window_end_vpos to its row number. */
16229 if (last_text_row)
16230 {
16231 xassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16232 w->window_end_bytepos
16233 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16234 w->window_end_pos
16235 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16236 w->window_end_vpos
16237 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16238 xassert (MATRIX_ROW (w->desired_matrix, XFASTINT (w->window_end_vpos))
16239 ->displays_text_p);
16240 }
16241 else
16242 {
16243 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16244 w->window_end_pos = make_number (Z - ZV);
16245 w->window_end_vpos = make_number (0);
16246 }
16247
16248 /* But that is not valid info until redisplay finishes. */
16249 w->window_end_valid = Qnil;
16250 return 1;
16251 }
16252
16253
16254 \f
16255 /************************************************************************
16256 Window redisplay reusing current matrix when buffer has not changed
16257 ************************************************************************/
16258
16259 /* Try redisplay of window W showing an unchanged buffer with a
16260 different window start than the last time it was displayed by
16261 reusing its current matrix. Value is non-zero if successful.
16262 W->start is the new window start. */
16263
16264 static int
16265 try_window_reusing_current_matrix (struct window *w)
16266 {
16267 struct frame *f = XFRAME (w->frame);
16268 struct glyph_row *bottom_row;
16269 struct it it;
16270 struct run run;
16271 struct text_pos start, new_start;
16272 int nrows_scrolled, i;
16273 struct glyph_row *last_text_row;
16274 struct glyph_row *last_reused_text_row;
16275 struct glyph_row *start_row;
16276 int start_vpos, min_y, max_y;
16277
16278 #if GLYPH_DEBUG
16279 if (inhibit_try_window_reusing)
16280 return 0;
16281 #endif
16282
16283 if (/* This function doesn't handle terminal frames. */
16284 !FRAME_WINDOW_P (f)
16285 /* Don't try to reuse the display if windows have been split
16286 or such. */
16287 || windows_or_buffers_changed
16288 || cursor_type_changed)
16289 return 0;
16290
16291 /* Can't do this if region may have changed. */
16292 if ((!NILP (Vtransient_mark_mode)
16293 && !NILP (BVAR (current_buffer, mark_active)))
16294 || !NILP (w->region_showing)
16295 || !NILP (Vshow_trailing_whitespace))
16296 return 0;
16297
16298 /* If top-line visibility has changed, give up. */
16299 if (WINDOW_WANTS_HEADER_LINE_P (w)
16300 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16301 return 0;
16302
16303 /* Give up if old or new display is scrolled vertically. We could
16304 make this function handle this, but right now it doesn't. */
16305 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16306 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16307 return 0;
16308
16309 /* The variable new_start now holds the new window start. The old
16310 start `start' can be determined from the current matrix. */
16311 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16312 start = start_row->minpos;
16313 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16314
16315 /* Clear the desired matrix for the display below. */
16316 clear_glyph_matrix (w->desired_matrix);
16317
16318 if (CHARPOS (new_start) <= CHARPOS (start))
16319 {
16320 /* Don't use this method if the display starts with an ellipsis
16321 displayed for invisible text. It's not easy to handle that case
16322 below, and it's certainly not worth the effort since this is
16323 not a frequent case. */
16324 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16325 return 0;
16326
16327 IF_DEBUG (debug_method_add (w, "twu1"));
16328
16329 /* Display up to a row that can be reused. The variable
16330 last_text_row is set to the last row displayed that displays
16331 text. Note that it.vpos == 0 if or if not there is a
16332 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16333 start_display (&it, w, new_start);
16334 w->cursor.vpos = -1;
16335 last_text_row = last_reused_text_row = NULL;
16336
16337 while (it.current_y < it.last_visible_y
16338 && !fonts_changed_p)
16339 {
16340 /* If we have reached into the characters in the START row,
16341 that means the line boundaries have changed. So we
16342 can't start copying with the row START. Maybe it will
16343 work to start copying with the following row. */
16344 while (IT_CHARPOS (it) > CHARPOS (start))
16345 {
16346 /* Advance to the next row as the "start". */
16347 start_row++;
16348 start = start_row->minpos;
16349 /* If there are no more rows to try, or just one, give up. */
16350 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16351 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16352 || CHARPOS (start) == ZV)
16353 {
16354 clear_glyph_matrix (w->desired_matrix);
16355 return 0;
16356 }
16357
16358 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16359 }
16360 /* If we have reached alignment, we can copy the rest of the
16361 rows. */
16362 if (IT_CHARPOS (it) == CHARPOS (start)
16363 /* Don't accept "alignment" inside a display vector,
16364 since start_row could have started in the middle of
16365 that same display vector (thus their character
16366 positions match), and we have no way of telling if
16367 that is the case. */
16368 && it.current.dpvec_index < 0)
16369 break;
16370
16371 if (display_line (&it))
16372 last_text_row = it.glyph_row - 1;
16373
16374 }
16375
16376 /* A value of current_y < last_visible_y means that we stopped
16377 at the previous window start, which in turn means that we
16378 have at least one reusable row. */
16379 if (it.current_y < it.last_visible_y)
16380 {
16381 struct glyph_row *row;
16382
16383 /* IT.vpos always starts from 0; it counts text lines. */
16384 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16385
16386 /* Find PT if not already found in the lines displayed. */
16387 if (w->cursor.vpos < 0)
16388 {
16389 int dy = it.current_y - start_row->y;
16390
16391 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16392 row = row_containing_pos (w, PT, row, NULL, dy);
16393 if (row)
16394 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16395 dy, nrows_scrolled);
16396 else
16397 {
16398 clear_glyph_matrix (w->desired_matrix);
16399 return 0;
16400 }
16401 }
16402
16403 /* Scroll the display. Do it before the current matrix is
16404 changed. The problem here is that update has not yet
16405 run, i.e. part of the current matrix is not up to date.
16406 scroll_run_hook will clear the cursor, and use the
16407 current matrix to get the height of the row the cursor is
16408 in. */
16409 run.current_y = start_row->y;
16410 run.desired_y = it.current_y;
16411 run.height = it.last_visible_y - it.current_y;
16412
16413 if (run.height > 0 && run.current_y != run.desired_y)
16414 {
16415 update_begin (f);
16416 FRAME_RIF (f)->update_window_begin_hook (w);
16417 FRAME_RIF (f)->clear_window_mouse_face (w);
16418 FRAME_RIF (f)->scroll_run_hook (w, &run);
16419 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16420 update_end (f);
16421 }
16422
16423 /* Shift current matrix down by nrows_scrolled lines. */
16424 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16425 rotate_matrix (w->current_matrix,
16426 start_vpos,
16427 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16428 nrows_scrolled);
16429
16430 /* Disable lines that must be updated. */
16431 for (i = 0; i < nrows_scrolled; ++i)
16432 (start_row + i)->enabled_p = 0;
16433
16434 /* Re-compute Y positions. */
16435 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16436 max_y = it.last_visible_y;
16437 for (row = start_row + nrows_scrolled;
16438 row < bottom_row;
16439 ++row)
16440 {
16441 row->y = it.current_y;
16442 row->visible_height = row->height;
16443
16444 if (row->y < min_y)
16445 row->visible_height -= min_y - row->y;
16446 if (row->y + row->height > max_y)
16447 row->visible_height -= row->y + row->height - max_y;
16448 if (row->fringe_bitmap_periodic_p)
16449 row->redraw_fringe_bitmaps_p = 1;
16450
16451 it.current_y += row->height;
16452
16453 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16454 last_reused_text_row = row;
16455 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16456 break;
16457 }
16458
16459 /* Disable lines in the current matrix which are now
16460 below the window. */
16461 for (++row; row < bottom_row; ++row)
16462 row->enabled_p = row->mode_line_p = 0;
16463 }
16464
16465 /* Update window_end_pos etc.; last_reused_text_row is the last
16466 reused row from the current matrix containing text, if any.
16467 The value of last_text_row is the last displayed line
16468 containing text. */
16469 if (last_reused_text_row)
16470 {
16471 w->window_end_bytepos
16472 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16473 w->window_end_pos
16474 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_reused_text_row));
16475 w->window_end_vpos
16476 = make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16477 w->current_matrix));
16478 }
16479 else if (last_text_row)
16480 {
16481 w->window_end_bytepos
16482 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16483 w->window_end_pos
16484 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16485 w->window_end_vpos
16486 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16487 }
16488 else
16489 {
16490 /* This window must be completely empty. */
16491 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16492 w->window_end_pos = make_number (Z - ZV);
16493 w->window_end_vpos = make_number (0);
16494 }
16495 w->window_end_valid = Qnil;
16496
16497 /* Update hint: don't try scrolling again in update_window. */
16498 w->desired_matrix->no_scrolling_p = 1;
16499
16500 #if GLYPH_DEBUG
16501 debug_method_add (w, "try_window_reusing_current_matrix 1");
16502 #endif
16503 return 1;
16504 }
16505 else if (CHARPOS (new_start) > CHARPOS (start))
16506 {
16507 struct glyph_row *pt_row, *row;
16508 struct glyph_row *first_reusable_row;
16509 struct glyph_row *first_row_to_display;
16510 int dy;
16511 int yb = window_text_bottom_y (w);
16512
16513 /* Find the row starting at new_start, if there is one. Don't
16514 reuse a partially visible line at the end. */
16515 first_reusable_row = start_row;
16516 while (first_reusable_row->enabled_p
16517 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16518 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16519 < CHARPOS (new_start)))
16520 ++first_reusable_row;
16521
16522 /* Give up if there is no row to reuse. */
16523 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16524 || !first_reusable_row->enabled_p
16525 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16526 != CHARPOS (new_start)))
16527 return 0;
16528
16529 /* We can reuse fully visible rows beginning with
16530 first_reusable_row to the end of the window. Set
16531 first_row_to_display to the first row that cannot be reused.
16532 Set pt_row to the row containing point, if there is any. */
16533 pt_row = NULL;
16534 for (first_row_to_display = first_reusable_row;
16535 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16536 ++first_row_to_display)
16537 {
16538 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16539 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16540 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16541 && first_row_to_display->ends_at_zv_p
16542 && pt_row == NULL)))
16543 pt_row = first_row_to_display;
16544 }
16545
16546 /* Start displaying at the start of first_row_to_display. */
16547 xassert (first_row_to_display->y < yb);
16548 init_to_row_start (&it, w, first_row_to_display);
16549
16550 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16551 - start_vpos);
16552 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16553 - nrows_scrolled);
16554 it.current_y = (first_row_to_display->y - first_reusable_row->y
16555 + WINDOW_HEADER_LINE_HEIGHT (w));
16556
16557 /* Display lines beginning with first_row_to_display in the
16558 desired matrix. Set last_text_row to the last row displayed
16559 that displays text. */
16560 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16561 if (pt_row == NULL)
16562 w->cursor.vpos = -1;
16563 last_text_row = NULL;
16564 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16565 if (display_line (&it))
16566 last_text_row = it.glyph_row - 1;
16567
16568 /* If point is in a reused row, adjust y and vpos of the cursor
16569 position. */
16570 if (pt_row)
16571 {
16572 w->cursor.vpos -= nrows_scrolled;
16573 w->cursor.y -= first_reusable_row->y - start_row->y;
16574 }
16575
16576 /* Give up if point isn't in a row displayed or reused. (This
16577 also handles the case where w->cursor.vpos < nrows_scrolled
16578 after the calls to display_line, which can happen with scroll
16579 margins. See bug#1295.) */
16580 if (w->cursor.vpos < 0)
16581 {
16582 clear_glyph_matrix (w->desired_matrix);
16583 return 0;
16584 }
16585
16586 /* Scroll the display. */
16587 run.current_y = first_reusable_row->y;
16588 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16589 run.height = it.last_visible_y - run.current_y;
16590 dy = run.current_y - run.desired_y;
16591
16592 if (run.height)
16593 {
16594 update_begin (f);
16595 FRAME_RIF (f)->update_window_begin_hook (w);
16596 FRAME_RIF (f)->clear_window_mouse_face (w);
16597 FRAME_RIF (f)->scroll_run_hook (w, &run);
16598 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16599 update_end (f);
16600 }
16601
16602 /* Adjust Y positions of reused rows. */
16603 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16604 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16605 max_y = it.last_visible_y;
16606 for (row = first_reusable_row; row < first_row_to_display; ++row)
16607 {
16608 row->y -= dy;
16609 row->visible_height = row->height;
16610 if (row->y < min_y)
16611 row->visible_height -= min_y - row->y;
16612 if (row->y + row->height > max_y)
16613 row->visible_height -= row->y + row->height - max_y;
16614 if (row->fringe_bitmap_periodic_p)
16615 row->redraw_fringe_bitmaps_p = 1;
16616 }
16617
16618 /* Scroll the current matrix. */
16619 xassert (nrows_scrolled > 0);
16620 rotate_matrix (w->current_matrix,
16621 start_vpos,
16622 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16623 -nrows_scrolled);
16624
16625 /* Disable rows not reused. */
16626 for (row -= nrows_scrolled; row < bottom_row; ++row)
16627 row->enabled_p = 0;
16628
16629 /* Point may have moved to a different line, so we cannot assume that
16630 the previous cursor position is valid; locate the correct row. */
16631 if (pt_row)
16632 {
16633 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16634 row < bottom_row
16635 && PT >= MATRIX_ROW_END_CHARPOS (row)
16636 && !row->ends_at_zv_p;
16637 row++)
16638 {
16639 w->cursor.vpos++;
16640 w->cursor.y = row->y;
16641 }
16642 if (row < bottom_row)
16643 {
16644 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16645 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16646
16647 /* Can't use this optimization with bidi-reordered glyph
16648 rows, unless cursor is already at point. */
16649 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16650 {
16651 if (!(w->cursor.hpos >= 0
16652 && w->cursor.hpos < row->used[TEXT_AREA]
16653 && BUFFERP (glyph->object)
16654 && glyph->charpos == PT))
16655 return 0;
16656 }
16657 else
16658 for (; glyph < end
16659 && (!BUFFERP (glyph->object)
16660 || glyph->charpos < PT);
16661 glyph++)
16662 {
16663 w->cursor.hpos++;
16664 w->cursor.x += glyph->pixel_width;
16665 }
16666 }
16667 }
16668
16669 /* Adjust window end. A null value of last_text_row means that
16670 the window end is in reused rows which in turn means that
16671 only its vpos can have changed. */
16672 if (last_text_row)
16673 {
16674 w->window_end_bytepos
16675 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16676 w->window_end_pos
16677 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
16678 w->window_end_vpos
16679 = make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix));
16680 }
16681 else
16682 {
16683 w->window_end_vpos
16684 = make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled);
16685 }
16686
16687 w->window_end_valid = Qnil;
16688 w->desired_matrix->no_scrolling_p = 1;
16689
16690 #if GLYPH_DEBUG
16691 debug_method_add (w, "try_window_reusing_current_matrix 2");
16692 #endif
16693 return 1;
16694 }
16695
16696 return 0;
16697 }
16698
16699
16700 \f
16701 /************************************************************************
16702 Window redisplay reusing current matrix when buffer has changed
16703 ************************************************************************/
16704
16705 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16706 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16707 ptrdiff_t *, ptrdiff_t *);
16708 static struct glyph_row *
16709 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16710 struct glyph_row *);
16711
16712
16713 /* Return the last row in MATRIX displaying text. If row START is
16714 non-null, start searching with that row. IT gives the dimensions
16715 of the display. Value is null if matrix is empty; otherwise it is
16716 a pointer to the row found. */
16717
16718 static struct glyph_row *
16719 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16720 struct glyph_row *start)
16721 {
16722 struct glyph_row *row, *row_found;
16723
16724 /* Set row_found to the last row in IT->w's current matrix
16725 displaying text. The loop looks funny but think of partially
16726 visible lines. */
16727 row_found = NULL;
16728 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16729 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16730 {
16731 xassert (row->enabled_p);
16732 row_found = row;
16733 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16734 break;
16735 ++row;
16736 }
16737
16738 return row_found;
16739 }
16740
16741
16742 /* Return the last row in the current matrix of W that is not affected
16743 by changes at the start of current_buffer that occurred since W's
16744 current matrix was built. Value is null if no such row exists.
16745
16746 BEG_UNCHANGED us the number of characters unchanged at the start of
16747 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16748 first changed character in current_buffer. Characters at positions <
16749 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16750 when the current matrix was built. */
16751
16752 static struct glyph_row *
16753 find_last_unchanged_at_beg_row (struct window *w)
16754 {
16755 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16756 struct glyph_row *row;
16757 struct glyph_row *row_found = NULL;
16758 int yb = window_text_bottom_y (w);
16759
16760 /* Find the last row displaying unchanged text. */
16761 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16762 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16763 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16764 ++row)
16765 {
16766 if (/* If row ends before first_changed_pos, it is unchanged,
16767 except in some case. */
16768 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16769 /* When row ends in ZV and we write at ZV it is not
16770 unchanged. */
16771 && !row->ends_at_zv_p
16772 /* When first_changed_pos is the end of a continued line,
16773 row is not unchanged because it may be no longer
16774 continued. */
16775 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16776 && (row->continued_p
16777 || row->exact_window_width_line_p))
16778 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16779 needs to be recomputed, so don't consider this row as
16780 unchanged. This happens when the last line was
16781 bidi-reordered and was killed immediately before this
16782 redisplay cycle. In that case, ROW->end stores the
16783 buffer position of the first visual-order character of
16784 the killed text, which is now beyond ZV. */
16785 && CHARPOS (row->end.pos) <= ZV)
16786 row_found = row;
16787
16788 /* Stop if last visible row. */
16789 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16790 break;
16791 }
16792
16793 return row_found;
16794 }
16795
16796
16797 /* Find the first glyph row in the current matrix of W that is not
16798 affected by changes at the end of current_buffer since the
16799 time W's current matrix was built.
16800
16801 Return in *DELTA the number of chars by which buffer positions in
16802 unchanged text at the end of current_buffer must be adjusted.
16803
16804 Return in *DELTA_BYTES the corresponding number of bytes.
16805
16806 Value is null if no such row exists, i.e. all rows are affected by
16807 changes. */
16808
16809 static struct glyph_row *
16810 find_first_unchanged_at_end_row (struct window *w,
16811 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16812 {
16813 struct glyph_row *row;
16814 struct glyph_row *row_found = NULL;
16815
16816 *delta = *delta_bytes = 0;
16817
16818 /* Display must not have been paused, otherwise the current matrix
16819 is not up to date. */
16820 eassert (!NILP (w->window_end_valid));
16821
16822 /* A value of window_end_pos >= END_UNCHANGED means that the window
16823 end is in the range of changed text. If so, there is no
16824 unchanged row at the end of W's current matrix. */
16825 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16826 return NULL;
16827
16828 /* Set row to the last row in W's current matrix displaying text. */
16829 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16830
16831 /* If matrix is entirely empty, no unchanged row exists. */
16832 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16833 {
16834 /* The value of row is the last glyph row in the matrix having a
16835 meaningful buffer position in it. The end position of row
16836 corresponds to window_end_pos. This allows us to translate
16837 buffer positions in the current matrix to current buffer
16838 positions for characters not in changed text. */
16839 ptrdiff_t Z_old =
16840 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16841 ptrdiff_t Z_BYTE_old =
16842 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16843 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16844 struct glyph_row *first_text_row
16845 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16846
16847 *delta = Z - Z_old;
16848 *delta_bytes = Z_BYTE - Z_BYTE_old;
16849
16850 /* Set last_unchanged_pos to the buffer position of the last
16851 character in the buffer that has not been changed. Z is the
16852 index + 1 of the last character in current_buffer, i.e. by
16853 subtracting END_UNCHANGED we get the index of the last
16854 unchanged character, and we have to add BEG to get its buffer
16855 position. */
16856 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16857 last_unchanged_pos_old = last_unchanged_pos - *delta;
16858
16859 /* Search backward from ROW for a row displaying a line that
16860 starts at a minimum position >= last_unchanged_pos_old. */
16861 for (; row > first_text_row; --row)
16862 {
16863 /* This used to abort, but it can happen.
16864 It is ok to just stop the search instead here. KFS. */
16865 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16866 break;
16867
16868 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16869 row_found = row;
16870 }
16871 }
16872
16873 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16874
16875 return row_found;
16876 }
16877
16878
16879 /* Make sure that glyph rows in the current matrix of window W
16880 reference the same glyph memory as corresponding rows in the
16881 frame's frame matrix. This function is called after scrolling W's
16882 current matrix on a terminal frame in try_window_id and
16883 try_window_reusing_current_matrix. */
16884
16885 static void
16886 sync_frame_with_window_matrix_rows (struct window *w)
16887 {
16888 struct frame *f = XFRAME (w->frame);
16889 struct glyph_row *window_row, *window_row_end, *frame_row;
16890
16891 /* Preconditions: W must be a leaf window and full-width. Its frame
16892 must have a frame matrix. */
16893 xassert (NILP (w->hchild) && NILP (w->vchild));
16894 xassert (WINDOW_FULL_WIDTH_P (w));
16895 xassert (!FRAME_WINDOW_P (f));
16896
16897 /* If W is a full-width window, glyph pointers in W's current matrix
16898 have, by definition, to be the same as glyph pointers in the
16899 corresponding frame matrix. Note that frame matrices have no
16900 marginal areas (see build_frame_matrix). */
16901 window_row = w->current_matrix->rows;
16902 window_row_end = window_row + w->current_matrix->nrows;
16903 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16904 while (window_row < window_row_end)
16905 {
16906 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16907 struct glyph *end = window_row->glyphs[LAST_AREA];
16908
16909 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16910 frame_row->glyphs[TEXT_AREA] = start;
16911 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16912 frame_row->glyphs[LAST_AREA] = end;
16913
16914 /* Disable frame rows whose corresponding window rows have
16915 been disabled in try_window_id. */
16916 if (!window_row->enabled_p)
16917 frame_row->enabled_p = 0;
16918
16919 ++window_row, ++frame_row;
16920 }
16921 }
16922
16923
16924 /* Find the glyph row in window W containing CHARPOS. Consider all
16925 rows between START and END (not inclusive). END null means search
16926 all rows to the end of the display area of W. Value is the row
16927 containing CHARPOS or null. */
16928
16929 struct glyph_row *
16930 row_containing_pos (struct window *w, ptrdiff_t charpos,
16931 struct glyph_row *start, struct glyph_row *end, int dy)
16932 {
16933 struct glyph_row *row = start;
16934 struct glyph_row *best_row = NULL;
16935 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16936 int last_y;
16937
16938 /* If we happen to start on a header-line, skip that. */
16939 if (row->mode_line_p)
16940 ++row;
16941
16942 if ((end && row >= end) || !row->enabled_p)
16943 return NULL;
16944
16945 last_y = window_text_bottom_y (w) - dy;
16946
16947 while (1)
16948 {
16949 /* Give up if we have gone too far. */
16950 if (end && row >= end)
16951 return NULL;
16952 /* This formerly returned if they were equal.
16953 I think that both quantities are of a "last plus one" type;
16954 if so, when they are equal, the row is within the screen. -- rms. */
16955 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16956 return NULL;
16957
16958 /* If it is in this row, return this row. */
16959 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16960 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16961 /* The end position of a row equals the start
16962 position of the next row. If CHARPOS is there, we
16963 would rather display it in the next line, except
16964 when this line ends in ZV. */
16965 && !row->ends_at_zv_p
16966 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16967 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16968 {
16969 struct glyph *g;
16970
16971 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16972 || (!best_row && !row->continued_p))
16973 return row;
16974 /* In bidi-reordered rows, there could be several rows
16975 occluding point, all of them belonging to the same
16976 continued line. We need to find the row which fits
16977 CHARPOS the best. */
16978 for (g = row->glyphs[TEXT_AREA];
16979 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16980 g++)
16981 {
16982 if (!STRINGP (g->object))
16983 {
16984 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16985 {
16986 mindif = eabs (g->charpos - charpos);
16987 best_row = row;
16988 /* Exact match always wins. */
16989 if (mindif == 0)
16990 return best_row;
16991 }
16992 }
16993 }
16994 }
16995 else if (best_row && !row->continued_p)
16996 return best_row;
16997 ++row;
16998 }
16999 }
17000
17001
17002 /* Try to redisplay window W by reusing its existing display. W's
17003 current matrix must be up to date when this function is called,
17004 i.e. window_end_valid must not be nil.
17005
17006 Value is
17007
17008 1 if display has been updated
17009 0 if otherwise unsuccessful
17010 -1 if redisplay with same window start is known not to succeed
17011
17012 The following steps are performed:
17013
17014 1. Find the last row in the current matrix of W that is not
17015 affected by changes at the start of current_buffer. If no such row
17016 is found, give up.
17017
17018 2. Find the first row in W's current matrix that is not affected by
17019 changes at the end of current_buffer. Maybe there is no such row.
17020
17021 3. Display lines beginning with the row + 1 found in step 1 to the
17022 row found in step 2 or, if step 2 didn't find a row, to the end of
17023 the window.
17024
17025 4. If cursor is not known to appear on the window, give up.
17026
17027 5. If display stopped at the row found in step 2, scroll the
17028 display and current matrix as needed.
17029
17030 6. Maybe display some lines at the end of W, if we must. This can
17031 happen under various circumstances, like a partially visible line
17032 becoming fully visible, or because newly displayed lines are displayed
17033 in smaller font sizes.
17034
17035 7. Update W's window end information. */
17036
17037 static int
17038 try_window_id (struct window *w)
17039 {
17040 struct frame *f = XFRAME (w->frame);
17041 struct glyph_matrix *current_matrix = w->current_matrix;
17042 struct glyph_matrix *desired_matrix = w->desired_matrix;
17043 struct glyph_row *last_unchanged_at_beg_row;
17044 struct glyph_row *first_unchanged_at_end_row;
17045 struct glyph_row *row;
17046 struct glyph_row *bottom_row;
17047 int bottom_vpos;
17048 struct it it;
17049 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17050 int dvpos, dy;
17051 struct text_pos start_pos;
17052 struct run run;
17053 int first_unchanged_at_end_vpos = 0;
17054 struct glyph_row *last_text_row, *last_text_row_at_end;
17055 struct text_pos start;
17056 ptrdiff_t first_changed_charpos, last_changed_charpos;
17057
17058 #if GLYPH_DEBUG
17059 if (inhibit_try_window_id)
17060 return 0;
17061 #endif
17062
17063 /* This is handy for debugging. */
17064 #if 0
17065 #define GIVE_UP(X) \
17066 do { \
17067 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17068 return 0; \
17069 } while (0)
17070 #else
17071 #define GIVE_UP(X) return 0
17072 #endif
17073
17074 SET_TEXT_POS_FROM_MARKER (start, w->start);
17075
17076 /* Don't use this for mini-windows because these can show
17077 messages and mini-buffers, and we don't handle that here. */
17078 if (MINI_WINDOW_P (w))
17079 GIVE_UP (1);
17080
17081 /* This flag is used to prevent redisplay optimizations. */
17082 if (windows_or_buffers_changed || cursor_type_changed)
17083 GIVE_UP (2);
17084
17085 /* Verify that narrowing has not changed.
17086 Also verify that we were not told to prevent redisplay optimizations.
17087 It would be nice to further
17088 reduce the number of cases where this prevents try_window_id. */
17089 if (current_buffer->clip_changed
17090 || current_buffer->prevent_redisplay_optimizations_p)
17091 GIVE_UP (3);
17092
17093 /* Window must either use window-based redisplay or be full width. */
17094 if (!FRAME_WINDOW_P (f)
17095 && (!FRAME_LINE_INS_DEL_OK (f)
17096 || !WINDOW_FULL_WIDTH_P (w)))
17097 GIVE_UP (4);
17098
17099 /* Give up if point is known NOT to appear in W. */
17100 if (PT < CHARPOS (start))
17101 GIVE_UP (5);
17102
17103 /* Another way to prevent redisplay optimizations. */
17104 if (XFASTINT (w->last_modified) == 0)
17105 GIVE_UP (6);
17106
17107 /* Verify that window is not hscrolled. */
17108 if (XFASTINT (w->hscroll) != 0)
17109 GIVE_UP (7);
17110
17111 /* Verify that display wasn't paused. */
17112 if (NILP (w->window_end_valid))
17113 GIVE_UP (8);
17114
17115 /* Can't use this if highlighting a region because a cursor movement
17116 will do more than just set the cursor. */
17117 if (!NILP (Vtransient_mark_mode)
17118 && !NILP (BVAR (current_buffer, mark_active)))
17119 GIVE_UP (9);
17120
17121 /* Likewise if highlighting trailing whitespace. */
17122 if (!NILP (Vshow_trailing_whitespace))
17123 GIVE_UP (11);
17124
17125 /* Likewise if showing a region. */
17126 if (!NILP (w->region_showing))
17127 GIVE_UP (10);
17128
17129 /* Can't use this if overlay arrow position and/or string have
17130 changed. */
17131 if (overlay_arrows_changed_p ())
17132 GIVE_UP (12);
17133
17134 /* When word-wrap is on, adding a space to the first word of a
17135 wrapped line can change the wrap position, altering the line
17136 above it. It might be worthwhile to handle this more
17137 intelligently, but for now just redisplay from scratch. */
17138 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17139 GIVE_UP (21);
17140
17141 /* Under bidi reordering, adding or deleting a character in the
17142 beginning of a paragraph, before the first strong directional
17143 character, can change the base direction of the paragraph (unless
17144 the buffer specifies a fixed paragraph direction), which will
17145 require to redisplay the whole paragraph. It might be worthwhile
17146 to find the paragraph limits and widen the range of redisplayed
17147 lines to that, but for now just give up this optimization and
17148 redisplay from scratch. */
17149 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17150 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17151 GIVE_UP (22);
17152
17153 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17154 only if buffer has really changed. The reason is that the gap is
17155 initially at Z for freshly visited files. The code below would
17156 set end_unchanged to 0 in that case. */
17157 if (MODIFF > SAVE_MODIFF
17158 /* This seems to happen sometimes after saving a buffer. */
17159 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17160 {
17161 if (GPT - BEG < BEG_UNCHANGED)
17162 BEG_UNCHANGED = GPT - BEG;
17163 if (Z - GPT < END_UNCHANGED)
17164 END_UNCHANGED = Z - GPT;
17165 }
17166
17167 /* The position of the first and last character that has been changed. */
17168 first_changed_charpos = BEG + BEG_UNCHANGED;
17169 last_changed_charpos = Z - END_UNCHANGED;
17170
17171 /* If window starts after a line end, and the last change is in
17172 front of that newline, then changes don't affect the display.
17173 This case happens with stealth-fontification. Note that although
17174 the display is unchanged, glyph positions in the matrix have to
17175 be adjusted, of course. */
17176 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17177 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17178 && ((last_changed_charpos < CHARPOS (start)
17179 && CHARPOS (start) == BEGV)
17180 || (last_changed_charpos < CHARPOS (start) - 1
17181 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17182 {
17183 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17184 struct glyph_row *r0;
17185
17186 /* Compute how many chars/bytes have been added to or removed
17187 from the buffer. */
17188 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17189 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17190 Z_delta = Z - Z_old;
17191 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17192
17193 /* Give up if PT is not in the window. Note that it already has
17194 been checked at the start of try_window_id that PT is not in
17195 front of the window start. */
17196 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17197 GIVE_UP (13);
17198
17199 /* If window start is unchanged, we can reuse the whole matrix
17200 as is, after adjusting glyph positions. No need to compute
17201 the window end again, since its offset from Z hasn't changed. */
17202 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17203 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17204 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17205 /* PT must not be in a partially visible line. */
17206 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17207 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17208 {
17209 /* Adjust positions in the glyph matrix. */
17210 if (Z_delta || Z_delta_bytes)
17211 {
17212 struct glyph_row *r1
17213 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17214 increment_matrix_positions (w->current_matrix,
17215 MATRIX_ROW_VPOS (r0, current_matrix),
17216 MATRIX_ROW_VPOS (r1, current_matrix),
17217 Z_delta, Z_delta_bytes);
17218 }
17219
17220 /* Set the cursor. */
17221 row = row_containing_pos (w, PT, r0, NULL, 0);
17222 if (row)
17223 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17224 else
17225 abort ();
17226 return 1;
17227 }
17228 }
17229
17230 /* Handle the case that changes are all below what is displayed in
17231 the window, and that PT is in the window. This shortcut cannot
17232 be taken if ZV is visible in the window, and text has been added
17233 there that is visible in the window. */
17234 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17235 /* ZV is not visible in the window, or there are no
17236 changes at ZV, actually. */
17237 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17238 || first_changed_charpos == last_changed_charpos))
17239 {
17240 struct glyph_row *r0;
17241
17242 /* Give up if PT is not in the window. Note that it already has
17243 been checked at the start of try_window_id that PT is not in
17244 front of the window start. */
17245 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17246 GIVE_UP (14);
17247
17248 /* If window start is unchanged, we can reuse the whole matrix
17249 as is, without changing glyph positions since no text has
17250 been added/removed in front of the window end. */
17251 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17252 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17253 /* PT must not be in a partially visible line. */
17254 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17255 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17256 {
17257 /* We have to compute the window end anew since text
17258 could have been added/removed after it. */
17259 w->window_end_pos
17260 = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17261 w->window_end_bytepos
17262 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17263
17264 /* Set the cursor. */
17265 row = row_containing_pos (w, PT, r0, NULL, 0);
17266 if (row)
17267 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17268 else
17269 abort ();
17270 return 2;
17271 }
17272 }
17273
17274 /* Give up if window start is in the changed area.
17275
17276 The condition used to read
17277
17278 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17279
17280 but why that was tested escapes me at the moment. */
17281 if (CHARPOS (start) >= first_changed_charpos
17282 && CHARPOS (start) <= last_changed_charpos)
17283 GIVE_UP (15);
17284
17285 /* Check that window start agrees with the start of the first glyph
17286 row in its current matrix. Check this after we know the window
17287 start is not in changed text, otherwise positions would not be
17288 comparable. */
17289 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17290 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17291 GIVE_UP (16);
17292
17293 /* Give up if the window ends in strings. Overlay strings
17294 at the end are difficult to handle, so don't try. */
17295 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17296 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17297 GIVE_UP (20);
17298
17299 /* Compute the position at which we have to start displaying new
17300 lines. Some of the lines at the top of the window might be
17301 reusable because they are not displaying changed text. Find the
17302 last row in W's current matrix not affected by changes at the
17303 start of current_buffer. Value is null if changes start in the
17304 first line of window. */
17305 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17306 if (last_unchanged_at_beg_row)
17307 {
17308 /* Avoid starting to display in the middle of a character, a TAB
17309 for instance. This is easier than to set up the iterator
17310 exactly, and it's not a frequent case, so the additional
17311 effort wouldn't really pay off. */
17312 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17313 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17314 && last_unchanged_at_beg_row > w->current_matrix->rows)
17315 --last_unchanged_at_beg_row;
17316
17317 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17318 GIVE_UP (17);
17319
17320 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17321 GIVE_UP (18);
17322 start_pos = it.current.pos;
17323
17324 /* Start displaying new lines in the desired matrix at the same
17325 vpos we would use in the current matrix, i.e. below
17326 last_unchanged_at_beg_row. */
17327 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17328 current_matrix);
17329 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17330 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17331
17332 xassert (it.hpos == 0 && it.current_x == 0);
17333 }
17334 else
17335 {
17336 /* There are no reusable lines at the start of the window.
17337 Start displaying in the first text line. */
17338 start_display (&it, w, start);
17339 it.vpos = it.first_vpos;
17340 start_pos = it.current.pos;
17341 }
17342
17343 /* Find the first row that is not affected by changes at the end of
17344 the buffer. Value will be null if there is no unchanged row, in
17345 which case we must redisplay to the end of the window. delta
17346 will be set to the value by which buffer positions beginning with
17347 first_unchanged_at_end_row have to be adjusted due to text
17348 changes. */
17349 first_unchanged_at_end_row
17350 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17351 IF_DEBUG (debug_delta = delta);
17352 IF_DEBUG (debug_delta_bytes = delta_bytes);
17353
17354 /* Set stop_pos to the buffer position up to which we will have to
17355 display new lines. If first_unchanged_at_end_row != NULL, this
17356 is the buffer position of the start of the line displayed in that
17357 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17358 that we don't stop at a buffer position. */
17359 stop_pos = 0;
17360 if (first_unchanged_at_end_row)
17361 {
17362 xassert (last_unchanged_at_beg_row == NULL
17363 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17364
17365 /* If this is a continuation line, move forward to the next one
17366 that isn't. Changes in lines above affect this line.
17367 Caution: this may move first_unchanged_at_end_row to a row
17368 not displaying text. */
17369 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17370 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17371 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17372 < it.last_visible_y))
17373 ++first_unchanged_at_end_row;
17374
17375 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17376 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17377 >= it.last_visible_y))
17378 first_unchanged_at_end_row = NULL;
17379 else
17380 {
17381 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17382 + delta);
17383 first_unchanged_at_end_vpos
17384 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17385 xassert (stop_pos >= Z - END_UNCHANGED);
17386 }
17387 }
17388 else if (last_unchanged_at_beg_row == NULL)
17389 GIVE_UP (19);
17390
17391
17392 #if GLYPH_DEBUG
17393
17394 /* Either there is no unchanged row at the end, or the one we have
17395 now displays text. This is a necessary condition for the window
17396 end pos calculation at the end of this function. */
17397 xassert (first_unchanged_at_end_row == NULL
17398 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17399
17400 debug_last_unchanged_at_beg_vpos
17401 = (last_unchanged_at_beg_row
17402 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17403 : -1);
17404 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17405
17406 #endif /* GLYPH_DEBUG != 0 */
17407
17408
17409 /* Display new lines. Set last_text_row to the last new line
17410 displayed which has text on it, i.e. might end up as being the
17411 line where the window_end_vpos is. */
17412 w->cursor.vpos = -1;
17413 last_text_row = NULL;
17414 overlay_arrow_seen = 0;
17415 while (it.current_y < it.last_visible_y
17416 && !fonts_changed_p
17417 && (first_unchanged_at_end_row == NULL
17418 || IT_CHARPOS (it) < stop_pos))
17419 {
17420 if (display_line (&it))
17421 last_text_row = it.glyph_row - 1;
17422 }
17423
17424 if (fonts_changed_p)
17425 return -1;
17426
17427
17428 /* Compute differences in buffer positions, y-positions etc. for
17429 lines reused at the bottom of the window. Compute what we can
17430 scroll. */
17431 if (first_unchanged_at_end_row
17432 /* No lines reused because we displayed everything up to the
17433 bottom of the window. */
17434 && it.current_y < it.last_visible_y)
17435 {
17436 dvpos = (it.vpos
17437 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17438 current_matrix));
17439 dy = it.current_y - first_unchanged_at_end_row->y;
17440 run.current_y = first_unchanged_at_end_row->y;
17441 run.desired_y = run.current_y + dy;
17442 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17443 }
17444 else
17445 {
17446 delta = delta_bytes = dvpos = dy
17447 = run.current_y = run.desired_y = run.height = 0;
17448 first_unchanged_at_end_row = NULL;
17449 }
17450 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17451
17452
17453 /* Find the cursor if not already found. We have to decide whether
17454 PT will appear on this window (it sometimes doesn't, but this is
17455 not a very frequent case.) This decision has to be made before
17456 the current matrix is altered. A value of cursor.vpos < 0 means
17457 that PT is either in one of the lines beginning at
17458 first_unchanged_at_end_row or below the window. Don't care for
17459 lines that might be displayed later at the window end; as
17460 mentioned, this is not a frequent case. */
17461 if (w->cursor.vpos < 0)
17462 {
17463 /* Cursor in unchanged rows at the top? */
17464 if (PT < CHARPOS (start_pos)
17465 && last_unchanged_at_beg_row)
17466 {
17467 row = row_containing_pos (w, PT,
17468 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17469 last_unchanged_at_beg_row + 1, 0);
17470 if (row)
17471 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17472 }
17473
17474 /* Start from first_unchanged_at_end_row looking for PT. */
17475 else if (first_unchanged_at_end_row)
17476 {
17477 row = row_containing_pos (w, PT - delta,
17478 first_unchanged_at_end_row, NULL, 0);
17479 if (row)
17480 set_cursor_from_row (w, row, w->current_matrix, delta,
17481 delta_bytes, dy, dvpos);
17482 }
17483
17484 /* Give up if cursor was not found. */
17485 if (w->cursor.vpos < 0)
17486 {
17487 clear_glyph_matrix (w->desired_matrix);
17488 return -1;
17489 }
17490 }
17491
17492 /* Don't let the cursor end in the scroll margins. */
17493 {
17494 int this_scroll_margin, cursor_height;
17495
17496 this_scroll_margin =
17497 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17498 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17499 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17500
17501 if ((w->cursor.y < this_scroll_margin
17502 && CHARPOS (start) > BEGV)
17503 /* Old redisplay didn't take scroll margin into account at the bottom,
17504 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17505 || (w->cursor.y + (make_cursor_line_fully_visible_p
17506 ? cursor_height + this_scroll_margin
17507 : 1)) > it.last_visible_y)
17508 {
17509 w->cursor.vpos = -1;
17510 clear_glyph_matrix (w->desired_matrix);
17511 return -1;
17512 }
17513 }
17514
17515 /* Scroll the display. Do it before changing the current matrix so
17516 that xterm.c doesn't get confused about where the cursor glyph is
17517 found. */
17518 if (dy && run.height)
17519 {
17520 update_begin (f);
17521
17522 if (FRAME_WINDOW_P (f))
17523 {
17524 FRAME_RIF (f)->update_window_begin_hook (w);
17525 FRAME_RIF (f)->clear_window_mouse_face (w);
17526 FRAME_RIF (f)->scroll_run_hook (w, &run);
17527 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17528 }
17529 else
17530 {
17531 /* Terminal frame. In this case, dvpos gives the number of
17532 lines to scroll by; dvpos < 0 means scroll up. */
17533 int from_vpos
17534 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17535 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17536 int end = (WINDOW_TOP_EDGE_LINE (w)
17537 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17538 + window_internal_height (w));
17539
17540 #if defined (HAVE_GPM) || defined (MSDOS)
17541 x_clear_window_mouse_face (w);
17542 #endif
17543 /* Perform the operation on the screen. */
17544 if (dvpos > 0)
17545 {
17546 /* Scroll last_unchanged_at_beg_row to the end of the
17547 window down dvpos lines. */
17548 set_terminal_window (f, end);
17549
17550 /* On dumb terminals delete dvpos lines at the end
17551 before inserting dvpos empty lines. */
17552 if (!FRAME_SCROLL_REGION_OK (f))
17553 ins_del_lines (f, end - dvpos, -dvpos);
17554
17555 /* Insert dvpos empty lines in front of
17556 last_unchanged_at_beg_row. */
17557 ins_del_lines (f, from, dvpos);
17558 }
17559 else if (dvpos < 0)
17560 {
17561 /* Scroll up last_unchanged_at_beg_vpos to the end of
17562 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17563 set_terminal_window (f, end);
17564
17565 /* Delete dvpos lines in front of
17566 last_unchanged_at_beg_vpos. ins_del_lines will set
17567 the cursor to the given vpos and emit |dvpos| delete
17568 line sequences. */
17569 ins_del_lines (f, from + dvpos, dvpos);
17570
17571 /* On a dumb terminal insert dvpos empty lines at the
17572 end. */
17573 if (!FRAME_SCROLL_REGION_OK (f))
17574 ins_del_lines (f, end + dvpos, -dvpos);
17575 }
17576
17577 set_terminal_window (f, 0);
17578 }
17579
17580 update_end (f);
17581 }
17582
17583 /* Shift reused rows of the current matrix to the right position.
17584 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17585 text. */
17586 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17587 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17588 if (dvpos < 0)
17589 {
17590 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17591 bottom_vpos, dvpos);
17592 enable_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17593 bottom_vpos, 0);
17594 }
17595 else if (dvpos > 0)
17596 {
17597 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17598 bottom_vpos, dvpos);
17599 enable_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17600 first_unchanged_at_end_vpos + dvpos, 0);
17601 }
17602
17603 /* For frame-based redisplay, make sure that current frame and window
17604 matrix are in sync with respect to glyph memory. */
17605 if (!FRAME_WINDOW_P (f))
17606 sync_frame_with_window_matrix_rows (w);
17607
17608 /* Adjust buffer positions in reused rows. */
17609 if (delta || delta_bytes)
17610 increment_matrix_positions (current_matrix,
17611 first_unchanged_at_end_vpos + dvpos,
17612 bottom_vpos, delta, delta_bytes);
17613
17614 /* Adjust Y positions. */
17615 if (dy)
17616 shift_glyph_matrix (w, current_matrix,
17617 first_unchanged_at_end_vpos + dvpos,
17618 bottom_vpos, dy);
17619
17620 if (first_unchanged_at_end_row)
17621 {
17622 first_unchanged_at_end_row += dvpos;
17623 if (first_unchanged_at_end_row->y >= it.last_visible_y
17624 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17625 first_unchanged_at_end_row = NULL;
17626 }
17627
17628 /* If scrolling up, there may be some lines to display at the end of
17629 the window. */
17630 last_text_row_at_end = NULL;
17631 if (dy < 0)
17632 {
17633 /* Scrolling up can leave for example a partially visible line
17634 at the end of the window to be redisplayed. */
17635 /* Set last_row to the glyph row in the current matrix where the
17636 window end line is found. It has been moved up or down in
17637 the matrix by dvpos. */
17638 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17639 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17640
17641 /* If last_row is the window end line, it should display text. */
17642 xassert (last_row->displays_text_p);
17643
17644 /* If window end line was partially visible before, begin
17645 displaying at that line. Otherwise begin displaying with the
17646 line following it. */
17647 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17648 {
17649 init_to_row_start (&it, w, last_row);
17650 it.vpos = last_vpos;
17651 it.current_y = last_row->y;
17652 }
17653 else
17654 {
17655 init_to_row_end (&it, w, last_row);
17656 it.vpos = 1 + last_vpos;
17657 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17658 ++last_row;
17659 }
17660
17661 /* We may start in a continuation line. If so, we have to
17662 get the right continuation_lines_width and current_x. */
17663 it.continuation_lines_width = last_row->continuation_lines_width;
17664 it.hpos = it.current_x = 0;
17665
17666 /* Display the rest of the lines at the window end. */
17667 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17668 while (it.current_y < it.last_visible_y
17669 && !fonts_changed_p)
17670 {
17671 /* Is it always sure that the display agrees with lines in
17672 the current matrix? I don't think so, so we mark rows
17673 displayed invalid in the current matrix by setting their
17674 enabled_p flag to zero. */
17675 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17676 if (display_line (&it))
17677 last_text_row_at_end = it.glyph_row - 1;
17678 }
17679 }
17680
17681 /* Update window_end_pos and window_end_vpos. */
17682 if (first_unchanged_at_end_row
17683 && !last_text_row_at_end)
17684 {
17685 /* Window end line if one of the preserved rows from the current
17686 matrix. Set row to the last row displaying text in current
17687 matrix starting at first_unchanged_at_end_row, after
17688 scrolling. */
17689 xassert (first_unchanged_at_end_row->displays_text_p);
17690 row = find_last_row_displaying_text (w->current_matrix, &it,
17691 first_unchanged_at_end_row);
17692 xassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17693
17694 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17695 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17696 w->window_end_vpos
17697 = make_number (MATRIX_ROW_VPOS (row, w->current_matrix));
17698 xassert (w->window_end_bytepos >= 0);
17699 IF_DEBUG (debug_method_add (w, "A"));
17700 }
17701 else if (last_text_row_at_end)
17702 {
17703 w->window_end_pos
17704 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end));
17705 w->window_end_bytepos
17706 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17707 w->window_end_vpos
17708 = make_number (MATRIX_ROW_VPOS (last_text_row_at_end, desired_matrix));
17709 xassert (w->window_end_bytepos >= 0);
17710 IF_DEBUG (debug_method_add (w, "B"));
17711 }
17712 else if (last_text_row)
17713 {
17714 /* We have displayed either to the end of the window or at the
17715 end of the window, i.e. the last row with text is to be found
17716 in the desired matrix. */
17717 w->window_end_pos
17718 = make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row));
17719 w->window_end_bytepos
17720 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17721 w->window_end_vpos
17722 = make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix));
17723 xassert (w->window_end_bytepos >= 0);
17724 }
17725 else if (first_unchanged_at_end_row == NULL
17726 && last_text_row == NULL
17727 && last_text_row_at_end == NULL)
17728 {
17729 /* Displayed to end of window, but no line containing text was
17730 displayed. Lines were deleted at the end of the window. */
17731 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17732 int vpos = XFASTINT (w->window_end_vpos);
17733 struct glyph_row *current_row = current_matrix->rows + vpos;
17734 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17735
17736 for (row = NULL;
17737 row == NULL && vpos >= first_vpos;
17738 --vpos, --current_row, --desired_row)
17739 {
17740 if (desired_row->enabled_p)
17741 {
17742 if (desired_row->displays_text_p)
17743 row = desired_row;
17744 }
17745 else if (current_row->displays_text_p)
17746 row = current_row;
17747 }
17748
17749 xassert (row != NULL);
17750 w->window_end_vpos = make_number (vpos + 1);
17751 w->window_end_pos = make_number (Z - MATRIX_ROW_END_CHARPOS (row));
17752 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17753 xassert (w->window_end_bytepos >= 0);
17754 IF_DEBUG (debug_method_add (w, "C"));
17755 }
17756 else
17757 abort ();
17758
17759 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17760 debug_end_vpos = XFASTINT (w->window_end_vpos));
17761
17762 /* Record that display has not been completed. */
17763 w->window_end_valid = Qnil;
17764 w->desired_matrix->no_scrolling_p = 1;
17765 return 3;
17766
17767 #undef GIVE_UP
17768 }
17769
17770
17771 \f
17772 /***********************************************************************
17773 More debugging support
17774 ***********************************************************************/
17775
17776 #if GLYPH_DEBUG
17777
17778 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17779 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17780 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17781
17782
17783 /* Dump the contents of glyph matrix MATRIX on stderr.
17784
17785 GLYPHS 0 means don't show glyph contents.
17786 GLYPHS 1 means show glyphs in short form
17787 GLYPHS > 1 means show glyphs in long form. */
17788
17789 void
17790 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17791 {
17792 int i;
17793 for (i = 0; i < matrix->nrows; ++i)
17794 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17795 }
17796
17797
17798 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17799 the glyph row and area where the glyph comes from. */
17800
17801 void
17802 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17803 {
17804 if (glyph->type == CHAR_GLYPH)
17805 {
17806 fprintf (stderr,
17807 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17808 glyph - row->glyphs[TEXT_AREA],
17809 'C',
17810 glyph->charpos,
17811 (BUFFERP (glyph->object)
17812 ? 'B'
17813 : (STRINGP (glyph->object)
17814 ? 'S'
17815 : '-')),
17816 glyph->pixel_width,
17817 glyph->u.ch,
17818 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17819 ? glyph->u.ch
17820 : '.'),
17821 glyph->face_id,
17822 glyph->left_box_line_p,
17823 glyph->right_box_line_p);
17824 }
17825 else if (glyph->type == STRETCH_GLYPH)
17826 {
17827 fprintf (stderr,
17828 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17829 glyph - row->glyphs[TEXT_AREA],
17830 'S',
17831 glyph->charpos,
17832 (BUFFERP (glyph->object)
17833 ? 'B'
17834 : (STRINGP (glyph->object)
17835 ? 'S'
17836 : '-')),
17837 glyph->pixel_width,
17838 0,
17839 '.',
17840 glyph->face_id,
17841 glyph->left_box_line_p,
17842 glyph->right_box_line_p);
17843 }
17844 else if (glyph->type == IMAGE_GLYPH)
17845 {
17846 fprintf (stderr,
17847 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17848 glyph - row->glyphs[TEXT_AREA],
17849 'I',
17850 glyph->charpos,
17851 (BUFFERP (glyph->object)
17852 ? 'B'
17853 : (STRINGP (glyph->object)
17854 ? 'S'
17855 : '-')),
17856 glyph->pixel_width,
17857 glyph->u.img_id,
17858 '.',
17859 glyph->face_id,
17860 glyph->left_box_line_p,
17861 glyph->right_box_line_p);
17862 }
17863 else if (glyph->type == COMPOSITE_GLYPH)
17864 {
17865 fprintf (stderr,
17866 " %5td %4c %6"pI"d %c %3d 0x%05x",
17867 glyph - row->glyphs[TEXT_AREA],
17868 '+',
17869 glyph->charpos,
17870 (BUFFERP (glyph->object)
17871 ? 'B'
17872 : (STRINGP (glyph->object)
17873 ? 'S'
17874 : '-')),
17875 glyph->pixel_width,
17876 glyph->u.cmp.id);
17877 if (glyph->u.cmp.automatic)
17878 fprintf (stderr,
17879 "[%d-%d]",
17880 glyph->slice.cmp.from, glyph->slice.cmp.to);
17881 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17882 glyph->face_id,
17883 glyph->left_box_line_p,
17884 glyph->right_box_line_p);
17885 }
17886 }
17887
17888
17889 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17890 GLYPHS 0 means don't show glyph contents.
17891 GLYPHS 1 means show glyphs in short form
17892 GLYPHS > 1 means show glyphs in long form. */
17893
17894 void
17895 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17896 {
17897 if (glyphs != 1)
17898 {
17899 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17900 fprintf (stderr, "======================================================================\n");
17901
17902 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17903 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17904 vpos,
17905 MATRIX_ROW_START_CHARPOS (row),
17906 MATRIX_ROW_END_CHARPOS (row),
17907 row->used[TEXT_AREA],
17908 row->contains_overlapping_glyphs_p,
17909 row->enabled_p,
17910 row->truncated_on_left_p,
17911 row->truncated_on_right_p,
17912 row->continued_p,
17913 MATRIX_ROW_CONTINUATION_LINE_P (row),
17914 row->displays_text_p,
17915 row->ends_at_zv_p,
17916 row->fill_line_p,
17917 row->ends_in_middle_of_char_p,
17918 row->starts_in_middle_of_char_p,
17919 row->mouse_face_p,
17920 row->x,
17921 row->y,
17922 row->pixel_width,
17923 row->height,
17924 row->visible_height,
17925 row->ascent,
17926 row->phys_ascent);
17927 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
17928 row->end.overlay_string_index,
17929 row->continuation_lines_width);
17930 fprintf (stderr, "%9"pI"d %5"pI"d\n",
17931 CHARPOS (row->start.string_pos),
17932 CHARPOS (row->end.string_pos));
17933 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
17934 row->end.dpvec_index);
17935 }
17936
17937 if (glyphs > 1)
17938 {
17939 int area;
17940
17941 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17942 {
17943 struct glyph *glyph = row->glyphs[area];
17944 struct glyph *glyph_end = glyph + row->used[area];
17945
17946 /* Glyph for a line end in text. */
17947 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17948 ++glyph_end;
17949
17950 if (glyph < glyph_end)
17951 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
17952
17953 for (; glyph < glyph_end; ++glyph)
17954 dump_glyph (row, glyph, area);
17955 }
17956 }
17957 else if (glyphs == 1)
17958 {
17959 int area;
17960
17961 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17962 {
17963 char *s = (char *) alloca (row->used[area] + 1);
17964 int i;
17965
17966 for (i = 0; i < row->used[area]; ++i)
17967 {
17968 struct glyph *glyph = row->glyphs[area] + i;
17969 if (glyph->type == CHAR_GLYPH
17970 && glyph->u.ch < 0x80
17971 && glyph->u.ch >= ' ')
17972 s[i] = glyph->u.ch;
17973 else
17974 s[i] = '.';
17975 }
17976
17977 s[i] = '\0';
17978 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17979 }
17980 }
17981 }
17982
17983
17984 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17985 Sdump_glyph_matrix, 0, 1, "p",
17986 doc: /* Dump the current matrix of the selected window to stderr.
17987 Shows contents of glyph row structures. With non-nil
17988 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17989 glyphs in short form, otherwise show glyphs in long form. */)
17990 (Lisp_Object glyphs)
17991 {
17992 struct window *w = XWINDOW (selected_window);
17993 struct buffer *buffer = XBUFFER (w->buffer);
17994
17995 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17996 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17997 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17998 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17999 fprintf (stderr, "=============================================\n");
18000 dump_glyph_matrix (w->current_matrix,
18001 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18002 return Qnil;
18003 }
18004
18005
18006 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18007 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18008 (void)
18009 {
18010 struct frame *f = XFRAME (selected_frame);
18011 dump_glyph_matrix (f->current_matrix, 1);
18012 return Qnil;
18013 }
18014
18015
18016 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18017 doc: /* Dump glyph row ROW to stderr.
18018 GLYPH 0 means don't dump glyphs.
18019 GLYPH 1 means dump glyphs in short form.
18020 GLYPH > 1 or omitted means dump glyphs in long form. */)
18021 (Lisp_Object row, Lisp_Object glyphs)
18022 {
18023 struct glyph_matrix *matrix;
18024 EMACS_INT vpos;
18025
18026 CHECK_NUMBER (row);
18027 matrix = XWINDOW (selected_window)->current_matrix;
18028 vpos = XINT (row);
18029 if (vpos >= 0 && vpos < matrix->nrows)
18030 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18031 vpos,
18032 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18033 return Qnil;
18034 }
18035
18036
18037 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18038 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18039 GLYPH 0 means don't dump glyphs.
18040 GLYPH 1 means dump glyphs in short form.
18041 GLYPH > 1 or omitted means dump glyphs in long form. */)
18042 (Lisp_Object row, Lisp_Object glyphs)
18043 {
18044 struct frame *sf = SELECTED_FRAME ();
18045 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18046 EMACS_INT vpos;
18047
18048 CHECK_NUMBER (row);
18049 vpos = XINT (row);
18050 if (vpos >= 0 && vpos < m->nrows)
18051 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18052 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18053 return Qnil;
18054 }
18055
18056
18057 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18058 doc: /* Toggle tracing of redisplay.
18059 With ARG, turn tracing on if and only if ARG is positive. */)
18060 (Lisp_Object arg)
18061 {
18062 if (NILP (arg))
18063 trace_redisplay_p = !trace_redisplay_p;
18064 else
18065 {
18066 arg = Fprefix_numeric_value (arg);
18067 trace_redisplay_p = XINT (arg) > 0;
18068 }
18069
18070 return Qnil;
18071 }
18072
18073
18074 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18075 doc: /* Like `format', but print result to stderr.
18076 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18077 (ptrdiff_t nargs, Lisp_Object *args)
18078 {
18079 Lisp_Object s = Fformat (nargs, args);
18080 fprintf (stderr, "%s", SDATA (s));
18081 return Qnil;
18082 }
18083
18084 #endif /* GLYPH_DEBUG */
18085
18086
18087 \f
18088 /***********************************************************************
18089 Building Desired Matrix Rows
18090 ***********************************************************************/
18091
18092 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18093 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18094
18095 static struct glyph_row *
18096 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18097 {
18098 struct frame *f = XFRAME (WINDOW_FRAME (w));
18099 struct buffer *buffer = XBUFFER (w->buffer);
18100 struct buffer *old = current_buffer;
18101 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18102 int arrow_len = SCHARS (overlay_arrow_string);
18103 const unsigned char *arrow_end = arrow_string + arrow_len;
18104 const unsigned char *p;
18105 struct it it;
18106 int multibyte_p;
18107 int n_glyphs_before;
18108
18109 set_buffer_temp (buffer);
18110 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18111 it.glyph_row->used[TEXT_AREA] = 0;
18112 SET_TEXT_POS (it.position, 0, 0);
18113
18114 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18115 p = arrow_string;
18116 while (p < arrow_end)
18117 {
18118 Lisp_Object face, ilisp;
18119
18120 /* Get the next character. */
18121 if (multibyte_p)
18122 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18123 else
18124 {
18125 it.c = it.char_to_display = *p, it.len = 1;
18126 if (! ASCII_CHAR_P (it.c))
18127 it.char_to_display = BYTE8_TO_CHAR (it.c);
18128 }
18129 p += it.len;
18130
18131 /* Get its face. */
18132 ilisp = make_number (p - arrow_string);
18133 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18134 it.face_id = compute_char_face (f, it.char_to_display, face);
18135
18136 /* Compute its width, get its glyphs. */
18137 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18138 SET_TEXT_POS (it.position, -1, -1);
18139 PRODUCE_GLYPHS (&it);
18140
18141 /* If this character doesn't fit any more in the line, we have
18142 to remove some glyphs. */
18143 if (it.current_x > it.last_visible_x)
18144 {
18145 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18146 break;
18147 }
18148 }
18149
18150 set_buffer_temp (old);
18151 return it.glyph_row;
18152 }
18153
18154
18155 /* Insert truncation glyphs at the start of IT->glyph_row. Truncation
18156 glyphs are only inserted for terminal frames since we can't really
18157 win with truncation glyphs when partially visible glyphs are
18158 involved. Which glyphs to insert is determined by
18159 produce_special_glyphs. */
18160
18161 static void
18162 insert_left_trunc_glyphs (struct it *it)
18163 {
18164 struct it truncate_it;
18165 struct glyph *from, *end, *to, *toend;
18166
18167 xassert (!FRAME_WINDOW_P (it->f));
18168
18169 /* Get the truncation glyphs. */
18170 truncate_it = *it;
18171 truncate_it.current_x = 0;
18172 truncate_it.face_id = DEFAULT_FACE_ID;
18173 truncate_it.glyph_row = &scratch_glyph_row;
18174 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18175 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18176 truncate_it.object = make_number (0);
18177 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18178
18179 /* Overwrite glyphs from IT with truncation glyphs. */
18180 if (!it->glyph_row->reversed_p)
18181 {
18182 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18183 end = from + truncate_it.glyph_row->used[TEXT_AREA];
18184 to = it->glyph_row->glyphs[TEXT_AREA];
18185 toend = to + it->glyph_row->used[TEXT_AREA];
18186
18187 while (from < end)
18188 *to++ = *from++;
18189
18190 /* There may be padding glyphs left over. Overwrite them too. */
18191 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18192 {
18193 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18194 while (from < end)
18195 *to++ = *from++;
18196 }
18197
18198 if (to > toend)
18199 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18200 }
18201 else
18202 {
18203 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18204 that back to front. */
18205 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18206 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18207 toend = it->glyph_row->glyphs[TEXT_AREA];
18208 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18209
18210 while (from >= end && to >= toend)
18211 *to-- = *from--;
18212 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18213 {
18214 from =
18215 truncate_it.glyph_row->glyphs[TEXT_AREA]
18216 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18217 while (from >= end && to >= toend)
18218 *to-- = *from--;
18219 }
18220 if (from >= end)
18221 {
18222 /* Need to free some room before prepending additional
18223 glyphs. */
18224 int move_by = from - end + 1;
18225 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18226 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18227
18228 for ( ; g >= g0; g--)
18229 g[move_by] = *g;
18230 while (from >= end)
18231 *to-- = *from--;
18232 it->glyph_row->used[TEXT_AREA] += move_by;
18233 }
18234 }
18235 }
18236
18237 /* Compute the hash code for ROW. */
18238 unsigned
18239 row_hash (struct glyph_row *row)
18240 {
18241 int area, k;
18242 unsigned hashval = 0;
18243
18244 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18245 for (k = 0; k < row->used[area]; ++k)
18246 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18247 + row->glyphs[area][k].u.val
18248 + row->glyphs[area][k].face_id
18249 + row->glyphs[area][k].padding_p
18250 + (row->glyphs[area][k].type << 2));
18251
18252 return hashval;
18253 }
18254
18255 /* Compute the pixel height and width of IT->glyph_row.
18256
18257 Most of the time, ascent and height of a display line will be equal
18258 to the max_ascent and max_height values of the display iterator
18259 structure. This is not the case if
18260
18261 1. We hit ZV without displaying anything. In this case, max_ascent
18262 and max_height will be zero.
18263
18264 2. We have some glyphs that don't contribute to the line height.
18265 (The glyph row flag contributes_to_line_height_p is for future
18266 pixmap extensions).
18267
18268 The first case is easily covered by using default values because in
18269 these cases, the line height does not really matter, except that it
18270 must not be zero. */
18271
18272 static void
18273 compute_line_metrics (struct it *it)
18274 {
18275 struct glyph_row *row = it->glyph_row;
18276
18277 if (FRAME_WINDOW_P (it->f))
18278 {
18279 int i, min_y, max_y;
18280
18281 /* The line may consist of one space only, that was added to
18282 place the cursor on it. If so, the row's height hasn't been
18283 computed yet. */
18284 if (row->height == 0)
18285 {
18286 if (it->max_ascent + it->max_descent == 0)
18287 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18288 row->ascent = it->max_ascent;
18289 row->height = it->max_ascent + it->max_descent;
18290 row->phys_ascent = it->max_phys_ascent;
18291 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18292 row->extra_line_spacing = it->max_extra_line_spacing;
18293 }
18294
18295 /* Compute the width of this line. */
18296 row->pixel_width = row->x;
18297 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18298 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18299
18300 xassert (row->pixel_width >= 0);
18301 xassert (row->ascent >= 0 && row->height > 0);
18302
18303 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18304 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18305
18306 /* If first line's physical ascent is larger than its logical
18307 ascent, use the physical ascent, and make the row taller.
18308 This makes accented characters fully visible. */
18309 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18310 && row->phys_ascent > row->ascent)
18311 {
18312 row->height += row->phys_ascent - row->ascent;
18313 row->ascent = row->phys_ascent;
18314 }
18315
18316 /* Compute how much of the line is visible. */
18317 row->visible_height = row->height;
18318
18319 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18320 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18321
18322 if (row->y < min_y)
18323 row->visible_height -= min_y - row->y;
18324 if (row->y + row->height > max_y)
18325 row->visible_height -= row->y + row->height - max_y;
18326 }
18327 else
18328 {
18329 row->pixel_width = row->used[TEXT_AREA];
18330 if (row->continued_p)
18331 row->pixel_width -= it->continuation_pixel_width;
18332 else if (row->truncated_on_right_p)
18333 row->pixel_width -= it->truncation_pixel_width;
18334 row->ascent = row->phys_ascent = 0;
18335 row->height = row->phys_height = row->visible_height = 1;
18336 row->extra_line_spacing = 0;
18337 }
18338
18339 /* Compute a hash code for this row. */
18340 row->hash = row_hash (row);
18341
18342 it->max_ascent = it->max_descent = 0;
18343 it->max_phys_ascent = it->max_phys_descent = 0;
18344 }
18345
18346
18347 /* Append one space to the glyph row of iterator IT if doing a
18348 window-based redisplay. The space has the same face as
18349 IT->face_id. Value is non-zero if a space was added.
18350
18351 This function is called to make sure that there is always one glyph
18352 at the end of a glyph row that the cursor can be set on under
18353 window-systems. (If there weren't such a glyph we would not know
18354 how wide and tall a box cursor should be displayed).
18355
18356 At the same time this space let's a nicely handle clearing to the
18357 end of the line if the row ends in italic text. */
18358
18359 static int
18360 append_space_for_newline (struct it *it, int default_face_p)
18361 {
18362 if (FRAME_WINDOW_P (it->f))
18363 {
18364 int n = it->glyph_row->used[TEXT_AREA];
18365
18366 if (it->glyph_row->glyphs[TEXT_AREA] + n
18367 < it->glyph_row->glyphs[1 + TEXT_AREA])
18368 {
18369 /* Save some values that must not be changed.
18370 Must save IT->c and IT->len because otherwise
18371 ITERATOR_AT_END_P wouldn't work anymore after
18372 append_space_for_newline has been called. */
18373 enum display_element_type saved_what = it->what;
18374 int saved_c = it->c, saved_len = it->len;
18375 int saved_char_to_display = it->char_to_display;
18376 int saved_x = it->current_x;
18377 int saved_face_id = it->face_id;
18378 struct text_pos saved_pos;
18379 Lisp_Object saved_object;
18380 struct face *face;
18381
18382 saved_object = it->object;
18383 saved_pos = it->position;
18384
18385 it->what = IT_CHARACTER;
18386 memset (&it->position, 0, sizeof it->position);
18387 it->object = make_number (0);
18388 it->c = it->char_to_display = ' ';
18389 it->len = 1;
18390
18391 /* If the default face was remapped, be sure to use the
18392 remapped face for the appended newline. */
18393 if (default_face_p)
18394 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18395 else if (it->face_before_selective_p)
18396 it->face_id = it->saved_face_id;
18397 face = FACE_FROM_ID (it->f, it->face_id);
18398 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18399
18400 PRODUCE_GLYPHS (it);
18401
18402 it->override_ascent = -1;
18403 it->constrain_row_ascent_descent_p = 0;
18404 it->current_x = saved_x;
18405 it->object = saved_object;
18406 it->position = saved_pos;
18407 it->what = saved_what;
18408 it->face_id = saved_face_id;
18409 it->len = saved_len;
18410 it->c = saved_c;
18411 it->char_to_display = saved_char_to_display;
18412 return 1;
18413 }
18414 }
18415
18416 return 0;
18417 }
18418
18419
18420 /* Extend the face of the last glyph in the text area of IT->glyph_row
18421 to the end of the display line. Called from display_line. If the
18422 glyph row is empty, add a space glyph to it so that we know the
18423 face to draw. Set the glyph row flag fill_line_p. If the glyph
18424 row is R2L, prepend a stretch glyph to cover the empty space to the
18425 left of the leftmost glyph. */
18426
18427 static void
18428 extend_face_to_end_of_line (struct it *it)
18429 {
18430 struct face *face, *default_face;
18431 struct frame *f = it->f;
18432
18433 /* If line is already filled, do nothing. Non window-system frames
18434 get a grace of one more ``pixel'' because their characters are
18435 1-``pixel'' wide, so they hit the equality too early. This grace
18436 is needed only for R2L rows that are not continued, to produce
18437 one extra blank where we could display the cursor. */
18438 if (it->current_x >= it->last_visible_x
18439 + (!FRAME_WINDOW_P (f)
18440 && it->glyph_row->reversed_p
18441 && !it->glyph_row->continued_p))
18442 return;
18443
18444 /* The default face, possibly remapped. */
18445 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18446
18447 /* Face extension extends the background and box of IT->face_id
18448 to the end of the line. If the background equals the background
18449 of the frame, we don't have to do anything. */
18450 if (it->face_before_selective_p)
18451 face = FACE_FROM_ID (f, it->saved_face_id);
18452 else
18453 face = FACE_FROM_ID (f, it->face_id);
18454
18455 if (FRAME_WINDOW_P (f)
18456 && it->glyph_row->displays_text_p
18457 && face->box == FACE_NO_BOX
18458 && face->background == FRAME_BACKGROUND_PIXEL (f)
18459 && !face->stipple
18460 && !it->glyph_row->reversed_p)
18461 return;
18462
18463 /* Set the glyph row flag indicating that the face of the last glyph
18464 in the text area has to be drawn to the end of the text area. */
18465 it->glyph_row->fill_line_p = 1;
18466
18467 /* If current character of IT is not ASCII, make sure we have the
18468 ASCII face. This will be automatically undone the next time
18469 get_next_display_element returns a multibyte character. Note
18470 that the character will always be single byte in unibyte
18471 text. */
18472 if (!ASCII_CHAR_P (it->c))
18473 {
18474 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18475 }
18476
18477 if (FRAME_WINDOW_P (f))
18478 {
18479 /* If the row is empty, add a space with the current face of IT,
18480 so that we know which face to draw. */
18481 if (it->glyph_row->used[TEXT_AREA] == 0)
18482 {
18483 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18484 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18485 it->glyph_row->used[TEXT_AREA] = 1;
18486 }
18487 #ifdef HAVE_WINDOW_SYSTEM
18488 if (it->glyph_row->reversed_p)
18489 {
18490 /* Prepend a stretch glyph to the row, such that the
18491 rightmost glyph will be drawn flushed all the way to the
18492 right margin of the window. The stretch glyph that will
18493 occupy the empty space, if any, to the left of the
18494 glyphs. */
18495 struct font *font = face->font ? face->font : FRAME_FONT (f);
18496 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18497 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18498 struct glyph *g;
18499 int row_width, stretch_ascent, stretch_width;
18500 struct text_pos saved_pos;
18501 int saved_face_id, saved_avoid_cursor;
18502
18503 for (row_width = 0, g = row_start; g < row_end; g++)
18504 row_width += g->pixel_width;
18505 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18506 if (stretch_width > 0)
18507 {
18508 stretch_ascent =
18509 (((it->ascent + it->descent)
18510 * FONT_BASE (font)) / FONT_HEIGHT (font));
18511 saved_pos = it->position;
18512 memset (&it->position, 0, sizeof it->position);
18513 saved_avoid_cursor = it->avoid_cursor_p;
18514 it->avoid_cursor_p = 1;
18515 saved_face_id = it->face_id;
18516 /* The last row's stretch glyph should get the default
18517 face, to avoid painting the rest of the window with
18518 the region face, if the region ends at ZV. */
18519 if (it->glyph_row->ends_at_zv_p)
18520 it->face_id = default_face->id;
18521 else
18522 it->face_id = face->id;
18523 append_stretch_glyph (it, make_number (0), stretch_width,
18524 it->ascent + it->descent, stretch_ascent);
18525 it->position = saved_pos;
18526 it->avoid_cursor_p = saved_avoid_cursor;
18527 it->face_id = saved_face_id;
18528 }
18529 }
18530 #endif /* HAVE_WINDOW_SYSTEM */
18531 }
18532 else
18533 {
18534 /* Save some values that must not be changed. */
18535 int saved_x = it->current_x;
18536 struct text_pos saved_pos;
18537 Lisp_Object saved_object;
18538 enum display_element_type saved_what = it->what;
18539 int saved_face_id = it->face_id;
18540
18541 saved_object = it->object;
18542 saved_pos = it->position;
18543
18544 it->what = IT_CHARACTER;
18545 memset (&it->position, 0, sizeof it->position);
18546 it->object = make_number (0);
18547 it->c = it->char_to_display = ' ';
18548 it->len = 1;
18549 /* The last row's blank glyphs should get the default face, to
18550 avoid painting the rest of the window with the region face,
18551 if the region ends at ZV. */
18552 if (it->glyph_row->ends_at_zv_p)
18553 it->face_id = default_face->id;
18554 else
18555 it->face_id = face->id;
18556
18557 PRODUCE_GLYPHS (it);
18558
18559 while (it->current_x <= it->last_visible_x)
18560 PRODUCE_GLYPHS (it);
18561
18562 /* Don't count these blanks really. It would let us insert a left
18563 truncation glyph below and make us set the cursor on them, maybe. */
18564 it->current_x = saved_x;
18565 it->object = saved_object;
18566 it->position = saved_pos;
18567 it->what = saved_what;
18568 it->face_id = saved_face_id;
18569 }
18570 }
18571
18572
18573 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18574 trailing whitespace. */
18575
18576 static int
18577 trailing_whitespace_p (ptrdiff_t charpos)
18578 {
18579 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18580 int c = 0;
18581
18582 while (bytepos < ZV_BYTE
18583 && (c = FETCH_CHAR (bytepos),
18584 c == ' ' || c == '\t'))
18585 ++bytepos;
18586
18587 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18588 {
18589 if (bytepos != PT_BYTE)
18590 return 1;
18591 }
18592 return 0;
18593 }
18594
18595
18596 /* Highlight trailing whitespace, if any, in ROW. */
18597
18598 static void
18599 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18600 {
18601 int used = row->used[TEXT_AREA];
18602
18603 if (used)
18604 {
18605 struct glyph *start = row->glyphs[TEXT_AREA];
18606 struct glyph *glyph = start + used - 1;
18607
18608 if (row->reversed_p)
18609 {
18610 /* Right-to-left rows need to be processed in the opposite
18611 direction, so swap the edge pointers. */
18612 glyph = start;
18613 start = row->glyphs[TEXT_AREA] + used - 1;
18614 }
18615
18616 /* Skip over glyphs inserted to display the cursor at the
18617 end of a line, for extending the face of the last glyph
18618 to the end of the line on terminals, and for truncation
18619 and continuation glyphs. */
18620 if (!row->reversed_p)
18621 {
18622 while (glyph >= start
18623 && glyph->type == CHAR_GLYPH
18624 && INTEGERP (glyph->object))
18625 --glyph;
18626 }
18627 else
18628 {
18629 while (glyph <= start
18630 && glyph->type == CHAR_GLYPH
18631 && INTEGERP (glyph->object))
18632 ++glyph;
18633 }
18634
18635 /* If last glyph is a space or stretch, and it's trailing
18636 whitespace, set the face of all trailing whitespace glyphs in
18637 IT->glyph_row to `trailing-whitespace'. */
18638 if ((row->reversed_p ? glyph <= start : glyph >= start)
18639 && BUFFERP (glyph->object)
18640 && (glyph->type == STRETCH_GLYPH
18641 || (glyph->type == CHAR_GLYPH
18642 && glyph->u.ch == ' '))
18643 && trailing_whitespace_p (glyph->charpos))
18644 {
18645 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18646 if (face_id < 0)
18647 return;
18648
18649 if (!row->reversed_p)
18650 {
18651 while (glyph >= start
18652 && BUFFERP (glyph->object)
18653 && (glyph->type == STRETCH_GLYPH
18654 || (glyph->type == CHAR_GLYPH
18655 && glyph->u.ch == ' ')))
18656 (glyph--)->face_id = face_id;
18657 }
18658 else
18659 {
18660 while (glyph <= start
18661 && BUFFERP (glyph->object)
18662 && (glyph->type == STRETCH_GLYPH
18663 || (glyph->type == CHAR_GLYPH
18664 && glyph->u.ch == ' ')))
18665 (glyph++)->face_id = face_id;
18666 }
18667 }
18668 }
18669 }
18670
18671
18672 /* Value is non-zero if glyph row ROW should be
18673 used to hold the cursor. */
18674
18675 static int
18676 cursor_row_p (struct glyph_row *row)
18677 {
18678 int result = 1;
18679
18680 if (PT == CHARPOS (row->end.pos)
18681 || PT == MATRIX_ROW_END_CHARPOS (row))
18682 {
18683 /* Suppose the row ends on a string.
18684 Unless the row is continued, that means it ends on a newline
18685 in the string. If it's anything other than a display string
18686 (e.g., a before-string from an overlay), we don't want the
18687 cursor there. (This heuristic seems to give the optimal
18688 behavior for the various types of multi-line strings.)
18689 One exception: if the string has `cursor' property on one of
18690 its characters, we _do_ want the cursor there. */
18691 if (CHARPOS (row->end.string_pos) >= 0)
18692 {
18693 if (row->continued_p)
18694 result = 1;
18695 else
18696 {
18697 /* Check for `display' property. */
18698 struct glyph *beg = row->glyphs[TEXT_AREA];
18699 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18700 struct glyph *glyph;
18701
18702 result = 0;
18703 for (glyph = end; glyph >= beg; --glyph)
18704 if (STRINGP (glyph->object))
18705 {
18706 Lisp_Object prop
18707 = Fget_char_property (make_number (PT),
18708 Qdisplay, Qnil);
18709 result =
18710 (!NILP (prop)
18711 && display_prop_string_p (prop, glyph->object));
18712 /* If there's a `cursor' property on one of the
18713 string's characters, this row is a cursor row,
18714 even though this is not a display string. */
18715 if (!result)
18716 {
18717 Lisp_Object s = glyph->object;
18718
18719 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18720 {
18721 ptrdiff_t gpos = glyph->charpos;
18722
18723 if (!NILP (Fget_char_property (make_number (gpos),
18724 Qcursor, s)))
18725 {
18726 result = 1;
18727 break;
18728 }
18729 }
18730 }
18731 break;
18732 }
18733 }
18734 }
18735 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18736 {
18737 /* If the row ends in middle of a real character,
18738 and the line is continued, we want the cursor here.
18739 That's because CHARPOS (ROW->end.pos) would equal
18740 PT if PT is before the character. */
18741 if (!row->ends_in_ellipsis_p)
18742 result = row->continued_p;
18743 else
18744 /* If the row ends in an ellipsis, then
18745 CHARPOS (ROW->end.pos) will equal point after the
18746 invisible text. We want that position to be displayed
18747 after the ellipsis. */
18748 result = 0;
18749 }
18750 /* If the row ends at ZV, display the cursor at the end of that
18751 row instead of at the start of the row below. */
18752 else if (row->ends_at_zv_p)
18753 result = 1;
18754 else
18755 result = 0;
18756 }
18757
18758 return result;
18759 }
18760
18761 \f
18762
18763 /* Push the property PROP so that it will be rendered at the current
18764 position in IT. Return 1 if PROP was successfully pushed, 0
18765 otherwise. Called from handle_line_prefix to handle the
18766 `line-prefix' and `wrap-prefix' properties. */
18767
18768 static int
18769 push_prefix_prop (struct it *it, Lisp_Object prop)
18770 {
18771 struct text_pos pos =
18772 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18773
18774 xassert (it->method == GET_FROM_BUFFER
18775 || it->method == GET_FROM_DISPLAY_VECTOR
18776 || it->method == GET_FROM_STRING);
18777
18778 /* We need to save the current buffer/string position, so it will be
18779 restored by pop_it, because iterate_out_of_display_property
18780 depends on that being set correctly, but some situations leave
18781 it->position not yet set when this function is called. */
18782 push_it (it, &pos);
18783
18784 if (STRINGP (prop))
18785 {
18786 if (SCHARS (prop) == 0)
18787 {
18788 pop_it (it);
18789 return 0;
18790 }
18791
18792 it->string = prop;
18793 it->string_from_prefix_prop_p = 1;
18794 it->multibyte_p = STRING_MULTIBYTE (it->string);
18795 it->current.overlay_string_index = -1;
18796 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18797 it->end_charpos = it->string_nchars = SCHARS (it->string);
18798 it->method = GET_FROM_STRING;
18799 it->stop_charpos = 0;
18800 it->prev_stop = 0;
18801 it->base_level_stop = 0;
18802
18803 /* Force paragraph direction to be that of the parent
18804 buffer/string. */
18805 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18806 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18807 else
18808 it->paragraph_embedding = L2R;
18809
18810 /* Set up the bidi iterator for this display string. */
18811 if (it->bidi_p)
18812 {
18813 it->bidi_it.string.lstring = it->string;
18814 it->bidi_it.string.s = NULL;
18815 it->bidi_it.string.schars = it->end_charpos;
18816 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18817 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18818 it->bidi_it.string.unibyte = !it->multibyte_p;
18819 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18820 }
18821 }
18822 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18823 {
18824 it->method = GET_FROM_STRETCH;
18825 it->object = prop;
18826 }
18827 #ifdef HAVE_WINDOW_SYSTEM
18828 else if (IMAGEP (prop))
18829 {
18830 it->what = IT_IMAGE;
18831 it->image_id = lookup_image (it->f, prop);
18832 it->method = GET_FROM_IMAGE;
18833 }
18834 #endif /* HAVE_WINDOW_SYSTEM */
18835 else
18836 {
18837 pop_it (it); /* bogus display property, give up */
18838 return 0;
18839 }
18840
18841 return 1;
18842 }
18843
18844 /* Return the character-property PROP at the current position in IT. */
18845
18846 static Lisp_Object
18847 get_it_property (struct it *it, Lisp_Object prop)
18848 {
18849 Lisp_Object position;
18850
18851 if (STRINGP (it->object))
18852 position = make_number (IT_STRING_CHARPOS (*it));
18853 else if (BUFFERP (it->object))
18854 position = make_number (IT_CHARPOS (*it));
18855 else
18856 return Qnil;
18857
18858 return Fget_char_property (position, prop, it->object);
18859 }
18860
18861 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18862
18863 static void
18864 handle_line_prefix (struct it *it)
18865 {
18866 Lisp_Object prefix;
18867
18868 if (it->continuation_lines_width > 0)
18869 {
18870 prefix = get_it_property (it, Qwrap_prefix);
18871 if (NILP (prefix))
18872 prefix = Vwrap_prefix;
18873 }
18874 else
18875 {
18876 prefix = get_it_property (it, Qline_prefix);
18877 if (NILP (prefix))
18878 prefix = Vline_prefix;
18879 }
18880 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18881 {
18882 /* If the prefix is wider than the window, and we try to wrap
18883 it, it would acquire its own wrap prefix, and so on till the
18884 iterator stack overflows. So, don't wrap the prefix. */
18885 it->line_wrap = TRUNCATE;
18886 it->avoid_cursor_p = 1;
18887 }
18888 }
18889
18890 \f
18891
18892 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18893 only for R2L lines from display_line and display_string, when they
18894 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18895 the line/string needs to be continued on the next glyph row. */
18896 static void
18897 unproduce_glyphs (struct it *it, int n)
18898 {
18899 struct glyph *glyph, *end;
18900
18901 xassert (it->glyph_row);
18902 xassert (it->glyph_row->reversed_p);
18903 xassert (it->area == TEXT_AREA);
18904 xassert (n <= it->glyph_row->used[TEXT_AREA]);
18905
18906 if (n > it->glyph_row->used[TEXT_AREA])
18907 n = it->glyph_row->used[TEXT_AREA];
18908 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18909 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18910 for ( ; glyph < end; glyph++)
18911 glyph[-n] = *glyph;
18912 }
18913
18914 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18915 and ROW->maxpos. */
18916 static void
18917 find_row_edges (struct it *it, struct glyph_row *row,
18918 ptrdiff_t min_pos, ptrdiff_t min_bpos,
18919 ptrdiff_t max_pos, ptrdiff_t max_bpos)
18920 {
18921 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18922 lines' rows is implemented for bidi-reordered rows. */
18923
18924 /* ROW->minpos is the value of min_pos, the minimal buffer position
18925 we have in ROW, or ROW->start.pos if that is smaller. */
18926 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18927 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18928 else
18929 /* We didn't find buffer positions smaller than ROW->start, or
18930 didn't find _any_ valid buffer positions in any of the glyphs,
18931 so we must trust the iterator's computed positions. */
18932 row->minpos = row->start.pos;
18933 if (max_pos <= 0)
18934 {
18935 max_pos = CHARPOS (it->current.pos);
18936 max_bpos = BYTEPOS (it->current.pos);
18937 }
18938
18939 /* Here are the various use-cases for ending the row, and the
18940 corresponding values for ROW->maxpos:
18941
18942 Line ends in a newline from buffer eol_pos + 1
18943 Line is continued from buffer max_pos + 1
18944 Line is truncated on right it->current.pos
18945 Line ends in a newline from string max_pos + 1(*)
18946 (*) + 1 only when line ends in a forward scan
18947 Line is continued from string max_pos
18948 Line is continued from display vector max_pos
18949 Line is entirely from a string min_pos == max_pos
18950 Line is entirely from a display vector min_pos == max_pos
18951 Line that ends at ZV ZV
18952
18953 If you discover other use-cases, please add them here as
18954 appropriate. */
18955 if (row->ends_at_zv_p)
18956 row->maxpos = it->current.pos;
18957 else if (row->used[TEXT_AREA])
18958 {
18959 int seen_this_string = 0;
18960 struct glyph_row *r1 = row - 1;
18961
18962 /* Did we see the same display string on the previous row? */
18963 if (STRINGP (it->object)
18964 /* this is not the first row */
18965 && row > it->w->desired_matrix->rows
18966 /* previous row is not the header line */
18967 && !r1->mode_line_p
18968 /* previous row also ends in a newline from a string */
18969 && r1->ends_in_newline_from_string_p)
18970 {
18971 struct glyph *start, *end;
18972
18973 /* Search for the last glyph of the previous row that came
18974 from buffer or string. Depending on whether the row is
18975 L2R or R2L, we need to process it front to back or the
18976 other way round. */
18977 if (!r1->reversed_p)
18978 {
18979 start = r1->glyphs[TEXT_AREA];
18980 end = start + r1->used[TEXT_AREA];
18981 /* Glyphs inserted by redisplay have an integer (zero)
18982 as their object. */
18983 while (end > start
18984 && INTEGERP ((end - 1)->object)
18985 && (end - 1)->charpos <= 0)
18986 --end;
18987 if (end > start)
18988 {
18989 if (EQ ((end - 1)->object, it->object))
18990 seen_this_string = 1;
18991 }
18992 else
18993 /* If all the glyphs of the previous row were inserted
18994 by redisplay, it means the previous row was
18995 produced from a single newline, which is only
18996 possible if that newline came from the same string
18997 as the one which produced this ROW. */
18998 seen_this_string = 1;
18999 }
19000 else
19001 {
19002 end = r1->glyphs[TEXT_AREA] - 1;
19003 start = end + r1->used[TEXT_AREA];
19004 while (end < start
19005 && INTEGERP ((end + 1)->object)
19006 && (end + 1)->charpos <= 0)
19007 ++end;
19008 if (end < start)
19009 {
19010 if (EQ ((end + 1)->object, it->object))
19011 seen_this_string = 1;
19012 }
19013 else
19014 seen_this_string = 1;
19015 }
19016 }
19017 /* Take note of each display string that covers a newline only
19018 once, the first time we see it. This is for when a display
19019 string includes more than one newline in it. */
19020 if (row->ends_in_newline_from_string_p && !seen_this_string)
19021 {
19022 /* If we were scanning the buffer forward when we displayed
19023 the string, we want to account for at least one buffer
19024 position that belongs to this row (position covered by
19025 the display string), so that cursor positioning will
19026 consider this row as a candidate when point is at the end
19027 of the visual line represented by this row. This is not
19028 required when scanning back, because max_pos will already
19029 have a much larger value. */
19030 if (CHARPOS (row->end.pos) > max_pos)
19031 INC_BOTH (max_pos, max_bpos);
19032 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19033 }
19034 else if (CHARPOS (it->eol_pos) > 0)
19035 SET_TEXT_POS (row->maxpos,
19036 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19037 else if (row->continued_p)
19038 {
19039 /* If max_pos is different from IT's current position, it
19040 means IT->method does not belong to the display element
19041 at max_pos. However, it also means that the display
19042 element at max_pos was displayed in its entirety on this
19043 line, which is equivalent to saying that the next line
19044 starts at the next buffer position. */
19045 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19046 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19047 else
19048 {
19049 INC_BOTH (max_pos, max_bpos);
19050 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19051 }
19052 }
19053 else if (row->truncated_on_right_p)
19054 /* display_line already called reseat_at_next_visible_line_start,
19055 which puts the iterator at the beginning of the next line, in
19056 the logical order. */
19057 row->maxpos = it->current.pos;
19058 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19059 /* A line that is entirely from a string/image/stretch... */
19060 row->maxpos = row->minpos;
19061 else
19062 abort ();
19063 }
19064 else
19065 row->maxpos = it->current.pos;
19066 }
19067
19068 /* Construct the glyph row IT->glyph_row in the desired matrix of
19069 IT->w from text at the current position of IT. See dispextern.h
19070 for an overview of struct it. Value is non-zero if
19071 IT->glyph_row displays text, as opposed to a line displaying ZV
19072 only. */
19073
19074 static int
19075 display_line (struct it *it)
19076 {
19077 struct glyph_row *row = it->glyph_row;
19078 Lisp_Object overlay_arrow_string;
19079 struct it wrap_it;
19080 void *wrap_data = NULL;
19081 int may_wrap = 0, wrap_x IF_LINT (= 0);
19082 int wrap_row_used = -1;
19083 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19084 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19085 int wrap_row_extra_line_spacing IF_LINT (= 0);
19086 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19087 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19088 int cvpos;
19089 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19090 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19091
19092 /* We always start displaying at hpos zero even if hscrolled. */
19093 xassert (it->hpos == 0 && it->current_x == 0);
19094
19095 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19096 >= it->w->desired_matrix->nrows)
19097 {
19098 it->w->nrows_scale_factor++;
19099 fonts_changed_p = 1;
19100 return 0;
19101 }
19102
19103 /* Is IT->w showing the region? */
19104 it->w->region_showing = it->region_beg_charpos > 0 ? Qt : Qnil;
19105
19106 /* Clear the result glyph row and enable it. */
19107 prepare_desired_row (row);
19108
19109 row->y = it->current_y;
19110 row->start = it->start;
19111 row->continuation_lines_width = it->continuation_lines_width;
19112 row->displays_text_p = 1;
19113 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19114 it->starts_in_middle_of_char_p = 0;
19115
19116 /* Arrange the overlays nicely for our purposes. Usually, we call
19117 display_line on only one line at a time, in which case this
19118 can't really hurt too much, or we call it on lines which appear
19119 one after another in the buffer, in which case all calls to
19120 recenter_overlay_lists but the first will be pretty cheap. */
19121 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19122
19123 /* Move over display elements that are not visible because we are
19124 hscrolled. This may stop at an x-position < IT->first_visible_x
19125 if the first glyph is partially visible or if we hit a line end. */
19126 if (it->current_x < it->first_visible_x)
19127 {
19128 this_line_min_pos = row->start.pos;
19129 move_it_in_display_line_to (it, ZV, it->first_visible_x,
19130 MOVE_TO_POS | MOVE_TO_X);
19131 /* Record the smallest positions seen while we moved over
19132 display elements that are not visible. This is needed by
19133 redisplay_internal for optimizing the case where the cursor
19134 stays inside the same line. The rest of this function only
19135 considers positions that are actually displayed, so
19136 RECORD_MAX_MIN_POS will not otherwise record positions that
19137 are hscrolled to the left of the left edge of the window. */
19138 min_pos = CHARPOS (this_line_min_pos);
19139 min_bpos = BYTEPOS (this_line_min_pos);
19140 }
19141 else
19142 {
19143 /* We only do this when not calling `move_it_in_display_line_to'
19144 above, because move_it_in_display_line_to calls
19145 handle_line_prefix itself. */
19146 handle_line_prefix (it);
19147 }
19148
19149 /* Get the initial row height. This is either the height of the
19150 text hscrolled, if there is any, or zero. */
19151 row->ascent = it->max_ascent;
19152 row->height = it->max_ascent + it->max_descent;
19153 row->phys_ascent = it->max_phys_ascent;
19154 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19155 row->extra_line_spacing = it->max_extra_line_spacing;
19156
19157 /* Utility macro to record max and min buffer positions seen until now. */
19158 #define RECORD_MAX_MIN_POS(IT) \
19159 do \
19160 { \
19161 int composition_p = !STRINGP ((IT)->string) \
19162 && ((IT)->what == IT_COMPOSITION); \
19163 ptrdiff_t current_pos = \
19164 composition_p ? (IT)->cmp_it.charpos \
19165 : IT_CHARPOS (*(IT)); \
19166 ptrdiff_t current_bpos = \
19167 composition_p ? CHAR_TO_BYTE (current_pos) \
19168 : IT_BYTEPOS (*(IT)); \
19169 if (current_pos < min_pos) \
19170 { \
19171 min_pos = current_pos; \
19172 min_bpos = current_bpos; \
19173 } \
19174 if (IT_CHARPOS (*it) > max_pos) \
19175 { \
19176 max_pos = IT_CHARPOS (*it); \
19177 max_bpos = IT_BYTEPOS (*it); \
19178 } \
19179 } \
19180 while (0)
19181
19182 /* Loop generating characters. The loop is left with IT on the next
19183 character to display. */
19184 while (1)
19185 {
19186 int n_glyphs_before, hpos_before, x_before;
19187 int x, nglyphs;
19188 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19189
19190 /* Retrieve the next thing to display. Value is zero if end of
19191 buffer reached. */
19192 if (!get_next_display_element (it))
19193 {
19194 /* Maybe add a space at the end of this line that is used to
19195 display the cursor there under X. Set the charpos of the
19196 first glyph of blank lines not corresponding to any text
19197 to -1. */
19198 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19199 row->exact_window_width_line_p = 1;
19200 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19201 || row->used[TEXT_AREA] == 0)
19202 {
19203 row->glyphs[TEXT_AREA]->charpos = -1;
19204 row->displays_text_p = 0;
19205
19206 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19207 && (!MINI_WINDOW_P (it->w)
19208 || (minibuf_level && EQ (it->window, minibuf_window))))
19209 row->indicate_empty_line_p = 1;
19210 }
19211
19212 it->continuation_lines_width = 0;
19213 row->ends_at_zv_p = 1;
19214 /* A row that displays right-to-left text must always have
19215 its last face extended all the way to the end of line,
19216 even if this row ends in ZV, because we still write to
19217 the screen left to right. We also need to extend the
19218 last face if the default face is remapped to some
19219 different face, otherwise the functions that clear
19220 portions of the screen will clear with the default face's
19221 background color. */
19222 if (row->reversed_p
19223 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19224 extend_face_to_end_of_line (it);
19225 break;
19226 }
19227
19228 /* Now, get the metrics of what we want to display. This also
19229 generates glyphs in `row' (which is IT->glyph_row). */
19230 n_glyphs_before = row->used[TEXT_AREA];
19231 x = it->current_x;
19232
19233 /* Remember the line height so far in case the next element doesn't
19234 fit on the line. */
19235 if (it->line_wrap != TRUNCATE)
19236 {
19237 ascent = it->max_ascent;
19238 descent = it->max_descent;
19239 phys_ascent = it->max_phys_ascent;
19240 phys_descent = it->max_phys_descent;
19241
19242 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19243 {
19244 if (IT_DISPLAYING_WHITESPACE (it))
19245 may_wrap = 1;
19246 else if (may_wrap)
19247 {
19248 SAVE_IT (wrap_it, *it, wrap_data);
19249 wrap_x = x;
19250 wrap_row_used = row->used[TEXT_AREA];
19251 wrap_row_ascent = row->ascent;
19252 wrap_row_height = row->height;
19253 wrap_row_phys_ascent = row->phys_ascent;
19254 wrap_row_phys_height = row->phys_height;
19255 wrap_row_extra_line_spacing = row->extra_line_spacing;
19256 wrap_row_min_pos = min_pos;
19257 wrap_row_min_bpos = min_bpos;
19258 wrap_row_max_pos = max_pos;
19259 wrap_row_max_bpos = max_bpos;
19260 may_wrap = 0;
19261 }
19262 }
19263 }
19264
19265 PRODUCE_GLYPHS (it);
19266
19267 /* If this display element was in marginal areas, continue with
19268 the next one. */
19269 if (it->area != TEXT_AREA)
19270 {
19271 row->ascent = max (row->ascent, it->max_ascent);
19272 row->height = max (row->height, it->max_ascent + it->max_descent);
19273 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19274 row->phys_height = max (row->phys_height,
19275 it->max_phys_ascent + it->max_phys_descent);
19276 row->extra_line_spacing = max (row->extra_line_spacing,
19277 it->max_extra_line_spacing);
19278 set_iterator_to_next (it, 1);
19279 continue;
19280 }
19281
19282 /* Does the display element fit on the line? If we truncate
19283 lines, we should draw past the right edge of the window. If
19284 we don't truncate, we want to stop so that we can display the
19285 continuation glyph before the right margin. If lines are
19286 continued, there are two possible strategies for characters
19287 resulting in more than 1 glyph (e.g. tabs): Display as many
19288 glyphs as possible in this line and leave the rest for the
19289 continuation line, or display the whole element in the next
19290 line. Original redisplay did the former, so we do it also. */
19291 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19292 hpos_before = it->hpos;
19293 x_before = x;
19294
19295 if (/* Not a newline. */
19296 nglyphs > 0
19297 /* Glyphs produced fit entirely in the line. */
19298 && it->current_x < it->last_visible_x)
19299 {
19300 it->hpos += nglyphs;
19301 row->ascent = max (row->ascent, it->max_ascent);
19302 row->height = max (row->height, it->max_ascent + it->max_descent);
19303 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19304 row->phys_height = max (row->phys_height,
19305 it->max_phys_ascent + it->max_phys_descent);
19306 row->extra_line_spacing = max (row->extra_line_spacing,
19307 it->max_extra_line_spacing);
19308 if (it->current_x - it->pixel_width < it->first_visible_x)
19309 row->x = x - it->first_visible_x;
19310 /* Record the maximum and minimum buffer positions seen so
19311 far in glyphs that will be displayed by this row. */
19312 if (it->bidi_p)
19313 RECORD_MAX_MIN_POS (it);
19314 }
19315 else
19316 {
19317 int i, new_x;
19318 struct glyph *glyph;
19319
19320 for (i = 0; i < nglyphs; ++i, x = new_x)
19321 {
19322 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19323 new_x = x + glyph->pixel_width;
19324
19325 if (/* Lines are continued. */
19326 it->line_wrap != TRUNCATE
19327 && (/* Glyph doesn't fit on the line. */
19328 new_x > it->last_visible_x
19329 /* Or it fits exactly on a window system frame. */
19330 || (new_x == it->last_visible_x
19331 && FRAME_WINDOW_P (it->f))))
19332 {
19333 /* End of a continued line. */
19334
19335 if (it->hpos == 0
19336 || (new_x == it->last_visible_x
19337 && FRAME_WINDOW_P (it->f)))
19338 {
19339 /* Current glyph is the only one on the line or
19340 fits exactly on the line. We must continue
19341 the line because we can't draw the cursor
19342 after the glyph. */
19343 row->continued_p = 1;
19344 it->current_x = new_x;
19345 it->continuation_lines_width += new_x;
19346 ++it->hpos;
19347 if (i == nglyphs - 1)
19348 {
19349 /* If line-wrap is on, check if a previous
19350 wrap point was found. */
19351 if (wrap_row_used > 0
19352 /* Even if there is a previous wrap
19353 point, continue the line here as
19354 usual, if (i) the previous character
19355 was a space or tab AND (ii) the
19356 current character is not. */
19357 && (!may_wrap
19358 || IT_DISPLAYING_WHITESPACE (it)))
19359 goto back_to_wrap;
19360
19361 /* Record the maximum and minimum buffer
19362 positions seen so far in glyphs that will be
19363 displayed by this row. */
19364 if (it->bidi_p)
19365 RECORD_MAX_MIN_POS (it);
19366 set_iterator_to_next (it, 1);
19367 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19368 {
19369 if (!get_next_display_element (it))
19370 {
19371 row->exact_window_width_line_p = 1;
19372 it->continuation_lines_width = 0;
19373 row->continued_p = 0;
19374 row->ends_at_zv_p = 1;
19375 }
19376 else if (ITERATOR_AT_END_OF_LINE_P (it))
19377 {
19378 row->continued_p = 0;
19379 row->exact_window_width_line_p = 1;
19380 }
19381 }
19382 }
19383 else if (it->bidi_p)
19384 RECORD_MAX_MIN_POS (it);
19385 }
19386 else if (CHAR_GLYPH_PADDING_P (*glyph)
19387 && !FRAME_WINDOW_P (it->f))
19388 {
19389 /* A padding glyph that doesn't fit on this line.
19390 This means the whole character doesn't fit
19391 on the line. */
19392 if (row->reversed_p)
19393 unproduce_glyphs (it, row->used[TEXT_AREA]
19394 - n_glyphs_before);
19395 row->used[TEXT_AREA] = n_glyphs_before;
19396
19397 /* Fill the rest of the row with continuation
19398 glyphs like in 20.x. */
19399 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19400 < row->glyphs[1 + TEXT_AREA])
19401 produce_special_glyphs (it, IT_CONTINUATION);
19402
19403 row->continued_p = 1;
19404 it->current_x = x_before;
19405 it->continuation_lines_width += x_before;
19406
19407 /* Restore the height to what it was before the
19408 element not fitting on the line. */
19409 it->max_ascent = ascent;
19410 it->max_descent = descent;
19411 it->max_phys_ascent = phys_ascent;
19412 it->max_phys_descent = phys_descent;
19413 }
19414 else if (wrap_row_used > 0)
19415 {
19416 back_to_wrap:
19417 if (row->reversed_p)
19418 unproduce_glyphs (it,
19419 row->used[TEXT_AREA] - wrap_row_used);
19420 RESTORE_IT (it, &wrap_it, wrap_data);
19421 it->continuation_lines_width += wrap_x;
19422 row->used[TEXT_AREA] = wrap_row_used;
19423 row->ascent = wrap_row_ascent;
19424 row->height = wrap_row_height;
19425 row->phys_ascent = wrap_row_phys_ascent;
19426 row->phys_height = wrap_row_phys_height;
19427 row->extra_line_spacing = wrap_row_extra_line_spacing;
19428 min_pos = wrap_row_min_pos;
19429 min_bpos = wrap_row_min_bpos;
19430 max_pos = wrap_row_max_pos;
19431 max_bpos = wrap_row_max_bpos;
19432 row->continued_p = 1;
19433 row->ends_at_zv_p = 0;
19434 row->exact_window_width_line_p = 0;
19435 it->continuation_lines_width += x;
19436
19437 /* Make sure that a non-default face is extended
19438 up to the right margin of the window. */
19439 extend_face_to_end_of_line (it);
19440 }
19441 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19442 {
19443 /* A TAB that extends past the right edge of the
19444 window. This produces a single glyph on
19445 window system frames. We leave the glyph in
19446 this row and let it fill the row, but don't
19447 consume the TAB. */
19448 it->continuation_lines_width += it->last_visible_x;
19449 row->ends_in_middle_of_char_p = 1;
19450 row->continued_p = 1;
19451 glyph->pixel_width = it->last_visible_x - x;
19452 it->starts_in_middle_of_char_p = 1;
19453 }
19454 else
19455 {
19456 /* Something other than a TAB that draws past
19457 the right edge of the window. Restore
19458 positions to values before the element. */
19459 if (row->reversed_p)
19460 unproduce_glyphs (it, row->used[TEXT_AREA]
19461 - (n_glyphs_before + i));
19462 row->used[TEXT_AREA] = n_glyphs_before + i;
19463
19464 /* Display continuation glyphs. */
19465 if (!FRAME_WINDOW_P (it->f))
19466 produce_special_glyphs (it, IT_CONTINUATION);
19467 row->continued_p = 1;
19468
19469 it->current_x = x_before;
19470 it->continuation_lines_width += x;
19471 extend_face_to_end_of_line (it);
19472
19473 if (nglyphs > 1 && i > 0)
19474 {
19475 row->ends_in_middle_of_char_p = 1;
19476 it->starts_in_middle_of_char_p = 1;
19477 }
19478
19479 /* Restore the height to what it was before the
19480 element not fitting on the line. */
19481 it->max_ascent = ascent;
19482 it->max_descent = descent;
19483 it->max_phys_ascent = phys_ascent;
19484 it->max_phys_descent = phys_descent;
19485 }
19486
19487 break;
19488 }
19489 else if (new_x > it->first_visible_x)
19490 {
19491 /* Increment number of glyphs actually displayed. */
19492 ++it->hpos;
19493
19494 /* Record the maximum and minimum buffer positions
19495 seen so far in glyphs that will be displayed by
19496 this row. */
19497 if (it->bidi_p)
19498 RECORD_MAX_MIN_POS (it);
19499
19500 if (x < it->first_visible_x)
19501 /* Glyph is partially visible, i.e. row starts at
19502 negative X position. */
19503 row->x = x - it->first_visible_x;
19504 }
19505 else
19506 {
19507 /* Glyph is completely off the left margin of the
19508 window. This should not happen because of the
19509 move_it_in_display_line at the start of this
19510 function, unless the text display area of the
19511 window is empty. */
19512 xassert (it->first_visible_x <= it->last_visible_x);
19513 }
19514 }
19515 /* Even if this display element produced no glyphs at all,
19516 we want to record its position. */
19517 if (it->bidi_p && nglyphs == 0)
19518 RECORD_MAX_MIN_POS (it);
19519
19520 row->ascent = max (row->ascent, it->max_ascent);
19521 row->height = max (row->height, it->max_ascent + it->max_descent);
19522 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19523 row->phys_height = max (row->phys_height,
19524 it->max_phys_ascent + it->max_phys_descent);
19525 row->extra_line_spacing = max (row->extra_line_spacing,
19526 it->max_extra_line_spacing);
19527
19528 /* End of this display line if row is continued. */
19529 if (row->continued_p || row->ends_at_zv_p)
19530 break;
19531 }
19532
19533 at_end_of_line:
19534 /* Is this a line end? If yes, we're also done, after making
19535 sure that a non-default face is extended up to the right
19536 margin of the window. */
19537 if (ITERATOR_AT_END_OF_LINE_P (it))
19538 {
19539 int used_before = row->used[TEXT_AREA];
19540
19541 row->ends_in_newline_from_string_p = STRINGP (it->object);
19542
19543 /* Add a space at the end of the line that is used to
19544 display the cursor there. */
19545 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19546 append_space_for_newline (it, 0);
19547
19548 /* Extend the face to the end of the line. */
19549 extend_face_to_end_of_line (it);
19550
19551 /* Make sure we have the position. */
19552 if (used_before == 0)
19553 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19554
19555 /* Record the position of the newline, for use in
19556 find_row_edges. */
19557 it->eol_pos = it->current.pos;
19558
19559 /* Consume the line end. This skips over invisible lines. */
19560 set_iterator_to_next (it, 1);
19561 it->continuation_lines_width = 0;
19562 break;
19563 }
19564
19565 /* Proceed with next display element. Note that this skips
19566 over lines invisible because of selective display. */
19567 set_iterator_to_next (it, 1);
19568
19569 /* If we truncate lines, we are done when the last displayed
19570 glyphs reach past the right margin of the window. */
19571 if (it->line_wrap == TRUNCATE
19572 && (FRAME_WINDOW_P (it->f)
19573 ? (it->current_x >= it->last_visible_x)
19574 : (it->current_x > it->last_visible_x)))
19575 {
19576 /* Maybe add truncation glyphs. */
19577 if (!FRAME_WINDOW_P (it->f))
19578 {
19579 int i, n;
19580
19581 if (!row->reversed_p)
19582 {
19583 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19584 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19585 break;
19586 }
19587 else
19588 {
19589 for (i = 0; i < row->used[TEXT_AREA]; i++)
19590 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19591 break;
19592 /* Remove any padding glyphs at the front of ROW, to
19593 make room for the truncation glyphs we will be
19594 adding below. The loop below always inserts at
19595 least one truncation glyph, so also remove the
19596 last glyph added to ROW. */
19597 unproduce_glyphs (it, i + 1);
19598 /* Adjust i for the loop below. */
19599 i = row->used[TEXT_AREA] - (i + 1);
19600 }
19601
19602 for (n = row->used[TEXT_AREA]; i < n; ++i)
19603 {
19604 row->used[TEXT_AREA] = i;
19605 produce_special_glyphs (it, IT_TRUNCATION);
19606 }
19607 }
19608 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19609 {
19610 /* Don't truncate if we can overflow newline into fringe. */
19611 if (!get_next_display_element (it))
19612 {
19613 it->continuation_lines_width = 0;
19614 row->ends_at_zv_p = 1;
19615 row->exact_window_width_line_p = 1;
19616 break;
19617 }
19618 if (ITERATOR_AT_END_OF_LINE_P (it))
19619 {
19620 row->exact_window_width_line_p = 1;
19621 goto at_end_of_line;
19622 }
19623 }
19624
19625 row->truncated_on_right_p = 1;
19626 it->continuation_lines_width = 0;
19627 reseat_at_next_visible_line_start (it, 0);
19628 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19629 it->hpos = hpos_before;
19630 it->current_x = x_before;
19631 break;
19632 }
19633 }
19634
19635 if (wrap_data)
19636 bidi_unshelve_cache (wrap_data, 1);
19637
19638 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19639 at the left window margin. */
19640 if (it->first_visible_x
19641 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19642 {
19643 if (!FRAME_WINDOW_P (it->f))
19644 insert_left_trunc_glyphs (it);
19645 row->truncated_on_left_p = 1;
19646 }
19647
19648 /* Remember the position at which this line ends.
19649
19650 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19651 cannot be before the call to find_row_edges below, since that is
19652 where these positions are determined. */
19653 row->end = it->current;
19654 if (!it->bidi_p)
19655 {
19656 row->minpos = row->start.pos;
19657 row->maxpos = row->end.pos;
19658 }
19659 else
19660 {
19661 /* ROW->minpos and ROW->maxpos must be the smallest and
19662 `1 + the largest' buffer positions in ROW. But if ROW was
19663 bidi-reordered, these two positions can be anywhere in the
19664 row, so we must determine them now. */
19665 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19666 }
19667
19668 /* If the start of this line is the overlay arrow-position, then
19669 mark this glyph row as the one containing the overlay arrow.
19670 This is clearly a mess with variable size fonts. It would be
19671 better to let it be displayed like cursors under X. */
19672 if ((row->displays_text_p || !overlay_arrow_seen)
19673 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19674 !NILP (overlay_arrow_string)))
19675 {
19676 /* Overlay arrow in window redisplay is a fringe bitmap. */
19677 if (STRINGP (overlay_arrow_string))
19678 {
19679 struct glyph_row *arrow_row
19680 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19681 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19682 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19683 struct glyph *p = row->glyphs[TEXT_AREA];
19684 struct glyph *p2, *end;
19685
19686 /* Copy the arrow glyphs. */
19687 while (glyph < arrow_end)
19688 *p++ = *glyph++;
19689
19690 /* Throw away padding glyphs. */
19691 p2 = p;
19692 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19693 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19694 ++p2;
19695 if (p2 > p)
19696 {
19697 while (p2 < end)
19698 *p++ = *p2++;
19699 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19700 }
19701 }
19702 else
19703 {
19704 xassert (INTEGERP (overlay_arrow_string));
19705 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19706 }
19707 overlay_arrow_seen = 1;
19708 }
19709
19710 /* Highlight trailing whitespace. */
19711 if (!NILP (Vshow_trailing_whitespace))
19712 highlight_trailing_whitespace (it->f, it->glyph_row);
19713
19714 /* Compute pixel dimensions of this line. */
19715 compute_line_metrics (it);
19716
19717 /* Implementation note: No changes in the glyphs of ROW or in their
19718 faces can be done past this point, because compute_line_metrics
19719 computes ROW's hash value and stores it within the glyph_row
19720 structure. */
19721
19722 /* Record whether this row ends inside an ellipsis. */
19723 row->ends_in_ellipsis_p
19724 = (it->method == GET_FROM_DISPLAY_VECTOR
19725 && it->ellipsis_p);
19726
19727 /* Save fringe bitmaps in this row. */
19728 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19729 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19730 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19731 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19732
19733 it->left_user_fringe_bitmap = 0;
19734 it->left_user_fringe_face_id = 0;
19735 it->right_user_fringe_bitmap = 0;
19736 it->right_user_fringe_face_id = 0;
19737
19738 /* Maybe set the cursor. */
19739 cvpos = it->w->cursor.vpos;
19740 if ((cvpos < 0
19741 /* In bidi-reordered rows, keep checking for proper cursor
19742 position even if one has been found already, because buffer
19743 positions in such rows change non-linearly with ROW->VPOS,
19744 when a line is continued. One exception: when we are at ZV,
19745 display cursor on the first suitable glyph row, since all
19746 the empty rows after that also have their position set to ZV. */
19747 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19748 lines' rows is implemented for bidi-reordered rows. */
19749 || (it->bidi_p
19750 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19751 && PT >= MATRIX_ROW_START_CHARPOS (row)
19752 && PT <= MATRIX_ROW_END_CHARPOS (row)
19753 && cursor_row_p (row))
19754 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19755
19756 /* Prepare for the next line. This line starts horizontally at (X
19757 HPOS) = (0 0). Vertical positions are incremented. As a
19758 convenience for the caller, IT->glyph_row is set to the next
19759 row to be used. */
19760 it->current_x = it->hpos = 0;
19761 it->current_y += row->height;
19762 SET_TEXT_POS (it->eol_pos, 0, 0);
19763 ++it->vpos;
19764 ++it->glyph_row;
19765 /* The next row should by default use the same value of the
19766 reversed_p flag as this one. set_iterator_to_next decides when
19767 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19768 the flag accordingly. */
19769 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19770 it->glyph_row->reversed_p = row->reversed_p;
19771 it->start = row->end;
19772 return row->displays_text_p;
19773
19774 #undef RECORD_MAX_MIN_POS
19775 }
19776
19777 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19778 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19779 doc: /* Return paragraph direction at point in BUFFER.
19780 Value is either `left-to-right' or `right-to-left'.
19781 If BUFFER is omitted or nil, it defaults to the current buffer.
19782
19783 Paragraph direction determines how the text in the paragraph is displayed.
19784 In left-to-right paragraphs, text begins at the left margin of the window
19785 and the reading direction is generally left to right. In right-to-left
19786 paragraphs, text begins at the right margin and is read from right to left.
19787
19788 See also `bidi-paragraph-direction'. */)
19789 (Lisp_Object buffer)
19790 {
19791 struct buffer *buf = current_buffer;
19792 struct buffer *old = buf;
19793
19794 if (! NILP (buffer))
19795 {
19796 CHECK_BUFFER (buffer);
19797 buf = XBUFFER (buffer);
19798 }
19799
19800 if (NILP (BVAR (buf, bidi_display_reordering))
19801 || NILP (BVAR (buf, enable_multibyte_characters))
19802 /* When we are loading loadup.el, the character property tables
19803 needed for bidi iteration are not yet available. */
19804 || !NILP (Vpurify_flag))
19805 return Qleft_to_right;
19806 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19807 return BVAR (buf, bidi_paragraph_direction);
19808 else
19809 {
19810 /* Determine the direction from buffer text. We could try to
19811 use current_matrix if it is up to date, but this seems fast
19812 enough as it is. */
19813 struct bidi_it itb;
19814 ptrdiff_t pos = BUF_PT (buf);
19815 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19816 int c;
19817 void *itb_data = bidi_shelve_cache ();
19818
19819 set_buffer_temp (buf);
19820 /* bidi_paragraph_init finds the base direction of the paragraph
19821 by searching forward from paragraph start. We need the base
19822 direction of the current or _previous_ paragraph, so we need
19823 to make sure we are within that paragraph. To that end, find
19824 the previous non-empty line. */
19825 if (pos >= ZV && pos > BEGV)
19826 {
19827 pos--;
19828 bytepos = CHAR_TO_BYTE (pos);
19829 }
19830 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19831 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19832 {
19833 while ((c = FETCH_BYTE (bytepos)) == '\n'
19834 || c == ' ' || c == '\t' || c == '\f')
19835 {
19836 if (bytepos <= BEGV_BYTE)
19837 break;
19838 bytepos--;
19839 pos--;
19840 }
19841 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19842 bytepos--;
19843 }
19844 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19845 itb.paragraph_dir = NEUTRAL_DIR;
19846 itb.string.s = NULL;
19847 itb.string.lstring = Qnil;
19848 itb.string.bufpos = 0;
19849 itb.string.unibyte = 0;
19850 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19851 bidi_unshelve_cache (itb_data, 0);
19852 set_buffer_temp (old);
19853 switch (itb.paragraph_dir)
19854 {
19855 case L2R:
19856 return Qleft_to_right;
19857 break;
19858 case R2L:
19859 return Qright_to_left;
19860 break;
19861 default:
19862 abort ();
19863 }
19864 }
19865 }
19866
19867
19868 \f
19869 /***********************************************************************
19870 Menu Bar
19871 ***********************************************************************/
19872
19873 /* Redisplay the menu bar in the frame for window W.
19874
19875 The menu bar of X frames that don't have X toolkit support is
19876 displayed in a special window W->frame->menu_bar_window.
19877
19878 The menu bar of terminal frames is treated specially as far as
19879 glyph matrices are concerned. Menu bar lines are not part of
19880 windows, so the update is done directly on the frame matrix rows
19881 for the menu bar. */
19882
19883 static void
19884 display_menu_bar (struct window *w)
19885 {
19886 struct frame *f = XFRAME (WINDOW_FRAME (w));
19887 struct it it;
19888 Lisp_Object items;
19889 int i;
19890
19891 /* Don't do all this for graphical frames. */
19892 #ifdef HAVE_NTGUI
19893 if (FRAME_W32_P (f))
19894 return;
19895 #endif
19896 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19897 if (FRAME_X_P (f))
19898 return;
19899 #endif
19900
19901 #ifdef HAVE_NS
19902 if (FRAME_NS_P (f))
19903 return;
19904 #endif /* HAVE_NS */
19905
19906 #ifdef USE_X_TOOLKIT
19907 xassert (!FRAME_WINDOW_P (f));
19908 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19909 it.first_visible_x = 0;
19910 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19911 #else /* not USE_X_TOOLKIT */
19912 if (FRAME_WINDOW_P (f))
19913 {
19914 /* Menu bar lines are displayed in the desired matrix of the
19915 dummy window menu_bar_window. */
19916 struct window *menu_w;
19917 xassert (WINDOWP (f->menu_bar_window));
19918 menu_w = XWINDOW (f->menu_bar_window);
19919 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
19920 MENU_FACE_ID);
19921 it.first_visible_x = 0;
19922 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19923 }
19924 else
19925 {
19926 /* This is a TTY frame, i.e. character hpos/vpos are used as
19927 pixel x/y. */
19928 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
19929 MENU_FACE_ID);
19930 it.first_visible_x = 0;
19931 it.last_visible_x = FRAME_COLS (f);
19932 }
19933 #endif /* not USE_X_TOOLKIT */
19934
19935 /* FIXME: This should be controlled by a user option. See the
19936 comments in redisplay_tool_bar and display_mode_line about
19937 this. */
19938 it.paragraph_embedding = L2R;
19939
19940 if (! mode_line_inverse_video)
19941 /* Force the menu-bar to be displayed in the default face. */
19942 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
19943
19944 /* Clear all rows of the menu bar. */
19945 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
19946 {
19947 struct glyph_row *row = it.glyph_row + i;
19948 clear_glyph_row (row);
19949 row->enabled_p = 1;
19950 row->full_width_p = 1;
19951 }
19952
19953 /* Display all items of the menu bar. */
19954 items = FRAME_MENU_BAR_ITEMS (it.f);
19955 for (i = 0; i < ASIZE (items); i += 4)
19956 {
19957 Lisp_Object string;
19958
19959 /* Stop at nil string. */
19960 string = AREF (items, i + 1);
19961 if (NILP (string))
19962 break;
19963
19964 /* Remember where item was displayed. */
19965 ASET (items, i + 3, make_number (it.hpos));
19966
19967 /* Display the item, pad with one space. */
19968 if (it.current_x < it.last_visible_x)
19969 display_string (NULL, string, Qnil, 0, 0, &it,
19970 SCHARS (string) + 1, 0, 0, -1);
19971 }
19972
19973 /* Fill out the line with spaces. */
19974 if (it.current_x < it.last_visible_x)
19975 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
19976
19977 /* Compute the total height of the lines. */
19978 compute_line_metrics (&it);
19979 }
19980
19981
19982 \f
19983 /***********************************************************************
19984 Mode Line
19985 ***********************************************************************/
19986
19987 /* Redisplay mode lines in the window tree whose root is WINDOW. If
19988 FORCE is non-zero, redisplay mode lines unconditionally.
19989 Otherwise, redisplay only mode lines that are garbaged. Value is
19990 the number of windows whose mode lines were redisplayed. */
19991
19992 static int
19993 redisplay_mode_lines (Lisp_Object window, int force)
19994 {
19995 int nwindows = 0;
19996
19997 while (!NILP (window))
19998 {
19999 struct window *w = XWINDOW (window);
20000
20001 if (WINDOWP (w->hchild))
20002 nwindows += redisplay_mode_lines (w->hchild, force);
20003 else if (WINDOWP (w->vchild))
20004 nwindows += redisplay_mode_lines (w->vchild, force);
20005 else if (force
20006 || FRAME_GARBAGED_P (XFRAME (w->frame))
20007 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20008 {
20009 struct text_pos lpoint;
20010 struct buffer *old = current_buffer;
20011
20012 /* Set the window's buffer for the mode line display. */
20013 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20014 set_buffer_internal_1 (XBUFFER (w->buffer));
20015
20016 /* Point refers normally to the selected window. For any
20017 other window, set up appropriate value. */
20018 if (!EQ (window, selected_window))
20019 {
20020 struct text_pos pt;
20021
20022 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20023 if (CHARPOS (pt) < BEGV)
20024 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20025 else if (CHARPOS (pt) > (ZV - 1))
20026 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20027 else
20028 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20029 }
20030
20031 /* Display mode lines. */
20032 clear_glyph_matrix (w->desired_matrix);
20033 if (display_mode_lines (w))
20034 {
20035 ++nwindows;
20036 w->must_be_updated_p = 1;
20037 }
20038
20039 /* Restore old settings. */
20040 set_buffer_internal_1 (old);
20041 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20042 }
20043
20044 window = w->next;
20045 }
20046
20047 return nwindows;
20048 }
20049
20050
20051 /* Display the mode and/or header line of window W. Value is the
20052 sum number of mode lines and header lines displayed. */
20053
20054 static int
20055 display_mode_lines (struct window *w)
20056 {
20057 Lisp_Object old_selected_window, old_selected_frame;
20058 int n = 0;
20059
20060 old_selected_frame = selected_frame;
20061 selected_frame = w->frame;
20062 old_selected_window = selected_window;
20063 XSETWINDOW (selected_window, w);
20064
20065 /* These will be set while the mode line specs are processed. */
20066 line_number_displayed = 0;
20067 w->column_number_displayed = Qnil;
20068
20069 if (WINDOW_WANTS_MODELINE_P (w))
20070 {
20071 struct window *sel_w = XWINDOW (old_selected_window);
20072
20073 /* Select mode line face based on the real selected window. */
20074 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20075 BVAR (current_buffer, mode_line_format));
20076 ++n;
20077 }
20078
20079 if (WINDOW_WANTS_HEADER_LINE_P (w))
20080 {
20081 display_mode_line (w, HEADER_LINE_FACE_ID,
20082 BVAR (current_buffer, header_line_format));
20083 ++n;
20084 }
20085
20086 selected_frame = old_selected_frame;
20087 selected_window = old_selected_window;
20088 return n;
20089 }
20090
20091
20092 /* Display mode or header line of window W. FACE_ID specifies which
20093 line to display; it is either MODE_LINE_FACE_ID or
20094 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20095 display. Value is the pixel height of the mode/header line
20096 displayed. */
20097
20098 static int
20099 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20100 {
20101 struct it it;
20102 struct face *face;
20103 ptrdiff_t count = SPECPDL_INDEX ();
20104
20105 init_iterator (&it, w, -1, -1, NULL, face_id);
20106 /* Don't extend on a previously drawn mode-line.
20107 This may happen if called from pos_visible_p. */
20108 it.glyph_row->enabled_p = 0;
20109 prepare_desired_row (it.glyph_row);
20110
20111 it.glyph_row->mode_line_p = 1;
20112
20113 if (! mode_line_inverse_video)
20114 /* Force the mode-line to be displayed in the default face. */
20115 it.base_face_id = it.face_id = DEFAULT_FACE_ID;
20116
20117 /* FIXME: This should be controlled by a user option. But
20118 supporting such an option is not trivial, since the mode line is
20119 made up of many separate strings. */
20120 it.paragraph_embedding = L2R;
20121
20122 record_unwind_protect (unwind_format_mode_line,
20123 format_mode_line_unwind_data (NULL, Qnil, 0));
20124
20125 mode_line_target = MODE_LINE_DISPLAY;
20126
20127 /* Temporarily make frame's keyboard the current kboard so that
20128 kboard-local variables in the mode_line_format will get the right
20129 values. */
20130 push_kboard (FRAME_KBOARD (it.f));
20131 record_unwind_save_match_data ();
20132 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20133 pop_kboard ();
20134
20135 unbind_to (count, Qnil);
20136
20137 /* Fill up with spaces. */
20138 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20139
20140 compute_line_metrics (&it);
20141 it.glyph_row->full_width_p = 1;
20142 it.glyph_row->continued_p = 0;
20143 it.glyph_row->truncated_on_left_p = 0;
20144 it.glyph_row->truncated_on_right_p = 0;
20145
20146 /* Make a 3D mode-line have a shadow at its right end. */
20147 face = FACE_FROM_ID (it.f, face_id);
20148 extend_face_to_end_of_line (&it);
20149 if (face->box != FACE_NO_BOX)
20150 {
20151 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20152 + it.glyph_row->used[TEXT_AREA] - 1);
20153 last->right_box_line_p = 1;
20154 }
20155
20156 return it.glyph_row->height;
20157 }
20158
20159 /* Move element ELT in LIST to the front of LIST.
20160 Return the updated list. */
20161
20162 static Lisp_Object
20163 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20164 {
20165 register Lisp_Object tail, prev;
20166 register Lisp_Object tem;
20167
20168 tail = list;
20169 prev = Qnil;
20170 while (CONSP (tail))
20171 {
20172 tem = XCAR (tail);
20173
20174 if (EQ (elt, tem))
20175 {
20176 /* Splice out the link TAIL. */
20177 if (NILP (prev))
20178 list = XCDR (tail);
20179 else
20180 Fsetcdr (prev, XCDR (tail));
20181
20182 /* Now make it the first. */
20183 Fsetcdr (tail, list);
20184 return tail;
20185 }
20186 else
20187 prev = tail;
20188 tail = XCDR (tail);
20189 QUIT;
20190 }
20191
20192 /* Not found--return unchanged LIST. */
20193 return list;
20194 }
20195
20196 /* Contribute ELT to the mode line for window IT->w. How it
20197 translates into text depends on its data type.
20198
20199 IT describes the display environment in which we display, as usual.
20200
20201 DEPTH is the depth in recursion. It is used to prevent
20202 infinite recursion here.
20203
20204 FIELD_WIDTH is the number of characters the display of ELT should
20205 occupy in the mode line, and PRECISION is the maximum number of
20206 characters to display from ELT's representation. See
20207 display_string for details.
20208
20209 Returns the hpos of the end of the text generated by ELT.
20210
20211 PROPS is a property list to add to any string we encounter.
20212
20213 If RISKY is nonzero, remove (disregard) any properties in any string
20214 we encounter, and ignore :eval and :propertize.
20215
20216 The global variable `mode_line_target' determines whether the
20217 output is passed to `store_mode_line_noprop',
20218 `store_mode_line_string', or `display_string'. */
20219
20220 static int
20221 display_mode_element (struct it *it, int depth, int field_width, int precision,
20222 Lisp_Object elt, Lisp_Object props, int risky)
20223 {
20224 int n = 0, field, prec;
20225 int literal = 0;
20226
20227 tail_recurse:
20228 if (depth > 100)
20229 elt = build_string ("*too-deep*");
20230
20231 depth++;
20232
20233 switch (SWITCH_ENUM_CAST (XTYPE (elt)))
20234 {
20235 case Lisp_String:
20236 {
20237 /* A string: output it and check for %-constructs within it. */
20238 unsigned char c;
20239 ptrdiff_t offset = 0;
20240
20241 if (SCHARS (elt) > 0
20242 && (!NILP (props) || risky))
20243 {
20244 Lisp_Object oprops, aelt;
20245 oprops = Ftext_properties_at (make_number (0), elt);
20246
20247 /* If the starting string's properties are not what
20248 we want, translate the string. Also, if the string
20249 is risky, do that anyway. */
20250
20251 if (NILP (Fequal (props, oprops)) || risky)
20252 {
20253 /* If the starting string has properties,
20254 merge the specified ones onto the existing ones. */
20255 if (! NILP (oprops) && !risky)
20256 {
20257 Lisp_Object tem;
20258
20259 oprops = Fcopy_sequence (oprops);
20260 tem = props;
20261 while (CONSP (tem))
20262 {
20263 oprops = Fplist_put (oprops, XCAR (tem),
20264 XCAR (XCDR (tem)));
20265 tem = XCDR (XCDR (tem));
20266 }
20267 props = oprops;
20268 }
20269
20270 aelt = Fassoc (elt, mode_line_proptrans_alist);
20271 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20272 {
20273 /* AELT is what we want. Move it to the front
20274 without consing. */
20275 elt = XCAR (aelt);
20276 mode_line_proptrans_alist
20277 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20278 }
20279 else
20280 {
20281 Lisp_Object tem;
20282
20283 /* If AELT has the wrong props, it is useless.
20284 so get rid of it. */
20285 if (! NILP (aelt))
20286 mode_line_proptrans_alist
20287 = Fdelq (aelt, mode_line_proptrans_alist);
20288
20289 elt = Fcopy_sequence (elt);
20290 Fset_text_properties (make_number (0), Flength (elt),
20291 props, elt);
20292 /* Add this item to mode_line_proptrans_alist. */
20293 mode_line_proptrans_alist
20294 = Fcons (Fcons (elt, props),
20295 mode_line_proptrans_alist);
20296 /* Truncate mode_line_proptrans_alist
20297 to at most 50 elements. */
20298 tem = Fnthcdr (make_number (50),
20299 mode_line_proptrans_alist);
20300 if (! NILP (tem))
20301 XSETCDR (tem, Qnil);
20302 }
20303 }
20304 }
20305
20306 offset = 0;
20307
20308 if (literal)
20309 {
20310 prec = precision - n;
20311 switch (mode_line_target)
20312 {
20313 case MODE_LINE_NOPROP:
20314 case MODE_LINE_TITLE:
20315 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20316 break;
20317 case MODE_LINE_STRING:
20318 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20319 break;
20320 case MODE_LINE_DISPLAY:
20321 n += display_string (NULL, elt, Qnil, 0, 0, it,
20322 0, prec, 0, STRING_MULTIBYTE (elt));
20323 break;
20324 }
20325
20326 break;
20327 }
20328
20329 /* Handle the non-literal case. */
20330
20331 while ((precision <= 0 || n < precision)
20332 && SREF (elt, offset) != 0
20333 && (mode_line_target != MODE_LINE_DISPLAY
20334 || it->current_x < it->last_visible_x))
20335 {
20336 ptrdiff_t last_offset = offset;
20337
20338 /* Advance to end of string or next format specifier. */
20339 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20340 ;
20341
20342 if (offset - 1 != last_offset)
20343 {
20344 ptrdiff_t nchars, nbytes;
20345
20346 /* Output to end of string or up to '%'. Field width
20347 is length of string. Don't output more than
20348 PRECISION allows us. */
20349 offset--;
20350
20351 prec = c_string_width (SDATA (elt) + last_offset,
20352 offset - last_offset, precision - n,
20353 &nchars, &nbytes);
20354
20355 switch (mode_line_target)
20356 {
20357 case MODE_LINE_NOPROP:
20358 case MODE_LINE_TITLE:
20359 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20360 break;
20361 case MODE_LINE_STRING:
20362 {
20363 ptrdiff_t bytepos = last_offset;
20364 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20365 ptrdiff_t endpos = (precision <= 0
20366 ? string_byte_to_char (elt, offset)
20367 : charpos + nchars);
20368
20369 n += store_mode_line_string (NULL,
20370 Fsubstring (elt, make_number (charpos),
20371 make_number (endpos)),
20372 0, 0, 0, Qnil);
20373 }
20374 break;
20375 case MODE_LINE_DISPLAY:
20376 {
20377 ptrdiff_t bytepos = last_offset;
20378 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20379
20380 if (precision <= 0)
20381 nchars = string_byte_to_char (elt, offset) - charpos;
20382 n += display_string (NULL, elt, Qnil, 0, charpos,
20383 it, 0, nchars, 0,
20384 STRING_MULTIBYTE (elt));
20385 }
20386 break;
20387 }
20388 }
20389 else /* c == '%' */
20390 {
20391 ptrdiff_t percent_position = offset;
20392
20393 /* Get the specified minimum width. Zero means
20394 don't pad. */
20395 field = 0;
20396 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20397 field = field * 10 + c - '0';
20398
20399 /* Don't pad beyond the total padding allowed. */
20400 if (field_width - n > 0 && field > field_width - n)
20401 field = field_width - n;
20402
20403 /* Note that either PRECISION <= 0 or N < PRECISION. */
20404 prec = precision - n;
20405
20406 if (c == 'M')
20407 n += display_mode_element (it, depth, field, prec,
20408 Vglobal_mode_string, props,
20409 risky);
20410 else if (c != 0)
20411 {
20412 int multibyte;
20413 ptrdiff_t bytepos, charpos;
20414 const char *spec;
20415 Lisp_Object string;
20416
20417 bytepos = percent_position;
20418 charpos = (STRING_MULTIBYTE (elt)
20419 ? string_byte_to_char (elt, bytepos)
20420 : bytepos);
20421 spec = decode_mode_spec (it->w, c, field, &string);
20422 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20423
20424 switch (mode_line_target)
20425 {
20426 case MODE_LINE_NOPROP:
20427 case MODE_LINE_TITLE:
20428 n += store_mode_line_noprop (spec, field, prec);
20429 break;
20430 case MODE_LINE_STRING:
20431 {
20432 Lisp_Object tem = build_string (spec);
20433 props = Ftext_properties_at (make_number (charpos), elt);
20434 /* Should only keep face property in props */
20435 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20436 }
20437 break;
20438 case MODE_LINE_DISPLAY:
20439 {
20440 int nglyphs_before, nwritten;
20441
20442 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20443 nwritten = display_string (spec, string, elt,
20444 charpos, 0, it,
20445 field, prec, 0,
20446 multibyte);
20447
20448 /* Assign to the glyphs written above the
20449 string where the `%x' came from, position
20450 of the `%'. */
20451 if (nwritten > 0)
20452 {
20453 struct glyph *glyph
20454 = (it->glyph_row->glyphs[TEXT_AREA]
20455 + nglyphs_before);
20456 int i;
20457
20458 for (i = 0; i < nwritten; ++i)
20459 {
20460 glyph[i].object = elt;
20461 glyph[i].charpos = charpos;
20462 }
20463
20464 n += nwritten;
20465 }
20466 }
20467 break;
20468 }
20469 }
20470 else /* c == 0 */
20471 break;
20472 }
20473 }
20474 }
20475 break;
20476
20477 case Lisp_Symbol:
20478 /* A symbol: process the value of the symbol recursively
20479 as if it appeared here directly. Avoid error if symbol void.
20480 Special case: if value of symbol is a string, output the string
20481 literally. */
20482 {
20483 register Lisp_Object tem;
20484
20485 /* If the variable is not marked as risky to set
20486 then its contents are risky to use. */
20487 if (NILP (Fget (elt, Qrisky_local_variable)))
20488 risky = 1;
20489
20490 tem = Fboundp (elt);
20491 if (!NILP (tem))
20492 {
20493 tem = Fsymbol_value (elt);
20494 /* If value is a string, output that string literally:
20495 don't check for % within it. */
20496 if (STRINGP (tem))
20497 literal = 1;
20498
20499 if (!EQ (tem, elt))
20500 {
20501 /* Give up right away for nil or t. */
20502 elt = tem;
20503 goto tail_recurse;
20504 }
20505 }
20506 }
20507 break;
20508
20509 case Lisp_Cons:
20510 {
20511 register Lisp_Object car, tem;
20512
20513 /* A cons cell: five distinct cases.
20514 If first element is :eval or :propertize, do something special.
20515 If first element is a string or a cons, process all the elements
20516 and effectively concatenate them.
20517 If first element is a negative number, truncate displaying cdr to
20518 at most that many characters. If positive, pad (with spaces)
20519 to at least that many characters.
20520 If first element is a symbol, process the cadr or caddr recursively
20521 according to whether the symbol's value is non-nil or nil. */
20522 car = XCAR (elt);
20523 if (EQ (car, QCeval))
20524 {
20525 /* An element of the form (:eval FORM) means evaluate FORM
20526 and use the result as mode line elements. */
20527
20528 if (risky)
20529 break;
20530
20531 if (CONSP (XCDR (elt)))
20532 {
20533 Lisp_Object spec;
20534 spec = safe_eval (XCAR (XCDR (elt)));
20535 n += display_mode_element (it, depth, field_width - n,
20536 precision - n, spec, props,
20537 risky);
20538 }
20539 }
20540 else if (EQ (car, QCpropertize))
20541 {
20542 /* An element of the form (:propertize ELT PROPS...)
20543 means display ELT but applying properties PROPS. */
20544
20545 if (risky)
20546 break;
20547
20548 if (CONSP (XCDR (elt)))
20549 n += display_mode_element (it, depth, field_width - n,
20550 precision - n, XCAR (XCDR (elt)),
20551 XCDR (XCDR (elt)), risky);
20552 }
20553 else if (SYMBOLP (car))
20554 {
20555 tem = Fboundp (car);
20556 elt = XCDR (elt);
20557 if (!CONSP (elt))
20558 goto invalid;
20559 /* elt is now the cdr, and we know it is a cons cell.
20560 Use its car if CAR has a non-nil value. */
20561 if (!NILP (tem))
20562 {
20563 tem = Fsymbol_value (car);
20564 if (!NILP (tem))
20565 {
20566 elt = XCAR (elt);
20567 goto tail_recurse;
20568 }
20569 }
20570 /* Symbol's value is nil (or symbol is unbound)
20571 Get the cddr of the original list
20572 and if possible find the caddr and use that. */
20573 elt = XCDR (elt);
20574 if (NILP (elt))
20575 break;
20576 else if (!CONSP (elt))
20577 goto invalid;
20578 elt = XCAR (elt);
20579 goto tail_recurse;
20580 }
20581 else if (INTEGERP (car))
20582 {
20583 register int lim = XINT (car);
20584 elt = XCDR (elt);
20585 if (lim < 0)
20586 {
20587 /* Negative int means reduce maximum width. */
20588 if (precision <= 0)
20589 precision = -lim;
20590 else
20591 precision = min (precision, -lim);
20592 }
20593 else if (lim > 0)
20594 {
20595 /* Padding specified. Don't let it be more than
20596 current maximum. */
20597 if (precision > 0)
20598 lim = min (precision, lim);
20599
20600 /* If that's more padding than already wanted, queue it.
20601 But don't reduce padding already specified even if
20602 that is beyond the current truncation point. */
20603 field_width = max (lim, field_width);
20604 }
20605 goto tail_recurse;
20606 }
20607 else if (STRINGP (car) || CONSP (car))
20608 {
20609 Lisp_Object halftail = elt;
20610 int len = 0;
20611
20612 while (CONSP (elt)
20613 && (precision <= 0 || n < precision))
20614 {
20615 n += display_mode_element (it, depth,
20616 /* Do padding only after the last
20617 element in the list. */
20618 (! CONSP (XCDR (elt))
20619 ? field_width - n
20620 : 0),
20621 precision - n, XCAR (elt),
20622 props, risky);
20623 elt = XCDR (elt);
20624 len++;
20625 if ((len & 1) == 0)
20626 halftail = XCDR (halftail);
20627 /* Check for cycle. */
20628 if (EQ (halftail, elt))
20629 break;
20630 }
20631 }
20632 }
20633 break;
20634
20635 default:
20636 invalid:
20637 elt = build_string ("*invalid*");
20638 goto tail_recurse;
20639 }
20640
20641 /* Pad to FIELD_WIDTH. */
20642 if (field_width > 0 && n < field_width)
20643 {
20644 switch (mode_line_target)
20645 {
20646 case MODE_LINE_NOPROP:
20647 case MODE_LINE_TITLE:
20648 n += store_mode_line_noprop ("", field_width - n, 0);
20649 break;
20650 case MODE_LINE_STRING:
20651 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20652 break;
20653 case MODE_LINE_DISPLAY:
20654 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20655 0, 0, 0);
20656 break;
20657 }
20658 }
20659
20660 return n;
20661 }
20662
20663 /* Store a mode-line string element in mode_line_string_list.
20664
20665 If STRING is non-null, display that C string. Otherwise, the Lisp
20666 string LISP_STRING is displayed.
20667
20668 FIELD_WIDTH is the minimum number of output glyphs to produce.
20669 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20670 with spaces. FIELD_WIDTH <= 0 means don't pad.
20671
20672 PRECISION is the maximum number of characters to output from
20673 STRING. PRECISION <= 0 means don't truncate the string.
20674
20675 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20676 properties to the string.
20677
20678 PROPS are the properties to add to the string.
20679 The mode_line_string_face face property is always added to the string.
20680 */
20681
20682 static int
20683 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20684 int field_width, int precision, Lisp_Object props)
20685 {
20686 ptrdiff_t len;
20687 int n = 0;
20688
20689 if (string != NULL)
20690 {
20691 len = strlen (string);
20692 if (precision > 0 && len > precision)
20693 len = precision;
20694 lisp_string = make_string (string, len);
20695 if (NILP (props))
20696 props = mode_line_string_face_prop;
20697 else if (!NILP (mode_line_string_face))
20698 {
20699 Lisp_Object face = Fplist_get (props, Qface);
20700 props = Fcopy_sequence (props);
20701 if (NILP (face))
20702 face = mode_line_string_face;
20703 else
20704 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20705 props = Fplist_put (props, Qface, face);
20706 }
20707 Fadd_text_properties (make_number (0), make_number (len),
20708 props, lisp_string);
20709 }
20710 else
20711 {
20712 len = XFASTINT (Flength (lisp_string));
20713 if (precision > 0 && len > precision)
20714 {
20715 len = precision;
20716 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20717 precision = -1;
20718 }
20719 if (!NILP (mode_line_string_face))
20720 {
20721 Lisp_Object face;
20722 if (NILP (props))
20723 props = Ftext_properties_at (make_number (0), lisp_string);
20724 face = Fplist_get (props, Qface);
20725 if (NILP (face))
20726 face = mode_line_string_face;
20727 else
20728 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20729 props = Fcons (Qface, Fcons (face, Qnil));
20730 if (copy_string)
20731 lisp_string = Fcopy_sequence (lisp_string);
20732 }
20733 if (!NILP (props))
20734 Fadd_text_properties (make_number (0), make_number (len),
20735 props, lisp_string);
20736 }
20737
20738 if (len > 0)
20739 {
20740 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20741 n += len;
20742 }
20743
20744 if (field_width > len)
20745 {
20746 field_width -= len;
20747 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20748 if (!NILP (props))
20749 Fadd_text_properties (make_number (0), make_number (field_width),
20750 props, lisp_string);
20751 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20752 n += field_width;
20753 }
20754
20755 return n;
20756 }
20757
20758
20759 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20760 1, 4, 0,
20761 doc: /* Format a string out of a mode line format specification.
20762 First arg FORMAT specifies the mode line format (see `mode-line-format'
20763 for details) to use.
20764
20765 By default, the format is evaluated for the currently selected window.
20766
20767 Optional second arg FACE specifies the face property to put on all
20768 characters for which no face is specified. The value nil means the
20769 default face. The value t means whatever face the window's mode line
20770 currently uses (either `mode-line' or `mode-line-inactive',
20771 depending on whether the window is the selected window or not).
20772 An integer value means the value string has no text
20773 properties.
20774
20775 Optional third and fourth args WINDOW and BUFFER specify the window
20776 and buffer to use as the context for the formatting (defaults
20777 are the selected window and the WINDOW's buffer). */)
20778 (Lisp_Object format, Lisp_Object face,
20779 Lisp_Object window, Lisp_Object buffer)
20780 {
20781 struct it it;
20782 int len;
20783 struct window *w;
20784 struct buffer *old_buffer = NULL;
20785 int face_id;
20786 int no_props = INTEGERP (face);
20787 ptrdiff_t count = SPECPDL_INDEX ();
20788 Lisp_Object str;
20789 int string_start = 0;
20790
20791 if (NILP (window))
20792 window = selected_window;
20793 CHECK_WINDOW (window);
20794 w = XWINDOW (window);
20795
20796 if (NILP (buffer))
20797 buffer = w->buffer;
20798 CHECK_BUFFER (buffer);
20799
20800 /* Make formatting the modeline a non-op when noninteractive, otherwise
20801 there will be problems later caused by a partially initialized frame. */
20802 if (NILP (format) || noninteractive)
20803 return empty_unibyte_string;
20804
20805 if (no_props)
20806 face = Qnil;
20807
20808 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20809 : EQ (face, Qt) ? (EQ (window, selected_window)
20810 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20811 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20812 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20813 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20814 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20815 : DEFAULT_FACE_ID;
20816
20817 if (XBUFFER (buffer) != current_buffer)
20818 old_buffer = current_buffer;
20819
20820 /* Save things including mode_line_proptrans_alist,
20821 and set that to nil so that we don't alter the outer value. */
20822 record_unwind_protect (unwind_format_mode_line,
20823 format_mode_line_unwind_data
20824 (old_buffer, selected_window, 1));
20825 mode_line_proptrans_alist = Qnil;
20826
20827 Fselect_window (window, Qt);
20828 if (old_buffer)
20829 set_buffer_internal_1 (XBUFFER (buffer));
20830
20831 init_iterator (&it, w, -1, -1, NULL, face_id);
20832
20833 if (no_props)
20834 {
20835 mode_line_target = MODE_LINE_NOPROP;
20836 mode_line_string_face_prop = Qnil;
20837 mode_line_string_list = Qnil;
20838 string_start = MODE_LINE_NOPROP_LEN (0);
20839 }
20840 else
20841 {
20842 mode_line_target = MODE_LINE_STRING;
20843 mode_line_string_list = Qnil;
20844 mode_line_string_face = face;
20845 mode_line_string_face_prop
20846 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20847 }
20848
20849 push_kboard (FRAME_KBOARD (it.f));
20850 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20851 pop_kboard ();
20852
20853 if (no_props)
20854 {
20855 len = MODE_LINE_NOPROP_LEN (string_start);
20856 str = make_string (mode_line_noprop_buf + string_start, len);
20857 }
20858 else
20859 {
20860 mode_line_string_list = Fnreverse (mode_line_string_list);
20861 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20862 empty_unibyte_string);
20863 }
20864
20865 unbind_to (count, Qnil);
20866 return str;
20867 }
20868
20869 /* Write a null-terminated, right justified decimal representation of
20870 the positive integer D to BUF using a minimal field width WIDTH. */
20871
20872 static void
20873 pint2str (register char *buf, register int width, register ptrdiff_t d)
20874 {
20875 register char *p = buf;
20876
20877 if (d <= 0)
20878 *p++ = '0';
20879 else
20880 {
20881 while (d > 0)
20882 {
20883 *p++ = d % 10 + '0';
20884 d /= 10;
20885 }
20886 }
20887
20888 for (width -= (int) (p - buf); width > 0; --width)
20889 *p++ = ' ';
20890 *p-- = '\0';
20891 while (p > buf)
20892 {
20893 d = *buf;
20894 *buf++ = *p;
20895 *p-- = d;
20896 }
20897 }
20898
20899 /* Write a null-terminated, right justified decimal and "human
20900 readable" representation of the nonnegative integer D to BUF using
20901 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20902
20903 static const char power_letter[] =
20904 {
20905 0, /* no letter */
20906 'k', /* kilo */
20907 'M', /* mega */
20908 'G', /* giga */
20909 'T', /* tera */
20910 'P', /* peta */
20911 'E', /* exa */
20912 'Z', /* zetta */
20913 'Y' /* yotta */
20914 };
20915
20916 static void
20917 pint2hrstr (char *buf, int width, ptrdiff_t d)
20918 {
20919 /* We aim to represent the nonnegative integer D as
20920 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20921 ptrdiff_t quotient = d;
20922 int remainder = 0;
20923 /* -1 means: do not use TENTHS. */
20924 int tenths = -1;
20925 int exponent = 0;
20926
20927 /* Length of QUOTIENT.TENTHS as a string. */
20928 int length;
20929
20930 char * psuffix;
20931 char * p;
20932
20933 if (1000 <= quotient)
20934 {
20935 /* Scale to the appropriate EXPONENT. */
20936 do
20937 {
20938 remainder = quotient % 1000;
20939 quotient /= 1000;
20940 exponent++;
20941 }
20942 while (1000 <= quotient);
20943
20944 /* Round to nearest and decide whether to use TENTHS or not. */
20945 if (quotient <= 9)
20946 {
20947 tenths = remainder / 100;
20948 if (50 <= remainder % 100)
20949 {
20950 if (tenths < 9)
20951 tenths++;
20952 else
20953 {
20954 quotient++;
20955 if (quotient == 10)
20956 tenths = -1;
20957 else
20958 tenths = 0;
20959 }
20960 }
20961 }
20962 else
20963 if (500 <= remainder)
20964 {
20965 if (quotient < 999)
20966 quotient++;
20967 else
20968 {
20969 quotient = 1;
20970 exponent++;
20971 tenths = 0;
20972 }
20973 }
20974 }
20975
20976 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
20977 if (tenths == -1 && quotient <= 99)
20978 if (quotient <= 9)
20979 length = 1;
20980 else
20981 length = 2;
20982 else
20983 length = 3;
20984 p = psuffix = buf + max (width, length);
20985
20986 /* Print EXPONENT. */
20987 *psuffix++ = power_letter[exponent];
20988 *psuffix = '\0';
20989
20990 /* Print TENTHS. */
20991 if (tenths >= 0)
20992 {
20993 *--p = '0' + tenths;
20994 *--p = '.';
20995 }
20996
20997 /* Print QUOTIENT. */
20998 do
20999 {
21000 int digit = quotient % 10;
21001 *--p = '0' + digit;
21002 }
21003 while ((quotient /= 10) != 0);
21004
21005 /* Print leading spaces. */
21006 while (buf < p)
21007 *--p = ' ';
21008 }
21009
21010 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21011 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21012 type of CODING_SYSTEM. Return updated pointer into BUF. */
21013
21014 static unsigned char invalid_eol_type[] = "(*invalid*)";
21015
21016 static char *
21017 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21018 {
21019 Lisp_Object val;
21020 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21021 const unsigned char *eol_str;
21022 int eol_str_len;
21023 /* The EOL conversion we are using. */
21024 Lisp_Object eoltype;
21025
21026 val = CODING_SYSTEM_SPEC (coding_system);
21027 eoltype = Qnil;
21028
21029 if (!VECTORP (val)) /* Not yet decided. */
21030 {
21031 *buf++ = multibyte ? '-' : ' ';
21032 if (eol_flag)
21033 eoltype = eol_mnemonic_undecided;
21034 /* Don't mention EOL conversion if it isn't decided. */
21035 }
21036 else
21037 {
21038 Lisp_Object attrs;
21039 Lisp_Object eolvalue;
21040
21041 attrs = AREF (val, 0);
21042 eolvalue = AREF (val, 2);
21043
21044 *buf++ = multibyte
21045 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21046 : ' ';
21047
21048 if (eol_flag)
21049 {
21050 /* The EOL conversion that is normal on this system. */
21051
21052 if (NILP (eolvalue)) /* Not yet decided. */
21053 eoltype = eol_mnemonic_undecided;
21054 else if (VECTORP (eolvalue)) /* Not yet decided. */
21055 eoltype = eol_mnemonic_undecided;
21056 else /* eolvalue is Qunix, Qdos, or Qmac. */
21057 eoltype = (EQ (eolvalue, Qunix)
21058 ? eol_mnemonic_unix
21059 : (EQ (eolvalue, Qdos) == 1
21060 ? eol_mnemonic_dos : eol_mnemonic_mac));
21061 }
21062 }
21063
21064 if (eol_flag)
21065 {
21066 /* Mention the EOL conversion if it is not the usual one. */
21067 if (STRINGP (eoltype))
21068 {
21069 eol_str = SDATA (eoltype);
21070 eol_str_len = SBYTES (eoltype);
21071 }
21072 else if (CHARACTERP (eoltype))
21073 {
21074 unsigned char *tmp = (unsigned char *) alloca (MAX_MULTIBYTE_LENGTH);
21075 int c = XFASTINT (eoltype);
21076 eol_str_len = CHAR_STRING (c, tmp);
21077 eol_str = tmp;
21078 }
21079 else
21080 {
21081 eol_str = invalid_eol_type;
21082 eol_str_len = sizeof (invalid_eol_type) - 1;
21083 }
21084 memcpy (buf, eol_str, eol_str_len);
21085 buf += eol_str_len;
21086 }
21087
21088 return buf;
21089 }
21090
21091 /* Return a string for the output of a mode line %-spec for window W,
21092 generated by character C. FIELD_WIDTH > 0 means pad the string
21093 returned with spaces to that value. Return a Lisp string in
21094 *STRING if the resulting string is taken from that Lisp string.
21095
21096 Note we operate on the current buffer for most purposes,
21097 the exception being w->base_line_pos. */
21098
21099 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21100
21101 static const char *
21102 decode_mode_spec (struct window *w, register int c, int field_width,
21103 Lisp_Object *string)
21104 {
21105 Lisp_Object obj;
21106 struct frame *f = XFRAME (WINDOW_FRAME (w));
21107 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21108 struct buffer *b = current_buffer;
21109
21110 obj = Qnil;
21111 *string = Qnil;
21112
21113 switch (c)
21114 {
21115 case '*':
21116 if (!NILP (BVAR (b, read_only)))
21117 return "%";
21118 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21119 return "*";
21120 return "-";
21121
21122 case '+':
21123 /* This differs from %* only for a modified read-only buffer. */
21124 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21125 return "*";
21126 if (!NILP (BVAR (b, read_only)))
21127 return "%";
21128 return "-";
21129
21130 case '&':
21131 /* This differs from %* in ignoring read-only-ness. */
21132 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21133 return "*";
21134 return "-";
21135
21136 case '%':
21137 return "%";
21138
21139 case '[':
21140 {
21141 int i;
21142 char *p;
21143
21144 if (command_loop_level > 5)
21145 return "[[[... ";
21146 p = decode_mode_spec_buf;
21147 for (i = 0; i < command_loop_level; i++)
21148 *p++ = '[';
21149 *p = 0;
21150 return decode_mode_spec_buf;
21151 }
21152
21153 case ']':
21154 {
21155 int i;
21156 char *p;
21157
21158 if (command_loop_level > 5)
21159 return " ...]]]";
21160 p = decode_mode_spec_buf;
21161 for (i = 0; i < command_loop_level; i++)
21162 *p++ = ']';
21163 *p = 0;
21164 return decode_mode_spec_buf;
21165 }
21166
21167 case '-':
21168 {
21169 register int i;
21170
21171 /* Let lots_of_dashes be a string of infinite length. */
21172 if (mode_line_target == MODE_LINE_NOPROP ||
21173 mode_line_target == MODE_LINE_STRING)
21174 return "--";
21175 if (field_width <= 0
21176 || field_width > sizeof (lots_of_dashes))
21177 {
21178 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21179 decode_mode_spec_buf[i] = '-';
21180 decode_mode_spec_buf[i] = '\0';
21181 return decode_mode_spec_buf;
21182 }
21183 else
21184 return lots_of_dashes;
21185 }
21186
21187 case 'b':
21188 obj = BVAR (b, name);
21189 break;
21190
21191 case 'c':
21192 /* %c and %l are ignored in `frame-title-format'.
21193 (In redisplay_internal, the frame title is drawn _before_ the
21194 windows are updated, so the stuff which depends on actual
21195 window contents (such as %l) may fail to render properly, or
21196 even crash emacs.) */
21197 if (mode_line_target == MODE_LINE_TITLE)
21198 return "";
21199 else
21200 {
21201 ptrdiff_t col = current_column ();
21202 w->column_number_displayed = make_number (col);
21203 pint2str (decode_mode_spec_buf, field_width, col);
21204 return decode_mode_spec_buf;
21205 }
21206
21207 case 'e':
21208 #ifndef SYSTEM_MALLOC
21209 {
21210 if (NILP (Vmemory_full))
21211 return "";
21212 else
21213 return "!MEM FULL! ";
21214 }
21215 #else
21216 return "";
21217 #endif
21218
21219 case 'F':
21220 /* %F displays the frame name. */
21221 if (!NILP (f->title))
21222 return SSDATA (f->title);
21223 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21224 return SSDATA (f->name);
21225 return "Emacs";
21226
21227 case 'f':
21228 obj = BVAR (b, filename);
21229 break;
21230
21231 case 'i':
21232 {
21233 ptrdiff_t size = ZV - BEGV;
21234 pint2str (decode_mode_spec_buf, field_width, size);
21235 return decode_mode_spec_buf;
21236 }
21237
21238 case 'I':
21239 {
21240 ptrdiff_t size = ZV - BEGV;
21241 pint2hrstr (decode_mode_spec_buf, field_width, size);
21242 return decode_mode_spec_buf;
21243 }
21244
21245 case 'l':
21246 {
21247 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21248 ptrdiff_t topline, nlines, height;
21249 ptrdiff_t junk;
21250
21251 /* %c and %l are ignored in `frame-title-format'. */
21252 if (mode_line_target == MODE_LINE_TITLE)
21253 return "";
21254
21255 startpos = XMARKER (w->start)->charpos;
21256 startpos_byte = marker_byte_position (w->start);
21257 height = WINDOW_TOTAL_LINES (w);
21258
21259 /* If we decided that this buffer isn't suitable for line numbers,
21260 don't forget that too fast. */
21261 if (EQ (w->base_line_pos, w->buffer))
21262 goto no_value;
21263 /* But do forget it, if the window shows a different buffer now. */
21264 else if (BUFFERP (w->base_line_pos))
21265 w->base_line_pos = Qnil;
21266
21267 /* If the buffer is very big, don't waste time. */
21268 if (INTEGERP (Vline_number_display_limit)
21269 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21270 {
21271 w->base_line_pos = Qnil;
21272 w->base_line_number = Qnil;
21273 goto no_value;
21274 }
21275
21276 if (INTEGERP (w->base_line_number)
21277 && INTEGERP (w->base_line_pos)
21278 && XFASTINT (w->base_line_pos) <= startpos)
21279 {
21280 line = XFASTINT (w->base_line_number);
21281 linepos = XFASTINT (w->base_line_pos);
21282 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21283 }
21284 else
21285 {
21286 line = 1;
21287 linepos = BUF_BEGV (b);
21288 linepos_byte = BUF_BEGV_BYTE (b);
21289 }
21290
21291 /* Count lines from base line to window start position. */
21292 nlines = display_count_lines (linepos_byte,
21293 startpos_byte,
21294 startpos, &junk);
21295
21296 topline = nlines + line;
21297
21298 /* Determine a new base line, if the old one is too close
21299 or too far away, or if we did not have one.
21300 "Too close" means it's plausible a scroll-down would
21301 go back past it. */
21302 if (startpos == BUF_BEGV (b))
21303 {
21304 w->base_line_number = make_number (topline);
21305 w->base_line_pos = make_number (BUF_BEGV (b));
21306 }
21307 else if (nlines < height + 25 || nlines > height * 3 + 50
21308 || linepos == BUF_BEGV (b))
21309 {
21310 ptrdiff_t limit = BUF_BEGV (b);
21311 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21312 ptrdiff_t position;
21313 ptrdiff_t distance =
21314 (height * 2 + 30) * line_number_display_limit_width;
21315
21316 if (startpos - distance > limit)
21317 {
21318 limit = startpos - distance;
21319 limit_byte = CHAR_TO_BYTE (limit);
21320 }
21321
21322 nlines = display_count_lines (startpos_byte,
21323 limit_byte,
21324 - (height * 2 + 30),
21325 &position);
21326 /* If we couldn't find the lines we wanted within
21327 line_number_display_limit_width chars per line,
21328 give up on line numbers for this window. */
21329 if (position == limit_byte && limit == startpos - distance)
21330 {
21331 w->base_line_pos = w->buffer;
21332 w->base_line_number = Qnil;
21333 goto no_value;
21334 }
21335
21336 w->base_line_number = make_number (topline - nlines);
21337 w->base_line_pos = make_number (BYTE_TO_CHAR (position));
21338 }
21339
21340 /* Now count lines from the start pos to point. */
21341 nlines = display_count_lines (startpos_byte,
21342 PT_BYTE, PT, &junk);
21343
21344 /* Record that we did display the line number. */
21345 line_number_displayed = 1;
21346
21347 /* Make the string to show. */
21348 pint2str (decode_mode_spec_buf, field_width, topline + nlines);
21349 return decode_mode_spec_buf;
21350 no_value:
21351 {
21352 char* p = decode_mode_spec_buf;
21353 int pad = field_width - 2;
21354 while (pad-- > 0)
21355 *p++ = ' ';
21356 *p++ = '?';
21357 *p++ = '?';
21358 *p = '\0';
21359 return decode_mode_spec_buf;
21360 }
21361 }
21362 break;
21363
21364 case 'm':
21365 obj = BVAR (b, mode_name);
21366 break;
21367
21368 case 'n':
21369 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21370 return " Narrow";
21371 break;
21372
21373 case 'p':
21374 {
21375 ptrdiff_t pos = marker_position (w->start);
21376 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21377
21378 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21379 {
21380 if (pos <= BUF_BEGV (b))
21381 return "All";
21382 else
21383 return "Bottom";
21384 }
21385 else if (pos <= BUF_BEGV (b))
21386 return "Top";
21387 else
21388 {
21389 if (total > 1000000)
21390 /* Do it differently for a large value, to avoid overflow. */
21391 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21392 else
21393 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21394 /* We can't normally display a 3-digit number,
21395 so get us a 2-digit number that is close. */
21396 if (total == 100)
21397 total = 99;
21398 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21399 return decode_mode_spec_buf;
21400 }
21401 }
21402
21403 /* Display percentage of size above the bottom of the screen. */
21404 case 'P':
21405 {
21406 ptrdiff_t toppos = marker_position (w->start);
21407 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21408 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21409
21410 if (botpos >= BUF_ZV (b))
21411 {
21412 if (toppos <= BUF_BEGV (b))
21413 return "All";
21414 else
21415 return "Bottom";
21416 }
21417 else
21418 {
21419 if (total > 1000000)
21420 /* Do it differently for a large value, to avoid overflow. */
21421 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21422 else
21423 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21424 /* We can't normally display a 3-digit number,
21425 so get us a 2-digit number that is close. */
21426 if (total == 100)
21427 total = 99;
21428 if (toppos <= BUF_BEGV (b))
21429 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21430 else
21431 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21432 return decode_mode_spec_buf;
21433 }
21434 }
21435
21436 case 's':
21437 /* status of process */
21438 obj = Fget_buffer_process (Fcurrent_buffer ());
21439 if (NILP (obj))
21440 return "no process";
21441 #ifndef MSDOS
21442 obj = Fsymbol_name (Fprocess_status (obj));
21443 #endif
21444 break;
21445
21446 case '@':
21447 {
21448 ptrdiff_t count = inhibit_garbage_collection ();
21449 Lisp_Object val = call1 (intern ("file-remote-p"),
21450 BVAR (current_buffer, directory));
21451 unbind_to (count, Qnil);
21452
21453 if (NILP (val))
21454 return "-";
21455 else
21456 return "@";
21457 }
21458
21459 case 't': /* indicate TEXT or BINARY */
21460 return "T";
21461
21462 case 'z':
21463 /* coding-system (not including end-of-line format) */
21464 case 'Z':
21465 /* coding-system (including end-of-line type) */
21466 {
21467 int eol_flag = (c == 'Z');
21468 char *p = decode_mode_spec_buf;
21469
21470 if (! FRAME_WINDOW_P (f))
21471 {
21472 /* No need to mention EOL here--the terminal never needs
21473 to do EOL conversion. */
21474 p = decode_mode_spec_coding (CODING_ID_NAME
21475 (FRAME_KEYBOARD_CODING (f)->id),
21476 p, 0);
21477 p = decode_mode_spec_coding (CODING_ID_NAME
21478 (FRAME_TERMINAL_CODING (f)->id),
21479 p, 0);
21480 }
21481 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21482 p, eol_flag);
21483
21484 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21485 #ifdef subprocesses
21486 obj = Fget_buffer_process (Fcurrent_buffer ());
21487 if (PROCESSP (obj))
21488 {
21489 p = decode_mode_spec_coding (XPROCESS (obj)->decode_coding_system,
21490 p, eol_flag);
21491 p = decode_mode_spec_coding (XPROCESS (obj)->encode_coding_system,
21492 p, eol_flag);
21493 }
21494 #endif /* subprocesses */
21495 #endif /* 0 */
21496 *p = 0;
21497 return decode_mode_spec_buf;
21498 }
21499 }
21500
21501 if (STRINGP (obj))
21502 {
21503 *string = obj;
21504 return SSDATA (obj);
21505 }
21506 else
21507 return "";
21508 }
21509
21510
21511 /* Count up to COUNT lines starting from START_BYTE.
21512 But don't go beyond LIMIT_BYTE.
21513 Return the number of lines thus found (always nonnegative).
21514
21515 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21516
21517 static ptrdiff_t
21518 display_count_lines (ptrdiff_t start_byte,
21519 ptrdiff_t limit_byte, ptrdiff_t count,
21520 ptrdiff_t *byte_pos_ptr)
21521 {
21522 register unsigned char *cursor;
21523 unsigned char *base;
21524
21525 register ptrdiff_t ceiling;
21526 register unsigned char *ceiling_addr;
21527 ptrdiff_t orig_count = count;
21528
21529 /* If we are not in selective display mode,
21530 check only for newlines. */
21531 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21532 && !INTEGERP (BVAR (current_buffer, selective_display)));
21533
21534 if (count > 0)
21535 {
21536 while (start_byte < limit_byte)
21537 {
21538 ceiling = BUFFER_CEILING_OF (start_byte);
21539 ceiling = min (limit_byte - 1, ceiling);
21540 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21541 base = (cursor = BYTE_POS_ADDR (start_byte));
21542 while (1)
21543 {
21544 if (selective_display)
21545 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21546 ;
21547 else
21548 while (*cursor != '\n' && ++cursor != ceiling_addr)
21549 ;
21550
21551 if (cursor != ceiling_addr)
21552 {
21553 if (--count == 0)
21554 {
21555 start_byte += cursor - base + 1;
21556 *byte_pos_ptr = start_byte;
21557 return orig_count;
21558 }
21559 else
21560 if (++cursor == ceiling_addr)
21561 break;
21562 }
21563 else
21564 break;
21565 }
21566 start_byte += cursor - base;
21567 }
21568 }
21569 else
21570 {
21571 while (start_byte > limit_byte)
21572 {
21573 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21574 ceiling = max (limit_byte, ceiling);
21575 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21576 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21577 while (1)
21578 {
21579 if (selective_display)
21580 while (--cursor != ceiling_addr
21581 && *cursor != '\n' && *cursor != 015)
21582 ;
21583 else
21584 while (--cursor != ceiling_addr && *cursor != '\n')
21585 ;
21586
21587 if (cursor != ceiling_addr)
21588 {
21589 if (++count == 0)
21590 {
21591 start_byte += cursor - base + 1;
21592 *byte_pos_ptr = start_byte;
21593 /* When scanning backwards, we should
21594 not count the newline posterior to which we stop. */
21595 return - orig_count - 1;
21596 }
21597 }
21598 else
21599 break;
21600 }
21601 /* Here we add 1 to compensate for the last decrement
21602 of CURSOR, which took it past the valid range. */
21603 start_byte += cursor - base + 1;
21604 }
21605 }
21606
21607 *byte_pos_ptr = limit_byte;
21608
21609 if (count < 0)
21610 return - orig_count + count;
21611 return orig_count - count;
21612
21613 }
21614
21615
21616 \f
21617 /***********************************************************************
21618 Displaying strings
21619 ***********************************************************************/
21620
21621 /* Display a NUL-terminated string, starting with index START.
21622
21623 If STRING is non-null, display that C string. Otherwise, the Lisp
21624 string LISP_STRING is displayed. There's a case that STRING is
21625 non-null and LISP_STRING is not nil. It means STRING is a string
21626 data of LISP_STRING. In that case, we display LISP_STRING while
21627 ignoring its text properties.
21628
21629 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21630 FACE_STRING. Display STRING or LISP_STRING with the face at
21631 FACE_STRING_POS in FACE_STRING:
21632
21633 Display the string in the environment given by IT, but use the
21634 standard display table, temporarily.
21635
21636 FIELD_WIDTH is the minimum number of output glyphs to produce.
21637 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21638 with spaces. If STRING has more characters, more than FIELD_WIDTH
21639 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21640
21641 PRECISION is the maximum number of characters to output from
21642 STRING. PRECISION < 0 means don't truncate the string.
21643
21644 This is roughly equivalent to printf format specifiers:
21645
21646 FIELD_WIDTH PRECISION PRINTF
21647 ----------------------------------------
21648 -1 -1 %s
21649 -1 10 %.10s
21650 10 -1 %10s
21651 20 10 %20.10s
21652
21653 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21654 display them, and < 0 means obey the current buffer's value of
21655 enable_multibyte_characters.
21656
21657 Value is the number of columns displayed. */
21658
21659 static int
21660 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21661 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21662 int field_width, int precision, int max_x, int multibyte)
21663 {
21664 int hpos_at_start = it->hpos;
21665 int saved_face_id = it->face_id;
21666 struct glyph_row *row = it->glyph_row;
21667 ptrdiff_t it_charpos;
21668
21669 /* Initialize the iterator IT for iteration over STRING beginning
21670 with index START. */
21671 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21672 precision, field_width, multibyte);
21673 if (string && STRINGP (lisp_string))
21674 /* LISP_STRING is the one returned by decode_mode_spec. We should
21675 ignore its text properties. */
21676 it->stop_charpos = it->end_charpos;
21677
21678 /* If displaying STRING, set up the face of the iterator from
21679 FACE_STRING, if that's given. */
21680 if (STRINGP (face_string))
21681 {
21682 ptrdiff_t endptr;
21683 struct face *face;
21684
21685 it->face_id
21686 = face_at_string_position (it->w, face_string, face_string_pos,
21687 0, it->region_beg_charpos,
21688 it->region_end_charpos,
21689 &endptr, it->base_face_id, 0);
21690 face = FACE_FROM_ID (it->f, it->face_id);
21691 it->face_box_p = face->box != FACE_NO_BOX;
21692 }
21693
21694 /* Set max_x to the maximum allowed X position. Don't let it go
21695 beyond the right edge of the window. */
21696 if (max_x <= 0)
21697 max_x = it->last_visible_x;
21698 else
21699 max_x = min (max_x, it->last_visible_x);
21700
21701 /* Skip over display elements that are not visible. because IT->w is
21702 hscrolled. */
21703 if (it->current_x < it->first_visible_x)
21704 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21705 MOVE_TO_POS | MOVE_TO_X);
21706
21707 row->ascent = it->max_ascent;
21708 row->height = it->max_ascent + it->max_descent;
21709 row->phys_ascent = it->max_phys_ascent;
21710 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21711 row->extra_line_spacing = it->max_extra_line_spacing;
21712
21713 if (STRINGP (it->string))
21714 it_charpos = IT_STRING_CHARPOS (*it);
21715 else
21716 it_charpos = IT_CHARPOS (*it);
21717
21718 /* This condition is for the case that we are called with current_x
21719 past last_visible_x. */
21720 while (it->current_x < max_x)
21721 {
21722 int x_before, x, n_glyphs_before, i, nglyphs;
21723
21724 /* Get the next display element. */
21725 if (!get_next_display_element (it))
21726 break;
21727
21728 /* Produce glyphs. */
21729 x_before = it->current_x;
21730 n_glyphs_before = row->used[TEXT_AREA];
21731 PRODUCE_GLYPHS (it);
21732
21733 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21734 i = 0;
21735 x = x_before;
21736 while (i < nglyphs)
21737 {
21738 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21739
21740 if (it->line_wrap != TRUNCATE
21741 && x + glyph->pixel_width > max_x)
21742 {
21743 /* End of continued line or max_x reached. */
21744 if (CHAR_GLYPH_PADDING_P (*glyph))
21745 {
21746 /* A wide character is unbreakable. */
21747 if (row->reversed_p)
21748 unproduce_glyphs (it, row->used[TEXT_AREA]
21749 - n_glyphs_before);
21750 row->used[TEXT_AREA] = n_glyphs_before;
21751 it->current_x = x_before;
21752 }
21753 else
21754 {
21755 if (row->reversed_p)
21756 unproduce_glyphs (it, row->used[TEXT_AREA]
21757 - (n_glyphs_before + i));
21758 row->used[TEXT_AREA] = n_glyphs_before + i;
21759 it->current_x = x;
21760 }
21761 break;
21762 }
21763 else if (x + glyph->pixel_width >= it->first_visible_x)
21764 {
21765 /* Glyph is at least partially visible. */
21766 ++it->hpos;
21767 if (x < it->first_visible_x)
21768 row->x = x - it->first_visible_x;
21769 }
21770 else
21771 {
21772 /* Glyph is off the left margin of the display area.
21773 Should not happen. */
21774 abort ();
21775 }
21776
21777 row->ascent = max (row->ascent, it->max_ascent);
21778 row->height = max (row->height, it->max_ascent + it->max_descent);
21779 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21780 row->phys_height = max (row->phys_height,
21781 it->max_phys_ascent + it->max_phys_descent);
21782 row->extra_line_spacing = max (row->extra_line_spacing,
21783 it->max_extra_line_spacing);
21784 x += glyph->pixel_width;
21785 ++i;
21786 }
21787
21788 /* Stop if max_x reached. */
21789 if (i < nglyphs)
21790 break;
21791
21792 /* Stop at line ends. */
21793 if (ITERATOR_AT_END_OF_LINE_P (it))
21794 {
21795 it->continuation_lines_width = 0;
21796 break;
21797 }
21798
21799 set_iterator_to_next (it, 1);
21800 if (STRINGP (it->string))
21801 it_charpos = IT_STRING_CHARPOS (*it);
21802 else
21803 it_charpos = IT_CHARPOS (*it);
21804
21805 /* Stop if truncating at the right edge. */
21806 if (it->line_wrap == TRUNCATE
21807 && it->current_x >= it->last_visible_x)
21808 {
21809 /* Add truncation mark, but don't do it if the line is
21810 truncated at a padding space. */
21811 if (it_charpos < it->string_nchars)
21812 {
21813 if (!FRAME_WINDOW_P (it->f))
21814 {
21815 int ii, n;
21816
21817 if (it->current_x > it->last_visible_x)
21818 {
21819 if (!row->reversed_p)
21820 {
21821 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21822 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21823 break;
21824 }
21825 else
21826 {
21827 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21828 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21829 break;
21830 unproduce_glyphs (it, ii + 1);
21831 ii = row->used[TEXT_AREA] - (ii + 1);
21832 }
21833 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21834 {
21835 row->used[TEXT_AREA] = ii;
21836 produce_special_glyphs (it, IT_TRUNCATION);
21837 }
21838 }
21839 produce_special_glyphs (it, IT_TRUNCATION);
21840 }
21841 row->truncated_on_right_p = 1;
21842 }
21843 break;
21844 }
21845 }
21846
21847 /* Maybe insert a truncation at the left. */
21848 if (it->first_visible_x
21849 && it_charpos > 0)
21850 {
21851 if (!FRAME_WINDOW_P (it->f))
21852 insert_left_trunc_glyphs (it);
21853 row->truncated_on_left_p = 1;
21854 }
21855
21856 it->face_id = saved_face_id;
21857
21858 /* Value is number of columns displayed. */
21859 return it->hpos - hpos_at_start;
21860 }
21861
21862
21863 \f
21864 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21865 appears as an element of LIST or as the car of an element of LIST.
21866 If PROPVAL is a list, compare each element against LIST in that
21867 way, and return 1/2 if any element of PROPVAL is found in LIST.
21868 Otherwise return 0. This function cannot quit.
21869 The return value is 2 if the text is invisible but with an ellipsis
21870 and 1 if it's invisible and without an ellipsis. */
21871
21872 int
21873 invisible_p (register Lisp_Object propval, Lisp_Object list)
21874 {
21875 register Lisp_Object tail, proptail;
21876
21877 for (tail = list; CONSP (tail); tail = XCDR (tail))
21878 {
21879 register Lisp_Object tem;
21880 tem = XCAR (tail);
21881 if (EQ (propval, tem))
21882 return 1;
21883 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21884 return NILP (XCDR (tem)) ? 1 : 2;
21885 }
21886
21887 if (CONSP (propval))
21888 {
21889 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21890 {
21891 Lisp_Object propelt;
21892 propelt = XCAR (proptail);
21893 for (tail = list; CONSP (tail); tail = XCDR (tail))
21894 {
21895 register Lisp_Object tem;
21896 tem = XCAR (tail);
21897 if (EQ (propelt, tem))
21898 return 1;
21899 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21900 return NILP (XCDR (tem)) ? 1 : 2;
21901 }
21902 }
21903 }
21904
21905 return 0;
21906 }
21907
21908 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21909 doc: /* Non-nil if the property makes the text invisible.
21910 POS-OR-PROP can be a marker or number, in which case it is taken to be
21911 a position in the current buffer and the value of the `invisible' property
21912 is checked; or it can be some other value, which is then presumed to be the
21913 value of the `invisible' property of the text of interest.
21914 The non-nil value returned can be t for truly invisible text or something
21915 else if the text is replaced by an ellipsis. */)
21916 (Lisp_Object pos_or_prop)
21917 {
21918 Lisp_Object prop
21919 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
21920 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
21921 : pos_or_prop);
21922 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
21923 return (invis == 0 ? Qnil
21924 : invis == 1 ? Qt
21925 : make_number (invis));
21926 }
21927
21928 /* Calculate a width or height in pixels from a specification using
21929 the following elements:
21930
21931 SPEC ::=
21932 NUM - a (fractional) multiple of the default font width/height
21933 (NUM) - specifies exactly NUM pixels
21934 UNIT - a fixed number of pixels, see below.
21935 ELEMENT - size of a display element in pixels, see below.
21936 (NUM . SPEC) - equals NUM * SPEC
21937 (+ SPEC SPEC ...) - add pixel values
21938 (- SPEC SPEC ...) - subtract pixel values
21939 (- SPEC) - negate pixel value
21940
21941 NUM ::=
21942 INT or FLOAT - a number constant
21943 SYMBOL - use symbol's (buffer local) variable binding.
21944
21945 UNIT ::=
21946 in - pixels per inch *)
21947 mm - pixels per 1/1000 meter *)
21948 cm - pixels per 1/100 meter *)
21949 width - width of current font in pixels.
21950 height - height of current font in pixels.
21951
21952 *) using the ratio(s) defined in display-pixels-per-inch.
21953
21954 ELEMENT ::=
21955
21956 left-fringe - left fringe width in pixels
21957 right-fringe - right fringe width in pixels
21958
21959 left-margin - left margin width in pixels
21960 right-margin - right margin width in pixels
21961
21962 scroll-bar - scroll-bar area width in pixels
21963
21964 Examples:
21965
21966 Pixels corresponding to 5 inches:
21967 (5 . in)
21968
21969 Total width of non-text areas on left side of window (if scroll-bar is on left):
21970 '(space :width (+ left-fringe left-margin scroll-bar))
21971
21972 Align to first text column (in header line):
21973 '(space :align-to 0)
21974
21975 Align to middle of text area minus half the width of variable `my-image'
21976 containing a loaded image:
21977 '(space :align-to (0.5 . (- text my-image)))
21978
21979 Width of left margin minus width of 1 character in the default font:
21980 '(space :width (- left-margin 1))
21981
21982 Width of left margin minus width of 2 characters in the current font:
21983 '(space :width (- left-margin (2 . width)))
21984
21985 Center 1 character over left-margin (in header line):
21986 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
21987
21988 Different ways to express width of left fringe plus left margin minus one pixel:
21989 '(space :width (- (+ left-fringe left-margin) (1)))
21990 '(space :width (+ left-fringe left-margin (- (1))))
21991 '(space :width (+ left-fringe left-margin (-1)))
21992
21993 */
21994
21995 #define NUMVAL(X) \
21996 ((INTEGERP (X) || FLOATP (X)) \
21997 ? XFLOATINT (X) \
21998 : - 1)
21999
22000 static int
22001 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22002 struct font *font, int width_p, int *align_to)
22003 {
22004 double pixels;
22005
22006 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22007 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22008
22009 if (NILP (prop))
22010 return OK_PIXELS (0);
22011
22012 xassert (FRAME_LIVE_P (it->f));
22013
22014 if (SYMBOLP (prop))
22015 {
22016 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22017 {
22018 char *unit = SSDATA (SYMBOL_NAME (prop));
22019
22020 if (unit[0] == 'i' && unit[1] == 'n')
22021 pixels = 1.0;
22022 else if (unit[0] == 'm' && unit[1] == 'm')
22023 pixels = 25.4;
22024 else if (unit[0] == 'c' && unit[1] == 'm')
22025 pixels = 2.54;
22026 else
22027 pixels = 0;
22028 if (pixels > 0)
22029 {
22030 double ppi;
22031 #ifdef HAVE_WINDOW_SYSTEM
22032 if (FRAME_WINDOW_P (it->f)
22033 && (ppi = (width_p
22034 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22035 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22036 ppi > 0))
22037 return OK_PIXELS (ppi / pixels);
22038 #endif
22039
22040 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22041 || (CONSP (Vdisplay_pixels_per_inch)
22042 && (ppi = (width_p
22043 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22044 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22045 ppi > 0)))
22046 return OK_PIXELS (ppi / pixels);
22047
22048 return 0;
22049 }
22050 }
22051
22052 #ifdef HAVE_WINDOW_SYSTEM
22053 if (EQ (prop, Qheight))
22054 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22055 if (EQ (prop, Qwidth))
22056 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22057 #else
22058 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22059 return OK_PIXELS (1);
22060 #endif
22061
22062 if (EQ (prop, Qtext))
22063 return OK_PIXELS (width_p
22064 ? window_box_width (it->w, TEXT_AREA)
22065 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22066
22067 if (align_to && *align_to < 0)
22068 {
22069 *res = 0;
22070 if (EQ (prop, Qleft))
22071 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22072 if (EQ (prop, Qright))
22073 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22074 if (EQ (prop, Qcenter))
22075 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22076 + window_box_width (it->w, TEXT_AREA) / 2);
22077 if (EQ (prop, Qleft_fringe))
22078 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22079 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22080 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22081 if (EQ (prop, Qright_fringe))
22082 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22083 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22084 : window_box_right_offset (it->w, TEXT_AREA));
22085 if (EQ (prop, Qleft_margin))
22086 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22087 if (EQ (prop, Qright_margin))
22088 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22089 if (EQ (prop, Qscroll_bar))
22090 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22091 ? 0
22092 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22093 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22094 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22095 : 0)));
22096 }
22097 else
22098 {
22099 if (EQ (prop, Qleft_fringe))
22100 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22101 if (EQ (prop, Qright_fringe))
22102 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22103 if (EQ (prop, Qleft_margin))
22104 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22105 if (EQ (prop, Qright_margin))
22106 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22107 if (EQ (prop, Qscroll_bar))
22108 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22109 }
22110
22111 prop = buffer_local_value_1 (prop, it->w->buffer);
22112 if (EQ (prop, Qunbound))
22113 prop = Qnil;
22114 }
22115
22116 if (INTEGERP (prop) || FLOATP (prop))
22117 {
22118 int base_unit = (width_p
22119 ? FRAME_COLUMN_WIDTH (it->f)
22120 : FRAME_LINE_HEIGHT (it->f));
22121 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22122 }
22123
22124 if (CONSP (prop))
22125 {
22126 Lisp_Object car = XCAR (prop);
22127 Lisp_Object cdr = XCDR (prop);
22128
22129 if (SYMBOLP (car))
22130 {
22131 #ifdef HAVE_WINDOW_SYSTEM
22132 if (FRAME_WINDOW_P (it->f)
22133 && valid_image_p (prop))
22134 {
22135 ptrdiff_t id = lookup_image (it->f, prop);
22136 struct image *img = IMAGE_FROM_ID (it->f, id);
22137
22138 return OK_PIXELS (width_p ? img->width : img->height);
22139 }
22140 #endif
22141 if (EQ (car, Qplus) || EQ (car, Qminus))
22142 {
22143 int first = 1;
22144 double px;
22145
22146 pixels = 0;
22147 while (CONSP (cdr))
22148 {
22149 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22150 font, width_p, align_to))
22151 return 0;
22152 if (first)
22153 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22154 else
22155 pixels += px;
22156 cdr = XCDR (cdr);
22157 }
22158 if (EQ (car, Qminus))
22159 pixels = -pixels;
22160 return OK_PIXELS (pixels);
22161 }
22162
22163 car = buffer_local_value_1 (car, it->w->buffer);
22164 if (EQ (car, Qunbound))
22165 car = Qnil;
22166 }
22167
22168 if (INTEGERP (car) || FLOATP (car))
22169 {
22170 double fact;
22171 pixels = XFLOATINT (car);
22172 if (NILP (cdr))
22173 return OK_PIXELS (pixels);
22174 if (calc_pixel_width_or_height (&fact, it, cdr,
22175 font, width_p, align_to))
22176 return OK_PIXELS (pixels * fact);
22177 return 0;
22178 }
22179
22180 return 0;
22181 }
22182
22183 return 0;
22184 }
22185
22186 \f
22187 /***********************************************************************
22188 Glyph Display
22189 ***********************************************************************/
22190
22191 #ifdef HAVE_WINDOW_SYSTEM
22192
22193 #if GLYPH_DEBUG
22194
22195 void
22196 dump_glyph_string (struct glyph_string *s)
22197 {
22198 fprintf (stderr, "glyph string\n");
22199 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22200 s->x, s->y, s->width, s->height);
22201 fprintf (stderr, " ybase = %d\n", s->ybase);
22202 fprintf (stderr, " hl = %d\n", s->hl);
22203 fprintf (stderr, " left overhang = %d, right = %d\n",
22204 s->left_overhang, s->right_overhang);
22205 fprintf (stderr, " nchars = %d\n", s->nchars);
22206 fprintf (stderr, " extends to end of line = %d\n",
22207 s->extends_to_end_of_line_p);
22208 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22209 fprintf (stderr, " bg width = %d\n", s->background_width);
22210 }
22211
22212 #endif /* GLYPH_DEBUG */
22213
22214 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22215 of XChar2b structures for S; it can't be allocated in
22216 init_glyph_string because it must be allocated via `alloca'. W
22217 is the window on which S is drawn. ROW and AREA are the glyph row
22218 and area within the row from which S is constructed. START is the
22219 index of the first glyph structure covered by S. HL is a
22220 face-override for drawing S. */
22221
22222 #ifdef HAVE_NTGUI
22223 #define OPTIONAL_HDC(hdc) HDC hdc,
22224 #define DECLARE_HDC(hdc) HDC hdc;
22225 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22226 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22227 #endif
22228
22229 #ifndef OPTIONAL_HDC
22230 #define OPTIONAL_HDC(hdc)
22231 #define DECLARE_HDC(hdc)
22232 #define ALLOCATE_HDC(hdc, f)
22233 #define RELEASE_HDC(hdc, f)
22234 #endif
22235
22236 static void
22237 init_glyph_string (struct glyph_string *s,
22238 OPTIONAL_HDC (hdc)
22239 XChar2b *char2b, struct window *w, struct glyph_row *row,
22240 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22241 {
22242 memset (s, 0, sizeof *s);
22243 s->w = w;
22244 s->f = XFRAME (w->frame);
22245 #ifdef HAVE_NTGUI
22246 s->hdc = hdc;
22247 #endif
22248 s->display = FRAME_X_DISPLAY (s->f);
22249 s->window = FRAME_X_WINDOW (s->f);
22250 s->char2b = char2b;
22251 s->hl = hl;
22252 s->row = row;
22253 s->area = area;
22254 s->first_glyph = row->glyphs[area] + start;
22255 s->height = row->height;
22256 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22257 s->ybase = s->y + row->ascent;
22258 }
22259
22260
22261 /* Append the list of glyph strings with head H and tail T to the list
22262 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22263
22264 static inline void
22265 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22266 struct glyph_string *h, struct glyph_string *t)
22267 {
22268 if (h)
22269 {
22270 if (*head)
22271 (*tail)->next = h;
22272 else
22273 *head = h;
22274 h->prev = *tail;
22275 *tail = t;
22276 }
22277 }
22278
22279
22280 /* Prepend the list of glyph strings with head H and tail T to the
22281 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22282 result. */
22283
22284 static inline void
22285 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22286 struct glyph_string *h, struct glyph_string *t)
22287 {
22288 if (h)
22289 {
22290 if (*head)
22291 (*head)->prev = t;
22292 else
22293 *tail = t;
22294 t->next = *head;
22295 *head = h;
22296 }
22297 }
22298
22299
22300 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22301 Set *HEAD and *TAIL to the resulting list. */
22302
22303 static inline void
22304 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22305 struct glyph_string *s)
22306 {
22307 s->next = s->prev = NULL;
22308 append_glyph_string_lists (head, tail, s, s);
22309 }
22310
22311
22312 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22313 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22314 make sure that X resources for the face returned are allocated.
22315 Value is a pointer to a realized face that is ready for display if
22316 DISPLAY_P is non-zero. */
22317
22318 static inline struct face *
22319 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22320 XChar2b *char2b, int display_p)
22321 {
22322 struct face *face = FACE_FROM_ID (f, face_id);
22323
22324 if (face->font)
22325 {
22326 unsigned code = face->font->driver->encode_char (face->font, c);
22327
22328 if (code != FONT_INVALID_CODE)
22329 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22330 else
22331 STORE_XCHAR2B (char2b, 0, 0);
22332 }
22333
22334 /* Make sure X resources of the face are allocated. */
22335 #ifdef HAVE_X_WINDOWS
22336 if (display_p)
22337 #endif
22338 {
22339 xassert (face != NULL);
22340 PREPARE_FACE_FOR_DISPLAY (f, face);
22341 }
22342
22343 return face;
22344 }
22345
22346
22347 /* Get face and two-byte form of character glyph GLYPH on frame F.
22348 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22349 a pointer to a realized face that is ready for display. */
22350
22351 static inline struct face *
22352 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22353 XChar2b *char2b, int *two_byte_p)
22354 {
22355 struct face *face;
22356
22357 xassert (glyph->type == CHAR_GLYPH);
22358 face = FACE_FROM_ID (f, glyph->face_id);
22359
22360 if (two_byte_p)
22361 *two_byte_p = 0;
22362
22363 if (face->font)
22364 {
22365 unsigned code;
22366
22367 if (CHAR_BYTE8_P (glyph->u.ch))
22368 code = CHAR_TO_BYTE8 (glyph->u.ch);
22369 else
22370 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22371
22372 if (code != FONT_INVALID_CODE)
22373 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22374 else
22375 STORE_XCHAR2B (char2b, 0, 0);
22376 }
22377
22378 /* Make sure X resources of the face are allocated. */
22379 xassert (face != NULL);
22380 PREPARE_FACE_FOR_DISPLAY (f, face);
22381 return face;
22382 }
22383
22384
22385 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22386 Return 1 if FONT has a glyph for C, otherwise return 0. */
22387
22388 static inline int
22389 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22390 {
22391 unsigned code;
22392
22393 if (CHAR_BYTE8_P (c))
22394 code = CHAR_TO_BYTE8 (c);
22395 else
22396 code = font->driver->encode_char (font, c);
22397
22398 if (code == FONT_INVALID_CODE)
22399 return 0;
22400 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22401 return 1;
22402 }
22403
22404
22405 /* Fill glyph string S with composition components specified by S->cmp.
22406
22407 BASE_FACE is the base face of the composition.
22408 S->cmp_from is the index of the first component for S.
22409
22410 OVERLAPS non-zero means S should draw the foreground only, and use
22411 its physical height for clipping. See also draw_glyphs.
22412
22413 Value is the index of a component not in S. */
22414
22415 static int
22416 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22417 int overlaps)
22418 {
22419 int i;
22420 /* For all glyphs of this composition, starting at the offset
22421 S->cmp_from, until we reach the end of the definition or encounter a
22422 glyph that requires the different face, add it to S. */
22423 struct face *face;
22424
22425 xassert (s);
22426
22427 s->for_overlaps = overlaps;
22428 s->face = NULL;
22429 s->font = NULL;
22430 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22431 {
22432 int c = COMPOSITION_GLYPH (s->cmp, i);
22433
22434 /* TAB in a composition means display glyphs with padding space
22435 on the left or right. */
22436 if (c != '\t')
22437 {
22438 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22439 -1, Qnil);
22440
22441 face = get_char_face_and_encoding (s->f, c, face_id,
22442 s->char2b + i, 1);
22443 if (face)
22444 {
22445 if (! s->face)
22446 {
22447 s->face = face;
22448 s->font = s->face->font;
22449 }
22450 else if (s->face != face)
22451 break;
22452 }
22453 }
22454 ++s->nchars;
22455 }
22456 s->cmp_to = i;
22457
22458 if (s->face == NULL)
22459 {
22460 s->face = base_face->ascii_face;
22461 s->font = s->face->font;
22462 }
22463
22464 /* All glyph strings for the same composition has the same width,
22465 i.e. the width set for the first component of the composition. */
22466 s->width = s->first_glyph->pixel_width;
22467
22468 /* If the specified font could not be loaded, use the frame's
22469 default font, but record the fact that we couldn't load it in
22470 the glyph string so that we can draw rectangles for the
22471 characters of the glyph string. */
22472 if (s->font == NULL)
22473 {
22474 s->font_not_found_p = 1;
22475 s->font = FRAME_FONT (s->f);
22476 }
22477
22478 /* Adjust base line for subscript/superscript text. */
22479 s->ybase += s->first_glyph->voffset;
22480
22481 /* This glyph string must always be drawn with 16-bit functions. */
22482 s->two_byte_p = 1;
22483
22484 return s->cmp_to;
22485 }
22486
22487 static int
22488 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22489 int start, int end, int overlaps)
22490 {
22491 struct glyph *glyph, *last;
22492 Lisp_Object lgstring;
22493 int i;
22494
22495 s->for_overlaps = overlaps;
22496 glyph = s->row->glyphs[s->area] + start;
22497 last = s->row->glyphs[s->area] + end;
22498 s->cmp_id = glyph->u.cmp.id;
22499 s->cmp_from = glyph->slice.cmp.from;
22500 s->cmp_to = glyph->slice.cmp.to + 1;
22501 s->face = FACE_FROM_ID (s->f, face_id);
22502 lgstring = composition_gstring_from_id (s->cmp_id);
22503 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22504 glyph++;
22505 while (glyph < last
22506 && glyph->u.cmp.automatic
22507 && glyph->u.cmp.id == s->cmp_id
22508 && s->cmp_to == glyph->slice.cmp.from)
22509 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22510
22511 for (i = s->cmp_from; i < s->cmp_to; i++)
22512 {
22513 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22514 unsigned code = LGLYPH_CODE (lglyph);
22515
22516 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22517 }
22518 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22519 return glyph - s->row->glyphs[s->area];
22520 }
22521
22522
22523 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22524 See the comment of fill_glyph_string for arguments.
22525 Value is the index of the first glyph not in S. */
22526
22527
22528 static int
22529 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22530 int start, int end, int overlaps)
22531 {
22532 struct glyph *glyph, *last;
22533 int voffset;
22534
22535 xassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22536 s->for_overlaps = overlaps;
22537 glyph = s->row->glyphs[s->area] + start;
22538 last = s->row->glyphs[s->area] + end;
22539 voffset = glyph->voffset;
22540 s->face = FACE_FROM_ID (s->f, face_id);
22541 s->font = s->face->font;
22542 s->nchars = 1;
22543 s->width = glyph->pixel_width;
22544 glyph++;
22545 while (glyph < last
22546 && glyph->type == GLYPHLESS_GLYPH
22547 && glyph->voffset == voffset
22548 && glyph->face_id == face_id)
22549 {
22550 s->nchars++;
22551 s->width += glyph->pixel_width;
22552 glyph++;
22553 }
22554 s->ybase += voffset;
22555 return glyph - s->row->glyphs[s->area];
22556 }
22557
22558
22559 /* Fill glyph string S from a sequence of character glyphs.
22560
22561 FACE_ID is the face id of the string. START is the index of the
22562 first glyph to consider, END is the index of the last + 1.
22563 OVERLAPS non-zero means S should draw the foreground only, and use
22564 its physical height for clipping. See also draw_glyphs.
22565
22566 Value is the index of the first glyph not in S. */
22567
22568 static int
22569 fill_glyph_string (struct glyph_string *s, int face_id,
22570 int start, int end, int overlaps)
22571 {
22572 struct glyph *glyph, *last;
22573 int voffset;
22574 int glyph_not_available_p;
22575
22576 xassert (s->f == XFRAME (s->w->frame));
22577 xassert (s->nchars == 0);
22578 xassert (start >= 0 && end > start);
22579
22580 s->for_overlaps = overlaps;
22581 glyph = s->row->glyphs[s->area] + start;
22582 last = s->row->glyphs[s->area] + end;
22583 voffset = glyph->voffset;
22584 s->padding_p = glyph->padding_p;
22585 glyph_not_available_p = glyph->glyph_not_available_p;
22586
22587 while (glyph < last
22588 && glyph->type == CHAR_GLYPH
22589 && glyph->voffset == voffset
22590 /* Same face id implies same font, nowadays. */
22591 && glyph->face_id == face_id
22592 && glyph->glyph_not_available_p == glyph_not_available_p)
22593 {
22594 int two_byte_p;
22595
22596 s->face = get_glyph_face_and_encoding (s->f, glyph,
22597 s->char2b + s->nchars,
22598 &two_byte_p);
22599 s->two_byte_p = two_byte_p;
22600 ++s->nchars;
22601 xassert (s->nchars <= end - start);
22602 s->width += glyph->pixel_width;
22603 if (glyph++->padding_p != s->padding_p)
22604 break;
22605 }
22606
22607 s->font = s->face->font;
22608
22609 /* If the specified font could not be loaded, use the frame's font,
22610 but record the fact that we couldn't load it in
22611 S->font_not_found_p so that we can draw rectangles for the
22612 characters of the glyph string. */
22613 if (s->font == NULL || glyph_not_available_p)
22614 {
22615 s->font_not_found_p = 1;
22616 s->font = FRAME_FONT (s->f);
22617 }
22618
22619 /* Adjust base line for subscript/superscript text. */
22620 s->ybase += voffset;
22621
22622 xassert (s->face && s->face->gc);
22623 return glyph - s->row->glyphs[s->area];
22624 }
22625
22626
22627 /* Fill glyph string S from image glyph S->first_glyph. */
22628
22629 static void
22630 fill_image_glyph_string (struct glyph_string *s)
22631 {
22632 xassert (s->first_glyph->type == IMAGE_GLYPH);
22633 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22634 xassert (s->img);
22635 s->slice = s->first_glyph->slice.img;
22636 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22637 s->font = s->face->font;
22638 s->width = s->first_glyph->pixel_width;
22639
22640 /* Adjust base line for subscript/superscript text. */
22641 s->ybase += s->first_glyph->voffset;
22642 }
22643
22644
22645 /* Fill glyph string S from a sequence of stretch glyphs.
22646
22647 START is the index of the first glyph to consider,
22648 END is the index of the last + 1.
22649
22650 Value is the index of the first glyph not in S. */
22651
22652 static int
22653 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22654 {
22655 struct glyph *glyph, *last;
22656 int voffset, face_id;
22657
22658 xassert (s->first_glyph->type == STRETCH_GLYPH);
22659
22660 glyph = s->row->glyphs[s->area] + start;
22661 last = s->row->glyphs[s->area] + end;
22662 face_id = glyph->face_id;
22663 s->face = FACE_FROM_ID (s->f, face_id);
22664 s->font = s->face->font;
22665 s->width = glyph->pixel_width;
22666 s->nchars = 1;
22667 voffset = glyph->voffset;
22668
22669 for (++glyph;
22670 (glyph < last
22671 && glyph->type == STRETCH_GLYPH
22672 && glyph->voffset == voffset
22673 && glyph->face_id == face_id);
22674 ++glyph)
22675 s->width += glyph->pixel_width;
22676
22677 /* Adjust base line for subscript/superscript text. */
22678 s->ybase += voffset;
22679
22680 /* The case that face->gc == 0 is handled when drawing the glyph
22681 string by calling PREPARE_FACE_FOR_DISPLAY. */
22682 xassert (s->face);
22683 return glyph - s->row->glyphs[s->area];
22684 }
22685
22686 static struct font_metrics *
22687 get_per_char_metric (struct font *font, XChar2b *char2b)
22688 {
22689 static struct font_metrics metrics;
22690 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22691
22692 if (! font || code == FONT_INVALID_CODE)
22693 return NULL;
22694 font->driver->text_extents (font, &code, 1, &metrics);
22695 return &metrics;
22696 }
22697
22698 /* EXPORT for RIF:
22699 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22700 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22701 assumed to be zero. */
22702
22703 void
22704 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22705 {
22706 *left = *right = 0;
22707
22708 if (glyph->type == CHAR_GLYPH)
22709 {
22710 struct face *face;
22711 XChar2b char2b;
22712 struct font_metrics *pcm;
22713
22714 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22715 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22716 {
22717 if (pcm->rbearing > pcm->width)
22718 *right = pcm->rbearing - pcm->width;
22719 if (pcm->lbearing < 0)
22720 *left = -pcm->lbearing;
22721 }
22722 }
22723 else if (glyph->type == COMPOSITE_GLYPH)
22724 {
22725 if (! glyph->u.cmp.automatic)
22726 {
22727 struct composition *cmp = composition_table[glyph->u.cmp.id];
22728
22729 if (cmp->rbearing > cmp->pixel_width)
22730 *right = cmp->rbearing - cmp->pixel_width;
22731 if (cmp->lbearing < 0)
22732 *left = - cmp->lbearing;
22733 }
22734 else
22735 {
22736 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22737 struct font_metrics metrics;
22738
22739 composition_gstring_width (gstring, glyph->slice.cmp.from,
22740 glyph->slice.cmp.to + 1, &metrics);
22741 if (metrics.rbearing > metrics.width)
22742 *right = metrics.rbearing - metrics.width;
22743 if (metrics.lbearing < 0)
22744 *left = - metrics.lbearing;
22745 }
22746 }
22747 }
22748
22749
22750 /* Return the index of the first glyph preceding glyph string S that
22751 is overwritten by S because of S's left overhang. Value is -1
22752 if no glyphs are overwritten. */
22753
22754 static int
22755 left_overwritten (struct glyph_string *s)
22756 {
22757 int k;
22758
22759 if (s->left_overhang)
22760 {
22761 int x = 0, i;
22762 struct glyph *glyphs = s->row->glyphs[s->area];
22763 int first = s->first_glyph - glyphs;
22764
22765 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22766 x -= glyphs[i].pixel_width;
22767
22768 k = i + 1;
22769 }
22770 else
22771 k = -1;
22772
22773 return k;
22774 }
22775
22776
22777 /* Return the index of the first glyph preceding glyph string S that
22778 is overwriting S because of its right overhang. Value is -1 if no
22779 glyph in front of S overwrites S. */
22780
22781 static int
22782 left_overwriting (struct glyph_string *s)
22783 {
22784 int i, k, x;
22785 struct glyph *glyphs = s->row->glyphs[s->area];
22786 int first = s->first_glyph - glyphs;
22787
22788 k = -1;
22789 x = 0;
22790 for (i = first - 1; i >= 0; --i)
22791 {
22792 int left, right;
22793 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22794 if (x + right > 0)
22795 k = i;
22796 x -= glyphs[i].pixel_width;
22797 }
22798
22799 return k;
22800 }
22801
22802
22803 /* Return the index of the last glyph following glyph string S that is
22804 overwritten by S because of S's right overhang. Value is -1 if
22805 no such glyph is found. */
22806
22807 static int
22808 right_overwritten (struct glyph_string *s)
22809 {
22810 int k = -1;
22811
22812 if (s->right_overhang)
22813 {
22814 int x = 0, i;
22815 struct glyph *glyphs = s->row->glyphs[s->area];
22816 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22817 int end = s->row->used[s->area];
22818
22819 for (i = first; i < end && s->right_overhang > x; ++i)
22820 x += glyphs[i].pixel_width;
22821
22822 k = i;
22823 }
22824
22825 return k;
22826 }
22827
22828
22829 /* Return the index of the last glyph following glyph string S that
22830 overwrites S because of its left overhang. Value is negative
22831 if no such glyph is found. */
22832
22833 static int
22834 right_overwriting (struct glyph_string *s)
22835 {
22836 int i, k, x;
22837 int end = s->row->used[s->area];
22838 struct glyph *glyphs = s->row->glyphs[s->area];
22839 int first = (s->first_glyph - glyphs) + (s->cmp ? 1 : s->nchars);
22840
22841 k = -1;
22842 x = 0;
22843 for (i = first; i < end; ++i)
22844 {
22845 int left, right;
22846 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22847 if (x - left < 0)
22848 k = i;
22849 x += glyphs[i].pixel_width;
22850 }
22851
22852 return k;
22853 }
22854
22855
22856 /* Set background width of glyph string S. START is the index of the
22857 first glyph following S. LAST_X is the right-most x-position + 1
22858 in the drawing area. */
22859
22860 static inline void
22861 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22862 {
22863 /* If the face of this glyph string has to be drawn to the end of
22864 the drawing area, set S->extends_to_end_of_line_p. */
22865
22866 if (start == s->row->used[s->area]
22867 && s->area == TEXT_AREA
22868 && ((s->row->fill_line_p
22869 && (s->hl == DRAW_NORMAL_TEXT
22870 || s->hl == DRAW_IMAGE_RAISED
22871 || s->hl == DRAW_IMAGE_SUNKEN))
22872 || s->hl == DRAW_MOUSE_FACE))
22873 s->extends_to_end_of_line_p = 1;
22874
22875 /* If S extends its face to the end of the line, set its
22876 background_width to the distance to the right edge of the drawing
22877 area. */
22878 if (s->extends_to_end_of_line_p)
22879 s->background_width = last_x - s->x + 1;
22880 else
22881 s->background_width = s->width;
22882 }
22883
22884
22885 /* Compute overhangs and x-positions for glyph string S and its
22886 predecessors, or successors. X is the starting x-position for S.
22887 BACKWARD_P non-zero means process predecessors. */
22888
22889 static void
22890 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22891 {
22892 if (backward_p)
22893 {
22894 while (s)
22895 {
22896 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22897 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22898 x -= s->width;
22899 s->x = x;
22900 s = s->prev;
22901 }
22902 }
22903 else
22904 {
22905 while (s)
22906 {
22907 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22908 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22909 s->x = x;
22910 x += s->width;
22911 s = s->next;
22912 }
22913 }
22914 }
22915
22916
22917
22918 /* The following macros are only called from draw_glyphs below.
22919 They reference the following parameters of that function directly:
22920 `w', `row', `area', and `overlap_p'
22921 as well as the following local variables:
22922 `s', `f', and `hdc' (in W32) */
22923
22924 #ifdef HAVE_NTGUI
22925 /* On W32, silently add local `hdc' variable to argument list of
22926 init_glyph_string. */
22927 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22928 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
22929 #else
22930 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
22931 init_glyph_string (s, char2b, w, row, area, start, hl)
22932 #endif
22933
22934 /* Add a glyph string for a stretch glyph to the list of strings
22935 between HEAD and TAIL. START is the index of the stretch glyph in
22936 row area AREA of glyph row ROW. END is the index of the last glyph
22937 in that glyph row area. X is the current output position assigned
22938 to the new glyph string constructed. HL overrides that face of the
22939 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22940 is the right-most x-position of the drawing area. */
22941
22942 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
22943 and below -- keep them on one line. */
22944 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22945 do \
22946 { \
22947 s = (struct glyph_string *) alloca (sizeof *s); \
22948 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22949 START = fill_stretch_glyph_string (s, START, END); \
22950 append_glyph_string (&HEAD, &TAIL, s); \
22951 s->x = (X); \
22952 } \
22953 while (0)
22954
22955
22956 /* Add a glyph string for an image glyph to the list of strings
22957 between HEAD and TAIL. START is the index of the image glyph in
22958 row area AREA of glyph row ROW. END is the index of the last glyph
22959 in that glyph row area. X is the current output position assigned
22960 to the new glyph string constructed. HL overrides that face of the
22961 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
22962 is the right-most x-position of the drawing area. */
22963
22964 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
22965 do \
22966 { \
22967 s = (struct glyph_string *) alloca (sizeof *s); \
22968 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
22969 fill_image_glyph_string (s); \
22970 append_glyph_string (&HEAD, &TAIL, s); \
22971 ++START; \
22972 s->x = (X); \
22973 } \
22974 while (0)
22975
22976
22977 /* Add a glyph string for a sequence of character glyphs to the list
22978 of strings between HEAD and TAIL. START is the index of the first
22979 glyph in row area AREA of glyph row ROW that is part of the new
22980 glyph string. END is the index of the last glyph in that glyph row
22981 area. X is the current output position assigned to the new glyph
22982 string constructed. HL overrides that face of the glyph; e.g. it
22983 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
22984 right-most x-position of the drawing area. */
22985
22986 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
22987 do \
22988 { \
22989 int face_id; \
22990 XChar2b *char2b; \
22991 \
22992 face_id = (row)->glyphs[area][START].face_id; \
22993 \
22994 s = (struct glyph_string *) alloca (sizeof *s); \
22995 char2b = (XChar2b *) alloca ((END - START) * sizeof *char2b); \
22996 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
22997 append_glyph_string (&HEAD, &TAIL, s); \
22998 s->x = (X); \
22999 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23000 } \
23001 while (0)
23002
23003
23004 /* Add a glyph string for a composite sequence to the list of strings
23005 between HEAD and TAIL. START is the index of the first glyph in
23006 row area AREA of glyph row ROW that is part of the new glyph
23007 string. END is the index of the last glyph in that glyph row area.
23008 X is the current output position assigned to the new glyph string
23009 constructed. HL overrides that face of the glyph; e.g. it is
23010 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23011 x-position of the drawing area. */
23012
23013 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23014 do { \
23015 int face_id = (row)->glyphs[area][START].face_id; \
23016 struct face *base_face = FACE_FROM_ID (f, face_id); \
23017 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23018 struct composition *cmp = composition_table[cmp_id]; \
23019 XChar2b *char2b; \
23020 struct glyph_string *first_s = NULL; \
23021 int n; \
23022 \
23023 char2b = (XChar2b *) alloca ((sizeof *char2b) * cmp->glyph_len); \
23024 \
23025 /* Make glyph_strings for each glyph sequence that is drawable by \
23026 the same face, and append them to HEAD/TAIL. */ \
23027 for (n = 0; n < cmp->glyph_len;) \
23028 { \
23029 s = (struct glyph_string *) alloca (sizeof *s); \
23030 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23031 append_glyph_string (&(HEAD), &(TAIL), s); \
23032 s->cmp = cmp; \
23033 s->cmp_from = n; \
23034 s->x = (X); \
23035 if (n == 0) \
23036 first_s = s; \
23037 n = fill_composite_glyph_string (s, base_face, overlaps); \
23038 } \
23039 \
23040 ++START; \
23041 s = first_s; \
23042 } while (0)
23043
23044
23045 /* Add a glyph string for a glyph-string sequence to the list of strings
23046 between HEAD and TAIL. */
23047
23048 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23049 do { \
23050 int face_id; \
23051 XChar2b *char2b; \
23052 Lisp_Object gstring; \
23053 \
23054 face_id = (row)->glyphs[area][START].face_id; \
23055 gstring = (composition_gstring_from_id \
23056 ((row)->glyphs[area][START].u.cmp.id)); \
23057 s = (struct glyph_string *) alloca (sizeof *s); \
23058 char2b = (XChar2b *) alloca ((sizeof *char2b) \
23059 * LGSTRING_GLYPH_LEN (gstring)); \
23060 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23061 append_glyph_string (&(HEAD), &(TAIL), s); \
23062 s->x = (X); \
23063 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23064 } while (0)
23065
23066
23067 /* Add a glyph string for a sequence of glyphless character's glyphs
23068 to the list of strings between HEAD and TAIL. The meanings of
23069 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23070
23071 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23072 do \
23073 { \
23074 int face_id; \
23075 \
23076 face_id = (row)->glyphs[area][START].face_id; \
23077 \
23078 s = (struct glyph_string *) alloca (sizeof *s); \
23079 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23080 append_glyph_string (&HEAD, &TAIL, s); \
23081 s->x = (X); \
23082 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23083 overlaps); \
23084 } \
23085 while (0)
23086
23087
23088 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23089 of AREA of glyph row ROW on window W between indices START and END.
23090 HL overrides the face for drawing glyph strings, e.g. it is
23091 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23092 x-positions of the drawing area.
23093
23094 This is an ugly monster macro construct because we must use alloca
23095 to allocate glyph strings (because draw_glyphs can be called
23096 asynchronously). */
23097
23098 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23099 do \
23100 { \
23101 HEAD = TAIL = NULL; \
23102 while (START < END) \
23103 { \
23104 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23105 switch (first_glyph->type) \
23106 { \
23107 case CHAR_GLYPH: \
23108 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23109 HL, X, LAST_X); \
23110 break; \
23111 \
23112 case COMPOSITE_GLYPH: \
23113 if (first_glyph->u.cmp.automatic) \
23114 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23115 HL, X, LAST_X); \
23116 else \
23117 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23118 HL, X, LAST_X); \
23119 break; \
23120 \
23121 case STRETCH_GLYPH: \
23122 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23123 HL, X, LAST_X); \
23124 break; \
23125 \
23126 case IMAGE_GLYPH: \
23127 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23128 HL, X, LAST_X); \
23129 break; \
23130 \
23131 case GLYPHLESS_GLYPH: \
23132 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23133 HL, X, LAST_X); \
23134 break; \
23135 \
23136 default: \
23137 abort (); \
23138 } \
23139 \
23140 if (s) \
23141 { \
23142 set_glyph_string_background_width (s, START, LAST_X); \
23143 (X) += s->width; \
23144 } \
23145 } \
23146 } while (0)
23147
23148
23149 /* Draw glyphs between START and END in AREA of ROW on window W,
23150 starting at x-position X. X is relative to AREA in W. HL is a
23151 face-override with the following meaning:
23152
23153 DRAW_NORMAL_TEXT draw normally
23154 DRAW_CURSOR draw in cursor face
23155 DRAW_MOUSE_FACE draw in mouse face.
23156 DRAW_INVERSE_VIDEO draw in mode line face
23157 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23158 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23159
23160 If OVERLAPS is non-zero, draw only the foreground of characters and
23161 clip to the physical height of ROW. Non-zero value also defines
23162 the overlapping part to be drawn:
23163
23164 OVERLAPS_PRED overlap with preceding rows
23165 OVERLAPS_SUCC overlap with succeeding rows
23166 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23167 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23168
23169 Value is the x-position reached, relative to AREA of W. */
23170
23171 static int
23172 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23173 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23174 enum draw_glyphs_face hl, int overlaps)
23175 {
23176 struct glyph_string *head, *tail;
23177 struct glyph_string *s;
23178 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23179 int i, j, x_reached, last_x, area_left = 0;
23180 struct frame *f = XFRAME (WINDOW_FRAME (w));
23181 DECLARE_HDC (hdc);
23182
23183 ALLOCATE_HDC (hdc, f);
23184
23185 /* Let's rather be paranoid than getting a SEGV. */
23186 end = min (end, row->used[area]);
23187 start = max (0, start);
23188 start = min (end, start);
23189
23190 /* Translate X to frame coordinates. Set last_x to the right
23191 end of the drawing area. */
23192 if (row->full_width_p)
23193 {
23194 /* X is relative to the left edge of W, without scroll bars
23195 or fringes. */
23196 area_left = WINDOW_LEFT_EDGE_X (w);
23197 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23198 }
23199 else
23200 {
23201 area_left = window_box_left (w, area);
23202 last_x = area_left + window_box_width (w, area);
23203 }
23204 x += area_left;
23205
23206 /* Build a doubly-linked list of glyph_string structures between
23207 head and tail from what we have to draw. Note that the macro
23208 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23209 the reason we use a separate variable `i'. */
23210 i = start;
23211 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23212 if (tail)
23213 x_reached = tail->x + tail->background_width;
23214 else
23215 x_reached = x;
23216
23217 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23218 the row, redraw some glyphs in front or following the glyph
23219 strings built above. */
23220 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23221 {
23222 struct glyph_string *h, *t;
23223 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23224 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23225 int check_mouse_face = 0;
23226 int dummy_x = 0;
23227
23228 /* If mouse highlighting is on, we may need to draw adjacent
23229 glyphs using mouse-face highlighting. */
23230 if (area == TEXT_AREA && row->mouse_face_p)
23231 {
23232 struct glyph_row *mouse_beg_row, *mouse_end_row;
23233
23234 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23235 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23236
23237 if (row >= mouse_beg_row && row <= mouse_end_row)
23238 {
23239 check_mouse_face = 1;
23240 mouse_beg_col = (row == mouse_beg_row)
23241 ? hlinfo->mouse_face_beg_col : 0;
23242 mouse_end_col = (row == mouse_end_row)
23243 ? hlinfo->mouse_face_end_col
23244 : row->used[TEXT_AREA];
23245 }
23246 }
23247
23248 /* Compute overhangs for all glyph strings. */
23249 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23250 for (s = head; s; s = s->next)
23251 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23252
23253 /* Prepend glyph strings for glyphs in front of the first glyph
23254 string that are overwritten because of the first glyph
23255 string's left overhang. The background of all strings
23256 prepended must be drawn because the first glyph string
23257 draws over it. */
23258 i = left_overwritten (head);
23259 if (i >= 0)
23260 {
23261 enum draw_glyphs_face overlap_hl;
23262
23263 /* If this row contains mouse highlighting, attempt to draw
23264 the overlapped glyphs with the correct highlight. This
23265 code fails if the overlap encompasses more than one glyph
23266 and mouse-highlight spans only some of these glyphs.
23267 However, making it work perfectly involves a lot more
23268 code, and I don't know if the pathological case occurs in
23269 practice, so we'll stick to this for now. --- cyd */
23270 if (check_mouse_face
23271 && mouse_beg_col < start && mouse_end_col > i)
23272 overlap_hl = DRAW_MOUSE_FACE;
23273 else
23274 overlap_hl = DRAW_NORMAL_TEXT;
23275
23276 j = i;
23277 BUILD_GLYPH_STRINGS (j, start, h, t,
23278 overlap_hl, dummy_x, last_x);
23279 start = i;
23280 compute_overhangs_and_x (t, head->x, 1);
23281 prepend_glyph_string_lists (&head, &tail, h, t);
23282 clip_head = head;
23283 }
23284
23285 /* Prepend glyph strings for glyphs in front of the first glyph
23286 string that overwrite that glyph string because of their
23287 right overhang. For these strings, only the foreground must
23288 be drawn, because it draws over the glyph string at `head'.
23289 The background must not be drawn because this would overwrite
23290 right overhangs of preceding glyphs for which no glyph
23291 strings exist. */
23292 i = left_overwriting (head);
23293 if (i >= 0)
23294 {
23295 enum draw_glyphs_face overlap_hl;
23296
23297 if (check_mouse_face
23298 && mouse_beg_col < start && mouse_end_col > i)
23299 overlap_hl = DRAW_MOUSE_FACE;
23300 else
23301 overlap_hl = DRAW_NORMAL_TEXT;
23302
23303 clip_head = head;
23304 BUILD_GLYPH_STRINGS (i, start, h, t,
23305 overlap_hl, dummy_x, last_x);
23306 for (s = h; s; s = s->next)
23307 s->background_filled_p = 1;
23308 compute_overhangs_and_x (t, head->x, 1);
23309 prepend_glyph_string_lists (&head, &tail, h, t);
23310 }
23311
23312 /* Append glyphs strings for glyphs following the last glyph
23313 string tail that are overwritten by tail. The background of
23314 these strings has to be drawn because tail's foreground draws
23315 over it. */
23316 i = right_overwritten (tail);
23317 if (i >= 0)
23318 {
23319 enum draw_glyphs_face overlap_hl;
23320
23321 if (check_mouse_face
23322 && mouse_beg_col < i && mouse_end_col > end)
23323 overlap_hl = DRAW_MOUSE_FACE;
23324 else
23325 overlap_hl = DRAW_NORMAL_TEXT;
23326
23327 BUILD_GLYPH_STRINGS (end, i, h, t,
23328 overlap_hl, x, last_x);
23329 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23330 we don't have `end = i;' here. */
23331 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23332 append_glyph_string_lists (&head, &tail, h, t);
23333 clip_tail = tail;
23334 }
23335
23336 /* Append glyph strings for glyphs following the last glyph
23337 string tail that overwrite tail. The foreground of such
23338 glyphs has to be drawn because it writes into the background
23339 of tail. The background must not be drawn because it could
23340 paint over the foreground of following glyphs. */
23341 i = right_overwriting (tail);
23342 if (i >= 0)
23343 {
23344 enum draw_glyphs_face overlap_hl;
23345 if (check_mouse_face
23346 && mouse_beg_col < i && mouse_end_col > end)
23347 overlap_hl = DRAW_MOUSE_FACE;
23348 else
23349 overlap_hl = DRAW_NORMAL_TEXT;
23350
23351 clip_tail = tail;
23352 i++; /* We must include the Ith glyph. */
23353 BUILD_GLYPH_STRINGS (end, i, h, t,
23354 overlap_hl, x, last_x);
23355 for (s = h; s; s = s->next)
23356 s->background_filled_p = 1;
23357 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23358 append_glyph_string_lists (&head, &tail, h, t);
23359 }
23360 if (clip_head || clip_tail)
23361 for (s = head; s; s = s->next)
23362 {
23363 s->clip_head = clip_head;
23364 s->clip_tail = clip_tail;
23365 }
23366 }
23367
23368 /* Draw all strings. */
23369 for (s = head; s; s = s->next)
23370 FRAME_RIF (f)->draw_glyph_string (s);
23371
23372 #ifndef HAVE_NS
23373 /* When focus a sole frame and move horizontally, this sets on_p to 0
23374 causing a failure to erase prev cursor position. */
23375 if (area == TEXT_AREA
23376 && !row->full_width_p
23377 /* When drawing overlapping rows, only the glyph strings'
23378 foreground is drawn, which doesn't erase a cursor
23379 completely. */
23380 && !overlaps)
23381 {
23382 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23383 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23384 : (tail ? tail->x + tail->background_width : x));
23385 x0 -= area_left;
23386 x1 -= area_left;
23387
23388 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23389 row->y, MATRIX_ROW_BOTTOM_Y (row));
23390 }
23391 #endif
23392
23393 /* Value is the x-position up to which drawn, relative to AREA of W.
23394 This doesn't include parts drawn because of overhangs. */
23395 if (row->full_width_p)
23396 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23397 else
23398 x_reached -= area_left;
23399
23400 RELEASE_HDC (hdc, f);
23401
23402 return x_reached;
23403 }
23404
23405 /* Expand row matrix if too narrow. Don't expand if area
23406 is not present. */
23407
23408 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23409 { \
23410 if (!fonts_changed_p \
23411 && (it->glyph_row->glyphs[area] \
23412 < it->glyph_row->glyphs[area + 1])) \
23413 { \
23414 it->w->ncols_scale_factor++; \
23415 fonts_changed_p = 1; \
23416 } \
23417 }
23418
23419 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23420 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23421
23422 static inline void
23423 append_glyph (struct it *it)
23424 {
23425 struct glyph *glyph;
23426 enum glyph_row_area area = it->area;
23427
23428 xassert (it->glyph_row);
23429 xassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23430
23431 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23432 if (glyph < it->glyph_row->glyphs[area + 1])
23433 {
23434 /* If the glyph row is reversed, we need to prepend the glyph
23435 rather than append it. */
23436 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23437 {
23438 struct glyph *g;
23439
23440 /* Make room for the additional glyph. */
23441 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23442 g[1] = *g;
23443 glyph = it->glyph_row->glyphs[area];
23444 }
23445 glyph->charpos = CHARPOS (it->position);
23446 glyph->object = it->object;
23447 if (it->pixel_width > 0)
23448 {
23449 glyph->pixel_width = it->pixel_width;
23450 glyph->padding_p = 0;
23451 }
23452 else
23453 {
23454 /* Assure at least 1-pixel width. Otherwise, cursor can't
23455 be displayed correctly. */
23456 glyph->pixel_width = 1;
23457 glyph->padding_p = 1;
23458 }
23459 glyph->ascent = it->ascent;
23460 glyph->descent = it->descent;
23461 glyph->voffset = it->voffset;
23462 glyph->type = CHAR_GLYPH;
23463 glyph->avoid_cursor_p = it->avoid_cursor_p;
23464 glyph->multibyte_p = it->multibyte_p;
23465 glyph->left_box_line_p = it->start_of_box_run_p;
23466 glyph->right_box_line_p = it->end_of_box_run_p;
23467 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23468 || it->phys_descent > it->descent);
23469 glyph->glyph_not_available_p = it->glyph_not_available_p;
23470 glyph->face_id = it->face_id;
23471 glyph->u.ch = it->char_to_display;
23472 glyph->slice.img = null_glyph_slice;
23473 glyph->font_type = FONT_TYPE_UNKNOWN;
23474 if (it->bidi_p)
23475 {
23476 glyph->resolved_level = it->bidi_it.resolved_level;
23477 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23478 abort ();
23479 glyph->bidi_type = it->bidi_it.type;
23480 }
23481 else
23482 {
23483 glyph->resolved_level = 0;
23484 glyph->bidi_type = UNKNOWN_BT;
23485 }
23486 ++it->glyph_row->used[area];
23487 }
23488 else
23489 IT_EXPAND_MATRIX_WIDTH (it, area);
23490 }
23491
23492 /* Store one glyph for the composition IT->cmp_it.id in
23493 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23494 non-null. */
23495
23496 static inline void
23497 append_composite_glyph (struct it *it)
23498 {
23499 struct glyph *glyph;
23500 enum glyph_row_area area = it->area;
23501
23502 xassert (it->glyph_row);
23503
23504 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23505 if (glyph < it->glyph_row->glyphs[area + 1])
23506 {
23507 /* If the glyph row is reversed, we need to prepend the glyph
23508 rather than append it. */
23509 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23510 {
23511 struct glyph *g;
23512
23513 /* Make room for the new glyph. */
23514 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23515 g[1] = *g;
23516 glyph = it->glyph_row->glyphs[it->area];
23517 }
23518 glyph->charpos = it->cmp_it.charpos;
23519 glyph->object = it->object;
23520 glyph->pixel_width = it->pixel_width;
23521 glyph->ascent = it->ascent;
23522 glyph->descent = it->descent;
23523 glyph->voffset = it->voffset;
23524 glyph->type = COMPOSITE_GLYPH;
23525 if (it->cmp_it.ch < 0)
23526 {
23527 glyph->u.cmp.automatic = 0;
23528 glyph->u.cmp.id = it->cmp_it.id;
23529 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23530 }
23531 else
23532 {
23533 glyph->u.cmp.automatic = 1;
23534 glyph->u.cmp.id = it->cmp_it.id;
23535 glyph->slice.cmp.from = it->cmp_it.from;
23536 glyph->slice.cmp.to = it->cmp_it.to - 1;
23537 }
23538 glyph->avoid_cursor_p = it->avoid_cursor_p;
23539 glyph->multibyte_p = it->multibyte_p;
23540 glyph->left_box_line_p = it->start_of_box_run_p;
23541 glyph->right_box_line_p = it->end_of_box_run_p;
23542 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23543 || it->phys_descent > it->descent);
23544 glyph->padding_p = 0;
23545 glyph->glyph_not_available_p = 0;
23546 glyph->face_id = it->face_id;
23547 glyph->font_type = FONT_TYPE_UNKNOWN;
23548 if (it->bidi_p)
23549 {
23550 glyph->resolved_level = it->bidi_it.resolved_level;
23551 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23552 abort ();
23553 glyph->bidi_type = it->bidi_it.type;
23554 }
23555 ++it->glyph_row->used[area];
23556 }
23557 else
23558 IT_EXPAND_MATRIX_WIDTH (it, area);
23559 }
23560
23561
23562 /* Change IT->ascent and IT->height according to the setting of
23563 IT->voffset. */
23564
23565 static inline void
23566 take_vertical_position_into_account (struct it *it)
23567 {
23568 if (it->voffset)
23569 {
23570 if (it->voffset < 0)
23571 /* Increase the ascent so that we can display the text higher
23572 in the line. */
23573 it->ascent -= it->voffset;
23574 else
23575 /* Increase the descent so that we can display the text lower
23576 in the line. */
23577 it->descent += it->voffset;
23578 }
23579 }
23580
23581
23582 /* Produce glyphs/get display metrics for the image IT is loaded with.
23583 See the description of struct display_iterator in dispextern.h for
23584 an overview of struct display_iterator. */
23585
23586 static void
23587 produce_image_glyph (struct it *it)
23588 {
23589 struct image *img;
23590 struct face *face;
23591 int glyph_ascent, crop;
23592 struct glyph_slice slice;
23593
23594 xassert (it->what == IT_IMAGE);
23595
23596 face = FACE_FROM_ID (it->f, it->face_id);
23597 xassert (face);
23598 /* Make sure X resources of the face is loaded. */
23599 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23600
23601 if (it->image_id < 0)
23602 {
23603 /* Fringe bitmap. */
23604 it->ascent = it->phys_ascent = 0;
23605 it->descent = it->phys_descent = 0;
23606 it->pixel_width = 0;
23607 it->nglyphs = 0;
23608 return;
23609 }
23610
23611 img = IMAGE_FROM_ID (it->f, it->image_id);
23612 xassert (img);
23613 /* Make sure X resources of the image is loaded. */
23614 prepare_image_for_display (it->f, img);
23615
23616 slice.x = slice.y = 0;
23617 slice.width = img->width;
23618 slice.height = img->height;
23619
23620 if (INTEGERP (it->slice.x))
23621 slice.x = XINT (it->slice.x);
23622 else if (FLOATP (it->slice.x))
23623 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23624
23625 if (INTEGERP (it->slice.y))
23626 slice.y = XINT (it->slice.y);
23627 else if (FLOATP (it->slice.y))
23628 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23629
23630 if (INTEGERP (it->slice.width))
23631 slice.width = XINT (it->slice.width);
23632 else if (FLOATP (it->slice.width))
23633 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23634
23635 if (INTEGERP (it->slice.height))
23636 slice.height = XINT (it->slice.height);
23637 else if (FLOATP (it->slice.height))
23638 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23639
23640 if (slice.x >= img->width)
23641 slice.x = img->width;
23642 if (slice.y >= img->height)
23643 slice.y = img->height;
23644 if (slice.x + slice.width >= img->width)
23645 slice.width = img->width - slice.x;
23646 if (slice.y + slice.height > img->height)
23647 slice.height = img->height - slice.y;
23648
23649 if (slice.width == 0 || slice.height == 0)
23650 return;
23651
23652 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23653
23654 it->descent = slice.height - glyph_ascent;
23655 if (slice.y == 0)
23656 it->descent += img->vmargin;
23657 if (slice.y + slice.height == img->height)
23658 it->descent += img->vmargin;
23659 it->phys_descent = it->descent;
23660
23661 it->pixel_width = slice.width;
23662 if (slice.x == 0)
23663 it->pixel_width += img->hmargin;
23664 if (slice.x + slice.width == img->width)
23665 it->pixel_width += img->hmargin;
23666
23667 /* It's quite possible for images to have an ascent greater than
23668 their height, so don't get confused in that case. */
23669 if (it->descent < 0)
23670 it->descent = 0;
23671
23672 it->nglyphs = 1;
23673
23674 if (face->box != FACE_NO_BOX)
23675 {
23676 if (face->box_line_width > 0)
23677 {
23678 if (slice.y == 0)
23679 it->ascent += face->box_line_width;
23680 if (slice.y + slice.height == img->height)
23681 it->descent += face->box_line_width;
23682 }
23683
23684 if (it->start_of_box_run_p && slice.x == 0)
23685 it->pixel_width += eabs (face->box_line_width);
23686 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23687 it->pixel_width += eabs (face->box_line_width);
23688 }
23689
23690 take_vertical_position_into_account (it);
23691
23692 /* Automatically crop wide image glyphs at right edge so we can
23693 draw the cursor on same display row. */
23694 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23695 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23696 {
23697 it->pixel_width -= crop;
23698 slice.width -= crop;
23699 }
23700
23701 if (it->glyph_row)
23702 {
23703 struct glyph *glyph;
23704 enum glyph_row_area area = it->area;
23705
23706 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23707 if (glyph < it->glyph_row->glyphs[area + 1])
23708 {
23709 glyph->charpos = CHARPOS (it->position);
23710 glyph->object = it->object;
23711 glyph->pixel_width = it->pixel_width;
23712 glyph->ascent = glyph_ascent;
23713 glyph->descent = it->descent;
23714 glyph->voffset = it->voffset;
23715 glyph->type = IMAGE_GLYPH;
23716 glyph->avoid_cursor_p = it->avoid_cursor_p;
23717 glyph->multibyte_p = it->multibyte_p;
23718 glyph->left_box_line_p = it->start_of_box_run_p;
23719 glyph->right_box_line_p = it->end_of_box_run_p;
23720 glyph->overlaps_vertically_p = 0;
23721 glyph->padding_p = 0;
23722 glyph->glyph_not_available_p = 0;
23723 glyph->face_id = it->face_id;
23724 glyph->u.img_id = img->id;
23725 glyph->slice.img = slice;
23726 glyph->font_type = FONT_TYPE_UNKNOWN;
23727 if (it->bidi_p)
23728 {
23729 glyph->resolved_level = it->bidi_it.resolved_level;
23730 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23731 abort ();
23732 glyph->bidi_type = it->bidi_it.type;
23733 }
23734 ++it->glyph_row->used[area];
23735 }
23736 else
23737 IT_EXPAND_MATRIX_WIDTH (it, area);
23738 }
23739 }
23740
23741
23742 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23743 of the glyph, WIDTH and HEIGHT are the width and height of the
23744 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23745
23746 static void
23747 append_stretch_glyph (struct it *it, Lisp_Object object,
23748 int width, int height, int ascent)
23749 {
23750 struct glyph *glyph;
23751 enum glyph_row_area area = it->area;
23752
23753 xassert (ascent >= 0 && ascent <= height);
23754
23755 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23756 if (glyph < it->glyph_row->glyphs[area + 1])
23757 {
23758 /* If the glyph row is reversed, we need to prepend the glyph
23759 rather than append it. */
23760 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23761 {
23762 struct glyph *g;
23763
23764 /* Make room for the additional glyph. */
23765 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23766 g[1] = *g;
23767 glyph = it->glyph_row->glyphs[area];
23768 }
23769 glyph->charpos = CHARPOS (it->position);
23770 glyph->object = object;
23771 glyph->pixel_width = width;
23772 glyph->ascent = ascent;
23773 glyph->descent = height - ascent;
23774 glyph->voffset = it->voffset;
23775 glyph->type = STRETCH_GLYPH;
23776 glyph->avoid_cursor_p = it->avoid_cursor_p;
23777 glyph->multibyte_p = it->multibyte_p;
23778 glyph->left_box_line_p = it->start_of_box_run_p;
23779 glyph->right_box_line_p = it->end_of_box_run_p;
23780 glyph->overlaps_vertically_p = 0;
23781 glyph->padding_p = 0;
23782 glyph->glyph_not_available_p = 0;
23783 glyph->face_id = it->face_id;
23784 glyph->u.stretch.ascent = ascent;
23785 glyph->u.stretch.height = height;
23786 glyph->slice.img = null_glyph_slice;
23787 glyph->font_type = FONT_TYPE_UNKNOWN;
23788 if (it->bidi_p)
23789 {
23790 glyph->resolved_level = it->bidi_it.resolved_level;
23791 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23792 abort ();
23793 glyph->bidi_type = it->bidi_it.type;
23794 }
23795 else
23796 {
23797 glyph->resolved_level = 0;
23798 glyph->bidi_type = UNKNOWN_BT;
23799 }
23800 ++it->glyph_row->used[area];
23801 }
23802 else
23803 IT_EXPAND_MATRIX_WIDTH (it, area);
23804 }
23805
23806 #endif /* HAVE_WINDOW_SYSTEM */
23807
23808 /* Produce a stretch glyph for iterator IT. IT->object is the value
23809 of the glyph property displayed. The value must be a list
23810 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23811 being recognized:
23812
23813 1. `:width WIDTH' specifies that the space should be WIDTH *
23814 canonical char width wide. WIDTH may be an integer or floating
23815 point number.
23816
23817 2. `:relative-width FACTOR' specifies that the width of the stretch
23818 should be computed from the width of the first character having the
23819 `glyph' property, and should be FACTOR times that width.
23820
23821 3. `:align-to HPOS' specifies that the space should be wide enough
23822 to reach HPOS, a value in canonical character units.
23823
23824 Exactly one of the above pairs must be present.
23825
23826 4. `:height HEIGHT' specifies that the height of the stretch produced
23827 should be HEIGHT, measured in canonical character units.
23828
23829 5. `:relative-height FACTOR' specifies that the height of the
23830 stretch should be FACTOR times the height of the characters having
23831 the glyph property.
23832
23833 Either none or exactly one of 4 or 5 must be present.
23834
23835 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23836 of the stretch should be used for the ascent of the stretch.
23837 ASCENT must be in the range 0 <= ASCENT <= 100. */
23838
23839 void
23840 produce_stretch_glyph (struct it *it)
23841 {
23842 /* (space :width WIDTH :height HEIGHT ...) */
23843 Lisp_Object prop, plist;
23844 int width = 0, height = 0, align_to = -1;
23845 int zero_width_ok_p = 0;
23846 int ascent = 0;
23847 double tem;
23848 struct face *face = NULL;
23849 struct font *font = NULL;
23850
23851 #ifdef HAVE_WINDOW_SYSTEM
23852 int zero_height_ok_p = 0;
23853
23854 if (FRAME_WINDOW_P (it->f))
23855 {
23856 face = FACE_FROM_ID (it->f, it->face_id);
23857 font = face->font ? face->font : FRAME_FONT (it->f);
23858 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23859 }
23860 #endif
23861
23862 /* List should start with `space'. */
23863 xassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23864 plist = XCDR (it->object);
23865
23866 /* Compute the width of the stretch. */
23867 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23868 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23869 {
23870 /* Absolute width `:width WIDTH' specified and valid. */
23871 zero_width_ok_p = 1;
23872 width = (int)tem;
23873 }
23874 #ifdef HAVE_WINDOW_SYSTEM
23875 else if (FRAME_WINDOW_P (it->f)
23876 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
23877 {
23878 /* Relative width `:relative-width FACTOR' specified and valid.
23879 Compute the width of the characters having the `glyph'
23880 property. */
23881 struct it it2;
23882 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
23883
23884 it2 = *it;
23885 if (it->multibyte_p)
23886 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
23887 else
23888 {
23889 it2.c = it2.char_to_display = *p, it2.len = 1;
23890 if (! ASCII_CHAR_P (it2.c))
23891 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
23892 }
23893
23894 it2.glyph_row = NULL;
23895 it2.what = IT_CHARACTER;
23896 x_produce_glyphs (&it2);
23897 width = NUMVAL (prop) * it2.pixel_width;
23898 }
23899 #endif /* HAVE_WINDOW_SYSTEM */
23900 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
23901 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
23902 {
23903 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
23904 align_to = (align_to < 0
23905 ? 0
23906 : align_to - window_box_left_offset (it->w, TEXT_AREA));
23907 else if (align_to < 0)
23908 align_to = window_box_left_offset (it->w, TEXT_AREA);
23909 width = max (0, (int)tem + align_to - it->current_x);
23910 zero_width_ok_p = 1;
23911 }
23912 else
23913 /* Nothing specified -> width defaults to canonical char width. */
23914 width = FRAME_COLUMN_WIDTH (it->f);
23915
23916 if (width <= 0 && (width < 0 || !zero_width_ok_p))
23917 width = 1;
23918
23919 #ifdef HAVE_WINDOW_SYSTEM
23920 /* Compute height. */
23921 if (FRAME_WINDOW_P (it->f))
23922 {
23923 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
23924 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23925 {
23926 height = (int)tem;
23927 zero_height_ok_p = 1;
23928 }
23929 else if (prop = Fplist_get (plist, QCrelative_height),
23930 NUMVAL (prop) > 0)
23931 height = FONT_HEIGHT (font) * NUMVAL (prop);
23932 else
23933 height = FONT_HEIGHT (font);
23934
23935 if (height <= 0 && (height < 0 || !zero_height_ok_p))
23936 height = 1;
23937
23938 /* Compute percentage of height used for ascent. If
23939 `:ascent ASCENT' is present and valid, use that. Otherwise,
23940 derive the ascent from the font in use. */
23941 if (prop = Fplist_get (plist, QCascent),
23942 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
23943 ascent = height * NUMVAL (prop) / 100.0;
23944 else if (!NILP (prop)
23945 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
23946 ascent = min (max (0, (int)tem), height);
23947 else
23948 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
23949 }
23950 else
23951 #endif /* HAVE_WINDOW_SYSTEM */
23952 height = 1;
23953
23954 if (width > 0 && it->line_wrap != TRUNCATE
23955 && it->current_x + width > it->last_visible_x)
23956 {
23957 width = it->last_visible_x - it->current_x;
23958 #ifdef HAVE_WINDOW_SYSTEM
23959 /* Subtract one more pixel from the stretch width, but only on
23960 GUI frames, since on a TTY each glyph is one "pixel" wide. */
23961 width -= FRAME_WINDOW_P (it->f);
23962 #endif
23963 }
23964
23965 if (width > 0 && height > 0 && it->glyph_row)
23966 {
23967 Lisp_Object o_object = it->object;
23968 Lisp_Object object = it->stack[it->sp - 1].string;
23969 int n = width;
23970
23971 if (!STRINGP (object))
23972 object = it->w->buffer;
23973 #ifdef HAVE_WINDOW_SYSTEM
23974 if (FRAME_WINDOW_P (it->f))
23975 append_stretch_glyph (it, object, width, height, ascent);
23976 else
23977 #endif
23978 {
23979 it->object = object;
23980 it->char_to_display = ' ';
23981 it->pixel_width = it->len = 1;
23982 while (n--)
23983 tty_append_glyph (it);
23984 it->object = o_object;
23985 }
23986 }
23987
23988 it->pixel_width = width;
23989 #ifdef HAVE_WINDOW_SYSTEM
23990 if (FRAME_WINDOW_P (it->f))
23991 {
23992 it->ascent = it->phys_ascent = ascent;
23993 it->descent = it->phys_descent = height - it->ascent;
23994 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
23995 take_vertical_position_into_account (it);
23996 }
23997 else
23998 #endif
23999 it->nglyphs = width;
24000 }
24001
24002 #ifdef HAVE_WINDOW_SYSTEM
24003
24004 /* Calculate line-height and line-spacing properties.
24005 An integer value specifies explicit pixel value.
24006 A float value specifies relative value to current face height.
24007 A cons (float . face-name) specifies relative value to
24008 height of specified face font.
24009
24010 Returns height in pixels, or nil. */
24011
24012
24013 static Lisp_Object
24014 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24015 int boff, int override)
24016 {
24017 Lisp_Object face_name = Qnil;
24018 int ascent, descent, height;
24019
24020 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24021 return val;
24022
24023 if (CONSP (val))
24024 {
24025 face_name = XCAR (val);
24026 val = XCDR (val);
24027 if (!NUMBERP (val))
24028 val = make_number (1);
24029 if (NILP (face_name))
24030 {
24031 height = it->ascent + it->descent;
24032 goto scale;
24033 }
24034 }
24035
24036 if (NILP (face_name))
24037 {
24038 font = FRAME_FONT (it->f);
24039 boff = FRAME_BASELINE_OFFSET (it->f);
24040 }
24041 else if (EQ (face_name, Qt))
24042 {
24043 override = 0;
24044 }
24045 else
24046 {
24047 int face_id;
24048 struct face *face;
24049
24050 face_id = lookup_named_face (it->f, face_name, 0);
24051 if (face_id < 0)
24052 return make_number (-1);
24053
24054 face = FACE_FROM_ID (it->f, face_id);
24055 font = face->font;
24056 if (font == NULL)
24057 return make_number (-1);
24058 boff = font->baseline_offset;
24059 if (font->vertical_centering)
24060 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24061 }
24062
24063 ascent = FONT_BASE (font) + boff;
24064 descent = FONT_DESCENT (font) - boff;
24065
24066 if (override)
24067 {
24068 it->override_ascent = ascent;
24069 it->override_descent = descent;
24070 it->override_boff = boff;
24071 }
24072
24073 height = ascent + descent;
24074
24075 scale:
24076 if (FLOATP (val))
24077 height = (int)(XFLOAT_DATA (val) * height);
24078 else if (INTEGERP (val))
24079 height *= XINT (val);
24080
24081 return make_number (height);
24082 }
24083
24084
24085 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24086 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24087 and only if this is for a character for which no font was found.
24088
24089 If the display method (it->glyphless_method) is
24090 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24091 length of the acronym or the hexadecimal string, UPPER_XOFF and
24092 UPPER_YOFF are pixel offsets for the upper part of the string,
24093 LOWER_XOFF and LOWER_YOFF are for the lower part.
24094
24095 For the other display methods, LEN through LOWER_YOFF are zero. */
24096
24097 static void
24098 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24099 short upper_xoff, short upper_yoff,
24100 short lower_xoff, short lower_yoff)
24101 {
24102 struct glyph *glyph;
24103 enum glyph_row_area area = it->area;
24104
24105 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24106 if (glyph < it->glyph_row->glyphs[area + 1])
24107 {
24108 /* If the glyph row is reversed, we need to prepend the glyph
24109 rather than append it. */
24110 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24111 {
24112 struct glyph *g;
24113
24114 /* Make room for the additional glyph. */
24115 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24116 g[1] = *g;
24117 glyph = it->glyph_row->glyphs[area];
24118 }
24119 glyph->charpos = CHARPOS (it->position);
24120 glyph->object = it->object;
24121 glyph->pixel_width = it->pixel_width;
24122 glyph->ascent = it->ascent;
24123 glyph->descent = it->descent;
24124 glyph->voffset = it->voffset;
24125 glyph->type = GLYPHLESS_GLYPH;
24126 glyph->u.glyphless.method = it->glyphless_method;
24127 glyph->u.glyphless.for_no_font = for_no_font;
24128 glyph->u.glyphless.len = len;
24129 glyph->u.glyphless.ch = it->c;
24130 glyph->slice.glyphless.upper_xoff = upper_xoff;
24131 glyph->slice.glyphless.upper_yoff = upper_yoff;
24132 glyph->slice.glyphless.lower_xoff = lower_xoff;
24133 glyph->slice.glyphless.lower_yoff = lower_yoff;
24134 glyph->avoid_cursor_p = it->avoid_cursor_p;
24135 glyph->multibyte_p = it->multibyte_p;
24136 glyph->left_box_line_p = it->start_of_box_run_p;
24137 glyph->right_box_line_p = it->end_of_box_run_p;
24138 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24139 || it->phys_descent > it->descent);
24140 glyph->padding_p = 0;
24141 glyph->glyph_not_available_p = 0;
24142 glyph->face_id = face_id;
24143 glyph->font_type = FONT_TYPE_UNKNOWN;
24144 if (it->bidi_p)
24145 {
24146 glyph->resolved_level = it->bidi_it.resolved_level;
24147 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24148 abort ();
24149 glyph->bidi_type = it->bidi_it.type;
24150 }
24151 ++it->glyph_row->used[area];
24152 }
24153 else
24154 IT_EXPAND_MATRIX_WIDTH (it, area);
24155 }
24156
24157
24158 /* Produce a glyph for a glyphless character for iterator IT.
24159 IT->glyphless_method specifies which method to use for displaying
24160 the character. See the description of enum
24161 glyphless_display_method in dispextern.h for the detail.
24162
24163 FOR_NO_FONT is nonzero if and only if this is for a character for
24164 which no font was found. ACRONYM, if non-nil, is an acronym string
24165 for the character. */
24166
24167 static void
24168 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24169 {
24170 int face_id;
24171 struct face *face;
24172 struct font *font;
24173 int base_width, base_height, width, height;
24174 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24175 int len;
24176
24177 /* Get the metrics of the base font. We always refer to the current
24178 ASCII face. */
24179 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24180 font = face->font ? face->font : FRAME_FONT (it->f);
24181 it->ascent = FONT_BASE (font) + font->baseline_offset;
24182 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24183 base_height = it->ascent + it->descent;
24184 base_width = font->average_width;
24185
24186 /* Get a face ID for the glyph by utilizing a cache (the same way as
24187 done for `escape-glyph' in get_next_display_element). */
24188 if (it->f == last_glyphless_glyph_frame
24189 && it->face_id == last_glyphless_glyph_face_id)
24190 {
24191 face_id = last_glyphless_glyph_merged_face_id;
24192 }
24193 else
24194 {
24195 /* Merge the `glyphless-char' face into the current face. */
24196 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24197 last_glyphless_glyph_frame = it->f;
24198 last_glyphless_glyph_face_id = it->face_id;
24199 last_glyphless_glyph_merged_face_id = face_id;
24200 }
24201
24202 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24203 {
24204 it->pixel_width = THIN_SPACE_WIDTH;
24205 len = 0;
24206 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24207 }
24208 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24209 {
24210 width = CHAR_WIDTH (it->c);
24211 if (width == 0)
24212 width = 1;
24213 else if (width > 4)
24214 width = 4;
24215 it->pixel_width = base_width * width;
24216 len = 0;
24217 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24218 }
24219 else
24220 {
24221 char buf[7];
24222 const char *str;
24223 unsigned int code[6];
24224 int upper_len;
24225 int ascent, descent;
24226 struct font_metrics metrics_upper, metrics_lower;
24227
24228 face = FACE_FROM_ID (it->f, face_id);
24229 font = face->font ? face->font : FRAME_FONT (it->f);
24230 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24231
24232 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24233 {
24234 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24235 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24236 if (CONSP (acronym))
24237 acronym = XCAR (acronym);
24238 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24239 }
24240 else
24241 {
24242 xassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24243 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24244 str = buf;
24245 }
24246 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24247 code[len] = font->driver->encode_char (font, str[len]);
24248 upper_len = (len + 1) / 2;
24249 font->driver->text_extents (font, code, upper_len,
24250 &metrics_upper);
24251 font->driver->text_extents (font, code + upper_len, len - upper_len,
24252 &metrics_lower);
24253
24254
24255
24256 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24257 width = max (metrics_upper.width, metrics_lower.width) + 4;
24258 upper_xoff = upper_yoff = 2; /* the typical case */
24259 if (base_width >= width)
24260 {
24261 /* Align the upper to the left, the lower to the right. */
24262 it->pixel_width = base_width;
24263 lower_xoff = base_width - 2 - metrics_lower.width;
24264 }
24265 else
24266 {
24267 /* Center the shorter one. */
24268 it->pixel_width = width;
24269 if (metrics_upper.width >= metrics_lower.width)
24270 lower_xoff = (width - metrics_lower.width) / 2;
24271 else
24272 {
24273 /* FIXME: This code doesn't look right. It formerly was
24274 missing the "lower_xoff = 0;", which couldn't have
24275 been right since it left lower_xoff uninitialized. */
24276 lower_xoff = 0;
24277 upper_xoff = (width - metrics_upper.width) / 2;
24278 }
24279 }
24280
24281 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24282 top, bottom, and between upper and lower strings. */
24283 height = (metrics_upper.ascent + metrics_upper.descent
24284 + metrics_lower.ascent + metrics_lower.descent) + 5;
24285 /* Center vertically.
24286 H:base_height, D:base_descent
24287 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24288
24289 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24290 descent = D - H/2 + h/2;
24291 lower_yoff = descent - 2 - ld;
24292 upper_yoff = lower_yoff - la - 1 - ud; */
24293 ascent = - (it->descent - (base_height + height + 1) / 2);
24294 descent = it->descent - (base_height - height) / 2;
24295 lower_yoff = descent - 2 - metrics_lower.descent;
24296 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24297 - metrics_upper.descent);
24298 /* Don't make the height shorter than the base height. */
24299 if (height > base_height)
24300 {
24301 it->ascent = ascent;
24302 it->descent = descent;
24303 }
24304 }
24305
24306 it->phys_ascent = it->ascent;
24307 it->phys_descent = it->descent;
24308 if (it->glyph_row)
24309 append_glyphless_glyph (it, face_id, for_no_font, len,
24310 upper_xoff, upper_yoff,
24311 lower_xoff, lower_yoff);
24312 it->nglyphs = 1;
24313 take_vertical_position_into_account (it);
24314 }
24315
24316
24317 /* RIF:
24318 Produce glyphs/get display metrics for the display element IT is
24319 loaded with. See the description of struct it in dispextern.h
24320 for an overview of struct it. */
24321
24322 void
24323 x_produce_glyphs (struct it *it)
24324 {
24325 int extra_line_spacing = it->extra_line_spacing;
24326
24327 it->glyph_not_available_p = 0;
24328
24329 if (it->what == IT_CHARACTER)
24330 {
24331 XChar2b char2b;
24332 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24333 struct font *font = face->font;
24334 struct font_metrics *pcm = NULL;
24335 int boff; /* baseline offset */
24336
24337 if (font == NULL)
24338 {
24339 /* When no suitable font is found, display this character by
24340 the method specified in the first extra slot of
24341 Vglyphless_char_display. */
24342 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24343
24344 xassert (it->what == IT_GLYPHLESS);
24345 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24346 goto done;
24347 }
24348
24349 boff = font->baseline_offset;
24350 if (font->vertical_centering)
24351 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24352
24353 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24354 {
24355 int stretched_p;
24356
24357 it->nglyphs = 1;
24358
24359 if (it->override_ascent >= 0)
24360 {
24361 it->ascent = it->override_ascent;
24362 it->descent = it->override_descent;
24363 boff = it->override_boff;
24364 }
24365 else
24366 {
24367 it->ascent = FONT_BASE (font) + boff;
24368 it->descent = FONT_DESCENT (font) - boff;
24369 }
24370
24371 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24372 {
24373 pcm = get_per_char_metric (font, &char2b);
24374 if (pcm->width == 0
24375 && pcm->rbearing == 0 && pcm->lbearing == 0)
24376 pcm = NULL;
24377 }
24378
24379 if (pcm)
24380 {
24381 it->phys_ascent = pcm->ascent + boff;
24382 it->phys_descent = pcm->descent - boff;
24383 it->pixel_width = pcm->width;
24384 }
24385 else
24386 {
24387 it->glyph_not_available_p = 1;
24388 it->phys_ascent = it->ascent;
24389 it->phys_descent = it->descent;
24390 it->pixel_width = font->space_width;
24391 }
24392
24393 if (it->constrain_row_ascent_descent_p)
24394 {
24395 if (it->descent > it->max_descent)
24396 {
24397 it->ascent += it->descent - it->max_descent;
24398 it->descent = it->max_descent;
24399 }
24400 if (it->ascent > it->max_ascent)
24401 {
24402 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24403 it->ascent = it->max_ascent;
24404 }
24405 it->phys_ascent = min (it->phys_ascent, it->ascent);
24406 it->phys_descent = min (it->phys_descent, it->descent);
24407 extra_line_spacing = 0;
24408 }
24409
24410 /* If this is a space inside a region of text with
24411 `space-width' property, change its width. */
24412 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24413 if (stretched_p)
24414 it->pixel_width *= XFLOATINT (it->space_width);
24415
24416 /* If face has a box, add the box thickness to the character
24417 height. If character has a box line to the left and/or
24418 right, add the box line width to the character's width. */
24419 if (face->box != FACE_NO_BOX)
24420 {
24421 int thick = face->box_line_width;
24422
24423 if (thick > 0)
24424 {
24425 it->ascent += thick;
24426 it->descent += thick;
24427 }
24428 else
24429 thick = -thick;
24430
24431 if (it->start_of_box_run_p)
24432 it->pixel_width += thick;
24433 if (it->end_of_box_run_p)
24434 it->pixel_width += thick;
24435 }
24436
24437 /* If face has an overline, add the height of the overline
24438 (1 pixel) and a 1 pixel margin to the character height. */
24439 if (face->overline_p)
24440 it->ascent += overline_margin;
24441
24442 if (it->constrain_row_ascent_descent_p)
24443 {
24444 if (it->ascent > it->max_ascent)
24445 it->ascent = it->max_ascent;
24446 if (it->descent > it->max_descent)
24447 it->descent = it->max_descent;
24448 }
24449
24450 take_vertical_position_into_account (it);
24451
24452 /* If we have to actually produce glyphs, do it. */
24453 if (it->glyph_row)
24454 {
24455 if (stretched_p)
24456 {
24457 /* Translate a space with a `space-width' property
24458 into a stretch glyph. */
24459 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24460 / FONT_HEIGHT (font));
24461 append_stretch_glyph (it, it->object, it->pixel_width,
24462 it->ascent + it->descent, ascent);
24463 }
24464 else
24465 append_glyph (it);
24466
24467 /* If characters with lbearing or rbearing are displayed
24468 in this line, record that fact in a flag of the
24469 glyph row. This is used to optimize X output code. */
24470 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24471 it->glyph_row->contains_overlapping_glyphs_p = 1;
24472 }
24473 if (! stretched_p && it->pixel_width == 0)
24474 /* We assure that all visible glyphs have at least 1-pixel
24475 width. */
24476 it->pixel_width = 1;
24477 }
24478 else if (it->char_to_display == '\n')
24479 {
24480 /* A newline has no width, but we need the height of the
24481 line. But if previous part of the line sets a height,
24482 don't increase that height */
24483
24484 Lisp_Object height;
24485 Lisp_Object total_height = Qnil;
24486
24487 it->override_ascent = -1;
24488 it->pixel_width = 0;
24489 it->nglyphs = 0;
24490
24491 height = get_it_property (it, Qline_height);
24492 /* Split (line-height total-height) list */
24493 if (CONSP (height)
24494 && CONSP (XCDR (height))
24495 && NILP (XCDR (XCDR (height))))
24496 {
24497 total_height = XCAR (XCDR (height));
24498 height = XCAR (height);
24499 }
24500 height = calc_line_height_property (it, height, font, boff, 1);
24501
24502 if (it->override_ascent >= 0)
24503 {
24504 it->ascent = it->override_ascent;
24505 it->descent = it->override_descent;
24506 boff = it->override_boff;
24507 }
24508 else
24509 {
24510 it->ascent = FONT_BASE (font) + boff;
24511 it->descent = FONT_DESCENT (font) - boff;
24512 }
24513
24514 if (EQ (height, Qt))
24515 {
24516 if (it->descent > it->max_descent)
24517 {
24518 it->ascent += it->descent - it->max_descent;
24519 it->descent = it->max_descent;
24520 }
24521 if (it->ascent > it->max_ascent)
24522 {
24523 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24524 it->ascent = it->max_ascent;
24525 }
24526 it->phys_ascent = min (it->phys_ascent, it->ascent);
24527 it->phys_descent = min (it->phys_descent, it->descent);
24528 it->constrain_row_ascent_descent_p = 1;
24529 extra_line_spacing = 0;
24530 }
24531 else
24532 {
24533 Lisp_Object spacing;
24534
24535 it->phys_ascent = it->ascent;
24536 it->phys_descent = it->descent;
24537
24538 if ((it->max_ascent > 0 || it->max_descent > 0)
24539 && face->box != FACE_NO_BOX
24540 && face->box_line_width > 0)
24541 {
24542 it->ascent += face->box_line_width;
24543 it->descent += face->box_line_width;
24544 }
24545 if (!NILP (height)
24546 && XINT (height) > it->ascent + it->descent)
24547 it->ascent = XINT (height) - it->descent;
24548
24549 if (!NILP (total_height))
24550 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24551 else
24552 {
24553 spacing = get_it_property (it, Qline_spacing);
24554 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24555 }
24556 if (INTEGERP (spacing))
24557 {
24558 extra_line_spacing = XINT (spacing);
24559 if (!NILP (total_height))
24560 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24561 }
24562 }
24563 }
24564 else /* i.e. (it->char_to_display == '\t') */
24565 {
24566 if (font->space_width > 0)
24567 {
24568 int tab_width = it->tab_width * font->space_width;
24569 int x = it->current_x + it->continuation_lines_width;
24570 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24571
24572 /* If the distance from the current position to the next tab
24573 stop is less than a space character width, use the
24574 tab stop after that. */
24575 if (next_tab_x - x < font->space_width)
24576 next_tab_x += tab_width;
24577
24578 it->pixel_width = next_tab_x - x;
24579 it->nglyphs = 1;
24580 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24581 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24582
24583 if (it->glyph_row)
24584 {
24585 append_stretch_glyph (it, it->object, it->pixel_width,
24586 it->ascent + it->descent, it->ascent);
24587 }
24588 }
24589 else
24590 {
24591 it->pixel_width = 0;
24592 it->nglyphs = 1;
24593 }
24594 }
24595 }
24596 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24597 {
24598 /* A static composition.
24599
24600 Note: A composition is represented as one glyph in the
24601 glyph matrix. There are no padding glyphs.
24602
24603 Important note: pixel_width, ascent, and descent are the
24604 values of what is drawn by draw_glyphs (i.e. the values of
24605 the overall glyphs composed). */
24606 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24607 int boff; /* baseline offset */
24608 struct composition *cmp = composition_table[it->cmp_it.id];
24609 int glyph_len = cmp->glyph_len;
24610 struct font *font = face->font;
24611
24612 it->nglyphs = 1;
24613
24614 /* If we have not yet calculated pixel size data of glyphs of
24615 the composition for the current face font, calculate them
24616 now. Theoretically, we have to check all fonts for the
24617 glyphs, but that requires much time and memory space. So,
24618 here we check only the font of the first glyph. This may
24619 lead to incorrect display, but it's very rare, and C-l
24620 (recenter-top-bottom) can correct the display anyway. */
24621 if (! cmp->font || cmp->font != font)
24622 {
24623 /* Ascent and descent of the font of the first character
24624 of this composition (adjusted by baseline offset).
24625 Ascent and descent of overall glyphs should not be less
24626 than these, respectively. */
24627 int font_ascent, font_descent, font_height;
24628 /* Bounding box of the overall glyphs. */
24629 int leftmost, rightmost, lowest, highest;
24630 int lbearing, rbearing;
24631 int i, width, ascent, descent;
24632 int left_padded = 0, right_padded = 0;
24633 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24634 XChar2b char2b;
24635 struct font_metrics *pcm;
24636 int font_not_found_p;
24637 ptrdiff_t pos;
24638
24639 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24640 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24641 break;
24642 if (glyph_len < cmp->glyph_len)
24643 right_padded = 1;
24644 for (i = 0; i < glyph_len; i++)
24645 {
24646 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24647 break;
24648 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24649 }
24650 if (i > 0)
24651 left_padded = 1;
24652
24653 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24654 : IT_CHARPOS (*it));
24655 /* If no suitable font is found, use the default font. */
24656 font_not_found_p = font == NULL;
24657 if (font_not_found_p)
24658 {
24659 face = face->ascii_face;
24660 font = face->font;
24661 }
24662 boff = font->baseline_offset;
24663 if (font->vertical_centering)
24664 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24665 font_ascent = FONT_BASE (font) + boff;
24666 font_descent = FONT_DESCENT (font) - boff;
24667 font_height = FONT_HEIGHT (font);
24668
24669 cmp->font = (void *) font;
24670
24671 pcm = NULL;
24672 if (! font_not_found_p)
24673 {
24674 get_char_face_and_encoding (it->f, c, it->face_id,
24675 &char2b, 0);
24676 pcm = get_per_char_metric (font, &char2b);
24677 }
24678
24679 /* Initialize the bounding box. */
24680 if (pcm)
24681 {
24682 width = cmp->glyph_len > 0 ? pcm->width : 0;
24683 ascent = pcm->ascent;
24684 descent = pcm->descent;
24685 lbearing = pcm->lbearing;
24686 rbearing = pcm->rbearing;
24687 }
24688 else
24689 {
24690 width = cmp->glyph_len > 0 ? font->space_width : 0;
24691 ascent = FONT_BASE (font);
24692 descent = FONT_DESCENT (font);
24693 lbearing = 0;
24694 rbearing = width;
24695 }
24696
24697 rightmost = width;
24698 leftmost = 0;
24699 lowest = - descent + boff;
24700 highest = ascent + boff;
24701
24702 if (! font_not_found_p
24703 && font->default_ascent
24704 && CHAR_TABLE_P (Vuse_default_ascent)
24705 && !NILP (Faref (Vuse_default_ascent,
24706 make_number (it->char_to_display))))
24707 highest = font->default_ascent + boff;
24708
24709 /* Draw the first glyph at the normal position. It may be
24710 shifted to right later if some other glyphs are drawn
24711 at the left. */
24712 cmp->offsets[i * 2] = 0;
24713 cmp->offsets[i * 2 + 1] = boff;
24714 cmp->lbearing = lbearing;
24715 cmp->rbearing = rbearing;
24716
24717 /* Set cmp->offsets for the remaining glyphs. */
24718 for (i++; i < glyph_len; i++)
24719 {
24720 int left, right, btm, top;
24721 int ch = COMPOSITION_GLYPH (cmp, i);
24722 int face_id;
24723 struct face *this_face;
24724
24725 if (ch == '\t')
24726 ch = ' ';
24727 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24728 this_face = FACE_FROM_ID (it->f, face_id);
24729 font = this_face->font;
24730
24731 if (font == NULL)
24732 pcm = NULL;
24733 else
24734 {
24735 get_char_face_and_encoding (it->f, ch, face_id,
24736 &char2b, 0);
24737 pcm = get_per_char_metric (font, &char2b);
24738 }
24739 if (! pcm)
24740 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24741 else
24742 {
24743 width = pcm->width;
24744 ascent = pcm->ascent;
24745 descent = pcm->descent;
24746 lbearing = pcm->lbearing;
24747 rbearing = pcm->rbearing;
24748 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24749 {
24750 /* Relative composition with or without
24751 alternate chars. */
24752 left = (leftmost + rightmost - width) / 2;
24753 btm = - descent + boff;
24754 if (font->relative_compose
24755 && (! CHAR_TABLE_P (Vignore_relative_composition)
24756 || NILP (Faref (Vignore_relative_composition,
24757 make_number (ch)))))
24758 {
24759
24760 if (- descent >= font->relative_compose)
24761 /* One extra pixel between two glyphs. */
24762 btm = highest + 1;
24763 else if (ascent <= 0)
24764 /* One extra pixel between two glyphs. */
24765 btm = lowest - 1 - ascent - descent;
24766 }
24767 }
24768 else
24769 {
24770 /* A composition rule is specified by an integer
24771 value that encodes global and new reference
24772 points (GREF and NREF). GREF and NREF are
24773 specified by numbers as below:
24774
24775 0---1---2 -- ascent
24776 | |
24777 | |
24778 | |
24779 9--10--11 -- center
24780 | |
24781 ---3---4---5--- baseline
24782 | |
24783 6---7---8 -- descent
24784 */
24785 int rule = COMPOSITION_RULE (cmp, i);
24786 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
24787
24788 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
24789 grefx = gref % 3, nrefx = nref % 3;
24790 grefy = gref / 3, nrefy = nref / 3;
24791 if (xoff)
24792 xoff = font_height * (xoff - 128) / 256;
24793 if (yoff)
24794 yoff = font_height * (yoff - 128) / 256;
24795
24796 left = (leftmost
24797 + grefx * (rightmost - leftmost) / 2
24798 - nrefx * width / 2
24799 + xoff);
24800
24801 btm = ((grefy == 0 ? highest
24802 : grefy == 1 ? 0
24803 : grefy == 2 ? lowest
24804 : (highest + lowest) / 2)
24805 - (nrefy == 0 ? ascent + descent
24806 : nrefy == 1 ? descent - boff
24807 : nrefy == 2 ? 0
24808 : (ascent + descent) / 2)
24809 + yoff);
24810 }
24811
24812 cmp->offsets[i * 2] = left;
24813 cmp->offsets[i * 2 + 1] = btm + descent;
24814
24815 /* Update the bounding box of the overall glyphs. */
24816 if (width > 0)
24817 {
24818 right = left + width;
24819 if (left < leftmost)
24820 leftmost = left;
24821 if (right > rightmost)
24822 rightmost = right;
24823 }
24824 top = btm + descent + ascent;
24825 if (top > highest)
24826 highest = top;
24827 if (btm < lowest)
24828 lowest = btm;
24829
24830 if (cmp->lbearing > left + lbearing)
24831 cmp->lbearing = left + lbearing;
24832 if (cmp->rbearing < left + rbearing)
24833 cmp->rbearing = left + rbearing;
24834 }
24835 }
24836
24837 /* If there are glyphs whose x-offsets are negative,
24838 shift all glyphs to the right and make all x-offsets
24839 non-negative. */
24840 if (leftmost < 0)
24841 {
24842 for (i = 0; i < cmp->glyph_len; i++)
24843 cmp->offsets[i * 2] -= leftmost;
24844 rightmost -= leftmost;
24845 cmp->lbearing -= leftmost;
24846 cmp->rbearing -= leftmost;
24847 }
24848
24849 if (left_padded && cmp->lbearing < 0)
24850 {
24851 for (i = 0; i < cmp->glyph_len; i++)
24852 cmp->offsets[i * 2] -= cmp->lbearing;
24853 rightmost -= cmp->lbearing;
24854 cmp->rbearing -= cmp->lbearing;
24855 cmp->lbearing = 0;
24856 }
24857 if (right_padded && rightmost < cmp->rbearing)
24858 {
24859 rightmost = cmp->rbearing;
24860 }
24861
24862 cmp->pixel_width = rightmost;
24863 cmp->ascent = highest;
24864 cmp->descent = - lowest;
24865 if (cmp->ascent < font_ascent)
24866 cmp->ascent = font_ascent;
24867 if (cmp->descent < font_descent)
24868 cmp->descent = font_descent;
24869 }
24870
24871 if (it->glyph_row
24872 && (cmp->lbearing < 0
24873 || cmp->rbearing > cmp->pixel_width))
24874 it->glyph_row->contains_overlapping_glyphs_p = 1;
24875
24876 it->pixel_width = cmp->pixel_width;
24877 it->ascent = it->phys_ascent = cmp->ascent;
24878 it->descent = it->phys_descent = cmp->descent;
24879 if (face->box != FACE_NO_BOX)
24880 {
24881 int thick = face->box_line_width;
24882
24883 if (thick > 0)
24884 {
24885 it->ascent += thick;
24886 it->descent += thick;
24887 }
24888 else
24889 thick = - thick;
24890
24891 if (it->start_of_box_run_p)
24892 it->pixel_width += thick;
24893 if (it->end_of_box_run_p)
24894 it->pixel_width += thick;
24895 }
24896
24897 /* If face has an overline, add the height of the overline
24898 (1 pixel) and a 1 pixel margin to the character height. */
24899 if (face->overline_p)
24900 it->ascent += overline_margin;
24901
24902 take_vertical_position_into_account (it);
24903 if (it->ascent < 0)
24904 it->ascent = 0;
24905 if (it->descent < 0)
24906 it->descent = 0;
24907
24908 if (it->glyph_row && cmp->glyph_len > 0)
24909 append_composite_glyph (it);
24910 }
24911 else if (it->what == IT_COMPOSITION)
24912 {
24913 /* A dynamic (automatic) composition. */
24914 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24915 Lisp_Object gstring;
24916 struct font_metrics metrics;
24917
24918 it->nglyphs = 1;
24919
24920 gstring = composition_gstring_from_id (it->cmp_it.id);
24921 it->pixel_width
24922 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
24923 &metrics);
24924 if (it->glyph_row
24925 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
24926 it->glyph_row->contains_overlapping_glyphs_p = 1;
24927 it->ascent = it->phys_ascent = metrics.ascent;
24928 it->descent = it->phys_descent = metrics.descent;
24929 if (face->box != FACE_NO_BOX)
24930 {
24931 int thick = face->box_line_width;
24932
24933 if (thick > 0)
24934 {
24935 it->ascent += thick;
24936 it->descent += thick;
24937 }
24938 else
24939 thick = - thick;
24940
24941 if (it->start_of_box_run_p)
24942 it->pixel_width += thick;
24943 if (it->end_of_box_run_p)
24944 it->pixel_width += thick;
24945 }
24946 /* If face has an overline, add the height of the overline
24947 (1 pixel) and a 1 pixel margin to the character height. */
24948 if (face->overline_p)
24949 it->ascent += overline_margin;
24950 take_vertical_position_into_account (it);
24951 if (it->ascent < 0)
24952 it->ascent = 0;
24953 if (it->descent < 0)
24954 it->descent = 0;
24955
24956 if (it->glyph_row)
24957 append_composite_glyph (it);
24958 }
24959 else if (it->what == IT_GLYPHLESS)
24960 produce_glyphless_glyph (it, 0, Qnil);
24961 else if (it->what == IT_IMAGE)
24962 produce_image_glyph (it);
24963 else if (it->what == IT_STRETCH)
24964 produce_stretch_glyph (it);
24965
24966 done:
24967 /* Accumulate dimensions. Note: can't assume that it->descent > 0
24968 because this isn't true for images with `:ascent 100'. */
24969 xassert (it->ascent >= 0 && it->descent >= 0);
24970 if (it->area == TEXT_AREA)
24971 it->current_x += it->pixel_width;
24972
24973 if (extra_line_spacing > 0)
24974 {
24975 it->descent += extra_line_spacing;
24976 if (extra_line_spacing > it->max_extra_line_spacing)
24977 it->max_extra_line_spacing = extra_line_spacing;
24978 }
24979
24980 it->max_ascent = max (it->max_ascent, it->ascent);
24981 it->max_descent = max (it->max_descent, it->descent);
24982 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
24983 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
24984 }
24985
24986 /* EXPORT for RIF:
24987 Output LEN glyphs starting at START at the nominal cursor position.
24988 Advance the nominal cursor over the text. The global variable
24989 updated_window contains the window being updated, updated_row is
24990 the glyph row being updated, and updated_area is the area of that
24991 row being updated. */
24992
24993 void
24994 x_write_glyphs (struct glyph *start, int len)
24995 {
24996 int x, hpos, chpos = updated_window->phys_cursor.hpos;
24997
24998 xassert (updated_window && updated_row);
24999 /* When the window is hscrolled, cursor hpos can legitimately be out
25000 of bounds, but we draw the cursor at the corresponding window
25001 margin in that case. */
25002 if (!updated_row->reversed_p && chpos < 0)
25003 chpos = 0;
25004 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25005 chpos = updated_row->used[TEXT_AREA] - 1;
25006
25007 BLOCK_INPUT;
25008
25009 /* Write glyphs. */
25010
25011 hpos = start - updated_row->glyphs[updated_area];
25012 x = draw_glyphs (updated_window, output_cursor.x,
25013 updated_row, updated_area,
25014 hpos, hpos + len,
25015 DRAW_NORMAL_TEXT, 0);
25016
25017 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25018 if (updated_area == TEXT_AREA
25019 && updated_window->phys_cursor_on_p
25020 && updated_window->phys_cursor.vpos == output_cursor.vpos
25021 && chpos >= hpos
25022 && chpos < hpos + len)
25023 updated_window->phys_cursor_on_p = 0;
25024
25025 UNBLOCK_INPUT;
25026
25027 /* Advance the output cursor. */
25028 output_cursor.hpos += len;
25029 output_cursor.x = x;
25030 }
25031
25032
25033 /* EXPORT for RIF:
25034 Insert LEN glyphs from START at the nominal cursor position. */
25035
25036 void
25037 x_insert_glyphs (struct glyph *start, int len)
25038 {
25039 struct frame *f;
25040 struct window *w;
25041 int line_height, shift_by_width, shifted_region_width;
25042 struct glyph_row *row;
25043 struct glyph *glyph;
25044 int frame_x, frame_y;
25045 ptrdiff_t hpos;
25046
25047 xassert (updated_window && updated_row);
25048 BLOCK_INPUT;
25049 w = updated_window;
25050 f = XFRAME (WINDOW_FRAME (w));
25051
25052 /* Get the height of the line we are in. */
25053 row = updated_row;
25054 line_height = row->height;
25055
25056 /* Get the width of the glyphs to insert. */
25057 shift_by_width = 0;
25058 for (glyph = start; glyph < start + len; ++glyph)
25059 shift_by_width += glyph->pixel_width;
25060
25061 /* Get the width of the region to shift right. */
25062 shifted_region_width = (window_box_width (w, updated_area)
25063 - output_cursor.x
25064 - shift_by_width);
25065
25066 /* Shift right. */
25067 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25068 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25069
25070 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25071 line_height, shift_by_width);
25072
25073 /* Write the glyphs. */
25074 hpos = start - row->glyphs[updated_area];
25075 draw_glyphs (w, output_cursor.x, row, updated_area,
25076 hpos, hpos + len,
25077 DRAW_NORMAL_TEXT, 0);
25078
25079 /* Advance the output cursor. */
25080 output_cursor.hpos += len;
25081 output_cursor.x += shift_by_width;
25082 UNBLOCK_INPUT;
25083 }
25084
25085
25086 /* EXPORT for RIF:
25087 Erase the current text line from the nominal cursor position
25088 (inclusive) to pixel column TO_X (exclusive). The idea is that
25089 everything from TO_X onward is already erased.
25090
25091 TO_X is a pixel position relative to updated_area of
25092 updated_window. TO_X == -1 means clear to the end of this area. */
25093
25094 void
25095 x_clear_end_of_line (int to_x)
25096 {
25097 struct frame *f;
25098 struct window *w = updated_window;
25099 int max_x, min_y, max_y;
25100 int from_x, from_y, to_y;
25101
25102 xassert (updated_window && updated_row);
25103 f = XFRAME (w->frame);
25104
25105 if (updated_row->full_width_p)
25106 max_x = WINDOW_TOTAL_WIDTH (w);
25107 else
25108 max_x = window_box_width (w, updated_area);
25109 max_y = window_text_bottom_y (w);
25110
25111 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25112 of window. For TO_X > 0, truncate to end of drawing area. */
25113 if (to_x == 0)
25114 return;
25115 else if (to_x < 0)
25116 to_x = max_x;
25117 else
25118 to_x = min (to_x, max_x);
25119
25120 to_y = min (max_y, output_cursor.y + updated_row->height);
25121
25122 /* Notice if the cursor will be cleared by this operation. */
25123 if (!updated_row->full_width_p)
25124 notice_overwritten_cursor (w, updated_area,
25125 output_cursor.x, -1,
25126 updated_row->y,
25127 MATRIX_ROW_BOTTOM_Y (updated_row));
25128
25129 from_x = output_cursor.x;
25130
25131 /* Translate to frame coordinates. */
25132 if (updated_row->full_width_p)
25133 {
25134 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25135 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25136 }
25137 else
25138 {
25139 int area_left = window_box_left (w, updated_area);
25140 from_x += area_left;
25141 to_x += area_left;
25142 }
25143
25144 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25145 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25146 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25147
25148 /* Prevent inadvertently clearing to end of the X window. */
25149 if (to_x > from_x && to_y > from_y)
25150 {
25151 BLOCK_INPUT;
25152 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25153 to_x - from_x, to_y - from_y);
25154 UNBLOCK_INPUT;
25155 }
25156 }
25157
25158 #endif /* HAVE_WINDOW_SYSTEM */
25159
25160
25161 \f
25162 /***********************************************************************
25163 Cursor types
25164 ***********************************************************************/
25165
25166 /* Value is the internal representation of the specified cursor type
25167 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25168 of the bar cursor. */
25169
25170 static enum text_cursor_kinds
25171 get_specified_cursor_type (Lisp_Object arg, int *width)
25172 {
25173 enum text_cursor_kinds type;
25174
25175 if (NILP (arg))
25176 return NO_CURSOR;
25177
25178 if (EQ (arg, Qbox))
25179 return FILLED_BOX_CURSOR;
25180
25181 if (EQ (arg, Qhollow))
25182 return HOLLOW_BOX_CURSOR;
25183
25184 if (EQ (arg, Qbar))
25185 {
25186 *width = 2;
25187 return BAR_CURSOR;
25188 }
25189
25190 if (CONSP (arg)
25191 && EQ (XCAR (arg), Qbar)
25192 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25193 {
25194 *width = XINT (XCDR (arg));
25195 return BAR_CURSOR;
25196 }
25197
25198 if (EQ (arg, Qhbar))
25199 {
25200 *width = 2;
25201 return HBAR_CURSOR;
25202 }
25203
25204 if (CONSP (arg)
25205 && EQ (XCAR (arg), Qhbar)
25206 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25207 {
25208 *width = XINT (XCDR (arg));
25209 return HBAR_CURSOR;
25210 }
25211
25212 /* Treat anything unknown as "hollow box cursor".
25213 It was bad to signal an error; people have trouble fixing
25214 .Xdefaults with Emacs, when it has something bad in it. */
25215 type = HOLLOW_BOX_CURSOR;
25216
25217 return type;
25218 }
25219
25220 /* Set the default cursor types for specified frame. */
25221 void
25222 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25223 {
25224 int width = 1;
25225 Lisp_Object tem;
25226
25227 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25228 FRAME_CURSOR_WIDTH (f) = width;
25229
25230 /* By default, set up the blink-off state depending on the on-state. */
25231
25232 tem = Fassoc (arg, Vblink_cursor_alist);
25233 if (!NILP (tem))
25234 {
25235 FRAME_BLINK_OFF_CURSOR (f)
25236 = get_specified_cursor_type (XCDR (tem), &width);
25237 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25238 }
25239 else
25240 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25241 }
25242
25243
25244 #ifdef HAVE_WINDOW_SYSTEM
25245
25246 /* Return the cursor we want to be displayed in window W. Return
25247 width of bar/hbar cursor through WIDTH arg. Return with
25248 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25249 (i.e. if the `system caret' should track this cursor).
25250
25251 In a mini-buffer window, we want the cursor only to appear if we
25252 are reading input from this window. For the selected window, we
25253 want the cursor type given by the frame parameter or buffer local
25254 setting of cursor-type. If explicitly marked off, draw no cursor.
25255 In all other cases, we want a hollow box cursor. */
25256
25257 static enum text_cursor_kinds
25258 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25259 int *active_cursor)
25260 {
25261 struct frame *f = XFRAME (w->frame);
25262 struct buffer *b = XBUFFER (w->buffer);
25263 int cursor_type = DEFAULT_CURSOR;
25264 Lisp_Object alt_cursor;
25265 int non_selected = 0;
25266
25267 *active_cursor = 1;
25268
25269 /* Echo area */
25270 if (cursor_in_echo_area
25271 && FRAME_HAS_MINIBUF_P (f)
25272 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25273 {
25274 if (w == XWINDOW (echo_area_window))
25275 {
25276 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25277 {
25278 *width = FRAME_CURSOR_WIDTH (f);
25279 return FRAME_DESIRED_CURSOR (f);
25280 }
25281 else
25282 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25283 }
25284
25285 *active_cursor = 0;
25286 non_selected = 1;
25287 }
25288
25289 /* Detect a nonselected window or nonselected frame. */
25290 else if (w != XWINDOW (f->selected_window)
25291 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25292 {
25293 *active_cursor = 0;
25294
25295 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25296 return NO_CURSOR;
25297
25298 non_selected = 1;
25299 }
25300
25301 /* Never display a cursor in a window in which cursor-type is nil. */
25302 if (NILP (BVAR (b, cursor_type)))
25303 return NO_CURSOR;
25304
25305 /* Get the normal cursor type for this window. */
25306 if (EQ (BVAR (b, cursor_type), Qt))
25307 {
25308 cursor_type = FRAME_DESIRED_CURSOR (f);
25309 *width = FRAME_CURSOR_WIDTH (f);
25310 }
25311 else
25312 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25313
25314 /* Use cursor-in-non-selected-windows instead
25315 for non-selected window or frame. */
25316 if (non_selected)
25317 {
25318 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25319 if (!EQ (Qt, alt_cursor))
25320 return get_specified_cursor_type (alt_cursor, width);
25321 /* t means modify the normal cursor type. */
25322 if (cursor_type == FILLED_BOX_CURSOR)
25323 cursor_type = HOLLOW_BOX_CURSOR;
25324 else if (cursor_type == BAR_CURSOR && *width > 1)
25325 --*width;
25326 return cursor_type;
25327 }
25328
25329 /* Use normal cursor if not blinked off. */
25330 if (!w->cursor_off_p)
25331 {
25332 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25333 {
25334 if (cursor_type == FILLED_BOX_CURSOR)
25335 {
25336 /* Using a block cursor on large images can be very annoying.
25337 So use a hollow cursor for "large" images.
25338 If image is not transparent (no mask), also use hollow cursor. */
25339 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25340 if (img != NULL && IMAGEP (img->spec))
25341 {
25342 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25343 where N = size of default frame font size.
25344 This should cover most of the "tiny" icons people may use. */
25345 if (!img->mask
25346 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25347 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25348 cursor_type = HOLLOW_BOX_CURSOR;
25349 }
25350 }
25351 else if (cursor_type != NO_CURSOR)
25352 {
25353 /* Display current only supports BOX and HOLLOW cursors for images.
25354 So for now, unconditionally use a HOLLOW cursor when cursor is
25355 not a solid box cursor. */
25356 cursor_type = HOLLOW_BOX_CURSOR;
25357 }
25358 }
25359 return cursor_type;
25360 }
25361
25362 /* Cursor is blinked off, so determine how to "toggle" it. */
25363
25364 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25365 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25366 return get_specified_cursor_type (XCDR (alt_cursor), width);
25367
25368 /* Then see if frame has specified a specific blink off cursor type. */
25369 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25370 {
25371 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25372 return FRAME_BLINK_OFF_CURSOR (f);
25373 }
25374
25375 #if 0
25376 /* Some people liked having a permanently visible blinking cursor,
25377 while others had very strong opinions against it. So it was
25378 decided to remove it. KFS 2003-09-03 */
25379
25380 /* Finally perform built-in cursor blinking:
25381 filled box <-> hollow box
25382 wide [h]bar <-> narrow [h]bar
25383 narrow [h]bar <-> no cursor
25384 other type <-> no cursor */
25385
25386 if (cursor_type == FILLED_BOX_CURSOR)
25387 return HOLLOW_BOX_CURSOR;
25388
25389 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25390 {
25391 *width = 1;
25392 return cursor_type;
25393 }
25394 #endif
25395
25396 return NO_CURSOR;
25397 }
25398
25399
25400 /* Notice when the text cursor of window W has been completely
25401 overwritten by a drawing operation that outputs glyphs in AREA
25402 starting at X0 and ending at X1 in the line starting at Y0 and
25403 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25404 the rest of the line after X0 has been written. Y coordinates
25405 are window-relative. */
25406
25407 static void
25408 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25409 int x0, int x1, int y0, int y1)
25410 {
25411 int cx0, cx1, cy0, cy1;
25412 struct glyph_row *row;
25413
25414 if (!w->phys_cursor_on_p)
25415 return;
25416 if (area != TEXT_AREA)
25417 return;
25418
25419 if (w->phys_cursor.vpos < 0
25420 || w->phys_cursor.vpos >= w->current_matrix->nrows
25421 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25422 !(row->enabled_p && row->displays_text_p)))
25423 return;
25424
25425 if (row->cursor_in_fringe_p)
25426 {
25427 row->cursor_in_fringe_p = 0;
25428 draw_fringe_bitmap (w, row, row->reversed_p);
25429 w->phys_cursor_on_p = 0;
25430 return;
25431 }
25432
25433 cx0 = w->phys_cursor.x;
25434 cx1 = cx0 + w->phys_cursor_width;
25435 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25436 return;
25437
25438 /* The cursor image will be completely removed from the
25439 screen if the output area intersects the cursor area in
25440 y-direction. When we draw in [y0 y1[, and some part of
25441 the cursor is at y < y0, that part must have been drawn
25442 before. When scrolling, the cursor is erased before
25443 actually scrolling, so we don't come here. When not
25444 scrolling, the rows above the old cursor row must have
25445 changed, and in this case these rows must have written
25446 over the cursor image.
25447
25448 Likewise if part of the cursor is below y1, with the
25449 exception of the cursor being in the first blank row at
25450 the buffer and window end because update_text_area
25451 doesn't draw that row. (Except when it does, but
25452 that's handled in update_text_area.) */
25453
25454 cy0 = w->phys_cursor.y;
25455 cy1 = cy0 + w->phys_cursor_height;
25456 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25457 return;
25458
25459 w->phys_cursor_on_p = 0;
25460 }
25461
25462 #endif /* HAVE_WINDOW_SYSTEM */
25463
25464 \f
25465 /************************************************************************
25466 Mouse Face
25467 ************************************************************************/
25468
25469 #ifdef HAVE_WINDOW_SYSTEM
25470
25471 /* EXPORT for RIF:
25472 Fix the display of area AREA of overlapping row ROW in window W
25473 with respect to the overlapping part OVERLAPS. */
25474
25475 void
25476 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25477 enum glyph_row_area area, int overlaps)
25478 {
25479 int i, x;
25480
25481 BLOCK_INPUT;
25482
25483 x = 0;
25484 for (i = 0; i < row->used[area];)
25485 {
25486 if (row->glyphs[area][i].overlaps_vertically_p)
25487 {
25488 int start = i, start_x = x;
25489
25490 do
25491 {
25492 x += row->glyphs[area][i].pixel_width;
25493 ++i;
25494 }
25495 while (i < row->used[area]
25496 && row->glyphs[area][i].overlaps_vertically_p);
25497
25498 draw_glyphs (w, start_x, row, area,
25499 start, i,
25500 DRAW_NORMAL_TEXT, overlaps);
25501 }
25502 else
25503 {
25504 x += row->glyphs[area][i].pixel_width;
25505 ++i;
25506 }
25507 }
25508
25509 UNBLOCK_INPUT;
25510 }
25511
25512
25513 /* EXPORT:
25514 Draw the cursor glyph of window W in glyph row ROW. See the
25515 comment of draw_glyphs for the meaning of HL. */
25516
25517 void
25518 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25519 enum draw_glyphs_face hl)
25520 {
25521 /* If cursor hpos is out of bounds, don't draw garbage. This can
25522 happen in mini-buffer windows when switching between echo area
25523 glyphs and mini-buffer. */
25524 if ((row->reversed_p
25525 ? (w->phys_cursor.hpos >= 0)
25526 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25527 {
25528 int on_p = w->phys_cursor_on_p;
25529 int x1;
25530 int hpos = w->phys_cursor.hpos;
25531
25532 /* When the window is hscrolled, cursor hpos can legitimately be
25533 out of bounds, but we draw the cursor at the corresponding
25534 window margin in that case. */
25535 if (!row->reversed_p && hpos < 0)
25536 hpos = 0;
25537 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25538 hpos = row->used[TEXT_AREA] - 1;
25539
25540 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25541 hl, 0);
25542 w->phys_cursor_on_p = on_p;
25543
25544 if (hl == DRAW_CURSOR)
25545 w->phys_cursor_width = x1 - w->phys_cursor.x;
25546 /* When we erase the cursor, and ROW is overlapped by other
25547 rows, make sure that these overlapping parts of other rows
25548 are redrawn. */
25549 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25550 {
25551 w->phys_cursor_width = x1 - w->phys_cursor.x;
25552
25553 if (row > w->current_matrix->rows
25554 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25555 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25556 OVERLAPS_ERASED_CURSOR);
25557
25558 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25559 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25560 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25561 OVERLAPS_ERASED_CURSOR);
25562 }
25563 }
25564 }
25565
25566
25567 /* EXPORT:
25568 Erase the image of a cursor of window W from the screen. */
25569
25570 void
25571 erase_phys_cursor (struct window *w)
25572 {
25573 struct frame *f = XFRAME (w->frame);
25574 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25575 int hpos = w->phys_cursor.hpos;
25576 int vpos = w->phys_cursor.vpos;
25577 int mouse_face_here_p = 0;
25578 struct glyph_matrix *active_glyphs = w->current_matrix;
25579 struct glyph_row *cursor_row;
25580 struct glyph *cursor_glyph;
25581 enum draw_glyphs_face hl;
25582
25583 /* No cursor displayed or row invalidated => nothing to do on the
25584 screen. */
25585 if (w->phys_cursor_type == NO_CURSOR)
25586 goto mark_cursor_off;
25587
25588 /* VPOS >= active_glyphs->nrows means that window has been resized.
25589 Don't bother to erase the cursor. */
25590 if (vpos >= active_glyphs->nrows)
25591 goto mark_cursor_off;
25592
25593 /* If row containing cursor is marked invalid, there is nothing we
25594 can do. */
25595 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25596 if (!cursor_row->enabled_p)
25597 goto mark_cursor_off;
25598
25599 /* If line spacing is > 0, old cursor may only be partially visible in
25600 window after split-window. So adjust visible height. */
25601 cursor_row->visible_height = min (cursor_row->visible_height,
25602 window_text_bottom_y (w) - cursor_row->y);
25603
25604 /* If row is completely invisible, don't attempt to delete a cursor which
25605 isn't there. This can happen if cursor is at top of a window, and
25606 we switch to a buffer with a header line in that window. */
25607 if (cursor_row->visible_height <= 0)
25608 goto mark_cursor_off;
25609
25610 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25611 if (cursor_row->cursor_in_fringe_p)
25612 {
25613 cursor_row->cursor_in_fringe_p = 0;
25614 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25615 goto mark_cursor_off;
25616 }
25617
25618 /* This can happen when the new row is shorter than the old one.
25619 In this case, either draw_glyphs or clear_end_of_line
25620 should have cleared the cursor. Note that we wouldn't be
25621 able to erase the cursor in this case because we don't have a
25622 cursor glyph at hand. */
25623 if ((cursor_row->reversed_p
25624 ? (w->phys_cursor.hpos < 0)
25625 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25626 goto mark_cursor_off;
25627
25628 /* When the window is hscrolled, cursor hpos can legitimately be out
25629 of bounds, but we draw the cursor at the corresponding window
25630 margin in that case. */
25631 if (!cursor_row->reversed_p && hpos < 0)
25632 hpos = 0;
25633 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25634 hpos = cursor_row->used[TEXT_AREA] - 1;
25635
25636 /* If the cursor is in the mouse face area, redisplay that when
25637 we clear the cursor. */
25638 if (! NILP (hlinfo->mouse_face_window)
25639 && coords_in_mouse_face_p (w, hpos, vpos)
25640 /* Don't redraw the cursor's spot in mouse face if it is at the
25641 end of a line (on a newline). The cursor appears there, but
25642 mouse highlighting does not. */
25643 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25644 mouse_face_here_p = 1;
25645
25646 /* Maybe clear the display under the cursor. */
25647 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25648 {
25649 int x, y, left_x;
25650 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25651 int width;
25652
25653 cursor_glyph = get_phys_cursor_glyph (w);
25654 if (cursor_glyph == NULL)
25655 goto mark_cursor_off;
25656
25657 width = cursor_glyph->pixel_width;
25658 left_x = window_box_left_offset (w, TEXT_AREA);
25659 x = w->phys_cursor.x;
25660 if (x < left_x)
25661 width -= left_x - x;
25662 width = min (width, window_box_width (w, TEXT_AREA) - x);
25663 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25664 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25665
25666 if (width > 0)
25667 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25668 }
25669
25670 /* Erase the cursor by redrawing the character underneath it. */
25671 if (mouse_face_here_p)
25672 hl = DRAW_MOUSE_FACE;
25673 else
25674 hl = DRAW_NORMAL_TEXT;
25675 draw_phys_cursor_glyph (w, cursor_row, hl);
25676
25677 mark_cursor_off:
25678 w->phys_cursor_on_p = 0;
25679 w->phys_cursor_type = NO_CURSOR;
25680 }
25681
25682
25683 /* EXPORT:
25684 Display or clear cursor of window W. If ON is zero, clear the
25685 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25686 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25687
25688 void
25689 display_and_set_cursor (struct window *w, int on,
25690 int hpos, int vpos, int x, int y)
25691 {
25692 struct frame *f = XFRAME (w->frame);
25693 int new_cursor_type;
25694 int new_cursor_width;
25695 int active_cursor;
25696 struct glyph_row *glyph_row;
25697 struct glyph *glyph;
25698
25699 /* This is pointless on invisible frames, and dangerous on garbaged
25700 windows and frames; in the latter case, the frame or window may
25701 be in the midst of changing its size, and x and y may be off the
25702 window. */
25703 if (! FRAME_VISIBLE_P (f)
25704 || FRAME_GARBAGED_P (f)
25705 || vpos >= w->current_matrix->nrows
25706 || hpos >= w->current_matrix->matrix_w)
25707 return;
25708
25709 /* If cursor is off and we want it off, return quickly. */
25710 if (!on && !w->phys_cursor_on_p)
25711 return;
25712
25713 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25714 /* If cursor row is not enabled, we don't really know where to
25715 display the cursor. */
25716 if (!glyph_row->enabled_p)
25717 {
25718 w->phys_cursor_on_p = 0;
25719 return;
25720 }
25721
25722 glyph = NULL;
25723 if (!glyph_row->exact_window_width_line_p
25724 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25725 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25726
25727 xassert (interrupt_input_blocked);
25728
25729 /* Set new_cursor_type to the cursor we want to be displayed. */
25730 new_cursor_type = get_window_cursor_type (w, glyph,
25731 &new_cursor_width, &active_cursor);
25732
25733 /* If cursor is currently being shown and we don't want it to be or
25734 it is in the wrong place, or the cursor type is not what we want,
25735 erase it. */
25736 if (w->phys_cursor_on_p
25737 && (!on
25738 || w->phys_cursor.x != x
25739 || w->phys_cursor.y != y
25740 || new_cursor_type != w->phys_cursor_type
25741 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25742 && new_cursor_width != w->phys_cursor_width)))
25743 erase_phys_cursor (w);
25744
25745 /* Don't check phys_cursor_on_p here because that flag is only set
25746 to zero in some cases where we know that the cursor has been
25747 completely erased, to avoid the extra work of erasing the cursor
25748 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25749 still not be visible, or it has only been partly erased. */
25750 if (on)
25751 {
25752 w->phys_cursor_ascent = glyph_row->ascent;
25753 w->phys_cursor_height = glyph_row->height;
25754
25755 /* Set phys_cursor_.* before x_draw_.* is called because some
25756 of them may need the information. */
25757 w->phys_cursor.x = x;
25758 w->phys_cursor.y = glyph_row->y;
25759 w->phys_cursor.hpos = hpos;
25760 w->phys_cursor.vpos = vpos;
25761 }
25762
25763 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25764 new_cursor_type, new_cursor_width,
25765 on, active_cursor);
25766 }
25767
25768
25769 /* Switch the display of W's cursor on or off, according to the value
25770 of ON. */
25771
25772 static void
25773 update_window_cursor (struct window *w, int on)
25774 {
25775 /* Don't update cursor in windows whose frame is in the process
25776 of being deleted. */
25777 if (w->current_matrix)
25778 {
25779 int hpos = w->phys_cursor.hpos;
25780 int vpos = w->phys_cursor.vpos;
25781 struct glyph_row *row;
25782
25783 if (vpos >= w->current_matrix->nrows
25784 || hpos >= w->current_matrix->matrix_w)
25785 return;
25786
25787 row = MATRIX_ROW (w->current_matrix, vpos);
25788
25789 /* When the window is hscrolled, cursor hpos can legitimately be
25790 out of bounds, but we draw the cursor at the corresponding
25791 window margin in that case. */
25792 if (!row->reversed_p && hpos < 0)
25793 hpos = 0;
25794 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25795 hpos = row->used[TEXT_AREA] - 1;
25796
25797 BLOCK_INPUT;
25798 display_and_set_cursor (w, on, hpos, vpos,
25799 w->phys_cursor.x, w->phys_cursor.y);
25800 UNBLOCK_INPUT;
25801 }
25802 }
25803
25804
25805 /* Call update_window_cursor with parameter ON_P on all leaf windows
25806 in the window tree rooted at W. */
25807
25808 static void
25809 update_cursor_in_window_tree (struct window *w, int on_p)
25810 {
25811 while (w)
25812 {
25813 if (!NILP (w->hchild))
25814 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
25815 else if (!NILP (w->vchild))
25816 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
25817 else
25818 update_window_cursor (w, on_p);
25819
25820 w = NILP (w->next) ? 0 : XWINDOW (w->next);
25821 }
25822 }
25823
25824
25825 /* EXPORT:
25826 Display the cursor on window W, or clear it, according to ON_P.
25827 Don't change the cursor's position. */
25828
25829 void
25830 x_update_cursor (struct frame *f, int on_p)
25831 {
25832 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
25833 }
25834
25835
25836 /* EXPORT:
25837 Clear the cursor of window W to background color, and mark the
25838 cursor as not shown. This is used when the text where the cursor
25839 is about to be rewritten. */
25840
25841 void
25842 x_clear_cursor (struct window *w)
25843 {
25844 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
25845 update_window_cursor (w, 0);
25846 }
25847
25848 #endif /* HAVE_WINDOW_SYSTEM */
25849
25850 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
25851 and MSDOS. */
25852 static void
25853 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
25854 int start_hpos, int end_hpos,
25855 enum draw_glyphs_face draw)
25856 {
25857 #ifdef HAVE_WINDOW_SYSTEM
25858 if (FRAME_WINDOW_P (XFRAME (w->frame)))
25859 {
25860 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
25861 return;
25862 }
25863 #endif
25864 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
25865 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
25866 #endif
25867 }
25868
25869 /* Display the active region described by mouse_face_* according to DRAW. */
25870
25871 static void
25872 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
25873 {
25874 struct window *w = XWINDOW (hlinfo->mouse_face_window);
25875 struct frame *f = XFRAME (WINDOW_FRAME (w));
25876
25877 if (/* If window is in the process of being destroyed, don't bother
25878 to do anything. */
25879 w->current_matrix != NULL
25880 /* Don't update mouse highlight if hidden */
25881 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
25882 /* Recognize when we are called to operate on rows that don't exist
25883 anymore. This can happen when a window is split. */
25884 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
25885 {
25886 int phys_cursor_on_p = w->phys_cursor_on_p;
25887 struct glyph_row *row, *first, *last;
25888
25889 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
25890 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
25891
25892 for (row = first; row <= last && row->enabled_p; ++row)
25893 {
25894 int start_hpos, end_hpos, start_x;
25895
25896 /* For all but the first row, the highlight starts at column 0. */
25897 if (row == first)
25898 {
25899 /* R2L rows have BEG and END in reversed order, but the
25900 screen drawing geometry is always left to right. So
25901 we need to mirror the beginning and end of the
25902 highlighted area in R2L rows. */
25903 if (!row->reversed_p)
25904 {
25905 start_hpos = hlinfo->mouse_face_beg_col;
25906 start_x = hlinfo->mouse_face_beg_x;
25907 }
25908 else if (row == last)
25909 {
25910 start_hpos = hlinfo->mouse_face_end_col;
25911 start_x = hlinfo->mouse_face_end_x;
25912 }
25913 else
25914 {
25915 start_hpos = 0;
25916 start_x = 0;
25917 }
25918 }
25919 else if (row->reversed_p && row == last)
25920 {
25921 start_hpos = hlinfo->mouse_face_end_col;
25922 start_x = hlinfo->mouse_face_end_x;
25923 }
25924 else
25925 {
25926 start_hpos = 0;
25927 start_x = 0;
25928 }
25929
25930 if (row == last)
25931 {
25932 if (!row->reversed_p)
25933 end_hpos = hlinfo->mouse_face_end_col;
25934 else if (row == first)
25935 end_hpos = hlinfo->mouse_face_beg_col;
25936 else
25937 {
25938 end_hpos = row->used[TEXT_AREA];
25939 if (draw == DRAW_NORMAL_TEXT)
25940 row->fill_line_p = 1; /* Clear to end of line */
25941 }
25942 }
25943 else if (row->reversed_p && row == first)
25944 end_hpos = hlinfo->mouse_face_beg_col;
25945 else
25946 {
25947 end_hpos = row->used[TEXT_AREA];
25948 if (draw == DRAW_NORMAL_TEXT)
25949 row->fill_line_p = 1; /* Clear to end of line */
25950 }
25951
25952 if (end_hpos > start_hpos)
25953 {
25954 draw_row_with_mouse_face (w, start_x, row,
25955 start_hpos, end_hpos, draw);
25956
25957 row->mouse_face_p
25958 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
25959 }
25960 }
25961
25962 #ifdef HAVE_WINDOW_SYSTEM
25963 /* When we've written over the cursor, arrange for it to
25964 be displayed again. */
25965 if (FRAME_WINDOW_P (f)
25966 && phys_cursor_on_p && !w->phys_cursor_on_p)
25967 {
25968 int hpos = w->phys_cursor.hpos;
25969
25970 /* When the window is hscrolled, cursor hpos can legitimately be
25971 out of bounds, but we draw the cursor at the corresponding
25972 window margin in that case. */
25973 if (!row->reversed_p && hpos < 0)
25974 hpos = 0;
25975 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25976 hpos = row->used[TEXT_AREA] - 1;
25977
25978 BLOCK_INPUT;
25979 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
25980 w->phys_cursor.x, w->phys_cursor.y);
25981 UNBLOCK_INPUT;
25982 }
25983 #endif /* HAVE_WINDOW_SYSTEM */
25984 }
25985
25986 #ifdef HAVE_WINDOW_SYSTEM
25987 /* Change the mouse cursor. */
25988 if (FRAME_WINDOW_P (f))
25989 {
25990 if (draw == DRAW_NORMAL_TEXT
25991 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
25992 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
25993 else if (draw == DRAW_MOUSE_FACE)
25994 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
25995 else
25996 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
25997 }
25998 #endif /* HAVE_WINDOW_SYSTEM */
25999 }
26000
26001 /* EXPORT:
26002 Clear out the mouse-highlighted active region.
26003 Redraw it un-highlighted first. Value is non-zero if mouse
26004 face was actually drawn unhighlighted. */
26005
26006 int
26007 clear_mouse_face (Mouse_HLInfo *hlinfo)
26008 {
26009 int cleared = 0;
26010
26011 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26012 {
26013 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26014 cleared = 1;
26015 }
26016
26017 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26018 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26019 hlinfo->mouse_face_window = Qnil;
26020 hlinfo->mouse_face_overlay = Qnil;
26021 return cleared;
26022 }
26023
26024 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26025 within the mouse face on that window. */
26026 static int
26027 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26028 {
26029 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26030
26031 /* Quickly resolve the easy cases. */
26032 if (!(WINDOWP (hlinfo->mouse_face_window)
26033 && XWINDOW (hlinfo->mouse_face_window) == w))
26034 return 0;
26035 if (vpos < hlinfo->mouse_face_beg_row
26036 || vpos > hlinfo->mouse_face_end_row)
26037 return 0;
26038 if (vpos > hlinfo->mouse_face_beg_row
26039 && vpos < hlinfo->mouse_face_end_row)
26040 return 1;
26041
26042 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26043 {
26044 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26045 {
26046 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26047 return 1;
26048 }
26049 else if ((vpos == hlinfo->mouse_face_beg_row
26050 && hpos >= hlinfo->mouse_face_beg_col)
26051 || (vpos == hlinfo->mouse_face_end_row
26052 && hpos < hlinfo->mouse_face_end_col))
26053 return 1;
26054 }
26055 else
26056 {
26057 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26058 {
26059 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26060 return 1;
26061 }
26062 else if ((vpos == hlinfo->mouse_face_beg_row
26063 && hpos <= hlinfo->mouse_face_beg_col)
26064 || (vpos == hlinfo->mouse_face_end_row
26065 && hpos > hlinfo->mouse_face_end_col))
26066 return 1;
26067 }
26068 return 0;
26069 }
26070
26071
26072 /* EXPORT:
26073 Non-zero if physical cursor of window W is within mouse face. */
26074
26075 int
26076 cursor_in_mouse_face_p (struct window *w)
26077 {
26078 int hpos = w->phys_cursor.hpos;
26079 int vpos = w->phys_cursor.vpos;
26080 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26081
26082 /* When the window is hscrolled, cursor hpos can legitimately be out
26083 of bounds, but we draw the cursor at the corresponding window
26084 margin in that case. */
26085 if (!row->reversed_p && hpos < 0)
26086 hpos = 0;
26087 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26088 hpos = row->used[TEXT_AREA] - 1;
26089
26090 return coords_in_mouse_face_p (w, hpos, vpos);
26091 }
26092
26093
26094 \f
26095 /* Find the glyph rows START_ROW and END_ROW of window W that display
26096 characters between buffer positions START_CHARPOS and END_CHARPOS
26097 (excluding END_CHARPOS). DISP_STRING is a display string that
26098 covers these buffer positions. This is similar to
26099 row_containing_pos, but is more accurate when bidi reordering makes
26100 buffer positions change non-linearly with glyph rows. */
26101 static void
26102 rows_from_pos_range (struct window *w,
26103 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26104 Lisp_Object disp_string,
26105 struct glyph_row **start, struct glyph_row **end)
26106 {
26107 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26108 int last_y = window_text_bottom_y (w);
26109 struct glyph_row *row;
26110
26111 *start = NULL;
26112 *end = NULL;
26113
26114 while (!first->enabled_p
26115 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26116 first++;
26117
26118 /* Find the START row. */
26119 for (row = first;
26120 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26121 row++)
26122 {
26123 /* A row can potentially be the START row if the range of the
26124 characters it displays intersects the range
26125 [START_CHARPOS..END_CHARPOS). */
26126 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26127 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26128 /* See the commentary in row_containing_pos, for the
26129 explanation of the complicated way to check whether
26130 some position is beyond the end of the characters
26131 displayed by a row. */
26132 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26133 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26134 && !row->ends_at_zv_p
26135 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26136 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26137 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26138 && !row->ends_at_zv_p
26139 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26140 {
26141 /* Found a candidate row. Now make sure at least one of the
26142 glyphs it displays has a charpos from the range
26143 [START_CHARPOS..END_CHARPOS).
26144
26145 This is not obvious because bidi reordering could make
26146 buffer positions of a row be 1,2,3,102,101,100, and if we
26147 want to highlight characters in [50..60), we don't want
26148 this row, even though [50..60) does intersect [1..103),
26149 the range of character positions given by the row's start
26150 and end positions. */
26151 struct glyph *g = row->glyphs[TEXT_AREA];
26152 struct glyph *e = g + row->used[TEXT_AREA];
26153
26154 while (g < e)
26155 {
26156 if (((BUFFERP (g->object) || INTEGERP (g->object))
26157 && start_charpos <= g->charpos && g->charpos < end_charpos)
26158 /* A glyph that comes from DISP_STRING is by
26159 definition to be highlighted. */
26160 || EQ (g->object, disp_string))
26161 *start = row;
26162 g++;
26163 }
26164 if (*start)
26165 break;
26166 }
26167 }
26168
26169 /* Find the END row. */
26170 if (!*start
26171 /* If the last row is partially visible, start looking for END
26172 from that row, instead of starting from FIRST. */
26173 && !(row->enabled_p
26174 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26175 row = first;
26176 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26177 {
26178 struct glyph_row *next = row + 1;
26179 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26180
26181 if (!next->enabled_p
26182 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26183 /* The first row >= START whose range of displayed characters
26184 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26185 is the row END + 1. */
26186 || (start_charpos < next_start
26187 && end_charpos < next_start)
26188 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26189 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26190 && !next->ends_at_zv_p
26191 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26192 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26193 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26194 && !next->ends_at_zv_p
26195 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26196 {
26197 *end = row;
26198 break;
26199 }
26200 else
26201 {
26202 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26203 but none of the characters it displays are in the range, it is
26204 also END + 1. */
26205 struct glyph *g = next->glyphs[TEXT_AREA];
26206 struct glyph *s = g;
26207 struct glyph *e = g + next->used[TEXT_AREA];
26208
26209 while (g < e)
26210 {
26211 if (((BUFFERP (g->object) || INTEGERP (g->object))
26212 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26213 /* If the buffer position of the first glyph in
26214 the row is equal to END_CHARPOS, it means
26215 the last character to be highlighted is the
26216 newline of ROW, and we must consider NEXT as
26217 END, not END+1. */
26218 || (((!next->reversed_p && g == s)
26219 || (next->reversed_p && g == e - 1))
26220 && (g->charpos == end_charpos
26221 /* Special case for when NEXT is an
26222 empty line at ZV. */
26223 || (g->charpos == -1
26224 && !row->ends_at_zv_p
26225 && next_start == end_charpos)))))
26226 /* A glyph that comes from DISP_STRING is by
26227 definition to be highlighted. */
26228 || EQ (g->object, disp_string))
26229 break;
26230 g++;
26231 }
26232 if (g == e)
26233 {
26234 *end = row;
26235 break;
26236 }
26237 /* The first row that ends at ZV must be the last to be
26238 highlighted. */
26239 else if (next->ends_at_zv_p)
26240 {
26241 *end = next;
26242 break;
26243 }
26244 }
26245 }
26246 }
26247
26248 /* This function sets the mouse_face_* elements of HLINFO, assuming
26249 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26250 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26251 for the overlay or run of text properties specifying the mouse
26252 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26253 before-string and after-string that must also be highlighted.
26254 DISP_STRING, if non-nil, is a display string that may cover some
26255 or all of the highlighted text. */
26256
26257 static void
26258 mouse_face_from_buffer_pos (Lisp_Object window,
26259 Mouse_HLInfo *hlinfo,
26260 ptrdiff_t mouse_charpos,
26261 ptrdiff_t start_charpos,
26262 ptrdiff_t end_charpos,
26263 Lisp_Object before_string,
26264 Lisp_Object after_string,
26265 Lisp_Object disp_string)
26266 {
26267 struct window *w = XWINDOW (window);
26268 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26269 struct glyph_row *r1, *r2;
26270 struct glyph *glyph, *end;
26271 ptrdiff_t ignore, pos;
26272 int x;
26273
26274 xassert (NILP (disp_string) || STRINGP (disp_string));
26275 xassert (NILP (before_string) || STRINGP (before_string));
26276 xassert (NILP (after_string) || STRINGP (after_string));
26277
26278 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26279 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26280 if (r1 == NULL)
26281 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26282 /* If the before-string or display-string contains newlines,
26283 rows_from_pos_range skips to its last row. Move back. */
26284 if (!NILP (before_string) || !NILP (disp_string))
26285 {
26286 struct glyph_row *prev;
26287 while ((prev = r1 - 1, prev >= first)
26288 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26289 && prev->used[TEXT_AREA] > 0)
26290 {
26291 struct glyph *beg = prev->glyphs[TEXT_AREA];
26292 glyph = beg + prev->used[TEXT_AREA];
26293 while (--glyph >= beg && INTEGERP (glyph->object));
26294 if (glyph < beg
26295 || !(EQ (glyph->object, before_string)
26296 || EQ (glyph->object, disp_string)))
26297 break;
26298 r1 = prev;
26299 }
26300 }
26301 if (r2 == NULL)
26302 {
26303 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26304 hlinfo->mouse_face_past_end = 1;
26305 }
26306 else if (!NILP (after_string))
26307 {
26308 /* If the after-string has newlines, advance to its last row. */
26309 struct glyph_row *next;
26310 struct glyph_row *last
26311 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26312
26313 for (next = r2 + 1;
26314 next <= last
26315 && next->used[TEXT_AREA] > 0
26316 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26317 ++next)
26318 r2 = next;
26319 }
26320 /* The rest of the display engine assumes that mouse_face_beg_row is
26321 either above mouse_face_end_row or identical to it. But with
26322 bidi-reordered continued lines, the row for START_CHARPOS could
26323 be below the row for END_CHARPOS. If so, swap the rows and store
26324 them in correct order. */
26325 if (r1->y > r2->y)
26326 {
26327 struct glyph_row *tem = r2;
26328
26329 r2 = r1;
26330 r1 = tem;
26331 }
26332
26333 hlinfo->mouse_face_beg_y = r1->y;
26334 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26335 hlinfo->mouse_face_end_y = r2->y;
26336 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26337
26338 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26339 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26340 could be anywhere in the row and in any order. The strategy
26341 below is to find the leftmost and the rightmost glyph that
26342 belongs to either of these 3 strings, or whose position is
26343 between START_CHARPOS and END_CHARPOS, and highlight all the
26344 glyphs between those two. This may cover more than just the text
26345 between START_CHARPOS and END_CHARPOS if the range of characters
26346 strides the bidi level boundary, e.g. if the beginning is in R2L
26347 text while the end is in L2R text or vice versa. */
26348 if (!r1->reversed_p)
26349 {
26350 /* This row is in a left to right paragraph. Scan it left to
26351 right. */
26352 glyph = r1->glyphs[TEXT_AREA];
26353 end = glyph + r1->used[TEXT_AREA];
26354 x = r1->x;
26355
26356 /* Skip truncation glyphs at the start of the glyph row. */
26357 if (r1->displays_text_p)
26358 for (; glyph < end
26359 && INTEGERP (glyph->object)
26360 && glyph->charpos < 0;
26361 ++glyph)
26362 x += glyph->pixel_width;
26363
26364 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26365 or DISP_STRING, and the first glyph from buffer whose
26366 position is between START_CHARPOS and END_CHARPOS. */
26367 for (; glyph < end
26368 && !INTEGERP (glyph->object)
26369 && !EQ (glyph->object, disp_string)
26370 && !(BUFFERP (glyph->object)
26371 && (glyph->charpos >= start_charpos
26372 && glyph->charpos < end_charpos));
26373 ++glyph)
26374 {
26375 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26376 are present at buffer positions between START_CHARPOS and
26377 END_CHARPOS, or if they come from an overlay. */
26378 if (EQ (glyph->object, before_string))
26379 {
26380 pos = string_buffer_position (before_string,
26381 start_charpos);
26382 /* If pos == 0, it means before_string came from an
26383 overlay, not from a buffer position. */
26384 if (!pos || (pos >= start_charpos && pos < end_charpos))
26385 break;
26386 }
26387 else if (EQ (glyph->object, after_string))
26388 {
26389 pos = string_buffer_position (after_string, end_charpos);
26390 if (!pos || (pos >= start_charpos && pos < end_charpos))
26391 break;
26392 }
26393 x += glyph->pixel_width;
26394 }
26395 hlinfo->mouse_face_beg_x = x;
26396 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26397 }
26398 else
26399 {
26400 /* This row is in a right to left paragraph. Scan it right to
26401 left. */
26402 struct glyph *g;
26403
26404 end = r1->glyphs[TEXT_AREA] - 1;
26405 glyph = end + r1->used[TEXT_AREA];
26406
26407 /* Skip truncation glyphs at the start of the glyph row. */
26408 if (r1->displays_text_p)
26409 for (; glyph > end
26410 && INTEGERP (glyph->object)
26411 && glyph->charpos < 0;
26412 --glyph)
26413 ;
26414
26415 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26416 or DISP_STRING, and the first glyph from buffer whose
26417 position is between START_CHARPOS and END_CHARPOS. */
26418 for (; glyph > end
26419 && !INTEGERP (glyph->object)
26420 && !EQ (glyph->object, disp_string)
26421 && !(BUFFERP (glyph->object)
26422 && (glyph->charpos >= start_charpos
26423 && glyph->charpos < end_charpos));
26424 --glyph)
26425 {
26426 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26427 are present at buffer positions between START_CHARPOS and
26428 END_CHARPOS, or if they come from an overlay. */
26429 if (EQ (glyph->object, before_string))
26430 {
26431 pos = string_buffer_position (before_string, start_charpos);
26432 /* If pos == 0, it means before_string came from an
26433 overlay, not from a buffer position. */
26434 if (!pos || (pos >= start_charpos && pos < end_charpos))
26435 break;
26436 }
26437 else if (EQ (glyph->object, after_string))
26438 {
26439 pos = string_buffer_position (after_string, end_charpos);
26440 if (!pos || (pos >= start_charpos && pos < end_charpos))
26441 break;
26442 }
26443 }
26444
26445 glyph++; /* first glyph to the right of the highlighted area */
26446 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26447 x += g->pixel_width;
26448 hlinfo->mouse_face_beg_x = x;
26449 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26450 }
26451
26452 /* If the highlight ends in a different row, compute GLYPH and END
26453 for the end row. Otherwise, reuse the values computed above for
26454 the row where the highlight begins. */
26455 if (r2 != r1)
26456 {
26457 if (!r2->reversed_p)
26458 {
26459 glyph = r2->glyphs[TEXT_AREA];
26460 end = glyph + r2->used[TEXT_AREA];
26461 x = r2->x;
26462 }
26463 else
26464 {
26465 end = r2->glyphs[TEXT_AREA] - 1;
26466 glyph = end + r2->used[TEXT_AREA];
26467 }
26468 }
26469
26470 if (!r2->reversed_p)
26471 {
26472 /* Skip truncation and continuation glyphs near the end of the
26473 row, and also blanks and stretch glyphs inserted by
26474 extend_face_to_end_of_line. */
26475 while (end > glyph
26476 && INTEGERP ((end - 1)->object))
26477 --end;
26478 /* Scan the rest of the glyph row from the end, looking for the
26479 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26480 DISP_STRING, or whose position is between START_CHARPOS
26481 and END_CHARPOS */
26482 for (--end;
26483 end > glyph
26484 && !INTEGERP (end->object)
26485 && !EQ (end->object, disp_string)
26486 && !(BUFFERP (end->object)
26487 && (end->charpos >= start_charpos
26488 && end->charpos < end_charpos));
26489 --end)
26490 {
26491 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26492 are present at buffer positions between START_CHARPOS and
26493 END_CHARPOS, or if they come from an overlay. */
26494 if (EQ (end->object, before_string))
26495 {
26496 pos = string_buffer_position (before_string, start_charpos);
26497 if (!pos || (pos >= start_charpos && pos < end_charpos))
26498 break;
26499 }
26500 else if (EQ (end->object, after_string))
26501 {
26502 pos = string_buffer_position (after_string, end_charpos);
26503 if (!pos || (pos >= start_charpos && pos < end_charpos))
26504 break;
26505 }
26506 }
26507 /* Find the X coordinate of the last glyph to be highlighted. */
26508 for (; glyph <= end; ++glyph)
26509 x += glyph->pixel_width;
26510
26511 hlinfo->mouse_face_end_x = x;
26512 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26513 }
26514 else
26515 {
26516 /* Skip truncation and continuation glyphs near the end of the
26517 row, and also blanks and stretch glyphs inserted by
26518 extend_face_to_end_of_line. */
26519 x = r2->x;
26520 end++;
26521 while (end < glyph
26522 && INTEGERP (end->object))
26523 {
26524 x += end->pixel_width;
26525 ++end;
26526 }
26527 /* Scan the rest of the glyph row from the end, looking for the
26528 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26529 DISP_STRING, or whose position is between START_CHARPOS
26530 and END_CHARPOS */
26531 for ( ;
26532 end < glyph
26533 && !INTEGERP (end->object)
26534 && !EQ (end->object, disp_string)
26535 && !(BUFFERP (end->object)
26536 && (end->charpos >= start_charpos
26537 && end->charpos < end_charpos));
26538 ++end)
26539 {
26540 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26541 are present at buffer positions between START_CHARPOS and
26542 END_CHARPOS, or if they come from an overlay. */
26543 if (EQ (end->object, before_string))
26544 {
26545 pos = string_buffer_position (before_string, start_charpos);
26546 if (!pos || (pos >= start_charpos && pos < end_charpos))
26547 break;
26548 }
26549 else if (EQ (end->object, after_string))
26550 {
26551 pos = string_buffer_position (after_string, end_charpos);
26552 if (!pos || (pos >= start_charpos && pos < end_charpos))
26553 break;
26554 }
26555 x += end->pixel_width;
26556 }
26557 /* If we exited the above loop because we arrived at the last
26558 glyph of the row, and its buffer position is still not in
26559 range, it means the last character in range is the preceding
26560 newline. Bump the end column and x values to get past the
26561 last glyph. */
26562 if (end == glyph
26563 && BUFFERP (end->object)
26564 && (end->charpos < start_charpos
26565 || end->charpos >= end_charpos))
26566 {
26567 x += end->pixel_width;
26568 ++end;
26569 }
26570 hlinfo->mouse_face_end_x = x;
26571 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26572 }
26573
26574 hlinfo->mouse_face_window = window;
26575 hlinfo->mouse_face_face_id
26576 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26577 mouse_charpos + 1,
26578 !hlinfo->mouse_face_hidden, -1);
26579 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26580 }
26581
26582 /* The following function is not used anymore (replaced with
26583 mouse_face_from_string_pos), but I leave it here for the time
26584 being, in case someone would. */
26585
26586 #if 0 /* not used */
26587
26588 /* Find the position of the glyph for position POS in OBJECT in
26589 window W's current matrix, and return in *X, *Y the pixel
26590 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26591
26592 RIGHT_P non-zero means return the position of the right edge of the
26593 glyph, RIGHT_P zero means return the left edge position.
26594
26595 If no glyph for POS exists in the matrix, return the position of
26596 the glyph with the next smaller position that is in the matrix, if
26597 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26598 exists in the matrix, return the position of the glyph with the
26599 next larger position in OBJECT.
26600
26601 Value is non-zero if a glyph was found. */
26602
26603 static int
26604 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26605 int *hpos, int *vpos, int *x, int *y, int right_p)
26606 {
26607 int yb = window_text_bottom_y (w);
26608 struct glyph_row *r;
26609 struct glyph *best_glyph = NULL;
26610 struct glyph_row *best_row = NULL;
26611 int best_x = 0;
26612
26613 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26614 r->enabled_p && r->y < yb;
26615 ++r)
26616 {
26617 struct glyph *g = r->glyphs[TEXT_AREA];
26618 struct glyph *e = g + r->used[TEXT_AREA];
26619 int gx;
26620
26621 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26622 if (EQ (g->object, object))
26623 {
26624 if (g->charpos == pos)
26625 {
26626 best_glyph = g;
26627 best_x = gx;
26628 best_row = r;
26629 goto found;
26630 }
26631 else if (best_glyph == NULL
26632 || ((eabs (g->charpos - pos)
26633 < eabs (best_glyph->charpos - pos))
26634 && (right_p
26635 ? g->charpos < pos
26636 : g->charpos > pos)))
26637 {
26638 best_glyph = g;
26639 best_x = gx;
26640 best_row = r;
26641 }
26642 }
26643 }
26644
26645 found:
26646
26647 if (best_glyph)
26648 {
26649 *x = best_x;
26650 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26651
26652 if (right_p)
26653 {
26654 *x += best_glyph->pixel_width;
26655 ++*hpos;
26656 }
26657
26658 *y = best_row->y;
26659 *vpos = best_row - w->current_matrix->rows;
26660 }
26661
26662 return best_glyph != NULL;
26663 }
26664 #endif /* not used */
26665
26666 /* Find the positions of the first and the last glyphs in window W's
26667 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26668 (assumed to be a string), and return in HLINFO's mouse_face_*
26669 members the pixel and column/row coordinates of those glyphs. */
26670
26671 static void
26672 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26673 Lisp_Object object,
26674 ptrdiff_t startpos, ptrdiff_t endpos)
26675 {
26676 int yb = window_text_bottom_y (w);
26677 struct glyph_row *r;
26678 struct glyph *g, *e;
26679 int gx;
26680 int found = 0;
26681
26682 /* Find the glyph row with at least one position in the range
26683 [STARTPOS..ENDPOS], and the first glyph in that row whose
26684 position belongs to that range. */
26685 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26686 r->enabled_p && r->y < yb;
26687 ++r)
26688 {
26689 if (!r->reversed_p)
26690 {
26691 g = r->glyphs[TEXT_AREA];
26692 e = g + r->used[TEXT_AREA];
26693 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26694 if (EQ (g->object, object)
26695 && startpos <= g->charpos && g->charpos <= endpos)
26696 {
26697 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26698 hlinfo->mouse_face_beg_y = r->y;
26699 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26700 hlinfo->mouse_face_beg_x = gx;
26701 found = 1;
26702 break;
26703 }
26704 }
26705 else
26706 {
26707 struct glyph *g1;
26708
26709 e = r->glyphs[TEXT_AREA];
26710 g = e + r->used[TEXT_AREA];
26711 for ( ; g > e; --g)
26712 if (EQ ((g-1)->object, object)
26713 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26714 {
26715 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26716 hlinfo->mouse_face_beg_y = r->y;
26717 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26718 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26719 gx += g1->pixel_width;
26720 hlinfo->mouse_face_beg_x = gx;
26721 found = 1;
26722 break;
26723 }
26724 }
26725 if (found)
26726 break;
26727 }
26728
26729 if (!found)
26730 return;
26731
26732 /* Starting with the next row, look for the first row which does NOT
26733 include any glyphs whose positions are in the range. */
26734 for (++r; r->enabled_p && r->y < yb; ++r)
26735 {
26736 g = r->glyphs[TEXT_AREA];
26737 e = g + r->used[TEXT_AREA];
26738 found = 0;
26739 for ( ; g < e; ++g)
26740 if (EQ (g->object, object)
26741 && startpos <= g->charpos && g->charpos <= endpos)
26742 {
26743 found = 1;
26744 break;
26745 }
26746 if (!found)
26747 break;
26748 }
26749
26750 /* The highlighted region ends on the previous row. */
26751 r--;
26752
26753 /* Set the end row and its vertical pixel coordinate. */
26754 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26755 hlinfo->mouse_face_end_y = r->y;
26756
26757 /* Compute and set the end column and the end column's horizontal
26758 pixel coordinate. */
26759 if (!r->reversed_p)
26760 {
26761 g = r->glyphs[TEXT_AREA];
26762 e = g + r->used[TEXT_AREA];
26763 for ( ; e > g; --e)
26764 if (EQ ((e-1)->object, object)
26765 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26766 break;
26767 hlinfo->mouse_face_end_col = e - g;
26768
26769 for (gx = r->x; g < e; ++g)
26770 gx += g->pixel_width;
26771 hlinfo->mouse_face_end_x = gx;
26772 }
26773 else
26774 {
26775 e = r->glyphs[TEXT_AREA];
26776 g = e + r->used[TEXT_AREA];
26777 for (gx = r->x ; e < g; ++e)
26778 {
26779 if (EQ (e->object, object)
26780 && startpos <= e->charpos && e->charpos <= endpos)
26781 break;
26782 gx += e->pixel_width;
26783 }
26784 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
26785 hlinfo->mouse_face_end_x = gx;
26786 }
26787 }
26788
26789 #ifdef HAVE_WINDOW_SYSTEM
26790
26791 /* See if position X, Y is within a hot-spot of an image. */
26792
26793 static int
26794 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
26795 {
26796 if (!CONSP (hot_spot))
26797 return 0;
26798
26799 if (EQ (XCAR (hot_spot), Qrect))
26800 {
26801 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
26802 Lisp_Object rect = XCDR (hot_spot);
26803 Lisp_Object tem;
26804 if (!CONSP (rect))
26805 return 0;
26806 if (!CONSP (XCAR (rect)))
26807 return 0;
26808 if (!CONSP (XCDR (rect)))
26809 return 0;
26810 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
26811 return 0;
26812 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
26813 return 0;
26814 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
26815 return 0;
26816 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
26817 return 0;
26818 return 1;
26819 }
26820 else if (EQ (XCAR (hot_spot), Qcircle))
26821 {
26822 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
26823 Lisp_Object circ = XCDR (hot_spot);
26824 Lisp_Object lr, lx0, ly0;
26825 if (CONSP (circ)
26826 && CONSP (XCAR (circ))
26827 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
26828 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
26829 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
26830 {
26831 double r = XFLOATINT (lr);
26832 double dx = XINT (lx0) - x;
26833 double dy = XINT (ly0) - y;
26834 return (dx * dx + dy * dy <= r * r);
26835 }
26836 }
26837 else if (EQ (XCAR (hot_spot), Qpoly))
26838 {
26839 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
26840 if (VECTORP (XCDR (hot_spot)))
26841 {
26842 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
26843 Lisp_Object *poly = v->contents;
26844 ptrdiff_t n = v->header.size;
26845 ptrdiff_t i;
26846 int inside = 0;
26847 Lisp_Object lx, ly;
26848 int x0, y0;
26849
26850 /* Need an even number of coordinates, and at least 3 edges. */
26851 if (n < 6 || n & 1)
26852 return 0;
26853
26854 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
26855 If count is odd, we are inside polygon. Pixels on edges
26856 may or may not be included depending on actual geometry of the
26857 polygon. */
26858 if ((lx = poly[n-2], !INTEGERP (lx))
26859 || (ly = poly[n-1], !INTEGERP (lx)))
26860 return 0;
26861 x0 = XINT (lx), y0 = XINT (ly);
26862 for (i = 0; i < n; i += 2)
26863 {
26864 int x1 = x0, y1 = y0;
26865 if ((lx = poly[i], !INTEGERP (lx))
26866 || (ly = poly[i+1], !INTEGERP (ly)))
26867 return 0;
26868 x0 = XINT (lx), y0 = XINT (ly);
26869
26870 /* Does this segment cross the X line? */
26871 if (x0 >= x)
26872 {
26873 if (x1 >= x)
26874 continue;
26875 }
26876 else if (x1 < x)
26877 continue;
26878 if (y > y0 && y > y1)
26879 continue;
26880 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
26881 inside = !inside;
26882 }
26883 return inside;
26884 }
26885 }
26886 return 0;
26887 }
26888
26889 Lisp_Object
26890 find_hot_spot (Lisp_Object map, int x, int y)
26891 {
26892 while (CONSP (map))
26893 {
26894 if (CONSP (XCAR (map))
26895 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
26896 return XCAR (map);
26897 map = XCDR (map);
26898 }
26899
26900 return Qnil;
26901 }
26902
26903 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
26904 3, 3, 0,
26905 doc: /* Lookup in image map MAP coordinates X and Y.
26906 An image map is an alist where each element has the format (AREA ID PLIST).
26907 An AREA is specified as either a rectangle, a circle, or a polygon:
26908 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
26909 pixel coordinates of the upper left and bottom right corners.
26910 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
26911 and the radius of the circle; r may be a float or integer.
26912 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
26913 vector describes one corner in the polygon.
26914 Returns the alist element for the first matching AREA in MAP. */)
26915 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
26916 {
26917 if (NILP (map))
26918 return Qnil;
26919
26920 CHECK_NUMBER (x);
26921 CHECK_NUMBER (y);
26922
26923 return find_hot_spot (map,
26924 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
26925 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
26926 }
26927
26928
26929 /* Display frame CURSOR, optionally using shape defined by POINTER. */
26930 static void
26931 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
26932 {
26933 /* Do not change cursor shape while dragging mouse. */
26934 if (!NILP (do_mouse_tracking))
26935 return;
26936
26937 if (!NILP (pointer))
26938 {
26939 if (EQ (pointer, Qarrow))
26940 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26941 else if (EQ (pointer, Qhand))
26942 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
26943 else if (EQ (pointer, Qtext))
26944 cursor = FRAME_X_OUTPUT (f)->text_cursor;
26945 else if (EQ (pointer, intern ("hdrag")))
26946 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
26947 #ifdef HAVE_X_WINDOWS
26948 else if (EQ (pointer, intern ("vdrag")))
26949 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
26950 #endif
26951 else if (EQ (pointer, intern ("hourglass")))
26952 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
26953 else if (EQ (pointer, Qmodeline))
26954 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
26955 else
26956 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
26957 }
26958
26959 if (cursor != No_Cursor)
26960 FRAME_RIF (f)->define_frame_cursor (f, cursor);
26961 }
26962
26963 #endif /* HAVE_WINDOW_SYSTEM */
26964
26965 /* Take proper action when mouse has moved to the mode or header line
26966 or marginal area AREA of window W, x-position X and y-position Y.
26967 X is relative to the start of the text display area of W, so the
26968 width of bitmap areas and scroll bars must be subtracted to get a
26969 position relative to the start of the mode line. */
26970
26971 static void
26972 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
26973 enum window_part area)
26974 {
26975 struct window *w = XWINDOW (window);
26976 struct frame *f = XFRAME (w->frame);
26977 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26978 #ifdef HAVE_WINDOW_SYSTEM
26979 Display_Info *dpyinfo;
26980 #endif
26981 Cursor cursor = No_Cursor;
26982 Lisp_Object pointer = Qnil;
26983 int dx, dy, width, height;
26984 ptrdiff_t charpos;
26985 Lisp_Object string, object = Qnil;
26986 Lisp_Object pos IF_LINT (= Qnil), help;
26987
26988 Lisp_Object mouse_face;
26989 int original_x_pixel = x;
26990 struct glyph * glyph = NULL, * row_start_glyph = NULL;
26991 struct glyph_row *row IF_LINT (= 0);
26992
26993 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
26994 {
26995 int x0;
26996 struct glyph *end;
26997
26998 /* Kludge alert: mode_line_string takes X/Y in pixels, but
26999 returns them in row/column units! */
27000 string = mode_line_string (w, area, &x, &y, &charpos,
27001 &object, &dx, &dy, &width, &height);
27002
27003 row = (area == ON_MODE_LINE
27004 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27005 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27006
27007 /* Find the glyph under the mouse pointer. */
27008 if (row->mode_line_p && row->enabled_p)
27009 {
27010 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27011 end = glyph + row->used[TEXT_AREA];
27012
27013 for (x0 = original_x_pixel;
27014 glyph < end && x0 >= glyph->pixel_width;
27015 ++glyph)
27016 x0 -= glyph->pixel_width;
27017
27018 if (glyph >= end)
27019 glyph = NULL;
27020 }
27021 }
27022 else
27023 {
27024 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27025 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27026 returns them in row/column units! */
27027 string = marginal_area_string (w, area, &x, &y, &charpos,
27028 &object, &dx, &dy, &width, &height);
27029 }
27030
27031 help = Qnil;
27032
27033 #ifdef HAVE_WINDOW_SYSTEM
27034 if (IMAGEP (object))
27035 {
27036 Lisp_Object image_map, hotspot;
27037 if ((image_map = Fplist_get (XCDR (object), QCmap),
27038 !NILP (image_map))
27039 && (hotspot = find_hot_spot (image_map, dx, dy),
27040 CONSP (hotspot))
27041 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27042 {
27043 Lisp_Object plist;
27044
27045 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27046 If so, we could look for mouse-enter, mouse-leave
27047 properties in PLIST (and do something...). */
27048 hotspot = XCDR (hotspot);
27049 if (CONSP (hotspot)
27050 && (plist = XCAR (hotspot), CONSP (plist)))
27051 {
27052 pointer = Fplist_get (plist, Qpointer);
27053 if (NILP (pointer))
27054 pointer = Qhand;
27055 help = Fplist_get (plist, Qhelp_echo);
27056 if (!NILP (help))
27057 {
27058 help_echo_string = help;
27059 XSETWINDOW (help_echo_window, w);
27060 help_echo_object = w->buffer;
27061 help_echo_pos = charpos;
27062 }
27063 }
27064 }
27065 if (NILP (pointer))
27066 pointer = Fplist_get (XCDR (object), QCpointer);
27067 }
27068 #endif /* HAVE_WINDOW_SYSTEM */
27069
27070 if (STRINGP (string))
27071 pos = make_number (charpos);
27072
27073 /* Set the help text and mouse pointer. If the mouse is on a part
27074 of the mode line without any text (e.g. past the right edge of
27075 the mode line text), use the default help text and pointer. */
27076 if (STRINGP (string) || area == ON_MODE_LINE)
27077 {
27078 /* Arrange to display the help by setting the global variables
27079 help_echo_string, help_echo_object, and help_echo_pos. */
27080 if (NILP (help))
27081 {
27082 if (STRINGP (string))
27083 help = Fget_text_property (pos, Qhelp_echo, string);
27084
27085 if (!NILP (help))
27086 {
27087 help_echo_string = help;
27088 XSETWINDOW (help_echo_window, w);
27089 help_echo_object = string;
27090 help_echo_pos = charpos;
27091 }
27092 else if (area == ON_MODE_LINE)
27093 {
27094 Lisp_Object default_help
27095 = buffer_local_value_1 (Qmode_line_default_help_echo,
27096 w->buffer);
27097
27098 if (STRINGP (default_help))
27099 {
27100 help_echo_string = default_help;
27101 XSETWINDOW (help_echo_window, w);
27102 help_echo_object = Qnil;
27103 help_echo_pos = -1;
27104 }
27105 }
27106 }
27107
27108 #ifdef HAVE_WINDOW_SYSTEM
27109 /* Change the mouse pointer according to what is under it. */
27110 if (FRAME_WINDOW_P (f))
27111 {
27112 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27113 if (STRINGP (string))
27114 {
27115 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27116
27117 if (NILP (pointer))
27118 pointer = Fget_text_property (pos, Qpointer, string);
27119
27120 /* Change the mouse pointer according to what is under X/Y. */
27121 if (NILP (pointer)
27122 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27123 {
27124 Lisp_Object map;
27125 map = Fget_text_property (pos, Qlocal_map, string);
27126 if (!KEYMAPP (map))
27127 map = Fget_text_property (pos, Qkeymap, string);
27128 if (!KEYMAPP (map))
27129 cursor = dpyinfo->vertical_scroll_bar_cursor;
27130 }
27131 }
27132 else
27133 /* Default mode-line pointer. */
27134 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27135 }
27136 #endif
27137 }
27138
27139 /* Change the mouse face according to what is under X/Y. */
27140 if (STRINGP (string))
27141 {
27142 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27143 if (!NILP (mouse_face)
27144 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27145 && glyph)
27146 {
27147 Lisp_Object b, e;
27148
27149 struct glyph * tmp_glyph;
27150
27151 int gpos;
27152 int gseq_length;
27153 int total_pixel_width;
27154 ptrdiff_t begpos, endpos, ignore;
27155
27156 int vpos, hpos;
27157
27158 b = Fprevious_single_property_change (make_number (charpos + 1),
27159 Qmouse_face, string, Qnil);
27160 if (NILP (b))
27161 begpos = 0;
27162 else
27163 begpos = XINT (b);
27164
27165 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27166 if (NILP (e))
27167 endpos = SCHARS (string);
27168 else
27169 endpos = XINT (e);
27170
27171 /* Calculate the glyph position GPOS of GLYPH in the
27172 displayed string, relative to the beginning of the
27173 highlighted part of the string.
27174
27175 Note: GPOS is different from CHARPOS. CHARPOS is the
27176 position of GLYPH in the internal string object. A mode
27177 line string format has structures which are converted to
27178 a flattened string by the Emacs Lisp interpreter. The
27179 internal string is an element of those structures. The
27180 displayed string is the flattened string. */
27181 tmp_glyph = row_start_glyph;
27182 while (tmp_glyph < glyph
27183 && (!(EQ (tmp_glyph->object, glyph->object)
27184 && begpos <= tmp_glyph->charpos
27185 && tmp_glyph->charpos < endpos)))
27186 tmp_glyph++;
27187 gpos = glyph - tmp_glyph;
27188
27189 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27190 the highlighted part of the displayed string to which
27191 GLYPH belongs. Note: GSEQ_LENGTH is different from
27192 SCHARS (STRING), because the latter returns the length of
27193 the internal string. */
27194 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27195 tmp_glyph > glyph
27196 && (!(EQ (tmp_glyph->object, glyph->object)
27197 && begpos <= tmp_glyph->charpos
27198 && tmp_glyph->charpos < endpos));
27199 tmp_glyph--)
27200 ;
27201 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27202
27203 /* Calculate the total pixel width of all the glyphs between
27204 the beginning of the highlighted area and GLYPH. */
27205 total_pixel_width = 0;
27206 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27207 total_pixel_width += tmp_glyph->pixel_width;
27208
27209 /* Pre calculation of re-rendering position. Note: X is in
27210 column units here, after the call to mode_line_string or
27211 marginal_area_string. */
27212 hpos = x - gpos;
27213 vpos = (area == ON_MODE_LINE
27214 ? (w->current_matrix)->nrows - 1
27215 : 0);
27216
27217 /* If GLYPH's position is included in the region that is
27218 already drawn in mouse face, we have nothing to do. */
27219 if ( EQ (window, hlinfo->mouse_face_window)
27220 && (!row->reversed_p
27221 ? (hlinfo->mouse_face_beg_col <= hpos
27222 && hpos < hlinfo->mouse_face_end_col)
27223 /* In R2L rows we swap BEG and END, see below. */
27224 : (hlinfo->mouse_face_end_col <= hpos
27225 && hpos < hlinfo->mouse_face_beg_col))
27226 && hlinfo->mouse_face_beg_row == vpos )
27227 return;
27228
27229 if (clear_mouse_face (hlinfo))
27230 cursor = No_Cursor;
27231
27232 if (!row->reversed_p)
27233 {
27234 hlinfo->mouse_face_beg_col = hpos;
27235 hlinfo->mouse_face_beg_x = original_x_pixel
27236 - (total_pixel_width + dx);
27237 hlinfo->mouse_face_end_col = hpos + gseq_length;
27238 hlinfo->mouse_face_end_x = 0;
27239 }
27240 else
27241 {
27242 /* In R2L rows, show_mouse_face expects BEG and END
27243 coordinates to be swapped. */
27244 hlinfo->mouse_face_end_col = hpos;
27245 hlinfo->mouse_face_end_x = original_x_pixel
27246 - (total_pixel_width + dx);
27247 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27248 hlinfo->mouse_face_beg_x = 0;
27249 }
27250
27251 hlinfo->mouse_face_beg_row = vpos;
27252 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27253 hlinfo->mouse_face_beg_y = 0;
27254 hlinfo->mouse_face_end_y = 0;
27255 hlinfo->mouse_face_past_end = 0;
27256 hlinfo->mouse_face_window = window;
27257
27258 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27259 charpos,
27260 0, 0, 0,
27261 &ignore,
27262 glyph->face_id,
27263 1);
27264 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27265
27266 if (NILP (pointer))
27267 pointer = Qhand;
27268 }
27269 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27270 clear_mouse_face (hlinfo);
27271 }
27272 #ifdef HAVE_WINDOW_SYSTEM
27273 if (FRAME_WINDOW_P (f))
27274 define_frame_cursor1 (f, cursor, pointer);
27275 #endif
27276 }
27277
27278
27279 /* EXPORT:
27280 Take proper action when the mouse has moved to position X, Y on
27281 frame F as regards highlighting characters that have mouse-face
27282 properties. Also de-highlighting chars where the mouse was before.
27283 X and Y can be negative or out of range. */
27284
27285 void
27286 note_mouse_highlight (struct frame *f, int x, int y)
27287 {
27288 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27289 enum window_part part = ON_NOTHING;
27290 Lisp_Object window;
27291 struct window *w;
27292 Cursor cursor = No_Cursor;
27293 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27294 struct buffer *b;
27295
27296 /* When a menu is active, don't highlight because this looks odd. */
27297 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27298 if (popup_activated ())
27299 return;
27300 #endif
27301
27302 if (NILP (Vmouse_highlight)
27303 || !f->glyphs_initialized_p
27304 || f->pointer_invisible)
27305 return;
27306
27307 hlinfo->mouse_face_mouse_x = x;
27308 hlinfo->mouse_face_mouse_y = y;
27309 hlinfo->mouse_face_mouse_frame = f;
27310
27311 if (hlinfo->mouse_face_defer)
27312 return;
27313
27314 if (gc_in_progress)
27315 {
27316 hlinfo->mouse_face_deferred_gc = 1;
27317 return;
27318 }
27319
27320 /* Which window is that in? */
27321 window = window_from_coordinates (f, x, y, &part, 1);
27322
27323 /* If displaying active text in another window, clear that. */
27324 if (! EQ (window, hlinfo->mouse_face_window)
27325 /* Also clear if we move out of text area in same window. */
27326 || (!NILP (hlinfo->mouse_face_window)
27327 && !NILP (window)
27328 && part != ON_TEXT
27329 && part != ON_MODE_LINE
27330 && part != ON_HEADER_LINE))
27331 clear_mouse_face (hlinfo);
27332
27333 /* Not on a window -> return. */
27334 if (!WINDOWP (window))
27335 return;
27336
27337 /* Reset help_echo_string. It will get recomputed below. */
27338 help_echo_string = Qnil;
27339
27340 /* Convert to window-relative pixel coordinates. */
27341 w = XWINDOW (window);
27342 frame_to_window_pixel_xy (w, &x, &y);
27343
27344 #ifdef HAVE_WINDOW_SYSTEM
27345 /* Handle tool-bar window differently since it doesn't display a
27346 buffer. */
27347 if (EQ (window, f->tool_bar_window))
27348 {
27349 note_tool_bar_highlight (f, x, y);
27350 return;
27351 }
27352 #endif
27353
27354 /* Mouse is on the mode, header line or margin? */
27355 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27356 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27357 {
27358 note_mode_line_or_margin_highlight (window, x, y, part);
27359 return;
27360 }
27361
27362 #ifdef HAVE_WINDOW_SYSTEM
27363 if (part == ON_VERTICAL_BORDER)
27364 {
27365 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27366 help_echo_string = build_string ("drag-mouse-1: resize");
27367 }
27368 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27369 || part == ON_SCROLL_BAR)
27370 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27371 else
27372 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27373 #endif
27374
27375 /* Are we in a window whose display is up to date?
27376 And verify the buffer's text has not changed. */
27377 b = XBUFFER (w->buffer);
27378 if (part == ON_TEXT
27379 && EQ (w->window_end_valid, w->buffer)
27380 && XFASTINT (w->last_modified) == BUF_MODIFF (b)
27381 && XFASTINT (w->last_overlay_modified) == BUF_OVERLAY_MODIFF (b))
27382 {
27383 int hpos, vpos, dx, dy, area = LAST_AREA;
27384 ptrdiff_t pos;
27385 struct glyph *glyph;
27386 Lisp_Object object;
27387 Lisp_Object mouse_face = Qnil, position;
27388 Lisp_Object *overlay_vec = NULL;
27389 ptrdiff_t i, noverlays;
27390 struct buffer *obuf;
27391 ptrdiff_t obegv, ozv;
27392 int same_region;
27393
27394 /* Find the glyph under X/Y. */
27395 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27396
27397 #ifdef HAVE_WINDOW_SYSTEM
27398 /* Look for :pointer property on image. */
27399 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27400 {
27401 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27402 if (img != NULL && IMAGEP (img->spec))
27403 {
27404 Lisp_Object image_map, hotspot;
27405 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27406 !NILP (image_map))
27407 && (hotspot = find_hot_spot (image_map,
27408 glyph->slice.img.x + dx,
27409 glyph->slice.img.y + dy),
27410 CONSP (hotspot))
27411 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27412 {
27413 Lisp_Object plist;
27414
27415 /* Could check XCAR (hotspot) to see if we enter/leave
27416 this hot-spot.
27417 If so, we could look for mouse-enter, mouse-leave
27418 properties in PLIST (and do something...). */
27419 hotspot = XCDR (hotspot);
27420 if (CONSP (hotspot)
27421 && (plist = XCAR (hotspot), CONSP (plist)))
27422 {
27423 pointer = Fplist_get (plist, Qpointer);
27424 if (NILP (pointer))
27425 pointer = Qhand;
27426 help_echo_string = Fplist_get (plist, Qhelp_echo);
27427 if (!NILP (help_echo_string))
27428 {
27429 help_echo_window = window;
27430 help_echo_object = glyph->object;
27431 help_echo_pos = glyph->charpos;
27432 }
27433 }
27434 }
27435 if (NILP (pointer))
27436 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27437 }
27438 }
27439 #endif /* HAVE_WINDOW_SYSTEM */
27440
27441 /* Clear mouse face if X/Y not over text. */
27442 if (glyph == NULL
27443 || area != TEXT_AREA
27444 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27445 /* Glyph's OBJECT is an integer for glyphs inserted by the
27446 display engine for its internal purposes, like truncation
27447 and continuation glyphs and blanks beyond the end of
27448 line's text on text terminals. If we are over such a
27449 glyph, we are not over any text. */
27450 || INTEGERP (glyph->object)
27451 /* R2L rows have a stretch glyph at their front, which
27452 stands for no text, whereas L2R rows have no glyphs at
27453 all beyond the end of text. Treat such stretch glyphs
27454 like we do with NULL glyphs in L2R rows. */
27455 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27456 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27457 && glyph->type == STRETCH_GLYPH
27458 && glyph->avoid_cursor_p))
27459 {
27460 if (clear_mouse_face (hlinfo))
27461 cursor = No_Cursor;
27462 #ifdef HAVE_WINDOW_SYSTEM
27463 if (FRAME_WINDOW_P (f) && NILP (pointer))
27464 {
27465 if (area != TEXT_AREA)
27466 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27467 else
27468 pointer = Vvoid_text_area_pointer;
27469 }
27470 #endif
27471 goto set_cursor;
27472 }
27473
27474 pos = glyph->charpos;
27475 object = glyph->object;
27476 if (!STRINGP (object) && !BUFFERP (object))
27477 goto set_cursor;
27478
27479 /* If we get an out-of-range value, return now; avoid an error. */
27480 if (BUFFERP (object) && pos > BUF_Z (b))
27481 goto set_cursor;
27482
27483 /* Make the window's buffer temporarily current for
27484 overlays_at and compute_char_face. */
27485 obuf = current_buffer;
27486 current_buffer = b;
27487 obegv = BEGV;
27488 ozv = ZV;
27489 BEGV = BEG;
27490 ZV = Z;
27491
27492 /* Is this char mouse-active or does it have help-echo? */
27493 position = make_number (pos);
27494
27495 if (BUFFERP (object))
27496 {
27497 /* Put all the overlays we want in a vector in overlay_vec. */
27498 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27499 /* Sort overlays into increasing priority order. */
27500 noverlays = sort_overlays (overlay_vec, noverlays, w);
27501 }
27502 else
27503 noverlays = 0;
27504
27505 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27506
27507 if (same_region)
27508 cursor = No_Cursor;
27509
27510 /* Check mouse-face highlighting. */
27511 if (! same_region
27512 /* If there exists an overlay with mouse-face overlapping
27513 the one we are currently highlighting, we have to
27514 check if we enter the overlapping overlay, and then
27515 highlight only that. */
27516 || (OVERLAYP (hlinfo->mouse_face_overlay)
27517 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27518 {
27519 /* Find the highest priority overlay with a mouse-face. */
27520 Lisp_Object overlay = Qnil;
27521 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27522 {
27523 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27524 if (!NILP (mouse_face))
27525 overlay = overlay_vec[i];
27526 }
27527
27528 /* If we're highlighting the same overlay as before, there's
27529 no need to do that again. */
27530 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27531 goto check_help_echo;
27532 hlinfo->mouse_face_overlay = overlay;
27533
27534 /* Clear the display of the old active region, if any. */
27535 if (clear_mouse_face (hlinfo))
27536 cursor = No_Cursor;
27537
27538 /* If no overlay applies, get a text property. */
27539 if (NILP (overlay))
27540 mouse_face = Fget_text_property (position, Qmouse_face, object);
27541
27542 /* Next, compute the bounds of the mouse highlighting and
27543 display it. */
27544 if (!NILP (mouse_face) && STRINGP (object))
27545 {
27546 /* The mouse-highlighting comes from a display string
27547 with a mouse-face. */
27548 Lisp_Object s, e;
27549 ptrdiff_t ignore;
27550
27551 s = Fprevious_single_property_change
27552 (make_number (pos + 1), Qmouse_face, object, Qnil);
27553 e = Fnext_single_property_change
27554 (position, Qmouse_face, object, Qnil);
27555 if (NILP (s))
27556 s = make_number (0);
27557 if (NILP (e))
27558 e = make_number (SCHARS (object) - 1);
27559 mouse_face_from_string_pos (w, hlinfo, object,
27560 XINT (s), XINT (e));
27561 hlinfo->mouse_face_past_end = 0;
27562 hlinfo->mouse_face_window = window;
27563 hlinfo->mouse_face_face_id
27564 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27565 glyph->face_id, 1);
27566 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27567 cursor = No_Cursor;
27568 }
27569 else
27570 {
27571 /* The mouse-highlighting, if any, comes from an overlay
27572 or text property in the buffer. */
27573 Lisp_Object buffer IF_LINT (= Qnil);
27574 Lisp_Object disp_string IF_LINT (= Qnil);
27575
27576 if (STRINGP (object))
27577 {
27578 /* If we are on a display string with no mouse-face,
27579 check if the text under it has one. */
27580 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27581 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27582 pos = string_buffer_position (object, start);
27583 if (pos > 0)
27584 {
27585 mouse_face = get_char_property_and_overlay
27586 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27587 buffer = w->buffer;
27588 disp_string = object;
27589 }
27590 }
27591 else
27592 {
27593 buffer = object;
27594 disp_string = Qnil;
27595 }
27596
27597 if (!NILP (mouse_face))
27598 {
27599 Lisp_Object before, after;
27600 Lisp_Object before_string, after_string;
27601 /* To correctly find the limits of mouse highlight
27602 in a bidi-reordered buffer, we must not use the
27603 optimization of limiting the search in
27604 previous-single-property-change and
27605 next-single-property-change, because
27606 rows_from_pos_range needs the real start and end
27607 positions to DTRT in this case. That's because
27608 the first row visible in a window does not
27609 necessarily display the character whose position
27610 is the smallest. */
27611 Lisp_Object lim1 =
27612 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27613 ? Fmarker_position (w->start)
27614 : Qnil;
27615 Lisp_Object lim2 =
27616 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27617 ? make_number (BUF_Z (XBUFFER (buffer))
27618 - XFASTINT (w->window_end_pos))
27619 : Qnil;
27620
27621 if (NILP (overlay))
27622 {
27623 /* Handle the text property case. */
27624 before = Fprevious_single_property_change
27625 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27626 after = Fnext_single_property_change
27627 (make_number (pos), Qmouse_face, buffer, lim2);
27628 before_string = after_string = Qnil;
27629 }
27630 else
27631 {
27632 /* Handle the overlay case. */
27633 before = Foverlay_start (overlay);
27634 after = Foverlay_end (overlay);
27635 before_string = Foverlay_get (overlay, Qbefore_string);
27636 after_string = Foverlay_get (overlay, Qafter_string);
27637
27638 if (!STRINGP (before_string)) before_string = Qnil;
27639 if (!STRINGP (after_string)) after_string = Qnil;
27640 }
27641
27642 mouse_face_from_buffer_pos (window, hlinfo, pos,
27643 NILP (before)
27644 ? 1
27645 : XFASTINT (before),
27646 NILP (after)
27647 ? BUF_Z (XBUFFER (buffer))
27648 : XFASTINT (after),
27649 before_string, after_string,
27650 disp_string);
27651 cursor = No_Cursor;
27652 }
27653 }
27654 }
27655
27656 check_help_echo:
27657
27658 /* Look for a `help-echo' property. */
27659 if (NILP (help_echo_string)) {
27660 Lisp_Object help, overlay;
27661
27662 /* Check overlays first. */
27663 help = overlay = Qnil;
27664 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27665 {
27666 overlay = overlay_vec[i];
27667 help = Foverlay_get (overlay, Qhelp_echo);
27668 }
27669
27670 if (!NILP (help))
27671 {
27672 help_echo_string = help;
27673 help_echo_window = window;
27674 help_echo_object = overlay;
27675 help_echo_pos = pos;
27676 }
27677 else
27678 {
27679 Lisp_Object obj = glyph->object;
27680 ptrdiff_t charpos = glyph->charpos;
27681
27682 /* Try text properties. */
27683 if (STRINGP (obj)
27684 && charpos >= 0
27685 && charpos < SCHARS (obj))
27686 {
27687 help = Fget_text_property (make_number (charpos),
27688 Qhelp_echo, obj);
27689 if (NILP (help))
27690 {
27691 /* If the string itself doesn't specify a help-echo,
27692 see if the buffer text ``under'' it does. */
27693 struct glyph_row *r
27694 = MATRIX_ROW (w->current_matrix, vpos);
27695 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27696 ptrdiff_t p = string_buffer_position (obj, start);
27697 if (p > 0)
27698 {
27699 help = Fget_char_property (make_number (p),
27700 Qhelp_echo, w->buffer);
27701 if (!NILP (help))
27702 {
27703 charpos = p;
27704 obj = w->buffer;
27705 }
27706 }
27707 }
27708 }
27709 else if (BUFFERP (obj)
27710 && charpos >= BEGV
27711 && charpos < ZV)
27712 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27713 obj);
27714
27715 if (!NILP (help))
27716 {
27717 help_echo_string = help;
27718 help_echo_window = window;
27719 help_echo_object = obj;
27720 help_echo_pos = charpos;
27721 }
27722 }
27723 }
27724
27725 #ifdef HAVE_WINDOW_SYSTEM
27726 /* Look for a `pointer' property. */
27727 if (FRAME_WINDOW_P (f) && NILP (pointer))
27728 {
27729 /* Check overlays first. */
27730 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27731 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27732
27733 if (NILP (pointer))
27734 {
27735 Lisp_Object obj = glyph->object;
27736 ptrdiff_t charpos = glyph->charpos;
27737
27738 /* Try text properties. */
27739 if (STRINGP (obj)
27740 && charpos >= 0
27741 && charpos < SCHARS (obj))
27742 {
27743 pointer = Fget_text_property (make_number (charpos),
27744 Qpointer, obj);
27745 if (NILP (pointer))
27746 {
27747 /* If the string itself doesn't specify a pointer,
27748 see if the buffer text ``under'' it does. */
27749 struct glyph_row *r
27750 = MATRIX_ROW (w->current_matrix, vpos);
27751 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27752 ptrdiff_t p = string_buffer_position (obj, start);
27753 if (p > 0)
27754 pointer = Fget_char_property (make_number (p),
27755 Qpointer, w->buffer);
27756 }
27757 }
27758 else if (BUFFERP (obj)
27759 && charpos >= BEGV
27760 && charpos < ZV)
27761 pointer = Fget_text_property (make_number (charpos),
27762 Qpointer, obj);
27763 }
27764 }
27765 #endif /* HAVE_WINDOW_SYSTEM */
27766
27767 BEGV = obegv;
27768 ZV = ozv;
27769 current_buffer = obuf;
27770 }
27771
27772 set_cursor:
27773
27774 #ifdef HAVE_WINDOW_SYSTEM
27775 if (FRAME_WINDOW_P (f))
27776 define_frame_cursor1 (f, cursor, pointer);
27777 #else
27778 /* This is here to prevent a compiler error, about "label at end of
27779 compound statement". */
27780 return;
27781 #endif
27782 }
27783
27784
27785 /* EXPORT for RIF:
27786 Clear any mouse-face on window W. This function is part of the
27787 redisplay interface, and is called from try_window_id and similar
27788 functions to ensure the mouse-highlight is off. */
27789
27790 void
27791 x_clear_window_mouse_face (struct window *w)
27792 {
27793 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
27794 Lisp_Object window;
27795
27796 BLOCK_INPUT;
27797 XSETWINDOW (window, w);
27798 if (EQ (window, hlinfo->mouse_face_window))
27799 clear_mouse_face (hlinfo);
27800 UNBLOCK_INPUT;
27801 }
27802
27803
27804 /* EXPORT:
27805 Just discard the mouse face information for frame F, if any.
27806 This is used when the size of F is changed. */
27807
27808 void
27809 cancel_mouse_face (struct frame *f)
27810 {
27811 Lisp_Object window;
27812 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27813
27814 window = hlinfo->mouse_face_window;
27815 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
27816 {
27817 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
27818 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
27819 hlinfo->mouse_face_window = Qnil;
27820 }
27821 }
27822
27823
27824 \f
27825 /***********************************************************************
27826 Exposure Events
27827 ***********************************************************************/
27828
27829 #ifdef HAVE_WINDOW_SYSTEM
27830
27831 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
27832 which intersects rectangle R. R is in window-relative coordinates. */
27833
27834 static void
27835 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
27836 enum glyph_row_area area)
27837 {
27838 struct glyph *first = row->glyphs[area];
27839 struct glyph *end = row->glyphs[area] + row->used[area];
27840 struct glyph *last;
27841 int first_x, start_x, x;
27842
27843 if (area == TEXT_AREA && row->fill_line_p)
27844 /* If row extends face to end of line write the whole line. */
27845 draw_glyphs (w, 0, row, area,
27846 0, row->used[area],
27847 DRAW_NORMAL_TEXT, 0);
27848 else
27849 {
27850 /* Set START_X to the window-relative start position for drawing glyphs of
27851 AREA. The first glyph of the text area can be partially visible.
27852 The first glyphs of other areas cannot. */
27853 start_x = window_box_left_offset (w, area);
27854 x = start_x;
27855 if (area == TEXT_AREA)
27856 x += row->x;
27857
27858 /* Find the first glyph that must be redrawn. */
27859 while (first < end
27860 && x + first->pixel_width < r->x)
27861 {
27862 x += first->pixel_width;
27863 ++first;
27864 }
27865
27866 /* Find the last one. */
27867 last = first;
27868 first_x = x;
27869 while (last < end
27870 && x < r->x + r->width)
27871 {
27872 x += last->pixel_width;
27873 ++last;
27874 }
27875
27876 /* Repaint. */
27877 if (last > first)
27878 draw_glyphs (w, first_x - start_x, row, area,
27879 first - row->glyphs[area], last - row->glyphs[area],
27880 DRAW_NORMAL_TEXT, 0);
27881 }
27882 }
27883
27884
27885 /* Redraw the parts of the glyph row ROW on window W intersecting
27886 rectangle R. R is in window-relative coordinates. Value is
27887 non-zero if mouse-face was overwritten. */
27888
27889 static int
27890 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
27891 {
27892 xassert (row->enabled_p);
27893
27894 if (row->mode_line_p || w->pseudo_window_p)
27895 draw_glyphs (w, 0, row, TEXT_AREA,
27896 0, row->used[TEXT_AREA],
27897 DRAW_NORMAL_TEXT, 0);
27898 else
27899 {
27900 if (row->used[LEFT_MARGIN_AREA])
27901 expose_area (w, row, r, LEFT_MARGIN_AREA);
27902 if (row->used[TEXT_AREA])
27903 expose_area (w, row, r, TEXT_AREA);
27904 if (row->used[RIGHT_MARGIN_AREA])
27905 expose_area (w, row, r, RIGHT_MARGIN_AREA);
27906 draw_row_fringe_bitmaps (w, row);
27907 }
27908
27909 return row->mouse_face_p;
27910 }
27911
27912
27913 /* Redraw those parts of glyphs rows during expose event handling that
27914 overlap other rows. Redrawing of an exposed line writes over parts
27915 of lines overlapping that exposed line; this function fixes that.
27916
27917 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
27918 row in W's current matrix that is exposed and overlaps other rows.
27919 LAST_OVERLAPPING_ROW is the last such row. */
27920
27921 static void
27922 expose_overlaps (struct window *w,
27923 struct glyph_row *first_overlapping_row,
27924 struct glyph_row *last_overlapping_row,
27925 XRectangle *r)
27926 {
27927 struct glyph_row *row;
27928
27929 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
27930 if (row->overlapping_p)
27931 {
27932 xassert (row->enabled_p && !row->mode_line_p);
27933
27934 row->clip = r;
27935 if (row->used[LEFT_MARGIN_AREA])
27936 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
27937
27938 if (row->used[TEXT_AREA])
27939 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
27940
27941 if (row->used[RIGHT_MARGIN_AREA])
27942 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
27943 row->clip = NULL;
27944 }
27945 }
27946
27947
27948 /* Return non-zero if W's cursor intersects rectangle R. */
27949
27950 static int
27951 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
27952 {
27953 XRectangle cr, result;
27954 struct glyph *cursor_glyph;
27955 struct glyph_row *row;
27956
27957 if (w->phys_cursor.vpos >= 0
27958 && w->phys_cursor.vpos < w->current_matrix->nrows
27959 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
27960 row->enabled_p)
27961 && row->cursor_in_fringe_p)
27962 {
27963 /* Cursor is in the fringe. */
27964 cr.x = window_box_right_offset (w,
27965 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
27966 ? RIGHT_MARGIN_AREA
27967 : TEXT_AREA));
27968 cr.y = row->y;
27969 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
27970 cr.height = row->height;
27971 return x_intersect_rectangles (&cr, r, &result);
27972 }
27973
27974 cursor_glyph = get_phys_cursor_glyph (w);
27975 if (cursor_glyph)
27976 {
27977 /* r is relative to W's box, but w->phys_cursor.x is relative
27978 to left edge of W's TEXT area. Adjust it. */
27979 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
27980 cr.y = w->phys_cursor.y;
27981 cr.width = cursor_glyph->pixel_width;
27982 cr.height = w->phys_cursor_height;
27983 /* ++KFS: W32 version used W32-specific IntersectRect here, but
27984 I assume the effect is the same -- and this is portable. */
27985 return x_intersect_rectangles (&cr, r, &result);
27986 }
27987 /* If we don't understand the format, pretend we're not in the hot-spot. */
27988 return 0;
27989 }
27990
27991
27992 /* EXPORT:
27993 Draw a vertical window border to the right of window W if W doesn't
27994 have vertical scroll bars. */
27995
27996 void
27997 x_draw_vertical_border (struct window *w)
27998 {
27999 struct frame *f = XFRAME (WINDOW_FRAME (w));
28000
28001 /* We could do better, if we knew what type of scroll-bar the adjacent
28002 windows (on either side) have... But we don't :-(
28003 However, I think this works ok. ++KFS 2003-04-25 */
28004
28005 /* Redraw borders between horizontally adjacent windows. Don't
28006 do it for frames with vertical scroll bars because either the
28007 right scroll bar of a window, or the left scroll bar of its
28008 neighbor will suffice as a border. */
28009 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28010 return;
28011
28012 if (!WINDOW_RIGHTMOST_P (w)
28013 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28014 {
28015 int x0, x1, y0, y1;
28016
28017 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28018 y1 -= 1;
28019
28020 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28021 x1 -= 1;
28022
28023 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28024 }
28025 else if (!WINDOW_LEFTMOST_P (w)
28026 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28027 {
28028 int x0, x1, y0, y1;
28029
28030 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28031 y1 -= 1;
28032
28033 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28034 x0 -= 1;
28035
28036 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28037 }
28038 }
28039
28040
28041 /* Redraw the part of window W intersection rectangle FR. Pixel
28042 coordinates in FR are frame-relative. Call this function with
28043 input blocked. Value is non-zero if the exposure overwrites
28044 mouse-face. */
28045
28046 static int
28047 expose_window (struct window *w, XRectangle *fr)
28048 {
28049 struct frame *f = XFRAME (w->frame);
28050 XRectangle wr, r;
28051 int mouse_face_overwritten_p = 0;
28052
28053 /* If window is not yet fully initialized, do nothing. This can
28054 happen when toolkit scroll bars are used and a window is split.
28055 Reconfiguring the scroll bar will generate an expose for a newly
28056 created window. */
28057 if (w->current_matrix == NULL)
28058 return 0;
28059
28060 /* When we're currently updating the window, display and current
28061 matrix usually don't agree. Arrange for a thorough display
28062 later. */
28063 if (w == updated_window)
28064 {
28065 SET_FRAME_GARBAGED (f);
28066 return 0;
28067 }
28068
28069 /* Frame-relative pixel rectangle of W. */
28070 wr.x = WINDOW_LEFT_EDGE_X (w);
28071 wr.y = WINDOW_TOP_EDGE_Y (w);
28072 wr.width = WINDOW_TOTAL_WIDTH (w);
28073 wr.height = WINDOW_TOTAL_HEIGHT (w);
28074
28075 if (x_intersect_rectangles (fr, &wr, &r))
28076 {
28077 int yb = window_text_bottom_y (w);
28078 struct glyph_row *row;
28079 int cursor_cleared_p, phys_cursor_on_p;
28080 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28081
28082 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28083 r.x, r.y, r.width, r.height));
28084
28085 /* Convert to window coordinates. */
28086 r.x -= WINDOW_LEFT_EDGE_X (w);
28087 r.y -= WINDOW_TOP_EDGE_Y (w);
28088
28089 /* Turn off the cursor. */
28090 if (!w->pseudo_window_p
28091 && phys_cursor_in_rect_p (w, &r))
28092 {
28093 x_clear_cursor (w);
28094 cursor_cleared_p = 1;
28095 }
28096 else
28097 cursor_cleared_p = 0;
28098
28099 /* If the row containing the cursor extends face to end of line,
28100 then expose_area might overwrite the cursor outside the
28101 rectangle and thus notice_overwritten_cursor might clear
28102 w->phys_cursor_on_p. We remember the original value and
28103 check later if it is changed. */
28104 phys_cursor_on_p = w->phys_cursor_on_p;
28105
28106 /* Update lines intersecting rectangle R. */
28107 first_overlapping_row = last_overlapping_row = NULL;
28108 for (row = w->current_matrix->rows;
28109 row->enabled_p;
28110 ++row)
28111 {
28112 int y0 = row->y;
28113 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28114
28115 if ((y0 >= r.y && y0 < r.y + r.height)
28116 || (y1 > r.y && y1 < r.y + r.height)
28117 || (r.y >= y0 && r.y < y1)
28118 || (r.y + r.height > y0 && r.y + r.height < y1))
28119 {
28120 /* A header line may be overlapping, but there is no need
28121 to fix overlapping areas for them. KFS 2005-02-12 */
28122 if (row->overlapping_p && !row->mode_line_p)
28123 {
28124 if (first_overlapping_row == NULL)
28125 first_overlapping_row = row;
28126 last_overlapping_row = row;
28127 }
28128
28129 row->clip = fr;
28130 if (expose_line (w, row, &r))
28131 mouse_face_overwritten_p = 1;
28132 row->clip = NULL;
28133 }
28134 else if (row->overlapping_p)
28135 {
28136 /* We must redraw a row overlapping the exposed area. */
28137 if (y0 < r.y
28138 ? y0 + row->phys_height > r.y
28139 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28140 {
28141 if (first_overlapping_row == NULL)
28142 first_overlapping_row = row;
28143 last_overlapping_row = row;
28144 }
28145 }
28146
28147 if (y1 >= yb)
28148 break;
28149 }
28150
28151 /* Display the mode line if there is one. */
28152 if (WINDOW_WANTS_MODELINE_P (w)
28153 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28154 row->enabled_p)
28155 && row->y < r.y + r.height)
28156 {
28157 if (expose_line (w, row, &r))
28158 mouse_face_overwritten_p = 1;
28159 }
28160
28161 if (!w->pseudo_window_p)
28162 {
28163 /* Fix the display of overlapping rows. */
28164 if (first_overlapping_row)
28165 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28166 fr);
28167
28168 /* Draw border between windows. */
28169 x_draw_vertical_border (w);
28170
28171 /* Turn the cursor on again. */
28172 if (cursor_cleared_p
28173 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28174 update_window_cursor (w, 1);
28175 }
28176 }
28177
28178 return mouse_face_overwritten_p;
28179 }
28180
28181
28182
28183 /* Redraw (parts) of all windows in the window tree rooted at W that
28184 intersect R. R contains frame pixel coordinates. Value is
28185 non-zero if the exposure overwrites mouse-face. */
28186
28187 static int
28188 expose_window_tree (struct window *w, XRectangle *r)
28189 {
28190 struct frame *f = XFRAME (w->frame);
28191 int mouse_face_overwritten_p = 0;
28192
28193 while (w && !FRAME_GARBAGED_P (f))
28194 {
28195 if (!NILP (w->hchild))
28196 mouse_face_overwritten_p
28197 |= expose_window_tree (XWINDOW (w->hchild), r);
28198 else if (!NILP (w->vchild))
28199 mouse_face_overwritten_p
28200 |= expose_window_tree (XWINDOW (w->vchild), r);
28201 else
28202 mouse_face_overwritten_p |= expose_window (w, r);
28203
28204 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28205 }
28206
28207 return mouse_face_overwritten_p;
28208 }
28209
28210
28211 /* EXPORT:
28212 Redisplay an exposed area of frame F. X and Y are the upper-left
28213 corner of the exposed rectangle. W and H are width and height of
28214 the exposed area. All are pixel values. W or H zero means redraw
28215 the entire frame. */
28216
28217 void
28218 expose_frame (struct frame *f, int x, int y, int w, int h)
28219 {
28220 XRectangle r;
28221 int mouse_face_overwritten_p = 0;
28222
28223 TRACE ((stderr, "expose_frame "));
28224
28225 /* No need to redraw if frame will be redrawn soon. */
28226 if (FRAME_GARBAGED_P (f))
28227 {
28228 TRACE ((stderr, " garbaged\n"));
28229 return;
28230 }
28231
28232 /* If basic faces haven't been realized yet, there is no point in
28233 trying to redraw anything. This can happen when we get an expose
28234 event while Emacs is starting, e.g. by moving another window. */
28235 if (FRAME_FACE_CACHE (f) == NULL
28236 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28237 {
28238 TRACE ((stderr, " no faces\n"));
28239 return;
28240 }
28241
28242 if (w == 0 || h == 0)
28243 {
28244 r.x = r.y = 0;
28245 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28246 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28247 }
28248 else
28249 {
28250 r.x = x;
28251 r.y = y;
28252 r.width = w;
28253 r.height = h;
28254 }
28255
28256 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28257 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28258
28259 if (WINDOWP (f->tool_bar_window))
28260 mouse_face_overwritten_p
28261 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28262
28263 #ifdef HAVE_X_WINDOWS
28264 #ifndef MSDOS
28265 #ifndef USE_X_TOOLKIT
28266 if (WINDOWP (f->menu_bar_window))
28267 mouse_face_overwritten_p
28268 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28269 #endif /* not USE_X_TOOLKIT */
28270 #endif
28271 #endif
28272
28273 /* Some window managers support a focus-follows-mouse style with
28274 delayed raising of frames. Imagine a partially obscured frame,
28275 and moving the mouse into partially obscured mouse-face on that
28276 frame. The visible part of the mouse-face will be highlighted,
28277 then the WM raises the obscured frame. With at least one WM, KDE
28278 2.1, Emacs is not getting any event for the raising of the frame
28279 (even tried with SubstructureRedirectMask), only Expose events.
28280 These expose events will draw text normally, i.e. not
28281 highlighted. Which means we must redo the highlight here.
28282 Subsume it under ``we love X''. --gerd 2001-08-15 */
28283 /* Included in Windows version because Windows most likely does not
28284 do the right thing if any third party tool offers
28285 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28286 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28287 {
28288 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28289 if (f == hlinfo->mouse_face_mouse_frame)
28290 {
28291 int mouse_x = hlinfo->mouse_face_mouse_x;
28292 int mouse_y = hlinfo->mouse_face_mouse_y;
28293 clear_mouse_face (hlinfo);
28294 note_mouse_highlight (f, mouse_x, mouse_y);
28295 }
28296 }
28297 }
28298
28299
28300 /* EXPORT:
28301 Determine the intersection of two rectangles R1 and R2. Return
28302 the intersection in *RESULT. Value is non-zero if RESULT is not
28303 empty. */
28304
28305 int
28306 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28307 {
28308 XRectangle *left, *right;
28309 XRectangle *upper, *lower;
28310 int intersection_p = 0;
28311
28312 /* Rearrange so that R1 is the left-most rectangle. */
28313 if (r1->x < r2->x)
28314 left = r1, right = r2;
28315 else
28316 left = r2, right = r1;
28317
28318 /* X0 of the intersection is right.x0, if this is inside R1,
28319 otherwise there is no intersection. */
28320 if (right->x <= left->x + left->width)
28321 {
28322 result->x = right->x;
28323
28324 /* The right end of the intersection is the minimum of
28325 the right ends of left and right. */
28326 result->width = (min (left->x + left->width, right->x + right->width)
28327 - result->x);
28328
28329 /* Same game for Y. */
28330 if (r1->y < r2->y)
28331 upper = r1, lower = r2;
28332 else
28333 upper = r2, lower = r1;
28334
28335 /* The upper end of the intersection is lower.y0, if this is inside
28336 of upper. Otherwise, there is no intersection. */
28337 if (lower->y <= upper->y + upper->height)
28338 {
28339 result->y = lower->y;
28340
28341 /* The lower end of the intersection is the minimum of the lower
28342 ends of upper and lower. */
28343 result->height = (min (lower->y + lower->height,
28344 upper->y + upper->height)
28345 - result->y);
28346 intersection_p = 1;
28347 }
28348 }
28349
28350 return intersection_p;
28351 }
28352
28353 #endif /* HAVE_WINDOW_SYSTEM */
28354
28355 \f
28356 /***********************************************************************
28357 Initialization
28358 ***********************************************************************/
28359
28360 void
28361 syms_of_xdisp (void)
28362 {
28363 Vwith_echo_area_save_vector = Qnil;
28364 staticpro (&Vwith_echo_area_save_vector);
28365
28366 Vmessage_stack = Qnil;
28367 staticpro (&Vmessage_stack);
28368
28369 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28370
28371 message_dolog_marker1 = Fmake_marker ();
28372 staticpro (&message_dolog_marker1);
28373 message_dolog_marker2 = Fmake_marker ();
28374 staticpro (&message_dolog_marker2);
28375 message_dolog_marker3 = Fmake_marker ();
28376 staticpro (&message_dolog_marker3);
28377
28378 #if GLYPH_DEBUG
28379 defsubr (&Sdump_frame_glyph_matrix);
28380 defsubr (&Sdump_glyph_matrix);
28381 defsubr (&Sdump_glyph_row);
28382 defsubr (&Sdump_tool_bar_row);
28383 defsubr (&Strace_redisplay);
28384 defsubr (&Strace_to_stderr);
28385 #endif
28386 #ifdef HAVE_WINDOW_SYSTEM
28387 defsubr (&Stool_bar_lines_needed);
28388 defsubr (&Slookup_image_map);
28389 #endif
28390 defsubr (&Sformat_mode_line);
28391 defsubr (&Sinvisible_p);
28392 defsubr (&Scurrent_bidi_paragraph_direction);
28393
28394 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28395 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28396 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28397 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28398 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28399 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28400 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28401 DEFSYM (Qeval, "eval");
28402 DEFSYM (QCdata, ":data");
28403 DEFSYM (Qdisplay, "display");
28404 DEFSYM (Qspace_width, "space-width");
28405 DEFSYM (Qraise, "raise");
28406 DEFSYM (Qslice, "slice");
28407 DEFSYM (Qspace, "space");
28408 DEFSYM (Qmargin, "margin");
28409 DEFSYM (Qpointer, "pointer");
28410 DEFSYM (Qleft_margin, "left-margin");
28411 DEFSYM (Qright_margin, "right-margin");
28412 DEFSYM (Qcenter, "center");
28413 DEFSYM (Qline_height, "line-height");
28414 DEFSYM (QCalign_to, ":align-to");
28415 DEFSYM (QCrelative_width, ":relative-width");
28416 DEFSYM (QCrelative_height, ":relative-height");
28417 DEFSYM (QCeval, ":eval");
28418 DEFSYM (QCpropertize, ":propertize");
28419 DEFSYM (QCfile, ":file");
28420 DEFSYM (Qfontified, "fontified");
28421 DEFSYM (Qfontification_functions, "fontification-functions");
28422 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28423 DEFSYM (Qescape_glyph, "escape-glyph");
28424 DEFSYM (Qnobreak_space, "nobreak-space");
28425 DEFSYM (Qimage, "image");
28426 DEFSYM (Qtext, "text");
28427 DEFSYM (Qboth, "both");
28428 DEFSYM (Qboth_horiz, "both-horiz");
28429 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28430 DEFSYM (QCmap, ":map");
28431 DEFSYM (QCpointer, ":pointer");
28432 DEFSYM (Qrect, "rect");
28433 DEFSYM (Qcircle, "circle");
28434 DEFSYM (Qpoly, "poly");
28435 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28436 DEFSYM (Qgrow_only, "grow-only");
28437 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28438 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28439 DEFSYM (Qposition, "position");
28440 DEFSYM (Qbuffer_position, "buffer-position");
28441 DEFSYM (Qobject, "object");
28442 DEFSYM (Qbar, "bar");
28443 DEFSYM (Qhbar, "hbar");
28444 DEFSYM (Qbox, "box");
28445 DEFSYM (Qhollow, "hollow");
28446 DEFSYM (Qhand, "hand");
28447 DEFSYM (Qarrow, "arrow");
28448 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28449
28450 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28451 Fcons (intern_c_string ("void-variable"), Qnil)),
28452 Qnil);
28453 staticpro (&list_of_error);
28454
28455 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28456 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28457 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28458 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28459
28460 echo_buffer[0] = echo_buffer[1] = Qnil;
28461 staticpro (&echo_buffer[0]);
28462 staticpro (&echo_buffer[1]);
28463
28464 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28465 staticpro (&echo_area_buffer[0]);
28466 staticpro (&echo_area_buffer[1]);
28467
28468 Vmessages_buffer_name = make_pure_c_string ("*Messages*");
28469 staticpro (&Vmessages_buffer_name);
28470
28471 mode_line_proptrans_alist = Qnil;
28472 staticpro (&mode_line_proptrans_alist);
28473 mode_line_string_list = Qnil;
28474 staticpro (&mode_line_string_list);
28475 mode_line_string_face = Qnil;
28476 staticpro (&mode_line_string_face);
28477 mode_line_string_face_prop = Qnil;
28478 staticpro (&mode_line_string_face_prop);
28479 Vmode_line_unwind_vector = Qnil;
28480 staticpro (&Vmode_line_unwind_vector);
28481
28482 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28483
28484 help_echo_string = Qnil;
28485 staticpro (&help_echo_string);
28486 help_echo_object = Qnil;
28487 staticpro (&help_echo_object);
28488 help_echo_window = Qnil;
28489 staticpro (&help_echo_window);
28490 previous_help_echo_string = Qnil;
28491 staticpro (&previous_help_echo_string);
28492 help_echo_pos = -1;
28493
28494 DEFSYM (Qright_to_left, "right-to-left");
28495 DEFSYM (Qleft_to_right, "left-to-right");
28496
28497 #ifdef HAVE_WINDOW_SYSTEM
28498 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28499 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28500 For example, if a block cursor is over a tab, it will be drawn as
28501 wide as that tab on the display. */);
28502 x_stretch_cursor_p = 0;
28503 #endif
28504
28505 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28506 doc: /* Non-nil means highlight trailing whitespace.
28507 The face used for trailing whitespace is `trailing-whitespace'. */);
28508 Vshow_trailing_whitespace = Qnil;
28509
28510 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28511 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28512 If the value is t, Emacs highlights non-ASCII chars which have the
28513 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28514 or `escape-glyph' face respectively.
28515
28516 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28517 U+2011 (non-breaking hyphen) are affected.
28518
28519 Any other non-nil value means to display these characters as a escape
28520 glyph followed by an ordinary space or hyphen.
28521
28522 A value of nil means no special handling of these characters. */);
28523 Vnobreak_char_display = Qt;
28524
28525 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28526 doc: /* The pointer shape to show in void text areas.
28527 A value of nil means to show the text pointer. Other options are `arrow',
28528 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28529 Vvoid_text_area_pointer = Qarrow;
28530
28531 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28532 doc: /* Non-nil means don't actually do any redisplay.
28533 This is used for internal purposes. */);
28534 Vinhibit_redisplay = Qnil;
28535
28536 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28537 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28538 Vglobal_mode_string = Qnil;
28539
28540 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28541 doc: /* Marker for where to display an arrow on top of the buffer text.
28542 This must be the beginning of a line in order to work.
28543 See also `overlay-arrow-string'. */);
28544 Voverlay_arrow_position = Qnil;
28545
28546 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28547 doc: /* String to display as an arrow in non-window frames.
28548 See also `overlay-arrow-position'. */);
28549 Voverlay_arrow_string = make_pure_c_string ("=>");
28550
28551 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28552 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28553 The symbols on this list are examined during redisplay to determine
28554 where to display overlay arrows. */);
28555 Voverlay_arrow_variable_list
28556 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28557
28558 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28559 doc: /* The number of lines to try scrolling a window by when point moves out.
28560 If that fails to bring point back on frame, point is centered instead.
28561 If this is zero, point is always centered after it moves off frame.
28562 If you want scrolling to always be a line at a time, you should set
28563 `scroll-conservatively' to a large value rather than set this to 1. */);
28564
28565 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28566 doc: /* Scroll up to this many lines, to bring point back on screen.
28567 If point moves off-screen, redisplay will scroll by up to
28568 `scroll-conservatively' lines in order to bring point just barely
28569 onto the screen again. If that cannot be done, then redisplay
28570 recenters point as usual.
28571
28572 If the value is greater than 100, redisplay will never recenter point,
28573 but will always scroll just enough text to bring point into view, even
28574 if you move far away.
28575
28576 A value of zero means always recenter point if it moves off screen. */);
28577 scroll_conservatively = 0;
28578
28579 DEFVAR_INT ("scroll-margin", scroll_margin,
28580 doc: /* Number of lines of margin at the top and bottom of a window.
28581 Recenter the window whenever point gets within this many lines
28582 of the top or bottom of the window. */);
28583 scroll_margin = 0;
28584
28585 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28586 doc: /* Pixels per inch value for non-window system displays.
28587 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28588 Vdisplay_pixels_per_inch = make_float (72.0);
28589
28590 #if GLYPH_DEBUG
28591 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28592 #endif
28593
28594 DEFVAR_LISP ("truncate-partial-width-windows",
28595 Vtruncate_partial_width_windows,
28596 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28597 For an integer value, truncate lines in each window narrower than the
28598 full frame width, provided the window width is less than that integer;
28599 otherwise, respect the value of `truncate-lines'.
28600
28601 For any other non-nil value, truncate lines in all windows that do
28602 not span the full frame width.
28603
28604 A value of nil means to respect the value of `truncate-lines'.
28605
28606 If `word-wrap' is enabled, you might want to reduce this. */);
28607 Vtruncate_partial_width_windows = make_number (50);
28608
28609 DEFVAR_BOOL ("mode-line-inverse-video", mode_line_inverse_video,
28610 doc: /* When nil, display the mode-line/header-line/menu-bar in the default face.
28611 Any other value means to use the appropriate face, `mode-line',
28612 `header-line', or `menu' respectively. */);
28613 mode_line_inverse_video = 1;
28614
28615 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28616 doc: /* Maximum buffer size for which line number should be displayed.
28617 If the buffer is bigger than this, the line number does not appear
28618 in the mode line. A value of nil means no limit. */);
28619 Vline_number_display_limit = Qnil;
28620
28621 DEFVAR_INT ("line-number-display-limit-width",
28622 line_number_display_limit_width,
28623 doc: /* Maximum line width (in characters) for line number display.
28624 If the average length of the lines near point is bigger than this, then the
28625 line number may be omitted from the mode line. */);
28626 line_number_display_limit_width = 200;
28627
28628 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28629 doc: /* Non-nil means highlight region even in nonselected windows. */);
28630 highlight_nonselected_windows = 0;
28631
28632 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28633 doc: /* Non-nil if more than one frame is visible on this display.
28634 Minibuffer-only frames don't count, but iconified frames do.
28635 This variable is not guaranteed to be accurate except while processing
28636 `frame-title-format' and `icon-title-format'. */);
28637
28638 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28639 doc: /* Template for displaying the title bar of visible frames.
28640 \(Assuming the window manager supports this feature.)
28641
28642 This variable has the same structure as `mode-line-format', except that
28643 the %c and %l constructs are ignored. It is used only on frames for
28644 which no explicit name has been set \(see `modify-frame-parameters'). */);
28645
28646 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28647 doc: /* Template for displaying the title bar of an iconified frame.
28648 \(Assuming the window manager supports this feature.)
28649 This variable has the same structure as `mode-line-format' (which see),
28650 and is used only on frames for which no explicit name has been set
28651 \(see `modify-frame-parameters'). */);
28652 Vicon_title_format
28653 = Vframe_title_format
28654 = pure_cons (intern_c_string ("multiple-frames"),
28655 pure_cons (make_pure_c_string ("%b"),
28656 pure_cons (pure_cons (empty_unibyte_string,
28657 pure_cons (intern_c_string ("invocation-name"),
28658 pure_cons (make_pure_c_string ("@"),
28659 pure_cons (intern_c_string ("system-name"),
28660 Qnil)))),
28661 Qnil)));
28662
28663 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28664 doc: /* Maximum number of lines to keep in the message log buffer.
28665 If nil, disable message logging. If t, log messages but don't truncate
28666 the buffer when it becomes large. */);
28667 Vmessage_log_max = make_number (100);
28668
28669 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28670 doc: /* Functions called before redisplay, if window sizes have changed.
28671 The value should be a list of functions that take one argument.
28672 Just before redisplay, for each frame, if any of its windows have changed
28673 size since the last redisplay, or have been split or deleted,
28674 all the functions in the list are called, with the frame as argument. */);
28675 Vwindow_size_change_functions = Qnil;
28676
28677 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28678 doc: /* List of functions to call before redisplaying a window with scrolling.
28679 Each function is called with two arguments, the window and its new
28680 display-start position. Note that these functions are also called by
28681 `set-window-buffer'. Also note that the value of `window-end' is not
28682 valid when these functions are called.
28683
28684 Warning: Do not use this feature to alter the way the window
28685 is scrolled. It is not designed for that, and such use probably won't
28686 work. */);
28687 Vwindow_scroll_functions = Qnil;
28688
28689 DEFVAR_LISP ("window-text-change-functions",
28690 Vwindow_text_change_functions,
28691 doc: /* Functions to call in redisplay when text in the window might change. */);
28692 Vwindow_text_change_functions = Qnil;
28693
28694 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28695 doc: /* Functions called when redisplay of a window reaches the end trigger.
28696 Each function is called with two arguments, the window and the end trigger value.
28697 See `set-window-redisplay-end-trigger'. */);
28698 Vredisplay_end_trigger_functions = Qnil;
28699
28700 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28701 doc: /* Non-nil means autoselect window with mouse pointer.
28702 If nil, do not autoselect windows.
28703 A positive number means delay autoselection by that many seconds: a
28704 window is autoselected only after the mouse has remained in that
28705 window for the duration of the delay.
28706 A negative number has a similar effect, but causes windows to be
28707 autoselected only after the mouse has stopped moving. \(Because of
28708 the way Emacs compares mouse events, you will occasionally wait twice
28709 that time before the window gets selected.\)
28710 Any other value means to autoselect window instantaneously when the
28711 mouse pointer enters it.
28712
28713 Autoselection selects the minibuffer only if it is active, and never
28714 unselects the minibuffer if it is active.
28715
28716 When customizing this variable make sure that the actual value of
28717 `focus-follows-mouse' matches the behavior of your window manager. */);
28718 Vmouse_autoselect_window = Qnil;
28719
28720 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28721 doc: /* Non-nil means automatically resize tool-bars.
28722 This dynamically changes the tool-bar's height to the minimum height
28723 that is needed to make all tool-bar items visible.
28724 If value is `grow-only', the tool-bar's height is only increased
28725 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28726 Vauto_resize_tool_bars = Qt;
28727
28728 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28729 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28730 auto_raise_tool_bar_buttons_p = 1;
28731
28732 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28733 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28734 make_cursor_line_fully_visible_p = 1;
28735
28736 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28737 doc: /* Border below tool-bar in pixels.
28738 If an integer, use it as the height of the border.
28739 If it is one of `internal-border-width' or `border-width', use the
28740 value of the corresponding frame parameter.
28741 Otherwise, no border is added below the tool-bar. */);
28742 Vtool_bar_border = Qinternal_border_width;
28743
28744 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28745 doc: /* Margin around tool-bar buttons in pixels.
28746 If an integer, use that for both horizontal and vertical margins.
28747 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28748 HORZ specifying the horizontal margin, and VERT specifying the
28749 vertical margin. */);
28750 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28751
28752 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28753 doc: /* Relief thickness of tool-bar buttons. */);
28754 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28755
28756 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28757 doc: /* Tool bar style to use.
28758 It can be one of
28759 image - show images only
28760 text - show text only
28761 both - show both, text below image
28762 both-horiz - show text to the right of the image
28763 text-image-horiz - show text to the left of the image
28764 any other - use system default or image if no system default.
28765
28766 This variable only affects the GTK+ toolkit version of Emacs. */);
28767 Vtool_bar_style = Qnil;
28768
28769 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28770 doc: /* Maximum number of characters a label can have to be shown.
28771 The tool bar style must also show labels for this to have any effect, see
28772 `tool-bar-style'. */);
28773 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28774
28775 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28776 doc: /* List of functions to call to fontify regions of text.
28777 Each function is called with one argument POS. Functions must
28778 fontify a region starting at POS in the current buffer, and give
28779 fontified regions the property `fontified'. */);
28780 Vfontification_functions = Qnil;
28781 Fmake_variable_buffer_local (Qfontification_functions);
28782
28783 DEFVAR_BOOL ("unibyte-display-via-language-environment",
28784 unibyte_display_via_language_environment,
28785 doc: /* Non-nil means display unibyte text according to language environment.
28786 Specifically, this means that raw bytes in the range 160-255 decimal
28787 are displayed by converting them to the equivalent multibyte characters
28788 according to the current language environment. As a result, they are
28789 displayed according to the current fontset.
28790
28791 Note that this variable affects only how these bytes are displayed,
28792 but does not change the fact they are interpreted as raw bytes. */);
28793 unibyte_display_via_language_environment = 0;
28794
28795 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
28796 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
28797 If a float, it specifies a fraction of the mini-window frame's height.
28798 If an integer, it specifies a number of lines. */);
28799 Vmax_mini_window_height = make_float (0.25);
28800
28801 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
28802 doc: /* How to resize mini-windows (the minibuffer and the echo area).
28803 A value of nil means don't automatically resize mini-windows.
28804 A value of t means resize them to fit the text displayed in them.
28805 A value of `grow-only', the default, means let mini-windows grow only;
28806 they return to their normal size when the minibuffer is closed, or the
28807 echo area becomes empty. */);
28808 Vresize_mini_windows = Qgrow_only;
28809
28810 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
28811 doc: /* Alist specifying how to blink the cursor off.
28812 Each element has the form (ON-STATE . OFF-STATE). Whenever the
28813 `cursor-type' frame-parameter or variable equals ON-STATE,
28814 comparing using `equal', Emacs uses OFF-STATE to specify
28815 how to blink it off. ON-STATE and OFF-STATE are values for
28816 the `cursor-type' frame parameter.
28817
28818 If a frame's ON-STATE has no entry in this list,
28819 the frame's other specifications determine how to blink the cursor off. */);
28820 Vblink_cursor_alist = Qnil;
28821
28822 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
28823 doc: /* Allow or disallow automatic horizontal scrolling of windows.
28824 If non-nil, windows are automatically scrolled horizontally to make
28825 point visible. */);
28826 automatic_hscrolling_p = 1;
28827 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
28828
28829 DEFVAR_INT ("hscroll-margin", hscroll_margin,
28830 doc: /* How many columns away from the window edge point is allowed to get
28831 before automatic hscrolling will horizontally scroll the window. */);
28832 hscroll_margin = 5;
28833
28834 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
28835 doc: /* How many columns to scroll the window when point gets too close to the edge.
28836 When point is less than `hscroll-margin' columns from the window
28837 edge, automatic hscrolling will scroll the window by the amount of columns
28838 determined by this variable. If its value is a positive integer, scroll that
28839 many columns. If it's a positive floating-point number, it specifies the
28840 fraction of the window's width to scroll. If it's nil or zero, point will be
28841 centered horizontally after the scroll. Any other value, including negative
28842 numbers, are treated as if the value were zero.
28843
28844 Automatic hscrolling always moves point outside the scroll margin, so if
28845 point was more than scroll step columns inside the margin, the window will
28846 scroll more than the value given by the scroll step.
28847
28848 Note that the lower bound for automatic hscrolling specified by `scroll-left'
28849 and `scroll-right' overrides this variable's effect. */);
28850 Vhscroll_step = make_number (0);
28851
28852 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
28853 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
28854 Bind this around calls to `message' to let it take effect. */);
28855 message_truncate_lines = 0;
28856
28857 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
28858 doc: /* Normal hook run to update the menu bar definitions.
28859 Redisplay runs this hook before it redisplays the menu bar.
28860 This is used to update submenus such as Buffers,
28861 whose contents depend on various data. */);
28862 Vmenu_bar_update_hook = Qnil;
28863
28864 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
28865 doc: /* Frame for which we are updating a menu.
28866 The enable predicate for a menu binding should check this variable. */);
28867 Vmenu_updating_frame = Qnil;
28868
28869 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
28870 doc: /* Non-nil means don't update menu bars. Internal use only. */);
28871 inhibit_menubar_update = 0;
28872
28873 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
28874 doc: /* Prefix prepended to all continuation lines at display time.
28875 The value may be a string, an image, or a stretch-glyph; it is
28876 interpreted in the same way as the value of a `display' text property.
28877
28878 This variable is overridden by any `wrap-prefix' text or overlay
28879 property.
28880
28881 To add a prefix to non-continuation lines, use `line-prefix'. */);
28882 Vwrap_prefix = Qnil;
28883 DEFSYM (Qwrap_prefix, "wrap-prefix");
28884 Fmake_variable_buffer_local (Qwrap_prefix);
28885
28886 DEFVAR_LISP ("line-prefix", Vline_prefix,
28887 doc: /* Prefix prepended to all non-continuation lines at display time.
28888 The value may be a string, an image, or a stretch-glyph; it is
28889 interpreted in the same way as the value of a `display' text property.
28890
28891 This variable is overridden by any `line-prefix' text or overlay
28892 property.
28893
28894 To add a prefix to continuation lines, use `wrap-prefix'. */);
28895 Vline_prefix = Qnil;
28896 DEFSYM (Qline_prefix, "line-prefix");
28897 Fmake_variable_buffer_local (Qline_prefix);
28898
28899 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
28900 doc: /* Non-nil means don't eval Lisp during redisplay. */);
28901 inhibit_eval_during_redisplay = 0;
28902
28903 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
28904 doc: /* Non-nil means don't free realized faces. Internal use only. */);
28905 inhibit_free_realized_faces = 0;
28906
28907 #if GLYPH_DEBUG
28908 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
28909 doc: /* Inhibit try_window_id display optimization. */);
28910 inhibit_try_window_id = 0;
28911
28912 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
28913 doc: /* Inhibit try_window_reusing display optimization. */);
28914 inhibit_try_window_reusing = 0;
28915
28916 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
28917 doc: /* Inhibit try_cursor_movement display optimization. */);
28918 inhibit_try_cursor_movement = 0;
28919 #endif /* GLYPH_DEBUG */
28920
28921 DEFVAR_INT ("overline-margin", overline_margin,
28922 doc: /* Space between overline and text, in pixels.
28923 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
28924 margin to the character height. */);
28925 overline_margin = 2;
28926
28927 DEFVAR_INT ("underline-minimum-offset",
28928 underline_minimum_offset,
28929 doc: /* Minimum distance between baseline and underline.
28930 This can improve legibility of underlined text at small font sizes,
28931 particularly when using variable `x-use-underline-position-properties'
28932 with fonts that specify an UNDERLINE_POSITION relatively close to the
28933 baseline. The default value is 1. */);
28934 underline_minimum_offset = 1;
28935
28936 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
28937 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
28938 This feature only works when on a window system that can change
28939 cursor shapes. */);
28940 display_hourglass_p = 1;
28941
28942 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
28943 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
28944 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
28945
28946 hourglass_atimer = NULL;
28947 hourglass_shown_p = 0;
28948
28949 DEFSYM (Qglyphless_char, "glyphless-char");
28950 DEFSYM (Qhex_code, "hex-code");
28951 DEFSYM (Qempty_box, "empty-box");
28952 DEFSYM (Qthin_space, "thin-space");
28953 DEFSYM (Qzero_width, "zero-width");
28954
28955 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
28956 /* Intern this now in case it isn't already done.
28957 Setting this variable twice is harmless.
28958 But don't staticpro it here--that is done in alloc.c. */
28959 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
28960 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
28961
28962 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
28963 doc: /* Char-table defining glyphless characters.
28964 Each element, if non-nil, should be one of the following:
28965 an ASCII acronym string: display this string in a box
28966 `hex-code': display the hexadecimal code of a character in a box
28967 `empty-box': display as an empty box
28968 `thin-space': display as 1-pixel width space
28969 `zero-width': don't display
28970 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
28971 display method for graphical terminals and text terminals respectively.
28972 GRAPHICAL and TEXT should each have one of the values listed above.
28973
28974 The char-table has one extra slot to control the display of a character for
28975 which no font is found. This slot only takes effect on graphical terminals.
28976 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
28977 `thin-space'. The default is `empty-box'. */);
28978 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
28979 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
28980 Qempty_box);
28981 }
28982
28983
28984 /* Initialize this module when Emacs starts. */
28985
28986 void
28987 init_xdisp (void)
28988 {
28989 current_header_line_height = current_mode_line_height = -1;
28990
28991 CHARPOS (this_line_start_pos) = 0;
28992
28993 if (!noninteractive)
28994 {
28995 struct window *m = XWINDOW (minibuf_window);
28996 Lisp_Object frame = m->frame;
28997 struct frame *f = XFRAME (frame);
28998 Lisp_Object root = FRAME_ROOT_WINDOW (f);
28999 struct window *r = XWINDOW (root);
29000 int i;
29001
29002 echo_area_window = minibuf_window;
29003
29004 XSETFASTINT (r->top_line, FRAME_TOP_MARGIN (f));
29005 XSETFASTINT (r->total_lines, FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f));
29006 XSETFASTINT (r->total_cols, FRAME_COLS (f));
29007 XSETFASTINT (m->top_line, FRAME_LINES (f) - 1);
29008 XSETFASTINT (m->total_lines, 1);
29009 XSETFASTINT (m->total_cols, FRAME_COLS (f));
29010
29011 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29012 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29013 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29014
29015 /* The default ellipsis glyphs `...'. */
29016 for (i = 0; i < 3; ++i)
29017 default_invis_vector[i] = make_number ('.');
29018 }
29019
29020 {
29021 /* Allocate the buffer for frame titles.
29022 Also used for `format-mode-line'. */
29023 int size = 100;
29024 mode_line_noprop_buf = (char *) xmalloc (size);
29025 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29026 mode_line_noprop_ptr = mode_line_noprop_buf;
29027 mode_line_target = MODE_LINE_DISPLAY;
29028 }
29029
29030 help_echo_showing_p = 0;
29031 }
29032
29033 /* Since w32 does not support atimers, it defines its own implementation of
29034 the following three functions in w32fns.c. */
29035 #ifndef WINDOWSNT
29036
29037 /* Platform-independent portion of hourglass implementation. */
29038
29039 /* Cancel a currently active hourglass timer, and start a new one. */
29040 void
29041 start_hourglass (void)
29042 {
29043 #if defined (HAVE_WINDOW_SYSTEM)
29044 EMACS_TIME delay;
29045 int secs = DEFAULT_HOURGLASS_DELAY, usecs = 0;
29046
29047 cancel_hourglass ();
29048
29049 if (NUMBERP (Vhourglass_delay))
29050 {
29051 double duration = extract_float (Vhourglass_delay);
29052 if (0 < duration)
29053 duration_to_sec_usec (duration, &secs, &usecs);
29054 }
29055
29056 EMACS_SET_SECS_USECS (delay, secs, usecs);
29057 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29058 show_hourglass, NULL);
29059 #endif
29060 }
29061
29062
29063 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29064 shown. */
29065 void
29066 cancel_hourglass (void)
29067 {
29068 #if defined (HAVE_WINDOW_SYSTEM)
29069 if (hourglass_atimer)
29070 {
29071 cancel_atimer (hourglass_atimer);
29072 hourglass_atimer = NULL;
29073 }
29074
29075 if (hourglass_shown_p)
29076 hide_hourglass ();
29077 #endif
29078 }
29079 #endif /* ! WINDOWSNT */