25c09fe40bd81e409f25622e3c0449d1ed7659b8
[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 #include "font.h"
317
318 #ifndef FRAME_X_OUTPUT
319 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
320 #endif
321
322 #define INFINITY 10000000
323
324 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
325 Lisp_Object Qwindow_scroll_functions;
326 static Lisp_Object Qwindow_text_change_functions;
327 static Lisp_Object Qredisplay_end_trigger_functions;
328 Lisp_Object Qinhibit_point_motion_hooks;
329 static Lisp_Object QCeval, QCpropertize;
330 Lisp_Object QCfile, QCdata;
331 static Lisp_Object Qfontified;
332 static Lisp_Object Qgrow_only;
333 static Lisp_Object Qinhibit_eval_during_redisplay;
334 static Lisp_Object Qbuffer_position, Qposition, Qobject;
335 static Lisp_Object Qright_to_left, Qleft_to_right;
336
337 /* Cursor shapes. */
338 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
339
340 /* Pointer shapes. */
341 static Lisp_Object Qarrow, Qhand;
342 Lisp_Object Qtext;
343
344 /* Holds the list (error). */
345 static Lisp_Object list_of_error;
346
347 static Lisp_Object Qfontification_functions;
348
349 static Lisp_Object Qwrap_prefix;
350 static Lisp_Object Qline_prefix;
351 static Lisp_Object Qredisplay_internal;
352
353 /* Non-nil means don't actually do any redisplay. */
354
355 Lisp_Object Qinhibit_redisplay;
356
357 /* Names of text properties relevant for redisplay. */
358
359 Lisp_Object Qdisplay;
360
361 Lisp_Object Qspace, QCalign_to;
362 static Lisp_Object QCrelative_width, QCrelative_height;
363 Lisp_Object Qleft_margin, Qright_margin;
364 static Lisp_Object Qspace_width, Qraise;
365 static Lisp_Object Qslice;
366 Lisp_Object Qcenter;
367 static Lisp_Object Qmargin, Qpointer;
368 static Lisp_Object Qline_height;
369
370 #ifdef HAVE_WINDOW_SYSTEM
371
372 /* Test if overflow newline into fringe. Called with iterator IT
373 at or past right window margin, and with IT->current_x set. */
374
375 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
376 (!NILP (Voverflow_newline_into_fringe) \
377 && FRAME_WINDOW_P ((IT)->f) \
378 && ((IT)->bidi_it.paragraph_dir == R2L \
379 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
380 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
381 && (IT)->current_x == (IT)->last_visible_x \
382 && (IT)->line_wrap != WORD_WRAP)
383
384 #else /* !HAVE_WINDOW_SYSTEM */
385 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
386 #endif /* HAVE_WINDOW_SYSTEM */
387
388 /* Test if the display element loaded in IT, or the underlying buffer
389 or string character, is a space or a TAB character. This is used
390 to determine where word wrapping can occur. */
391
392 #define IT_DISPLAYING_WHITESPACE(it) \
393 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
394 || ((STRINGP (it->string) \
395 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
396 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
397 || (it->s \
398 && (it->s[IT_BYTEPOS (*it)] == ' ' \
399 || it->s[IT_BYTEPOS (*it)] == '\t')) \
400 || (IT_BYTEPOS (*it) < ZV_BYTE \
401 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
402 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
403
404 /* Name of the face used to highlight trailing whitespace. */
405
406 static Lisp_Object Qtrailing_whitespace;
407
408 /* Name and number of the face used to highlight escape glyphs. */
409
410 static Lisp_Object Qescape_glyph;
411
412 /* Name and number of the face used to highlight non-breaking spaces. */
413
414 static Lisp_Object Qnobreak_space;
415
416 /* The symbol `image' which is the car of the lists used to represent
417 images in Lisp. Also a tool bar style. */
418
419 Lisp_Object Qimage;
420
421 /* The image map types. */
422 Lisp_Object QCmap;
423 static Lisp_Object QCpointer;
424 static Lisp_Object Qrect, Qcircle, Qpoly;
425
426 /* Tool bar styles */
427 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
428
429 /* Non-zero means print newline to stdout before next mini-buffer
430 message. */
431
432 int noninteractive_need_newline;
433
434 /* Non-zero means print newline to message log before next message. */
435
436 static int message_log_need_newline;
437
438 /* Three markers that message_dolog uses.
439 It could allocate them itself, but that causes trouble
440 in handling memory-full errors. */
441 static Lisp_Object message_dolog_marker1;
442 static Lisp_Object message_dolog_marker2;
443 static Lisp_Object message_dolog_marker3;
444 \f
445 /* The buffer position of the first character appearing entirely or
446 partially on the line of the selected window which contains the
447 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
448 redisplay optimization in redisplay_internal. */
449
450 static struct text_pos this_line_start_pos;
451
452 /* Number of characters past the end of the line above, including the
453 terminating newline. */
454
455 static struct text_pos this_line_end_pos;
456
457 /* The vertical positions and the height of this line. */
458
459 static int this_line_vpos;
460 static int this_line_y;
461 static int this_line_pixel_height;
462
463 /* X position at which this display line starts. Usually zero;
464 negative if first character is partially visible. */
465
466 static int this_line_start_x;
467
468 /* The smallest character position seen by move_it_* functions as they
469 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
470 hscrolled lines, see display_line. */
471
472 static struct text_pos this_line_min_pos;
473
474 /* Buffer that this_line_.* variables are referring to. */
475
476 static struct buffer *this_line_buffer;
477
478
479 /* Values of those variables at last redisplay are stored as
480 properties on `overlay-arrow-position' symbol. However, if
481 Voverlay_arrow_position is a marker, last-arrow-position is its
482 numerical position. */
483
484 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
485
486 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
487 properties on a symbol in overlay-arrow-variable-list. */
488
489 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
490
491 Lisp_Object Qmenu_bar_update_hook;
492
493 /* Nonzero if an overlay arrow has been displayed in this window. */
494
495 static int overlay_arrow_seen;
496
497 /* Vector containing glyphs for an ellipsis `...'. */
498
499 static Lisp_Object default_invis_vector[3];
500
501 /* This is the window where the echo area message was displayed. It
502 is always a mini-buffer window, but it may not be the same window
503 currently active as a mini-buffer. */
504
505 Lisp_Object echo_area_window;
506
507 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
508 pushes the current message and the value of
509 message_enable_multibyte on the stack, the function restore_message
510 pops the stack and displays MESSAGE again. */
511
512 static Lisp_Object Vmessage_stack;
513
514 /* Nonzero means multibyte characters were enabled when the echo area
515 message was specified. */
516
517 static int message_enable_multibyte;
518
519 /* Nonzero if we should redraw the mode lines on the next redisplay. */
520
521 int update_mode_lines;
522
523 /* Nonzero if window sizes or contents have changed since last
524 redisplay that finished. */
525
526 int windows_or_buffers_changed;
527
528 /* Nonzero means a frame's cursor type has been changed. */
529
530 int cursor_type_changed;
531
532 /* Nonzero after display_mode_line if %l was used and it displayed a
533 line number. */
534
535 static int line_number_displayed;
536
537 /* The name of the *Messages* buffer, a string. */
538
539 static Lisp_Object Vmessages_buffer_name;
540
541 /* Current, index 0, and last displayed echo area message. Either
542 buffers from echo_buffers, or nil to indicate no message. */
543
544 Lisp_Object echo_area_buffer[2];
545
546 /* The buffers referenced from echo_area_buffer. */
547
548 static Lisp_Object echo_buffer[2];
549
550 /* A vector saved used in with_area_buffer to reduce consing. */
551
552 static Lisp_Object Vwith_echo_area_save_vector;
553
554 /* Non-zero means display_echo_area should display the last echo area
555 message again. Set by redisplay_preserve_echo_area. */
556
557 static int display_last_displayed_message_p;
558
559 /* Nonzero if echo area is being used by print; zero if being used by
560 message. */
561
562 static int message_buf_print;
563
564 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
565
566 static Lisp_Object Qinhibit_menubar_update;
567 static Lisp_Object Qmessage_truncate_lines;
568
569 /* Set to 1 in clear_message to make redisplay_internal aware
570 of an emptied echo area. */
571
572 static int message_cleared_p;
573
574 /* A scratch glyph row with contents used for generating truncation
575 glyphs. Also used in direct_output_for_insert. */
576
577 #define MAX_SCRATCH_GLYPHS 100
578 static struct glyph_row scratch_glyph_row;
579 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
580
581 /* Ascent and height of the last line processed by move_it_to. */
582
583 static int last_max_ascent, last_height;
584
585 /* Non-zero if there's a help-echo in the echo area. */
586
587 int help_echo_showing_p;
588
589 /* If >= 0, computed, exact values of mode-line and header-line height
590 to use in the macros CURRENT_MODE_LINE_HEIGHT and
591 CURRENT_HEADER_LINE_HEIGHT. */
592
593 int current_mode_line_height, current_header_line_height;
594
595 /* The maximum distance to look ahead for text properties. Values
596 that are too small let us call compute_char_face and similar
597 functions too often which is expensive. Values that are too large
598 let us call compute_char_face and alike too often because we
599 might not be interested in text properties that far away. */
600
601 #define TEXT_PROP_DISTANCE_LIMIT 100
602
603 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
604 iterator state and later restore it. This is needed because the
605 bidi iterator on bidi.c keeps a stacked cache of its states, which
606 is really a singleton. When we use scratch iterator objects to
607 move around the buffer, we can cause the bidi cache to be pushed or
608 popped, and therefore we need to restore the cache state when we
609 return to the original iterator. */
610 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
611 do { \
612 if (CACHE) \
613 bidi_unshelve_cache (CACHE, 1); \
614 ITCOPY = ITORIG; \
615 CACHE = bidi_shelve_cache (); \
616 } while (0)
617
618 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
619 do { \
620 if (pITORIG != pITCOPY) \
621 *(pITORIG) = *(pITCOPY); \
622 bidi_unshelve_cache (CACHE, 0); \
623 CACHE = NULL; \
624 } while (0)
625
626 #ifdef GLYPH_DEBUG
627
628 /* Non-zero means print traces of redisplay if compiled with
629 GLYPH_DEBUG defined. */
630
631 int trace_redisplay_p;
632
633 #endif /* GLYPH_DEBUG */
634
635 #ifdef DEBUG_TRACE_MOVE
636 /* Non-zero means trace with TRACE_MOVE to stderr. */
637 int trace_move;
638
639 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
640 #else
641 #define TRACE_MOVE(x) (void) 0
642 #endif
643
644 static Lisp_Object Qauto_hscroll_mode;
645
646 /* Buffer being redisplayed -- for redisplay_window_error. */
647
648 static struct buffer *displayed_buffer;
649
650 /* Value returned from text property handlers (see below). */
651
652 enum prop_handled
653 {
654 HANDLED_NORMALLY,
655 HANDLED_RECOMPUTE_PROPS,
656 HANDLED_OVERLAY_STRING_CONSUMED,
657 HANDLED_RETURN
658 };
659
660 /* A description of text properties that redisplay is interested
661 in. */
662
663 struct props
664 {
665 /* The name of the property. */
666 Lisp_Object *name;
667
668 /* A unique index for the property. */
669 enum prop_idx idx;
670
671 /* A handler function called to set up iterator IT from the property
672 at IT's current position. Value is used to steer handle_stop. */
673 enum prop_handled (*handler) (struct it *it);
674 };
675
676 static enum prop_handled handle_face_prop (struct it *);
677 static enum prop_handled handle_invisible_prop (struct it *);
678 static enum prop_handled handle_display_prop (struct it *);
679 static enum prop_handled handle_composition_prop (struct it *);
680 static enum prop_handled handle_overlay_change (struct it *);
681 static enum prop_handled handle_fontified_prop (struct it *);
682
683 /* Properties handled by iterators. */
684
685 static struct props it_props[] =
686 {
687 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
688 /* Handle `face' before `display' because some sub-properties of
689 `display' need to know the face. */
690 {&Qface, FACE_PROP_IDX, handle_face_prop},
691 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
692 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
693 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
694 {NULL, 0, NULL}
695 };
696
697 /* Value is the position described by X. If X is a marker, value is
698 the marker_position of X. Otherwise, value is X. */
699
700 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
701
702 /* Enumeration returned by some move_it_.* functions internally. */
703
704 enum move_it_result
705 {
706 /* Not used. Undefined value. */
707 MOVE_UNDEFINED,
708
709 /* Move ended at the requested buffer position or ZV. */
710 MOVE_POS_MATCH_OR_ZV,
711
712 /* Move ended at the requested X pixel position. */
713 MOVE_X_REACHED,
714
715 /* Move within a line ended at the end of a line that must be
716 continued. */
717 MOVE_LINE_CONTINUED,
718
719 /* Move within a line ended at the end of a line that would
720 be displayed truncated. */
721 MOVE_LINE_TRUNCATED,
722
723 /* Move within a line ended at a line end. */
724 MOVE_NEWLINE_OR_CR
725 };
726
727 /* This counter is used to clear the face cache every once in a while
728 in redisplay_internal. It is incremented for each redisplay.
729 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
730 cleared. */
731
732 #define CLEAR_FACE_CACHE_COUNT 500
733 static int clear_face_cache_count;
734
735 /* Similarly for the image cache. */
736
737 #ifdef HAVE_WINDOW_SYSTEM
738 #define CLEAR_IMAGE_CACHE_COUNT 101
739 static int clear_image_cache_count;
740
741 /* Null glyph slice */
742 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
743 #endif
744
745 /* True while redisplay_internal is in progress. */
746
747 bool redisplaying_p;
748
749 static Lisp_Object Qinhibit_free_realized_faces;
750 static Lisp_Object Qmode_line_default_help_echo;
751
752 /* If a string, XTread_socket generates an event to display that string.
753 (The display is done in read_char.) */
754
755 Lisp_Object help_echo_string;
756 Lisp_Object help_echo_window;
757 Lisp_Object help_echo_object;
758 ptrdiff_t help_echo_pos;
759
760 /* Temporary variable for XTread_socket. */
761
762 Lisp_Object previous_help_echo_string;
763
764 /* Platform-independent portion of hourglass implementation. */
765
766 /* Non-zero means an hourglass cursor is currently shown. */
767 int hourglass_shown_p;
768
769 /* If non-null, an asynchronous timer that, when it expires, displays
770 an hourglass cursor on all frames. */
771 struct atimer *hourglass_atimer;
772
773 /* Name of the face used to display glyphless characters. */
774 Lisp_Object Qglyphless_char;
775
776 /* Symbol for the purpose of Vglyphless_char_display. */
777 static Lisp_Object Qglyphless_char_display;
778
779 /* Method symbols for Vglyphless_char_display. */
780 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
781
782 /* Default pixel width of `thin-space' display method. */
783 #define THIN_SPACE_WIDTH 1
784
785 /* Default number of seconds to wait before displaying an hourglass
786 cursor. */
787 #define DEFAULT_HOURGLASS_DELAY 1
788
789 \f
790 /* Function prototypes. */
791
792 static void setup_for_ellipsis (struct it *, int);
793 static void set_iterator_to_next (struct it *, int);
794 static void mark_window_display_accurate_1 (struct window *, int);
795 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
796 static int display_prop_string_p (Lisp_Object, Lisp_Object);
797 static int cursor_row_p (struct glyph_row *);
798 static int redisplay_mode_lines (Lisp_Object, int);
799 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
800
801 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
802
803 static void handle_line_prefix (struct it *);
804
805 static void pint2str (char *, int, ptrdiff_t);
806 static void pint2hrstr (char *, int, ptrdiff_t);
807 static struct text_pos run_window_scroll_functions (Lisp_Object,
808 struct text_pos);
809 static void reconsider_clip_changes (struct window *, struct buffer *);
810 static int text_outside_line_unchanged_p (struct window *,
811 ptrdiff_t, ptrdiff_t);
812 static void store_mode_line_noprop_char (char);
813 static int store_mode_line_noprop (const char *, int, int);
814 static void handle_stop (struct it *);
815 static void handle_stop_backwards (struct it *, ptrdiff_t);
816 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
817 static void ensure_echo_area_buffers (void);
818 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
819 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
820 static int with_echo_area_buffer (struct window *, int,
821 int (*) (ptrdiff_t, Lisp_Object),
822 ptrdiff_t, Lisp_Object);
823 static void clear_garbaged_frames (void);
824 static int current_message_1 (ptrdiff_t, Lisp_Object);
825 static void pop_message (void);
826 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
827 static void set_message (Lisp_Object);
828 static int set_message_1 (ptrdiff_t, Lisp_Object);
829 static int display_echo_area (struct window *);
830 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
831 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
832 static Lisp_Object unwind_redisplay (Lisp_Object);
833 static int string_char_and_length (const unsigned char *, int *);
834 static struct text_pos display_prop_end (struct it *, Lisp_Object,
835 struct text_pos);
836 static int compute_window_start_on_continuation_line (struct window *);
837 static void insert_left_trunc_glyphs (struct it *);
838 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
839 Lisp_Object);
840 static void extend_face_to_end_of_line (struct it *);
841 static int append_space_for_newline (struct it *, int);
842 static int cursor_row_fully_visible_p (struct window *, int, int);
843 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
844 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
845 static int trailing_whitespace_p (ptrdiff_t);
846 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
847 static void push_it (struct it *, struct text_pos *);
848 static void iterate_out_of_display_property (struct it *);
849 static void pop_it (struct it *);
850 static void sync_frame_with_window_matrix_rows (struct window *);
851 static void redisplay_internal (void);
852 static int echo_area_display (int);
853 static void redisplay_windows (Lisp_Object);
854 static void redisplay_window (Lisp_Object, int);
855 static Lisp_Object redisplay_window_error (Lisp_Object);
856 static Lisp_Object redisplay_window_0 (Lisp_Object);
857 static Lisp_Object redisplay_window_1 (Lisp_Object);
858 static int set_cursor_from_row (struct window *, struct glyph_row *,
859 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
860 int, int);
861 static int update_menu_bar (struct frame *, int, int);
862 static int try_window_reusing_current_matrix (struct window *);
863 static int try_window_id (struct window *);
864 static int display_line (struct it *);
865 static int display_mode_lines (struct window *);
866 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
867 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
868 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
869 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
870 static void display_menu_bar (struct window *);
871 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
872 ptrdiff_t *);
873 static int display_string (const char *, Lisp_Object, Lisp_Object,
874 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
875 static void compute_line_metrics (struct it *);
876 static void run_redisplay_end_trigger_hook (struct it *);
877 static int get_overlay_strings (struct it *, ptrdiff_t);
878 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
879 static void next_overlay_string (struct it *);
880 static void reseat (struct it *, struct text_pos, int);
881 static void reseat_1 (struct it *, struct text_pos, int);
882 static void back_to_previous_visible_line_start (struct it *);
883 void reseat_at_previous_visible_line_start (struct it *);
884 static void reseat_at_next_visible_line_start (struct it *, int);
885 static int next_element_from_ellipsis (struct it *);
886 static int next_element_from_display_vector (struct it *);
887 static int next_element_from_string (struct it *);
888 static int next_element_from_c_string (struct it *);
889 static int next_element_from_buffer (struct it *);
890 static int next_element_from_composition (struct it *);
891 static int next_element_from_image (struct it *);
892 static int next_element_from_stretch (struct it *);
893 static void load_overlay_strings (struct it *, ptrdiff_t);
894 static int init_from_display_pos (struct it *, struct window *,
895 struct display_pos *);
896 static void reseat_to_string (struct it *, const char *,
897 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
898 static int get_next_display_element (struct it *);
899 static enum move_it_result
900 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
901 enum move_operation_enum);
902 void move_it_vertically_backward (struct it *, int);
903 static void get_visually_first_element (struct it *);
904 static void init_to_row_start (struct it *, struct window *,
905 struct glyph_row *);
906 static int init_to_row_end (struct it *, struct window *,
907 struct glyph_row *);
908 static void back_to_previous_line_start (struct it *);
909 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
910 static struct text_pos string_pos_nchars_ahead (struct text_pos,
911 Lisp_Object, ptrdiff_t);
912 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
913 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
914 static ptrdiff_t number_of_chars (const char *, int);
915 static void compute_stop_pos (struct it *);
916 static void compute_string_pos (struct text_pos *, struct text_pos,
917 Lisp_Object);
918 static int face_before_or_after_it_pos (struct it *, int);
919 static ptrdiff_t next_overlay_change (ptrdiff_t);
920 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
921 Lisp_Object, struct text_pos *, ptrdiff_t, int);
922 static int handle_single_display_spec (struct it *, Lisp_Object,
923 Lisp_Object, Lisp_Object,
924 struct text_pos *, ptrdiff_t, int, int);
925 static int underlying_face_id (struct it *);
926 static int in_ellipses_for_invisible_text_p (struct display_pos *,
927 struct window *);
928
929 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
930 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
931
932 #ifdef HAVE_WINDOW_SYSTEM
933
934 static void x_consider_frame_title (Lisp_Object);
935 static int tool_bar_lines_needed (struct frame *, int *);
936 static void update_tool_bar (struct frame *, int);
937 static void build_desired_tool_bar_string (struct frame *f);
938 static int redisplay_tool_bar (struct frame *);
939 static void display_tool_bar_line (struct it *, int);
940 static void notice_overwritten_cursor (struct window *,
941 enum glyph_row_area,
942 int, int, int, int);
943 static void append_stretch_glyph (struct it *, Lisp_Object,
944 int, int, int);
945
946
947 #endif /* HAVE_WINDOW_SYSTEM */
948
949 static void produce_special_glyphs (struct it *, enum display_element_type);
950 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
951 static int coords_in_mouse_face_p (struct window *, int, int);
952
953
954 \f
955 /***********************************************************************
956 Window display dimensions
957 ***********************************************************************/
958
959 /* Return the bottom boundary y-position for text lines in window W.
960 This is the first y position at which a line cannot start.
961 It is relative to the top of the window.
962
963 This is the height of W minus the height of a mode line, if any. */
964
965 int
966 window_text_bottom_y (struct window *w)
967 {
968 int height = WINDOW_TOTAL_HEIGHT (w);
969
970 if (WINDOW_WANTS_MODELINE_P (w))
971 height -= CURRENT_MODE_LINE_HEIGHT (w);
972 return height;
973 }
974
975 /* Return the pixel width of display area AREA of window W. AREA < 0
976 means return the total width of W, not including fringes to
977 the left and right of the window. */
978
979 int
980 window_box_width (struct window *w, int area)
981 {
982 int cols = XFASTINT (w->total_cols);
983 int pixels = 0;
984
985 if (!w->pseudo_window_p)
986 {
987 cols -= WINDOW_SCROLL_BAR_COLS (w);
988
989 if (area == TEXT_AREA)
990 {
991 if (INTEGERP (w->left_margin_cols))
992 cols -= XFASTINT (w->left_margin_cols);
993 if (INTEGERP (w->right_margin_cols))
994 cols -= XFASTINT (w->right_margin_cols);
995 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
996 }
997 else if (area == LEFT_MARGIN_AREA)
998 {
999 cols = (INTEGERP (w->left_margin_cols)
1000 ? XFASTINT (w->left_margin_cols) : 0);
1001 pixels = 0;
1002 }
1003 else if (area == RIGHT_MARGIN_AREA)
1004 {
1005 cols = (INTEGERP (w->right_margin_cols)
1006 ? XFASTINT (w->right_margin_cols) : 0);
1007 pixels = 0;
1008 }
1009 }
1010
1011 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1012 }
1013
1014
1015 /* Return the pixel height of the display area of window W, not
1016 including mode lines of W, if any. */
1017
1018 int
1019 window_box_height (struct window *w)
1020 {
1021 struct frame *f = XFRAME (w->frame);
1022 int height = WINDOW_TOTAL_HEIGHT (w);
1023
1024 eassert (height >= 0);
1025
1026 /* Note: the code below that determines the mode-line/header-line
1027 height is essentially the same as that contained in the macro
1028 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1029 the appropriate glyph row has its `mode_line_p' flag set,
1030 and if it doesn't, uses estimate_mode_line_height instead. */
1031
1032 if (WINDOW_WANTS_MODELINE_P (w))
1033 {
1034 struct glyph_row *ml_row
1035 = (w->current_matrix && w->current_matrix->rows
1036 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1037 : 0);
1038 if (ml_row && ml_row->mode_line_p)
1039 height -= ml_row->height;
1040 else
1041 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1042 }
1043
1044 if (WINDOW_WANTS_HEADER_LINE_P (w))
1045 {
1046 struct glyph_row *hl_row
1047 = (w->current_matrix && w->current_matrix->rows
1048 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1049 : 0);
1050 if (hl_row && hl_row->mode_line_p)
1051 height -= hl_row->height;
1052 else
1053 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1054 }
1055
1056 /* With a very small font and a mode-line that's taller than
1057 default, we might end up with a negative height. */
1058 return max (0, height);
1059 }
1060
1061 /* Return the window-relative coordinate of the left edge of display
1062 area AREA of window W. AREA < 0 means return the left edge of the
1063 whole window, to the right of the left fringe of W. */
1064
1065 int
1066 window_box_left_offset (struct window *w, int area)
1067 {
1068 int x;
1069
1070 if (w->pseudo_window_p)
1071 return 0;
1072
1073 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1074
1075 if (area == TEXT_AREA)
1076 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1077 + window_box_width (w, LEFT_MARGIN_AREA));
1078 else if (area == RIGHT_MARGIN_AREA)
1079 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1080 + window_box_width (w, LEFT_MARGIN_AREA)
1081 + window_box_width (w, TEXT_AREA)
1082 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1083 ? 0
1084 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1085 else if (area == LEFT_MARGIN_AREA
1086 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1087 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1088
1089 return x;
1090 }
1091
1092
1093 /* Return the window-relative coordinate of the right edge of display
1094 area AREA of window W. AREA < 0 means return the right edge of the
1095 whole window, to the left of the right fringe of W. */
1096
1097 int
1098 window_box_right_offset (struct window *w, int area)
1099 {
1100 return window_box_left_offset (w, area) + window_box_width (w, area);
1101 }
1102
1103 /* Return the frame-relative coordinate of the left edge of display
1104 area AREA of window W. AREA < 0 means return the left edge of the
1105 whole window, to the right of the left fringe of W. */
1106
1107 int
1108 window_box_left (struct window *w, int area)
1109 {
1110 struct frame *f = XFRAME (w->frame);
1111 int x;
1112
1113 if (w->pseudo_window_p)
1114 return FRAME_INTERNAL_BORDER_WIDTH (f);
1115
1116 x = (WINDOW_LEFT_EDGE_X (w)
1117 + window_box_left_offset (w, area));
1118
1119 return x;
1120 }
1121
1122
1123 /* Return the frame-relative coordinate of the right edge of display
1124 area AREA of window W. AREA < 0 means return the right edge of the
1125 whole window, to the left of the right fringe of W. */
1126
1127 int
1128 window_box_right (struct window *w, int area)
1129 {
1130 return window_box_left (w, area) + window_box_width (w, area);
1131 }
1132
1133 /* Get the bounding box of the display area AREA of window W, without
1134 mode lines, in frame-relative coordinates. AREA < 0 means the
1135 whole window, not including the left and right fringes of
1136 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1137 coordinates of the upper-left corner of the box. Return in
1138 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1139
1140 void
1141 window_box (struct window *w, int area, int *box_x, int *box_y,
1142 int *box_width, int *box_height)
1143 {
1144 if (box_width)
1145 *box_width = window_box_width (w, area);
1146 if (box_height)
1147 *box_height = window_box_height (w);
1148 if (box_x)
1149 *box_x = window_box_left (w, area);
1150 if (box_y)
1151 {
1152 *box_y = WINDOW_TOP_EDGE_Y (w);
1153 if (WINDOW_WANTS_HEADER_LINE_P (w))
1154 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1155 }
1156 }
1157
1158
1159 /* Get the bounding box of the display area AREA of window W, without
1160 mode lines. AREA < 0 means the whole window, not including the
1161 left and right fringe of the window. Return in *TOP_LEFT_X
1162 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1163 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1164 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1165 box. */
1166
1167 static void
1168 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1169 int *bottom_right_x, int *bottom_right_y)
1170 {
1171 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1172 bottom_right_y);
1173 *bottom_right_x += *top_left_x;
1174 *bottom_right_y += *top_left_y;
1175 }
1176
1177
1178 \f
1179 /***********************************************************************
1180 Utilities
1181 ***********************************************************************/
1182
1183 /* Return the bottom y-position of the line the iterator IT is in.
1184 This can modify IT's settings. */
1185
1186 int
1187 line_bottom_y (struct it *it)
1188 {
1189 int line_height = it->max_ascent + it->max_descent;
1190 int line_top_y = it->current_y;
1191
1192 if (line_height == 0)
1193 {
1194 if (last_height)
1195 line_height = last_height;
1196 else if (IT_CHARPOS (*it) < ZV)
1197 {
1198 move_it_by_lines (it, 1);
1199 line_height = (it->max_ascent || it->max_descent
1200 ? it->max_ascent + it->max_descent
1201 : last_height);
1202 }
1203 else
1204 {
1205 struct glyph_row *row = it->glyph_row;
1206
1207 /* Use the default character height. */
1208 it->glyph_row = NULL;
1209 it->what = IT_CHARACTER;
1210 it->c = ' ';
1211 it->len = 1;
1212 PRODUCE_GLYPHS (it);
1213 line_height = it->ascent + it->descent;
1214 it->glyph_row = row;
1215 }
1216 }
1217
1218 return line_top_y + line_height;
1219 }
1220
1221 /* Subroutine of pos_visible_p below. Extracts a display string, if
1222 any, from the display spec given as its argument. */
1223 static Lisp_Object
1224 string_from_display_spec (Lisp_Object spec)
1225 {
1226 if (CONSP (spec))
1227 {
1228 while (CONSP (spec))
1229 {
1230 if (STRINGP (XCAR (spec)))
1231 return XCAR (spec);
1232 spec = XCDR (spec);
1233 }
1234 }
1235 else if (VECTORP (spec))
1236 {
1237 ptrdiff_t i;
1238
1239 for (i = 0; i < ASIZE (spec); i++)
1240 {
1241 if (STRINGP (AREF (spec, i)))
1242 return AREF (spec, i);
1243 }
1244 return Qnil;
1245 }
1246
1247 return spec;
1248 }
1249
1250
1251 /* Limit insanely large values of W->hscroll on frame F to the largest
1252 value that will still prevent first_visible_x and last_visible_x of
1253 'struct it' from overflowing an int. */
1254 static int
1255 window_hscroll_limited (struct window *w, struct frame *f)
1256 {
1257 ptrdiff_t window_hscroll = w->hscroll;
1258 int window_text_width = window_box_width (w, TEXT_AREA);
1259 int colwidth = FRAME_COLUMN_WIDTH (f);
1260
1261 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1262 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1263
1264 return window_hscroll;
1265 }
1266
1267 /* Return 1 if position CHARPOS is visible in window W.
1268 CHARPOS < 0 means return info about WINDOW_END position.
1269 If visible, set *X and *Y to pixel coordinates of top left corner.
1270 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1271 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1272
1273 int
1274 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1275 int *rtop, int *rbot, int *rowh, int *vpos)
1276 {
1277 struct it it;
1278 void *itdata = bidi_shelve_cache ();
1279 struct text_pos top;
1280 int visible_p = 0;
1281 struct buffer *old_buffer = NULL;
1282
1283 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1284 return visible_p;
1285
1286 if (XBUFFER (w->buffer) != current_buffer)
1287 {
1288 old_buffer = current_buffer;
1289 set_buffer_internal_1 (XBUFFER (w->buffer));
1290 }
1291
1292 SET_TEXT_POS_FROM_MARKER (top, w->start);
1293 /* Scrolling a minibuffer window via scroll bar when the echo area
1294 shows long text sometimes resets the minibuffer contents behind
1295 our backs. */
1296 if (CHARPOS (top) > ZV)
1297 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1298
1299 /* Compute exact mode line heights. */
1300 if (WINDOW_WANTS_MODELINE_P (w))
1301 current_mode_line_height
1302 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1303 BVAR (current_buffer, mode_line_format));
1304
1305 if (WINDOW_WANTS_HEADER_LINE_P (w))
1306 current_header_line_height
1307 = display_mode_line (w, HEADER_LINE_FACE_ID,
1308 BVAR (current_buffer, header_line_format));
1309
1310 start_display (&it, w, top);
1311 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1312 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1313
1314 if (charpos >= 0
1315 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1316 && IT_CHARPOS (it) >= charpos)
1317 /* When scanning backwards under bidi iteration, move_it_to
1318 stops at or _before_ CHARPOS, because it stops at or to
1319 the _right_ of the character at CHARPOS. */
1320 || (it.bidi_p && it.bidi_it.scan_dir == -1
1321 && IT_CHARPOS (it) <= charpos)))
1322 {
1323 /* We have reached CHARPOS, or passed it. How the call to
1324 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1325 or covered by a display property, move_it_to stops at the end
1326 of the invisible text, to the right of CHARPOS. (ii) If
1327 CHARPOS is in a display vector, move_it_to stops on its last
1328 glyph. */
1329 int top_x = it.current_x;
1330 int top_y = it.current_y;
1331 /* Calling line_bottom_y may change it.method, it.position, etc. */
1332 enum it_method it_method = it.method;
1333 int bottom_y = (last_height = 0, line_bottom_y (&it));
1334 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1335
1336 if (top_y < window_top_y)
1337 visible_p = bottom_y > window_top_y;
1338 else if (top_y < it.last_visible_y)
1339 visible_p = 1;
1340 if (bottom_y >= it.last_visible_y
1341 && it.bidi_p && it.bidi_it.scan_dir == -1
1342 && IT_CHARPOS (it) < charpos)
1343 {
1344 /* When the last line of the window is scanned backwards
1345 under bidi iteration, we could be duped into thinking
1346 that we have passed CHARPOS, when in fact move_it_to
1347 simply stopped short of CHARPOS because it reached
1348 last_visible_y. To see if that's what happened, we call
1349 move_it_to again with a slightly larger vertical limit,
1350 and see if it actually moved vertically; if it did, we
1351 didn't really reach CHARPOS, which is beyond window end. */
1352 struct it save_it = it;
1353 /* Why 10? because we don't know how many canonical lines
1354 will the height of the next line(s) be. So we guess. */
1355 int ten_more_lines =
1356 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1357
1358 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1359 MOVE_TO_POS | MOVE_TO_Y);
1360 if (it.current_y > top_y)
1361 visible_p = 0;
1362
1363 it = save_it;
1364 }
1365 if (visible_p)
1366 {
1367 if (it_method == GET_FROM_DISPLAY_VECTOR)
1368 {
1369 /* We stopped on the last glyph of a display vector.
1370 Try and recompute. Hack alert! */
1371 if (charpos < 2 || top.charpos >= charpos)
1372 top_x = it.glyph_row->x;
1373 else
1374 {
1375 struct it it2;
1376 start_display (&it2, w, top);
1377 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1378 get_next_display_element (&it2);
1379 PRODUCE_GLYPHS (&it2);
1380 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1381 || it2.current_x > it2.last_visible_x)
1382 top_x = it.glyph_row->x;
1383 else
1384 {
1385 top_x = it2.current_x;
1386 top_y = it2.current_y;
1387 }
1388 }
1389 }
1390 else if (IT_CHARPOS (it) != charpos)
1391 {
1392 Lisp_Object cpos = make_number (charpos);
1393 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1394 Lisp_Object string = string_from_display_spec (spec);
1395 int newline_in_string = 0;
1396
1397 if (STRINGP (string))
1398 {
1399 const char *s = SSDATA (string);
1400 const char *e = s + SBYTES (string);
1401 while (s < e)
1402 {
1403 if (*s++ == '\n')
1404 {
1405 newline_in_string = 1;
1406 break;
1407 }
1408 }
1409 }
1410 /* The tricky code below is needed because there's a
1411 discrepancy between move_it_to and how we set cursor
1412 when the display line ends in a newline from a
1413 display string. move_it_to will stop _after_ such
1414 display strings, whereas set_cursor_from_row
1415 conspires with cursor_row_p to place the cursor on
1416 the first glyph produced from the display string. */
1417
1418 /* We have overshoot PT because it is covered by a
1419 display property whose value is a string. If the
1420 string includes embedded newlines, we are also in the
1421 wrong display line. Backtrack to the correct line,
1422 where the display string begins. */
1423 if (newline_in_string)
1424 {
1425 Lisp_Object startpos, endpos;
1426 EMACS_INT start, end;
1427 struct it it3;
1428 int it3_moved;
1429
1430 /* Find the first and the last buffer positions
1431 covered by the display string. */
1432 endpos =
1433 Fnext_single_char_property_change (cpos, Qdisplay,
1434 Qnil, Qnil);
1435 startpos =
1436 Fprevious_single_char_property_change (endpos, Qdisplay,
1437 Qnil, Qnil);
1438 start = XFASTINT (startpos);
1439 end = XFASTINT (endpos);
1440 /* Move to the last buffer position before the
1441 display property. */
1442 start_display (&it3, w, top);
1443 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1444 /* Move forward one more line if the position before
1445 the display string is a newline or if it is the
1446 rightmost character on a line that is
1447 continued or word-wrapped. */
1448 if (it3.method == GET_FROM_BUFFER
1449 && it3.c == '\n')
1450 move_it_by_lines (&it3, 1);
1451 else if (move_it_in_display_line_to (&it3, -1,
1452 it3.current_x
1453 + it3.pixel_width,
1454 MOVE_TO_X)
1455 == MOVE_LINE_CONTINUED)
1456 {
1457 move_it_by_lines (&it3, 1);
1458 /* When we are under word-wrap, the #$@%!
1459 move_it_by_lines moves 2 lines, so we need to
1460 fix that up. */
1461 if (it3.line_wrap == WORD_WRAP)
1462 move_it_by_lines (&it3, -1);
1463 }
1464
1465 /* Record the vertical coordinate of the display
1466 line where we wound up. */
1467 top_y = it3.current_y;
1468 if (it3.bidi_p)
1469 {
1470 /* When characters are reordered for display,
1471 the character displayed to the left of the
1472 display string could be _after_ the display
1473 property in the logical order. Use the
1474 smallest vertical position of these two. */
1475 start_display (&it3, w, top);
1476 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1477 if (it3.current_y < top_y)
1478 top_y = it3.current_y;
1479 }
1480 /* Move from the top of the window to the beginning
1481 of the display line where the display string
1482 begins. */
1483 start_display (&it3, w, top);
1484 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1485 /* If it3_moved stays zero after the 'while' loop
1486 below, that means we already were at a newline
1487 before the loop (e.g., the display string begins
1488 with a newline), so we don't need to (and cannot)
1489 inspect the glyphs of it3.glyph_row, because
1490 PRODUCE_GLYPHS will not produce anything for a
1491 newline, and thus it3.glyph_row stays at its
1492 stale content it got at top of the window. */
1493 it3_moved = 0;
1494 /* Finally, advance the iterator until we hit the
1495 first display element whose character position is
1496 CHARPOS, or until the first newline from the
1497 display string, which signals the end of the
1498 display line. */
1499 while (get_next_display_element (&it3))
1500 {
1501 PRODUCE_GLYPHS (&it3);
1502 if (IT_CHARPOS (it3) == charpos
1503 || ITERATOR_AT_END_OF_LINE_P (&it3))
1504 break;
1505 it3_moved = 1;
1506 set_iterator_to_next (&it3, 0);
1507 }
1508 top_x = it3.current_x - it3.pixel_width;
1509 /* Normally, we would exit the above loop because we
1510 found the display element whose character
1511 position is CHARPOS. For the contingency that we
1512 didn't, and stopped at the first newline from the
1513 display string, move back over the glyphs
1514 produced from the string, until we find the
1515 rightmost glyph not from the string. */
1516 if (it3_moved
1517 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1518 {
1519 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1520 + it3.glyph_row->used[TEXT_AREA];
1521
1522 while (EQ ((g - 1)->object, string))
1523 {
1524 --g;
1525 top_x -= g->pixel_width;
1526 }
1527 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1528 + it3.glyph_row->used[TEXT_AREA]);
1529 }
1530 }
1531 }
1532
1533 *x = top_x;
1534 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1535 *rtop = max (0, window_top_y - top_y);
1536 *rbot = max (0, bottom_y - it.last_visible_y);
1537 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1538 - max (top_y, window_top_y)));
1539 *vpos = it.vpos;
1540 }
1541 }
1542 else
1543 {
1544 /* We were asked to provide info about WINDOW_END. */
1545 struct it it2;
1546 void *it2data = NULL;
1547
1548 SAVE_IT (it2, it, it2data);
1549 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1550 move_it_by_lines (&it, 1);
1551 if (charpos < IT_CHARPOS (it)
1552 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1553 {
1554 visible_p = 1;
1555 RESTORE_IT (&it2, &it2, it2data);
1556 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1557 *x = it2.current_x;
1558 *y = it2.current_y + it2.max_ascent - it2.ascent;
1559 *rtop = max (0, -it2.current_y);
1560 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1561 - it.last_visible_y));
1562 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1563 it.last_visible_y)
1564 - max (it2.current_y,
1565 WINDOW_HEADER_LINE_HEIGHT (w))));
1566 *vpos = it2.vpos;
1567 }
1568 else
1569 bidi_unshelve_cache (it2data, 1);
1570 }
1571 bidi_unshelve_cache (itdata, 0);
1572
1573 if (old_buffer)
1574 set_buffer_internal_1 (old_buffer);
1575
1576 current_header_line_height = current_mode_line_height = -1;
1577
1578 if (visible_p && w->hscroll > 0)
1579 *x -=
1580 window_hscroll_limited (w, WINDOW_XFRAME (w))
1581 * WINDOW_FRAME_COLUMN_WIDTH (w);
1582
1583 #if 0
1584 /* Debugging code. */
1585 if (visible_p)
1586 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1587 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1588 else
1589 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1590 #endif
1591
1592 return visible_p;
1593 }
1594
1595
1596 /* Return the next character from STR. Return in *LEN the length of
1597 the character. This is like STRING_CHAR_AND_LENGTH but never
1598 returns an invalid character. If we find one, we return a `?', but
1599 with the length of the invalid character. */
1600
1601 static int
1602 string_char_and_length (const unsigned char *str, int *len)
1603 {
1604 int c;
1605
1606 c = STRING_CHAR_AND_LENGTH (str, *len);
1607 if (!CHAR_VALID_P (c))
1608 /* We may not change the length here because other places in Emacs
1609 don't use this function, i.e. they silently accept invalid
1610 characters. */
1611 c = '?';
1612
1613 return c;
1614 }
1615
1616
1617
1618 /* Given a position POS containing a valid character and byte position
1619 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1620
1621 static struct text_pos
1622 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1623 {
1624 eassert (STRINGP (string) && nchars >= 0);
1625
1626 if (STRING_MULTIBYTE (string))
1627 {
1628 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1629 int len;
1630
1631 while (nchars--)
1632 {
1633 string_char_and_length (p, &len);
1634 p += len;
1635 CHARPOS (pos) += 1;
1636 BYTEPOS (pos) += len;
1637 }
1638 }
1639 else
1640 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1641
1642 return pos;
1643 }
1644
1645
1646 /* Value is the text position, i.e. character and byte position,
1647 for character position CHARPOS in STRING. */
1648
1649 static struct text_pos
1650 string_pos (ptrdiff_t charpos, Lisp_Object string)
1651 {
1652 struct text_pos pos;
1653 eassert (STRINGP (string));
1654 eassert (charpos >= 0);
1655 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1656 return pos;
1657 }
1658
1659
1660 /* Value is a text position, i.e. character and byte position, for
1661 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1662 means recognize multibyte characters. */
1663
1664 static struct text_pos
1665 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1666 {
1667 struct text_pos pos;
1668
1669 eassert (s != NULL);
1670 eassert (charpos >= 0);
1671
1672 if (multibyte_p)
1673 {
1674 int len;
1675
1676 SET_TEXT_POS (pos, 0, 0);
1677 while (charpos--)
1678 {
1679 string_char_and_length ((const unsigned char *) s, &len);
1680 s += len;
1681 CHARPOS (pos) += 1;
1682 BYTEPOS (pos) += len;
1683 }
1684 }
1685 else
1686 SET_TEXT_POS (pos, charpos, charpos);
1687
1688 return pos;
1689 }
1690
1691
1692 /* Value is the number of characters in C string S. MULTIBYTE_P
1693 non-zero means recognize multibyte characters. */
1694
1695 static ptrdiff_t
1696 number_of_chars (const char *s, int multibyte_p)
1697 {
1698 ptrdiff_t nchars;
1699
1700 if (multibyte_p)
1701 {
1702 ptrdiff_t rest = strlen (s);
1703 int len;
1704 const unsigned char *p = (const unsigned char *) s;
1705
1706 for (nchars = 0; rest > 0; ++nchars)
1707 {
1708 string_char_and_length (p, &len);
1709 rest -= len, p += len;
1710 }
1711 }
1712 else
1713 nchars = strlen (s);
1714
1715 return nchars;
1716 }
1717
1718
1719 /* Compute byte position NEWPOS->bytepos corresponding to
1720 NEWPOS->charpos. POS is a known position in string STRING.
1721 NEWPOS->charpos must be >= POS.charpos. */
1722
1723 static void
1724 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1725 {
1726 eassert (STRINGP (string));
1727 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1728
1729 if (STRING_MULTIBYTE (string))
1730 *newpos = string_pos_nchars_ahead (pos, string,
1731 CHARPOS (*newpos) - CHARPOS (pos));
1732 else
1733 BYTEPOS (*newpos) = CHARPOS (*newpos);
1734 }
1735
1736 /* EXPORT:
1737 Return an estimation of the pixel height of mode or header lines on
1738 frame F. FACE_ID specifies what line's height to estimate. */
1739
1740 int
1741 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1742 {
1743 #ifdef HAVE_WINDOW_SYSTEM
1744 if (FRAME_WINDOW_P (f))
1745 {
1746 int height = FONT_HEIGHT (FRAME_FONT (f));
1747
1748 /* This function is called so early when Emacs starts that the face
1749 cache and mode line face are not yet initialized. */
1750 if (FRAME_FACE_CACHE (f))
1751 {
1752 struct face *face = FACE_FROM_ID (f, face_id);
1753 if (face)
1754 {
1755 if (face->font)
1756 height = FONT_HEIGHT (face->font);
1757 if (face->box_line_width > 0)
1758 height += 2 * face->box_line_width;
1759 }
1760 }
1761
1762 return height;
1763 }
1764 #endif
1765
1766 return 1;
1767 }
1768
1769 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1770 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1771 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1772 not force the value into range. */
1773
1774 void
1775 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1776 int *x, int *y, NativeRectangle *bounds, int noclip)
1777 {
1778
1779 #ifdef HAVE_WINDOW_SYSTEM
1780 if (FRAME_WINDOW_P (f))
1781 {
1782 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1783 even for negative values. */
1784 if (pix_x < 0)
1785 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1786 if (pix_y < 0)
1787 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1788
1789 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1790 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1791
1792 if (bounds)
1793 STORE_NATIVE_RECT (*bounds,
1794 FRAME_COL_TO_PIXEL_X (f, pix_x),
1795 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1796 FRAME_COLUMN_WIDTH (f) - 1,
1797 FRAME_LINE_HEIGHT (f) - 1);
1798
1799 if (!noclip)
1800 {
1801 if (pix_x < 0)
1802 pix_x = 0;
1803 else if (pix_x > FRAME_TOTAL_COLS (f))
1804 pix_x = FRAME_TOTAL_COLS (f);
1805
1806 if (pix_y < 0)
1807 pix_y = 0;
1808 else if (pix_y > FRAME_LINES (f))
1809 pix_y = FRAME_LINES (f);
1810 }
1811 }
1812 #endif
1813
1814 *x = pix_x;
1815 *y = pix_y;
1816 }
1817
1818
1819 /* Find the glyph under window-relative coordinates X/Y in window W.
1820 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1821 strings. Return in *HPOS and *VPOS the row and column number of
1822 the glyph found. Return in *AREA the glyph area containing X.
1823 Value is a pointer to the glyph found or null if X/Y is not on
1824 text, or we can't tell because W's current matrix is not up to
1825 date. */
1826
1827 static
1828 struct glyph *
1829 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1830 int *dx, int *dy, int *area)
1831 {
1832 struct glyph *glyph, *end;
1833 struct glyph_row *row = NULL;
1834 int x0, i;
1835
1836 /* Find row containing Y. Give up if some row is not enabled. */
1837 for (i = 0; i < w->current_matrix->nrows; ++i)
1838 {
1839 row = MATRIX_ROW (w->current_matrix, i);
1840 if (!row->enabled_p)
1841 return NULL;
1842 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1843 break;
1844 }
1845
1846 *vpos = i;
1847 *hpos = 0;
1848
1849 /* Give up if Y is not in the window. */
1850 if (i == w->current_matrix->nrows)
1851 return NULL;
1852
1853 /* Get the glyph area containing X. */
1854 if (w->pseudo_window_p)
1855 {
1856 *area = TEXT_AREA;
1857 x0 = 0;
1858 }
1859 else
1860 {
1861 if (x < window_box_left_offset (w, TEXT_AREA))
1862 {
1863 *area = LEFT_MARGIN_AREA;
1864 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1865 }
1866 else if (x < window_box_right_offset (w, TEXT_AREA))
1867 {
1868 *area = TEXT_AREA;
1869 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1870 }
1871 else
1872 {
1873 *area = RIGHT_MARGIN_AREA;
1874 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1875 }
1876 }
1877
1878 /* Find glyph containing X. */
1879 glyph = row->glyphs[*area];
1880 end = glyph + row->used[*area];
1881 x -= x0;
1882 while (glyph < end && x >= glyph->pixel_width)
1883 {
1884 x -= glyph->pixel_width;
1885 ++glyph;
1886 }
1887
1888 if (glyph == end)
1889 return NULL;
1890
1891 if (dx)
1892 {
1893 *dx = x;
1894 *dy = y - (row->y + row->ascent - glyph->ascent);
1895 }
1896
1897 *hpos = glyph - row->glyphs[*area];
1898 return glyph;
1899 }
1900
1901 /* Convert frame-relative x/y to coordinates relative to window W.
1902 Takes pseudo-windows into account. */
1903
1904 static void
1905 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1906 {
1907 if (w->pseudo_window_p)
1908 {
1909 /* A pseudo-window is always full-width, and starts at the
1910 left edge of the frame, plus a frame border. */
1911 struct frame *f = XFRAME (w->frame);
1912 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1913 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1914 }
1915 else
1916 {
1917 *x -= WINDOW_LEFT_EDGE_X (w);
1918 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1919 }
1920 }
1921
1922 #ifdef HAVE_WINDOW_SYSTEM
1923
1924 /* EXPORT:
1925 Return in RECTS[] at most N clipping rectangles for glyph string S.
1926 Return the number of stored rectangles. */
1927
1928 int
1929 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1930 {
1931 XRectangle r;
1932
1933 if (n <= 0)
1934 return 0;
1935
1936 if (s->row->full_width_p)
1937 {
1938 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1939 r.x = WINDOW_LEFT_EDGE_X (s->w);
1940 r.width = WINDOW_TOTAL_WIDTH (s->w);
1941
1942 /* Unless displaying a mode or menu bar line, which are always
1943 fully visible, clip to the visible part of the row. */
1944 if (s->w->pseudo_window_p)
1945 r.height = s->row->visible_height;
1946 else
1947 r.height = s->height;
1948 }
1949 else
1950 {
1951 /* This is a text line that may be partially visible. */
1952 r.x = window_box_left (s->w, s->area);
1953 r.width = window_box_width (s->w, s->area);
1954 r.height = s->row->visible_height;
1955 }
1956
1957 if (s->clip_head)
1958 if (r.x < s->clip_head->x)
1959 {
1960 if (r.width >= s->clip_head->x - r.x)
1961 r.width -= s->clip_head->x - r.x;
1962 else
1963 r.width = 0;
1964 r.x = s->clip_head->x;
1965 }
1966 if (s->clip_tail)
1967 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1968 {
1969 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1970 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1971 else
1972 r.width = 0;
1973 }
1974
1975 /* If S draws overlapping rows, it's sufficient to use the top and
1976 bottom of the window for clipping because this glyph string
1977 intentionally draws over other lines. */
1978 if (s->for_overlaps)
1979 {
1980 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
1981 r.height = window_text_bottom_y (s->w) - r.y;
1982
1983 /* Alas, the above simple strategy does not work for the
1984 environments with anti-aliased text: if the same text is
1985 drawn onto the same place multiple times, it gets thicker.
1986 If the overlap we are processing is for the erased cursor, we
1987 take the intersection with the rectangle of the cursor. */
1988 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
1989 {
1990 XRectangle rc, r_save = r;
1991
1992 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
1993 rc.y = s->w->phys_cursor.y;
1994 rc.width = s->w->phys_cursor_width;
1995 rc.height = s->w->phys_cursor_height;
1996
1997 x_intersect_rectangles (&r_save, &rc, &r);
1998 }
1999 }
2000 else
2001 {
2002 /* Don't use S->y for clipping because it doesn't take partially
2003 visible lines into account. For example, it can be negative for
2004 partially visible lines at the top of a window. */
2005 if (!s->row->full_width_p
2006 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2007 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2008 else
2009 r.y = max (0, s->row->y);
2010 }
2011
2012 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2013
2014 /* If drawing the cursor, don't let glyph draw outside its
2015 advertised boundaries. Cleartype does this under some circumstances. */
2016 if (s->hl == DRAW_CURSOR)
2017 {
2018 struct glyph *glyph = s->first_glyph;
2019 int height, max_y;
2020
2021 if (s->x > r.x)
2022 {
2023 r.width -= s->x - r.x;
2024 r.x = s->x;
2025 }
2026 r.width = min (r.width, glyph->pixel_width);
2027
2028 /* If r.y is below window bottom, ensure that we still see a cursor. */
2029 height = min (glyph->ascent + glyph->descent,
2030 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2031 max_y = window_text_bottom_y (s->w) - height;
2032 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2033 if (s->ybase - glyph->ascent > max_y)
2034 {
2035 r.y = max_y;
2036 r.height = height;
2037 }
2038 else
2039 {
2040 /* Don't draw cursor glyph taller than our actual glyph. */
2041 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2042 if (height < r.height)
2043 {
2044 max_y = r.y + r.height;
2045 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2046 r.height = min (max_y - r.y, height);
2047 }
2048 }
2049 }
2050
2051 if (s->row->clip)
2052 {
2053 XRectangle r_save = r;
2054
2055 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2056 r.width = 0;
2057 }
2058
2059 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2060 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2061 {
2062 #ifdef CONVERT_FROM_XRECT
2063 CONVERT_FROM_XRECT (r, *rects);
2064 #else
2065 *rects = r;
2066 #endif
2067 return 1;
2068 }
2069 else
2070 {
2071 /* If we are processing overlapping and allowed to return
2072 multiple clipping rectangles, we exclude the row of the glyph
2073 string from the clipping rectangle. This is to avoid drawing
2074 the same text on the environment with anti-aliasing. */
2075 #ifdef CONVERT_FROM_XRECT
2076 XRectangle rs[2];
2077 #else
2078 XRectangle *rs = rects;
2079 #endif
2080 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2081
2082 if (s->for_overlaps & OVERLAPS_PRED)
2083 {
2084 rs[i] = r;
2085 if (r.y + r.height > row_y)
2086 {
2087 if (r.y < row_y)
2088 rs[i].height = row_y - r.y;
2089 else
2090 rs[i].height = 0;
2091 }
2092 i++;
2093 }
2094 if (s->for_overlaps & OVERLAPS_SUCC)
2095 {
2096 rs[i] = r;
2097 if (r.y < row_y + s->row->visible_height)
2098 {
2099 if (r.y + r.height > row_y + s->row->visible_height)
2100 {
2101 rs[i].y = row_y + s->row->visible_height;
2102 rs[i].height = r.y + r.height - rs[i].y;
2103 }
2104 else
2105 rs[i].height = 0;
2106 }
2107 i++;
2108 }
2109
2110 n = i;
2111 #ifdef CONVERT_FROM_XRECT
2112 for (i = 0; i < n; i++)
2113 CONVERT_FROM_XRECT (rs[i], rects[i]);
2114 #endif
2115 return n;
2116 }
2117 }
2118
2119 /* EXPORT:
2120 Return in *NR the clipping rectangle for glyph string S. */
2121
2122 void
2123 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2124 {
2125 get_glyph_string_clip_rects (s, nr, 1);
2126 }
2127
2128
2129 /* EXPORT:
2130 Return the position and height of the phys cursor in window W.
2131 Set w->phys_cursor_width to width of phys cursor.
2132 */
2133
2134 void
2135 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2136 struct glyph *glyph, int *xp, int *yp, int *heightp)
2137 {
2138 struct frame *f = XFRAME (WINDOW_FRAME (w));
2139 int x, y, wd, h, h0, y0;
2140
2141 /* Compute the width of the rectangle to draw. If on a stretch
2142 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2143 rectangle as wide as the glyph, but use a canonical character
2144 width instead. */
2145 wd = glyph->pixel_width - 1;
2146 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2147 wd++; /* Why? */
2148 #endif
2149
2150 x = w->phys_cursor.x;
2151 if (x < 0)
2152 {
2153 wd += x;
2154 x = 0;
2155 }
2156
2157 if (glyph->type == STRETCH_GLYPH
2158 && !x_stretch_cursor_p)
2159 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2160 w->phys_cursor_width = wd;
2161
2162 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2163
2164 /* If y is below window bottom, ensure that we still see a cursor. */
2165 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2166
2167 h = max (h0, glyph->ascent + glyph->descent);
2168 h0 = min (h0, glyph->ascent + glyph->descent);
2169
2170 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2171 if (y < y0)
2172 {
2173 h = max (h - (y0 - y) + 1, h0);
2174 y = y0 - 1;
2175 }
2176 else
2177 {
2178 y0 = window_text_bottom_y (w) - h0;
2179 if (y > y0)
2180 {
2181 h += y - y0;
2182 y = y0;
2183 }
2184 }
2185
2186 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2187 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2188 *heightp = h;
2189 }
2190
2191 /*
2192 * Remember which glyph the mouse is over.
2193 */
2194
2195 void
2196 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2197 {
2198 Lisp_Object window;
2199 struct window *w;
2200 struct glyph_row *r, *gr, *end_row;
2201 enum window_part part;
2202 enum glyph_row_area area;
2203 int x, y, width, height;
2204
2205 /* Try to determine frame pixel position and size of the glyph under
2206 frame pixel coordinates X/Y on frame F. */
2207
2208 if (!f->glyphs_initialized_p
2209 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2210 NILP (window)))
2211 {
2212 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2213 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2214 goto virtual_glyph;
2215 }
2216
2217 w = XWINDOW (window);
2218 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2219 height = WINDOW_FRAME_LINE_HEIGHT (w);
2220
2221 x = window_relative_x_coord (w, part, gx);
2222 y = gy - WINDOW_TOP_EDGE_Y (w);
2223
2224 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2225 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2226
2227 if (w->pseudo_window_p)
2228 {
2229 area = TEXT_AREA;
2230 part = ON_MODE_LINE; /* Don't adjust margin. */
2231 goto text_glyph;
2232 }
2233
2234 switch (part)
2235 {
2236 case ON_LEFT_MARGIN:
2237 area = LEFT_MARGIN_AREA;
2238 goto text_glyph;
2239
2240 case ON_RIGHT_MARGIN:
2241 area = RIGHT_MARGIN_AREA;
2242 goto text_glyph;
2243
2244 case ON_HEADER_LINE:
2245 case ON_MODE_LINE:
2246 gr = (part == ON_HEADER_LINE
2247 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2248 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2249 gy = gr->y;
2250 area = TEXT_AREA;
2251 goto text_glyph_row_found;
2252
2253 case ON_TEXT:
2254 area = TEXT_AREA;
2255
2256 text_glyph:
2257 gr = 0; gy = 0;
2258 for (; r <= end_row && r->enabled_p; ++r)
2259 if (r->y + r->height > y)
2260 {
2261 gr = r; gy = r->y;
2262 break;
2263 }
2264
2265 text_glyph_row_found:
2266 if (gr && gy <= y)
2267 {
2268 struct glyph *g = gr->glyphs[area];
2269 struct glyph *end = g + gr->used[area];
2270
2271 height = gr->height;
2272 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2273 if (gx + g->pixel_width > x)
2274 break;
2275
2276 if (g < end)
2277 {
2278 if (g->type == IMAGE_GLYPH)
2279 {
2280 /* Don't remember when mouse is over image, as
2281 image may have hot-spots. */
2282 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2283 return;
2284 }
2285 width = g->pixel_width;
2286 }
2287 else
2288 {
2289 /* Use nominal char spacing at end of line. */
2290 x -= gx;
2291 gx += (x / width) * width;
2292 }
2293
2294 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2295 gx += window_box_left_offset (w, area);
2296 }
2297 else
2298 {
2299 /* Use nominal line height at end of window. */
2300 gx = (x / width) * width;
2301 y -= gy;
2302 gy += (y / height) * height;
2303 }
2304 break;
2305
2306 case ON_LEFT_FRINGE:
2307 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2308 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2309 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2310 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2311 goto row_glyph;
2312
2313 case ON_RIGHT_FRINGE:
2314 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2315 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2316 : window_box_right_offset (w, TEXT_AREA));
2317 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2318 goto row_glyph;
2319
2320 case ON_SCROLL_BAR:
2321 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2322 ? 0
2323 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2324 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2325 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2326 : 0)));
2327 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2328
2329 row_glyph:
2330 gr = 0, gy = 0;
2331 for (; r <= end_row && r->enabled_p; ++r)
2332 if (r->y + r->height > y)
2333 {
2334 gr = r; gy = r->y;
2335 break;
2336 }
2337
2338 if (gr && gy <= y)
2339 height = gr->height;
2340 else
2341 {
2342 /* Use nominal line height at end of window. */
2343 y -= gy;
2344 gy += (y / height) * height;
2345 }
2346 break;
2347
2348 default:
2349 ;
2350 virtual_glyph:
2351 /* If there is no glyph under the mouse, then we divide the screen
2352 into a grid of the smallest glyph in the frame, and use that
2353 as our "glyph". */
2354
2355 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2356 round down even for negative values. */
2357 if (gx < 0)
2358 gx -= width - 1;
2359 if (gy < 0)
2360 gy -= height - 1;
2361
2362 gx = (gx / width) * width;
2363 gy = (gy / height) * height;
2364
2365 goto store_rect;
2366 }
2367
2368 gx += WINDOW_LEFT_EDGE_X (w);
2369 gy += WINDOW_TOP_EDGE_Y (w);
2370
2371 store_rect:
2372 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2373
2374 /* Visible feedback for debugging. */
2375 #if 0
2376 #if HAVE_X_WINDOWS
2377 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2378 f->output_data.x->normal_gc,
2379 gx, gy, width, height);
2380 #endif
2381 #endif
2382 }
2383
2384
2385 #endif /* HAVE_WINDOW_SYSTEM */
2386
2387 \f
2388 /***********************************************************************
2389 Lisp form evaluation
2390 ***********************************************************************/
2391
2392 /* Error handler for safe_eval and safe_call. */
2393
2394 static Lisp_Object
2395 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2396 {
2397 add_to_log ("Error during redisplay: %S signaled %S",
2398 Flist (nargs, args), arg);
2399 return Qnil;
2400 }
2401
2402 /* Call function FUNC with the rest of NARGS - 1 arguments
2403 following. Return the result, or nil if something went
2404 wrong. Prevent redisplay during the evaluation. */
2405
2406 Lisp_Object
2407 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2408 {
2409 Lisp_Object val;
2410
2411 if (inhibit_eval_during_redisplay)
2412 val = Qnil;
2413 else
2414 {
2415 va_list ap;
2416 ptrdiff_t i;
2417 ptrdiff_t count = SPECPDL_INDEX ();
2418 struct gcpro gcpro1;
2419 Lisp_Object *args = alloca (nargs * word_size);
2420
2421 args[0] = func;
2422 va_start (ap, func);
2423 for (i = 1; i < nargs; i++)
2424 args[i] = va_arg (ap, Lisp_Object);
2425 va_end (ap);
2426
2427 GCPRO1 (args[0]);
2428 gcpro1.nvars = nargs;
2429 specbind (Qinhibit_redisplay, Qt);
2430 /* Use Qt to ensure debugger does not run,
2431 so there is no possibility of wanting to redisplay. */
2432 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2433 safe_eval_handler);
2434 UNGCPRO;
2435 val = unbind_to (count, val);
2436 }
2437
2438 return val;
2439 }
2440
2441
2442 /* Call function FN with one argument ARG.
2443 Return the result, or nil if something went wrong. */
2444
2445 Lisp_Object
2446 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2447 {
2448 return safe_call (2, fn, arg);
2449 }
2450
2451 static Lisp_Object Qeval;
2452
2453 Lisp_Object
2454 safe_eval (Lisp_Object sexpr)
2455 {
2456 return safe_call1 (Qeval, sexpr);
2457 }
2458
2459 /* Call function FN with two arguments ARG1 and ARG2.
2460 Return the result, or nil if something went wrong. */
2461
2462 Lisp_Object
2463 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2464 {
2465 return safe_call (3, fn, arg1, arg2);
2466 }
2467
2468
2469 \f
2470 /***********************************************************************
2471 Debugging
2472 ***********************************************************************/
2473
2474 #if 0
2475
2476 /* Define CHECK_IT to perform sanity checks on iterators.
2477 This is for debugging. It is too slow to do unconditionally. */
2478
2479 static void
2480 check_it (struct it *it)
2481 {
2482 if (it->method == GET_FROM_STRING)
2483 {
2484 eassert (STRINGP (it->string));
2485 eassert (IT_STRING_CHARPOS (*it) >= 0);
2486 }
2487 else
2488 {
2489 eassert (IT_STRING_CHARPOS (*it) < 0);
2490 if (it->method == GET_FROM_BUFFER)
2491 {
2492 /* Check that character and byte positions agree. */
2493 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2494 }
2495 }
2496
2497 if (it->dpvec)
2498 eassert (it->current.dpvec_index >= 0);
2499 else
2500 eassert (it->current.dpvec_index < 0);
2501 }
2502
2503 #define CHECK_IT(IT) check_it ((IT))
2504
2505 #else /* not 0 */
2506
2507 #define CHECK_IT(IT) (void) 0
2508
2509 #endif /* not 0 */
2510
2511
2512 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2513
2514 /* Check that the window end of window W is what we expect it
2515 to be---the last row in the current matrix displaying text. */
2516
2517 static void
2518 check_window_end (struct window *w)
2519 {
2520 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2521 {
2522 struct glyph_row *row;
2523 eassert ((row = MATRIX_ROW (w->current_matrix,
2524 XFASTINT (w->window_end_vpos)),
2525 !row->enabled_p
2526 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2527 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2528 }
2529 }
2530
2531 #define CHECK_WINDOW_END(W) check_window_end ((W))
2532
2533 #else
2534
2535 #define CHECK_WINDOW_END(W) (void) 0
2536
2537 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2538
2539 /* Return mark position if current buffer has the region of non-zero length,
2540 or -1 otherwise. */
2541
2542 static ptrdiff_t
2543 markpos_of_region (void)
2544 {
2545 if (!NILP (Vtransient_mark_mode)
2546 && !NILP (BVAR (current_buffer, mark_active))
2547 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2548 {
2549 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2550
2551 if (markpos != PT)
2552 return markpos;
2553 }
2554 return -1;
2555 }
2556
2557 /***********************************************************************
2558 Iterator initialization
2559 ***********************************************************************/
2560
2561 /* Initialize IT for displaying current_buffer in window W, starting
2562 at character position CHARPOS. CHARPOS < 0 means that no buffer
2563 position is specified which is useful when the iterator is assigned
2564 a position later. BYTEPOS is the byte position corresponding to
2565 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2566
2567 If ROW is not null, calls to produce_glyphs with IT as parameter
2568 will produce glyphs in that row.
2569
2570 BASE_FACE_ID is the id of a base face to use. It must be one of
2571 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2572 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2573 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2574
2575 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2576 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2577 will be initialized to use the corresponding mode line glyph row of
2578 the desired matrix of W. */
2579
2580 void
2581 init_iterator (struct it *it, struct window *w,
2582 ptrdiff_t charpos, ptrdiff_t bytepos,
2583 struct glyph_row *row, enum face_id base_face_id)
2584 {
2585 ptrdiff_t markpos;
2586 enum face_id remapped_base_face_id = base_face_id;
2587
2588 /* Some precondition checks. */
2589 eassert (w != NULL && it != NULL);
2590 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2591 && charpos <= ZV));
2592
2593 /* If face attributes have been changed since the last redisplay,
2594 free realized faces now because they depend on face definitions
2595 that might have changed. Don't free faces while there might be
2596 desired matrices pending which reference these faces. */
2597 if (face_change_count && !inhibit_free_realized_faces)
2598 {
2599 face_change_count = 0;
2600 free_all_realized_faces (Qnil);
2601 }
2602
2603 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2604 if (! NILP (Vface_remapping_alist))
2605 remapped_base_face_id
2606 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2607
2608 /* Use one of the mode line rows of W's desired matrix if
2609 appropriate. */
2610 if (row == NULL)
2611 {
2612 if (base_face_id == MODE_LINE_FACE_ID
2613 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2614 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2615 else if (base_face_id == HEADER_LINE_FACE_ID)
2616 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2617 }
2618
2619 /* Clear IT. */
2620 memset (it, 0, sizeof *it);
2621 it->current.overlay_string_index = -1;
2622 it->current.dpvec_index = -1;
2623 it->base_face_id = remapped_base_face_id;
2624 it->string = Qnil;
2625 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2626 it->paragraph_embedding = L2R;
2627 it->bidi_it.string.lstring = Qnil;
2628 it->bidi_it.string.s = NULL;
2629 it->bidi_it.string.bufpos = 0;
2630
2631 /* The window in which we iterate over current_buffer: */
2632 XSETWINDOW (it->window, w);
2633 it->w = w;
2634 it->f = XFRAME (w->frame);
2635
2636 it->cmp_it.id = -1;
2637
2638 /* Extra space between lines (on window systems only). */
2639 if (base_face_id == DEFAULT_FACE_ID
2640 && FRAME_WINDOW_P (it->f))
2641 {
2642 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2643 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2644 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2645 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2646 * FRAME_LINE_HEIGHT (it->f));
2647 else if (it->f->extra_line_spacing > 0)
2648 it->extra_line_spacing = it->f->extra_line_spacing;
2649 it->max_extra_line_spacing = 0;
2650 }
2651
2652 /* If realized faces have been removed, e.g. because of face
2653 attribute changes of named faces, recompute them. When running
2654 in batch mode, the face cache of the initial frame is null. If
2655 we happen to get called, make a dummy face cache. */
2656 if (FRAME_FACE_CACHE (it->f) == NULL)
2657 init_frame_faces (it->f);
2658 if (FRAME_FACE_CACHE (it->f)->used == 0)
2659 recompute_basic_faces (it->f);
2660
2661 /* Current value of the `slice', `space-width', and 'height' properties. */
2662 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2663 it->space_width = Qnil;
2664 it->font_height = Qnil;
2665 it->override_ascent = -1;
2666
2667 /* Are control characters displayed as `^C'? */
2668 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2669
2670 /* -1 means everything between a CR and the following line end
2671 is invisible. >0 means lines indented more than this value are
2672 invisible. */
2673 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2674 ? (clip_to_bounds
2675 (-1, XINT (BVAR (current_buffer, selective_display)),
2676 PTRDIFF_MAX))
2677 : (!NILP (BVAR (current_buffer, selective_display))
2678 ? -1 : 0));
2679 it->selective_display_ellipsis_p
2680 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2681
2682 /* Display table to use. */
2683 it->dp = window_display_table (w);
2684
2685 /* Are multibyte characters enabled in current_buffer? */
2686 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2687
2688 /* If visible region is of non-zero length, set IT->region_beg_charpos
2689 and IT->region_end_charpos to the start and end of a visible region
2690 in window IT->w. Set both to -1 to indicate no region. */
2691 markpos = markpos_of_region ();
2692 if (0 <= markpos
2693 /* Maybe highlight only in selected window. */
2694 && (/* Either show region everywhere. */
2695 highlight_nonselected_windows
2696 /* Or show region in the selected window. */
2697 || w == XWINDOW (selected_window)
2698 /* Or show the region if we are in the mini-buffer and W is
2699 the window the mini-buffer refers to. */
2700 || (MINI_WINDOW_P (XWINDOW (selected_window))
2701 && WINDOWP (minibuf_selected_window)
2702 && w == XWINDOW (minibuf_selected_window))))
2703 {
2704 it->region_beg_charpos = min (PT, markpos);
2705 it->region_end_charpos = max (PT, markpos);
2706 }
2707 else
2708 it->region_beg_charpos = it->region_end_charpos = -1;
2709
2710 /* Get the position at which the redisplay_end_trigger hook should
2711 be run, if it is to be run at all. */
2712 if (MARKERP (w->redisplay_end_trigger)
2713 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2714 it->redisplay_end_trigger_charpos
2715 = marker_position (w->redisplay_end_trigger);
2716 else if (INTEGERP (w->redisplay_end_trigger))
2717 it->redisplay_end_trigger_charpos =
2718 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2719
2720 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2721
2722 /* Are lines in the display truncated? */
2723 if (base_face_id != DEFAULT_FACE_ID
2724 || it->w->hscroll
2725 || (! WINDOW_FULL_WIDTH_P (it->w)
2726 && ((!NILP (Vtruncate_partial_width_windows)
2727 && !INTEGERP (Vtruncate_partial_width_windows))
2728 || (INTEGERP (Vtruncate_partial_width_windows)
2729 && (WINDOW_TOTAL_COLS (it->w)
2730 < XINT (Vtruncate_partial_width_windows))))))
2731 it->line_wrap = TRUNCATE;
2732 else if (NILP (BVAR (current_buffer, truncate_lines)))
2733 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2734 ? WINDOW_WRAP : WORD_WRAP;
2735 else
2736 it->line_wrap = TRUNCATE;
2737
2738 /* Get dimensions of truncation and continuation glyphs. These are
2739 displayed as fringe bitmaps under X, but we need them for such
2740 frames when the fringes are turned off. But leave the dimensions
2741 zero for tooltip frames, as these glyphs look ugly there and also
2742 sabotage calculations of tooltip dimensions in x-show-tip. */
2743 #ifdef HAVE_WINDOW_SYSTEM
2744 if (!(FRAME_WINDOW_P (it->f)
2745 && FRAMEP (tip_frame)
2746 && it->f == XFRAME (tip_frame)))
2747 #endif
2748 {
2749 if (it->line_wrap == TRUNCATE)
2750 {
2751 /* We will need the truncation glyph. */
2752 eassert (it->glyph_row == NULL);
2753 produce_special_glyphs (it, IT_TRUNCATION);
2754 it->truncation_pixel_width = it->pixel_width;
2755 }
2756 else
2757 {
2758 /* We will need the continuation glyph. */
2759 eassert (it->glyph_row == NULL);
2760 produce_special_glyphs (it, IT_CONTINUATION);
2761 it->continuation_pixel_width = it->pixel_width;
2762 }
2763 }
2764
2765 /* Reset these values to zero because the produce_special_glyphs
2766 above has changed them. */
2767 it->pixel_width = it->ascent = it->descent = 0;
2768 it->phys_ascent = it->phys_descent = 0;
2769
2770 /* Set this after getting the dimensions of truncation and
2771 continuation glyphs, so that we don't produce glyphs when calling
2772 produce_special_glyphs, above. */
2773 it->glyph_row = row;
2774 it->area = TEXT_AREA;
2775
2776 /* Forget any previous info about this row being reversed. */
2777 if (it->glyph_row)
2778 it->glyph_row->reversed_p = 0;
2779
2780 /* Get the dimensions of the display area. The display area
2781 consists of the visible window area plus a horizontally scrolled
2782 part to the left of the window. All x-values are relative to the
2783 start of this total display area. */
2784 if (base_face_id != DEFAULT_FACE_ID)
2785 {
2786 /* Mode lines, menu bar in terminal frames. */
2787 it->first_visible_x = 0;
2788 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2789 }
2790 else
2791 {
2792 it->first_visible_x =
2793 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2794 it->last_visible_x = (it->first_visible_x
2795 + window_box_width (w, TEXT_AREA));
2796
2797 /* If we truncate lines, leave room for the truncation glyph(s) at
2798 the right margin. Otherwise, leave room for the continuation
2799 glyph(s). Done only if the window has no fringes. Since we
2800 don't know at this point whether there will be any R2L lines in
2801 the window, we reserve space for truncation/continuation glyphs
2802 even if only one of the fringes is absent. */
2803 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2804 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2805 {
2806 if (it->line_wrap == TRUNCATE)
2807 it->last_visible_x -= it->truncation_pixel_width;
2808 else
2809 it->last_visible_x -= it->continuation_pixel_width;
2810 }
2811
2812 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2813 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2814 }
2815
2816 /* Leave room for a border glyph. */
2817 if (!FRAME_WINDOW_P (it->f)
2818 && !WINDOW_RIGHTMOST_P (it->w))
2819 it->last_visible_x -= 1;
2820
2821 it->last_visible_y = window_text_bottom_y (w);
2822
2823 /* For mode lines and alike, arrange for the first glyph having a
2824 left box line if the face specifies a box. */
2825 if (base_face_id != DEFAULT_FACE_ID)
2826 {
2827 struct face *face;
2828
2829 it->face_id = remapped_base_face_id;
2830
2831 /* If we have a boxed mode line, make the first character appear
2832 with a left box line. */
2833 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2834 if (face->box != FACE_NO_BOX)
2835 it->start_of_box_run_p = 1;
2836 }
2837
2838 /* If a buffer position was specified, set the iterator there,
2839 getting overlays and face properties from that position. */
2840 if (charpos >= BUF_BEG (current_buffer))
2841 {
2842 it->end_charpos = ZV;
2843 IT_CHARPOS (*it) = charpos;
2844
2845 /* We will rely on `reseat' to set this up properly, via
2846 handle_face_prop. */
2847 it->face_id = it->base_face_id;
2848
2849 /* Compute byte position if not specified. */
2850 if (bytepos < charpos)
2851 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2852 else
2853 IT_BYTEPOS (*it) = bytepos;
2854
2855 it->start = it->current;
2856 /* Do we need to reorder bidirectional text? Not if this is a
2857 unibyte buffer: by definition, none of the single-byte
2858 characters are strong R2L, so no reordering is needed. And
2859 bidi.c doesn't support unibyte buffers anyway. Also, don't
2860 reorder while we are loading loadup.el, since the tables of
2861 character properties needed for reordering are not yet
2862 available. */
2863 it->bidi_p =
2864 NILP (Vpurify_flag)
2865 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2866 && it->multibyte_p;
2867
2868 /* If we are to reorder bidirectional text, init the bidi
2869 iterator. */
2870 if (it->bidi_p)
2871 {
2872 /* Note the paragraph direction that this buffer wants to
2873 use. */
2874 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2875 Qleft_to_right))
2876 it->paragraph_embedding = L2R;
2877 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2878 Qright_to_left))
2879 it->paragraph_embedding = R2L;
2880 else
2881 it->paragraph_embedding = NEUTRAL_DIR;
2882 bidi_unshelve_cache (NULL, 0);
2883 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2884 &it->bidi_it);
2885 }
2886
2887 /* Compute faces etc. */
2888 reseat (it, it->current.pos, 1);
2889 }
2890
2891 CHECK_IT (it);
2892 }
2893
2894
2895 /* Initialize IT for the display of window W with window start POS. */
2896
2897 void
2898 start_display (struct it *it, struct window *w, struct text_pos pos)
2899 {
2900 struct glyph_row *row;
2901 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2902
2903 row = w->desired_matrix->rows + first_vpos;
2904 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2905 it->first_vpos = first_vpos;
2906
2907 /* Don't reseat to previous visible line start if current start
2908 position is in a string or image. */
2909 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2910 {
2911 int start_at_line_beg_p;
2912 int first_y = it->current_y;
2913
2914 /* If window start is not at a line start, skip forward to POS to
2915 get the correct continuation lines width. */
2916 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2917 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2918 if (!start_at_line_beg_p)
2919 {
2920 int new_x;
2921
2922 reseat_at_previous_visible_line_start (it);
2923 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2924
2925 new_x = it->current_x + it->pixel_width;
2926
2927 /* If lines are continued, this line may end in the middle
2928 of a multi-glyph character (e.g. a control character
2929 displayed as \003, or in the middle of an overlay
2930 string). In this case move_it_to above will not have
2931 taken us to the start of the continuation line but to the
2932 end of the continued line. */
2933 if (it->current_x > 0
2934 && it->line_wrap != TRUNCATE /* Lines are continued. */
2935 && (/* And glyph doesn't fit on the line. */
2936 new_x > it->last_visible_x
2937 /* Or it fits exactly and we're on a window
2938 system frame. */
2939 || (new_x == it->last_visible_x
2940 && FRAME_WINDOW_P (it->f)
2941 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2942 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2943 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2944 {
2945 if ((it->current.dpvec_index >= 0
2946 || it->current.overlay_string_index >= 0)
2947 /* If we are on a newline from a display vector or
2948 overlay string, then we are already at the end of
2949 a screen line; no need to go to the next line in
2950 that case, as this line is not really continued.
2951 (If we do go to the next line, C-e will not DTRT.) */
2952 && it->c != '\n')
2953 {
2954 set_iterator_to_next (it, 1);
2955 move_it_in_display_line_to (it, -1, -1, 0);
2956 }
2957
2958 it->continuation_lines_width += it->current_x;
2959 }
2960 /* If the character at POS is displayed via a display
2961 vector, move_it_to above stops at the final glyph of
2962 IT->dpvec. To make the caller redisplay that character
2963 again (a.k.a. start at POS), we need to reset the
2964 dpvec_index to the beginning of IT->dpvec. */
2965 else if (it->current.dpvec_index >= 0)
2966 it->current.dpvec_index = 0;
2967
2968 /* We're starting a new display line, not affected by the
2969 height of the continued line, so clear the appropriate
2970 fields in the iterator structure. */
2971 it->max_ascent = it->max_descent = 0;
2972 it->max_phys_ascent = it->max_phys_descent = 0;
2973
2974 it->current_y = first_y;
2975 it->vpos = 0;
2976 it->current_x = it->hpos = 0;
2977 }
2978 }
2979 }
2980
2981
2982 /* Return 1 if POS is a position in ellipses displayed for invisible
2983 text. W is the window we display, for text property lookup. */
2984
2985 static int
2986 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
2987 {
2988 Lisp_Object prop, window;
2989 int ellipses_p = 0;
2990 ptrdiff_t charpos = CHARPOS (pos->pos);
2991
2992 /* If POS specifies a position in a display vector, this might
2993 be for an ellipsis displayed for invisible text. We won't
2994 get the iterator set up for delivering that ellipsis unless
2995 we make sure that it gets aware of the invisible text. */
2996 if (pos->dpvec_index >= 0
2997 && pos->overlay_string_index < 0
2998 && CHARPOS (pos->string_pos) < 0
2999 && charpos > BEGV
3000 && (XSETWINDOW (window, w),
3001 prop = Fget_char_property (make_number (charpos),
3002 Qinvisible, window),
3003 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3004 {
3005 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3006 window);
3007 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3008 }
3009
3010 return ellipses_p;
3011 }
3012
3013
3014 /* Initialize IT for stepping through current_buffer in window W,
3015 starting at position POS that includes overlay string and display
3016 vector/ control character translation position information. Value
3017 is zero if there are overlay strings with newlines at POS. */
3018
3019 static int
3020 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3021 {
3022 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3023 int i, overlay_strings_with_newlines = 0;
3024
3025 /* If POS specifies a position in a display vector, this might
3026 be for an ellipsis displayed for invisible text. We won't
3027 get the iterator set up for delivering that ellipsis unless
3028 we make sure that it gets aware of the invisible text. */
3029 if (in_ellipses_for_invisible_text_p (pos, w))
3030 {
3031 --charpos;
3032 bytepos = 0;
3033 }
3034
3035 /* Keep in mind: the call to reseat in init_iterator skips invisible
3036 text, so we might end up at a position different from POS. This
3037 is only a problem when POS is a row start after a newline and an
3038 overlay starts there with an after-string, and the overlay has an
3039 invisible property. Since we don't skip invisible text in
3040 display_line and elsewhere immediately after consuming the
3041 newline before the row start, such a POS will not be in a string,
3042 but the call to init_iterator below will move us to the
3043 after-string. */
3044 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3045
3046 /* This only scans the current chunk -- it should scan all chunks.
3047 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3048 to 16 in 22.1 to make this a lesser problem. */
3049 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3050 {
3051 const char *s = SSDATA (it->overlay_strings[i]);
3052 const char *e = s + SBYTES (it->overlay_strings[i]);
3053
3054 while (s < e && *s != '\n')
3055 ++s;
3056
3057 if (s < e)
3058 {
3059 overlay_strings_with_newlines = 1;
3060 break;
3061 }
3062 }
3063
3064 /* If position is within an overlay string, set up IT to the right
3065 overlay string. */
3066 if (pos->overlay_string_index >= 0)
3067 {
3068 int relative_index;
3069
3070 /* If the first overlay string happens to have a `display'
3071 property for an image, the iterator will be set up for that
3072 image, and we have to undo that setup first before we can
3073 correct the overlay string index. */
3074 if (it->method == GET_FROM_IMAGE)
3075 pop_it (it);
3076
3077 /* We already have the first chunk of overlay strings in
3078 IT->overlay_strings. Load more until the one for
3079 pos->overlay_string_index is in IT->overlay_strings. */
3080 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3081 {
3082 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3083 it->current.overlay_string_index = 0;
3084 while (n--)
3085 {
3086 load_overlay_strings (it, 0);
3087 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3088 }
3089 }
3090
3091 it->current.overlay_string_index = pos->overlay_string_index;
3092 relative_index = (it->current.overlay_string_index
3093 % OVERLAY_STRING_CHUNK_SIZE);
3094 it->string = it->overlay_strings[relative_index];
3095 eassert (STRINGP (it->string));
3096 it->current.string_pos = pos->string_pos;
3097 it->method = GET_FROM_STRING;
3098 it->end_charpos = SCHARS (it->string);
3099 /* Set up the bidi iterator for this overlay string. */
3100 if (it->bidi_p)
3101 {
3102 it->bidi_it.string.lstring = it->string;
3103 it->bidi_it.string.s = NULL;
3104 it->bidi_it.string.schars = SCHARS (it->string);
3105 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3106 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3107 it->bidi_it.string.unibyte = !it->multibyte_p;
3108 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3109 FRAME_WINDOW_P (it->f), &it->bidi_it);
3110
3111 /* Synchronize the state of the bidi iterator with
3112 pos->string_pos. For any string position other than
3113 zero, this will be done automagically when we resume
3114 iteration over the string and get_visually_first_element
3115 is called. But if string_pos is zero, and the string is
3116 to be reordered for display, we need to resync manually,
3117 since it could be that the iteration state recorded in
3118 pos ended at string_pos of 0 moving backwards in string. */
3119 if (CHARPOS (pos->string_pos) == 0)
3120 {
3121 get_visually_first_element (it);
3122 if (IT_STRING_CHARPOS (*it) != 0)
3123 do {
3124 /* Paranoia. */
3125 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3126 bidi_move_to_visually_next (&it->bidi_it);
3127 } while (it->bidi_it.charpos != 0);
3128 }
3129 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3130 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3131 }
3132 }
3133
3134 if (CHARPOS (pos->string_pos) >= 0)
3135 {
3136 /* Recorded position is not in an overlay string, but in another
3137 string. This can only be a string from a `display' property.
3138 IT should already be filled with that string. */
3139 it->current.string_pos = pos->string_pos;
3140 eassert (STRINGP (it->string));
3141 if (it->bidi_p)
3142 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3143 FRAME_WINDOW_P (it->f), &it->bidi_it);
3144 }
3145
3146 /* Restore position in display vector translations, control
3147 character translations or ellipses. */
3148 if (pos->dpvec_index >= 0)
3149 {
3150 if (it->dpvec == NULL)
3151 get_next_display_element (it);
3152 eassert (it->dpvec && it->current.dpvec_index == 0);
3153 it->current.dpvec_index = pos->dpvec_index;
3154 }
3155
3156 CHECK_IT (it);
3157 return !overlay_strings_with_newlines;
3158 }
3159
3160
3161 /* Initialize IT for stepping through current_buffer in window W
3162 starting at ROW->start. */
3163
3164 static void
3165 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3166 {
3167 init_from_display_pos (it, w, &row->start);
3168 it->start = row->start;
3169 it->continuation_lines_width = row->continuation_lines_width;
3170 CHECK_IT (it);
3171 }
3172
3173
3174 /* Initialize IT for stepping through current_buffer in window W
3175 starting in the line following ROW, i.e. starting at ROW->end.
3176 Value is zero if there are overlay strings with newlines at ROW's
3177 end position. */
3178
3179 static int
3180 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3181 {
3182 int success = 0;
3183
3184 if (init_from_display_pos (it, w, &row->end))
3185 {
3186 if (row->continued_p)
3187 it->continuation_lines_width
3188 = row->continuation_lines_width + row->pixel_width;
3189 CHECK_IT (it);
3190 success = 1;
3191 }
3192
3193 return success;
3194 }
3195
3196
3197
3198 \f
3199 /***********************************************************************
3200 Text properties
3201 ***********************************************************************/
3202
3203 /* Called when IT reaches IT->stop_charpos. Handle text property and
3204 overlay changes. Set IT->stop_charpos to the next position where
3205 to stop. */
3206
3207 static void
3208 handle_stop (struct it *it)
3209 {
3210 enum prop_handled handled;
3211 int handle_overlay_change_p;
3212 struct props *p;
3213
3214 it->dpvec = NULL;
3215 it->current.dpvec_index = -1;
3216 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3217 it->ignore_overlay_strings_at_pos_p = 0;
3218 it->ellipsis_p = 0;
3219
3220 /* Use face of preceding text for ellipsis (if invisible) */
3221 if (it->selective_display_ellipsis_p)
3222 it->saved_face_id = it->face_id;
3223
3224 do
3225 {
3226 handled = HANDLED_NORMALLY;
3227
3228 /* Call text property handlers. */
3229 for (p = it_props; p->handler; ++p)
3230 {
3231 handled = p->handler (it);
3232
3233 if (handled == HANDLED_RECOMPUTE_PROPS)
3234 break;
3235 else if (handled == HANDLED_RETURN)
3236 {
3237 /* We still want to show before and after strings from
3238 overlays even if the actual buffer text is replaced. */
3239 if (!handle_overlay_change_p
3240 || it->sp > 1
3241 /* Don't call get_overlay_strings_1 if we already
3242 have overlay strings loaded, because doing so
3243 will load them again and push the iterator state
3244 onto the stack one more time, which is not
3245 expected by the rest of the code that processes
3246 overlay strings. */
3247 || (it->current.overlay_string_index < 0
3248 ? !get_overlay_strings_1 (it, 0, 0)
3249 : 0))
3250 {
3251 if (it->ellipsis_p)
3252 setup_for_ellipsis (it, 0);
3253 /* When handling a display spec, we might load an
3254 empty string. In that case, discard it here. We
3255 used to discard it in handle_single_display_spec,
3256 but that causes get_overlay_strings_1, above, to
3257 ignore overlay strings that we must check. */
3258 if (STRINGP (it->string) && !SCHARS (it->string))
3259 pop_it (it);
3260 return;
3261 }
3262 else if (STRINGP (it->string) && !SCHARS (it->string))
3263 pop_it (it);
3264 else
3265 {
3266 it->ignore_overlay_strings_at_pos_p = 1;
3267 it->string_from_display_prop_p = 0;
3268 it->from_disp_prop_p = 0;
3269 handle_overlay_change_p = 0;
3270 }
3271 handled = HANDLED_RECOMPUTE_PROPS;
3272 break;
3273 }
3274 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3275 handle_overlay_change_p = 0;
3276 }
3277
3278 if (handled != HANDLED_RECOMPUTE_PROPS)
3279 {
3280 /* Don't check for overlay strings below when set to deliver
3281 characters from a display vector. */
3282 if (it->method == GET_FROM_DISPLAY_VECTOR)
3283 handle_overlay_change_p = 0;
3284
3285 /* Handle overlay changes.
3286 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3287 if it finds overlays. */
3288 if (handle_overlay_change_p)
3289 handled = handle_overlay_change (it);
3290 }
3291
3292 if (it->ellipsis_p)
3293 {
3294 setup_for_ellipsis (it, 0);
3295 break;
3296 }
3297 }
3298 while (handled == HANDLED_RECOMPUTE_PROPS);
3299
3300 /* Determine where to stop next. */
3301 if (handled == HANDLED_NORMALLY)
3302 compute_stop_pos (it);
3303 }
3304
3305
3306 /* Compute IT->stop_charpos from text property and overlay change
3307 information for IT's current position. */
3308
3309 static void
3310 compute_stop_pos (struct it *it)
3311 {
3312 register INTERVAL iv, next_iv;
3313 Lisp_Object object, limit, position;
3314 ptrdiff_t charpos, bytepos;
3315
3316 if (STRINGP (it->string))
3317 {
3318 /* Strings are usually short, so don't limit the search for
3319 properties. */
3320 it->stop_charpos = it->end_charpos;
3321 object = it->string;
3322 limit = Qnil;
3323 charpos = IT_STRING_CHARPOS (*it);
3324 bytepos = IT_STRING_BYTEPOS (*it);
3325 }
3326 else
3327 {
3328 ptrdiff_t pos;
3329
3330 /* If end_charpos is out of range for some reason, such as a
3331 misbehaving display function, rationalize it (Bug#5984). */
3332 if (it->end_charpos > ZV)
3333 it->end_charpos = ZV;
3334 it->stop_charpos = it->end_charpos;
3335
3336 /* If next overlay change is in front of the current stop pos
3337 (which is IT->end_charpos), stop there. Note: value of
3338 next_overlay_change is point-max if no overlay change
3339 follows. */
3340 charpos = IT_CHARPOS (*it);
3341 bytepos = IT_BYTEPOS (*it);
3342 pos = next_overlay_change (charpos);
3343 if (pos < it->stop_charpos)
3344 it->stop_charpos = pos;
3345
3346 /* If showing the region, we have to stop at the region
3347 start or end because the face might change there. */
3348 if (it->region_beg_charpos > 0)
3349 {
3350 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3351 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3352 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3353 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3354 }
3355
3356 /* Set up variables for computing the stop position from text
3357 property changes. */
3358 XSETBUFFER (object, current_buffer);
3359 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3360 }
3361
3362 /* Get the interval containing IT's position. Value is a null
3363 interval if there isn't such an interval. */
3364 position = make_number (charpos);
3365 iv = validate_interval_range (object, &position, &position, 0);
3366 if (iv)
3367 {
3368 Lisp_Object values_here[LAST_PROP_IDX];
3369 struct props *p;
3370
3371 /* Get properties here. */
3372 for (p = it_props; p->handler; ++p)
3373 values_here[p->idx] = textget (iv->plist, *p->name);
3374
3375 /* Look for an interval following iv that has different
3376 properties. */
3377 for (next_iv = next_interval (iv);
3378 (next_iv
3379 && (NILP (limit)
3380 || XFASTINT (limit) > next_iv->position));
3381 next_iv = next_interval (next_iv))
3382 {
3383 for (p = it_props; p->handler; ++p)
3384 {
3385 Lisp_Object new_value;
3386
3387 new_value = textget (next_iv->plist, *p->name);
3388 if (!EQ (values_here[p->idx], new_value))
3389 break;
3390 }
3391
3392 if (p->handler)
3393 break;
3394 }
3395
3396 if (next_iv)
3397 {
3398 if (INTEGERP (limit)
3399 && next_iv->position >= XFASTINT (limit))
3400 /* No text property change up to limit. */
3401 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3402 else
3403 /* Text properties change in next_iv. */
3404 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3405 }
3406 }
3407
3408 if (it->cmp_it.id < 0)
3409 {
3410 ptrdiff_t stoppos = it->end_charpos;
3411
3412 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3413 stoppos = -1;
3414 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3415 stoppos, it->string);
3416 }
3417
3418 eassert (STRINGP (it->string)
3419 || (it->stop_charpos >= BEGV
3420 && it->stop_charpos >= IT_CHARPOS (*it)));
3421 }
3422
3423
3424 /* Return the position of the next overlay change after POS in
3425 current_buffer. Value is point-max if no overlay change
3426 follows. This is like `next-overlay-change' but doesn't use
3427 xmalloc. */
3428
3429 static ptrdiff_t
3430 next_overlay_change (ptrdiff_t pos)
3431 {
3432 ptrdiff_t i, noverlays;
3433 ptrdiff_t endpos;
3434 Lisp_Object *overlays;
3435
3436 /* Get all overlays at the given position. */
3437 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3438
3439 /* If any of these overlays ends before endpos,
3440 use its ending point instead. */
3441 for (i = 0; i < noverlays; ++i)
3442 {
3443 Lisp_Object oend;
3444 ptrdiff_t oendpos;
3445
3446 oend = OVERLAY_END (overlays[i]);
3447 oendpos = OVERLAY_POSITION (oend);
3448 endpos = min (endpos, oendpos);
3449 }
3450
3451 return endpos;
3452 }
3453
3454 /* How many characters forward to search for a display property or
3455 display string. Searching too far forward makes the bidi display
3456 sluggish, especially in small windows. */
3457 #define MAX_DISP_SCAN 250
3458
3459 /* Return the character position of a display string at or after
3460 position specified by POSITION. If no display string exists at or
3461 after POSITION, return ZV. A display string is either an overlay
3462 with `display' property whose value is a string, or a `display'
3463 text property whose value is a string. STRING is data about the
3464 string to iterate; if STRING->lstring is nil, we are iterating a
3465 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3466 on a GUI frame. DISP_PROP is set to zero if we searched
3467 MAX_DISP_SCAN characters forward without finding any display
3468 strings, non-zero otherwise. It is set to 2 if the display string
3469 uses any kind of `(space ...)' spec that will produce a stretch of
3470 white space in the text area. */
3471 ptrdiff_t
3472 compute_display_string_pos (struct text_pos *position,
3473 struct bidi_string_data *string,
3474 int frame_window_p, int *disp_prop)
3475 {
3476 /* OBJECT = nil means current buffer. */
3477 Lisp_Object object =
3478 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3479 Lisp_Object pos, spec, limpos;
3480 int string_p = (string && (STRINGP (string->lstring) || string->s));
3481 ptrdiff_t eob = string_p ? string->schars : ZV;
3482 ptrdiff_t begb = string_p ? 0 : BEGV;
3483 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3484 ptrdiff_t lim =
3485 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3486 struct text_pos tpos;
3487 int rv = 0;
3488
3489 *disp_prop = 1;
3490
3491 if (charpos >= eob
3492 /* We don't support display properties whose values are strings
3493 that have display string properties. */
3494 || string->from_disp_str
3495 /* C strings cannot have display properties. */
3496 || (string->s && !STRINGP (object)))
3497 {
3498 *disp_prop = 0;
3499 return eob;
3500 }
3501
3502 /* If the character at CHARPOS is where the display string begins,
3503 return CHARPOS. */
3504 pos = make_number (charpos);
3505 if (STRINGP (object))
3506 bufpos = string->bufpos;
3507 else
3508 bufpos = charpos;
3509 tpos = *position;
3510 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3511 && (charpos <= begb
3512 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3513 object),
3514 spec))
3515 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3516 frame_window_p)))
3517 {
3518 if (rv == 2)
3519 *disp_prop = 2;
3520 return charpos;
3521 }
3522
3523 /* Look forward for the first character with a `display' property
3524 that will replace the underlying text when displayed. */
3525 limpos = make_number (lim);
3526 do {
3527 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3528 CHARPOS (tpos) = XFASTINT (pos);
3529 if (CHARPOS (tpos) >= lim)
3530 {
3531 *disp_prop = 0;
3532 break;
3533 }
3534 if (STRINGP (object))
3535 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3536 else
3537 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3538 spec = Fget_char_property (pos, Qdisplay, object);
3539 if (!STRINGP (object))
3540 bufpos = CHARPOS (tpos);
3541 } while (NILP (spec)
3542 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3543 bufpos, frame_window_p)));
3544 if (rv == 2)
3545 *disp_prop = 2;
3546
3547 return CHARPOS (tpos);
3548 }
3549
3550 /* Return the character position of the end of the display string that
3551 started at CHARPOS. If there's no display string at CHARPOS,
3552 return -1. A display string is either an overlay with `display'
3553 property whose value is a string or a `display' text property whose
3554 value is a string. */
3555 ptrdiff_t
3556 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3557 {
3558 /* OBJECT = nil means current buffer. */
3559 Lisp_Object object =
3560 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3561 Lisp_Object pos = make_number (charpos);
3562 ptrdiff_t eob =
3563 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3564
3565 if (charpos >= eob || (string->s && !STRINGP (object)))
3566 return eob;
3567
3568 /* It could happen that the display property or overlay was removed
3569 since we found it in compute_display_string_pos above. One way
3570 this can happen is if JIT font-lock was called (through
3571 handle_fontified_prop), and jit-lock-functions remove text
3572 properties or overlays from the portion of buffer that includes
3573 CHARPOS. Muse mode is known to do that, for example. In this
3574 case, we return -1 to the caller, to signal that no display
3575 string is actually present at CHARPOS. See bidi_fetch_char for
3576 how this is handled.
3577
3578 An alternative would be to never look for display properties past
3579 it->stop_charpos. But neither compute_display_string_pos nor
3580 bidi_fetch_char that calls it know or care where the next
3581 stop_charpos is. */
3582 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3583 return -1;
3584
3585 /* Look forward for the first character where the `display' property
3586 changes. */
3587 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3588
3589 return XFASTINT (pos);
3590 }
3591
3592
3593 \f
3594 /***********************************************************************
3595 Fontification
3596 ***********************************************************************/
3597
3598 /* Handle changes in the `fontified' property of the current buffer by
3599 calling hook functions from Qfontification_functions to fontify
3600 regions of text. */
3601
3602 static enum prop_handled
3603 handle_fontified_prop (struct it *it)
3604 {
3605 Lisp_Object prop, pos;
3606 enum prop_handled handled = HANDLED_NORMALLY;
3607
3608 if (!NILP (Vmemory_full))
3609 return handled;
3610
3611 /* Get the value of the `fontified' property at IT's current buffer
3612 position. (The `fontified' property doesn't have a special
3613 meaning in strings.) If the value is nil, call functions from
3614 Qfontification_functions. */
3615 if (!STRINGP (it->string)
3616 && it->s == NULL
3617 && !NILP (Vfontification_functions)
3618 && !NILP (Vrun_hooks)
3619 && (pos = make_number (IT_CHARPOS (*it)),
3620 prop = Fget_char_property (pos, Qfontified, Qnil),
3621 /* Ignore the special cased nil value always present at EOB since
3622 no amount of fontifying will be able to change it. */
3623 NILP (prop) && IT_CHARPOS (*it) < Z))
3624 {
3625 ptrdiff_t count = SPECPDL_INDEX ();
3626 Lisp_Object val;
3627 struct buffer *obuf = current_buffer;
3628 int begv = BEGV, zv = ZV;
3629 int old_clip_changed = current_buffer->clip_changed;
3630
3631 val = Vfontification_functions;
3632 specbind (Qfontification_functions, Qnil);
3633
3634 eassert (it->end_charpos == ZV);
3635
3636 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3637 safe_call1 (val, pos);
3638 else
3639 {
3640 Lisp_Object fns, fn;
3641 struct gcpro gcpro1, gcpro2;
3642
3643 fns = Qnil;
3644 GCPRO2 (val, fns);
3645
3646 for (; CONSP (val); val = XCDR (val))
3647 {
3648 fn = XCAR (val);
3649
3650 if (EQ (fn, Qt))
3651 {
3652 /* A value of t indicates this hook has a local
3653 binding; it means to run the global binding too.
3654 In a global value, t should not occur. If it
3655 does, we must ignore it to avoid an endless
3656 loop. */
3657 for (fns = Fdefault_value (Qfontification_functions);
3658 CONSP (fns);
3659 fns = XCDR (fns))
3660 {
3661 fn = XCAR (fns);
3662 if (!EQ (fn, Qt))
3663 safe_call1 (fn, pos);
3664 }
3665 }
3666 else
3667 safe_call1 (fn, pos);
3668 }
3669
3670 UNGCPRO;
3671 }
3672
3673 unbind_to (count, Qnil);
3674
3675 /* Fontification functions routinely call `save-restriction'.
3676 Normally, this tags clip_changed, which can confuse redisplay
3677 (see discussion in Bug#6671). Since we don't perform any
3678 special handling of fontification changes in the case where
3679 `save-restriction' isn't called, there's no point doing so in
3680 this case either. So, if the buffer's restrictions are
3681 actually left unchanged, reset clip_changed. */
3682 if (obuf == current_buffer)
3683 {
3684 if (begv == BEGV && zv == ZV)
3685 current_buffer->clip_changed = old_clip_changed;
3686 }
3687 /* There isn't much we can reasonably do to protect against
3688 misbehaving fontification, but here's a fig leaf. */
3689 else if (BUFFER_LIVE_P (obuf))
3690 set_buffer_internal_1 (obuf);
3691
3692 /* The fontification code may have added/removed text.
3693 It could do even a lot worse, but let's at least protect against
3694 the most obvious case where only the text past `pos' gets changed',
3695 as is/was done in grep.el where some escapes sequences are turned
3696 into face properties (bug#7876). */
3697 it->end_charpos = ZV;
3698
3699 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3700 something. This avoids an endless loop if they failed to
3701 fontify the text for which reason ever. */
3702 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3703 handled = HANDLED_RECOMPUTE_PROPS;
3704 }
3705
3706 return handled;
3707 }
3708
3709
3710 \f
3711 /***********************************************************************
3712 Faces
3713 ***********************************************************************/
3714
3715 /* Set up iterator IT from face properties at its current position.
3716 Called from handle_stop. */
3717
3718 static enum prop_handled
3719 handle_face_prop (struct it *it)
3720 {
3721 int new_face_id;
3722 ptrdiff_t next_stop;
3723
3724 if (!STRINGP (it->string))
3725 {
3726 new_face_id
3727 = face_at_buffer_position (it->w,
3728 IT_CHARPOS (*it),
3729 it->region_beg_charpos,
3730 it->region_end_charpos,
3731 &next_stop,
3732 (IT_CHARPOS (*it)
3733 + TEXT_PROP_DISTANCE_LIMIT),
3734 0, it->base_face_id);
3735
3736 /* Is this a start of a run of characters with box face?
3737 Caveat: this can be called for a freshly initialized
3738 iterator; face_id is -1 in this case. We know that the new
3739 face will not change until limit, i.e. if the new face has a
3740 box, all characters up to limit will have one. But, as
3741 usual, we don't know whether limit is really the end. */
3742 if (new_face_id != it->face_id)
3743 {
3744 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3745 /* If it->face_id is -1, old_face below will be NULL, see
3746 the definition of FACE_FROM_ID. This will happen if this
3747 is the initial call that gets the face. */
3748 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3749
3750 /* If the value of face_id of the iterator is -1, we have to
3751 look in front of IT's position and see whether there is a
3752 face there that's different from new_face_id. */
3753 if (!old_face && IT_CHARPOS (*it) > BEG)
3754 {
3755 int prev_face_id = face_before_it_pos (it);
3756
3757 old_face = FACE_FROM_ID (it->f, prev_face_id);
3758 }
3759
3760 /* If the new face has a box, but the old face does not,
3761 this is the start of a run of characters with box face,
3762 i.e. this character has a shadow on the left side. */
3763 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3764 && (old_face == NULL || !old_face->box));
3765 it->face_box_p = new_face->box != FACE_NO_BOX;
3766 }
3767 }
3768 else
3769 {
3770 int base_face_id;
3771 ptrdiff_t bufpos;
3772 int i;
3773 Lisp_Object from_overlay
3774 = (it->current.overlay_string_index >= 0
3775 ? it->string_overlays[it->current.overlay_string_index
3776 % OVERLAY_STRING_CHUNK_SIZE]
3777 : Qnil);
3778
3779 /* See if we got to this string directly or indirectly from
3780 an overlay property. That includes the before-string or
3781 after-string of an overlay, strings in display properties
3782 provided by an overlay, their text properties, etc.
3783
3784 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3785 if (! NILP (from_overlay))
3786 for (i = it->sp - 1; i >= 0; i--)
3787 {
3788 if (it->stack[i].current.overlay_string_index >= 0)
3789 from_overlay
3790 = it->string_overlays[it->stack[i].current.overlay_string_index
3791 % OVERLAY_STRING_CHUNK_SIZE];
3792 else if (! NILP (it->stack[i].from_overlay))
3793 from_overlay = it->stack[i].from_overlay;
3794
3795 if (!NILP (from_overlay))
3796 break;
3797 }
3798
3799 if (! NILP (from_overlay))
3800 {
3801 bufpos = IT_CHARPOS (*it);
3802 /* For a string from an overlay, the base face depends
3803 only on text properties and ignores overlays. */
3804 base_face_id
3805 = face_for_overlay_string (it->w,
3806 IT_CHARPOS (*it),
3807 it->region_beg_charpos,
3808 it->region_end_charpos,
3809 &next_stop,
3810 (IT_CHARPOS (*it)
3811 + TEXT_PROP_DISTANCE_LIMIT),
3812 0,
3813 from_overlay);
3814 }
3815 else
3816 {
3817 bufpos = 0;
3818
3819 /* For strings from a `display' property, use the face at
3820 IT's current buffer position as the base face to merge
3821 with, so that overlay strings appear in the same face as
3822 surrounding text, unless they specify their own
3823 faces. */
3824 base_face_id = it->string_from_prefix_prop_p
3825 ? DEFAULT_FACE_ID
3826 : underlying_face_id (it);
3827 }
3828
3829 new_face_id = face_at_string_position (it->w,
3830 it->string,
3831 IT_STRING_CHARPOS (*it),
3832 bufpos,
3833 it->region_beg_charpos,
3834 it->region_end_charpos,
3835 &next_stop,
3836 base_face_id, 0);
3837
3838 /* Is this a start of a run of characters with box? Caveat:
3839 this can be called for a freshly allocated iterator; face_id
3840 is -1 is this case. We know that the new face will not
3841 change until the next check pos, i.e. if the new face has a
3842 box, all characters up to that position will have a
3843 box. But, as usual, we don't know whether that position
3844 is really the end. */
3845 if (new_face_id != it->face_id)
3846 {
3847 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3848 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3849
3850 /* If new face has a box but old face hasn't, this is the
3851 start of a run of characters with box, i.e. it has a
3852 shadow on the left side. */
3853 it->start_of_box_run_p
3854 = new_face->box && (old_face == NULL || !old_face->box);
3855 it->face_box_p = new_face->box != FACE_NO_BOX;
3856 }
3857 }
3858
3859 it->face_id = new_face_id;
3860 return HANDLED_NORMALLY;
3861 }
3862
3863
3864 /* Return the ID of the face ``underlying'' IT's current position,
3865 which is in a string. If the iterator is associated with a
3866 buffer, return the face at IT's current buffer position.
3867 Otherwise, use the iterator's base_face_id. */
3868
3869 static int
3870 underlying_face_id (struct it *it)
3871 {
3872 int face_id = it->base_face_id, i;
3873
3874 eassert (STRINGP (it->string));
3875
3876 for (i = it->sp - 1; i >= 0; --i)
3877 if (NILP (it->stack[i].string))
3878 face_id = it->stack[i].face_id;
3879
3880 return face_id;
3881 }
3882
3883
3884 /* Compute the face one character before or after the current position
3885 of IT, in the visual order. BEFORE_P non-zero means get the face
3886 in front (to the left in L2R paragraphs, to the right in R2L
3887 paragraphs) of IT's screen position. Value is the ID of the face. */
3888
3889 static int
3890 face_before_or_after_it_pos (struct it *it, int before_p)
3891 {
3892 int face_id, limit;
3893 ptrdiff_t next_check_charpos;
3894 struct it it_copy;
3895 void *it_copy_data = NULL;
3896
3897 eassert (it->s == NULL);
3898
3899 if (STRINGP (it->string))
3900 {
3901 ptrdiff_t bufpos, charpos;
3902 int base_face_id;
3903
3904 /* No face change past the end of the string (for the case
3905 we are padding with spaces). No face change before the
3906 string start. */
3907 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3908 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3909 return it->face_id;
3910
3911 if (!it->bidi_p)
3912 {
3913 /* Set charpos to the position before or after IT's current
3914 position, in the logical order, which in the non-bidi
3915 case is the same as the visual order. */
3916 if (before_p)
3917 charpos = IT_STRING_CHARPOS (*it) - 1;
3918 else if (it->what == IT_COMPOSITION)
3919 /* For composition, we must check the character after the
3920 composition. */
3921 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3922 else
3923 charpos = IT_STRING_CHARPOS (*it) + 1;
3924 }
3925 else
3926 {
3927 if (before_p)
3928 {
3929 /* With bidi iteration, the character before the current
3930 in the visual order cannot be found by simple
3931 iteration, because "reverse" reordering is not
3932 supported. Instead, we need to use the move_it_*
3933 family of functions. */
3934 /* Ignore face changes before the first visible
3935 character on this display line. */
3936 if (it->current_x <= it->first_visible_x)
3937 return it->face_id;
3938 SAVE_IT (it_copy, *it, it_copy_data);
3939 /* Implementation note: Since move_it_in_display_line
3940 works in the iterator geometry, and thinks the first
3941 character is always the leftmost, even in R2L lines,
3942 we don't need to distinguish between the R2L and L2R
3943 cases here. */
3944 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3945 it_copy.current_x - 1, MOVE_TO_X);
3946 charpos = IT_STRING_CHARPOS (it_copy);
3947 RESTORE_IT (it, it, it_copy_data);
3948 }
3949 else
3950 {
3951 /* Set charpos to the string position of the character
3952 that comes after IT's current position in the visual
3953 order. */
3954 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3955
3956 it_copy = *it;
3957 while (n--)
3958 bidi_move_to_visually_next (&it_copy.bidi_it);
3959
3960 charpos = it_copy.bidi_it.charpos;
3961 }
3962 }
3963 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3964
3965 if (it->current.overlay_string_index >= 0)
3966 bufpos = IT_CHARPOS (*it);
3967 else
3968 bufpos = 0;
3969
3970 base_face_id = underlying_face_id (it);
3971
3972 /* Get the face for ASCII, or unibyte. */
3973 face_id = face_at_string_position (it->w,
3974 it->string,
3975 charpos,
3976 bufpos,
3977 it->region_beg_charpos,
3978 it->region_end_charpos,
3979 &next_check_charpos,
3980 base_face_id, 0);
3981
3982 /* Correct the face for charsets different from ASCII. Do it
3983 for the multibyte case only. The face returned above is
3984 suitable for unibyte text if IT->string is unibyte. */
3985 if (STRING_MULTIBYTE (it->string))
3986 {
3987 struct text_pos pos1 = string_pos (charpos, it->string);
3988 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
3989 int c, len;
3990 struct face *face = FACE_FROM_ID (it->f, face_id);
3991
3992 c = string_char_and_length (p, &len);
3993 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
3994 }
3995 }
3996 else
3997 {
3998 struct text_pos pos;
3999
4000 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4001 || (IT_CHARPOS (*it) <= BEGV && before_p))
4002 return it->face_id;
4003
4004 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4005 pos = it->current.pos;
4006
4007 if (!it->bidi_p)
4008 {
4009 if (before_p)
4010 DEC_TEXT_POS (pos, it->multibyte_p);
4011 else
4012 {
4013 if (it->what == IT_COMPOSITION)
4014 {
4015 /* For composition, we must check the position after
4016 the composition. */
4017 pos.charpos += it->cmp_it.nchars;
4018 pos.bytepos += it->len;
4019 }
4020 else
4021 INC_TEXT_POS (pos, it->multibyte_p);
4022 }
4023 }
4024 else
4025 {
4026 if (before_p)
4027 {
4028 /* With bidi iteration, the character before the current
4029 in the visual order cannot be found by simple
4030 iteration, because "reverse" reordering is not
4031 supported. Instead, we need to use the move_it_*
4032 family of functions. */
4033 /* Ignore face changes before the first visible
4034 character on this display line. */
4035 if (it->current_x <= it->first_visible_x)
4036 return it->face_id;
4037 SAVE_IT (it_copy, *it, it_copy_data);
4038 /* Implementation note: Since move_it_in_display_line
4039 works in the iterator geometry, and thinks the first
4040 character is always the leftmost, even in R2L lines,
4041 we don't need to distinguish between the R2L and L2R
4042 cases here. */
4043 move_it_in_display_line (&it_copy, ZV,
4044 it_copy.current_x - 1, MOVE_TO_X);
4045 pos = it_copy.current.pos;
4046 RESTORE_IT (it, it, it_copy_data);
4047 }
4048 else
4049 {
4050 /* Set charpos to the buffer position of the character
4051 that comes after IT's current position in the visual
4052 order. */
4053 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4054
4055 it_copy = *it;
4056 while (n--)
4057 bidi_move_to_visually_next (&it_copy.bidi_it);
4058
4059 SET_TEXT_POS (pos,
4060 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4061 }
4062 }
4063 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4064
4065 /* Determine face for CHARSET_ASCII, or unibyte. */
4066 face_id = face_at_buffer_position (it->w,
4067 CHARPOS (pos),
4068 it->region_beg_charpos,
4069 it->region_end_charpos,
4070 &next_check_charpos,
4071 limit, 0, -1);
4072
4073 /* Correct the face for charsets different from ASCII. Do it
4074 for the multibyte case only. The face returned above is
4075 suitable for unibyte text if current_buffer is unibyte. */
4076 if (it->multibyte_p)
4077 {
4078 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4079 struct face *face = FACE_FROM_ID (it->f, face_id);
4080 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4081 }
4082 }
4083
4084 return face_id;
4085 }
4086
4087
4088 \f
4089 /***********************************************************************
4090 Invisible text
4091 ***********************************************************************/
4092
4093 /* Set up iterator IT from invisible properties at its current
4094 position. Called from handle_stop. */
4095
4096 static enum prop_handled
4097 handle_invisible_prop (struct it *it)
4098 {
4099 enum prop_handled handled = HANDLED_NORMALLY;
4100 int invis_p;
4101 Lisp_Object prop;
4102
4103 if (STRINGP (it->string))
4104 {
4105 Lisp_Object end_charpos, limit, charpos;
4106
4107 /* Get the value of the invisible text property at the
4108 current position. Value will be nil if there is no such
4109 property. */
4110 charpos = make_number (IT_STRING_CHARPOS (*it));
4111 prop = Fget_text_property (charpos, Qinvisible, it->string);
4112 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4113
4114 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4115 {
4116 /* Record whether we have to display an ellipsis for the
4117 invisible text. */
4118 int display_ellipsis_p = (invis_p == 2);
4119 ptrdiff_t len, endpos;
4120
4121 handled = HANDLED_RECOMPUTE_PROPS;
4122
4123 /* Get the position at which the next visible text can be
4124 found in IT->string, if any. */
4125 endpos = len = SCHARS (it->string);
4126 XSETINT (limit, len);
4127 do
4128 {
4129 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4130 it->string, limit);
4131 if (INTEGERP (end_charpos))
4132 {
4133 endpos = XFASTINT (end_charpos);
4134 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4135 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4136 if (invis_p == 2)
4137 display_ellipsis_p = 1;
4138 }
4139 }
4140 while (invis_p && endpos < len);
4141
4142 if (display_ellipsis_p)
4143 it->ellipsis_p = 1;
4144
4145 if (endpos < len)
4146 {
4147 /* Text at END_CHARPOS is visible. Move IT there. */
4148 struct text_pos old;
4149 ptrdiff_t oldpos;
4150
4151 old = it->current.string_pos;
4152 oldpos = CHARPOS (old);
4153 if (it->bidi_p)
4154 {
4155 if (it->bidi_it.first_elt
4156 && it->bidi_it.charpos < SCHARS (it->string))
4157 bidi_paragraph_init (it->paragraph_embedding,
4158 &it->bidi_it, 1);
4159 /* Bidi-iterate out of the invisible text. */
4160 do
4161 {
4162 bidi_move_to_visually_next (&it->bidi_it);
4163 }
4164 while (oldpos <= it->bidi_it.charpos
4165 && it->bidi_it.charpos < endpos);
4166
4167 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4168 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4169 if (IT_CHARPOS (*it) >= endpos)
4170 it->prev_stop = endpos;
4171 }
4172 else
4173 {
4174 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4175 compute_string_pos (&it->current.string_pos, old, it->string);
4176 }
4177 }
4178 else
4179 {
4180 /* The rest of the string is invisible. If this is an
4181 overlay string, proceed with the next overlay string
4182 or whatever comes and return a character from there. */
4183 if (it->current.overlay_string_index >= 0
4184 && !display_ellipsis_p)
4185 {
4186 next_overlay_string (it);
4187 /* Don't check for overlay strings when we just
4188 finished processing them. */
4189 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4190 }
4191 else
4192 {
4193 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4194 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4195 }
4196 }
4197 }
4198 }
4199 else
4200 {
4201 ptrdiff_t newpos, next_stop, start_charpos, tem;
4202 Lisp_Object pos, overlay;
4203
4204 /* First of all, is there invisible text at this position? */
4205 tem = start_charpos = IT_CHARPOS (*it);
4206 pos = make_number (tem);
4207 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4208 &overlay);
4209 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4210
4211 /* If we are on invisible text, skip over it. */
4212 if (invis_p && start_charpos < it->end_charpos)
4213 {
4214 /* Record whether we have to display an ellipsis for the
4215 invisible text. */
4216 int display_ellipsis_p = invis_p == 2;
4217
4218 handled = HANDLED_RECOMPUTE_PROPS;
4219
4220 /* Loop skipping over invisible text. The loop is left at
4221 ZV or with IT on the first char being visible again. */
4222 do
4223 {
4224 /* Try to skip some invisible text. Return value is the
4225 position reached which can be equal to where we start
4226 if there is nothing invisible there. This skips both
4227 over invisible text properties and overlays with
4228 invisible property. */
4229 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4230
4231 /* If we skipped nothing at all we weren't at invisible
4232 text in the first place. If everything to the end of
4233 the buffer was skipped, end the loop. */
4234 if (newpos == tem || newpos >= ZV)
4235 invis_p = 0;
4236 else
4237 {
4238 /* We skipped some characters but not necessarily
4239 all there are. Check if we ended up on visible
4240 text. Fget_char_property returns the property of
4241 the char before the given position, i.e. if we
4242 get invis_p = 0, this means that the char at
4243 newpos is visible. */
4244 pos = make_number (newpos);
4245 prop = Fget_char_property (pos, Qinvisible, it->window);
4246 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4247 }
4248
4249 /* If we ended up on invisible text, proceed to
4250 skip starting with next_stop. */
4251 if (invis_p)
4252 tem = next_stop;
4253
4254 /* If there are adjacent invisible texts, don't lose the
4255 second one's ellipsis. */
4256 if (invis_p == 2)
4257 display_ellipsis_p = 1;
4258 }
4259 while (invis_p);
4260
4261 /* The position newpos is now either ZV or on visible text. */
4262 if (it->bidi_p)
4263 {
4264 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4265 int on_newline =
4266 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4267 int after_newline =
4268 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4269
4270 /* If the invisible text ends on a newline or on a
4271 character after a newline, we can avoid the costly,
4272 character by character, bidi iteration to NEWPOS, and
4273 instead simply reseat the iterator there. That's
4274 because all bidi reordering information is tossed at
4275 the newline. This is a big win for modes that hide
4276 complete lines, like Outline, Org, etc. */
4277 if (on_newline || after_newline)
4278 {
4279 struct text_pos tpos;
4280 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4281
4282 SET_TEXT_POS (tpos, newpos, bpos);
4283 reseat_1 (it, tpos, 0);
4284 /* If we reseat on a newline/ZV, we need to prep the
4285 bidi iterator for advancing to the next character
4286 after the newline/EOB, keeping the current paragraph
4287 direction (so that PRODUCE_GLYPHS does TRT wrt
4288 prepending/appending glyphs to a glyph row). */
4289 if (on_newline)
4290 {
4291 it->bidi_it.first_elt = 0;
4292 it->bidi_it.paragraph_dir = pdir;
4293 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4294 it->bidi_it.nchars = 1;
4295 it->bidi_it.ch_len = 1;
4296 }
4297 }
4298 else /* Must use the slow method. */
4299 {
4300 /* With bidi iteration, the region of invisible text
4301 could start and/or end in the middle of a
4302 non-base embedding level. Therefore, we need to
4303 skip invisible text using the bidi iterator,
4304 starting at IT's current position, until we find
4305 ourselves outside of the invisible text.
4306 Skipping invisible text _after_ bidi iteration
4307 avoids affecting the visual order of the
4308 displayed text when invisible properties are
4309 added or removed. */
4310 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4311 {
4312 /* If we were `reseat'ed to a new paragraph,
4313 determine the paragraph base direction. We
4314 need to do it now because
4315 next_element_from_buffer may not have a
4316 chance to do it, if we are going to skip any
4317 text at the beginning, which resets the
4318 FIRST_ELT flag. */
4319 bidi_paragraph_init (it->paragraph_embedding,
4320 &it->bidi_it, 1);
4321 }
4322 do
4323 {
4324 bidi_move_to_visually_next (&it->bidi_it);
4325 }
4326 while (it->stop_charpos <= it->bidi_it.charpos
4327 && it->bidi_it.charpos < newpos);
4328 IT_CHARPOS (*it) = it->bidi_it.charpos;
4329 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4330 /* If we overstepped NEWPOS, record its position in
4331 the iterator, so that we skip invisible text if
4332 later the bidi iteration lands us in the
4333 invisible region again. */
4334 if (IT_CHARPOS (*it) >= newpos)
4335 it->prev_stop = newpos;
4336 }
4337 }
4338 else
4339 {
4340 IT_CHARPOS (*it) = newpos;
4341 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4342 }
4343
4344 /* If there are before-strings at the start of invisible
4345 text, and the text is invisible because of a text
4346 property, arrange to show before-strings because 20.x did
4347 it that way. (If the text is invisible because of an
4348 overlay property instead of a text property, this is
4349 already handled in the overlay code.) */
4350 if (NILP (overlay)
4351 && get_overlay_strings (it, it->stop_charpos))
4352 {
4353 handled = HANDLED_RECOMPUTE_PROPS;
4354 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4355 }
4356 else if (display_ellipsis_p)
4357 {
4358 /* Make sure that the glyphs of the ellipsis will get
4359 correct `charpos' values. If we would not update
4360 it->position here, the glyphs would belong to the
4361 last visible character _before_ the invisible
4362 text, which confuses `set_cursor_from_row'.
4363
4364 We use the last invisible position instead of the
4365 first because this way the cursor is always drawn on
4366 the first "." of the ellipsis, whenever PT is inside
4367 the invisible text. Otherwise the cursor would be
4368 placed _after_ the ellipsis when the point is after the
4369 first invisible character. */
4370 if (!STRINGP (it->object))
4371 {
4372 it->position.charpos = newpos - 1;
4373 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4374 }
4375 it->ellipsis_p = 1;
4376 /* Let the ellipsis display before
4377 considering any properties of the following char.
4378 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4379 handled = HANDLED_RETURN;
4380 }
4381 }
4382 }
4383
4384 return handled;
4385 }
4386
4387
4388 /* Make iterator IT return `...' next.
4389 Replaces LEN characters from buffer. */
4390
4391 static void
4392 setup_for_ellipsis (struct it *it, int len)
4393 {
4394 /* Use the display table definition for `...'. Invalid glyphs
4395 will be handled by the method returning elements from dpvec. */
4396 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4397 {
4398 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4399 it->dpvec = v->contents;
4400 it->dpend = v->contents + v->header.size;
4401 }
4402 else
4403 {
4404 /* Default `...'. */
4405 it->dpvec = default_invis_vector;
4406 it->dpend = default_invis_vector + 3;
4407 }
4408
4409 it->dpvec_char_len = len;
4410 it->current.dpvec_index = 0;
4411 it->dpvec_face_id = -1;
4412
4413 /* Remember the current face id in case glyphs specify faces.
4414 IT's face is restored in set_iterator_to_next.
4415 saved_face_id was set to preceding char's face in handle_stop. */
4416 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4417 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4418
4419 it->method = GET_FROM_DISPLAY_VECTOR;
4420 it->ellipsis_p = 1;
4421 }
4422
4423
4424 \f
4425 /***********************************************************************
4426 'display' property
4427 ***********************************************************************/
4428
4429 /* Set up iterator IT from `display' property at its current position.
4430 Called from handle_stop.
4431 We return HANDLED_RETURN if some part of the display property
4432 overrides the display of the buffer text itself.
4433 Otherwise we return HANDLED_NORMALLY. */
4434
4435 static enum prop_handled
4436 handle_display_prop (struct it *it)
4437 {
4438 Lisp_Object propval, object, overlay;
4439 struct text_pos *position;
4440 ptrdiff_t bufpos;
4441 /* Nonzero if some property replaces the display of the text itself. */
4442 int display_replaced_p = 0;
4443
4444 if (STRINGP (it->string))
4445 {
4446 object = it->string;
4447 position = &it->current.string_pos;
4448 bufpos = CHARPOS (it->current.pos);
4449 }
4450 else
4451 {
4452 XSETWINDOW (object, it->w);
4453 position = &it->current.pos;
4454 bufpos = CHARPOS (*position);
4455 }
4456
4457 /* Reset those iterator values set from display property values. */
4458 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4459 it->space_width = Qnil;
4460 it->font_height = Qnil;
4461 it->voffset = 0;
4462
4463 /* We don't support recursive `display' properties, i.e. string
4464 values that have a string `display' property, that have a string
4465 `display' property etc. */
4466 if (!it->string_from_display_prop_p)
4467 it->area = TEXT_AREA;
4468
4469 propval = get_char_property_and_overlay (make_number (position->charpos),
4470 Qdisplay, object, &overlay);
4471 if (NILP (propval))
4472 return HANDLED_NORMALLY;
4473 /* Now OVERLAY is the overlay that gave us this property, or nil
4474 if it was a text property. */
4475
4476 if (!STRINGP (it->string))
4477 object = it->w->buffer;
4478
4479 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4480 position, bufpos,
4481 FRAME_WINDOW_P (it->f));
4482
4483 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4484 }
4485
4486 /* Subroutine of handle_display_prop. Returns non-zero if the display
4487 specification in SPEC is a replacing specification, i.e. it would
4488 replace the text covered by `display' property with something else,
4489 such as an image or a display string. If SPEC includes any kind or
4490 `(space ...) specification, the value is 2; this is used by
4491 compute_display_string_pos, which see.
4492
4493 See handle_single_display_spec for documentation of arguments.
4494 frame_window_p is non-zero if the window being redisplayed is on a
4495 GUI frame; this argument is used only if IT is NULL, see below.
4496
4497 IT can be NULL, if this is called by the bidi reordering code
4498 through compute_display_string_pos, which see. In that case, this
4499 function only examines SPEC, but does not otherwise "handle" it, in
4500 the sense that it doesn't set up members of IT from the display
4501 spec. */
4502 static int
4503 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4504 Lisp_Object overlay, struct text_pos *position,
4505 ptrdiff_t bufpos, int frame_window_p)
4506 {
4507 int replacing_p = 0;
4508 int rv;
4509
4510 if (CONSP (spec)
4511 /* Simple specifications. */
4512 && !EQ (XCAR (spec), Qimage)
4513 && !EQ (XCAR (spec), Qspace)
4514 && !EQ (XCAR (spec), Qwhen)
4515 && !EQ (XCAR (spec), Qslice)
4516 && !EQ (XCAR (spec), Qspace_width)
4517 && !EQ (XCAR (spec), Qheight)
4518 && !EQ (XCAR (spec), Qraise)
4519 /* Marginal area specifications. */
4520 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4521 && !EQ (XCAR (spec), Qleft_fringe)
4522 && !EQ (XCAR (spec), Qright_fringe)
4523 && !NILP (XCAR (spec)))
4524 {
4525 for (; CONSP (spec); spec = XCDR (spec))
4526 {
4527 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4528 overlay, position, bufpos,
4529 replacing_p, frame_window_p)))
4530 {
4531 replacing_p = rv;
4532 /* If some text in a string is replaced, `position' no
4533 longer points to the position of `object'. */
4534 if (!it || STRINGP (object))
4535 break;
4536 }
4537 }
4538 }
4539 else if (VECTORP (spec))
4540 {
4541 ptrdiff_t i;
4542 for (i = 0; i < ASIZE (spec); ++i)
4543 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4544 overlay, position, bufpos,
4545 replacing_p, frame_window_p)))
4546 {
4547 replacing_p = rv;
4548 /* If some text in a string is replaced, `position' no
4549 longer points to the position of `object'. */
4550 if (!it || STRINGP (object))
4551 break;
4552 }
4553 }
4554 else
4555 {
4556 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4557 position, bufpos, 0,
4558 frame_window_p)))
4559 replacing_p = rv;
4560 }
4561
4562 return replacing_p;
4563 }
4564
4565 /* Value is the position of the end of the `display' property starting
4566 at START_POS in OBJECT. */
4567
4568 static struct text_pos
4569 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4570 {
4571 Lisp_Object end;
4572 struct text_pos end_pos;
4573
4574 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4575 Qdisplay, object, Qnil);
4576 CHARPOS (end_pos) = XFASTINT (end);
4577 if (STRINGP (object))
4578 compute_string_pos (&end_pos, start_pos, it->string);
4579 else
4580 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4581
4582 return end_pos;
4583 }
4584
4585
4586 /* Set up IT from a single `display' property specification SPEC. OBJECT
4587 is the object in which the `display' property was found. *POSITION
4588 is the position in OBJECT at which the `display' property was found.
4589 BUFPOS is the buffer position of OBJECT (different from POSITION if
4590 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4591 previously saw a display specification which already replaced text
4592 display with something else, for example an image; we ignore such
4593 properties after the first one has been processed.
4594
4595 OVERLAY is the overlay this `display' property came from,
4596 or nil if it was a text property.
4597
4598 If SPEC is a `space' or `image' specification, and in some other
4599 cases too, set *POSITION to the position where the `display'
4600 property ends.
4601
4602 If IT is NULL, only examine the property specification in SPEC, but
4603 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4604 is intended to be displayed in a window on a GUI frame.
4605
4606 Value is non-zero if something was found which replaces the display
4607 of buffer or string text. */
4608
4609 static int
4610 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4611 Lisp_Object overlay, struct text_pos *position,
4612 ptrdiff_t bufpos, int display_replaced_p,
4613 int frame_window_p)
4614 {
4615 Lisp_Object form;
4616 Lisp_Object location, value;
4617 struct text_pos start_pos = *position;
4618 int valid_p;
4619
4620 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4621 If the result is non-nil, use VALUE instead of SPEC. */
4622 form = Qt;
4623 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4624 {
4625 spec = XCDR (spec);
4626 if (!CONSP (spec))
4627 return 0;
4628 form = XCAR (spec);
4629 spec = XCDR (spec);
4630 }
4631
4632 if (!NILP (form) && !EQ (form, Qt))
4633 {
4634 ptrdiff_t count = SPECPDL_INDEX ();
4635 struct gcpro gcpro1;
4636
4637 /* Bind `object' to the object having the `display' property, a
4638 buffer or string. Bind `position' to the position in the
4639 object where the property was found, and `buffer-position'
4640 to the current position in the buffer. */
4641
4642 if (NILP (object))
4643 XSETBUFFER (object, current_buffer);
4644 specbind (Qobject, object);
4645 specbind (Qposition, make_number (CHARPOS (*position)));
4646 specbind (Qbuffer_position, make_number (bufpos));
4647 GCPRO1 (form);
4648 form = safe_eval (form);
4649 UNGCPRO;
4650 unbind_to (count, Qnil);
4651 }
4652
4653 if (NILP (form))
4654 return 0;
4655
4656 /* Handle `(height HEIGHT)' specifications. */
4657 if (CONSP (spec)
4658 && EQ (XCAR (spec), Qheight)
4659 && CONSP (XCDR (spec)))
4660 {
4661 if (it)
4662 {
4663 if (!FRAME_WINDOW_P (it->f))
4664 return 0;
4665
4666 it->font_height = XCAR (XCDR (spec));
4667 if (!NILP (it->font_height))
4668 {
4669 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4670 int new_height = -1;
4671
4672 if (CONSP (it->font_height)
4673 && (EQ (XCAR (it->font_height), Qplus)
4674 || EQ (XCAR (it->font_height), Qminus))
4675 && CONSP (XCDR (it->font_height))
4676 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4677 {
4678 /* `(+ N)' or `(- N)' where N is an integer. */
4679 int steps = XINT (XCAR (XCDR (it->font_height)));
4680 if (EQ (XCAR (it->font_height), Qplus))
4681 steps = - steps;
4682 it->face_id = smaller_face (it->f, it->face_id, steps);
4683 }
4684 else if (FUNCTIONP (it->font_height))
4685 {
4686 /* Call function with current height as argument.
4687 Value is the new height. */
4688 Lisp_Object height;
4689 height = safe_call1 (it->font_height,
4690 face->lface[LFACE_HEIGHT_INDEX]);
4691 if (NUMBERP (height))
4692 new_height = XFLOATINT (height);
4693 }
4694 else if (NUMBERP (it->font_height))
4695 {
4696 /* Value is a multiple of the canonical char height. */
4697 struct face *f;
4698
4699 f = FACE_FROM_ID (it->f,
4700 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4701 new_height = (XFLOATINT (it->font_height)
4702 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4703 }
4704 else
4705 {
4706 /* Evaluate IT->font_height with `height' bound to the
4707 current specified height to get the new height. */
4708 ptrdiff_t count = SPECPDL_INDEX ();
4709
4710 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4711 value = safe_eval (it->font_height);
4712 unbind_to (count, Qnil);
4713
4714 if (NUMBERP (value))
4715 new_height = XFLOATINT (value);
4716 }
4717
4718 if (new_height > 0)
4719 it->face_id = face_with_height (it->f, it->face_id, new_height);
4720 }
4721 }
4722
4723 return 0;
4724 }
4725
4726 /* Handle `(space-width WIDTH)'. */
4727 if (CONSP (spec)
4728 && EQ (XCAR (spec), Qspace_width)
4729 && CONSP (XCDR (spec)))
4730 {
4731 if (it)
4732 {
4733 if (!FRAME_WINDOW_P (it->f))
4734 return 0;
4735
4736 value = XCAR (XCDR (spec));
4737 if (NUMBERP (value) && XFLOATINT (value) > 0)
4738 it->space_width = value;
4739 }
4740
4741 return 0;
4742 }
4743
4744 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4745 if (CONSP (spec)
4746 && EQ (XCAR (spec), Qslice))
4747 {
4748 Lisp_Object tem;
4749
4750 if (it)
4751 {
4752 if (!FRAME_WINDOW_P (it->f))
4753 return 0;
4754
4755 if (tem = XCDR (spec), CONSP (tem))
4756 {
4757 it->slice.x = XCAR (tem);
4758 if (tem = XCDR (tem), CONSP (tem))
4759 {
4760 it->slice.y = XCAR (tem);
4761 if (tem = XCDR (tem), CONSP (tem))
4762 {
4763 it->slice.width = XCAR (tem);
4764 if (tem = XCDR (tem), CONSP (tem))
4765 it->slice.height = XCAR (tem);
4766 }
4767 }
4768 }
4769 }
4770
4771 return 0;
4772 }
4773
4774 /* Handle `(raise FACTOR)'. */
4775 if (CONSP (spec)
4776 && EQ (XCAR (spec), Qraise)
4777 && CONSP (XCDR (spec)))
4778 {
4779 if (it)
4780 {
4781 if (!FRAME_WINDOW_P (it->f))
4782 return 0;
4783
4784 #ifdef HAVE_WINDOW_SYSTEM
4785 value = XCAR (XCDR (spec));
4786 if (NUMBERP (value))
4787 {
4788 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4789 it->voffset = - (XFLOATINT (value)
4790 * (FONT_HEIGHT (face->font)));
4791 }
4792 #endif /* HAVE_WINDOW_SYSTEM */
4793 }
4794
4795 return 0;
4796 }
4797
4798 /* Don't handle the other kinds of display specifications
4799 inside a string that we got from a `display' property. */
4800 if (it && it->string_from_display_prop_p)
4801 return 0;
4802
4803 /* Characters having this form of property are not displayed, so
4804 we have to find the end of the property. */
4805 if (it)
4806 {
4807 start_pos = *position;
4808 *position = display_prop_end (it, object, start_pos);
4809 }
4810 value = Qnil;
4811
4812 /* Stop the scan at that end position--we assume that all
4813 text properties change there. */
4814 if (it)
4815 it->stop_charpos = position->charpos;
4816
4817 /* Handle `(left-fringe BITMAP [FACE])'
4818 and `(right-fringe BITMAP [FACE])'. */
4819 if (CONSP (spec)
4820 && (EQ (XCAR (spec), Qleft_fringe)
4821 || EQ (XCAR (spec), Qright_fringe))
4822 && CONSP (XCDR (spec)))
4823 {
4824 int fringe_bitmap;
4825
4826 if (it)
4827 {
4828 if (!FRAME_WINDOW_P (it->f))
4829 /* If we return here, POSITION has been advanced
4830 across the text with this property. */
4831 {
4832 /* Synchronize the bidi iterator with POSITION. This is
4833 needed because we are not going to push the iterator
4834 on behalf of this display property, so there will be
4835 no pop_it call to do this synchronization for us. */
4836 if (it->bidi_p)
4837 {
4838 it->position = *position;
4839 iterate_out_of_display_property (it);
4840 *position = it->position;
4841 }
4842 return 1;
4843 }
4844 }
4845 else if (!frame_window_p)
4846 return 1;
4847
4848 #ifdef HAVE_WINDOW_SYSTEM
4849 value = XCAR (XCDR (spec));
4850 if (!SYMBOLP (value)
4851 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4852 /* If we return here, POSITION has been advanced
4853 across the text with this property. */
4854 {
4855 if (it && it->bidi_p)
4856 {
4857 it->position = *position;
4858 iterate_out_of_display_property (it);
4859 *position = it->position;
4860 }
4861 return 1;
4862 }
4863
4864 if (it)
4865 {
4866 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4867
4868 if (CONSP (XCDR (XCDR (spec))))
4869 {
4870 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4871 int face_id2 = lookup_derived_face (it->f, face_name,
4872 FRINGE_FACE_ID, 0);
4873 if (face_id2 >= 0)
4874 face_id = face_id2;
4875 }
4876
4877 /* Save current settings of IT so that we can restore them
4878 when we are finished with the glyph property value. */
4879 push_it (it, position);
4880
4881 it->area = TEXT_AREA;
4882 it->what = IT_IMAGE;
4883 it->image_id = -1; /* no image */
4884 it->position = start_pos;
4885 it->object = NILP (object) ? it->w->buffer : object;
4886 it->method = GET_FROM_IMAGE;
4887 it->from_overlay = Qnil;
4888 it->face_id = face_id;
4889 it->from_disp_prop_p = 1;
4890
4891 /* Say that we haven't consumed the characters with
4892 `display' property yet. The call to pop_it in
4893 set_iterator_to_next will clean this up. */
4894 *position = start_pos;
4895
4896 if (EQ (XCAR (spec), Qleft_fringe))
4897 {
4898 it->left_user_fringe_bitmap = fringe_bitmap;
4899 it->left_user_fringe_face_id = face_id;
4900 }
4901 else
4902 {
4903 it->right_user_fringe_bitmap = fringe_bitmap;
4904 it->right_user_fringe_face_id = face_id;
4905 }
4906 }
4907 #endif /* HAVE_WINDOW_SYSTEM */
4908 return 1;
4909 }
4910
4911 /* Prepare to handle `((margin left-margin) ...)',
4912 `((margin right-margin) ...)' and `((margin nil) ...)'
4913 prefixes for display specifications. */
4914 location = Qunbound;
4915 if (CONSP (spec) && CONSP (XCAR (spec)))
4916 {
4917 Lisp_Object tem;
4918
4919 value = XCDR (spec);
4920 if (CONSP (value))
4921 value = XCAR (value);
4922
4923 tem = XCAR (spec);
4924 if (EQ (XCAR (tem), Qmargin)
4925 && (tem = XCDR (tem),
4926 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4927 (NILP (tem)
4928 || EQ (tem, Qleft_margin)
4929 || EQ (tem, Qright_margin))))
4930 location = tem;
4931 }
4932
4933 if (EQ (location, Qunbound))
4934 {
4935 location = Qnil;
4936 value = spec;
4937 }
4938
4939 /* After this point, VALUE is the property after any
4940 margin prefix has been stripped. It must be a string,
4941 an image specification, or `(space ...)'.
4942
4943 LOCATION specifies where to display: `left-margin',
4944 `right-margin' or nil. */
4945
4946 valid_p = (STRINGP (value)
4947 #ifdef HAVE_WINDOW_SYSTEM
4948 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4949 && valid_image_p (value))
4950 #endif /* not HAVE_WINDOW_SYSTEM */
4951 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4952
4953 if (valid_p && !display_replaced_p)
4954 {
4955 int retval = 1;
4956
4957 if (!it)
4958 {
4959 /* Callers need to know whether the display spec is any kind
4960 of `(space ...)' spec that is about to affect text-area
4961 display. */
4962 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4963 retval = 2;
4964 return retval;
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 it->from_overlay = overlay;
4971 it->from_disp_prop_p = 1;
4972
4973 if (NILP (location))
4974 it->area = TEXT_AREA;
4975 else if (EQ (location, Qleft_margin))
4976 it->area = LEFT_MARGIN_AREA;
4977 else
4978 it->area = RIGHT_MARGIN_AREA;
4979
4980 if (STRINGP (value))
4981 {
4982 it->string = value;
4983 it->multibyte_p = STRING_MULTIBYTE (it->string);
4984 it->current.overlay_string_index = -1;
4985 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
4986 it->end_charpos = it->string_nchars = SCHARS (it->string);
4987 it->method = GET_FROM_STRING;
4988 it->stop_charpos = 0;
4989 it->prev_stop = 0;
4990 it->base_level_stop = 0;
4991 it->string_from_display_prop_p = 1;
4992 /* Say that we haven't consumed the characters with
4993 `display' property yet. The call to pop_it in
4994 set_iterator_to_next will clean this up. */
4995 if (BUFFERP (object))
4996 *position = start_pos;
4997
4998 /* Force paragraph direction to be that of the parent
4999 object. If the parent object's paragraph direction is
5000 not yet determined, default to L2R. */
5001 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5002 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5003 else
5004 it->paragraph_embedding = L2R;
5005
5006 /* Set up the bidi iterator for this display string. */
5007 if (it->bidi_p)
5008 {
5009 it->bidi_it.string.lstring = it->string;
5010 it->bidi_it.string.s = NULL;
5011 it->bidi_it.string.schars = it->end_charpos;
5012 it->bidi_it.string.bufpos = bufpos;
5013 it->bidi_it.string.from_disp_str = 1;
5014 it->bidi_it.string.unibyte = !it->multibyte_p;
5015 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5016 }
5017 }
5018 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5019 {
5020 it->method = GET_FROM_STRETCH;
5021 it->object = value;
5022 *position = it->position = start_pos;
5023 retval = 1 + (it->area == TEXT_AREA);
5024 }
5025 #ifdef HAVE_WINDOW_SYSTEM
5026 else
5027 {
5028 it->what = IT_IMAGE;
5029 it->image_id = lookup_image (it->f, value);
5030 it->position = start_pos;
5031 it->object = NILP (object) ? it->w->buffer : object;
5032 it->method = GET_FROM_IMAGE;
5033
5034 /* Say that we haven't consumed the characters with
5035 `display' property yet. The call to pop_it in
5036 set_iterator_to_next will clean this up. */
5037 *position = start_pos;
5038 }
5039 #endif /* HAVE_WINDOW_SYSTEM */
5040
5041 return retval;
5042 }
5043
5044 /* Invalid property or property not supported. Restore
5045 POSITION to what it was before. */
5046 *position = start_pos;
5047 return 0;
5048 }
5049
5050 /* Check if PROP is a display property value whose text should be
5051 treated as intangible. OVERLAY is the overlay from which PROP
5052 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5053 specify the buffer position covered by PROP. */
5054
5055 int
5056 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5057 ptrdiff_t charpos, ptrdiff_t bytepos)
5058 {
5059 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5060 struct text_pos position;
5061
5062 SET_TEXT_POS (position, charpos, bytepos);
5063 return handle_display_spec (NULL, prop, Qnil, overlay,
5064 &position, charpos, frame_window_p);
5065 }
5066
5067
5068 /* Return 1 if PROP is a display sub-property value containing STRING.
5069
5070 Implementation note: this and the following function are really
5071 special cases of handle_display_spec and
5072 handle_single_display_spec, and should ideally use the same code.
5073 Until they do, these two pairs must be consistent and must be
5074 modified in sync. */
5075
5076 static int
5077 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5078 {
5079 if (EQ (string, prop))
5080 return 1;
5081
5082 /* Skip over `when FORM'. */
5083 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5084 {
5085 prop = XCDR (prop);
5086 if (!CONSP (prop))
5087 return 0;
5088 /* Actually, the condition following `when' should be eval'ed,
5089 like handle_single_display_spec does, and we should return
5090 zero if it evaluates to nil. However, this function is
5091 called only when the buffer was already displayed and some
5092 glyph in the glyph matrix was found to come from a display
5093 string. Therefore, the condition was already evaluated, and
5094 the result was non-nil, otherwise the display string wouldn't
5095 have been displayed and we would have never been called for
5096 this property. Thus, we can skip the evaluation and assume
5097 its result is non-nil. */
5098 prop = XCDR (prop);
5099 }
5100
5101 if (CONSP (prop))
5102 /* Skip over `margin LOCATION'. */
5103 if (EQ (XCAR (prop), Qmargin))
5104 {
5105 prop = XCDR (prop);
5106 if (!CONSP (prop))
5107 return 0;
5108
5109 prop = XCDR (prop);
5110 if (!CONSP (prop))
5111 return 0;
5112 }
5113
5114 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5115 }
5116
5117
5118 /* Return 1 if STRING appears in the `display' property PROP. */
5119
5120 static int
5121 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5122 {
5123 if (CONSP (prop)
5124 && !EQ (XCAR (prop), Qwhen)
5125 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5126 {
5127 /* A list of sub-properties. */
5128 while (CONSP (prop))
5129 {
5130 if (single_display_spec_string_p (XCAR (prop), string))
5131 return 1;
5132 prop = XCDR (prop);
5133 }
5134 }
5135 else if (VECTORP (prop))
5136 {
5137 /* A vector of sub-properties. */
5138 ptrdiff_t i;
5139 for (i = 0; i < ASIZE (prop); ++i)
5140 if (single_display_spec_string_p (AREF (prop, i), string))
5141 return 1;
5142 }
5143 else
5144 return single_display_spec_string_p (prop, string);
5145
5146 return 0;
5147 }
5148
5149 /* Look for STRING in overlays and text properties in the current
5150 buffer, between character positions FROM and TO (excluding TO).
5151 BACK_P non-zero means look back (in this case, TO is supposed to be
5152 less than FROM).
5153 Value is the first character position where STRING was found, or
5154 zero if it wasn't found before hitting TO.
5155
5156 This function may only use code that doesn't eval because it is
5157 called asynchronously from note_mouse_highlight. */
5158
5159 static ptrdiff_t
5160 string_buffer_position_lim (Lisp_Object string,
5161 ptrdiff_t from, ptrdiff_t to, int back_p)
5162 {
5163 Lisp_Object limit, prop, pos;
5164 int found = 0;
5165
5166 pos = make_number (max (from, BEGV));
5167
5168 if (!back_p) /* looking forward */
5169 {
5170 limit = make_number (min (to, ZV));
5171 while (!found && !EQ (pos, limit))
5172 {
5173 prop = Fget_char_property (pos, Qdisplay, Qnil);
5174 if (!NILP (prop) && display_prop_string_p (prop, string))
5175 found = 1;
5176 else
5177 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5178 limit);
5179 }
5180 }
5181 else /* looking back */
5182 {
5183 limit = make_number (max (to, BEGV));
5184 while (!found && !EQ (pos, limit))
5185 {
5186 prop = Fget_char_property (pos, Qdisplay, Qnil);
5187 if (!NILP (prop) && display_prop_string_p (prop, string))
5188 found = 1;
5189 else
5190 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5191 limit);
5192 }
5193 }
5194
5195 return found ? XINT (pos) : 0;
5196 }
5197
5198 /* Determine which buffer position in current buffer STRING comes from.
5199 AROUND_CHARPOS is an approximate position where it could come from.
5200 Value is the buffer position or 0 if it couldn't be determined.
5201
5202 This function is necessary because we don't record buffer positions
5203 in glyphs generated from strings (to keep struct glyph small).
5204 This function may only use code that doesn't eval because it is
5205 called asynchronously from note_mouse_highlight. */
5206
5207 static ptrdiff_t
5208 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5209 {
5210 const int MAX_DISTANCE = 1000;
5211 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5212 around_charpos + MAX_DISTANCE,
5213 0);
5214
5215 if (!found)
5216 found = string_buffer_position_lim (string, around_charpos,
5217 around_charpos - MAX_DISTANCE, 1);
5218 return found;
5219 }
5220
5221
5222 \f
5223 /***********************************************************************
5224 `composition' property
5225 ***********************************************************************/
5226
5227 /* Set up iterator IT from `composition' property at its current
5228 position. Called from handle_stop. */
5229
5230 static enum prop_handled
5231 handle_composition_prop (struct it *it)
5232 {
5233 Lisp_Object prop, string;
5234 ptrdiff_t pos, pos_byte, start, end;
5235
5236 if (STRINGP (it->string))
5237 {
5238 unsigned char *s;
5239
5240 pos = IT_STRING_CHARPOS (*it);
5241 pos_byte = IT_STRING_BYTEPOS (*it);
5242 string = it->string;
5243 s = SDATA (string) + pos_byte;
5244 it->c = STRING_CHAR (s);
5245 }
5246 else
5247 {
5248 pos = IT_CHARPOS (*it);
5249 pos_byte = IT_BYTEPOS (*it);
5250 string = Qnil;
5251 it->c = FETCH_CHAR (pos_byte);
5252 }
5253
5254 /* If there's a valid composition and point is not inside of the
5255 composition (in the case that the composition is from the current
5256 buffer), draw a glyph composed from the composition components. */
5257 if (find_composition (pos, -1, &start, &end, &prop, string)
5258 && COMPOSITION_VALID_P (start, end, prop)
5259 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5260 {
5261 if (start < pos)
5262 /* As we can't handle this situation (perhaps font-lock added
5263 a new composition), we just return here hoping that next
5264 redisplay will detect this composition much earlier. */
5265 return HANDLED_NORMALLY;
5266 if (start != pos)
5267 {
5268 if (STRINGP (it->string))
5269 pos_byte = string_char_to_byte (it->string, start);
5270 else
5271 pos_byte = CHAR_TO_BYTE (start);
5272 }
5273 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5274 prop, string);
5275
5276 if (it->cmp_it.id >= 0)
5277 {
5278 it->cmp_it.ch = -1;
5279 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5280 it->cmp_it.nglyphs = -1;
5281 }
5282 }
5283
5284 return HANDLED_NORMALLY;
5285 }
5286
5287
5288 \f
5289 /***********************************************************************
5290 Overlay strings
5291 ***********************************************************************/
5292
5293 /* The following structure is used to record overlay strings for
5294 later sorting in load_overlay_strings. */
5295
5296 struct overlay_entry
5297 {
5298 Lisp_Object overlay;
5299 Lisp_Object string;
5300 EMACS_INT priority;
5301 int after_string_p;
5302 };
5303
5304
5305 /* Set up iterator IT from overlay strings at its current position.
5306 Called from handle_stop. */
5307
5308 static enum prop_handled
5309 handle_overlay_change (struct it *it)
5310 {
5311 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5312 return HANDLED_RECOMPUTE_PROPS;
5313 else
5314 return HANDLED_NORMALLY;
5315 }
5316
5317
5318 /* Set up the next overlay string for delivery by IT, if there is an
5319 overlay string to deliver. Called by set_iterator_to_next when the
5320 end of the current overlay string is reached. If there are more
5321 overlay strings to display, IT->string and
5322 IT->current.overlay_string_index are set appropriately here.
5323 Otherwise IT->string is set to nil. */
5324
5325 static void
5326 next_overlay_string (struct it *it)
5327 {
5328 ++it->current.overlay_string_index;
5329 if (it->current.overlay_string_index == it->n_overlay_strings)
5330 {
5331 /* No more overlay strings. Restore IT's settings to what
5332 they were before overlay strings were processed, and
5333 continue to deliver from current_buffer. */
5334
5335 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5336 pop_it (it);
5337 eassert (it->sp > 0
5338 || (NILP (it->string)
5339 && it->method == GET_FROM_BUFFER
5340 && it->stop_charpos >= BEGV
5341 && it->stop_charpos <= it->end_charpos));
5342 it->current.overlay_string_index = -1;
5343 it->n_overlay_strings = 0;
5344 it->overlay_strings_charpos = -1;
5345 /* If there's an empty display string on the stack, pop the
5346 stack, to resync the bidi iterator with IT's position. Such
5347 empty strings are pushed onto the stack in
5348 get_overlay_strings_1. */
5349 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5350 pop_it (it);
5351
5352 /* If we're at the end of the buffer, record that we have
5353 processed the overlay strings there already, so that
5354 next_element_from_buffer doesn't try it again. */
5355 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5356 it->overlay_strings_at_end_processed_p = 1;
5357 }
5358 else
5359 {
5360 /* There are more overlay strings to process. If
5361 IT->current.overlay_string_index has advanced to a position
5362 where we must load IT->overlay_strings with more strings, do
5363 it. We must load at the IT->overlay_strings_charpos where
5364 IT->n_overlay_strings was originally computed; when invisible
5365 text is present, this might not be IT_CHARPOS (Bug#7016). */
5366 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5367
5368 if (it->current.overlay_string_index && i == 0)
5369 load_overlay_strings (it, it->overlay_strings_charpos);
5370
5371 /* Initialize IT to deliver display elements from the overlay
5372 string. */
5373 it->string = it->overlay_strings[i];
5374 it->multibyte_p = STRING_MULTIBYTE (it->string);
5375 SET_TEXT_POS (it->current.string_pos, 0, 0);
5376 it->method = GET_FROM_STRING;
5377 it->stop_charpos = 0;
5378 it->end_charpos = SCHARS (it->string);
5379 if (it->cmp_it.stop_pos >= 0)
5380 it->cmp_it.stop_pos = 0;
5381 it->prev_stop = 0;
5382 it->base_level_stop = 0;
5383
5384 /* Set up the bidi iterator for this overlay string. */
5385 if (it->bidi_p)
5386 {
5387 it->bidi_it.string.lstring = it->string;
5388 it->bidi_it.string.s = NULL;
5389 it->bidi_it.string.schars = SCHARS (it->string);
5390 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5391 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5392 it->bidi_it.string.unibyte = !it->multibyte_p;
5393 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5394 }
5395 }
5396
5397 CHECK_IT (it);
5398 }
5399
5400
5401 /* Compare two overlay_entry structures E1 and E2. Used as a
5402 comparison function for qsort in load_overlay_strings. Overlay
5403 strings for the same position are sorted so that
5404
5405 1. All after-strings come in front of before-strings, except
5406 when they come from the same overlay.
5407
5408 2. Within after-strings, strings are sorted so that overlay strings
5409 from overlays with higher priorities come first.
5410
5411 2. Within before-strings, strings are sorted so that overlay
5412 strings from overlays with higher priorities come last.
5413
5414 Value is analogous to strcmp. */
5415
5416
5417 static int
5418 compare_overlay_entries (const void *e1, const void *e2)
5419 {
5420 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5421 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5422 int result;
5423
5424 if (entry1->after_string_p != entry2->after_string_p)
5425 {
5426 /* Let after-strings appear in front of before-strings if
5427 they come from different overlays. */
5428 if (EQ (entry1->overlay, entry2->overlay))
5429 result = entry1->after_string_p ? 1 : -1;
5430 else
5431 result = entry1->after_string_p ? -1 : 1;
5432 }
5433 else if (entry1->priority != entry2->priority)
5434 {
5435 if (entry1->after_string_p)
5436 /* After-strings sorted in order of decreasing priority. */
5437 result = entry2->priority < entry1->priority ? -1 : 1;
5438 else
5439 /* Before-strings sorted in order of increasing priority. */
5440 result = entry1->priority < entry2->priority ? -1 : 1;
5441 }
5442 else
5443 result = 0;
5444
5445 return result;
5446 }
5447
5448
5449 /* Load the vector IT->overlay_strings with overlay strings from IT's
5450 current buffer position, or from CHARPOS if that is > 0. Set
5451 IT->n_overlays to the total number of overlay strings found.
5452
5453 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5454 a time. On entry into load_overlay_strings,
5455 IT->current.overlay_string_index gives the number of overlay
5456 strings that have already been loaded by previous calls to this
5457 function.
5458
5459 IT->add_overlay_start contains an additional overlay start
5460 position to consider for taking overlay strings from, if non-zero.
5461 This position comes into play when the overlay has an `invisible'
5462 property, and both before and after-strings. When we've skipped to
5463 the end of the overlay, because of its `invisible' property, we
5464 nevertheless want its before-string to appear.
5465 IT->add_overlay_start will contain the overlay start position
5466 in this case.
5467
5468 Overlay strings are sorted so that after-string strings come in
5469 front of before-string strings. Within before and after-strings,
5470 strings are sorted by overlay priority. See also function
5471 compare_overlay_entries. */
5472
5473 static void
5474 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5475 {
5476 Lisp_Object overlay, window, str, invisible;
5477 struct Lisp_Overlay *ov;
5478 ptrdiff_t start, end;
5479 ptrdiff_t size = 20;
5480 ptrdiff_t n = 0, i, j;
5481 int invis_p;
5482 struct overlay_entry *entries = alloca (size * sizeof *entries);
5483 USE_SAFE_ALLOCA;
5484
5485 if (charpos <= 0)
5486 charpos = IT_CHARPOS (*it);
5487
5488 /* Append the overlay string STRING of overlay OVERLAY to vector
5489 `entries' which has size `size' and currently contains `n'
5490 elements. AFTER_P non-zero means STRING is an after-string of
5491 OVERLAY. */
5492 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5493 do \
5494 { \
5495 Lisp_Object priority; \
5496 \
5497 if (n == size) \
5498 { \
5499 struct overlay_entry *old = entries; \
5500 SAFE_NALLOCA (entries, 2, size); \
5501 memcpy (entries, old, size * sizeof *entries); \
5502 size *= 2; \
5503 } \
5504 \
5505 entries[n].string = (STRING); \
5506 entries[n].overlay = (OVERLAY); \
5507 priority = Foverlay_get ((OVERLAY), Qpriority); \
5508 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5509 entries[n].after_string_p = (AFTER_P); \
5510 ++n; \
5511 } \
5512 while (0)
5513
5514 /* Process overlay before the overlay center. */
5515 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5516 {
5517 XSETMISC (overlay, ov);
5518 eassert (OVERLAYP (overlay));
5519 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5520 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5521
5522 if (end < charpos)
5523 break;
5524
5525 /* Skip this overlay if it doesn't start or end at IT's current
5526 position. */
5527 if (end != charpos && start != charpos)
5528 continue;
5529
5530 /* Skip this overlay if it doesn't apply to IT->w. */
5531 window = Foverlay_get (overlay, Qwindow);
5532 if (WINDOWP (window) && XWINDOW (window) != it->w)
5533 continue;
5534
5535 /* If the text ``under'' the overlay is invisible, both before-
5536 and after-strings from this overlay are visible; start and
5537 end position are indistinguishable. */
5538 invisible = Foverlay_get (overlay, Qinvisible);
5539 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5540
5541 /* If overlay has a non-empty before-string, record it. */
5542 if ((start == charpos || (end == charpos && invis_p))
5543 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5544 && SCHARS (str))
5545 RECORD_OVERLAY_STRING (overlay, str, 0);
5546
5547 /* If overlay has a non-empty after-string, record it. */
5548 if ((end == charpos || (start == charpos && invis_p))
5549 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5550 && SCHARS (str))
5551 RECORD_OVERLAY_STRING (overlay, str, 1);
5552 }
5553
5554 /* Process overlays after the overlay center. */
5555 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5556 {
5557 XSETMISC (overlay, ov);
5558 eassert (OVERLAYP (overlay));
5559 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5560 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5561
5562 if (start > charpos)
5563 break;
5564
5565 /* Skip this overlay if it doesn't start or end at IT's current
5566 position. */
5567 if (end != charpos && start != charpos)
5568 continue;
5569
5570 /* Skip this overlay if it doesn't apply to IT->w. */
5571 window = Foverlay_get (overlay, Qwindow);
5572 if (WINDOWP (window) && XWINDOW (window) != it->w)
5573 continue;
5574
5575 /* If the text ``under'' the overlay is invisible, it has a zero
5576 dimension, and both before- and after-strings apply. */
5577 invisible = Foverlay_get (overlay, Qinvisible);
5578 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5579
5580 /* If overlay has a non-empty before-string, record it. */
5581 if ((start == charpos || (end == charpos && invis_p))
5582 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5583 && SCHARS (str))
5584 RECORD_OVERLAY_STRING (overlay, str, 0);
5585
5586 /* If overlay has a non-empty after-string, record it. */
5587 if ((end == charpos || (start == charpos && invis_p))
5588 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5589 && SCHARS (str))
5590 RECORD_OVERLAY_STRING (overlay, str, 1);
5591 }
5592
5593 #undef RECORD_OVERLAY_STRING
5594
5595 /* Sort entries. */
5596 if (n > 1)
5597 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5598
5599 /* Record number of overlay strings, and where we computed it. */
5600 it->n_overlay_strings = n;
5601 it->overlay_strings_charpos = charpos;
5602
5603 /* IT->current.overlay_string_index is the number of overlay strings
5604 that have already been consumed by IT. Copy some of the
5605 remaining overlay strings to IT->overlay_strings. */
5606 i = 0;
5607 j = it->current.overlay_string_index;
5608 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5609 {
5610 it->overlay_strings[i] = entries[j].string;
5611 it->string_overlays[i++] = entries[j++].overlay;
5612 }
5613
5614 CHECK_IT (it);
5615 SAFE_FREE ();
5616 }
5617
5618
5619 /* Get the first chunk of overlay strings at IT's current buffer
5620 position, or at CHARPOS if that is > 0. Value is non-zero if at
5621 least one overlay string was found. */
5622
5623 static int
5624 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5625 {
5626 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5627 process. This fills IT->overlay_strings with strings, and sets
5628 IT->n_overlay_strings to the total number of strings to process.
5629 IT->pos.overlay_string_index has to be set temporarily to zero
5630 because load_overlay_strings needs this; it must be set to -1
5631 when no overlay strings are found because a zero value would
5632 indicate a position in the first overlay string. */
5633 it->current.overlay_string_index = 0;
5634 load_overlay_strings (it, charpos);
5635
5636 /* If we found overlay strings, set up IT to deliver display
5637 elements from the first one. Otherwise set up IT to deliver
5638 from current_buffer. */
5639 if (it->n_overlay_strings)
5640 {
5641 /* Make sure we know settings in current_buffer, so that we can
5642 restore meaningful values when we're done with the overlay
5643 strings. */
5644 if (compute_stop_p)
5645 compute_stop_pos (it);
5646 eassert (it->face_id >= 0);
5647
5648 /* Save IT's settings. They are restored after all overlay
5649 strings have been processed. */
5650 eassert (!compute_stop_p || it->sp == 0);
5651
5652 /* When called from handle_stop, there might be an empty display
5653 string loaded. In that case, don't bother saving it. But
5654 don't use this optimization with the bidi iterator, since we
5655 need the corresponding pop_it call to resync the bidi
5656 iterator's position with IT's position, after we are done
5657 with the overlay strings. (The corresponding call to pop_it
5658 in case of an empty display string is in
5659 next_overlay_string.) */
5660 if (!(!it->bidi_p
5661 && STRINGP (it->string) && !SCHARS (it->string)))
5662 push_it (it, NULL);
5663
5664 /* Set up IT to deliver display elements from the first overlay
5665 string. */
5666 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5667 it->string = it->overlay_strings[0];
5668 it->from_overlay = Qnil;
5669 it->stop_charpos = 0;
5670 eassert (STRINGP (it->string));
5671 it->end_charpos = SCHARS (it->string);
5672 it->prev_stop = 0;
5673 it->base_level_stop = 0;
5674 it->multibyte_p = STRING_MULTIBYTE (it->string);
5675 it->method = GET_FROM_STRING;
5676 it->from_disp_prop_p = 0;
5677
5678 /* Force paragraph direction to be that of the parent
5679 buffer. */
5680 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5681 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5682 else
5683 it->paragraph_embedding = L2R;
5684
5685 /* Set up the bidi iterator for this overlay string. */
5686 if (it->bidi_p)
5687 {
5688 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5689
5690 it->bidi_it.string.lstring = it->string;
5691 it->bidi_it.string.s = NULL;
5692 it->bidi_it.string.schars = SCHARS (it->string);
5693 it->bidi_it.string.bufpos = pos;
5694 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5695 it->bidi_it.string.unibyte = !it->multibyte_p;
5696 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5697 }
5698 return 1;
5699 }
5700
5701 it->current.overlay_string_index = -1;
5702 return 0;
5703 }
5704
5705 static int
5706 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5707 {
5708 it->string = Qnil;
5709 it->method = GET_FROM_BUFFER;
5710
5711 (void) get_overlay_strings_1 (it, charpos, 1);
5712
5713 CHECK_IT (it);
5714
5715 /* Value is non-zero if we found at least one overlay string. */
5716 return STRINGP (it->string);
5717 }
5718
5719
5720 \f
5721 /***********************************************************************
5722 Saving and restoring state
5723 ***********************************************************************/
5724
5725 /* Save current settings of IT on IT->stack. Called, for example,
5726 before setting up IT for an overlay string, to be able to restore
5727 IT's settings to what they were after the overlay string has been
5728 processed. If POSITION is non-NULL, it is the position to save on
5729 the stack instead of IT->position. */
5730
5731 static void
5732 push_it (struct it *it, struct text_pos *position)
5733 {
5734 struct iterator_stack_entry *p;
5735
5736 eassert (it->sp < IT_STACK_SIZE);
5737 p = it->stack + it->sp;
5738
5739 p->stop_charpos = it->stop_charpos;
5740 p->prev_stop = it->prev_stop;
5741 p->base_level_stop = it->base_level_stop;
5742 p->cmp_it = it->cmp_it;
5743 eassert (it->face_id >= 0);
5744 p->face_id = it->face_id;
5745 p->string = it->string;
5746 p->method = it->method;
5747 p->from_overlay = it->from_overlay;
5748 switch (p->method)
5749 {
5750 case GET_FROM_IMAGE:
5751 p->u.image.object = it->object;
5752 p->u.image.image_id = it->image_id;
5753 p->u.image.slice = it->slice;
5754 break;
5755 case GET_FROM_STRETCH:
5756 p->u.stretch.object = it->object;
5757 break;
5758 }
5759 p->position = position ? *position : it->position;
5760 p->current = it->current;
5761 p->end_charpos = it->end_charpos;
5762 p->string_nchars = it->string_nchars;
5763 p->area = it->area;
5764 p->multibyte_p = it->multibyte_p;
5765 p->avoid_cursor_p = it->avoid_cursor_p;
5766 p->space_width = it->space_width;
5767 p->font_height = it->font_height;
5768 p->voffset = it->voffset;
5769 p->string_from_display_prop_p = it->string_from_display_prop_p;
5770 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5771 p->display_ellipsis_p = 0;
5772 p->line_wrap = it->line_wrap;
5773 p->bidi_p = it->bidi_p;
5774 p->paragraph_embedding = it->paragraph_embedding;
5775 p->from_disp_prop_p = it->from_disp_prop_p;
5776 ++it->sp;
5777
5778 /* Save the state of the bidi iterator as well. */
5779 if (it->bidi_p)
5780 bidi_push_it (&it->bidi_it);
5781 }
5782
5783 static void
5784 iterate_out_of_display_property (struct it *it)
5785 {
5786 int buffer_p = !STRINGP (it->string);
5787 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5788 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5789
5790 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5791
5792 /* Maybe initialize paragraph direction. If we are at the beginning
5793 of a new paragraph, next_element_from_buffer may not have a
5794 chance to do that. */
5795 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5796 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5797 /* prev_stop can be zero, so check against BEGV as well. */
5798 while (it->bidi_it.charpos >= bob
5799 && it->prev_stop <= it->bidi_it.charpos
5800 && it->bidi_it.charpos < CHARPOS (it->position)
5801 && it->bidi_it.charpos < eob)
5802 bidi_move_to_visually_next (&it->bidi_it);
5803 /* Record the stop_pos we just crossed, for when we cross it
5804 back, maybe. */
5805 if (it->bidi_it.charpos > CHARPOS (it->position))
5806 it->prev_stop = CHARPOS (it->position);
5807 /* If we ended up not where pop_it put us, resync IT's
5808 positional members with the bidi iterator. */
5809 if (it->bidi_it.charpos != CHARPOS (it->position))
5810 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5811 if (buffer_p)
5812 it->current.pos = it->position;
5813 else
5814 it->current.string_pos = it->position;
5815 }
5816
5817 /* Restore IT's settings from IT->stack. Called, for example, when no
5818 more overlay strings must be processed, and we return to delivering
5819 display elements from a buffer, or when the end of a string from a
5820 `display' property is reached and we return to delivering display
5821 elements from an overlay string, or from a buffer. */
5822
5823 static void
5824 pop_it (struct it *it)
5825 {
5826 struct iterator_stack_entry *p;
5827 int from_display_prop = it->from_disp_prop_p;
5828
5829 eassert (it->sp > 0);
5830 --it->sp;
5831 p = it->stack + it->sp;
5832 it->stop_charpos = p->stop_charpos;
5833 it->prev_stop = p->prev_stop;
5834 it->base_level_stop = p->base_level_stop;
5835 it->cmp_it = p->cmp_it;
5836 it->face_id = p->face_id;
5837 it->current = p->current;
5838 it->position = p->position;
5839 it->string = p->string;
5840 it->from_overlay = p->from_overlay;
5841 if (NILP (it->string))
5842 SET_TEXT_POS (it->current.string_pos, -1, -1);
5843 it->method = p->method;
5844 switch (it->method)
5845 {
5846 case GET_FROM_IMAGE:
5847 it->image_id = p->u.image.image_id;
5848 it->object = p->u.image.object;
5849 it->slice = p->u.image.slice;
5850 break;
5851 case GET_FROM_STRETCH:
5852 it->object = p->u.stretch.object;
5853 break;
5854 case GET_FROM_BUFFER:
5855 it->object = it->w->buffer;
5856 break;
5857 case GET_FROM_STRING:
5858 it->object = it->string;
5859 break;
5860 case GET_FROM_DISPLAY_VECTOR:
5861 if (it->s)
5862 it->method = GET_FROM_C_STRING;
5863 else if (STRINGP (it->string))
5864 it->method = GET_FROM_STRING;
5865 else
5866 {
5867 it->method = GET_FROM_BUFFER;
5868 it->object = it->w->buffer;
5869 }
5870 }
5871 it->end_charpos = p->end_charpos;
5872 it->string_nchars = p->string_nchars;
5873 it->area = p->area;
5874 it->multibyte_p = p->multibyte_p;
5875 it->avoid_cursor_p = p->avoid_cursor_p;
5876 it->space_width = p->space_width;
5877 it->font_height = p->font_height;
5878 it->voffset = p->voffset;
5879 it->string_from_display_prop_p = p->string_from_display_prop_p;
5880 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5881 it->line_wrap = p->line_wrap;
5882 it->bidi_p = p->bidi_p;
5883 it->paragraph_embedding = p->paragraph_embedding;
5884 it->from_disp_prop_p = p->from_disp_prop_p;
5885 if (it->bidi_p)
5886 {
5887 bidi_pop_it (&it->bidi_it);
5888 /* Bidi-iterate until we get out of the portion of text, if any,
5889 covered by a `display' text property or by an overlay with
5890 `display' property. (We cannot just jump there, because the
5891 internal coherency of the bidi iterator state can not be
5892 preserved across such jumps.) We also must determine the
5893 paragraph base direction if the overlay we just processed is
5894 at the beginning of a new paragraph. */
5895 if (from_display_prop
5896 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5897 iterate_out_of_display_property (it);
5898
5899 eassert ((BUFFERP (it->object)
5900 && IT_CHARPOS (*it) == it->bidi_it.charpos
5901 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5902 || (STRINGP (it->object)
5903 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5904 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5905 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5906 }
5907 }
5908
5909
5910 \f
5911 /***********************************************************************
5912 Moving over lines
5913 ***********************************************************************/
5914
5915 /* Set IT's current position to the previous line start. */
5916
5917 static void
5918 back_to_previous_line_start (struct it *it)
5919 {
5920 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5921 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5922 }
5923
5924
5925 /* Move IT to the next line start.
5926
5927 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5928 we skipped over part of the text (as opposed to moving the iterator
5929 continuously over the text). Otherwise, don't change the value
5930 of *SKIPPED_P.
5931
5932 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5933 iterator on the newline, if it was found.
5934
5935 Newlines may come from buffer text, overlay strings, or strings
5936 displayed via the `display' property. That's the reason we can't
5937 simply use find_next_newline_no_quit.
5938
5939 Note that this function may not skip over invisible text that is so
5940 because of text properties and immediately follows a newline. If
5941 it would, function reseat_at_next_visible_line_start, when called
5942 from set_iterator_to_next, would effectively make invisible
5943 characters following a newline part of the wrong glyph row, which
5944 leads to wrong cursor motion. */
5945
5946 static int
5947 forward_to_next_line_start (struct it *it, int *skipped_p,
5948 struct bidi_it *bidi_it_prev)
5949 {
5950 ptrdiff_t old_selective;
5951 int newline_found_p, n;
5952 const int MAX_NEWLINE_DISTANCE = 500;
5953
5954 /* If already on a newline, just consume it to avoid unintended
5955 skipping over invisible text below. */
5956 if (it->what == IT_CHARACTER
5957 && it->c == '\n'
5958 && CHARPOS (it->position) == IT_CHARPOS (*it))
5959 {
5960 if (it->bidi_p && bidi_it_prev)
5961 *bidi_it_prev = it->bidi_it;
5962 set_iterator_to_next (it, 0);
5963 it->c = 0;
5964 return 1;
5965 }
5966
5967 /* Don't handle selective display in the following. It's (a)
5968 unnecessary because it's done by the caller, and (b) leads to an
5969 infinite recursion because next_element_from_ellipsis indirectly
5970 calls this function. */
5971 old_selective = it->selective;
5972 it->selective = 0;
5973
5974 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5975 from buffer text. */
5976 for (n = newline_found_p = 0;
5977 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5978 n += STRINGP (it->string) ? 0 : 1)
5979 {
5980 if (!get_next_display_element (it))
5981 return 0;
5982 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
5983 if (newline_found_p && it->bidi_p && bidi_it_prev)
5984 *bidi_it_prev = it->bidi_it;
5985 set_iterator_to_next (it, 0);
5986 }
5987
5988 /* If we didn't find a newline near enough, see if we can use a
5989 short-cut. */
5990 if (!newline_found_p)
5991 {
5992 ptrdiff_t start = IT_CHARPOS (*it);
5993 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
5994 Lisp_Object pos;
5995
5996 eassert (!STRINGP (it->string));
5997
5998 /* If there isn't any `display' property in sight, and no
5999 overlays, we can just use the position of the newline in
6000 buffer text. */
6001 if (it->stop_charpos >= limit
6002 || ((pos = Fnext_single_property_change (make_number (start),
6003 Qdisplay, Qnil,
6004 make_number (limit)),
6005 NILP (pos))
6006 && next_overlay_change (start) == ZV))
6007 {
6008 if (!it->bidi_p)
6009 {
6010 IT_CHARPOS (*it) = limit;
6011 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
6012 }
6013 else
6014 {
6015 struct bidi_it bprev;
6016
6017 /* Help bidi.c avoid expensive searches for display
6018 properties and overlays, by telling it that there are
6019 none up to `limit'. */
6020 if (it->bidi_it.disp_pos < limit)
6021 {
6022 it->bidi_it.disp_pos = limit;
6023 it->bidi_it.disp_prop = 0;
6024 }
6025 do {
6026 bprev = it->bidi_it;
6027 bidi_move_to_visually_next (&it->bidi_it);
6028 } while (it->bidi_it.charpos != limit);
6029 IT_CHARPOS (*it) = limit;
6030 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6031 if (bidi_it_prev)
6032 *bidi_it_prev = bprev;
6033 }
6034 *skipped_p = newline_found_p = 1;
6035 }
6036 else
6037 {
6038 while (get_next_display_element (it)
6039 && !newline_found_p)
6040 {
6041 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6042 if (newline_found_p && it->bidi_p && bidi_it_prev)
6043 *bidi_it_prev = it->bidi_it;
6044 set_iterator_to_next (it, 0);
6045 }
6046 }
6047 }
6048
6049 it->selective = old_selective;
6050 return newline_found_p;
6051 }
6052
6053
6054 /* Set IT's current position to the previous visible line start. Skip
6055 invisible text that is so either due to text properties or due to
6056 selective display. Caution: this does not change IT->current_x and
6057 IT->hpos. */
6058
6059 static void
6060 back_to_previous_visible_line_start (struct it *it)
6061 {
6062 while (IT_CHARPOS (*it) > BEGV)
6063 {
6064 back_to_previous_line_start (it);
6065
6066 if (IT_CHARPOS (*it) <= BEGV)
6067 break;
6068
6069 /* If selective > 0, then lines indented more than its value are
6070 invisible. */
6071 if (it->selective > 0
6072 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6073 it->selective))
6074 continue;
6075
6076 /* Check the newline before point for invisibility. */
6077 {
6078 Lisp_Object prop;
6079 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6080 Qinvisible, it->window);
6081 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6082 continue;
6083 }
6084
6085 if (IT_CHARPOS (*it) <= BEGV)
6086 break;
6087
6088 {
6089 struct it it2;
6090 void *it2data = NULL;
6091 ptrdiff_t pos;
6092 ptrdiff_t beg, end;
6093 Lisp_Object val, overlay;
6094
6095 SAVE_IT (it2, *it, it2data);
6096
6097 /* If newline is part of a composition, continue from start of composition */
6098 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6099 && beg < IT_CHARPOS (*it))
6100 goto replaced;
6101
6102 /* If newline is replaced by a display property, find start of overlay
6103 or interval and continue search from that point. */
6104 pos = --IT_CHARPOS (it2);
6105 --IT_BYTEPOS (it2);
6106 it2.sp = 0;
6107 bidi_unshelve_cache (NULL, 0);
6108 it2.string_from_display_prop_p = 0;
6109 it2.from_disp_prop_p = 0;
6110 if (handle_display_prop (&it2) == HANDLED_RETURN
6111 && !NILP (val = get_char_property_and_overlay
6112 (make_number (pos), Qdisplay, Qnil, &overlay))
6113 && (OVERLAYP (overlay)
6114 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6115 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6116 {
6117 RESTORE_IT (it, it, it2data);
6118 goto replaced;
6119 }
6120
6121 /* Newline is not replaced by anything -- so we are done. */
6122 RESTORE_IT (it, it, it2data);
6123 break;
6124
6125 replaced:
6126 if (beg < BEGV)
6127 beg = BEGV;
6128 IT_CHARPOS (*it) = beg;
6129 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6130 }
6131 }
6132
6133 it->continuation_lines_width = 0;
6134
6135 eassert (IT_CHARPOS (*it) >= BEGV);
6136 eassert (IT_CHARPOS (*it) == BEGV
6137 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6138 CHECK_IT (it);
6139 }
6140
6141
6142 /* Reseat iterator IT at the previous visible line start. Skip
6143 invisible text that is so either due to text properties or due to
6144 selective display. At the end, update IT's overlay information,
6145 face information etc. */
6146
6147 void
6148 reseat_at_previous_visible_line_start (struct it *it)
6149 {
6150 back_to_previous_visible_line_start (it);
6151 reseat (it, it->current.pos, 1);
6152 CHECK_IT (it);
6153 }
6154
6155
6156 /* Reseat iterator IT on the next visible line start in the current
6157 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6158 preceding the line start. Skip over invisible text that is so
6159 because of selective display. Compute faces, overlays etc at the
6160 new position. Note that this function does not skip over text that
6161 is invisible because of text properties. */
6162
6163 static void
6164 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6165 {
6166 int newline_found_p, skipped_p = 0;
6167 struct bidi_it bidi_it_prev;
6168
6169 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6170
6171 /* Skip over lines that are invisible because they are indented
6172 more than the value of IT->selective. */
6173 if (it->selective > 0)
6174 while (IT_CHARPOS (*it) < ZV
6175 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6176 it->selective))
6177 {
6178 eassert (IT_BYTEPOS (*it) == BEGV
6179 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6180 newline_found_p =
6181 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6182 }
6183
6184 /* Position on the newline if that's what's requested. */
6185 if (on_newline_p && newline_found_p)
6186 {
6187 if (STRINGP (it->string))
6188 {
6189 if (IT_STRING_CHARPOS (*it) > 0)
6190 {
6191 if (!it->bidi_p)
6192 {
6193 --IT_STRING_CHARPOS (*it);
6194 --IT_STRING_BYTEPOS (*it);
6195 }
6196 else
6197 {
6198 /* We need to restore the bidi iterator to the state
6199 it had on the newline, and resync the IT's
6200 position with that. */
6201 it->bidi_it = bidi_it_prev;
6202 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6203 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6204 }
6205 }
6206 }
6207 else if (IT_CHARPOS (*it) > BEGV)
6208 {
6209 if (!it->bidi_p)
6210 {
6211 --IT_CHARPOS (*it);
6212 --IT_BYTEPOS (*it);
6213 }
6214 else
6215 {
6216 /* We need to restore the bidi iterator to the state it
6217 had on the newline and resync IT with that. */
6218 it->bidi_it = bidi_it_prev;
6219 IT_CHARPOS (*it) = it->bidi_it.charpos;
6220 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6221 }
6222 reseat (it, it->current.pos, 0);
6223 }
6224 }
6225 else if (skipped_p)
6226 reseat (it, it->current.pos, 0);
6227
6228 CHECK_IT (it);
6229 }
6230
6231
6232 \f
6233 /***********************************************************************
6234 Changing an iterator's position
6235 ***********************************************************************/
6236
6237 /* Change IT's current position to POS in current_buffer. If FORCE_P
6238 is non-zero, always check for text properties at the new position.
6239 Otherwise, text properties are only looked up if POS >=
6240 IT->check_charpos of a property. */
6241
6242 static void
6243 reseat (struct it *it, struct text_pos pos, int force_p)
6244 {
6245 ptrdiff_t original_pos = IT_CHARPOS (*it);
6246
6247 reseat_1 (it, pos, 0);
6248
6249 /* Determine where to check text properties. Avoid doing it
6250 where possible because text property lookup is very expensive. */
6251 if (force_p
6252 || CHARPOS (pos) > it->stop_charpos
6253 || CHARPOS (pos) < original_pos)
6254 {
6255 if (it->bidi_p)
6256 {
6257 /* For bidi iteration, we need to prime prev_stop and
6258 base_level_stop with our best estimations. */
6259 /* Implementation note: Of course, POS is not necessarily a
6260 stop position, so assigning prev_pos to it is a lie; we
6261 should have called compute_stop_backwards. However, if
6262 the current buffer does not include any R2L characters,
6263 that call would be a waste of cycles, because the
6264 iterator will never move back, and thus never cross this
6265 "fake" stop position. So we delay that backward search
6266 until the time we really need it, in next_element_from_buffer. */
6267 if (CHARPOS (pos) != it->prev_stop)
6268 it->prev_stop = CHARPOS (pos);
6269 if (CHARPOS (pos) < it->base_level_stop)
6270 it->base_level_stop = 0; /* meaning it's unknown */
6271 handle_stop (it);
6272 }
6273 else
6274 {
6275 handle_stop (it);
6276 it->prev_stop = it->base_level_stop = 0;
6277 }
6278
6279 }
6280
6281 CHECK_IT (it);
6282 }
6283
6284
6285 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6286 IT->stop_pos to POS, also. */
6287
6288 static void
6289 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6290 {
6291 /* Don't call this function when scanning a C string. */
6292 eassert (it->s == NULL);
6293
6294 /* POS must be a reasonable value. */
6295 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6296
6297 it->current.pos = it->position = pos;
6298 it->end_charpos = ZV;
6299 it->dpvec = NULL;
6300 it->current.dpvec_index = -1;
6301 it->current.overlay_string_index = -1;
6302 IT_STRING_CHARPOS (*it) = -1;
6303 IT_STRING_BYTEPOS (*it) = -1;
6304 it->string = Qnil;
6305 it->method = GET_FROM_BUFFER;
6306 it->object = it->w->buffer;
6307 it->area = TEXT_AREA;
6308 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6309 it->sp = 0;
6310 it->string_from_display_prop_p = 0;
6311 it->string_from_prefix_prop_p = 0;
6312
6313 it->from_disp_prop_p = 0;
6314 it->face_before_selective_p = 0;
6315 if (it->bidi_p)
6316 {
6317 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6318 &it->bidi_it);
6319 bidi_unshelve_cache (NULL, 0);
6320 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6321 it->bidi_it.string.s = NULL;
6322 it->bidi_it.string.lstring = Qnil;
6323 it->bidi_it.string.bufpos = 0;
6324 it->bidi_it.string.unibyte = 0;
6325 }
6326
6327 if (set_stop_p)
6328 {
6329 it->stop_charpos = CHARPOS (pos);
6330 it->base_level_stop = CHARPOS (pos);
6331 }
6332 /* This make the information stored in it->cmp_it invalidate. */
6333 it->cmp_it.id = -1;
6334 }
6335
6336
6337 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6338 If S is non-null, it is a C string to iterate over. Otherwise,
6339 STRING gives a Lisp string to iterate over.
6340
6341 If PRECISION > 0, don't return more then PRECISION number of
6342 characters from the string.
6343
6344 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6345 characters have been returned. FIELD_WIDTH < 0 means an infinite
6346 field width.
6347
6348 MULTIBYTE = 0 means disable processing of multibyte characters,
6349 MULTIBYTE > 0 means enable it,
6350 MULTIBYTE < 0 means use IT->multibyte_p.
6351
6352 IT must be initialized via a prior call to init_iterator before
6353 calling this function. */
6354
6355 static void
6356 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6357 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6358 int multibyte)
6359 {
6360 /* No region in strings. */
6361 it->region_beg_charpos = it->region_end_charpos = -1;
6362
6363 /* No text property checks performed by default, but see below. */
6364 it->stop_charpos = -1;
6365
6366 /* Set iterator position and end position. */
6367 memset (&it->current, 0, sizeof it->current);
6368 it->current.overlay_string_index = -1;
6369 it->current.dpvec_index = -1;
6370 eassert (charpos >= 0);
6371
6372 /* If STRING is specified, use its multibyteness, otherwise use the
6373 setting of MULTIBYTE, if specified. */
6374 if (multibyte >= 0)
6375 it->multibyte_p = multibyte > 0;
6376
6377 /* Bidirectional reordering of strings is controlled by the default
6378 value of bidi-display-reordering. Don't try to reorder while
6379 loading loadup.el, as the necessary character property tables are
6380 not yet available. */
6381 it->bidi_p =
6382 NILP (Vpurify_flag)
6383 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6384
6385 if (s == NULL)
6386 {
6387 eassert (STRINGP (string));
6388 it->string = string;
6389 it->s = NULL;
6390 it->end_charpos = it->string_nchars = SCHARS (string);
6391 it->method = GET_FROM_STRING;
6392 it->current.string_pos = string_pos (charpos, string);
6393
6394 if (it->bidi_p)
6395 {
6396 it->bidi_it.string.lstring = string;
6397 it->bidi_it.string.s = NULL;
6398 it->bidi_it.string.schars = it->end_charpos;
6399 it->bidi_it.string.bufpos = 0;
6400 it->bidi_it.string.from_disp_str = 0;
6401 it->bidi_it.string.unibyte = !it->multibyte_p;
6402 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6403 FRAME_WINDOW_P (it->f), &it->bidi_it);
6404 }
6405 }
6406 else
6407 {
6408 it->s = (const unsigned char *) s;
6409 it->string = Qnil;
6410
6411 /* Note that we use IT->current.pos, not it->current.string_pos,
6412 for displaying C strings. */
6413 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6414 if (it->multibyte_p)
6415 {
6416 it->current.pos = c_string_pos (charpos, s, 1);
6417 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6418 }
6419 else
6420 {
6421 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6422 it->end_charpos = it->string_nchars = strlen (s);
6423 }
6424
6425 if (it->bidi_p)
6426 {
6427 it->bidi_it.string.lstring = Qnil;
6428 it->bidi_it.string.s = (const unsigned char *) s;
6429 it->bidi_it.string.schars = it->end_charpos;
6430 it->bidi_it.string.bufpos = 0;
6431 it->bidi_it.string.from_disp_str = 0;
6432 it->bidi_it.string.unibyte = !it->multibyte_p;
6433 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6434 &it->bidi_it);
6435 }
6436 it->method = GET_FROM_C_STRING;
6437 }
6438
6439 /* PRECISION > 0 means don't return more than PRECISION characters
6440 from the string. */
6441 if (precision > 0 && it->end_charpos - charpos > precision)
6442 {
6443 it->end_charpos = it->string_nchars = charpos + precision;
6444 if (it->bidi_p)
6445 it->bidi_it.string.schars = it->end_charpos;
6446 }
6447
6448 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6449 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6450 FIELD_WIDTH < 0 means infinite field width. This is useful for
6451 padding with `-' at the end of a mode line. */
6452 if (field_width < 0)
6453 field_width = INFINITY;
6454 /* Implementation note: We deliberately don't enlarge
6455 it->bidi_it.string.schars here to fit it->end_charpos, because
6456 the bidi iterator cannot produce characters out of thin air. */
6457 if (field_width > it->end_charpos - charpos)
6458 it->end_charpos = charpos + field_width;
6459
6460 /* Use the standard display table for displaying strings. */
6461 if (DISP_TABLE_P (Vstandard_display_table))
6462 it->dp = XCHAR_TABLE (Vstandard_display_table);
6463
6464 it->stop_charpos = charpos;
6465 it->prev_stop = charpos;
6466 it->base_level_stop = 0;
6467 if (it->bidi_p)
6468 {
6469 it->bidi_it.first_elt = 1;
6470 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6471 it->bidi_it.disp_pos = -1;
6472 }
6473 if (s == NULL && it->multibyte_p)
6474 {
6475 ptrdiff_t endpos = SCHARS (it->string);
6476 if (endpos > it->end_charpos)
6477 endpos = it->end_charpos;
6478 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6479 it->string);
6480 }
6481 CHECK_IT (it);
6482 }
6483
6484
6485 \f
6486 /***********************************************************************
6487 Iteration
6488 ***********************************************************************/
6489
6490 /* Map enum it_method value to corresponding next_element_from_* function. */
6491
6492 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6493 {
6494 next_element_from_buffer,
6495 next_element_from_display_vector,
6496 next_element_from_string,
6497 next_element_from_c_string,
6498 next_element_from_image,
6499 next_element_from_stretch
6500 };
6501
6502 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6503
6504
6505 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6506 (possibly with the following characters). */
6507
6508 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6509 ((IT)->cmp_it.id >= 0 \
6510 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6511 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6512 END_CHARPOS, (IT)->w, \
6513 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6514 (IT)->string)))
6515
6516
6517 /* Lookup the char-table Vglyphless_char_display for character C (-1
6518 if we want information for no-font case), and return the display
6519 method symbol. By side-effect, update it->what and
6520 it->glyphless_method. This function is called from
6521 get_next_display_element for each character element, and from
6522 x_produce_glyphs when no suitable font was found. */
6523
6524 Lisp_Object
6525 lookup_glyphless_char_display (int c, struct it *it)
6526 {
6527 Lisp_Object glyphless_method = Qnil;
6528
6529 if (CHAR_TABLE_P (Vglyphless_char_display)
6530 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6531 {
6532 if (c >= 0)
6533 {
6534 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6535 if (CONSP (glyphless_method))
6536 glyphless_method = FRAME_WINDOW_P (it->f)
6537 ? XCAR (glyphless_method)
6538 : XCDR (glyphless_method);
6539 }
6540 else
6541 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6542 }
6543
6544 retry:
6545 if (NILP (glyphless_method))
6546 {
6547 if (c >= 0)
6548 /* The default is to display the character by a proper font. */
6549 return Qnil;
6550 /* The default for the no-font case is to display an empty box. */
6551 glyphless_method = Qempty_box;
6552 }
6553 if (EQ (glyphless_method, Qzero_width))
6554 {
6555 if (c >= 0)
6556 return glyphless_method;
6557 /* This method can't be used for the no-font case. */
6558 glyphless_method = Qempty_box;
6559 }
6560 if (EQ (glyphless_method, Qthin_space))
6561 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6562 else if (EQ (glyphless_method, Qempty_box))
6563 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6564 else if (EQ (glyphless_method, Qhex_code))
6565 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6566 else if (STRINGP (glyphless_method))
6567 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6568 else
6569 {
6570 /* Invalid value. We use the default method. */
6571 glyphless_method = Qnil;
6572 goto retry;
6573 }
6574 it->what = IT_GLYPHLESS;
6575 return glyphless_method;
6576 }
6577
6578 /* Load IT's display element fields with information about the next
6579 display element from the current position of IT. Value is zero if
6580 end of buffer (or C string) is reached. */
6581
6582 static struct frame *last_escape_glyph_frame = NULL;
6583 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6584 static int last_escape_glyph_merged_face_id = 0;
6585
6586 struct frame *last_glyphless_glyph_frame = NULL;
6587 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6588 int last_glyphless_glyph_merged_face_id = 0;
6589
6590 static int
6591 get_next_display_element (struct it *it)
6592 {
6593 /* Non-zero means that we found a display element. Zero means that
6594 we hit the end of what we iterate over. Performance note: the
6595 function pointer `method' used here turns out to be faster than
6596 using a sequence of if-statements. */
6597 int success_p;
6598
6599 get_next:
6600 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6601
6602 if (it->what == IT_CHARACTER)
6603 {
6604 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6605 and only if (a) the resolved directionality of that character
6606 is R..." */
6607 /* FIXME: Do we need an exception for characters from display
6608 tables? */
6609 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6610 it->c = bidi_mirror_char (it->c);
6611 /* Map via display table or translate control characters.
6612 IT->c, IT->len etc. have been set to the next character by
6613 the function call above. If we have a display table, and it
6614 contains an entry for IT->c, translate it. Don't do this if
6615 IT->c itself comes from a display table, otherwise we could
6616 end up in an infinite recursion. (An alternative could be to
6617 count the recursion depth of this function and signal an
6618 error when a certain maximum depth is reached.) Is it worth
6619 it? */
6620 if (success_p && it->dpvec == NULL)
6621 {
6622 Lisp_Object dv;
6623 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6624 int nonascii_space_p = 0;
6625 int nonascii_hyphen_p = 0;
6626 int c = it->c; /* This is the character to display. */
6627
6628 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6629 {
6630 eassert (SINGLE_BYTE_CHAR_P (c));
6631 if (unibyte_display_via_language_environment)
6632 {
6633 c = DECODE_CHAR (unibyte, c);
6634 if (c < 0)
6635 c = BYTE8_TO_CHAR (it->c);
6636 }
6637 else
6638 c = BYTE8_TO_CHAR (it->c);
6639 }
6640
6641 if (it->dp
6642 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6643 VECTORP (dv)))
6644 {
6645 struct Lisp_Vector *v = XVECTOR (dv);
6646
6647 /* Return the first character from the display table
6648 entry, if not empty. If empty, don't display the
6649 current character. */
6650 if (v->header.size)
6651 {
6652 it->dpvec_char_len = it->len;
6653 it->dpvec = v->contents;
6654 it->dpend = v->contents + v->header.size;
6655 it->current.dpvec_index = 0;
6656 it->dpvec_face_id = -1;
6657 it->saved_face_id = it->face_id;
6658 it->method = GET_FROM_DISPLAY_VECTOR;
6659 it->ellipsis_p = 0;
6660 }
6661 else
6662 {
6663 set_iterator_to_next (it, 0);
6664 }
6665 goto get_next;
6666 }
6667
6668 if (! NILP (lookup_glyphless_char_display (c, it)))
6669 {
6670 if (it->what == IT_GLYPHLESS)
6671 goto done;
6672 /* Don't display this character. */
6673 set_iterator_to_next (it, 0);
6674 goto get_next;
6675 }
6676
6677 /* If `nobreak-char-display' is non-nil, we display
6678 non-ASCII spaces and hyphens specially. */
6679 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6680 {
6681 if (c == 0xA0)
6682 nonascii_space_p = 1;
6683 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6684 nonascii_hyphen_p = 1;
6685 }
6686
6687 /* Translate control characters into `\003' or `^C' form.
6688 Control characters coming from a display table entry are
6689 currently not translated because we use IT->dpvec to hold
6690 the translation. This could easily be changed but I
6691 don't believe that it is worth doing.
6692
6693 The characters handled by `nobreak-char-display' must be
6694 translated too.
6695
6696 Non-printable characters and raw-byte characters are also
6697 translated to octal form. */
6698 if (((c < ' ' || c == 127) /* ASCII control chars */
6699 ? (it->area != TEXT_AREA
6700 /* In mode line, treat \n, \t like other crl chars. */
6701 || (c != '\t'
6702 && it->glyph_row
6703 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6704 || (c != '\n' && c != '\t'))
6705 : (nonascii_space_p
6706 || nonascii_hyphen_p
6707 || CHAR_BYTE8_P (c)
6708 || ! CHAR_PRINTABLE_P (c))))
6709 {
6710 /* C is a control character, non-ASCII space/hyphen,
6711 raw-byte, or a non-printable character which must be
6712 displayed either as '\003' or as `^C' where the '\\'
6713 and '^' can be defined in the display table. Fill
6714 IT->ctl_chars with glyphs for what we have to
6715 display. Then, set IT->dpvec to these glyphs. */
6716 Lisp_Object gc;
6717 int ctl_len;
6718 int face_id;
6719 int lface_id = 0;
6720 int escape_glyph;
6721
6722 /* Handle control characters with ^. */
6723
6724 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6725 {
6726 int g;
6727
6728 g = '^'; /* default glyph for Control */
6729 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6730 if (it->dp
6731 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6732 {
6733 g = GLYPH_CODE_CHAR (gc);
6734 lface_id = GLYPH_CODE_FACE (gc);
6735 }
6736 if (lface_id)
6737 {
6738 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6739 }
6740 else if (it->f == last_escape_glyph_frame
6741 && it->face_id == last_escape_glyph_face_id)
6742 {
6743 face_id = last_escape_glyph_merged_face_id;
6744 }
6745 else
6746 {
6747 /* Merge the escape-glyph face into the current face. */
6748 face_id = merge_faces (it->f, Qescape_glyph, 0,
6749 it->face_id);
6750 last_escape_glyph_frame = it->f;
6751 last_escape_glyph_face_id = it->face_id;
6752 last_escape_glyph_merged_face_id = face_id;
6753 }
6754
6755 XSETINT (it->ctl_chars[0], g);
6756 XSETINT (it->ctl_chars[1], c ^ 0100);
6757 ctl_len = 2;
6758 goto display_control;
6759 }
6760
6761 /* Handle non-ascii space in the mode where it only gets
6762 highlighting. */
6763
6764 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6765 {
6766 /* Merge `nobreak-space' into the current face. */
6767 face_id = merge_faces (it->f, Qnobreak_space, 0,
6768 it->face_id);
6769 XSETINT (it->ctl_chars[0], ' ');
6770 ctl_len = 1;
6771 goto display_control;
6772 }
6773
6774 /* Handle sequences that start with the "escape glyph". */
6775
6776 /* the default escape glyph is \. */
6777 escape_glyph = '\\';
6778
6779 if (it->dp
6780 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6781 {
6782 escape_glyph = GLYPH_CODE_CHAR (gc);
6783 lface_id = GLYPH_CODE_FACE (gc);
6784 }
6785 if (lface_id)
6786 {
6787 /* The display table specified a face.
6788 Merge it into face_id and also into escape_glyph. */
6789 face_id = merge_faces (it->f, Qt, lface_id,
6790 it->face_id);
6791 }
6792 else if (it->f == last_escape_glyph_frame
6793 && it->face_id == last_escape_glyph_face_id)
6794 {
6795 face_id = last_escape_glyph_merged_face_id;
6796 }
6797 else
6798 {
6799 /* Merge the escape-glyph face into the current face. */
6800 face_id = merge_faces (it->f, Qescape_glyph, 0,
6801 it->face_id);
6802 last_escape_glyph_frame = it->f;
6803 last_escape_glyph_face_id = it->face_id;
6804 last_escape_glyph_merged_face_id = face_id;
6805 }
6806
6807 /* Draw non-ASCII hyphen with just highlighting: */
6808
6809 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6810 {
6811 XSETINT (it->ctl_chars[0], '-');
6812 ctl_len = 1;
6813 goto display_control;
6814 }
6815
6816 /* Draw non-ASCII space/hyphen with escape glyph: */
6817
6818 if (nonascii_space_p || nonascii_hyphen_p)
6819 {
6820 XSETINT (it->ctl_chars[0], escape_glyph);
6821 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6822 ctl_len = 2;
6823 goto display_control;
6824 }
6825
6826 {
6827 char str[10];
6828 int len, i;
6829
6830 if (CHAR_BYTE8_P (c))
6831 /* Display \200 instead of \17777600. */
6832 c = CHAR_TO_BYTE8 (c);
6833 len = sprintf (str, "%03o", c);
6834
6835 XSETINT (it->ctl_chars[0], escape_glyph);
6836 for (i = 0; i < len; i++)
6837 XSETINT (it->ctl_chars[i + 1], str[i]);
6838 ctl_len = len + 1;
6839 }
6840
6841 display_control:
6842 /* Set up IT->dpvec and return first character from it. */
6843 it->dpvec_char_len = it->len;
6844 it->dpvec = it->ctl_chars;
6845 it->dpend = it->dpvec + ctl_len;
6846 it->current.dpvec_index = 0;
6847 it->dpvec_face_id = face_id;
6848 it->saved_face_id = it->face_id;
6849 it->method = GET_FROM_DISPLAY_VECTOR;
6850 it->ellipsis_p = 0;
6851 goto get_next;
6852 }
6853 it->char_to_display = c;
6854 }
6855 else if (success_p)
6856 {
6857 it->char_to_display = it->c;
6858 }
6859 }
6860
6861 /* Adjust face id for a multibyte character. There are no multibyte
6862 character in unibyte text. */
6863 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6864 && it->multibyte_p
6865 && success_p
6866 && FRAME_WINDOW_P (it->f))
6867 {
6868 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6869
6870 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6871 {
6872 /* Automatic composition with glyph-string. */
6873 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6874
6875 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6876 }
6877 else
6878 {
6879 ptrdiff_t pos = (it->s ? -1
6880 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6881 : IT_CHARPOS (*it));
6882 int c;
6883
6884 if (it->what == IT_CHARACTER)
6885 c = it->char_to_display;
6886 else
6887 {
6888 struct composition *cmp = composition_table[it->cmp_it.id];
6889 int i;
6890
6891 c = ' ';
6892 for (i = 0; i < cmp->glyph_len; i++)
6893 /* TAB in a composition means display glyphs with
6894 padding space on the left or right. */
6895 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6896 break;
6897 }
6898 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6899 }
6900 }
6901
6902 done:
6903 /* Is this character the last one of a run of characters with
6904 box? If yes, set IT->end_of_box_run_p to 1. */
6905 if (it->face_box_p
6906 && it->s == NULL)
6907 {
6908 if (it->method == GET_FROM_STRING && it->sp)
6909 {
6910 int face_id = underlying_face_id (it);
6911 struct face *face = FACE_FROM_ID (it->f, face_id);
6912
6913 if (face)
6914 {
6915 if (face->box == FACE_NO_BOX)
6916 {
6917 /* If the box comes from face properties in a
6918 display string, check faces in that string. */
6919 int string_face_id = face_after_it_pos (it);
6920 it->end_of_box_run_p
6921 = (FACE_FROM_ID (it->f, string_face_id)->box
6922 == FACE_NO_BOX);
6923 }
6924 /* Otherwise, the box comes from the underlying face.
6925 If this is the last string character displayed, check
6926 the next buffer location. */
6927 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6928 && (it->current.overlay_string_index
6929 == it->n_overlay_strings - 1))
6930 {
6931 ptrdiff_t ignore;
6932 int next_face_id;
6933 struct text_pos pos = it->current.pos;
6934 INC_TEXT_POS (pos, it->multibyte_p);
6935
6936 next_face_id = face_at_buffer_position
6937 (it->w, CHARPOS (pos), it->region_beg_charpos,
6938 it->region_end_charpos, &ignore,
6939 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6940 -1);
6941 it->end_of_box_run_p
6942 = (FACE_FROM_ID (it->f, next_face_id)->box
6943 == FACE_NO_BOX);
6944 }
6945 }
6946 }
6947 else
6948 {
6949 int face_id = face_after_it_pos (it);
6950 it->end_of_box_run_p
6951 = (face_id != it->face_id
6952 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6953 }
6954 }
6955 /* If we reached the end of the object we've been iterating (e.g., a
6956 display string or an overlay string), and there's something on
6957 IT->stack, proceed with what's on the stack. It doesn't make
6958 sense to return zero if there's unprocessed stuff on the stack,
6959 because otherwise that stuff will never be displayed. */
6960 if (!success_p && it->sp > 0)
6961 {
6962 set_iterator_to_next (it, 0);
6963 success_p = get_next_display_element (it);
6964 }
6965
6966 /* Value is 0 if end of buffer or string reached. */
6967 return success_p;
6968 }
6969
6970
6971 /* Move IT to the next display element.
6972
6973 RESEAT_P non-zero means if called on a newline in buffer text,
6974 skip to the next visible line start.
6975
6976 Functions get_next_display_element and set_iterator_to_next are
6977 separate because I find this arrangement easier to handle than a
6978 get_next_display_element function that also increments IT's
6979 position. The way it is we can first look at an iterator's current
6980 display element, decide whether it fits on a line, and if it does,
6981 increment the iterator position. The other way around we probably
6982 would either need a flag indicating whether the iterator has to be
6983 incremented the next time, or we would have to implement a
6984 decrement position function which would not be easy to write. */
6985
6986 void
6987 set_iterator_to_next (struct it *it, int reseat_p)
6988 {
6989 /* Reset flags indicating start and end of a sequence of characters
6990 with box. Reset them at the start of this function because
6991 moving the iterator to a new position might set them. */
6992 it->start_of_box_run_p = it->end_of_box_run_p = 0;
6993
6994 switch (it->method)
6995 {
6996 case GET_FROM_BUFFER:
6997 /* The current display element of IT is a character from
6998 current_buffer. Advance in the buffer, and maybe skip over
6999 invisible lines that are so because of selective display. */
7000 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7001 reseat_at_next_visible_line_start (it, 0);
7002 else if (it->cmp_it.id >= 0)
7003 {
7004 /* We are currently getting glyphs from a composition. */
7005 int i;
7006
7007 if (! it->bidi_p)
7008 {
7009 IT_CHARPOS (*it) += it->cmp_it.nchars;
7010 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7011 if (it->cmp_it.to < it->cmp_it.nglyphs)
7012 {
7013 it->cmp_it.from = it->cmp_it.to;
7014 }
7015 else
7016 {
7017 it->cmp_it.id = -1;
7018 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7019 IT_BYTEPOS (*it),
7020 it->end_charpos, Qnil);
7021 }
7022 }
7023 else if (! it->cmp_it.reversed_p)
7024 {
7025 /* Composition created while scanning forward. */
7026 /* Update IT's char/byte positions to point to the first
7027 character of the next grapheme cluster, or to the
7028 character visually after the current composition. */
7029 for (i = 0; i < it->cmp_it.nchars; i++)
7030 bidi_move_to_visually_next (&it->bidi_it);
7031 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7032 IT_CHARPOS (*it) = it->bidi_it.charpos;
7033
7034 if (it->cmp_it.to < it->cmp_it.nglyphs)
7035 {
7036 /* Proceed to the next grapheme cluster. */
7037 it->cmp_it.from = it->cmp_it.to;
7038 }
7039 else
7040 {
7041 /* No more grapheme clusters in this composition.
7042 Find the next stop position. */
7043 ptrdiff_t stop = it->end_charpos;
7044 if (it->bidi_it.scan_dir < 0)
7045 /* Now we are scanning backward and don't know
7046 where to stop. */
7047 stop = -1;
7048 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7049 IT_BYTEPOS (*it), stop, Qnil);
7050 }
7051 }
7052 else
7053 {
7054 /* Composition created while scanning backward. */
7055 /* Update IT's char/byte positions to point to the last
7056 character of the previous grapheme cluster, or the
7057 character visually after the current composition. */
7058 for (i = 0; i < it->cmp_it.nchars; i++)
7059 bidi_move_to_visually_next (&it->bidi_it);
7060 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7061 IT_CHARPOS (*it) = it->bidi_it.charpos;
7062 if (it->cmp_it.from > 0)
7063 {
7064 /* Proceed to the previous grapheme cluster. */
7065 it->cmp_it.to = it->cmp_it.from;
7066 }
7067 else
7068 {
7069 /* No more grapheme clusters in this composition.
7070 Find the next stop position. */
7071 ptrdiff_t stop = it->end_charpos;
7072 if (it->bidi_it.scan_dir < 0)
7073 /* Now we are scanning backward and don't know
7074 where to stop. */
7075 stop = -1;
7076 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7077 IT_BYTEPOS (*it), stop, Qnil);
7078 }
7079 }
7080 }
7081 else
7082 {
7083 eassert (it->len != 0);
7084
7085 if (!it->bidi_p)
7086 {
7087 IT_BYTEPOS (*it) += it->len;
7088 IT_CHARPOS (*it) += 1;
7089 }
7090 else
7091 {
7092 int prev_scan_dir = it->bidi_it.scan_dir;
7093 /* If this is a new paragraph, determine its base
7094 direction (a.k.a. its base embedding level). */
7095 if (it->bidi_it.new_paragraph)
7096 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7097 bidi_move_to_visually_next (&it->bidi_it);
7098 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7099 IT_CHARPOS (*it) = it->bidi_it.charpos;
7100 if (prev_scan_dir != it->bidi_it.scan_dir)
7101 {
7102 /* As the scan direction was changed, we must
7103 re-compute the stop position for composition. */
7104 ptrdiff_t stop = it->end_charpos;
7105 if (it->bidi_it.scan_dir < 0)
7106 stop = -1;
7107 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7108 IT_BYTEPOS (*it), stop, Qnil);
7109 }
7110 }
7111 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7112 }
7113 break;
7114
7115 case GET_FROM_C_STRING:
7116 /* Current display element of IT is from a C string. */
7117 if (!it->bidi_p
7118 /* If the string position is beyond string's end, it means
7119 next_element_from_c_string is padding the string with
7120 blanks, in which case we bypass the bidi iterator,
7121 because it cannot deal with such virtual characters. */
7122 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7123 {
7124 IT_BYTEPOS (*it) += it->len;
7125 IT_CHARPOS (*it) += 1;
7126 }
7127 else
7128 {
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 break;
7134
7135 case GET_FROM_DISPLAY_VECTOR:
7136 /* Current display element of IT is from a display table entry.
7137 Advance in the display table definition. Reset it to null if
7138 end reached, and continue with characters from buffers/
7139 strings. */
7140 ++it->current.dpvec_index;
7141
7142 /* Restore face of the iterator to what they were before the
7143 display vector entry (these entries may contain faces). */
7144 it->face_id = it->saved_face_id;
7145
7146 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7147 {
7148 int recheck_faces = it->ellipsis_p;
7149
7150 if (it->s)
7151 it->method = GET_FROM_C_STRING;
7152 else if (STRINGP (it->string))
7153 it->method = GET_FROM_STRING;
7154 else
7155 {
7156 it->method = GET_FROM_BUFFER;
7157 it->object = it->w->buffer;
7158 }
7159
7160 it->dpvec = NULL;
7161 it->current.dpvec_index = -1;
7162
7163 /* Skip over characters which were displayed via IT->dpvec. */
7164 if (it->dpvec_char_len < 0)
7165 reseat_at_next_visible_line_start (it, 1);
7166 else if (it->dpvec_char_len > 0)
7167 {
7168 if (it->method == GET_FROM_STRING
7169 && it->n_overlay_strings > 0)
7170 it->ignore_overlay_strings_at_pos_p = 1;
7171 it->len = it->dpvec_char_len;
7172 set_iterator_to_next (it, reseat_p);
7173 }
7174
7175 /* Maybe recheck faces after display vector */
7176 if (recheck_faces)
7177 it->stop_charpos = IT_CHARPOS (*it);
7178 }
7179 break;
7180
7181 case GET_FROM_STRING:
7182 /* Current display element is a character from a Lisp string. */
7183 eassert (it->s == NULL && STRINGP (it->string));
7184 /* Don't advance past string end. These conditions are true
7185 when set_iterator_to_next is called at the end of
7186 get_next_display_element, in which case the Lisp string is
7187 already exhausted, and all we want is pop the iterator
7188 stack. */
7189 if (it->current.overlay_string_index >= 0)
7190 {
7191 /* This is an overlay string, so there's no padding with
7192 spaces, and the number of characters in the string is
7193 where the string ends. */
7194 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7195 goto consider_string_end;
7196 }
7197 else
7198 {
7199 /* Not an overlay string. There could be padding, so test
7200 against it->end_charpos . */
7201 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7202 goto consider_string_end;
7203 }
7204 if (it->cmp_it.id >= 0)
7205 {
7206 int i;
7207
7208 if (! it->bidi_p)
7209 {
7210 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7211 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7212 if (it->cmp_it.to < it->cmp_it.nglyphs)
7213 it->cmp_it.from = it->cmp_it.to;
7214 else
7215 {
7216 it->cmp_it.id = -1;
7217 composition_compute_stop_pos (&it->cmp_it,
7218 IT_STRING_CHARPOS (*it),
7219 IT_STRING_BYTEPOS (*it),
7220 it->end_charpos, it->string);
7221 }
7222 }
7223 else if (! it->cmp_it.reversed_p)
7224 {
7225 for (i = 0; i < it->cmp_it.nchars; i++)
7226 bidi_move_to_visually_next (&it->bidi_it);
7227 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7228 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7229
7230 if (it->cmp_it.to < it->cmp_it.nglyphs)
7231 it->cmp_it.from = it->cmp_it.to;
7232 else
7233 {
7234 ptrdiff_t stop = it->end_charpos;
7235 if (it->bidi_it.scan_dir < 0)
7236 stop = -1;
7237 composition_compute_stop_pos (&it->cmp_it,
7238 IT_STRING_CHARPOS (*it),
7239 IT_STRING_BYTEPOS (*it), stop,
7240 it->string);
7241 }
7242 }
7243 else
7244 {
7245 for (i = 0; i < it->cmp_it.nchars; i++)
7246 bidi_move_to_visually_next (&it->bidi_it);
7247 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7248 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7249 if (it->cmp_it.from > 0)
7250 it->cmp_it.to = it->cmp_it.from;
7251 else
7252 {
7253 ptrdiff_t stop = it->end_charpos;
7254 if (it->bidi_it.scan_dir < 0)
7255 stop = -1;
7256 composition_compute_stop_pos (&it->cmp_it,
7257 IT_STRING_CHARPOS (*it),
7258 IT_STRING_BYTEPOS (*it), stop,
7259 it->string);
7260 }
7261 }
7262 }
7263 else
7264 {
7265 if (!it->bidi_p
7266 /* If the string position is beyond string's end, it
7267 means next_element_from_string is padding the string
7268 with blanks, in which case we bypass the bidi
7269 iterator, because it cannot deal with such virtual
7270 characters. */
7271 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7272 {
7273 IT_STRING_BYTEPOS (*it) += it->len;
7274 IT_STRING_CHARPOS (*it) += 1;
7275 }
7276 else
7277 {
7278 int prev_scan_dir = it->bidi_it.scan_dir;
7279
7280 bidi_move_to_visually_next (&it->bidi_it);
7281 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7282 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7283 if (prev_scan_dir != it->bidi_it.scan_dir)
7284 {
7285 ptrdiff_t stop = it->end_charpos;
7286
7287 if (it->bidi_it.scan_dir < 0)
7288 stop = -1;
7289 composition_compute_stop_pos (&it->cmp_it,
7290 IT_STRING_CHARPOS (*it),
7291 IT_STRING_BYTEPOS (*it), stop,
7292 it->string);
7293 }
7294 }
7295 }
7296
7297 consider_string_end:
7298
7299 if (it->current.overlay_string_index >= 0)
7300 {
7301 /* IT->string is an overlay string. Advance to the
7302 next, if there is one. */
7303 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7304 {
7305 it->ellipsis_p = 0;
7306 next_overlay_string (it);
7307 if (it->ellipsis_p)
7308 setup_for_ellipsis (it, 0);
7309 }
7310 }
7311 else
7312 {
7313 /* IT->string is not an overlay string. If we reached
7314 its end, and there is something on IT->stack, proceed
7315 with what is on the stack. This can be either another
7316 string, this time an overlay string, or a buffer. */
7317 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7318 && it->sp > 0)
7319 {
7320 pop_it (it);
7321 if (it->method == GET_FROM_STRING)
7322 goto consider_string_end;
7323 }
7324 }
7325 break;
7326
7327 case GET_FROM_IMAGE:
7328 case GET_FROM_STRETCH:
7329 /* The position etc with which we have to proceed are on
7330 the stack. The position may be at the end of a string,
7331 if the `display' property takes up the whole string. */
7332 eassert (it->sp > 0);
7333 pop_it (it);
7334 if (it->method == GET_FROM_STRING)
7335 goto consider_string_end;
7336 break;
7337
7338 default:
7339 /* There are no other methods defined, so this should be a bug. */
7340 emacs_abort ();
7341 }
7342
7343 eassert (it->method != GET_FROM_STRING
7344 || (STRINGP (it->string)
7345 && IT_STRING_CHARPOS (*it) >= 0));
7346 }
7347
7348 /* Load IT's display element fields with information about the next
7349 display element which comes from a display table entry or from the
7350 result of translating a control character to one of the forms `^C'
7351 or `\003'.
7352
7353 IT->dpvec holds the glyphs to return as characters.
7354 IT->saved_face_id holds the face id before the display vector--it
7355 is restored into IT->face_id in set_iterator_to_next. */
7356
7357 static int
7358 next_element_from_display_vector (struct it *it)
7359 {
7360 Lisp_Object gc;
7361
7362 /* Precondition. */
7363 eassert (it->dpvec && it->current.dpvec_index >= 0);
7364
7365 it->face_id = it->saved_face_id;
7366
7367 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7368 That seemed totally bogus - so I changed it... */
7369 gc = it->dpvec[it->current.dpvec_index];
7370
7371 if (GLYPH_CODE_P (gc))
7372 {
7373 it->c = GLYPH_CODE_CHAR (gc);
7374 it->len = CHAR_BYTES (it->c);
7375
7376 /* The entry may contain a face id to use. Such a face id is
7377 the id of a Lisp face, not a realized face. A face id of
7378 zero means no face is specified. */
7379 if (it->dpvec_face_id >= 0)
7380 it->face_id = it->dpvec_face_id;
7381 else
7382 {
7383 int lface_id = GLYPH_CODE_FACE (gc);
7384 if (lface_id > 0)
7385 it->face_id = merge_faces (it->f, Qt, lface_id,
7386 it->saved_face_id);
7387 }
7388 }
7389 else
7390 /* Display table entry is invalid. Return a space. */
7391 it->c = ' ', it->len = 1;
7392
7393 /* Don't change position and object of the iterator here. They are
7394 still the values of the character that had this display table
7395 entry or was translated, and that's what we want. */
7396 it->what = IT_CHARACTER;
7397 return 1;
7398 }
7399
7400 /* Get the first element of string/buffer in the visual order, after
7401 being reseated to a new position in a string or a buffer. */
7402 static void
7403 get_visually_first_element (struct it *it)
7404 {
7405 int string_p = STRINGP (it->string) || it->s;
7406 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7407 ptrdiff_t bob = (string_p ? 0 : BEGV);
7408
7409 if (STRINGP (it->string))
7410 {
7411 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7412 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7413 }
7414 else
7415 {
7416 it->bidi_it.charpos = IT_CHARPOS (*it);
7417 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7418 }
7419
7420 if (it->bidi_it.charpos == eob)
7421 {
7422 /* Nothing to do, but reset the FIRST_ELT flag, like
7423 bidi_paragraph_init does, because we are not going to
7424 call it. */
7425 it->bidi_it.first_elt = 0;
7426 }
7427 else if (it->bidi_it.charpos == bob
7428 || (!string_p
7429 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7430 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7431 {
7432 /* If we are at the beginning of a line/string, we can produce
7433 the next element right away. */
7434 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7435 bidi_move_to_visually_next (&it->bidi_it);
7436 }
7437 else
7438 {
7439 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7440
7441 /* We need to prime the bidi iterator starting at the line's or
7442 string's beginning, before we will be able to produce the
7443 next element. */
7444 if (string_p)
7445 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7446 else
7447 {
7448 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7449 -1);
7450 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7451 }
7452 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7453 do
7454 {
7455 /* Now return to buffer/string position where we were asked
7456 to get the next display element, and produce that. */
7457 bidi_move_to_visually_next (&it->bidi_it);
7458 }
7459 while (it->bidi_it.bytepos != orig_bytepos
7460 && it->bidi_it.charpos < eob);
7461 }
7462
7463 /* Adjust IT's position information to where we ended up. */
7464 if (STRINGP (it->string))
7465 {
7466 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7467 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7468 }
7469 else
7470 {
7471 IT_CHARPOS (*it) = it->bidi_it.charpos;
7472 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7473 }
7474
7475 if (STRINGP (it->string) || !it->s)
7476 {
7477 ptrdiff_t stop, charpos, bytepos;
7478
7479 if (STRINGP (it->string))
7480 {
7481 eassert (!it->s);
7482 stop = SCHARS (it->string);
7483 if (stop > it->end_charpos)
7484 stop = it->end_charpos;
7485 charpos = IT_STRING_CHARPOS (*it);
7486 bytepos = IT_STRING_BYTEPOS (*it);
7487 }
7488 else
7489 {
7490 stop = it->end_charpos;
7491 charpos = IT_CHARPOS (*it);
7492 bytepos = IT_BYTEPOS (*it);
7493 }
7494 if (it->bidi_it.scan_dir < 0)
7495 stop = -1;
7496 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7497 it->string);
7498 }
7499 }
7500
7501 /* Load IT with the next display element from Lisp string IT->string.
7502 IT->current.string_pos is the current position within the string.
7503 If IT->current.overlay_string_index >= 0, the Lisp string is an
7504 overlay string. */
7505
7506 static int
7507 next_element_from_string (struct it *it)
7508 {
7509 struct text_pos position;
7510
7511 eassert (STRINGP (it->string));
7512 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7513 eassert (IT_STRING_CHARPOS (*it) >= 0);
7514 position = it->current.string_pos;
7515
7516 /* With bidi reordering, the character to display might not be the
7517 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7518 that we were reseat()ed to a new string, whose paragraph
7519 direction is not known. */
7520 if (it->bidi_p && it->bidi_it.first_elt)
7521 {
7522 get_visually_first_element (it);
7523 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7524 }
7525
7526 /* Time to check for invisible text? */
7527 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7528 {
7529 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7530 {
7531 if (!(!it->bidi_p
7532 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7533 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7534 {
7535 /* With bidi non-linear iteration, we could find
7536 ourselves far beyond the last computed stop_charpos,
7537 with several other stop positions in between that we
7538 missed. Scan them all now, in buffer's logical
7539 order, until we find and handle the last stop_charpos
7540 that precedes our current position. */
7541 handle_stop_backwards (it, it->stop_charpos);
7542 return GET_NEXT_DISPLAY_ELEMENT (it);
7543 }
7544 else
7545 {
7546 if (it->bidi_p)
7547 {
7548 /* Take note of the stop position we just moved
7549 across, for when we will move back across it. */
7550 it->prev_stop = it->stop_charpos;
7551 /* If we are at base paragraph embedding level, take
7552 note of the last stop position seen at this
7553 level. */
7554 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7555 it->base_level_stop = it->stop_charpos;
7556 }
7557 handle_stop (it);
7558
7559 /* Since a handler may have changed IT->method, we must
7560 recurse here. */
7561 return GET_NEXT_DISPLAY_ELEMENT (it);
7562 }
7563 }
7564 else if (it->bidi_p
7565 /* If we are before prev_stop, we may have overstepped
7566 on our way backwards a stop_pos, and if so, we need
7567 to handle that stop_pos. */
7568 && IT_STRING_CHARPOS (*it) < it->prev_stop
7569 /* We can sometimes back up for reasons that have nothing
7570 to do with bidi reordering. E.g., compositions. The
7571 code below is only needed when we are above the base
7572 embedding level, so test for that explicitly. */
7573 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7574 {
7575 /* If we lost track of base_level_stop, we have no better
7576 place for handle_stop_backwards to start from than string
7577 beginning. This happens, e.g., when we were reseated to
7578 the previous screenful of text by vertical-motion. */
7579 if (it->base_level_stop <= 0
7580 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7581 it->base_level_stop = 0;
7582 handle_stop_backwards (it, it->base_level_stop);
7583 return GET_NEXT_DISPLAY_ELEMENT (it);
7584 }
7585 }
7586
7587 if (it->current.overlay_string_index >= 0)
7588 {
7589 /* Get the next character from an overlay string. In overlay
7590 strings, there is no field width or padding with spaces to
7591 do. */
7592 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7593 {
7594 it->what = IT_EOB;
7595 return 0;
7596 }
7597 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7598 IT_STRING_BYTEPOS (*it),
7599 it->bidi_it.scan_dir < 0
7600 ? -1
7601 : SCHARS (it->string))
7602 && next_element_from_composition (it))
7603 {
7604 return 1;
7605 }
7606 else if (STRING_MULTIBYTE (it->string))
7607 {
7608 const unsigned char *s = (SDATA (it->string)
7609 + IT_STRING_BYTEPOS (*it));
7610 it->c = string_char_and_length (s, &it->len);
7611 }
7612 else
7613 {
7614 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7615 it->len = 1;
7616 }
7617 }
7618 else
7619 {
7620 /* Get the next character from a Lisp string that is not an
7621 overlay string. Such strings come from the mode line, for
7622 example. We may have to pad with spaces, or truncate the
7623 string. See also next_element_from_c_string. */
7624 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7625 {
7626 it->what = IT_EOB;
7627 return 0;
7628 }
7629 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7630 {
7631 /* Pad with spaces. */
7632 it->c = ' ', it->len = 1;
7633 CHARPOS (position) = BYTEPOS (position) = -1;
7634 }
7635 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7636 IT_STRING_BYTEPOS (*it),
7637 it->bidi_it.scan_dir < 0
7638 ? -1
7639 : it->string_nchars)
7640 && next_element_from_composition (it))
7641 {
7642 return 1;
7643 }
7644 else if (STRING_MULTIBYTE (it->string))
7645 {
7646 const unsigned char *s = (SDATA (it->string)
7647 + IT_STRING_BYTEPOS (*it));
7648 it->c = string_char_and_length (s, &it->len);
7649 }
7650 else
7651 {
7652 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7653 it->len = 1;
7654 }
7655 }
7656
7657 /* Record what we have and where it came from. */
7658 it->what = IT_CHARACTER;
7659 it->object = it->string;
7660 it->position = position;
7661 return 1;
7662 }
7663
7664
7665 /* Load IT with next display element from C string IT->s.
7666 IT->string_nchars is the maximum number of characters to return
7667 from the string. IT->end_charpos may be greater than
7668 IT->string_nchars when this function is called, in which case we
7669 may have to return padding spaces. Value is zero if end of string
7670 reached, including padding spaces. */
7671
7672 static int
7673 next_element_from_c_string (struct it *it)
7674 {
7675 int success_p = 1;
7676
7677 eassert (it->s);
7678 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7679 it->what = IT_CHARACTER;
7680 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7681 it->object = Qnil;
7682
7683 /* With bidi reordering, the character to display might not be the
7684 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7685 we were reseated to a new string, whose paragraph direction is
7686 not known. */
7687 if (it->bidi_p && it->bidi_it.first_elt)
7688 get_visually_first_element (it);
7689
7690 /* IT's position can be greater than IT->string_nchars in case a
7691 field width or precision has been specified when the iterator was
7692 initialized. */
7693 if (IT_CHARPOS (*it) >= it->end_charpos)
7694 {
7695 /* End of the game. */
7696 it->what = IT_EOB;
7697 success_p = 0;
7698 }
7699 else if (IT_CHARPOS (*it) >= it->string_nchars)
7700 {
7701 /* Pad with spaces. */
7702 it->c = ' ', it->len = 1;
7703 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7704 }
7705 else if (it->multibyte_p)
7706 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7707 else
7708 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7709
7710 return success_p;
7711 }
7712
7713
7714 /* Set up IT to return characters from an ellipsis, if appropriate.
7715 The definition of the ellipsis glyphs may come from a display table
7716 entry. This function fills IT with the first glyph from the
7717 ellipsis if an ellipsis is to be displayed. */
7718
7719 static int
7720 next_element_from_ellipsis (struct it *it)
7721 {
7722 if (it->selective_display_ellipsis_p)
7723 setup_for_ellipsis (it, it->len);
7724 else
7725 {
7726 /* The face at the current position may be different from the
7727 face we find after the invisible text. Remember what it
7728 was in IT->saved_face_id, and signal that it's there by
7729 setting face_before_selective_p. */
7730 it->saved_face_id = it->face_id;
7731 it->method = GET_FROM_BUFFER;
7732 it->object = it->w->buffer;
7733 reseat_at_next_visible_line_start (it, 1);
7734 it->face_before_selective_p = 1;
7735 }
7736
7737 return GET_NEXT_DISPLAY_ELEMENT (it);
7738 }
7739
7740
7741 /* Deliver an image display element. The iterator IT is already
7742 filled with image information (done in handle_display_prop). Value
7743 is always 1. */
7744
7745
7746 static int
7747 next_element_from_image (struct it *it)
7748 {
7749 it->what = IT_IMAGE;
7750 it->ignore_overlay_strings_at_pos_p = 0;
7751 return 1;
7752 }
7753
7754
7755 /* Fill iterator IT with next display element from a stretch glyph
7756 property. IT->object is the value of the text property. Value is
7757 always 1. */
7758
7759 static int
7760 next_element_from_stretch (struct it *it)
7761 {
7762 it->what = IT_STRETCH;
7763 return 1;
7764 }
7765
7766 /* Scan backwards from IT's current position until we find a stop
7767 position, or until BEGV. This is called when we find ourself
7768 before both the last known prev_stop and base_level_stop while
7769 reordering bidirectional text. */
7770
7771 static void
7772 compute_stop_pos_backwards (struct it *it)
7773 {
7774 const int SCAN_BACK_LIMIT = 1000;
7775 struct text_pos pos;
7776 struct display_pos save_current = it->current;
7777 struct text_pos save_position = it->position;
7778 ptrdiff_t charpos = IT_CHARPOS (*it);
7779 ptrdiff_t where_we_are = charpos;
7780 ptrdiff_t save_stop_pos = it->stop_charpos;
7781 ptrdiff_t save_end_pos = it->end_charpos;
7782
7783 eassert (NILP (it->string) && !it->s);
7784 eassert (it->bidi_p);
7785 it->bidi_p = 0;
7786 do
7787 {
7788 it->end_charpos = min (charpos + 1, ZV);
7789 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7790 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7791 reseat_1 (it, pos, 0);
7792 compute_stop_pos (it);
7793 /* We must advance forward, right? */
7794 if (it->stop_charpos <= charpos)
7795 emacs_abort ();
7796 }
7797 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7798
7799 if (it->stop_charpos <= where_we_are)
7800 it->prev_stop = it->stop_charpos;
7801 else
7802 it->prev_stop = BEGV;
7803 it->bidi_p = 1;
7804 it->current = save_current;
7805 it->position = save_position;
7806 it->stop_charpos = save_stop_pos;
7807 it->end_charpos = save_end_pos;
7808 }
7809
7810 /* Scan forward from CHARPOS in the current buffer/string, until we
7811 find a stop position > current IT's position. Then handle the stop
7812 position before that. This is called when we bump into a stop
7813 position while reordering bidirectional text. CHARPOS should be
7814 the last previously processed stop_pos (or BEGV/0, if none were
7815 processed yet) whose position is less that IT's current
7816 position. */
7817
7818 static void
7819 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7820 {
7821 int bufp = !STRINGP (it->string);
7822 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7823 struct display_pos save_current = it->current;
7824 struct text_pos save_position = it->position;
7825 struct text_pos pos1;
7826 ptrdiff_t next_stop;
7827
7828 /* Scan in strict logical order. */
7829 eassert (it->bidi_p);
7830 it->bidi_p = 0;
7831 do
7832 {
7833 it->prev_stop = charpos;
7834 if (bufp)
7835 {
7836 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7837 reseat_1 (it, pos1, 0);
7838 }
7839 else
7840 it->current.string_pos = string_pos (charpos, it->string);
7841 compute_stop_pos (it);
7842 /* We must advance forward, right? */
7843 if (it->stop_charpos <= it->prev_stop)
7844 emacs_abort ();
7845 charpos = it->stop_charpos;
7846 }
7847 while (charpos <= where_we_are);
7848
7849 it->bidi_p = 1;
7850 it->current = save_current;
7851 it->position = save_position;
7852 next_stop = it->stop_charpos;
7853 it->stop_charpos = it->prev_stop;
7854 handle_stop (it);
7855 it->stop_charpos = next_stop;
7856 }
7857
7858 /* Load IT with the next display element from current_buffer. Value
7859 is zero if end of buffer reached. IT->stop_charpos is the next
7860 position at which to stop and check for text properties or buffer
7861 end. */
7862
7863 static int
7864 next_element_from_buffer (struct it *it)
7865 {
7866 int success_p = 1;
7867
7868 eassert (IT_CHARPOS (*it) >= BEGV);
7869 eassert (NILP (it->string) && !it->s);
7870 eassert (!it->bidi_p
7871 || (EQ (it->bidi_it.string.lstring, Qnil)
7872 && it->bidi_it.string.s == NULL));
7873
7874 /* With bidi reordering, the character to display might not be the
7875 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7876 we were reseat()ed to a new buffer position, which is potentially
7877 a different paragraph. */
7878 if (it->bidi_p && it->bidi_it.first_elt)
7879 {
7880 get_visually_first_element (it);
7881 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7882 }
7883
7884 if (IT_CHARPOS (*it) >= it->stop_charpos)
7885 {
7886 if (IT_CHARPOS (*it) >= it->end_charpos)
7887 {
7888 int overlay_strings_follow_p;
7889
7890 /* End of the game, except when overlay strings follow that
7891 haven't been returned yet. */
7892 if (it->overlay_strings_at_end_processed_p)
7893 overlay_strings_follow_p = 0;
7894 else
7895 {
7896 it->overlay_strings_at_end_processed_p = 1;
7897 overlay_strings_follow_p = get_overlay_strings (it, 0);
7898 }
7899
7900 if (overlay_strings_follow_p)
7901 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7902 else
7903 {
7904 it->what = IT_EOB;
7905 it->position = it->current.pos;
7906 success_p = 0;
7907 }
7908 }
7909 else if (!(!it->bidi_p
7910 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7911 || IT_CHARPOS (*it) == it->stop_charpos))
7912 {
7913 /* With bidi non-linear iteration, we could find ourselves
7914 far beyond the last computed stop_charpos, with several
7915 other stop positions in between that we missed. Scan
7916 them all now, in buffer's logical order, until we find
7917 and handle the last stop_charpos that precedes our
7918 current position. */
7919 handle_stop_backwards (it, it->stop_charpos);
7920 return GET_NEXT_DISPLAY_ELEMENT (it);
7921 }
7922 else
7923 {
7924 if (it->bidi_p)
7925 {
7926 /* Take note of the stop position we just moved across,
7927 for when we will move back across it. */
7928 it->prev_stop = it->stop_charpos;
7929 /* If we are at base paragraph embedding level, take
7930 note of the last stop position seen at this
7931 level. */
7932 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7933 it->base_level_stop = it->stop_charpos;
7934 }
7935 handle_stop (it);
7936 return GET_NEXT_DISPLAY_ELEMENT (it);
7937 }
7938 }
7939 else if (it->bidi_p
7940 /* If we are before prev_stop, we may have overstepped on
7941 our way backwards a stop_pos, and if so, we need to
7942 handle that stop_pos. */
7943 && IT_CHARPOS (*it) < it->prev_stop
7944 /* We can sometimes back up for reasons that have nothing
7945 to do with bidi reordering. E.g., compositions. The
7946 code below is only needed when we are above the base
7947 embedding level, so test for that explicitly. */
7948 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7949 {
7950 if (it->base_level_stop <= 0
7951 || IT_CHARPOS (*it) < it->base_level_stop)
7952 {
7953 /* If we lost track of base_level_stop, we need to find
7954 prev_stop by looking backwards. This happens, e.g., when
7955 we were reseated to the previous screenful of text by
7956 vertical-motion. */
7957 it->base_level_stop = BEGV;
7958 compute_stop_pos_backwards (it);
7959 handle_stop_backwards (it, it->prev_stop);
7960 }
7961 else
7962 handle_stop_backwards (it, it->base_level_stop);
7963 return GET_NEXT_DISPLAY_ELEMENT (it);
7964 }
7965 else
7966 {
7967 /* No face changes, overlays etc. in sight, so just return a
7968 character from current_buffer. */
7969 unsigned char *p;
7970 ptrdiff_t stop;
7971
7972 /* Maybe run the redisplay end trigger hook. Performance note:
7973 This doesn't seem to cost measurable time. */
7974 if (it->redisplay_end_trigger_charpos
7975 && it->glyph_row
7976 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7977 run_redisplay_end_trigger_hook (it);
7978
7979 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
7980 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
7981 stop)
7982 && next_element_from_composition (it))
7983 {
7984 return 1;
7985 }
7986
7987 /* Get the next character, maybe multibyte. */
7988 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
7989 if (it->multibyte_p && !ASCII_BYTE_P (*p))
7990 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
7991 else
7992 it->c = *p, it->len = 1;
7993
7994 /* Record what we have and where it came from. */
7995 it->what = IT_CHARACTER;
7996 it->object = it->w->buffer;
7997 it->position = it->current.pos;
7998
7999 /* Normally we return the character found above, except when we
8000 really want to return an ellipsis for selective display. */
8001 if (it->selective)
8002 {
8003 if (it->c == '\n')
8004 {
8005 /* A value of selective > 0 means hide lines indented more
8006 than that number of columns. */
8007 if (it->selective > 0
8008 && IT_CHARPOS (*it) + 1 < ZV
8009 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8010 IT_BYTEPOS (*it) + 1,
8011 it->selective))
8012 {
8013 success_p = next_element_from_ellipsis (it);
8014 it->dpvec_char_len = -1;
8015 }
8016 }
8017 else if (it->c == '\r' && it->selective == -1)
8018 {
8019 /* A value of selective == -1 means that everything from the
8020 CR to the end of the line is invisible, with maybe an
8021 ellipsis displayed for it. */
8022 success_p = next_element_from_ellipsis (it);
8023 it->dpvec_char_len = -1;
8024 }
8025 }
8026 }
8027
8028 /* Value is zero if end of buffer reached. */
8029 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8030 return success_p;
8031 }
8032
8033
8034 /* Run the redisplay end trigger hook for IT. */
8035
8036 static void
8037 run_redisplay_end_trigger_hook (struct it *it)
8038 {
8039 Lisp_Object args[3];
8040
8041 /* IT->glyph_row should be non-null, i.e. we should be actually
8042 displaying something, or otherwise we should not run the hook. */
8043 eassert (it->glyph_row);
8044
8045 /* Set up hook arguments. */
8046 args[0] = Qredisplay_end_trigger_functions;
8047 args[1] = it->window;
8048 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8049 it->redisplay_end_trigger_charpos = 0;
8050
8051 /* Since we are *trying* to run these functions, don't try to run
8052 them again, even if they get an error. */
8053 wset_redisplay_end_trigger (it->w, Qnil);
8054 Frun_hook_with_args (3, args);
8055
8056 /* Notice if it changed the face of the character we are on. */
8057 handle_face_prop (it);
8058 }
8059
8060
8061 /* Deliver a composition display element. Unlike the other
8062 next_element_from_XXX, this function is not registered in the array
8063 get_next_element[]. It is called from next_element_from_buffer and
8064 next_element_from_string when necessary. */
8065
8066 static int
8067 next_element_from_composition (struct it *it)
8068 {
8069 it->what = IT_COMPOSITION;
8070 it->len = it->cmp_it.nbytes;
8071 if (STRINGP (it->string))
8072 {
8073 if (it->c < 0)
8074 {
8075 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8076 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8077 return 0;
8078 }
8079 it->position = it->current.string_pos;
8080 it->object = it->string;
8081 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8082 IT_STRING_BYTEPOS (*it), it->string);
8083 }
8084 else
8085 {
8086 if (it->c < 0)
8087 {
8088 IT_CHARPOS (*it) += it->cmp_it.nchars;
8089 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8090 if (it->bidi_p)
8091 {
8092 if (it->bidi_it.new_paragraph)
8093 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8094 /* Resync the bidi iterator with IT's new position.
8095 FIXME: this doesn't support bidirectional text. */
8096 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8097 bidi_move_to_visually_next (&it->bidi_it);
8098 }
8099 return 0;
8100 }
8101 it->position = it->current.pos;
8102 it->object = it->w->buffer;
8103 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8104 IT_BYTEPOS (*it), Qnil);
8105 }
8106 return 1;
8107 }
8108
8109
8110 \f
8111 /***********************************************************************
8112 Moving an iterator without producing glyphs
8113 ***********************************************************************/
8114
8115 /* Check if iterator is at a position corresponding to a valid buffer
8116 position after some move_it_ call. */
8117
8118 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8119 ((it)->method == GET_FROM_STRING \
8120 ? IT_STRING_CHARPOS (*it) == 0 \
8121 : 1)
8122
8123
8124 /* Move iterator IT to a specified buffer or X position within one
8125 line on the display without producing glyphs.
8126
8127 OP should be a bit mask including some or all of these bits:
8128 MOVE_TO_X: Stop upon reaching x-position TO_X.
8129 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8130 Regardless of OP's value, stop upon reaching the end of the display line.
8131
8132 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8133 This means, in particular, that TO_X includes window's horizontal
8134 scroll amount.
8135
8136 The return value has several possible values that
8137 say what condition caused the scan to stop:
8138
8139 MOVE_POS_MATCH_OR_ZV
8140 - when TO_POS or ZV was reached.
8141
8142 MOVE_X_REACHED
8143 -when TO_X was reached before TO_POS or ZV were reached.
8144
8145 MOVE_LINE_CONTINUED
8146 - when we reached the end of the display area and the line must
8147 be continued.
8148
8149 MOVE_LINE_TRUNCATED
8150 - when we reached the end of the display area and the line is
8151 truncated.
8152
8153 MOVE_NEWLINE_OR_CR
8154 - when we stopped at a line end, i.e. a newline or a CR and selective
8155 display is on. */
8156
8157 static enum move_it_result
8158 move_it_in_display_line_to (struct it *it,
8159 ptrdiff_t to_charpos, int to_x,
8160 enum move_operation_enum op)
8161 {
8162 enum move_it_result result = MOVE_UNDEFINED;
8163 struct glyph_row *saved_glyph_row;
8164 struct it wrap_it, atpos_it, atx_it, ppos_it;
8165 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8166 void *ppos_data = NULL;
8167 int may_wrap = 0;
8168 enum it_method prev_method = it->method;
8169 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8170 int saw_smaller_pos = prev_pos < to_charpos;
8171
8172 /* Don't produce glyphs in produce_glyphs. */
8173 saved_glyph_row = it->glyph_row;
8174 it->glyph_row = NULL;
8175
8176 /* Use wrap_it to save a copy of IT wherever a word wrap could
8177 occur. Use atpos_it to save a copy of IT at the desired buffer
8178 position, if found, so that we can scan ahead and check if the
8179 word later overshoots the window edge. Use atx_it similarly, for
8180 pixel positions. */
8181 wrap_it.sp = -1;
8182 atpos_it.sp = -1;
8183 atx_it.sp = -1;
8184
8185 /* Use ppos_it under bidi reordering to save a copy of IT for the
8186 position > CHARPOS that is the closest to CHARPOS. We restore
8187 that position in IT when we have scanned the entire display line
8188 without finding a match for CHARPOS and all the character
8189 positions are greater than CHARPOS. */
8190 if (it->bidi_p)
8191 {
8192 SAVE_IT (ppos_it, *it, ppos_data);
8193 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8194 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8195 SAVE_IT (ppos_it, *it, ppos_data);
8196 }
8197
8198 #define BUFFER_POS_REACHED_P() \
8199 ((op & MOVE_TO_POS) != 0 \
8200 && BUFFERP (it->object) \
8201 && (IT_CHARPOS (*it) == to_charpos \
8202 || ((!it->bidi_p \
8203 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8204 && IT_CHARPOS (*it) > to_charpos) \
8205 || (it->what == IT_COMPOSITION \
8206 && ((IT_CHARPOS (*it) > to_charpos \
8207 && to_charpos >= it->cmp_it.charpos) \
8208 || (IT_CHARPOS (*it) < to_charpos \
8209 && to_charpos <= it->cmp_it.charpos)))) \
8210 && (it->method == GET_FROM_BUFFER \
8211 || (it->method == GET_FROM_DISPLAY_VECTOR \
8212 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8213
8214 /* If there's a line-/wrap-prefix, handle it. */
8215 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8216 && it->current_y < it->last_visible_y)
8217 handle_line_prefix (it);
8218
8219 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8220 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8221
8222 while (1)
8223 {
8224 int x, i, ascent = 0, descent = 0;
8225
8226 /* Utility macro to reset an iterator with x, ascent, and descent. */
8227 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8228 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8229 (IT)->max_descent = descent)
8230
8231 /* Stop if we move beyond TO_CHARPOS (after an image or a
8232 display string or stretch glyph). */
8233 if ((op & MOVE_TO_POS) != 0
8234 && BUFFERP (it->object)
8235 && it->method == GET_FROM_BUFFER
8236 && (((!it->bidi_p
8237 /* When the iterator is at base embedding level, we
8238 are guaranteed that characters are delivered for
8239 display in strictly increasing order of their
8240 buffer positions. */
8241 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8242 && IT_CHARPOS (*it) > to_charpos)
8243 || (it->bidi_p
8244 && (prev_method == GET_FROM_IMAGE
8245 || prev_method == GET_FROM_STRETCH
8246 || prev_method == GET_FROM_STRING)
8247 /* Passed TO_CHARPOS from left to right. */
8248 && ((prev_pos < to_charpos
8249 && IT_CHARPOS (*it) > to_charpos)
8250 /* Passed TO_CHARPOS from right to left. */
8251 || (prev_pos > to_charpos
8252 && IT_CHARPOS (*it) < to_charpos)))))
8253 {
8254 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8255 {
8256 result = MOVE_POS_MATCH_OR_ZV;
8257 break;
8258 }
8259 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8260 /* If wrap_it is valid, the current position might be in a
8261 word that is wrapped. So, save the iterator in
8262 atpos_it and continue to see if wrapping happens. */
8263 SAVE_IT (atpos_it, *it, atpos_data);
8264 }
8265
8266 /* Stop when ZV reached.
8267 We used to stop here when TO_CHARPOS reached as well, but that is
8268 too soon if this glyph does not fit on this line. So we handle it
8269 explicitly below. */
8270 if (!get_next_display_element (it))
8271 {
8272 result = MOVE_POS_MATCH_OR_ZV;
8273 break;
8274 }
8275
8276 if (it->line_wrap == TRUNCATE)
8277 {
8278 if (BUFFER_POS_REACHED_P ())
8279 {
8280 result = MOVE_POS_MATCH_OR_ZV;
8281 break;
8282 }
8283 }
8284 else
8285 {
8286 if (it->line_wrap == WORD_WRAP)
8287 {
8288 if (IT_DISPLAYING_WHITESPACE (it))
8289 may_wrap = 1;
8290 else if (may_wrap)
8291 {
8292 /* We have reached a glyph that follows one or more
8293 whitespace characters. If the position is
8294 already found, we are done. */
8295 if (atpos_it.sp >= 0)
8296 {
8297 RESTORE_IT (it, &atpos_it, atpos_data);
8298 result = MOVE_POS_MATCH_OR_ZV;
8299 goto done;
8300 }
8301 if (atx_it.sp >= 0)
8302 {
8303 RESTORE_IT (it, &atx_it, atx_data);
8304 result = MOVE_X_REACHED;
8305 goto done;
8306 }
8307 /* Otherwise, we can wrap here. */
8308 SAVE_IT (wrap_it, *it, wrap_data);
8309 may_wrap = 0;
8310 }
8311 }
8312 }
8313
8314 /* Remember the line height for the current line, in case
8315 the next element doesn't fit on the line. */
8316 ascent = it->max_ascent;
8317 descent = it->max_descent;
8318
8319 /* The call to produce_glyphs will get the metrics of the
8320 display element IT is loaded with. Record the x-position
8321 before this display element, in case it doesn't fit on the
8322 line. */
8323 x = it->current_x;
8324
8325 PRODUCE_GLYPHS (it);
8326
8327 if (it->area != TEXT_AREA)
8328 {
8329 prev_method = it->method;
8330 if (it->method == GET_FROM_BUFFER)
8331 prev_pos = IT_CHARPOS (*it);
8332 set_iterator_to_next (it, 1);
8333 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8334 SET_TEXT_POS (this_line_min_pos,
8335 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8336 if (it->bidi_p
8337 && (op & MOVE_TO_POS)
8338 && IT_CHARPOS (*it) > to_charpos
8339 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8340 SAVE_IT (ppos_it, *it, ppos_data);
8341 continue;
8342 }
8343
8344 /* The number of glyphs we get back in IT->nglyphs will normally
8345 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8346 character on a terminal frame, or (iii) a line end. For the
8347 second case, IT->nglyphs - 1 padding glyphs will be present.
8348 (On X frames, there is only one glyph produced for a
8349 composite character.)
8350
8351 The behavior implemented below means, for continuation lines,
8352 that as many spaces of a TAB as fit on the current line are
8353 displayed there. For terminal frames, as many glyphs of a
8354 multi-glyph character are displayed in the current line, too.
8355 This is what the old redisplay code did, and we keep it that
8356 way. Under X, the whole shape of a complex character must
8357 fit on the line or it will be completely displayed in the
8358 next line.
8359
8360 Note that both for tabs and padding glyphs, all glyphs have
8361 the same width. */
8362 if (it->nglyphs)
8363 {
8364 /* More than one glyph or glyph doesn't fit on line. All
8365 glyphs have the same width. */
8366 int single_glyph_width = it->pixel_width / it->nglyphs;
8367 int new_x;
8368 int x_before_this_char = x;
8369 int hpos_before_this_char = it->hpos;
8370
8371 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8372 {
8373 new_x = x + single_glyph_width;
8374
8375 /* We want to leave anything reaching TO_X to the caller. */
8376 if ((op & MOVE_TO_X) && new_x > to_x)
8377 {
8378 if (BUFFER_POS_REACHED_P ())
8379 {
8380 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8381 goto buffer_pos_reached;
8382 if (atpos_it.sp < 0)
8383 {
8384 SAVE_IT (atpos_it, *it, atpos_data);
8385 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8386 }
8387 }
8388 else
8389 {
8390 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8391 {
8392 it->current_x = x;
8393 result = MOVE_X_REACHED;
8394 break;
8395 }
8396 if (atx_it.sp < 0)
8397 {
8398 SAVE_IT (atx_it, *it, atx_data);
8399 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8400 }
8401 }
8402 }
8403
8404 if (/* Lines are continued. */
8405 it->line_wrap != TRUNCATE
8406 && (/* And glyph doesn't fit on the line. */
8407 new_x > it->last_visible_x
8408 /* Or it fits exactly and we're on a window
8409 system frame. */
8410 || (new_x == it->last_visible_x
8411 && FRAME_WINDOW_P (it->f)
8412 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8413 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8414 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8415 {
8416 if (/* IT->hpos == 0 means the very first glyph
8417 doesn't fit on the line, e.g. a wide image. */
8418 it->hpos == 0
8419 || (new_x == it->last_visible_x
8420 && FRAME_WINDOW_P (it->f)))
8421 {
8422 ++it->hpos;
8423 it->current_x = new_x;
8424
8425 /* The character's last glyph just barely fits
8426 in this row. */
8427 if (i == it->nglyphs - 1)
8428 {
8429 /* If this is the destination position,
8430 return a position *before* it in this row,
8431 now that we know it fits in this row. */
8432 if (BUFFER_POS_REACHED_P ())
8433 {
8434 if (it->line_wrap != WORD_WRAP
8435 || wrap_it.sp < 0)
8436 {
8437 it->hpos = hpos_before_this_char;
8438 it->current_x = x_before_this_char;
8439 result = MOVE_POS_MATCH_OR_ZV;
8440 break;
8441 }
8442 if (it->line_wrap == WORD_WRAP
8443 && atpos_it.sp < 0)
8444 {
8445 SAVE_IT (atpos_it, *it, atpos_data);
8446 atpos_it.current_x = x_before_this_char;
8447 atpos_it.hpos = hpos_before_this_char;
8448 }
8449 }
8450
8451 prev_method = it->method;
8452 if (it->method == GET_FROM_BUFFER)
8453 prev_pos = IT_CHARPOS (*it);
8454 set_iterator_to_next (it, 1);
8455 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8456 SET_TEXT_POS (this_line_min_pos,
8457 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8458 /* On graphical terminals, newlines may
8459 "overflow" into the fringe if
8460 overflow-newline-into-fringe is non-nil.
8461 On text terminals, and on graphical
8462 terminals with no right margin, newlines
8463 may overflow into the last glyph on the
8464 display line.*/
8465 if (!FRAME_WINDOW_P (it->f)
8466 || ((it->bidi_p
8467 && it->bidi_it.paragraph_dir == R2L)
8468 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8469 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8470 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8471 {
8472 if (!get_next_display_element (it))
8473 {
8474 result = MOVE_POS_MATCH_OR_ZV;
8475 break;
8476 }
8477 if (BUFFER_POS_REACHED_P ())
8478 {
8479 if (ITERATOR_AT_END_OF_LINE_P (it))
8480 result = MOVE_POS_MATCH_OR_ZV;
8481 else
8482 result = MOVE_LINE_CONTINUED;
8483 break;
8484 }
8485 if (ITERATOR_AT_END_OF_LINE_P (it))
8486 {
8487 result = MOVE_NEWLINE_OR_CR;
8488 break;
8489 }
8490 }
8491 }
8492 }
8493 else
8494 IT_RESET_X_ASCENT_DESCENT (it);
8495
8496 if (wrap_it.sp >= 0)
8497 {
8498 RESTORE_IT (it, &wrap_it, wrap_data);
8499 atpos_it.sp = -1;
8500 atx_it.sp = -1;
8501 }
8502
8503 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8504 IT_CHARPOS (*it)));
8505 result = MOVE_LINE_CONTINUED;
8506 break;
8507 }
8508
8509 if (BUFFER_POS_REACHED_P ())
8510 {
8511 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8512 goto buffer_pos_reached;
8513 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8514 {
8515 SAVE_IT (atpos_it, *it, atpos_data);
8516 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8517 }
8518 }
8519
8520 if (new_x > it->first_visible_x)
8521 {
8522 /* Glyph is visible. Increment number of glyphs that
8523 would be displayed. */
8524 ++it->hpos;
8525 }
8526 }
8527
8528 if (result != MOVE_UNDEFINED)
8529 break;
8530 }
8531 else if (BUFFER_POS_REACHED_P ())
8532 {
8533 buffer_pos_reached:
8534 IT_RESET_X_ASCENT_DESCENT (it);
8535 result = MOVE_POS_MATCH_OR_ZV;
8536 break;
8537 }
8538 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8539 {
8540 /* Stop when TO_X specified and reached. This check is
8541 necessary here because of lines consisting of a line end,
8542 only. The line end will not produce any glyphs and we
8543 would never get MOVE_X_REACHED. */
8544 eassert (it->nglyphs == 0);
8545 result = MOVE_X_REACHED;
8546 break;
8547 }
8548
8549 /* Is this a line end? If yes, we're done. */
8550 if (ITERATOR_AT_END_OF_LINE_P (it))
8551 {
8552 /* If we are past TO_CHARPOS, but never saw any character
8553 positions smaller than TO_CHARPOS, return
8554 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8555 did. */
8556 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8557 {
8558 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8559 {
8560 if (IT_CHARPOS (ppos_it) < ZV)
8561 {
8562 RESTORE_IT (it, &ppos_it, ppos_data);
8563 result = MOVE_POS_MATCH_OR_ZV;
8564 }
8565 else
8566 goto buffer_pos_reached;
8567 }
8568 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8569 && IT_CHARPOS (*it) > to_charpos)
8570 goto buffer_pos_reached;
8571 else
8572 result = MOVE_NEWLINE_OR_CR;
8573 }
8574 else
8575 result = MOVE_NEWLINE_OR_CR;
8576 break;
8577 }
8578
8579 prev_method = it->method;
8580 if (it->method == GET_FROM_BUFFER)
8581 prev_pos = IT_CHARPOS (*it);
8582 /* The current display element has been consumed. Advance
8583 to the next. */
8584 set_iterator_to_next (it, 1);
8585 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8586 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8587 if (IT_CHARPOS (*it) < to_charpos)
8588 saw_smaller_pos = 1;
8589 if (it->bidi_p
8590 && (op & MOVE_TO_POS)
8591 && IT_CHARPOS (*it) >= to_charpos
8592 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8593 SAVE_IT (ppos_it, *it, ppos_data);
8594
8595 /* Stop if lines are truncated and IT's current x-position is
8596 past the right edge of the window now. */
8597 if (it->line_wrap == TRUNCATE
8598 && it->current_x >= it->last_visible_x)
8599 {
8600 if (!FRAME_WINDOW_P (it->f)
8601 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8602 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8603 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8604 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8605 {
8606 int at_eob_p = 0;
8607
8608 if ((at_eob_p = !get_next_display_element (it))
8609 || BUFFER_POS_REACHED_P ()
8610 /* If we are past TO_CHARPOS, but never saw any
8611 character positions smaller than TO_CHARPOS,
8612 return MOVE_POS_MATCH_OR_ZV, like the
8613 unidirectional display did. */
8614 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8615 && !saw_smaller_pos
8616 && IT_CHARPOS (*it) > to_charpos))
8617 {
8618 if (it->bidi_p
8619 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8620 RESTORE_IT (it, &ppos_it, ppos_data);
8621 result = MOVE_POS_MATCH_OR_ZV;
8622 break;
8623 }
8624 if (ITERATOR_AT_END_OF_LINE_P (it))
8625 {
8626 result = MOVE_NEWLINE_OR_CR;
8627 break;
8628 }
8629 }
8630 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8631 && !saw_smaller_pos
8632 && IT_CHARPOS (*it) > to_charpos)
8633 {
8634 if (IT_CHARPOS (ppos_it) < ZV)
8635 RESTORE_IT (it, &ppos_it, ppos_data);
8636 result = MOVE_POS_MATCH_OR_ZV;
8637 break;
8638 }
8639 result = MOVE_LINE_TRUNCATED;
8640 break;
8641 }
8642 #undef IT_RESET_X_ASCENT_DESCENT
8643 }
8644
8645 #undef BUFFER_POS_REACHED_P
8646
8647 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8648 restore the saved iterator. */
8649 if (atpos_it.sp >= 0)
8650 RESTORE_IT (it, &atpos_it, atpos_data);
8651 else if (atx_it.sp >= 0)
8652 RESTORE_IT (it, &atx_it, atx_data);
8653
8654 done:
8655
8656 if (atpos_data)
8657 bidi_unshelve_cache (atpos_data, 1);
8658 if (atx_data)
8659 bidi_unshelve_cache (atx_data, 1);
8660 if (wrap_data)
8661 bidi_unshelve_cache (wrap_data, 1);
8662 if (ppos_data)
8663 bidi_unshelve_cache (ppos_data, 1);
8664
8665 /* Restore the iterator settings altered at the beginning of this
8666 function. */
8667 it->glyph_row = saved_glyph_row;
8668 return result;
8669 }
8670
8671 /* For external use. */
8672 void
8673 move_it_in_display_line (struct it *it,
8674 ptrdiff_t to_charpos, int to_x,
8675 enum move_operation_enum op)
8676 {
8677 if (it->line_wrap == WORD_WRAP
8678 && (op & MOVE_TO_X))
8679 {
8680 struct it save_it;
8681 void *save_data = NULL;
8682 int skip;
8683
8684 SAVE_IT (save_it, *it, save_data);
8685 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8686 /* When word-wrap is on, TO_X may lie past the end
8687 of a wrapped line. Then it->current is the
8688 character on the next line, so backtrack to the
8689 space before the wrap point. */
8690 if (skip == MOVE_LINE_CONTINUED)
8691 {
8692 int prev_x = max (it->current_x - 1, 0);
8693 RESTORE_IT (it, &save_it, save_data);
8694 move_it_in_display_line_to
8695 (it, -1, prev_x, MOVE_TO_X);
8696 }
8697 else
8698 bidi_unshelve_cache (save_data, 1);
8699 }
8700 else
8701 move_it_in_display_line_to (it, to_charpos, to_x, op);
8702 }
8703
8704
8705 /* Move IT forward until it satisfies one or more of the criteria in
8706 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8707
8708 OP is a bit-mask that specifies where to stop, and in particular,
8709 which of those four position arguments makes a difference. See the
8710 description of enum move_operation_enum.
8711
8712 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8713 screen line, this function will set IT to the next position that is
8714 displayed to the right of TO_CHARPOS on the screen. */
8715
8716 void
8717 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8718 {
8719 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8720 int line_height, line_start_x = 0, reached = 0;
8721 void *backup_data = NULL;
8722
8723 for (;;)
8724 {
8725 if (op & MOVE_TO_VPOS)
8726 {
8727 /* If no TO_CHARPOS and no TO_X specified, stop at the
8728 start of the line TO_VPOS. */
8729 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8730 {
8731 if (it->vpos == to_vpos)
8732 {
8733 reached = 1;
8734 break;
8735 }
8736 else
8737 skip = move_it_in_display_line_to (it, -1, -1, 0);
8738 }
8739 else
8740 {
8741 /* TO_VPOS >= 0 means stop at TO_X in the line at
8742 TO_VPOS, or at TO_POS, whichever comes first. */
8743 if (it->vpos == to_vpos)
8744 {
8745 reached = 2;
8746 break;
8747 }
8748
8749 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8750
8751 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8752 {
8753 reached = 3;
8754 break;
8755 }
8756 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8757 {
8758 /* We have reached TO_X but not in the line we want. */
8759 skip = move_it_in_display_line_to (it, to_charpos,
8760 -1, MOVE_TO_POS);
8761 if (skip == MOVE_POS_MATCH_OR_ZV)
8762 {
8763 reached = 4;
8764 break;
8765 }
8766 }
8767 }
8768 }
8769 else if (op & MOVE_TO_Y)
8770 {
8771 struct it it_backup;
8772
8773 if (it->line_wrap == WORD_WRAP)
8774 SAVE_IT (it_backup, *it, backup_data);
8775
8776 /* TO_Y specified means stop at TO_X in the line containing
8777 TO_Y---or at TO_CHARPOS if this is reached first. The
8778 problem is that we can't really tell whether the line
8779 contains TO_Y before we have completely scanned it, and
8780 this may skip past TO_X. What we do is to first scan to
8781 TO_X.
8782
8783 If TO_X is not specified, use a TO_X of zero. The reason
8784 is to make the outcome of this function more predictable.
8785 If we didn't use TO_X == 0, we would stop at the end of
8786 the line which is probably not what a caller would expect
8787 to happen. */
8788 skip = move_it_in_display_line_to
8789 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8790 (MOVE_TO_X | (op & MOVE_TO_POS)));
8791
8792 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8793 if (skip == MOVE_POS_MATCH_OR_ZV)
8794 reached = 5;
8795 else if (skip == MOVE_X_REACHED)
8796 {
8797 /* If TO_X was reached, we want to know whether TO_Y is
8798 in the line. We know this is the case if the already
8799 scanned glyphs make the line tall enough. Otherwise,
8800 we must check by scanning the rest of the line. */
8801 line_height = it->max_ascent + it->max_descent;
8802 if (to_y >= it->current_y
8803 && to_y < it->current_y + line_height)
8804 {
8805 reached = 6;
8806 break;
8807 }
8808 SAVE_IT (it_backup, *it, backup_data);
8809 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8810 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8811 op & MOVE_TO_POS);
8812 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8813 line_height = it->max_ascent + it->max_descent;
8814 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8815
8816 if (to_y >= it->current_y
8817 && to_y < it->current_y + line_height)
8818 {
8819 /* If TO_Y is in this line and TO_X was reached
8820 above, we scanned too far. We have to restore
8821 IT's settings to the ones before skipping. But
8822 keep the more accurate values of max_ascent and
8823 max_descent we've found while skipping the rest
8824 of the line, for the sake of callers, such as
8825 pos_visible_p, that need to know the line
8826 height. */
8827 int max_ascent = it->max_ascent;
8828 int max_descent = it->max_descent;
8829
8830 RESTORE_IT (it, &it_backup, backup_data);
8831 it->max_ascent = max_ascent;
8832 it->max_descent = max_descent;
8833 reached = 6;
8834 }
8835 else
8836 {
8837 skip = skip2;
8838 if (skip == MOVE_POS_MATCH_OR_ZV)
8839 reached = 7;
8840 }
8841 }
8842 else
8843 {
8844 /* Check whether TO_Y is in this line. */
8845 line_height = it->max_ascent + it->max_descent;
8846 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8847
8848 if (to_y >= it->current_y
8849 && to_y < it->current_y + line_height)
8850 {
8851 /* When word-wrap is on, TO_X may lie past the end
8852 of a wrapped line. Then it->current is the
8853 character on the next line, so backtrack to the
8854 space before the wrap point. */
8855 if (skip == MOVE_LINE_CONTINUED
8856 && it->line_wrap == WORD_WRAP)
8857 {
8858 int prev_x = max (it->current_x - 1, 0);
8859 RESTORE_IT (it, &it_backup, backup_data);
8860 skip = move_it_in_display_line_to
8861 (it, -1, prev_x, MOVE_TO_X);
8862 }
8863 reached = 6;
8864 }
8865 }
8866
8867 if (reached)
8868 break;
8869 }
8870 else if (BUFFERP (it->object)
8871 && (it->method == GET_FROM_BUFFER
8872 || it->method == GET_FROM_STRETCH)
8873 && IT_CHARPOS (*it) >= to_charpos
8874 /* Under bidi iteration, a call to set_iterator_to_next
8875 can scan far beyond to_charpos if the initial
8876 portion of the next line needs to be reordered. In
8877 that case, give move_it_in_display_line_to another
8878 chance below. */
8879 && !(it->bidi_p
8880 && it->bidi_it.scan_dir == -1))
8881 skip = MOVE_POS_MATCH_OR_ZV;
8882 else
8883 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8884
8885 switch (skip)
8886 {
8887 case MOVE_POS_MATCH_OR_ZV:
8888 reached = 8;
8889 goto out;
8890
8891 case MOVE_NEWLINE_OR_CR:
8892 set_iterator_to_next (it, 1);
8893 it->continuation_lines_width = 0;
8894 break;
8895
8896 case MOVE_LINE_TRUNCATED:
8897 it->continuation_lines_width = 0;
8898 reseat_at_next_visible_line_start (it, 0);
8899 if ((op & MOVE_TO_POS) != 0
8900 && IT_CHARPOS (*it) > to_charpos)
8901 {
8902 reached = 9;
8903 goto out;
8904 }
8905 break;
8906
8907 case MOVE_LINE_CONTINUED:
8908 /* For continued lines ending in a tab, some of the glyphs
8909 associated with the tab are displayed on the current
8910 line. Since it->current_x does not include these glyphs,
8911 we use it->last_visible_x instead. */
8912 if (it->c == '\t')
8913 {
8914 it->continuation_lines_width += it->last_visible_x;
8915 /* When moving by vpos, ensure that the iterator really
8916 advances to the next line (bug#847, bug#969). Fixme:
8917 do we need to do this in other circumstances? */
8918 if (it->current_x != it->last_visible_x
8919 && (op & MOVE_TO_VPOS)
8920 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8921 {
8922 line_start_x = it->current_x + it->pixel_width
8923 - it->last_visible_x;
8924 set_iterator_to_next (it, 0);
8925 }
8926 }
8927 else
8928 it->continuation_lines_width += it->current_x;
8929 break;
8930
8931 default:
8932 emacs_abort ();
8933 }
8934
8935 /* Reset/increment for the next run. */
8936 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8937 it->current_x = line_start_x;
8938 line_start_x = 0;
8939 it->hpos = 0;
8940 it->current_y += it->max_ascent + it->max_descent;
8941 ++it->vpos;
8942 last_height = it->max_ascent + it->max_descent;
8943 last_max_ascent = it->max_ascent;
8944 it->max_ascent = it->max_descent = 0;
8945 }
8946
8947 out:
8948
8949 /* On text terminals, we may stop at the end of a line in the middle
8950 of a multi-character glyph. If the glyph itself is continued,
8951 i.e. it is actually displayed on the next line, don't treat this
8952 stopping point as valid; move to the next line instead (unless
8953 that brings us offscreen). */
8954 if (!FRAME_WINDOW_P (it->f)
8955 && op & MOVE_TO_POS
8956 && IT_CHARPOS (*it) == to_charpos
8957 && it->what == IT_CHARACTER
8958 && it->nglyphs > 1
8959 && it->line_wrap == WINDOW_WRAP
8960 && it->current_x == it->last_visible_x - 1
8961 && it->c != '\n'
8962 && it->c != '\t'
8963 && it->vpos < XFASTINT (it->w->window_end_vpos))
8964 {
8965 it->continuation_lines_width += it->current_x;
8966 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8967 it->current_y += it->max_ascent + it->max_descent;
8968 ++it->vpos;
8969 last_height = it->max_ascent + it->max_descent;
8970 last_max_ascent = it->max_ascent;
8971 }
8972
8973 if (backup_data)
8974 bidi_unshelve_cache (backup_data, 1);
8975
8976 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8977 }
8978
8979
8980 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
8981
8982 If DY > 0, move IT backward at least that many pixels. DY = 0
8983 means move IT backward to the preceding line start or BEGV. This
8984 function may move over more than DY pixels if IT->current_y - DY
8985 ends up in the middle of a line; in this case IT->current_y will be
8986 set to the top of the line moved to. */
8987
8988 void
8989 move_it_vertically_backward (struct it *it, int dy)
8990 {
8991 int nlines, h;
8992 struct it it2, it3;
8993 void *it2data = NULL, *it3data = NULL;
8994 ptrdiff_t start_pos;
8995 int nchars_per_row
8996 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
8997 ptrdiff_t pos_limit;
8998
8999 move_further_back:
9000 eassert (dy >= 0);
9001
9002 start_pos = IT_CHARPOS (*it);
9003
9004 /* Estimate how many newlines we must move back. */
9005 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
9006 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9007
9008 /* Set the iterator's position that many lines back. But don't go
9009 back more than NLINES full screen lines -- this wins a day with
9010 buffers which have very long lines. */
9011 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9012 back_to_previous_visible_line_start (it);
9013
9014 /* Reseat the iterator here. When moving backward, we don't want
9015 reseat to skip forward over invisible text, set up the iterator
9016 to deliver from overlay strings at the new position etc. So,
9017 use reseat_1 here. */
9018 reseat_1 (it, it->current.pos, 1);
9019
9020 /* We are now surely at a line start. */
9021 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9022 reordering is in effect. */
9023 it->continuation_lines_width = 0;
9024
9025 /* Move forward and see what y-distance we moved. First move to the
9026 start of the next line so that we get its height. We need this
9027 height to be able to tell whether we reached the specified
9028 y-distance. */
9029 SAVE_IT (it2, *it, it2data);
9030 it2.max_ascent = it2.max_descent = 0;
9031 do
9032 {
9033 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9034 MOVE_TO_POS | MOVE_TO_VPOS);
9035 }
9036 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9037 /* If we are in a display string which starts at START_POS,
9038 and that display string includes a newline, and we are
9039 right after that newline (i.e. at the beginning of a
9040 display line), exit the loop, because otherwise we will
9041 infloop, since move_it_to will see that it is already at
9042 START_POS and will not move. */
9043 || (it2.method == GET_FROM_STRING
9044 && IT_CHARPOS (it2) == start_pos
9045 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9046 eassert (IT_CHARPOS (*it) >= BEGV);
9047 SAVE_IT (it3, it2, it3data);
9048
9049 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9050 eassert (IT_CHARPOS (*it) >= BEGV);
9051 /* H is the actual vertical distance from the position in *IT
9052 and the starting position. */
9053 h = it2.current_y - it->current_y;
9054 /* NLINES is the distance in number of lines. */
9055 nlines = it2.vpos - it->vpos;
9056
9057 /* Correct IT's y and vpos position
9058 so that they are relative to the starting point. */
9059 it->vpos -= nlines;
9060 it->current_y -= h;
9061
9062 if (dy == 0)
9063 {
9064 /* DY == 0 means move to the start of the screen line. The
9065 value of nlines is > 0 if continuation lines were involved,
9066 or if the original IT position was at start of a line. */
9067 RESTORE_IT (it, it, it2data);
9068 if (nlines > 0)
9069 move_it_by_lines (it, nlines);
9070 /* The above code moves us to some position NLINES down,
9071 usually to its first glyph (leftmost in an L2R line), but
9072 that's not necessarily the start of the line, under bidi
9073 reordering. We want to get to the character position
9074 that is immediately after the newline of the previous
9075 line. */
9076 if (it->bidi_p
9077 && !it->continuation_lines_width
9078 && !STRINGP (it->string)
9079 && IT_CHARPOS (*it) > BEGV
9080 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9081 {
9082 ptrdiff_t nl_pos =
9083 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
9084
9085 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
9086 }
9087 bidi_unshelve_cache (it3data, 1);
9088 }
9089 else
9090 {
9091 /* The y-position we try to reach, relative to *IT.
9092 Note that H has been subtracted in front of the if-statement. */
9093 int target_y = it->current_y + h - dy;
9094 int y0 = it3.current_y;
9095 int y1;
9096 int line_height;
9097
9098 RESTORE_IT (&it3, &it3, it3data);
9099 y1 = line_bottom_y (&it3);
9100 line_height = y1 - y0;
9101 RESTORE_IT (it, it, it2data);
9102 /* If we did not reach target_y, try to move further backward if
9103 we can. If we moved too far backward, try to move forward. */
9104 if (target_y < it->current_y
9105 /* This is heuristic. In a window that's 3 lines high, with
9106 a line height of 13 pixels each, recentering with point
9107 on the bottom line will try to move -39/2 = 19 pixels
9108 backward. Try to avoid moving into the first line. */
9109 && (it->current_y - target_y
9110 > min (window_box_height (it->w), line_height * 2 / 3))
9111 && IT_CHARPOS (*it) > BEGV)
9112 {
9113 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9114 target_y - it->current_y));
9115 dy = it->current_y - target_y;
9116 goto move_further_back;
9117 }
9118 else if (target_y >= it->current_y + line_height
9119 && IT_CHARPOS (*it) < ZV)
9120 {
9121 /* Should move forward by at least one line, maybe more.
9122
9123 Note: Calling move_it_by_lines can be expensive on
9124 terminal frames, where compute_motion is used (via
9125 vmotion) to do the job, when there are very long lines
9126 and truncate-lines is nil. That's the reason for
9127 treating terminal frames specially here. */
9128
9129 if (!FRAME_WINDOW_P (it->f))
9130 move_it_vertically (it, target_y - (it->current_y + line_height));
9131 else
9132 {
9133 do
9134 {
9135 move_it_by_lines (it, 1);
9136 }
9137 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9138 }
9139 }
9140 }
9141 }
9142
9143
9144 /* Move IT by a specified amount of pixel lines DY. DY negative means
9145 move backwards. DY = 0 means move to start of screen line. At the
9146 end, IT will be on the start of a screen line. */
9147
9148 void
9149 move_it_vertically (struct it *it, int dy)
9150 {
9151 if (dy <= 0)
9152 move_it_vertically_backward (it, -dy);
9153 else
9154 {
9155 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9156 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9157 MOVE_TO_POS | MOVE_TO_Y);
9158 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9159
9160 /* If buffer ends in ZV without a newline, move to the start of
9161 the line to satisfy the post-condition. */
9162 if (IT_CHARPOS (*it) == ZV
9163 && ZV > BEGV
9164 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9165 move_it_by_lines (it, 0);
9166 }
9167 }
9168
9169
9170 /* Move iterator IT past the end of the text line it is in. */
9171
9172 void
9173 move_it_past_eol (struct it *it)
9174 {
9175 enum move_it_result rc;
9176
9177 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9178 if (rc == MOVE_NEWLINE_OR_CR)
9179 set_iterator_to_next (it, 0);
9180 }
9181
9182
9183 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9184 negative means move up. DVPOS == 0 means move to the start of the
9185 screen line.
9186
9187 Optimization idea: If we would know that IT->f doesn't use
9188 a face with proportional font, we could be faster for
9189 truncate-lines nil. */
9190
9191 void
9192 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9193 {
9194
9195 /* The commented-out optimization uses vmotion on terminals. This
9196 gives bad results, because elements like it->what, on which
9197 callers such as pos_visible_p rely, aren't updated. */
9198 /* struct position pos;
9199 if (!FRAME_WINDOW_P (it->f))
9200 {
9201 struct text_pos textpos;
9202
9203 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9204 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9205 reseat (it, textpos, 1);
9206 it->vpos += pos.vpos;
9207 it->current_y += pos.vpos;
9208 }
9209 else */
9210
9211 if (dvpos == 0)
9212 {
9213 /* DVPOS == 0 means move to the start of the screen line. */
9214 move_it_vertically_backward (it, 0);
9215 /* Let next call to line_bottom_y calculate real line height */
9216 last_height = 0;
9217 }
9218 else if (dvpos > 0)
9219 {
9220 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9221 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9222 {
9223 /* Only move to the next buffer position if we ended up in a
9224 string from display property, not in an overlay string
9225 (before-string or after-string). That is because the
9226 latter don't conceal the underlying buffer position, so
9227 we can ask to move the iterator to the exact position we
9228 are interested in. Note that, even if we are already at
9229 IT_CHARPOS (*it), the call below is not a no-op, as it
9230 will detect that we are at the end of the string, pop the
9231 iterator, and compute it->current_x and it->hpos
9232 correctly. */
9233 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9234 -1, -1, -1, MOVE_TO_POS);
9235 }
9236 }
9237 else
9238 {
9239 struct it it2;
9240 void *it2data = NULL;
9241 ptrdiff_t start_charpos, i;
9242 int nchars_per_row
9243 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9244 ptrdiff_t pos_limit;
9245
9246 /* Start at the beginning of the screen line containing IT's
9247 position. This may actually move vertically backwards,
9248 in case of overlays, so adjust dvpos accordingly. */
9249 dvpos += it->vpos;
9250 move_it_vertically_backward (it, 0);
9251 dvpos -= it->vpos;
9252
9253 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9254 screen lines, and reseat the iterator there. */
9255 start_charpos = IT_CHARPOS (*it);
9256 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9257 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9258 back_to_previous_visible_line_start (it);
9259 reseat (it, it->current.pos, 1);
9260
9261 /* Move further back if we end up in a string or an image. */
9262 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9263 {
9264 /* First try to move to start of display line. */
9265 dvpos += it->vpos;
9266 move_it_vertically_backward (it, 0);
9267 dvpos -= it->vpos;
9268 if (IT_POS_VALID_AFTER_MOVE_P (it))
9269 break;
9270 /* If start of line is still in string or image,
9271 move further back. */
9272 back_to_previous_visible_line_start (it);
9273 reseat (it, it->current.pos, 1);
9274 dvpos--;
9275 }
9276
9277 it->current_x = it->hpos = 0;
9278
9279 /* Above call may have moved too far if continuation lines
9280 are involved. Scan forward and see if it did. */
9281 SAVE_IT (it2, *it, it2data);
9282 it2.vpos = it2.current_y = 0;
9283 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9284 it->vpos -= it2.vpos;
9285 it->current_y -= it2.current_y;
9286 it->current_x = it->hpos = 0;
9287
9288 /* If we moved too far back, move IT some lines forward. */
9289 if (it2.vpos > -dvpos)
9290 {
9291 int delta = it2.vpos + dvpos;
9292
9293 RESTORE_IT (&it2, &it2, it2data);
9294 SAVE_IT (it2, *it, it2data);
9295 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9296 /* Move back again if we got too far ahead. */
9297 if (IT_CHARPOS (*it) >= start_charpos)
9298 RESTORE_IT (it, &it2, it2data);
9299 else
9300 bidi_unshelve_cache (it2data, 1);
9301 }
9302 else
9303 RESTORE_IT (it, it, it2data);
9304 }
9305 }
9306
9307 /* Return 1 if IT points into the middle of a display vector. */
9308
9309 int
9310 in_display_vector_p (struct it *it)
9311 {
9312 return (it->method == GET_FROM_DISPLAY_VECTOR
9313 && it->current.dpvec_index > 0
9314 && it->dpvec + it->current.dpvec_index != it->dpend);
9315 }
9316
9317 \f
9318 /***********************************************************************
9319 Messages
9320 ***********************************************************************/
9321
9322
9323 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9324 to *Messages*. */
9325
9326 void
9327 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9328 {
9329 Lisp_Object args[3];
9330 Lisp_Object msg, fmt;
9331 char *buffer;
9332 ptrdiff_t len;
9333 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9334 USE_SAFE_ALLOCA;
9335
9336 fmt = msg = Qnil;
9337 GCPRO4 (fmt, msg, arg1, arg2);
9338
9339 args[0] = fmt = build_string (format);
9340 args[1] = arg1;
9341 args[2] = arg2;
9342 msg = Fformat (3, args);
9343
9344 len = SBYTES (msg) + 1;
9345 buffer = SAFE_ALLOCA (len);
9346 memcpy (buffer, SDATA (msg), len);
9347
9348 message_dolog (buffer, len - 1, 1, 0);
9349 SAFE_FREE ();
9350
9351 UNGCPRO;
9352 }
9353
9354
9355 /* Output a newline in the *Messages* buffer if "needs" one. */
9356
9357 void
9358 message_log_maybe_newline (void)
9359 {
9360 if (message_log_need_newline)
9361 message_dolog ("", 0, 1, 0);
9362 }
9363
9364
9365 /* Add a string M of length NBYTES to the message log, optionally
9366 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9367 nonzero, means interpret the contents of M as multibyte. This
9368 function calls low-level routines in order to bypass text property
9369 hooks, etc. which might not be safe to run.
9370
9371 This may GC (insert may run before/after change hooks),
9372 so the buffer M must NOT point to a Lisp string. */
9373
9374 void
9375 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9376 {
9377 const unsigned char *msg = (const unsigned char *) m;
9378
9379 if (!NILP (Vmemory_full))
9380 return;
9381
9382 if (!NILP (Vmessage_log_max))
9383 {
9384 struct buffer *oldbuf;
9385 Lisp_Object oldpoint, oldbegv, oldzv;
9386 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9387 ptrdiff_t point_at_end = 0;
9388 ptrdiff_t zv_at_end = 0;
9389 Lisp_Object old_deactivate_mark;
9390 bool shown;
9391 struct gcpro gcpro1;
9392
9393 old_deactivate_mark = Vdeactivate_mark;
9394 oldbuf = current_buffer;
9395 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9396 bset_undo_list (current_buffer, Qt);
9397
9398 oldpoint = message_dolog_marker1;
9399 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9400 oldbegv = message_dolog_marker2;
9401 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9402 oldzv = message_dolog_marker3;
9403 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9404 GCPRO1 (old_deactivate_mark);
9405
9406 if (PT == Z)
9407 point_at_end = 1;
9408 if (ZV == Z)
9409 zv_at_end = 1;
9410
9411 BEGV = BEG;
9412 BEGV_BYTE = BEG_BYTE;
9413 ZV = Z;
9414 ZV_BYTE = Z_BYTE;
9415 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9416
9417 /* Insert the string--maybe converting multibyte to single byte
9418 or vice versa, so that all the text fits the buffer. */
9419 if (multibyte
9420 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9421 {
9422 ptrdiff_t i;
9423 int c, char_bytes;
9424 char work[1];
9425
9426 /* Convert a multibyte string to single-byte
9427 for the *Message* buffer. */
9428 for (i = 0; i < nbytes; i += char_bytes)
9429 {
9430 c = string_char_and_length (msg + i, &char_bytes);
9431 work[0] = (ASCII_CHAR_P (c)
9432 ? c
9433 : multibyte_char_to_unibyte (c));
9434 insert_1_both (work, 1, 1, 1, 0, 0);
9435 }
9436 }
9437 else if (! multibyte
9438 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9439 {
9440 ptrdiff_t i;
9441 int c, char_bytes;
9442 unsigned char str[MAX_MULTIBYTE_LENGTH];
9443 /* Convert a single-byte string to multibyte
9444 for the *Message* buffer. */
9445 for (i = 0; i < nbytes; i++)
9446 {
9447 c = msg[i];
9448 MAKE_CHAR_MULTIBYTE (c);
9449 char_bytes = CHAR_STRING (c, str);
9450 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9451 }
9452 }
9453 else if (nbytes)
9454 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9455
9456 if (nlflag)
9457 {
9458 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9459 printmax_t dups;
9460
9461 insert_1_both ("\n", 1, 1, 1, 0, 0);
9462
9463 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9464 this_bol = PT;
9465 this_bol_byte = PT_BYTE;
9466
9467 /* See if this line duplicates the previous one.
9468 If so, combine duplicates. */
9469 if (this_bol > BEG)
9470 {
9471 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9472 prev_bol = PT;
9473 prev_bol_byte = PT_BYTE;
9474
9475 dups = message_log_check_duplicate (prev_bol_byte,
9476 this_bol_byte);
9477 if (dups)
9478 {
9479 del_range_both (prev_bol, prev_bol_byte,
9480 this_bol, this_bol_byte, 0);
9481 if (dups > 1)
9482 {
9483 char dupstr[sizeof " [ times]"
9484 + INT_STRLEN_BOUND (printmax_t)];
9485
9486 /* If you change this format, don't forget to also
9487 change message_log_check_duplicate. */
9488 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9489 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9490 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9491 }
9492 }
9493 }
9494
9495 /* If we have more than the desired maximum number of lines
9496 in the *Messages* buffer now, delete the oldest ones.
9497 This is safe because we don't have undo in this buffer. */
9498
9499 if (NATNUMP (Vmessage_log_max))
9500 {
9501 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9502 -XFASTINT (Vmessage_log_max) - 1, 0);
9503 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9504 }
9505 }
9506 BEGV = marker_position (oldbegv);
9507 BEGV_BYTE = marker_byte_position (oldbegv);
9508
9509 if (zv_at_end)
9510 {
9511 ZV = Z;
9512 ZV_BYTE = Z_BYTE;
9513 }
9514 else
9515 {
9516 ZV = marker_position (oldzv);
9517 ZV_BYTE = marker_byte_position (oldzv);
9518 }
9519
9520 if (point_at_end)
9521 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9522 else
9523 /* We can't do Fgoto_char (oldpoint) because it will run some
9524 Lisp code. */
9525 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9526 marker_byte_position (oldpoint));
9527
9528 UNGCPRO;
9529 unchain_marker (XMARKER (oldpoint));
9530 unchain_marker (XMARKER (oldbegv));
9531 unchain_marker (XMARKER (oldzv));
9532
9533 shown = buffer_window_count (current_buffer) > 0;
9534 set_buffer_internal (oldbuf);
9535 if (!shown)
9536 windows_or_buffers_changed = old_windows_or_buffers_changed;
9537 message_log_need_newline = !nlflag;
9538 Vdeactivate_mark = old_deactivate_mark;
9539 }
9540 }
9541
9542
9543 /* We are at the end of the buffer after just having inserted a newline.
9544 (Note: We depend on the fact we won't be crossing the gap.)
9545 Check to see if the most recent message looks a lot like the previous one.
9546 Return 0 if different, 1 if the new one should just replace it, or a
9547 value N > 1 if we should also append " [N times]". */
9548
9549 static intmax_t
9550 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9551 {
9552 ptrdiff_t i;
9553 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9554 int seen_dots = 0;
9555 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9556 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9557
9558 for (i = 0; i < len; i++)
9559 {
9560 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9561 seen_dots = 1;
9562 if (p1[i] != p2[i])
9563 return seen_dots;
9564 }
9565 p1 += len;
9566 if (*p1 == '\n')
9567 return 2;
9568 if (*p1++ == ' ' && *p1++ == '[')
9569 {
9570 char *pend;
9571 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9572 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9573 return n + 1;
9574 }
9575 return 0;
9576 }
9577 \f
9578
9579 /* Display an echo area message M with a specified length of NBYTES
9580 bytes. The string may include null characters. If M is not a
9581 string, clear out any existing message, and let the mini-buffer
9582 text show through.
9583
9584 This function cancels echoing. */
9585
9586 void
9587 message3 (Lisp_Object m)
9588 {
9589 struct gcpro gcpro1;
9590
9591 GCPRO1 (m);
9592 clear_message (1,1);
9593 cancel_echoing ();
9594
9595 /* First flush out any partial line written with print. */
9596 message_log_maybe_newline ();
9597 if (STRINGP (m))
9598 {
9599 ptrdiff_t nbytes = SBYTES (m);
9600 int multibyte = STRING_MULTIBYTE (m);
9601 USE_SAFE_ALLOCA;
9602 char *buffer = SAFE_ALLOCA (nbytes);
9603 memcpy (buffer, SDATA (m), nbytes);
9604 message_dolog (buffer, nbytes, 1, multibyte);
9605 SAFE_FREE ();
9606 }
9607 message3_nolog (m);
9608
9609 UNGCPRO;
9610 }
9611
9612
9613 /* The non-logging version of message3.
9614 This does not cancel echoing, because it is used for echoing.
9615 Perhaps we need to make a separate function for echoing
9616 and make this cancel echoing. */
9617
9618 void
9619 message3_nolog (Lisp_Object m)
9620 {
9621 struct frame *sf = SELECTED_FRAME ();
9622
9623 if (FRAME_INITIAL_P (sf))
9624 {
9625 if (noninteractive_need_newline)
9626 putc ('\n', stderr);
9627 noninteractive_need_newline = 0;
9628 if (STRINGP (m))
9629 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9630 if (cursor_in_echo_area == 0)
9631 fprintf (stderr, "\n");
9632 fflush (stderr);
9633 }
9634 /* Error messages get reported properly by cmd_error, so this must be just an
9635 informative message; if the frame hasn't really been initialized yet, just
9636 toss it. */
9637 else if (INTERACTIVE && sf->glyphs_initialized_p)
9638 {
9639 /* Get the frame containing the mini-buffer
9640 that the selected frame is using. */
9641 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9642 Lisp_Object frame = XWINDOW (mini_window)->frame;
9643 struct frame *f = XFRAME (frame);
9644
9645 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9646 Fmake_frame_visible (frame);
9647
9648 if (STRINGP (m) && SCHARS (m) > 0)
9649 {
9650 set_message (m);
9651 if (minibuffer_auto_raise)
9652 Fraise_frame (frame);
9653 /* Assume we are not echoing.
9654 (If we are, echo_now will override this.) */
9655 echo_message_buffer = Qnil;
9656 }
9657 else
9658 clear_message (1, 1);
9659
9660 do_pending_window_change (0);
9661 echo_area_display (1);
9662 do_pending_window_change (0);
9663 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9664 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9665 }
9666 }
9667
9668
9669 /* Display a null-terminated echo area message M. If M is 0, clear
9670 out any existing message, and let the mini-buffer text show through.
9671
9672 The buffer M must continue to exist until after the echo area gets
9673 cleared or some other message gets displayed there. Do not pass
9674 text that is stored in a Lisp string. Do not pass text in a buffer
9675 that was alloca'd. */
9676
9677 void
9678 message1 (const char *m)
9679 {
9680 message3 (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9681 }
9682
9683
9684 /* The non-logging counterpart of message1. */
9685
9686 void
9687 message1_nolog (const char *m)
9688 {
9689 message3_nolog (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9690 }
9691
9692 /* Display a message M which contains a single %s
9693 which gets replaced with STRING. */
9694
9695 void
9696 message_with_string (const char *m, Lisp_Object string, int log)
9697 {
9698 CHECK_STRING (string);
9699
9700 if (noninteractive)
9701 {
9702 if (m)
9703 {
9704 if (noninteractive_need_newline)
9705 putc ('\n', stderr);
9706 noninteractive_need_newline = 0;
9707 fprintf (stderr, m, SDATA (string));
9708 if (!cursor_in_echo_area)
9709 fprintf (stderr, "\n");
9710 fflush (stderr);
9711 }
9712 }
9713 else if (INTERACTIVE)
9714 {
9715 /* The frame whose minibuffer we're going to display the message on.
9716 It may be larger than the selected frame, so we need
9717 to use its buffer, not the selected frame's buffer. */
9718 Lisp_Object mini_window;
9719 struct frame *f, *sf = SELECTED_FRAME ();
9720
9721 /* Get the frame containing the minibuffer
9722 that the selected frame is using. */
9723 mini_window = FRAME_MINIBUF_WINDOW (sf);
9724 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9725
9726 /* Error messages get reported properly by cmd_error, so this must be
9727 just an informative message; if the frame hasn't really been
9728 initialized yet, just toss it. */
9729 if (f->glyphs_initialized_p)
9730 {
9731 Lisp_Object args[2], msg;
9732 struct gcpro gcpro1, gcpro2;
9733
9734 args[0] = build_string (m);
9735 args[1] = msg = string;
9736 GCPRO2 (args[0], msg);
9737 gcpro1.nvars = 2;
9738
9739 msg = Fformat (2, args);
9740
9741 if (log)
9742 message3 (msg);
9743 else
9744 message3_nolog (msg);
9745
9746 UNGCPRO;
9747
9748 /* Print should start at the beginning of the message
9749 buffer next time. */
9750 message_buf_print = 0;
9751 }
9752 }
9753 }
9754
9755
9756 /* Dump an informative message to the minibuf. If M is 0, clear out
9757 any existing message, and let the mini-buffer text show through. */
9758
9759 static void
9760 vmessage (const char *m, va_list ap)
9761 {
9762 if (noninteractive)
9763 {
9764 if (m)
9765 {
9766 if (noninteractive_need_newline)
9767 putc ('\n', stderr);
9768 noninteractive_need_newline = 0;
9769 vfprintf (stderr, m, ap);
9770 if (cursor_in_echo_area == 0)
9771 fprintf (stderr, "\n");
9772 fflush (stderr);
9773 }
9774 }
9775 else if (INTERACTIVE)
9776 {
9777 /* The frame whose mini-buffer we're going to display the message
9778 on. It may be larger than the selected frame, so we need to
9779 use its buffer, not the selected frame's buffer. */
9780 Lisp_Object mini_window;
9781 struct frame *f, *sf = SELECTED_FRAME ();
9782
9783 /* Get the frame containing the mini-buffer
9784 that the selected frame is using. */
9785 mini_window = FRAME_MINIBUF_WINDOW (sf);
9786 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9787
9788 /* Error messages get reported properly by cmd_error, so this must be
9789 just an informative message; if the frame hasn't really been
9790 initialized yet, just toss it. */
9791 if (f->glyphs_initialized_p)
9792 {
9793 if (m)
9794 {
9795 ptrdiff_t len;
9796 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9797 char *message_buf = alloca (maxsize + 1);
9798
9799 len = doprnt (message_buf, maxsize, m, (char *)0, ap);
9800
9801 message3 (make_string (message_buf, len));
9802 }
9803 else
9804 message1 (0);
9805
9806 /* Print should start at the beginning of the message
9807 buffer next time. */
9808 message_buf_print = 0;
9809 }
9810 }
9811 }
9812
9813 void
9814 message (const char *m, ...)
9815 {
9816 va_list ap;
9817 va_start (ap, m);
9818 vmessage (m, ap);
9819 va_end (ap);
9820 }
9821
9822
9823 #if 0
9824 /* The non-logging version of message. */
9825
9826 void
9827 message_nolog (const char *m, ...)
9828 {
9829 Lisp_Object old_log_max;
9830 va_list ap;
9831 va_start (ap, m);
9832 old_log_max = Vmessage_log_max;
9833 Vmessage_log_max = Qnil;
9834 vmessage (m, ap);
9835 Vmessage_log_max = old_log_max;
9836 va_end (ap);
9837 }
9838 #endif
9839
9840
9841 /* Display the current message in the current mini-buffer. This is
9842 only called from error handlers in process.c, and is not time
9843 critical. */
9844
9845 void
9846 update_echo_area (void)
9847 {
9848 if (!NILP (echo_area_buffer[0]))
9849 {
9850 Lisp_Object string;
9851 string = Fcurrent_message ();
9852 message3 (string);
9853 }
9854 }
9855
9856
9857 /* Make sure echo area buffers in `echo_buffers' are live.
9858 If they aren't, make new ones. */
9859
9860 static void
9861 ensure_echo_area_buffers (void)
9862 {
9863 int i;
9864
9865 for (i = 0; i < 2; ++i)
9866 if (!BUFFERP (echo_buffer[i])
9867 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9868 {
9869 char name[30];
9870 Lisp_Object old_buffer;
9871 int j;
9872
9873 old_buffer = echo_buffer[i];
9874 echo_buffer[i] = Fget_buffer_create
9875 (make_formatted_string (name, " *Echo Area %d*", i));
9876 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9877 /* to force word wrap in echo area -
9878 it was decided to postpone this*/
9879 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9880
9881 for (j = 0; j < 2; ++j)
9882 if (EQ (old_buffer, echo_area_buffer[j]))
9883 echo_area_buffer[j] = echo_buffer[i];
9884 }
9885 }
9886
9887
9888 /* Call FN with args A1..A2 with either the current or last displayed
9889 echo_area_buffer as current buffer.
9890
9891 WHICH zero means use the current message buffer
9892 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9893 from echo_buffer[] and clear it.
9894
9895 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9896 suitable buffer from echo_buffer[] and clear it.
9897
9898 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9899 that the current message becomes the last displayed one, make
9900 choose a suitable buffer for echo_area_buffer[0], and clear it.
9901
9902 Value is what FN returns. */
9903
9904 static int
9905 with_echo_area_buffer (struct window *w, int which,
9906 int (*fn) (ptrdiff_t, Lisp_Object),
9907 ptrdiff_t a1, Lisp_Object a2)
9908 {
9909 Lisp_Object buffer;
9910 int this_one, the_other, clear_buffer_p, rc;
9911 ptrdiff_t count = SPECPDL_INDEX ();
9912
9913 /* If buffers aren't live, make new ones. */
9914 ensure_echo_area_buffers ();
9915
9916 clear_buffer_p = 0;
9917
9918 if (which == 0)
9919 this_one = 0, the_other = 1;
9920 else if (which > 0)
9921 this_one = 1, the_other = 0;
9922 else
9923 {
9924 this_one = 0, the_other = 1;
9925 clear_buffer_p = 1;
9926
9927 /* We need a fresh one in case the current echo buffer equals
9928 the one containing the last displayed echo area message. */
9929 if (!NILP (echo_area_buffer[this_one])
9930 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9931 echo_area_buffer[this_one] = Qnil;
9932 }
9933
9934 /* Choose a suitable buffer from echo_buffer[] is we don't
9935 have one. */
9936 if (NILP (echo_area_buffer[this_one]))
9937 {
9938 echo_area_buffer[this_one]
9939 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9940 ? echo_buffer[the_other]
9941 : echo_buffer[this_one]);
9942 clear_buffer_p = 1;
9943 }
9944
9945 buffer = echo_area_buffer[this_one];
9946
9947 /* Don't get confused by reusing the buffer used for echoing
9948 for a different purpose. */
9949 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9950 cancel_echoing ();
9951
9952 record_unwind_protect (unwind_with_echo_area_buffer,
9953 with_echo_area_buffer_unwind_data (w));
9954
9955 /* Make the echo area buffer current. Note that for display
9956 purposes, it is not necessary that the displayed window's buffer
9957 == current_buffer, except for text property lookup. So, let's
9958 only set that buffer temporarily here without doing a full
9959 Fset_window_buffer. We must also change w->pointm, though,
9960 because otherwise an assertions in unshow_buffer fails, and Emacs
9961 aborts. */
9962 set_buffer_internal_1 (XBUFFER (buffer));
9963 if (w)
9964 {
9965 wset_buffer (w, buffer);
9966 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
9967 }
9968
9969 bset_undo_list (current_buffer, Qt);
9970 bset_read_only (current_buffer, Qnil);
9971 specbind (Qinhibit_read_only, Qt);
9972 specbind (Qinhibit_modification_hooks, Qt);
9973
9974 if (clear_buffer_p && Z > BEG)
9975 del_range (BEG, Z);
9976
9977 eassert (BEGV >= BEG);
9978 eassert (ZV <= Z && ZV >= BEGV);
9979
9980 rc = fn (a1, a2);
9981
9982 eassert (BEGV >= BEG);
9983 eassert (ZV <= Z && ZV >= BEGV);
9984
9985 unbind_to (count, Qnil);
9986 return rc;
9987 }
9988
9989
9990 /* Save state that should be preserved around the call to the function
9991 FN called in with_echo_area_buffer. */
9992
9993 static Lisp_Object
9994 with_echo_area_buffer_unwind_data (struct window *w)
9995 {
9996 int i = 0;
9997 Lisp_Object vector, tmp;
9998
9999 /* Reduce consing by keeping one vector in
10000 Vwith_echo_area_save_vector. */
10001 vector = Vwith_echo_area_save_vector;
10002 Vwith_echo_area_save_vector = Qnil;
10003
10004 if (NILP (vector))
10005 vector = Fmake_vector (make_number (7), Qnil);
10006
10007 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10008 ASET (vector, i, Vdeactivate_mark); ++i;
10009 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10010
10011 if (w)
10012 {
10013 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10014 ASET (vector, i, w->buffer); ++i;
10015 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10016 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10017 }
10018 else
10019 {
10020 int end = i + 4;
10021 for (; i < end; ++i)
10022 ASET (vector, i, Qnil);
10023 }
10024
10025 eassert (i == ASIZE (vector));
10026 return vector;
10027 }
10028
10029
10030 /* Restore global state from VECTOR which was created by
10031 with_echo_area_buffer_unwind_data. */
10032
10033 static Lisp_Object
10034 unwind_with_echo_area_buffer (Lisp_Object vector)
10035 {
10036 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10037 Vdeactivate_mark = AREF (vector, 1);
10038 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10039
10040 if (WINDOWP (AREF (vector, 3)))
10041 {
10042 struct window *w;
10043 Lisp_Object buffer, charpos, bytepos;
10044
10045 w = XWINDOW (AREF (vector, 3));
10046 buffer = AREF (vector, 4);
10047 charpos = AREF (vector, 5);
10048 bytepos = AREF (vector, 6);
10049
10050 wset_buffer (w, buffer);
10051 set_marker_both (w->pointm, buffer,
10052 XFASTINT (charpos), XFASTINT (bytepos));
10053 }
10054
10055 Vwith_echo_area_save_vector = vector;
10056 return Qnil;
10057 }
10058
10059
10060 /* Set up the echo area for use by print functions. MULTIBYTE_P
10061 non-zero means we will print multibyte. */
10062
10063 void
10064 setup_echo_area_for_printing (int multibyte_p)
10065 {
10066 /* If we can't find an echo area any more, exit. */
10067 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10068 Fkill_emacs (Qnil);
10069
10070 ensure_echo_area_buffers ();
10071
10072 if (!message_buf_print)
10073 {
10074 /* A message has been output since the last time we printed.
10075 Choose a fresh echo area buffer. */
10076 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10077 echo_area_buffer[0] = echo_buffer[1];
10078 else
10079 echo_area_buffer[0] = echo_buffer[0];
10080
10081 /* Switch to that buffer and clear it. */
10082 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10083 bset_truncate_lines (current_buffer, Qnil);
10084
10085 if (Z > BEG)
10086 {
10087 ptrdiff_t count = SPECPDL_INDEX ();
10088 specbind (Qinhibit_read_only, Qt);
10089 /* Note that undo recording is always disabled. */
10090 del_range (BEG, Z);
10091 unbind_to (count, Qnil);
10092 }
10093 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10094
10095 /* Set up the buffer for the multibyteness we need. */
10096 if (multibyte_p
10097 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10098 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10099
10100 /* Raise the frame containing the echo area. */
10101 if (minibuffer_auto_raise)
10102 {
10103 struct frame *sf = SELECTED_FRAME ();
10104 Lisp_Object mini_window;
10105 mini_window = FRAME_MINIBUF_WINDOW (sf);
10106 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10107 }
10108
10109 message_log_maybe_newline ();
10110 message_buf_print = 1;
10111 }
10112 else
10113 {
10114 if (NILP (echo_area_buffer[0]))
10115 {
10116 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10117 echo_area_buffer[0] = echo_buffer[1];
10118 else
10119 echo_area_buffer[0] = echo_buffer[0];
10120 }
10121
10122 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10123 {
10124 /* Someone switched buffers between print requests. */
10125 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10126 bset_truncate_lines (current_buffer, Qnil);
10127 }
10128 }
10129 }
10130
10131
10132 /* Display an echo area message in window W. Value is non-zero if W's
10133 height is changed. If display_last_displayed_message_p is
10134 non-zero, display the message that was last displayed, otherwise
10135 display the current message. */
10136
10137 static int
10138 display_echo_area (struct window *w)
10139 {
10140 int i, no_message_p, window_height_changed_p;
10141
10142 /* Temporarily disable garbage collections while displaying the echo
10143 area. This is done because a GC can print a message itself.
10144 That message would modify the echo area buffer's contents while a
10145 redisplay of the buffer is going on, and seriously confuse
10146 redisplay. */
10147 ptrdiff_t count = inhibit_garbage_collection ();
10148
10149 /* If there is no message, we must call display_echo_area_1
10150 nevertheless because it resizes the window. But we will have to
10151 reset the echo_area_buffer in question to nil at the end because
10152 with_echo_area_buffer will sets it to an empty buffer. */
10153 i = display_last_displayed_message_p ? 1 : 0;
10154 no_message_p = NILP (echo_area_buffer[i]);
10155
10156 window_height_changed_p
10157 = with_echo_area_buffer (w, display_last_displayed_message_p,
10158 display_echo_area_1,
10159 (intptr_t) w, Qnil);
10160
10161 if (no_message_p)
10162 echo_area_buffer[i] = Qnil;
10163
10164 unbind_to (count, Qnil);
10165 return window_height_changed_p;
10166 }
10167
10168
10169 /* Helper for display_echo_area. Display the current buffer which
10170 contains the current echo area message in window W, a mini-window,
10171 a pointer to which is passed in A1. A2..A4 are currently not used.
10172 Change the height of W so that all of the message is displayed.
10173 Value is non-zero if height of W was changed. */
10174
10175 static int
10176 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10177 {
10178 intptr_t i1 = a1;
10179 struct window *w = (struct window *) i1;
10180 Lisp_Object window;
10181 struct text_pos start;
10182 int window_height_changed_p = 0;
10183
10184 /* Do this before displaying, so that we have a large enough glyph
10185 matrix for the display. If we can't get enough space for the
10186 whole text, display the last N lines. That works by setting w->start. */
10187 window_height_changed_p = resize_mini_window (w, 0);
10188
10189 /* Use the starting position chosen by resize_mini_window. */
10190 SET_TEXT_POS_FROM_MARKER (start, w->start);
10191
10192 /* Display. */
10193 clear_glyph_matrix (w->desired_matrix);
10194 XSETWINDOW (window, w);
10195 try_window (window, start, 0);
10196
10197 return window_height_changed_p;
10198 }
10199
10200
10201 /* Resize the echo area window to exactly the size needed for the
10202 currently displayed message, if there is one. If a mini-buffer
10203 is active, don't shrink it. */
10204
10205 void
10206 resize_echo_area_exactly (void)
10207 {
10208 if (BUFFERP (echo_area_buffer[0])
10209 && WINDOWP (echo_area_window))
10210 {
10211 struct window *w = XWINDOW (echo_area_window);
10212 int resized_p;
10213 Lisp_Object resize_exactly;
10214
10215 if (minibuf_level == 0)
10216 resize_exactly = Qt;
10217 else
10218 resize_exactly = Qnil;
10219
10220 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10221 (intptr_t) w, resize_exactly);
10222 if (resized_p)
10223 {
10224 ++windows_or_buffers_changed;
10225 ++update_mode_lines;
10226 redisplay_internal ();
10227 }
10228 }
10229 }
10230
10231
10232 /* Callback function for with_echo_area_buffer, when used from
10233 resize_echo_area_exactly. A1 contains a pointer to the window to
10234 resize, EXACTLY non-nil means resize the mini-window exactly to the
10235 size of the text displayed. A3 and A4 are not used. Value is what
10236 resize_mini_window returns. */
10237
10238 static int
10239 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10240 {
10241 intptr_t i1 = a1;
10242 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10243 }
10244
10245
10246 /* Resize mini-window W to fit the size of its contents. EXACT_P
10247 means size the window exactly to the size needed. Otherwise, it's
10248 only enlarged until W's buffer is empty.
10249
10250 Set W->start to the right place to begin display. If the whole
10251 contents fit, start at the beginning. Otherwise, start so as
10252 to make the end of the contents appear. This is particularly
10253 important for y-or-n-p, but seems desirable generally.
10254
10255 Value is non-zero if the window height has been changed. */
10256
10257 int
10258 resize_mini_window (struct window *w, int exact_p)
10259 {
10260 struct frame *f = XFRAME (w->frame);
10261 int window_height_changed_p = 0;
10262
10263 eassert (MINI_WINDOW_P (w));
10264
10265 /* By default, start display at the beginning. */
10266 set_marker_both (w->start, w->buffer,
10267 BUF_BEGV (XBUFFER (w->buffer)),
10268 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10269
10270 /* Don't resize windows while redisplaying a window; it would
10271 confuse redisplay functions when the size of the window they are
10272 displaying changes from under them. Such a resizing can happen,
10273 for instance, when which-func prints a long message while
10274 we are running fontification-functions. We're running these
10275 functions with safe_call which binds inhibit-redisplay to t. */
10276 if (!NILP (Vinhibit_redisplay))
10277 return 0;
10278
10279 /* Nil means don't try to resize. */
10280 if (NILP (Vresize_mini_windows)
10281 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10282 return 0;
10283
10284 if (!FRAME_MINIBUF_ONLY_P (f))
10285 {
10286 struct it it;
10287 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10288 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10289 int height;
10290 EMACS_INT max_height;
10291 int unit = FRAME_LINE_HEIGHT (f);
10292 struct text_pos start;
10293 struct buffer *old_current_buffer = NULL;
10294
10295 if (current_buffer != XBUFFER (w->buffer))
10296 {
10297 old_current_buffer = current_buffer;
10298 set_buffer_internal (XBUFFER (w->buffer));
10299 }
10300
10301 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10302
10303 /* Compute the max. number of lines specified by the user. */
10304 if (FLOATP (Vmax_mini_window_height))
10305 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10306 else if (INTEGERP (Vmax_mini_window_height))
10307 max_height = XINT (Vmax_mini_window_height);
10308 else
10309 max_height = total_height / 4;
10310
10311 /* Correct that max. height if it's bogus. */
10312 max_height = clip_to_bounds (1, max_height, total_height);
10313
10314 /* Find out the height of the text in the window. */
10315 if (it.line_wrap == TRUNCATE)
10316 height = 1;
10317 else
10318 {
10319 last_height = 0;
10320 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10321 if (it.max_ascent == 0 && it.max_descent == 0)
10322 height = it.current_y + last_height;
10323 else
10324 height = it.current_y + it.max_ascent + it.max_descent;
10325 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10326 height = (height + unit - 1) / unit;
10327 }
10328
10329 /* Compute a suitable window start. */
10330 if (height > max_height)
10331 {
10332 height = max_height;
10333 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10334 move_it_vertically_backward (&it, (height - 1) * unit);
10335 start = it.current.pos;
10336 }
10337 else
10338 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10339 SET_MARKER_FROM_TEXT_POS (w->start, start);
10340
10341 if (EQ (Vresize_mini_windows, Qgrow_only))
10342 {
10343 /* Let it grow only, until we display an empty message, in which
10344 case the window shrinks again. */
10345 if (height > WINDOW_TOTAL_LINES (w))
10346 {
10347 int old_height = WINDOW_TOTAL_LINES (w);
10348 freeze_window_starts (f, 1);
10349 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10350 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10351 }
10352 else if (height < WINDOW_TOTAL_LINES (w)
10353 && (exact_p || BEGV == ZV))
10354 {
10355 int old_height = WINDOW_TOTAL_LINES (w);
10356 freeze_window_starts (f, 0);
10357 shrink_mini_window (w);
10358 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10359 }
10360 }
10361 else
10362 {
10363 /* Always resize to exact size needed. */
10364 if (height > WINDOW_TOTAL_LINES (w))
10365 {
10366 int old_height = WINDOW_TOTAL_LINES (w);
10367 freeze_window_starts (f, 1);
10368 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10369 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10370 }
10371 else if (height < WINDOW_TOTAL_LINES (w))
10372 {
10373 int old_height = WINDOW_TOTAL_LINES (w);
10374 freeze_window_starts (f, 0);
10375 shrink_mini_window (w);
10376
10377 if (height)
10378 {
10379 freeze_window_starts (f, 1);
10380 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10381 }
10382
10383 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10384 }
10385 }
10386
10387 if (old_current_buffer)
10388 set_buffer_internal (old_current_buffer);
10389 }
10390
10391 return window_height_changed_p;
10392 }
10393
10394
10395 /* Value is the current message, a string, or nil if there is no
10396 current message. */
10397
10398 Lisp_Object
10399 current_message (void)
10400 {
10401 Lisp_Object msg;
10402
10403 if (!BUFFERP (echo_area_buffer[0]))
10404 msg = Qnil;
10405 else
10406 {
10407 with_echo_area_buffer (0, 0, current_message_1,
10408 (intptr_t) &msg, Qnil);
10409 if (NILP (msg))
10410 echo_area_buffer[0] = Qnil;
10411 }
10412
10413 return msg;
10414 }
10415
10416
10417 static int
10418 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10419 {
10420 intptr_t i1 = a1;
10421 Lisp_Object *msg = (Lisp_Object *) i1;
10422
10423 if (Z > BEG)
10424 *msg = make_buffer_string (BEG, Z, 1);
10425 else
10426 *msg = Qnil;
10427 return 0;
10428 }
10429
10430
10431 /* Push the current message on Vmessage_stack for later restoration
10432 by restore_message. Value is non-zero if the current message isn't
10433 empty. This is a relatively infrequent operation, so it's not
10434 worth optimizing. */
10435
10436 bool
10437 push_message (void)
10438 {
10439 Lisp_Object msg = current_message ();
10440 Vmessage_stack = Fcons (msg, Vmessage_stack);
10441 return STRINGP (msg);
10442 }
10443
10444
10445 /* Restore message display from the top of Vmessage_stack. */
10446
10447 void
10448 restore_message (void)
10449 {
10450 eassert (CONSP (Vmessage_stack));
10451 message3_nolog (XCAR (Vmessage_stack));
10452 }
10453
10454
10455 /* Handler for record_unwind_protect calling pop_message. */
10456
10457 Lisp_Object
10458 pop_message_unwind (Lisp_Object dummy)
10459 {
10460 pop_message ();
10461 return Qnil;
10462 }
10463
10464 /* Pop the top-most entry off Vmessage_stack. */
10465
10466 static void
10467 pop_message (void)
10468 {
10469 eassert (CONSP (Vmessage_stack));
10470 Vmessage_stack = XCDR (Vmessage_stack);
10471 }
10472
10473
10474 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10475 exits. If the stack is not empty, we have a missing pop_message
10476 somewhere. */
10477
10478 void
10479 check_message_stack (void)
10480 {
10481 if (!NILP (Vmessage_stack))
10482 emacs_abort ();
10483 }
10484
10485
10486 /* Truncate to NCHARS what will be displayed in the echo area the next
10487 time we display it---but don't redisplay it now. */
10488
10489 void
10490 truncate_echo_area (ptrdiff_t nchars)
10491 {
10492 if (nchars == 0)
10493 echo_area_buffer[0] = Qnil;
10494 else if (!noninteractive
10495 && INTERACTIVE
10496 && !NILP (echo_area_buffer[0]))
10497 {
10498 struct frame *sf = SELECTED_FRAME ();
10499 /* Error messages get reported properly by cmd_error, so this must be
10500 just an informative message; if the frame hasn't really been
10501 initialized yet, just toss it. */
10502 if (sf->glyphs_initialized_p)
10503 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10504 }
10505 }
10506
10507
10508 /* Helper function for truncate_echo_area. Truncate the current
10509 message to at most NCHARS characters. */
10510
10511 static int
10512 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10513 {
10514 if (BEG + nchars < Z)
10515 del_range (BEG + nchars, Z);
10516 if (Z == BEG)
10517 echo_area_buffer[0] = Qnil;
10518 return 0;
10519 }
10520
10521 /* Set the current message to STRING. */
10522
10523 static void
10524 set_message (Lisp_Object string)
10525 {
10526 eassert (STRINGP (string));
10527
10528 message_enable_multibyte = STRING_MULTIBYTE (string);
10529
10530 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10531 message_buf_print = 0;
10532 help_echo_showing_p = 0;
10533
10534 if (STRINGP (Vdebug_on_message)
10535 && fast_string_match (Vdebug_on_message, string) >= 0)
10536 call_debugger (list2 (Qerror, string));
10537 }
10538
10539
10540 /* Helper function for set_message. First argument is ignored and second
10541 argument has the same meaning as for set_message.
10542 This function is called with the echo area buffer being current. */
10543
10544 static int
10545 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10546 {
10547 eassert (STRINGP (string));
10548
10549 /* Change multibyteness of the echo buffer appropriately. */
10550 if (message_enable_multibyte
10551 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10552 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10553
10554 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10555 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10556 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10557
10558 /* Insert new message at BEG. */
10559 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10560
10561 /* This function takes care of single/multibyte conversion.
10562 We just have to ensure that the echo area buffer has the right
10563 setting of enable_multibyte_characters. */
10564 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10565
10566 return 0;
10567 }
10568
10569
10570 /* Clear messages. CURRENT_P non-zero means clear the current
10571 message. LAST_DISPLAYED_P non-zero means clear the message
10572 last displayed. */
10573
10574 void
10575 clear_message (int current_p, int last_displayed_p)
10576 {
10577 if (current_p)
10578 {
10579 echo_area_buffer[0] = Qnil;
10580 message_cleared_p = 1;
10581 }
10582
10583 if (last_displayed_p)
10584 echo_area_buffer[1] = Qnil;
10585
10586 message_buf_print = 0;
10587 }
10588
10589 /* Clear garbaged frames.
10590
10591 This function is used where the old redisplay called
10592 redraw_garbaged_frames which in turn called redraw_frame which in
10593 turn called clear_frame. The call to clear_frame was a source of
10594 flickering. I believe a clear_frame is not necessary. It should
10595 suffice in the new redisplay to invalidate all current matrices,
10596 and ensure a complete redisplay of all windows. */
10597
10598 static void
10599 clear_garbaged_frames (void)
10600 {
10601 if (frame_garbaged)
10602 {
10603 Lisp_Object tail, frame;
10604 int changed_count = 0;
10605
10606 FOR_EACH_FRAME (tail, frame)
10607 {
10608 struct frame *f = XFRAME (frame);
10609
10610 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10611 {
10612 if (f->resized_p)
10613 {
10614 redraw_frame (f);
10615 f->force_flush_display_p = 1;
10616 }
10617 clear_current_matrices (f);
10618 changed_count++;
10619 f->garbaged = 0;
10620 f->resized_p = 0;
10621 }
10622 }
10623
10624 frame_garbaged = 0;
10625 if (changed_count)
10626 ++windows_or_buffers_changed;
10627 }
10628 }
10629
10630
10631 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10632 is non-zero update selected_frame. Value is non-zero if the
10633 mini-windows height has been changed. */
10634
10635 static int
10636 echo_area_display (int update_frame_p)
10637 {
10638 Lisp_Object mini_window;
10639 struct window *w;
10640 struct frame *f;
10641 int window_height_changed_p = 0;
10642 struct frame *sf = SELECTED_FRAME ();
10643
10644 mini_window = FRAME_MINIBUF_WINDOW (sf);
10645 w = XWINDOW (mini_window);
10646 f = XFRAME (WINDOW_FRAME (w));
10647
10648 /* Don't display if frame is invisible or not yet initialized. */
10649 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10650 return 0;
10651
10652 #ifdef HAVE_WINDOW_SYSTEM
10653 /* When Emacs starts, selected_frame may be the initial terminal
10654 frame. If we let this through, a message would be displayed on
10655 the terminal. */
10656 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10657 return 0;
10658 #endif /* HAVE_WINDOW_SYSTEM */
10659
10660 /* Redraw garbaged frames. */
10661 clear_garbaged_frames ();
10662
10663 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10664 {
10665 echo_area_window = mini_window;
10666 window_height_changed_p = display_echo_area (w);
10667 w->must_be_updated_p = 1;
10668
10669 /* Update the display, unless called from redisplay_internal.
10670 Also don't update the screen during redisplay itself. The
10671 update will happen at the end of redisplay, and an update
10672 here could cause confusion. */
10673 if (update_frame_p && !redisplaying_p)
10674 {
10675 int n = 0;
10676
10677 /* If the display update has been interrupted by pending
10678 input, update mode lines in the frame. Due to the
10679 pending input, it might have been that redisplay hasn't
10680 been called, so that mode lines above the echo area are
10681 garbaged. This looks odd, so we prevent it here. */
10682 if (!display_completed)
10683 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10684
10685 if (window_height_changed_p
10686 /* Don't do this if Emacs is shutting down. Redisplay
10687 needs to run hooks. */
10688 && !NILP (Vrun_hooks))
10689 {
10690 /* Must update other windows. Likewise as in other
10691 cases, don't let this update be interrupted by
10692 pending input. */
10693 ptrdiff_t count = SPECPDL_INDEX ();
10694 specbind (Qredisplay_dont_pause, Qt);
10695 windows_or_buffers_changed = 1;
10696 redisplay_internal ();
10697 unbind_to (count, Qnil);
10698 }
10699 else if (FRAME_WINDOW_P (f) && n == 0)
10700 {
10701 /* Window configuration is the same as before.
10702 Can do with a display update of the echo area,
10703 unless we displayed some mode lines. */
10704 update_single_window (w, 1);
10705 FRAME_RIF (f)->flush_display (f);
10706 }
10707 else
10708 update_frame (f, 1, 1);
10709
10710 /* If cursor is in the echo area, make sure that the next
10711 redisplay displays the minibuffer, so that the cursor will
10712 be replaced with what the minibuffer wants. */
10713 if (cursor_in_echo_area)
10714 ++windows_or_buffers_changed;
10715 }
10716 }
10717 else if (!EQ (mini_window, selected_window))
10718 windows_or_buffers_changed++;
10719
10720 /* Last displayed message is now the current message. */
10721 echo_area_buffer[1] = echo_area_buffer[0];
10722 /* Inform read_char that we're not echoing. */
10723 echo_message_buffer = Qnil;
10724
10725 /* Prevent redisplay optimization in redisplay_internal by resetting
10726 this_line_start_pos. This is done because the mini-buffer now
10727 displays the message instead of its buffer text. */
10728 if (EQ (mini_window, selected_window))
10729 CHARPOS (this_line_start_pos) = 0;
10730
10731 return window_height_changed_p;
10732 }
10733
10734 /* Nonzero if the current window's buffer is shown in more than one
10735 window and was modified since last redisplay. */
10736
10737 static int
10738 buffer_shared_and_changed (void)
10739 {
10740 return (buffer_window_count (current_buffer) > 1
10741 && UNCHANGED_MODIFIED < MODIFF);
10742 }
10743
10744 /* Nonzero if W doesn't reflect the actual state of current buffer due
10745 to its text or overlays change. FIXME: this may be called when
10746 XBUFFER (w->buffer) != current_buffer, which looks suspicious. */
10747
10748 static int
10749 window_outdated (struct window *w)
10750 {
10751 return (w->last_modified < MODIFF
10752 || w->last_overlay_modified < OVERLAY_MODIFF);
10753 }
10754
10755 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10756 is enabled and mark of W's buffer was changed since last W's update. */
10757
10758 static int
10759 window_buffer_changed (struct window *w)
10760 {
10761 struct buffer *b = XBUFFER (w->buffer);
10762
10763 eassert (BUFFER_LIVE_P (b));
10764
10765 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10766 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10767 != (w->region_showing != 0)));
10768 }
10769
10770 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10771
10772 static int
10773 mode_line_update_needed (struct window *w)
10774 {
10775 return (w->column_number_displayed != -1
10776 && !(PT == w->last_point && !window_outdated (w))
10777 && (w->column_number_displayed != current_column ()));
10778 }
10779
10780 /***********************************************************************
10781 Mode Lines and Frame Titles
10782 ***********************************************************************/
10783
10784 /* A buffer for constructing non-propertized mode-line strings and
10785 frame titles in it; allocated from the heap in init_xdisp and
10786 resized as needed in store_mode_line_noprop_char. */
10787
10788 static char *mode_line_noprop_buf;
10789
10790 /* The buffer's end, and a current output position in it. */
10791
10792 static char *mode_line_noprop_buf_end;
10793 static char *mode_line_noprop_ptr;
10794
10795 #define MODE_LINE_NOPROP_LEN(start) \
10796 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10797
10798 static enum {
10799 MODE_LINE_DISPLAY = 0,
10800 MODE_LINE_TITLE,
10801 MODE_LINE_NOPROP,
10802 MODE_LINE_STRING
10803 } mode_line_target;
10804
10805 /* Alist that caches the results of :propertize.
10806 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10807 static Lisp_Object mode_line_proptrans_alist;
10808
10809 /* List of strings making up the mode-line. */
10810 static Lisp_Object mode_line_string_list;
10811
10812 /* Base face property when building propertized mode line string. */
10813 static Lisp_Object mode_line_string_face;
10814 static Lisp_Object mode_line_string_face_prop;
10815
10816
10817 /* Unwind data for mode line strings */
10818
10819 static Lisp_Object Vmode_line_unwind_vector;
10820
10821 static Lisp_Object
10822 format_mode_line_unwind_data (struct frame *target_frame,
10823 struct buffer *obuf,
10824 Lisp_Object owin,
10825 int save_proptrans)
10826 {
10827 Lisp_Object vector, tmp;
10828
10829 /* Reduce consing by keeping one vector in
10830 Vwith_echo_area_save_vector. */
10831 vector = Vmode_line_unwind_vector;
10832 Vmode_line_unwind_vector = Qnil;
10833
10834 if (NILP (vector))
10835 vector = Fmake_vector (make_number (10), Qnil);
10836
10837 ASET (vector, 0, make_number (mode_line_target));
10838 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10839 ASET (vector, 2, mode_line_string_list);
10840 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10841 ASET (vector, 4, mode_line_string_face);
10842 ASET (vector, 5, mode_line_string_face_prop);
10843
10844 if (obuf)
10845 XSETBUFFER (tmp, obuf);
10846 else
10847 tmp = Qnil;
10848 ASET (vector, 6, tmp);
10849 ASET (vector, 7, owin);
10850 if (target_frame)
10851 {
10852 /* Similarly to `with-selected-window', if the operation selects
10853 a window on another frame, we must restore that frame's
10854 selected window, and (for a tty) the top-frame. */
10855 ASET (vector, 8, target_frame->selected_window);
10856 if (FRAME_TERMCAP_P (target_frame))
10857 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10858 }
10859
10860 return vector;
10861 }
10862
10863 static Lisp_Object
10864 unwind_format_mode_line (Lisp_Object vector)
10865 {
10866 Lisp_Object old_window = AREF (vector, 7);
10867 Lisp_Object target_frame_window = AREF (vector, 8);
10868 Lisp_Object old_top_frame = AREF (vector, 9);
10869
10870 mode_line_target = XINT (AREF (vector, 0));
10871 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10872 mode_line_string_list = AREF (vector, 2);
10873 if (! EQ (AREF (vector, 3), Qt))
10874 mode_line_proptrans_alist = AREF (vector, 3);
10875 mode_line_string_face = AREF (vector, 4);
10876 mode_line_string_face_prop = AREF (vector, 5);
10877
10878 /* Select window before buffer, since it may change the buffer. */
10879 if (!NILP (old_window))
10880 {
10881 /* If the operation that we are unwinding had selected a window
10882 on a different frame, reset its frame-selected-window. For a
10883 text terminal, reset its top-frame if necessary. */
10884 if (!NILP (target_frame_window))
10885 {
10886 Lisp_Object frame
10887 = WINDOW_FRAME (XWINDOW (target_frame_window));
10888
10889 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10890 Fselect_window (target_frame_window, Qt);
10891
10892 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10893 Fselect_frame (old_top_frame, Qt);
10894 }
10895
10896 Fselect_window (old_window, Qt);
10897 }
10898
10899 if (!NILP (AREF (vector, 6)))
10900 {
10901 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10902 ASET (vector, 6, Qnil);
10903 }
10904
10905 Vmode_line_unwind_vector = vector;
10906 return Qnil;
10907 }
10908
10909
10910 /* Store a single character C for the frame title in mode_line_noprop_buf.
10911 Re-allocate mode_line_noprop_buf if necessary. */
10912
10913 static void
10914 store_mode_line_noprop_char (char c)
10915 {
10916 /* If output position has reached the end of the allocated buffer,
10917 increase the buffer's size. */
10918 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10919 {
10920 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10921 ptrdiff_t size = len;
10922 mode_line_noprop_buf =
10923 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10924 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10925 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10926 }
10927
10928 *mode_line_noprop_ptr++ = c;
10929 }
10930
10931
10932 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10933 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10934 characters that yield more columns than PRECISION; PRECISION <= 0
10935 means copy the whole string. Pad with spaces until FIELD_WIDTH
10936 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10937 pad. Called from display_mode_element when it is used to build a
10938 frame title. */
10939
10940 static int
10941 store_mode_line_noprop (const char *string, int field_width, int precision)
10942 {
10943 const unsigned char *str = (const unsigned char *) string;
10944 int n = 0;
10945 ptrdiff_t dummy, nbytes;
10946
10947 /* Copy at most PRECISION chars from STR. */
10948 nbytes = strlen (string);
10949 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10950 while (nbytes--)
10951 store_mode_line_noprop_char (*str++);
10952
10953 /* Fill up with spaces until FIELD_WIDTH reached. */
10954 while (field_width > 0
10955 && n < field_width)
10956 {
10957 store_mode_line_noprop_char (' ');
10958 ++n;
10959 }
10960
10961 return n;
10962 }
10963
10964 /***********************************************************************
10965 Frame Titles
10966 ***********************************************************************/
10967
10968 #ifdef HAVE_WINDOW_SYSTEM
10969
10970 /* Set the title of FRAME, if it has changed. The title format is
10971 Vicon_title_format if FRAME is iconified, otherwise it is
10972 frame_title_format. */
10973
10974 static void
10975 x_consider_frame_title (Lisp_Object frame)
10976 {
10977 struct frame *f = XFRAME (frame);
10978
10979 if (FRAME_WINDOW_P (f)
10980 || FRAME_MINIBUF_ONLY_P (f)
10981 || f->explicit_name)
10982 {
10983 /* Do we have more than one visible frame on this X display? */
10984 Lisp_Object tail, other_frame, fmt;
10985 ptrdiff_t title_start;
10986 char *title;
10987 ptrdiff_t len;
10988 struct it it;
10989 ptrdiff_t count = SPECPDL_INDEX ();
10990
10991 FOR_EACH_FRAME (tail, other_frame)
10992 {
10993 struct frame *tf = XFRAME (other_frame);
10994
10995 if (tf != f
10996 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
10997 && !FRAME_MINIBUF_ONLY_P (tf)
10998 && !EQ (other_frame, tip_frame)
10999 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11000 break;
11001 }
11002
11003 /* Set global variable indicating that multiple frames exist. */
11004 multiple_frames = CONSP (tail);
11005
11006 /* Switch to the buffer of selected window of the frame. Set up
11007 mode_line_target so that display_mode_element will output into
11008 mode_line_noprop_buf; then display the title. */
11009 record_unwind_protect (unwind_format_mode_line,
11010 format_mode_line_unwind_data
11011 (f, current_buffer, selected_window, 0));
11012
11013 Fselect_window (f->selected_window, Qt);
11014 set_buffer_internal_1
11015 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11016 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11017
11018 mode_line_target = MODE_LINE_TITLE;
11019 title_start = MODE_LINE_NOPROP_LEN (0);
11020 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11021 NULL, DEFAULT_FACE_ID);
11022 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11023 len = MODE_LINE_NOPROP_LEN (title_start);
11024 title = mode_line_noprop_buf + title_start;
11025 unbind_to (count, Qnil);
11026
11027 /* Set the title only if it's changed. This avoids consing in
11028 the common case where it hasn't. (If it turns out that we've
11029 already wasted too much time by walking through the list with
11030 display_mode_element, then we might need to optimize at a
11031 higher level than this.) */
11032 if (! STRINGP (f->name)
11033 || SBYTES (f->name) != len
11034 || memcmp (title, SDATA (f->name), len) != 0)
11035 x_implicitly_set_name (f, make_string (title, len), Qnil);
11036 }
11037 }
11038
11039 #endif /* not HAVE_WINDOW_SYSTEM */
11040
11041 \f
11042 /***********************************************************************
11043 Menu Bars
11044 ***********************************************************************/
11045
11046
11047 /* Prepare for redisplay by updating menu-bar item lists when
11048 appropriate. This can call eval. */
11049
11050 void
11051 prepare_menu_bars (void)
11052 {
11053 int all_windows;
11054 struct gcpro gcpro1, gcpro2;
11055 struct frame *f;
11056 Lisp_Object tooltip_frame;
11057
11058 #ifdef HAVE_WINDOW_SYSTEM
11059 tooltip_frame = tip_frame;
11060 #else
11061 tooltip_frame = Qnil;
11062 #endif
11063
11064 /* Update all frame titles based on their buffer names, etc. We do
11065 this before the menu bars so that the buffer-menu will show the
11066 up-to-date frame titles. */
11067 #ifdef HAVE_WINDOW_SYSTEM
11068 if (windows_or_buffers_changed || update_mode_lines)
11069 {
11070 Lisp_Object tail, frame;
11071
11072 FOR_EACH_FRAME (tail, frame)
11073 {
11074 f = XFRAME (frame);
11075 if (!EQ (frame, tooltip_frame)
11076 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11077 x_consider_frame_title (frame);
11078 }
11079 }
11080 #endif /* HAVE_WINDOW_SYSTEM */
11081
11082 /* Update the menu bar item lists, if appropriate. This has to be
11083 done before any actual redisplay or generation of display lines. */
11084 all_windows = (update_mode_lines
11085 || buffer_shared_and_changed ()
11086 || windows_or_buffers_changed);
11087 if (all_windows)
11088 {
11089 Lisp_Object tail, frame;
11090 ptrdiff_t count = SPECPDL_INDEX ();
11091 /* 1 means that update_menu_bar has run its hooks
11092 so any further calls to update_menu_bar shouldn't do so again. */
11093 int menu_bar_hooks_run = 0;
11094
11095 record_unwind_save_match_data ();
11096
11097 FOR_EACH_FRAME (tail, frame)
11098 {
11099 f = XFRAME (frame);
11100
11101 /* Ignore tooltip frame. */
11102 if (EQ (frame, tooltip_frame))
11103 continue;
11104
11105 /* If a window on this frame changed size, report that to
11106 the user and clear the size-change flag. */
11107 if (FRAME_WINDOW_SIZES_CHANGED (f))
11108 {
11109 Lisp_Object functions;
11110
11111 /* Clear flag first in case we get an error below. */
11112 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11113 functions = Vwindow_size_change_functions;
11114 GCPRO2 (tail, functions);
11115
11116 while (CONSP (functions))
11117 {
11118 if (!EQ (XCAR (functions), Qt))
11119 call1 (XCAR (functions), frame);
11120 functions = XCDR (functions);
11121 }
11122 UNGCPRO;
11123 }
11124
11125 GCPRO1 (tail);
11126 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11127 #ifdef HAVE_WINDOW_SYSTEM
11128 update_tool_bar (f, 0);
11129 #endif
11130 #ifdef HAVE_NS
11131 if (windows_or_buffers_changed
11132 && FRAME_NS_P (f))
11133 ns_set_doc_edited
11134 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->buffer));
11135 #endif
11136 UNGCPRO;
11137 }
11138
11139 unbind_to (count, Qnil);
11140 }
11141 else
11142 {
11143 struct frame *sf = SELECTED_FRAME ();
11144 update_menu_bar (sf, 1, 0);
11145 #ifdef HAVE_WINDOW_SYSTEM
11146 update_tool_bar (sf, 1);
11147 #endif
11148 }
11149 }
11150
11151
11152 /* Update the menu bar item list for frame F. This has to be done
11153 before we start to fill in any display lines, because it can call
11154 eval.
11155
11156 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11157
11158 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11159 already ran the menu bar hooks for this redisplay, so there
11160 is no need to run them again. The return value is the
11161 updated value of this flag, to pass to the next call. */
11162
11163 static int
11164 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11165 {
11166 Lisp_Object window;
11167 register struct window *w;
11168
11169 /* If called recursively during a menu update, do nothing. This can
11170 happen when, for instance, an activate-menubar-hook causes a
11171 redisplay. */
11172 if (inhibit_menubar_update)
11173 return hooks_run;
11174
11175 window = FRAME_SELECTED_WINDOW (f);
11176 w = XWINDOW (window);
11177
11178 if (FRAME_WINDOW_P (f)
11179 ?
11180 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11181 || defined (HAVE_NS) || defined (USE_GTK)
11182 FRAME_EXTERNAL_MENU_BAR (f)
11183 #else
11184 FRAME_MENU_BAR_LINES (f) > 0
11185 #endif
11186 : FRAME_MENU_BAR_LINES (f) > 0)
11187 {
11188 /* If the user has switched buffers or windows, we need to
11189 recompute to reflect the new bindings. But we'll
11190 recompute when update_mode_lines is set too; that means
11191 that people can use force-mode-line-update to request
11192 that the menu bar be recomputed. The adverse effect on
11193 the rest of the redisplay algorithm is about the same as
11194 windows_or_buffers_changed anyway. */
11195 if (windows_or_buffers_changed
11196 /* This used to test w->update_mode_line, but we believe
11197 there is no need to recompute the menu in that case. */
11198 || update_mode_lines
11199 || window_buffer_changed (w))
11200 {
11201 struct buffer *prev = current_buffer;
11202 ptrdiff_t count = SPECPDL_INDEX ();
11203
11204 specbind (Qinhibit_menubar_update, Qt);
11205
11206 set_buffer_internal_1 (XBUFFER (w->buffer));
11207 if (save_match_data)
11208 record_unwind_save_match_data ();
11209 if (NILP (Voverriding_local_map_menu_flag))
11210 {
11211 specbind (Qoverriding_terminal_local_map, Qnil);
11212 specbind (Qoverriding_local_map, Qnil);
11213 }
11214
11215 if (!hooks_run)
11216 {
11217 /* Run the Lucid hook. */
11218 safe_run_hooks (Qactivate_menubar_hook);
11219
11220 /* If it has changed current-menubar from previous value,
11221 really recompute the menu-bar from the value. */
11222 if (! NILP (Vlucid_menu_bar_dirty_flag))
11223 call0 (Qrecompute_lucid_menubar);
11224
11225 safe_run_hooks (Qmenu_bar_update_hook);
11226
11227 hooks_run = 1;
11228 }
11229
11230 XSETFRAME (Vmenu_updating_frame, f);
11231 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11232
11233 /* Redisplay the menu bar in case we changed it. */
11234 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11235 || defined (HAVE_NS) || defined (USE_GTK)
11236 if (FRAME_WINDOW_P (f))
11237 {
11238 #if defined (HAVE_NS)
11239 /* All frames on Mac OS share the same menubar. So only
11240 the selected frame should be allowed to set it. */
11241 if (f == SELECTED_FRAME ())
11242 #endif
11243 set_frame_menubar (f, 0, 0);
11244 }
11245 else
11246 /* On a terminal screen, the menu bar is an ordinary screen
11247 line, and this makes it get updated. */
11248 w->update_mode_line = 1;
11249 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11250 /* In the non-toolkit version, the menu bar is an ordinary screen
11251 line, and this makes it get updated. */
11252 w->update_mode_line = 1;
11253 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11254
11255 unbind_to (count, Qnil);
11256 set_buffer_internal_1 (prev);
11257 }
11258 }
11259
11260 return hooks_run;
11261 }
11262
11263
11264 \f
11265 /***********************************************************************
11266 Output Cursor
11267 ***********************************************************************/
11268
11269 #ifdef HAVE_WINDOW_SYSTEM
11270
11271 /* EXPORT:
11272 Nominal cursor position -- where to draw output.
11273 HPOS and VPOS are window relative glyph matrix coordinates.
11274 X and Y are window relative pixel coordinates. */
11275
11276 struct cursor_pos output_cursor;
11277
11278
11279 /* EXPORT:
11280 Set the global variable output_cursor to CURSOR. All cursor
11281 positions are relative to updated_window. */
11282
11283 void
11284 set_output_cursor (struct cursor_pos *cursor)
11285 {
11286 output_cursor.hpos = cursor->hpos;
11287 output_cursor.vpos = cursor->vpos;
11288 output_cursor.x = cursor->x;
11289 output_cursor.y = cursor->y;
11290 }
11291
11292
11293 /* EXPORT for RIF:
11294 Set a nominal cursor position.
11295
11296 HPOS and VPOS are column/row positions in a window glyph matrix. X
11297 and Y are window text area relative pixel positions.
11298
11299 If this is done during an update, updated_window will contain the
11300 window that is being updated and the position is the future output
11301 cursor position for that window. If updated_window is null, use
11302 selected_window and display the cursor at the given position. */
11303
11304 void
11305 x_cursor_to (int vpos, int hpos, int y, int x)
11306 {
11307 struct window *w;
11308
11309 /* If updated_window is not set, work on selected_window. */
11310 if (updated_window)
11311 w = updated_window;
11312 else
11313 w = XWINDOW (selected_window);
11314
11315 /* Set the output cursor. */
11316 output_cursor.hpos = hpos;
11317 output_cursor.vpos = vpos;
11318 output_cursor.x = x;
11319 output_cursor.y = y;
11320
11321 /* If not called as part of an update, really display the cursor.
11322 This will also set the cursor position of W. */
11323 if (updated_window == NULL)
11324 {
11325 block_input ();
11326 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11327 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11328 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11329 unblock_input ();
11330 }
11331 }
11332
11333 #endif /* HAVE_WINDOW_SYSTEM */
11334
11335 \f
11336 /***********************************************************************
11337 Tool-bars
11338 ***********************************************************************/
11339
11340 #ifdef HAVE_WINDOW_SYSTEM
11341
11342 /* Where the mouse was last time we reported a mouse event. */
11343
11344 FRAME_PTR last_mouse_frame;
11345
11346 /* Tool-bar item index of the item on which a mouse button was pressed
11347 or -1. */
11348
11349 int last_tool_bar_item;
11350
11351 /* Select `frame' temporarily without running all the code in
11352 do_switch_frame.
11353 FIXME: Maybe do_switch_frame should be trimmed down similarly
11354 when `norecord' is set. */
11355 static Lisp_Object
11356 fast_set_selected_frame (Lisp_Object frame)
11357 {
11358 if (!EQ (selected_frame, frame))
11359 {
11360 selected_frame = frame;
11361 selected_window = XFRAME (frame)->selected_window;
11362 }
11363 return Qnil;
11364 }
11365
11366 /* Update the tool-bar item list for frame F. This has to be done
11367 before we start to fill in any display lines. Called from
11368 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11369 and restore it here. */
11370
11371 static void
11372 update_tool_bar (struct frame *f, int save_match_data)
11373 {
11374 #if defined (USE_GTK) || defined (HAVE_NS)
11375 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11376 #else
11377 int do_update = WINDOWP (f->tool_bar_window)
11378 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11379 #endif
11380
11381 if (do_update)
11382 {
11383 Lisp_Object window;
11384 struct window *w;
11385
11386 window = FRAME_SELECTED_WINDOW (f);
11387 w = XWINDOW (window);
11388
11389 /* If the user has switched buffers or windows, we need to
11390 recompute to reflect the new bindings. But we'll
11391 recompute when update_mode_lines is set too; that means
11392 that people can use force-mode-line-update to request
11393 that the menu bar be recomputed. The adverse effect on
11394 the rest of the redisplay algorithm is about the same as
11395 windows_or_buffers_changed anyway. */
11396 if (windows_or_buffers_changed
11397 || w->update_mode_line
11398 || update_mode_lines
11399 || window_buffer_changed (w))
11400 {
11401 struct buffer *prev = current_buffer;
11402 ptrdiff_t count = SPECPDL_INDEX ();
11403 Lisp_Object frame, new_tool_bar;
11404 int new_n_tool_bar;
11405 struct gcpro gcpro1;
11406
11407 /* Set current_buffer to the buffer of the selected
11408 window of the frame, so that we get the right local
11409 keymaps. */
11410 set_buffer_internal_1 (XBUFFER (w->buffer));
11411
11412 /* Save match data, if we must. */
11413 if (save_match_data)
11414 record_unwind_save_match_data ();
11415
11416 /* Make sure that we don't accidentally use bogus keymaps. */
11417 if (NILP (Voverriding_local_map_menu_flag))
11418 {
11419 specbind (Qoverriding_terminal_local_map, Qnil);
11420 specbind (Qoverriding_local_map, Qnil);
11421 }
11422
11423 GCPRO1 (new_tool_bar);
11424
11425 /* We must temporarily set the selected frame to this frame
11426 before calling tool_bar_items, because the calculation of
11427 the tool-bar keymap uses the selected frame (see
11428 `tool-bar-make-keymap' in tool-bar.el). */
11429 eassert (EQ (selected_window,
11430 /* Since we only explicitly preserve selected_frame,
11431 check that selected_window would be redundant. */
11432 XFRAME (selected_frame)->selected_window));
11433 record_unwind_protect (fast_set_selected_frame, selected_frame);
11434 XSETFRAME (frame, f);
11435 fast_set_selected_frame (frame);
11436
11437 /* Build desired tool-bar items from keymaps. */
11438 new_tool_bar
11439 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11440 &new_n_tool_bar);
11441
11442 /* Redisplay the tool-bar if we changed it. */
11443 if (new_n_tool_bar != f->n_tool_bar_items
11444 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11445 {
11446 /* Redisplay that happens asynchronously due to an expose event
11447 may access f->tool_bar_items. Make sure we update both
11448 variables within BLOCK_INPUT so no such event interrupts. */
11449 block_input ();
11450 fset_tool_bar_items (f, new_tool_bar);
11451 f->n_tool_bar_items = new_n_tool_bar;
11452 w->update_mode_line = 1;
11453 unblock_input ();
11454 }
11455
11456 UNGCPRO;
11457
11458 unbind_to (count, Qnil);
11459 set_buffer_internal_1 (prev);
11460 }
11461 }
11462 }
11463
11464
11465 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11466 F's desired tool-bar contents. F->tool_bar_items must have
11467 been set up previously by calling prepare_menu_bars. */
11468
11469 static void
11470 build_desired_tool_bar_string (struct frame *f)
11471 {
11472 int i, size, size_needed;
11473 struct gcpro gcpro1, gcpro2, gcpro3;
11474 Lisp_Object image, plist, props;
11475
11476 image = plist = props = Qnil;
11477 GCPRO3 (image, plist, props);
11478
11479 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11480 Otherwise, make a new string. */
11481
11482 /* The size of the string we might be able to reuse. */
11483 size = (STRINGP (f->desired_tool_bar_string)
11484 ? SCHARS (f->desired_tool_bar_string)
11485 : 0);
11486
11487 /* We need one space in the string for each image. */
11488 size_needed = f->n_tool_bar_items;
11489
11490 /* Reuse f->desired_tool_bar_string, if possible. */
11491 if (size < size_needed || NILP (f->desired_tool_bar_string))
11492 fset_desired_tool_bar_string
11493 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11494 else
11495 {
11496 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11497 Fremove_text_properties (make_number (0), make_number (size),
11498 props, f->desired_tool_bar_string);
11499 }
11500
11501 /* Put a `display' property on the string for the images to display,
11502 put a `menu_item' property on tool-bar items with a value that
11503 is the index of the item in F's tool-bar item vector. */
11504 for (i = 0; i < f->n_tool_bar_items; ++i)
11505 {
11506 #define PROP(IDX) \
11507 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11508
11509 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11510 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11511 int hmargin, vmargin, relief, idx, end;
11512
11513 /* If image is a vector, choose the image according to the
11514 button state. */
11515 image = PROP (TOOL_BAR_ITEM_IMAGES);
11516 if (VECTORP (image))
11517 {
11518 if (enabled_p)
11519 idx = (selected_p
11520 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11521 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11522 else
11523 idx = (selected_p
11524 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11525 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11526
11527 eassert (ASIZE (image) >= idx);
11528 image = AREF (image, idx);
11529 }
11530 else
11531 idx = -1;
11532
11533 /* Ignore invalid image specifications. */
11534 if (!valid_image_p (image))
11535 continue;
11536
11537 /* Display the tool-bar button pressed, or depressed. */
11538 plist = Fcopy_sequence (XCDR (image));
11539
11540 /* Compute margin and relief to draw. */
11541 relief = (tool_bar_button_relief >= 0
11542 ? tool_bar_button_relief
11543 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11544 hmargin = vmargin = relief;
11545
11546 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11547 INT_MAX - max (hmargin, vmargin)))
11548 {
11549 hmargin += XFASTINT (Vtool_bar_button_margin);
11550 vmargin += XFASTINT (Vtool_bar_button_margin);
11551 }
11552 else if (CONSP (Vtool_bar_button_margin))
11553 {
11554 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11555 INT_MAX - hmargin))
11556 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11557
11558 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11559 INT_MAX - vmargin))
11560 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11561 }
11562
11563 if (auto_raise_tool_bar_buttons_p)
11564 {
11565 /* Add a `:relief' property to the image spec if the item is
11566 selected. */
11567 if (selected_p)
11568 {
11569 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11570 hmargin -= relief;
11571 vmargin -= relief;
11572 }
11573 }
11574 else
11575 {
11576 /* If image is selected, display it pressed, i.e. with a
11577 negative relief. If it's not selected, display it with a
11578 raised relief. */
11579 plist = Fplist_put (plist, QCrelief,
11580 (selected_p
11581 ? make_number (-relief)
11582 : make_number (relief)));
11583 hmargin -= relief;
11584 vmargin -= relief;
11585 }
11586
11587 /* Put a margin around the image. */
11588 if (hmargin || vmargin)
11589 {
11590 if (hmargin == vmargin)
11591 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11592 else
11593 plist = Fplist_put (plist, QCmargin,
11594 Fcons (make_number (hmargin),
11595 make_number (vmargin)));
11596 }
11597
11598 /* If button is not enabled, and we don't have special images
11599 for the disabled state, make the image appear disabled by
11600 applying an appropriate algorithm to it. */
11601 if (!enabled_p && idx < 0)
11602 plist = Fplist_put (plist, QCconversion, Qdisabled);
11603
11604 /* Put a `display' text property on the string for the image to
11605 display. Put a `menu-item' property on the string that gives
11606 the start of this item's properties in the tool-bar items
11607 vector. */
11608 image = Fcons (Qimage, plist);
11609 props = list4 (Qdisplay, image,
11610 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11611
11612 /* Let the last image hide all remaining spaces in the tool bar
11613 string. The string can be longer than needed when we reuse a
11614 previous string. */
11615 if (i + 1 == f->n_tool_bar_items)
11616 end = SCHARS (f->desired_tool_bar_string);
11617 else
11618 end = i + 1;
11619 Fadd_text_properties (make_number (i), make_number (end),
11620 props, f->desired_tool_bar_string);
11621 #undef PROP
11622 }
11623
11624 UNGCPRO;
11625 }
11626
11627
11628 /* Display one line of the tool-bar of frame IT->f.
11629
11630 HEIGHT specifies the desired height of the tool-bar line.
11631 If the actual height of the glyph row is less than HEIGHT, the
11632 row's height is increased to HEIGHT, and the icons are centered
11633 vertically in the new height.
11634
11635 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11636 count a final empty row in case the tool-bar width exactly matches
11637 the window width.
11638 */
11639
11640 static void
11641 display_tool_bar_line (struct it *it, int height)
11642 {
11643 struct glyph_row *row = it->glyph_row;
11644 int max_x = it->last_visible_x;
11645 struct glyph *last;
11646
11647 prepare_desired_row (row);
11648 row->y = it->current_y;
11649
11650 /* Note that this isn't made use of if the face hasn't a box,
11651 so there's no need to check the face here. */
11652 it->start_of_box_run_p = 1;
11653
11654 while (it->current_x < max_x)
11655 {
11656 int x, n_glyphs_before, i, nglyphs;
11657 struct it it_before;
11658
11659 /* Get the next display element. */
11660 if (!get_next_display_element (it))
11661 {
11662 /* Don't count empty row if we are counting needed tool-bar lines. */
11663 if (height < 0 && !it->hpos)
11664 return;
11665 break;
11666 }
11667
11668 /* Produce glyphs. */
11669 n_glyphs_before = row->used[TEXT_AREA];
11670 it_before = *it;
11671
11672 PRODUCE_GLYPHS (it);
11673
11674 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11675 i = 0;
11676 x = it_before.current_x;
11677 while (i < nglyphs)
11678 {
11679 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11680
11681 if (x + glyph->pixel_width > max_x)
11682 {
11683 /* Glyph doesn't fit on line. Backtrack. */
11684 row->used[TEXT_AREA] = n_glyphs_before;
11685 *it = it_before;
11686 /* If this is the only glyph on this line, it will never fit on the
11687 tool-bar, so skip it. But ensure there is at least one glyph,
11688 so we don't accidentally disable the tool-bar. */
11689 if (n_glyphs_before == 0
11690 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11691 break;
11692 goto out;
11693 }
11694
11695 ++it->hpos;
11696 x += glyph->pixel_width;
11697 ++i;
11698 }
11699
11700 /* Stop at line end. */
11701 if (ITERATOR_AT_END_OF_LINE_P (it))
11702 break;
11703
11704 set_iterator_to_next (it, 1);
11705 }
11706
11707 out:;
11708
11709 row->displays_text_p = row->used[TEXT_AREA] != 0;
11710
11711 /* Use default face for the border below the tool bar.
11712
11713 FIXME: When auto-resize-tool-bars is grow-only, there is
11714 no additional border below the possibly empty tool-bar lines.
11715 So to make the extra empty lines look "normal", we have to
11716 use the tool-bar face for the border too. */
11717 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11718 it->face_id = DEFAULT_FACE_ID;
11719
11720 extend_face_to_end_of_line (it);
11721 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11722 last->right_box_line_p = 1;
11723 if (last == row->glyphs[TEXT_AREA])
11724 last->left_box_line_p = 1;
11725
11726 /* Make line the desired height and center it vertically. */
11727 if ((height -= it->max_ascent + it->max_descent) > 0)
11728 {
11729 /* Don't add more than one line height. */
11730 height %= FRAME_LINE_HEIGHT (it->f);
11731 it->max_ascent += height / 2;
11732 it->max_descent += (height + 1) / 2;
11733 }
11734
11735 compute_line_metrics (it);
11736
11737 /* If line is empty, make it occupy the rest of the tool-bar. */
11738 if (!row->displays_text_p)
11739 {
11740 row->height = row->phys_height = it->last_visible_y - row->y;
11741 row->visible_height = row->height;
11742 row->ascent = row->phys_ascent = 0;
11743 row->extra_line_spacing = 0;
11744 }
11745
11746 row->full_width_p = 1;
11747 row->continued_p = 0;
11748 row->truncated_on_left_p = 0;
11749 row->truncated_on_right_p = 0;
11750
11751 it->current_x = it->hpos = 0;
11752 it->current_y += row->height;
11753 ++it->vpos;
11754 ++it->glyph_row;
11755 }
11756
11757
11758 /* Max tool-bar height. */
11759
11760 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11761 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11762
11763 /* Value is the number of screen lines needed to make all tool-bar
11764 items of frame F visible. The number of actual rows needed is
11765 returned in *N_ROWS if non-NULL. */
11766
11767 static int
11768 tool_bar_lines_needed (struct frame *f, int *n_rows)
11769 {
11770 struct window *w = XWINDOW (f->tool_bar_window);
11771 struct it it;
11772 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11773 the desired matrix, so use (unused) mode-line row as temporary row to
11774 avoid destroying the first tool-bar row. */
11775 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11776
11777 /* Initialize an iterator for iteration over
11778 F->desired_tool_bar_string in the tool-bar window of frame F. */
11779 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11780 it.first_visible_x = 0;
11781 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11782 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11783 it.paragraph_embedding = L2R;
11784
11785 while (!ITERATOR_AT_END_P (&it))
11786 {
11787 clear_glyph_row (temp_row);
11788 it.glyph_row = temp_row;
11789 display_tool_bar_line (&it, -1);
11790 }
11791 clear_glyph_row (temp_row);
11792
11793 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11794 if (n_rows)
11795 *n_rows = it.vpos > 0 ? it.vpos : -1;
11796
11797 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11798 }
11799
11800
11801 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11802 0, 1, 0,
11803 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11804 If FRAME is nil or omitted, use the selected frame. */)
11805 (Lisp_Object frame)
11806 {
11807 struct frame *f = decode_any_frame (frame);
11808 struct window *w;
11809 int nlines = 0;
11810
11811 if (WINDOWP (f->tool_bar_window)
11812 && (w = XWINDOW (f->tool_bar_window),
11813 WINDOW_TOTAL_LINES (w) > 0))
11814 {
11815 update_tool_bar (f, 1);
11816 if (f->n_tool_bar_items)
11817 {
11818 build_desired_tool_bar_string (f);
11819 nlines = tool_bar_lines_needed (f, NULL);
11820 }
11821 }
11822
11823 return make_number (nlines);
11824 }
11825
11826
11827 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11828 height should be changed. */
11829
11830 static int
11831 redisplay_tool_bar (struct frame *f)
11832 {
11833 struct window *w;
11834 struct it it;
11835 struct glyph_row *row;
11836
11837 #if defined (USE_GTK) || defined (HAVE_NS)
11838 if (FRAME_EXTERNAL_TOOL_BAR (f))
11839 update_frame_tool_bar (f);
11840 return 0;
11841 #endif
11842
11843 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11844 do anything. This means you must start with tool-bar-lines
11845 non-zero to get the auto-sizing effect. Or in other words, you
11846 can turn off tool-bars by specifying tool-bar-lines zero. */
11847 if (!WINDOWP (f->tool_bar_window)
11848 || (w = XWINDOW (f->tool_bar_window),
11849 WINDOW_TOTAL_LINES (w) == 0))
11850 return 0;
11851
11852 /* Set up an iterator for the tool-bar window. */
11853 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11854 it.first_visible_x = 0;
11855 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11856 row = it.glyph_row;
11857
11858 /* Build a string that represents the contents of the tool-bar. */
11859 build_desired_tool_bar_string (f);
11860 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11861 /* FIXME: This should be controlled by a user option. But it
11862 doesn't make sense to have an R2L tool bar if the menu bar cannot
11863 be drawn also R2L, and making the menu bar R2L is tricky due
11864 toolkit-specific code that implements it. If an R2L tool bar is
11865 ever supported, display_tool_bar_line should also be augmented to
11866 call unproduce_glyphs like display_line and display_string
11867 do. */
11868 it.paragraph_embedding = L2R;
11869
11870 if (f->n_tool_bar_rows == 0)
11871 {
11872 int nlines;
11873
11874 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11875 nlines != WINDOW_TOTAL_LINES (w)))
11876 {
11877 Lisp_Object frame;
11878 int old_height = WINDOW_TOTAL_LINES (w);
11879
11880 XSETFRAME (frame, f);
11881 Fmodify_frame_parameters (frame,
11882 Fcons (Fcons (Qtool_bar_lines,
11883 make_number (nlines)),
11884 Qnil));
11885 if (WINDOW_TOTAL_LINES (w) != old_height)
11886 {
11887 clear_glyph_matrix (w->desired_matrix);
11888 fonts_changed_p = 1;
11889 return 1;
11890 }
11891 }
11892 }
11893
11894 /* Display as many lines as needed to display all tool-bar items. */
11895
11896 if (f->n_tool_bar_rows > 0)
11897 {
11898 int border, rows, height, extra;
11899
11900 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11901 border = XINT (Vtool_bar_border);
11902 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11903 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11904 else if (EQ (Vtool_bar_border, Qborder_width))
11905 border = f->border_width;
11906 else
11907 border = 0;
11908 if (border < 0)
11909 border = 0;
11910
11911 rows = f->n_tool_bar_rows;
11912 height = max (1, (it.last_visible_y - border) / rows);
11913 extra = it.last_visible_y - border - height * rows;
11914
11915 while (it.current_y < it.last_visible_y)
11916 {
11917 int h = 0;
11918 if (extra > 0 && rows-- > 0)
11919 {
11920 h = (extra + rows - 1) / rows;
11921 extra -= h;
11922 }
11923 display_tool_bar_line (&it, height + h);
11924 }
11925 }
11926 else
11927 {
11928 while (it.current_y < it.last_visible_y)
11929 display_tool_bar_line (&it, 0);
11930 }
11931
11932 /* It doesn't make much sense to try scrolling in the tool-bar
11933 window, so don't do it. */
11934 w->desired_matrix->no_scrolling_p = 1;
11935 w->must_be_updated_p = 1;
11936
11937 if (!NILP (Vauto_resize_tool_bars))
11938 {
11939 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11940 int change_height_p = 0;
11941
11942 /* If we couldn't display everything, change the tool-bar's
11943 height if there is room for more. */
11944 if (IT_STRING_CHARPOS (it) < it.end_charpos
11945 && it.current_y < max_tool_bar_height)
11946 change_height_p = 1;
11947
11948 row = it.glyph_row - 1;
11949
11950 /* If there are blank lines at the end, except for a partially
11951 visible blank line at the end that is smaller than
11952 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11953 if (!row->displays_text_p
11954 && row->height >= FRAME_LINE_HEIGHT (f))
11955 change_height_p = 1;
11956
11957 /* If row displays tool-bar items, but is partially visible,
11958 change the tool-bar's height. */
11959 if (row->displays_text_p
11960 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
11961 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
11962 change_height_p = 1;
11963
11964 /* Resize windows as needed by changing the `tool-bar-lines'
11965 frame parameter. */
11966 if (change_height_p)
11967 {
11968 Lisp_Object frame;
11969 int old_height = WINDOW_TOTAL_LINES (w);
11970 int nrows;
11971 int nlines = tool_bar_lines_needed (f, &nrows);
11972
11973 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
11974 && !f->minimize_tool_bar_window_p)
11975 ? (nlines > old_height)
11976 : (nlines != old_height));
11977 f->minimize_tool_bar_window_p = 0;
11978
11979 if (change_height_p)
11980 {
11981 XSETFRAME (frame, f);
11982 Fmodify_frame_parameters (frame,
11983 Fcons (Fcons (Qtool_bar_lines,
11984 make_number (nlines)),
11985 Qnil));
11986 if (WINDOW_TOTAL_LINES (w) != old_height)
11987 {
11988 clear_glyph_matrix (w->desired_matrix);
11989 f->n_tool_bar_rows = nrows;
11990 fonts_changed_p = 1;
11991 return 1;
11992 }
11993 }
11994 }
11995 }
11996
11997 f->minimize_tool_bar_window_p = 0;
11998 return 0;
11999 }
12000
12001
12002 /* Get information about the tool-bar item which is displayed in GLYPH
12003 on frame F. Return in *PROP_IDX the index where tool-bar item
12004 properties start in F->tool_bar_items. Value is zero if
12005 GLYPH doesn't display a tool-bar item. */
12006
12007 static int
12008 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12009 {
12010 Lisp_Object prop;
12011 int success_p;
12012 int charpos;
12013
12014 /* This function can be called asynchronously, which means we must
12015 exclude any possibility that Fget_text_property signals an
12016 error. */
12017 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12018 charpos = max (0, charpos);
12019
12020 /* Get the text property `menu-item' at pos. The value of that
12021 property is the start index of this item's properties in
12022 F->tool_bar_items. */
12023 prop = Fget_text_property (make_number (charpos),
12024 Qmenu_item, f->current_tool_bar_string);
12025 if (INTEGERP (prop))
12026 {
12027 *prop_idx = XINT (prop);
12028 success_p = 1;
12029 }
12030 else
12031 success_p = 0;
12032
12033 return success_p;
12034 }
12035
12036 \f
12037 /* Get information about the tool-bar item at position X/Y on frame F.
12038 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12039 the current matrix of the tool-bar window of F, or NULL if not
12040 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12041 item in F->tool_bar_items. Value is
12042
12043 -1 if X/Y is not on a tool-bar item
12044 0 if X/Y is on the same item that was highlighted before.
12045 1 otherwise. */
12046
12047 static int
12048 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12049 int *hpos, int *vpos, int *prop_idx)
12050 {
12051 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12052 struct window *w = XWINDOW (f->tool_bar_window);
12053 int area;
12054
12055 /* Find the glyph under X/Y. */
12056 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12057 if (*glyph == NULL)
12058 return -1;
12059
12060 /* Get the start of this tool-bar item's properties in
12061 f->tool_bar_items. */
12062 if (!tool_bar_item_info (f, *glyph, prop_idx))
12063 return -1;
12064
12065 /* Is mouse on the highlighted item? */
12066 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12067 && *vpos >= hlinfo->mouse_face_beg_row
12068 && *vpos <= hlinfo->mouse_face_end_row
12069 && (*vpos > hlinfo->mouse_face_beg_row
12070 || *hpos >= hlinfo->mouse_face_beg_col)
12071 && (*vpos < hlinfo->mouse_face_end_row
12072 || *hpos < hlinfo->mouse_face_end_col
12073 || hlinfo->mouse_face_past_end))
12074 return 0;
12075
12076 return 1;
12077 }
12078
12079
12080 /* EXPORT:
12081 Handle mouse button event on the tool-bar of frame F, at
12082 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12083 0 for button release. MODIFIERS is event modifiers for button
12084 release. */
12085
12086 void
12087 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12088 int modifiers)
12089 {
12090 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12091 struct window *w = XWINDOW (f->tool_bar_window);
12092 int hpos, vpos, prop_idx;
12093 struct glyph *glyph;
12094 Lisp_Object enabled_p;
12095
12096 /* If not on the highlighted tool-bar item, return. */
12097 frame_to_window_pixel_xy (w, &x, &y);
12098 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12099 return;
12100
12101 /* If item is disabled, do nothing. */
12102 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12103 if (NILP (enabled_p))
12104 return;
12105
12106 if (down_p)
12107 {
12108 /* Show item in pressed state. */
12109 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12110 last_tool_bar_item = prop_idx;
12111 }
12112 else
12113 {
12114 Lisp_Object key, frame;
12115 struct input_event event;
12116 EVENT_INIT (event);
12117
12118 /* Show item in released state. */
12119 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12120
12121 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12122
12123 XSETFRAME (frame, f);
12124 event.kind = TOOL_BAR_EVENT;
12125 event.frame_or_window = frame;
12126 event.arg = frame;
12127 kbd_buffer_store_event (&event);
12128
12129 event.kind = TOOL_BAR_EVENT;
12130 event.frame_or_window = frame;
12131 event.arg = key;
12132 event.modifiers = modifiers;
12133 kbd_buffer_store_event (&event);
12134 last_tool_bar_item = -1;
12135 }
12136 }
12137
12138
12139 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12140 tool-bar window-relative coordinates X/Y. Called from
12141 note_mouse_highlight. */
12142
12143 static void
12144 note_tool_bar_highlight (struct frame *f, int x, int y)
12145 {
12146 Lisp_Object window = f->tool_bar_window;
12147 struct window *w = XWINDOW (window);
12148 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12149 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12150 int hpos, vpos;
12151 struct glyph *glyph;
12152 struct glyph_row *row;
12153 int i;
12154 Lisp_Object enabled_p;
12155 int prop_idx;
12156 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12157 int mouse_down_p, rc;
12158
12159 /* Function note_mouse_highlight is called with negative X/Y
12160 values when mouse moves outside of the frame. */
12161 if (x <= 0 || y <= 0)
12162 {
12163 clear_mouse_face (hlinfo);
12164 return;
12165 }
12166
12167 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12168 if (rc < 0)
12169 {
12170 /* Not on tool-bar item. */
12171 clear_mouse_face (hlinfo);
12172 return;
12173 }
12174 else if (rc == 0)
12175 /* On same tool-bar item as before. */
12176 goto set_help_echo;
12177
12178 clear_mouse_face (hlinfo);
12179
12180 /* Mouse is down, but on different tool-bar item? */
12181 mouse_down_p = (dpyinfo->grabbed
12182 && f == last_mouse_frame
12183 && FRAME_LIVE_P (f));
12184 if (mouse_down_p
12185 && last_tool_bar_item != prop_idx)
12186 return;
12187
12188 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12189
12190 /* If tool-bar item is not enabled, don't highlight it. */
12191 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12192 if (!NILP (enabled_p))
12193 {
12194 /* Compute the x-position of the glyph. In front and past the
12195 image is a space. We include this in the highlighted area. */
12196 row = MATRIX_ROW (w->current_matrix, vpos);
12197 for (i = x = 0; i < hpos; ++i)
12198 x += row->glyphs[TEXT_AREA][i].pixel_width;
12199
12200 /* Record this as the current active region. */
12201 hlinfo->mouse_face_beg_col = hpos;
12202 hlinfo->mouse_face_beg_row = vpos;
12203 hlinfo->mouse_face_beg_x = x;
12204 hlinfo->mouse_face_beg_y = row->y;
12205 hlinfo->mouse_face_past_end = 0;
12206
12207 hlinfo->mouse_face_end_col = hpos + 1;
12208 hlinfo->mouse_face_end_row = vpos;
12209 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12210 hlinfo->mouse_face_end_y = row->y;
12211 hlinfo->mouse_face_window = window;
12212 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12213
12214 /* Display it as active. */
12215 show_mouse_face (hlinfo, draw);
12216 }
12217
12218 set_help_echo:
12219
12220 /* Set help_echo_string to a help string to display for this tool-bar item.
12221 XTread_socket does the rest. */
12222 help_echo_object = help_echo_window = Qnil;
12223 help_echo_pos = -1;
12224 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12225 if (NILP (help_echo_string))
12226 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12227 }
12228
12229 #endif /* HAVE_WINDOW_SYSTEM */
12230
12231
12232 \f
12233 /************************************************************************
12234 Horizontal scrolling
12235 ************************************************************************/
12236
12237 static int hscroll_window_tree (Lisp_Object);
12238 static int hscroll_windows (Lisp_Object);
12239
12240 /* For all leaf windows in the window tree rooted at WINDOW, set their
12241 hscroll value so that PT is (i) visible in the window, and (ii) so
12242 that it is not within a certain margin at the window's left and
12243 right border. Value is non-zero if any window's hscroll has been
12244 changed. */
12245
12246 static int
12247 hscroll_window_tree (Lisp_Object window)
12248 {
12249 int hscrolled_p = 0;
12250 int hscroll_relative_p = FLOATP (Vhscroll_step);
12251 int hscroll_step_abs = 0;
12252 double hscroll_step_rel = 0;
12253
12254 if (hscroll_relative_p)
12255 {
12256 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12257 if (hscroll_step_rel < 0)
12258 {
12259 hscroll_relative_p = 0;
12260 hscroll_step_abs = 0;
12261 }
12262 }
12263 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12264 {
12265 hscroll_step_abs = XINT (Vhscroll_step);
12266 if (hscroll_step_abs < 0)
12267 hscroll_step_abs = 0;
12268 }
12269 else
12270 hscroll_step_abs = 0;
12271
12272 while (WINDOWP (window))
12273 {
12274 struct window *w = XWINDOW (window);
12275
12276 if (WINDOWP (w->hchild))
12277 hscrolled_p |= hscroll_window_tree (w->hchild);
12278 else if (WINDOWP (w->vchild))
12279 hscrolled_p |= hscroll_window_tree (w->vchild);
12280 else if (w->cursor.vpos >= 0)
12281 {
12282 int h_margin;
12283 int text_area_width;
12284 struct glyph_row *current_cursor_row
12285 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12286 struct glyph_row *desired_cursor_row
12287 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12288 struct glyph_row *cursor_row
12289 = (desired_cursor_row->enabled_p
12290 ? desired_cursor_row
12291 : current_cursor_row);
12292 int row_r2l_p = cursor_row->reversed_p;
12293
12294 text_area_width = window_box_width (w, TEXT_AREA);
12295
12296 /* Scroll when cursor is inside this scroll margin. */
12297 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12298
12299 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12300 /* For left-to-right rows, hscroll when cursor is either
12301 (i) inside the right hscroll margin, or (ii) if it is
12302 inside the left margin and the window is already
12303 hscrolled. */
12304 && ((!row_r2l_p
12305 && ((w->hscroll
12306 && w->cursor.x <= h_margin)
12307 || (cursor_row->enabled_p
12308 && cursor_row->truncated_on_right_p
12309 && (w->cursor.x >= text_area_width - h_margin))))
12310 /* For right-to-left rows, the logic is similar,
12311 except that rules for scrolling to left and right
12312 are reversed. E.g., if cursor.x <= h_margin, we
12313 need to hscroll "to the right" unconditionally,
12314 and that will scroll the screen to the left so as
12315 to reveal the next portion of the row. */
12316 || (row_r2l_p
12317 && ((cursor_row->enabled_p
12318 /* FIXME: It is confusing to set the
12319 truncated_on_right_p flag when R2L rows
12320 are actually truncated on the left. */
12321 && cursor_row->truncated_on_right_p
12322 && w->cursor.x <= h_margin)
12323 || (w->hscroll
12324 && (w->cursor.x >= text_area_width - h_margin))))))
12325 {
12326 struct it it;
12327 ptrdiff_t hscroll;
12328 struct buffer *saved_current_buffer;
12329 ptrdiff_t pt;
12330 int wanted_x;
12331
12332 /* Find point in a display of infinite width. */
12333 saved_current_buffer = current_buffer;
12334 current_buffer = XBUFFER (w->buffer);
12335
12336 if (w == XWINDOW (selected_window))
12337 pt = PT;
12338 else
12339 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12340
12341 /* Move iterator to pt starting at cursor_row->start in
12342 a line with infinite width. */
12343 init_to_row_start (&it, w, cursor_row);
12344 it.last_visible_x = INFINITY;
12345 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12346 current_buffer = saved_current_buffer;
12347
12348 /* Position cursor in window. */
12349 if (!hscroll_relative_p && hscroll_step_abs == 0)
12350 hscroll = max (0, (it.current_x
12351 - (ITERATOR_AT_END_OF_LINE_P (&it)
12352 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12353 : (text_area_width / 2))))
12354 / FRAME_COLUMN_WIDTH (it.f);
12355 else if ((!row_r2l_p
12356 && w->cursor.x >= text_area_width - h_margin)
12357 || (row_r2l_p && w->cursor.x <= h_margin))
12358 {
12359 if (hscroll_relative_p)
12360 wanted_x = text_area_width * (1 - hscroll_step_rel)
12361 - h_margin;
12362 else
12363 wanted_x = text_area_width
12364 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12365 - h_margin;
12366 hscroll
12367 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12368 }
12369 else
12370 {
12371 if (hscroll_relative_p)
12372 wanted_x = text_area_width * hscroll_step_rel
12373 + h_margin;
12374 else
12375 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12376 + h_margin;
12377 hscroll
12378 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12379 }
12380 hscroll = max (hscroll, w->min_hscroll);
12381
12382 /* Don't prevent redisplay optimizations if hscroll
12383 hasn't changed, as it will unnecessarily slow down
12384 redisplay. */
12385 if (w->hscroll != hscroll)
12386 {
12387 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12388 w->hscroll = hscroll;
12389 hscrolled_p = 1;
12390 }
12391 }
12392 }
12393
12394 window = w->next;
12395 }
12396
12397 /* Value is non-zero if hscroll of any leaf window has been changed. */
12398 return hscrolled_p;
12399 }
12400
12401
12402 /* Set hscroll so that cursor is visible and not inside horizontal
12403 scroll margins for all windows in the tree rooted at WINDOW. See
12404 also hscroll_window_tree above. Value is non-zero if any window's
12405 hscroll has been changed. If it has, desired matrices on the frame
12406 of WINDOW are cleared. */
12407
12408 static int
12409 hscroll_windows (Lisp_Object window)
12410 {
12411 int hscrolled_p = hscroll_window_tree (window);
12412 if (hscrolled_p)
12413 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12414 return hscrolled_p;
12415 }
12416
12417
12418 \f
12419 /************************************************************************
12420 Redisplay
12421 ************************************************************************/
12422
12423 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12424 to a non-zero value. This is sometimes handy to have in a debugger
12425 session. */
12426
12427 #ifdef GLYPH_DEBUG
12428
12429 /* First and last unchanged row for try_window_id. */
12430
12431 static int debug_first_unchanged_at_end_vpos;
12432 static int debug_last_unchanged_at_beg_vpos;
12433
12434 /* Delta vpos and y. */
12435
12436 static int debug_dvpos, debug_dy;
12437
12438 /* Delta in characters and bytes for try_window_id. */
12439
12440 static ptrdiff_t debug_delta, debug_delta_bytes;
12441
12442 /* Values of window_end_pos and window_end_vpos at the end of
12443 try_window_id. */
12444
12445 static ptrdiff_t debug_end_vpos;
12446
12447 /* Append a string to W->desired_matrix->method. FMT is a printf
12448 format string. If trace_redisplay_p is non-zero also printf the
12449 resulting string to stderr. */
12450
12451 static void debug_method_add (struct window *, char const *, ...)
12452 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12453
12454 static void
12455 debug_method_add (struct window *w, char const *fmt, ...)
12456 {
12457 char *method = w->desired_matrix->method;
12458 int len = strlen (method);
12459 int size = sizeof w->desired_matrix->method;
12460 int remaining = size - len - 1;
12461 va_list ap;
12462
12463 if (len && remaining)
12464 {
12465 method[len] = '|';
12466 --remaining, ++len;
12467 }
12468
12469 va_start (ap, fmt);
12470 vsnprintf (method + len, remaining + 1, fmt, ap);
12471 va_end (ap);
12472
12473 if (trace_redisplay_p)
12474 fprintf (stderr, "%p (%s): %s\n",
12475 w,
12476 ((BUFFERP (w->buffer)
12477 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12478 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12479 : "no buffer"),
12480 method + len);
12481 }
12482
12483 #endif /* GLYPH_DEBUG */
12484
12485
12486 /* Value is non-zero if all changes in window W, which displays
12487 current_buffer, are in the text between START and END. START is a
12488 buffer position, END is given as a distance from Z. Used in
12489 redisplay_internal for display optimization. */
12490
12491 static int
12492 text_outside_line_unchanged_p (struct window *w,
12493 ptrdiff_t start, ptrdiff_t end)
12494 {
12495 int unchanged_p = 1;
12496
12497 /* If text or overlays have changed, see where. */
12498 if (window_outdated (w))
12499 {
12500 /* Gap in the line? */
12501 if (GPT < start || Z - GPT < end)
12502 unchanged_p = 0;
12503
12504 /* Changes start in front of the line, or end after it? */
12505 if (unchanged_p
12506 && (BEG_UNCHANGED < start - 1
12507 || END_UNCHANGED < end))
12508 unchanged_p = 0;
12509
12510 /* If selective display, can't optimize if changes start at the
12511 beginning of the line. */
12512 if (unchanged_p
12513 && INTEGERP (BVAR (current_buffer, selective_display))
12514 && XINT (BVAR (current_buffer, selective_display)) > 0
12515 && (BEG_UNCHANGED < start || GPT <= start))
12516 unchanged_p = 0;
12517
12518 /* If there are overlays at the start or end of the line, these
12519 may have overlay strings with newlines in them. A change at
12520 START, for instance, may actually concern the display of such
12521 overlay strings as well, and they are displayed on different
12522 lines. So, quickly rule out this case. (For the future, it
12523 might be desirable to implement something more telling than
12524 just BEG/END_UNCHANGED.) */
12525 if (unchanged_p)
12526 {
12527 if (BEG + BEG_UNCHANGED == start
12528 && overlay_touches_p (start))
12529 unchanged_p = 0;
12530 if (END_UNCHANGED == end
12531 && overlay_touches_p (Z - end))
12532 unchanged_p = 0;
12533 }
12534
12535 /* Under bidi reordering, adding or deleting a character in the
12536 beginning of a paragraph, before the first strong directional
12537 character, can change the base direction of the paragraph (unless
12538 the buffer specifies a fixed paragraph direction), which will
12539 require to redisplay the whole paragraph. It might be worthwhile
12540 to find the paragraph limits and widen the range of redisplayed
12541 lines to that, but for now just give up this optimization. */
12542 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12543 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12544 unchanged_p = 0;
12545 }
12546
12547 return unchanged_p;
12548 }
12549
12550
12551 /* Do a frame update, taking possible shortcuts into account. This is
12552 the main external entry point for redisplay.
12553
12554 If the last redisplay displayed an echo area message and that message
12555 is no longer requested, we clear the echo area or bring back the
12556 mini-buffer if that is in use. */
12557
12558 void
12559 redisplay (void)
12560 {
12561 redisplay_internal ();
12562 }
12563
12564
12565 static Lisp_Object
12566 overlay_arrow_string_or_property (Lisp_Object var)
12567 {
12568 Lisp_Object val;
12569
12570 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12571 return val;
12572
12573 return Voverlay_arrow_string;
12574 }
12575
12576 /* Return 1 if there are any overlay-arrows in current_buffer. */
12577 static int
12578 overlay_arrow_in_current_buffer_p (void)
12579 {
12580 Lisp_Object vlist;
12581
12582 for (vlist = Voverlay_arrow_variable_list;
12583 CONSP (vlist);
12584 vlist = XCDR (vlist))
12585 {
12586 Lisp_Object var = XCAR (vlist);
12587 Lisp_Object val;
12588
12589 if (!SYMBOLP (var))
12590 continue;
12591 val = find_symbol_value (var);
12592 if (MARKERP (val)
12593 && current_buffer == XMARKER (val)->buffer)
12594 return 1;
12595 }
12596 return 0;
12597 }
12598
12599
12600 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12601 has changed. */
12602
12603 static int
12604 overlay_arrows_changed_p (void)
12605 {
12606 Lisp_Object vlist;
12607
12608 for (vlist = Voverlay_arrow_variable_list;
12609 CONSP (vlist);
12610 vlist = XCDR (vlist))
12611 {
12612 Lisp_Object var = XCAR (vlist);
12613 Lisp_Object val, pstr;
12614
12615 if (!SYMBOLP (var))
12616 continue;
12617 val = find_symbol_value (var);
12618 if (!MARKERP (val))
12619 continue;
12620 if (! EQ (COERCE_MARKER (val),
12621 Fget (var, Qlast_arrow_position))
12622 || ! (pstr = overlay_arrow_string_or_property (var),
12623 EQ (pstr, Fget (var, Qlast_arrow_string))))
12624 return 1;
12625 }
12626 return 0;
12627 }
12628
12629 /* Mark overlay arrows to be updated on next redisplay. */
12630
12631 static void
12632 update_overlay_arrows (int up_to_date)
12633 {
12634 Lisp_Object vlist;
12635
12636 for (vlist = Voverlay_arrow_variable_list;
12637 CONSP (vlist);
12638 vlist = XCDR (vlist))
12639 {
12640 Lisp_Object var = XCAR (vlist);
12641
12642 if (!SYMBOLP (var))
12643 continue;
12644
12645 if (up_to_date > 0)
12646 {
12647 Lisp_Object val = find_symbol_value (var);
12648 Fput (var, Qlast_arrow_position,
12649 COERCE_MARKER (val));
12650 Fput (var, Qlast_arrow_string,
12651 overlay_arrow_string_or_property (var));
12652 }
12653 else if (up_to_date < 0
12654 || !NILP (Fget (var, Qlast_arrow_position)))
12655 {
12656 Fput (var, Qlast_arrow_position, Qt);
12657 Fput (var, Qlast_arrow_string, Qt);
12658 }
12659 }
12660 }
12661
12662
12663 /* Return overlay arrow string to display at row.
12664 Return integer (bitmap number) for arrow bitmap in left fringe.
12665 Return nil if no overlay arrow. */
12666
12667 static Lisp_Object
12668 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12669 {
12670 Lisp_Object vlist;
12671
12672 for (vlist = Voverlay_arrow_variable_list;
12673 CONSP (vlist);
12674 vlist = XCDR (vlist))
12675 {
12676 Lisp_Object var = XCAR (vlist);
12677 Lisp_Object val;
12678
12679 if (!SYMBOLP (var))
12680 continue;
12681
12682 val = find_symbol_value (var);
12683
12684 if (MARKERP (val)
12685 && current_buffer == XMARKER (val)->buffer
12686 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12687 {
12688 if (FRAME_WINDOW_P (it->f)
12689 /* FIXME: if ROW->reversed_p is set, this should test
12690 the right fringe, not the left one. */
12691 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12692 {
12693 #ifdef HAVE_WINDOW_SYSTEM
12694 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12695 {
12696 int fringe_bitmap;
12697 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12698 return make_number (fringe_bitmap);
12699 }
12700 #endif
12701 return make_number (-1); /* Use default arrow bitmap. */
12702 }
12703 return overlay_arrow_string_or_property (var);
12704 }
12705 }
12706
12707 return Qnil;
12708 }
12709
12710 /* Return 1 if point moved out of or into a composition. Otherwise
12711 return 0. PREV_BUF and PREV_PT are the last point buffer and
12712 position. BUF and PT are the current point buffer and position. */
12713
12714 static int
12715 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12716 struct buffer *buf, ptrdiff_t pt)
12717 {
12718 ptrdiff_t start, end;
12719 Lisp_Object prop;
12720 Lisp_Object buffer;
12721
12722 XSETBUFFER (buffer, buf);
12723 /* Check a composition at the last point if point moved within the
12724 same buffer. */
12725 if (prev_buf == buf)
12726 {
12727 if (prev_pt == pt)
12728 /* Point didn't move. */
12729 return 0;
12730
12731 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12732 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12733 && COMPOSITION_VALID_P (start, end, prop)
12734 && start < prev_pt && end > prev_pt)
12735 /* The last point was within the composition. Return 1 iff
12736 point moved out of the composition. */
12737 return (pt <= start || pt >= end);
12738 }
12739
12740 /* Check a composition at the current point. */
12741 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12742 && find_composition (pt, -1, &start, &end, &prop, buffer)
12743 && COMPOSITION_VALID_P (start, end, prop)
12744 && start < pt && end > pt);
12745 }
12746
12747
12748 /* Reconsider the setting of B->clip_changed which is displayed
12749 in window W. */
12750
12751 static void
12752 reconsider_clip_changes (struct window *w, struct buffer *b)
12753 {
12754 if (b->clip_changed
12755 && w->window_end_valid
12756 && w->current_matrix->buffer == b
12757 && w->current_matrix->zv == BUF_ZV (b)
12758 && w->current_matrix->begv == BUF_BEGV (b))
12759 b->clip_changed = 0;
12760
12761 /* If display wasn't paused, and W is not a tool bar window, see if
12762 point has been moved into or out of a composition. In that case,
12763 we set b->clip_changed to 1 to force updating the screen. If
12764 b->clip_changed has already been set to 1, we can skip this
12765 check. */
12766 if (!b->clip_changed && BUFFERP (w->buffer) && w->window_end_valid)
12767 {
12768 ptrdiff_t pt;
12769
12770 if (w == XWINDOW (selected_window))
12771 pt = PT;
12772 else
12773 pt = marker_position (w->pointm);
12774
12775 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12776 || pt != w->last_point)
12777 && check_point_in_composition (w->current_matrix->buffer,
12778 w->last_point,
12779 XBUFFER (w->buffer), pt))
12780 b->clip_changed = 1;
12781 }
12782 }
12783 \f
12784
12785 #define STOP_POLLING \
12786 do { if (! polling_stopped_here) stop_polling (); \
12787 polling_stopped_here = 1; } while (0)
12788
12789 #define RESUME_POLLING \
12790 do { if (polling_stopped_here) start_polling (); \
12791 polling_stopped_here = 0; } while (0)
12792
12793
12794 /* Perhaps in the future avoid recentering windows if it
12795 is not necessary; currently that causes some problems. */
12796
12797 static void
12798 redisplay_internal (void)
12799 {
12800 struct window *w = XWINDOW (selected_window);
12801 struct window *sw;
12802 struct frame *fr;
12803 int pending;
12804 int must_finish = 0;
12805 struct text_pos tlbufpos, tlendpos;
12806 int number_of_visible_frames;
12807 ptrdiff_t count, count1;
12808 struct frame *sf;
12809 int polling_stopped_here = 0;
12810 Lisp_Object tail, frame;
12811 struct backtrace backtrace;
12812
12813 /* Non-zero means redisplay has to consider all windows on all
12814 frames. Zero means, only selected_window is considered. */
12815 int consider_all_windows_p;
12816
12817 /* Non-zero means redisplay has to redisplay the miniwindow. */
12818 int update_miniwindow_p = 0;
12819
12820 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12821
12822 /* No redisplay if running in batch mode or frame is not yet fully
12823 initialized, or redisplay is explicitly turned off by setting
12824 Vinhibit_redisplay. */
12825 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12826 || !NILP (Vinhibit_redisplay))
12827 return;
12828
12829 /* Don't examine these until after testing Vinhibit_redisplay.
12830 When Emacs is shutting down, perhaps because its connection to
12831 X has dropped, we should not look at them at all. */
12832 fr = XFRAME (w->frame);
12833 sf = SELECTED_FRAME ();
12834
12835 if (!fr->glyphs_initialized_p)
12836 return;
12837
12838 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12839 if (popup_activated ())
12840 return;
12841 #endif
12842
12843 /* I don't think this happens but let's be paranoid. */
12844 if (redisplaying_p)
12845 return;
12846
12847 /* Record a function that clears redisplaying_p
12848 when we leave this function. */
12849 count = SPECPDL_INDEX ();
12850 record_unwind_protect (unwind_redisplay, selected_frame);
12851 redisplaying_p = 1;
12852 specbind (Qinhibit_free_realized_faces, Qnil);
12853
12854 /* Record this function, so it appears on the profiler's backtraces. */
12855 backtrace.next = backtrace_list;
12856 backtrace.function = Qredisplay_internal;
12857 backtrace.args = &Qnil;
12858 backtrace.nargs = 0;
12859 backtrace.debug_on_exit = 0;
12860 backtrace_list = &backtrace;
12861
12862 FOR_EACH_FRAME (tail, frame)
12863 XFRAME (frame)->already_hscrolled_p = 0;
12864
12865 retry:
12866 /* Remember the currently selected window. */
12867 sw = w;
12868
12869 pending = 0;
12870 reconsider_clip_changes (w, current_buffer);
12871 last_escape_glyph_frame = NULL;
12872 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12873 last_glyphless_glyph_frame = NULL;
12874 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12875
12876 /* If new fonts have been loaded that make a glyph matrix adjustment
12877 necessary, do it. */
12878 if (fonts_changed_p)
12879 {
12880 adjust_glyphs (NULL);
12881 ++windows_or_buffers_changed;
12882 fonts_changed_p = 0;
12883 }
12884
12885 /* If face_change_count is non-zero, init_iterator will free all
12886 realized faces, which includes the faces referenced from current
12887 matrices. So, we can't reuse current matrices in this case. */
12888 if (face_change_count)
12889 ++windows_or_buffers_changed;
12890
12891 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12892 && FRAME_TTY (sf)->previous_frame != sf)
12893 {
12894 /* Since frames on a single ASCII terminal share the same
12895 display area, displaying a different frame means redisplay
12896 the whole thing. */
12897 windows_or_buffers_changed++;
12898 SET_FRAME_GARBAGED (sf);
12899 #ifndef DOS_NT
12900 set_tty_color_mode (FRAME_TTY (sf), sf);
12901 #endif
12902 FRAME_TTY (sf)->previous_frame = sf;
12903 }
12904
12905 /* Set the visible flags for all frames. Do this before checking for
12906 resized or garbaged frames; they want to know if their frames are
12907 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
12908 number_of_visible_frames = 0;
12909
12910 FOR_EACH_FRAME (tail, frame)
12911 {
12912 struct frame *f = XFRAME (frame);
12913
12914 if (FRAME_VISIBLE_P (f))
12915 ++number_of_visible_frames;
12916 clear_desired_matrices (f);
12917 }
12918
12919 /* Notice any pending interrupt request to change frame size. */
12920 do_pending_window_change (1);
12921
12922 /* do_pending_window_change could change the selected_window due to
12923 frame resizing which makes the selected window too small. */
12924 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12925 {
12926 sw = w;
12927 reconsider_clip_changes (w, current_buffer);
12928 }
12929
12930 /* Clear frames marked as garbaged. */
12931 clear_garbaged_frames ();
12932
12933 /* Build menubar and tool-bar items. */
12934 if (NILP (Vmemory_full))
12935 prepare_menu_bars ();
12936
12937 if (windows_or_buffers_changed)
12938 update_mode_lines++;
12939
12940 /* Detect case that we need to write or remove a star in the mode line. */
12941 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
12942 {
12943 w->update_mode_line = 1;
12944 if (buffer_shared_and_changed ())
12945 update_mode_lines++;
12946 }
12947
12948 /* Avoid invocation of point motion hooks by `current_column' below. */
12949 count1 = SPECPDL_INDEX ();
12950 specbind (Qinhibit_point_motion_hooks, Qt);
12951
12952 if (mode_line_update_needed (w))
12953 w->update_mode_line = 1;
12954
12955 unbind_to (count1, Qnil);
12956
12957 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
12958
12959 consider_all_windows_p = (update_mode_lines
12960 || buffer_shared_and_changed ()
12961 || cursor_type_changed);
12962
12963 /* If specs for an arrow have changed, do thorough redisplay
12964 to ensure we remove any arrow that should no longer exist. */
12965 if (overlay_arrows_changed_p ())
12966 consider_all_windows_p = windows_or_buffers_changed = 1;
12967
12968 /* Normally the message* functions will have already displayed and
12969 updated the echo area, but the frame may have been trashed, or
12970 the update may have been preempted, so display the echo area
12971 again here. Checking message_cleared_p captures the case that
12972 the echo area should be cleared. */
12973 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
12974 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
12975 || (message_cleared_p
12976 && minibuf_level == 0
12977 /* If the mini-window is currently selected, this means the
12978 echo-area doesn't show through. */
12979 && !MINI_WINDOW_P (XWINDOW (selected_window))))
12980 {
12981 int window_height_changed_p = echo_area_display (0);
12982
12983 if (message_cleared_p)
12984 update_miniwindow_p = 1;
12985
12986 must_finish = 1;
12987
12988 /* If we don't display the current message, don't clear the
12989 message_cleared_p flag, because, if we did, we wouldn't clear
12990 the echo area in the next redisplay which doesn't preserve
12991 the echo area. */
12992 if (!display_last_displayed_message_p)
12993 message_cleared_p = 0;
12994
12995 if (fonts_changed_p)
12996 goto retry;
12997 else if (window_height_changed_p)
12998 {
12999 consider_all_windows_p = 1;
13000 ++update_mode_lines;
13001 ++windows_or_buffers_changed;
13002
13003 /* If window configuration was changed, frames may have been
13004 marked garbaged. Clear them or we will experience
13005 surprises wrt scrolling. */
13006 clear_garbaged_frames ();
13007 }
13008 }
13009 else if (EQ (selected_window, minibuf_window)
13010 && (current_buffer->clip_changed || window_outdated (w))
13011 && resize_mini_window (w, 0))
13012 {
13013 /* Resized active mini-window to fit the size of what it is
13014 showing if its contents might have changed. */
13015 must_finish = 1;
13016 /* FIXME: this causes all frames to be updated, which seems unnecessary
13017 since only the current frame needs to be considered. This function
13018 needs to be rewritten with two variables, consider_all_windows and
13019 consider_all_frames. */
13020 consider_all_windows_p = 1;
13021 ++windows_or_buffers_changed;
13022 ++update_mode_lines;
13023
13024 /* If window configuration was changed, frames may have been
13025 marked garbaged. Clear them or we will experience
13026 surprises wrt scrolling. */
13027 clear_garbaged_frames ();
13028 }
13029
13030 /* If showing the region, and mark has changed, we must redisplay
13031 the whole window. The assignment to this_line_start_pos prevents
13032 the optimization directly below this if-statement. */
13033 if (((!NILP (Vtransient_mark_mode)
13034 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13035 != (w->region_showing > 0))
13036 || (w->region_showing
13037 && w->region_showing
13038 != XINT (Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13039 CHARPOS (this_line_start_pos) = 0;
13040
13041 /* Optimize the case that only the line containing the cursor in the
13042 selected window has changed. Variables starting with this_ are
13043 set in display_line and record information about the line
13044 containing the cursor. */
13045 tlbufpos = this_line_start_pos;
13046 tlendpos = this_line_end_pos;
13047 if (!consider_all_windows_p
13048 && CHARPOS (tlbufpos) > 0
13049 && !w->update_mode_line
13050 && !current_buffer->clip_changed
13051 && !current_buffer->prevent_redisplay_optimizations_p
13052 && FRAME_VISIBLE_P (XFRAME (w->frame))
13053 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13054 /* Make sure recorded data applies to current buffer, etc. */
13055 && this_line_buffer == current_buffer
13056 && current_buffer == XBUFFER (w->buffer)
13057 && !w->force_start
13058 && !w->optional_new_start
13059 /* Point must be on the line that we have info recorded about. */
13060 && PT >= CHARPOS (tlbufpos)
13061 && PT <= Z - CHARPOS (tlendpos)
13062 /* All text outside that line, including its final newline,
13063 must be unchanged. */
13064 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13065 CHARPOS (tlendpos)))
13066 {
13067 if (CHARPOS (tlbufpos) > BEGV
13068 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13069 && (CHARPOS (tlbufpos) == ZV
13070 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13071 /* Former continuation line has disappeared by becoming empty. */
13072 goto cancel;
13073 else if (window_outdated (w) || MINI_WINDOW_P (w))
13074 {
13075 /* We have to handle the case of continuation around a
13076 wide-column character (see the comment in indent.c around
13077 line 1340).
13078
13079 For instance, in the following case:
13080
13081 -------- Insert --------
13082 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13083 J_I_ ==> J_I_ `^^' are cursors.
13084 ^^ ^^
13085 -------- --------
13086
13087 As we have to redraw the line above, we cannot use this
13088 optimization. */
13089
13090 struct it it;
13091 int line_height_before = this_line_pixel_height;
13092
13093 /* Note that start_display will handle the case that the
13094 line starting at tlbufpos is a continuation line. */
13095 start_display (&it, w, tlbufpos);
13096
13097 /* Implementation note: It this still necessary? */
13098 if (it.current_x != this_line_start_x)
13099 goto cancel;
13100
13101 TRACE ((stderr, "trying display optimization 1\n"));
13102 w->cursor.vpos = -1;
13103 overlay_arrow_seen = 0;
13104 it.vpos = this_line_vpos;
13105 it.current_y = this_line_y;
13106 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13107 display_line (&it);
13108
13109 /* If line contains point, is not continued,
13110 and ends at same distance from eob as before, we win. */
13111 if (w->cursor.vpos >= 0
13112 /* Line is not continued, otherwise this_line_start_pos
13113 would have been set to 0 in display_line. */
13114 && CHARPOS (this_line_start_pos)
13115 /* Line ends as before. */
13116 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13117 /* Line has same height as before. Otherwise other lines
13118 would have to be shifted up or down. */
13119 && this_line_pixel_height == line_height_before)
13120 {
13121 /* If this is not the window's last line, we must adjust
13122 the charstarts of the lines below. */
13123 if (it.current_y < it.last_visible_y)
13124 {
13125 struct glyph_row *row
13126 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13127 ptrdiff_t delta, delta_bytes;
13128
13129 /* We used to distinguish between two cases here,
13130 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13131 when the line ends in a newline or the end of the
13132 buffer's accessible portion. But both cases did
13133 the same, so they were collapsed. */
13134 delta = (Z
13135 - CHARPOS (tlendpos)
13136 - MATRIX_ROW_START_CHARPOS (row));
13137 delta_bytes = (Z_BYTE
13138 - BYTEPOS (tlendpos)
13139 - MATRIX_ROW_START_BYTEPOS (row));
13140
13141 increment_matrix_positions (w->current_matrix,
13142 this_line_vpos + 1,
13143 w->current_matrix->nrows,
13144 delta, delta_bytes);
13145 }
13146
13147 /* If this row displays text now but previously didn't,
13148 or vice versa, w->window_end_vpos may have to be
13149 adjusted. */
13150 if ((it.glyph_row - 1)->displays_text_p)
13151 {
13152 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13153 wset_window_end_vpos (w, make_number (this_line_vpos));
13154 }
13155 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13156 && this_line_vpos > 0)
13157 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13158 w->window_end_valid = 0;
13159
13160 /* Update hint: No need to try to scroll in update_window. */
13161 w->desired_matrix->no_scrolling_p = 1;
13162
13163 #ifdef GLYPH_DEBUG
13164 *w->desired_matrix->method = 0;
13165 debug_method_add (w, "optimization 1");
13166 #endif
13167 #ifdef HAVE_WINDOW_SYSTEM
13168 update_window_fringes (w, 0);
13169 #endif
13170 goto update;
13171 }
13172 else
13173 goto cancel;
13174 }
13175 else if (/* Cursor position hasn't changed. */
13176 PT == w->last_point
13177 /* Make sure the cursor was last displayed
13178 in this window. Otherwise we have to reposition it. */
13179 && 0 <= w->cursor.vpos
13180 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13181 {
13182 if (!must_finish)
13183 {
13184 do_pending_window_change (1);
13185 /* If selected_window changed, redisplay again. */
13186 if (WINDOWP (selected_window)
13187 && (w = XWINDOW (selected_window)) != sw)
13188 goto retry;
13189
13190 /* We used to always goto end_of_redisplay here, but this
13191 isn't enough if we have a blinking cursor. */
13192 if (w->cursor_off_p == w->last_cursor_off_p)
13193 goto end_of_redisplay;
13194 }
13195 goto update;
13196 }
13197 /* If highlighting the region, or if the cursor is in the echo area,
13198 then we can't just move the cursor. */
13199 else if (! (!NILP (Vtransient_mark_mode)
13200 && !NILP (BVAR (current_buffer, mark_active)))
13201 && (EQ (selected_window,
13202 BVAR (current_buffer, last_selected_window))
13203 || highlight_nonselected_windows)
13204 && !w->region_showing
13205 && NILP (Vshow_trailing_whitespace)
13206 && !cursor_in_echo_area)
13207 {
13208 struct it it;
13209 struct glyph_row *row;
13210
13211 /* Skip from tlbufpos to PT and see where it is. Note that
13212 PT may be in invisible text. If so, we will end at the
13213 next visible position. */
13214 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13215 NULL, DEFAULT_FACE_ID);
13216 it.current_x = this_line_start_x;
13217 it.current_y = this_line_y;
13218 it.vpos = this_line_vpos;
13219
13220 /* The call to move_it_to stops in front of PT, but
13221 moves over before-strings. */
13222 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13223
13224 if (it.vpos == this_line_vpos
13225 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13226 row->enabled_p))
13227 {
13228 eassert (this_line_vpos == it.vpos);
13229 eassert (this_line_y == it.current_y);
13230 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13231 #ifdef GLYPH_DEBUG
13232 *w->desired_matrix->method = 0;
13233 debug_method_add (w, "optimization 3");
13234 #endif
13235 goto update;
13236 }
13237 else
13238 goto cancel;
13239 }
13240
13241 cancel:
13242 /* Text changed drastically or point moved off of line. */
13243 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13244 }
13245
13246 CHARPOS (this_line_start_pos) = 0;
13247 consider_all_windows_p |= buffer_shared_and_changed ();
13248 ++clear_face_cache_count;
13249 #ifdef HAVE_WINDOW_SYSTEM
13250 ++clear_image_cache_count;
13251 #endif
13252
13253 w->region_showing = XINT (Fmarker_position (BVAR (XBUFFER (w->buffer), mark)));
13254
13255 /* Build desired matrices, and update the display. If
13256 consider_all_windows_p is non-zero, do it for all windows on all
13257 frames. Otherwise do it for selected_window, only. */
13258
13259 if (consider_all_windows_p)
13260 {
13261 FOR_EACH_FRAME (tail, frame)
13262 XFRAME (frame)->updated_p = 0;
13263
13264 FOR_EACH_FRAME (tail, frame)
13265 {
13266 struct frame *f = XFRAME (frame);
13267
13268 /* We don't have to do anything for unselected terminal
13269 frames. */
13270 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13271 && !EQ (FRAME_TTY (f)->top_frame, frame))
13272 continue;
13273
13274 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13275 {
13276 /* Mark all the scroll bars to be removed; we'll redeem
13277 the ones we want when we redisplay their windows. */
13278 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13279 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13280
13281 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13282 redisplay_windows (FRAME_ROOT_WINDOW (f));
13283
13284 /* The X error handler may have deleted that frame. */
13285 if (!FRAME_LIVE_P (f))
13286 continue;
13287
13288 /* Any scroll bars which redisplay_windows should have
13289 nuked should now go away. */
13290 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13291 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13292
13293 /* If fonts changed, display again. */
13294 /* ??? rms: I suspect it is a mistake to jump all the way
13295 back to retry here. It should just retry this frame. */
13296 if (fonts_changed_p)
13297 goto retry;
13298
13299 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13300 {
13301 /* See if we have to hscroll. */
13302 if (!f->already_hscrolled_p)
13303 {
13304 f->already_hscrolled_p = 1;
13305 if (hscroll_windows (f->root_window))
13306 goto retry;
13307 }
13308
13309 /* Prevent various kinds of signals during display
13310 update. stdio is not robust about handling
13311 signals, which can cause an apparent I/O
13312 error. */
13313 if (interrupt_input)
13314 unrequest_sigio ();
13315 STOP_POLLING;
13316
13317 /* Update the display. */
13318 set_window_update_flags (XWINDOW (f->root_window), 1);
13319 pending |= update_frame (f, 0, 0);
13320 f->updated_p = 1;
13321 }
13322 }
13323 }
13324
13325 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13326
13327 if (!pending)
13328 {
13329 /* Do the mark_window_display_accurate after all windows have
13330 been redisplayed because this call resets flags in buffers
13331 which are needed for proper redisplay. */
13332 FOR_EACH_FRAME (tail, frame)
13333 {
13334 struct frame *f = XFRAME (frame);
13335 if (f->updated_p)
13336 {
13337 mark_window_display_accurate (f->root_window, 1);
13338 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13339 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13340 }
13341 }
13342 }
13343 }
13344 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13345 {
13346 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13347 struct frame *mini_frame;
13348
13349 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13350 /* Use list_of_error, not Qerror, so that
13351 we catch only errors and don't run the debugger. */
13352 internal_condition_case_1 (redisplay_window_1, selected_window,
13353 list_of_error,
13354 redisplay_window_error);
13355 if (update_miniwindow_p)
13356 internal_condition_case_1 (redisplay_window_1, mini_window,
13357 list_of_error,
13358 redisplay_window_error);
13359
13360 /* Compare desired and current matrices, perform output. */
13361
13362 update:
13363 /* If fonts changed, display again. */
13364 if (fonts_changed_p)
13365 goto retry;
13366
13367 /* Prevent various kinds of signals during display update.
13368 stdio is not robust about handling signals,
13369 which can cause an apparent I/O error. */
13370 if (interrupt_input)
13371 unrequest_sigio ();
13372 STOP_POLLING;
13373
13374 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13375 {
13376 if (hscroll_windows (selected_window))
13377 goto retry;
13378
13379 XWINDOW (selected_window)->must_be_updated_p = 1;
13380 pending = update_frame (sf, 0, 0);
13381 }
13382
13383 /* We may have called echo_area_display at the top of this
13384 function. If the echo area is on another frame, that may
13385 have put text on a frame other than the selected one, so the
13386 above call to update_frame would not have caught it. Catch
13387 it here. */
13388 mini_window = FRAME_MINIBUF_WINDOW (sf);
13389 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13390
13391 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13392 {
13393 XWINDOW (mini_window)->must_be_updated_p = 1;
13394 pending |= update_frame (mini_frame, 0, 0);
13395 if (!pending && hscroll_windows (mini_window))
13396 goto retry;
13397 }
13398 }
13399
13400 /* If display was paused because of pending input, make sure we do a
13401 thorough update the next time. */
13402 if (pending)
13403 {
13404 /* Prevent the optimization at the beginning of
13405 redisplay_internal that tries a single-line update of the
13406 line containing the cursor in the selected window. */
13407 CHARPOS (this_line_start_pos) = 0;
13408
13409 /* Let the overlay arrow be updated the next time. */
13410 update_overlay_arrows (0);
13411
13412 /* If we pause after scrolling, some rows in the current
13413 matrices of some windows are not valid. */
13414 if (!WINDOW_FULL_WIDTH_P (w)
13415 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13416 update_mode_lines = 1;
13417 }
13418 else
13419 {
13420 if (!consider_all_windows_p)
13421 {
13422 /* This has already been done above if
13423 consider_all_windows_p is set. */
13424 mark_window_display_accurate_1 (w, 1);
13425
13426 /* Say overlay arrows are up to date. */
13427 update_overlay_arrows (1);
13428
13429 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13430 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13431 }
13432
13433 update_mode_lines = 0;
13434 windows_or_buffers_changed = 0;
13435 cursor_type_changed = 0;
13436 }
13437
13438 /* Start SIGIO interrupts coming again. Having them off during the
13439 code above makes it less likely one will discard output, but not
13440 impossible, since there might be stuff in the system buffer here.
13441 But it is much hairier to try to do anything about that. */
13442 if (interrupt_input)
13443 request_sigio ();
13444 RESUME_POLLING;
13445
13446 /* If a frame has become visible which was not before, redisplay
13447 again, so that we display it. Expose events for such a frame
13448 (which it gets when becoming visible) don't call the parts of
13449 redisplay constructing glyphs, so simply exposing a frame won't
13450 display anything in this case. So, we have to display these
13451 frames here explicitly. */
13452 if (!pending)
13453 {
13454 int new_count = 0;
13455
13456 FOR_EACH_FRAME (tail, frame)
13457 {
13458 int this_is_visible = 0;
13459
13460 if (XFRAME (frame)->visible)
13461 this_is_visible = 1;
13462
13463 if (this_is_visible)
13464 new_count++;
13465 }
13466
13467 if (new_count != number_of_visible_frames)
13468 windows_or_buffers_changed++;
13469 }
13470
13471 /* Change frame size now if a change is pending. */
13472 do_pending_window_change (1);
13473
13474 /* If we just did a pending size change, or have additional
13475 visible frames, or selected_window changed, redisplay again. */
13476 if ((windows_or_buffers_changed && !pending)
13477 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13478 goto retry;
13479
13480 /* Clear the face and image caches.
13481
13482 We used to do this only if consider_all_windows_p. But the cache
13483 needs to be cleared if a timer creates images in the current
13484 buffer (e.g. the test case in Bug#6230). */
13485
13486 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13487 {
13488 clear_face_cache (0);
13489 clear_face_cache_count = 0;
13490 }
13491
13492 #ifdef HAVE_WINDOW_SYSTEM
13493 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13494 {
13495 clear_image_caches (Qnil);
13496 clear_image_cache_count = 0;
13497 }
13498 #endif /* HAVE_WINDOW_SYSTEM */
13499
13500 end_of_redisplay:
13501 backtrace_list = backtrace.next;
13502 unbind_to (count, Qnil);
13503 RESUME_POLLING;
13504 }
13505
13506
13507 /* Redisplay, but leave alone any recent echo area message unless
13508 another message has been requested in its place.
13509
13510 This is useful in situations where you need to redisplay but no
13511 user action has occurred, making it inappropriate for the message
13512 area to be cleared. See tracking_off and
13513 wait_reading_process_output for examples of these situations.
13514
13515 FROM_WHERE is an integer saying from where this function was
13516 called. This is useful for debugging. */
13517
13518 void
13519 redisplay_preserve_echo_area (int from_where)
13520 {
13521 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13522
13523 if (!NILP (echo_area_buffer[1]))
13524 {
13525 /* We have a previously displayed message, but no current
13526 message. Redisplay the previous message. */
13527 display_last_displayed_message_p = 1;
13528 redisplay_internal ();
13529 display_last_displayed_message_p = 0;
13530 }
13531 else
13532 redisplay_internal ();
13533
13534 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13535 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13536 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13537 }
13538
13539
13540 /* Function registered with record_unwind_protect in redisplay_internal.
13541 Clear redisplaying_p. Also select the previously selected frame. */
13542
13543 static Lisp_Object
13544 unwind_redisplay (Lisp_Object old_frame)
13545 {
13546 redisplaying_p = 0;
13547 return Qnil;
13548 }
13549
13550
13551 /* Mark the display of leaf window W as accurate or inaccurate.
13552 If ACCURATE_P is non-zero mark display of W as accurate. If
13553 ACCURATE_P is zero, arrange for W to be redisplayed the next
13554 time redisplay_internal is called. */
13555
13556 static void
13557 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13558 {
13559 struct buffer *b = XBUFFER (w->buffer);
13560
13561 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13562 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13563 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13564
13565 if (accurate_p)
13566 {
13567 b->clip_changed = 0;
13568 b->prevent_redisplay_optimizations_p = 0;
13569
13570 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13571 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13572 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13573 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13574
13575 w->current_matrix->buffer = b;
13576 w->current_matrix->begv = BUF_BEGV (b);
13577 w->current_matrix->zv = BUF_ZV (b);
13578
13579 w->last_cursor = w->cursor;
13580 w->last_cursor_off_p = w->cursor_off_p;
13581
13582 if (w == XWINDOW (selected_window))
13583 w->last_point = BUF_PT (b);
13584 else
13585 w->last_point = marker_position (w->pointm);
13586
13587 w->window_end_valid = 1;
13588 w->update_mode_line = 0;
13589 }
13590 }
13591
13592
13593 /* Mark the display of windows in the window tree rooted at WINDOW as
13594 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13595 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13596 be redisplayed the next time redisplay_internal is called. */
13597
13598 void
13599 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13600 {
13601 struct window *w;
13602
13603 for (; !NILP (window); window = w->next)
13604 {
13605 w = XWINDOW (window);
13606 if (!NILP (w->vchild))
13607 mark_window_display_accurate (w->vchild, accurate_p);
13608 else if (!NILP (w->hchild))
13609 mark_window_display_accurate (w->hchild, accurate_p);
13610 else if (BUFFERP (w->buffer))
13611 mark_window_display_accurate_1 (w, accurate_p);
13612 }
13613
13614 if (accurate_p)
13615 update_overlay_arrows (1);
13616 else
13617 /* Force a thorough redisplay the next time by setting
13618 last_arrow_position and last_arrow_string to t, which is
13619 unequal to any useful value of Voverlay_arrow_... */
13620 update_overlay_arrows (-1);
13621 }
13622
13623
13624 /* Return value in display table DP (Lisp_Char_Table *) for character
13625 C. Since a display table doesn't have any parent, we don't have to
13626 follow parent. Do not call this function directly but use the
13627 macro DISP_CHAR_VECTOR. */
13628
13629 Lisp_Object
13630 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13631 {
13632 Lisp_Object val;
13633
13634 if (ASCII_CHAR_P (c))
13635 {
13636 val = dp->ascii;
13637 if (SUB_CHAR_TABLE_P (val))
13638 val = XSUB_CHAR_TABLE (val)->contents[c];
13639 }
13640 else
13641 {
13642 Lisp_Object table;
13643
13644 XSETCHAR_TABLE (table, dp);
13645 val = char_table_ref (table, c);
13646 }
13647 if (NILP (val))
13648 val = dp->defalt;
13649 return val;
13650 }
13651
13652
13653 \f
13654 /***********************************************************************
13655 Window Redisplay
13656 ***********************************************************************/
13657
13658 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13659
13660 static void
13661 redisplay_windows (Lisp_Object window)
13662 {
13663 while (!NILP (window))
13664 {
13665 struct window *w = XWINDOW (window);
13666
13667 if (!NILP (w->hchild))
13668 redisplay_windows (w->hchild);
13669 else if (!NILP (w->vchild))
13670 redisplay_windows (w->vchild);
13671 else if (!NILP (w->buffer))
13672 {
13673 displayed_buffer = XBUFFER (w->buffer);
13674 /* Use list_of_error, not Qerror, so that
13675 we catch only errors and don't run the debugger. */
13676 internal_condition_case_1 (redisplay_window_0, window,
13677 list_of_error,
13678 redisplay_window_error);
13679 }
13680
13681 window = w->next;
13682 }
13683 }
13684
13685 static Lisp_Object
13686 redisplay_window_error (Lisp_Object ignore)
13687 {
13688 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13689 return Qnil;
13690 }
13691
13692 static Lisp_Object
13693 redisplay_window_0 (Lisp_Object window)
13694 {
13695 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13696 redisplay_window (window, 0);
13697 return Qnil;
13698 }
13699
13700 static Lisp_Object
13701 redisplay_window_1 (Lisp_Object window)
13702 {
13703 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13704 redisplay_window (window, 1);
13705 return Qnil;
13706 }
13707 \f
13708
13709 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13710 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13711 which positions recorded in ROW differ from current buffer
13712 positions.
13713
13714 Return 0 if cursor is not on this row, 1 otherwise. */
13715
13716 static int
13717 set_cursor_from_row (struct window *w, struct glyph_row *row,
13718 struct glyph_matrix *matrix,
13719 ptrdiff_t delta, ptrdiff_t delta_bytes,
13720 int dy, int dvpos)
13721 {
13722 struct glyph *glyph = row->glyphs[TEXT_AREA];
13723 struct glyph *end = glyph + row->used[TEXT_AREA];
13724 struct glyph *cursor = NULL;
13725 /* The last known character position in row. */
13726 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13727 int x = row->x;
13728 ptrdiff_t pt_old = PT - delta;
13729 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13730 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13731 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13732 /* A glyph beyond the edge of TEXT_AREA which we should never
13733 touch. */
13734 struct glyph *glyphs_end = end;
13735 /* Non-zero means we've found a match for cursor position, but that
13736 glyph has the avoid_cursor_p flag set. */
13737 int match_with_avoid_cursor = 0;
13738 /* Non-zero means we've seen at least one glyph that came from a
13739 display string. */
13740 int string_seen = 0;
13741 /* Largest and smallest buffer positions seen so far during scan of
13742 glyph row. */
13743 ptrdiff_t bpos_max = pos_before;
13744 ptrdiff_t bpos_min = pos_after;
13745 /* Last buffer position covered by an overlay string with an integer
13746 `cursor' property. */
13747 ptrdiff_t bpos_covered = 0;
13748 /* Non-zero means the display string on which to display the cursor
13749 comes from a text property, not from an overlay. */
13750 int string_from_text_prop = 0;
13751
13752 /* Don't even try doing anything if called for a mode-line or
13753 header-line row, since the rest of the code isn't prepared to
13754 deal with such calamities. */
13755 eassert (!row->mode_line_p);
13756 if (row->mode_line_p)
13757 return 0;
13758
13759 /* Skip over glyphs not having an object at the start and the end of
13760 the row. These are special glyphs like truncation marks on
13761 terminal frames. */
13762 if (row->displays_text_p)
13763 {
13764 if (!row->reversed_p)
13765 {
13766 while (glyph < end
13767 && INTEGERP (glyph->object)
13768 && glyph->charpos < 0)
13769 {
13770 x += glyph->pixel_width;
13771 ++glyph;
13772 }
13773 while (end > glyph
13774 && INTEGERP ((end - 1)->object)
13775 /* CHARPOS is zero for blanks and stretch glyphs
13776 inserted by extend_face_to_end_of_line. */
13777 && (end - 1)->charpos <= 0)
13778 --end;
13779 glyph_before = glyph - 1;
13780 glyph_after = end;
13781 }
13782 else
13783 {
13784 struct glyph *g;
13785
13786 /* If the glyph row is reversed, we need to process it from back
13787 to front, so swap the edge pointers. */
13788 glyphs_end = end = glyph - 1;
13789 glyph += row->used[TEXT_AREA] - 1;
13790
13791 while (glyph > end + 1
13792 && INTEGERP (glyph->object)
13793 && glyph->charpos < 0)
13794 {
13795 --glyph;
13796 x -= glyph->pixel_width;
13797 }
13798 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13799 --glyph;
13800 /* By default, in reversed rows we put the cursor on the
13801 rightmost (first in the reading order) glyph. */
13802 for (g = end + 1; g < glyph; g++)
13803 x += g->pixel_width;
13804 while (end < glyph
13805 && INTEGERP ((end + 1)->object)
13806 && (end + 1)->charpos <= 0)
13807 ++end;
13808 glyph_before = glyph + 1;
13809 glyph_after = end;
13810 }
13811 }
13812 else if (row->reversed_p)
13813 {
13814 /* In R2L rows that don't display text, put the cursor on the
13815 rightmost glyph. Case in point: an empty last line that is
13816 part of an R2L paragraph. */
13817 cursor = end - 1;
13818 /* Avoid placing the cursor on the last glyph of the row, where
13819 on terminal frames we hold the vertical border between
13820 adjacent windows. */
13821 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13822 && !WINDOW_RIGHTMOST_P (w)
13823 && cursor == row->glyphs[LAST_AREA] - 1)
13824 cursor--;
13825 x = -1; /* will be computed below, at label compute_x */
13826 }
13827
13828 /* Step 1: Try to find the glyph whose character position
13829 corresponds to point. If that's not possible, find 2 glyphs
13830 whose character positions are the closest to point, one before
13831 point, the other after it. */
13832 if (!row->reversed_p)
13833 while (/* not marched to end of glyph row */
13834 glyph < end
13835 /* glyph was not inserted by redisplay for internal purposes */
13836 && !INTEGERP (glyph->object))
13837 {
13838 if (BUFFERP (glyph->object))
13839 {
13840 ptrdiff_t dpos = glyph->charpos - pt_old;
13841
13842 if (glyph->charpos > bpos_max)
13843 bpos_max = glyph->charpos;
13844 if (glyph->charpos < bpos_min)
13845 bpos_min = glyph->charpos;
13846 if (!glyph->avoid_cursor_p)
13847 {
13848 /* If we hit point, we've found the glyph on which to
13849 display the cursor. */
13850 if (dpos == 0)
13851 {
13852 match_with_avoid_cursor = 0;
13853 break;
13854 }
13855 /* See if we've found a better approximation to
13856 POS_BEFORE or to POS_AFTER. */
13857 if (0 > dpos && dpos > pos_before - pt_old)
13858 {
13859 pos_before = glyph->charpos;
13860 glyph_before = glyph;
13861 }
13862 else if (0 < dpos && dpos < pos_after - pt_old)
13863 {
13864 pos_after = glyph->charpos;
13865 glyph_after = glyph;
13866 }
13867 }
13868 else if (dpos == 0)
13869 match_with_avoid_cursor = 1;
13870 }
13871 else if (STRINGP (glyph->object))
13872 {
13873 Lisp_Object chprop;
13874 ptrdiff_t glyph_pos = glyph->charpos;
13875
13876 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13877 glyph->object);
13878 if (!NILP (chprop))
13879 {
13880 /* If the string came from a `display' text property,
13881 look up the buffer position of that property and
13882 use that position to update bpos_max, as if we
13883 actually saw such a position in one of the row's
13884 glyphs. This helps with supporting integer values
13885 of `cursor' property on the display string in
13886 situations where most or all of the row's buffer
13887 text is completely covered by display properties,
13888 so that no glyph with valid buffer positions is
13889 ever seen in the row. */
13890 ptrdiff_t prop_pos =
13891 string_buffer_position_lim (glyph->object, pos_before,
13892 pos_after, 0);
13893
13894 if (prop_pos >= pos_before)
13895 bpos_max = prop_pos - 1;
13896 }
13897 if (INTEGERP (chprop))
13898 {
13899 bpos_covered = bpos_max + XINT (chprop);
13900 /* If the `cursor' property covers buffer positions up
13901 to and including point, we should display cursor on
13902 this glyph. Note that, if a `cursor' property on one
13903 of the string's characters has an integer value, we
13904 will break out of the loop below _before_ we get to
13905 the position match above. IOW, integer values of
13906 the `cursor' property override the "exact match for
13907 point" strategy of positioning the cursor. */
13908 /* Implementation note: bpos_max == pt_old when, e.g.,
13909 we are in an empty line, where bpos_max is set to
13910 MATRIX_ROW_START_CHARPOS, see above. */
13911 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13912 {
13913 cursor = glyph;
13914 break;
13915 }
13916 }
13917
13918 string_seen = 1;
13919 }
13920 x += glyph->pixel_width;
13921 ++glyph;
13922 }
13923 else if (glyph > end) /* row is reversed */
13924 while (!INTEGERP (glyph->object))
13925 {
13926 if (BUFFERP (glyph->object))
13927 {
13928 ptrdiff_t dpos = glyph->charpos - pt_old;
13929
13930 if (glyph->charpos > bpos_max)
13931 bpos_max = glyph->charpos;
13932 if (glyph->charpos < bpos_min)
13933 bpos_min = glyph->charpos;
13934 if (!glyph->avoid_cursor_p)
13935 {
13936 if (dpos == 0)
13937 {
13938 match_with_avoid_cursor = 0;
13939 break;
13940 }
13941 if (0 > dpos && dpos > pos_before - pt_old)
13942 {
13943 pos_before = glyph->charpos;
13944 glyph_before = glyph;
13945 }
13946 else if (0 < dpos && dpos < pos_after - pt_old)
13947 {
13948 pos_after = glyph->charpos;
13949 glyph_after = glyph;
13950 }
13951 }
13952 else if (dpos == 0)
13953 match_with_avoid_cursor = 1;
13954 }
13955 else if (STRINGP (glyph->object))
13956 {
13957 Lisp_Object chprop;
13958 ptrdiff_t glyph_pos = glyph->charpos;
13959
13960 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13961 glyph->object);
13962 if (!NILP (chprop))
13963 {
13964 ptrdiff_t prop_pos =
13965 string_buffer_position_lim (glyph->object, pos_before,
13966 pos_after, 0);
13967
13968 if (prop_pos >= pos_before)
13969 bpos_max = prop_pos - 1;
13970 }
13971 if (INTEGERP (chprop))
13972 {
13973 bpos_covered = bpos_max + XINT (chprop);
13974 /* If the `cursor' property covers buffer positions up
13975 to and including point, we should display cursor on
13976 this glyph. */
13977 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13978 {
13979 cursor = glyph;
13980 break;
13981 }
13982 }
13983 string_seen = 1;
13984 }
13985 --glyph;
13986 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
13987 {
13988 x--; /* can't use any pixel_width */
13989 break;
13990 }
13991 x -= glyph->pixel_width;
13992 }
13993
13994 /* Step 2: If we didn't find an exact match for point, we need to
13995 look for a proper place to put the cursor among glyphs between
13996 GLYPH_BEFORE and GLYPH_AFTER. */
13997 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
13998 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
13999 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14000 {
14001 /* An empty line has a single glyph whose OBJECT is zero and
14002 whose CHARPOS is the position of a newline on that line.
14003 Note that on a TTY, there are more glyphs after that, which
14004 were produced by extend_face_to_end_of_line, but their
14005 CHARPOS is zero or negative. */
14006 int empty_line_p =
14007 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14008 && INTEGERP (glyph->object) && glyph->charpos > 0
14009 /* On a TTY, continued and truncated rows also have a glyph at
14010 their end whose OBJECT is zero and whose CHARPOS is
14011 positive (the continuation and truncation glyphs), but such
14012 rows are obviously not "empty". */
14013 && !(row->continued_p || row->truncated_on_right_p);
14014
14015 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14016 {
14017 ptrdiff_t ellipsis_pos;
14018
14019 /* Scan back over the ellipsis glyphs. */
14020 if (!row->reversed_p)
14021 {
14022 ellipsis_pos = (glyph - 1)->charpos;
14023 while (glyph > row->glyphs[TEXT_AREA]
14024 && (glyph - 1)->charpos == ellipsis_pos)
14025 glyph--, x -= glyph->pixel_width;
14026 /* That loop always goes one position too far, including
14027 the glyph before the ellipsis. So scan forward over
14028 that one. */
14029 x += glyph->pixel_width;
14030 glyph++;
14031 }
14032 else /* row is reversed */
14033 {
14034 ellipsis_pos = (glyph + 1)->charpos;
14035 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14036 && (glyph + 1)->charpos == ellipsis_pos)
14037 glyph++, x += glyph->pixel_width;
14038 x -= glyph->pixel_width;
14039 glyph--;
14040 }
14041 }
14042 else if (match_with_avoid_cursor)
14043 {
14044 cursor = glyph_after;
14045 x = -1;
14046 }
14047 else if (string_seen)
14048 {
14049 int incr = row->reversed_p ? -1 : +1;
14050
14051 /* Need to find the glyph that came out of a string which is
14052 present at point. That glyph is somewhere between
14053 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14054 positioned between POS_BEFORE and POS_AFTER in the
14055 buffer. */
14056 struct glyph *start, *stop;
14057 ptrdiff_t pos = pos_before;
14058
14059 x = -1;
14060
14061 /* If the row ends in a newline from a display string,
14062 reordering could have moved the glyphs belonging to the
14063 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14064 in this case we extend the search to the last glyph in
14065 the row that was not inserted by redisplay. */
14066 if (row->ends_in_newline_from_string_p)
14067 {
14068 glyph_after = end;
14069 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14070 }
14071
14072 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14073 correspond to POS_BEFORE and POS_AFTER, respectively. We
14074 need START and STOP in the order that corresponds to the
14075 row's direction as given by its reversed_p flag. If the
14076 directionality of characters between POS_BEFORE and
14077 POS_AFTER is the opposite of the row's base direction,
14078 these characters will have been reordered for display,
14079 and we need to reverse START and STOP. */
14080 if (!row->reversed_p)
14081 {
14082 start = min (glyph_before, glyph_after);
14083 stop = max (glyph_before, glyph_after);
14084 }
14085 else
14086 {
14087 start = max (glyph_before, glyph_after);
14088 stop = min (glyph_before, glyph_after);
14089 }
14090 for (glyph = start + incr;
14091 row->reversed_p ? glyph > stop : glyph < stop; )
14092 {
14093
14094 /* Any glyphs that come from the buffer are here because
14095 of bidi reordering. Skip them, and only pay
14096 attention to glyphs that came from some string. */
14097 if (STRINGP (glyph->object))
14098 {
14099 Lisp_Object str;
14100 ptrdiff_t tem;
14101 /* If the display property covers the newline, we
14102 need to search for it one position farther. */
14103 ptrdiff_t lim = pos_after
14104 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14105
14106 string_from_text_prop = 0;
14107 str = glyph->object;
14108 tem = string_buffer_position_lim (str, pos, lim, 0);
14109 if (tem == 0 /* from overlay */
14110 || pos <= tem)
14111 {
14112 /* If the string from which this glyph came is
14113 found in the buffer at point, or at position
14114 that is closer to point than pos_after, then
14115 we've found the glyph we've been looking for.
14116 If it comes from an overlay (tem == 0), and
14117 it has the `cursor' property on one of its
14118 glyphs, record that glyph as a candidate for
14119 displaying the cursor. (As in the
14120 unidirectional version, we will display the
14121 cursor on the last candidate we find.) */
14122 if (tem == 0
14123 || tem == pt_old
14124 || (tem - pt_old > 0 && tem < pos_after))
14125 {
14126 /* The glyphs from this string could have
14127 been reordered. Find the one with the
14128 smallest string position. Or there could
14129 be a character in the string with the
14130 `cursor' property, which means display
14131 cursor on that character's glyph. */
14132 ptrdiff_t strpos = glyph->charpos;
14133
14134 if (tem)
14135 {
14136 cursor = glyph;
14137 string_from_text_prop = 1;
14138 }
14139 for ( ;
14140 (row->reversed_p ? glyph > stop : glyph < stop)
14141 && EQ (glyph->object, str);
14142 glyph += incr)
14143 {
14144 Lisp_Object cprop;
14145 ptrdiff_t gpos = glyph->charpos;
14146
14147 cprop = Fget_char_property (make_number (gpos),
14148 Qcursor,
14149 glyph->object);
14150 if (!NILP (cprop))
14151 {
14152 cursor = glyph;
14153 break;
14154 }
14155 if (tem && glyph->charpos < strpos)
14156 {
14157 strpos = glyph->charpos;
14158 cursor = glyph;
14159 }
14160 }
14161
14162 if (tem == pt_old
14163 || (tem - pt_old > 0 && tem < pos_after))
14164 goto compute_x;
14165 }
14166 if (tem)
14167 pos = tem + 1; /* don't find previous instances */
14168 }
14169 /* This string is not what we want; skip all of the
14170 glyphs that came from it. */
14171 while ((row->reversed_p ? glyph > stop : glyph < stop)
14172 && EQ (glyph->object, str))
14173 glyph += incr;
14174 }
14175 else
14176 glyph += incr;
14177 }
14178
14179 /* If we reached the end of the line, and END was from a string,
14180 the cursor is not on this line. */
14181 if (cursor == NULL
14182 && (row->reversed_p ? glyph <= end : glyph >= end)
14183 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14184 && STRINGP (end->object)
14185 && row->continued_p)
14186 return 0;
14187 }
14188 /* A truncated row may not include PT among its character positions.
14189 Setting the cursor inside the scroll margin will trigger
14190 recalculation of hscroll in hscroll_window_tree. But if a
14191 display string covers point, defer to the string-handling
14192 code below to figure this out. */
14193 else if (row->truncated_on_left_p && pt_old < bpos_min)
14194 {
14195 cursor = glyph_before;
14196 x = -1;
14197 }
14198 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14199 /* Zero-width characters produce no glyphs. */
14200 || (!empty_line_p
14201 && (row->reversed_p
14202 ? glyph_after > glyphs_end
14203 : glyph_after < glyphs_end)))
14204 {
14205 cursor = glyph_after;
14206 x = -1;
14207 }
14208 }
14209
14210 compute_x:
14211 if (cursor != NULL)
14212 glyph = cursor;
14213 else if (glyph == glyphs_end
14214 && pos_before == pos_after
14215 && STRINGP ((row->reversed_p
14216 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14217 : row->glyphs[TEXT_AREA])->object))
14218 {
14219 /* If all the glyphs of this row came from strings, put the
14220 cursor on the first glyph of the row. This avoids having the
14221 cursor outside of the text area in this very rare and hard
14222 use case. */
14223 glyph =
14224 row->reversed_p
14225 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14226 : row->glyphs[TEXT_AREA];
14227 }
14228 if (x < 0)
14229 {
14230 struct glyph *g;
14231
14232 /* Need to compute x that corresponds to GLYPH. */
14233 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14234 {
14235 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14236 emacs_abort ();
14237 x += g->pixel_width;
14238 }
14239 }
14240
14241 /* ROW could be part of a continued line, which, under bidi
14242 reordering, might have other rows whose start and end charpos
14243 occlude point. Only set w->cursor if we found a better
14244 approximation to the cursor position than we have from previously
14245 examined candidate rows belonging to the same continued line. */
14246 if (/* we already have a candidate row */
14247 w->cursor.vpos >= 0
14248 /* that candidate is not the row we are processing */
14249 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14250 /* Make sure cursor.vpos specifies a row whose start and end
14251 charpos occlude point, and it is valid candidate for being a
14252 cursor-row. This is because some callers of this function
14253 leave cursor.vpos at the row where the cursor was displayed
14254 during the last redisplay cycle. */
14255 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14256 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14257 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14258 {
14259 struct glyph *g1 =
14260 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14261
14262 /* Don't consider glyphs that are outside TEXT_AREA. */
14263 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14264 return 0;
14265 /* Keep the candidate whose buffer position is the closest to
14266 point or has the `cursor' property. */
14267 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14268 w->cursor.hpos >= 0
14269 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14270 && ((BUFFERP (g1->object)
14271 && (g1->charpos == pt_old /* an exact match always wins */
14272 || (BUFFERP (glyph->object)
14273 && eabs (g1->charpos - pt_old)
14274 < eabs (glyph->charpos - pt_old))))
14275 /* previous candidate is a glyph from a string that has
14276 a non-nil `cursor' property */
14277 || (STRINGP (g1->object)
14278 && (!NILP (Fget_char_property (make_number (g1->charpos),
14279 Qcursor, g1->object))
14280 /* previous candidate is from the same display
14281 string as this one, and the display string
14282 came from a text property */
14283 || (EQ (g1->object, glyph->object)
14284 && string_from_text_prop)
14285 /* this candidate is from newline and its
14286 position is not an exact match */
14287 || (INTEGERP (glyph->object)
14288 && glyph->charpos != pt_old)))))
14289 return 0;
14290 /* If this candidate gives an exact match, use that. */
14291 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14292 /* If this candidate is a glyph created for the
14293 terminating newline of a line, and point is on that
14294 newline, it wins because it's an exact match. */
14295 || (!row->continued_p
14296 && INTEGERP (glyph->object)
14297 && glyph->charpos == 0
14298 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14299 /* Otherwise, keep the candidate that comes from a row
14300 spanning less buffer positions. This may win when one or
14301 both candidate positions are on glyphs that came from
14302 display strings, for which we cannot compare buffer
14303 positions. */
14304 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14305 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14306 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14307 return 0;
14308 }
14309 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14310 w->cursor.x = x;
14311 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14312 w->cursor.y = row->y + dy;
14313
14314 if (w == XWINDOW (selected_window))
14315 {
14316 if (!row->continued_p
14317 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14318 && row->x == 0)
14319 {
14320 this_line_buffer = XBUFFER (w->buffer);
14321
14322 CHARPOS (this_line_start_pos)
14323 = MATRIX_ROW_START_CHARPOS (row) + delta;
14324 BYTEPOS (this_line_start_pos)
14325 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14326
14327 CHARPOS (this_line_end_pos)
14328 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14329 BYTEPOS (this_line_end_pos)
14330 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14331
14332 this_line_y = w->cursor.y;
14333 this_line_pixel_height = row->height;
14334 this_line_vpos = w->cursor.vpos;
14335 this_line_start_x = row->x;
14336 }
14337 else
14338 CHARPOS (this_line_start_pos) = 0;
14339 }
14340
14341 return 1;
14342 }
14343
14344
14345 /* Run window scroll functions, if any, for WINDOW with new window
14346 start STARTP. Sets the window start of WINDOW to that position.
14347
14348 We assume that the window's buffer is really current. */
14349
14350 static struct text_pos
14351 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14352 {
14353 struct window *w = XWINDOW (window);
14354 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14355
14356 if (current_buffer != XBUFFER (w->buffer))
14357 emacs_abort ();
14358
14359 if (!NILP (Vwindow_scroll_functions))
14360 {
14361 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14362 make_number (CHARPOS (startp)));
14363 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14364 /* In case the hook functions switch buffers. */
14365 set_buffer_internal (XBUFFER (w->buffer));
14366 }
14367
14368 return startp;
14369 }
14370
14371
14372 /* Make sure the line containing the cursor is fully visible.
14373 A value of 1 means there is nothing to be done.
14374 (Either the line is fully visible, or it cannot be made so,
14375 or we cannot tell.)
14376
14377 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14378 is higher than window.
14379
14380 A value of 0 means the caller should do scrolling
14381 as if point had gone off the screen. */
14382
14383 static int
14384 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14385 {
14386 struct glyph_matrix *matrix;
14387 struct glyph_row *row;
14388 int window_height;
14389
14390 if (!make_cursor_line_fully_visible_p)
14391 return 1;
14392
14393 /* It's not always possible to find the cursor, e.g, when a window
14394 is full of overlay strings. Don't do anything in that case. */
14395 if (w->cursor.vpos < 0)
14396 return 1;
14397
14398 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14399 row = MATRIX_ROW (matrix, w->cursor.vpos);
14400
14401 /* If the cursor row is not partially visible, there's nothing to do. */
14402 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14403 return 1;
14404
14405 /* If the row the cursor is in is taller than the window's height,
14406 it's not clear what to do, so do nothing. */
14407 window_height = window_box_height (w);
14408 if (row->height >= window_height)
14409 {
14410 if (!force_p || MINI_WINDOW_P (w)
14411 || w->vscroll || w->cursor.vpos == 0)
14412 return 1;
14413 }
14414 return 0;
14415 }
14416
14417
14418 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14419 non-zero means only WINDOW is redisplayed in redisplay_internal.
14420 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14421 in redisplay_window to bring a partially visible line into view in
14422 the case that only the cursor has moved.
14423
14424 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14425 last screen line's vertical height extends past the end of the screen.
14426
14427 Value is
14428
14429 1 if scrolling succeeded
14430
14431 0 if scrolling didn't find point.
14432
14433 -1 if new fonts have been loaded so that we must interrupt
14434 redisplay, adjust glyph matrices, and try again. */
14435
14436 enum
14437 {
14438 SCROLLING_SUCCESS,
14439 SCROLLING_FAILED,
14440 SCROLLING_NEED_LARGER_MATRICES
14441 };
14442
14443 /* If scroll-conservatively is more than this, never recenter.
14444
14445 If you change this, don't forget to update the doc string of
14446 `scroll-conservatively' and the Emacs manual. */
14447 #define SCROLL_LIMIT 100
14448
14449 static int
14450 try_scrolling (Lisp_Object window, int just_this_one_p,
14451 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14452 int temp_scroll_step, int last_line_misfit)
14453 {
14454 struct window *w = XWINDOW (window);
14455 struct frame *f = XFRAME (w->frame);
14456 struct text_pos pos, startp;
14457 struct it it;
14458 int this_scroll_margin, scroll_max, rc, height;
14459 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14460 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14461 Lisp_Object aggressive;
14462 /* We will never try scrolling more than this number of lines. */
14463 int scroll_limit = SCROLL_LIMIT;
14464
14465 #ifdef GLYPH_DEBUG
14466 debug_method_add (w, "try_scrolling");
14467 #endif
14468
14469 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14470
14471 /* Compute scroll margin height in pixels. We scroll when point is
14472 within this distance from the top or bottom of the window. */
14473 if (scroll_margin > 0)
14474 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14475 * FRAME_LINE_HEIGHT (f);
14476 else
14477 this_scroll_margin = 0;
14478
14479 /* Force arg_scroll_conservatively to have a reasonable value, to
14480 avoid scrolling too far away with slow move_it_* functions. Note
14481 that the user can supply scroll-conservatively equal to
14482 `most-positive-fixnum', which can be larger than INT_MAX. */
14483 if (arg_scroll_conservatively > scroll_limit)
14484 {
14485 arg_scroll_conservatively = scroll_limit + 1;
14486 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14487 }
14488 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14489 /* Compute how much we should try to scroll maximally to bring
14490 point into view. */
14491 scroll_max = (max (scroll_step,
14492 max (arg_scroll_conservatively, temp_scroll_step))
14493 * FRAME_LINE_HEIGHT (f));
14494 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14495 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14496 /* We're trying to scroll because of aggressive scrolling but no
14497 scroll_step is set. Choose an arbitrary one. */
14498 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14499 else
14500 scroll_max = 0;
14501
14502 too_near_end:
14503
14504 /* Decide whether to scroll down. */
14505 if (PT > CHARPOS (startp))
14506 {
14507 int scroll_margin_y;
14508
14509 /* Compute the pixel ypos of the scroll margin, then move IT to
14510 either that ypos or PT, whichever comes first. */
14511 start_display (&it, w, startp);
14512 scroll_margin_y = it.last_visible_y - this_scroll_margin
14513 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14514 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14515 (MOVE_TO_POS | MOVE_TO_Y));
14516
14517 if (PT > CHARPOS (it.current.pos))
14518 {
14519 int y0 = line_bottom_y (&it);
14520 /* Compute how many pixels below window bottom to stop searching
14521 for PT. This avoids costly search for PT that is far away if
14522 the user limited scrolling by a small number of lines, but
14523 always finds PT if scroll_conservatively is set to a large
14524 number, such as most-positive-fixnum. */
14525 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14526 int y_to_move = it.last_visible_y + slack;
14527
14528 /* Compute the distance from the scroll margin to PT or to
14529 the scroll limit, whichever comes first. This should
14530 include the height of the cursor line, to make that line
14531 fully visible. */
14532 move_it_to (&it, PT, -1, y_to_move,
14533 -1, MOVE_TO_POS | MOVE_TO_Y);
14534 dy = line_bottom_y (&it) - y0;
14535
14536 if (dy > scroll_max)
14537 return SCROLLING_FAILED;
14538
14539 if (dy > 0)
14540 scroll_down_p = 1;
14541 }
14542 }
14543
14544 if (scroll_down_p)
14545 {
14546 /* Point is in or below the bottom scroll margin, so move the
14547 window start down. If scrolling conservatively, move it just
14548 enough down to make point visible. If scroll_step is set,
14549 move it down by scroll_step. */
14550 if (arg_scroll_conservatively)
14551 amount_to_scroll
14552 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14553 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14554 else if (scroll_step || temp_scroll_step)
14555 amount_to_scroll = scroll_max;
14556 else
14557 {
14558 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14559 height = WINDOW_BOX_TEXT_HEIGHT (w);
14560 if (NUMBERP (aggressive))
14561 {
14562 double float_amount = XFLOATINT (aggressive) * height;
14563 int aggressive_scroll = float_amount;
14564 if (aggressive_scroll == 0 && float_amount > 0)
14565 aggressive_scroll = 1;
14566 /* Don't let point enter the scroll margin near top of
14567 the window. This could happen if the value of
14568 scroll_up_aggressively is too large and there are
14569 non-zero margins, because scroll_up_aggressively
14570 means put point that fraction of window height
14571 _from_the_bottom_margin_. */
14572 if (aggressive_scroll + 2*this_scroll_margin > height)
14573 aggressive_scroll = height - 2*this_scroll_margin;
14574 amount_to_scroll = dy + aggressive_scroll;
14575 }
14576 }
14577
14578 if (amount_to_scroll <= 0)
14579 return SCROLLING_FAILED;
14580
14581 start_display (&it, w, startp);
14582 if (arg_scroll_conservatively <= scroll_limit)
14583 move_it_vertically (&it, amount_to_scroll);
14584 else
14585 {
14586 /* Extra precision for users who set scroll-conservatively
14587 to a large number: make sure the amount we scroll
14588 the window start is never less than amount_to_scroll,
14589 which was computed as distance from window bottom to
14590 point. This matters when lines at window top and lines
14591 below window bottom have different height. */
14592 struct it it1;
14593 void *it1data = NULL;
14594 /* We use a temporary it1 because line_bottom_y can modify
14595 its argument, if it moves one line down; see there. */
14596 int start_y;
14597
14598 SAVE_IT (it1, it, it1data);
14599 start_y = line_bottom_y (&it1);
14600 do {
14601 RESTORE_IT (&it, &it, it1data);
14602 move_it_by_lines (&it, 1);
14603 SAVE_IT (it1, it, it1data);
14604 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14605 }
14606
14607 /* If STARTP is unchanged, move it down another screen line. */
14608 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14609 move_it_by_lines (&it, 1);
14610 startp = it.current.pos;
14611 }
14612 else
14613 {
14614 struct text_pos scroll_margin_pos = startp;
14615
14616 /* See if point is inside the scroll margin at the top of the
14617 window. */
14618 if (this_scroll_margin)
14619 {
14620 start_display (&it, w, startp);
14621 move_it_vertically (&it, this_scroll_margin);
14622 scroll_margin_pos = it.current.pos;
14623 }
14624
14625 if (PT < CHARPOS (scroll_margin_pos))
14626 {
14627 /* Point is in the scroll margin at the top of the window or
14628 above what is displayed in the window. */
14629 int y0, y_to_move;
14630
14631 /* Compute the vertical distance from PT to the scroll
14632 margin position. Move as far as scroll_max allows, or
14633 one screenful, or 10 screen lines, whichever is largest.
14634 Give up if distance is greater than scroll_max or if we
14635 didn't reach the scroll margin position. */
14636 SET_TEXT_POS (pos, PT, PT_BYTE);
14637 start_display (&it, w, pos);
14638 y0 = it.current_y;
14639 y_to_move = max (it.last_visible_y,
14640 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14641 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14642 y_to_move, -1,
14643 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14644 dy = it.current_y - y0;
14645 if (dy > scroll_max
14646 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14647 return SCROLLING_FAILED;
14648
14649 /* Compute new window start. */
14650 start_display (&it, w, startp);
14651
14652 if (arg_scroll_conservatively)
14653 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14654 max (scroll_step, temp_scroll_step));
14655 else if (scroll_step || temp_scroll_step)
14656 amount_to_scroll = scroll_max;
14657 else
14658 {
14659 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14660 height = WINDOW_BOX_TEXT_HEIGHT (w);
14661 if (NUMBERP (aggressive))
14662 {
14663 double float_amount = XFLOATINT (aggressive) * height;
14664 int aggressive_scroll = float_amount;
14665 if (aggressive_scroll == 0 && float_amount > 0)
14666 aggressive_scroll = 1;
14667 /* Don't let point enter the scroll margin near
14668 bottom of the window, if the value of
14669 scroll_down_aggressively happens to be too
14670 large. */
14671 if (aggressive_scroll + 2*this_scroll_margin > height)
14672 aggressive_scroll = height - 2*this_scroll_margin;
14673 amount_to_scroll = dy + aggressive_scroll;
14674 }
14675 }
14676
14677 if (amount_to_scroll <= 0)
14678 return SCROLLING_FAILED;
14679
14680 move_it_vertically_backward (&it, amount_to_scroll);
14681 startp = it.current.pos;
14682 }
14683 }
14684
14685 /* Run window scroll functions. */
14686 startp = run_window_scroll_functions (window, startp);
14687
14688 /* Display the window. Give up if new fonts are loaded, or if point
14689 doesn't appear. */
14690 if (!try_window (window, startp, 0))
14691 rc = SCROLLING_NEED_LARGER_MATRICES;
14692 else if (w->cursor.vpos < 0)
14693 {
14694 clear_glyph_matrix (w->desired_matrix);
14695 rc = SCROLLING_FAILED;
14696 }
14697 else
14698 {
14699 /* Maybe forget recorded base line for line number display. */
14700 if (!just_this_one_p
14701 || current_buffer->clip_changed
14702 || BEG_UNCHANGED < CHARPOS (startp))
14703 w->base_line_number = 0;
14704
14705 /* If cursor ends up on a partially visible line,
14706 treat that as being off the bottom of the screen. */
14707 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14708 /* It's possible that the cursor is on the first line of the
14709 buffer, which is partially obscured due to a vscroll
14710 (Bug#7537). In that case, avoid looping forever . */
14711 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14712 {
14713 clear_glyph_matrix (w->desired_matrix);
14714 ++extra_scroll_margin_lines;
14715 goto too_near_end;
14716 }
14717 rc = SCROLLING_SUCCESS;
14718 }
14719
14720 return rc;
14721 }
14722
14723
14724 /* Compute a suitable window start for window W if display of W starts
14725 on a continuation line. Value is non-zero if a new window start
14726 was computed.
14727
14728 The new window start will be computed, based on W's width, starting
14729 from the start of the continued line. It is the start of the
14730 screen line with the minimum distance from the old start W->start. */
14731
14732 static int
14733 compute_window_start_on_continuation_line (struct window *w)
14734 {
14735 struct text_pos pos, start_pos;
14736 int window_start_changed_p = 0;
14737
14738 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14739
14740 /* If window start is on a continuation line... Window start may be
14741 < BEGV in case there's invisible text at the start of the
14742 buffer (M-x rmail, for example). */
14743 if (CHARPOS (start_pos) > BEGV
14744 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14745 {
14746 struct it it;
14747 struct glyph_row *row;
14748
14749 /* Handle the case that the window start is out of range. */
14750 if (CHARPOS (start_pos) < BEGV)
14751 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14752 else if (CHARPOS (start_pos) > ZV)
14753 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14754
14755 /* Find the start of the continued line. This should be fast
14756 because scan_buffer is fast (newline cache). */
14757 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14758 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14759 row, DEFAULT_FACE_ID);
14760 reseat_at_previous_visible_line_start (&it);
14761
14762 /* If the line start is "too far" away from the window start,
14763 say it takes too much time to compute a new window start. */
14764 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14765 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14766 {
14767 int min_distance, distance;
14768
14769 /* Move forward by display lines to find the new window
14770 start. If window width was enlarged, the new start can
14771 be expected to be > the old start. If window width was
14772 decreased, the new window start will be < the old start.
14773 So, we're looking for the display line start with the
14774 minimum distance from the old window start. */
14775 pos = it.current.pos;
14776 min_distance = INFINITY;
14777 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14778 distance < min_distance)
14779 {
14780 min_distance = distance;
14781 pos = it.current.pos;
14782 move_it_by_lines (&it, 1);
14783 }
14784
14785 /* Set the window start there. */
14786 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14787 window_start_changed_p = 1;
14788 }
14789 }
14790
14791 return window_start_changed_p;
14792 }
14793
14794
14795 /* Try cursor movement in case text has not changed in window WINDOW,
14796 with window start STARTP. Value is
14797
14798 CURSOR_MOVEMENT_SUCCESS if successful
14799
14800 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14801
14802 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14803 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14804 we want to scroll as if scroll-step were set to 1. See the code.
14805
14806 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14807 which case we have to abort this redisplay, and adjust matrices
14808 first. */
14809
14810 enum
14811 {
14812 CURSOR_MOVEMENT_SUCCESS,
14813 CURSOR_MOVEMENT_CANNOT_BE_USED,
14814 CURSOR_MOVEMENT_MUST_SCROLL,
14815 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14816 };
14817
14818 static int
14819 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14820 {
14821 struct window *w = XWINDOW (window);
14822 struct frame *f = XFRAME (w->frame);
14823 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14824
14825 #ifdef GLYPH_DEBUG
14826 if (inhibit_try_cursor_movement)
14827 return rc;
14828 #endif
14829
14830 /* Previously, there was a check for Lisp integer in the
14831 if-statement below. Now, this field is converted to
14832 ptrdiff_t, thus zero means invalid position in a buffer. */
14833 eassert (w->last_point > 0);
14834
14835 /* Handle case where text has not changed, only point, and it has
14836 not moved off the frame. */
14837 if (/* Point may be in this window. */
14838 PT >= CHARPOS (startp)
14839 /* Selective display hasn't changed. */
14840 && !current_buffer->clip_changed
14841 /* Function force-mode-line-update is used to force a thorough
14842 redisplay. It sets either windows_or_buffers_changed or
14843 update_mode_lines. So don't take a shortcut here for these
14844 cases. */
14845 && !update_mode_lines
14846 && !windows_or_buffers_changed
14847 && !cursor_type_changed
14848 /* Can't use this case if highlighting a region. When a
14849 region exists, cursor movement has to do more than just
14850 set the cursor. */
14851 && markpos_of_region () < 0
14852 && !w->region_showing
14853 && NILP (Vshow_trailing_whitespace)
14854 /* This code is not used for mini-buffer for the sake of the case
14855 of redisplaying to replace an echo area message; since in
14856 that case the mini-buffer contents per se are usually
14857 unchanged. This code is of no real use in the mini-buffer
14858 since the handling of this_line_start_pos, etc., in redisplay
14859 handles the same cases. */
14860 && !EQ (window, minibuf_window)
14861 /* When splitting windows or for new windows, it happens that
14862 redisplay is called with a nil window_end_vpos or one being
14863 larger than the window. This should really be fixed in
14864 window.c. I don't have this on my list, now, so we do
14865 approximately the same as the old redisplay code. --gerd. */
14866 && INTEGERP (w->window_end_vpos)
14867 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14868 && (FRAME_WINDOW_P (f)
14869 || !overlay_arrow_in_current_buffer_p ()))
14870 {
14871 int this_scroll_margin, top_scroll_margin;
14872 struct glyph_row *row = NULL;
14873
14874 #ifdef GLYPH_DEBUG
14875 debug_method_add (w, "cursor movement");
14876 #endif
14877
14878 /* Scroll if point within this distance from the top or bottom
14879 of the window. This is a pixel value. */
14880 if (scroll_margin > 0)
14881 {
14882 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14883 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14884 }
14885 else
14886 this_scroll_margin = 0;
14887
14888 top_scroll_margin = this_scroll_margin;
14889 if (WINDOW_WANTS_HEADER_LINE_P (w))
14890 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14891
14892 /* Start with the row the cursor was displayed during the last
14893 not paused redisplay. Give up if that row is not valid. */
14894 if (w->last_cursor.vpos < 0
14895 || w->last_cursor.vpos >= w->current_matrix->nrows)
14896 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14897 else
14898 {
14899 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14900 if (row->mode_line_p)
14901 ++row;
14902 if (!row->enabled_p)
14903 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14904 }
14905
14906 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14907 {
14908 int scroll_p = 0, must_scroll = 0;
14909 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14910
14911 if (PT > w->last_point)
14912 {
14913 /* Point has moved forward. */
14914 while (MATRIX_ROW_END_CHARPOS (row) < PT
14915 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14916 {
14917 eassert (row->enabled_p);
14918 ++row;
14919 }
14920
14921 /* If the end position of a row equals the start
14922 position of the next row, and PT is at that position,
14923 we would rather display cursor in the next line. */
14924 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14925 && MATRIX_ROW_END_CHARPOS (row) == PT
14926 && row < w->current_matrix->rows
14927 + w->current_matrix->nrows - 1
14928 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14929 && !cursor_row_p (row))
14930 ++row;
14931
14932 /* If within the scroll margin, scroll. Note that
14933 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14934 the next line would be drawn, and that
14935 this_scroll_margin can be zero. */
14936 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14937 || PT > MATRIX_ROW_END_CHARPOS (row)
14938 /* Line is completely visible last line in window
14939 and PT is to be set in the next line. */
14940 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14941 && PT == MATRIX_ROW_END_CHARPOS (row)
14942 && !row->ends_at_zv_p
14943 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14944 scroll_p = 1;
14945 }
14946 else if (PT < w->last_point)
14947 {
14948 /* Cursor has to be moved backward. Note that PT >=
14949 CHARPOS (startp) because of the outer if-statement. */
14950 while (!row->mode_line_p
14951 && (MATRIX_ROW_START_CHARPOS (row) > PT
14952 || (MATRIX_ROW_START_CHARPOS (row) == PT
14953 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
14954 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
14955 row > w->current_matrix->rows
14956 && (row-1)->ends_in_newline_from_string_p))))
14957 && (row->y > top_scroll_margin
14958 || CHARPOS (startp) == BEGV))
14959 {
14960 eassert (row->enabled_p);
14961 --row;
14962 }
14963
14964 /* Consider the following case: Window starts at BEGV,
14965 there is invisible, intangible text at BEGV, so that
14966 display starts at some point START > BEGV. It can
14967 happen that we are called with PT somewhere between
14968 BEGV and START. Try to handle that case. */
14969 if (row < w->current_matrix->rows
14970 || row->mode_line_p)
14971 {
14972 row = w->current_matrix->rows;
14973 if (row->mode_line_p)
14974 ++row;
14975 }
14976
14977 /* Due to newlines in overlay strings, we may have to
14978 skip forward over overlay strings. */
14979 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14980 && MATRIX_ROW_END_CHARPOS (row) == PT
14981 && !cursor_row_p (row))
14982 ++row;
14983
14984 /* If within the scroll margin, scroll. */
14985 if (row->y < top_scroll_margin
14986 && CHARPOS (startp) != BEGV)
14987 scroll_p = 1;
14988 }
14989 else
14990 {
14991 /* Cursor did not move. So don't scroll even if cursor line
14992 is partially visible, as it was so before. */
14993 rc = CURSOR_MOVEMENT_SUCCESS;
14994 }
14995
14996 if (PT < MATRIX_ROW_START_CHARPOS (row)
14997 || PT > MATRIX_ROW_END_CHARPOS (row))
14998 {
14999 /* if PT is not in the glyph row, give up. */
15000 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15001 must_scroll = 1;
15002 }
15003 else if (rc != CURSOR_MOVEMENT_SUCCESS
15004 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15005 {
15006 struct glyph_row *row1;
15007
15008 /* If rows are bidi-reordered and point moved, back up
15009 until we find a row that does not belong to a
15010 continuation line. This is because we must consider
15011 all rows of a continued line as candidates for the
15012 new cursor positioning, since row start and end
15013 positions change non-linearly with vertical position
15014 in such rows. */
15015 /* FIXME: Revisit this when glyph ``spilling'' in
15016 continuation lines' rows is implemented for
15017 bidi-reordered rows. */
15018 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15019 MATRIX_ROW_CONTINUATION_LINE_P (row);
15020 --row)
15021 {
15022 /* If we hit the beginning of the displayed portion
15023 without finding the first row of a continued
15024 line, give up. */
15025 if (row <= row1)
15026 {
15027 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15028 break;
15029 }
15030 eassert (row->enabled_p);
15031 }
15032 }
15033 if (must_scroll)
15034 ;
15035 else if (rc != CURSOR_MOVEMENT_SUCCESS
15036 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15037 /* Make sure this isn't a header line by any chance, since
15038 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15039 && !row->mode_line_p
15040 && make_cursor_line_fully_visible_p)
15041 {
15042 if (PT == MATRIX_ROW_END_CHARPOS (row)
15043 && !row->ends_at_zv_p
15044 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15045 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15046 else if (row->height > window_box_height (w))
15047 {
15048 /* If we end up in a partially visible line, let's
15049 make it fully visible, except when it's taller
15050 than the window, in which case we can't do much
15051 about it. */
15052 *scroll_step = 1;
15053 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15054 }
15055 else
15056 {
15057 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15058 if (!cursor_row_fully_visible_p (w, 0, 1))
15059 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15060 else
15061 rc = CURSOR_MOVEMENT_SUCCESS;
15062 }
15063 }
15064 else if (scroll_p)
15065 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15066 else if (rc != CURSOR_MOVEMENT_SUCCESS
15067 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15068 {
15069 /* With bidi-reordered rows, there could be more than
15070 one candidate row whose start and end positions
15071 occlude point. We need to let set_cursor_from_row
15072 find the best candidate. */
15073 /* FIXME: Revisit this when glyph ``spilling'' in
15074 continuation lines' rows is implemented for
15075 bidi-reordered rows. */
15076 int rv = 0;
15077
15078 do
15079 {
15080 int at_zv_p = 0, exact_match_p = 0;
15081
15082 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15083 && PT <= MATRIX_ROW_END_CHARPOS (row)
15084 && cursor_row_p (row))
15085 rv |= set_cursor_from_row (w, row, w->current_matrix,
15086 0, 0, 0, 0);
15087 /* As soon as we've found the exact match for point,
15088 or the first suitable row whose ends_at_zv_p flag
15089 is set, we are done. */
15090 at_zv_p =
15091 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15092 if (rv && !at_zv_p
15093 && w->cursor.hpos >= 0
15094 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15095 w->cursor.vpos))
15096 {
15097 struct glyph_row *candidate =
15098 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15099 struct glyph *g =
15100 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15101 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15102
15103 exact_match_p =
15104 (BUFFERP (g->object) && g->charpos == PT)
15105 || (INTEGERP (g->object)
15106 && (g->charpos == PT
15107 || (g->charpos == 0 && endpos - 1 == PT)));
15108 }
15109 if (rv && (at_zv_p || exact_match_p))
15110 {
15111 rc = CURSOR_MOVEMENT_SUCCESS;
15112 break;
15113 }
15114 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15115 break;
15116 ++row;
15117 }
15118 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15119 || row->continued_p)
15120 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15121 || (MATRIX_ROW_START_CHARPOS (row) == PT
15122 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15123 /* If we didn't find any candidate rows, or exited the
15124 loop before all the candidates were examined, signal
15125 to the caller that this method failed. */
15126 if (rc != CURSOR_MOVEMENT_SUCCESS
15127 && !(rv
15128 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15129 && !row->continued_p))
15130 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15131 else if (rv)
15132 rc = CURSOR_MOVEMENT_SUCCESS;
15133 }
15134 else
15135 {
15136 do
15137 {
15138 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15139 {
15140 rc = CURSOR_MOVEMENT_SUCCESS;
15141 break;
15142 }
15143 ++row;
15144 }
15145 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15146 && MATRIX_ROW_START_CHARPOS (row) == PT
15147 && cursor_row_p (row));
15148 }
15149 }
15150 }
15151
15152 return rc;
15153 }
15154
15155 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15156 static
15157 #endif
15158 void
15159 set_vertical_scroll_bar (struct window *w)
15160 {
15161 ptrdiff_t start, end, whole;
15162
15163 /* Calculate the start and end positions for the current window.
15164 At some point, it would be nice to choose between scrollbars
15165 which reflect the whole buffer size, with special markers
15166 indicating narrowing, and scrollbars which reflect only the
15167 visible region.
15168
15169 Note that mini-buffers sometimes aren't displaying any text. */
15170 if (!MINI_WINDOW_P (w)
15171 || (w == XWINDOW (minibuf_window)
15172 && NILP (echo_area_buffer[0])))
15173 {
15174 struct buffer *buf = XBUFFER (w->buffer);
15175 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15176 start = marker_position (w->start) - BUF_BEGV (buf);
15177 /* I don't think this is guaranteed to be right. For the
15178 moment, we'll pretend it is. */
15179 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15180
15181 if (end < start)
15182 end = start;
15183 if (whole < (end - start))
15184 whole = end - start;
15185 }
15186 else
15187 start = end = whole = 0;
15188
15189 /* Indicate what this scroll bar ought to be displaying now. */
15190 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15191 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15192 (w, end - start, whole, start);
15193 }
15194
15195
15196 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15197 selected_window is redisplayed.
15198
15199 We can return without actually redisplaying the window if
15200 fonts_changed_p. In that case, redisplay_internal will
15201 retry. */
15202
15203 static void
15204 redisplay_window (Lisp_Object window, int just_this_one_p)
15205 {
15206 struct window *w = XWINDOW (window);
15207 struct frame *f = XFRAME (w->frame);
15208 struct buffer *buffer = XBUFFER (w->buffer);
15209 struct buffer *old = current_buffer;
15210 struct text_pos lpoint, opoint, startp;
15211 int update_mode_line;
15212 int tem;
15213 struct it it;
15214 /* Record it now because it's overwritten. */
15215 int current_matrix_up_to_date_p = 0;
15216 int used_current_matrix_p = 0;
15217 /* This is less strict than current_matrix_up_to_date_p.
15218 It indicates that the buffer contents and narrowing are unchanged. */
15219 int buffer_unchanged_p = 0;
15220 int temp_scroll_step = 0;
15221 ptrdiff_t count = SPECPDL_INDEX ();
15222 int rc;
15223 int centering_position = -1;
15224 int last_line_misfit = 0;
15225 ptrdiff_t beg_unchanged, end_unchanged;
15226
15227 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15228 opoint = lpoint;
15229
15230 /* W must be a leaf window here. */
15231 eassert (!NILP (w->buffer));
15232 #ifdef GLYPH_DEBUG
15233 *w->desired_matrix->method = 0;
15234 #endif
15235
15236 restart:
15237 reconsider_clip_changes (w, buffer);
15238
15239 /* Has the mode line to be updated? */
15240 update_mode_line = (w->update_mode_line
15241 || update_mode_lines
15242 || buffer->clip_changed
15243 || buffer->prevent_redisplay_optimizations_p);
15244
15245 if (MINI_WINDOW_P (w))
15246 {
15247 if (w == XWINDOW (echo_area_window)
15248 && !NILP (echo_area_buffer[0]))
15249 {
15250 if (update_mode_line)
15251 /* We may have to update a tty frame's menu bar or a
15252 tool-bar. Example `M-x C-h C-h C-g'. */
15253 goto finish_menu_bars;
15254 else
15255 /* We've already displayed the echo area glyphs in this window. */
15256 goto finish_scroll_bars;
15257 }
15258 else if ((w != XWINDOW (minibuf_window)
15259 || minibuf_level == 0)
15260 /* When buffer is nonempty, redisplay window normally. */
15261 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15262 /* Quail displays non-mini buffers in minibuffer window.
15263 In that case, redisplay the window normally. */
15264 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15265 {
15266 /* W is a mini-buffer window, but it's not active, so clear
15267 it. */
15268 int yb = window_text_bottom_y (w);
15269 struct glyph_row *row;
15270 int y;
15271
15272 for (y = 0, row = w->desired_matrix->rows;
15273 y < yb;
15274 y += row->height, ++row)
15275 blank_row (w, row, y);
15276 goto finish_scroll_bars;
15277 }
15278
15279 clear_glyph_matrix (w->desired_matrix);
15280 }
15281
15282 /* Otherwise set up data on this window; select its buffer and point
15283 value. */
15284 /* Really select the buffer, for the sake of buffer-local
15285 variables. */
15286 set_buffer_internal_1 (XBUFFER (w->buffer));
15287
15288 current_matrix_up_to_date_p
15289 = (w->window_end_valid
15290 && !current_buffer->clip_changed
15291 && !current_buffer->prevent_redisplay_optimizations_p
15292 && !window_outdated (w));
15293
15294 /* Run the window-bottom-change-functions
15295 if it is possible that the text on the screen has changed
15296 (either due to modification of the text, or any other reason). */
15297 if (!current_matrix_up_to_date_p
15298 && !NILP (Vwindow_text_change_functions))
15299 {
15300 safe_run_hooks (Qwindow_text_change_functions);
15301 goto restart;
15302 }
15303
15304 beg_unchanged = BEG_UNCHANGED;
15305 end_unchanged = END_UNCHANGED;
15306
15307 SET_TEXT_POS (opoint, PT, PT_BYTE);
15308
15309 specbind (Qinhibit_point_motion_hooks, Qt);
15310
15311 buffer_unchanged_p
15312 = (w->window_end_valid
15313 && !current_buffer->clip_changed
15314 && !window_outdated (w));
15315
15316 /* When windows_or_buffers_changed is non-zero, we can't rely on
15317 the window end being valid, so set it to nil there. */
15318 if (windows_or_buffers_changed)
15319 {
15320 /* If window starts on a continuation line, maybe adjust the
15321 window start in case the window's width changed. */
15322 if (XMARKER (w->start)->buffer == current_buffer)
15323 compute_window_start_on_continuation_line (w);
15324
15325 w->window_end_valid = 0;
15326 }
15327
15328 /* Some sanity checks. */
15329 CHECK_WINDOW_END (w);
15330 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15331 emacs_abort ();
15332 if (BYTEPOS (opoint) < CHARPOS (opoint))
15333 emacs_abort ();
15334
15335 if (mode_line_update_needed (w))
15336 update_mode_line = 1;
15337
15338 /* Point refers normally to the selected window. For any other
15339 window, set up appropriate value. */
15340 if (!EQ (window, selected_window))
15341 {
15342 ptrdiff_t new_pt = marker_position (w->pointm);
15343 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15344 if (new_pt < BEGV)
15345 {
15346 new_pt = BEGV;
15347 new_pt_byte = BEGV_BYTE;
15348 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15349 }
15350 else if (new_pt > (ZV - 1))
15351 {
15352 new_pt = ZV;
15353 new_pt_byte = ZV_BYTE;
15354 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15355 }
15356
15357 /* We don't use SET_PT so that the point-motion hooks don't run. */
15358 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15359 }
15360
15361 /* If any of the character widths specified in the display table
15362 have changed, invalidate the width run cache. It's true that
15363 this may be a bit late to catch such changes, but the rest of
15364 redisplay goes (non-fatally) haywire when the display table is
15365 changed, so why should we worry about doing any better? */
15366 if (current_buffer->width_run_cache)
15367 {
15368 struct Lisp_Char_Table *disptab = buffer_display_table ();
15369
15370 if (! disptab_matches_widthtab
15371 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15372 {
15373 invalidate_region_cache (current_buffer,
15374 current_buffer->width_run_cache,
15375 BEG, Z);
15376 recompute_width_table (current_buffer, disptab);
15377 }
15378 }
15379
15380 /* If window-start is screwed up, choose a new one. */
15381 if (XMARKER (w->start)->buffer != current_buffer)
15382 goto recenter;
15383
15384 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15385
15386 /* If someone specified a new starting point but did not insist,
15387 check whether it can be used. */
15388 if (w->optional_new_start
15389 && CHARPOS (startp) >= BEGV
15390 && CHARPOS (startp) <= ZV)
15391 {
15392 w->optional_new_start = 0;
15393 start_display (&it, w, startp);
15394 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15395 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15396 if (IT_CHARPOS (it) == PT)
15397 w->force_start = 1;
15398 /* IT may overshoot PT if text at PT is invisible. */
15399 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15400 w->force_start = 1;
15401 }
15402
15403 force_start:
15404
15405 /* Handle case where place to start displaying has been specified,
15406 unless the specified location is outside the accessible range. */
15407 if (w->force_start || w->frozen_window_start_p)
15408 {
15409 /* We set this later on if we have to adjust point. */
15410 int new_vpos = -1;
15411
15412 w->force_start = 0;
15413 w->vscroll = 0;
15414 w->window_end_valid = 0;
15415
15416 /* Forget any recorded base line for line number display. */
15417 if (!buffer_unchanged_p)
15418 w->base_line_number = 0;
15419
15420 /* Redisplay the mode line. Select the buffer properly for that.
15421 Also, run the hook window-scroll-functions
15422 because we have scrolled. */
15423 /* Note, we do this after clearing force_start because
15424 if there's an error, it is better to forget about force_start
15425 than to get into an infinite loop calling the hook functions
15426 and having them get more errors. */
15427 if (!update_mode_line
15428 || ! NILP (Vwindow_scroll_functions))
15429 {
15430 update_mode_line = 1;
15431 w->update_mode_line = 1;
15432 startp = run_window_scroll_functions (window, startp);
15433 }
15434
15435 w->last_modified = 0;
15436 w->last_overlay_modified = 0;
15437 if (CHARPOS (startp) < BEGV)
15438 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15439 else if (CHARPOS (startp) > ZV)
15440 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15441
15442 /* Redisplay, then check if cursor has been set during the
15443 redisplay. Give up if new fonts were loaded. */
15444 /* We used to issue a CHECK_MARGINS argument to try_window here,
15445 but this causes scrolling to fail when point begins inside
15446 the scroll margin (bug#148) -- cyd */
15447 if (!try_window (window, startp, 0))
15448 {
15449 w->force_start = 1;
15450 clear_glyph_matrix (w->desired_matrix);
15451 goto need_larger_matrices;
15452 }
15453
15454 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15455 {
15456 /* If point does not appear, try to move point so it does
15457 appear. The desired matrix has been built above, so we
15458 can use it here. */
15459 new_vpos = window_box_height (w) / 2;
15460 }
15461
15462 if (!cursor_row_fully_visible_p (w, 0, 0))
15463 {
15464 /* Point does appear, but on a line partly visible at end of window.
15465 Move it back to a fully-visible line. */
15466 new_vpos = window_box_height (w);
15467 }
15468 else if (w->cursor.vpos >=0)
15469 {
15470 /* Some people insist on not letting point enter the scroll
15471 margin, even though this part handles windows that didn't
15472 scroll at all. */
15473 int margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15474 int pixel_margin = margin * FRAME_LINE_HEIGHT (f);
15475 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15476
15477 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15478 below, which finds the row to move point to, advances by
15479 the Y coordinate of the _next_ row, see the definition of
15480 MATRIX_ROW_BOTTOM_Y. */
15481 if (w->cursor.vpos < margin + header_line)
15482 new_vpos
15483 = pixel_margin + (header_line
15484 ? CURRENT_HEADER_LINE_HEIGHT (w)
15485 : 0) + FRAME_LINE_HEIGHT (f);
15486 else
15487 {
15488 int window_height = window_box_height (w);
15489
15490 if (header_line)
15491 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15492 if (w->cursor.y >= window_height - pixel_margin)
15493 new_vpos = window_height - pixel_margin;
15494 }
15495 }
15496
15497 /* If we need to move point for either of the above reasons,
15498 now actually do it. */
15499 if (new_vpos >= 0)
15500 {
15501 struct glyph_row *row;
15502
15503 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15504 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15505 ++row;
15506
15507 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15508 MATRIX_ROW_START_BYTEPOS (row));
15509
15510 if (w != XWINDOW (selected_window))
15511 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15512 else if (current_buffer == old)
15513 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15514
15515 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15516
15517 /* If we are highlighting the region, then we just changed
15518 the region, so redisplay to show it. */
15519 if (0 <= markpos_of_region ())
15520 {
15521 clear_glyph_matrix (w->desired_matrix);
15522 if (!try_window (window, startp, 0))
15523 goto need_larger_matrices;
15524 }
15525 }
15526
15527 #ifdef GLYPH_DEBUG
15528 debug_method_add (w, "forced window start");
15529 #endif
15530 goto done;
15531 }
15532
15533 /* Handle case where text has not changed, only point, and it has
15534 not moved off the frame, and we are not retrying after hscroll.
15535 (current_matrix_up_to_date_p is nonzero when retrying.) */
15536 if (current_matrix_up_to_date_p
15537 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15538 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15539 {
15540 switch (rc)
15541 {
15542 case CURSOR_MOVEMENT_SUCCESS:
15543 used_current_matrix_p = 1;
15544 goto done;
15545
15546 case CURSOR_MOVEMENT_MUST_SCROLL:
15547 goto try_to_scroll;
15548
15549 default:
15550 emacs_abort ();
15551 }
15552 }
15553 /* If current starting point was originally the beginning of a line
15554 but no longer is, find a new starting point. */
15555 else if (w->start_at_line_beg
15556 && !(CHARPOS (startp) <= BEGV
15557 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15558 {
15559 #ifdef GLYPH_DEBUG
15560 debug_method_add (w, "recenter 1");
15561 #endif
15562 goto recenter;
15563 }
15564
15565 /* Try scrolling with try_window_id. Value is > 0 if update has
15566 been done, it is -1 if we know that the same window start will
15567 not work. It is 0 if unsuccessful for some other reason. */
15568 else if ((tem = try_window_id (w)) != 0)
15569 {
15570 #ifdef GLYPH_DEBUG
15571 debug_method_add (w, "try_window_id %d", tem);
15572 #endif
15573
15574 if (fonts_changed_p)
15575 goto need_larger_matrices;
15576 if (tem > 0)
15577 goto done;
15578
15579 /* Otherwise try_window_id has returned -1 which means that we
15580 don't want the alternative below this comment to execute. */
15581 }
15582 else if (CHARPOS (startp) >= BEGV
15583 && CHARPOS (startp) <= ZV
15584 && PT >= CHARPOS (startp)
15585 && (CHARPOS (startp) < ZV
15586 /* Avoid starting at end of buffer. */
15587 || CHARPOS (startp) == BEGV
15588 || !window_outdated (w)))
15589 {
15590 int d1, d2, d3, d4, d5, d6;
15591
15592 /* If first window line is a continuation line, and window start
15593 is inside the modified region, but the first change is before
15594 current window start, we must select a new window start.
15595
15596 However, if this is the result of a down-mouse event (e.g. by
15597 extending the mouse-drag-overlay), we don't want to select a
15598 new window start, since that would change the position under
15599 the mouse, resulting in an unwanted mouse-movement rather
15600 than a simple mouse-click. */
15601 if (!w->start_at_line_beg
15602 && NILP (do_mouse_tracking)
15603 && CHARPOS (startp) > BEGV
15604 && CHARPOS (startp) > BEG + beg_unchanged
15605 && CHARPOS (startp) <= Z - end_unchanged
15606 /* Even if w->start_at_line_beg is nil, a new window may
15607 start at a line_beg, since that's how set_buffer_window
15608 sets it. So, we need to check the return value of
15609 compute_window_start_on_continuation_line. (See also
15610 bug#197). */
15611 && XMARKER (w->start)->buffer == current_buffer
15612 && compute_window_start_on_continuation_line (w)
15613 /* It doesn't make sense to force the window start like we
15614 do at label force_start if it is already known that point
15615 will not be visible in the resulting window, because
15616 doing so will move point from its correct position
15617 instead of scrolling the window to bring point into view.
15618 See bug#9324. */
15619 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15620 {
15621 w->force_start = 1;
15622 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15623 goto force_start;
15624 }
15625
15626 #ifdef GLYPH_DEBUG
15627 debug_method_add (w, "same window start");
15628 #endif
15629
15630 /* Try to redisplay starting at same place as before.
15631 If point has not moved off frame, accept the results. */
15632 if (!current_matrix_up_to_date_p
15633 /* Don't use try_window_reusing_current_matrix in this case
15634 because a window scroll function can have changed the
15635 buffer. */
15636 || !NILP (Vwindow_scroll_functions)
15637 || MINI_WINDOW_P (w)
15638 || !(used_current_matrix_p
15639 = try_window_reusing_current_matrix (w)))
15640 {
15641 IF_DEBUG (debug_method_add (w, "1"));
15642 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15643 /* -1 means we need to scroll.
15644 0 means we need new matrices, but fonts_changed_p
15645 is set in that case, so we will detect it below. */
15646 goto try_to_scroll;
15647 }
15648
15649 if (fonts_changed_p)
15650 goto need_larger_matrices;
15651
15652 if (w->cursor.vpos >= 0)
15653 {
15654 if (!just_this_one_p
15655 || current_buffer->clip_changed
15656 || BEG_UNCHANGED < CHARPOS (startp))
15657 /* Forget any recorded base line for line number display. */
15658 w->base_line_number = 0;
15659
15660 if (!cursor_row_fully_visible_p (w, 1, 0))
15661 {
15662 clear_glyph_matrix (w->desired_matrix);
15663 last_line_misfit = 1;
15664 }
15665 /* Drop through and scroll. */
15666 else
15667 goto done;
15668 }
15669 else
15670 clear_glyph_matrix (w->desired_matrix);
15671 }
15672
15673 try_to_scroll:
15674
15675 w->last_modified = 0;
15676 w->last_overlay_modified = 0;
15677
15678 /* Redisplay the mode line. Select the buffer properly for that. */
15679 if (!update_mode_line)
15680 {
15681 update_mode_line = 1;
15682 w->update_mode_line = 1;
15683 }
15684
15685 /* Try to scroll by specified few lines. */
15686 if ((scroll_conservatively
15687 || emacs_scroll_step
15688 || temp_scroll_step
15689 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15690 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15691 && CHARPOS (startp) >= BEGV
15692 && CHARPOS (startp) <= ZV)
15693 {
15694 /* The function returns -1 if new fonts were loaded, 1 if
15695 successful, 0 if not successful. */
15696 int ss = try_scrolling (window, just_this_one_p,
15697 scroll_conservatively,
15698 emacs_scroll_step,
15699 temp_scroll_step, last_line_misfit);
15700 switch (ss)
15701 {
15702 case SCROLLING_SUCCESS:
15703 goto done;
15704
15705 case SCROLLING_NEED_LARGER_MATRICES:
15706 goto need_larger_matrices;
15707
15708 case SCROLLING_FAILED:
15709 break;
15710
15711 default:
15712 emacs_abort ();
15713 }
15714 }
15715
15716 /* Finally, just choose a place to start which positions point
15717 according to user preferences. */
15718
15719 recenter:
15720
15721 #ifdef GLYPH_DEBUG
15722 debug_method_add (w, "recenter");
15723 #endif
15724
15725 /* Forget any previously recorded base line for line number display. */
15726 if (!buffer_unchanged_p)
15727 w->base_line_number = 0;
15728
15729 /* Determine the window start relative to point. */
15730 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15731 it.current_y = it.last_visible_y;
15732 if (centering_position < 0)
15733 {
15734 int margin =
15735 scroll_margin > 0
15736 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15737 : 0;
15738 ptrdiff_t margin_pos = CHARPOS (startp);
15739 Lisp_Object aggressive;
15740 int scrolling_up;
15741
15742 /* If there is a scroll margin at the top of the window, find
15743 its character position. */
15744 if (margin
15745 /* Cannot call start_display if startp is not in the
15746 accessible region of the buffer. This can happen when we
15747 have just switched to a different buffer and/or changed
15748 its restriction. In that case, startp is initialized to
15749 the character position 1 (BEGV) because we did not yet
15750 have chance to display the buffer even once. */
15751 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15752 {
15753 struct it it1;
15754 void *it1data = NULL;
15755
15756 SAVE_IT (it1, it, it1data);
15757 start_display (&it1, w, startp);
15758 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15759 margin_pos = IT_CHARPOS (it1);
15760 RESTORE_IT (&it, &it, it1data);
15761 }
15762 scrolling_up = PT > margin_pos;
15763 aggressive =
15764 scrolling_up
15765 ? BVAR (current_buffer, scroll_up_aggressively)
15766 : BVAR (current_buffer, scroll_down_aggressively);
15767
15768 if (!MINI_WINDOW_P (w)
15769 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15770 {
15771 int pt_offset = 0;
15772
15773 /* Setting scroll-conservatively overrides
15774 scroll-*-aggressively. */
15775 if (!scroll_conservatively && NUMBERP (aggressive))
15776 {
15777 double float_amount = XFLOATINT (aggressive);
15778
15779 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15780 if (pt_offset == 0 && float_amount > 0)
15781 pt_offset = 1;
15782 if (pt_offset && margin > 0)
15783 margin -= 1;
15784 }
15785 /* Compute how much to move the window start backward from
15786 point so that point will be displayed where the user
15787 wants it. */
15788 if (scrolling_up)
15789 {
15790 centering_position = it.last_visible_y;
15791 if (pt_offset)
15792 centering_position -= pt_offset;
15793 centering_position -=
15794 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15795 + WINDOW_HEADER_LINE_HEIGHT (w);
15796 /* Don't let point enter the scroll margin near top of
15797 the window. */
15798 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15799 centering_position = margin * FRAME_LINE_HEIGHT (f);
15800 }
15801 else
15802 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15803 }
15804 else
15805 /* Set the window start half the height of the window backward
15806 from point. */
15807 centering_position = window_box_height (w) / 2;
15808 }
15809 move_it_vertically_backward (&it, centering_position);
15810
15811 eassert (IT_CHARPOS (it) >= BEGV);
15812
15813 /* The function move_it_vertically_backward may move over more
15814 than the specified y-distance. If it->w is small, e.g. a
15815 mini-buffer window, we may end up in front of the window's
15816 display area. Start displaying at the start of the line
15817 containing PT in this case. */
15818 if (it.current_y <= 0)
15819 {
15820 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15821 move_it_vertically_backward (&it, 0);
15822 it.current_y = 0;
15823 }
15824
15825 it.current_x = it.hpos = 0;
15826
15827 /* Set the window start position here explicitly, to avoid an
15828 infinite loop in case the functions in window-scroll-functions
15829 get errors. */
15830 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15831
15832 /* Run scroll hooks. */
15833 startp = run_window_scroll_functions (window, it.current.pos);
15834
15835 /* Redisplay the window. */
15836 if (!current_matrix_up_to_date_p
15837 || windows_or_buffers_changed
15838 || cursor_type_changed
15839 /* Don't use try_window_reusing_current_matrix in this case
15840 because it can have changed the buffer. */
15841 || !NILP (Vwindow_scroll_functions)
15842 || !just_this_one_p
15843 || MINI_WINDOW_P (w)
15844 || !(used_current_matrix_p
15845 = try_window_reusing_current_matrix (w)))
15846 try_window (window, startp, 0);
15847
15848 /* If new fonts have been loaded (due to fontsets), give up. We
15849 have to start a new redisplay since we need to re-adjust glyph
15850 matrices. */
15851 if (fonts_changed_p)
15852 goto need_larger_matrices;
15853
15854 /* If cursor did not appear assume that the middle of the window is
15855 in the first line of the window. Do it again with the next line.
15856 (Imagine a window of height 100, displaying two lines of height
15857 60. Moving back 50 from it->last_visible_y will end in the first
15858 line.) */
15859 if (w->cursor.vpos < 0)
15860 {
15861 if (w->window_end_valid && PT >= Z - XFASTINT (w->window_end_pos))
15862 {
15863 clear_glyph_matrix (w->desired_matrix);
15864 move_it_by_lines (&it, 1);
15865 try_window (window, it.current.pos, 0);
15866 }
15867 else if (PT < IT_CHARPOS (it))
15868 {
15869 clear_glyph_matrix (w->desired_matrix);
15870 move_it_by_lines (&it, -1);
15871 try_window (window, it.current.pos, 0);
15872 }
15873 else
15874 {
15875 /* Not much we can do about it. */
15876 }
15877 }
15878
15879 /* Consider the following case: Window starts at BEGV, there is
15880 invisible, intangible text at BEGV, so that display starts at
15881 some point START > BEGV. It can happen that we are called with
15882 PT somewhere between BEGV and START. Try to handle that case. */
15883 if (w->cursor.vpos < 0)
15884 {
15885 struct glyph_row *row = w->current_matrix->rows;
15886 if (row->mode_line_p)
15887 ++row;
15888 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15889 }
15890
15891 if (!cursor_row_fully_visible_p (w, 0, 0))
15892 {
15893 /* If vscroll is enabled, disable it and try again. */
15894 if (w->vscroll)
15895 {
15896 w->vscroll = 0;
15897 clear_glyph_matrix (w->desired_matrix);
15898 goto recenter;
15899 }
15900
15901 /* Users who set scroll-conservatively to a large number want
15902 point just above/below the scroll margin. If we ended up
15903 with point's row partially visible, move the window start to
15904 make that row fully visible and out of the margin. */
15905 if (scroll_conservatively > SCROLL_LIMIT)
15906 {
15907 int margin =
15908 scroll_margin > 0
15909 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15910 : 0;
15911 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15912
15913 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15914 clear_glyph_matrix (w->desired_matrix);
15915 if (1 == try_window (window, it.current.pos,
15916 TRY_WINDOW_CHECK_MARGINS))
15917 goto done;
15918 }
15919
15920 /* If centering point failed to make the whole line visible,
15921 put point at the top instead. That has to make the whole line
15922 visible, if it can be done. */
15923 if (centering_position == 0)
15924 goto done;
15925
15926 clear_glyph_matrix (w->desired_matrix);
15927 centering_position = 0;
15928 goto recenter;
15929 }
15930
15931 done:
15932
15933 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15934 w->start_at_line_beg = (CHARPOS (startp) == BEGV
15935 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
15936
15937 /* Display the mode line, if we must. */
15938 if ((update_mode_line
15939 /* If window not full width, must redo its mode line
15940 if (a) the window to its side is being redone and
15941 (b) we do a frame-based redisplay. This is a consequence
15942 of how inverted lines are drawn in frame-based redisplay. */
15943 || (!just_this_one_p
15944 && !FRAME_WINDOW_P (f)
15945 && !WINDOW_FULL_WIDTH_P (w))
15946 /* Line number to display. */
15947 || w->base_line_pos > 0
15948 /* Column number is displayed and different from the one displayed. */
15949 || (w->column_number_displayed != -1
15950 && (w->column_number_displayed != current_column ())))
15951 /* This means that the window has a mode line. */
15952 && (WINDOW_WANTS_MODELINE_P (w)
15953 || WINDOW_WANTS_HEADER_LINE_P (w)))
15954 {
15955 display_mode_lines (w);
15956
15957 /* If mode line height has changed, arrange for a thorough
15958 immediate redisplay using the correct mode line height. */
15959 if (WINDOW_WANTS_MODELINE_P (w)
15960 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
15961 {
15962 fonts_changed_p = 1;
15963 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
15964 = DESIRED_MODE_LINE_HEIGHT (w);
15965 }
15966
15967 /* If header line height has changed, arrange for a thorough
15968 immediate redisplay using the correct header line height. */
15969 if (WINDOW_WANTS_HEADER_LINE_P (w)
15970 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
15971 {
15972 fonts_changed_p = 1;
15973 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
15974 = DESIRED_HEADER_LINE_HEIGHT (w);
15975 }
15976
15977 if (fonts_changed_p)
15978 goto need_larger_matrices;
15979 }
15980
15981 if (!line_number_displayed && w->base_line_pos != -1)
15982 {
15983 w->base_line_pos = 0;
15984 w->base_line_number = 0;
15985 }
15986
15987 finish_menu_bars:
15988
15989 /* When we reach a frame's selected window, redo the frame's menu bar. */
15990 if (update_mode_line
15991 && EQ (FRAME_SELECTED_WINDOW (f), window))
15992 {
15993 int redisplay_menu_p = 0;
15994
15995 if (FRAME_WINDOW_P (f))
15996 {
15997 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
15998 || defined (HAVE_NS) || defined (USE_GTK)
15999 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16000 #else
16001 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16002 #endif
16003 }
16004 else
16005 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16006
16007 if (redisplay_menu_p)
16008 display_menu_bar (w);
16009
16010 #ifdef HAVE_WINDOW_SYSTEM
16011 if (FRAME_WINDOW_P (f))
16012 {
16013 #if defined (USE_GTK) || defined (HAVE_NS)
16014 if (FRAME_EXTERNAL_TOOL_BAR (f))
16015 redisplay_tool_bar (f);
16016 #else
16017 if (WINDOWP (f->tool_bar_window)
16018 && (FRAME_TOOL_BAR_LINES (f) > 0
16019 || !NILP (Vauto_resize_tool_bars))
16020 && redisplay_tool_bar (f))
16021 ignore_mouse_drag_p = 1;
16022 #endif
16023 }
16024 #endif
16025 }
16026
16027 #ifdef HAVE_WINDOW_SYSTEM
16028 if (FRAME_WINDOW_P (f)
16029 && update_window_fringes (w, (just_this_one_p
16030 || (!used_current_matrix_p && !overlay_arrow_seen)
16031 || w->pseudo_window_p)))
16032 {
16033 update_begin (f);
16034 block_input ();
16035 if (draw_window_fringes (w, 1))
16036 x_draw_vertical_border (w);
16037 unblock_input ();
16038 update_end (f);
16039 }
16040 #endif /* HAVE_WINDOW_SYSTEM */
16041
16042 /* We go to this label, with fonts_changed_p set,
16043 if it is necessary to try again using larger glyph matrices.
16044 We have to redeem the scroll bar even in this case,
16045 because the loop in redisplay_internal expects that. */
16046 need_larger_matrices:
16047 ;
16048 finish_scroll_bars:
16049
16050 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16051 {
16052 /* Set the thumb's position and size. */
16053 set_vertical_scroll_bar (w);
16054
16055 /* Note that we actually used the scroll bar attached to this
16056 window, so it shouldn't be deleted at the end of redisplay. */
16057 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16058 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16059 }
16060
16061 /* Restore current_buffer and value of point in it. The window
16062 update may have changed the buffer, so first make sure `opoint'
16063 is still valid (Bug#6177). */
16064 if (CHARPOS (opoint) < BEGV)
16065 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16066 else if (CHARPOS (opoint) > ZV)
16067 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16068 else
16069 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16070
16071 set_buffer_internal_1 (old);
16072 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16073 shorter. This can be caused by log truncation in *Messages*. */
16074 if (CHARPOS (lpoint) <= ZV)
16075 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16076
16077 unbind_to (count, Qnil);
16078 }
16079
16080
16081 /* Build the complete desired matrix of WINDOW with a window start
16082 buffer position POS.
16083
16084 Value is 1 if successful. It is zero if fonts were loaded during
16085 redisplay which makes re-adjusting glyph matrices necessary, and -1
16086 if point would appear in the scroll margins.
16087 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16088 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16089 set in FLAGS.) */
16090
16091 int
16092 try_window (Lisp_Object window, struct text_pos pos, int flags)
16093 {
16094 struct window *w = XWINDOW (window);
16095 struct it it;
16096 struct glyph_row *last_text_row = NULL;
16097 struct frame *f = XFRAME (w->frame);
16098
16099 /* Make POS the new window start. */
16100 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16101
16102 /* Mark cursor position as unknown. No overlay arrow seen. */
16103 w->cursor.vpos = -1;
16104 overlay_arrow_seen = 0;
16105
16106 /* Initialize iterator and info to start at POS. */
16107 start_display (&it, w, pos);
16108
16109 /* Display all lines of W. */
16110 while (it.current_y < it.last_visible_y)
16111 {
16112 if (display_line (&it))
16113 last_text_row = it.glyph_row - 1;
16114 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16115 return 0;
16116 }
16117
16118 /* Don't let the cursor end in the scroll margins. */
16119 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16120 && !MINI_WINDOW_P (w))
16121 {
16122 int this_scroll_margin;
16123
16124 if (scroll_margin > 0)
16125 {
16126 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16127 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16128 }
16129 else
16130 this_scroll_margin = 0;
16131
16132 if ((w->cursor.y >= 0 /* not vscrolled */
16133 && w->cursor.y < this_scroll_margin
16134 && CHARPOS (pos) > BEGV
16135 && IT_CHARPOS (it) < ZV)
16136 /* rms: considering make_cursor_line_fully_visible_p here
16137 seems to give wrong results. We don't want to recenter
16138 when the last line is partly visible, we want to allow
16139 that case to be handled in the usual way. */
16140 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16141 {
16142 w->cursor.vpos = -1;
16143 clear_glyph_matrix (w->desired_matrix);
16144 return -1;
16145 }
16146 }
16147
16148 /* If bottom moved off end of frame, change mode line percentage. */
16149 if (XFASTINT (w->window_end_pos) <= 0
16150 && Z != IT_CHARPOS (it))
16151 w->update_mode_line = 1;
16152
16153 /* Set window_end_pos to the offset of the last character displayed
16154 on the window from the end of current_buffer. Set
16155 window_end_vpos to its row number. */
16156 if (last_text_row)
16157 {
16158 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16159 w->window_end_bytepos
16160 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16161 wset_window_end_pos
16162 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16163 wset_window_end_vpos
16164 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16165 eassert
16166 (MATRIX_ROW (w->desired_matrix,
16167 XFASTINT (w->window_end_vpos))->displays_text_p);
16168 }
16169 else
16170 {
16171 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16172 wset_window_end_pos (w, make_number (Z - ZV));
16173 wset_window_end_vpos (w, make_number (0));
16174 }
16175
16176 /* But that is not valid info until redisplay finishes. */
16177 w->window_end_valid = 0;
16178 return 1;
16179 }
16180
16181
16182 \f
16183 /************************************************************************
16184 Window redisplay reusing current matrix when buffer has not changed
16185 ************************************************************************/
16186
16187 /* Try redisplay of window W showing an unchanged buffer with a
16188 different window start than the last time it was displayed by
16189 reusing its current matrix. Value is non-zero if successful.
16190 W->start is the new window start. */
16191
16192 static int
16193 try_window_reusing_current_matrix (struct window *w)
16194 {
16195 struct frame *f = XFRAME (w->frame);
16196 struct glyph_row *bottom_row;
16197 struct it it;
16198 struct run run;
16199 struct text_pos start, new_start;
16200 int nrows_scrolled, i;
16201 struct glyph_row *last_text_row;
16202 struct glyph_row *last_reused_text_row;
16203 struct glyph_row *start_row;
16204 int start_vpos, min_y, max_y;
16205
16206 #ifdef GLYPH_DEBUG
16207 if (inhibit_try_window_reusing)
16208 return 0;
16209 #endif
16210
16211 if (/* This function doesn't handle terminal frames. */
16212 !FRAME_WINDOW_P (f)
16213 /* Don't try to reuse the display if windows have been split
16214 or such. */
16215 || windows_or_buffers_changed
16216 || cursor_type_changed)
16217 return 0;
16218
16219 /* Can't do this if region may have changed. */
16220 if (0 <= markpos_of_region ()
16221 || w->region_showing
16222 || !NILP (Vshow_trailing_whitespace))
16223 return 0;
16224
16225 /* If top-line visibility has changed, give up. */
16226 if (WINDOW_WANTS_HEADER_LINE_P (w)
16227 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16228 return 0;
16229
16230 /* Give up if old or new display is scrolled vertically. We could
16231 make this function handle this, but right now it doesn't. */
16232 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16233 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16234 return 0;
16235
16236 /* The variable new_start now holds the new window start. The old
16237 start `start' can be determined from the current matrix. */
16238 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16239 start = start_row->minpos;
16240 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16241
16242 /* Clear the desired matrix for the display below. */
16243 clear_glyph_matrix (w->desired_matrix);
16244
16245 if (CHARPOS (new_start) <= CHARPOS (start))
16246 {
16247 /* Don't use this method if the display starts with an ellipsis
16248 displayed for invisible text. It's not easy to handle that case
16249 below, and it's certainly not worth the effort since this is
16250 not a frequent case. */
16251 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16252 return 0;
16253
16254 IF_DEBUG (debug_method_add (w, "twu1"));
16255
16256 /* Display up to a row that can be reused. The variable
16257 last_text_row is set to the last row displayed that displays
16258 text. Note that it.vpos == 0 if or if not there is a
16259 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16260 start_display (&it, w, new_start);
16261 w->cursor.vpos = -1;
16262 last_text_row = last_reused_text_row = NULL;
16263
16264 while (it.current_y < it.last_visible_y
16265 && !fonts_changed_p)
16266 {
16267 /* If we have reached into the characters in the START row,
16268 that means the line boundaries have changed. So we
16269 can't start copying with the row START. Maybe it will
16270 work to start copying with the following row. */
16271 while (IT_CHARPOS (it) > CHARPOS (start))
16272 {
16273 /* Advance to the next row as the "start". */
16274 start_row++;
16275 start = start_row->minpos;
16276 /* If there are no more rows to try, or just one, give up. */
16277 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16278 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16279 || CHARPOS (start) == ZV)
16280 {
16281 clear_glyph_matrix (w->desired_matrix);
16282 return 0;
16283 }
16284
16285 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16286 }
16287 /* If we have reached alignment, we can copy the rest of the
16288 rows. */
16289 if (IT_CHARPOS (it) == CHARPOS (start)
16290 /* Don't accept "alignment" inside a display vector,
16291 since start_row could have started in the middle of
16292 that same display vector (thus their character
16293 positions match), and we have no way of telling if
16294 that is the case. */
16295 && it.current.dpvec_index < 0)
16296 break;
16297
16298 if (display_line (&it))
16299 last_text_row = it.glyph_row - 1;
16300
16301 }
16302
16303 /* A value of current_y < last_visible_y means that we stopped
16304 at the previous window start, which in turn means that we
16305 have at least one reusable row. */
16306 if (it.current_y < it.last_visible_y)
16307 {
16308 struct glyph_row *row;
16309
16310 /* IT.vpos always starts from 0; it counts text lines. */
16311 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16312
16313 /* Find PT if not already found in the lines displayed. */
16314 if (w->cursor.vpos < 0)
16315 {
16316 int dy = it.current_y - start_row->y;
16317
16318 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16319 row = row_containing_pos (w, PT, row, NULL, dy);
16320 if (row)
16321 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16322 dy, nrows_scrolled);
16323 else
16324 {
16325 clear_glyph_matrix (w->desired_matrix);
16326 return 0;
16327 }
16328 }
16329
16330 /* Scroll the display. Do it before the current matrix is
16331 changed. The problem here is that update has not yet
16332 run, i.e. part of the current matrix is not up to date.
16333 scroll_run_hook will clear the cursor, and use the
16334 current matrix to get the height of the row the cursor is
16335 in. */
16336 run.current_y = start_row->y;
16337 run.desired_y = it.current_y;
16338 run.height = it.last_visible_y - it.current_y;
16339
16340 if (run.height > 0 && run.current_y != run.desired_y)
16341 {
16342 update_begin (f);
16343 FRAME_RIF (f)->update_window_begin_hook (w);
16344 FRAME_RIF (f)->clear_window_mouse_face (w);
16345 FRAME_RIF (f)->scroll_run_hook (w, &run);
16346 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16347 update_end (f);
16348 }
16349
16350 /* Shift current matrix down by nrows_scrolled lines. */
16351 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16352 rotate_matrix (w->current_matrix,
16353 start_vpos,
16354 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16355 nrows_scrolled);
16356
16357 /* Disable lines that must be updated. */
16358 for (i = 0; i < nrows_scrolled; ++i)
16359 (start_row + i)->enabled_p = 0;
16360
16361 /* Re-compute Y positions. */
16362 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16363 max_y = it.last_visible_y;
16364 for (row = start_row + nrows_scrolled;
16365 row < bottom_row;
16366 ++row)
16367 {
16368 row->y = it.current_y;
16369 row->visible_height = row->height;
16370
16371 if (row->y < min_y)
16372 row->visible_height -= min_y - row->y;
16373 if (row->y + row->height > max_y)
16374 row->visible_height -= row->y + row->height - max_y;
16375 if (row->fringe_bitmap_periodic_p)
16376 row->redraw_fringe_bitmaps_p = 1;
16377
16378 it.current_y += row->height;
16379
16380 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16381 last_reused_text_row = row;
16382 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16383 break;
16384 }
16385
16386 /* Disable lines in the current matrix which are now
16387 below the window. */
16388 for (++row; row < bottom_row; ++row)
16389 row->enabled_p = row->mode_line_p = 0;
16390 }
16391
16392 /* Update window_end_pos etc.; last_reused_text_row is the last
16393 reused row from the current matrix containing text, if any.
16394 The value of last_text_row is the last displayed line
16395 containing text. */
16396 if (last_reused_text_row)
16397 {
16398 w->window_end_bytepos
16399 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16400 wset_window_end_pos
16401 (w, make_number (Z
16402 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16403 wset_window_end_vpos
16404 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16405 w->current_matrix)));
16406 }
16407 else if (last_text_row)
16408 {
16409 w->window_end_bytepos
16410 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16411 wset_window_end_pos
16412 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16413 wset_window_end_vpos
16414 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16415 w->desired_matrix)));
16416 }
16417 else
16418 {
16419 /* This window must be completely empty. */
16420 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16421 wset_window_end_pos (w, make_number (Z - ZV));
16422 wset_window_end_vpos (w, make_number (0));
16423 }
16424 w->window_end_valid = 0;
16425
16426 /* Update hint: don't try scrolling again in update_window. */
16427 w->desired_matrix->no_scrolling_p = 1;
16428
16429 #ifdef GLYPH_DEBUG
16430 debug_method_add (w, "try_window_reusing_current_matrix 1");
16431 #endif
16432 return 1;
16433 }
16434 else if (CHARPOS (new_start) > CHARPOS (start))
16435 {
16436 struct glyph_row *pt_row, *row;
16437 struct glyph_row *first_reusable_row;
16438 struct glyph_row *first_row_to_display;
16439 int dy;
16440 int yb = window_text_bottom_y (w);
16441
16442 /* Find the row starting at new_start, if there is one. Don't
16443 reuse a partially visible line at the end. */
16444 first_reusable_row = start_row;
16445 while (first_reusable_row->enabled_p
16446 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16447 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16448 < CHARPOS (new_start)))
16449 ++first_reusable_row;
16450
16451 /* Give up if there is no row to reuse. */
16452 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16453 || !first_reusable_row->enabled_p
16454 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16455 != CHARPOS (new_start)))
16456 return 0;
16457
16458 /* We can reuse fully visible rows beginning with
16459 first_reusable_row to the end of the window. Set
16460 first_row_to_display to the first row that cannot be reused.
16461 Set pt_row to the row containing point, if there is any. */
16462 pt_row = NULL;
16463 for (first_row_to_display = first_reusable_row;
16464 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16465 ++first_row_to_display)
16466 {
16467 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16468 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16469 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16470 && first_row_to_display->ends_at_zv_p
16471 && pt_row == NULL)))
16472 pt_row = first_row_to_display;
16473 }
16474
16475 /* Start displaying at the start of first_row_to_display. */
16476 eassert (first_row_to_display->y < yb);
16477 init_to_row_start (&it, w, first_row_to_display);
16478
16479 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16480 - start_vpos);
16481 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16482 - nrows_scrolled);
16483 it.current_y = (first_row_to_display->y - first_reusable_row->y
16484 + WINDOW_HEADER_LINE_HEIGHT (w));
16485
16486 /* Display lines beginning with first_row_to_display in the
16487 desired matrix. Set last_text_row to the last row displayed
16488 that displays text. */
16489 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16490 if (pt_row == NULL)
16491 w->cursor.vpos = -1;
16492 last_text_row = NULL;
16493 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16494 if (display_line (&it))
16495 last_text_row = it.glyph_row - 1;
16496
16497 /* If point is in a reused row, adjust y and vpos of the cursor
16498 position. */
16499 if (pt_row)
16500 {
16501 w->cursor.vpos -= nrows_scrolled;
16502 w->cursor.y -= first_reusable_row->y - start_row->y;
16503 }
16504
16505 /* Give up if point isn't in a row displayed or reused. (This
16506 also handles the case where w->cursor.vpos < nrows_scrolled
16507 after the calls to display_line, which can happen with scroll
16508 margins. See bug#1295.) */
16509 if (w->cursor.vpos < 0)
16510 {
16511 clear_glyph_matrix (w->desired_matrix);
16512 return 0;
16513 }
16514
16515 /* Scroll the display. */
16516 run.current_y = first_reusable_row->y;
16517 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16518 run.height = it.last_visible_y - run.current_y;
16519 dy = run.current_y - run.desired_y;
16520
16521 if (run.height)
16522 {
16523 update_begin (f);
16524 FRAME_RIF (f)->update_window_begin_hook (w);
16525 FRAME_RIF (f)->clear_window_mouse_face (w);
16526 FRAME_RIF (f)->scroll_run_hook (w, &run);
16527 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16528 update_end (f);
16529 }
16530
16531 /* Adjust Y positions of reused rows. */
16532 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16533 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16534 max_y = it.last_visible_y;
16535 for (row = first_reusable_row; row < first_row_to_display; ++row)
16536 {
16537 row->y -= dy;
16538 row->visible_height = row->height;
16539 if (row->y < min_y)
16540 row->visible_height -= min_y - row->y;
16541 if (row->y + row->height > max_y)
16542 row->visible_height -= row->y + row->height - max_y;
16543 if (row->fringe_bitmap_periodic_p)
16544 row->redraw_fringe_bitmaps_p = 1;
16545 }
16546
16547 /* Scroll the current matrix. */
16548 eassert (nrows_scrolled > 0);
16549 rotate_matrix (w->current_matrix,
16550 start_vpos,
16551 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16552 -nrows_scrolled);
16553
16554 /* Disable rows not reused. */
16555 for (row -= nrows_scrolled; row < bottom_row; ++row)
16556 row->enabled_p = 0;
16557
16558 /* Point may have moved to a different line, so we cannot assume that
16559 the previous cursor position is valid; locate the correct row. */
16560 if (pt_row)
16561 {
16562 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16563 row < bottom_row
16564 && PT >= MATRIX_ROW_END_CHARPOS (row)
16565 && !row->ends_at_zv_p;
16566 row++)
16567 {
16568 w->cursor.vpos++;
16569 w->cursor.y = row->y;
16570 }
16571 if (row < bottom_row)
16572 {
16573 /* Can't simply scan the row for point with
16574 bidi-reordered glyph rows. Let set_cursor_from_row
16575 figure out where to put the cursor, and if it fails,
16576 give up. */
16577 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16578 {
16579 if (!set_cursor_from_row (w, row, w->current_matrix,
16580 0, 0, 0, 0))
16581 {
16582 clear_glyph_matrix (w->desired_matrix);
16583 return 0;
16584 }
16585 }
16586 else
16587 {
16588 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16589 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16590
16591 for (; glyph < end
16592 && (!BUFFERP (glyph->object)
16593 || glyph->charpos < PT);
16594 glyph++)
16595 {
16596 w->cursor.hpos++;
16597 w->cursor.x += glyph->pixel_width;
16598 }
16599 }
16600 }
16601 }
16602
16603 /* Adjust window end. A null value of last_text_row means that
16604 the window end is in reused rows which in turn means that
16605 only its vpos can have changed. */
16606 if (last_text_row)
16607 {
16608 w->window_end_bytepos
16609 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16610 wset_window_end_pos
16611 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16612 wset_window_end_vpos
16613 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16614 w->desired_matrix)));
16615 }
16616 else
16617 {
16618 wset_window_end_vpos
16619 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16620 }
16621
16622 w->window_end_valid = 0;
16623 w->desired_matrix->no_scrolling_p = 1;
16624
16625 #ifdef GLYPH_DEBUG
16626 debug_method_add (w, "try_window_reusing_current_matrix 2");
16627 #endif
16628 return 1;
16629 }
16630
16631 return 0;
16632 }
16633
16634
16635 \f
16636 /************************************************************************
16637 Window redisplay reusing current matrix when buffer has changed
16638 ************************************************************************/
16639
16640 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16641 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16642 ptrdiff_t *, ptrdiff_t *);
16643 static struct glyph_row *
16644 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16645 struct glyph_row *);
16646
16647
16648 /* Return the last row in MATRIX displaying text. If row START is
16649 non-null, start searching with that row. IT gives the dimensions
16650 of the display. Value is null if matrix is empty; otherwise it is
16651 a pointer to the row found. */
16652
16653 static struct glyph_row *
16654 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16655 struct glyph_row *start)
16656 {
16657 struct glyph_row *row, *row_found;
16658
16659 /* Set row_found to the last row in IT->w's current matrix
16660 displaying text. The loop looks funny but think of partially
16661 visible lines. */
16662 row_found = NULL;
16663 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16664 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16665 {
16666 eassert (row->enabled_p);
16667 row_found = row;
16668 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16669 break;
16670 ++row;
16671 }
16672
16673 return row_found;
16674 }
16675
16676
16677 /* Return the last row in the current matrix of W that is not affected
16678 by changes at the start of current_buffer that occurred since W's
16679 current matrix was built. Value is null if no such row exists.
16680
16681 BEG_UNCHANGED us the number of characters unchanged at the start of
16682 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16683 first changed character in current_buffer. Characters at positions <
16684 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16685 when the current matrix was built. */
16686
16687 static struct glyph_row *
16688 find_last_unchanged_at_beg_row (struct window *w)
16689 {
16690 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16691 struct glyph_row *row;
16692 struct glyph_row *row_found = NULL;
16693 int yb = window_text_bottom_y (w);
16694
16695 /* Find the last row displaying unchanged text. */
16696 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16697 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16698 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16699 ++row)
16700 {
16701 if (/* If row ends before first_changed_pos, it is unchanged,
16702 except in some case. */
16703 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16704 /* When row ends in ZV and we write at ZV it is not
16705 unchanged. */
16706 && !row->ends_at_zv_p
16707 /* When first_changed_pos is the end of a continued line,
16708 row is not unchanged because it may be no longer
16709 continued. */
16710 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16711 && (row->continued_p
16712 || row->exact_window_width_line_p))
16713 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16714 needs to be recomputed, so don't consider this row as
16715 unchanged. This happens when the last line was
16716 bidi-reordered and was killed immediately before this
16717 redisplay cycle. In that case, ROW->end stores the
16718 buffer position of the first visual-order character of
16719 the killed text, which is now beyond ZV. */
16720 && CHARPOS (row->end.pos) <= ZV)
16721 row_found = row;
16722
16723 /* Stop if last visible row. */
16724 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16725 break;
16726 }
16727
16728 return row_found;
16729 }
16730
16731
16732 /* Find the first glyph row in the current matrix of W that is not
16733 affected by changes at the end of current_buffer since the
16734 time W's current matrix was built.
16735
16736 Return in *DELTA the number of chars by which buffer positions in
16737 unchanged text at the end of current_buffer must be adjusted.
16738
16739 Return in *DELTA_BYTES the corresponding number of bytes.
16740
16741 Value is null if no such row exists, i.e. all rows are affected by
16742 changes. */
16743
16744 static struct glyph_row *
16745 find_first_unchanged_at_end_row (struct window *w,
16746 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16747 {
16748 struct glyph_row *row;
16749 struct glyph_row *row_found = NULL;
16750
16751 *delta = *delta_bytes = 0;
16752
16753 /* Display must not have been paused, otherwise the current matrix
16754 is not up to date. */
16755 eassert (w->window_end_valid);
16756
16757 /* A value of window_end_pos >= END_UNCHANGED means that the window
16758 end is in the range of changed text. If so, there is no
16759 unchanged row at the end of W's current matrix. */
16760 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16761 return NULL;
16762
16763 /* Set row to the last row in W's current matrix displaying text. */
16764 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16765
16766 /* If matrix is entirely empty, no unchanged row exists. */
16767 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16768 {
16769 /* The value of row is the last glyph row in the matrix having a
16770 meaningful buffer position in it. The end position of row
16771 corresponds to window_end_pos. This allows us to translate
16772 buffer positions in the current matrix to current buffer
16773 positions for characters not in changed text. */
16774 ptrdiff_t Z_old =
16775 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16776 ptrdiff_t Z_BYTE_old =
16777 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16778 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16779 struct glyph_row *first_text_row
16780 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16781
16782 *delta = Z - Z_old;
16783 *delta_bytes = Z_BYTE - Z_BYTE_old;
16784
16785 /* Set last_unchanged_pos to the buffer position of the last
16786 character in the buffer that has not been changed. Z is the
16787 index + 1 of the last character in current_buffer, i.e. by
16788 subtracting END_UNCHANGED we get the index of the last
16789 unchanged character, and we have to add BEG to get its buffer
16790 position. */
16791 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16792 last_unchanged_pos_old = last_unchanged_pos - *delta;
16793
16794 /* Search backward from ROW for a row displaying a line that
16795 starts at a minimum position >= last_unchanged_pos_old. */
16796 for (; row > first_text_row; --row)
16797 {
16798 /* This used to abort, but it can happen.
16799 It is ok to just stop the search instead here. KFS. */
16800 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16801 break;
16802
16803 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16804 row_found = row;
16805 }
16806 }
16807
16808 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16809
16810 return row_found;
16811 }
16812
16813
16814 /* Make sure that glyph rows in the current matrix of window W
16815 reference the same glyph memory as corresponding rows in the
16816 frame's frame matrix. This function is called after scrolling W's
16817 current matrix on a terminal frame in try_window_id and
16818 try_window_reusing_current_matrix. */
16819
16820 static void
16821 sync_frame_with_window_matrix_rows (struct window *w)
16822 {
16823 struct frame *f = XFRAME (w->frame);
16824 struct glyph_row *window_row, *window_row_end, *frame_row;
16825
16826 /* Preconditions: W must be a leaf window and full-width. Its frame
16827 must have a frame matrix. */
16828 eassert (NILP (w->hchild) && NILP (w->vchild));
16829 eassert (WINDOW_FULL_WIDTH_P (w));
16830 eassert (!FRAME_WINDOW_P (f));
16831
16832 /* If W is a full-width window, glyph pointers in W's current matrix
16833 have, by definition, to be the same as glyph pointers in the
16834 corresponding frame matrix. Note that frame matrices have no
16835 marginal areas (see build_frame_matrix). */
16836 window_row = w->current_matrix->rows;
16837 window_row_end = window_row + w->current_matrix->nrows;
16838 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16839 while (window_row < window_row_end)
16840 {
16841 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16842 struct glyph *end = window_row->glyphs[LAST_AREA];
16843
16844 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16845 frame_row->glyphs[TEXT_AREA] = start;
16846 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16847 frame_row->glyphs[LAST_AREA] = end;
16848
16849 /* Disable frame rows whose corresponding window rows have
16850 been disabled in try_window_id. */
16851 if (!window_row->enabled_p)
16852 frame_row->enabled_p = 0;
16853
16854 ++window_row, ++frame_row;
16855 }
16856 }
16857
16858
16859 /* Find the glyph row in window W containing CHARPOS. Consider all
16860 rows between START and END (not inclusive). END null means search
16861 all rows to the end of the display area of W. Value is the row
16862 containing CHARPOS or null. */
16863
16864 struct glyph_row *
16865 row_containing_pos (struct window *w, ptrdiff_t charpos,
16866 struct glyph_row *start, struct glyph_row *end, int dy)
16867 {
16868 struct glyph_row *row = start;
16869 struct glyph_row *best_row = NULL;
16870 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
16871 int last_y;
16872
16873 /* If we happen to start on a header-line, skip that. */
16874 if (row->mode_line_p)
16875 ++row;
16876
16877 if ((end && row >= end) || !row->enabled_p)
16878 return NULL;
16879
16880 last_y = window_text_bottom_y (w) - dy;
16881
16882 while (1)
16883 {
16884 /* Give up if we have gone too far. */
16885 if (end && row >= end)
16886 return NULL;
16887 /* This formerly returned if they were equal.
16888 I think that both quantities are of a "last plus one" type;
16889 if so, when they are equal, the row is within the screen. -- rms. */
16890 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16891 return NULL;
16892
16893 /* If it is in this row, return this row. */
16894 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16895 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16896 /* The end position of a row equals the start
16897 position of the next row. If CHARPOS is there, we
16898 would rather display it in the next line, except
16899 when this line ends in ZV. */
16900 && !row->ends_at_zv_p
16901 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
16902 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16903 {
16904 struct glyph *g;
16905
16906 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
16907 || (!best_row && !row->continued_p))
16908 return row;
16909 /* In bidi-reordered rows, there could be several rows
16910 occluding point, all of them belonging to the same
16911 continued line. We need to find the row which fits
16912 CHARPOS the best. */
16913 for (g = row->glyphs[TEXT_AREA];
16914 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16915 g++)
16916 {
16917 if (!STRINGP (g->object))
16918 {
16919 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16920 {
16921 mindif = eabs (g->charpos - charpos);
16922 best_row = row;
16923 /* Exact match always wins. */
16924 if (mindif == 0)
16925 return best_row;
16926 }
16927 }
16928 }
16929 }
16930 else if (best_row && !row->continued_p)
16931 return best_row;
16932 ++row;
16933 }
16934 }
16935
16936
16937 /* Try to redisplay window W by reusing its existing display. W's
16938 current matrix must be up to date when this function is called,
16939 i.e. window_end_valid must be nonzero.
16940
16941 Value is
16942
16943 1 if display has been updated
16944 0 if otherwise unsuccessful
16945 -1 if redisplay with same window start is known not to succeed
16946
16947 The following steps are performed:
16948
16949 1. Find the last row in the current matrix of W that is not
16950 affected by changes at the start of current_buffer. If no such row
16951 is found, give up.
16952
16953 2. Find the first row in W's current matrix that is not affected by
16954 changes at the end of current_buffer. Maybe there is no such row.
16955
16956 3. Display lines beginning with the row + 1 found in step 1 to the
16957 row found in step 2 or, if step 2 didn't find a row, to the end of
16958 the window.
16959
16960 4. If cursor is not known to appear on the window, give up.
16961
16962 5. If display stopped at the row found in step 2, scroll the
16963 display and current matrix as needed.
16964
16965 6. Maybe display some lines at the end of W, if we must. This can
16966 happen under various circumstances, like a partially visible line
16967 becoming fully visible, or because newly displayed lines are displayed
16968 in smaller font sizes.
16969
16970 7. Update W's window end information. */
16971
16972 static int
16973 try_window_id (struct window *w)
16974 {
16975 struct frame *f = XFRAME (w->frame);
16976 struct glyph_matrix *current_matrix = w->current_matrix;
16977 struct glyph_matrix *desired_matrix = w->desired_matrix;
16978 struct glyph_row *last_unchanged_at_beg_row;
16979 struct glyph_row *first_unchanged_at_end_row;
16980 struct glyph_row *row;
16981 struct glyph_row *bottom_row;
16982 int bottom_vpos;
16983 struct it it;
16984 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
16985 int dvpos, dy;
16986 struct text_pos start_pos;
16987 struct run run;
16988 int first_unchanged_at_end_vpos = 0;
16989 struct glyph_row *last_text_row, *last_text_row_at_end;
16990 struct text_pos start;
16991 ptrdiff_t first_changed_charpos, last_changed_charpos;
16992
16993 #ifdef GLYPH_DEBUG
16994 if (inhibit_try_window_id)
16995 return 0;
16996 #endif
16997
16998 /* This is handy for debugging. */
16999 #if 0
17000 #define GIVE_UP(X) \
17001 do { \
17002 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17003 return 0; \
17004 } while (0)
17005 #else
17006 #define GIVE_UP(X) return 0
17007 #endif
17008
17009 SET_TEXT_POS_FROM_MARKER (start, w->start);
17010
17011 /* Don't use this for mini-windows because these can show
17012 messages and mini-buffers, and we don't handle that here. */
17013 if (MINI_WINDOW_P (w))
17014 GIVE_UP (1);
17015
17016 /* This flag is used to prevent redisplay optimizations. */
17017 if (windows_or_buffers_changed || cursor_type_changed)
17018 GIVE_UP (2);
17019
17020 /* Verify that narrowing has not changed.
17021 Also verify that we were not told to prevent redisplay optimizations.
17022 It would be nice to further
17023 reduce the number of cases where this prevents try_window_id. */
17024 if (current_buffer->clip_changed
17025 || current_buffer->prevent_redisplay_optimizations_p)
17026 GIVE_UP (3);
17027
17028 /* Window must either use window-based redisplay or be full width. */
17029 if (!FRAME_WINDOW_P (f)
17030 && (!FRAME_LINE_INS_DEL_OK (f)
17031 || !WINDOW_FULL_WIDTH_P (w)))
17032 GIVE_UP (4);
17033
17034 /* Give up if point is known NOT to appear in W. */
17035 if (PT < CHARPOS (start))
17036 GIVE_UP (5);
17037
17038 /* Another way to prevent redisplay optimizations. */
17039 if (w->last_modified == 0)
17040 GIVE_UP (6);
17041
17042 /* Verify that window is not hscrolled. */
17043 if (w->hscroll != 0)
17044 GIVE_UP (7);
17045
17046 /* Verify that display wasn't paused. */
17047 if (!w->window_end_valid)
17048 GIVE_UP (8);
17049
17050 /* Can't use this if highlighting a region because a cursor movement
17051 will do more than just set the cursor. */
17052 if (0 <= markpos_of_region ())
17053 GIVE_UP (9);
17054
17055 /* Likewise if highlighting trailing whitespace. */
17056 if (!NILP (Vshow_trailing_whitespace))
17057 GIVE_UP (11);
17058
17059 /* Likewise if showing a region. */
17060 if (w->region_showing)
17061 GIVE_UP (10);
17062
17063 /* Can't use this if overlay arrow position and/or string have
17064 changed. */
17065 if (overlay_arrows_changed_p ())
17066 GIVE_UP (12);
17067
17068 /* When word-wrap is on, adding a space to the first word of a
17069 wrapped line can change the wrap position, altering the line
17070 above it. It might be worthwhile to handle this more
17071 intelligently, but for now just redisplay from scratch. */
17072 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17073 GIVE_UP (21);
17074
17075 /* Under bidi reordering, adding or deleting a character in the
17076 beginning of a paragraph, before the first strong directional
17077 character, can change the base direction of the paragraph (unless
17078 the buffer specifies a fixed paragraph direction), which will
17079 require to redisplay the whole paragraph. It might be worthwhile
17080 to find the paragraph limits and widen the range of redisplayed
17081 lines to that, but for now just give up this optimization and
17082 redisplay from scratch. */
17083 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17084 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17085 GIVE_UP (22);
17086
17087 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17088 only if buffer has really changed. The reason is that the gap is
17089 initially at Z for freshly visited files. The code below would
17090 set end_unchanged to 0 in that case. */
17091 if (MODIFF > SAVE_MODIFF
17092 /* This seems to happen sometimes after saving a buffer. */
17093 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17094 {
17095 if (GPT - BEG < BEG_UNCHANGED)
17096 BEG_UNCHANGED = GPT - BEG;
17097 if (Z - GPT < END_UNCHANGED)
17098 END_UNCHANGED = Z - GPT;
17099 }
17100
17101 /* The position of the first and last character that has been changed. */
17102 first_changed_charpos = BEG + BEG_UNCHANGED;
17103 last_changed_charpos = Z - END_UNCHANGED;
17104
17105 /* If window starts after a line end, and the last change is in
17106 front of that newline, then changes don't affect the display.
17107 This case happens with stealth-fontification. Note that although
17108 the display is unchanged, glyph positions in the matrix have to
17109 be adjusted, of course. */
17110 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17111 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17112 && ((last_changed_charpos < CHARPOS (start)
17113 && CHARPOS (start) == BEGV)
17114 || (last_changed_charpos < CHARPOS (start) - 1
17115 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17116 {
17117 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17118 struct glyph_row *r0;
17119
17120 /* Compute how many chars/bytes have been added to or removed
17121 from the buffer. */
17122 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17123 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17124 Z_delta = Z - Z_old;
17125 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17126
17127 /* Give up if PT is not in the window. Note that it already has
17128 been checked at the start of try_window_id that PT is not in
17129 front of the window start. */
17130 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17131 GIVE_UP (13);
17132
17133 /* If window start is unchanged, we can reuse the whole matrix
17134 as is, after adjusting glyph positions. No need to compute
17135 the window end again, since its offset from Z hasn't changed. */
17136 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17137 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17138 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17139 /* PT must not be in a partially visible line. */
17140 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17141 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17142 {
17143 /* Adjust positions in the glyph matrix. */
17144 if (Z_delta || Z_delta_bytes)
17145 {
17146 struct glyph_row *r1
17147 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17148 increment_matrix_positions (w->current_matrix,
17149 MATRIX_ROW_VPOS (r0, current_matrix),
17150 MATRIX_ROW_VPOS (r1, current_matrix),
17151 Z_delta, Z_delta_bytes);
17152 }
17153
17154 /* Set the cursor. */
17155 row = row_containing_pos (w, PT, r0, NULL, 0);
17156 if (row)
17157 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17158 else
17159 emacs_abort ();
17160 return 1;
17161 }
17162 }
17163
17164 /* Handle the case that changes are all below what is displayed in
17165 the window, and that PT is in the window. This shortcut cannot
17166 be taken if ZV is visible in the window, and text has been added
17167 there that is visible in the window. */
17168 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17169 /* ZV is not visible in the window, or there are no
17170 changes at ZV, actually. */
17171 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17172 || first_changed_charpos == last_changed_charpos))
17173 {
17174 struct glyph_row *r0;
17175
17176 /* Give up if PT is not in the window. Note that it already has
17177 been checked at the start of try_window_id that PT is not in
17178 front of the window start. */
17179 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17180 GIVE_UP (14);
17181
17182 /* If window start is unchanged, we can reuse the whole matrix
17183 as is, without changing glyph positions since no text has
17184 been added/removed in front of the window end. */
17185 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17186 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17187 /* PT must not be in a partially visible line. */
17188 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17189 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17190 {
17191 /* We have to compute the window end anew since text
17192 could have been added/removed after it. */
17193 wset_window_end_pos
17194 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17195 w->window_end_bytepos
17196 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17197
17198 /* Set the cursor. */
17199 row = row_containing_pos (w, PT, r0, NULL, 0);
17200 if (row)
17201 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17202 else
17203 emacs_abort ();
17204 return 2;
17205 }
17206 }
17207
17208 /* Give up if window start is in the changed area.
17209
17210 The condition used to read
17211
17212 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17213
17214 but why that was tested escapes me at the moment. */
17215 if (CHARPOS (start) >= first_changed_charpos
17216 && CHARPOS (start) <= last_changed_charpos)
17217 GIVE_UP (15);
17218
17219 /* Check that window start agrees with the start of the first glyph
17220 row in its current matrix. Check this after we know the window
17221 start is not in changed text, otherwise positions would not be
17222 comparable. */
17223 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17224 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17225 GIVE_UP (16);
17226
17227 /* Give up if the window ends in strings. Overlay strings
17228 at the end are difficult to handle, so don't try. */
17229 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17230 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17231 GIVE_UP (20);
17232
17233 /* Compute the position at which we have to start displaying new
17234 lines. Some of the lines at the top of the window might be
17235 reusable because they are not displaying changed text. Find the
17236 last row in W's current matrix not affected by changes at the
17237 start of current_buffer. Value is null if changes start in the
17238 first line of window. */
17239 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17240 if (last_unchanged_at_beg_row)
17241 {
17242 /* Avoid starting to display in the middle of a character, a TAB
17243 for instance. This is easier than to set up the iterator
17244 exactly, and it's not a frequent case, so the additional
17245 effort wouldn't really pay off. */
17246 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17247 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17248 && last_unchanged_at_beg_row > w->current_matrix->rows)
17249 --last_unchanged_at_beg_row;
17250
17251 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17252 GIVE_UP (17);
17253
17254 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17255 GIVE_UP (18);
17256 start_pos = it.current.pos;
17257
17258 /* Start displaying new lines in the desired matrix at the same
17259 vpos we would use in the current matrix, i.e. below
17260 last_unchanged_at_beg_row. */
17261 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17262 current_matrix);
17263 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17264 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17265
17266 eassert (it.hpos == 0 && it.current_x == 0);
17267 }
17268 else
17269 {
17270 /* There are no reusable lines at the start of the window.
17271 Start displaying in the first text line. */
17272 start_display (&it, w, start);
17273 it.vpos = it.first_vpos;
17274 start_pos = it.current.pos;
17275 }
17276
17277 /* Find the first row that is not affected by changes at the end of
17278 the buffer. Value will be null if there is no unchanged row, in
17279 which case we must redisplay to the end of the window. delta
17280 will be set to the value by which buffer positions beginning with
17281 first_unchanged_at_end_row have to be adjusted due to text
17282 changes. */
17283 first_unchanged_at_end_row
17284 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17285 IF_DEBUG (debug_delta = delta);
17286 IF_DEBUG (debug_delta_bytes = delta_bytes);
17287
17288 /* Set stop_pos to the buffer position up to which we will have to
17289 display new lines. If first_unchanged_at_end_row != NULL, this
17290 is the buffer position of the start of the line displayed in that
17291 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17292 that we don't stop at a buffer position. */
17293 stop_pos = 0;
17294 if (first_unchanged_at_end_row)
17295 {
17296 eassert (last_unchanged_at_beg_row == NULL
17297 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17298
17299 /* If this is a continuation line, move forward to the next one
17300 that isn't. Changes in lines above affect this line.
17301 Caution: this may move first_unchanged_at_end_row to a row
17302 not displaying text. */
17303 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17304 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17305 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17306 < it.last_visible_y))
17307 ++first_unchanged_at_end_row;
17308
17309 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17310 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17311 >= it.last_visible_y))
17312 first_unchanged_at_end_row = NULL;
17313 else
17314 {
17315 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17316 + delta);
17317 first_unchanged_at_end_vpos
17318 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17319 eassert (stop_pos >= Z - END_UNCHANGED);
17320 }
17321 }
17322 else if (last_unchanged_at_beg_row == NULL)
17323 GIVE_UP (19);
17324
17325
17326 #ifdef GLYPH_DEBUG
17327
17328 /* Either there is no unchanged row at the end, or the one we have
17329 now displays text. This is a necessary condition for the window
17330 end pos calculation at the end of this function. */
17331 eassert (first_unchanged_at_end_row == NULL
17332 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17333
17334 debug_last_unchanged_at_beg_vpos
17335 = (last_unchanged_at_beg_row
17336 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17337 : -1);
17338 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17339
17340 #endif /* GLYPH_DEBUG */
17341
17342
17343 /* Display new lines. Set last_text_row to the last new line
17344 displayed which has text on it, i.e. might end up as being the
17345 line where the window_end_vpos is. */
17346 w->cursor.vpos = -1;
17347 last_text_row = NULL;
17348 overlay_arrow_seen = 0;
17349 while (it.current_y < it.last_visible_y
17350 && !fonts_changed_p
17351 && (first_unchanged_at_end_row == NULL
17352 || IT_CHARPOS (it) < stop_pos))
17353 {
17354 if (display_line (&it))
17355 last_text_row = it.glyph_row - 1;
17356 }
17357
17358 if (fonts_changed_p)
17359 return -1;
17360
17361
17362 /* Compute differences in buffer positions, y-positions etc. for
17363 lines reused at the bottom of the window. Compute what we can
17364 scroll. */
17365 if (first_unchanged_at_end_row
17366 /* No lines reused because we displayed everything up to the
17367 bottom of the window. */
17368 && it.current_y < it.last_visible_y)
17369 {
17370 dvpos = (it.vpos
17371 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17372 current_matrix));
17373 dy = it.current_y - first_unchanged_at_end_row->y;
17374 run.current_y = first_unchanged_at_end_row->y;
17375 run.desired_y = run.current_y + dy;
17376 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17377 }
17378 else
17379 {
17380 delta = delta_bytes = dvpos = dy
17381 = run.current_y = run.desired_y = run.height = 0;
17382 first_unchanged_at_end_row = NULL;
17383 }
17384 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17385
17386
17387 /* Find the cursor if not already found. We have to decide whether
17388 PT will appear on this window (it sometimes doesn't, but this is
17389 not a very frequent case.) This decision has to be made before
17390 the current matrix is altered. A value of cursor.vpos < 0 means
17391 that PT is either in one of the lines beginning at
17392 first_unchanged_at_end_row or below the window. Don't care for
17393 lines that might be displayed later at the window end; as
17394 mentioned, this is not a frequent case. */
17395 if (w->cursor.vpos < 0)
17396 {
17397 /* Cursor in unchanged rows at the top? */
17398 if (PT < CHARPOS (start_pos)
17399 && last_unchanged_at_beg_row)
17400 {
17401 row = row_containing_pos (w, PT,
17402 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17403 last_unchanged_at_beg_row + 1, 0);
17404 if (row)
17405 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17406 }
17407
17408 /* Start from first_unchanged_at_end_row looking for PT. */
17409 else if (first_unchanged_at_end_row)
17410 {
17411 row = row_containing_pos (w, PT - delta,
17412 first_unchanged_at_end_row, NULL, 0);
17413 if (row)
17414 set_cursor_from_row (w, row, w->current_matrix, delta,
17415 delta_bytes, dy, dvpos);
17416 }
17417
17418 /* Give up if cursor was not found. */
17419 if (w->cursor.vpos < 0)
17420 {
17421 clear_glyph_matrix (w->desired_matrix);
17422 return -1;
17423 }
17424 }
17425
17426 /* Don't let the cursor end in the scroll margins. */
17427 {
17428 int this_scroll_margin, cursor_height;
17429
17430 this_scroll_margin =
17431 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17432 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17433 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17434
17435 if ((w->cursor.y < this_scroll_margin
17436 && CHARPOS (start) > BEGV)
17437 /* Old redisplay didn't take scroll margin into account at the bottom,
17438 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17439 || (w->cursor.y + (make_cursor_line_fully_visible_p
17440 ? cursor_height + this_scroll_margin
17441 : 1)) > it.last_visible_y)
17442 {
17443 w->cursor.vpos = -1;
17444 clear_glyph_matrix (w->desired_matrix);
17445 return -1;
17446 }
17447 }
17448
17449 /* Scroll the display. Do it before changing the current matrix so
17450 that xterm.c doesn't get confused about where the cursor glyph is
17451 found. */
17452 if (dy && run.height)
17453 {
17454 update_begin (f);
17455
17456 if (FRAME_WINDOW_P (f))
17457 {
17458 FRAME_RIF (f)->update_window_begin_hook (w);
17459 FRAME_RIF (f)->clear_window_mouse_face (w);
17460 FRAME_RIF (f)->scroll_run_hook (w, &run);
17461 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17462 }
17463 else
17464 {
17465 /* Terminal frame. In this case, dvpos gives the number of
17466 lines to scroll by; dvpos < 0 means scroll up. */
17467 int from_vpos
17468 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17469 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17470 int end = (WINDOW_TOP_EDGE_LINE (w)
17471 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17472 + window_internal_height (w));
17473
17474 #if defined (HAVE_GPM) || defined (MSDOS)
17475 x_clear_window_mouse_face (w);
17476 #endif
17477 /* Perform the operation on the screen. */
17478 if (dvpos > 0)
17479 {
17480 /* Scroll last_unchanged_at_beg_row to the end of the
17481 window down dvpos lines. */
17482 set_terminal_window (f, end);
17483
17484 /* On dumb terminals delete dvpos lines at the end
17485 before inserting dvpos empty lines. */
17486 if (!FRAME_SCROLL_REGION_OK (f))
17487 ins_del_lines (f, end - dvpos, -dvpos);
17488
17489 /* Insert dvpos empty lines in front of
17490 last_unchanged_at_beg_row. */
17491 ins_del_lines (f, from, dvpos);
17492 }
17493 else if (dvpos < 0)
17494 {
17495 /* Scroll up last_unchanged_at_beg_vpos to the end of
17496 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17497 set_terminal_window (f, end);
17498
17499 /* Delete dvpos lines in front of
17500 last_unchanged_at_beg_vpos. ins_del_lines will set
17501 the cursor to the given vpos and emit |dvpos| delete
17502 line sequences. */
17503 ins_del_lines (f, from + dvpos, dvpos);
17504
17505 /* On a dumb terminal insert dvpos empty lines at the
17506 end. */
17507 if (!FRAME_SCROLL_REGION_OK (f))
17508 ins_del_lines (f, end + dvpos, -dvpos);
17509 }
17510
17511 set_terminal_window (f, 0);
17512 }
17513
17514 update_end (f);
17515 }
17516
17517 /* Shift reused rows of the current matrix to the right position.
17518 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17519 text. */
17520 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17521 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17522 if (dvpos < 0)
17523 {
17524 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17525 bottom_vpos, dvpos);
17526 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17527 bottom_vpos);
17528 }
17529 else if (dvpos > 0)
17530 {
17531 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17532 bottom_vpos, dvpos);
17533 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17534 first_unchanged_at_end_vpos + dvpos);
17535 }
17536
17537 /* For frame-based redisplay, make sure that current frame and window
17538 matrix are in sync with respect to glyph memory. */
17539 if (!FRAME_WINDOW_P (f))
17540 sync_frame_with_window_matrix_rows (w);
17541
17542 /* Adjust buffer positions in reused rows. */
17543 if (delta || delta_bytes)
17544 increment_matrix_positions (current_matrix,
17545 first_unchanged_at_end_vpos + dvpos,
17546 bottom_vpos, delta, delta_bytes);
17547
17548 /* Adjust Y positions. */
17549 if (dy)
17550 shift_glyph_matrix (w, current_matrix,
17551 first_unchanged_at_end_vpos + dvpos,
17552 bottom_vpos, dy);
17553
17554 if (first_unchanged_at_end_row)
17555 {
17556 first_unchanged_at_end_row += dvpos;
17557 if (first_unchanged_at_end_row->y >= it.last_visible_y
17558 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17559 first_unchanged_at_end_row = NULL;
17560 }
17561
17562 /* If scrolling up, there may be some lines to display at the end of
17563 the window. */
17564 last_text_row_at_end = NULL;
17565 if (dy < 0)
17566 {
17567 /* Scrolling up can leave for example a partially visible line
17568 at the end of the window to be redisplayed. */
17569 /* Set last_row to the glyph row in the current matrix where the
17570 window end line is found. It has been moved up or down in
17571 the matrix by dvpos. */
17572 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17573 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17574
17575 /* If last_row is the window end line, it should display text. */
17576 eassert (last_row->displays_text_p);
17577
17578 /* If window end line was partially visible before, begin
17579 displaying at that line. Otherwise begin displaying with the
17580 line following it. */
17581 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17582 {
17583 init_to_row_start (&it, w, last_row);
17584 it.vpos = last_vpos;
17585 it.current_y = last_row->y;
17586 }
17587 else
17588 {
17589 init_to_row_end (&it, w, last_row);
17590 it.vpos = 1 + last_vpos;
17591 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17592 ++last_row;
17593 }
17594
17595 /* We may start in a continuation line. If so, we have to
17596 get the right continuation_lines_width and current_x. */
17597 it.continuation_lines_width = last_row->continuation_lines_width;
17598 it.hpos = it.current_x = 0;
17599
17600 /* Display the rest of the lines at the window end. */
17601 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17602 while (it.current_y < it.last_visible_y
17603 && !fonts_changed_p)
17604 {
17605 /* Is it always sure that the display agrees with lines in
17606 the current matrix? I don't think so, so we mark rows
17607 displayed invalid in the current matrix by setting their
17608 enabled_p flag to zero. */
17609 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17610 if (display_line (&it))
17611 last_text_row_at_end = it.glyph_row - 1;
17612 }
17613 }
17614
17615 /* Update window_end_pos and window_end_vpos. */
17616 if (first_unchanged_at_end_row
17617 && !last_text_row_at_end)
17618 {
17619 /* Window end line if one of the preserved rows from the current
17620 matrix. Set row to the last row displaying text in current
17621 matrix starting at first_unchanged_at_end_row, after
17622 scrolling. */
17623 eassert (first_unchanged_at_end_row->displays_text_p);
17624 row = find_last_row_displaying_text (w->current_matrix, &it,
17625 first_unchanged_at_end_row);
17626 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17627
17628 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17629 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17630 wset_window_end_vpos
17631 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17632 eassert (w->window_end_bytepos >= 0);
17633 IF_DEBUG (debug_method_add (w, "A"));
17634 }
17635 else if (last_text_row_at_end)
17636 {
17637 wset_window_end_pos
17638 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17639 w->window_end_bytepos
17640 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17641 wset_window_end_vpos
17642 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17643 desired_matrix)));
17644 eassert (w->window_end_bytepos >= 0);
17645 IF_DEBUG (debug_method_add (w, "B"));
17646 }
17647 else if (last_text_row)
17648 {
17649 /* We have displayed either to the end of the window or at the
17650 end of the window, i.e. the last row with text is to be found
17651 in the desired matrix. */
17652 wset_window_end_pos
17653 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17654 w->window_end_bytepos
17655 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17656 wset_window_end_vpos
17657 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17658 eassert (w->window_end_bytepos >= 0);
17659 }
17660 else if (first_unchanged_at_end_row == NULL
17661 && last_text_row == NULL
17662 && last_text_row_at_end == NULL)
17663 {
17664 /* Displayed to end of window, but no line containing text was
17665 displayed. Lines were deleted at the end of the window. */
17666 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17667 int vpos = XFASTINT (w->window_end_vpos);
17668 struct glyph_row *current_row = current_matrix->rows + vpos;
17669 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17670
17671 for (row = NULL;
17672 row == NULL && vpos >= first_vpos;
17673 --vpos, --current_row, --desired_row)
17674 {
17675 if (desired_row->enabled_p)
17676 {
17677 if (desired_row->displays_text_p)
17678 row = desired_row;
17679 }
17680 else if (current_row->displays_text_p)
17681 row = current_row;
17682 }
17683
17684 eassert (row != NULL);
17685 wset_window_end_vpos (w, make_number (vpos + 1));
17686 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17687 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17688 eassert (w->window_end_bytepos >= 0);
17689 IF_DEBUG (debug_method_add (w, "C"));
17690 }
17691 else
17692 emacs_abort ();
17693
17694 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17695 debug_end_vpos = XFASTINT (w->window_end_vpos));
17696
17697 /* Record that display has not been completed. */
17698 w->window_end_valid = 0;
17699 w->desired_matrix->no_scrolling_p = 1;
17700 return 3;
17701
17702 #undef GIVE_UP
17703 }
17704
17705
17706 \f
17707 /***********************************************************************
17708 More debugging support
17709 ***********************************************************************/
17710
17711 #ifdef GLYPH_DEBUG
17712
17713 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17714 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17715 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17716
17717
17718 /* Dump the contents of glyph matrix MATRIX on stderr.
17719
17720 GLYPHS 0 means don't show glyph contents.
17721 GLYPHS 1 means show glyphs in short form
17722 GLYPHS > 1 means show glyphs in long form. */
17723
17724 void
17725 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17726 {
17727 int i;
17728 for (i = 0; i < matrix->nrows; ++i)
17729 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17730 }
17731
17732
17733 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17734 the glyph row and area where the glyph comes from. */
17735
17736 void
17737 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17738 {
17739 if (glyph->type == CHAR_GLYPH
17740 || glyph->type == GLYPHLESS_GLYPH)
17741 {
17742 fprintf (stderr,
17743 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17744 glyph - row->glyphs[TEXT_AREA],
17745 (glyph->type == CHAR_GLYPH
17746 ? 'C'
17747 : 'G'),
17748 glyph->charpos,
17749 (BUFFERP (glyph->object)
17750 ? 'B'
17751 : (STRINGP (glyph->object)
17752 ? 'S'
17753 : (INTEGERP (glyph->object)
17754 ? '0'
17755 : '-'))),
17756 glyph->pixel_width,
17757 glyph->u.ch,
17758 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17759 ? glyph->u.ch
17760 : '.'),
17761 glyph->face_id,
17762 glyph->left_box_line_p,
17763 glyph->right_box_line_p);
17764 }
17765 else if (glyph->type == STRETCH_GLYPH)
17766 {
17767 fprintf (stderr,
17768 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17769 glyph - row->glyphs[TEXT_AREA],
17770 'S',
17771 glyph->charpos,
17772 (BUFFERP (glyph->object)
17773 ? 'B'
17774 : (STRINGP (glyph->object)
17775 ? 'S'
17776 : (INTEGERP (glyph->object)
17777 ? '0'
17778 : '-'))),
17779 glyph->pixel_width,
17780 0,
17781 ' ',
17782 glyph->face_id,
17783 glyph->left_box_line_p,
17784 glyph->right_box_line_p);
17785 }
17786 else if (glyph->type == IMAGE_GLYPH)
17787 {
17788 fprintf (stderr,
17789 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17790 glyph - row->glyphs[TEXT_AREA],
17791 'I',
17792 glyph->charpos,
17793 (BUFFERP (glyph->object)
17794 ? 'B'
17795 : (STRINGP (glyph->object)
17796 ? 'S'
17797 : (INTEGERP (glyph->object)
17798 ? '0'
17799 : '-'))),
17800 glyph->pixel_width,
17801 glyph->u.img_id,
17802 '.',
17803 glyph->face_id,
17804 glyph->left_box_line_p,
17805 glyph->right_box_line_p);
17806 }
17807 else if (glyph->type == COMPOSITE_GLYPH)
17808 {
17809 fprintf (stderr,
17810 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17811 glyph - row->glyphs[TEXT_AREA],
17812 '+',
17813 glyph->charpos,
17814 (BUFFERP (glyph->object)
17815 ? 'B'
17816 : (STRINGP (glyph->object)
17817 ? 'S'
17818 : (INTEGERP (glyph->object)
17819 ? '0'
17820 : '-'))),
17821 glyph->pixel_width,
17822 glyph->u.cmp.id);
17823 if (glyph->u.cmp.automatic)
17824 fprintf (stderr,
17825 "[%d-%d]",
17826 glyph->slice.cmp.from, glyph->slice.cmp.to);
17827 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17828 glyph->face_id,
17829 glyph->left_box_line_p,
17830 glyph->right_box_line_p);
17831 }
17832 }
17833
17834
17835 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17836 GLYPHS 0 means don't show glyph contents.
17837 GLYPHS 1 means show glyphs in short form
17838 GLYPHS > 1 means show glyphs in long form. */
17839
17840 void
17841 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17842 {
17843 if (glyphs != 1)
17844 {
17845 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17846 fprintf (stderr, "==============================================================================\n");
17847
17848 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17849 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17850 vpos,
17851 MATRIX_ROW_START_CHARPOS (row),
17852 MATRIX_ROW_END_CHARPOS (row),
17853 row->used[TEXT_AREA],
17854 row->contains_overlapping_glyphs_p,
17855 row->enabled_p,
17856 row->truncated_on_left_p,
17857 row->truncated_on_right_p,
17858 row->continued_p,
17859 MATRIX_ROW_CONTINUATION_LINE_P (row),
17860 row->displays_text_p,
17861 row->ends_at_zv_p,
17862 row->fill_line_p,
17863 row->ends_in_middle_of_char_p,
17864 row->starts_in_middle_of_char_p,
17865 row->mouse_face_p,
17866 row->x,
17867 row->y,
17868 row->pixel_width,
17869 row->height,
17870 row->visible_height,
17871 row->ascent,
17872 row->phys_ascent);
17873 /* The next 3 lines should align to "Start" in the header. */
17874 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17875 row->end.overlay_string_index,
17876 row->continuation_lines_width);
17877 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17878 CHARPOS (row->start.string_pos),
17879 CHARPOS (row->end.string_pos));
17880 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17881 row->end.dpvec_index);
17882 }
17883
17884 if (glyphs > 1)
17885 {
17886 int area;
17887
17888 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17889 {
17890 struct glyph *glyph = row->glyphs[area];
17891 struct glyph *glyph_end = glyph + row->used[area];
17892
17893 /* Glyph for a line end in text. */
17894 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17895 ++glyph_end;
17896
17897 if (glyph < glyph_end)
17898 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
17899
17900 for (; glyph < glyph_end; ++glyph)
17901 dump_glyph (row, glyph, area);
17902 }
17903 }
17904 else if (glyphs == 1)
17905 {
17906 int area;
17907
17908 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17909 {
17910 char *s = alloca (row->used[area] + 4);
17911 int i;
17912
17913 for (i = 0; i < row->used[area]; ++i)
17914 {
17915 struct glyph *glyph = row->glyphs[area] + i;
17916 if (i == row->used[area] - 1
17917 && area == TEXT_AREA
17918 && INTEGERP (glyph->object)
17919 && glyph->type == CHAR_GLYPH
17920 && glyph->u.ch == ' ')
17921 {
17922 strcpy (&s[i], "[\\n]");
17923 i += 4;
17924 }
17925 else if (glyph->type == CHAR_GLYPH
17926 && glyph->u.ch < 0x80
17927 && glyph->u.ch >= ' ')
17928 s[i] = glyph->u.ch;
17929 else
17930 s[i] = '.';
17931 }
17932
17933 s[i] = '\0';
17934 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17935 }
17936 }
17937 }
17938
17939
17940 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17941 Sdump_glyph_matrix, 0, 1, "p",
17942 doc: /* Dump the current matrix of the selected window to stderr.
17943 Shows contents of glyph row structures. With non-nil
17944 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17945 glyphs in short form, otherwise show glyphs in long form. */)
17946 (Lisp_Object glyphs)
17947 {
17948 struct window *w = XWINDOW (selected_window);
17949 struct buffer *buffer = XBUFFER (w->buffer);
17950
17951 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
17952 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
17953 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
17954 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
17955 fprintf (stderr, "=============================================\n");
17956 dump_glyph_matrix (w->current_matrix,
17957 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
17958 return Qnil;
17959 }
17960
17961
17962 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
17963 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
17964 (void)
17965 {
17966 struct frame *f = XFRAME (selected_frame);
17967 dump_glyph_matrix (f->current_matrix, 1);
17968 return Qnil;
17969 }
17970
17971
17972 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
17973 doc: /* Dump glyph row ROW to stderr.
17974 GLYPH 0 means don't dump glyphs.
17975 GLYPH 1 means dump glyphs in short form.
17976 GLYPH > 1 or omitted means dump glyphs in long form. */)
17977 (Lisp_Object row, Lisp_Object glyphs)
17978 {
17979 struct glyph_matrix *matrix;
17980 EMACS_INT vpos;
17981
17982 CHECK_NUMBER (row);
17983 matrix = XWINDOW (selected_window)->current_matrix;
17984 vpos = XINT (row);
17985 if (vpos >= 0 && vpos < matrix->nrows)
17986 dump_glyph_row (MATRIX_ROW (matrix, vpos),
17987 vpos,
17988 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
17989 return Qnil;
17990 }
17991
17992
17993 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
17994 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
17995 GLYPH 0 means don't dump glyphs.
17996 GLYPH 1 means dump glyphs in short form.
17997 GLYPH > 1 or omitted means dump glyphs in long form. */)
17998 (Lisp_Object row, Lisp_Object glyphs)
17999 {
18000 struct frame *sf = SELECTED_FRAME ();
18001 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18002 EMACS_INT vpos;
18003
18004 CHECK_NUMBER (row);
18005 vpos = XINT (row);
18006 if (vpos >= 0 && vpos < m->nrows)
18007 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18008 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18009 return Qnil;
18010 }
18011
18012
18013 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18014 doc: /* Toggle tracing of redisplay.
18015 With ARG, turn tracing on if and only if ARG is positive. */)
18016 (Lisp_Object arg)
18017 {
18018 if (NILP (arg))
18019 trace_redisplay_p = !trace_redisplay_p;
18020 else
18021 {
18022 arg = Fprefix_numeric_value (arg);
18023 trace_redisplay_p = XINT (arg) > 0;
18024 }
18025
18026 return Qnil;
18027 }
18028
18029
18030 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18031 doc: /* Like `format', but print result to stderr.
18032 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18033 (ptrdiff_t nargs, Lisp_Object *args)
18034 {
18035 Lisp_Object s = Fformat (nargs, args);
18036 fprintf (stderr, "%s", SDATA (s));
18037 return Qnil;
18038 }
18039
18040 #endif /* GLYPH_DEBUG */
18041
18042
18043 \f
18044 /***********************************************************************
18045 Building Desired Matrix Rows
18046 ***********************************************************************/
18047
18048 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18049 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18050
18051 static struct glyph_row *
18052 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18053 {
18054 struct frame *f = XFRAME (WINDOW_FRAME (w));
18055 struct buffer *buffer = XBUFFER (w->buffer);
18056 struct buffer *old = current_buffer;
18057 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18058 int arrow_len = SCHARS (overlay_arrow_string);
18059 const unsigned char *arrow_end = arrow_string + arrow_len;
18060 const unsigned char *p;
18061 struct it it;
18062 int multibyte_p;
18063 int n_glyphs_before;
18064
18065 set_buffer_temp (buffer);
18066 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18067 it.glyph_row->used[TEXT_AREA] = 0;
18068 SET_TEXT_POS (it.position, 0, 0);
18069
18070 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18071 p = arrow_string;
18072 while (p < arrow_end)
18073 {
18074 Lisp_Object face, ilisp;
18075
18076 /* Get the next character. */
18077 if (multibyte_p)
18078 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18079 else
18080 {
18081 it.c = it.char_to_display = *p, it.len = 1;
18082 if (! ASCII_CHAR_P (it.c))
18083 it.char_to_display = BYTE8_TO_CHAR (it.c);
18084 }
18085 p += it.len;
18086
18087 /* Get its face. */
18088 ilisp = make_number (p - arrow_string);
18089 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18090 it.face_id = compute_char_face (f, it.char_to_display, face);
18091
18092 /* Compute its width, get its glyphs. */
18093 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18094 SET_TEXT_POS (it.position, -1, -1);
18095 PRODUCE_GLYPHS (&it);
18096
18097 /* If this character doesn't fit any more in the line, we have
18098 to remove some glyphs. */
18099 if (it.current_x > it.last_visible_x)
18100 {
18101 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18102 break;
18103 }
18104 }
18105
18106 set_buffer_temp (old);
18107 return it.glyph_row;
18108 }
18109
18110
18111 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18112 glyphs to insert is determined by produce_special_glyphs. */
18113
18114 static void
18115 insert_left_trunc_glyphs (struct it *it)
18116 {
18117 struct it truncate_it;
18118 struct glyph *from, *end, *to, *toend;
18119
18120 eassert (!FRAME_WINDOW_P (it->f)
18121 || (!it->glyph_row->reversed_p
18122 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18123 || (it->glyph_row->reversed_p
18124 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18125
18126 /* Get the truncation glyphs. */
18127 truncate_it = *it;
18128 truncate_it.current_x = 0;
18129 truncate_it.face_id = DEFAULT_FACE_ID;
18130 truncate_it.glyph_row = &scratch_glyph_row;
18131 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18132 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18133 truncate_it.object = make_number (0);
18134 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18135
18136 /* Overwrite glyphs from IT with truncation glyphs. */
18137 if (!it->glyph_row->reversed_p)
18138 {
18139 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18140
18141 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18142 end = from + tused;
18143 to = it->glyph_row->glyphs[TEXT_AREA];
18144 toend = to + it->glyph_row->used[TEXT_AREA];
18145 if (FRAME_WINDOW_P (it->f))
18146 {
18147 /* On GUI frames, when variable-size fonts are displayed,
18148 the truncation glyphs may need more pixels than the row's
18149 glyphs they overwrite. We overwrite more glyphs to free
18150 enough screen real estate, and enlarge the stretch glyph
18151 on the right (see display_line), if there is one, to
18152 preserve the screen position of the truncation glyphs on
18153 the right. */
18154 int w = 0;
18155 struct glyph *g = to;
18156 short used;
18157
18158 /* The first glyph could be partially visible, in which case
18159 it->glyph_row->x will be negative. But we want the left
18160 truncation glyphs to be aligned at the left margin of the
18161 window, so we override the x coordinate at which the row
18162 will begin. */
18163 it->glyph_row->x = 0;
18164 while (g < toend && w < it->truncation_pixel_width)
18165 {
18166 w += g->pixel_width;
18167 ++g;
18168 }
18169 if (g - to - tused > 0)
18170 {
18171 memmove (to + tused, g, (toend - g) * sizeof(*g));
18172 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18173 }
18174 used = it->glyph_row->used[TEXT_AREA];
18175 if (it->glyph_row->truncated_on_right_p
18176 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18177 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18178 == STRETCH_GLYPH)
18179 {
18180 int extra = w - it->truncation_pixel_width;
18181
18182 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18183 }
18184 }
18185
18186 while (from < end)
18187 *to++ = *from++;
18188
18189 /* There may be padding glyphs left over. Overwrite them too. */
18190 if (!FRAME_WINDOW_P (it->f))
18191 {
18192 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18193 {
18194 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18195 while (from < end)
18196 *to++ = *from++;
18197 }
18198 }
18199
18200 if (to > toend)
18201 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18202 }
18203 else
18204 {
18205 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18206
18207 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18208 that back to front. */
18209 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18210 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18211 toend = it->glyph_row->glyphs[TEXT_AREA];
18212 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18213 if (FRAME_WINDOW_P (it->f))
18214 {
18215 int w = 0;
18216 struct glyph *g = to;
18217
18218 while (g >= toend && w < it->truncation_pixel_width)
18219 {
18220 w += g->pixel_width;
18221 --g;
18222 }
18223 if (to - g - tused > 0)
18224 to = g + tused;
18225 if (it->glyph_row->truncated_on_right_p
18226 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18227 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18228 {
18229 int extra = w - it->truncation_pixel_width;
18230
18231 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18232 }
18233 }
18234
18235 while (from >= end && to >= toend)
18236 *to-- = *from--;
18237 if (!FRAME_WINDOW_P (it->f))
18238 {
18239 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18240 {
18241 from =
18242 truncate_it.glyph_row->glyphs[TEXT_AREA]
18243 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18244 while (from >= end && to >= toend)
18245 *to-- = *from--;
18246 }
18247 }
18248 if (from >= end)
18249 {
18250 /* Need to free some room before prepending additional
18251 glyphs. */
18252 int move_by = from - end + 1;
18253 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18254 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18255
18256 for ( ; g >= g0; g--)
18257 g[move_by] = *g;
18258 while (from >= end)
18259 *to-- = *from--;
18260 it->glyph_row->used[TEXT_AREA] += move_by;
18261 }
18262 }
18263 }
18264
18265 /* Compute the hash code for ROW. */
18266 unsigned
18267 row_hash (struct glyph_row *row)
18268 {
18269 int area, k;
18270 unsigned hashval = 0;
18271
18272 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18273 for (k = 0; k < row->used[area]; ++k)
18274 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18275 + row->glyphs[area][k].u.val
18276 + row->glyphs[area][k].face_id
18277 + row->glyphs[area][k].padding_p
18278 + (row->glyphs[area][k].type << 2));
18279
18280 return hashval;
18281 }
18282
18283 /* Compute the pixel height and width of IT->glyph_row.
18284
18285 Most of the time, ascent and height of a display line will be equal
18286 to the max_ascent and max_height values of the display iterator
18287 structure. This is not the case if
18288
18289 1. We hit ZV without displaying anything. In this case, max_ascent
18290 and max_height will be zero.
18291
18292 2. We have some glyphs that don't contribute to the line height.
18293 (The glyph row flag contributes_to_line_height_p is for future
18294 pixmap extensions).
18295
18296 The first case is easily covered by using default values because in
18297 these cases, the line height does not really matter, except that it
18298 must not be zero. */
18299
18300 static void
18301 compute_line_metrics (struct it *it)
18302 {
18303 struct glyph_row *row = it->glyph_row;
18304
18305 if (FRAME_WINDOW_P (it->f))
18306 {
18307 int i, min_y, max_y;
18308
18309 /* The line may consist of one space only, that was added to
18310 place the cursor on it. If so, the row's height hasn't been
18311 computed yet. */
18312 if (row->height == 0)
18313 {
18314 if (it->max_ascent + it->max_descent == 0)
18315 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18316 row->ascent = it->max_ascent;
18317 row->height = it->max_ascent + it->max_descent;
18318 row->phys_ascent = it->max_phys_ascent;
18319 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18320 row->extra_line_spacing = it->max_extra_line_spacing;
18321 }
18322
18323 /* Compute the width of this line. */
18324 row->pixel_width = row->x;
18325 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18326 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18327
18328 eassert (row->pixel_width >= 0);
18329 eassert (row->ascent >= 0 && row->height > 0);
18330
18331 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18332 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18333
18334 /* If first line's physical ascent is larger than its logical
18335 ascent, use the physical ascent, and make the row taller.
18336 This makes accented characters fully visible. */
18337 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18338 && row->phys_ascent > row->ascent)
18339 {
18340 row->height += row->phys_ascent - row->ascent;
18341 row->ascent = row->phys_ascent;
18342 }
18343
18344 /* Compute how much of the line is visible. */
18345 row->visible_height = row->height;
18346
18347 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18348 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18349
18350 if (row->y < min_y)
18351 row->visible_height -= min_y - row->y;
18352 if (row->y + row->height > max_y)
18353 row->visible_height -= row->y + row->height - max_y;
18354 }
18355 else
18356 {
18357 row->pixel_width = row->used[TEXT_AREA];
18358 if (row->continued_p)
18359 row->pixel_width -= it->continuation_pixel_width;
18360 else if (row->truncated_on_right_p)
18361 row->pixel_width -= it->truncation_pixel_width;
18362 row->ascent = row->phys_ascent = 0;
18363 row->height = row->phys_height = row->visible_height = 1;
18364 row->extra_line_spacing = 0;
18365 }
18366
18367 /* Compute a hash code for this row. */
18368 row->hash = row_hash (row);
18369
18370 it->max_ascent = it->max_descent = 0;
18371 it->max_phys_ascent = it->max_phys_descent = 0;
18372 }
18373
18374
18375 /* Append one space to the glyph row of iterator IT if doing a
18376 window-based redisplay. The space has the same face as
18377 IT->face_id. Value is non-zero if a space was added.
18378
18379 This function is called to make sure that there is always one glyph
18380 at the end of a glyph row that the cursor can be set on under
18381 window-systems. (If there weren't such a glyph we would not know
18382 how wide and tall a box cursor should be displayed).
18383
18384 At the same time this space let's a nicely handle clearing to the
18385 end of the line if the row ends in italic text. */
18386
18387 static int
18388 append_space_for_newline (struct it *it, int default_face_p)
18389 {
18390 if (FRAME_WINDOW_P (it->f))
18391 {
18392 int n = it->glyph_row->used[TEXT_AREA];
18393
18394 if (it->glyph_row->glyphs[TEXT_AREA] + n
18395 < it->glyph_row->glyphs[1 + TEXT_AREA])
18396 {
18397 /* Save some values that must not be changed.
18398 Must save IT->c and IT->len because otherwise
18399 ITERATOR_AT_END_P wouldn't work anymore after
18400 append_space_for_newline has been called. */
18401 enum display_element_type saved_what = it->what;
18402 int saved_c = it->c, saved_len = it->len;
18403 int saved_char_to_display = it->char_to_display;
18404 int saved_x = it->current_x;
18405 int saved_face_id = it->face_id;
18406 int saved_box_end = it->end_of_box_run_p;
18407 struct text_pos saved_pos;
18408 Lisp_Object saved_object;
18409 struct face *face;
18410
18411 saved_object = it->object;
18412 saved_pos = it->position;
18413
18414 it->what = IT_CHARACTER;
18415 memset (&it->position, 0, sizeof it->position);
18416 it->object = make_number (0);
18417 it->c = it->char_to_display = ' ';
18418 it->len = 1;
18419
18420 /* If the default face was remapped, be sure to use the
18421 remapped face for the appended newline. */
18422 if (default_face_p)
18423 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18424 else if (it->face_before_selective_p)
18425 it->face_id = it->saved_face_id;
18426 face = FACE_FROM_ID (it->f, it->face_id);
18427 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18428 /* In R2L rows, we will prepend a stretch glyph that will
18429 have the end_of_box_run_p flag set for it, so there's no
18430 need for the appended newline glyph to have that flag
18431 set. */
18432 if (it->glyph_row->reversed_p
18433 /* But if the appended newline glyph goes all the way to
18434 the end of the row, there will be no stretch glyph,
18435 so leave the box flag set. */
18436 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18437 it->end_of_box_run_p = 0;
18438
18439 PRODUCE_GLYPHS (it);
18440
18441 it->override_ascent = -1;
18442 it->constrain_row_ascent_descent_p = 0;
18443 it->current_x = saved_x;
18444 it->object = saved_object;
18445 it->position = saved_pos;
18446 it->what = saved_what;
18447 it->face_id = saved_face_id;
18448 it->len = saved_len;
18449 it->c = saved_c;
18450 it->char_to_display = saved_char_to_display;
18451 it->end_of_box_run_p = saved_box_end;
18452 return 1;
18453 }
18454 }
18455
18456 return 0;
18457 }
18458
18459
18460 /* Extend the face of the last glyph in the text area of IT->glyph_row
18461 to the end of the display line. Called from display_line. If the
18462 glyph row is empty, add a space glyph to it so that we know the
18463 face to draw. Set the glyph row flag fill_line_p. If the glyph
18464 row is R2L, prepend a stretch glyph to cover the empty space to the
18465 left of the leftmost glyph. */
18466
18467 static void
18468 extend_face_to_end_of_line (struct it *it)
18469 {
18470 struct face *face, *default_face;
18471 struct frame *f = it->f;
18472
18473 /* If line is already filled, do nothing. Non window-system frames
18474 get a grace of one more ``pixel'' because their characters are
18475 1-``pixel'' wide, so they hit the equality too early. This grace
18476 is needed only for R2L rows that are not continued, to produce
18477 one extra blank where we could display the cursor. */
18478 if (it->current_x >= it->last_visible_x
18479 + (!FRAME_WINDOW_P (f)
18480 && it->glyph_row->reversed_p
18481 && !it->glyph_row->continued_p))
18482 return;
18483
18484 /* The default face, possibly remapped. */
18485 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18486
18487 /* Face extension extends the background and box of IT->face_id
18488 to the end of the line. If the background equals the background
18489 of the frame, we don't have to do anything. */
18490 if (it->face_before_selective_p)
18491 face = FACE_FROM_ID (f, it->saved_face_id);
18492 else
18493 face = FACE_FROM_ID (f, it->face_id);
18494
18495 if (FRAME_WINDOW_P (f)
18496 && it->glyph_row->displays_text_p
18497 && face->box == FACE_NO_BOX
18498 && face->background == FRAME_BACKGROUND_PIXEL (f)
18499 && !face->stipple
18500 && !it->glyph_row->reversed_p)
18501 return;
18502
18503 /* Set the glyph row flag indicating that the face of the last glyph
18504 in the text area has to be drawn to the end of the text area. */
18505 it->glyph_row->fill_line_p = 1;
18506
18507 /* If current character of IT is not ASCII, make sure we have the
18508 ASCII face. This will be automatically undone the next time
18509 get_next_display_element returns a multibyte character. Note
18510 that the character will always be single byte in unibyte
18511 text. */
18512 if (!ASCII_CHAR_P (it->c))
18513 {
18514 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18515 }
18516
18517 if (FRAME_WINDOW_P (f))
18518 {
18519 /* If the row is empty, add a space with the current face of IT,
18520 so that we know which face to draw. */
18521 if (it->glyph_row->used[TEXT_AREA] == 0)
18522 {
18523 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18524 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18525 it->glyph_row->used[TEXT_AREA] = 1;
18526 }
18527 #ifdef HAVE_WINDOW_SYSTEM
18528 if (it->glyph_row->reversed_p)
18529 {
18530 /* Prepend a stretch glyph to the row, such that the
18531 rightmost glyph will be drawn flushed all the way to the
18532 right margin of the window. The stretch glyph that will
18533 occupy the empty space, if any, to the left of the
18534 glyphs. */
18535 struct font *font = face->font ? face->font : FRAME_FONT (f);
18536 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18537 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18538 struct glyph *g;
18539 int row_width, stretch_ascent, stretch_width;
18540 struct text_pos saved_pos;
18541 int saved_face_id, saved_avoid_cursor, saved_box_start;
18542
18543 for (row_width = 0, g = row_start; g < row_end; g++)
18544 row_width += g->pixel_width;
18545 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18546 if (stretch_width > 0)
18547 {
18548 stretch_ascent =
18549 (((it->ascent + it->descent)
18550 * FONT_BASE (font)) / FONT_HEIGHT (font));
18551 saved_pos = it->position;
18552 memset (&it->position, 0, sizeof it->position);
18553 saved_avoid_cursor = it->avoid_cursor_p;
18554 it->avoid_cursor_p = 1;
18555 saved_face_id = it->face_id;
18556 saved_box_start = it->start_of_box_run_p;
18557 /* The last row's stretch glyph should get the default
18558 face, to avoid painting the rest of the window with
18559 the region face, if the region ends at ZV. */
18560 if (it->glyph_row->ends_at_zv_p)
18561 it->face_id = default_face->id;
18562 else
18563 it->face_id = face->id;
18564 it->start_of_box_run_p = 0;
18565 append_stretch_glyph (it, make_number (0), stretch_width,
18566 it->ascent + it->descent, stretch_ascent);
18567 it->position = saved_pos;
18568 it->avoid_cursor_p = saved_avoid_cursor;
18569 it->face_id = saved_face_id;
18570 it->start_of_box_run_p = saved_box_start;
18571 }
18572 }
18573 #endif /* HAVE_WINDOW_SYSTEM */
18574 }
18575 else
18576 {
18577 /* Save some values that must not be changed. */
18578 int saved_x = it->current_x;
18579 struct text_pos saved_pos;
18580 Lisp_Object saved_object;
18581 enum display_element_type saved_what = it->what;
18582 int saved_face_id = it->face_id;
18583
18584 saved_object = it->object;
18585 saved_pos = it->position;
18586
18587 it->what = IT_CHARACTER;
18588 memset (&it->position, 0, sizeof it->position);
18589 it->object = make_number (0);
18590 it->c = it->char_to_display = ' ';
18591 it->len = 1;
18592 /* The last row's blank glyphs should get the default face, to
18593 avoid painting the rest of the window with the region face,
18594 if the region ends at ZV. */
18595 if (it->glyph_row->ends_at_zv_p)
18596 it->face_id = default_face->id;
18597 else
18598 it->face_id = face->id;
18599
18600 PRODUCE_GLYPHS (it);
18601
18602 while (it->current_x <= it->last_visible_x)
18603 PRODUCE_GLYPHS (it);
18604
18605 /* Don't count these blanks really. It would let us insert a left
18606 truncation glyph below and make us set the cursor on them, maybe. */
18607 it->current_x = saved_x;
18608 it->object = saved_object;
18609 it->position = saved_pos;
18610 it->what = saved_what;
18611 it->face_id = saved_face_id;
18612 }
18613 }
18614
18615
18616 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18617 trailing whitespace. */
18618
18619 static int
18620 trailing_whitespace_p (ptrdiff_t charpos)
18621 {
18622 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18623 int c = 0;
18624
18625 while (bytepos < ZV_BYTE
18626 && (c = FETCH_CHAR (bytepos),
18627 c == ' ' || c == '\t'))
18628 ++bytepos;
18629
18630 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18631 {
18632 if (bytepos != PT_BYTE)
18633 return 1;
18634 }
18635 return 0;
18636 }
18637
18638
18639 /* Highlight trailing whitespace, if any, in ROW. */
18640
18641 static void
18642 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18643 {
18644 int used = row->used[TEXT_AREA];
18645
18646 if (used)
18647 {
18648 struct glyph *start = row->glyphs[TEXT_AREA];
18649 struct glyph *glyph = start + used - 1;
18650
18651 if (row->reversed_p)
18652 {
18653 /* Right-to-left rows need to be processed in the opposite
18654 direction, so swap the edge pointers. */
18655 glyph = start;
18656 start = row->glyphs[TEXT_AREA] + used - 1;
18657 }
18658
18659 /* Skip over glyphs inserted to display the cursor at the
18660 end of a line, for extending the face of the last glyph
18661 to the end of the line on terminals, and for truncation
18662 and continuation glyphs. */
18663 if (!row->reversed_p)
18664 {
18665 while (glyph >= start
18666 && glyph->type == CHAR_GLYPH
18667 && INTEGERP (glyph->object))
18668 --glyph;
18669 }
18670 else
18671 {
18672 while (glyph <= start
18673 && glyph->type == CHAR_GLYPH
18674 && INTEGERP (glyph->object))
18675 ++glyph;
18676 }
18677
18678 /* If last glyph is a space or stretch, and it's trailing
18679 whitespace, set the face of all trailing whitespace glyphs in
18680 IT->glyph_row to `trailing-whitespace'. */
18681 if ((row->reversed_p ? glyph <= start : glyph >= start)
18682 && BUFFERP (glyph->object)
18683 && (glyph->type == STRETCH_GLYPH
18684 || (glyph->type == CHAR_GLYPH
18685 && glyph->u.ch == ' '))
18686 && trailing_whitespace_p (glyph->charpos))
18687 {
18688 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18689 if (face_id < 0)
18690 return;
18691
18692 if (!row->reversed_p)
18693 {
18694 while (glyph >= start
18695 && BUFFERP (glyph->object)
18696 && (glyph->type == STRETCH_GLYPH
18697 || (glyph->type == CHAR_GLYPH
18698 && glyph->u.ch == ' ')))
18699 (glyph--)->face_id = face_id;
18700 }
18701 else
18702 {
18703 while (glyph <= start
18704 && BUFFERP (glyph->object)
18705 && (glyph->type == STRETCH_GLYPH
18706 || (glyph->type == CHAR_GLYPH
18707 && glyph->u.ch == ' ')))
18708 (glyph++)->face_id = face_id;
18709 }
18710 }
18711 }
18712 }
18713
18714
18715 /* Value is non-zero if glyph row ROW should be
18716 used to hold the cursor. */
18717
18718 static int
18719 cursor_row_p (struct glyph_row *row)
18720 {
18721 int result = 1;
18722
18723 if (PT == CHARPOS (row->end.pos)
18724 || PT == MATRIX_ROW_END_CHARPOS (row))
18725 {
18726 /* Suppose the row ends on a string.
18727 Unless the row is continued, that means it ends on a newline
18728 in the string. If it's anything other than a display string
18729 (e.g., a before-string from an overlay), we don't want the
18730 cursor there. (This heuristic seems to give the optimal
18731 behavior for the various types of multi-line strings.)
18732 One exception: if the string has `cursor' property on one of
18733 its characters, we _do_ want the cursor there. */
18734 if (CHARPOS (row->end.string_pos) >= 0)
18735 {
18736 if (row->continued_p)
18737 result = 1;
18738 else
18739 {
18740 /* Check for `display' property. */
18741 struct glyph *beg = row->glyphs[TEXT_AREA];
18742 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18743 struct glyph *glyph;
18744
18745 result = 0;
18746 for (glyph = end; glyph >= beg; --glyph)
18747 if (STRINGP (glyph->object))
18748 {
18749 Lisp_Object prop
18750 = Fget_char_property (make_number (PT),
18751 Qdisplay, Qnil);
18752 result =
18753 (!NILP (prop)
18754 && display_prop_string_p (prop, glyph->object));
18755 /* If there's a `cursor' property on one of the
18756 string's characters, this row is a cursor row,
18757 even though this is not a display string. */
18758 if (!result)
18759 {
18760 Lisp_Object s = glyph->object;
18761
18762 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18763 {
18764 ptrdiff_t gpos = glyph->charpos;
18765
18766 if (!NILP (Fget_char_property (make_number (gpos),
18767 Qcursor, s)))
18768 {
18769 result = 1;
18770 break;
18771 }
18772 }
18773 }
18774 break;
18775 }
18776 }
18777 }
18778 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18779 {
18780 /* If the row ends in middle of a real character,
18781 and the line is continued, we want the cursor here.
18782 That's because CHARPOS (ROW->end.pos) would equal
18783 PT if PT is before the character. */
18784 if (!row->ends_in_ellipsis_p)
18785 result = row->continued_p;
18786 else
18787 /* If the row ends in an ellipsis, then
18788 CHARPOS (ROW->end.pos) will equal point after the
18789 invisible text. We want that position to be displayed
18790 after the ellipsis. */
18791 result = 0;
18792 }
18793 /* If the row ends at ZV, display the cursor at the end of that
18794 row instead of at the start of the row below. */
18795 else if (row->ends_at_zv_p)
18796 result = 1;
18797 else
18798 result = 0;
18799 }
18800
18801 return result;
18802 }
18803
18804 \f
18805
18806 /* Push the property PROP so that it will be rendered at the current
18807 position in IT. Return 1 if PROP was successfully pushed, 0
18808 otherwise. Called from handle_line_prefix to handle the
18809 `line-prefix' and `wrap-prefix' properties. */
18810
18811 static int
18812 push_prefix_prop (struct it *it, Lisp_Object prop)
18813 {
18814 struct text_pos pos =
18815 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18816
18817 eassert (it->method == GET_FROM_BUFFER
18818 || it->method == GET_FROM_DISPLAY_VECTOR
18819 || it->method == GET_FROM_STRING);
18820
18821 /* We need to save the current buffer/string position, so it will be
18822 restored by pop_it, because iterate_out_of_display_property
18823 depends on that being set correctly, but some situations leave
18824 it->position not yet set when this function is called. */
18825 push_it (it, &pos);
18826
18827 if (STRINGP (prop))
18828 {
18829 if (SCHARS (prop) == 0)
18830 {
18831 pop_it (it);
18832 return 0;
18833 }
18834
18835 it->string = prop;
18836 it->string_from_prefix_prop_p = 1;
18837 it->multibyte_p = STRING_MULTIBYTE (it->string);
18838 it->current.overlay_string_index = -1;
18839 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18840 it->end_charpos = it->string_nchars = SCHARS (it->string);
18841 it->method = GET_FROM_STRING;
18842 it->stop_charpos = 0;
18843 it->prev_stop = 0;
18844 it->base_level_stop = 0;
18845
18846 /* Force paragraph direction to be that of the parent
18847 buffer/string. */
18848 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18849 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18850 else
18851 it->paragraph_embedding = L2R;
18852
18853 /* Set up the bidi iterator for this display string. */
18854 if (it->bidi_p)
18855 {
18856 it->bidi_it.string.lstring = it->string;
18857 it->bidi_it.string.s = NULL;
18858 it->bidi_it.string.schars = it->end_charpos;
18859 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18860 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18861 it->bidi_it.string.unibyte = !it->multibyte_p;
18862 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18863 }
18864 }
18865 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18866 {
18867 it->method = GET_FROM_STRETCH;
18868 it->object = prop;
18869 }
18870 #ifdef HAVE_WINDOW_SYSTEM
18871 else if (IMAGEP (prop))
18872 {
18873 it->what = IT_IMAGE;
18874 it->image_id = lookup_image (it->f, prop);
18875 it->method = GET_FROM_IMAGE;
18876 }
18877 #endif /* HAVE_WINDOW_SYSTEM */
18878 else
18879 {
18880 pop_it (it); /* bogus display property, give up */
18881 return 0;
18882 }
18883
18884 return 1;
18885 }
18886
18887 /* Return the character-property PROP at the current position in IT. */
18888
18889 static Lisp_Object
18890 get_it_property (struct it *it, Lisp_Object prop)
18891 {
18892 Lisp_Object position;
18893
18894 if (STRINGP (it->object))
18895 position = make_number (IT_STRING_CHARPOS (*it));
18896 else if (BUFFERP (it->object))
18897 position = make_number (IT_CHARPOS (*it));
18898 else
18899 return Qnil;
18900
18901 return Fget_char_property (position, prop, it->object);
18902 }
18903
18904 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18905
18906 static void
18907 handle_line_prefix (struct it *it)
18908 {
18909 Lisp_Object prefix;
18910
18911 if (it->continuation_lines_width > 0)
18912 {
18913 prefix = get_it_property (it, Qwrap_prefix);
18914 if (NILP (prefix))
18915 prefix = Vwrap_prefix;
18916 }
18917 else
18918 {
18919 prefix = get_it_property (it, Qline_prefix);
18920 if (NILP (prefix))
18921 prefix = Vline_prefix;
18922 }
18923 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18924 {
18925 /* If the prefix is wider than the window, and we try to wrap
18926 it, it would acquire its own wrap prefix, and so on till the
18927 iterator stack overflows. So, don't wrap the prefix. */
18928 it->line_wrap = TRUNCATE;
18929 it->avoid_cursor_p = 1;
18930 }
18931 }
18932
18933 \f
18934
18935 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18936 only for R2L lines from display_line and display_string, when they
18937 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
18938 the line/string needs to be continued on the next glyph row. */
18939 static void
18940 unproduce_glyphs (struct it *it, int n)
18941 {
18942 struct glyph *glyph, *end;
18943
18944 eassert (it->glyph_row);
18945 eassert (it->glyph_row->reversed_p);
18946 eassert (it->area == TEXT_AREA);
18947 eassert (n <= it->glyph_row->used[TEXT_AREA]);
18948
18949 if (n > it->glyph_row->used[TEXT_AREA])
18950 n = it->glyph_row->used[TEXT_AREA];
18951 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
18952 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
18953 for ( ; glyph < end; glyph++)
18954 glyph[-n] = *glyph;
18955 }
18956
18957 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
18958 and ROW->maxpos. */
18959 static void
18960 find_row_edges (struct it *it, struct glyph_row *row,
18961 ptrdiff_t min_pos, ptrdiff_t min_bpos,
18962 ptrdiff_t max_pos, ptrdiff_t max_bpos)
18963 {
18964 /* FIXME: Revisit this when glyph ``spilling'' in continuation
18965 lines' rows is implemented for bidi-reordered rows. */
18966
18967 /* ROW->minpos is the value of min_pos, the minimal buffer position
18968 we have in ROW, or ROW->start.pos if that is smaller. */
18969 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
18970 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
18971 else
18972 /* We didn't find buffer positions smaller than ROW->start, or
18973 didn't find _any_ valid buffer positions in any of the glyphs,
18974 so we must trust the iterator's computed positions. */
18975 row->minpos = row->start.pos;
18976 if (max_pos <= 0)
18977 {
18978 max_pos = CHARPOS (it->current.pos);
18979 max_bpos = BYTEPOS (it->current.pos);
18980 }
18981
18982 /* Here are the various use-cases for ending the row, and the
18983 corresponding values for ROW->maxpos:
18984
18985 Line ends in a newline from buffer eol_pos + 1
18986 Line is continued from buffer max_pos + 1
18987 Line is truncated on right it->current.pos
18988 Line ends in a newline from string max_pos + 1(*)
18989 (*) + 1 only when line ends in a forward scan
18990 Line is continued from string max_pos
18991 Line is continued from display vector max_pos
18992 Line is entirely from a string min_pos == max_pos
18993 Line is entirely from a display vector min_pos == max_pos
18994 Line that ends at ZV ZV
18995
18996 If you discover other use-cases, please add them here as
18997 appropriate. */
18998 if (row->ends_at_zv_p)
18999 row->maxpos = it->current.pos;
19000 else if (row->used[TEXT_AREA])
19001 {
19002 int seen_this_string = 0;
19003 struct glyph_row *r1 = row - 1;
19004
19005 /* Did we see the same display string on the previous row? */
19006 if (STRINGP (it->object)
19007 /* this is not the first row */
19008 && row > it->w->desired_matrix->rows
19009 /* previous row is not the header line */
19010 && !r1->mode_line_p
19011 /* previous row also ends in a newline from a string */
19012 && r1->ends_in_newline_from_string_p)
19013 {
19014 struct glyph *start, *end;
19015
19016 /* Search for the last glyph of the previous row that came
19017 from buffer or string. Depending on whether the row is
19018 L2R or R2L, we need to process it front to back or the
19019 other way round. */
19020 if (!r1->reversed_p)
19021 {
19022 start = r1->glyphs[TEXT_AREA];
19023 end = start + r1->used[TEXT_AREA];
19024 /* Glyphs inserted by redisplay have an integer (zero)
19025 as their object. */
19026 while (end > start
19027 && INTEGERP ((end - 1)->object)
19028 && (end - 1)->charpos <= 0)
19029 --end;
19030 if (end > start)
19031 {
19032 if (EQ ((end - 1)->object, it->object))
19033 seen_this_string = 1;
19034 }
19035 else
19036 /* If all the glyphs of the previous row were inserted
19037 by redisplay, it means the previous row was
19038 produced from a single newline, which is only
19039 possible if that newline came from the same string
19040 as the one which produced this ROW. */
19041 seen_this_string = 1;
19042 }
19043 else
19044 {
19045 end = r1->glyphs[TEXT_AREA] - 1;
19046 start = end + r1->used[TEXT_AREA];
19047 while (end < start
19048 && INTEGERP ((end + 1)->object)
19049 && (end + 1)->charpos <= 0)
19050 ++end;
19051 if (end < start)
19052 {
19053 if (EQ ((end + 1)->object, it->object))
19054 seen_this_string = 1;
19055 }
19056 else
19057 seen_this_string = 1;
19058 }
19059 }
19060 /* Take note of each display string that covers a newline only
19061 once, the first time we see it. This is for when a display
19062 string includes more than one newline in it. */
19063 if (row->ends_in_newline_from_string_p && !seen_this_string)
19064 {
19065 /* If we were scanning the buffer forward when we displayed
19066 the string, we want to account for at least one buffer
19067 position that belongs to this row (position covered by
19068 the display string), so that cursor positioning will
19069 consider this row as a candidate when point is at the end
19070 of the visual line represented by this row. This is not
19071 required when scanning back, because max_pos will already
19072 have a much larger value. */
19073 if (CHARPOS (row->end.pos) > max_pos)
19074 INC_BOTH (max_pos, max_bpos);
19075 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19076 }
19077 else if (CHARPOS (it->eol_pos) > 0)
19078 SET_TEXT_POS (row->maxpos,
19079 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19080 else if (row->continued_p)
19081 {
19082 /* If max_pos is different from IT's current position, it
19083 means IT->method does not belong to the display element
19084 at max_pos. However, it also means that the display
19085 element at max_pos was displayed in its entirety on this
19086 line, which is equivalent to saying that the next line
19087 starts at the next buffer position. */
19088 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19089 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19090 else
19091 {
19092 INC_BOTH (max_pos, max_bpos);
19093 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19094 }
19095 }
19096 else if (row->truncated_on_right_p)
19097 /* display_line already called reseat_at_next_visible_line_start,
19098 which puts the iterator at the beginning of the next line, in
19099 the logical order. */
19100 row->maxpos = it->current.pos;
19101 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19102 /* A line that is entirely from a string/image/stretch... */
19103 row->maxpos = row->minpos;
19104 else
19105 emacs_abort ();
19106 }
19107 else
19108 row->maxpos = it->current.pos;
19109 }
19110
19111 /* Construct the glyph row IT->glyph_row in the desired matrix of
19112 IT->w from text at the current position of IT. See dispextern.h
19113 for an overview of struct it. Value is non-zero if
19114 IT->glyph_row displays text, as opposed to a line displaying ZV
19115 only. */
19116
19117 static int
19118 display_line (struct it *it)
19119 {
19120 struct glyph_row *row = it->glyph_row;
19121 Lisp_Object overlay_arrow_string;
19122 struct it wrap_it;
19123 void *wrap_data = NULL;
19124 int may_wrap = 0, wrap_x IF_LINT (= 0);
19125 int wrap_row_used = -1;
19126 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19127 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19128 int wrap_row_extra_line_spacing IF_LINT (= 0);
19129 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19130 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19131 int cvpos;
19132 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19133 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19134
19135 /* We always start displaying at hpos zero even if hscrolled. */
19136 eassert (it->hpos == 0 && it->current_x == 0);
19137
19138 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19139 >= it->w->desired_matrix->nrows)
19140 {
19141 it->w->nrows_scale_factor++;
19142 fonts_changed_p = 1;
19143 return 0;
19144 }
19145
19146 /* Is IT->w showing the region? */
19147 it->w->region_showing = it->region_beg_charpos > 0 ? -1 : 0;
19148
19149 /* Clear the result glyph row and enable it. */
19150 prepare_desired_row (row);
19151
19152 row->y = it->current_y;
19153 row->start = it->start;
19154 row->continuation_lines_width = it->continuation_lines_width;
19155 row->displays_text_p = 1;
19156 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19157 it->starts_in_middle_of_char_p = 0;
19158
19159 /* Arrange the overlays nicely for our purposes. Usually, we call
19160 display_line on only one line at a time, in which case this
19161 can't really hurt too much, or we call it on lines which appear
19162 one after another in the buffer, in which case all calls to
19163 recenter_overlay_lists but the first will be pretty cheap. */
19164 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19165
19166 /* Move over display elements that are not visible because we are
19167 hscrolled. This may stop at an x-position < IT->first_visible_x
19168 if the first glyph is partially visible or if we hit a line end. */
19169 if (it->current_x < it->first_visible_x)
19170 {
19171 enum move_it_result move_result;
19172
19173 this_line_min_pos = row->start.pos;
19174 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19175 MOVE_TO_POS | MOVE_TO_X);
19176 /* If we are under a large hscroll, move_it_in_display_line_to
19177 could hit the end of the line without reaching
19178 it->first_visible_x. Pretend that we did reach it. This is
19179 especially important on a TTY, where we will call
19180 extend_face_to_end_of_line, which needs to know how many
19181 blank glyphs to produce. */
19182 if (it->current_x < it->first_visible_x
19183 && (move_result == MOVE_NEWLINE_OR_CR
19184 || move_result == MOVE_POS_MATCH_OR_ZV))
19185 it->current_x = it->first_visible_x;
19186
19187 /* Record the smallest positions seen while we moved over
19188 display elements that are not visible. This is needed by
19189 redisplay_internal for optimizing the case where the cursor
19190 stays inside the same line. The rest of this function only
19191 considers positions that are actually displayed, so
19192 RECORD_MAX_MIN_POS will not otherwise record positions that
19193 are hscrolled to the left of the left edge of the window. */
19194 min_pos = CHARPOS (this_line_min_pos);
19195 min_bpos = BYTEPOS (this_line_min_pos);
19196 }
19197 else
19198 {
19199 /* We only do this when not calling `move_it_in_display_line_to'
19200 above, because move_it_in_display_line_to calls
19201 handle_line_prefix itself. */
19202 handle_line_prefix (it);
19203 }
19204
19205 /* Get the initial row height. This is either the height of the
19206 text hscrolled, if there is any, or zero. */
19207 row->ascent = it->max_ascent;
19208 row->height = it->max_ascent + it->max_descent;
19209 row->phys_ascent = it->max_phys_ascent;
19210 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19211 row->extra_line_spacing = it->max_extra_line_spacing;
19212
19213 /* Utility macro to record max and min buffer positions seen until now. */
19214 #define RECORD_MAX_MIN_POS(IT) \
19215 do \
19216 { \
19217 int composition_p = !STRINGP ((IT)->string) \
19218 && ((IT)->what == IT_COMPOSITION); \
19219 ptrdiff_t current_pos = \
19220 composition_p ? (IT)->cmp_it.charpos \
19221 : IT_CHARPOS (*(IT)); \
19222 ptrdiff_t current_bpos = \
19223 composition_p ? CHAR_TO_BYTE (current_pos) \
19224 : IT_BYTEPOS (*(IT)); \
19225 if (current_pos < min_pos) \
19226 { \
19227 min_pos = current_pos; \
19228 min_bpos = current_bpos; \
19229 } \
19230 if (IT_CHARPOS (*it) > max_pos) \
19231 { \
19232 max_pos = IT_CHARPOS (*it); \
19233 max_bpos = IT_BYTEPOS (*it); \
19234 } \
19235 } \
19236 while (0)
19237
19238 /* Loop generating characters. The loop is left with IT on the next
19239 character to display. */
19240 while (1)
19241 {
19242 int n_glyphs_before, hpos_before, x_before;
19243 int x, nglyphs;
19244 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19245
19246 /* Retrieve the next thing to display. Value is zero if end of
19247 buffer reached. */
19248 if (!get_next_display_element (it))
19249 {
19250 /* Maybe add a space at the end of this line that is used to
19251 display the cursor there under X. Set the charpos of the
19252 first glyph of blank lines not corresponding to any text
19253 to -1. */
19254 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19255 row->exact_window_width_line_p = 1;
19256 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19257 || row->used[TEXT_AREA] == 0)
19258 {
19259 row->glyphs[TEXT_AREA]->charpos = -1;
19260 row->displays_text_p = 0;
19261
19262 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19263 && (!MINI_WINDOW_P (it->w)
19264 || (minibuf_level && EQ (it->window, minibuf_window))))
19265 row->indicate_empty_line_p = 1;
19266 }
19267
19268 it->continuation_lines_width = 0;
19269 row->ends_at_zv_p = 1;
19270 /* A row that displays right-to-left text must always have
19271 its last face extended all the way to the end of line,
19272 even if this row ends in ZV, because we still write to
19273 the screen left to right. We also need to extend the
19274 last face if the default face is remapped to some
19275 different face, otherwise the functions that clear
19276 portions of the screen will clear with the default face's
19277 background color. */
19278 if (row->reversed_p
19279 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19280 extend_face_to_end_of_line (it);
19281 break;
19282 }
19283
19284 /* Now, get the metrics of what we want to display. This also
19285 generates glyphs in `row' (which is IT->glyph_row). */
19286 n_glyphs_before = row->used[TEXT_AREA];
19287 x = it->current_x;
19288
19289 /* Remember the line height so far in case the next element doesn't
19290 fit on the line. */
19291 if (it->line_wrap != TRUNCATE)
19292 {
19293 ascent = it->max_ascent;
19294 descent = it->max_descent;
19295 phys_ascent = it->max_phys_ascent;
19296 phys_descent = it->max_phys_descent;
19297
19298 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19299 {
19300 if (IT_DISPLAYING_WHITESPACE (it))
19301 may_wrap = 1;
19302 else if (may_wrap)
19303 {
19304 SAVE_IT (wrap_it, *it, wrap_data);
19305 wrap_x = x;
19306 wrap_row_used = row->used[TEXT_AREA];
19307 wrap_row_ascent = row->ascent;
19308 wrap_row_height = row->height;
19309 wrap_row_phys_ascent = row->phys_ascent;
19310 wrap_row_phys_height = row->phys_height;
19311 wrap_row_extra_line_spacing = row->extra_line_spacing;
19312 wrap_row_min_pos = min_pos;
19313 wrap_row_min_bpos = min_bpos;
19314 wrap_row_max_pos = max_pos;
19315 wrap_row_max_bpos = max_bpos;
19316 may_wrap = 0;
19317 }
19318 }
19319 }
19320
19321 PRODUCE_GLYPHS (it);
19322
19323 /* If this display element was in marginal areas, continue with
19324 the next one. */
19325 if (it->area != TEXT_AREA)
19326 {
19327 row->ascent = max (row->ascent, it->max_ascent);
19328 row->height = max (row->height, it->max_ascent + it->max_descent);
19329 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19330 row->phys_height = max (row->phys_height,
19331 it->max_phys_ascent + it->max_phys_descent);
19332 row->extra_line_spacing = max (row->extra_line_spacing,
19333 it->max_extra_line_spacing);
19334 set_iterator_to_next (it, 1);
19335 continue;
19336 }
19337
19338 /* Does the display element fit on the line? If we truncate
19339 lines, we should draw past the right edge of the window. If
19340 we don't truncate, we want to stop so that we can display the
19341 continuation glyph before the right margin. If lines are
19342 continued, there are two possible strategies for characters
19343 resulting in more than 1 glyph (e.g. tabs): Display as many
19344 glyphs as possible in this line and leave the rest for the
19345 continuation line, or display the whole element in the next
19346 line. Original redisplay did the former, so we do it also. */
19347 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19348 hpos_before = it->hpos;
19349 x_before = x;
19350
19351 if (/* Not a newline. */
19352 nglyphs > 0
19353 /* Glyphs produced fit entirely in the line. */
19354 && it->current_x < it->last_visible_x)
19355 {
19356 it->hpos += nglyphs;
19357 row->ascent = max (row->ascent, it->max_ascent);
19358 row->height = max (row->height, it->max_ascent + it->max_descent);
19359 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19360 row->phys_height = max (row->phys_height,
19361 it->max_phys_ascent + it->max_phys_descent);
19362 row->extra_line_spacing = max (row->extra_line_spacing,
19363 it->max_extra_line_spacing);
19364 if (it->current_x - it->pixel_width < it->first_visible_x)
19365 row->x = x - it->first_visible_x;
19366 /* Record the maximum and minimum buffer positions seen so
19367 far in glyphs that will be displayed by this row. */
19368 if (it->bidi_p)
19369 RECORD_MAX_MIN_POS (it);
19370 }
19371 else
19372 {
19373 int i, new_x;
19374 struct glyph *glyph;
19375
19376 for (i = 0; i < nglyphs; ++i, x = new_x)
19377 {
19378 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19379 new_x = x + glyph->pixel_width;
19380
19381 if (/* Lines are continued. */
19382 it->line_wrap != TRUNCATE
19383 && (/* Glyph doesn't fit on the line. */
19384 new_x > it->last_visible_x
19385 /* Or it fits exactly on a window system frame. */
19386 || (new_x == it->last_visible_x
19387 && FRAME_WINDOW_P (it->f)
19388 && (row->reversed_p
19389 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19390 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19391 {
19392 /* End of a continued line. */
19393
19394 if (it->hpos == 0
19395 || (new_x == it->last_visible_x
19396 && FRAME_WINDOW_P (it->f)
19397 && (row->reversed_p
19398 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19399 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19400 {
19401 /* Current glyph is the only one on the line or
19402 fits exactly on the line. We must continue
19403 the line because we can't draw the cursor
19404 after the glyph. */
19405 row->continued_p = 1;
19406 it->current_x = new_x;
19407 it->continuation_lines_width += new_x;
19408 ++it->hpos;
19409 if (i == nglyphs - 1)
19410 {
19411 /* If line-wrap is on, check if a previous
19412 wrap point was found. */
19413 if (wrap_row_used > 0
19414 /* Even if there is a previous wrap
19415 point, continue the line here as
19416 usual, if (i) the previous character
19417 was a space or tab AND (ii) the
19418 current character is not. */
19419 && (!may_wrap
19420 || IT_DISPLAYING_WHITESPACE (it)))
19421 goto back_to_wrap;
19422
19423 /* Record the maximum and minimum buffer
19424 positions seen so far in glyphs that will be
19425 displayed by this row. */
19426 if (it->bidi_p)
19427 RECORD_MAX_MIN_POS (it);
19428 set_iterator_to_next (it, 1);
19429 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19430 {
19431 if (!get_next_display_element (it))
19432 {
19433 row->exact_window_width_line_p = 1;
19434 it->continuation_lines_width = 0;
19435 row->continued_p = 0;
19436 row->ends_at_zv_p = 1;
19437 }
19438 else if (ITERATOR_AT_END_OF_LINE_P (it))
19439 {
19440 row->continued_p = 0;
19441 row->exact_window_width_line_p = 1;
19442 }
19443 }
19444 }
19445 else if (it->bidi_p)
19446 RECORD_MAX_MIN_POS (it);
19447 }
19448 else if (CHAR_GLYPH_PADDING_P (*glyph)
19449 && !FRAME_WINDOW_P (it->f))
19450 {
19451 /* A padding glyph that doesn't fit on this line.
19452 This means the whole character doesn't fit
19453 on the line. */
19454 if (row->reversed_p)
19455 unproduce_glyphs (it, row->used[TEXT_AREA]
19456 - n_glyphs_before);
19457 row->used[TEXT_AREA] = n_glyphs_before;
19458
19459 /* Fill the rest of the row with continuation
19460 glyphs like in 20.x. */
19461 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19462 < row->glyphs[1 + TEXT_AREA])
19463 produce_special_glyphs (it, IT_CONTINUATION);
19464
19465 row->continued_p = 1;
19466 it->current_x = x_before;
19467 it->continuation_lines_width += x_before;
19468
19469 /* Restore the height to what it was before the
19470 element not fitting on the line. */
19471 it->max_ascent = ascent;
19472 it->max_descent = descent;
19473 it->max_phys_ascent = phys_ascent;
19474 it->max_phys_descent = phys_descent;
19475 }
19476 else if (wrap_row_used > 0)
19477 {
19478 back_to_wrap:
19479 if (row->reversed_p)
19480 unproduce_glyphs (it,
19481 row->used[TEXT_AREA] - wrap_row_used);
19482 RESTORE_IT (it, &wrap_it, wrap_data);
19483 it->continuation_lines_width += wrap_x;
19484 row->used[TEXT_AREA] = wrap_row_used;
19485 row->ascent = wrap_row_ascent;
19486 row->height = wrap_row_height;
19487 row->phys_ascent = wrap_row_phys_ascent;
19488 row->phys_height = wrap_row_phys_height;
19489 row->extra_line_spacing = wrap_row_extra_line_spacing;
19490 min_pos = wrap_row_min_pos;
19491 min_bpos = wrap_row_min_bpos;
19492 max_pos = wrap_row_max_pos;
19493 max_bpos = wrap_row_max_bpos;
19494 row->continued_p = 1;
19495 row->ends_at_zv_p = 0;
19496 row->exact_window_width_line_p = 0;
19497 it->continuation_lines_width += x;
19498
19499 /* Make sure that a non-default face is extended
19500 up to the right margin of the window. */
19501 extend_face_to_end_of_line (it);
19502 }
19503 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19504 {
19505 /* A TAB that extends past the right edge of the
19506 window. This produces a single glyph on
19507 window system frames. We leave the glyph in
19508 this row and let it fill the row, but don't
19509 consume the TAB. */
19510 if ((row->reversed_p
19511 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19512 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19513 produce_special_glyphs (it, IT_CONTINUATION);
19514 it->continuation_lines_width += it->last_visible_x;
19515 row->ends_in_middle_of_char_p = 1;
19516 row->continued_p = 1;
19517 glyph->pixel_width = it->last_visible_x - x;
19518 it->starts_in_middle_of_char_p = 1;
19519 }
19520 else
19521 {
19522 /* Something other than a TAB that draws past
19523 the right edge of the window. Restore
19524 positions to values before the element. */
19525 if (row->reversed_p)
19526 unproduce_glyphs (it, row->used[TEXT_AREA]
19527 - (n_glyphs_before + i));
19528 row->used[TEXT_AREA] = n_glyphs_before + i;
19529
19530 /* Display continuation glyphs. */
19531 it->current_x = x_before;
19532 it->continuation_lines_width += x;
19533 if (!FRAME_WINDOW_P (it->f)
19534 || (row->reversed_p
19535 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19536 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19537 produce_special_glyphs (it, IT_CONTINUATION);
19538 row->continued_p = 1;
19539
19540 extend_face_to_end_of_line (it);
19541
19542 if (nglyphs > 1 && i > 0)
19543 {
19544 row->ends_in_middle_of_char_p = 1;
19545 it->starts_in_middle_of_char_p = 1;
19546 }
19547
19548 /* Restore the height to what it was before the
19549 element not fitting on the line. */
19550 it->max_ascent = ascent;
19551 it->max_descent = descent;
19552 it->max_phys_ascent = phys_ascent;
19553 it->max_phys_descent = phys_descent;
19554 }
19555
19556 break;
19557 }
19558 else if (new_x > it->first_visible_x)
19559 {
19560 /* Increment number of glyphs actually displayed. */
19561 ++it->hpos;
19562
19563 /* Record the maximum and minimum buffer positions
19564 seen so far in glyphs that will be displayed by
19565 this row. */
19566 if (it->bidi_p)
19567 RECORD_MAX_MIN_POS (it);
19568
19569 if (x < it->first_visible_x)
19570 /* Glyph is partially visible, i.e. row starts at
19571 negative X position. */
19572 row->x = x - it->first_visible_x;
19573 }
19574 else
19575 {
19576 /* Glyph is completely off the left margin of the
19577 window. This should not happen because of the
19578 move_it_in_display_line at the start of this
19579 function, unless the text display area of the
19580 window is empty. */
19581 eassert (it->first_visible_x <= it->last_visible_x);
19582 }
19583 }
19584 /* Even if this display element produced no glyphs at all,
19585 we want to record its position. */
19586 if (it->bidi_p && nglyphs == 0)
19587 RECORD_MAX_MIN_POS (it);
19588
19589 row->ascent = max (row->ascent, it->max_ascent);
19590 row->height = max (row->height, it->max_ascent + it->max_descent);
19591 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19592 row->phys_height = max (row->phys_height,
19593 it->max_phys_ascent + it->max_phys_descent);
19594 row->extra_line_spacing = max (row->extra_line_spacing,
19595 it->max_extra_line_spacing);
19596
19597 /* End of this display line if row is continued. */
19598 if (row->continued_p || row->ends_at_zv_p)
19599 break;
19600 }
19601
19602 at_end_of_line:
19603 /* Is this a line end? If yes, we're also done, after making
19604 sure that a non-default face is extended up to the right
19605 margin of the window. */
19606 if (ITERATOR_AT_END_OF_LINE_P (it))
19607 {
19608 int used_before = row->used[TEXT_AREA];
19609
19610 row->ends_in_newline_from_string_p = STRINGP (it->object);
19611
19612 /* Add a space at the end of the line that is used to
19613 display the cursor there. */
19614 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19615 append_space_for_newline (it, 0);
19616
19617 /* Extend the face to the end of the line. */
19618 extend_face_to_end_of_line (it);
19619
19620 /* Make sure we have the position. */
19621 if (used_before == 0)
19622 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19623
19624 /* Record the position of the newline, for use in
19625 find_row_edges. */
19626 it->eol_pos = it->current.pos;
19627
19628 /* Consume the line end. This skips over invisible lines. */
19629 set_iterator_to_next (it, 1);
19630 it->continuation_lines_width = 0;
19631 break;
19632 }
19633
19634 /* Proceed with next display element. Note that this skips
19635 over lines invisible because of selective display. */
19636 set_iterator_to_next (it, 1);
19637
19638 /* If we truncate lines, we are done when the last displayed
19639 glyphs reach past the right margin of the window. */
19640 if (it->line_wrap == TRUNCATE
19641 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19642 ? (it->current_x >= it->last_visible_x)
19643 : (it->current_x > it->last_visible_x)))
19644 {
19645 /* Maybe add truncation glyphs. */
19646 if (!FRAME_WINDOW_P (it->f)
19647 || (row->reversed_p
19648 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19649 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19650 {
19651 int i, n;
19652
19653 if (!row->reversed_p)
19654 {
19655 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19656 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19657 break;
19658 }
19659 else
19660 {
19661 for (i = 0; i < row->used[TEXT_AREA]; i++)
19662 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19663 break;
19664 /* Remove any padding glyphs at the front of ROW, to
19665 make room for the truncation glyphs we will be
19666 adding below. The loop below always inserts at
19667 least one truncation glyph, so also remove the
19668 last glyph added to ROW. */
19669 unproduce_glyphs (it, i + 1);
19670 /* Adjust i for the loop below. */
19671 i = row->used[TEXT_AREA] - (i + 1);
19672 }
19673
19674 it->current_x = x_before;
19675 if (!FRAME_WINDOW_P (it->f))
19676 {
19677 for (n = row->used[TEXT_AREA]; i < n; ++i)
19678 {
19679 row->used[TEXT_AREA] = i;
19680 produce_special_glyphs (it, IT_TRUNCATION);
19681 }
19682 }
19683 else
19684 {
19685 row->used[TEXT_AREA] = i;
19686 produce_special_glyphs (it, IT_TRUNCATION);
19687 }
19688 }
19689 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19690 {
19691 /* Don't truncate if we can overflow newline into fringe. */
19692 if (!get_next_display_element (it))
19693 {
19694 it->continuation_lines_width = 0;
19695 row->ends_at_zv_p = 1;
19696 row->exact_window_width_line_p = 1;
19697 break;
19698 }
19699 if (ITERATOR_AT_END_OF_LINE_P (it))
19700 {
19701 row->exact_window_width_line_p = 1;
19702 goto at_end_of_line;
19703 }
19704 it->current_x = x_before;
19705 }
19706
19707 row->truncated_on_right_p = 1;
19708 it->continuation_lines_width = 0;
19709 reseat_at_next_visible_line_start (it, 0);
19710 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19711 it->hpos = hpos_before;
19712 break;
19713 }
19714 }
19715
19716 if (wrap_data)
19717 bidi_unshelve_cache (wrap_data, 1);
19718
19719 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19720 at the left window margin. */
19721 if (it->first_visible_x
19722 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19723 {
19724 if (!FRAME_WINDOW_P (it->f)
19725 || (row->reversed_p
19726 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19727 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19728 insert_left_trunc_glyphs (it);
19729 row->truncated_on_left_p = 1;
19730 }
19731
19732 /* Remember the position at which this line ends.
19733
19734 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19735 cannot be before the call to find_row_edges below, since that is
19736 where these positions are determined. */
19737 row->end = it->current;
19738 if (!it->bidi_p)
19739 {
19740 row->minpos = row->start.pos;
19741 row->maxpos = row->end.pos;
19742 }
19743 else
19744 {
19745 /* ROW->minpos and ROW->maxpos must be the smallest and
19746 `1 + the largest' buffer positions in ROW. But if ROW was
19747 bidi-reordered, these two positions can be anywhere in the
19748 row, so we must determine them now. */
19749 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19750 }
19751
19752 /* If the start of this line is the overlay arrow-position, then
19753 mark this glyph row as the one containing the overlay arrow.
19754 This is clearly a mess with variable size fonts. It would be
19755 better to let it be displayed like cursors under X. */
19756 if ((row->displays_text_p || !overlay_arrow_seen)
19757 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19758 !NILP (overlay_arrow_string)))
19759 {
19760 /* Overlay arrow in window redisplay is a fringe bitmap. */
19761 if (STRINGP (overlay_arrow_string))
19762 {
19763 struct glyph_row *arrow_row
19764 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19765 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19766 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19767 struct glyph *p = row->glyphs[TEXT_AREA];
19768 struct glyph *p2, *end;
19769
19770 /* Copy the arrow glyphs. */
19771 while (glyph < arrow_end)
19772 *p++ = *glyph++;
19773
19774 /* Throw away padding glyphs. */
19775 p2 = p;
19776 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19777 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19778 ++p2;
19779 if (p2 > p)
19780 {
19781 while (p2 < end)
19782 *p++ = *p2++;
19783 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19784 }
19785 }
19786 else
19787 {
19788 eassert (INTEGERP (overlay_arrow_string));
19789 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19790 }
19791 overlay_arrow_seen = 1;
19792 }
19793
19794 /* Highlight trailing whitespace. */
19795 if (!NILP (Vshow_trailing_whitespace))
19796 highlight_trailing_whitespace (it->f, it->glyph_row);
19797
19798 /* Compute pixel dimensions of this line. */
19799 compute_line_metrics (it);
19800
19801 /* Implementation note: No changes in the glyphs of ROW or in their
19802 faces can be done past this point, because compute_line_metrics
19803 computes ROW's hash value and stores it within the glyph_row
19804 structure. */
19805
19806 /* Record whether this row ends inside an ellipsis. */
19807 row->ends_in_ellipsis_p
19808 = (it->method == GET_FROM_DISPLAY_VECTOR
19809 && it->ellipsis_p);
19810
19811 /* Save fringe bitmaps in this row. */
19812 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19813 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19814 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19815 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19816
19817 it->left_user_fringe_bitmap = 0;
19818 it->left_user_fringe_face_id = 0;
19819 it->right_user_fringe_bitmap = 0;
19820 it->right_user_fringe_face_id = 0;
19821
19822 /* Maybe set the cursor. */
19823 cvpos = it->w->cursor.vpos;
19824 if ((cvpos < 0
19825 /* In bidi-reordered rows, keep checking for proper cursor
19826 position even if one has been found already, because buffer
19827 positions in such rows change non-linearly with ROW->VPOS,
19828 when a line is continued. One exception: when we are at ZV,
19829 display cursor on the first suitable glyph row, since all
19830 the empty rows after that also have their position set to ZV. */
19831 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19832 lines' rows is implemented for bidi-reordered rows. */
19833 || (it->bidi_p
19834 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19835 && PT >= MATRIX_ROW_START_CHARPOS (row)
19836 && PT <= MATRIX_ROW_END_CHARPOS (row)
19837 && cursor_row_p (row))
19838 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19839
19840 /* Prepare for the next line. This line starts horizontally at (X
19841 HPOS) = (0 0). Vertical positions are incremented. As a
19842 convenience for the caller, IT->glyph_row is set to the next
19843 row to be used. */
19844 it->current_x = it->hpos = 0;
19845 it->current_y += row->height;
19846 SET_TEXT_POS (it->eol_pos, 0, 0);
19847 ++it->vpos;
19848 ++it->glyph_row;
19849 /* The next row should by default use the same value of the
19850 reversed_p flag as this one. set_iterator_to_next decides when
19851 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19852 the flag accordingly. */
19853 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19854 it->glyph_row->reversed_p = row->reversed_p;
19855 it->start = row->end;
19856 return row->displays_text_p;
19857
19858 #undef RECORD_MAX_MIN_POS
19859 }
19860
19861 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19862 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19863 doc: /* Return paragraph direction at point in BUFFER.
19864 Value is either `left-to-right' or `right-to-left'.
19865 If BUFFER is omitted or nil, it defaults to the current buffer.
19866
19867 Paragraph direction determines how the text in the paragraph is displayed.
19868 In left-to-right paragraphs, text begins at the left margin of the window
19869 and the reading direction is generally left to right. In right-to-left
19870 paragraphs, text begins at the right margin and is read from right to left.
19871
19872 See also `bidi-paragraph-direction'. */)
19873 (Lisp_Object buffer)
19874 {
19875 struct buffer *buf = current_buffer;
19876 struct buffer *old = buf;
19877
19878 if (! NILP (buffer))
19879 {
19880 CHECK_BUFFER (buffer);
19881 buf = XBUFFER (buffer);
19882 }
19883
19884 if (NILP (BVAR (buf, bidi_display_reordering))
19885 || NILP (BVAR (buf, enable_multibyte_characters))
19886 /* When we are loading loadup.el, the character property tables
19887 needed for bidi iteration are not yet available. */
19888 || !NILP (Vpurify_flag))
19889 return Qleft_to_right;
19890 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19891 return BVAR (buf, bidi_paragraph_direction);
19892 else
19893 {
19894 /* Determine the direction from buffer text. We could try to
19895 use current_matrix if it is up to date, but this seems fast
19896 enough as it is. */
19897 struct bidi_it itb;
19898 ptrdiff_t pos = BUF_PT (buf);
19899 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19900 int c;
19901 void *itb_data = bidi_shelve_cache ();
19902
19903 set_buffer_temp (buf);
19904 /* bidi_paragraph_init finds the base direction of the paragraph
19905 by searching forward from paragraph start. We need the base
19906 direction of the current or _previous_ paragraph, so we need
19907 to make sure we are within that paragraph. To that end, find
19908 the previous non-empty line. */
19909 if (pos >= ZV && pos > BEGV)
19910 {
19911 pos--;
19912 bytepos = CHAR_TO_BYTE (pos);
19913 }
19914 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19915 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19916 {
19917 while ((c = FETCH_BYTE (bytepos)) == '\n'
19918 || c == ' ' || c == '\t' || c == '\f')
19919 {
19920 if (bytepos <= BEGV_BYTE)
19921 break;
19922 bytepos--;
19923 pos--;
19924 }
19925 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19926 bytepos--;
19927 }
19928 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19929 itb.paragraph_dir = NEUTRAL_DIR;
19930 itb.string.s = NULL;
19931 itb.string.lstring = Qnil;
19932 itb.string.bufpos = 0;
19933 itb.string.unibyte = 0;
19934 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19935 bidi_unshelve_cache (itb_data, 0);
19936 set_buffer_temp (old);
19937 switch (itb.paragraph_dir)
19938 {
19939 case L2R:
19940 return Qleft_to_right;
19941 break;
19942 case R2L:
19943 return Qright_to_left;
19944 break;
19945 default:
19946 emacs_abort ();
19947 }
19948 }
19949 }
19950
19951
19952 \f
19953 /***********************************************************************
19954 Menu Bar
19955 ***********************************************************************/
19956
19957 /* Redisplay the menu bar in the frame for window W.
19958
19959 The menu bar of X frames that don't have X toolkit support is
19960 displayed in a special window W->frame->menu_bar_window.
19961
19962 The menu bar of terminal frames is treated specially as far as
19963 glyph matrices are concerned. Menu bar lines are not part of
19964 windows, so the update is done directly on the frame matrix rows
19965 for the menu bar. */
19966
19967 static void
19968 display_menu_bar (struct window *w)
19969 {
19970 struct frame *f = XFRAME (WINDOW_FRAME (w));
19971 struct it it;
19972 Lisp_Object items;
19973 int i;
19974
19975 /* Don't do all this for graphical frames. */
19976 #ifdef HAVE_NTGUI
19977 if (FRAME_W32_P (f))
19978 return;
19979 #endif
19980 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
19981 if (FRAME_X_P (f))
19982 return;
19983 #endif
19984
19985 #ifdef HAVE_NS
19986 if (FRAME_NS_P (f))
19987 return;
19988 #endif /* HAVE_NS */
19989
19990 #ifdef USE_X_TOOLKIT
19991 eassert (!FRAME_WINDOW_P (f));
19992 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
19993 it.first_visible_x = 0;
19994 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
19995 #else /* not USE_X_TOOLKIT */
19996 if (FRAME_WINDOW_P (f))
19997 {
19998 /* Menu bar lines are displayed in the desired matrix of the
19999 dummy window menu_bar_window. */
20000 struct window *menu_w;
20001 eassert (WINDOWP (f->menu_bar_window));
20002 menu_w = XWINDOW (f->menu_bar_window);
20003 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20004 MENU_FACE_ID);
20005 it.first_visible_x = 0;
20006 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20007 }
20008 else
20009 {
20010 /* This is a TTY frame, i.e. character hpos/vpos are used as
20011 pixel x/y. */
20012 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20013 MENU_FACE_ID);
20014 it.first_visible_x = 0;
20015 it.last_visible_x = FRAME_COLS (f);
20016 }
20017 #endif /* not USE_X_TOOLKIT */
20018
20019 /* FIXME: This should be controlled by a user option. See the
20020 comments in redisplay_tool_bar and display_mode_line about
20021 this. */
20022 it.paragraph_embedding = L2R;
20023
20024 /* Clear all rows of the menu bar. */
20025 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20026 {
20027 struct glyph_row *row = it.glyph_row + i;
20028 clear_glyph_row (row);
20029 row->enabled_p = 1;
20030 row->full_width_p = 1;
20031 }
20032
20033 /* Display all items of the menu bar. */
20034 items = FRAME_MENU_BAR_ITEMS (it.f);
20035 for (i = 0; i < ASIZE (items); i += 4)
20036 {
20037 Lisp_Object string;
20038
20039 /* Stop at nil string. */
20040 string = AREF (items, i + 1);
20041 if (NILP (string))
20042 break;
20043
20044 /* Remember where item was displayed. */
20045 ASET (items, i + 3, make_number (it.hpos));
20046
20047 /* Display the item, pad with one space. */
20048 if (it.current_x < it.last_visible_x)
20049 display_string (NULL, string, Qnil, 0, 0, &it,
20050 SCHARS (string) + 1, 0, 0, -1);
20051 }
20052
20053 /* Fill out the line with spaces. */
20054 if (it.current_x < it.last_visible_x)
20055 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20056
20057 /* Compute the total height of the lines. */
20058 compute_line_metrics (&it);
20059 }
20060
20061
20062 \f
20063 /***********************************************************************
20064 Mode Line
20065 ***********************************************************************/
20066
20067 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20068 FORCE is non-zero, redisplay mode lines unconditionally.
20069 Otherwise, redisplay only mode lines that are garbaged. Value is
20070 the number of windows whose mode lines were redisplayed. */
20071
20072 static int
20073 redisplay_mode_lines (Lisp_Object window, int force)
20074 {
20075 int nwindows = 0;
20076
20077 while (!NILP (window))
20078 {
20079 struct window *w = XWINDOW (window);
20080
20081 if (WINDOWP (w->hchild))
20082 nwindows += redisplay_mode_lines (w->hchild, force);
20083 else if (WINDOWP (w->vchild))
20084 nwindows += redisplay_mode_lines (w->vchild, force);
20085 else if (force
20086 || FRAME_GARBAGED_P (XFRAME (w->frame))
20087 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20088 {
20089 struct text_pos lpoint;
20090 struct buffer *old = current_buffer;
20091
20092 /* Set the window's buffer for the mode line display. */
20093 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20094 set_buffer_internal_1 (XBUFFER (w->buffer));
20095
20096 /* Point refers normally to the selected window. For any
20097 other window, set up appropriate value. */
20098 if (!EQ (window, selected_window))
20099 {
20100 struct text_pos pt;
20101
20102 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20103 if (CHARPOS (pt) < BEGV)
20104 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20105 else if (CHARPOS (pt) > (ZV - 1))
20106 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20107 else
20108 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20109 }
20110
20111 /* Display mode lines. */
20112 clear_glyph_matrix (w->desired_matrix);
20113 if (display_mode_lines (w))
20114 {
20115 ++nwindows;
20116 w->must_be_updated_p = 1;
20117 }
20118
20119 /* Restore old settings. */
20120 set_buffer_internal_1 (old);
20121 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20122 }
20123
20124 window = w->next;
20125 }
20126
20127 return nwindows;
20128 }
20129
20130
20131 /* Display the mode and/or header line of window W. Value is the
20132 sum number of mode lines and header lines displayed. */
20133
20134 static int
20135 display_mode_lines (struct window *w)
20136 {
20137 Lisp_Object old_selected_window = selected_window;
20138 Lisp_Object old_selected_frame = selected_frame;
20139 Lisp_Object new_frame = w->frame;
20140 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20141 int n = 0;
20142
20143 selected_frame = new_frame;
20144 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20145 or window's point, then we'd need select_window_1 here as well. */
20146 XSETWINDOW (selected_window, w);
20147 XFRAME (new_frame)->selected_window = selected_window;
20148
20149 /* These will be set while the mode line specs are processed. */
20150 line_number_displayed = 0;
20151 w->column_number_displayed = -1;
20152
20153 if (WINDOW_WANTS_MODELINE_P (w))
20154 {
20155 struct window *sel_w = XWINDOW (old_selected_window);
20156
20157 /* Select mode line face based on the real selected window. */
20158 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20159 BVAR (current_buffer, mode_line_format));
20160 ++n;
20161 }
20162
20163 if (WINDOW_WANTS_HEADER_LINE_P (w))
20164 {
20165 display_mode_line (w, HEADER_LINE_FACE_ID,
20166 BVAR (current_buffer, header_line_format));
20167 ++n;
20168 }
20169
20170 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20171 selected_frame = old_selected_frame;
20172 selected_window = old_selected_window;
20173 return n;
20174 }
20175
20176
20177 /* Display mode or header line of window W. FACE_ID specifies which
20178 line to display; it is either MODE_LINE_FACE_ID or
20179 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20180 display. Value is the pixel height of the mode/header line
20181 displayed. */
20182
20183 static int
20184 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20185 {
20186 struct it it;
20187 struct face *face;
20188 ptrdiff_t count = SPECPDL_INDEX ();
20189
20190 init_iterator (&it, w, -1, -1, NULL, face_id);
20191 /* Don't extend on a previously drawn mode-line.
20192 This may happen if called from pos_visible_p. */
20193 it.glyph_row->enabled_p = 0;
20194 prepare_desired_row (it.glyph_row);
20195
20196 it.glyph_row->mode_line_p = 1;
20197
20198 /* FIXME: This should be controlled by a user option. But
20199 supporting such an option is not trivial, since the mode line is
20200 made up of many separate strings. */
20201 it.paragraph_embedding = L2R;
20202
20203 record_unwind_protect (unwind_format_mode_line,
20204 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20205
20206 mode_line_target = MODE_LINE_DISPLAY;
20207
20208 /* Temporarily make frame's keyboard the current kboard so that
20209 kboard-local variables in the mode_line_format will get the right
20210 values. */
20211 push_kboard (FRAME_KBOARD (it.f));
20212 record_unwind_save_match_data ();
20213 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20214 pop_kboard ();
20215
20216 unbind_to (count, Qnil);
20217
20218 /* Fill up with spaces. */
20219 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20220
20221 compute_line_metrics (&it);
20222 it.glyph_row->full_width_p = 1;
20223 it.glyph_row->continued_p = 0;
20224 it.glyph_row->truncated_on_left_p = 0;
20225 it.glyph_row->truncated_on_right_p = 0;
20226
20227 /* Make a 3D mode-line have a shadow at its right end. */
20228 face = FACE_FROM_ID (it.f, face_id);
20229 extend_face_to_end_of_line (&it);
20230 if (face->box != FACE_NO_BOX)
20231 {
20232 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20233 + it.glyph_row->used[TEXT_AREA] - 1);
20234 last->right_box_line_p = 1;
20235 }
20236
20237 return it.glyph_row->height;
20238 }
20239
20240 /* Move element ELT in LIST to the front of LIST.
20241 Return the updated list. */
20242
20243 static Lisp_Object
20244 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20245 {
20246 register Lisp_Object tail, prev;
20247 register Lisp_Object tem;
20248
20249 tail = list;
20250 prev = Qnil;
20251 while (CONSP (tail))
20252 {
20253 tem = XCAR (tail);
20254
20255 if (EQ (elt, tem))
20256 {
20257 /* Splice out the link TAIL. */
20258 if (NILP (prev))
20259 list = XCDR (tail);
20260 else
20261 Fsetcdr (prev, XCDR (tail));
20262
20263 /* Now make it the first. */
20264 Fsetcdr (tail, list);
20265 return tail;
20266 }
20267 else
20268 prev = tail;
20269 tail = XCDR (tail);
20270 QUIT;
20271 }
20272
20273 /* Not found--return unchanged LIST. */
20274 return list;
20275 }
20276
20277 /* Contribute ELT to the mode line for window IT->w. How it
20278 translates into text depends on its data type.
20279
20280 IT describes the display environment in which we display, as usual.
20281
20282 DEPTH is the depth in recursion. It is used to prevent
20283 infinite recursion here.
20284
20285 FIELD_WIDTH is the number of characters the display of ELT should
20286 occupy in the mode line, and PRECISION is the maximum number of
20287 characters to display from ELT's representation. See
20288 display_string for details.
20289
20290 Returns the hpos of the end of the text generated by ELT.
20291
20292 PROPS is a property list to add to any string we encounter.
20293
20294 If RISKY is nonzero, remove (disregard) any properties in any string
20295 we encounter, and ignore :eval and :propertize.
20296
20297 The global variable `mode_line_target' determines whether the
20298 output is passed to `store_mode_line_noprop',
20299 `store_mode_line_string', or `display_string'. */
20300
20301 static int
20302 display_mode_element (struct it *it, int depth, int field_width, int precision,
20303 Lisp_Object elt, Lisp_Object props, int risky)
20304 {
20305 int n = 0, field, prec;
20306 int literal = 0;
20307
20308 tail_recurse:
20309 if (depth > 100)
20310 elt = build_string ("*too-deep*");
20311
20312 depth++;
20313
20314 switch (XTYPE (elt))
20315 {
20316 case Lisp_String:
20317 {
20318 /* A string: output it and check for %-constructs within it. */
20319 unsigned char c;
20320 ptrdiff_t offset = 0;
20321
20322 if (SCHARS (elt) > 0
20323 && (!NILP (props) || risky))
20324 {
20325 Lisp_Object oprops, aelt;
20326 oprops = Ftext_properties_at (make_number (0), elt);
20327
20328 /* If the starting string's properties are not what
20329 we want, translate the string. Also, if the string
20330 is risky, do that anyway. */
20331
20332 if (NILP (Fequal (props, oprops)) || risky)
20333 {
20334 /* If the starting string has properties,
20335 merge the specified ones onto the existing ones. */
20336 if (! NILP (oprops) && !risky)
20337 {
20338 Lisp_Object tem;
20339
20340 oprops = Fcopy_sequence (oprops);
20341 tem = props;
20342 while (CONSP (tem))
20343 {
20344 oprops = Fplist_put (oprops, XCAR (tem),
20345 XCAR (XCDR (tem)));
20346 tem = XCDR (XCDR (tem));
20347 }
20348 props = oprops;
20349 }
20350
20351 aelt = Fassoc (elt, mode_line_proptrans_alist);
20352 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20353 {
20354 /* AELT is what we want. Move it to the front
20355 without consing. */
20356 elt = XCAR (aelt);
20357 mode_line_proptrans_alist
20358 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20359 }
20360 else
20361 {
20362 Lisp_Object tem;
20363
20364 /* If AELT has the wrong props, it is useless.
20365 so get rid of it. */
20366 if (! NILP (aelt))
20367 mode_line_proptrans_alist
20368 = Fdelq (aelt, mode_line_proptrans_alist);
20369
20370 elt = Fcopy_sequence (elt);
20371 Fset_text_properties (make_number (0), Flength (elt),
20372 props, elt);
20373 /* Add this item to mode_line_proptrans_alist. */
20374 mode_line_proptrans_alist
20375 = Fcons (Fcons (elt, props),
20376 mode_line_proptrans_alist);
20377 /* Truncate mode_line_proptrans_alist
20378 to at most 50 elements. */
20379 tem = Fnthcdr (make_number (50),
20380 mode_line_proptrans_alist);
20381 if (! NILP (tem))
20382 XSETCDR (tem, Qnil);
20383 }
20384 }
20385 }
20386
20387 offset = 0;
20388
20389 if (literal)
20390 {
20391 prec = precision - n;
20392 switch (mode_line_target)
20393 {
20394 case MODE_LINE_NOPROP:
20395 case MODE_LINE_TITLE:
20396 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20397 break;
20398 case MODE_LINE_STRING:
20399 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20400 break;
20401 case MODE_LINE_DISPLAY:
20402 n += display_string (NULL, elt, Qnil, 0, 0, it,
20403 0, prec, 0, STRING_MULTIBYTE (elt));
20404 break;
20405 }
20406
20407 break;
20408 }
20409
20410 /* Handle the non-literal case. */
20411
20412 while ((precision <= 0 || n < precision)
20413 && SREF (elt, offset) != 0
20414 && (mode_line_target != MODE_LINE_DISPLAY
20415 || it->current_x < it->last_visible_x))
20416 {
20417 ptrdiff_t last_offset = offset;
20418
20419 /* Advance to end of string or next format specifier. */
20420 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20421 ;
20422
20423 if (offset - 1 != last_offset)
20424 {
20425 ptrdiff_t nchars, nbytes;
20426
20427 /* Output to end of string or up to '%'. Field width
20428 is length of string. Don't output more than
20429 PRECISION allows us. */
20430 offset--;
20431
20432 prec = c_string_width (SDATA (elt) + last_offset,
20433 offset - last_offset, precision - n,
20434 &nchars, &nbytes);
20435
20436 switch (mode_line_target)
20437 {
20438 case MODE_LINE_NOPROP:
20439 case MODE_LINE_TITLE:
20440 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20441 break;
20442 case MODE_LINE_STRING:
20443 {
20444 ptrdiff_t bytepos = last_offset;
20445 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20446 ptrdiff_t endpos = (precision <= 0
20447 ? string_byte_to_char (elt, offset)
20448 : charpos + nchars);
20449
20450 n += store_mode_line_string (NULL,
20451 Fsubstring (elt, make_number (charpos),
20452 make_number (endpos)),
20453 0, 0, 0, Qnil);
20454 }
20455 break;
20456 case MODE_LINE_DISPLAY:
20457 {
20458 ptrdiff_t bytepos = last_offset;
20459 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20460
20461 if (precision <= 0)
20462 nchars = string_byte_to_char (elt, offset) - charpos;
20463 n += display_string (NULL, elt, Qnil, 0, charpos,
20464 it, 0, nchars, 0,
20465 STRING_MULTIBYTE (elt));
20466 }
20467 break;
20468 }
20469 }
20470 else /* c == '%' */
20471 {
20472 ptrdiff_t percent_position = offset;
20473
20474 /* Get the specified minimum width. Zero means
20475 don't pad. */
20476 field = 0;
20477 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20478 field = field * 10 + c - '0';
20479
20480 /* Don't pad beyond the total padding allowed. */
20481 if (field_width - n > 0 && field > field_width - n)
20482 field = field_width - n;
20483
20484 /* Note that either PRECISION <= 0 or N < PRECISION. */
20485 prec = precision - n;
20486
20487 if (c == 'M')
20488 n += display_mode_element (it, depth, field, prec,
20489 Vglobal_mode_string, props,
20490 risky);
20491 else if (c != 0)
20492 {
20493 int multibyte;
20494 ptrdiff_t bytepos, charpos;
20495 const char *spec;
20496 Lisp_Object string;
20497
20498 bytepos = percent_position;
20499 charpos = (STRING_MULTIBYTE (elt)
20500 ? string_byte_to_char (elt, bytepos)
20501 : bytepos);
20502 spec = decode_mode_spec (it->w, c, field, &string);
20503 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20504
20505 switch (mode_line_target)
20506 {
20507 case MODE_LINE_NOPROP:
20508 case MODE_LINE_TITLE:
20509 n += store_mode_line_noprop (spec, field, prec);
20510 break;
20511 case MODE_LINE_STRING:
20512 {
20513 Lisp_Object tem = build_string (spec);
20514 props = Ftext_properties_at (make_number (charpos), elt);
20515 /* Should only keep face property in props */
20516 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20517 }
20518 break;
20519 case MODE_LINE_DISPLAY:
20520 {
20521 int nglyphs_before, nwritten;
20522
20523 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20524 nwritten = display_string (spec, string, elt,
20525 charpos, 0, it,
20526 field, prec, 0,
20527 multibyte);
20528
20529 /* Assign to the glyphs written above the
20530 string where the `%x' came from, position
20531 of the `%'. */
20532 if (nwritten > 0)
20533 {
20534 struct glyph *glyph
20535 = (it->glyph_row->glyphs[TEXT_AREA]
20536 + nglyphs_before);
20537 int i;
20538
20539 for (i = 0; i < nwritten; ++i)
20540 {
20541 glyph[i].object = elt;
20542 glyph[i].charpos = charpos;
20543 }
20544
20545 n += nwritten;
20546 }
20547 }
20548 break;
20549 }
20550 }
20551 else /* c == 0 */
20552 break;
20553 }
20554 }
20555 }
20556 break;
20557
20558 case Lisp_Symbol:
20559 /* A symbol: process the value of the symbol recursively
20560 as if it appeared here directly. Avoid error if symbol void.
20561 Special case: if value of symbol is a string, output the string
20562 literally. */
20563 {
20564 register Lisp_Object tem;
20565
20566 /* If the variable is not marked as risky to set
20567 then its contents are risky to use. */
20568 if (NILP (Fget (elt, Qrisky_local_variable)))
20569 risky = 1;
20570
20571 tem = Fboundp (elt);
20572 if (!NILP (tem))
20573 {
20574 tem = Fsymbol_value (elt);
20575 /* If value is a string, output that string literally:
20576 don't check for % within it. */
20577 if (STRINGP (tem))
20578 literal = 1;
20579
20580 if (!EQ (tem, elt))
20581 {
20582 /* Give up right away for nil or t. */
20583 elt = tem;
20584 goto tail_recurse;
20585 }
20586 }
20587 }
20588 break;
20589
20590 case Lisp_Cons:
20591 {
20592 register Lisp_Object car, tem;
20593
20594 /* A cons cell: five distinct cases.
20595 If first element is :eval or :propertize, do something special.
20596 If first element is a string or a cons, process all the elements
20597 and effectively concatenate them.
20598 If first element is a negative number, truncate displaying cdr to
20599 at most that many characters. If positive, pad (with spaces)
20600 to at least that many characters.
20601 If first element is a symbol, process the cadr or caddr recursively
20602 according to whether the symbol's value is non-nil or nil. */
20603 car = XCAR (elt);
20604 if (EQ (car, QCeval))
20605 {
20606 /* An element of the form (:eval FORM) means evaluate FORM
20607 and use the result as mode line elements. */
20608
20609 if (risky)
20610 break;
20611
20612 if (CONSP (XCDR (elt)))
20613 {
20614 Lisp_Object spec;
20615 spec = safe_eval (XCAR (XCDR (elt)));
20616 n += display_mode_element (it, depth, field_width - n,
20617 precision - n, spec, props,
20618 risky);
20619 }
20620 }
20621 else if (EQ (car, QCpropertize))
20622 {
20623 /* An element of the form (:propertize ELT PROPS...)
20624 means display ELT but applying properties PROPS. */
20625
20626 if (risky)
20627 break;
20628
20629 if (CONSP (XCDR (elt)))
20630 n += display_mode_element (it, depth, field_width - n,
20631 precision - n, XCAR (XCDR (elt)),
20632 XCDR (XCDR (elt)), risky);
20633 }
20634 else if (SYMBOLP (car))
20635 {
20636 tem = Fboundp (car);
20637 elt = XCDR (elt);
20638 if (!CONSP (elt))
20639 goto invalid;
20640 /* elt is now the cdr, and we know it is a cons cell.
20641 Use its car if CAR has a non-nil value. */
20642 if (!NILP (tem))
20643 {
20644 tem = Fsymbol_value (car);
20645 if (!NILP (tem))
20646 {
20647 elt = XCAR (elt);
20648 goto tail_recurse;
20649 }
20650 }
20651 /* Symbol's value is nil (or symbol is unbound)
20652 Get the cddr of the original list
20653 and if possible find the caddr and use that. */
20654 elt = XCDR (elt);
20655 if (NILP (elt))
20656 break;
20657 else if (!CONSP (elt))
20658 goto invalid;
20659 elt = XCAR (elt);
20660 goto tail_recurse;
20661 }
20662 else if (INTEGERP (car))
20663 {
20664 register int lim = XINT (car);
20665 elt = XCDR (elt);
20666 if (lim < 0)
20667 {
20668 /* Negative int means reduce maximum width. */
20669 if (precision <= 0)
20670 precision = -lim;
20671 else
20672 precision = min (precision, -lim);
20673 }
20674 else if (lim > 0)
20675 {
20676 /* Padding specified. Don't let it be more than
20677 current maximum. */
20678 if (precision > 0)
20679 lim = min (precision, lim);
20680
20681 /* If that's more padding than already wanted, queue it.
20682 But don't reduce padding already specified even if
20683 that is beyond the current truncation point. */
20684 field_width = max (lim, field_width);
20685 }
20686 goto tail_recurse;
20687 }
20688 else if (STRINGP (car) || CONSP (car))
20689 {
20690 Lisp_Object halftail = elt;
20691 int len = 0;
20692
20693 while (CONSP (elt)
20694 && (precision <= 0 || n < precision))
20695 {
20696 n += display_mode_element (it, depth,
20697 /* Do padding only after the last
20698 element in the list. */
20699 (! CONSP (XCDR (elt))
20700 ? field_width - n
20701 : 0),
20702 precision - n, XCAR (elt),
20703 props, risky);
20704 elt = XCDR (elt);
20705 len++;
20706 if ((len & 1) == 0)
20707 halftail = XCDR (halftail);
20708 /* Check for cycle. */
20709 if (EQ (halftail, elt))
20710 break;
20711 }
20712 }
20713 }
20714 break;
20715
20716 default:
20717 invalid:
20718 elt = build_string ("*invalid*");
20719 goto tail_recurse;
20720 }
20721
20722 /* Pad to FIELD_WIDTH. */
20723 if (field_width > 0 && n < field_width)
20724 {
20725 switch (mode_line_target)
20726 {
20727 case MODE_LINE_NOPROP:
20728 case MODE_LINE_TITLE:
20729 n += store_mode_line_noprop ("", field_width - n, 0);
20730 break;
20731 case MODE_LINE_STRING:
20732 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20733 break;
20734 case MODE_LINE_DISPLAY:
20735 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20736 0, 0, 0);
20737 break;
20738 }
20739 }
20740
20741 return n;
20742 }
20743
20744 /* Store a mode-line string element in mode_line_string_list.
20745
20746 If STRING is non-null, display that C string. Otherwise, the Lisp
20747 string LISP_STRING is displayed.
20748
20749 FIELD_WIDTH is the minimum number of output glyphs to produce.
20750 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20751 with spaces. FIELD_WIDTH <= 0 means don't pad.
20752
20753 PRECISION is the maximum number of characters to output from
20754 STRING. PRECISION <= 0 means don't truncate the string.
20755
20756 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20757 properties to the string.
20758
20759 PROPS are the properties to add to the string.
20760 The mode_line_string_face face property is always added to the string.
20761 */
20762
20763 static int
20764 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20765 int field_width, int precision, Lisp_Object props)
20766 {
20767 ptrdiff_t len;
20768 int n = 0;
20769
20770 if (string != NULL)
20771 {
20772 len = strlen (string);
20773 if (precision > 0 && len > precision)
20774 len = precision;
20775 lisp_string = make_string (string, len);
20776 if (NILP (props))
20777 props = mode_line_string_face_prop;
20778 else if (!NILP (mode_line_string_face))
20779 {
20780 Lisp_Object face = Fplist_get (props, Qface);
20781 props = Fcopy_sequence (props);
20782 if (NILP (face))
20783 face = mode_line_string_face;
20784 else
20785 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20786 props = Fplist_put (props, Qface, face);
20787 }
20788 Fadd_text_properties (make_number (0), make_number (len),
20789 props, lisp_string);
20790 }
20791 else
20792 {
20793 len = XFASTINT (Flength (lisp_string));
20794 if (precision > 0 && len > precision)
20795 {
20796 len = precision;
20797 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20798 precision = -1;
20799 }
20800 if (!NILP (mode_line_string_face))
20801 {
20802 Lisp_Object face;
20803 if (NILP (props))
20804 props = Ftext_properties_at (make_number (0), lisp_string);
20805 face = Fplist_get (props, Qface);
20806 if (NILP (face))
20807 face = mode_line_string_face;
20808 else
20809 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20810 props = Fcons (Qface, Fcons (face, Qnil));
20811 if (copy_string)
20812 lisp_string = Fcopy_sequence (lisp_string);
20813 }
20814 if (!NILP (props))
20815 Fadd_text_properties (make_number (0), make_number (len),
20816 props, lisp_string);
20817 }
20818
20819 if (len > 0)
20820 {
20821 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20822 n += len;
20823 }
20824
20825 if (field_width > len)
20826 {
20827 field_width -= len;
20828 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20829 if (!NILP (props))
20830 Fadd_text_properties (make_number (0), make_number (field_width),
20831 props, lisp_string);
20832 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20833 n += field_width;
20834 }
20835
20836 return n;
20837 }
20838
20839
20840 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20841 1, 4, 0,
20842 doc: /* Format a string out of a mode line format specification.
20843 First arg FORMAT specifies the mode line format (see `mode-line-format'
20844 for details) to use.
20845
20846 By default, the format is evaluated for the currently selected window.
20847
20848 Optional second arg FACE specifies the face property to put on all
20849 characters for which no face is specified. The value nil means the
20850 default face. The value t means whatever face the window's mode line
20851 currently uses (either `mode-line' or `mode-line-inactive',
20852 depending on whether the window is the selected window or not).
20853 An integer value means the value string has no text
20854 properties.
20855
20856 Optional third and fourth args WINDOW and BUFFER specify the window
20857 and buffer to use as the context for the formatting (defaults
20858 are the selected window and the WINDOW's buffer). */)
20859 (Lisp_Object format, Lisp_Object face,
20860 Lisp_Object window, Lisp_Object buffer)
20861 {
20862 struct it it;
20863 int len;
20864 struct window *w;
20865 struct buffer *old_buffer = NULL;
20866 int face_id;
20867 int no_props = INTEGERP (face);
20868 ptrdiff_t count = SPECPDL_INDEX ();
20869 Lisp_Object str;
20870 int string_start = 0;
20871
20872 w = decode_any_window (window);
20873 XSETWINDOW (window, w);
20874
20875 if (NILP (buffer))
20876 buffer = w->buffer;
20877 CHECK_BUFFER (buffer);
20878
20879 /* Make formatting the modeline a non-op when noninteractive, otherwise
20880 there will be problems later caused by a partially initialized frame. */
20881 if (NILP (format) || noninteractive)
20882 return empty_unibyte_string;
20883
20884 if (no_props)
20885 face = Qnil;
20886
20887 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20888 : EQ (face, Qt) ? (EQ (window, selected_window)
20889 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20890 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20891 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20892 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20893 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20894 : DEFAULT_FACE_ID;
20895
20896 old_buffer = current_buffer;
20897
20898 /* Save things including mode_line_proptrans_alist,
20899 and set that to nil so that we don't alter the outer value. */
20900 record_unwind_protect (unwind_format_mode_line,
20901 format_mode_line_unwind_data
20902 (XFRAME (WINDOW_FRAME (w)),
20903 old_buffer, selected_window, 1));
20904 mode_line_proptrans_alist = Qnil;
20905
20906 Fselect_window (window, Qt);
20907 set_buffer_internal_1 (XBUFFER (buffer));
20908
20909 init_iterator (&it, w, -1, -1, NULL, face_id);
20910
20911 if (no_props)
20912 {
20913 mode_line_target = MODE_LINE_NOPROP;
20914 mode_line_string_face_prop = Qnil;
20915 mode_line_string_list = Qnil;
20916 string_start = MODE_LINE_NOPROP_LEN (0);
20917 }
20918 else
20919 {
20920 mode_line_target = MODE_LINE_STRING;
20921 mode_line_string_list = Qnil;
20922 mode_line_string_face = face;
20923 mode_line_string_face_prop
20924 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20925 }
20926
20927 push_kboard (FRAME_KBOARD (it.f));
20928 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20929 pop_kboard ();
20930
20931 if (no_props)
20932 {
20933 len = MODE_LINE_NOPROP_LEN (string_start);
20934 str = make_string (mode_line_noprop_buf + string_start, len);
20935 }
20936 else
20937 {
20938 mode_line_string_list = Fnreverse (mode_line_string_list);
20939 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20940 empty_unibyte_string);
20941 }
20942
20943 unbind_to (count, Qnil);
20944 return str;
20945 }
20946
20947 /* Write a null-terminated, right justified decimal representation of
20948 the positive integer D to BUF using a minimal field width WIDTH. */
20949
20950 static void
20951 pint2str (register char *buf, register int width, register ptrdiff_t d)
20952 {
20953 register char *p = buf;
20954
20955 if (d <= 0)
20956 *p++ = '0';
20957 else
20958 {
20959 while (d > 0)
20960 {
20961 *p++ = d % 10 + '0';
20962 d /= 10;
20963 }
20964 }
20965
20966 for (width -= (int) (p - buf); width > 0; --width)
20967 *p++ = ' ';
20968 *p-- = '\0';
20969 while (p > buf)
20970 {
20971 d = *buf;
20972 *buf++ = *p;
20973 *p-- = d;
20974 }
20975 }
20976
20977 /* Write a null-terminated, right justified decimal and "human
20978 readable" representation of the nonnegative integer D to BUF using
20979 a minimal field width WIDTH. D should be smaller than 999.5e24. */
20980
20981 static const char power_letter[] =
20982 {
20983 0, /* no letter */
20984 'k', /* kilo */
20985 'M', /* mega */
20986 'G', /* giga */
20987 'T', /* tera */
20988 'P', /* peta */
20989 'E', /* exa */
20990 'Z', /* zetta */
20991 'Y' /* yotta */
20992 };
20993
20994 static void
20995 pint2hrstr (char *buf, int width, ptrdiff_t d)
20996 {
20997 /* We aim to represent the nonnegative integer D as
20998 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
20999 ptrdiff_t quotient = d;
21000 int remainder = 0;
21001 /* -1 means: do not use TENTHS. */
21002 int tenths = -1;
21003 int exponent = 0;
21004
21005 /* Length of QUOTIENT.TENTHS as a string. */
21006 int length;
21007
21008 char * psuffix;
21009 char * p;
21010
21011 if (1000 <= quotient)
21012 {
21013 /* Scale to the appropriate EXPONENT. */
21014 do
21015 {
21016 remainder = quotient % 1000;
21017 quotient /= 1000;
21018 exponent++;
21019 }
21020 while (1000 <= quotient);
21021
21022 /* Round to nearest and decide whether to use TENTHS or not. */
21023 if (quotient <= 9)
21024 {
21025 tenths = remainder / 100;
21026 if (50 <= remainder % 100)
21027 {
21028 if (tenths < 9)
21029 tenths++;
21030 else
21031 {
21032 quotient++;
21033 if (quotient == 10)
21034 tenths = -1;
21035 else
21036 tenths = 0;
21037 }
21038 }
21039 }
21040 else
21041 if (500 <= remainder)
21042 {
21043 if (quotient < 999)
21044 quotient++;
21045 else
21046 {
21047 quotient = 1;
21048 exponent++;
21049 tenths = 0;
21050 }
21051 }
21052 }
21053
21054 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21055 if (tenths == -1 && quotient <= 99)
21056 if (quotient <= 9)
21057 length = 1;
21058 else
21059 length = 2;
21060 else
21061 length = 3;
21062 p = psuffix = buf + max (width, length);
21063
21064 /* Print EXPONENT. */
21065 *psuffix++ = power_letter[exponent];
21066 *psuffix = '\0';
21067
21068 /* Print TENTHS. */
21069 if (tenths >= 0)
21070 {
21071 *--p = '0' + tenths;
21072 *--p = '.';
21073 }
21074
21075 /* Print QUOTIENT. */
21076 do
21077 {
21078 int digit = quotient % 10;
21079 *--p = '0' + digit;
21080 }
21081 while ((quotient /= 10) != 0);
21082
21083 /* Print leading spaces. */
21084 while (buf < p)
21085 *--p = ' ';
21086 }
21087
21088 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21089 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21090 type of CODING_SYSTEM. Return updated pointer into BUF. */
21091
21092 static unsigned char invalid_eol_type[] = "(*invalid*)";
21093
21094 static char *
21095 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21096 {
21097 Lisp_Object val;
21098 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21099 const unsigned char *eol_str;
21100 int eol_str_len;
21101 /* The EOL conversion we are using. */
21102 Lisp_Object eoltype;
21103
21104 val = CODING_SYSTEM_SPEC (coding_system);
21105 eoltype = Qnil;
21106
21107 if (!VECTORP (val)) /* Not yet decided. */
21108 {
21109 *buf++ = multibyte ? '-' : ' ';
21110 if (eol_flag)
21111 eoltype = eol_mnemonic_undecided;
21112 /* Don't mention EOL conversion if it isn't decided. */
21113 }
21114 else
21115 {
21116 Lisp_Object attrs;
21117 Lisp_Object eolvalue;
21118
21119 attrs = AREF (val, 0);
21120 eolvalue = AREF (val, 2);
21121
21122 *buf++ = multibyte
21123 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21124 : ' ';
21125
21126 if (eol_flag)
21127 {
21128 /* The EOL conversion that is normal on this system. */
21129
21130 if (NILP (eolvalue)) /* Not yet decided. */
21131 eoltype = eol_mnemonic_undecided;
21132 else if (VECTORP (eolvalue)) /* Not yet decided. */
21133 eoltype = eol_mnemonic_undecided;
21134 else /* eolvalue is Qunix, Qdos, or Qmac. */
21135 eoltype = (EQ (eolvalue, Qunix)
21136 ? eol_mnemonic_unix
21137 : (EQ (eolvalue, Qdos) == 1
21138 ? eol_mnemonic_dos : eol_mnemonic_mac));
21139 }
21140 }
21141
21142 if (eol_flag)
21143 {
21144 /* Mention the EOL conversion if it is not the usual one. */
21145 if (STRINGP (eoltype))
21146 {
21147 eol_str = SDATA (eoltype);
21148 eol_str_len = SBYTES (eoltype);
21149 }
21150 else if (CHARACTERP (eoltype))
21151 {
21152 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21153 int c = XFASTINT (eoltype);
21154 eol_str_len = CHAR_STRING (c, tmp);
21155 eol_str = tmp;
21156 }
21157 else
21158 {
21159 eol_str = invalid_eol_type;
21160 eol_str_len = sizeof (invalid_eol_type) - 1;
21161 }
21162 memcpy (buf, eol_str, eol_str_len);
21163 buf += eol_str_len;
21164 }
21165
21166 return buf;
21167 }
21168
21169 /* Return a string for the output of a mode line %-spec for window W,
21170 generated by character C. FIELD_WIDTH > 0 means pad the string
21171 returned with spaces to that value. Return a Lisp string in
21172 *STRING if the resulting string is taken from that Lisp string.
21173
21174 Note we operate on the current buffer for most purposes. */
21175
21176 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21177
21178 static const char *
21179 decode_mode_spec (struct window *w, register int c, int field_width,
21180 Lisp_Object *string)
21181 {
21182 Lisp_Object obj;
21183 struct frame *f = XFRAME (WINDOW_FRAME (w));
21184 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21185 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21186 produce strings from numerical values, so limit preposterously
21187 large values of FIELD_WIDTH to avoid overrunning the buffer's
21188 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21189 bytes plus the terminating null. */
21190 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21191 struct buffer *b = current_buffer;
21192
21193 obj = Qnil;
21194 *string = Qnil;
21195
21196 switch (c)
21197 {
21198 case '*':
21199 if (!NILP (BVAR (b, read_only)))
21200 return "%";
21201 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21202 return "*";
21203 return "-";
21204
21205 case '+':
21206 /* This differs from %* only for a modified read-only buffer. */
21207 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21208 return "*";
21209 if (!NILP (BVAR (b, read_only)))
21210 return "%";
21211 return "-";
21212
21213 case '&':
21214 /* This differs from %* in ignoring read-only-ness. */
21215 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21216 return "*";
21217 return "-";
21218
21219 case '%':
21220 return "%";
21221
21222 case '[':
21223 {
21224 int i;
21225 char *p;
21226
21227 if (command_loop_level > 5)
21228 return "[[[... ";
21229 p = decode_mode_spec_buf;
21230 for (i = 0; i < command_loop_level; i++)
21231 *p++ = '[';
21232 *p = 0;
21233 return decode_mode_spec_buf;
21234 }
21235
21236 case ']':
21237 {
21238 int i;
21239 char *p;
21240
21241 if (command_loop_level > 5)
21242 return " ...]]]";
21243 p = decode_mode_spec_buf;
21244 for (i = 0; i < command_loop_level; i++)
21245 *p++ = ']';
21246 *p = 0;
21247 return decode_mode_spec_buf;
21248 }
21249
21250 case '-':
21251 {
21252 register int i;
21253
21254 /* Let lots_of_dashes be a string of infinite length. */
21255 if (mode_line_target == MODE_LINE_NOPROP
21256 || mode_line_target == MODE_LINE_STRING)
21257 return "--";
21258 if (field_width <= 0
21259 || field_width > sizeof (lots_of_dashes))
21260 {
21261 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21262 decode_mode_spec_buf[i] = '-';
21263 decode_mode_spec_buf[i] = '\0';
21264 return decode_mode_spec_buf;
21265 }
21266 else
21267 return lots_of_dashes;
21268 }
21269
21270 case 'b':
21271 obj = BVAR (b, name);
21272 break;
21273
21274 case 'c':
21275 /* %c and %l are ignored in `frame-title-format'.
21276 (In redisplay_internal, the frame title is drawn _before_ the
21277 windows are updated, so the stuff which depends on actual
21278 window contents (such as %l) may fail to render properly, or
21279 even crash emacs.) */
21280 if (mode_line_target == MODE_LINE_TITLE)
21281 return "";
21282 else
21283 {
21284 ptrdiff_t col = current_column ();
21285 w->column_number_displayed = col;
21286 pint2str (decode_mode_spec_buf, width, col);
21287 return decode_mode_spec_buf;
21288 }
21289
21290 case 'e':
21291 #ifndef SYSTEM_MALLOC
21292 {
21293 if (NILP (Vmemory_full))
21294 return "";
21295 else
21296 return "!MEM FULL! ";
21297 }
21298 #else
21299 return "";
21300 #endif
21301
21302 case 'F':
21303 /* %F displays the frame name. */
21304 if (!NILP (f->title))
21305 return SSDATA (f->title);
21306 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21307 return SSDATA (f->name);
21308 return "Emacs";
21309
21310 case 'f':
21311 obj = BVAR (b, filename);
21312 break;
21313
21314 case 'i':
21315 {
21316 ptrdiff_t size = ZV - BEGV;
21317 pint2str (decode_mode_spec_buf, width, size);
21318 return decode_mode_spec_buf;
21319 }
21320
21321 case 'I':
21322 {
21323 ptrdiff_t size = ZV - BEGV;
21324 pint2hrstr (decode_mode_spec_buf, width, size);
21325 return decode_mode_spec_buf;
21326 }
21327
21328 case 'l':
21329 {
21330 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21331 ptrdiff_t topline, nlines, height;
21332 ptrdiff_t junk;
21333
21334 /* %c and %l are ignored in `frame-title-format'. */
21335 if (mode_line_target == MODE_LINE_TITLE)
21336 return "";
21337
21338 startpos = marker_position (w->start);
21339 startpos_byte = marker_byte_position (w->start);
21340 height = WINDOW_TOTAL_LINES (w);
21341
21342 /* If we decided that this buffer isn't suitable for line numbers,
21343 don't forget that too fast. */
21344 if (w->base_line_pos == -1)
21345 goto no_value;
21346
21347 /* If the buffer is very big, don't waste time. */
21348 if (INTEGERP (Vline_number_display_limit)
21349 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21350 {
21351 w->base_line_pos = 0;
21352 w->base_line_number = 0;
21353 goto no_value;
21354 }
21355
21356 if (w->base_line_number > 0
21357 && w->base_line_pos > 0
21358 && w->base_line_pos <= startpos)
21359 {
21360 line = w->base_line_number;
21361 linepos = w->base_line_pos;
21362 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21363 }
21364 else
21365 {
21366 line = 1;
21367 linepos = BUF_BEGV (b);
21368 linepos_byte = BUF_BEGV_BYTE (b);
21369 }
21370
21371 /* Count lines from base line to window start position. */
21372 nlines = display_count_lines (linepos_byte,
21373 startpos_byte,
21374 startpos, &junk);
21375
21376 topline = nlines + line;
21377
21378 /* Determine a new base line, if the old one is too close
21379 or too far away, or if we did not have one.
21380 "Too close" means it's plausible a scroll-down would
21381 go back past it. */
21382 if (startpos == BUF_BEGV (b))
21383 {
21384 w->base_line_number = topline;
21385 w->base_line_pos = BUF_BEGV (b);
21386 }
21387 else if (nlines < height + 25 || nlines > height * 3 + 50
21388 || linepos == BUF_BEGV (b))
21389 {
21390 ptrdiff_t limit = BUF_BEGV (b);
21391 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21392 ptrdiff_t position;
21393 ptrdiff_t distance =
21394 (height * 2 + 30) * line_number_display_limit_width;
21395
21396 if (startpos - distance > limit)
21397 {
21398 limit = startpos - distance;
21399 limit_byte = CHAR_TO_BYTE (limit);
21400 }
21401
21402 nlines = display_count_lines (startpos_byte,
21403 limit_byte,
21404 - (height * 2 + 30),
21405 &position);
21406 /* If we couldn't find the lines we wanted within
21407 line_number_display_limit_width chars per line,
21408 give up on line numbers for this window. */
21409 if (position == limit_byte && limit == startpos - distance)
21410 {
21411 w->base_line_pos = -1;
21412 w->base_line_number = 0;
21413 goto no_value;
21414 }
21415
21416 w->base_line_number = topline - nlines;
21417 w->base_line_pos = BYTE_TO_CHAR (position);
21418 }
21419
21420 /* Now count lines from the start pos to point. */
21421 nlines = display_count_lines (startpos_byte,
21422 PT_BYTE, PT, &junk);
21423
21424 /* Record that we did display the line number. */
21425 line_number_displayed = 1;
21426
21427 /* Make the string to show. */
21428 pint2str (decode_mode_spec_buf, width, topline + nlines);
21429 return decode_mode_spec_buf;
21430 no_value:
21431 {
21432 char* p = decode_mode_spec_buf;
21433 int pad = width - 2;
21434 while (pad-- > 0)
21435 *p++ = ' ';
21436 *p++ = '?';
21437 *p++ = '?';
21438 *p = '\0';
21439 return decode_mode_spec_buf;
21440 }
21441 }
21442 break;
21443
21444 case 'm':
21445 obj = BVAR (b, mode_name);
21446 break;
21447
21448 case 'n':
21449 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21450 return " Narrow";
21451 break;
21452
21453 case 'p':
21454 {
21455 ptrdiff_t pos = marker_position (w->start);
21456 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21457
21458 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21459 {
21460 if (pos <= BUF_BEGV (b))
21461 return "All";
21462 else
21463 return "Bottom";
21464 }
21465 else if (pos <= BUF_BEGV (b))
21466 return "Top";
21467 else
21468 {
21469 if (total > 1000000)
21470 /* Do it differently for a large value, to avoid overflow. */
21471 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21472 else
21473 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21474 /* We can't normally display a 3-digit number,
21475 so get us a 2-digit number that is close. */
21476 if (total == 100)
21477 total = 99;
21478 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21479 return decode_mode_spec_buf;
21480 }
21481 }
21482
21483 /* Display percentage of size above the bottom of the screen. */
21484 case 'P':
21485 {
21486 ptrdiff_t toppos = marker_position (w->start);
21487 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21488 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21489
21490 if (botpos >= BUF_ZV (b))
21491 {
21492 if (toppos <= BUF_BEGV (b))
21493 return "All";
21494 else
21495 return "Bottom";
21496 }
21497 else
21498 {
21499 if (total > 1000000)
21500 /* Do it differently for a large value, to avoid overflow. */
21501 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21502 else
21503 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21504 /* We can't normally display a 3-digit number,
21505 so get us a 2-digit number that is close. */
21506 if (total == 100)
21507 total = 99;
21508 if (toppos <= BUF_BEGV (b))
21509 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21510 else
21511 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21512 return decode_mode_spec_buf;
21513 }
21514 }
21515
21516 case 's':
21517 /* status of process */
21518 obj = Fget_buffer_process (Fcurrent_buffer ());
21519 if (NILP (obj))
21520 return "no process";
21521 #ifndef MSDOS
21522 obj = Fsymbol_name (Fprocess_status (obj));
21523 #endif
21524 break;
21525
21526 case '@':
21527 {
21528 ptrdiff_t count = inhibit_garbage_collection ();
21529 Lisp_Object val = call1 (intern ("file-remote-p"),
21530 BVAR (current_buffer, directory));
21531 unbind_to (count, Qnil);
21532
21533 if (NILP (val))
21534 return "-";
21535 else
21536 return "@";
21537 }
21538
21539 case 'z':
21540 /* coding-system (not including end-of-line format) */
21541 case 'Z':
21542 /* coding-system (including end-of-line type) */
21543 {
21544 int eol_flag = (c == 'Z');
21545 char *p = decode_mode_spec_buf;
21546
21547 if (! FRAME_WINDOW_P (f))
21548 {
21549 /* No need to mention EOL here--the terminal never needs
21550 to do EOL conversion. */
21551 p = decode_mode_spec_coding (CODING_ID_NAME
21552 (FRAME_KEYBOARD_CODING (f)->id),
21553 p, 0);
21554 p = decode_mode_spec_coding (CODING_ID_NAME
21555 (FRAME_TERMINAL_CODING (f)->id),
21556 p, 0);
21557 }
21558 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21559 p, eol_flag);
21560
21561 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21562 #ifdef subprocesses
21563 obj = Fget_buffer_process (Fcurrent_buffer ());
21564 if (PROCESSP (obj))
21565 {
21566 p = decode_mode_spec_coding
21567 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21568 p = decode_mode_spec_coding
21569 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21570 }
21571 #endif /* subprocesses */
21572 #endif /* 0 */
21573 *p = 0;
21574 return decode_mode_spec_buf;
21575 }
21576 }
21577
21578 if (STRINGP (obj))
21579 {
21580 *string = obj;
21581 return SSDATA (obj);
21582 }
21583 else
21584 return "";
21585 }
21586
21587
21588 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
21589 means count lines back from START_BYTE. But don't go beyond
21590 LIMIT_BYTE. Return the number of lines thus found (always
21591 nonnegative).
21592
21593 Set *BYTE_POS_PTR to the byte position where we stopped. This is
21594 either the position COUNT lines after/before START_BYTE, if we
21595 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
21596 COUNT lines. */
21597
21598 static ptrdiff_t
21599 display_count_lines (ptrdiff_t start_byte,
21600 ptrdiff_t limit_byte, ptrdiff_t count,
21601 ptrdiff_t *byte_pos_ptr)
21602 {
21603 register unsigned char *cursor;
21604 unsigned char *base;
21605
21606 register ptrdiff_t ceiling;
21607 register unsigned char *ceiling_addr;
21608 ptrdiff_t orig_count = count;
21609
21610 /* If we are not in selective display mode,
21611 check only for newlines. */
21612 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21613 && !INTEGERP (BVAR (current_buffer, selective_display)));
21614
21615 if (count > 0)
21616 {
21617 while (start_byte < limit_byte)
21618 {
21619 ceiling = BUFFER_CEILING_OF (start_byte);
21620 ceiling = min (limit_byte - 1, ceiling);
21621 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21622 base = (cursor = BYTE_POS_ADDR (start_byte));
21623 while (1)
21624 {
21625 if (selective_display)
21626 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21627 ;
21628 else
21629 while (*cursor != '\n' && ++cursor != ceiling_addr)
21630 ;
21631
21632 if (cursor != ceiling_addr)
21633 {
21634 if (--count == 0)
21635 {
21636 start_byte += cursor - base + 1;
21637 *byte_pos_ptr = start_byte;
21638 return orig_count;
21639 }
21640 else
21641 if (++cursor == ceiling_addr)
21642 break;
21643 }
21644 else
21645 break;
21646 }
21647 start_byte += cursor - base;
21648 }
21649 }
21650 else
21651 {
21652 while (start_byte > limit_byte)
21653 {
21654 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21655 ceiling = max (limit_byte, ceiling);
21656 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21657 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21658 while (1)
21659 {
21660 if (selective_display)
21661 while (--cursor != ceiling_addr
21662 && *cursor != '\n' && *cursor != 015)
21663 ;
21664 else
21665 while (--cursor != ceiling_addr && *cursor != '\n')
21666 ;
21667
21668 if (cursor != ceiling_addr)
21669 {
21670 if (++count == 0)
21671 {
21672 start_byte += cursor - base + 1;
21673 *byte_pos_ptr = start_byte;
21674 /* When scanning backwards, we should
21675 not count the newline posterior to which we stop. */
21676 return - orig_count - 1;
21677 }
21678 }
21679 else
21680 break;
21681 }
21682 /* Here we add 1 to compensate for the last decrement
21683 of CURSOR, which took it past the valid range. */
21684 start_byte += cursor - base + 1;
21685 }
21686 }
21687
21688 *byte_pos_ptr = limit_byte;
21689
21690 if (count < 0)
21691 return - orig_count + count;
21692 return orig_count - count;
21693
21694 }
21695
21696
21697 \f
21698 /***********************************************************************
21699 Displaying strings
21700 ***********************************************************************/
21701
21702 /* Display a NUL-terminated string, starting with index START.
21703
21704 If STRING is non-null, display that C string. Otherwise, the Lisp
21705 string LISP_STRING is displayed. There's a case that STRING is
21706 non-null and LISP_STRING is not nil. It means STRING is a string
21707 data of LISP_STRING. In that case, we display LISP_STRING while
21708 ignoring its text properties.
21709
21710 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21711 FACE_STRING. Display STRING or LISP_STRING with the face at
21712 FACE_STRING_POS in FACE_STRING:
21713
21714 Display the string in the environment given by IT, but use the
21715 standard display table, temporarily.
21716
21717 FIELD_WIDTH is the minimum number of output glyphs to produce.
21718 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21719 with spaces. If STRING has more characters, more than FIELD_WIDTH
21720 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21721
21722 PRECISION is the maximum number of characters to output from
21723 STRING. PRECISION < 0 means don't truncate the string.
21724
21725 This is roughly equivalent to printf format specifiers:
21726
21727 FIELD_WIDTH PRECISION PRINTF
21728 ----------------------------------------
21729 -1 -1 %s
21730 -1 10 %.10s
21731 10 -1 %10s
21732 20 10 %20.10s
21733
21734 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21735 display them, and < 0 means obey the current buffer's value of
21736 enable_multibyte_characters.
21737
21738 Value is the number of columns displayed. */
21739
21740 static int
21741 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21742 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21743 int field_width, int precision, int max_x, int multibyte)
21744 {
21745 int hpos_at_start = it->hpos;
21746 int saved_face_id = it->face_id;
21747 struct glyph_row *row = it->glyph_row;
21748 ptrdiff_t it_charpos;
21749
21750 /* Initialize the iterator IT for iteration over STRING beginning
21751 with index START. */
21752 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21753 precision, field_width, multibyte);
21754 if (string && STRINGP (lisp_string))
21755 /* LISP_STRING is the one returned by decode_mode_spec. We should
21756 ignore its text properties. */
21757 it->stop_charpos = it->end_charpos;
21758
21759 /* If displaying STRING, set up the face of the iterator from
21760 FACE_STRING, if that's given. */
21761 if (STRINGP (face_string))
21762 {
21763 ptrdiff_t endptr;
21764 struct face *face;
21765
21766 it->face_id
21767 = face_at_string_position (it->w, face_string, face_string_pos,
21768 0, it->region_beg_charpos,
21769 it->region_end_charpos,
21770 &endptr, it->base_face_id, 0);
21771 face = FACE_FROM_ID (it->f, it->face_id);
21772 it->face_box_p = face->box != FACE_NO_BOX;
21773 }
21774
21775 /* Set max_x to the maximum allowed X position. Don't let it go
21776 beyond the right edge of the window. */
21777 if (max_x <= 0)
21778 max_x = it->last_visible_x;
21779 else
21780 max_x = min (max_x, it->last_visible_x);
21781
21782 /* Skip over display elements that are not visible. because IT->w is
21783 hscrolled. */
21784 if (it->current_x < it->first_visible_x)
21785 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21786 MOVE_TO_POS | MOVE_TO_X);
21787
21788 row->ascent = it->max_ascent;
21789 row->height = it->max_ascent + it->max_descent;
21790 row->phys_ascent = it->max_phys_ascent;
21791 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21792 row->extra_line_spacing = it->max_extra_line_spacing;
21793
21794 if (STRINGP (it->string))
21795 it_charpos = IT_STRING_CHARPOS (*it);
21796 else
21797 it_charpos = IT_CHARPOS (*it);
21798
21799 /* This condition is for the case that we are called with current_x
21800 past last_visible_x. */
21801 while (it->current_x < max_x)
21802 {
21803 int x_before, x, n_glyphs_before, i, nglyphs;
21804
21805 /* Get the next display element. */
21806 if (!get_next_display_element (it))
21807 break;
21808
21809 /* Produce glyphs. */
21810 x_before = it->current_x;
21811 n_glyphs_before = row->used[TEXT_AREA];
21812 PRODUCE_GLYPHS (it);
21813
21814 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21815 i = 0;
21816 x = x_before;
21817 while (i < nglyphs)
21818 {
21819 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21820
21821 if (it->line_wrap != TRUNCATE
21822 && x + glyph->pixel_width > max_x)
21823 {
21824 /* End of continued line or max_x reached. */
21825 if (CHAR_GLYPH_PADDING_P (*glyph))
21826 {
21827 /* A wide character is unbreakable. */
21828 if (row->reversed_p)
21829 unproduce_glyphs (it, row->used[TEXT_AREA]
21830 - n_glyphs_before);
21831 row->used[TEXT_AREA] = n_glyphs_before;
21832 it->current_x = x_before;
21833 }
21834 else
21835 {
21836 if (row->reversed_p)
21837 unproduce_glyphs (it, row->used[TEXT_AREA]
21838 - (n_glyphs_before + i));
21839 row->used[TEXT_AREA] = n_glyphs_before + i;
21840 it->current_x = x;
21841 }
21842 break;
21843 }
21844 else if (x + glyph->pixel_width >= it->first_visible_x)
21845 {
21846 /* Glyph is at least partially visible. */
21847 ++it->hpos;
21848 if (x < it->first_visible_x)
21849 row->x = x - it->first_visible_x;
21850 }
21851 else
21852 {
21853 /* Glyph is off the left margin of the display area.
21854 Should not happen. */
21855 emacs_abort ();
21856 }
21857
21858 row->ascent = max (row->ascent, it->max_ascent);
21859 row->height = max (row->height, it->max_ascent + it->max_descent);
21860 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21861 row->phys_height = max (row->phys_height,
21862 it->max_phys_ascent + it->max_phys_descent);
21863 row->extra_line_spacing = max (row->extra_line_spacing,
21864 it->max_extra_line_spacing);
21865 x += glyph->pixel_width;
21866 ++i;
21867 }
21868
21869 /* Stop if max_x reached. */
21870 if (i < nglyphs)
21871 break;
21872
21873 /* Stop at line ends. */
21874 if (ITERATOR_AT_END_OF_LINE_P (it))
21875 {
21876 it->continuation_lines_width = 0;
21877 break;
21878 }
21879
21880 set_iterator_to_next (it, 1);
21881 if (STRINGP (it->string))
21882 it_charpos = IT_STRING_CHARPOS (*it);
21883 else
21884 it_charpos = IT_CHARPOS (*it);
21885
21886 /* Stop if truncating at the right edge. */
21887 if (it->line_wrap == TRUNCATE
21888 && it->current_x >= it->last_visible_x)
21889 {
21890 /* Add truncation mark, but don't do it if the line is
21891 truncated at a padding space. */
21892 if (it_charpos < it->string_nchars)
21893 {
21894 if (!FRAME_WINDOW_P (it->f))
21895 {
21896 int ii, n;
21897
21898 if (it->current_x > it->last_visible_x)
21899 {
21900 if (!row->reversed_p)
21901 {
21902 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21903 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21904 break;
21905 }
21906 else
21907 {
21908 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21909 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21910 break;
21911 unproduce_glyphs (it, ii + 1);
21912 ii = row->used[TEXT_AREA] - (ii + 1);
21913 }
21914 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21915 {
21916 row->used[TEXT_AREA] = ii;
21917 produce_special_glyphs (it, IT_TRUNCATION);
21918 }
21919 }
21920 produce_special_glyphs (it, IT_TRUNCATION);
21921 }
21922 row->truncated_on_right_p = 1;
21923 }
21924 break;
21925 }
21926 }
21927
21928 /* Maybe insert a truncation at the left. */
21929 if (it->first_visible_x
21930 && it_charpos > 0)
21931 {
21932 if (!FRAME_WINDOW_P (it->f)
21933 || (row->reversed_p
21934 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21935 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21936 insert_left_trunc_glyphs (it);
21937 row->truncated_on_left_p = 1;
21938 }
21939
21940 it->face_id = saved_face_id;
21941
21942 /* Value is number of columns displayed. */
21943 return it->hpos - hpos_at_start;
21944 }
21945
21946
21947 \f
21948 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
21949 appears as an element of LIST or as the car of an element of LIST.
21950 If PROPVAL is a list, compare each element against LIST in that
21951 way, and return 1/2 if any element of PROPVAL is found in LIST.
21952 Otherwise return 0. This function cannot quit.
21953 The return value is 2 if the text is invisible but with an ellipsis
21954 and 1 if it's invisible and without an ellipsis. */
21955
21956 int
21957 invisible_p (register Lisp_Object propval, Lisp_Object list)
21958 {
21959 register Lisp_Object tail, proptail;
21960
21961 for (tail = list; CONSP (tail); tail = XCDR (tail))
21962 {
21963 register Lisp_Object tem;
21964 tem = XCAR (tail);
21965 if (EQ (propval, tem))
21966 return 1;
21967 if (CONSP (tem) && EQ (propval, XCAR (tem)))
21968 return NILP (XCDR (tem)) ? 1 : 2;
21969 }
21970
21971 if (CONSP (propval))
21972 {
21973 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
21974 {
21975 Lisp_Object propelt;
21976 propelt = XCAR (proptail);
21977 for (tail = list; CONSP (tail); tail = XCDR (tail))
21978 {
21979 register Lisp_Object tem;
21980 tem = XCAR (tail);
21981 if (EQ (propelt, tem))
21982 return 1;
21983 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
21984 return NILP (XCDR (tem)) ? 1 : 2;
21985 }
21986 }
21987 }
21988
21989 return 0;
21990 }
21991
21992 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
21993 doc: /* Non-nil if the property makes the text invisible.
21994 POS-OR-PROP can be a marker or number, in which case it is taken to be
21995 a position in the current buffer and the value of the `invisible' property
21996 is checked; or it can be some other value, which is then presumed to be the
21997 value of the `invisible' property of the text of interest.
21998 The non-nil value returned can be t for truly invisible text or something
21999 else if the text is replaced by an ellipsis. */)
22000 (Lisp_Object pos_or_prop)
22001 {
22002 Lisp_Object prop
22003 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22004 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22005 : pos_or_prop);
22006 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22007 return (invis == 0 ? Qnil
22008 : invis == 1 ? Qt
22009 : make_number (invis));
22010 }
22011
22012 /* Calculate a width or height in pixels from a specification using
22013 the following elements:
22014
22015 SPEC ::=
22016 NUM - a (fractional) multiple of the default font width/height
22017 (NUM) - specifies exactly NUM pixels
22018 UNIT - a fixed number of pixels, see below.
22019 ELEMENT - size of a display element in pixels, see below.
22020 (NUM . SPEC) - equals NUM * SPEC
22021 (+ SPEC SPEC ...) - add pixel values
22022 (- SPEC SPEC ...) - subtract pixel values
22023 (- SPEC) - negate pixel value
22024
22025 NUM ::=
22026 INT or FLOAT - a number constant
22027 SYMBOL - use symbol's (buffer local) variable binding.
22028
22029 UNIT ::=
22030 in - pixels per inch *)
22031 mm - pixels per 1/1000 meter *)
22032 cm - pixels per 1/100 meter *)
22033 width - width of current font in pixels.
22034 height - height of current font in pixels.
22035
22036 *) using the ratio(s) defined in display-pixels-per-inch.
22037
22038 ELEMENT ::=
22039
22040 left-fringe - left fringe width in pixels
22041 right-fringe - right fringe width in pixels
22042
22043 left-margin - left margin width in pixels
22044 right-margin - right margin width in pixels
22045
22046 scroll-bar - scroll-bar area width in pixels
22047
22048 Examples:
22049
22050 Pixels corresponding to 5 inches:
22051 (5 . in)
22052
22053 Total width of non-text areas on left side of window (if scroll-bar is on left):
22054 '(space :width (+ left-fringe left-margin scroll-bar))
22055
22056 Align to first text column (in header line):
22057 '(space :align-to 0)
22058
22059 Align to middle of text area minus half the width of variable `my-image'
22060 containing a loaded image:
22061 '(space :align-to (0.5 . (- text my-image)))
22062
22063 Width of left margin minus width of 1 character in the default font:
22064 '(space :width (- left-margin 1))
22065
22066 Width of left margin minus width of 2 characters in the current font:
22067 '(space :width (- left-margin (2 . width)))
22068
22069 Center 1 character over left-margin (in header line):
22070 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22071
22072 Different ways to express width of left fringe plus left margin minus one pixel:
22073 '(space :width (- (+ left-fringe left-margin) (1)))
22074 '(space :width (+ left-fringe left-margin (- (1))))
22075 '(space :width (+ left-fringe left-margin (-1)))
22076
22077 */
22078
22079 #define NUMVAL(X) \
22080 ((INTEGERP (X) || FLOATP (X)) \
22081 ? XFLOATINT (X) \
22082 : - 1)
22083
22084 static int
22085 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22086 struct font *font, int width_p, int *align_to)
22087 {
22088 double pixels;
22089
22090 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22091 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22092
22093 if (NILP (prop))
22094 return OK_PIXELS (0);
22095
22096 eassert (FRAME_LIVE_P (it->f));
22097
22098 if (SYMBOLP (prop))
22099 {
22100 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22101 {
22102 char *unit = SSDATA (SYMBOL_NAME (prop));
22103
22104 if (unit[0] == 'i' && unit[1] == 'n')
22105 pixels = 1.0;
22106 else if (unit[0] == 'm' && unit[1] == 'm')
22107 pixels = 25.4;
22108 else if (unit[0] == 'c' && unit[1] == 'm')
22109 pixels = 2.54;
22110 else
22111 pixels = 0;
22112 if (pixels > 0)
22113 {
22114 double ppi;
22115 #ifdef HAVE_WINDOW_SYSTEM
22116 if (FRAME_WINDOW_P (it->f)
22117 && (ppi = (width_p
22118 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22119 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22120 ppi > 0))
22121 return OK_PIXELS (ppi / pixels);
22122 #endif
22123
22124 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22125 || (CONSP (Vdisplay_pixels_per_inch)
22126 && (ppi = (width_p
22127 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22128 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22129 ppi > 0)))
22130 return OK_PIXELS (ppi / pixels);
22131
22132 return 0;
22133 }
22134 }
22135
22136 #ifdef HAVE_WINDOW_SYSTEM
22137 if (EQ (prop, Qheight))
22138 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22139 if (EQ (prop, Qwidth))
22140 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22141 #else
22142 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22143 return OK_PIXELS (1);
22144 #endif
22145
22146 if (EQ (prop, Qtext))
22147 return OK_PIXELS (width_p
22148 ? window_box_width (it->w, TEXT_AREA)
22149 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22150
22151 if (align_to && *align_to < 0)
22152 {
22153 *res = 0;
22154 if (EQ (prop, Qleft))
22155 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22156 if (EQ (prop, Qright))
22157 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22158 if (EQ (prop, Qcenter))
22159 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22160 + window_box_width (it->w, TEXT_AREA) / 2);
22161 if (EQ (prop, Qleft_fringe))
22162 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22163 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22164 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22165 if (EQ (prop, Qright_fringe))
22166 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22167 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22168 : window_box_right_offset (it->w, TEXT_AREA));
22169 if (EQ (prop, Qleft_margin))
22170 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22171 if (EQ (prop, Qright_margin))
22172 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22173 if (EQ (prop, Qscroll_bar))
22174 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22175 ? 0
22176 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22177 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22178 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22179 : 0)));
22180 }
22181 else
22182 {
22183 if (EQ (prop, Qleft_fringe))
22184 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22185 if (EQ (prop, Qright_fringe))
22186 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22187 if (EQ (prop, Qleft_margin))
22188 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22189 if (EQ (prop, Qright_margin))
22190 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22191 if (EQ (prop, Qscroll_bar))
22192 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22193 }
22194
22195 prop = buffer_local_value_1 (prop, it->w->buffer);
22196 if (EQ (prop, Qunbound))
22197 prop = Qnil;
22198 }
22199
22200 if (INTEGERP (prop) || FLOATP (prop))
22201 {
22202 int base_unit = (width_p
22203 ? FRAME_COLUMN_WIDTH (it->f)
22204 : FRAME_LINE_HEIGHT (it->f));
22205 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22206 }
22207
22208 if (CONSP (prop))
22209 {
22210 Lisp_Object car = XCAR (prop);
22211 Lisp_Object cdr = XCDR (prop);
22212
22213 if (SYMBOLP (car))
22214 {
22215 #ifdef HAVE_WINDOW_SYSTEM
22216 if (FRAME_WINDOW_P (it->f)
22217 && valid_image_p (prop))
22218 {
22219 ptrdiff_t id = lookup_image (it->f, prop);
22220 struct image *img = IMAGE_FROM_ID (it->f, id);
22221
22222 return OK_PIXELS (width_p ? img->width : img->height);
22223 }
22224 #endif
22225 if (EQ (car, Qplus) || EQ (car, Qminus))
22226 {
22227 int first = 1;
22228 double px;
22229
22230 pixels = 0;
22231 while (CONSP (cdr))
22232 {
22233 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22234 font, width_p, align_to))
22235 return 0;
22236 if (first)
22237 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22238 else
22239 pixels += px;
22240 cdr = XCDR (cdr);
22241 }
22242 if (EQ (car, Qminus))
22243 pixels = -pixels;
22244 return OK_PIXELS (pixels);
22245 }
22246
22247 car = buffer_local_value_1 (car, it->w->buffer);
22248 if (EQ (car, Qunbound))
22249 car = Qnil;
22250 }
22251
22252 if (INTEGERP (car) || FLOATP (car))
22253 {
22254 double fact;
22255 pixels = XFLOATINT (car);
22256 if (NILP (cdr))
22257 return OK_PIXELS (pixels);
22258 if (calc_pixel_width_or_height (&fact, it, cdr,
22259 font, width_p, align_to))
22260 return OK_PIXELS (pixels * fact);
22261 return 0;
22262 }
22263
22264 return 0;
22265 }
22266
22267 return 0;
22268 }
22269
22270 \f
22271 /***********************************************************************
22272 Glyph Display
22273 ***********************************************************************/
22274
22275 #ifdef HAVE_WINDOW_SYSTEM
22276
22277 #ifdef GLYPH_DEBUG
22278
22279 void
22280 dump_glyph_string (struct glyph_string *s)
22281 {
22282 fprintf (stderr, "glyph string\n");
22283 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22284 s->x, s->y, s->width, s->height);
22285 fprintf (stderr, " ybase = %d\n", s->ybase);
22286 fprintf (stderr, " hl = %d\n", s->hl);
22287 fprintf (stderr, " left overhang = %d, right = %d\n",
22288 s->left_overhang, s->right_overhang);
22289 fprintf (stderr, " nchars = %d\n", s->nchars);
22290 fprintf (stderr, " extends to end of line = %d\n",
22291 s->extends_to_end_of_line_p);
22292 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22293 fprintf (stderr, " bg width = %d\n", s->background_width);
22294 }
22295
22296 #endif /* GLYPH_DEBUG */
22297
22298 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22299 of XChar2b structures for S; it can't be allocated in
22300 init_glyph_string because it must be allocated via `alloca'. W
22301 is the window on which S is drawn. ROW and AREA are the glyph row
22302 and area within the row from which S is constructed. START is the
22303 index of the first glyph structure covered by S. HL is a
22304 face-override for drawing S. */
22305
22306 #ifdef HAVE_NTGUI
22307 #define OPTIONAL_HDC(hdc) HDC hdc,
22308 #define DECLARE_HDC(hdc) HDC hdc;
22309 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22310 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22311 #endif
22312
22313 #ifndef OPTIONAL_HDC
22314 #define OPTIONAL_HDC(hdc)
22315 #define DECLARE_HDC(hdc)
22316 #define ALLOCATE_HDC(hdc, f)
22317 #define RELEASE_HDC(hdc, f)
22318 #endif
22319
22320 static void
22321 init_glyph_string (struct glyph_string *s,
22322 OPTIONAL_HDC (hdc)
22323 XChar2b *char2b, struct window *w, struct glyph_row *row,
22324 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22325 {
22326 memset (s, 0, sizeof *s);
22327 s->w = w;
22328 s->f = XFRAME (w->frame);
22329 #ifdef HAVE_NTGUI
22330 s->hdc = hdc;
22331 #endif
22332 s->display = FRAME_X_DISPLAY (s->f);
22333 s->window = FRAME_X_WINDOW (s->f);
22334 s->char2b = char2b;
22335 s->hl = hl;
22336 s->row = row;
22337 s->area = area;
22338 s->first_glyph = row->glyphs[area] + start;
22339 s->height = row->height;
22340 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22341 s->ybase = s->y + row->ascent;
22342 }
22343
22344
22345 /* Append the list of glyph strings with head H and tail T to the list
22346 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22347
22348 static void
22349 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22350 struct glyph_string *h, struct glyph_string *t)
22351 {
22352 if (h)
22353 {
22354 if (*head)
22355 (*tail)->next = h;
22356 else
22357 *head = h;
22358 h->prev = *tail;
22359 *tail = t;
22360 }
22361 }
22362
22363
22364 /* Prepend the list of glyph strings with head H and tail T to the
22365 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22366 result. */
22367
22368 static void
22369 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22370 struct glyph_string *h, struct glyph_string *t)
22371 {
22372 if (h)
22373 {
22374 if (*head)
22375 (*head)->prev = t;
22376 else
22377 *tail = t;
22378 t->next = *head;
22379 *head = h;
22380 }
22381 }
22382
22383
22384 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22385 Set *HEAD and *TAIL to the resulting list. */
22386
22387 static void
22388 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22389 struct glyph_string *s)
22390 {
22391 s->next = s->prev = NULL;
22392 append_glyph_string_lists (head, tail, s, s);
22393 }
22394
22395
22396 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22397 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22398 make sure that X resources for the face returned are allocated.
22399 Value is a pointer to a realized face that is ready for display if
22400 DISPLAY_P is non-zero. */
22401
22402 static struct face *
22403 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22404 XChar2b *char2b, int display_p)
22405 {
22406 struct face *face = FACE_FROM_ID (f, face_id);
22407
22408 if (face->font)
22409 {
22410 unsigned code = face->font->driver->encode_char (face->font, c);
22411
22412 if (code != FONT_INVALID_CODE)
22413 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22414 else
22415 STORE_XCHAR2B (char2b, 0, 0);
22416 }
22417
22418 /* Make sure X resources of the face are allocated. */
22419 #ifdef HAVE_X_WINDOWS
22420 if (display_p)
22421 #endif
22422 {
22423 eassert (face != NULL);
22424 PREPARE_FACE_FOR_DISPLAY (f, face);
22425 }
22426
22427 return face;
22428 }
22429
22430
22431 /* Get face and two-byte form of character glyph GLYPH on frame F.
22432 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22433 a pointer to a realized face that is ready for display. */
22434
22435 static struct face *
22436 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22437 XChar2b *char2b, int *two_byte_p)
22438 {
22439 struct face *face;
22440
22441 eassert (glyph->type == CHAR_GLYPH);
22442 face = FACE_FROM_ID (f, glyph->face_id);
22443
22444 if (two_byte_p)
22445 *two_byte_p = 0;
22446
22447 if (face->font)
22448 {
22449 unsigned code;
22450
22451 if (CHAR_BYTE8_P (glyph->u.ch))
22452 code = CHAR_TO_BYTE8 (glyph->u.ch);
22453 else
22454 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22455
22456 if (code != FONT_INVALID_CODE)
22457 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22458 else
22459 STORE_XCHAR2B (char2b, 0, 0);
22460 }
22461
22462 /* Make sure X resources of the face are allocated. */
22463 eassert (face != NULL);
22464 PREPARE_FACE_FOR_DISPLAY (f, face);
22465 return face;
22466 }
22467
22468
22469 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22470 Return 1 if FONT has a glyph for C, otherwise return 0. */
22471
22472 static int
22473 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22474 {
22475 unsigned code;
22476
22477 if (CHAR_BYTE8_P (c))
22478 code = CHAR_TO_BYTE8 (c);
22479 else
22480 code = font->driver->encode_char (font, c);
22481
22482 if (code == FONT_INVALID_CODE)
22483 return 0;
22484 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22485 return 1;
22486 }
22487
22488
22489 /* Fill glyph string S with composition components specified by S->cmp.
22490
22491 BASE_FACE is the base face of the composition.
22492 S->cmp_from is the index of the first component for S.
22493
22494 OVERLAPS non-zero means S should draw the foreground only, and use
22495 its physical height for clipping. See also draw_glyphs.
22496
22497 Value is the index of a component not in S. */
22498
22499 static int
22500 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22501 int overlaps)
22502 {
22503 int i;
22504 /* For all glyphs of this composition, starting at the offset
22505 S->cmp_from, until we reach the end of the definition or encounter a
22506 glyph that requires the different face, add it to S. */
22507 struct face *face;
22508
22509 eassert (s);
22510
22511 s->for_overlaps = overlaps;
22512 s->face = NULL;
22513 s->font = NULL;
22514 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22515 {
22516 int c = COMPOSITION_GLYPH (s->cmp, i);
22517
22518 /* TAB in a composition means display glyphs with padding space
22519 on the left or right. */
22520 if (c != '\t')
22521 {
22522 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22523 -1, Qnil);
22524
22525 face = get_char_face_and_encoding (s->f, c, face_id,
22526 s->char2b + i, 1);
22527 if (face)
22528 {
22529 if (! s->face)
22530 {
22531 s->face = face;
22532 s->font = s->face->font;
22533 }
22534 else if (s->face != face)
22535 break;
22536 }
22537 }
22538 ++s->nchars;
22539 }
22540 s->cmp_to = i;
22541
22542 if (s->face == NULL)
22543 {
22544 s->face = base_face->ascii_face;
22545 s->font = s->face->font;
22546 }
22547
22548 /* All glyph strings for the same composition has the same width,
22549 i.e. the width set for the first component of the composition. */
22550 s->width = s->first_glyph->pixel_width;
22551
22552 /* If the specified font could not be loaded, use the frame's
22553 default font, but record the fact that we couldn't load it in
22554 the glyph string so that we can draw rectangles for the
22555 characters of the glyph string. */
22556 if (s->font == NULL)
22557 {
22558 s->font_not_found_p = 1;
22559 s->font = FRAME_FONT (s->f);
22560 }
22561
22562 /* Adjust base line for subscript/superscript text. */
22563 s->ybase += s->first_glyph->voffset;
22564
22565 /* This glyph string must always be drawn with 16-bit functions. */
22566 s->two_byte_p = 1;
22567
22568 return s->cmp_to;
22569 }
22570
22571 static int
22572 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22573 int start, int end, int overlaps)
22574 {
22575 struct glyph *glyph, *last;
22576 Lisp_Object lgstring;
22577 int i;
22578
22579 s->for_overlaps = overlaps;
22580 glyph = s->row->glyphs[s->area] + start;
22581 last = s->row->glyphs[s->area] + end;
22582 s->cmp_id = glyph->u.cmp.id;
22583 s->cmp_from = glyph->slice.cmp.from;
22584 s->cmp_to = glyph->slice.cmp.to + 1;
22585 s->face = FACE_FROM_ID (s->f, face_id);
22586 lgstring = composition_gstring_from_id (s->cmp_id);
22587 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22588 glyph++;
22589 while (glyph < last
22590 && glyph->u.cmp.automatic
22591 && glyph->u.cmp.id == s->cmp_id
22592 && s->cmp_to == glyph->slice.cmp.from)
22593 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22594
22595 for (i = s->cmp_from; i < s->cmp_to; i++)
22596 {
22597 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22598 unsigned code = LGLYPH_CODE (lglyph);
22599
22600 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22601 }
22602 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22603 return glyph - s->row->glyphs[s->area];
22604 }
22605
22606
22607 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22608 See the comment of fill_glyph_string for arguments.
22609 Value is the index of the first glyph not in S. */
22610
22611
22612 static int
22613 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22614 int start, int end, int overlaps)
22615 {
22616 struct glyph *glyph, *last;
22617 int voffset;
22618
22619 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22620 s->for_overlaps = overlaps;
22621 glyph = s->row->glyphs[s->area] + start;
22622 last = s->row->glyphs[s->area] + end;
22623 voffset = glyph->voffset;
22624 s->face = FACE_FROM_ID (s->f, face_id);
22625 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22626 s->nchars = 1;
22627 s->width = glyph->pixel_width;
22628 glyph++;
22629 while (glyph < last
22630 && glyph->type == GLYPHLESS_GLYPH
22631 && glyph->voffset == voffset
22632 && glyph->face_id == face_id)
22633 {
22634 s->nchars++;
22635 s->width += glyph->pixel_width;
22636 glyph++;
22637 }
22638 s->ybase += voffset;
22639 return glyph - s->row->glyphs[s->area];
22640 }
22641
22642
22643 /* Fill glyph string S from a sequence of character glyphs.
22644
22645 FACE_ID is the face id of the string. START is the index of the
22646 first glyph to consider, END is the index of the last + 1.
22647 OVERLAPS non-zero means S should draw the foreground only, and use
22648 its physical height for clipping. See also draw_glyphs.
22649
22650 Value is the index of the first glyph not in S. */
22651
22652 static int
22653 fill_glyph_string (struct glyph_string *s, int face_id,
22654 int start, int end, int overlaps)
22655 {
22656 struct glyph *glyph, *last;
22657 int voffset;
22658 int glyph_not_available_p;
22659
22660 eassert (s->f == XFRAME (s->w->frame));
22661 eassert (s->nchars == 0);
22662 eassert (start >= 0 && end > start);
22663
22664 s->for_overlaps = overlaps;
22665 glyph = s->row->glyphs[s->area] + start;
22666 last = s->row->glyphs[s->area] + end;
22667 voffset = glyph->voffset;
22668 s->padding_p = glyph->padding_p;
22669 glyph_not_available_p = glyph->glyph_not_available_p;
22670
22671 while (glyph < last
22672 && glyph->type == CHAR_GLYPH
22673 && glyph->voffset == voffset
22674 /* Same face id implies same font, nowadays. */
22675 && glyph->face_id == face_id
22676 && glyph->glyph_not_available_p == glyph_not_available_p)
22677 {
22678 int two_byte_p;
22679
22680 s->face = get_glyph_face_and_encoding (s->f, glyph,
22681 s->char2b + s->nchars,
22682 &two_byte_p);
22683 s->two_byte_p = two_byte_p;
22684 ++s->nchars;
22685 eassert (s->nchars <= end - start);
22686 s->width += glyph->pixel_width;
22687 if (glyph++->padding_p != s->padding_p)
22688 break;
22689 }
22690
22691 s->font = s->face->font;
22692
22693 /* If the specified font could not be loaded, use the frame's font,
22694 but record the fact that we couldn't load it in
22695 S->font_not_found_p so that we can draw rectangles for the
22696 characters of the glyph string. */
22697 if (s->font == NULL || glyph_not_available_p)
22698 {
22699 s->font_not_found_p = 1;
22700 s->font = FRAME_FONT (s->f);
22701 }
22702
22703 /* Adjust base line for subscript/superscript text. */
22704 s->ybase += voffset;
22705
22706 eassert (s->face && s->face->gc);
22707 return glyph - s->row->glyphs[s->area];
22708 }
22709
22710
22711 /* Fill glyph string S from image glyph S->first_glyph. */
22712
22713 static void
22714 fill_image_glyph_string (struct glyph_string *s)
22715 {
22716 eassert (s->first_glyph->type == IMAGE_GLYPH);
22717 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22718 eassert (s->img);
22719 s->slice = s->first_glyph->slice.img;
22720 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22721 s->font = s->face->font;
22722 s->width = s->first_glyph->pixel_width;
22723
22724 /* Adjust base line for subscript/superscript text. */
22725 s->ybase += s->first_glyph->voffset;
22726 }
22727
22728
22729 /* Fill glyph string S from a sequence of stretch glyphs.
22730
22731 START is the index of the first glyph to consider,
22732 END is the index of the last + 1.
22733
22734 Value is the index of the first glyph not in S. */
22735
22736 static int
22737 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22738 {
22739 struct glyph *glyph, *last;
22740 int voffset, face_id;
22741
22742 eassert (s->first_glyph->type == STRETCH_GLYPH);
22743
22744 glyph = s->row->glyphs[s->area] + start;
22745 last = s->row->glyphs[s->area] + end;
22746 face_id = glyph->face_id;
22747 s->face = FACE_FROM_ID (s->f, face_id);
22748 s->font = s->face->font;
22749 s->width = glyph->pixel_width;
22750 s->nchars = 1;
22751 voffset = glyph->voffset;
22752
22753 for (++glyph;
22754 (glyph < last
22755 && glyph->type == STRETCH_GLYPH
22756 && glyph->voffset == voffset
22757 && glyph->face_id == face_id);
22758 ++glyph)
22759 s->width += glyph->pixel_width;
22760
22761 /* Adjust base line for subscript/superscript text. */
22762 s->ybase += voffset;
22763
22764 /* The case that face->gc == 0 is handled when drawing the glyph
22765 string by calling PREPARE_FACE_FOR_DISPLAY. */
22766 eassert (s->face);
22767 return glyph - s->row->glyphs[s->area];
22768 }
22769
22770 static struct font_metrics *
22771 get_per_char_metric (struct font *font, XChar2b *char2b)
22772 {
22773 static struct font_metrics metrics;
22774 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22775
22776 if (! font || code == FONT_INVALID_CODE)
22777 return NULL;
22778 font->driver->text_extents (font, &code, 1, &metrics);
22779 return &metrics;
22780 }
22781
22782 /* EXPORT for RIF:
22783 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22784 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22785 assumed to be zero. */
22786
22787 void
22788 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22789 {
22790 *left = *right = 0;
22791
22792 if (glyph->type == CHAR_GLYPH)
22793 {
22794 struct face *face;
22795 XChar2b char2b;
22796 struct font_metrics *pcm;
22797
22798 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22799 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22800 {
22801 if (pcm->rbearing > pcm->width)
22802 *right = pcm->rbearing - pcm->width;
22803 if (pcm->lbearing < 0)
22804 *left = -pcm->lbearing;
22805 }
22806 }
22807 else if (glyph->type == COMPOSITE_GLYPH)
22808 {
22809 if (! glyph->u.cmp.automatic)
22810 {
22811 struct composition *cmp = composition_table[glyph->u.cmp.id];
22812
22813 if (cmp->rbearing > cmp->pixel_width)
22814 *right = cmp->rbearing - cmp->pixel_width;
22815 if (cmp->lbearing < 0)
22816 *left = - cmp->lbearing;
22817 }
22818 else
22819 {
22820 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22821 struct font_metrics metrics;
22822
22823 composition_gstring_width (gstring, glyph->slice.cmp.from,
22824 glyph->slice.cmp.to + 1, &metrics);
22825 if (metrics.rbearing > metrics.width)
22826 *right = metrics.rbearing - metrics.width;
22827 if (metrics.lbearing < 0)
22828 *left = - metrics.lbearing;
22829 }
22830 }
22831 }
22832
22833
22834 /* Return the index of the first glyph preceding glyph string S that
22835 is overwritten by S because of S's left overhang. Value is -1
22836 if no glyphs are overwritten. */
22837
22838 static int
22839 left_overwritten (struct glyph_string *s)
22840 {
22841 int k;
22842
22843 if (s->left_overhang)
22844 {
22845 int x = 0, i;
22846 struct glyph *glyphs = s->row->glyphs[s->area];
22847 int first = s->first_glyph - glyphs;
22848
22849 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22850 x -= glyphs[i].pixel_width;
22851
22852 k = i + 1;
22853 }
22854 else
22855 k = -1;
22856
22857 return k;
22858 }
22859
22860
22861 /* Return the index of the first glyph preceding glyph string S that
22862 is overwriting S because of its right overhang. Value is -1 if no
22863 glyph in front of S overwrites S. */
22864
22865 static int
22866 left_overwriting (struct glyph_string *s)
22867 {
22868 int i, k, x;
22869 struct glyph *glyphs = s->row->glyphs[s->area];
22870 int first = s->first_glyph - glyphs;
22871
22872 k = -1;
22873 x = 0;
22874 for (i = first - 1; i >= 0; --i)
22875 {
22876 int left, right;
22877 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22878 if (x + right > 0)
22879 k = i;
22880 x -= glyphs[i].pixel_width;
22881 }
22882
22883 return k;
22884 }
22885
22886
22887 /* Return the index of the last glyph following glyph string S that is
22888 overwritten by S because of S's right overhang. Value is -1 if
22889 no such glyph is found. */
22890
22891 static int
22892 right_overwritten (struct glyph_string *s)
22893 {
22894 int k = -1;
22895
22896 if (s->right_overhang)
22897 {
22898 int x = 0, i;
22899 struct glyph *glyphs = s->row->glyphs[s->area];
22900 int first = (s->first_glyph - glyphs
22901 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
22902 int end = s->row->used[s->area];
22903
22904 for (i = first; i < end && s->right_overhang > x; ++i)
22905 x += glyphs[i].pixel_width;
22906
22907 k = i;
22908 }
22909
22910 return k;
22911 }
22912
22913
22914 /* Return the index of the last glyph following glyph string S that
22915 overwrites S because of its left overhang. Value is negative
22916 if no such glyph is found. */
22917
22918 static int
22919 right_overwriting (struct glyph_string *s)
22920 {
22921 int i, k, x;
22922 int end = s->row->used[s->area];
22923 struct glyph *glyphs = s->row->glyphs[s->area];
22924 int first = (s->first_glyph - glyphs
22925 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
22926
22927 k = -1;
22928 x = 0;
22929 for (i = first; i < end; ++i)
22930 {
22931 int left, right;
22932 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22933 if (x - left < 0)
22934 k = i;
22935 x += glyphs[i].pixel_width;
22936 }
22937
22938 return k;
22939 }
22940
22941
22942 /* Set background width of glyph string S. START is the index of the
22943 first glyph following S. LAST_X is the right-most x-position + 1
22944 in the drawing area. */
22945
22946 static void
22947 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22948 {
22949 /* If the face of this glyph string has to be drawn to the end of
22950 the drawing area, set S->extends_to_end_of_line_p. */
22951
22952 if (start == s->row->used[s->area]
22953 && s->area == TEXT_AREA
22954 && ((s->row->fill_line_p
22955 && (s->hl == DRAW_NORMAL_TEXT
22956 || s->hl == DRAW_IMAGE_RAISED
22957 || s->hl == DRAW_IMAGE_SUNKEN))
22958 || s->hl == DRAW_MOUSE_FACE))
22959 s->extends_to_end_of_line_p = 1;
22960
22961 /* If S extends its face to the end of the line, set its
22962 background_width to the distance to the right edge of the drawing
22963 area. */
22964 if (s->extends_to_end_of_line_p)
22965 s->background_width = last_x - s->x + 1;
22966 else
22967 s->background_width = s->width;
22968 }
22969
22970
22971 /* Compute overhangs and x-positions for glyph string S and its
22972 predecessors, or successors. X is the starting x-position for S.
22973 BACKWARD_P non-zero means process predecessors. */
22974
22975 static void
22976 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
22977 {
22978 if (backward_p)
22979 {
22980 while (s)
22981 {
22982 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22983 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22984 x -= s->width;
22985 s->x = x;
22986 s = s->prev;
22987 }
22988 }
22989 else
22990 {
22991 while (s)
22992 {
22993 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
22994 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
22995 s->x = x;
22996 x += s->width;
22997 s = s->next;
22998 }
22999 }
23000 }
23001
23002
23003
23004 /* The following macros are only called from draw_glyphs below.
23005 They reference the following parameters of that function directly:
23006 `w', `row', `area', and `overlap_p'
23007 as well as the following local variables:
23008 `s', `f', and `hdc' (in W32) */
23009
23010 #ifdef HAVE_NTGUI
23011 /* On W32, silently add local `hdc' variable to argument list of
23012 init_glyph_string. */
23013 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23014 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23015 #else
23016 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23017 init_glyph_string (s, char2b, w, row, area, start, hl)
23018 #endif
23019
23020 /* Add a glyph string for a stretch glyph to the list of strings
23021 between HEAD and TAIL. START is the index of the stretch glyph in
23022 row area AREA of glyph row ROW. END is the index of the last glyph
23023 in that glyph row area. X is the current output position assigned
23024 to the new glyph string constructed. HL overrides that face of the
23025 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23026 is the right-most x-position of the drawing area. */
23027
23028 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23029 and below -- keep them on one line. */
23030 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23031 do \
23032 { \
23033 s = alloca (sizeof *s); \
23034 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23035 START = fill_stretch_glyph_string (s, START, END); \
23036 append_glyph_string (&HEAD, &TAIL, s); \
23037 s->x = (X); \
23038 } \
23039 while (0)
23040
23041
23042 /* Add a glyph string for an image glyph to the list of strings
23043 between HEAD and TAIL. START is the index of the image glyph in
23044 row area AREA of glyph row ROW. END is the index of the last glyph
23045 in that glyph row area. X is the current output position assigned
23046 to the new glyph string constructed. HL overrides that face of the
23047 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23048 is the right-most x-position of the drawing area. */
23049
23050 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23051 do \
23052 { \
23053 s = alloca (sizeof *s); \
23054 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23055 fill_image_glyph_string (s); \
23056 append_glyph_string (&HEAD, &TAIL, s); \
23057 ++START; \
23058 s->x = (X); \
23059 } \
23060 while (0)
23061
23062
23063 /* Add a glyph string for a sequence of character glyphs to the list
23064 of strings between HEAD and TAIL. START is the index of the first
23065 glyph in row area AREA of glyph row ROW that is part of the new
23066 glyph string. END is the index of the last glyph in that glyph row
23067 area. X is the current output position assigned to the new glyph
23068 string constructed. HL overrides that face of the glyph; e.g. it
23069 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23070 right-most x-position of the drawing area. */
23071
23072 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23073 do \
23074 { \
23075 int face_id; \
23076 XChar2b *char2b; \
23077 \
23078 face_id = (row)->glyphs[area][START].face_id; \
23079 \
23080 s = alloca (sizeof *s); \
23081 char2b = alloca ((END - START) * sizeof *char2b); \
23082 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23083 append_glyph_string (&HEAD, &TAIL, s); \
23084 s->x = (X); \
23085 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23086 } \
23087 while (0)
23088
23089
23090 /* Add a glyph string for a composite sequence to the list of strings
23091 between HEAD and TAIL. START is the index of the first glyph in
23092 row area AREA of glyph row ROW that is part of the new glyph
23093 string. END is the index of the last glyph in that glyph row area.
23094 X is the current output position assigned to the new glyph string
23095 constructed. HL overrides that face of the glyph; e.g. it is
23096 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23097 x-position of the drawing area. */
23098
23099 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23100 do { \
23101 int face_id = (row)->glyphs[area][START].face_id; \
23102 struct face *base_face = FACE_FROM_ID (f, face_id); \
23103 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23104 struct composition *cmp = composition_table[cmp_id]; \
23105 XChar2b *char2b; \
23106 struct glyph_string *first_s = NULL; \
23107 int n; \
23108 \
23109 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23110 \
23111 /* Make glyph_strings for each glyph sequence that is drawable by \
23112 the same face, and append them to HEAD/TAIL. */ \
23113 for (n = 0; n < cmp->glyph_len;) \
23114 { \
23115 s = alloca (sizeof *s); \
23116 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23117 append_glyph_string (&(HEAD), &(TAIL), s); \
23118 s->cmp = cmp; \
23119 s->cmp_from = n; \
23120 s->x = (X); \
23121 if (n == 0) \
23122 first_s = s; \
23123 n = fill_composite_glyph_string (s, base_face, overlaps); \
23124 } \
23125 \
23126 ++START; \
23127 s = first_s; \
23128 } while (0)
23129
23130
23131 /* Add a glyph string for a glyph-string sequence to the list of strings
23132 between HEAD and TAIL. */
23133
23134 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23135 do { \
23136 int face_id; \
23137 XChar2b *char2b; \
23138 Lisp_Object gstring; \
23139 \
23140 face_id = (row)->glyphs[area][START].face_id; \
23141 gstring = (composition_gstring_from_id \
23142 ((row)->glyphs[area][START].u.cmp.id)); \
23143 s = alloca (sizeof *s); \
23144 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23145 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23146 append_glyph_string (&(HEAD), &(TAIL), s); \
23147 s->x = (X); \
23148 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23149 } while (0)
23150
23151
23152 /* Add a glyph string for a sequence of glyphless character's glyphs
23153 to the list of strings between HEAD and TAIL. The meanings of
23154 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23155
23156 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23157 do \
23158 { \
23159 int face_id; \
23160 \
23161 face_id = (row)->glyphs[area][START].face_id; \
23162 \
23163 s = alloca (sizeof *s); \
23164 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23165 append_glyph_string (&HEAD, &TAIL, s); \
23166 s->x = (X); \
23167 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23168 overlaps); \
23169 } \
23170 while (0)
23171
23172
23173 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23174 of AREA of glyph row ROW on window W between indices START and END.
23175 HL overrides the face for drawing glyph strings, e.g. it is
23176 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23177 x-positions of the drawing area.
23178
23179 This is an ugly monster macro construct because we must use alloca
23180 to allocate glyph strings (because draw_glyphs can be called
23181 asynchronously). */
23182
23183 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23184 do \
23185 { \
23186 HEAD = TAIL = NULL; \
23187 while (START < END) \
23188 { \
23189 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23190 switch (first_glyph->type) \
23191 { \
23192 case CHAR_GLYPH: \
23193 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23194 HL, X, LAST_X); \
23195 break; \
23196 \
23197 case COMPOSITE_GLYPH: \
23198 if (first_glyph->u.cmp.automatic) \
23199 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23200 HL, X, LAST_X); \
23201 else \
23202 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23203 HL, X, LAST_X); \
23204 break; \
23205 \
23206 case STRETCH_GLYPH: \
23207 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23208 HL, X, LAST_X); \
23209 break; \
23210 \
23211 case IMAGE_GLYPH: \
23212 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23213 HL, X, LAST_X); \
23214 break; \
23215 \
23216 case GLYPHLESS_GLYPH: \
23217 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23218 HL, X, LAST_X); \
23219 break; \
23220 \
23221 default: \
23222 emacs_abort (); \
23223 } \
23224 \
23225 if (s) \
23226 { \
23227 set_glyph_string_background_width (s, START, LAST_X); \
23228 (X) += s->width; \
23229 } \
23230 } \
23231 } while (0)
23232
23233
23234 /* Draw glyphs between START and END in AREA of ROW on window W,
23235 starting at x-position X. X is relative to AREA in W. HL is a
23236 face-override with the following meaning:
23237
23238 DRAW_NORMAL_TEXT draw normally
23239 DRAW_CURSOR draw in cursor face
23240 DRAW_MOUSE_FACE draw in mouse face.
23241 DRAW_INVERSE_VIDEO draw in mode line face
23242 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23243 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23244
23245 If OVERLAPS is non-zero, draw only the foreground of characters and
23246 clip to the physical height of ROW. Non-zero value also defines
23247 the overlapping part to be drawn:
23248
23249 OVERLAPS_PRED overlap with preceding rows
23250 OVERLAPS_SUCC overlap with succeeding rows
23251 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23252 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23253
23254 Value is the x-position reached, relative to AREA of W. */
23255
23256 static int
23257 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23258 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23259 enum draw_glyphs_face hl, int overlaps)
23260 {
23261 struct glyph_string *head, *tail;
23262 struct glyph_string *s;
23263 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23264 int i, j, x_reached, last_x, area_left = 0;
23265 struct frame *f = XFRAME (WINDOW_FRAME (w));
23266 DECLARE_HDC (hdc);
23267
23268 ALLOCATE_HDC (hdc, f);
23269
23270 /* Let's rather be paranoid than getting a SEGV. */
23271 end = min (end, row->used[area]);
23272 start = clip_to_bounds (0, start, end);
23273
23274 /* Translate X to frame coordinates. Set last_x to the right
23275 end of the drawing area. */
23276 if (row->full_width_p)
23277 {
23278 /* X is relative to the left edge of W, without scroll bars
23279 or fringes. */
23280 area_left = WINDOW_LEFT_EDGE_X (w);
23281 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23282 }
23283 else
23284 {
23285 area_left = window_box_left (w, area);
23286 last_x = area_left + window_box_width (w, area);
23287 }
23288 x += area_left;
23289
23290 /* Build a doubly-linked list of glyph_string structures between
23291 head and tail from what we have to draw. Note that the macro
23292 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23293 the reason we use a separate variable `i'. */
23294 i = start;
23295 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23296 if (tail)
23297 x_reached = tail->x + tail->background_width;
23298 else
23299 x_reached = x;
23300
23301 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23302 the row, redraw some glyphs in front or following the glyph
23303 strings built above. */
23304 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23305 {
23306 struct glyph_string *h, *t;
23307 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23308 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23309 int check_mouse_face = 0;
23310 int dummy_x = 0;
23311
23312 /* If mouse highlighting is on, we may need to draw adjacent
23313 glyphs using mouse-face highlighting. */
23314 if (area == TEXT_AREA && row->mouse_face_p
23315 && hlinfo->mouse_face_beg_row >= 0
23316 && hlinfo->mouse_face_end_row >= 0)
23317 {
23318 struct glyph_row *mouse_beg_row, *mouse_end_row;
23319
23320 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23321 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23322
23323 if (row >= mouse_beg_row && row <= mouse_end_row)
23324 {
23325 check_mouse_face = 1;
23326 mouse_beg_col = (row == mouse_beg_row)
23327 ? hlinfo->mouse_face_beg_col : 0;
23328 mouse_end_col = (row == mouse_end_row)
23329 ? hlinfo->mouse_face_end_col
23330 : row->used[TEXT_AREA];
23331 }
23332 }
23333
23334 /* Compute overhangs for all glyph strings. */
23335 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23336 for (s = head; s; s = s->next)
23337 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23338
23339 /* Prepend glyph strings for glyphs in front of the first glyph
23340 string that are overwritten because of the first glyph
23341 string's left overhang. The background of all strings
23342 prepended must be drawn because the first glyph string
23343 draws over it. */
23344 i = left_overwritten (head);
23345 if (i >= 0)
23346 {
23347 enum draw_glyphs_face overlap_hl;
23348
23349 /* If this row contains mouse highlighting, attempt to draw
23350 the overlapped glyphs with the correct highlight. This
23351 code fails if the overlap encompasses more than one glyph
23352 and mouse-highlight spans only some of these glyphs.
23353 However, making it work perfectly involves a lot more
23354 code, and I don't know if the pathological case occurs in
23355 practice, so we'll stick to this for now. --- cyd */
23356 if (check_mouse_face
23357 && mouse_beg_col < start && mouse_end_col > i)
23358 overlap_hl = DRAW_MOUSE_FACE;
23359 else
23360 overlap_hl = DRAW_NORMAL_TEXT;
23361
23362 j = i;
23363 BUILD_GLYPH_STRINGS (j, start, h, t,
23364 overlap_hl, dummy_x, last_x);
23365 start = i;
23366 compute_overhangs_and_x (t, head->x, 1);
23367 prepend_glyph_string_lists (&head, &tail, h, t);
23368 clip_head = head;
23369 }
23370
23371 /* Prepend glyph strings for glyphs in front of the first glyph
23372 string that overwrite that glyph string because of their
23373 right overhang. For these strings, only the foreground must
23374 be drawn, because it draws over the glyph string at `head'.
23375 The background must not be drawn because this would overwrite
23376 right overhangs of preceding glyphs for which no glyph
23377 strings exist. */
23378 i = left_overwriting (head);
23379 if (i >= 0)
23380 {
23381 enum draw_glyphs_face overlap_hl;
23382
23383 if (check_mouse_face
23384 && mouse_beg_col < start && mouse_end_col > i)
23385 overlap_hl = DRAW_MOUSE_FACE;
23386 else
23387 overlap_hl = DRAW_NORMAL_TEXT;
23388
23389 clip_head = head;
23390 BUILD_GLYPH_STRINGS (i, start, h, t,
23391 overlap_hl, dummy_x, last_x);
23392 for (s = h; s; s = s->next)
23393 s->background_filled_p = 1;
23394 compute_overhangs_and_x (t, head->x, 1);
23395 prepend_glyph_string_lists (&head, &tail, h, t);
23396 }
23397
23398 /* Append glyphs strings for glyphs following the last glyph
23399 string tail that are overwritten by tail. The background of
23400 these strings has to be drawn because tail's foreground draws
23401 over it. */
23402 i = right_overwritten (tail);
23403 if (i >= 0)
23404 {
23405 enum draw_glyphs_face overlap_hl;
23406
23407 if (check_mouse_face
23408 && mouse_beg_col < i && mouse_end_col > end)
23409 overlap_hl = DRAW_MOUSE_FACE;
23410 else
23411 overlap_hl = DRAW_NORMAL_TEXT;
23412
23413 BUILD_GLYPH_STRINGS (end, i, h, t,
23414 overlap_hl, x, last_x);
23415 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23416 we don't have `end = i;' here. */
23417 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23418 append_glyph_string_lists (&head, &tail, h, t);
23419 clip_tail = tail;
23420 }
23421
23422 /* Append glyph strings for glyphs following the last glyph
23423 string tail that overwrite tail. The foreground of such
23424 glyphs has to be drawn because it writes into the background
23425 of tail. The background must not be drawn because it could
23426 paint over the foreground of following glyphs. */
23427 i = right_overwriting (tail);
23428 if (i >= 0)
23429 {
23430 enum draw_glyphs_face overlap_hl;
23431 if (check_mouse_face
23432 && mouse_beg_col < i && mouse_end_col > end)
23433 overlap_hl = DRAW_MOUSE_FACE;
23434 else
23435 overlap_hl = DRAW_NORMAL_TEXT;
23436
23437 clip_tail = tail;
23438 i++; /* We must include the Ith glyph. */
23439 BUILD_GLYPH_STRINGS (end, i, h, t,
23440 overlap_hl, x, last_x);
23441 for (s = h; s; s = s->next)
23442 s->background_filled_p = 1;
23443 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23444 append_glyph_string_lists (&head, &tail, h, t);
23445 }
23446 if (clip_head || clip_tail)
23447 for (s = head; s; s = s->next)
23448 {
23449 s->clip_head = clip_head;
23450 s->clip_tail = clip_tail;
23451 }
23452 }
23453
23454 /* Draw all strings. */
23455 for (s = head; s; s = s->next)
23456 FRAME_RIF (f)->draw_glyph_string (s);
23457
23458 #ifndef HAVE_NS
23459 /* When focus a sole frame and move horizontally, this sets on_p to 0
23460 causing a failure to erase prev cursor position. */
23461 if (area == TEXT_AREA
23462 && !row->full_width_p
23463 /* When drawing overlapping rows, only the glyph strings'
23464 foreground is drawn, which doesn't erase a cursor
23465 completely. */
23466 && !overlaps)
23467 {
23468 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23469 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23470 : (tail ? tail->x + tail->background_width : x));
23471 x0 -= area_left;
23472 x1 -= area_left;
23473
23474 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23475 row->y, MATRIX_ROW_BOTTOM_Y (row));
23476 }
23477 #endif
23478
23479 /* Value is the x-position up to which drawn, relative to AREA of W.
23480 This doesn't include parts drawn because of overhangs. */
23481 if (row->full_width_p)
23482 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23483 else
23484 x_reached -= area_left;
23485
23486 RELEASE_HDC (hdc, f);
23487
23488 return x_reached;
23489 }
23490
23491 /* Expand row matrix if too narrow. Don't expand if area
23492 is not present. */
23493
23494 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23495 { \
23496 if (!fonts_changed_p \
23497 && (it->glyph_row->glyphs[area] \
23498 < it->glyph_row->glyphs[area + 1])) \
23499 { \
23500 it->w->ncols_scale_factor++; \
23501 fonts_changed_p = 1; \
23502 } \
23503 }
23504
23505 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23506 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23507
23508 static void
23509 append_glyph (struct it *it)
23510 {
23511 struct glyph *glyph;
23512 enum glyph_row_area area = it->area;
23513
23514 eassert (it->glyph_row);
23515 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23516
23517 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23518 if (glyph < it->glyph_row->glyphs[area + 1])
23519 {
23520 /* If the glyph row is reversed, we need to prepend the glyph
23521 rather than append it. */
23522 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23523 {
23524 struct glyph *g;
23525
23526 /* Make room for the additional glyph. */
23527 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23528 g[1] = *g;
23529 glyph = it->glyph_row->glyphs[area];
23530 }
23531 glyph->charpos = CHARPOS (it->position);
23532 glyph->object = it->object;
23533 if (it->pixel_width > 0)
23534 {
23535 glyph->pixel_width = it->pixel_width;
23536 glyph->padding_p = 0;
23537 }
23538 else
23539 {
23540 /* Assure at least 1-pixel width. Otherwise, cursor can't
23541 be displayed correctly. */
23542 glyph->pixel_width = 1;
23543 glyph->padding_p = 1;
23544 }
23545 glyph->ascent = it->ascent;
23546 glyph->descent = it->descent;
23547 glyph->voffset = it->voffset;
23548 glyph->type = CHAR_GLYPH;
23549 glyph->avoid_cursor_p = it->avoid_cursor_p;
23550 glyph->multibyte_p = it->multibyte_p;
23551 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23552 {
23553 /* In R2L rows, the left and the right box edges need to be
23554 drawn in reverse direction. */
23555 glyph->right_box_line_p = it->start_of_box_run_p;
23556 glyph->left_box_line_p = it->end_of_box_run_p;
23557 }
23558 else
23559 {
23560 glyph->left_box_line_p = it->start_of_box_run_p;
23561 glyph->right_box_line_p = it->end_of_box_run_p;
23562 }
23563 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23564 || it->phys_descent > it->descent);
23565 glyph->glyph_not_available_p = it->glyph_not_available_p;
23566 glyph->face_id = it->face_id;
23567 glyph->u.ch = it->char_to_display;
23568 glyph->slice.img = null_glyph_slice;
23569 glyph->font_type = FONT_TYPE_UNKNOWN;
23570 if (it->bidi_p)
23571 {
23572 glyph->resolved_level = it->bidi_it.resolved_level;
23573 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23574 emacs_abort ();
23575 glyph->bidi_type = it->bidi_it.type;
23576 }
23577 else
23578 {
23579 glyph->resolved_level = 0;
23580 glyph->bidi_type = UNKNOWN_BT;
23581 }
23582 ++it->glyph_row->used[area];
23583 }
23584 else
23585 IT_EXPAND_MATRIX_WIDTH (it, area);
23586 }
23587
23588 /* Store one glyph for the composition IT->cmp_it.id in
23589 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23590 non-null. */
23591
23592 static void
23593 append_composite_glyph (struct it *it)
23594 {
23595 struct glyph *glyph;
23596 enum glyph_row_area area = it->area;
23597
23598 eassert (it->glyph_row);
23599
23600 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23601 if (glyph < it->glyph_row->glyphs[area + 1])
23602 {
23603 /* If the glyph row is reversed, we need to prepend the glyph
23604 rather than append it. */
23605 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23606 {
23607 struct glyph *g;
23608
23609 /* Make room for the new glyph. */
23610 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23611 g[1] = *g;
23612 glyph = it->glyph_row->glyphs[it->area];
23613 }
23614 glyph->charpos = it->cmp_it.charpos;
23615 glyph->object = it->object;
23616 glyph->pixel_width = it->pixel_width;
23617 glyph->ascent = it->ascent;
23618 glyph->descent = it->descent;
23619 glyph->voffset = it->voffset;
23620 glyph->type = COMPOSITE_GLYPH;
23621 if (it->cmp_it.ch < 0)
23622 {
23623 glyph->u.cmp.automatic = 0;
23624 glyph->u.cmp.id = it->cmp_it.id;
23625 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23626 }
23627 else
23628 {
23629 glyph->u.cmp.automatic = 1;
23630 glyph->u.cmp.id = it->cmp_it.id;
23631 glyph->slice.cmp.from = it->cmp_it.from;
23632 glyph->slice.cmp.to = it->cmp_it.to - 1;
23633 }
23634 glyph->avoid_cursor_p = it->avoid_cursor_p;
23635 glyph->multibyte_p = it->multibyte_p;
23636 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23637 {
23638 /* In R2L rows, the left and the right box edges need to be
23639 drawn in reverse direction. */
23640 glyph->right_box_line_p = it->start_of_box_run_p;
23641 glyph->left_box_line_p = it->end_of_box_run_p;
23642 }
23643 else
23644 {
23645 glyph->left_box_line_p = it->start_of_box_run_p;
23646 glyph->right_box_line_p = it->end_of_box_run_p;
23647 }
23648 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23649 || it->phys_descent > it->descent);
23650 glyph->padding_p = 0;
23651 glyph->glyph_not_available_p = 0;
23652 glyph->face_id = it->face_id;
23653 glyph->font_type = FONT_TYPE_UNKNOWN;
23654 if (it->bidi_p)
23655 {
23656 glyph->resolved_level = it->bidi_it.resolved_level;
23657 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23658 emacs_abort ();
23659 glyph->bidi_type = it->bidi_it.type;
23660 }
23661 ++it->glyph_row->used[area];
23662 }
23663 else
23664 IT_EXPAND_MATRIX_WIDTH (it, area);
23665 }
23666
23667
23668 /* Change IT->ascent and IT->height according to the setting of
23669 IT->voffset. */
23670
23671 static void
23672 take_vertical_position_into_account (struct it *it)
23673 {
23674 if (it->voffset)
23675 {
23676 if (it->voffset < 0)
23677 /* Increase the ascent so that we can display the text higher
23678 in the line. */
23679 it->ascent -= it->voffset;
23680 else
23681 /* Increase the descent so that we can display the text lower
23682 in the line. */
23683 it->descent += it->voffset;
23684 }
23685 }
23686
23687
23688 /* Produce glyphs/get display metrics for the image IT is loaded with.
23689 See the description of struct display_iterator in dispextern.h for
23690 an overview of struct display_iterator. */
23691
23692 static void
23693 produce_image_glyph (struct it *it)
23694 {
23695 struct image *img;
23696 struct face *face;
23697 int glyph_ascent, crop;
23698 struct glyph_slice slice;
23699
23700 eassert (it->what == IT_IMAGE);
23701
23702 face = FACE_FROM_ID (it->f, it->face_id);
23703 eassert (face);
23704 /* Make sure X resources of the face is loaded. */
23705 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23706
23707 if (it->image_id < 0)
23708 {
23709 /* Fringe bitmap. */
23710 it->ascent = it->phys_ascent = 0;
23711 it->descent = it->phys_descent = 0;
23712 it->pixel_width = 0;
23713 it->nglyphs = 0;
23714 return;
23715 }
23716
23717 img = IMAGE_FROM_ID (it->f, it->image_id);
23718 eassert (img);
23719 /* Make sure X resources of the image is loaded. */
23720 prepare_image_for_display (it->f, img);
23721
23722 slice.x = slice.y = 0;
23723 slice.width = img->width;
23724 slice.height = img->height;
23725
23726 if (INTEGERP (it->slice.x))
23727 slice.x = XINT (it->slice.x);
23728 else if (FLOATP (it->slice.x))
23729 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23730
23731 if (INTEGERP (it->slice.y))
23732 slice.y = XINT (it->slice.y);
23733 else if (FLOATP (it->slice.y))
23734 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23735
23736 if (INTEGERP (it->slice.width))
23737 slice.width = XINT (it->slice.width);
23738 else if (FLOATP (it->slice.width))
23739 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23740
23741 if (INTEGERP (it->slice.height))
23742 slice.height = XINT (it->slice.height);
23743 else if (FLOATP (it->slice.height))
23744 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23745
23746 if (slice.x >= img->width)
23747 slice.x = img->width;
23748 if (slice.y >= img->height)
23749 slice.y = img->height;
23750 if (slice.x + slice.width >= img->width)
23751 slice.width = img->width - slice.x;
23752 if (slice.y + slice.height > img->height)
23753 slice.height = img->height - slice.y;
23754
23755 if (slice.width == 0 || slice.height == 0)
23756 return;
23757
23758 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23759
23760 it->descent = slice.height - glyph_ascent;
23761 if (slice.y == 0)
23762 it->descent += img->vmargin;
23763 if (slice.y + slice.height == img->height)
23764 it->descent += img->vmargin;
23765 it->phys_descent = it->descent;
23766
23767 it->pixel_width = slice.width;
23768 if (slice.x == 0)
23769 it->pixel_width += img->hmargin;
23770 if (slice.x + slice.width == img->width)
23771 it->pixel_width += img->hmargin;
23772
23773 /* It's quite possible for images to have an ascent greater than
23774 their height, so don't get confused in that case. */
23775 if (it->descent < 0)
23776 it->descent = 0;
23777
23778 it->nglyphs = 1;
23779
23780 if (face->box != FACE_NO_BOX)
23781 {
23782 if (face->box_line_width > 0)
23783 {
23784 if (slice.y == 0)
23785 it->ascent += face->box_line_width;
23786 if (slice.y + slice.height == img->height)
23787 it->descent += face->box_line_width;
23788 }
23789
23790 if (it->start_of_box_run_p && slice.x == 0)
23791 it->pixel_width += eabs (face->box_line_width);
23792 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23793 it->pixel_width += eabs (face->box_line_width);
23794 }
23795
23796 take_vertical_position_into_account (it);
23797
23798 /* Automatically crop wide image glyphs at right edge so we can
23799 draw the cursor on same display row. */
23800 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23801 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23802 {
23803 it->pixel_width -= crop;
23804 slice.width -= crop;
23805 }
23806
23807 if (it->glyph_row)
23808 {
23809 struct glyph *glyph;
23810 enum glyph_row_area area = it->area;
23811
23812 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23813 if (glyph < it->glyph_row->glyphs[area + 1])
23814 {
23815 glyph->charpos = CHARPOS (it->position);
23816 glyph->object = it->object;
23817 glyph->pixel_width = it->pixel_width;
23818 glyph->ascent = glyph_ascent;
23819 glyph->descent = it->descent;
23820 glyph->voffset = it->voffset;
23821 glyph->type = IMAGE_GLYPH;
23822 glyph->avoid_cursor_p = it->avoid_cursor_p;
23823 glyph->multibyte_p = it->multibyte_p;
23824 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23825 {
23826 /* In R2L rows, the left and the right box edges need to be
23827 drawn in reverse direction. */
23828 glyph->right_box_line_p = it->start_of_box_run_p;
23829 glyph->left_box_line_p = it->end_of_box_run_p;
23830 }
23831 else
23832 {
23833 glyph->left_box_line_p = it->start_of_box_run_p;
23834 glyph->right_box_line_p = it->end_of_box_run_p;
23835 }
23836 glyph->overlaps_vertically_p = 0;
23837 glyph->padding_p = 0;
23838 glyph->glyph_not_available_p = 0;
23839 glyph->face_id = it->face_id;
23840 glyph->u.img_id = img->id;
23841 glyph->slice.img = slice;
23842 glyph->font_type = FONT_TYPE_UNKNOWN;
23843 if (it->bidi_p)
23844 {
23845 glyph->resolved_level = it->bidi_it.resolved_level;
23846 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23847 emacs_abort ();
23848 glyph->bidi_type = it->bidi_it.type;
23849 }
23850 ++it->glyph_row->used[area];
23851 }
23852 else
23853 IT_EXPAND_MATRIX_WIDTH (it, area);
23854 }
23855 }
23856
23857
23858 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23859 of the glyph, WIDTH and HEIGHT are the width and height of the
23860 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23861
23862 static void
23863 append_stretch_glyph (struct it *it, Lisp_Object object,
23864 int width, int height, int ascent)
23865 {
23866 struct glyph *glyph;
23867 enum glyph_row_area area = it->area;
23868
23869 eassert (ascent >= 0 && ascent <= height);
23870
23871 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23872 if (glyph < it->glyph_row->glyphs[area + 1])
23873 {
23874 /* If the glyph row is reversed, we need to prepend the glyph
23875 rather than append it. */
23876 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23877 {
23878 struct glyph *g;
23879
23880 /* Make room for the additional glyph. */
23881 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23882 g[1] = *g;
23883 glyph = it->glyph_row->glyphs[area];
23884 }
23885 glyph->charpos = CHARPOS (it->position);
23886 glyph->object = object;
23887 glyph->pixel_width = width;
23888 glyph->ascent = ascent;
23889 glyph->descent = height - ascent;
23890 glyph->voffset = it->voffset;
23891 glyph->type = STRETCH_GLYPH;
23892 glyph->avoid_cursor_p = it->avoid_cursor_p;
23893 glyph->multibyte_p = it->multibyte_p;
23894 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23895 {
23896 /* In R2L rows, the left and the right box edges need to be
23897 drawn in reverse direction. */
23898 glyph->right_box_line_p = it->start_of_box_run_p;
23899 glyph->left_box_line_p = it->end_of_box_run_p;
23900 }
23901 else
23902 {
23903 glyph->left_box_line_p = it->start_of_box_run_p;
23904 glyph->right_box_line_p = it->end_of_box_run_p;
23905 }
23906 glyph->overlaps_vertically_p = 0;
23907 glyph->padding_p = 0;
23908 glyph->glyph_not_available_p = 0;
23909 glyph->face_id = it->face_id;
23910 glyph->u.stretch.ascent = ascent;
23911 glyph->u.stretch.height = height;
23912 glyph->slice.img = null_glyph_slice;
23913 glyph->font_type = FONT_TYPE_UNKNOWN;
23914 if (it->bidi_p)
23915 {
23916 glyph->resolved_level = it->bidi_it.resolved_level;
23917 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23918 emacs_abort ();
23919 glyph->bidi_type = it->bidi_it.type;
23920 }
23921 else
23922 {
23923 glyph->resolved_level = 0;
23924 glyph->bidi_type = UNKNOWN_BT;
23925 }
23926 ++it->glyph_row->used[area];
23927 }
23928 else
23929 IT_EXPAND_MATRIX_WIDTH (it, area);
23930 }
23931
23932 #endif /* HAVE_WINDOW_SYSTEM */
23933
23934 /* Produce a stretch glyph for iterator IT. IT->object is the value
23935 of the glyph property displayed. The value must be a list
23936 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23937 being recognized:
23938
23939 1. `:width WIDTH' specifies that the space should be WIDTH *
23940 canonical char width wide. WIDTH may be an integer or floating
23941 point number.
23942
23943 2. `:relative-width FACTOR' specifies that the width of the stretch
23944 should be computed from the width of the first character having the
23945 `glyph' property, and should be FACTOR times that width.
23946
23947 3. `:align-to HPOS' specifies that the space should be wide enough
23948 to reach HPOS, a value in canonical character units.
23949
23950 Exactly one of the above pairs must be present.
23951
23952 4. `:height HEIGHT' specifies that the height of the stretch produced
23953 should be HEIGHT, measured in canonical character units.
23954
23955 5. `:relative-height FACTOR' specifies that the height of the
23956 stretch should be FACTOR times the height of the characters having
23957 the glyph property.
23958
23959 Either none or exactly one of 4 or 5 must be present.
23960
23961 6. `:ascent ASCENT' specifies that ASCENT percent of the height
23962 of the stretch should be used for the ascent of the stretch.
23963 ASCENT must be in the range 0 <= ASCENT <= 100. */
23964
23965 void
23966 produce_stretch_glyph (struct it *it)
23967 {
23968 /* (space :width WIDTH :height HEIGHT ...) */
23969 Lisp_Object prop, plist;
23970 int width = 0, height = 0, align_to = -1;
23971 int zero_width_ok_p = 0;
23972 double tem;
23973 struct font *font = NULL;
23974
23975 #ifdef HAVE_WINDOW_SYSTEM
23976 int ascent = 0;
23977 int zero_height_ok_p = 0;
23978
23979 if (FRAME_WINDOW_P (it->f))
23980 {
23981 struct face *face = FACE_FROM_ID (it->f, it->face_id);
23982 font = face->font ? face->font : FRAME_FONT (it->f);
23983 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23984 }
23985 #endif
23986
23987 /* List should start with `space'. */
23988 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
23989 plist = XCDR (it->object);
23990
23991 /* Compute the width of the stretch. */
23992 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
23993 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
23994 {
23995 /* Absolute width `:width WIDTH' specified and valid. */
23996 zero_width_ok_p = 1;
23997 width = (int)tem;
23998 }
23999 #ifdef HAVE_WINDOW_SYSTEM
24000 else if (FRAME_WINDOW_P (it->f)
24001 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24002 {
24003 /* Relative width `:relative-width FACTOR' specified and valid.
24004 Compute the width of the characters having the `glyph'
24005 property. */
24006 struct it it2;
24007 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24008
24009 it2 = *it;
24010 if (it->multibyte_p)
24011 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24012 else
24013 {
24014 it2.c = it2.char_to_display = *p, it2.len = 1;
24015 if (! ASCII_CHAR_P (it2.c))
24016 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24017 }
24018
24019 it2.glyph_row = NULL;
24020 it2.what = IT_CHARACTER;
24021 x_produce_glyphs (&it2);
24022 width = NUMVAL (prop) * it2.pixel_width;
24023 }
24024 #endif /* HAVE_WINDOW_SYSTEM */
24025 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24026 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24027 {
24028 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24029 align_to = (align_to < 0
24030 ? 0
24031 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24032 else if (align_to < 0)
24033 align_to = window_box_left_offset (it->w, TEXT_AREA);
24034 width = max (0, (int)tem + align_to - it->current_x);
24035 zero_width_ok_p = 1;
24036 }
24037 else
24038 /* Nothing specified -> width defaults to canonical char width. */
24039 width = FRAME_COLUMN_WIDTH (it->f);
24040
24041 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24042 width = 1;
24043
24044 #ifdef HAVE_WINDOW_SYSTEM
24045 /* Compute height. */
24046 if (FRAME_WINDOW_P (it->f))
24047 {
24048 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24049 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24050 {
24051 height = (int)tem;
24052 zero_height_ok_p = 1;
24053 }
24054 else if (prop = Fplist_get (plist, QCrelative_height),
24055 NUMVAL (prop) > 0)
24056 height = FONT_HEIGHT (font) * NUMVAL (prop);
24057 else
24058 height = FONT_HEIGHT (font);
24059
24060 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24061 height = 1;
24062
24063 /* Compute percentage of height used for ascent. If
24064 `:ascent ASCENT' is present and valid, use that. Otherwise,
24065 derive the ascent from the font in use. */
24066 if (prop = Fplist_get (plist, QCascent),
24067 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24068 ascent = height * NUMVAL (prop) / 100.0;
24069 else if (!NILP (prop)
24070 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24071 ascent = min (max (0, (int)tem), height);
24072 else
24073 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24074 }
24075 else
24076 #endif /* HAVE_WINDOW_SYSTEM */
24077 height = 1;
24078
24079 if (width > 0 && it->line_wrap != TRUNCATE
24080 && it->current_x + width > it->last_visible_x)
24081 {
24082 width = it->last_visible_x - it->current_x;
24083 #ifdef HAVE_WINDOW_SYSTEM
24084 /* Subtract one more pixel from the stretch width, but only on
24085 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24086 width -= FRAME_WINDOW_P (it->f);
24087 #endif
24088 }
24089
24090 if (width > 0 && height > 0 && it->glyph_row)
24091 {
24092 Lisp_Object o_object = it->object;
24093 Lisp_Object object = it->stack[it->sp - 1].string;
24094 int n = width;
24095
24096 if (!STRINGP (object))
24097 object = it->w->buffer;
24098 #ifdef HAVE_WINDOW_SYSTEM
24099 if (FRAME_WINDOW_P (it->f))
24100 append_stretch_glyph (it, object, width, height, ascent);
24101 else
24102 #endif
24103 {
24104 it->object = object;
24105 it->char_to_display = ' ';
24106 it->pixel_width = it->len = 1;
24107 while (n--)
24108 tty_append_glyph (it);
24109 it->object = o_object;
24110 }
24111 }
24112
24113 it->pixel_width = width;
24114 #ifdef HAVE_WINDOW_SYSTEM
24115 if (FRAME_WINDOW_P (it->f))
24116 {
24117 it->ascent = it->phys_ascent = ascent;
24118 it->descent = it->phys_descent = height - it->ascent;
24119 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24120 take_vertical_position_into_account (it);
24121 }
24122 else
24123 #endif
24124 it->nglyphs = width;
24125 }
24126
24127 /* Get information about special display element WHAT in an
24128 environment described by IT. WHAT is one of IT_TRUNCATION or
24129 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24130 non-null glyph_row member. This function ensures that fields like
24131 face_id, c, len of IT are left untouched. */
24132
24133 static void
24134 produce_special_glyphs (struct it *it, enum display_element_type what)
24135 {
24136 struct it temp_it;
24137 Lisp_Object gc;
24138 GLYPH glyph;
24139
24140 temp_it = *it;
24141 temp_it.object = make_number (0);
24142 memset (&temp_it.current, 0, sizeof temp_it.current);
24143
24144 if (what == IT_CONTINUATION)
24145 {
24146 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24147 if (it->bidi_it.paragraph_dir == R2L)
24148 SET_GLYPH_FROM_CHAR (glyph, '/');
24149 else
24150 SET_GLYPH_FROM_CHAR (glyph, '\\');
24151 if (it->dp
24152 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24153 {
24154 /* FIXME: Should we mirror GC for R2L lines? */
24155 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24156 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24157 }
24158 }
24159 else if (what == IT_TRUNCATION)
24160 {
24161 /* Truncation glyph. */
24162 SET_GLYPH_FROM_CHAR (glyph, '$');
24163 if (it->dp
24164 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24165 {
24166 /* FIXME: Should we mirror GC for R2L lines? */
24167 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24168 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24169 }
24170 }
24171 else
24172 emacs_abort ();
24173
24174 #ifdef HAVE_WINDOW_SYSTEM
24175 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24176 is turned off, we precede the truncation/continuation glyphs by a
24177 stretch glyph whose width is computed such that these special
24178 glyphs are aligned at the window margin, even when very different
24179 fonts are used in different glyph rows. */
24180 if (FRAME_WINDOW_P (temp_it.f)
24181 /* init_iterator calls this with it->glyph_row == NULL, and it
24182 wants only the pixel width of the truncation/continuation
24183 glyphs. */
24184 && temp_it.glyph_row
24185 /* insert_left_trunc_glyphs calls us at the beginning of the
24186 row, and it has its own calculation of the stretch glyph
24187 width. */
24188 && temp_it.glyph_row->used[TEXT_AREA] > 0
24189 && (temp_it.glyph_row->reversed_p
24190 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24191 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24192 {
24193 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24194
24195 if (stretch_width > 0)
24196 {
24197 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24198 struct font *font =
24199 face->font ? face->font : FRAME_FONT (temp_it.f);
24200 int stretch_ascent =
24201 (((temp_it.ascent + temp_it.descent)
24202 * FONT_BASE (font)) / FONT_HEIGHT (font));
24203
24204 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24205 temp_it.ascent + temp_it.descent,
24206 stretch_ascent);
24207 }
24208 }
24209 #endif
24210
24211 temp_it.dp = NULL;
24212 temp_it.what = IT_CHARACTER;
24213 temp_it.len = 1;
24214 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24215 temp_it.face_id = GLYPH_FACE (glyph);
24216 temp_it.len = CHAR_BYTES (temp_it.c);
24217
24218 PRODUCE_GLYPHS (&temp_it);
24219 it->pixel_width = temp_it.pixel_width;
24220 it->nglyphs = temp_it.pixel_width;
24221 }
24222
24223 #ifdef HAVE_WINDOW_SYSTEM
24224
24225 /* Calculate line-height and line-spacing properties.
24226 An integer value specifies explicit pixel value.
24227 A float value specifies relative value to current face height.
24228 A cons (float . face-name) specifies relative value to
24229 height of specified face font.
24230
24231 Returns height in pixels, or nil. */
24232
24233
24234 static Lisp_Object
24235 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24236 int boff, int override)
24237 {
24238 Lisp_Object face_name = Qnil;
24239 int ascent, descent, height;
24240
24241 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24242 return val;
24243
24244 if (CONSP (val))
24245 {
24246 face_name = XCAR (val);
24247 val = XCDR (val);
24248 if (!NUMBERP (val))
24249 val = make_number (1);
24250 if (NILP (face_name))
24251 {
24252 height = it->ascent + it->descent;
24253 goto scale;
24254 }
24255 }
24256
24257 if (NILP (face_name))
24258 {
24259 font = FRAME_FONT (it->f);
24260 boff = FRAME_BASELINE_OFFSET (it->f);
24261 }
24262 else if (EQ (face_name, Qt))
24263 {
24264 override = 0;
24265 }
24266 else
24267 {
24268 int face_id;
24269 struct face *face;
24270
24271 face_id = lookup_named_face (it->f, face_name, 0);
24272 if (face_id < 0)
24273 return make_number (-1);
24274
24275 face = FACE_FROM_ID (it->f, face_id);
24276 font = face->font;
24277 if (font == NULL)
24278 return make_number (-1);
24279 boff = font->baseline_offset;
24280 if (font->vertical_centering)
24281 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24282 }
24283
24284 ascent = FONT_BASE (font) + boff;
24285 descent = FONT_DESCENT (font) - boff;
24286
24287 if (override)
24288 {
24289 it->override_ascent = ascent;
24290 it->override_descent = descent;
24291 it->override_boff = boff;
24292 }
24293
24294 height = ascent + descent;
24295
24296 scale:
24297 if (FLOATP (val))
24298 height = (int)(XFLOAT_DATA (val) * height);
24299 else if (INTEGERP (val))
24300 height *= XINT (val);
24301
24302 return make_number (height);
24303 }
24304
24305
24306 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24307 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24308 and only if this is for a character for which no font was found.
24309
24310 If the display method (it->glyphless_method) is
24311 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24312 length of the acronym or the hexadecimal string, UPPER_XOFF and
24313 UPPER_YOFF are pixel offsets for the upper part of the string,
24314 LOWER_XOFF and LOWER_YOFF are for the lower part.
24315
24316 For the other display methods, LEN through LOWER_YOFF are zero. */
24317
24318 static void
24319 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24320 short upper_xoff, short upper_yoff,
24321 short lower_xoff, short lower_yoff)
24322 {
24323 struct glyph *glyph;
24324 enum glyph_row_area area = it->area;
24325
24326 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24327 if (glyph < it->glyph_row->glyphs[area + 1])
24328 {
24329 /* If the glyph row is reversed, we need to prepend the glyph
24330 rather than append it. */
24331 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24332 {
24333 struct glyph *g;
24334
24335 /* Make room for the additional glyph. */
24336 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24337 g[1] = *g;
24338 glyph = it->glyph_row->glyphs[area];
24339 }
24340 glyph->charpos = CHARPOS (it->position);
24341 glyph->object = it->object;
24342 glyph->pixel_width = it->pixel_width;
24343 glyph->ascent = it->ascent;
24344 glyph->descent = it->descent;
24345 glyph->voffset = it->voffset;
24346 glyph->type = GLYPHLESS_GLYPH;
24347 glyph->u.glyphless.method = it->glyphless_method;
24348 glyph->u.glyphless.for_no_font = for_no_font;
24349 glyph->u.glyphless.len = len;
24350 glyph->u.glyphless.ch = it->c;
24351 glyph->slice.glyphless.upper_xoff = upper_xoff;
24352 glyph->slice.glyphless.upper_yoff = upper_yoff;
24353 glyph->slice.glyphless.lower_xoff = lower_xoff;
24354 glyph->slice.glyphless.lower_yoff = lower_yoff;
24355 glyph->avoid_cursor_p = it->avoid_cursor_p;
24356 glyph->multibyte_p = it->multibyte_p;
24357 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24358 {
24359 /* In R2L rows, the left and the right box edges need to be
24360 drawn in reverse direction. */
24361 glyph->right_box_line_p = it->start_of_box_run_p;
24362 glyph->left_box_line_p = it->end_of_box_run_p;
24363 }
24364 else
24365 {
24366 glyph->left_box_line_p = it->start_of_box_run_p;
24367 glyph->right_box_line_p = it->end_of_box_run_p;
24368 }
24369 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24370 || it->phys_descent > it->descent);
24371 glyph->padding_p = 0;
24372 glyph->glyph_not_available_p = 0;
24373 glyph->face_id = face_id;
24374 glyph->font_type = FONT_TYPE_UNKNOWN;
24375 if (it->bidi_p)
24376 {
24377 glyph->resolved_level = it->bidi_it.resolved_level;
24378 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24379 emacs_abort ();
24380 glyph->bidi_type = it->bidi_it.type;
24381 }
24382 ++it->glyph_row->used[area];
24383 }
24384 else
24385 IT_EXPAND_MATRIX_WIDTH (it, area);
24386 }
24387
24388
24389 /* Produce a glyph for a glyphless character for iterator IT.
24390 IT->glyphless_method specifies which method to use for displaying
24391 the character. See the description of enum
24392 glyphless_display_method in dispextern.h for the detail.
24393
24394 FOR_NO_FONT is nonzero if and only if this is for a character for
24395 which no font was found. ACRONYM, if non-nil, is an acronym string
24396 for the character. */
24397
24398 static void
24399 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24400 {
24401 int face_id;
24402 struct face *face;
24403 struct font *font;
24404 int base_width, base_height, width, height;
24405 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24406 int len;
24407
24408 /* Get the metrics of the base font. We always refer to the current
24409 ASCII face. */
24410 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24411 font = face->font ? face->font : FRAME_FONT (it->f);
24412 it->ascent = FONT_BASE (font) + font->baseline_offset;
24413 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24414 base_height = it->ascent + it->descent;
24415 base_width = font->average_width;
24416
24417 /* Get a face ID for the glyph by utilizing a cache (the same way as
24418 done for `escape-glyph' in get_next_display_element). */
24419 if (it->f == last_glyphless_glyph_frame
24420 && it->face_id == last_glyphless_glyph_face_id)
24421 {
24422 face_id = last_glyphless_glyph_merged_face_id;
24423 }
24424 else
24425 {
24426 /* Merge the `glyphless-char' face into the current face. */
24427 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24428 last_glyphless_glyph_frame = it->f;
24429 last_glyphless_glyph_face_id = it->face_id;
24430 last_glyphless_glyph_merged_face_id = face_id;
24431 }
24432
24433 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24434 {
24435 it->pixel_width = THIN_SPACE_WIDTH;
24436 len = 0;
24437 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24438 }
24439 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24440 {
24441 width = CHAR_WIDTH (it->c);
24442 if (width == 0)
24443 width = 1;
24444 else if (width > 4)
24445 width = 4;
24446 it->pixel_width = base_width * width;
24447 len = 0;
24448 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24449 }
24450 else
24451 {
24452 char buf[7];
24453 const char *str;
24454 unsigned int code[6];
24455 int upper_len;
24456 int ascent, descent;
24457 struct font_metrics metrics_upper, metrics_lower;
24458
24459 face = FACE_FROM_ID (it->f, face_id);
24460 font = face->font ? face->font : FRAME_FONT (it->f);
24461 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24462
24463 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24464 {
24465 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24466 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24467 if (CONSP (acronym))
24468 acronym = XCAR (acronym);
24469 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24470 }
24471 else
24472 {
24473 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24474 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24475 str = buf;
24476 }
24477 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24478 code[len] = font->driver->encode_char (font, str[len]);
24479 upper_len = (len + 1) / 2;
24480 font->driver->text_extents (font, code, upper_len,
24481 &metrics_upper);
24482 font->driver->text_extents (font, code + upper_len, len - upper_len,
24483 &metrics_lower);
24484
24485
24486
24487 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24488 width = max (metrics_upper.width, metrics_lower.width) + 4;
24489 upper_xoff = upper_yoff = 2; /* the typical case */
24490 if (base_width >= width)
24491 {
24492 /* Align the upper to the left, the lower to the right. */
24493 it->pixel_width = base_width;
24494 lower_xoff = base_width - 2 - metrics_lower.width;
24495 }
24496 else
24497 {
24498 /* Center the shorter one. */
24499 it->pixel_width = width;
24500 if (metrics_upper.width >= metrics_lower.width)
24501 lower_xoff = (width - metrics_lower.width) / 2;
24502 else
24503 {
24504 /* FIXME: This code doesn't look right. It formerly was
24505 missing the "lower_xoff = 0;", which couldn't have
24506 been right since it left lower_xoff uninitialized. */
24507 lower_xoff = 0;
24508 upper_xoff = (width - metrics_upper.width) / 2;
24509 }
24510 }
24511
24512 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24513 top, bottom, and between upper and lower strings. */
24514 height = (metrics_upper.ascent + metrics_upper.descent
24515 + metrics_lower.ascent + metrics_lower.descent) + 5;
24516 /* Center vertically.
24517 H:base_height, D:base_descent
24518 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24519
24520 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24521 descent = D - H/2 + h/2;
24522 lower_yoff = descent - 2 - ld;
24523 upper_yoff = lower_yoff - la - 1 - ud; */
24524 ascent = - (it->descent - (base_height + height + 1) / 2);
24525 descent = it->descent - (base_height - height) / 2;
24526 lower_yoff = descent - 2 - metrics_lower.descent;
24527 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24528 - metrics_upper.descent);
24529 /* Don't make the height shorter than the base height. */
24530 if (height > base_height)
24531 {
24532 it->ascent = ascent;
24533 it->descent = descent;
24534 }
24535 }
24536
24537 it->phys_ascent = it->ascent;
24538 it->phys_descent = it->descent;
24539 if (it->glyph_row)
24540 append_glyphless_glyph (it, face_id, for_no_font, len,
24541 upper_xoff, upper_yoff,
24542 lower_xoff, lower_yoff);
24543 it->nglyphs = 1;
24544 take_vertical_position_into_account (it);
24545 }
24546
24547
24548 /* RIF:
24549 Produce glyphs/get display metrics for the display element IT is
24550 loaded with. See the description of struct it in dispextern.h
24551 for an overview of struct it. */
24552
24553 void
24554 x_produce_glyphs (struct it *it)
24555 {
24556 int extra_line_spacing = it->extra_line_spacing;
24557
24558 it->glyph_not_available_p = 0;
24559
24560 if (it->what == IT_CHARACTER)
24561 {
24562 XChar2b char2b;
24563 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24564 struct font *font = face->font;
24565 struct font_metrics *pcm = NULL;
24566 int boff; /* baseline offset */
24567
24568 if (font == NULL)
24569 {
24570 /* When no suitable font is found, display this character by
24571 the method specified in the first extra slot of
24572 Vglyphless_char_display. */
24573 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24574
24575 eassert (it->what == IT_GLYPHLESS);
24576 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24577 goto done;
24578 }
24579
24580 boff = font->baseline_offset;
24581 if (font->vertical_centering)
24582 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24583
24584 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24585 {
24586 int stretched_p;
24587
24588 it->nglyphs = 1;
24589
24590 if (it->override_ascent >= 0)
24591 {
24592 it->ascent = it->override_ascent;
24593 it->descent = it->override_descent;
24594 boff = it->override_boff;
24595 }
24596 else
24597 {
24598 it->ascent = FONT_BASE (font) + boff;
24599 it->descent = FONT_DESCENT (font) - boff;
24600 }
24601
24602 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24603 {
24604 pcm = get_per_char_metric (font, &char2b);
24605 if (pcm->width == 0
24606 && pcm->rbearing == 0 && pcm->lbearing == 0)
24607 pcm = NULL;
24608 }
24609
24610 if (pcm)
24611 {
24612 it->phys_ascent = pcm->ascent + boff;
24613 it->phys_descent = pcm->descent - boff;
24614 it->pixel_width = pcm->width;
24615 }
24616 else
24617 {
24618 it->glyph_not_available_p = 1;
24619 it->phys_ascent = it->ascent;
24620 it->phys_descent = it->descent;
24621 it->pixel_width = font->space_width;
24622 }
24623
24624 if (it->constrain_row_ascent_descent_p)
24625 {
24626 if (it->descent > it->max_descent)
24627 {
24628 it->ascent += it->descent - it->max_descent;
24629 it->descent = it->max_descent;
24630 }
24631 if (it->ascent > it->max_ascent)
24632 {
24633 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24634 it->ascent = it->max_ascent;
24635 }
24636 it->phys_ascent = min (it->phys_ascent, it->ascent);
24637 it->phys_descent = min (it->phys_descent, it->descent);
24638 extra_line_spacing = 0;
24639 }
24640
24641 /* If this is a space inside a region of text with
24642 `space-width' property, change its width. */
24643 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24644 if (stretched_p)
24645 it->pixel_width *= XFLOATINT (it->space_width);
24646
24647 /* If face has a box, add the box thickness to the character
24648 height. If character has a box line to the left and/or
24649 right, add the box line width to the character's width. */
24650 if (face->box != FACE_NO_BOX)
24651 {
24652 int thick = face->box_line_width;
24653
24654 if (thick > 0)
24655 {
24656 it->ascent += thick;
24657 it->descent += thick;
24658 }
24659 else
24660 thick = -thick;
24661
24662 if (it->start_of_box_run_p)
24663 it->pixel_width += thick;
24664 if (it->end_of_box_run_p)
24665 it->pixel_width += thick;
24666 }
24667
24668 /* If face has an overline, add the height of the overline
24669 (1 pixel) and a 1 pixel margin to the character height. */
24670 if (face->overline_p)
24671 it->ascent += overline_margin;
24672
24673 if (it->constrain_row_ascent_descent_p)
24674 {
24675 if (it->ascent > it->max_ascent)
24676 it->ascent = it->max_ascent;
24677 if (it->descent > it->max_descent)
24678 it->descent = it->max_descent;
24679 }
24680
24681 take_vertical_position_into_account (it);
24682
24683 /* If we have to actually produce glyphs, do it. */
24684 if (it->glyph_row)
24685 {
24686 if (stretched_p)
24687 {
24688 /* Translate a space with a `space-width' property
24689 into a stretch glyph. */
24690 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24691 / FONT_HEIGHT (font));
24692 append_stretch_glyph (it, it->object, it->pixel_width,
24693 it->ascent + it->descent, ascent);
24694 }
24695 else
24696 append_glyph (it);
24697
24698 /* If characters with lbearing or rbearing are displayed
24699 in this line, record that fact in a flag of the
24700 glyph row. This is used to optimize X output code. */
24701 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24702 it->glyph_row->contains_overlapping_glyphs_p = 1;
24703 }
24704 if (! stretched_p && it->pixel_width == 0)
24705 /* We assure that all visible glyphs have at least 1-pixel
24706 width. */
24707 it->pixel_width = 1;
24708 }
24709 else if (it->char_to_display == '\n')
24710 {
24711 /* A newline has no width, but we need the height of the
24712 line. But if previous part of the line sets a height,
24713 don't increase that height */
24714
24715 Lisp_Object height;
24716 Lisp_Object total_height = Qnil;
24717
24718 it->override_ascent = -1;
24719 it->pixel_width = 0;
24720 it->nglyphs = 0;
24721
24722 height = get_it_property (it, Qline_height);
24723 /* Split (line-height total-height) list */
24724 if (CONSP (height)
24725 && CONSP (XCDR (height))
24726 && NILP (XCDR (XCDR (height))))
24727 {
24728 total_height = XCAR (XCDR (height));
24729 height = XCAR (height);
24730 }
24731 height = calc_line_height_property (it, height, font, boff, 1);
24732
24733 if (it->override_ascent >= 0)
24734 {
24735 it->ascent = it->override_ascent;
24736 it->descent = it->override_descent;
24737 boff = it->override_boff;
24738 }
24739 else
24740 {
24741 it->ascent = FONT_BASE (font) + boff;
24742 it->descent = FONT_DESCENT (font) - boff;
24743 }
24744
24745 if (EQ (height, Qt))
24746 {
24747 if (it->descent > it->max_descent)
24748 {
24749 it->ascent += it->descent - it->max_descent;
24750 it->descent = it->max_descent;
24751 }
24752 if (it->ascent > it->max_ascent)
24753 {
24754 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24755 it->ascent = it->max_ascent;
24756 }
24757 it->phys_ascent = min (it->phys_ascent, it->ascent);
24758 it->phys_descent = min (it->phys_descent, it->descent);
24759 it->constrain_row_ascent_descent_p = 1;
24760 extra_line_spacing = 0;
24761 }
24762 else
24763 {
24764 Lisp_Object spacing;
24765
24766 it->phys_ascent = it->ascent;
24767 it->phys_descent = it->descent;
24768
24769 if ((it->max_ascent > 0 || it->max_descent > 0)
24770 && face->box != FACE_NO_BOX
24771 && face->box_line_width > 0)
24772 {
24773 it->ascent += face->box_line_width;
24774 it->descent += face->box_line_width;
24775 }
24776 if (!NILP (height)
24777 && XINT (height) > it->ascent + it->descent)
24778 it->ascent = XINT (height) - it->descent;
24779
24780 if (!NILP (total_height))
24781 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24782 else
24783 {
24784 spacing = get_it_property (it, Qline_spacing);
24785 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24786 }
24787 if (INTEGERP (spacing))
24788 {
24789 extra_line_spacing = XINT (spacing);
24790 if (!NILP (total_height))
24791 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24792 }
24793 }
24794 }
24795 else /* i.e. (it->char_to_display == '\t') */
24796 {
24797 if (font->space_width > 0)
24798 {
24799 int tab_width = it->tab_width * font->space_width;
24800 int x = it->current_x + it->continuation_lines_width;
24801 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24802
24803 /* If the distance from the current position to the next tab
24804 stop is less than a space character width, use the
24805 tab stop after that. */
24806 if (next_tab_x - x < font->space_width)
24807 next_tab_x += tab_width;
24808
24809 it->pixel_width = next_tab_x - x;
24810 it->nglyphs = 1;
24811 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24812 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24813
24814 if (it->glyph_row)
24815 {
24816 append_stretch_glyph (it, it->object, it->pixel_width,
24817 it->ascent + it->descent, it->ascent);
24818 }
24819 }
24820 else
24821 {
24822 it->pixel_width = 0;
24823 it->nglyphs = 1;
24824 }
24825 }
24826 }
24827 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24828 {
24829 /* A static composition.
24830
24831 Note: A composition is represented as one glyph in the
24832 glyph matrix. There are no padding glyphs.
24833
24834 Important note: pixel_width, ascent, and descent are the
24835 values of what is drawn by draw_glyphs (i.e. the values of
24836 the overall glyphs composed). */
24837 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24838 int boff; /* baseline offset */
24839 struct composition *cmp = composition_table[it->cmp_it.id];
24840 int glyph_len = cmp->glyph_len;
24841 struct font *font = face->font;
24842
24843 it->nglyphs = 1;
24844
24845 /* If we have not yet calculated pixel size data of glyphs of
24846 the composition for the current face font, calculate them
24847 now. Theoretically, we have to check all fonts for the
24848 glyphs, but that requires much time and memory space. So,
24849 here we check only the font of the first glyph. This may
24850 lead to incorrect display, but it's very rare, and C-l
24851 (recenter-top-bottom) can correct the display anyway. */
24852 if (! cmp->font || cmp->font != font)
24853 {
24854 /* Ascent and descent of the font of the first character
24855 of this composition (adjusted by baseline offset).
24856 Ascent and descent of overall glyphs should not be less
24857 than these, respectively. */
24858 int font_ascent, font_descent, font_height;
24859 /* Bounding box of the overall glyphs. */
24860 int leftmost, rightmost, lowest, highest;
24861 int lbearing, rbearing;
24862 int i, width, ascent, descent;
24863 int left_padded = 0, right_padded = 0;
24864 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24865 XChar2b char2b;
24866 struct font_metrics *pcm;
24867 int font_not_found_p;
24868 ptrdiff_t pos;
24869
24870 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24871 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24872 break;
24873 if (glyph_len < cmp->glyph_len)
24874 right_padded = 1;
24875 for (i = 0; i < glyph_len; i++)
24876 {
24877 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24878 break;
24879 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24880 }
24881 if (i > 0)
24882 left_padded = 1;
24883
24884 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24885 : IT_CHARPOS (*it));
24886 /* If no suitable font is found, use the default font. */
24887 font_not_found_p = font == NULL;
24888 if (font_not_found_p)
24889 {
24890 face = face->ascii_face;
24891 font = face->font;
24892 }
24893 boff = font->baseline_offset;
24894 if (font->vertical_centering)
24895 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24896 font_ascent = FONT_BASE (font) + boff;
24897 font_descent = FONT_DESCENT (font) - boff;
24898 font_height = FONT_HEIGHT (font);
24899
24900 cmp->font = font;
24901
24902 pcm = NULL;
24903 if (! font_not_found_p)
24904 {
24905 get_char_face_and_encoding (it->f, c, it->face_id,
24906 &char2b, 0);
24907 pcm = get_per_char_metric (font, &char2b);
24908 }
24909
24910 /* Initialize the bounding box. */
24911 if (pcm)
24912 {
24913 width = cmp->glyph_len > 0 ? pcm->width : 0;
24914 ascent = pcm->ascent;
24915 descent = pcm->descent;
24916 lbearing = pcm->lbearing;
24917 rbearing = pcm->rbearing;
24918 }
24919 else
24920 {
24921 width = cmp->glyph_len > 0 ? font->space_width : 0;
24922 ascent = FONT_BASE (font);
24923 descent = FONT_DESCENT (font);
24924 lbearing = 0;
24925 rbearing = width;
24926 }
24927
24928 rightmost = width;
24929 leftmost = 0;
24930 lowest = - descent + boff;
24931 highest = ascent + boff;
24932
24933 if (! font_not_found_p
24934 && font->default_ascent
24935 && CHAR_TABLE_P (Vuse_default_ascent)
24936 && !NILP (Faref (Vuse_default_ascent,
24937 make_number (it->char_to_display))))
24938 highest = font->default_ascent + boff;
24939
24940 /* Draw the first glyph at the normal position. It may be
24941 shifted to right later if some other glyphs are drawn
24942 at the left. */
24943 cmp->offsets[i * 2] = 0;
24944 cmp->offsets[i * 2 + 1] = boff;
24945 cmp->lbearing = lbearing;
24946 cmp->rbearing = rbearing;
24947
24948 /* Set cmp->offsets for the remaining glyphs. */
24949 for (i++; i < glyph_len; i++)
24950 {
24951 int left, right, btm, top;
24952 int ch = COMPOSITION_GLYPH (cmp, i);
24953 int face_id;
24954 struct face *this_face;
24955
24956 if (ch == '\t')
24957 ch = ' ';
24958 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
24959 this_face = FACE_FROM_ID (it->f, face_id);
24960 font = this_face->font;
24961
24962 if (font == NULL)
24963 pcm = NULL;
24964 else
24965 {
24966 get_char_face_and_encoding (it->f, ch, face_id,
24967 &char2b, 0);
24968 pcm = get_per_char_metric (font, &char2b);
24969 }
24970 if (! pcm)
24971 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24972 else
24973 {
24974 width = pcm->width;
24975 ascent = pcm->ascent;
24976 descent = pcm->descent;
24977 lbearing = pcm->lbearing;
24978 rbearing = pcm->rbearing;
24979 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
24980 {
24981 /* Relative composition with or without
24982 alternate chars. */
24983 left = (leftmost + rightmost - width) / 2;
24984 btm = - descent + boff;
24985 if (font->relative_compose
24986 && (! CHAR_TABLE_P (Vignore_relative_composition)
24987 || NILP (Faref (Vignore_relative_composition,
24988 make_number (ch)))))
24989 {
24990
24991 if (- descent >= font->relative_compose)
24992 /* One extra pixel between two glyphs. */
24993 btm = highest + 1;
24994 else if (ascent <= 0)
24995 /* One extra pixel between two glyphs. */
24996 btm = lowest - 1 - ascent - descent;
24997 }
24998 }
24999 else
25000 {
25001 /* A composition rule is specified by an integer
25002 value that encodes global and new reference
25003 points (GREF and NREF). GREF and NREF are
25004 specified by numbers as below:
25005
25006 0---1---2 -- ascent
25007 | |
25008 | |
25009 | |
25010 9--10--11 -- center
25011 | |
25012 ---3---4---5--- baseline
25013 | |
25014 6---7---8 -- descent
25015 */
25016 int rule = COMPOSITION_RULE (cmp, i);
25017 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25018
25019 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25020 grefx = gref % 3, nrefx = nref % 3;
25021 grefy = gref / 3, nrefy = nref / 3;
25022 if (xoff)
25023 xoff = font_height * (xoff - 128) / 256;
25024 if (yoff)
25025 yoff = font_height * (yoff - 128) / 256;
25026
25027 left = (leftmost
25028 + grefx * (rightmost - leftmost) / 2
25029 - nrefx * width / 2
25030 + xoff);
25031
25032 btm = ((grefy == 0 ? highest
25033 : grefy == 1 ? 0
25034 : grefy == 2 ? lowest
25035 : (highest + lowest) / 2)
25036 - (nrefy == 0 ? ascent + descent
25037 : nrefy == 1 ? descent - boff
25038 : nrefy == 2 ? 0
25039 : (ascent + descent) / 2)
25040 + yoff);
25041 }
25042
25043 cmp->offsets[i * 2] = left;
25044 cmp->offsets[i * 2 + 1] = btm + descent;
25045
25046 /* Update the bounding box of the overall glyphs. */
25047 if (width > 0)
25048 {
25049 right = left + width;
25050 if (left < leftmost)
25051 leftmost = left;
25052 if (right > rightmost)
25053 rightmost = right;
25054 }
25055 top = btm + descent + ascent;
25056 if (top > highest)
25057 highest = top;
25058 if (btm < lowest)
25059 lowest = btm;
25060
25061 if (cmp->lbearing > left + lbearing)
25062 cmp->lbearing = left + lbearing;
25063 if (cmp->rbearing < left + rbearing)
25064 cmp->rbearing = left + rbearing;
25065 }
25066 }
25067
25068 /* If there are glyphs whose x-offsets are negative,
25069 shift all glyphs to the right and make all x-offsets
25070 non-negative. */
25071 if (leftmost < 0)
25072 {
25073 for (i = 0; i < cmp->glyph_len; i++)
25074 cmp->offsets[i * 2] -= leftmost;
25075 rightmost -= leftmost;
25076 cmp->lbearing -= leftmost;
25077 cmp->rbearing -= leftmost;
25078 }
25079
25080 if (left_padded && cmp->lbearing < 0)
25081 {
25082 for (i = 0; i < cmp->glyph_len; i++)
25083 cmp->offsets[i * 2] -= cmp->lbearing;
25084 rightmost -= cmp->lbearing;
25085 cmp->rbearing -= cmp->lbearing;
25086 cmp->lbearing = 0;
25087 }
25088 if (right_padded && rightmost < cmp->rbearing)
25089 {
25090 rightmost = cmp->rbearing;
25091 }
25092
25093 cmp->pixel_width = rightmost;
25094 cmp->ascent = highest;
25095 cmp->descent = - lowest;
25096 if (cmp->ascent < font_ascent)
25097 cmp->ascent = font_ascent;
25098 if (cmp->descent < font_descent)
25099 cmp->descent = font_descent;
25100 }
25101
25102 if (it->glyph_row
25103 && (cmp->lbearing < 0
25104 || cmp->rbearing > cmp->pixel_width))
25105 it->glyph_row->contains_overlapping_glyphs_p = 1;
25106
25107 it->pixel_width = cmp->pixel_width;
25108 it->ascent = it->phys_ascent = cmp->ascent;
25109 it->descent = it->phys_descent = cmp->descent;
25110 if (face->box != FACE_NO_BOX)
25111 {
25112 int thick = face->box_line_width;
25113
25114 if (thick > 0)
25115 {
25116 it->ascent += thick;
25117 it->descent += thick;
25118 }
25119 else
25120 thick = - thick;
25121
25122 if (it->start_of_box_run_p)
25123 it->pixel_width += thick;
25124 if (it->end_of_box_run_p)
25125 it->pixel_width += thick;
25126 }
25127
25128 /* If face has an overline, add the height of the overline
25129 (1 pixel) and a 1 pixel margin to the character height. */
25130 if (face->overline_p)
25131 it->ascent += overline_margin;
25132
25133 take_vertical_position_into_account (it);
25134 if (it->ascent < 0)
25135 it->ascent = 0;
25136 if (it->descent < 0)
25137 it->descent = 0;
25138
25139 if (it->glyph_row && cmp->glyph_len > 0)
25140 append_composite_glyph (it);
25141 }
25142 else if (it->what == IT_COMPOSITION)
25143 {
25144 /* A dynamic (automatic) composition. */
25145 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25146 Lisp_Object gstring;
25147 struct font_metrics metrics;
25148
25149 it->nglyphs = 1;
25150
25151 gstring = composition_gstring_from_id (it->cmp_it.id);
25152 it->pixel_width
25153 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25154 &metrics);
25155 if (it->glyph_row
25156 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25157 it->glyph_row->contains_overlapping_glyphs_p = 1;
25158 it->ascent = it->phys_ascent = metrics.ascent;
25159 it->descent = it->phys_descent = metrics.descent;
25160 if (face->box != FACE_NO_BOX)
25161 {
25162 int thick = face->box_line_width;
25163
25164 if (thick > 0)
25165 {
25166 it->ascent += thick;
25167 it->descent += thick;
25168 }
25169 else
25170 thick = - thick;
25171
25172 if (it->start_of_box_run_p)
25173 it->pixel_width += thick;
25174 if (it->end_of_box_run_p)
25175 it->pixel_width += thick;
25176 }
25177 /* If face has an overline, add the height of the overline
25178 (1 pixel) and a 1 pixel margin to the character height. */
25179 if (face->overline_p)
25180 it->ascent += overline_margin;
25181 take_vertical_position_into_account (it);
25182 if (it->ascent < 0)
25183 it->ascent = 0;
25184 if (it->descent < 0)
25185 it->descent = 0;
25186
25187 if (it->glyph_row)
25188 append_composite_glyph (it);
25189 }
25190 else if (it->what == IT_GLYPHLESS)
25191 produce_glyphless_glyph (it, 0, Qnil);
25192 else if (it->what == IT_IMAGE)
25193 produce_image_glyph (it);
25194 else if (it->what == IT_STRETCH)
25195 produce_stretch_glyph (it);
25196
25197 done:
25198 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25199 because this isn't true for images with `:ascent 100'. */
25200 eassert (it->ascent >= 0 && it->descent >= 0);
25201 if (it->area == TEXT_AREA)
25202 it->current_x += it->pixel_width;
25203
25204 if (extra_line_spacing > 0)
25205 {
25206 it->descent += extra_line_spacing;
25207 if (extra_line_spacing > it->max_extra_line_spacing)
25208 it->max_extra_line_spacing = extra_line_spacing;
25209 }
25210
25211 it->max_ascent = max (it->max_ascent, it->ascent);
25212 it->max_descent = max (it->max_descent, it->descent);
25213 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25214 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25215 }
25216
25217 /* EXPORT for RIF:
25218 Output LEN glyphs starting at START at the nominal cursor position.
25219 Advance the nominal cursor over the text. The global variable
25220 updated_window contains the window being updated, updated_row is
25221 the glyph row being updated, and updated_area is the area of that
25222 row being updated. */
25223
25224 void
25225 x_write_glyphs (struct glyph *start, int len)
25226 {
25227 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25228
25229 eassert (updated_window && updated_row);
25230 /* When the window is hscrolled, cursor hpos can legitimately be out
25231 of bounds, but we draw the cursor at the corresponding window
25232 margin in that case. */
25233 if (!updated_row->reversed_p && chpos < 0)
25234 chpos = 0;
25235 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25236 chpos = updated_row->used[TEXT_AREA] - 1;
25237
25238 block_input ();
25239
25240 /* Write glyphs. */
25241
25242 hpos = start - updated_row->glyphs[updated_area];
25243 x = draw_glyphs (updated_window, output_cursor.x,
25244 updated_row, updated_area,
25245 hpos, hpos + len,
25246 DRAW_NORMAL_TEXT, 0);
25247
25248 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25249 if (updated_area == TEXT_AREA
25250 && updated_window->phys_cursor_on_p
25251 && updated_window->phys_cursor.vpos == output_cursor.vpos
25252 && chpos >= hpos
25253 && chpos < hpos + len)
25254 updated_window->phys_cursor_on_p = 0;
25255
25256 unblock_input ();
25257
25258 /* Advance the output cursor. */
25259 output_cursor.hpos += len;
25260 output_cursor.x = x;
25261 }
25262
25263
25264 /* EXPORT for RIF:
25265 Insert LEN glyphs from START at the nominal cursor position. */
25266
25267 void
25268 x_insert_glyphs (struct glyph *start, int len)
25269 {
25270 struct frame *f;
25271 struct window *w;
25272 int line_height, shift_by_width, shifted_region_width;
25273 struct glyph_row *row;
25274 struct glyph *glyph;
25275 int frame_x, frame_y;
25276 ptrdiff_t hpos;
25277
25278 eassert (updated_window && updated_row);
25279 block_input ();
25280 w = updated_window;
25281 f = XFRAME (WINDOW_FRAME (w));
25282
25283 /* Get the height of the line we are in. */
25284 row = updated_row;
25285 line_height = row->height;
25286
25287 /* Get the width of the glyphs to insert. */
25288 shift_by_width = 0;
25289 for (glyph = start; glyph < start + len; ++glyph)
25290 shift_by_width += glyph->pixel_width;
25291
25292 /* Get the width of the region to shift right. */
25293 shifted_region_width = (window_box_width (w, updated_area)
25294 - output_cursor.x
25295 - shift_by_width);
25296
25297 /* Shift right. */
25298 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25299 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25300
25301 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25302 line_height, shift_by_width);
25303
25304 /* Write the glyphs. */
25305 hpos = start - row->glyphs[updated_area];
25306 draw_glyphs (w, output_cursor.x, row, updated_area,
25307 hpos, hpos + len,
25308 DRAW_NORMAL_TEXT, 0);
25309
25310 /* Advance the output cursor. */
25311 output_cursor.hpos += len;
25312 output_cursor.x += shift_by_width;
25313 unblock_input ();
25314 }
25315
25316
25317 /* EXPORT for RIF:
25318 Erase the current text line from the nominal cursor position
25319 (inclusive) to pixel column TO_X (exclusive). The idea is that
25320 everything from TO_X onward is already erased.
25321
25322 TO_X is a pixel position relative to updated_area of
25323 updated_window. TO_X == -1 means clear to the end of this area. */
25324
25325 void
25326 x_clear_end_of_line (int to_x)
25327 {
25328 struct frame *f;
25329 struct window *w = updated_window;
25330 int max_x, min_y, max_y;
25331 int from_x, from_y, to_y;
25332
25333 eassert (updated_window && updated_row);
25334 f = XFRAME (w->frame);
25335
25336 if (updated_row->full_width_p)
25337 max_x = WINDOW_TOTAL_WIDTH (w);
25338 else
25339 max_x = window_box_width (w, updated_area);
25340 max_y = window_text_bottom_y (w);
25341
25342 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25343 of window. For TO_X > 0, truncate to end of drawing area. */
25344 if (to_x == 0)
25345 return;
25346 else if (to_x < 0)
25347 to_x = max_x;
25348 else
25349 to_x = min (to_x, max_x);
25350
25351 to_y = min (max_y, output_cursor.y + updated_row->height);
25352
25353 /* Notice if the cursor will be cleared by this operation. */
25354 if (!updated_row->full_width_p)
25355 notice_overwritten_cursor (w, updated_area,
25356 output_cursor.x, -1,
25357 updated_row->y,
25358 MATRIX_ROW_BOTTOM_Y (updated_row));
25359
25360 from_x = output_cursor.x;
25361
25362 /* Translate to frame coordinates. */
25363 if (updated_row->full_width_p)
25364 {
25365 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25366 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25367 }
25368 else
25369 {
25370 int area_left = window_box_left (w, updated_area);
25371 from_x += area_left;
25372 to_x += area_left;
25373 }
25374
25375 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25376 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25377 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25378
25379 /* Prevent inadvertently clearing to end of the X window. */
25380 if (to_x > from_x && to_y > from_y)
25381 {
25382 block_input ();
25383 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25384 to_x - from_x, to_y - from_y);
25385 unblock_input ();
25386 }
25387 }
25388
25389 #endif /* HAVE_WINDOW_SYSTEM */
25390
25391
25392 \f
25393 /***********************************************************************
25394 Cursor types
25395 ***********************************************************************/
25396
25397 /* Value is the internal representation of the specified cursor type
25398 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25399 of the bar cursor. */
25400
25401 static enum text_cursor_kinds
25402 get_specified_cursor_type (Lisp_Object arg, int *width)
25403 {
25404 enum text_cursor_kinds type;
25405
25406 if (NILP (arg))
25407 return NO_CURSOR;
25408
25409 if (EQ (arg, Qbox))
25410 return FILLED_BOX_CURSOR;
25411
25412 if (EQ (arg, Qhollow))
25413 return HOLLOW_BOX_CURSOR;
25414
25415 if (EQ (arg, Qbar))
25416 {
25417 *width = 2;
25418 return BAR_CURSOR;
25419 }
25420
25421 if (CONSP (arg)
25422 && EQ (XCAR (arg), Qbar)
25423 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25424 {
25425 *width = XINT (XCDR (arg));
25426 return BAR_CURSOR;
25427 }
25428
25429 if (EQ (arg, Qhbar))
25430 {
25431 *width = 2;
25432 return HBAR_CURSOR;
25433 }
25434
25435 if (CONSP (arg)
25436 && EQ (XCAR (arg), Qhbar)
25437 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25438 {
25439 *width = XINT (XCDR (arg));
25440 return HBAR_CURSOR;
25441 }
25442
25443 /* Treat anything unknown as "hollow box cursor".
25444 It was bad to signal an error; people have trouble fixing
25445 .Xdefaults with Emacs, when it has something bad in it. */
25446 type = HOLLOW_BOX_CURSOR;
25447
25448 return type;
25449 }
25450
25451 /* Set the default cursor types for specified frame. */
25452 void
25453 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25454 {
25455 int width = 1;
25456 Lisp_Object tem;
25457
25458 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25459 FRAME_CURSOR_WIDTH (f) = width;
25460
25461 /* By default, set up the blink-off state depending on the on-state. */
25462
25463 tem = Fassoc (arg, Vblink_cursor_alist);
25464 if (!NILP (tem))
25465 {
25466 FRAME_BLINK_OFF_CURSOR (f)
25467 = get_specified_cursor_type (XCDR (tem), &width);
25468 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25469 }
25470 else
25471 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25472 }
25473
25474
25475 #ifdef HAVE_WINDOW_SYSTEM
25476
25477 /* Return the cursor we want to be displayed in window W. Return
25478 width of bar/hbar cursor through WIDTH arg. Return with
25479 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25480 (i.e. if the `system caret' should track this cursor).
25481
25482 In a mini-buffer window, we want the cursor only to appear if we
25483 are reading input from this window. For the selected window, we
25484 want the cursor type given by the frame parameter or buffer local
25485 setting of cursor-type. If explicitly marked off, draw no cursor.
25486 In all other cases, we want a hollow box cursor. */
25487
25488 static enum text_cursor_kinds
25489 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25490 int *active_cursor)
25491 {
25492 struct frame *f = XFRAME (w->frame);
25493 struct buffer *b = XBUFFER (w->buffer);
25494 int cursor_type = DEFAULT_CURSOR;
25495 Lisp_Object alt_cursor;
25496 int non_selected = 0;
25497
25498 *active_cursor = 1;
25499
25500 /* Echo area */
25501 if (cursor_in_echo_area
25502 && FRAME_HAS_MINIBUF_P (f)
25503 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25504 {
25505 if (w == XWINDOW (echo_area_window))
25506 {
25507 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25508 {
25509 *width = FRAME_CURSOR_WIDTH (f);
25510 return FRAME_DESIRED_CURSOR (f);
25511 }
25512 else
25513 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25514 }
25515
25516 *active_cursor = 0;
25517 non_selected = 1;
25518 }
25519
25520 /* Detect a nonselected window or nonselected frame. */
25521 else if (w != XWINDOW (f->selected_window)
25522 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25523 {
25524 *active_cursor = 0;
25525
25526 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25527 return NO_CURSOR;
25528
25529 non_selected = 1;
25530 }
25531
25532 /* Never display a cursor in a window in which cursor-type is nil. */
25533 if (NILP (BVAR (b, cursor_type)))
25534 return NO_CURSOR;
25535
25536 /* Get the normal cursor type for this window. */
25537 if (EQ (BVAR (b, cursor_type), Qt))
25538 {
25539 cursor_type = FRAME_DESIRED_CURSOR (f);
25540 *width = FRAME_CURSOR_WIDTH (f);
25541 }
25542 else
25543 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25544
25545 /* Use cursor-in-non-selected-windows instead
25546 for non-selected window or frame. */
25547 if (non_selected)
25548 {
25549 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25550 if (!EQ (Qt, alt_cursor))
25551 return get_specified_cursor_type (alt_cursor, width);
25552 /* t means modify the normal cursor type. */
25553 if (cursor_type == FILLED_BOX_CURSOR)
25554 cursor_type = HOLLOW_BOX_CURSOR;
25555 else if (cursor_type == BAR_CURSOR && *width > 1)
25556 --*width;
25557 return cursor_type;
25558 }
25559
25560 /* Use normal cursor if not blinked off. */
25561 if (!w->cursor_off_p)
25562 {
25563 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25564 {
25565 if (cursor_type == FILLED_BOX_CURSOR)
25566 {
25567 /* Using a block cursor on large images can be very annoying.
25568 So use a hollow cursor for "large" images.
25569 If image is not transparent (no mask), also use hollow cursor. */
25570 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25571 if (img != NULL && IMAGEP (img->spec))
25572 {
25573 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25574 where N = size of default frame font size.
25575 This should cover most of the "tiny" icons people may use. */
25576 if (!img->mask
25577 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25578 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25579 cursor_type = HOLLOW_BOX_CURSOR;
25580 }
25581 }
25582 else if (cursor_type != NO_CURSOR)
25583 {
25584 /* Display current only supports BOX and HOLLOW cursors for images.
25585 So for now, unconditionally use a HOLLOW cursor when cursor is
25586 not a solid box cursor. */
25587 cursor_type = HOLLOW_BOX_CURSOR;
25588 }
25589 }
25590 return cursor_type;
25591 }
25592
25593 /* Cursor is blinked off, so determine how to "toggle" it. */
25594
25595 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25596 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25597 return get_specified_cursor_type (XCDR (alt_cursor), width);
25598
25599 /* Then see if frame has specified a specific blink off cursor type. */
25600 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25601 {
25602 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25603 return FRAME_BLINK_OFF_CURSOR (f);
25604 }
25605
25606 #if 0
25607 /* Some people liked having a permanently visible blinking cursor,
25608 while others had very strong opinions against it. So it was
25609 decided to remove it. KFS 2003-09-03 */
25610
25611 /* Finally perform built-in cursor blinking:
25612 filled box <-> hollow box
25613 wide [h]bar <-> narrow [h]bar
25614 narrow [h]bar <-> no cursor
25615 other type <-> no cursor */
25616
25617 if (cursor_type == FILLED_BOX_CURSOR)
25618 return HOLLOW_BOX_CURSOR;
25619
25620 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25621 {
25622 *width = 1;
25623 return cursor_type;
25624 }
25625 #endif
25626
25627 return NO_CURSOR;
25628 }
25629
25630
25631 /* Notice when the text cursor of window W has been completely
25632 overwritten by a drawing operation that outputs glyphs in AREA
25633 starting at X0 and ending at X1 in the line starting at Y0 and
25634 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25635 the rest of the line after X0 has been written. Y coordinates
25636 are window-relative. */
25637
25638 static void
25639 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25640 int x0, int x1, int y0, int y1)
25641 {
25642 int cx0, cx1, cy0, cy1;
25643 struct glyph_row *row;
25644
25645 if (!w->phys_cursor_on_p)
25646 return;
25647 if (area != TEXT_AREA)
25648 return;
25649
25650 if (w->phys_cursor.vpos < 0
25651 || w->phys_cursor.vpos >= w->current_matrix->nrows
25652 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25653 !(row->enabled_p && row->displays_text_p)))
25654 return;
25655
25656 if (row->cursor_in_fringe_p)
25657 {
25658 row->cursor_in_fringe_p = 0;
25659 draw_fringe_bitmap (w, row, row->reversed_p);
25660 w->phys_cursor_on_p = 0;
25661 return;
25662 }
25663
25664 cx0 = w->phys_cursor.x;
25665 cx1 = cx0 + w->phys_cursor_width;
25666 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25667 return;
25668
25669 /* The cursor image will be completely removed from the
25670 screen if the output area intersects the cursor area in
25671 y-direction. When we draw in [y0 y1[, and some part of
25672 the cursor is at y < y0, that part must have been drawn
25673 before. When scrolling, the cursor is erased before
25674 actually scrolling, so we don't come here. When not
25675 scrolling, the rows above the old cursor row must have
25676 changed, and in this case these rows must have written
25677 over the cursor image.
25678
25679 Likewise if part of the cursor is below y1, with the
25680 exception of the cursor being in the first blank row at
25681 the buffer and window end because update_text_area
25682 doesn't draw that row. (Except when it does, but
25683 that's handled in update_text_area.) */
25684
25685 cy0 = w->phys_cursor.y;
25686 cy1 = cy0 + w->phys_cursor_height;
25687 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25688 return;
25689
25690 w->phys_cursor_on_p = 0;
25691 }
25692
25693 #endif /* HAVE_WINDOW_SYSTEM */
25694
25695 \f
25696 /************************************************************************
25697 Mouse Face
25698 ************************************************************************/
25699
25700 #ifdef HAVE_WINDOW_SYSTEM
25701
25702 /* EXPORT for RIF:
25703 Fix the display of area AREA of overlapping row ROW in window W
25704 with respect to the overlapping part OVERLAPS. */
25705
25706 void
25707 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25708 enum glyph_row_area area, int overlaps)
25709 {
25710 int i, x;
25711
25712 block_input ();
25713
25714 x = 0;
25715 for (i = 0; i < row->used[area];)
25716 {
25717 if (row->glyphs[area][i].overlaps_vertically_p)
25718 {
25719 int start = i, start_x = x;
25720
25721 do
25722 {
25723 x += row->glyphs[area][i].pixel_width;
25724 ++i;
25725 }
25726 while (i < row->used[area]
25727 && row->glyphs[area][i].overlaps_vertically_p);
25728
25729 draw_glyphs (w, start_x, row, area,
25730 start, i,
25731 DRAW_NORMAL_TEXT, overlaps);
25732 }
25733 else
25734 {
25735 x += row->glyphs[area][i].pixel_width;
25736 ++i;
25737 }
25738 }
25739
25740 unblock_input ();
25741 }
25742
25743
25744 /* EXPORT:
25745 Draw the cursor glyph of window W in glyph row ROW. See the
25746 comment of draw_glyphs for the meaning of HL. */
25747
25748 void
25749 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25750 enum draw_glyphs_face hl)
25751 {
25752 /* If cursor hpos is out of bounds, don't draw garbage. This can
25753 happen in mini-buffer windows when switching between echo area
25754 glyphs and mini-buffer. */
25755 if ((row->reversed_p
25756 ? (w->phys_cursor.hpos >= 0)
25757 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25758 {
25759 int on_p = w->phys_cursor_on_p;
25760 int x1;
25761 int hpos = w->phys_cursor.hpos;
25762
25763 /* When the window is hscrolled, cursor hpos can legitimately be
25764 out of bounds, but we draw the cursor at the corresponding
25765 window margin in that case. */
25766 if (!row->reversed_p && hpos < 0)
25767 hpos = 0;
25768 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25769 hpos = row->used[TEXT_AREA] - 1;
25770
25771 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25772 hl, 0);
25773 w->phys_cursor_on_p = on_p;
25774
25775 if (hl == DRAW_CURSOR)
25776 w->phys_cursor_width = x1 - w->phys_cursor.x;
25777 /* When we erase the cursor, and ROW is overlapped by other
25778 rows, make sure that these overlapping parts of other rows
25779 are redrawn. */
25780 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25781 {
25782 w->phys_cursor_width = x1 - w->phys_cursor.x;
25783
25784 if (row > w->current_matrix->rows
25785 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25786 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25787 OVERLAPS_ERASED_CURSOR);
25788
25789 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25790 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25791 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25792 OVERLAPS_ERASED_CURSOR);
25793 }
25794 }
25795 }
25796
25797
25798 /* EXPORT:
25799 Erase the image of a cursor of window W from the screen. */
25800
25801 void
25802 erase_phys_cursor (struct window *w)
25803 {
25804 struct frame *f = XFRAME (w->frame);
25805 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25806 int hpos = w->phys_cursor.hpos;
25807 int vpos = w->phys_cursor.vpos;
25808 int mouse_face_here_p = 0;
25809 struct glyph_matrix *active_glyphs = w->current_matrix;
25810 struct glyph_row *cursor_row;
25811 struct glyph *cursor_glyph;
25812 enum draw_glyphs_face hl;
25813
25814 /* No cursor displayed or row invalidated => nothing to do on the
25815 screen. */
25816 if (w->phys_cursor_type == NO_CURSOR)
25817 goto mark_cursor_off;
25818
25819 /* VPOS >= active_glyphs->nrows means that window has been resized.
25820 Don't bother to erase the cursor. */
25821 if (vpos >= active_glyphs->nrows)
25822 goto mark_cursor_off;
25823
25824 /* If row containing cursor is marked invalid, there is nothing we
25825 can do. */
25826 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25827 if (!cursor_row->enabled_p)
25828 goto mark_cursor_off;
25829
25830 /* If line spacing is > 0, old cursor may only be partially visible in
25831 window after split-window. So adjust visible height. */
25832 cursor_row->visible_height = min (cursor_row->visible_height,
25833 window_text_bottom_y (w) - cursor_row->y);
25834
25835 /* If row is completely invisible, don't attempt to delete a cursor which
25836 isn't there. This can happen if cursor is at top of a window, and
25837 we switch to a buffer with a header line in that window. */
25838 if (cursor_row->visible_height <= 0)
25839 goto mark_cursor_off;
25840
25841 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25842 if (cursor_row->cursor_in_fringe_p)
25843 {
25844 cursor_row->cursor_in_fringe_p = 0;
25845 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25846 goto mark_cursor_off;
25847 }
25848
25849 /* This can happen when the new row is shorter than the old one.
25850 In this case, either draw_glyphs or clear_end_of_line
25851 should have cleared the cursor. Note that we wouldn't be
25852 able to erase the cursor in this case because we don't have a
25853 cursor glyph at hand. */
25854 if ((cursor_row->reversed_p
25855 ? (w->phys_cursor.hpos < 0)
25856 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25857 goto mark_cursor_off;
25858
25859 /* When the window is hscrolled, cursor hpos can legitimately be out
25860 of bounds, but we draw the cursor at the corresponding window
25861 margin in that case. */
25862 if (!cursor_row->reversed_p && hpos < 0)
25863 hpos = 0;
25864 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25865 hpos = cursor_row->used[TEXT_AREA] - 1;
25866
25867 /* If the cursor is in the mouse face area, redisplay that when
25868 we clear the cursor. */
25869 if (! NILP (hlinfo->mouse_face_window)
25870 && coords_in_mouse_face_p (w, hpos, vpos)
25871 /* Don't redraw the cursor's spot in mouse face if it is at the
25872 end of a line (on a newline). The cursor appears there, but
25873 mouse highlighting does not. */
25874 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25875 mouse_face_here_p = 1;
25876
25877 /* Maybe clear the display under the cursor. */
25878 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25879 {
25880 int x, y, left_x;
25881 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25882 int width;
25883
25884 cursor_glyph = get_phys_cursor_glyph (w);
25885 if (cursor_glyph == NULL)
25886 goto mark_cursor_off;
25887
25888 width = cursor_glyph->pixel_width;
25889 left_x = window_box_left_offset (w, TEXT_AREA);
25890 x = w->phys_cursor.x;
25891 if (x < left_x)
25892 width -= left_x - x;
25893 width = min (width, window_box_width (w, TEXT_AREA) - x);
25894 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25895 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25896
25897 if (width > 0)
25898 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25899 }
25900
25901 /* Erase the cursor by redrawing the character underneath it. */
25902 if (mouse_face_here_p)
25903 hl = DRAW_MOUSE_FACE;
25904 else
25905 hl = DRAW_NORMAL_TEXT;
25906 draw_phys_cursor_glyph (w, cursor_row, hl);
25907
25908 mark_cursor_off:
25909 w->phys_cursor_on_p = 0;
25910 w->phys_cursor_type = NO_CURSOR;
25911 }
25912
25913
25914 /* EXPORT:
25915 Display or clear cursor of window W. If ON is zero, clear the
25916 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25917 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25918
25919 void
25920 display_and_set_cursor (struct window *w, int on,
25921 int hpos, int vpos, int x, int y)
25922 {
25923 struct frame *f = XFRAME (w->frame);
25924 int new_cursor_type;
25925 int new_cursor_width;
25926 int active_cursor;
25927 struct glyph_row *glyph_row;
25928 struct glyph *glyph;
25929
25930 /* This is pointless on invisible frames, and dangerous on garbaged
25931 windows and frames; in the latter case, the frame or window may
25932 be in the midst of changing its size, and x and y may be off the
25933 window. */
25934 if (! FRAME_VISIBLE_P (f)
25935 || FRAME_GARBAGED_P (f)
25936 || vpos >= w->current_matrix->nrows
25937 || hpos >= w->current_matrix->matrix_w)
25938 return;
25939
25940 /* If cursor is off and we want it off, return quickly. */
25941 if (!on && !w->phys_cursor_on_p)
25942 return;
25943
25944 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25945 /* If cursor row is not enabled, we don't really know where to
25946 display the cursor. */
25947 if (!glyph_row->enabled_p)
25948 {
25949 w->phys_cursor_on_p = 0;
25950 return;
25951 }
25952
25953 glyph = NULL;
25954 if (!glyph_row->exact_window_width_line_p
25955 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
25956 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
25957
25958 eassert (input_blocked_p ());
25959
25960 /* Set new_cursor_type to the cursor we want to be displayed. */
25961 new_cursor_type = get_window_cursor_type (w, glyph,
25962 &new_cursor_width, &active_cursor);
25963
25964 /* If cursor is currently being shown and we don't want it to be or
25965 it is in the wrong place, or the cursor type is not what we want,
25966 erase it. */
25967 if (w->phys_cursor_on_p
25968 && (!on
25969 || w->phys_cursor.x != x
25970 || w->phys_cursor.y != y
25971 || new_cursor_type != w->phys_cursor_type
25972 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
25973 && new_cursor_width != w->phys_cursor_width)))
25974 erase_phys_cursor (w);
25975
25976 /* Don't check phys_cursor_on_p here because that flag is only set
25977 to zero in some cases where we know that the cursor has been
25978 completely erased, to avoid the extra work of erasing the cursor
25979 twice. In other words, phys_cursor_on_p can be 1 and the cursor
25980 still not be visible, or it has only been partly erased. */
25981 if (on)
25982 {
25983 w->phys_cursor_ascent = glyph_row->ascent;
25984 w->phys_cursor_height = glyph_row->height;
25985
25986 /* Set phys_cursor_.* before x_draw_.* is called because some
25987 of them may need the information. */
25988 w->phys_cursor.x = x;
25989 w->phys_cursor.y = glyph_row->y;
25990 w->phys_cursor.hpos = hpos;
25991 w->phys_cursor.vpos = vpos;
25992 }
25993
25994 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
25995 new_cursor_type, new_cursor_width,
25996 on, active_cursor);
25997 }
25998
25999
26000 /* Switch the display of W's cursor on or off, according to the value
26001 of ON. */
26002
26003 static void
26004 update_window_cursor (struct window *w, int on)
26005 {
26006 /* Don't update cursor in windows whose frame is in the process
26007 of being deleted. */
26008 if (w->current_matrix)
26009 {
26010 int hpos = w->phys_cursor.hpos;
26011 int vpos = w->phys_cursor.vpos;
26012 struct glyph_row *row;
26013
26014 if (vpos >= w->current_matrix->nrows
26015 || hpos >= w->current_matrix->matrix_w)
26016 return;
26017
26018 row = MATRIX_ROW (w->current_matrix, vpos);
26019
26020 /* When the window is hscrolled, cursor hpos can legitimately be
26021 out of bounds, but we draw the cursor at the corresponding
26022 window margin in that case. */
26023 if (!row->reversed_p && hpos < 0)
26024 hpos = 0;
26025 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26026 hpos = row->used[TEXT_AREA] - 1;
26027
26028 block_input ();
26029 display_and_set_cursor (w, on, hpos, vpos,
26030 w->phys_cursor.x, w->phys_cursor.y);
26031 unblock_input ();
26032 }
26033 }
26034
26035
26036 /* Call update_window_cursor with parameter ON_P on all leaf windows
26037 in the window tree rooted at W. */
26038
26039 static void
26040 update_cursor_in_window_tree (struct window *w, int on_p)
26041 {
26042 while (w)
26043 {
26044 if (!NILP (w->hchild))
26045 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26046 else if (!NILP (w->vchild))
26047 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26048 else
26049 update_window_cursor (w, on_p);
26050
26051 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26052 }
26053 }
26054
26055
26056 /* EXPORT:
26057 Display the cursor on window W, or clear it, according to ON_P.
26058 Don't change the cursor's position. */
26059
26060 void
26061 x_update_cursor (struct frame *f, int on_p)
26062 {
26063 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26064 }
26065
26066
26067 /* EXPORT:
26068 Clear the cursor of window W to background color, and mark the
26069 cursor as not shown. This is used when the text where the cursor
26070 is about to be rewritten. */
26071
26072 void
26073 x_clear_cursor (struct window *w)
26074 {
26075 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26076 update_window_cursor (w, 0);
26077 }
26078
26079 #endif /* HAVE_WINDOW_SYSTEM */
26080
26081 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26082 and MSDOS. */
26083 static void
26084 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26085 int start_hpos, int end_hpos,
26086 enum draw_glyphs_face draw)
26087 {
26088 #ifdef HAVE_WINDOW_SYSTEM
26089 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26090 {
26091 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26092 return;
26093 }
26094 #endif
26095 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26096 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26097 #endif
26098 }
26099
26100 /* Display the active region described by mouse_face_* according to DRAW. */
26101
26102 static void
26103 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26104 {
26105 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26106 struct frame *f = XFRAME (WINDOW_FRAME (w));
26107
26108 if (/* If window is in the process of being destroyed, don't bother
26109 to do anything. */
26110 w->current_matrix != NULL
26111 /* Don't update mouse highlight if hidden */
26112 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26113 /* Recognize when we are called to operate on rows that don't exist
26114 anymore. This can happen when a window is split. */
26115 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26116 {
26117 int phys_cursor_on_p = w->phys_cursor_on_p;
26118 struct glyph_row *row, *first, *last;
26119
26120 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26121 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26122
26123 for (row = first; row <= last && row->enabled_p; ++row)
26124 {
26125 int start_hpos, end_hpos, start_x;
26126
26127 /* For all but the first row, the highlight starts at column 0. */
26128 if (row == first)
26129 {
26130 /* R2L rows have BEG and END in reversed order, but the
26131 screen drawing geometry is always left to right. So
26132 we need to mirror the beginning and end of the
26133 highlighted area in R2L rows. */
26134 if (!row->reversed_p)
26135 {
26136 start_hpos = hlinfo->mouse_face_beg_col;
26137 start_x = hlinfo->mouse_face_beg_x;
26138 }
26139 else if (row == last)
26140 {
26141 start_hpos = hlinfo->mouse_face_end_col;
26142 start_x = hlinfo->mouse_face_end_x;
26143 }
26144 else
26145 {
26146 start_hpos = 0;
26147 start_x = 0;
26148 }
26149 }
26150 else if (row->reversed_p && row == last)
26151 {
26152 start_hpos = hlinfo->mouse_face_end_col;
26153 start_x = hlinfo->mouse_face_end_x;
26154 }
26155 else
26156 {
26157 start_hpos = 0;
26158 start_x = 0;
26159 }
26160
26161 if (row == last)
26162 {
26163 if (!row->reversed_p)
26164 end_hpos = hlinfo->mouse_face_end_col;
26165 else if (row == first)
26166 end_hpos = hlinfo->mouse_face_beg_col;
26167 else
26168 {
26169 end_hpos = row->used[TEXT_AREA];
26170 if (draw == DRAW_NORMAL_TEXT)
26171 row->fill_line_p = 1; /* Clear to end of line */
26172 }
26173 }
26174 else if (row->reversed_p && row == first)
26175 end_hpos = hlinfo->mouse_face_beg_col;
26176 else
26177 {
26178 end_hpos = row->used[TEXT_AREA];
26179 if (draw == DRAW_NORMAL_TEXT)
26180 row->fill_line_p = 1; /* Clear to end of line */
26181 }
26182
26183 if (end_hpos > start_hpos)
26184 {
26185 draw_row_with_mouse_face (w, start_x, row,
26186 start_hpos, end_hpos, draw);
26187
26188 row->mouse_face_p
26189 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26190 }
26191 }
26192
26193 #ifdef HAVE_WINDOW_SYSTEM
26194 /* When we've written over the cursor, arrange for it to
26195 be displayed again. */
26196 if (FRAME_WINDOW_P (f)
26197 && phys_cursor_on_p && !w->phys_cursor_on_p)
26198 {
26199 int hpos = w->phys_cursor.hpos;
26200
26201 /* When the window is hscrolled, cursor hpos can legitimately be
26202 out of bounds, but we draw the cursor at the corresponding
26203 window margin in that case. */
26204 if (!row->reversed_p && hpos < 0)
26205 hpos = 0;
26206 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26207 hpos = row->used[TEXT_AREA] - 1;
26208
26209 block_input ();
26210 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26211 w->phys_cursor.x, w->phys_cursor.y);
26212 unblock_input ();
26213 }
26214 #endif /* HAVE_WINDOW_SYSTEM */
26215 }
26216
26217 #ifdef HAVE_WINDOW_SYSTEM
26218 /* Change the mouse cursor. */
26219 if (FRAME_WINDOW_P (f))
26220 {
26221 if (draw == DRAW_NORMAL_TEXT
26222 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26223 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26224 else if (draw == DRAW_MOUSE_FACE)
26225 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26226 else
26227 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26228 }
26229 #endif /* HAVE_WINDOW_SYSTEM */
26230 }
26231
26232 /* EXPORT:
26233 Clear out the mouse-highlighted active region.
26234 Redraw it un-highlighted first. Value is non-zero if mouse
26235 face was actually drawn unhighlighted. */
26236
26237 int
26238 clear_mouse_face (Mouse_HLInfo *hlinfo)
26239 {
26240 int cleared = 0;
26241
26242 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26243 {
26244 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26245 cleared = 1;
26246 }
26247
26248 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26249 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26250 hlinfo->mouse_face_window = Qnil;
26251 hlinfo->mouse_face_overlay = Qnil;
26252 return cleared;
26253 }
26254
26255 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26256 within the mouse face on that window. */
26257 static int
26258 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26259 {
26260 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26261
26262 /* Quickly resolve the easy cases. */
26263 if (!(WINDOWP (hlinfo->mouse_face_window)
26264 && XWINDOW (hlinfo->mouse_face_window) == w))
26265 return 0;
26266 if (vpos < hlinfo->mouse_face_beg_row
26267 || vpos > hlinfo->mouse_face_end_row)
26268 return 0;
26269 if (vpos > hlinfo->mouse_face_beg_row
26270 && vpos < hlinfo->mouse_face_end_row)
26271 return 1;
26272
26273 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26274 {
26275 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26276 {
26277 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26278 return 1;
26279 }
26280 else if ((vpos == hlinfo->mouse_face_beg_row
26281 && hpos >= hlinfo->mouse_face_beg_col)
26282 || (vpos == hlinfo->mouse_face_end_row
26283 && hpos < hlinfo->mouse_face_end_col))
26284 return 1;
26285 }
26286 else
26287 {
26288 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26289 {
26290 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26291 return 1;
26292 }
26293 else if ((vpos == hlinfo->mouse_face_beg_row
26294 && hpos <= hlinfo->mouse_face_beg_col)
26295 || (vpos == hlinfo->mouse_face_end_row
26296 && hpos > hlinfo->mouse_face_end_col))
26297 return 1;
26298 }
26299 return 0;
26300 }
26301
26302
26303 /* EXPORT:
26304 Non-zero if physical cursor of window W is within mouse face. */
26305
26306 int
26307 cursor_in_mouse_face_p (struct window *w)
26308 {
26309 int hpos = w->phys_cursor.hpos;
26310 int vpos = w->phys_cursor.vpos;
26311 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26312
26313 /* When the window is hscrolled, cursor hpos can legitimately be out
26314 of bounds, but we draw the cursor at the corresponding window
26315 margin in that case. */
26316 if (!row->reversed_p && hpos < 0)
26317 hpos = 0;
26318 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26319 hpos = row->used[TEXT_AREA] - 1;
26320
26321 return coords_in_mouse_face_p (w, hpos, vpos);
26322 }
26323
26324
26325 \f
26326 /* Find the glyph rows START_ROW and END_ROW of window W that display
26327 characters between buffer positions START_CHARPOS and END_CHARPOS
26328 (excluding END_CHARPOS). DISP_STRING is a display string that
26329 covers these buffer positions. This is similar to
26330 row_containing_pos, but is more accurate when bidi reordering makes
26331 buffer positions change non-linearly with glyph rows. */
26332 static void
26333 rows_from_pos_range (struct window *w,
26334 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26335 Lisp_Object disp_string,
26336 struct glyph_row **start, struct glyph_row **end)
26337 {
26338 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26339 int last_y = window_text_bottom_y (w);
26340 struct glyph_row *row;
26341
26342 *start = NULL;
26343 *end = NULL;
26344
26345 while (!first->enabled_p
26346 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26347 first++;
26348
26349 /* Find the START row. */
26350 for (row = first;
26351 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26352 row++)
26353 {
26354 /* A row can potentially be the START row if the range of the
26355 characters it displays intersects the range
26356 [START_CHARPOS..END_CHARPOS). */
26357 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26358 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26359 /* See the commentary in row_containing_pos, for the
26360 explanation of the complicated way to check whether
26361 some position is beyond the end of the characters
26362 displayed by a row. */
26363 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26364 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26365 && !row->ends_at_zv_p
26366 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26367 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26368 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26369 && !row->ends_at_zv_p
26370 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26371 {
26372 /* Found a candidate row. Now make sure at least one of the
26373 glyphs it displays has a charpos from the range
26374 [START_CHARPOS..END_CHARPOS).
26375
26376 This is not obvious because bidi reordering could make
26377 buffer positions of a row be 1,2,3,102,101,100, and if we
26378 want to highlight characters in [50..60), we don't want
26379 this row, even though [50..60) does intersect [1..103),
26380 the range of character positions given by the row's start
26381 and end positions. */
26382 struct glyph *g = row->glyphs[TEXT_AREA];
26383 struct glyph *e = g + row->used[TEXT_AREA];
26384
26385 while (g < e)
26386 {
26387 if (((BUFFERP (g->object) || INTEGERP (g->object))
26388 && start_charpos <= g->charpos && g->charpos < end_charpos)
26389 /* A glyph that comes from DISP_STRING is by
26390 definition to be highlighted. */
26391 || EQ (g->object, disp_string))
26392 *start = row;
26393 g++;
26394 }
26395 if (*start)
26396 break;
26397 }
26398 }
26399
26400 /* Find the END row. */
26401 if (!*start
26402 /* If the last row is partially visible, start looking for END
26403 from that row, instead of starting from FIRST. */
26404 && !(row->enabled_p
26405 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26406 row = first;
26407 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26408 {
26409 struct glyph_row *next = row + 1;
26410 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26411
26412 if (!next->enabled_p
26413 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26414 /* The first row >= START whose range of displayed characters
26415 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26416 is the row END + 1. */
26417 || (start_charpos < next_start
26418 && end_charpos < next_start)
26419 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26420 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26421 && !next->ends_at_zv_p
26422 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26423 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26424 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26425 && !next->ends_at_zv_p
26426 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26427 {
26428 *end = row;
26429 break;
26430 }
26431 else
26432 {
26433 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26434 but none of the characters it displays are in the range, it is
26435 also END + 1. */
26436 struct glyph *g = next->glyphs[TEXT_AREA];
26437 struct glyph *s = g;
26438 struct glyph *e = g + next->used[TEXT_AREA];
26439
26440 while (g < e)
26441 {
26442 if (((BUFFERP (g->object) || INTEGERP (g->object))
26443 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26444 /* If the buffer position of the first glyph in
26445 the row is equal to END_CHARPOS, it means
26446 the last character to be highlighted is the
26447 newline of ROW, and we must consider NEXT as
26448 END, not END+1. */
26449 || (((!next->reversed_p && g == s)
26450 || (next->reversed_p && g == e - 1))
26451 && (g->charpos == end_charpos
26452 /* Special case for when NEXT is an
26453 empty line at ZV. */
26454 || (g->charpos == -1
26455 && !row->ends_at_zv_p
26456 && next_start == end_charpos)))))
26457 /* A glyph that comes from DISP_STRING is by
26458 definition to be highlighted. */
26459 || EQ (g->object, disp_string))
26460 break;
26461 g++;
26462 }
26463 if (g == e)
26464 {
26465 *end = row;
26466 break;
26467 }
26468 /* The first row that ends at ZV must be the last to be
26469 highlighted. */
26470 else if (next->ends_at_zv_p)
26471 {
26472 *end = next;
26473 break;
26474 }
26475 }
26476 }
26477 }
26478
26479 /* This function sets the mouse_face_* elements of HLINFO, assuming
26480 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26481 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26482 for the overlay or run of text properties specifying the mouse
26483 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26484 before-string and after-string that must also be highlighted.
26485 DISP_STRING, if non-nil, is a display string that may cover some
26486 or all of the highlighted text. */
26487
26488 static void
26489 mouse_face_from_buffer_pos (Lisp_Object window,
26490 Mouse_HLInfo *hlinfo,
26491 ptrdiff_t mouse_charpos,
26492 ptrdiff_t start_charpos,
26493 ptrdiff_t end_charpos,
26494 Lisp_Object before_string,
26495 Lisp_Object after_string,
26496 Lisp_Object disp_string)
26497 {
26498 struct window *w = XWINDOW (window);
26499 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26500 struct glyph_row *r1, *r2;
26501 struct glyph *glyph, *end;
26502 ptrdiff_t ignore, pos;
26503 int x;
26504
26505 eassert (NILP (disp_string) || STRINGP (disp_string));
26506 eassert (NILP (before_string) || STRINGP (before_string));
26507 eassert (NILP (after_string) || STRINGP (after_string));
26508
26509 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26510 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26511 if (r1 == NULL)
26512 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26513 /* If the before-string or display-string contains newlines,
26514 rows_from_pos_range skips to its last row. Move back. */
26515 if (!NILP (before_string) || !NILP (disp_string))
26516 {
26517 struct glyph_row *prev;
26518 while ((prev = r1 - 1, prev >= first)
26519 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26520 && prev->used[TEXT_AREA] > 0)
26521 {
26522 struct glyph *beg = prev->glyphs[TEXT_AREA];
26523 glyph = beg + prev->used[TEXT_AREA];
26524 while (--glyph >= beg && INTEGERP (glyph->object));
26525 if (glyph < beg
26526 || !(EQ (glyph->object, before_string)
26527 || EQ (glyph->object, disp_string)))
26528 break;
26529 r1 = prev;
26530 }
26531 }
26532 if (r2 == NULL)
26533 {
26534 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26535 hlinfo->mouse_face_past_end = 1;
26536 }
26537 else if (!NILP (after_string))
26538 {
26539 /* If the after-string has newlines, advance to its last row. */
26540 struct glyph_row *next;
26541 struct glyph_row *last
26542 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26543
26544 for (next = r2 + 1;
26545 next <= last
26546 && next->used[TEXT_AREA] > 0
26547 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26548 ++next)
26549 r2 = next;
26550 }
26551 /* The rest of the display engine assumes that mouse_face_beg_row is
26552 either above mouse_face_end_row or identical to it. But with
26553 bidi-reordered continued lines, the row for START_CHARPOS could
26554 be below the row for END_CHARPOS. If so, swap the rows and store
26555 them in correct order. */
26556 if (r1->y > r2->y)
26557 {
26558 struct glyph_row *tem = r2;
26559
26560 r2 = r1;
26561 r1 = tem;
26562 }
26563
26564 hlinfo->mouse_face_beg_y = r1->y;
26565 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26566 hlinfo->mouse_face_end_y = r2->y;
26567 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26568
26569 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26570 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26571 could be anywhere in the row and in any order. The strategy
26572 below is to find the leftmost and the rightmost glyph that
26573 belongs to either of these 3 strings, or whose position is
26574 between START_CHARPOS and END_CHARPOS, and highlight all the
26575 glyphs between those two. This may cover more than just the text
26576 between START_CHARPOS and END_CHARPOS if the range of characters
26577 strides the bidi level boundary, e.g. if the beginning is in R2L
26578 text while the end is in L2R text or vice versa. */
26579 if (!r1->reversed_p)
26580 {
26581 /* This row is in a left to right paragraph. Scan it left to
26582 right. */
26583 glyph = r1->glyphs[TEXT_AREA];
26584 end = glyph + r1->used[TEXT_AREA];
26585 x = r1->x;
26586
26587 /* Skip truncation glyphs at the start of the glyph row. */
26588 if (r1->displays_text_p)
26589 for (; glyph < end
26590 && INTEGERP (glyph->object)
26591 && glyph->charpos < 0;
26592 ++glyph)
26593 x += glyph->pixel_width;
26594
26595 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26596 or DISP_STRING, and the first glyph from buffer whose
26597 position is between START_CHARPOS and END_CHARPOS. */
26598 for (; glyph < end
26599 && !INTEGERP (glyph->object)
26600 && !EQ (glyph->object, disp_string)
26601 && !(BUFFERP (glyph->object)
26602 && (glyph->charpos >= start_charpos
26603 && glyph->charpos < end_charpos));
26604 ++glyph)
26605 {
26606 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26607 are present at buffer positions between START_CHARPOS and
26608 END_CHARPOS, or if they come from an overlay. */
26609 if (EQ (glyph->object, before_string))
26610 {
26611 pos = string_buffer_position (before_string,
26612 start_charpos);
26613 /* If pos == 0, it means before_string came from an
26614 overlay, not from a buffer position. */
26615 if (!pos || (pos >= start_charpos && pos < end_charpos))
26616 break;
26617 }
26618 else if (EQ (glyph->object, after_string))
26619 {
26620 pos = string_buffer_position (after_string, end_charpos);
26621 if (!pos || (pos >= start_charpos && pos < end_charpos))
26622 break;
26623 }
26624 x += glyph->pixel_width;
26625 }
26626 hlinfo->mouse_face_beg_x = x;
26627 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26628 }
26629 else
26630 {
26631 /* This row is in a right to left paragraph. Scan it right to
26632 left. */
26633 struct glyph *g;
26634
26635 end = r1->glyphs[TEXT_AREA] - 1;
26636 glyph = end + r1->used[TEXT_AREA];
26637
26638 /* Skip truncation glyphs at the start of the glyph row. */
26639 if (r1->displays_text_p)
26640 for (; glyph > end
26641 && INTEGERP (glyph->object)
26642 && glyph->charpos < 0;
26643 --glyph)
26644 ;
26645
26646 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26647 or DISP_STRING, and the first glyph from buffer whose
26648 position is between START_CHARPOS and END_CHARPOS. */
26649 for (; glyph > end
26650 && !INTEGERP (glyph->object)
26651 && !EQ (glyph->object, disp_string)
26652 && !(BUFFERP (glyph->object)
26653 && (glyph->charpos >= start_charpos
26654 && glyph->charpos < end_charpos));
26655 --glyph)
26656 {
26657 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26658 are present at buffer positions between START_CHARPOS and
26659 END_CHARPOS, or if they come from an overlay. */
26660 if (EQ (glyph->object, before_string))
26661 {
26662 pos = string_buffer_position (before_string, start_charpos);
26663 /* If pos == 0, it means before_string came from an
26664 overlay, not from a buffer position. */
26665 if (!pos || (pos >= start_charpos && pos < end_charpos))
26666 break;
26667 }
26668 else if (EQ (glyph->object, after_string))
26669 {
26670 pos = string_buffer_position (after_string, end_charpos);
26671 if (!pos || (pos >= start_charpos && pos < end_charpos))
26672 break;
26673 }
26674 }
26675
26676 glyph++; /* first glyph to the right of the highlighted area */
26677 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26678 x += g->pixel_width;
26679 hlinfo->mouse_face_beg_x = x;
26680 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26681 }
26682
26683 /* If the highlight ends in a different row, compute GLYPH and END
26684 for the end row. Otherwise, reuse the values computed above for
26685 the row where the highlight begins. */
26686 if (r2 != r1)
26687 {
26688 if (!r2->reversed_p)
26689 {
26690 glyph = r2->glyphs[TEXT_AREA];
26691 end = glyph + r2->used[TEXT_AREA];
26692 x = r2->x;
26693 }
26694 else
26695 {
26696 end = r2->glyphs[TEXT_AREA] - 1;
26697 glyph = end + r2->used[TEXT_AREA];
26698 }
26699 }
26700
26701 if (!r2->reversed_p)
26702 {
26703 /* Skip truncation and continuation glyphs near the end of the
26704 row, and also blanks and stretch glyphs inserted by
26705 extend_face_to_end_of_line. */
26706 while (end > glyph
26707 && INTEGERP ((end - 1)->object))
26708 --end;
26709 /* Scan the rest of the glyph row from the end, looking for the
26710 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26711 DISP_STRING, or whose position is between START_CHARPOS
26712 and END_CHARPOS */
26713 for (--end;
26714 end > glyph
26715 && !INTEGERP (end->object)
26716 && !EQ (end->object, disp_string)
26717 && !(BUFFERP (end->object)
26718 && (end->charpos >= start_charpos
26719 && end->charpos < end_charpos));
26720 --end)
26721 {
26722 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26723 are present at buffer positions between START_CHARPOS and
26724 END_CHARPOS, or if they come from an overlay. */
26725 if (EQ (end->object, before_string))
26726 {
26727 pos = string_buffer_position (before_string, start_charpos);
26728 if (!pos || (pos >= start_charpos && pos < end_charpos))
26729 break;
26730 }
26731 else if (EQ (end->object, after_string))
26732 {
26733 pos = string_buffer_position (after_string, end_charpos);
26734 if (!pos || (pos >= start_charpos && pos < end_charpos))
26735 break;
26736 }
26737 }
26738 /* Find the X coordinate of the last glyph to be highlighted. */
26739 for (; glyph <= end; ++glyph)
26740 x += glyph->pixel_width;
26741
26742 hlinfo->mouse_face_end_x = x;
26743 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26744 }
26745 else
26746 {
26747 /* Skip truncation and continuation glyphs near the end of the
26748 row, and also blanks and stretch glyphs inserted by
26749 extend_face_to_end_of_line. */
26750 x = r2->x;
26751 end++;
26752 while (end < glyph
26753 && INTEGERP (end->object))
26754 {
26755 x += end->pixel_width;
26756 ++end;
26757 }
26758 /* Scan the rest of the glyph row from the end, looking for the
26759 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26760 DISP_STRING, or whose position is between START_CHARPOS
26761 and END_CHARPOS */
26762 for ( ;
26763 end < glyph
26764 && !INTEGERP (end->object)
26765 && !EQ (end->object, disp_string)
26766 && !(BUFFERP (end->object)
26767 && (end->charpos >= start_charpos
26768 && end->charpos < end_charpos));
26769 ++end)
26770 {
26771 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26772 are present at buffer positions between START_CHARPOS and
26773 END_CHARPOS, or if they come from an overlay. */
26774 if (EQ (end->object, before_string))
26775 {
26776 pos = string_buffer_position (before_string, start_charpos);
26777 if (!pos || (pos >= start_charpos && pos < end_charpos))
26778 break;
26779 }
26780 else if (EQ (end->object, after_string))
26781 {
26782 pos = string_buffer_position (after_string, end_charpos);
26783 if (!pos || (pos >= start_charpos && pos < end_charpos))
26784 break;
26785 }
26786 x += end->pixel_width;
26787 }
26788 /* If we exited the above loop because we arrived at the last
26789 glyph of the row, and its buffer position is still not in
26790 range, it means the last character in range is the preceding
26791 newline. Bump the end column and x values to get past the
26792 last glyph. */
26793 if (end == glyph
26794 && BUFFERP (end->object)
26795 && (end->charpos < start_charpos
26796 || end->charpos >= end_charpos))
26797 {
26798 x += end->pixel_width;
26799 ++end;
26800 }
26801 hlinfo->mouse_face_end_x = x;
26802 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26803 }
26804
26805 hlinfo->mouse_face_window = window;
26806 hlinfo->mouse_face_face_id
26807 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26808 mouse_charpos + 1,
26809 !hlinfo->mouse_face_hidden, -1);
26810 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26811 }
26812
26813 /* The following function is not used anymore (replaced with
26814 mouse_face_from_string_pos), but I leave it here for the time
26815 being, in case someone would. */
26816
26817 #if 0 /* not used */
26818
26819 /* Find the position of the glyph for position POS in OBJECT in
26820 window W's current matrix, and return in *X, *Y the pixel
26821 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26822
26823 RIGHT_P non-zero means return the position of the right edge of the
26824 glyph, RIGHT_P zero means return the left edge position.
26825
26826 If no glyph for POS exists in the matrix, return the position of
26827 the glyph with the next smaller position that is in the matrix, if
26828 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26829 exists in the matrix, return the position of the glyph with the
26830 next larger position in OBJECT.
26831
26832 Value is non-zero if a glyph was found. */
26833
26834 static int
26835 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26836 int *hpos, int *vpos, int *x, int *y, int right_p)
26837 {
26838 int yb = window_text_bottom_y (w);
26839 struct glyph_row *r;
26840 struct glyph *best_glyph = NULL;
26841 struct glyph_row *best_row = NULL;
26842 int best_x = 0;
26843
26844 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26845 r->enabled_p && r->y < yb;
26846 ++r)
26847 {
26848 struct glyph *g = r->glyphs[TEXT_AREA];
26849 struct glyph *e = g + r->used[TEXT_AREA];
26850 int gx;
26851
26852 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26853 if (EQ (g->object, object))
26854 {
26855 if (g->charpos == pos)
26856 {
26857 best_glyph = g;
26858 best_x = gx;
26859 best_row = r;
26860 goto found;
26861 }
26862 else if (best_glyph == NULL
26863 || ((eabs (g->charpos - pos)
26864 < eabs (best_glyph->charpos - pos))
26865 && (right_p
26866 ? g->charpos < pos
26867 : g->charpos > pos)))
26868 {
26869 best_glyph = g;
26870 best_x = gx;
26871 best_row = r;
26872 }
26873 }
26874 }
26875
26876 found:
26877
26878 if (best_glyph)
26879 {
26880 *x = best_x;
26881 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26882
26883 if (right_p)
26884 {
26885 *x += best_glyph->pixel_width;
26886 ++*hpos;
26887 }
26888
26889 *y = best_row->y;
26890 *vpos = best_row - w->current_matrix->rows;
26891 }
26892
26893 return best_glyph != NULL;
26894 }
26895 #endif /* not used */
26896
26897 /* Find the positions of the first and the last glyphs in window W's
26898 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26899 (assumed to be a string), and return in HLINFO's mouse_face_*
26900 members the pixel and column/row coordinates of those glyphs. */
26901
26902 static void
26903 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26904 Lisp_Object object,
26905 ptrdiff_t startpos, ptrdiff_t endpos)
26906 {
26907 int yb = window_text_bottom_y (w);
26908 struct glyph_row *r;
26909 struct glyph *g, *e;
26910 int gx;
26911 int found = 0;
26912
26913 /* Find the glyph row with at least one position in the range
26914 [STARTPOS..ENDPOS], and the first glyph in that row whose
26915 position belongs to that range. */
26916 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26917 r->enabled_p && r->y < yb;
26918 ++r)
26919 {
26920 if (!r->reversed_p)
26921 {
26922 g = r->glyphs[TEXT_AREA];
26923 e = g + r->used[TEXT_AREA];
26924 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26925 if (EQ (g->object, object)
26926 && startpos <= g->charpos && g->charpos <= endpos)
26927 {
26928 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26929 hlinfo->mouse_face_beg_y = r->y;
26930 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26931 hlinfo->mouse_face_beg_x = gx;
26932 found = 1;
26933 break;
26934 }
26935 }
26936 else
26937 {
26938 struct glyph *g1;
26939
26940 e = r->glyphs[TEXT_AREA];
26941 g = e + r->used[TEXT_AREA];
26942 for ( ; g > e; --g)
26943 if (EQ ((g-1)->object, object)
26944 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26945 {
26946 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
26947 hlinfo->mouse_face_beg_y = r->y;
26948 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26949 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26950 gx += g1->pixel_width;
26951 hlinfo->mouse_face_beg_x = gx;
26952 found = 1;
26953 break;
26954 }
26955 }
26956 if (found)
26957 break;
26958 }
26959
26960 if (!found)
26961 return;
26962
26963 /* Starting with the next row, look for the first row which does NOT
26964 include any glyphs whose positions are in the range. */
26965 for (++r; r->enabled_p && r->y < yb; ++r)
26966 {
26967 g = r->glyphs[TEXT_AREA];
26968 e = g + r->used[TEXT_AREA];
26969 found = 0;
26970 for ( ; g < e; ++g)
26971 if (EQ (g->object, object)
26972 && startpos <= g->charpos && g->charpos <= endpos)
26973 {
26974 found = 1;
26975 break;
26976 }
26977 if (!found)
26978 break;
26979 }
26980
26981 /* The highlighted region ends on the previous row. */
26982 r--;
26983
26984 /* Set the end row and its vertical pixel coordinate. */
26985 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
26986 hlinfo->mouse_face_end_y = r->y;
26987
26988 /* Compute and set the end column and the end column's horizontal
26989 pixel coordinate. */
26990 if (!r->reversed_p)
26991 {
26992 g = r->glyphs[TEXT_AREA];
26993 e = g + r->used[TEXT_AREA];
26994 for ( ; e > g; --e)
26995 if (EQ ((e-1)->object, object)
26996 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
26997 break;
26998 hlinfo->mouse_face_end_col = e - g;
26999
27000 for (gx = r->x; g < e; ++g)
27001 gx += g->pixel_width;
27002 hlinfo->mouse_face_end_x = gx;
27003 }
27004 else
27005 {
27006 e = r->glyphs[TEXT_AREA];
27007 g = e + r->used[TEXT_AREA];
27008 for (gx = r->x ; e < g; ++e)
27009 {
27010 if (EQ (e->object, object)
27011 && startpos <= e->charpos && e->charpos <= endpos)
27012 break;
27013 gx += e->pixel_width;
27014 }
27015 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27016 hlinfo->mouse_face_end_x = gx;
27017 }
27018 }
27019
27020 #ifdef HAVE_WINDOW_SYSTEM
27021
27022 /* See if position X, Y is within a hot-spot of an image. */
27023
27024 static int
27025 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27026 {
27027 if (!CONSP (hot_spot))
27028 return 0;
27029
27030 if (EQ (XCAR (hot_spot), Qrect))
27031 {
27032 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27033 Lisp_Object rect = XCDR (hot_spot);
27034 Lisp_Object tem;
27035 if (!CONSP (rect))
27036 return 0;
27037 if (!CONSP (XCAR (rect)))
27038 return 0;
27039 if (!CONSP (XCDR (rect)))
27040 return 0;
27041 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27042 return 0;
27043 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27044 return 0;
27045 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27046 return 0;
27047 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27048 return 0;
27049 return 1;
27050 }
27051 else if (EQ (XCAR (hot_spot), Qcircle))
27052 {
27053 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27054 Lisp_Object circ = XCDR (hot_spot);
27055 Lisp_Object lr, lx0, ly0;
27056 if (CONSP (circ)
27057 && CONSP (XCAR (circ))
27058 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27059 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27060 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27061 {
27062 double r = XFLOATINT (lr);
27063 double dx = XINT (lx0) - x;
27064 double dy = XINT (ly0) - y;
27065 return (dx * dx + dy * dy <= r * r);
27066 }
27067 }
27068 else if (EQ (XCAR (hot_spot), Qpoly))
27069 {
27070 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27071 if (VECTORP (XCDR (hot_spot)))
27072 {
27073 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27074 Lisp_Object *poly = v->contents;
27075 ptrdiff_t n = v->header.size;
27076 ptrdiff_t i;
27077 int inside = 0;
27078 Lisp_Object lx, ly;
27079 int x0, y0;
27080
27081 /* Need an even number of coordinates, and at least 3 edges. */
27082 if (n < 6 || n & 1)
27083 return 0;
27084
27085 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27086 If count is odd, we are inside polygon. Pixels on edges
27087 may or may not be included depending on actual geometry of the
27088 polygon. */
27089 if ((lx = poly[n-2], !INTEGERP (lx))
27090 || (ly = poly[n-1], !INTEGERP (lx)))
27091 return 0;
27092 x0 = XINT (lx), y0 = XINT (ly);
27093 for (i = 0; i < n; i += 2)
27094 {
27095 int x1 = x0, y1 = y0;
27096 if ((lx = poly[i], !INTEGERP (lx))
27097 || (ly = poly[i+1], !INTEGERP (ly)))
27098 return 0;
27099 x0 = XINT (lx), y0 = XINT (ly);
27100
27101 /* Does this segment cross the X line? */
27102 if (x0 >= x)
27103 {
27104 if (x1 >= x)
27105 continue;
27106 }
27107 else if (x1 < x)
27108 continue;
27109 if (y > y0 && y > y1)
27110 continue;
27111 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27112 inside = !inside;
27113 }
27114 return inside;
27115 }
27116 }
27117 return 0;
27118 }
27119
27120 Lisp_Object
27121 find_hot_spot (Lisp_Object map, int x, int y)
27122 {
27123 while (CONSP (map))
27124 {
27125 if (CONSP (XCAR (map))
27126 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27127 return XCAR (map);
27128 map = XCDR (map);
27129 }
27130
27131 return Qnil;
27132 }
27133
27134 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27135 3, 3, 0,
27136 doc: /* Lookup in image map MAP coordinates X and Y.
27137 An image map is an alist where each element has the format (AREA ID PLIST).
27138 An AREA is specified as either a rectangle, a circle, or a polygon:
27139 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27140 pixel coordinates of the upper left and bottom right corners.
27141 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27142 and the radius of the circle; r may be a float or integer.
27143 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27144 vector describes one corner in the polygon.
27145 Returns the alist element for the first matching AREA in MAP. */)
27146 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27147 {
27148 if (NILP (map))
27149 return Qnil;
27150
27151 CHECK_NUMBER (x);
27152 CHECK_NUMBER (y);
27153
27154 return find_hot_spot (map,
27155 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27156 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27157 }
27158
27159
27160 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27161 static void
27162 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27163 {
27164 /* Do not change cursor shape while dragging mouse. */
27165 if (!NILP (do_mouse_tracking))
27166 return;
27167
27168 if (!NILP (pointer))
27169 {
27170 if (EQ (pointer, Qarrow))
27171 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27172 else if (EQ (pointer, Qhand))
27173 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27174 else if (EQ (pointer, Qtext))
27175 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27176 else if (EQ (pointer, intern ("hdrag")))
27177 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27178 #ifdef HAVE_X_WINDOWS
27179 else if (EQ (pointer, intern ("vdrag")))
27180 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27181 #endif
27182 else if (EQ (pointer, intern ("hourglass")))
27183 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27184 else if (EQ (pointer, Qmodeline))
27185 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27186 else
27187 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27188 }
27189
27190 if (cursor != No_Cursor)
27191 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27192 }
27193
27194 #endif /* HAVE_WINDOW_SYSTEM */
27195
27196 /* Take proper action when mouse has moved to the mode or header line
27197 or marginal area AREA of window W, x-position X and y-position Y.
27198 X is relative to the start of the text display area of W, so the
27199 width of bitmap areas and scroll bars must be subtracted to get a
27200 position relative to the start of the mode line. */
27201
27202 static void
27203 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27204 enum window_part area)
27205 {
27206 struct window *w = XWINDOW (window);
27207 struct frame *f = XFRAME (w->frame);
27208 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27209 #ifdef HAVE_WINDOW_SYSTEM
27210 Display_Info *dpyinfo;
27211 #endif
27212 Cursor cursor = No_Cursor;
27213 Lisp_Object pointer = Qnil;
27214 int dx, dy, width, height;
27215 ptrdiff_t charpos;
27216 Lisp_Object string, object = Qnil;
27217 Lisp_Object pos IF_LINT (= Qnil), help;
27218
27219 Lisp_Object mouse_face;
27220 int original_x_pixel = x;
27221 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27222 struct glyph_row *row IF_LINT (= 0);
27223
27224 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27225 {
27226 int x0;
27227 struct glyph *end;
27228
27229 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27230 returns them in row/column units! */
27231 string = mode_line_string (w, area, &x, &y, &charpos,
27232 &object, &dx, &dy, &width, &height);
27233
27234 row = (area == ON_MODE_LINE
27235 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27236 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27237
27238 /* Find the glyph under the mouse pointer. */
27239 if (row->mode_line_p && row->enabled_p)
27240 {
27241 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27242 end = glyph + row->used[TEXT_AREA];
27243
27244 for (x0 = original_x_pixel;
27245 glyph < end && x0 >= glyph->pixel_width;
27246 ++glyph)
27247 x0 -= glyph->pixel_width;
27248
27249 if (glyph >= end)
27250 glyph = NULL;
27251 }
27252 }
27253 else
27254 {
27255 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27256 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27257 returns them in row/column units! */
27258 string = marginal_area_string (w, area, &x, &y, &charpos,
27259 &object, &dx, &dy, &width, &height);
27260 }
27261
27262 help = Qnil;
27263
27264 #ifdef HAVE_WINDOW_SYSTEM
27265 if (IMAGEP (object))
27266 {
27267 Lisp_Object image_map, hotspot;
27268 if ((image_map = Fplist_get (XCDR (object), QCmap),
27269 !NILP (image_map))
27270 && (hotspot = find_hot_spot (image_map, dx, dy),
27271 CONSP (hotspot))
27272 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27273 {
27274 Lisp_Object plist;
27275
27276 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27277 If so, we could look for mouse-enter, mouse-leave
27278 properties in PLIST (and do something...). */
27279 hotspot = XCDR (hotspot);
27280 if (CONSP (hotspot)
27281 && (plist = XCAR (hotspot), CONSP (plist)))
27282 {
27283 pointer = Fplist_get (plist, Qpointer);
27284 if (NILP (pointer))
27285 pointer = Qhand;
27286 help = Fplist_get (plist, Qhelp_echo);
27287 if (!NILP (help))
27288 {
27289 help_echo_string = help;
27290 XSETWINDOW (help_echo_window, w);
27291 help_echo_object = w->buffer;
27292 help_echo_pos = charpos;
27293 }
27294 }
27295 }
27296 if (NILP (pointer))
27297 pointer = Fplist_get (XCDR (object), QCpointer);
27298 }
27299 #endif /* HAVE_WINDOW_SYSTEM */
27300
27301 if (STRINGP (string))
27302 pos = make_number (charpos);
27303
27304 /* Set the help text and mouse pointer. If the mouse is on a part
27305 of the mode line without any text (e.g. past the right edge of
27306 the mode line text), use the default help text and pointer. */
27307 if (STRINGP (string) || area == ON_MODE_LINE)
27308 {
27309 /* Arrange to display the help by setting the global variables
27310 help_echo_string, help_echo_object, and help_echo_pos. */
27311 if (NILP (help))
27312 {
27313 if (STRINGP (string))
27314 help = Fget_text_property (pos, Qhelp_echo, string);
27315
27316 if (!NILP (help))
27317 {
27318 help_echo_string = help;
27319 XSETWINDOW (help_echo_window, w);
27320 help_echo_object = string;
27321 help_echo_pos = charpos;
27322 }
27323 else if (area == ON_MODE_LINE)
27324 {
27325 Lisp_Object default_help
27326 = buffer_local_value_1 (Qmode_line_default_help_echo,
27327 w->buffer);
27328
27329 if (STRINGP (default_help))
27330 {
27331 help_echo_string = default_help;
27332 XSETWINDOW (help_echo_window, w);
27333 help_echo_object = Qnil;
27334 help_echo_pos = -1;
27335 }
27336 }
27337 }
27338
27339 #ifdef HAVE_WINDOW_SYSTEM
27340 /* Change the mouse pointer according to what is under it. */
27341 if (FRAME_WINDOW_P (f))
27342 {
27343 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27344 if (STRINGP (string))
27345 {
27346 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27347
27348 if (NILP (pointer))
27349 pointer = Fget_text_property (pos, Qpointer, string);
27350
27351 /* Change the mouse pointer according to what is under X/Y. */
27352 if (NILP (pointer)
27353 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27354 {
27355 Lisp_Object map;
27356 map = Fget_text_property (pos, Qlocal_map, string);
27357 if (!KEYMAPP (map))
27358 map = Fget_text_property (pos, Qkeymap, string);
27359 if (!KEYMAPP (map))
27360 cursor = dpyinfo->vertical_scroll_bar_cursor;
27361 }
27362 }
27363 else
27364 /* Default mode-line pointer. */
27365 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27366 }
27367 #endif
27368 }
27369
27370 /* Change the mouse face according to what is under X/Y. */
27371 if (STRINGP (string))
27372 {
27373 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27374 if (!NILP (mouse_face)
27375 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27376 && glyph)
27377 {
27378 Lisp_Object b, e;
27379
27380 struct glyph * tmp_glyph;
27381
27382 int gpos;
27383 int gseq_length;
27384 int total_pixel_width;
27385 ptrdiff_t begpos, endpos, ignore;
27386
27387 int vpos, hpos;
27388
27389 b = Fprevious_single_property_change (make_number (charpos + 1),
27390 Qmouse_face, string, Qnil);
27391 if (NILP (b))
27392 begpos = 0;
27393 else
27394 begpos = XINT (b);
27395
27396 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27397 if (NILP (e))
27398 endpos = SCHARS (string);
27399 else
27400 endpos = XINT (e);
27401
27402 /* Calculate the glyph position GPOS of GLYPH in the
27403 displayed string, relative to the beginning of the
27404 highlighted part of the string.
27405
27406 Note: GPOS is different from CHARPOS. CHARPOS is the
27407 position of GLYPH in the internal string object. A mode
27408 line string format has structures which are converted to
27409 a flattened string by the Emacs Lisp interpreter. The
27410 internal string is an element of those structures. The
27411 displayed string is the flattened string. */
27412 tmp_glyph = row_start_glyph;
27413 while (tmp_glyph < glyph
27414 && (!(EQ (tmp_glyph->object, glyph->object)
27415 && begpos <= tmp_glyph->charpos
27416 && tmp_glyph->charpos < endpos)))
27417 tmp_glyph++;
27418 gpos = glyph - tmp_glyph;
27419
27420 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27421 the highlighted part of the displayed string to which
27422 GLYPH belongs. Note: GSEQ_LENGTH is different from
27423 SCHARS (STRING), because the latter returns the length of
27424 the internal string. */
27425 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27426 tmp_glyph > glyph
27427 && (!(EQ (tmp_glyph->object, glyph->object)
27428 && begpos <= tmp_glyph->charpos
27429 && tmp_glyph->charpos < endpos));
27430 tmp_glyph--)
27431 ;
27432 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27433
27434 /* Calculate the total pixel width of all the glyphs between
27435 the beginning of the highlighted area and GLYPH. */
27436 total_pixel_width = 0;
27437 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27438 total_pixel_width += tmp_glyph->pixel_width;
27439
27440 /* Pre calculation of re-rendering position. Note: X is in
27441 column units here, after the call to mode_line_string or
27442 marginal_area_string. */
27443 hpos = x - gpos;
27444 vpos = (area == ON_MODE_LINE
27445 ? (w->current_matrix)->nrows - 1
27446 : 0);
27447
27448 /* If GLYPH's position is included in the region that is
27449 already drawn in mouse face, we have nothing to do. */
27450 if ( EQ (window, hlinfo->mouse_face_window)
27451 && (!row->reversed_p
27452 ? (hlinfo->mouse_face_beg_col <= hpos
27453 && hpos < hlinfo->mouse_face_end_col)
27454 /* In R2L rows we swap BEG and END, see below. */
27455 : (hlinfo->mouse_face_end_col <= hpos
27456 && hpos < hlinfo->mouse_face_beg_col))
27457 && hlinfo->mouse_face_beg_row == vpos )
27458 return;
27459
27460 if (clear_mouse_face (hlinfo))
27461 cursor = No_Cursor;
27462
27463 if (!row->reversed_p)
27464 {
27465 hlinfo->mouse_face_beg_col = hpos;
27466 hlinfo->mouse_face_beg_x = original_x_pixel
27467 - (total_pixel_width + dx);
27468 hlinfo->mouse_face_end_col = hpos + gseq_length;
27469 hlinfo->mouse_face_end_x = 0;
27470 }
27471 else
27472 {
27473 /* In R2L rows, show_mouse_face expects BEG and END
27474 coordinates to be swapped. */
27475 hlinfo->mouse_face_end_col = hpos;
27476 hlinfo->mouse_face_end_x = original_x_pixel
27477 - (total_pixel_width + dx);
27478 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27479 hlinfo->mouse_face_beg_x = 0;
27480 }
27481
27482 hlinfo->mouse_face_beg_row = vpos;
27483 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27484 hlinfo->mouse_face_beg_y = 0;
27485 hlinfo->mouse_face_end_y = 0;
27486 hlinfo->mouse_face_past_end = 0;
27487 hlinfo->mouse_face_window = window;
27488
27489 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27490 charpos,
27491 0, 0, 0,
27492 &ignore,
27493 glyph->face_id,
27494 1);
27495 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27496
27497 if (NILP (pointer))
27498 pointer = Qhand;
27499 }
27500 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27501 clear_mouse_face (hlinfo);
27502 }
27503 #ifdef HAVE_WINDOW_SYSTEM
27504 if (FRAME_WINDOW_P (f))
27505 define_frame_cursor1 (f, cursor, pointer);
27506 #endif
27507 }
27508
27509
27510 /* EXPORT:
27511 Take proper action when the mouse has moved to position X, Y on
27512 frame F as regards highlighting characters that have mouse-face
27513 properties. Also de-highlighting chars where the mouse was before.
27514 X and Y can be negative or out of range. */
27515
27516 void
27517 note_mouse_highlight (struct frame *f, int x, int y)
27518 {
27519 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27520 enum window_part part = ON_NOTHING;
27521 Lisp_Object window;
27522 struct window *w;
27523 Cursor cursor = No_Cursor;
27524 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27525 struct buffer *b;
27526
27527 /* When a menu is active, don't highlight because this looks odd. */
27528 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27529 if (popup_activated ())
27530 return;
27531 #endif
27532
27533 if (NILP (Vmouse_highlight)
27534 || !f->glyphs_initialized_p
27535 || f->pointer_invisible)
27536 return;
27537
27538 hlinfo->mouse_face_mouse_x = x;
27539 hlinfo->mouse_face_mouse_y = y;
27540 hlinfo->mouse_face_mouse_frame = f;
27541
27542 if (hlinfo->mouse_face_defer)
27543 return;
27544
27545 /* Which window is that in? */
27546 window = window_from_coordinates (f, x, y, &part, 1);
27547
27548 /* If displaying active text in another window, clear that. */
27549 if (! EQ (window, hlinfo->mouse_face_window)
27550 /* Also clear if we move out of text area in same window. */
27551 || (!NILP (hlinfo->mouse_face_window)
27552 && !NILP (window)
27553 && part != ON_TEXT
27554 && part != ON_MODE_LINE
27555 && part != ON_HEADER_LINE))
27556 clear_mouse_face (hlinfo);
27557
27558 /* Not on a window -> return. */
27559 if (!WINDOWP (window))
27560 return;
27561
27562 /* Reset help_echo_string. It will get recomputed below. */
27563 help_echo_string = Qnil;
27564
27565 /* Convert to window-relative pixel coordinates. */
27566 w = XWINDOW (window);
27567 frame_to_window_pixel_xy (w, &x, &y);
27568
27569 #ifdef HAVE_WINDOW_SYSTEM
27570 /* Handle tool-bar window differently since it doesn't display a
27571 buffer. */
27572 if (EQ (window, f->tool_bar_window))
27573 {
27574 note_tool_bar_highlight (f, x, y);
27575 return;
27576 }
27577 #endif
27578
27579 /* Mouse is on the mode, header line or margin? */
27580 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27581 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27582 {
27583 note_mode_line_or_margin_highlight (window, x, y, part);
27584 return;
27585 }
27586
27587 #ifdef HAVE_WINDOW_SYSTEM
27588 if (part == ON_VERTICAL_BORDER)
27589 {
27590 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27591 help_echo_string = build_string ("drag-mouse-1: resize");
27592 }
27593 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27594 || part == ON_SCROLL_BAR)
27595 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27596 else
27597 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27598 #endif
27599
27600 /* Are we in a window whose display is up to date?
27601 And verify the buffer's text has not changed. */
27602 b = XBUFFER (w->buffer);
27603 if (part == ON_TEXT
27604 && w->window_end_valid
27605 && w->last_modified == BUF_MODIFF (b)
27606 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27607 {
27608 int hpos, vpos, dx, dy, area = LAST_AREA;
27609 ptrdiff_t pos;
27610 struct glyph *glyph;
27611 Lisp_Object object;
27612 Lisp_Object mouse_face = Qnil, position;
27613 Lisp_Object *overlay_vec = NULL;
27614 ptrdiff_t i, noverlays;
27615 struct buffer *obuf;
27616 ptrdiff_t obegv, ozv;
27617 int same_region;
27618
27619 /* Find the glyph under X/Y. */
27620 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27621
27622 #ifdef HAVE_WINDOW_SYSTEM
27623 /* Look for :pointer property on image. */
27624 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27625 {
27626 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27627 if (img != NULL && IMAGEP (img->spec))
27628 {
27629 Lisp_Object image_map, hotspot;
27630 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27631 !NILP (image_map))
27632 && (hotspot = find_hot_spot (image_map,
27633 glyph->slice.img.x + dx,
27634 glyph->slice.img.y + dy),
27635 CONSP (hotspot))
27636 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27637 {
27638 Lisp_Object plist;
27639
27640 /* Could check XCAR (hotspot) to see if we enter/leave
27641 this hot-spot.
27642 If so, we could look for mouse-enter, mouse-leave
27643 properties in PLIST (and do something...). */
27644 hotspot = XCDR (hotspot);
27645 if (CONSP (hotspot)
27646 && (plist = XCAR (hotspot), CONSP (plist)))
27647 {
27648 pointer = Fplist_get (plist, Qpointer);
27649 if (NILP (pointer))
27650 pointer = Qhand;
27651 help_echo_string = Fplist_get (plist, Qhelp_echo);
27652 if (!NILP (help_echo_string))
27653 {
27654 help_echo_window = window;
27655 help_echo_object = glyph->object;
27656 help_echo_pos = glyph->charpos;
27657 }
27658 }
27659 }
27660 if (NILP (pointer))
27661 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27662 }
27663 }
27664 #endif /* HAVE_WINDOW_SYSTEM */
27665
27666 /* Clear mouse face if X/Y not over text. */
27667 if (glyph == NULL
27668 || area != TEXT_AREA
27669 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27670 /* Glyph's OBJECT is an integer for glyphs inserted by the
27671 display engine for its internal purposes, like truncation
27672 and continuation glyphs and blanks beyond the end of
27673 line's text on text terminals. If we are over such a
27674 glyph, we are not over any text. */
27675 || INTEGERP (glyph->object)
27676 /* R2L rows have a stretch glyph at their front, which
27677 stands for no text, whereas L2R rows have no glyphs at
27678 all beyond the end of text. Treat such stretch glyphs
27679 like we do with NULL glyphs in L2R rows. */
27680 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27681 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27682 && glyph->type == STRETCH_GLYPH
27683 && glyph->avoid_cursor_p))
27684 {
27685 if (clear_mouse_face (hlinfo))
27686 cursor = No_Cursor;
27687 #ifdef HAVE_WINDOW_SYSTEM
27688 if (FRAME_WINDOW_P (f) && NILP (pointer))
27689 {
27690 if (area != TEXT_AREA)
27691 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27692 else
27693 pointer = Vvoid_text_area_pointer;
27694 }
27695 #endif
27696 goto set_cursor;
27697 }
27698
27699 pos = glyph->charpos;
27700 object = glyph->object;
27701 if (!STRINGP (object) && !BUFFERP (object))
27702 goto set_cursor;
27703
27704 /* If we get an out-of-range value, return now; avoid an error. */
27705 if (BUFFERP (object) && pos > BUF_Z (b))
27706 goto set_cursor;
27707
27708 /* Make the window's buffer temporarily current for
27709 overlays_at and compute_char_face. */
27710 obuf = current_buffer;
27711 current_buffer = b;
27712 obegv = BEGV;
27713 ozv = ZV;
27714 BEGV = BEG;
27715 ZV = Z;
27716
27717 /* Is this char mouse-active or does it have help-echo? */
27718 position = make_number (pos);
27719
27720 if (BUFFERP (object))
27721 {
27722 /* Put all the overlays we want in a vector in overlay_vec. */
27723 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27724 /* Sort overlays into increasing priority order. */
27725 noverlays = sort_overlays (overlay_vec, noverlays, w);
27726 }
27727 else
27728 noverlays = 0;
27729
27730 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27731
27732 if (same_region)
27733 cursor = No_Cursor;
27734
27735 /* Check mouse-face highlighting. */
27736 if (! same_region
27737 /* If there exists an overlay with mouse-face overlapping
27738 the one we are currently highlighting, we have to
27739 check if we enter the overlapping overlay, and then
27740 highlight only that. */
27741 || (OVERLAYP (hlinfo->mouse_face_overlay)
27742 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27743 {
27744 /* Find the highest priority overlay with a mouse-face. */
27745 Lisp_Object overlay = Qnil;
27746 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27747 {
27748 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27749 if (!NILP (mouse_face))
27750 overlay = overlay_vec[i];
27751 }
27752
27753 /* If we're highlighting the same overlay as before, there's
27754 no need to do that again. */
27755 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27756 goto check_help_echo;
27757 hlinfo->mouse_face_overlay = overlay;
27758
27759 /* Clear the display of the old active region, if any. */
27760 if (clear_mouse_face (hlinfo))
27761 cursor = No_Cursor;
27762
27763 /* If no overlay applies, get a text property. */
27764 if (NILP (overlay))
27765 mouse_face = Fget_text_property (position, Qmouse_face, object);
27766
27767 /* Next, compute the bounds of the mouse highlighting and
27768 display it. */
27769 if (!NILP (mouse_face) && STRINGP (object))
27770 {
27771 /* The mouse-highlighting comes from a display string
27772 with a mouse-face. */
27773 Lisp_Object s, e;
27774 ptrdiff_t ignore;
27775
27776 s = Fprevious_single_property_change
27777 (make_number (pos + 1), Qmouse_face, object, Qnil);
27778 e = Fnext_single_property_change
27779 (position, Qmouse_face, object, Qnil);
27780 if (NILP (s))
27781 s = make_number (0);
27782 if (NILP (e))
27783 e = make_number (SCHARS (object) - 1);
27784 mouse_face_from_string_pos (w, hlinfo, object,
27785 XINT (s), XINT (e));
27786 hlinfo->mouse_face_past_end = 0;
27787 hlinfo->mouse_face_window = window;
27788 hlinfo->mouse_face_face_id
27789 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27790 glyph->face_id, 1);
27791 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27792 cursor = No_Cursor;
27793 }
27794 else
27795 {
27796 /* The mouse-highlighting, if any, comes from an overlay
27797 or text property in the buffer. */
27798 Lisp_Object buffer IF_LINT (= Qnil);
27799 Lisp_Object disp_string IF_LINT (= Qnil);
27800
27801 if (STRINGP (object))
27802 {
27803 /* If we are on a display string with no mouse-face,
27804 check if the text under it has one. */
27805 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27806 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27807 pos = string_buffer_position (object, start);
27808 if (pos > 0)
27809 {
27810 mouse_face = get_char_property_and_overlay
27811 (make_number (pos), Qmouse_face, w->buffer, &overlay);
27812 buffer = w->buffer;
27813 disp_string = object;
27814 }
27815 }
27816 else
27817 {
27818 buffer = object;
27819 disp_string = Qnil;
27820 }
27821
27822 if (!NILP (mouse_face))
27823 {
27824 Lisp_Object before, after;
27825 Lisp_Object before_string, after_string;
27826 /* To correctly find the limits of mouse highlight
27827 in a bidi-reordered buffer, we must not use the
27828 optimization of limiting the search in
27829 previous-single-property-change and
27830 next-single-property-change, because
27831 rows_from_pos_range needs the real start and end
27832 positions to DTRT in this case. That's because
27833 the first row visible in a window does not
27834 necessarily display the character whose position
27835 is the smallest. */
27836 Lisp_Object lim1 =
27837 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27838 ? Fmarker_position (w->start)
27839 : Qnil;
27840 Lisp_Object lim2 =
27841 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27842 ? make_number (BUF_Z (XBUFFER (buffer))
27843 - XFASTINT (w->window_end_pos))
27844 : Qnil;
27845
27846 if (NILP (overlay))
27847 {
27848 /* Handle the text property case. */
27849 before = Fprevious_single_property_change
27850 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27851 after = Fnext_single_property_change
27852 (make_number (pos), Qmouse_face, buffer, lim2);
27853 before_string = after_string = Qnil;
27854 }
27855 else
27856 {
27857 /* Handle the overlay case. */
27858 before = Foverlay_start (overlay);
27859 after = Foverlay_end (overlay);
27860 before_string = Foverlay_get (overlay, Qbefore_string);
27861 after_string = Foverlay_get (overlay, Qafter_string);
27862
27863 if (!STRINGP (before_string)) before_string = Qnil;
27864 if (!STRINGP (after_string)) after_string = Qnil;
27865 }
27866
27867 mouse_face_from_buffer_pos (window, hlinfo, pos,
27868 NILP (before)
27869 ? 1
27870 : XFASTINT (before),
27871 NILP (after)
27872 ? BUF_Z (XBUFFER (buffer))
27873 : XFASTINT (after),
27874 before_string, after_string,
27875 disp_string);
27876 cursor = No_Cursor;
27877 }
27878 }
27879 }
27880
27881 check_help_echo:
27882
27883 /* Look for a `help-echo' property. */
27884 if (NILP (help_echo_string)) {
27885 Lisp_Object help, overlay;
27886
27887 /* Check overlays first. */
27888 help = overlay = Qnil;
27889 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27890 {
27891 overlay = overlay_vec[i];
27892 help = Foverlay_get (overlay, Qhelp_echo);
27893 }
27894
27895 if (!NILP (help))
27896 {
27897 help_echo_string = help;
27898 help_echo_window = window;
27899 help_echo_object = overlay;
27900 help_echo_pos = pos;
27901 }
27902 else
27903 {
27904 Lisp_Object obj = glyph->object;
27905 ptrdiff_t charpos = glyph->charpos;
27906
27907 /* Try text properties. */
27908 if (STRINGP (obj)
27909 && charpos >= 0
27910 && charpos < SCHARS (obj))
27911 {
27912 help = Fget_text_property (make_number (charpos),
27913 Qhelp_echo, obj);
27914 if (NILP (help))
27915 {
27916 /* If the string itself doesn't specify a help-echo,
27917 see if the buffer text ``under'' it does. */
27918 struct glyph_row *r
27919 = MATRIX_ROW (w->current_matrix, vpos);
27920 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27921 ptrdiff_t p = string_buffer_position (obj, start);
27922 if (p > 0)
27923 {
27924 help = Fget_char_property (make_number (p),
27925 Qhelp_echo, w->buffer);
27926 if (!NILP (help))
27927 {
27928 charpos = p;
27929 obj = w->buffer;
27930 }
27931 }
27932 }
27933 }
27934 else if (BUFFERP (obj)
27935 && charpos >= BEGV
27936 && charpos < ZV)
27937 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27938 obj);
27939
27940 if (!NILP (help))
27941 {
27942 help_echo_string = help;
27943 help_echo_window = window;
27944 help_echo_object = obj;
27945 help_echo_pos = charpos;
27946 }
27947 }
27948 }
27949
27950 #ifdef HAVE_WINDOW_SYSTEM
27951 /* Look for a `pointer' property. */
27952 if (FRAME_WINDOW_P (f) && NILP (pointer))
27953 {
27954 /* Check overlays first. */
27955 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
27956 pointer = Foverlay_get (overlay_vec[i], Qpointer);
27957
27958 if (NILP (pointer))
27959 {
27960 Lisp_Object obj = glyph->object;
27961 ptrdiff_t charpos = glyph->charpos;
27962
27963 /* Try text properties. */
27964 if (STRINGP (obj)
27965 && charpos >= 0
27966 && charpos < SCHARS (obj))
27967 {
27968 pointer = Fget_text_property (make_number (charpos),
27969 Qpointer, obj);
27970 if (NILP (pointer))
27971 {
27972 /* If the string itself doesn't specify a pointer,
27973 see if the buffer text ``under'' it does. */
27974 struct glyph_row *r
27975 = MATRIX_ROW (w->current_matrix, vpos);
27976 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27977 ptrdiff_t p = string_buffer_position (obj, start);
27978 if (p > 0)
27979 pointer = Fget_char_property (make_number (p),
27980 Qpointer, w->buffer);
27981 }
27982 }
27983 else if (BUFFERP (obj)
27984 && charpos >= BEGV
27985 && charpos < ZV)
27986 pointer = Fget_text_property (make_number (charpos),
27987 Qpointer, obj);
27988 }
27989 }
27990 #endif /* HAVE_WINDOW_SYSTEM */
27991
27992 BEGV = obegv;
27993 ZV = ozv;
27994 current_buffer = obuf;
27995 }
27996
27997 set_cursor:
27998
27999 #ifdef HAVE_WINDOW_SYSTEM
28000 if (FRAME_WINDOW_P (f))
28001 define_frame_cursor1 (f, cursor, pointer);
28002 #else
28003 /* This is here to prevent a compiler error, about "label at end of
28004 compound statement". */
28005 return;
28006 #endif
28007 }
28008
28009
28010 /* EXPORT for RIF:
28011 Clear any mouse-face on window W. This function is part of the
28012 redisplay interface, and is called from try_window_id and similar
28013 functions to ensure the mouse-highlight is off. */
28014
28015 void
28016 x_clear_window_mouse_face (struct window *w)
28017 {
28018 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28019 Lisp_Object window;
28020
28021 block_input ();
28022 XSETWINDOW (window, w);
28023 if (EQ (window, hlinfo->mouse_face_window))
28024 clear_mouse_face (hlinfo);
28025 unblock_input ();
28026 }
28027
28028
28029 /* EXPORT:
28030 Just discard the mouse face information for frame F, if any.
28031 This is used when the size of F is changed. */
28032
28033 void
28034 cancel_mouse_face (struct frame *f)
28035 {
28036 Lisp_Object window;
28037 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28038
28039 window = hlinfo->mouse_face_window;
28040 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28041 {
28042 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28043 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28044 hlinfo->mouse_face_window = Qnil;
28045 }
28046 }
28047
28048
28049 \f
28050 /***********************************************************************
28051 Exposure Events
28052 ***********************************************************************/
28053
28054 #ifdef HAVE_WINDOW_SYSTEM
28055
28056 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28057 which intersects rectangle R. R is in window-relative coordinates. */
28058
28059 static void
28060 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28061 enum glyph_row_area area)
28062 {
28063 struct glyph *first = row->glyphs[area];
28064 struct glyph *end = row->glyphs[area] + row->used[area];
28065 struct glyph *last;
28066 int first_x, start_x, x;
28067
28068 if (area == TEXT_AREA && row->fill_line_p)
28069 /* If row extends face to end of line write the whole line. */
28070 draw_glyphs (w, 0, row, area,
28071 0, row->used[area],
28072 DRAW_NORMAL_TEXT, 0);
28073 else
28074 {
28075 /* Set START_X to the window-relative start position for drawing glyphs of
28076 AREA. The first glyph of the text area can be partially visible.
28077 The first glyphs of other areas cannot. */
28078 start_x = window_box_left_offset (w, area);
28079 x = start_x;
28080 if (area == TEXT_AREA)
28081 x += row->x;
28082
28083 /* Find the first glyph that must be redrawn. */
28084 while (first < end
28085 && x + first->pixel_width < r->x)
28086 {
28087 x += first->pixel_width;
28088 ++first;
28089 }
28090
28091 /* Find the last one. */
28092 last = first;
28093 first_x = x;
28094 while (last < end
28095 && x < r->x + r->width)
28096 {
28097 x += last->pixel_width;
28098 ++last;
28099 }
28100
28101 /* Repaint. */
28102 if (last > first)
28103 draw_glyphs (w, first_x - start_x, row, area,
28104 first - row->glyphs[area], last - row->glyphs[area],
28105 DRAW_NORMAL_TEXT, 0);
28106 }
28107 }
28108
28109
28110 /* Redraw the parts of the glyph row ROW on window W intersecting
28111 rectangle R. R is in window-relative coordinates. Value is
28112 non-zero if mouse-face was overwritten. */
28113
28114 static int
28115 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28116 {
28117 eassert (row->enabled_p);
28118
28119 if (row->mode_line_p || w->pseudo_window_p)
28120 draw_glyphs (w, 0, row, TEXT_AREA,
28121 0, row->used[TEXT_AREA],
28122 DRAW_NORMAL_TEXT, 0);
28123 else
28124 {
28125 if (row->used[LEFT_MARGIN_AREA])
28126 expose_area (w, row, r, LEFT_MARGIN_AREA);
28127 if (row->used[TEXT_AREA])
28128 expose_area (w, row, r, TEXT_AREA);
28129 if (row->used[RIGHT_MARGIN_AREA])
28130 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28131 draw_row_fringe_bitmaps (w, row);
28132 }
28133
28134 return row->mouse_face_p;
28135 }
28136
28137
28138 /* Redraw those parts of glyphs rows during expose event handling that
28139 overlap other rows. Redrawing of an exposed line writes over parts
28140 of lines overlapping that exposed line; this function fixes that.
28141
28142 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28143 row in W's current matrix that is exposed and overlaps other rows.
28144 LAST_OVERLAPPING_ROW is the last such row. */
28145
28146 static void
28147 expose_overlaps (struct window *w,
28148 struct glyph_row *first_overlapping_row,
28149 struct glyph_row *last_overlapping_row,
28150 XRectangle *r)
28151 {
28152 struct glyph_row *row;
28153
28154 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28155 if (row->overlapping_p)
28156 {
28157 eassert (row->enabled_p && !row->mode_line_p);
28158
28159 row->clip = r;
28160 if (row->used[LEFT_MARGIN_AREA])
28161 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28162
28163 if (row->used[TEXT_AREA])
28164 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28165
28166 if (row->used[RIGHT_MARGIN_AREA])
28167 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28168 row->clip = NULL;
28169 }
28170 }
28171
28172
28173 /* Return non-zero if W's cursor intersects rectangle R. */
28174
28175 static int
28176 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28177 {
28178 XRectangle cr, result;
28179 struct glyph *cursor_glyph;
28180 struct glyph_row *row;
28181
28182 if (w->phys_cursor.vpos >= 0
28183 && w->phys_cursor.vpos < w->current_matrix->nrows
28184 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28185 row->enabled_p)
28186 && row->cursor_in_fringe_p)
28187 {
28188 /* Cursor is in the fringe. */
28189 cr.x = window_box_right_offset (w,
28190 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28191 ? RIGHT_MARGIN_AREA
28192 : TEXT_AREA));
28193 cr.y = row->y;
28194 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28195 cr.height = row->height;
28196 return x_intersect_rectangles (&cr, r, &result);
28197 }
28198
28199 cursor_glyph = get_phys_cursor_glyph (w);
28200 if (cursor_glyph)
28201 {
28202 /* r is relative to W's box, but w->phys_cursor.x is relative
28203 to left edge of W's TEXT area. Adjust it. */
28204 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28205 cr.y = w->phys_cursor.y;
28206 cr.width = cursor_glyph->pixel_width;
28207 cr.height = w->phys_cursor_height;
28208 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28209 I assume the effect is the same -- and this is portable. */
28210 return x_intersect_rectangles (&cr, r, &result);
28211 }
28212 /* If we don't understand the format, pretend we're not in the hot-spot. */
28213 return 0;
28214 }
28215
28216
28217 /* EXPORT:
28218 Draw a vertical window border to the right of window W if W doesn't
28219 have vertical scroll bars. */
28220
28221 void
28222 x_draw_vertical_border (struct window *w)
28223 {
28224 struct frame *f = XFRAME (WINDOW_FRAME (w));
28225
28226 /* We could do better, if we knew what type of scroll-bar the adjacent
28227 windows (on either side) have... But we don't :-(
28228 However, I think this works ok. ++KFS 2003-04-25 */
28229
28230 /* Redraw borders between horizontally adjacent windows. Don't
28231 do it for frames with vertical scroll bars because either the
28232 right scroll bar of a window, or the left scroll bar of its
28233 neighbor will suffice as a border. */
28234 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28235 return;
28236
28237 if (!WINDOW_RIGHTMOST_P (w)
28238 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28239 {
28240 int x0, x1, y0, y1;
28241
28242 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28243 y1 -= 1;
28244
28245 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28246 x1 -= 1;
28247
28248 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28249 }
28250 else if (!WINDOW_LEFTMOST_P (w)
28251 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28252 {
28253 int x0, x1, y0, y1;
28254
28255 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28256 y1 -= 1;
28257
28258 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28259 x0 -= 1;
28260
28261 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28262 }
28263 }
28264
28265
28266 /* Redraw the part of window W intersection rectangle FR. Pixel
28267 coordinates in FR are frame-relative. Call this function with
28268 input blocked. Value is non-zero if the exposure overwrites
28269 mouse-face. */
28270
28271 static int
28272 expose_window (struct window *w, XRectangle *fr)
28273 {
28274 struct frame *f = XFRAME (w->frame);
28275 XRectangle wr, r;
28276 int mouse_face_overwritten_p = 0;
28277
28278 /* If window is not yet fully initialized, do nothing. This can
28279 happen when toolkit scroll bars are used and a window is split.
28280 Reconfiguring the scroll bar will generate an expose for a newly
28281 created window. */
28282 if (w->current_matrix == NULL)
28283 return 0;
28284
28285 /* When we're currently updating the window, display and current
28286 matrix usually don't agree. Arrange for a thorough display
28287 later. */
28288 if (w == updated_window)
28289 {
28290 SET_FRAME_GARBAGED (f);
28291 return 0;
28292 }
28293
28294 /* Frame-relative pixel rectangle of W. */
28295 wr.x = WINDOW_LEFT_EDGE_X (w);
28296 wr.y = WINDOW_TOP_EDGE_Y (w);
28297 wr.width = WINDOW_TOTAL_WIDTH (w);
28298 wr.height = WINDOW_TOTAL_HEIGHT (w);
28299
28300 if (x_intersect_rectangles (fr, &wr, &r))
28301 {
28302 int yb = window_text_bottom_y (w);
28303 struct glyph_row *row;
28304 int cursor_cleared_p, phys_cursor_on_p;
28305 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28306
28307 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28308 r.x, r.y, r.width, r.height));
28309
28310 /* Convert to window coordinates. */
28311 r.x -= WINDOW_LEFT_EDGE_X (w);
28312 r.y -= WINDOW_TOP_EDGE_Y (w);
28313
28314 /* Turn off the cursor. */
28315 if (!w->pseudo_window_p
28316 && phys_cursor_in_rect_p (w, &r))
28317 {
28318 x_clear_cursor (w);
28319 cursor_cleared_p = 1;
28320 }
28321 else
28322 cursor_cleared_p = 0;
28323
28324 /* If the row containing the cursor extends face to end of line,
28325 then expose_area might overwrite the cursor outside the
28326 rectangle and thus notice_overwritten_cursor might clear
28327 w->phys_cursor_on_p. We remember the original value and
28328 check later if it is changed. */
28329 phys_cursor_on_p = w->phys_cursor_on_p;
28330
28331 /* Update lines intersecting rectangle R. */
28332 first_overlapping_row = last_overlapping_row = NULL;
28333 for (row = w->current_matrix->rows;
28334 row->enabled_p;
28335 ++row)
28336 {
28337 int y0 = row->y;
28338 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28339
28340 if ((y0 >= r.y && y0 < r.y + r.height)
28341 || (y1 > r.y && y1 < r.y + r.height)
28342 || (r.y >= y0 && r.y < y1)
28343 || (r.y + r.height > y0 && r.y + r.height < y1))
28344 {
28345 /* A header line may be overlapping, but there is no need
28346 to fix overlapping areas for them. KFS 2005-02-12 */
28347 if (row->overlapping_p && !row->mode_line_p)
28348 {
28349 if (first_overlapping_row == NULL)
28350 first_overlapping_row = row;
28351 last_overlapping_row = row;
28352 }
28353
28354 row->clip = fr;
28355 if (expose_line (w, row, &r))
28356 mouse_face_overwritten_p = 1;
28357 row->clip = NULL;
28358 }
28359 else if (row->overlapping_p)
28360 {
28361 /* We must redraw a row overlapping the exposed area. */
28362 if (y0 < r.y
28363 ? y0 + row->phys_height > r.y
28364 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28365 {
28366 if (first_overlapping_row == NULL)
28367 first_overlapping_row = row;
28368 last_overlapping_row = row;
28369 }
28370 }
28371
28372 if (y1 >= yb)
28373 break;
28374 }
28375
28376 /* Display the mode line if there is one. */
28377 if (WINDOW_WANTS_MODELINE_P (w)
28378 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28379 row->enabled_p)
28380 && row->y < r.y + r.height)
28381 {
28382 if (expose_line (w, row, &r))
28383 mouse_face_overwritten_p = 1;
28384 }
28385
28386 if (!w->pseudo_window_p)
28387 {
28388 /* Fix the display of overlapping rows. */
28389 if (first_overlapping_row)
28390 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28391 fr);
28392
28393 /* Draw border between windows. */
28394 x_draw_vertical_border (w);
28395
28396 /* Turn the cursor on again. */
28397 if (cursor_cleared_p
28398 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28399 update_window_cursor (w, 1);
28400 }
28401 }
28402
28403 return mouse_face_overwritten_p;
28404 }
28405
28406
28407
28408 /* Redraw (parts) of all windows in the window tree rooted at W that
28409 intersect R. R contains frame pixel coordinates. Value is
28410 non-zero if the exposure overwrites mouse-face. */
28411
28412 static int
28413 expose_window_tree (struct window *w, XRectangle *r)
28414 {
28415 struct frame *f = XFRAME (w->frame);
28416 int mouse_face_overwritten_p = 0;
28417
28418 while (w && !FRAME_GARBAGED_P (f))
28419 {
28420 if (!NILP (w->hchild))
28421 mouse_face_overwritten_p
28422 |= expose_window_tree (XWINDOW (w->hchild), r);
28423 else if (!NILP (w->vchild))
28424 mouse_face_overwritten_p
28425 |= expose_window_tree (XWINDOW (w->vchild), r);
28426 else
28427 mouse_face_overwritten_p |= expose_window (w, r);
28428
28429 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28430 }
28431
28432 return mouse_face_overwritten_p;
28433 }
28434
28435
28436 /* EXPORT:
28437 Redisplay an exposed area of frame F. X and Y are the upper-left
28438 corner of the exposed rectangle. W and H are width and height of
28439 the exposed area. All are pixel values. W or H zero means redraw
28440 the entire frame. */
28441
28442 void
28443 expose_frame (struct frame *f, int x, int y, int w, int h)
28444 {
28445 XRectangle r;
28446 int mouse_face_overwritten_p = 0;
28447
28448 TRACE ((stderr, "expose_frame "));
28449
28450 /* No need to redraw if frame will be redrawn soon. */
28451 if (FRAME_GARBAGED_P (f))
28452 {
28453 TRACE ((stderr, " garbaged\n"));
28454 return;
28455 }
28456
28457 /* If basic faces haven't been realized yet, there is no point in
28458 trying to redraw anything. This can happen when we get an expose
28459 event while Emacs is starting, e.g. by moving another window. */
28460 if (FRAME_FACE_CACHE (f) == NULL
28461 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28462 {
28463 TRACE ((stderr, " no faces\n"));
28464 return;
28465 }
28466
28467 if (w == 0 || h == 0)
28468 {
28469 r.x = r.y = 0;
28470 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28471 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28472 }
28473 else
28474 {
28475 r.x = x;
28476 r.y = y;
28477 r.width = w;
28478 r.height = h;
28479 }
28480
28481 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28482 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28483
28484 if (WINDOWP (f->tool_bar_window))
28485 mouse_face_overwritten_p
28486 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28487
28488 #ifdef HAVE_X_WINDOWS
28489 #ifndef MSDOS
28490 #ifndef USE_X_TOOLKIT
28491 if (WINDOWP (f->menu_bar_window))
28492 mouse_face_overwritten_p
28493 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28494 #endif /* not USE_X_TOOLKIT */
28495 #endif
28496 #endif
28497
28498 /* Some window managers support a focus-follows-mouse style with
28499 delayed raising of frames. Imagine a partially obscured frame,
28500 and moving the mouse into partially obscured mouse-face on that
28501 frame. The visible part of the mouse-face will be highlighted,
28502 then the WM raises the obscured frame. With at least one WM, KDE
28503 2.1, Emacs is not getting any event for the raising of the frame
28504 (even tried with SubstructureRedirectMask), only Expose events.
28505 These expose events will draw text normally, i.e. not
28506 highlighted. Which means we must redo the highlight here.
28507 Subsume it under ``we love X''. --gerd 2001-08-15 */
28508 /* Included in Windows version because Windows most likely does not
28509 do the right thing if any third party tool offers
28510 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28511 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28512 {
28513 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28514 if (f == hlinfo->mouse_face_mouse_frame)
28515 {
28516 int mouse_x = hlinfo->mouse_face_mouse_x;
28517 int mouse_y = hlinfo->mouse_face_mouse_y;
28518 clear_mouse_face (hlinfo);
28519 note_mouse_highlight (f, mouse_x, mouse_y);
28520 }
28521 }
28522 }
28523
28524
28525 /* EXPORT:
28526 Determine the intersection of two rectangles R1 and R2. Return
28527 the intersection in *RESULT. Value is non-zero if RESULT is not
28528 empty. */
28529
28530 int
28531 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28532 {
28533 XRectangle *left, *right;
28534 XRectangle *upper, *lower;
28535 int intersection_p = 0;
28536
28537 /* Rearrange so that R1 is the left-most rectangle. */
28538 if (r1->x < r2->x)
28539 left = r1, right = r2;
28540 else
28541 left = r2, right = r1;
28542
28543 /* X0 of the intersection is right.x0, if this is inside R1,
28544 otherwise there is no intersection. */
28545 if (right->x <= left->x + left->width)
28546 {
28547 result->x = right->x;
28548
28549 /* The right end of the intersection is the minimum of
28550 the right ends of left and right. */
28551 result->width = (min (left->x + left->width, right->x + right->width)
28552 - result->x);
28553
28554 /* Same game for Y. */
28555 if (r1->y < r2->y)
28556 upper = r1, lower = r2;
28557 else
28558 upper = r2, lower = r1;
28559
28560 /* The upper end of the intersection is lower.y0, if this is inside
28561 of upper. Otherwise, there is no intersection. */
28562 if (lower->y <= upper->y + upper->height)
28563 {
28564 result->y = lower->y;
28565
28566 /* The lower end of the intersection is the minimum of the lower
28567 ends of upper and lower. */
28568 result->height = (min (lower->y + lower->height,
28569 upper->y + upper->height)
28570 - result->y);
28571 intersection_p = 1;
28572 }
28573 }
28574
28575 return intersection_p;
28576 }
28577
28578 #endif /* HAVE_WINDOW_SYSTEM */
28579
28580 \f
28581 /***********************************************************************
28582 Initialization
28583 ***********************************************************************/
28584
28585 void
28586 syms_of_xdisp (void)
28587 {
28588 Vwith_echo_area_save_vector = Qnil;
28589 staticpro (&Vwith_echo_area_save_vector);
28590
28591 Vmessage_stack = Qnil;
28592 staticpro (&Vmessage_stack);
28593
28594 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28595 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
28596
28597 message_dolog_marker1 = Fmake_marker ();
28598 staticpro (&message_dolog_marker1);
28599 message_dolog_marker2 = Fmake_marker ();
28600 staticpro (&message_dolog_marker2);
28601 message_dolog_marker3 = Fmake_marker ();
28602 staticpro (&message_dolog_marker3);
28603
28604 #ifdef GLYPH_DEBUG
28605 defsubr (&Sdump_frame_glyph_matrix);
28606 defsubr (&Sdump_glyph_matrix);
28607 defsubr (&Sdump_glyph_row);
28608 defsubr (&Sdump_tool_bar_row);
28609 defsubr (&Strace_redisplay);
28610 defsubr (&Strace_to_stderr);
28611 #endif
28612 #ifdef HAVE_WINDOW_SYSTEM
28613 defsubr (&Stool_bar_lines_needed);
28614 defsubr (&Slookup_image_map);
28615 #endif
28616 defsubr (&Sformat_mode_line);
28617 defsubr (&Sinvisible_p);
28618 defsubr (&Scurrent_bidi_paragraph_direction);
28619
28620 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28621 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28622 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28623 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28624 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28625 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28626 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28627 DEFSYM (Qeval, "eval");
28628 DEFSYM (QCdata, ":data");
28629 DEFSYM (Qdisplay, "display");
28630 DEFSYM (Qspace_width, "space-width");
28631 DEFSYM (Qraise, "raise");
28632 DEFSYM (Qslice, "slice");
28633 DEFSYM (Qspace, "space");
28634 DEFSYM (Qmargin, "margin");
28635 DEFSYM (Qpointer, "pointer");
28636 DEFSYM (Qleft_margin, "left-margin");
28637 DEFSYM (Qright_margin, "right-margin");
28638 DEFSYM (Qcenter, "center");
28639 DEFSYM (Qline_height, "line-height");
28640 DEFSYM (QCalign_to, ":align-to");
28641 DEFSYM (QCrelative_width, ":relative-width");
28642 DEFSYM (QCrelative_height, ":relative-height");
28643 DEFSYM (QCeval, ":eval");
28644 DEFSYM (QCpropertize, ":propertize");
28645 DEFSYM (QCfile, ":file");
28646 DEFSYM (Qfontified, "fontified");
28647 DEFSYM (Qfontification_functions, "fontification-functions");
28648 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28649 DEFSYM (Qescape_glyph, "escape-glyph");
28650 DEFSYM (Qnobreak_space, "nobreak-space");
28651 DEFSYM (Qimage, "image");
28652 DEFSYM (Qtext, "text");
28653 DEFSYM (Qboth, "both");
28654 DEFSYM (Qboth_horiz, "both-horiz");
28655 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28656 DEFSYM (QCmap, ":map");
28657 DEFSYM (QCpointer, ":pointer");
28658 DEFSYM (Qrect, "rect");
28659 DEFSYM (Qcircle, "circle");
28660 DEFSYM (Qpoly, "poly");
28661 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28662 DEFSYM (Qgrow_only, "grow-only");
28663 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28664 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28665 DEFSYM (Qposition, "position");
28666 DEFSYM (Qbuffer_position, "buffer-position");
28667 DEFSYM (Qobject, "object");
28668 DEFSYM (Qbar, "bar");
28669 DEFSYM (Qhbar, "hbar");
28670 DEFSYM (Qbox, "box");
28671 DEFSYM (Qhollow, "hollow");
28672 DEFSYM (Qhand, "hand");
28673 DEFSYM (Qarrow, "arrow");
28674 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28675
28676 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28677 Fcons (intern_c_string ("void-variable"), Qnil)),
28678 Qnil);
28679 staticpro (&list_of_error);
28680
28681 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28682 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28683 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28684 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28685
28686 echo_buffer[0] = echo_buffer[1] = Qnil;
28687 staticpro (&echo_buffer[0]);
28688 staticpro (&echo_buffer[1]);
28689
28690 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28691 staticpro (&echo_area_buffer[0]);
28692 staticpro (&echo_area_buffer[1]);
28693
28694 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28695 staticpro (&Vmessages_buffer_name);
28696
28697 mode_line_proptrans_alist = Qnil;
28698 staticpro (&mode_line_proptrans_alist);
28699 mode_line_string_list = Qnil;
28700 staticpro (&mode_line_string_list);
28701 mode_line_string_face = Qnil;
28702 staticpro (&mode_line_string_face);
28703 mode_line_string_face_prop = Qnil;
28704 staticpro (&mode_line_string_face_prop);
28705 Vmode_line_unwind_vector = Qnil;
28706 staticpro (&Vmode_line_unwind_vector);
28707
28708 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28709
28710 help_echo_string = Qnil;
28711 staticpro (&help_echo_string);
28712 help_echo_object = Qnil;
28713 staticpro (&help_echo_object);
28714 help_echo_window = Qnil;
28715 staticpro (&help_echo_window);
28716 previous_help_echo_string = Qnil;
28717 staticpro (&previous_help_echo_string);
28718 help_echo_pos = -1;
28719
28720 DEFSYM (Qright_to_left, "right-to-left");
28721 DEFSYM (Qleft_to_right, "left-to-right");
28722
28723 #ifdef HAVE_WINDOW_SYSTEM
28724 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28725 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28726 For example, if a block cursor is over a tab, it will be drawn as
28727 wide as that tab on the display. */);
28728 x_stretch_cursor_p = 0;
28729 #endif
28730
28731 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28732 doc: /* Non-nil means highlight trailing whitespace.
28733 The face used for trailing whitespace is `trailing-whitespace'. */);
28734 Vshow_trailing_whitespace = Qnil;
28735
28736 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28737 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28738 If the value is t, Emacs highlights non-ASCII chars which have the
28739 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28740 or `escape-glyph' face respectively.
28741
28742 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28743 U+2011 (non-breaking hyphen) are affected.
28744
28745 Any other non-nil value means to display these characters as a escape
28746 glyph followed by an ordinary space or hyphen.
28747
28748 A value of nil means no special handling of these characters. */);
28749 Vnobreak_char_display = Qt;
28750
28751 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28752 doc: /* The pointer shape to show in void text areas.
28753 A value of nil means to show the text pointer. Other options are `arrow',
28754 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28755 Vvoid_text_area_pointer = Qarrow;
28756
28757 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28758 doc: /* Non-nil means don't actually do any redisplay.
28759 This is used for internal purposes. */);
28760 Vinhibit_redisplay = Qnil;
28761
28762 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28763 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28764 Vglobal_mode_string = Qnil;
28765
28766 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28767 doc: /* Marker for where to display an arrow on top of the buffer text.
28768 This must be the beginning of a line in order to work.
28769 See also `overlay-arrow-string'. */);
28770 Voverlay_arrow_position = Qnil;
28771
28772 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28773 doc: /* String to display as an arrow in non-window frames.
28774 See also `overlay-arrow-position'. */);
28775 Voverlay_arrow_string = build_pure_c_string ("=>");
28776
28777 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28778 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28779 The symbols on this list are examined during redisplay to determine
28780 where to display overlay arrows. */);
28781 Voverlay_arrow_variable_list
28782 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28783
28784 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28785 doc: /* The number of lines to try scrolling a window by when point moves out.
28786 If that fails to bring point back on frame, point is centered instead.
28787 If this is zero, point is always centered after it moves off frame.
28788 If you want scrolling to always be a line at a time, you should set
28789 `scroll-conservatively' to a large value rather than set this to 1. */);
28790
28791 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28792 doc: /* Scroll up to this many lines, to bring point back on screen.
28793 If point moves off-screen, redisplay will scroll by up to
28794 `scroll-conservatively' lines in order to bring point just barely
28795 onto the screen again. If that cannot be done, then redisplay
28796 recenters point as usual.
28797
28798 If the value is greater than 100, redisplay will never recenter point,
28799 but will always scroll just enough text to bring point into view, even
28800 if you move far away.
28801
28802 A value of zero means always recenter point if it moves off screen. */);
28803 scroll_conservatively = 0;
28804
28805 DEFVAR_INT ("scroll-margin", scroll_margin,
28806 doc: /* Number of lines of margin at the top and bottom of a window.
28807 Recenter the window whenever point gets within this many lines
28808 of the top or bottom of the window. */);
28809 scroll_margin = 0;
28810
28811 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28812 doc: /* Pixels per inch value for non-window system displays.
28813 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28814 Vdisplay_pixels_per_inch = make_float (72.0);
28815
28816 #ifdef GLYPH_DEBUG
28817 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28818 #endif
28819
28820 DEFVAR_LISP ("truncate-partial-width-windows",
28821 Vtruncate_partial_width_windows,
28822 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28823 For an integer value, truncate lines in each window narrower than the
28824 full frame width, provided the window width is less than that integer;
28825 otherwise, respect the value of `truncate-lines'.
28826
28827 For any other non-nil value, truncate lines in all windows that do
28828 not span the full frame width.
28829
28830 A value of nil means to respect the value of `truncate-lines'.
28831
28832 If `word-wrap' is enabled, you might want to reduce this. */);
28833 Vtruncate_partial_width_windows = make_number (50);
28834
28835 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28836 doc: /* Maximum buffer size for which line number should be displayed.
28837 If the buffer is bigger than this, the line number does not appear
28838 in the mode line. A value of nil means no limit. */);
28839 Vline_number_display_limit = Qnil;
28840
28841 DEFVAR_INT ("line-number-display-limit-width",
28842 line_number_display_limit_width,
28843 doc: /* Maximum line width (in characters) for line number display.
28844 If the average length of the lines near point is bigger than this, then the
28845 line number may be omitted from the mode line. */);
28846 line_number_display_limit_width = 200;
28847
28848 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28849 doc: /* Non-nil means highlight region even in nonselected windows. */);
28850 highlight_nonselected_windows = 0;
28851
28852 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28853 doc: /* Non-nil if more than one frame is visible on this display.
28854 Minibuffer-only frames don't count, but iconified frames do.
28855 This variable is not guaranteed to be accurate except while processing
28856 `frame-title-format' and `icon-title-format'. */);
28857
28858 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28859 doc: /* Template for displaying the title bar of visible frames.
28860 \(Assuming the window manager supports this feature.)
28861
28862 This variable has the same structure as `mode-line-format', except that
28863 the %c and %l constructs are ignored. It is used only on frames for
28864 which no explicit name has been set \(see `modify-frame-parameters'). */);
28865
28866 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28867 doc: /* Template for displaying the title bar of an iconified frame.
28868 \(Assuming the window manager supports this feature.)
28869 This variable has the same structure as `mode-line-format' (which see),
28870 and is used only on frames for which no explicit name has been set
28871 \(see `modify-frame-parameters'). */);
28872 Vicon_title_format
28873 = Vframe_title_format
28874 = listn (CONSTYPE_PURE, 3,
28875 intern_c_string ("multiple-frames"),
28876 build_pure_c_string ("%b"),
28877 listn (CONSTYPE_PURE, 4,
28878 empty_unibyte_string,
28879 intern_c_string ("invocation-name"),
28880 build_pure_c_string ("@"),
28881 intern_c_string ("system-name")));
28882
28883 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28884 doc: /* Maximum number of lines to keep in the message log buffer.
28885 If nil, disable message logging. If t, log messages but don't truncate
28886 the buffer when it becomes large. */);
28887 Vmessage_log_max = make_number (1000);
28888
28889 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28890 doc: /* Functions called before redisplay, if window sizes have changed.
28891 The value should be a list of functions that take one argument.
28892 Just before redisplay, for each frame, if any of its windows have changed
28893 size since the last redisplay, or have been split or deleted,
28894 all the functions in the list are called, with the frame as argument. */);
28895 Vwindow_size_change_functions = Qnil;
28896
28897 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28898 doc: /* List of functions to call before redisplaying a window with scrolling.
28899 Each function is called with two arguments, the window and its new
28900 display-start position. Note that these functions are also called by
28901 `set-window-buffer'. Also note that the value of `window-end' is not
28902 valid when these functions are called.
28903
28904 Warning: Do not use this feature to alter the way the window
28905 is scrolled. It is not designed for that, and such use probably won't
28906 work. */);
28907 Vwindow_scroll_functions = Qnil;
28908
28909 DEFVAR_LISP ("window-text-change-functions",
28910 Vwindow_text_change_functions,
28911 doc: /* Functions to call in redisplay when text in the window might change. */);
28912 Vwindow_text_change_functions = Qnil;
28913
28914 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28915 doc: /* Functions called when redisplay of a window reaches the end trigger.
28916 Each function is called with two arguments, the window and the end trigger value.
28917 See `set-window-redisplay-end-trigger'. */);
28918 Vredisplay_end_trigger_functions = Qnil;
28919
28920 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28921 doc: /* Non-nil means autoselect window with mouse pointer.
28922 If nil, do not autoselect windows.
28923 A positive number means delay autoselection by that many seconds: a
28924 window is autoselected only after the mouse has remained in that
28925 window for the duration of the delay.
28926 A negative number has a similar effect, but causes windows to be
28927 autoselected only after the mouse has stopped moving. \(Because of
28928 the way Emacs compares mouse events, you will occasionally wait twice
28929 that time before the window gets selected.\)
28930 Any other value means to autoselect window instantaneously when the
28931 mouse pointer enters it.
28932
28933 Autoselection selects the minibuffer only if it is active, and never
28934 unselects the minibuffer if it is active.
28935
28936 When customizing this variable make sure that the actual value of
28937 `focus-follows-mouse' matches the behavior of your window manager. */);
28938 Vmouse_autoselect_window = Qnil;
28939
28940 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28941 doc: /* Non-nil means automatically resize tool-bars.
28942 This dynamically changes the tool-bar's height to the minimum height
28943 that is needed to make all tool-bar items visible.
28944 If value is `grow-only', the tool-bar's height is only increased
28945 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28946 Vauto_resize_tool_bars = Qt;
28947
28948 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
28949 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
28950 auto_raise_tool_bar_buttons_p = 1;
28951
28952 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
28953 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
28954 make_cursor_line_fully_visible_p = 1;
28955
28956 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
28957 doc: /* Border below tool-bar in pixels.
28958 If an integer, use it as the height of the border.
28959 If it is one of `internal-border-width' or `border-width', use the
28960 value of the corresponding frame parameter.
28961 Otherwise, no border is added below the tool-bar. */);
28962 Vtool_bar_border = Qinternal_border_width;
28963
28964 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
28965 doc: /* Margin around tool-bar buttons in pixels.
28966 If an integer, use that for both horizontal and vertical margins.
28967 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
28968 HORZ specifying the horizontal margin, and VERT specifying the
28969 vertical margin. */);
28970 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
28971
28972 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
28973 doc: /* Relief thickness of tool-bar buttons. */);
28974 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
28975
28976 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
28977 doc: /* Tool bar style to use.
28978 It can be one of
28979 image - show images only
28980 text - show text only
28981 both - show both, text below image
28982 both-horiz - show text to the right of the image
28983 text-image-horiz - show text to the left of the image
28984 any other - use system default or image if no system default.
28985
28986 This variable only affects the GTK+ toolkit version of Emacs. */);
28987 Vtool_bar_style = Qnil;
28988
28989 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
28990 doc: /* Maximum number of characters a label can have to be shown.
28991 The tool bar style must also show labels for this to have any effect, see
28992 `tool-bar-style'. */);
28993 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
28994
28995 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
28996 doc: /* List of functions to call to fontify regions of text.
28997 Each function is called with one argument POS. Functions must
28998 fontify a region starting at POS in the current buffer, and give
28999 fontified regions the property `fontified'. */);
29000 Vfontification_functions = Qnil;
29001 Fmake_variable_buffer_local (Qfontification_functions);
29002
29003 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29004 unibyte_display_via_language_environment,
29005 doc: /* Non-nil means display unibyte text according to language environment.
29006 Specifically, this means that raw bytes in the range 160-255 decimal
29007 are displayed by converting them to the equivalent multibyte characters
29008 according to the current language environment. As a result, they are
29009 displayed according to the current fontset.
29010
29011 Note that this variable affects only how these bytes are displayed,
29012 but does not change the fact they are interpreted as raw bytes. */);
29013 unibyte_display_via_language_environment = 0;
29014
29015 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29016 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29017 If a float, it specifies a fraction of the mini-window frame's height.
29018 If an integer, it specifies a number of lines. */);
29019 Vmax_mini_window_height = make_float (0.25);
29020
29021 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29022 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29023 A value of nil means don't automatically resize mini-windows.
29024 A value of t means resize them to fit the text displayed in them.
29025 A value of `grow-only', the default, means let mini-windows grow only;
29026 they return to their normal size when the minibuffer is closed, or the
29027 echo area becomes empty. */);
29028 Vresize_mini_windows = Qgrow_only;
29029
29030 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29031 doc: /* Alist specifying how to blink the cursor off.
29032 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29033 `cursor-type' frame-parameter or variable equals ON-STATE,
29034 comparing using `equal', Emacs uses OFF-STATE to specify
29035 how to blink it off. ON-STATE and OFF-STATE are values for
29036 the `cursor-type' frame parameter.
29037
29038 If a frame's ON-STATE has no entry in this list,
29039 the frame's other specifications determine how to blink the cursor off. */);
29040 Vblink_cursor_alist = Qnil;
29041
29042 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29043 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29044 If non-nil, windows are automatically scrolled horizontally to make
29045 point visible. */);
29046 automatic_hscrolling_p = 1;
29047 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29048
29049 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29050 doc: /* How many columns away from the window edge point is allowed to get
29051 before automatic hscrolling will horizontally scroll the window. */);
29052 hscroll_margin = 5;
29053
29054 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29055 doc: /* How many columns to scroll the window when point gets too close to the edge.
29056 When point is less than `hscroll-margin' columns from the window
29057 edge, automatic hscrolling will scroll the window by the amount of columns
29058 determined by this variable. If its value is a positive integer, scroll that
29059 many columns. If it's a positive floating-point number, it specifies the
29060 fraction of the window's width to scroll. If it's nil or zero, point will be
29061 centered horizontally after the scroll. Any other value, including negative
29062 numbers, are treated as if the value were zero.
29063
29064 Automatic hscrolling always moves point outside the scroll margin, so if
29065 point was more than scroll step columns inside the margin, the window will
29066 scroll more than the value given by the scroll step.
29067
29068 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29069 and `scroll-right' overrides this variable's effect. */);
29070 Vhscroll_step = make_number (0);
29071
29072 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29073 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29074 Bind this around calls to `message' to let it take effect. */);
29075 message_truncate_lines = 0;
29076
29077 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29078 doc: /* Normal hook run to update the menu bar definitions.
29079 Redisplay runs this hook before it redisplays the menu bar.
29080 This is used to update submenus such as Buffers,
29081 whose contents depend on various data. */);
29082 Vmenu_bar_update_hook = Qnil;
29083
29084 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29085 doc: /* Frame for which we are updating a menu.
29086 The enable predicate for a menu binding should check this variable. */);
29087 Vmenu_updating_frame = Qnil;
29088
29089 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29090 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29091 inhibit_menubar_update = 0;
29092
29093 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29094 doc: /* Prefix prepended to all continuation lines at display time.
29095 The value may be a string, an image, or a stretch-glyph; it is
29096 interpreted in the same way as the value of a `display' text property.
29097
29098 This variable is overridden by any `wrap-prefix' text or overlay
29099 property.
29100
29101 To add a prefix to non-continuation lines, use `line-prefix'. */);
29102 Vwrap_prefix = Qnil;
29103 DEFSYM (Qwrap_prefix, "wrap-prefix");
29104 Fmake_variable_buffer_local (Qwrap_prefix);
29105
29106 DEFVAR_LISP ("line-prefix", Vline_prefix,
29107 doc: /* Prefix prepended to all non-continuation lines at display time.
29108 The value may be a string, an image, or a stretch-glyph; it is
29109 interpreted in the same way as the value of a `display' text property.
29110
29111 This variable is overridden by any `line-prefix' text or overlay
29112 property.
29113
29114 To add a prefix to continuation lines, use `wrap-prefix'. */);
29115 Vline_prefix = Qnil;
29116 DEFSYM (Qline_prefix, "line-prefix");
29117 Fmake_variable_buffer_local (Qline_prefix);
29118
29119 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29120 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29121 inhibit_eval_during_redisplay = 0;
29122
29123 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29124 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29125 inhibit_free_realized_faces = 0;
29126
29127 #ifdef GLYPH_DEBUG
29128 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29129 doc: /* Inhibit try_window_id display optimization. */);
29130 inhibit_try_window_id = 0;
29131
29132 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29133 doc: /* Inhibit try_window_reusing display optimization. */);
29134 inhibit_try_window_reusing = 0;
29135
29136 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29137 doc: /* Inhibit try_cursor_movement display optimization. */);
29138 inhibit_try_cursor_movement = 0;
29139 #endif /* GLYPH_DEBUG */
29140
29141 DEFVAR_INT ("overline-margin", overline_margin,
29142 doc: /* Space between overline and text, in pixels.
29143 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29144 margin to the character height. */);
29145 overline_margin = 2;
29146
29147 DEFVAR_INT ("underline-minimum-offset",
29148 underline_minimum_offset,
29149 doc: /* Minimum distance between baseline and underline.
29150 This can improve legibility of underlined text at small font sizes,
29151 particularly when using variable `x-use-underline-position-properties'
29152 with fonts that specify an UNDERLINE_POSITION relatively close to the
29153 baseline. The default value is 1. */);
29154 underline_minimum_offset = 1;
29155
29156 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29157 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29158 This feature only works when on a window system that can change
29159 cursor shapes. */);
29160 display_hourglass_p = 1;
29161
29162 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29163 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29164 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29165
29166 hourglass_atimer = NULL;
29167 hourglass_shown_p = 0;
29168
29169 DEFSYM (Qglyphless_char, "glyphless-char");
29170 DEFSYM (Qhex_code, "hex-code");
29171 DEFSYM (Qempty_box, "empty-box");
29172 DEFSYM (Qthin_space, "thin-space");
29173 DEFSYM (Qzero_width, "zero-width");
29174
29175 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29176 /* Intern this now in case it isn't already done.
29177 Setting this variable twice is harmless.
29178 But don't staticpro it here--that is done in alloc.c. */
29179 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29180 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29181
29182 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29183 doc: /* Char-table defining glyphless characters.
29184 Each element, if non-nil, should be one of the following:
29185 an ASCII acronym string: display this string in a box
29186 `hex-code': display the hexadecimal code of a character in a box
29187 `empty-box': display as an empty box
29188 `thin-space': display as 1-pixel width space
29189 `zero-width': don't display
29190 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29191 display method for graphical terminals and text terminals respectively.
29192 GRAPHICAL and TEXT should each have one of the values listed above.
29193
29194 The char-table has one extra slot to control the display of a character for
29195 which no font is found. This slot only takes effect on graphical terminals.
29196 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29197 `thin-space'. The default is `empty-box'. */);
29198 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29199 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29200 Qempty_box);
29201
29202 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29203 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29204 Vdebug_on_message = Qnil;
29205 }
29206
29207
29208 /* Initialize this module when Emacs starts. */
29209
29210 void
29211 init_xdisp (void)
29212 {
29213 current_header_line_height = current_mode_line_height = -1;
29214
29215 CHARPOS (this_line_start_pos) = 0;
29216
29217 if (!noninteractive)
29218 {
29219 struct window *m = XWINDOW (minibuf_window);
29220 Lisp_Object frame = m->frame;
29221 struct frame *f = XFRAME (frame);
29222 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29223 struct window *r = XWINDOW (root);
29224 int i;
29225
29226 echo_area_window = minibuf_window;
29227
29228 wset_top_line (r, make_number (FRAME_TOP_MARGIN (f)));
29229 wset_total_lines
29230 (r, make_number (FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f)));
29231 wset_total_cols (r, make_number (FRAME_COLS (f)));
29232 wset_top_line (m, make_number (FRAME_LINES (f) - 1));
29233 wset_total_lines (m, make_number (1));
29234 wset_total_cols (m, make_number (FRAME_COLS (f)));
29235
29236 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29237 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29238 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29239
29240 /* The default ellipsis glyphs `...'. */
29241 for (i = 0; i < 3; ++i)
29242 default_invis_vector[i] = make_number ('.');
29243 }
29244
29245 {
29246 /* Allocate the buffer for frame titles.
29247 Also used for `format-mode-line'. */
29248 int size = 100;
29249 mode_line_noprop_buf = xmalloc (size);
29250 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29251 mode_line_noprop_ptr = mode_line_noprop_buf;
29252 mode_line_target = MODE_LINE_DISPLAY;
29253 }
29254
29255 help_echo_showing_p = 0;
29256 }
29257
29258 /* Platform-independent portion of hourglass implementation. */
29259
29260 /* Cancel a currently active hourglass timer, and start a new one. */
29261 void
29262 start_hourglass (void)
29263 {
29264 #if defined (HAVE_WINDOW_SYSTEM)
29265 EMACS_TIME delay;
29266
29267 cancel_hourglass ();
29268
29269 if (INTEGERP (Vhourglass_delay)
29270 && XINT (Vhourglass_delay) > 0)
29271 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29272 TYPE_MAXIMUM (time_t)),
29273 0);
29274 else if (FLOATP (Vhourglass_delay)
29275 && XFLOAT_DATA (Vhourglass_delay) > 0)
29276 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29277 else
29278 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29279
29280 #ifdef HAVE_NTGUI
29281 {
29282 extern void w32_note_current_window (void);
29283 w32_note_current_window ();
29284 }
29285 #endif /* HAVE_NTGUI */
29286
29287 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29288 show_hourglass, NULL);
29289 #endif
29290 }
29291
29292
29293 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29294 shown. */
29295 void
29296 cancel_hourglass (void)
29297 {
29298 #if defined (HAVE_WINDOW_SYSTEM)
29299 if (hourglass_atimer)
29300 {
29301 cancel_atimer (hourglass_atimer);
29302 hourglass_atimer = NULL;
29303 }
29304
29305 if (hourglass_shown_p)
29306 hide_hourglass ();
29307 #endif
29308 }