Drop FRAME_PTR typedef.
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2013 Free Software Foundation,
4 Inc.
5
6 This file is part of GNU Emacs.
7
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20
21 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
22
23 Redisplay.
24
25 Emacs separates the task of updating the display from code
26 modifying global state, e.g. buffer text. This way functions
27 operating on buffers don't also have to be concerned with updating
28 the display.
29
30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay'
34 in xdisp.c is the only entry into the inner redisplay code.
35
36 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. Under window systems
38 like X, some portions of the redisplay code are also called
39 asynchronously during mouse movement or expose events. It is very
40 important that these code parts do NOT use the C library (malloc,
41 free) because many C libraries under Unix are not reentrant. They
42 may also NOT call functions of the Lisp interpreter which could
43 change the interpreter's state. If you don't follow these rules,
44 you will encounter bugs which are very hard to explain.
45
46 +--------------+ redisplay +----------------+
47 | Lisp machine |---------------->| Redisplay code |<--+
48 +--------------+ (xdisp.c) +----------------+ |
49 ^ | |
50 +----------------------------------+ |
51 Don't use this path when called |
52 asynchronously! |
53 |
54 expose_window (asynchronous) |
55 |
56 X expose events -----+
57
58 What does redisplay do? Obviously, it has to figure out somehow what
59 has been changed since the last time the display has been updated,
60 and to make these changes visible. Preferably it would do that in
61 a moderately intelligent way, i.e. fast.
62
63 Changes in buffer text can be deduced from window and buffer
64 structures, and from some global variables like `beg_unchanged' and
65 `end_unchanged'. The contents of the display are additionally
66 recorded in a `glyph matrix', a two-dimensional matrix of glyph
67 structures. Each row in such a matrix corresponds to a line on the
68 display, and each glyph in a row corresponds to a column displaying
69 a character, an image, or what else. This matrix is called the
70 `current glyph matrix' or `current matrix' in redisplay
71 terminology.
72
73 For buffer parts that have been changed since the last update, a
74 second glyph matrix is constructed, the so called `desired glyph
75 matrix' or short `desired matrix'. Current and desired matrix are
76 then compared to find a cheap way to update the display, e.g. by
77 reusing part of the display by scrolling lines.
78
79 You will find a lot of redisplay optimizations when you start
80 looking at the innards of redisplay. The overall goal of all these
81 optimizations is to make redisplay fast because it is done
82 frequently. Some of these optimizations are implemented by the
83 following functions:
84
85 . try_cursor_movement
86
87 This function tries to update the display if the text in the
88 window did not change and did not scroll, only point moved, and
89 it did not move off the displayed portion of the text.
90
91 . try_window_reusing_current_matrix
92
93 This function reuses the current matrix of a window when text
94 has not changed, but the window start changed (e.g., due to
95 scrolling).
96
97 . try_window_id
98
99 This function attempts to redisplay a window by reusing parts of
100 its existing display. It finds and reuses the part that was not
101 changed, and redraws the rest.
102
103 . try_window
104
105 This function performs the full redisplay of a single window
106 assuming that its fonts were not changed and that the cursor
107 will not end up in the scroll margins. (Loading fonts requires
108 re-adjustment of dimensions of glyph matrices, which makes this
109 method impossible to use.)
110
111 These optimizations are tried in sequence (some can be skipped if
112 it is known that they are not applicable). If none of the
113 optimizations were successful, redisplay calls redisplay_windows,
114 which performs a full redisplay of all windows.
115
116 Desired matrices.
117
118 Desired matrices are always built per Emacs window. The function
119 `display_line' is the central function to look at if you are
120 interested. It constructs one row in a desired matrix given an
121 iterator structure containing both a buffer position and a
122 description of the environment in which the text is to be
123 displayed. But this is too early, read on.
124
125 Characters and pixmaps displayed for a range of buffer text depend
126 on various settings of buffers and windows, on overlays and text
127 properties, on display tables, on selective display. The good news
128 is that all this hairy stuff is hidden behind a small set of
129 interface functions taking an iterator structure (struct it)
130 argument.
131
132 Iteration over things to be displayed is then simple. It is
133 started by initializing an iterator with a call to init_iterator,
134 passing it the buffer position where to start iteration. For
135 iteration over strings, pass -1 as the position to init_iterator,
136 and call reseat_to_string when the string is ready, to initialize
137 the iterator for that string. Thereafter, calls to
138 get_next_display_element fill the iterator structure with relevant
139 information about the next thing to display. Calls to
140 set_iterator_to_next move the iterator to the next thing.
141
142 Besides this, an iterator also contains information about the
143 display environment in which glyphs for display elements are to be
144 produced. It has fields for the width and height of the display,
145 the information whether long lines are truncated or continued, a
146 current X and Y position, and lots of other stuff you can better
147 see in dispextern.h.
148
149 Glyphs in a desired matrix are normally constructed in a loop
150 calling get_next_display_element and then PRODUCE_GLYPHS. The call
151 to PRODUCE_GLYPHS will fill the iterator structure with pixel
152 information about the element being displayed and at the same time
153 produce glyphs for it. If the display element fits on the line
154 being displayed, set_iterator_to_next is called next, otherwise the
155 glyphs produced are discarded. The function display_line is the
156 workhorse of filling glyph rows in the desired matrix with glyphs.
157 In addition to producing glyphs, it also handles line truncation
158 and continuation, word wrap, and cursor positioning (for the
159 latter, see also set_cursor_from_row).
160
161 Frame matrices.
162
163 That just couldn't be all, could it? What about terminal types not
164 supporting operations on sub-windows of the screen? To update the
165 display on such a terminal, window-based glyph matrices are not
166 well suited. To be able to reuse part of the display (scrolling
167 lines up and down), we must instead have a view of the whole
168 screen. This is what `frame matrices' are for. They are a trick.
169
170 Frames on terminals like above have a glyph pool. Windows on such
171 a frame sub-allocate their glyph memory from their frame's glyph
172 pool. The frame itself is given its own glyph matrices. By
173 coincidence---or maybe something else---rows in window glyph
174 matrices are slices of corresponding rows in frame matrices. Thus
175 writing to window matrices implicitly updates a frame matrix which
176 provides us with the view of the whole screen that we originally
177 wanted to have without having to move many bytes around. To be
178 honest, there is a little bit more done, but not much more. If you
179 plan to extend that code, take a look at dispnew.c. The function
180 build_frame_matrix is a good starting point.
181
182 Bidirectional display.
183
184 Bidirectional display adds quite some hair to this already complex
185 design. The good news are that a large portion of that hairy stuff
186 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
187 reordering engine which is called by set_iterator_to_next and
188 returns the next character to display in the visual order. See
189 commentary on bidi.c for more details. As far as redisplay is
190 concerned, the effect of calling bidi_move_to_visually_next, the
191 main interface of the reordering engine, is that the iterator gets
192 magically placed on the buffer or string position that is to be
193 displayed next. In other words, a linear iteration through the
194 buffer/string is replaced with a non-linear one. All the rest of
195 the redisplay is oblivious to the bidi reordering.
196
197 Well, almost oblivious---there are still complications, most of
198 them due to the fact that buffer and string positions no longer
199 change monotonously with glyph indices in a glyph row. Moreover,
200 for continued lines, the buffer positions may not even be
201 monotonously changing with vertical positions. Also, accounting
202 for face changes, overlays, etc. becomes more complex because
203 non-linear iteration could potentially skip many positions with
204 changes, and then cross them again on the way back...
205
206 One other prominent effect of bidirectional display is that some
207 paragraphs of text need to be displayed starting at the right
208 margin of the window---the so-called right-to-left, or R2L
209 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
210 which have their reversed_p flag set. The bidi reordering engine
211 produces characters in such rows starting from the character which
212 should be the rightmost on display. PRODUCE_GLYPHS then reverses
213 the order, when it fills up the glyph row whose reversed_p flag is
214 set, by prepending each new glyph to what is already there, instead
215 of appending it. When the glyph row is complete, the function
216 extend_face_to_end_of_line fills the empty space to the left of the
217 leftmost character with special glyphs, which will display as,
218 well, empty. On text terminals, these special glyphs are simply
219 blank characters. On graphics terminals, there's a single stretch
220 glyph of a suitably computed width. Both the blanks and the
221 stretch glyph are given the face of the background of the line.
222 This way, the terminal-specific back-end can still draw the glyphs
223 left to right, even for R2L lines.
224
225 Bidirectional display and character compositions
226
227 Some scripts cannot be displayed by drawing each character
228 individually, because adjacent characters change each other's shape
229 on display. For example, Arabic and Indic scripts belong to this
230 category.
231
232 Emacs display supports this by providing "character compositions",
233 most of which is implemented in composite.c. During the buffer
234 scan that delivers characters to PRODUCE_GLYPHS, if the next
235 character to be delivered is a composed character, the iteration
236 calls composition_reseat_it and next_element_from_composition. If
237 they succeed to compose the character with one or more of the
238 following characters, the whole sequence of characters that where
239 composed is recorded in the `struct composition_it' object that is
240 part of the buffer iterator. The composed sequence could produce
241 one or more font glyphs (called "grapheme clusters") on the screen.
242 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
243 in the direction corresponding to the current bidi scan direction
244 (recorded in the scan_dir member of the `struct bidi_it' object
245 that is part of the buffer iterator). In particular, if the bidi
246 iterator currently scans the buffer backwards, the grapheme
247 clusters are delivered back to front. This reorders the grapheme
248 clusters as appropriate for the current bidi context. Note that
249 this means that the grapheme clusters are always stored in the
250 LGSTRING object (see composite.c) in the logical order.
251
252 Moving an iterator in bidirectional text
253 without producing glyphs
254
255 Note one important detail mentioned above: that the bidi reordering
256 engine, driven by the iterator, produces characters in R2L rows
257 starting at the character that will be the rightmost on display.
258 As far as the iterator is concerned, the geometry of such rows is
259 still left to right, i.e. the iterator "thinks" the first character
260 is at the leftmost pixel position. The iterator does not know that
261 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
262 delivers. This is important when functions from the move_it_*
263 family are used to get to certain screen position or to match
264 screen coordinates with buffer coordinates: these functions use the
265 iterator geometry, which is left to right even in R2L paragraphs.
266 This works well with most callers of move_it_*, because they need
267 to get to a specific column, and columns are still numbered in the
268 reading order, i.e. the rightmost character in a R2L paragraph is
269 still column zero. But some callers do not get well with this; a
270 notable example is mouse clicks that need to find the character
271 that corresponds to certain pixel coordinates. See
272 buffer_posn_from_coords in dispnew.c for how this is handled. */
273
274 #include <config.h>
275 #include <stdio.h>
276 #include <limits.h>
277
278 #include "lisp.h"
279 #include "atimer.h"
280 #include "keyboard.h"
281 #include "frame.h"
282 #include "window.h"
283 #include "termchar.h"
284 #include "dispextern.h"
285 #include "character.h"
286 #include "buffer.h"
287 #include "charset.h"
288 #include "indent.h"
289 #include "commands.h"
290 #include "keymap.h"
291 #include "macros.h"
292 #include "disptab.h"
293 #include "termhooks.h"
294 #include "termopts.h"
295 #include "intervals.h"
296 #include "coding.h"
297 #include "process.h"
298 #include "region-cache.h"
299 #include "font.h"
300 #include "fontset.h"
301 #include "blockinput.h"
302
303 #ifdef HAVE_X_WINDOWS
304 #include "xterm.h"
305 #endif
306 #ifdef HAVE_NTGUI
307 #include "w32term.h"
308 #endif
309 #ifdef HAVE_NS
310 #include "nsterm.h"
311 #endif
312 #ifdef USE_GTK
313 #include "gtkutil.h"
314 #endif
315
316 #ifndef FRAME_X_OUTPUT
317 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
318 #endif
319
320 #define INFINITY 10000000
321
322 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
323 Lisp_Object Qwindow_scroll_functions;
324 static Lisp_Object Qwindow_text_change_functions;
325 static Lisp_Object Qredisplay_end_trigger_functions;
326 Lisp_Object Qinhibit_point_motion_hooks;
327 static Lisp_Object QCeval, QCpropertize;
328 Lisp_Object QCfile, QCdata;
329 static Lisp_Object Qfontified;
330 static Lisp_Object Qgrow_only;
331 static Lisp_Object Qinhibit_eval_during_redisplay;
332 static Lisp_Object Qbuffer_position, Qposition, Qobject;
333 static Lisp_Object Qright_to_left, Qleft_to_right;
334
335 /* Cursor shapes. */
336 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
337
338 /* Pointer shapes. */
339 static Lisp_Object Qarrow, Qhand;
340 Lisp_Object Qtext;
341
342 /* Holds the list (error). */
343 static Lisp_Object list_of_error;
344
345 static Lisp_Object Qfontification_functions;
346
347 static Lisp_Object Qwrap_prefix;
348 static Lisp_Object Qline_prefix;
349 static Lisp_Object Qredisplay_internal;
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
381 #else /* !HAVE_WINDOW_SYSTEM */
382 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
383 #endif /* HAVE_WINDOW_SYSTEM */
384
385 /* Test if the display element loaded in IT, or the underlying buffer
386 or string character, is a space or a TAB character. This is used
387 to determine where word wrapping can occur. */
388
389 #define IT_DISPLAYING_WHITESPACE(it) \
390 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
391 || ((STRINGP (it->string) \
392 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
393 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
394 || (it->s \
395 && (it->s[IT_BYTEPOS (*it)] == ' ' \
396 || it->s[IT_BYTEPOS (*it)] == '\t')) \
397 || (IT_BYTEPOS (*it) < ZV_BYTE \
398 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
399 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
400
401 /* Name of the face used to highlight trailing whitespace. */
402
403 static Lisp_Object Qtrailing_whitespace;
404
405 /* Name and number of the face used to highlight escape glyphs. */
406
407 static Lisp_Object Qescape_glyph;
408
409 /* Name and number of the face used to highlight non-breaking spaces. */
410
411 static Lisp_Object Qnobreak_space;
412
413 /* The symbol `image' which is the car of the lists used to represent
414 images in Lisp. Also a tool bar style. */
415
416 Lisp_Object Qimage;
417
418 /* The image map types. */
419 Lisp_Object QCmap;
420 static Lisp_Object QCpointer;
421 static Lisp_Object Qrect, Qcircle, Qpoly;
422
423 /* Tool bar styles */
424 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
425
426 /* Non-zero means print newline to stdout before next mini-buffer
427 message. */
428
429 int noninteractive_need_newline;
430
431 /* Non-zero means print newline to message log before next message. */
432
433 static int message_log_need_newline;
434
435 /* Three markers that message_dolog uses.
436 It could allocate them itself, but that causes trouble
437 in handling memory-full errors. */
438 static Lisp_Object message_dolog_marker1;
439 static Lisp_Object message_dolog_marker2;
440 static Lisp_Object message_dolog_marker3;
441 \f
442 /* The buffer position of the first character appearing entirely or
443 partially on the line of the selected window which contains the
444 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
445 redisplay optimization in redisplay_internal. */
446
447 static struct text_pos this_line_start_pos;
448
449 /* Number of characters past the end of the line above, including the
450 terminating newline. */
451
452 static struct text_pos this_line_end_pos;
453
454 /* The vertical positions and the height of this line. */
455
456 static int this_line_vpos;
457 static int this_line_y;
458 static int this_line_pixel_height;
459
460 /* X position at which this display line starts. Usually zero;
461 negative if first character is partially visible. */
462
463 static int this_line_start_x;
464
465 /* The smallest character position seen by move_it_* functions as they
466 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
467 hscrolled lines, see display_line. */
468
469 static struct text_pos this_line_min_pos;
470
471 /* Buffer that this_line_.* variables are referring to. */
472
473 static struct buffer *this_line_buffer;
474
475
476 /* Values of those variables at last redisplay are stored as
477 properties on `overlay-arrow-position' symbol. However, if
478 Voverlay_arrow_position is a marker, last-arrow-position is its
479 numerical position. */
480
481 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
482
483 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
484 properties on a symbol in overlay-arrow-variable-list. */
485
486 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
487
488 Lisp_Object Qmenu_bar_update_hook;
489
490 /* Nonzero if an overlay arrow has been displayed in this window. */
491
492 static int overlay_arrow_seen;
493
494 /* Vector containing glyphs for an ellipsis `...'. */
495
496 static Lisp_Object default_invis_vector[3];
497
498 /* This is the window where the echo area message was displayed. It
499 is always a mini-buffer window, but it may not be the same window
500 currently active as a mini-buffer. */
501
502 Lisp_Object echo_area_window;
503
504 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
505 pushes the current message and the value of
506 message_enable_multibyte on the stack, the function restore_message
507 pops the stack and displays MESSAGE again. */
508
509 static Lisp_Object Vmessage_stack;
510
511 /* Nonzero means multibyte characters were enabled when the echo area
512 message was specified. */
513
514 static int message_enable_multibyte;
515
516 /* Nonzero if we should redraw the mode lines on the next redisplay. */
517
518 int update_mode_lines;
519
520 /* Nonzero if window sizes or contents have changed since last
521 redisplay that finished. */
522
523 int windows_or_buffers_changed;
524
525 /* Nonzero means a frame's cursor type has been changed. */
526
527 int cursor_type_changed;
528
529 /* Nonzero after display_mode_line if %l was used and it displayed a
530 line number. */
531
532 static int line_number_displayed;
533
534 /* The name of the *Messages* buffer, a string. */
535
536 static Lisp_Object Vmessages_buffer_name;
537
538 /* Current, index 0, and last displayed echo area message. Either
539 buffers from echo_buffers, or nil to indicate no message. */
540
541 Lisp_Object echo_area_buffer[2];
542
543 /* The buffers referenced from echo_area_buffer. */
544
545 static Lisp_Object echo_buffer[2];
546
547 /* A vector saved used in with_area_buffer to reduce consing. */
548
549 static Lisp_Object Vwith_echo_area_save_vector;
550
551 /* Non-zero means display_echo_area should display the last echo area
552 message again. Set by redisplay_preserve_echo_area. */
553
554 static int display_last_displayed_message_p;
555
556 /* Nonzero if echo area is being used by print; zero if being used by
557 message. */
558
559 static int message_buf_print;
560
561 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
562
563 static Lisp_Object Qinhibit_menubar_update;
564 static Lisp_Object Qmessage_truncate_lines;
565
566 /* Set to 1 in clear_message to make redisplay_internal aware
567 of an emptied echo area. */
568
569 static int message_cleared_p;
570
571 /* A scratch glyph row with contents used for generating truncation
572 glyphs. Also used in direct_output_for_insert. */
573
574 #define MAX_SCRATCH_GLYPHS 100
575 static struct glyph_row scratch_glyph_row;
576 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
577
578 /* Ascent and height of the last line processed by move_it_to. */
579
580 static int last_height;
581
582 /* Non-zero if there's a help-echo in the echo area. */
583
584 int help_echo_showing_p;
585
586 /* If >= 0, computed, exact values of mode-line and header-line height
587 to use in the macros CURRENT_MODE_LINE_HEIGHT and
588 CURRENT_HEADER_LINE_HEIGHT. */
589
590 int current_mode_line_height, current_header_line_height;
591
592 /* The maximum distance to look ahead for text properties. Values
593 that are too small let us call compute_char_face and similar
594 functions too often which is expensive. Values that are too large
595 let us call compute_char_face and alike too often because we
596 might not be interested in text properties that far away. */
597
598 #define TEXT_PROP_DISTANCE_LIMIT 100
599
600 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
601 iterator state and later restore it. This is needed because the
602 bidi iterator on bidi.c keeps a stacked cache of its states, which
603 is really a singleton. When we use scratch iterator objects to
604 move around the buffer, we can cause the bidi cache to be pushed or
605 popped, and therefore we need to restore the cache state when we
606 return to the original iterator. */
607 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
608 do { \
609 if (CACHE) \
610 bidi_unshelve_cache (CACHE, 1); \
611 ITCOPY = ITORIG; \
612 CACHE = bidi_shelve_cache (); \
613 } while (0)
614
615 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
616 do { \
617 if (pITORIG != pITCOPY) \
618 *(pITORIG) = *(pITCOPY); \
619 bidi_unshelve_cache (CACHE, 0); \
620 CACHE = NULL; \
621 } while (0)
622
623 #ifdef GLYPH_DEBUG
624
625 /* Non-zero means print traces of redisplay if compiled with
626 GLYPH_DEBUG defined. */
627
628 int trace_redisplay_p;
629
630 #endif /* GLYPH_DEBUG */
631
632 #ifdef DEBUG_TRACE_MOVE
633 /* Non-zero means trace with TRACE_MOVE to stderr. */
634 int trace_move;
635
636 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
637 #else
638 #define TRACE_MOVE(x) (void) 0
639 #endif
640
641 static Lisp_Object Qauto_hscroll_mode;
642
643 /* Buffer being redisplayed -- for redisplay_window_error. */
644
645 static struct buffer *displayed_buffer;
646
647 /* Value returned from text property handlers (see below). */
648
649 enum prop_handled
650 {
651 HANDLED_NORMALLY,
652 HANDLED_RECOMPUTE_PROPS,
653 HANDLED_OVERLAY_STRING_CONSUMED,
654 HANDLED_RETURN
655 };
656
657 /* A description of text properties that redisplay is interested
658 in. */
659
660 struct props
661 {
662 /* The name of the property. */
663 Lisp_Object *name;
664
665 /* A unique index for the property. */
666 enum prop_idx idx;
667
668 /* A handler function called to set up iterator IT from the property
669 at IT's current position. Value is used to steer handle_stop. */
670 enum prop_handled (*handler) (struct it *it);
671 };
672
673 static enum prop_handled handle_face_prop (struct it *);
674 static enum prop_handled handle_invisible_prop (struct it *);
675 static enum prop_handled handle_display_prop (struct it *);
676 static enum prop_handled handle_composition_prop (struct it *);
677 static enum prop_handled handle_overlay_change (struct it *);
678 static enum prop_handled handle_fontified_prop (struct it *);
679
680 /* Properties handled by iterators. */
681
682 static struct props it_props[] =
683 {
684 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
685 /* Handle `face' before `display' because some sub-properties of
686 `display' need to know the face. */
687 {&Qface, FACE_PROP_IDX, handle_face_prop},
688 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
689 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
690 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
691 {NULL, 0, NULL}
692 };
693
694 /* Value is the position described by X. If X is a marker, value is
695 the marker_position of X. Otherwise, value is X. */
696
697 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
698
699 /* Enumeration returned by some move_it_.* functions internally. */
700
701 enum move_it_result
702 {
703 /* Not used. Undefined value. */
704 MOVE_UNDEFINED,
705
706 /* Move ended at the requested buffer position or ZV. */
707 MOVE_POS_MATCH_OR_ZV,
708
709 /* Move ended at the requested X pixel position. */
710 MOVE_X_REACHED,
711
712 /* Move within a line ended at the end of a line that must be
713 continued. */
714 MOVE_LINE_CONTINUED,
715
716 /* Move within a line ended at the end of a line that would
717 be displayed truncated. */
718 MOVE_LINE_TRUNCATED,
719
720 /* Move within a line ended at a line end. */
721 MOVE_NEWLINE_OR_CR
722 };
723
724 /* This counter is used to clear the face cache every once in a while
725 in redisplay_internal. It is incremented for each redisplay.
726 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
727 cleared. */
728
729 #define CLEAR_FACE_CACHE_COUNT 500
730 static int clear_face_cache_count;
731
732 /* Similarly for the image cache. */
733
734 #ifdef HAVE_WINDOW_SYSTEM
735 #define CLEAR_IMAGE_CACHE_COUNT 101
736 static int clear_image_cache_count;
737
738 /* Null glyph slice */
739 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
740 #endif
741
742 /* True while redisplay_internal is in progress. */
743
744 bool redisplaying_p;
745
746 static Lisp_Object Qinhibit_free_realized_faces;
747 static Lisp_Object Qmode_line_default_help_echo;
748
749 /* If a string, XTread_socket generates an event to display that string.
750 (The display is done in read_char.) */
751
752 Lisp_Object help_echo_string;
753 Lisp_Object help_echo_window;
754 Lisp_Object help_echo_object;
755 ptrdiff_t help_echo_pos;
756
757 /* Temporary variable for XTread_socket. */
758
759 Lisp_Object previous_help_echo_string;
760
761 /* Platform-independent portion of hourglass implementation. */
762
763 /* Non-zero means an hourglass cursor is currently shown. */
764 int hourglass_shown_p;
765
766 /* If non-null, an asynchronous timer that, when it expires, displays
767 an hourglass cursor on all frames. */
768 struct atimer *hourglass_atimer;
769
770 /* Name of the face used to display glyphless characters. */
771 Lisp_Object Qglyphless_char;
772
773 /* Symbol for the purpose of Vglyphless_char_display. */
774 static Lisp_Object Qglyphless_char_display;
775
776 /* Method symbols for Vglyphless_char_display. */
777 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
778
779 /* Default pixel width of `thin-space' display method. */
780 #define THIN_SPACE_WIDTH 1
781
782 /* Default number of seconds to wait before displaying an hourglass
783 cursor. */
784 #define DEFAULT_HOURGLASS_DELAY 1
785
786 \f
787 /* Function prototypes. */
788
789 static void setup_for_ellipsis (struct it *, int);
790 static void set_iterator_to_next (struct it *, int);
791 static void mark_window_display_accurate_1 (struct window *, int);
792 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
793 static int display_prop_string_p (Lisp_Object, Lisp_Object);
794 static int row_for_charpos_p (struct glyph_row *, ptrdiff_t);
795 static int cursor_row_p (struct glyph_row *);
796 static int redisplay_mode_lines (Lisp_Object, int);
797 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
798
799 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
800
801 static void handle_line_prefix (struct it *);
802
803 static void pint2str (char *, int, ptrdiff_t);
804 static void pint2hrstr (char *, int, ptrdiff_t);
805 static struct text_pos run_window_scroll_functions (Lisp_Object,
806 struct text_pos);
807 static void reconsider_clip_changes (struct window *, struct buffer *);
808 static int text_outside_line_unchanged_p (struct window *,
809 ptrdiff_t, ptrdiff_t);
810 static void store_mode_line_noprop_char (char);
811 static int store_mode_line_noprop (const char *, int, int);
812 static void handle_stop (struct it *);
813 static void handle_stop_backwards (struct it *, ptrdiff_t);
814 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
815 static void ensure_echo_area_buffers (void);
816 static void unwind_with_echo_area_buffer (Lisp_Object);
817 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
818 static int with_echo_area_buffer (struct window *, int,
819 int (*) (ptrdiff_t, Lisp_Object),
820 ptrdiff_t, Lisp_Object);
821 static void clear_garbaged_frames (void);
822 static int current_message_1 (ptrdiff_t, Lisp_Object);
823 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
824 static void set_message (Lisp_Object);
825 static int set_message_1 (ptrdiff_t, Lisp_Object);
826 static int display_echo_area (struct window *);
827 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
828 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
829 static void unwind_redisplay (void);
830 static int string_char_and_length (const unsigned char *, int *);
831 static struct text_pos display_prop_end (struct it *, Lisp_Object,
832 struct text_pos);
833 static int compute_window_start_on_continuation_line (struct window *);
834 static void insert_left_trunc_glyphs (struct it *);
835 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
836 Lisp_Object);
837 static void extend_face_to_end_of_line (struct it *);
838 static int append_space_for_newline (struct it *, int);
839 static int cursor_row_fully_visible_p (struct window *, int, int);
840 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
841 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
842 static int trailing_whitespace_p (ptrdiff_t);
843 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
844 static void push_it (struct it *, struct text_pos *);
845 static void iterate_out_of_display_property (struct it *);
846 static void pop_it (struct it *);
847 static void sync_frame_with_window_matrix_rows (struct window *);
848 static void redisplay_internal (void);
849 static int echo_area_display (int);
850 static void redisplay_windows (Lisp_Object);
851 static void redisplay_window (Lisp_Object, int);
852 static Lisp_Object redisplay_window_error (Lisp_Object);
853 static Lisp_Object redisplay_window_0 (Lisp_Object);
854 static Lisp_Object redisplay_window_1 (Lisp_Object);
855 static int set_cursor_from_row (struct window *, struct glyph_row *,
856 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
857 int, int);
858 static int update_menu_bar (struct frame *, int, int);
859 static int try_window_reusing_current_matrix (struct window *);
860 static int try_window_id (struct window *);
861 static int display_line (struct it *);
862 static int display_mode_lines (struct window *);
863 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
864 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
865 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
866 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
867 static void display_menu_bar (struct window *);
868 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
869 ptrdiff_t *);
870 static int display_string (const char *, Lisp_Object, Lisp_Object,
871 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
872 static void compute_line_metrics (struct it *);
873 static void run_redisplay_end_trigger_hook (struct it *);
874 static int get_overlay_strings (struct it *, ptrdiff_t);
875 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
876 static void next_overlay_string (struct it *);
877 static void reseat (struct it *, struct text_pos, int);
878 static void reseat_1 (struct it *, struct text_pos, int);
879 static void back_to_previous_visible_line_start (struct it *);
880 static void reseat_at_next_visible_line_start (struct it *, int);
881 static int next_element_from_ellipsis (struct it *);
882 static int next_element_from_display_vector (struct it *);
883 static int next_element_from_string (struct it *);
884 static int next_element_from_c_string (struct it *);
885 static int next_element_from_buffer (struct it *);
886 static int next_element_from_composition (struct it *);
887 static int next_element_from_image (struct it *);
888 static int next_element_from_stretch (struct it *);
889 static void load_overlay_strings (struct it *, ptrdiff_t);
890 static int init_from_display_pos (struct it *, struct window *,
891 struct display_pos *);
892 static void reseat_to_string (struct it *, const char *,
893 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
894 static int get_next_display_element (struct it *);
895 static enum move_it_result
896 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
897 enum move_operation_enum);
898 static void get_visually_first_element (struct it *);
899 static void init_to_row_start (struct it *, struct window *,
900 struct glyph_row *);
901 static int init_to_row_end (struct it *, struct window *,
902 struct glyph_row *);
903 static void back_to_previous_line_start (struct it *);
904 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
905 static struct text_pos string_pos_nchars_ahead (struct text_pos,
906 Lisp_Object, ptrdiff_t);
907 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
908 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
909 static ptrdiff_t number_of_chars (const char *, bool);
910 static void compute_stop_pos (struct it *);
911 static void compute_string_pos (struct text_pos *, struct text_pos,
912 Lisp_Object);
913 static int face_before_or_after_it_pos (struct it *, int);
914 static ptrdiff_t next_overlay_change (ptrdiff_t);
915 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
916 Lisp_Object, struct text_pos *, ptrdiff_t, int);
917 static int handle_single_display_spec (struct it *, Lisp_Object,
918 Lisp_Object, Lisp_Object,
919 struct text_pos *, ptrdiff_t, int, int);
920 static int underlying_face_id (struct it *);
921 static int in_ellipses_for_invisible_text_p (struct display_pos *,
922 struct window *);
923
924 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
925 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
926
927 #ifdef HAVE_WINDOW_SYSTEM
928
929 static void x_consider_frame_title (Lisp_Object);
930 static int tool_bar_lines_needed (struct frame *, int *);
931 static void update_tool_bar (struct frame *, int);
932 static void build_desired_tool_bar_string (struct frame *f);
933 static int redisplay_tool_bar (struct frame *);
934 static void display_tool_bar_line (struct it *, int);
935 static void notice_overwritten_cursor (struct window *,
936 enum glyph_row_area,
937 int, int, int, int);
938 static void append_stretch_glyph (struct it *, Lisp_Object,
939 int, int, int);
940
941
942 #endif /* HAVE_WINDOW_SYSTEM */
943
944 static void produce_special_glyphs (struct it *, enum display_element_type);
945 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
946 static int coords_in_mouse_face_p (struct window *, int, int);
947
948
949 \f
950 /***********************************************************************
951 Window display dimensions
952 ***********************************************************************/
953
954 /* Return the bottom boundary y-position for text lines in window W.
955 This is the first y position at which a line cannot start.
956 It is relative to the top of the window.
957
958 This is the height of W minus the height of a mode line, if any. */
959
960 int
961 window_text_bottom_y (struct window *w)
962 {
963 int height = WINDOW_TOTAL_HEIGHT (w);
964
965 if (WINDOW_WANTS_MODELINE_P (w))
966 height -= CURRENT_MODE_LINE_HEIGHT (w);
967 return height;
968 }
969
970 /* Return the pixel width of display area AREA of window W. AREA < 0
971 means return the total width of W, not including fringes to
972 the left and right of the window. */
973
974 int
975 window_box_width (struct window *w, int area)
976 {
977 int cols = w->total_cols;
978 int pixels = 0;
979
980 if (!w->pseudo_window_p)
981 {
982 cols -= WINDOW_SCROLL_BAR_COLS (w);
983
984 if (area == TEXT_AREA)
985 {
986 if (INTEGERP (w->left_margin_cols))
987 cols -= XFASTINT (w->left_margin_cols);
988 if (INTEGERP (w->right_margin_cols))
989 cols -= XFASTINT (w->right_margin_cols);
990 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
991 }
992 else if (area == LEFT_MARGIN_AREA)
993 {
994 cols = (INTEGERP (w->left_margin_cols)
995 ? XFASTINT (w->left_margin_cols) : 0);
996 pixels = 0;
997 }
998 else if (area == RIGHT_MARGIN_AREA)
999 {
1000 cols = (INTEGERP (w->right_margin_cols)
1001 ? XFASTINT (w->right_margin_cols) : 0);
1002 pixels = 0;
1003 }
1004 }
1005
1006 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1007 }
1008
1009
1010 /* Return the pixel height of the display area of window W, not
1011 including mode lines of W, if any. */
1012
1013 int
1014 window_box_height (struct window *w)
1015 {
1016 struct frame *f = XFRAME (w->frame);
1017 int height = WINDOW_TOTAL_HEIGHT (w);
1018
1019 eassert (height >= 0);
1020
1021 /* Note: the code below that determines the mode-line/header-line
1022 height is essentially the same as that contained in the macro
1023 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1024 the appropriate glyph row has its `mode_line_p' flag set,
1025 and if it doesn't, uses estimate_mode_line_height instead. */
1026
1027 if (WINDOW_WANTS_MODELINE_P (w))
1028 {
1029 struct glyph_row *ml_row
1030 = (w->current_matrix && w->current_matrix->rows
1031 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1032 : 0);
1033 if (ml_row && ml_row->mode_line_p)
1034 height -= ml_row->height;
1035 else
1036 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1037 }
1038
1039 if (WINDOW_WANTS_HEADER_LINE_P (w))
1040 {
1041 struct glyph_row *hl_row
1042 = (w->current_matrix && w->current_matrix->rows
1043 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1044 : 0);
1045 if (hl_row && hl_row->mode_line_p)
1046 height -= hl_row->height;
1047 else
1048 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1049 }
1050
1051 /* With a very small font and a mode-line that's taller than
1052 default, we might end up with a negative height. */
1053 return max (0, height);
1054 }
1055
1056 /* Return the window-relative coordinate of the left edge of display
1057 area AREA of window W. AREA < 0 means return the left edge of the
1058 whole window, to the right of the left fringe of W. */
1059
1060 int
1061 window_box_left_offset (struct window *w, int area)
1062 {
1063 int x;
1064
1065 if (w->pseudo_window_p)
1066 return 0;
1067
1068 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1069
1070 if (area == TEXT_AREA)
1071 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1072 + window_box_width (w, LEFT_MARGIN_AREA));
1073 else if (area == RIGHT_MARGIN_AREA)
1074 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1075 + window_box_width (w, LEFT_MARGIN_AREA)
1076 + window_box_width (w, TEXT_AREA)
1077 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1078 ? 0
1079 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1080 else if (area == LEFT_MARGIN_AREA
1081 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1082 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1083
1084 return x;
1085 }
1086
1087
1088 /* Return the window-relative coordinate of the right edge of display
1089 area AREA of window W. AREA < 0 means return the right edge of the
1090 whole window, to the left of the right fringe of W. */
1091
1092 int
1093 window_box_right_offset (struct window *w, int area)
1094 {
1095 return window_box_left_offset (w, area) + window_box_width (w, area);
1096 }
1097
1098 /* Return the frame-relative coordinate of the left edge of display
1099 area AREA of window W. AREA < 0 means return the left edge of the
1100 whole window, to the right of the left fringe of W. */
1101
1102 int
1103 window_box_left (struct window *w, int area)
1104 {
1105 struct frame *f = XFRAME (w->frame);
1106 int x;
1107
1108 if (w->pseudo_window_p)
1109 return FRAME_INTERNAL_BORDER_WIDTH (f);
1110
1111 x = (WINDOW_LEFT_EDGE_X (w)
1112 + window_box_left_offset (w, area));
1113
1114 return x;
1115 }
1116
1117
1118 /* Return the frame-relative coordinate of the right edge of display
1119 area AREA of window W. AREA < 0 means return the right edge of the
1120 whole window, to the left of the right fringe of W. */
1121
1122 int
1123 window_box_right (struct window *w, int area)
1124 {
1125 return window_box_left (w, area) + window_box_width (w, area);
1126 }
1127
1128 /* Get the bounding box of the display area AREA of window W, without
1129 mode lines, in frame-relative coordinates. AREA < 0 means the
1130 whole window, not including the left and right fringes of
1131 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1132 coordinates of the upper-left corner of the box. Return in
1133 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1134
1135 void
1136 window_box (struct window *w, int area, int *box_x, int *box_y,
1137 int *box_width, int *box_height)
1138 {
1139 if (box_width)
1140 *box_width = window_box_width (w, area);
1141 if (box_height)
1142 *box_height = window_box_height (w);
1143 if (box_x)
1144 *box_x = window_box_left (w, area);
1145 if (box_y)
1146 {
1147 *box_y = WINDOW_TOP_EDGE_Y (w);
1148 if (WINDOW_WANTS_HEADER_LINE_P (w))
1149 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1150 }
1151 }
1152
1153
1154 /* Get the bounding box of the display area AREA of window W, without
1155 mode lines. AREA < 0 means the whole window, not including the
1156 left and right fringe of the window. Return in *TOP_LEFT_X
1157 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1158 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1159 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1160 box. */
1161
1162 static void
1163 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1164 int *bottom_right_x, int *bottom_right_y)
1165 {
1166 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1167 bottom_right_y);
1168 *bottom_right_x += *top_left_x;
1169 *bottom_right_y += *top_left_y;
1170 }
1171
1172
1173 \f
1174 /***********************************************************************
1175 Utilities
1176 ***********************************************************************/
1177
1178 /* Return the bottom y-position of the line the iterator IT is in.
1179 This can modify IT's settings. */
1180
1181 int
1182 line_bottom_y (struct it *it)
1183 {
1184 int line_height = it->max_ascent + it->max_descent;
1185 int line_top_y = it->current_y;
1186
1187 if (line_height == 0)
1188 {
1189 if (last_height)
1190 line_height = last_height;
1191 else if (IT_CHARPOS (*it) < ZV)
1192 {
1193 move_it_by_lines (it, 1);
1194 line_height = (it->max_ascent || it->max_descent
1195 ? it->max_ascent + it->max_descent
1196 : last_height);
1197 }
1198 else
1199 {
1200 struct glyph_row *row = it->glyph_row;
1201
1202 /* Use the default character height. */
1203 it->glyph_row = NULL;
1204 it->what = IT_CHARACTER;
1205 it->c = ' ';
1206 it->len = 1;
1207 PRODUCE_GLYPHS (it);
1208 line_height = it->ascent + it->descent;
1209 it->glyph_row = row;
1210 }
1211 }
1212
1213 return line_top_y + line_height;
1214 }
1215
1216 DEFUN ("line-pixel-height", Fline_pixel_height,
1217 Sline_pixel_height, 0, 0, 0,
1218 doc: /* Return height in pixels of text line in the selected window.
1219
1220 Value is the height in pixels of the line at point. */)
1221 (void)
1222 {
1223 struct it it;
1224 struct text_pos pt;
1225 struct window *w = XWINDOW (selected_window);
1226
1227 SET_TEXT_POS (pt, PT, PT_BYTE);
1228 start_display (&it, w, pt);
1229 it.vpos = it.current_y = 0;
1230 last_height = 0;
1231 return make_number (line_bottom_y (&it));
1232 }
1233
1234 /* Return the default pixel height of text lines in window W. The
1235 value is the canonical height of the W frame's default font, plus
1236 any extra space required by the line-spacing variable or frame
1237 parameter.
1238
1239 Implementation note: this ignores any line-spacing text properties
1240 put on the newline characters. This is because those properties
1241 only affect the _screen_ line ending in the newline (i.e., in a
1242 continued line, only the last screen line will be affected), which
1243 means only a small number of lines in a buffer can ever use this
1244 feature. Since this function is used to compute the default pixel
1245 equivalent of text lines in a window, we can safely ignore those
1246 few lines. For the same reasons, we ignore the line-height
1247 properties. */
1248 int
1249 default_line_pixel_height (struct window *w)
1250 {
1251 struct frame *f = WINDOW_XFRAME (w);
1252 int height = FRAME_LINE_HEIGHT (f);
1253
1254 if (!FRAME_INITIAL_P (f) && BUFFERP (w->contents))
1255 {
1256 struct buffer *b = XBUFFER (w->contents);
1257 Lisp_Object val = BVAR (b, extra_line_spacing);
1258
1259 if (NILP (val))
1260 val = BVAR (&buffer_defaults, extra_line_spacing);
1261 if (!NILP (val))
1262 {
1263 if (RANGED_INTEGERP (0, val, INT_MAX))
1264 height += XFASTINT (val);
1265 else if (FLOATP (val))
1266 {
1267 int addon = XFLOAT_DATA (val) * height + 0.5;
1268
1269 if (addon >= 0)
1270 height += addon;
1271 }
1272 }
1273 else
1274 height += f->extra_line_spacing;
1275 }
1276
1277 return height;
1278 }
1279
1280 /* Subroutine of pos_visible_p below. Extracts a display string, if
1281 any, from the display spec given as its argument. */
1282 static Lisp_Object
1283 string_from_display_spec (Lisp_Object spec)
1284 {
1285 if (CONSP (spec))
1286 {
1287 while (CONSP (spec))
1288 {
1289 if (STRINGP (XCAR (spec)))
1290 return XCAR (spec);
1291 spec = XCDR (spec);
1292 }
1293 }
1294 else if (VECTORP (spec))
1295 {
1296 ptrdiff_t i;
1297
1298 for (i = 0; i < ASIZE (spec); i++)
1299 {
1300 if (STRINGP (AREF (spec, i)))
1301 return AREF (spec, i);
1302 }
1303 return Qnil;
1304 }
1305
1306 return spec;
1307 }
1308
1309
1310 /* Limit insanely large values of W->hscroll on frame F to the largest
1311 value that will still prevent first_visible_x and last_visible_x of
1312 'struct it' from overflowing an int. */
1313 static int
1314 window_hscroll_limited (struct window *w, struct frame *f)
1315 {
1316 ptrdiff_t window_hscroll = w->hscroll;
1317 int window_text_width = window_box_width (w, TEXT_AREA);
1318 int colwidth = FRAME_COLUMN_WIDTH (f);
1319
1320 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1321 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1322
1323 return window_hscroll;
1324 }
1325
1326 /* Return 1 if position CHARPOS is visible in window W.
1327 CHARPOS < 0 means return info about WINDOW_END position.
1328 If visible, set *X and *Y to pixel coordinates of top left corner.
1329 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1330 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1331
1332 int
1333 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1334 int *rtop, int *rbot, int *rowh, int *vpos)
1335 {
1336 struct it it;
1337 void *itdata = bidi_shelve_cache ();
1338 struct text_pos top;
1339 int visible_p = 0;
1340 struct buffer *old_buffer = NULL;
1341
1342 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1343 return visible_p;
1344
1345 if (XBUFFER (w->contents) != current_buffer)
1346 {
1347 old_buffer = current_buffer;
1348 set_buffer_internal_1 (XBUFFER (w->contents));
1349 }
1350
1351 SET_TEXT_POS_FROM_MARKER (top, w->start);
1352 /* Scrolling a minibuffer window via scroll bar when the echo area
1353 shows long text sometimes resets the minibuffer contents behind
1354 our backs. */
1355 if (CHARPOS (top) > ZV)
1356 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1357
1358 /* Compute exact mode line heights. */
1359 if (WINDOW_WANTS_MODELINE_P (w))
1360 current_mode_line_height
1361 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1362 BVAR (current_buffer, mode_line_format));
1363
1364 if (WINDOW_WANTS_HEADER_LINE_P (w))
1365 current_header_line_height
1366 = display_mode_line (w, HEADER_LINE_FACE_ID,
1367 BVAR (current_buffer, header_line_format));
1368
1369 start_display (&it, w, top);
1370 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1371 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1372
1373 if (charpos >= 0
1374 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1375 && IT_CHARPOS (it) >= charpos)
1376 /* When scanning backwards under bidi iteration, move_it_to
1377 stops at or _before_ CHARPOS, because it stops at or to
1378 the _right_ of the character at CHARPOS. */
1379 || (it.bidi_p && it.bidi_it.scan_dir == -1
1380 && IT_CHARPOS (it) <= charpos)))
1381 {
1382 /* We have reached CHARPOS, or passed it. How the call to
1383 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1384 or covered by a display property, move_it_to stops at the end
1385 of the invisible text, to the right of CHARPOS. (ii) If
1386 CHARPOS is in a display vector, move_it_to stops on its last
1387 glyph. */
1388 int top_x = it.current_x;
1389 int top_y = it.current_y;
1390 /* Calling line_bottom_y may change it.method, it.position, etc. */
1391 enum it_method it_method = it.method;
1392 int bottom_y = (last_height = 0, line_bottom_y (&it));
1393 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1394
1395 if (top_y < window_top_y)
1396 visible_p = bottom_y > window_top_y;
1397 else if (top_y < it.last_visible_y)
1398 visible_p = 1;
1399 if (bottom_y >= it.last_visible_y
1400 && it.bidi_p && it.bidi_it.scan_dir == -1
1401 && IT_CHARPOS (it) < charpos)
1402 {
1403 /* When the last line of the window is scanned backwards
1404 under bidi iteration, we could be duped into thinking
1405 that we have passed CHARPOS, when in fact move_it_to
1406 simply stopped short of CHARPOS because it reached
1407 last_visible_y. To see if that's what happened, we call
1408 move_it_to again with a slightly larger vertical limit,
1409 and see if it actually moved vertically; if it did, we
1410 didn't really reach CHARPOS, which is beyond window end. */
1411 struct it save_it = it;
1412 /* Why 10? because we don't know how many canonical lines
1413 will the height of the next line(s) be. So we guess. */
1414 int ten_more_lines = 10 * default_line_pixel_height (w);
1415
1416 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1417 MOVE_TO_POS | MOVE_TO_Y);
1418 if (it.current_y > top_y)
1419 visible_p = 0;
1420
1421 it = save_it;
1422 }
1423 if (visible_p)
1424 {
1425 if (it_method == GET_FROM_DISPLAY_VECTOR)
1426 {
1427 /* We stopped on the last glyph of a display vector.
1428 Try and recompute. Hack alert! */
1429 if (charpos < 2 || top.charpos >= charpos)
1430 top_x = it.glyph_row->x;
1431 else
1432 {
1433 struct it it2, it2_prev;
1434 /* The idea is to get to the previous buffer
1435 position, consume the character there, and use
1436 the pixel coordinates we get after that. But if
1437 the previous buffer position is also displayed
1438 from a display vector, we need to consume all of
1439 the glyphs from that display vector. */
1440 start_display (&it2, w, top);
1441 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1442 /* If we didn't get to CHARPOS - 1, there's some
1443 replacing display property at that position, and
1444 we stopped after it. That is exactly the place
1445 whose coordinates we want. */
1446 if (IT_CHARPOS (it2) != charpos - 1)
1447 it2_prev = it2;
1448 else
1449 {
1450 /* Iterate until we get out of the display
1451 vector that displays the character at
1452 CHARPOS - 1. */
1453 do {
1454 get_next_display_element (&it2);
1455 PRODUCE_GLYPHS (&it2);
1456 it2_prev = it2;
1457 set_iterator_to_next (&it2, 1);
1458 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1459 && IT_CHARPOS (it2) < charpos);
1460 }
1461 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1462 || it2_prev.current_x > it2_prev.last_visible_x)
1463 top_x = it.glyph_row->x;
1464 else
1465 {
1466 top_x = it2_prev.current_x;
1467 top_y = it2_prev.current_y;
1468 }
1469 }
1470 }
1471 else if (IT_CHARPOS (it) != charpos)
1472 {
1473 Lisp_Object cpos = make_number (charpos);
1474 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1475 Lisp_Object string = string_from_display_spec (spec);
1476 struct text_pos tpos;
1477 int replacing_spec_p;
1478 bool newline_in_string
1479 = (STRINGP (string)
1480 && memchr (SDATA (string), '\n', SBYTES (string)));
1481
1482 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1483 replacing_spec_p
1484 = (!NILP (spec)
1485 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1486 charpos, FRAME_WINDOW_P (it.f)));
1487 /* The tricky code below is needed because there's a
1488 discrepancy between move_it_to and how we set cursor
1489 when PT is at the beginning of a portion of text
1490 covered by a display property or an overlay with a
1491 display property, or the display line ends in a
1492 newline from a display string. move_it_to will stop
1493 _after_ such display strings, whereas
1494 set_cursor_from_row conspires with cursor_row_p to
1495 place the cursor on the first glyph produced from the
1496 display string. */
1497
1498 /* We have overshoot PT because it is covered by a
1499 display property that replaces the text it covers.
1500 If the string includes embedded newlines, we are also
1501 in the wrong display line. Backtrack to the correct
1502 line, where the display property begins. */
1503 if (replacing_spec_p)
1504 {
1505 Lisp_Object startpos, endpos;
1506 EMACS_INT start, end;
1507 struct it it3;
1508 int it3_moved;
1509
1510 /* Find the first and the last buffer positions
1511 covered by the display string. */
1512 endpos =
1513 Fnext_single_char_property_change (cpos, Qdisplay,
1514 Qnil, Qnil);
1515 startpos =
1516 Fprevious_single_char_property_change (endpos, Qdisplay,
1517 Qnil, Qnil);
1518 start = XFASTINT (startpos);
1519 end = XFASTINT (endpos);
1520 /* Move to the last buffer position before the
1521 display property. */
1522 start_display (&it3, w, top);
1523 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1524 /* Move forward one more line if the position before
1525 the display string is a newline or if it is the
1526 rightmost character on a line that is
1527 continued or word-wrapped. */
1528 if (it3.method == GET_FROM_BUFFER
1529 && (it3.c == '\n'
1530 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1531 move_it_by_lines (&it3, 1);
1532 else if (move_it_in_display_line_to (&it3, -1,
1533 it3.current_x
1534 + it3.pixel_width,
1535 MOVE_TO_X)
1536 == MOVE_LINE_CONTINUED)
1537 {
1538 move_it_by_lines (&it3, 1);
1539 /* When we are under word-wrap, the #$@%!
1540 move_it_by_lines moves 2 lines, so we need to
1541 fix that up. */
1542 if (it3.line_wrap == WORD_WRAP)
1543 move_it_by_lines (&it3, -1);
1544 }
1545
1546 /* Record the vertical coordinate of the display
1547 line where we wound up. */
1548 top_y = it3.current_y;
1549 if (it3.bidi_p)
1550 {
1551 /* When characters are reordered for display,
1552 the character displayed to the left of the
1553 display string could be _after_ the display
1554 property in the logical order. Use the
1555 smallest vertical position of these two. */
1556 start_display (&it3, w, top);
1557 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1558 if (it3.current_y < top_y)
1559 top_y = it3.current_y;
1560 }
1561 /* Move from the top of the window to the beginning
1562 of the display line where the display string
1563 begins. */
1564 start_display (&it3, w, top);
1565 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1566 /* If it3_moved stays zero after the 'while' loop
1567 below, that means we already were at a newline
1568 before the loop (e.g., the display string begins
1569 with a newline), so we don't need to (and cannot)
1570 inspect the glyphs of it3.glyph_row, because
1571 PRODUCE_GLYPHS will not produce anything for a
1572 newline, and thus it3.glyph_row stays at its
1573 stale content it got at top of the window. */
1574 it3_moved = 0;
1575 /* Finally, advance the iterator until we hit the
1576 first display element whose character position is
1577 CHARPOS, or until the first newline from the
1578 display string, which signals the end of the
1579 display line. */
1580 while (get_next_display_element (&it3))
1581 {
1582 PRODUCE_GLYPHS (&it3);
1583 if (IT_CHARPOS (it3) == charpos
1584 || ITERATOR_AT_END_OF_LINE_P (&it3))
1585 break;
1586 it3_moved = 1;
1587 set_iterator_to_next (&it3, 0);
1588 }
1589 top_x = it3.current_x - it3.pixel_width;
1590 /* Normally, we would exit the above loop because we
1591 found the display element whose character
1592 position is CHARPOS. For the contingency that we
1593 didn't, and stopped at the first newline from the
1594 display string, move back over the glyphs
1595 produced from the string, until we find the
1596 rightmost glyph not from the string. */
1597 if (it3_moved
1598 && newline_in_string
1599 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1600 {
1601 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1602 + it3.glyph_row->used[TEXT_AREA];
1603
1604 while (EQ ((g - 1)->object, string))
1605 {
1606 --g;
1607 top_x -= g->pixel_width;
1608 }
1609 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1610 + it3.glyph_row->used[TEXT_AREA]);
1611 }
1612 }
1613 }
1614
1615 *x = top_x;
1616 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1617 *rtop = max (0, window_top_y - top_y);
1618 *rbot = max (0, bottom_y - it.last_visible_y);
1619 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1620 - max (top_y, window_top_y)));
1621 *vpos = it.vpos;
1622 }
1623 }
1624 else
1625 {
1626 /* We were asked to provide info about WINDOW_END. */
1627 struct it it2;
1628 void *it2data = NULL;
1629
1630 SAVE_IT (it2, it, it2data);
1631 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1632 move_it_by_lines (&it, 1);
1633 if (charpos < IT_CHARPOS (it)
1634 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1635 {
1636 visible_p = 1;
1637 RESTORE_IT (&it2, &it2, it2data);
1638 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1639 *x = it2.current_x;
1640 *y = it2.current_y + it2.max_ascent - it2.ascent;
1641 *rtop = max (0, -it2.current_y);
1642 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1643 - it.last_visible_y));
1644 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1645 it.last_visible_y)
1646 - max (it2.current_y,
1647 WINDOW_HEADER_LINE_HEIGHT (w))));
1648 *vpos = it2.vpos;
1649 }
1650 else
1651 bidi_unshelve_cache (it2data, 1);
1652 }
1653 bidi_unshelve_cache (itdata, 0);
1654
1655 if (old_buffer)
1656 set_buffer_internal_1 (old_buffer);
1657
1658 current_header_line_height = current_mode_line_height = -1;
1659
1660 if (visible_p && w->hscroll > 0)
1661 *x -=
1662 window_hscroll_limited (w, WINDOW_XFRAME (w))
1663 * WINDOW_FRAME_COLUMN_WIDTH (w);
1664
1665 #if 0
1666 /* Debugging code. */
1667 if (visible_p)
1668 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1669 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1670 else
1671 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1672 #endif
1673
1674 return visible_p;
1675 }
1676
1677
1678 /* Return the next character from STR. Return in *LEN the length of
1679 the character. This is like STRING_CHAR_AND_LENGTH but never
1680 returns an invalid character. If we find one, we return a `?', but
1681 with the length of the invalid character. */
1682
1683 static int
1684 string_char_and_length (const unsigned char *str, int *len)
1685 {
1686 int c;
1687
1688 c = STRING_CHAR_AND_LENGTH (str, *len);
1689 if (!CHAR_VALID_P (c))
1690 /* We may not change the length here because other places in Emacs
1691 don't use this function, i.e. they silently accept invalid
1692 characters. */
1693 c = '?';
1694
1695 return c;
1696 }
1697
1698
1699
1700 /* Given a position POS containing a valid character and byte position
1701 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1702
1703 static struct text_pos
1704 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1705 {
1706 eassert (STRINGP (string) && nchars >= 0);
1707
1708 if (STRING_MULTIBYTE (string))
1709 {
1710 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1711 int len;
1712
1713 while (nchars--)
1714 {
1715 string_char_and_length (p, &len);
1716 p += len;
1717 CHARPOS (pos) += 1;
1718 BYTEPOS (pos) += len;
1719 }
1720 }
1721 else
1722 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1723
1724 return pos;
1725 }
1726
1727
1728 /* Value is the text position, i.e. character and byte position,
1729 for character position CHARPOS in STRING. */
1730
1731 static struct text_pos
1732 string_pos (ptrdiff_t charpos, Lisp_Object string)
1733 {
1734 struct text_pos pos;
1735 eassert (STRINGP (string));
1736 eassert (charpos >= 0);
1737 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1738 return pos;
1739 }
1740
1741
1742 /* Value is a text position, i.e. character and byte position, for
1743 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1744 means recognize multibyte characters. */
1745
1746 static struct text_pos
1747 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1748 {
1749 struct text_pos pos;
1750
1751 eassert (s != NULL);
1752 eassert (charpos >= 0);
1753
1754 if (multibyte_p)
1755 {
1756 int len;
1757
1758 SET_TEXT_POS (pos, 0, 0);
1759 while (charpos--)
1760 {
1761 string_char_and_length ((const unsigned char *) s, &len);
1762 s += len;
1763 CHARPOS (pos) += 1;
1764 BYTEPOS (pos) += len;
1765 }
1766 }
1767 else
1768 SET_TEXT_POS (pos, charpos, charpos);
1769
1770 return pos;
1771 }
1772
1773
1774 /* Value is the number of characters in C string S. MULTIBYTE_P
1775 non-zero means recognize multibyte characters. */
1776
1777 static ptrdiff_t
1778 number_of_chars (const char *s, bool multibyte_p)
1779 {
1780 ptrdiff_t nchars;
1781
1782 if (multibyte_p)
1783 {
1784 ptrdiff_t rest = strlen (s);
1785 int len;
1786 const unsigned char *p = (const unsigned char *) s;
1787
1788 for (nchars = 0; rest > 0; ++nchars)
1789 {
1790 string_char_and_length (p, &len);
1791 rest -= len, p += len;
1792 }
1793 }
1794 else
1795 nchars = strlen (s);
1796
1797 return nchars;
1798 }
1799
1800
1801 /* Compute byte position NEWPOS->bytepos corresponding to
1802 NEWPOS->charpos. POS is a known position in string STRING.
1803 NEWPOS->charpos must be >= POS.charpos. */
1804
1805 static void
1806 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1807 {
1808 eassert (STRINGP (string));
1809 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1810
1811 if (STRING_MULTIBYTE (string))
1812 *newpos = string_pos_nchars_ahead (pos, string,
1813 CHARPOS (*newpos) - CHARPOS (pos));
1814 else
1815 BYTEPOS (*newpos) = CHARPOS (*newpos);
1816 }
1817
1818 /* EXPORT:
1819 Return an estimation of the pixel height of mode or header lines on
1820 frame F. FACE_ID specifies what line's height to estimate. */
1821
1822 int
1823 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1824 {
1825 #ifdef HAVE_WINDOW_SYSTEM
1826 if (FRAME_WINDOW_P (f))
1827 {
1828 int height = FONT_HEIGHT (FRAME_FONT (f));
1829
1830 /* This function is called so early when Emacs starts that the face
1831 cache and mode line face are not yet initialized. */
1832 if (FRAME_FACE_CACHE (f))
1833 {
1834 struct face *face = FACE_FROM_ID (f, face_id);
1835 if (face)
1836 {
1837 if (face->font)
1838 height = FONT_HEIGHT (face->font);
1839 if (face->box_line_width > 0)
1840 height += 2 * face->box_line_width;
1841 }
1842 }
1843
1844 return height;
1845 }
1846 #endif
1847
1848 return 1;
1849 }
1850
1851 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1852 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1853 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1854 not force the value into range. */
1855
1856 void
1857 pixel_to_glyph_coords (struct frame *f, register int pix_x, register int pix_y,
1858 int *x, int *y, NativeRectangle *bounds, int noclip)
1859 {
1860
1861 #ifdef HAVE_WINDOW_SYSTEM
1862 if (FRAME_WINDOW_P (f))
1863 {
1864 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1865 even for negative values. */
1866 if (pix_x < 0)
1867 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1868 if (pix_y < 0)
1869 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1870
1871 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1872 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1873
1874 if (bounds)
1875 STORE_NATIVE_RECT (*bounds,
1876 FRAME_COL_TO_PIXEL_X (f, pix_x),
1877 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1878 FRAME_COLUMN_WIDTH (f) - 1,
1879 FRAME_LINE_HEIGHT (f) - 1);
1880
1881 if (!noclip)
1882 {
1883 if (pix_x < 0)
1884 pix_x = 0;
1885 else if (pix_x > FRAME_TOTAL_COLS (f))
1886 pix_x = FRAME_TOTAL_COLS (f);
1887
1888 if (pix_y < 0)
1889 pix_y = 0;
1890 else if (pix_y > FRAME_LINES (f))
1891 pix_y = FRAME_LINES (f);
1892 }
1893 }
1894 #endif
1895
1896 *x = pix_x;
1897 *y = pix_y;
1898 }
1899
1900
1901 /* Find the glyph under window-relative coordinates X/Y in window W.
1902 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1903 strings. Return in *HPOS and *VPOS the row and column number of
1904 the glyph found. Return in *AREA the glyph area containing X.
1905 Value is a pointer to the glyph found or null if X/Y is not on
1906 text, or we can't tell because W's current matrix is not up to
1907 date. */
1908
1909 static
1910 struct glyph *
1911 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1912 int *dx, int *dy, int *area)
1913 {
1914 struct glyph *glyph, *end;
1915 struct glyph_row *row = NULL;
1916 int x0, i;
1917
1918 /* Find row containing Y. Give up if some row is not enabled. */
1919 for (i = 0; i < w->current_matrix->nrows; ++i)
1920 {
1921 row = MATRIX_ROW (w->current_matrix, i);
1922 if (!row->enabled_p)
1923 return NULL;
1924 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1925 break;
1926 }
1927
1928 *vpos = i;
1929 *hpos = 0;
1930
1931 /* Give up if Y is not in the window. */
1932 if (i == w->current_matrix->nrows)
1933 return NULL;
1934
1935 /* Get the glyph area containing X. */
1936 if (w->pseudo_window_p)
1937 {
1938 *area = TEXT_AREA;
1939 x0 = 0;
1940 }
1941 else
1942 {
1943 if (x < window_box_left_offset (w, TEXT_AREA))
1944 {
1945 *area = LEFT_MARGIN_AREA;
1946 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1947 }
1948 else if (x < window_box_right_offset (w, TEXT_AREA))
1949 {
1950 *area = TEXT_AREA;
1951 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1952 }
1953 else
1954 {
1955 *area = RIGHT_MARGIN_AREA;
1956 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1957 }
1958 }
1959
1960 /* Find glyph containing X. */
1961 glyph = row->glyphs[*area];
1962 end = glyph + row->used[*area];
1963 x -= x0;
1964 while (glyph < end && x >= glyph->pixel_width)
1965 {
1966 x -= glyph->pixel_width;
1967 ++glyph;
1968 }
1969
1970 if (glyph == end)
1971 return NULL;
1972
1973 if (dx)
1974 {
1975 *dx = x;
1976 *dy = y - (row->y + row->ascent - glyph->ascent);
1977 }
1978
1979 *hpos = glyph - row->glyphs[*area];
1980 return glyph;
1981 }
1982
1983 /* Convert frame-relative x/y to coordinates relative to window W.
1984 Takes pseudo-windows into account. */
1985
1986 static void
1987 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1988 {
1989 if (w->pseudo_window_p)
1990 {
1991 /* A pseudo-window is always full-width, and starts at the
1992 left edge of the frame, plus a frame border. */
1993 struct frame *f = XFRAME (w->frame);
1994 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1995 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1996 }
1997 else
1998 {
1999 *x -= WINDOW_LEFT_EDGE_X (w);
2000 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
2001 }
2002 }
2003
2004 #ifdef HAVE_WINDOW_SYSTEM
2005
2006 /* EXPORT:
2007 Return in RECTS[] at most N clipping rectangles for glyph string S.
2008 Return the number of stored rectangles. */
2009
2010 int
2011 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
2012 {
2013 XRectangle r;
2014
2015 if (n <= 0)
2016 return 0;
2017
2018 if (s->row->full_width_p)
2019 {
2020 /* Draw full-width. X coordinates are relative to S->w->left_col. */
2021 r.x = WINDOW_LEFT_EDGE_X (s->w);
2022 r.width = WINDOW_TOTAL_WIDTH (s->w);
2023
2024 /* Unless displaying a mode or menu bar line, which are always
2025 fully visible, clip to the visible part of the row. */
2026 if (s->w->pseudo_window_p)
2027 r.height = s->row->visible_height;
2028 else
2029 r.height = s->height;
2030 }
2031 else
2032 {
2033 /* This is a text line that may be partially visible. */
2034 r.x = window_box_left (s->w, s->area);
2035 r.width = window_box_width (s->w, s->area);
2036 r.height = s->row->visible_height;
2037 }
2038
2039 if (s->clip_head)
2040 if (r.x < s->clip_head->x)
2041 {
2042 if (r.width >= s->clip_head->x - r.x)
2043 r.width -= s->clip_head->x - r.x;
2044 else
2045 r.width = 0;
2046 r.x = s->clip_head->x;
2047 }
2048 if (s->clip_tail)
2049 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2050 {
2051 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2052 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2053 else
2054 r.width = 0;
2055 }
2056
2057 /* If S draws overlapping rows, it's sufficient to use the top and
2058 bottom of the window for clipping because this glyph string
2059 intentionally draws over other lines. */
2060 if (s->for_overlaps)
2061 {
2062 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2063 r.height = window_text_bottom_y (s->w) - r.y;
2064
2065 /* Alas, the above simple strategy does not work for the
2066 environments with anti-aliased text: if the same text is
2067 drawn onto the same place multiple times, it gets thicker.
2068 If the overlap we are processing is for the erased cursor, we
2069 take the intersection with the rectangle of the cursor. */
2070 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2071 {
2072 XRectangle rc, r_save = r;
2073
2074 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2075 rc.y = s->w->phys_cursor.y;
2076 rc.width = s->w->phys_cursor_width;
2077 rc.height = s->w->phys_cursor_height;
2078
2079 x_intersect_rectangles (&r_save, &rc, &r);
2080 }
2081 }
2082 else
2083 {
2084 /* Don't use S->y for clipping because it doesn't take partially
2085 visible lines into account. For example, it can be negative for
2086 partially visible lines at the top of a window. */
2087 if (!s->row->full_width_p
2088 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2089 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2090 else
2091 r.y = max (0, s->row->y);
2092 }
2093
2094 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2095
2096 /* If drawing the cursor, don't let glyph draw outside its
2097 advertised boundaries. Cleartype does this under some circumstances. */
2098 if (s->hl == DRAW_CURSOR)
2099 {
2100 struct glyph *glyph = s->first_glyph;
2101 int height, max_y;
2102
2103 if (s->x > r.x)
2104 {
2105 r.width -= s->x - r.x;
2106 r.x = s->x;
2107 }
2108 r.width = min (r.width, glyph->pixel_width);
2109
2110 /* If r.y is below window bottom, ensure that we still see a cursor. */
2111 height = min (glyph->ascent + glyph->descent,
2112 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2113 max_y = window_text_bottom_y (s->w) - height;
2114 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2115 if (s->ybase - glyph->ascent > max_y)
2116 {
2117 r.y = max_y;
2118 r.height = height;
2119 }
2120 else
2121 {
2122 /* Don't draw cursor glyph taller than our actual glyph. */
2123 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2124 if (height < r.height)
2125 {
2126 max_y = r.y + r.height;
2127 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2128 r.height = min (max_y - r.y, height);
2129 }
2130 }
2131 }
2132
2133 if (s->row->clip)
2134 {
2135 XRectangle r_save = r;
2136
2137 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2138 r.width = 0;
2139 }
2140
2141 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2142 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2143 {
2144 #ifdef CONVERT_FROM_XRECT
2145 CONVERT_FROM_XRECT (r, *rects);
2146 #else
2147 *rects = r;
2148 #endif
2149 return 1;
2150 }
2151 else
2152 {
2153 /* If we are processing overlapping and allowed to return
2154 multiple clipping rectangles, we exclude the row of the glyph
2155 string from the clipping rectangle. This is to avoid drawing
2156 the same text on the environment with anti-aliasing. */
2157 #ifdef CONVERT_FROM_XRECT
2158 XRectangle rs[2];
2159 #else
2160 XRectangle *rs = rects;
2161 #endif
2162 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2163
2164 if (s->for_overlaps & OVERLAPS_PRED)
2165 {
2166 rs[i] = r;
2167 if (r.y + r.height > row_y)
2168 {
2169 if (r.y < row_y)
2170 rs[i].height = row_y - r.y;
2171 else
2172 rs[i].height = 0;
2173 }
2174 i++;
2175 }
2176 if (s->for_overlaps & OVERLAPS_SUCC)
2177 {
2178 rs[i] = r;
2179 if (r.y < row_y + s->row->visible_height)
2180 {
2181 if (r.y + r.height > row_y + s->row->visible_height)
2182 {
2183 rs[i].y = row_y + s->row->visible_height;
2184 rs[i].height = r.y + r.height - rs[i].y;
2185 }
2186 else
2187 rs[i].height = 0;
2188 }
2189 i++;
2190 }
2191
2192 n = i;
2193 #ifdef CONVERT_FROM_XRECT
2194 for (i = 0; i < n; i++)
2195 CONVERT_FROM_XRECT (rs[i], rects[i]);
2196 #endif
2197 return n;
2198 }
2199 }
2200
2201 /* EXPORT:
2202 Return in *NR the clipping rectangle for glyph string S. */
2203
2204 void
2205 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2206 {
2207 get_glyph_string_clip_rects (s, nr, 1);
2208 }
2209
2210
2211 /* EXPORT:
2212 Return the position and height of the phys cursor in window W.
2213 Set w->phys_cursor_width to width of phys cursor.
2214 */
2215
2216 void
2217 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2218 struct glyph *glyph, int *xp, int *yp, int *heightp)
2219 {
2220 struct frame *f = XFRAME (WINDOW_FRAME (w));
2221 int x, y, wd, h, h0, y0;
2222
2223 /* Compute the width of the rectangle to draw. If on a stretch
2224 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2225 rectangle as wide as the glyph, but use a canonical character
2226 width instead. */
2227 wd = glyph->pixel_width - 1;
2228 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2229 wd++; /* Why? */
2230 #endif
2231
2232 x = w->phys_cursor.x;
2233 if (x < 0)
2234 {
2235 wd += x;
2236 x = 0;
2237 }
2238
2239 if (glyph->type == STRETCH_GLYPH
2240 && !x_stretch_cursor_p)
2241 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2242 w->phys_cursor_width = wd;
2243
2244 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2245
2246 /* If y is below window bottom, ensure that we still see a cursor. */
2247 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2248
2249 h = max (h0, glyph->ascent + glyph->descent);
2250 h0 = min (h0, glyph->ascent + glyph->descent);
2251
2252 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2253 if (y < y0)
2254 {
2255 h = max (h - (y0 - y) + 1, h0);
2256 y = y0 - 1;
2257 }
2258 else
2259 {
2260 y0 = window_text_bottom_y (w) - h0;
2261 if (y > y0)
2262 {
2263 h += y - y0;
2264 y = y0;
2265 }
2266 }
2267
2268 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2269 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2270 *heightp = h;
2271 }
2272
2273 /*
2274 * Remember which glyph the mouse is over.
2275 */
2276
2277 void
2278 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2279 {
2280 Lisp_Object window;
2281 struct window *w;
2282 struct glyph_row *r, *gr, *end_row;
2283 enum window_part part;
2284 enum glyph_row_area area;
2285 int x, y, width, height;
2286
2287 /* Try to determine frame pixel position and size of the glyph under
2288 frame pixel coordinates X/Y on frame F. */
2289
2290 if (!f->glyphs_initialized_p
2291 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2292 NILP (window)))
2293 {
2294 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2295 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2296 goto virtual_glyph;
2297 }
2298
2299 w = XWINDOW (window);
2300 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2301 height = WINDOW_FRAME_LINE_HEIGHT (w);
2302
2303 x = window_relative_x_coord (w, part, gx);
2304 y = gy - WINDOW_TOP_EDGE_Y (w);
2305
2306 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2307 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2308
2309 if (w->pseudo_window_p)
2310 {
2311 area = TEXT_AREA;
2312 part = ON_MODE_LINE; /* Don't adjust margin. */
2313 goto text_glyph;
2314 }
2315
2316 switch (part)
2317 {
2318 case ON_LEFT_MARGIN:
2319 area = LEFT_MARGIN_AREA;
2320 goto text_glyph;
2321
2322 case ON_RIGHT_MARGIN:
2323 area = RIGHT_MARGIN_AREA;
2324 goto text_glyph;
2325
2326 case ON_HEADER_LINE:
2327 case ON_MODE_LINE:
2328 gr = (part == ON_HEADER_LINE
2329 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2330 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2331 gy = gr->y;
2332 area = TEXT_AREA;
2333 goto text_glyph_row_found;
2334
2335 case ON_TEXT:
2336 area = TEXT_AREA;
2337
2338 text_glyph:
2339 gr = 0; gy = 0;
2340 for (; r <= end_row && r->enabled_p; ++r)
2341 if (r->y + r->height > y)
2342 {
2343 gr = r; gy = r->y;
2344 break;
2345 }
2346
2347 text_glyph_row_found:
2348 if (gr && gy <= y)
2349 {
2350 struct glyph *g = gr->glyphs[area];
2351 struct glyph *end = g + gr->used[area];
2352
2353 height = gr->height;
2354 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2355 if (gx + g->pixel_width > x)
2356 break;
2357
2358 if (g < end)
2359 {
2360 if (g->type == IMAGE_GLYPH)
2361 {
2362 /* Don't remember when mouse is over image, as
2363 image may have hot-spots. */
2364 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2365 return;
2366 }
2367 width = g->pixel_width;
2368 }
2369 else
2370 {
2371 /* Use nominal char spacing at end of line. */
2372 x -= gx;
2373 gx += (x / width) * width;
2374 }
2375
2376 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2377 gx += window_box_left_offset (w, area);
2378 }
2379 else
2380 {
2381 /* Use nominal line height at end of window. */
2382 gx = (x / width) * width;
2383 y -= gy;
2384 gy += (y / height) * height;
2385 }
2386 break;
2387
2388 case ON_LEFT_FRINGE:
2389 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2390 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2391 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2392 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2393 goto row_glyph;
2394
2395 case ON_RIGHT_FRINGE:
2396 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2397 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2398 : window_box_right_offset (w, TEXT_AREA));
2399 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2400 goto row_glyph;
2401
2402 case ON_SCROLL_BAR:
2403 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2404 ? 0
2405 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2406 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2407 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2408 : 0)));
2409 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2410
2411 row_glyph:
2412 gr = 0, gy = 0;
2413 for (; r <= end_row && r->enabled_p; ++r)
2414 if (r->y + r->height > y)
2415 {
2416 gr = r; gy = r->y;
2417 break;
2418 }
2419
2420 if (gr && gy <= y)
2421 height = gr->height;
2422 else
2423 {
2424 /* Use nominal line height at end of window. */
2425 y -= gy;
2426 gy += (y / height) * height;
2427 }
2428 break;
2429
2430 default:
2431 ;
2432 virtual_glyph:
2433 /* If there is no glyph under the mouse, then we divide the screen
2434 into a grid of the smallest glyph in the frame, and use that
2435 as our "glyph". */
2436
2437 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2438 round down even for negative values. */
2439 if (gx < 0)
2440 gx -= width - 1;
2441 if (gy < 0)
2442 gy -= height - 1;
2443
2444 gx = (gx / width) * width;
2445 gy = (gy / height) * height;
2446
2447 goto store_rect;
2448 }
2449
2450 gx += WINDOW_LEFT_EDGE_X (w);
2451 gy += WINDOW_TOP_EDGE_Y (w);
2452
2453 store_rect:
2454 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2455
2456 /* Visible feedback for debugging. */
2457 #if 0
2458 #if HAVE_X_WINDOWS
2459 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2460 f->output_data.x->normal_gc,
2461 gx, gy, width, height);
2462 #endif
2463 #endif
2464 }
2465
2466
2467 #endif /* HAVE_WINDOW_SYSTEM */
2468
2469 \f
2470 /***********************************************************************
2471 Lisp form evaluation
2472 ***********************************************************************/
2473
2474 /* Error handler for safe_eval and safe_call. */
2475
2476 static Lisp_Object
2477 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2478 {
2479 add_to_log ("Error during redisplay: %S signaled %S",
2480 Flist (nargs, args), arg);
2481 return Qnil;
2482 }
2483
2484 /* Call function FUNC with the rest of NARGS - 1 arguments
2485 following. Return the result, or nil if something went
2486 wrong. Prevent redisplay during the evaluation. */
2487
2488 Lisp_Object
2489 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2490 {
2491 Lisp_Object val;
2492
2493 if (inhibit_eval_during_redisplay)
2494 val = Qnil;
2495 else
2496 {
2497 va_list ap;
2498 ptrdiff_t i;
2499 ptrdiff_t count = SPECPDL_INDEX ();
2500 struct gcpro gcpro1;
2501 Lisp_Object *args = alloca (nargs * word_size);
2502
2503 args[0] = func;
2504 va_start (ap, func);
2505 for (i = 1; i < nargs; i++)
2506 args[i] = va_arg (ap, Lisp_Object);
2507 va_end (ap);
2508
2509 GCPRO1 (args[0]);
2510 gcpro1.nvars = nargs;
2511 specbind (Qinhibit_redisplay, Qt);
2512 /* Use Qt to ensure debugger does not run,
2513 so there is no possibility of wanting to redisplay. */
2514 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2515 safe_eval_handler);
2516 UNGCPRO;
2517 val = unbind_to (count, val);
2518 }
2519
2520 return val;
2521 }
2522
2523
2524 /* Call function FN with one argument ARG.
2525 Return the result, or nil if something went wrong. */
2526
2527 Lisp_Object
2528 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2529 {
2530 return safe_call (2, fn, arg);
2531 }
2532
2533 static Lisp_Object Qeval;
2534
2535 Lisp_Object
2536 safe_eval (Lisp_Object sexpr)
2537 {
2538 return safe_call1 (Qeval, sexpr);
2539 }
2540
2541 /* Call function FN with two arguments ARG1 and ARG2.
2542 Return the result, or nil if something went wrong. */
2543
2544 Lisp_Object
2545 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2546 {
2547 return safe_call (3, fn, arg1, arg2);
2548 }
2549
2550
2551 \f
2552 /***********************************************************************
2553 Debugging
2554 ***********************************************************************/
2555
2556 #if 0
2557
2558 /* Define CHECK_IT to perform sanity checks on iterators.
2559 This is for debugging. It is too slow to do unconditionally. */
2560
2561 static void
2562 check_it (struct it *it)
2563 {
2564 if (it->method == GET_FROM_STRING)
2565 {
2566 eassert (STRINGP (it->string));
2567 eassert (IT_STRING_CHARPOS (*it) >= 0);
2568 }
2569 else
2570 {
2571 eassert (IT_STRING_CHARPOS (*it) < 0);
2572 if (it->method == GET_FROM_BUFFER)
2573 {
2574 /* Check that character and byte positions agree. */
2575 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2576 }
2577 }
2578
2579 if (it->dpvec)
2580 eassert (it->current.dpvec_index >= 0);
2581 else
2582 eassert (it->current.dpvec_index < 0);
2583 }
2584
2585 #define CHECK_IT(IT) check_it ((IT))
2586
2587 #else /* not 0 */
2588
2589 #define CHECK_IT(IT) (void) 0
2590
2591 #endif /* not 0 */
2592
2593
2594 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2595
2596 /* Check that the window end of window W is what we expect it
2597 to be---the last row in the current matrix displaying text. */
2598
2599 static void
2600 check_window_end (struct window *w)
2601 {
2602 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2603 {
2604 struct glyph_row *row;
2605 eassert ((row = MATRIX_ROW (w->current_matrix,
2606 XFASTINT (w->window_end_vpos)),
2607 !row->enabled_p
2608 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2609 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2610 }
2611 }
2612
2613 #define CHECK_WINDOW_END(W) check_window_end ((W))
2614
2615 #else
2616
2617 #define CHECK_WINDOW_END(W) (void) 0
2618
2619 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2620
2621 /* Return mark position if current buffer has the region of non-zero length,
2622 or -1 otherwise. */
2623
2624 static ptrdiff_t
2625 markpos_of_region (void)
2626 {
2627 if (!NILP (Vtransient_mark_mode)
2628 && !NILP (BVAR (current_buffer, mark_active))
2629 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2630 {
2631 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2632
2633 if (markpos != PT)
2634 return markpos;
2635 }
2636 return -1;
2637 }
2638
2639 /***********************************************************************
2640 Iterator initialization
2641 ***********************************************************************/
2642
2643 /* Initialize IT for displaying current_buffer in window W, starting
2644 at character position CHARPOS. CHARPOS < 0 means that no buffer
2645 position is specified which is useful when the iterator is assigned
2646 a position later. BYTEPOS is the byte position corresponding to
2647 CHARPOS.
2648
2649 If ROW is not null, calls to produce_glyphs with IT as parameter
2650 will produce glyphs in that row.
2651
2652 BASE_FACE_ID is the id of a base face to use. It must be one of
2653 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2654 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2655 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2656
2657 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2658 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2659 will be initialized to use the corresponding mode line glyph row of
2660 the desired matrix of W. */
2661
2662 void
2663 init_iterator (struct it *it, struct window *w,
2664 ptrdiff_t charpos, ptrdiff_t bytepos,
2665 struct glyph_row *row, enum face_id base_face_id)
2666 {
2667 ptrdiff_t markpos;
2668 enum face_id remapped_base_face_id = base_face_id;
2669
2670 /* Some precondition checks. */
2671 eassert (w != NULL && it != NULL);
2672 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2673 && charpos <= ZV));
2674
2675 /* If face attributes have been changed since the last redisplay,
2676 free realized faces now because they depend on face definitions
2677 that might have changed. Don't free faces while there might be
2678 desired matrices pending which reference these faces. */
2679 if (face_change_count && !inhibit_free_realized_faces)
2680 {
2681 face_change_count = 0;
2682 free_all_realized_faces (Qnil);
2683 }
2684
2685 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2686 if (! NILP (Vface_remapping_alist))
2687 remapped_base_face_id
2688 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2689
2690 /* Use one of the mode line rows of W's desired matrix if
2691 appropriate. */
2692 if (row == NULL)
2693 {
2694 if (base_face_id == MODE_LINE_FACE_ID
2695 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2696 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2697 else if (base_face_id == HEADER_LINE_FACE_ID)
2698 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2699 }
2700
2701 /* Clear IT. */
2702 memset (it, 0, sizeof *it);
2703 it->current.overlay_string_index = -1;
2704 it->current.dpvec_index = -1;
2705 it->base_face_id = remapped_base_face_id;
2706 it->string = Qnil;
2707 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2708 it->paragraph_embedding = L2R;
2709 it->bidi_it.string.lstring = Qnil;
2710 it->bidi_it.string.s = NULL;
2711 it->bidi_it.string.bufpos = 0;
2712 it->bidi_it.w = w;
2713
2714 /* The window in which we iterate over current_buffer: */
2715 XSETWINDOW (it->window, w);
2716 it->w = w;
2717 it->f = XFRAME (w->frame);
2718
2719 it->cmp_it.id = -1;
2720
2721 /* Extra space between lines (on window systems only). */
2722 if (base_face_id == DEFAULT_FACE_ID
2723 && FRAME_WINDOW_P (it->f))
2724 {
2725 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2726 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2727 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2728 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2729 * FRAME_LINE_HEIGHT (it->f));
2730 else if (it->f->extra_line_spacing > 0)
2731 it->extra_line_spacing = it->f->extra_line_spacing;
2732 it->max_extra_line_spacing = 0;
2733 }
2734
2735 /* If realized faces have been removed, e.g. because of face
2736 attribute changes of named faces, recompute them. When running
2737 in batch mode, the face cache of the initial frame is null. If
2738 we happen to get called, make a dummy face cache. */
2739 if (FRAME_FACE_CACHE (it->f) == NULL)
2740 init_frame_faces (it->f);
2741 if (FRAME_FACE_CACHE (it->f)->used == 0)
2742 recompute_basic_faces (it->f);
2743
2744 /* Current value of the `slice', `space-width', and 'height' properties. */
2745 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2746 it->space_width = Qnil;
2747 it->font_height = Qnil;
2748 it->override_ascent = -1;
2749
2750 /* Are control characters displayed as `^C'? */
2751 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2752
2753 /* -1 means everything between a CR and the following line end
2754 is invisible. >0 means lines indented more than this value are
2755 invisible. */
2756 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2757 ? (clip_to_bounds
2758 (-1, XINT (BVAR (current_buffer, selective_display)),
2759 PTRDIFF_MAX))
2760 : (!NILP (BVAR (current_buffer, selective_display))
2761 ? -1 : 0));
2762 it->selective_display_ellipsis_p
2763 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2764
2765 /* Display table to use. */
2766 it->dp = window_display_table (w);
2767
2768 /* Are multibyte characters enabled in current_buffer? */
2769 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2770
2771 /* If visible region is of non-zero length, set IT->region_beg_charpos
2772 and IT->region_end_charpos to the start and end of a visible region
2773 in window IT->w. Set both to -1 to indicate no region. */
2774 markpos = markpos_of_region ();
2775 if (markpos >= 0
2776 /* Maybe highlight only in selected window. */
2777 && (/* Either show region everywhere. */
2778 highlight_nonselected_windows
2779 /* Or show region in the selected window. */
2780 || w == XWINDOW (selected_window)
2781 /* Or show the region if we are in the mini-buffer and W is
2782 the window the mini-buffer refers to. */
2783 || (MINI_WINDOW_P (XWINDOW (selected_window))
2784 && WINDOWP (minibuf_selected_window)
2785 && w == XWINDOW (minibuf_selected_window))))
2786 {
2787 it->region_beg_charpos = min (PT, markpos);
2788 it->region_end_charpos = max (PT, markpos);
2789 }
2790 else
2791 it->region_beg_charpos = it->region_end_charpos = -1;
2792
2793 /* Get the position at which the redisplay_end_trigger hook should
2794 be run, if it is to be run at all. */
2795 if (MARKERP (w->redisplay_end_trigger)
2796 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2797 it->redisplay_end_trigger_charpos
2798 = marker_position (w->redisplay_end_trigger);
2799 else if (INTEGERP (w->redisplay_end_trigger))
2800 it->redisplay_end_trigger_charpos =
2801 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2802
2803 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2804
2805 /* Are lines in the display truncated? */
2806 if (base_face_id != DEFAULT_FACE_ID
2807 || it->w->hscroll
2808 || (! WINDOW_FULL_WIDTH_P (it->w)
2809 && ((!NILP (Vtruncate_partial_width_windows)
2810 && !INTEGERP (Vtruncate_partial_width_windows))
2811 || (INTEGERP (Vtruncate_partial_width_windows)
2812 && (WINDOW_TOTAL_COLS (it->w)
2813 < XINT (Vtruncate_partial_width_windows))))))
2814 it->line_wrap = TRUNCATE;
2815 else if (NILP (BVAR (current_buffer, truncate_lines)))
2816 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2817 ? WINDOW_WRAP : WORD_WRAP;
2818 else
2819 it->line_wrap = TRUNCATE;
2820
2821 /* Get dimensions of truncation and continuation glyphs. These are
2822 displayed as fringe bitmaps under X, but we need them for such
2823 frames when the fringes are turned off. But leave the dimensions
2824 zero for tooltip frames, as these glyphs look ugly there and also
2825 sabotage calculations of tooltip dimensions in x-show-tip. */
2826 #ifdef HAVE_WINDOW_SYSTEM
2827 if (!(FRAME_WINDOW_P (it->f)
2828 && FRAMEP (tip_frame)
2829 && it->f == XFRAME (tip_frame)))
2830 #endif
2831 {
2832 if (it->line_wrap == TRUNCATE)
2833 {
2834 /* We will need the truncation glyph. */
2835 eassert (it->glyph_row == NULL);
2836 produce_special_glyphs (it, IT_TRUNCATION);
2837 it->truncation_pixel_width = it->pixel_width;
2838 }
2839 else
2840 {
2841 /* We will need the continuation glyph. */
2842 eassert (it->glyph_row == NULL);
2843 produce_special_glyphs (it, IT_CONTINUATION);
2844 it->continuation_pixel_width = it->pixel_width;
2845 }
2846 }
2847
2848 /* Reset these values to zero because the produce_special_glyphs
2849 above has changed them. */
2850 it->pixel_width = it->ascent = it->descent = 0;
2851 it->phys_ascent = it->phys_descent = 0;
2852
2853 /* Set this after getting the dimensions of truncation and
2854 continuation glyphs, so that we don't produce glyphs when calling
2855 produce_special_glyphs, above. */
2856 it->glyph_row = row;
2857 it->area = TEXT_AREA;
2858
2859 /* Forget any previous info about this row being reversed. */
2860 if (it->glyph_row)
2861 it->glyph_row->reversed_p = 0;
2862
2863 /* Get the dimensions of the display area. The display area
2864 consists of the visible window area plus a horizontally scrolled
2865 part to the left of the window. All x-values are relative to the
2866 start of this total display area. */
2867 if (base_face_id != DEFAULT_FACE_ID)
2868 {
2869 /* Mode lines, menu bar in terminal frames. */
2870 it->first_visible_x = 0;
2871 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2872 }
2873 else
2874 {
2875 it->first_visible_x =
2876 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2877 it->last_visible_x = (it->first_visible_x
2878 + window_box_width (w, TEXT_AREA));
2879
2880 /* If we truncate lines, leave room for the truncation glyph(s) at
2881 the right margin. Otherwise, leave room for the continuation
2882 glyph(s). Done only if the window has no fringes. Since we
2883 don't know at this point whether there will be any R2L lines in
2884 the window, we reserve space for truncation/continuation glyphs
2885 even if only one of the fringes is absent. */
2886 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2887 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2888 {
2889 if (it->line_wrap == TRUNCATE)
2890 it->last_visible_x -= it->truncation_pixel_width;
2891 else
2892 it->last_visible_x -= it->continuation_pixel_width;
2893 }
2894
2895 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2896 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2897 }
2898
2899 /* Leave room for a border glyph. */
2900 if (!FRAME_WINDOW_P (it->f)
2901 && !WINDOW_RIGHTMOST_P (it->w))
2902 it->last_visible_x -= 1;
2903
2904 it->last_visible_y = window_text_bottom_y (w);
2905
2906 /* For mode lines and alike, arrange for the first glyph having a
2907 left box line if the face specifies a box. */
2908 if (base_face_id != DEFAULT_FACE_ID)
2909 {
2910 struct face *face;
2911
2912 it->face_id = remapped_base_face_id;
2913
2914 /* If we have a boxed mode line, make the first character appear
2915 with a left box line. */
2916 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2917 if (face->box != FACE_NO_BOX)
2918 it->start_of_box_run_p = 1;
2919 }
2920
2921 /* If a buffer position was specified, set the iterator there,
2922 getting overlays and face properties from that position. */
2923 if (charpos >= BUF_BEG (current_buffer))
2924 {
2925 it->end_charpos = ZV;
2926 eassert (charpos == BYTE_TO_CHAR (bytepos));
2927 IT_CHARPOS (*it) = charpos;
2928 IT_BYTEPOS (*it) = bytepos;
2929
2930 /* We will rely on `reseat' to set this up properly, via
2931 handle_face_prop. */
2932 it->face_id = it->base_face_id;
2933
2934 it->start = it->current;
2935 /* Do we need to reorder bidirectional text? Not if this is a
2936 unibyte buffer: by definition, none of the single-byte
2937 characters are strong R2L, so no reordering is needed. And
2938 bidi.c doesn't support unibyte buffers anyway. Also, don't
2939 reorder while we are loading loadup.el, since the tables of
2940 character properties needed for reordering are not yet
2941 available. */
2942 it->bidi_p =
2943 NILP (Vpurify_flag)
2944 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2945 && it->multibyte_p;
2946
2947 /* If we are to reorder bidirectional text, init the bidi
2948 iterator. */
2949 if (it->bidi_p)
2950 {
2951 /* Note the paragraph direction that this buffer wants to
2952 use. */
2953 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2954 Qleft_to_right))
2955 it->paragraph_embedding = L2R;
2956 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2957 Qright_to_left))
2958 it->paragraph_embedding = R2L;
2959 else
2960 it->paragraph_embedding = NEUTRAL_DIR;
2961 bidi_unshelve_cache (NULL, 0);
2962 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2963 &it->bidi_it);
2964 }
2965
2966 /* Compute faces etc. */
2967 reseat (it, it->current.pos, 1);
2968 }
2969
2970 CHECK_IT (it);
2971 }
2972
2973
2974 /* Initialize IT for the display of window W with window start POS. */
2975
2976 void
2977 start_display (struct it *it, struct window *w, struct text_pos pos)
2978 {
2979 struct glyph_row *row;
2980 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2981
2982 row = w->desired_matrix->rows + first_vpos;
2983 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2984 it->first_vpos = first_vpos;
2985
2986 /* Don't reseat to previous visible line start if current start
2987 position is in a string or image. */
2988 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2989 {
2990 int start_at_line_beg_p;
2991 int first_y = it->current_y;
2992
2993 /* If window start is not at a line start, skip forward to POS to
2994 get the correct continuation lines width. */
2995 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2996 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2997 if (!start_at_line_beg_p)
2998 {
2999 int new_x;
3000
3001 reseat_at_previous_visible_line_start (it);
3002 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
3003
3004 new_x = it->current_x + it->pixel_width;
3005
3006 /* If lines are continued, this line may end in the middle
3007 of a multi-glyph character (e.g. a control character
3008 displayed as \003, or in the middle of an overlay
3009 string). In this case move_it_to above will not have
3010 taken us to the start of the continuation line but to the
3011 end of the continued line. */
3012 if (it->current_x > 0
3013 && it->line_wrap != TRUNCATE /* Lines are continued. */
3014 && (/* And glyph doesn't fit on the line. */
3015 new_x > it->last_visible_x
3016 /* Or it fits exactly and we're on a window
3017 system frame. */
3018 || (new_x == it->last_visible_x
3019 && FRAME_WINDOW_P (it->f)
3020 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
3021 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
3022 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
3023 {
3024 if ((it->current.dpvec_index >= 0
3025 || it->current.overlay_string_index >= 0)
3026 /* If we are on a newline from a display vector or
3027 overlay string, then we are already at the end of
3028 a screen line; no need to go to the next line in
3029 that case, as this line is not really continued.
3030 (If we do go to the next line, C-e will not DTRT.) */
3031 && it->c != '\n')
3032 {
3033 set_iterator_to_next (it, 1);
3034 move_it_in_display_line_to (it, -1, -1, 0);
3035 }
3036
3037 it->continuation_lines_width += it->current_x;
3038 }
3039 /* If the character at POS is displayed via a display
3040 vector, move_it_to above stops at the final glyph of
3041 IT->dpvec. To make the caller redisplay that character
3042 again (a.k.a. start at POS), we need to reset the
3043 dpvec_index to the beginning of IT->dpvec. */
3044 else if (it->current.dpvec_index >= 0)
3045 it->current.dpvec_index = 0;
3046
3047 /* We're starting a new display line, not affected by the
3048 height of the continued line, so clear the appropriate
3049 fields in the iterator structure. */
3050 it->max_ascent = it->max_descent = 0;
3051 it->max_phys_ascent = it->max_phys_descent = 0;
3052
3053 it->current_y = first_y;
3054 it->vpos = 0;
3055 it->current_x = it->hpos = 0;
3056 }
3057 }
3058 }
3059
3060
3061 /* Return 1 if POS is a position in ellipses displayed for invisible
3062 text. W is the window we display, for text property lookup. */
3063
3064 static int
3065 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3066 {
3067 Lisp_Object prop, window;
3068 int ellipses_p = 0;
3069 ptrdiff_t charpos = CHARPOS (pos->pos);
3070
3071 /* If POS specifies a position in a display vector, this might
3072 be for an ellipsis displayed for invisible text. We won't
3073 get the iterator set up for delivering that ellipsis unless
3074 we make sure that it gets aware of the invisible text. */
3075 if (pos->dpvec_index >= 0
3076 && pos->overlay_string_index < 0
3077 && CHARPOS (pos->string_pos) < 0
3078 && charpos > BEGV
3079 && (XSETWINDOW (window, w),
3080 prop = Fget_char_property (make_number (charpos),
3081 Qinvisible, window),
3082 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3083 {
3084 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3085 window);
3086 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3087 }
3088
3089 return ellipses_p;
3090 }
3091
3092
3093 /* Initialize IT for stepping through current_buffer in window W,
3094 starting at position POS that includes overlay string and display
3095 vector/ control character translation position information. Value
3096 is zero if there are overlay strings with newlines at POS. */
3097
3098 static int
3099 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3100 {
3101 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3102 int i, overlay_strings_with_newlines = 0;
3103
3104 /* If POS specifies a position in a display vector, this might
3105 be for an ellipsis displayed for invisible text. We won't
3106 get the iterator set up for delivering that ellipsis unless
3107 we make sure that it gets aware of the invisible text. */
3108 if (in_ellipses_for_invisible_text_p (pos, w))
3109 {
3110 --charpos;
3111 bytepos = 0;
3112 }
3113
3114 /* Keep in mind: the call to reseat in init_iterator skips invisible
3115 text, so we might end up at a position different from POS. This
3116 is only a problem when POS is a row start after a newline and an
3117 overlay starts there with an after-string, and the overlay has an
3118 invisible property. Since we don't skip invisible text in
3119 display_line and elsewhere immediately after consuming the
3120 newline before the row start, such a POS will not be in a string,
3121 but the call to init_iterator below will move us to the
3122 after-string. */
3123 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3124
3125 /* This only scans the current chunk -- it should scan all chunks.
3126 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3127 to 16 in 22.1 to make this a lesser problem. */
3128 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3129 {
3130 const char *s = SSDATA (it->overlay_strings[i]);
3131 const char *e = s + SBYTES (it->overlay_strings[i]);
3132
3133 while (s < e && *s != '\n')
3134 ++s;
3135
3136 if (s < e)
3137 {
3138 overlay_strings_with_newlines = 1;
3139 break;
3140 }
3141 }
3142
3143 /* If position is within an overlay string, set up IT to the right
3144 overlay string. */
3145 if (pos->overlay_string_index >= 0)
3146 {
3147 int relative_index;
3148
3149 /* If the first overlay string happens to have a `display'
3150 property for an image, the iterator will be set up for that
3151 image, and we have to undo that setup first before we can
3152 correct the overlay string index. */
3153 if (it->method == GET_FROM_IMAGE)
3154 pop_it (it);
3155
3156 /* We already have the first chunk of overlay strings in
3157 IT->overlay_strings. Load more until the one for
3158 pos->overlay_string_index is in IT->overlay_strings. */
3159 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3160 {
3161 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3162 it->current.overlay_string_index = 0;
3163 while (n--)
3164 {
3165 load_overlay_strings (it, 0);
3166 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3167 }
3168 }
3169
3170 it->current.overlay_string_index = pos->overlay_string_index;
3171 relative_index = (it->current.overlay_string_index
3172 % OVERLAY_STRING_CHUNK_SIZE);
3173 it->string = it->overlay_strings[relative_index];
3174 eassert (STRINGP (it->string));
3175 it->current.string_pos = pos->string_pos;
3176 it->method = GET_FROM_STRING;
3177 it->end_charpos = SCHARS (it->string);
3178 /* Set up the bidi iterator for this overlay string. */
3179 if (it->bidi_p)
3180 {
3181 it->bidi_it.string.lstring = it->string;
3182 it->bidi_it.string.s = NULL;
3183 it->bidi_it.string.schars = SCHARS (it->string);
3184 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3185 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3186 it->bidi_it.string.unibyte = !it->multibyte_p;
3187 it->bidi_it.w = it->w;
3188 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3189 FRAME_WINDOW_P (it->f), &it->bidi_it);
3190
3191 /* Synchronize the state of the bidi iterator with
3192 pos->string_pos. For any string position other than
3193 zero, this will be done automagically when we resume
3194 iteration over the string and get_visually_first_element
3195 is called. But if string_pos is zero, and the string is
3196 to be reordered for display, we need to resync manually,
3197 since it could be that the iteration state recorded in
3198 pos ended at string_pos of 0 moving backwards in string. */
3199 if (CHARPOS (pos->string_pos) == 0)
3200 {
3201 get_visually_first_element (it);
3202 if (IT_STRING_CHARPOS (*it) != 0)
3203 do {
3204 /* Paranoia. */
3205 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3206 bidi_move_to_visually_next (&it->bidi_it);
3207 } while (it->bidi_it.charpos != 0);
3208 }
3209 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3210 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3211 }
3212 }
3213
3214 if (CHARPOS (pos->string_pos) >= 0)
3215 {
3216 /* Recorded position is not in an overlay string, but in another
3217 string. This can only be a string from a `display' property.
3218 IT should already be filled with that string. */
3219 it->current.string_pos = pos->string_pos;
3220 eassert (STRINGP (it->string));
3221 if (it->bidi_p)
3222 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3223 FRAME_WINDOW_P (it->f), &it->bidi_it);
3224 }
3225
3226 /* Restore position in display vector translations, control
3227 character translations or ellipses. */
3228 if (pos->dpvec_index >= 0)
3229 {
3230 if (it->dpvec == NULL)
3231 get_next_display_element (it);
3232 eassert (it->dpvec && it->current.dpvec_index == 0);
3233 it->current.dpvec_index = pos->dpvec_index;
3234 }
3235
3236 CHECK_IT (it);
3237 return !overlay_strings_with_newlines;
3238 }
3239
3240
3241 /* Initialize IT for stepping through current_buffer in window W
3242 starting at ROW->start. */
3243
3244 static void
3245 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3246 {
3247 init_from_display_pos (it, w, &row->start);
3248 it->start = row->start;
3249 it->continuation_lines_width = row->continuation_lines_width;
3250 CHECK_IT (it);
3251 }
3252
3253
3254 /* Initialize IT for stepping through current_buffer in window W
3255 starting in the line following ROW, i.e. starting at ROW->end.
3256 Value is zero if there are overlay strings with newlines at ROW's
3257 end position. */
3258
3259 static int
3260 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3261 {
3262 int success = 0;
3263
3264 if (init_from_display_pos (it, w, &row->end))
3265 {
3266 if (row->continued_p)
3267 it->continuation_lines_width
3268 = row->continuation_lines_width + row->pixel_width;
3269 CHECK_IT (it);
3270 success = 1;
3271 }
3272
3273 return success;
3274 }
3275
3276
3277
3278 \f
3279 /***********************************************************************
3280 Text properties
3281 ***********************************************************************/
3282
3283 /* Called when IT reaches IT->stop_charpos. Handle text property and
3284 overlay changes. Set IT->stop_charpos to the next position where
3285 to stop. */
3286
3287 static void
3288 handle_stop (struct it *it)
3289 {
3290 enum prop_handled handled;
3291 int handle_overlay_change_p;
3292 struct props *p;
3293
3294 it->dpvec = NULL;
3295 it->current.dpvec_index = -1;
3296 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3297 it->ignore_overlay_strings_at_pos_p = 0;
3298 it->ellipsis_p = 0;
3299
3300 /* Use face of preceding text for ellipsis (if invisible) */
3301 if (it->selective_display_ellipsis_p)
3302 it->saved_face_id = it->face_id;
3303
3304 do
3305 {
3306 handled = HANDLED_NORMALLY;
3307
3308 /* Call text property handlers. */
3309 for (p = it_props; p->handler; ++p)
3310 {
3311 handled = p->handler (it);
3312
3313 if (handled == HANDLED_RECOMPUTE_PROPS)
3314 break;
3315 else if (handled == HANDLED_RETURN)
3316 {
3317 /* We still want to show before and after strings from
3318 overlays even if the actual buffer text is replaced. */
3319 if (!handle_overlay_change_p
3320 || it->sp > 1
3321 /* Don't call get_overlay_strings_1 if we already
3322 have overlay strings loaded, because doing so
3323 will load them again and push the iterator state
3324 onto the stack one more time, which is not
3325 expected by the rest of the code that processes
3326 overlay strings. */
3327 || (it->current.overlay_string_index < 0
3328 ? !get_overlay_strings_1 (it, 0, 0)
3329 : 0))
3330 {
3331 if (it->ellipsis_p)
3332 setup_for_ellipsis (it, 0);
3333 /* When handling a display spec, we might load an
3334 empty string. In that case, discard it here. We
3335 used to discard it in handle_single_display_spec,
3336 but that causes get_overlay_strings_1, above, to
3337 ignore overlay strings that we must check. */
3338 if (STRINGP (it->string) && !SCHARS (it->string))
3339 pop_it (it);
3340 return;
3341 }
3342 else if (STRINGP (it->string) && !SCHARS (it->string))
3343 pop_it (it);
3344 else
3345 {
3346 it->ignore_overlay_strings_at_pos_p = 1;
3347 it->string_from_display_prop_p = 0;
3348 it->from_disp_prop_p = 0;
3349 handle_overlay_change_p = 0;
3350 }
3351 handled = HANDLED_RECOMPUTE_PROPS;
3352 break;
3353 }
3354 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3355 handle_overlay_change_p = 0;
3356 }
3357
3358 if (handled != HANDLED_RECOMPUTE_PROPS)
3359 {
3360 /* Don't check for overlay strings below when set to deliver
3361 characters from a display vector. */
3362 if (it->method == GET_FROM_DISPLAY_VECTOR)
3363 handle_overlay_change_p = 0;
3364
3365 /* Handle overlay changes.
3366 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3367 if it finds overlays. */
3368 if (handle_overlay_change_p)
3369 handled = handle_overlay_change (it);
3370 }
3371
3372 if (it->ellipsis_p)
3373 {
3374 setup_for_ellipsis (it, 0);
3375 break;
3376 }
3377 }
3378 while (handled == HANDLED_RECOMPUTE_PROPS);
3379
3380 /* Determine where to stop next. */
3381 if (handled == HANDLED_NORMALLY)
3382 compute_stop_pos (it);
3383 }
3384
3385
3386 /* Compute IT->stop_charpos from text property and overlay change
3387 information for IT's current position. */
3388
3389 static void
3390 compute_stop_pos (struct it *it)
3391 {
3392 register INTERVAL iv, next_iv;
3393 Lisp_Object object, limit, position;
3394 ptrdiff_t charpos, bytepos;
3395
3396 if (STRINGP (it->string))
3397 {
3398 /* Strings are usually short, so don't limit the search for
3399 properties. */
3400 it->stop_charpos = it->end_charpos;
3401 object = it->string;
3402 limit = Qnil;
3403 charpos = IT_STRING_CHARPOS (*it);
3404 bytepos = IT_STRING_BYTEPOS (*it);
3405 }
3406 else
3407 {
3408 ptrdiff_t pos;
3409
3410 /* If end_charpos is out of range for some reason, such as a
3411 misbehaving display function, rationalize it (Bug#5984). */
3412 if (it->end_charpos > ZV)
3413 it->end_charpos = ZV;
3414 it->stop_charpos = it->end_charpos;
3415
3416 /* If next overlay change is in front of the current stop pos
3417 (which is IT->end_charpos), stop there. Note: value of
3418 next_overlay_change is point-max if no overlay change
3419 follows. */
3420 charpos = IT_CHARPOS (*it);
3421 bytepos = IT_BYTEPOS (*it);
3422 pos = next_overlay_change (charpos);
3423 if (pos < it->stop_charpos)
3424 it->stop_charpos = pos;
3425
3426 /* If showing the region, we have to stop at the region
3427 start or end because the face might change there. */
3428 if (it->region_beg_charpos > 0)
3429 {
3430 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3431 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3432 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3433 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3434 }
3435
3436 /* Set up variables for computing the stop position from text
3437 property changes. */
3438 XSETBUFFER (object, current_buffer);
3439 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3440 }
3441
3442 /* Get the interval containing IT's position. Value is a null
3443 interval if there isn't such an interval. */
3444 position = make_number (charpos);
3445 iv = validate_interval_range (object, &position, &position, 0);
3446 if (iv)
3447 {
3448 Lisp_Object values_here[LAST_PROP_IDX];
3449 struct props *p;
3450
3451 /* Get properties here. */
3452 for (p = it_props; p->handler; ++p)
3453 values_here[p->idx] = textget (iv->plist, *p->name);
3454
3455 /* Look for an interval following iv that has different
3456 properties. */
3457 for (next_iv = next_interval (iv);
3458 (next_iv
3459 && (NILP (limit)
3460 || XFASTINT (limit) > next_iv->position));
3461 next_iv = next_interval (next_iv))
3462 {
3463 for (p = it_props; p->handler; ++p)
3464 {
3465 Lisp_Object new_value;
3466
3467 new_value = textget (next_iv->plist, *p->name);
3468 if (!EQ (values_here[p->idx], new_value))
3469 break;
3470 }
3471
3472 if (p->handler)
3473 break;
3474 }
3475
3476 if (next_iv)
3477 {
3478 if (INTEGERP (limit)
3479 && next_iv->position >= XFASTINT (limit))
3480 /* No text property change up to limit. */
3481 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3482 else
3483 /* Text properties change in next_iv. */
3484 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3485 }
3486 }
3487
3488 if (it->cmp_it.id < 0)
3489 {
3490 ptrdiff_t stoppos = it->end_charpos;
3491
3492 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3493 stoppos = -1;
3494 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3495 stoppos, it->string);
3496 }
3497
3498 eassert (STRINGP (it->string)
3499 || (it->stop_charpos >= BEGV
3500 && it->stop_charpos >= IT_CHARPOS (*it)));
3501 }
3502
3503
3504 /* Return the position of the next overlay change after POS in
3505 current_buffer. Value is point-max if no overlay change
3506 follows. This is like `next-overlay-change' but doesn't use
3507 xmalloc. */
3508
3509 static ptrdiff_t
3510 next_overlay_change (ptrdiff_t pos)
3511 {
3512 ptrdiff_t i, noverlays;
3513 ptrdiff_t endpos;
3514 Lisp_Object *overlays;
3515
3516 /* Get all overlays at the given position. */
3517 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3518
3519 /* If any of these overlays ends before endpos,
3520 use its ending point instead. */
3521 for (i = 0; i < noverlays; ++i)
3522 {
3523 Lisp_Object oend;
3524 ptrdiff_t oendpos;
3525
3526 oend = OVERLAY_END (overlays[i]);
3527 oendpos = OVERLAY_POSITION (oend);
3528 endpos = min (endpos, oendpos);
3529 }
3530
3531 return endpos;
3532 }
3533
3534 /* How many characters forward to search for a display property or
3535 display string. Searching too far forward makes the bidi display
3536 sluggish, especially in small windows. */
3537 #define MAX_DISP_SCAN 250
3538
3539 /* Return the character position of a display string at or after
3540 position specified by POSITION. If no display string exists at or
3541 after POSITION, return ZV. A display string is either an overlay
3542 with `display' property whose value is a string, or a `display'
3543 text property whose value is a string. STRING is data about the
3544 string to iterate; if STRING->lstring is nil, we are iterating a
3545 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3546 on a GUI frame. DISP_PROP is set to zero if we searched
3547 MAX_DISP_SCAN characters forward without finding any display
3548 strings, non-zero otherwise. It is set to 2 if the display string
3549 uses any kind of `(space ...)' spec that will produce a stretch of
3550 white space in the text area. */
3551 ptrdiff_t
3552 compute_display_string_pos (struct text_pos *position,
3553 struct bidi_string_data *string,
3554 struct window *w,
3555 int frame_window_p, int *disp_prop)
3556 {
3557 /* OBJECT = nil means current buffer. */
3558 Lisp_Object object, object1;
3559 Lisp_Object pos, spec, limpos;
3560 int string_p = (string && (STRINGP (string->lstring) || string->s));
3561 ptrdiff_t eob = string_p ? string->schars : ZV;
3562 ptrdiff_t begb = string_p ? 0 : BEGV;
3563 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3564 ptrdiff_t lim =
3565 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3566 struct text_pos tpos;
3567 int rv = 0;
3568
3569 if (string && STRINGP (string->lstring))
3570 object1 = object = string->lstring;
3571 else if (w && !string_p)
3572 {
3573 XSETWINDOW (object, w);
3574 object1 = Qnil;
3575 }
3576 else
3577 object1 = object = Qnil;
3578
3579 *disp_prop = 1;
3580
3581 if (charpos >= eob
3582 /* We don't support display properties whose values are strings
3583 that have display string properties. */
3584 || string->from_disp_str
3585 /* C strings cannot have display properties. */
3586 || (string->s && !STRINGP (object)))
3587 {
3588 *disp_prop = 0;
3589 return eob;
3590 }
3591
3592 /* If the character at CHARPOS is where the display string begins,
3593 return CHARPOS. */
3594 pos = make_number (charpos);
3595 if (STRINGP (object))
3596 bufpos = string->bufpos;
3597 else
3598 bufpos = charpos;
3599 tpos = *position;
3600 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3601 && (charpos <= begb
3602 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3603 object),
3604 spec))
3605 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3606 frame_window_p)))
3607 {
3608 if (rv == 2)
3609 *disp_prop = 2;
3610 return charpos;
3611 }
3612
3613 /* Look forward for the first character with a `display' property
3614 that will replace the underlying text when displayed. */
3615 limpos = make_number (lim);
3616 do {
3617 pos = Fnext_single_char_property_change (pos, Qdisplay, object1, limpos);
3618 CHARPOS (tpos) = XFASTINT (pos);
3619 if (CHARPOS (tpos) >= lim)
3620 {
3621 *disp_prop = 0;
3622 break;
3623 }
3624 if (STRINGP (object))
3625 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3626 else
3627 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3628 spec = Fget_char_property (pos, Qdisplay, object);
3629 if (!STRINGP (object))
3630 bufpos = CHARPOS (tpos);
3631 } while (NILP (spec)
3632 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3633 bufpos, frame_window_p)));
3634 if (rv == 2)
3635 *disp_prop = 2;
3636
3637 return CHARPOS (tpos);
3638 }
3639
3640 /* Return the character position of the end of the display string that
3641 started at CHARPOS. If there's no display string at CHARPOS,
3642 return -1. A display string is either an overlay with `display'
3643 property whose value is a string or a `display' text property whose
3644 value is a string. */
3645 ptrdiff_t
3646 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3647 {
3648 /* OBJECT = nil means current buffer. */
3649 Lisp_Object object =
3650 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3651 Lisp_Object pos = make_number (charpos);
3652 ptrdiff_t eob =
3653 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3654
3655 if (charpos >= eob || (string->s && !STRINGP (object)))
3656 return eob;
3657
3658 /* It could happen that the display property or overlay was removed
3659 since we found it in compute_display_string_pos above. One way
3660 this can happen is if JIT font-lock was called (through
3661 handle_fontified_prop), and jit-lock-functions remove text
3662 properties or overlays from the portion of buffer that includes
3663 CHARPOS. Muse mode is known to do that, for example. In this
3664 case, we return -1 to the caller, to signal that no display
3665 string is actually present at CHARPOS. See bidi_fetch_char for
3666 how this is handled.
3667
3668 An alternative would be to never look for display properties past
3669 it->stop_charpos. But neither compute_display_string_pos nor
3670 bidi_fetch_char that calls it know or care where the next
3671 stop_charpos is. */
3672 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3673 return -1;
3674
3675 /* Look forward for the first character where the `display' property
3676 changes. */
3677 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3678
3679 return XFASTINT (pos);
3680 }
3681
3682
3683 \f
3684 /***********************************************************************
3685 Fontification
3686 ***********************************************************************/
3687
3688 /* Handle changes in the `fontified' property of the current buffer by
3689 calling hook functions from Qfontification_functions to fontify
3690 regions of text. */
3691
3692 static enum prop_handled
3693 handle_fontified_prop (struct it *it)
3694 {
3695 Lisp_Object prop, pos;
3696 enum prop_handled handled = HANDLED_NORMALLY;
3697
3698 if (!NILP (Vmemory_full))
3699 return handled;
3700
3701 /* Get the value of the `fontified' property at IT's current buffer
3702 position. (The `fontified' property doesn't have a special
3703 meaning in strings.) If the value is nil, call functions from
3704 Qfontification_functions. */
3705 if (!STRINGP (it->string)
3706 && it->s == NULL
3707 && !NILP (Vfontification_functions)
3708 && !NILP (Vrun_hooks)
3709 && (pos = make_number (IT_CHARPOS (*it)),
3710 prop = Fget_char_property (pos, Qfontified, Qnil),
3711 /* Ignore the special cased nil value always present at EOB since
3712 no amount of fontifying will be able to change it. */
3713 NILP (prop) && IT_CHARPOS (*it) < Z))
3714 {
3715 ptrdiff_t count = SPECPDL_INDEX ();
3716 Lisp_Object val;
3717 struct buffer *obuf = current_buffer;
3718 int begv = BEGV, zv = ZV;
3719 int old_clip_changed = current_buffer->clip_changed;
3720
3721 val = Vfontification_functions;
3722 specbind (Qfontification_functions, Qnil);
3723
3724 eassert (it->end_charpos == ZV);
3725
3726 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3727 safe_call1 (val, pos);
3728 else
3729 {
3730 Lisp_Object fns, fn;
3731 struct gcpro gcpro1, gcpro2;
3732
3733 fns = Qnil;
3734 GCPRO2 (val, fns);
3735
3736 for (; CONSP (val); val = XCDR (val))
3737 {
3738 fn = XCAR (val);
3739
3740 if (EQ (fn, Qt))
3741 {
3742 /* A value of t indicates this hook has a local
3743 binding; it means to run the global binding too.
3744 In a global value, t should not occur. If it
3745 does, we must ignore it to avoid an endless
3746 loop. */
3747 for (fns = Fdefault_value (Qfontification_functions);
3748 CONSP (fns);
3749 fns = XCDR (fns))
3750 {
3751 fn = XCAR (fns);
3752 if (!EQ (fn, Qt))
3753 safe_call1 (fn, pos);
3754 }
3755 }
3756 else
3757 safe_call1 (fn, pos);
3758 }
3759
3760 UNGCPRO;
3761 }
3762
3763 unbind_to (count, Qnil);
3764
3765 /* Fontification functions routinely call `save-restriction'.
3766 Normally, this tags clip_changed, which can confuse redisplay
3767 (see discussion in Bug#6671). Since we don't perform any
3768 special handling of fontification changes in the case where
3769 `save-restriction' isn't called, there's no point doing so in
3770 this case either. So, if the buffer's restrictions are
3771 actually left unchanged, reset clip_changed. */
3772 if (obuf == current_buffer)
3773 {
3774 if (begv == BEGV && zv == ZV)
3775 current_buffer->clip_changed = old_clip_changed;
3776 }
3777 /* There isn't much we can reasonably do to protect against
3778 misbehaving fontification, but here's a fig leaf. */
3779 else if (BUFFER_LIVE_P (obuf))
3780 set_buffer_internal_1 (obuf);
3781
3782 /* The fontification code may have added/removed text.
3783 It could do even a lot worse, but let's at least protect against
3784 the most obvious case where only the text past `pos' gets changed',
3785 as is/was done in grep.el where some escapes sequences are turned
3786 into face properties (bug#7876). */
3787 it->end_charpos = ZV;
3788
3789 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3790 something. This avoids an endless loop if they failed to
3791 fontify the text for which reason ever. */
3792 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3793 handled = HANDLED_RECOMPUTE_PROPS;
3794 }
3795
3796 return handled;
3797 }
3798
3799
3800 \f
3801 /***********************************************************************
3802 Faces
3803 ***********************************************************************/
3804
3805 /* Set up iterator IT from face properties at its current position.
3806 Called from handle_stop. */
3807
3808 static enum prop_handled
3809 handle_face_prop (struct it *it)
3810 {
3811 int new_face_id;
3812 ptrdiff_t next_stop;
3813
3814 if (!STRINGP (it->string))
3815 {
3816 new_face_id
3817 = face_at_buffer_position (it->w,
3818 IT_CHARPOS (*it),
3819 it->region_beg_charpos,
3820 it->region_end_charpos,
3821 &next_stop,
3822 (IT_CHARPOS (*it)
3823 + TEXT_PROP_DISTANCE_LIMIT),
3824 0, it->base_face_id);
3825
3826 /* Is this a start of a run of characters with box face?
3827 Caveat: this can be called for a freshly initialized
3828 iterator; face_id is -1 in this case. We know that the new
3829 face will not change until limit, i.e. if the new face has a
3830 box, all characters up to limit will have one. But, as
3831 usual, we don't know whether limit is really the end. */
3832 if (new_face_id != it->face_id)
3833 {
3834 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3835 /* If it->face_id is -1, old_face below will be NULL, see
3836 the definition of FACE_FROM_ID. This will happen if this
3837 is the initial call that gets the face. */
3838 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3839
3840 /* If the value of face_id of the iterator is -1, we have to
3841 look in front of IT's position and see whether there is a
3842 face there that's different from new_face_id. */
3843 if (!old_face && IT_CHARPOS (*it) > BEG)
3844 {
3845 int prev_face_id = face_before_it_pos (it);
3846
3847 old_face = FACE_FROM_ID (it->f, prev_face_id);
3848 }
3849
3850 /* If the new face has a box, but the old face does not,
3851 this is the start of a run of characters with box face,
3852 i.e. this character has a shadow on the left side. */
3853 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3854 && (old_face == NULL || !old_face->box));
3855 it->face_box_p = new_face->box != FACE_NO_BOX;
3856 }
3857 }
3858 else
3859 {
3860 int base_face_id;
3861 ptrdiff_t bufpos;
3862 int i;
3863 Lisp_Object from_overlay
3864 = (it->current.overlay_string_index >= 0
3865 ? it->string_overlays[it->current.overlay_string_index
3866 % OVERLAY_STRING_CHUNK_SIZE]
3867 : Qnil);
3868
3869 /* See if we got to this string directly or indirectly from
3870 an overlay property. That includes the before-string or
3871 after-string of an overlay, strings in display properties
3872 provided by an overlay, their text properties, etc.
3873
3874 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3875 if (! NILP (from_overlay))
3876 for (i = it->sp - 1; i >= 0; i--)
3877 {
3878 if (it->stack[i].current.overlay_string_index >= 0)
3879 from_overlay
3880 = it->string_overlays[it->stack[i].current.overlay_string_index
3881 % OVERLAY_STRING_CHUNK_SIZE];
3882 else if (! NILP (it->stack[i].from_overlay))
3883 from_overlay = it->stack[i].from_overlay;
3884
3885 if (!NILP (from_overlay))
3886 break;
3887 }
3888
3889 if (! NILP (from_overlay))
3890 {
3891 bufpos = IT_CHARPOS (*it);
3892 /* For a string from an overlay, the base face depends
3893 only on text properties and ignores overlays. */
3894 base_face_id
3895 = face_for_overlay_string (it->w,
3896 IT_CHARPOS (*it),
3897 it->region_beg_charpos,
3898 it->region_end_charpos,
3899 &next_stop,
3900 (IT_CHARPOS (*it)
3901 + TEXT_PROP_DISTANCE_LIMIT),
3902 0,
3903 from_overlay);
3904 }
3905 else
3906 {
3907 bufpos = 0;
3908
3909 /* For strings from a `display' property, use the face at
3910 IT's current buffer position as the base face to merge
3911 with, so that overlay strings appear in the same face as
3912 surrounding text, unless they specify their own
3913 faces. */
3914 base_face_id = it->string_from_prefix_prop_p
3915 ? DEFAULT_FACE_ID
3916 : underlying_face_id (it);
3917 }
3918
3919 new_face_id = face_at_string_position (it->w,
3920 it->string,
3921 IT_STRING_CHARPOS (*it),
3922 bufpos,
3923 it->region_beg_charpos,
3924 it->region_end_charpos,
3925 &next_stop,
3926 base_face_id, 0);
3927
3928 /* Is this a start of a run of characters with box? Caveat:
3929 this can be called for a freshly allocated iterator; face_id
3930 is -1 is this case. We know that the new face will not
3931 change until the next check pos, i.e. if the new face has a
3932 box, all characters up to that position will have a
3933 box. But, as usual, we don't know whether that position
3934 is really the end. */
3935 if (new_face_id != it->face_id)
3936 {
3937 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3938 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3939
3940 /* If new face has a box but old face hasn't, this is the
3941 start of a run of characters with box, i.e. it has a
3942 shadow on the left side. */
3943 it->start_of_box_run_p
3944 = new_face->box && (old_face == NULL || !old_face->box);
3945 it->face_box_p = new_face->box != FACE_NO_BOX;
3946 }
3947 }
3948
3949 it->face_id = new_face_id;
3950 return HANDLED_NORMALLY;
3951 }
3952
3953
3954 /* Return the ID of the face ``underlying'' IT's current position,
3955 which is in a string. If the iterator is associated with a
3956 buffer, return the face at IT's current buffer position.
3957 Otherwise, use the iterator's base_face_id. */
3958
3959 static int
3960 underlying_face_id (struct it *it)
3961 {
3962 int face_id = it->base_face_id, i;
3963
3964 eassert (STRINGP (it->string));
3965
3966 for (i = it->sp - 1; i >= 0; --i)
3967 if (NILP (it->stack[i].string))
3968 face_id = it->stack[i].face_id;
3969
3970 return face_id;
3971 }
3972
3973
3974 /* Compute the face one character before or after the current position
3975 of IT, in the visual order. BEFORE_P non-zero means get the face
3976 in front (to the left in L2R paragraphs, to the right in R2L
3977 paragraphs) of IT's screen position. Value is the ID of the face. */
3978
3979 static int
3980 face_before_or_after_it_pos (struct it *it, int before_p)
3981 {
3982 int face_id, limit;
3983 ptrdiff_t next_check_charpos;
3984 struct it it_copy;
3985 void *it_copy_data = NULL;
3986
3987 eassert (it->s == NULL);
3988
3989 if (STRINGP (it->string))
3990 {
3991 ptrdiff_t bufpos, charpos;
3992 int base_face_id;
3993
3994 /* No face change past the end of the string (for the case
3995 we are padding with spaces). No face change before the
3996 string start. */
3997 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3998 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3999 return it->face_id;
4000
4001 if (!it->bidi_p)
4002 {
4003 /* Set charpos to the position before or after IT's current
4004 position, in the logical order, which in the non-bidi
4005 case is the same as the visual order. */
4006 if (before_p)
4007 charpos = IT_STRING_CHARPOS (*it) - 1;
4008 else if (it->what == IT_COMPOSITION)
4009 /* For composition, we must check the character after the
4010 composition. */
4011 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
4012 else
4013 charpos = IT_STRING_CHARPOS (*it) + 1;
4014 }
4015 else
4016 {
4017 if (before_p)
4018 {
4019 /* With bidi iteration, the character before the current
4020 in the visual order cannot be found by simple
4021 iteration, because "reverse" reordering is not
4022 supported. Instead, we need to use the move_it_*
4023 family of functions. */
4024 /* Ignore face changes before the first visible
4025 character on this display line. */
4026 if (it->current_x <= it->first_visible_x)
4027 return it->face_id;
4028 SAVE_IT (it_copy, *it, it_copy_data);
4029 /* Implementation note: Since move_it_in_display_line
4030 works in the iterator geometry, and thinks the first
4031 character is always the leftmost, even in R2L lines,
4032 we don't need to distinguish between the R2L and L2R
4033 cases here. */
4034 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
4035 it_copy.current_x - 1, MOVE_TO_X);
4036 charpos = IT_STRING_CHARPOS (it_copy);
4037 RESTORE_IT (it, it, it_copy_data);
4038 }
4039 else
4040 {
4041 /* Set charpos to the string position of the character
4042 that comes after IT's current position in the visual
4043 order. */
4044 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4045
4046 it_copy = *it;
4047 while (n--)
4048 bidi_move_to_visually_next (&it_copy.bidi_it);
4049
4050 charpos = it_copy.bidi_it.charpos;
4051 }
4052 }
4053 eassert (0 <= charpos && charpos <= SCHARS (it->string));
4054
4055 if (it->current.overlay_string_index >= 0)
4056 bufpos = IT_CHARPOS (*it);
4057 else
4058 bufpos = 0;
4059
4060 base_face_id = underlying_face_id (it);
4061
4062 /* Get the face for ASCII, or unibyte. */
4063 face_id = face_at_string_position (it->w,
4064 it->string,
4065 charpos,
4066 bufpos,
4067 it->region_beg_charpos,
4068 it->region_end_charpos,
4069 &next_check_charpos,
4070 base_face_id, 0);
4071
4072 /* Correct the face for charsets different from ASCII. Do it
4073 for the multibyte case only. The face returned above is
4074 suitable for unibyte text if IT->string is unibyte. */
4075 if (STRING_MULTIBYTE (it->string))
4076 {
4077 struct text_pos pos1 = string_pos (charpos, it->string);
4078 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4079 int c, len;
4080 struct face *face = FACE_FROM_ID (it->f, face_id);
4081
4082 c = string_char_and_length (p, &len);
4083 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4084 }
4085 }
4086 else
4087 {
4088 struct text_pos pos;
4089
4090 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4091 || (IT_CHARPOS (*it) <= BEGV && before_p))
4092 return it->face_id;
4093
4094 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4095 pos = it->current.pos;
4096
4097 if (!it->bidi_p)
4098 {
4099 if (before_p)
4100 DEC_TEXT_POS (pos, it->multibyte_p);
4101 else
4102 {
4103 if (it->what == IT_COMPOSITION)
4104 {
4105 /* For composition, we must check the position after
4106 the composition. */
4107 pos.charpos += it->cmp_it.nchars;
4108 pos.bytepos += it->len;
4109 }
4110 else
4111 INC_TEXT_POS (pos, it->multibyte_p);
4112 }
4113 }
4114 else
4115 {
4116 if (before_p)
4117 {
4118 /* With bidi iteration, the character before the current
4119 in the visual order cannot be found by simple
4120 iteration, because "reverse" reordering is not
4121 supported. Instead, we need to use the move_it_*
4122 family of functions. */
4123 /* Ignore face changes before the first visible
4124 character on this display line. */
4125 if (it->current_x <= it->first_visible_x)
4126 return it->face_id;
4127 SAVE_IT (it_copy, *it, it_copy_data);
4128 /* Implementation note: Since move_it_in_display_line
4129 works in the iterator geometry, and thinks the first
4130 character is always the leftmost, even in R2L lines,
4131 we don't need to distinguish between the R2L and L2R
4132 cases here. */
4133 move_it_in_display_line (&it_copy, ZV,
4134 it_copy.current_x - 1, MOVE_TO_X);
4135 pos = it_copy.current.pos;
4136 RESTORE_IT (it, it, it_copy_data);
4137 }
4138 else
4139 {
4140 /* Set charpos to the buffer position of the character
4141 that comes after IT's current position in the visual
4142 order. */
4143 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4144
4145 it_copy = *it;
4146 while (n--)
4147 bidi_move_to_visually_next (&it_copy.bidi_it);
4148
4149 SET_TEXT_POS (pos,
4150 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4151 }
4152 }
4153 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4154
4155 /* Determine face for CHARSET_ASCII, or unibyte. */
4156 face_id = face_at_buffer_position (it->w,
4157 CHARPOS (pos),
4158 it->region_beg_charpos,
4159 it->region_end_charpos,
4160 &next_check_charpos,
4161 limit, 0, -1);
4162
4163 /* Correct the face for charsets different from ASCII. Do it
4164 for the multibyte case only. The face returned above is
4165 suitable for unibyte text if current_buffer is unibyte. */
4166 if (it->multibyte_p)
4167 {
4168 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4169 struct face *face = FACE_FROM_ID (it->f, face_id);
4170 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4171 }
4172 }
4173
4174 return face_id;
4175 }
4176
4177
4178 \f
4179 /***********************************************************************
4180 Invisible text
4181 ***********************************************************************/
4182
4183 /* Set up iterator IT from invisible properties at its current
4184 position. Called from handle_stop. */
4185
4186 static enum prop_handled
4187 handle_invisible_prop (struct it *it)
4188 {
4189 enum prop_handled handled = HANDLED_NORMALLY;
4190 int invis_p;
4191 Lisp_Object prop;
4192
4193 if (STRINGP (it->string))
4194 {
4195 Lisp_Object end_charpos, limit, charpos;
4196
4197 /* Get the value of the invisible text property at the
4198 current position. Value will be nil if there is no such
4199 property. */
4200 charpos = make_number (IT_STRING_CHARPOS (*it));
4201 prop = Fget_text_property (charpos, Qinvisible, it->string);
4202 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4203
4204 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4205 {
4206 /* Record whether we have to display an ellipsis for the
4207 invisible text. */
4208 int display_ellipsis_p = (invis_p == 2);
4209 ptrdiff_t len, endpos;
4210
4211 handled = HANDLED_RECOMPUTE_PROPS;
4212
4213 /* Get the position at which the next visible text can be
4214 found in IT->string, if any. */
4215 endpos = len = SCHARS (it->string);
4216 XSETINT (limit, len);
4217 do
4218 {
4219 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4220 it->string, limit);
4221 if (INTEGERP (end_charpos))
4222 {
4223 endpos = XFASTINT (end_charpos);
4224 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4225 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4226 if (invis_p == 2)
4227 display_ellipsis_p = 1;
4228 }
4229 }
4230 while (invis_p && endpos < len);
4231
4232 if (display_ellipsis_p)
4233 it->ellipsis_p = 1;
4234
4235 if (endpos < len)
4236 {
4237 /* Text at END_CHARPOS is visible. Move IT there. */
4238 struct text_pos old;
4239 ptrdiff_t oldpos;
4240
4241 old = it->current.string_pos;
4242 oldpos = CHARPOS (old);
4243 if (it->bidi_p)
4244 {
4245 if (it->bidi_it.first_elt
4246 && it->bidi_it.charpos < SCHARS (it->string))
4247 bidi_paragraph_init (it->paragraph_embedding,
4248 &it->bidi_it, 1);
4249 /* Bidi-iterate out of the invisible text. */
4250 do
4251 {
4252 bidi_move_to_visually_next (&it->bidi_it);
4253 }
4254 while (oldpos <= it->bidi_it.charpos
4255 && it->bidi_it.charpos < endpos);
4256
4257 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4258 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4259 if (IT_CHARPOS (*it) >= endpos)
4260 it->prev_stop = endpos;
4261 }
4262 else
4263 {
4264 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4265 compute_string_pos (&it->current.string_pos, old, it->string);
4266 }
4267 }
4268 else
4269 {
4270 /* The rest of the string is invisible. If this is an
4271 overlay string, proceed with the next overlay string
4272 or whatever comes and return a character from there. */
4273 if (it->current.overlay_string_index >= 0
4274 && !display_ellipsis_p)
4275 {
4276 next_overlay_string (it);
4277 /* Don't check for overlay strings when we just
4278 finished processing them. */
4279 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4280 }
4281 else
4282 {
4283 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4284 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4285 }
4286 }
4287 }
4288 }
4289 else
4290 {
4291 ptrdiff_t newpos, next_stop, start_charpos, tem;
4292 Lisp_Object pos, overlay;
4293
4294 /* First of all, is there invisible text at this position? */
4295 tem = start_charpos = IT_CHARPOS (*it);
4296 pos = make_number (tem);
4297 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4298 &overlay);
4299 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4300
4301 /* If we are on invisible text, skip over it. */
4302 if (invis_p && start_charpos < it->end_charpos)
4303 {
4304 /* Record whether we have to display an ellipsis for the
4305 invisible text. */
4306 int display_ellipsis_p = invis_p == 2;
4307
4308 handled = HANDLED_RECOMPUTE_PROPS;
4309
4310 /* Loop skipping over invisible text. The loop is left at
4311 ZV or with IT on the first char being visible again. */
4312 do
4313 {
4314 /* Try to skip some invisible text. Return value is the
4315 position reached which can be equal to where we start
4316 if there is nothing invisible there. This skips both
4317 over invisible text properties and overlays with
4318 invisible property. */
4319 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4320
4321 /* If we skipped nothing at all we weren't at invisible
4322 text in the first place. If everything to the end of
4323 the buffer was skipped, end the loop. */
4324 if (newpos == tem || newpos >= ZV)
4325 invis_p = 0;
4326 else
4327 {
4328 /* We skipped some characters but not necessarily
4329 all there are. Check if we ended up on visible
4330 text. Fget_char_property returns the property of
4331 the char before the given position, i.e. if we
4332 get invis_p = 0, this means that the char at
4333 newpos is visible. */
4334 pos = make_number (newpos);
4335 prop = Fget_char_property (pos, Qinvisible, it->window);
4336 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4337 }
4338
4339 /* If we ended up on invisible text, proceed to
4340 skip starting with next_stop. */
4341 if (invis_p)
4342 tem = next_stop;
4343
4344 /* If there are adjacent invisible texts, don't lose the
4345 second one's ellipsis. */
4346 if (invis_p == 2)
4347 display_ellipsis_p = 1;
4348 }
4349 while (invis_p);
4350
4351 /* The position newpos is now either ZV or on visible text. */
4352 if (it->bidi_p)
4353 {
4354 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4355 int on_newline =
4356 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4357 int after_newline =
4358 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4359
4360 /* If the invisible text ends on a newline or on a
4361 character after a newline, we can avoid the costly,
4362 character by character, bidi iteration to NEWPOS, and
4363 instead simply reseat the iterator there. That's
4364 because all bidi reordering information is tossed at
4365 the newline. This is a big win for modes that hide
4366 complete lines, like Outline, Org, etc. */
4367 if (on_newline || after_newline)
4368 {
4369 struct text_pos tpos;
4370 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4371
4372 SET_TEXT_POS (tpos, newpos, bpos);
4373 reseat_1 (it, tpos, 0);
4374 /* If we reseat on a newline/ZV, we need to prep the
4375 bidi iterator for advancing to the next character
4376 after the newline/EOB, keeping the current paragraph
4377 direction (so that PRODUCE_GLYPHS does TRT wrt
4378 prepending/appending glyphs to a glyph row). */
4379 if (on_newline)
4380 {
4381 it->bidi_it.first_elt = 0;
4382 it->bidi_it.paragraph_dir = pdir;
4383 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4384 it->bidi_it.nchars = 1;
4385 it->bidi_it.ch_len = 1;
4386 }
4387 }
4388 else /* Must use the slow method. */
4389 {
4390 /* With bidi iteration, the region of invisible text
4391 could start and/or end in the middle of a
4392 non-base embedding level. Therefore, we need to
4393 skip invisible text using the bidi iterator,
4394 starting at IT's current position, until we find
4395 ourselves outside of the invisible text.
4396 Skipping invisible text _after_ bidi iteration
4397 avoids affecting the visual order of the
4398 displayed text when invisible properties are
4399 added or removed. */
4400 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4401 {
4402 /* If we were `reseat'ed to a new paragraph,
4403 determine the paragraph base direction. We
4404 need to do it now because
4405 next_element_from_buffer may not have a
4406 chance to do it, if we are going to skip any
4407 text at the beginning, which resets the
4408 FIRST_ELT flag. */
4409 bidi_paragraph_init (it->paragraph_embedding,
4410 &it->bidi_it, 1);
4411 }
4412 do
4413 {
4414 bidi_move_to_visually_next (&it->bidi_it);
4415 }
4416 while (it->stop_charpos <= it->bidi_it.charpos
4417 && it->bidi_it.charpos < newpos);
4418 IT_CHARPOS (*it) = it->bidi_it.charpos;
4419 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4420 /* If we overstepped NEWPOS, record its position in
4421 the iterator, so that we skip invisible text if
4422 later the bidi iteration lands us in the
4423 invisible region again. */
4424 if (IT_CHARPOS (*it) >= newpos)
4425 it->prev_stop = newpos;
4426 }
4427 }
4428 else
4429 {
4430 IT_CHARPOS (*it) = newpos;
4431 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4432 }
4433
4434 /* If there are before-strings at the start of invisible
4435 text, and the text is invisible because of a text
4436 property, arrange to show before-strings because 20.x did
4437 it that way. (If the text is invisible because of an
4438 overlay property instead of a text property, this is
4439 already handled in the overlay code.) */
4440 if (NILP (overlay)
4441 && get_overlay_strings (it, it->stop_charpos))
4442 {
4443 handled = HANDLED_RECOMPUTE_PROPS;
4444 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4445 }
4446 else if (display_ellipsis_p)
4447 {
4448 /* Make sure that the glyphs of the ellipsis will get
4449 correct `charpos' values. If we would not update
4450 it->position here, the glyphs would belong to the
4451 last visible character _before_ the invisible
4452 text, which confuses `set_cursor_from_row'.
4453
4454 We use the last invisible position instead of the
4455 first because this way the cursor is always drawn on
4456 the first "." of the ellipsis, whenever PT is inside
4457 the invisible text. Otherwise the cursor would be
4458 placed _after_ the ellipsis when the point is after the
4459 first invisible character. */
4460 if (!STRINGP (it->object))
4461 {
4462 it->position.charpos = newpos - 1;
4463 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4464 }
4465 it->ellipsis_p = 1;
4466 /* Let the ellipsis display before
4467 considering any properties of the following char.
4468 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4469 handled = HANDLED_RETURN;
4470 }
4471 }
4472 }
4473
4474 return handled;
4475 }
4476
4477
4478 /* Make iterator IT return `...' next.
4479 Replaces LEN characters from buffer. */
4480
4481 static void
4482 setup_for_ellipsis (struct it *it, int len)
4483 {
4484 /* Use the display table definition for `...'. Invalid glyphs
4485 will be handled by the method returning elements from dpvec. */
4486 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4487 {
4488 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4489 it->dpvec = v->contents;
4490 it->dpend = v->contents + v->header.size;
4491 }
4492 else
4493 {
4494 /* Default `...'. */
4495 it->dpvec = default_invis_vector;
4496 it->dpend = default_invis_vector + 3;
4497 }
4498
4499 it->dpvec_char_len = len;
4500 it->current.dpvec_index = 0;
4501 it->dpvec_face_id = -1;
4502
4503 /* Remember the current face id in case glyphs specify faces.
4504 IT's face is restored in set_iterator_to_next.
4505 saved_face_id was set to preceding char's face in handle_stop. */
4506 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4507 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4508
4509 it->method = GET_FROM_DISPLAY_VECTOR;
4510 it->ellipsis_p = 1;
4511 }
4512
4513
4514 \f
4515 /***********************************************************************
4516 'display' property
4517 ***********************************************************************/
4518
4519 /* Set up iterator IT from `display' property at its current position.
4520 Called from handle_stop.
4521 We return HANDLED_RETURN if some part of the display property
4522 overrides the display of the buffer text itself.
4523 Otherwise we return HANDLED_NORMALLY. */
4524
4525 static enum prop_handled
4526 handle_display_prop (struct it *it)
4527 {
4528 Lisp_Object propval, object, overlay;
4529 struct text_pos *position;
4530 ptrdiff_t bufpos;
4531 /* Nonzero if some property replaces the display of the text itself. */
4532 int display_replaced_p = 0;
4533
4534 if (STRINGP (it->string))
4535 {
4536 object = it->string;
4537 position = &it->current.string_pos;
4538 bufpos = CHARPOS (it->current.pos);
4539 }
4540 else
4541 {
4542 XSETWINDOW (object, it->w);
4543 position = &it->current.pos;
4544 bufpos = CHARPOS (*position);
4545 }
4546
4547 /* Reset those iterator values set from display property values. */
4548 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4549 it->space_width = Qnil;
4550 it->font_height = Qnil;
4551 it->voffset = 0;
4552
4553 /* We don't support recursive `display' properties, i.e. string
4554 values that have a string `display' property, that have a string
4555 `display' property etc. */
4556 if (!it->string_from_display_prop_p)
4557 it->area = TEXT_AREA;
4558
4559 propval = get_char_property_and_overlay (make_number (position->charpos),
4560 Qdisplay, object, &overlay);
4561 if (NILP (propval))
4562 return HANDLED_NORMALLY;
4563 /* Now OVERLAY is the overlay that gave us this property, or nil
4564 if it was a text property. */
4565
4566 if (!STRINGP (it->string))
4567 object = it->w->contents;
4568
4569 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4570 position, bufpos,
4571 FRAME_WINDOW_P (it->f));
4572
4573 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4574 }
4575
4576 /* Subroutine of handle_display_prop. Returns non-zero if the display
4577 specification in SPEC is a replacing specification, i.e. it would
4578 replace the text covered by `display' property with something else,
4579 such as an image or a display string. If SPEC includes any kind or
4580 `(space ...) specification, the value is 2; this is used by
4581 compute_display_string_pos, which see.
4582
4583 See handle_single_display_spec for documentation of arguments.
4584 frame_window_p is non-zero if the window being redisplayed is on a
4585 GUI frame; this argument is used only if IT is NULL, see below.
4586
4587 IT can be NULL, if this is called by the bidi reordering code
4588 through compute_display_string_pos, which see. In that case, this
4589 function only examines SPEC, but does not otherwise "handle" it, in
4590 the sense that it doesn't set up members of IT from the display
4591 spec. */
4592 static int
4593 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4594 Lisp_Object overlay, struct text_pos *position,
4595 ptrdiff_t bufpos, int frame_window_p)
4596 {
4597 int replacing_p = 0;
4598 int rv;
4599
4600 if (CONSP (spec)
4601 /* Simple specifications. */
4602 && !EQ (XCAR (spec), Qimage)
4603 && !EQ (XCAR (spec), Qspace)
4604 && !EQ (XCAR (spec), Qwhen)
4605 && !EQ (XCAR (spec), Qslice)
4606 && !EQ (XCAR (spec), Qspace_width)
4607 && !EQ (XCAR (spec), Qheight)
4608 && !EQ (XCAR (spec), Qraise)
4609 /* Marginal area specifications. */
4610 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4611 && !EQ (XCAR (spec), Qleft_fringe)
4612 && !EQ (XCAR (spec), Qright_fringe)
4613 && !NILP (XCAR (spec)))
4614 {
4615 for (; CONSP (spec); spec = XCDR (spec))
4616 {
4617 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4618 overlay, position, bufpos,
4619 replacing_p, frame_window_p)))
4620 {
4621 replacing_p = rv;
4622 /* If some text in a string is replaced, `position' no
4623 longer points to the position of `object'. */
4624 if (!it || STRINGP (object))
4625 break;
4626 }
4627 }
4628 }
4629 else if (VECTORP (spec))
4630 {
4631 ptrdiff_t i;
4632 for (i = 0; i < ASIZE (spec); ++i)
4633 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4634 overlay, position, bufpos,
4635 replacing_p, frame_window_p)))
4636 {
4637 replacing_p = rv;
4638 /* If some text in a string is replaced, `position' no
4639 longer points to the position of `object'. */
4640 if (!it || STRINGP (object))
4641 break;
4642 }
4643 }
4644 else
4645 {
4646 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4647 position, bufpos, 0,
4648 frame_window_p)))
4649 replacing_p = rv;
4650 }
4651
4652 return replacing_p;
4653 }
4654
4655 /* Value is the position of the end of the `display' property starting
4656 at START_POS in OBJECT. */
4657
4658 static struct text_pos
4659 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4660 {
4661 Lisp_Object end;
4662 struct text_pos end_pos;
4663
4664 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4665 Qdisplay, object, Qnil);
4666 CHARPOS (end_pos) = XFASTINT (end);
4667 if (STRINGP (object))
4668 compute_string_pos (&end_pos, start_pos, it->string);
4669 else
4670 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4671
4672 return end_pos;
4673 }
4674
4675
4676 /* Set up IT from a single `display' property specification SPEC. OBJECT
4677 is the object in which the `display' property was found. *POSITION
4678 is the position in OBJECT at which the `display' property was found.
4679 BUFPOS is the buffer position of OBJECT (different from POSITION if
4680 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4681 previously saw a display specification which already replaced text
4682 display with something else, for example an image; we ignore such
4683 properties after the first one has been processed.
4684
4685 OVERLAY is the overlay this `display' property came from,
4686 or nil if it was a text property.
4687
4688 If SPEC is a `space' or `image' specification, and in some other
4689 cases too, set *POSITION to the position where the `display'
4690 property ends.
4691
4692 If IT is NULL, only examine the property specification in SPEC, but
4693 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4694 is intended to be displayed in a window on a GUI frame.
4695
4696 Value is non-zero if something was found which replaces the display
4697 of buffer or string text. */
4698
4699 static int
4700 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4701 Lisp_Object overlay, struct text_pos *position,
4702 ptrdiff_t bufpos, int display_replaced_p,
4703 int frame_window_p)
4704 {
4705 Lisp_Object form;
4706 Lisp_Object location, value;
4707 struct text_pos start_pos = *position;
4708 int valid_p;
4709
4710 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4711 If the result is non-nil, use VALUE instead of SPEC. */
4712 form = Qt;
4713 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4714 {
4715 spec = XCDR (spec);
4716 if (!CONSP (spec))
4717 return 0;
4718 form = XCAR (spec);
4719 spec = XCDR (spec);
4720 }
4721
4722 if (!NILP (form) && !EQ (form, Qt))
4723 {
4724 ptrdiff_t count = SPECPDL_INDEX ();
4725 struct gcpro gcpro1;
4726
4727 /* Bind `object' to the object having the `display' property, a
4728 buffer or string. Bind `position' to the position in the
4729 object where the property was found, and `buffer-position'
4730 to the current position in the buffer. */
4731
4732 if (NILP (object))
4733 XSETBUFFER (object, current_buffer);
4734 specbind (Qobject, object);
4735 specbind (Qposition, make_number (CHARPOS (*position)));
4736 specbind (Qbuffer_position, make_number (bufpos));
4737 GCPRO1 (form);
4738 form = safe_eval (form);
4739 UNGCPRO;
4740 unbind_to (count, Qnil);
4741 }
4742
4743 if (NILP (form))
4744 return 0;
4745
4746 /* Handle `(height HEIGHT)' specifications. */
4747 if (CONSP (spec)
4748 && EQ (XCAR (spec), Qheight)
4749 && CONSP (XCDR (spec)))
4750 {
4751 if (it)
4752 {
4753 if (!FRAME_WINDOW_P (it->f))
4754 return 0;
4755
4756 it->font_height = XCAR (XCDR (spec));
4757 if (!NILP (it->font_height))
4758 {
4759 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4760 int new_height = -1;
4761
4762 if (CONSP (it->font_height)
4763 && (EQ (XCAR (it->font_height), Qplus)
4764 || EQ (XCAR (it->font_height), Qminus))
4765 && CONSP (XCDR (it->font_height))
4766 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4767 {
4768 /* `(+ N)' or `(- N)' where N is an integer. */
4769 int steps = XINT (XCAR (XCDR (it->font_height)));
4770 if (EQ (XCAR (it->font_height), Qplus))
4771 steps = - steps;
4772 it->face_id = smaller_face (it->f, it->face_id, steps);
4773 }
4774 else if (FUNCTIONP (it->font_height))
4775 {
4776 /* Call function with current height as argument.
4777 Value is the new height. */
4778 Lisp_Object height;
4779 height = safe_call1 (it->font_height,
4780 face->lface[LFACE_HEIGHT_INDEX]);
4781 if (NUMBERP (height))
4782 new_height = XFLOATINT (height);
4783 }
4784 else if (NUMBERP (it->font_height))
4785 {
4786 /* Value is a multiple of the canonical char height. */
4787 struct face *f;
4788
4789 f = FACE_FROM_ID (it->f,
4790 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4791 new_height = (XFLOATINT (it->font_height)
4792 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4793 }
4794 else
4795 {
4796 /* Evaluate IT->font_height with `height' bound to the
4797 current specified height to get the new height. */
4798 ptrdiff_t count = SPECPDL_INDEX ();
4799
4800 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4801 value = safe_eval (it->font_height);
4802 unbind_to (count, Qnil);
4803
4804 if (NUMBERP (value))
4805 new_height = XFLOATINT (value);
4806 }
4807
4808 if (new_height > 0)
4809 it->face_id = face_with_height (it->f, it->face_id, new_height);
4810 }
4811 }
4812
4813 return 0;
4814 }
4815
4816 /* Handle `(space-width WIDTH)'. */
4817 if (CONSP (spec)
4818 && EQ (XCAR (spec), Qspace_width)
4819 && CONSP (XCDR (spec)))
4820 {
4821 if (it)
4822 {
4823 if (!FRAME_WINDOW_P (it->f))
4824 return 0;
4825
4826 value = XCAR (XCDR (spec));
4827 if (NUMBERP (value) && XFLOATINT (value) > 0)
4828 it->space_width = value;
4829 }
4830
4831 return 0;
4832 }
4833
4834 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4835 if (CONSP (spec)
4836 && EQ (XCAR (spec), Qslice))
4837 {
4838 Lisp_Object tem;
4839
4840 if (it)
4841 {
4842 if (!FRAME_WINDOW_P (it->f))
4843 return 0;
4844
4845 if (tem = XCDR (spec), CONSP (tem))
4846 {
4847 it->slice.x = XCAR (tem);
4848 if (tem = XCDR (tem), CONSP (tem))
4849 {
4850 it->slice.y = XCAR (tem);
4851 if (tem = XCDR (tem), CONSP (tem))
4852 {
4853 it->slice.width = XCAR (tem);
4854 if (tem = XCDR (tem), CONSP (tem))
4855 it->slice.height = XCAR (tem);
4856 }
4857 }
4858 }
4859 }
4860
4861 return 0;
4862 }
4863
4864 /* Handle `(raise FACTOR)'. */
4865 if (CONSP (spec)
4866 && EQ (XCAR (spec), Qraise)
4867 && CONSP (XCDR (spec)))
4868 {
4869 if (it)
4870 {
4871 if (!FRAME_WINDOW_P (it->f))
4872 return 0;
4873
4874 #ifdef HAVE_WINDOW_SYSTEM
4875 value = XCAR (XCDR (spec));
4876 if (NUMBERP (value))
4877 {
4878 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4879 it->voffset = - (XFLOATINT (value)
4880 * (FONT_HEIGHT (face->font)));
4881 }
4882 #endif /* HAVE_WINDOW_SYSTEM */
4883 }
4884
4885 return 0;
4886 }
4887
4888 /* Don't handle the other kinds of display specifications
4889 inside a string that we got from a `display' property. */
4890 if (it && it->string_from_display_prop_p)
4891 return 0;
4892
4893 /* Characters having this form of property are not displayed, so
4894 we have to find the end of the property. */
4895 if (it)
4896 {
4897 start_pos = *position;
4898 *position = display_prop_end (it, object, start_pos);
4899 }
4900 value = Qnil;
4901
4902 /* Stop the scan at that end position--we assume that all
4903 text properties change there. */
4904 if (it)
4905 it->stop_charpos = position->charpos;
4906
4907 /* Handle `(left-fringe BITMAP [FACE])'
4908 and `(right-fringe BITMAP [FACE])'. */
4909 if (CONSP (spec)
4910 && (EQ (XCAR (spec), Qleft_fringe)
4911 || EQ (XCAR (spec), Qright_fringe))
4912 && CONSP (XCDR (spec)))
4913 {
4914 int fringe_bitmap;
4915
4916 if (it)
4917 {
4918 if (!FRAME_WINDOW_P (it->f))
4919 /* If we return here, POSITION has been advanced
4920 across the text with this property. */
4921 {
4922 /* Synchronize the bidi iterator with POSITION. This is
4923 needed because we are not going to push the iterator
4924 on behalf of this display property, so there will be
4925 no pop_it call to do this synchronization for us. */
4926 if (it->bidi_p)
4927 {
4928 it->position = *position;
4929 iterate_out_of_display_property (it);
4930 *position = it->position;
4931 }
4932 return 1;
4933 }
4934 }
4935 else if (!frame_window_p)
4936 return 1;
4937
4938 #ifdef HAVE_WINDOW_SYSTEM
4939 value = XCAR (XCDR (spec));
4940 if (!SYMBOLP (value)
4941 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4942 /* If we return here, POSITION has been advanced
4943 across the text with this property. */
4944 {
4945 if (it && it->bidi_p)
4946 {
4947 it->position = *position;
4948 iterate_out_of_display_property (it);
4949 *position = it->position;
4950 }
4951 return 1;
4952 }
4953
4954 if (it)
4955 {
4956 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4957
4958 if (CONSP (XCDR (XCDR (spec))))
4959 {
4960 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4961 int face_id2 = lookup_derived_face (it->f, face_name,
4962 FRINGE_FACE_ID, 0);
4963 if (face_id2 >= 0)
4964 face_id = face_id2;
4965 }
4966
4967 /* Save current settings of IT so that we can restore them
4968 when we are finished with the glyph property value. */
4969 push_it (it, position);
4970
4971 it->area = TEXT_AREA;
4972 it->what = IT_IMAGE;
4973 it->image_id = -1; /* no image */
4974 it->position = start_pos;
4975 it->object = NILP (object) ? it->w->contents : object;
4976 it->method = GET_FROM_IMAGE;
4977 it->from_overlay = Qnil;
4978 it->face_id = face_id;
4979 it->from_disp_prop_p = 1;
4980
4981 /* Say that we haven't consumed the characters with
4982 `display' property yet. The call to pop_it in
4983 set_iterator_to_next will clean this up. */
4984 *position = start_pos;
4985
4986 if (EQ (XCAR (spec), Qleft_fringe))
4987 {
4988 it->left_user_fringe_bitmap = fringe_bitmap;
4989 it->left_user_fringe_face_id = face_id;
4990 }
4991 else
4992 {
4993 it->right_user_fringe_bitmap = fringe_bitmap;
4994 it->right_user_fringe_face_id = face_id;
4995 }
4996 }
4997 #endif /* HAVE_WINDOW_SYSTEM */
4998 return 1;
4999 }
5000
5001 /* Prepare to handle `((margin left-margin) ...)',
5002 `((margin right-margin) ...)' and `((margin nil) ...)'
5003 prefixes for display specifications. */
5004 location = Qunbound;
5005 if (CONSP (spec) && CONSP (XCAR (spec)))
5006 {
5007 Lisp_Object tem;
5008
5009 value = XCDR (spec);
5010 if (CONSP (value))
5011 value = XCAR (value);
5012
5013 tem = XCAR (spec);
5014 if (EQ (XCAR (tem), Qmargin)
5015 && (tem = XCDR (tem),
5016 tem = CONSP (tem) ? XCAR (tem) : Qnil,
5017 (NILP (tem)
5018 || EQ (tem, Qleft_margin)
5019 || EQ (tem, Qright_margin))))
5020 location = tem;
5021 }
5022
5023 if (EQ (location, Qunbound))
5024 {
5025 location = Qnil;
5026 value = spec;
5027 }
5028
5029 /* After this point, VALUE is the property after any
5030 margin prefix has been stripped. It must be a string,
5031 an image specification, or `(space ...)'.
5032
5033 LOCATION specifies where to display: `left-margin',
5034 `right-margin' or nil. */
5035
5036 valid_p = (STRINGP (value)
5037 #ifdef HAVE_WINDOW_SYSTEM
5038 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
5039 && valid_image_p (value))
5040 #endif /* not HAVE_WINDOW_SYSTEM */
5041 || (CONSP (value) && EQ (XCAR (value), Qspace)));
5042
5043 if (valid_p && !display_replaced_p)
5044 {
5045 int retval = 1;
5046
5047 if (!it)
5048 {
5049 /* Callers need to know whether the display spec is any kind
5050 of `(space ...)' spec that is about to affect text-area
5051 display. */
5052 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
5053 retval = 2;
5054 return retval;
5055 }
5056
5057 /* Save current settings of IT so that we can restore them
5058 when we are finished with the glyph property value. */
5059 push_it (it, position);
5060 it->from_overlay = overlay;
5061 it->from_disp_prop_p = 1;
5062
5063 if (NILP (location))
5064 it->area = TEXT_AREA;
5065 else if (EQ (location, Qleft_margin))
5066 it->area = LEFT_MARGIN_AREA;
5067 else
5068 it->area = RIGHT_MARGIN_AREA;
5069
5070 if (STRINGP (value))
5071 {
5072 it->string = value;
5073 it->multibyte_p = STRING_MULTIBYTE (it->string);
5074 it->current.overlay_string_index = -1;
5075 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5076 it->end_charpos = it->string_nchars = SCHARS (it->string);
5077 it->method = GET_FROM_STRING;
5078 it->stop_charpos = 0;
5079 it->prev_stop = 0;
5080 it->base_level_stop = 0;
5081 it->string_from_display_prop_p = 1;
5082 /* Say that we haven't consumed the characters with
5083 `display' property yet. The call to pop_it in
5084 set_iterator_to_next will clean this up. */
5085 if (BUFFERP (object))
5086 *position = start_pos;
5087
5088 /* Force paragraph direction to be that of the parent
5089 object. If the parent object's paragraph direction is
5090 not yet determined, default to L2R. */
5091 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5092 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5093 else
5094 it->paragraph_embedding = L2R;
5095
5096 /* Set up the bidi iterator for this display string. */
5097 if (it->bidi_p)
5098 {
5099 it->bidi_it.string.lstring = it->string;
5100 it->bidi_it.string.s = NULL;
5101 it->bidi_it.string.schars = it->end_charpos;
5102 it->bidi_it.string.bufpos = bufpos;
5103 it->bidi_it.string.from_disp_str = 1;
5104 it->bidi_it.string.unibyte = !it->multibyte_p;
5105 it->bidi_it.w = it->w;
5106 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5107 }
5108 }
5109 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5110 {
5111 it->method = GET_FROM_STRETCH;
5112 it->object = value;
5113 *position = it->position = start_pos;
5114 retval = 1 + (it->area == TEXT_AREA);
5115 }
5116 #ifdef HAVE_WINDOW_SYSTEM
5117 else
5118 {
5119 it->what = IT_IMAGE;
5120 it->image_id = lookup_image (it->f, value);
5121 it->position = start_pos;
5122 it->object = NILP (object) ? it->w->contents : object;
5123 it->method = GET_FROM_IMAGE;
5124
5125 /* Say that we haven't consumed the characters with
5126 `display' property yet. The call to pop_it in
5127 set_iterator_to_next will clean this up. */
5128 *position = start_pos;
5129 }
5130 #endif /* HAVE_WINDOW_SYSTEM */
5131
5132 return retval;
5133 }
5134
5135 /* Invalid property or property not supported. Restore
5136 POSITION to what it was before. */
5137 *position = start_pos;
5138 return 0;
5139 }
5140
5141 /* Check if PROP is a display property value whose text should be
5142 treated as intangible. OVERLAY is the overlay from which PROP
5143 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5144 specify the buffer position covered by PROP. */
5145
5146 int
5147 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5148 ptrdiff_t charpos, ptrdiff_t bytepos)
5149 {
5150 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5151 struct text_pos position;
5152
5153 SET_TEXT_POS (position, charpos, bytepos);
5154 return handle_display_spec (NULL, prop, Qnil, overlay,
5155 &position, charpos, frame_window_p);
5156 }
5157
5158
5159 /* Return 1 if PROP is a display sub-property value containing STRING.
5160
5161 Implementation note: this and the following function are really
5162 special cases of handle_display_spec and
5163 handle_single_display_spec, and should ideally use the same code.
5164 Until they do, these two pairs must be consistent and must be
5165 modified in sync. */
5166
5167 static int
5168 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5169 {
5170 if (EQ (string, prop))
5171 return 1;
5172
5173 /* Skip over `when FORM'. */
5174 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5175 {
5176 prop = XCDR (prop);
5177 if (!CONSP (prop))
5178 return 0;
5179 /* Actually, the condition following `when' should be eval'ed,
5180 like handle_single_display_spec does, and we should return
5181 zero if it evaluates to nil. However, this function is
5182 called only when the buffer was already displayed and some
5183 glyph in the glyph matrix was found to come from a display
5184 string. Therefore, the condition was already evaluated, and
5185 the result was non-nil, otherwise the display string wouldn't
5186 have been displayed and we would have never been called for
5187 this property. Thus, we can skip the evaluation and assume
5188 its result is non-nil. */
5189 prop = XCDR (prop);
5190 }
5191
5192 if (CONSP (prop))
5193 /* Skip over `margin LOCATION'. */
5194 if (EQ (XCAR (prop), Qmargin))
5195 {
5196 prop = XCDR (prop);
5197 if (!CONSP (prop))
5198 return 0;
5199
5200 prop = XCDR (prop);
5201 if (!CONSP (prop))
5202 return 0;
5203 }
5204
5205 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5206 }
5207
5208
5209 /* Return 1 if STRING appears in the `display' property PROP. */
5210
5211 static int
5212 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5213 {
5214 if (CONSP (prop)
5215 && !EQ (XCAR (prop), Qwhen)
5216 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5217 {
5218 /* A list of sub-properties. */
5219 while (CONSP (prop))
5220 {
5221 if (single_display_spec_string_p (XCAR (prop), string))
5222 return 1;
5223 prop = XCDR (prop);
5224 }
5225 }
5226 else if (VECTORP (prop))
5227 {
5228 /* A vector of sub-properties. */
5229 ptrdiff_t i;
5230 for (i = 0; i < ASIZE (prop); ++i)
5231 if (single_display_spec_string_p (AREF (prop, i), string))
5232 return 1;
5233 }
5234 else
5235 return single_display_spec_string_p (prop, string);
5236
5237 return 0;
5238 }
5239
5240 /* Look for STRING in overlays and text properties in the current
5241 buffer, between character positions FROM and TO (excluding TO).
5242 BACK_P non-zero means look back (in this case, TO is supposed to be
5243 less than FROM).
5244 Value is the first character position where STRING was found, or
5245 zero if it wasn't found before hitting TO.
5246
5247 This function may only use code that doesn't eval because it is
5248 called asynchronously from note_mouse_highlight. */
5249
5250 static ptrdiff_t
5251 string_buffer_position_lim (Lisp_Object string,
5252 ptrdiff_t from, ptrdiff_t to, int back_p)
5253 {
5254 Lisp_Object limit, prop, pos;
5255 int found = 0;
5256
5257 pos = make_number (max (from, BEGV));
5258
5259 if (!back_p) /* looking forward */
5260 {
5261 limit = make_number (min (to, ZV));
5262 while (!found && !EQ (pos, limit))
5263 {
5264 prop = Fget_char_property (pos, Qdisplay, Qnil);
5265 if (!NILP (prop) && display_prop_string_p (prop, string))
5266 found = 1;
5267 else
5268 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5269 limit);
5270 }
5271 }
5272 else /* looking back */
5273 {
5274 limit = make_number (max (to, BEGV));
5275 while (!found && !EQ (pos, limit))
5276 {
5277 prop = Fget_char_property (pos, Qdisplay, Qnil);
5278 if (!NILP (prop) && display_prop_string_p (prop, string))
5279 found = 1;
5280 else
5281 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5282 limit);
5283 }
5284 }
5285
5286 return found ? XINT (pos) : 0;
5287 }
5288
5289 /* Determine which buffer position in current buffer STRING comes from.
5290 AROUND_CHARPOS is an approximate position where it could come from.
5291 Value is the buffer position or 0 if it couldn't be determined.
5292
5293 This function is necessary because we don't record buffer positions
5294 in glyphs generated from strings (to keep struct glyph small).
5295 This function may only use code that doesn't eval because it is
5296 called asynchronously from note_mouse_highlight. */
5297
5298 static ptrdiff_t
5299 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5300 {
5301 const int MAX_DISTANCE = 1000;
5302 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5303 around_charpos + MAX_DISTANCE,
5304 0);
5305
5306 if (!found)
5307 found = string_buffer_position_lim (string, around_charpos,
5308 around_charpos - MAX_DISTANCE, 1);
5309 return found;
5310 }
5311
5312
5313 \f
5314 /***********************************************************************
5315 `composition' property
5316 ***********************************************************************/
5317
5318 /* Set up iterator IT from `composition' property at its current
5319 position. Called from handle_stop. */
5320
5321 static enum prop_handled
5322 handle_composition_prop (struct it *it)
5323 {
5324 Lisp_Object prop, string;
5325 ptrdiff_t pos, pos_byte, start, end;
5326
5327 if (STRINGP (it->string))
5328 {
5329 unsigned char *s;
5330
5331 pos = IT_STRING_CHARPOS (*it);
5332 pos_byte = IT_STRING_BYTEPOS (*it);
5333 string = it->string;
5334 s = SDATA (string) + pos_byte;
5335 it->c = STRING_CHAR (s);
5336 }
5337 else
5338 {
5339 pos = IT_CHARPOS (*it);
5340 pos_byte = IT_BYTEPOS (*it);
5341 string = Qnil;
5342 it->c = FETCH_CHAR (pos_byte);
5343 }
5344
5345 /* If there's a valid composition and point is not inside of the
5346 composition (in the case that the composition is from the current
5347 buffer), draw a glyph composed from the composition components. */
5348 if (find_composition (pos, -1, &start, &end, &prop, string)
5349 && COMPOSITION_VALID_P (start, end, prop)
5350 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5351 {
5352 if (start < pos)
5353 /* As we can't handle this situation (perhaps font-lock added
5354 a new composition), we just return here hoping that next
5355 redisplay will detect this composition much earlier. */
5356 return HANDLED_NORMALLY;
5357 if (start != pos)
5358 {
5359 if (STRINGP (it->string))
5360 pos_byte = string_char_to_byte (it->string, start);
5361 else
5362 pos_byte = CHAR_TO_BYTE (start);
5363 }
5364 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5365 prop, string);
5366
5367 if (it->cmp_it.id >= 0)
5368 {
5369 it->cmp_it.ch = -1;
5370 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5371 it->cmp_it.nglyphs = -1;
5372 }
5373 }
5374
5375 return HANDLED_NORMALLY;
5376 }
5377
5378
5379 \f
5380 /***********************************************************************
5381 Overlay strings
5382 ***********************************************************************/
5383
5384 /* The following structure is used to record overlay strings for
5385 later sorting in load_overlay_strings. */
5386
5387 struct overlay_entry
5388 {
5389 Lisp_Object overlay;
5390 Lisp_Object string;
5391 EMACS_INT priority;
5392 int after_string_p;
5393 };
5394
5395
5396 /* Set up iterator IT from overlay strings at its current position.
5397 Called from handle_stop. */
5398
5399 static enum prop_handled
5400 handle_overlay_change (struct it *it)
5401 {
5402 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5403 return HANDLED_RECOMPUTE_PROPS;
5404 else
5405 return HANDLED_NORMALLY;
5406 }
5407
5408
5409 /* Set up the next overlay string for delivery by IT, if there is an
5410 overlay string to deliver. Called by set_iterator_to_next when the
5411 end of the current overlay string is reached. If there are more
5412 overlay strings to display, IT->string and
5413 IT->current.overlay_string_index are set appropriately here.
5414 Otherwise IT->string is set to nil. */
5415
5416 static void
5417 next_overlay_string (struct it *it)
5418 {
5419 ++it->current.overlay_string_index;
5420 if (it->current.overlay_string_index == it->n_overlay_strings)
5421 {
5422 /* No more overlay strings. Restore IT's settings to what
5423 they were before overlay strings were processed, and
5424 continue to deliver from current_buffer. */
5425
5426 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5427 pop_it (it);
5428 eassert (it->sp > 0
5429 || (NILP (it->string)
5430 && it->method == GET_FROM_BUFFER
5431 && it->stop_charpos >= BEGV
5432 && it->stop_charpos <= it->end_charpos));
5433 it->current.overlay_string_index = -1;
5434 it->n_overlay_strings = 0;
5435 it->overlay_strings_charpos = -1;
5436 /* If there's an empty display string on the stack, pop the
5437 stack, to resync the bidi iterator with IT's position. Such
5438 empty strings are pushed onto the stack in
5439 get_overlay_strings_1. */
5440 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5441 pop_it (it);
5442
5443 /* If we're at the end of the buffer, record that we have
5444 processed the overlay strings there already, so that
5445 next_element_from_buffer doesn't try it again. */
5446 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5447 it->overlay_strings_at_end_processed_p = 1;
5448 }
5449 else
5450 {
5451 /* There are more overlay strings to process. If
5452 IT->current.overlay_string_index has advanced to a position
5453 where we must load IT->overlay_strings with more strings, do
5454 it. We must load at the IT->overlay_strings_charpos where
5455 IT->n_overlay_strings was originally computed; when invisible
5456 text is present, this might not be IT_CHARPOS (Bug#7016). */
5457 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5458
5459 if (it->current.overlay_string_index && i == 0)
5460 load_overlay_strings (it, it->overlay_strings_charpos);
5461
5462 /* Initialize IT to deliver display elements from the overlay
5463 string. */
5464 it->string = it->overlay_strings[i];
5465 it->multibyte_p = STRING_MULTIBYTE (it->string);
5466 SET_TEXT_POS (it->current.string_pos, 0, 0);
5467 it->method = GET_FROM_STRING;
5468 it->stop_charpos = 0;
5469 it->end_charpos = SCHARS (it->string);
5470 if (it->cmp_it.stop_pos >= 0)
5471 it->cmp_it.stop_pos = 0;
5472 it->prev_stop = 0;
5473 it->base_level_stop = 0;
5474
5475 /* Set up the bidi iterator for this overlay string. */
5476 if (it->bidi_p)
5477 {
5478 it->bidi_it.string.lstring = it->string;
5479 it->bidi_it.string.s = NULL;
5480 it->bidi_it.string.schars = SCHARS (it->string);
5481 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5482 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5483 it->bidi_it.string.unibyte = !it->multibyte_p;
5484 it->bidi_it.w = it->w;
5485 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5486 }
5487 }
5488
5489 CHECK_IT (it);
5490 }
5491
5492
5493 /* Compare two overlay_entry structures E1 and E2. Used as a
5494 comparison function for qsort in load_overlay_strings. Overlay
5495 strings for the same position are sorted so that
5496
5497 1. All after-strings come in front of before-strings, except
5498 when they come from the same overlay.
5499
5500 2. Within after-strings, strings are sorted so that overlay strings
5501 from overlays with higher priorities come first.
5502
5503 2. Within before-strings, strings are sorted so that overlay
5504 strings from overlays with higher priorities come last.
5505
5506 Value is analogous to strcmp. */
5507
5508
5509 static int
5510 compare_overlay_entries (const void *e1, const void *e2)
5511 {
5512 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5513 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5514 int result;
5515
5516 if (entry1->after_string_p != entry2->after_string_p)
5517 {
5518 /* Let after-strings appear in front of before-strings if
5519 they come from different overlays. */
5520 if (EQ (entry1->overlay, entry2->overlay))
5521 result = entry1->after_string_p ? 1 : -1;
5522 else
5523 result = entry1->after_string_p ? -1 : 1;
5524 }
5525 else if (entry1->priority != entry2->priority)
5526 {
5527 if (entry1->after_string_p)
5528 /* After-strings sorted in order of decreasing priority. */
5529 result = entry2->priority < entry1->priority ? -1 : 1;
5530 else
5531 /* Before-strings sorted in order of increasing priority. */
5532 result = entry1->priority < entry2->priority ? -1 : 1;
5533 }
5534 else
5535 result = 0;
5536
5537 return result;
5538 }
5539
5540
5541 /* Load the vector IT->overlay_strings with overlay strings from IT's
5542 current buffer position, or from CHARPOS if that is > 0. Set
5543 IT->n_overlays to the total number of overlay strings found.
5544
5545 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5546 a time. On entry into load_overlay_strings,
5547 IT->current.overlay_string_index gives the number of overlay
5548 strings that have already been loaded by previous calls to this
5549 function.
5550
5551 IT->add_overlay_start contains an additional overlay start
5552 position to consider for taking overlay strings from, if non-zero.
5553 This position comes into play when the overlay has an `invisible'
5554 property, and both before and after-strings. When we've skipped to
5555 the end of the overlay, because of its `invisible' property, we
5556 nevertheless want its before-string to appear.
5557 IT->add_overlay_start will contain the overlay start position
5558 in this case.
5559
5560 Overlay strings are sorted so that after-string strings come in
5561 front of before-string strings. Within before and after-strings,
5562 strings are sorted by overlay priority. See also function
5563 compare_overlay_entries. */
5564
5565 static void
5566 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5567 {
5568 Lisp_Object overlay, window, str, invisible;
5569 struct Lisp_Overlay *ov;
5570 ptrdiff_t start, end;
5571 ptrdiff_t size = 20;
5572 ptrdiff_t n = 0, i, j;
5573 int invis_p;
5574 struct overlay_entry *entries = alloca (size * sizeof *entries);
5575 USE_SAFE_ALLOCA;
5576
5577 if (charpos <= 0)
5578 charpos = IT_CHARPOS (*it);
5579
5580 /* Append the overlay string STRING of overlay OVERLAY to vector
5581 `entries' which has size `size' and currently contains `n'
5582 elements. AFTER_P non-zero means STRING is an after-string of
5583 OVERLAY. */
5584 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5585 do \
5586 { \
5587 Lisp_Object priority; \
5588 \
5589 if (n == size) \
5590 { \
5591 struct overlay_entry *old = entries; \
5592 SAFE_NALLOCA (entries, 2, size); \
5593 memcpy (entries, old, size * sizeof *entries); \
5594 size *= 2; \
5595 } \
5596 \
5597 entries[n].string = (STRING); \
5598 entries[n].overlay = (OVERLAY); \
5599 priority = Foverlay_get ((OVERLAY), Qpriority); \
5600 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5601 entries[n].after_string_p = (AFTER_P); \
5602 ++n; \
5603 } \
5604 while (0)
5605
5606 /* Process overlay before the overlay center. */
5607 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5608 {
5609 XSETMISC (overlay, ov);
5610 eassert (OVERLAYP (overlay));
5611 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5612 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5613
5614 if (end < charpos)
5615 break;
5616
5617 /* Skip this overlay if it doesn't start or end at IT's current
5618 position. */
5619 if (end != charpos && start != charpos)
5620 continue;
5621
5622 /* Skip this overlay if it doesn't apply to IT->w. */
5623 window = Foverlay_get (overlay, Qwindow);
5624 if (WINDOWP (window) && XWINDOW (window) != it->w)
5625 continue;
5626
5627 /* If the text ``under'' the overlay is invisible, both before-
5628 and after-strings from this overlay are visible; start and
5629 end position are indistinguishable. */
5630 invisible = Foverlay_get (overlay, Qinvisible);
5631 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5632
5633 /* If overlay has a non-empty before-string, record it. */
5634 if ((start == charpos || (end == charpos && invis_p))
5635 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5636 && SCHARS (str))
5637 RECORD_OVERLAY_STRING (overlay, str, 0);
5638
5639 /* If overlay has a non-empty after-string, record it. */
5640 if ((end == charpos || (start == charpos && invis_p))
5641 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5642 && SCHARS (str))
5643 RECORD_OVERLAY_STRING (overlay, str, 1);
5644 }
5645
5646 /* Process overlays after the overlay center. */
5647 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5648 {
5649 XSETMISC (overlay, ov);
5650 eassert (OVERLAYP (overlay));
5651 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5652 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5653
5654 if (start > charpos)
5655 break;
5656
5657 /* Skip this overlay if it doesn't start or end at IT's current
5658 position. */
5659 if (end != charpos && start != charpos)
5660 continue;
5661
5662 /* Skip this overlay if it doesn't apply to IT->w. */
5663 window = Foverlay_get (overlay, Qwindow);
5664 if (WINDOWP (window) && XWINDOW (window) != it->w)
5665 continue;
5666
5667 /* If the text ``under'' the overlay is invisible, it has a zero
5668 dimension, and both before- and after-strings apply. */
5669 invisible = Foverlay_get (overlay, Qinvisible);
5670 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5671
5672 /* If overlay has a non-empty before-string, record it. */
5673 if ((start == charpos || (end == charpos && invis_p))
5674 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5675 && SCHARS (str))
5676 RECORD_OVERLAY_STRING (overlay, str, 0);
5677
5678 /* If overlay has a non-empty after-string, record it. */
5679 if ((end == charpos || (start == charpos && invis_p))
5680 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5681 && SCHARS (str))
5682 RECORD_OVERLAY_STRING (overlay, str, 1);
5683 }
5684
5685 #undef RECORD_OVERLAY_STRING
5686
5687 /* Sort entries. */
5688 if (n > 1)
5689 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5690
5691 /* Record number of overlay strings, and where we computed it. */
5692 it->n_overlay_strings = n;
5693 it->overlay_strings_charpos = charpos;
5694
5695 /* IT->current.overlay_string_index is the number of overlay strings
5696 that have already been consumed by IT. Copy some of the
5697 remaining overlay strings to IT->overlay_strings. */
5698 i = 0;
5699 j = it->current.overlay_string_index;
5700 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5701 {
5702 it->overlay_strings[i] = entries[j].string;
5703 it->string_overlays[i++] = entries[j++].overlay;
5704 }
5705
5706 CHECK_IT (it);
5707 SAFE_FREE ();
5708 }
5709
5710
5711 /* Get the first chunk of overlay strings at IT's current buffer
5712 position, or at CHARPOS if that is > 0. Value is non-zero if at
5713 least one overlay string was found. */
5714
5715 static int
5716 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5717 {
5718 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5719 process. This fills IT->overlay_strings with strings, and sets
5720 IT->n_overlay_strings to the total number of strings to process.
5721 IT->pos.overlay_string_index has to be set temporarily to zero
5722 because load_overlay_strings needs this; it must be set to -1
5723 when no overlay strings are found because a zero value would
5724 indicate a position in the first overlay string. */
5725 it->current.overlay_string_index = 0;
5726 load_overlay_strings (it, charpos);
5727
5728 /* If we found overlay strings, set up IT to deliver display
5729 elements from the first one. Otherwise set up IT to deliver
5730 from current_buffer. */
5731 if (it->n_overlay_strings)
5732 {
5733 /* Make sure we know settings in current_buffer, so that we can
5734 restore meaningful values when we're done with the overlay
5735 strings. */
5736 if (compute_stop_p)
5737 compute_stop_pos (it);
5738 eassert (it->face_id >= 0);
5739
5740 /* Save IT's settings. They are restored after all overlay
5741 strings have been processed. */
5742 eassert (!compute_stop_p || it->sp == 0);
5743
5744 /* When called from handle_stop, there might be an empty display
5745 string loaded. In that case, don't bother saving it. But
5746 don't use this optimization with the bidi iterator, since we
5747 need the corresponding pop_it call to resync the bidi
5748 iterator's position with IT's position, after we are done
5749 with the overlay strings. (The corresponding call to pop_it
5750 in case of an empty display string is in
5751 next_overlay_string.) */
5752 if (!(!it->bidi_p
5753 && STRINGP (it->string) && !SCHARS (it->string)))
5754 push_it (it, NULL);
5755
5756 /* Set up IT to deliver display elements from the first overlay
5757 string. */
5758 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5759 it->string = it->overlay_strings[0];
5760 it->from_overlay = Qnil;
5761 it->stop_charpos = 0;
5762 eassert (STRINGP (it->string));
5763 it->end_charpos = SCHARS (it->string);
5764 it->prev_stop = 0;
5765 it->base_level_stop = 0;
5766 it->multibyte_p = STRING_MULTIBYTE (it->string);
5767 it->method = GET_FROM_STRING;
5768 it->from_disp_prop_p = 0;
5769
5770 /* Force paragraph direction to be that of the parent
5771 buffer. */
5772 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5773 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5774 else
5775 it->paragraph_embedding = L2R;
5776
5777 /* Set up the bidi iterator for this overlay string. */
5778 if (it->bidi_p)
5779 {
5780 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5781
5782 it->bidi_it.string.lstring = it->string;
5783 it->bidi_it.string.s = NULL;
5784 it->bidi_it.string.schars = SCHARS (it->string);
5785 it->bidi_it.string.bufpos = pos;
5786 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5787 it->bidi_it.string.unibyte = !it->multibyte_p;
5788 it->bidi_it.w = it->w;
5789 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5790 }
5791 return 1;
5792 }
5793
5794 it->current.overlay_string_index = -1;
5795 return 0;
5796 }
5797
5798 static int
5799 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5800 {
5801 it->string = Qnil;
5802 it->method = GET_FROM_BUFFER;
5803
5804 (void) get_overlay_strings_1 (it, charpos, 1);
5805
5806 CHECK_IT (it);
5807
5808 /* Value is non-zero if we found at least one overlay string. */
5809 return STRINGP (it->string);
5810 }
5811
5812
5813 \f
5814 /***********************************************************************
5815 Saving and restoring state
5816 ***********************************************************************/
5817
5818 /* Save current settings of IT on IT->stack. Called, for example,
5819 before setting up IT for an overlay string, to be able to restore
5820 IT's settings to what they were after the overlay string has been
5821 processed. If POSITION is non-NULL, it is the position to save on
5822 the stack instead of IT->position. */
5823
5824 static void
5825 push_it (struct it *it, struct text_pos *position)
5826 {
5827 struct iterator_stack_entry *p;
5828
5829 eassert (it->sp < IT_STACK_SIZE);
5830 p = it->stack + it->sp;
5831
5832 p->stop_charpos = it->stop_charpos;
5833 p->prev_stop = it->prev_stop;
5834 p->base_level_stop = it->base_level_stop;
5835 p->cmp_it = it->cmp_it;
5836 eassert (it->face_id >= 0);
5837 p->face_id = it->face_id;
5838 p->string = it->string;
5839 p->method = it->method;
5840 p->from_overlay = it->from_overlay;
5841 switch (p->method)
5842 {
5843 case GET_FROM_IMAGE:
5844 p->u.image.object = it->object;
5845 p->u.image.image_id = it->image_id;
5846 p->u.image.slice = it->slice;
5847 break;
5848 case GET_FROM_STRETCH:
5849 p->u.stretch.object = it->object;
5850 break;
5851 }
5852 p->position = position ? *position : it->position;
5853 p->current = it->current;
5854 p->end_charpos = it->end_charpos;
5855 p->string_nchars = it->string_nchars;
5856 p->area = it->area;
5857 p->multibyte_p = it->multibyte_p;
5858 p->avoid_cursor_p = it->avoid_cursor_p;
5859 p->space_width = it->space_width;
5860 p->font_height = it->font_height;
5861 p->voffset = it->voffset;
5862 p->string_from_display_prop_p = it->string_from_display_prop_p;
5863 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5864 p->display_ellipsis_p = 0;
5865 p->line_wrap = it->line_wrap;
5866 p->bidi_p = it->bidi_p;
5867 p->paragraph_embedding = it->paragraph_embedding;
5868 p->from_disp_prop_p = it->from_disp_prop_p;
5869 ++it->sp;
5870
5871 /* Save the state of the bidi iterator as well. */
5872 if (it->bidi_p)
5873 bidi_push_it (&it->bidi_it);
5874 }
5875
5876 static void
5877 iterate_out_of_display_property (struct it *it)
5878 {
5879 int buffer_p = !STRINGP (it->string);
5880 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5881 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5882
5883 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5884
5885 /* Maybe initialize paragraph direction. If we are at the beginning
5886 of a new paragraph, next_element_from_buffer may not have a
5887 chance to do that. */
5888 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5889 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5890 /* prev_stop can be zero, so check against BEGV as well. */
5891 while (it->bidi_it.charpos >= bob
5892 && it->prev_stop <= it->bidi_it.charpos
5893 && it->bidi_it.charpos < CHARPOS (it->position)
5894 && it->bidi_it.charpos < eob)
5895 bidi_move_to_visually_next (&it->bidi_it);
5896 /* Record the stop_pos we just crossed, for when we cross it
5897 back, maybe. */
5898 if (it->bidi_it.charpos > CHARPOS (it->position))
5899 it->prev_stop = CHARPOS (it->position);
5900 /* If we ended up not where pop_it put us, resync IT's
5901 positional members with the bidi iterator. */
5902 if (it->bidi_it.charpos != CHARPOS (it->position))
5903 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5904 if (buffer_p)
5905 it->current.pos = it->position;
5906 else
5907 it->current.string_pos = it->position;
5908 }
5909
5910 /* Restore IT's settings from IT->stack. Called, for example, when no
5911 more overlay strings must be processed, and we return to delivering
5912 display elements from a buffer, or when the end of a string from a
5913 `display' property is reached and we return to delivering display
5914 elements from an overlay string, or from a buffer. */
5915
5916 static void
5917 pop_it (struct it *it)
5918 {
5919 struct iterator_stack_entry *p;
5920 int from_display_prop = it->from_disp_prop_p;
5921
5922 eassert (it->sp > 0);
5923 --it->sp;
5924 p = it->stack + it->sp;
5925 it->stop_charpos = p->stop_charpos;
5926 it->prev_stop = p->prev_stop;
5927 it->base_level_stop = p->base_level_stop;
5928 it->cmp_it = p->cmp_it;
5929 it->face_id = p->face_id;
5930 it->current = p->current;
5931 it->position = p->position;
5932 it->string = p->string;
5933 it->from_overlay = p->from_overlay;
5934 if (NILP (it->string))
5935 SET_TEXT_POS (it->current.string_pos, -1, -1);
5936 it->method = p->method;
5937 switch (it->method)
5938 {
5939 case GET_FROM_IMAGE:
5940 it->image_id = p->u.image.image_id;
5941 it->object = p->u.image.object;
5942 it->slice = p->u.image.slice;
5943 break;
5944 case GET_FROM_STRETCH:
5945 it->object = p->u.stretch.object;
5946 break;
5947 case GET_FROM_BUFFER:
5948 it->object = it->w->contents;
5949 break;
5950 case GET_FROM_STRING:
5951 it->object = it->string;
5952 break;
5953 case GET_FROM_DISPLAY_VECTOR:
5954 if (it->s)
5955 it->method = GET_FROM_C_STRING;
5956 else if (STRINGP (it->string))
5957 it->method = GET_FROM_STRING;
5958 else
5959 {
5960 it->method = GET_FROM_BUFFER;
5961 it->object = it->w->contents;
5962 }
5963 }
5964 it->end_charpos = p->end_charpos;
5965 it->string_nchars = p->string_nchars;
5966 it->area = p->area;
5967 it->multibyte_p = p->multibyte_p;
5968 it->avoid_cursor_p = p->avoid_cursor_p;
5969 it->space_width = p->space_width;
5970 it->font_height = p->font_height;
5971 it->voffset = p->voffset;
5972 it->string_from_display_prop_p = p->string_from_display_prop_p;
5973 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5974 it->line_wrap = p->line_wrap;
5975 it->bidi_p = p->bidi_p;
5976 it->paragraph_embedding = p->paragraph_embedding;
5977 it->from_disp_prop_p = p->from_disp_prop_p;
5978 if (it->bidi_p)
5979 {
5980 bidi_pop_it (&it->bidi_it);
5981 /* Bidi-iterate until we get out of the portion of text, if any,
5982 covered by a `display' text property or by an overlay with
5983 `display' property. (We cannot just jump there, because the
5984 internal coherency of the bidi iterator state can not be
5985 preserved across such jumps.) We also must determine the
5986 paragraph base direction if the overlay we just processed is
5987 at the beginning of a new paragraph. */
5988 if (from_display_prop
5989 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5990 iterate_out_of_display_property (it);
5991
5992 eassert ((BUFFERP (it->object)
5993 && IT_CHARPOS (*it) == it->bidi_it.charpos
5994 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5995 || (STRINGP (it->object)
5996 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5997 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5998 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5999 }
6000 }
6001
6002
6003 \f
6004 /***********************************************************************
6005 Moving over lines
6006 ***********************************************************************/
6007
6008 /* Set IT's current position to the previous line start. */
6009
6010 static void
6011 back_to_previous_line_start (struct it *it)
6012 {
6013 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
6014
6015 DEC_BOTH (cp, bp);
6016 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
6017 }
6018
6019
6020 /* Move IT to the next line start.
6021
6022 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
6023 we skipped over part of the text (as opposed to moving the iterator
6024 continuously over the text). Otherwise, don't change the value
6025 of *SKIPPED_P.
6026
6027 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
6028 iterator on the newline, if it was found.
6029
6030 Newlines may come from buffer text, overlay strings, or strings
6031 displayed via the `display' property. That's the reason we can't
6032 simply use find_newline_no_quit.
6033
6034 Note that this function may not skip over invisible text that is so
6035 because of text properties and immediately follows a newline. If
6036 it would, function reseat_at_next_visible_line_start, when called
6037 from set_iterator_to_next, would effectively make invisible
6038 characters following a newline part of the wrong glyph row, which
6039 leads to wrong cursor motion. */
6040
6041 static int
6042 forward_to_next_line_start (struct it *it, int *skipped_p,
6043 struct bidi_it *bidi_it_prev)
6044 {
6045 ptrdiff_t old_selective;
6046 int newline_found_p, n;
6047 const int MAX_NEWLINE_DISTANCE = 500;
6048
6049 /* If already on a newline, just consume it to avoid unintended
6050 skipping over invisible text below. */
6051 if (it->what == IT_CHARACTER
6052 && it->c == '\n'
6053 && CHARPOS (it->position) == IT_CHARPOS (*it))
6054 {
6055 if (it->bidi_p && bidi_it_prev)
6056 *bidi_it_prev = it->bidi_it;
6057 set_iterator_to_next (it, 0);
6058 it->c = 0;
6059 return 1;
6060 }
6061
6062 /* Don't handle selective display in the following. It's (a)
6063 unnecessary because it's done by the caller, and (b) leads to an
6064 infinite recursion because next_element_from_ellipsis indirectly
6065 calls this function. */
6066 old_selective = it->selective;
6067 it->selective = 0;
6068
6069 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6070 from buffer text. */
6071 for (n = newline_found_p = 0;
6072 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6073 n += STRINGP (it->string) ? 0 : 1)
6074 {
6075 if (!get_next_display_element (it))
6076 return 0;
6077 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6078 if (newline_found_p && it->bidi_p && bidi_it_prev)
6079 *bidi_it_prev = it->bidi_it;
6080 set_iterator_to_next (it, 0);
6081 }
6082
6083 /* If we didn't find a newline near enough, see if we can use a
6084 short-cut. */
6085 if (!newline_found_p)
6086 {
6087 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6088 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6089 1, &bytepos);
6090 Lisp_Object pos;
6091
6092 eassert (!STRINGP (it->string));
6093
6094 /* If there isn't any `display' property in sight, and no
6095 overlays, we can just use the position of the newline in
6096 buffer text. */
6097 if (it->stop_charpos >= limit
6098 || ((pos = Fnext_single_property_change (make_number (start),
6099 Qdisplay, Qnil,
6100 make_number (limit)),
6101 NILP (pos))
6102 && next_overlay_change (start) == ZV))
6103 {
6104 if (!it->bidi_p)
6105 {
6106 IT_CHARPOS (*it) = limit;
6107 IT_BYTEPOS (*it) = bytepos;
6108 }
6109 else
6110 {
6111 struct bidi_it bprev;
6112
6113 /* Help bidi.c avoid expensive searches for display
6114 properties and overlays, by telling it that there are
6115 none up to `limit'. */
6116 if (it->bidi_it.disp_pos < limit)
6117 {
6118 it->bidi_it.disp_pos = limit;
6119 it->bidi_it.disp_prop = 0;
6120 }
6121 do {
6122 bprev = it->bidi_it;
6123 bidi_move_to_visually_next (&it->bidi_it);
6124 } while (it->bidi_it.charpos != limit);
6125 IT_CHARPOS (*it) = limit;
6126 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6127 if (bidi_it_prev)
6128 *bidi_it_prev = bprev;
6129 }
6130 *skipped_p = newline_found_p = 1;
6131 }
6132 else
6133 {
6134 while (get_next_display_element (it)
6135 && !newline_found_p)
6136 {
6137 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6138 if (newline_found_p && it->bidi_p && bidi_it_prev)
6139 *bidi_it_prev = it->bidi_it;
6140 set_iterator_to_next (it, 0);
6141 }
6142 }
6143 }
6144
6145 it->selective = old_selective;
6146 return newline_found_p;
6147 }
6148
6149
6150 /* Set IT's current position to the previous visible line start. Skip
6151 invisible text that is so either due to text properties or due to
6152 selective display. Caution: this does not change IT->current_x and
6153 IT->hpos. */
6154
6155 static void
6156 back_to_previous_visible_line_start (struct it *it)
6157 {
6158 while (IT_CHARPOS (*it) > BEGV)
6159 {
6160 back_to_previous_line_start (it);
6161
6162 if (IT_CHARPOS (*it) <= BEGV)
6163 break;
6164
6165 /* If selective > 0, then lines indented more than its value are
6166 invisible. */
6167 if (it->selective > 0
6168 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6169 it->selective))
6170 continue;
6171
6172 /* Check the newline before point for invisibility. */
6173 {
6174 Lisp_Object prop;
6175 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6176 Qinvisible, it->window);
6177 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6178 continue;
6179 }
6180
6181 if (IT_CHARPOS (*it) <= BEGV)
6182 break;
6183
6184 {
6185 struct it it2;
6186 void *it2data = NULL;
6187 ptrdiff_t pos;
6188 ptrdiff_t beg, end;
6189 Lisp_Object val, overlay;
6190
6191 SAVE_IT (it2, *it, it2data);
6192
6193 /* If newline is part of a composition, continue from start of composition */
6194 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6195 && beg < IT_CHARPOS (*it))
6196 goto replaced;
6197
6198 /* If newline is replaced by a display property, find start of overlay
6199 or interval and continue search from that point. */
6200 pos = --IT_CHARPOS (it2);
6201 --IT_BYTEPOS (it2);
6202 it2.sp = 0;
6203 bidi_unshelve_cache (NULL, 0);
6204 it2.string_from_display_prop_p = 0;
6205 it2.from_disp_prop_p = 0;
6206 if (handle_display_prop (&it2) == HANDLED_RETURN
6207 && !NILP (val = get_char_property_and_overlay
6208 (make_number (pos), Qdisplay, Qnil, &overlay))
6209 && (OVERLAYP (overlay)
6210 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6211 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6212 {
6213 RESTORE_IT (it, it, it2data);
6214 goto replaced;
6215 }
6216
6217 /* Newline is not replaced by anything -- so we are done. */
6218 RESTORE_IT (it, it, it2data);
6219 break;
6220
6221 replaced:
6222 if (beg < BEGV)
6223 beg = BEGV;
6224 IT_CHARPOS (*it) = beg;
6225 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6226 }
6227 }
6228
6229 it->continuation_lines_width = 0;
6230
6231 eassert (IT_CHARPOS (*it) >= BEGV);
6232 eassert (IT_CHARPOS (*it) == BEGV
6233 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6234 CHECK_IT (it);
6235 }
6236
6237
6238 /* Reseat iterator IT at the previous visible line start. Skip
6239 invisible text that is so either due to text properties or due to
6240 selective display. At the end, update IT's overlay information,
6241 face information etc. */
6242
6243 void
6244 reseat_at_previous_visible_line_start (struct it *it)
6245 {
6246 back_to_previous_visible_line_start (it);
6247 reseat (it, it->current.pos, 1);
6248 CHECK_IT (it);
6249 }
6250
6251
6252 /* Reseat iterator IT on the next visible line start in the current
6253 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6254 preceding the line start. Skip over invisible text that is so
6255 because of selective display. Compute faces, overlays etc at the
6256 new position. Note that this function does not skip over text that
6257 is invisible because of text properties. */
6258
6259 static void
6260 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6261 {
6262 int newline_found_p, skipped_p = 0;
6263 struct bidi_it bidi_it_prev;
6264
6265 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6266
6267 /* Skip over lines that are invisible because they are indented
6268 more than the value of IT->selective. */
6269 if (it->selective > 0)
6270 while (IT_CHARPOS (*it) < ZV
6271 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6272 it->selective))
6273 {
6274 eassert (IT_BYTEPOS (*it) == BEGV
6275 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6276 newline_found_p =
6277 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6278 }
6279
6280 /* Position on the newline if that's what's requested. */
6281 if (on_newline_p && newline_found_p)
6282 {
6283 if (STRINGP (it->string))
6284 {
6285 if (IT_STRING_CHARPOS (*it) > 0)
6286 {
6287 if (!it->bidi_p)
6288 {
6289 --IT_STRING_CHARPOS (*it);
6290 --IT_STRING_BYTEPOS (*it);
6291 }
6292 else
6293 {
6294 /* We need to restore the bidi iterator to the state
6295 it had on the newline, and resync the IT's
6296 position with that. */
6297 it->bidi_it = bidi_it_prev;
6298 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6299 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6300 }
6301 }
6302 }
6303 else if (IT_CHARPOS (*it) > BEGV)
6304 {
6305 if (!it->bidi_p)
6306 {
6307 --IT_CHARPOS (*it);
6308 --IT_BYTEPOS (*it);
6309 }
6310 else
6311 {
6312 /* We need to restore the bidi iterator to the state it
6313 had on the newline and resync IT with that. */
6314 it->bidi_it = bidi_it_prev;
6315 IT_CHARPOS (*it) = it->bidi_it.charpos;
6316 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6317 }
6318 reseat (it, it->current.pos, 0);
6319 }
6320 }
6321 else if (skipped_p)
6322 reseat (it, it->current.pos, 0);
6323
6324 CHECK_IT (it);
6325 }
6326
6327
6328 \f
6329 /***********************************************************************
6330 Changing an iterator's position
6331 ***********************************************************************/
6332
6333 /* Change IT's current position to POS in current_buffer. If FORCE_P
6334 is non-zero, always check for text properties at the new position.
6335 Otherwise, text properties are only looked up if POS >=
6336 IT->check_charpos of a property. */
6337
6338 static void
6339 reseat (struct it *it, struct text_pos pos, int force_p)
6340 {
6341 ptrdiff_t original_pos = IT_CHARPOS (*it);
6342
6343 reseat_1 (it, pos, 0);
6344
6345 /* Determine where to check text properties. Avoid doing it
6346 where possible because text property lookup is very expensive. */
6347 if (force_p
6348 || CHARPOS (pos) > it->stop_charpos
6349 || CHARPOS (pos) < original_pos)
6350 {
6351 if (it->bidi_p)
6352 {
6353 /* For bidi iteration, we need to prime prev_stop and
6354 base_level_stop with our best estimations. */
6355 /* Implementation note: Of course, POS is not necessarily a
6356 stop position, so assigning prev_pos to it is a lie; we
6357 should have called compute_stop_backwards. However, if
6358 the current buffer does not include any R2L characters,
6359 that call would be a waste of cycles, because the
6360 iterator will never move back, and thus never cross this
6361 "fake" stop position. So we delay that backward search
6362 until the time we really need it, in next_element_from_buffer. */
6363 if (CHARPOS (pos) != it->prev_stop)
6364 it->prev_stop = CHARPOS (pos);
6365 if (CHARPOS (pos) < it->base_level_stop)
6366 it->base_level_stop = 0; /* meaning it's unknown */
6367 handle_stop (it);
6368 }
6369 else
6370 {
6371 handle_stop (it);
6372 it->prev_stop = it->base_level_stop = 0;
6373 }
6374
6375 }
6376
6377 CHECK_IT (it);
6378 }
6379
6380
6381 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6382 IT->stop_pos to POS, also. */
6383
6384 static void
6385 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6386 {
6387 /* Don't call this function when scanning a C string. */
6388 eassert (it->s == NULL);
6389
6390 /* POS must be a reasonable value. */
6391 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6392
6393 it->current.pos = it->position = pos;
6394 it->end_charpos = ZV;
6395 it->dpvec = NULL;
6396 it->current.dpvec_index = -1;
6397 it->current.overlay_string_index = -1;
6398 IT_STRING_CHARPOS (*it) = -1;
6399 IT_STRING_BYTEPOS (*it) = -1;
6400 it->string = Qnil;
6401 it->method = GET_FROM_BUFFER;
6402 it->object = it->w->contents;
6403 it->area = TEXT_AREA;
6404 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6405 it->sp = 0;
6406 it->string_from_display_prop_p = 0;
6407 it->string_from_prefix_prop_p = 0;
6408
6409 it->from_disp_prop_p = 0;
6410 it->face_before_selective_p = 0;
6411 if (it->bidi_p)
6412 {
6413 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6414 &it->bidi_it);
6415 bidi_unshelve_cache (NULL, 0);
6416 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6417 it->bidi_it.string.s = NULL;
6418 it->bidi_it.string.lstring = Qnil;
6419 it->bidi_it.string.bufpos = 0;
6420 it->bidi_it.string.unibyte = 0;
6421 it->bidi_it.w = it->w;
6422 }
6423
6424 if (set_stop_p)
6425 {
6426 it->stop_charpos = CHARPOS (pos);
6427 it->base_level_stop = CHARPOS (pos);
6428 }
6429 /* This make the information stored in it->cmp_it invalidate. */
6430 it->cmp_it.id = -1;
6431 }
6432
6433
6434 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6435 If S is non-null, it is a C string to iterate over. Otherwise,
6436 STRING gives a Lisp string to iterate over.
6437
6438 If PRECISION > 0, don't return more then PRECISION number of
6439 characters from the string.
6440
6441 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6442 characters have been returned. FIELD_WIDTH < 0 means an infinite
6443 field width.
6444
6445 MULTIBYTE = 0 means disable processing of multibyte characters,
6446 MULTIBYTE > 0 means enable it,
6447 MULTIBYTE < 0 means use IT->multibyte_p.
6448
6449 IT must be initialized via a prior call to init_iterator before
6450 calling this function. */
6451
6452 static void
6453 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6454 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6455 int multibyte)
6456 {
6457 /* No region in strings. */
6458 it->region_beg_charpos = it->region_end_charpos = -1;
6459
6460 /* No text property checks performed by default, but see below. */
6461 it->stop_charpos = -1;
6462
6463 /* Set iterator position and end position. */
6464 memset (&it->current, 0, sizeof it->current);
6465 it->current.overlay_string_index = -1;
6466 it->current.dpvec_index = -1;
6467 eassert (charpos >= 0);
6468
6469 /* If STRING is specified, use its multibyteness, otherwise use the
6470 setting of MULTIBYTE, if specified. */
6471 if (multibyte >= 0)
6472 it->multibyte_p = multibyte > 0;
6473
6474 /* Bidirectional reordering of strings is controlled by the default
6475 value of bidi-display-reordering. Don't try to reorder while
6476 loading loadup.el, as the necessary character property tables are
6477 not yet available. */
6478 it->bidi_p =
6479 NILP (Vpurify_flag)
6480 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6481
6482 if (s == NULL)
6483 {
6484 eassert (STRINGP (string));
6485 it->string = string;
6486 it->s = NULL;
6487 it->end_charpos = it->string_nchars = SCHARS (string);
6488 it->method = GET_FROM_STRING;
6489 it->current.string_pos = string_pos (charpos, string);
6490
6491 if (it->bidi_p)
6492 {
6493 it->bidi_it.string.lstring = string;
6494 it->bidi_it.string.s = NULL;
6495 it->bidi_it.string.schars = it->end_charpos;
6496 it->bidi_it.string.bufpos = 0;
6497 it->bidi_it.string.from_disp_str = 0;
6498 it->bidi_it.string.unibyte = !it->multibyte_p;
6499 it->bidi_it.w = it->w;
6500 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6501 FRAME_WINDOW_P (it->f), &it->bidi_it);
6502 }
6503 }
6504 else
6505 {
6506 it->s = (const unsigned char *) s;
6507 it->string = Qnil;
6508
6509 /* Note that we use IT->current.pos, not it->current.string_pos,
6510 for displaying C strings. */
6511 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6512 if (it->multibyte_p)
6513 {
6514 it->current.pos = c_string_pos (charpos, s, 1);
6515 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6516 }
6517 else
6518 {
6519 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6520 it->end_charpos = it->string_nchars = strlen (s);
6521 }
6522
6523 if (it->bidi_p)
6524 {
6525 it->bidi_it.string.lstring = Qnil;
6526 it->bidi_it.string.s = (const unsigned char *) s;
6527 it->bidi_it.string.schars = it->end_charpos;
6528 it->bidi_it.string.bufpos = 0;
6529 it->bidi_it.string.from_disp_str = 0;
6530 it->bidi_it.string.unibyte = !it->multibyte_p;
6531 it->bidi_it.w = it->w;
6532 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6533 &it->bidi_it);
6534 }
6535 it->method = GET_FROM_C_STRING;
6536 }
6537
6538 /* PRECISION > 0 means don't return more than PRECISION characters
6539 from the string. */
6540 if (precision > 0 && it->end_charpos - charpos > precision)
6541 {
6542 it->end_charpos = it->string_nchars = charpos + precision;
6543 if (it->bidi_p)
6544 it->bidi_it.string.schars = it->end_charpos;
6545 }
6546
6547 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6548 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6549 FIELD_WIDTH < 0 means infinite field width. This is useful for
6550 padding with `-' at the end of a mode line. */
6551 if (field_width < 0)
6552 field_width = INFINITY;
6553 /* Implementation note: We deliberately don't enlarge
6554 it->bidi_it.string.schars here to fit it->end_charpos, because
6555 the bidi iterator cannot produce characters out of thin air. */
6556 if (field_width > it->end_charpos - charpos)
6557 it->end_charpos = charpos + field_width;
6558
6559 /* Use the standard display table for displaying strings. */
6560 if (DISP_TABLE_P (Vstandard_display_table))
6561 it->dp = XCHAR_TABLE (Vstandard_display_table);
6562
6563 it->stop_charpos = charpos;
6564 it->prev_stop = charpos;
6565 it->base_level_stop = 0;
6566 if (it->bidi_p)
6567 {
6568 it->bidi_it.first_elt = 1;
6569 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6570 it->bidi_it.disp_pos = -1;
6571 }
6572 if (s == NULL && it->multibyte_p)
6573 {
6574 ptrdiff_t endpos = SCHARS (it->string);
6575 if (endpos > it->end_charpos)
6576 endpos = it->end_charpos;
6577 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6578 it->string);
6579 }
6580 CHECK_IT (it);
6581 }
6582
6583
6584 \f
6585 /***********************************************************************
6586 Iteration
6587 ***********************************************************************/
6588
6589 /* Map enum it_method value to corresponding next_element_from_* function. */
6590
6591 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6592 {
6593 next_element_from_buffer,
6594 next_element_from_display_vector,
6595 next_element_from_string,
6596 next_element_from_c_string,
6597 next_element_from_image,
6598 next_element_from_stretch
6599 };
6600
6601 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6602
6603
6604 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6605 (possibly with the following characters). */
6606
6607 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6608 ((IT)->cmp_it.id >= 0 \
6609 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6610 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6611 END_CHARPOS, (IT)->w, \
6612 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6613 (IT)->string)))
6614
6615
6616 /* Lookup the char-table Vglyphless_char_display for character C (-1
6617 if we want information for no-font case), and return the display
6618 method symbol. By side-effect, update it->what and
6619 it->glyphless_method. This function is called from
6620 get_next_display_element for each character element, and from
6621 x_produce_glyphs when no suitable font was found. */
6622
6623 Lisp_Object
6624 lookup_glyphless_char_display (int c, struct it *it)
6625 {
6626 Lisp_Object glyphless_method = Qnil;
6627
6628 if (CHAR_TABLE_P (Vglyphless_char_display)
6629 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6630 {
6631 if (c >= 0)
6632 {
6633 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6634 if (CONSP (glyphless_method))
6635 glyphless_method = FRAME_WINDOW_P (it->f)
6636 ? XCAR (glyphless_method)
6637 : XCDR (glyphless_method);
6638 }
6639 else
6640 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6641 }
6642
6643 retry:
6644 if (NILP (glyphless_method))
6645 {
6646 if (c >= 0)
6647 /* The default is to display the character by a proper font. */
6648 return Qnil;
6649 /* The default for the no-font case is to display an empty box. */
6650 glyphless_method = Qempty_box;
6651 }
6652 if (EQ (glyphless_method, Qzero_width))
6653 {
6654 if (c >= 0)
6655 return glyphless_method;
6656 /* This method can't be used for the no-font case. */
6657 glyphless_method = Qempty_box;
6658 }
6659 if (EQ (glyphless_method, Qthin_space))
6660 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6661 else if (EQ (glyphless_method, Qempty_box))
6662 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6663 else if (EQ (glyphless_method, Qhex_code))
6664 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6665 else if (STRINGP (glyphless_method))
6666 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6667 else
6668 {
6669 /* Invalid value. We use the default method. */
6670 glyphless_method = Qnil;
6671 goto retry;
6672 }
6673 it->what = IT_GLYPHLESS;
6674 return glyphless_method;
6675 }
6676
6677 /* Load IT's display element fields with information about the next
6678 display element from the current position of IT. Value is zero if
6679 end of buffer (or C string) is reached. */
6680
6681 static struct frame *last_escape_glyph_frame = NULL;
6682 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6683 static int last_escape_glyph_merged_face_id = 0;
6684
6685 struct frame *last_glyphless_glyph_frame = NULL;
6686 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6687 int last_glyphless_glyph_merged_face_id = 0;
6688
6689 static int
6690 get_next_display_element (struct it *it)
6691 {
6692 /* Non-zero means that we found a display element. Zero means that
6693 we hit the end of what we iterate over. Performance note: the
6694 function pointer `method' used here turns out to be faster than
6695 using a sequence of if-statements. */
6696 int success_p;
6697
6698 get_next:
6699 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6700
6701 if (it->what == IT_CHARACTER)
6702 {
6703 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6704 and only if (a) the resolved directionality of that character
6705 is R..." */
6706 /* FIXME: Do we need an exception for characters from display
6707 tables? */
6708 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6709 it->c = bidi_mirror_char (it->c);
6710 /* Map via display table or translate control characters.
6711 IT->c, IT->len etc. have been set to the next character by
6712 the function call above. If we have a display table, and it
6713 contains an entry for IT->c, translate it. Don't do this if
6714 IT->c itself comes from a display table, otherwise we could
6715 end up in an infinite recursion. (An alternative could be to
6716 count the recursion depth of this function and signal an
6717 error when a certain maximum depth is reached.) Is it worth
6718 it? */
6719 if (success_p && it->dpvec == NULL)
6720 {
6721 Lisp_Object dv;
6722 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6723 int nonascii_space_p = 0;
6724 int nonascii_hyphen_p = 0;
6725 int c = it->c; /* This is the character to display. */
6726
6727 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6728 {
6729 eassert (SINGLE_BYTE_CHAR_P (c));
6730 if (unibyte_display_via_language_environment)
6731 {
6732 c = DECODE_CHAR (unibyte, c);
6733 if (c < 0)
6734 c = BYTE8_TO_CHAR (it->c);
6735 }
6736 else
6737 c = BYTE8_TO_CHAR (it->c);
6738 }
6739
6740 if (it->dp
6741 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6742 VECTORP (dv)))
6743 {
6744 struct Lisp_Vector *v = XVECTOR (dv);
6745
6746 /* Return the first character from the display table
6747 entry, if not empty. If empty, don't display the
6748 current character. */
6749 if (v->header.size)
6750 {
6751 it->dpvec_char_len = it->len;
6752 it->dpvec = v->contents;
6753 it->dpend = v->contents + v->header.size;
6754 it->current.dpvec_index = 0;
6755 it->dpvec_face_id = -1;
6756 it->saved_face_id = it->face_id;
6757 it->method = GET_FROM_DISPLAY_VECTOR;
6758 it->ellipsis_p = 0;
6759 }
6760 else
6761 {
6762 set_iterator_to_next (it, 0);
6763 }
6764 goto get_next;
6765 }
6766
6767 if (! NILP (lookup_glyphless_char_display (c, it)))
6768 {
6769 if (it->what == IT_GLYPHLESS)
6770 goto done;
6771 /* Don't display this character. */
6772 set_iterator_to_next (it, 0);
6773 goto get_next;
6774 }
6775
6776 /* If `nobreak-char-display' is non-nil, we display
6777 non-ASCII spaces and hyphens specially. */
6778 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6779 {
6780 if (c == 0xA0)
6781 nonascii_space_p = 1;
6782 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6783 nonascii_hyphen_p = 1;
6784 }
6785
6786 /* Translate control characters into `\003' or `^C' form.
6787 Control characters coming from a display table entry are
6788 currently not translated because we use IT->dpvec to hold
6789 the translation. This could easily be changed but I
6790 don't believe that it is worth doing.
6791
6792 The characters handled by `nobreak-char-display' must be
6793 translated too.
6794
6795 Non-printable characters and raw-byte characters are also
6796 translated to octal form. */
6797 if (((c < ' ' || c == 127) /* ASCII control chars */
6798 ? (it->area != TEXT_AREA
6799 /* In mode line, treat \n, \t like other crl chars. */
6800 || (c != '\t'
6801 && it->glyph_row
6802 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6803 || (c != '\n' && c != '\t'))
6804 : (nonascii_space_p
6805 || nonascii_hyphen_p
6806 || CHAR_BYTE8_P (c)
6807 || ! CHAR_PRINTABLE_P (c))))
6808 {
6809 /* C is a control character, non-ASCII space/hyphen,
6810 raw-byte, or a non-printable character which must be
6811 displayed either as '\003' or as `^C' where the '\\'
6812 and '^' can be defined in the display table. Fill
6813 IT->ctl_chars with glyphs for what we have to
6814 display. Then, set IT->dpvec to these glyphs. */
6815 Lisp_Object gc;
6816 int ctl_len;
6817 int face_id;
6818 int lface_id = 0;
6819 int escape_glyph;
6820
6821 /* Handle control characters with ^. */
6822
6823 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6824 {
6825 int g;
6826
6827 g = '^'; /* default glyph for Control */
6828 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6829 if (it->dp
6830 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6831 {
6832 g = GLYPH_CODE_CHAR (gc);
6833 lface_id = GLYPH_CODE_FACE (gc);
6834 }
6835 if (lface_id)
6836 {
6837 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6838 }
6839 else if (it->f == last_escape_glyph_frame
6840 && it->face_id == last_escape_glyph_face_id)
6841 {
6842 face_id = last_escape_glyph_merged_face_id;
6843 }
6844 else
6845 {
6846 /* Merge the escape-glyph face into the current face. */
6847 face_id = merge_faces (it->f, Qescape_glyph, 0,
6848 it->face_id);
6849 last_escape_glyph_frame = it->f;
6850 last_escape_glyph_face_id = it->face_id;
6851 last_escape_glyph_merged_face_id = face_id;
6852 }
6853
6854 XSETINT (it->ctl_chars[0], g);
6855 XSETINT (it->ctl_chars[1], c ^ 0100);
6856 ctl_len = 2;
6857 goto display_control;
6858 }
6859
6860 /* Handle non-ascii space in the mode where it only gets
6861 highlighting. */
6862
6863 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6864 {
6865 /* Merge `nobreak-space' into the current face. */
6866 face_id = merge_faces (it->f, Qnobreak_space, 0,
6867 it->face_id);
6868 XSETINT (it->ctl_chars[0], ' ');
6869 ctl_len = 1;
6870 goto display_control;
6871 }
6872
6873 /* Handle sequences that start with the "escape glyph". */
6874
6875 /* the default escape glyph is \. */
6876 escape_glyph = '\\';
6877
6878 if (it->dp
6879 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6880 {
6881 escape_glyph = GLYPH_CODE_CHAR (gc);
6882 lface_id = GLYPH_CODE_FACE (gc);
6883 }
6884 if (lface_id)
6885 {
6886 /* The display table specified a face.
6887 Merge it into face_id and also into escape_glyph. */
6888 face_id = merge_faces (it->f, Qt, lface_id,
6889 it->face_id);
6890 }
6891 else if (it->f == last_escape_glyph_frame
6892 && it->face_id == last_escape_glyph_face_id)
6893 {
6894 face_id = last_escape_glyph_merged_face_id;
6895 }
6896 else
6897 {
6898 /* Merge the escape-glyph face into the current face. */
6899 face_id = merge_faces (it->f, Qescape_glyph, 0,
6900 it->face_id);
6901 last_escape_glyph_frame = it->f;
6902 last_escape_glyph_face_id = it->face_id;
6903 last_escape_glyph_merged_face_id = face_id;
6904 }
6905
6906 /* Draw non-ASCII hyphen with just highlighting: */
6907
6908 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6909 {
6910 XSETINT (it->ctl_chars[0], '-');
6911 ctl_len = 1;
6912 goto display_control;
6913 }
6914
6915 /* Draw non-ASCII space/hyphen with escape glyph: */
6916
6917 if (nonascii_space_p || nonascii_hyphen_p)
6918 {
6919 XSETINT (it->ctl_chars[0], escape_glyph);
6920 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6921 ctl_len = 2;
6922 goto display_control;
6923 }
6924
6925 {
6926 char str[10];
6927 int len, i;
6928
6929 if (CHAR_BYTE8_P (c))
6930 /* Display \200 instead of \17777600. */
6931 c = CHAR_TO_BYTE8 (c);
6932 len = sprintf (str, "%03o", c);
6933
6934 XSETINT (it->ctl_chars[0], escape_glyph);
6935 for (i = 0; i < len; i++)
6936 XSETINT (it->ctl_chars[i + 1], str[i]);
6937 ctl_len = len + 1;
6938 }
6939
6940 display_control:
6941 /* Set up IT->dpvec and return first character from it. */
6942 it->dpvec_char_len = it->len;
6943 it->dpvec = it->ctl_chars;
6944 it->dpend = it->dpvec + ctl_len;
6945 it->current.dpvec_index = 0;
6946 it->dpvec_face_id = face_id;
6947 it->saved_face_id = it->face_id;
6948 it->method = GET_FROM_DISPLAY_VECTOR;
6949 it->ellipsis_p = 0;
6950 goto get_next;
6951 }
6952 it->char_to_display = c;
6953 }
6954 else if (success_p)
6955 {
6956 it->char_to_display = it->c;
6957 }
6958 }
6959
6960 /* Adjust face id for a multibyte character. There are no multibyte
6961 character in unibyte text. */
6962 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6963 && it->multibyte_p
6964 && success_p
6965 && FRAME_WINDOW_P (it->f))
6966 {
6967 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6968
6969 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6970 {
6971 /* Automatic composition with glyph-string. */
6972 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6973
6974 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6975 }
6976 else
6977 {
6978 ptrdiff_t pos = (it->s ? -1
6979 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6980 : IT_CHARPOS (*it));
6981 int c;
6982
6983 if (it->what == IT_CHARACTER)
6984 c = it->char_to_display;
6985 else
6986 {
6987 struct composition *cmp = composition_table[it->cmp_it.id];
6988 int i;
6989
6990 c = ' ';
6991 for (i = 0; i < cmp->glyph_len; i++)
6992 /* TAB in a composition means display glyphs with
6993 padding space on the left or right. */
6994 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6995 break;
6996 }
6997 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6998 }
6999 }
7000
7001 done:
7002 /* Is this character the last one of a run of characters with
7003 box? If yes, set IT->end_of_box_run_p to 1. */
7004 if (it->face_box_p
7005 && it->s == NULL)
7006 {
7007 if (it->method == GET_FROM_STRING && it->sp)
7008 {
7009 int face_id = underlying_face_id (it);
7010 struct face *face = FACE_FROM_ID (it->f, face_id);
7011
7012 if (face)
7013 {
7014 if (face->box == FACE_NO_BOX)
7015 {
7016 /* If the box comes from face properties in a
7017 display string, check faces in that string. */
7018 int string_face_id = face_after_it_pos (it);
7019 it->end_of_box_run_p
7020 = (FACE_FROM_ID (it->f, string_face_id)->box
7021 == FACE_NO_BOX);
7022 }
7023 /* Otherwise, the box comes from the underlying face.
7024 If this is the last string character displayed, check
7025 the next buffer location. */
7026 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
7027 && (it->current.overlay_string_index
7028 == it->n_overlay_strings - 1))
7029 {
7030 ptrdiff_t ignore;
7031 int next_face_id;
7032 struct text_pos pos = it->current.pos;
7033 INC_TEXT_POS (pos, it->multibyte_p);
7034
7035 next_face_id = face_at_buffer_position
7036 (it->w, CHARPOS (pos), it->region_beg_charpos,
7037 it->region_end_charpos, &ignore,
7038 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
7039 -1);
7040 it->end_of_box_run_p
7041 = (FACE_FROM_ID (it->f, next_face_id)->box
7042 == FACE_NO_BOX);
7043 }
7044 }
7045 }
7046 else
7047 {
7048 int face_id = face_after_it_pos (it);
7049 it->end_of_box_run_p
7050 = (face_id != it->face_id
7051 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7052 }
7053 }
7054 /* If we reached the end of the object we've been iterating (e.g., a
7055 display string or an overlay string), and there's something on
7056 IT->stack, proceed with what's on the stack. It doesn't make
7057 sense to return zero if there's unprocessed stuff on the stack,
7058 because otherwise that stuff will never be displayed. */
7059 if (!success_p && it->sp > 0)
7060 {
7061 set_iterator_to_next (it, 0);
7062 success_p = get_next_display_element (it);
7063 }
7064
7065 /* Value is 0 if end of buffer or string reached. */
7066 return success_p;
7067 }
7068
7069
7070 /* Move IT to the next display element.
7071
7072 RESEAT_P non-zero means if called on a newline in buffer text,
7073 skip to the next visible line start.
7074
7075 Functions get_next_display_element and set_iterator_to_next are
7076 separate because I find this arrangement easier to handle than a
7077 get_next_display_element function that also increments IT's
7078 position. The way it is we can first look at an iterator's current
7079 display element, decide whether it fits on a line, and if it does,
7080 increment the iterator position. The other way around we probably
7081 would either need a flag indicating whether the iterator has to be
7082 incremented the next time, or we would have to implement a
7083 decrement position function which would not be easy to write. */
7084
7085 void
7086 set_iterator_to_next (struct it *it, int reseat_p)
7087 {
7088 /* Reset flags indicating start and end of a sequence of characters
7089 with box. Reset them at the start of this function because
7090 moving the iterator to a new position might set them. */
7091 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7092
7093 switch (it->method)
7094 {
7095 case GET_FROM_BUFFER:
7096 /* The current display element of IT is a character from
7097 current_buffer. Advance in the buffer, and maybe skip over
7098 invisible lines that are so because of selective display. */
7099 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7100 reseat_at_next_visible_line_start (it, 0);
7101 else if (it->cmp_it.id >= 0)
7102 {
7103 /* We are currently getting glyphs from a composition. */
7104 int i;
7105
7106 if (! it->bidi_p)
7107 {
7108 IT_CHARPOS (*it) += it->cmp_it.nchars;
7109 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7110 if (it->cmp_it.to < it->cmp_it.nglyphs)
7111 {
7112 it->cmp_it.from = it->cmp_it.to;
7113 }
7114 else
7115 {
7116 it->cmp_it.id = -1;
7117 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7118 IT_BYTEPOS (*it),
7119 it->end_charpos, Qnil);
7120 }
7121 }
7122 else if (! it->cmp_it.reversed_p)
7123 {
7124 /* Composition created while scanning forward. */
7125 /* Update IT's char/byte positions to point to the first
7126 character of the next grapheme cluster, or to the
7127 character visually after the current composition. */
7128 for (i = 0; i < it->cmp_it.nchars; i++)
7129 bidi_move_to_visually_next (&it->bidi_it);
7130 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7131 IT_CHARPOS (*it) = it->bidi_it.charpos;
7132
7133 if (it->cmp_it.to < it->cmp_it.nglyphs)
7134 {
7135 /* Proceed to the next grapheme cluster. */
7136 it->cmp_it.from = it->cmp_it.to;
7137 }
7138 else
7139 {
7140 /* No more grapheme clusters in this composition.
7141 Find the next stop position. */
7142 ptrdiff_t stop = it->end_charpos;
7143 if (it->bidi_it.scan_dir < 0)
7144 /* Now we are scanning backward and don't know
7145 where to stop. */
7146 stop = -1;
7147 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7148 IT_BYTEPOS (*it), stop, Qnil);
7149 }
7150 }
7151 else
7152 {
7153 /* Composition created while scanning backward. */
7154 /* Update IT's char/byte positions to point to the last
7155 character of the previous grapheme cluster, or the
7156 character visually after the current composition. */
7157 for (i = 0; i < it->cmp_it.nchars; i++)
7158 bidi_move_to_visually_next (&it->bidi_it);
7159 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7160 IT_CHARPOS (*it) = it->bidi_it.charpos;
7161 if (it->cmp_it.from > 0)
7162 {
7163 /* Proceed to the previous grapheme cluster. */
7164 it->cmp_it.to = it->cmp_it.from;
7165 }
7166 else
7167 {
7168 /* No more grapheme clusters in this composition.
7169 Find the next stop position. */
7170 ptrdiff_t stop = it->end_charpos;
7171 if (it->bidi_it.scan_dir < 0)
7172 /* Now we are scanning backward and don't know
7173 where to stop. */
7174 stop = -1;
7175 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7176 IT_BYTEPOS (*it), stop, Qnil);
7177 }
7178 }
7179 }
7180 else
7181 {
7182 eassert (it->len != 0);
7183
7184 if (!it->bidi_p)
7185 {
7186 IT_BYTEPOS (*it) += it->len;
7187 IT_CHARPOS (*it) += 1;
7188 }
7189 else
7190 {
7191 int prev_scan_dir = it->bidi_it.scan_dir;
7192 /* If this is a new paragraph, determine its base
7193 direction (a.k.a. its base embedding level). */
7194 if (it->bidi_it.new_paragraph)
7195 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7196 bidi_move_to_visually_next (&it->bidi_it);
7197 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7198 IT_CHARPOS (*it) = it->bidi_it.charpos;
7199 if (prev_scan_dir != it->bidi_it.scan_dir)
7200 {
7201 /* As the scan direction was changed, we must
7202 re-compute the stop position for composition. */
7203 ptrdiff_t stop = it->end_charpos;
7204 if (it->bidi_it.scan_dir < 0)
7205 stop = -1;
7206 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7207 IT_BYTEPOS (*it), stop, Qnil);
7208 }
7209 }
7210 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7211 }
7212 break;
7213
7214 case GET_FROM_C_STRING:
7215 /* Current display element of IT is from a C string. */
7216 if (!it->bidi_p
7217 /* If the string position is beyond string's end, it means
7218 next_element_from_c_string is padding the string with
7219 blanks, in which case we bypass the bidi iterator,
7220 because it cannot deal with such virtual characters. */
7221 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7222 {
7223 IT_BYTEPOS (*it) += it->len;
7224 IT_CHARPOS (*it) += 1;
7225 }
7226 else
7227 {
7228 bidi_move_to_visually_next (&it->bidi_it);
7229 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7230 IT_CHARPOS (*it) = it->bidi_it.charpos;
7231 }
7232 break;
7233
7234 case GET_FROM_DISPLAY_VECTOR:
7235 /* Current display element of IT is from a display table entry.
7236 Advance in the display table definition. Reset it to null if
7237 end reached, and continue with characters from buffers/
7238 strings. */
7239 ++it->current.dpvec_index;
7240
7241 /* Restore face of the iterator to what they were before the
7242 display vector entry (these entries may contain faces). */
7243 it->face_id = it->saved_face_id;
7244
7245 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7246 {
7247 int recheck_faces = it->ellipsis_p;
7248
7249 if (it->s)
7250 it->method = GET_FROM_C_STRING;
7251 else if (STRINGP (it->string))
7252 it->method = GET_FROM_STRING;
7253 else
7254 {
7255 it->method = GET_FROM_BUFFER;
7256 it->object = it->w->contents;
7257 }
7258
7259 it->dpvec = NULL;
7260 it->current.dpvec_index = -1;
7261
7262 /* Skip over characters which were displayed via IT->dpvec. */
7263 if (it->dpvec_char_len < 0)
7264 reseat_at_next_visible_line_start (it, 1);
7265 else if (it->dpvec_char_len > 0)
7266 {
7267 if (it->method == GET_FROM_STRING
7268 && it->current.overlay_string_index >= 0
7269 && it->n_overlay_strings > 0)
7270 it->ignore_overlay_strings_at_pos_p = 1;
7271 it->len = it->dpvec_char_len;
7272 set_iterator_to_next (it, reseat_p);
7273 }
7274
7275 /* Maybe recheck faces after display vector */
7276 if (recheck_faces)
7277 it->stop_charpos = IT_CHARPOS (*it);
7278 }
7279 break;
7280
7281 case GET_FROM_STRING:
7282 /* Current display element is a character from a Lisp string. */
7283 eassert (it->s == NULL && STRINGP (it->string));
7284 /* Don't advance past string end. These conditions are true
7285 when set_iterator_to_next is called at the end of
7286 get_next_display_element, in which case the Lisp string is
7287 already exhausted, and all we want is pop the iterator
7288 stack. */
7289 if (it->current.overlay_string_index >= 0)
7290 {
7291 /* This is an overlay string, so there's no padding with
7292 spaces, and the number of characters in the string is
7293 where the string ends. */
7294 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7295 goto consider_string_end;
7296 }
7297 else
7298 {
7299 /* Not an overlay string. There could be padding, so test
7300 against it->end_charpos . */
7301 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7302 goto consider_string_end;
7303 }
7304 if (it->cmp_it.id >= 0)
7305 {
7306 int i;
7307
7308 if (! it->bidi_p)
7309 {
7310 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7311 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7312 if (it->cmp_it.to < it->cmp_it.nglyphs)
7313 it->cmp_it.from = it->cmp_it.to;
7314 else
7315 {
7316 it->cmp_it.id = -1;
7317 composition_compute_stop_pos (&it->cmp_it,
7318 IT_STRING_CHARPOS (*it),
7319 IT_STRING_BYTEPOS (*it),
7320 it->end_charpos, it->string);
7321 }
7322 }
7323 else if (! it->cmp_it.reversed_p)
7324 {
7325 for (i = 0; i < it->cmp_it.nchars; i++)
7326 bidi_move_to_visually_next (&it->bidi_it);
7327 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7328 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7329
7330 if (it->cmp_it.to < it->cmp_it.nglyphs)
7331 it->cmp_it.from = it->cmp_it.to;
7332 else
7333 {
7334 ptrdiff_t stop = it->end_charpos;
7335 if (it->bidi_it.scan_dir < 0)
7336 stop = -1;
7337 composition_compute_stop_pos (&it->cmp_it,
7338 IT_STRING_CHARPOS (*it),
7339 IT_STRING_BYTEPOS (*it), stop,
7340 it->string);
7341 }
7342 }
7343 else
7344 {
7345 for (i = 0; i < it->cmp_it.nchars; i++)
7346 bidi_move_to_visually_next (&it->bidi_it);
7347 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7348 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7349 if (it->cmp_it.from > 0)
7350 it->cmp_it.to = it->cmp_it.from;
7351 else
7352 {
7353 ptrdiff_t stop = it->end_charpos;
7354 if (it->bidi_it.scan_dir < 0)
7355 stop = -1;
7356 composition_compute_stop_pos (&it->cmp_it,
7357 IT_STRING_CHARPOS (*it),
7358 IT_STRING_BYTEPOS (*it), stop,
7359 it->string);
7360 }
7361 }
7362 }
7363 else
7364 {
7365 if (!it->bidi_p
7366 /* If the string position is beyond string's end, it
7367 means next_element_from_string is padding the string
7368 with blanks, in which case we bypass the bidi
7369 iterator, because it cannot deal with such virtual
7370 characters. */
7371 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7372 {
7373 IT_STRING_BYTEPOS (*it) += it->len;
7374 IT_STRING_CHARPOS (*it) += 1;
7375 }
7376 else
7377 {
7378 int prev_scan_dir = it->bidi_it.scan_dir;
7379
7380 bidi_move_to_visually_next (&it->bidi_it);
7381 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7382 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7383 if (prev_scan_dir != it->bidi_it.scan_dir)
7384 {
7385 ptrdiff_t stop = it->end_charpos;
7386
7387 if (it->bidi_it.scan_dir < 0)
7388 stop = -1;
7389 composition_compute_stop_pos (&it->cmp_it,
7390 IT_STRING_CHARPOS (*it),
7391 IT_STRING_BYTEPOS (*it), stop,
7392 it->string);
7393 }
7394 }
7395 }
7396
7397 consider_string_end:
7398
7399 if (it->current.overlay_string_index >= 0)
7400 {
7401 /* IT->string is an overlay string. Advance to the
7402 next, if there is one. */
7403 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7404 {
7405 it->ellipsis_p = 0;
7406 next_overlay_string (it);
7407 if (it->ellipsis_p)
7408 setup_for_ellipsis (it, 0);
7409 }
7410 }
7411 else
7412 {
7413 /* IT->string is not an overlay string. If we reached
7414 its end, and there is something on IT->stack, proceed
7415 with what is on the stack. This can be either another
7416 string, this time an overlay string, or a buffer. */
7417 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7418 && it->sp > 0)
7419 {
7420 pop_it (it);
7421 if (it->method == GET_FROM_STRING)
7422 goto consider_string_end;
7423 }
7424 }
7425 break;
7426
7427 case GET_FROM_IMAGE:
7428 case GET_FROM_STRETCH:
7429 /* The position etc with which we have to proceed are on
7430 the stack. The position may be at the end of a string,
7431 if the `display' property takes up the whole string. */
7432 eassert (it->sp > 0);
7433 pop_it (it);
7434 if (it->method == GET_FROM_STRING)
7435 goto consider_string_end;
7436 break;
7437
7438 default:
7439 /* There are no other methods defined, so this should be a bug. */
7440 emacs_abort ();
7441 }
7442
7443 eassert (it->method != GET_FROM_STRING
7444 || (STRINGP (it->string)
7445 && IT_STRING_CHARPOS (*it) >= 0));
7446 }
7447
7448 /* Load IT's display element fields with information about the next
7449 display element which comes from a display table entry or from the
7450 result of translating a control character to one of the forms `^C'
7451 or `\003'.
7452
7453 IT->dpvec holds the glyphs to return as characters.
7454 IT->saved_face_id holds the face id before the display vector--it
7455 is restored into IT->face_id in set_iterator_to_next. */
7456
7457 static int
7458 next_element_from_display_vector (struct it *it)
7459 {
7460 Lisp_Object gc;
7461
7462 /* Precondition. */
7463 eassert (it->dpvec && it->current.dpvec_index >= 0);
7464
7465 it->face_id = it->saved_face_id;
7466
7467 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7468 That seemed totally bogus - so I changed it... */
7469 gc = it->dpvec[it->current.dpvec_index];
7470
7471 if (GLYPH_CODE_P (gc))
7472 {
7473 it->c = GLYPH_CODE_CHAR (gc);
7474 it->len = CHAR_BYTES (it->c);
7475
7476 /* The entry may contain a face id to use. Such a face id is
7477 the id of a Lisp face, not a realized face. A face id of
7478 zero means no face is specified. */
7479 if (it->dpvec_face_id >= 0)
7480 it->face_id = it->dpvec_face_id;
7481 else
7482 {
7483 int lface_id = GLYPH_CODE_FACE (gc);
7484 if (lface_id > 0)
7485 it->face_id = merge_faces (it->f, Qt, lface_id,
7486 it->saved_face_id);
7487 }
7488 }
7489 else
7490 /* Display table entry is invalid. Return a space. */
7491 it->c = ' ', it->len = 1;
7492
7493 /* Don't change position and object of the iterator here. They are
7494 still the values of the character that had this display table
7495 entry or was translated, and that's what we want. */
7496 it->what = IT_CHARACTER;
7497 return 1;
7498 }
7499
7500 /* Get the first element of string/buffer in the visual order, after
7501 being reseated to a new position in a string or a buffer. */
7502 static void
7503 get_visually_first_element (struct it *it)
7504 {
7505 int string_p = STRINGP (it->string) || it->s;
7506 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7507 ptrdiff_t bob = (string_p ? 0 : BEGV);
7508
7509 if (STRINGP (it->string))
7510 {
7511 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7512 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7513 }
7514 else
7515 {
7516 it->bidi_it.charpos = IT_CHARPOS (*it);
7517 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7518 }
7519
7520 if (it->bidi_it.charpos == eob)
7521 {
7522 /* Nothing to do, but reset the FIRST_ELT flag, like
7523 bidi_paragraph_init does, because we are not going to
7524 call it. */
7525 it->bidi_it.first_elt = 0;
7526 }
7527 else if (it->bidi_it.charpos == bob
7528 || (!string_p
7529 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7530 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7531 {
7532 /* If we are at the beginning of a line/string, we can produce
7533 the next element right away. */
7534 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7535 bidi_move_to_visually_next (&it->bidi_it);
7536 }
7537 else
7538 {
7539 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7540
7541 /* We need to prime the bidi iterator starting at the line's or
7542 string's beginning, before we will be able to produce the
7543 next element. */
7544 if (string_p)
7545 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7546 else
7547 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7548 IT_BYTEPOS (*it), -1,
7549 &it->bidi_it.bytepos);
7550 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7551 do
7552 {
7553 /* Now return to buffer/string position where we were asked
7554 to get the next display element, and produce that. */
7555 bidi_move_to_visually_next (&it->bidi_it);
7556 }
7557 while (it->bidi_it.bytepos != orig_bytepos
7558 && it->bidi_it.charpos < eob);
7559 }
7560
7561 /* Adjust IT's position information to where we ended up. */
7562 if (STRINGP (it->string))
7563 {
7564 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7565 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7566 }
7567 else
7568 {
7569 IT_CHARPOS (*it) = it->bidi_it.charpos;
7570 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7571 }
7572
7573 if (STRINGP (it->string) || !it->s)
7574 {
7575 ptrdiff_t stop, charpos, bytepos;
7576
7577 if (STRINGP (it->string))
7578 {
7579 eassert (!it->s);
7580 stop = SCHARS (it->string);
7581 if (stop > it->end_charpos)
7582 stop = it->end_charpos;
7583 charpos = IT_STRING_CHARPOS (*it);
7584 bytepos = IT_STRING_BYTEPOS (*it);
7585 }
7586 else
7587 {
7588 stop = it->end_charpos;
7589 charpos = IT_CHARPOS (*it);
7590 bytepos = IT_BYTEPOS (*it);
7591 }
7592 if (it->bidi_it.scan_dir < 0)
7593 stop = -1;
7594 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7595 it->string);
7596 }
7597 }
7598
7599 /* Load IT with the next display element from Lisp string IT->string.
7600 IT->current.string_pos is the current position within the string.
7601 If IT->current.overlay_string_index >= 0, the Lisp string is an
7602 overlay string. */
7603
7604 static int
7605 next_element_from_string (struct it *it)
7606 {
7607 struct text_pos position;
7608
7609 eassert (STRINGP (it->string));
7610 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7611 eassert (IT_STRING_CHARPOS (*it) >= 0);
7612 position = it->current.string_pos;
7613
7614 /* With bidi reordering, the character to display might not be the
7615 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7616 that we were reseat()ed to a new string, whose paragraph
7617 direction is not known. */
7618 if (it->bidi_p && it->bidi_it.first_elt)
7619 {
7620 get_visually_first_element (it);
7621 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7622 }
7623
7624 /* Time to check for invisible text? */
7625 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7626 {
7627 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7628 {
7629 if (!(!it->bidi_p
7630 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7631 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7632 {
7633 /* With bidi non-linear iteration, we could find
7634 ourselves far beyond the last computed stop_charpos,
7635 with several other stop positions in between that we
7636 missed. Scan them all now, in buffer's logical
7637 order, until we find and handle the last stop_charpos
7638 that precedes our current position. */
7639 handle_stop_backwards (it, it->stop_charpos);
7640 return GET_NEXT_DISPLAY_ELEMENT (it);
7641 }
7642 else
7643 {
7644 if (it->bidi_p)
7645 {
7646 /* Take note of the stop position we just moved
7647 across, for when we will move back across it. */
7648 it->prev_stop = it->stop_charpos;
7649 /* If we are at base paragraph embedding level, take
7650 note of the last stop position seen at this
7651 level. */
7652 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7653 it->base_level_stop = it->stop_charpos;
7654 }
7655 handle_stop (it);
7656
7657 /* Since a handler may have changed IT->method, we must
7658 recurse here. */
7659 return GET_NEXT_DISPLAY_ELEMENT (it);
7660 }
7661 }
7662 else if (it->bidi_p
7663 /* If we are before prev_stop, we may have overstepped
7664 on our way backwards a stop_pos, and if so, we need
7665 to handle that stop_pos. */
7666 && IT_STRING_CHARPOS (*it) < it->prev_stop
7667 /* We can sometimes back up for reasons that have nothing
7668 to do with bidi reordering. E.g., compositions. The
7669 code below is only needed when we are above the base
7670 embedding level, so test for that explicitly. */
7671 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7672 {
7673 /* If we lost track of base_level_stop, we have no better
7674 place for handle_stop_backwards to start from than string
7675 beginning. This happens, e.g., when we were reseated to
7676 the previous screenful of text by vertical-motion. */
7677 if (it->base_level_stop <= 0
7678 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7679 it->base_level_stop = 0;
7680 handle_stop_backwards (it, it->base_level_stop);
7681 return GET_NEXT_DISPLAY_ELEMENT (it);
7682 }
7683 }
7684
7685 if (it->current.overlay_string_index >= 0)
7686 {
7687 /* Get the next character from an overlay string. In overlay
7688 strings, there is no field width or padding with spaces to
7689 do. */
7690 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7691 {
7692 it->what = IT_EOB;
7693 return 0;
7694 }
7695 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7696 IT_STRING_BYTEPOS (*it),
7697 it->bidi_it.scan_dir < 0
7698 ? -1
7699 : SCHARS (it->string))
7700 && next_element_from_composition (it))
7701 {
7702 return 1;
7703 }
7704 else if (STRING_MULTIBYTE (it->string))
7705 {
7706 const unsigned char *s = (SDATA (it->string)
7707 + IT_STRING_BYTEPOS (*it));
7708 it->c = string_char_and_length (s, &it->len);
7709 }
7710 else
7711 {
7712 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7713 it->len = 1;
7714 }
7715 }
7716 else
7717 {
7718 /* Get the next character from a Lisp string that is not an
7719 overlay string. Such strings come from the mode line, for
7720 example. We may have to pad with spaces, or truncate the
7721 string. See also next_element_from_c_string. */
7722 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7723 {
7724 it->what = IT_EOB;
7725 return 0;
7726 }
7727 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7728 {
7729 /* Pad with spaces. */
7730 it->c = ' ', it->len = 1;
7731 CHARPOS (position) = BYTEPOS (position) = -1;
7732 }
7733 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7734 IT_STRING_BYTEPOS (*it),
7735 it->bidi_it.scan_dir < 0
7736 ? -1
7737 : it->string_nchars)
7738 && next_element_from_composition (it))
7739 {
7740 return 1;
7741 }
7742 else if (STRING_MULTIBYTE (it->string))
7743 {
7744 const unsigned char *s = (SDATA (it->string)
7745 + IT_STRING_BYTEPOS (*it));
7746 it->c = string_char_and_length (s, &it->len);
7747 }
7748 else
7749 {
7750 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7751 it->len = 1;
7752 }
7753 }
7754
7755 /* Record what we have and where it came from. */
7756 it->what = IT_CHARACTER;
7757 it->object = it->string;
7758 it->position = position;
7759 return 1;
7760 }
7761
7762
7763 /* Load IT with next display element from C string IT->s.
7764 IT->string_nchars is the maximum number of characters to return
7765 from the string. IT->end_charpos may be greater than
7766 IT->string_nchars when this function is called, in which case we
7767 may have to return padding spaces. Value is zero if end of string
7768 reached, including padding spaces. */
7769
7770 static int
7771 next_element_from_c_string (struct it *it)
7772 {
7773 int success_p = 1;
7774
7775 eassert (it->s);
7776 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7777 it->what = IT_CHARACTER;
7778 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7779 it->object = Qnil;
7780
7781 /* With bidi reordering, the character to display might not be the
7782 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7783 we were reseated to a new string, whose paragraph direction is
7784 not known. */
7785 if (it->bidi_p && it->bidi_it.first_elt)
7786 get_visually_first_element (it);
7787
7788 /* IT's position can be greater than IT->string_nchars in case a
7789 field width or precision has been specified when the iterator was
7790 initialized. */
7791 if (IT_CHARPOS (*it) >= it->end_charpos)
7792 {
7793 /* End of the game. */
7794 it->what = IT_EOB;
7795 success_p = 0;
7796 }
7797 else if (IT_CHARPOS (*it) >= it->string_nchars)
7798 {
7799 /* Pad with spaces. */
7800 it->c = ' ', it->len = 1;
7801 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7802 }
7803 else if (it->multibyte_p)
7804 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7805 else
7806 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7807
7808 return success_p;
7809 }
7810
7811
7812 /* Set up IT to return characters from an ellipsis, if appropriate.
7813 The definition of the ellipsis glyphs may come from a display table
7814 entry. This function fills IT with the first glyph from the
7815 ellipsis if an ellipsis is to be displayed. */
7816
7817 static int
7818 next_element_from_ellipsis (struct it *it)
7819 {
7820 if (it->selective_display_ellipsis_p)
7821 setup_for_ellipsis (it, it->len);
7822 else
7823 {
7824 /* The face at the current position may be different from the
7825 face we find after the invisible text. Remember what it
7826 was in IT->saved_face_id, and signal that it's there by
7827 setting face_before_selective_p. */
7828 it->saved_face_id = it->face_id;
7829 it->method = GET_FROM_BUFFER;
7830 it->object = it->w->contents;
7831 reseat_at_next_visible_line_start (it, 1);
7832 it->face_before_selective_p = 1;
7833 }
7834
7835 return GET_NEXT_DISPLAY_ELEMENT (it);
7836 }
7837
7838
7839 /* Deliver an image display element. The iterator IT is already
7840 filled with image information (done in handle_display_prop). Value
7841 is always 1. */
7842
7843
7844 static int
7845 next_element_from_image (struct it *it)
7846 {
7847 it->what = IT_IMAGE;
7848 it->ignore_overlay_strings_at_pos_p = 0;
7849 return 1;
7850 }
7851
7852
7853 /* Fill iterator IT with next display element from a stretch glyph
7854 property. IT->object is the value of the text property. Value is
7855 always 1. */
7856
7857 static int
7858 next_element_from_stretch (struct it *it)
7859 {
7860 it->what = IT_STRETCH;
7861 return 1;
7862 }
7863
7864 /* Scan backwards from IT's current position until we find a stop
7865 position, or until BEGV. This is called when we find ourself
7866 before both the last known prev_stop and base_level_stop while
7867 reordering bidirectional text. */
7868
7869 static void
7870 compute_stop_pos_backwards (struct it *it)
7871 {
7872 const int SCAN_BACK_LIMIT = 1000;
7873 struct text_pos pos;
7874 struct display_pos save_current = it->current;
7875 struct text_pos save_position = it->position;
7876 ptrdiff_t charpos = IT_CHARPOS (*it);
7877 ptrdiff_t where_we_are = charpos;
7878 ptrdiff_t save_stop_pos = it->stop_charpos;
7879 ptrdiff_t save_end_pos = it->end_charpos;
7880
7881 eassert (NILP (it->string) && !it->s);
7882 eassert (it->bidi_p);
7883 it->bidi_p = 0;
7884 do
7885 {
7886 it->end_charpos = min (charpos + 1, ZV);
7887 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7888 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7889 reseat_1 (it, pos, 0);
7890 compute_stop_pos (it);
7891 /* We must advance forward, right? */
7892 if (it->stop_charpos <= charpos)
7893 emacs_abort ();
7894 }
7895 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7896
7897 if (it->stop_charpos <= where_we_are)
7898 it->prev_stop = it->stop_charpos;
7899 else
7900 it->prev_stop = BEGV;
7901 it->bidi_p = 1;
7902 it->current = save_current;
7903 it->position = save_position;
7904 it->stop_charpos = save_stop_pos;
7905 it->end_charpos = save_end_pos;
7906 }
7907
7908 /* Scan forward from CHARPOS in the current buffer/string, until we
7909 find a stop position > current IT's position. Then handle the stop
7910 position before that. This is called when we bump into a stop
7911 position while reordering bidirectional text. CHARPOS should be
7912 the last previously processed stop_pos (or BEGV/0, if none were
7913 processed yet) whose position is less that IT's current
7914 position. */
7915
7916 static void
7917 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7918 {
7919 int bufp = !STRINGP (it->string);
7920 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7921 struct display_pos save_current = it->current;
7922 struct text_pos save_position = it->position;
7923 struct text_pos pos1;
7924 ptrdiff_t next_stop;
7925
7926 /* Scan in strict logical order. */
7927 eassert (it->bidi_p);
7928 it->bidi_p = 0;
7929 do
7930 {
7931 it->prev_stop = charpos;
7932 if (bufp)
7933 {
7934 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7935 reseat_1 (it, pos1, 0);
7936 }
7937 else
7938 it->current.string_pos = string_pos (charpos, it->string);
7939 compute_stop_pos (it);
7940 /* We must advance forward, right? */
7941 if (it->stop_charpos <= it->prev_stop)
7942 emacs_abort ();
7943 charpos = it->stop_charpos;
7944 }
7945 while (charpos <= where_we_are);
7946
7947 it->bidi_p = 1;
7948 it->current = save_current;
7949 it->position = save_position;
7950 next_stop = it->stop_charpos;
7951 it->stop_charpos = it->prev_stop;
7952 handle_stop (it);
7953 it->stop_charpos = next_stop;
7954 }
7955
7956 /* Load IT with the next display element from current_buffer. Value
7957 is zero if end of buffer reached. IT->stop_charpos is the next
7958 position at which to stop and check for text properties or buffer
7959 end. */
7960
7961 static int
7962 next_element_from_buffer (struct it *it)
7963 {
7964 int success_p = 1;
7965
7966 eassert (IT_CHARPOS (*it) >= BEGV);
7967 eassert (NILP (it->string) && !it->s);
7968 eassert (!it->bidi_p
7969 || (EQ (it->bidi_it.string.lstring, Qnil)
7970 && it->bidi_it.string.s == NULL));
7971
7972 /* With bidi reordering, the character to display might not be the
7973 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7974 we were reseat()ed to a new buffer position, which is potentially
7975 a different paragraph. */
7976 if (it->bidi_p && it->bidi_it.first_elt)
7977 {
7978 get_visually_first_element (it);
7979 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7980 }
7981
7982 if (IT_CHARPOS (*it) >= it->stop_charpos)
7983 {
7984 if (IT_CHARPOS (*it) >= it->end_charpos)
7985 {
7986 int overlay_strings_follow_p;
7987
7988 /* End of the game, except when overlay strings follow that
7989 haven't been returned yet. */
7990 if (it->overlay_strings_at_end_processed_p)
7991 overlay_strings_follow_p = 0;
7992 else
7993 {
7994 it->overlay_strings_at_end_processed_p = 1;
7995 overlay_strings_follow_p = get_overlay_strings (it, 0);
7996 }
7997
7998 if (overlay_strings_follow_p)
7999 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
8000 else
8001 {
8002 it->what = IT_EOB;
8003 it->position = it->current.pos;
8004 success_p = 0;
8005 }
8006 }
8007 else if (!(!it->bidi_p
8008 || BIDI_AT_BASE_LEVEL (it->bidi_it)
8009 || IT_CHARPOS (*it) == it->stop_charpos))
8010 {
8011 /* With bidi non-linear iteration, we could find ourselves
8012 far beyond the last computed stop_charpos, with several
8013 other stop positions in between that we missed. Scan
8014 them all now, in buffer's logical order, until we find
8015 and handle the last stop_charpos that precedes our
8016 current position. */
8017 handle_stop_backwards (it, it->stop_charpos);
8018 return GET_NEXT_DISPLAY_ELEMENT (it);
8019 }
8020 else
8021 {
8022 if (it->bidi_p)
8023 {
8024 /* Take note of the stop position we just moved across,
8025 for when we will move back across it. */
8026 it->prev_stop = it->stop_charpos;
8027 /* If we are at base paragraph embedding level, take
8028 note of the last stop position seen at this
8029 level. */
8030 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
8031 it->base_level_stop = it->stop_charpos;
8032 }
8033 handle_stop (it);
8034 return GET_NEXT_DISPLAY_ELEMENT (it);
8035 }
8036 }
8037 else if (it->bidi_p
8038 /* If we are before prev_stop, we may have overstepped on
8039 our way backwards a stop_pos, and if so, we need to
8040 handle that stop_pos. */
8041 && IT_CHARPOS (*it) < it->prev_stop
8042 /* We can sometimes back up for reasons that have nothing
8043 to do with bidi reordering. E.g., compositions. The
8044 code below is only needed when we are above the base
8045 embedding level, so test for that explicitly. */
8046 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8047 {
8048 if (it->base_level_stop <= 0
8049 || IT_CHARPOS (*it) < it->base_level_stop)
8050 {
8051 /* If we lost track of base_level_stop, we need to find
8052 prev_stop by looking backwards. This happens, e.g., when
8053 we were reseated to the previous screenful of text by
8054 vertical-motion. */
8055 it->base_level_stop = BEGV;
8056 compute_stop_pos_backwards (it);
8057 handle_stop_backwards (it, it->prev_stop);
8058 }
8059 else
8060 handle_stop_backwards (it, it->base_level_stop);
8061 return GET_NEXT_DISPLAY_ELEMENT (it);
8062 }
8063 else
8064 {
8065 /* No face changes, overlays etc. in sight, so just return a
8066 character from current_buffer. */
8067 unsigned char *p;
8068 ptrdiff_t stop;
8069
8070 /* Maybe run the redisplay end trigger hook. Performance note:
8071 This doesn't seem to cost measurable time. */
8072 if (it->redisplay_end_trigger_charpos
8073 && it->glyph_row
8074 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8075 run_redisplay_end_trigger_hook (it);
8076
8077 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8078 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8079 stop)
8080 && next_element_from_composition (it))
8081 {
8082 return 1;
8083 }
8084
8085 /* Get the next character, maybe multibyte. */
8086 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8087 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8088 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8089 else
8090 it->c = *p, it->len = 1;
8091
8092 /* Record what we have and where it came from. */
8093 it->what = IT_CHARACTER;
8094 it->object = it->w->contents;
8095 it->position = it->current.pos;
8096
8097 /* Normally we return the character found above, except when we
8098 really want to return an ellipsis for selective display. */
8099 if (it->selective)
8100 {
8101 if (it->c == '\n')
8102 {
8103 /* A value of selective > 0 means hide lines indented more
8104 than that number of columns. */
8105 if (it->selective > 0
8106 && IT_CHARPOS (*it) + 1 < ZV
8107 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8108 IT_BYTEPOS (*it) + 1,
8109 it->selective))
8110 {
8111 success_p = next_element_from_ellipsis (it);
8112 it->dpvec_char_len = -1;
8113 }
8114 }
8115 else if (it->c == '\r' && it->selective == -1)
8116 {
8117 /* A value of selective == -1 means that everything from the
8118 CR to the end of the line is invisible, with maybe an
8119 ellipsis displayed for it. */
8120 success_p = next_element_from_ellipsis (it);
8121 it->dpvec_char_len = -1;
8122 }
8123 }
8124 }
8125
8126 /* Value is zero if end of buffer reached. */
8127 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8128 return success_p;
8129 }
8130
8131
8132 /* Run the redisplay end trigger hook for IT. */
8133
8134 static void
8135 run_redisplay_end_trigger_hook (struct it *it)
8136 {
8137 Lisp_Object args[3];
8138
8139 /* IT->glyph_row should be non-null, i.e. we should be actually
8140 displaying something, or otherwise we should not run the hook. */
8141 eassert (it->glyph_row);
8142
8143 /* Set up hook arguments. */
8144 args[0] = Qredisplay_end_trigger_functions;
8145 args[1] = it->window;
8146 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8147 it->redisplay_end_trigger_charpos = 0;
8148
8149 /* Since we are *trying* to run these functions, don't try to run
8150 them again, even if they get an error. */
8151 wset_redisplay_end_trigger (it->w, Qnil);
8152 Frun_hook_with_args (3, args);
8153
8154 /* Notice if it changed the face of the character we are on. */
8155 handle_face_prop (it);
8156 }
8157
8158
8159 /* Deliver a composition display element. Unlike the other
8160 next_element_from_XXX, this function is not registered in the array
8161 get_next_element[]. It is called from next_element_from_buffer and
8162 next_element_from_string when necessary. */
8163
8164 static int
8165 next_element_from_composition (struct it *it)
8166 {
8167 it->what = IT_COMPOSITION;
8168 it->len = it->cmp_it.nbytes;
8169 if (STRINGP (it->string))
8170 {
8171 if (it->c < 0)
8172 {
8173 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8174 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8175 return 0;
8176 }
8177 it->position = it->current.string_pos;
8178 it->object = it->string;
8179 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8180 IT_STRING_BYTEPOS (*it), it->string);
8181 }
8182 else
8183 {
8184 if (it->c < 0)
8185 {
8186 IT_CHARPOS (*it) += it->cmp_it.nchars;
8187 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8188 if (it->bidi_p)
8189 {
8190 if (it->bidi_it.new_paragraph)
8191 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8192 /* Resync the bidi iterator with IT's new position.
8193 FIXME: this doesn't support bidirectional text. */
8194 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8195 bidi_move_to_visually_next (&it->bidi_it);
8196 }
8197 return 0;
8198 }
8199 it->position = it->current.pos;
8200 it->object = it->w->contents;
8201 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8202 IT_BYTEPOS (*it), Qnil);
8203 }
8204 return 1;
8205 }
8206
8207
8208 \f
8209 /***********************************************************************
8210 Moving an iterator without producing glyphs
8211 ***********************************************************************/
8212
8213 /* Check if iterator is at a position corresponding to a valid buffer
8214 position after some move_it_ call. */
8215
8216 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8217 ((it)->method == GET_FROM_STRING \
8218 ? IT_STRING_CHARPOS (*it) == 0 \
8219 : 1)
8220
8221
8222 /* Move iterator IT to a specified buffer or X position within one
8223 line on the display without producing glyphs.
8224
8225 OP should be a bit mask including some or all of these bits:
8226 MOVE_TO_X: Stop upon reaching x-position TO_X.
8227 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8228 Regardless of OP's value, stop upon reaching the end of the display line.
8229
8230 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8231 This means, in particular, that TO_X includes window's horizontal
8232 scroll amount.
8233
8234 The return value has several possible values that
8235 say what condition caused the scan to stop:
8236
8237 MOVE_POS_MATCH_OR_ZV
8238 - when TO_POS or ZV was reached.
8239
8240 MOVE_X_REACHED
8241 -when TO_X was reached before TO_POS or ZV were reached.
8242
8243 MOVE_LINE_CONTINUED
8244 - when we reached the end of the display area and the line must
8245 be continued.
8246
8247 MOVE_LINE_TRUNCATED
8248 - when we reached the end of the display area and the line is
8249 truncated.
8250
8251 MOVE_NEWLINE_OR_CR
8252 - when we stopped at a line end, i.e. a newline or a CR and selective
8253 display is on. */
8254
8255 static enum move_it_result
8256 move_it_in_display_line_to (struct it *it,
8257 ptrdiff_t to_charpos, int to_x,
8258 enum move_operation_enum op)
8259 {
8260 enum move_it_result result = MOVE_UNDEFINED;
8261 struct glyph_row *saved_glyph_row;
8262 struct it wrap_it, atpos_it, atx_it, ppos_it;
8263 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8264 void *ppos_data = NULL;
8265 int may_wrap = 0;
8266 enum it_method prev_method = it->method;
8267 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8268 int saw_smaller_pos = prev_pos < to_charpos;
8269
8270 /* Don't produce glyphs in produce_glyphs. */
8271 saved_glyph_row = it->glyph_row;
8272 it->glyph_row = NULL;
8273
8274 /* Use wrap_it to save a copy of IT wherever a word wrap could
8275 occur. Use atpos_it to save a copy of IT at the desired buffer
8276 position, if found, so that we can scan ahead and check if the
8277 word later overshoots the window edge. Use atx_it similarly, for
8278 pixel positions. */
8279 wrap_it.sp = -1;
8280 atpos_it.sp = -1;
8281 atx_it.sp = -1;
8282
8283 /* Use ppos_it under bidi reordering to save a copy of IT for the
8284 position > CHARPOS that is the closest to CHARPOS. We restore
8285 that position in IT when we have scanned the entire display line
8286 without finding a match for CHARPOS and all the character
8287 positions are greater than CHARPOS. */
8288 if (it->bidi_p)
8289 {
8290 SAVE_IT (ppos_it, *it, ppos_data);
8291 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8292 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8293 SAVE_IT (ppos_it, *it, ppos_data);
8294 }
8295
8296 #define BUFFER_POS_REACHED_P() \
8297 ((op & MOVE_TO_POS) != 0 \
8298 && BUFFERP (it->object) \
8299 && (IT_CHARPOS (*it) == to_charpos \
8300 || ((!it->bidi_p \
8301 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8302 && IT_CHARPOS (*it) > to_charpos) \
8303 || (it->what == IT_COMPOSITION \
8304 && ((IT_CHARPOS (*it) > to_charpos \
8305 && to_charpos >= it->cmp_it.charpos) \
8306 || (IT_CHARPOS (*it) < to_charpos \
8307 && to_charpos <= it->cmp_it.charpos)))) \
8308 && (it->method == GET_FROM_BUFFER \
8309 || (it->method == GET_FROM_DISPLAY_VECTOR \
8310 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8311
8312 /* If there's a line-/wrap-prefix, handle it. */
8313 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8314 && it->current_y < it->last_visible_y)
8315 handle_line_prefix (it);
8316
8317 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8318 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8319
8320 while (1)
8321 {
8322 int x, i, ascent = 0, descent = 0;
8323
8324 /* Utility macro to reset an iterator with x, ascent, and descent. */
8325 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8326 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8327 (IT)->max_descent = descent)
8328
8329 /* Stop if we move beyond TO_CHARPOS (after an image or a
8330 display string or stretch glyph). */
8331 if ((op & MOVE_TO_POS) != 0
8332 && BUFFERP (it->object)
8333 && it->method == GET_FROM_BUFFER
8334 && (((!it->bidi_p
8335 /* When the iterator is at base embedding level, we
8336 are guaranteed that characters are delivered for
8337 display in strictly increasing order of their
8338 buffer positions. */
8339 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8340 && IT_CHARPOS (*it) > to_charpos)
8341 || (it->bidi_p
8342 && (prev_method == GET_FROM_IMAGE
8343 || prev_method == GET_FROM_STRETCH
8344 || prev_method == GET_FROM_STRING)
8345 /* Passed TO_CHARPOS from left to right. */
8346 && ((prev_pos < to_charpos
8347 && IT_CHARPOS (*it) > to_charpos)
8348 /* Passed TO_CHARPOS from right to left. */
8349 || (prev_pos > to_charpos
8350 && IT_CHARPOS (*it) < to_charpos)))))
8351 {
8352 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8353 {
8354 result = MOVE_POS_MATCH_OR_ZV;
8355 break;
8356 }
8357 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8358 /* If wrap_it is valid, the current position might be in a
8359 word that is wrapped. So, save the iterator in
8360 atpos_it and continue to see if wrapping happens. */
8361 SAVE_IT (atpos_it, *it, atpos_data);
8362 }
8363
8364 /* Stop when ZV reached.
8365 We used to stop here when TO_CHARPOS reached as well, but that is
8366 too soon if this glyph does not fit on this line. So we handle it
8367 explicitly below. */
8368 if (!get_next_display_element (it))
8369 {
8370 result = MOVE_POS_MATCH_OR_ZV;
8371 break;
8372 }
8373
8374 if (it->line_wrap == TRUNCATE)
8375 {
8376 if (BUFFER_POS_REACHED_P ())
8377 {
8378 result = MOVE_POS_MATCH_OR_ZV;
8379 break;
8380 }
8381 }
8382 else
8383 {
8384 if (it->line_wrap == WORD_WRAP)
8385 {
8386 if (IT_DISPLAYING_WHITESPACE (it))
8387 may_wrap = 1;
8388 else if (may_wrap)
8389 {
8390 /* We have reached a glyph that follows one or more
8391 whitespace characters. If the position is
8392 already found, we are done. */
8393 if (atpos_it.sp >= 0)
8394 {
8395 RESTORE_IT (it, &atpos_it, atpos_data);
8396 result = MOVE_POS_MATCH_OR_ZV;
8397 goto done;
8398 }
8399 if (atx_it.sp >= 0)
8400 {
8401 RESTORE_IT (it, &atx_it, atx_data);
8402 result = MOVE_X_REACHED;
8403 goto done;
8404 }
8405 /* Otherwise, we can wrap here. */
8406 SAVE_IT (wrap_it, *it, wrap_data);
8407 may_wrap = 0;
8408 }
8409 }
8410 }
8411
8412 /* Remember the line height for the current line, in case
8413 the next element doesn't fit on the line. */
8414 ascent = it->max_ascent;
8415 descent = it->max_descent;
8416
8417 /* The call to produce_glyphs will get the metrics of the
8418 display element IT is loaded with. Record the x-position
8419 before this display element, in case it doesn't fit on the
8420 line. */
8421 x = it->current_x;
8422
8423 PRODUCE_GLYPHS (it);
8424
8425 if (it->area != TEXT_AREA)
8426 {
8427 prev_method = it->method;
8428 if (it->method == GET_FROM_BUFFER)
8429 prev_pos = IT_CHARPOS (*it);
8430 set_iterator_to_next (it, 1);
8431 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8432 SET_TEXT_POS (this_line_min_pos,
8433 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8434 if (it->bidi_p
8435 && (op & MOVE_TO_POS)
8436 && IT_CHARPOS (*it) > to_charpos
8437 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8438 SAVE_IT (ppos_it, *it, ppos_data);
8439 continue;
8440 }
8441
8442 /* The number of glyphs we get back in IT->nglyphs will normally
8443 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8444 character on a terminal frame, or (iii) a line end. For the
8445 second case, IT->nglyphs - 1 padding glyphs will be present.
8446 (On X frames, there is only one glyph produced for a
8447 composite character.)
8448
8449 The behavior implemented below means, for continuation lines,
8450 that as many spaces of a TAB as fit on the current line are
8451 displayed there. For terminal frames, as many glyphs of a
8452 multi-glyph character are displayed in the current line, too.
8453 This is what the old redisplay code did, and we keep it that
8454 way. Under X, the whole shape of a complex character must
8455 fit on the line or it will be completely displayed in the
8456 next line.
8457
8458 Note that both for tabs and padding glyphs, all glyphs have
8459 the same width. */
8460 if (it->nglyphs)
8461 {
8462 /* More than one glyph or glyph doesn't fit on line. All
8463 glyphs have the same width. */
8464 int single_glyph_width = it->pixel_width / it->nglyphs;
8465 int new_x;
8466 int x_before_this_char = x;
8467 int hpos_before_this_char = it->hpos;
8468
8469 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8470 {
8471 new_x = x + single_glyph_width;
8472
8473 /* We want to leave anything reaching TO_X to the caller. */
8474 if ((op & MOVE_TO_X) && new_x > to_x)
8475 {
8476 if (BUFFER_POS_REACHED_P ())
8477 {
8478 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8479 goto buffer_pos_reached;
8480 if (atpos_it.sp < 0)
8481 {
8482 SAVE_IT (atpos_it, *it, atpos_data);
8483 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8484 }
8485 }
8486 else
8487 {
8488 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8489 {
8490 it->current_x = x;
8491 result = MOVE_X_REACHED;
8492 break;
8493 }
8494 if (atx_it.sp < 0)
8495 {
8496 SAVE_IT (atx_it, *it, atx_data);
8497 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8498 }
8499 }
8500 }
8501
8502 if (/* Lines are continued. */
8503 it->line_wrap != TRUNCATE
8504 && (/* And glyph doesn't fit on the line. */
8505 new_x > it->last_visible_x
8506 /* Or it fits exactly and we're on a window
8507 system frame. */
8508 || (new_x == it->last_visible_x
8509 && FRAME_WINDOW_P (it->f)
8510 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8511 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8512 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8513 {
8514 if (/* IT->hpos == 0 means the very first glyph
8515 doesn't fit on the line, e.g. a wide image. */
8516 it->hpos == 0
8517 || (new_x == it->last_visible_x
8518 && FRAME_WINDOW_P (it->f)))
8519 {
8520 ++it->hpos;
8521 it->current_x = new_x;
8522
8523 /* The character's last glyph just barely fits
8524 in this row. */
8525 if (i == it->nglyphs - 1)
8526 {
8527 /* If this is the destination position,
8528 return a position *before* it in this row,
8529 now that we know it fits in this row. */
8530 if (BUFFER_POS_REACHED_P ())
8531 {
8532 if (it->line_wrap != WORD_WRAP
8533 || wrap_it.sp < 0)
8534 {
8535 it->hpos = hpos_before_this_char;
8536 it->current_x = x_before_this_char;
8537 result = MOVE_POS_MATCH_OR_ZV;
8538 break;
8539 }
8540 if (it->line_wrap == WORD_WRAP
8541 && atpos_it.sp < 0)
8542 {
8543 SAVE_IT (atpos_it, *it, atpos_data);
8544 atpos_it.current_x = x_before_this_char;
8545 atpos_it.hpos = hpos_before_this_char;
8546 }
8547 }
8548
8549 prev_method = it->method;
8550 if (it->method == GET_FROM_BUFFER)
8551 prev_pos = IT_CHARPOS (*it);
8552 set_iterator_to_next (it, 1);
8553 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8554 SET_TEXT_POS (this_line_min_pos,
8555 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8556 /* On graphical terminals, newlines may
8557 "overflow" into the fringe if
8558 overflow-newline-into-fringe is non-nil.
8559 On text terminals, and on graphical
8560 terminals with no right margin, newlines
8561 may overflow into the last glyph on the
8562 display line.*/
8563 if (!FRAME_WINDOW_P (it->f)
8564 || ((it->bidi_p
8565 && it->bidi_it.paragraph_dir == R2L)
8566 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8567 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8568 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8569 {
8570 if (!get_next_display_element (it))
8571 {
8572 result = MOVE_POS_MATCH_OR_ZV;
8573 break;
8574 }
8575 if (BUFFER_POS_REACHED_P ())
8576 {
8577 if (ITERATOR_AT_END_OF_LINE_P (it))
8578 result = MOVE_POS_MATCH_OR_ZV;
8579 else
8580 result = MOVE_LINE_CONTINUED;
8581 break;
8582 }
8583 if (ITERATOR_AT_END_OF_LINE_P (it)
8584 && (it->line_wrap != WORD_WRAP
8585 || wrap_it.sp < 0))
8586 {
8587 result = MOVE_NEWLINE_OR_CR;
8588 break;
8589 }
8590 }
8591 }
8592 }
8593 else
8594 IT_RESET_X_ASCENT_DESCENT (it);
8595
8596 if (wrap_it.sp >= 0)
8597 {
8598 RESTORE_IT (it, &wrap_it, wrap_data);
8599 atpos_it.sp = -1;
8600 atx_it.sp = -1;
8601 }
8602
8603 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8604 IT_CHARPOS (*it)));
8605 result = MOVE_LINE_CONTINUED;
8606 break;
8607 }
8608
8609 if (BUFFER_POS_REACHED_P ())
8610 {
8611 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8612 goto buffer_pos_reached;
8613 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8614 {
8615 SAVE_IT (atpos_it, *it, atpos_data);
8616 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8617 }
8618 }
8619
8620 if (new_x > it->first_visible_x)
8621 {
8622 /* Glyph is visible. Increment number of glyphs that
8623 would be displayed. */
8624 ++it->hpos;
8625 }
8626 }
8627
8628 if (result != MOVE_UNDEFINED)
8629 break;
8630 }
8631 else if (BUFFER_POS_REACHED_P ())
8632 {
8633 buffer_pos_reached:
8634 IT_RESET_X_ASCENT_DESCENT (it);
8635 result = MOVE_POS_MATCH_OR_ZV;
8636 break;
8637 }
8638 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8639 {
8640 /* Stop when TO_X specified and reached. This check is
8641 necessary here because of lines consisting of a line end,
8642 only. The line end will not produce any glyphs and we
8643 would never get MOVE_X_REACHED. */
8644 eassert (it->nglyphs == 0);
8645 result = MOVE_X_REACHED;
8646 break;
8647 }
8648
8649 /* Is this a line end? If yes, we're done. */
8650 if (ITERATOR_AT_END_OF_LINE_P (it))
8651 {
8652 /* If we are past TO_CHARPOS, but never saw any character
8653 positions smaller than TO_CHARPOS, return
8654 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8655 did. */
8656 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8657 {
8658 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8659 {
8660 if (IT_CHARPOS (ppos_it) < ZV)
8661 {
8662 RESTORE_IT (it, &ppos_it, ppos_data);
8663 result = MOVE_POS_MATCH_OR_ZV;
8664 }
8665 else
8666 goto buffer_pos_reached;
8667 }
8668 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8669 && IT_CHARPOS (*it) > to_charpos)
8670 goto buffer_pos_reached;
8671 else
8672 result = MOVE_NEWLINE_OR_CR;
8673 }
8674 else
8675 result = MOVE_NEWLINE_OR_CR;
8676 break;
8677 }
8678
8679 prev_method = it->method;
8680 if (it->method == GET_FROM_BUFFER)
8681 prev_pos = IT_CHARPOS (*it);
8682 /* The current display element has been consumed. Advance
8683 to the next. */
8684 set_iterator_to_next (it, 1);
8685 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8686 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8687 if (IT_CHARPOS (*it) < to_charpos)
8688 saw_smaller_pos = 1;
8689 if (it->bidi_p
8690 && (op & MOVE_TO_POS)
8691 && IT_CHARPOS (*it) >= to_charpos
8692 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8693 SAVE_IT (ppos_it, *it, ppos_data);
8694
8695 /* Stop if lines are truncated and IT's current x-position is
8696 past the right edge of the window now. */
8697 if (it->line_wrap == TRUNCATE
8698 && it->current_x >= it->last_visible_x)
8699 {
8700 if (!FRAME_WINDOW_P (it->f)
8701 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8702 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8703 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8704 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8705 {
8706 int at_eob_p = 0;
8707
8708 if ((at_eob_p = !get_next_display_element (it))
8709 || BUFFER_POS_REACHED_P ()
8710 /* If we are past TO_CHARPOS, but never saw any
8711 character positions smaller than TO_CHARPOS,
8712 return MOVE_POS_MATCH_OR_ZV, like the
8713 unidirectional display did. */
8714 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8715 && !saw_smaller_pos
8716 && IT_CHARPOS (*it) > to_charpos))
8717 {
8718 if (it->bidi_p
8719 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8720 RESTORE_IT (it, &ppos_it, ppos_data);
8721 result = MOVE_POS_MATCH_OR_ZV;
8722 break;
8723 }
8724 if (ITERATOR_AT_END_OF_LINE_P (it))
8725 {
8726 result = MOVE_NEWLINE_OR_CR;
8727 break;
8728 }
8729 }
8730 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8731 && !saw_smaller_pos
8732 && IT_CHARPOS (*it) > to_charpos)
8733 {
8734 if (IT_CHARPOS (ppos_it) < ZV)
8735 RESTORE_IT (it, &ppos_it, ppos_data);
8736 result = MOVE_POS_MATCH_OR_ZV;
8737 break;
8738 }
8739 result = MOVE_LINE_TRUNCATED;
8740 break;
8741 }
8742 #undef IT_RESET_X_ASCENT_DESCENT
8743 }
8744
8745 #undef BUFFER_POS_REACHED_P
8746
8747 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8748 restore the saved iterator. */
8749 if (atpos_it.sp >= 0)
8750 RESTORE_IT (it, &atpos_it, atpos_data);
8751 else if (atx_it.sp >= 0)
8752 RESTORE_IT (it, &atx_it, atx_data);
8753
8754 done:
8755
8756 if (atpos_data)
8757 bidi_unshelve_cache (atpos_data, 1);
8758 if (atx_data)
8759 bidi_unshelve_cache (atx_data, 1);
8760 if (wrap_data)
8761 bidi_unshelve_cache (wrap_data, 1);
8762 if (ppos_data)
8763 bidi_unshelve_cache (ppos_data, 1);
8764
8765 /* Restore the iterator settings altered at the beginning of this
8766 function. */
8767 it->glyph_row = saved_glyph_row;
8768 return result;
8769 }
8770
8771 /* For external use. */
8772 void
8773 move_it_in_display_line (struct it *it,
8774 ptrdiff_t to_charpos, int to_x,
8775 enum move_operation_enum op)
8776 {
8777 if (it->line_wrap == WORD_WRAP
8778 && (op & MOVE_TO_X))
8779 {
8780 struct it save_it;
8781 void *save_data = NULL;
8782 int skip;
8783
8784 SAVE_IT (save_it, *it, save_data);
8785 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8786 /* When word-wrap is on, TO_X may lie past the end
8787 of a wrapped line. Then it->current is the
8788 character on the next line, so backtrack to the
8789 space before the wrap point. */
8790 if (skip == MOVE_LINE_CONTINUED)
8791 {
8792 int prev_x = max (it->current_x - 1, 0);
8793 RESTORE_IT (it, &save_it, save_data);
8794 move_it_in_display_line_to
8795 (it, -1, prev_x, MOVE_TO_X);
8796 }
8797 else
8798 bidi_unshelve_cache (save_data, 1);
8799 }
8800 else
8801 move_it_in_display_line_to (it, to_charpos, to_x, op);
8802 }
8803
8804
8805 /* Move IT forward until it satisfies one or more of the criteria in
8806 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8807
8808 OP is a bit-mask that specifies where to stop, and in particular,
8809 which of those four position arguments makes a difference. See the
8810 description of enum move_operation_enum.
8811
8812 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8813 screen line, this function will set IT to the next position that is
8814 displayed to the right of TO_CHARPOS on the screen. */
8815
8816 void
8817 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8818 {
8819 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8820 int line_height, line_start_x = 0, reached = 0;
8821 void *backup_data = NULL;
8822
8823 for (;;)
8824 {
8825 if (op & MOVE_TO_VPOS)
8826 {
8827 /* If no TO_CHARPOS and no TO_X specified, stop at the
8828 start of the line TO_VPOS. */
8829 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8830 {
8831 if (it->vpos == to_vpos)
8832 {
8833 reached = 1;
8834 break;
8835 }
8836 else
8837 skip = move_it_in_display_line_to (it, -1, -1, 0);
8838 }
8839 else
8840 {
8841 /* TO_VPOS >= 0 means stop at TO_X in the line at
8842 TO_VPOS, or at TO_POS, whichever comes first. */
8843 if (it->vpos == to_vpos)
8844 {
8845 reached = 2;
8846 break;
8847 }
8848
8849 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8850
8851 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8852 {
8853 reached = 3;
8854 break;
8855 }
8856 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8857 {
8858 /* We have reached TO_X but not in the line we want. */
8859 skip = move_it_in_display_line_to (it, to_charpos,
8860 -1, MOVE_TO_POS);
8861 if (skip == MOVE_POS_MATCH_OR_ZV)
8862 {
8863 reached = 4;
8864 break;
8865 }
8866 }
8867 }
8868 }
8869 else if (op & MOVE_TO_Y)
8870 {
8871 struct it it_backup;
8872
8873 if (it->line_wrap == WORD_WRAP)
8874 SAVE_IT (it_backup, *it, backup_data);
8875
8876 /* TO_Y specified means stop at TO_X in the line containing
8877 TO_Y---or at TO_CHARPOS if this is reached first. The
8878 problem is that we can't really tell whether the line
8879 contains TO_Y before we have completely scanned it, and
8880 this may skip past TO_X. What we do is to first scan to
8881 TO_X.
8882
8883 If TO_X is not specified, use a TO_X of zero. The reason
8884 is to make the outcome of this function more predictable.
8885 If we didn't use TO_X == 0, we would stop at the end of
8886 the line which is probably not what a caller would expect
8887 to happen. */
8888 skip = move_it_in_display_line_to
8889 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8890 (MOVE_TO_X | (op & MOVE_TO_POS)));
8891
8892 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8893 if (skip == MOVE_POS_MATCH_OR_ZV)
8894 reached = 5;
8895 else if (skip == MOVE_X_REACHED)
8896 {
8897 /* If TO_X was reached, we want to know whether TO_Y is
8898 in the line. We know this is the case if the already
8899 scanned glyphs make the line tall enough. Otherwise,
8900 we must check by scanning the rest of the line. */
8901 line_height = it->max_ascent + it->max_descent;
8902 if (to_y >= it->current_y
8903 && to_y < it->current_y + line_height)
8904 {
8905 reached = 6;
8906 break;
8907 }
8908 SAVE_IT (it_backup, *it, backup_data);
8909 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8910 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8911 op & MOVE_TO_POS);
8912 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8913 line_height = it->max_ascent + it->max_descent;
8914 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8915
8916 if (to_y >= it->current_y
8917 && to_y < it->current_y + line_height)
8918 {
8919 /* If TO_Y is in this line and TO_X was reached
8920 above, we scanned too far. We have to restore
8921 IT's settings to the ones before skipping. But
8922 keep the more accurate values of max_ascent and
8923 max_descent we've found while skipping the rest
8924 of the line, for the sake of callers, such as
8925 pos_visible_p, that need to know the line
8926 height. */
8927 int max_ascent = it->max_ascent;
8928 int max_descent = it->max_descent;
8929
8930 RESTORE_IT (it, &it_backup, backup_data);
8931 it->max_ascent = max_ascent;
8932 it->max_descent = max_descent;
8933 reached = 6;
8934 }
8935 else
8936 {
8937 skip = skip2;
8938 if (skip == MOVE_POS_MATCH_OR_ZV)
8939 reached = 7;
8940 }
8941 }
8942 else
8943 {
8944 /* Check whether TO_Y is in this line. */
8945 line_height = it->max_ascent + it->max_descent;
8946 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8947
8948 if (to_y >= it->current_y
8949 && to_y < it->current_y + line_height)
8950 {
8951 /* When word-wrap is on, TO_X may lie past the end
8952 of a wrapped line. Then it->current is the
8953 character on the next line, so backtrack to the
8954 space before the wrap point. */
8955 if (skip == MOVE_LINE_CONTINUED
8956 && it->line_wrap == WORD_WRAP)
8957 {
8958 int prev_x = max (it->current_x - 1, 0);
8959 RESTORE_IT (it, &it_backup, backup_data);
8960 skip = move_it_in_display_line_to
8961 (it, -1, prev_x, MOVE_TO_X);
8962 }
8963 reached = 6;
8964 }
8965 }
8966
8967 if (reached)
8968 break;
8969 }
8970 else if (BUFFERP (it->object)
8971 && (it->method == GET_FROM_BUFFER
8972 || it->method == GET_FROM_STRETCH)
8973 && IT_CHARPOS (*it) >= to_charpos
8974 /* Under bidi iteration, a call to set_iterator_to_next
8975 can scan far beyond to_charpos if the initial
8976 portion of the next line needs to be reordered. In
8977 that case, give move_it_in_display_line_to another
8978 chance below. */
8979 && !(it->bidi_p
8980 && it->bidi_it.scan_dir == -1))
8981 skip = MOVE_POS_MATCH_OR_ZV;
8982 else
8983 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8984
8985 switch (skip)
8986 {
8987 case MOVE_POS_MATCH_OR_ZV:
8988 reached = 8;
8989 goto out;
8990
8991 case MOVE_NEWLINE_OR_CR:
8992 set_iterator_to_next (it, 1);
8993 it->continuation_lines_width = 0;
8994 break;
8995
8996 case MOVE_LINE_TRUNCATED:
8997 it->continuation_lines_width = 0;
8998 reseat_at_next_visible_line_start (it, 0);
8999 if ((op & MOVE_TO_POS) != 0
9000 && IT_CHARPOS (*it) > to_charpos)
9001 {
9002 reached = 9;
9003 goto out;
9004 }
9005 break;
9006
9007 case MOVE_LINE_CONTINUED:
9008 /* For continued lines ending in a tab, some of the glyphs
9009 associated with the tab are displayed on the current
9010 line. Since it->current_x does not include these glyphs,
9011 we use it->last_visible_x instead. */
9012 if (it->c == '\t')
9013 {
9014 it->continuation_lines_width += it->last_visible_x;
9015 /* When moving by vpos, ensure that the iterator really
9016 advances to the next line (bug#847, bug#969). Fixme:
9017 do we need to do this in other circumstances? */
9018 if (it->current_x != it->last_visible_x
9019 && (op & MOVE_TO_VPOS)
9020 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
9021 {
9022 line_start_x = it->current_x + it->pixel_width
9023 - it->last_visible_x;
9024 set_iterator_to_next (it, 0);
9025 }
9026 }
9027 else
9028 it->continuation_lines_width += it->current_x;
9029 break;
9030
9031 default:
9032 emacs_abort ();
9033 }
9034
9035 /* Reset/increment for the next run. */
9036 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9037 it->current_x = line_start_x;
9038 line_start_x = 0;
9039 it->hpos = 0;
9040 it->current_y += it->max_ascent + it->max_descent;
9041 ++it->vpos;
9042 last_height = it->max_ascent + it->max_descent;
9043 it->max_ascent = it->max_descent = 0;
9044 }
9045
9046 out:
9047
9048 /* On text terminals, we may stop at the end of a line in the middle
9049 of a multi-character glyph. If the glyph itself is continued,
9050 i.e. it is actually displayed on the next line, don't treat this
9051 stopping point as valid; move to the next line instead (unless
9052 that brings us offscreen). */
9053 if (!FRAME_WINDOW_P (it->f)
9054 && op & MOVE_TO_POS
9055 && IT_CHARPOS (*it) == to_charpos
9056 && it->what == IT_CHARACTER
9057 && it->nglyphs > 1
9058 && it->line_wrap == WINDOW_WRAP
9059 && it->current_x == it->last_visible_x - 1
9060 && it->c != '\n'
9061 && it->c != '\t'
9062 && it->vpos < XFASTINT (it->w->window_end_vpos))
9063 {
9064 it->continuation_lines_width += it->current_x;
9065 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9066 it->current_y += it->max_ascent + it->max_descent;
9067 ++it->vpos;
9068 last_height = it->max_ascent + it->max_descent;
9069 }
9070
9071 if (backup_data)
9072 bidi_unshelve_cache (backup_data, 1);
9073
9074 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9075 }
9076
9077
9078 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9079
9080 If DY > 0, move IT backward at least that many pixels. DY = 0
9081 means move IT backward to the preceding line start or BEGV. This
9082 function may move over more than DY pixels if IT->current_y - DY
9083 ends up in the middle of a line; in this case IT->current_y will be
9084 set to the top of the line moved to. */
9085
9086 void
9087 move_it_vertically_backward (struct it *it, int dy)
9088 {
9089 int nlines, h;
9090 struct it it2, it3;
9091 void *it2data = NULL, *it3data = NULL;
9092 ptrdiff_t start_pos;
9093 int nchars_per_row
9094 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9095 ptrdiff_t pos_limit;
9096
9097 move_further_back:
9098 eassert (dy >= 0);
9099
9100 start_pos = IT_CHARPOS (*it);
9101
9102 /* Estimate how many newlines we must move back. */
9103 nlines = max (1, dy / default_line_pixel_height (it->w));
9104 if (it->line_wrap == TRUNCATE)
9105 pos_limit = BEGV;
9106 else
9107 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9108
9109 /* Set the iterator's position that many lines back. But don't go
9110 back more than NLINES full screen lines -- this wins a day with
9111 buffers which have very long lines. */
9112 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9113 back_to_previous_visible_line_start (it);
9114
9115 /* Reseat the iterator here. When moving backward, we don't want
9116 reseat to skip forward over invisible text, set up the iterator
9117 to deliver from overlay strings at the new position etc. So,
9118 use reseat_1 here. */
9119 reseat_1 (it, it->current.pos, 1);
9120
9121 /* We are now surely at a line start. */
9122 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9123 reordering is in effect. */
9124 it->continuation_lines_width = 0;
9125
9126 /* Move forward and see what y-distance we moved. First move to the
9127 start of the next line so that we get its height. We need this
9128 height to be able to tell whether we reached the specified
9129 y-distance. */
9130 SAVE_IT (it2, *it, it2data);
9131 it2.max_ascent = it2.max_descent = 0;
9132 do
9133 {
9134 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9135 MOVE_TO_POS | MOVE_TO_VPOS);
9136 }
9137 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9138 /* If we are in a display string which starts at START_POS,
9139 and that display string includes a newline, and we are
9140 right after that newline (i.e. at the beginning of a
9141 display line), exit the loop, because otherwise we will
9142 infloop, since move_it_to will see that it is already at
9143 START_POS and will not move. */
9144 || (it2.method == GET_FROM_STRING
9145 && IT_CHARPOS (it2) == start_pos
9146 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9147 eassert (IT_CHARPOS (*it) >= BEGV);
9148 SAVE_IT (it3, it2, it3data);
9149
9150 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9151 eassert (IT_CHARPOS (*it) >= BEGV);
9152 /* H is the actual vertical distance from the position in *IT
9153 and the starting position. */
9154 h = it2.current_y - it->current_y;
9155 /* NLINES is the distance in number of lines. */
9156 nlines = it2.vpos - it->vpos;
9157
9158 /* Correct IT's y and vpos position
9159 so that they are relative to the starting point. */
9160 it->vpos -= nlines;
9161 it->current_y -= h;
9162
9163 if (dy == 0)
9164 {
9165 /* DY == 0 means move to the start of the screen line. The
9166 value of nlines is > 0 if continuation lines were involved,
9167 or if the original IT position was at start of a line. */
9168 RESTORE_IT (it, it, it2data);
9169 if (nlines > 0)
9170 move_it_by_lines (it, nlines);
9171 /* The above code moves us to some position NLINES down,
9172 usually to its first glyph (leftmost in an L2R line), but
9173 that's not necessarily the start of the line, under bidi
9174 reordering. We want to get to the character position
9175 that is immediately after the newline of the previous
9176 line. */
9177 if (it->bidi_p
9178 && !it->continuation_lines_width
9179 && !STRINGP (it->string)
9180 && IT_CHARPOS (*it) > BEGV
9181 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9182 {
9183 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9184
9185 DEC_BOTH (cp, bp);
9186 cp = find_newline_no_quit (cp, bp, -1, NULL);
9187 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9188 }
9189 bidi_unshelve_cache (it3data, 1);
9190 }
9191 else
9192 {
9193 /* The y-position we try to reach, relative to *IT.
9194 Note that H has been subtracted in front of the if-statement. */
9195 int target_y = it->current_y + h - dy;
9196 int y0 = it3.current_y;
9197 int y1;
9198 int line_height;
9199
9200 RESTORE_IT (&it3, &it3, it3data);
9201 y1 = line_bottom_y (&it3);
9202 line_height = y1 - y0;
9203 RESTORE_IT (it, it, it2data);
9204 /* If we did not reach target_y, try to move further backward if
9205 we can. If we moved too far backward, try to move forward. */
9206 if (target_y < it->current_y
9207 /* This is heuristic. In a window that's 3 lines high, with
9208 a line height of 13 pixels each, recentering with point
9209 on the bottom line will try to move -39/2 = 19 pixels
9210 backward. Try to avoid moving into the first line. */
9211 && (it->current_y - target_y
9212 > min (window_box_height (it->w), line_height * 2 / 3))
9213 && IT_CHARPOS (*it) > BEGV)
9214 {
9215 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9216 target_y - it->current_y));
9217 dy = it->current_y - target_y;
9218 goto move_further_back;
9219 }
9220 else if (target_y >= it->current_y + line_height
9221 && IT_CHARPOS (*it) < ZV)
9222 {
9223 /* Should move forward by at least one line, maybe more.
9224
9225 Note: Calling move_it_by_lines can be expensive on
9226 terminal frames, where compute_motion is used (via
9227 vmotion) to do the job, when there are very long lines
9228 and truncate-lines is nil. That's the reason for
9229 treating terminal frames specially here. */
9230
9231 if (!FRAME_WINDOW_P (it->f))
9232 move_it_vertically (it, target_y - (it->current_y + line_height));
9233 else
9234 {
9235 do
9236 {
9237 move_it_by_lines (it, 1);
9238 }
9239 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9240 }
9241 }
9242 }
9243 }
9244
9245
9246 /* Move IT by a specified amount of pixel lines DY. DY negative means
9247 move backwards. DY = 0 means move to start of screen line. At the
9248 end, IT will be on the start of a screen line. */
9249
9250 void
9251 move_it_vertically (struct it *it, int dy)
9252 {
9253 if (dy <= 0)
9254 move_it_vertically_backward (it, -dy);
9255 else
9256 {
9257 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9258 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9259 MOVE_TO_POS | MOVE_TO_Y);
9260 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9261
9262 /* If buffer ends in ZV without a newline, move to the start of
9263 the line to satisfy the post-condition. */
9264 if (IT_CHARPOS (*it) == ZV
9265 && ZV > BEGV
9266 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9267 move_it_by_lines (it, 0);
9268 }
9269 }
9270
9271
9272 /* Move iterator IT past the end of the text line it is in. */
9273
9274 void
9275 move_it_past_eol (struct it *it)
9276 {
9277 enum move_it_result rc;
9278
9279 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9280 if (rc == MOVE_NEWLINE_OR_CR)
9281 set_iterator_to_next (it, 0);
9282 }
9283
9284
9285 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9286 negative means move up. DVPOS == 0 means move to the start of the
9287 screen line.
9288
9289 Optimization idea: If we would know that IT->f doesn't use
9290 a face with proportional font, we could be faster for
9291 truncate-lines nil. */
9292
9293 void
9294 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9295 {
9296
9297 /* The commented-out optimization uses vmotion on terminals. This
9298 gives bad results, because elements like it->what, on which
9299 callers such as pos_visible_p rely, aren't updated. */
9300 /* struct position pos;
9301 if (!FRAME_WINDOW_P (it->f))
9302 {
9303 struct text_pos textpos;
9304
9305 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9306 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9307 reseat (it, textpos, 1);
9308 it->vpos += pos.vpos;
9309 it->current_y += pos.vpos;
9310 }
9311 else */
9312
9313 if (dvpos == 0)
9314 {
9315 /* DVPOS == 0 means move to the start of the screen line. */
9316 move_it_vertically_backward (it, 0);
9317 /* Let next call to line_bottom_y calculate real line height */
9318 last_height = 0;
9319 }
9320 else if (dvpos > 0)
9321 {
9322 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9323 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9324 {
9325 /* Only move to the next buffer position if we ended up in a
9326 string from display property, not in an overlay string
9327 (before-string or after-string). That is because the
9328 latter don't conceal the underlying buffer position, so
9329 we can ask to move the iterator to the exact position we
9330 are interested in. Note that, even if we are already at
9331 IT_CHARPOS (*it), the call below is not a no-op, as it
9332 will detect that we are at the end of the string, pop the
9333 iterator, and compute it->current_x and it->hpos
9334 correctly. */
9335 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9336 -1, -1, -1, MOVE_TO_POS);
9337 }
9338 }
9339 else
9340 {
9341 struct it it2;
9342 void *it2data = NULL;
9343 ptrdiff_t start_charpos, i;
9344 int nchars_per_row
9345 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9346 ptrdiff_t pos_limit;
9347
9348 /* Start at the beginning of the screen line containing IT's
9349 position. This may actually move vertically backwards,
9350 in case of overlays, so adjust dvpos accordingly. */
9351 dvpos += it->vpos;
9352 move_it_vertically_backward (it, 0);
9353 dvpos -= it->vpos;
9354
9355 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9356 screen lines, and reseat the iterator there. */
9357 start_charpos = IT_CHARPOS (*it);
9358 if (it->line_wrap == TRUNCATE)
9359 pos_limit = BEGV;
9360 else
9361 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9362 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9363 back_to_previous_visible_line_start (it);
9364 reseat (it, it->current.pos, 1);
9365
9366 /* Move further back if we end up in a string or an image. */
9367 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9368 {
9369 /* First try to move to start of display line. */
9370 dvpos += it->vpos;
9371 move_it_vertically_backward (it, 0);
9372 dvpos -= it->vpos;
9373 if (IT_POS_VALID_AFTER_MOVE_P (it))
9374 break;
9375 /* If start of line is still in string or image,
9376 move further back. */
9377 back_to_previous_visible_line_start (it);
9378 reseat (it, it->current.pos, 1);
9379 dvpos--;
9380 }
9381
9382 it->current_x = it->hpos = 0;
9383
9384 /* Above call may have moved too far if continuation lines
9385 are involved. Scan forward and see if it did. */
9386 SAVE_IT (it2, *it, it2data);
9387 it2.vpos = it2.current_y = 0;
9388 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9389 it->vpos -= it2.vpos;
9390 it->current_y -= it2.current_y;
9391 it->current_x = it->hpos = 0;
9392
9393 /* If we moved too far back, move IT some lines forward. */
9394 if (it2.vpos > -dvpos)
9395 {
9396 int delta = it2.vpos + dvpos;
9397
9398 RESTORE_IT (&it2, &it2, it2data);
9399 SAVE_IT (it2, *it, it2data);
9400 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9401 /* Move back again if we got too far ahead. */
9402 if (IT_CHARPOS (*it) >= start_charpos)
9403 RESTORE_IT (it, &it2, it2data);
9404 else
9405 bidi_unshelve_cache (it2data, 1);
9406 }
9407 else
9408 RESTORE_IT (it, it, it2data);
9409 }
9410 }
9411
9412 /* Return 1 if IT points into the middle of a display vector. */
9413
9414 int
9415 in_display_vector_p (struct it *it)
9416 {
9417 return (it->method == GET_FROM_DISPLAY_VECTOR
9418 && it->current.dpvec_index > 0
9419 && it->dpvec + it->current.dpvec_index != it->dpend);
9420 }
9421
9422 \f
9423 /***********************************************************************
9424 Messages
9425 ***********************************************************************/
9426
9427
9428 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9429 to *Messages*. */
9430
9431 void
9432 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9433 {
9434 Lisp_Object args[3];
9435 Lisp_Object msg, fmt;
9436 char *buffer;
9437 ptrdiff_t len;
9438 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9439 USE_SAFE_ALLOCA;
9440
9441 fmt = msg = Qnil;
9442 GCPRO4 (fmt, msg, arg1, arg2);
9443
9444 args[0] = fmt = build_string (format);
9445 args[1] = arg1;
9446 args[2] = arg2;
9447 msg = Fformat (3, args);
9448
9449 len = SBYTES (msg) + 1;
9450 buffer = SAFE_ALLOCA (len);
9451 memcpy (buffer, SDATA (msg), len);
9452
9453 message_dolog (buffer, len - 1, 1, 0);
9454 SAFE_FREE ();
9455
9456 UNGCPRO;
9457 }
9458
9459
9460 /* Output a newline in the *Messages* buffer if "needs" one. */
9461
9462 void
9463 message_log_maybe_newline (void)
9464 {
9465 if (message_log_need_newline)
9466 message_dolog ("", 0, 1, 0);
9467 }
9468
9469
9470 /* Add a string M of length NBYTES to the message log, optionally
9471 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9472 true, means interpret the contents of M as multibyte. This
9473 function calls low-level routines in order to bypass text property
9474 hooks, etc. which might not be safe to run.
9475
9476 This may GC (insert may run before/after change hooks),
9477 so the buffer M must NOT point to a Lisp string. */
9478
9479 void
9480 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9481 {
9482 const unsigned char *msg = (const unsigned char *) m;
9483
9484 if (!NILP (Vmemory_full))
9485 return;
9486
9487 if (!NILP (Vmessage_log_max))
9488 {
9489 struct buffer *oldbuf;
9490 Lisp_Object oldpoint, oldbegv, oldzv;
9491 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9492 ptrdiff_t point_at_end = 0;
9493 ptrdiff_t zv_at_end = 0;
9494 Lisp_Object old_deactivate_mark;
9495 bool shown;
9496 struct gcpro gcpro1;
9497
9498 old_deactivate_mark = Vdeactivate_mark;
9499 oldbuf = current_buffer;
9500 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9501 bset_undo_list (current_buffer, Qt);
9502
9503 oldpoint = message_dolog_marker1;
9504 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9505 oldbegv = message_dolog_marker2;
9506 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9507 oldzv = message_dolog_marker3;
9508 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9509 GCPRO1 (old_deactivate_mark);
9510
9511 if (PT == Z)
9512 point_at_end = 1;
9513 if (ZV == Z)
9514 zv_at_end = 1;
9515
9516 BEGV = BEG;
9517 BEGV_BYTE = BEG_BYTE;
9518 ZV = Z;
9519 ZV_BYTE = Z_BYTE;
9520 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9521
9522 /* Insert the string--maybe converting multibyte to single byte
9523 or vice versa, so that all the text fits the buffer. */
9524 if (multibyte
9525 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9526 {
9527 ptrdiff_t i;
9528 int c, char_bytes;
9529 char work[1];
9530
9531 /* Convert a multibyte string to single-byte
9532 for the *Message* buffer. */
9533 for (i = 0; i < nbytes; i += char_bytes)
9534 {
9535 c = string_char_and_length (msg + i, &char_bytes);
9536 work[0] = (ASCII_CHAR_P (c)
9537 ? c
9538 : multibyte_char_to_unibyte (c));
9539 insert_1_both (work, 1, 1, 1, 0, 0);
9540 }
9541 }
9542 else if (! multibyte
9543 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9544 {
9545 ptrdiff_t i;
9546 int c, char_bytes;
9547 unsigned char str[MAX_MULTIBYTE_LENGTH];
9548 /* Convert a single-byte string to multibyte
9549 for the *Message* buffer. */
9550 for (i = 0; i < nbytes; i++)
9551 {
9552 c = msg[i];
9553 MAKE_CHAR_MULTIBYTE (c);
9554 char_bytes = CHAR_STRING (c, str);
9555 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9556 }
9557 }
9558 else if (nbytes)
9559 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9560
9561 if (nlflag)
9562 {
9563 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9564 printmax_t dups;
9565
9566 insert_1_both ("\n", 1, 1, 1, 0, 0);
9567
9568 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9569 this_bol = PT;
9570 this_bol_byte = PT_BYTE;
9571
9572 /* See if this line duplicates the previous one.
9573 If so, combine duplicates. */
9574 if (this_bol > BEG)
9575 {
9576 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9577 prev_bol = PT;
9578 prev_bol_byte = PT_BYTE;
9579
9580 dups = message_log_check_duplicate (prev_bol_byte,
9581 this_bol_byte);
9582 if (dups)
9583 {
9584 del_range_both (prev_bol, prev_bol_byte,
9585 this_bol, this_bol_byte, 0);
9586 if (dups > 1)
9587 {
9588 char dupstr[sizeof " [ times]"
9589 + INT_STRLEN_BOUND (printmax_t)];
9590
9591 /* If you change this format, don't forget to also
9592 change message_log_check_duplicate. */
9593 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9594 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9595 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9596 }
9597 }
9598 }
9599
9600 /* If we have more than the desired maximum number of lines
9601 in the *Messages* buffer now, delete the oldest ones.
9602 This is safe because we don't have undo in this buffer. */
9603
9604 if (NATNUMP (Vmessage_log_max))
9605 {
9606 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9607 -XFASTINT (Vmessage_log_max) - 1, 0);
9608 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9609 }
9610 }
9611 BEGV = marker_position (oldbegv);
9612 BEGV_BYTE = marker_byte_position (oldbegv);
9613
9614 if (zv_at_end)
9615 {
9616 ZV = Z;
9617 ZV_BYTE = Z_BYTE;
9618 }
9619 else
9620 {
9621 ZV = marker_position (oldzv);
9622 ZV_BYTE = marker_byte_position (oldzv);
9623 }
9624
9625 if (point_at_end)
9626 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9627 else
9628 /* We can't do Fgoto_char (oldpoint) because it will run some
9629 Lisp code. */
9630 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9631 marker_byte_position (oldpoint));
9632
9633 UNGCPRO;
9634 unchain_marker (XMARKER (oldpoint));
9635 unchain_marker (XMARKER (oldbegv));
9636 unchain_marker (XMARKER (oldzv));
9637
9638 shown = buffer_window_count (current_buffer) > 0;
9639 set_buffer_internal (oldbuf);
9640 /* We called insert_1_both above with its 5th argument (PREPARE)
9641 zero, which prevents insert_1_both from calling
9642 prepare_to_modify_buffer, which in turns prevents us from
9643 incrementing windows_or_buffers_changed even if *Messages* is
9644 shown in some window. So we must manually incrementing
9645 windows_or_buffers_changed here to make up for that. */
9646 if (shown)
9647 windows_or_buffers_changed++;
9648 else
9649 windows_or_buffers_changed = old_windows_or_buffers_changed;
9650 message_log_need_newline = !nlflag;
9651 Vdeactivate_mark = old_deactivate_mark;
9652 }
9653 }
9654
9655
9656 /* We are at the end of the buffer after just having inserted a newline.
9657 (Note: We depend on the fact we won't be crossing the gap.)
9658 Check to see if the most recent message looks a lot like the previous one.
9659 Return 0 if different, 1 if the new one should just replace it, or a
9660 value N > 1 if we should also append " [N times]". */
9661
9662 static intmax_t
9663 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9664 {
9665 ptrdiff_t i;
9666 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9667 int seen_dots = 0;
9668 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9669 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9670
9671 for (i = 0; i < len; i++)
9672 {
9673 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9674 seen_dots = 1;
9675 if (p1[i] != p2[i])
9676 return seen_dots;
9677 }
9678 p1 += len;
9679 if (*p1 == '\n')
9680 return 2;
9681 if (*p1++ == ' ' && *p1++ == '[')
9682 {
9683 char *pend;
9684 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9685 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9686 return n + 1;
9687 }
9688 return 0;
9689 }
9690 \f
9691
9692 /* Display an echo area message M with a specified length of NBYTES
9693 bytes. The string may include null characters. If M is not a
9694 string, clear out any existing message, and let the mini-buffer
9695 text show through.
9696
9697 This function cancels echoing. */
9698
9699 void
9700 message3 (Lisp_Object m)
9701 {
9702 struct gcpro gcpro1;
9703
9704 GCPRO1 (m);
9705 clear_message (1,1);
9706 cancel_echoing ();
9707
9708 /* First flush out any partial line written with print. */
9709 message_log_maybe_newline ();
9710 if (STRINGP (m))
9711 {
9712 ptrdiff_t nbytes = SBYTES (m);
9713 bool multibyte = STRING_MULTIBYTE (m);
9714 USE_SAFE_ALLOCA;
9715 char *buffer = SAFE_ALLOCA (nbytes);
9716 memcpy (buffer, SDATA (m), nbytes);
9717 message_dolog (buffer, nbytes, 1, multibyte);
9718 SAFE_FREE ();
9719 }
9720 message3_nolog (m);
9721
9722 UNGCPRO;
9723 }
9724
9725
9726 /* The non-logging version of message3.
9727 This does not cancel echoing, because it is used for echoing.
9728 Perhaps we need to make a separate function for echoing
9729 and make this cancel echoing. */
9730
9731 void
9732 message3_nolog (Lisp_Object m)
9733 {
9734 struct frame *sf = SELECTED_FRAME ();
9735
9736 if (FRAME_INITIAL_P (sf))
9737 {
9738 if (noninteractive_need_newline)
9739 putc ('\n', stderr);
9740 noninteractive_need_newline = 0;
9741 if (STRINGP (m))
9742 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9743 if (cursor_in_echo_area == 0)
9744 fprintf (stderr, "\n");
9745 fflush (stderr);
9746 }
9747 /* Error messages get reported properly by cmd_error, so this must be just an
9748 informative message; if the frame hasn't really been initialized yet, just
9749 toss it. */
9750 else if (INTERACTIVE && sf->glyphs_initialized_p)
9751 {
9752 /* Get the frame containing the mini-buffer
9753 that the selected frame is using. */
9754 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9755 Lisp_Object frame = XWINDOW (mini_window)->frame;
9756 struct frame *f = XFRAME (frame);
9757
9758 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9759 Fmake_frame_visible (frame);
9760
9761 if (STRINGP (m) && SCHARS (m) > 0)
9762 {
9763 set_message (m);
9764 if (minibuffer_auto_raise)
9765 Fraise_frame (frame);
9766 /* Assume we are not echoing.
9767 (If we are, echo_now will override this.) */
9768 echo_message_buffer = Qnil;
9769 }
9770 else
9771 clear_message (1, 1);
9772
9773 do_pending_window_change (0);
9774 echo_area_display (1);
9775 do_pending_window_change (0);
9776 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9777 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9778 }
9779 }
9780
9781
9782 /* Display a null-terminated echo area message M. If M is 0, clear
9783 out any existing message, and let the mini-buffer text show through.
9784
9785 The buffer M must continue to exist until after the echo area gets
9786 cleared or some other message gets displayed there. Do not pass
9787 text that is stored in a Lisp string. Do not pass text in a buffer
9788 that was alloca'd. */
9789
9790 void
9791 message1 (const char *m)
9792 {
9793 message3 (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9794 }
9795
9796
9797 /* The non-logging counterpart of message1. */
9798
9799 void
9800 message1_nolog (const char *m)
9801 {
9802 message3_nolog (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9803 }
9804
9805 /* Display a message M which contains a single %s
9806 which gets replaced with STRING. */
9807
9808 void
9809 message_with_string (const char *m, Lisp_Object string, int log)
9810 {
9811 CHECK_STRING (string);
9812
9813 if (noninteractive)
9814 {
9815 if (m)
9816 {
9817 if (noninteractive_need_newline)
9818 putc ('\n', stderr);
9819 noninteractive_need_newline = 0;
9820 fprintf (stderr, m, SDATA (string));
9821 if (!cursor_in_echo_area)
9822 fprintf (stderr, "\n");
9823 fflush (stderr);
9824 }
9825 }
9826 else if (INTERACTIVE)
9827 {
9828 /* The frame whose minibuffer we're going to display the message on.
9829 It may be larger than the selected frame, so we need
9830 to use its buffer, not the selected frame's buffer. */
9831 Lisp_Object mini_window;
9832 struct frame *f, *sf = SELECTED_FRAME ();
9833
9834 /* Get the frame containing the minibuffer
9835 that the selected frame is using. */
9836 mini_window = FRAME_MINIBUF_WINDOW (sf);
9837 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9838
9839 /* Error messages get reported properly by cmd_error, so this must be
9840 just an informative message; if the frame hasn't really been
9841 initialized yet, just toss it. */
9842 if (f->glyphs_initialized_p)
9843 {
9844 Lisp_Object args[2], msg;
9845 struct gcpro gcpro1, gcpro2;
9846
9847 args[0] = build_string (m);
9848 args[1] = msg = string;
9849 GCPRO2 (args[0], msg);
9850 gcpro1.nvars = 2;
9851
9852 msg = Fformat (2, args);
9853
9854 if (log)
9855 message3 (msg);
9856 else
9857 message3_nolog (msg);
9858
9859 UNGCPRO;
9860
9861 /* Print should start at the beginning of the message
9862 buffer next time. */
9863 message_buf_print = 0;
9864 }
9865 }
9866 }
9867
9868
9869 /* Dump an informative message to the minibuf. If M is 0, clear out
9870 any existing message, and let the mini-buffer text show through. */
9871
9872 static void
9873 vmessage (const char *m, va_list ap)
9874 {
9875 if (noninteractive)
9876 {
9877 if (m)
9878 {
9879 if (noninteractive_need_newline)
9880 putc ('\n', stderr);
9881 noninteractive_need_newline = 0;
9882 vfprintf (stderr, m, ap);
9883 if (cursor_in_echo_area == 0)
9884 fprintf (stderr, "\n");
9885 fflush (stderr);
9886 }
9887 }
9888 else if (INTERACTIVE)
9889 {
9890 /* The frame whose mini-buffer we're going to display the message
9891 on. It may be larger than the selected frame, so we need to
9892 use its buffer, not the selected frame's buffer. */
9893 Lisp_Object mini_window;
9894 struct frame *f, *sf = SELECTED_FRAME ();
9895
9896 /* Get the frame containing the mini-buffer
9897 that the selected frame is using. */
9898 mini_window = FRAME_MINIBUF_WINDOW (sf);
9899 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9900
9901 /* Error messages get reported properly by cmd_error, so this must be
9902 just an informative message; if the frame hasn't really been
9903 initialized yet, just toss it. */
9904 if (f->glyphs_initialized_p)
9905 {
9906 if (m)
9907 {
9908 ptrdiff_t len;
9909 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9910 char *message_buf = alloca (maxsize + 1);
9911
9912 len = doprnt (message_buf, maxsize, m, (char *)0, ap);
9913
9914 message3 (make_string (message_buf, len));
9915 }
9916 else
9917 message1 (0);
9918
9919 /* Print should start at the beginning of the message
9920 buffer next time. */
9921 message_buf_print = 0;
9922 }
9923 }
9924 }
9925
9926 void
9927 message (const char *m, ...)
9928 {
9929 va_list ap;
9930 va_start (ap, m);
9931 vmessage (m, ap);
9932 va_end (ap);
9933 }
9934
9935
9936 #if 0
9937 /* The non-logging version of message. */
9938
9939 void
9940 message_nolog (const char *m, ...)
9941 {
9942 Lisp_Object old_log_max;
9943 va_list ap;
9944 va_start (ap, m);
9945 old_log_max = Vmessage_log_max;
9946 Vmessage_log_max = Qnil;
9947 vmessage (m, ap);
9948 Vmessage_log_max = old_log_max;
9949 va_end (ap);
9950 }
9951 #endif
9952
9953
9954 /* Display the current message in the current mini-buffer. This is
9955 only called from error handlers in process.c, and is not time
9956 critical. */
9957
9958 void
9959 update_echo_area (void)
9960 {
9961 if (!NILP (echo_area_buffer[0]))
9962 {
9963 Lisp_Object string;
9964 string = Fcurrent_message ();
9965 message3 (string);
9966 }
9967 }
9968
9969
9970 /* Make sure echo area buffers in `echo_buffers' are live.
9971 If they aren't, make new ones. */
9972
9973 static void
9974 ensure_echo_area_buffers (void)
9975 {
9976 int i;
9977
9978 for (i = 0; i < 2; ++i)
9979 if (!BUFFERP (echo_buffer[i])
9980 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9981 {
9982 char name[30];
9983 Lisp_Object old_buffer;
9984 int j;
9985
9986 old_buffer = echo_buffer[i];
9987 echo_buffer[i] = Fget_buffer_create
9988 (make_formatted_string (name, " *Echo Area %d*", i));
9989 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9990 /* to force word wrap in echo area -
9991 it was decided to postpone this*/
9992 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9993
9994 for (j = 0; j < 2; ++j)
9995 if (EQ (old_buffer, echo_area_buffer[j]))
9996 echo_area_buffer[j] = echo_buffer[i];
9997 }
9998 }
9999
10000
10001 /* Call FN with args A1..A2 with either the current or last displayed
10002 echo_area_buffer as current buffer.
10003
10004 WHICH zero means use the current message buffer
10005 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10006 from echo_buffer[] and clear it.
10007
10008 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10009 suitable buffer from echo_buffer[] and clear it.
10010
10011 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10012 that the current message becomes the last displayed one, make
10013 choose a suitable buffer for echo_area_buffer[0], and clear it.
10014
10015 Value is what FN returns. */
10016
10017 static int
10018 with_echo_area_buffer (struct window *w, int which,
10019 int (*fn) (ptrdiff_t, Lisp_Object),
10020 ptrdiff_t a1, Lisp_Object a2)
10021 {
10022 Lisp_Object buffer;
10023 int this_one, the_other, clear_buffer_p, rc;
10024 ptrdiff_t count = SPECPDL_INDEX ();
10025
10026 /* If buffers aren't live, make new ones. */
10027 ensure_echo_area_buffers ();
10028
10029 clear_buffer_p = 0;
10030
10031 if (which == 0)
10032 this_one = 0, the_other = 1;
10033 else if (which > 0)
10034 this_one = 1, the_other = 0;
10035 else
10036 {
10037 this_one = 0, the_other = 1;
10038 clear_buffer_p = 1;
10039
10040 /* We need a fresh one in case the current echo buffer equals
10041 the one containing the last displayed echo area message. */
10042 if (!NILP (echo_area_buffer[this_one])
10043 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10044 echo_area_buffer[this_one] = Qnil;
10045 }
10046
10047 /* Choose a suitable buffer from echo_buffer[] is we don't
10048 have one. */
10049 if (NILP (echo_area_buffer[this_one]))
10050 {
10051 echo_area_buffer[this_one]
10052 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10053 ? echo_buffer[the_other]
10054 : echo_buffer[this_one]);
10055 clear_buffer_p = 1;
10056 }
10057
10058 buffer = echo_area_buffer[this_one];
10059
10060 /* Don't get confused by reusing the buffer used for echoing
10061 for a different purpose. */
10062 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10063 cancel_echoing ();
10064
10065 record_unwind_protect (unwind_with_echo_area_buffer,
10066 with_echo_area_buffer_unwind_data (w));
10067
10068 /* Make the echo area buffer current. Note that for display
10069 purposes, it is not necessary that the displayed window's buffer
10070 == current_buffer, except for text property lookup. So, let's
10071 only set that buffer temporarily here without doing a full
10072 Fset_window_buffer. We must also change w->pointm, though,
10073 because otherwise an assertions in unshow_buffer fails, and Emacs
10074 aborts. */
10075 set_buffer_internal_1 (XBUFFER (buffer));
10076 if (w)
10077 {
10078 wset_buffer (w, buffer);
10079 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10080 }
10081
10082 bset_undo_list (current_buffer, Qt);
10083 bset_read_only (current_buffer, Qnil);
10084 specbind (Qinhibit_read_only, Qt);
10085 specbind (Qinhibit_modification_hooks, Qt);
10086
10087 if (clear_buffer_p && Z > BEG)
10088 del_range (BEG, Z);
10089
10090 eassert (BEGV >= BEG);
10091 eassert (ZV <= Z && ZV >= BEGV);
10092
10093 rc = fn (a1, a2);
10094
10095 eassert (BEGV >= BEG);
10096 eassert (ZV <= Z && ZV >= BEGV);
10097
10098 unbind_to (count, Qnil);
10099 return rc;
10100 }
10101
10102
10103 /* Save state that should be preserved around the call to the function
10104 FN called in with_echo_area_buffer. */
10105
10106 static Lisp_Object
10107 with_echo_area_buffer_unwind_data (struct window *w)
10108 {
10109 int i = 0;
10110 Lisp_Object vector, tmp;
10111
10112 /* Reduce consing by keeping one vector in
10113 Vwith_echo_area_save_vector. */
10114 vector = Vwith_echo_area_save_vector;
10115 Vwith_echo_area_save_vector = Qnil;
10116
10117 if (NILP (vector))
10118 vector = Fmake_vector (make_number (9), Qnil);
10119
10120 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10121 ASET (vector, i, Vdeactivate_mark); ++i;
10122 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10123
10124 if (w)
10125 {
10126 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10127 ASET (vector, i, w->contents); ++i;
10128 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10129 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10130 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10131 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10132 }
10133 else
10134 {
10135 int end = i + 6;
10136 for (; i < end; ++i)
10137 ASET (vector, i, Qnil);
10138 }
10139
10140 eassert (i == ASIZE (vector));
10141 return vector;
10142 }
10143
10144
10145 /* Restore global state from VECTOR which was created by
10146 with_echo_area_buffer_unwind_data. */
10147
10148 static void
10149 unwind_with_echo_area_buffer (Lisp_Object vector)
10150 {
10151 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10152 Vdeactivate_mark = AREF (vector, 1);
10153 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10154
10155 if (WINDOWP (AREF (vector, 3)))
10156 {
10157 struct window *w;
10158 Lisp_Object buffer;
10159
10160 w = XWINDOW (AREF (vector, 3));
10161 buffer = AREF (vector, 4);
10162
10163 wset_buffer (w, buffer);
10164 set_marker_both (w->pointm, buffer,
10165 XFASTINT (AREF (vector, 5)),
10166 XFASTINT (AREF (vector, 6)));
10167 set_marker_both (w->start, buffer,
10168 XFASTINT (AREF (vector, 7)),
10169 XFASTINT (AREF (vector, 8)));
10170 }
10171
10172 Vwith_echo_area_save_vector = vector;
10173 }
10174
10175
10176 /* Set up the echo area for use by print functions. MULTIBYTE_P
10177 non-zero means we will print multibyte. */
10178
10179 void
10180 setup_echo_area_for_printing (int multibyte_p)
10181 {
10182 /* If we can't find an echo area any more, exit. */
10183 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10184 Fkill_emacs (Qnil);
10185
10186 ensure_echo_area_buffers ();
10187
10188 if (!message_buf_print)
10189 {
10190 /* A message has been output since the last time we printed.
10191 Choose a fresh echo area buffer. */
10192 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10193 echo_area_buffer[0] = echo_buffer[1];
10194 else
10195 echo_area_buffer[0] = echo_buffer[0];
10196
10197 /* Switch to that buffer and clear it. */
10198 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10199 bset_truncate_lines (current_buffer, Qnil);
10200
10201 if (Z > BEG)
10202 {
10203 ptrdiff_t count = SPECPDL_INDEX ();
10204 specbind (Qinhibit_read_only, Qt);
10205 /* Note that undo recording is always disabled. */
10206 del_range (BEG, Z);
10207 unbind_to (count, Qnil);
10208 }
10209 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10210
10211 /* Set up the buffer for the multibyteness we need. */
10212 if (multibyte_p
10213 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10214 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10215
10216 /* Raise the frame containing the echo area. */
10217 if (minibuffer_auto_raise)
10218 {
10219 struct frame *sf = SELECTED_FRAME ();
10220 Lisp_Object mini_window;
10221 mini_window = FRAME_MINIBUF_WINDOW (sf);
10222 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10223 }
10224
10225 message_log_maybe_newline ();
10226 message_buf_print = 1;
10227 }
10228 else
10229 {
10230 if (NILP (echo_area_buffer[0]))
10231 {
10232 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10233 echo_area_buffer[0] = echo_buffer[1];
10234 else
10235 echo_area_buffer[0] = echo_buffer[0];
10236 }
10237
10238 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10239 {
10240 /* Someone switched buffers between print requests. */
10241 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10242 bset_truncate_lines (current_buffer, Qnil);
10243 }
10244 }
10245 }
10246
10247
10248 /* Display an echo area message in window W. Value is non-zero if W's
10249 height is changed. If display_last_displayed_message_p is
10250 non-zero, display the message that was last displayed, otherwise
10251 display the current message. */
10252
10253 static int
10254 display_echo_area (struct window *w)
10255 {
10256 int i, no_message_p, window_height_changed_p;
10257
10258 /* Temporarily disable garbage collections while displaying the echo
10259 area. This is done because a GC can print a message itself.
10260 That message would modify the echo area buffer's contents while a
10261 redisplay of the buffer is going on, and seriously confuse
10262 redisplay. */
10263 ptrdiff_t count = inhibit_garbage_collection ();
10264
10265 /* If there is no message, we must call display_echo_area_1
10266 nevertheless because it resizes the window. But we will have to
10267 reset the echo_area_buffer in question to nil at the end because
10268 with_echo_area_buffer will sets it to an empty buffer. */
10269 i = display_last_displayed_message_p ? 1 : 0;
10270 no_message_p = NILP (echo_area_buffer[i]);
10271
10272 window_height_changed_p
10273 = with_echo_area_buffer (w, display_last_displayed_message_p,
10274 display_echo_area_1,
10275 (intptr_t) w, Qnil);
10276
10277 if (no_message_p)
10278 echo_area_buffer[i] = Qnil;
10279
10280 unbind_to (count, Qnil);
10281 return window_height_changed_p;
10282 }
10283
10284
10285 /* Helper for display_echo_area. Display the current buffer which
10286 contains the current echo area message in window W, a mini-window,
10287 a pointer to which is passed in A1. A2..A4 are currently not used.
10288 Change the height of W so that all of the message is displayed.
10289 Value is non-zero if height of W was changed. */
10290
10291 static int
10292 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10293 {
10294 intptr_t i1 = a1;
10295 struct window *w = (struct window *) i1;
10296 Lisp_Object window;
10297 struct text_pos start;
10298 int window_height_changed_p = 0;
10299
10300 /* Do this before displaying, so that we have a large enough glyph
10301 matrix for the display. If we can't get enough space for the
10302 whole text, display the last N lines. That works by setting w->start. */
10303 window_height_changed_p = resize_mini_window (w, 0);
10304
10305 /* Use the starting position chosen by resize_mini_window. */
10306 SET_TEXT_POS_FROM_MARKER (start, w->start);
10307
10308 /* Display. */
10309 clear_glyph_matrix (w->desired_matrix);
10310 XSETWINDOW (window, w);
10311 try_window (window, start, 0);
10312
10313 return window_height_changed_p;
10314 }
10315
10316
10317 /* Resize the echo area window to exactly the size needed for the
10318 currently displayed message, if there is one. If a mini-buffer
10319 is active, don't shrink it. */
10320
10321 void
10322 resize_echo_area_exactly (void)
10323 {
10324 if (BUFFERP (echo_area_buffer[0])
10325 && WINDOWP (echo_area_window))
10326 {
10327 struct window *w = XWINDOW (echo_area_window);
10328 int resized_p;
10329 Lisp_Object resize_exactly;
10330
10331 if (minibuf_level == 0)
10332 resize_exactly = Qt;
10333 else
10334 resize_exactly = Qnil;
10335
10336 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10337 (intptr_t) w, resize_exactly);
10338 if (resized_p)
10339 {
10340 ++windows_or_buffers_changed;
10341 ++update_mode_lines;
10342 redisplay_internal ();
10343 }
10344 }
10345 }
10346
10347
10348 /* Callback function for with_echo_area_buffer, when used from
10349 resize_echo_area_exactly. A1 contains a pointer to the window to
10350 resize, EXACTLY non-nil means resize the mini-window exactly to the
10351 size of the text displayed. A3 and A4 are not used. Value is what
10352 resize_mini_window returns. */
10353
10354 static int
10355 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10356 {
10357 intptr_t i1 = a1;
10358 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10359 }
10360
10361
10362 /* Resize mini-window W to fit the size of its contents. EXACT_P
10363 means size the window exactly to the size needed. Otherwise, it's
10364 only enlarged until W's buffer is empty.
10365
10366 Set W->start to the right place to begin display. If the whole
10367 contents fit, start at the beginning. Otherwise, start so as
10368 to make the end of the contents appear. This is particularly
10369 important for y-or-n-p, but seems desirable generally.
10370
10371 Value is non-zero if the window height has been changed. */
10372
10373 int
10374 resize_mini_window (struct window *w, int exact_p)
10375 {
10376 struct frame *f = XFRAME (w->frame);
10377 int window_height_changed_p = 0;
10378
10379 eassert (MINI_WINDOW_P (w));
10380
10381 /* By default, start display at the beginning. */
10382 set_marker_both (w->start, w->contents,
10383 BUF_BEGV (XBUFFER (w->contents)),
10384 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10385
10386 /* Don't resize windows while redisplaying a window; it would
10387 confuse redisplay functions when the size of the window they are
10388 displaying changes from under them. Such a resizing can happen,
10389 for instance, when which-func prints a long message while
10390 we are running fontification-functions. We're running these
10391 functions with safe_call which binds inhibit-redisplay to t. */
10392 if (!NILP (Vinhibit_redisplay))
10393 return 0;
10394
10395 /* Nil means don't try to resize. */
10396 if (NILP (Vresize_mini_windows)
10397 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10398 return 0;
10399
10400 if (!FRAME_MINIBUF_ONLY_P (f))
10401 {
10402 struct it it;
10403 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10404 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10405 int height;
10406 EMACS_INT max_height;
10407 int unit = FRAME_LINE_HEIGHT (f);
10408 struct text_pos start;
10409 struct buffer *old_current_buffer = NULL;
10410
10411 if (current_buffer != XBUFFER (w->contents))
10412 {
10413 old_current_buffer = current_buffer;
10414 set_buffer_internal (XBUFFER (w->contents));
10415 }
10416
10417 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10418
10419 /* Compute the max. number of lines specified by the user. */
10420 if (FLOATP (Vmax_mini_window_height))
10421 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10422 else if (INTEGERP (Vmax_mini_window_height))
10423 max_height = XINT (Vmax_mini_window_height);
10424 else
10425 max_height = total_height / 4;
10426
10427 /* Correct that max. height if it's bogus. */
10428 max_height = clip_to_bounds (1, max_height, total_height);
10429
10430 /* Find out the height of the text in the window. */
10431 if (it.line_wrap == TRUNCATE)
10432 height = 1;
10433 else
10434 {
10435 last_height = 0;
10436 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10437 if (it.max_ascent == 0 && it.max_descent == 0)
10438 height = it.current_y + last_height;
10439 else
10440 height = it.current_y + it.max_ascent + it.max_descent;
10441 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10442 height = (height + unit - 1) / unit;
10443 }
10444
10445 /* Compute a suitable window start. */
10446 if (height > max_height)
10447 {
10448 height = max_height;
10449 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10450 move_it_vertically_backward (&it, (height - 1) * unit);
10451 start = it.current.pos;
10452 }
10453 else
10454 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10455 SET_MARKER_FROM_TEXT_POS (w->start, start);
10456
10457 if (EQ (Vresize_mini_windows, Qgrow_only))
10458 {
10459 /* Let it grow only, until we display an empty message, in which
10460 case the window shrinks again. */
10461 if (height > WINDOW_TOTAL_LINES (w))
10462 {
10463 int old_height = WINDOW_TOTAL_LINES (w);
10464 freeze_window_starts (f, 1);
10465 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10466 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10467 }
10468 else if (height < WINDOW_TOTAL_LINES (w)
10469 && (exact_p || BEGV == ZV))
10470 {
10471 int old_height = WINDOW_TOTAL_LINES (w);
10472 freeze_window_starts (f, 0);
10473 shrink_mini_window (w);
10474 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10475 }
10476 }
10477 else
10478 {
10479 /* Always resize to exact size needed. */
10480 if (height > WINDOW_TOTAL_LINES (w))
10481 {
10482 int old_height = WINDOW_TOTAL_LINES (w);
10483 freeze_window_starts (f, 1);
10484 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10485 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10486 }
10487 else if (height < WINDOW_TOTAL_LINES (w))
10488 {
10489 int old_height = WINDOW_TOTAL_LINES (w);
10490 freeze_window_starts (f, 0);
10491 shrink_mini_window (w);
10492
10493 if (height)
10494 {
10495 freeze_window_starts (f, 1);
10496 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10497 }
10498
10499 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10500 }
10501 }
10502
10503 if (old_current_buffer)
10504 set_buffer_internal (old_current_buffer);
10505 }
10506
10507 return window_height_changed_p;
10508 }
10509
10510
10511 /* Value is the current message, a string, or nil if there is no
10512 current message. */
10513
10514 Lisp_Object
10515 current_message (void)
10516 {
10517 Lisp_Object msg;
10518
10519 if (!BUFFERP (echo_area_buffer[0]))
10520 msg = Qnil;
10521 else
10522 {
10523 with_echo_area_buffer (0, 0, current_message_1,
10524 (intptr_t) &msg, Qnil);
10525 if (NILP (msg))
10526 echo_area_buffer[0] = Qnil;
10527 }
10528
10529 return msg;
10530 }
10531
10532
10533 static int
10534 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10535 {
10536 intptr_t i1 = a1;
10537 Lisp_Object *msg = (Lisp_Object *) i1;
10538
10539 if (Z > BEG)
10540 *msg = make_buffer_string (BEG, Z, 1);
10541 else
10542 *msg = Qnil;
10543 return 0;
10544 }
10545
10546
10547 /* Push the current message on Vmessage_stack for later restoration
10548 by restore_message. Value is non-zero if the current message isn't
10549 empty. This is a relatively infrequent operation, so it's not
10550 worth optimizing. */
10551
10552 bool
10553 push_message (void)
10554 {
10555 Lisp_Object msg = current_message ();
10556 Vmessage_stack = Fcons (msg, Vmessage_stack);
10557 return STRINGP (msg);
10558 }
10559
10560
10561 /* Restore message display from the top of Vmessage_stack. */
10562
10563 void
10564 restore_message (void)
10565 {
10566 eassert (CONSP (Vmessage_stack));
10567 message3_nolog (XCAR (Vmessage_stack));
10568 }
10569
10570
10571 /* Handler for unwind-protect calling pop_message. */
10572
10573 void
10574 pop_message_unwind (void)
10575 {
10576 /* Pop the top-most entry off Vmessage_stack. */
10577 eassert (CONSP (Vmessage_stack));
10578 Vmessage_stack = XCDR (Vmessage_stack);
10579 }
10580
10581
10582 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10583 exits. If the stack is not empty, we have a missing pop_message
10584 somewhere. */
10585
10586 void
10587 check_message_stack (void)
10588 {
10589 if (!NILP (Vmessage_stack))
10590 emacs_abort ();
10591 }
10592
10593
10594 /* Truncate to NCHARS what will be displayed in the echo area the next
10595 time we display it---but don't redisplay it now. */
10596
10597 void
10598 truncate_echo_area (ptrdiff_t nchars)
10599 {
10600 if (nchars == 0)
10601 echo_area_buffer[0] = Qnil;
10602 else if (!noninteractive
10603 && INTERACTIVE
10604 && !NILP (echo_area_buffer[0]))
10605 {
10606 struct frame *sf = SELECTED_FRAME ();
10607 /* Error messages get reported properly by cmd_error, so this must be
10608 just an informative message; if the frame hasn't really been
10609 initialized yet, just toss it. */
10610 if (sf->glyphs_initialized_p)
10611 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10612 }
10613 }
10614
10615
10616 /* Helper function for truncate_echo_area. Truncate the current
10617 message to at most NCHARS characters. */
10618
10619 static int
10620 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10621 {
10622 if (BEG + nchars < Z)
10623 del_range (BEG + nchars, Z);
10624 if (Z == BEG)
10625 echo_area_buffer[0] = Qnil;
10626 return 0;
10627 }
10628
10629 /* Set the current message to STRING. */
10630
10631 static void
10632 set_message (Lisp_Object string)
10633 {
10634 eassert (STRINGP (string));
10635
10636 message_enable_multibyte = STRING_MULTIBYTE (string);
10637
10638 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10639 message_buf_print = 0;
10640 help_echo_showing_p = 0;
10641
10642 if (STRINGP (Vdebug_on_message)
10643 && STRINGP (string)
10644 && fast_string_match (Vdebug_on_message, string) >= 0)
10645 call_debugger (list2 (Qerror, string));
10646 }
10647
10648
10649 /* Helper function for set_message. First argument is ignored and second
10650 argument has the same meaning as for set_message.
10651 This function is called with the echo area buffer being current. */
10652
10653 static int
10654 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10655 {
10656 eassert (STRINGP (string));
10657
10658 /* Change multibyteness of the echo buffer appropriately. */
10659 if (message_enable_multibyte
10660 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10661 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10662
10663 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10664 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10665 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10666
10667 /* Insert new message at BEG. */
10668 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10669
10670 /* This function takes care of single/multibyte conversion.
10671 We just have to ensure that the echo area buffer has the right
10672 setting of enable_multibyte_characters. */
10673 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10674
10675 return 0;
10676 }
10677
10678
10679 /* Clear messages. CURRENT_P non-zero means clear the current
10680 message. LAST_DISPLAYED_P non-zero means clear the message
10681 last displayed. */
10682
10683 void
10684 clear_message (int current_p, int last_displayed_p)
10685 {
10686 if (current_p)
10687 {
10688 echo_area_buffer[0] = Qnil;
10689 message_cleared_p = 1;
10690 }
10691
10692 if (last_displayed_p)
10693 echo_area_buffer[1] = Qnil;
10694
10695 message_buf_print = 0;
10696 }
10697
10698 /* Clear garbaged frames.
10699
10700 This function is used where the old redisplay called
10701 redraw_garbaged_frames which in turn called redraw_frame which in
10702 turn called clear_frame. The call to clear_frame was a source of
10703 flickering. I believe a clear_frame is not necessary. It should
10704 suffice in the new redisplay to invalidate all current matrices,
10705 and ensure a complete redisplay of all windows. */
10706
10707 static void
10708 clear_garbaged_frames (void)
10709 {
10710 if (frame_garbaged)
10711 {
10712 Lisp_Object tail, frame;
10713 int changed_count = 0;
10714
10715 FOR_EACH_FRAME (tail, frame)
10716 {
10717 struct frame *f = XFRAME (frame);
10718
10719 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10720 {
10721 if (f->resized_p)
10722 {
10723 redraw_frame (f);
10724 f->force_flush_display_p = 1;
10725 }
10726 clear_current_matrices (f);
10727 changed_count++;
10728 f->garbaged = 0;
10729 f->resized_p = 0;
10730 }
10731 }
10732
10733 frame_garbaged = 0;
10734 if (changed_count)
10735 ++windows_or_buffers_changed;
10736 }
10737 }
10738
10739
10740 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10741 is non-zero update selected_frame. Value is non-zero if the
10742 mini-windows height has been changed. */
10743
10744 static int
10745 echo_area_display (int update_frame_p)
10746 {
10747 Lisp_Object mini_window;
10748 struct window *w;
10749 struct frame *f;
10750 int window_height_changed_p = 0;
10751 struct frame *sf = SELECTED_FRAME ();
10752
10753 mini_window = FRAME_MINIBUF_WINDOW (sf);
10754 w = XWINDOW (mini_window);
10755 f = XFRAME (WINDOW_FRAME (w));
10756
10757 /* Don't display if frame is invisible or not yet initialized. */
10758 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10759 return 0;
10760
10761 #ifdef HAVE_WINDOW_SYSTEM
10762 /* When Emacs starts, selected_frame may be the initial terminal
10763 frame. If we let this through, a message would be displayed on
10764 the terminal. */
10765 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10766 return 0;
10767 #endif /* HAVE_WINDOW_SYSTEM */
10768
10769 /* Redraw garbaged frames. */
10770 clear_garbaged_frames ();
10771
10772 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10773 {
10774 echo_area_window = mini_window;
10775 window_height_changed_p = display_echo_area (w);
10776 w->must_be_updated_p = 1;
10777
10778 /* Update the display, unless called from redisplay_internal.
10779 Also don't update the screen during redisplay itself. The
10780 update will happen at the end of redisplay, and an update
10781 here could cause confusion. */
10782 if (update_frame_p && !redisplaying_p)
10783 {
10784 int n = 0;
10785
10786 /* If the display update has been interrupted by pending
10787 input, update mode lines in the frame. Due to the
10788 pending input, it might have been that redisplay hasn't
10789 been called, so that mode lines above the echo area are
10790 garbaged. This looks odd, so we prevent it here. */
10791 if (!display_completed)
10792 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10793
10794 if (window_height_changed_p
10795 /* Don't do this if Emacs is shutting down. Redisplay
10796 needs to run hooks. */
10797 && !NILP (Vrun_hooks))
10798 {
10799 /* Must update other windows. Likewise as in other
10800 cases, don't let this update be interrupted by
10801 pending input. */
10802 ptrdiff_t count = SPECPDL_INDEX ();
10803 specbind (Qredisplay_dont_pause, Qt);
10804 windows_or_buffers_changed = 1;
10805 redisplay_internal ();
10806 unbind_to (count, Qnil);
10807 }
10808 else if (FRAME_WINDOW_P (f) && n == 0)
10809 {
10810 /* Window configuration is the same as before.
10811 Can do with a display update of the echo area,
10812 unless we displayed some mode lines. */
10813 update_single_window (w, 1);
10814 FRAME_RIF (f)->flush_display (f);
10815 }
10816 else
10817 update_frame (f, 1, 1);
10818
10819 /* If cursor is in the echo area, make sure that the next
10820 redisplay displays the minibuffer, so that the cursor will
10821 be replaced with what the minibuffer wants. */
10822 if (cursor_in_echo_area)
10823 ++windows_or_buffers_changed;
10824 }
10825 }
10826 else if (!EQ (mini_window, selected_window))
10827 windows_or_buffers_changed++;
10828
10829 /* Last displayed message is now the current message. */
10830 echo_area_buffer[1] = echo_area_buffer[0];
10831 /* Inform read_char that we're not echoing. */
10832 echo_message_buffer = Qnil;
10833
10834 /* Prevent redisplay optimization in redisplay_internal by resetting
10835 this_line_start_pos. This is done because the mini-buffer now
10836 displays the message instead of its buffer text. */
10837 if (EQ (mini_window, selected_window))
10838 CHARPOS (this_line_start_pos) = 0;
10839
10840 return window_height_changed_p;
10841 }
10842
10843 /* Nonzero if the current window's buffer is shown in more than one
10844 window and was modified since last redisplay. */
10845
10846 static int
10847 buffer_shared_and_changed (void)
10848 {
10849 return (buffer_window_count (current_buffer) > 1
10850 && UNCHANGED_MODIFIED < MODIFF);
10851 }
10852
10853 /* Nonzero if W doesn't reflect the actual state of current buffer due
10854 to its text or overlays change. FIXME: this may be called when
10855 XBUFFER (w->contents) != current_buffer, which looks suspicious. */
10856
10857 static int
10858 window_outdated (struct window *w)
10859 {
10860 return (w->last_modified < MODIFF
10861 || w->last_overlay_modified < OVERLAY_MODIFF);
10862 }
10863
10864 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10865 is enabled and mark of W's buffer was changed since last W's update. */
10866
10867 static int
10868 window_buffer_changed (struct window *w)
10869 {
10870 struct buffer *b = XBUFFER (w->contents);
10871
10872 eassert (BUFFER_LIVE_P (b));
10873
10874 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10875 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10876 != (w->region_showing != 0)));
10877 }
10878
10879 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10880
10881 static int
10882 mode_line_update_needed (struct window *w)
10883 {
10884 return (w->column_number_displayed != -1
10885 && !(PT == w->last_point && !window_outdated (w))
10886 && (w->column_number_displayed != current_column ()));
10887 }
10888
10889 /***********************************************************************
10890 Mode Lines and Frame Titles
10891 ***********************************************************************/
10892
10893 /* A buffer for constructing non-propertized mode-line strings and
10894 frame titles in it; allocated from the heap in init_xdisp and
10895 resized as needed in store_mode_line_noprop_char. */
10896
10897 static char *mode_line_noprop_buf;
10898
10899 /* The buffer's end, and a current output position in it. */
10900
10901 static char *mode_line_noprop_buf_end;
10902 static char *mode_line_noprop_ptr;
10903
10904 #define MODE_LINE_NOPROP_LEN(start) \
10905 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10906
10907 static enum {
10908 MODE_LINE_DISPLAY = 0,
10909 MODE_LINE_TITLE,
10910 MODE_LINE_NOPROP,
10911 MODE_LINE_STRING
10912 } mode_line_target;
10913
10914 /* Alist that caches the results of :propertize.
10915 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10916 static Lisp_Object mode_line_proptrans_alist;
10917
10918 /* List of strings making up the mode-line. */
10919 static Lisp_Object mode_line_string_list;
10920
10921 /* Base face property when building propertized mode line string. */
10922 static Lisp_Object mode_line_string_face;
10923 static Lisp_Object mode_line_string_face_prop;
10924
10925
10926 /* Unwind data for mode line strings */
10927
10928 static Lisp_Object Vmode_line_unwind_vector;
10929
10930 static Lisp_Object
10931 format_mode_line_unwind_data (struct frame *target_frame,
10932 struct buffer *obuf,
10933 Lisp_Object owin,
10934 int save_proptrans)
10935 {
10936 Lisp_Object vector, tmp;
10937
10938 /* Reduce consing by keeping one vector in
10939 Vwith_echo_area_save_vector. */
10940 vector = Vmode_line_unwind_vector;
10941 Vmode_line_unwind_vector = Qnil;
10942
10943 if (NILP (vector))
10944 vector = Fmake_vector (make_number (10), Qnil);
10945
10946 ASET (vector, 0, make_number (mode_line_target));
10947 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10948 ASET (vector, 2, mode_line_string_list);
10949 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10950 ASET (vector, 4, mode_line_string_face);
10951 ASET (vector, 5, mode_line_string_face_prop);
10952
10953 if (obuf)
10954 XSETBUFFER (tmp, obuf);
10955 else
10956 tmp = Qnil;
10957 ASET (vector, 6, tmp);
10958 ASET (vector, 7, owin);
10959 if (target_frame)
10960 {
10961 /* Similarly to `with-selected-window', if the operation selects
10962 a window on another frame, we must restore that frame's
10963 selected window, and (for a tty) the top-frame. */
10964 ASET (vector, 8, target_frame->selected_window);
10965 if (FRAME_TERMCAP_P (target_frame))
10966 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10967 }
10968
10969 return vector;
10970 }
10971
10972 static void
10973 unwind_format_mode_line (Lisp_Object vector)
10974 {
10975 Lisp_Object old_window = AREF (vector, 7);
10976 Lisp_Object target_frame_window = AREF (vector, 8);
10977 Lisp_Object old_top_frame = AREF (vector, 9);
10978
10979 mode_line_target = XINT (AREF (vector, 0));
10980 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10981 mode_line_string_list = AREF (vector, 2);
10982 if (! EQ (AREF (vector, 3), Qt))
10983 mode_line_proptrans_alist = AREF (vector, 3);
10984 mode_line_string_face = AREF (vector, 4);
10985 mode_line_string_face_prop = AREF (vector, 5);
10986
10987 /* Select window before buffer, since it may change the buffer. */
10988 if (!NILP (old_window))
10989 {
10990 /* If the operation that we are unwinding had selected a window
10991 on a different frame, reset its frame-selected-window. For a
10992 text terminal, reset its top-frame if necessary. */
10993 if (!NILP (target_frame_window))
10994 {
10995 Lisp_Object frame
10996 = WINDOW_FRAME (XWINDOW (target_frame_window));
10997
10998 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10999 Fselect_window (target_frame_window, Qt);
11000
11001 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11002 Fselect_frame (old_top_frame, Qt);
11003 }
11004
11005 Fselect_window (old_window, Qt);
11006 }
11007
11008 if (!NILP (AREF (vector, 6)))
11009 {
11010 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11011 ASET (vector, 6, Qnil);
11012 }
11013
11014 Vmode_line_unwind_vector = vector;
11015 }
11016
11017
11018 /* Store a single character C for the frame title in mode_line_noprop_buf.
11019 Re-allocate mode_line_noprop_buf if necessary. */
11020
11021 static void
11022 store_mode_line_noprop_char (char c)
11023 {
11024 /* If output position has reached the end of the allocated buffer,
11025 increase the buffer's size. */
11026 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11027 {
11028 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11029 ptrdiff_t size = len;
11030 mode_line_noprop_buf =
11031 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11032 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11033 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11034 }
11035
11036 *mode_line_noprop_ptr++ = c;
11037 }
11038
11039
11040 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11041 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11042 characters that yield more columns than PRECISION; PRECISION <= 0
11043 means copy the whole string. Pad with spaces until FIELD_WIDTH
11044 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11045 pad. Called from display_mode_element when it is used to build a
11046 frame title. */
11047
11048 static int
11049 store_mode_line_noprop (const char *string, int field_width, int precision)
11050 {
11051 const unsigned char *str = (const unsigned char *) string;
11052 int n = 0;
11053 ptrdiff_t dummy, nbytes;
11054
11055 /* Copy at most PRECISION chars from STR. */
11056 nbytes = strlen (string);
11057 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11058 while (nbytes--)
11059 store_mode_line_noprop_char (*str++);
11060
11061 /* Fill up with spaces until FIELD_WIDTH reached. */
11062 while (field_width > 0
11063 && n < field_width)
11064 {
11065 store_mode_line_noprop_char (' ');
11066 ++n;
11067 }
11068
11069 return n;
11070 }
11071
11072 /***********************************************************************
11073 Frame Titles
11074 ***********************************************************************/
11075
11076 #ifdef HAVE_WINDOW_SYSTEM
11077
11078 /* Set the title of FRAME, if it has changed. The title format is
11079 Vicon_title_format if FRAME is iconified, otherwise it is
11080 frame_title_format. */
11081
11082 static void
11083 x_consider_frame_title (Lisp_Object frame)
11084 {
11085 struct frame *f = XFRAME (frame);
11086
11087 if (FRAME_WINDOW_P (f)
11088 || FRAME_MINIBUF_ONLY_P (f)
11089 || f->explicit_name)
11090 {
11091 /* Do we have more than one visible frame on this X display? */
11092 Lisp_Object tail, other_frame, fmt;
11093 ptrdiff_t title_start;
11094 char *title;
11095 ptrdiff_t len;
11096 struct it it;
11097 ptrdiff_t count = SPECPDL_INDEX ();
11098
11099 FOR_EACH_FRAME (tail, other_frame)
11100 {
11101 struct frame *tf = XFRAME (other_frame);
11102
11103 if (tf != f
11104 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11105 && !FRAME_MINIBUF_ONLY_P (tf)
11106 && !EQ (other_frame, tip_frame)
11107 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11108 break;
11109 }
11110
11111 /* Set global variable indicating that multiple frames exist. */
11112 multiple_frames = CONSP (tail);
11113
11114 /* Switch to the buffer of selected window of the frame. Set up
11115 mode_line_target so that display_mode_element will output into
11116 mode_line_noprop_buf; then display the title. */
11117 record_unwind_protect (unwind_format_mode_line,
11118 format_mode_line_unwind_data
11119 (f, current_buffer, selected_window, 0));
11120
11121 Fselect_window (f->selected_window, Qt);
11122 set_buffer_internal_1
11123 (XBUFFER (XWINDOW (f->selected_window)->contents));
11124 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11125
11126 mode_line_target = MODE_LINE_TITLE;
11127 title_start = MODE_LINE_NOPROP_LEN (0);
11128 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11129 NULL, DEFAULT_FACE_ID);
11130 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11131 len = MODE_LINE_NOPROP_LEN (title_start);
11132 title = mode_line_noprop_buf + title_start;
11133 unbind_to (count, Qnil);
11134
11135 /* Set the title only if it's changed. This avoids consing in
11136 the common case where it hasn't. (If it turns out that we've
11137 already wasted too much time by walking through the list with
11138 display_mode_element, then we might need to optimize at a
11139 higher level than this.) */
11140 if (! STRINGP (f->name)
11141 || SBYTES (f->name) != len
11142 || memcmp (title, SDATA (f->name), len) != 0)
11143 x_implicitly_set_name (f, make_string (title, len), Qnil);
11144 }
11145 }
11146
11147 #endif /* not HAVE_WINDOW_SYSTEM */
11148
11149 \f
11150 /***********************************************************************
11151 Menu Bars
11152 ***********************************************************************/
11153
11154
11155 /* Prepare for redisplay by updating menu-bar item lists when
11156 appropriate. This can call eval. */
11157
11158 void
11159 prepare_menu_bars (void)
11160 {
11161 int all_windows;
11162 struct gcpro gcpro1, gcpro2;
11163 struct frame *f;
11164 Lisp_Object tooltip_frame;
11165
11166 #ifdef HAVE_WINDOW_SYSTEM
11167 tooltip_frame = tip_frame;
11168 #else
11169 tooltip_frame = Qnil;
11170 #endif
11171
11172 /* Update all frame titles based on their buffer names, etc. We do
11173 this before the menu bars so that the buffer-menu will show the
11174 up-to-date frame titles. */
11175 #ifdef HAVE_WINDOW_SYSTEM
11176 if (windows_or_buffers_changed || update_mode_lines)
11177 {
11178 Lisp_Object tail, frame;
11179
11180 FOR_EACH_FRAME (tail, frame)
11181 {
11182 f = XFRAME (frame);
11183 if (!EQ (frame, tooltip_frame)
11184 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11185 x_consider_frame_title (frame);
11186 }
11187 }
11188 #endif /* HAVE_WINDOW_SYSTEM */
11189
11190 /* Update the menu bar item lists, if appropriate. This has to be
11191 done before any actual redisplay or generation of display lines. */
11192 all_windows = (update_mode_lines
11193 || buffer_shared_and_changed ()
11194 || windows_or_buffers_changed);
11195 if (all_windows)
11196 {
11197 Lisp_Object tail, frame;
11198 ptrdiff_t count = SPECPDL_INDEX ();
11199 /* 1 means that update_menu_bar has run its hooks
11200 so any further calls to update_menu_bar shouldn't do so again. */
11201 int menu_bar_hooks_run = 0;
11202
11203 record_unwind_save_match_data ();
11204
11205 FOR_EACH_FRAME (tail, frame)
11206 {
11207 f = XFRAME (frame);
11208
11209 /* Ignore tooltip frame. */
11210 if (EQ (frame, tooltip_frame))
11211 continue;
11212
11213 /* If a window on this frame changed size, report that to
11214 the user and clear the size-change flag. */
11215 if (FRAME_WINDOW_SIZES_CHANGED (f))
11216 {
11217 Lisp_Object functions;
11218
11219 /* Clear flag first in case we get an error below. */
11220 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11221 functions = Vwindow_size_change_functions;
11222 GCPRO2 (tail, functions);
11223
11224 while (CONSP (functions))
11225 {
11226 if (!EQ (XCAR (functions), Qt))
11227 call1 (XCAR (functions), frame);
11228 functions = XCDR (functions);
11229 }
11230 UNGCPRO;
11231 }
11232
11233 GCPRO1 (tail);
11234 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11235 #ifdef HAVE_WINDOW_SYSTEM
11236 update_tool_bar (f, 0);
11237 #endif
11238 #ifdef HAVE_NS
11239 if (windows_or_buffers_changed
11240 && FRAME_NS_P (f))
11241 ns_set_doc_edited
11242 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11243 #endif
11244 UNGCPRO;
11245 }
11246
11247 unbind_to (count, Qnil);
11248 }
11249 else
11250 {
11251 struct frame *sf = SELECTED_FRAME ();
11252 update_menu_bar (sf, 1, 0);
11253 #ifdef HAVE_WINDOW_SYSTEM
11254 update_tool_bar (sf, 1);
11255 #endif
11256 }
11257 }
11258
11259
11260 /* Update the menu bar item list for frame F. This has to be done
11261 before we start to fill in any display lines, because it can call
11262 eval.
11263
11264 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11265
11266 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11267 already ran the menu bar hooks for this redisplay, so there
11268 is no need to run them again. The return value is the
11269 updated value of this flag, to pass to the next call. */
11270
11271 static int
11272 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11273 {
11274 Lisp_Object window;
11275 register struct window *w;
11276
11277 /* If called recursively during a menu update, do nothing. This can
11278 happen when, for instance, an activate-menubar-hook causes a
11279 redisplay. */
11280 if (inhibit_menubar_update)
11281 return hooks_run;
11282
11283 window = FRAME_SELECTED_WINDOW (f);
11284 w = XWINDOW (window);
11285
11286 if (FRAME_WINDOW_P (f)
11287 ?
11288 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11289 || defined (HAVE_NS) || defined (USE_GTK)
11290 FRAME_EXTERNAL_MENU_BAR (f)
11291 #else
11292 FRAME_MENU_BAR_LINES (f) > 0
11293 #endif
11294 : FRAME_MENU_BAR_LINES (f) > 0)
11295 {
11296 /* If the user has switched buffers or windows, we need to
11297 recompute to reflect the new bindings. But we'll
11298 recompute when update_mode_lines is set too; that means
11299 that people can use force-mode-line-update to request
11300 that the menu bar be recomputed. The adverse effect on
11301 the rest of the redisplay algorithm is about the same as
11302 windows_or_buffers_changed anyway. */
11303 if (windows_or_buffers_changed
11304 /* This used to test w->update_mode_line, but we believe
11305 there is no need to recompute the menu in that case. */
11306 || update_mode_lines
11307 || window_buffer_changed (w))
11308 {
11309 struct buffer *prev = current_buffer;
11310 ptrdiff_t count = SPECPDL_INDEX ();
11311
11312 specbind (Qinhibit_menubar_update, Qt);
11313
11314 set_buffer_internal_1 (XBUFFER (w->contents));
11315 if (save_match_data)
11316 record_unwind_save_match_data ();
11317 if (NILP (Voverriding_local_map_menu_flag))
11318 {
11319 specbind (Qoverriding_terminal_local_map, Qnil);
11320 specbind (Qoverriding_local_map, Qnil);
11321 }
11322
11323 if (!hooks_run)
11324 {
11325 /* Run the Lucid hook. */
11326 safe_run_hooks (Qactivate_menubar_hook);
11327
11328 /* If it has changed current-menubar from previous value,
11329 really recompute the menu-bar from the value. */
11330 if (! NILP (Vlucid_menu_bar_dirty_flag))
11331 call0 (Qrecompute_lucid_menubar);
11332
11333 safe_run_hooks (Qmenu_bar_update_hook);
11334
11335 hooks_run = 1;
11336 }
11337
11338 XSETFRAME (Vmenu_updating_frame, f);
11339 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11340
11341 /* Redisplay the menu bar in case we changed it. */
11342 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11343 || defined (HAVE_NS) || defined (USE_GTK)
11344 if (FRAME_WINDOW_P (f))
11345 {
11346 #if defined (HAVE_NS)
11347 /* All frames on Mac OS share the same menubar. So only
11348 the selected frame should be allowed to set it. */
11349 if (f == SELECTED_FRAME ())
11350 #endif
11351 set_frame_menubar (f, 0, 0);
11352 }
11353 else
11354 /* On a terminal screen, the menu bar is an ordinary screen
11355 line, and this makes it get updated. */
11356 w->update_mode_line = 1;
11357 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11358 /* In the non-toolkit version, the menu bar is an ordinary screen
11359 line, and this makes it get updated. */
11360 w->update_mode_line = 1;
11361 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11362
11363 unbind_to (count, Qnil);
11364 set_buffer_internal_1 (prev);
11365 }
11366 }
11367
11368 return hooks_run;
11369 }
11370
11371
11372 \f
11373 /***********************************************************************
11374 Output Cursor
11375 ***********************************************************************/
11376
11377 #ifdef HAVE_WINDOW_SYSTEM
11378
11379 /* EXPORT:
11380 Nominal cursor position -- where to draw output.
11381 HPOS and VPOS are window relative glyph matrix coordinates.
11382 X and Y are window relative pixel coordinates. */
11383
11384 struct cursor_pos output_cursor;
11385
11386
11387 /* EXPORT:
11388 Set the global variable output_cursor to CURSOR. All cursor
11389 positions are relative to updated_window. */
11390
11391 void
11392 set_output_cursor (struct cursor_pos *cursor)
11393 {
11394 output_cursor.hpos = cursor->hpos;
11395 output_cursor.vpos = cursor->vpos;
11396 output_cursor.x = cursor->x;
11397 output_cursor.y = cursor->y;
11398 }
11399
11400
11401 /* EXPORT for RIF:
11402 Set a nominal cursor position.
11403
11404 HPOS and VPOS are column/row positions in a window glyph matrix. X
11405 and Y are window text area relative pixel positions.
11406
11407 If this is done during an update, updated_window will contain the
11408 window that is being updated and the position is the future output
11409 cursor position for that window. If updated_window is null, use
11410 selected_window and display the cursor at the given position. */
11411
11412 void
11413 x_cursor_to (int vpos, int hpos, int y, int x)
11414 {
11415 struct window *w;
11416
11417 /* If updated_window is not set, work on selected_window. */
11418 if (updated_window)
11419 w = updated_window;
11420 else
11421 w = XWINDOW (selected_window);
11422
11423 /* Set the output cursor. */
11424 output_cursor.hpos = hpos;
11425 output_cursor.vpos = vpos;
11426 output_cursor.x = x;
11427 output_cursor.y = y;
11428
11429 /* If not called as part of an update, really display the cursor.
11430 This will also set the cursor position of W. */
11431 if (updated_window == NULL)
11432 {
11433 block_input ();
11434 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11435 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11436 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11437 unblock_input ();
11438 }
11439 }
11440
11441 #endif /* HAVE_WINDOW_SYSTEM */
11442
11443 \f
11444 /***********************************************************************
11445 Tool-bars
11446 ***********************************************************************/
11447
11448 #ifdef HAVE_WINDOW_SYSTEM
11449
11450 /* Where the mouse was last time we reported a mouse event. */
11451
11452 struct frame *last_mouse_frame;
11453
11454 /* Tool-bar item index of the item on which a mouse button was pressed
11455 or -1. */
11456
11457 int last_tool_bar_item;
11458
11459 /* Select `frame' temporarily without running all the code in
11460 do_switch_frame.
11461 FIXME: Maybe do_switch_frame should be trimmed down similarly
11462 when `norecord' is set. */
11463 static void
11464 fast_set_selected_frame (Lisp_Object frame)
11465 {
11466 if (!EQ (selected_frame, frame))
11467 {
11468 selected_frame = frame;
11469 selected_window = XFRAME (frame)->selected_window;
11470 }
11471 }
11472
11473 /* Update the tool-bar item list for frame F. This has to be done
11474 before we start to fill in any display lines. Called from
11475 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11476 and restore it here. */
11477
11478 static void
11479 update_tool_bar (struct frame *f, int save_match_data)
11480 {
11481 #if defined (USE_GTK) || defined (HAVE_NS)
11482 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11483 #else
11484 int do_update = WINDOWP (f->tool_bar_window)
11485 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11486 #endif
11487
11488 if (do_update)
11489 {
11490 Lisp_Object window;
11491 struct window *w;
11492
11493 window = FRAME_SELECTED_WINDOW (f);
11494 w = XWINDOW (window);
11495
11496 /* If the user has switched buffers or windows, we need to
11497 recompute to reflect the new bindings. But we'll
11498 recompute when update_mode_lines is set too; that means
11499 that people can use force-mode-line-update to request
11500 that the menu bar be recomputed. The adverse effect on
11501 the rest of the redisplay algorithm is about the same as
11502 windows_or_buffers_changed anyway. */
11503 if (windows_or_buffers_changed
11504 || w->update_mode_line
11505 || update_mode_lines
11506 || window_buffer_changed (w))
11507 {
11508 struct buffer *prev = current_buffer;
11509 ptrdiff_t count = SPECPDL_INDEX ();
11510 Lisp_Object frame, new_tool_bar;
11511 int new_n_tool_bar;
11512 struct gcpro gcpro1;
11513
11514 /* Set current_buffer to the buffer of the selected
11515 window of the frame, so that we get the right local
11516 keymaps. */
11517 set_buffer_internal_1 (XBUFFER (w->contents));
11518
11519 /* Save match data, if we must. */
11520 if (save_match_data)
11521 record_unwind_save_match_data ();
11522
11523 /* Make sure that we don't accidentally use bogus keymaps. */
11524 if (NILP (Voverriding_local_map_menu_flag))
11525 {
11526 specbind (Qoverriding_terminal_local_map, Qnil);
11527 specbind (Qoverriding_local_map, Qnil);
11528 }
11529
11530 GCPRO1 (new_tool_bar);
11531
11532 /* We must temporarily set the selected frame to this frame
11533 before calling tool_bar_items, because the calculation of
11534 the tool-bar keymap uses the selected frame (see
11535 `tool-bar-make-keymap' in tool-bar.el). */
11536 eassert (EQ (selected_window,
11537 /* Since we only explicitly preserve selected_frame,
11538 check that selected_window would be redundant. */
11539 XFRAME (selected_frame)->selected_window));
11540 record_unwind_protect (fast_set_selected_frame, selected_frame);
11541 XSETFRAME (frame, f);
11542 fast_set_selected_frame (frame);
11543
11544 /* Build desired tool-bar items from keymaps. */
11545 new_tool_bar
11546 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11547 &new_n_tool_bar);
11548
11549 /* Redisplay the tool-bar if we changed it. */
11550 if (new_n_tool_bar != f->n_tool_bar_items
11551 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11552 {
11553 /* Redisplay that happens asynchronously due to an expose event
11554 may access f->tool_bar_items. Make sure we update both
11555 variables within BLOCK_INPUT so no such event interrupts. */
11556 block_input ();
11557 fset_tool_bar_items (f, new_tool_bar);
11558 f->n_tool_bar_items = new_n_tool_bar;
11559 w->update_mode_line = 1;
11560 unblock_input ();
11561 }
11562
11563 UNGCPRO;
11564
11565 unbind_to (count, Qnil);
11566 set_buffer_internal_1 (prev);
11567 }
11568 }
11569 }
11570
11571
11572 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11573 F's desired tool-bar contents. F->tool_bar_items must have
11574 been set up previously by calling prepare_menu_bars. */
11575
11576 static void
11577 build_desired_tool_bar_string (struct frame *f)
11578 {
11579 int i, size, size_needed;
11580 struct gcpro gcpro1, gcpro2, gcpro3;
11581 Lisp_Object image, plist, props;
11582
11583 image = plist = props = Qnil;
11584 GCPRO3 (image, plist, props);
11585
11586 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11587 Otherwise, make a new string. */
11588
11589 /* The size of the string we might be able to reuse. */
11590 size = (STRINGP (f->desired_tool_bar_string)
11591 ? SCHARS (f->desired_tool_bar_string)
11592 : 0);
11593
11594 /* We need one space in the string for each image. */
11595 size_needed = f->n_tool_bar_items;
11596
11597 /* Reuse f->desired_tool_bar_string, if possible. */
11598 if (size < size_needed || NILP (f->desired_tool_bar_string))
11599 fset_desired_tool_bar_string
11600 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11601 else
11602 {
11603 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11604 Fremove_text_properties (make_number (0), make_number (size),
11605 props, f->desired_tool_bar_string);
11606 }
11607
11608 /* Put a `display' property on the string for the images to display,
11609 put a `menu_item' property on tool-bar items with a value that
11610 is the index of the item in F's tool-bar item vector. */
11611 for (i = 0; i < f->n_tool_bar_items; ++i)
11612 {
11613 #define PROP(IDX) \
11614 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11615
11616 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11617 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11618 int hmargin, vmargin, relief, idx, end;
11619
11620 /* If image is a vector, choose the image according to the
11621 button state. */
11622 image = PROP (TOOL_BAR_ITEM_IMAGES);
11623 if (VECTORP (image))
11624 {
11625 if (enabled_p)
11626 idx = (selected_p
11627 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11628 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11629 else
11630 idx = (selected_p
11631 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11632 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11633
11634 eassert (ASIZE (image) >= idx);
11635 image = AREF (image, idx);
11636 }
11637 else
11638 idx = -1;
11639
11640 /* Ignore invalid image specifications. */
11641 if (!valid_image_p (image))
11642 continue;
11643
11644 /* Display the tool-bar button pressed, or depressed. */
11645 plist = Fcopy_sequence (XCDR (image));
11646
11647 /* Compute margin and relief to draw. */
11648 relief = (tool_bar_button_relief >= 0
11649 ? tool_bar_button_relief
11650 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11651 hmargin = vmargin = relief;
11652
11653 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11654 INT_MAX - max (hmargin, vmargin)))
11655 {
11656 hmargin += XFASTINT (Vtool_bar_button_margin);
11657 vmargin += XFASTINT (Vtool_bar_button_margin);
11658 }
11659 else if (CONSP (Vtool_bar_button_margin))
11660 {
11661 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11662 INT_MAX - hmargin))
11663 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11664
11665 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11666 INT_MAX - vmargin))
11667 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11668 }
11669
11670 if (auto_raise_tool_bar_buttons_p)
11671 {
11672 /* Add a `:relief' property to the image spec if the item is
11673 selected. */
11674 if (selected_p)
11675 {
11676 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11677 hmargin -= relief;
11678 vmargin -= relief;
11679 }
11680 }
11681 else
11682 {
11683 /* If image is selected, display it pressed, i.e. with a
11684 negative relief. If it's not selected, display it with a
11685 raised relief. */
11686 plist = Fplist_put (plist, QCrelief,
11687 (selected_p
11688 ? make_number (-relief)
11689 : make_number (relief)));
11690 hmargin -= relief;
11691 vmargin -= relief;
11692 }
11693
11694 /* Put a margin around the image. */
11695 if (hmargin || vmargin)
11696 {
11697 if (hmargin == vmargin)
11698 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11699 else
11700 plist = Fplist_put (plist, QCmargin,
11701 Fcons (make_number (hmargin),
11702 make_number (vmargin)));
11703 }
11704
11705 /* If button is not enabled, and we don't have special images
11706 for the disabled state, make the image appear disabled by
11707 applying an appropriate algorithm to it. */
11708 if (!enabled_p && idx < 0)
11709 plist = Fplist_put (plist, QCconversion, Qdisabled);
11710
11711 /* Put a `display' text property on the string for the image to
11712 display. Put a `menu-item' property on the string that gives
11713 the start of this item's properties in the tool-bar items
11714 vector. */
11715 image = Fcons (Qimage, plist);
11716 props = list4 (Qdisplay, image,
11717 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11718
11719 /* Let the last image hide all remaining spaces in the tool bar
11720 string. The string can be longer than needed when we reuse a
11721 previous string. */
11722 if (i + 1 == f->n_tool_bar_items)
11723 end = SCHARS (f->desired_tool_bar_string);
11724 else
11725 end = i + 1;
11726 Fadd_text_properties (make_number (i), make_number (end),
11727 props, f->desired_tool_bar_string);
11728 #undef PROP
11729 }
11730
11731 UNGCPRO;
11732 }
11733
11734
11735 /* Display one line of the tool-bar of frame IT->f.
11736
11737 HEIGHT specifies the desired height of the tool-bar line.
11738 If the actual height of the glyph row is less than HEIGHT, the
11739 row's height is increased to HEIGHT, and the icons are centered
11740 vertically in the new height.
11741
11742 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11743 count a final empty row in case the tool-bar width exactly matches
11744 the window width.
11745 */
11746
11747 static void
11748 display_tool_bar_line (struct it *it, int height)
11749 {
11750 struct glyph_row *row = it->glyph_row;
11751 int max_x = it->last_visible_x;
11752 struct glyph *last;
11753
11754 prepare_desired_row (row);
11755 row->y = it->current_y;
11756
11757 /* Note that this isn't made use of if the face hasn't a box,
11758 so there's no need to check the face here. */
11759 it->start_of_box_run_p = 1;
11760
11761 while (it->current_x < max_x)
11762 {
11763 int x, n_glyphs_before, i, nglyphs;
11764 struct it it_before;
11765
11766 /* Get the next display element. */
11767 if (!get_next_display_element (it))
11768 {
11769 /* Don't count empty row if we are counting needed tool-bar lines. */
11770 if (height < 0 && !it->hpos)
11771 return;
11772 break;
11773 }
11774
11775 /* Produce glyphs. */
11776 n_glyphs_before = row->used[TEXT_AREA];
11777 it_before = *it;
11778
11779 PRODUCE_GLYPHS (it);
11780
11781 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11782 i = 0;
11783 x = it_before.current_x;
11784 while (i < nglyphs)
11785 {
11786 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11787
11788 if (x + glyph->pixel_width > max_x)
11789 {
11790 /* Glyph doesn't fit on line. Backtrack. */
11791 row->used[TEXT_AREA] = n_glyphs_before;
11792 *it = it_before;
11793 /* If this is the only glyph on this line, it will never fit on the
11794 tool-bar, so skip it. But ensure there is at least one glyph,
11795 so we don't accidentally disable the tool-bar. */
11796 if (n_glyphs_before == 0
11797 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11798 break;
11799 goto out;
11800 }
11801
11802 ++it->hpos;
11803 x += glyph->pixel_width;
11804 ++i;
11805 }
11806
11807 /* Stop at line end. */
11808 if (ITERATOR_AT_END_OF_LINE_P (it))
11809 break;
11810
11811 set_iterator_to_next (it, 1);
11812 }
11813
11814 out:;
11815
11816 row->displays_text_p = row->used[TEXT_AREA] != 0;
11817
11818 /* Use default face for the border below the tool bar.
11819
11820 FIXME: When auto-resize-tool-bars is grow-only, there is
11821 no additional border below the possibly empty tool-bar lines.
11822 So to make the extra empty lines look "normal", we have to
11823 use the tool-bar face for the border too. */
11824 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11825 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11826 it->face_id = DEFAULT_FACE_ID;
11827
11828 extend_face_to_end_of_line (it);
11829 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11830 last->right_box_line_p = 1;
11831 if (last == row->glyphs[TEXT_AREA])
11832 last->left_box_line_p = 1;
11833
11834 /* Make line the desired height and center it vertically. */
11835 if ((height -= it->max_ascent + it->max_descent) > 0)
11836 {
11837 /* Don't add more than one line height. */
11838 height %= FRAME_LINE_HEIGHT (it->f);
11839 it->max_ascent += height / 2;
11840 it->max_descent += (height + 1) / 2;
11841 }
11842
11843 compute_line_metrics (it);
11844
11845 /* If line is empty, make it occupy the rest of the tool-bar. */
11846 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11847 {
11848 row->height = row->phys_height = it->last_visible_y - row->y;
11849 row->visible_height = row->height;
11850 row->ascent = row->phys_ascent = 0;
11851 row->extra_line_spacing = 0;
11852 }
11853
11854 row->full_width_p = 1;
11855 row->continued_p = 0;
11856 row->truncated_on_left_p = 0;
11857 row->truncated_on_right_p = 0;
11858
11859 it->current_x = it->hpos = 0;
11860 it->current_y += row->height;
11861 ++it->vpos;
11862 ++it->glyph_row;
11863 }
11864
11865
11866 /* Max tool-bar height. */
11867
11868 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11869 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11870
11871 /* Value is the number of screen lines needed to make all tool-bar
11872 items of frame F visible. The number of actual rows needed is
11873 returned in *N_ROWS if non-NULL. */
11874
11875 static int
11876 tool_bar_lines_needed (struct frame *f, int *n_rows)
11877 {
11878 struct window *w = XWINDOW (f->tool_bar_window);
11879 struct it it;
11880 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11881 the desired matrix, so use (unused) mode-line row as temporary row to
11882 avoid destroying the first tool-bar row. */
11883 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11884
11885 /* Initialize an iterator for iteration over
11886 F->desired_tool_bar_string in the tool-bar window of frame F. */
11887 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11888 it.first_visible_x = 0;
11889 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11890 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11891 it.paragraph_embedding = L2R;
11892
11893 while (!ITERATOR_AT_END_P (&it))
11894 {
11895 clear_glyph_row (temp_row);
11896 it.glyph_row = temp_row;
11897 display_tool_bar_line (&it, -1);
11898 }
11899 clear_glyph_row (temp_row);
11900
11901 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11902 if (n_rows)
11903 *n_rows = it.vpos > 0 ? it.vpos : -1;
11904
11905 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11906 }
11907
11908
11909 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11910 0, 1, 0,
11911 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11912 If FRAME is nil or omitted, use the selected frame. */)
11913 (Lisp_Object frame)
11914 {
11915 struct frame *f = decode_any_frame (frame);
11916 struct window *w;
11917 int nlines = 0;
11918
11919 if (WINDOWP (f->tool_bar_window)
11920 && (w = XWINDOW (f->tool_bar_window),
11921 WINDOW_TOTAL_LINES (w) > 0))
11922 {
11923 update_tool_bar (f, 1);
11924 if (f->n_tool_bar_items)
11925 {
11926 build_desired_tool_bar_string (f);
11927 nlines = tool_bar_lines_needed (f, NULL);
11928 }
11929 }
11930
11931 return make_number (nlines);
11932 }
11933
11934
11935 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11936 height should be changed. */
11937
11938 static int
11939 redisplay_tool_bar (struct frame *f)
11940 {
11941 struct window *w;
11942 struct it it;
11943 struct glyph_row *row;
11944
11945 #if defined (USE_GTK) || defined (HAVE_NS)
11946 if (FRAME_EXTERNAL_TOOL_BAR (f))
11947 update_frame_tool_bar (f);
11948 return 0;
11949 #endif
11950
11951 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11952 do anything. This means you must start with tool-bar-lines
11953 non-zero to get the auto-sizing effect. Or in other words, you
11954 can turn off tool-bars by specifying tool-bar-lines zero. */
11955 if (!WINDOWP (f->tool_bar_window)
11956 || (w = XWINDOW (f->tool_bar_window),
11957 WINDOW_TOTAL_LINES (w) == 0))
11958 return 0;
11959
11960 /* Set up an iterator for the tool-bar window. */
11961 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11962 it.first_visible_x = 0;
11963 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11964 row = it.glyph_row;
11965
11966 /* Build a string that represents the contents of the tool-bar. */
11967 build_desired_tool_bar_string (f);
11968 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11969 /* FIXME: This should be controlled by a user option. But it
11970 doesn't make sense to have an R2L tool bar if the menu bar cannot
11971 be drawn also R2L, and making the menu bar R2L is tricky due
11972 toolkit-specific code that implements it. If an R2L tool bar is
11973 ever supported, display_tool_bar_line should also be augmented to
11974 call unproduce_glyphs like display_line and display_string
11975 do. */
11976 it.paragraph_embedding = L2R;
11977
11978 if (f->n_tool_bar_rows == 0)
11979 {
11980 int nlines;
11981
11982 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11983 nlines != WINDOW_TOTAL_LINES (w)))
11984 {
11985 Lisp_Object frame;
11986 int old_height = WINDOW_TOTAL_LINES (w);
11987
11988 XSETFRAME (frame, f);
11989 Fmodify_frame_parameters (frame,
11990 list1 (Fcons (Qtool_bar_lines,
11991 make_number (nlines))));
11992 if (WINDOW_TOTAL_LINES (w) != old_height)
11993 {
11994 clear_glyph_matrix (w->desired_matrix);
11995 fonts_changed_p = 1;
11996 return 1;
11997 }
11998 }
11999 }
12000
12001 /* Display as many lines as needed to display all tool-bar items. */
12002
12003 if (f->n_tool_bar_rows > 0)
12004 {
12005 int border, rows, height, extra;
12006
12007 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12008 border = XINT (Vtool_bar_border);
12009 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12010 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12011 else if (EQ (Vtool_bar_border, Qborder_width))
12012 border = f->border_width;
12013 else
12014 border = 0;
12015 if (border < 0)
12016 border = 0;
12017
12018 rows = f->n_tool_bar_rows;
12019 height = max (1, (it.last_visible_y - border) / rows);
12020 extra = it.last_visible_y - border - height * rows;
12021
12022 while (it.current_y < it.last_visible_y)
12023 {
12024 int h = 0;
12025 if (extra > 0 && rows-- > 0)
12026 {
12027 h = (extra + rows - 1) / rows;
12028 extra -= h;
12029 }
12030 display_tool_bar_line (&it, height + h);
12031 }
12032 }
12033 else
12034 {
12035 while (it.current_y < it.last_visible_y)
12036 display_tool_bar_line (&it, 0);
12037 }
12038
12039 /* It doesn't make much sense to try scrolling in the tool-bar
12040 window, so don't do it. */
12041 w->desired_matrix->no_scrolling_p = 1;
12042 w->must_be_updated_p = 1;
12043
12044 if (!NILP (Vauto_resize_tool_bars))
12045 {
12046 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12047 int change_height_p = 0;
12048
12049 /* If we couldn't display everything, change the tool-bar's
12050 height if there is room for more. */
12051 if (IT_STRING_CHARPOS (it) < it.end_charpos
12052 && it.current_y < max_tool_bar_height)
12053 change_height_p = 1;
12054
12055 row = it.glyph_row - 1;
12056
12057 /* If there are blank lines at the end, except for a partially
12058 visible blank line at the end that is smaller than
12059 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12060 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
12061 && row->height >= FRAME_LINE_HEIGHT (f))
12062 change_height_p = 1;
12063
12064 /* If row displays tool-bar items, but is partially visible,
12065 change the tool-bar's height. */
12066 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12067 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12068 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12069 change_height_p = 1;
12070
12071 /* Resize windows as needed by changing the `tool-bar-lines'
12072 frame parameter. */
12073 if (change_height_p)
12074 {
12075 Lisp_Object frame;
12076 int old_height = WINDOW_TOTAL_LINES (w);
12077 int nrows;
12078 int nlines = tool_bar_lines_needed (f, &nrows);
12079
12080 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12081 && !f->minimize_tool_bar_window_p)
12082 ? (nlines > old_height)
12083 : (nlines != old_height));
12084 f->minimize_tool_bar_window_p = 0;
12085
12086 if (change_height_p)
12087 {
12088 XSETFRAME (frame, f);
12089 Fmodify_frame_parameters (frame,
12090 list1 (Fcons (Qtool_bar_lines,
12091 make_number (nlines))));
12092 if (WINDOW_TOTAL_LINES (w) != old_height)
12093 {
12094 clear_glyph_matrix (w->desired_matrix);
12095 f->n_tool_bar_rows = nrows;
12096 fonts_changed_p = 1;
12097 return 1;
12098 }
12099 }
12100 }
12101 }
12102
12103 f->minimize_tool_bar_window_p = 0;
12104 return 0;
12105 }
12106
12107
12108 /* Get information about the tool-bar item which is displayed in GLYPH
12109 on frame F. Return in *PROP_IDX the index where tool-bar item
12110 properties start in F->tool_bar_items. Value is zero if
12111 GLYPH doesn't display a tool-bar item. */
12112
12113 static int
12114 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12115 {
12116 Lisp_Object prop;
12117 int success_p;
12118 int charpos;
12119
12120 /* This function can be called asynchronously, which means we must
12121 exclude any possibility that Fget_text_property signals an
12122 error. */
12123 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12124 charpos = max (0, charpos);
12125
12126 /* Get the text property `menu-item' at pos. The value of that
12127 property is the start index of this item's properties in
12128 F->tool_bar_items. */
12129 prop = Fget_text_property (make_number (charpos),
12130 Qmenu_item, f->current_tool_bar_string);
12131 if (INTEGERP (prop))
12132 {
12133 *prop_idx = XINT (prop);
12134 success_p = 1;
12135 }
12136 else
12137 success_p = 0;
12138
12139 return success_p;
12140 }
12141
12142 \f
12143 /* Get information about the tool-bar item at position X/Y on frame F.
12144 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12145 the current matrix of the tool-bar window of F, or NULL if not
12146 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12147 item in F->tool_bar_items. Value is
12148
12149 -1 if X/Y is not on a tool-bar item
12150 0 if X/Y is on the same item that was highlighted before.
12151 1 otherwise. */
12152
12153 static int
12154 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12155 int *hpos, int *vpos, int *prop_idx)
12156 {
12157 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12158 struct window *w = XWINDOW (f->tool_bar_window);
12159 int area;
12160
12161 /* Find the glyph under X/Y. */
12162 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12163 if (*glyph == NULL)
12164 return -1;
12165
12166 /* Get the start of this tool-bar item's properties in
12167 f->tool_bar_items. */
12168 if (!tool_bar_item_info (f, *glyph, prop_idx))
12169 return -1;
12170
12171 /* Is mouse on the highlighted item? */
12172 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12173 && *vpos >= hlinfo->mouse_face_beg_row
12174 && *vpos <= hlinfo->mouse_face_end_row
12175 && (*vpos > hlinfo->mouse_face_beg_row
12176 || *hpos >= hlinfo->mouse_face_beg_col)
12177 && (*vpos < hlinfo->mouse_face_end_row
12178 || *hpos < hlinfo->mouse_face_end_col
12179 || hlinfo->mouse_face_past_end))
12180 return 0;
12181
12182 return 1;
12183 }
12184
12185
12186 /* EXPORT:
12187 Handle mouse button event on the tool-bar of frame F, at
12188 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12189 0 for button release. MODIFIERS is event modifiers for button
12190 release. */
12191
12192 void
12193 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12194 int modifiers)
12195 {
12196 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12197 struct window *w = XWINDOW (f->tool_bar_window);
12198 int hpos, vpos, prop_idx;
12199 struct glyph *glyph;
12200 Lisp_Object enabled_p;
12201 int ts;
12202
12203 /* If not on the highlighted tool-bar item, and mouse-highlight is
12204 non-nil, return. This is so we generate the tool-bar button
12205 click only when the mouse button is released on the same item as
12206 where it was pressed. However, when mouse-highlight is disabled,
12207 generate the click when the button is released regardless of the
12208 highlight, since tool-bar items are not highlighted in that
12209 case. */
12210 frame_to_window_pixel_xy (w, &x, &y);
12211 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12212 if (ts == -1
12213 || (ts != 0 && !NILP (Vmouse_highlight)))
12214 return;
12215
12216 /* When mouse-highlight is off, generate the click for the item
12217 where the button was pressed, disregarding where it was
12218 released. */
12219 if (NILP (Vmouse_highlight) && !down_p)
12220 prop_idx = last_tool_bar_item;
12221
12222 /* If item is disabled, do nothing. */
12223 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12224 if (NILP (enabled_p))
12225 return;
12226
12227 if (down_p)
12228 {
12229 /* Show item in pressed state. */
12230 if (!NILP (Vmouse_highlight))
12231 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12232 last_tool_bar_item = prop_idx;
12233 }
12234 else
12235 {
12236 Lisp_Object key, frame;
12237 struct input_event event;
12238 EVENT_INIT (event);
12239
12240 /* Show item in released state. */
12241 if (!NILP (Vmouse_highlight))
12242 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12243
12244 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12245
12246 XSETFRAME (frame, f);
12247 event.kind = TOOL_BAR_EVENT;
12248 event.frame_or_window = frame;
12249 event.arg = frame;
12250 kbd_buffer_store_event (&event);
12251
12252 event.kind = TOOL_BAR_EVENT;
12253 event.frame_or_window = frame;
12254 event.arg = key;
12255 event.modifiers = modifiers;
12256 kbd_buffer_store_event (&event);
12257 last_tool_bar_item = -1;
12258 }
12259 }
12260
12261
12262 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12263 tool-bar window-relative coordinates X/Y. Called from
12264 note_mouse_highlight. */
12265
12266 static void
12267 note_tool_bar_highlight (struct frame *f, int x, int y)
12268 {
12269 Lisp_Object window = f->tool_bar_window;
12270 struct window *w = XWINDOW (window);
12271 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12272 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12273 int hpos, vpos;
12274 struct glyph *glyph;
12275 struct glyph_row *row;
12276 int i;
12277 Lisp_Object enabled_p;
12278 int prop_idx;
12279 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12280 int mouse_down_p, rc;
12281
12282 /* Function note_mouse_highlight is called with negative X/Y
12283 values when mouse moves outside of the frame. */
12284 if (x <= 0 || y <= 0)
12285 {
12286 clear_mouse_face (hlinfo);
12287 return;
12288 }
12289
12290 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12291 if (rc < 0)
12292 {
12293 /* Not on tool-bar item. */
12294 clear_mouse_face (hlinfo);
12295 return;
12296 }
12297 else if (rc == 0)
12298 /* On same tool-bar item as before. */
12299 goto set_help_echo;
12300
12301 clear_mouse_face (hlinfo);
12302
12303 /* Mouse is down, but on different tool-bar item? */
12304 mouse_down_p = (dpyinfo->grabbed
12305 && f == last_mouse_frame
12306 && FRAME_LIVE_P (f));
12307 if (mouse_down_p
12308 && last_tool_bar_item != prop_idx)
12309 return;
12310
12311 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12312
12313 /* If tool-bar item is not enabled, don't highlight it. */
12314 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12315 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12316 {
12317 /* Compute the x-position of the glyph. In front and past the
12318 image is a space. We include this in the highlighted area. */
12319 row = MATRIX_ROW (w->current_matrix, vpos);
12320 for (i = x = 0; i < hpos; ++i)
12321 x += row->glyphs[TEXT_AREA][i].pixel_width;
12322
12323 /* Record this as the current active region. */
12324 hlinfo->mouse_face_beg_col = hpos;
12325 hlinfo->mouse_face_beg_row = vpos;
12326 hlinfo->mouse_face_beg_x = x;
12327 hlinfo->mouse_face_beg_y = row->y;
12328 hlinfo->mouse_face_past_end = 0;
12329
12330 hlinfo->mouse_face_end_col = hpos + 1;
12331 hlinfo->mouse_face_end_row = vpos;
12332 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12333 hlinfo->mouse_face_end_y = row->y;
12334 hlinfo->mouse_face_window = window;
12335 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12336
12337 /* Display it as active. */
12338 show_mouse_face (hlinfo, draw);
12339 }
12340
12341 set_help_echo:
12342
12343 /* Set help_echo_string to a help string to display for this tool-bar item.
12344 XTread_socket does the rest. */
12345 help_echo_object = help_echo_window = Qnil;
12346 help_echo_pos = -1;
12347 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12348 if (NILP (help_echo_string))
12349 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12350 }
12351
12352 #endif /* HAVE_WINDOW_SYSTEM */
12353
12354
12355 \f
12356 /************************************************************************
12357 Horizontal scrolling
12358 ************************************************************************/
12359
12360 static int hscroll_window_tree (Lisp_Object);
12361 static int hscroll_windows (Lisp_Object);
12362
12363 /* For all leaf windows in the window tree rooted at WINDOW, set their
12364 hscroll value so that PT is (i) visible in the window, and (ii) so
12365 that it is not within a certain margin at the window's left and
12366 right border. Value is non-zero if any window's hscroll has been
12367 changed. */
12368
12369 static int
12370 hscroll_window_tree (Lisp_Object window)
12371 {
12372 int hscrolled_p = 0;
12373 int hscroll_relative_p = FLOATP (Vhscroll_step);
12374 int hscroll_step_abs = 0;
12375 double hscroll_step_rel = 0;
12376
12377 if (hscroll_relative_p)
12378 {
12379 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12380 if (hscroll_step_rel < 0)
12381 {
12382 hscroll_relative_p = 0;
12383 hscroll_step_abs = 0;
12384 }
12385 }
12386 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12387 {
12388 hscroll_step_abs = XINT (Vhscroll_step);
12389 if (hscroll_step_abs < 0)
12390 hscroll_step_abs = 0;
12391 }
12392 else
12393 hscroll_step_abs = 0;
12394
12395 while (WINDOWP (window))
12396 {
12397 struct window *w = XWINDOW (window);
12398
12399 if (WINDOWP (w->contents))
12400 hscrolled_p |= hscroll_window_tree (w->contents);
12401 else if (w->cursor.vpos >= 0)
12402 {
12403 int h_margin;
12404 int text_area_width;
12405 struct glyph_row *current_cursor_row
12406 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12407 struct glyph_row *desired_cursor_row
12408 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12409 struct glyph_row *cursor_row
12410 = (desired_cursor_row->enabled_p
12411 ? desired_cursor_row
12412 : current_cursor_row);
12413 int row_r2l_p = cursor_row->reversed_p;
12414
12415 text_area_width = window_box_width (w, TEXT_AREA);
12416
12417 /* Scroll when cursor is inside this scroll margin. */
12418 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12419
12420 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12421 /* For left-to-right rows, hscroll when cursor is either
12422 (i) inside the right hscroll margin, or (ii) if it is
12423 inside the left margin and the window is already
12424 hscrolled. */
12425 && ((!row_r2l_p
12426 && ((w->hscroll
12427 && w->cursor.x <= h_margin)
12428 || (cursor_row->enabled_p
12429 && cursor_row->truncated_on_right_p
12430 && (w->cursor.x >= text_area_width - h_margin))))
12431 /* For right-to-left rows, the logic is similar,
12432 except that rules for scrolling to left and right
12433 are reversed. E.g., if cursor.x <= h_margin, we
12434 need to hscroll "to the right" unconditionally,
12435 and that will scroll the screen to the left so as
12436 to reveal the next portion of the row. */
12437 || (row_r2l_p
12438 && ((cursor_row->enabled_p
12439 /* FIXME: It is confusing to set the
12440 truncated_on_right_p flag when R2L rows
12441 are actually truncated on the left. */
12442 && cursor_row->truncated_on_right_p
12443 && w->cursor.x <= h_margin)
12444 || (w->hscroll
12445 && (w->cursor.x >= text_area_width - h_margin))))))
12446 {
12447 struct it it;
12448 ptrdiff_t hscroll;
12449 struct buffer *saved_current_buffer;
12450 ptrdiff_t pt;
12451 int wanted_x;
12452
12453 /* Find point in a display of infinite width. */
12454 saved_current_buffer = current_buffer;
12455 current_buffer = XBUFFER (w->contents);
12456
12457 if (w == XWINDOW (selected_window))
12458 pt = PT;
12459 else
12460 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12461
12462 /* Move iterator to pt starting at cursor_row->start in
12463 a line with infinite width. */
12464 init_to_row_start (&it, w, cursor_row);
12465 it.last_visible_x = INFINITY;
12466 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12467 current_buffer = saved_current_buffer;
12468
12469 /* Position cursor in window. */
12470 if (!hscroll_relative_p && hscroll_step_abs == 0)
12471 hscroll = max (0, (it.current_x
12472 - (ITERATOR_AT_END_OF_LINE_P (&it)
12473 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12474 : (text_area_width / 2))))
12475 / FRAME_COLUMN_WIDTH (it.f);
12476 else if ((!row_r2l_p
12477 && w->cursor.x >= text_area_width - h_margin)
12478 || (row_r2l_p && w->cursor.x <= h_margin))
12479 {
12480 if (hscroll_relative_p)
12481 wanted_x = text_area_width * (1 - hscroll_step_rel)
12482 - h_margin;
12483 else
12484 wanted_x = text_area_width
12485 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12486 - h_margin;
12487 hscroll
12488 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12489 }
12490 else
12491 {
12492 if (hscroll_relative_p)
12493 wanted_x = text_area_width * hscroll_step_rel
12494 + h_margin;
12495 else
12496 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12497 + h_margin;
12498 hscroll
12499 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12500 }
12501 hscroll = max (hscroll, w->min_hscroll);
12502
12503 /* Don't prevent redisplay optimizations if hscroll
12504 hasn't changed, as it will unnecessarily slow down
12505 redisplay. */
12506 if (w->hscroll != hscroll)
12507 {
12508 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12509 w->hscroll = hscroll;
12510 hscrolled_p = 1;
12511 }
12512 }
12513 }
12514
12515 window = w->next;
12516 }
12517
12518 /* Value is non-zero if hscroll of any leaf window has been changed. */
12519 return hscrolled_p;
12520 }
12521
12522
12523 /* Set hscroll so that cursor is visible and not inside horizontal
12524 scroll margins for all windows in the tree rooted at WINDOW. See
12525 also hscroll_window_tree above. Value is non-zero if any window's
12526 hscroll has been changed. If it has, desired matrices on the frame
12527 of WINDOW are cleared. */
12528
12529 static int
12530 hscroll_windows (Lisp_Object window)
12531 {
12532 int hscrolled_p = hscroll_window_tree (window);
12533 if (hscrolled_p)
12534 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12535 return hscrolled_p;
12536 }
12537
12538
12539 \f
12540 /************************************************************************
12541 Redisplay
12542 ************************************************************************/
12543
12544 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12545 to a non-zero value. This is sometimes handy to have in a debugger
12546 session. */
12547
12548 #ifdef GLYPH_DEBUG
12549
12550 /* First and last unchanged row for try_window_id. */
12551
12552 static int debug_first_unchanged_at_end_vpos;
12553 static int debug_last_unchanged_at_beg_vpos;
12554
12555 /* Delta vpos and y. */
12556
12557 static int debug_dvpos, debug_dy;
12558
12559 /* Delta in characters and bytes for try_window_id. */
12560
12561 static ptrdiff_t debug_delta, debug_delta_bytes;
12562
12563 /* Values of window_end_pos and window_end_vpos at the end of
12564 try_window_id. */
12565
12566 static ptrdiff_t debug_end_vpos;
12567
12568 /* Append a string to W->desired_matrix->method. FMT is a printf
12569 format string. If trace_redisplay_p is non-zero also printf the
12570 resulting string to stderr. */
12571
12572 static void debug_method_add (struct window *, char const *, ...)
12573 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12574
12575 static void
12576 debug_method_add (struct window *w, char const *fmt, ...)
12577 {
12578 void *ptr = w;
12579 char *method = w->desired_matrix->method;
12580 int len = strlen (method);
12581 int size = sizeof w->desired_matrix->method;
12582 int remaining = size - len - 1;
12583 va_list ap;
12584
12585 if (len && remaining)
12586 {
12587 method[len] = '|';
12588 --remaining, ++len;
12589 }
12590
12591 va_start (ap, fmt);
12592 vsnprintf (method + len, remaining + 1, fmt, ap);
12593 va_end (ap);
12594
12595 if (trace_redisplay_p)
12596 fprintf (stderr, "%p (%s): %s\n",
12597 ptr,
12598 ((BUFFERP (w->contents)
12599 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12600 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12601 : "no buffer"),
12602 method + len);
12603 }
12604
12605 #endif /* GLYPH_DEBUG */
12606
12607
12608 /* Value is non-zero if all changes in window W, which displays
12609 current_buffer, are in the text between START and END. START is a
12610 buffer position, END is given as a distance from Z. Used in
12611 redisplay_internal for display optimization. */
12612
12613 static int
12614 text_outside_line_unchanged_p (struct window *w,
12615 ptrdiff_t start, ptrdiff_t end)
12616 {
12617 int unchanged_p = 1;
12618
12619 /* If text or overlays have changed, see where. */
12620 if (window_outdated (w))
12621 {
12622 /* Gap in the line? */
12623 if (GPT < start || Z - GPT < end)
12624 unchanged_p = 0;
12625
12626 /* Changes start in front of the line, or end after it? */
12627 if (unchanged_p
12628 && (BEG_UNCHANGED < start - 1
12629 || END_UNCHANGED < end))
12630 unchanged_p = 0;
12631
12632 /* If selective display, can't optimize if changes start at the
12633 beginning of the line. */
12634 if (unchanged_p
12635 && INTEGERP (BVAR (current_buffer, selective_display))
12636 && XINT (BVAR (current_buffer, selective_display)) > 0
12637 && (BEG_UNCHANGED < start || GPT <= start))
12638 unchanged_p = 0;
12639
12640 /* If there are overlays at the start or end of the line, these
12641 may have overlay strings with newlines in them. A change at
12642 START, for instance, may actually concern the display of such
12643 overlay strings as well, and they are displayed on different
12644 lines. So, quickly rule out this case. (For the future, it
12645 might be desirable to implement something more telling than
12646 just BEG/END_UNCHANGED.) */
12647 if (unchanged_p)
12648 {
12649 if (BEG + BEG_UNCHANGED == start
12650 && overlay_touches_p (start))
12651 unchanged_p = 0;
12652 if (END_UNCHANGED == end
12653 && overlay_touches_p (Z - end))
12654 unchanged_p = 0;
12655 }
12656
12657 /* Under bidi reordering, adding or deleting a character in the
12658 beginning of a paragraph, before the first strong directional
12659 character, can change the base direction of the paragraph (unless
12660 the buffer specifies a fixed paragraph direction), which will
12661 require to redisplay the whole paragraph. It might be worthwhile
12662 to find the paragraph limits and widen the range of redisplayed
12663 lines to that, but for now just give up this optimization. */
12664 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12665 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12666 unchanged_p = 0;
12667 }
12668
12669 return unchanged_p;
12670 }
12671
12672
12673 /* Do a frame update, taking possible shortcuts into account. This is
12674 the main external entry point for redisplay.
12675
12676 If the last redisplay displayed an echo area message and that message
12677 is no longer requested, we clear the echo area or bring back the
12678 mini-buffer if that is in use. */
12679
12680 void
12681 redisplay (void)
12682 {
12683 redisplay_internal ();
12684 }
12685
12686
12687 static Lisp_Object
12688 overlay_arrow_string_or_property (Lisp_Object var)
12689 {
12690 Lisp_Object val;
12691
12692 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12693 return val;
12694
12695 return Voverlay_arrow_string;
12696 }
12697
12698 /* Return 1 if there are any overlay-arrows in current_buffer. */
12699 static int
12700 overlay_arrow_in_current_buffer_p (void)
12701 {
12702 Lisp_Object vlist;
12703
12704 for (vlist = Voverlay_arrow_variable_list;
12705 CONSP (vlist);
12706 vlist = XCDR (vlist))
12707 {
12708 Lisp_Object var = XCAR (vlist);
12709 Lisp_Object val;
12710
12711 if (!SYMBOLP (var))
12712 continue;
12713 val = find_symbol_value (var);
12714 if (MARKERP (val)
12715 && current_buffer == XMARKER (val)->buffer)
12716 return 1;
12717 }
12718 return 0;
12719 }
12720
12721
12722 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12723 has changed. */
12724
12725 static int
12726 overlay_arrows_changed_p (void)
12727 {
12728 Lisp_Object vlist;
12729
12730 for (vlist = Voverlay_arrow_variable_list;
12731 CONSP (vlist);
12732 vlist = XCDR (vlist))
12733 {
12734 Lisp_Object var = XCAR (vlist);
12735 Lisp_Object val, pstr;
12736
12737 if (!SYMBOLP (var))
12738 continue;
12739 val = find_symbol_value (var);
12740 if (!MARKERP (val))
12741 continue;
12742 if (! EQ (COERCE_MARKER (val),
12743 Fget (var, Qlast_arrow_position))
12744 || ! (pstr = overlay_arrow_string_or_property (var),
12745 EQ (pstr, Fget (var, Qlast_arrow_string))))
12746 return 1;
12747 }
12748 return 0;
12749 }
12750
12751 /* Mark overlay arrows to be updated on next redisplay. */
12752
12753 static void
12754 update_overlay_arrows (int up_to_date)
12755 {
12756 Lisp_Object vlist;
12757
12758 for (vlist = Voverlay_arrow_variable_list;
12759 CONSP (vlist);
12760 vlist = XCDR (vlist))
12761 {
12762 Lisp_Object var = XCAR (vlist);
12763
12764 if (!SYMBOLP (var))
12765 continue;
12766
12767 if (up_to_date > 0)
12768 {
12769 Lisp_Object val = find_symbol_value (var);
12770 Fput (var, Qlast_arrow_position,
12771 COERCE_MARKER (val));
12772 Fput (var, Qlast_arrow_string,
12773 overlay_arrow_string_or_property (var));
12774 }
12775 else if (up_to_date < 0
12776 || !NILP (Fget (var, Qlast_arrow_position)))
12777 {
12778 Fput (var, Qlast_arrow_position, Qt);
12779 Fput (var, Qlast_arrow_string, Qt);
12780 }
12781 }
12782 }
12783
12784
12785 /* Return overlay arrow string to display at row.
12786 Return integer (bitmap number) for arrow bitmap in left fringe.
12787 Return nil if no overlay arrow. */
12788
12789 static Lisp_Object
12790 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12791 {
12792 Lisp_Object vlist;
12793
12794 for (vlist = Voverlay_arrow_variable_list;
12795 CONSP (vlist);
12796 vlist = XCDR (vlist))
12797 {
12798 Lisp_Object var = XCAR (vlist);
12799 Lisp_Object val;
12800
12801 if (!SYMBOLP (var))
12802 continue;
12803
12804 val = find_symbol_value (var);
12805
12806 if (MARKERP (val)
12807 && current_buffer == XMARKER (val)->buffer
12808 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12809 {
12810 if (FRAME_WINDOW_P (it->f)
12811 /* FIXME: if ROW->reversed_p is set, this should test
12812 the right fringe, not the left one. */
12813 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12814 {
12815 #ifdef HAVE_WINDOW_SYSTEM
12816 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12817 {
12818 int fringe_bitmap;
12819 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12820 return make_number (fringe_bitmap);
12821 }
12822 #endif
12823 return make_number (-1); /* Use default arrow bitmap. */
12824 }
12825 return overlay_arrow_string_or_property (var);
12826 }
12827 }
12828
12829 return Qnil;
12830 }
12831
12832 /* Return 1 if point moved out of or into a composition. Otherwise
12833 return 0. PREV_BUF and PREV_PT are the last point buffer and
12834 position. BUF and PT are the current point buffer and position. */
12835
12836 static int
12837 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12838 struct buffer *buf, ptrdiff_t pt)
12839 {
12840 ptrdiff_t start, end;
12841 Lisp_Object prop;
12842 Lisp_Object buffer;
12843
12844 XSETBUFFER (buffer, buf);
12845 /* Check a composition at the last point if point moved within the
12846 same buffer. */
12847 if (prev_buf == buf)
12848 {
12849 if (prev_pt == pt)
12850 /* Point didn't move. */
12851 return 0;
12852
12853 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12854 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12855 && COMPOSITION_VALID_P (start, end, prop)
12856 && start < prev_pt && end > prev_pt)
12857 /* The last point was within the composition. Return 1 iff
12858 point moved out of the composition. */
12859 return (pt <= start || pt >= end);
12860 }
12861
12862 /* Check a composition at the current point. */
12863 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12864 && find_composition (pt, -1, &start, &end, &prop, buffer)
12865 && COMPOSITION_VALID_P (start, end, prop)
12866 && start < pt && end > pt);
12867 }
12868
12869
12870 /* Reconsider the setting of B->clip_changed which is displayed
12871 in window W. */
12872
12873 static void
12874 reconsider_clip_changes (struct window *w, struct buffer *b)
12875 {
12876 if (b->clip_changed
12877 && w->window_end_valid
12878 && w->current_matrix->buffer == b
12879 && w->current_matrix->zv == BUF_ZV (b)
12880 && w->current_matrix->begv == BUF_BEGV (b))
12881 b->clip_changed = 0;
12882
12883 /* If display wasn't paused, and W is not a tool bar window, see if
12884 point has been moved into or out of a composition. In that case,
12885 we set b->clip_changed to 1 to force updating the screen. If
12886 b->clip_changed has already been set to 1, we can skip this
12887 check. */
12888 if (!b->clip_changed && BUFFERP (w->contents) && w->window_end_valid)
12889 {
12890 ptrdiff_t pt;
12891
12892 if (w == XWINDOW (selected_window))
12893 pt = PT;
12894 else
12895 pt = marker_position (w->pointm);
12896
12897 if ((w->current_matrix->buffer != XBUFFER (w->contents)
12898 || pt != w->last_point)
12899 && check_point_in_composition (w->current_matrix->buffer,
12900 w->last_point,
12901 XBUFFER (w->contents), pt))
12902 b->clip_changed = 1;
12903 }
12904 }
12905 \f
12906
12907 #define STOP_POLLING \
12908 do { if (! polling_stopped_here) stop_polling (); \
12909 polling_stopped_here = 1; } while (0)
12910
12911 #define RESUME_POLLING \
12912 do { if (polling_stopped_here) start_polling (); \
12913 polling_stopped_here = 0; } while (0)
12914
12915
12916 /* Perhaps in the future avoid recentering windows if it
12917 is not necessary; currently that causes some problems. */
12918
12919 static void
12920 redisplay_internal (void)
12921 {
12922 struct window *w = XWINDOW (selected_window);
12923 struct window *sw;
12924 struct frame *fr;
12925 int pending;
12926 int must_finish = 0;
12927 struct text_pos tlbufpos, tlendpos;
12928 int number_of_visible_frames;
12929 ptrdiff_t count, count1;
12930 struct frame *sf;
12931 int polling_stopped_here = 0;
12932 Lisp_Object tail, frame;
12933
12934 /* Non-zero means redisplay has to consider all windows on all
12935 frames. Zero means, only selected_window is considered. */
12936 int consider_all_windows_p;
12937
12938 /* Non-zero means redisplay has to redisplay the miniwindow. */
12939 int update_miniwindow_p = 0;
12940
12941 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12942
12943 /* No redisplay if running in batch mode or frame is not yet fully
12944 initialized, or redisplay is explicitly turned off by setting
12945 Vinhibit_redisplay. */
12946 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12947 || !NILP (Vinhibit_redisplay))
12948 return;
12949
12950 /* Don't examine these until after testing Vinhibit_redisplay.
12951 When Emacs is shutting down, perhaps because its connection to
12952 X has dropped, we should not look at them at all. */
12953 fr = XFRAME (w->frame);
12954 sf = SELECTED_FRAME ();
12955
12956 if (!fr->glyphs_initialized_p)
12957 return;
12958
12959 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12960 if (popup_activated ())
12961 return;
12962 #endif
12963
12964 /* I don't think this happens but let's be paranoid. */
12965 if (redisplaying_p)
12966 return;
12967
12968 /* Record a function that clears redisplaying_p
12969 when we leave this function. */
12970 count = SPECPDL_INDEX ();
12971 record_unwind_protect_void (unwind_redisplay);
12972 redisplaying_p = 1;
12973 specbind (Qinhibit_free_realized_faces, Qnil);
12974
12975 /* Record this function, so it appears on the profiler's backtraces. */
12976 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12977
12978 FOR_EACH_FRAME (tail, frame)
12979 XFRAME (frame)->already_hscrolled_p = 0;
12980
12981 retry:
12982 /* Remember the currently selected window. */
12983 sw = w;
12984
12985 pending = 0;
12986 reconsider_clip_changes (w, current_buffer);
12987 last_escape_glyph_frame = NULL;
12988 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12989 last_glyphless_glyph_frame = NULL;
12990 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12991
12992 /* If new fonts have been loaded that make a glyph matrix adjustment
12993 necessary, do it. */
12994 if (fonts_changed_p)
12995 {
12996 adjust_glyphs (NULL);
12997 ++windows_or_buffers_changed;
12998 fonts_changed_p = 0;
12999 }
13000
13001 /* If face_change_count is non-zero, init_iterator will free all
13002 realized faces, which includes the faces referenced from current
13003 matrices. So, we can't reuse current matrices in this case. */
13004 if (face_change_count)
13005 ++windows_or_buffers_changed;
13006
13007 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13008 && FRAME_TTY (sf)->previous_frame != sf)
13009 {
13010 /* Since frames on a single ASCII terminal share the same
13011 display area, displaying a different frame means redisplay
13012 the whole thing. */
13013 windows_or_buffers_changed++;
13014 SET_FRAME_GARBAGED (sf);
13015 #ifndef DOS_NT
13016 set_tty_color_mode (FRAME_TTY (sf), sf);
13017 #endif
13018 FRAME_TTY (sf)->previous_frame = sf;
13019 }
13020
13021 /* Set the visible flags for all frames. Do this before checking for
13022 resized or garbaged frames; they want to know if their frames are
13023 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13024 number_of_visible_frames = 0;
13025
13026 FOR_EACH_FRAME (tail, frame)
13027 {
13028 struct frame *f = XFRAME (frame);
13029
13030 if (FRAME_VISIBLE_P (f))
13031 ++number_of_visible_frames;
13032 clear_desired_matrices (f);
13033 }
13034
13035 /* Notice any pending interrupt request to change frame size. */
13036 do_pending_window_change (1);
13037
13038 /* do_pending_window_change could change the selected_window due to
13039 frame resizing which makes the selected window too small. */
13040 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13041 {
13042 sw = w;
13043 reconsider_clip_changes (w, current_buffer);
13044 }
13045
13046 /* Clear frames marked as garbaged. */
13047 clear_garbaged_frames ();
13048
13049 /* Build menubar and tool-bar items. */
13050 if (NILP (Vmemory_full))
13051 prepare_menu_bars ();
13052
13053 if (windows_or_buffers_changed)
13054 update_mode_lines++;
13055
13056 /* Detect case that we need to write or remove a star in the mode line. */
13057 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13058 {
13059 w->update_mode_line = 1;
13060 if (buffer_shared_and_changed ())
13061 update_mode_lines++;
13062 }
13063
13064 /* Avoid invocation of point motion hooks by `current_column' below. */
13065 count1 = SPECPDL_INDEX ();
13066 specbind (Qinhibit_point_motion_hooks, Qt);
13067
13068 if (mode_line_update_needed (w))
13069 w->update_mode_line = 1;
13070
13071 unbind_to (count1, Qnil);
13072
13073 consider_all_windows_p = (update_mode_lines
13074 || buffer_shared_and_changed ()
13075 || cursor_type_changed);
13076
13077 /* If specs for an arrow have changed, do thorough redisplay
13078 to ensure we remove any arrow that should no longer exist. */
13079 if (overlay_arrows_changed_p ())
13080 consider_all_windows_p = windows_or_buffers_changed = 1;
13081
13082 /* Normally the message* functions will have already displayed and
13083 updated the echo area, but the frame may have been trashed, or
13084 the update may have been preempted, so display the echo area
13085 again here. Checking message_cleared_p captures the case that
13086 the echo area should be cleared. */
13087 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13088 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13089 || (message_cleared_p
13090 && minibuf_level == 0
13091 /* If the mini-window is currently selected, this means the
13092 echo-area doesn't show through. */
13093 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13094 {
13095 int window_height_changed_p = echo_area_display (0);
13096
13097 if (message_cleared_p)
13098 update_miniwindow_p = 1;
13099
13100 must_finish = 1;
13101
13102 /* If we don't display the current message, don't clear the
13103 message_cleared_p flag, because, if we did, we wouldn't clear
13104 the echo area in the next redisplay which doesn't preserve
13105 the echo area. */
13106 if (!display_last_displayed_message_p)
13107 message_cleared_p = 0;
13108
13109 if (fonts_changed_p)
13110 goto retry;
13111 else if (window_height_changed_p)
13112 {
13113 consider_all_windows_p = 1;
13114 ++update_mode_lines;
13115 ++windows_or_buffers_changed;
13116
13117 /* If window configuration was changed, frames may have been
13118 marked garbaged. Clear them or we will experience
13119 surprises wrt scrolling. */
13120 clear_garbaged_frames ();
13121 }
13122 }
13123 else if (EQ (selected_window, minibuf_window)
13124 && (current_buffer->clip_changed || window_outdated (w))
13125 && resize_mini_window (w, 0))
13126 {
13127 /* Resized active mini-window to fit the size of what it is
13128 showing if its contents might have changed. */
13129 must_finish = 1;
13130 /* FIXME: this causes all frames to be updated, which seems unnecessary
13131 since only the current frame needs to be considered. This function
13132 needs to be rewritten with two variables, consider_all_windows and
13133 consider_all_frames. */
13134 consider_all_windows_p = 1;
13135 ++windows_or_buffers_changed;
13136 ++update_mode_lines;
13137
13138 /* If window configuration was changed, frames may have been
13139 marked garbaged. Clear them or we will experience
13140 surprises wrt scrolling. */
13141 clear_garbaged_frames ();
13142 }
13143
13144 /* If showing the region, and mark has changed, we must redisplay
13145 the whole window. The assignment to this_line_start_pos prevents
13146 the optimization directly below this if-statement. */
13147 if (((!NILP (Vtransient_mark_mode)
13148 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13149 != (w->region_showing > 0))
13150 || (w->region_showing
13151 && w->region_showing
13152 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13153 CHARPOS (this_line_start_pos) = 0;
13154
13155 /* Optimize the case that only the line containing the cursor in the
13156 selected window has changed. Variables starting with this_ are
13157 set in display_line and record information about the line
13158 containing the cursor. */
13159 tlbufpos = this_line_start_pos;
13160 tlendpos = this_line_end_pos;
13161 if (!consider_all_windows_p
13162 && CHARPOS (tlbufpos) > 0
13163 && !w->update_mode_line
13164 && !current_buffer->clip_changed
13165 && !current_buffer->prevent_redisplay_optimizations_p
13166 && FRAME_VISIBLE_P (XFRAME (w->frame))
13167 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13168 /* Make sure recorded data applies to current buffer, etc. */
13169 && this_line_buffer == current_buffer
13170 && current_buffer == XBUFFER (w->contents)
13171 && !w->force_start
13172 && !w->optional_new_start
13173 /* Point must be on the line that we have info recorded about. */
13174 && PT >= CHARPOS (tlbufpos)
13175 && PT <= Z - CHARPOS (tlendpos)
13176 /* All text outside that line, including its final newline,
13177 must be unchanged. */
13178 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13179 CHARPOS (tlendpos)))
13180 {
13181 if (CHARPOS (tlbufpos) > BEGV
13182 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13183 && (CHARPOS (tlbufpos) == ZV
13184 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13185 /* Former continuation line has disappeared by becoming empty. */
13186 goto cancel;
13187 else if (window_outdated (w) || MINI_WINDOW_P (w))
13188 {
13189 /* We have to handle the case of continuation around a
13190 wide-column character (see the comment in indent.c around
13191 line 1340).
13192
13193 For instance, in the following case:
13194
13195 -------- Insert --------
13196 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13197 J_I_ ==> J_I_ `^^' are cursors.
13198 ^^ ^^
13199 -------- --------
13200
13201 As we have to redraw the line above, we cannot use this
13202 optimization. */
13203
13204 struct it it;
13205 int line_height_before = this_line_pixel_height;
13206
13207 /* Note that start_display will handle the case that the
13208 line starting at tlbufpos is a continuation line. */
13209 start_display (&it, w, tlbufpos);
13210
13211 /* Implementation note: It this still necessary? */
13212 if (it.current_x != this_line_start_x)
13213 goto cancel;
13214
13215 TRACE ((stderr, "trying display optimization 1\n"));
13216 w->cursor.vpos = -1;
13217 overlay_arrow_seen = 0;
13218 it.vpos = this_line_vpos;
13219 it.current_y = this_line_y;
13220 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13221 display_line (&it);
13222
13223 /* If line contains point, is not continued,
13224 and ends at same distance from eob as before, we win. */
13225 if (w->cursor.vpos >= 0
13226 /* Line is not continued, otherwise this_line_start_pos
13227 would have been set to 0 in display_line. */
13228 && CHARPOS (this_line_start_pos)
13229 /* Line ends as before. */
13230 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13231 /* Line has same height as before. Otherwise other lines
13232 would have to be shifted up or down. */
13233 && this_line_pixel_height == line_height_before)
13234 {
13235 /* If this is not the window's last line, we must adjust
13236 the charstarts of the lines below. */
13237 if (it.current_y < it.last_visible_y)
13238 {
13239 struct glyph_row *row
13240 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13241 ptrdiff_t delta, delta_bytes;
13242
13243 /* We used to distinguish between two cases here,
13244 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13245 when the line ends in a newline or the end of the
13246 buffer's accessible portion. But both cases did
13247 the same, so they were collapsed. */
13248 delta = (Z
13249 - CHARPOS (tlendpos)
13250 - MATRIX_ROW_START_CHARPOS (row));
13251 delta_bytes = (Z_BYTE
13252 - BYTEPOS (tlendpos)
13253 - MATRIX_ROW_START_BYTEPOS (row));
13254
13255 increment_matrix_positions (w->current_matrix,
13256 this_line_vpos + 1,
13257 w->current_matrix->nrows,
13258 delta, delta_bytes);
13259 }
13260
13261 /* If this row displays text now but previously didn't,
13262 or vice versa, w->window_end_vpos may have to be
13263 adjusted. */
13264 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13265 {
13266 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13267 wset_window_end_vpos (w, make_number (this_line_vpos));
13268 }
13269 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13270 && this_line_vpos > 0)
13271 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13272 w->window_end_valid = 0;
13273
13274 /* Update hint: No need to try to scroll in update_window. */
13275 w->desired_matrix->no_scrolling_p = 1;
13276
13277 #ifdef GLYPH_DEBUG
13278 *w->desired_matrix->method = 0;
13279 debug_method_add (w, "optimization 1");
13280 #endif
13281 #ifdef HAVE_WINDOW_SYSTEM
13282 update_window_fringes (w, 0);
13283 #endif
13284 goto update;
13285 }
13286 else
13287 goto cancel;
13288 }
13289 else if (/* Cursor position hasn't changed. */
13290 PT == w->last_point
13291 /* Make sure the cursor was last displayed
13292 in this window. Otherwise we have to reposition it. */
13293 && 0 <= w->cursor.vpos
13294 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13295 {
13296 if (!must_finish)
13297 {
13298 do_pending_window_change (1);
13299 /* If selected_window changed, redisplay again. */
13300 if (WINDOWP (selected_window)
13301 && (w = XWINDOW (selected_window)) != sw)
13302 goto retry;
13303
13304 /* We used to always goto end_of_redisplay here, but this
13305 isn't enough if we have a blinking cursor. */
13306 if (w->cursor_off_p == w->last_cursor_off_p)
13307 goto end_of_redisplay;
13308 }
13309 goto update;
13310 }
13311 /* If highlighting the region, or if the cursor is in the echo area,
13312 then we can't just move the cursor. */
13313 else if (! (!NILP (Vtransient_mark_mode)
13314 && !NILP (BVAR (current_buffer, mark_active)))
13315 && (EQ (selected_window,
13316 BVAR (current_buffer, last_selected_window))
13317 || highlight_nonselected_windows)
13318 && !w->region_showing
13319 && NILP (Vshow_trailing_whitespace)
13320 && !cursor_in_echo_area)
13321 {
13322 struct it it;
13323 struct glyph_row *row;
13324
13325 /* Skip from tlbufpos to PT and see where it is. Note that
13326 PT may be in invisible text. If so, we will end at the
13327 next visible position. */
13328 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13329 NULL, DEFAULT_FACE_ID);
13330 it.current_x = this_line_start_x;
13331 it.current_y = this_line_y;
13332 it.vpos = this_line_vpos;
13333
13334 /* The call to move_it_to stops in front of PT, but
13335 moves over before-strings. */
13336 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13337
13338 if (it.vpos == this_line_vpos
13339 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13340 row->enabled_p))
13341 {
13342 eassert (this_line_vpos == it.vpos);
13343 eassert (this_line_y == it.current_y);
13344 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13345 #ifdef GLYPH_DEBUG
13346 *w->desired_matrix->method = 0;
13347 debug_method_add (w, "optimization 3");
13348 #endif
13349 goto update;
13350 }
13351 else
13352 goto cancel;
13353 }
13354
13355 cancel:
13356 /* Text changed drastically or point moved off of line. */
13357 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13358 }
13359
13360 CHARPOS (this_line_start_pos) = 0;
13361 consider_all_windows_p |= buffer_shared_and_changed ();
13362 ++clear_face_cache_count;
13363 #ifdef HAVE_WINDOW_SYSTEM
13364 ++clear_image_cache_count;
13365 #endif
13366
13367 /* Build desired matrices, and update the display. If
13368 consider_all_windows_p is non-zero, do it for all windows on all
13369 frames. Otherwise do it for selected_window, only. */
13370
13371 if (consider_all_windows_p)
13372 {
13373 FOR_EACH_FRAME (tail, frame)
13374 XFRAME (frame)->updated_p = 0;
13375
13376 FOR_EACH_FRAME (tail, frame)
13377 {
13378 struct frame *f = XFRAME (frame);
13379
13380 /* We don't have to do anything for unselected terminal
13381 frames. */
13382 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13383 && !EQ (FRAME_TTY (f)->top_frame, frame))
13384 continue;
13385
13386 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13387 {
13388 /* Mark all the scroll bars to be removed; we'll redeem
13389 the ones we want when we redisplay their windows. */
13390 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13391 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13392
13393 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13394 redisplay_windows (FRAME_ROOT_WINDOW (f));
13395
13396 /* The X error handler may have deleted that frame. */
13397 if (!FRAME_LIVE_P (f))
13398 continue;
13399
13400 /* Any scroll bars which redisplay_windows should have
13401 nuked should now go away. */
13402 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13403 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13404
13405 /* If fonts changed, display again. */
13406 /* ??? rms: I suspect it is a mistake to jump all the way
13407 back to retry here. It should just retry this frame. */
13408 if (fonts_changed_p)
13409 goto retry;
13410
13411 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13412 {
13413 /* See if we have to hscroll. */
13414 if (!f->already_hscrolled_p)
13415 {
13416 f->already_hscrolled_p = 1;
13417 if (hscroll_windows (f->root_window))
13418 goto retry;
13419 }
13420
13421 /* Prevent various kinds of signals during display
13422 update. stdio is not robust about handling
13423 signals, which can cause an apparent I/O
13424 error. */
13425 if (interrupt_input)
13426 unrequest_sigio ();
13427 STOP_POLLING;
13428
13429 /* Update the display. */
13430 set_window_update_flags (XWINDOW (f->root_window), 1);
13431 pending |= update_frame (f, 0, 0);
13432 f->updated_p = 1;
13433 }
13434 }
13435 }
13436
13437 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13438
13439 if (!pending)
13440 {
13441 /* Do the mark_window_display_accurate after all windows have
13442 been redisplayed because this call resets flags in buffers
13443 which are needed for proper redisplay. */
13444 FOR_EACH_FRAME (tail, frame)
13445 {
13446 struct frame *f = XFRAME (frame);
13447 if (f->updated_p)
13448 {
13449 mark_window_display_accurate (f->root_window, 1);
13450 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13451 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13452 }
13453 }
13454 }
13455 }
13456 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13457 {
13458 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13459 struct frame *mini_frame;
13460
13461 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13462 /* Use list_of_error, not Qerror, so that
13463 we catch only errors and don't run the debugger. */
13464 internal_condition_case_1 (redisplay_window_1, selected_window,
13465 list_of_error,
13466 redisplay_window_error);
13467 if (update_miniwindow_p)
13468 internal_condition_case_1 (redisplay_window_1, mini_window,
13469 list_of_error,
13470 redisplay_window_error);
13471
13472 /* Compare desired and current matrices, perform output. */
13473
13474 update:
13475 /* If fonts changed, display again. */
13476 if (fonts_changed_p)
13477 goto retry;
13478
13479 /* Prevent various kinds of signals during display update.
13480 stdio is not robust about handling signals,
13481 which can cause an apparent I/O error. */
13482 if (interrupt_input)
13483 unrequest_sigio ();
13484 STOP_POLLING;
13485
13486 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13487 {
13488 if (hscroll_windows (selected_window))
13489 goto retry;
13490
13491 XWINDOW (selected_window)->must_be_updated_p = 1;
13492 pending = update_frame (sf, 0, 0);
13493 }
13494
13495 /* We may have called echo_area_display at the top of this
13496 function. If the echo area is on another frame, that may
13497 have put text on a frame other than the selected one, so the
13498 above call to update_frame would not have caught it. Catch
13499 it here. */
13500 mini_window = FRAME_MINIBUF_WINDOW (sf);
13501 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13502
13503 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13504 {
13505 XWINDOW (mini_window)->must_be_updated_p = 1;
13506 pending |= update_frame (mini_frame, 0, 0);
13507 if (!pending && hscroll_windows (mini_window))
13508 goto retry;
13509 }
13510 }
13511
13512 /* If display was paused because of pending input, make sure we do a
13513 thorough update the next time. */
13514 if (pending)
13515 {
13516 /* Prevent the optimization at the beginning of
13517 redisplay_internal that tries a single-line update of the
13518 line containing the cursor in the selected window. */
13519 CHARPOS (this_line_start_pos) = 0;
13520
13521 /* Let the overlay arrow be updated the next time. */
13522 update_overlay_arrows (0);
13523
13524 /* If we pause after scrolling, some rows in the current
13525 matrices of some windows are not valid. */
13526 if (!WINDOW_FULL_WIDTH_P (w)
13527 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13528 update_mode_lines = 1;
13529 }
13530 else
13531 {
13532 if (!consider_all_windows_p)
13533 {
13534 /* This has already been done above if
13535 consider_all_windows_p is set. */
13536 mark_window_display_accurate_1 (w, 1);
13537
13538 /* Say overlay arrows are up to date. */
13539 update_overlay_arrows (1);
13540
13541 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13542 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13543 }
13544
13545 update_mode_lines = 0;
13546 windows_or_buffers_changed = 0;
13547 cursor_type_changed = 0;
13548 }
13549
13550 /* Start SIGIO interrupts coming again. Having them off during the
13551 code above makes it less likely one will discard output, but not
13552 impossible, since there might be stuff in the system buffer here.
13553 But it is much hairier to try to do anything about that. */
13554 if (interrupt_input)
13555 request_sigio ();
13556 RESUME_POLLING;
13557
13558 /* If a frame has become visible which was not before, redisplay
13559 again, so that we display it. Expose events for such a frame
13560 (which it gets when becoming visible) don't call the parts of
13561 redisplay constructing glyphs, so simply exposing a frame won't
13562 display anything in this case. So, we have to display these
13563 frames here explicitly. */
13564 if (!pending)
13565 {
13566 int new_count = 0;
13567
13568 FOR_EACH_FRAME (tail, frame)
13569 {
13570 int this_is_visible = 0;
13571
13572 if (XFRAME (frame)->visible)
13573 this_is_visible = 1;
13574
13575 if (this_is_visible)
13576 new_count++;
13577 }
13578
13579 if (new_count != number_of_visible_frames)
13580 windows_or_buffers_changed++;
13581 }
13582
13583 /* Change frame size now if a change is pending. */
13584 do_pending_window_change (1);
13585
13586 /* If we just did a pending size change, or have additional
13587 visible frames, or selected_window changed, redisplay again. */
13588 if ((windows_or_buffers_changed && !pending)
13589 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13590 goto retry;
13591
13592 /* Clear the face and image caches.
13593
13594 We used to do this only if consider_all_windows_p. But the cache
13595 needs to be cleared if a timer creates images in the current
13596 buffer (e.g. the test case in Bug#6230). */
13597
13598 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13599 {
13600 clear_face_cache (0);
13601 clear_face_cache_count = 0;
13602 }
13603
13604 #ifdef HAVE_WINDOW_SYSTEM
13605 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13606 {
13607 clear_image_caches (Qnil);
13608 clear_image_cache_count = 0;
13609 }
13610 #endif /* HAVE_WINDOW_SYSTEM */
13611
13612 end_of_redisplay:
13613 unbind_to (count, Qnil);
13614 RESUME_POLLING;
13615 }
13616
13617
13618 /* Redisplay, but leave alone any recent echo area message unless
13619 another message has been requested in its place.
13620
13621 This is useful in situations where you need to redisplay but no
13622 user action has occurred, making it inappropriate for the message
13623 area to be cleared. See tracking_off and
13624 wait_reading_process_output for examples of these situations.
13625
13626 FROM_WHERE is an integer saying from where this function was
13627 called. This is useful for debugging. */
13628
13629 void
13630 redisplay_preserve_echo_area (int from_where)
13631 {
13632 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13633
13634 if (!NILP (echo_area_buffer[1]))
13635 {
13636 /* We have a previously displayed message, but no current
13637 message. Redisplay the previous message. */
13638 display_last_displayed_message_p = 1;
13639 redisplay_internal ();
13640 display_last_displayed_message_p = 0;
13641 }
13642 else
13643 redisplay_internal ();
13644
13645 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13646 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13647 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13648 }
13649
13650
13651 /* Function registered with record_unwind_protect in redisplay_internal. */
13652
13653 static void
13654 unwind_redisplay (void)
13655 {
13656 redisplaying_p = 0;
13657 }
13658
13659
13660 /* Mark the display of leaf window W as accurate or inaccurate.
13661 If ACCURATE_P is non-zero mark display of W as accurate. If
13662 ACCURATE_P is zero, arrange for W to be redisplayed the next
13663 time redisplay_internal is called. */
13664
13665 static void
13666 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13667 {
13668 struct buffer *b = XBUFFER (w->contents);
13669
13670 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13671 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13672 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13673
13674 if (accurate_p)
13675 {
13676 b->clip_changed = 0;
13677 b->prevent_redisplay_optimizations_p = 0;
13678
13679 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13680 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13681 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13682 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13683
13684 w->current_matrix->buffer = b;
13685 w->current_matrix->begv = BUF_BEGV (b);
13686 w->current_matrix->zv = BUF_ZV (b);
13687
13688 w->last_cursor = w->cursor;
13689 w->last_cursor_off_p = w->cursor_off_p;
13690
13691 if (w == XWINDOW (selected_window))
13692 w->last_point = BUF_PT (b);
13693 else
13694 w->last_point = marker_position (w->pointm);
13695
13696 w->window_end_valid = 1;
13697 w->update_mode_line = 0;
13698 }
13699 }
13700
13701
13702 /* Mark the display of windows in the window tree rooted at WINDOW as
13703 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13704 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13705 be redisplayed the next time redisplay_internal is called. */
13706
13707 void
13708 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13709 {
13710 struct window *w;
13711
13712 for (; !NILP (window); window = w->next)
13713 {
13714 w = XWINDOW (window);
13715 if (WINDOWP (w->contents))
13716 mark_window_display_accurate (w->contents, accurate_p);
13717 else
13718 mark_window_display_accurate_1 (w, accurate_p);
13719 }
13720
13721 if (accurate_p)
13722 update_overlay_arrows (1);
13723 else
13724 /* Force a thorough redisplay the next time by setting
13725 last_arrow_position and last_arrow_string to t, which is
13726 unequal to any useful value of Voverlay_arrow_... */
13727 update_overlay_arrows (-1);
13728 }
13729
13730
13731 /* Return value in display table DP (Lisp_Char_Table *) for character
13732 C. Since a display table doesn't have any parent, we don't have to
13733 follow parent. Do not call this function directly but use the
13734 macro DISP_CHAR_VECTOR. */
13735
13736 Lisp_Object
13737 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13738 {
13739 Lisp_Object val;
13740
13741 if (ASCII_CHAR_P (c))
13742 {
13743 val = dp->ascii;
13744 if (SUB_CHAR_TABLE_P (val))
13745 val = XSUB_CHAR_TABLE (val)->contents[c];
13746 }
13747 else
13748 {
13749 Lisp_Object table;
13750
13751 XSETCHAR_TABLE (table, dp);
13752 val = char_table_ref (table, c);
13753 }
13754 if (NILP (val))
13755 val = dp->defalt;
13756 return val;
13757 }
13758
13759
13760 \f
13761 /***********************************************************************
13762 Window Redisplay
13763 ***********************************************************************/
13764
13765 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13766
13767 static void
13768 redisplay_windows (Lisp_Object window)
13769 {
13770 while (!NILP (window))
13771 {
13772 struct window *w = XWINDOW (window);
13773
13774 if (WINDOWP (w->contents))
13775 redisplay_windows (w->contents);
13776 else if (BUFFERP (w->contents))
13777 {
13778 displayed_buffer = XBUFFER (w->contents);
13779 /* Use list_of_error, not Qerror, so that
13780 we catch only errors and don't run the debugger. */
13781 internal_condition_case_1 (redisplay_window_0, window,
13782 list_of_error,
13783 redisplay_window_error);
13784 }
13785
13786 window = w->next;
13787 }
13788 }
13789
13790 static Lisp_Object
13791 redisplay_window_error (Lisp_Object ignore)
13792 {
13793 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13794 return Qnil;
13795 }
13796
13797 static Lisp_Object
13798 redisplay_window_0 (Lisp_Object window)
13799 {
13800 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13801 redisplay_window (window, 0);
13802 return Qnil;
13803 }
13804
13805 static Lisp_Object
13806 redisplay_window_1 (Lisp_Object window)
13807 {
13808 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13809 redisplay_window (window, 1);
13810 return Qnil;
13811 }
13812 \f
13813
13814 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13815 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13816 which positions recorded in ROW differ from current buffer
13817 positions.
13818
13819 Return 0 if cursor is not on this row, 1 otherwise. */
13820
13821 static int
13822 set_cursor_from_row (struct window *w, struct glyph_row *row,
13823 struct glyph_matrix *matrix,
13824 ptrdiff_t delta, ptrdiff_t delta_bytes,
13825 int dy, int dvpos)
13826 {
13827 struct glyph *glyph = row->glyphs[TEXT_AREA];
13828 struct glyph *end = glyph + row->used[TEXT_AREA];
13829 struct glyph *cursor = NULL;
13830 /* The last known character position in row. */
13831 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13832 int x = row->x;
13833 ptrdiff_t pt_old = PT - delta;
13834 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13835 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13836 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13837 /* A glyph beyond the edge of TEXT_AREA which we should never
13838 touch. */
13839 struct glyph *glyphs_end = end;
13840 /* Non-zero means we've found a match for cursor position, but that
13841 glyph has the avoid_cursor_p flag set. */
13842 int match_with_avoid_cursor = 0;
13843 /* Non-zero means we've seen at least one glyph that came from a
13844 display string. */
13845 int string_seen = 0;
13846 /* Largest and smallest buffer positions seen so far during scan of
13847 glyph row. */
13848 ptrdiff_t bpos_max = pos_before;
13849 ptrdiff_t bpos_min = pos_after;
13850 /* Last buffer position covered by an overlay string with an integer
13851 `cursor' property. */
13852 ptrdiff_t bpos_covered = 0;
13853 /* Non-zero means the display string on which to display the cursor
13854 comes from a text property, not from an overlay. */
13855 int string_from_text_prop = 0;
13856
13857 /* Don't even try doing anything if called for a mode-line or
13858 header-line row, since the rest of the code isn't prepared to
13859 deal with such calamities. */
13860 eassert (!row->mode_line_p);
13861 if (row->mode_line_p)
13862 return 0;
13863
13864 /* Skip over glyphs not having an object at the start and the end of
13865 the row. These are special glyphs like truncation marks on
13866 terminal frames. */
13867 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13868 {
13869 if (!row->reversed_p)
13870 {
13871 while (glyph < end
13872 && INTEGERP (glyph->object)
13873 && glyph->charpos < 0)
13874 {
13875 x += glyph->pixel_width;
13876 ++glyph;
13877 }
13878 while (end > glyph
13879 && INTEGERP ((end - 1)->object)
13880 /* CHARPOS is zero for blanks and stretch glyphs
13881 inserted by extend_face_to_end_of_line. */
13882 && (end - 1)->charpos <= 0)
13883 --end;
13884 glyph_before = glyph - 1;
13885 glyph_after = end;
13886 }
13887 else
13888 {
13889 struct glyph *g;
13890
13891 /* If the glyph row is reversed, we need to process it from back
13892 to front, so swap the edge pointers. */
13893 glyphs_end = end = glyph - 1;
13894 glyph += row->used[TEXT_AREA] - 1;
13895
13896 while (glyph > end + 1
13897 && INTEGERP (glyph->object)
13898 && glyph->charpos < 0)
13899 {
13900 --glyph;
13901 x -= glyph->pixel_width;
13902 }
13903 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13904 --glyph;
13905 /* By default, in reversed rows we put the cursor on the
13906 rightmost (first in the reading order) glyph. */
13907 for (g = end + 1; g < glyph; g++)
13908 x += g->pixel_width;
13909 while (end < glyph
13910 && INTEGERP ((end + 1)->object)
13911 && (end + 1)->charpos <= 0)
13912 ++end;
13913 glyph_before = glyph + 1;
13914 glyph_after = end;
13915 }
13916 }
13917 else if (row->reversed_p)
13918 {
13919 /* In R2L rows that don't display text, put the cursor on the
13920 rightmost glyph. Case in point: an empty last line that is
13921 part of an R2L paragraph. */
13922 cursor = end - 1;
13923 /* Avoid placing the cursor on the last glyph of the row, where
13924 on terminal frames we hold the vertical border between
13925 adjacent windows. */
13926 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13927 && !WINDOW_RIGHTMOST_P (w)
13928 && cursor == row->glyphs[LAST_AREA] - 1)
13929 cursor--;
13930 x = -1; /* will be computed below, at label compute_x */
13931 }
13932
13933 /* Step 1: Try to find the glyph whose character position
13934 corresponds to point. If that's not possible, find 2 glyphs
13935 whose character positions are the closest to point, one before
13936 point, the other after it. */
13937 if (!row->reversed_p)
13938 while (/* not marched to end of glyph row */
13939 glyph < end
13940 /* glyph was not inserted by redisplay for internal purposes */
13941 && !INTEGERP (glyph->object))
13942 {
13943 if (BUFFERP (glyph->object))
13944 {
13945 ptrdiff_t dpos = glyph->charpos - pt_old;
13946
13947 if (glyph->charpos > bpos_max)
13948 bpos_max = glyph->charpos;
13949 if (glyph->charpos < bpos_min)
13950 bpos_min = glyph->charpos;
13951 if (!glyph->avoid_cursor_p)
13952 {
13953 /* If we hit point, we've found the glyph on which to
13954 display the cursor. */
13955 if (dpos == 0)
13956 {
13957 match_with_avoid_cursor = 0;
13958 break;
13959 }
13960 /* See if we've found a better approximation to
13961 POS_BEFORE or to POS_AFTER. */
13962 if (0 > dpos && dpos > pos_before - pt_old)
13963 {
13964 pos_before = glyph->charpos;
13965 glyph_before = glyph;
13966 }
13967 else if (0 < dpos && dpos < pos_after - pt_old)
13968 {
13969 pos_after = glyph->charpos;
13970 glyph_after = glyph;
13971 }
13972 }
13973 else if (dpos == 0)
13974 match_with_avoid_cursor = 1;
13975 }
13976 else if (STRINGP (glyph->object))
13977 {
13978 Lisp_Object chprop;
13979 ptrdiff_t glyph_pos = glyph->charpos;
13980
13981 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13982 glyph->object);
13983 if (!NILP (chprop))
13984 {
13985 /* If the string came from a `display' text property,
13986 look up the buffer position of that property and
13987 use that position to update bpos_max, as if we
13988 actually saw such a position in one of the row's
13989 glyphs. This helps with supporting integer values
13990 of `cursor' property on the display string in
13991 situations where most or all of the row's buffer
13992 text is completely covered by display properties,
13993 so that no glyph with valid buffer positions is
13994 ever seen in the row. */
13995 ptrdiff_t prop_pos =
13996 string_buffer_position_lim (glyph->object, pos_before,
13997 pos_after, 0);
13998
13999 if (prop_pos >= pos_before)
14000 bpos_max = prop_pos - 1;
14001 }
14002 if (INTEGERP (chprop))
14003 {
14004 bpos_covered = bpos_max + XINT (chprop);
14005 /* If the `cursor' property covers buffer positions up
14006 to and including point, we should display cursor on
14007 this glyph. Note that, if a `cursor' property on one
14008 of the string's characters has an integer value, we
14009 will break out of the loop below _before_ we get to
14010 the position match above. IOW, integer values of
14011 the `cursor' property override the "exact match for
14012 point" strategy of positioning the cursor. */
14013 /* Implementation note: bpos_max == pt_old when, e.g.,
14014 we are in an empty line, where bpos_max is set to
14015 MATRIX_ROW_START_CHARPOS, see above. */
14016 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14017 {
14018 cursor = glyph;
14019 break;
14020 }
14021 }
14022
14023 string_seen = 1;
14024 }
14025 x += glyph->pixel_width;
14026 ++glyph;
14027 }
14028 else if (glyph > end) /* row is reversed */
14029 while (!INTEGERP (glyph->object))
14030 {
14031 if (BUFFERP (glyph->object))
14032 {
14033 ptrdiff_t dpos = glyph->charpos - pt_old;
14034
14035 if (glyph->charpos > bpos_max)
14036 bpos_max = glyph->charpos;
14037 if (glyph->charpos < bpos_min)
14038 bpos_min = glyph->charpos;
14039 if (!glyph->avoid_cursor_p)
14040 {
14041 if (dpos == 0)
14042 {
14043 match_with_avoid_cursor = 0;
14044 break;
14045 }
14046 if (0 > dpos && dpos > pos_before - pt_old)
14047 {
14048 pos_before = glyph->charpos;
14049 glyph_before = glyph;
14050 }
14051 else if (0 < dpos && dpos < pos_after - pt_old)
14052 {
14053 pos_after = glyph->charpos;
14054 glyph_after = glyph;
14055 }
14056 }
14057 else if (dpos == 0)
14058 match_with_avoid_cursor = 1;
14059 }
14060 else if (STRINGP (glyph->object))
14061 {
14062 Lisp_Object chprop;
14063 ptrdiff_t glyph_pos = glyph->charpos;
14064
14065 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14066 glyph->object);
14067 if (!NILP (chprop))
14068 {
14069 ptrdiff_t prop_pos =
14070 string_buffer_position_lim (glyph->object, pos_before,
14071 pos_after, 0);
14072
14073 if (prop_pos >= pos_before)
14074 bpos_max = prop_pos - 1;
14075 }
14076 if (INTEGERP (chprop))
14077 {
14078 bpos_covered = bpos_max + XINT (chprop);
14079 /* If the `cursor' property covers buffer positions up
14080 to and including point, we should display cursor on
14081 this glyph. */
14082 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14083 {
14084 cursor = glyph;
14085 break;
14086 }
14087 }
14088 string_seen = 1;
14089 }
14090 --glyph;
14091 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14092 {
14093 x--; /* can't use any pixel_width */
14094 break;
14095 }
14096 x -= glyph->pixel_width;
14097 }
14098
14099 /* Step 2: If we didn't find an exact match for point, we need to
14100 look for a proper place to put the cursor among glyphs between
14101 GLYPH_BEFORE and GLYPH_AFTER. */
14102 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14103 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14104 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14105 {
14106 /* An empty line has a single glyph whose OBJECT is zero and
14107 whose CHARPOS is the position of a newline on that line.
14108 Note that on a TTY, there are more glyphs after that, which
14109 were produced by extend_face_to_end_of_line, but their
14110 CHARPOS is zero or negative. */
14111 int empty_line_p =
14112 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14113 && INTEGERP (glyph->object) && glyph->charpos > 0
14114 /* On a TTY, continued and truncated rows also have a glyph at
14115 their end whose OBJECT is zero and whose CHARPOS is
14116 positive (the continuation and truncation glyphs), but such
14117 rows are obviously not "empty". */
14118 && !(row->continued_p || row->truncated_on_right_p);
14119
14120 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14121 {
14122 ptrdiff_t ellipsis_pos;
14123
14124 /* Scan back over the ellipsis glyphs. */
14125 if (!row->reversed_p)
14126 {
14127 ellipsis_pos = (glyph - 1)->charpos;
14128 while (glyph > row->glyphs[TEXT_AREA]
14129 && (glyph - 1)->charpos == ellipsis_pos)
14130 glyph--, x -= glyph->pixel_width;
14131 /* That loop always goes one position too far, including
14132 the glyph before the ellipsis. So scan forward over
14133 that one. */
14134 x += glyph->pixel_width;
14135 glyph++;
14136 }
14137 else /* row is reversed */
14138 {
14139 ellipsis_pos = (glyph + 1)->charpos;
14140 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14141 && (glyph + 1)->charpos == ellipsis_pos)
14142 glyph++, x += glyph->pixel_width;
14143 x -= glyph->pixel_width;
14144 glyph--;
14145 }
14146 }
14147 else if (match_with_avoid_cursor)
14148 {
14149 cursor = glyph_after;
14150 x = -1;
14151 }
14152 else if (string_seen)
14153 {
14154 int incr = row->reversed_p ? -1 : +1;
14155
14156 /* Need to find the glyph that came out of a string which is
14157 present at point. That glyph is somewhere between
14158 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14159 positioned between POS_BEFORE and POS_AFTER in the
14160 buffer. */
14161 struct glyph *start, *stop;
14162 ptrdiff_t pos = pos_before;
14163
14164 x = -1;
14165
14166 /* If the row ends in a newline from a display string,
14167 reordering could have moved the glyphs belonging to the
14168 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14169 in this case we extend the search to the last glyph in
14170 the row that was not inserted by redisplay. */
14171 if (row->ends_in_newline_from_string_p)
14172 {
14173 glyph_after = end;
14174 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14175 }
14176
14177 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14178 correspond to POS_BEFORE and POS_AFTER, respectively. We
14179 need START and STOP in the order that corresponds to the
14180 row's direction as given by its reversed_p flag. If the
14181 directionality of characters between POS_BEFORE and
14182 POS_AFTER is the opposite of the row's base direction,
14183 these characters will have been reordered for display,
14184 and we need to reverse START and STOP. */
14185 if (!row->reversed_p)
14186 {
14187 start = min (glyph_before, glyph_after);
14188 stop = max (glyph_before, glyph_after);
14189 }
14190 else
14191 {
14192 start = max (glyph_before, glyph_after);
14193 stop = min (glyph_before, glyph_after);
14194 }
14195 for (glyph = start + incr;
14196 row->reversed_p ? glyph > stop : glyph < stop; )
14197 {
14198
14199 /* Any glyphs that come from the buffer are here because
14200 of bidi reordering. Skip them, and only pay
14201 attention to glyphs that came from some string. */
14202 if (STRINGP (glyph->object))
14203 {
14204 Lisp_Object str;
14205 ptrdiff_t tem;
14206 /* If the display property covers the newline, we
14207 need to search for it one position farther. */
14208 ptrdiff_t lim = pos_after
14209 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14210
14211 string_from_text_prop = 0;
14212 str = glyph->object;
14213 tem = string_buffer_position_lim (str, pos, lim, 0);
14214 if (tem == 0 /* from overlay */
14215 || pos <= tem)
14216 {
14217 /* If the string from which this glyph came is
14218 found in the buffer at point, or at position
14219 that is closer to point than pos_after, then
14220 we've found the glyph we've been looking for.
14221 If it comes from an overlay (tem == 0), and
14222 it has the `cursor' property on one of its
14223 glyphs, record that glyph as a candidate for
14224 displaying the cursor. (As in the
14225 unidirectional version, we will display the
14226 cursor on the last candidate we find.) */
14227 if (tem == 0
14228 || tem == pt_old
14229 || (tem - pt_old > 0 && tem < pos_after))
14230 {
14231 /* The glyphs from this string could have
14232 been reordered. Find the one with the
14233 smallest string position. Or there could
14234 be a character in the string with the
14235 `cursor' property, which means display
14236 cursor on that character's glyph. */
14237 ptrdiff_t strpos = glyph->charpos;
14238
14239 if (tem)
14240 {
14241 cursor = glyph;
14242 string_from_text_prop = 1;
14243 }
14244 for ( ;
14245 (row->reversed_p ? glyph > stop : glyph < stop)
14246 && EQ (glyph->object, str);
14247 glyph += incr)
14248 {
14249 Lisp_Object cprop;
14250 ptrdiff_t gpos = glyph->charpos;
14251
14252 cprop = Fget_char_property (make_number (gpos),
14253 Qcursor,
14254 glyph->object);
14255 if (!NILP (cprop))
14256 {
14257 cursor = glyph;
14258 break;
14259 }
14260 if (tem && glyph->charpos < strpos)
14261 {
14262 strpos = glyph->charpos;
14263 cursor = glyph;
14264 }
14265 }
14266
14267 if (tem == pt_old
14268 || (tem - pt_old > 0 && tem < pos_after))
14269 goto compute_x;
14270 }
14271 if (tem)
14272 pos = tem + 1; /* don't find previous instances */
14273 }
14274 /* This string is not what we want; skip all of the
14275 glyphs that came from it. */
14276 while ((row->reversed_p ? glyph > stop : glyph < stop)
14277 && EQ (glyph->object, str))
14278 glyph += incr;
14279 }
14280 else
14281 glyph += incr;
14282 }
14283
14284 /* If we reached the end of the line, and END was from a string,
14285 the cursor is not on this line. */
14286 if (cursor == NULL
14287 && (row->reversed_p ? glyph <= end : glyph >= end)
14288 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14289 && STRINGP (end->object)
14290 && row->continued_p)
14291 return 0;
14292 }
14293 /* A truncated row may not include PT among its character positions.
14294 Setting the cursor inside the scroll margin will trigger
14295 recalculation of hscroll in hscroll_window_tree. But if a
14296 display string covers point, defer to the string-handling
14297 code below to figure this out. */
14298 else if (row->truncated_on_left_p && pt_old < bpos_min)
14299 {
14300 cursor = glyph_before;
14301 x = -1;
14302 }
14303 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14304 /* Zero-width characters produce no glyphs. */
14305 || (!empty_line_p
14306 && (row->reversed_p
14307 ? glyph_after > glyphs_end
14308 : glyph_after < glyphs_end)))
14309 {
14310 cursor = glyph_after;
14311 x = -1;
14312 }
14313 }
14314
14315 compute_x:
14316 if (cursor != NULL)
14317 glyph = cursor;
14318 else if (glyph == glyphs_end
14319 && pos_before == pos_after
14320 && STRINGP ((row->reversed_p
14321 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14322 : row->glyphs[TEXT_AREA])->object))
14323 {
14324 /* If all the glyphs of this row came from strings, put the
14325 cursor on the first glyph of the row. This avoids having the
14326 cursor outside of the text area in this very rare and hard
14327 use case. */
14328 glyph =
14329 row->reversed_p
14330 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14331 : row->glyphs[TEXT_AREA];
14332 }
14333 if (x < 0)
14334 {
14335 struct glyph *g;
14336
14337 /* Need to compute x that corresponds to GLYPH. */
14338 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14339 {
14340 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14341 emacs_abort ();
14342 x += g->pixel_width;
14343 }
14344 }
14345
14346 /* ROW could be part of a continued line, which, under bidi
14347 reordering, might have other rows whose start and end charpos
14348 occlude point. Only set w->cursor if we found a better
14349 approximation to the cursor position than we have from previously
14350 examined candidate rows belonging to the same continued line. */
14351 if (/* we already have a candidate row */
14352 w->cursor.vpos >= 0
14353 /* that candidate is not the row we are processing */
14354 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14355 /* Make sure cursor.vpos specifies a row whose start and end
14356 charpos occlude point, and it is valid candidate for being a
14357 cursor-row. This is because some callers of this function
14358 leave cursor.vpos at the row where the cursor was displayed
14359 during the last redisplay cycle. */
14360 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14361 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14362 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14363 {
14364 struct glyph *g1 =
14365 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14366
14367 /* Don't consider glyphs that are outside TEXT_AREA. */
14368 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14369 return 0;
14370 /* Keep the candidate whose buffer position is the closest to
14371 point or has the `cursor' property. */
14372 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14373 w->cursor.hpos >= 0
14374 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14375 && ((BUFFERP (g1->object)
14376 && (g1->charpos == pt_old /* an exact match always wins */
14377 || (BUFFERP (glyph->object)
14378 && eabs (g1->charpos - pt_old)
14379 < eabs (glyph->charpos - pt_old))))
14380 /* previous candidate is a glyph from a string that has
14381 a non-nil `cursor' property */
14382 || (STRINGP (g1->object)
14383 && (!NILP (Fget_char_property (make_number (g1->charpos),
14384 Qcursor, g1->object))
14385 /* previous candidate is from the same display
14386 string as this one, and the display string
14387 came from a text property */
14388 || (EQ (g1->object, glyph->object)
14389 && string_from_text_prop)
14390 /* this candidate is from newline and its
14391 position is not an exact match */
14392 || (INTEGERP (glyph->object)
14393 && glyph->charpos != pt_old)))))
14394 return 0;
14395 /* If this candidate gives an exact match, use that. */
14396 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14397 /* If this candidate is a glyph created for the
14398 terminating newline of a line, and point is on that
14399 newline, it wins because it's an exact match. */
14400 || (!row->continued_p
14401 && INTEGERP (glyph->object)
14402 && glyph->charpos == 0
14403 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14404 /* Otherwise, keep the candidate that comes from a row
14405 spanning less buffer positions. This may win when one or
14406 both candidate positions are on glyphs that came from
14407 display strings, for which we cannot compare buffer
14408 positions. */
14409 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14410 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14411 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14412 return 0;
14413 }
14414 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14415 w->cursor.x = x;
14416 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14417 w->cursor.y = row->y + dy;
14418
14419 if (w == XWINDOW (selected_window))
14420 {
14421 if (!row->continued_p
14422 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14423 && row->x == 0)
14424 {
14425 this_line_buffer = XBUFFER (w->contents);
14426
14427 CHARPOS (this_line_start_pos)
14428 = MATRIX_ROW_START_CHARPOS (row) + delta;
14429 BYTEPOS (this_line_start_pos)
14430 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14431
14432 CHARPOS (this_line_end_pos)
14433 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14434 BYTEPOS (this_line_end_pos)
14435 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14436
14437 this_line_y = w->cursor.y;
14438 this_line_pixel_height = row->height;
14439 this_line_vpos = w->cursor.vpos;
14440 this_line_start_x = row->x;
14441 }
14442 else
14443 CHARPOS (this_line_start_pos) = 0;
14444 }
14445
14446 return 1;
14447 }
14448
14449
14450 /* Run window scroll functions, if any, for WINDOW with new window
14451 start STARTP. Sets the window start of WINDOW to that position.
14452
14453 We assume that the window's buffer is really current. */
14454
14455 static struct text_pos
14456 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14457 {
14458 struct window *w = XWINDOW (window);
14459 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14460
14461 if (current_buffer != XBUFFER (w->contents))
14462 emacs_abort ();
14463
14464 if (!NILP (Vwindow_scroll_functions))
14465 {
14466 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14467 make_number (CHARPOS (startp)));
14468 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14469 /* In case the hook functions switch buffers. */
14470 set_buffer_internal (XBUFFER (w->contents));
14471 }
14472
14473 return startp;
14474 }
14475
14476
14477 /* Make sure the line containing the cursor is fully visible.
14478 A value of 1 means there is nothing to be done.
14479 (Either the line is fully visible, or it cannot be made so,
14480 or we cannot tell.)
14481
14482 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14483 is higher than window.
14484
14485 A value of 0 means the caller should do scrolling
14486 as if point had gone off the screen. */
14487
14488 static int
14489 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14490 {
14491 struct glyph_matrix *matrix;
14492 struct glyph_row *row;
14493 int window_height;
14494
14495 if (!make_cursor_line_fully_visible_p)
14496 return 1;
14497
14498 /* It's not always possible to find the cursor, e.g, when a window
14499 is full of overlay strings. Don't do anything in that case. */
14500 if (w->cursor.vpos < 0)
14501 return 1;
14502
14503 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14504 row = MATRIX_ROW (matrix, w->cursor.vpos);
14505
14506 /* If the cursor row is not partially visible, there's nothing to do. */
14507 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14508 return 1;
14509
14510 /* If the row the cursor is in is taller than the window's height,
14511 it's not clear what to do, so do nothing. */
14512 window_height = window_box_height (w);
14513 if (row->height >= window_height)
14514 {
14515 if (!force_p || MINI_WINDOW_P (w)
14516 || w->vscroll || w->cursor.vpos == 0)
14517 return 1;
14518 }
14519 return 0;
14520 }
14521
14522
14523 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14524 non-zero means only WINDOW is redisplayed in redisplay_internal.
14525 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14526 in redisplay_window to bring a partially visible line into view in
14527 the case that only the cursor has moved.
14528
14529 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14530 last screen line's vertical height extends past the end of the screen.
14531
14532 Value is
14533
14534 1 if scrolling succeeded
14535
14536 0 if scrolling didn't find point.
14537
14538 -1 if new fonts have been loaded so that we must interrupt
14539 redisplay, adjust glyph matrices, and try again. */
14540
14541 enum
14542 {
14543 SCROLLING_SUCCESS,
14544 SCROLLING_FAILED,
14545 SCROLLING_NEED_LARGER_MATRICES
14546 };
14547
14548 /* If scroll-conservatively is more than this, never recenter.
14549
14550 If you change this, don't forget to update the doc string of
14551 `scroll-conservatively' and the Emacs manual. */
14552 #define SCROLL_LIMIT 100
14553
14554 static int
14555 try_scrolling (Lisp_Object window, int just_this_one_p,
14556 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14557 int temp_scroll_step, int last_line_misfit)
14558 {
14559 struct window *w = XWINDOW (window);
14560 struct frame *f = XFRAME (w->frame);
14561 struct text_pos pos, startp;
14562 struct it it;
14563 int this_scroll_margin, scroll_max, rc, height;
14564 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14565 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14566 Lisp_Object aggressive;
14567 /* We will never try scrolling more than this number of lines. */
14568 int scroll_limit = SCROLL_LIMIT;
14569 int frame_line_height = default_line_pixel_height (w);
14570 int window_total_lines
14571 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14572
14573 #ifdef GLYPH_DEBUG
14574 debug_method_add (w, "try_scrolling");
14575 #endif
14576
14577 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14578
14579 /* Compute scroll margin height in pixels. We scroll when point is
14580 within this distance from the top or bottom of the window. */
14581 if (scroll_margin > 0)
14582 this_scroll_margin = min (scroll_margin, window_total_lines / 4)
14583 * frame_line_height;
14584 else
14585 this_scroll_margin = 0;
14586
14587 /* Force arg_scroll_conservatively to have a reasonable value, to
14588 avoid scrolling too far away with slow move_it_* functions. Note
14589 that the user can supply scroll-conservatively equal to
14590 `most-positive-fixnum', which can be larger than INT_MAX. */
14591 if (arg_scroll_conservatively > scroll_limit)
14592 {
14593 arg_scroll_conservatively = scroll_limit + 1;
14594 scroll_max = scroll_limit * frame_line_height;
14595 }
14596 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14597 /* Compute how much we should try to scroll maximally to bring
14598 point into view. */
14599 scroll_max = (max (scroll_step,
14600 max (arg_scroll_conservatively, temp_scroll_step))
14601 * frame_line_height);
14602 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14603 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14604 /* We're trying to scroll because of aggressive scrolling but no
14605 scroll_step is set. Choose an arbitrary one. */
14606 scroll_max = 10 * frame_line_height;
14607 else
14608 scroll_max = 0;
14609
14610 too_near_end:
14611
14612 /* Decide whether to scroll down. */
14613 if (PT > CHARPOS (startp))
14614 {
14615 int scroll_margin_y;
14616
14617 /* Compute the pixel ypos of the scroll margin, then move IT to
14618 either that ypos or PT, whichever comes first. */
14619 start_display (&it, w, startp);
14620 scroll_margin_y = it.last_visible_y - this_scroll_margin
14621 - frame_line_height * extra_scroll_margin_lines;
14622 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14623 (MOVE_TO_POS | MOVE_TO_Y));
14624
14625 if (PT > CHARPOS (it.current.pos))
14626 {
14627 int y0 = line_bottom_y (&it);
14628 /* Compute how many pixels below window bottom to stop searching
14629 for PT. This avoids costly search for PT that is far away if
14630 the user limited scrolling by a small number of lines, but
14631 always finds PT if scroll_conservatively is set to a large
14632 number, such as most-positive-fixnum. */
14633 int slack = max (scroll_max, 10 * frame_line_height);
14634 int y_to_move = it.last_visible_y + slack;
14635
14636 /* Compute the distance from the scroll margin to PT or to
14637 the scroll limit, whichever comes first. This should
14638 include the height of the cursor line, to make that line
14639 fully visible. */
14640 move_it_to (&it, PT, -1, y_to_move,
14641 -1, MOVE_TO_POS | MOVE_TO_Y);
14642 dy = line_bottom_y (&it) - y0;
14643
14644 if (dy > scroll_max)
14645 return SCROLLING_FAILED;
14646
14647 if (dy > 0)
14648 scroll_down_p = 1;
14649 }
14650 }
14651
14652 if (scroll_down_p)
14653 {
14654 /* Point is in or below the bottom scroll margin, so move the
14655 window start down. If scrolling conservatively, move it just
14656 enough down to make point visible. If scroll_step is set,
14657 move it down by scroll_step. */
14658 if (arg_scroll_conservatively)
14659 amount_to_scroll
14660 = min (max (dy, frame_line_height),
14661 frame_line_height * arg_scroll_conservatively);
14662 else if (scroll_step || temp_scroll_step)
14663 amount_to_scroll = scroll_max;
14664 else
14665 {
14666 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14667 height = WINDOW_BOX_TEXT_HEIGHT (w);
14668 if (NUMBERP (aggressive))
14669 {
14670 double float_amount = XFLOATINT (aggressive) * height;
14671 int aggressive_scroll = float_amount;
14672 if (aggressive_scroll == 0 && float_amount > 0)
14673 aggressive_scroll = 1;
14674 /* Don't let point enter the scroll margin near top of
14675 the window. This could happen if the value of
14676 scroll_up_aggressively is too large and there are
14677 non-zero margins, because scroll_up_aggressively
14678 means put point that fraction of window height
14679 _from_the_bottom_margin_. */
14680 if (aggressive_scroll + 2*this_scroll_margin > height)
14681 aggressive_scroll = height - 2*this_scroll_margin;
14682 amount_to_scroll = dy + aggressive_scroll;
14683 }
14684 }
14685
14686 if (amount_to_scroll <= 0)
14687 return SCROLLING_FAILED;
14688
14689 start_display (&it, w, startp);
14690 if (arg_scroll_conservatively <= scroll_limit)
14691 move_it_vertically (&it, amount_to_scroll);
14692 else
14693 {
14694 /* Extra precision for users who set scroll-conservatively
14695 to a large number: make sure the amount we scroll
14696 the window start is never less than amount_to_scroll,
14697 which was computed as distance from window bottom to
14698 point. This matters when lines at window top and lines
14699 below window bottom have different height. */
14700 struct it it1;
14701 void *it1data = NULL;
14702 /* We use a temporary it1 because line_bottom_y can modify
14703 its argument, if it moves one line down; see there. */
14704 int start_y;
14705
14706 SAVE_IT (it1, it, it1data);
14707 start_y = line_bottom_y (&it1);
14708 do {
14709 RESTORE_IT (&it, &it, it1data);
14710 move_it_by_lines (&it, 1);
14711 SAVE_IT (it1, it, it1data);
14712 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14713 }
14714
14715 /* If STARTP is unchanged, move it down another screen line. */
14716 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14717 move_it_by_lines (&it, 1);
14718 startp = it.current.pos;
14719 }
14720 else
14721 {
14722 struct text_pos scroll_margin_pos = startp;
14723 int y_offset = 0;
14724
14725 /* See if point is inside the scroll margin at the top of the
14726 window. */
14727 if (this_scroll_margin)
14728 {
14729 int y_start;
14730
14731 start_display (&it, w, startp);
14732 y_start = it.current_y;
14733 move_it_vertically (&it, this_scroll_margin);
14734 scroll_margin_pos = it.current.pos;
14735 /* If we didn't move enough before hitting ZV, request
14736 additional amount of scroll, to move point out of the
14737 scroll margin. */
14738 if (IT_CHARPOS (it) == ZV
14739 && it.current_y - y_start < this_scroll_margin)
14740 y_offset = this_scroll_margin - (it.current_y - y_start);
14741 }
14742
14743 if (PT < CHARPOS (scroll_margin_pos))
14744 {
14745 /* Point is in the scroll margin at the top of the window or
14746 above what is displayed in the window. */
14747 int y0, y_to_move;
14748
14749 /* Compute the vertical distance from PT to the scroll
14750 margin position. Move as far as scroll_max allows, or
14751 one screenful, or 10 screen lines, whichever is largest.
14752 Give up if distance is greater than scroll_max or if we
14753 didn't reach the scroll margin position. */
14754 SET_TEXT_POS (pos, PT, PT_BYTE);
14755 start_display (&it, w, pos);
14756 y0 = it.current_y;
14757 y_to_move = max (it.last_visible_y,
14758 max (scroll_max, 10 * frame_line_height));
14759 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14760 y_to_move, -1,
14761 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14762 dy = it.current_y - y0;
14763 if (dy > scroll_max
14764 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14765 return SCROLLING_FAILED;
14766
14767 /* Additional scroll for when ZV was too close to point. */
14768 dy += y_offset;
14769
14770 /* Compute new window start. */
14771 start_display (&it, w, startp);
14772
14773 if (arg_scroll_conservatively)
14774 amount_to_scroll = max (dy, frame_line_height *
14775 max (scroll_step, temp_scroll_step));
14776 else if (scroll_step || temp_scroll_step)
14777 amount_to_scroll = scroll_max;
14778 else
14779 {
14780 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14781 height = WINDOW_BOX_TEXT_HEIGHT (w);
14782 if (NUMBERP (aggressive))
14783 {
14784 double float_amount = XFLOATINT (aggressive) * height;
14785 int aggressive_scroll = float_amount;
14786 if (aggressive_scroll == 0 && float_amount > 0)
14787 aggressive_scroll = 1;
14788 /* Don't let point enter the scroll margin near
14789 bottom of the window, if the value of
14790 scroll_down_aggressively happens to be too
14791 large. */
14792 if (aggressive_scroll + 2*this_scroll_margin > height)
14793 aggressive_scroll = height - 2*this_scroll_margin;
14794 amount_to_scroll = dy + aggressive_scroll;
14795 }
14796 }
14797
14798 if (amount_to_scroll <= 0)
14799 return SCROLLING_FAILED;
14800
14801 move_it_vertically_backward (&it, amount_to_scroll);
14802 startp = it.current.pos;
14803 }
14804 }
14805
14806 /* Run window scroll functions. */
14807 startp = run_window_scroll_functions (window, startp);
14808
14809 /* Display the window. Give up if new fonts are loaded, or if point
14810 doesn't appear. */
14811 if (!try_window (window, startp, 0))
14812 rc = SCROLLING_NEED_LARGER_MATRICES;
14813 else if (w->cursor.vpos < 0)
14814 {
14815 clear_glyph_matrix (w->desired_matrix);
14816 rc = SCROLLING_FAILED;
14817 }
14818 else
14819 {
14820 /* Maybe forget recorded base line for line number display. */
14821 if (!just_this_one_p
14822 || current_buffer->clip_changed
14823 || BEG_UNCHANGED < CHARPOS (startp))
14824 w->base_line_number = 0;
14825
14826 /* If cursor ends up on a partially visible line,
14827 treat that as being off the bottom of the screen. */
14828 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14829 /* It's possible that the cursor is on the first line of the
14830 buffer, which is partially obscured due to a vscroll
14831 (Bug#7537). In that case, avoid looping forever . */
14832 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14833 {
14834 clear_glyph_matrix (w->desired_matrix);
14835 ++extra_scroll_margin_lines;
14836 goto too_near_end;
14837 }
14838 rc = SCROLLING_SUCCESS;
14839 }
14840
14841 return rc;
14842 }
14843
14844
14845 /* Compute a suitable window start for window W if display of W starts
14846 on a continuation line. Value is non-zero if a new window start
14847 was computed.
14848
14849 The new window start will be computed, based on W's width, starting
14850 from the start of the continued line. It is the start of the
14851 screen line with the minimum distance from the old start W->start. */
14852
14853 static int
14854 compute_window_start_on_continuation_line (struct window *w)
14855 {
14856 struct text_pos pos, start_pos;
14857 int window_start_changed_p = 0;
14858
14859 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14860
14861 /* If window start is on a continuation line... Window start may be
14862 < BEGV in case there's invisible text at the start of the
14863 buffer (M-x rmail, for example). */
14864 if (CHARPOS (start_pos) > BEGV
14865 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14866 {
14867 struct it it;
14868 struct glyph_row *row;
14869
14870 /* Handle the case that the window start is out of range. */
14871 if (CHARPOS (start_pos) < BEGV)
14872 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14873 else if (CHARPOS (start_pos) > ZV)
14874 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14875
14876 /* Find the start of the continued line. This should be fast
14877 because find_newline is fast (newline cache). */
14878 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14879 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14880 row, DEFAULT_FACE_ID);
14881 reseat_at_previous_visible_line_start (&it);
14882
14883 /* If the line start is "too far" away from the window start,
14884 say it takes too much time to compute a new window start. */
14885 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14886 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14887 {
14888 int min_distance, distance;
14889
14890 /* Move forward by display lines to find the new window
14891 start. If window width was enlarged, the new start can
14892 be expected to be > the old start. If window width was
14893 decreased, the new window start will be < the old start.
14894 So, we're looking for the display line start with the
14895 minimum distance from the old window start. */
14896 pos = it.current.pos;
14897 min_distance = INFINITY;
14898 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14899 distance < min_distance)
14900 {
14901 min_distance = distance;
14902 pos = it.current.pos;
14903 move_it_by_lines (&it, 1);
14904 }
14905
14906 /* Set the window start there. */
14907 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14908 window_start_changed_p = 1;
14909 }
14910 }
14911
14912 return window_start_changed_p;
14913 }
14914
14915
14916 /* Try cursor movement in case text has not changed in window WINDOW,
14917 with window start STARTP. Value is
14918
14919 CURSOR_MOVEMENT_SUCCESS if successful
14920
14921 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14922
14923 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14924 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14925 we want to scroll as if scroll-step were set to 1. See the code.
14926
14927 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14928 which case we have to abort this redisplay, and adjust matrices
14929 first. */
14930
14931 enum
14932 {
14933 CURSOR_MOVEMENT_SUCCESS,
14934 CURSOR_MOVEMENT_CANNOT_BE_USED,
14935 CURSOR_MOVEMENT_MUST_SCROLL,
14936 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14937 };
14938
14939 static int
14940 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14941 {
14942 struct window *w = XWINDOW (window);
14943 struct frame *f = XFRAME (w->frame);
14944 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14945
14946 #ifdef GLYPH_DEBUG
14947 if (inhibit_try_cursor_movement)
14948 return rc;
14949 #endif
14950
14951 /* Previously, there was a check for Lisp integer in the
14952 if-statement below. Now, this field is converted to
14953 ptrdiff_t, thus zero means invalid position in a buffer. */
14954 eassert (w->last_point > 0);
14955
14956 /* Handle case where text has not changed, only point, and it has
14957 not moved off the frame. */
14958 if (/* Point may be in this window. */
14959 PT >= CHARPOS (startp)
14960 /* Selective display hasn't changed. */
14961 && !current_buffer->clip_changed
14962 /* Function force-mode-line-update is used to force a thorough
14963 redisplay. It sets either windows_or_buffers_changed or
14964 update_mode_lines. So don't take a shortcut here for these
14965 cases. */
14966 && !update_mode_lines
14967 && !windows_or_buffers_changed
14968 && !cursor_type_changed
14969 /* Can't use this case if highlighting a region. When a
14970 region exists, cursor movement has to do more than just
14971 set the cursor. */
14972 && markpos_of_region () < 0
14973 && !w->region_showing
14974 && NILP (Vshow_trailing_whitespace)
14975 /* This code is not used for mini-buffer for the sake of the case
14976 of redisplaying to replace an echo area message; since in
14977 that case the mini-buffer contents per se are usually
14978 unchanged. This code is of no real use in the mini-buffer
14979 since the handling of this_line_start_pos, etc., in redisplay
14980 handles the same cases. */
14981 && !EQ (window, minibuf_window)
14982 /* When splitting windows or for new windows, it happens that
14983 redisplay is called with a nil window_end_vpos or one being
14984 larger than the window. This should really be fixed in
14985 window.c. I don't have this on my list, now, so we do
14986 approximately the same as the old redisplay code. --gerd. */
14987 && INTEGERP (w->window_end_vpos)
14988 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14989 && (FRAME_WINDOW_P (f)
14990 || !overlay_arrow_in_current_buffer_p ()))
14991 {
14992 int this_scroll_margin, top_scroll_margin;
14993 struct glyph_row *row = NULL;
14994 int frame_line_height = default_line_pixel_height (w);
14995 int window_total_lines
14996 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
14997
14998 #ifdef GLYPH_DEBUG
14999 debug_method_add (w, "cursor movement");
15000 #endif
15001
15002 /* Scroll if point within this distance from the top or bottom
15003 of the window. This is a pixel value. */
15004 if (scroll_margin > 0)
15005 {
15006 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
15007 this_scroll_margin *= frame_line_height;
15008 }
15009 else
15010 this_scroll_margin = 0;
15011
15012 top_scroll_margin = this_scroll_margin;
15013 if (WINDOW_WANTS_HEADER_LINE_P (w))
15014 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15015
15016 /* Start with the row the cursor was displayed during the last
15017 not paused redisplay. Give up if that row is not valid. */
15018 if (w->last_cursor.vpos < 0
15019 || w->last_cursor.vpos >= w->current_matrix->nrows)
15020 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15021 else
15022 {
15023 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15024 if (row->mode_line_p)
15025 ++row;
15026 if (!row->enabled_p)
15027 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15028 }
15029
15030 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15031 {
15032 int scroll_p = 0, must_scroll = 0;
15033 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15034
15035 if (PT > w->last_point)
15036 {
15037 /* Point has moved forward. */
15038 while (MATRIX_ROW_END_CHARPOS (row) < PT
15039 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15040 {
15041 eassert (row->enabled_p);
15042 ++row;
15043 }
15044
15045 /* If the end position of a row equals the start
15046 position of the next row, and PT is at that position,
15047 we would rather display cursor in the next line. */
15048 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15049 && MATRIX_ROW_END_CHARPOS (row) == PT
15050 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
15051 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15052 && !cursor_row_p (row))
15053 ++row;
15054
15055 /* If within the scroll margin, scroll. Note that
15056 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15057 the next line would be drawn, and that
15058 this_scroll_margin can be zero. */
15059 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15060 || PT > MATRIX_ROW_END_CHARPOS (row)
15061 /* Line is completely visible last line in window
15062 and PT is to be set in the next line. */
15063 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15064 && PT == MATRIX_ROW_END_CHARPOS (row)
15065 && !row->ends_at_zv_p
15066 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15067 scroll_p = 1;
15068 }
15069 else if (PT < w->last_point)
15070 {
15071 /* Cursor has to be moved backward. Note that PT >=
15072 CHARPOS (startp) because of the outer if-statement. */
15073 while (!row->mode_line_p
15074 && (MATRIX_ROW_START_CHARPOS (row) > PT
15075 || (MATRIX_ROW_START_CHARPOS (row) == PT
15076 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15077 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15078 row > w->current_matrix->rows
15079 && (row-1)->ends_in_newline_from_string_p))))
15080 && (row->y > top_scroll_margin
15081 || CHARPOS (startp) == BEGV))
15082 {
15083 eassert (row->enabled_p);
15084 --row;
15085 }
15086
15087 /* Consider the following case: Window starts at BEGV,
15088 there is invisible, intangible text at BEGV, so that
15089 display starts at some point START > BEGV. It can
15090 happen that we are called with PT somewhere between
15091 BEGV and START. Try to handle that case. */
15092 if (row < w->current_matrix->rows
15093 || row->mode_line_p)
15094 {
15095 row = w->current_matrix->rows;
15096 if (row->mode_line_p)
15097 ++row;
15098 }
15099
15100 /* Due to newlines in overlay strings, we may have to
15101 skip forward over overlay strings. */
15102 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15103 && MATRIX_ROW_END_CHARPOS (row) == PT
15104 && !cursor_row_p (row))
15105 ++row;
15106
15107 /* If within the scroll margin, scroll. */
15108 if (row->y < top_scroll_margin
15109 && CHARPOS (startp) != BEGV)
15110 scroll_p = 1;
15111 }
15112 else
15113 {
15114 /* Cursor did not move. So don't scroll even if cursor line
15115 is partially visible, as it was so before. */
15116 rc = CURSOR_MOVEMENT_SUCCESS;
15117 }
15118
15119 if (PT < MATRIX_ROW_START_CHARPOS (row)
15120 || PT > MATRIX_ROW_END_CHARPOS (row))
15121 {
15122 /* if PT is not in the glyph row, give up. */
15123 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15124 must_scroll = 1;
15125 }
15126 else if (rc != CURSOR_MOVEMENT_SUCCESS
15127 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15128 {
15129 struct glyph_row *row1;
15130
15131 /* If rows are bidi-reordered and point moved, back up
15132 until we find a row that does not belong to a
15133 continuation line. This is because we must consider
15134 all rows of a continued line as candidates for the
15135 new cursor positioning, since row start and end
15136 positions change non-linearly with vertical position
15137 in such rows. */
15138 /* FIXME: Revisit this when glyph ``spilling'' in
15139 continuation lines' rows is implemented for
15140 bidi-reordered rows. */
15141 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15142 MATRIX_ROW_CONTINUATION_LINE_P (row);
15143 --row)
15144 {
15145 /* If we hit the beginning of the displayed portion
15146 without finding the first row of a continued
15147 line, give up. */
15148 if (row <= row1)
15149 {
15150 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15151 break;
15152 }
15153 eassert (row->enabled_p);
15154 }
15155 }
15156 if (must_scroll)
15157 ;
15158 else if (rc != CURSOR_MOVEMENT_SUCCESS
15159 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15160 /* Make sure this isn't a header line by any chance, since
15161 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15162 && !row->mode_line_p
15163 && make_cursor_line_fully_visible_p)
15164 {
15165 if (PT == MATRIX_ROW_END_CHARPOS (row)
15166 && !row->ends_at_zv_p
15167 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15168 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15169 else if (row->height > window_box_height (w))
15170 {
15171 /* If we end up in a partially visible line, let's
15172 make it fully visible, except when it's taller
15173 than the window, in which case we can't do much
15174 about it. */
15175 *scroll_step = 1;
15176 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15177 }
15178 else
15179 {
15180 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15181 if (!cursor_row_fully_visible_p (w, 0, 1))
15182 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15183 else
15184 rc = CURSOR_MOVEMENT_SUCCESS;
15185 }
15186 }
15187 else if (scroll_p)
15188 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15189 else if (rc != CURSOR_MOVEMENT_SUCCESS
15190 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15191 {
15192 /* With bidi-reordered rows, there could be more than
15193 one candidate row whose start and end positions
15194 occlude point. We need to let set_cursor_from_row
15195 find the best candidate. */
15196 /* FIXME: Revisit this when glyph ``spilling'' in
15197 continuation lines' rows is implemented for
15198 bidi-reordered rows. */
15199 int rv = 0;
15200
15201 do
15202 {
15203 int at_zv_p = 0, exact_match_p = 0;
15204
15205 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15206 && PT <= MATRIX_ROW_END_CHARPOS (row)
15207 && cursor_row_p (row))
15208 rv |= set_cursor_from_row (w, row, w->current_matrix,
15209 0, 0, 0, 0);
15210 /* As soon as we've found the exact match for point,
15211 or the first suitable row whose ends_at_zv_p flag
15212 is set, we are done. */
15213 at_zv_p =
15214 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15215 if (rv && !at_zv_p
15216 && w->cursor.hpos >= 0
15217 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15218 w->cursor.vpos))
15219 {
15220 struct glyph_row *candidate =
15221 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15222 struct glyph *g =
15223 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15224 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15225
15226 exact_match_p =
15227 (BUFFERP (g->object) && g->charpos == PT)
15228 || (INTEGERP (g->object)
15229 && (g->charpos == PT
15230 || (g->charpos == 0 && endpos - 1 == PT)));
15231 }
15232 if (rv && (at_zv_p || exact_match_p))
15233 {
15234 rc = CURSOR_MOVEMENT_SUCCESS;
15235 break;
15236 }
15237 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15238 break;
15239 ++row;
15240 }
15241 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15242 || row->continued_p)
15243 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15244 || (MATRIX_ROW_START_CHARPOS (row) == PT
15245 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15246 /* If we didn't find any candidate rows, or exited the
15247 loop before all the candidates were examined, signal
15248 to the caller that this method failed. */
15249 if (rc != CURSOR_MOVEMENT_SUCCESS
15250 && !(rv
15251 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15252 && !row->continued_p))
15253 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15254 else if (rv)
15255 rc = CURSOR_MOVEMENT_SUCCESS;
15256 }
15257 else
15258 {
15259 do
15260 {
15261 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15262 {
15263 rc = CURSOR_MOVEMENT_SUCCESS;
15264 break;
15265 }
15266 ++row;
15267 }
15268 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15269 && MATRIX_ROW_START_CHARPOS (row) == PT
15270 && cursor_row_p (row));
15271 }
15272 }
15273 }
15274
15275 return rc;
15276 }
15277
15278 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15279 static
15280 #endif
15281 void
15282 set_vertical_scroll_bar (struct window *w)
15283 {
15284 ptrdiff_t start, end, whole;
15285
15286 /* Calculate the start and end positions for the current window.
15287 At some point, it would be nice to choose between scrollbars
15288 which reflect the whole buffer size, with special markers
15289 indicating narrowing, and scrollbars which reflect only the
15290 visible region.
15291
15292 Note that mini-buffers sometimes aren't displaying any text. */
15293 if (!MINI_WINDOW_P (w)
15294 || (w == XWINDOW (minibuf_window)
15295 && NILP (echo_area_buffer[0])))
15296 {
15297 struct buffer *buf = XBUFFER (w->contents);
15298 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15299 start = marker_position (w->start) - BUF_BEGV (buf);
15300 /* I don't think this is guaranteed to be right. For the
15301 moment, we'll pretend it is. */
15302 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15303
15304 if (end < start)
15305 end = start;
15306 if (whole < (end - start))
15307 whole = end - start;
15308 }
15309 else
15310 start = end = whole = 0;
15311
15312 /* Indicate what this scroll bar ought to be displaying now. */
15313 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15314 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15315 (w, end - start, whole, start);
15316 }
15317
15318
15319 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15320 selected_window is redisplayed.
15321
15322 We can return without actually redisplaying the window if
15323 fonts_changed_p. In that case, redisplay_internal will
15324 retry. */
15325
15326 static void
15327 redisplay_window (Lisp_Object window, int just_this_one_p)
15328 {
15329 struct window *w = XWINDOW (window);
15330 struct frame *f = XFRAME (w->frame);
15331 struct buffer *buffer = XBUFFER (w->contents);
15332 struct buffer *old = current_buffer;
15333 struct text_pos lpoint, opoint, startp;
15334 int update_mode_line;
15335 int tem;
15336 struct it it;
15337 /* Record it now because it's overwritten. */
15338 int current_matrix_up_to_date_p = 0;
15339 int used_current_matrix_p = 0;
15340 /* This is less strict than current_matrix_up_to_date_p.
15341 It indicates that the buffer contents and narrowing are unchanged. */
15342 int buffer_unchanged_p = 0;
15343 int temp_scroll_step = 0;
15344 ptrdiff_t count = SPECPDL_INDEX ();
15345 int rc;
15346 int centering_position = -1;
15347 int last_line_misfit = 0;
15348 ptrdiff_t beg_unchanged, end_unchanged;
15349 int frame_line_height;
15350
15351 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15352 opoint = lpoint;
15353
15354 #ifdef GLYPH_DEBUG
15355 *w->desired_matrix->method = 0;
15356 #endif
15357
15358 /* Make sure that both W's markers are valid. */
15359 eassert (XMARKER (w->start)->buffer == buffer);
15360 eassert (XMARKER (w->pointm)->buffer == buffer);
15361
15362 restart:
15363 reconsider_clip_changes (w, buffer);
15364 frame_line_height = default_line_pixel_height (w);
15365
15366 /* Has the mode line to be updated? */
15367 update_mode_line = (w->update_mode_line
15368 || update_mode_lines
15369 || buffer->clip_changed
15370 || buffer->prevent_redisplay_optimizations_p);
15371
15372 if (MINI_WINDOW_P (w))
15373 {
15374 if (w == XWINDOW (echo_area_window)
15375 && !NILP (echo_area_buffer[0]))
15376 {
15377 if (update_mode_line)
15378 /* We may have to update a tty frame's menu bar or a
15379 tool-bar. Example `M-x C-h C-h C-g'. */
15380 goto finish_menu_bars;
15381 else
15382 /* We've already displayed the echo area glyphs in this window. */
15383 goto finish_scroll_bars;
15384 }
15385 else if ((w != XWINDOW (minibuf_window)
15386 || minibuf_level == 0)
15387 /* When buffer is nonempty, redisplay window normally. */
15388 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15389 /* Quail displays non-mini buffers in minibuffer window.
15390 In that case, redisplay the window normally. */
15391 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15392 {
15393 /* W is a mini-buffer window, but it's not active, so clear
15394 it. */
15395 int yb = window_text_bottom_y (w);
15396 struct glyph_row *row;
15397 int y;
15398
15399 for (y = 0, row = w->desired_matrix->rows;
15400 y < yb;
15401 y += row->height, ++row)
15402 blank_row (w, row, y);
15403 goto finish_scroll_bars;
15404 }
15405
15406 clear_glyph_matrix (w->desired_matrix);
15407 }
15408
15409 /* Otherwise set up data on this window; select its buffer and point
15410 value. */
15411 /* Really select the buffer, for the sake of buffer-local
15412 variables. */
15413 set_buffer_internal_1 (XBUFFER (w->contents));
15414
15415 current_matrix_up_to_date_p
15416 = (w->window_end_valid
15417 && !current_buffer->clip_changed
15418 && !current_buffer->prevent_redisplay_optimizations_p
15419 && !window_outdated (w));
15420
15421 /* Run the window-bottom-change-functions
15422 if it is possible that the text on the screen has changed
15423 (either due to modification of the text, or any other reason). */
15424 if (!current_matrix_up_to_date_p
15425 && !NILP (Vwindow_text_change_functions))
15426 {
15427 safe_run_hooks (Qwindow_text_change_functions);
15428 goto restart;
15429 }
15430
15431 beg_unchanged = BEG_UNCHANGED;
15432 end_unchanged = END_UNCHANGED;
15433
15434 SET_TEXT_POS (opoint, PT, PT_BYTE);
15435
15436 specbind (Qinhibit_point_motion_hooks, Qt);
15437
15438 buffer_unchanged_p
15439 = (w->window_end_valid
15440 && !current_buffer->clip_changed
15441 && !window_outdated (w));
15442
15443 /* When windows_or_buffers_changed is non-zero, we can't rely on
15444 the window end being valid, so set it to nil there. */
15445 if (windows_or_buffers_changed)
15446 {
15447 /* If window starts on a continuation line, maybe adjust the
15448 window start in case the window's width changed. */
15449 if (XMARKER (w->start)->buffer == current_buffer)
15450 compute_window_start_on_continuation_line (w);
15451
15452 w->window_end_valid = 0;
15453 }
15454
15455 /* Some sanity checks. */
15456 CHECK_WINDOW_END (w);
15457 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15458 emacs_abort ();
15459 if (BYTEPOS (opoint) < CHARPOS (opoint))
15460 emacs_abort ();
15461
15462 if (mode_line_update_needed (w))
15463 update_mode_line = 1;
15464
15465 /* Point refers normally to the selected window. For any other
15466 window, set up appropriate value. */
15467 if (!EQ (window, selected_window))
15468 {
15469 ptrdiff_t new_pt = marker_position (w->pointm);
15470 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15471 if (new_pt < BEGV)
15472 {
15473 new_pt = BEGV;
15474 new_pt_byte = BEGV_BYTE;
15475 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15476 }
15477 else if (new_pt > (ZV - 1))
15478 {
15479 new_pt = ZV;
15480 new_pt_byte = ZV_BYTE;
15481 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15482 }
15483
15484 /* We don't use SET_PT so that the point-motion hooks don't run. */
15485 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15486 }
15487
15488 /* If any of the character widths specified in the display table
15489 have changed, invalidate the width run cache. It's true that
15490 this may be a bit late to catch such changes, but the rest of
15491 redisplay goes (non-fatally) haywire when the display table is
15492 changed, so why should we worry about doing any better? */
15493 if (current_buffer->width_run_cache)
15494 {
15495 struct Lisp_Char_Table *disptab = buffer_display_table ();
15496
15497 if (! disptab_matches_widthtab
15498 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15499 {
15500 invalidate_region_cache (current_buffer,
15501 current_buffer->width_run_cache,
15502 BEG, Z);
15503 recompute_width_table (current_buffer, disptab);
15504 }
15505 }
15506
15507 /* If window-start is screwed up, choose a new one. */
15508 if (XMARKER (w->start)->buffer != current_buffer)
15509 goto recenter;
15510
15511 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15512
15513 /* If someone specified a new starting point but did not insist,
15514 check whether it can be used. */
15515 if (w->optional_new_start
15516 && CHARPOS (startp) >= BEGV
15517 && CHARPOS (startp) <= ZV)
15518 {
15519 w->optional_new_start = 0;
15520 start_display (&it, w, startp);
15521 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15522 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15523 if (IT_CHARPOS (it) == PT)
15524 w->force_start = 1;
15525 /* IT may overshoot PT if text at PT is invisible. */
15526 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15527 w->force_start = 1;
15528 }
15529
15530 force_start:
15531
15532 /* Handle case where place to start displaying has been specified,
15533 unless the specified location is outside the accessible range. */
15534 if (w->force_start || w->frozen_window_start_p)
15535 {
15536 /* We set this later on if we have to adjust point. */
15537 int new_vpos = -1;
15538
15539 w->force_start = 0;
15540 w->vscroll = 0;
15541 w->window_end_valid = 0;
15542
15543 /* Forget any recorded base line for line number display. */
15544 if (!buffer_unchanged_p)
15545 w->base_line_number = 0;
15546
15547 /* Redisplay the mode line. Select the buffer properly for that.
15548 Also, run the hook window-scroll-functions
15549 because we have scrolled. */
15550 /* Note, we do this after clearing force_start because
15551 if there's an error, it is better to forget about force_start
15552 than to get into an infinite loop calling the hook functions
15553 and having them get more errors. */
15554 if (!update_mode_line
15555 || ! NILP (Vwindow_scroll_functions))
15556 {
15557 update_mode_line = 1;
15558 w->update_mode_line = 1;
15559 startp = run_window_scroll_functions (window, startp);
15560 }
15561
15562 w->last_modified = 0;
15563 w->last_overlay_modified = 0;
15564 if (CHARPOS (startp) < BEGV)
15565 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15566 else if (CHARPOS (startp) > ZV)
15567 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15568
15569 /* Redisplay, then check if cursor has been set during the
15570 redisplay. Give up if new fonts were loaded. */
15571 /* We used to issue a CHECK_MARGINS argument to try_window here,
15572 but this causes scrolling to fail when point begins inside
15573 the scroll margin (bug#148) -- cyd */
15574 if (!try_window (window, startp, 0))
15575 {
15576 w->force_start = 1;
15577 clear_glyph_matrix (w->desired_matrix);
15578 goto need_larger_matrices;
15579 }
15580
15581 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15582 {
15583 /* If point does not appear, try to move point so it does
15584 appear. The desired matrix has been built above, so we
15585 can use it here. */
15586 new_vpos = window_box_height (w) / 2;
15587 }
15588
15589 if (!cursor_row_fully_visible_p (w, 0, 0))
15590 {
15591 /* Point does appear, but on a line partly visible at end of window.
15592 Move it back to a fully-visible line. */
15593 new_vpos = window_box_height (w);
15594 }
15595 else if (w->cursor.vpos >=0)
15596 {
15597 /* Some people insist on not letting point enter the scroll
15598 margin, even though this part handles windows that didn't
15599 scroll at all. */
15600 int window_total_lines
15601 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15602 int margin = min (scroll_margin, window_total_lines / 4);
15603 int pixel_margin = margin * frame_line_height;
15604 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15605
15606 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15607 below, which finds the row to move point to, advances by
15608 the Y coordinate of the _next_ row, see the definition of
15609 MATRIX_ROW_BOTTOM_Y. */
15610 if (w->cursor.vpos < margin + header_line)
15611 {
15612 w->cursor.vpos = -1;
15613 clear_glyph_matrix (w->desired_matrix);
15614 goto try_to_scroll;
15615 }
15616 else
15617 {
15618 int window_height = window_box_height (w);
15619
15620 if (header_line)
15621 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15622 if (w->cursor.y >= window_height - pixel_margin)
15623 {
15624 w->cursor.vpos = -1;
15625 clear_glyph_matrix (w->desired_matrix);
15626 goto try_to_scroll;
15627 }
15628 }
15629 }
15630
15631 /* If we need to move point for either of the above reasons,
15632 now actually do it. */
15633 if (new_vpos >= 0)
15634 {
15635 struct glyph_row *row;
15636
15637 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15638 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15639 ++row;
15640
15641 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15642 MATRIX_ROW_START_BYTEPOS (row));
15643
15644 if (w != XWINDOW (selected_window))
15645 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15646 else if (current_buffer == old)
15647 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15648
15649 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15650
15651 /* If we are highlighting the region, then we just changed
15652 the region, so redisplay to show it. */
15653 if (markpos_of_region () >= 0)
15654 {
15655 clear_glyph_matrix (w->desired_matrix);
15656 if (!try_window (window, startp, 0))
15657 goto need_larger_matrices;
15658 }
15659 }
15660
15661 #ifdef GLYPH_DEBUG
15662 debug_method_add (w, "forced window start");
15663 #endif
15664 goto done;
15665 }
15666
15667 /* Handle case where text has not changed, only point, and it has
15668 not moved off the frame, and we are not retrying after hscroll.
15669 (current_matrix_up_to_date_p is nonzero when retrying.) */
15670 if (current_matrix_up_to_date_p
15671 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15672 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15673 {
15674 switch (rc)
15675 {
15676 case CURSOR_MOVEMENT_SUCCESS:
15677 used_current_matrix_p = 1;
15678 goto done;
15679
15680 case CURSOR_MOVEMENT_MUST_SCROLL:
15681 goto try_to_scroll;
15682
15683 default:
15684 emacs_abort ();
15685 }
15686 }
15687 /* If current starting point was originally the beginning of a line
15688 but no longer is, find a new starting point. */
15689 else if (w->start_at_line_beg
15690 && !(CHARPOS (startp) <= BEGV
15691 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15692 {
15693 #ifdef GLYPH_DEBUG
15694 debug_method_add (w, "recenter 1");
15695 #endif
15696 goto recenter;
15697 }
15698
15699 /* Try scrolling with try_window_id. Value is > 0 if update has
15700 been done, it is -1 if we know that the same window start will
15701 not work. It is 0 if unsuccessful for some other reason. */
15702 else if ((tem = try_window_id (w)) != 0)
15703 {
15704 #ifdef GLYPH_DEBUG
15705 debug_method_add (w, "try_window_id %d", tem);
15706 #endif
15707
15708 if (fonts_changed_p)
15709 goto need_larger_matrices;
15710 if (tem > 0)
15711 goto done;
15712
15713 /* Otherwise try_window_id has returned -1 which means that we
15714 don't want the alternative below this comment to execute. */
15715 }
15716 else if (CHARPOS (startp) >= BEGV
15717 && CHARPOS (startp) <= ZV
15718 && PT >= CHARPOS (startp)
15719 && (CHARPOS (startp) < ZV
15720 /* Avoid starting at end of buffer. */
15721 || CHARPOS (startp) == BEGV
15722 || !window_outdated (w)))
15723 {
15724 int d1, d2, d3, d4, d5, d6;
15725
15726 /* If first window line is a continuation line, and window start
15727 is inside the modified region, but the first change is before
15728 current window start, we must select a new window start.
15729
15730 However, if this is the result of a down-mouse event (e.g. by
15731 extending the mouse-drag-overlay), we don't want to select a
15732 new window start, since that would change the position under
15733 the mouse, resulting in an unwanted mouse-movement rather
15734 than a simple mouse-click. */
15735 if (!w->start_at_line_beg
15736 && NILP (do_mouse_tracking)
15737 && CHARPOS (startp) > BEGV
15738 && CHARPOS (startp) > BEG + beg_unchanged
15739 && CHARPOS (startp) <= Z - end_unchanged
15740 /* Even if w->start_at_line_beg is nil, a new window may
15741 start at a line_beg, since that's how set_buffer_window
15742 sets it. So, we need to check the return value of
15743 compute_window_start_on_continuation_line. (See also
15744 bug#197). */
15745 && XMARKER (w->start)->buffer == current_buffer
15746 && compute_window_start_on_continuation_line (w)
15747 /* It doesn't make sense to force the window start like we
15748 do at label force_start if it is already known that point
15749 will not be visible in the resulting window, because
15750 doing so will move point from its correct position
15751 instead of scrolling the window to bring point into view.
15752 See bug#9324. */
15753 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15754 {
15755 w->force_start = 1;
15756 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15757 goto force_start;
15758 }
15759
15760 #ifdef GLYPH_DEBUG
15761 debug_method_add (w, "same window start");
15762 #endif
15763
15764 /* Try to redisplay starting at same place as before.
15765 If point has not moved off frame, accept the results. */
15766 if (!current_matrix_up_to_date_p
15767 /* Don't use try_window_reusing_current_matrix in this case
15768 because a window scroll function can have changed the
15769 buffer. */
15770 || !NILP (Vwindow_scroll_functions)
15771 || MINI_WINDOW_P (w)
15772 || !(used_current_matrix_p
15773 = try_window_reusing_current_matrix (w)))
15774 {
15775 IF_DEBUG (debug_method_add (w, "1"));
15776 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15777 /* -1 means we need to scroll.
15778 0 means we need new matrices, but fonts_changed_p
15779 is set in that case, so we will detect it below. */
15780 goto try_to_scroll;
15781 }
15782
15783 if (fonts_changed_p)
15784 goto need_larger_matrices;
15785
15786 if (w->cursor.vpos >= 0)
15787 {
15788 if (!just_this_one_p
15789 || current_buffer->clip_changed
15790 || BEG_UNCHANGED < CHARPOS (startp))
15791 /* Forget any recorded base line for line number display. */
15792 w->base_line_number = 0;
15793
15794 if (!cursor_row_fully_visible_p (w, 1, 0))
15795 {
15796 clear_glyph_matrix (w->desired_matrix);
15797 last_line_misfit = 1;
15798 }
15799 /* Drop through and scroll. */
15800 else
15801 goto done;
15802 }
15803 else
15804 clear_glyph_matrix (w->desired_matrix);
15805 }
15806
15807 try_to_scroll:
15808
15809 w->last_modified = 0;
15810 w->last_overlay_modified = 0;
15811
15812 /* Redisplay the mode line. Select the buffer properly for that. */
15813 if (!update_mode_line)
15814 {
15815 update_mode_line = 1;
15816 w->update_mode_line = 1;
15817 }
15818
15819 /* Try to scroll by specified few lines. */
15820 if ((scroll_conservatively
15821 || emacs_scroll_step
15822 || temp_scroll_step
15823 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15824 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15825 && CHARPOS (startp) >= BEGV
15826 && CHARPOS (startp) <= ZV)
15827 {
15828 /* The function returns -1 if new fonts were loaded, 1 if
15829 successful, 0 if not successful. */
15830 int ss = try_scrolling (window, just_this_one_p,
15831 scroll_conservatively,
15832 emacs_scroll_step,
15833 temp_scroll_step, last_line_misfit);
15834 switch (ss)
15835 {
15836 case SCROLLING_SUCCESS:
15837 goto done;
15838
15839 case SCROLLING_NEED_LARGER_MATRICES:
15840 goto need_larger_matrices;
15841
15842 case SCROLLING_FAILED:
15843 break;
15844
15845 default:
15846 emacs_abort ();
15847 }
15848 }
15849
15850 /* Finally, just choose a place to start which positions point
15851 according to user preferences. */
15852
15853 recenter:
15854
15855 #ifdef GLYPH_DEBUG
15856 debug_method_add (w, "recenter");
15857 #endif
15858
15859 /* Forget any previously recorded base line for line number display. */
15860 if (!buffer_unchanged_p)
15861 w->base_line_number = 0;
15862
15863 /* Determine the window start relative to point. */
15864 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15865 it.current_y = it.last_visible_y;
15866 if (centering_position < 0)
15867 {
15868 int window_total_lines
15869 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
15870 int margin =
15871 scroll_margin > 0
15872 ? min (scroll_margin, window_total_lines / 4)
15873 : 0;
15874 ptrdiff_t margin_pos = CHARPOS (startp);
15875 Lisp_Object aggressive;
15876 int scrolling_up;
15877
15878 /* If there is a scroll margin at the top of the window, find
15879 its character position. */
15880 if (margin
15881 /* Cannot call start_display if startp is not in the
15882 accessible region of the buffer. This can happen when we
15883 have just switched to a different buffer and/or changed
15884 its restriction. In that case, startp is initialized to
15885 the character position 1 (BEGV) because we did not yet
15886 have chance to display the buffer even once. */
15887 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15888 {
15889 struct it it1;
15890 void *it1data = NULL;
15891
15892 SAVE_IT (it1, it, it1data);
15893 start_display (&it1, w, startp);
15894 move_it_vertically (&it1, margin * frame_line_height);
15895 margin_pos = IT_CHARPOS (it1);
15896 RESTORE_IT (&it, &it, it1data);
15897 }
15898 scrolling_up = PT > margin_pos;
15899 aggressive =
15900 scrolling_up
15901 ? BVAR (current_buffer, scroll_up_aggressively)
15902 : BVAR (current_buffer, scroll_down_aggressively);
15903
15904 if (!MINI_WINDOW_P (w)
15905 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15906 {
15907 int pt_offset = 0;
15908
15909 /* Setting scroll-conservatively overrides
15910 scroll-*-aggressively. */
15911 if (!scroll_conservatively && NUMBERP (aggressive))
15912 {
15913 double float_amount = XFLOATINT (aggressive);
15914
15915 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15916 if (pt_offset == 0 && float_amount > 0)
15917 pt_offset = 1;
15918 if (pt_offset && margin > 0)
15919 margin -= 1;
15920 }
15921 /* Compute how much to move the window start backward from
15922 point so that point will be displayed where the user
15923 wants it. */
15924 if (scrolling_up)
15925 {
15926 centering_position = it.last_visible_y;
15927 if (pt_offset)
15928 centering_position -= pt_offset;
15929 centering_position -=
15930 frame_line_height * (1 + margin + (last_line_misfit != 0))
15931 + WINDOW_HEADER_LINE_HEIGHT (w);
15932 /* Don't let point enter the scroll margin near top of
15933 the window. */
15934 if (centering_position < margin * frame_line_height)
15935 centering_position = margin * frame_line_height;
15936 }
15937 else
15938 centering_position = margin * frame_line_height + pt_offset;
15939 }
15940 else
15941 /* Set the window start half the height of the window backward
15942 from point. */
15943 centering_position = window_box_height (w) / 2;
15944 }
15945 move_it_vertically_backward (&it, centering_position);
15946
15947 eassert (IT_CHARPOS (it) >= BEGV);
15948
15949 /* The function move_it_vertically_backward may move over more
15950 than the specified y-distance. If it->w is small, e.g. a
15951 mini-buffer window, we may end up in front of the window's
15952 display area. Start displaying at the start of the line
15953 containing PT in this case. */
15954 if (it.current_y <= 0)
15955 {
15956 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15957 move_it_vertically_backward (&it, 0);
15958 it.current_y = 0;
15959 }
15960
15961 it.current_x = it.hpos = 0;
15962
15963 /* Set the window start position here explicitly, to avoid an
15964 infinite loop in case the functions in window-scroll-functions
15965 get errors. */
15966 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15967
15968 /* Run scroll hooks. */
15969 startp = run_window_scroll_functions (window, it.current.pos);
15970
15971 /* Redisplay the window. */
15972 if (!current_matrix_up_to_date_p
15973 || windows_or_buffers_changed
15974 || cursor_type_changed
15975 /* Don't use try_window_reusing_current_matrix in this case
15976 because it can have changed the buffer. */
15977 || !NILP (Vwindow_scroll_functions)
15978 || !just_this_one_p
15979 || MINI_WINDOW_P (w)
15980 || !(used_current_matrix_p
15981 = try_window_reusing_current_matrix (w)))
15982 try_window (window, startp, 0);
15983
15984 /* If new fonts have been loaded (due to fontsets), give up. We
15985 have to start a new redisplay since we need to re-adjust glyph
15986 matrices. */
15987 if (fonts_changed_p)
15988 goto need_larger_matrices;
15989
15990 /* If cursor did not appear assume that the middle of the window is
15991 in the first line of the window. Do it again with the next line.
15992 (Imagine a window of height 100, displaying two lines of height
15993 60. Moving back 50 from it->last_visible_y will end in the first
15994 line.) */
15995 if (w->cursor.vpos < 0)
15996 {
15997 if (w->window_end_valid && PT >= Z - XFASTINT (w->window_end_pos))
15998 {
15999 clear_glyph_matrix (w->desired_matrix);
16000 move_it_by_lines (&it, 1);
16001 try_window (window, it.current.pos, 0);
16002 }
16003 else if (PT < IT_CHARPOS (it))
16004 {
16005 clear_glyph_matrix (w->desired_matrix);
16006 move_it_by_lines (&it, -1);
16007 try_window (window, it.current.pos, 0);
16008 }
16009 else
16010 {
16011 /* Not much we can do about it. */
16012 }
16013 }
16014
16015 /* Consider the following case: Window starts at BEGV, there is
16016 invisible, intangible text at BEGV, so that display starts at
16017 some point START > BEGV. It can happen that we are called with
16018 PT somewhere between BEGV and START. Try to handle that case. */
16019 if (w->cursor.vpos < 0)
16020 {
16021 struct glyph_row *row = w->current_matrix->rows;
16022 if (row->mode_line_p)
16023 ++row;
16024 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16025 }
16026
16027 if (!cursor_row_fully_visible_p (w, 0, 0))
16028 {
16029 /* If vscroll is enabled, disable it and try again. */
16030 if (w->vscroll)
16031 {
16032 w->vscroll = 0;
16033 clear_glyph_matrix (w->desired_matrix);
16034 goto recenter;
16035 }
16036
16037 /* Users who set scroll-conservatively to a large number want
16038 point just above/below the scroll margin. If we ended up
16039 with point's row partially visible, move the window start to
16040 make that row fully visible and out of the margin. */
16041 if (scroll_conservatively > SCROLL_LIMIT)
16042 {
16043 int window_total_lines
16044 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) * frame_line_height;
16045 int margin =
16046 scroll_margin > 0
16047 ? min (scroll_margin, window_total_lines / 4)
16048 : 0;
16049 int move_down = w->cursor.vpos >= window_total_lines / 2;
16050
16051 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16052 clear_glyph_matrix (w->desired_matrix);
16053 if (1 == try_window (window, it.current.pos,
16054 TRY_WINDOW_CHECK_MARGINS))
16055 goto done;
16056 }
16057
16058 /* If centering point failed to make the whole line visible,
16059 put point at the top instead. That has to make the whole line
16060 visible, if it can be done. */
16061 if (centering_position == 0)
16062 goto done;
16063
16064 clear_glyph_matrix (w->desired_matrix);
16065 centering_position = 0;
16066 goto recenter;
16067 }
16068
16069 done:
16070
16071 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16072 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16073 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16074
16075 /* Display the mode line, if we must. */
16076 if ((update_mode_line
16077 /* If window not full width, must redo its mode line
16078 if (a) the window to its side is being redone and
16079 (b) we do a frame-based redisplay. This is a consequence
16080 of how inverted lines are drawn in frame-based redisplay. */
16081 || (!just_this_one_p
16082 && !FRAME_WINDOW_P (f)
16083 && !WINDOW_FULL_WIDTH_P (w))
16084 /* Line number to display. */
16085 || w->base_line_pos > 0
16086 /* Column number is displayed and different from the one displayed. */
16087 || (w->column_number_displayed != -1
16088 && (w->column_number_displayed != current_column ())))
16089 /* This means that the window has a mode line. */
16090 && (WINDOW_WANTS_MODELINE_P (w)
16091 || WINDOW_WANTS_HEADER_LINE_P (w)))
16092 {
16093 display_mode_lines (w);
16094
16095 /* If mode line height has changed, arrange for a thorough
16096 immediate redisplay using the correct mode line height. */
16097 if (WINDOW_WANTS_MODELINE_P (w)
16098 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16099 {
16100 fonts_changed_p = 1;
16101 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16102 = DESIRED_MODE_LINE_HEIGHT (w);
16103 }
16104
16105 /* If header line height has changed, arrange for a thorough
16106 immediate redisplay using the correct header line height. */
16107 if (WINDOW_WANTS_HEADER_LINE_P (w)
16108 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16109 {
16110 fonts_changed_p = 1;
16111 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16112 = DESIRED_HEADER_LINE_HEIGHT (w);
16113 }
16114
16115 if (fonts_changed_p)
16116 goto need_larger_matrices;
16117 }
16118
16119 if (!line_number_displayed && w->base_line_pos != -1)
16120 {
16121 w->base_line_pos = 0;
16122 w->base_line_number = 0;
16123 }
16124
16125 finish_menu_bars:
16126
16127 /* When we reach a frame's selected window, redo the frame's menu bar. */
16128 if (update_mode_line
16129 && EQ (FRAME_SELECTED_WINDOW (f), window))
16130 {
16131 int redisplay_menu_p = 0;
16132
16133 if (FRAME_WINDOW_P (f))
16134 {
16135 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16136 || defined (HAVE_NS) || defined (USE_GTK)
16137 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16138 #else
16139 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16140 #endif
16141 }
16142 else
16143 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16144
16145 if (redisplay_menu_p)
16146 display_menu_bar (w);
16147
16148 #ifdef HAVE_WINDOW_SYSTEM
16149 if (FRAME_WINDOW_P (f))
16150 {
16151 #if defined (USE_GTK) || defined (HAVE_NS)
16152 if (FRAME_EXTERNAL_TOOL_BAR (f))
16153 redisplay_tool_bar (f);
16154 #else
16155 if (WINDOWP (f->tool_bar_window)
16156 && (FRAME_TOOL_BAR_LINES (f) > 0
16157 || !NILP (Vauto_resize_tool_bars))
16158 && redisplay_tool_bar (f))
16159 ignore_mouse_drag_p = 1;
16160 #endif
16161 }
16162 #endif
16163 }
16164
16165 #ifdef HAVE_WINDOW_SYSTEM
16166 if (FRAME_WINDOW_P (f)
16167 && update_window_fringes (w, (just_this_one_p
16168 || (!used_current_matrix_p && !overlay_arrow_seen)
16169 || w->pseudo_window_p)))
16170 {
16171 update_begin (f);
16172 block_input ();
16173 if (draw_window_fringes (w, 1))
16174 x_draw_vertical_border (w);
16175 unblock_input ();
16176 update_end (f);
16177 }
16178 #endif /* HAVE_WINDOW_SYSTEM */
16179
16180 /* We go to this label, with fonts_changed_p set,
16181 if it is necessary to try again using larger glyph matrices.
16182 We have to redeem the scroll bar even in this case,
16183 because the loop in redisplay_internal expects that. */
16184 need_larger_matrices:
16185 ;
16186 finish_scroll_bars:
16187
16188 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16189 {
16190 /* Set the thumb's position and size. */
16191 set_vertical_scroll_bar (w);
16192
16193 /* Note that we actually used the scroll bar attached to this
16194 window, so it shouldn't be deleted at the end of redisplay. */
16195 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16196 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16197 }
16198
16199 /* Restore current_buffer and value of point in it. The window
16200 update may have changed the buffer, so first make sure `opoint'
16201 is still valid (Bug#6177). */
16202 if (CHARPOS (opoint) < BEGV)
16203 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16204 else if (CHARPOS (opoint) > ZV)
16205 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16206 else
16207 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16208
16209 set_buffer_internal_1 (old);
16210 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16211 shorter. This can be caused by log truncation in *Messages*. */
16212 if (CHARPOS (lpoint) <= ZV)
16213 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16214
16215 unbind_to (count, Qnil);
16216 }
16217
16218
16219 /* Build the complete desired matrix of WINDOW with a window start
16220 buffer position POS.
16221
16222 Value is 1 if successful. It is zero if fonts were loaded during
16223 redisplay which makes re-adjusting glyph matrices necessary, and -1
16224 if point would appear in the scroll margins.
16225 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16226 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16227 set in FLAGS.) */
16228
16229 int
16230 try_window (Lisp_Object window, struct text_pos pos, int flags)
16231 {
16232 struct window *w = XWINDOW (window);
16233 struct it it;
16234 struct glyph_row *last_text_row = NULL;
16235 struct frame *f = XFRAME (w->frame);
16236 int frame_line_height = default_line_pixel_height (w);
16237
16238 /* Make POS the new window start. */
16239 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16240
16241 /* Mark cursor position as unknown. No overlay arrow seen. */
16242 w->cursor.vpos = -1;
16243 overlay_arrow_seen = 0;
16244
16245 /* Initialize iterator and info to start at POS. */
16246 start_display (&it, w, pos);
16247
16248 /* Display all lines of W. */
16249 while (it.current_y < it.last_visible_y)
16250 {
16251 if (display_line (&it))
16252 last_text_row = it.glyph_row - 1;
16253 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16254 return 0;
16255 }
16256
16257 /* Don't let the cursor end in the scroll margins. */
16258 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16259 && !MINI_WINDOW_P (w))
16260 {
16261 int this_scroll_margin;
16262 int window_total_lines
16263 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
16264
16265 if (scroll_margin > 0)
16266 {
16267 this_scroll_margin = min (scroll_margin, window_total_lines / 4);
16268 this_scroll_margin *= frame_line_height;
16269 }
16270 else
16271 this_scroll_margin = 0;
16272
16273 if ((w->cursor.y >= 0 /* not vscrolled */
16274 && w->cursor.y < this_scroll_margin
16275 && CHARPOS (pos) > BEGV
16276 && IT_CHARPOS (it) < ZV)
16277 /* rms: considering make_cursor_line_fully_visible_p here
16278 seems to give wrong results. We don't want to recenter
16279 when the last line is partly visible, we want to allow
16280 that case to be handled in the usual way. */
16281 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16282 {
16283 w->cursor.vpos = -1;
16284 clear_glyph_matrix (w->desired_matrix);
16285 return -1;
16286 }
16287 }
16288
16289 /* If bottom moved off end of frame, change mode line percentage. */
16290 if (XFASTINT (w->window_end_pos) <= 0
16291 && Z != IT_CHARPOS (it))
16292 w->update_mode_line = 1;
16293
16294 /* Set window_end_pos to the offset of the last character displayed
16295 on the window from the end of current_buffer. Set
16296 window_end_vpos to its row number. */
16297 if (last_text_row)
16298 {
16299 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16300 w->window_end_bytepos
16301 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16302 wset_window_end_pos
16303 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16304 wset_window_end_vpos
16305 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16306 eassert
16307 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16308 XFASTINT (w->window_end_vpos))));
16309 }
16310 else
16311 {
16312 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16313 wset_window_end_pos (w, make_number (Z - ZV));
16314 wset_window_end_vpos (w, make_number (0));
16315 }
16316
16317 /* But that is not valid info until redisplay finishes. */
16318 w->window_end_valid = 0;
16319 return 1;
16320 }
16321
16322
16323 \f
16324 /************************************************************************
16325 Window redisplay reusing current matrix when buffer has not changed
16326 ************************************************************************/
16327
16328 /* Try redisplay of window W showing an unchanged buffer with a
16329 different window start than the last time it was displayed by
16330 reusing its current matrix. Value is non-zero if successful.
16331 W->start is the new window start. */
16332
16333 static int
16334 try_window_reusing_current_matrix (struct window *w)
16335 {
16336 struct frame *f = XFRAME (w->frame);
16337 struct glyph_row *bottom_row;
16338 struct it it;
16339 struct run run;
16340 struct text_pos start, new_start;
16341 int nrows_scrolled, i;
16342 struct glyph_row *last_text_row;
16343 struct glyph_row *last_reused_text_row;
16344 struct glyph_row *start_row;
16345 int start_vpos, min_y, max_y;
16346
16347 #ifdef GLYPH_DEBUG
16348 if (inhibit_try_window_reusing)
16349 return 0;
16350 #endif
16351
16352 if (/* This function doesn't handle terminal frames. */
16353 !FRAME_WINDOW_P (f)
16354 /* Don't try to reuse the display if windows have been split
16355 or such. */
16356 || windows_or_buffers_changed
16357 || cursor_type_changed)
16358 return 0;
16359
16360 /* Can't do this if region may have changed. */
16361 if (markpos_of_region () >= 0
16362 || w->region_showing
16363 || !NILP (Vshow_trailing_whitespace))
16364 return 0;
16365
16366 /* If top-line visibility has changed, give up. */
16367 if (WINDOW_WANTS_HEADER_LINE_P (w)
16368 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16369 return 0;
16370
16371 /* Give up if old or new display is scrolled vertically. We could
16372 make this function handle this, but right now it doesn't. */
16373 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16374 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16375 return 0;
16376
16377 /* The variable new_start now holds the new window start. The old
16378 start `start' can be determined from the current matrix. */
16379 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16380 start = start_row->minpos;
16381 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16382
16383 /* Clear the desired matrix for the display below. */
16384 clear_glyph_matrix (w->desired_matrix);
16385
16386 if (CHARPOS (new_start) <= CHARPOS (start))
16387 {
16388 /* Don't use this method if the display starts with an ellipsis
16389 displayed for invisible text. It's not easy to handle that case
16390 below, and it's certainly not worth the effort since this is
16391 not a frequent case. */
16392 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16393 return 0;
16394
16395 IF_DEBUG (debug_method_add (w, "twu1"));
16396
16397 /* Display up to a row that can be reused. The variable
16398 last_text_row is set to the last row displayed that displays
16399 text. Note that it.vpos == 0 if or if not there is a
16400 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16401 start_display (&it, w, new_start);
16402 w->cursor.vpos = -1;
16403 last_text_row = last_reused_text_row = NULL;
16404
16405 while (it.current_y < it.last_visible_y
16406 && !fonts_changed_p)
16407 {
16408 /* If we have reached into the characters in the START row,
16409 that means the line boundaries have changed. So we
16410 can't start copying with the row START. Maybe it will
16411 work to start copying with the following row. */
16412 while (IT_CHARPOS (it) > CHARPOS (start))
16413 {
16414 /* Advance to the next row as the "start". */
16415 start_row++;
16416 start = start_row->minpos;
16417 /* If there are no more rows to try, or just one, give up. */
16418 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16419 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16420 || CHARPOS (start) == ZV)
16421 {
16422 clear_glyph_matrix (w->desired_matrix);
16423 return 0;
16424 }
16425
16426 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16427 }
16428 /* If we have reached alignment, we can copy the rest of the
16429 rows. */
16430 if (IT_CHARPOS (it) == CHARPOS (start)
16431 /* Don't accept "alignment" inside a display vector,
16432 since start_row could have started in the middle of
16433 that same display vector (thus their character
16434 positions match), and we have no way of telling if
16435 that is the case. */
16436 && it.current.dpvec_index < 0)
16437 break;
16438
16439 if (display_line (&it))
16440 last_text_row = it.glyph_row - 1;
16441
16442 }
16443
16444 /* A value of current_y < last_visible_y means that we stopped
16445 at the previous window start, which in turn means that we
16446 have at least one reusable row. */
16447 if (it.current_y < it.last_visible_y)
16448 {
16449 struct glyph_row *row;
16450
16451 /* IT.vpos always starts from 0; it counts text lines. */
16452 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16453
16454 /* Find PT if not already found in the lines displayed. */
16455 if (w->cursor.vpos < 0)
16456 {
16457 int dy = it.current_y - start_row->y;
16458
16459 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16460 row = row_containing_pos (w, PT, row, NULL, dy);
16461 if (row)
16462 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16463 dy, nrows_scrolled);
16464 else
16465 {
16466 clear_glyph_matrix (w->desired_matrix);
16467 return 0;
16468 }
16469 }
16470
16471 /* Scroll the display. Do it before the current matrix is
16472 changed. The problem here is that update has not yet
16473 run, i.e. part of the current matrix is not up to date.
16474 scroll_run_hook will clear the cursor, and use the
16475 current matrix to get the height of the row the cursor is
16476 in. */
16477 run.current_y = start_row->y;
16478 run.desired_y = it.current_y;
16479 run.height = it.last_visible_y - it.current_y;
16480
16481 if (run.height > 0 && run.current_y != run.desired_y)
16482 {
16483 update_begin (f);
16484 FRAME_RIF (f)->update_window_begin_hook (w);
16485 FRAME_RIF (f)->clear_window_mouse_face (w);
16486 FRAME_RIF (f)->scroll_run_hook (w, &run);
16487 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16488 update_end (f);
16489 }
16490
16491 /* Shift current matrix down by nrows_scrolled lines. */
16492 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16493 rotate_matrix (w->current_matrix,
16494 start_vpos,
16495 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16496 nrows_scrolled);
16497
16498 /* Disable lines that must be updated. */
16499 for (i = 0; i < nrows_scrolled; ++i)
16500 (start_row + i)->enabled_p = 0;
16501
16502 /* Re-compute Y positions. */
16503 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16504 max_y = it.last_visible_y;
16505 for (row = start_row + nrows_scrolled;
16506 row < bottom_row;
16507 ++row)
16508 {
16509 row->y = it.current_y;
16510 row->visible_height = row->height;
16511
16512 if (row->y < min_y)
16513 row->visible_height -= min_y - row->y;
16514 if (row->y + row->height > max_y)
16515 row->visible_height -= row->y + row->height - max_y;
16516 if (row->fringe_bitmap_periodic_p)
16517 row->redraw_fringe_bitmaps_p = 1;
16518
16519 it.current_y += row->height;
16520
16521 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16522 last_reused_text_row = row;
16523 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16524 break;
16525 }
16526
16527 /* Disable lines in the current matrix which are now
16528 below the window. */
16529 for (++row; row < bottom_row; ++row)
16530 row->enabled_p = row->mode_line_p = 0;
16531 }
16532
16533 /* Update window_end_pos etc.; last_reused_text_row is the last
16534 reused row from the current matrix containing text, if any.
16535 The value of last_text_row is the last displayed line
16536 containing text. */
16537 if (last_reused_text_row)
16538 {
16539 w->window_end_bytepos
16540 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16541 wset_window_end_pos
16542 (w, make_number (Z
16543 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16544 wset_window_end_vpos
16545 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16546 w->current_matrix)));
16547 }
16548 else if (last_text_row)
16549 {
16550 w->window_end_bytepos
16551 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16552 wset_window_end_pos
16553 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16554 wset_window_end_vpos
16555 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16556 w->desired_matrix)));
16557 }
16558 else
16559 {
16560 /* This window must be completely empty. */
16561 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16562 wset_window_end_pos (w, make_number (Z - ZV));
16563 wset_window_end_vpos (w, make_number (0));
16564 }
16565 w->window_end_valid = 0;
16566
16567 /* Update hint: don't try scrolling again in update_window. */
16568 w->desired_matrix->no_scrolling_p = 1;
16569
16570 #ifdef GLYPH_DEBUG
16571 debug_method_add (w, "try_window_reusing_current_matrix 1");
16572 #endif
16573 return 1;
16574 }
16575 else if (CHARPOS (new_start) > CHARPOS (start))
16576 {
16577 struct glyph_row *pt_row, *row;
16578 struct glyph_row *first_reusable_row;
16579 struct glyph_row *first_row_to_display;
16580 int dy;
16581 int yb = window_text_bottom_y (w);
16582
16583 /* Find the row starting at new_start, if there is one. Don't
16584 reuse a partially visible line at the end. */
16585 first_reusable_row = start_row;
16586 while (first_reusable_row->enabled_p
16587 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16588 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16589 < CHARPOS (new_start)))
16590 ++first_reusable_row;
16591
16592 /* Give up if there is no row to reuse. */
16593 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16594 || !first_reusable_row->enabled_p
16595 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16596 != CHARPOS (new_start)))
16597 return 0;
16598
16599 /* We can reuse fully visible rows beginning with
16600 first_reusable_row to the end of the window. Set
16601 first_row_to_display to the first row that cannot be reused.
16602 Set pt_row to the row containing point, if there is any. */
16603 pt_row = NULL;
16604 for (first_row_to_display = first_reusable_row;
16605 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16606 ++first_row_to_display)
16607 {
16608 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16609 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16610 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16611 && first_row_to_display->ends_at_zv_p
16612 && pt_row == NULL)))
16613 pt_row = first_row_to_display;
16614 }
16615
16616 /* Start displaying at the start of first_row_to_display. */
16617 eassert (first_row_to_display->y < yb);
16618 init_to_row_start (&it, w, first_row_to_display);
16619
16620 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16621 - start_vpos);
16622 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16623 - nrows_scrolled);
16624 it.current_y = (first_row_to_display->y - first_reusable_row->y
16625 + WINDOW_HEADER_LINE_HEIGHT (w));
16626
16627 /* Display lines beginning with first_row_to_display in the
16628 desired matrix. Set last_text_row to the last row displayed
16629 that displays text. */
16630 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16631 if (pt_row == NULL)
16632 w->cursor.vpos = -1;
16633 last_text_row = NULL;
16634 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16635 if (display_line (&it))
16636 last_text_row = it.glyph_row - 1;
16637
16638 /* If point is in a reused row, adjust y and vpos of the cursor
16639 position. */
16640 if (pt_row)
16641 {
16642 w->cursor.vpos -= nrows_scrolled;
16643 w->cursor.y -= first_reusable_row->y - start_row->y;
16644 }
16645
16646 /* Give up if point isn't in a row displayed or reused. (This
16647 also handles the case where w->cursor.vpos < nrows_scrolled
16648 after the calls to display_line, which can happen with scroll
16649 margins. See bug#1295.) */
16650 if (w->cursor.vpos < 0)
16651 {
16652 clear_glyph_matrix (w->desired_matrix);
16653 return 0;
16654 }
16655
16656 /* Scroll the display. */
16657 run.current_y = first_reusable_row->y;
16658 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16659 run.height = it.last_visible_y - run.current_y;
16660 dy = run.current_y - run.desired_y;
16661
16662 if (run.height)
16663 {
16664 update_begin (f);
16665 FRAME_RIF (f)->update_window_begin_hook (w);
16666 FRAME_RIF (f)->clear_window_mouse_face (w);
16667 FRAME_RIF (f)->scroll_run_hook (w, &run);
16668 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16669 update_end (f);
16670 }
16671
16672 /* Adjust Y positions of reused rows. */
16673 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16674 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16675 max_y = it.last_visible_y;
16676 for (row = first_reusable_row; row < first_row_to_display; ++row)
16677 {
16678 row->y -= dy;
16679 row->visible_height = row->height;
16680 if (row->y < min_y)
16681 row->visible_height -= min_y - row->y;
16682 if (row->y + row->height > max_y)
16683 row->visible_height -= row->y + row->height - max_y;
16684 if (row->fringe_bitmap_periodic_p)
16685 row->redraw_fringe_bitmaps_p = 1;
16686 }
16687
16688 /* Scroll the current matrix. */
16689 eassert (nrows_scrolled > 0);
16690 rotate_matrix (w->current_matrix,
16691 start_vpos,
16692 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16693 -nrows_scrolled);
16694
16695 /* Disable rows not reused. */
16696 for (row -= nrows_scrolled; row < bottom_row; ++row)
16697 row->enabled_p = 0;
16698
16699 /* Point may have moved to a different line, so we cannot assume that
16700 the previous cursor position is valid; locate the correct row. */
16701 if (pt_row)
16702 {
16703 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16704 row < bottom_row
16705 && PT >= MATRIX_ROW_END_CHARPOS (row)
16706 && !row->ends_at_zv_p;
16707 row++)
16708 {
16709 w->cursor.vpos++;
16710 w->cursor.y = row->y;
16711 }
16712 if (row < bottom_row)
16713 {
16714 /* Can't simply scan the row for point with
16715 bidi-reordered glyph rows. Let set_cursor_from_row
16716 figure out where to put the cursor, and if it fails,
16717 give up. */
16718 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16719 {
16720 if (!set_cursor_from_row (w, row, w->current_matrix,
16721 0, 0, 0, 0))
16722 {
16723 clear_glyph_matrix (w->desired_matrix);
16724 return 0;
16725 }
16726 }
16727 else
16728 {
16729 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16730 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16731
16732 for (; glyph < end
16733 && (!BUFFERP (glyph->object)
16734 || glyph->charpos < PT);
16735 glyph++)
16736 {
16737 w->cursor.hpos++;
16738 w->cursor.x += glyph->pixel_width;
16739 }
16740 }
16741 }
16742 }
16743
16744 /* Adjust window end. A null value of last_text_row means that
16745 the window end is in reused rows which in turn means that
16746 only its vpos can have changed. */
16747 if (last_text_row)
16748 {
16749 w->window_end_bytepos
16750 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16751 wset_window_end_pos
16752 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16753 wset_window_end_vpos
16754 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16755 w->desired_matrix)));
16756 }
16757 else
16758 {
16759 wset_window_end_vpos
16760 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16761 }
16762
16763 w->window_end_valid = 0;
16764 w->desired_matrix->no_scrolling_p = 1;
16765
16766 #ifdef GLYPH_DEBUG
16767 debug_method_add (w, "try_window_reusing_current_matrix 2");
16768 #endif
16769 return 1;
16770 }
16771
16772 return 0;
16773 }
16774
16775
16776 \f
16777 /************************************************************************
16778 Window redisplay reusing current matrix when buffer has changed
16779 ************************************************************************/
16780
16781 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16782 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16783 ptrdiff_t *, ptrdiff_t *);
16784 static struct glyph_row *
16785 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16786 struct glyph_row *);
16787
16788
16789 /* Return the last row in MATRIX displaying text. If row START is
16790 non-null, start searching with that row. IT gives the dimensions
16791 of the display. Value is null if matrix is empty; otherwise it is
16792 a pointer to the row found. */
16793
16794 static struct glyph_row *
16795 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16796 struct glyph_row *start)
16797 {
16798 struct glyph_row *row, *row_found;
16799
16800 /* Set row_found to the last row in IT->w's current matrix
16801 displaying text. The loop looks funny but think of partially
16802 visible lines. */
16803 row_found = NULL;
16804 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16805 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16806 {
16807 eassert (row->enabled_p);
16808 row_found = row;
16809 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16810 break;
16811 ++row;
16812 }
16813
16814 return row_found;
16815 }
16816
16817
16818 /* Return the last row in the current matrix of W that is not affected
16819 by changes at the start of current_buffer that occurred since W's
16820 current matrix was built. Value is null if no such row exists.
16821
16822 BEG_UNCHANGED us the number of characters unchanged at the start of
16823 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16824 first changed character in current_buffer. Characters at positions <
16825 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16826 when the current matrix was built. */
16827
16828 static struct glyph_row *
16829 find_last_unchanged_at_beg_row (struct window *w)
16830 {
16831 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16832 struct glyph_row *row;
16833 struct glyph_row *row_found = NULL;
16834 int yb = window_text_bottom_y (w);
16835
16836 /* Find the last row displaying unchanged text. */
16837 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16838 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16839 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16840 ++row)
16841 {
16842 if (/* If row ends before first_changed_pos, it is unchanged,
16843 except in some case. */
16844 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16845 /* When row ends in ZV and we write at ZV it is not
16846 unchanged. */
16847 && !row->ends_at_zv_p
16848 /* When first_changed_pos is the end of a continued line,
16849 row is not unchanged because it may be no longer
16850 continued. */
16851 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16852 && (row->continued_p
16853 || row->exact_window_width_line_p))
16854 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16855 needs to be recomputed, so don't consider this row as
16856 unchanged. This happens when the last line was
16857 bidi-reordered and was killed immediately before this
16858 redisplay cycle. In that case, ROW->end stores the
16859 buffer position of the first visual-order character of
16860 the killed text, which is now beyond ZV. */
16861 && CHARPOS (row->end.pos) <= ZV)
16862 row_found = row;
16863
16864 /* Stop if last visible row. */
16865 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16866 break;
16867 }
16868
16869 return row_found;
16870 }
16871
16872
16873 /* Find the first glyph row in the current matrix of W that is not
16874 affected by changes at the end of current_buffer since the
16875 time W's current matrix was built.
16876
16877 Return in *DELTA the number of chars by which buffer positions in
16878 unchanged text at the end of current_buffer must be adjusted.
16879
16880 Return in *DELTA_BYTES the corresponding number of bytes.
16881
16882 Value is null if no such row exists, i.e. all rows are affected by
16883 changes. */
16884
16885 static struct glyph_row *
16886 find_first_unchanged_at_end_row (struct window *w,
16887 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16888 {
16889 struct glyph_row *row;
16890 struct glyph_row *row_found = NULL;
16891
16892 *delta = *delta_bytes = 0;
16893
16894 /* Display must not have been paused, otherwise the current matrix
16895 is not up to date. */
16896 eassert (w->window_end_valid);
16897
16898 /* A value of window_end_pos >= END_UNCHANGED means that the window
16899 end is in the range of changed text. If so, there is no
16900 unchanged row at the end of W's current matrix. */
16901 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16902 return NULL;
16903
16904 /* Set row to the last row in W's current matrix displaying text. */
16905 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16906
16907 /* If matrix is entirely empty, no unchanged row exists. */
16908 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16909 {
16910 /* The value of row is the last glyph row in the matrix having a
16911 meaningful buffer position in it. The end position of row
16912 corresponds to window_end_pos. This allows us to translate
16913 buffer positions in the current matrix to current buffer
16914 positions for characters not in changed text. */
16915 ptrdiff_t Z_old =
16916 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16917 ptrdiff_t Z_BYTE_old =
16918 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16919 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16920 struct glyph_row *first_text_row
16921 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16922
16923 *delta = Z - Z_old;
16924 *delta_bytes = Z_BYTE - Z_BYTE_old;
16925
16926 /* Set last_unchanged_pos to the buffer position of the last
16927 character in the buffer that has not been changed. Z is the
16928 index + 1 of the last character in current_buffer, i.e. by
16929 subtracting END_UNCHANGED we get the index of the last
16930 unchanged character, and we have to add BEG to get its buffer
16931 position. */
16932 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16933 last_unchanged_pos_old = last_unchanged_pos - *delta;
16934
16935 /* Search backward from ROW for a row displaying a line that
16936 starts at a minimum position >= last_unchanged_pos_old. */
16937 for (; row > first_text_row; --row)
16938 {
16939 /* This used to abort, but it can happen.
16940 It is ok to just stop the search instead here. KFS. */
16941 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16942 break;
16943
16944 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16945 row_found = row;
16946 }
16947 }
16948
16949 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16950
16951 return row_found;
16952 }
16953
16954
16955 /* Make sure that glyph rows in the current matrix of window W
16956 reference the same glyph memory as corresponding rows in the
16957 frame's frame matrix. This function is called after scrolling W's
16958 current matrix on a terminal frame in try_window_id and
16959 try_window_reusing_current_matrix. */
16960
16961 static void
16962 sync_frame_with_window_matrix_rows (struct window *w)
16963 {
16964 struct frame *f = XFRAME (w->frame);
16965 struct glyph_row *window_row, *window_row_end, *frame_row;
16966
16967 /* Preconditions: W must be a leaf window and full-width. Its frame
16968 must have a frame matrix. */
16969 eassert (BUFFERP (w->contents));
16970 eassert (WINDOW_FULL_WIDTH_P (w));
16971 eassert (!FRAME_WINDOW_P (f));
16972
16973 /* If W is a full-width window, glyph pointers in W's current matrix
16974 have, by definition, to be the same as glyph pointers in the
16975 corresponding frame matrix. Note that frame matrices have no
16976 marginal areas (see build_frame_matrix). */
16977 window_row = w->current_matrix->rows;
16978 window_row_end = window_row + w->current_matrix->nrows;
16979 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16980 while (window_row < window_row_end)
16981 {
16982 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16983 struct glyph *end = window_row->glyphs[LAST_AREA];
16984
16985 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16986 frame_row->glyphs[TEXT_AREA] = start;
16987 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16988 frame_row->glyphs[LAST_AREA] = end;
16989
16990 /* Disable frame rows whose corresponding window rows have
16991 been disabled in try_window_id. */
16992 if (!window_row->enabled_p)
16993 frame_row->enabled_p = 0;
16994
16995 ++window_row, ++frame_row;
16996 }
16997 }
16998
16999
17000 /* Find the glyph row in window W containing CHARPOS. Consider all
17001 rows between START and END (not inclusive). END null means search
17002 all rows to the end of the display area of W. Value is the row
17003 containing CHARPOS or null. */
17004
17005 struct glyph_row *
17006 row_containing_pos (struct window *w, ptrdiff_t charpos,
17007 struct glyph_row *start, struct glyph_row *end, int dy)
17008 {
17009 struct glyph_row *row = start;
17010 struct glyph_row *best_row = NULL;
17011 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
17012 int last_y;
17013
17014 /* If we happen to start on a header-line, skip that. */
17015 if (row->mode_line_p)
17016 ++row;
17017
17018 if ((end && row >= end) || !row->enabled_p)
17019 return NULL;
17020
17021 last_y = window_text_bottom_y (w) - dy;
17022
17023 while (1)
17024 {
17025 /* Give up if we have gone too far. */
17026 if (end && row >= end)
17027 return NULL;
17028 /* This formerly returned if they were equal.
17029 I think that both quantities are of a "last plus one" type;
17030 if so, when they are equal, the row is within the screen. -- rms. */
17031 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17032 return NULL;
17033
17034 /* If it is in this row, return this row. */
17035 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17036 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17037 /* The end position of a row equals the start
17038 position of the next row. If CHARPOS is there, we
17039 would rather consider it displayed in the next
17040 line, except when this line ends in ZV. */
17041 && !row_for_charpos_p (row, charpos)))
17042 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17043 {
17044 struct glyph *g;
17045
17046 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17047 || (!best_row && !row->continued_p))
17048 return row;
17049 /* In bidi-reordered rows, there could be several rows whose
17050 edges surround CHARPOS, all of these rows belonging to
17051 the same continued line. We need to find the row which
17052 fits CHARPOS the best. */
17053 for (g = row->glyphs[TEXT_AREA];
17054 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17055 g++)
17056 {
17057 if (!STRINGP (g->object))
17058 {
17059 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17060 {
17061 mindif = eabs (g->charpos - charpos);
17062 best_row = row;
17063 /* Exact match always wins. */
17064 if (mindif == 0)
17065 return best_row;
17066 }
17067 }
17068 }
17069 }
17070 else if (best_row && !row->continued_p)
17071 return best_row;
17072 ++row;
17073 }
17074 }
17075
17076
17077 /* Try to redisplay window W by reusing its existing display. W's
17078 current matrix must be up to date when this function is called,
17079 i.e. window_end_valid must be nonzero.
17080
17081 Value is
17082
17083 1 if display has been updated
17084 0 if otherwise unsuccessful
17085 -1 if redisplay with same window start is known not to succeed
17086
17087 The following steps are performed:
17088
17089 1. Find the last row in the current matrix of W that is not
17090 affected by changes at the start of current_buffer. If no such row
17091 is found, give up.
17092
17093 2. Find the first row in W's current matrix that is not affected by
17094 changes at the end of current_buffer. Maybe there is no such row.
17095
17096 3. Display lines beginning with the row + 1 found in step 1 to the
17097 row found in step 2 or, if step 2 didn't find a row, to the end of
17098 the window.
17099
17100 4. If cursor is not known to appear on the window, give up.
17101
17102 5. If display stopped at the row found in step 2, scroll the
17103 display and current matrix as needed.
17104
17105 6. Maybe display some lines at the end of W, if we must. This can
17106 happen under various circumstances, like a partially visible line
17107 becoming fully visible, or because newly displayed lines are displayed
17108 in smaller font sizes.
17109
17110 7. Update W's window end information. */
17111
17112 static int
17113 try_window_id (struct window *w)
17114 {
17115 struct frame *f = XFRAME (w->frame);
17116 struct glyph_matrix *current_matrix = w->current_matrix;
17117 struct glyph_matrix *desired_matrix = w->desired_matrix;
17118 struct glyph_row *last_unchanged_at_beg_row;
17119 struct glyph_row *first_unchanged_at_end_row;
17120 struct glyph_row *row;
17121 struct glyph_row *bottom_row;
17122 int bottom_vpos;
17123 struct it it;
17124 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17125 int dvpos, dy;
17126 struct text_pos start_pos;
17127 struct run run;
17128 int first_unchanged_at_end_vpos = 0;
17129 struct glyph_row *last_text_row, *last_text_row_at_end;
17130 struct text_pos start;
17131 ptrdiff_t first_changed_charpos, last_changed_charpos;
17132
17133 #ifdef GLYPH_DEBUG
17134 if (inhibit_try_window_id)
17135 return 0;
17136 #endif
17137
17138 /* This is handy for debugging. */
17139 #if 0
17140 #define GIVE_UP(X) \
17141 do { \
17142 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17143 return 0; \
17144 } while (0)
17145 #else
17146 #define GIVE_UP(X) return 0
17147 #endif
17148
17149 SET_TEXT_POS_FROM_MARKER (start, w->start);
17150
17151 /* Don't use this for mini-windows because these can show
17152 messages and mini-buffers, and we don't handle that here. */
17153 if (MINI_WINDOW_P (w))
17154 GIVE_UP (1);
17155
17156 /* This flag is used to prevent redisplay optimizations. */
17157 if (windows_or_buffers_changed || cursor_type_changed)
17158 GIVE_UP (2);
17159
17160 /* Verify that narrowing has not changed.
17161 Also verify that we were not told to prevent redisplay optimizations.
17162 It would be nice to further
17163 reduce the number of cases where this prevents try_window_id. */
17164 if (current_buffer->clip_changed
17165 || current_buffer->prevent_redisplay_optimizations_p)
17166 GIVE_UP (3);
17167
17168 /* Window must either use window-based redisplay or be full width. */
17169 if (!FRAME_WINDOW_P (f)
17170 && (!FRAME_LINE_INS_DEL_OK (f)
17171 || !WINDOW_FULL_WIDTH_P (w)))
17172 GIVE_UP (4);
17173
17174 /* Give up if point is known NOT to appear in W. */
17175 if (PT < CHARPOS (start))
17176 GIVE_UP (5);
17177
17178 /* Another way to prevent redisplay optimizations. */
17179 if (w->last_modified == 0)
17180 GIVE_UP (6);
17181
17182 /* Verify that window is not hscrolled. */
17183 if (w->hscroll != 0)
17184 GIVE_UP (7);
17185
17186 /* Verify that display wasn't paused. */
17187 if (!w->window_end_valid)
17188 GIVE_UP (8);
17189
17190 /* Can't use this if highlighting a region because a cursor movement
17191 will do more than just set the cursor. */
17192 if (markpos_of_region () >= 0)
17193 GIVE_UP (9);
17194
17195 /* Likewise if highlighting trailing whitespace. */
17196 if (!NILP (Vshow_trailing_whitespace))
17197 GIVE_UP (11);
17198
17199 /* Likewise if showing a region. */
17200 if (w->region_showing)
17201 GIVE_UP (10);
17202
17203 /* Can't use this if overlay arrow position and/or string have
17204 changed. */
17205 if (overlay_arrows_changed_p ())
17206 GIVE_UP (12);
17207
17208 /* When word-wrap is on, adding a space to the first word of a
17209 wrapped line can change the wrap position, altering the line
17210 above it. It might be worthwhile to handle this more
17211 intelligently, but for now just redisplay from scratch. */
17212 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17213 GIVE_UP (21);
17214
17215 /* Under bidi reordering, adding or deleting a character in the
17216 beginning of a paragraph, before the first strong directional
17217 character, can change the base direction of the paragraph (unless
17218 the buffer specifies a fixed paragraph direction), which will
17219 require to redisplay the whole paragraph. It might be worthwhile
17220 to find the paragraph limits and widen the range of redisplayed
17221 lines to that, but for now just give up this optimization and
17222 redisplay from scratch. */
17223 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17224 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17225 GIVE_UP (22);
17226
17227 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17228 only if buffer has really changed. The reason is that the gap is
17229 initially at Z for freshly visited files. The code below would
17230 set end_unchanged to 0 in that case. */
17231 if (MODIFF > SAVE_MODIFF
17232 /* This seems to happen sometimes after saving a buffer. */
17233 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17234 {
17235 if (GPT - BEG < BEG_UNCHANGED)
17236 BEG_UNCHANGED = GPT - BEG;
17237 if (Z - GPT < END_UNCHANGED)
17238 END_UNCHANGED = Z - GPT;
17239 }
17240
17241 /* The position of the first and last character that has been changed. */
17242 first_changed_charpos = BEG + BEG_UNCHANGED;
17243 last_changed_charpos = Z - END_UNCHANGED;
17244
17245 /* If window starts after a line end, and the last change is in
17246 front of that newline, then changes don't affect the display.
17247 This case happens with stealth-fontification. Note that although
17248 the display is unchanged, glyph positions in the matrix have to
17249 be adjusted, of course. */
17250 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17251 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17252 && ((last_changed_charpos < CHARPOS (start)
17253 && CHARPOS (start) == BEGV)
17254 || (last_changed_charpos < CHARPOS (start) - 1
17255 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17256 {
17257 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17258 struct glyph_row *r0;
17259
17260 /* Compute how many chars/bytes have been added to or removed
17261 from the buffer. */
17262 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17263 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17264 Z_delta = Z - Z_old;
17265 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17266
17267 /* Give up if PT is not in the window. Note that it already has
17268 been checked at the start of try_window_id that PT is not in
17269 front of the window start. */
17270 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17271 GIVE_UP (13);
17272
17273 /* If window start is unchanged, we can reuse the whole matrix
17274 as is, after adjusting glyph positions. No need to compute
17275 the window end again, since its offset from Z hasn't changed. */
17276 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17277 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17278 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17279 /* PT must not be in a partially visible line. */
17280 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17281 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17282 {
17283 /* Adjust positions in the glyph matrix. */
17284 if (Z_delta || Z_delta_bytes)
17285 {
17286 struct glyph_row *r1
17287 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17288 increment_matrix_positions (w->current_matrix,
17289 MATRIX_ROW_VPOS (r0, current_matrix),
17290 MATRIX_ROW_VPOS (r1, current_matrix),
17291 Z_delta, Z_delta_bytes);
17292 }
17293
17294 /* Set the cursor. */
17295 row = row_containing_pos (w, PT, r0, NULL, 0);
17296 if (row)
17297 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17298 else
17299 emacs_abort ();
17300 return 1;
17301 }
17302 }
17303
17304 /* Handle the case that changes are all below what is displayed in
17305 the window, and that PT is in the window. This shortcut cannot
17306 be taken if ZV is visible in the window, and text has been added
17307 there that is visible in the window. */
17308 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17309 /* ZV is not visible in the window, or there are no
17310 changes at ZV, actually. */
17311 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17312 || first_changed_charpos == last_changed_charpos))
17313 {
17314 struct glyph_row *r0;
17315
17316 /* Give up if PT is not in the window. Note that it already has
17317 been checked at the start of try_window_id that PT is not in
17318 front of the window start. */
17319 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17320 GIVE_UP (14);
17321
17322 /* If window start is unchanged, we can reuse the whole matrix
17323 as is, without changing glyph positions since no text has
17324 been added/removed in front of the window end. */
17325 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17326 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17327 /* PT must not be in a partially visible line. */
17328 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17329 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17330 {
17331 /* We have to compute the window end anew since text
17332 could have been added/removed after it. */
17333 wset_window_end_pos
17334 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17335 w->window_end_bytepos
17336 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17337
17338 /* Set the cursor. */
17339 row = row_containing_pos (w, PT, r0, NULL, 0);
17340 if (row)
17341 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17342 else
17343 emacs_abort ();
17344 return 2;
17345 }
17346 }
17347
17348 /* Give up if window start is in the changed area.
17349
17350 The condition used to read
17351
17352 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17353
17354 but why that was tested escapes me at the moment. */
17355 if (CHARPOS (start) >= first_changed_charpos
17356 && CHARPOS (start) <= last_changed_charpos)
17357 GIVE_UP (15);
17358
17359 /* Check that window start agrees with the start of the first glyph
17360 row in its current matrix. Check this after we know the window
17361 start is not in changed text, otherwise positions would not be
17362 comparable. */
17363 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17364 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17365 GIVE_UP (16);
17366
17367 /* Give up if the window ends in strings. Overlay strings
17368 at the end are difficult to handle, so don't try. */
17369 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17370 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17371 GIVE_UP (20);
17372
17373 /* Compute the position at which we have to start displaying new
17374 lines. Some of the lines at the top of the window might be
17375 reusable because they are not displaying changed text. Find the
17376 last row in W's current matrix not affected by changes at the
17377 start of current_buffer. Value is null if changes start in the
17378 first line of window. */
17379 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17380 if (last_unchanged_at_beg_row)
17381 {
17382 /* Avoid starting to display in the middle of a character, a TAB
17383 for instance. This is easier than to set up the iterator
17384 exactly, and it's not a frequent case, so the additional
17385 effort wouldn't really pay off. */
17386 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17387 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17388 && last_unchanged_at_beg_row > w->current_matrix->rows)
17389 --last_unchanged_at_beg_row;
17390
17391 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17392 GIVE_UP (17);
17393
17394 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17395 GIVE_UP (18);
17396 start_pos = it.current.pos;
17397
17398 /* Start displaying new lines in the desired matrix at the same
17399 vpos we would use in the current matrix, i.e. below
17400 last_unchanged_at_beg_row. */
17401 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17402 current_matrix);
17403 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17404 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17405
17406 eassert (it.hpos == 0 && it.current_x == 0);
17407 }
17408 else
17409 {
17410 /* There are no reusable lines at the start of the window.
17411 Start displaying in the first text line. */
17412 start_display (&it, w, start);
17413 it.vpos = it.first_vpos;
17414 start_pos = it.current.pos;
17415 }
17416
17417 /* Find the first row that is not affected by changes at the end of
17418 the buffer. Value will be null if there is no unchanged row, in
17419 which case we must redisplay to the end of the window. delta
17420 will be set to the value by which buffer positions beginning with
17421 first_unchanged_at_end_row have to be adjusted due to text
17422 changes. */
17423 first_unchanged_at_end_row
17424 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17425 IF_DEBUG (debug_delta = delta);
17426 IF_DEBUG (debug_delta_bytes = delta_bytes);
17427
17428 /* Set stop_pos to the buffer position up to which we will have to
17429 display new lines. If first_unchanged_at_end_row != NULL, this
17430 is the buffer position of the start of the line displayed in that
17431 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17432 that we don't stop at a buffer position. */
17433 stop_pos = 0;
17434 if (first_unchanged_at_end_row)
17435 {
17436 eassert (last_unchanged_at_beg_row == NULL
17437 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17438
17439 /* If this is a continuation line, move forward to the next one
17440 that isn't. Changes in lines above affect this line.
17441 Caution: this may move first_unchanged_at_end_row to a row
17442 not displaying text. */
17443 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17444 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17445 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17446 < it.last_visible_y))
17447 ++first_unchanged_at_end_row;
17448
17449 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17450 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17451 >= it.last_visible_y))
17452 first_unchanged_at_end_row = NULL;
17453 else
17454 {
17455 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17456 + delta);
17457 first_unchanged_at_end_vpos
17458 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17459 eassert (stop_pos >= Z - END_UNCHANGED);
17460 }
17461 }
17462 else if (last_unchanged_at_beg_row == NULL)
17463 GIVE_UP (19);
17464
17465
17466 #ifdef GLYPH_DEBUG
17467
17468 /* Either there is no unchanged row at the end, or the one we have
17469 now displays text. This is a necessary condition for the window
17470 end pos calculation at the end of this function. */
17471 eassert (first_unchanged_at_end_row == NULL
17472 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17473
17474 debug_last_unchanged_at_beg_vpos
17475 = (last_unchanged_at_beg_row
17476 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17477 : -1);
17478 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17479
17480 #endif /* GLYPH_DEBUG */
17481
17482
17483 /* Display new lines. Set last_text_row to the last new line
17484 displayed which has text on it, i.e. might end up as being the
17485 line where the window_end_vpos is. */
17486 w->cursor.vpos = -1;
17487 last_text_row = NULL;
17488 overlay_arrow_seen = 0;
17489 while (it.current_y < it.last_visible_y
17490 && !fonts_changed_p
17491 && (first_unchanged_at_end_row == NULL
17492 || IT_CHARPOS (it) < stop_pos))
17493 {
17494 if (display_line (&it))
17495 last_text_row = it.glyph_row - 1;
17496 }
17497
17498 if (fonts_changed_p)
17499 return -1;
17500
17501
17502 /* Compute differences in buffer positions, y-positions etc. for
17503 lines reused at the bottom of the window. Compute what we can
17504 scroll. */
17505 if (first_unchanged_at_end_row
17506 /* No lines reused because we displayed everything up to the
17507 bottom of the window. */
17508 && it.current_y < it.last_visible_y)
17509 {
17510 dvpos = (it.vpos
17511 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17512 current_matrix));
17513 dy = it.current_y - first_unchanged_at_end_row->y;
17514 run.current_y = first_unchanged_at_end_row->y;
17515 run.desired_y = run.current_y + dy;
17516 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17517 }
17518 else
17519 {
17520 delta = delta_bytes = dvpos = dy
17521 = run.current_y = run.desired_y = run.height = 0;
17522 first_unchanged_at_end_row = NULL;
17523 }
17524 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17525
17526
17527 /* Find the cursor if not already found. We have to decide whether
17528 PT will appear on this window (it sometimes doesn't, but this is
17529 not a very frequent case.) This decision has to be made before
17530 the current matrix is altered. A value of cursor.vpos < 0 means
17531 that PT is either in one of the lines beginning at
17532 first_unchanged_at_end_row or below the window. Don't care for
17533 lines that might be displayed later at the window end; as
17534 mentioned, this is not a frequent case. */
17535 if (w->cursor.vpos < 0)
17536 {
17537 /* Cursor in unchanged rows at the top? */
17538 if (PT < CHARPOS (start_pos)
17539 && last_unchanged_at_beg_row)
17540 {
17541 row = row_containing_pos (w, PT,
17542 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17543 last_unchanged_at_beg_row + 1, 0);
17544 if (row)
17545 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17546 }
17547
17548 /* Start from first_unchanged_at_end_row looking for PT. */
17549 else if (first_unchanged_at_end_row)
17550 {
17551 row = row_containing_pos (w, PT - delta,
17552 first_unchanged_at_end_row, NULL, 0);
17553 if (row)
17554 set_cursor_from_row (w, row, w->current_matrix, delta,
17555 delta_bytes, dy, dvpos);
17556 }
17557
17558 /* Give up if cursor was not found. */
17559 if (w->cursor.vpos < 0)
17560 {
17561 clear_glyph_matrix (w->desired_matrix);
17562 return -1;
17563 }
17564 }
17565
17566 /* Don't let the cursor end in the scroll margins. */
17567 {
17568 int this_scroll_margin, cursor_height;
17569 int frame_line_height = default_line_pixel_height (w);
17570 int window_total_lines
17571 = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (it.f) / frame_line_height;
17572
17573 this_scroll_margin =
17574 max (0, min (scroll_margin, window_total_lines / 4));
17575 this_scroll_margin *= frame_line_height;
17576 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17577
17578 if ((w->cursor.y < this_scroll_margin
17579 && CHARPOS (start) > BEGV)
17580 /* Old redisplay didn't take scroll margin into account at the bottom,
17581 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17582 || (w->cursor.y + (make_cursor_line_fully_visible_p
17583 ? cursor_height + this_scroll_margin
17584 : 1)) > it.last_visible_y)
17585 {
17586 w->cursor.vpos = -1;
17587 clear_glyph_matrix (w->desired_matrix);
17588 return -1;
17589 }
17590 }
17591
17592 /* Scroll the display. Do it before changing the current matrix so
17593 that xterm.c doesn't get confused about where the cursor glyph is
17594 found. */
17595 if (dy && run.height)
17596 {
17597 update_begin (f);
17598
17599 if (FRAME_WINDOW_P (f))
17600 {
17601 FRAME_RIF (f)->update_window_begin_hook (w);
17602 FRAME_RIF (f)->clear_window_mouse_face (w);
17603 FRAME_RIF (f)->scroll_run_hook (w, &run);
17604 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17605 }
17606 else
17607 {
17608 /* Terminal frame. In this case, dvpos gives the number of
17609 lines to scroll by; dvpos < 0 means scroll up. */
17610 int from_vpos
17611 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17612 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17613 int end = (WINDOW_TOP_EDGE_LINE (w)
17614 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17615 + window_internal_height (w));
17616
17617 #if defined (HAVE_GPM) || defined (MSDOS)
17618 x_clear_window_mouse_face (w);
17619 #endif
17620 /* Perform the operation on the screen. */
17621 if (dvpos > 0)
17622 {
17623 /* Scroll last_unchanged_at_beg_row to the end of the
17624 window down dvpos lines. */
17625 set_terminal_window (f, end);
17626
17627 /* On dumb terminals delete dvpos lines at the end
17628 before inserting dvpos empty lines. */
17629 if (!FRAME_SCROLL_REGION_OK (f))
17630 ins_del_lines (f, end - dvpos, -dvpos);
17631
17632 /* Insert dvpos empty lines in front of
17633 last_unchanged_at_beg_row. */
17634 ins_del_lines (f, from, dvpos);
17635 }
17636 else if (dvpos < 0)
17637 {
17638 /* Scroll up last_unchanged_at_beg_vpos to the end of
17639 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17640 set_terminal_window (f, end);
17641
17642 /* Delete dvpos lines in front of
17643 last_unchanged_at_beg_vpos. ins_del_lines will set
17644 the cursor to the given vpos and emit |dvpos| delete
17645 line sequences. */
17646 ins_del_lines (f, from + dvpos, dvpos);
17647
17648 /* On a dumb terminal insert dvpos empty lines at the
17649 end. */
17650 if (!FRAME_SCROLL_REGION_OK (f))
17651 ins_del_lines (f, end + dvpos, -dvpos);
17652 }
17653
17654 set_terminal_window (f, 0);
17655 }
17656
17657 update_end (f);
17658 }
17659
17660 /* Shift reused rows of the current matrix to the right position.
17661 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17662 text. */
17663 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17664 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17665 if (dvpos < 0)
17666 {
17667 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17668 bottom_vpos, dvpos);
17669 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17670 bottom_vpos);
17671 }
17672 else if (dvpos > 0)
17673 {
17674 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17675 bottom_vpos, dvpos);
17676 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17677 first_unchanged_at_end_vpos + dvpos);
17678 }
17679
17680 /* For frame-based redisplay, make sure that current frame and window
17681 matrix are in sync with respect to glyph memory. */
17682 if (!FRAME_WINDOW_P (f))
17683 sync_frame_with_window_matrix_rows (w);
17684
17685 /* Adjust buffer positions in reused rows. */
17686 if (delta || delta_bytes)
17687 increment_matrix_positions (current_matrix,
17688 first_unchanged_at_end_vpos + dvpos,
17689 bottom_vpos, delta, delta_bytes);
17690
17691 /* Adjust Y positions. */
17692 if (dy)
17693 shift_glyph_matrix (w, current_matrix,
17694 first_unchanged_at_end_vpos + dvpos,
17695 bottom_vpos, dy);
17696
17697 if (first_unchanged_at_end_row)
17698 {
17699 first_unchanged_at_end_row += dvpos;
17700 if (first_unchanged_at_end_row->y >= it.last_visible_y
17701 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17702 first_unchanged_at_end_row = NULL;
17703 }
17704
17705 /* If scrolling up, there may be some lines to display at the end of
17706 the window. */
17707 last_text_row_at_end = NULL;
17708 if (dy < 0)
17709 {
17710 /* Scrolling up can leave for example a partially visible line
17711 at the end of the window to be redisplayed. */
17712 /* Set last_row to the glyph row in the current matrix where the
17713 window end line is found. It has been moved up or down in
17714 the matrix by dvpos. */
17715 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17716 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17717
17718 /* If last_row is the window end line, it should display text. */
17719 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17720
17721 /* If window end line was partially visible before, begin
17722 displaying at that line. Otherwise begin displaying with the
17723 line following it. */
17724 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17725 {
17726 init_to_row_start (&it, w, last_row);
17727 it.vpos = last_vpos;
17728 it.current_y = last_row->y;
17729 }
17730 else
17731 {
17732 init_to_row_end (&it, w, last_row);
17733 it.vpos = 1 + last_vpos;
17734 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17735 ++last_row;
17736 }
17737
17738 /* We may start in a continuation line. If so, we have to
17739 get the right continuation_lines_width and current_x. */
17740 it.continuation_lines_width = last_row->continuation_lines_width;
17741 it.hpos = it.current_x = 0;
17742
17743 /* Display the rest of the lines at the window end. */
17744 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17745 while (it.current_y < it.last_visible_y
17746 && !fonts_changed_p)
17747 {
17748 /* Is it always sure that the display agrees with lines in
17749 the current matrix? I don't think so, so we mark rows
17750 displayed invalid in the current matrix by setting their
17751 enabled_p flag to zero. */
17752 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17753 if (display_line (&it))
17754 last_text_row_at_end = it.glyph_row - 1;
17755 }
17756 }
17757
17758 /* Update window_end_pos and window_end_vpos. */
17759 if (first_unchanged_at_end_row
17760 && !last_text_row_at_end)
17761 {
17762 /* Window end line if one of the preserved rows from the current
17763 matrix. Set row to the last row displaying text in current
17764 matrix starting at first_unchanged_at_end_row, after
17765 scrolling. */
17766 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17767 row = find_last_row_displaying_text (w->current_matrix, &it,
17768 first_unchanged_at_end_row);
17769 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17770
17771 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17772 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17773 wset_window_end_vpos
17774 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17775 eassert (w->window_end_bytepos >= 0);
17776 IF_DEBUG (debug_method_add (w, "A"));
17777 }
17778 else if (last_text_row_at_end)
17779 {
17780 wset_window_end_pos
17781 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17782 w->window_end_bytepos
17783 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17784 wset_window_end_vpos
17785 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17786 desired_matrix)));
17787 eassert (w->window_end_bytepos >= 0);
17788 IF_DEBUG (debug_method_add (w, "B"));
17789 }
17790 else if (last_text_row)
17791 {
17792 /* We have displayed either to the end of the window or at the
17793 end of the window, i.e. the last row with text is to be found
17794 in the desired matrix. */
17795 wset_window_end_pos
17796 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17797 w->window_end_bytepos
17798 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17799 wset_window_end_vpos
17800 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17801 eassert (w->window_end_bytepos >= 0);
17802 }
17803 else if (first_unchanged_at_end_row == NULL
17804 && last_text_row == NULL
17805 && last_text_row_at_end == NULL)
17806 {
17807 /* Displayed to end of window, but no line containing text was
17808 displayed. Lines were deleted at the end of the window. */
17809 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17810 int vpos = XFASTINT (w->window_end_vpos);
17811 struct glyph_row *current_row = current_matrix->rows + vpos;
17812 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17813
17814 for (row = NULL;
17815 row == NULL && vpos >= first_vpos;
17816 --vpos, --current_row, --desired_row)
17817 {
17818 if (desired_row->enabled_p)
17819 {
17820 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17821 row = desired_row;
17822 }
17823 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17824 row = current_row;
17825 }
17826
17827 eassert (row != NULL);
17828 wset_window_end_vpos (w, make_number (vpos + 1));
17829 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17830 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17831 eassert (w->window_end_bytepos >= 0);
17832 IF_DEBUG (debug_method_add (w, "C"));
17833 }
17834 else
17835 emacs_abort ();
17836
17837 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17838 debug_end_vpos = XFASTINT (w->window_end_vpos));
17839
17840 /* Record that display has not been completed. */
17841 w->window_end_valid = 0;
17842 w->desired_matrix->no_scrolling_p = 1;
17843 return 3;
17844
17845 #undef GIVE_UP
17846 }
17847
17848
17849 \f
17850 /***********************************************************************
17851 More debugging support
17852 ***********************************************************************/
17853
17854 #ifdef GLYPH_DEBUG
17855
17856 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17857 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17858 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17859
17860
17861 /* Dump the contents of glyph matrix MATRIX on stderr.
17862
17863 GLYPHS 0 means don't show glyph contents.
17864 GLYPHS 1 means show glyphs in short form
17865 GLYPHS > 1 means show glyphs in long form. */
17866
17867 void
17868 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17869 {
17870 int i;
17871 for (i = 0; i < matrix->nrows; ++i)
17872 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17873 }
17874
17875
17876 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17877 the glyph row and area where the glyph comes from. */
17878
17879 void
17880 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17881 {
17882 if (glyph->type == CHAR_GLYPH
17883 || glyph->type == GLYPHLESS_GLYPH)
17884 {
17885 fprintf (stderr,
17886 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17887 glyph - row->glyphs[TEXT_AREA],
17888 (glyph->type == CHAR_GLYPH
17889 ? 'C'
17890 : 'G'),
17891 glyph->charpos,
17892 (BUFFERP (glyph->object)
17893 ? 'B'
17894 : (STRINGP (glyph->object)
17895 ? 'S'
17896 : (INTEGERP (glyph->object)
17897 ? '0'
17898 : '-'))),
17899 glyph->pixel_width,
17900 glyph->u.ch,
17901 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17902 ? glyph->u.ch
17903 : '.'),
17904 glyph->face_id,
17905 glyph->left_box_line_p,
17906 glyph->right_box_line_p);
17907 }
17908 else if (glyph->type == STRETCH_GLYPH)
17909 {
17910 fprintf (stderr,
17911 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17912 glyph - row->glyphs[TEXT_AREA],
17913 'S',
17914 glyph->charpos,
17915 (BUFFERP (glyph->object)
17916 ? 'B'
17917 : (STRINGP (glyph->object)
17918 ? 'S'
17919 : (INTEGERP (glyph->object)
17920 ? '0'
17921 : '-'))),
17922 glyph->pixel_width,
17923 0,
17924 ' ',
17925 glyph->face_id,
17926 glyph->left_box_line_p,
17927 glyph->right_box_line_p);
17928 }
17929 else if (glyph->type == IMAGE_GLYPH)
17930 {
17931 fprintf (stderr,
17932 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17933 glyph - row->glyphs[TEXT_AREA],
17934 'I',
17935 glyph->charpos,
17936 (BUFFERP (glyph->object)
17937 ? 'B'
17938 : (STRINGP (glyph->object)
17939 ? 'S'
17940 : (INTEGERP (glyph->object)
17941 ? '0'
17942 : '-'))),
17943 glyph->pixel_width,
17944 glyph->u.img_id,
17945 '.',
17946 glyph->face_id,
17947 glyph->left_box_line_p,
17948 glyph->right_box_line_p);
17949 }
17950 else if (glyph->type == COMPOSITE_GLYPH)
17951 {
17952 fprintf (stderr,
17953 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17954 glyph - row->glyphs[TEXT_AREA],
17955 '+',
17956 glyph->charpos,
17957 (BUFFERP (glyph->object)
17958 ? 'B'
17959 : (STRINGP (glyph->object)
17960 ? 'S'
17961 : (INTEGERP (glyph->object)
17962 ? '0'
17963 : '-'))),
17964 glyph->pixel_width,
17965 glyph->u.cmp.id);
17966 if (glyph->u.cmp.automatic)
17967 fprintf (stderr,
17968 "[%d-%d]",
17969 glyph->slice.cmp.from, glyph->slice.cmp.to);
17970 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17971 glyph->face_id,
17972 glyph->left_box_line_p,
17973 glyph->right_box_line_p);
17974 }
17975 }
17976
17977
17978 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17979 GLYPHS 0 means don't show glyph contents.
17980 GLYPHS 1 means show glyphs in short form
17981 GLYPHS > 1 means show glyphs in long form. */
17982
17983 void
17984 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17985 {
17986 if (glyphs != 1)
17987 {
17988 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17989 fprintf (stderr, "==============================================================================\n");
17990
17991 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17992 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17993 vpos,
17994 MATRIX_ROW_START_CHARPOS (row),
17995 MATRIX_ROW_END_CHARPOS (row),
17996 row->used[TEXT_AREA],
17997 row->contains_overlapping_glyphs_p,
17998 row->enabled_p,
17999 row->truncated_on_left_p,
18000 row->truncated_on_right_p,
18001 row->continued_p,
18002 MATRIX_ROW_CONTINUATION_LINE_P (row),
18003 MATRIX_ROW_DISPLAYS_TEXT_P (row),
18004 row->ends_at_zv_p,
18005 row->fill_line_p,
18006 row->ends_in_middle_of_char_p,
18007 row->starts_in_middle_of_char_p,
18008 row->mouse_face_p,
18009 row->x,
18010 row->y,
18011 row->pixel_width,
18012 row->height,
18013 row->visible_height,
18014 row->ascent,
18015 row->phys_ascent);
18016 /* The next 3 lines should align to "Start" in the header. */
18017 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
18018 row->end.overlay_string_index,
18019 row->continuation_lines_width);
18020 fprintf (stderr, " %9"pI"d %9"pI"d\n",
18021 CHARPOS (row->start.string_pos),
18022 CHARPOS (row->end.string_pos));
18023 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
18024 row->end.dpvec_index);
18025 }
18026
18027 if (glyphs > 1)
18028 {
18029 int area;
18030
18031 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18032 {
18033 struct glyph *glyph = row->glyphs[area];
18034 struct glyph *glyph_end = glyph + row->used[area];
18035
18036 /* Glyph for a line end in text. */
18037 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18038 ++glyph_end;
18039
18040 if (glyph < glyph_end)
18041 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
18042
18043 for (; glyph < glyph_end; ++glyph)
18044 dump_glyph (row, glyph, area);
18045 }
18046 }
18047 else if (glyphs == 1)
18048 {
18049 int area;
18050
18051 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18052 {
18053 char *s = alloca (row->used[area] + 4);
18054 int i;
18055
18056 for (i = 0; i < row->used[area]; ++i)
18057 {
18058 struct glyph *glyph = row->glyphs[area] + i;
18059 if (i == row->used[area] - 1
18060 && area == TEXT_AREA
18061 && INTEGERP (glyph->object)
18062 && glyph->type == CHAR_GLYPH
18063 && glyph->u.ch == ' ')
18064 {
18065 strcpy (&s[i], "[\\n]");
18066 i += 4;
18067 }
18068 else if (glyph->type == CHAR_GLYPH
18069 && glyph->u.ch < 0x80
18070 && glyph->u.ch >= ' ')
18071 s[i] = glyph->u.ch;
18072 else
18073 s[i] = '.';
18074 }
18075
18076 s[i] = '\0';
18077 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18078 }
18079 }
18080 }
18081
18082
18083 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18084 Sdump_glyph_matrix, 0, 1, "p",
18085 doc: /* Dump the current matrix of the selected window to stderr.
18086 Shows contents of glyph row structures. With non-nil
18087 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18088 glyphs in short form, otherwise show glyphs in long form. */)
18089 (Lisp_Object glyphs)
18090 {
18091 struct window *w = XWINDOW (selected_window);
18092 struct buffer *buffer = XBUFFER (w->contents);
18093
18094 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18095 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18096 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18097 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18098 fprintf (stderr, "=============================================\n");
18099 dump_glyph_matrix (w->current_matrix,
18100 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18101 return Qnil;
18102 }
18103
18104
18105 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18106 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18107 (void)
18108 {
18109 struct frame *f = XFRAME (selected_frame);
18110 dump_glyph_matrix (f->current_matrix, 1);
18111 return Qnil;
18112 }
18113
18114
18115 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18116 doc: /* Dump glyph row ROW to stderr.
18117 GLYPH 0 means don't dump glyphs.
18118 GLYPH 1 means dump glyphs in short form.
18119 GLYPH > 1 or omitted means dump glyphs in long form. */)
18120 (Lisp_Object row, Lisp_Object glyphs)
18121 {
18122 struct glyph_matrix *matrix;
18123 EMACS_INT vpos;
18124
18125 CHECK_NUMBER (row);
18126 matrix = XWINDOW (selected_window)->current_matrix;
18127 vpos = XINT (row);
18128 if (vpos >= 0 && vpos < matrix->nrows)
18129 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18130 vpos,
18131 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18132 return Qnil;
18133 }
18134
18135
18136 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18137 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18138 GLYPH 0 means don't dump glyphs.
18139 GLYPH 1 means dump glyphs in short form.
18140 GLYPH > 1 or omitted means dump glyphs in long form. */)
18141 (Lisp_Object row, Lisp_Object glyphs)
18142 {
18143 struct frame *sf = SELECTED_FRAME ();
18144 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18145 EMACS_INT vpos;
18146
18147 CHECK_NUMBER (row);
18148 vpos = XINT (row);
18149 if (vpos >= 0 && vpos < m->nrows)
18150 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18151 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18152 return Qnil;
18153 }
18154
18155
18156 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18157 doc: /* Toggle tracing of redisplay.
18158 With ARG, turn tracing on if and only if ARG is positive. */)
18159 (Lisp_Object arg)
18160 {
18161 if (NILP (arg))
18162 trace_redisplay_p = !trace_redisplay_p;
18163 else
18164 {
18165 arg = Fprefix_numeric_value (arg);
18166 trace_redisplay_p = XINT (arg) > 0;
18167 }
18168
18169 return Qnil;
18170 }
18171
18172
18173 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18174 doc: /* Like `format', but print result to stderr.
18175 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18176 (ptrdiff_t nargs, Lisp_Object *args)
18177 {
18178 Lisp_Object s = Fformat (nargs, args);
18179 fprintf (stderr, "%s", SDATA (s));
18180 return Qnil;
18181 }
18182
18183 #endif /* GLYPH_DEBUG */
18184
18185
18186 \f
18187 /***********************************************************************
18188 Building Desired Matrix Rows
18189 ***********************************************************************/
18190
18191 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18192 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18193
18194 static struct glyph_row *
18195 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18196 {
18197 struct frame *f = XFRAME (WINDOW_FRAME (w));
18198 struct buffer *buffer = XBUFFER (w->contents);
18199 struct buffer *old = current_buffer;
18200 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18201 int arrow_len = SCHARS (overlay_arrow_string);
18202 const unsigned char *arrow_end = arrow_string + arrow_len;
18203 const unsigned char *p;
18204 struct it it;
18205 bool multibyte_p;
18206 int n_glyphs_before;
18207
18208 set_buffer_temp (buffer);
18209 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18210 it.glyph_row->used[TEXT_AREA] = 0;
18211 SET_TEXT_POS (it.position, 0, 0);
18212
18213 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18214 p = arrow_string;
18215 while (p < arrow_end)
18216 {
18217 Lisp_Object face, ilisp;
18218
18219 /* Get the next character. */
18220 if (multibyte_p)
18221 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18222 else
18223 {
18224 it.c = it.char_to_display = *p, it.len = 1;
18225 if (! ASCII_CHAR_P (it.c))
18226 it.char_to_display = BYTE8_TO_CHAR (it.c);
18227 }
18228 p += it.len;
18229
18230 /* Get its face. */
18231 ilisp = make_number (p - arrow_string);
18232 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18233 it.face_id = compute_char_face (f, it.char_to_display, face);
18234
18235 /* Compute its width, get its glyphs. */
18236 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18237 SET_TEXT_POS (it.position, -1, -1);
18238 PRODUCE_GLYPHS (&it);
18239
18240 /* If this character doesn't fit any more in the line, we have
18241 to remove some glyphs. */
18242 if (it.current_x > it.last_visible_x)
18243 {
18244 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18245 break;
18246 }
18247 }
18248
18249 set_buffer_temp (old);
18250 return it.glyph_row;
18251 }
18252
18253
18254 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18255 glyphs to insert is determined by produce_special_glyphs. */
18256
18257 static void
18258 insert_left_trunc_glyphs (struct it *it)
18259 {
18260 struct it truncate_it;
18261 struct glyph *from, *end, *to, *toend;
18262
18263 eassert (!FRAME_WINDOW_P (it->f)
18264 || (!it->glyph_row->reversed_p
18265 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18266 || (it->glyph_row->reversed_p
18267 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18268
18269 /* Get the truncation glyphs. */
18270 truncate_it = *it;
18271 truncate_it.current_x = 0;
18272 truncate_it.face_id = DEFAULT_FACE_ID;
18273 truncate_it.glyph_row = &scratch_glyph_row;
18274 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18275 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18276 truncate_it.object = make_number (0);
18277 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18278
18279 /* Overwrite glyphs from IT with truncation glyphs. */
18280 if (!it->glyph_row->reversed_p)
18281 {
18282 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18283
18284 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18285 end = from + tused;
18286 to = it->glyph_row->glyphs[TEXT_AREA];
18287 toend = to + it->glyph_row->used[TEXT_AREA];
18288 if (FRAME_WINDOW_P (it->f))
18289 {
18290 /* On GUI frames, when variable-size fonts are displayed,
18291 the truncation glyphs may need more pixels than the row's
18292 glyphs they overwrite. We overwrite more glyphs to free
18293 enough screen real estate, and enlarge the stretch glyph
18294 on the right (see display_line), if there is one, to
18295 preserve the screen position of the truncation glyphs on
18296 the right. */
18297 int w = 0;
18298 struct glyph *g = to;
18299 short used;
18300
18301 /* The first glyph could be partially visible, in which case
18302 it->glyph_row->x will be negative. But we want the left
18303 truncation glyphs to be aligned at the left margin of the
18304 window, so we override the x coordinate at which the row
18305 will begin. */
18306 it->glyph_row->x = 0;
18307 while (g < toend && w < it->truncation_pixel_width)
18308 {
18309 w += g->pixel_width;
18310 ++g;
18311 }
18312 if (g - to - tused > 0)
18313 {
18314 memmove (to + tused, g, (toend - g) * sizeof(*g));
18315 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18316 }
18317 used = it->glyph_row->used[TEXT_AREA];
18318 if (it->glyph_row->truncated_on_right_p
18319 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18320 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18321 == STRETCH_GLYPH)
18322 {
18323 int extra = w - it->truncation_pixel_width;
18324
18325 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18326 }
18327 }
18328
18329 while (from < end)
18330 *to++ = *from++;
18331
18332 /* There may be padding glyphs left over. Overwrite them too. */
18333 if (!FRAME_WINDOW_P (it->f))
18334 {
18335 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18336 {
18337 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18338 while (from < end)
18339 *to++ = *from++;
18340 }
18341 }
18342
18343 if (to > toend)
18344 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18345 }
18346 else
18347 {
18348 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18349
18350 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18351 that back to front. */
18352 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18353 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18354 toend = it->glyph_row->glyphs[TEXT_AREA];
18355 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18356 if (FRAME_WINDOW_P (it->f))
18357 {
18358 int w = 0;
18359 struct glyph *g = to;
18360
18361 while (g >= toend && w < it->truncation_pixel_width)
18362 {
18363 w += g->pixel_width;
18364 --g;
18365 }
18366 if (to - g - tused > 0)
18367 to = g + tused;
18368 if (it->glyph_row->truncated_on_right_p
18369 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18370 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18371 {
18372 int extra = w - it->truncation_pixel_width;
18373
18374 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18375 }
18376 }
18377
18378 while (from >= end && to >= toend)
18379 *to-- = *from--;
18380 if (!FRAME_WINDOW_P (it->f))
18381 {
18382 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18383 {
18384 from =
18385 truncate_it.glyph_row->glyphs[TEXT_AREA]
18386 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18387 while (from >= end && to >= toend)
18388 *to-- = *from--;
18389 }
18390 }
18391 if (from >= end)
18392 {
18393 /* Need to free some room before prepending additional
18394 glyphs. */
18395 int move_by = from - end + 1;
18396 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18397 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18398
18399 for ( ; g >= g0; g--)
18400 g[move_by] = *g;
18401 while (from >= end)
18402 *to-- = *from--;
18403 it->glyph_row->used[TEXT_AREA] += move_by;
18404 }
18405 }
18406 }
18407
18408 /* Compute the hash code for ROW. */
18409 unsigned
18410 row_hash (struct glyph_row *row)
18411 {
18412 int area, k;
18413 unsigned hashval = 0;
18414
18415 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18416 for (k = 0; k < row->used[area]; ++k)
18417 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18418 + row->glyphs[area][k].u.val
18419 + row->glyphs[area][k].face_id
18420 + row->glyphs[area][k].padding_p
18421 + (row->glyphs[area][k].type << 2));
18422
18423 return hashval;
18424 }
18425
18426 /* Compute the pixel height and width of IT->glyph_row.
18427
18428 Most of the time, ascent and height of a display line will be equal
18429 to the max_ascent and max_height values of the display iterator
18430 structure. This is not the case if
18431
18432 1. We hit ZV without displaying anything. In this case, max_ascent
18433 and max_height will be zero.
18434
18435 2. We have some glyphs that don't contribute to the line height.
18436 (The glyph row flag contributes_to_line_height_p is for future
18437 pixmap extensions).
18438
18439 The first case is easily covered by using default values because in
18440 these cases, the line height does not really matter, except that it
18441 must not be zero. */
18442
18443 static void
18444 compute_line_metrics (struct it *it)
18445 {
18446 struct glyph_row *row = it->glyph_row;
18447
18448 if (FRAME_WINDOW_P (it->f))
18449 {
18450 int i, min_y, max_y;
18451
18452 /* The line may consist of one space only, that was added to
18453 place the cursor on it. If so, the row's height hasn't been
18454 computed yet. */
18455 if (row->height == 0)
18456 {
18457 if (it->max_ascent + it->max_descent == 0)
18458 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18459 row->ascent = it->max_ascent;
18460 row->height = it->max_ascent + it->max_descent;
18461 row->phys_ascent = it->max_phys_ascent;
18462 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18463 row->extra_line_spacing = it->max_extra_line_spacing;
18464 }
18465
18466 /* Compute the width of this line. */
18467 row->pixel_width = row->x;
18468 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18469 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18470
18471 eassert (row->pixel_width >= 0);
18472 eassert (row->ascent >= 0 && row->height > 0);
18473
18474 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18475 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18476
18477 /* If first line's physical ascent is larger than its logical
18478 ascent, use the physical ascent, and make the row taller.
18479 This makes accented characters fully visible. */
18480 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18481 && row->phys_ascent > row->ascent)
18482 {
18483 row->height += row->phys_ascent - row->ascent;
18484 row->ascent = row->phys_ascent;
18485 }
18486
18487 /* Compute how much of the line is visible. */
18488 row->visible_height = row->height;
18489
18490 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18491 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18492
18493 if (row->y < min_y)
18494 row->visible_height -= min_y - row->y;
18495 if (row->y + row->height > max_y)
18496 row->visible_height -= row->y + row->height - max_y;
18497 }
18498 else
18499 {
18500 row->pixel_width = row->used[TEXT_AREA];
18501 if (row->continued_p)
18502 row->pixel_width -= it->continuation_pixel_width;
18503 else if (row->truncated_on_right_p)
18504 row->pixel_width -= it->truncation_pixel_width;
18505 row->ascent = row->phys_ascent = 0;
18506 row->height = row->phys_height = row->visible_height = 1;
18507 row->extra_line_spacing = 0;
18508 }
18509
18510 /* Compute a hash code for this row. */
18511 row->hash = row_hash (row);
18512
18513 it->max_ascent = it->max_descent = 0;
18514 it->max_phys_ascent = it->max_phys_descent = 0;
18515 }
18516
18517
18518 /* Append one space to the glyph row of iterator IT if doing a
18519 window-based redisplay. The space has the same face as
18520 IT->face_id. Value is non-zero if a space was added.
18521
18522 This function is called to make sure that there is always one glyph
18523 at the end of a glyph row that the cursor can be set on under
18524 window-systems. (If there weren't such a glyph we would not know
18525 how wide and tall a box cursor should be displayed).
18526
18527 At the same time this space let's a nicely handle clearing to the
18528 end of the line if the row ends in italic text. */
18529
18530 static int
18531 append_space_for_newline (struct it *it, int default_face_p)
18532 {
18533 if (FRAME_WINDOW_P (it->f))
18534 {
18535 int n = it->glyph_row->used[TEXT_AREA];
18536
18537 if (it->glyph_row->glyphs[TEXT_AREA] + n
18538 < it->glyph_row->glyphs[1 + TEXT_AREA])
18539 {
18540 /* Save some values that must not be changed.
18541 Must save IT->c and IT->len because otherwise
18542 ITERATOR_AT_END_P wouldn't work anymore after
18543 append_space_for_newline has been called. */
18544 enum display_element_type saved_what = it->what;
18545 int saved_c = it->c, saved_len = it->len;
18546 int saved_char_to_display = it->char_to_display;
18547 int saved_x = it->current_x;
18548 int saved_face_id = it->face_id;
18549 int saved_box_end = it->end_of_box_run_p;
18550 struct text_pos saved_pos;
18551 Lisp_Object saved_object;
18552 struct face *face;
18553
18554 saved_object = it->object;
18555 saved_pos = it->position;
18556
18557 it->what = IT_CHARACTER;
18558 memset (&it->position, 0, sizeof it->position);
18559 it->object = make_number (0);
18560 it->c = it->char_to_display = ' ';
18561 it->len = 1;
18562
18563 /* If the default face was remapped, be sure to use the
18564 remapped face for the appended newline. */
18565 if (default_face_p)
18566 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18567 else if (it->face_before_selective_p)
18568 it->face_id = it->saved_face_id;
18569 face = FACE_FROM_ID (it->f, it->face_id);
18570 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18571 /* In R2L rows, we will prepend a stretch glyph that will
18572 have the end_of_box_run_p flag set for it, so there's no
18573 need for the appended newline glyph to have that flag
18574 set. */
18575 if (it->glyph_row->reversed_p
18576 /* But if the appended newline glyph goes all the way to
18577 the end of the row, there will be no stretch glyph,
18578 so leave the box flag set. */
18579 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18580 it->end_of_box_run_p = 0;
18581
18582 PRODUCE_GLYPHS (it);
18583
18584 it->override_ascent = -1;
18585 it->constrain_row_ascent_descent_p = 0;
18586 it->current_x = saved_x;
18587 it->object = saved_object;
18588 it->position = saved_pos;
18589 it->what = saved_what;
18590 it->face_id = saved_face_id;
18591 it->len = saved_len;
18592 it->c = saved_c;
18593 it->char_to_display = saved_char_to_display;
18594 it->end_of_box_run_p = saved_box_end;
18595 return 1;
18596 }
18597 }
18598
18599 return 0;
18600 }
18601
18602
18603 /* Extend the face of the last glyph in the text area of IT->glyph_row
18604 to the end of the display line. Called from display_line. If the
18605 glyph row is empty, add a space glyph to it so that we know the
18606 face to draw. Set the glyph row flag fill_line_p. If the glyph
18607 row is R2L, prepend a stretch glyph to cover the empty space to the
18608 left of the leftmost glyph. */
18609
18610 static void
18611 extend_face_to_end_of_line (struct it *it)
18612 {
18613 struct face *face, *default_face;
18614 struct frame *f = it->f;
18615
18616 /* If line is already filled, do nothing. Non window-system frames
18617 get a grace of one more ``pixel'' because their characters are
18618 1-``pixel'' wide, so they hit the equality too early. This grace
18619 is needed only for R2L rows that are not continued, to produce
18620 one extra blank where we could display the cursor. */
18621 if (it->current_x >= it->last_visible_x
18622 + (!FRAME_WINDOW_P (f)
18623 && it->glyph_row->reversed_p
18624 && !it->glyph_row->continued_p))
18625 return;
18626
18627 /* The default face, possibly remapped. */
18628 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18629
18630 /* Face extension extends the background and box of IT->face_id
18631 to the end of the line. If the background equals the background
18632 of the frame, we don't have to do anything. */
18633 if (it->face_before_selective_p)
18634 face = FACE_FROM_ID (f, it->saved_face_id);
18635 else
18636 face = FACE_FROM_ID (f, it->face_id);
18637
18638 if (FRAME_WINDOW_P (f)
18639 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18640 && face->box == FACE_NO_BOX
18641 && face->background == FRAME_BACKGROUND_PIXEL (f)
18642 && !face->stipple
18643 && !it->glyph_row->reversed_p)
18644 return;
18645
18646 /* Set the glyph row flag indicating that the face of the last glyph
18647 in the text area has to be drawn to the end of the text area. */
18648 it->glyph_row->fill_line_p = 1;
18649
18650 /* If current character of IT is not ASCII, make sure we have the
18651 ASCII face. This will be automatically undone the next time
18652 get_next_display_element returns a multibyte character. Note
18653 that the character will always be single byte in unibyte
18654 text. */
18655 if (!ASCII_CHAR_P (it->c))
18656 {
18657 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18658 }
18659
18660 if (FRAME_WINDOW_P (f))
18661 {
18662 /* If the row is empty, add a space with the current face of IT,
18663 so that we know which face to draw. */
18664 if (it->glyph_row->used[TEXT_AREA] == 0)
18665 {
18666 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18667 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18668 it->glyph_row->used[TEXT_AREA] = 1;
18669 }
18670 #ifdef HAVE_WINDOW_SYSTEM
18671 if (it->glyph_row->reversed_p)
18672 {
18673 /* Prepend a stretch glyph to the row, such that the
18674 rightmost glyph will be drawn flushed all the way to the
18675 right margin of the window. The stretch glyph that will
18676 occupy the empty space, if any, to the left of the
18677 glyphs. */
18678 struct font *font = face->font ? face->font : FRAME_FONT (f);
18679 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18680 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18681 struct glyph *g;
18682 int row_width, stretch_ascent, stretch_width;
18683 struct text_pos saved_pos;
18684 int saved_face_id, saved_avoid_cursor, saved_box_start;
18685
18686 for (row_width = 0, g = row_start; g < row_end; g++)
18687 row_width += g->pixel_width;
18688 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18689 if (stretch_width > 0)
18690 {
18691 stretch_ascent =
18692 (((it->ascent + it->descent)
18693 * FONT_BASE (font)) / FONT_HEIGHT (font));
18694 saved_pos = it->position;
18695 memset (&it->position, 0, sizeof it->position);
18696 saved_avoid_cursor = it->avoid_cursor_p;
18697 it->avoid_cursor_p = 1;
18698 saved_face_id = it->face_id;
18699 saved_box_start = it->start_of_box_run_p;
18700 /* The last row's stretch glyph should get the default
18701 face, to avoid painting the rest of the window with
18702 the region face, if the region ends at ZV. */
18703 if (it->glyph_row->ends_at_zv_p)
18704 it->face_id = default_face->id;
18705 else
18706 it->face_id = face->id;
18707 it->start_of_box_run_p = 0;
18708 append_stretch_glyph (it, make_number (0), stretch_width,
18709 it->ascent + it->descent, stretch_ascent);
18710 it->position = saved_pos;
18711 it->avoid_cursor_p = saved_avoid_cursor;
18712 it->face_id = saved_face_id;
18713 it->start_of_box_run_p = saved_box_start;
18714 }
18715 }
18716 #endif /* HAVE_WINDOW_SYSTEM */
18717 }
18718 else
18719 {
18720 /* Save some values that must not be changed. */
18721 int saved_x = it->current_x;
18722 struct text_pos saved_pos;
18723 Lisp_Object saved_object;
18724 enum display_element_type saved_what = it->what;
18725 int saved_face_id = it->face_id;
18726
18727 saved_object = it->object;
18728 saved_pos = it->position;
18729
18730 it->what = IT_CHARACTER;
18731 memset (&it->position, 0, sizeof it->position);
18732 it->object = make_number (0);
18733 it->c = it->char_to_display = ' ';
18734 it->len = 1;
18735 /* The last row's blank glyphs should get the default face, to
18736 avoid painting the rest of the window with the region face,
18737 if the region ends at ZV. */
18738 if (it->glyph_row->ends_at_zv_p)
18739 it->face_id = default_face->id;
18740 else
18741 it->face_id = face->id;
18742
18743 PRODUCE_GLYPHS (it);
18744
18745 while (it->current_x <= it->last_visible_x)
18746 PRODUCE_GLYPHS (it);
18747
18748 /* Don't count these blanks really. It would let us insert a left
18749 truncation glyph below and make us set the cursor on them, maybe. */
18750 it->current_x = saved_x;
18751 it->object = saved_object;
18752 it->position = saved_pos;
18753 it->what = saved_what;
18754 it->face_id = saved_face_id;
18755 }
18756 }
18757
18758
18759 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18760 trailing whitespace. */
18761
18762 static int
18763 trailing_whitespace_p (ptrdiff_t charpos)
18764 {
18765 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18766 int c = 0;
18767
18768 while (bytepos < ZV_BYTE
18769 && (c = FETCH_CHAR (bytepos),
18770 c == ' ' || c == '\t'))
18771 ++bytepos;
18772
18773 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18774 {
18775 if (bytepos != PT_BYTE)
18776 return 1;
18777 }
18778 return 0;
18779 }
18780
18781
18782 /* Highlight trailing whitespace, if any, in ROW. */
18783
18784 static void
18785 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18786 {
18787 int used = row->used[TEXT_AREA];
18788
18789 if (used)
18790 {
18791 struct glyph *start = row->glyphs[TEXT_AREA];
18792 struct glyph *glyph = start + used - 1;
18793
18794 if (row->reversed_p)
18795 {
18796 /* Right-to-left rows need to be processed in the opposite
18797 direction, so swap the edge pointers. */
18798 glyph = start;
18799 start = row->glyphs[TEXT_AREA] + used - 1;
18800 }
18801
18802 /* Skip over glyphs inserted to display the cursor at the
18803 end of a line, for extending the face of the last glyph
18804 to the end of the line on terminals, and for truncation
18805 and continuation glyphs. */
18806 if (!row->reversed_p)
18807 {
18808 while (glyph >= start
18809 && glyph->type == CHAR_GLYPH
18810 && INTEGERP (glyph->object))
18811 --glyph;
18812 }
18813 else
18814 {
18815 while (glyph <= start
18816 && glyph->type == CHAR_GLYPH
18817 && INTEGERP (glyph->object))
18818 ++glyph;
18819 }
18820
18821 /* If last glyph is a space or stretch, and it's trailing
18822 whitespace, set the face of all trailing whitespace glyphs in
18823 IT->glyph_row to `trailing-whitespace'. */
18824 if ((row->reversed_p ? glyph <= start : glyph >= start)
18825 && BUFFERP (glyph->object)
18826 && (glyph->type == STRETCH_GLYPH
18827 || (glyph->type == CHAR_GLYPH
18828 && glyph->u.ch == ' '))
18829 && trailing_whitespace_p (glyph->charpos))
18830 {
18831 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18832 if (face_id < 0)
18833 return;
18834
18835 if (!row->reversed_p)
18836 {
18837 while (glyph >= start
18838 && BUFFERP (glyph->object)
18839 && (glyph->type == STRETCH_GLYPH
18840 || (glyph->type == CHAR_GLYPH
18841 && glyph->u.ch == ' ')))
18842 (glyph--)->face_id = face_id;
18843 }
18844 else
18845 {
18846 while (glyph <= start
18847 && BUFFERP (glyph->object)
18848 && (glyph->type == STRETCH_GLYPH
18849 || (glyph->type == CHAR_GLYPH
18850 && glyph->u.ch == ' ')))
18851 (glyph++)->face_id = face_id;
18852 }
18853 }
18854 }
18855 }
18856
18857
18858 /* Value is non-zero if glyph row ROW should be
18859 considered to hold the buffer position CHARPOS. */
18860
18861 static int
18862 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18863 {
18864 int result = 1;
18865
18866 if (charpos == CHARPOS (row->end.pos)
18867 || charpos == MATRIX_ROW_END_CHARPOS (row))
18868 {
18869 /* Suppose the row ends on a string.
18870 Unless the row is continued, that means it ends on a newline
18871 in the string. If it's anything other than a display string
18872 (e.g., a before-string from an overlay), we don't want the
18873 cursor there. (This heuristic seems to give the optimal
18874 behavior for the various types of multi-line strings.)
18875 One exception: if the string has `cursor' property on one of
18876 its characters, we _do_ want the cursor there. */
18877 if (CHARPOS (row->end.string_pos) >= 0)
18878 {
18879 if (row->continued_p)
18880 result = 1;
18881 else
18882 {
18883 /* Check for `display' property. */
18884 struct glyph *beg = row->glyphs[TEXT_AREA];
18885 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18886 struct glyph *glyph;
18887
18888 result = 0;
18889 for (glyph = end; glyph >= beg; --glyph)
18890 if (STRINGP (glyph->object))
18891 {
18892 Lisp_Object prop
18893 = Fget_char_property (make_number (charpos),
18894 Qdisplay, Qnil);
18895 result =
18896 (!NILP (prop)
18897 && display_prop_string_p (prop, glyph->object));
18898 /* If there's a `cursor' property on one of the
18899 string's characters, this row is a cursor row,
18900 even though this is not a display string. */
18901 if (!result)
18902 {
18903 Lisp_Object s = glyph->object;
18904
18905 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18906 {
18907 ptrdiff_t gpos = glyph->charpos;
18908
18909 if (!NILP (Fget_char_property (make_number (gpos),
18910 Qcursor, s)))
18911 {
18912 result = 1;
18913 break;
18914 }
18915 }
18916 }
18917 break;
18918 }
18919 }
18920 }
18921 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18922 {
18923 /* If the row ends in middle of a real character,
18924 and the line is continued, we want the cursor here.
18925 That's because CHARPOS (ROW->end.pos) would equal
18926 PT if PT is before the character. */
18927 if (!row->ends_in_ellipsis_p)
18928 result = row->continued_p;
18929 else
18930 /* If the row ends in an ellipsis, then
18931 CHARPOS (ROW->end.pos) will equal point after the
18932 invisible text. We want that position to be displayed
18933 after the ellipsis. */
18934 result = 0;
18935 }
18936 /* If the row ends at ZV, display the cursor at the end of that
18937 row instead of at the start of the row below. */
18938 else if (row->ends_at_zv_p)
18939 result = 1;
18940 else
18941 result = 0;
18942 }
18943
18944 return result;
18945 }
18946
18947 /* Value is non-zero if glyph row ROW should be
18948 used to hold the cursor. */
18949
18950 static int
18951 cursor_row_p (struct glyph_row *row)
18952 {
18953 return row_for_charpos_p (row, PT);
18954 }
18955
18956 \f
18957
18958 /* Push the property PROP so that it will be rendered at the current
18959 position in IT. Return 1 if PROP was successfully pushed, 0
18960 otherwise. Called from handle_line_prefix to handle the
18961 `line-prefix' and `wrap-prefix' properties. */
18962
18963 static int
18964 push_prefix_prop (struct it *it, Lisp_Object prop)
18965 {
18966 struct text_pos pos =
18967 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18968
18969 eassert (it->method == GET_FROM_BUFFER
18970 || it->method == GET_FROM_DISPLAY_VECTOR
18971 || it->method == GET_FROM_STRING);
18972
18973 /* We need to save the current buffer/string position, so it will be
18974 restored by pop_it, because iterate_out_of_display_property
18975 depends on that being set correctly, but some situations leave
18976 it->position not yet set when this function is called. */
18977 push_it (it, &pos);
18978
18979 if (STRINGP (prop))
18980 {
18981 if (SCHARS (prop) == 0)
18982 {
18983 pop_it (it);
18984 return 0;
18985 }
18986
18987 it->string = prop;
18988 it->string_from_prefix_prop_p = 1;
18989 it->multibyte_p = STRING_MULTIBYTE (it->string);
18990 it->current.overlay_string_index = -1;
18991 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18992 it->end_charpos = it->string_nchars = SCHARS (it->string);
18993 it->method = GET_FROM_STRING;
18994 it->stop_charpos = 0;
18995 it->prev_stop = 0;
18996 it->base_level_stop = 0;
18997
18998 /* Force paragraph direction to be that of the parent
18999 buffer/string. */
19000 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19001 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19002 else
19003 it->paragraph_embedding = L2R;
19004
19005 /* Set up the bidi iterator for this display string. */
19006 if (it->bidi_p)
19007 {
19008 it->bidi_it.string.lstring = it->string;
19009 it->bidi_it.string.s = NULL;
19010 it->bidi_it.string.schars = it->end_charpos;
19011 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19012 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19013 it->bidi_it.string.unibyte = !it->multibyte_p;
19014 it->bidi_it.w = it->w;
19015 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19016 }
19017 }
19018 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19019 {
19020 it->method = GET_FROM_STRETCH;
19021 it->object = prop;
19022 }
19023 #ifdef HAVE_WINDOW_SYSTEM
19024 else if (IMAGEP (prop))
19025 {
19026 it->what = IT_IMAGE;
19027 it->image_id = lookup_image (it->f, prop);
19028 it->method = GET_FROM_IMAGE;
19029 }
19030 #endif /* HAVE_WINDOW_SYSTEM */
19031 else
19032 {
19033 pop_it (it); /* bogus display property, give up */
19034 return 0;
19035 }
19036
19037 return 1;
19038 }
19039
19040 /* Return the character-property PROP at the current position in IT. */
19041
19042 static Lisp_Object
19043 get_it_property (struct it *it, Lisp_Object prop)
19044 {
19045 Lisp_Object position, object = it->object;
19046
19047 if (STRINGP (object))
19048 position = make_number (IT_STRING_CHARPOS (*it));
19049 else if (BUFFERP (object))
19050 {
19051 position = make_number (IT_CHARPOS (*it));
19052 object = it->window;
19053 }
19054 else
19055 return Qnil;
19056
19057 return Fget_char_property (position, prop, object);
19058 }
19059
19060 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19061
19062 static void
19063 handle_line_prefix (struct it *it)
19064 {
19065 Lisp_Object prefix;
19066
19067 if (it->continuation_lines_width > 0)
19068 {
19069 prefix = get_it_property (it, Qwrap_prefix);
19070 if (NILP (prefix))
19071 prefix = Vwrap_prefix;
19072 }
19073 else
19074 {
19075 prefix = get_it_property (it, Qline_prefix);
19076 if (NILP (prefix))
19077 prefix = Vline_prefix;
19078 }
19079 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19080 {
19081 /* If the prefix is wider than the window, and we try to wrap
19082 it, it would acquire its own wrap prefix, and so on till the
19083 iterator stack overflows. So, don't wrap the prefix. */
19084 it->line_wrap = TRUNCATE;
19085 it->avoid_cursor_p = 1;
19086 }
19087 }
19088
19089 \f
19090
19091 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19092 only for R2L lines from display_line and display_string, when they
19093 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19094 the line/string needs to be continued on the next glyph row. */
19095 static void
19096 unproduce_glyphs (struct it *it, int n)
19097 {
19098 struct glyph *glyph, *end;
19099
19100 eassert (it->glyph_row);
19101 eassert (it->glyph_row->reversed_p);
19102 eassert (it->area == TEXT_AREA);
19103 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19104
19105 if (n > it->glyph_row->used[TEXT_AREA])
19106 n = it->glyph_row->used[TEXT_AREA];
19107 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19108 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19109 for ( ; glyph < end; glyph++)
19110 glyph[-n] = *glyph;
19111 }
19112
19113 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19114 and ROW->maxpos. */
19115 static void
19116 find_row_edges (struct it *it, struct glyph_row *row,
19117 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19118 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19119 {
19120 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19121 lines' rows is implemented for bidi-reordered rows. */
19122
19123 /* ROW->minpos is the value of min_pos, the minimal buffer position
19124 we have in ROW, or ROW->start.pos if that is smaller. */
19125 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19126 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19127 else
19128 /* We didn't find buffer positions smaller than ROW->start, or
19129 didn't find _any_ valid buffer positions in any of the glyphs,
19130 so we must trust the iterator's computed positions. */
19131 row->minpos = row->start.pos;
19132 if (max_pos <= 0)
19133 {
19134 max_pos = CHARPOS (it->current.pos);
19135 max_bpos = BYTEPOS (it->current.pos);
19136 }
19137
19138 /* Here are the various use-cases for ending the row, and the
19139 corresponding values for ROW->maxpos:
19140
19141 Line ends in a newline from buffer eol_pos + 1
19142 Line is continued from buffer max_pos + 1
19143 Line is truncated on right it->current.pos
19144 Line ends in a newline from string max_pos + 1(*)
19145 (*) + 1 only when line ends in a forward scan
19146 Line is continued from string max_pos
19147 Line is continued from display vector max_pos
19148 Line is entirely from a string min_pos == max_pos
19149 Line is entirely from a display vector min_pos == max_pos
19150 Line that ends at ZV ZV
19151
19152 If you discover other use-cases, please add them here as
19153 appropriate. */
19154 if (row->ends_at_zv_p)
19155 row->maxpos = it->current.pos;
19156 else if (row->used[TEXT_AREA])
19157 {
19158 int seen_this_string = 0;
19159 struct glyph_row *r1 = row - 1;
19160
19161 /* Did we see the same display string on the previous row? */
19162 if (STRINGP (it->object)
19163 /* this is not the first row */
19164 && row > it->w->desired_matrix->rows
19165 /* previous row is not the header line */
19166 && !r1->mode_line_p
19167 /* previous row also ends in a newline from a string */
19168 && r1->ends_in_newline_from_string_p)
19169 {
19170 struct glyph *start, *end;
19171
19172 /* Search for the last glyph of the previous row that came
19173 from buffer or string. Depending on whether the row is
19174 L2R or R2L, we need to process it front to back or the
19175 other way round. */
19176 if (!r1->reversed_p)
19177 {
19178 start = r1->glyphs[TEXT_AREA];
19179 end = start + r1->used[TEXT_AREA];
19180 /* Glyphs inserted by redisplay have an integer (zero)
19181 as their object. */
19182 while (end > start
19183 && INTEGERP ((end - 1)->object)
19184 && (end - 1)->charpos <= 0)
19185 --end;
19186 if (end > start)
19187 {
19188 if (EQ ((end - 1)->object, it->object))
19189 seen_this_string = 1;
19190 }
19191 else
19192 /* If all the glyphs of the previous row were inserted
19193 by redisplay, it means the previous row was
19194 produced from a single newline, which is only
19195 possible if that newline came from the same string
19196 as the one which produced this ROW. */
19197 seen_this_string = 1;
19198 }
19199 else
19200 {
19201 end = r1->glyphs[TEXT_AREA] - 1;
19202 start = end + r1->used[TEXT_AREA];
19203 while (end < start
19204 && INTEGERP ((end + 1)->object)
19205 && (end + 1)->charpos <= 0)
19206 ++end;
19207 if (end < start)
19208 {
19209 if (EQ ((end + 1)->object, it->object))
19210 seen_this_string = 1;
19211 }
19212 else
19213 seen_this_string = 1;
19214 }
19215 }
19216 /* Take note of each display string that covers a newline only
19217 once, the first time we see it. This is for when a display
19218 string includes more than one newline in it. */
19219 if (row->ends_in_newline_from_string_p && !seen_this_string)
19220 {
19221 /* If we were scanning the buffer forward when we displayed
19222 the string, we want to account for at least one buffer
19223 position that belongs to this row (position covered by
19224 the display string), so that cursor positioning will
19225 consider this row as a candidate when point is at the end
19226 of the visual line represented by this row. This is not
19227 required when scanning back, because max_pos will already
19228 have a much larger value. */
19229 if (CHARPOS (row->end.pos) > max_pos)
19230 INC_BOTH (max_pos, max_bpos);
19231 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19232 }
19233 else if (CHARPOS (it->eol_pos) > 0)
19234 SET_TEXT_POS (row->maxpos,
19235 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19236 else if (row->continued_p)
19237 {
19238 /* If max_pos is different from IT's current position, it
19239 means IT->method does not belong to the display element
19240 at max_pos. However, it also means that the display
19241 element at max_pos was displayed in its entirety on this
19242 line, which is equivalent to saying that the next line
19243 starts at the next buffer position. */
19244 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19245 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19246 else
19247 {
19248 INC_BOTH (max_pos, max_bpos);
19249 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19250 }
19251 }
19252 else if (row->truncated_on_right_p)
19253 /* display_line already called reseat_at_next_visible_line_start,
19254 which puts the iterator at the beginning of the next line, in
19255 the logical order. */
19256 row->maxpos = it->current.pos;
19257 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19258 /* A line that is entirely from a string/image/stretch... */
19259 row->maxpos = row->minpos;
19260 else
19261 emacs_abort ();
19262 }
19263 else
19264 row->maxpos = it->current.pos;
19265 }
19266
19267 /* Construct the glyph row IT->glyph_row in the desired matrix of
19268 IT->w from text at the current position of IT. See dispextern.h
19269 for an overview of struct it. Value is non-zero if
19270 IT->glyph_row displays text, as opposed to a line displaying ZV
19271 only. */
19272
19273 static int
19274 display_line (struct it *it)
19275 {
19276 struct glyph_row *row = it->glyph_row;
19277 Lisp_Object overlay_arrow_string;
19278 struct it wrap_it;
19279 void *wrap_data = NULL;
19280 int may_wrap = 0, wrap_x IF_LINT (= 0);
19281 int wrap_row_used = -1;
19282 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19283 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19284 int wrap_row_extra_line_spacing IF_LINT (= 0);
19285 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19286 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19287 int cvpos;
19288 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19289 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19290
19291 /* We always start displaying at hpos zero even if hscrolled. */
19292 eassert (it->hpos == 0 && it->current_x == 0);
19293
19294 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19295 >= it->w->desired_matrix->nrows)
19296 {
19297 it->w->nrows_scale_factor++;
19298 fonts_changed_p = 1;
19299 return 0;
19300 }
19301
19302 /* Is IT->w showing the region? */
19303 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19304
19305 /* Clear the result glyph row and enable it. */
19306 prepare_desired_row (row);
19307
19308 row->y = it->current_y;
19309 row->start = it->start;
19310 row->continuation_lines_width = it->continuation_lines_width;
19311 row->displays_text_p = 1;
19312 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19313 it->starts_in_middle_of_char_p = 0;
19314
19315 /* Arrange the overlays nicely for our purposes. Usually, we call
19316 display_line on only one line at a time, in which case this
19317 can't really hurt too much, or we call it on lines which appear
19318 one after another in the buffer, in which case all calls to
19319 recenter_overlay_lists but the first will be pretty cheap. */
19320 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19321
19322 /* Move over display elements that are not visible because we are
19323 hscrolled. This may stop at an x-position < IT->first_visible_x
19324 if the first glyph is partially visible or if we hit a line end. */
19325 if (it->current_x < it->first_visible_x)
19326 {
19327 enum move_it_result move_result;
19328
19329 this_line_min_pos = row->start.pos;
19330 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19331 MOVE_TO_POS | MOVE_TO_X);
19332 /* If we are under a large hscroll, move_it_in_display_line_to
19333 could hit the end of the line without reaching
19334 it->first_visible_x. Pretend that we did reach it. This is
19335 especially important on a TTY, where we will call
19336 extend_face_to_end_of_line, which needs to know how many
19337 blank glyphs to produce. */
19338 if (it->current_x < it->first_visible_x
19339 && (move_result == MOVE_NEWLINE_OR_CR
19340 || move_result == MOVE_POS_MATCH_OR_ZV))
19341 it->current_x = it->first_visible_x;
19342
19343 /* Record the smallest positions seen while we moved over
19344 display elements that are not visible. This is needed by
19345 redisplay_internal for optimizing the case where the cursor
19346 stays inside the same line. The rest of this function only
19347 considers positions that are actually displayed, so
19348 RECORD_MAX_MIN_POS will not otherwise record positions that
19349 are hscrolled to the left of the left edge of the window. */
19350 min_pos = CHARPOS (this_line_min_pos);
19351 min_bpos = BYTEPOS (this_line_min_pos);
19352 }
19353 else
19354 {
19355 /* We only do this when not calling `move_it_in_display_line_to'
19356 above, because move_it_in_display_line_to calls
19357 handle_line_prefix itself. */
19358 handle_line_prefix (it);
19359 }
19360
19361 /* Get the initial row height. This is either the height of the
19362 text hscrolled, if there is any, or zero. */
19363 row->ascent = it->max_ascent;
19364 row->height = it->max_ascent + it->max_descent;
19365 row->phys_ascent = it->max_phys_ascent;
19366 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19367 row->extra_line_spacing = it->max_extra_line_spacing;
19368
19369 /* Utility macro to record max and min buffer positions seen until now. */
19370 #define RECORD_MAX_MIN_POS(IT) \
19371 do \
19372 { \
19373 int composition_p = !STRINGP ((IT)->string) \
19374 && ((IT)->what == IT_COMPOSITION); \
19375 ptrdiff_t current_pos = \
19376 composition_p ? (IT)->cmp_it.charpos \
19377 : IT_CHARPOS (*(IT)); \
19378 ptrdiff_t current_bpos = \
19379 composition_p ? CHAR_TO_BYTE (current_pos) \
19380 : IT_BYTEPOS (*(IT)); \
19381 if (current_pos < min_pos) \
19382 { \
19383 min_pos = current_pos; \
19384 min_bpos = current_bpos; \
19385 } \
19386 if (IT_CHARPOS (*it) > max_pos) \
19387 { \
19388 max_pos = IT_CHARPOS (*it); \
19389 max_bpos = IT_BYTEPOS (*it); \
19390 } \
19391 } \
19392 while (0)
19393
19394 /* Loop generating characters. The loop is left with IT on the next
19395 character to display. */
19396 while (1)
19397 {
19398 int n_glyphs_before, hpos_before, x_before;
19399 int x, nglyphs;
19400 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19401
19402 /* Retrieve the next thing to display. Value is zero if end of
19403 buffer reached. */
19404 if (!get_next_display_element (it))
19405 {
19406 /* Maybe add a space at the end of this line that is used to
19407 display the cursor there under X. Set the charpos of the
19408 first glyph of blank lines not corresponding to any text
19409 to -1. */
19410 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19411 row->exact_window_width_line_p = 1;
19412 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19413 || row->used[TEXT_AREA] == 0)
19414 {
19415 row->glyphs[TEXT_AREA]->charpos = -1;
19416 row->displays_text_p = 0;
19417
19418 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19419 && (!MINI_WINDOW_P (it->w)
19420 || (minibuf_level && EQ (it->window, minibuf_window))))
19421 row->indicate_empty_line_p = 1;
19422 }
19423
19424 it->continuation_lines_width = 0;
19425 row->ends_at_zv_p = 1;
19426 /* A row that displays right-to-left text must always have
19427 its last face extended all the way to the end of line,
19428 even if this row ends in ZV, because we still write to
19429 the screen left to right. We also need to extend the
19430 last face if the default face is remapped to some
19431 different face, otherwise the functions that clear
19432 portions of the screen will clear with the default face's
19433 background color. */
19434 if (row->reversed_p
19435 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19436 extend_face_to_end_of_line (it);
19437 break;
19438 }
19439
19440 /* Now, get the metrics of what we want to display. This also
19441 generates glyphs in `row' (which is IT->glyph_row). */
19442 n_glyphs_before = row->used[TEXT_AREA];
19443 x = it->current_x;
19444
19445 /* Remember the line height so far in case the next element doesn't
19446 fit on the line. */
19447 if (it->line_wrap != TRUNCATE)
19448 {
19449 ascent = it->max_ascent;
19450 descent = it->max_descent;
19451 phys_ascent = it->max_phys_ascent;
19452 phys_descent = it->max_phys_descent;
19453
19454 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19455 {
19456 if (IT_DISPLAYING_WHITESPACE (it))
19457 may_wrap = 1;
19458 else if (may_wrap)
19459 {
19460 SAVE_IT (wrap_it, *it, wrap_data);
19461 wrap_x = x;
19462 wrap_row_used = row->used[TEXT_AREA];
19463 wrap_row_ascent = row->ascent;
19464 wrap_row_height = row->height;
19465 wrap_row_phys_ascent = row->phys_ascent;
19466 wrap_row_phys_height = row->phys_height;
19467 wrap_row_extra_line_spacing = row->extra_line_spacing;
19468 wrap_row_min_pos = min_pos;
19469 wrap_row_min_bpos = min_bpos;
19470 wrap_row_max_pos = max_pos;
19471 wrap_row_max_bpos = max_bpos;
19472 may_wrap = 0;
19473 }
19474 }
19475 }
19476
19477 PRODUCE_GLYPHS (it);
19478
19479 /* If this display element was in marginal areas, continue with
19480 the next one. */
19481 if (it->area != TEXT_AREA)
19482 {
19483 row->ascent = max (row->ascent, it->max_ascent);
19484 row->height = max (row->height, it->max_ascent + it->max_descent);
19485 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19486 row->phys_height = max (row->phys_height,
19487 it->max_phys_ascent + it->max_phys_descent);
19488 row->extra_line_spacing = max (row->extra_line_spacing,
19489 it->max_extra_line_spacing);
19490 set_iterator_to_next (it, 1);
19491 continue;
19492 }
19493
19494 /* Does the display element fit on the line? If we truncate
19495 lines, we should draw past the right edge of the window. If
19496 we don't truncate, we want to stop so that we can display the
19497 continuation glyph before the right margin. If lines are
19498 continued, there are two possible strategies for characters
19499 resulting in more than 1 glyph (e.g. tabs): Display as many
19500 glyphs as possible in this line and leave the rest for the
19501 continuation line, or display the whole element in the next
19502 line. Original redisplay did the former, so we do it also. */
19503 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19504 hpos_before = it->hpos;
19505 x_before = x;
19506
19507 if (/* Not a newline. */
19508 nglyphs > 0
19509 /* Glyphs produced fit entirely in the line. */
19510 && it->current_x < it->last_visible_x)
19511 {
19512 it->hpos += nglyphs;
19513 row->ascent = max (row->ascent, it->max_ascent);
19514 row->height = max (row->height, it->max_ascent + it->max_descent);
19515 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19516 row->phys_height = max (row->phys_height,
19517 it->max_phys_ascent + it->max_phys_descent);
19518 row->extra_line_spacing = max (row->extra_line_spacing,
19519 it->max_extra_line_spacing);
19520 if (it->current_x - it->pixel_width < it->first_visible_x)
19521 row->x = x - it->first_visible_x;
19522 /* Record the maximum and minimum buffer positions seen so
19523 far in glyphs that will be displayed by this row. */
19524 if (it->bidi_p)
19525 RECORD_MAX_MIN_POS (it);
19526 }
19527 else
19528 {
19529 int i, new_x;
19530 struct glyph *glyph;
19531
19532 for (i = 0; i < nglyphs; ++i, x = new_x)
19533 {
19534 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19535 new_x = x + glyph->pixel_width;
19536
19537 if (/* Lines are continued. */
19538 it->line_wrap != TRUNCATE
19539 && (/* Glyph doesn't fit on the line. */
19540 new_x > it->last_visible_x
19541 /* Or it fits exactly on a window system frame. */
19542 || (new_x == it->last_visible_x
19543 && FRAME_WINDOW_P (it->f)
19544 && (row->reversed_p
19545 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19546 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19547 {
19548 /* End of a continued line. */
19549
19550 if (it->hpos == 0
19551 || (new_x == it->last_visible_x
19552 && FRAME_WINDOW_P (it->f)
19553 && (row->reversed_p
19554 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19555 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19556 {
19557 /* Current glyph is the only one on the line or
19558 fits exactly on the line. We must continue
19559 the line because we can't draw the cursor
19560 after the glyph. */
19561 row->continued_p = 1;
19562 it->current_x = new_x;
19563 it->continuation_lines_width += new_x;
19564 ++it->hpos;
19565 if (i == nglyphs - 1)
19566 {
19567 /* If line-wrap is on, check if a previous
19568 wrap point was found. */
19569 if (wrap_row_used > 0
19570 /* Even if there is a previous wrap
19571 point, continue the line here as
19572 usual, if (i) the previous character
19573 was a space or tab AND (ii) the
19574 current character is not. */
19575 && (!may_wrap
19576 || IT_DISPLAYING_WHITESPACE (it)))
19577 goto back_to_wrap;
19578
19579 /* Record the maximum and minimum buffer
19580 positions seen so far in glyphs that will be
19581 displayed by this row. */
19582 if (it->bidi_p)
19583 RECORD_MAX_MIN_POS (it);
19584 set_iterator_to_next (it, 1);
19585 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19586 {
19587 if (!get_next_display_element (it))
19588 {
19589 row->exact_window_width_line_p = 1;
19590 it->continuation_lines_width = 0;
19591 row->continued_p = 0;
19592 row->ends_at_zv_p = 1;
19593 }
19594 else if (ITERATOR_AT_END_OF_LINE_P (it))
19595 {
19596 row->continued_p = 0;
19597 row->exact_window_width_line_p = 1;
19598 }
19599 }
19600 }
19601 else if (it->bidi_p)
19602 RECORD_MAX_MIN_POS (it);
19603 }
19604 else if (CHAR_GLYPH_PADDING_P (*glyph)
19605 && !FRAME_WINDOW_P (it->f))
19606 {
19607 /* A padding glyph that doesn't fit on this line.
19608 This means the whole character doesn't fit
19609 on the line. */
19610 if (row->reversed_p)
19611 unproduce_glyphs (it, row->used[TEXT_AREA]
19612 - n_glyphs_before);
19613 row->used[TEXT_AREA] = n_glyphs_before;
19614
19615 /* Fill the rest of the row with continuation
19616 glyphs like in 20.x. */
19617 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19618 < row->glyphs[1 + TEXT_AREA])
19619 produce_special_glyphs (it, IT_CONTINUATION);
19620
19621 row->continued_p = 1;
19622 it->current_x = x_before;
19623 it->continuation_lines_width += x_before;
19624
19625 /* Restore the height to what it was before the
19626 element not fitting on the line. */
19627 it->max_ascent = ascent;
19628 it->max_descent = descent;
19629 it->max_phys_ascent = phys_ascent;
19630 it->max_phys_descent = phys_descent;
19631 }
19632 else if (wrap_row_used > 0)
19633 {
19634 back_to_wrap:
19635 if (row->reversed_p)
19636 unproduce_glyphs (it,
19637 row->used[TEXT_AREA] - wrap_row_used);
19638 RESTORE_IT (it, &wrap_it, wrap_data);
19639 it->continuation_lines_width += wrap_x;
19640 row->used[TEXT_AREA] = wrap_row_used;
19641 row->ascent = wrap_row_ascent;
19642 row->height = wrap_row_height;
19643 row->phys_ascent = wrap_row_phys_ascent;
19644 row->phys_height = wrap_row_phys_height;
19645 row->extra_line_spacing = wrap_row_extra_line_spacing;
19646 min_pos = wrap_row_min_pos;
19647 min_bpos = wrap_row_min_bpos;
19648 max_pos = wrap_row_max_pos;
19649 max_bpos = wrap_row_max_bpos;
19650 row->continued_p = 1;
19651 row->ends_at_zv_p = 0;
19652 row->exact_window_width_line_p = 0;
19653 it->continuation_lines_width += x;
19654
19655 /* Make sure that a non-default face is extended
19656 up to the right margin of the window. */
19657 extend_face_to_end_of_line (it);
19658 }
19659 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19660 {
19661 /* A TAB that extends past the right edge of the
19662 window. This produces a single glyph on
19663 window system frames. We leave the glyph in
19664 this row and let it fill the row, but don't
19665 consume the TAB. */
19666 if ((row->reversed_p
19667 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19668 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19669 produce_special_glyphs (it, IT_CONTINUATION);
19670 it->continuation_lines_width += it->last_visible_x;
19671 row->ends_in_middle_of_char_p = 1;
19672 row->continued_p = 1;
19673 glyph->pixel_width = it->last_visible_x - x;
19674 it->starts_in_middle_of_char_p = 1;
19675 }
19676 else
19677 {
19678 /* Something other than a TAB that draws past
19679 the right edge of the window. Restore
19680 positions to values before the element. */
19681 if (row->reversed_p)
19682 unproduce_glyphs (it, row->used[TEXT_AREA]
19683 - (n_glyphs_before + i));
19684 row->used[TEXT_AREA] = n_glyphs_before + i;
19685
19686 /* Display continuation glyphs. */
19687 it->current_x = x_before;
19688 it->continuation_lines_width += x;
19689 if (!FRAME_WINDOW_P (it->f)
19690 || (row->reversed_p
19691 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19692 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19693 produce_special_glyphs (it, IT_CONTINUATION);
19694 row->continued_p = 1;
19695
19696 extend_face_to_end_of_line (it);
19697
19698 if (nglyphs > 1 && i > 0)
19699 {
19700 row->ends_in_middle_of_char_p = 1;
19701 it->starts_in_middle_of_char_p = 1;
19702 }
19703
19704 /* Restore the height to what it was before the
19705 element not fitting on the line. */
19706 it->max_ascent = ascent;
19707 it->max_descent = descent;
19708 it->max_phys_ascent = phys_ascent;
19709 it->max_phys_descent = phys_descent;
19710 }
19711
19712 break;
19713 }
19714 else if (new_x > it->first_visible_x)
19715 {
19716 /* Increment number of glyphs actually displayed. */
19717 ++it->hpos;
19718
19719 /* Record the maximum and minimum buffer positions
19720 seen so far in glyphs that will be displayed by
19721 this row. */
19722 if (it->bidi_p)
19723 RECORD_MAX_MIN_POS (it);
19724
19725 if (x < it->first_visible_x)
19726 /* Glyph is partially visible, i.e. row starts at
19727 negative X position. */
19728 row->x = x - it->first_visible_x;
19729 }
19730 else
19731 {
19732 /* Glyph is completely off the left margin of the
19733 window. This should not happen because of the
19734 move_it_in_display_line at the start of this
19735 function, unless the text display area of the
19736 window is empty. */
19737 eassert (it->first_visible_x <= it->last_visible_x);
19738 }
19739 }
19740 /* Even if this display element produced no glyphs at all,
19741 we want to record its position. */
19742 if (it->bidi_p && nglyphs == 0)
19743 RECORD_MAX_MIN_POS (it);
19744
19745 row->ascent = max (row->ascent, it->max_ascent);
19746 row->height = max (row->height, it->max_ascent + it->max_descent);
19747 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19748 row->phys_height = max (row->phys_height,
19749 it->max_phys_ascent + it->max_phys_descent);
19750 row->extra_line_spacing = max (row->extra_line_spacing,
19751 it->max_extra_line_spacing);
19752
19753 /* End of this display line if row is continued. */
19754 if (row->continued_p || row->ends_at_zv_p)
19755 break;
19756 }
19757
19758 at_end_of_line:
19759 /* Is this a line end? If yes, we're also done, after making
19760 sure that a non-default face is extended up to the right
19761 margin of the window. */
19762 if (ITERATOR_AT_END_OF_LINE_P (it))
19763 {
19764 int used_before = row->used[TEXT_AREA];
19765
19766 row->ends_in_newline_from_string_p = STRINGP (it->object);
19767
19768 /* Add a space at the end of the line that is used to
19769 display the cursor there. */
19770 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19771 append_space_for_newline (it, 0);
19772
19773 /* Extend the face to the end of the line. */
19774 extend_face_to_end_of_line (it);
19775
19776 /* Make sure we have the position. */
19777 if (used_before == 0)
19778 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19779
19780 /* Record the position of the newline, for use in
19781 find_row_edges. */
19782 it->eol_pos = it->current.pos;
19783
19784 /* Consume the line end. This skips over invisible lines. */
19785 set_iterator_to_next (it, 1);
19786 it->continuation_lines_width = 0;
19787 break;
19788 }
19789
19790 /* Proceed with next display element. Note that this skips
19791 over lines invisible because of selective display. */
19792 set_iterator_to_next (it, 1);
19793
19794 /* If we truncate lines, we are done when the last displayed
19795 glyphs reach past the right margin of the window. */
19796 if (it->line_wrap == TRUNCATE
19797 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19798 ? (it->current_x >= it->last_visible_x)
19799 : (it->current_x > it->last_visible_x)))
19800 {
19801 /* Maybe add truncation glyphs. */
19802 if (!FRAME_WINDOW_P (it->f)
19803 || (row->reversed_p
19804 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19805 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19806 {
19807 int i, n;
19808
19809 if (!row->reversed_p)
19810 {
19811 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19812 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19813 break;
19814 }
19815 else
19816 {
19817 for (i = 0; i < row->used[TEXT_AREA]; i++)
19818 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19819 break;
19820 /* Remove any padding glyphs at the front of ROW, to
19821 make room for the truncation glyphs we will be
19822 adding below. The loop below always inserts at
19823 least one truncation glyph, so also remove the
19824 last glyph added to ROW. */
19825 unproduce_glyphs (it, i + 1);
19826 /* Adjust i for the loop below. */
19827 i = row->used[TEXT_AREA] - (i + 1);
19828 }
19829
19830 it->current_x = x_before;
19831 if (!FRAME_WINDOW_P (it->f))
19832 {
19833 for (n = row->used[TEXT_AREA]; i < n; ++i)
19834 {
19835 row->used[TEXT_AREA] = i;
19836 produce_special_glyphs (it, IT_TRUNCATION);
19837 }
19838 }
19839 else
19840 {
19841 row->used[TEXT_AREA] = i;
19842 produce_special_glyphs (it, IT_TRUNCATION);
19843 }
19844 }
19845 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19846 {
19847 /* Don't truncate if we can overflow newline into fringe. */
19848 if (!get_next_display_element (it))
19849 {
19850 it->continuation_lines_width = 0;
19851 row->ends_at_zv_p = 1;
19852 row->exact_window_width_line_p = 1;
19853 break;
19854 }
19855 if (ITERATOR_AT_END_OF_LINE_P (it))
19856 {
19857 row->exact_window_width_line_p = 1;
19858 goto at_end_of_line;
19859 }
19860 it->current_x = x_before;
19861 }
19862
19863 row->truncated_on_right_p = 1;
19864 it->continuation_lines_width = 0;
19865 reseat_at_next_visible_line_start (it, 0);
19866 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19867 it->hpos = hpos_before;
19868 break;
19869 }
19870 }
19871
19872 if (wrap_data)
19873 bidi_unshelve_cache (wrap_data, 1);
19874
19875 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19876 at the left window margin. */
19877 if (it->first_visible_x
19878 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19879 {
19880 if (!FRAME_WINDOW_P (it->f)
19881 || (row->reversed_p
19882 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19883 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19884 insert_left_trunc_glyphs (it);
19885 row->truncated_on_left_p = 1;
19886 }
19887
19888 /* Remember the position at which this line ends.
19889
19890 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19891 cannot be before the call to find_row_edges below, since that is
19892 where these positions are determined. */
19893 row->end = it->current;
19894 if (!it->bidi_p)
19895 {
19896 row->minpos = row->start.pos;
19897 row->maxpos = row->end.pos;
19898 }
19899 else
19900 {
19901 /* ROW->minpos and ROW->maxpos must be the smallest and
19902 `1 + the largest' buffer positions in ROW. But if ROW was
19903 bidi-reordered, these two positions can be anywhere in the
19904 row, so we must determine them now. */
19905 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19906 }
19907
19908 /* If the start of this line is the overlay arrow-position, then
19909 mark this glyph row as the one containing the overlay arrow.
19910 This is clearly a mess with variable size fonts. It would be
19911 better to let it be displayed like cursors under X. */
19912 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19913 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19914 !NILP (overlay_arrow_string)))
19915 {
19916 /* Overlay arrow in window redisplay is a fringe bitmap. */
19917 if (STRINGP (overlay_arrow_string))
19918 {
19919 struct glyph_row *arrow_row
19920 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19921 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19922 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19923 struct glyph *p = row->glyphs[TEXT_AREA];
19924 struct glyph *p2, *end;
19925
19926 /* Copy the arrow glyphs. */
19927 while (glyph < arrow_end)
19928 *p++ = *glyph++;
19929
19930 /* Throw away padding glyphs. */
19931 p2 = p;
19932 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19933 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19934 ++p2;
19935 if (p2 > p)
19936 {
19937 while (p2 < end)
19938 *p++ = *p2++;
19939 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19940 }
19941 }
19942 else
19943 {
19944 eassert (INTEGERP (overlay_arrow_string));
19945 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19946 }
19947 overlay_arrow_seen = 1;
19948 }
19949
19950 /* Highlight trailing whitespace. */
19951 if (!NILP (Vshow_trailing_whitespace))
19952 highlight_trailing_whitespace (it->f, it->glyph_row);
19953
19954 /* Compute pixel dimensions of this line. */
19955 compute_line_metrics (it);
19956
19957 /* Implementation note: No changes in the glyphs of ROW or in their
19958 faces can be done past this point, because compute_line_metrics
19959 computes ROW's hash value and stores it within the glyph_row
19960 structure. */
19961
19962 /* Record whether this row ends inside an ellipsis. */
19963 row->ends_in_ellipsis_p
19964 = (it->method == GET_FROM_DISPLAY_VECTOR
19965 && it->ellipsis_p);
19966
19967 /* Save fringe bitmaps in this row. */
19968 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19969 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19970 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19971 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19972
19973 it->left_user_fringe_bitmap = 0;
19974 it->left_user_fringe_face_id = 0;
19975 it->right_user_fringe_bitmap = 0;
19976 it->right_user_fringe_face_id = 0;
19977
19978 /* Maybe set the cursor. */
19979 cvpos = it->w->cursor.vpos;
19980 if ((cvpos < 0
19981 /* In bidi-reordered rows, keep checking for proper cursor
19982 position even if one has been found already, because buffer
19983 positions in such rows change non-linearly with ROW->VPOS,
19984 when a line is continued. One exception: when we are at ZV,
19985 display cursor on the first suitable glyph row, since all
19986 the empty rows after that also have their position set to ZV. */
19987 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19988 lines' rows is implemented for bidi-reordered rows. */
19989 || (it->bidi_p
19990 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19991 && PT >= MATRIX_ROW_START_CHARPOS (row)
19992 && PT <= MATRIX_ROW_END_CHARPOS (row)
19993 && cursor_row_p (row))
19994 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19995
19996 /* Prepare for the next line. This line starts horizontally at (X
19997 HPOS) = (0 0). Vertical positions are incremented. As a
19998 convenience for the caller, IT->glyph_row is set to the next
19999 row to be used. */
20000 it->current_x = it->hpos = 0;
20001 it->current_y += row->height;
20002 SET_TEXT_POS (it->eol_pos, 0, 0);
20003 ++it->vpos;
20004 ++it->glyph_row;
20005 /* The next row should by default use the same value of the
20006 reversed_p flag as this one. set_iterator_to_next decides when
20007 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20008 the flag accordingly. */
20009 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20010 it->glyph_row->reversed_p = row->reversed_p;
20011 it->start = row->end;
20012 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
20013
20014 #undef RECORD_MAX_MIN_POS
20015 }
20016
20017 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20018 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20019 doc: /* Return paragraph direction at point in BUFFER.
20020 Value is either `left-to-right' or `right-to-left'.
20021 If BUFFER is omitted or nil, it defaults to the current buffer.
20022
20023 Paragraph direction determines how the text in the paragraph is displayed.
20024 In left-to-right paragraphs, text begins at the left margin of the window
20025 and the reading direction is generally left to right. In right-to-left
20026 paragraphs, text begins at the right margin and is read from right to left.
20027
20028 See also `bidi-paragraph-direction'. */)
20029 (Lisp_Object buffer)
20030 {
20031 struct buffer *buf = current_buffer;
20032 struct buffer *old = buf;
20033
20034 if (! NILP (buffer))
20035 {
20036 CHECK_BUFFER (buffer);
20037 buf = XBUFFER (buffer);
20038 }
20039
20040 if (NILP (BVAR (buf, bidi_display_reordering))
20041 || NILP (BVAR (buf, enable_multibyte_characters))
20042 /* When we are loading loadup.el, the character property tables
20043 needed for bidi iteration are not yet available. */
20044 || !NILP (Vpurify_flag))
20045 return Qleft_to_right;
20046 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20047 return BVAR (buf, bidi_paragraph_direction);
20048 else
20049 {
20050 /* Determine the direction from buffer text. We could try to
20051 use current_matrix if it is up to date, but this seems fast
20052 enough as it is. */
20053 struct bidi_it itb;
20054 ptrdiff_t pos = BUF_PT (buf);
20055 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20056 int c;
20057 void *itb_data = bidi_shelve_cache ();
20058
20059 set_buffer_temp (buf);
20060 /* bidi_paragraph_init finds the base direction of the paragraph
20061 by searching forward from paragraph start. We need the base
20062 direction of the current or _previous_ paragraph, so we need
20063 to make sure we are within that paragraph. To that end, find
20064 the previous non-empty line. */
20065 if (pos >= ZV && pos > BEGV)
20066 DEC_BOTH (pos, bytepos);
20067 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20068 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20069 {
20070 while ((c = FETCH_BYTE (bytepos)) == '\n'
20071 || c == ' ' || c == '\t' || c == '\f')
20072 {
20073 if (bytepos <= BEGV_BYTE)
20074 break;
20075 bytepos--;
20076 pos--;
20077 }
20078 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20079 bytepos--;
20080 }
20081 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20082 itb.paragraph_dir = NEUTRAL_DIR;
20083 itb.string.s = NULL;
20084 itb.string.lstring = Qnil;
20085 itb.string.bufpos = 0;
20086 itb.string.unibyte = 0;
20087 /* We have no window to use here for ignoring window-specific
20088 overlays. Using NULL for window pointer will cause
20089 compute_display_string_pos to use the current buffer. */
20090 itb.w = NULL;
20091 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20092 bidi_unshelve_cache (itb_data, 0);
20093 set_buffer_temp (old);
20094 switch (itb.paragraph_dir)
20095 {
20096 case L2R:
20097 return Qleft_to_right;
20098 break;
20099 case R2L:
20100 return Qright_to_left;
20101 break;
20102 default:
20103 emacs_abort ();
20104 }
20105 }
20106 }
20107
20108 DEFUN ("move-point-visually", Fmove_point_visually,
20109 Smove_point_visually, 1, 1, 0,
20110 doc: /* Move point in the visual order in the specified DIRECTION.
20111 DIRECTION can be 1, meaning move to the right, or -1, which moves to the
20112 left.
20113
20114 Value is the new character position of point. */)
20115 (Lisp_Object direction)
20116 {
20117 struct window *w = XWINDOW (selected_window);
20118 struct buffer *b = NULL;
20119 struct glyph_row *row;
20120 int dir;
20121 Lisp_Object paragraph_dir;
20122
20123 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH) \
20124 (!(ROW)->continued_p \
20125 && INTEGERP ((GLYPH)->object) \
20126 && (GLYPH)->type == CHAR_GLYPH \
20127 && (GLYPH)->u.ch == ' ' \
20128 && (GLYPH)->charpos >= 0 \
20129 && !(GLYPH)->avoid_cursor_p)
20130
20131 CHECK_NUMBER (direction);
20132 dir = XINT (direction);
20133 if (dir > 0)
20134 dir = 1;
20135 else
20136 dir = -1;
20137
20138 if (BUFFERP (w->contents))
20139 b = XBUFFER (w->contents);
20140
20141 /* If current matrix is up-to-date, we can use the information
20142 recorded in the glyphs, at least as long as the goal is on the
20143 screen. */
20144 if (w->window_end_valid
20145 && !windows_or_buffers_changed
20146 && b
20147 && !b->clip_changed
20148 && !b->prevent_redisplay_optimizations_p
20149 && w->last_modified >= BUF_MODIFF (b)
20150 && w->last_overlay_modified >= BUF_OVERLAY_MODIFF (b)
20151 && w->cursor.vpos >= 0
20152 && w->cursor.vpos < w->current_matrix->nrows
20153 && (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos))->enabled_p)
20154 {
20155 struct glyph *g = row->glyphs[TEXT_AREA];
20156 struct glyph *e = dir > 0 ? g + row->used[TEXT_AREA] : g - 1;
20157 struct glyph *gpt = g + w->cursor.hpos;
20158
20159 for (g = gpt + dir; (dir > 0 ? g < e : g > e); g += dir)
20160 {
20161 if (BUFFERP (g->object) && g->charpos != PT)
20162 {
20163 SET_PT (g->charpos);
20164 w->cursor.vpos = -1;
20165 return make_number (PT);
20166 }
20167 else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
20168 {
20169 ptrdiff_t new_pos;
20170
20171 if (BUFFERP (gpt->object))
20172 {
20173 new_pos = PT;
20174 if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
20175 new_pos += (row->reversed_p ? -dir : dir);
20176 else
20177 new_pos -= (row->reversed_p ? -dir : dir);;
20178 }
20179 else if (BUFFERP (g->object))
20180 new_pos = g->charpos;
20181 else
20182 break;
20183 SET_PT (new_pos);
20184 w->cursor.vpos = -1;
20185 return make_number (PT);
20186 }
20187 else if (ROW_GLYPH_NEWLINE_P (row, g))
20188 {
20189 /* Glyphs inserted at the end of a non-empty line for
20190 positioning the cursor have zero charpos, so we must
20191 deduce the value of point by other means. */
20192 if (g->charpos > 0)
20193 SET_PT (g->charpos);
20194 else if (row->ends_at_zv_p && PT != ZV)
20195 SET_PT (ZV);
20196 else if (PT != MATRIX_ROW_END_CHARPOS (row) - 1)
20197 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20198 else
20199 break;
20200 w->cursor.vpos = -1;
20201 return make_number (PT);
20202 }
20203 }
20204 if (g == e || INTEGERP (g->object))
20205 {
20206 if (row->truncated_on_left_p || row->truncated_on_right_p)
20207 goto simulate_display;
20208 if (!row->reversed_p)
20209 row += dir;
20210 else
20211 row -= dir;
20212 if (row < MATRIX_FIRST_TEXT_ROW (w->current_matrix)
20213 || row > MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
20214 goto simulate_display;
20215
20216 if (dir > 0)
20217 {
20218 if (row->reversed_p && !row->continued_p)
20219 {
20220 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20221 w->cursor.vpos = -1;
20222 return make_number (PT);
20223 }
20224 g = row->glyphs[TEXT_AREA];
20225 e = g + row->used[TEXT_AREA];
20226 for ( ; g < e; g++)
20227 {
20228 if (BUFFERP (g->object)
20229 /* Empty lines have only one glyph, which stands
20230 for the newline, and whose charpos is the
20231 buffer position of the newline. */
20232 || ROW_GLYPH_NEWLINE_P (row, g)
20233 /* When the buffer ends in a newline, the line at
20234 EOB also has one glyph, but its charpos is -1. */
20235 || (row->ends_at_zv_p
20236 && !row->reversed_p
20237 && INTEGERP (g->object)
20238 && g->type == CHAR_GLYPH
20239 && g->u.ch == ' '))
20240 {
20241 if (g->charpos > 0)
20242 SET_PT (g->charpos);
20243 else if (!row->reversed_p
20244 && row->ends_at_zv_p
20245 && PT != ZV)
20246 SET_PT (ZV);
20247 else
20248 continue;
20249 w->cursor.vpos = -1;
20250 return make_number (PT);
20251 }
20252 }
20253 }
20254 else
20255 {
20256 if (!row->reversed_p && !row->continued_p)
20257 {
20258 SET_PT (MATRIX_ROW_END_CHARPOS (row) - 1);
20259 w->cursor.vpos = -1;
20260 return make_number (PT);
20261 }
20262 e = row->glyphs[TEXT_AREA];
20263 g = e + row->used[TEXT_AREA] - 1;
20264 for ( ; g >= e; g--)
20265 {
20266 if (BUFFERP (g->object)
20267 || (ROW_GLYPH_NEWLINE_P (row, g)
20268 && g->charpos > 0)
20269 /* Empty R2L lines on GUI frames have the buffer
20270 position of the newline stored in the stretch
20271 glyph. */
20272 || g->type == STRETCH_GLYPH
20273 || (row->ends_at_zv_p
20274 && row->reversed_p
20275 && INTEGERP (g->object)
20276 && g->type == CHAR_GLYPH
20277 && g->u.ch == ' '))
20278 {
20279 if (g->charpos > 0)
20280 SET_PT (g->charpos);
20281 else if (row->reversed_p
20282 && row->ends_at_zv_p
20283 && PT != ZV)
20284 SET_PT (ZV);
20285 else
20286 continue;
20287 w->cursor.vpos = -1;
20288 return make_number (PT);
20289 }
20290 }
20291 }
20292 }
20293 }
20294
20295 simulate_display:
20296
20297 /* If we wind up here, we failed to move by using the glyphs, so we
20298 need to simulate display instead. */
20299
20300 if (b)
20301 paragraph_dir = Fcurrent_bidi_paragraph_direction (w->contents);
20302 else
20303 paragraph_dir = Qleft_to_right;
20304 if (EQ (paragraph_dir, Qright_to_left))
20305 dir = -dir;
20306 if (PT <= BEGV && dir < 0)
20307 xsignal0 (Qbeginning_of_buffer);
20308 else if (PT >= ZV && dir > 0)
20309 xsignal0 (Qend_of_buffer);
20310 else
20311 {
20312 struct text_pos pt;
20313 struct it it;
20314 int pt_x, target_x, pixel_width, pt_vpos;
20315 bool at_eol_p;
20316 bool overshoot_expected = false;
20317 bool target_is_eol_p = false;
20318
20319 /* Setup the arena. */
20320 SET_TEXT_POS (pt, PT, PT_BYTE);
20321 start_display (&it, w, pt);
20322
20323 if (it.cmp_it.id < 0
20324 && it.method == GET_FROM_STRING
20325 && it.area == TEXT_AREA
20326 && it.string_from_display_prop_p
20327 && (it.sp > 0 && it.stack[it.sp - 1].method == GET_FROM_BUFFER))
20328 overshoot_expected = true;
20329
20330 /* Find the X coordinate of point. We start from the beginning
20331 of this or previous line to make sure we are before point in
20332 the logical order (since the move_it_* functions can only
20333 move forward). */
20334 reseat_at_previous_visible_line_start (&it);
20335 it.current_x = it.hpos = it.current_y = it.vpos = 0;
20336 if (IT_CHARPOS (it) != PT)
20337 move_it_to (&it, overshoot_expected ? PT - 1 : PT,
20338 -1, -1, -1, MOVE_TO_POS);
20339 pt_x = it.current_x;
20340 pt_vpos = it.vpos;
20341 if (dir > 0 || overshoot_expected)
20342 {
20343 struct glyph_row *row = it.glyph_row;
20344
20345 /* When point is at beginning of line, we don't have
20346 information about the glyph there loaded into struct
20347 it. Calling get_next_display_element fixes that. */
20348 if (pt_x == 0)
20349 get_next_display_element (&it);
20350 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20351 it.glyph_row = NULL;
20352 PRODUCE_GLYPHS (&it); /* compute it.pixel_width */
20353 it.glyph_row = row;
20354 /* PRODUCE_GLYPHS advances it.current_x, so we must restore
20355 it, lest it will become out of sync with it's buffer
20356 position. */
20357 it.current_x = pt_x;
20358 }
20359 else
20360 at_eol_p = ITERATOR_AT_END_OF_LINE_P (&it);
20361 pixel_width = it.pixel_width;
20362 if (overshoot_expected && at_eol_p)
20363 pixel_width = 0;
20364 else if (pixel_width <= 0)
20365 pixel_width = 1;
20366
20367 /* If there's a display string at point, we are actually at the
20368 glyph to the left of point, so we need to correct the X
20369 coordinate. */
20370 if (overshoot_expected)
20371 pt_x += pixel_width;
20372
20373 /* Compute target X coordinate, either to the left or to the
20374 right of point. On TTY frames, all characters have the same
20375 pixel width of 1, so we can use that. On GUI frames we don't
20376 have an easy way of getting at the pixel width of the
20377 character to the left of point, so we use a different method
20378 of getting to that place. */
20379 if (dir > 0)
20380 target_x = pt_x + pixel_width;
20381 else
20382 target_x = pt_x - (!FRAME_WINDOW_P (it.f)) * pixel_width;
20383
20384 /* Target X coordinate could be one line above or below the line
20385 of point, in which case we need to adjust the target X
20386 coordinate. Also, if moving to the left, we need to begin at
20387 the left edge of the point's screen line. */
20388 if (dir < 0)
20389 {
20390 if (pt_x > 0)
20391 {
20392 start_display (&it, w, pt);
20393 reseat_at_previous_visible_line_start (&it);
20394 it.current_x = it.current_y = it.hpos = 0;
20395 if (pt_vpos != 0)
20396 move_it_by_lines (&it, pt_vpos);
20397 }
20398 else
20399 {
20400 move_it_by_lines (&it, -1);
20401 target_x = it.last_visible_x - !FRAME_WINDOW_P (it.f);
20402 target_is_eol_p = true;
20403 }
20404 }
20405 else
20406 {
20407 if (at_eol_p
20408 || (target_x >= it.last_visible_x
20409 && it.line_wrap != TRUNCATE))
20410 {
20411 if (pt_x > 0)
20412 move_it_by_lines (&it, 0);
20413 move_it_by_lines (&it, 1);
20414 target_x = 0;
20415 }
20416 }
20417
20418 /* Move to the target X coordinate. */
20419 #ifdef HAVE_WINDOW_SYSTEM
20420 /* On GUI frames, as we don't know the X coordinate of the
20421 character to the left of point, moving point to the left
20422 requires walking, one grapheme cluster at a time, until we
20423 find ourself at a place immediately to the left of the
20424 character at point. */
20425 if (FRAME_WINDOW_P (it.f) && dir < 0)
20426 {
20427 struct text_pos new_pos = it.current.pos;
20428 enum move_it_result rc = MOVE_X_REACHED;
20429
20430 while (it.current_x + it.pixel_width <= target_x
20431 && rc == MOVE_X_REACHED)
20432 {
20433 int new_x = it.current_x + it.pixel_width;
20434
20435 new_pos = it.current.pos;
20436 if (new_x == it.current_x)
20437 new_x++;
20438 rc = move_it_in_display_line_to (&it, ZV, new_x,
20439 MOVE_TO_POS | MOVE_TO_X);
20440 if (ITERATOR_AT_END_OF_LINE_P (&it) && !target_is_eol_p)
20441 break;
20442 }
20443 /* If we ended up on a composed character inside
20444 bidi-reordered text (e.g., Hebrew text with diacritics),
20445 the iterator gives us the buffer position of the last (in
20446 logical order) character of the composed grapheme cluster,
20447 which is not what we want. So we cheat: we compute the
20448 character position of the character that follows (in the
20449 logical order) the one where the above loop stopped. That
20450 character will appear on display to the left of point. */
20451 if (it.bidi_p
20452 && it.bidi_it.scan_dir == -1
20453 && new_pos.charpos - IT_CHARPOS (it) > 1)
20454 {
20455 new_pos.charpos = IT_CHARPOS (it) + 1;
20456 new_pos.bytepos = CHAR_TO_BYTE (new_pos.charpos);
20457 }
20458 it.current.pos = new_pos;
20459 }
20460 else
20461 #endif
20462 if (it.current_x != target_x)
20463 move_it_in_display_line_to (&it, ZV, target_x, MOVE_TO_POS | MOVE_TO_X);
20464
20465 /* When lines are truncated, the above loop will stop at the
20466 window edge. But we want to get to the end of line, even if
20467 it is beyond the window edge; automatic hscroll will then
20468 scroll the window to show point as appropriate. */
20469 if (target_is_eol_p && it.line_wrap == TRUNCATE
20470 && get_next_display_element (&it))
20471 {
20472 struct text_pos new_pos = it.current.pos;
20473
20474 while (!ITERATOR_AT_END_OF_LINE_P (&it))
20475 {
20476 set_iterator_to_next (&it, 0);
20477 if (it.method == GET_FROM_BUFFER)
20478 new_pos = it.current.pos;
20479 if (!get_next_display_element (&it))
20480 break;
20481 }
20482
20483 it.current.pos = new_pos;
20484 }
20485
20486 /* If we ended up in a display string that covers point, move to
20487 buffer position to the right in the visual order. */
20488 if (dir > 0)
20489 {
20490 while (IT_CHARPOS (it) == PT)
20491 {
20492 set_iterator_to_next (&it, 0);
20493 if (!get_next_display_element (&it))
20494 break;
20495 }
20496 }
20497
20498 /* Move point to that position. */
20499 SET_PT_BOTH (IT_CHARPOS (it), IT_BYTEPOS (it));
20500 }
20501
20502 return make_number (PT);
20503
20504 #undef ROW_GLYPH_NEWLINE_P
20505 }
20506
20507 \f
20508 /***********************************************************************
20509 Menu Bar
20510 ***********************************************************************/
20511
20512 /* Redisplay the menu bar in the frame for window W.
20513
20514 The menu bar of X frames that don't have X toolkit support is
20515 displayed in a special window W->frame->menu_bar_window.
20516
20517 The menu bar of terminal frames is treated specially as far as
20518 glyph matrices are concerned. Menu bar lines are not part of
20519 windows, so the update is done directly on the frame matrix rows
20520 for the menu bar. */
20521
20522 static void
20523 display_menu_bar (struct window *w)
20524 {
20525 struct frame *f = XFRAME (WINDOW_FRAME (w));
20526 struct it it;
20527 Lisp_Object items;
20528 int i;
20529
20530 /* Don't do all this for graphical frames. */
20531 #ifdef HAVE_NTGUI
20532 if (FRAME_W32_P (f))
20533 return;
20534 #endif
20535 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20536 if (FRAME_X_P (f))
20537 return;
20538 #endif
20539
20540 #ifdef HAVE_NS
20541 if (FRAME_NS_P (f))
20542 return;
20543 #endif /* HAVE_NS */
20544
20545 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20546 eassert (!FRAME_WINDOW_P (f));
20547 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20548 it.first_visible_x = 0;
20549 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20550 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20551 if (FRAME_WINDOW_P (f))
20552 {
20553 /* Menu bar lines are displayed in the desired matrix of the
20554 dummy window menu_bar_window. */
20555 struct window *menu_w;
20556 menu_w = XWINDOW (f->menu_bar_window);
20557 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20558 MENU_FACE_ID);
20559 it.first_visible_x = 0;
20560 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20561 }
20562 else
20563 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20564 {
20565 /* This is a TTY frame, i.e. character hpos/vpos are used as
20566 pixel x/y. */
20567 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20568 MENU_FACE_ID);
20569 it.first_visible_x = 0;
20570 it.last_visible_x = FRAME_COLS (f);
20571 }
20572
20573 /* FIXME: This should be controlled by a user option. See the
20574 comments in redisplay_tool_bar and display_mode_line about
20575 this. */
20576 it.paragraph_embedding = L2R;
20577
20578 /* Clear all rows of the menu bar. */
20579 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20580 {
20581 struct glyph_row *row = it.glyph_row + i;
20582 clear_glyph_row (row);
20583 row->enabled_p = 1;
20584 row->full_width_p = 1;
20585 }
20586
20587 /* Display all items of the menu bar. */
20588 items = FRAME_MENU_BAR_ITEMS (it.f);
20589 for (i = 0; i < ASIZE (items); i += 4)
20590 {
20591 Lisp_Object string;
20592
20593 /* Stop at nil string. */
20594 string = AREF (items, i + 1);
20595 if (NILP (string))
20596 break;
20597
20598 /* Remember where item was displayed. */
20599 ASET (items, i + 3, make_number (it.hpos));
20600
20601 /* Display the item, pad with one space. */
20602 if (it.current_x < it.last_visible_x)
20603 display_string (NULL, string, Qnil, 0, 0, &it,
20604 SCHARS (string) + 1, 0, 0, -1);
20605 }
20606
20607 /* Fill out the line with spaces. */
20608 if (it.current_x < it.last_visible_x)
20609 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20610
20611 /* Compute the total height of the lines. */
20612 compute_line_metrics (&it);
20613 }
20614
20615
20616 \f
20617 /***********************************************************************
20618 Mode Line
20619 ***********************************************************************/
20620
20621 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20622 FORCE is non-zero, redisplay mode lines unconditionally.
20623 Otherwise, redisplay only mode lines that are garbaged. Value is
20624 the number of windows whose mode lines were redisplayed. */
20625
20626 static int
20627 redisplay_mode_lines (Lisp_Object window, int force)
20628 {
20629 int nwindows = 0;
20630
20631 while (!NILP (window))
20632 {
20633 struct window *w = XWINDOW (window);
20634
20635 if (WINDOWP (w->contents))
20636 nwindows += redisplay_mode_lines (w->contents, force);
20637 else if (force
20638 || FRAME_GARBAGED_P (XFRAME (w->frame))
20639 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20640 {
20641 struct text_pos lpoint;
20642 struct buffer *old = current_buffer;
20643
20644 /* Set the window's buffer for the mode line display. */
20645 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20646 set_buffer_internal_1 (XBUFFER (w->contents));
20647
20648 /* Point refers normally to the selected window. For any
20649 other window, set up appropriate value. */
20650 if (!EQ (window, selected_window))
20651 {
20652 struct text_pos pt;
20653
20654 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20655 if (CHARPOS (pt) < BEGV)
20656 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20657 else if (CHARPOS (pt) > (ZV - 1))
20658 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20659 else
20660 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20661 }
20662
20663 /* Display mode lines. */
20664 clear_glyph_matrix (w->desired_matrix);
20665 if (display_mode_lines (w))
20666 {
20667 ++nwindows;
20668 w->must_be_updated_p = 1;
20669 }
20670
20671 /* Restore old settings. */
20672 set_buffer_internal_1 (old);
20673 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20674 }
20675
20676 window = w->next;
20677 }
20678
20679 return nwindows;
20680 }
20681
20682
20683 /* Display the mode and/or header line of window W. Value is the
20684 sum number of mode lines and header lines displayed. */
20685
20686 static int
20687 display_mode_lines (struct window *w)
20688 {
20689 Lisp_Object old_selected_window = selected_window;
20690 Lisp_Object old_selected_frame = selected_frame;
20691 Lisp_Object new_frame = w->frame;
20692 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20693 int n = 0;
20694
20695 selected_frame = new_frame;
20696 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20697 or window's point, then we'd need select_window_1 here as well. */
20698 XSETWINDOW (selected_window, w);
20699 XFRAME (new_frame)->selected_window = selected_window;
20700
20701 /* These will be set while the mode line specs are processed. */
20702 line_number_displayed = 0;
20703 w->column_number_displayed = -1;
20704
20705 if (WINDOW_WANTS_MODELINE_P (w))
20706 {
20707 struct window *sel_w = XWINDOW (old_selected_window);
20708
20709 /* Select mode line face based on the real selected window. */
20710 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20711 BVAR (current_buffer, mode_line_format));
20712 ++n;
20713 }
20714
20715 if (WINDOW_WANTS_HEADER_LINE_P (w))
20716 {
20717 display_mode_line (w, HEADER_LINE_FACE_ID,
20718 BVAR (current_buffer, header_line_format));
20719 ++n;
20720 }
20721
20722 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20723 selected_frame = old_selected_frame;
20724 selected_window = old_selected_window;
20725 return n;
20726 }
20727
20728
20729 /* Display mode or header line of window W. FACE_ID specifies which
20730 line to display; it is either MODE_LINE_FACE_ID or
20731 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20732 display. Value is the pixel height of the mode/header line
20733 displayed. */
20734
20735 static int
20736 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20737 {
20738 struct it it;
20739 struct face *face;
20740 ptrdiff_t count = SPECPDL_INDEX ();
20741
20742 init_iterator (&it, w, -1, -1, NULL, face_id);
20743 /* Don't extend on a previously drawn mode-line.
20744 This may happen if called from pos_visible_p. */
20745 it.glyph_row->enabled_p = 0;
20746 prepare_desired_row (it.glyph_row);
20747
20748 it.glyph_row->mode_line_p = 1;
20749
20750 /* FIXME: This should be controlled by a user option. But
20751 supporting such an option is not trivial, since the mode line is
20752 made up of many separate strings. */
20753 it.paragraph_embedding = L2R;
20754
20755 record_unwind_protect (unwind_format_mode_line,
20756 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20757
20758 mode_line_target = MODE_LINE_DISPLAY;
20759
20760 /* Temporarily make frame's keyboard the current kboard so that
20761 kboard-local variables in the mode_line_format will get the right
20762 values. */
20763 push_kboard (FRAME_KBOARD (it.f));
20764 record_unwind_save_match_data ();
20765 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20766 pop_kboard ();
20767
20768 unbind_to (count, Qnil);
20769
20770 /* Fill up with spaces. */
20771 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20772
20773 compute_line_metrics (&it);
20774 it.glyph_row->full_width_p = 1;
20775 it.glyph_row->continued_p = 0;
20776 it.glyph_row->truncated_on_left_p = 0;
20777 it.glyph_row->truncated_on_right_p = 0;
20778
20779 /* Make a 3D mode-line have a shadow at its right end. */
20780 face = FACE_FROM_ID (it.f, face_id);
20781 extend_face_to_end_of_line (&it);
20782 if (face->box != FACE_NO_BOX)
20783 {
20784 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20785 + it.glyph_row->used[TEXT_AREA] - 1);
20786 last->right_box_line_p = 1;
20787 }
20788
20789 return it.glyph_row->height;
20790 }
20791
20792 /* Move element ELT in LIST to the front of LIST.
20793 Return the updated list. */
20794
20795 static Lisp_Object
20796 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20797 {
20798 register Lisp_Object tail, prev;
20799 register Lisp_Object tem;
20800
20801 tail = list;
20802 prev = Qnil;
20803 while (CONSP (tail))
20804 {
20805 tem = XCAR (tail);
20806
20807 if (EQ (elt, tem))
20808 {
20809 /* Splice out the link TAIL. */
20810 if (NILP (prev))
20811 list = XCDR (tail);
20812 else
20813 Fsetcdr (prev, XCDR (tail));
20814
20815 /* Now make it the first. */
20816 Fsetcdr (tail, list);
20817 return tail;
20818 }
20819 else
20820 prev = tail;
20821 tail = XCDR (tail);
20822 QUIT;
20823 }
20824
20825 /* Not found--return unchanged LIST. */
20826 return list;
20827 }
20828
20829 /* Contribute ELT to the mode line for window IT->w. How it
20830 translates into text depends on its data type.
20831
20832 IT describes the display environment in which we display, as usual.
20833
20834 DEPTH is the depth in recursion. It is used to prevent
20835 infinite recursion here.
20836
20837 FIELD_WIDTH is the number of characters the display of ELT should
20838 occupy in the mode line, and PRECISION is the maximum number of
20839 characters to display from ELT's representation. See
20840 display_string for details.
20841
20842 Returns the hpos of the end of the text generated by ELT.
20843
20844 PROPS is a property list to add to any string we encounter.
20845
20846 If RISKY is nonzero, remove (disregard) any properties in any string
20847 we encounter, and ignore :eval and :propertize.
20848
20849 The global variable `mode_line_target' determines whether the
20850 output is passed to `store_mode_line_noprop',
20851 `store_mode_line_string', or `display_string'. */
20852
20853 static int
20854 display_mode_element (struct it *it, int depth, int field_width, int precision,
20855 Lisp_Object elt, Lisp_Object props, int risky)
20856 {
20857 int n = 0, field, prec;
20858 int literal = 0;
20859
20860 tail_recurse:
20861 if (depth > 100)
20862 elt = build_string ("*too-deep*");
20863
20864 depth++;
20865
20866 switch (XTYPE (elt))
20867 {
20868 case Lisp_String:
20869 {
20870 /* A string: output it and check for %-constructs within it. */
20871 unsigned char c;
20872 ptrdiff_t offset = 0;
20873
20874 if (SCHARS (elt) > 0
20875 && (!NILP (props) || risky))
20876 {
20877 Lisp_Object oprops, aelt;
20878 oprops = Ftext_properties_at (make_number (0), elt);
20879
20880 /* If the starting string's properties are not what
20881 we want, translate the string. Also, if the string
20882 is risky, do that anyway. */
20883
20884 if (NILP (Fequal (props, oprops)) || risky)
20885 {
20886 /* If the starting string has properties,
20887 merge the specified ones onto the existing ones. */
20888 if (! NILP (oprops) && !risky)
20889 {
20890 Lisp_Object tem;
20891
20892 oprops = Fcopy_sequence (oprops);
20893 tem = props;
20894 while (CONSP (tem))
20895 {
20896 oprops = Fplist_put (oprops, XCAR (tem),
20897 XCAR (XCDR (tem)));
20898 tem = XCDR (XCDR (tem));
20899 }
20900 props = oprops;
20901 }
20902
20903 aelt = Fassoc (elt, mode_line_proptrans_alist);
20904 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20905 {
20906 /* AELT is what we want. Move it to the front
20907 without consing. */
20908 elt = XCAR (aelt);
20909 mode_line_proptrans_alist
20910 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20911 }
20912 else
20913 {
20914 Lisp_Object tem;
20915
20916 /* If AELT has the wrong props, it is useless.
20917 so get rid of it. */
20918 if (! NILP (aelt))
20919 mode_line_proptrans_alist
20920 = Fdelq (aelt, mode_line_proptrans_alist);
20921
20922 elt = Fcopy_sequence (elt);
20923 Fset_text_properties (make_number (0), Flength (elt),
20924 props, elt);
20925 /* Add this item to mode_line_proptrans_alist. */
20926 mode_line_proptrans_alist
20927 = Fcons (Fcons (elt, props),
20928 mode_line_proptrans_alist);
20929 /* Truncate mode_line_proptrans_alist
20930 to at most 50 elements. */
20931 tem = Fnthcdr (make_number (50),
20932 mode_line_proptrans_alist);
20933 if (! NILP (tem))
20934 XSETCDR (tem, Qnil);
20935 }
20936 }
20937 }
20938
20939 offset = 0;
20940
20941 if (literal)
20942 {
20943 prec = precision - n;
20944 switch (mode_line_target)
20945 {
20946 case MODE_LINE_NOPROP:
20947 case MODE_LINE_TITLE:
20948 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20949 break;
20950 case MODE_LINE_STRING:
20951 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20952 break;
20953 case MODE_LINE_DISPLAY:
20954 n += display_string (NULL, elt, Qnil, 0, 0, it,
20955 0, prec, 0, STRING_MULTIBYTE (elt));
20956 break;
20957 }
20958
20959 break;
20960 }
20961
20962 /* Handle the non-literal case. */
20963
20964 while ((precision <= 0 || n < precision)
20965 && SREF (elt, offset) != 0
20966 && (mode_line_target != MODE_LINE_DISPLAY
20967 || it->current_x < it->last_visible_x))
20968 {
20969 ptrdiff_t last_offset = offset;
20970
20971 /* Advance to end of string or next format specifier. */
20972 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20973 ;
20974
20975 if (offset - 1 != last_offset)
20976 {
20977 ptrdiff_t nchars, nbytes;
20978
20979 /* Output to end of string or up to '%'. Field width
20980 is length of string. Don't output more than
20981 PRECISION allows us. */
20982 offset--;
20983
20984 prec = c_string_width (SDATA (elt) + last_offset,
20985 offset - last_offset, precision - n,
20986 &nchars, &nbytes);
20987
20988 switch (mode_line_target)
20989 {
20990 case MODE_LINE_NOPROP:
20991 case MODE_LINE_TITLE:
20992 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20993 break;
20994 case MODE_LINE_STRING:
20995 {
20996 ptrdiff_t bytepos = last_offset;
20997 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20998 ptrdiff_t endpos = (precision <= 0
20999 ? string_byte_to_char (elt, offset)
21000 : charpos + nchars);
21001
21002 n += store_mode_line_string (NULL,
21003 Fsubstring (elt, make_number (charpos),
21004 make_number (endpos)),
21005 0, 0, 0, Qnil);
21006 }
21007 break;
21008 case MODE_LINE_DISPLAY:
21009 {
21010 ptrdiff_t bytepos = last_offset;
21011 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
21012
21013 if (precision <= 0)
21014 nchars = string_byte_to_char (elt, offset) - charpos;
21015 n += display_string (NULL, elt, Qnil, 0, charpos,
21016 it, 0, nchars, 0,
21017 STRING_MULTIBYTE (elt));
21018 }
21019 break;
21020 }
21021 }
21022 else /* c == '%' */
21023 {
21024 ptrdiff_t percent_position = offset;
21025
21026 /* Get the specified minimum width. Zero means
21027 don't pad. */
21028 field = 0;
21029 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
21030 field = field * 10 + c - '0';
21031
21032 /* Don't pad beyond the total padding allowed. */
21033 if (field_width - n > 0 && field > field_width - n)
21034 field = field_width - n;
21035
21036 /* Note that either PRECISION <= 0 or N < PRECISION. */
21037 prec = precision - n;
21038
21039 if (c == 'M')
21040 n += display_mode_element (it, depth, field, prec,
21041 Vglobal_mode_string, props,
21042 risky);
21043 else if (c != 0)
21044 {
21045 bool multibyte;
21046 ptrdiff_t bytepos, charpos;
21047 const char *spec;
21048 Lisp_Object string;
21049
21050 bytepos = percent_position;
21051 charpos = (STRING_MULTIBYTE (elt)
21052 ? string_byte_to_char (elt, bytepos)
21053 : bytepos);
21054 spec = decode_mode_spec (it->w, c, field, &string);
21055 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
21056
21057 switch (mode_line_target)
21058 {
21059 case MODE_LINE_NOPROP:
21060 case MODE_LINE_TITLE:
21061 n += store_mode_line_noprop (spec, field, prec);
21062 break;
21063 case MODE_LINE_STRING:
21064 {
21065 Lisp_Object tem = build_string (spec);
21066 props = Ftext_properties_at (make_number (charpos), elt);
21067 /* Should only keep face property in props */
21068 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
21069 }
21070 break;
21071 case MODE_LINE_DISPLAY:
21072 {
21073 int nglyphs_before, nwritten;
21074
21075 nglyphs_before = it->glyph_row->used[TEXT_AREA];
21076 nwritten = display_string (spec, string, elt,
21077 charpos, 0, it,
21078 field, prec, 0,
21079 multibyte);
21080
21081 /* Assign to the glyphs written above the
21082 string where the `%x' came from, position
21083 of the `%'. */
21084 if (nwritten > 0)
21085 {
21086 struct glyph *glyph
21087 = (it->glyph_row->glyphs[TEXT_AREA]
21088 + nglyphs_before);
21089 int i;
21090
21091 for (i = 0; i < nwritten; ++i)
21092 {
21093 glyph[i].object = elt;
21094 glyph[i].charpos = charpos;
21095 }
21096
21097 n += nwritten;
21098 }
21099 }
21100 break;
21101 }
21102 }
21103 else /* c == 0 */
21104 break;
21105 }
21106 }
21107 }
21108 break;
21109
21110 case Lisp_Symbol:
21111 /* A symbol: process the value of the symbol recursively
21112 as if it appeared here directly. Avoid error if symbol void.
21113 Special case: if value of symbol is a string, output the string
21114 literally. */
21115 {
21116 register Lisp_Object tem;
21117
21118 /* If the variable is not marked as risky to set
21119 then its contents are risky to use. */
21120 if (NILP (Fget (elt, Qrisky_local_variable)))
21121 risky = 1;
21122
21123 tem = Fboundp (elt);
21124 if (!NILP (tem))
21125 {
21126 tem = Fsymbol_value (elt);
21127 /* If value is a string, output that string literally:
21128 don't check for % within it. */
21129 if (STRINGP (tem))
21130 literal = 1;
21131
21132 if (!EQ (tem, elt))
21133 {
21134 /* Give up right away for nil or t. */
21135 elt = tem;
21136 goto tail_recurse;
21137 }
21138 }
21139 }
21140 break;
21141
21142 case Lisp_Cons:
21143 {
21144 register Lisp_Object car, tem;
21145
21146 /* A cons cell: five distinct cases.
21147 If first element is :eval or :propertize, do something special.
21148 If first element is a string or a cons, process all the elements
21149 and effectively concatenate them.
21150 If first element is a negative number, truncate displaying cdr to
21151 at most that many characters. If positive, pad (with spaces)
21152 to at least that many characters.
21153 If first element is a symbol, process the cadr or caddr recursively
21154 according to whether the symbol's value is non-nil or nil. */
21155 car = XCAR (elt);
21156 if (EQ (car, QCeval))
21157 {
21158 /* An element of the form (:eval FORM) means evaluate FORM
21159 and use the result as mode line elements. */
21160
21161 if (risky)
21162 break;
21163
21164 if (CONSP (XCDR (elt)))
21165 {
21166 Lisp_Object spec;
21167 spec = safe_eval (XCAR (XCDR (elt)));
21168 n += display_mode_element (it, depth, field_width - n,
21169 precision - n, spec, props,
21170 risky);
21171 }
21172 }
21173 else if (EQ (car, QCpropertize))
21174 {
21175 /* An element of the form (:propertize ELT PROPS...)
21176 means display ELT but applying properties PROPS. */
21177
21178 if (risky)
21179 break;
21180
21181 if (CONSP (XCDR (elt)))
21182 n += display_mode_element (it, depth, field_width - n,
21183 precision - n, XCAR (XCDR (elt)),
21184 XCDR (XCDR (elt)), risky);
21185 }
21186 else if (SYMBOLP (car))
21187 {
21188 tem = Fboundp (car);
21189 elt = XCDR (elt);
21190 if (!CONSP (elt))
21191 goto invalid;
21192 /* elt is now the cdr, and we know it is a cons cell.
21193 Use its car if CAR has a non-nil value. */
21194 if (!NILP (tem))
21195 {
21196 tem = Fsymbol_value (car);
21197 if (!NILP (tem))
21198 {
21199 elt = XCAR (elt);
21200 goto tail_recurse;
21201 }
21202 }
21203 /* Symbol's value is nil (or symbol is unbound)
21204 Get the cddr of the original list
21205 and if possible find the caddr and use that. */
21206 elt = XCDR (elt);
21207 if (NILP (elt))
21208 break;
21209 else if (!CONSP (elt))
21210 goto invalid;
21211 elt = XCAR (elt);
21212 goto tail_recurse;
21213 }
21214 else if (INTEGERP (car))
21215 {
21216 register int lim = XINT (car);
21217 elt = XCDR (elt);
21218 if (lim < 0)
21219 {
21220 /* Negative int means reduce maximum width. */
21221 if (precision <= 0)
21222 precision = -lim;
21223 else
21224 precision = min (precision, -lim);
21225 }
21226 else if (lim > 0)
21227 {
21228 /* Padding specified. Don't let it be more than
21229 current maximum. */
21230 if (precision > 0)
21231 lim = min (precision, lim);
21232
21233 /* If that's more padding than already wanted, queue it.
21234 But don't reduce padding already specified even if
21235 that is beyond the current truncation point. */
21236 field_width = max (lim, field_width);
21237 }
21238 goto tail_recurse;
21239 }
21240 else if (STRINGP (car) || CONSP (car))
21241 {
21242 Lisp_Object halftail = elt;
21243 int len = 0;
21244
21245 while (CONSP (elt)
21246 && (precision <= 0 || n < precision))
21247 {
21248 n += display_mode_element (it, depth,
21249 /* Do padding only after the last
21250 element in the list. */
21251 (! CONSP (XCDR (elt))
21252 ? field_width - n
21253 : 0),
21254 precision - n, XCAR (elt),
21255 props, risky);
21256 elt = XCDR (elt);
21257 len++;
21258 if ((len & 1) == 0)
21259 halftail = XCDR (halftail);
21260 /* Check for cycle. */
21261 if (EQ (halftail, elt))
21262 break;
21263 }
21264 }
21265 }
21266 break;
21267
21268 default:
21269 invalid:
21270 elt = build_string ("*invalid*");
21271 goto tail_recurse;
21272 }
21273
21274 /* Pad to FIELD_WIDTH. */
21275 if (field_width > 0 && n < field_width)
21276 {
21277 switch (mode_line_target)
21278 {
21279 case MODE_LINE_NOPROP:
21280 case MODE_LINE_TITLE:
21281 n += store_mode_line_noprop ("", field_width - n, 0);
21282 break;
21283 case MODE_LINE_STRING:
21284 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21285 break;
21286 case MODE_LINE_DISPLAY:
21287 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21288 0, 0, 0);
21289 break;
21290 }
21291 }
21292
21293 return n;
21294 }
21295
21296 /* Store a mode-line string element in mode_line_string_list.
21297
21298 If STRING is non-null, display that C string. Otherwise, the Lisp
21299 string LISP_STRING is displayed.
21300
21301 FIELD_WIDTH is the minimum number of output glyphs to produce.
21302 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21303 with spaces. FIELD_WIDTH <= 0 means don't pad.
21304
21305 PRECISION is the maximum number of characters to output from
21306 STRING. PRECISION <= 0 means don't truncate the string.
21307
21308 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21309 properties to the string.
21310
21311 PROPS are the properties to add to the string.
21312 The mode_line_string_face face property is always added to the string.
21313 */
21314
21315 static int
21316 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21317 int field_width, int precision, Lisp_Object props)
21318 {
21319 ptrdiff_t len;
21320 int n = 0;
21321
21322 if (string != NULL)
21323 {
21324 len = strlen (string);
21325 if (precision > 0 && len > precision)
21326 len = precision;
21327 lisp_string = make_string (string, len);
21328 if (NILP (props))
21329 props = mode_line_string_face_prop;
21330 else if (!NILP (mode_line_string_face))
21331 {
21332 Lisp_Object face = Fplist_get (props, Qface);
21333 props = Fcopy_sequence (props);
21334 if (NILP (face))
21335 face = mode_line_string_face;
21336 else
21337 face = list2 (face, mode_line_string_face);
21338 props = Fplist_put (props, Qface, face);
21339 }
21340 Fadd_text_properties (make_number (0), make_number (len),
21341 props, lisp_string);
21342 }
21343 else
21344 {
21345 len = XFASTINT (Flength (lisp_string));
21346 if (precision > 0 && len > precision)
21347 {
21348 len = precision;
21349 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21350 precision = -1;
21351 }
21352 if (!NILP (mode_line_string_face))
21353 {
21354 Lisp_Object face;
21355 if (NILP (props))
21356 props = Ftext_properties_at (make_number (0), lisp_string);
21357 face = Fplist_get (props, Qface);
21358 if (NILP (face))
21359 face = mode_line_string_face;
21360 else
21361 face = list2 (face, mode_line_string_face);
21362 props = list2 (Qface, face);
21363 if (copy_string)
21364 lisp_string = Fcopy_sequence (lisp_string);
21365 }
21366 if (!NILP (props))
21367 Fadd_text_properties (make_number (0), make_number (len),
21368 props, lisp_string);
21369 }
21370
21371 if (len > 0)
21372 {
21373 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21374 n += len;
21375 }
21376
21377 if (field_width > len)
21378 {
21379 field_width -= len;
21380 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21381 if (!NILP (props))
21382 Fadd_text_properties (make_number (0), make_number (field_width),
21383 props, lisp_string);
21384 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21385 n += field_width;
21386 }
21387
21388 return n;
21389 }
21390
21391
21392 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21393 1, 4, 0,
21394 doc: /* Format a string out of a mode line format specification.
21395 First arg FORMAT specifies the mode line format (see `mode-line-format'
21396 for details) to use.
21397
21398 By default, the format is evaluated for the currently selected window.
21399
21400 Optional second arg FACE specifies the face property to put on all
21401 characters for which no face is specified. The value nil means the
21402 default face. The value t means whatever face the window's mode line
21403 currently uses (either `mode-line' or `mode-line-inactive',
21404 depending on whether the window is the selected window or not).
21405 An integer value means the value string has no text
21406 properties.
21407
21408 Optional third and fourth args WINDOW and BUFFER specify the window
21409 and buffer to use as the context for the formatting (defaults
21410 are the selected window and the WINDOW's buffer). */)
21411 (Lisp_Object format, Lisp_Object face,
21412 Lisp_Object window, Lisp_Object buffer)
21413 {
21414 struct it it;
21415 int len;
21416 struct window *w;
21417 struct buffer *old_buffer = NULL;
21418 int face_id;
21419 int no_props = INTEGERP (face);
21420 ptrdiff_t count = SPECPDL_INDEX ();
21421 Lisp_Object str;
21422 int string_start = 0;
21423
21424 w = decode_any_window (window);
21425 XSETWINDOW (window, w);
21426
21427 if (NILP (buffer))
21428 buffer = w->contents;
21429 CHECK_BUFFER (buffer);
21430
21431 /* Make formatting the modeline a non-op when noninteractive, otherwise
21432 there will be problems later caused by a partially initialized frame. */
21433 if (NILP (format) || noninteractive)
21434 return empty_unibyte_string;
21435
21436 if (no_props)
21437 face = Qnil;
21438
21439 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21440 : EQ (face, Qt) ? (EQ (window, selected_window)
21441 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21442 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21443 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21444 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21445 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21446 : DEFAULT_FACE_ID;
21447
21448 old_buffer = current_buffer;
21449
21450 /* Save things including mode_line_proptrans_alist,
21451 and set that to nil so that we don't alter the outer value. */
21452 record_unwind_protect (unwind_format_mode_line,
21453 format_mode_line_unwind_data
21454 (XFRAME (WINDOW_FRAME (w)),
21455 old_buffer, selected_window, 1));
21456 mode_line_proptrans_alist = Qnil;
21457
21458 Fselect_window (window, Qt);
21459 set_buffer_internal_1 (XBUFFER (buffer));
21460
21461 init_iterator (&it, w, -1, -1, NULL, face_id);
21462
21463 if (no_props)
21464 {
21465 mode_line_target = MODE_LINE_NOPROP;
21466 mode_line_string_face_prop = Qnil;
21467 mode_line_string_list = Qnil;
21468 string_start = MODE_LINE_NOPROP_LEN (0);
21469 }
21470 else
21471 {
21472 mode_line_target = MODE_LINE_STRING;
21473 mode_line_string_list = Qnil;
21474 mode_line_string_face = face;
21475 mode_line_string_face_prop
21476 = NILP (face) ? Qnil : list2 (Qface, face);
21477 }
21478
21479 push_kboard (FRAME_KBOARD (it.f));
21480 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21481 pop_kboard ();
21482
21483 if (no_props)
21484 {
21485 len = MODE_LINE_NOPROP_LEN (string_start);
21486 str = make_string (mode_line_noprop_buf + string_start, len);
21487 }
21488 else
21489 {
21490 mode_line_string_list = Fnreverse (mode_line_string_list);
21491 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21492 empty_unibyte_string);
21493 }
21494
21495 unbind_to (count, Qnil);
21496 return str;
21497 }
21498
21499 /* Write a null-terminated, right justified decimal representation of
21500 the positive integer D to BUF using a minimal field width WIDTH. */
21501
21502 static void
21503 pint2str (register char *buf, register int width, register ptrdiff_t d)
21504 {
21505 register char *p = buf;
21506
21507 if (d <= 0)
21508 *p++ = '0';
21509 else
21510 {
21511 while (d > 0)
21512 {
21513 *p++ = d % 10 + '0';
21514 d /= 10;
21515 }
21516 }
21517
21518 for (width -= (int) (p - buf); width > 0; --width)
21519 *p++ = ' ';
21520 *p-- = '\0';
21521 while (p > buf)
21522 {
21523 d = *buf;
21524 *buf++ = *p;
21525 *p-- = d;
21526 }
21527 }
21528
21529 /* Write a null-terminated, right justified decimal and "human
21530 readable" representation of the nonnegative integer D to BUF using
21531 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21532
21533 static const char power_letter[] =
21534 {
21535 0, /* no letter */
21536 'k', /* kilo */
21537 'M', /* mega */
21538 'G', /* giga */
21539 'T', /* tera */
21540 'P', /* peta */
21541 'E', /* exa */
21542 'Z', /* zetta */
21543 'Y' /* yotta */
21544 };
21545
21546 static void
21547 pint2hrstr (char *buf, int width, ptrdiff_t d)
21548 {
21549 /* We aim to represent the nonnegative integer D as
21550 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21551 ptrdiff_t quotient = d;
21552 int remainder = 0;
21553 /* -1 means: do not use TENTHS. */
21554 int tenths = -1;
21555 int exponent = 0;
21556
21557 /* Length of QUOTIENT.TENTHS as a string. */
21558 int length;
21559
21560 char * psuffix;
21561 char * p;
21562
21563 if (quotient >= 1000)
21564 {
21565 /* Scale to the appropriate EXPONENT. */
21566 do
21567 {
21568 remainder = quotient % 1000;
21569 quotient /= 1000;
21570 exponent++;
21571 }
21572 while (quotient >= 1000);
21573
21574 /* Round to nearest and decide whether to use TENTHS or not. */
21575 if (quotient <= 9)
21576 {
21577 tenths = remainder / 100;
21578 if (remainder % 100 >= 50)
21579 {
21580 if (tenths < 9)
21581 tenths++;
21582 else
21583 {
21584 quotient++;
21585 if (quotient == 10)
21586 tenths = -1;
21587 else
21588 tenths = 0;
21589 }
21590 }
21591 }
21592 else
21593 if (remainder >= 500)
21594 {
21595 if (quotient < 999)
21596 quotient++;
21597 else
21598 {
21599 quotient = 1;
21600 exponent++;
21601 tenths = 0;
21602 }
21603 }
21604 }
21605
21606 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21607 if (tenths == -1 && quotient <= 99)
21608 if (quotient <= 9)
21609 length = 1;
21610 else
21611 length = 2;
21612 else
21613 length = 3;
21614 p = psuffix = buf + max (width, length);
21615
21616 /* Print EXPONENT. */
21617 *psuffix++ = power_letter[exponent];
21618 *psuffix = '\0';
21619
21620 /* Print TENTHS. */
21621 if (tenths >= 0)
21622 {
21623 *--p = '0' + tenths;
21624 *--p = '.';
21625 }
21626
21627 /* Print QUOTIENT. */
21628 do
21629 {
21630 int digit = quotient % 10;
21631 *--p = '0' + digit;
21632 }
21633 while ((quotient /= 10) != 0);
21634
21635 /* Print leading spaces. */
21636 while (buf < p)
21637 *--p = ' ';
21638 }
21639
21640 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21641 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21642 type of CODING_SYSTEM. Return updated pointer into BUF. */
21643
21644 static unsigned char invalid_eol_type[] = "(*invalid*)";
21645
21646 static char *
21647 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21648 {
21649 Lisp_Object val;
21650 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21651 const unsigned char *eol_str;
21652 int eol_str_len;
21653 /* The EOL conversion we are using. */
21654 Lisp_Object eoltype;
21655
21656 val = CODING_SYSTEM_SPEC (coding_system);
21657 eoltype = Qnil;
21658
21659 if (!VECTORP (val)) /* Not yet decided. */
21660 {
21661 *buf++ = multibyte ? '-' : ' ';
21662 if (eol_flag)
21663 eoltype = eol_mnemonic_undecided;
21664 /* Don't mention EOL conversion if it isn't decided. */
21665 }
21666 else
21667 {
21668 Lisp_Object attrs;
21669 Lisp_Object eolvalue;
21670
21671 attrs = AREF (val, 0);
21672 eolvalue = AREF (val, 2);
21673
21674 *buf++ = multibyte
21675 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21676 : ' ';
21677
21678 if (eol_flag)
21679 {
21680 /* The EOL conversion that is normal on this system. */
21681
21682 if (NILP (eolvalue)) /* Not yet decided. */
21683 eoltype = eol_mnemonic_undecided;
21684 else if (VECTORP (eolvalue)) /* Not yet decided. */
21685 eoltype = eol_mnemonic_undecided;
21686 else /* eolvalue is Qunix, Qdos, or Qmac. */
21687 eoltype = (EQ (eolvalue, Qunix)
21688 ? eol_mnemonic_unix
21689 : (EQ (eolvalue, Qdos) == 1
21690 ? eol_mnemonic_dos : eol_mnemonic_mac));
21691 }
21692 }
21693
21694 if (eol_flag)
21695 {
21696 /* Mention the EOL conversion if it is not the usual one. */
21697 if (STRINGP (eoltype))
21698 {
21699 eol_str = SDATA (eoltype);
21700 eol_str_len = SBYTES (eoltype);
21701 }
21702 else if (CHARACTERP (eoltype))
21703 {
21704 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21705 int c = XFASTINT (eoltype);
21706 eol_str_len = CHAR_STRING (c, tmp);
21707 eol_str = tmp;
21708 }
21709 else
21710 {
21711 eol_str = invalid_eol_type;
21712 eol_str_len = sizeof (invalid_eol_type) - 1;
21713 }
21714 memcpy (buf, eol_str, eol_str_len);
21715 buf += eol_str_len;
21716 }
21717
21718 return buf;
21719 }
21720
21721 /* Return a string for the output of a mode line %-spec for window W,
21722 generated by character C. FIELD_WIDTH > 0 means pad the string
21723 returned with spaces to that value. Return a Lisp string in
21724 *STRING if the resulting string is taken from that Lisp string.
21725
21726 Note we operate on the current buffer for most purposes. */
21727
21728 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21729
21730 static const char *
21731 decode_mode_spec (struct window *w, register int c, int field_width,
21732 Lisp_Object *string)
21733 {
21734 Lisp_Object obj;
21735 struct frame *f = XFRAME (WINDOW_FRAME (w));
21736 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21737 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21738 produce strings from numerical values, so limit preposterously
21739 large values of FIELD_WIDTH to avoid overrunning the buffer's
21740 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21741 bytes plus the terminating null. */
21742 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21743 struct buffer *b = current_buffer;
21744
21745 obj = Qnil;
21746 *string = Qnil;
21747
21748 switch (c)
21749 {
21750 case '*':
21751 if (!NILP (BVAR (b, read_only)))
21752 return "%";
21753 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21754 return "*";
21755 return "-";
21756
21757 case '+':
21758 /* This differs from %* only for a modified read-only buffer. */
21759 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21760 return "*";
21761 if (!NILP (BVAR (b, read_only)))
21762 return "%";
21763 return "-";
21764
21765 case '&':
21766 /* This differs from %* in ignoring read-only-ness. */
21767 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21768 return "*";
21769 return "-";
21770
21771 case '%':
21772 return "%";
21773
21774 case '[':
21775 {
21776 int i;
21777 char *p;
21778
21779 if (command_loop_level > 5)
21780 return "[[[... ";
21781 p = decode_mode_spec_buf;
21782 for (i = 0; i < command_loop_level; i++)
21783 *p++ = '[';
21784 *p = 0;
21785 return decode_mode_spec_buf;
21786 }
21787
21788 case ']':
21789 {
21790 int i;
21791 char *p;
21792
21793 if (command_loop_level > 5)
21794 return " ...]]]";
21795 p = decode_mode_spec_buf;
21796 for (i = 0; i < command_loop_level; i++)
21797 *p++ = ']';
21798 *p = 0;
21799 return decode_mode_spec_buf;
21800 }
21801
21802 case '-':
21803 {
21804 register int i;
21805
21806 /* Let lots_of_dashes be a string of infinite length. */
21807 if (mode_line_target == MODE_LINE_NOPROP
21808 || mode_line_target == MODE_LINE_STRING)
21809 return "--";
21810 if (field_width <= 0
21811 || field_width > sizeof (lots_of_dashes))
21812 {
21813 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21814 decode_mode_spec_buf[i] = '-';
21815 decode_mode_spec_buf[i] = '\0';
21816 return decode_mode_spec_buf;
21817 }
21818 else
21819 return lots_of_dashes;
21820 }
21821
21822 case 'b':
21823 obj = BVAR (b, name);
21824 break;
21825
21826 case 'c':
21827 /* %c and %l are ignored in `frame-title-format'.
21828 (In redisplay_internal, the frame title is drawn _before_ the
21829 windows are updated, so the stuff which depends on actual
21830 window contents (such as %l) may fail to render properly, or
21831 even crash emacs.) */
21832 if (mode_line_target == MODE_LINE_TITLE)
21833 return "";
21834 else
21835 {
21836 ptrdiff_t col = current_column ();
21837 w->column_number_displayed = col;
21838 pint2str (decode_mode_spec_buf, width, col);
21839 return decode_mode_spec_buf;
21840 }
21841
21842 case 'e':
21843 #ifndef SYSTEM_MALLOC
21844 {
21845 if (NILP (Vmemory_full))
21846 return "";
21847 else
21848 return "!MEM FULL! ";
21849 }
21850 #else
21851 return "";
21852 #endif
21853
21854 case 'F':
21855 /* %F displays the frame name. */
21856 if (!NILP (f->title))
21857 return SSDATA (f->title);
21858 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21859 return SSDATA (f->name);
21860 return "Emacs";
21861
21862 case 'f':
21863 obj = BVAR (b, filename);
21864 break;
21865
21866 case 'i':
21867 {
21868 ptrdiff_t size = ZV - BEGV;
21869 pint2str (decode_mode_spec_buf, width, size);
21870 return decode_mode_spec_buf;
21871 }
21872
21873 case 'I':
21874 {
21875 ptrdiff_t size = ZV - BEGV;
21876 pint2hrstr (decode_mode_spec_buf, width, size);
21877 return decode_mode_spec_buf;
21878 }
21879
21880 case 'l':
21881 {
21882 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21883 ptrdiff_t topline, nlines, height;
21884 ptrdiff_t junk;
21885
21886 /* %c and %l are ignored in `frame-title-format'. */
21887 if (mode_line_target == MODE_LINE_TITLE)
21888 return "";
21889
21890 startpos = marker_position (w->start);
21891 startpos_byte = marker_byte_position (w->start);
21892 height = WINDOW_TOTAL_LINES (w);
21893
21894 /* If we decided that this buffer isn't suitable for line numbers,
21895 don't forget that too fast. */
21896 if (w->base_line_pos == -1)
21897 goto no_value;
21898
21899 /* If the buffer is very big, don't waste time. */
21900 if (INTEGERP (Vline_number_display_limit)
21901 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21902 {
21903 w->base_line_pos = 0;
21904 w->base_line_number = 0;
21905 goto no_value;
21906 }
21907
21908 if (w->base_line_number > 0
21909 && w->base_line_pos > 0
21910 && w->base_line_pos <= startpos)
21911 {
21912 line = w->base_line_number;
21913 linepos = w->base_line_pos;
21914 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21915 }
21916 else
21917 {
21918 line = 1;
21919 linepos = BUF_BEGV (b);
21920 linepos_byte = BUF_BEGV_BYTE (b);
21921 }
21922
21923 /* Count lines from base line to window start position. */
21924 nlines = display_count_lines (linepos_byte,
21925 startpos_byte,
21926 startpos, &junk);
21927
21928 topline = nlines + line;
21929
21930 /* Determine a new base line, if the old one is too close
21931 or too far away, or if we did not have one.
21932 "Too close" means it's plausible a scroll-down would
21933 go back past it. */
21934 if (startpos == BUF_BEGV (b))
21935 {
21936 w->base_line_number = topline;
21937 w->base_line_pos = BUF_BEGV (b);
21938 }
21939 else if (nlines < height + 25 || nlines > height * 3 + 50
21940 || linepos == BUF_BEGV (b))
21941 {
21942 ptrdiff_t limit = BUF_BEGV (b);
21943 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21944 ptrdiff_t position;
21945 ptrdiff_t distance =
21946 (height * 2 + 30) * line_number_display_limit_width;
21947
21948 if (startpos - distance > limit)
21949 {
21950 limit = startpos - distance;
21951 limit_byte = CHAR_TO_BYTE (limit);
21952 }
21953
21954 nlines = display_count_lines (startpos_byte,
21955 limit_byte,
21956 - (height * 2 + 30),
21957 &position);
21958 /* If we couldn't find the lines we wanted within
21959 line_number_display_limit_width chars per line,
21960 give up on line numbers for this window. */
21961 if (position == limit_byte && limit == startpos - distance)
21962 {
21963 w->base_line_pos = -1;
21964 w->base_line_number = 0;
21965 goto no_value;
21966 }
21967
21968 w->base_line_number = topline - nlines;
21969 w->base_line_pos = BYTE_TO_CHAR (position);
21970 }
21971
21972 /* Now count lines from the start pos to point. */
21973 nlines = display_count_lines (startpos_byte,
21974 PT_BYTE, PT, &junk);
21975
21976 /* Record that we did display the line number. */
21977 line_number_displayed = 1;
21978
21979 /* Make the string to show. */
21980 pint2str (decode_mode_spec_buf, width, topline + nlines);
21981 return decode_mode_spec_buf;
21982 no_value:
21983 {
21984 char* p = decode_mode_spec_buf;
21985 int pad = width - 2;
21986 while (pad-- > 0)
21987 *p++ = ' ';
21988 *p++ = '?';
21989 *p++ = '?';
21990 *p = '\0';
21991 return decode_mode_spec_buf;
21992 }
21993 }
21994 break;
21995
21996 case 'm':
21997 obj = BVAR (b, mode_name);
21998 break;
21999
22000 case 'n':
22001 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
22002 return " Narrow";
22003 break;
22004
22005 case 'p':
22006 {
22007 ptrdiff_t pos = marker_position (w->start);
22008 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22009
22010 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
22011 {
22012 if (pos <= BUF_BEGV (b))
22013 return "All";
22014 else
22015 return "Bottom";
22016 }
22017 else if (pos <= BUF_BEGV (b))
22018 return "Top";
22019 else
22020 {
22021 if (total > 1000000)
22022 /* Do it differently for a large value, to avoid overflow. */
22023 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22024 else
22025 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
22026 /* We can't normally display a 3-digit number,
22027 so get us a 2-digit number that is close. */
22028 if (total == 100)
22029 total = 99;
22030 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22031 return decode_mode_spec_buf;
22032 }
22033 }
22034
22035 /* Display percentage of size above the bottom of the screen. */
22036 case 'P':
22037 {
22038 ptrdiff_t toppos = marker_position (w->start);
22039 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
22040 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
22041
22042 if (botpos >= BUF_ZV (b))
22043 {
22044 if (toppos <= BUF_BEGV (b))
22045 return "All";
22046 else
22047 return "Bottom";
22048 }
22049 else
22050 {
22051 if (total > 1000000)
22052 /* Do it differently for a large value, to avoid overflow. */
22053 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
22054 else
22055 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
22056 /* We can't normally display a 3-digit number,
22057 so get us a 2-digit number that is close. */
22058 if (total == 100)
22059 total = 99;
22060 if (toppos <= BUF_BEGV (b))
22061 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
22062 else
22063 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
22064 return decode_mode_spec_buf;
22065 }
22066 }
22067
22068 case 's':
22069 /* status of process */
22070 obj = Fget_buffer_process (Fcurrent_buffer ());
22071 if (NILP (obj))
22072 return "no process";
22073 #ifndef MSDOS
22074 obj = Fsymbol_name (Fprocess_status (obj));
22075 #endif
22076 break;
22077
22078 case '@':
22079 {
22080 ptrdiff_t count = inhibit_garbage_collection ();
22081 Lisp_Object val = call1 (intern ("file-remote-p"),
22082 BVAR (current_buffer, directory));
22083 unbind_to (count, Qnil);
22084
22085 if (NILP (val))
22086 return "-";
22087 else
22088 return "@";
22089 }
22090
22091 case 'z':
22092 /* coding-system (not including end-of-line format) */
22093 case 'Z':
22094 /* coding-system (including end-of-line type) */
22095 {
22096 int eol_flag = (c == 'Z');
22097 char *p = decode_mode_spec_buf;
22098
22099 if (! FRAME_WINDOW_P (f))
22100 {
22101 /* No need to mention EOL here--the terminal never needs
22102 to do EOL conversion. */
22103 p = decode_mode_spec_coding (CODING_ID_NAME
22104 (FRAME_KEYBOARD_CODING (f)->id),
22105 p, 0);
22106 p = decode_mode_spec_coding (CODING_ID_NAME
22107 (FRAME_TERMINAL_CODING (f)->id),
22108 p, 0);
22109 }
22110 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
22111 p, eol_flag);
22112
22113 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
22114 #ifdef subprocesses
22115 obj = Fget_buffer_process (Fcurrent_buffer ());
22116 if (PROCESSP (obj))
22117 {
22118 p = decode_mode_spec_coding
22119 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
22120 p = decode_mode_spec_coding
22121 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
22122 }
22123 #endif /* subprocesses */
22124 #endif /* 0 */
22125 *p = 0;
22126 return decode_mode_spec_buf;
22127 }
22128 }
22129
22130 if (STRINGP (obj))
22131 {
22132 *string = obj;
22133 return SSDATA (obj);
22134 }
22135 else
22136 return "";
22137 }
22138
22139
22140 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
22141 means count lines back from START_BYTE. But don't go beyond
22142 LIMIT_BYTE. Return the number of lines thus found (always
22143 nonnegative).
22144
22145 Set *BYTE_POS_PTR to the byte position where we stopped. This is
22146 either the position COUNT lines after/before START_BYTE, if we
22147 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
22148 COUNT lines. */
22149
22150 static ptrdiff_t
22151 display_count_lines (ptrdiff_t start_byte,
22152 ptrdiff_t limit_byte, ptrdiff_t count,
22153 ptrdiff_t *byte_pos_ptr)
22154 {
22155 register unsigned char *cursor;
22156 unsigned char *base;
22157
22158 register ptrdiff_t ceiling;
22159 register unsigned char *ceiling_addr;
22160 ptrdiff_t orig_count = count;
22161
22162 /* If we are not in selective display mode,
22163 check only for newlines. */
22164 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
22165 && !INTEGERP (BVAR (current_buffer, selective_display)));
22166
22167 if (count > 0)
22168 {
22169 while (start_byte < limit_byte)
22170 {
22171 ceiling = BUFFER_CEILING_OF (start_byte);
22172 ceiling = min (limit_byte - 1, ceiling);
22173 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
22174 base = (cursor = BYTE_POS_ADDR (start_byte));
22175
22176 do
22177 {
22178 if (selective_display)
22179 {
22180 while (*cursor != '\n' && *cursor != 015
22181 && ++cursor != ceiling_addr)
22182 continue;
22183 if (cursor == ceiling_addr)
22184 break;
22185 }
22186 else
22187 {
22188 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
22189 if (! cursor)
22190 break;
22191 }
22192
22193 cursor++;
22194
22195 if (--count == 0)
22196 {
22197 start_byte += cursor - base;
22198 *byte_pos_ptr = start_byte;
22199 return orig_count;
22200 }
22201 }
22202 while (cursor < ceiling_addr);
22203
22204 start_byte += ceiling_addr - base;
22205 }
22206 }
22207 else
22208 {
22209 while (start_byte > limit_byte)
22210 {
22211 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
22212 ceiling = max (limit_byte, ceiling);
22213 ceiling_addr = BYTE_POS_ADDR (ceiling);
22214 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
22215 while (1)
22216 {
22217 if (selective_display)
22218 {
22219 while (--cursor >= ceiling_addr
22220 && *cursor != '\n' && *cursor != 015)
22221 continue;
22222 if (cursor < ceiling_addr)
22223 break;
22224 }
22225 else
22226 {
22227 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
22228 if (! cursor)
22229 break;
22230 }
22231
22232 if (++count == 0)
22233 {
22234 start_byte += cursor - base + 1;
22235 *byte_pos_ptr = start_byte;
22236 /* When scanning backwards, we should
22237 not count the newline posterior to which we stop. */
22238 return - orig_count - 1;
22239 }
22240 }
22241 start_byte += ceiling_addr - base;
22242 }
22243 }
22244
22245 *byte_pos_ptr = limit_byte;
22246
22247 if (count < 0)
22248 return - orig_count + count;
22249 return orig_count - count;
22250
22251 }
22252
22253
22254 \f
22255 /***********************************************************************
22256 Displaying strings
22257 ***********************************************************************/
22258
22259 /* Display a NUL-terminated string, starting with index START.
22260
22261 If STRING is non-null, display that C string. Otherwise, the Lisp
22262 string LISP_STRING is displayed. There's a case that STRING is
22263 non-null and LISP_STRING is not nil. It means STRING is a string
22264 data of LISP_STRING. In that case, we display LISP_STRING while
22265 ignoring its text properties.
22266
22267 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22268 FACE_STRING. Display STRING or LISP_STRING with the face at
22269 FACE_STRING_POS in FACE_STRING:
22270
22271 Display the string in the environment given by IT, but use the
22272 standard display table, temporarily.
22273
22274 FIELD_WIDTH is the minimum number of output glyphs to produce.
22275 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22276 with spaces. If STRING has more characters, more than FIELD_WIDTH
22277 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22278
22279 PRECISION is the maximum number of characters to output from
22280 STRING. PRECISION < 0 means don't truncate the string.
22281
22282 This is roughly equivalent to printf format specifiers:
22283
22284 FIELD_WIDTH PRECISION PRINTF
22285 ----------------------------------------
22286 -1 -1 %s
22287 -1 10 %.10s
22288 10 -1 %10s
22289 20 10 %20.10s
22290
22291 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22292 display them, and < 0 means obey the current buffer's value of
22293 enable_multibyte_characters.
22294
22295 Value is the number of columns displayed. */
22296
22297 static int
22298 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22299 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22300 int field_width, int precision, int max_x, int multibyte)
22301 {
22302 int hpos_at_start = it->hpos;
22303 int saved_face_id = it->face_id;
22304 struct glyph_row *row = it->glyph_row;
22305 ptrdiff_t it_charpos;
22306
22307 /* Initialize the iterator IT for iteration over STRING beginning
22308 with index START. */
22309 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22310 precision, field_width, multibyte);
22311 if (string && STRINGP (lisp_string))
22312 /* LISP_STRING is the one returned by decode_mode_spec. We should
22313 ignore its text properties. */
22314 it->stop_charpos = it->end_charpos;
22315
22316 /* If displaying STRING, set up the face of the iterator from
22317 FACE_STRING, if that's given. */
22318 if (STRINGP (face_string))
22319 {
22320 ptrdiff_t endptr;
22321 struct face *face;
22322
22323 it->face_id
22324 = face_at_string_position (it->w, face_string, face_string_pos,
22325 0, it->region_beg_charpos,
22326 it->region_end_charpos,
22327 &endptr, it->base_face_id, 0);
22328 face = FACE_FROM_ID (it->f, it->face_id);
22329 it->face_box_p = face->box != FACE_NO_BOX;
22330 }
22331
22332 /* Set max_x to the maximum allowed X position. Don't let it go
22333 beyond the right edge of the window. */
22334 if (max_x <= 0)
22335 max_x = it->last_visible_x;
22336 else
22337 max_x = min (max_x, it->last_visible_x);
22338
22339 /* Skip over display elements that are not visible. because IT->w is
22340 hscrolled. */
22341 if (it->current_x < it->first_visible_x)
22342 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22343 MOVE_TO_POS | MOVE_TO_X);
22344
22345 row->ascent = it->max_ascent;
22346 row->height = it->max_ascent + it->max_descent;
22347 row->phys_ascent = it->max_phys_ascent;
22348 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22349 row->extra_line_spacing = it->max_extra_line_spacing;
22350
22351 if (STRINGP (it->string))
22352 it_charpos = IT_STRING_CHARPOS (*it);
22353 else
22354 it_charpos = IT_CHARPOS (*it);
22355
22356 /* This condition is for the case that we are called with current_x
22357 past last_visible_x. */
22358 while (it->current_x < max_x)
22359 {
22360 int x_before, x, n_glyphs_before, i, nglyphs;
22361
22362 /* Get the next display element. */
22363 if (!get_next_display_element (it))
22364 break;
22365
22366 /* Produce glyphs. */
22367 x_before = it->current_x;
22368 n_glyphs_before = row->used[TEXT_AREA];
22369 PRODUCE_GLYPHS (it);
22370
22371 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22372 i = 0;
22373 x = x_before;
22374 while (i < nglyphs)
22375 {
22376 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22377
22378 if (it->line_wrap != TRUNCATE
22379 && x + glyph->pixel_width > max_x)
22380 {
22381 /* End of continued line or max_x reached. */
22382 if (CHAR_GLYPH_PADDING_P (*glyph))
22383 {
22384 /* A wide character is unbreakable. */
22385 if (row->reversed_p)
22386 unproduce_glyphs (it, row->used[TEXT_AREA]
22387 - n_glyphs_before);
22388 row->used[TEXT_AREA] = n_glyphs_before;
22389 it->current_x = x_before;
22390 }
22391 else
22392 {
22393 if (row->reversed_p)
22394 unproduce_glyphs (it, row->used[TEXT_AREA]
22395 - (n_glyphs_before + i));
22396 row->used[TEXT_AREA] = n_glyphs_before + i;
22397 it->current_x = x;
22398 }
22399 break;
22400 }
22401 else if (x + glyph->pixel_width >= it->first_visible_x)
22402 {
22403 /* Glyph is at least partially visible. */
22404 ++it->hpos;
22405 if (x < it->first_visible_x)
22406 row->x = x - it->first_visible_x;
22407 }
22408 else
22409 {
22410 /* Glyph is off the left margin of the display area.
22411 Should not happen. */
22412 emacs_abort ();
22413 }
22414
22415 row->ascent = max (row->ascent, it->max_ascent);
22416 row->height = max (row->height, it->max_ascent + it->max_descent);
22417 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22418 row->phys_height = max (row->phys_height,
22419 it->max_phys_ascent + it->max_phys_descent);
22420 row->extra_line_spacing = max (row->extra_line_spacing,
22421 it->max_extra_line_spacing);
22422 x += glyph->pixel_width;
22423 ++i;
22424 }
22425
22426 /* Stop if max_x reached. */
22427 if (i < nglyphs)
22428 break;
22429
22430 /* Stop at line ends. */
22431 if (ITERATOR_AT_END_OF_LINE_P (it))
22432 {
22433 it->continuation_lines_width = 0;
22434 break;
22435 }
22436
22437 set_iterator_to_next (it, 1);
22438 if (STRINGP (it->string))
22439 it_charpos = IT_STRING_CHARPOS (*it);
22440 else
22441 it_charpos = IT_CHARPOS (*it);
22442
22443 /* Stop if truncating at the right edge. */
22444 if (it->line_wrap == TRUNCATE
22445 && it->current_x >= it->last_visible_x)
22446 {
22447 /* Add truncation mark, but don't do it if the line is
22448 truncated at a padding space. */
22449 if (it_charpos < it->string_nchars)
22450 {
22451 if (!FRAME_WINDOW_P (it->f))
22452 {
22453 int ii, n;
22454
22455 if (it->current_x > it->last_visible_x)
22456 {
22457 if (!row->reversed_p)
22458 {
22459 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22460 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22461 break;
22462 }
22463 else
22464 {
22465 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22466 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22467 break;
22468 unproduce_glyphs (it, ii + 1);
22469 ii = row->used[TEXT_AREA] - (ii + 1);
22470 }
22471 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22472 {
22473 row->used[TEXT_AREA] = ii;
22474 produce_special_glyphs (it, IT_TRUNCATION);
22475 }
22476 }
22477 produce_special_glyphs (it, IT_TRUNCATION);
22478 }
22479 row->truncated_on_right_p = 1;
22480 }
22481 break;
22482 }
22483 }
22484
22485 /* Maybe insert a truncation at the left. */
22486 if (it->first_visible_x
22487 && it_charpos > 0)
22488 {
22489 if (!FRAME_WINDOW_P (it->f)
22490 || (row->reversed_p
22491 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22492 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22493 insert_left_trunc_glyphs (it);
22494 row->truncated_on_left_p = 1;
22495 }
22496
22497 it->face_id = saved_face_id;
22498
22499 /* Value is number of columns displayed. */
22500 return it->hpos - hpos_at_start;
22501 }
22502
22503
22504 \f
22505 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22506 appears as an element of LIST or as the car of an element of LIST.
22507 If PROPVAL is a list, compare each element against LIST in that
22508 way, and return 1/2 if any element of PROPVAL is found in LIST.
22509 Otherwise return 0. This function cannot quit.
22510 The return value is 2 if the text is invisible but with an ellipsis
22511 and 1 if it's invisible and without an ellipsis. */
22512
22513 int
22514 invisible_p (register Lisp_Object propval, Lisp_Object list)
22515 {
22516 register Lisp_Object tail, proptail;
22517
22518 for (tail = list; CONSP (tail); tail = XCDR (tail))
22519 {
22520 register Lisp_Object tem;
22521 tem = XCAR (tail);
22522 if (EQ (propval, tem))
22523 return 1;
22524 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22525 return NILP (XCDR (tem)) ? 1 : 2;
22526 }
22527
22528 if (CONSP (propval))
22529 {
22530 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22531 {
22532 Lisp_Object propelt;
22533 propelt = XCAR (proptail);
22534 for (tail = list; CONSP (tail); tail = XCDR (tail))
22535 {
22536 register Lisp_Object tem;
22537 tem = XCAR (tail);
22538 if (EQ (propelt, tem))
22539 return 1;
22540 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22541 return NILP (XCDR (tem)) ? 1 : 2;
22542 }
22543 }
22544 }
22545
22546 return 0;
22547 }
22548
22549 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22550 doc: /* Non-nil if the property makes the text invisible.
22551 POS-OR-PROP can be a marker or number, in which case it is taken to be
22552 a position in the current buffer and the value of the `invisible' property
22553 is checked; or it can be some other value, which is then presumed to be the
22554 value of the `invisible' property of the text of interest.
22555 The non-nil value returned can be t for truly invisible text or something
22556 else if the text is replaced by an ellipsis. */)
22557 (Lisp_Object pos_or_prop)
22558 {
22559 Lisp_Object prop
22560 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22561 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22562 : pos_or_prop);
22563 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22564 return (invis == 0 ? Qnil
22565 : invis == 1 ? Qt
22566 : make_number (invis));
22567 }
22568
22569 /* Calculate a width or height in pixels from a specification using
22570 the following elements:
22571
22572 SPEC ::=
22573 NUM - a (fractional) multiple of the default font width/height
22574 (NUM) - specifies exactly NUM pixels
22575 UNIT - a fixed number of pixels, see below.
22576 ELEMENT - size of a display element in pixels, see below.
22577 (NUM . SPEC) - equals NUM * SPEC
22578 (+ SPEC SPEC ...) - add pixel values
22579 (- SPEC SPEC ...) - subtract pixel values
22580 (- SPEC) - negate pixel value
22581
22582 NUM ::=
22583 INT or FLOAT - a number constant
22584 SYMBOL - use symbol's (buffer local) variable binding.
22585
22586 UNIT ::=
22587 in - pixels per inch *)
22588 mm - pixels per 1/1000 meter *)
22589 cm - pixels per 1/100 meter *)
22590 width - width of current font in pixels.
22591 height - height of current font in pixels.
22592
22593 *) using the ratio(s) defined in display-pixels-per-inch.
22594
22595 ELEMENT ::=
22596
22597 left-fringe - left fringe width in pixels
22598 right-fringe - right fringe width in pixels
22599
22600 left-margin - left margin width in pixels
22601 right-margin - right margin width in pixels
22602
22603 scroll-bar - scroll-bar area width in pixels
22604
22605 Examples:
22606
22607 Pixels corresponding to 5 inches:
22608 (5 . in)
22609
22610 Total width of non-text areas on left side of window (if scroll-bar is on left):
22611 '(space :width (+ left-fringe left-margin scroll-bar))
22612
22613 Align to first text column (in header line):
22614 '(space :align-to 0)
22615
22616 Align to middle of text area minus half the width of variable `my-image'
22617 containing a loaded image:
22618 '(space :align-to (0.5 . (- text my-image)))
22619
22620 Width of left margin minus width of 1 character in the default font:
22621 '(space :width (- left-margin 1))
22622
22623 Width of left margin minus width of 2 characters in the current font:
22624 '(space :width (- left-margin (2 . width)))
22625
22626 Center 1 character over left-margin (in header line):
22627 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22628
22629 Different ways to express width of left fringe plus left margin minus one pixel:
22630 '(space :width (- (+ left-fringe left-margin) (1)))
22631 '(space :width (+ left-fringe left-margin (- (1))))
22632 '(space :width (+ left-fringe left-margin (-1)))
22633
22634 */
22635
22636 static int
22637 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22638 struct font *font, int width_p, int *align_to)
22639 {
22640 double pixels;
22641
22642 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22643 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22644
22645 if (NILP (prop))
22646 return OK_PIXELS (0);
22647
22648 eassert (FRAME_LIVE_P (it->f));
22649
22650 if (SYMBOLP (prop))
22651 {
22652 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22653 {
22654 char *unit = SSDATA (SYMBOL_NAME (prop));
22655
22656 if (unit[0] == 'i' && unit[1] == 'n')
22657 pixels = 1.0;
22658 else if (unit[0] == 'm' && unit[1] == 'm')
22659 pixels = 25.4;
22660 else if (unit[0] == 'c' && unit[1] == 'm')
22661 pixels = 2.54;
22662 else
22663 pixels = 0;
22664 if (pixels > 0)
22665 {
22666 double ppi = (width_p ? FRAME_RES_X (it->f)
22667 : FRAME_RES_Y (it->f));
22668
22669 if (ppi > 0)
22670 return OK_PIXELS (ppi / pixels);
22671 return 0;
22672 }
22673 }
22674
22675 #ifdef HAVE_WINDOW_SYSTEM
22676 if (EQ (prop, Qheight))
22677 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22678 if (EQ (prop, Qwidth))
22679 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22680 #else
22681 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22682 return OK_PIXELS (1);
22683 #endif
22684
22685 if (EQ (prop, Qtext))
22686 return OK_PIXELS (width_p
22687 ? window_box_width (it->w, TEXT_AREA)
22688 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22689
22690 if (align_to && *align_to < 0)
22691 {
22692 *res = 0;
22693 if (EQ (prop, Qleft))
22694 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22695 if (EQ (prop, Qright))
22696 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22697 if (EQ (prop, Qcenter))
22698 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22699 + window_box_width (it->w, TEXT_AREA) / 2);
22700 if (EQ (prop, Qleft_fringe))
22701 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22702 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22703 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22704 if (EQ (prop, Qright_fringe))
22705 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22706 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22707 : window_box_right_offset (it->w, TEXT_AREA));
22708 if (EQ (prop, Qleft_margin))
22709 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22710 if (EQ (prop, Qright_margin))
22711 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22712 if (EQ (prop, Qscroll_bar))
22713 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22714 ? 0
22715 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22716 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22717 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22718 : 0)));
22719 }
22720 else
22721 {
22722 if (EQ (prop, Qleft_fringe))
22723 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22724 if (EQ (prop, Qright_fringe))
22725 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22726 if (EQ (prop, Qleft_margin))
22727 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22728 if (EQ (prop, Qright_margin))
22729 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22730 if (EQ (prop, Qscroll_bar))
22731 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22732 }
22733
22734 prop = buffer_local_value_1 (prop, it->w->contents);
22735 if (EQ (prop, Qunbound))
22736 prop = Qnil;
22737 }
22738
22739 if (INTEGERP (prop) || FLOATP (prop))
22740 {
22741 int base_unit = (width_p
22742 ? FRAME_COLUMN_WIDTH (it->f)
22743 : FRAME_LINE_HEIGHT (it->f));
22744 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22745 }
22746
22747 if (CONSP (prop))
22748 {
22749 Lisp_Object car = XCAR (prop);
22750 Lisp_Object cdr = XCDR (prop);
22751
22752 if (SYMBOLP (car))
22753 {
22754 #ifdef HAVE_WINDOW_SYSTEM
22755 if (FRAME_WINDOW_P (it->f)
22756 && valid_image_p (prop))
22757 {
22758 ptrdiff_t id = lookup_image (it->f, prop);
22759 struct image *img = IMAGE_FROM_ID (it->f, id);
22760
22761 return OK_PIXELS (width_p ? img->width : img->height);
22762 }
22763 #endif
22764 if (EQ (car, Qplus) || EQ (car, Qminus))
22765 {
22766 int first = 1;
22767 double px;
22768
22769 pixels = 0;
22770 while (CONSP (cdr))
22771 {
22772 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22773 font, width_p, align_to))
22774 return 0;
22775 if (first)
22776 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22777 else
22778 pixels += px;
22779 cdr = XCDR (cdr);
22780 }
22781 if (EQ (car, Qminus))
22782 pixels = -pixels;
22783 return OK_PIXELS (pixels);
22784 }
22785
22786 car = buffer_local_value_1 (car, it->w->contents);
22787 if (EQ (car, Qunbound))
22788 car = Qnil;
22789 }
22790
22791 if (INTEGERP (car) || FLOATP (car))
22792 {
22793 double fact;
22794 pixels = XFLOATINT (car);
22795 if (NILP (cdr))
22796 return OK_PIXELS (pixels);
22797 if (calc_pixel_width_or_height (&fact, it, cdr,
22798 font, width_p, align_to))
22799 return OK_PIXELS (pixels * fact);
22800 return 0;
22801 }
22802
22803 return 0;
22804 }
22805
22806 return 0;
22807 }
22808
22809 \f
22810 /***********************************************************************
22811 Glyph Display
22812 ***********************************************************************/
22813
22814 #ifdef HAVE_WINDOW_SYSTEM
22815
22816 #ifdef GLYPH_DEBUG
22817
22818 void
22819 dump_glyph_string (struct glyph_string *s)
22820 {
22821 fprintf (stderr, "glyph string\n");
22822 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22823 s->x, s->y, s->width, s->height);
22824 fprintf (stderr, " ybase = %d\n", s->ybase);
22825 fprintf (stderr, " hl = %d\n", s->hl);
22826 fprintf (stderr, " left overhang = %d, right = %d\n",
22827 s->left_overhang, s->right_overhang);
22828 fprintf (stderr, " nchars = %d\n", s->nchars);
22829 fprintf (stderr, " extends to end of line = %d\n",
22830 s->extends_to_end_of_line_p);
22831 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22832 fprintf (stderr, " bg width = %d\n", s->background_width);
22833 }
22834
22835 #endif /* GLYPH_DEBUG */
22836
22837 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22838 of XChar2b structures for S; it can't be allocated in
22839 init_glyph_string because it must be allocated via `alloca'. W
22840 is the window on which S is drawn. ROW and AREA are the glyph row
22841 and area within the row from which S is constructed. START is the
22842 index of the first glyph structure covered by S. HL is a
22843 face-override for drawing S. */
22844
22845 #ifdef HAVE_NTGUI
22846 #define OPTIONAL_HDC(hdc) HDC hdc,
22847 #define DECLARE_HDC(hdc) HDC hdc;
22848 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22849 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22850 #endif
22851
22852 #ifndef OPTIONAL_HDC
22853 #define OPTIONAL_HDC(hdc)
22854 #define DECLARE_HDC(hdc)
22855 #define ALLOCATE_HDC(hdc, f)
22856 #define RELEASE_HDC(hdc, f)
22857 #endif
22858
22859 static void
22860 init_glyph_string (struct glyph_string *s,
22861 OPTIONAL_HDC (hdc)
22862 XChar2b *char2b, struct window *w, struct glyph_row *row,
22863 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22864 {
22865 memset (s, 0, sizeof *s);
22866 s->w = w;
22867 s->f = XFRAME (w->frame);
22868 #ifdef HAVE_NTGUI
22869 s->hdc = hdc;
22870 #endif
22871 s->display = FRAME_X_DISPLAY (s->f);
22872 s->window = FRAME_X_WINDOW (s->f);
22873 s->char2b = char2b;
22874 s->hl = hl;
22875 s->row = row;
22876 s->area = area;
22877 s->first_glyph = row->glyphs[area] + start;
22878 s->height = row->height;
22879 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22880 s->ybase = s->y + row->ascent;
22881 }
22882
22883
22884 /* Append the list of glyph strings with head H and tail T to the list
22885 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22886
22887 static void
22888 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22889 struct glyph_string *h, struct glyph_string *t)
22890 {
22891 if (h)
22892 {
22893 if (*head)
22894 (*tail)->next = h;
22895 else
22896 *head = h;
22897 h->prev = *tail;
22898 *tail = t;
22899 }
22900 }
22901
22902
22903 /* Prepend the list of glyph strings with head H and tail T to the
22904 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22905 result. */
22906
22907 static void
22908 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22909 struct glyph_string *h, struct glyph_string *t)
22910 {
22911 if (h)
22912 {
22913 if (*head)
22914 (*head)->prev = t;
22915 else
22916 *tail = t;
22917 t->next = *head;
22918 *head = h;
22919 }
22920 }
22921
22922
22923 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22924 Set *HEAD and *TAIL to the resulting list. */
22925
22926 static void
22927 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22928 struct glyph_string *s)
22929 {
22930 s->next = s->prev = NULL;
22931 append_glyph_string_lists (head, tail, s, s);
22932 }
22933
22934
22935 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22936 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22937 make sure that X resources for the face returned are allocated.
22938 Value is a pointer to a realized face that is ready for display if
22939 DISPLAY_P is non-zero. */
22940
22941 static struct face *
22942 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22943 XChar2b *char2b, int display_p)
22944 {
22945 struct face *face = FACE_FROM_ID (f, face_id);
22946 unsigned code = 0;
22947
22948 if (face->font)
22949 {
22950 code = face->font->driver->encode_char (face->font, c);
22951
22952 if (code == FONT_INVALID_CODE)
22953 code = 0;
22954 }
22955 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22956
22957 /* Make sure X resources of the face are allocated. */
22958 #ifdef HAVE_X_WINDOWS
22959 if (display_p)
22960 #endif
22961 {
22962 eassert (face != NULL);
22963 PREPARE_FACE_FOR_DISPLAY (f, face);
22964 }
22965
22966 return face;
22967 }
22968
22969
22970 /* Get face and two-byte form of character glyph GLYPH on frame F.
22971 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22972 a pointer to a realized face that is ready for display. */
22973
22974 static struct face *
22975 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22976 XChar2b *char2b, int *two_byte_p)
22977 {
22978 struct face *face;
22979 unsigned code = 0;
22980
22981 eassert (glyph->type == CHAR_GLYPH);
22982 face = FACE_FROM_ID (f, glyph->face_id);
22983
22984 /* Make sure X resources of the face are allocated. */
22985 eassert (face != NULL);
22986 PREPARE_FACE_FOR_DISPLAY (f, face);
22987
22988 if (two_byte_p)
22989 *two_byte_p = 0;
22990
22991 if (face->font)
22992 {
22993 if (CHAR_BYTE8_P (glyph->u.ch))
22994 code = CHAR_TO_BYTE8 (glyph->u.ch);
22995 else
22996 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22997
22998 if (code == FONT_INVALID_CODE)
22999 code = 0;
23000 }
23001
23002 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23003 return face;
23004 }
23005
23006
23007 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
23008 Return 1 if FONT has a glyph for C, otherwise return 0. */
23009
23010 static int
23011 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
23012 {
23013 unsigned code;
23014
23015 if (CHAR_BYTE8_P (c))
23016 code = CHAR_TO_BYTE8 (c);
23017 else
23018 code = font->driver->encode_char (font, c);
23019
23020 if (code == FONT_INVALID_CODE)
23021 return 0;
23022 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
23023 return 1;
23024 }
23025
23026
23027 /* Fill glyph string S with composition components specified by S->cmp.
23028
23029 BASE_FACE is the base face of the composition.
23030 S->cmp_from is the index of the first component for S.
23031
23032 OVERLAPS non-zero means S should draw the foreground only, and use
23033 its physical height for clipping. See also draw_glyphs.
23034
23035 Value is the index of a component not in S. */
23036
23037 static int
23038 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
23039 int overlaps)
23040 {
23041 int i;
23042 /* For all glyphs of this composition, starting at the offset
23043 S->cmp_from, until we reach the end of the definition or encounter a
23044 glyph that requires the different face, add it to S. */
23045 struct face *face;
23046
23047 eassert (s);
23048
23049 s->for_overlaps = overlaps;
23050 s->face = NULL;
23051 s->font = NULL;
23052 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
23053 {
23054 int c = COMPOSITION_GLYPH (s->cmp, i);
23055
23056 /* TAB in a composition means display glyphs with padding space
23057 on the left or right. */
23058 if (c != '\t')
23059 {
23060 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
23061 -1, Qnil);
23062
23063 face = get_char_face_and_encoding (s->f, c, face_id,
23064 s->char2b + i, 1);
23065 if (face)
23066 {
23067 if (! s->face)
23068 {
23069 s->face = face;
23070 s->font = s->face->font;
23071 }
23072 else if (s->face != face)
23073 break;
23074 }
23075 }
23076 ++s->nchars;
23077 }
23078 s->cmp_to = i;
23079
23080 if (s->face == NULL)
23081 {
23082 s->face = base_face->ascii_face;
23083 s->font = s->face->font;
23084 }
23085
23086 /* All glyph strings for the same composition has the same width,
23087 i.e. the width set for the first component of the composition. */
23088 s->width = s->first_glyph->pixel_width;
23089
23090 /* If the specified font could not be loaded, use the frame's
23091 default font, but record the fact that we couldn't load it in
23092 the glyph string so that we can draw rectangles for the
23093 characters of the glyph string. */
23094 if (s->font == NULL)
23095 {
23096 s->font_not_found_p = 1;
23097 s->font = FRAME_FONT (s->f);
23098 }
23099
23100 /* Adjust base line for subscript/superscript text. */
23101 s->ybase += s->first_glyph->voffset;
23102
23103 /* This glyph string must always be drawn with 16-bit functions. */
23104 s->two_byte_p = 1;
23105
23106 return s->cmp_to;
23107 }
23108
23109 static int
23110 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
23111 int start, int end, int overlaps)
23112 {
23113 struct glyph *glyph, *last;
23114 Lisp_Object lgstring;
23115 int i;
23116
23117 s->for_overlaps = overlaps;
23118 glyph = s->row->glyphs[s->area] + start;
23119 last = s->row->glyphs[s->area] + end;
23120 s->cmp_id = glyph->u.cmp.id;
23121 s->cmp_from = glyph->slice.cmp.from;
23122 s->cmp_to = glyph->slice.cmp.to + 1;
23123 s->face = FACE_FROM_ID (s->f, face_id);
23124 lgstring = composition_gstring_from_id (s->cmp_id);
23125 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
23126 glyph++;
23127 while (glyph < last
23128 && glyph->u.cmp.automatic
23129 && glyph->u.cmp.id == s->cmp_id
23130 && s->cmp_to == glyph->slice.cmp.from)
23131 s->cmp_to = (glyph++)->slice.cmp.to + 1;
23132
23133 for (i = s->cmp_from; i < s->cmp_to; i++)
23134 {
23135 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
23136 unsigned code = LGLYPH_CODE (lglyph);
23137
23138 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
23139 }
23140 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
23141 return glyph - s->row->glyphs[s->area];
23142 }
23143
23144
23145 /* Fill glyph string S from a sequence glyphs for glyphless characters.
23146 See the comment of fill_glyph_string for arguments.
23147 Value is the index of the first glyph not in S. */
23148
23149
23150 static int
23151 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
23152 int start, int end, int overlaps)
23153 {
23154 struct glyph *glyph, *last;
23155 int voffset;
23156
23157 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
23158 s->for_overlaps = overlaps;
23159 glyph = s->row->glyphs[s->area] + start;
23160 last = s->row->glyphs[s->area] + end;
23161 voffset = glyph->voffset;
23162 s->face = FACE_FROM_ID (s->f, face_id);
23163 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
23164 s->nchars = 1;
23165 s->width = glyph->pixel_width;
23166 glyph++;
23167 while (glyph < last
23168 && glyph->type == GLYPHLESS_GLYPH
23169 && glyph->voffset == voffset
23170 && glyph->face_id == face_id)
23171 {
23172 s->nchars++;
23173 s->width += glyph->pixel_width;
23174 glyph++;
23175 }
23176 s->ybase += voffset;
23177 return glyph - s->row->glyphs[s->area];
23178 }
23179
23180
23181 /* Fill glyph string S from a sequence of character glyphs.
23182
23183 FACE_ID is the face id of the string. START is the index of the
23184 first glyph to consider, END is the index of the last + 1.
23185 OVERLAPS non-zero means S should draw the foreground only, and use
23186 its physical height for clipping. See also draw_glyphs.
23187
23188 Value is the index of the first glyph not in S. */
23189
23190 static int
23191 fill_glyph_string (struct glyph_string *s, int face_id,
23192 int start, int end, int overlaps)
23193 {
23194 struct glyph *glyph, *last;
23195 int voffset;
23196 int glyph_not_available_p;
23197
23198 eassert (s->f == XFRAME (s->w->frame));
23199 eassert (s->nchars == 0);
23200 eassert (start >= 0 && end > start);
23201
23202 s->for_overlaps = overlaps;
23203 glyph = s->row->glyphs[s->area] + start;
23204 last = s->row->glyphs[s->area] + end;
23205 voffset = glyph->voffset;
23206 s->padding_p = glyph->padding_p;
23207 glyph_not_available_p = glyph->glyph_not_available_p;
23208
23209 while (glyph < last
23210 && glyph->type == CHAR_GLYPH
23211 && glyph->voffset == voffset
23212 /* Same face id implies same font, nowadays. */
23213 && glyph->face_id == face_id
23214 && glyph->glyph_not_available_p == glyph_not_available_p)
23215 {
23216 int two_byte_p;
23217
23218 s->face = get_glyph_face_and_encoding (s->f, glyph,
23219 s->char2b + s->nchars,
23220 &two_byte_p);
23221 s->two_byte_p = two_byte_p;
23222 ++s->nchars;
23223 eassert (s->nchars <= end - start);
23224 s->width += glyph->pixel_width;
23225 if (glyph++->padding_p != s->padding_p)
23226 break;
23227 }
23228
23229 s->font = s->face->font;
23230
23231 /* If the specified font could not be loaded, use the frame's font,
23232 but record the fact that we couldn't load it in
23233 S->font_not_found_p so that we can draw rectangles for the
23234 characters of the glyph string. */
23235 if (s->font == NULL || glyph_not_available_p)
23236 {
23237 s->font_not_found_p = 1;
23238 s->font = FRAME_FONT (s->f);
23239 }
23240
23241 /* Adjust base line for subscript/superscript text. */
23242 s->ybase += voffset;
23243
23244 eassert (s->face && s->face->gc);
23245 return glyph - s->row->glyphs[s->area];
23246 }
23247
23248
23249 /* Fill glyph string S from image glyph S->first_glyph. */
23250
23251 static void
23252 fill_image_glyph_string (struct glyph_string *s)
23253 {
23254 eassert (s->first_glyph->type == IMAGE_GLYPH);
23255 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23256 eassert (s->img);
23257 s->slice = s->first_glyph->slice.img;
23258 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23259 s->font = s->face->font;
23260 s->width = s->first_glyph->pixel_width;
23261
23262 /* Adjust base line for subscript/superscript text. */
23263 s->ybase += s->first_glyph->voffset;
23264 }
23265
23266
23267 /* Fill glyph string S from a sequence of stretch glyphs.
23268
23269 START is the index of the first glyph to consider,
23270 END is the index of the last + 1.
23271
23272 Value is the index of the first glyph not in S. */
23273
23274 static int
23275 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23276 {
23277 struct glyph *glyph, *last;
23278 int voffset, face_id;
23279
23280 eassert (s->first_glyph->type == STRETCH_GLYPH);
23281
23282 glyph = s->row->glyphs[s->area] + start;
23283 last = s->row->glyphs[s->area] + end;
23284 face_id = glyph->face_id;
23285 s->face = FACE_FROM_ID (s->f, face_id);
23286 s->font = s->face->font;
23287 s->width = glyph->pixel_width;
23288 s->nchars = 1;
23289 voffset = glyph->voffset;
23290
23291 for (++glyph;
23292 (glyph < last
23293 && glyph->type == STRETCH_GLYPH
23294 && glyph->voffset == voffset
23295 && glyph->face_id == face_id);
23296 ++glyph)
23297 s->width += glyph->pixel_width;
23298
23299 /* Adjust base line for subscript/superscript text. */
23300 s->ybase += voffset;
23301
23302 /* The case that face->gc == 0 is handled when drawing the glyph
23303 string by calling PREPARE_FACE_FOR_DISPLAY. */
23304 eassert (s->face);
23305 return glyph - s->row->glyphs[s->area];
23306 }
23307
23308 static struct font_metrics *
23309 get_per_char_metric (struct font *font, XChar2b *char2b)
23310 {
23311 static struct font_metrics metrics;
23312 unsigned code;
23313
23314 if (! font)
23315 return NULL;
23316 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23317 if (code == FONT_INVALID_CODE)
23318 return NULL;
23319 font->driver->text_extents (font, &code, 1, &metrics);
23320 return &metrics;
23321 }
23322
23323 /* EXPORT for RIF:
23324 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23325 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23326 assumed to be zero. */
23327
23328 void
23329 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23330 {
23331 *left = *right = 0;
23332
23333 if (glyph->type == CHAR_GLYPH)
23334 {
23335 struct face *face;
23336 XChar2b char2b;
23337 struct font_metrics *pcm;
23338
23339 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23340 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23341 {
23342 if (pcm->rbearing > pcm->width)
23343 *right = pcm->rbearing - pcm->width;
23344 if (pcm->lbearing < 0)
23345 *left = -pcm->lbearing;
23346 }
23347 }
23348 else if (glyph->type == COMPOSITE_GLYPH)
23349 {
23350 if (! glyph->u.cmp.automatic)
23351 {
23352 struct composition *cmp = composition_table[glyph->u.cmp.id];
23353
23354 if (cmp->rbearing > cmp->pixel_width)
23355 *right = cmp->rbearing - cmp->pixel_width;
23356 if (cmp->lbearing < 0)
23357 *left = - cmp->lbearing;
23358 }
23359 else
23360 {
23361 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23362 struct font_metrics metrics;
23363
23364 composition_gstring_width (gstring, glyph->slice.cmp.from,
23365 glyph->slice.cmp.to + 1, &metrics);
23366 if (metrics.rbearing > metrics.width)
23367 *right = metrics.rbearing - metrics.width;
23368 if (metrics.lbearing < 0)
23369 *left = - metrics.lbearing;
23370 }
23371 }
23372 }
23373
23374
23375 /* Return the index of the first glyph preceding glyph string S that
23376 is overwritten by S because of S's left overhang. Value is -1
23377 if no glyphs are overwritten. */
23378
23379 static int
23380 left_overwritten (struct glyph_string *s)
23381 {
23382 int k;
23383
23384 if (s->left_overhang)
23385 {
23386 int x = 0, i;
23387 struct glyph *glyphs = s->row->glyphs[s->area];
23388 int first = s->first_glyph - glyphs;
23389
23390 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23391 x -= glyphs[i].pixel_width;
23392
23393 k = i + 1;
23394 }
23395 else
23396 k = -1;
23397
23398 return k;
23399 }
23400
23401
23402 /* Return the index of the first glyph preceding glyph string S that
23403 is overwriting S because of its right overhang. Value is -1 if no
23404 glyph in front of S overwrites S. */
23405
23406 static int
23407 left_overwriting (struct glyph_string *s)
23408 {
23409 int i, k, x;
23410 struct glyph *glyphs = s->row->glyphs[s->area];
23411 int first = s->first_glyph - glyphs;
23412
23413 k = -1;
23414 x = 0;
23415 for (i = first - 1; i >= 0; --i)
23416 {
23417 int left, right;
23418 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23419 if (x + right > 0)
23420 k = i;
23421 x -= glyphs[i].pixel_width;
23422 }
23423
23424 return k;
23425 }
23426
23427
23428 /* Return the index of the last glyph following glyph string S that is
23429 overwritten by S because of S's right overhang. Value is -1 if
23430 no such glyph is found. */
23431
23432 static int
23433 right_overwritten (struct glyph_string *s)
23434 {
23435 int k = -1;
23436
23437 if (s->right_overhang)
23438 {
23439 int x = 0, i;
23440 struct glyph *glyphs = s->row->glyphs[s->area];
23441 int first = (s->first_glyph - glyphs
23442 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23443 int end = s->row->used[s->area];
23444
23445 for (i = first; i < end && s->right_overhang > x; ++i)
23446 x += glyphs[i].pixel_width;
23447
23448 k = i;
23449 }
23450
23451 return k;
23452 }
23453
23454
23455 /* Return the index of the last glyph following glyph string S that
23456 overwrites S because of its left overhang. Value is negative
23457 if no such glyph is found. */
23458
23459 static int
23460 right_overwriting (struct glyph_string *s)
23461 {
23462 int i, k, x;
23463 int end = s->row->used[s->area];
23464 struct glyph *glyphs = s->row->glyphs[s->area];
23465 int first = (s->first_glyph - glyphs
23466 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23467
23468 k = -1;
23469 x = 0;
23470 for (i = first; i < end; ++i)
23471 {
23472 int left, right;
23473 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23474 if (x - left < 0)
23475 k = i;
23476 x += glyphs[i].pixel_width;
23477 }
23478
23479 return k;
23480 }
23481
23482
23483 /* Set background width of glyph string S. START is the index of the
23484 first glyph following S. LAST_X is the right-most x-position + 1
23485 in the drawing area. */
23486
23487 static void
23488 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23489 {
23490 /* If the face of this glyph string has to be drawn to the end of
23491 the drawing area, set S->extends_to_end_of_line_p. */
23492
23493 if (start == s->row->used[s->area]
23494 && s->area == TEXT_AREA
23495 && ((s->row->fill_line_p
23496 && (s->hl == DRAW_NORMAL_TEXT
23497 || s->hl == DRAW_IMAGE_RAISED
23498 || s->hl == DRAW_IMAGE_SUNKEN))
23499 || s->hl == DRAW_MOUSE_FACE))
23500 s->extends_to_end_of_line_p = 1;
23501
23502 /* If S extends its face to the end of the line, set its
23503 background_width to the distance to the right edge of the drawing
23504 area. */
23505 if (s->extends_to_end_of_line_p)
23506 s->background_width = last_x - s->x + 1;
23507 else
23508 s->background_width = s->width;
23509 }
23510
23511
23512 /* Compute overhangs and x-positions for glyph string S and its
23513 predecessors, or successors. X is the starting x-position for S.
23514 BACKWARD_P non-zero means process predecessors. */
23515
23516 static void
23517 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23518 {
23519 if (backward_p)
23520 {
23521 while (s)
23522 {
23523 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23524 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23525 x -= s->width;
23526 s->x = x;
23527 s = s->prev;
23528 }
23529 }
23530 else
23531 {
23532 while (s)
23533 {
23534 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23535 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23536 s->x = x;
23537 x += s->width;
23538 s = s->next;
23539 }
23540 }
23541 }
23542
23543
23544
23545 /* The following macros are only called from draw_glyphs below.
23546 They reference the following parameters of that function directly:
23547 `w', `row', `area', and `overlap_p'
23548 as well as the following local variables:
23549 `s', `f', and `hdc' (in W32) */
23550
23551 #ifdef HAVE_NTGUI
23552 /* On W32, silently add local `hdc' variable to argument list of
23553 init_glyph_string. */
23554 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23555 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23556 #else
23557 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23558 init_glyph_string (s, char2b, w, row, area, start, hl)
23559 #endif
23560
23561 /* Add a glyph string for a stretch glyph to the list of strings
23562 between HEAD and TAIL. START is the index of the stretch glyph in
23563 row area AREA of glyph row ROW. END is the index of the last glyph
23564 in that glyph row area. X is the current output position assigned
23565 to the new glyph string constructed. HL overrides that face of the
23566 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23567 is the right-most x-position of the drawing area. */
23568
23569 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23570 and below -- keep them on one line. */
23571 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23572 do \
23573 { \
23574 s = alloca (sizeof *s); \
23575 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23576 START = fill_stretch_glyph_string (s, START, END); \
23577 append_glyph_string (&HEAD, &TAIL, s); \
23578 s->x = (X); \
23579 } \
23580 while (0)
23581
23582
23583 /* Add a glyph string for an image glyph to the list of strings
23584 between HEAD and TAIL. START is the index of the image glyph in
23585 row area AREA of glyph row ROW. END is the index of the last glyph
23586 in that glyph row area. X is the current output position assigned
23587 to the new glyph string constructed. HL overrides that face of the
23588 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23589 is the right-most x-position of the drawing area. */
23590
23591 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23592 do \
23593 { \
23594 s = alloca (sizeof *s); \
23595 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23596 fill_image_glyph_string (s); \
23597 append_glyph_string (&HEAD, &TAIL, s); \
23598 ++START; \
23599 s->x = (X); \
23600 } \
23601 while (0)
23602
23603
23604 /* Add a glyph string for a sequence of character glyphs to the list
23605 of strings between HEAD and TAIL. START is the index of the first
23606 glyph in row area AREA of glyph row ROW that is part of the new
23607 glyph string. END is the index of the last glyph in that glyph row
23608 area. X is the current output position assigned to the new glyph
23609 string constructed. HL overrides that face of the glyph; e.g. it
23610 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23611 right-most x-position of the drawing area. */
23612
23613 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23614 do \
23615 { \
23616 int face_id; \
23617 XChar2b *char2b; \
23618 \
23619 face_id = (row)->glyphs[area][START].face_id; \
23620 \
23621 s = alloca (sizeof *s); \
23622 char2b = alloca ((END - START) * sizeof *char2b); \
23623 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23624 append_glyph_string (&HEAD, &TAIL, s); \
23625 s->x = (X); \
23626 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23627 } \
23628 while (0)
23629
23630
23631 /* Add a glyph string for a composite sequence to the list of strings
23632 between HEAD and TAIL. START is the index of the first glyph in
23633 row area AREA of glyph row ROW that is part of the new glyph
23634 string. END is the index of the last glyph in that glyph row area.
23635 X is the current output position assigned to the new glyph string
23636 constructed. HL overrides that face of the glyph; e.g. it is
23637 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23638 x-position of the drawing area. */
23639
23640 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23641 do { \
23642 int face_id = (row)->glyphs[area][START].face_id; \
23643 struct face *base_face = FACE_FROM_ID (f, face_id); \
23644 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23645 struct composition *cmp = composition_table[cmp_id]; \
23646 XChar2b *char2b; \
23647 struct glyph_string *first_s = NULL; \
23648 int n; \
23649 \
23650 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23651 \
23652 /* Make glyph_strings for each glyph sequence that is drawable by \
23653 the same face, and append them to HEAD/TAIL. */ \
23654 for (n = 0; n < cmp->glyph_len;) \
23655 { \
23656 s = alloca (sizeof *s); \
23657 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23658 append_glyph_string (&(HEAD), &(TAIL), s); \
23659 s->cmp = cmp; \
23660 s->cmp_from = n; \
23661 s->x = (X); \
23662 if (n == 0) \
23663 first_s = s; \
23664 n = fill_composite_glyph_string (s, base_face, overlaps); \
23665 } \
23666 \
23667 ++START; \
23668 s = first_s; \
23669 } while (0)
23670
23671
23672 /* Add a glyph string for a glyph-string sequence to the list of strings
23673 between HEAD and TAIL. */
23674
23675 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23676 do { \
23677 int face_id; \
23678 XChar2b *char2b; \
23679 Lisp_Object gstring; \
23680 \
23681 face_id = (row)->glyphs[area][START].face_id; \
23682 gstring = (composition_gstring_from_id \
23683 ((row)->glyphs[area][START].u.cmp.id)); \
23684 s = alloca (sizeof *s); \
23685 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23686 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23687 append_glyph_string (&(HEAD), &(TAIL), s); \
23688 s->x = (X); \
23689 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23690 } while (0)
23691
23692
23693 /* Add a glyph string for a sequence of glyphless character's glyphs
23694 to the list of strings between HEAD and TAIL. The meanings of
23695 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23696
23697 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23698 do \
23699 { \
23700 int face_id; \
23701 \
23702 face_id = (row)->glyphs[area][START].face_id; \
23703 \
23704 s = alloca (sizeof *s); \
23705 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23706 append_glyph_string (&HEAD, &TAIL, s); \
23707 s->x = (X); \
23708 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23709 overlaps); \
23710 } \
23711 while (0)
23712
23713
23714 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23715 of AREA of glyph row ROW on window W between indices START and END.
23716 HL overrides the face for drawing glyph strings, e.g. it is
23717 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23718 x-positions of the drawing area.
23719
23720 This is an ugly monster macro construct because we must use alloca
23721 to allocate glyph strings (because draw_glyphs can be called
23722 asynchronously). */
23723
23724 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23725 do \
23726 { \
23727 HEAD = TAIL = NULL; \
23728 while (START < END) \
23729 { \
23730 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23731 switch (first_glyph->type) \
23732 { \
23733 case CHAR_GLYPH: \
23734 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23735 HL, X, LAST_X); \
23736 break; \
23737 \
23738 case COMPOSITE_GLYPH: \
23739 if (first_glyph->u.cmp.automatic) \
23740 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23741 HL, X, LAST_X); \
23742 else \
23743 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23744 HL, X, LAST_X); \
23745 break; \
23746 \
23747 case STRETCH_GLYPH: \
23748 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23749 HL, X, LAST_X); \
23750 break; \
23751 \
23752 case IMAGE_GLYPH: \
23753 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23754 HL, X, LAST_X); \
23755 break; \
23756 \
23757 case GLYPHLESS_GLYPH: \
23758 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23759 HL, X, LAST_X); \
23760 break; \
23761 \
23762 default: \
23763 emacs_abort (); \
23764 } \
23765 \
23766 if (s) \
23767 { \
23768 set_glyph_string_background_width (s, START, LAST_X); \
23769 (X) += s->width; \
23770 } \
23771 } \
23772 } while (0)
23773
23774
23775 /* Draw glyphs between START and END in AREA of ROW on window W,
23776 starting at x-position X. X is relative to AREA in W. HL is a
23777 face-override with the following meaning:
23778
23779 DRAW_NORMAL_TEXT draw normally
23780 DRAW_CURSOR draw in cursor face
23781 DRAW_MOUSE_FACE draw in mouse face.
23782 DRAW_INVERSE_VIDEO draw in mode line face
23783 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23784 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23785
23786 If OVERLAPS is non-zero, draw only the foreground of characters and
23787 clip to the physical height of ROW. Non-zero value also defines
23788 the overlapping part to be drawn:
23789
23790 OVERLAPS_PRED overlap with preceding rows
23791 OVERLAPS_SUCC overlap with succeeding rows
23792 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23793 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23794
23795 Value is the x-position reached, relative to AREA of W. */
23796
23797 static int
23798 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23799 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23800 enum draw_glyphs_face hl, int overlaps)
23801 {
23802 struct glyph_string *head, *tail;
23803 struct glyph_string *s;
23804 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23805 int i, j, x_reached, last_x, area_left = 0;
23806 struct frame *f = XFRAME (WINDOW_FRAME (w));
23807 DECLARE_HDC (hdc);
23808
23809 ALLOCATE_HDC (hdc, f);
23810
23811 /* Let's rather be paranoid than getting a SEGV. */
23812 end = min (end, row->used[area]);
23813 start = clip_to_bounds (0, start, end);
23814
23815 /* Translate X to frame coordinates. Set last_x to the right
23816 end of the drawing area. */
23817 if (row->full_width_p)
23818 {
23819 /* X is relative to the left edge of W, without scroll bars
23820 or fringes. */
23821 area_left = WINDOW_LEFT_EDGE_X (w);
23822 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23823 }
23824 else
23825 {
23826 area_left = window_box_left (w, area);
23827 last_x = area_left + window_box_width (w, area);
23828 }
23829 x += area_left;
23830
23831 /* Build a doubly-linked list of glyph_string structures between
23832 head and tail from what we have to draw. Note that the macro
23833 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23834 the reason we use a separate variable `i'. */
23835 i = start;
23836 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23837 if (tail)
23838 x_reached = tail->x + tail->background_width;
23839 else
23840 x_reached = x;
23841
23842 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23843 the row, redraw some glyphs in front or following the glyph
23844 strings built above. */
23845 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23846 {
23847 struct glyph_string *h, *t;
23848 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23849 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23850 int check_mouse_face = 0;
23851 int dummy_x = 0;
23852
23853 /* If mouse highlighting is on, we may need to draw adjacent
23854 glyphs using mouse-face highlighting. */
23855 if (area == TEXT_AREA && row->mouse_face_p
23856 && hlinfo->mouse_face_beg_row >= 0
23857 && hlinfo->mouse_face_end_row >= 0)
23858 {
23859 struct glyph_row *mouse_beg_row, *mouse_end_row;
23860
23861 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23862 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23863
23864 if (row >= mouse_beg_row && row <= mouse_end_row)
23865 {
23866 check_mouse_face = 1;
23867 mouse_beg_col = (row == mouse_beg_row)
23868 ? hlinfo->mouse_face_beg_col : 0;
23869 mouse_end_col = (row == mouse_end_row)
23870 ? hlinfo->mouse_face_end_col
23871 : row->used[TEXT_AREA];
23872 }
23873 }
23874
23875 /* Compute overhangs for all glyph strings. */
23876 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23877 for (s = head; s; s = s->next)
23878 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23879
23880 /* Prepend glyph strings for glyphs in front of the first glyph
23881 string that are overwritten because of the first glyph
23882 string's left overhang. The background of all strings
23883 prepended must be drawn because the first glyph string
23884 draws over it. */
23885 i = left_overwritten (head);
23886 if (i >= 0)
23887 {
23888 enum draw_glyphs_face overlap_hl;
23889
23890 /* If this row contains mouse highlighting, attempt to draw
23891 the overlapped glyphs with the correct highlight. This
23892 code fails if the overlap encompasses more than one glyph
23893 and mouse-highlight spans only some of these glyphs.
23894 However, making it work perfectly involves a lot more
23895 code, and I don't know if the pathological case occurs in
23896 practice, so we'll stick to this for now. --- cyd */
23897 if (check_mouse_face
23898 && mouse_beg_col < start && mouse_end_col > i)
23899 overlap_hl = DRAW_MOUSE_FACE;
23900 else
23901 overlap_hl = DRAW_NORMAL_TEXT;
23902
23903 j = i;
23904 BUILD_GLYPH_STRINGS (j, start, h, t,
23905 overlap_hl, dummy_x, last_x);
23906 start = i;
23907 compute_overhangs_and_x (t, head->x, 1);
23908 prepend_glyph_string_lists (&head, &tail, h, t);
23909 clip_head = head;
23910 }
23911
23912 /* Prepend glyph strings for glyphs in front of the first glyph
23913 string that overwrite that glyph string because of their
23914 right overhang. For these strings, only the foreground must
23915 be drawn, because it draws over the glyph string at `head'.
23916 The background must not be drawn because this would overwrite
23917 right overhangs of preceding glyphs for which no glyph
23918 strings exist. */
23919 i = left_overwriting (head);
23920 if (i >= 0)
23921 {
23922 enum draw_glyphs_face overlap_hl;
23923
23924 if (check_mouse_face
23925 && mouse_beg_col < start && mouse_end_col > i)
23926 overlap_hl = DRAW_MOUSE_FACE;
23927 else
23928 overlap_hl = DRAW_NORMAL_TEXT;
23929
23930 clip_head = head;
23931 BUILD_GLYPH_STRINGS (i, start, h, t,
23932 overlap_hl, dummy_x, last_x);
23933 for (s = h; s; s = s->next)
23934 s->background_filled_p = 1;
23935 compute_overhangs_and_x (t, head->x, 1);
23936 prepend_glyph_string_lists (&head, &tail, h, t);
23937 }
23938
23939 /* Append glyphs strings for glyphs following the last glyph
23940 string tail that are overwritten by tail. The background of
23941 these strings has to be drawn because tail's foreground draws
23942 over it. */
23943 i = right_overwritten (tail);
23944 if (i >= 0)
23945 {
23946 enum draw_glyphs_face overlap_hl;
23947
23948 if (check_mouse_face
23949 && mouse_beg_col < i && mouse_end_col > end)
23950 overlap_hl = DRAW_MOUSE_FACE;
23951 else
23952 overlap_hl = DRAW_NORMAL_TEXT;
23953
23954 BUILD_GLYPH_STRINGS (end, i, h, t,
23955 overlap_hl, x, last_x);
23956 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23957 we don't have `end = i;' here. */
23958 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23959 append_glyph_string_lists (&head, &tail, h, t);
23960 clip_tail = tail;
23961 }
23962
23963 /* Append glyph strings for glyphs following the last glyph
23964 string tail that overwrite tail. The foreground of such
23965 glyphs has to be drawn because it writes into the background
23966 of tail. The background must not be drawn because it could
23967 paint over the foreground of following glyphs. */
23968 i = right_overwriting (tail);
23969 if (i >= 0)
23970 {
23971 enum draw_glyphs_face overlap_hl;
23972 if (check_mouse_face
23973 && mouse_beg_col < i && mouse_end_col > end)
23974 overlap_hl = DRAW_MOUSE_FACE;
23975 else
23976 overlap_hl = DRAW_NORMAL_TEXT;
23977
23978 clip_tail = tail;
23979 i++; /* We must include the Ith glyph. */
23980 BUILD_GLYPH_STRINGS (end, i, h, t,
23981 overlap_hl, x, last_x);
23982 for (s = h; s; s = s->next)
23983 s->background_filled_p = 1;
23984 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23985 append_glyph_string_lists (&head, &tail, h, t);
23986 }
23987 if (clip_head || clip_tail)
23988 for (s = head; s; s = s->next)
23989 {
23990 s->clip_head = clip_head;
23991 s->clip_tail = clip_tail;
23992 }
23993 }
23994
23995 /* Draw all strings. */
23996 for (s = head; s; s = s->next)
23997 FRAME_RIF (f)->draw_glyph_string (s);
23998
23999 #ifndef HAVE_NS
24000 /* When focus a sole frame and move horizontally, this sets on_p to 0
24001 causing a failure to erase prev cursor position. */
24002 if (area == TEXT_AREA
24003 && !row->full_width_p
24004 /* When drawing overlapping rows, only the glyph strings'
24005 foreground is drawn, which doesn't erase a cursor
24006 completely. */
24007 && !overlaps)
24008 {
24009 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
24010 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
24011 : (tail ? tail->x + tail->background_width : x));
24012 x0 -= area_left;
24013 x1 -= area_left;
24014
24015 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
24016 row->y, MATRIX_ROW_BOTTOM_Y (row));
24017 }
24018 #endif
24019
24020 /* Value is the x-position up to which drawn, relative to AREA of W.
24021 This doesn't include parts drawn because of overhangs. */
24022 if (row->full_width_p)
24023 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
24024 else
24025 x_reached -= area_left;
24026
24027 RELEASE_HDC (hdc, f);
24028
24029 return x_reached;
24030 }
24031
24032 /* Expand row matrix if too narrow. Don't expand if area
24033 is not present. */
24034
24035 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
24036 { \
24037 if (!fonts_changed_p \
24038 && (it->glyph_row->glyphs[area] \
24039 < it->glyph_row->glyphs[area + 1])) \
24040 { \
24041 it->w->ncols_scale_factor++; \
24042 fonts_changed_p = 1; \
24043 } \
24044 }
24045
24046 /* Store one glyph for IT->char_to_display in IT->glyph_row.
24047 Called from x_produce_glyphs when IT->glyph_row is non-null. */
24048
24049 static void
24050 append_glyph (struct it *it)
24051 {
24052 struct glyph *glyph;
24053 enum glyph_row_area area = it->area;
24054
24055 eassert (it->glyph_row);
24056 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
24057
24058 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24059 if (glyph < it->glyph_row->glyphs[area + 1])
24060 {
24061 /* If the glyph row is reversed, we need to prepend the glyph
24062 rather than append it. */
24063 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24064 {
24065 struct glyph *g;
24066
24067 /* Make room for the additional glyph. */
24068 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24069 g[1] = *g;
24070 glyph = it->glyph_row->glyphs[area];
24071 }
24072 glyph->charpos = CHARPOS (it->position);
24073 glyph->object = it->object;
24074 if (it->pixel_width > 0)
24075 {
24076 glyph->pixel_width = it->pixel_width;
24077 glyph->padding_p = 0;
24078 }
24079 else
24080 {
24081 /* Assure at least 1-pixel width. Otherwise, cursor can't
24082 be displayed correctly. */
24083 glyph->pixel_width = 1;
24084 glyph->padding_p = 1;
24085 }
24086 glyph->ascent = it->ascent;
24087 glyph->descent = it->descent;
24088 glyph->voffset = it->voffset;
24089 glyph->type = CHAR_GLYPH;
24090 glyph->avoid_cursor_p = it->avoid_cursor_p;
24091 glyph->multibyte_p = it->multibyte_p;
24092 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24093 {
24094 /* In R2L rows, the left and the right box edges need to be
24095 drawn in reverse direction. */
24096 glyph->right_box_line_p = it->start_of_box_run_p;
24097 glyph->left_box_line_p = it->end_of_box_run_p;
24098 }
24099 else
24100 {
24101 glyph->left_box_line_p = it->start_of_box_run_p;
24102 glyph->right_box_line_p = it->end_of_box_run_p;
24103 }
24104 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24105 || it->phys_descent > it->descent);
24106 glyph->glyph_not_available_p = it->glyph_not_available_p;
24107 glyph->face_id = it->face_id;
24108 glyph->u.ch = it->char_to_display;
24109 glyph->slice.img = null_glyph_slice;
24110 glyph->font_type = FONT_TYPE_UNKNOWN;
24111 if (it->bidi_p)
24112 {
24113 glyph->resolved_level = it->bidi_it.resolved_level;
24114 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24115 emacs_abort ();
24116 glyph->bidi_type = it->bidi_it.type;
24117 }
24118 else
24119 {
24120 glyph->resolved_level = 0;
24121 glyph->bidi_type = UNKNOWN_BT;
24122 }
24123 ++it->glyph_row->used[area];
24124 }
24125 else
24126 IT_EXPAND_MATRIX_WIDTH (it, area);
24127 }
24128
24129 /* Store one glyph for the composition IT->cmp_it.id in
24130 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
24131 non-null. */
24132
24133 static void
24134 append_composite_glyph (struct it *it)
24135 {
24136 struct glyph *glyph;
24137 enum glyph_row_area area = it->area;
24138
24139 eassert (it->glyph_row);
24140
24141 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24142 if (glyph < it->glyph_row->glyphs[area + 1])
24143 {
24144 /* If the glyph row is reversed, we need to prepend the glyph
24145 rather than append it. */
24146 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
24147 {
24148 struct glyph *g;
24149
24150 /* Make room for the new glyph. */
24151 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
24152 g[1] = *g;
24153 glyph = it->glyph_row->glyphs[it->area];
24154 }
24155 glyph->charpos = it->cmp_it.charpos;
24156 glyph->object = it->object;
24157 glyph->pixel_width = it->pixel_width;
24158 glyph->ascent = it->ascent;
24159 glyph->descent = it->descent;
24160 glyph->voffset = it->voffset;
24161 glyph->type = COMPOSITE_GLYPH;
24162 if (it->cmp_it.ch < 0)
24163 {
24164 glyph->u.cmp.automatic = 0;
24165 glyph->u.cmp.id = it->cmp_it.id;
24166 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
24167 }
24168 else
24169 {
24170 glyph->u.cmp.automatic = 1;
24171 glyph->u.cmp.id = it->cmp_it.id;
24172 glyph->slice.cmp.from = it->cmp_it.from;
24173 glyph->slice.cmp.to = it->cmp_it.to - 1;
24174 }
24175 glyph->avoid_cursor_p = it->avoid_cursor_p;
24176 glyph->multibyte_p = it->multibyte_p;
24177 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24178 {
24179 /* In R2L rows, the left and the right box edges need to be
24180 drawn in reverse direction. */
24181 glyph->right_box_line_p = it->start_of_box_run_p;
24182 glyph->left_box_line_p = it->end_of_box_run_p;
24183 }
24184 else
24185 {
24186 glyph->left_box_line_p = it->start_of_box_run_p;
24187 glyph->right_box_line_p = it->end_of_box_run_p;
24188 }
24189 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24190 || it->phys_descent > it->descent);
24191 glyph->padding_p = 0;
24192 glyph->glyph_not_available_p = 0;
24193 glyph->face_id = it->face_id;
24194 glyph->font_type = FONT_TYPE_UNKNOWN;
24195 if (it->bidi_p)
24196 {
24197 glyph->resolved_level = it->bidi_it.resolved_level;
24198 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24199 emacs_abort ();
24200 glyph->bidi_type = it->bidi_it.type;
24201 }
24202 ++it->glyph_row->used[area];
24203 }
24204 else
24205 IT_EXPAND_MATRIX_WIDTH (it, area);
24206 }
24207
24208
24209 /* Change IT->ascent and IT->height according to the setting of
24210 IT->voffset. */
24211
24212 static void
24213 take_vertical_position_into_account (struct it *it)
24214 {
24215 if (it->voffset)
24216 {
24217 if (it->voffset < 0)
24218 /* Increase the ascent so that we can display the text higher
24219 in the line. */
24220 it->ascent -= it->voffset;
24221 else
24222 /* Increase the descent so that we can display the text lower
24223 in the line. */
24224 it->descent += it->voffset;
24225 }
24226 }
24227
24228
24229 /* Produce glyphs/get display metrics for the image IT is loaded with.
24230 See the description of struct display_iterator in dispextern.h for
24231 an overview of struct display_iterator. */
24232
24233 static void
24234 produce_image_glyph (struct it *it)
24235 {
24236 struct image *img;
24237 struct face *face;
24238 int glyph_ascent, crop;
24239 struct glyph_slice slice;
24240
24241 eassert (it->what == IT_IMAGE);
24242
24243 face = FACE_FROM_ID (it->f, it->face_id);
24244 eassert (face);
24245 /* Make sure X resources of the face is loaded. */
24246 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24247
24248 if (it->image_id < 0)
24249 {
24250 /* Fringe bitmap. */
24251 it->ascent = it->phys_ascent = 0;
24252 it->descent = it->phys_descent = 0;
24253 it->pixel_width = 0;
24254 it->nglyphs = 0;
24255 return;
24256 }
24257
24258 img = IMAGE_FROM_ID (it->f, it->image_id);
24259 eassert (img);
24260 /* Make sure X resources of the image is loaded. */
24261 prepare_image_for_display (it->f, img);
24262
24263 slice.x = slice.y = 0;
24264 slice.width = img->width;
24265 slice.height = img->height;
24266
24267 if (INTEGERP (it->slice.x))
24268 slice.x = XINT (it->slice.x);
24269 else if (FLOATP (it->slice.x))
24270 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24271
24272 if (INTEGERP (it->slice.y))
24273 slice.y = XINT (it->slice.y);
24274 else if (FLOATP (it->slice.y))
24275 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24276
24277 if (INTEGERP (it->slice.width))
24278 slice.width = XINT (it->slice.width);
24279 else if (FLOATP (it->slice.width))
24280 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24281
24282 if (INTEGERP (it->slice.height))
24283 slice.height = XINT (it->slice.height);
24284 else if (FLOATP (it->slice.height))
24285 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24286
24287 if (slice.x >= img->width)
24288 slice.x = img->width;
24289 if (slice.y >= img->height)
24290 slice.y = img->height;
24291 if (slice.x + slice.width >= img->width)
24292 slice.width = img->width - slice.x;
24293 if (slice.y + slice.height > img->height)
24294 slice.height = img->height - slice.y;
24295
24296 if (slice.width == 0 || slice.height == 0)
24297 return;
24298
24299 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24300
24301 it->descent = slice.height - glyph_ascent;
24302 if (slice.y == 0)
24303 it->descent += img->vmargin;
24304 if (slice.y + slice.height == img->height)
24305 it->descent += img->vmargin;
24306 it->phys_descent = it->descent;
24307
24308 it->pixel_width = slice.width;
24309 if (slice.x == 0)
24310 it->pixel_width += img->hmargin;
24311 if (slice.x + slice.width == img->width)
24312 it->pixel_width += img->hmargin;
24313
24314 /* It's quite possible for images to have an ascent greater than
24315 their height, so don't get confused in that case. */
24316 if (it->descent < 0)
24317 it->descent = 0;
24318
24319 it->nglyphs = 1;
24320
24321 if (face->box != FACE_NO_BOX)
24322 {
24323 if (face->box_line_width > 0)
24324 {
24325 if (slice.y == 0)
24326 it->ascent += face->box_line_width;
24327 if (slice.y + slice.height == img->height)
24328 it->descent += face->box_line_width;
24329 }
24330
24331 if (it->start_of_box_run_p && slice.x == 0)
24332 it->pixel_width += eabs (face->box_line_width);
24333 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24334 it->pixel_width += eabs (face->box_line_width);
24335 }
24336
24337 take_vertical_position_into_account (it);
24338
24339 /* Automatically crop wide image glyphs at right edge so we can
24340 draw the cursor on same display row. */
24341 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24342 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24343 {
24344 it->pixel_width -= crop;
24345 slice.width -= crop;
24346 }
24347
24348 if (it->glyph_row)
24349 {
24350 struct glyph *glyph;
24351 enum glyph_row_area area = it->area;
24352
24353 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24354 if (glyph < it->glyph_row->glyphs[area + 1])
24355 {
24356 glyph->charpos = CHARPOS (it->position);
24357 glyph->object = it->object;
24358 glyph->pixel_width = it->pixel_width;
24359 glyph->ascent = glyph_ascent;
24360 glyph->descent = it->descent;
24361 glyph->voffset = it->voffset;
24362 glyph->type = IMAGE_GLYPH;
24363 glyph->avoid_cursor_p = it->avoid_cursor_p;
24364 glyph->multibyte_p = it->multibyte_p;
24365 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24366 {
24367 /* In R2L rows, the left and the right box edges need to be
24368 drawn in reverse direction. */
24369 glyph->right_box_line_p = it->start_of_box_run_p;
24370 glyph->left_box_line_p = it->end_of_box_run_p;
24371 }
24372 else
24373 {
24374 glyph->left_box_line_p = it->start_of_box_run_p;
24375 glyph->right_box_line_p = it->end_of_box_run_p;
24376 }
24377 glyph->overlaps_vertically_p = 0;
24378 glyph->padding_p = 0;
24379 glyph->glyph_not_available_p = 0;
24380 glyph->face_id = it->face_id;
24381 glyph->u.img_id = img->id;
24382 glyph->slice.img = slice;
24383 glyph->font_type = FONT_TYPE_UNKNOWN;
24384 if (it->bidi_p)
24385 {
24386 glyph->resolved_level = it->bidi_it.resolved_level;
24387 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24388 emacs_abort ();
24389 glyph->bidi_type = it->bidi_it.type;
24390 }
24391 ++it->glyph_row->used[area];
24392 }
24393 else
24394 IT_EXPAND_MATRIX_WIDTH (it, area);
24395 }
24396 }
24397
24398
24399 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24400 of the glyph, WIDTH and HEIGHT are the width and height of the
24401 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24402
24403 static void
24404 append_stretch_glyph (struct it *it, Lisp_Object object,
24405 int width, int height, int ascent)
24406 {
24407 struct glyph *glyph;
24408 enum glyph_row_area area = it->area;
24409
24410 eassert (ascent >= 0 && ascent <= height);
24411
24412 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24413 if (glyph < it->glyph_row->glyphs[area + 1])
24414 {
24415 /* If the glyph row is reversed, we need to prepend the glyph
24416 rather than append it. */
24417 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24418 {
24419 struct glyph *g;
24420
24421 /* Make room for the additional glyph. */
24422 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24423 g[1] = *g;
24424 glyph = it->glyph_row->glyphs[area];
24425 }
24426 glyph->charpos = CHARPOS (it->position);
24427 glyph->object = object;
24428 glyph->pixel_width = width;
24429 glyph->ascent = ascent;
24430 glyph->descent = height - ascent;
24431 glyph->voffset = it->voffset;
24432 glyph->type = STRETCH_GLYPH;
24433 glyph->avoid_cursor_p = it->avoid_cursor_p;
24434 glyph->multibyte_p = it->multibyte_p;
24435 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24436 {
24437 /* In R2L rows, the left and the right box edges need to be
24438 drawn in reverse direction. */
24439 glyph->right_box_line_p = it->start_of_box_run_p;
24440 glyph->left_box_line_p = it->end_of_box_run_p;
24441 }
24442 else
24443 {
24444 glyph->left_box_line_p = it->start_of_box_run_p;
24445 glyph->right_box_line_p = it->end_of_box_run_p;
24446 }
24447 glyph->overlaps_vertically_p = 0;
24448 glyph->padding_p = 0;
24449 glyph->glyph_not_available_p = 0;
24450 glyph->face_id = it->face_id;
24451 glyph->u.stretch.ascent = ascent;
24452 glyph->u.stretch.height = height;
24453 glyph->slice.img = null_glyph_slice;
24454 glyph->font_type = FONT_TYPE_UNKNOWN;
24455 if (it->bidi_p)
24456 {
24457 glyph->resolved_level = it->bidi_it.resolved_level;
24458 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24459 emacs_abort ();
24460 glyph->bidi_type = it->bidi_it.type;
24461 }
24462 else
24463 {
24464 glyph->resolved_level = 0;
24465 glyph->bidi_type = UNKNOWN_BT;
24466 }
24467 ++it->glyph_row->used[area];
24468 }
24469 else
24470 IT_EXPAND_MATRIX_WIDTH (it, area);
24471 }
24472
24473 #endif /* HAVE_WINDOW_SYSTEM */
24474
24475 /* Produce a stretch glyph for iterator IT. IT->object is the value
24476 of the glyph property displayed. The value must be a list
24477 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24478 being recognized:
24479
24480 1. `:width WIDTH' specifies that the space should be WIDTH *
24481 canonical char width wide. WIDTH may be an integer or floating
24482 point number.
24483
24484 2. `:relative-width FACTOR' specifies that the width of the stretch
24485 should be computed from the width of the first character having the
24486 `glyph' property, and should be FACTOR times that width.
24487
24488 3. `:align-to HPOS' specifies that the space should be wide enough
24489 to reach HPOS, a value in canonical character units.
24490
24491 Exactly one of the above pairs must be present.
24492
24493 4. `:height HEIGHT' specifies that the height of the stretch produced
24494 should be HEIGHT, measured in canonical character units.
24495
24496 5. `:relative-height FACTOR' specifies that the height of the
24497 stretch should be FACTOR times the height of the characters having
24498 the glyph property.
24499
24500 Either none or exactly one of 4 or 5 must be present.
24501
24502 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24503 of the stretch should be used for the ascent of the stretch.
24504 ASCENT must be in the range 0 <= ASCENT <= 100. */
24505
24506 void
24507 produce_stretch_glyph (struct it *it)
24508 {
24509 /* (space :width WIDTH :height HEIGHT ...) */
24510 Lisp_Object prop, plist;
24511 int width = 0, height = 0, align_to = -1;
24512 int zero_width_ok_p = 0;
24513 double tem;
24514 struct font *font = NULL;
24515
24516 #ifdef HAVE_WINDOW_SYSTEM
24517 int ascent = 0;
24518 int zero_height_ok_p = 0;
24519
24520 if (FRAME_WINDOW_P (it->f))
24521 {
24522 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24523 font = face->font ? face->font : FRAME_FONT (it->f);
24524 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24525 }
24526 #endif
24527
24528 /* List should start with `space'. */
24529 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24530 plist = XCDR (it->object);
24531
24532 /* Compute the width of the stretch. */
24533 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24534 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24535 {
24536 /* Absolute width `:width WIDTH' specified and valid. */
24537 zero_width_ok_p = 1;
24538 width = (int)tem;
24539 }
24540 #ifdef HAVE_WINDOW_SYSTEM
24541 else if (FRAME_WINDOW_P (it->f)
24542 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24543 {
24544 /* Relative width `:relative-width FACTOR' specified and valid.
24545 Compute the width of the characters having the `glyph'
24546 property. */
24547 struct it it2;
24548 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24549
24550 it2 = *it;
24551 if (it->multibyte_p)
24552 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24553 else
24554 {
24555 it2.c = it2.char_to_display = *p, it2.len = 1;
24556 if (! ASCII_CHAR_P (it2.c))
24557 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24558 }
24559
24560 it2.glyph_row = NULL;
24561 it2.what = IT_CHARACTER;
24562 x_produce_glyphs (&it2);
24563 width = NUMVAL (prop) * it2.pixel_width;
24564 }
24565 #endif /* HAVE_WINDOW_SYSTEM */
24566 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24567 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24568 {
24569 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24570 align_to = (align_to < 0
24571 ? 0
24572 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24573 else if (align_to < 0)
24574 align_to = window_box_left_offset (it->w, TEXT_AREA);
24575 width = max (0, (int)tem + align_to - it->current_x);
24576 zero_width_ok_p = 1;
24577 }
24578 else
24579 /* Nothing specified -> width defaults to canonical char width. */
24580 width = FRAME_COLUMN_WIDTH (it->f);
24581
24582 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24583 width = 1;
24584
24585 #ifdef HAVE_WINDOW_SYSTEM
24586 /* Compute height. */
24587 if (FRAME_WINDOW_P (it->f))
24588 {
24589 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24590 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24591 {
24592 height = (int)tem;
24593 zero_height_ok_p = 1;
24594 }
24595 else if (prop = Fplist_get (plist, QCrelative_height),
24596 NUMVAL (prop) > 0)
24597 height = FONT_HEIGHT (font) * NUMVAL (prop);
24598 else
24599 height = FONT_HEIGHT (font);
24600
24601 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24602 height = 1;
24603
24604 /* Compute percentage of height used for ascent. If
24605 `:ascent ASCENT' is present and valid, use that. Otherwise,
24606 derive the ascent from the font in use. */
24607 if (prop = Fplist_get (plist, QCascent),
24608 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24609 ascent = height * NUMVAL (prop) / 100.0;
24610 else if (!NILP (prop)
24611 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24612 ascent = min (max (0, (int)tem), height);
24613 else
24614 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24615 }
24616 else
24617 #endif /* HAVE_WINDOW_SYSTEM */
24618 height = 1;
24619
24620 if (width > 0 && it->line_wrap != TRUNCATE
24621 && it->current_x + width > it->last_visible_x)
24622 {
24623 width = it->last_visible_x - it->current_x;
24624 #ifdef HAVE_WINDOW_SYSTEM
24625 /* Subtract one more pixel from the stretch width, but only on
24626 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24627 width -= FRAME_WINDOW_P (it->f);
24628 #endif
24629 }
24630
24631 if (width > 0 && height > 0 && it->glyph_row)
24632 {
24633 Lisp_Object o_object = it->object;
24634 Lisp_Object object = it->stack[it->sp - 1].string;
24635 int n = width;
24636
24637 if (!STRINGP (object))
24638 object = it->w->contents;
24639 #ifdef HAVE_WINDOW_SYSTEM
24640 if (FRAME_WINDOW_P (it->f))
24641 append_stretch_glyph (it, object, width, height, ascent);
24642 else
24643 #endif
24644 {
24645 it->object = object;
24646 it->char_to_display = ' ';
24647 it->pixel_width = it->len = 1;
24648 while (n--)
24649 tty_append_glyph (it);
24650 it->object = o_object;
24651 }
24652 }
24653
24654 it->pixel_width = width;
24655 #ifdef HAVE_WINDOW_SYSTEM
24656 if (FRAME_WINDOW_P (it->f))
24657 {
24658 it->ascent = it->phys_ascent = ascent;
24659 it->descent = it->phys_descent = height - it->ascent;
24660 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24661 take_vertical_position_into_account (it);
24662 }
24663 else
24664 #endif
24665 it->nglyphs = width;
24666 }
24667
24668 /* Get information about special display element WHAT in an
24669 environment described by IT. WHAT is one of IT_TRUNCATION or
24670 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24671 non-null glyph_row member. This function ensures that fields like
24672 face_id, c, len of IT are left untouched. */
24673
24674 static void
24675 produce_special_glyphs (struct it *it, enum display_element_type what)
24676 {
24677 struct it temp_it;
24678 Lisp_Object gc;
24679 GLYPH glyph;
24680
24681 temp_it = *it;
24682 temp_it.object = make_number (0);
24683 memset (&temp_it.current, 0, sizeof temp_it.current);
24684
24685 if (what == IT_CONTINUATION)
24686 {
24687 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24688 if (it->bidi_it.paragraph_dir == R2L)
24689 SET_GLYPH_FROM_CHAR (glyph, '/');
24690 else
24691 SET_GLYPH_FROM_CHAR (glyph, '\\');
24692 if (it->dp
24693 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24694 {
24695 /* FIXME: Should we mirror GC for R2L lines? */
24696 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24697 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24698 }
24699 }
24700 else if (what == IT_TRUNCATION)
24701 {
24702 /* Truncation glyph. */
24703 SET_GLYPH_FROM_CHAR (glyph, '$');
24704 if (it->dp
24705 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24706 {
24707 /* FIXME: Should we mirror GC for R2L lines? */
24708 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24709 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24710 }
24711 }
24712 else
24713 emacs_abort ();
24714
24715 #ifdef HAVE_WINDOW_SYSTEM
24716 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24717 is turned off, we precede the truncation/continuation glyphs by a
24718 stretch glyph whose width is computed such that these special
24719 glyphs are aligned at the window margin, even when very different
24720 fonts are used in different glyph rows. */
24721 if (FRAME_WINDOW_P (temp_it.f)
24722 /* init_iterator calls this with it->glyph_row == NULL, and it
24723 wants only the pixel width of the truncation/continuation
24724 glyphs. */
24725 && temp_it.glyph_row
24726 /* insert_left_trunc_glyphs calls us at the beginning of the
24727 row, and it has its own calculation of the stretch glyph
24728 width. */
24729 && temp_it.glyph_row->used[TEXT_AREA] > 0
24730 && (temp_it.glyph_row->reversed_p
24731 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24732 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24733 {
24734 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24735
24736 if (stretch_width > 0)
24737 {
24738 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24739 struct font *font =
24740 face->font ? face->font : FRAME_FONT (temp_it.f);
24741 int stretch_ascent =
24742 (((temp_it.ascent + temp_it.descent)
24743 * FONT_BASE (font)) / FONT_HEIGHT (font));
24744
24745 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24746 temp_it.ascent + temp_it.descent,
24747 stretch_ascent);
24748 }
24749 }
24750 #endif
24751
24752 temp_it.dp = NULL;
24753 temp_it.what = IT_CHARACTER;
24754 temp_it.len = 1;
24755 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24756 temp_it.face_id = GLYPH_FACE (glyph);
24757 temp_it.len = CHAR_BYTES (temp_it.c);
24758
24759 PRODUCE_GLYPHS (&temp_it);
24760 it->pixel_width = temp_it.pixel_width;
24761 it->nglyphs = temp_it.pixel_width;
24762 }
24763
24764 #ifdef HAVE_WINDOW_SYSTEM
24765
24766 /* Calculate line-height and line-spacing properties.
24767 An integer value specifies explicit pixel value.
24768 A float value specifies relative value to current face height.
24769 A cons (float . face-name) specifies relative value to
24770 height of specified face font.
24771
24772 Returns height in pixels, or nil. */
24773
24774
24775 static Lisp_Object
24776 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24777 int boff, int override)
24778 {
24779 Lisp_Object face_name = Qnil;
24780 int ascent, descent, height;
24781
24782 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24783 return val;
24784
24785 if (CONSP (val))
24786 {
24787 face_name = XCAR (val);
24788 val = XCDR (val);
24789 if (!NUMBERP (val))
24790 val = make_number (1);
24791 if (NILP (face_name))
24792 {
24793 height = it->ascent + it->descent;
24794 goto scale;
24795 }
24796 }
24797
24798 if (NILP (face_name))
24799 {
24800 font = FRAME_FONT (it->f);
24801 boff = FRAME_BASELINE_OFFSET (it->f);
24802 }
24803 else if (EQ (face_name, Qt))
24804 {
24805 override = 0;
24806 }
24807 else
24808 {
24809 int face_id;
24810 struct face *face;
24811
24812 face_id = lookup_named_face (it->f, face_name, 0);
24813 if (face_id < 0)
24814 return make_number (-1);
24815
24816 face = FACE_FROM_ID (it->f, face_id);
24817 font = face->font;
24818 if (font == NULL)
24819 return make_number (-1);
24820 boff = font->baseline_offset;
24821 if (font->vertical_centering)
24822 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24823 }
24824
24825 ascent = FONT_BASE (font) + boff;
24826 descent = FONT_DESCENT (font) - boff;
24827
24828 if (override)
24829 {
24830 it->override_ascent = ascent;
24831 it->override_descent = descent;
24832 it->override_boff = boff;
24833 }
24834
24835 height = ascent + descent;
24836
24837 scale:
24838 if (FLOATP (val))
24839 height = (int)(XFLOAT_DATA (val) * height);
24840 else if (INTEGERP (val))
24841 height *= XINT (val);
24842
24843 return make_number (height);
24844 }
24845
24846
24847 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24848 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24849 and only if this is for a character for which no font was found.
24850
24851 If the display method (it->glyphless_method) is
24852 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24853 length of the acronym or the hexadecimal string, UPPER_XOFF and
24854 UPPER_YOFF are pixel offsets for the upper part of the string,
24855 LOWER_XOFF and LOWER_YOFF are for the lower part.
24856
24857 For the other display methods, LEN through LOWER_YOFF are zero. */
24858
24859 static void
24860 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24861 short upper_xoff, short upper_yoff,
24862 short lower_xoff, short lower_yoff)
24863 {
24864 struct glyph *glyph;
24865 enum glyph_row_area area = it->area;
24866
24867 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24868 if (glyph < it->glyph_row->glyphs[area + 1])
24869 {
24870 /* If the glyph row is reversed, we need to prepend the glyph
24871 rather than append it. */
24872 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24873 {
24874 struct glyph *g;
24875
24876 /* Make room for the additional glyph. */
24877 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24878 g[1] = *g;
24879 glyph = it->glyph_row->glyphs[area];
24880 }
24881 glyph->charpos = CHARPOS (it->position);
24882 glyph->object = it->object;
24883 glyph->pixel_width = it->pixel_width;
24884 glyph->ascent = it->ascent;
24885 glyph->descent = it->descent;
24886 glyph->voffset = it->voffset;
24887 glyph->type = GLYPHLESS_GLYPH;
24888 glyph->u.glyphless.method = it->glyphless_method;
24889 glyph->u.glyphless.for_no_font = for_no_font;
24890 glyph->u.glyphless.len = len;
24891 glyph->u.glyphless.ch = it->c;
24892 glyph->slice.glyphless.upper_xoff = upper_xoff;
24893 glyph->slice.glyphless.upper_yoff = upper_yoff;
24894 glyph->slice.glyphless.lower_xoff = lower_xoff;
24895 glyph->slice.glyphless.lower_yoff = lower_yoff;
24896 glyph->avoid_cursor_p = it->avoid_cursor_p;
24897 glyph->multibyte_p = it->multibyte_p;
24898 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24899 {
24900 /* In R2L rows, the left and the right box edges need to be
24901 drawn in reverse direction. */
24902 glyph->right_box_line_p = it->start_of_box_run_p;
24903 glyph->left_box_line_p = it->end_of_box_run_p;
24904 }
24905 else
24906 {
24907 glyph->left_box_line_p = it->start_of_box_run_p;
24908 glyph->right_box_line_p = it->end_of_box_run_p;
24909 }
24910 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24911 || it->phys_descent > it->descent);
24912 glyph->padding_p = 0;
24913 glyph->glyph_not_available_p = 0;
24914 glyph->face_id = face_id;
24915 glyph->font_type = FONT_TYPE_UNKNOWN;
24916 if (it->bidi_p)
24917 {
24918 glyph->resolved_level = it->bidi_it.resolved_level;
24919 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24920 emacs_abort ();
24921 glyph->bidi_type = it->bidi_it.type;
24922 }
24923 ++it->glyph_row->used[area];
24924 }
24925 else
24926 IT_EXPAND_MATRIX_WIDTH (it, area);
24927 }
24928
24929
24930 /* Produce a glyph for a glyphless character for iterator IT.
24931 IT->glyphless_method specifies which method to use for displaying
24932 the character. See the description of enum
24933 glyphless_display_method in dispextern.h for the detail.
24934
24935 FOR_NO_FONT is nonzero if and only if this is for a character for
24936 which no font was found. ACRONYM, if non-nil, is an acronym string
24937 for the character. */
24938
24939 static void
24940 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24941 {
24942 int face_id;
24943 struct face *face;
24944 struct font *font;
24945 int base_width, base_height, width, height;
24946 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24947 int len;
24948
24949 /* Get the metrics of the base font. We always refer to the current
24950 ASCII face. */
24951 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24952 font = face->font ? face->font : FRAME_FONT (it->f);
24953 it->ascent = FONT_BASE (font) + font->baseline_offset;
24954 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24955 base_height = it->ascent + it->descent;
24956 base_width = font->average_width;
24957
24958 /* Get a face ID for the glyph by utilizing a cache (the same way as
24959 done for `escape-glyph' in get_next_display_element). */
24960 if (it->f == last_glyphless_glyph_frame
24961 && it->face_id == last_glyphless_glyph_face_id)
24962 {
24963 face_id = last_glyphless_glyph_merged_face_id;
24964 }
24965 else
24966 {
24967 /* Merge the `glyphless-char' face into the current face. */
24968 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24969 last_glyphless_glyph_frame = it->f;
24970 last_glyphless_glyph_face_id = it->face_id;
24971 last_glyphless_glyph_merged_face_id = face_id;
24972 }
24973
24974 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24975 {
24976 it->pixel_width = THIN_SPACE_WIDTH;
24977 len = 0;
24978 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24979 }
24980 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24981 {
24982 width = CHAR_WIDTH (it->c);
24983 if (width == 0)
24984 width = 1;
24985 else if (width > 4)
24986 width = 4;
24987 it->pixel_width = base_width * width;
24988 len = 0;
24989 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24990 }
24991 else
24992 {
24993 char buf[7];
24994 const char *str;
24995 unsigned int code[6];
24996 int upper_len;
24997 int ascent, descent;
24998 struct font_metrics metrics_upper, metrics_lower;
24999
25000 face = FACE_FROM_ID (it->f, face_id);
25001 font = face->font ? face->font : FRAME_FONT (it->f);
25002 PREPARE_FACE_FOR_DISPLAY (it->f, face);
25003
25004 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
25005 {
25006 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
25007 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
25008 if (CONSP (acronym))
25009 acronym = XCAR (acronym);
25010 str = STRINGP (acronym) ? SSDATA (acronym) : "";
25011 }
25012 else
25013 {
25014 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
25015 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
25016 str = buf;
25017 }
25018 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
25019 code[len] = font->driver->encode_char (font, str[len]);
25020 upper_len = (len + 1) / 2;
25021 font->driver->text_extents (font, code, upper_len,
25022 &metrics_upper);
25023 font->driver->text_extents (font, code + upper_len, len - upper_len,
25024 &metrics_lower);
25025
25026
25027
25028 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
25029 width = max (metrics_upper.width, metrics_lower.width) + 4;
25030 upper_xoff = upper_yoff = 2; /* the typical case */
25031 if (base_width >= width)
25032 {
25033 /* Align the upper to the left, the lower to the right. */
25034 it->pixel_width = base_width;
25035 lower_xoff = base_width - 2 - metrics_lower.width;
25036 }
25037 else
25038 {
25039 /* Center the shorter one. */
25040 it->pixel_width = width;
25041 if (metrics_upper.width >= metrics_lower.width)
25042 lower_xoff = (width - metrics_lower.width) / 2;
25043 else
25044 {
25045 /* FIXME: This code doesn't look right. It formerly was
25046 missing the "lower_xoff = 0;", which couldn't have
25047 been right since it left lower_xoff uninitialized. */
25048 lower_xoff = 0;
25049 upper_xoff = (width - metrics_upper.width) / 2;
25050 }
25051 }
25052
25053 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
25054 top, bottom, and between upper and lower strings. */
25055 height = (metrics_upper.ascent + metrics_upper.descent
25056 + metrics_lower.ascent + metrics_lower.descent) + 5;
25057 /* Center vertically.
25058 H:base_height, D:base_descent
25059 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
25060
25061 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
25062 descent = D - H/2 + h/2;
25063 lower_yoff = descent - 2 - ld;
25064 upper_yoff = lower_yoff - la - 1 - ud; */
25065 ascent = - (it->descent - (base_height + height + 1) / 2);
25066 descent = it->descent - (base_height - height) / 2;
25067 lower_yoff = descent - 2 - metrics_lower.descent;
25068 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
25069 - metrics_upper.descent);
25070 /* Don't make the height shorter than the base height. */
25071 if (height > base_height)
25072 {
25073 it->ascent = ascent;
25074 it->descent = descent;
25075 }
25076 }
25077
25078 it->phys_ascent = it->ascent;
25079 it->phys_descent = it->descent;
25080 if (it->glyph_row)
25081 append_glyphless_glyph (it, face_id, for_no_font, len,
25082 upper_xoff, upper_yoff,
25083 lower_xoff, lower_yoff);
25084 it->nglyphs = 1;
25085 take_vertical_position_into_account (it);
25086 }
25087
25088
25089 /* RIF:
25090 Produce glyphs/get display metrics for the display element IT is
25091 loaded with. See the description of struct it in dispextern.h
25092 for an overview of struct it. */
25093
25094 void
25095 x_produce_glyphs (struct it *it)
25096 {
25097 int extra_line_spacing = it->extra_line_spacing;
25098
25099 it->glyph_not_available_p = 0;
25100
25101 if (it->what == IT_CHARACTER)
25102 {
25103 XChar2b char2b;
25104 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25105 struct font *font = face->font;
25106 struct font_metrics *pcm = NULL;
25107 int boff; /* baseline offset */
25108
25109 if (font == NULL)
25110 {
25111 /* When no suitable font is found, display this character by
25112 the method specified in the first extra slot of
25113 Vglyphless_char_display. */
25114 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
25115
25116 eassert (it->what == IT_GLYPHLESS);
25117 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
25118 goto done;
25119 }
25120
25121 boff = font->baseline_offset;
25122 if (font->vertical_centering)
25123 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25124
25125 if (it->char_to_display != '\n' && it->char_to_display != '\t')
25126 {
25127 int stretched_p;
25128
25129 it->nglyphs = 1;
25130
25131 if (it->override_ascent >= 0)
25132 {
25133 it->ascent = it->override_ascent;
25134 it->descent = it->override_descent;
25135 boff = it->override_boff;
25136 }
25137 else
25138 {
25139 it->ascent = FONT_BASE (font) + boff;
25140 it->descent = FONT_DESCENT (font) - boff;
25141 }
25142
25143 if (get_char_glyph_code (it->char_to_display, font, &char2b))
25144 {
25145 pcm = get_per_char_metric (font, &char2b);
25146 if (pcm->width == 0
25147 && pcm->rbearing == 0 && pcm->lbearing == 0)
25148 pcm = NULL;
25149 }
25150
25151 if (pcm)
25152 {
25153 it->phys_ascent = pcm->ascent + boff;
25154 it->phys_descent = pcm->descent - boff;
25155 it->pixel_width = pcm->width;
25156 }
25157 else
25158 {
25159 it->glyph_not_available_p = 1;
25160 it->phys_ascent = it->ascent;
25161 it->phys_descent = it->descent;
25162 it->pixel_width = font->space_width;
25163 }
25164
25165 if (it->constrain_row_ascent_descent_p)
25166 {
25167 if (it->descent > it->max_descent)
25168 {
25169 it->ascent += it->descent - it->max_descent;
25170 it->descent = it->max_descent;
25171 }
25172 if (it->ascent > it->max_ascent)
25173 {
25174 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25175 it->ascent = it->max_ascent;
25176 }
25177 it->phys_ascent = min (it->phys_ascent, it->ascent);
25178 it->phys_descent = min (it->phys_descent, it->descent);
25179 extra_line_spacing = 0;
25180 }
25181
25182 /* If this is a space inside a region of text with
25183 `space-width' property, change its width. */
25184 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25185 if (stretched_p)
25186 it->pixel_width *= XFLOATINT (it->space_width);
25187
25188 /* If face has a box, add the box thickness to the character
25189 height. If character has a box line to the left and/or
25190 right, add the box line width to the character's width. */
25191 if (face->box != FACE_NO_BOX)
25192 {
25193 int thick = face->box_line_width;
25194
25195 if (thick > 0)
25196 {
25197 it->ascent += thick;
25198 it->descent += thick;
25199 }
25200 else
25201 thick = -thick;
25202
25203 if (it->start_of_box_run_p)
25204 it->pixel_width += thick;
25205 if (it->end_of_box_run_p)
25206 it->pixel_width += thick;
25207 }
25208
25209 /* If face has an overline, add the height of the overline
25210 (1 pixel) and a 1 pixel margin to the character height. */
25211 if (face->overline_p)
25212 it->ascent += overline_margin;
25213
25214 if (it->constrain_row_ascent_descent_p)
25215 {
25216 if (it->ascent > it->max_ascent)
25217 it->ascent = it->max_ascent;
25218 if (it->descent > it->max_descent)
25219 it->descent = it->max_descent;
25220 }
25221
25222 take_vertical_position_into_account (it);
25223
25224 /* If we have to actually produce glyphs, do it. */
25225 if (it->glyph_row)
25226 {
25227 if (stretched_p)
25228 {
25229 /* Translate a space with a `space-width' property
25230 into a stretch glyph. */
25231 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25232 / FONT_HEIGHT (font));
25233 append_stretch_glyph (it, it->object, it->pixel_width,
25234 it->ascent + it->descent, ascent);
25235 }
25236 else
25237 append_glyph (it);
25238
25239 /* If characters with lbearing or rbearing are displayed
25240 in this line, record that fact in a flag of the
25241 glyph row. This is used to optimize X output code. */
25242 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25243 it->glyph_row->contains_overlapping_glyphs_p = 1;
25244 }
25245 if (! stretched_p && it->pixel_width == 0)
25246 /* We assure that all visible glyphs have at least 1-pixel
25247 width. */
25248 it->pixel_width = 1;
25249 }
25250 else if (it->char_to_display == '\n')
25251 {
25252 /* A newline has no width, but we need the height of the
25253 line. But if previous part of the line sets a height,
25254 don't increase that height */
25255
25256 Lisp_Object height;
25257 Lisp_Object total_height = Qnil;
25258
25259 it->override_ascent = -1;
25260 it->pixel_width = 0;
25261 it->nglyphs = 0;
25262
25263 height = get_it_property (it, Qline_height);
25264 /* Split (line-height total-height) list */
25265 if (CONSP (height)
25266 && CONSP (XCDR (height))
25267 && NILP (XCDR (XCDR (height))))
25268 {
25269 total_height = XCAR (XCDR (height));
25270 height = XCAR (height);
25271 }
25272 height = calc_line_height_property (it, height, font, boff, 1);
25273
25274 if (it->override_ascent >= 0)
25275 {
25276 it->ascent = it->override_ascent;
25277 it->descent = it->override_descent;
25278 boff = it->override_boff;
25279 }
25280 else
25281 {
25282 it->ascent = FONT_BASE (font) + boff;
25283 it->descent = FONT_DESCENT (font) - boff;
25284 }
25285
25286 if (EQ (height, Qt))
25287 {
25288 if (it->descent > it->max_descent)
25289 {
25290 it->ascent += it->descent - it->max_descent;
25291 it->descent = it->max_descent;
25292 }
25293 if (it->ascent > it->max_ascent)
25294 {
25295 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25296 it->ascent = it->max_ascent;
25297 }
25298 it->phys_ascent = min (it->phys_ascent, it->ascent);
25299 it->phys_descent = min (it->phys_descent, it->descent);
25300 it->constrain_row_ascent_descent_p = 1;
25301 extra_line_spacing = 0;
25302 }
25303 else
25304 {
25305 Lisp_Object spacing;
25306
25307 it->phys_ascent = it->ascent;
25308 it->phys_descent = it->descent;
25309
25310 if ((it->max_ascent > 0 || it->max_descent > 0)
25311 && face->box != FACE_NO_BOX
25312 && face->box_line_width > 0)
25313 {
25314 it->ascent += face->box_line_width;
25315 it->descent += face->box_line_width;
25316 }
25317 if (!NILP (height)
25318 && XINT (height) > it->ascent + it->descent)
25319 it->ascent = XINT (height) - it->descent;
25320
25321 if (!NILP (total_height))
25322 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25323 else
25324 {
25325 spacing = get_it_property (it, Qline_spacing);
25326 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25327 }
25328 if (INTEGERP (spacing))
25329 {
25330 extra_line_spacing = XINT (spacing);
25331 if (!NILP (total_height))
25332 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25333 }
25334 }
25335 }
25336 else /* i.e. (it->char_to_display == '\t') */
25337 {
25338 if (font->space_width > 0)
25339 {
25340 int tab_width = it->tab_width * font->space_width;
25341 int x = it->current_x + it->continuation_lines_width;
25342 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25343
25344 /* If the distance from the current position to the next tab
25345 stop is less than a space character width, use the
25346 tab stop after that. */
25347 if (next_tab_x - x < font->space_width)
25348 next_tab_x += tab_width;
25349
25350 it->pixel_width = next_tab_x - x;
25351 it->nglyphs = 1;
25352 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25353 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25354
25355 if (it->glyph_row)
25356 {
25357 append_stretch_glyph (it, it->object, it->pixel_width,
25358 it->ascent + it->descent, it->ascent);
25359 }
25360 }
25361 else
25362 {
25363 it->pixel_width = 0;
25364 it->nglyphs = 1;
25365 }
25366 }
25367 }
25368 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25369 {
25370 /* A static composition.
25371
25372 Note: A composition is represented as one glyph in the
25373 glyph matrix. There are no padding glyphs.
25374
25375 Important note: pixel_width, ascent, and descent are the
25376 values of what is drawn by draw_glyphs (i.e. the values of
25377 the overall glyphs composed). */
25378 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25379 int boff; /* baseline offset */
25380 struct composition *cmp = composition_table[it->cmp_it.id];
25381 int glyph_len = cmp->glyph_len;
25382 struct font *font = face->font;
25383
25384 it->nglyphs = 1;
25385
25386 /* If we have not yet calculated pixel size data of glyphs of
25387 the composition for the current face font, calculate them
25388 now. Theoretically, we have to check all fonts for the
25389 glyphs, but that requires much time and memory space. So,
25390 here we check only the font of the first glyph. This may
25391 lead to incorrect display, but it's very rare, and C-l
25392 (recenter-top-bottom) can correct the display anyway. */
25393 if (! cmp->font || cmp->font != font)
25394 {
25395 /* Ascent and descent of the font of the first character
25396 of this composition (adjusted by baseline offset).
25397 Ascent and descent of overall glyphs should not be less
25398 than these, respectively. */
25399 int font_ascent, font_descent, font_height;
25400 /* Bounding box of the overall glyphs. */
25401 int leftmost, rightmost, lowest, highest;
25402 int lbearing, rbearing;
25403 int i, width, ascent, descent;
25404 int left_padded = 0, right_padded = 0;
25405 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25406 XChar2b char2b;
25407 struct font_metrics *pcm;
25408 int font_not_found_p;
25409 ptrdiff_t pos;
25410
25411 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25412 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25413 break;
25414 if (glyph_len < cmp->glyph_len)
25415 right_padded = 1;
25416 for (i = 0; i < glyph_len; i++)
25417 {
25418 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25419 break;
25420 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25421 }
25422 if (i > 0)
25423 left_padded = 1;
25424
25425 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25426 : IT_CHARPOS (*it));
25427 /* If no suitable font is found, use the default font. */
25428 font_not_found_p = font == NULL;
25429 if (font_not_found_p)
25430 {
25431 face = face->ascii_face;
25432 font = face->font;
25433 }
25434 boff = font->baseline_offset;
25435 if (font->vertical_centering)
25436 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25437 font_ascent = FONT_BASE (font) + boff;
25438 font_descent = FONT_DESCENT (font) - boff;
25439 font_height = FONT_HEIGHT (font);
25440
25441 cmp->font = font;
25442
25443 pcm = NULL;
25444 if (! font_not_found_p)
25445 {
25446 get_char_face_and_encoding (it->f, c, it->face_id,
25447 &char2b, 0);
25448 pcm = get_per_char_metric (font, &char2b);
25449 }
25450
25451 /* Initialize the bounding box. */
25452 if (pcm)
25453 {
25454 width = cmp->glyph_len > 0 ? pcm->width : 0;
25455 ascent = pcm->ascent;
25456 descent = pcm->descent;
25457 lbearing = pcm->lbearing;
25458 rbearing = pcm->rbearing;
25459 }
25460 else
25461 {
25462 width = cmp->glyph_len > 0 ? font->space_width : 0;
25463 ascent = FONT_BASE (font);
25464 descent = FONT_DESCENT (font);
25465 lbearing = 0;
25466 rbearing = width;
25467 }
25468
25469 rightmost = width;
25470 leftmost = 0;
25471 lowest = - descent + boff;
25472 highest = ascent + boff;
25473
25474 if (! font_not_found_p
25475 && font->default_ascent
25476 && CHAR_TABLE_P (Vuse_default_ascent)
25477 && !NILP (Faref (Vuse_default_ascent,
25478 make_number (it->char_to_display))))
25479 highest = font->default_ascent + boff;
25480
25481 /* Draw the first glyph at the normal position. It may be
25482 shifted to right later if some other glyphs are drawn
25483 at the left. */
25484 cmp->offsets[i * 2] = 0;
25485 cmp->offsets[i * 2 + 1] = boff;
25486 cmp->lbearing = lbearing;
25487 cmp->rbearing = rbearing;
25488
25489 /* Set cmp->offsets for the remaining glyphs. */
25490 for (i++; i < glyph_len; i++)
25491 {
25492 int left, right, btm, top;
25493 int ch = COMPOSITION_GLYPH (cmp, i);
25494 int face_id;
25495 struct face *this_face;
25496
25497 if (ch == '\t')
25498 ch = ' ';
25499 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25500 this_face = FACE_FROM_ID (it->f, face_id);
25501 font = this_face->font;
25502
25503 if (font == NULL)
25504 pcm = NULL;
25505 else
25506 {
25507 get_char_face_and_encoding (it->f, ch, face_id,
25508 &char2b, 0);
25509 pcm = get_per_char_metric (font, &char2b);
25510 }
25511 if (! pcm)
25512 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25513 else
25514 {
25515 width = pcm->width;
25516 ascent = pcm->ascent;
25517 descent = pcm->descent;
25518 lbearing = pcm->lbearing;
25519 rbearing = pcm->rbearing;
25520 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25521 {
25522 /* Relative composition with or without
25523 alternate chars. */
25524 left = (leftmost + rightmost - width) / 2;
25525 btm = - descent + boff;
25526 if (font->relative_compose
25527 && (! CHAR_TABLE_P (Vignore_relative_composition)
25528 || NILP (Faref (Vignore_relative_composition,
25529 make_number (ch)))))
25530 {
25531
25532 if (- descent >= font->relative_compose)
25533 /* One extra pixel between two glyphs. */
25534 btm = highest + 1;
25535 else if (ascent <= 0)
25536 /* One extra pixel between two glyphs. */
25537 btm = lowest - 1 - ascent - descent;
25538 }
25539 }
25540 else
25541 {
25542 /* A composition rule is specified by an integer
25543 value that encodes global and new reference
25544 points (GREF and NREF). GREF and NREF are
25545 specified by numbers as below:
25546
25547 0---1---2 -- ascent
25548 | |
25549 | |
25550 | |
25551 9--10--11 -- center
25552 | |
25553 ---3---4---5--- baseline
25554 | |
25555 6---7---8 -- descent
25556 */
25557 int rule = COMPOSITION_RULE (cmp, i);
25558 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25559
25560 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25561 grefx = gref % 3, nrefx = nref % 3;
25562 grefy = gref / 3, nrefy = nref / 3;
25563 if (xoff)
25564 xoff = font_height * (xoff - 128) / 256;
25565 if (yoff)
25566 yoff = font_height * (yoff - 128) / 256;
25567
25568 left = (leftmost
25569 + grefx * (rightmost - leftmost) / 2
25570 - nrefx * width / 2
25571 + xoff);
25572
25573 btm = ((grefy == 0 ? highest
25574 : grefy == 1 ? 0
25575 : grefy == 2 ? lowest
25576 : (highest + lowest) / 2)
25577 - (nrefy == 0 ? ascent + descent
25578 : nrefy == 1 ? descent - boff
25579 : nrefy == 2 ? 0
25580 : (ascent + descent) / 2)
25581 + yoff);
25582 }
25583
25584 cmp->offsets[i * 2] = left;
25585 cmp->offsets[i * 2 + 1] = btm + descent;
25586
25587 /* Update the bounding box of the overall glyphs. */
25588 if (width > 0)
25589 {
25590 right = left + width;
25591 if (left < leftmost)
25592 leftmost = left;
25593 if (right > rightmost)
25594 rightmost = right;
25595 }
25596 top = btm + descent + ascent;
25597 if (top > highest)
25598 highest = top;
25599 if (btm < lowest)
25600 lowest = btm;
25601
25602 if (cmp->lbearing > left + lbearing)
25603 cmp->lbearing = left + lbearing;
25604 if (cmp->rbearing < left + rbearing)
25605 cmp->rbearing = left + rbearing;
25606 }
25607 }
25608
25609 /* If there are glyphs whose x-offsets are negative,
25610 shift all glyphs to the right and make all x-offsets
25611 non-negative. */
25612 if (leftmost < 0)
25613 {
25614 for (i = 0; i < cmp->glyph_len; i++)
25615 cmp->offsets[i * 2] -= leftmost;
25616 rightmost -= leftmost;
25617 cmp->lbearing -= leftmost;
25618 cmp->rbearing -= leftmost;
25619 }
25620
25621 if (left_padded && cmp->lbearing < 0)
25622 {
25623 for (i = 0; i < cmp->glyph_len; i++)
25624 cmp->offsets[i * 2] -= cmp->lbearing;
25625 rightmost -= cmp->lbearing;
25626 cmp->rbearing -= cmp->lbearing;
25627 cmp->lbearing = 0;
25628 }
25629 if (right_padded && rightmost < cmp->rbearing)
25630 {
25631 rightmost = cmp->rbearing;
25632 }
25633
25634 cmp->pixel_width = rightmost;
25635 cmp->ascent = highest;
25636 cmp->descent = - lowest;
25637 if (cmp->ascent < font_ascent)
25638 cmp->ascent = font_ascent;
25639 if (cmp->descent < font_descent)
25640 cmp->descent = font_descent;
25641 }
25642
25643 if (it->glyph_row
25644 && (cmp->lbearing < 0
25645 || cmp->rbearing > cmp->pixel_width))
25646 it->glyph_row->contains_overlapping_glyphs_p = 1;
25647
25648 it->pixel_width = cmp->pixel_width;
25649 it->ascent = it->phys_ascent = cmp->ascent;
25650 it->descent = it->phys_descent = cmp->descent;
25651 if (face->box != FACE_NO_BOX)
25652 {
25653 int thick = face->box_line_width;
25654
25655 if (thick > 0)
25656 {
25657 it->ascent += thick;
25658 it->descent += thick;
25659 }
25660 else
25661 thick = - thick;
25662
25663 if (it->start_of_box_run_p)
25664 it->pixel_width += thick;
25665 if (it->end_of_box_run_p)
25666 it->pixel_width += thick;
25667 }
25668
25669 /* If face has an overline, add the height of the overline
25670 (1 pixel) and a 1 pixel margin to the character height. */
25671 if (face->overline_p)
25672 it->ascent += overline_margin;
25673
25674 take_vertical_position_into_account (it);
25675 if (it->ascent < 0)
25676 it->ascent = 0;
25677 if (it->descent < 0)
25678 it->descent = 0;
25679
25680 if (it->glyph_row && cmp->glyph_len > 0)
25681 append_composite_glyph (it);
25682 }
25683 else if (it->what == IT_COMPOSITION)
25684 {
25685 /* A dynamic (automatic) composition. */
25686 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25687 Lisp_Object gstring;
25688 struct font_metrics metrics;
25689
25690 it->nglyphs = 1;
25691
25692 gstring = composition_gstring_from_id (it->cmp_it.id);
25693 it->pixel_width
25694 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25695 &metrics);
25696 if (it->glyph_row
25697 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25698 it->glyph_row->contains_overlapping_glyphs_p = 1;
25699 it->ascent = it->phys_ascent = metrics.ascent;
25700 it->descent = it->phys_descent = metrics.descent;
25701 if (face->box != FACE_NO_BOX)
25702 {
25703 int thick = face->box_line_width;
25704
25705 if (thick > 0)
25706 {
25707 it->ascent += thick;
25708 it->descent += thick;
25709 }
25710 else
25711 thick = - thick;
25712
25713 if (it->start_of_box_run_p)
25714 it->pixel_width += thick;
25715 if (it->end_of_box_run_p)
25716 it->pixel_width += thick;
25717 }
25718 /* If face has an overline, add the height of the overline
25719 (1 pixel) and a 1 pixel margin to the character height. */
25720 if (face->overline_p)
25721 it->ascent += overline_margin;
25722 take_vertical_position_into_account (it);
25723 if (it->ascent < 0)
25724 it->ascent = 0;
25725 if (it->descent < 0)
25726 it->descent = 0;
25727
25728 if (it->glyph_row)
25729 append_composite_glyph (it);
25730 }
25731 else if (it->what == IT_GLYPHLESS)
25732 produce_glyphless_glyph (it, 0, Qnil);
25733 else if (it->what == IT_IMAGE)
25734 produce_image_glyph (it);
25735 else if (it->what == IT_STRETCH)
25736 produce_stretch_glyph (it);
25737
25738 done:
25739 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25740 because this isn't true for images with `:ascent 100'. */
25741 eassert (it->ascent >= 0 && it->descent >= 0);
25742 if (it->area == TEXT_AREA)
25743 it->current_x += it->pixel_width;
25744
25745 if (extra_line_spacing > 0)
25746 {
25747 it->descent += extra_line_spacing;
25748 if (extra_line_spacing > it->max_extra_line_spacing)
25749 it->max_extra_line_spacing = extra_line_spacing;
25750 }
25751
25752 it->max_ascent = max (it->max_ascent, it->ascent);
25753 it->max_descent = max (it->max_descent, it->descent);
25754 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25755 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25756 }
25757
25758 /* EXPORT for RIF:
25759 Output LEN glyphs starting at START at the nominal cursor position.
25760 Advance the nominal cursor over the text. The global variable
25761 updated_window contains the window being updated, updated_row is
25762 the glyph row being updated, and updated_area is the area of that
25763 row being updated. */
25764
25765 void
25766 x_write_glyphs (struct glyph *start, int len)
25767 {
25768 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25769
25770 eassert (updated_window && updated_row);
25771 /* When the window is hscrolled, cursor hpos can legitimately be out
25772 of bounds, but we draw the cursor at the corresponding window
25773 margin in that case. */
25774 if (!updated_row->reversed_p && chpos < 0)
25775 chpos = 0;
25776 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25777 chpos = updated_row->used[TEXT_AREA] - 1;
25778
25779 block_input ();
25780
25781 /* Write glyphs. */
25782
25783 hpos = start - updated_row->glyphs[updated_area];
25784 x = draw_glyphs (updated_window, output_cursor.x,
25785 updated_row, updated_area,
25786 hpos, hpos + len,
25787 DRAW_NORMAL_TEXT, 0);
25788
25789 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25790 if (updated_area == TEXT_AREA
25791 && updated_window->phys_cursor_on_p
25792 && updated_window->phys_cursor.vpos == output_cursor.vpos
25793 && chpos >= hpos
25794 && chpos < hpos + len)
25795 updated_window->phys_cursor_on_p = 0;
25796
25797 unblock_input ();
25798
25799 /* Advance the output cursor. */
25800 output_cursor.hpos += len;
25801 output_cursor.x = x;
25802 }
25803
25804
25805 /* EXPORT for RIF:
25806 Insert LEN glyphs from START at the nominal cursor position. */
25807
25808 void
25809 x_insert_glyphs (struct glyph *start, int len)
25810 {
25811 struct frame *f;
25812 struct window *w;
25813 int line_height, shift_by_width, shifted_region_width;
25814 struct glyph_row *row;
25815 struct glyph *glyph;
25816 int frame_x, frame_y;
25817 ptrdiff_t hpos;
25818
25819 eassert (updated_window && updated_row);
25820 block_input ();
25821 w = updated_window;
25822 f = XFRAME (WINDOW_FRAME (w));
25823
25824 /* Get the height of the line we are in. */
25825 row = updated_row;
25826 line_height = row->height;
25827
25828 /* Get the width of the glyphs to insert. */
25829 shift_by_width = 0;
25830 for (glyph = start; glyph < start + len; ++glyph)
25831 shift_by_width += glyph->pixel_width;
25832
25833 /* Get the width of the region to shift right. */
25834 shifted_region_width = (window_box_width (w, updated_area)
25835 - output_cursor.x
25836 - shift_by_width);
25837
25838 /* Shift right. */
25839 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25840 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25841
25842 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25843 line_height, shift_by_width);
25844
25845 /* Write the glyphs. */
25846 hpos = start - row->glyphs[updated_area];
25847 draw_glyphs (w, output_cursor.x, row, updated_area,
25848 hpos, hpos + len,
25849 DRAW_NORMAL_TEXT, 0);
25850
25851 /* Advance the output cursor. */
25852 output_cursor.hpos += len;
25853 output_cursor.x += shift_by_width;
25854 unblock_input ();
25855 }
25856
25857
25858 /* EXPORT for RIF:
25859 Erase the current text line from the nominal cursor position
25860 (inclusive) to pixel column TO_X (exclusive). The idea is that
25861 everything from TO_X onward is already erased.
25862
25863 TO_X is a pixel position relative to updated_area of
25864 updated_window. TO_X == -1 means clear to the end of this area. */
25865
25866 void
25867 x_clear_end_of_line (int to_x)
25868 {
25869 struct frame *f;
25870 struct window *w = updated_window;
25871 int max_x, min_y, max_y;
25872 int from_x, from_y, to_y;
25873
25874 eassert (updated_window && updated_row);
25875 f = XFRAME (w->frame);
25876
25877 if (updated_row->full_width_p)
25878 max_x = WINDOW_TOTAL_WIDTH (w);
25879 else
25880 max_x = window_box_width (w, updated_area);
25881 max_y = window_text_bottom_y (w);
25882
25883 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25884 of window. For TO_X > 0, truncate to end of drawing area. */
25885 if (to_x == 0)
25886 return;
25887 else if (to_x < 0)
25888 to_x = max_x;
25889 else
25890 to_x = min (to_x, max_x);
25891
25892 to_y = min (max_y, output_cursor.y + updated_row->height);
25893
25894 /* Notice if the cursor will be cleared by this operation. */
25895 if (!updated_row->full_width_p)
25896 notice_overwritten_cursor (w, updated_area,
25897 output_cursor.x, -1,
25898 updated_row->y,
25899 MATRIX_ROW_BOTTOM_Y (updated_row));
25900
25901 from_x = output_cursor.x;
25902
25903 /* Translate to frame coordinates. */
25904 if (updated_row->full_width_p)
25905 {
25906 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25907 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25908 }
25909 else
25910 {
25911 int area_left = window_box_left (w, updated_area);
25912 from_x += area_left;
25913 to_x += area_left;
25914 }
25915
25916 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25917 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25918 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25919
25920 /* Prevent inadvertently clearing to end of the X window. */
25921 if (to_x > from_x && to_y > from_y)
25922 {
25923 block_input ();
25924 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25925 to_x - from_x, to_y - from_y);
25926 unblock_input ();
25927 }
25928 }
25929
25930 #endif /* HAVE_WINDOW_SYSTEM */
25931
25932
25933 \f
25934 /***********************************************************************
25935 Cursor types
25936 ***********************************************************************/
25937
25938 /* Value is the internal representation of the specified cursor type
25939 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25940 of the bar cursor. */
25941
25942 static enum text_cursor_kinds
25943 get_specified_cursor_type (Lisp_Object arg, int *width)
25944 {
25945 enum text_cursor_kinds type;
25946
25947 if (NILP (arg))
25948 return NO_CURSOR;
25949
25950 if (EQ (arg, Qbox))
25951 return FILLED_BOX_CURSOR;
25952
25953 if (EQ (arg, Qhollow))
25954 return HOLLOW_BOX_CURSOR;
25955
25956 if (EQ (arg, Qbar))
25957 {
25958 *width = 2;
25959 return BAR_CURSOR;
25960 }
25961
25962 if (CONSP (arg)
25963 && EQ (XCAR (arg), Qbar)
25964 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25965 {
25966 *width = XINT (XCDR (arg));
25967 return BAR_CURSOR;
25968 }
25969
25970 if (EQ (arg, Qhbar))
25971 {
25972 *width = 2;
25973 return HBAR_CURSOR;
25974 }
25975
25976 if (CONSP (arg)
25977 && EQ (XCAR (arg), Qhbar)
25978 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25979 {
25980 *width = XINT (XCDR (arg));
25981 return HBAR_CURSOR;
25982 }
25983
25984 /* Treat anything unknown as "hollow box cursor".
25985 It was bad to signal an error; people have trouble fixing
25986 .Xdefaults with Emacs, when it has something bad in it. */
25987 type = HOLLOW_BOX_CURSOR;
25988
25989 return type;
25990 }
25991
25992 /* Set the default cursor types for specified frame. */
25993 void
25994 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25995 {
25996 int width = 1;
25997 Lisp_Object tem;
25998
25999 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
26000 FRAME_CURSOR_WIDTH (f) = width;
26001
26002 /* By default, set up the blink-off state depending on the on-state. */
26003
26004 tem = Fassoc (arg, Vblink_cursor_alist);
26005 if (!NILP (tem))
26006 {
26007 FRAME_BLINK_OFF_CURSOR (f)
26008 = get_specified_cursor_type (XCDR (tem), &width);
26009 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
26010 }
26011 else
26012 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
26013 }
26014
26015
26016 #ifdef HAVE_WINDOW_SYSTEM
26017
26018 /* Return the cursor we want to be displayed in window W. Return
26019 width of bar/hbar cursor through WIDTH arg. Return with
26020 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
26021 (i.e. if the `system caret' should track this cursor).
26022
26023 In a mini-buffer window, we want the cursor only to appear if we
26024 are reading input from this window. For the selected window, we
26025 want the cursor type given by the frame parameter or buffer local
26026 setting of cursor-type. If explicitly marked off, draw no cursor.
26027 In all other cases, we want a hollow box cursor. */
26028
26029 static enum text_cursor_kinds
26030 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
26031 int *active_cursor)
26032 {
26033 struct frame *f = XFRAME (w->frame);
26034 struct buffer *b = XBUFFER (w->contents);
26035 int cursor_type = DEFAULT_CURSOR;
26036 Lisp_Object alt_cursor;
26037 int non_selected = 0;
26038
26039 *active_cursor = 1;
26040
26041 /* Echo area */
26042 if (cursor_in_echo_area
26043 && FRAME_HAS_MINIBUF_P (f)
26044 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
26045 {
26046 if (w == XWINDOW (echo_area_window))
26047 {
26048 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
26049 {
26050 *width = FRAME_CURSOR_WIDTH (f);
26051 return FRAME_DESIRED_CURSOR (f);
26052 }
26053 else
26054 return get_specified_cursor_type (BVAR (b, cursor_type), width);
26055 }
26056
26057 *active_cursor = 0;
26058 non_selected = 1;
26059 }
26060
26061 /* Detect a nonselected window or nonselected frame. */
26062 else if (w != XWINDOW (f->selected_window)
26063 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
26064 {
26065 *active_cursor = 0;
26066
26067 if (MINI_WINDOW_P (w) && minibuf_level == 0)
26068 return NO_CURSOR;
26069
26070 non_selected = 1;
26071 }
26072
26073 /* Never display a cursor in a window in which cursor-type is nil. */
26074 if (NILP (BVAR (b, cursor_type)))
26075 return NO_CURSOR;
26076
26077 /* Get the normal cursor type for this window. */
26078 if (EQ (BVAR (b, cursor_type), Qt))
26079 {
26080 cursor_type = FRAME_DESIRED_CURSOR (f);
26081 *width = FRAME_CURSOR_WIDTH (f);
26082 }
26083 else
26084 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
26085
26086 /* Use cursor-in-non-selected-windows instead
26087 for non-selected window or frame. */
26088 if (non_selected)
26089 {
26090 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
26091 if (!EQ (Qt, alt_cursor))
26092 return get_specified_cursor_type (alt_cursor, width);
26093 /* t means modify the normal cursor type. */
26094 if (cursor_type == FILLED_BOX_CURSOR)
26095 cursor_type = HOLLOW_BOX_CURSOR;
26096 else if (cursor_type == BAR_CURSOR && *width > 1)
26097 --*width;
26098 return cursor_type;
26099 }
26100
26101 /* Use normal cursor if not blinked off. */
26102 if (!w->cursor_off_p)
26103 {
26104 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
26105 {
26106 if (cursor_type == FILLED_BOX_CURSOR)
26107 {
26108 /* Using a block cursor on large images can be very annoying.
26109 So use a hollow cursor for "large" images.
26110 If image is not transparent (no mask), also use hollow cursor. */
26111 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
26112 if (img != NULL && IMAGEP (img->spec))
26113 {
26114 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
26115 where N = size of default frame font size.
26116 This should cover most of the "tiny" icons people may use. */
26117 if (!img->mask
26118 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
26119 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
26120 cursor_type = HOLLOW_BOX_CURSOR;
26121 }
26122 }
26123 else if (cursor_type != NO_CURSOR)
26124 {
26125 /* Display current only supports BOX and HOLLOW cursors for images.
26126 So for now, unconditionally use a HOLLOW cursor when cursor is
26127 not a solid box cursor. */
26128 cursor_type = HOLLOW_BOX_CURSOR;
26129 }
26130 }
26131 return cursor_type;
26132 }
26133
26134 /* Cursor is blinked off, so determine how to "toggle" it. */
26135
26136 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
26137 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
26138 return get_specified_cursor_type (XCDR (alt_cursor), width);
26139
26140 /* Then see if frame has specified a specific blink off cursor type. */
26141 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
26142 {
26143 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
26144 return FRAME_BLINK_OFF_CURSOR (f);
26145 }
26146
26147 #if 0
26148 /* Some people liked having a permanently visible blinking cursor,
26149 while others had very strong opinions against it. So it was
26150 decided to remove it. KFS 2003-09-03 */
26151
26152 /* Finally perform built-in cursor blinking:
26153 filled box <-> hollow box
26154 wide [h]bar <-> narrow [h]bar
26155 narrow [h]bar <-> no cursor
26156 other type <-> no cursor */
26157
26158 if (cursor_type == FILLED_BOX_CURSOR)
26159 return HOLLOW_BOX_CURSOR;
26160
26161 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26162 {
26163 *width = 1;
26164 return cursor_type;
26165 }
26166 #endif
26167
26168 return NO_CURSOR;
26169 }
26170
26171
26172 /* Notice when the text cursor of window W has been completely
26173 overwritten by a drawing operation that outputs glyphs in AREA
26174 starting at X0 and ending at X1 in the line starting at Y0 and
26175 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26176 the rest of the line after X0 has been written. Y coordinates
26177 are window-relative. */
26178
26179 static void
26180 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26181 int x0, int x1, int y0, int y1)
26182 {
26183 int cx0, cx1, cy0, cy1;
26184 struct glyph_row *row;
26185
26186 if (!w->phys_cursor_on_p)
26187 return;
26188 if (area != TEXT_AREA)
26189 return;
26190
26191 if (w->phys_cursor.vpos < 0
26192 || w->phys_cursor.vpos >= w->current_matrix->nrows
26193 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26194 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
26195 return;
26196
26197 if (row->cursor_in_fringe_p)
26198 {
26199 row->cursor_in_fringe_p = 0;
26200 draw_fringe_bitmap (w, row, row->reversed_p);
26201 w->phys_cursor_on_p = 0;
26202 return;
26203 }
26204
26205 cx0 = w->phys_cursor.x;
26206 cx1 = cx0 + w->phys_cursor_width;
26207 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26208 return;
26209
26210 /* The cursor image will be completely removed from the
26211 screen if the output area intersects the cursor area in
26212 y-direction. When we draw in [y0 y1[, and some part of
26213 the cursor is at y < y0, that part must have been drawn
26214 before. When scrolling, the cursor is erased before
26215 actually scrolling, so we don't come here. When not
26216 scrolling, the rows above the old cursor row must have
26217 changed, and in this case these rows must have written
26218 over the cursor image.
26219
26220 Likewise if part of the cursor is below y1, with the
26221 exception of the cursor being in the first blank row at
26222 the buffer and window end because update_text_area
26223 doesn't draw that row. (Except when it does, but
26224 that's handled in update_text_area.) */
26225
26226 cy0 = w->phys_cursor.y;
26227 cy1 = cy0 + w->phys_cursor_height;
26228 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26229 return;
26230
26231 w->phys_cursor_on_p = 0;
26232 }
26233
26234 #endif /* HAVE_WINDOW_SYSTEM */
26235
26236 \f
26237 /************************************************************************
26238 Mouse Face
26239 ************************************************************************/
26240
26241 #ifdef HAVE_WINDOW_SYSTEM
26242
26243 /* EXPORT for RIF:
26244 Fix the display of area AREA of overlapping row ROW in window W
26245 with respect to the overlapping part OVERLAPS. */
26246
26247 void
26248 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26249 enum glyph_row_area area, int overlaps)
26250 {
26251 int i, x;
26252
26253 block_input ();
26254
26255 x = 0;
26256 for (i = 0; i < row->used[area];)
26257 {
26258 if (row->glyphs[area][i].overlaps_vertically_p)
26259 {
26260 int start = i, start_x = x;
26261
26262 do
26263 {
26264 x += row->glyphs[area][i].pixel_width;
26265 ++i;
26266 }
26267 while (i < row->used[area]
26268 && row->glyphs[area][i].overlaps_vertically_p);
26269
26270 draw_glyphs (w, start_x, row, area,
26271 start, i,
26272 DRAW_NORMAL_TEXT, overlaps);
26273 }
26274 else
26275 {
26276 x += row->glyphs[area][i].pixel_width;
26277 ++i;
26278 }
26279 }
26280
26281 unblock_input ();
26282 }
26283
26284
26285 /* EXPORT:
26286 Draw the cursor glyph of window W in glyph row ROW. See the
26287 comment of draw_glyphs for the meaning of HL. */
26288
26289 void
26290 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26291 enum draw_glyphs_face hl)
26292 {
26293 /* If cursor hpos is out of bounds, don't draw garbage. This can
26294 happen in mini-buffer windows when switching between echo area
26295 glyphs and mini-buffer. */
26296 if ((row->reversed_p
26297 ? (w->phys_cursor.hpos >= 0)
26298 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26299 {
26300 int on_p = w->phys_cursor_on_p;
26301 int x1;
26302 int hpos = w->phys_cursor.hpos;
26303
26304 /* When the window is hscrolled, cursor hpos can legitimately be
26305 out of bounds, but we draw the cursor at the corresponding
26306 window margin in that case. */
26307 if (!row->reversed_p && hpos < 0)
26308 hpos = 0;
26309 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26310 hpos = row->used[TEXT_AREA] - 1;
26311
26312 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26313 hl, 0);
26314 w->phys_cursor_on_p = on_p;
26315
26316 if (hl == DRAW_CURSOR)
26317 w->phys_cursor_width = x1 - w->phys_cursor.x;
26318 /* When we erase the cursor, and ROW is overlapped by other
26319 rows, make sure that these overlapping parts of other rows
26320 are redrawn. */
26321 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26322 {
26323 w->phys_cursor_width = x1 - w->phys_cursor.x;
26324
26325 if (row > w->current_matrix->rows
26326 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26327 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26328 OVERLAPS_ERASED_CURSOR);
26329
26330 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26331 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26332 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26333 OVERLAPS_ERASED_CURSOR);
26334 }
26335 }
26336 }
26337
26338
26339 /* EXPORT:
26340 Erase the image of a cursor of window W from the screen. */
26341
26342 void
26343 erase_phys_cursor (struct window *w)
26344 {
26345 struct frame *f = XFRAME (w->frame);
26346 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26347 int hpos = w->phys_cursor.hpos;
26348 int vpos = w->phys_cursor.vpos;
26349 int mouse_face_here_p = 0;
26350 struct glyph_matrix *active_glyphs = w->current_matrix;
26351 struct glyph_row *cursor_row;
26352 struct glyph *cursor_glyph;
26353 enum draw_glyphs_face hl;
26354
26355 /* No cursor displayed or row invalidated => nothing to do on the
26356 screen. */
26357 if (w->phys_cursor_type == NO_CURSOR)
26358 goto mark_cursor_off;
26359
26360 /* VPOS >= active_glyphs->nrows means that window has been resized.
26361 Don't bother to erase the cursor. */
26362 if (vpos >= active_glyphs->nrows)
26363 goto mark_cursor_off;
26364
26365 /* If row containing cursor is marked invalid, there is nothing we
26366 can do. */
26367 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26368 if (!cursor_row->enabled_p)
26369 goto mark_cursor_off;
26370
26371 /* If line spacing is > 0, old cursor may only be partially visible in
26372 window after split-window. So adjust visible height. */
26373 cursor_row->visible_height = min (cursor_row->visible_height,
26374 window_text_bottom_y (w) - cursor_row->y);
26375
26376 /* If row is completely invisible, don't attempt to delete a cursor which
26377 isn't there. This can happen if cursor is at top of a window, and
26378 we switch to a buffer with a header line in that window. */
26379 if (cursor_row->visible_height <= 0)
26380 goto mark_cursor_off;
26381
26382 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26383 if (cursor_row->cursor_in_fringe_p)
26384 {
26385 cursor_row->cursor_in_fringe_p = 0;
26386 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26387 goto mark_cursor_off;
26388 }
26389
26390 /* This can happen when the new row is shorter than the old one.
26391 In this case, either draw_glyphs or clear_end_of_line
26392 should have cleared the cursor. Note that we wouldn't be
26393 able to erase the cursor in this case because we don't have a
26394 cursor glyph at hand. */
26395 if ((cursor_row->reversed_p
26396 ? (w->phys_cursor.hpos < 0)
26397 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26398 goto mark_cursor_off;
26399
26400 /* When the window is hscrolled, cursor hpos can legitimately be out
26401 of bounds, but we draw the cursor at the corresponding window
26402 margin in that case. */
26403 if (!cursor_row->reversed_p && hpos < 0)
26404 hpos = 0;
26405 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26406 hpos = cursor_row->used[TEXT_AREA] - 1;
26407
26408 /* If the cursor is in the mouse face area, redisplay that when
26409 we clear the cursor. */
26410 if (! NILP (hlinfo->mouse_face_window)
26411 && coords_in_mouse_face_p (w, hpos, vpos)
26412 /* Don't redraw the cursor's spot in mouse face if it is at the
26413 end of a line (on a newline). The cursor appears there, but
26414 mouse highlighting does not. */
26415 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26416 mouse_face_here_p = 1;
26417
26418 /* Maybe clear the display under the cursor. */
26419 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26420 {
26421 int x, y, left_x;
26422 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26423 int width;
26424
26425 cursor_glyph = get_phys_cursor_glyph (w);
26426 if (cursor_glyph == NULL)
26427 goto mark_cursor_off;
26428
26429 width = cursor_glyph->pixel_width;
26430 left_x = window_box_left_offset (w, TEXT_AREA);
26431 x = w->phys_cursor.x;
26432 if (x < left_x)
26433 width -= left_x - x;
26434 width = min (width, window_box_width (w, TEXT_AREA) - x);
26435 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26436 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26437
26438 if (width > 0)
26439 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26440 }
26441
26442 /* Erase the cursor by redrawing the character underneath it. */
26443 if (mouse_face_here_p)
26444 hl = DRAW_MOUSE_FACE;
26445 else
26446 hl = DRAW_NORMAL_TEXT;
26447 draw_phys_cursor_glyph (w, cursor_row, hl);
26448
26449 mark_cursor_off:
26450 w->phys_cursor_on_p = 0;
26451 w->phys_cursor_type = NO_CURSOR;
26452 }
26453
26454
26455 /* EXPORT:
26456 Display or clear cursor of window W. If ON is zero, clear the
26457 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26458 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26459
26460 void
26461 display_and_set_cursor (struct window *w, int on,
26462 int hpos, int vpos, int x, int y)
26463 {
26464 struct frame *f = XFRAME (w->frame);
26465 int new_cursor_type;
26466 int new_cursor_width;
26467 int active_cursor;
26468 struct glyph_row *glyph_row;
26469 struct glyph *glyph;
26470
26471 /* This is pointless on invisible frames, and dangerous on garbaged
26472 windows and frames; in the latter case, the frame or window may
26473 be in the midst of changing its size, and x and y may be off the
26474 window. */
26475 if (! FRAME_VISIBLE_P (f)
26476 || FRAME_GARBAGED_P (f)
26477 || vpos >= w->current_matrix->nrows
26478 || hpos >= w->current_matrix->matrix_w)
26479 return;
26480
26481 /* If cursor is off and we want it off, return quickly. */
26482 if (!on && !w->phys_cursor_on_p)
26483 return;
26484
26485 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26486 /* If cursor row is not enabled, we don't really know where to
26487 display the cursor. */
26488 if (!glyph_row->enabled_p)
26489 {
26490 w->phys_cursor_on_p = 0;
26491 return;
26492 }
26493
26494 glyph = NULL;
26495 if (!glyph_row->exact_window_width_line_p
26496 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26497 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26498
26499 eassert (input_blocked_p ());
26500
26501 /* Set new_cursor_type to the cursor we want to be displayed. */
26502 new_cursor_type = get_window_cursor_type (w, glyph,
26503 &new_cursor_width, &active_cursor);
26504
26505 /* If cursor is currently being shown and we don't want it to be or
26506 it is in the wrong place, or the cursor type is not what we want,
26507 erase it. */
26508 if (w->phys_cursor_on_p
26509 && (!on
26510 || w->phys_cursor.x != x
26511 || w->phys_cursor.y != y
26512 || new_cursor_type != w->phys_cursor_type
26513 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26514 && new_cursor_width != w->phys_cursor_width)))
26515 erase_phys_cursor (w);
26516
26517 /* Don't check phys_cursor_on_p here because that flag is only set
26518 to zero in some cases where we know that the cursor has been
26519 completely erased, to avoid the extra work of erasing the cursor
26520 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26521 still not be visible, or it has only been partly erased. */
26522 if (on)
26523 {
26524 w->phys_cursor_ascent = glyph_row->ascent;
26525 w->phys_cursor_height = glyph_row->height;
26526
26527 /* Set phys_cursor_.* before x_draw_.* is called because some
26528 of them may need the information. */
26529 w->phys_cursor.x = x;
26530 w->phys_cursor.y = glyph_row->y;
26531 w->phys_cursor.hpos = hpos;
26532 w->phys_cursor.vpos = vpos;
26533 }
26534
26535 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26536 new_cursor_type, new_cursor_width,
26537 on, active_cursor);
26538 }
26539
26540
26541 /* Switch the display of W's cursor on or off, according to the value
26542 of ON. */
26543
26544 static void
26545 update_window_cursor (struct window *w, int on)
26546 {
26547 /* Don't update cursor in windows whose frame is in the process
26548 of being deleted. */
26549 if (w->current_matrix)
26550 {
26551 int hpos = w->phys_cursor.hpos;
26552 int vpos = w->phys_cursor.vpos;
26553 struct glyph_row *row;
26554
26555 if (vpos >= w->current_matrix->nrows
26556 || hpos >= w->current_matrix->matrix_w)
26557 return;
26558
26559 row = MATRIX_ROW (w->current_matrix, vpos);
26560
26561 /* When the window is hscrolled, cursor hpos can legitimately be
26562 out of bounds, but we draw the cursor at the corresponding
26563 window margin in that case. */
26564 if (!row->reversed_p && hpos < 0)
26565 hpos = 0;
26566 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26567 hpos = row->used[TEXT_AREA] - 1;
26568
26569 block_input ();
26570 display_and_set_cursor (w, on, hpos, vpos,
26571 w->phys_cursor.x, w->phys_cursor.y);
26572 unblock_input ();
26573 }
26574 }
26575
26576
26577 /* Call update_window_cursor with parameter ON_P on all leaf windows
26578 in the window tree rooted at W. */
26579
26580 static void
26581 update_cursor_in_window_tree (struct window *w, int on_p)
26582 {
26583 while (w)
26584 {
26585 if (WINDOWP (w->contents))
26586 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26587 else
26588 update_window_cursor (w, on_p);
26589
26590 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26591 }
26592 }
26593
26594
26595 /* EXPORT:
26596 Display the cursor on window W, or clear it, according to ON_P.
26597 Don't change the cursor's position. */
26598
26599 void
26600 x_update_cursor (struct frame *f, int on_p)
26601 {
26602 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26603 }
26604
26605
26606 /* EXPORT:
26607 Clear the cursor of window W to background color, and mark the
26608 cursor as not shown. This is used when the text where the cursor
26609 is about to be rewritten. */
26610
26611 void
26612 x_clear_cursor (struct window *w)
26613 {
26614 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26615 update_window_cursor (w, 0);
26616 }
26617
26618 #endif /* HAVE_WINDOW_SYSTEM */
26619
26620 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26621 and MSDOS. */
26622 static void
26623 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26624 int start_hpos, int end_hpos,
26625 enum draw_glyphs_face draw)
26626 {
26627 #ifdef HAVE_WINDOW_SYSTEM
26628 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26629 {
26630 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26631 return;
26632 }
26633 #endif
26634 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26635 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26636 #endif
26637 }
26638
26639 /* Display the active region described by mouse_face_* according to DRAW. */
26640
26641 static void
26642 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26643 {
26644 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26645 struct frame *f = XFRAME (WINDOW_FRAME (w));
26646
26647 if (/* If window is in the process of being destroyed, don't bother
26648 to do anything. */
26649 w->current_matrix != NULL
26650 /* Don't update mouse highlight if hidden */
26651 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26652 /* Recognize when we are called to operate on rows that don't exist
26653 anymore. This can happen when a window is split. */
26654 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26655 {
26656 int phys_cursor_on_p = w->phys_cursor_on_p;
26657 struct glyph_row *row, *first, *last;
26658
26659 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26660 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26661
26662 for (row = first; row <= last && row->enabled_p; ++row)
26663 {
26664 int start_hpos, end_hpos, start_x;
26665
26666 /* For all but the first row, the highlight starts at column 0. */
26667 if (row == first)
26668 {
26669 /* R2L rows have BEG and END in reversed order, but the
26670 screen drawing geometry is always left to right. So
26671 we need to mirror the beginning and end of the
26672 highlighted area in R2L rows. */
26673 if (!row->reversed_p)
26674 {
26675 start_hpos = hlinfo->mouse_face_beg_col;
26676 start_x = hlinfo->mouse_face_beg_x;
26677 }
26678 else if (row == last)
26679 {
26680 start_hpos = hlinfo->mouse_face_end_col;
26681 start_x = hlinfo->mouse_face_end_x;
26682 }
26683 else
26684 {
26685 start_hpos = 0;
26686 start_x = 0;
26687 }
26688 }
26689 else if (row->reversed_p && row == last)
26690 {
26691 start_hpos = hlinfo->mouse_face_end_col;
26692 start_x = hlinfo->mouse_face_end_x;
26693 }
26694 else
26695 {
26696 start_hpos = 0;
26697 start_x = 0;
26698 }
26699
26700 if (row == last)
26701 {
26702 if (!row->reversed_p)
26703 end_hpos = hlinfo->mouse_face_end_col;
26704 else if (row == first)
26705 end_hpos = hlinfo->mouse_face_beg_col;
26706 else
26707 {
26708 end_hpos = row->used[TEXT_AREA];
26709 if (draw == DRAW_NORMAL_TEXT)
26710 row->fill_line_p = 1; /* Clear to end of line */
26711 }
26712 }
26713 else if (row->reversed_p && row == first)
26714 end_hpos = hlinfo->mouse_face_beg_col;
26715 else
26716 {
26717 end_hpos = row->used[TEXT_AREA];
26718 if (draw == DRAW_NORMAL_TEXT)
26719 row->fill_line_p = 1; /* Clear to end of line */
26720 }
26721
26722 if (end_hpos > start_hpos)
26723 {
26724 draw_row_with_mouse_face (w, start_x, row,
26725 start_hpos, end_hpos, draw);
26726
26727 row->mouse_face_p
26728 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26729 }
26730 }
26731
26732 #ifdef HAVE_WINDOW_SYSTEM
26733 /* When we've written over the cursor, arrange for it to
26734 be displayed again. */
26735 if (FRAME_WINDOW_P (f)
26736 && phys_cursor_on_p && !w->phys_cursor_on_p)
26737 {
26738 int hpos = w->phys_cursor.hpos;
26739
26740 /* When the window is hscrolled, cursor hpos can legitimately be
26741 out of bounds, but we draw the cursor at the corresponding
26742 window margin in that case. */
26743 if (!row->reversed_p && hpos < 0)
26744 hpos = 0;
26745 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26746 hpos = row->used[TEXT_AREA] - 1;
26747
26748 block_input ();
26749 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26750 w->phys_cursor.x, w->phys_cursor.y);
26751 unblock_input ();
26752 }
26753 #endif /* HAVE_WINDOW_SYSTEM */
26754 }
26755
26756 #ifdef HAVE_WINDOW_SYSTEM
26757 /* Change the mouse cursor. */
26758 if (FRAME_WINDOW_P (f))
26759 {
26760 if (draw == DRAW_NORMAL_TEXT
26761 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26762 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26763 else if (draw == DRAW_MOUSE_FACE)
26764 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26765 else
26766 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26767 }
26768 #endif /* HAVE_WINDOW_SYSTEM */
26769 }
26770
26771 /* EXPORT:
26772 Clear out the mouse-highlighted active region.
26773 Redraw it un-highlighted first. Value is non-zero if mouse
26774 face was actually drawn unhighlighted. */
26775
26776 int
26777 clear_mouse_face (Mouse_HLInfo *hlinfo)
26778 {
26779 int cleared = 0;
26780
26781 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26782 {
26783 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26784 cleared = 1;
26785 }
26786
26787 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26788 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26789 hlinfo->mouse_face_window = Qnil;
26790 hlinfo->mouse_face_overlay = Qnil;
26791 return cleared;
26792 }
26793
26794 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26795 within the mouse face on that window. */
26796 static int
26797 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26798 {
26799 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26800
26801 /* Quickly resolve the easy cases. */
26802 if (!(WINDOWP (hlinfo->mouse_face_window)
26803 && XWINDOW (hlinfo->mouse_face_window) == w))
26804 return 0;
26805 if (vpos < hlinfo->mouse_face_beg_row
26806 || vpos > hlinfo->mouse_face_end_row)
26807 return 0;
26808 if (vpos > hlinfo->mouse_face_beg_row
26809 && vpos < hlinfo->mouse_face_end_row)
26810 return 1;
26811
26812 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26813 {
26814 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26815 {
26816 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26817 return 1;
26818 }
26819 else if ((vpos == hlinfo->mouse_face_beg_row
26820 && hpos >= hlinfo->mouse_face_beg_col)
26821 || (vpos == hlinfo->mouse_face_end_row
26822 && hpos < hlinfo->mouse_face_end_col))
26823 return 1;
26824 }
26825 else
26826 {
26827 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26828 {
26829 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26830 return 1;
26831 }
26832 else if ((vpos == hlinfo->mouse_face_beg_row
26833 && hpos <= hlinfo->mouse_face_beg_col)
26834 || (vpos == hlinfo->mouse_face_end_row
26835 && hpos > hlinfo->mouse_face_end_col))
26836 return 1;
26837 }
26838 return 0;
26839 }
26840
26841
26842 /* EXPORT:
26843 Non-zero if physical cursor of window W is within mouse face. */
26844
26845 int
26846 cursor_in_mouse_face_p (struct window *w)
26847 {
26848 int hpos = w->phys_cursor.hpos;
26849 int vpos = w->phys_cursor.vpos;
26850 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26851
26852 /* When the window is hscrolled, cursor hpos can legitimately be out
26853 of bounds, but we draw the cursor at the corresponding window
26854 margin in that case. */
26855 if (!row->reversed_p && hpos < 0)
26856 hpos = 0;
26857 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26858 hpos = row->used[TEXT_AREA] - 1;
26859
26860 return coords_in_mouse_face_p (w, hpos, vpos);
26861 }
26862
26863
26864 \f
26865 /* Find the glyph rows START_ROW and END_ROW of window W that display
26866 characters between buffer positions START_CHARPOS and END_CHARPOS
26867 (excluding END_CHARPOS). DISP_STRING is a display string that
26868 covers these buffer positions. This is similar to
26869 row_containing_pos, but is more accurate when bidi reordering makes
26870 buffer positions change non-linearly with glyph rows. */
26871 static void
26872 rows_from_pos_range (struct window *w,
26873 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26874 Lisp_Object disp_string,
26875 struct glyph_row **start, struct glyph_row **end)
26876 {
26877 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26878 int last_y = window_text_bottom_y (w);
26879 struct glyph_row *row;
26880
26881 *start = NULL;
26882 *end = NULL;
26883
26884 while (!first->enabled_p
26885 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26886 first++;
26887
26888 /* Find the START row. */
26889 for (row = first;
26890 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26891 row++)
26892 {
26893 /* A row can potentially be the START row if the range of the
26894 characters it displays intersects the range
26895 [START_CHARPOS..END_CHARPOS). */
26896 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26897 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26898 /* See the commentary in row_containing_pos, for the
26899 explanation of the complicated way to check whether
26900 some position is beyond the end of the characters
26901 displayed by a row. */
26902 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26903 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26904 && !row->ends_at_zv_p
26905 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26906 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26907 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26908 && !row->ends_at_zv_p
26909 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26910 {
26911 /* Found a candidate row. Now make sure at least one of the
26912 glyphs it displays has a charpos from the range
26913 [START_CHARPOS..END_CHARPOS).
26914
26915 This is not obvious because bidi reordering could make
26916 buffer positions of a row be 1,2,3,102,101,100, and if we
26917 want to highlight characters in [50..60), we don't want
26918 this row, even though [50..60) does intersect [1..103),
26919 the range of character positions given by the row's start
26920 and end positions. */
26921 struct glyph *g = row->glyphs[TEXT_AREA];
26922 struct glyph *e = g + row->used[TEXT_AREA];
26923
26924 while (g < e)
26925 {
26926 if (((BUFFERP (g->object) || INTEGERP (g->object))
26927 && start_charpos <= g->charpos && g->charpos < end_charpos)
26928 /* A glyph that comes from DISP_STRING is by
26929 definition to be highlighted. */
26930 || EQ (g->object, disp_string))
26931 *start = row;
26932 g++;
26933 }
26934 if (*start)
26935 break;
26936 }
26937 }
26938
26939 /* Find the END row. */
26940 if (!*start
26941 /* If the last row is partially visible, start looking for END
26942 from that row, instead of starting from FIRST. */
26943 && !(row->enabled_p
26944 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26945 row = first;
26946 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26947 {
26948 struct glyph_row *next = row + 1;
26949 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26950
26951 if (!next->enabled_p
26952 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26953 /* The first row >= START whose range of displayed characters
26954 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26955 is the row END + 1. */
26956 || (start_charpos < next_start
26957 && end_charpos < next_start)
26958 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26959 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26960 && !next->ends_at_zv_p
26961 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26962 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26963 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26964 && !next->ends_at_zv_p
26965 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26966 {
26967 *end = row;
26968 break;
26969 }
26970 else
26971 {
26972 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26973 but none of the characters it displays are in the range, it is
26974 also END + 1. */
26975 struct glyph *g = next->glyphs[TEXT_AREA];
26976 struct glyph *s = g;
26977 struct glyph *e = g + next->used[TEXT_AREA];
26978
26979 while (g < e)
26980 {
26981 if (((BUFFERP (g->object) || INTEGERP (g->object))
26982 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26983 /* If the buffer position of the first glyph in
26984 the row is equal to END_CHARPOS, it means
26985 the last character to be highlighted is the
26986 newline of ROW, and we must consider NEXT as
26987 END, not END+1. */
26988 || (((!next->reversed_p && g == s)
26989 || (next->reversed_p && g == e - 1))
26990 && (g->charpos == end_charpos
26991 /* Special case for when NEXT is an
26992 empty line at ZV. */
26993 || (g->charpos == -1
26994 && !row->ends_at_zv_p
26995 && next_start == end_charpos)))))
26996 /* A glyph that comes from DISP_STRING is by
26997 definition to be highlighted. */
26998 || EQ (g->object, disp_string))
26999 break;
27000 g++;
27001 }
27002 if (g == e)
27003 {
27004 *end = row;
27005 break;
27006 }
27007 /* The first row that ends at ZV must be the last to be
27008 highlighted. */
27009 else if (next->ends_at_zv_p)
27010 {
27011 *end = next;
27012 break;
27013 }
27014 }
27015 }
27016 }
27017
27018 /* This function sets the mouse_face_* elements of HLINFO, assuming
27019 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
27020 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
27021 for the overlay or run of text properties specifying the mouse
27022 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
27023 before-string and after-string that must also be highlighted.
27024 DISP_STRING, if non-nil, is a display string that may cover some
27025 or all of the highlighted text. */
27026
27027 static void
27028 mouse_face_from_buffer_pos (Lisp_Object window,
27029 Mouse_HLInfo *hlinfo,
27030 ptrdiff_t mouse_charpos,
27031 ptrdiff_t start_charpos,
27032 ptrdiff_t end_charpos,
27033 Lisp_Object before_string,
27034 Lisp_Object after_string,
27035 Lisp_Object disp_string)
27036 {
27037 struct window *w = XWINDOW (window);
27038 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27039 struct glyph_row *r1, *r2;
27040 struct glyph *glyph, *end;
27041 ptrdiff_t ignore, pos;
27042 int x;
27043
27044 eassert (NILP (disp_string) || STRINGP (disp_string));
27045 eassert (NILP (before_string) || STRINGP (before_string));
27046 eassert (NILP (after_string) || STRINGP (after_string));
27047
27048 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
27049 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
27050 if (r1 == NULL)
27051 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27052 /* If the before-string or display-string contains newlines,
27053 rows_from_pos_range skips to its last row. Move back. */
27054 if (!NILP (before_string) || !NILP (disp_string))
27055 {
27056 struct glyph_row *prev;
27057 while ((prev = r1 - 1, prev >= first)
27058 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
27059 && prev->used[TEXT_AREA] > 0)
27060 {
27061 struct glyph *beg = prev->glyphs[TEXT_AREA];
27062 glyph = beg + prev->used[TEXT_AREA];
27063 while (--glyph >= beg && INTEGERP (glyph->object));
27064 if (glyph < beg
27065 || !(EQ (glyph->object, before_string)
27066 || EQ (glyph->object, disp_string)))
27067 break;
27068 r1 = prev;
27069 }
27070 }
27071 if (r2 == NULL)
27072 {
27073 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27074 hlinfo->mouse_face_past_end = 1;
27075 }
27076 else if (!NILP (after_string))
27077 {
27078 /* If the after-string has newlines, advance to its last row. */
27079 struct glyph_row *next;
27080 struct glyph_row *last
27081 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
27082
27083 for (next = r2 + 1;
27084 next <= last
27085 && next->used[TEXT_AREA] > 0
27086 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
27087 ++next)
27088 r2 = next;
27089 }
27090 /* The rest of the display engine assumes that mouse_face_beg_row is
27091 either above mouse_face_end_row or identical to it. But with
27092 bidi-reordered continued lines, the row for START_CHARPOS could
27093 be below the row for END_CHARPOS. If so, swap the rows and store
27094 them in correct order. */
27095 if (r1->y > r2->y)
27096 {
27097 struct glyph_row *tem = r2;
27098
27099 r2 = r1;
27100 r1 = tem;
27101 }
27102
27103 hlinfo->mouse_face_beg_y = r1->y;
27104 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
27105 hlinfo->mouse_face_end_y = r2->y;
27106 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
27107
27108 /* For a bidi-reordered row, the positions of BEFORE_STRING,
27109 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
27110 could be anywhere in the row and in any order. The strategy
27111 below is to find the leftmost and the rightmost glyph that
27112 belongs to either of these 3 strings, or whose position is
27113 between START_CHARPOS and END_CHARPOS, and highlight all the
27114 glyphs between those two. This may cover more than just the text
27115 between START_CHARPOS and END_CHARPOS if the range of characters
27116 strides the bidi level boundary, e.g. if the beginning is in R2L
27117 text while the end is in L2R text or vice versa. */
27118 if (!r1->reversed_p)
27119 {
27120 /* This row is in a left to right paragraph. Scan it left to
27121 right. */
27122 glyph = r1->glyphs[TEXT_AREA];
27123 end = glyph + r1->used[TEXT_AREA];
27124 x = r1->x;
27125
27126 /* Skip truncation glyphs at the start of the glyph row. */
27127 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27128 for (; glyph < end
27129 && INTEGERP (glyph->object)
27130 && glyph->charpos < 0;
27131 ++glyph)
27132 x += glyph->pixel_width;
27133
27134 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27135 or DISP_STRING, and the first glyph from buffer whose
27136 position is between START_CHARPOS and END_CHARPOS. */
27137 for (; glyph < end
27138 && !INTEGERP (glyph->object)
27139 && !EQ (glyph->object, disp_string)
27140 && !(BUFFERP (glyph->object)
27141 && (glyph->charpos >= start_charpos
27142 && glyph->charpos < end_charpos));
27143 ++glyph)
27144 {
27145 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27146 are present at buffer positions between START_CHARPOS and
27147 END_CHARPOS, or if they come from an overlay. */
27148 if (EQ (glyph->object, before_string))
27149 {
27150 pos = string_buffer_position (before_string,
27151 start_charpos);
27152 /* If pos == 0, it means before_string came from an
27153 overlay, not from a buffer position. */
27154 if (!pos || (pos >= start_charpos && pos < end_charpos))
27155 break;
27156 }
27157 else if (EQ (glyph->object, after_string))
27158 {
27159 pos = string_buffer_position (after_string, end_charpos);
27160 if (!pos || (pos >= start_charpos && pos < end_charpos))
27161 break;
27162 }
27163 x += glyph->pixel_width;
27164 }
27165 hlinfo->mouse_face_beg_x = x;
27166 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27167 }
27168 else
27169 {
27170 /* This row is in a right to left paragraph. Scan it right to
27171 left. */
27172 struct glyph *g;
27173
27174 end = r1->glyphs[TEXT_AREA] - 1;
27175 glyph = end + r1->used[TEXT_AREA];
27176
27177 /* Skip truncation glyphs at the start of the glyph row. */
27178 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
27179 for (; glyph > end
27180 && INTEGERP (glyph->object)
27181 && glyph->charpos < 0;
27182 --glyph)
27183 ;
27184
27185 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27186 or DISP_STRING, and the first glyph from buffer whose
27187 position is between START_CHARPOS and END_CHARPOS. */
27188 for (; glyph > end
27189 && !INTEGERP (glyph->object)
27190 && !EQ (glyph->object, disp_string)
27191 && !(BUFFERP (glyph->object)
27192 && (glyph->charpos >= start_charpos
27193 && glyph->charpos < end_charpos));
27194 --glyph)
27195 {
27196 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27197 are present at buffer positions between START_CHARPOS and
27198 END_CHARPOS, or if they come from an overlay. */
27199 if (EQ (glyph->object, before_string))
27200 {
27201 pos = string_buffer_position (before_string, start_charpos);
27202 /* If pos == 0, it means before_string came from an
27203 overlay, not from a buffer position. */
27204 if (!pos || (pos >= start_charpos && pos < end_charpos))
27205 break;
27206 }
27207 else if (EQ (glyph->object, after_string))
27208 {
27209 pos = string_buffer_position (after_string, end_charpos);
27210 if (!pos || (pos >= start_charpos && pos < end_charpos))
27211 break;
27212 }
27213 }
27214
27215 glyph++; /* first glyph to the right of the highlighted area */
27216 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27217 x += g->pixel_width;
27218 hlinfo->mouse_face_beg_x = x;
27219 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27220 }
27221
27222 /* If the highlight ends in a different row, compute GLYPH and END
27223 for the end row. Otherwise, reuse the values computed above for
27224 the row where the highlight begins. */
27225 if (r2 != r1)
27226 {
27227 if (!r2->reversed_p)
27228 {
27229 glyph = r2->glyphs[TEXT_AREA];
27230 end = glyph + r2->used[TEXT_AREA];
27231 x = r2->x;
27232 }
27233 else
27234 {
27235 end = r2->glyphs[TEXT_AREA] - 1;
27236 glyph = end + r2->used[TEXT_AREA];
27237 }
27238 }
27239
27240 if (!r2->reversed_p)
27241 {
27242 /* Skip truncation and continuation glyphs near the end of the
27243 row, and also blanks and stretch glyphs inserted by
27244 extend_face_to_end_of_line. */
27245 while (end > glyph
27246 && INTEGERP ((end - 1)->object))
27247 --end;
27248 /* Scan the rest of the glyph row from the end, looking for the
27249 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27250 DISP_STRING, or whose position is between START_CHARPOS
27251 and END_CHARPOS */
27252 for (--end;
27253 end > glyph
27254 && !INTEGERP (end->object)
27255 && !EQ (end->object, disp_string)
27256 && !(BUFFERP (end->object)
27257 && (end->charpos >= start_charpos
27258 && end->charpos < end_charpos));
27259 --end)
27260 {
27261 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27262 are present at buffer positions between START_CHARPOS and
27263 END_CHARPOS, or if they come from an overlay. */
27264 if (EQ (end->object, before_string))
27265 {
27266 pos = string_buffer_position (before_string, start_charpos);
27267 if (!pos || (pos >= start_charpos && pos < end_charpos))
27268 break;
27269 }
27270 else if (EQ (end->object, after_string))
27271 {
27272 pos = string_buffer_position (after_string, end_charpos);
27273 if (!pos || (pos >= start_charpos && pos < end_charpos))
27274 break;
27275 }
27276 }
27277 /* Find the X coordinate of the last glyph to be highlighted. */
27278 for (; glyph <= end; ++glyph)
27279 x += glyph->pixel_width;
27280
27281 hlinfo->mouse_face_end_x = x;
27282 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27283 }
27284 else
27285 {
27286 /* Skip truncation and continuation glyphs near the end of the
27287 row, and also blanks and stretch glyphs inserted by
27288 extend_face_to_end_of_line. */
27289 x = r2->x;
27290 end++;
27291 while (end < glyph
27292 && INTEGERP (end->object))
27293 {
27294 x += end->pixel_width;
27295 ++end;
27296 }
27297 /* Scan the rest of the glyph row from the end, looking for the
27298 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27299 DISP_STRING, or whose position is between START_CHARPOS
27300 and END_CHARPOS */
27301 for ( ;
27302 end < glyph
27303 && !INTEGERP (end->object)
27304 && !EQ (end->object, disp_string)
27305 && !(BUFFERP (end->object)
27306 && (end->charpos >= start_charpos
27307 && end->charpos < end_charpos));
27308 ++end)
27309 {
27310 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27311 are present at buffer positions between START_CHARPOS and
27312 END_CHARPOS, or if they come from an overlay. */
27313 if (EQ (end->object, before_string))
27314 {
27315 pos = string_buffer_position (before_string, start_charpos);
27316 if (!pos || (pos >= start_charpos && pos < end_charpos))
27317 break;
27318 }
27319 else if (EQ (end->object, after_string))
27320 {
27321 pos = string_buffer_position (after_string, end_charpos);
27322 if (!pos || (pos >= start_charpos && pos < end_charpos))
27323 break;
27324 }
27325 x += end->pixel_width;
27326 }
27327 /* If we exited the above loop because we arrived at the last
27328 glyph of the row, and its buffer position is still not in
27329 range, it means the last character in range is the preceding
27330 newline. Bump the end column and x values to get past the
27331 last glyph. */
27332 if (end == glyph
27333 && BUFFERP (end->object)
27334 && (end->charpos < start_charpos
27335 || end->charpos >= end_charpos))
27336 {
27337 x += end->pixel_width;
27338 ++end;
27339 }
27340 hlinfo->mouse_face_end_x = x;
27341 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27342 }
27343
27344 hlinfo->mouse_face_window = window;
27345 hlinfo->mouse_face_face_id
27346 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27347 mouse_charpos + 1,
27348 !hlinfo->mouse_face_hidden, -1);
27349 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27350 }
27351
27352 /* The following function is not used anymore (replaced with
27353 mouse_face_from_string_pos), but I leave it here for the time
27354 being, in case someone would. */
27355
27356 #if 0 /* not used */
27357
27358 /* Find the position of the glyph for position POS in OBJECT in
27359 window W's current matrix, and return in *X, *Y the pixel
27360 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27361
27362 RIGHT_P non-zero means return the position of the right edge of the
27363 glyph, RIGHT_P zero means return the left edge position.
27364
27365 If no glyph for POS exists in the matrix, return the position of
27366 the glyph with the next smaller position that is in the matrix, if
27367 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27368 exists in the matrix, return the position of the glyph with the
27369 next larger position in OBJECT.
27370
27371 Value is non-zero if a glyph was found. */
27372
27373 static int
27374 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27375 int *hpos, int *vpos, int *x, int *y, int right_p)
27376 {
27377 int yb = window_text_bottom_y (w);
27378 struct glyph_row *r;
27379 struct glyph *best_glyph = NULL;
27380 struct glyph_row *best_row = NULL;
27381 int best_x = 0;
27382
27383 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27384 r->enabled_p && r->y < yb;
27385 ++r)
27386 {
27387 struct glyph *g = r->glyphs[TEXT_AREA];
27388 struct glyph *e = g + r->used[TEXT_AREA];
27389 int gx;
27390
27391 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27392 if (EQ (g->object, object))
27393 {
27394 if (g->charpos == pos)
27395 {
27396 best_glyph = g;
27397 best_x = gx;
27398 best_row = r;
27399 goto found;
27400 }
27401 else if (best_glyph == NULL
27402 || ((eabs (g->charpos - pos)
27403 < eabs (best_glyph->charpos - pos))
27404 && (right_p
27405 ? g->charpos < pos
27406 : g->charpos > pos)))
27407 {
27408 best_glyph = g;
27409 best_x = gx;
27410 best_row = r;
27411 }
27412 }
27413 }
27414
27415 found:
27416
27417 if (best_glyph)
27418 {
27419 *x = best_x;
27420 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27421
27422 if (right_p)
27423 {
27424 *x += best_glyph->pixel_width;
27425 ++*hpos;
27426 }
27427
27428 *y = best_row->y;
27429 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
27430 }
27431
27432 return best_glyph != NULL;
27433 }
27434 #endif /* not used */
27435
27436 /* Find the positions of the first and the last glyphs in window W's
27437 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27438 (assumed to be a string), and return in HLINFO's mouse_face_*
27439 members the pixel and column/row coordinates of those glyphs. */
27440
27441 static void
27442 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27443 Lisp_Object object,
27444 ptrdiff_t startpos, ptrdiff_t endpos)
27445 {
27446 int yb = window_text_bottom_y (w);
27447 struct glyph_row *r;
27448 struct glyph *g, *e;
27449 int gx;
27450 int found = 0;
27451
27452 /* Find the glyph row with at least one position in the range
27453 [STARTPOS..ENDPOS], and the first glyph in that row whose
27454 position belongs to that range. */
27455 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27456 r->enabled_p && r->y < yb;
27457 ++r)
27458 {
27459 if (!r->reversed_p)
27460 {
27461 g = r->glyphs[TEXT_AREA];
27462 e = g + r->used[TEXT_AREA];
27463 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27464 if (EQ (g->object, object)
27465 && startpos <= g->charpos && g->charpos <= endpos)
27466 {
27467 hlinfo->mouse_face_beg_row
27468 = MATRIX_ROW_VPOS (r, w->current_matrix);
27469 hlinfo->mouse_face_beg_y = r->y;
27470 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27471 hlinfo->mouse_face_beg_x = gx;
27472 found = 1;
27473 break;
27474 }
27475 }
27476 else
27477 {
27478 struct glyph *g1;
27479
27480 e = r->glyphs[TEXT_AREA];
27481 g = e + r->used[TEXT_AREA];
27482 for ( ; g > e; --g)
27483 if (EQ ((g-1)->object, object)
27484 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27485 {
27486 hlinfo->mouse_face_beg_row
27487 = MATRIX_ROW_VPOS (r, w->current_matrix);
27488 hlinfo->mouse_face_beg_y = r->y;
27489 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27490 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27491 gx += g1->pixel_width;
27492 hlinfo->mouse_face_beg_x = gx;
27493 found = 1;
27494 break;
27495 }
27496 }
27497 if (found)
27498 break;
27499 }
27500
27501 if (!found)
27502 return;
27503
27504 /* Starting with the next row, look for the first row which does NOT
27505 include any glyphs whose positions are in the range. */
27506 for (++r; r->enabled_p && r->y < yb; ++r)
27507 {
27508 g = r->glyphs[TEXT_AREA];
27509 e = g + r->used[TEXT_AREA];
27510 found = 0;
27511 for ( ; g < e; ++g)
27512 if (EQ (g->object, object)
27513 && startpos <= g->charpos && g->charpos <= endpos)
27514 {
27515 found = 1;
27516 break;
27517 }
27518 if (!found)
27519 break;
27520 }
27521
27522 /* The highlighted region ends on the previous row. */
27523 r--;
27524
27525 /* Set the end row and its vertical pixel coordinate. */
27526 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27527 hlinfo->mouse_face_end_y = r->y;
27528
27529 /* Compute and set the end column and the end column's horizontal
27530 pixel coordinate. */
27531 if (!r->reversed_p)
27532 {
27533 g = r->glyphs[TEXT_AREA];
27534 e = g + r->used[TEXT_AREA];
27535 for ( ; e > g; --e)
27536 if (EQ ((e-1)->object, object)
27537 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27538 break;
27539 hlinfo->mouse_face_end_col = e - g;
27540
27541 for (gx = r->x; g < e; ++g)
27542 gx += g->pixel_width;
27543 hlinfo->mouse_face_end_x = gx;
27544 }
27545 else
27546 {
27547 e = r->glyphs[TEXT_AREA];
27548 g = e + r->used[TEXT_AREA];
27549 for (gx = r->x ; e < g; ++e)
27550 {
27551 if (EQ (e->object, object)
27552 && startpos <= e->charpos && e->charpos <= endpos)
27553 break;
27554 gx += e->pixel_width;
27555 }
27556 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27557 hlinfo->mouse_face_end_x = gx;
27558 }
27559 }
27560
27561 #ifdef HAVE_WINDOW_SYSTEM
27562
27563 /* See if position X, Y is within a hot-spot of an image. */
27564
27565 static int
27566 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27567 {
27568 if (!CONSP (hot_spot))
27569 return 0;
27570
27571 if (EQ (XCAR (hot_spot), Qrect))
27572 {
27573 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27574 Lisp_Object rect = XCDR (hot_spot);
27575 Lisp_Object tem;
27576 if (!CONSP (rect))
27577 return 0;
27578 if (!CONSP (XCAR (rect)))
27579 return 0;
27580 if (!CONSP (XCDR (rect)))
27581 return 0;
27582 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27583 return 0;
27584 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27585 return 0;
27586 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27587 return 0;
27588 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27589 return 0;
27590 return 1;
27591 }
27592 else if (EQ (XCAR (hot_spot), Qcircle))
27593 {
27594 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27595 Lisp_Object circ = XCDR (hot_spot);
27596 Lisp_Object lr, lx0, ly0;
27597 if (CONSP (circ)
27598 && CONSP (XCAR (circ))
27599 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27600 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27601 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27602 {
27603 double r = XFLOATINT (lr);
27604 double dx = XINT (lx0) - x;
27605 double dy = XINT (ly0) - y;
27606 return (dx * dx + dy * dy <= r * r);
27607 }
27608 }
27609 else if (EQ (XCAR (hot_spot), Qpoly))
27610 {
27611 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27612 if (VECTORP (XCDR (hot_spot)))
27613 {
27614 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27615 Lisp_Object *poly = v->contents;
27616 ptrdiff_t n = v->header.size;
27617 ptrdiff_t i;
27618 int inside = 0;
27619 Lisp_Object lx, ly;
27620 int x0, y0;
27621
27622 /* Need an even number of coordinates, and at least 3 edges. */
27623 if (n < 6 || n & 1)
27624 return 0;
27625
27626 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27627 If count is odd, we are inside polygon. Pixels on edges
27628 may or may not be included depending on actual geometry of the
27629 polygon. */
27630 if ((lx = poly[n-2], !INTEGERP (lx))
27631 || (ly = poly[n-1], !INTEGERP (lx)))
27632 return 0;
27633 x0 = XINT (lx), y0 = XINT (ly);
27634 for (i = 0; i < n; i += 2)
27635 {
27636 int x1 = x0, y1 = y0;
27637 if ((lx = poly[i], !INTEGERP (lx))
27638 || (ly = poly[i+1], !INTEGERP (ly)))
27639 return 0;
27640 x0 = XINT (lx), y0 = XINT (ly);
27641
27642 /* Does this segment cross the X line? */
27643 if (x0 >= x)
27644 {
27645 if (x1 >= x)
27646 continue;
27647 }
27648 else if (x1 < x)
27649 continue;
27650 if (y > y0 && y > y1)
27651 continue;
27652 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27653 inside = !inside;
27654 }
27655 return inside;
27656 }
27657 }
27658 return 0;
27659 }
27660
27661 Lisp_Object
27662 find_hot_spot (Lisp_Object map, int x, int y)
27663 {
27664 while (CONSP (map))
27665 {
27666 if (CONSP (XCAR (map))
27667 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27668 return XCAR (map);
27669 map = XCDR (map);
27670 }
27671
27672 return Qnil;
27673 }
27674
27675 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27676 3, 3, 0,
27677 doc: /* Lookup in image map MAP coordinates X and Y.
27678 An image map is an alist where each element has the format (AREA ID PLIST).
27679 An AREA is specified as either a rectangle, a circle, or a polygon:
27680 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27681 pixel coordinates of the upper left and bottom right corners.
27682 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27683 and the radius of the circle; r may be a float or integer.
27684 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27685 vector describes one corner in the polygon.
27686 Returns the alist element for the first matching AREA in MAP. */)
27687 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27688 {
27689 if (NILP (map))
27690 return Qnil;
27691
27692 CHECK_NUMBER (x);
27693 CHECK_NUMBER (y);
27694
27695 return find_hot_spot (map,
27696 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27697 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27698 }
27699
27700
27701 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27702 static void
27703 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27704 {
27705 /* Do not change cursor shape while dragging mouse. */
27706 if (!NILP (do_mouse_tracking))
27707 return;
27708
27709 if (!NILP (pointer))
27710 {
27711 if (EQ (pointer, Qarrow))
27712 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27713 else if (EQ (pointer, Qhand))
27714 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27715 else if (EQ (pointer, Qtext))
27716 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27717 else if (EQ (pointer, intern ("hdrag")))
27718 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27719 #ifdef HAVE_X_WINDOWS
27720 else if (EQ (pointer, intern ("vdrag")))
27721 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27722 #endif
27723 else if (EQ (pointer, intern ("hourglass")))
27724 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27725 else if (EQ (pointer, Qmodeline))
27726 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27727 else
27728 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27729 }
27730
27731 if (cursor != No_Cursor)
27732 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27733 }
27734
27735 #endif /* HAVE_WINDOW_SYSTEM */
27736
27737 /* Take proper action when mouse has moved to the mode or header line
27738 or marginal area AREA of window W, x-position X and y-position Y.
27739 X is relative to the start of the text display area of W, so the
27740 width of bitmap areas and scroll bars must be subtracted to get a
27741 position relative to the start of the mode line. */
27742
27743 static void
27744 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27745 enum window_part area)
27746 {
27747 struct window *w = XWINDOW (window);
27748 struct frame *f = XFRAME (w->frame);
27749 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27750 #ifdef HAVE_WINDOW_SYSTEM
27751 Display_Info *dpyinfo;
27752 #endif
27753 Cursor cursor = No_Cursor;
27754 Lisp_Object pointer = Qnil;
27755 int dx, dy, width, height;
27756 ptrdiff_t charpos;
27757 Lisp_Object string, object = Qnil;
27758 Lisp_Object pos IF_LINT (= Qnil), help;
27759
27760 Lisp_Object mouse_face;
27761 int original_x_pixel = x;
27762 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27763 struct glyph_row *row IF_LINT (= 0);
27764
27765 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27766 {
27767 int x0;
27768 struct glyph *end;
27769
27770 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27771 returns them in row/column units! */
27772 string = mode_line_string (w, area, &x, &y, &charpos,
27773 &object, &dx, &dy, &width, &height);
27774
27775 row = (area == ON_MODE_LINE
27776 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27777 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27778
27779 /* Find the glyph under the mouse pointer. */
27780 if (row->mode_line_p && row->enabled_p)
27781 {
27782 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27783 end = glyph + row->used[TEXT_AREA];
27784
27785 for (x0 = original_x_pixel;
27786 glyph < end && x0 >= glyph->pixel_width;
27787 ++glyph)
27788 x0 -= glyph->pixel_width;
27789
27790 if (glyph >= end)
27791 glyph = NULL;
27792 }
27793 }
27794 else
27795 {
27796 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27797 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27798 returns them in row/column units! */
27799 string = marginal_area_string (w, area, &x, &y, &charpos,
27800 &object, &dx, &dy, &width, &height);
27801 }
27802
27803 help = Qnil;
27804
27805 #ifdef HAVE_WINDOW_SYSTEM
27806 if (IMAGEP (object))
27807 {
27808 Lisp_Object image_map, hotspot;
27809 if ((image_map = Fplist_get (XCDR (object), QCmap),
27810 !NILP (image_map))
27811 && (hotspot = find_hot_spot (image_map, dx, dy),
27812 CONSP (hotspot))
27813 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27814 {
27815 Lisp_Object plist;
27816
27817 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27818 If so, we could look for mouse-enter, mouse-leave
27819 properties in PLIST (and do something...). */
27820 hotspot = XCDR (hotspot);
27821 if (CONSP (hotspot)
27822 && (plist = XCAR (hotspot), CONSP (plist)))
27823 {
27824 pointer = Fplist_get (plist, Qpointer);
27825 if (NILP (pointer))
27826 pointer = Qhand;
27827 help = Fplist_get (plist, Qhelp_echo);
27828 if (!NILP (help))
27829 {
27830 help_echo_string = help;
27831 XSETWINDOW (help_echo_window, w);
27832 help_echo_object = w->contents;
27833 help_echo_pos = charpos;
27834 }
27835 }
27836 }
27837 if (NILP (pointer))
27838 pointer = Fplist_get (XCDR (object), QCpointer);
27839 }
27840 #endif /* HAVE_WINDOW_SYSTEM */
27841
27842 if (STRINGP (string))
27843 pos = make_number (charpos);
27844
27845 /* Set the help text and mouse pointer. If the mouse is on a part
27846 of the mode line without any text (e.g. past the right edge of
27847 the mode line text), use the default help text and pointer. */
27848 if (STRINGP (string) || area == ON_MODE_LINE)
27849 {
27850 /* Arrange to display the help by setting the global variables
27851 help_echo_string, help_echo_object, and help_echo_pos. */
27852 if (NILP (help))
27853 {
27854 if (STRINGP (string))
27855 help = Fget_text_property (pos, Qhelp_echo, string);
27856
27857 if (!NILP (help))
27858 {
27859 help_echo_string = help;
27860 XSETWINDOW (help_echo_window, w);
27861 help_echo_object = string;
27862 help_echo_pos = charpos;
27863 }
27864 else if (area == ON_MODE_LINE)
27865 {
27866 Lisp_Object default_help
27867 = buffer_local_value_1 (Qmode_line_default_help_echo,
27868 w->contents);
27869
27870 if (STRINGP (default_help))
27871 {
27872 help_echo_string = default_help;
27873 XSETWINDOW (help_echo_window, w);
27874 help_echo_object = Qnil;
27875 help_echo_pos = -1;
27876 }
27877 }
27878 }
27879
27880 #ifdef HAVE_WINDOW_SYSTEM
27881 /* Change the mouse pointer according to what is under it. */
27882 if (FRAME_WINDOW_P (f))
27883 {
27884 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27885 if (STRINGP (string))
27886 {
27887 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27888
27889 if (NILP (pointer))
27890 pointer = Fget_text_property (pos, Qpointer, string);
27891
27892 /* Change the mouse pointer according to what is under X/Y. */
27893 if (NILP (pointer)
27894 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27895 {
27896 Lisp_Object map;
27897 map = Fget_text_property (pos, Qlocal_map, string);
27898 if (!KEYMAPP (map))
27899 map = Fget_text_property (pos, Qkeymap, string);
27900 if (!KEYMAPP (map))
27901 cursor = dpyinfo->vertical_scroll_bar_cursor;
27902 }
27903 }
27904 else
27905 /* Default mode-line pointer. */
27906 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27907 }
27908 #endif
27909 }
27910
27911 /* Change the mouse face according to what is under X/Y. */
27912 if (STRINGP (string))
27913 {
27914 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27915 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27916 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27917 && glyph)
27918 {
27919 Lisp_Object b, e;
27920
27921 struct glyph * tmp_glyph;
27922
27923 int gpos;
27924 int gseq_length;
27925 int total_pixel_width;
27926 ptrdiff_t begpos, endpos, ignore;
27927
27928 int vpos, hpos;
27929
27930 b = Fprevious_single_property_change (make_number (charpos + 1),
27931 Qmouse_face, string, Qnil);
27932 if (NILP (b))
27933 begpos = 0;
27934 else
27935 begpos = XINT (b);
27936
27937 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27938 if (NILP (e))
27939 endpos = SCHARS (string);
27940 else
27941 endpos = XINT (e);
27942
27943 /* Calculate the glyph position GPOS of GLYPH in the
27944 displayed string, relative to the beginning of the
27945 highlighted part of the string.
27946
27947 Note: GPOS is different from CHARPOS. CHARPOS is the
27948 position of GLYPH in the internal string object. A mode
27949 line string format has structures which are converted to
27950 a flattened string by the Emacs Lisp interpreter. The
27951 internal string is an element of those structures. The
27952 displayed string is the flattened string. */
27953 tmp_glyph = row_start_glyph;
27954 while (tmp_glyph < glyph
27955 && (!(EQ (tmp_glyph->object, glyph->object)
27956 && begpos <= tmp_glyph->charpos
27957 && tmp_glyph->charpos < endpos)))
27958 tmp_glyph++;
27959 gpos = glyph - tmp_glyph;
27960
27961 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27962 the highlighted part of the displayed string to which
27963 GLYPH belongs. Note: GSEQ_LENGTH is different from
27964 SCHARS (STRING), because the latter returns the length of
27965 the internal string. */
27966 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27967 tmp_glyph > glyph
27968 && (!(EQ (tmp_glyph->object, glyph->object)
27969 && begpos <= tmp_glyph->charpos
27970 && tmp_glyph->charpos < endpos));
27971 tmp_glyph--)
27972 ;
27973 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27974
27975 /* Calculate the total pixel width of all the glyphs between
27976 the beginning of the highlighted area and GLYPH. */
27977 total_pixel_width = 0;
27978 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27979 total_pixel_width += tmp_glyph->pixel_width;
27980
27981 /* Pre calculation of re-rendering position. Note: X is in
27982 column units here, after the call to mode_line_string or
27983 marginal_area_string. */
27984 hpos = x - gpos;
27985 vpos = (area == ON_MODE_LINE
27986 ? (w->current_matrix)->nrows - 1
27987 : 0);
27988
27989 /* If GLYPH's position is included in the region that is
27990 already drawn in mouse face, we have nothing to do. */
27991 if ( EQ (window, hlinfo->mouse_face_window)
27992 && (!row->reversed_p
27993 ? (hlinfo->mouse_face_beg_col <= hpos
27994 && hpos < hlinfo->mouse_face_end_col)
27995 /* In R2L rows we swap BEG and END, see below. */
27996 : (hlinfo->mouse_face_end_col <= hpos
27997 && hpos < hlinfo->mouse_face_beg_col))
27998 && hlinfo->mouse_face_beg_row == vpos )
27999 return;
28000
28001 if (clear_mouse_face (hlinfo))
28002 cursor = No_Cursor;
28003
28004 if (!row->reversed_p)
28005 {
28006 hlinfo->mouse_face_beg_col = hpos;
28007 hlinfo->mouse_face_beg_x = original_x_pixel
28008 - (total_pixel_width + dx);
28009 hlinfo->mouse_face_end_col = hpos + gseq_length;
28010 hlinfo->mouse_face_end_x = 0;
28011 }
28012 else
28013 {
28014 /* In R2L rows, show_mouse_face expects BEG and END
28015 coordinates to be swapped. */
28016 hlinfo->mouse_face_end_col = hpos;
28017 hlinfo->mouse_face_end_x = original_x_pixel
28018 - (total_pixel_width + dx);
28019 hlinfo->mouse_face_beg_col = hpos + gseq_length;
28020 hlinfo->mouse_face_beg_x = 0;
28021 }
28022
28023 hlinfo->mouse_face_beg_row = vpos;
28024 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
28025 hlinfo->mouse_face_beg_y = 0;
28026 hlinfo->mouse_face_end_y = 0;
28027 hlinfo->mouse_face_past_end = 0;
28028 hlinfo->mouse_face_window = window;
28029
28030 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
28031 charpos,
28032 0, 0, 0,
28033 &ignore,
28034 glyph->face_id,
28035 1);
28036 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28037
28038 if (NILP (pointer))
28039 pointer = Qhand;
28040 }
28041 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
28042 clear_mouse_face (hlinfo);
28043 }
28044 #ifdef HAVE_WINDOW_SYSTEM
28045 if (FRAME_WINDOW_P (f))
28046 define_frame_cursor1 (f, cursor, pointer);
28047 #endif
28048 }
28049
28050
28051 /* EXPORT:
28052 Take proper action when the mouse has moved to position X, Y on
28053 frame F with regards to highlighting portions of display that have
28054 mouse-face properties. Also de-highlight portions of display where
28055 the mouse was before, set the mouse pointer shape as appropriate
28056 for the mouse coordinates, and activate help echo (tooltips).
28057 X and Y can be negative or out of range. */
28058
28059 void
28060 note_mouse_highlight (struct frame *f, int x, int y)
28061 {
28062 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28063 enum window_part part = ON_NOTHING;
28064 Lisp_Object window;
28065 struct window *w;
28066 Cursor cursor = No_Cursor;
28067 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
28068 struct buffer *b;
28069
28070 /* When a menu is active, don't highlight because this looks odd. */
28071 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
28072 if (popup_activated ())
28073 return;
28074 #endif
28075
28076 if (!f->glyphs_initialized_p
28077 || f->pointer_invisible)
28078 return;
28079
28080 hlinfo->mouse_face_mouse_x = x;
28081 hlinfo->mouse_face_mouse_y = y;
28082 hlinfo->mouse_face_mouse_frame = f;
28083
28084 if (hlinfo->mouse_face_defer)
28085 return;
28086
28087 /* Which window is that in? */
28088 window = window_from_coordinates (f, x, y, &part, 1);
28089
28090 /* If displaying active text in another window, clear that. */
28091 if (! EQ (window, hlinfo->mouse_face_window)
28092 /* Also clear if we move out of text area in same window. */
28093 || (!NILP (hlinfo->mouse_face_window)
28094 && !NILP (window)
28095 && part != ON_TEXT
28096 && part != ON_MODE_LINE
28097 && part != ON_HEADER_LINE))
28098 clear_mouse_face (hlinfo);
28099
28100 /* Not on a window -> return. */
28101 if (!WINDOWP (window))
28102 return;
28103
28104 /* Reset help_echo_string. It will get recomputed below. */
28105 help_echo_string = Qnil;
28106
28107 /* Convert to window-relative pixel coordinates. */
28108 w = XWINDOW (window);
28109 frame_to_window_pixel_xy (w, &x, &y);
28110
28111 #ifdef HAVE_WINDOW_SYSTEM
28112 /* Handle tool-bar window differently since it doesn't display a
28113 buffer. */
28114 if (EQ (window, f->tool_bar_window))
28115 {
28116 note_tool_bar_highlight (f, x, y);
28117 return;
28118 }
28119 #endif
28120
28121 /* Mouse is on the mode, header line or margin? */
28122 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
28123 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
28124 {
28125 note_mode_line_or_margin_highlight (window, x, y, part);
28126 return;
28127 }
28128
28129 #ifdef HAVE_WINDOW_SYSTEM
28130 if (part == ON_VERTICAL_BORDER)
28131 {
28132 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
28133 help_echo_string = build_string ("drag-mouse-1: resize");
28134 }
28135 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
28136 || part == ON_SCROLL_BAR)
28137 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28138 else
28139 cursor = FRAME_X_OUTPUT (f)->text_cursor;
28140 #endif
28141
28142 /* Are we in a window whose display is up to date?
28143 And verify the buffer's text has not changed. */
28144 b = XBUFFER (w->contents);
28145 if (part == ON_TEXT
28146 && w->window_end_valid
28147 && w->last_modified == BUF_MODIFF (b)
28148 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
28149 {
28150 int hpos, vpos, dx, dy, area = LAST_AREA;
28151 ptrdiff_t pos;
28152 struct glyph *glyph;
28153 Lisp_Object object;
28154 Lisp_Object mouse_face = Qnil, position;
28155 Lisp_Object *overlay_vec = NULL;
28156 ptrdiff_t i, noverlays;
28157 struct buffer *obuf;
28158 ptrdiff_t obegv, ozv;
28159 int same_region;
28160
28161 /* Find the glyph under X/Y. */
28162 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28163
28164 #ifdef HAVE_WINDOW_SYSTEM
28165 /* Look for :pointer property on image. */
28166 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28167 {
28168 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28169 if (img != NULL && IMAGEP (img->spec))
28170 {
28171 Lisp_Object image_map, hotspot;
28172 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28173 !NILP (image_map))
28174 && (hotspot = find_hot_spot (image_map,
28175 glyph->slice.img.x + dx,
28176 glyph->slice.img.y + dy),
28177 CONSP (hotspot))
28178 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28179 {
28180 Lisp_Object plist;
28181
28182 /* Could check XCAR (hotspot) to see if we enter/leave
28183 this hot-spot.
28184 If so, we could look for mouse-enter, mouse-leave
28185 properties in PLIST (and do something...). */
28186 hotspot = XCDR (hotspot);
28187 if (CONSP (hotspot)
28188 && (plist = XCAR (hotspot), CONSP (plist)))
28189 {
28190 pointer = Fplist_get (plist, Qpointer);
28191 if (NILP (pointer))
28192 pointer = Qhand;
28193 help_echo_string = Fplist_get (plist, Qhelp_echo);
28194 if (!NILP (help_echo_string))
28195 {
28196 help_echo_window = window;
28197 help_echo_object = glyph->object;
28198 help_echo_pos = glyph->charpos;
28199 }
28200 }
28201 }
28202 if (NILP (pointer))
28203 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28204 }
28205 }
28206 #endif /* HAVE_WINDOW_SYSTEM */
28207
28208 /* Clear mouse face if X/Y not over text. */
28209 if (glyph == NULL
28210 || area != TEXT_AREA
28211 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
28212 /* Glyph's OBJECT is an integer for glyphs inserted by the
28213 display engine for its internal purposes, like truncation
28214 and continuation glyphs and blanks beyond the end of
28215 line's text on text terminals. If we are over such a
28216 glyph, we are not over any text. */
28217 || INTEGERP (glyph->object)
28218 /* R2L rows have a stretch glyph at their front, which
28219 stands for no text, whereas L2R rows have no glyphs at
28220 all beyond the end of text. Treat such stretch glyphs
28221 like we do with NULL glyphs in L2R rows. */
28222 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28223 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
28224 && glyph->type == STRETCH_GLYPH
28225 && glyph->avoid_cursor_p))
28226 {
28227 if (clear_mouse_face (hlinfo))
28228 cursor = No_Cursor;
28229 #ifdef HAVE_WINDOW_SYSTEM
28230 if (FRAME_WINDOW_P (f) && NILP (pointer))
28231 {
28232 if (area != TEXT_AREA)
28233 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28234 else
28235 pointer = Vvoid_text_area_pointer;
28236 }
28237 #endif
28238 goto set_cursor;
28239 }
28240
28241 pos = glyph->charpos;
28242 object = glyph->object;
28243 if (!STRINGP (object) && !BUFFERP (object))
28244 goto set_cursor;
28245
28246 /* If we get an out-of-range value, return now; avoid an error. */
28247 if (BUFFERP (object) && pos > BUF_Z (b))
28248 goto set_cursor;
28249
28250 /* Make the window's buffer temporarily current for
28251 overlays_at and compute_char_face. */
28252 obuf = current_buffer;
28253 current_buffer = b;
28254 obegv = BEGV;
28255 ozv = ZV;
28256 BEGV = BEG;
28257 ZV = Z;
28258
28259 /* Is this char mouse-active or does it have help-echo? */
28260 position = make_number (pos);
28261
28262 if (BUFFERP (object))
28263 {
28264 /* Put all the overlays we want in a vector in overlay_vec. */
28265 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28266 /* Sort overlays into increasing priority order. */
28267 noverlays = sort_overlays (overlay_vec, noverlays, w);
28268 }
28269 else
28270 noverlays = 0;
28271
28272 if (NILP (Vmouse_highlight))
28273 {
28274 clear_mouse_face (hlinfo);
28275 goto check_help_echo;
28276 }
28277
28278 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28279
28280 if (same_region)
28281 cursor = No_Cursor;
28282
28283 /* Check mouse-face highlighting. */
28284 if (! same_region
28285 /* If there exists an overlay with mouse-face overlapping
28286 the one we are currently highlighting, we have to
28287 check if we enter the overlapping overlay, and then
28288 highlight only that. */
28289 || (OVERLAYP (hlinfo->mouse_face_overlay)
28290 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28291 {
28292 /* Find the highest priority overlay with a mouse-face. */
28293 Lisp_Object overlay = Qnil;
28294 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28295 {
28296 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28297 if (!NILP (mouse_face))
28298 overlay = overlay_vec[i];
28299 }
28300
28301 /* If we're highlighting the same overlay as before, there's
28302 no need to do that again. */
28303 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28304 goto check_help_echo;
28305 hlinfo->mouse_face_overlay = overlay;
28306
28307 /* Clear the display of the old active region, if any. */
28308 if (clear_mouse_face (hlinfo))
28309 cursor = No_Cursor;
28310
28311 /* If no overlay applies, get a text property. */
28312 if (NILP (overlay))
28313 mouse_face = Fget_text_property (position, Qmouse_face, object);
28314
28315 /* Next, compute the bounds of the mouse highlighting and
28316 display it. */
28317 if (!NILP (mouse_face) && STRINGP (object))
28318 {
28319 /* The mouse-highlighting comes from a display string
28320 with a mouse-face. */
28321 Lisp_Object s, e;
28322 ptrdiff_t ignore;
28323
28324 s = Fprevious_single_property_change
28325 (make_number (pos + 1), Qmouse_face, object, Qnil);
28326 e = Fnext_single_property_change
28327 (position, Qmouse_face, object, Qnil);
28328 if (NILP (s))
28329 s = make_number (0);
28330 if (NILP (e))
28331 e = make_number (SCHARS (object) - 1);
28332 mouse_face_from_string_pos (w, hlinfo, object,
28333 XINT (s), XINT (e));
28334 hlinfo->mouse_face_past_end = 0;
28335 hlinfo->mouse_face_window = window;
28336 hlinfo->mouse_face_face_id
28337 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28338 glyph->face_id, 1);
28339 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28340 cursor = No_Cursor;
28341 }
28342 else
28343 {
28344 /* The mouse-highlighting, if any, comes from an overlay
28345 or text property in the buffer. */
28346 Lisp_Object buffer IF_LINT (= Qnil);
28347 Lisp_Object disp_string IF_LINT (= Qnil);
28348
28349 if (STRINGP (object))
28350 {
28351 /* If we are on a display string with no mouse-face,
28352 check if the text under it has one. */
28353 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28354 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28355 pos = string_buffer_position (object, start);
28356 if (pos > 0)
28357 {
28358 mouse_face = get_char_property_and_overlay
28359 (make_number (pos), Qmouse_face, w->contents, &overlay);
28360 buffer = w->contents;
28361 disp_string = object;
28362 }
28363 }
28364 else
28365 {
28366 buffer = object;
28367 disp_string = Qnil;
28368 }
28369
28370 if (!NILP (mouse_face))
28371 {
28372 Lisp_Object before, after;
28373 Lisp_Object before_string, after_string;
28374 /* To correctly find the limits of mouse highlight
28375 in a bidi-reordered buffer, we must not use the
28376 optimization of limiting the search in
28377 previous-single-property-change and
28378 next-single-property-change, because
28379 rows_from_pos_range needs the real start and end
28380 positions to DTRT in this case. That's because
28381 the first row visible in a window does not
28382 necessarily display the character whose position
28383 is the smallest. */
28384 Lisp_Object lim1 =
28385 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28386 ? Fmarker_position (w->start)
28387 : Qnil;
28388 Lisp_Object lim2 =
28389 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28390 ? make_number (BUF_Z (XBUFFER (buffer))
28391 - XFASTINT (w->window_end_pos))
28392 : Qnil;
28393
28394 if (NILP (overlay))
28395 {
28396 /* Handle the text property case. */
28397 before = Fprevious_single_property_change
28398 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28399 after = Fnext_single_property_change
28400 (make_number (pos), Qmouse_face, buffer, lim2);
28401 before_string = after_string = Qnil;
28402 }
28403 else
28404 {
28405 /* Handle the overlay case. */
28406 before = Foverlay_start (overlay);
28407 after = Foverlay_end (overlay);
28408 before_string = Foverlay_get (overlay, Qbefore_string);
28409 after_string = Foverlay_get (overlay, Qafter_string);
28410
28411 if (!STRINGP (before_string)) before_string = Qnil;
28412 if (!STRINGP (after_string)) after_string = Qnil;
28413 }
28414
28415 mouse_face_from_buffer_pos (window, hlinfo, pos,
28416 NILP (before)
28417 ? 1
28418 : XFASTINT (before),
28419 NILP (after)
28420 ? BUF_Z (XBUFFER (buffer))
28421 : XFASTINT (after),
28422 before_string, after_string,
28423 disp_string);
28424 cursor = No_Cursor;
28425 }
28426 }
28427 }
28428
28429 check_help_echo:
28430
28431 /* Look for a `help-echo' property. */
28432 if (NILP (help_echo_string)) {
28433 Lisp_Object help, overlay;
28434
28435 /* Check overlays first. */
28436 help = overlay = Qnil;
28437 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28438 {
28439 overlay = overlay_vec[i];
28440 help = Foverlay_get (overlay, Qhelp_echo);
28441 }
28442
28443 if (!NILP (help))
28444 {
28445 help_echo_string = help;
28446 help_echo_window = window;
28447 help_echo_object = overlay;
28448 help_echo_pos = pos;
28449 }
28450 else
28451 {
28452 Lisp_Object obj = glyph->object;
28453 ptrdiff_t charpos = glyph->charpos;
28454
28455 /* Try text properties. */
28456 if (STRINGP (obj)
28457 && charpos >= 0
28458 && charpos < SCHARS (obj))
28459 {
28460 help = Fget_text_property (make_number (charpos),
28461 Qhelp_echo, obj);
28462 if (NILP (help))
28463 {
28464 /* If the string itself doesn't specify a help-echo,
28465 see if the buffer text ``under'' it does. */
28466 struct glyph_row *r
28467 = MATRIX_ROW (w->current_matrix, vpos);
28468 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28469 ptrdiff_t p = string_buffer_position (obj, start);
28470 if (p > 0)
28471 {
28472 help = Fget_char_property (make_number (p),
28473 Qhelp_echo, w->contents);
28474 if (!NILP (help))
28475 {
28476 charpos = p;
28477 obj = w->contents;
28478 }
28479 }
28480 }
28481 }
28482 else if (BUFFERP (obj)
28483 && charpos >= BEGV
28484 && charpos < ZV)
28485 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28486 obj);
28487
28488 if (!NILP (help))
28489 {
28490 help_echo_string = help;
28491 help_echo_window = window;
28492 help_echo_object = obj;
28493 help_echo_pos = charpos;
28494 }
28495 }
28496 }
28497
28498 #ifdef HAVE_WINDOW_SYSTEM
28499 /* Look for a `pointer' property. */
28500 if (FRAME_WINDOW_P (f) && NILP (pointer))
28501 {
28502 /* Check overlays first. */
28503 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28504 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28505
28506 if (NILP (pointer))
28507 {
28508 Lisp_Object obj = glyph->object;
28509 ptrdiff_t charpos = glyph->charpos;
28510
28511 /* Try text properties. */
28512 if (STRINGP (obj)
28513 && charpos >= 0
28514 && charpos < SCHARS (obj))
28515 {
28516 pointer = Fget_text_property (make_number (charpos),
28517 Qpointer, obj);
28518 if (NILP (pointer))
28519 {
28520 /* If the string itself doesn't specify a pointer,
28521 see if the buffer text ``under'' it does. */
28522 struct glyph_row *r
28523 = MATRIX_ROW (w->current_matrix, vpos);
28524 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28525 ptrdiff_t p = string_buffer_position (obj, start);
28526 if (p > 0)
28527 pointer = Fget_char_property (make_number (p),
28528 Qpointer, w->contents);
28529 }
28530 }
28531 else if (BUFFERP (obj)
28532 && charpos >= BEGV
28533 && charpos < ZV)
28534 pointer = Fget_text_property (make_number (charpos),
28535 Qpointer, obj);
28536 }
28537 }
28538 #endif /* HAVE_WINDOW_SYSTEM */
28539
28540 BEGV = obegv;
28541 ZV = ozv;
28542 current_buffer = obuf;
28543 }
28544
28545 set_cursor:
28546
28547 #ifdef HAVE_WINDOW_SYSTEM
28548 if (FRAME_WINDOW_P (f))
28549 define_frame_cursor1 (f, cursor, pointer);
28550 #else
28551 /* This is here to prevent a compiler error, about "label at end of
28552 compound statement". */
28553 return;
28554 #endif
28555 }
28556
28557
28558 /* EXPORT for RIF:
28559 Clear any mouse-face on window W. This function is part of the
28560 redisplay interface, and is called from try_window_id and similar
28561 functions to ensure the mouse-highlight is off. */
28562
28563 void
28564 x_clear_window_mouse_face (struct window *w)
28565 {
28566 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28567 Lisp_Object window;
28568
28569 block_input ();
28570 XSETWINDOW (window, w);
28571 if (EQ (window, hlinfo->mouse_face_window))
28572 clear_mouse_face (hlinfo);
28573 unblock_input ();
28574 }
28575
28576
28577 /* EXPORT:
28578 Just discard the mouse face information for frame F, if any.
28579 This is used when the size of F is changed. */
28580
28581 void
28582 cancel_mouse_face (struct frame *f)
28583 {
28584 Lisp_Object window;
28585 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28586
28587 window = hlinfo->mouse_face_window;
28588 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28589 {
28590 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28591 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28592 hlinfo->mouse_face_window = Qnil;
28593 }
28594 }
28595
28596
28597 \f
28598 /***********************************************************************
28599 Exposure Events
28600 ***********************************************************************/
28601
28602 #ifdef HAVE_WINDOW_SYSTEM
28603
28604 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28605 which intersects rectangle R. R is in window-relative coordinates. */
28606
28607 static void
28608 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28609 enum glyph_row_area area)
28610 {
28611 struct glyph *first = row->glyphs[area];
28612 struct glyph *end = row->glyphs[area] + row->used[area];
28613 struct glyph *last;
28614 int first_x, start_x, x;
28615
28616 if (area == TEXT_AREA && row->fill_line_p)
28617 /* If row extends face to end of line write the whole line. */
28618 draw_glyphs (w, 0, row, area,
28619 0, row->used[area],
28620 DRAW_NORMAL_TEXT, 0);
28621 else
28622 {
28623 /* Set START_X to the window-relative start position for drawing glyphs of
28624 AREA. The first glyph of the text area can be partially visible.
28625 The first glyphs of other areas cannot. */
28626 start_x = window_box_left_offset (w, area);
28627 x = start_x;
28628 if (area == TEXT_AREA)
28629 x += row->x;
28630
28631 /* Find the first glyph that must be redrawn. */
28632 while (first < end
28633 && x + first->pixel_width < r->x)
28634 {
28635 x += first->pixel_width;
28636 ++first;
28637 }
28638
28639 /* Find the last one. */
28640 last = first;
28641 first_x = x;
28642 while (last < end
28643 && x < r->x + r->width)
28644 {
28645 x += last->pixel_width;
28646 ++last;
28647 }
28648
28649 /* Repaint. */
28650 if (last > first)
28651 draw_glyphs (w, first_x - start_x, row, area,
28652 first - row->glyphs[area], last - row->glyphs[area],
28653 DRAW_NORMAL_TEXT, 0);
28654 }
28655 }
28656
28657
28658 /* Redraw the parts of the glyph row ROW on window W intersecting
28659 rectangle R. R is in window-relative coordinates. Value is
28660 non-zero if mouse-face was overwritten. */
28661
28662 static int
28663 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28664 {
28665 eassert (row->enabled_p);
28666
28667 if (row->mode_line_p || w->pseudo_window_p)
28668 draw_glyphs (w, 0, row, TEXT_AREA,
28669 0, row->used[TEXT_AREA],
28670 DRAW_NORMAL_TEXT, 0);
28671 else
28672 {
28673 if (row->used[LEFT_MARGIN_AREA])
28674 expose_area (w, row, r, LEFT_MARGIN_AREA);
28675 if (row->used[TEXT_AREA])
28676 expose_area (w, row, r, TEXT_AREA);
28677 if (row->used[RIGHT_MARGIN_AREA])
28678 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28679 draw_row_fringe_bitmaps (w, row);
28680 }
28681
28682 return row->mouse_face_p;
28683 }
28684
28685
28686 /* Redraw those parts of glyphs rows during expose event handling that
28687 overlap other rows. Redrawing of an exposed line writes over parts
28688 of lines overlapping that exposed line; this function fixes that.
28689
28690 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28691 row in W's current matrix that is exposed and overlaps other rows.
28692 LAST_OVERLAPPING_ROW is the last such row. */
28693
28694 static void
28695 expose_overlaps (struct window *w,
28696 struct glyph_row *first_overlapping_row,
28697 struct glyph_row *last_overlapping_row,
28698 XRectangle *r)
28699 {
28700 struct glyph_row *row;
28701
28702 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28703 if (row->overlapping_p)
28704 {
28705 eassert (row->enabled_p && !row->mode_line_p);
28706
28707 row->clip = r;
28708 if (row->used[LEFT_MARGIN_AREA])
28709 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28710
28711 if (row->used[TEXT_AREA])
28712 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28713
28714 if (row->used[RIGHT_MARGIN_AREA])
28715 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28716 row->clip = NULL;
28717 }
28718 }
28719
28720
28721 /* Return non-zero if W's cursor intersects rectangle R. */
28722
28723 static int
28724 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28725 {
28726 XRectangle cr, result;
28727 struct glyph *cursor_glyph;
28728 struct glyph_row *row;
28729
28730 if (w->phys_cursor.vpos >= 0
28731 && w->phys_cursor.vpos < w->current_matrix->nrows
28732 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28733 row->enabled_p)
28734 && row->cursor_in_fringe_p)
28735 {
28736 /* Cursor is in the fringe. */
28737 cr.x = window_box_right_offset (w,
28738 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28739 ? RIGHT_MARGIN_AREA
28740 : TEXT_AREA));
28741 cr.y = row->y;
28742 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28743 cr.height = row->height;
28744 return x_intersect_rectangles (&cr, r, &result);
28745 }
28746
28747 cursor_glyph = get_phys_cursor_glyph (w);
28748 if (cursor_glyph)
28749 {
28750 /* r is relative to W's box, but w->phys_cursor.x is relative
28751 to left edge of W's TEXT area. Adjust it. */
28752 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28753 cr.y = w->phys_cursor.y;
28754 cr.width = cursor_glyph->pixel_width;
28755 cr.height = w->phys_cursor_height;
28756 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28757 I assume the effect is the same -- and this is portable. */
28758 return x_intersect_rectangles (&cr, r, &result);
28759 }
28760 /* If we don't understand the format, pretend we're not in the hot-spot. */
28761 return 0;
28762 }
28763
28764
28765 /* EXPORT:
28766 Draw a vertical window border to the right of window W if W doesn't
28767 have vertical scroll bars. */
28768
28769 void
28770 x_draw_vertical_border (struct window *w)
28771 {
28772 struct frame *f = XFRAME (WINDOW_FRAME (w));
28773
28774 /* We could do better, if we knew what type of scroll-bar the adjacent
28775 windows (on either side) have... But we don't :-(
28776 However, I think this works ok. ++KFS 2003-04-25 */
28777
28778 /* Redraw borders between horizontally adjacent windows. Don't
28779 do it for frames with vertical scroll bars because either the
28780 right scroll bar of a window, or the left scroll bar of its
28781 neighbor will suffice as a border. */
28782 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28783 return;
28784
28785 /* Note: It is necessary to redraw both the left and the right
28786 borders, for when only this single window W is being
28787 redisplayed. */
28788 if (!WINDOW_RIGHTMOST_P (w)
28789 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28790 {
28791 int x0, x1, y0, y1;
28792
28793 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28794 y1 -= 1;
28795
28796 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28797 x1 -= 1;
28798
28799 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28800 }
28801 if (!WINDOW_LEFTMOST_P (w)
28802 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28803 {
28804 int x0, x1, y0, y1;
28805
28806 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28807 y1 -= 1;
28808
28809 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28810 x0 -= 1;
28811
28812 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28813 }
28814 }
28815
28816
28817 /* Redraw the part of window W intersection rectangle FR. Pixel
28818 coordinates in FR are frame-relative. Call this function with
28819 input blocked. Value is non-zero if the exposure overwrites
28820 mouse-face. */
28821
28822 static int
28823 expose_window (struct window *w, XRectangle *fr)
28824 {
28825 struct frame *f = XFRAME (w->frame);
28826 XRectangle wr, r;
28827 int mouse_face_overwritten_p = 0;
28828
28829 /* If window is not yet fully initialized, do nothing. This can
28830 happen when toolkit scroll bars are used and a window is split.
28831 Reconfiguring the scroll bar will generate an expose for a newly
28832 created window. */
28833 if (w->current_matrix == NULL)
28834 return 0;
28835
28836 /* When we're currently updating the window, display and current
28837 matrix usually don't agree. Arrange for a thorough display
28838 later. */
28839 if (w == updated_window)
28840 {
28841 SET_FRAME_GARBAGED (f);
28842 return 0;
28843 }
28844
28845 /* Frame-relative pixel rectangle of W. */
28846 wr.x = WINDOW_LEFT_EDGE_X (w);
28847 wr.y = WINDOW_TOP_EDGE_Y (w);
28848 wr.width = WINDOW_TOTAL_WIDTH (w);
28849 wr.height = WINDOW_TOTAL_HEIGHT (w);
28850
28851 if (x_intersect_rectangles (fr, &wr, &r))
28852 {
28853 int yb = window_text_bottom_y (w);
28854 struct glyph_row *row;
28855 int cursor_cleared_p, phys_cursor_on_p;
28856 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28857
28858 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28859 r.x, r.y, r.width, r.height));
28860
28861 /* Convert to window coordinates. */
28862 r.x -= WINDOW_LEFT_EDGE_X (w);
28863 r.y -= WINDOW_TOP_EDGE_Y (w);
28864
28865 /* Turn off the cursor. */
28866 if (!w->pseudo_window_p
28867 && phys_cursor_in_rect_p (w, &r))
28868 {
28869 x_clear_cursor (w);
28870 cursor_cleared_p = 1;
28871 }
28872 else
28873 cursor_cleared_p = 0;
28874
28875 /* If the row containing the cursor extends face to end of line,
28876 then expose_area might overwrite the cursor outside the
28877 rectangle and thus notice_overwritten_cursor might clear
28878 w->phys_cursor_on_p. We remember the original value and
28879 check later if it is changed. */
28880 phys_cursor_on_p = w->phys_cursor_on_p;
28881
28882 /* Update lines intersecting rectangle R. */
28883 first_overlapping_row = last_overlapping_row = NULL;
28884 for (row = w->current_matrix->rows;
28885 row->enabled_p;
28886 ++row)
28887 {
28888 int y0 = row->y;
28889 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28890
28891 if ((y0 >= r.y && y0 < r.y + r.height)
28892 || (y1 > r.y && y1 < r.y + r.height)
28893 || (r.y >= y0 && r.y < y1)
28894 || (r.y + r.height > y0 && r.y + r.height < y1))
28895 {
28896 /* A header line may be overlapping, but there is no need
28897 to fix overlapping areas for them. KFS 2005-02-12 */
28898 if (row->overlapping_p && !row->mode_line_p)
28899 {
28900 if (first_overlapping_row == NULL)
28901 first_overlapping_row = row;
28902 last_overlapping_row = row;
28903 }
28904
28905 row->clip = fr;
28906 if (expose_line (w, row, &r))
28907 mouse_face_overwritten_p = 1;
28908 row->clip = NULL;
28909 }
28910 else if (row->overlapping_p)
28911 {
28912 /* We must redraw a row overlapping the exposed area. */
28913 if (y0 < r.y
28914 ? y0 + row->phys_height > r.y
28915 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28916 {
28917 if (first_overlapping_row == NULL)
28918 first_overlapping_row = row;
28919 last_overlapping_row = row;
28920 }
28921 }
28922
28923 if (y1 >= yb)
28924 break;
28925 }
28926
28927 /* Display the mode line if there is one. */
28928 if (WINDOW_WANTS_MODELINE_P (w)
28929 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28930 row->enabled_p)
28931 && row->y < r.y + r.height)
28932 {
28933 if (expose_line (w, row, &r))
28934 mouse_face_overwritten_p = 1;
28935 }
28936
28937 if (!w->pseudo_window_p)
28938 {
28939 /* Fix the display of overlapping rows. */
28940 if (first_overlapping_row)
28941 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28942 fr);
28943
28944 /* Draw border between windows. */
28945 x_draw_vertical_border (w);
28946
28947 /* Turn the cursor on again. */
28948 if (cursor_cleared_p
28949 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28950 update_window_cursor (w, 1);
28951 }
28952 }
28953
28954 return mouse_face_overwritten_p;
28955 }
28956
28957
28958
28959 /* Redraw (parts) of all windows in the window tree rooted at W that
28960 intersect R. R contains frame pixel coordinates. Value is
28961 non-zero if the exposure overwrites mouse-face. */
28962
28963 static int
28964 expose_window_tree (struct window *w, XRectangle *r)
28965 {
28966 struct frame *f = XFRAME (w->frame);
28967 int mouse_face_overwritten_p = 0;
28968
28969 while (w && !FRAME_GARBAGED_P (f))
28970 {
28971 if (WINDOWP (w->contents))
28972 mouse_face_overwritten_p
28973 |= expose_window_tree (XWINDOW (w->contents), r);
28974 else
28975 mouse_face_overwritten_p |= expose_window (w, r);
28976
28977 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28978 }
28979
28980 return mouse_face_overwritten_p;
28981 }
28982
28983
28984 /* EXPORT:
28985 Redisplay an exposed area of frame F. X and Y are the upper-left
28986 corner of the exposed rectangle. W and H are width and height of
28987 the exposed area. All are pixel values. W or H zero means redraw
28988 the entire frame. */
28989
28990 void
28991 expose_frame (struct frame *f, int x, int y, int w, int h)
28992 {
28993 XRectangle r;
28994 int mouse_face_overwritten_p = 0;
28995
28996 TRACE ((stderr, "expose_frame "));
28997
28998 /* No need to redraw if frame will be redrawn soon. */
28999 if (FRAME_GARBAGED_P (f))
29000 {
29001 TRACE ((stderr, " garbaged\n"));
29002 return;
29003 }
29004
29005 /* If basic faces haven't been realized yet, there is no point in
29006 trying to redraw anything. This can happen when we get an expose
29007 event while Emacs is starting, e.g. by moving another window. */
29008 if (FRAME_FACE_CACHE (f) == NULL
29009 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
29010 {
29011 TRACE ((stderr, " no faces\n"));
29012 return;
29013 }
29014
29015 if (w == 0 || h == 0)
29016 {
29017 r.x = r.y = 0;
29018 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
29019 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
29020 }
29021 else
29022 {
29023 r.x = x;
29024 r.y = y;
29025 r.width = w;
29026 r.height = h;
29027 }
29028
29029 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
29030 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
29031
29032 if (WINDOWP (f->tool_bar_window))
29033 mouse_face_overwritten_p
29034 |= expose_window (XWINDOW (f->tool_bar_window), &r);
29035
29036 #ifdef HAVE_X_WINDOWS
29037 #ifndef MSDOS
29038 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
29039 if (WINDOWP (f->menu_bar_window))
29040 mouse_face_overwritten_p
29041 |= expose_window (XWINDOW (f->menu_bar_window), &r);
29042 #endif /* not USE_X_TOOLKIT and not USE_GTK */
29043 #endif
29044 #endif
29045
29046 /* Some window managers support a focus-follows-mouse style with
29047 delayed raising of frames. Imagine a partially obscured frame,
29048 and moving the mouse into partially obscured mouse-face on that
29049 frame. The visible part of the mouse-face will be highlighted,
29050 then the WM raises the obscured frame. With at least one WM, KDE
29051 2.1, Emacs is not getting any event for the raising of the frame
29052 (even tried with SubstructureRedirectMask), only Expose events.
29053 These expose events will draw text normally, i.e. not
29054 highlighted. Which means we must redo the highlight here.
29055 Subsume it under ``we love X''. --gerd 2001-08-15 */
29056 /* Included in Windows version because Windows most likely does not
29057 do the right thing if any third party tool offers
29058 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
29059 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
29060 {
29061 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
29062 if (f == hlinfo->mouse_face_mouse_frame)
29063 {
29064 int mouse_x = hlinfo->mouse_face_mouse_x;
29065 int mouse_y = hlinfo->mouse_face_mouse_y;
29066 clear_mouse_face (hlinfo);
29067 note_mouse_highlight (f, mouse_x, mouse_y);
29068 }
29069 }
29070 }
29071
29072
29073 /* EXPORT:
29074 Determine the intersection of two rectangles R1 and R2. Return
29075 the intersection in *RESULT. Value is non-zero if RESULT is not
29076 empty. */
29077
29078 int
29079 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
29080 {
29081 XRectangle *left, *right;
29082 XRectangle *upper, *lower;
29083 int intersection_p = 0;
29084
29085 /* Rearrange so that R1 is the left-most rectangle. */
29086 if (r1->x < r2->x)
29087 left = r1, right = r2;
29088 else
29089 left = r2, right = r1;
29090
29091 /* X0 of the intersection is right.x0, if this is inside R1,
29092 otherwise there is no intersection. */
29093 if (right->x <= left->x + left->width)
29094 {
29095 result->x = right->x;
29096
29097 /* The right end of the intersection is the minimum of
29098 the right ends of left and right. */
29099 result->width = (min (left->x + left->width, right->x + right->width)
29100 - result->x);
29101
29102 /* Same game for Y. */
29103 if (r1->y < r2->y)
29104 upper = r1, lower = r2;
29105 else
29106 upper = r2, lower = r1;
29107
29108 /* The upper end of the intersection is lower.y0, if this is inside
29109 of upper. Otherwise, there is no intersection. */
29110 if (lower->y <= upper->y + upper->height)
29111 {
29112 result->y = lower->y;
29113
29114 /* The lower end of the intersection is the minimum of the lower
29115 ends of upper and lower. */
29116 result->height = (min (lower->y + lower->height,
29117 upper->y + upper->height)
29118 - result->y);
29119 intersection_p = 1;
29120 }
29121 }
29122
29123 return intersection_p;
29124 }
29125
29126 #endif /* HAVE_WINDOW_SYSTEM */
29127
29128 \f
29129 /***********************************************************************
29130 Initialization
29131 ***********************************************************************/
29132
29133 void
29134 syms_of_xdisp (void)
29135 {
29136 Vwith_echo_area_save_vector = Qnil;
29137 staticpro (&Vwith_echo_area_save_vector);
29138
29139 Vmessage_stack = Qnil;
29140 staticpro (&Vmessage_stack);
29141
29142 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
29143 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
29144
29145 message_dolog_marker1 = Fmake_marker ();
29146 staticpro (&message_dolog_marker1);
29147 message_dolog_marker2 = Fmake_marker ();
29148 staticpro (&message_dolog_marker2);
29149 message_dolog_marker3 = Fmake_marker ();
29150 staticpro (&message_dolog_marker3);
29151
29152 #ifdef GLYPH_DEBUG
29153 defsubr (&Sdump_frame_glyph_matrix);
29154 defsubr (&Sdump_glyph_matrix);
29155 defsubr (&Sdump_glyph_row);
29156 defsubr (&Sdump_tool_bar_row);
29157 defsubr (&Strace_redisplay);
29158 defsubr (&Strace_to_stderr);
29159 #endif
29160 #ifdef HAVE_WINDOW_SYSTEM
29161 defsubr (&Stool_bar_lines_needed);
29162 defsubr (&Slookup_image_map);
29163 #endif
29164 defsubr (&Sline_pixel_height);
29165 defsubr (&Sformat_mode_line);
29166 defsubr (&Sinvisible_p);
29167 defsubr (&Scurrent_bidi_paragraph_direction);
29168 defsubr (&Smove_point_visually);
29169
29170 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29171 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29172 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29173 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29174 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29175 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29176 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29177 DEFSYM (Qeval, "eval");
29178 DEFSYM (QCdata, ":data");
29179 DEFSYM (Qdisplay, "display");
29180 DEFSYM (Qspace_width, "space-width");
29181 DEFSYM (Qraise, "raise");
29182 DEFSYM (Qslice, "slice");
29183 DEFSYM (Qspace, "space");
29184 DEFSYM (Qmargin, "margin");
29185 DEFSYM (Qpointer, "pointer");
29186 DEFSYM (Qleft_margin, "left-margin");
29187 DEFSYM (Qright_margin, "right-margin");
29188 DEFSYM (Qcenter, "center");
29189 DEFSYM (Qline_height, "line-height");
29190 DEFSYM (QCalign_to, ":align-to");
29191 DEFSYM (QCrelative_width, ":relative-width");
29192 DEFSYM (QCrelative_height, ":relative-height");
29193 DEFSYM (QCeval, ":eval");
29194 DEFSYM (QCpropertize, ":propertize");
29195 DEFSYM (QCfile, ":file");
29196 DEFSYM (Qfontified, "fontified");
29197 DEFSYM (Qfontification_functions, "fontification-functions");
29198 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29199 DEFSYM (Qescape_glyph, "escape-glyph");
29200 DEFSYM (Qnobreak_space, "nobreak-space");
29201 DEFSYM (Qimage, "image");
29202 DEFSYM (Qtext, "text");
29203 DEFSYM (Qboth, "both");
29204 DEFSYM (Qboth_horiz, "both-horiz");
29205 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29206 DEFSYM (QCmap, ":map");
29207 DEFSYM (QCpointer, ":pointer");
29208 DEFSYM (Qrect, "rect");
29209 DEFSYM (Qcircle, "circle");
29210 DEFSYM (Qpoly, "poly");
29211 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29212 DEFSYM (Qgrow_only, "grow-only");
29213 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29214 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29215 DEFSYM (Qposition, "position");
29216 DEFSYM (Qbuffer_position, "buffer-position");
29217 DEFSYM (Qobject, "object");
29218 DEFSYM (Qbar, "bar");
29219 DEFSYM (Qhbar, "hbar");
29220 DEFSYM (Qbox, "box");
29221 DEFSYM (Qhollow, "hollow");
29222 DEFSYM (Qhand, "hand");
29223 DEFSYM (Qarrow, "arrow");
29224 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29225
29226 list_of_error = list1 (list2 (intern_c_string ("error"),
29227 intern_c_string ("void-variable")));
29228 staticpro (&list_of_error);
29229
29230 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29231 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29232 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29233 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29234
29235 echo_buffer[0] = echo_buffer[1] = Qnil;
29236 staticpro (&echo_buffer[0]);
29237 staticpro (&echo_buffer[1]);
29238
29239 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29240 staticpro (&echo_area_buffer[0]);
29241 staticpro (&echo_area_buffer[1]);
29242
29243 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29244 staticpro (&Vmessages_buffer_name);
29245
29246 mode_line_proptrans_alist = Qnil;
29247 staticpro (&mode_line_proptrans_alist);
29248 mode_line_string_list = Qnil;
29249 staticpro (&mode_line_string_list);
29250 mode_line_string_face = Qnil;
29251 staticpro (&mode_line_string_face);
29252 mode_line_string_face_prop = Qnil;
29253 staticpro (&mode_line_string_face_prop);
29254 Vmode_line_unwind_vector = Qnil;
29255 staticpro (&Vmode_line_unwind_vector);
29256
29257 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29258
29259 help_echo_string = Qnil;
29260 staticpro (&help_echo_string);
29261 help_echo_object = Qnil;
29262 staticpro (&help_echo_object);
29263 help_echo_window = Qnil;
29264 staticpro (&help_echo_window);
29265 previous_help_echo_string = Qnil;
29266 staticpro (&previous_help_echo_string);
29267 help_echo_pos = -1;
29268
29269 DEFSYM (Qright_to_left, "right-to-left");
29270 DEFSYM (Qleft_to_right, "left-to-right");
29271
29272 #ifdef HAVE_WINDOW_SYSTEM
29273 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29274 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29275 For example, if a block cursor is over a tab, it will be drawn as
29276 wide as that tab on the display. */);
29277 x_stretch_cursor_p = 0;
29278 #endif
29279
29280 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29281 doc: /* Non-nil means highlight trailing whitespace.
29282 The face used for trailing whitespace is `trailing-whitespace'. */);
29283 Vshow_trailing_whitespace = Qnil;
29284
29285 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29286 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29287 If the value is t, Emacs highlights non-ASCII chars which have the
29288 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29289 or `escape-glyph' face respectively.
29290
29291 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29292 U+2011 (non-breaking hyphen) are affected.
29293
29294 Any other non-nil value means to display these characters as a escape
29295 glyph followed by an ordinary space or hyphen.
29296
29297 A value of nil means no special handling of these characters. */);
29298 Vnobreak_char_display = Qt;
29299
29300 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29301 doc: /* The pointer shape to show in void text areas.
29302 A value of nil means to show the text pointer. Other options are `arrow',
29303 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29304 Vvoid_text_area_pointer = Qarrow;
29305
29306 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29307 doc: /* Non-nil means don't actually do any redisplay.
29308 This is used for internal purposes. */);
29309 Vinhibit_redisplay = Qnil;
29310
29311 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29312 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29313 Vglobal_mode_string = Qnil;
29314
29315 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29316 doc: /* Marker for where to display an arrow on top of the buffer text.
29317 This must be the beginning of a line in order to work.
29318 See also `overlay-arrow-string'. */);
29319 Voverlay_arrow_position = Qnil;
29320
29321 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29322 doc: /* String to display as an arrow in non-window frames.
29323 See also `overlay-arrow-position'. */);
29324 Voverlay_arrow_string = build_pure_c_string ("=>");
29325
29326 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29327 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29328 The symbols on this list are examined during redisplay to determine
29329 where to display overlay arrows. */);
29330 Voverlay_arrow_variable_list
29331 = list1 (intern_c_string ("overlay-arrow-position"));
29332
29333 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29334 doc: /* The number of lines to try scrolling a window by when point moves out.
29335 If that fails to bring point back on frame, point is centered instead.
29336 If this is zero, point is always centered after it moves off frame.
29337 If you want scrolling to always be a line at a time, you should set
29338 `scroll-conservatively' to a large value rather than set this to 1. */);
29339
29340 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29341 doc: /* Scroll up to this many lines, to bring point back on screen.
29342 If point moves off-screen, redisplay will scroll by up to
29343 `scroll-conservatively' lines in order to bring point just barely
29344 onto the screen again. If that cannot be done, then redisplay
29345 recenters point as usual.
29346
29347 If the value is greater than 100, redisplay will never recenter point,
29348 but will always scroll just enough text to bring point into view, even
29349 if you move far away.
29350
29351 A value of zero means always recenter point if it moves off screen. */);
29352 scroll_conservatively = 0;
29353
29354 DEFVAR_INT ("scroll-margin", scroll_margin,
29355 doc: /* Number of lines of margin at the top and bottom of a window.
29356 Recenter the window whenever point gets within this many lines
29357 of the top or bottom of the window. */);
29358 scroll_margin = 0;
29359
29360 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29361 doc: /* Pixels per inch value for non-window system displays.
29362 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29363 Vdisplay_pixels_per_inch = make_float (72.0);
29364
29365 #ifdef GLYPH_DEBUG
29366 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29367 #endif
29368
29369 DEFVAR_LISP ("truncate-partial-width-windows",
29370 Vtruncate_partial_width_windows,
29371 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29372 For an integer value, truncate lines in each window narrower than the
29373 full frame width, provided the window width is less than that integer;
29374 otherwise, respect the value of `truncate-lines'.
29375
29376 For any other non-nil value, truncate lines in all windows that do
29377 not span the full frame width.
29378
29379 A value of nil means to respect the value of `truncate-lines'.
29380
29381 If `word-wrap' is enabled, you might want to reduce this. */);
29382 Vtruncate_partial_width_windows = make_number (50);
29383
29384 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29385 doc: /* Maximum buffer size for which line number should be displayed.
29386 If the buffer is bigger than this, the line number does not appear
29387 in the mode line. A value of nil means no limit. */);
29388 Vline_number_display_limit = Qnil;
29389
29390 DEFVAR_INT ("line-number-display-limit-width",
29391 line_number_display_limit_width,
29392 doc: /* Maximum line width (in characters) for line number display.
29393 If the average length of the lines near point is bigger than this, then the
29394 line number may be omitted from the mode line. */);
29395 line_number_display_limit_width = 200;
29396
29397 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29398 doc: /* Non-nil means highlight region even in nonselected windows. */);
29399 highlight_nonselected_windows = 0;
29400
29401 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29402 doc: /* Non-nil if more than one frame is visible on this display.
29403 Minibuffer-only frames don't count, but iconified frames do.
29404 This variable is not guaranteed to be accurate except while processing
29405 `frame-title-format' and `icon-title-format'. */);
29406
29407 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29408 doc: /* Template for displaying the title bar of visible frames.
29409 \(Assuming the window manager supports this feature.)
29410
29411 This variable has the same structure as `mode-line-format', except that
29412 the %c and %l constructs are ignored. It is used only on frames for
29413 which no explicit name has been set \(see `modify-frame-parameters'). */);
29414
29415 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29416 doc: /* Template for displaying the title bar of an iconified frame.
29417 \(Assuming the window manager supports this feature.)
29418 This variable has the same structure as `mode-line-format' (which see),
29419 and is used only on frames for which no explicit name has been set
29420 \(see `modify-frame-parameters'). */);
29421 Vicon_title_format
29422 = Vframe_title_format
29423 = listn (CONSTYPE_PURE, 3,
29424 intern_c_string ("multiple-frames"),
29425 build_pure_c_string ("%b"),
29426 listn (CONSTYPE_PURE, 4,
29427 empty_unibyte_string,
29428 intern_c_string ("invocation-name"),
29429 build_pure_c_string ("@"),
29430 intern_c_string ("system-name")));
29431
29432 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29433 doc: /* Maximum number of lines to keep in the message log buffer.
29434 If nil, disable message logging. If t, log messages but don't truncate
29435 the buffer when it becomes large. */);
29436 Vmessage_log_max = make_number (1000);
29437
29438 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29439 doc: /* Functions called before redisplay, if window sizes have changed.
29440 The value should be a list of functions that take one argument.
29441 Just before redisplay, for each frame, if any of its windows have changed
29442 size since the last redisplay, or have been split or deleted,
29443 all the functions in the list are called, with the frame as argument. */);
29444 Vwindow_size_change_functions = Qnil;
29445
29446 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29447 doc: /* List of functions to call before redisplaying a window with scrolling.
29448 Each function is called with two arguments, the window and its new
29449 display-start position. Note that these functions are also called by
29450 `set-window-buffer'. Also note that the value of `window-end' is not
29451 valid when these functions are called.
29452
29453 Warning: Do not use this feature to alter the way the window
29454 is scrolled. It is not designed for that, and such use probably won't
29455 work. */);
29456 Vwindow_scroll_functions = Qnil;
29457
29458 DEFVAR_LISP ("window-text-change-functions",
29459 Vwindow_text_change_functions,
29460 doc: /* Functions to call in redisplay when text in the window might change. */);
29461 Vwindow_text_change_functions = Qnil;
29462
29463 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29464 doc: /* Functions called when redisplay of a window reaches the end trigger.
29465 Each function is called with two arguments, the window and the end trigger value.
29466 See `set-window-redisplay-end-trigger'. */);
29467 Vredisplay_end_trigger_functions = Qnil;
29468
29469 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29470 doc: /* Non-nil means autoselect window with mouse pointer.
29471 If nil, do not autoselect windows.
29472 A positive number means delay autoselection by that many seconds: a
29473 window is autoselected only after the mouse has remained in that
29474 window for the duration of the delay.
29475 A negative number has a similar effect, but causes windows to be
29476 autoselected only after the mouse has stopped moving. \(Because of
29477 the way Emacs compares mouse events, you will occasionally wait twice
29478 that time before the window gets selected.\)
29479 Any other value means to autoselect window instantaneously when the
29480 mouse pointer enters it.
29481
29482 Autoselection selects the minibuffer only if it is active, and never
29483 unselects the minibuffer if it is active.
29484
29485 When customizing this variable make sure that the actual value of
29486 `focus-follows-mouse' matches the behavior of your window manager. */);
29487 Vmouse_autoselect_window = Qnil;
29488
29489 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29490 doc: /* Non-nil means automatically resize tool-bars.
29491 This dynamically changes the tool-bar's height to the minimum height
29492 that is needed to make all tool-bar items visible.
29493 If value is `grow-only', the tool-bar's height is only increased
29494 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29495 Vauto_resize_tool_bars = Qt;
29496
29497 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29498 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29499 auto_raise_tool_bar_buttons_p = 1;
29500
29501 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29502 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29503 make_cursor_line_fully_visible_p = 1;
29504
29505 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29506 doc: /* Border below tool-bar in pixels.
29507 If an integer, use it as the height of the border.
29508 If it is one of `internal-border-width' or `border-width', use the
29509 value of the corresponding frame parameter.
29510 Otherwise, no border is added below the tool-bar. */);
29511 Vtool_bar_border = Qinternal_border_width;
29512
29513 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29514 doc: /* Margin around tool-bar buttons in pixels.
29515 If an integer, use that for both horizontal and vertical margins.
29516 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29517 HORZ specifying the horizontal margin, and VERT specifying the
29518 vertical margin. */);
29519 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29520
29521 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29522 doc: /* Relief thickness of tool-bar buttons. */);
29523 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29524
29525 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29526 doc: /* Tool bar style to use.
29527 It can be one of
29528 image - show images only
29529 text - show text only
29530 both - show both, text below image
29531 both-horiz - show text to the right of the image
29532 text-image-horiz - show text to the left of the image
29533 any other - use system default or image if no system default.
29534
29535 This variable only affects the GTK+ toolkit version of Emacs. */);
29536 Vtool_bar_style = Qnil;
29537
29538 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29539 doc: /* Maximum number of characters a label can have to be shown.
29540 The tool bar style must also show labels for this to have any effect, see
29541 `tool-bar-style'. */);
29542 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29543
29544 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29545 doc: /* List of functions to call to fontify regions of text.
29546 Each function is called with one argument POS. Functions must
29547 fontify a region starting at POS in the current buffer, and give
29548 fontified regions the property `fontified'. */);
29549 Vfontification_functions = Qnil;
29550 Fmake_variable_buffer_local (Qfontification_functions);
29551
29552 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29553 unibyte_display_via_language_environment,
29554 doc: /* Non-nil means display unibyte text according to language environment.
29555 Specifically, this means that raw bytes in the range 160-255 decimal
29556 are displayed by converting them to the equivalent multibyte characters
29557 according to the current language environment. As a result, they are
29558 displayed according to the current fontset.
29559
29560 Note that this variable affects only how these bytes are displayed,
29561 but does not change the fact they are interpreted as raw bytes. */);
29562 unibyte_display_via_language_environment = 0;
29563
29564 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29565 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29566 If a float, it specifies a fraction of the mini-window frame's height.
29567 If an integer, it specifies a number of lines. */);
29568 Vmax_mini_window_height = make_float (0.25);
29569
29570 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29571 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29572 A value of nil means don't automatically resize mini-windows.
29573 A value of t means resize them to fit the text displayed in them.
29574 A value of `grow-only', the default, means let mini-windows grow only;
29575 they return to their normal size when the minibuffer is closed, or the
29576 echo area becomes empty. */);
29577 Vresize_mini_windows = Qgrow_only;
29578
29579 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29580 doc: /* Alist specifying how to blink the cursor off.
29581 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29582 `cursor-type' frame-parameter or variable equals ON-STATE,
29583 comparing using `equal', Emacs uses OFF-STATE to specify
29584 how to blink it off. ON-STATE and OFF-STATE are values for
29585 the `cursor-type' frame parameter.
29586
29587 If a frame's ON-STATE has no entry in this list,
29588 the frame's other specifications determine how to blink the cursor off. */);
29589 Vblink_cursor_alist = Qnil;
29590
29591 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29592 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29593 If non-nil, windows are automatically scrolled horizontally to make
29594 point visible. */);
29595 automatic_hscrolling_p = 1;
29596 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29597
29598 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29599 doc: /* How many columns away from the window edge point is allowed to get
29600 before automatic hscrolling will horizontally scroll the window. */);
29601 hscroll_margin = 5;
29602
29603 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29604 doc: /* How many columns to scroll the window when point gets too close to the edge.
29605 When point is less than `hscroll-margin' columns from the window
29606 edge, automatic hscrolling will scroll the window by the amount of columns
29607 determined by this variable. If its value is a positive integer, scroll that
29608 many columns. If it's a positive floating-point number, it specifies the
29609 fraction of the window's width to scroll. If it's nil or zero, point will be
29610 centered horizontally after the scroll. Any other value, including negative
29611 numbers, are treated as if the value were zero.
29612
29613 Automatic hscrolling always moves point outside the scroll margin, so if
29614 point was more than scroll step columns inside the margin, the window will
29615 scroll more than the value given by the scroll step.
29616
29617 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29618 and `scroll-right' overrides this variable's effect. */);
29619 Vhscroll_step = make_number (0);
29620
29621 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29622 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29623 Bind this around calls to `message' to let it take effect. */);
29624 message_truncate_lines = 0;
29625
29626 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29627 doc: /* Normal hook run to update the menu bar definitions.
29628 Redisplay runs this hook before it redisplays the menu bar.
29629 This is used to update submenus such as Buffers,
29630 whose contents depend on various data. */);
29631 Vmenu_bar_update_hook = Qnil;
29632
29633 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29634 doc: /* Frame for which we are updating a menu.
29635 The enable predicate for a menu binding should check this variable. */);
29636 Vmenu_updating_frame = Qnil;
29637
29638 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29639 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29640 inhibit_menubar_update = 0;
29641
29642 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29643 doc: /* Prefix prepended to all continuation lines at display time.
29644 The value may be a string, an image, or a stretch-glyph; it is
29645 interpreted in the same way as the value of a `display' text property.
29646
29647 This variable is overridden by any `wrap-prefix' text or overlay
29648 property.
29649
29650 To add a prefix to non-continuation lines, use `line-prefix'. */);
29651 Vwrap_prefix = Qnil;
29652 DEFSYM (Qwrap_prefix, "wrap-prefix");
29653 Fmake_variable_buffer_local (Qwrap_prefix);
29654
29655 DEFVAR_LISP ("line-prefix", Vline_prefix,
29656 doc: /* Prefix prepended to all non-continuation lines at display time.
29657 The value may be a string, an image, or a stretch-glyph; it is
29658 interpreted in the same way as the value of a `display' text property.
29659
29660 This variable is overridden by any `line-prefix' text or overlay
29661 property.
29662
29663 To add a prefix to continuation lines, use `wrap-prefix'. */);
29664 Vline_prefix = Qnil;
29665 DEFSYM (Qline_prefix, "line-prefix");
29666 Fmake_variable_buffer_local (Qline_prefix);
29667
29668 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29669 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29670 inhibit_eval_during_redisplay = 0;
29671
29672 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29673 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29674 inhibit_free_realized_faces = 0;
29675
29676 #ifdef GLYPH_DEBUG
29677 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29678 doc: /* Inhibit try_window_id display optimization. */);
29679 inhibit_try_window_id = 0;
29680
29681 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29682 doc: /* Inhibit try_window_reusing display optimization. */);
29683 inhibit_try_window_reusing = 0;
29684
29685 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29686 doc: /* Inhibit try_cursor_movement display optimization. */);
29687 inhibit_try_cursor_movement = 0;
29688 #endif /* GLYPH_DEBUG */
29689
29690 DEFVAR_INT ("overline-margin", overline_margin,
29691 doc: /* Space between overline and text, in pixels.
29692 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29693 margin to the character height. */);
29694 overline_margin = 2;
29695
29696 DEFVAR_INT ("underline-minimum-offset",
29697 underline_minimum_offset,
29698 doc: /* Minimum distance between baseline and underline.
29699 This can improve legibility of underlined text at small font sizes,
29700 particularly when using variable `x-use-underline-position-properties'
29701 with fonts that specify an UNDERLINE_POSITION relatively close to the
29702 baseline. The default value is 1. */);
29703 underline_minimum_offset = 1;
29704
29705 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29706 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29707 This feature only works when on a window system that can change
29708 cursor shapes. */);
29709 display_hourglass_p = 1;
29710
29711 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29712 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29713 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29714
29715 hourglass_atimer = NULL;
29716 hourglass_shown_p = 0;
29717
29718 DEFSYM (Qglyphless_char, "glyphless-char");
29719 DEFSYM (Qhex_code, "hex-code");
29720 DEFSYM (Qempty_box, "empty-box");
29721 DEFSYM (Qthin_space, "thin-space");
29722 DEFSYM (Qzero_width, "zero-width");
29723
29724 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29725 /* Intern this now in case it isn't already done.
29726 Setting this variable twice is harmless.
29727 But don't staticpro it here--that is done in alloc.c. */
29728 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29729 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29730
29731 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29732 doc: /* Char-table defining glyphless characters.
29733 Each element, if non-nil, should be one of the following:
29734 an ASCII acronym string: display this string in a box
29735 `hex-code': display the hexadecimal code of a character in a box
29736 `empty-box': display as an empty box
29737 `thin-space': display as 1-pixel width space
29738 `zero-width': don't display
29739 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29740 display method for graphical terminals and text terminals respectively.
29741 GRAPHICAL and TEXT should each have one of the values listed above.
29742
29743 The char-table has one extra slot to control the display of a character for
29744 which no font is found. This slot only takes effect on graphical terminals.
29745 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29746 `thin-space'. The default is `empty-box'. */);
29747 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29748 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29749 Qempty_box);
29750
29751 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29752 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29753 Vdebug_on_message = Qnil;
29754 }
29755
29756
29757 /* Initialize this module when Emacs starts. */
29758
29759 void
29760 init_xdisp (void)
29761 {
29762 current_header_line_height = current_mode_line_height = -1;
29763
29764 CHARPOS (this_line_start_pos) = 0;
29765
29766 if (!noninteractive)
29767 {
29768 struct window *m = XWINDOW (minibuf_window);
29769 Lisp_Object frame = m->frame;
29770 struct frame *f = XFRAME (frame);
29771 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29772 struct window *r = XWINDOW (root);
29773 int i;
29774
29775 echo_area_window = minibuf_window;
29776
29777 r->top_line = FRAME_TOP_MARGIN (f);
29778 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29779 r->total_cols = FRAME_COLS (f);
29780
29781 m->top_line = FRAME_LINES (f) - 1;
29782 m->total_lines = 1;
29783 m->total_cols = FRAME_COLS (f);
29784
29785 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29786 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29787 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29788
29789 /* The default ellipsis glyphs `...'. */
29790 for (i = 0; i < 3; ++i)
29791 default_invis_vector[i] = make_number ('.');
29792 }
29793
29794 {
29795 /* Allocate the buffer for frame titles.
29796 Also used for `format-mode-line'. */
29797 int size = 100;
29798 mode_line_noprop_buf = xmalloc (size);
29799 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29800 mode_line_noprop_ptr = mode_line_noprop_buf;
29801 mode_line_target = MODE_LINE_DISPLAY;
29802 }
29803
29804 help_echo_showing_p = 0;
29805 }
29806
29807 /* Platform-independent portion of hourglass implementation. */
29808
29809 /* Cancel a currently active hourglass timer, and start a new one. */
29810 void
29811 start_hourglass (void)
29812 {
29813 #if defined (HAVE_WINDOW_SYSTEM)
29814 EMACS_TIME delay;
29815
29816 cancel_hourglass ();
29817
29818 if (INTEGERP (Vhourglass_delay)
29819 && XINT (Vhourglass_delay) > 0)
29820 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29821 TYPE_MAXIMUM (time_t)),
29822 0);
29823 else if (FLOATP (Vhourglass_delay)
29824 && XFLOAT_DATA (Vhourglass_delay) > 0)
29825 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29826 else
29827 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29828
29829 #ifdef HAVE_NTGUI
29830 {
29831 extern void w32_note_current_window (void);
29832 w32_note_current_window ();
29833 }
29834 #endif /* HAVE_NTGUI */
29835
29836 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29837 show_hourglass, NULL);
29838 #endif
29839 }
29840
29841
29842 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29843 shown. */
29844 void
29845 cancel_hourglass (void)
29846 {
29847 #if defined (HAVE_WINDOW_SYSTEM)
29848 if (hourglass_atimer)
29849 {
29850 cancel_atimer (hourglass_atimer);
29851 hourglass_atimer = NULL;
29852 }
29853
29854 if (hourglass_shown_p)
29855 hide_hourglass ();
29856 #endif
29857 }