Improve the fix for bug #14558.
[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_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 row_for_charpos_p (struct glyph_row *, ptrdiff_t);
798 static int cursor_row_p (struct glyph_row *);
799 static int redisplay_mode_lines (Lisp_Object, int);
800 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
801
802 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
803
804 static void handle_line_prefix (struct it *);
805
806 static void pint2str (char *, int, ptrdiff_t);
807 static void pint2hrstr (char *, int, ptrdiff_t);
808 static struct text_pos run_window_scroll_functions (Lisp_Object,
809 struct text_pos);
810 static void reconsider_clip_changes (struct window *, struct buffer *);
811 static int text_outside_line_unchanged_p (struct window *,
812 ptrdiff_t, ptrdiff_t);
813 static void store_mode_line_noprop_char (char);
814 static int store_mode_line_noprop (const char *, int, int);
815 static void handle_stop (struct it *);
816 static void handle_stop_backwards (struct it *, ptrdiff_t);
817 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
818 static void ensure_echo_area_buffers (void);
819 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
820 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
821 static int with_echo_area_buffer (struct window *, int,
822 int (*) (ptrdiff_t, Lisp_Object),
823 ptrdiff_t, Lisp_Object);
824 static void clear_garbaged_frames (void);
825 static int current_message_1 (ptrdiff_t, Lisp_Object);
826 static void pop_message (void);
827 static int truncate_message_1 (ptrdiff_t, Lisp_Object);
828 static void set_message (Lisp_Object);
829 static int set_message_1 (ptrdiff_t, Lisp_Object);
830 static int display_echo_area (struct window *);
831 static int display_echo_area_1 (ptrdiff_t, Lisp_Object);
832 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object);
833 static Lisp_Object unwind_redisplay (Lisp_Object);
834 static int string_char_and_length (const unsigned char *, int *);
835 static struct text_pos display_prop_end (struct it *, Lisp_Object,
836 struct text_pos);
837 static int compute_window_start_on_continuation_line (struct window *);
838 static void insert_left_trunc_glyphs (struct it *);
839 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
840 Lisp_Object);
841 static void extend_face_to_end_of_line (struct it *);
842 static int append_space_for_newline (struct it *, int);
843 static int cursor_row_fully_visible_p (struct window *, int, int);
844 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
845 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
846 static int trailing_whitespace_p (ptrdiff_t);
847 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
848 static void push_it (struct it *, struct text_pos *);
849 static void iterate_out_of_display_property (struct it *);
850 static void pop_it (struct it *);
851 static void sync_frame_with_window_matrix_rows (struct window *);
852 static void redisplay_internal (void);
853 static int echo_area_display (int);
854 static void redisplay_windows (Lisp_Object);
855 static void redisplay_window (Lisp_Object, int);
856 static Lisp_Object redisplay_window_error (Lisp_Object);
857 static Lisp_Object redisplay_window_0 (Lisp_Object);
858 static Lisp_Object redisplay_window_1 (Lisp_Object);
859 static int set_cursor_from_row (struct window *, struct glyph_row *,
860 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
861 int, int);
862 static int update_menu_bar (struct frame *, int, int);
863 static int try_window_reusing_current_matrix (struct window *);
864 static int try_window_id (struct window *);
865 static int display_line (struct it *);
866 static int display_mode_lines (struct window *);
867 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
868 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
869 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
870 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
871 static void display_menu_bar (struct window *);
872 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
873 ptrdiff_t *);
874 static int display_string (const char *, Lisp_Object, Lisp_Object,
875 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
876 static void compute_line_metrics (struct it *);
877 static void run_redisplay_end_trigger_hook (struct it *);
878 static int get_overlay_strings (struct it *, ptrdiff_t);
879 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
880 static void next_overlay_string (struct it *);
881 static void reseat (struct it *, struct text_pos, int);
882 static void reseat_1 (struct it *, struct text_pos, int);
883 static void back_to_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 static void get_visually_first_element (struct it *);
903 static void init_to_row_start (struct it *, struct window *,
904 struct glyph_row *);
905 static int init_to_row_end (struct it *, struct window *,
906 struct glyph_row *);
907 static void back_to_previous_line_start (struct it *);
908 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
909 static struct text_pos string_pos_nchars_ahead (struct text_pos,
910 Lisp_Object, ptrdiff_t);
911 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
912 static struct text_pos c_string_pos (ptrdiff_t, const char *, bool);
913 static ptrdiff_t number_of_chars (const char *, bool);
914 static void compute_stop_pos (struct it *);
915 static void compute_string_pos (struct text_pos *, struct text_pos,
916 Lisp_Object);
917 static int face_before_or_after_it_pos (struct it *, int);
918 static ptrdiff_t next_overlay_change (ptrdiff_t);
919 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
920 Lisp_Object, struct text_pos *, ptrdiff_t, int);
921 static int handle_single_display_spec (struct it *, Lisp_Object,
922 Lisp_Object, Lisp_Object,
923 struct text_pos *, ptrdiff_t, int, int);
924 static int underlying_face_id (struct it *);
925 static int in_ellipses_for_invisible_text_p (struct display_pos *,
926 struct window *);
927
928 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
929 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
930
931 #ifdef HAVE_WINDOW_SYSTEM
932
933 static void x_consider_frame_title (Lisp_Object);
934 static int tool_bar_lines_needed (struct frame *, int *);
935 static void update_tool_bar (struct frame *, int);
936 static void build_desired_tool_bar_string (struct frame *f);
937 static int redisplay_tool_bar (struct frame *);
938 static void display_tool_bar_line (struct it *, int);
939 static void notice_overwritten_cursor (struct window *,
940 enum glyph_row_area,
941 int, int, int, int);
942 static void append_stretch_glyph (struct it *, Lisp_Object,
943 int, int, int);
944
945
946 #endif /* HAVE_WINDOW_SYSTEM */
947
948 static void produce_special_glyphs (struct it *, enum display_element_type);
949 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
950 static int coords_in_mouse_face_p (struct window *, int, int);
951
952
953 \f
954 /***********************************************************************
955 Window display dimensions
956 ***********************************************************************/
957
958 /* Return the bottom boundary y-position for text lines in window W.
959 This is the first y position at which a line cannot start.
960 It is relative to the top of the window.
961
962 This is the height of W minus the height of a mode line, if any. */
963
964 int
965 window_text_bottom_y (struct window *w)
966 {
967 int height = WINDOW_TOTAL_HEIGHT (w);
968
969 if (WINDOW_WANTS_MODELINE_P (w))
970 height -= CURRENT_MODE_LINE_HEIGHT (w);
971 return height;
972 }
973
974 /* Return the pixel width of display area AREA of window W. AREA < 0
975 means return the total width of W, not including fringes to
976 the left and right of the window. */
977
978 int
979 window_box_width (struct window *w, int area)
980 {
981 int cols = w->total_cols;
982 int pixels = 0;
983
984 if (!w->pseudo_window_p)
985 {
986 cols -= WINDOW_SCROLL_BAR_COLS (w);
987
988 if (area == TEXT_AREA)
989 {
990 if (INTEGERP (w->left_margin_cols))
991 cols -= XFASTINT (w->left_margin_cols);
992 if (INTEGERP (w->right_margin_cols))
993 cols -= XFASTINT (w->right_margin_cols);
994 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
995 }
996 else if (area == LEFT_MARGIN_AREA)
997 {
998 cols = (INTEGERP (w->left_margin_cols)
999 ? XFASTINT (w->left_margin_cols) : 0);
1000 pixels = 0;
1001 }
1002 else if (area == RIGHT_MARGIN_AREA)
1003 {
1004 cols = (INTEGERP (w->right_margin_cols)
1005 ? XFASTINT (w->right_margin_cols) : 0);
1006 pixels = 0;
1007 }
1008 }
1009
1010 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1011 }
1012
1013
1014 /* Return the pixel height of the display area of window W, not
1015 including mode lines of W, if any. */
1016
1017 int
1018 window_box_height (struct window *w)
1019 {
1020 struct frame *f = XFRAME (w->frame);
1021 int height = WINDOW_TOTAL_HEIGHT (w);
1022
1023 eassert (height >= 0);
1024
1025 /* Note: the code below that determines the mode-line/header-line
1026 height is essentially the same as that contained in the macro
1027 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1028 the appropriate glyph row has its `mode_line_p' flag set,
1029 and if it doesn't, uses estimate_mode_line_height instead. */
1030
1031 if (WINDOW_WANTS_MODELINE_P (w))
1032 {
1033 struct glyph_row *ml_row
1034 = (w->current_matrix && w->current_matrix->rows
1035 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1036 : 0);
1037 if (ml_row && ml_row->mode_line_p)
1038 height -= ml_row->height;
1039 else
1040 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1041 }
1042
1043 if (WINDOW_WANTS_HEADER_LINE_P (w))
1044 {
1045 struct glyph_row *hl_row
1046 = (w->current_matrix && w->current_matrix->rows
1047 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1048 : 0);
1049 if (hl_row && hl_row->mode_line_p)
1050 height -= hl_row->height;
1051 else
1052 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1053 }
1054
1055 /* With a very small font and a mode-line that's taller than
1056 default, we might end up with a negative height. */
1057 return max (0, height);
1058 }
1059
1060 /* Return the window-relative coordinate of the left edge of display
1061 area AREA of window W. AREA < 0 means return the left edge of the
1062 whole window, to the right of the left fringe of W. */
1063
1064 int
1065 window_box_left_offset (struct window *w, int area)
1066 {
1067 int x;
1068
1069 if (w->pseudo_window_p)
1070 return 0;
1071
1072 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1073
1074 if (area == TEXT_AREA)
1075 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1076 + window_box_width (w, LEFT_MARGIN_AREA));
1077 else if (area == RIGHT_MARGIN_AREA)
1078 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1079 + window_box_width (w, LEFT_MARGIN_AREA)
1080 + window_box_width (w, TEXT_AREA)
1081 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1082 ? 0
1083 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1084 else if (area == LEFT_MARGIN_AREA
1085 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1086 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1087
1088 return x;
1089 }
1090
1091
1092 /* Return the window-relative coordinate of the right edge of display
1093 area AREA of window W. AREA < 0 means return the right edge of the
1094 whole window, to the left of the right fringe of W. */
1095
1096 int
1097 window_box_right_offset (struct window *w, int area)
1098 {
1099 return window_box_left_offset (w, area) + window_box_width (w, area);
1100 }
1101
1102 /* Return the frame-relative coordinate of the left edge of display
1103 area AREA of window W. AREA < 0 means return the left edge of the
1104 whole window, to the right of the left fringe of W. */
1105
1106 int
1107 window_box_left (struct window *w, int area)
1108 {
1109 struct frame *f = XFRAME (w->frame);
1110 int x;
1111
1112 if (w->pseudo_window_p)
1113 return FRAME_INTERNAL_BORDER_WIDTH (f);
1114
1115 x = (WINDOW_LEFT_EDGE_X (w)
1116 + window_box_left_offset (w, area));
1117
1118 return x;
1119 }
1120
1121
1122 /* Return the frame-relative coordinate of the right edge of display
1123 area AREA of window W. AREA < 0 means return the right edge of the
1124 whole window, to the left of the right fringe of W. */
1125
1126 int
1127 window_box_right (struct window *w, int area)
1128 {
1129 return window_box_left (w, area) + window_box_width (w, area);
1130 }
1131
1132 /* Get the bounding box of the display area AREA of window W, without
1133 mode lines, in frame-relative coordinates. AREA < 0 means the
1134 whole window, not including the left and right fringes of
1135 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1136 coordinates of the upper-left corner of the box. Return in
1137 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1138
1139 void
1140 window_box (struct window *w, int area, int *box_x, int *box_y,
1141 int *box_width, int *box_height)
1142 {
1143 if (box_width)
1144 *box_width = window_box_width (w, area);
1145 if (box_height)
1146 *box_height = window_box_height (w);
1147 if (box_x)
1148 *box_x = window_box_left (w, area);
1149 if (box_y)
1150 {
1151 *box_y = WINDOW_TOP_EDGE_Y (w);
1152 if (WINDOW_WANTS_HEADER_LINE_P (w))
1153 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1154 }
1155 }
1156
1157
1158 /* Get the bounding box of the display area AREA of window W, without
1159 mode lines. AREA < 0 means the whole window, not including the
1160 left and right fringe of the window. Return in *TOP_LEFT_X
1161 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1162 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1163 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1164 box. */
1165
1166 static void
1167 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1168 int *bottom_right_x, int *bottom_right_y)
1169 {
1170 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1171 bottom_right_y);
1172 *bottom_right_x += *top_left_x;
1173 *bottom_right_y += *top_left_y;
1174 }
1175
1176
1177 \f
1178 /***********************************************************************
1179 Utilities
1180 ***********************************************************************/
1181
1182 /* Return the bottom y-position of the line the iterator IT is in.
1183 This can modify IT's settings. */
1184
1185 int
1186 line_bottom_y (struct it *it)
1187 {
1188 int line_height = it->max_ascent + it->max_descent;
1189 int line_top_y = it->current_y;
1190
1191 if (line_height == 0)
1192 {
1193 if (last_height)
1194 line_height = last_height;
1195 else if (IT_CHARPOS (*it) < ZV)
1196 {
1197 move_it_by_lines (it, 1);
1198 line_height = (it->max_ascent || it->max_descent
1199 ? it->max_ascent + it->max_descent
1200 : last_height);
1201 }
1202 else
1203 {
1204 struct glyph_row *row = it->glyph_row;
1205
1206 /* Use the default character height. */
1207 it->glyph_row = NULL;
1208 it->what = IT_CHARACTER;
1209 it->c = ' ';
1210 it->len = 1;
1211 PRODUCE_GLYPHS (it);
1212 line_height = it->ascent + it->descent;
1213 it->glyph_row = row;
1214 }
1215 }
1216
1217 return line_top_y + line_height;
1218 }
1219
1220 /* Subroutine of pos_visible_p below. Extracts a display string, if
1221 any, from the display spec given as its argument. */
1222 static Lisp_Object
1223 string_from_display_spec (Lisp_Object spec)
1224 {
1225 if (CONSP (spec))
1226 {
1227 while (CONSP (spec))
1228 {
1229 if (STRINGP (XCAR (spec)))
1230 return XCAR (spec);
1231 spec = XCDR (spec);
1232 }
1233 }
1234 else if (VECTORP (spec))
1235 {
1236 ptrdiff_t i;
1237
1238 for (i = 0; i < ASIZE (spec); i++)
1239 {
1240 if (STRINGP (AREF (spec, i)))
1241 return AREF (spec, i);
1242 }
1243 return Qnil;
1244 }
1245
1246 return spec;
1247 }
1248
1249
1250 /* Limit insanely large values of W->hscroll on frame F to the largest
1251 value that will still prevent first_visible_x and last_visible_x of
1252 'struct it' from overflowing an int. */
1253 static int
1254 window_hscroll_limited (struct window *w, struct frame *f)
1255 {
1256 ptrdiff_t window_hscroll = w->hscroll;
1257 int window_text_width = window_box_width (w, TEXT_AREA);
1258 int colwidth = FRAME_COLUMN_WIDTH (f);
1259
1260 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1261 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1262
1263 return window_hscroll;
1264 }
1265
1266 /* Return 1 if position CHARPOS is visible in window W.
1267 CHARPOS < 0 means return info about WINDOW_END position.
1268 If visible, set *X and *Y to pixel coordinates of top left corner.
1269 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1270 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1271
1272 int
1273 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1274 int *rtop, int *rbot, int *rowh, int *vpos)
1275 {
1276 struct it it;
1277 void *itdata = bidi_shelve_cache ();
1278 struct text_pos top;
1279 int visible_p = 0;
1280 struct buffer *old_buffer = NULL;
1281
1282 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1283 return visible_p;
1284
1285 if (XBUFFER (w->contents) != current_buffer)
1286 {
1287 old_buffer = current_buffer;
1288 set_buffer_internal_1 (XBUFFER (w->contents));
1289 }
1290
1291 SET_TEXT_POS_FROM_MARKER (top, w->start);
1292 /* Scrolling a minibuffer window via scroll bar when the echo area
1293 shows long text sometimes resets the minibuffer contents behind
1294 our backs. */
1295 if (CHARPOS (top) > ZV)
1296 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1297
1298 /* Compute exact mode line heights. */
1299 if (WINDOW_WANTS_MODELINE_P (w))
1300 current_mode_line_height
1301 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1302 BVAR (current_buffer, mode_line_format));
1303
1304 if (WINDOW_WANTS_HEADER_LINE_P (w))
1305 current_header_line_height
1306 = display_mode_line (w, HEADER_LINE_FACE_ID,
1307 BVAR (current_buffer, header_line_format));
1308
1309 start_display (&it, w, top);
1310 move_it_to (&it, charpos, -1, it.last_visible_y - 1, -1,
1311 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1312
1313 if (charpos >= 0
1314 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1315 && IT_CHARPOS (it) >= charpos)
1316 /* When scanning backwards under bidi iteration, move_it_to
1317 stops at or _before_ CHARPOS, because it stops at or to
1318 the _right_ of the character at CHARPOS. */
1319 || (it.bidi_p && it.bidi_it.scan_dir == -1
1320 && IT_CHARPOS (it) <= charpos)))
1321 {
1322 /* We have reached CHARPOS, or passed it. How the call to
1323 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1324 or covered by a display property, move_it_to stops at the end
1325 of the invisible text, to the right of CHARPOS. (ii) If
1326 CHARPOS is in a display vector, move_it_to stops on its last
1327 glyph. */
1328 int top_x = it.current_x;
1329 int top_y = it.current_y;
1330 /* Calling line_bottom_y may change it.method, it.position, etc. */
1331 enum it_method it_method = it.method;
1332 int bottom_y = (last_height = 0, line_bottom_y (&it));
1333 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1334
1335 if (top_y < window_top_y)
1336 visible_p = bottom_y > window_top_y;
1337 else if (top_y < it.last_visible_y)
1338 visible_p = 1;
1339 if (bottom_y >= it.last_visible_y
1340 && it.bidi_p && it.bidi_it.scan_dir == -1
1341 && IT_CHARPOS (it) < charpos)
1342 {
1343 /* When the last line of the window is scanned backwards
1344 under bidi iteration, we could be duped into thinking
1345 that we have passed CHARPOS, when in fact move_it_to
1346 simply stopped short of CHARPOS because it reached
1347 last_visible_y. To see if that's what happened, we call
1348 move_it_to again with a slightly larger vertical limit,
1349 and see if it actually moved vertically; if it did, we
1350 didn't really reach CHARPOS, which is beyond window end. */
1351 struct it save_it = it;
1352 /* Why 10? because we don't know how many canonical lines
1353 will the height of the next line(s) be. So we guess. */
1354 int ten_more_lines =
1355 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1356
1357 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1358 MOVE_TO_POS | MOVE_TO_Y);
1359 if (it.current_y > top_y)
1360 visible_p = 0;
1361
1362 it = save_it;
1363 }
1364 if (visible_p)
1365 {
1366 if (it_method == GET_FROM_DISPLAY_VECTOR)
1367 {
1368 /* We stopped on the last glyph of a display vector.
1369 Try and recompute. Hack alert! */
1370 if (charpos < 2 || top.charpos >= charpos)
1371 top_x = it.glyph_row->x;
1372 else
1373 {
1374 struct it it2, it2_prev;
1375 /* The idea is to get to the previous buffer
1376 position, consume the character there, and use
1377 the pixel coordinates we get after that. But if
1378 the previous buffer position is also displayed
1379 from a display vector, we need to consume all of
1380 the glyphs from that display vector. */
1381 start_display (&it2, w, top);
1382 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1383 /* If we didn't get to CHARPOS - 1, there's some
1384 replacing display property at that position, and
1385 we stopped after it. That is exactly the place
1386 whose coordinates we want. */
1387 if (IT_CHARPOS (it2) != charpos - 1)
1388 it2_prev = it2;
1389 else
1390 {
1391 /* Iterate until we get out of the display
1392 vector that displays the character at
1393 CHARPOS - 1. */
1394 do {
1395 get_next_display_element (&it2);
1396 PRODUCE_GLYPHS (&it2);
1397 it2_prev = it2;
1398 set_iterator_to_next (&it2, 1);
1399 } while (it2.method == GET_FROM_DISPLAY_VECTOR
1400 && IT_CHARPOS (it2) < charpos);
1401 }
1402 if (ITERATOR_AT_END_OF_LINE_P (&it2_prev)
1403 || it2_prev.current_x > it2_prev.last_visible_x)
1404 top_x = it.glyph_row->x;
1405 else
1406 {
1407 top_x = it2_prev.current_x;
1408 top_y = it2_prev.current_y;
1409 }
1410 }
1411 }
1412 else if (IT_CHARPOS (it) != charpos)
1413 {
1414 Lisp_Object cpos = make_number (charpos);
1415 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1416 Lisp_Object string = string_from_display_spec (spec);
1417 struct text_pos tpos;
1418 int replacing_spec_p;
1419 bool newline_in_string
1420 = (STRINGP (string)
1421 && memchr (SDATA (string), '\n', SBYTES (string)));
1422
1423 SET_TEXT_POS (tpos, charpos, CHAR_TO_BYTE (charpos));
1424 replacing_spec_p
1425 = (!NILP (spec)
1426 && handle_display_spec (NULL, spec, Qnil, Qnil, &tpos,
1427 charpos, FRAME_WINDOW_P (it.f)));
1428 /* The tricky code below is needed because there's a
1429 discrepancy between move_it_to and how we set cursor
1430 when PT is at the beginning of a portion of text
1431 covered by a display property or an overlay with a
1432 display property, or the display line ends in a
1433 newline from a display string. move_it_to will stop
1434 _after_ such display strings, whereas
1435 set_cursor_from_row conspires with cursor_row_p to
1436 place the cursor on the first glyph produced from the
1437 display string. */
1438
1439 /* We have overshoot PT because it is covered by a
1440 display property that replaces the text it covers.
1441 If the string includes embedded newlines, we are also
1442 in the wrong display line. Backtrack to the correct
1443 line, where the display property begins. */
1444 if (replacing_spec_p)
1445 {
1446 Lisp_Object startpos, endpos;
1447 EMACS_INT start, end;
1448 struct it it3;
1449 int it3_moved;
1450
1451 /* Find the first and the last buffer positions
1452 covered by the display string. */
1453 endpos =
1454 Fnext_single_char_property_change (cpos, Qdisplay,
1455 Qnil, Qnil);
1456 startpos =
1457 Fprevious_single_char_property_change (endpos, Qdisplay,
1458 Qnil, Qnil);
1459 start = XFASTINT (startpos);
1460 end = XFASTINT (endpos);
1461 /* Move to the last buffer position before the
1462 display property. */
1463 start_display (&it3, w, top);
1464 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1465 /* Move forward one more line if the position before
1466 the display string is a newline or if it is the
1467 rightmost character on a line that is
1468 continued or word-wrapped. */
1469 if (it3.method == GET_FROM_BUFFER
1470 && (it3.c == '\n'
1471 || FETCH_BYTE (IT_BYTEPOS (it3)) == '\n'))
1472 move_it_by_lines (&it3, 1);
1473 else if (move_it_in_display_line_to (&it3, -1,
1474 it3.current_x
1475 + it3.pixel_width,
1476 MOVE_TO_X)
1477 == MOVE_LINE_CONTINUED)
1478 {
1479 move_it_by_lines (&it3, 1);
1480 /* When we are under word-wrap, the #$@%!
1481 move_it_by_lines moves 2 lines, so we need to
1482 fix that up. */
1483 if (it3.line_wrap == WORD_WRAP)
1484 move_it_by_lines (&it3, -1);
1485 }
1486
1487 /* Record the vertical coordinate of the display
1488 line where we wound up. */
1489 top_y = it3.current_y;
1490 if (it3.bidi_p)
1491 {
1492 /* When characters are reordered for display,
1493 the character displayed to the left of the
1494 display string could be _after_ the display
1495 property in the logical order. Use the
1496 smallest vertical position of these two. */
1497 start_display (&it3, w, top);
1498 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1499 if (it3.current_y < top_y)
1500 top_y = it3.current_y;
1501 }
1502 /* Move from the top of the window to the beginning
1503 of the display line where the display string
1504 begins. */
1505 start_display (&it3, w, top);
1506 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1507 /* If it3_moved stays zero after the 'while' loop
1508 below, that means we already were at a newline
1509 before the loop (e.g., the display string begins
1510 with a newline), so we don't need to (and cannot)
1511 inspect the glyphs of it3.glyph_row, because
1512 PRODUCE_GLYPHS will not produce anything for a
1513 newline, and thus it3.glyph_row stays at its
1514 stale content it got at top of the window. */
1515 it3_moved = 0;
1516 /* Finally, advance the iterator until we hit the
1517 first display element whose character position is
1518 CHARPOS, or until the first newline from the
1519 display string, which signals the end of the
1520 display line. */
1521 while (get_next_display_element (&it3))
1522 {
1523 PRODUCE_GLYPHS (&it3);
1524 if (IT_CHARPOS (it3) == charpos
1525 || ITERATOR_AT_END_OF_LINE_P (&it3))
1526 break;
1527 it3_moved = 1;
1528 set_iterator_to_next (&it3, 0);
1529 }
1530 top_x = it3.current_x - it3.pixel_width;
1531 /* Normally, we would exit the above loop because we
1532 found the display element whose character
1533 position is CHARPOS. For the contingency that we
1534 didn't, and stopped at the first newline from the
1535 display string, move back over the glyphs
1536 produced from the string, until we find the
1537 rightmost glyph not from the string. */
1538 if (it3_moved
1539 && newline_in_string
1540 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1541 {
1542 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1543 + it3.glyph_row->used[TEXT_AREA];
1544
1545 while (EQ ((g - 1)->object, string))
1546 {
1547 --g;
1548 top_x -= g->pixel_width;
1549 }
1550 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1551 + it3.glyph_row->used[TEXT_AREA]);
1552 }
1553 }
1554 }
1555
1556 *x = top_x;
1557 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1558 *rtop = max (0, window_top_y - top_y);
1559 *rbot = max (0, bottom_y - it.last_visible_y);
1560 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1561 - max (top_y, window_top_y)));
1562 *vpos = it.vpos;
1563 }
1564 }
1565 else
1566 {
1567 /* We were asked to provide info about WINDOW_END. */
1568 struct it it2;
1569 void *it2data = NULL;
1570
1571 SAVE_IT (it2, it, it2data);
1572 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1573 move_it_by_lines (&it, 1);
1574 if (charpos < IT_CHARPOS (it)
1575 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1576 {
1577 visible_p = 1;
1578 RESTORE_IT (&it2, &it2, it2data);
1579 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1580 *x = it2.current_x;
1581 *y = it2.current_y + it2.max_ascent - it2.ascent;
1582 *rtop = max (0, -it2.current_y);
1583 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1584 - it.last_visible_y));
1585 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1586 it.last_visible_y)
1587 - max (it2.current_y,
1588 WINDOW_HEADER_LINE_HEIGHT (w))));
1589 *vpos = it2.vpos;
1590 }
1591 else
1592 bidi_unshelve_cache (it2data, 1);
1593 }
1594 bidi_unshelve_cache (itdata, 0);
1595
1596 if (old_buffer)
1597 set_buffer_internal_1 (old_buffer);
1598
1599 current_header_line_height = current_mode_line_height = -1;
1600
1601 if (visible_p && w->hscroll > 0)
1602 *x -=
1603 window_hscroll_limited (w, WINDOW_XFRAME (w))
1604 * WINDOW_FRAME_COLUMN_WIDTH (w);
1605
1606 #if 0
1607 /* Debugging code. */
1608 if (visible_p)
1609 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1610 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1611 else
1612 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1613 #endif
1614
1615 return visible_p;
1616 }
1617
1618
1619 /* Return the next character from STR. Return in *LEN the length of
1620 the character. This is like STRING_CHAR_AND_LENGTH but never
1621 returns an invalid character. If we find one, we return a `?', but
1622 with the length of the invalid character. */
1623
1624 static int
1625 string_char_and_length (const unsigned char *str, int *len)
1626 {
1627 int c;
1628
1629 c = STRING_CHAR_AND_LENGTH (str, *len);
1630 if (!CHAR_VALID_P (c))
1631 /* We may not change the length here because other places in Emacs
1632 don't use this function, i.e. they silently accept invalid
1633 characters. */
1634 c = '?';
1635
1636 return c;
1637 }
1638
1639
1640
1641 /* Given a position POS containing a valid character and byte position
1642 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1643
1644 static struct text_pos
1645 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1646 {
1647 eassert (STRINGP (string) && nchars >= 0);
1648
1649 if (STRING_MULTIBYTE (string))
1650 {
1651 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1652 int len;
1653
1654 while (nchars--)
1655 {
1656 string_char_and_length (p, &len);
1657 p += len;
1658 CHARPOS (pos) += 1;
1659 BYTEPOS (pos) += len;
1660 }
1661 }
1662 else
1663 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1664
1665 return pos;
1666 }
1667
1668
1669 /* Value is the text position, i.e. character and byte position,
1670 for character position CHARPOS in STRING. */
1671
1672 static struct text_pos
1673 string_pos (ptrdiff_t charpos, Lisp_Object string)
1674 {
1675 struct text_pos pos;
1676 eassert (STRINGP (string));
1677 eassert (charpos >= 0);
1678 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1679 return pos;
1680 }
1681
1682
1683 /* Value is a text position, i.e. character and byte position, for
1684 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1685 means recognize multibyte characters. */
1686
1687 static struct text_pos
1688 c_string_pos (ptrdiff_t charpos, const char *s, bool multibyte_p)
1689 {
1690 struct text_pos pos;
1691
1692 eassert (s != NULL);
1693 eassert (charpos >= 0);
1694
1695 if (multibyte_p)
1696 {
1697 int len;
1698
1699 SET_TEXT_POS (pos, 0, 0);
1700 while (charpos--)
1701 {
1702 string_char_and_length ((const unsigned char *) s, &len);
1703 s += len;
1704 CHARPOS (pos) += 1;
1705 BYTEPOS (pos) += len;
1706 }
1707 }
1708 else
1709 SET_TEXT_POS (pos, charpos, charpos);
1710
1711 return pos;
1712 }
1713
1714
1715 /* Value is the number of characters in C string S. MULTIBYTE_P
1716 non-zero means recognize multibyte characters. */
1717
1718 static ptrdiff_t
1719 number_of_chars (const char *s, bool multibyte_p)
1720 {
1721 ptrdiff_t nchars;
1722
1723 if (multibyte_p)
1724 {
1725 ptrdiff_t rest = strlen (s);
1726 int len;
1727 const unsigned char *p = (const unsigned char *) s;
1728
1729 for (nchars = 0; rest > 0; ++nchars)
1730 {
1731 string_char_and_length (p, &len);
1732 rest -= len, p += len;
1733 }
1734 }
1735 else
1736 nchars = strlen (s);
1737
1738 return nchars;
1739 }
1740
1741
1742 /* Compute byte position NEWPOS->bytepos corresponding to
1743 NEWPOS->charpos. POS is a known position in string STRING.
1744 NEWPOS->charpos must be >= POS.charpos. */
1745
1746 static void
1747 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1748 {
1749 eassert (STRINGP (string));
1750 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1751
1752 if (STRING_MULTIBYTE (string))
1753 *newpos = string_pos_nchars_ahead (pos, string,
1754 CHARPOS (*newpos) - CHARPOS (pos));
1755 else
1756 BYTEPOS (*newpos) = CHARPOS (*newpos);
1757 }
1758
1759 /* EXPORT:
1760 Return an estimation of the pixel height of mode or header lines on
1761 frame F. FACE_ID specifies what line's height to estimate. */
1762
1763 int
1764 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1765 {
1766 #ifdef HAVE_WINDOW_SYSTEM
1767 if (FRAME_WINDOW_P (f))
1768 {
1769 int height = FONT_HEIGHT (FRAME_FONT (f));
1770
1771 /* This function is called so early when Emacs starts that the face
1772 cache and mode line face are not yet initialized. */
1773 if (FRAME_FACE_CACHE (f))
1774 {
1775 struct face *face = FACE_FROM_ID (f, face_id);
1776 if (face)
1777 {
1778 if (face->font)
1779 height = FONT_HEIGHT (face->font);
1780 if (face->box_line_width > 0)
1781 height += 2 * face->box_line_width;
1782 }
1783 }
1784
1785 return height;
1786 }
1787 #endif
1788
1789 return 1;
1790 }
1791
1792 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1793 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1794 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1795 not force the value into range. */
1796
1797 void
1798 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1799 int *x, int *y, NativeRectangle *bounds, int noclip)
1800 {
1801
1802 #ifdef HAVE_WINDOW_SYSTEM
1803 if (FRAME_WINDOW_P (f))
1804 {
1805 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1806 even for negative values. */
1807 if (pix_x < 0)
1808 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1809 if (pix_y < 0)
1810 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1811
1812 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1813 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1814
1815 if (bounds)
1816 STORE_NATIVE_RECT (*bounds,
1817 FRAME_COL_TO_PIXEL_X (f, pix_x),
1818 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1819 FRAME_COLUMN_WIDTH (f) - 1,
1820 FRAME_LINE_HEIGHT (f) - 1);
1821
1822 if (!noclip)
1823 {
1824 if (pix_x < 0)
1825 pix_x = 0;
1826 else if (pix_x > FRAME_TOTAL_COLS (f))
1827 pix_x = FRAME_TOTAL_COLS (f);
1828
1829 if (pix_y < 0)
1830 pix_y = 0;
1831 else if (pix_y > FRAME_LINES (f))
1832 pix_y = FRAME_LINES (f);
1833 }
1834 }
1835 #endif
1836
1837 *x = pix_x;
1838 *y = pix_y;
1839 }
1840
1841
1842 /* Find the glyph under window-relative coordinates X/Y in window W.
1843 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1844 strings. Return in *HPOS and *VPOS the row and column number of
1845 the glyph found. Return in *AREA the glyph area containing X.
1846 Value is a pointer to the glyph found or null if X/Y is not on
1847 text, or we can't tell because W's current matrix is not up to
1848 date. */
1849
1850 static
1851 struct glyph *
1852 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1853 int *dx, int *dy, int *area)
1854 {
1855 struct glyph *glyph, *end;
1856 struct glyph_row *row = NULL;
1857 int x0, i;
1858
1859 /* Find row containing Y. Give up if some row is not enabled. */
1860 for (i = 0; i < w->current_matrix->nrows; ++i)
1861 {
1862 row = MATRIX_ROW (w->current_matrix, i);
1863 if (!row->enabled_p)
1864 return NULL;
1865 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1866 break;
1867 }
1868
1869 *vpos = i;
1870 *hpos = 0;
1871
1872 /* Give up if Y is not in the window. */
1873 if (i == w->current_matrix->nrows)
1874 return NULL;
1875
1876 /* Get the glyph area containing X. */
1877 if (w->pseudo_window_p)
1878 {
1879 *area = TEXT_AREA;
1880 x0 = 0;
1881 }
1882 else
1883 {
1884 if (x < window_box_left_offset (w, TEXT_AREA))
1885 {
1886 *area = LEFT_MARGIN_AREA;
1887 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1888 }
1889 else if (x < window_box_right_offset (w, TEXT_AREA))
1890 {
1891 *area = TEXT_AREA;
1892 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1893 }
1894 else
1895 {
1896 *area = RIGHT_MARGIN_AREA;
1897 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1898 }
1899 }
1900
1901 /* Find glyph containing X. */
1902 glyph = row->glyphs[*area];
1903 end = glyph + row->used[*area];
1904 x -= x0;
1905 while (glyph < end && x >= glyph->pixel_width)
1906 {
1907 x -= glyph->pixel_width;
1908 ++glyph;
1909 }
1910
1911 if (glyph == end)
1912 return NULL;
1913
1914 if (dx)
1915 {
1916 *dx = x;
1917 *dy = y - (row->y + row->ascent - glyph->ascent);
1918 }
1919
1920 *hpos = glyph - row->glyphs[*area];
1921 return glyph;
1922 }
1923
1924 /* Convert frame-relative x/y to coordinates relative to window W.
1925 Takes pseudo-windows into account. */
1926
1927 static void
1928 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1929 {
1930 if (w->pseudo_window_p)
1931 {
1932 /* A pseudo-window is always full-width, and starts at the
1933 left edge of the frame, plus a frame border. */
1934 struct frame *f = XFRAME (w->frame);
1935 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1936 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1937 }
1938 else
1939 {
1940 *x -= WINDOW_LEFT_EDGE_X (w);
1941 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1942 }
1943 }
1944
1945 #ifdef HAVE_WINDOW_SYSTEM
1946
1947 /* EXPORT:
1948 Return in RECTS[] at most N clipping rectangles for glyph string S.
1949 Return the number of stored rectangles. */
1950
1951 int
1952 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1953 {
1954 XRectangle r;
1955
1956 if (n <= 0)
1957 return 0;
1958
1959 if (s->row->full_width_p)
1960 {
1961 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1962 r.x = WINDOW_LEFT_EDGE_X (s->w);
1963 r.width = WINDOW_TOTAL_WIDTH (s->w);
1964
1965 /* Unless displaying a mode or menu bar line, which are always
1966 fully visible, clip to the visible part of the row. */
1967 if (s->w->pseudo_window_p)
1968 r.height = s->row->visible_height;
1969 else
1970 r.height = s->height;
1971 }
1972 else
1973 {
1974 /* This is a text line that may be partially visible. */
1975 r.x = window_box_left (s->w, s->area);
1976 r.width = window_box_width (s->w, s->area);
1977 r.height = s->row->visible_height;
1978 }
1979
1980 if (s->clip_head)
1981 if (r.x < s->clip_head->x)
1982 {
1983 if (r.width >= s->clip_head->x - r.x)
1984 r.width -= s->clip_head->x - r.x;
1985 else
1986 r.width = 0;
1987 r.x = s->clip_head->x;
1988 }
1989 if (s->clip_tail)
1990 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1991 {
1992 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1993 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1994 else
1995 r.width = 0;
1996 }
1997
1998 /* If S draws overlapping rows, it's sufficient to use the top and
1999 bottom of the window for clipping because this glyph string
2000 intentionally draws over other lines. */
2001 if (s->for_overlaps)
2002 {
2003 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2004 r.height = window_text_bottom_y (s->w) - r.y;
2005
2006 /* Alas, the above simple strategy does not work for the
2007 environments with anti-aliased text: if the same text is
2008 drawn onto the same place multiple times, it gets thicker.
2009 If the overlap we are processing is for the erased cursor, we
2010 take the intersection with the rectangle of the cursor. */
2011 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2012 {
2013 XRectangle rc, r_save = r;
2014
2015 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2016 rc.y = s->w->phys_cursor.y;
2017 rc.width = s->w->phys_cursor_width;
2018 rc.height = s->w->phys_cursor_height;
2019
2020 x_intersect_rectangles (&r_save, &rc, &r);
2021 }
2022 }
2023 else
2024 {
2025 /* Don't use S->y for clipping because it doesn't take partially
2026 visible lines into account. For example, it can be negative for
2027 partially visible lines at the top of a window. */
2028 if (!s->row->full_width_p
2029 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2030 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2031 else
2032 r.y = max (0, s->row->y);
2033 }
2034
2035 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2036
2037 /* If drawing the cursor, don't let glyph draw outside its
2038 advertised boundaries. Cleartype does this under some circumstances. */
2039 if (s->hl == DRAW_CURSOR)
2040 {
2041 struct glyph *glyph = s->first_glyph;
2042 int height, max_y;
2043
2044 if (s->x > r.x)
2045 {
2046 r.width -= s->x - r.x;
2047 r.x = s->x;
2048 }
2049 r.width = min (r.width, glyph->pixel_width);
2050
2051 /* If r.y is below window bottom, ensure that we still see a cursor. */
2052 height = min (glyph->ascent + glyph->descent,
2053 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2054 max_y = window_text_bottom_y (s->w) - height;
2055 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2056 if (s->ybase - glyph->ascent > max_y)
2057 {
2058 r.y = max_y;
2059 r.height = height;
2060 }
2061 else
2062 {
2063 /* Don't draw cursor glyph taller than our actual glyph. */
2064 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2065 if (height < r.height)
2066 {
2067 max_y = r.y + r.height;
2068 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2069 r.height = min (max_y - r.y, height);
2070 }
2071 }
2072 }
2073
2074 if (s->row->clip)
2075 {
2076 XRectangle r_save = r;
2077
2078 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2079 r.width = 0;
2080 }
2081
2082 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2083 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2084 {
2085 #ifdef CONVERT_FROM_XRECT
2086 CONVERT_FROM_XRECT (r, *rects);
2087 #else
2088 *rects = r;
2089 #endif
2090 return 1;
2091 }
2092 else
2093 {
2094 /* If we are processing overlapping and allowed to return
2095 multiple clipping rectangles, we exclude the row of the glyph
2096 string from the clipping rectangle. This is to avoid drawing
2097 the same text on the environment with anti-aliasing. */
2098 #ifdef CONVERT_FROM_XRECT
2099 XRectangle rs[2];
2100 #else
2101 XRectangle *rs = rects;
2102 #endif
2103 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2104
2105 if (s->for_overlaps & OVERLAPS_PRED)
2106 {
2107 rs[i] = r;
2108 if (r.y + r.height > row_y)
2109 {
2110 if (r.y < row_y)
2111 rs[i].height = row_y - r.y;
2112 else
2113 rs[i].height = 0;
2114 }
2115 i++;
2116 }
2117 if (s->for_overlaps & OVERLAPS_SUCC)
2118 {
2119 rs[i] = r;
2120 if (r.y < row_y + s->row->visible_height)
2121 {
2122 if (r.y + r.height > row_y + s->row->visible_height)
2123 {
2124 rs[i].y = row_y + s->row->visible_height;
2125 rs[i].height = r.y + r.height - rs[i].y;
2126 }
2127 else
2128 rs[i].height = 0;
2129 }
2130 i++;
2131 }
2132
2133 n = i;
2134 #ifdef CONVERT_FROM_XRECT
2135 for (i = 0; i < n; i++)
2136 CONVERT_FROM_XRECT (rs[i], rects[i]);
2137 #endif
2138 return n;
2139 }
2140 }
2141
2142 /* EXPORT:
2143 Return in *NR the clipping rectangle for glyph string S. */
2144
2145 void
2146 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2147 {
2148 get_glyph_string_clip_rects (s, nr, 1);
2149 }
2150
2151
2152 /* EXPORT:
2153 Return the position and height of the phys cursor in window W.
2154 Set w->phys_cursor_width to width of phys cursor.
2155 */
2156
2157 void
2158 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2159 struct glyph *glyph, int *xp, int *yp, int *heightp)
2160 {
2161 struct frame *f = XFRAME (WINDOW_FRAME (w));
2162 int x, y, wd, h, h0, y0;
2163
2164 /* Compute the width of the rectangle to draw. If on a stretch
2165 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2166 rectangle as wide as the glyph, but use a canonical character
2167 width instead. */
2168 wd = glyph->pixel_width - 1;
2169 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2170 wd++; /* Why? */
2171 #endif
2172
2173 x = w->phys_cursor.x;
2174 if (x < 0)
2175 {
2176 wd += x;
2177 x = 0;
2178 }
2179
2180 if (glyph->type == STRETCH_GLYPH
2181 && !x_stretch_cursor_p)
2182 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2183 w->phys_cursor_width = wd;
2184
2185 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2186
2187 /* If y is below window bottom, ensure that we still see a cursor. */
2188 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2189
2190 h = max (h0, glyph->ascent + glyph->descent);
2191 h0 = min (h0, glyph->ascent + glyph->descent);
2192
2193 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2194 if (y < y0)
2195 {
2196 h = max (h - (y0 - y) + 1, h0);
2197 y = y0 - 1;
2198 }
2199 else
2200 {
2201 y0 = window_text_bottom_y (w) - h0;
2202 if (y > y0)
2203 {
2204 h += y - y0;
2205 y = y0;
2206 }
2207 }
2208
2209 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2210 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2211 *heightp = h;
2212 }
2213
2214 /*
2215 * Remember which glyph the mouse is over.
2216 */
2217
2218 void
2219 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2220 {
2221 Lisp_Object window;
2222 struct window *w;
2223 struct glyph_row *r, *gr, *end_row;
2224 enum window_part part;
2225 enum glyph_row_area area;
2226 int x, y, width, height;
2227
2228 /* Try to determine frame pixel position and size of the glyph under
2229 frame pixel coordinates X/Y on frame F. */
2230
2231 if (!f->glyphs_initialized_p
2232 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2233 NILP (window)))
2234 {
2235 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2236 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2237 goto virtual_glyph;
2238 }
2239
2240 w = XWINDOW (window);
2241 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2242 height = WINDOW_FRAME_LINE_HEIGHT (w);
2243
2244 x = window_relative_x_coord (w, part, gx);
2245 y = gy - WINDOW_TOP_EDGE_Y (w);
2246
2247 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2248 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2249
2250 if (w->pseudo_window_p)
2251 {
2252 area = TEXT_AREA;
2253 part = ON_MODE_LINE; /* Don't adjust margin. */
2254 goto text_glyph;
2255 }
2256
2257 switch (part)
2258 {
2259 case ON_LEFT_MARGIN:
2260 area = LEFT_MARGIN_AREA;
2261 goto text_glyph;
2262
2263 case ON_RIGHT_MARGIN:
2264 area = RIGHT_MARGIN_AREA;
2265 goto text_glyph;
2266
2267 case ON_HEADER_LINE:
2268 case ON_MODE_LINE:
2269 gr = (part == ON_HEADER_LINE
2270 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2271 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2272 gy = gr->y;
2273 area = TEXT_AREA;
2274 goto text_glyph_row_found;
2275
2276 case ON_TEXT:
2277 area = TEXT_AREA;
2278
2279 text_glyph:
2280 gr = 0; gy = 0;
2281 for (; r <= end_row && r->enabled_p; ++r)
2282 if (r->y + r->height > y)
2283 {
2284 gr = r; gy = r->y;
2285 break;
2286 }
2287
2288 text_glyph_row_found:
2289 if (gr && gy <= y)
2290 {
2291 struct glyph *g = gr->glyphs[area];
2292 struct glyph *end = g + gr->used[area];
2293
2294 height = gr->height;
2295 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2296 if (gx + g->pixel_width > x)
2297 break;
2298
2299 if (g < end)
2300 {
2301 if (g->type == IMAGE_GLYPH)
2302 {
2303 /* Don't remember when mouse is over image, as
2304 image may have hot-spots. */
2305 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2306 return;
2307 }
2308 width = g->pixel_width;
2309 }
2310 else
2311 {
2312 /* Use nominal char spacing at end of line. */
2313 x -= gx;
2314 gx += (x / width) * width;
2315 }
2316
2317 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2318 gx += window_box_left_offset (w, area);
2319 }
2320 else
2321 {
2322 /* Use nominal line height at end of window. */
2323 gx = (x / width) * width;
2324 y -= gy;
2325 gy += (y / height) * height;
2326 }
2327 break;
2328
2329 case ON_LEFT_FRINGE:
2330 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2331 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2332 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2333 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2334 goto row_glyph;
2335
2336 case ON_RIGHT_FRINGE:
2337 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2338 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2339 : window_box_right_offset (w, TEXT_AREA));
2340 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2341 goto row_glyph;
2342
2343 case ON_SCROLL_BAR:
2344 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2345 ? 0
2346 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2347 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2348 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2349 : 0)));
2350 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2351
2352 row_glyph:
2353 gr = 0, gy = 0;
2354 for (; r <= end_row && r->enabled_p; ++r)
2355 if (r->y + r->height > y)
2356 {
2357 gr = r; gy = r->y;
2358 break;
2359 }
2360
2361 if (gr && gy <= y)
2362 height = gr->height;
2363 else
2364 {
2365 /* Use nominal line height at end of window. */
2366 y -= gy;
2367 gy += (y / height) * height;
2368 }
2369 break;
2370
2371 default:
2372 ;
2373 virtual_glyph:
2374 /* If there is no glyph under the mouse, then we divide the screen
2375 into a grid of the smallest glyph in the frame, and use that
2376 as our "glyph". */
2377
2378 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2379 round down even for negative values. */
2380 if (gx < 0)
2381 gx -= width - 1;
2382 if (gy < 0)
2383 gy -= height - 1;
2384
2385 gx = (gx / width) * width;
2386 gy = (gy / height) * height;
2387
2388 goto store_rect;
2389 }
2390
2391 gx += WINDOW_LEFT_EDGE_X (w);
2392 gy += WINDOW_TOP_EDGE_Y (w);
2393
2394 store_rect:
2395 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2396
2397 /* Visible feedback for debugging. */
2398 #if 0
2399 #if HAVE_X_WINDOWS
2400 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2401 f->output_data.x->normal_gc,
2402 gx, gy, width, height);
2403 #endif
2404 #endif
2405 }
2406
2407
2408 #endif /* HAVE_WINDOW_SYSTEM */
2409
2410 \f
2411 /***********************************************************************
2412 Lisp form evaluation
2413 ***********************************************************************/
2414
2415 /* Error handler for safe_eval and safe_call. */
2416
2417 static Lisp_Object
2418 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2419 {
2420 add_to_log ("Error during redisplay: %S signaled %S",
2421 Flist (nargs, args), arg);
2422 return Qnil;
2423 }
2424
2425 /* Call function FUNC with the rest of NARGS - 1 arguments
2426 following. Return the result, or nil if something went
2427 wrong. Prevent redisplay during the evaluation. */
2428
2429 Lisp_Object
2430 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2431 {
2432 Lisp_Object val;
2433
2434 if (inhibit_eval_during_redisplay)
2435 val = Qnil;
2436 else
2437 {
2438 va_list ap;
2439 ptrdiff_t i;
2440 ptrdiff_t count = SPECPDL_INDEX ();
2441 struct gcpro gcpro1;
2442 Lisp_Object *args = alloca (nargs * word_size);
2443
2444 args[0] = func;
2445 va_start (ap, func);
2446 for (i = 1; i < nargs; i++)
2447 args[i] = va_arg (ap, Lisp_Object);
2448 va_end (ap);
2449
2450 GCPRO1 (args[0]);
2451 gcpro1.nvars = nargs;
2452 specbind (Qinhibit_redisplay, Qt);
2453 /* Use Qt to ensure debugger does not run,
2454 so there is no possibility of wanting to redisplay. */
2455 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2456 safe_eval_handler);
2457 UNGCPRO;
2458 val = unbind_to (count, val);
2459 }
2460
2461 return val;
2462 }
2463
2464
2465 /* Call function FN with one argument ARG.
2466 Return the result, or nil if something went wrong. */
2467
2468 Lisp_Object
2469 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2470 {
2471 return safe_call (2, fn, arg);
2472 }
2473
2474 static Lisp_Object Qeval;
2475
2476 Lisp_Object
2477 safe_eval (Lisp_Object sexpr)
2478 {
2479 return safe_call1 (Qeval, sexpr);
2480 }
2481
2482 /* Call function FN with two arguments ARG1 and ARG2.
2483 Return the result, or nil if something went wrong. */
2484
2485 Lisp_Object
2486 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2487 {
2488 return safe_call (3, fn, arg1, arg2);
2489 }
2490
2491
2492 \f
2493 /***********************************************************************
2494 Debugging
2495 ***********************************************************************/
2496
2497 #if 0
2498
2499 /* Define CHECK_IT to perform sanity checks on iterators.
2500 This is for debugging. It is too slow to do unconditionally. */
2501
2502 static void
2503 check_it (struct it *it)
2504 {
2505 if (it->method == GET_FROM_STRING)
2506 {
2507 eassert (STRINGP (it->string));
2508 eassert (IT_STRING_CHARPOS (*it) >= 0);
2509 }
2510 else
2511 {
2512 eassert (IT_STRING_CHARPOS (*it) < 0);
2513 if (it->method == GET_FROM_BUFFER)
2514 {
2515 /* Check that character and byte positions agree. */
2516 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2517 }
2518 }
2519
2520 if (it->dpvec)
2521 eassert (it->current.dpvec_index >= 0);
2522 else
2523 eassert (it->current.dpvec_index < 0);
2524 }
2525
2526 #define CHECK_IT(IT) check_it ((IT))
2527
2528 #else /* not 0 */
2529
2530 #define CHECK_IT(IT) (void) 0
2531
2532 #endif /* not 0 */
2533
2534
2535 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2536
2537 /* Check that the window end of window W is what we expect it
2538 to be---the last row in the current matrix displaying text. */
2539
2540 static void
2541 check_window_end (struct window *w)
2542 {
2543 if (!MINI_WINDOW_P (w) && w->window_end_valid)
2544 {
2545 struct glyph_row *row;
2546 eassert ((row = MATRIX_ROW (w->current_matrix,
2547 XFASTINT (w->window_end_vpos)),
2548 !row->enabled_p
2549 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2550 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2551 }
2552 }
2553
2554 #define CHECK_WINDOW_END(W) check_window_end ((W))
2555
2556 #else
2557
2558 #define CHECK_WINDOW_END(W) (void) 0
2559
2560 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2561
2562 /* Return mark position if current buffer has the region of non-zero length,
2563 or -1 otherwise. */
2564
2565 static ptrdiff_t
2566 markpos_of_region (void)
2567 {
2568 if (!NILP (Vtransient_mark_mode)
2569 && !NILP (BVAR (current_buffer, mark_active))
2570 && XMARKER (BVAR (current_buffer, mark))->buffer != NULL)
2571 {
2572 ptrdiff_t markpos = XMARKER (BVAR (current_buffer, mark))->charpos;
2573
2574 if (markpos != PT)
2575 return markpos;
2576 }
2577 return -1;
2578 }
2579
2580 /***********************************************************************
2581 Iterator initialization
2582 ***********************************************************************/
2583
2584 /* Initialize IT for displaying current_buffer in window W, starting
2585 at character position CHARPOS. CHARPOS < 0 means that no buffer
2586 position is specified which is useful when the iterator is assigned
2587 a position later. BYTEPOS is the byte position corresponding to
2588 CHARPOS.
2589
2590 If ROW is not null, calls to produce_glyphs with IT as parameter
2591 will produce glyphs in that row.
2592
2593 BASE_FACE_ID is the id of a base face to use. It must be one of
2594 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2595 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2596 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2597
2598 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2599 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2600 will be initialized to use the corresponding mode line glyph row of
2601 the desired matrix of W. */
2602
2603 void
2604 init_iterator (struct it *it, struct window *w,
2605 ptrdiff_t charpos, ptrdiff_t bytepos,
2606 struct glyph_row *row, enum face_id base_face_id)
2607 {
2608 ptrdiff_t markpos;
2609 enum face_id remapped_base_face_id = base_face_id;
2610
2611 /* Some precondition checks. */
2612 eassert (w != NULL && it != NULL);
2613 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2614 && charpos <= ZV));
2615
2616 /* If face attributes have been changed since the last redisplay,
2617 free realized faces now because they depend on face definitions
2618 that might have changed. Don't free faces while there might be
2619 desired matrices pending which reference these faces. */
2620 if (face_change_count && !inhibit_free_realized_faces)
2621 {
2622 face_change_count = 0;
2623 free_all_realized_faces (Qnil);
2624 }
2625
2626 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2627 if (! NILP (Vface_remapping_alist))
2628 remapped_base_face_id
2629 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2630
2631 /* Use one of the mode line rows of W's desired matrix if
2632 appropriate. */
2633 if (row == NULL)
2634 {
2635 if (base_face_id == MODE_LINE_FACE_ID
2636 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2637 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2638 else if (base_face_id == HEADER_LINE_FACE_ID)
2639 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2640 }
2641
2642 /* Clear IT. */
2643 memset (it, 0, sizeof *it);
2644 it->current.overlay_string_index = -1;
2645 it->current.dpvec_index = -1;
2646 it->base_face_id = remapped_base_face_id;
2647 it->string = Qnil;
2648 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2649 it->paragraph_embedding = L2R;
2650 it->bidi_it.string.lstring = Qnil;
2651 it->bidi_it.string.s = NULL;
2652 it->bidi_it.string.bufpos = 0;
2653
2654 /* The window in which we iterate over current_buffer: */
2655 XSETWINDOW (it->window, w);
2656 it->w = w;
2657 it->f = XFRAME (w->frame);
2658
2659 it->cmp_it.id = -1;
2660
2661 /* Extra space between lines (on window systems only). */
2662 if (base_face_id == DEFAULT_FACE_ID
2663 && FRAME_WINDOW_P (it->f))
2664 {
2665 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2666 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2667 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2668 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2669 * FRAME_LINE_HEIGHT (it->f));
2670 else if (it->f->extra_line_spacing > 0)
2671 it->extra_line_spacing = it->f->extra_line_spacing;
2672 it->max_extra_line_spacing = 0;
2673 }
2674
2675 /* If realized faces have been removed, e.g. because of face
2676 attribute changes of named faces, recompute them. When running
2677 in batch mode, the face cache of the initial frame is null. If
2678 we happen to get called, make a dummy face cache. */
2679 if (FRAME_FACE_CACHE (it->f) == NULL)
2680 init_frame_faces (it->f);
2681 if (FRAME_FACE_CACHE (it->f)->used == 0)
2682 recompute_basic_faces (it->f);
2683
2684 /* Current value of the `slice', `space-width', and 'height' properties. */
2685 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2686 it->space_width = Qnil;
2687 it->font_height = Qnil;
2688 it->override_ascent = -1;
2689
2690 /* Are control characters displayed as `^C'? */
2691 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2692
2693 /* -1 means everything between a CR and the following line end
2694 is invisible. >0 means lines indented more than this value are
2695 invisible. */
2696 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2697 ? (clip_to_bounds
2698 (-1, XINT (BVAR (current_buffer, selective_display)),
2699 PTRDIFF_MAX))
2700 : (!NILP (BVAR (current_buffer, selective_display))
2701 ? -1 : 0));
2702 it->selective_display_ellipsis_p
2703 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2704
2705 /* Display table to use. */
2706 it->dp = window_display_table (w);
2707
2708 /* Are multibyte characters enabled in current_buffer? */
2709 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2710
2711 /* If visible region is of non-zero length, set IT->region_beg_charpos
2712 and IT->region_end_charpos to the start and end of a visible region
2713 in window IT->w. Set both to -1 to indicate no region. */
2714 markpos = markpos_of_region ();
2715 if (markpos >= 0
2716 /* Maybe highlight only in selected window. */
2717 && (/* Either show region everywhere. */
2718 highlight_nonselected_windows
2719 /* Or show region in the selected window. */
2720 || w == XWINDOW (selected_window)
2721 /* Or show the region if we are in the mini-buffer and W is
2722 the window the mini-buffer refers to. */
2723 || (MINI_WINDOW_P (XWINDOW (selected_window))
2724 && WINDOWP (minibuf_selected_window)
2725 && w == XWINDOW (minibuf_selected_window))))
2726 {
2727 it->region_beg_charpos = min (PT, markpos);
2728 it->region_end_charpos = max (PT, markpos);
2729 }
2730 else
2731 it->region_beg_charpos = it->region_end_charpos = -1;
2732
2733 /* Get the position at which the redisplay_end_trigger hook should
2734 be run, if it is to be run at all. */
2735 if (MARKERP (w->redisplay_end_trigger)
2736 && XMARKER (w->redisplay_end_trigger)->buffer != 0)
2737 it->redisplay_end_trigger_charpos
2738 = marker_position (w->redisplay_end_trigger);
2739 else if (INTEGERP (w->redisplay_end_trigger))
2740 it->redisplay_end_trigger_charpos =
2741 clip_to_bounds (PTRDIFF_MIN, XINT (w->redisplay_end_trigger), PTRDIFF_MAX);
2742
2743 it->tab_width = SANE_TAB_WIDTH (current_buffer);
2744
2745 /* Are lines in the display truncated? */
2746 if (base_face_id != DEFAULT_FACE_ID
2747 || it->w->hscroll
2748 || (! WINDOW_FULL_WIDTH_P (it->w)
2749 && ((!NILP (Vtruncate_partial_width_windows)
2750 && !INTEGERP (Vtruncate_partial_width_windows))
2751 || (INTEGERP (Vtruncate_partial_width_windows)
2752 && (WINDOW_TOTAL_COLS (it->w)
2753 < XINT (Vtruncate_partial_width_windows))))))
2754 it->line_wrap = TRUNCATE;
2755 else if (NILP (BVAR (current_buffer, truncate_lines)))
2756 it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
2757 ? WINDOW_WRAP : WORD_WRAP;
2758 else
2759 it->line_wrap = TRUNCATE;
2760
2761 /* Get dimensions of truncation and continuation glyphs. These are
2762 displayed as fringe bitmaps under X, but we need them for such
2763 frames when the fringes are turned off. But leave the dimensions
2764 zero for tooltip frames, as these glyphs look ugly there and also
2765 sabotage calculations of tooltip dimensions in x-show-tip. */
2766 #ifdef HAVE_WINDOW_SYSTEM
2767 if (!(FRAME_WINDOW_P (it->f)
2768 && FRAMEP (tip_frame)
2769 && it->f == XFRAME (tip_frame)))
2770 #endif
2771 {
2772 if (it->line_wrap == TRUNCATE)
2773 {
2774 /* We will need the truncation glyph. */
2775 eassert (it->glyph_row == NULL);
2776 produce_special_glyphs (it, IT_TRUNCATION);
2777 it->truncation_pixel_width = it->pixel_width;
2778 }
2779 else
2780 {
2781 /* We will need the continuation glyph. */
2782 eassert (it->glyph_row == NULL);
2783 produce_special_glyphs (it, IT_CONTINUATION);
2784 it->continuation_pixel_width = it->pixel_width;
2785 }
2786 }
2787
2788 /* Reset these values to zero because the produce_special_glyphs
2789 above has changed them. */
2790 it->pixel_width = it->ascent = it->descent = 0;
2791 it->phys_ascent = it->phys_descent = 0;
2792
2793 /* Set this after getting the dimensions of truncation and
2794 continuation glyphs, so that we don't produce glyphs when calling
2795 produce_special_glyphs, above. */
2796 it->glyph_row = row;
2797 it->area = TEXT_AREA;
2798
2799 /* Forget any previous info about this row being reversed. */
2800 if (it->glyph_row)
2801 it->glyph_row->reversed_p = 0;
2802
2803 /* Get the dimensions of the display area. The display area
2804 consists of the visible window area plus a horizontally scrolled
2805 part to the left of the window. All x-values are relative to the
2806 start of this total display area. */
2807 if (base_face_id != DEFAULT_FACE_ID)
2808 {
2809 /* Mode lines, menu bar in terminal frames. */
2810 it->first_visible_x = 0;
2811 it->last_visible_x = WINDOW_TOTAL_WIDTH (w);
2812 }
2813 else
2814 {
2815 it->first_visible_x =
2816 window_hscroll_limited (it->w, it->f) * FRAME_COLUMN_WIDTH (it->f);
2817 it->last_visible_x = (it->first_visible_x
2818 + window_box_width (w, TEXT_AREA));
2819
2820 /* If we truncate lines, leave room for the truncation glyph(s) at
2821 the right margin. Otherwise, leave room for the continuation
2822 glyph(s). Done only if the window has no fringes. Since we
2823 don't know at this point whether there will be any R2L lines in
2824 the window, we reserve space for truncation/continuation glyphs
2825 even if only one of the fringes is absent. */
2826 if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
2827 || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
2828 {
2829 if (it->line_wrap == TRUNCATE)
2830 it->last_visible_x -= it->truncation_pixel_width;
2831 else
2832 it->last_visible_x -= it->continuation_pixel_width;
2833 }
2834
2835 it->header_line_p = WINDOW_WANTS_HEADER_LINE_P (w);
2836 it->current_y = WINDOW_HEADER_LINE_HEIGHT (w) + w->vscroll;
2837 }
2838
2839 /* Leave room for a border glyph. */
2840 if (!FRAME_WINDOW_P (it->f)
2841 && !WINDOW_RIGHTMOST_P (it->w))
2842 it->last_visible_x -= 1;
2843
2844 it->last_visible_y = window_text_bottom_y (w);
2845
2846 /* For mode lines and alike, arrange for the first glyph having a
2847 left box line if the face specifies a box. */
2848 if (base_face_id != DEFAULT_FACE_ID)
2849 {
2850 struct face *face;
2851
2852 it->face_id = remapped_base_face_id;
2853
2854 /* If we have a boxed mode line, make the first character appear
2855 with a left box line. */
2856 face = FACE_FROM_ID (it->f, remapped_base_face_id);
2857 if (face->box != FACE_NO_BOX)
2858 it->start_of_box_run_p = 1;
2859 }
2860
2861 /* If a buffer position was specified, set the iterator there,
2862 getting overlays and face properties from that position. */
2863 if (charpos >= BUF_BEG (current_buffer))
2864 {
2865 it->end_charpos = ZV;
2866 eassert (charpos == BYTE_TO_CHAR (bytepos));
2867 IT_CHARPOS (*it) = charpos;
2868 IT_BYTEPOS (*it) = bytepos;
2869
2870 /* We will rely on `reseat' to set this up properly, via
2871 handle_face_prop. */
2872 it->face_id = it->base_face_id;
2873
2874 it->start = it->current;
2875 /* Do we need to reorder bidirectional text? Not if this is a
2876 unibyte buffer: by definition, none of the single-byte
2877 characters are strong R2L, so no reordering is needed. And
2878 bidi.c doesn't support unibyte buffers anyway. Also, don't
2879 reorder while we are loading loadup.el, since the tables of
2880 character properties needed for reordering are not yet
2881 available. */
2882 it->bidi_p =
2883 NILP (Vpurify_flag)
2884 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2885 && it->multibyte_p;
2886
2887 /* If we are to reorder bidirectional text, init the bidi
2888 iterator. */
2889 if (it->bidi_p)
2890 {
2891 /* Note the paragraph direction that this buffer wants to
2892 use. */
2893 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2894 Qleft_to_right))
2895 it->paragraph_embedding = L2R;
2896 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2897 Qright_to_left))
2898 it->paragraph_embedding = R2L;
2899 else
2900 it->paragraph_embedding = NEUTRAL_DIR;
2901 bidi_unshelve_cache (NULL, 0);
2902 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2903 &it->bidi_it);
2904 }
2905
2906 /* Compute faces etc. */
2907 reseat (it, it->current.pos, 1);
2908 }
2909
2910 CHECK_IT (it);
2911 }
2912
2913
2914 /* Initialize IT for the display of window W with window start POS. */
2915
2916 void
2917 start_display (struct it *it, struct window *w, struct text_pos pos)
2918 {
2919 struct glyph_row *row;
2920 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2921
2922 row = w->desired_matrix->rows + first_vpos;
2923 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2924 it->first_vpos = first_vpos;
2925
2926 /* Don't reseat to previous visible line start if current start
2927 position is in a string or image. */
2928 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2929 {
2930 int start_at_line_beg_p;
2931 int first_y = it->current_y;
2932
2933 /* If window start is not at a line start, skip forward to POS to
2934 get the correct continuation lines width. */
2935 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2936 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2937 if (!start_at_line_beg_p)
2938 {
2939 int new_x;
2940
2941 reseat_at_previous_visible_line_start (it);
2942 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2943
2944 new_x = it->current_x + it->pixel_width;
2945
2946 /* If lines are continued, this line may end in the middle
2947 of a multi-glyph character (e.g. a control character
2948 displayed as \003, or in the middle of an overlay
2949 string). In this case move_it_to above will not have
2950 taken us to the start of the continuation line but to the
2951 end of the continued line. */
2952 if (it->current_x > 0
2953 && it->line_wrap != TRUNCATE /* Lines are continued. */
2954 && (/* And glyph doesn't fit on the line. */
2955 new_x > it->last_visible_x
2956 /* Or it fits exactly and we're on a window
2957 system frame. */
2958 || (new_x == it->last_visible_x
2959 && FRAME_WINDOW_P (it->f)
2960 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2961 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2962 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2963 {
2964 if ((it->current.dpvec_index >= 0
2965 || it->current.overlay_string_index >= 0)
2966 /* If we are on a newline from a display vector or
2967 overlay string, then we are already at the end of
2968 a screen line; no need to go to the next line in
2969 that case, as this line is not really continued.
2970 (If we do go to the next line, C-e will not DTRT.) */
2971 && it->c != '\n')
2972 {
2973 set_iterator_to_next (it, 1);
2974 move_it_in_display_line_to (it, -1, -1, 0);
2975 }
2976
2977 it->continuation_lines_width += it->current_x;
2978 }
2979 /* If the character at POS is displayed via a display
2980 vector, move_it_to above stops at the final glyph of
2981 IT->dpvec. To make the caller redisplay that character
2982 again (a.k.a. start at POS), we need to reset the
2983 dpvec_index to the beginning of IT->dpvec. */
2984 else if (it->current.dpvec_index >= 0)
2985 it->current.dpvec_index = 0;
2986
2987 /* We're starting a new display line, not affected by the
2988 height of the continued line, so clear the appropriate
2989 fields in the iterator structure. */
2990 it->max_ascent = it->max_descent = 0;
2991 it->max_phys_ascent = it->max_phys_descent = 0;
2992
2993 it->current_y = first_y;
2994 it->vpos = 0;
2995 it->current_x = it->hpos = 0;
2996 }
2997 }
2998 }
2999
3000
3001 /* Return 1 if POS is a position in ellipses displayed for invisible
3002 text. W is the window we display, for text property lookup. */
3003
3004 static int
3005 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3006 {
3007 Lisp_Object prop, window;
3008 int ellipses_p = 0;
3009 ptrdiff_t charpos = CHARPOS (pos->pos);
3010
3011 /* If POS specifies a position in a display vector, this might
3012 be for an ellipsis displayed for invisible text. We won't
3013 get the iterator set up for delivering that ellipsis unless
3014 we make sure that it gets aware of the invisible text. */
3015 if (pos->dpvec_index >= 0
3016 && pos->overlay_string_index < 0
3017 && CHARPOS (pos->string_pos) < 0
3018 && charpos > BEGV
3019 && (XSETWINDOW (window, w),
3020 prop = Fget_char_property (make_number (charpos),
3021 Qinvisible, window),
3022 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3023 {
3024 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3025 window);
3026 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3027 }
3028
3029 return ellipses_p;
3030 }
3031
3032
3033 /* Initialize IT for stepping through current_buffer in window W,
3034 starting at position POS that includes overlay string and display
3035 vector/ control character translation position information. Value
3036 is zero if there are overlay strings with newlines at POS. */
3037
3038 static int
3039 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3040 {
3041 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3042 int i, overlay_strings_with_newlines = 0;
3043
3044 /* If POS specifies a position in a display vector, this might
3045 be for an ellipsis displayed for invisible text. We won't
3046 get the iterator set up for delivering that ellipsis unless
3047 we make sure that it gets aware of the invisible text. */
3048 if (in_ellipses_for_invisible_text_p (pos, w))
3049 {
3050 --charpos;
3051 bytepos = 0;
3052 }
3053
3054 /* Keep in mind: the call to reseat in init_iterator skips invisible
3055 text, so we might end up at a position different from POS. This
3056 is only a problem when POS is a row start after a newline and an
3057 overlay starts there with an after-string, and the overlay has an
3058 invisible property. Since we don't skip invisible text in
3059 display_line and elsewhere immediately after consuming the
3060 newline before the row start, such a POS will not be in a string,
3061 but the call to init_iterator below will move us to the
3062 after-string. */
3063 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3064
3065 /* This only scans the current chunk -- it should scan all chunks.
3066 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3067 to 16 in 22.1 to make this a lesser problem. */
3068 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3069 {
3070 const char *s = SSDATA (it->overlay_strings[i]);
3071 const char *e = s + SBYTES (it->overlay_strings[i]);
3072
3073 while (s < e && *s != '\n')
3074 ++s;
3075
3076 if (s < e)
3077 {
3078 overlay_strings_with_newlines = 1;
3079 break;
3080 }
3081 }
3082
3083 /* If position is within an overlay string, set up IT to the right
3084 overlay string. */
3085 if (pos->overlay_string_index >= 0)
3086 {
3087 int relative_index;
3088
3089 /* If the first overlay string happens to have a `display'
3090 property for an image, the iterator will be set up for that
3091 image, and we have to undo that setup first before we can
3092 correct the overlay string index. */
3093 if (it->method == GET_FROM_IMAGE)
3094 pop_it (it);
3095
3096 /* We already have the first chunk of overlay strings in
3097 IT->overlay_strings. Load more until the one for
3098 pos->overlay_string_index is in IT->overlay_strings. */
3099 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3100 {
3101 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3102 it->current.overlay_string_index = 0;
3103 while (n--)
3104 {
3105 load_overlay_strings (it, 0);
3106 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3107 }
3108 }
3109
3110 it->current.overlay_string_index = pos->overlay_string_index;
3111 relative_index = (it->current.overlay_string_index
3112 % OVERLAY_STRING_CHUNK_SIZE);
3113 it->string = it->overlay_strings[relative_index];
3114 eassert (STRINGP (it->string));
3115 it->current.string_pos = pos->string_pos;
3116 it->method = GET_FROM_STRING;
3117 it->end_charpos = SCHARS (it->string);
3118 /* Set up the bidi iterator for this overlay string. */
3119 if (it->bidi_p)
3120 {
3121 it->bidi_it.string.lstring = it->string;
3122 it->bidi_it.string.s = NULL;
3123 it->bidi_it.string.schars = SCHARS (it->string);
3124 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3125 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3126 it->bidi_it.string.unibyte = !it->multibyte_p;
3127 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3128 FRAME_WINDOW_P (it->f), &it->bidi_it);
3129
3130 /* Synchronize the state of the bidi iterator with
3131 pos->string_pos. For any string position other than
3132 zero, this will be done automagically when we resume
3133 iteration over the string and get_visually_first_element
3134 is called. But if string_pos is zero, and the string is
3135 to be reordered for display, we need to resync manually,
3136 since it could be that the iteration state recorded in
3137 pos ended at string_pos of 0 moving backwards in string. */
3138 if (CHARPOS (pos->string_pos) == 0)
3139 {
3140 get_visually_first_element (it);
3141 if (IT_STRING_CHARPOS (*it) != 0)
3142 do {
3143 /* Paranoia. */
3144 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3145 bidi_move_to_visually_next (&it->bidi_it);
3146 } while (it->bidi_it.charpos != 0);
3147 }
3148 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3149 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3150 }
3151 }
3152
3153 if (CHARPOS (pos->string_pos) >= 0)
3154 {
3155 /* Recorded position is not in an overlay string, but in another
3156 string. This can only be a string from a `display' property.
3157 IT should already be filled with that string. */
3158 it->current.string_pos = pos->string_pos;
3159 eassert (STRINGP (it->string));
3160 if (it->bidi_p)
3161 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3162 FRAME_WINDOW_P (it->f), &it->bidi_it);
3163 }
3164
3165 /* Restore position in display vector translations, control
3166 character translations or ellipses. */
3167 if (pos->dpvec_index >= 0)
3168 {
3169 if (it->dpvec == NULL)
3170 get_next_display_element (it);
3171 eassert (it->dpvec && it->current.dpvec_index == 0);
3172 it->current.dpvec_index = pos->dpvec_index;
3173 }
3174
3175 CHECK_IT (it);
3176 return !overlay_strings_with_newlines;
3177 }
3178
3179
3180 /* Initialize IT for stepping through current_buffer in window W
3181 starting at ROW->start. */
3182
3183 static void
3184 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3185 {
3186 init_from_display_pos (it, w, &row->start);
3187 it->start = row->start;
3188 it->continuation_lines_width = row->continuation_lines_width;
3189 CHECK_IT (it);
3190 }
3191
3192
3193 /* Initialize IT for stepping through current_buffer in window W
3194 starting in the line following ROW, i.e. starting at ROW->end.
3195 Value is zero if there are overlay strings with newlines at ROW's
3196 end position. */
3197
3198 static int
3199 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3200 {
3201 int success = 0;
3202
3203 if (init_from_display_pos (it, w, &row->end))
3204 {
3205 if (row->continued_p)
3206 it->continuation_lines_width
3207 = row->continuation_lines_width + row->pixel_width;
3208 CHECK_IT (it);
3209 success = 1;
3210 }
3211
3212 return success;
3213 }
3214
3215
3216
3217 \f
3218 /***********************************************************************
3219 Text properties
3220 ***********************************************************************/
3221
3222 /* Called when IT reaches IT->stop_charpos. Handle text property and
3223 overlay changes. Set IT->stop_charpos to the next position where
3224 to stop. */
3225
3226 static void
3227 handle_stop (struct it *it)
3228 {
3229 enum prop_handled handled;
3230 int handle_overlay_change_p;
3231 struct props *p;
3232
3233 it->dpvec = NULL;
3234 it->current.dpvec_index = -1;
3235 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3236 it->ignore_overlay_strings_at_pos_p = 0;
3237 it->ellipsis_p = 0;
3238
3239 /* Use face of preceding text for ellipsis (if invisible) */
3240 if (it->selective_display_ellipsis_p)
3241 it->saved_face_id = it->face_id;
3242
3243 do
3244 {
3245 handled = HANDLED_NORMALLY;
3246
3247 /* Call text property handlers. */
3248 for (p = it_props; p->handler; ++p)
3249 {
3250 handled = p->handler (it);
3251
3252 if (handled == HANDLED_RECOMPUTE_PROPS)
3253 break;
3254 else if (handled == HANDLED_RETURN)
3255 {
3256 /* We still want to show before and after strings from
3257 overlays even if the actual buffer text is replaced. */
3258 if (!handle_overlay_change_p
3259 || it->sp > 1
3260 /* Don't call get_overlay_strings_1 if we already
3261 have overlay strings loaded, because doing so
3262 will load them again and push the iterator state
3263 onto the stack one more time, which is not
3264 expected by the rest of the code that processes
3265 overlay strings. */
3266 || (it->current.overlay_string_index < 0
3267 ? !get_overlay_strings_1 (it, 0, 0)
3268 : 0))
3269 {
3270 if (it->ellipsis_p)
3271 setup_for_ellipsis (it, 0);
3272 /* When handling a display spec, we might load an
3273 empty string. In that case, discard it here. We
3274 used to discard it in handle_single_display_spec,
3275 but that causes get_overlay_strings_1, above, to
3276 ignore overlay strings that we must check. */
3277 if (STRINGP (it->string) && !SCHARS (it->string))
3278 pop_it (it);
3279 return;
3280 }
3281 else if (STRINGP (it->string) && !SCHARS (it->string))
3282 pop_it (it);
3283 else
3284 {
3285 it->ignore_overlay_strings_at_pos_p = 1;
3286 it->string_from_display_prop_p = 0;
3287 it->from_disp_prop_p = 0;
3288 handle_overlay_change_p = 0;
3289 }
3290 handled = HANDLED_RECOMPUTE_PROPS;
3291 break;
3292 }
3293 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3294 handle_overlay_change_p = 0;
3295 }
3296
3297 if (handled != HANDLED_RECOMPUTE_PROPS)
3298 {
3299 /* Don't check for overlay strings below when set to deliver
3300 characters from a display vector. */
3301 if (it->method == GET_FROM_DISPLAY_VECTOR)
3302 handle_overlay_change_p = 0;
3303
3304 /* Handle overlay changes.
3305 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3306 if it finds overlays. */
3307 if (handle_overlay_change_p)
3308 handled = handle_overlay_change (it);
3309 }
3310
3311 if (it->ellipsis_p)
3312 {
3313 setup_for_ellipsis (it, 0);
3314 break;
3315 }
3316 }
3317 while (handled == HANDLED_RECOMPUTE_PROPS);
3318
3319 /* Determine where to stop next. */
3320 if (handled == HANDLED_NORMALLY)
3321 compute_stop_pos (it);
3322 }
3323
3324
3325 /* Compute IT->stop_charpos from text property and overlay change
3326 information for IT's current position. */
3327
3328 static void
3329 compute_stop_pos (struct it *it)
3330 {
3331 register INTERVAL iv, next_iv;
3332 Lisp_Object object, limit, position;
3333 ptrdiff_t charpos, bytepos;
3334
3335 if (STRINGP (it->string))
3336 {
3337 /* Strings are usually short, so don't limit the search for
3338 properties. */
3339 it->stop_charpos = it->end_charpos;
3340 object = it->string;
3341 limit = Qnil;
3342 charpos = IT_STRING_CHARPOS (*it);
3343 bytepos = IT_STRING_BYTEPOS (*it);
3344 }
3345 else
3346 {
3347 ptrdiff_t pos;
3348
3349 /* If end_charpos is out of range for some reason, such as a
3350 misbehaving display function, rationalize it (Bug#5984). */
3351 if (it->end_charpos > ZV)
3352 it->end_charpos = ZV;
3353 it->stop_charpos = it->end_charpos;
3354
3355 /* If next overlay change is in front of the current stop pos
3356 (which is IT->end_charpos), stop there. Note: value of
3357 next_overlay_change is point-max if no overlay change
3358 follows. */
3359 charpos = IT_CHARPOS (*it);
3360 bytepos = IT_BYTEPOS (*it);
3361 pos = next_overlay_change (charpos);
3362 if (pos < it->stop_charpos)
3363 it->stop_charpos = pos;
3364
3365 /* If showing the region, we have to stop at the region
3366 start or end because the face might change there. */
3367 if (it->region_beg_charpos > 0)
3368 {
3369 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3370 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3371 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3372 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3373 }
3374
3375 /* Set up variables for computing the stop position from text
3376 property changes. */
3377 XSETBUFFER (object, current_buffer);
3378 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3379 }
3380
3381 /* Get the interval containing IT's position. Value is a null
3382 interval if there isn't such an interval. */
3383 position = make_number (charpos);
3384 iv = validate_interval_range (object, &position, &position, 0);
3385 if (iv)
3386 {
3387 Lisp_Object values_here[LAST_PROP_IDX];
3388 struct props *p;
3389
3390 /* Get properties here. */
3391 for (p = it_props; p->handler; ++p)
3392 values_here[p->idx] = textget (iv->plist, *p->name);
3393
3394 /* Look for an interval following iv that has different
3395 properties. */
3396 for (next_iv = next_interval (iv);
3397 (next_iv
3398 && (NILP (limit)
3399 || XFASTINT (limit) > next_iv->position));
3400 next_iv = next_interval (next_iv))
3401 {
3402 for (p = it_props; p->handler; ++p)
3403 {
3404 Lisp_Object new_value;
3405
3406 new_value = textget (next_iv->plist, *p->name);
3407 if (!EQ (values_here[p->idx], new_value))
3408 break;
3409 }
3410
3411 if (p->handler)
3412 break;
3413 }
3414
3415 if (next_iv)
3416 {
3417 if (INTEGERP (limit)
3418 && next_iv->position >= XFASTINT (limit))
3419 /* No text property change up to limit. */
3420 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3421 else
3422 /* Text properties change in next_iv. */
3423 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3424 }
3425 }
3426
3427 if (it->cmp_it.id < 0)
3428 {
3429 ptrdiff_t stoppos = it->end_charpos;
3430
3431 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3432 stoppos = -1;
3433 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3434 stoppos, it->string);
3435 }
3436
3437 eassert (STRINGP (it->string)
3438 || (it->stop_charpos >= BEGV
3439 && it->stop_charpos >= IT_CHARPOS (*it)));
3440 }
3441
3442
3443 /* Return the position of the next overlay change after POS in
3444 current_buffer. Value is point-max if no overlay change
3445 follows. This is like `next-overlay-change' but doesn't use
3446 xmalloc. */
3447
3448 static ptrdiff_t
3449 next_overlay_change (ptrdiff_t pos)
3450 {
3451 ptrdiff_t i, noverlays;
3452 ptrdiff_t endpos;
3453 Lisp_Object *overlays;
3454
3455 /* Get all overlays at the given position. */
3456 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3457
3458 /* If any of these overlays ends before endpos,
3459 use its ending point instead. */
3460 for (i = 0; i < noverlays; ++i)
3461 {
3462 Lisp_Object oend;
3463 ptrdiff_t oendpos;
3464
3465 oend = OVERLAY_END (overlays[i]);
3466 oendpos = OVERLAY_POSITION (oend);
3467 endpos = min (endpos, oendpos);
3468 }
3469
3470 return endpos;
3471 }
3472
3473 /* How many characters forward to search for a display property or
3474 display string. Searching too far forward makes the bidi display
3475 sluggish, especially in small windows. */
3476 #define MAX_DISP_SCAN 250
3477
3478 /* Return the character position of a display string at or after
3479 position specified by POSITION. If no display string exists at or
3480 after POSITION, return ZV. A display string is either an overlay
3481 with `display' property whose value is a string, or a `display'
3482 text property whose value is a string. STRING is data about the
3483 string to iterate; if STRING->lstring is nil, we are iterating a
3484 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3485 on a GUI frame. DISP_PROP is set to zero if we searched
3486 MAX_DISP_SCAN characters forward without finding any display
3487 strings, non-zero otherwise. It is set to 2 if the display string
3488 uses any kind of `(space ...)' spec that will produce a stretch of
3489 white space in the text area. */
3490 ptrdiff_t
3491 compute_display_string_pos (struct text_pos *position,
3492 struct bidi_string_data *string,
3493 int frame_window_p, int *disp_prop)
3494 {
3495 /* OBJECT = nil means current buffer. */
3496 Lisp_Object object =
3497 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3498 Lisp_Object pos, spec, limpos;
3499 int string_p = (string && (STRINGP (string->lstring) || string->s));
3500 ptrdiff_t eob = string_p ? string->schars : ZV;
3501 ptrdiff_t begb = string_p ? 0 : BEGV;
3502 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3503 ptrdiff_t lim =
3504 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3505 struct text_pos tpos;
3506 int rv = 0;
3507
3508 *disp_prop = 1;
3509
3510 if (charpos >= eob
3511 /* We don't support display properties whose values are strings
3512 that have display string properties. */
3513 || string->from_disp_str
3514 /* C strings cannot have display properties. */
3515 || (string->s && !STRINGP (object)))
3516 {
3517 *disp_prop = 0;
3518 return eob;
3519 }
3520
3521 /* If the character at CHARPOS is where the display string begins,
3522 return CHARPOS. */
3523 pos = make_number (charpos);
3524 if (STRINGP (object))
3525 bufpos = string->bufpos;
3526 else
3527 bufpos = charpos;
3528 tpos = *position;
3529 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3530 && (charpos <= begb
3531 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3532 object),
3533 spec))
3534 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3535 frame_window_p)))
3536 {
3537 if (rv == 2)
3538 *disp_prop = 2;
3539 return charpos;
3540 }
3541
3542 /* Look forward for the first character with a `display' property
3543 that will replace the underlying text when displayed. */
3544 limpos = make_number (lim);
3545 do {
3546 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3547 CHARPOS (tpos) = XFASTINT (pos);
3548 if (CHARPOS (tpos) >= lim)
3549 {
3550 *disp_prop = 0;
3551 break;
3552 }
3553 if (STRINGP (object))
3554 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3555 else
3556 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3557 spec = Fget_char_property (pos, Qdisplay, object);
3558 if (!STRINGP (object))
3559 bufpos = CHARPOS (tpos);
3560 } while (NILP (spec)
3561 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3562 bufpos, frame_window_p)));
3563 if (rv == 2)
3564 *disp_prop = 2;
3565
3566 return CHARPOS (tpos);
3567 }
3568
3569 /* Return the character position of the end of the display string that
3570 started at CHARPOS. If there's no display string at CHARPOS,
3571 return -1. A display string is either an overlay with `display'
3572 property whose value is a string or a `display' text property whose
3573 value is a string. */
3574 ptrdiff_t
3575 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3576 {
3577 /* OBJECT = nil means current buffer. */
3578 Lisp_Object object =
3579 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3580 Lisp_Object pos = make_number (charpos);
3581 ptrdiff_t eob =
3582 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3583
3584 if (charpos >= eob || (string->s && !STRINGP (object)))
3585 return eob;
3586
3587 /* It could happen that the display property or overlay was removed
3588 since we found it in compute_display_string_pos above. One way
3589 this can happen is if JIT font-lock was called (through
3590 handle_fontified_prop), and jit-lock-functions remove text
3591 properties or overlays from the portion of buffer that includes
3592 CHARPOS. Muse mode is known to do that, for example. In this
3593 case, we return -1 to the caller, to signal that no display
3594 string is actually present at CHARPOS. See bidi_fetch_char for
3595 how this is handled.
3596
3597 An alternative would be to never look for display properties past
3598 it->stop_charpos. But neither compute_display_string_pos nor
3599 bidi_fetch_char that calls it know or care where the next
3600 stop_charpos is. */
3601 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3602 return -1;
3603
3604 /* Look forward for the first character where the `display' property
3605 changes. */
3606 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3607
3608 return XFASTINT (pos);
3609 }
3610
3611
3612 \f
3613 /***********************************************************************
3614 Fontification
3615 ***********************************************************************/
3616
3617 /* Handle changes in the `fontified' property of the current buffer by
3618 calling hook functions from Qfontification_functions to fontify
3619 regions of text. */
3620
3621 static enum prop_handled
3622 handle_fontified_prop (struct it *it)
3623 {
3624 Lisp_Object prop, pos;
3625 enum prop_handled handled = HANDLED_NORMALLY;
3626
3627 if (!NILP (Vmemory_full))
3628 return handled;
3629
3630 /* Get the value of the `fontified' property at IT's current buffer
3631 position. (The `fontified' property doesn't have a special
3632 meaning in strings.) If the value is nil, call functions from
3633 Qfontification_functions. */
3634 if (!STRINGP (it->string)
3635 && it->s == NULL
3636 && !NILP (Vfontification_functions)
3637 && !NILP (Vrun_hooks)
3638 && (pos = make_number (IT_CHARPOS (*it)),
3639 prop = Fget_char_property (pos, Qfontified, Qnil),
3640 /* Ignore the special cased nil value always present at EOB since
3641 no amount of fontifying will be able to change it. */
3642 NILP (prop) && IT_CHARPOS (*it) < Z))
3643 {
3644 ptrdiff_t count = SPECPDL_INDEX ();
3645 Lisp_Object val;
3646 struct buffer *obuf = current_buffer;
3647 int begv = BEGV, zv = ZV;
3648 int old_clip_changed = current_buffer->clip_changed;
3649
3650 val = Vfontification_functions;
3651 specbind (Qfontification_functions, Qnil);
3652
3653 eassert (it->end_charpos == ZV);
3654
3655 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3656 safe_call1 (val, pos);
3657 else
3658 {
3659 Lisp_Object fns, fn;
3660 struct gcpro gcpro1, gcpro2;
3661
3662 fns = Qnil;
3663 GCPRO2 (val, fns);
3664
3665 for (; CONSP (val); val = XCDR (val))
3666 {
3667 fn = XCAR (val);
3668
3669 if (EQ (fn, Qt))
3670 {
3671 /* A value of t indicates this hook has a local
3672 binding; it means to run the global binding too.
3673 In a global value, t should not occur. If it
3674 does, we must ignore it to avoid an endless
3675 loop. */
3676 for (fns = Fdefault_value (Qfontification_functions);
3677 CONSP (fns);
3678 fns = XCDR (fns))
3679 {
3680 fn = XCAR (fns);
3681 if (!EQ (fn, Qt))
3682 safe_call1 (fn, pos);
3683 }
3684 }
3685 else
3686 safe_call1 (fn, pos);
3687 }
3688
3689 UNGCPRO;
3690 }
3691
3692 unbind_to (count, Qnil);
3693
3694 /* Fontification functions routinely call `save-restriction'.
3695 Normally, this tags clip_changed, which can confuse redisplay
3696 (see discussion in Bug#6671). Since we don't perform any
3697 special handling of fontification changes in the case where
3698 `save-restriction' isn't called, there's no point doing so in
3699 this case either. So, if the buffer's restrictions are
3700 actually left unchanged, reset clip_changed. */
3701 if (obuf == current_buffer)
3702 {
3703 if (begv == BEGV && zv == ZV)
3704 current_buffer->clip_changed = old_clip_changed;
3705 }
3706 /* There isn't much we can reasonably do to protect against
3707 misbehaving fontification, but here's a fig leaf. */
3708 else if (BUFFER_LIVE_P (obuf))
3709 set_buffer_internal_1 (obuf);
3710
3711 /* The fontification code may have added/removed text.
3712 It could do even a lot worse, but let's at least protect against
3713 the most obvious case where only the text past `pos' gets changed',
3714 as is/was done in grep.el where some escapes sequences are turned
3715 into face properties (bug#7876). */
3716 it->end_charpos = ZV;
3717
3718 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3719 something. This avoids an endless loop if they failed to
3720 fontify the text for which reason ever. */
3721 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3722 handled = HANDLED_RECOMPUTE_PROPS;
3723 }
3724
3725 return handled;
3726 }
3727
3728
3729 \f
3730 /***********************************************************************
3731 Faces
3732 ***********************************************************************/
3733
3734 /* Set up iterator IT from face properties at its current position.
3735 Called from handle_stop. */
3736
3737 static enum prop_handled
3738 handle_face_prop (struct it *it)
3739 {
3740 int new_face_id;
3741 ptrdiff_t next_stop;
3742
3743 if (!STRINGP (it->string))
3744 {
3745 new_face_id
3746 = face_at_buffer_position (it->w,
3747 IT_CHARPOS (*it),
3748 it->region_beg_charpos,
3749 it->region_end_charpos,
3750 &next_stop,
3751 (IT_CHARPOS (*it)
3752 + TEXT_PROP_DISTANCE_LIMIT),
3753 0, it->base_face_id);
3754
3755 /* Is this a start of a run of characters with box face?
3756 Caveat: this can be called for a freshly initialized
3757 iterator; face_id is -1 in this case. We know that the new
3758 face will not change until limit, i.e. if the new face has a
3759 box, all characters up to limit will have one. But, as
3760 usual, we don't know whether limit is really the end. */
3761 if (new_face_id != it->face_id)
3762 {
3763 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3764 /* If it->face_id is -1, old_face below will be NULL, see
3765 the definition of FACE_FROM_ID. This will happen if this
3766 is the initial call that gets the face. */
3767 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3768
3769 /* If the value of face_id of the iterator is -1, we have to
3770 look in front of IT's position and see whether there is a
3771 face there that's different from new_face_id. */
3772 if (!old_face && IT_CHARPOS (*it) > BEG)
3773 {
3774 int prev_face_id = face_before_it_pos (it);
3775
3776 old_face = FACE_FROM_ID (it->f, prev_face_id);
3777 }
3778
3779 /* If the new face has a box, but the old face does not,
3780 this is the start of a run of characters with box face,
3781 i.e. this character has a shadow on the left side. */
3782 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3783 && (old_face == NULL || !old_face->box));
3784 it->face_box_p = new_face->box != FACE_NO_BOX;
3785 }
3786 }
3787 else
3788 {
3789 int base_face_id;
3790 ptrdiff_t bufpos;
3791 int i;
3792 Lisp_Object from_overlay
3793 = (it->current.overlay_string_index >= 0
3794 ? it->string_overlays[it->current.overlay_string_index
3795 % OVERLAY_STRING_CHUNK_SIZE]
3796 : Qnil);
3797
3798 /* See if we got to this string directly or indirectly from
3799 an overlay property. That includes the before-string or
3800 after-string of an overlay, strings in display properties
3801 provided by an overlay, their text properties, etc.
3802
3803 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3804 if (! NILP (from_overlay))
3805 for (i = it->sp - 1; i >= 0; i--)
3806 {
3807 if (it->stack[i].current.overlay_string_index >= 0)
3808 from_overlay
3809 = it->string_overlays[it->stack[i].current.overlay_string_index
3810 % OVERLAY_STRING_CHUNK_SIZE];
3811 else if (! NILP (it->stack[i].from_overlay))
3812 from_overlay = it->stack[i].from_overlay;
3813
3814 if (!NILP (from_overlay))
3815 break;
3816 }
3817
3818 if (! NILP (from_overlay))
3819 {
3820 bufpos = IT_CHARPOS (*it);
3821 /* For a string from an overlay, the base face depends
3822 only on text properties and ignores overlays. */
3823 base_face_id
3824 = face_for_overlay_string (it->w,
3825 IT_CHARPOS (*it),
3826 it->region_beg_charpos,
3827 it->region_end_charpos,
3828 &next_stop,
3829 (IT_CHARPOS (*it)
3830 + TEXT_PROP_DISTANCE_LIMIT),
3831 0,
3832 from_overlay);
3833 }
3834 else
3835 {
3836 bufpos = 0;
3837
3838 /* For strings from a `display' property, use the face at
3839 IT's current buffer position as the base face to merge
3840 with, so that overlay strings appear in the same face as
3841 surrounding text, unless they specify their own
3842 faces. */
3843 base_face_id = it->string_from_prefix_prop_p
3844 ? DEFAULT_FACE_ID
3845 : underlying_face_id (it);
3846 }
3847
3848 new_face_id = face_at_string_position (it->w,
3849 it->string,
3850 IT_STRING_CHARPOS (*it),
3851 bufpos,
3852 it->region_beg_charpos,
3853 it->region_end_charpos,
3854 &next_stop,
3855 base_face_id, 0);
3856
3857 /* Is this a start of a run of characters with box? Caveat:
3858 this can be called for a freshly allocated iterator; face_id
3859 is -1 is this case. We know that the new face will not
3860 change until the next check pos, i.e. if the new face has a
3861 box, all characters up to that position will have a
3862 box. But, as usual, we don't know whether that position
3863 is really the end. */
3864 if (new_face_id != it->face_id)
3865 {
3866 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3867 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3868
3869 /* If new face has a box but old face hasn't, this is the
3870 start of a run of characters with box, i.e. it has a
3871 shadow on the left side. */
3872 it->start_of_box_run_p
3873 = new_face->box && (old_face == NULL || !old_face->box);
3874 it->face_box_p = new_face->box != FACE_NO_BOX;
3875 }
3876 }
3877
3878 it->face_id = new_face_id;
3879 return HANDLED_NORMALLY;
3880 }
3881
3882
3883 /* Return the ID of the face ``underlying'' IT's current position,
3884 which is in a string. If the iterator is associated with a
3885 buffer, return the face at IT's current buffer position.
3886 Otherwise, use the iterator's base_face_id. */
3887
3888 static int
3889 underlying_face_id (struct it *it)
3890 {
3891 int face_id = it->base_face_id, i;
3892
3893 eassert (STRINGP (it->string));
3894
3895 for (i = it->sp - 1; i >= 0; --i)
3896 if (NILP (it->stack[i].string))
3897 face_id = it->stack[i].face_id;
3898
3899 return face_id;
3900 }
3901
3902
3903 /* Compute the face one character before or after the current position
3904 of IT, in the visual order. BEFORE_P non-zero means get the face
3905 in front (to the left in L2R paragraphs, to the right in R2L
3906 paragraphs) of IT's screen position. Value is the ID of the face. */
3907
3908 static int
3909 face_before_or_after_it_pos (struct it *it, int before_p)
3910 {
3911 int face_id, limit;
3912 ptrdiff_t next_check_charpos;
3913 struct it it_copy;
3914 void *it_copy_data = NULL;
3915
3916 eassert (it->s == NULL);
3917
3918 if (STRINGP (it->string))
3919 {
3920 ptrdiff_t bufpos, charpos;
3921 int base_face_id;
3922
3923 /* No face change past the end of the string (for the case
3924 we are padding with spaces). No face change before the
3925 string start. */
3926 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3927 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3928 return it->face_id;
3929
3930 if (!it->bidi_p)
3931 {
3932 /* Set charpos to the position before or after IT's current
3933 position, in the logical order, which in the non-bidi
3934 case is the same as the visual order. */
3935 if (before_p)
3936 charpos = IT_STRING_CHARPOS (*it) - 1;
3937 else if (it->what == IT_COMPOSITION)
3938 /* For composition, we must check the character after the
3939 composition. */
3940 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3941 else
3942 charpos = IT_STRING_CHARPOS (*it) + 1;
3943 }
3944 else
3945 {
3946 if (before_p)
3947 {
3948 /* With bidi iteration, the character before the current
3949 in the visual order cannot be found by simple
3950 iteration, because "reverse" reordering is not
3951 supported. Instead, we need to use the move_it_*
3952 family of functions. */
3953 /* Ignore face changes before the first visible
3954 character on this display line. */
3955 if (it->current_x <= it->first_visible_x)
3956 return it->face_id;
3957 SAVE_IT (it_copy, *it, it_copy_data);
3958 /* Implementation note: Since move_it_in_display_line
3959 works in the iterator geometry, and thinks the first
3960 character is always the leftmost, even in R2L lines,
3961 we don't need to distinguish between the R2L and L2R
3962 cases here. */
3963 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3964 it_copy.current_x - 1, MOVE_TO_X);
3965 charpos = IT_STRING_CHARPOS (it_copy);
3966 RESTORE_IT (it, it, it_copy_data);
3967 }
3968 else
3969 {
3970 /* Set charpos to the string position of the character
3971 that comes after IT's current position in the visual
3972 order. */
3973 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3974
3975 it_copy = *it;
3976 while (n--)
3977 bidi_move_to_visually_next (&it_copy.bidi_it);
3978
3979 charpos = it_copy.bidi_it.charpos;
3980 }
3981 }
3982 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3983
3984 if (it->current.overlay_string_index >= 0)
3985 bufpos = IT_CHARPOS (*it);
3986 else
3987 bufpos = 0;
3988
3989 base_face_id = underlying_face_id (it);
3990
3991 /* Get the face for ASCII, or unibyte. */
3992 face_id = face_at_string_position (it->w,
3993 it->string,
3994 charpos,
3995 bufpos,
3996 it->region_beg_charpos,
3997 it->region_end_charpos,
3998 &next_check_charpos,
3999 base_face_id, 0);
4000
4001 /* Correct the face for charsets different from ASCII. Do it
4002 for the multibyte case only. The face returned above is
4003 suitable for unibyte text if IT->string is unibyte. */
4004 if (STRING_MULTIBYTE (it->string))
4005 {
4006 struct text_pos pos1 = string_pos (charpos, it->string);
4007 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4008 int c, len;
4009 struct face *face = FACE_FROM_ID (it->f, face_id);
4010
4011 c = string_char_and_length (p, &len);
4012 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4013 }
4014 }
4015 else
4016 {
4017 struct text_pos pos;
4018
4019 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4020 || (IT_CHARPOS (*it) <= BEGV && before_p))
4021 return it->face_id;
4022
4023 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4024 pos = it->current.pos;
4025
4026 if (!it->bidi_p)
4027 {
4028 if (before_p)
4029 DEC_TEXT_POS (pos, it->multibyte_p);
4030 else
4031 {
4032 if (it->what == IT_COMPOSITION)
4033 {
4034 /* For composition, we must check the position after
4035 the composition. */
4036 pos.charpos += it->cmp_it.nchars;
4037 pos.bytepos += it->len;
4038 }
4039 else
4040 INC_TEXT_POS (pos, it->multibyte_p);
4041 }
4042 }
4043 else
4044 {
4045 if (before_p)
4046 {
4047 /* With bidi iteration, the character before the current
4048 in the visual order cannot be found by simple
4049 iteration, because "reverse" reordering is not
4050 supported. Instead, we need to use the move_it_*
4051 family of functions. */
4052 /* Ignore face changes before the first visible
4053 character on this display line. */
4054 if (it->current_x <= it->first_visible_x)
4055 return it->face_id;
4056 SAVE_IT (it_copy, *it, it_copy_data);
4057 /* Implementation note: Since move_it_in_display_line
4058 works in the iterator geometry, and thinks the first
4059 character is always the leftmost, even in R2L lines,
4060 we don't need to distinguish between the R2L and L2R
4061 cases here. */
4062 move_it_in_display_line (&it_copy, ZV,
4063 it_copy.current_x - 1, MOVE_TO_X);
4064 pos = it_copy.current.pos;
4065 RESTORE_IT (it, it, it_copy_data);
4066 }
4067 else
4068 {
4069 /* Set charpos to the buffer position of the character
4070 that comes after IT's current position in the visual
4071 order. */
4072 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4073
4074 it_copy = *it;
4075 while (n--)
4076 bidi_move_to_visually_next (&it_copy.bidi_it);
4077
4078 SET_TEXT_POS (pos,
4079 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4080 }
4081 }
4082 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4083
4084 /* Determine face for CHARSET_ASCII, or unibyte. */
4085 face_id = face_at_buffer_position (it->w,
4086 CHARPOS (pos),
4087 it->region_beg_charpos,
4088 it->region_end_charpos,
4089 &next_check_charpos,
4090 limit, 0, -1);
4091
4092 /* Correct the face for charsets different from ASCII. Do it
4093 for the multibyte case only. The face returned above is
4094 suitable for unibyte text if current_buffer is unibyte. */
4095 if (it->multibyte_p)
4096 {
4097 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4098 struct face *face = FACE_FROM_ID (it->f, face_id);
4099 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4100 }
4101 }
4102
4103 return face_id;
4104 }
4105
4106
4107 \f
4108 /***********************************************************************
4109 Invisible text
4110 ***********************************************************************/
4111
4112 /* Set up iterator IT from invisible properties at its current
4113 position. Called from handle_stop. */
4114
4115 static enum prop_handled
4116 handle_invisible_prop (struct it *it)
4117 {
4118 enum prop_handled handled = HANDLED_NORMALLY;
4119 int invis_p;
4120 Lisp_Object prop;
4121
4122 if (STRINGP (it->string))
4123 {
4124 Lisp_Object end_charpos, limit, charpos;
4125
4126 /* Get the value of the invisible text property at the
4127 current position. Value will be nil if there is no such
4128 property. */
4129 charpos = make_number (IT_STRING_CHARPOS (*it));
4130 prop = Fget_text_property (charpos, Qinvisible, it->string);
4131 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4132
4133 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4134 {
4135 /* Record whether we have to display an ellipsis for the
4136 invisible text. */
4137 int display_ellipsis_p = (invis_p == 2);
4138 ptrdiff_t len, endpos;
4139
4140 handled = HANDLED_RECOMPUTE_PROPS;
4141
4142 /* Get the position at which the next visible text can be
4143 found in IT->string, if any. */
4144 endpos = len = SCHARS (it->string);
4145 XSETINT (limit, len);
4146 do
4147 {
4148 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4149 it->string, limit);
4150 if (INTEGERP (end_charpos))
4151 {
4152 endpos = XFASTINT (end_charpos);
4153 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4154 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4155 if (invis_p == 2)
4156 display_ellipsis_p = 1;
4157 }
4158 }
4159 while (invis_p && endpos < len);
4160
4161 if (display_ellipsis_p)
4162 it->ellipsis_p = 1;
4163
4164 if (endpos < len)
4165 {
4166 /* Text at END_CHARPOS is visible. Move IT there. */
4167 struct text_pos old;
4168 ptrdiff_t oldpos;
4169
4170 old = it->current.string_pos;
4171 oldpos = CHARPOS (old);
4172 if (it->bidi_p)
4173 {
4174 if (it->bidi_it.first_elt
4175 && it->bidi_it.charpos < SCHARS (it->string))
4176 bidi_paragraph_init (it->paragraph_embedding,
4177 &it->bidi_it, 1);
4178 /* Bidi-iterate out of the invisible text. */
4179 do
4180 {
4181 bidi_move_to_visually_next (&it->bidi_it);
4182 }
4183 while (oldpos <= it->bidi_it.charpos
4184 && it->bidi_it.charpos < endpos);
4185
4186 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4187 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4188 if (IT_CHARPOS (*it) >= endpos)
4189 it->prev_stop = endpos;
4190 }
4191 else
4192 {
4193 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4194 compute_string_pos (&it->current.string_pos, old, it->string);
4195 }
4196 }
4197 else
4198 {
4199 /* The rest of the string is invisible. If this is an
4200 overlay string, proceed with the next overlay string
4201 or whatever comes and return a character from there. */
4202 if (it->current.overlay_string_index >= 0
4203 && !display_ellipsis_p)
4204 {
4205 next_overlay_string (it);
4206 /* Don't check for overlay strings when we just
4207 finished processing them. */
4208 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4209 }
4210 else
4211 {
4212 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4213 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4214 }
4215 }
4216 }
4217 }
4218 else
4219 {
4220 ptrdiff_t newpos, next_stop, start_charpos, tem;
4221 Lisp_Object pos, overlay;
4222
4223 /* First of all, is there invisible text at this position? */
4224 tem = start_charpos = IT_CHARPOS (*it);
4225 pos = make_number (tem);
4226 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4227 &overlay);
4228 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4229
4230 /* If we are on invisible text, skip over it. */
4231 if (invis_p && start_charpos < it->end_charpos)
4232 {
4233 /* Record whether we have to display an ellipsis for the
4234 invisible text. */
4235 int display_ellipsis_p = invis_p == 2;
4236
4237 handled = HANDLED_RECOMPUTE_PROPS;
4238
4239 /* Loop skipping over invisible text. The loop is left at
4240 ZV or with IT on the first char being visible again. */
4241 do
4242 {
4243 /* Try to skip some invisible text. Return value is the
4244 position reached which can be equal to where we start
4245 if there is nothing invisible there. This skips both
4246 over invisible text properties and overlays with
4247 invisible property. */
4248 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4249
4250 /* If we skipped nothing at all we weren't at invisible
4251 text in the first place. If everything to the end of
4252 the buffer was skipped, end the loop. */
4253 if (newpos == tem || newpos >= ZV)
4254 invis_p = 0;
4255 else
4256 {
4257 /* We skipped some characters but not necessarily
4258 all there are. Check if we ended up on visible
4259 text. Fget_char_property returns the property of
4260 the char before the given position, i.e. if we
4261 get invis_p = 0, this means that the char at
4262 newpos is visible. */
4263 pos = make_number (newpos);
4264 prop = Fget_char_property (pos, Qinvisible, it->window);
4265 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4266 }
4267
4268 /* If we ended up on invisible text, proceed to
4269 skip starting with next_stop. */
4270 if (invis_p)
4271 tem = next_stop;
4272
4273 /* If there are adjacent invisible texts, don't lose the
4274 second one's ellipsis. */
4275 if (invis_p == 2)
4276 display_ellipsis_p = 1;
4277 }
4278 while (invis_p);
4279
4280 /* The position newpos is now either ZV or on visible text. */
4281 if (it->bidi_p)
4282 {
4283 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4284 int on_newline =
4285 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4286 int after_newline =
4287 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4288
4289 /* If the invisible text ends on a newline or on a
4290 character after a newline, we can avoid the costly,
4291 character by character, bidi iteration to NEWPOS, and
4292 instead simply reseat the iterator there. That's
4293 because all bidi reordering information is tossed at
4294 the newline. This is a big win for modes that hide
4295 complete lines, like Outline, Org, etc. */
4296 if (on_newline || after_newline)
4297 {
4298 struct text_pos tpos;
4299 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4300
4301 SET_TEXT_POS (tpos, newpos, bpos);
4302 reseat_1 (it, tpos, 0);
4303 /* If we reseat on a newline/ZV, we need to prep the
4304 bidi iterator for advancing to the next character
4305 after the newline/EOB, keeping the current paragraph
4306 direction (so that PRODUCE_GLYPHS does TRT wrt
4307 prepending/appending glyphs to a glyph row). */
4308 if (on_newline)
4309 {
4310 it->bidi_it.first_elt = 0;
4311 it->bidi_it.paragraph_dir = pdir;
4312 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4313 it->bidi_it.nchars = 1;
4314 it->bidi_it.ch_len = 1;
4315 }
4316 }
4317 else /* Must use the slow method. */
4318 {
4319 /* With bidi iteration, the region of invisible text
4320 could start and/or end in the middle of a
4321 non-base embedding level. Therefore, we need to
4322 skip invisible text using the bidi iterator,
4323 starting at IT's current position, until we find
4324 ourselves outside of the invisible text.
4325 Skipping invisible text _after_ bidi iteration
4326 avoids affecting the visual order of the
4327 displayed text when invisible properties are
4328 added or removed. */
4329 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4330 {
4331 /* If we were `reseat'ed to a new paragraph,
4332 determine the paragraph base direction. We
4333 need to do it now because
4334 next_element_from_buffer may not have a
4335 chance to do it, if we are going to skip any
4336 text at the beginning, which resets the
4337 FIRST_ELT flag. */
4338 bidi_paragraph_init (it->paragraph_embedding,
4339 &it->bidi_it, 1);
4340 }
4341 do
4342 {
4343 bidi_move_to_visually_next (&it->bidi_it);
4344 }
4345 while (it->stop_charpos <= it->bidi_it.charpos
4346 && it->bidi_it.charpos < newpos);
4347 IT_CHARPOS (*it) = it->bidi_it.charpos;
4348 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4349 /* If we overstepped NEWPOS, record its position in
4350 the iterator, so that we skip invisible text if
4351 later the bidi iteration lands us in the
4352 invisible region again. */
4353 if (IT_CHARPOS (*it) >= newpos)
4354 it->prev_stop = newpos;
4355 }
4356 }
4357 else
4358 {
4359 IT_CHARPOS (*it) = newpos;
4360 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4361 }
4362
4363 /* If there are before-strings at the start of invisible
4364 text, and the text is invisible because of a text
4365 property, arrange to show before-strings because 20.x did
4366 it that way. (If the text is invisible because of an
4367 overlay property instead of a text property, this is
4368 already handled in the overlay code.) */
4369 if (NILP (overlay)
4370 && get_overlay_strings (it, it->stop_charpos))
4371 {
4372 handled = HANDLED_RECOMPUTE_PROPS;
4373 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4374 }
4375 else if (display_ellipsis_p)
4376 {
4377 /* Make sure that the glyphs of the ellipsis will get
4378 correct `charpos' values. If we would not update
4379 it->position here, the glyphs would belong to the
4380 last visible character _before_ the invisible
4381 text, which confuses `set_cursor_from_row'.
4382
4383 We use the last invisible position instead of the
4384 first because this way the cursor is always drawn on
4385 the first "." of the ellipsis, whenever PT is inside
4386 the invisible text. Otherwise the cursor would be
4387 placed _after_ the ellipsis when the point is after the
4388 first invisible character. */
4389 if (!STRINGP (it->object))
4390 {
4391 it->position.charpos = newpos - 1;
4392 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4393 }
4394 it->ellipsis_p = 1;
4395 /* Let the ellipsis display before
4396 considering any properties of the following char.
4397 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4398 handled = HANDLED_RETURN;
4399 }
4400 }
4401 }
4402
4403 return handled;
4404 }
4405
4406
4407 /* Make iterator IT return `...' next.
4408 Replaces LEN characters from buffer. */
4409
4410 static void
4411 setup_for_ellipsis (struct it *it, int len)
4412 {
4413 /* Use the display table definition for `...'. Invalid glyphs
4414 will be handled by the method returning elements from dpvec. */
4415 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4416 {
4417 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4418 it->dpvec = v->contents;
4419 it->dpend = v->contents + v->header.size;
4420 }
4421 else
4422 {
4423 /* Default `...'. */
4424 it->dpvec = default_invis_vector;
4425 it->dpend = default_invis_vector + 3;
4426 }
4427
4428 it->dpvec_char_len = len;
4429 it->current.dpvec_index = 0;
4430 it->dpvec_face_id = -1;
4431
4432 /* Remember the current face id in case glyphs specify faces.
4433 IT's face is restored in set_iterator_to_next.
4434 saved_face_id was set to preceding char's face in handle_stop. */
4435 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4436 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4437
4438 it->method = GET_FROM_DISPLAY_VECTOR;
4439 it->ellipsis_p = 1;
4440 }
4441
4442
4443 \f
4444 /***********************************************************************
4445 'display' property
4446 ***********************************************************************/
4447
4448 /* Set up iterator IT from `display' property at its current position.
4449 Called from handle_stop.
4450 We return HANDLED_RETURN if some part of the display property
4451 overrides the display of the buffer text itself.
4452 Otherwise we return HANDLED_NORMALLY. */
4453
4454 static enum prop_handled
4455 handle_display_prop (struct it *it)
4456 {
4457 Lisp_Object propval, object, overlay;
4458 struct text_pos *position;
4459 ptrdiff_t bufpos;
4460 /* Nonzero if some property replaces the display of the text itself. */
4461 int display_replaced_p = 0;
4462
4463 if (STRINGP (it->string))
4464 {
4465 object = it->string;
4466 position = &it->current.string_pos;
4467 bufpos = CHARPOS (it->current.pos);
4468 }
4469 else
4470 {
4471 XSETWINDOW (object, it->w);
4472 position = &it->current.pos;
4473 bufpos = CHARPOS (*position);
4474 }
4475
4476 /* Reset those iterator values set from display property values. */
4477 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4478 it->space_width = Qnil;
4479 it->font_height = Qnil;
4480 it->voffset = 0;
4481
4482 /* We don't support recursive `display' properties, i.e. string
4483 values that have a string `display' property, that have a string
4484 `display' property etc. */
4485 if (!it->string_from_display_prop_p)
4486 it->area = TEXT_AREA;
4487
4488 propval = get_char_property_and_overlay (make_number (position->charpos),
4489 Qdisplay, object, &overlay);
4490 if (NILP (propval))
4491 return HANDLED_NORMALLY;
4492 /* Now OVERLAY is the overlay that gave us this property, or nil
4493 if it was a text property. */
4494
4495 if (!STRINGP (it->string))
4496 object = it->w->contents;
4497
4498 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4499 position, bufpos,
4500 FRAME_WINDOW_P (it->f));
4501
4502 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4503 }
4504
4505 /* Subroutine of handle_display_prop. Returns non-zero if the display
4506 specification in SPEC is a replacing specification, i.e. it would
4507 replace the text covered by `display' property with something else,
4508 such as an image or a display string. If SPEC includes any kind or
4509 `(space ...) specification, the value is 2; this is used by
4510 compute_display_string_pos, which see.
4511
4512 See handle_single_display_spec for documentation of arguments.
4513 frame_window_p is non-zero if the window being redisplayed is on a
4514 GUI frame; this argument is used only if IT is NULL, see below.
4515
4516 IT can be NULL, if this is called by the bidi reordering code
4517 through compute_display_string_pos, which see. In that case, this
4518 function only examines SPEC, but does not otherwise "handle" it, in
4519 the sense that it doesn't set up members of IT from the display
4520 spec. */
4521 static int
4522 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4523 Lisp_Object overlay, struct text_pos *position,
4524 ptrdiff_t bufpos, int frame_window_p)
4525 {
4526 int replacing_p = 0;
4527 int rv;
4528
4529 if (CONSP (spec)
4530 /* Simple specifications. */
4531 && !EQ (XCAR (spec), Qimage)
4532 && !EQ (XCAR (spec), Qspace)
4533 && !EQ (XCAR (spec), Qwhen)
4534 && !EQ (XCAR (spec), Qslice)
4535 && !EQ (XCAR (spec), Qspace_width)
4536 && !EQ (XCAR (spec), Qheight)
4537 && !EQ (XCAR (spec), Qraise)
4538 /* Marginal area specifications. */
4539 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4540 && !EQ (XCAR (spec), Qleft_fringe)
4541 && !EQ (XCAR (spec), Qright_fringe)
4542 && !NILP (XCAR (spec)))
4543 {
4544 for (; CONSP (spec); spec = XCDR (spec))
4545 {
4546 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4547 overlay, position, bufpos,
4548 replacing_p, frame_window_p)))
4549 {
4550 replacing_p = rv;
4551 /* If some text in a string is replaced, `position' no
4552 longer points to the position of `object'. */
4553 if (!it || STRINGP (object))
4554 break;
4555 }
4556 }
4557 }
4558 else if (VECTORP (spec))
4559 {
4560 ptrdiff_t i;
4561 for (i = 0; i < ASIZE (spec); ++i)
4562 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4563 overlay, position, bufpos,
4564 replacing_p, frame_window_p)))
4565 {
4566 replacing_p = rv;
4567 /* If some text in a string is replaced, `position' no
4568 longer points to the position of `object'. */
4569 if (!it || STRINGP (object))
4570 break;
4571 }
4572 }
4573 else
4574 {
4575 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4576 position, bufpos, 0,
4577 frame_window_p)))
4578 replacing_p = rv;
4579 }
4580
4581 return replacing_p;
4582 }
4583
4584 /* Value is the position of the end of the `display' property starting
4585 at START_POS in OBJECT. */
4586
4587 static struct text_pos
4588 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4589 {
4590 Lisp_Object end;
4591 struct text_pos end_pos;
4592
4593 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4594 Qdisplay, object, Qnil);
4595 CHARPOS (end_pos) = XFASTINT (end);
4596 if (STRINGP (object))
4597 compute_string_pos (&end_pos, start_pos, it->string);
4598 else
4599 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4600
4601 return end_pos;
4602 }
4603
4604
4605 /* Set up IT from a single `display' property specification SPEC. OBJECT
4606 is the object in which the `display' property was found. *POSITION
4607 is the position in OBJECT at which the `display' property was found.
4608 BUFPOS is the buffer position of OBJECT (different from POSITION if
4609 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4610 previously saw a display specification which already replaced text
4611 display with something else, for example an image; we ignore such
4612 properties after the first one has been processed.
4613
4614 OVERLAY is the overlay this `display' property came from,
4615 or nil if it was a text property.
4616
4617 If SPEC is a `space' or `image' specification, and in some other
4618 cases too, set *POSITION to the position where the `display'
4619 property ends.
4620
4621 If IT is NULL, only examine the property specification in SPEC, but
4622 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4623 is intended to be displayed in a window on a GUI frame.
4624
4625 Value is non-zero if something was found which replaces the display
4626 of buffer or string text. */
4627
4628 static int
4629 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4630 Lisp_Object overlay, struct text_pos *position,
4631 ptrdiff_t bufpos, int display_replaced_p,
4632 int frame_window_p)
4633 {
4634 Lisp_Object form;
4635 Lisp_Object location, value;
4636 struct text_pos start_pos = *position;
4637 int valid_p;
4638
4639 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4640 If the result is non-nil, use VALUE instead of SPEC. */
4641 form = Qt;
4642 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4643 {
4644 spec = XCDR (spec);
4645 if (!CONSP (spec))
4646 return 0;
4647 form = XCAR (spec);
4648 spec = XCDR (spec);
4649 }
4650
4651 if (!NILP (form) && !EQ (form, Qt))
4652 {
4653 ptrdiff_t count = SPECPDL_INDEX ();
4654 struct gcpro gcpro1;
4655
4656 /* Bind `object' to the object having the `display' property, a
4657 buffer or string. Bind `position' to the position in the
4658 object where the property was found, and `buffer-position'
4659 to the current position in the buffer. */
4660
4661 if (NILP (object))
4662 XSETBUFFER (object, current_buffer);
4663 specbind (Qobject, object);
4664 specbind (Qposition, make_number (CHARPOS (*position)));
4665 specbind (Qbuffer_position, make_number (bufpos));
4666 GCPRO1 (form);
4667 form = safe_eval (form);
4668 UNGCPRO;
4669 unbind_to (count, Qnil);
4670 }
4671
4672 if (NILP (form))
4673 return 0;
4674
4675 /* Handle `(height HEIGHT)' specifications. */
4676 if (CONSP (spec)
4677 && EQ (XCAR (spec), Qheight)
4678 && CONSP (XCDR (spec)))
4679 {
4680 if (it)
4681 {
4682 if (!FRAME_WINDOW_P (it->f))
4683 return 0;
4684
4685 it->font_height = XCAR (XCDR (spec));
4686 if (!NILP (it->font_height))
4687 {
4688 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4689 int new_height = -1;
4690
4691 if (CONSP (it->font_height)
4692 && (EQ (XCAR (it->font_height), Qplus)
4693 || EQ (XCAR (it->font_height), Qminus))
4694 && CONSP (XCDR (it->font_height))
4695 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4696 {
4697 /* `(+ N)' or `(- N)' where N is an integer. */
4698 int steps = XINT (XCAR (XCDR (it->font_height)));
4699 if (EQ (XCAR (it->font_height), Qplus))
4700 steps = - steps;
4701 it->face_id = smaller_face (it->f, it->face_id, steps);
4702 }
4703 else if (FUNCTIONP (it->font_height))
4704 {
4705 /* Call function with current height as argument.
4706 Value is the new height. */
4707 Lisp_Object height;
4708 height = safe_call1 (it->font_height,
4709 face->lface[LFACE_HEIGHT_INDEX]);
4710 if (NUMBERP (height))
4711 new_height = XFLOATINT (height);
4712 }
4713 else if (NUMBERP (it->font_height))
4714 {
4715 /* Value is a multiple of the canonical char height. */
4716 struct face *f;
4717
4718 f = FACE_FROM_ID (it->f,
4719 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4720 new_height = (XFLOATINT (it->font_height)
4721 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4722 }
4723 else
4724 {
4725 /* Evaluate IT->font_height with `height' bound to the
4726 current specified height to get the new height. */
4727 ptrdiff_t count = SPECPDL_INDEX ();
4728
4729 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4730 value = safe_eval (it->font_height);
4731 unbind_to (count, Qnil);
4732
4733 if (NUMBERP (value))
4734 new_height = XFLOATINT (value);
4735 }
4736
4737 if (new_height > 0)
4738 it->face_id = face_with_height (it->f, it->face_id, new_height);
4739 }
4740 }
4741
4742 return 0;
4743 }
4744
4745 /* Handle `(space-width WIDTH)'. */
4746 if (CONSP (spec)
4747 && EQ (XCAR (spec), Qspace_width)
4748 && CONSP (XCDR (spec)))
4749 {
4750 if (it)
4751 {
4752 if (!FRAME_WINDOW_P (it->f))
4753 return 0;
4754
4755 value = XCAR (XCDR (spec));
4756 if (NUMBERP (value) && XFLOATINT (value) > 0)
4757 it->space_width = value;
4758 }
4759
4760 return 0;
4761 }
4762
4763 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4764 if (CONSP (spec)
4765 && EQ (XCAR (spec), Qslice))
4766 {
4767 Lisp_Object tem;
4768
4769 if (it)
4770 {
4771 if (!FRAME_WINDOW_P (it->f))
4772 return 0;
4773
4774 if (tem = XCDR (spec), CONSP (tem))
4775 {
4776 it->slice.x = XCAR (tem);
4777 if (tem = XCDR (tem), CONSP (tem))
4778 {
4779 it->slice.y = XCAR (tem);
4780 if (tem = XCDR (tem), CONSP (tem))
4781 {
4782 it->slice.width = XCAR (tem);
4783 if (tem = XCDR (tem), CONSP (tem))
4784 it->slice.height = XCAR (tem);
4785 }
4786 }
4787 }
4788 }
4789
4790 return 0;
4791 }
4792
4793 /* Handle `(raise FACTOR)'. */
4794 if (CONSP (spec)
4795 && EQ (XCAR (spec), Qraise)
4796 && CONSP (XCDR (spec)))
4797 {
4798 if (it)
4799 {
4800 if (!FRAME_WINDOW_P (it->f))
4801 return 0;
4802
4803 #ifdef HAVE_WINDOW_SYSTEM
4804 value = XCAR (XCDR (spec));
4805 if (NUMBERP (value))
4806 {
4807 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4808 it->voffset = - (XFLOATINT (value)
4809 * (FONT_HEIGHT (face->font)));
4810 }
4811 #endif /* HAVE_WINDOW_SYSTEM */
4812 }
4813
4814 return 0;
4815 }
4816
4817 /* Don't handle the other kinds of display specifications
4818 inside a string that we got from a `display' property. */
4819 if (it && it->string_from_display_prop_p)
4820 return 0;
4821
4822 /* Characters having this form of property are not displayed, so
4823 we have to find the end of the property. */
4824 if (it)
4825 {
4826 start_pos = *position;
4827 *position = display_prop_end (it, object, start_pos);
4828 }
4829 value = Qnil;
4830
4831 /* Stop the scan at that end position--we assume that all
4832 text properties change there. */
4833 if (it)
4834 it->stop_charpos = position->charpos;
4835
4836 /* Handle `(left-fringe BITMAP [FACE])'
4837 and `(right-fringe BITMAP [FACE])'. */
4838 if (CONSP (spec)
4839 && (EQ (XCAR (spec), Qleft_fringe)
4840 || EQ (XCAR (spec), Qright_fringe))
4841 && CONSP (XCDR (spec)))
4842 {
4843 int fringe_bitmap;
4844
4845 if (it)
4846 {
4847 if (!FRAME_WINDOW_P (it->f))
4848 /* If we return here, POSITION has been advanced
4849 across the text with this property. */
4850 {
4851 /* Synchronize the bidi iterator with POSITION. This is
4852 needed because we are not going to push the iterator
4853 on behalf of this display property, so there will be
4854 no pop_it call to do this synchronization for us. */
4855 if (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 else if (!frame_window_p)
4865 return 1;
4866
4867 #ifdef HAVE_WINDOW_SYSTEM
4868 value = XCAR (XCDR (spec));
4869 if (!SYMBOLP (value)
4870 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4871 /* If we return here, POSITION has been advanced
4872 across the text with this property. */
4873 {
4874 if (it && it->bidi_p)
4875 {
4876 it->position = *position;
4877 iterate_out_of_display_property (it);
4878 *position = it->position;
4879 }
4880 return 1;
4881 }
4882
4883 if (it)
4884 {
4885 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4886
4887 if (CONSP (XCDR (XCDR (spec))))
4888 {
4889 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4890 int face_id2 = lookup_derived_face (it->f, face_name,
4891 FRINGE_FACE_ID, 0);
4892 if (face_id2 >= 0)
4893 face_id = face_id2;
4894 }
4895
4896 /* Save current settings of IT so that we can restore them
4897 when we are finished with the glyph property value. */
4898 push_it (it, position);
4899
4900 it->area = TEXT_AREA;
4901 it->what = IT_IMAGE;
4902 it->image_id = -1; /* no image */
4903 it->position = start_pos;
4904 it->object = NILP (object) ? it->w->contents : object;
4905 it->method = GET_FROM_IMAGE;
4906 it->from_overlay = Qnil;
4907 it->face_id = face_id;
4908 it->from_disp_prop_p = 1;
4909
4910 /* Say that we haven't consumed the characters with
4911 `display' property yet. The call to pop_it in
4912 set_iterator_to_next will clean this up. */
4913 *position = start_pos;
4914
4915 if (EQ (XCAR (spec), Qleft_fringe))
4916 {
4917 it->left_user_fringe_bitmap = fringe_bitmap;
4918 it->left_user_fringe_face_id = face_id;
4919 }
4920 else
4921 {
4922 it->right_user_fringe_bitmap = fringe_bitmap;
4923 it->right_user_fringe_face_id = face_id;
4924 }
4925 }
4926 #endif /* HAVE_WINDOW_SYSTEM */
4927 return 1;
4928 }
4929
4930 /* Prepare to handle `((margin left-margin) ...)',
4931 `((margin right-margin) ...)' and `((margin nil) ...)'
4932 prefixes for display specifications. */
4933 location = Qunbound;
4934 if (CONSP (spec) && CONSP (XCAR (spec)))
4935 {
4936 Lisp_Object tem;
4937
4938 value = XCDR (spec);
4939 if (CONSP (value))
4940 value = XCAR (value);
4941
4942 tem = XCAR (spec);
4943 if (EQ (XCAR (tem), Qmargin)
4944 && (tem = XCDR (tem),
4945 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4946 (NILP (tem)
4947 || EQ (tem, Qleft_margin)
4948 || EQ (tem, Qright_margin))))
4949 location = tem;
4950 }
4951
4952 if (EQ (location, Qunbound))
4953 {
4954 location = Qnil;
4955 value = spec;
4956 }
4957
4958 /* After this point, VALUE is the property after any
4959 margin prefix has been stripped. It must be a string,
4960 an image specification, or `(space ...)'.
4961
4962 LOCATION specifies where to display: `left-margin',
4963 `right-margin' or nil. */
4964
4965 valid_p = (STRINGP (value)
4966 #ifdef HAVE_WINDOW_SYSTEM
4967 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4968 && valid_image_p (value))
4969 #endif /* not HAVE_WINDOW_SYSTEM */
4970 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4971
4972 if (valid_p && !display_replaced_p)
4973 {
4974 int retval = 1;
4975
4976 if (!it)
4977 {
4978 /* Callers need to know whether the display spec is any kind
4979 of `(space ...)' spec that is about to affect text-area
4980 display. */
4981 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4982 retval = 2;
4983 return retval;
4984 }
4985
4986 /* Save current settings of IT so that we can restore them
4987 when we are finished with the glyph property value. */
4988 push_it (it, position);
4989 it->from_overlay = overlay;
4990 it->from_disp_prop_p = 1;
4991
4992 if (NILP (location))
4993 it->area = TEXT_AREA;
4994 else if (EQ (location, Qleft_margin))
4995 it->area = LEFT_MARGIN_AREA;
4996 else
4997 it->area = RIGHT_MARGIN_AREA;
4998
4999 if (STRINGP (value))
5000 {
5001 it->string = value;
5002 it->multibyte_p = STRING_MULTIBYTE (it->string);
5003 it->current.overlay_string_index = -1;
5004 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5005 it->end_charpos = it->string_nchars = SCHARS (it->string);
5006 it->method = GET_FROM_STRING;
5007 it->stop_charpos = 0;
5008 it->prev_stop = 0;
5009 it->base_level_stop = 0;
5010 it->string_from_display_prop_p = 1;
5011 /* Say that we haven't consumed the characters with
5012 `display' property yet. The call to pop_it in
5013 set_iterator_to_next will clean this up. */
5014 if (BUFFERP (object))
5015 *position = start_pos;
5016
5017 /* Force paragraph direction to be that of the parent
5018 object. If the parent object's paragraph direction is
5019 not yet determined, default to L2R. */
5020 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5021 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5022 else
5023 it->paragraph_embedding = L2R;
5024
5025 /* Set up the bidi iterator for this display string. */
5026 if (it->bidi_p)
5027 {
5028 it->bidi_it.string.lstring = it->string;
5029 it->bidi_it.string.s = NULL;
5030 it->bidi_it.string.schars = it->end_charpos;
5031 it->bidi_it.string.bufpos = bufpos;
5032 it->bidi_it.string.from_disp_str = 1;
5033 it->bidi_it.string.unibyte = !it->multibyte_p;
5034 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5035 }
5036 }
5037 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5038 {
5039 it->method = GET_FROM_STRETCH;
5040 it->object = value;
5041 *position = it->position = start_pos;
5042 retval = 1 + (it->area == TEXT_AREA);
5043 }
5044 #ifdef HAVE_WINDOW_SYSTEM
5045 else
5046 {
5047 it->what = IT_IMAGE;
5048 it->image_id = lookup_image (it->f, value);
5049 it->position = start_pos;
5050 it->object = NILP (object) ? it->w->contents : object;
5051 it->method = GET_FROM_IMAGE;
5052
5053 /* Say that we haven't consumed the characters with
5054 `display' property yet. The call to pop_it in
5055 set_iterator_to_next will clean this up. */
5056 *position = start_pos;
5057 }
5058 #endif /* HAVE_WINDOW_SYSTEM */
5059
5060 return retval;
5061 }
5062
5063 /* Invalid property or property not supported. Restore
5064 POSITION to what it was before. */
5065 *position = start_pos;
5066 return 0;
5067 }
5068
5069 /* Check if PROP is a display property value whose text should be
5070 treated as intangible. OVERLAY is the overlay from which PROP
5071 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5072 specify the buffer position covered by PROP. */
5073
5074 int
5075 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5076 ptrdiff_t charpos, ptrdiff_t bytepos)
5077 {
5078 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5079 struct text_pos position;
5080
5081 SET_TEXT_POS (position, charpos, bytepos);
5082 return handle_display_spec (NULL, prop, Qnil, overlay,
5083 &position, charpos, frame_window_p);
5084 }
5085
5086
5087 /* Return 1 if PROP is a display sub-property value containing STRING.
5088
5089 Implementation note: this and the following function are really
5090 special cases of handle_display_spec and
5091 handle_single_display_spec, and should ideally use the same code.
5092 Until they do, these two pairs must be consistent and must be
5093 modified in sync. */
5094
5095 static int
5096 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5097 {
5098 if (EQ (string, prop))
5099 return 1;
5100
5101 /* Skip over `when FORM'. */
5102 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5103 {
5104 prop = XCDR (prop);
5105 if (!CONSP (prop))
5106 return 0;
5107 /* Actually, the condition following `when' should be eval'ed,
5108 like handle_single_display_spec does, and we should return
5109 zero if it evaluates to nil. However, this function is
5110 called only when the buffer was already displayed and some
5111 glyph in the glyph matrix was found to come from a display
5112 string. Therefore, the condition was already evaluated, and
5113 the result was non-nil, otherwise the display string wouldn't
5114 have been displayed and we would have never been called for
5115 this property. Thus, we can skip the evaluation and assume
5116 its result is non-nil. */
5117 prop = XCDR (prop);
5118 }
5119
5120 if (CONSP (prop))
5121 /* Skip over `margin LOCATION'. */
5122 if (EQ (XCAR (prop), Qmargin))
5123 {
5124 prop = XCDR (prop);
5125 if (!CONSP (prop))
5126 return 0;
5127
5128 prop = XCDR (prop);
5129 if (!CONSP (prop))
5130 return 0;
5131 }
5132
5133 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5134 }
5135
5136
5137 /* Return 1 if STRING appears in the `display' property PROP. */
5138
5139 static int
5140 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5141 {
5142 if (CONSP (prop)
5143 && !EQ (XCAR (prop), Qwhen)
5144 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5145 {
5146 /* A list of sub-properties. */
5147 while (CONSP (prop))
5148 {
5149 if (single_display_spec_string_p (XCAR (prop), string))
5150 return 1;
5151 prop = XCDR (prop);
5152 }
5153 }
5154 else if (VECTORP (prop))
5155 {
5156 /* A vector of sub-properties. */
5157 ptrdiff_t i;
5158 for (i = 0; i < ASIZE (prop); ++i)
5159 if (single_display_spec_string_p (AREF (prop, i), string))
5160 return 1;
5161 }
5162 else
5163 return single_display_spec_string_p (prop, string);
5164
5165 return 0;
5166 }
5167
5168 /* Look for STRING in overlays and text properties in the current
5169 buffer, between character positions FROM and TO (excluding TO).
5170 BACK_P non-zero means look back (in this case, TO is supposed to be
5171 less than FROM).
5172 Value is the first character position where STRING was found, or
5173 zero if it wasn't found before hitting TO.
5174
5175 This function may only use code that doesn't eval because it is
5176 called asynchronously from note_mouse_highlight. */
5177
5178 static ptrdiff_t
5179 string_buffer_position_lim (Lisp_Object string,
5180 ptrdiff_t from, ptrdiff_t to, int back_p)
5181 {
5182 Lisp_Object limit, prop, pos;
5183 int found = 0;
5184
5185 pos = make_number (max (from, BEGV));
5186
5187 if (!back_p) /* looking forward */
5188 {
5189 limit = make_number (min (to, ZV));
5190 while (!found && !EQ (pos, limit))
5191 {
5192 prop = Fget_char_property (pos, Qdisplay, Qnil);
5193 if (!NILP (prop) && display_prop_string_p (prop, string))
5194 found = 1;
5195 else
5196 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5197 limit);
5198 }
5199 }
5200 else /* looking back */
5201 {
5202 limit = make_number (max (to, BEGV));
5203 while (!found && !EQ (pos, limit))
5204 {
5205 prop = Fget_char_property (pos, Qdisplay, Qnil);
5206 if (!NILP (prop) && display_prop_string_p (prop, string))
5207 found = 1;
5208 else
5209 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5210 limit);
5211 }
5212 }
5213
5214 return found ? XINT (pos) : 0;
5215 }
5216
5217 /* Determine which buffer position in current buffer STRING comes from.
5218 AROUND_CHARPOS is an approximate position where it could come from.
5219 Value is the buffer position or 0 if it couldn't be determined.
5220
5221 This function is necessary because we don't record buffer positions
5222 in glyphs generated from strings (to keep struct glyph small).
5223 This function may only use code that doesn't eval because it is
5224 called asynchronously from note_mouse_highlight. */
5225
5226 static ptrdiff_t
5227 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5228 {
5229 const int MAX_DISTANCE = 1000;
5230 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5231 around_charpos + MAX_DISTANCE,
5232 0);
5233
5234 if (!found)
5235 found = string_buffer_position_lim (string, around_charpos,
5236 around_charpos - MAX_DISTANCE, 1);
5237 return found;
5238 }
5239
5240
5241 \f
5242 /***********************************************************************
5243 `composition' property
5244 ***********************************************************************/
5245
5246 /* Set up iterator IT from `composition' property at its current
5247 position. Called from handle_stop. */
5248
5249 static enum prop_handled
5250 handle_composition_prop (struct it *it)
5251 {
5252 Lisp_Object prop, string;
5253 ptrdiff_t pos, pos_byte, start, end;
5254
5255 if (STRINGP (it->string))
5256 {
5257 unsigned char *s;
5258
5259 pos = IT_STRING_CHARPOS (*it);
5260 pos_byte = IT_STRING_BYTEPOS (*it);
5261 string = it->string;
5262 s = SDATA (string) + pos_byte;
5263 it->c = STRING_CHAR (s);
5264 }
5265 else
5266 {
5267 pos = IT_CHARPOS (*it);
5268 pos_byte = IT_BYTEPOS (*it);
5269 string = Qnil;
5270 it->c = FETCH_CHAR (pos_byte);
5271 }
5272
5273 /* If there's a valid composition and point is not inside of the
5274 composition (in the case that the composition is from the current
5275 buffer), draw a glyph composed from the composition components. */
5276 if (find_composition (pos, -1, &start, &end, &prop, string)
5277 && COMPOSITION_VALID_P (start, end, prop)
5278 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5279 {
5280 if (start < pos)
5281 /* As we can't handle this situation (perhaps font-lock added
5282 a new composition), we just return here hoping that next
5283 redisplay will detect this composition much earlier. */
5284 return HANDLED_NORMALLY;
5285 if (start != pos)
5286 {
5287 if (STRINGP (it->string))
5288 pos_byte = string_char_to_byte (it->string, start);
5289 else
5290 pos_byte = CHAR_TO_BYTE (start);
5291 }
5292 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5293 prop, string);
5294
5295 if (it->cmp_it.id >= 0)
5296 {
5297 it->cmp_it.ch = -1;
5298 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5299 it->cmp_it.nglyphs = -1;
5300 }
5301 }
5302
5303 return HANDLED_NORMALLY;
5304 }
5305
5306
5307 \f
5308 /***********************************************************************
5309 Overlay strings
5310 ***********************************************************************/
5311
5312 /* The following structure is used to record overlay strings for
5313 later sorting in load_overlay_strings. */
5314
5315 struct overlay_entry
5316 {
5317 Lisp_Object overlay;
5318 Lisp_Object string;
5319 EMACS_INT priority;
5320 int after_string_p;
5321 };
5322
5323
5324 /* Set up iterator IT from overlay strings at its current position.
5325 Called from handle_stop. */
5326
5327 static enum prop_handled
5328 handle_overlay_change (struct it *it)
5329 {
5330 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5331 return HANDLED_RECOMPUTE_PROPS;
5332 else
5333 return HANDLED_NORMALLY;
5334 }
5335
5336
5337 /* Set up the next overlay string for delivery by IT, if there is an
5338 overlay string to deliver. Called by set_iterator_to_next when the
5339 end of the current overlay string is reached. If there are more
5340 overlay strings to display, IT->string and
5341 IT->current.overlay_string_index are set appropriately here.
5342 Otherwise IT->string is set to nil. */
5343
5344 static void
5345 next_overlay_string (struct it *it)
5346 {
5347 ++it->current.overlay_string_index;
5348 if (it->current.overlay_string_index == it->n_overlay_strings)
5349 {
5350 /* No more overlay strings. Restore IT's settings to what
5351 they were before overlay strings were processed, and
5352 continue to deliver from current_buffer. */
5353
5354 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5355 pop_it (it);
5356 eassert (it->sp > 0
5357 || (NILP (it->string)
5358 && it->method == GET_FROM_BUFFER
5359 && it->stop_charpos >= BEGV
5360 && it->stop_charpos <= it->end_charpos));
5361 it->current.overlay_string_index = -1;
5362 it->n_overlay_strings = 0;
5363 it->overlay_strings_charpos = -1;
5364 /* If there's an empty display string on the stack, pop the
5365 stack, to resync the bidi iterator with IT's position. Such
5366 empty strings are pushed onto the stack in
5367 get_overlay_strings_1. */
5368 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5369 pop_it (it);
5370
5371 /* If we're at the end of the buffer, record that we have
5372 processed the overlay strings there already, so that
5373 next_element_from_buffer doesn't try it again. */
5374 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5375 it->overlay_strings_at_end_processed_p = 1;
5376 }
5377 else
5378 {
5379 /* There are more overlay strings to process. If
5380 IT->current.overlay_string_index has advanced to a position
5381 where we must load IT->overlay_strings with more strings, do
5382 it. We must load at the IT->overlay_strings_charpos where
5383 IT->n_overlay_strings was originally computed; when invisible
5384 text is present, this might not be IT_CHARPOS (Bug#7016). */
5385 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5386
5387 if (it->current.overlay_string_index && i == 0)
5388 load_overlay_strings (it, it->overlay_strings_charpos);
5389
5390 /* Initialize IT to deliver display elements from the overlay
5391 string. */
5392 it->string = it->overlay_strings[i];
5393 it->multibyte_p = STRING_MULTIBYTE (it->string);
5394 SET_TEXT_POS (it->current.string_pos, 0, 0);
5395 it->method = GET_FROM_STRING;
5396 it->stop_charpos = 0;
5397 it->end_charpos = SCHARS (it->string);
5398 if (it->cmp_it.stop_pos >= 0)
5399 it->cmp_it.stop_pos = 0;
5400 it->prev_stop = 0;
5401 it->base_level_stop = 0;
5402
5403 /* Set up the bidi iterator for this overlay string. */
5404 if (it->bidi_p)
5405 {
5406 it->bidi_it.string.lstring = it->string;
5407 it->bidi_it.string.s = NULL;
5408 it->bidi_it.string.schars = SCHARS (it->string);
5409 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5410 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5411 it->bidi_it.string.unibyte = !it->multibyte_p;
5412 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5413 }
5414 }
5415
5416 CHECK_IT (it);
5417 }
5418
5419
5420 /* Compare two overlay_entry structures E1 and E2. Used as a
5421 comparison function for qsort in load_overlay_strings. Overlay
5422 strings for the same position are sorted so that
5423
5424 1. All after-strings come in front of before-strings, except
5425 when they come from the same overlay.
5426
5427 2. Within after-strings, strings are sorted so that overlay strings
5428 from overlays with higher priorities come first.
5429
5430 2. Within before-strings, strings are sorted so that overlay
5431 strings from overlays with higher priorities come last.
5432
5433 Value is analogous to strcmp. */
5434
5435
5436 static int
5437 compare_overlay_entries (const void *e1, const void *e2)
5438 {
5439 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5440 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5441 int result;
5442
5443 if (entry1->after_string_p != entry2->after_string_p)
5444 {
5445 /* Let after-strings appear in front of before-strings if
5446 they come from different overlays. */
5447 if (EQ (entry1->overlay, entry2->overlay))
5448 result = entry1->after_string_p ? 1 : -1;
5449 else
5450 result = entry1->after_string_p ? -1 : 1;
5451 }
5452 else if (entry1->priority != entry2->priority)
5453 {
5454 if (entry1->after_string_p)
5455 /* After-strings sorted in order of decreasing priority. */
5456 result = entry2->priority < entry1->priority ? -1 : 1;
5457 else
5458 /* Before-strings sorted in order of increasing priority. */
5459 result = entry1->priority < entry2->priority ? -1 : 1;
5460 }
5461 else
5462 result = 0;
5463
5464 return result;
5465 }
5466
5467
5468 /* Load the vector IT->overlay_strings with overlay strings from IT's
5469 current buffer position, or from CHARPOS if that is > 0. Set
5470 IT->n_overlays to the total number of overlay strings found.
5471
5472 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5473 a time. On entry into load_overlay_strings,
5474 IT->current.overlay_string_index gives the number of overlay
5475 strings that have already been loaded by previous calls to this
5476 function.
5477
5478 IT->add_overlay_start contains an additional overlay start
5479 position to consider for taking overlay strings from, if non-zero.
5480 This position comes into play when the overlay has an `invisible'
5481 property, and both before and after-strings. When we've skipped to
5482 the end of the overlay, because of its `invisible' property, we
5483 nevertheless want its before-string to appear.
5484 IT->add_overlay_start will contain the overlay start position
5485 in this case.
5486
5487 Overlay strings are sorted so that after-string strings come in
5488 front of before-string strings. Within before and after-strings,
5489 strings are sorted by overlay priority. See also function
5490 compare_overlay_entries. */
5491
5492 static void
5493 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5494 {
5495 Lisp_Object overlay, window, str, invisible;
5496 struct Lisp_Overlay *ov;
5497 ptrdiff_t start, end;
5498 ptrdiff_t size = 20;
5499 ptrdiff_t n = 0, i, j;
5500 int invis_p;
5501 struct overlay_entry *entries = alloca (size * sizeof *entries);
5502 USE_SAFE_ALLOCA;
5503
5504 if (charpos <= 0)
5505 charpos = IT_CHARPOS (*it);
5506
5507 /* Append the overlay string STRING of overlay OVERLAY to vector
5508 `entries' which has size `size' and currently contains `n'
5509 elements. AFTER_P non-zero means STRING is an after-string of
5510 OVERLAY. */
5511 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5512 do \
5513 { \
5514 Lisp_Object priority; \
5515 \
5516 if (n == size) \
5517 { \
5518 struct overlay_entry *old = entries; \
5519 SAFE_NALLOCA (entries, 2, size); \
5520 memcpy (entries, old, size * sizeof *entries); \
5521 size *= 2; \
5522 } \
5523 \
5524 entries[n].string = (STRING); \
5525 entries[n].overlay = (OVERLAY); \
5526 priority = Foverlay_get ((OVERLAY), Qpriority); \
5527 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5528 entries[n].after_string_p = (AFTER_P); \
5529 ++n; \
5530 } \
5531 while (0)
5532
5533 /* Process overlay before the overlay center. */
5534 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5535 {
5536 XSETMISC (overlay, ov);
5537 eassert (OVERLAYP (overlay));
5538 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5539 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5540
5541 if (end < charpos)
5542 break;
5543
5544 /* Skip this overlay if it doesn't start or end at IT's current
5545 position. */
5546 if (end != charpos && start != charpos)
5547 continue;
5548
5549 /* Skip this overlay if it doesn't apply to IT->w. */
5550 window = Foverlay_get (overlay, Qwindow);
5551 if (WINDOWP (window) && XWINDOW (window) != it->w)
5552 continue;
5553
5554 /* If the text ``under'' the overlay is invisible, both before-
5555 and after-strings from this overlay are visible; start and
5556 end position are indistinguishable. */
5557 invisible = Foverlay_get (overlay, Qinvisible);
5558 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5559
5560 /* If overlay has a non-empty before-string, record it. */
5561 if ((start == charpos || (end == charpos && invis_p))
5562 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5563 && SCHARS (str))
5564 RECORD_OVERLAY_STRING (overlay, str, 0);
5565
5566 /* If overlay has a non-empty after-string, record it. */
5567 if ((end == charpos || (start == charpos && invis_p))
5568 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5569 && SCHARS (str))
5570 RECORD_OVERLAY_STRING (overlay, str, 1);
5571 }
5572
5573 /* Process overlays after the overlay center. */
5574 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5575 {
5576 XSETMISC (overlay, ov);
5577 eassert (OVERLAYP (overlay));
5578 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5579 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5580
5581 if (start > charpos)
5582 break;
5583
5584 /* Skip this overlay if it doesn't start or end at IT's current
5585 position. */
5586 if (end != charpos && start != charpos)
5587 continue;
5588
5589 /* Skip this overlay if it doesn't apply to IT->w. */
5590 window = Foverlay_get (overlay, Qwindow);
5591 if (WINDOWP (window) && XWINDOW (window) != it->w)
5592 continue;
5593
5594 /* If the text ``under'' the overlay is invisible, it has a zero
5595 dimension, and both before- and after-strings apply. */
5596 invisible = Foverlay_get (overlay, Qinvisible);
5597 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5598
5599 /* If overlay has a non-empty before-string, record it. */
5600 if ((start == charpos || (end == charpos && invis_p))
5601 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5602 && SCHARS (str))
5603 RECORD_OVERLAY_STRING (overlay, str, 0);
5604
5605 /* If overlay has a non-empty after-string, record it. */
5606 if ((end == charpos || (start == charpos && invis_p))
5607 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5608 && SCHARS (str))
5609 RECORD_OVERLAY_STRING (overlay, str, 1);
5610 }
5611
5612 #undef RECORD_OVERLAY_STRING
5613
5614 /* Sort entries. */
5615 if (n > 1)
5616 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5617
5618 /* Record number of overlay strings, and where we computed it. */
5619 it->n_overlay_strings = n;
5620 it->overlay_strings_charpos = charpos;
5621
5622 /* IT->current.overlay_string_index is the number of overlay strings
5623 that have already been consumed by IT. Copy some of the
5624 remaining overlay strings to IT->overlay_strings. */
5625 i = 0;
5626 j = it->current.overlay_string_index;
5627 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5628 {
5629 it->overlay_strings[i] = entries[j].string;
5630 it->string_overlays[i++] = entries[j++].overlay;
5631 }
5632
5633 CHECK_IT (it);
5634 SAFE_FREE ();
5635 }
5636
5637
5638 /* Get the first chunk of overlay strings at IT's current buffer
5639 position, or at CHARPOS if that is > 0. Value is non-zero if at
5640 least one overlay string was found. */
5641
5642 static int
5643 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5644 {
5645 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5646 process. This fills IT->overlay_strings with strings, and sets
5647 IT->n_overlay_strings to the total number of strings to process.
5648 IT->pos.overlay_string_index has to be set temporarily to zero
5649 because load_overlay_strings needs this; it must be set to -1
5650 when no overlay strings are found because a zero value would
5651 indicate a position in the first overlay string. */
5652 it->current.overlay_string_index = 0;
5653 load_overlay_strings (it, charpos);
5654
5655 /* If we found overlay strings, set up IT to deliver display
5656 elements from the first one. Otherwise set up IT to deliver
5657 from current_buffer. */
5658 if (it->n_overlay_strings)
5659 {
5660 /* Make sure we know settings in current_buffer, so that we can
5661 restore meaningful values when we're done with the overlay
5662 strings. */
5663 if (compute_stop_p)
5664 compute_stop_pos (it);
5665 eassert (it->face_id >= 0);
5666
5667 /* Save IT's settings. They are restored after all overlay
5668 strings have been processed. */
5669 eassert (!compute_stop_p || it->sp == 0);
5670
5671 /* When called from handle_stop, there might be an empty display
5672 string loaded. In that case, don't bother saving it. But
5673 don't use this optimization with the bidi iterator, since we
5674 need the corresponding pop_it call to resync the bidi
5675 iterator's position with IT's position, after we are done
5676 with the overlay strings. (The corresponding call to pop_it
5677 in case of an empty display string is in
5678 next_overlay_string.) */
5679 if (!(!it->bidi_p
5680 && STRINGP (it->string) && !SCHARS (it->string)))
5681 push_it (it, NULL);
5682
5683 /* Set up IT to deliver display elements from the first overlay
5684 string. */
5685 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5686 it->string = it->overlay_strings[0];
5687 it->from_overlay = Qnil;
5688 it->stop_charpos = 0;
5689 eassert (STRINGP (it->string));
5690 it->end_charpos = SCHARS (it->string);
5691 it->prev_stop = 0;
5692 it->base_level_stop = 0;
5693 it->multibyte_p = STRING_MULTIBYTE (it->string);
5694 it->method = GET_FROM_STRING;
5695 it->from_disp_prop_p = 0;
5696
5697 /* Force paragraph direction to be that of the parent
5698 buffer. */
5699 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5700 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5701 else
5702 it->paragraph_embedding = L2R;
5703
5704 /* Set up the bidi iterator for this overlay string. */
5705 if (it->bidi_p)
5706 {
5707 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5708
5709 it->bidi_it.string.lstring = it->string;
5710 it->bidi_it.string.s = NULL;
5711 it->bidi_it.string.schars = SCHARS (it->string);
5712 it->bidi_it.string.bufpos = pos;
5713 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5714 it->bidi_it.string.unibyte = !it->multibyte_p;
5715 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5716 }
5717 return 1;
5718 }
5719
5720 it->current.overlay_string_index = -1;
5721 return 0;
5722 }
5723
5724 static int
5725 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5726 {
5727 it->string = Qnil;
5728 it->method = GET_FROM_BUFFER;
5729
5730 (void) get_overlay_strings_1 (it, charpos, 1);
5731
5732 CHECK_IT (it);
5733
5734 /* Value is non-zero if we found at least one overlay string. */
5735 return STRINGP (it->string);
5736 }
5737
5738
5739 \f
5740 /***********************************************************************
5741 Saving and restoring state
5742 ***********************************************************************/
5743
5744 /* Save current settings of IT on IT->stack. Called, for example,
5745 before setting up IT for an overlay string, to be able to restore
5746 IT's settings to what they were after the overlay string has been
5747 processed. If POSITION is non-NULL, it is the position to save on
5748 the stack instead of IT->position. */
5749
5750 static void
5751 push_it (struct it *it, struct text_pos *position)
5752 {
5753 struct iterator_stack_entry *p;
5754
5755 eassert (it->sp < IT_STACK_SIZE);
5756 p = it->stack + it->sp;
5757
5758 p->stop_charpos = it->stop_charpos;
5759 p->prev_stop = it->prev_stop;
5760 p->base_level_stop = it->base_level_stop;
5761 p->cmp_it = it->cmp_it;
5762 eassert (it->face_id >= 0);
5763 p->face_id = it->face_id;
5764 p->string = it->string;
5765 p->method = it->method;
5766 p->from_overlay = it->from_overlay;
5767 switch (p->method)
5768 {
5769 case GET_FROM_IMAGE:
5770 p->u.image.object = it->object;
5771 p->u.image.image_id = it->image_id;
5772 p->u.image.slice = it->slice;
5773 break;
5774 case GET_FROM_STRETCH:
5775 p->u.stretch.object = it->object;
5776 break;
5777 }
5778 p->position = position ? *position : it->position;
5779 p->current = it->current;
5780 p->end_charpos = it->end_charpos;
5781 p->string_nchars = it->string_nchars;
5782 p->area = it->area;
5783 p->multibyte_p = it->multibyte_p;
5784 p->avoid_cursor_p = it->avoid_cursor_p;
5785 p->space_width = it->space_width;
5786 p->font_height = it->font_height;
5787 p->voffset = it->voffset;
5788 p->string_from_display_prop_p = it->string_from_display_prop_p;
5789 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5790 p->display_ellipsis_p = 0;
5791 p->line_wrap = it->line_wrap;
5792 p->bidi_p = it->bidi_p;
5793 p->paragraph_embedding = it->paragraph_embedding;
5794 p->from_disp_prop_p = it->from_disp_prop_p;
5795 ++it->sp;
5796
5797 /* Save the state of the bidi iterator as well. */
5798 if (it->bidi_p)
5799 bidi_push_it (&it->bidi_it);
5800 }
5801
5802 static void
5803 iterate_out_of_display_property (struct it *it)
5804 {
5805 int buffer_p = !STRINGP (it->string);
5806 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5807 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5808
5809 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5810
5811 /* Maybe initialize paragraph direction. If we are at the beginning
5812 of a new paragraph, next_element_from_buffer may not have a
5813 chance to do that. */
5814 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5815 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5816 /* prev_stop can be zero, so check against BEGV as well. */
5817 while (it->bidi_it.charpos >= bob
5818 && it->prev_stop <= it->bidi_it.charpos
5819 && it->bidi_it.charpos < CHARPOS (it->position)
5820 && it->bidi_it.charpos < eob)
5821 bidi_move_to_visually_next (&it->bidi_it);
5822 /* Record the stop_pos we just crossed, for when we cross it
5823 back, maybe. */
5824 if (it->bidi_it.charpos > CHARPOS (it->position))
5825 it->prev_stop = CHARPOS (it->position);
5826 /* If we ended up not where pop_it put us, resync IT's
5827 positional members with the bidi iterator. */
5828 if (it->bidi_it.charpos != CHARPOS (it->position))
5829 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5830 if (buffer_p)
5831 it->current.pos = it->position;
5832 else
5833 it->current.string_pos = it->position;
5834 }
5835
5836 /* Restore IT's settings from IT->stack. Called, for example, when no
5837 more overlay strings must be processed, and we return to delivering
5838 display elements from a buffer, or when the end of a string from a
5839 `display' property is reached and we return to delivering display
5840 elements from an overlay string, or from a buffer. */
5841
5842 static void
5843 pop_it (struct it *it)
5844 {
5845 struct iterator_stack_entry *p;
5846 int from_display_prop = it->from_disp_prop_p;
5847
5848 eassert (it->sp > 0);
5849 --it->sp;
5850 p = it->stack + it->sp;
5851 it->stop_charpos = p->stop_charpos;
5852 it->prev_stop = p->prev_stop;
5853 it->base_level_stop = p->base_level_stop;
5854 it->cmp_it = p->cmp_it;
5855 it->face_id = p->face_id;
5856 it->current = p->current;
5857 it->position = p->position;
5858 it->string = p->string;
5859 it->from_overlay = p->from_overlay;
5860 if (NILP (it->string))
5861 SET_TEXT_POS (it->current.string_pos, -1, -1);
5862 it->method = p->method;
5863 switch (it->method)
5864 {
5865 case GET_FROM_IMAGE:
5866 it->image_id = p->u.image.image_id;
5867 it->object = p->u.image.object;
5868 it->slice = p->u.image.slice;
5869 break;
5870 case GET_FROM_STRETCH:
5871 it->object = p->u.stretch.object;
5872 break;
5873 case GET_FROM_BUFFER:
5874 it->object = it->w->contents;
5875 break;
5876 case GET_FROM_STRING:
5877 it->object = it->string;
5878 break;
5879 case GET_FROM_DISPLAY_VECTOR:
5880 if (it->s)
5881 it->method = GET_FROM_C_STRING;
5882 else if (STRINGP (it->string))
5883 it->method = GET_FROM_STRING;
5884 else
5885 {
5886 it->method = GET_FROM_BUFFER;
5887 it->object = it->w->contents;
5888 }
5889 }
5890 it->end_charpos = p->end_charpos;
5891 it->string_nchars = p->string_nchars;
5892 it->area = p->area;
5893 it->multibyte_p = p->multibyte_p;
5894 it->avoid_cursor_p = p->avoid_cursor_p;
5895 it->space_width = p->space_width;
5896 it->font_height = p->font_height;
5897 it->voffset = p->voffset;
5898 it->string_from_display_prop_p = p->string_from_display_prop_p;
5899 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5900 it->line_wrap = p->line_wrap;
5901 it->bidi_p = p->bidi_p;
5902 it->paragraph_embedding = p->paragraph_embedding;
5903 it->from_disp_prop_p = p->from_disp_prop_p;
5904 if (it->bidi_p)
5905 {
5906 bidi_pop_it (&it->bidi_it);
5907 /* Bidi-iterate until we get out of the portion of text, if any,
5908 covered by a `display' text property or by an overlay with
5909 `display' property. (We cannot just jump there, because the
5910 internal coherency of the bidi iterator state can not be
5911 preserved across such jumps.) We also must determine the
5912 paragraph base direction if the overlay we just processed is
5913 at the beginning of a new paragraph. */
5914 if (from_display_prop
5915 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5916 iterate_out_of_display_property (it);
5917
5918 eassert ((BUFFERP (it->object)
5919 && IT_CHARPOS (*it) == it->bidi_it.charpos
5920 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5921 || (STRINGP (it->object)
5922 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5923 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5924 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5925 }
5926 }
5927
5928
5929 \f
5930 /***********************************************************************
5931 Moving over lines
5932 ***********************************************************************/
5933
5934 /* Set IT's current position to the previous line start. */
5935
5936 static void
5937 back_to_previous_line_start (struct it *it)
5938 {
5939 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
5940
5941 DEC_BOTH (cp, bp);
5942 IT_CHARPOS (*it) = find_newline_no_quit (cp, bp, -1, &IT_BYTEPOS (*it));
5943 }
5944
5945
5946 /* Move IT to the next line start.
5947
5948 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5949 we skipped over part of the text (as opposed to moving the iterator
5950 continuously over the text). Otherwise, don't change the value
5951 of *SKIPPED_P.
5952
5953 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5954 iterator on the newline, if it was found.
5955
5956 Newlines may come from buffer text, overlay strings, or strings
5957 displayed via the `display' property. That's the reason we can't
5958 simply use find_newline_no_quit.
5959
5960 Note that this function may not skip over invisible text that is so
5961 because of text properties and immediately follows a newline. If
5962 it would, function reseat_at_next_visible_line_start, when called
5963 from set_iterator_to_next, would effectively make invisible
5964 characters following a newline part of the wrong glyph row, which
5965 leads to wrong cursor motion. */
5966
5967 static int
5968 forward_to_next_line_start (struct it *it, int *skipped_p,
5969 struct bidi_it *bidi_it_prev)
5970 {
5971 ptrdiff_t old_selective;
5972 int newline_found_p, n;
5973 const int MAX_NEWLINE_DISTANCE = 500;
5974
5975 /* If already on a newline, just consume it to avoid unintended
5976 skipping over invisible text below. */
5977 if (it->what == IT_CHARACTER
5978 && it->c == '\n'
5979 && CHARPOS (it->position) == IT_CHARPOS (*it))
5980 {
5981 if (it->bidi_p && bidi_it_prev)
5982 *bidi_it_prev = it->bidi_it;
5983 set_iterator_to_next (it, 0);
5984 it->c = 0;
5985 return 1;
5986 }
5987
5988 /* Don't handle selective display in the following. It's (a)
5989 unnecessary because it's done by the caller, and (b) leads to an
5990 infinite recursion because next_element_from_ellipsis indirectly
5991 calls this function. */
5992 old_selective = it->selective;
5993 it->selective = 0;
5994
5995 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5996 from buffer text. */
5997 for (n = newline_found_p = 0;
5998 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
5999 n += STRINGP (it->string) ? 0 : 1)
6000 {
6001 if (!get_next_display_element (it))
6002 return 0;
6003 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6004 if (newline_found_p && it->bidi_p && bidi_it_prev)
6005 *bidi_it_prev = it->bidi_it;
6006 set_iterator_to_next (it, 0);
6007 }
6008
6009 /* If we didn't find a newline near enough, see if we can use a
6010 short-cut. */
6011 if (!newline_found_p)
6012 {
6013 ptrdiff_t bytepos, start = IT_CHARPOS (*it);
6014 ptrdiff_t limit = find_newline_no_quit (start, IT_BYTEPOS (*it),
6015 1, &bytepos);
6016 Lisp_Object pos;
6017
6018 eassert (!STRINGP (it->string));
6019
6020 /* If there isn't any `display' property in sight, and no
6021 overlays, we can just use the position of the newline in
6022 buffer text. */
6023 if (it->stop_charpos >= limit
6024 || ((pos = Fnext_single_property_change (make_number (start),
6025 Qdisplay, Qnil,
6026 make_number (limit)),
6027 NILP (pos))
6028 && next_overlay_change (start) == ZV))
6029 {
6030 if (!it->bidi_p)
6031 {
6032 IT_CHARPOS (*it) = limit;
6033 IT_BYTEPOS (*it) = bytepos;
6034 }
6035 else
6036 {
6037 struct bidi_it bprev;
6038
6039 /* Help bidi.c avoid expensive searches for display
6040 properties and overlays, by telling it that there are
6041 none up to `limit'. */
6042 if (it->bidi_it.disp_pos < limit)
6043 {
6044 it->bidi_it.disp_pos = limit;
6045 it->bidi_it.disp_prop = 0;
6046 }
6047 do {
6048 bprev = it->bidi_it;
6049 bidi_move_to_visually_next (&it->bidi_it);
6050 } while (it->bidi_it.charpos != limit);
6051 IT_CHARPOS (*it) = limit;
6052 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6053 if (bidi_it_prev)
6054 *bidi_it_prev = bprev;
6055 }
6056 *skipped_p = newline_found_p = 1;
6057 }
6058 else
6059 {
6060 while (get_next_display_element (it)
6061 && !newline_found_p)
6062 {
6063 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6064 if (newline_found_p && it->bidi_p && bidi_it_prev)
6065 *bidi_it_prev = it->bidi_it;
6066 set_iterator_to_next (it, 0);
6067 }
6068 }
6069 }
6070
6071 it->selective = old_selective;
6072 return newline_found_p;
6073 }
6074
6075
6076 /* Set IT's current position to the previous visible line start. Skip
6077 invisible text that is so either due to text properties or due to
6078 selective display. Caution: this does not change IT->current_x and
6079 IT->hpos. */
6080
6081 static void
6082 back_to_previous_visible_line_start (struct it *it)
6083 {
6084 while (IT_CHARPOS (*it) > BEGV)
6085 {
6086 back_to_previous_line_start (it);
6087
6088 if (IT_CHARPOS (*it) <= BEGV)
6089 break;
6090
6091 /* If selective > 0, then lines indented more than its value are
6092 invisible. */
6093 if (it->selective > 0
6094 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6095 it->selective))
6096 continue;
6097
6098 /* Check the newline before point for invisibility. */
6099 {
6100 Lisp_Object prop;
6101 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6102 Qinvisible, it->window);
6103 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6104 continue;
6105 }
6106
6107 if (IT_CHARPOS (*it) <= BEGV)
6108 break;
6109
6110 {
6111 struct it it2;
6112 void *it2data = NULL;
6113 ptrdiff_t pos;
6114 ptrdiff_t beg, end;
6115 Lisp_Object val, overlay;
6116
6117 SAVE_IT (it2, *it, it2data);
6118
6119 /* If newline is part of a composition, continue from start of composition */
6120 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6121 && beg < IT_CHARPOS (*it))
6122 goto replaced;
6123
6124 /* If newline is replaced by a display property, find start of overlay
6125 or interval and continue search from that point. */
6126 pos = --IT_CHARPOS (it2);
6127 --IT_BYTEPOS (it2);
6128 it2.sp = 0;
6129 bidi_unshelve_cache (NULL, 0);
6130 it2.string_from_display_prop_p = 0;
6131 it2.from_disp_prop_p = 0;
6132 if (handle_display_prop (&it2) == HANDLED_RETURN
6133 && !NILP (val = get_char_property_and_overlay
6134 (make_number (pos), Qdisplay, Qnil, &overlay))
6135 && (OVERLAYP (overlay)
6136 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6137 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6138 {
6139 RESTORE_IT (it, it, it2data);
6140 goto replaced;
6141 }
6142
6143 /* Newline is not replaced by anything -- so we are done. */
6144 RESTORE_IT (it, it, it2data);
6145 break;
6146
6147 replaced:
6148 if (beg < BEGV)
6149 beg = BEGV;
6150 IT_CHARPOS (*it) = beg;
6151 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6152 }
6153 }
6154
6155 it->continuation_lines_width = 0;
6156
6157 eassert (IT_CHARPOS (*it) >= BEGV);
6158 eassert (IT_CHARPOS (*it) == BEGV
6159 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6160 CHECK_IT (it);
6161 }
6162
6163
6164 /* Reseat iterator IT at the previous visible line start. Skip
6165 invisible text that is so either due to text properties or due to
6166 selective display. At the end, update IT's overlay information,
6167 face information etc. */
6168
6169 void
6170 reseat_at_previous_visible_line_start (struct it *it)
6171 {
6172 back_to_previous_visible_line_start (it);
6173 reseat (it, it->current.pos, 1);
6174 CHECK_IT (it);
6175 }
6176
6177
6178 /* Reseat iterator IT on the next visible line start in the current
6179 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6180 preceding the line start. Skip over invisible text that is so
6181 because of selective display. Compute faces, overlays etc at the
6182 new position. Note that this function does not skip over text that
6183 is invisible because of text properties. */
6184
6185 static void
6186 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6187 {
6188 int newline_found_p, skipped_p = 0;
6189 struct bidi_it bidi_it_prev;
6190
6191 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6192
6193 /* Skip over lines that are invisible because they are indented
6194 more than the value of IT->selective. */
6195 if (it->selective > 0)
6196 while (IT_CHARPOS (*it) < ZV
6197 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6198 it->selective))
6199 {
6200 eassert (IT_BYTEPOS (*it) == BEGV
6201 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6202 newline_found_p =
6203 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6204 }
6205
6206 /* Position on the newline if that's what's requested. */
6207 if (on_newline_p && newline_found_p)
6208 {
6209 if (STRINGP (it->string))
6210 {
6211 if (IT_STRING_CHARPOS (*it) > 0)
6212 {
6213 if (!it->bidi_p)
6214 {
6215 --IT_STRING_CHARPOS (*it);
6216 --IT_STRING_BYTEPOS (*it);
6217 }
6218 else
6219 {
6220 /* We need to restore the bidi iterator to the state
6221 it had on the newline, and resync the IT's
6222 position with that. */
6223 it->bidi_it = bidi_it_prev;
6224 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6225 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6226 }
6227 }
6228 }
6229 else if (IT_CHARPOS (*it) > BEGV)
6230 {
6231 if (!it->bidi_p)
6232 {
6233 --IT_CHARPOS (*it);
6234 --IT_BYTEPOS (*it);
6235 }
6236 else
6237 {
6238 /* We need to restore the bidi iterator to the state it
6239 had on the newline and resync IT with that. */
6240 it->bidi_it = bidi_it_prev;
6241 IT_CHARPOS (*it) = it->bidi_it.charpos;
6242 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6243 }
6244 reseat (it, it->current.pos, 0);
6245 }
6246 }
6247 else if (skipped_p)
6248 reseat (it, it->current.pos, 0);
6249
6250 CHECK_IT (it);
6251 }
6252
6253
6254 \f
6255 /***********************************************************************
6256 Changing an iterator's position
6257 ***********************************************************************/
6258
6259 /* Change IT's current position to POS in current_buffer. If FORCE_P
6260 is non-zero, always check for text properties at the new position.
6261 Otherwise, text properties are only looked up if POS >=
6262 IT->check_charpos of a property. */
6263
6264 static void
6265 reseat (struct it *it, struct text_pos pos, int force_p)
6266 {
6267 ptrdiff_t original_pos = IT_CHARPOS (*it);
6268
6269 reseat_1 (it, pos, 0);
6270
6271 /* Determine where to check text properties. Avoid doing it
6272 where possible because text property lookup is very expensive. */
6273 if (force_p
6274 || CHARPOS (pos) > it->stop_charpos
6275 || CHARPOS (pos) < original_pos)
6276 {
6277 if (it->bidi_p)
6278 {
6279 /* For bidi iteration, we need to prime prev_stop and
6280 base_level_stop with our best estimations. */
6281 /* Implementation note: Of course, POS is not necessarily a
6282 stop position, so assigning prev_pos to it is a lie; we
6283 should have called compute_stop_backwards. However, if
6284 the current buffer does not include any R2L characters,
6285 that call would be a waste of cycles, because the
6286 iterator will never move back, and thus never cross this
6287 "fake" stop position. So we delay that backward search
6288 until the time we really need it, in next_element_from_buffer. */
6289 if (CHARPOS (pos) != it->prev_stop)
6290 it->prev_stop = CHARPOS (pos);
6291 if (CHARPOS (pos) < it->base_level_stop)
6292 it->base_level_stop = 0; /* meaning it's unknown */
6293 handle_stop (it);
6294 }
6295 else
6296 {
6297 handle_stop (it);
6298 it->prev_stop = it->base_level_stop = 0;
6299 }
6300
6301 }
6302
6303 CHECK_IT (it);
6304 }
6305
6306
6307 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6308 IT->stop_pos to POS, also. */
6309
6310 static void
6311 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6312 {
6313 /* Don't call this function when scanning a C string. */
6314 eassert (it->s == NULL);
6315
6316 /* POS must be a reasonable value. */
6317 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6318
6319 it->current.pos = it->position = pos;
6320 it->end_charpos = ZV;
6321 it->dpvec = NULL;
6322 it->current.dpvec_index = -1;
6323 it->current.overlay_string_index = -1;
6324 IT_STRING_CHARPOS (*it) = -1;
6325 IT_STRING_BYTEPOS (*it) = -1;
6326 it->string = Qnil;
6327 it->method = GET_FROM_BUFFER;
6328 it->object = it->w->contents;
6329 it->area = TEXT_AREA;
6330 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6331 it->sp = 0;
6332 it->string_from_display_prop_p = 0;
6333 it->string_from_prefix_prop_p = 0;
6334
6335 it->from_disp_prop_p = 0;
6336 it->face_before_selective_p = 0;
6337 if (it->bidi_p)
6338 {
6339 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6340 &it->bidi_it);
6341 bidi_unshelve_cache (NULL, 0);
6342 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6343 it->bidi_it.string.s = NULL;
6344 it->bidi_it.string.lstring = Qnil;
6345 it->bidi_it.string.bufpos = 0;
6346 it->bidi_it.string.unibyte = 0;
6347 }
6348
6349 if (set_stop_p)
6350 {
6351 it->stop_charpos = CHARPOS (pos);
6352 it->base_level_stop = CHARPOS (pos);
6353 }
6354 /* This make the information stored in it->cmp_it invalidate. */
6355 it->cmp_it.id = -1;
6356 }
6357
6358
6359 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6360 If S is non-null, it is a C string to iterate over. Otherwise,
6361 STRING gives a Lisp string to iterate over.
6362
6363 If PRECISION > 0, don't return more then PRECISION number of
6364 characters from the string.
6365
6366 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6367 characters have been returned. FIELD_WIDTH < 0 means an infinite
6368 field width.
6369
6370 MULTIBYTE = 0 means disable processing of multibyte characters,
6371 MULTIBYTE > 0 means enable it,
6372 MULTIBYTE < 0 means use IT->multibyte_p.
6373
6374 IT must be initialized via a prior call to init_iterator before
6375 calling this function. */
6376
6377 static void
6378 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6379 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6380 int multibyte)
6381 {
6382 /* No region in strings. */
6383 it->region_beg_charpos = it->region_end_charpos = -1;
6384
6385 /* No text property checks performed by default, but see below. */
6386 it->stop_charpos = -1;
6387
6388 /* Set iterator position and end position. */
6389 memset (&it->current, 0, sizeof it->current);
6390 it->current.overlay_string_index = -1;
6391 it->current.dpvec_index = -1;
6392 eassert (charpos >= 0);
6393
6394 /* If STRING is specified, use its multibyteness, otherwise use the
6395 setting of MULTIBYTE, if specified. */
6396 if (multibyte >= 0)
6397 it->multibyte_p = multibyte > 0;
6398
6399 /* Bidirectional reordering of strings is controlled by the default
6400 value of bidi-display-reordering. Don't try to reorder while
6401 loading loadup.el, as the necessary character property tables are
6402 not yet available. */
6403 it->bidi_p =
6404 NILP (Vpurify_flag)
6405 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6406
6407 if (s == NULL)
6408 {
6409 eassert (STRINGP (string));
6410 it->string = string;
6411 it->s = NULL;
6412 it->end_charpos = it->string_nchars = SCHARS (string);
6413 it->method = GET_FROM_STRING;
6414 it->current.string_pos = string_pos (charpos, string);
6415
6416 if (it->bidi_p)
6417 {
6418 it->bidi_it.string.lstring = string;
6419 it->bidi_it.string.s = NULL;
6420 it->bidi_it.string.schars = it->end_charpos;
6421 it->bidi_it.string.bufpos = 0;
6422 it->bidi_it.string.from_disp_str = 0;
6423 it->bidi_it.string.unibyte = !it->multibyte_p;
6424 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6425 FRAME_WINDOW_P (it->f), &it->bidi_it);
6426 }
6427 }
6428 else
6429 {
6430 it->s = (const unsigned char *) s;
6431 it->string = Qnil;
6432
6433 /* Note that we use IT->current.pos, not it->current.string_pos,
6434 for displaying C strings. */
6435 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6436 if (it->multibyte_p)
6437 {
6438 it->current.pos = c_string_pos (charpos, s, 1);
6439 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6440 }
6441 else
6442 {
6443 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6444 it->end_charpos = it->string_nchars = strlen (s);
6445 }
6446
6447 if (it->bidi_p)
6448 {
6449 it->bidi_it.string.lstring = Qnil;
6450 it->bidi_it.string.s = (const unsigned char *) s;
6451 it->bidi_it.string.schars = it->end_charpos;
6452 it->bidi_it.string.bufpos = 0;
6453 it->bidi_it.string.from_disp_str = 0;
6454 it->bidi_it.string.unibyte = !it->multibyte_p;
6455 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6456 &it->bidi_it);
6457 }
6458 it->method = GET_FROM_C_STRING;
6459 }
6460
6461 /* PRECISION > 0 means don't return more than PRECISION characters
6462 from the string. */
6463 if (precision > 0 && it->end_charpos - charpos > precision)
6464 {
6465 it->end_charpos = it->string_nchars = charpos + precision;
6466 if (it->bidi_p)
6467 it->bidi_it.string.schars = it->end_charpos;
6468 }
6469
6470 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6471 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6472 FIELD_WIDTH < 0 means infinite field width. This is useful for
6473 padding with `-' at the end of a mode line. */
6474 if (field_width < 0)
6475 field_width = INFINITY;
6476 /* Implementation note: We deliberately don't enlarge
6477 it->bidi_it.string.schars here to fit it->end_charpos, because
6478 the bidi iterator cannot produce characters out of thin air. */
6479 if (field_width > it->end_charpos - charpos)
6480 it->end_charpos = charpos + field_width;
6481
6482 /* Use the standard display table for displaying strings. */
6483 if (DISP_TABLE_P (Vstandard_display_table))
6484 it->dp = XCHAR_TABLE (Vstandard_display_table);
6485
6486 it->stop_charpos = charpos;
6487 it->prev_stop = charpos;
6488 it->base_level_stop = 0;
6489 if (it->bidi_p)
6490 {
6491 it->bidi_it.first_elt = 1;
6492 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6493 it->bidi_it.disp_pos = -1;
6494 }
6495 if (s == NULL && it->multibyte_p)
6496 {
6497 ptrdiff_t endpos = SCHARS (it->string);
6498 if (endpos > it->end_charpos)
6499 endpos = it->end_charpos;
6500 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6501 it->string);
6502 }
6503 CHECK_IT (it);
6504 }
6505
6506
6507 \f
6508 /***********************************************************************
6509 Iteration
6510 ***********************************************************************/
6511
6512 /* Map enum it_method value to corresponding next_element_from_* function. */
6513
6514 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6515 {
6516 next_element_from_buffer,
6517 next_element_from_display_vector,
6518 next_element_from_string,
6519 next_element_from_c_string,
6520 next_element_from_image,
6521 next_element_from_stretch
6522 };
6523
6524 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6525
6526
6527 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6528 (possibly with the following characters). */
6529
6530 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6531 ((IT)->cmp_it.id >= 0 \
6532 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6533 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6534 END_CHARPOS, (IT)->w, \
6535 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6536 (IT)->string)))
6537
6538
6539 /* Lookup the char-table Vglyphless_char_display for character C (-1
6540 if we want information for no-font case), and return the display
6541 method symbol. By side-effect, update it->what and
6542 it->glyphless_method. This function is called from
6543 get_next_display_element for each character element, and from
6544 x_produce_glyphs when no suitable font was found. */
6545
6546 Lisp_Object
6547 lookup_glyphless_char_display (int c, struct it *it)
6548 {
6549 Lisp_Object glyphless_method = Qnil;
6550
6551 if (CHAR_TABLE_P (Vglyphless_char_display)
6552 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6553 {
6554 if (c >= 0)
6555 {
6556 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6557 if (CONSP (glyphless_method))
6558 glyphless_method = FRAME_WINDOW_P (it->f)
6559 ? XCAR (glyphless_method)
6560 : XCDR (glyphless_method);
6561 }
6562 else
6563 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6564 }
6565
6566 retry:
6567 if (NILP (glyphless_method))
6568 {
6569 if (c >= 0)
6570 /* The default is to display the character by a proper font. */
6571 return Qnil;
6572 /* The default for the no-font case is to display an empty box. */
6573 glyphless_method = Qempty_box;
6574 }
6575 if (EQ (glyphless_method, Qzero_width))
6576 {
6577 if (c >= 0)
6578 return glyphless_method;
6579 /* This method can't be used for the no-font case. */
6580 glyphless_method = Qempty_box;
6581 }
6582 if (EQ (glyphless_method, Qthin_space))
6583 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6584 else if (EQ (glyphless_method, Qempty_box))
6585 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6586 else if (EQ (glyphless_method, Qhex_code))
6587 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6588 else if (STRINGP (glyphless_method))
6589 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6590 else
6591 {
6592 /* Invalid value. We use the default method. */
6593 glyphless_method = Qnil;
6594 goto retry;
6595 }
6596 it->what = IT_GLYPHLESS;
6597 return glyphless_method;
6598 }
6599
6600 /* Load IT's display element fields with information about the next
6601 display element from the current position of IT. Value is zero if
6602 end of buffer (or C string) is reached. */
6603
6604 static struct frame *last_escape_glyph_frame = NULL;
6605 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6606 static int last_escape_glyph_merged_face_id = 0;
6607
6608 struct frame *last_glyphless_glyph_frame = NULL;
6609 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6610 int last_glyphless_glyph_merged_face_id = 0;
6611
6612 static int
6613 get_next_display_element (struct it *it)
6614 {
6615 /* Non-zero means that we found a display element. Zero means that
6616 we hit the end of what we iterate over. Performance note: the
6617 function pointer `method' used here turns out to be faster than
6618 using a sequence of if-statements. */
6619 int success_p;
6620
6621 get_next:
6622 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6623
6624 if (it->what == IT_CHARACTER)
6625 {
6626 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6627 and only if (a) the resolved directionality of that character
6628 is R..." */
6629 /* FIXME: Do we need an exception for characters from display
6630 tables? */
6631 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6632 it->c = bidi_mirror_char (it->c);
6633 /* Map via display table or translate control characters.
6634 IT->c, IT->len etc. have been set to the next character by
6635 the function call above. If we have a display table, and it
6636 contains an entry for IT->c, translate it. Don't do this if
6637 IT->c itself comes from a display table, otherwise we could
6638 end up in an infinite recursion. (An alternative could be to
6639 count the recursion depth of this function and signal an
6640 error when a certain maximum depth is reached.) Is it worth
6641 it? */
6642 if (success_p && it->dpvec == NULL)
6643 {
6644 Lisp_Object dv;
6645 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6646 int nonascii_space_p = 0;
6647 int nonascii_hyphen_p = 0;
6648 int c = it->c; /* This is the character to display. */
6649
6650 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6651 {
6652 eassert (SINGLE_BYTE_CHAR_P (c));
6653 if (unibyte_display_via_language_environment)
6654 {
6655 c = DECODE_CHAR (unibyte, c);
6656 if (c < 0)
6657 c = BYTE8_TO_CHAR (it->c);
6658 }
6659 else
6660 c = BYTE8_TO_CHAR (it->c);
6661 }
6662
6663 if (it->dp
6664 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6665 VECTORP (dv)))
6666 {
6667 struct Lisp_Vector *v = XVECTOR (dv);
6668
6669 /* Return the first character from the display table
6670 entry, if not empty. If empty, don't display the
6671 current character. */
6672 if (v->header.size)
6673 {
6674 it->dpvec_char_len = it->len;
6675 it->dpvec = v->contents;
6676 it->dpend = v->contents + v->header.size;
6677 it->current.dpvec_index = 0;
6678 it->dpvec_face_id = -1;
6679 it->saved_face_id = it->face_id;
6680 it->method = GET_FROM_DISPLAY_VECTOR;
6681 it->ellipsis_p = 0;
6682 }
6683 else
6684 {
6685 set_iterator_to_next (it, 0);
6686 }
6687 goto get_next;
6688 }
6689
6690 if (! NILP (lookup_glyphless_char_display (c, it)))
6691 {
6692 if (it->what == IT_GLYPHLESS)
6693 goto done;
6694 /* Don't display this character. */
6695 set_iterator_to_next (it, 0);
6696 goto get_next;
6697 }
6698
6699 /* If `nobreak-char-display' is non-nil, we display
6700 non-ASCII spaces and hyphens specially. */
6701 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6702 {
6703 if (c == 0xA0)
6704 nonascii_space_p = 1;
6705 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6706 nonascii_hyphen_p = 1;
6707 }
6708
6709 /* Translate control characters into `\003' or `^C' form.
6710 Control characters coming from a display table entry are
6711 currently not translated because we use IT->dpvec to hold
6712 the translation. This could easily be changed but I
6713 don't believe that it is worth doing.
6714
6715 The characters handled by `nobreak-char-display' must be
6716 translated too.
6717
6718 Non-printable characters and raw-byte characters are also
6719 translated to octal form. */
6720 if (((c < ' ' || c == 127) /* ASCII control chars */
6721 ? (it->area != TEXT_AREA
6722 /* In mode line, treat \n, \t like other crl chars. */
6723 || (c != '\t'
6724 && it->glyph_row
6725 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6726 || (c != '\n' && c != '\t'))
6727 : (nonascii_space_p
6728 || nonascii_hyphen_p
6729 || CHAR_BYTE8_P (c)
6730 || ! CHAR_PRINTABLE_P (c))))
6731 {
6732 /* C is a control character, non-ASCII space/hyphen,
6733 raw-byte, or a non-printable character which must be
6734 displayed either as '\003' or as `^C' where the '\\'
6735 and '^' can be defined in the display table. Fill
6736 IT->ctl_chars with glyphs for what we have to
6737 display. Then, set IT->dpvec to these glyphs. */
6738 Lisp_Object gc;
6739 int ctl_len;
6740 int face_id;
6741 int lface_id = 0;
6742 int escape_glyph;
6743
6744 /* Handle control characters with ^. */
6745
6746 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6747 {
6748 int g;
6749
6750 g = '^'; /* default glyph for Control */
6751 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6752 if (it->dp
6753 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6754 {
6755 g = GLYPH_CODE_CHAR (gc);
6756 lface_id = GLYPH_CODE_FACE (gc);
6757 }
6758 if (lface_id)
6759 {
6760 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6761 }
6762 else if (it->f == last_escape_glyph_frame
6763 && it->face_id == last_escape_glyph_face_id)
6764 {
6765 face_id = last_escape_glyph_merged_face_id;
6766 }
6767 else
6768 {
6769 /* Merge the escape-glyph face into the current face. */
6770 face_id = merge_faces (it->f, Qescape_glyph, 0,
6771 it->face_id);
6772 last_escape_glyph_frame = it->f;
6773 last_escape_glyph_face_id = it->face_id;
6774 last_escape_glyph_merged_face_id = face_id;
6775 }
6776
6777 XSETINT (it->ctl_chars[0], g);
6778 XSETINT (it->ctl_chars[1], c ^ 0100);
6779 ctl_len = 2;
6780 goto display_control;
6781 }
6782
6783 /* Handle non-ascii space in the mode where it only gets
6784 highlighting. */
6785
6786 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6787 {
6788 /* Merge `nobreak-space' into the current face. */
6789 face_id = merge_faces (it->f, Qnobreak_space, 0,
6790 it->face_id);
6791 XSETINT (it->ctl_chars[0], ' ');
6792 ctl_len = 1;
6793 goto display_control;
6794 }
6795
6796 /* Handle sequences that start with the "escape glyph". */
6797
6798 /* the default escape glyph is \. */
6799 escape_glyph = '\\';
6800
6801 if (it->dp
6802 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6803 {
6804 escape_glyph = GLYPH_CODE_CHAR (gc);
6805 lface_id = GLYPH_CODE_FACE (gc);
6806 }
6807 if (lface_id)
6808 {
6809 /* The display table specified a face.
6810 Merge it into face_id and also into escape_glyph. */
6811 face_id = merge_faces (it->f, Qt, lface_id,
6812 it->face_id);
6813 }
6814 else if (it->f == last_escape_glyph_frame
6815 && it->face_id == last_escape_glyph_face_id)
6816 {
6817 face_id = last_escape_glyph_merged_face_id;
6818 }
6819 else
6820 {
6821 /* Merge the escape-glyph face into the current face. */
6822 face_id = merge_faces (it->f, Qescape_glyph, 0,
6823 it->face_id);
6824 last_escape_glyph_frame = it->f;
6825 last_escape_glyph_face_id = it->face_id;
6826 last_escape_glyph_merged_face_id = face_id;
6827 }
6828
6829 /* Draw non-ASCII hyphen with just highlighting: */
6830
6831 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6832 {
6833 XSETINT (it->ctl_chars[0], '-');
6834 ctl_len = 1;
6835 goto display_control;
6836 }
6837
6838 /* Draw non-ASCII space/hyphen with escape glyph: */
6839
6840 if (nonascii_space_p || nonascii_hyphen_p)
6841 {
6842 XSETINT (it->ctl_chars[0], escape_glyph);
6843 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6844 ctl_len = 2;
6845 goto display_control;
6846 }
6847
6848 {
6849 char str[10];
6850 int len, i;
6851
6852 if (CHAR_BYTE8_P (c))
6853 /* Display \200 instead of \17777600. */
6854 c = CHAR_TO_BYTE8 (c);
6855 len = sprintf (str, "%03o", c);
6856
6857 XSETINT (it->ctl_chars[0], escape_glyph);
6858 for (i = 0; i < len; i++)
6859 XSETINT (it->ctl_chars[i + 1], str[i]);
6860 ctl_len = len + 1;
6861 }
6862
6863 display_control:
6864 /* Set up IT->dpvec and return first character from it. */
6865 it->dpvec_char_len = it->len;
6866 it->dpvec = it->ctl_chars;
6867 it->dpend = it->dpvec + ctl_len;
6868 it->current.dpvec_index = 0;
6869 it->dpvec_face_id = face_id;
6870 it->saved_face_id = it->face_id;
6871 it->method = GET_FROM_DISPLAY_VECTOR;
6872 it->ellipsis_p = 0;
6873 goto get_next;
6874 }
6875 it->char_to_display = c;
6876 }
6877 else if (success_p)
6878 {
6879 it->char_to_display = it->c;
6880 }
6881 }
6882
6883 /* Adjust face id for a multibyte character. There are no multibyte
6884 character in unibyte text. */
6885 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6886 && it->multibyte_p
6887 && success_p
6888 && FRAME_WINDOW_P (it->f))
6889 {
6890 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6891
6892 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6893 {
6894 /* Automatic composition with glyph-string. */
6895 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6896
6897 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6898 }
6899 else
6900 {
6901 ptrdiff_t pos = (it->s ? -1
6902 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6903 : IT_CHARPOS (*it));
6904 int c;
6905
6906 if (it->what == IT_CHARACTER)
6907 c = it->char_to_display;
6908 else
6909 {
6910 struct composition *cmp = composition_table[it->cmp_it.id];
6911 int i;
6912
6913 c = ' ';
6914 for (i = 0; i < cmp->glyph_len; i++)
6915 /* TAB in a composition means display glyphs with
6916 padding space on the left or right. */
6917 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6918 break;
6919 }
6920 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6921 }
6922 }
6923
6924 done:
6925 /* Is this character the last one of a run of characters with
6926 box? If yes, set IT->end_of_box_run_p to 1. */
6927 if (it->face_box_p
6928 && it->s == NULL)
6929 {
6930 if (it->method == GET_FROM_STRING && it->sp)
6931 {
6932 int face_id = underlying_face_id (it);
6933 struct face *face = FACE_FROM_ID (it->f, face_id);
6934
6935 if (face)
6936 {
6937 if (face->box == FACE_NO_BOX)
6938 {
6939 /* If the box comes from face properties in a
6940 display string, check faces in that string. */
6941 int string_face_id = face_after_it_pos (it);
6942 it->end_of_box_run_p
6943 = (FACE_FROM_ID (it->f, string_face_id)->box
6944 == FACE_NO_BOX);
6945 }
6946 /* Otherwise, the box comes from the underlying face.
6947 If this is the last string character displayed, check
6948 the next buffer location. */
6949 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6950 && (it->current.overlay_string_index
6951 == it->n_overlay_strings - 1))
6952 {
6953 ptrdiff_t ignore;
6954 int next_face_id;
6955 struct text_pos pos = it->current.pos;
6956 INC_TEXT_POS (pos, it->multibyte_p);
6957
6958 next_face_id = face_at_buffer_position
6959 (it->w, CHARPOS (pos), it->region_beg_charpos,
6960 it->region_end_charpos, &ignore,
6961 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6962 -1);
6963 it->end_of_box_run_p
6964 = (FACE_FROM_ID (it->f, next_face_id)->box
6965 == FACE_NO_BOX);
6966 }
6967 }
6968 }
6969 else
6970 {
6971 int face_id = face_after_it_pos (it);
6972 it->end_of_box_run_p
6973 = (face_id != it->face_id
6974 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6975 }
6976 }
6977 /* If we reached the end of the object we've been iterating (e.g., a
6978 display string or an overlay string), and there's something on
6979 IT->stack, proceed with what's on the stack. It doesn't make
6980 sense to return zero if there's unprocessed stuff on the stack,
6981 because otherwise that stuff will never be displayed. */
6982 if (!success_p && it->sp > 0)
6983 {
6984 set_iterator_to_next (it, 0);
6985 success_p = get_next_display_element (it);
6986 }
6987
6988 /* Value is 0 if end of buffer or string reached. */
6989 return success_p;
6990 }
6991
6992
6993 /* Move IT to the next display element.
6994
6995 RESEAT_P non-zero means if called on a newline in buffer text,
6996 skip to the next visible line start.
6997
6998 Functions get_next_display_element and set_iterator_to_next are
6999 separate because I find this arrangement easier to handle than a
7000 get_next_display_element function that also increments IT's
7001 position. The way it is we can first look at an iterator's current
7002 display element, decide whether it fits on a line, and if it does,
7003 increment the iterator position. The other way around we probably
7004 would either need a flag indicating whether the iterator has to be
7005 incremented the next time, or we would have to implement a
7006 decrement position function which would not be easy to write. */
7007
7008 void
7009 set_iterator_to_next (struct it *it, int reseat_p)
7010 {
7011 /* Reset flags indicating start and end of a sequence of characters
7012 with box. Reset them at the start of this function because
7013 moving the iterator to a new position might set them. */
7014 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7015
7016 switch (it->method)
7017 {
7018 case GET_FROM_BUFFER:
7019 /* The current display element of IT is a character from
7020 current_buffer. Advance in the buffer, and maybe skip over
7021 invisible lines that are so because of selective display. */
7022 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7023 reseat_at_next_visible_line_start (it, 0);
7024 else if (it->cmp_it.id >= 0)
7025 {
7026 /* We are currently getting glyphs from a composition. */
7027 int i;
7028
7029 if (! it->bidi_p)
7030 {
7031 IT_CHARPOS (*it) += it->cmp_it.nchars;
7032 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7033 if (it->cmp_it.to < it->cmp_it.nglyphs)
7034 {
7035 it->cmp_it.from = it->cmp_it.to;
7036 }
7037 else
7038 {
7039 it->cmp_it.id = -1;
7040 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7041 IT_BYTEPOS (*it),
7042 it->end_charpos, Qnil);
7043 }
7044 }
7045 else if (! it->cmp_it.reversed_p)
7046 {
7047 /* Composition created while scanning forward. */
7048 /* Update IT's char/byte positions to point to the first
7049 character of the next grapheme cluster, or to the
7050 character visually after the current composition. */
7051 for (i = 0; i < it->cmp_it.nchars; i++)
7052 bidi_move_to_visually_next (&it->bidi_it);
7053 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7054 IT_CHARPOS (*it) = it->bidi_it.charpos;
7055
7056 if (it->cmp_it.to < it->cmp_it.nglyphs)
7057 {
7058 /* Proceed to the next grapheme cluster. */
7059 it->cmp_it.from = it->cmp_it.to;
7060 }
7061 else
7062 {
7063 /* No more grapheme clusters in this composition.
7064 Find the next stop position. */
7065 ptrdiff_t stop = it->end_charpos;
7066 if (it->bidi_it.scan_dir < 0)
7067 /* Now we are scanning backward and don't know
7068 where to stop. */
7069 stop = -1;
7070 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7071 IT_BYTEPOS (*it), stop, Qnil);
7072 }
7073 }
7074 else
7075 {
7076 /* Composition created while scanning backward. */
7077 /* Update IT's char/byte positions to point to the last
7078 character of the previous grapheme cluster, or the
7079 character visually after the current composition. */
7080 for (i = 0; i < it->cmp_it.nchars; i++)
7081 bidi_move_to_visually_next (&it->bidi_it);
7082 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7083 IT_CHARPOS (*it) = it->bidi_it.charpos;
7084 if (it->cmp_it.from > 0)
7085 {
7086 /* Proceed to the previous grapheme cluster. */
7087 it->cmp_it.to = it->cmp_it.from;
7088 }
7089 else
7090 {
7091 /* No more grapheme clusters in this composition.
7092 Find the next stop position. */
7093 ptrdiff_t stop = it->end_charpos;
7094 if (it->bidi_it.scan_dir < 0)
7095 /* Now we are scanning backward and don't know
7096 where to stop. */
7097 stop = -1;
7098 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7099 IT_BYTEPOS (*it), stop, Qnil);
7100 }
7101 }
7102 }
7103 else
7104 {
7105 eassert (it->len != 0);
7106
7107 if (!it->bidi_p)
7108 {
7109 IT_BYTEPOS (*it) += it->len;
7110 IT_CHARPOS (*it) += 1;
7111 }
7112 else
7113 {
7114 int prev_scan_dir = it->bidi_it.scan_dir;
7115 /* If this is a new paragraph, determine its base
7116 direction (a.k.a. its base embedding level). */
7117 if (it->bidi_it.new_paragraph)
7118 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7119 bidi_move_to_visually_next (&it->bidi_it);
7120 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7121 IT_CHARPOS (*it) = it->bidi_it.charpos;
7122 if (prev_scan_dir != it->bidi_it.scan_dir)
7123 {
7124 /* As the scan direction was changed, we must
7125 re-compute the stop position for composition. */
7126 ptrdiff_t stop = it->end_charpos;
7127 if (it->bidi_it.scan_dir < 0)
7128 stop = -1;
7129 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7130 IT_BYTEPOS (*it), stop, Qnil);
7131 }
7132 }
7133 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7134 }
7135 break;
7136
7137 case GET_FROM_C_STRING:
7138 /* Current display element of IT is from a C string. */
7139 if (!it->bidi_p
7140 /* If the string position is beyond string's end, it means
7141 next_element_from_c_string is padding the string with
7142 blanks, in which case we bypass the bidi iterator,
7143 because it cannot deal with such virtual characters. */
7144 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7145 {
7146 IT_BYTEPOS (*it) += it->len;
7147 IT_CHARPOS (*it) += 1;
7148 }
7149 else
7150 {
7151 bidi_move_to_visually_next (&it->bidi_it);
7152 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7153 IT_CHARPOS (*it) = it->bidi_it.charpos;
7154 }
7155 break;
7156
7157 case GET_FROM_DISPLAY_VECTOR:
7158 /* Current display element of IT is from a display table entry.
7159 Advance in the display table definition. Reset it to null if
7160 end reached, and continue with characters from buffers/
7161 strings. */
7162 ++it->current.dpvec_index;
7163
7164 /* Restore face of the iterator to what they were before the
7165 display vector entry (these entries may contain faces). */
7166 it->face_id = it->saved_face_id;
7167
7168 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7169 {
7170 int recheck_faces = it->ellipsis_p;
7171
7172 if (it->s)
7173 it->method = GET_FROM_C_STRING;
7174 else if (STRINGP (it->string))
7175 it->method = GET_FROM_STRING;
7176 else
7177 {
7178 it->method = GET_FROM_BUFFER;
7179 it->object = it->w->contents;
7180 }
7181
7182 it->dpvec = NULL;
7183 it->current.dpvec_index = -1;
7184
7185 /* Skip over characters which were displayed via IT->dpvec. */
7186 if (it->dpvec_char_len < 0)
7187 reseat_at_next_visible_line_start (it, 1);
7188 else if (it->dpvec_char_len > 0)
7189 {
7190 if (it->method == GET_FROM_STRING
7191 && it->current.overlay_string_index >= 0
7192 && it->n_overlay_strings > 0)
7193 it->ignore_overlay_strings_at_pos_p = 1;
7194 it->len = it->dpvec_char_len;
7195 set_iterator_to_next (it, reseat_p);
7196 }
7197
7198 /* Maybe recheck faces after display vector */
7199 if (recheck_faces)
7200 it->stop_charpos = IT_CHARPOS (*it);
7201 }
7202 break;
7203
7204 case GET_FROM_STRING:
7205 /* Current display element is a character from a Lisp string. */
7206 eassert (it->s == NULL && STRINGP (it->string));
7207 /* Don't advance past string end. These conditions are true
7208 when set_iterator_to_next is called at the end of
7209 get_next_display_element, in which case the Lisp string is
7210 already exhausted, and all we want is pop the iterator
7211 stack. */
7212 if (it->current.overlay_string_index >= 0)
7213 {
7214 /* This is an overlay string, so there's no padding with
7215 spaces, and the number of characters in the string is
7216 where the string ends. */
7217 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7218 goto consider_string_end;
7219 }
7220 else
7221 {
7222 /* Not an overlay string. There could be padding, so test
7223 against it->end_charpos . */
7224 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7225 goto consider_string_end;
7226 }
7227 if (it->cmp_it.id >= 0)
7228 {
7229 int i;
7230
7231 if (! it->bidi_p)
7232 {
7233 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7234 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7235 if (it->cmp_it.to < it->cmp_it.nglyphs)
7236 it->cmp_it.from = it->cmp_it.to;
7237 else
7238 {
7239 it->cmp_it.id = -1;
7240 composition_compute_stop_pos (&it->cmp_it,
7241 IT_STRING_CHARPOS (*it),
7242 IT_STRING_BYTEPOS (*it),
7243 it->end_charpos, it->string);
7244 }
7245 }
7246 else if (! it->cmp_it.reversed_p)
7247 {
7248 for (i = 0; i < it->cmp_it.nchars; i++)
7249 bidi_move_to_visually_next (&it->bidi_it);
7250 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7251 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7252
7253 if (it->cmp_it.to < it->cmp_it.nglyphs)
7254 it->cmp_it.from = it->cmp_it.to;
7255 else
7256 {
7257 ptrdiff_t stop = it->end_charpos;
7258 if (it->bidi_it.scan_dir < 0)
7259 stop = -1;
7260 composition_compute_stop_pos (&it->cmp_it,
7261 IT_STRING_CHARPOS (*it),
7262 IT_STRING_BYTEPOS (*it), stop,
7263 it->string);
7264 }
7265 }
7266 else
7267 {
7268 for (i = 0; i < it->cmp_it.nchars; i++)
7269 bidi_move_to_visually_next (&it->bidi_it);
7270 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7271 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7272 if (it->cmp_it.from > 0)
7273 it->cmp_it.to = it->cmp_it.from;
7274 else
7275 {
7276 ptrdiff_t stop = it->end_charpos;
7277 if (it->bidi_it.scan_dir < 0)
7278 stop = -1;
7279 composition_compute_stop_pos (&it->cmp_it,
7280 IT_STRING_CHARPOS (*it),
7281 IT_STRING_BYTEPOS (*it), stop,
7282 it->string);
7283 }
7284 }
7285 }
7286 else
7287 {
7288 if (!it->bidi_p
7289 /* If the string position is beyond string's end, it
7290 means next_element_from_string is padding the string
7291 with blanks, in which case we bypass the bidi
7292 iterator, because it cannot deal with such virtual
7293 characters. */
7294 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7295 {
7296 IT_STRING_BYTEPOS (*it) += it->len;
7297 IT_STRING_CHARPOS (*it) += 1;
7298 }
7299 else
7300 {
7301 int prev_scan_dir = it->bidi_it.scan_dir;
7302
7303 bidi_move_to_visually_next (&it->bidi_it);
7304 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7305 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7306 if (prev_scan_dir != it->bidi_it.scan_dir)
7307 {
7308 ptrdiff_t stop = it->end_charpos;
7309
7310 if (it->bidi_it.scan_dir < 0)
7311 stop = -1;
7312 composition_compute_stop_pos (&it->cmp_it,
7313 IT_STRING_CHARPOS (*it),
7314 IT_STRING_BYTEPOS (*it), stop,
7315 it->string);
7316 }
7317 }
7318 }
7319
7320 consider_string_end:
7321
7322 if (it->current.overlay_string_index >= 0)
7323 {
7324 /* IT->string is an overlay string. Advance to the
7325 next, if there is one. */
7326 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7327 {
7328 it->ellipsis_p = 0;
7329 next_overlay_string (it);
7330 if (it->ellipsis_p)
7331 setup_for_ellipsis (it, 0);
7332 }
7333 }
7334 else
7335 {
7336 /* IT->string is not an overlay string. If we reached
7337 its end, and there is something on IT->stack, proceed
7338 with what is on the stack. This can be either another
7339 string, this time an overlay string, or a buffer. */
7340 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7341 && it->sp > 0)
7342 {
7343 pop_it (it);
7344 if (it->method == GET_FROM_STRING)
7345 goto consider_string_end;
7346 }
7347 }
7348 break;
7349
7350 case GET_FROM_IMAGE:
7351 case GET_FROM_STRETCH:
7352 /* The position etc with which we have to proceed are on
7353 the stack. The position may be at the end of a string,
7354 if the `display' property takes up the whole string. */
7355 eassert (it->sp > 0);
7356 pop_it (it);
7357 if (it->method == GET_FROM_STRING)
7358 goto consider_string_end;
7359 break;
7360
7361 default:
7362 /* There are no other methods defined, so this should be a bug. */
7363 emacs_abort ();
7364 }
7365
7366 eassert (it->method != GET_FROM_STRING
7367 || (STRINGP (it->string)
7368 && IT_STRING_CHARPOS (*it) >= 0));
7369 }
7370
7371 /* Load IT's display element fields with information about the next
7372 display element which comes from a display table entry or from the
7373 result of translating a control character to one of the forms `^C'
7374 or `\003'.
7375
7376 IT->dpvec holds the glyphs to return as characters.
7377 IT->saved_face_id holds the face id before the display vector--it
7378 is restored into IT->face_id in set_iterator_to_next. */
7379
7380 static int
7381 next_element_from_display_vector (struct it *it)
7382 {
7383 Lisp_Object gc;
7384
7385 /* Precondition. */
7386 eassert (it->dpvec && it->current.dpvec_index >= 0);
7387
7388 it->face_id = it->saved_face_id;
7389
7390 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7391 That seemed totally bogus - so I changed it... */
7392 gc = it->dpvec[it->current.dpvec_index];
7393
7394 if (GLYPH_CODE_P (gc))
7395 {
7396 it->c = GLYPH_CODE_CHAR (gc);
7397 it->len = CHAR_BYTES (it->c);
7398
7399 /* The entry may contain a face id to use. Such a face id is
7400 the id of a Lisp face, not a realized face. A face id of
7401 zero means no face is specified. */
7402 if (it->dpvec_face_id >= 0)
7403 it->face_id = it->dpvec_face_id;
7404 else
7405 {
7406 int lface_id = GLYPH_CODE_FACE (gc);
7407 if (lface_id > 0)
7408 it->face_id = merge_faces (it->f, Qt, lface_id,
7409 it->saved_face_id);
7410 }
7411 }
7412 else
7413 /* Display table entry is invalid. Return a space. */
7414 it->c = ' ', it->len = 1;
7415
7416 /* Don't change position and object of the iterator here. They are
7417 still the values of the character that had this display table
7418 entry or was translated, and that's what we want. */
7419 it->what = IT_CHARACTER;
7420 return 1;
7421 }
7422
7423 /* Get the first element of string/buffer in the visual order, after
7424 being reseated to a new position in a string or a buffer. */
7425 static void
7426 get_visually_first_element (struct it *it)
7427 {
7428 int string_p = STRINGP (it->string) || it->s;
7429 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7430 ptrdiff_t bob = (string_p ? 0 : BEGV);
7431
7432 if (STRINGP (it->string))
7433 {
7434 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7435 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7436 }
7437 else
7438 {
7439 it->bidi_it.charpos = IT_CHARPOS (*it);
7440 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7441 }
7442
7443 if (it->bidi_it.charpos == eob)
7444 {
7445 /* Nothing to do, but reset the FIRST_ELT flag, like
7446 bidi_paragraph_init does, because we are not going to
7447 call it. */
7448 it->bidi_it.first_elt = 0;
7449 }
7450 else if (it->bidi_it.charpos == bob
7451 || (!string_p
7452 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7453 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7454 {
7455 /* If we are at the beginning of a line/string, we can produce
7456 the next element right away. */
7457 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7458 bidi_move_to_visually_next (&it->bidi_it);
7459 }
7460 else
7461 {
7462 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7463
7464 /* We need to prime the bidi iterator starting at the line's or
7465 string's beginning, before we will be able to produce the
7466 next element. */
7467 if (string_p)
7468 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7469 else
7470 it->bidi_it.charpos = find_newline_no_quit (IT_CHARPOS (*it),
7471 IT_BYTEPOS (*it), -1,
7472 &it->bidi_it.bytepos);
7473 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7474 do
7475 {
7476 /* Now return to buffer/string position where we were asked
7477 to get the next display element, and produce that. */
7478 bidi_move_to_visually_next (&it->bidi_it);
7479 }
7480 while (it->bidi_it.bytepos != orig_bytepos
7481 && it->bidi_it.charpos < eob);
7482 }
7483
7484 /* Adjust IT's position information to where we ended up. */
7485 if (STRINGP (it->string))
7486 {
7487 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7488 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7489 }
7490 else
7491 {
7492 IT_CHARPOS (*it) = it->bidi_it.charpos;
7493 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7494 }
7495
7496 if (STRINGP (it->string) || !it->s)
7497 {
7498 ptrdiff_t stop, charpos, bytepos;
7499
7500 if (STRINGP (it->string))
7501 {
7502 eassert (!it->s);
7503 stop = SCHARS (it->string);
7504 if (stop > it->end_charpos)
7505 stop = it->end_charpos;
7506 charpos = IT_STRING_CHARPOS (*it);
7507 bytepos = IT_STRING_BYTEPOS (*it);
7508 }
7509 else
7510 {
7511 stop = it->end_charpos;
7512 charpos = IT_CHARPOS (*it);
7513 bytepos = IT_BYTEPOS (*it);
7514 }
7515 if (it->bidi_it.scan_dir < 0)
7516 stop = -1;
7517 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7518 it->string);
7519 }
7520 }
7521
7522 /* Load IT with the next display element from Lisp string IT->string.
7523 IT->current.string_pos is the current position within the string.
7524 If IT->current.overlay_string_index >= 0, the Lisp string is an
7525 overlay string. */
7526
7527 static int
7528 next_element_from_string (struct it *it)
7529 {
7530 struct text_pos position;
7531
7532 eassert (STRINGP (it->string));
7533 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7534 eassert (IT_STRING_CHARPOS (*it) >= 0);
7535 position = it->current.string_pos;
7536
7537 /* With bidi reordering, the character to display might not be the
7538 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7539 that we were reseat()ed to a new string, whose paragraph
7540 direction is not known. */
7541 if (it->bidi_p && it->bidi_it.first_elt)
7542 {
7543 get_visually_first_element (it);
7544 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7545 }
7546
7547 /* Time to check for invisible text? */
7548 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7549 {
7550 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7551 {
7552 if (!(!it->bidi_p
7553 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7554 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7555 {
7556 /* With bidi non-linear iteration, we could find
7557 ourselves far beyond the last computed stop_charpos,
7558 with several other stop positions in between that we
7559 missed. Scan them all now, in buffer's logical
7560 order, until we find and handle the last stop_charpos
7561 that precedes our current position. */
7562 handle_stop_backwards (it, it->stop_charpos);
7563 return GET_NEXT_DISPLAY_ELEMENT (it);
7564 }
7565 else
7566 {
7567 if (it->bidi_p)
7568 {
7569 /* Take note of the stop position we just moved
7570 across, for when we will move back across it. */
7571 it->prev_stop = it->stop_charpos;
7572 /* If we are at base paragraph embedding level, take
7573 note of the last stop position seen at this
7574 level. */
7575 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7576 it->base_level_stop = it->stop_charpos;
7577 }
7578 handle_stop (it);
7579
7580 /* Since a handler may have changed IT->method, we must
7581 recurse here. */
7582 return GET_NEXT_DISPLAY_ELEMENT (it);
7583 }
7584 }
7585 else if (it->bidi_p
7586 /* If we are before prev_stop, we may have overstepped
7587 on our way backwards a stop_pos, and if so, we need
7588 to handle that stop_pos. */
7589 && IT_STRING_CHARPOS (*it) < it->prev_stop
7590 /* We can sometimes back up for reasons that have nothing
7591 to do with bidi reordering. E.g., compositions. The
7592 code below is only needed when we are above the base
7593 embedding level, so test for that explicitly. */
7594 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7595 {
7596 /* If we lost track of base_level_stop, we have no better
7597 place for handle_stop_backwards to start from than string
7598 beginning. This happens, e.g., when we were reseated to
7599 the previous screenful of text by vertical-motion. */
7600 if (it->base_level_stop <= 0
7601 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7602 it->base_level_stop = 0;
7603 handle_stop_backwards (it, it->base_level_stop);
7604 return GET_NEXT_DISPLAY_ELEMENT (it);
7605 }
7606 }
7607
7608 if (it->current.overlay_string_index >= 0)
7609 {
7610 /* Get the next character from an overlay string. In overlay
7611 strings, there is no field width or padding with spaces to
7612 do. */
7613 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7614 {
7615 it->what = IT_EOB;
7616 return 0;
7617 }
7618 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7619 IT_STRING_BYTEPOS (*it),
7620 it->bidi_it.scan_dir < 0
7621 ? -1
7622 : SCHARS (it->string))
7623 && next_element_from_composition (it))
7624 {
7625 return 1;
7626 }
7627 else if (STRING_MULTIBYTE (it->string))
7628 {
7629 const unsigned char *s = (SDATA (it->string)
7630 + IT_STRING_BYTEPOS (*it));
7631 it->c = string_char_and_length (s, &it->len);
7632 }
7633 else
7634 {
7635 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7636 it->len = 1;
7637 }
7638 }
7639 else
7640 {
7641 /* Get the next character from a Lisp string that is not an
7642 overlay string. Such strings come from the mode line, for
7643 example. We may have to pad with spaces, or truncate the
7644 string. See also next_element_from_c_string. */
7645 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7646 {
7647 it->what = IT_EOB;
7648 return 0;
7649 }
7650 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7651 {
7652 /* Pad with spaces. */
7653 it->c = ' ', it->len = 1;
7654 CHARPOS (position) = BYTEPOS (position) = -1;
7655 }
7656 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7657 IT_STRING_BYTEPOS (*it),
7658 it->bidi_it.scan_dir < 0
7659 ? -1
7660 : it->string_nchars)
7661 && next_element_from_composition (it))
7662 {
7663 return 1;
7664 }
7665 else if (STRING_MULTIBYTE (it->string))
7666 {
7667 const unsigned char *s = (SDATA (it->string)
7668 + IT_STRING_BYTEPOS (*it));
7669 it->c = string_char_and_length (s, &it->len);
7670 }
7671 else
7672 {
7673 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7674 it->len = 1;
7675 }
7676 }
7677
7678 /* Record what we have and where it came from. */
7679 it->what = IT_CHARACTER;
7680 it->object = it->string;
7681 it->position = position;
7682 return 1;
7683 }
7684
7685
7686 /* Load IT with next display element from C string IT->s.
7687 IT->string_nchars is the maximum number of characters to return
7688 from the string. IT->end_charpos may be greater than
7689 IT->string_nchars when this function is called, in which case we
7690 may have to return padding spaces. Value is zero if end of string
7691 reached, including padding spaces. */
7692
7693 static int
7694 next_element_from_c_string (struct it *it)
7695 {
7696 int success_p = 1;
7697
7698 eassert (it->s);
7699 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7700 it->what = IT_CHARACTER;
7701 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7702 it->object = Qnil;
7703
7704 /* With bidi reordering, the character to display might not be the
7705 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7706 we were reseated to a new string, whose paragraph direction is
7707 not known. */
7708 if (it->bidi_p && it->bidi_it.first_elt)
7709 get_visually_first_element (it);
7710
7711 /* IT's position can be greater than IT->string_nchars in case a
7712 field width or precision has been specified when the iterator was
7713 initialized. */
7714 if (IT_CHARPOS (*it) >= it->end_charpos)
7715 {
7716 /* End of the game. */
7717 it->what = IT_EOB;
7718 success_p = 0;
7719 }
7720 else if (IT_CHARPOS (*it) >= it->string_nchars)
7721 {
7722 /* Pad with spaces. */
7723 it->c = ' ', it->len = 1;
7724 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7725 }
7726 else if (it->multibyte_p)
7727 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7728 else
7729 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7730
7731 return success_p;
7732 }
7733
7734
7735 /* Set up IT to return characters from an ellipsis, if appropriate.
7736 The definition of the ellipsis glyphs may come from a display table
7737 entry. This function fills IT with the first glyph from the
7738 ellipsis if an ellipsis is to be displayed. */
7739
7740 static int
7741 next_element_from_ellipsis (struct it *it)
7742 {
7743 if (it->selective_display_ellipsis_p)
7744 setup_for_ellipsis (it, it->len);
7745 else
7746 {
7747 /* The face at the current position may be different from the
7748 face we find after the invisible text. Remember what it
7749 was in IT->saved_face_id, and signal that it's there by
7750 setting face_before_selective_p. */
7751 it->saved_face_id = it->face_id;
7752 it->method = GET_FROM_BUFFER;
7753 it->object = it->w->contents;
7754 reseat_at_next_visible_line_start (it, 1);
7755 it->face_before_selective_p = 1;
7756 }
7757
7758 return GET_NEXT_DISPLAY_ELEMENT (it);
7759 }
7760
7761
7762 /* Deliver an image display element. The iterator IT is already
7763 filled with image information (done in handle_display_prop). Value
7764 is always 1. */
7765
7766
7767 static int
7768 next_element_from_image (struct it *it)
7769 {
7770 it->what = IT_IMAGE;
7771 it->ignore_overlay_strings_at_pos_p = 0;
7772 return 1;
7773 }
7774
7775
7776 /* Fill iterator IT with next display element from a stretch glyph
7777 property. IT->object is the value of the text property. Value is
7778 always 1. */
7779
7780 static int
7781 next_element_from_stretch (struct it *it)
7782 {
7783 it->what = IT_STRETCH;
7784 return 1;
7785 }
7786
7787 /* Scan backwards from IT's current position until we find a stop
7788 position, or until BEGV. This is called when we find ourself
7789 before both the last known prev_stop and base_level_stop while
7790 reordering bidirectional text. */
7791
7792 static void
7793 compute_stop_pos_backwards (struct it *it)
7794 {
7795 const int SCAN_BACK_LIMIT = 1000;
7796 struct text_pos pos;
7797 struct display_pos save_current = it->current;
7798 struct text_pos save_position = it->position;
7799 ptrdiff_t charpos = IT_CHARPOS (*it);
7800 ptrdiff_t where_we_are = charpos;
7801 ptrdiff_t save_stop_pos = it->stop_charpos;
7802 ptrdiff_t save_end_pos = it->end_charpos;
7803
7804 eassert (NILP (it->string) && !it->s);
7805 eassert (it->bidi_p);
7806 it->bidi_p = 0;
7807 do
7808 {
7809 it->end_charpos = min (charpos + 1, ZV);
7810 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7811 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7812 reseat_1 (it, pos, 0);
7813 compute_stop_pos (it);
7814 /* We must advance forward, right? */
7815 if (it->stop_charpos <= charpos)
7816 emacs_abort ();
7817 }
7818 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7819
7820 if (it->stop_charpos <= where_we_are)
7821 it->prev_stop = it->stop_charpos;
7822 else
7823 it->prev_stop = BEGV;
7824 it->bidi_p = 1;
7825 it->current = save_current;
7826 it->position = save_position;
7827 it->stop_charpos = save_stop_pos;
7828 it->end_charpos = save_end_pos;
7829 }
7830
7831 /* Scan forward from CHARPOS in the current buffer/string, until we
7832 find a stop position > current IT's position. Then handle the stop
7833 position before that. This is called when we bump into a stop
7834 position while reordering bidirectional text. CHARPOS should be
7835 the last previously processed stop_pos (or BEGV/0, if none were
7836 processed yet) whose position is less that IT's current
7837 position. */
7838
7839 static void
7840 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7841 {
7842 int bufp = !STRINGP (it->string);
7843 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7844 struct display_pos save_current = it->current;
7845 struct text_pos save_position = it->position;
7846 struct text_pos pos1;
7847 ptrdiff_t next_stop;
7848
7849 /* Scan in strict logical order. */
7850 eassert (it->bidi_p);
7851 it->bidi_p = 0;
7852 do
7853 {
7854 it->prev_stop = charpos;
7855 if (bufp)
7856 {
7857 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7858 reseat_1 (it, pos1, 0);
7859 }
7860 else
7861 it->current.string_pos = string_pos (charpos, it->string);
7862 compute_stop_pos (it);
7863 /* We must advance forward, right? */
7864 if (it->stop_charpos <= it->prev_stop)
7865 emacs_abort ();
7866 charpos = it->stop_charpos;
7867 }
7868 while (charpos <= where_we_are);
7869
7870 it->bidi_p = 1;
7871 it->current = save_current;
7872 it->position = save_position;
7873 next_stop = it->stop_charpos;
7874 it->stop_charpos = it->prev_stop;
7875 handle_stop (it);
7876 it->stop_charpos = next_stop;
7877 }
7878
7879 /* Load IT with the next display element from current_buffer. Value
7880 is zero if end of buffer reached. IT->stop_charpos is the next
7881 position at which to stop and check for text properties or buffer
7882 end. */
7883
7884 static int
7885 next_element_from_buffer (struct it *it)
7886 {
7887 int success_p = 1;
7888
7889 eassert (IT_CHARPOS (*it) >= BEGV);
7890 eassert (NILP (it->string) && !it->s);
7891 eassert (!it->bidi_p
7892 || (EQ (it->bidi_it.string.lstring, Qnil)
7893 && it->bidi_it.string.s == NULL));
7894
7895 /* With bidi reordering, the character to display might not be the
7896 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7897 we were reseat()ed to a new buffer position, which is potentially
7898 a different paragraph. */
7899 if (it->bidi_p && it->bidi_it.first_elt)
7900 {
7901 get_visually_first_element (it);
7902 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7903 }
7904
7905 if (IT_CHARPOS (*it) >= it->stop_charpos)
7906 {
7907 if (IT_CHARPOS (*it) >= it->end_charpos)
7908 {
7909 int overlay_strings_follow_p;
7910
7911 /* End of the game, except when overlay strings follow that
7912 haven't been returned yet. */
7913 if (it->overlay_strings_at_end_processed_p)
7914 overlay_strings_follow_p = 0;
7915 else
7916 {
7917 it->overlay_strings_at_end_processed_p = 1;
7918 overlay_strings_follow_p = get_overlay_strings (it, 0);
7919 }
7920
7921 if (overlay_strings_follow_p)
7922 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7923 else
7924 {
7925 it->what = IT_EOB;
7926 it->position = it->current.pos;
7927 success_p = 0;
7928 }
7929 }
7930 else if (!(!it->bidi_p
7931 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7932 || IT_CHARPOS (*it) == it->stop_charpos))
7933 {
7934 /* With bidi non-linear iteration, we could find ourselves
7935 far beyond the last computed stop_charpos, with several
7936 other stop positions in between that we missed. Scan
7937 them all now, in buffer's logical order, until we find
7938 and handle the last stop_charpos that precedes our
7939 current position. */
7940 handle_stop_backwards (it, it->stop_charpos);
7941 return GET_NEXT_DISPLAY_ELEMENT (it);
7942 }
7943 else
7944 {
7945 if (it->bidi_p)
7946 {
7947 /* Take note of the stop position we just moved across,
7948 for when we will move back across it. */
7949 it->prev_stop = it->stop_charpos;
7950 /* If we are at base paragraph embedding level, take
7951 note of the last stop position seen at this
7952 level. */
7953 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7954 it->base_level_stop = it->stop_charpos;
7955 }
7956 handle_stop (it);
7957 return GET_NEXT_DISPLAY_ELEMENT (it);
7958 }
7959 }
7960 else if (it->bidi_p
7961 /* If we are before prev_stop, we may have overstepped on
7962 our way backwards a stop_pos, and if so, we need to
7963 handle that stop_pos. */
7964 && IT_CHARPOS (*it) < it->prev_stop
7965 /* We can sometimes back up for reasons that have nothing
7966 to do with bidi reordering. E.g., compositions. The
7967 code below is only needed when we are above the base
7968 embedding level, so test for that explicitly. */
7969 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7970 {
7971 if (it->base_level_stop <= 0
7972 || IT_CHARPOS (*it) < it->base_level_stop)
7973 {
7974 /* If we lost track of base_level_stop, we need to find
7975 prev_stop by looking backwards. This happens, e.g., when
7976 we were reseated to the previous screenful of text by
7977 vertical-motion. */
7978 it->base_level_stop = BEGV;
7979 compute_stop_pos_backwards (it);
7980 handle_stop_backwards (it, it->prev_stop);
7981 }
7982 else
7983 handle_stop_backwards (it, it->base_level_stop);
7984 return GET_NEXT_DISPLAY_ELEMENT (it);
7985 }
7986 else
7987 {
7988 /* No face changes, overlays etc. in sight, so just return a
7989 character from current_buffer. */
7990 unsigned char *p;
7991 ptrdiff_t stop;
7992
7993 /* Maybe run the redisplay end trigger hook. Performance note:
7994 This doesn't seem to cost measurable time. */
7995 if (it->redisplay_end_trigger_charpos
7996 && it->glyph_row
7997 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
7998 run_redisplay_end_trigger_hook (it);
7999
8000 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8001 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8002 stop)
8003 && next_element_from_composition (it))
8004 {
8005 return 1;
8006 }
8007
8008 /* Get the next character, maybe multibyte. */
8009 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8010 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8011 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8012 else
8013 it->c = *p, it->len = 1;
8014
8015 /* Record what we have and where it came from. */
8016 it->what = IT_CHARACTER;
8017 it->object = it->w->contents;
8018 it->position = it->current.pos;
8019
8020 /* Normally we return the character found above, except when we
8021 really want to return an ellipsis for selective display. */
8022 if (it->selective)
8023 {
8024 if (it->c == '\n')
8025 {
8026 /* A value of selective > 0 means hide lines indented more
8027 than that number of columns. */
8028 if (it->selective > 0
8029 && IT_CHARPOS (*it) + 1 < ZV
8030 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8031 IT_BYTEPOS (*it) + 1,
8032 it->selective))
8033 {
8034 success_p = next_element_from_ellipsis (it);
8035 it->dpvec_char_len = -1;
8036 }
8037 }
8038 else if (it->c == '\r' && it->selective == -1)
8039 {
8040 /* A value of selective == -1 means that everything from the
8041 CR to the end of the line is invisible, with maybe an
8042 ellipsis displayed for it. */
8043 success_p = next_element_from_ellipsis (it);
8044 it->dpvec_char_len = -1;
8045 }
8046 }
8047 }
8048
8049 /* Value is zero if end of buffer reached. */
8050 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8051 return success_p;
8052 }
8053
8054
8055 /* Run the redisplay end trigger hook for IT. */
8056
8057 static void
8058 run_redisplay_end_trigger_hook (struct it *it)
8059 {
8060 Lisp_Object args[3];
8061
8062 /* IT->glyph_row should be non-null, i.e. we should be actually
8063 displaying something, or otherwise we should not run the hook. */
8064 eassert (it->glyph_row);
8065
8066 /* Set up hook arguments. */
8067 args[0] = Qredisplay_end_trigger_functions;
8068 args[1] = it->window;
8069 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8070 it->redisplay_end_trigger_charpos = 0;
8071
8072 /* Since we are *trying* to run these functions, don't try to run
8073 them again, even if they get an error. */
8074 wset_redisplay_end_trigger (it->w, Qnil);
8075 Frun_hook_with_args (3, args);
8076
8077 /* Notice if it changed the face of the character we are on. */
8078 handle_face_prop (it);
8079 }
8080
8081
8082 /* Deliver a composition display element. Unlike the other
8083 next_element_from_XXX, this function is not registered in the array
8084 get_next_element[]. It is called from next_element_from_buffer and
8085 next_element_from_string when necessary. */
8086
8087 static int
8088 next_element_from_composition (struct it *it)
8089 {
8090 it->what = IT_COMPOSITION;
8091 it->len = it->cmp_it.nbytes;
8092 if (STRINGP (it->string))
8093 {
8094 if (it->c < 0)
8095 {
8096 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8097 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8098 return 0;
8099 }
8100 it->position = it->current.string_pos;
8101 it->object = it->string;
8102 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8103 IT_STRING_BYTEPOS (*it), it->string);
8104 }
8105 else
8106 {
8107 if (it->c < 0)
8108 {
8109 IT_CHARPOS (*it) += it->cmp_it.nchars;
8110 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8111 if (it->bidi_p)
8112 {
8113 if (it->bidi_it.new_paragraph)
8114 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8115 /* Resync the bidi iterator with IT's new position.
8116 FIXME: this doesn't support bidirectional text. */
8117 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8118 bidi_move_to_visually_next (&it->bidi_it);
8119 }
8120 return 0;
8121 }
8122 it->position = it->current.pos;
8123 it->object = it->w->contents;
8124 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8125 IT_BYTEPOS (*it), Qnil);
8126 }
8127 return 1;
8128 }
8129
8130
8131 \f
8132 /***********************************************************************
8133 Moving an iterator without producing glyphs
8134 ***********************************************************************/
8135
8136 /* Check if iterator is at a position corresponding to a valid buffer
8137 position after some move_it_ call. */
8138
8139 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8140 ((it)->method == GET_FROM_STRING \
8141 ? IT_STRING_CHARPOS (*it) == 0 \
8142 : 1)
8143
8144
8145 /* Move iterator IT to a specified buffer or X position within one
8146 line on the display without producing glyphs.
8147
8148 OP should be a bit mask including some or all of these bits:
8149 MOVE_TO_X: Stop upon reaching x-position TO_X.
8150 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8151 Regardless of OP's value, stop upon reaching the end of the display line.
8152
8153 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8154 This means, in particular, that TO_X includes window's horizontal
8155 scroll amount.
8156
8157 The return value has several possible values that
8158 say what condition caused the scan to stop:
8159
8160 MOVE_POS_MATCH_OR_ZV
8161 - when TO_POS or ZV was reached.
8162
8163 MOVE_X_REACHED
8164 -when TO_X was reached before TO_POS or ZV were reached.
8165
8166 MOVE_LINE_CONTINUED
8167 - when we reached the end of the display area and the line must
8168 be continued.
8169
8170 MOVE_LINE_TRUNCATED
8171 - when we reached the end of the display area and the line is
8172 truncated.
8173
8174 MOVE_NEWLINE_OR_CR
8175 - when we stopped at a line end, i.e. a newline or a CR and selective
8176 display is on. */
8177
8178 static enum move_it_result
8179 move_it_in_display_line_to (struct it *it,
8180 ptrdiff_t to_charpos, int to_x,
8181 enum move_operation_enum op)
8182 {
8183 enum move_it_result result = MOVE_UNDEFINED;
8184 struct glyph_row *saved_glyph_row;
8185 struct it wrap_it, atpos_it, atx_it, ppos_it;
8186 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8187 void *ppos_data = NULL;
8188 int may_wrap = 0;
8189 enum it_method prev_method = it->method;
8190 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8191 int saw_smaller_pos = prev_pos < to_charpos;
8192
8193 /* Don't produce glyphs in produce_glyphs. */
8194 saved_glyph_row = it->glyph_row;
8195 it->glyph_row = NULL;
8196
8197 /* Use wrap_it to save a copy of IT wherever a word wrap could
8198 occur. Use atpos_it to save a copy of IT at the desired buffer
8199 position, if found, so that we can scan ahead and check if the
8200 word later overshoots the window edge. Use atx_it similarly, for
8201 pixel positions. */
8202 wrap_it.sp = -1;
8203 atpos_it.sp = -1;
8204 atx_it.sp = -1;
8205
8206 /* Use ppos_it under bidi reordering to save a copy of IT for the
8207 position > CHARPOS that is the closest to CHARPOS. We restore
8208 that position in IT when we have scanned the entire display line
8209 without finding a match for CHARPOS and all the character
8210 positions are greater than CHARPOS. */
8211 if (it->bidi_p)
8212 {
8213 SAVE_IT (ppos_it, *it, ppos_data);
8214 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8215 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8216 SAVE_IT (ppos_it, *it, ppos_data);
8217 }
8218
8219 #define BUFFER_POS_REACHED_P() \
8220 ((op & MOVE_TO_POS) != 0 \
8221 && BUFFERP (it->object) \
8222 && (IT_CHARPOS (*it) == to_charpos \
8223 || ((!it->bidi_p \
8224 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8225 && IT_CHARPOS (*it) > to_charpos) \
8226 || (it->what == IT_COMPOSITION \
8227 && ((IT_CHARPOS (*it) > to_charpos \
8228 && to_charpos >= it->cmp_it.charpos) \
8229 || (IT_CHARPOS (*it) < to_charpos \
8230 && to_charpos <= it->cmp_it.charpos)))) \
8231 && (it->method == GET_FROM_BUFFER \
8232 || (it->method == GET_FROM_DISPLAY_VECTOR \
8233 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8234
8235 /* If there's a line-/wrap-prefix, handle it. */
8236 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8237 && it->current_y < it->last_visible_y)
8238 handle_line_prefix (it);
8239
8240 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8241 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8242
8243 while (1)
8244 {
8245 int x, i, ascent = 0, descent = 0;
8246
8247 /* Utility macro to reset an iterator with x, ascent, and descent. */
8248 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8249 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8250 (IT)->max_descent = descent)
8251
8252 /* Stop if we move beyond TO_CHARPOS (after an image or a
8253 display string or stretch glyph). */
8254 if ((op & MOVE_TO_POS) != 0
8255 && BUFFERP (it->object)
8256 && it->method == GET_FROM_BUFFER
8257 && (((!it->bidi_p
8258 /* When the iterator is at base embedding level, we
8259 are guaranteed that characters are delivered for
8260 display in strictly increasing order of their
8261 buffer positions. */
8262 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8263 && IT_CHARPOS (*it) > to_charpos)
8264 || (it->bidi_p
8265 && (prev_method == GET_FROM_IMAGE
8266 || prev_method == GET_FROM_STRETCH
8267 || prev_method == GET_FROM_STRING)
8268 /* Passed TO_CHARPOS from left to right. */
8269 && ((prev_pos < to_charpos
8270 && IT_CHARPOS (*it) > to_charpos)
8271 /* Passed TO_CHARPOS from right to left. */
8272 || (prev_pos > to_charpos
8273 && IT_CHARPOS (*it) < to_charpos)))))
8274 {
8275 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8276 {
8277 result = MOVE_POS_MATCH_OR_ZV;
8278 break;
8279 }
8280 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8281 /* If wrap_it is valid, the current position might be in a
8282 word that is wrapped. So, save the iterator in
8283 atpos_it and continue to see if wrapping happens. */
8284 SAVE_IT (atpos_it, *it, atpos_data);
8285 }
8286
8287 /* Stop when ZV reached.
8288 We used to stop here when TO_CHARPOS reached as well, but that is
8289 too soon if this glyph does not fit on this line. So we handle it
8290 explicitly below. */
8291 if (!get_next_display_element (it))
8292 {
8293 result = MOVE_POS_MATCH_OR_ZV;
8294 break;
8295 }
8296
8297 if (it->line_wrap == TRUNCATE)
8298 {
8299 if (BUFFER_POS_REACHED_P ())
8300 {
8301 result = MOVE_POS_MATCH_OR_ZV;
8302 break;
8303 }
8304 }
8305 else
8306 {
8307 if (it->line_wrap == WORD_WRAP)
8308 {
8309 if (IT_DISPLAYING_WHITESPACE (it))
8310 may_wrap = 1;
8311 else if (may_wrap)
8312 {
8313 /* We have reached a glyph that follows one or more
8314 whitespace characters. If the position is
8315 already found, we are done. */
8316 if (atpos_it.sp >= 0)
8317 {
8318 RESTORE_IT (it, &atpos_it, atpos_data);
8319 result = MOVE_POS_MATCH_OR_ZV;
8320 goto done;
8321 }
8322 if (atx_it.sp >= 0)
8323 {
8324 RESTORE_IT (it, &atx_it, atx_data);
8325 result = MOVE_X_REACHED;
8326 goto done;
8327 }
8328 /* Otherwise, we can wrap here. */
8329 SAVE_IT (wrap_it, *it, wrap_data);
8330 may_wrap = 0;
8331 }
8332 }
8333 }
8334
8335 /* Remember the line height for the current line, in case
8336 the next element doesn't fit on the line. */
8337 ascent = it->max_ascent;
8338 descent = it->max_descent;
8339
8340 /* The call to produce_glyphs will get the metrics of the
8341 display element IT is loaded with. Record the x-position
8342 before this display element, in case it doesn't fit on the
8343 line. */
8344 x = it->current_x;
8345
8346 PRODUCE_GLYPHS (it);
8347
8348 if (it->area != TEXT_AREA)
8349 {
8350 prev_method = it->method;
8351 if (it->method == GET_FROM_BUFFER)
8352 prev_pos = IT_CHARPOS (*it);
8353 set_iterator_to_next (it, 1);
8354 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8355 SET_TEXT_POS (this_line_min_pos,
8356 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8357 if (it->bidi_p
8358 && (op & MOVE_TO_POS)
8359 && IT_CHARPOS (*it) > to_charpos
8360 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8361 SAVE_IT (ppos_it, *it, ppos_data);
8362 continue;
8363 }
8364
8365 /* The number of glyphs we get back in IT->nglyphs will normally
8366 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8367 character on a terminal frame, or (iii) a line end. For the
8368 second case, IT->nglyphs - 1 padding glyphs will be present.
8369 (On X frames, there is only one glyph produced for a
8370 composite character.)
8371
8372 The behavior implemented below means, for continuation lines,
8373 that as many spaces of a TAB as fit on the current line are
8374 displayed there. For terminal frames, as many glyphs of a
8375 multi-glyph character are displayed in the current line, too.
8376 This is what the old redisplay code did, and we keep it that
8377 way. Under X, the whole shape of a complex character must
8378 fit on the line or it will be completely displayed in the
8379 next line.
8380
8381 Note that both for tabs and padding glyphs, all glyphs have
8382 the same width. */
8383 if (it->nglyphs)
8384 {
8385 /* More than one glyph or glyph doesn't fit on line. All
8386 glyphs have the same width. */
8387 int single_glyph_width = it->pixel_width / it->nglyphs;
8388 int new_x;
8389 int x_before_this_char = x;
8390 int hpos_before_this_char = it->hpos;
8391
8392 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8393 {
8394 new_x = x + single_glyph_width;
8395
8396 /* We want to leave anything reaching TO_X to the caller. */
8397 if ((op & MOVE_TO_X) && new_x > to_x)
8398 {
8399 if (BUFFER_POS_REACHED_P ())
8400 {
8401 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8402 goto buffer_pos_reached;
8403 if (atpos_it.sp < 0)
8404 {
8405 SAVE_IT (atpos_it, *it, atpos_data);
8406 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8407 }
8408 }
8409 else
8410 {
8411 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8412 {
8413 it->current_x = x;
8414 result = MOVE_X_REACHED;
8415 break;
8416 }
8417 if (atx_it.sp < 0)
8418 {
8419 SAVE_IT (atx_it, *it, atx_data);
8420 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8421 }
8422 }
8423 }
8424
8425 if (/* Lines are continued. */
8426 it->line_wrap != TRUNCATE
8427 && (/* And glyph doesn't fit on the line. */
8428 new_x > it->last_visible_x
8429 /* Or it fits exactly and we're on a window
8430 system frame. */
8431 || (new_x == it->last_visible_x
8432 && FRAME_WINDOW_P (it->f)
8433 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8434 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8435 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8436 {
8437 if (/* IT->hpos == 0 means the very first glyph
8438 doesn't fit on the line, e.g. a wide image. */
8439 it->hpos == 0
8440 || (new_x == it->last_visible_x
8441 && FRAME_WINDOW_P (it->f)))
8442 {
8443 ++it->hpos;
8444 it->current_x = new_x;
8445
8446 /* The character's last glyph just barely fits
8447 in this row. */
8448 if (i == it->nglyphs - 1)
8449 {
8450 /* If this is the destination position,
8451 return a position *before* it in this row,
8452 now that we know it fits in this row. */
8453 if (BUFFER_POS_REACHED_P ())
8454 {
8455 if (it->line_wrap != WORD_WRAP
8456 || wrap_it.sp < 0)
8457 {
8458 it->hpos = hpos_before_this_char;
8459 it->current_x = x_before_this_char;
8460 result = MOVE_POS_MATCH_OR_ZV;
8461 break;
8462 }
8463 if (it->line_wrap == WORD_WRAP
8464 && atpos_it.sp < 0)
8465 {
8466 SAVE_IT (atpos_it, *it, atpos_data);
8467 atpos_it.current_x = x_before_this_char;
8468 atpos_it.hpos = hpos_before_this_char;
8469 }
8470 }
8471
8472 prev_method = it->method;
8473 if (it->method == GET_FROM_BUFFER)
8474 prev_pos = IT_CHARPOS (*it);
8475 set_iterator_to_next (it, 1);
8476 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8477 SET_TEXT_POS (this_line_min_pos,
8478 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8479 /* On graphical terminals, newlines may
8480 "overflow" into the fringe if
8481 overflow-newline-into-fringe is non-nil.
8482 On text terminals, and on graphical
8483 terminals with no right margin, newlines
8484 may overflow into the last glyph on the
8485 display line.*/
8486 if (!FRAME_WINDOW_P (it->f)
8487 || ((it->bidi_p
8488 && it->bidi_it.paragraph_dir == R2L)
8489 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8490 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8491 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8492 {
8493 if (!get_next_display_element (it))
8494 {
8495 result = MOVE_POS_MATCH_OR_ZV;
8496 break;
8497 }
8498 if (BUFFER_POS_REACHED_P ())
8499 {
8500 if (ITERATOR_AT_END_OF_LINE_P (it))
8501 result = MOVE_POS_MATCH_OR_ZV;
8502 else
8503 result = MOVE_LINE_CONTINUED;
8504 break;
8505 }
8506 if (ITERATOR_AT_END_OF_LINE_P (it))
8507 {
8508 result = MOVE_NEWLINE_OR_CR;
8509 break;
8510 }
8511 }
8512 }
8513 }
8514 else
8515 IT_RESET_X_ASCENT_DESCENT (it);
8516
8517 if (wrap_it.sp >= 0)
8518 {
8519 RESTORE_IT (it, &wrap_it, wrap_data);
8520 atpos_it.sp = -1;
8521 atx_it.sp = -1;
8522 }
8523
8524 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8525 IT_CHARPOS (*it)));
8526 result = MOVE_LINE_CONTINUED;
8527 break;
8528 }
8529
8530 if (BUFFER_POS_REACHED_P ())
8531 {
8532 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8533 goto buffer_pos_reached;
8534 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8535 {
8536 SAVE_IT (atpos_it, *it, atpos_data);
8537 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8538 }
8539 }
8540
8541 if (new_x > it->first_visible_x)
8542 {
8543 /* Glyph is visible. Increment number of glyphs that
8544 would be displayed. */
8545 ++it->hpos;
8546 }
8547 }
8548
8549 if (result != MOVE_UNDEFINED)
8550 break;
8551 }
8552 else if (BUFFER_POS_REACHED_P ())
8553 {
8554 buffer_pos_reached:
8555 IT_RESET_X_ASCENT_DESCENT (it);
8556 result = MOVE_POS_MATCH_OR_ZV;
8557 break;
8558 }
8559 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8560 {
8561 /* Stop when TO_X specified and reached. This check is
8562 necessary here because of lines consisting of a line end,
8563 only. The line end will not produce any glyphs and we
8564 would never get MOVE_X_REACHED. */
8565 eassert (it->nglyphs == 0);
8566 result = MOVE_X_REACHED;
8567 break;
8568 }
8569
8570 /* Is this a line end? If yes, we're done. */
8571 if (ITERATOR_AT_END_OF_LINE_P (it))
8572 {
8573 /* If we are past TO_CHARPOS, but never saw any character
8574 positions smaller than TO_CHARPOS, return
8575 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8576 did. */
8577 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8578 {
8579 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8580 {
8581 if (IT_CHARPOS (ppos_it) < ZV)
8582 {
8583 RESTORE_IT (it, &ppos_it, ppos_data);
8584 result = MOVE_POS_MATCH_OR_ZV;
8585 }
8586 else
8587 goto buffer_pos_reached;
8588 }
8589 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8590 && IT_CHARPOS (*it) > to_charpos)
8591 goto buffer_pos_reached;
8592 else
8593 result = MOVE_NEWLINE_OR_CR;
8594 }
8595 else
8596 result = MOVE_NEWLINE_OR_CR;
8597 break;
8598 }
8599
8600 prev_method = it->method;
8601 if (it->method == GET_FROM_BUFFER)
8602 prev_pos = IT_CHARPOS (*it);
8603 /* The current display element has been consumed. Advance
8604 to the next. */
8605 set_iterator_to_next (it, 1);
8606 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8607 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8608 if (IT_CHARPOS (*it) < to_charpos)
8609 saw_smaller_pos = 1;
8610 if (it->bidi_p
8611 && (op & MOVE_TO_POS)
8612 && IT_CHARPOS (*it) >= to_charpos
8613 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8614 SAVE_IT (ppos_it, *it, ppos_data);
8615
8616 /* Stop if lines are truncated and IT's current x-position is
8617 past the right edge of the window now. */
8618 if (it->line_wrap == TRUNCATE
8619 && it->current_x >= it->last_visible_x)
8620 {
8621 if (!FRAME_WINDOW_P (it->f)
8622 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8623 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8624 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8625 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8626 {
8627 int at_eob_p = 0;
8628
8629 if ((at_eob_p = !get_next_display_element (it))
8630 || BUFFER_POS_REACHED_P ()
8631 /* If we are past TO_CHARPOS, but never saw any
8632 character positions smaller than TO_CHARPOS,
8633 return MOVE_POS_MATCH_OR_ZV, like the
8634 unidirectional display did. */
8635 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8636 && !saw_smaller_pos
8637 && IT_CHARPOS (*it) > to_charpos))
8638 {
8639 if (it->bidi_p
8640 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8641 RESTORE_IT (it, &ppos_it, ppos_data);
8642 result = MOVE_POS_MATCH_OR_ZV;
8643 break;
8644 }
8645 if (ITERATOR_AT_END_OF_LINE_P (it))
8646 {
8647 result = MOVE_NEWLINE_OR_CR;
8648 break;
8649 }
8650 }
8651 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8652 && !saw_smaller_pos
8653 && IT_CHARPOS (*it) > to_charpos)
8654 {
8655 if (IT_CHARPOS (ppos_it) < ZV)
8656 RESTORE_IT (it, &ppos_it, ppos_data);
8657 result = MOVE_POS_MATCH_OR_ZV;
8658 break;
8659 }
8660 result = MOVE_LINE_TRUNCATED;
8661 break;
8662 }
8663 #undef IT_RESET_X_ASCENT_DESCENT
8664 }
8665
8666 #undef BUFFER_POS_REACHED_P
8667
8668 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8669 restore the saved iterator. */
8670 if (atpos_it.sp >= 0)
8671 RESTORE_IT (it, &atpos_it, atpos_data);
8672 else if (atx_it.sp >= 0)
8673 RESTORE_IT (it, &atx_it, atx_data);
8674
8675 done:
8676
8677 if (atpos_data)
8678 bidi_unshelve_cache (atpos_data, 1);
8679 if (atx_data)
8680 bidi_unshelve_cache (atx_data, 1);
8681 if (wrap_data)
8682 bidi_unshelve_cache (wrap_data, 1);
8683 if (ppos_data)
8684 bidi_unshelve_cache (ppos_data, 1);
8685
8686 /* Restore the iterator settings altered at the beginning of this
8687 function. */
8688 it->glyph_row = saved_glyph_row;
8689 return result;
8690 }
8691
8692 /* For external use. */
8693 void
8694 move_it_in_display_line (struct it *it,
8695 ptrdiff_t to_charpos, int to_x,
8696 enum move_operation_enum op)
8697 {
8698 if (it->line_wrap == WORD_WRAP
8699 && (op & MOVE_TO_X))
8700 {
8701 struct it save_it;
8702 void *save_data = NULL;
8703 int skip;
8704
8705 SAVE_IT (save_it, *it, save_data);
8706 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8707 /* When word-wrap is on, TO_X may lie past the end
8708 of a wrapped line. Then it->current is the
8709 character on the next line, so backtrack to the
8710 space before the wrap point. */
8711 if (skip == MOVE_LINE_CONTINUED)
8712 {
8713 int prev_x = max (it->current_x - 1, 0);
8714 RESTORE_IT (it, &save_it, save_data);
8715 move_it_in_display_line_to
8716 (it, -1, prev_x, MOVE_TO_X);
8717 }
8718 else
8719 bidi_unshelve_cache (save_data, 1);
8720 }
8721 else
8722 move_it_in_display_line_to (it, to_charpos, to_x, op);
8723 }
8724
8725
8726 /* Move IT forward until it satisfies one or more of the criteria in
8727 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8728
8729 OP is a bit-mask that specifies where to stop, and in particular,
8730 which of those four position arguments makes a difference. See the
8731 description of enum move_operation_enum.
8732
8733 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8734 screen line, this function will set IT to the next position that is
8735 displayed to the right of TO_CHARPOS on the screen. */
8736
8737 void
8738 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8739 {
8740 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8741 int line_height, line_start_x = 0, reached = 0;
8742 void *backup_data = NULL;
8743
8744 for (;;)
8745 {
8746 if (op & MOVE_TO_VPOS)
8747 {
8748 /* If no TO_CHARPOS and no TO_X specified, stop at the
8749 start of the line TO_VPOS. */
8750 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8751 {
8752 if (it->vpos == to_vpos)
8753 {
8754 reached = 1;
8755 break;
8756 }
8757 else
8758 skip = move_it_in_display_line_to (it, -1, -1, 0);
8759 }
8760 else
8761 {
8762 /* TO_VPOS >= 0 means stop at TO_X in the line at
8763 TO_VPOS, or at TO_POS, whichever comes first. */
8764 if (it->vpos == to_vpos)
8765 {
8766 reached = 2;
8767 break;
8768 }
8769
8770 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8771
8772 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8773 {
8774 reached = 3;
8775 break;
8776 }
8777 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8778 {
8779 /* We have reached TO_X but not in the line we want. */
8780 skip = move_it_in_display_line_to (it, to_charpos,
8781 -1, MOVE_TO_POS);
8782 if (skip == MOVE_POS_MATCH_OR_ZV)
8783 {
8784 reached = 4;
8785 break;
8786 }
8787 }
8788 }
8789 }
8790 else if (op & MOVE_TO_Y)
8791 {
8792 struct it it_backup;
8793
8794 if (it->line_wrap == WORD_WRAP)
8795 SAVE_IT (it_backup, *it, backup_data);
8796
8797 /* TO_Y specified means stop at TO_X in the line containing
8798 TO_Y---or at TO_CHARPOS if this is reached first. The
8799 problem is that we can't really tell whether the line
8800 contains TO_Y before we have completely scanned it, and
8801 this may skip past TO_X. What we do is to first scan to
8802 TO_X.
8803
8804 If TO_X is not specified, use a TO_X of zero. The reason
8805 is to make the outcome of this function more predictable.
8806 If we didn't use TO_X == 0, we would stop at the end of
8807 the line which is probably not what a caller would expect
8808 to happen. */
8809 skip = move_it_in_display_line_to
8810 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8811 (MOVE_TO_X | (op & MOVE_TO_POS)));
8812
8813 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8814 if (skip == MOVE_POS_MATCH_OR_ZV)
8815 reached = 5;
8816 else if (skip == MOVE_X_REACHED)
8817 {
8818 /* If TO_X was reached, we want to know whether TO_Y is
8819 in the line. We know this is the case if the already
8820 scanned glyphs make the line tall enough. Otherwise,
8821 we must check by scanning the rest of the line. */
8822 line_height = it->max_ascent + it->max_descent;
8823 if (to_y >= it->current_y
8824 && to_y < it->current_y + line_height)
8825 {
8826 reached = 6;
8827 break;
8828 }
8829 SAVE_IT (it_backup, *it, backup_data);
8830 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8831 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8832 op & MOVE_TO_POS);
8833 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8834 line_height = it->max_ascent + it->max_descent;
8835 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8836
8837 if (to_y >= it->current_y
8838 && to_y < it->current_y + line_height)
8839 {
8840 /* If TO_Y is in this line and TO_X was reached
8841 above, we scanned too far. We have to restore
8842 IT's settings to the ones before skipping. But
8843 keep the more accurate values of max_ascent and
8844 max_descent we've found while skipping the rest
8845 of the line, for the sake of callers, such as
8846 pos_visible_p, that need to know the line
8847 height. */
8848 int max_ascent = it->max_ascent;
8849 int max_descent = it->max_descent;
8850
8851 RESTORE_IT (it, &it_backup, backup_data);
8852 it->max_ascent = max_ascent;
8853 it->max_descent = max_descent;
8854 reached = 6;
8855 }
8856 else
8857 {
8858 skip = skip2;
8859 if (skip == MOVE_POS_MATCH_OR_ZV)
8860 reached = 7;
8861 }
8862 }
8863 else
8864 {
8865 /* Check whether TO_Y is in this line. */
8866 line_height = it->max_ascent + it->max_descent;
8867 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8868
8869 if (to_y >= it->current_y
8870 && to_y < it->current_y + line_height)
8871 {
8872 /* When word-wrap is on, TO_X may lie past the end
8873 of a wrapped line. Then it->current is the
8874 character on the next line, so backtrack to the
8875 space before the wrap point. */
8876 if (skip == MOVE_LINE_CONTINUED
8877 && it->line_wrap == WORD_WRAP)
8878 {
8879 int prev_x = max (it->current_x - 1, 0);
8880 RESTORE_IT (it, &it_backup, backup_data);
8881 skip = move_it_in_display_line_to
8882 (it, -1, prev_x, MOVE_TO_X);
8883 }
8884 reached = 6;
8885 }
8886 }
8887
8888 if (reached)
8889 break;
8890 }
8891 else if (BUFFERP (it->object)
8892 && (it->method == GET_FROM_BUFFER
8893 || it->method == GET_FROM_STRETCH)
8894 && IT_CHARPOS (*it) >= to_charpos
8895 /* Under bidi iteration, a call to set_iterator_to_next
8896 can scan far beyond to_charpos if the initial
8897 portion of the next line needs to be reordered. In
8898 that case, give move_it_in_display_line_to another
8899 chance below. */
8900 && !(it->bidi_p
8901 && it->bidi_it.scan_dir == -1))
8902 skip = MOVE_POS_MATCH_OR_ZV;
8903 else
8904 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8905
8906 switch (skip)
8907 {
8908 case MOVE_POS_MATCH_OR_ZV:
8909 reached = 8;
8910 goto out;
8911
8912 case MOVE_NEWLINE_OR_CR:
8913 set_iterator_to_next (it, 1);
8914 it->continuation_lines_width = 0;
8915 break;
8916
8917 case MOVE_LINE_TRUNCATED:
8918 it->continuation_lines_width = 0;
8919 reseat_at_next_visible_line_start (it, 0);
8920 if ((op & MOVE_TO_POS) != 0
8921 && IT_CHARPOS (*it) > to_charpos)
8922 {
8923 reached = 9;
8924 goto out;
8925 }
8926 break;
8927
8928 case MOVE_LINE_CONTINUED:
8929 /* For continued lines ending in a tab, some of the glyphs
8930 associated with the tab are displayed on the current
8931 line. Since it->current_x does not include these glyphs,
8932 we use it->last_visible_x instead. */
8933 if (it->c == '\t')
8934 {
8935 it->continuation_lines_width += it->last_visible_x;
8936 /* When moving by vpos, ensure that the iterator really
8937 advances to the next line (bug#847, bug#969). Fixme:
8938 do we need to do this in other circumstances? */
8939 if (it->current_x != it->last_visible_x
8940 && (op & MOVE_TO_VPOS)
8941 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8942 {
8943 line_start_x = it->current_x + it->pixel_width
8944 - it->last_visible_x;
8945 set_iterator_to_next (it, 0);
8946 }
8947 }
8948 else
8949 it->continuation_lines_width += it->current_x;
8950 break;
8951
8952 default:
8953 emacs_abort ();
8954 }
8955
8956 /* Reset/increment for the next run. */
8957 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8958 it->current_x = line_start_x;
8959 line_start_x = 0;
8960 it->hpos = 0;
8961 it->current_y += it->max_ascent + it->max_descent;
8962 ++it->vpos;
8963 last_height = it->max_ascent + it->max_descent;
8964 it->max_ascent = it->max_descent = 0;
8965 }
8966
8967 out:
8968
8969 /* On text terminals, we may stop at the end of a line in the middle
8970 of a multi-character glyph. If the glyph itself is continued,
8971 i.e. it is actually displayed on the next line, don't treat this
8972 stopping point as valid; move to the next line instead (unless
8973 that brings us offscreen). */
8974 if (!FRAME_WINDOW_P (it->f)
8975 && op & MOVE_TO_POS
8976 && IT_CHARPOS (*it) == to_charpos
8977 && it->what == IT_CHARACTER
8978 && it->nglyphs > 1
8979 && it->line_wrap == WINDOW_WRAP
8980 && it->current_x == it->last_visible_x - 1
8981 && it->c != '\n'
8982 && it->c != '\t'
8983 && it->vpos < XFASTINT (it->w->window_end_vpos))
8984 {
8985 it->continuation_lines_width += it->current_x;
8986 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8987 it->current_y += it->max_ascent + it->max_descent;
8988 ++it->vpos;
8989 last_height = it->max_ascent + it->max_descent;
8990 }
8991
8992 if (backup_data)
8993 bidi_unshelve_cache (backup_data, 1);
8994
8995 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
8996 }
8997
8998
8999 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9000
9001 If DY > 0, move IT backward at least that many pixels. DY = 0
9002 means move IT backward to the preceding line start or BEGV. This
9003 function may move over more than DY pixels if IT->current_y - DY
9004 ends up in the middle of a line; in this case IT->current_y will be
9005 set to the top of the line moved to. */
9006
9007 void
9008 move_it_vertically_backward (struct it *it, int dy)
9009 {
9010 int nlines, h;
9011 struct it it2, it3;
9012 void *it2data = NULL, *it3data = NULL;
9013 ptrdiff_t start_pos;
9014 int nchars_per_row
9015 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9016 ptrdiff_t pos_limit;
9017
9018 move_further_back:
9019 eassert (dy >= 0);
9020
9021 start_pos = IT_CHARPOS (*it);
9022
9023 /* Estimate how many newlines we must move back. */
9024 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
9025 if (it->line_wrap == TRUNCATE)
9026 pos_limit = BEGV;
9027 else
9028 pos_limit = max (start_pos - nlines * nchars_per_row, BEGV);
9029
9030 /* Set the iterator's position that many lines back. But don't go
9031 back more than NLINES full screen lines -- this wins a day with
9032 buffers which have very long lines. */
9033 while (nlines-- && IT_CHARPOS (*it) > pos_limit)
9034 back_to_previous_visible_line_start (it);
9035
9036 /* Reseat the iterator here. When moving backward, we don't want
9037 reseat to skip forward over invisible text, set up the iterator
9038 to deliver from overlay strings at the new position etc. So,
9039 use reseat_1 here. */
9040 reseat_1 (it, it->current.pos, 1);
9041
9042 /* We are now surely at a line start. */
9043 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9044 reordering is in effect. */
9045 it->continuation_lines_width = 0;
9046
9047 /* Move forward and see what y-distance we moved. First move to the
9048 start of the next line so that we get its height. We need this
9049 height to be able to tell whether we reached the specified
9050 y-distance. */
9051 SAVE_IT (it2, *it, it2data);
9052 it2.max_ascent = it2.max_descent = 0;
9053 do
9054 {
9055 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9056 MOVE_TO_POS | MOVE_TO_VPOS);
9057 }
9058 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9059 /* If we are in a display string which starts at START_POS,
9060 and that display string includes a newline, and we are
9061 right after that newline (i.e. at the beginning of a
9062 display line), exit the loop, because otherwise we will
9063 infloop, since move_it_to will see that it is already at
9064 START_POS and will not move. */
9065 || (it2.method == GET_FROM_STRING
9066 && IT_CHARPOS (it2) == start_pos
9067 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9068 eassert (IT_CHARPOS (*it) >= BEGV);
9069 SAVE_IT (it3, it2, it3data);
9070
9071 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9072 eassert (IT_CHARPOS (*it) >= BEGV);
9073 /* H is the actual vertical distance from the position in *IT
9074 and the starting position. */
9075 h = it2.current_y - it->current_y;
9076 /* NLINES is the distance in number of lines. */
9077 nlines = it2.vpos - it->vpos;
9078
9079 /* Correct IT's y and vpos position
9080 so that they are relative to the starting point. */
9081 it->vpos -= nlines;
9082 it->current_y -= h;
9083
9084 if (dy == 0)
9085 {
9086 /* DY == 0 means move to the start of the screen line. The
9087 value of nlines is > 0 if continuation lines were involved,
9088 or if the original IT position was at start of a line. */
9089 RESTORE_IT (it, it, it2data);
9090 if (nlines > 0)
9091 move_it_by_lines (it, nlines);
9092 /* The above code moves us to some position NLINES down,
9093 usually to its first glyph (leftmost in an L2R line), but
9094 that's not necessarily the start of the line, under bidi
9095 reordering. We want to get to the character position
9096 that is immediately after the newline of the previous
9097 line. */
9098 if (it->bidi_p
9099 && !it->continuation_lines_width
9100 && !STRINGP (it->string)
9101 && IT_CHARPOS (*it) > BEGV
9102 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9103 {
9104 ptrdiff_t cp = IT_CHARPOS (*it), bp = IT_BYTEPOS (*it);
9105
9106 DEC_BOTH (cp, bp);
9107 cp = find_newline_no_quit (cp, bp, -1, NULL);
9108 move_it_to (it, cp, -1, -1, -1, MOVE_TO_POS);
9109 }
9110 bidi_unshelve_cache (it3data, 1);
9111 }
9112 else
9113 {
9114 /* The y-position we try to reach, relative to *IT.
9115 Note that H has been subtracted in front of the if-statement. */
9116 int target_y = it->current_y + h - dy;
9117 int y0 = it3.current_y;
9118 int y1;
9119 int line_height;
9120
9121 RESTORE_IT (&it3, &it3, it3data);
9122 y1 = line_bottom_y (&it3);
9123 line_height = y1 - y0;
9124 RESTORE_IT (it, it, it2data);
9125 /* If we did not reach target_y, try to move further backward if
9126 we can. If we moved too far backward, try to move forward. */
9127 if (target_y < it->current_y
9128 /* This is heuristic. In a window that's 3 lines high, with
9129 a line height of 13 pixels each, recentering with point
9130 on the bottom line will try to move -39/2 = 19 pixels
9131 backward. Try to avoid moving into the first line. */
9132 && (it->current_y - target_y
9133 > min (window_box_height (it->w), line_height * 2 / 3))
9134 && IT_CHARPOS (*it) > BEGV)
9135 {
9136 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9137 target_y - it->current_y));
9138 dy = it->current_y - target_y;
9139 goto move_further_back;
9140 }
9141 else if (target_y >= it->current_y + line_height
9142 && IT_CHARPOS (*it) < ZV)
9143 {
9144 /* Should move forward by at least one line, maybe more.
9145
9146 Note: Calling move_it_by_lines can be expensive on
9147 terminal frames, where compute_motion is used (via
9148 vmotion) to do the job, when there are very long lines
9149 and truncate-lines is nil. That's the reason for
9150 treating terminal frames specially here. */
9151
9152 if (!FRAME_WINDOW_P (it->f))
9153 move_it_vertically (it, target_y - (it->current_y + line_height));
9154 else
9155 {
9156 do
9157 {
9158 move_it_by_lines (it, 1);
9159 }
9160 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9161 }
9162 }
9163 }
9164 }
9165
9166
9167 /* Move IT by a specified amount of pixel lines DY. DY negative means
9168 move backwards. DY = 0 means move to start of screen line. At the
9169 end, IT will be on the start of a screen line. */
9170
9171 void
9172 move_it_vertically (struct it *it, int dy)
9173 {
9174 if (dy <= 0)
9175 move_it_vertically_backward (it, -dy);
9176 else
9177 {
9178 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9179 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9180 MOVE_TO_POS | MOVE_TO_Y);
9181 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9182
9183 /* If buffer ends in ZV without a newline, move to the start of
9184 the line to satisfy the post-condition. */
9185 if (IT_CHARPOS (*it) == ZV
9186 && ZV > BEGV
9187 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9188 move_it_by_lines (it, 0);
9189 }
9190 }
9191
9192
9193 /* Move iterator IT past the end of the text line it is in. */
9194
9195 void
9196 move_it_past_eol (struct it *it)
9197 {
9198 enum move_it_result rc;
9199
9200 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9201 if (rc == MOVE_NEWLINE_OR_CR)
9202 set_iterator_to_next (it, 0);
9203 }
9204
9205
9206 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9207 negative means move up. DVPOS == 0 means move to the start of the
9208 screen line.
9209
9210 Optimization idea: If we would know that IT->f doesn't use
9211 a face with proportional font, we could be faster for
9212 truncate-lines nil. */
9213
9214 void
9215 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9216 {
9217
9218 /* The commented-out optimization uses vmotion on terminals. This
9219 gives bad results, because elements like it->what, on which
9220 callers such as pos_visible_p rely, aren't updated. */
9221 /* struct position pos;
9222 if (!FRAME_WINDOW_P (it->f))
9223 {
9224 struct text_pos textpos;
9225
9226 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9227 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9228 reseat (it, textpos, 1);
9229 it->vpos += pos.vpos;
9230 it->current_y += pos.vpos;
9231 }
9232 else */
9233
9234 if (dvpos == 0)
9235 {
9236 /* DVPOS == 0 means move to the start of the screen line. */
9237 move_it_vertically_backward (it, 0);
9238 /* Let next call to line_bottom_y calculate real line height */
9239 last_height = 0;
9240 }
9241 else if (dvpos > 0)
9242 {
9243 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9244 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9245 {
9246 /* Only move to the next buffer position if we ended up in a
9247 string from display property, not in an overlay string
9248 (before-string or after-string). That is because the
9249 latter don't conceal the underlying buffer position, so
9250 we can ask to move the iterator to the exact position we
9251 are interested in. Note that, even if we are already at
9252 IT_CHARPOS (*it), the call below is not a no-op, as it
9253 will detect that we are at the end of the string, pop the
9254 iterator, and compute it->current_x and it->hpos
9255 correctly. */
9256 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9257 -1, -1, -1, MOVE_TO_POS);
9258 }
9259 }
9260 else
9261 {
9262 struct it it2;
9263 void *it2data = NULL;
9264 ptrdiff_t start_charpos, i;
9265 int nchars_per_row
9266 = (it->last_visible_x - it->first_visible_x) / FRAME_COLUMN_WIDTH (it->f);
9267 ptrdiff_t pos_limit;
9268
9269 /* Start at the beginning of the screen line containing IT's
9270 position. This may actually move vertically backwards,
9271 in case of overlays, so adjust dvpos accordingly. */
9272 dvpos += it->vpos;
9273 move_it_vertically_backward (it, 0);
9274 dvpos -= it->vpos;
9275
9276 /* Go back -DVPOS buffer lines, but no farther than -DVPOS full
9277 screen lines, and reseat the iterator there. */
9278 start_charpos = IT_CHARPOS (*it);
9279 if (it->line_wrap == TRUNCATE)
9280 pos_limit = BEGV;
9281 else
9282 pos_limit = max (start_charpos + dvpos * nchars_per_row, BEGV);
9283 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > pos_limit; --i)
9284 back_to_previous_visible_line_start (it);
9285 reseat (it, it->current.pos, 1);
9286
9287 /* Move further back if we end up in a string or an image. */
9288 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9289 {
9290 /* First try to move to start of display line. */
9291 dvpos += it->vpos;
9292 move_it_vertically_backward (it, 0);
9293 dvpos -= it->vpos;
9294 if (IT_POS_VALID_AFTER_MOVE_P (it))
9295 break;
9296 /* If start of line is still in string or image,
9297 move further back. */
9298 back_to_previous_visible_line_start (it);
9299 reseat (it, it->current.pos, 1);
9300 dvpos--;
9301 }
9302
9303 it->current_x = it->hpos = 0;
9304
9305 /* Above call may have moved too far if continuation lines
9306 are involved. Scan forward and see if it did. */
9307 SAVE_IT (it2, *it, it2data);
9308 it2.vpos = it2.current_y = 0;
9309 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9310 it->vpos -= it2.vpos;
9311 it->current_y -= it2.current_y;
9312 it->current_x = it->hpos = 0;
9313
9314 /* If we moved too far back, move IT some lines forward. */
9315 if (it2.vpos > -dvpos)
9316 {
9317 int delta = it2.vpos + dvpos;
9318
9319 RESTORE_IT (&it2, &it2, it2data);
9320 SAVE_IT (it2, *it, it2data);
9321 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9322 /* Move back again if we got too far ahead. */
9323 if (IT_CHARPOS (*it) >= start_charpos)
9324 RESTORE_IT (it, &it2, it2data);
9325 else
9326 bidi_unshelve_cache (it2data, 1);
9327 }
9328 else
9329 RESTORE_IT (it, it, it2data);
9330 }
9331 }
9332
9333 /* Return 1 if IT points into the middle of a display vector. */
9334
9335 int
9336 in_display_vector_p (struct it *it)
9337 {
9338 return (it->method == GET_FROM_DISPLAY_VECTOR
9339 && it->current.dpvec_index > 0
9340 && it->dpvec + it->current.dpvec_index != it->dpend);
9341 }
9342
9343 \f
9344 /***********************************************************************
9345 Messages
9346 ***********************************************************************/
9347
9348
9349 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9350 to *Messages*. */
9351
9352 void
9353 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9354 {
9355 Lisp_Object args[3];
9356 Lisp_Object msg, fmt;
9357 char *buffer;
9358 ptrdiff_t len;
9359 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9360 USE_SAFE_ALLOCA;
9361
9362 fmt = msg = Qnil;
9363 GCPRO4 (fmt, msg, arg1, arg2);
9364
9365 args[0] = fmt = build_string (format);
9366 args[1] = arg1;
9367 args[2] = arg2;
9368 msg = Fformat (3, args);
9369
9370 len = SBYTES (msg) + 1;
9371 buffer = SAFE_ALLOCA (len);
9372 memcpy (buffer, SDATA (msg), len);
9373
9374 message_dolog (buffer, len - 1, 1, 0);
9375 SAFE_FREE ();
9376
9377 UNGCPRO;
9378 }
9379
9380
9381 /* Output a newline in the *Messages* buffer if "needs" one. */
9382
9383 void
9384 message_log_maybe_newline (void)
9385 {
9386 if (message_log_need_newline)
9387 message_dolog ("", 0, 1, 0);
9388 }
9389
9390
9391 /* Add a string M of length NBYTES to the message log, optionally
9392 terminated with a newline when NLFLAG is true. MULTIBYTE, if
9393 true, means interpret the contents of M as multibyte. This
9394 function calls low-level routines in order to bypass text property
9395 hooks, etc. which might not be safe to run.
9396
9397 This may GC (insert may run before/after change hooks),
9398 so the buffer M must NOT point to a Lisp string. */
9399
9400 void
9401 message_dolog (const char *m, ptrdiff_t nbytes, bool nlflag, bool multibyte)
9402 {
9403 const unsigned char *msg = (const unsigned char *) m;
9404
9405 if (!NILP (Vmemory_full))
9406 return;
9407
9408 if (!NILP (Vmessage_log_max))
9409 {
9410 struct buffer *oldbuf;
9411 Lisp_Object oldpoint, oldbegv, oldzv;
9412 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9413 ptrdiff_t point_at_end = 0;
9414 ptrdiff_t zv_at_end = 0;
9415 Lisp_Object old_deactivate_mark;
9416 bool shown;
9417 struct gcpro gcpro1;
9418
9419 old_deactivate_mark = Vdeactivate_mark;
9420 oldbuf = current_buffer;
9421 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9422 bset_undo_list (current_buffer, Qt);
9423
9424 oldpoint = message_dolog_marker1;
9425 set_marker_restricted_both (oldpoint, Qnil, PT, PT_BYTE);
9426 oldbegv = message_dolog_marker2;
9427 set_marker_restricted_both (oldbegv, Qnil, BEGV, BEGV_BYTE);
9428 oldzv = message_dolog_marker3;
9429 set_marker_restricted_both (oldzv, Qnil, ZV, ZV_BYTE);
9430 GCPRO1 (old_deactivate_mark);
9431
9432 if (PT == Z)
9433 point_at_end = 1;
9434 if (ZV == Z)
9435 zv_at_end = 1;
9436
9437 BEGV = BEG;
9438 BEGV_BYTE = BEG_BYTE;
9439 ZV = Z;
9440 ZV_BYTE = Z_BYTE;
9441 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9442
9443 /* Insert the string--maybe converting multibyte to single byte
9444 or vice versa, so that all the text fits the buffer. */
9445 if (multibyte
9446 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9447 {
9448 ptrdiff_t i;
9449 int c, char_bytes;
9450 char work[1];
9451
9452 /* Convert a multibyte string to single-byte
9453 for the *Message* buffer. */
9454 for (i = 0; i < nbytes; i += char_bytes)
9455 {
9456 c = string_char_and_length (msg + i, &char_bytes);
9457 work[0] = (ASCII_CHAR_P (c)
9458 ? c
9459 : multibyte_char_to_unibyte (c));
9460 insert_1_both (work, 1, 1, 1, 0, 0);
9461 }
9462 }
9463 else if (! multibyte
9464 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9465 {
9466 ptrdiff_t i;
9467 int c, char_bytes;
9468 unsigned char str[MAX_MULTIBYTE_LENGTH];
9469 /* Convert a single-byte string to multibyte
9470 for the *Message* buffer. */
9471 for (i = 0; i < nbytes; i++)
9472 {
9473 c = msg[i];
9474 MAKE_CHAR_MULTIBYTE (c);
9475 char_bytes = CHAR_STRING (c, str);
9476 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9477 }
9478 }
9479 else if (nbytes)
9480 insert_1_both (m, chars_in_text (msg, nbytes), nbytes, 1, 0, 0);
9481
9482 if (nlflag)
9483 {
9484 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9485 printmax_t dups;
9486
9487 insert_1_both ("\n", 1, 1, 1, 0, 0);
9488
9489 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9490 this_bol = PT;
9491 this_bol_byte = PT_BYTE;
9492
9493 /* See if this line duplicates the previous one.
9494 If so, combine duplicates. */
9495 if (this_bol > BEG)
9496 {
9497 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9498 prev_bol = PT;
9499 prev_bol_byte = PT_BYTE;
9500
9501 dups = message_log_check_duplicate (prev_bol_byte,
9502 this_bol_byte);
9503 if (dups)
9504 {
9505 del_range_both (prev_bol, prev_bol_byte,
9506 this_bol, this_bol_byte, 0);
9507 if (dups > 1)
9508 {
9509 char dupstr[sizeof " [ times]"
9510 + INT_STRLEN_BOUND (printmax_t)];
9511
9512 /* If you change this format, don't forget to also
9513 change message_log_check_duplicate. */
9514 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9515 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9516 insert_1_both (dupstr, duplen, duplen, 1, 0, 1);
9517 }
9518 }
9519 }
9520
9521 /* If we have more than the desired maximum number of lines
9522 in the *Messages* buffer now, delete the oldest ones.
9523 This is safe because we don't have undo in this buffer. */
9524
9525 if (NATNUMP (Vmessage_log_max))
9526 {
9527 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9528 -XFASTINT (Vmessage_log_max) - 1, 0);
9529 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9530 }
9531 }
9532 BEGV = marker_position (oldbegv);
9533 BEGV_BYTE = marker_byte_position (oldbegv);
9534
9535 if (zv_at_end)
9536 {
9537 ZV = Z;
9538 ZV_BYTE = Z_BYTE;
9539 }
9540 else
9541 {
9542 ZV = marker_position (oldzv);
9543 ZV_BYTE = marker_byte_position (oldzv);
9544 }
9545
9546 if (point_at_end)
9547 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9548 else
9549 /* We can't do Fgoto_char (oldpoint) because it will run some
9550 Lisp code. */
9551 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9552 marker_byte_position (oldpoint));
9553
9554 UNGCPRO;
9555 unchain_marker (XMARKER (oldpoint));
9556 unchain_marker (XMARKER (oldbegv));
9557 unchain_marker (XMARKER (oldzv));
9558
9559 shown = buffer_window_count (current_buffer) > 0;
9560 set_buffer_internal (oldbuf);
9561 /* We called insert_1_both above with its 5th argument (PREPARE)
9562 zero, which prevents insert_1_both from calling
9563 prepare_to_modify_buffer, which in turns prevents us from
9564 incrementing windows_or_buffers_changed even if *Messages* is
9565 shown in some window. So we must manually incrementing
9566 windows_or_buffers_changed here to make up for that. */
9567 if (shown)
9568 windows_or_buffers_changed++;
9569 else
9570 windows_or_buffers_changed = old_windows_or_buffers_changed;
9571 message_log_need_newline = !nlflag;
9572 Vdeactivate_mark = old_deactivate_mark;
9573 }
9574 }
9575
9576
9577 /* We are at the end of the buffer after just having inserted a newline.
9578 (Note: We depend on the fact we won't be crossing the gap.)
9579 Check to see if the most recent message looks a lot like the previous one.
9580 Return 0 if different, 1 if the new one should just replace it, or a
9581 value N > 1 if we should also append " [N times]". */
9582
9583 static intmax_t
9584 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9585 {
9586 ptrdiff_t i;
9587 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9588 int seen_dots = 0;
9589 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9590 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9591
9592 for (i = 0; i < len; i++)
9593 {
9594 if (i >= 3 && p1[i - 3] == '.' && p1[i - 2] == '.' && p1[i - 1] == '.')
9595 seen_dots = 1;
9596 if (p1[i] != p2[i])
9597 return seen_dots;
9598 }
9599 p1 += len;
9600 if (*p1 == '\n')
9601 return 2;
9602 if (*p1++ == ' ' && *p1++ == '[')
9603 {
9604 char *pend;
9605 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9606 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9607 return n + 1;
9608 }
9609 return 0;
9610 }
9611 \f
9612
9613 /* Display an echo area message M with a specified length of NBYTES
9614 bytes. The string may include null characters. If M is not a
9615 string, clear out any existing message, and let the mini-buffer
9616 text show through.
9617
9618 This function cancels echoing. */
9619
9620 void
9621 message3 (Lisp_Object m)
9622 {
9623 struct gcpro gcpro1;
9624
9625 GCPRO1 (m);
9626 clear_message (1,1);
9627 cancel_echoing ();
9628
9629 /* First flush out any partial line written with print. */
9630 message_log_maybe_newline ();
9631 if (STRINGP (m))
9632 {
9633 ptrdiff_t nbytes = SBYTES (m);
9634 bool multibyte = STRING_MULTIBYTE (m);
9635 USE_SAFE_ALLOCA;
9636 char *buffer = SAFE_ALLOCA (nbytes);
9637 memcpy (buffer, SDATA (m), nbytes);
9638 message_dolog (buffer, nbytes, 1, multibyte);
9639 SAFE_FREE ();
9640 }
9641 message3_nolog (m);
9642
9643 UNGCPRO;
9644 }
9645
9646
9647 /* The non-logging version of message3.
9648 This does not cancel echoing, because it is used for echoing.
9649 Perhaps we need to make a separate function for echoing
9650 and make this cancel echoing. */
9651
9652 void
9653 message3_nolog (Lisp_Object m)
9654 {
9655 struct frame *sf = SELECTED_FRAME ();
9656
9657 if (FRAME_INITIAL_P (sf))
9658 {
9659 if (noninteractive_need_newline)
9660 putc ('\n', stderr);
9661 noninteractive_need_newline = 0;
9662 if (STRINGP (m))
9663 fwrite (SDATA (m), SBYTES (m), 1, stderr);
9664 if (cursor_in_echo_area == 0)
9665 fprintf (stderr, "\n");
9666 fflush (stderr);
9667 }
9668 /* Error messages get reported properly by cmd_error, so this must be just an
9669 informative message; if the frame hasn't really been initialized yet, just
9670 toss it. */
9671 else if (INTERACTIVE && sf->glyphs_initialized_p)
9672 {
9673 /* Get the frame containing the mini-buffer
9674 that the selected frame is using. */
9675 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
9676 Lisp_Object frame = XWINDOW (mini_window)->frame;
9677 struct frame *f = XFRAME (frame);
9678
9679 if (FRAME_VISIBLE_P (sf) && !FRAME_VISIBLE_P (f))
9680 Fmake_frame_visible (frame);
9681
9682 if (STRINGP (m) && SCHARS (m) > 0)
9683 {
9684 set_message (m);
9685 if (minibuffer_auto_raise)
9686 Fraise_frame (frame);
9687 /* Assume we are not echoing.
9688 (If we are, echo_now will override this.) */
9689 echo_message_buffer = Qnil;
9690 }
9691 else
9692 clear_message (1, 1);
9693
9694 do_pending_window_change (0);
9695 echo_area_display (1);
9696 do_pending_window_change (0);
9697 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9698 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9699 }
9700 }
9701
9702
9703 /* Display a null-terminated echo area message M. If M is 0, clear
9704 out any existing message, and let the mini-buffer text show through.
9705
9706 The buffer M must continue to exist until after the echo area gets
9707 cleared or some other message gets displayed there. Do not pass
9708 text that is stored in a Lisp string. Do not pass text in a buffer
9709 that was alloca'd. */
9710
9711 void
9712 message1 (const char *m)
9713 {
9714 message3 (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9715 }
9716
9717
9718 /* The non-logging counterpart of message1. */
9719
9720 void
9721 message1_nolog (const char *m)
9722 {
9723 message3_nolog (m ? make_unibyte_string (m, strlen (m)) : Qnil);
9724 }
9725
9726 /* Display a message M which contains a single %s
9727 which gets replaced with STRING. */
9728
9729 void
9730 message_with_string (const char *m, Lisp_Object string, int log)
9731 {
9732 CHECK_STRING (string);
9733
9734 if (noninteractive)
9735 {
9736 if (m)
9737 {
9738 if (noninteractive_need_newline)
9739 putc ('\n', stderr);
9740 noninteractive_need_newline = 0;
9741 fprintf (stderr, m, SDATA (string));
9742 if (!cursor_in_echo_area)
9743 fprintf (stderr, "\n");
9744 fflush (stderr);
9745 }
9746 }
9747 else if (INTERACTIVE)
9748 {
9749 /* The frame whose minibuffer we're going to display the message on.
9750 It may be larger than the selected frame, so we need
9751 to use its buffer, not the selected frame's buffer. */
9752 Lisp_Object mini_window;
9753 struct frame *f, *sf = SELECTED_FRAME ();
9754
9755 /* Get the frame containing the minibuffer
9756 that the selected frame is using. */
9757 mini_window = FRAME_MINIBUF_WINDOW (sf);
9758 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9759
9760 /* Error messages get reported properly by cmd_error, so this must be
9761 just an informative message; if the frame hasn't really been
9762 initialized yet, just toss it. */
9763 if (f->glyphs_initialized_p)
9764 {
9765 Lisp_Object args[2], msg;
9766 struct gcpro gcpro1, gcpro2;
9767
9768 args[0] = build_string (m);
9769 args[1] = msg = string;
9770 GCPRO2 (args[0], msg);
9771 gcpro1.nvars = 2;
9772
9773 msg = Fformat (2, args);
9774
9775 if (log)
9776 message3 (msg);
9777 else
9778 message3_nolog (msg);
9779
9780 UNGCPRO;
9781
9782 /* Print should start at the beginning of the message
9783 buffer next time. */
9784 message_buf_print = 0;
9785 }
9786 }
9787 }
9788
9789
9790 /* Dump an informative message to the minibuf. If M is 0, clear out
9791 any existing message, and let the mini-buffer text show through. */
9792
9793 static void
9794 vmessage (const char *m, va_list ap)
9795 {
9796 if (noninteractive)
9797 {
9798 if (m)
9799 {
9800 if (noninteractive_need_newline)
9801 putc ('\n', stderr);
9802 noninteractive_need_newline = 0;
9803 vfprintf (stderr, m, ap);
9804 if (cursor_in_echo_area == 0)
9805 fprintf (stderr, "\n");
9806 fflush (stderr);
9807 }
9808 }
9809 else if (INTERACTIVE)
9810 {
9811 /* The frame whose mini-buffer we're going to display the message
9812 on. It may be larger than the selected frame, so we need to
9813 use its buffer, not the selected frame's buffer. */
9814 Lisp_Object mini_window;
9815 struct frame *f, *sf = SELECTED_FRAME ();
9816
9817 /* Get the frame containing the mini-buffer
9818 that the selected frame is using. */
9819 mini_window = FRAME_MINIBUF_WINDOW (sf);
9820 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9821
9822 /* Error messages get reported properly by cmd_error, so this must be
9823 just an informative message; if the frame hasn't really been
9824 initialized yet, just toss it. */
9825 if (f->glyphs_initialized_p)
9826 {
9827 if (m)
9828 {
9829 ptrdiff_t len;
9830 ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
9831 char *message_buf = alloca (maxsize + 1);
9832
9833 len = doprnt (message_buf, maxsize, m, (char *)0, ap);
9834
9835 message3 (make_string (message_buf, len));
9836 }
9837 else
9838 message1 (0);
9839
9840 /* Print should start at the beginning of the message
9841 buffer next time. */
9842 message_buf_print = 0;
9843 }
9844 }
9845 }
9846
9847 void
9848 message (const char *m, ...)
9849 {
9850 va_list ap;
9851 va_start (ap, m);
9852 vmessage (m, ap);
9853 va_end (ap);
9854 }
9855
9856
9857 #if 0
9858 /* The non-logging version of message. */
9859
9860 void
9861 message_nolog (const char *m, ...)
9862 {
9863 Lisp_Object old_log_max;
9864 va_list ap;
9865 va_start (ap, m);
9866 old_log_max = Vmessage_log_max;
9867 Vmessage_log_max = Qnil;
9868 vmessage (m, ap);
9869 Vmessage_log_max = old_log_max;
9870 va_end (ap);
9871 }
9872 #endif
9873
9874
9875 /* Display the current message in the current mini-buffer. This is
9876 only called from error handlers in process.c, and is not time
9877 critical. */
9878
9879 void
9880 update_echo_area (void)
9881 {
9882 if (!NILP (echo_area_buffer[0]))
9883 {
9884 Lisp_Object string;
9885 string = Fcurrent_message ();
9886 message3 (string);
9887 }
9888 }
9889
9890
9891 /* Make sure echo area buffers in `echo_buffers' are live.
9892 If they aren't, make new ones. */
9893
9894 static void
9895 ensure_echo_area_buffers (void)
9896 {
9897 int i;
9898
9899 for (i = 0; i < 2; ++i)
9900 if (!BUFFERP (echo_buffer[i])
9901 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9902 {
9903 char name[30];
9904 Lisp_Object old_buffer;
9905 int j;
9906
9907 old_buffer = echo_buffer[i];
9908 echo_buffer[i] = Fget_buffer_create
9909 (make_formatted_string (name, " *Echo Area %d*", i));
9910 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9911 /* to force word wrap in echo area -
9912 it was decided to postpone this*/
9913 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9914
9915 for (j = 0; j < 2; ++j)
9916 if (EQ (old_buffer, echo_area_buffer[j]))
9917 echo_area_buffer[j] = echo_buffer[i];
9918 }
9919 }
9920
9921
9922 /* Call FN with args A1..A2 with either the current or last displayed
9923 echo_area_buffer as current buffer.
9924
9925 WHICH zero means use the current message buffer
9926 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9927 from echo_buffer[] and clear it.
9928
9929 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9930 suitable buffer from echo_buffer[] and clear it.
9931
9932 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9933 that the current message becomes the last displayed one, make
9934 choose a suitable buffer for echo_area_buffer[0], and clear it.
9935
9936 Value is what FN returns. */
9937
9938 static int
9939 with_echo_area_buffer (struct window *w, int which,
9940 int (*fn) (ptrdiff_t, Lisp_Object),
9941 ptrdiff_t a1, Lisp_Object a2)
9942 {
9943 Lisp_Object buffer;
9944 int this_one, the_other, clear_buffer_p, rc;
9945 ptrdiff_t count = SPECPDL_INDEX ();
9946
9947 /* If buffers aren't live, make new ones. */
9948 ensure_echo_area_buffers ();
9949
9950 clear_buffer_p = 0;
9951
9952 if (which == 0)
9953 this_one = 0, the_other = 1;
9954 else if (which > 0)
9955 this_one = 1, the_other = 0;
9956 else
9957 {
9958 this_one = 0, the_other = 1;
9959 clear_buffer_p = 1;
9960
9961 /* We need a fresh one in case the current echo buffer equals
9962 the one containing the last displayed echo area message. */
9963 if (!NILP (echo_area_buffer[this_one])
9964 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
9965 echo_area_buffer[this_one] = Qnil;
9966 }
9967
9968 /* Choose a suitable buffer from echo_buffer[] is we don't
9969 have one. */
9970 if (NILP (echo_area_buffer[this_one]))
9971 {
9972 echo_area_buffer[this_one]
9973 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
9974 ? echo_buffer[the_other]
9975 : echo_buffer[this_one]);
9976 clear_buffer_p = 1;
9977 }
9978
9979 buffer = echo_area_buffer[this_one];
9980
9981 /* Don't get confused by reusing the buffer used for echoing
9982 for a different purpose. */
9983 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
9984 cancel_echoing ();
9985
9986 record_unwind_protect (unwind_with_echo_area_buffer,
9987 with_echo_area_buffer_unwind_data (w));
9988
9989 /* Make the echo area buffer current. Note that for display
9990 purposes, it is not necessary that the displayed window's buffer
9991 == current_buffer, except for text property lookup. So, let's
9992 only set that buffer temporarily here without doing a full
9993 Fset_window_buffer. We must also change w->pointm, though,
9994 because otherwise an assertions in unshow_buffer fails, and Emacs
9995 aborts. */
9996 set_buffer_internal_1 (XBUFFER (buffer));
9997 if (w)
9998 {
9999 wset_buffer (w, buffer);
10000 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10001 }
10002
10003 bset_undo_list (current_buffer, Qt);
10004 bset_read_only (current_buffer, Qnil);
10005 specbind (Qinhibit_read_only, Qt);
10006 specbind (Qinhibit_modification_hooks, Qt);
10007
10008 if (clear_buffer_p && Z > BEG)
10009 del_range (BEG, Z);
10010
10011 eassert (BEGV >= BEG);
10012 eassert (ZV <= Z && ZV >= BEGV);
10013
10014 rc = fn (a1, a2);
10015
10016 eassert (BEGV >= BEG);
10017 eassert (ZV <= Z && ZV >= BEGV);
10018
10019 unbind_to (count, Qnil);
10020 return rc;
10021 }
10022
10023
10024 /* Save state that should be preserved around the call to the function
10025 FN called in with_echo_area_buffer. */
10026
10027 static Lisp_Object
10028 with_echo_area_buffer_unwind_data (struct window *w)
10029 {
10030 int i = 0;
10031 Lisp_Object vector, tmp;
10032
10033 /* Reduce consing by keeping one vector in
10034 Vwith_echo_area_save_vector. */
10035 vector = Vwith_echo_area_save_vector;
10036 Vwith_echo_area_save_vector = Qnil;
10037
10038 if (NILP (vector))
10039 vector = Fmake_vector (make_number (9), Qnil);
10040
10041 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10042 ASET (vector, i, Vdeactivate_mark); ++i;
10043 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10044
10045 if (w)
10046 {
10047 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10048 ASET (vector, i, w->contents); ++i;
10049 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10050 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10051 ASET (vector, i, make_number (marker_position (w->start))); ++i;
10052 ASET (vector, i, make_number (marker_byte_position (w->start))); ++i;
10053 }
10054 else
10055 {
10056 int end = i + 6;
10057 for (; i < end; ++i)
10058 ASET (vector, i, Qnil);
10059 }
10060
10061 eassert (i == ASIZE (vector));
10062 return vector;
10063 }
10064
10065
10066 /* Restore global state from VECTOR which was created by
10067 with_echo_area_buffer_unwind_data. */
10068
10069 static Lisp_Object
10070 unwind_with_echo_area_buffer (Lisp_Object vector)
10071 {
10072 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10073 Vdeactivate_mark = AREF (vector, 1);
10074 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10075
10076 if (WINDOWP (AREF (vector, 3)))
10077 {
10078 struct window *w;
10079 Lisp_Object buffer;
10080
10081 w = XWINDOW (AREF (vector, 3));
10082 buffer = AREF (vector, 4);
10083
10084 wset_buffer (w, buffer);
10085 set_marker_both (w->pointm, buffer,
10086 XFASTINT (AREF (vector, 5)),
10087 XFASTINT (AREF (vector, 6)));
10088 set_marker_both (w->start, buffer,
10089 XFASTINT (AREF (vector, 7)),
10090 XFASTINT (AREF (vector, 8)));
10091 }
10092
10093 Vwith_echo_area_save_vector = vector;
10094 return Qnil;
10095 }
10096
10097
10098 /* Set up the echo area for use by print functions. MULTIBYTE_P
10099 non-zero means we will print multibyte. */
10100
10101 void
10102 setup_echo_area_for_printing (int multibyte_p)
10103 {
10104 /* If we can't find an echo area any more, exit. */
10105 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10106 Fkill_emacs (Qnil);
10107
10108 ensure_echo_area_buffers ();
10109
10110 if (!message_buf_print)
10111 {
10112 /* A message has been output since the last time we printed.
10113 Choose a fresh echo area buffer. */
10114 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10115 echo_area_buffer[0] = echo_buffer[1];
10116 else
10117 echo_area_buffer[0] = echo_buffer[0];
10118
10119 /* Switch to that buffer and clear it. */
10120 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10121 bset_truncate_lines (current_buffer, Qnil);
10122
10123 if (Z > BEG)
10124 {
10125 ptrdiff_t count = SPECPDL_INDEX ();
10126 specbind (Qinhibit_read_only, Qt);
10127 /* Note that undo recording is always disabled. */
10128 del_range (BEG, Z);
10129 unbind_to (count, Qnil);
10130 }
10131 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10132
10133 /* Set up the buffer for the multibyteness we need. */
10134 if (multibyte_p
10135 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10136 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10137
10138 /* Raise the frame containing the echo area. */
10139 if (minibuffer_auto_raise)
10140 {
10141 struct frame *sf = SELECTED_FRAME ();
10142 Lisp_Object mini_window;
10143 mini_window = FRAME_MINIBUF_WINDOW (sf);
10144 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10145 }
10146
10147 message_log_maybe_newline ();
10148 message_buf_print = 1;
10149 }
10150 else
10151 {
10152 if (NILP (echo_area_buffer[0]))
10153 {
10154 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10155 echo_area_buffer[0] = echo_buffer[1];
10156 else
10157 echo_area_buffer[0] = echo_buffer[0];
10158 }
10159
10160 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10161 {
10162 /* Someone switched buffers between print requests. */
10163 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10164 bset_truncate_lines (current_buffer, Qnil);
10165 }
10166 }
10167 }
10168
10169
10170 /* Display an echo area message in window W. Value is non-zero if W's
10171 height is changed. If display_last_displayed_message_p is
10172 non-zero, display the message that was last displayed, otherwise
10173 display the current message. */
10174
10175 static int
10176 display_echo_area (struct window *w)
10177 {
10178 int i, no_message_p, window_height_changed_p;
10179
10180 /* Temporarily disable garbage collections while displaying the echo
10181 area. This is done because a GC can print a message itself.
10182 That message would modify the echo area buffer's contents while a
10183 redisplay of the buffer is going on, and seriously confuse
10184 redisplay. */
10185 ptrdiff_t count = inhibit_garbage_collection ();
10186
10187 /* If there is no message, we must call display_echo_area_1
10188 nevertheless because it resizes the window. But we will have to
10189 reset the echo_area_buffer in question to nil at the end because
10190 with_echo_area_buffer will sets it to an empty buffer. */
10191 i = display_last_displayed_message_p ? 1 : 0;
10192 no_message_p = NILP (echo_area_buffer[i]);
10193
10194 window_height_changed_p
10195 = with_echo_area_buffer (w, display_last_displayed_message_p,
10196 display_echo_area_1,
10197 (intptr_t) w, Qnil);
10198
10199 if (no_message_p)
10200 echo_area_buffer[i] = Qnil;
10201
10202 unbind_to (count, Qnil);
10203 return window_height_changed_p;
10204 }
10205
10206
10207 /* Helper for display_echo_area. Display the current buffer which
10208 contains the current echo area message in window W, a mini-window,
10209 a pointer to which is passed in A1. A2..A4 are currently not used.
10210 Change the height of W so that all of the message is displayed.
10211 Value is non-zero if height of W was changed. */
10212
10213 static int
10214 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2)
10215 {
10216 intptr_t i1 = a1;
10217 struct window *w = (struct window *) i1;
10218 Lisp_Object window;
10219 struct text_pos start;
10220 int window_height_changed_p = 0;
10221
10222 /* Do this before displaying, so that we have a large enough glyph
10223 matrix for the display. If we can't get enough space for the
10224 whole text, display the last N lines. That works by setting w->start. */
10225 window_height_changed_p = resize_mini_window (w, 0);
10226
10227 /* Use the starting position chosen by resize_mini_window. */
10228 SET_TEXT_POS_FROM_MARKER (start, w->start);
10229
10230 /* Display. */
10231 clear_glyph_matrix (w->desired_matrix);
10232 XSETWINDOW (window, w);
10233 try_window (window, start, 0);
10234
10235 return window_height_changed_p;
10236 }
10237
10238
10239 /* Resize the echo area window to exactly the size needed for the
10240 currently displayed message, if there is one. If a mini-buffer
10241 is active, don't shrink it. */
10242
10243 void
10244 resize_echo_area_exactly (void)
10245 {
10246 if (BUFFERP (echo_area_buffer[0])
10247 && WINDOWP (echo_area_window))
10248 {
10249 struct window *w = XWINDOW (echo_area_window);
10250 int resized_p;
10251 Lisp_Object resize_exactly;
10252
10253 if (minibuf_level == 0)
10254 resize_exactly = Qt;
10255 else
10256 resize_exactly = Qnil;
10257
10258 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10259 (intptr_t) w, resize_exactly);
10260 if (resized_p)
10261 {
10262 ++windows_or_buffers_changed;
10263 ++update_mode_lines;
10264 redisplay_internal ();
10265 }
10266 }
10267 }
10268
10269
10270 /* Callback function for with_echo_area_buffer, when used from
10271 resize_echo_area_exactly. A1 contains a pointer to the window to
10272 resize, EXACTLY non-nil means resize the mini-window exactly to the
10273 size of the text displayed. A3 and A4 are not used. Value is what
10274 resize_mini_window returns. */
10275
10276 static int
10277 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly)
10278 {
10279 intptr_t i1 = a1;
10280 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10281 }
10282
10283
10284 /* Resize mini-window W to fit the size of its contents. EXACT_P
10285 means size the window exactly to the size needed. Otherwise, it's
10286 only enlarged until W's buffer is empty.
10287
10288 Set W->start to the right place to begin display. If the whole
10289 contents fit, start at the beginning. Otherwise, start so as
10290 to make the end of the contents appear. This is particularly
10291 important for y-or-n-p, but seems desirable generally.
10292
10293 Value is non-zero if the window height has been changed. */
10294
10295 int
10296 resize_mini_window (struct window *w, int exact_p)
10297 {
10298 struct frame *f = XFRAME (w->frame);
10299 int window_height_changed_p = 0;
10300
10301 eassert (MINI_WINDOW_P (w));
10302
10303 /* By default, start display at the beginning. */
10304 set_marker_both (w->start, w->contents,
10305 BUF_BEGV (XBUFFER (w->contents)),
10306 BUF_BEGV_BYTE (XBUFFER (w->contents)));
10307
10308 /* Don't resize windows while redisplaying a window; it would
10309 confuse redisplay functions when the size of the window they are
10310 displaying changes from under them. Such a resizing can happen,
10311 for instance, when which-func prints a long message while
10312 we are running fontification-functions. We're running these
10313 functions with safe_call which binds inhibit-redisplay to t. */
10314 if (!NILP (Vinhibit_redisplay))
10315 return 0;
10316
10317 /* Nil means don't try to resize. */
10318 if (NILP (Vresize_mini_windows)
10319 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10320 return 0;
10321
10322 if (!FRAME_MINIBUF_ONLY_P (f))
10323 {
10324 struct it it;
10325 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10326 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10327 int height;
10328 EMACS_INT max_height;
10329 int unit = FRAME_LINE_HEIGHT (f);
10330 struct text_pos start;
10331 struct buffer *old_current_buffer = NULL;
10332
10333 if (current_buffer != XBUFFER (w->contents))
10334 {
10335 old_current_buffer = current_buffer;
10336 set_buffer_internal (XBUFFER (w->contents));
10337 }
10338
10339 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10340
10341 /* Compute the max. number of lines specified by the user. */
10342 if (FLOATP (Vmax_mini_window_height))
10343 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10344 else if (INTEGERP (Vmax_mini_window_height))
10345 max_height = XINT (Vmax_mini_window_height);
10346 else
10347 max_height = total_height / 4;
10348
10349 /* Correct that max. height if it's bogus. */
10350 max_height = clip_to_bounds (1, max_height, total_height);
10351
10352 /* Find out the height of the text in the window. */
10353 if (it.line_wrap == TRUNCATE)
10354 height = 1;
10355 else
10356 {
10357 last_height = 0;
10358 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10359 if (it.max_ascent == 0 && it.max_descent == 0)
10360 height = it.current_y + last_height;
10361 else
10362 height = it.current_y + it.max_ascent + it.max_descent;
10363 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10364 height = (height + unit - 1) / unit;
10365 }
10366
10367 /* Compute a suitable window start. */
10368 if (height > max_height)
10369 {
10370 height = max_height;
10371 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10372 move_it_vertically_backward (&it, (height - 1) * unit);
10373 start = it.current.pos;
10374 }
10375 else
10376 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10377 SET_MARKER_FROM_TEXT_POS (w->start, start);
10378
10379 if (EQ (Vresize_mini_windows, Qgrow_only))
10380 {
10381 /* Let it grow only, until we display an empty message, in which
10382 case the window shrinks again. */
10383 if (height > WINDOW_TOTAL_LINES (w))
10384 {
10385 int old_height = WINDOW_TOTAL_LINES (w);
10386 freeze_window_starts (f, 1);
10387 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10388 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10389 }
10390 else if (height < WINDOW_TOTAL_LINES (w)
10391 && (exact_p || BEGV == ZV))
10392 {
10393 int old_height = WINDOW_TOTAL_LINES (w);
10394 freeze_window_starts (f, 0);
10395 shrink_mini_window (w);
10396 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10397 }
10398 }
10399 else
10400 {
10401 /* Always resize to exact size needed. */
10402 if (height > WINDOW_TOTAL_LINES (w))
10403 {
10404 int old_height = WINDOW_TOTAL_LINES (w);
10405 freeze_window_starts (f, 1);
10406 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10407 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10408 }
10409 else if (height < WINDOW_TOTAL_LINES (w))
10410 {
10411 int old_height = WINDOW_TOTAL_LINES (w);
10412 freeze_window_starts (f, 0);
10413 shrink_mini_window (w);
10414
10415 if (height)
10416 {
10417 freeze_window_starts (f, 1);
10418 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10419 }
10420
10421 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10422 }
10423 }
10424
10425 if (old_current_buffer)
10426 set_buffer_internal (old_current_buffer);
10427 }
10428
10429 return window_height_changed_p;
10430 }
10431
10432
10433 /* Value is the current message, a string, or nil if there is no
10434 current message. */
10435
10436 Lisp_Object
10437 current_message (void)
10438 {
10439 Lisp_Object msg;
10440
10441 if (!BUFFERP (echo_area_buffer[0]))
10442 msg = Qnil;
10443 else
10444 {
10445 with_echo_area_buffer (0, 0, current_message_1,
10446 (intptr_t) &msg, Qnil);
10447 if (NILP (msg))
10448 echo_area_buffer[0] = Qnil;
10449 }
10450
10451 return msg;
10452 }
10453
10454
10455 static int
10456 current_message_1 (ptrdiff_t a1, Lisp_Object a2)
10457 {
10458 intptr_t i1 = a1;
10459 Lisp_Object *msg = (Lisp_Object *) i1;
10460
10461 if (Z > BEG)
10462 *msg = make_buffer_string (BEG, Z, 1);
10463 else
10464 *msg = Qnil;
10465 return 0;
10466 }
10467
10468
10469 /* Push the current message on Vmessage_stack for later restoration
10470 by restore_message. Value is non-zero if the current message isn't
10471 empty. This is a relatively infrequent operation, so it's not
10472 worth optimizing. */
10473
10474 bool
10475 push_message (void)
10476 {
10477 Lisp_Object msg = current_message ();
10478 Vmessage_stack = Fcons (msg, Vmessage_stack);
10479 return STRINGP (msg);
10480 }
10481
10482
10483 /* Restore message display from the top of Vmessage_stack. */
10484
10485 void
10486 restore_message (void)
10487 {
10488 eassert (CONSP (Vmessage_stack));
10489 message3_nolog (XCAR (Vmessage_stack));
10490 }
10491
10492
10493 /* Handler for record_unwind_protect calling pop_message. */
10494
10495 Lisp_Object
10496 pop_message_unwind (Lisp_Object dummy)
10497 {
10498 pop_message ();
10499 return Qnil;
10500 }
10501
10502 /* Pop the top-most entry off Vmessage_stack. */
10503
10504 static void
10505 pop_message (void)
10506 {
10507 eassert (CONSP (Vmessage_stack));
10508 Vmessage_stack = XCDR (Vmessage_stack);
10509 }
10510
10511
10512 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10513 exits. If the stack is not empty, we have a missing pop_message
10514 somewhere. */
10515
10516 void
10517 check_message_stack (void)
10518 {
10519 if (!NILP (Vmessage_stack))
10520 emacs_abort ();
10521 }
10522
10523
10524 /* Truncate to NCHARS what will be displayed in the echo area the next
10525 time we display it---but don't redisplay it now. */
10526
10527 void
10528 truncate_echo_area (ptrdiff_t nchars)
10529 {
10530 if (nchars == 0)
10531 echo_area_buffer[0] = Qnil;
10532 else if (!noninteractive
10533 && INTERACTIVE
10534 && !NILP (echo_area_buffer[0]))
10535 {
10536 struct frame *sf = SELECTED_FRAME ();
10537 /* Error messages get reported properly by cmd_error, so this must be
10538 just an informative message; if the frame hasn't really been
10539 initialized yet, just toss it. */
10540 if (sf->glyphs_initialized_p)
10541 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil);
10542 }
10543 }
10544
10545
10546 /* Helper function for truncate_echo_area. Truncate the current
10547 message to at most NCHARS characters. */
10548
10549 static int
10550 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2)
10551 {
10552 if (BEG + nchars < Z)
10553 del_range (BEG + nchars, Z);
10554 if (Z == BEG)
10555 echo_area_buffer[0] = Qnil;
10556 return 0;
10557 }
10558
10559 /* Set the current message to STRING. */
10560
10561 static void
10562 set_message (Lisp_Object string)
10563 {
10564 eassert (STRINGP (string));
10565
10566 message_enable_multibyte = STRING_MULTIBYTE (string);
10567
10568 with_echo_area_buffer (0, -1, set_message_1, 0, string);
10569 message_buf_print = 0;
10570 help_echo_showing_p = 0;
10571
10572 if (STRINGP (Vdebug_on_message)
10573 && STRINGP (string)
10574 && fast_string_match (Vdebug_on_message, string) >= 0)
10575 call_debugger (list2 (Qerror, string));
10576 }
10577
10578
10579 /* Helper function for set_message. First argument is ignored and second
10580 argument has the same meaning as for set_message.
10581 This function is called with the echo area buffer being current. */
10582
10583 static int
10584 set_message_1 (ptrdiff_t a1, Lisp_Object string)
10585 {
10586 eassert (STRINGP (string));
10587
10588 /* Change multibyteness of the echo buffer appropriately. */
10589 if (message_enable_multibyte
10590 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10591 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10592
10593 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10594 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10595 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10596
10597 /* Insert new message at BEG. */
10598 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10599
10600 /* This function takes care of single/multibyte conversion.
10601 We just have to ensure that the echo area buffer has the right
10602 setting of enable_multibyte_characters. */
10603 insert_from_string (string, 0, 0, SCHARS (string), SBYTES (string), 1);
10604
10605 return 0;
10606 }
10607
10608
10609 /* Clear messages. CURRENT_P non-zero means clear the current
10610 message. LAST_DISPLAYED_P non-zero means clear the message
10611 last displayed. */
10612
10613 void
10614 clear_message (int current_p, int last_displayed_p)
10615 {
10616 if (current_p)
10617 {
10618 echo_area_buffer[0] = Qnil;
10619 message_cleared_p = 1;
10620 }
10621
10622 if (last_displayed_p)
10623 echo_area_buffer[1] = Qnil;
10624
10625 message_buf_print = 0;
10626 }
10627
10628 /* Clear garbaged frames.
10629
10630 This function is used where the old redisplay called
10631 redraw_garbaged_frames which in turn called redraw_frame which in
10632 turn called clear_frame. The call to clear_frame was a source of
10633 flickering. I believe a clear_frame is not necessary. It should
10634 suffice in the new redisplay to invalidate all current matrices,
10635 and ensure a complete redisplay of all windows. */
10636
10637 static void
10638 clear_garbaged_frames (void)
10639 {
10640 if (frame_garbaged)
10641 {
10642 Lisp_Object tail, frame;
10643 int changed_count = 0;
10644
10645 FOR_EACH_FRAME (tail, frame)
10646 {
10647 struct frame *f = XFRAME (frame);
10648
10649 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10650 {
10651 if (f->resized_p)
10652 {
10653 redraw_frame (f);
10654 f->force_flush_display_p = 1;
10655 }
10656 clear_current_matrices (f);
10657 changed_count++;
10658 f->garbaged = 0;
10659 f->resized_p = 0;
10660 }
10661 }
10662
10663 frame_garbaged = 0;
10664 if (changed_count)
10665 ++windows_or_buffers_changed;
10666 }
10667 }
10668
10669
10670 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10671 is non-zero update selected_frame. Value is non-zero if the
10672 mini-windows height has been changed. */
10673
10674 static int
10675 echo_area_display (int update_frame_p)
10676 {
10677 Lisp_Object mini_window;
10678 struct window *w;
10679 struct frame *f;
10680 int window_height_changed_p = 0;
10681 struct frame *sf = SELECTED_FRAME ();
10682
10683 mini_window = FRAME_MINIBUF_WINDOW (sf);
10684 w = XWINDOW (mini_window);
10685 f = XFRAME (WINDOW_FRAME (w));
10686
10687 /* Don't display if frame is invisible or not yet initialized. */
10688 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10689 return 0;
10690
10691 #ifdef HAVE_WINDOW_SYSTEM
10692 /* When Emacs starts, selected_frame may be the initial terminal
10693 frame. If we let this through, a message would be displayed on
10694 the terminal. */
10695 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10696 return 0;
10697 #endif /* HAVE_WINDOW_SYSTEM */
10698
10699 /* Redraw garbaged frames. */
10700 clear_garbaged_frames ();
10701
10702 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10703 {
10704 echo_area_window = mini_window;
10705 window_height_changed_p = display_echo_area (w);
10706 w->must_be_updated_p = 1;
10707
10708 /* Update the display, unless called from redisplay_internal.
10709 Also don't update the screen during redisplay itself. The
10710 update will happen at the end of redisplay, and an update
10711 here could cause confusion. */
10712 if (update_frame_p && !redisplaying_p)
10713 {
10714 int n = 0;
10715
10716 /* If the display update has been interrupted by pending
10717 input, update mode lines in the frame. Due to the
10718 pending input, it might have been that redisplay hasn't
10719 been called, so that mode lines above the echo area are
10720 garbaged. This looks odd, so we prevent it here. */
10721 if (!display_completed)
10722 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10723
10724 if (window_height_changed_p
10725 /* Don't do this if Emacs is shutting down. Redisplay
10726 needs to run hooks. */
10727 && !NILP (Vrun_hooks))
10728 {
10729 /* Must update other windows. Likewise as in other
10730 cases, don't let this update be interrupted by
10731 pending input. */
10732 ptrdiff_t count = SPECPDL_INDEX ();
10733 specbind (Qredisplay_dont_pause, Qt);
10734 windows_or_buffers_changed = 1;
10735 redisplay_internal ();
10736 unbind_to (count, Qnil);
10737 }
10738 else if (FRAME_WINDOW_P (f) && n == 0)
10739 {
10740 /* Window configuration is the same as before.
10741 Can do with a display update of the echo area,
10742 unless we displayed some mode lines. */
10743 update_single_window (w, 1);
10744 FRAME_RIF (f)->flush_display (f);
10745 }
10746 else
10747 update_frame (f, 1, 1);
10748
10749 /* If cursor is in the echo area, make sure that the next
10750 redisplay displays the minibuffer, so that the cursor will
10751 be replaced with what the minibuffer wants. */
10752 if (cursor_in_echo_area)
10753 ++windows_or_buffers_changed;
10754 }
10755 }
10756 else if (!EQ (mini_window, selected_window))
10757 windows_or_buffers_changed++;
10758
10759 /* Last displayed message is now the current message. */
10760 echo_area_buffer[1] = echo_area_buffer[0];
10761 /* Inform read_char that we're not echoing. */
10762 echo_message_buffer = Qnil;
10763
10764 /* Prevent redisplay optimization in redisplay_internal by resetting
10765 this_line_start_pos. This is done because the mini-buffer now
10766 displays the message instead of its buffer text. */
10767 if (EQ (mini_window, selected_window))
10768 CHARPOS (this_line_start_pos) = 0;
10769
10770 return window_height_changed_p;
10771 }
10772
10773 /* Nonzero if the current window's buffer is shown in more than one
10774 window and was modified since last redisplay. */
10775
10776 static int
10777 buffer_shared_and_changed (void)
10778 {
10779 return (buffer_window_count (current_buffer) > 1
10780 && UNCHANGED_MODIFIED < MODIFF);
10781 }
10782
10783 /* Nonzero if W doesn't reflect the actual state of current buffer due
10784 to its text or overlays change. FIXME: this may be called when
10785 XBUFFER (w->contents) != current_buffer, which looks suspicious. */
10786
10787 static int
10788 window_outdated (struct window *w)
10789 {
10790 return (w->last_modified < MODIFF
10791 || w->last_overlay_modified < OVERLAY_MODIFF);
10792 }
10793
10794 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10795 is enabled and mark of W's buffer was changed since last W's update. */
10796
10797 static int
10798 window_buffer_changed (struct window *w)
10799 {
10800 struct buffer *b = XBUFFER (w->contents);
10801
10802 eassert (BUFFER_LIVE_P (b));
10803
10804 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10805 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10806 != (w->region_showing != 0)));
10807 }
10808
10809 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10810
10811 static int
10812 mode_line_update_needed (struct window *w)
10813 {
10814 return (w->column_number_displayed != -1
10815 && !(PT == w->last_point && !window_outdated (w))
10816 && (w->column_number_displayed != current_column ()));
10817 }
10818
10819 /***********************************************************************
10820 Mode Lines and Frame Titles
10821 ***********************************************************************/
10822
10823 /* A buffer for constructing non-propertized mode-line strings and
10824 frame titles in it; allocated from the heap in init_xdisp and
10825 resized as needed in store_mode_line_noprop_char. */
10826
10827 static char *mode_line_noprop_buf;
10828
10829 /* The buffer's end, and a current output position in it. */
10830
10831 static char *mode_line_noprop_buf_end;
10832 static char *mode_line_noprop_ptr;
10833
10834 #define MODE_LINE_NOPROP_LEN(start) \
10835 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10836
10837 static enum {
10838 MODE_LINE_DISPLAY = 0,
10839 MODE_LINE_TITLE,
10840 MODE_LINE_NOPROP,
10841 MODE_LINE_STRING
10842 } mode_line_target;
10843
10844 /* Alist that caches the results of :propertize.
10845 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10846 static Lisp_Object mode_line_proptrans_alist;
10847
10848 /* List of strings making up the mode-line. */
10849 static Lisp_Object mode_line_string_list;
10850
10851 /* Base face property when building propertized mode line string. */
10852 static Lisp_Object mode_line_string_face;
10853 static Lisp_Object mode_line_string_face_prop;
10854
10855
10856 /* Unwind data for mode line strings */
10857
10858 static Lisp_Object Vmode_line_unwind_vector;
10859
10860 static Lisp_Object
10861 format_mode_line_unwind_data (struct frame *target_frame,
10862 struct buffer *obuf,
10863 Lisp_Object owin,
10864 int save_proptrans)
10865 {
10866 Lisp_Object vector, tmp;
10867
10868 /* Reduce consing by keeping one vector in
10869 Vwith_echo_area_save_vector. */
10870 vector = Vmode_line_unwind_vector;
10871 Vmode_line_unwind_vector = Qnil;
10872
10873 if (NILP (vector))
10874 vector = Fmake_vector (make_number (10), Qnil);
10875
10876 ASET (vector, 0, make_number (mode_line_target));
10877 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
10878 ASET (vector, 2, mode_line_string_list);
10879 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
10880 ASET (vector, 4, mode_line_string_face);
10881 ASET (vector, 5, mode_line_string_face_prop);
10882
10883 if (obuf)
10884 XSETBUFFER (tmp, obuf);
10885 else
10886 tmp = Qnil;
10887 ASET (vector, 6, tmp);
10888 ASET (vector, 7, owin);
10889 if (target_frame)
10890 {
10891 /* Similarly to `with-selected-window', if the operation selects
10892 a window on another frame, we must restore that frame's
10893 selected window, and (for a tty) the top-frame. */
10894 ASET (vector, 8, target_frame->selected_window);
10895 if (FRAME_TERMCAP_P (target_frame))
10896 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
10897 }
10898
10899 return vector;
10900 }
10901
10902 static Lisp_Object
10903 unwind_format_mode_line (Lisp_Object vector)
10904 {
10905 Lisp_Object old_window = AREF (vector, 7);
10906 Lisp_Object target_frame_window = AREF (vector, 8);
10907 Lisp_Object old_top_frame = AREF (vector, 9);
10908
10909 mode_line_target = XINT (AREF (vector, 0));
10910 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
10911 mode_line_string_list = AREF (vector, 2);
10912 if (! EQ (AREF (vector, 3), Qt))
10913 mode_line_proptrans_alist = AREF (vector, 3);
10914 mode_line_string_face = AREF (vector, 4);
10915 mode_line_string_face_prop = AREF (vector, 5);
10916
10917 /* Select window before buffer, since it may change the buffer. */
10918 if (!NILP (old_window))
10919 {
10920 /* If the operation that we are unwinding had selected a window
10921 on a different frame, reset its frame-selected-window. For a
10922 text terminal, reset its top-frame if necessary. */
10923 if (!NILP (target_frame_window))
10924 {
10925 Lisp_Object frame
10926 = WINDOW_FRAME (XWINDOW (target_frame_window));
10927
10928 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
10929 Fselect_window (target_frame_window, Qt);
10930
10931 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
10932 Fselect_frame (old_top_frame, Qt);
10933 }
10934
10935 Fselect_window (old_window, Qt);
10936 }
10937
10938 if (!NILP (AREF (vector, 6)))
10939 {
10940 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
10941 ASET (vector, 6, Qnil);
10942 }
10943
10944 Vmode_line_unwind_vector = vector;
10945 return Qnil;
10946 }
10947
10948
10949 /* Store a single character C for the frame title in mode_line_noprop_buf.
10950 Re-allocate mode_line_noprop_buf if necessary. */
10951
10952 static void
10953 store_mode_line_noprop_char (char c)
10954 {
10955 /* If output position has reached the end of the allocated buffer,
10956 increase the buffer's size. */
10957 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
10958 {
10959 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
10960 ptrdiff_t size = len;
10961 mode_line_noprop_buf =
10962 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
10963 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
10964 mode_line_noprop_ptr = mode_line_noprop_buf + len;
10965 }
10966
10967 *mode_line_noprop_ptr++ = c;
10968 }
10969
10970
10971 /* Store part of a frame title in mode_line_noprop_buf, beginning at
10972 mode_line_noprop_ptr. STRING is the string to store. Do not copy
10973 characters that yield more columns than PRECISION; PRECISION <= 0
10974 means copy the whole string. Pad with spaces until FIELD_WIDTH
10975 number of characters have been copied; FIELD_WIDTH <= 0 means don't
10976 pad. Called from display_mode_element when it is used to build a
10977 frame title. */
10978
10979 static int
10980 store_mode_line_noprop (const char *string, int field_width, int precision)
10981 {
10982 const unsigned char *str = (const unsigned char *) string;
10983 int n = 0;
10984 ptrdiff_t dummy, nbytes;
10985
10986 /* Copy at most PRECISION chars from STR. */
10987 nbytes = strlen (string);
10988 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
10989 while (nbytes--)
10990 store_mode_line_noprop_char (*str++);
10991
10992 /* Fill up with spaces until FIELD_WIDTH reached. */
10993 while (field_width > 0
10994 && n < field_width)
10995 {
10996 store_mode_line_noprop_char (' ');
10997 ++n;
10998 }
10999
11000 return n;
11001 }
11002
11003 /***********************************************************************
11004 Frame Titles
11005 ***********************************************************************/
11006
11007 #ifdef HAVE_WINDOW_SYSTEM
11008
11009 /* Set the title of FRAME, if it has changed. The title format is
11010 Vicon_title_format if FRAME is iconified, otherwise it is
11011 frame_title_format. */
11012
11013 static void
11014 x_consider_frame_title (Lisp_Object frame)
11015 {
11016 struct frame *f = XFRAME (frame);
11017
11018 if (FRAME_WINDOW_P (f)
11019 || FRAME_MINIBUF_ONLY_P (f)
11020 || f->explicit_name)
11021 {
11022 /* Do we have more than one visible frame on this X display? */
11023 Lisp_Object tail, other_frame, fmt;
11024 ptrdiff_t title_start;
11025 char *title;
11026 ptrdiff_t len;
11027 struct it it;
11028 ptrdiff_t count = SPECPDL_INDEX ();
11029
11030 FOR_EACH_FRAME (tail, other_frame)
11031 {
11032 struct frame *tf = XFRAME (other_frame);
11033
11034 if (tf != f
11035 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11036 && !FRAME_MINIBUF_ONLY_P (tf)
11037 && !EQ (other_frame, tip_frame)
11038 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11039 break;
11040 }
11041
11042 /* Set global variable indicating that multiple frames exist. */
11043 multiple_frames = CONSP (tail);
11044
11045 /* Switch to the buffer of selected window of the frame. Set up
11046 mode_line_target so that display_mode_element will output into
11047 mode_line_noprop_buf; then display the title. */
11048 record_unwind_protect (unwind_format_mode_line,
11049 format_mode_line_unwind_data
11050 (f, current_buffer, selected_window, 0));
11051
11052 Fselect_window (f->selected_window, Qt);
11053 set_buffer_internal_1
11054 (XBUFFER (XWINDOW (f->selected_window)->contents));
11055 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11056
11057 mode_line_target = MODE_LINE_TITLE;
11058 title_start = MODE_LINE_NOPROP_LEN (0);
11059 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11060 NULL, DEFAULT_FACE_ID);
11061 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11062 len = MODE_LINE_NOPROP_LEN (title_start);
11063 title = mode_line_noprop_buf + title_start;
11064 unbind_to (count, Qnil);
11065
11066 /* Set the title only if it's changed. This avoids consing in
11067 the common case where it hasn't. (If it turns out that we've
11068 already wasted too much time by walking through the list with
11069 display_mode_element, then we might need to optimize at a
11070 higher level than this.) */
11071 if (! STRINGP (f->name)
11072 || SBYTES (f->name) != len
11073 || memcmp (title, SDATA (f->name), len) != 0)
11074 x_implicitly_set_name (f, make_string (title, len), Qnil);
11075 }
11076 }
11077
11078 #endif /* not HAVE_WINDOW_SYSTEM */
11079
11080 \f
11081 /***********************************************************************
11082 Menu Bars
11083 ***********************************************************************/
11084
11085
11086 /* Prepare for redisplay by updating menu-bar item lists when
11087 appropriate. This can call eval. */
11088
11089 void
11090 prepare_menu_bars (void)
11091 {
11092 int all_windows;
11093 struct gcpro gcpro1, gcpro2;
11094 struct frame *f;
11095 Lisp_Object tooltip_frame;
11096
11097 #ifdef HAVE_WINDOW_SYSTEM
11098 tooltip_frame = tip_frame;
11099 #else
11100 tooltip_frame = Qnil;
11101 #endif
11102
11103 /* Update all frame titles based on their buffer names, etc. We do
11104 this before the menu bars so that the buffer-menu will show the
11105 up-to-date frame titles. */
11106 #ifdef HAVE_WINDOW_SYSTEM
11107 if (windows_or_buffers_changed || update_mode_lines)
11108 {
11109 Lisp_Object tail, frame;
11110
11111 FOR_EACH_FRAME (tail, frame)
11112 {
11113 f = XFRAME (frame);
11114 if (!EQ (frame, tooltip_frame)
11115 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11116 x_consider_frame_title (frame);
11117 }
11118 }
11119 #endif /* HAVE_WINDOW_SYSTEM */
11120
11121 /* Update the menu bar item lists, if appropriate. This has to be
11122 done before any actual redisplay or generation of display lines. */
11123 all_windows = (update_mode_lines
11124 || buffer_shared_and_changed ()
11125 || windows_or_buffers_changed);
11126 if (all_windows)
11127 {
11128 Lisp_Object tail, frame;
11129 ptrdiff_t count = SPECPDL_INDEX ();
11130 /* 1 means that update_menu_bar has run its hooks
11131 so any further calls to update_menu_bar shouldn't do so again. */
11132 int menu_bar_hooks_run = 0;
11133
11134 record_unwind_save_match_data ();
11135
11136 FOR_EACH_FRAME (tail, frame)
11137 {
11138 f = XFRAME (frame);
11139
11140 /* Ignore tooltip frame. */
11141 if (EQ (frame, tooltip_frame))
11142 continue;
11143
11144 /* If a window on this frame changed size, report that to
11145 the user and clear the size-change flag. */
11146 if (FRAME_WINDOW_SIZES_CHANGED (f))
11147 {
11148 Lisp_Object functions;
11149
11150 /* Clear flag first in case we get an error below. */
11151 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11152 functions = Vwindow_size_change_functions;
11153 GCPRO2 (tail, functions);
11154
11155 while (CONSP (functions))
11156 {
11157 if (!EQ (XCAR (functions), Qt))
11158 call1 (XCAR (functions), frame);
11159 functions = XCDR (functions);
11160 }
11161 UNGCPRO;
11162 }
11163
11164 GCPRO1 (tail);
11165 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11166 #ifdef HAVE_WINDOW_SYSTEM
11167 update_tool_bar (f, 0);
11168 #endif
11169 #ifdef HAVE_NS
11170 if (windows_or_buffers_changed
11171 && FRAME_NS_P (f))
11172 ns_set_doc_edited
11173 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
11174 #endif
11175 UNGCPRO;
11176 }
11177
11178 unbind_to (count, Qnil);
11179 }
11180 else
11181 {
11182 struct frame *sf = SELECTED_FRAME ();
11183 update_menu_bar (sf, 1, 0);
11184 #ifdef HAVE_WINDOW_SYSTEM
11185 update_tool_bar (sf, 1);
11186 #endif
11187 }
11188 }
11189
11190
11191 /* Update the menu bar item list for frame F. This has to be done
11192 before we start to fill in any display lines, because it can call
11193 eval.
11194
11195 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11196
11197 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11198 already ran the menu bar hooks for this redisplay, so there
11199 is no need to run them again. The return value is the
11200 updated value of this flag, to pass to the next call. */
11201
11202 static int
11203 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11204 {
11205 Lisp_Object window;
11206 register struct window *w;
11207
11208 /* If called recursively during a menu update, do nothing. This can
11209 happen when, for instance, an activate-menubar-hook causes a
11210 redisplay. */
11211 if (inhibit_menubar_update)
11212 return hooks_run;
11213
11214 window = FRAME_SELECTED_WINDOW (f);
11215 w = XWINDOW (window);
11216
11217 if (FRAME_WINDOW_P (f)
11218 ?
11219 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11220 || defined (HAVE_NS) || defined (USE_GTK)
11221 FRAME_EXTERNAL_MENU_BAR (f)
11222 #else
11223 FRAME_MENU_BAR_LINES (f) > 0
11224 #endif
11225 : FRAME_MENU_BAR_LINES (f) > 0)
11226 {
11227 /* If the user has switched buffers or windows, we need to
11228 recompute to reflect the new bindings. But we'll
11229 recompute when update_mode_lines is set too; that means
11230 that people can use force-mode-line-update to request
11231 that the menu bar be recomputed. The adverse effect on
11232 the rest of the redisplay algorithm is about the same as
11233 windows_or_buffers_changed anyway. */
11234 if (windows_or_buffers_changed
11235 /* This used to test w->update_mode_line, but we believe
11236 there is no need to recompute the menu in that case. */
11237 || update_mode_lines
11238 || window_buffer_changed (w))
11239 {
11240 struct buffer *prev = current_buffer;
11241 ptrdiff_t count = SPECPDL_INDEX ();
11242
11243 specbind (Qinhibit_menubar_update, Qt);
11244
11245 set_buffer_internal_1 (XBUFFER (w->contents));
11246 if (save_match_data)
11247 record_unwind_save_match_data ();
11248 if (NILP (Voverriding_local_map_menu_flag))
11249 {
11250 specbind (Qoverriding_terminal_local_map, Qnil);
11251 specbind (Qoverriding_local_map, Qnil);
11252 }
11253
11254 if (!hooks_run)
11255 {
11256 /* Run the Lucid hook. */
11257 safe_run_hooks (Qactivate_menubar_hook);
11258
11259 /* If it has changed current-menubar from previous value,
11260 really recompute the menu-bar from the value. */
11261 if (! NILP (Vlucid_menu_bar_dirty_flag))
11262 call0 (Qrecompute_lucid_menubar);
11263
11264 safe_run_hooks (Qmenu_bar_update_hook);
11265
11266 hooks_run = 1;
11267 }
11268
11269 XSETFRAME (Vmenu_updating_frame, f);
11270 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11271
11272 /* Redisplay the menu bar in case we changed it. */
11273 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11274 || defined (HAVE_NS) || defined (USE_GTK)
11275 if (FRAME_WINDOW_P (f))
11276 {
11277 #if defined (HAVE_NS)
11278 /* All frames on Mac OS share the same menubar. So only
11279 the selected frame should be allowed to set it. */
11280 if (f == SELECTED_FRAME ())
11281 #endif
11282 set_frame_menubar (f, 0, 0);
11283 }
11284 else
11285 /* On a terminal screen, the menu bar is an ordinary screen
11286 line, and this makes it get updated. */
11287 w->update_mode_line = 1;
11288 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11289 /* In the non-toolkit version, the menu bar is an ordinary screen
11290 line, and this makes it get updated. */
11291 w->update_mode_line = 1;
11292 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11293
11294 unbind_to (count, Qnil);
11295 set_buffer_internal_1 (prev);
11296 }
11297 }
11298
11299 return hooks_run;
11300 }
11301
11302
11303 \f
11304 /***********************************************************************
11305 Output Cursor
11306 ***********************************************************************/
11307
11308 #ifdef HAVE_WINDOW_SYSTEM
11309
11310 /* EXPORT:
11311 Nominal cursor position -- where to draw output.
11312 HPOS and VPOS are window relative glyph matrix coordinates.
11313 X and Y are window relative pixel coordinates. */
11314
11315 struct cursor_pos output_cursor;
11316
11317
11318 /* EXPORT:
11319 Set the global variable output_cursor to CURSOR. All cursor
11320 positions are relative to updated_window. */
11321
11322 void
11323 set_output_cursor (struct cursor_pos *cursor)
11324 {
11325 output_cursor.hpos = cursor->hpos;
11326 output_cursor.vpos = cursor->vpos;
11327 output_cursor.x = cursor->x;
11328 output_cursor.y = cursor->y;
11329 }
11330
11331
11332 /* EXPORT for RIF:
11333 Set a nominal cursor position.
11334
11335 HPOS and VPOS are column/row positions in a window glyph matrix. X
11336 and Y are window text area relative pixel positions.
11337
11338 If this is done during an update, updated_window will contain the
11339 window that is being updated and the position is the future output
11340 cursor position for that window. If updated_window is null, use
11341 selected_window and display the cursor at the given position. */
11342
11343 void
11344 x_cursor_to (int vpos, int hpos, int y, int x)
11345 {
11346 struct window *w;
11347
11348 /* If updated_window is not set, work on selected_window. */
11349 if (updated_window)
11350 w = updated_window;
11351 else
11352 w = XWINDOW (selected_window);
11353
11354 /* Set the output cursor. */
11355 output_cursor.hpos = hpos;
11356 output_cursor.vpos = vpos;
11357 output_cursor.x = x;
11358 output_cursor.y = y;
11359
11360 /* If not called as part of an update, really display the cursor.
11361 This will also set the cursor position of W. */
11362 if (updated_window == NULL)
11363 {
11364 block_input ();
11365 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11366 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11367 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11368 unblock_input ();
11369 }
11370 }
11371
11372 #endif /* HAVE_WINDOW_SYSTEM */
11373
11374 \f
11375 /***********************************************************************
11376 Tool-bars
11377 ***********************************************************************/
11378
11379 #ifdef HAVE_WINDOW_SYSTEM
11380
11381 /* Where the mouse was last time we reported a mouse event. */
11382
11383 FRAME_PTR last_mouse_frame;
11384
11385 /* Tool-bar item index of the item on which a mouse button was pressed
11386 or -1. */
11387
11388 int last_tool_bar_item;
11389
11390 /* Select `frame' temporarily without running all the code in
11391 do_switch_frame.
11392 FIXME: Maybe do_switch_frame should be trimmed down similarly
11393 when `norecord' is set. */
11394 static Lisp_Object
11395 fast_set_selected_frame (Lisp_Object frame)
11396 {
11397 if (!EQ (selected_frame, frame))
11398 {
11399 selected_frame = frame;
11400 selected_window = XFRAME (frame)->selected_window;
11401 }
11402 return Qnil;
11403 }
11404
11405 /* Update the tool-bar item list for frame F. This has to be done
11406 before we start to fill in any display lines. Called from
11407 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11408 and restore it here. */
11409
11410 static void
11411 update_tool_bar (struct frame *f, int save_match_data)
11412 {
11413 #if defined (USE_GTK) || defined (HAVE_NS)
11414 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11415 #else
11416 int do_update = WINDOWP (f->tool_bar_window)
11417 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11418 #endif
11419
11420 if (do_update)
11421 {
11422 Lisp_Object window;
11423 struct window *w;
11424
11425 window = FRAME_SELECTED_WINDOW (f);
11426 w = XWINDOW (window);
11427
11428 /* If the user has switched buffers or windows, we need to
11429 recompute to reflect the new bindings. But we'll
11430 recompute when update_mode_lines is set too; that means
11431 that people can use force-mode-line-update to request
11432 that the menu bar be recomputed. The adverse effect on
11433 the rest of the redisplay algorithm is about the same as
11434 windows_or_buffers_changed anyway. */
11435 if (windows_or_buffers_changed
11436 || w->update_mode_line
11437 || update_mode_lines
11438 || window_buffer_changed (w))
11439 {
11440 struct buffer *prev = current_buffer;
11441 ptrdiff_t count = SPECPDL_INDEX ();
11442 Lisp_Object frame, new_tool_bar;
11443 int new_n_tool_bar;
11444 struct gcpro gcpro1;
11445
11446 /* Set current_buffer to the buffer of the selected
11447 window of the frame, so that we get the right local
11448 keymaps. */
11449 set_buffer_internal_1 (XBUFFER (w->contents));
11450
11451 /* Save match data, if we must. */
11452 if (save_match_data)
11453 record_unwind_save_match_data ();
11454
11455 /* Make sure that we don't accidentally use bogus keymaps. */
11456 if (NILP (Voverriding_local_map_menu_flag))
11457 {
11458 specbind (Qoverriding_terminal_local_map, Qnil);
11459 specbind (Qoverriding_local_map, Qnil);
11460 }
11461
11462 GCPRO1 (new_tool_bar);
11463
11464 /* We must temporarily set the selected frame to this frame
11465 before calling tool_bar_items, because the calculation of
11466 the tool-bar keymap uses the selected frame (see
11467 `tool-bar-make-keymap' in tool-bar.el). */
11468 eassert (EQ (selected_window,
11469 /* Since we only explicitly preserve selected_frame,
11470 check that selected_window would be redundant. */
11471 XFRAME (selected_frame)->selected_window));
11472 record_unwind_protect (fast_set_selected_frame, selected_frame);
11473 XSETFRAME (frame, f);
11474 fast_set_selected_frame (frame);
11475
11476 /* Build desired tool-bar items from keymaps. */
11477 new_tool_bar
11478 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11479 &new_n_tool_bar);
11480
11481 /* Redisplay the tool-bar if we changed it. */
11482 if (new_n_tool_bar != f->n_tool_bar_items
11483 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11484 {
11485 /* Redisplay that happens asynchronously due to an expose event
11486 may access f->tool_bar_items. Make sure we update both
11487 variables within BLOCK_INPUT so no such event interrupts. */
11488 block_input ();
11489 fset_tool_bar_items (f, new_tool_bar);
11490 f->n_tool_bar_items = new_n_tool_bar;
11491 w->update_mode_line = 1;
11492 unblock_input ();
11493 }
11494
11495 UNGCPRO;
11496
11497 unbind_to (count, Qnil);
11498 set_buffer_internal_1 (prev);
11499 }
11500 }
11501 }
11502
11503
11504 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11505 F's desired tool-bar contents. F->tool_bar_items must have
11506 been set up previously by calling prepare_menu_bars. */
11507
11508 static void
11509 build_desired_tool_bar_string (struct frame *f)
11510 {
11511 int i, size, size_needed;
11512 struct gcpro gcpro1, gcpro2, gcpro3;
11513 Lisp_Object image, plist, props;
11514
11515 image = plist = props = Qnil;
11516 GCPRO3 (image, plist, props);
11517
11518 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11519 Otherwise, make a new string. */
11520
11521 /* The size of the string we might be able to reuse. */
11522 size = (STRINGP (f->desired_tool_bar_string)
11523 ? SCHARS (f->desired_tool_bar_string)
11524 : 0);
11525
11526 /* We need one space in the string for each image. */
11527 size_needed = f->n_tool_bar_items;
11528
11529 /* Reuse f->desired_tool_bar_string, if possible. */
11530 if (size < size_needed || NILP (f->desired_tool_bar_string))
11531 fset_desired_tool_bar_string
11532 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11533 else
11534 {
11535 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11536 Fremove_text_properties (make_number (0), make_number (size),
11537 props, f->desired_tool_bar_string);
11538 }
11539
11540 /* Put a `display' property on the string for the images to display,
11541 put a `menu_item' property on tool-bar items with a value that
11542 is the index of the item in F's tool-bar item vector. */
11543 for (i = 0; i < f->n_tool_bar_items; ++i)
11544 {
11545 #define PROP(IDX) \
11546 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11547
11548 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11549 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11550 int hmargin, vmargin, relief, idx, end;
11551
11552 /* If image is a vector, choose the image according to the
11553 button state. */
11554 image = PROP (TOOL_BAR_ITEM_IMAGES);
11555 if (VECTORP (image))
11556 {
11557 if (enabled_p)
11558 idx = (selected_p
11559 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11560 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11561 else
11562 idx = (selected_p
11563 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11564 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11565
11566 eassert (ASIZE (image) >= idx);
11567 image = AREF (image, idx);
11568 }
11569 else
11570 idx = -1;
11571
11572 /* Ignore invalid image specifications. */
11573 if (!valid_image_p (image))
11574 continue;
11575
11576 /* Display the tool-bar button pressed, or depressed. */
11577 plist = Fcopy_sequence (XCDR (image));
11578
11579 /* Compute margin and relief to draw. */
11580 relief = (tool_bar_button_relief >= 0
11581 ? tool_bar_button_relief
11582 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11583 hmargin = vmargin = relief;
11584
11585 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11586 INT_MAX - max (hmargin, vmargin)))
11587 {
11588 hmargin += XFASTINT (Vtool_bar_button_margin);
11589 vmargin += XFASTINT (Vtool_bar_button_margin);
11590 }
11591 else if (CONSP (Vtool_bar_button_margin))
11592 {
11593 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11594 INT_MAX - hmargin))
11595 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11596
11597 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11598 INT_MAX - vmargin))
11599 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11600 }
11601
11602 if (auto_raise_tool_bar_buttons_p)
11603 {
11604 /* Add a `:relief' property to the image spec if the item is
11605 selected. */
11606 if (selected_p)
11607 {
11608 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11609 hmargin -= relief;
11610 vmargin -= relief;
11611 }
11612 }
11613 else
11614 {
11615 /* If image is selected, display it pressed, i.e. with a
11616 negative relief. If it's not selected, display it with a
11617 raised relief. */
11618 plist = Fplist_put (plist, QCrelief,
11619 (selected_p
11620 ? make_number (-relief)
11621 : make_number (relief)));
11622 hmargin -= relief;
11623 vmargin -= relief;
11624 }
11625
11626 /* Put a margin around the image. */
11627 if (hmargin || vmargin)
11628 {
11629 if (hmargin == vmargin)
11630 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11631 else
11632 plist = Fplist_put (plist, QCmargin,
11633 Fcons (make_number (hmargin),
11634 make_number (vmargin)));
11635 }
11636
11637 /* If button is not enabled, and we don't have special images
11638 for the disabled state, make the image appear disabled by
11639 applying an appropriate algorithm to it. */
11640 if (!enabled_p && idx < 0)
11641 plist = Fplist_put (plist, QCconversion, Qdisabled);
11642
11643 /* Put a `display' text property on the string for the image to
11644 display. Put a `menu-item' property on the string that gives
11645 the start of this item's properties in the tool-bar items
11646 vector. */
11647 image = Fcons (Qimage, plist);
11648 props = list4 (Qdisplay, image,
11649 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11650
11651 /* Let the last image hide all remaining spaces in the tool bar
11652 string. The string can be longer than needed when we reuse a
11653 previous string. */
11654 if (i + 1 == f->n_tool_bar_items)
11655 end = SCHARS (f->desired_tool_bar_string);
11656 else
11657 end = i + 1;
11658 Fadd_text_properties (make_number (i), make_number (end),
11659 props, f->desired_tool_bar_string);
11660 #undef PROP
11661 }
11662
11663 UNGCPRO;
11664 }
11665
11666
11667 /* Display one line of the tool-bar of frame IT->f.
11668
11669 HEIGHT specifies the desired height of the tool-bar line.
11670 If the actual height of the glyph row is less than HEIGHT, the
11671 row's height is increased to HEIGHT, and the icons are centered
11672 vertically in the new height.
11673
11674 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11675 count a final empty row in case the tool-bar width exactly matches
11676 the window width.
11677 */
11678
11679 static void
11680 display_tool_bar_line (struct it *it, int height)
11681 {
11682 struct glyph_row *row = it->glyph_row;
11683 int max_x = it->last_visible_x;
11684 struct glyph *last;
11685
11686 prepare_desired_row (row);
11687 row->y = it->current_y;
11688
11689 /* Note that this isn't made use of if the face hasn't a box,
11690 so there's no need to check the face here. */
11691 it->start_of_box_run_p = 1;
11692
11693 while (it->current_x < max_x)
11694 {
11695 int x, n_glyphs_before, i, nglyphs;
11696 struct it it_before;
11697
11698 /* Get the next display element. */
11699 if (!get_next_display_element (it))
11700 {
11701 /* Don't count empty row if we are counting needed tool-bar lines. */
11702 if (height < 0 && !it->hpos)
11703 return;
11704 break;
11705 }
11706
11707 /* Produce glyphs. */
11708 n_glyphs_before = row->used[TEXT_AREA];
11709 it_before = *it;
11710
11711 PRODUCE_GLYPHS (it);
11712
11713 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11714 i = 0;
11715 x = it_before.current_x;
11716 while (i < nglyphs)
11717 {
11718 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11719
11720 if (x + glyph->pixel_width > max_x)
11721 {
11722 /* Glyph doesn't fit on line. Backtrack. */
11723 row->used[TEXT_AREA] = n_glyphs_before;
11724 *it = it_before;
11725 /* If this is the only glyph on this line, it will never fit on the
11726 tool-bar, so skip it. But ensure there is at least one glyph,
11727 so we don't accidentally disable the tool-bar. */
11728 if (n_glyphs_before == 0
11729 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11730 break;
11731 goto out;
11732 }
11733
11734 ++it->hpos;
11735 x += glyph->pixel_width;
11736 ++i;
11737 }
11738
11739 /* Stop at line end. */
11740 if (ITERATOR_AT_END_OF_LINE_P (it))
11741 break;
11742
11743 set_iterator_to_next (it, 1);
11744 }
11745
11746 out:;
11747
11748 row->displays_text_p = row->used[TEXT_AREA] != 0;
11749
11750 /* Use default face for the border below the tool bar.
11751
11752 FIXME: When auto-resize-tool-bars is grow-only, there is
11753 no additional border below the possibly empty tool-bar lines.
11754 So to make the extra empty lines look "normal", we have to
11755 use the tool-bar face for the border too. */
11756 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11757 && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11758 it->face_id = DEFAULT_FACE_ID;
11759
11760 extend_face_to_end_of_line (it);
11761 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11762 last->right_box_line_p = 1;
11763 if (last == row->glyphs[TEXT_AREA])
11764 last->left_box_line_p = 1;
11765
11766 /* Make line the desired height and center it vertically. */
11767 if ((height -= it->max_ascent + it->max_descent) > 0)
11768 {
11769 /* Don't add more than one line height. */
11770 height %= FRAME_LINE_HEIGHT (it->f);
11771 it->max_ascent += height / 2;
11772 it->max_descent += (height + 1) / 2;
11773 }
11774
11775 compute_line_metrics (it);
11776
11777 /* If line is empty, make it occupy the rest of the tool-bar. */
11778 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row))
11779 {
11780 row->height = row->phys_height = it->last_visible_y - row->y;
11781 row->visible_height = row->height;
11782 row->ascent = row->phys_ascent = 0;
11783 row->extra_line_spacing = 0;
11784 }
11785
11786 row->full_width_p = 1;
11787 row->continued_p = 0;
11788 row->truncated_on_left_p = 0;
11789 row->truncated_on_right_p = 0;
11790
11791 it->current_x = it->hpos = 0;
11792 it->current_y += row->height;
11793 ++it->vpos;
11794 ++it->glyph_row;
11795 }
11796
11797
11798 /* Max tool-bar height. */
11799
11800 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11801 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11802
11803 /* Value is the number of screen lines needed to make all tool-bar
11804 items of frame F visible. The number of actual rows needed is
11805 returned in *N_ROWS if non-NULL. */
11806
11807 static int
11808 tool_bar_lines_needed (struct frame *f, int *n_rows)
11809 {
11810 struct window *w = XWINDOW (f->tool_bar_window);
11811 struct it it;
11812 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11813 the desired matrix, so use (unused) mode-line row as temporary row to
11814 avoid destroying the first tool-bar row. */
11815 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11816
11817 /* Initialize an iterator for iteration over
11818 F->desired_tool_bar_string in the tool-bar window of frame F. */
11819 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11820 it.first_visible_x = 0;
11821 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11822 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11823 it.paragraph_embedding = L2R;
11824
11825 while (!ITERATOR_AT_END_P (&it))
11826 {
11827 clear_glyph_row (temp_row);
11828 it.glyph_row = temp_row;
11829 display_tool_bar_line (&it, -1);
11830 }
11831 clear_glyph_row (temp_row);
11832
11833 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11834 if (n_rows)
11835 *n_rows = it.vpos > 0 ? it.vpos : -1;
11836
11837 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11838 }
11839
11840
11841 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11842 0, 1, 0,
11843 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11844 If FRAME is nil or omitted, use the selected frame. */)
11845 (Lisp_Object frame)
11846 {
11847 struct frame *f = decode_any_frame (frame);
11848 struct window *w;
11849 int nlines = 0;
11850
11851 if (WINDOWP (f->tool_bar_window)
11852 && (w = XWINDOW (f->tool_bar_window),
11853 WINDOW_TOTAL_LINES (w) > 0))
11854 {
11855 update_tool_bar (f, 1);
11856 if (f->n_tool_bar_items)
11857 {
11858 build_desired_tool_bar_string (f);
11859 nlines = tool_bar_lines_needed (f, NULL);
11860 }
11861 }
11862
11863 return make_number (nlines);
11864 }
11865
11866
11867 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11868 height should be changed. */
11869
11870 static int
11871 redisplay_tool_bar (struct frame *f)
11872 {
11873 struct window *w;
11874 struct it it;
11875 struct glyph_row *row;
11876
11877 #if defined (USE_GTK) || defined (HAVE_NS)
11878 if (FRAME_EXTERNAL_TOOL_BAR (f))
11879 update_frame_tool_bar (f);
11880 return 0;
11881 #endif
11882
11883 /* If frame hasn't a tool-bar window or if it is zero-height, don't
11884 do anything. This means you must start with tool-bar-lines
11885 non-zero to get the auto-sizing effect. Or in other words, you
11886 can turn off tool-bars by specifying tool-bar-lines zero. */
11887 if (!WINDOWP (f->tool_bar_window)
11888 || (w = XWINDOW (f->tool_bar_window),
11889 WINDOW_TOTAL_LINES (w) == 0))
11890 return 0;
11891
11892 /* Set up an iterator for the tool-bar window. */
11893 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
11894 it.first_visible_x = 0;
11895 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11896 row = it.glyph_row;
11897
11898 /* Build a string that represents the contents of the tool-bar. */
11899 build_desired_tool_bar_string (f);
11900 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11901 /* FIXME: This should be controlled by a user option. But it
11902 doesn't make sense to have an R2L tool bar if the menu bar cannot
11903 be drawn also R2L, and making the menu bar R2L is tricky due
11904 toolkit-specific code that implements it. If an R2L tool bar is
11905 ever supported, display_tool_bar_line should also be augmented to
11906 call unproduce_glyphs like display_line and display_string
11907 do. */
11908 it.paragraph_embedding = L2R;
11909
11910 if (f->n_tool_bar_rows == 0)
11911 {
11912 int nlines;
11913
11914 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
11915 nlines != WINDOW_TOTAL_LINES (w)))
11916 {
11917 Lisp_Object frame;
11918 int old_height = WINDOW_TOTAL_LINES (w);
11919
11920 XSETFRAME (frame, f);
11921 Fmodify_frame_parameters (frame,
11922 Fcons (Fcons (Qtool_bar_lines,
11923 make_number (nlines)),
11924 Qnil));
11925 if (WINDOW_TOTAL_LINES (w) != old_height)
11926 {
11927 clear_glyph_matrix (w->desired_matrix);
11928 fonts_changed_p = 1;
11929 return 1;
11930 }
11931 }
11932 }
11933
11934 /* Display as many lines as needed to display all tool-bar items. */
11935
11936 if (f->n_tool_bar_rows > 0)
11937 {
11938 int border, rows, height, extra;
11939
11940 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
11941 border = XINT (Vtool_bar_border);
11942 else if (EQ (Vtool_bar_border, Qinternal_border_width))
11943 border = FRAME_INTERNAL_BORDER_WIDTH (f);
11944 else if (EQ (Vtool_bar_border, Qborder_width))
11945 border = f->border_width;
11946 else
11947 border = 0;
11948 if (border < 0)
11949 border = 0;
11950
11951 rows = f->n_tool_bar_rows;
11952 height = max (1, (it.last_visible_y - border) / rows);
11953 extra = it.last_visible_y - border - height * rows;
11954
11955 while (it.current_y < it.last_visible_y)
11956 {
11957 int h = 0;
11958 if (extra > 0 && rows-- > 0)
11959 {
11960 h = (extra + rows - 1) / rows;
11961 extra -= h;
11962 }
11963 display_tool_bar_line (&it, height + h);
11964 }
11965 }
11966 else
11967 {
11968 while (it.current_y < it.last_visible_y)
11969 display_tool_bar_line (&it, 0);
11970 }
11971
11972 /* It doesn't make much sense to try scrolling in the tool-bar
11973 window, so don't do it. */
11974 w->desired_matrix->no_scrolling_p = 1;
11975 w->must_be_updated_p = 1;
11976
11977 if (!NILP (Vauto_resize_tool_bars))
11978 {
11979 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
11980 int change_height_p = 0;
11981
11982 /* If we couldn't display everything, change the tool-bar's
11983 height if there is room for more. */
11984 if (IT_STRING_CHARPOS (it) < it.end_charpos
11985 && it.current_y < max_tool_bar_height)
11986 change_height_p = 1;
11987
11988 row = it.glyph_row - 1;
11989
11990 /* If there are blank lines at the end, except for a partially
11991 visible blank line at the end that is smaller than
11992 FRAME_LINE_HEIGHT, change the tool-bar's height. */
11993 if (!MATRIX_ROW_DISPLAYS_TEXT_P (row)
11994 && row->height >= FRAME_LINE_HEIGHT (f))
11995 change_height_p = 1;
11996
11997 /* If row displays tool-bar items, but is partially visible,
11998 change the tool-bar's height. */
11999 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
12000 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12001 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12002 change_height_p = 1;
12003
12004 /* Resize windows as needed by changing the `tool-bar-lines'
12005 frame parameter. */
12006 if (change_height_p)
12007 {
12008 Lisp_Object frame;
12009 int old_height = WINDOW_TOTAL_LINES (w);
12010 int nrows;
12011 int nlines = tool_bar_lines_needed (f, &nrows);
12012
12013 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12014 && !f->minimize_tool_bar_window_p)
12015 ? (nlines > old_height)
12016 : (nlines != old_height));
12017 f->minimize_tool_bar_window_p = 0;
12018
12019 if (change_height_p)
12020 {
12021 XSETFRAME (frame, f);
12022 Fmodify_frame_parameters (frame,
12023 Fcons (Fcons (Qtool_bar_lines,
12024 make_number (nlines)),
12025 Qnil));
12026 if (WINDOW_TOTAL_LINES (w) != old_height)
12027 {
12028 clear_glyph_matrix (w->desired_matrix);
12029 f->n_tool_bar_rows = nrows;
12030 fonts_changed_p = 1;
12031 return 1;
12032 }
12033 }
12034 }
12035 }
12036
12037 f->minimize_tool_bar_window_p = 0;
12038 return 0;
12039 }
12040
12041
12042 /* Get information about the tool-bar item which is displayed in GLYPH
12043 on frame F. Return in *PROP_IDX the index where tool-bar item
12044 properties start in F->tool_bar_items. Value is zero if
12045 GLYPH doesn't display a tool-bar item. */
12046
12047 static int
12048 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12049 {
12050 Lisp_Object prop;
12051 int success_p;
12052 int charpos;
12053
12054 /* This function can be called asynchronously, which means we must
12055 exclude any possibility that Fget_text_property signals an
12056 error. */
12057 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12058 charpos = max (0, charpos);
12059
12060 /* Get the text property `menu-item' at pos. The value of that
12061 property is the start index of this item's properties in
12062 F->tool_bar_items. */
12063 prop = Fget_text_property (make_number (charpos),
12064 Qmenu_item, f->current_tool_bar_string);
12065 if (INTEGERP (prop))
12066 {
12067 *prop_idx = XINT (prop);
12068 success_p = 1;
12069 }
12070 else
12071 success_p = 0;
12072
12073 return success_p;
12074 }
12075
12076 \f
12077 /* Get information about the tool-bar item at position X/Y on frame F.
12078 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12079 the current matrix of the tool-bar window of F, or NULL if not
12080 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12081 item in F->tool_bar_items. Value is
12082
12083 -1 if X/Y is not on a tool-bar item
12084 0 if X/Y is on the same item that was highlighted before.
12085 1 otherwise. */
12086
12087 static int
12088 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12089 int *hpos, int *vpos, int *prop_idx)
12090 {
12091 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12092 struct window *w = XWINDOW (f->tool_bar_window);
12093 int area;
12094
12095 /* Find the glyph under X/Y. */
12096 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12097 if (*glyph == NULL)
12098 return -1;
12099
12100 /* Get the start of this tool-bar item's properties in
12101 f->tool_bar_items. */
12102 if (!tool_bar_item_info (f, *glyph, prop_idx))
12103 return -1;
12104
12105 /* Is mouse on the highlighted item? */
12106 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12107 && *vpos >= hlinfo->mouse_face_beg_row
12108 && *vpos <= hlinfo->mouse_face_end_row
12109 && (*vpos > hlinfo->mouse_face_beg_row
12110 || *hpos >= hlinfo->mouse_face_beg_col)
12111 && (*vpos < hlinfo->mouse_face_end_row
12112 || *hpos < hlinfo->mouse_face_end_col
12113 || hlinfo->mouse_face_past_end))
12114 return 0;
12115
12116 return 1;
12117 }
12118
12119
12120 /* EXPORT:
12121 Handle mouse button event on the tool-bar of frame F, at
12122 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12123 0 for button release. MODIFIERS is event modifiers for button
12124 release. */
12125
12126 void
12127 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12128 int modifiers)
12129 {
12130 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12131 struct window *w = XWINDOW (f->tool_bar_window);
12132 int hpos, vpos, prop_idx;
12133 struct glyph *glyph;
12134 Lisp_Object enabled_p;
12135 int ts;
12136
12137 /* If not on the highlighted tool-bar item, and mouse-highlight is
12138 non-nil, return. This is so we generate the tool-bar button
12139 click only when the mouse button is released on the same item as
12140 where it was pressed. However, when mouse-highlight is disabled,
12141 generate the click when the button is released regardless of the
12142 highlight, since tool-bar items are not highlighted in that
12143 case. */
12144 frame_to_window_pixel_xy (w, &x, &y);
12145 ts = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12146 if (ts == -1
12147 || (ts != 0 && !NILP (Vmouse_highlight)))
12148 return;
12149
12150 /* When mouse-highlight is off, generate the click for the item
12151 where the button was pressed, disregarding where it was
12152 released. */
12153 if (NILP (Vmouse_highlight) && !down_p)
12154 prop_idx = last_tool_bar_item;
12155
12156 /* If item is disabled, do nothing. */
12157 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12158 if (NILP (enabled_p))
12159 return;
12160
12161 if (down_p)
12162 {
12163 /* Show item in pressed state. */
12164 if (!NILP (Vmouse_highlight))
12165 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12166 last_tool_bar_item = prop_idx;
12167 }
12168 else
12169 {
12170 Lisp_Object key, frame;
12171 struct input_event event;
12172 EVENT_INIT (event);
12173
12174 /* Show item in released state. */
12175 if (!NILP (Vmouse_highlight))
12176 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12177
12178 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12179
12180 XSETFRAME (frame, f);
12181 event.kind = TOOL_BAR_EVENT;
12182 event.frame_or_window = frame;
12183 event.arg = frame;
12184 kbd_buffer_store_event (&event);
12185
12186 event.kind = TOOL_BAR_EVENT;
12187 event.frame_or_window = frame;
12188 event.arg = key;
12189 event.modifiers = modifiers;
12190 kbd_buffer_store_event (&event);
12191 last_tool_bar_item = -1;
12192 }
12193 }
12194
12195
12196 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12197 tool-bar window-relative coordinates X/Y. Called from
12198 note_mouse_highlight. */
12199
12200 static void
12201 note_tool_bar_highlight (struct frame *f, int x, int y)
12202 {
12203 Lisp_Object window = f->tool_bar_window;
12204 struct window *w = XWINDOW (window);
12205 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12206 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12207 int hpos, vpos;
12208 struct glyph *glyph;
12209 struct glyph_row *row;
12210 int i;
12211 Lisp_Object enabled_p;
12212 int prop_idx;
12213 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12214 int mouse_down_p, rc;
12215
12216 /* Function note_mouse_highlight is called with negative X/Y
12217 values when mouse moves outside of the frame. */
12218 if (x <= 0 || y <= 0)
12219 {
12220 clear_mouse_face (hlinfo);
12221 return;
12222 }
12223
12224 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12225 if (rc < 0)
12226 {
12227 /* Not on tool-bar item. */
12228 clear_mouse_face (hlinfo);
12229 return;
12230 }
12231 else if (rc == 0)
12232 /* On same tool-bar item as before. */
12233 goto set_help_echo;
12234
12235 clear_mouse_face (hlinfo);
12236
12237 /* Mouse is down, but on different tool-bar item? */
12238 mouse_down_p = (dpyinfo->grabbed
12239 && f == last_mouse_frame
12240 && FRAME_LIVE_P (f));
12241 if (mouse_down_p
12242 && last_tool_bar_item != prop_idx)
12243 return;
12244
12245 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12246
12247 /* If tool-bar item is not enabled, don't highlight it. */
12248 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12249 if (!NILP (enabled_p) && !NILP (Vmouse_highlight))
12250 {
12251 /* Compute the x-position of the glyph. In front and past the
12252 image is a space. We include this in the highlighted area. */
12253 row = MATRIX_ROW (w->current_matrix, vpos);
12254 for (i = x = 0; i < hpos; ++i)
12255 x += row->glyphs[TEXT_AREA][i].pixel_width;
12256
12257 /* Record this as the current active region. */
12258 hlinfo->mouse_face_beg_col = hpos;
12259 hlinfo->mouse_face_beg_row = vpos;
12260 hlinfo->mouse_face_beg_x = x;
12261 hlinfo->mouse_face_beg_y = row->y;
12262 hlinfo->mouse_face_past_end = 0;
12263
12264 hlinfo->mouse_face_end_col = hpos + 1;
12265 hlinfo->mouse_face_end_row = vpos;
12266 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12267 hlinfo->mouse_face_end_y = row->y;
12268 hlinfo->mouse_face_window = window;
12269 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12270
12271 /* Display it as active. */
12272 show_mouse_face (hlinfo, draw);
12273 }
12274
12275 set_help_echo:
12276
12277 /* Set help_echo_string to a help string to display for this tool-bar item.
12278 XTread_socket does the rest. */
12279 help_echo_object = help_echo_window = Qnil;
12280 help_echo_pos = -1;
12281 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12282 if (NILP (help_echo_string))
12283 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12284 }
12285
12286 #endif /* HAVE_WINDOW_SYSTEM */
12287
12288
12289 \f
12290 /************************************************************************
12291 Horizontal scrolling
12292 ************************************************************************/
12293
12294 static int hscroll_window_tree (Lisp_Object);
12295 static int hscroll_windows (Lisp_Object);
12296
12297 /* For all leaf windows in the window tree rooted at WINDOW, set their
12298 hscroll value so that PT is (i) visible in the window, and (ii) so
12299 that it is not within a certain margin at the window's left and
12300 right border. Value is non-zero if any window's hscroll has been
12301 changed. */
12302
12303 static int
12304 hscroll_window_tree (Lisp_Object window)
12305 {
12306 int hscrolled_p = 0;
12307 int hscroll_relative_p = FLOATP (Vhscroll_step);
12308 int hscroll_step_abs = 0;
12309 double hscroll_step_rel = 0;
12310
12311 if (hscroll_relative_p)
12312 {
12313 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12314 if (hscroll_step_rel < 0)
12315 {
12316 hscroll_relative_p = 0;
12317 hscroll_step_abs = 0;
12318 }
12319 }
12320 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12321 {
12322 hscroll_step_abs = XINT (Vhscroll_step);
12323 if (hscroll_step_abs < 0)
12324 hscroll_step_abs = 0;
12325 }
12326 else
12327 hscroll_step_abs = 0;
12328
12329 while (WINDOWP (window))
12330 {
12331 struct window *w = XWINDOW (window);
12332
12333 if (WINDOWP (w->contents))
12334 hscrolled_p |= hscroll_window_tree (w->contents);
12335 else if (w->cursor.vpos >= 0)
12336 {
12337 int h_margin;
12338 int text_area_width;
12339 struct glyph_row *current_cursor_row
12340 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12341 struct glyph_row *desired_cursor_row
12342 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12343 struct glyph_row *cursor_row
12344 = (desired_cursor_row->enabled_p
12345 ? desired_cursor_row
12346 : current_cursor_row);
12347 int row_r2l_p = cursor_row->reversed_p;
12348
12349 text_area_width = window_box_width (w, TEXT_AREA);
12350
12351 /* Scroll when cursor is inside this scroll margin. */
12352 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12353
12354 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->contents))
12355 /* For left-to-right rows, hscroll when cursor is either
12356 (i) inside the right hscroll margin, or (ii) if it is
12357 inside the left margin and the window is already
12358 hscrolled. */
12359 && ((!row_r2l_p
12360 && ((w->hscroll
12361 && w->cursor.x <= h_margin)
12362 || (cursor_row->enabled_p
12363 && cursor_row->truncated_on_right_p
12364 && (w->cursor.x >= text_area_width - h_margin))))
12365 /* For right-to-left rows, the logic is similar,
12366 except that rules for scrolling to left and right
12367 are reversed. E.g., if cursor.x <= h_margin, we
12368 need to hscroll "to the right" unconditionally,
12369 and that will scroll the screen to the left so as
12370 to reveal the next portion of the row. */
12371 || (row_r2l_p
12372 && ((cursor_row->enabled_p
12373 /* FIXME: It is confusing to set the
12374 truncated_on_right_p flag when R2L rows
12375 are actually truncated on the left. */
12376 && cursor_row->truncated_on_right_p
12377 && w->cursor.x <= h_margin)
12378 || (w->hscroll
12379 && (w->cursor.x >= text_area_width - h_margin))))))
12380 {
12381 struct it it;
12382 ptrdiff_t hscroll;
12383 struct buffer *saved_current_buffer;
12384 ptrdiff_t pt;
12385 int wanted_x;
12386
12387 /* Find point in a display of infinite width. */
12388 saved_current_buffer = current_buffer;
12389 current_buffer = XBUFFER (w->contents);
12390
12391 if (w == XWINDOW (selected_window))
12392 pt = PT;
12393 else
12394 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12395
12396 /* Move iterator to pt starting at cursor_row->start in
12397 a line with infinite width. */
12398 init_to_row_start (&it, w, cursor_row);
12399 it.last_visible_x = INFINITY;
12400 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12401 current_buffer = saved_current_buffer;
12402
12403 /* Position cursor in window. */
12404 if (!hscroll_relative_p && hscroll_step_abs == 0)
12405 hscroll = max (0, (it.current_x
12406 - (ITERATOR_AT_END_OF_LINE_P (&it)
12407 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12408 : (text_area_width / 2))))
12409 / FRAME_COLUMN_WIDTH (it.f);
12410 else if ((!row_r2l_p
12411 && w->cursor.x >= text_area_width - h_margin)
12412 || (row_r2l_p && w->cursor.x <= h_margin))
12413 {
12414 if (hscroll_relative_p)
12415 wanted_x = text_area_width * (1 - hscroll_step_rel)
12416 - h_margin;
12417 else
12418 wanted_x = text_area_width
12419 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12420 - h_margin;
12421 hscroll
12422 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12423 }
12424 else
12425 {
12426 if (hscroll_relative_p)
12427 wanted_x = text_area_width * hscroll_step_rel
12428 + h_margin;
12429 else
12430 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12431 + h_margin;
12432 hscroll
12433 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12434 }
12435 hscroll = max (hscroll, w->min_hscroll);
12436
12437 /* Don't prevent redisplay optimizations if hscroll
12438 hasn't changed, as it will unnecessarily slow down
12439 redisplay. */
12440 if (w->hscroll != hscroll)
12441 {
12442 XBUFFER (w->contents)->prevent_redisplay_optimizations_p = 1;
12443 w->hscroll = hscroll;
12444 hscrolled_p = 1;
12445 }
12446 }
12447 }
12448
12449 window = w->next;
12450 }
12451
12452 /* Value is non-zero if hscroll of any leaf window has been changed. */
12453 return hscrolled_p;
12454 }
12455
12456
12457 /* Set hscroll so that cursor is visible and not inside horizontal
12458 scroll margins for all windows in the tree rooted at WINDOW. See
12459 also hscroll_window_tree above. Value is non-zero if any window's
12460 hscroll has been changed. If it has, desired matrices on the frame
12461 of WINDOW are cleared. */
12462
12463 static int
12464 hscroll_windows (Lisp_Object window)
12465 {
12466 int hscrolled_p = hscroll_window_tree (window);
12467 if (hscrolled_p)
12468 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12469 return hscrolled_p;
12470 }
12471
12472
12473 \f
12474 /************************************************************************
12475 Redisplay
12476 ************************************************************************/
12477
12478 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12479 to a non-zero value. This is sometimes handy to have in a debugger
12480 session. */
12481
12482 #ifdef GLYPH_DEBUG
12483
12484 /* First and last unchanged row for try_window_id. */
12485
12486 static int debug_first_unchanged_at_end_vpos;
12487 static int debug_last_unchanged_at_beg_vpos;
12488
12489 /* Delta vpos and y. */
12490
12491 static int debug_dvpos, debug_dy;
12492
12493 /* Delta in characters and bytes for try_window_id. */
12494
12495 static ptrdiff_t debug_delta, debug_delta_bytes;
12496
12497 /* Values of window_end_pos and window_end_vpos at the end of
12498 try_window_id. */
12499
12500 static ptrdiff_t debug_end_vpos;
12501
12502 /* Append a string to W->desired_matrix->method. FMT is a printf
12503 format string. If trace_redisplay_p is non-zero also printf the
12504 resulting string to stderr. */
12505
12506 static void debug_method_add (struct window *, char const *, ...)
12507 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12508
12509 static void
12510 debug_method_add (struct window *w, char const *fmt, ...)
12511 {
12512 char *method = w->desired_matrix->method;
12513 int len = strlen (method);
12514 int size = sizeof w->desired_matrix->method;
12515 int remaining = size - len - 1;
12516 va_list ap;
12517
12518 if (len && remaining)
12519 {
12520 method[len] = '|';
12521 --remaining, ++len;
12522 }
12523
12524 va_start (ap, fmt);
12525 vsnprintf (method + len, remaining + 1, fmt, ap);
12526 va_end (ap);
12527
12528 if (trace_redisplay_p)
12529 fprintf (stderr, "%p (%s): %s\n",
12530 w,
12531 ((BUFFERP (w->contents)
12532 && STRINGP (BVAR (XBUFFER (w->contents), name)))
12533 ? SSDATA (BVAR (XBUFFER (w->contents), name))
12534 : "no buffer"),
12535 method + len);
12536 }
12537
12538 #endif /* GLYPH_DEBUG */
12539
12540
12541 /* Value is non-zero if all changes in window W, which displays
12542 current_buffer, are in the text between START and END. START is a
12543 buffer position, END is given as a distance from Z. Used in
12544 redisplay_internal for display optimization. */
12545
12546 static int
12547 text_outside_line_unchanged_p (struct window *w,
12548 ptrdiff_t start, ptrdiff_t end)
12549 {
12550 int unchanged_p = 1;
12551
12552 /* If text or overlays have changed, see where. */
12553 if (window_outdated (w))
12554 {
12555 /* Gap in the line? */
12556 if (GPT < start || Z - GPT < end)
12557 unchanged_p = 0;
12558
12559 /* Changes start in front of the line, or end after it? */
12560 if (unchanged_p
12561 && (BEG_UNCHANGED < start - 1
12562 || END_UNCHANGED < end))
12563 unchanged_p = 0;
12564
12565 /* If selective display, can't optimize if changes start at the
12566 beginning of the line. */
12567 if (unchanged_p
12568 && INTEGERP (BVAR (current_buffer, selective_display))
12569 && XINT (BVAR (current_buffer, selective_display)) > 0
12570 && (BEG_UNCHANGED < start || GPT <= start))
12571 unchanged_p = 0;
12572
12573 /* If there are overlays at the start or end of the line, these
12574 may have overlay strings with newlines in them. A change at
12575 START, for instance, may actually concern the display of such
12576 overlay strings as well, and they are displayed on different
12577 lines. So, quickly rule out this case. (For the future, it
12578 might be desirable to implement something more telling than
12579 just BEG/END_UNCHANGED.) */
12580 if (unchanged_p)
12581 {
12582 if (BEG + BEG_UNCHANGED == start
12583 && overlay_touches_p (start))
12584 unchanged_p = 0;
12585 if (END_UNCHANGED == end
12586 && overlay_touches_p (Z - end))
12587 unchanged_p = 0;
12588 }
12589
12590 /* Under bidi reordering, adding or deleting a character in the
12591 beginning of a paragraph, before the first strong directional
12592 character, can change the base direction of the paragraph (unless
12593 the buffer specifies a fixed paragraph direction), which will
12594 require to redisplay the whole paragraph. It might be worthwhile
12595 to find the paragraph limits and widen the range of redisplayed
12596 lines to that, but for now just give up this optimization. */
12597 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
12598 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
12599 unchanged_p = 0;
12600 }
12601
12602 return unchanged_p;
12603 }
12604
12605
12606 /* Do a frame update, taking possible shortcuts into account. This is
12607 the main external entry point for redisplay.
12608
12609 If the last redisplay displayed an echo area message and that message
12610 is no longer requested, we clear the echo area or bring back the
12611 mini-buffer if that is in use. */
12612
12613 void
12614 redisplay (void)
12615 {
12616 redisplay_internal ();
12617 }
12618
12619
12620 static Lisp_Object
12621 overlay_arrow_string_or_property (Lisp_Object var)
12622 {
12623 Lisp_Object val;
12624
12625 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12626 return val;
12627
12628 return Voverlay_arrow_string;
12629 }
12630
12631 /* Return 1 if there are any overlay-arrows in current_buffer. */
12632 static int
12633 overlay_arrow_in_current_buffer_p (void)
12634 {
12635 Lisp_Object vlist;
12636
12637 for (vlist = Voverlay_arrow_variable_list;
12638 CONSP (vlist);
12639 vlist = XCDR (vlist))
12640 {
12641 Lisp_Object var = XCAR (vlist);
12642 Lisp_Object val;
12643
12644 if (!SYMBOLP (var))
12645 continue;
12646 val = find_symbol_value (var);
12647 if (MARKERP (val)
12648 && current_buffer == XMARKER (val)->buffer)
12649 return 1;
12650 }
12651 return 0;
12652 }
12653
12654
12655 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12656 has changed. */
12657
12658 static int
12659 overlay_arrows_changed_p (void)
12660 {
12661 Lisp_Object vlist;
12662
12663 for (vlist = Voverlay_arrow_variable_list;
12664 CONSP (vlist);
12665 vlist = XCDR (vlist))
12666 {
12667 Lisp_Object var = XCAR (vlist);
12668 Lisp_Object val, pstr;
12669
12670 if (!SYMBOLP (var))
12671 continue;
12672 val = find_symbol_value (var);
12673 if (!MARKERP (val))
12674 continue;
12675 if (! EQ (COERCE_MARKER (val),
12676 Fget (var, Qlast_arrow_position))
12677 || ! (pstr = overlay_arrow_string_or_property (var),
12678 EQ (pstr, Fget (var, Qlast_arrow_string))))
12679 return 1;
12680 }
12681 return 0;
12682 }
12683
12684 /* Mark overlay arrows to be updated on next redisplay. */
12685
12686 static void
12687 update_overlay_arrows (int up_to_date)
12688 {
12689 Lisp_Object vlist;
12690
12691 for (vlist = Voverlay_arrow_variable_list;
12692 CONSP (vlist);
12693 vlist = XCDR (vlist))
12694 {
12695 Lisp_Object var = XCAR (vlist);
12696
12697 if (!SYMBOLP (var))
12698 continue;
12699
12700 if (up_to_date > 0)
12701 {
12702 Lisp_Object val = find_symbol_value (var);
12703 Fput (var, Qlast_arrow_position,
12704 COERCE_MARKER (val));
12705 Fput (var, Qlast_arrow_string,
12706 overlay_arrow_string_or_property (var));
12707 }
12708 else if (up_to_date < 0
12709 || !NILP (Fget (var, Qlast_arrow_position)))
12710 {
12711 Fput (var, Qlast_arrow_position, Qt);
12712 Fput (var, Qlast_arrow_string, Qt);
12713 }
12714 }
12715 }
12716
12717
12718 /* Return overlay arrow string to display at row.
12719 Return integer (bitmap number) for arrow bitmap in left fringe.
12720 Return nil if no overlay arrow. */
12721
12722 static Lisp_Object
12723 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12724 {
12725 Lisp_Object vlist;
12726
12727 for (vlist = Voverlay_arrow_variable_list;
12728 CONSP (vlist);
12729 vlist = XCDR (vlist))
12730 {
12731 Lisp_Object var = XCAR (vlist);
12732 Lisp_Object val;
12733
12734 if (!SYMBOLP (var))
12735 continue;
12736
12737 val = find_symbol_value (var);
12738
12739 if (MARKERP (val)
12740 && current_buffer == XMARKER (val)->buffer
12741 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12742 {
12743 if (FRAME_WINDOW_P (it->f)
12744 /* FIXME: if ROW->reversed_p is set, this should test
12745 the right fringe, not the left one. */
12746 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12747 {
12748 #ifdef HAVE_WINDOW_SYSTEM
12749 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12750 {
12751 int fringe_bitmap;
12752 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12753 return make_number (fringe_bitmap);
12754 }
12755 #endif
12756 return make_number (-1); /* Use default arrow bitmap. */
12757 }
12758 return overlay_arrow_string_or_property (var);
12759 }
12760 }
12761
12762 return Qnil;
12763 }
12764
12765 /* Return 1 if point moved out of or into a composition. Otherwise
12766 return 0. PREV_BUF and PREV_PT are the last point buffer and
12767 position. BUF and PT are the current point buffer and position. */
12768
12769 static int
12770 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12771 struct buffer *buf, ptrdiff_t pt)
12772 {
12773 ptrdiff_t start, end;
12774 Lisp_Object prop;
12775 Lisp_Object buffer;
12776
12777 XSETBUFFER (buffer, buf);
12778 /* Check a composition at the last point if point moved within the
12779 same buffer. */
12780 if (prev_buf == buf)
12781 {
12782 if (prev_pt == pt)
12783 /* Point didn't move. */
12784 return 0;
12785
12786 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12787 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12788 && COMPOSITION_VALID_P (start, end, prop)
12789 && start < prev_pt && end > prev_pt)
12790 /* The last point was within the composition. Return 1 iff
12791 point moved out of the composition. */
12792 return (pt <= start || pt >= end);
12793 }
12794
12795 /* Check a composition at the current point. */
12796 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12797 && find_composition (pt, -1, &start, &end, &prop, buffer)
12798 && COMPOSITION_VALID_P (start, end, prop)
12799 && start < pt && end > pt);
12800 }
12801
12802
12803 /* Reconsider the setting of B->clip_changed which is displayed
12804 in window W. */
12805
12806 static void
12807 reconsider_clip_changes (struct window *w, struct buffer *b)
12808 {
12809 if (b->clip_changed
12810 && w->window_end_valid
12811 && w->current_matrix->buffer == b
12812 && w->current_matrix->zv == BUF_ZV (b)
12813 && w->current_matrix->begv == BUF_BEGV (b))
12814 b->clip_changed = 0;
12815
12816 /* If display wasn't paused, and W is not a tool bar window, see if
12817 point has been moved into or out of a composition. In that case,
12818 we set b->clip_changed to 1 to force updating the screen. If
12819 b->clip_changed has already been set to 1, we can skip this
12820 check. */
12821 if (!b->clip_changed && BUFFERP (w->contents) && w->window_end_valid)
12822 {
12823 ptrdiff_t pt;
12824
12825 if (w == XWINDOW (selected_window))
12826 pt = PT;
12827 else
12828 pt = marker_position (w->pointm);
12829
12830 if ((w->current_matrix->buffer != XBUFFER (w->contents)
12831 || pt != w->last_point)
12832 && check_point_in_composition (w->current_matrix->buffer,
12833 w->last_point,
12834 XBUFFER (w->contents), pt))
12835 b->clip_changed = 1;
12836 }
12837 }
12838 \f
12839
12840 #define STOP_POLLING \
12841 do { if (! polling_stopped_here) stop_polling (); \
12842 polling_stopped_here = 1; } while (0)
12843
12844 #define RESUME_POLLING \
12845 do { if (polling_stopped_here) start_polling (); \
12846 polling_stopped_here = 0; } while (0)
12847
12848
12849 /* Perhaps in the future avoid recentering windows if it
12850 is not necessary; currently that causes some problems. */
12851
12852 static void
12853 redisplay_internal (void)
12854 {
12855 struct window *w = XWINDOW (selected_window);
12856 struct window *sw;
12857 struct frame *fr;
12858 int pending;
12859 int must_finish = 0;
12860 struct text_pos tlbufpos, tlendpos;
12861 int number_of_visible_frames;
12862 ptrdiff_t count, count1;
12863 struct frame *sf;
12864 int polling_stopped_here = 0;
12865 Lisp_Object tail, frame;
12866
12867 /* Non-zero means redisplay has to consider all windows on all
12868 frames. Zero means, only selected_window is considered. */
12869 int consider_all_windows_p;
12870
12871 /* Non-zero means redisplay has to redisplay the miniwindow. */
12872 int update_miniwindow_p = 0;
12873
12874 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
12875
12876 /* No redisplay if running in batch mode or frame is not yet fully
12877 initialized, or redisplay is explicitly turned off by setting
12878 Vinhibit_redisplay. */
12879 if (FRAME_INITIAL_P (SELECTED_FRAME ())
12880 || !NILP (Vinhibit_redisplay))
12881 return;
12882
12883 /* Don't examine these until after testing Vinhibit_redisplay.
12884 When Emacs is shutting down, perhaps because its connection to
12885 X has dropped, we should not look at them at all. */
12886 fr = XFRAME (w->frame);
12887 sf = SELECTED_FRAME ();
12888
12889 if (!fr->glyphs_initialized_p)
12890 return;
12891
12892 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
12893 if (popup_activated ())
12894 return;
12895 #endif
12896
12897 /* I don't think this happens but let's be paranoid. */
12898 if (redisplaying_p)
12899 return;
12900
12901 /* Record a function that clears redisplaying_p
12902 when we leave this function. */
12903 count = SPECPDL_INDEX ();
12904 record_unwind_protect (unwind_redisplay, selected_frame);
12905 redisplaying_p = 1;
12906 specbind (Qinhibit_free_realized_faces, Qnil);
12907
12908 /* Record this function, so it appears on the profiler's backtraces. */
12909 record_in_backtrace (Qredisplay_internal, &Qnil, 0);
12910
12911 FOR_EACH_FRAME (tail, frame)
12912 XFRAME (frame)->already_hscrolled_p = 0;
12913
12914 retry:
12915 /* Remember the currently selected window. */
12916 sw = w;
12917
12918 pending = 0;
12919 reconsider_clip_changes (w, current_buffer);
12920 last_escape_glyph_frame = NULL;
12921 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
12922 last_glyphless_glyph_frame = NULL;
12923 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
12924
12925 /* If new fonts have been loaded that make a glyph matrix adjustment
12926 necessary, do it. */
12927 if (fonts_changed_p)
12928 {
12929 adjust_glyphs (NULL);
12930 ++windows_or_buffers_changed;
12931 fonts_changed_p = 0;
12932 }
12933
12934 /* If face_change_count is non-zero, init_iterator will free all
12935 realized faces, which includes the faces referenced from current
12936 matrices. So, we can't reuse current matrices in this case. */
12937 if (face_change_count)
12938 ++windows_or_buffers_changed;
12939
12940 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
12941 && FRAME_TTY (sf)->previous_frame != sf)
12942 {
12943 /* Since frames on a single ASCII terminal share the same
12944 display area, displaying a different frame means redisplay
12945 the whole thing. */
12946 windows_or_buffers_changed++;
12947 SET_FRAME_GARBAGED (sf);
12948 #ifndef DOS_NT
12949 set_tty_color_mode (FRAME_TTY (sf), sf);
12950 #endif
12951 FRAME_TTY (sf)->previous_frame = sf;
12952 }
12953
12954 /* Set the visible flags for all frames. Do this before checking for
12955 resized or garbaged frames; they want to know if their frames are
12956 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
12957 number_of_visible_frames = 0;
12958
12959 FOR_EACH_FRAME (tail, frame)
12960 {
12961 struct frame *f = XFRAME (frame);
12962
12963 if (FRAME_VISIBLE_P (f))
12964 ++number_of_visible_frames;
12965 clear_desired_matrices (f);
12966 }
12967
12968 /* Notice any pending interrupt request to change frame size. */
12969 do_pending_window_change (1);
12970
12971 /* do_pending_window_change could change the selected_window due to
12972 frame resizing which makes the selected window too small. */
12973 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
12974 {
12975 sw = w;
12976 reconsider_clip_changes (w, current_buffer);
12977 }
12978
12979 /* Clear frames marked as garbaged. */
12980 clear_garbaged_frames ();
12981
12982 /* Build menubar and tool-bar items. */
12983 if (NILP (Vmemory_full))
12984 prepare_menu_bars ();
12985
12986 if (windows_or_buffers_changed)
12987 update_mode_lines++;
12988
12989 /* Detect case that we need to write or remove a star in the mode line. */
12990 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
12991 {
12992 w->update_mode_line = 1;
12993 if (buffer_shared_and_changed ())
12994 update_mode_lines++;
12995 }
12996
12997 /* Avoid invocation of point motion hooks by `current_column' below. */
12998 count1 = SPECPDL_INDEX ();
12999 specbind (Qinhibit_point_motion_hooks, Qt);
13000
13001 if (mode_line_update_needed (w))
13002 w->update_mode_line = 1;
13003
13004 unbind_to (count1, Qnil);
13005
13006 consider_all_windows_p = (update_mode_lines
13007 || buffer_shared_and_changed ()
13008 || cursor_type_changed);
13009
13010 /* If specs for an arrow have changed, do thorough redisplay
13011 to ensure we remove any arrow that should no longer exist. */
13012 if (overlay_arrows_changed_p ())
13013 consider_all_windows_p = windows_or_buffers_changed = 1;
13014
13015 /* Normally the message* functions will have already displayed and
13016 updated the echo area, but the frame may have been trashed, or
13017 the update may have been preempted, so display the echo area
13018 again here. Checking message_cleared_p captures the case that
13019 the echo area should be cleared. */
13020 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13021 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13022 || (message_cleared_p
13023 && minibuf_level == 0
13024 /* If the mini-window is currently selected, this means the
13025 echo-area doesn't show through. */
13026 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13027 {
13028 int window_height_changed_p = echo_area_display (0);
13029
13030 if (message_cleared_p)
13031 update_miniwindow_p = 1;
13032
13033 must_finish = 1;
13034
13035 /* If we don't display the current message, don't clear the
13036 message_cleared_p flag, because, if we did, we wouldn't clear
13037 the echo area in the next redisplay which doesn't preserve
13038 the echo area. */
13039 if (!display_last_displayed_message_p)
13040 message_cleared_p = 0;
13041
13042 if (fonts_changed_p)
13043 goto retry;
13044 else if (window_height_changed_p)
13045 {
13046 consider_all_windows_p = 1;
13047 ++update_mode_lines;
13048 ++windows_or_buffers_changed;
13049
13050 /* If window configuration was changed, frames may have been
13051 marked garbaged. Clear them or we will experience
13052 surprises wrt scrolling. */
13053 clear_garbaged_frames ();
13054 }
13055 }
13056 else if (EQ (selected_window, minibuf_window)
13057 && (current_buffer->clip_changed || window_outdated (w))
13058 && resize_mini_window (w, 0))
13059 {
13060 /* Resized active mini-window to fit the size of what it is
13061 showing if its contents might have changed. */
13062 must_finish = 1;
13063 /* FIXME: this causes all frames to be updated, which seems unnecessary
13064 since only the current frame needs to be considered. This function
13065 needs to be rewritten with two variables, consider_all_windows and
13066 consider_all_frames. */
13067 consider_all_windows_p = 1;
13068 ++windows_or_buffers_changed;
13069 ++update_mode_lines;
13070
13071 /* If window configuration was changed, frames may have been
13072 marked garbaged. Clear them or we will experience
13073 surprises wrt scrolling. */
13074 clear_garbaged_frames ();
13075 }
13076
13077 /* If showing the region, and mark has changed, we must redisplay
13078 the whole window. The assignment to this_line_start_pos prevents
13079 the optimization directly below this if-statement. */
13080 if (((!NILP (Vtransient_mark_mode)
13081 && !NILP (BVAR (XBUFFER (w->contents), mark_active)))
13082 != (w->region_showing > 0))
13083 || (w->region_showing
13084 && w->region_showing
13085 != XINT (Fmarker_position (BVAR (XBUFFER (w->contents), mark)))))
13086 CHARPOS (this_line_start_pos) = 0;
13087
13088 /* Optimize the case that only the line containing the cursor in the
13089 selected window has changed. Variables starting with this_ are
13090 set in display_line and record information about the line
13091 containing the cursor. */
13092 tlbufpos = this_line_start_pos;
13093 tlendpos = this_line_end_pos;
13094 if (!consider_all_windows_p
13095 && CHARPOS (tlbufpos) > 0
13096 && !w->update_mode_line
13097 && !current_buffer->clip_changed
13098 && !current_buffer->prevent_redisplay_optimizations_p
13099 && FRAME_VISIBLE_P (XFRAME (w->frame))
13100 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13101 /* Make sure recorded data applies to current buffer, etc. */
13102 && this_line_buffer == current_buffer
13103 && current_buffer == XBUFFER (w->contents)
13104 && !w->force_start
13105 && !w->optional_new_start
13106 /* Point must be on the line that we have info recorded about. */
13107 && PT >= CHARPOS (tlbufpos)
13108 && PT <= Z - CHARPOS (tlendpos)
13109 /* All text outside that line, including its final newline,
13110 must be unchanged. */
13111 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13112 CHARPOS (tlendpos)))
13113 {
13114 if (CHARPOS (tlbufpos) > BEGV
13115 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13116 && (CHARPOS (tlbufpos) == ZV
13117 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13118 /* Former continuation line has disappeared by becoming empty. */
13119 goto cancel;
13120 else if (window_outdated (w) || MINI_WINDOW_P (w))
13121 {
13122 /* We have to handle the case of continuation around a
13123 wide-column character (see the comment in indent.c around
13124 line 1340).
13125
13126 For instance, in the following case:
13127
13128 -------- Insert --------
13129 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13130 J_I_ ==> J_I_ `^^' are cursors.
13131 ^^ ^^
13132 -------- --------
13133
13134 As we have to redraw the line above, we cannot use this
13135 optimization. */
13136
13137 struct it it;
13138 int line_height_before = this_line_pixel_height;
13139
13140 /* Note that start_display will handle the case that the
13141 line starting at tlbufpos is a continuation line. */
13142 start_display (&it, w, tlbufpos);
13143
13144 /* Implementation note: It this still necessary? */
13145 if (it.current_x != this_line_start_x)
13146 goto cancel;
13147
13148 TRACE ((stderr, "trying display optimization 1\n"));
13149 w->cursor.vpos = -1;
13150 overlay_arrow_seen = 0;
13151 it.vpos = this_line_vpos;
13152 it.current_y = this_line_y;
13153 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13154 display_line (&it);
13155
13156 /* If line contains point, is not continued,
13157 and ends at same distance from eob as before, we win. */
13158 if (w->cursor.vpos >= 0
13159 /* Line is not continued, otherwise this_line_start_pos
13160 would have been set to 0 in display_line. */
13161 && CHARPOS (this_line_start_pos)
13162 /* Line ends as before. */
13163 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13164 /* Line has same height as before. Otherwise other lines
13165 would have to be shifted up or down. */
13166 && this_line_pixel_height == line_height_before)
13167 {
13168 /* If this is not the window's last line, we must adjust
13169 the charstarts of the lines below. */
13170 if (it.current_y < it.last_visible_y)
13171 {
13172 struct glyph_row *row
13173 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13174 ptrdiff_t delta, delta_bytes;
13175
13176 /* We used to distinguish between two cases here,
13177 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13178 when the line ends in a newline or the end of the
13179 buffer's accessible portion. But both cases did
13180 the same, so they were collapsed. */
13181 delta = (Z
13182 - CHARPOS (tlendpos)
13183 - MATRIX_ROW_START_CHARPOS (row));
13184 delta_bytes = (Z_BYTE
13185 - BYTEPOS (tlendpos)
13186 - MATRIX_ROW_START_BYTEPOS (row));
13187
13188 increment_matrix_positions (w->current_matrix,
13189 this_line_vpos + 1,
13190 w->current_matrix->nrows,
13191 delta, delta_bytes);
13192 }
13193
13194 /* If this row displays text now but previously didn't,
13195 or vice versa, w->window_end_vpos may have to be
13196 adjusted. */
13197 if (MATRIX_ROW_DISPLAYS_TEXT_P (it.glyph_row - 1))
13198 {
13199 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13200 wset_window_end_vpos (w, make_number (this_line_vpos));
13201 }
13202 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13203 && this_line_vpos > 0)
13204 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13205 w->window_end_valid = 0;
13206
13207 /* Update hint: No need to try to scroll in update_window. */
13208 w->desired_matrix->no_scrolling_p = 1;
13209
13210 #ifdef GLYPH_DEBUG
13211 *w->desired_matrix->method = 0;
13212 debug_method_add (w, "optimization 1");
13213 #endif
13214 #ifdef HAVE_WINDOW_SYSTEM
13215 update_window_fringes (w, 0);
13216 #endif
13217 goto update;
13218 }
13219 else
13220 goto cancel;
13221 }
13222 else if (/* Cursor position hasn't changed. */
13223 PT == w->last_point
13224 /* Make sure the cursor was last displayed
13225 in this window. Otherwise we have to reposition it. */
13226 && 0 <= w->cursor.vpos
13227 && w->cursor.vpos < WINDOW_TOTAL_LINES (w))
13228 {
13229 if (!must_finish)
13230 {
13231 do_pending_window_change (1);
13232 /* If selected_window changed, redisplay again. */
13233 if (WINDOWP (selected_window)
13234 && (w = XWINDOW (selected_window)) != sw)
13235 goto retry;
13236
13237 /* We used to always goto end_of_redisplay here, but this
13238 isn't enough if we have a blinking cursor. */
13239 if (w->cursor_off_p == w->last_cursor_off_p)
13240 goto end_of_redisplay;
13241 }
13242 goto update;
13243 }
13244 /* If highlighting the region, or if the cursor is in the echo area,
13245 then we can't just move the cursor. */
13246 else if (! (!NILP (Vtransient_mark_mode)
13247 && !NILP (BVAR (current_buffer, mark_active)))
13248 && (EQ (selected_window,
13249 BVAR (current_buffer, last_selected_window))
13250 || highlight_nonselected_windows)
13251 && !w->region_showing
13252 && NILP (Vshow_trailing_whitespace)
13253 && !cursor_in_echo_area)
13254 {
13255 struct it it;
13256 struct glyph_row *row;
13257
13258 /* Skip from tlbufpos to PT and see where it is. Note that
13259 PT may be in invisible text. If so, we will end at the
13260 next visible position. */
13261 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13262 NULL, DEFAULT_FACE_ID);
13263 it.current_x = this_line_start_x;
13264 it.current_y = this_line_y;
13265 it.vpos = this_line_vpos;
13266
13267 /* The call to move_it_to stops in front of PT, but
13268 moves over before-strings. */
13269 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13270
13271 if (it.vpos == this_line_vpos
13272 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13273 row->enabled_p))
13274 {
13275 eassert (this_line_vpos == it.vpos);
13276 eassert (this_line_y == it.current_y);
13277 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13278 #ifdef GLYPH_DEBUG
13279 *w->desired_matrix->method = 0;
13280 debug_method_add (w, "optimization 3");
13281 #endif
13282 goto update;
13283 }
13284 else
13285 goto cancel;
13286 }
13287
13288 cancel:
13289 /* Text changed drastically or point moved off of line. */
13290 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13291 }
13292
13293 CHARPOS (this_line_start_pos) = 0;
13294 consider_all_windows_p |= buffer_shared_and_changed ();
13295 ++clear_face_cache_count;
13296 #ifdef HAVE_WINDOW_SYSTEM
13297 ++clear_image_cache_count;
13298 #endif
13299
13300 /* Build desired matrices, and update the display. If
13301 consider_all_windows_p is non-zero, do it for all windows on all
13302 frames. Otherwise do it for selected_window, only. */
13303
13304 if (consider_all_windows_p)
13305 {
13306 FOR_EACH_FRAME (tail, frame)
13307 XFRAME (frame)->updated_p = 0;
13308
13309 FOR_EACH_FRAME (tail, frame)
13310 {
13311 struct frame *f = XFRAME (frame);
13312
13313 /* We don't have to do anything for unselected terminal
13314 frames. */
13315 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13316 && !EQ (FRAME_TTY (f)->top_frame, frame))
13317 continue;
13318
13319 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13320 {
13321 /* Mark all the scroll bars to be removed; we'll redeem
13322 the ones we want when we redisplay their windows. */
13323 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13324 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13325
13326 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13327 redisplay_windows (FRAME_ROOT_WINDOW (f));
13328
13329 /* The X error handler may have deleted that frame. */
13330 if (!FRAME_LIVE_P (f))
13331 continue;
13332
13333 /* Any scroll bars which redisplay_windows should have
13334 nuked should now go away. */
13335 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13336 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13337
13338 /* If fonts changed, display again. */
13339 /* ??? rms: I suspect it is a mistake to jump all the way
13340 back to retry here. It should just retry this frame. */
13341 if (fonts_changed_p)
13342 goto retry;
13343
13344 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13345 {
13346 /* See if we have to hscroll. */
13347 if (!f->already_hscrolled_p)
13348 {
13349 f->already_hscrolled_p = 1;
13350 if (hscroll_windows (f->root_window))
13351 goto retry;
13352 }
13353
13354 /* Prevent various kinds of signals during display
13355 update. stdio is not robust about handling
13356 signals, which can cause an apparent I/O
13357 error. */
13358 if (interrupt_input)
13359 unrequest_sigio ();
13360 STOP_POLLING;
13361
13362 /* Update the display. */
13363 set_window_update_flags (XWINDOW (f->root_window), 1);
13364 pending |= update_frame (f, 0, 0);
13365 f->updated_p = 1;
13366 }
13367 }
13368 }
13369
13370 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13371
13372 if (!pending)
13373 {
13374 /* Do the mark_window_display_accurate after all windows have
13375 been redisplayed because this call resets flags in buffers
13376 which are needed for proper redisplay. */
13377 FOR_EACH_FRAME (tail, frame)
13378 {
13379 struct frame *f = XFRAME (frame);
13380 if (f->updated_p)
13381 {
13382 mark_window_display_accurate (f->root_window, 1);
13383 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13384 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13385 }
13386 }
13387 }
13388 }
13389 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13390 {
13391 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13392 struct frame *mini_frame;
13393
13394 displayed_buffer = XBUFFER (XWINDOW (selected_window)->contents);
13395 /* Use list_of_error, not Qerror, so that
13396 we catch only errors and don't run the debugger. */
13397 internal_condition_case_1 (redisplay_window_1, selected_window,
13398 list_of_error,
13399 redisplay_window_error);
13400 if (update_miniwindow_p)
13401 internal_condition_case_1 (redisplay_window_1, mini_window,
13402 list_of_error,
13403 redisplay_window_error);
13404
13405 /* Compare desired and current matrices, perform output. */
13406
13407 update:
13408 /* If fonts changed, display again. */
13409 if (fonts_changed_p)
13410 goto retry;
13411
13412 /* Prevent various kinds of signals during display update.
13413 stdio is not robust about handling signals,
13414 which can cause an apparent I/O error. */
13415 if (interrupt_input)
13416 unrequest_sigio ();
13417 STOP_POLLING;
13418
13419 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13420 {
13421 if (hscroll_windows (selected_window))
13422 goto retry;
13423
13424 XWINDOW (selected_window)->must_be_updated_p = 1;
13425 pending = update_frame (sf, 0, 0);
13426 }
13427
13428 /* We may have called echo_area_display at the top of this
13429 function. If the echo area is on another frame, that may
13430 have put text on a frame other than the selected one, so the
13431 above call to update_frame would not have caught it. Catch
13432 it here. */
13433 mini_window = FRAME_MINIBUF_WINDOW (sf);
13434 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13435
13436 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13437 {
13438 XWINDOW (mini_window)->must_be_updated_p = 1;
13439 pending |= update_frame (mini_frame, 0, 0);
13440 if (!pending && hscroll_windows (mini_window))
13441 goto retry;
13442 }
13443 }
13444
13445 /* If display was paused because of pending input, make sure we do a
13446 thorough update the next time. */
13447 if (pending)
13448 {
13449 /* Prevent the optimization at the beginning of
13450 redisplay_internal that tries a single-line update of the
13451 line containing the cursor in the selected window. */
13452 CHARPOS (this_line_start_pos) = 0;
13453
13454 /* Let the overlay arrow be updated the next time. */
13455 update_overlay_arrows (0);
13456
13457 /* If we pause after scrolling, some rows in the current
13458 matrices of some windows are not valid. */
13459 if (!WINDOW_FULL_WIDTH_P (w)
13460 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13461 update_mode_lines = 1;
13462 }
13463 else
13464 {
13465 if (!consider_all_windows_p)
13466 {
13467 /* This has already been done above if
13468 consider_all_windows_p is set. */
13469 mark_window_display_accurate_1 (w, 1);
13470
13471 /* Say overlay arrows are up to date. */
13472 update_overlay_arrows (1);
13473
13474 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13475 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13476 }
13477
13478 update_mode_lines = 0;
13479 windows_or_buffers_changed = 0;
13480 cursor_type_changed = 0;
13481 }
13482
13483 /* Start SIGIO interrupts coming again. Having them off during the
13484 code above makes it less likely one will discard output, but not
13485 impossible, since there might be stuff in the system buffer here.
13486 But it is much hairier to try to do anything about that. */
13487 if (interrupt_input)
13488 request_sigio ();
13489 RESUME_POLLING;
13490
13491 /* If a frame has become visible which was not before, redisplay
13492 again, so that we display it. Expose events for such a frame
13493 (which it gets when becoming visible) don't call the parts of
13494 redisplay constructing glyphs, so simply exposing a frame won't
13495 display anything in this case. So, we have to display these
13496 frames here explicitly. */
13497 if (!pending)
13498 {
13499 int new_count = 0;
13500
13501 FOR_EACH_FRAME (tail, frame)
13502 {
13503 int this_is_visible = 0;
13504
13505 if (XFRAME (frame)->visible)
13506 this_is_visible = 1;
13507
13508 if (this_is_visible)
13509 new_count++;
13510 }
13511
13512 if (new_count != number_of_visible_frames)
13513 windows_or_buffers_changed++;
13514 }
13515
13516 /* Change frame size now if a change is pending. */
13517 do_pending_window_change (1);
13518
13519 /* If we just did a pending size change, or have additional
13520 visible frames, or selected_window changed, redisplay again. */
13521 if ((windows_or_buffers_changed && !pending)
13522 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13523 goto retry;
13524
13525 /* Clear the face and image caches.
13526
13527 We used to do this only if consider_all_windows_p. But the cache
13528 needs to be cleared if a timer creates images in the current
13529 buffer (e.g. the test case in Bug#6230). */
13530
13531 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13532 {
13533 clear_face_cache (0);
13534 clear_face_cache_count = 0;
13535 }
13536
13537 #ifdef HAVE_WINDOW_SYSTEM
13538 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13539 {
13540 clear_image_caches (Qnil);
13541 clear_image_cache_count = 0;
13542 }
13543 #endif /* HAVE_WINDOW_SYSTEM */
13544
13545 end_of_redisplay:
13546 unbind_to (count, Qnil);
13547 RESUME_POLLING;
13548 }
13549
13550
13551 /* Redisplay, but leave alone any recent echo area message unless
13552 another message has been requested in its place.
13553
13554 This is useful in situations where you need to redisplay but no
13555 user action has occurred, making it inappropriate for the message
13556 area to be cleared. See tracking_off and
13557 wait_reading_process_output for examples of these situations.
13558
13559 FROM_WHERE is an integer saying from where this function was
13560 called. This is useful for debugging. */
13561
13562 void
13563 redisplay_preserve_echo_area (int from_where)
13564 {
13565 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13566
13567 if (!NILP (echo_area_buffer[1]))
13568 {
13569 /* We have a previously displayed message, but no current
13570 message. Redisplay the previous message. */
13571 display_last_displayed_message_p = 1;
13572 redisplay_internal ();
13573 display_last_displayed_message_p = 0;
13574 }
13575 else
13576 redisplay_internal ();
13577
13578 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13579 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13580 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13581 }
13582
13583
13584 /* Function registered with record_unwind_protect in redisplay_internal.
13585 Clear redisplaying_p. Also select the previously selected frame. */
13586
13587 static Lisp_Object
13588 unwind_redisplay (Lisp_Object old_frame)
13589 {
13590 redisplaying_p = 0;
13591 return Qnil;
13592 }
13593
13594
13595 /* Mark the display of leaf window W as accurate or inaccurate.
13596 If ACCURATE_P is non-zero mark display of W as accurate. If
13597 ACCURATE_P is zero, arrange for W to be redisplayed the next
13598 time redisplay_internal is called. */
13599
13600 static void
13601 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13602 {
13603 struct buffer *b = XBUFFER (w->contents);
13604
13605 w->last_modified = accurate_p ? BUF_MODIFF (b) : 0;
13606 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF (b) : 0;
13607 w->last_had_star = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13608
13609 if (accurate_p)
13610 {
13611 b->clip_changed = 0;
13612 b->prevent_redisplay_optimizations_p = 0;
13613
13614 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13615 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13616 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13617 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13618
13619 w->current_matrix->buffer = b;
13620 w->current_matrix->begv = BUF_BEGV (b);
13621 w->current_matrix->zv = BUF_ZV (b);
13622
13623 w->last_cursor = w->cursor;
13624 w->last_cursor_off_p = w->cursor_off_p;
13625
13626 if (w == XWINDOW (selected_window))
13627 w->last_point = BUF_PT (b);
13628 else
13629 w->last_point = marker_position (w->pointm);
13630
13631 w->window_end_valid = 1;
13632 w->update_mode_line = 0;
13633 }
13634 }
13635
13636
13637 /* Mark the display of windows in the window tree rooted at WINDOW as
13638 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13639 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13640 be redisplayed the next time redisplay_internal is called. */
13641
13642 void
13643 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13644 {
13645 struct window *w;
13646
13647 for (; !NILP (window); window = w->next)
13648 {
13649 w = XWINDOW (window);
13650 if (WINDOWP (w->contents))
13651 mark_window_display_accurate (w->contents, accurate_p);
13652 else
13653 mark_window_display_accurate_1 (w, accurate_p);
13654 }
13655
13656 if (accurate_p)
13657 update_overlay_arrows (1);
13658 else
13659 /* Force a thorough redisplay the next time by setting
13660 last_arrow_position and last_arrow_string to t, which is
13661 unequal to any useful value of Voverlay_arrow_... */
13662 update_overlay_arrows (-1);
13663 }
13664
13665
13666 /* Return value in display table DP (Lisp_Char_Table *) for character
13667 C. Since a display table doesn't have any parent, we don't have to
13668 follow parent. Do not call this function directly but use the
13669 macro DISP_CHAR_VECTOR. */
13670
13671 Lisp_Object
13672 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13673 {
13674 Lisp_Object val;
13675
13676 if (ASCII_CHAR_P (c))
13677 {
13678 val = dp->ascii;
13679 if (SUB_CHAR_TABLE_P (val))
13680 val = XSUB_CHAR_TABLE (val)->contents[c];
13681 }
13682 else
13683 {
13684 Lisp_Object table;
13685
13686 XSETCHAR_TABLE (table, dp);
13687 val = char_table_ref (table, c);
13688 }
13689 if (NILP (val))
13690 val = dp->defalt;
13691 return val;
13692 }
13693
13694
13695 \f
13696 /***********************************************************************
13697 Window Redisplay
13698 ***********************************************************************/
13699
13700 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13701
13702 static void
13703 redisplay_windows (Lisp_Object window)
13704 {
13705 while (!NILP (window))
13706 {
13707 struct window *w = XWINDOW (window);
13708
13709 if (WINDOWP (w->contents))
13710 redisplay_windows (w->contents);
13711 else if (BUFFERP (w->contents))
13712 {
13713 displayed_buffer = XBUFFER (w->contents);
13714 /* Use list_of_error, not Qerror, so that
13715 we catch only errors and don't run the debugger. */
13716 internal_condition_case_1 (redisplay_window_0, window,
13717 list_of_error,
13718 redisplay_window_error);
13719 }
13720
13721 window = w->next;
13722 }
13723 }
13724
13725 static Lisp_Object
13726 redisplay_window_error (Lisp_Object ignore)
13727 {
13728 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13729 return Qnil;
13730 }
13731
13732 static Lisp_Object
13733 redisplay_window_0 (Lisp_Object window)
13734 {
13735 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13736 redisplay_window (window, 0);
13737 return Qnil;
13738 }
13739
13740 static Lisp_Object
13741 redisplay_window_1 (Lisp_Object window)
13742 {
13743 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13744 redisplay_window (window, 1);
13745 return Qnil;
13746 }
13747 \f
13748
13749 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13750 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13751 which positions recorded in ROW differ from current buffer
13752 positions.
13753
13754 Return 0 if cursor is not on this row, 1 otherwise. */
13755
13756 static int
13757 set_cursor_from_row (struct window *w, struct glyph_row *row,
13758 struct glyph_matrix *matrix,
13759 ptrdiff_t delta, ptrdiff_t delta_bytes,
13760 int dy, int dvpos)
13761 {
13762 struct glyph *glyph = row->glyphs[TEXT_AREA];
13763 struct glyph *end = glyph + row->used[TEXT_AREA];
13764 struct glyph *cursor = NULL;
13765 /* The last known character position in row. */
13766 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13767 int x = row->x;
13768 ptrdiff_t pt_old = PT - delta;
13769 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13770 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13771 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13772 /* A glyph beyond the edge of TEXT_AREA which we should never
13773 touch. */
13774 struct glyph *glyphs_end = end;
13775 /* Non-zero means we've found a match for cursor position, but that
13776 glyph has the avoid_cursor_p flag set. */
13777 int match_with_avoid_cursor = 0;
13778 /* Non-zero means we've seen at least one glyph that came from a
13779 display string. */
13780 int string_seen = 0;
13781 /* Largest and smallest buffer positions seen so far during scan of
13782 glyph row. */
13783 ptrdiff_t bpos_max = pos_before;
13784 ptrdiff_t bpos_min = pos_after;
13785 /* Last buffer position covered by an overlay string with an integer
13786 `cursor' property. */
13787 ptrdiff_t bpos_covered = 0;
13788 /* Non-zero means the display string on which to display the cursor
13789 comes from a text property, not from an overlay. */
13790 int string_from_text_prop = 0;
13791
13792 /* Don't even try doing anything if called for a mode-line or
13793 header-line row, since the rest of the code isn't prepared to
13794 deal with such calamities. */
13795 eassert (!row->mode_line_p);
13796 if (row->mode_line_p)
13797 return 0;
13798
13799 /* Skip over glyphs not having an object at the start and the end of
13800 the row. These are special glyphs like truncation marks on
13801 terminal frames. */
13802 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
13803 {
13804 if (!row->reversed_p)
13805 {
13806 while (glyph < end
13807 && INTEGERP (glyph->object)
13808 && glyph->charpos < 0)
13809 {
13810 x += glyph->pixel_width;
13811 ++glyph;
13812 }
13813 while (end > glyph
13814 && INTEGERP ((end - 1)->object)
13815 /* CHARPOS is zero for blanks and stretch glyphs
13816 inserted by extend_face_to_end_of_line. */
13817 && (end - 1)->charpos <= 0)
13818 --end;
13819 glyph_before = glyph - 1;
13820 glyph_after = end;
13821 }
13822 else
13823 {
13824 struct glyph *g;
13825
13826 /* If the glyph row is reversed, we need to process it from back
13827 to front, so swap the edge pointers. */
13828 glyphs_end = end = glyph - 1;
13829 glyph += row->used[TEXT_AREA] - 1;
13830
13831 while (glyph > end + 1
13832 && INTEGERP (glyph->object)
13833 && glyph->charpos < 0)
13834 {
13835 --glyph;
13836 x -= glyph->pixel_width;
13837 }
13838 if (INTEGERP (glyph->object) && glyph->charpos < 0)
13839 --glyph;
13840 /* By default, in reversed rows we put the cursor on the
13841 rightmost (first in the reading order) glyph. */
13842 for (g = end + 1; g < glyph; g++)
13843 x += g->pixel_width;
13844 while (end < glyph
13845 && INTEGERP ((end + 1)->object)
13846 && (end + 1)->charpos <= 0)
13847 ++end;
13848 glyph_before = glyph + 1;
13849 glyph_after = end;
13850 }
13851 }
13852 else if (row->reversed_p)
13853 {
13854 /* In R2L rows that don't display text, put the cursor on the
13855 rightmost glyph. Case in point: an empty last line that is
13856 part of an R2L paragraph. */
13857 cursor = end - 1;
13858 /* Avoid placing the cursor on the last glyph of the row, where
13859 on terminal frames we hold the vertical border between
13860 adjacent windows. */
13861 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
13862 && !WINDOW_RIGHTMOST_P (w)
13863 && cursor == row->glyphs[LAST_AREA] - 1)
13864 cursor--;
13865 x = -1; /* will be computed below, at label compute_x */
13866 }
13867
13868 /* Step 1: Try to find the glyph whose character position
13869 corresponds to point. If that's not possible, find 2 glyphs
13870 whose character positions are the closest to point, one before
13871 point, the other after it. */
13872 if (!row->reversed_p)
13873 while (/* not marched to end of glyph row */
13874 glyph < end
13875 /* glyph was not inserted by redisplay for internal purposes */
13876 && !INTEGERP (glyph->object))
13877 {
13878 if (BUFFERP (glyph->object))
13879 {
13880 ptrdiff_t dpos = glyph->charpos - pt_old;
13881
13882 if (glyph->charpos > bpos_max)
13883 bpos_max = glyph->charpos;
13884 if (glyph->charpos < bpos_min)
13885 bpos_min = glyph->charpos;
13886 if (!glyph->avoid_cursor_p)
13887 {
13888 /* If we hit point, we've found the glyph on which to
13889 display the cursor. */
13890 if (dpos == 0)
13891 {
13892 match_with_avoid_cursor = 0;
13893 break;
13894 }
13895 /* See if we've found a better approximation to
13896 POS_BEFORE or to POS_AFTER. */
13897 if (0 > dpos && dpos > pos_before - pt_old)
13898 {
13899 pos_before = glyph->charpos;
13900 glyph_before = glyph;
13901 }
13902 else if (0 < dpos && dpos < pos_after - pt_old)
13903 {
13904 pos_after = glyph->charpos;
13905 glyph_after = glyph;
13906 }
13907 }
13908 else if (dpos == 0)
13909 match_with_avoid_cursor = 1;
13910 }
13911 else if (STRINGP (glyph->object))
13912 {
13913 Lisp_Object chprop;
13914 ptrdiff_t glyph_pos = glyph->charpos;
13915
13916 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
13917 glyph->object);
13918 if (!NILP (chprop))
13919 {
13920 /* If the string came from a `display' text property,
13921 look up the buffer position of that property and
13922 use that position to update bpos_max, as if we
13923 actually saw such a position in one of the row's
13924 glyphs. This helps with supporting integer values
13925 of `cursor' property on the display string in
13926 situations where most or all of the row's buffer
13927 text is completely covered by display properties,
13928 so that no glyph with valid buffer positions is
13929 ever seen in the row. */
13930 ptrdiff_t prop_pos =
13931 string_buffer_position_lim (glyph->object, pos_before,
13932 pos_after, 0);
13933
13934 if (prop_pos >= pos_before)
13935 bpos_max = prop_pos - 1;
13936 }
13937 if (INTEGERP (chprop))
13938 {
13939 bpos_covered = bpos_max + XINT (chprop);
13940 /* If the `cursor' property covers buffer positions up
13941 to and including point, we should display cursor on
13942 this glyph. Note that, if a `cursor' property on one
13943 of the string's characters has an integer value, we
13944 will break out of the loop below _before_ we get to
13945 the position match above. IOW, integer values of
13946 the `cursor' property override the "exact match for
13947 point" strategy of positioning the cursor. */
13948 /* Implementation note: bpos_max == pt_old when, e.g.,
13949 we are in an empty line, where bpos_max is set to
13950 MATRIX_ROW_START_CHARPOS, see above. */
13951 if (bpos_max <= pt_old && bpos_covered >= pt_old)
13952 {
13953 cursor = glyph;
13954 break;
13955 }
13956 }
13957
13958 string_seen = 1;
13959 }
13960 x += glyph->pixel_width;
13961 ++glyph;
13962 }
13963 else if (glyph > end) /* row is reversed */
13964 while (!INTEGERP (glyph->object))
13965 {
13966 if (BUFFERP (glyph->object))
13967 {
13968 ptrdiff_t dpos = glyph->charpos - pt_old;
13969
13970 if (glyph->charpos > bpos_max)
13971 bpos_max = glyph->charpos;
13972 if (glyph->charpos < bpos_min)
13973 bpos_min = glyph->charpos;
13974 if (!glyph->avoid_cursor_p)
13975 {
13976 if (dpos == 0)
13977 {
13978 match_with_avoid_cursor = 0;
13979 break;
13980 }
13981 if (0 > dpos && dpos > pos_before - pt_old)
13982 {
13983 pos_before = glyph->charpos;
13984 glyph_before = glyph;
13985 }
13986 else if (0 < dpos && dpos < pos_after - pt_old)
13987 {
13988 pos_after = glyph->charpos;
13989 glyph_after = glyph;
13990 }
13991 }
13992 else if (dpos == 0)
13993 match_with_avoid_cursor = 1;
13994 }
13995 else if (STRINGP (glyph->object))
13996 {
13997 Lisp_Object chprop;
13998 ptrdiff_t glyph_pos = glyph->charpos;
13999
14000 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14001 glyph->object);
14002 if (!NILP (chprop))
14003 {
14004 ptrdiff_t prop_pos =
14005 string_buffer_position_lim (glyph->object, pos_before,
14006 pos_after, 0);
14007
14008 if (prop_pos >= pos_before)
14009 bpos_max = prop_pos - 1;
14010 }
14011 if (INTEGERP (chprop))
14012 {
14013 bpos_covered = bpos_max + XINT (chprop);
14014 /* If the `cursor' property covers buffer positions up
14015 to and including point, we should display cursor on
14016 this glyph. */
14017 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14018 {
14019 cursor = glyph;
14020 break;
14021 }
14022 }
14023 string_seen = 1;
14024 }
14025 --glyph;
14026 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14027 {
14028 x--; /* can't use any pixel_width */
14029 break;
14030 }
14031 x -= glyph->pixel_width;
14032 }
14033
14034 /* Step 2: If we didn't find an exact match for point, we need to
14035 look for a proper place to put the cursor among glyphs between
14036 GLYPH_BEFORE and GLYPH_AFTER. */
14037 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14038 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14039 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14040 {
14041 /* An empty line has a single glyph whose OBJECT is zero and
14042 whose CHARPOS is the position of a newline on that line.
14043 Note that on a TTY, there are more glyphs after that, which
14044 were produced by extend_face_to_end_of_line, but their
14045 CHARPOS is zero or negative. */
14046 int empty_line_p =
14047 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14048 && INTEGERP (glyph->object) && glyph->charpos > 0
14049 /* On a TTY, continued and truncated rows also have a glyph at
14050 their end whose OBJECT is zero and whose CHARPOS is
14051 positive (the continuation and truncation glyphs), but such
14052 rows are obviously not "empty". */
14053 && !(row->continued_p || row->truncated_on_right_p);
14054
14055 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14056 {
14057 ptrdiff_t ellipsis_pos;
14058
14059 /* Scan back over the ellipsis glyphs. */
14060 if (!row->reversed_p)
14061 {
14062 ellipsis_pos = (glyph - 1)->charpos;
14063 while (glyph > row->glyphs[TEXT_AREA]
14064 && (glyph - 1)->charpos == ellipsis_pos)
14065 glyph--, x -= glyph->pixel_width;
14066 /* That loop always goes one position too far, including
14067 the glyph before the ellipsis. So scan forward over
14068 that one. */
14069 x += glyph->pixel_width;
14070 glyph++;
14071 }
14072 else /* row is reversed */
14073 {
14074 ellipsis_pos = (glyph + 1)->charpos;
14075 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14076 && (glyph + 1)->charpos == ellipsis_pos)
14077 glyph++, x += glyph->pixel_width;
14078 x -= glyph->pixel_width;
14079 glyph--;
14080 }
14081 }
14082 else if (match_with_avoid_cursor)
14083 {
14084 cursor = glyph_after;
14085 x = -1;
14086 }
14087 else if (string_seen)
14088 {
14089 int incr = row->reversed_p ? -1 : +1;
14090
14091 /* Need to find the glyph that came out of a string which is
14092 present at point. That glyph is somewhere between
14093 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14094 positioned between POS_BEFORE and POS_AFTER in the
14095 buffer. */
14096 struct glyph *start, *stop;
14097 ptrdiff_t pos = pos_before;
14098
14099 x = -1;
14100
14101 /* If the row ends in a newline from a display string,
14102 reordering could have moved the glyphs belonging to the
14103 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14104 in this case we extend the search to the last glyph in
14105 the row that was not inserted by redisplay. */
14106 if (row->ends_in_newline_from_string_p)
14107 {
14108 glyph_after = end;
14109 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14110 }
14111
14112 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14113 correspond to POS_BEFORE and POS_AFTER, respectively. We
14114 need START and STOP in the order that corresponds to the
14115 row's direction as given by its reversed_p flag. If the
14116 directionality of characters between POS_BEFORE and
14117 POS_AFTER is the opposite of the row's base direction,
14118 these characters will have been reordered for display,
14119 and we need to reverse START and STOP. */
14120 if (!row->reversed_p)
14121 {
14122 start = min (glyph_before, glyph_after);
14123 stop = max (glyph_before, glyph_after);
14124 }
14125 else
14126 {
14127 start = max (glyph_before, glyph_after);
14128 stop = min (glyph_before, glyph_after);
14129 }
14130 for (glyph = start + incr;
14131 row->reversed_p ? glyph > stop : glyph < stop; )
14132 {
14133
14134 /* Any glyphs that come from the buffer are here because
14135 of bidi reordering. Skip them, and only pay
14136 attention to glyphs that came from some string. */
14137 if (STRINGP (glyph->object))
14138 {
14139 Lisp_Object str;
14140 ptrdiff_t tem;
14141 /* If the display property covers the newline, we
14142 need to search for it one position farther. */
14143 ptrdiff_t lim = pos_after
14144 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14145
14146 string_from_text_prop = 0;
14147 str = glyph->object;
14148 tem = string_buffer_position_lim (str, pos, lim, 0);
14149 if (tem == 0 /* from overlay */
14150 || pos <= tem)
14151 {
14152 /* If the string from which this glyph came is
14153 found in the buffer at point, or at position
14154 that is closer to point than pos_after, then
14155 we've found the glyph we've been looking for.
14156 If it comes from an overlay (tem == 0), and
14157 it has the `cursor' property on one of its
14158 glyphs, record that glyph as a candidate for
14159 displaying the cursor. (As in the
14160 unidirectional version, we will display the
14161 cursor on the last candidate we find.) */
14162 if (tem == 0
14163 || tem == pt_old
14164 || (tem - pt_old > 0 && tem < pos_after))
14165 {
14166 /* The glyphs from this string could have
14167 been reordered. Find the one with the
14168 smallest string position. Or there could
14169 be a character in the string with the
14170 `cursor' property, which means display
14171 cursor on that character's glyph. */
14172 ptrdiff_t strpos = glyph->charpos;
14173
14174 if (tem)
14175 {
14176 cursor = glyph;
14177 string_from_text_prop = 1;
14178 }
14179 for ( ;
14180 (row->reversed_p ? glyph > stop : glyph < stop)
14181 && EQ (glyph->object, str);
14182 glyph += incr)
14183 {
14184 Lisp_Object cprop;
14185 ptrdiff_t gpos = glyph->charpos;
14186
14187 cprop = Fget_char_property (make_number (gpos),
14188 Qcursor,
14189 glyph->object);
14190 if (!NILP (cprop))
14191 {
14192 cursor = glyph;
14193 break;
14194 }
14195 if (tem && glyph->charpos < strpos)
14196 {
14197 strpos = glyph->charpos;
14198 cursor = glyph;
14199 }
14200 }
14201
14202 if (tem == pt_old
14203 || (tem - pt_old > 0 && tem < pos_after))
14204 goto compute_x;
14205 }
14206 if (tem)
14207 pos = tem + 1; /* don't find previous instances */
14208 }
14209 /* This string is not what we want; skip all of the
14210 glyphs that came from it. */
14211 while ((row->reversed_p ? glyph > stop : glyph < stop)
14212 && EQ (glyph->object, str))
14213 glyph += incr;
14214 }
14215 else
14216 glyph += incr;
14217 }
14218
14219 /* If we reached the end of the line, and END was from a string,
14220 the cursor is not on this line. */
14221 if (cursor == NULL
14222 && (row->reversed_p ? glyph <= end : glyph >= end)
14223 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14224 && STRINGP (end->object)
14225 && row->continued_p)
14226 return 0;
14227 }
14228 /* A truncated row may not include PT among its character positions.
14229 Setting the cursor inside the scroll margin will trigger
14230 recalculation of hscroll in hscroll_window_tree. But if a
14231 display string covers point, defer to the string-handling
14232 code below to figure this out. */
14233 else if (row->truncated_on_left_p && pt_old < bpos_min)
14234 {
14235 cursor = glyph_before;
14236 x = -1;
14237 }
14238 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14239 /* Zero-width characters produce no glyphs. */
14240 || (!empty_line_p
14241 && (row->reversed_p
14242 ? glyph_after > glyphs_end
14243 : glyph_after < glyphs_end)))
14244 {
14245 cursor = glyph_after;
14246 x = -1;
14247 }
14248 }
14249
14250 compute_x:
14251 if (cursor != NULL)
14252 glyph = cursor;
14253 else if (glyph == glyphs_end
14254 && pos_before == pos_after
14255 && STRINGP ((row->reversed_p
14256 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14257 : row->glyphs[TEXT_AREA])->object))
14258 {
14259 /* If all the glyphs of this row came from strings, put the
14260 cursor on the first glyph of the row. This avoids having the
14261 cursor outside of the text area in this very rare and hard
14262 use case. */
14263 glyph =
14264 row->reversed_p
14265 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14266 : row->glyphs[TEXT_AREA];
14267 }
14268 if (x < 0)
14269 {
14270 struct glyph *g;
14271
14272 /* Need to compute x that corresponds to GLYPH. */
14273 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14274 {
14275 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14276 emacs_abort ();
14277 x += g->pixel_width;
14278 }
14279 }
14280
14281 /* ROW could be part of a continued line, which, under bidi
14282 reordering, might have other rows whose start and end charpos
14283 occlude point. Only set w->cursor if we found a better
14284 approximation to the cursor position than we have from previously
14285 examined candidate rows belonging to the same continued line. */
14286 if (/* we already have a candidate row */
14287 w->cursor.vpos >= 0
14288 /* that candidate is not the row we are processing */
14289 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14290 /* Make sure cursor.vpos specifies a row whose start and end
14291 charpos occlude point, and it is valid candidate for being a
14292 cursor-row. This is because some callers of this function
14293 leave cursor.vpos at the row where the cursor was displayed
14294 during the last redisplay cycle. */
14295 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14296 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14297 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14298 {
14299 struct glyph *g1 =
14300 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14301
14302 /* Don't consider glyphs that are outside TEXT_AREA. */
14303 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14304 return 0;
14305 /* Keep the candidate whose buffer position is the closest to
14306 point or has the `cursor' property. */
14307 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14308 w->cursor.hpos >= 0
14309 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14310 && ((BUFFERP (g1->object)
14311 && (g1->charpos == pt_old /* an exact match always wins */
14312 || (BUFFERP (glyph->object)
14313 && eabs (g1->charpos - pt_old)
14314 < eabs (glyph->charpos - pt_old))))
14315 /* previous candidate is a glyph from a string that has
14316 a non-nil `cursor' property */
14317 || (STRINGP (g1->object)
14318 && (!NILP (Fget_char_property (make_number (g1->charpos),
14319 Qcursor, g1->object))
14320 /* previous candidate is from the same display
14321 string as this one, and the display string
14322 came from a text property */
14323 || (EQ (g1->object, glyph->object)
14324 && string_from_text_prop)
14325 /* this candidate is from newline and its
14326 position is not an exact match */
14327 || (INTEGERP (glyph->object)
14328 && glyph->charpos != pt_old)))))
14329 return 0;
14330 /* If this candidate gives an exact match, use that. */
14331 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14332 /* If this candidate is a glyph created for the
14333 terminating newline of a line, and point is on that
14334 newline, it wins because it's an exact match. */
14335 || (!row->continued_p
14336 && INTEGERP (glyph->object)
14337 && glyph->charpos == 0
14338 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14339 /* Otherwise, keep the candidate that comes from a row
14340 spanning less buffer positions. This may win when one or
14341 both candidate positions are on glyphs that came from
14342 display strings, for which we cannot compare buffer
14343 positions. */
14344 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14345 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14346 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14347 return 0;
14348 }
14349 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14350 w->cursor.x = x;
14351 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14352 w->cursor.y = row->y + dy;
14353
14354 if (w == XWINDOW (selected_window))
14355 {
14356 if (!row->continued_p
14357 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14358 && row->x == 0)
14359 {
14360 this_line_buffer = XBUFFER (w->contents);
14361
14362 CHARPOS (this_line_start_pos)
14363 = MATRIX_ROW_START_CHARPOS (row) + delta;
14364 BYTEPOS (this_line_start_pos)
14365 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14366
14367 CHARPOS (this_line_end_pos)
14368 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14369 BYTEPOS (this_line_end_pos)
14370 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14371
14372 this_line_y = w->cursor.y;
14373 this_line_pixel_height = row->height;
14374 this_line_vpos = w->cursor.vpos;
14375 this_line_start_x = row->x;
14376 }
14377 else
14378 CHARPOS (this_line_start_pos) = 0;
14379 }
14380
14381 return 1;
14382 }
14383
14384
14385 /* Run window scroll functions, if any, for WINDOW with new window
14386 start STARTP. Sets the window start of WINDOW to that position.
14387
14388 We assume that the window's buffer is really current. */
14389
14390 static struct text_pos
14391 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14392 {
14393 struct window *w = XWINDOW (window);
14394 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14395
14396 if (current_buffer != XBUFFER (w->contents))
14397 emacs_abort ();
14398
14399 if (!NILP (Vwindow_scroll_functions))
14400 {
14401 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14402 make_number (CHARPOS (startp)));
14403 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14404 /* In case the hook functions switch buffers. */
14405 set_buffer_internal (XBUFFER (w->contents));
14406 }
14407
14408 return startp;
14409 }
14410
14411
14412 /* Make sure the line containing the cursor is fully visible.
14413 A value of 1 means there is nothing to be done.
14414 (Either the line is fully visible, or it cannot be made so,
14415 or we cannot tell.)
14416
14417 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14418 is higher than window.
14419
14420 A value of 0 means the caller should do scrolling
14421 as if point had gone off the screen. */
14422
14423 static int
14424 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14425 {
14426 struct glyph_matrix *matrix;
14427 struct glyph_row *row;
14428 int window_height;
14429
14430 if (!make_cursor_line_fully_visible_p)
14431 return 1;
14432
14433 /* It's not always possible to find the cursor, e.g, when a window
14434 is full of overlay strings. Don't do anything in that case. */
14435 if (w->cursor.vpos < 0)
14436 return 1;
14437
14438 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14439 row = MATRIX_ROW (matrix, w->cursor.vpos);
14440
14441 /* If the cursor row is not partially visible, there's nothing to do. */
14442 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14443 return 1;
14444
14445 /* If the row the cursor is in is taller than the window's height,
14446 it's not clear what to do, so do nothing. */
14447 window_height = window_box_height (w);
14448 if (row->height >= window_height)
14449 {
14450 if (!force_p || MINI_WINDOW_P (w)
14451 || w->vscroll || w->cursor.vpos == 0)
14452 return 1;
14453 }
14454 return 0;
14455 }
14456
14457
14458 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14459 non-zero means only WINDOW is redisplayed in redisplay_internal.
14460 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14461 in redisplay_window to bring a partially visible line into view in
14462 the case that only the cursor has moved.
14463
14464 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14465 last screen line's vertical height extends past the end of the screen.
14466
14467 Value is
14468
14469 1 if scrolling succeeded
14470
14471 0 if scrolling didn't find point.
14472
14473 -1 if new fonts have been loaded so that we must interrupt
14474 redisplay, adjust glyph matrices, and try again. */
14475
14476 enum
14477 {
14478 SCROLLING_SUCCESS,
14479 SCROLLING_FAILED,
14480 SCROLLING_NEED_LARGER_MATRICES
14481 };
14482
14483 /* If scroll-conservatively is more than this, never recenter.
14484
14485 If you change this, don't forget to update the doc string of
14486 `scroll-conservatively' and the Emacs manual. */
14487 #define SCROLL_LIMIT 100
14488
14489 static int
14490 try_scrolling (Lisp_Object window, int just_this_one_p,
14491 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14492 int temp_scroll_step, int last_line_misfit)
14493 {
14494 struct window *w = XWINDOW (window);
14495 struct frame *f = XFRAME (w->frame);
14496 struct text_pos pos, startp;
14497 struct it it;
14498 int this_scroll_margin, scroll_max, rc, height;
14499 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14500 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14501 Lisp_Object aggressive;
14502 /* We will never try scrolling more than this number of lines. */
14503 int scroll_limit = SCROLL_LIMIT;
14504
14505 #ifdef GLYPH_DEBUG
14506 debug_method_add (w, "try_scrolling");
14507 #endif
14508
14509 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14510
14511 /* Compute scroll margin height in pixels. We scroll when point is
14512 within this distance from the top or bottom of the window. */
14513 if (scroll_margin > 0)
14514 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14515 * FRAME_LINE_HEIGHT (f);
14516 else
14517 this_scroll_margin = 0;
14518
14519 /* Force arg_scroll_conservatively to have a reasonable value, to
14520 avoid scrolling too far away with slow move_it_* functions. Note
14521 that the user can supply scroll-conservatively equal to
14522 `most-positive-fixnum', which can be larger than INT_MAX. */
14523 if (arg_scroll_conservatively > scroll_limit)
14524 {
14525 arg_scroll_conservatively = scroll_limit + 1;
14526 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14527 }
14528 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14529 /* Compute how much we should try to scroll maximally to bring
14530 point into view. */
14531 scroll_max = (max (scroll_step,
14532 max (arg_scroll_conservatively, temp_scroll_step))
14533 * FRAME_LINE_HEIGHT (f));
14534 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14535 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14536 /* We're trying to scroll because of aggressive scrolling but no
14537 scroll_step is set. Choose an arbitrary one. */
14538 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14539 else
14540 scroll_max = 0;
14541
14542 too_near_end:
14543
14544 /* Decide whether to scroll down. */
14545 if (PT > CHARPOS (startp))
14546 {
14547 int scroll_margin_y;
14548
14549 /* Compute the pixel ypos of the scroll margin, then move IT to
14550 either that ypos or PT, whichever comes first. */
14551 start_display (&it, w, startp);
14552 scroll_margin_y = it.last_visible_y - this_scroll_margin
14553 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14554 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14555 (MOVE_TO_POS | MOVE_TO_Y));
14556
14557 if (PT > CHARPOS (it.current.pos))
14558 {
14559 int y0 = line_bottom_y (&it);
14560 /* Compute how many pixels below window bottom to stop searching
14561 for PT. This avoids costly search for PT that is far away if
14562 the user limited scrolling by a small number of lines, but
14563 always finds PT if scroll_conservatively is set to a large
14564 number, such as most-positive-fixnum. */
14565 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14566 int y_to_move = it.last_visible_y + slack;
14567
14568 /* Compute the distance from the scroll margin to PT or to
14569 the scroll limit, whichever comes first. This should
14570 include the height of the cursor line, to make that line
14571 fully visible. */
14572 move_it_to (&it, PT, -1, y_to_move,
14573 -1, MOVE_TO_POS | MOVE_TO_Y);
14574 dy = line_bottom_y (&it) - y0;
14575
14576 if (dy > scroll_max)
14577 return SCROLLING_FAILED;
14578
14579 if (dy > 0)
14580 scroll_down_p = 1;
14581 }
14582 }
14583
14584 if (scroll_down_p)
14585 {
14586 /* Point is in or below the bottom scroll margin, so move the
14587 window start down. If scrolling conservatively, move it just
14588 enough down to make point visible. If scroll_step is set,
14589 move it down by scroll_step. */
14590 if (arg_scroll_conservatively)
14591 amount_to_scroll
14592 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14593 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14594 else if (scroll_step || temp_scroll_step)
14595 amount_to_scroll = scroll_max;
14596 else
14597 {
14598 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14599 height = WINDOW_BOX_TEXT_HEIGHT (w);
14600 if (NUMBERP (aggressive))
14601 {
14602 double float_amount = XFLOATINT (aggressive) * height;
14603 int aggressive_scroll = float_amount;
14604 if (aggressive_scroll == 0 && float_amount > 0)
14605 aggressive_scroll = 1;
14606 /* Don't let point enter the scroll margin near top of
14607 the window. This could happen if the value of
14608 scroll_up_aggressively is too large and there are
14609 non-zero margins, because scroll_up_aggressively
14610 means put point that fraction of window height
14611 _from_the_bottom_margin_. */
14612 if (aggressive_scroll + 2*this_scroll_margin > height)
14613 aggressive_scroll = height - 2*this_scroll_margin;
14614 amount_to_scroll = dy + aggressive_scroll;
14615 }
14616 }
14617
14618 if (amount_to_scroll <= 0)
14619 return SCROLLING_FAILED;
14620
14621 start_display (&it, w, startp);
14622 if (arg_scroll_conservatively <= scroll_limit)
14623 move_it_vertically (&it, amount_to_scroll);
14624 else
14625 {
14626 /* Extra precision for users who set scroll-conservatively
14627 to a large number: make sure the amount we scroll
14628 the window start is never less than amount_to_scroll,
14629 which was computed as distance from window bottom to
14630 point. This matters when lines at window top and lines
14631 below window bottom have different height. */
14632 struct it it1;
14633 void *it1data = NULL;
14634 /* We use a temporary it1 because line_bottom_y can modify
14635 its argument, if it moves one line down; see there. */
14636 int start_y;
14637
14638 SAVE_IT (it1, it, it1data);
14639 start_y = line_bottom_y (&it1);
14640 do {
14641 RESTORE_IT (&it, &it, it1data);
14642 move_it_by_lines (&it, 1);
14643 SAVE_IT (it1, it, it1data);
14644 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14645 }
14646
14647 /* If STARTP is unchanged, move it down another screen line. */
14648 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14649 move_it_by_lines (&it, 1);
14650 startp = it.current.pos;
14651 }
14652 else
14653 {
14654 struct text_pos scroll_margin_pos = startp;
14655 int y_offset = 0;
14656
14657 /* See if point is inside the scroll margin at the top of the
14658 window. */
14659 if (this_scroll_margin)
14660 {
14661 int y_start;
14662
14663 start_display (&it, w, startp);
14664 y_start = it.current_y;
14665 move_it_vertically (&it, this_scroll_margin);
14666 scroll_margin_pos = it.current.pos;
14667 /* If we didn't move enough before hitting ZV, request
14668 additional amount of scroll, to move point out of the
14669 scroll margin. */
14670 if (IT_CHARPOS (it) == ZV
14671 && it.current_y - y_start < this_scroll_margin)
14672 y_offset = this_scroll_margin - (it.current_y - y_start);
14673 }
14674
14675 if (PT < CHARPOS (scroll_margin_pos))
14676 {
14677 /* Point is in the scroll margin at the top of the window or
14678 above what is displayed in the window. */
14679 int y0, y_to_move;
14680
14681 /* Compute the vertical distance from PT to the scroll
14682 margin position. Move as far as scroll_max allows, or
14683 one screenful, or 10 screen lines, whichever is largest.
14684 Give up if distance is greater than scroll_max or if we
14685 didn't reach the scroll margin position. */
14686 SET_TEXT_POS (pos, PT, PT_BYTE);
14687 start_display (&it, w, pos);
14688 y0 = it.current_y;
14689 y_to_move = max (it.last_visible_y,
14690 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14691 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14692 y_to_move, -1,
14693 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14694 dy = it.current_y - y0;
14695 if (dy > scroll_max
14696 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14697 return SCROLLING_FAILED;
14698
14699 /* Additional scroll for when ZV was too close to point. */
14700 dy += y_offset;
14701
14702 /* Compute new window start. */
14703 start_display (&it, w, startp);
14704
14705 if (arg_scroll_conservatively)
14706 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14707 max (scroll_step, temp_scroll_step));
14708 else if (scroll_step || temp_scroll_step)
14709 amount_to_scroll = scroll_max;
14710 else
14711 {
14712 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14713 height = WINDOW_BOX_TEXT_HEIGHT (w);
14714 if (NUMBERP (aggressive))
14715 {
14716 double float_amount = XFLOATINT (aggressive) * height;
14717 int aggressive_scroll = float_amount;
14718 if (aggressive_scroll == 0 && float_amount > 0)
14719 aggressive_scroll = 1;
14720 /* Don't let point enter the scroll margin near
14721 bottom of the window, if the value of
14722 scroll_down_aggressively happens to be too
14723 large. */
14724 if (aggressive_scroll + 2*this_scroll_margin > height)
14725 aggressive_scroll = height - 2*this_scroll_margin;
14726 amount_to_scroll = dy + aggressive_scroll;
14727 }
14728 }
14729
14730 if (amount_to_scroll <= 0)
14731 return SCROLLING_FAILED;
14732
14733 move_it_vertically_backward (&it, amount_to_scroll);
14734 startp = it.current.pos;
14735 }
14736 }
14737
14738 /* Run window scroll functions. */
14739 startp = run_window_scroll_functions (window, startp);
14740
14741 /* Display the window. Give up if new fonts are loaded, or if point
14742 doesn't appear. */
14743 if (!try_window (window, startp, 0))
14744 rc = SCROLLING_NEED_LARGER_MATRICES;
14745 else if (w->cursor.vpos < 0)
14746 {
14747 clear_glyph_matrix (w->desired_matrix);
14748 rc = SCROLLING_FAILED;
14749 }
14750 else
14751 {
14752 /* Maybe forget recorded base line for line number display. */
14753 if (!just_this_one_p
14754 || current_buffer->clip_changed
14755 || BEG_UNCHANGED < CHARPOS (startp))
14756 w->base_line_number = 0;
14757
14758 /* If cursor ends up on a partially visible line,
14759 treat that as being off the bottom of the screen. */
14760 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14761 /* It's possible that the cursor is on the first line of the
14762 buffer, which is partially obscured due to a vscroll
14763 (Bug#7537). In that case, avoid looping forever . */
14764 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14765 {
14766 clear_glyph_matrix (w->desired_matrix);
14767 ++extra_scroll_margin_lines;
14768 goto too_near_end;
14769 }
14770 rc = SCROLLING_SUCCESS;
14771 }
14772
14773 return rc;
14774 }
14775
14776
14777 /* Compute a suitable window start for window W if display of W starts
14778 on a continuation line. Value is non-zero if a new window start
14779 was computed.
14780
14781 The new window start will be computed, based on W's width, starting
14782 from the start of the continued line. It is the start of the
14783 screen line with the minimum distance from the old start W->start. */
14784
14785 static int
14786 compute_window_start_on_continuation_line (struct window *w)
14787 {
14788 struct text_pos pos, start_pos;
14789 int window_start_changed_p = 0;
14790
14791 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14792
14793 /* If window start is on a continuation line... Window start may be
14794 < BEGV in case there's invisible text at the start of the
14795 buffer (M-x rmail, for example). */
14796 if (CHARPOS (start_pos) > BEGV
14797 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14798 {
14799 struct it it;
14800 struct glyph_row *row;
14801
14802 /* Handle the case that the window start is out of range. */
14803 if (CHARPOS (start_pos) < BEGV)
14804 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14805 else if (CHARPOS (start_pos) > ZV)
14806 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14807
14808 /* Find the start of the continued line. This should be fast
14809 because find_newline is fast (newline cache). */
14810 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14811 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14812 row, DEFAULT_FACE_ID);
14813 reseat_at_previous_visible_line_start (&it);
14814
14815 /* If the line start is "too far" away from the window start,
14816 say it takes too much time to compute a new window start. */
14817 if (CHARPOS (start_pos) - IT_CHARPOS (it)
14818 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
14819 {
14820 int min_distance, distance;
14821
14822 /* Move forward by display lines to find the new window
14823 start. If window width was enlarged, the new start can
14824 be expected to be > the old start. If window width was
14825 decreased, the new window start will be < the old start.
14826 So, we're looking for the display line start with the
14827 minimum distance from the old window start. */
14828 pos = it.current.pos;
14829 min_distance = INFINITY;
14830 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
14831 distance < min_distance)
14832 {
14833 min_distance = distance;
14834 pos = it.current.pos;
14835 move_it_by_lines (&it, 1);
14836 }
14837
14838 /* Set the window start there. */
14839 SET_MARKER_FROM_TEXT_POS (w->start, pos);
14840 window_start_changed_p = 1;
14841 }
14842 }
14843
14844 return window_start_changed_p;
14845 }
14846
14847
14848 /* Try cursor movement in case text has not changed in window WINDOW,
14849 with window start STARTP. Value is
14850
14851 CURSOR_MOVEMENT_SUCCESS if successful
14852
14853 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
14854
14855 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
14856 display. *SCROLL_STEP is set to 1, under certain circumstances, if
14857 we want to scroll as if scroll-step were set to 1. See the code.
14858
14859 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
14860 which case we have to abort this redisplay, and adjust matrices
14861 first. */
14862
14863 enum
14864 {
14865 CURSOR_MOVEMENT_SUCCESS,
14866 CURSOR_MOVEMENT_CANNOT_BE_USED,
14867 CURSOR_MOVEMENT_MUST_SCROLL,
14868 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
14869 };
14870
14871 static int
14872 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
14873 {
14874 struct window *w = XWINDOW (window);
14875 struct frame *f = XFRAME (w->frame);
14876 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
14877
14878 #ifdef GLYPH_DEBUG
14879 if (inhibit_try_cursor_movement)
14880 return rc;
14881 #endif
14882
14883 /* Previously, there was a check for Lisp integer in the
14884 if-statement below. Now, this field is converted to
14885 ptrdiff_t, thus zero means invalid position in a buffer. */
14886 eassert (w->last_point > 0);
14887
14888 /* Handle case where text has not changed, only point, and it has
14889 not moved off the frame. */
14890 if (/* Point may be in this window. */
14891 PT >= CHARPOS (startp)
14892 /* Selective display hasn't changed. */
14893 && !current_buffer->clip_changed
14894 /* Function force-mode-line-update is used to force a thorough
14895 redisplay. It sets either windows_or_buffers_changed or
14896 update_mode_lines. So don't take a shortcut here for these
14897 cases. */
14898 && !update_mode_lines
14899 && !windows_or_buffers_changed
14900 && !cursor_type_changed
14901 /* Can't use this case if highlighting a region. When a
14902 region exists, cursor movement has to do more than just
14903 set the cursor. */
14904 && markpos_of_region () < 0
14905 && !w->region_showing
14906 && NILP (Vshow_trailing_whitespace)
14907 /* This code is not used for mini-buffer for the sake of the case
14908 of redisplaying to replace an echo area message; since in
14909 that case the mini-buffer contents per se are usually
14910 unchanged. This code is of no real use in the mini-buffer
14911 since the handling of this_line_start_pos, etc., in redisplay
14912 handles the same cases. */
14913 && !EQ (window, minibuf_window)
14914 /* When splitting windows or for new windows, it happens that
14915 redisplay is called with a nil window_end_vpos or one being
14916 larger than the window. This should really be fixed in
14917 window.c. I don't have this on my list, now, so we do
14918 approximately the same as the old redisplay code. --gerd. */
14919 && INTEGERP (w->window_end_vpos)
14920 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
14921 && (FRAME_WINDOW_P (f)
14922 || !overlay_arrow_in_current_buffer_p ()))
14923 {
14924 int this_scroll_margin, top_scroll_margin;
14925 struct glyph_row *row = NULL;
14926
14927 #ifdef GLYPH_DEBUG
14928 debug_method_add (w, "cursor movement");
14929 #endif
14930
14931 /* Scroll if point within this distance from the top or bottom
14932 of the window. This is a pixel value. */
14933 if (scroll_margin > 0)
14934 {
14935 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
14936 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
14937 }
14938 else
14939 this_scroll_margin = 0;
14940
14941 top_scroll_margin = this_scroll_margin;
14942 if (WINDOW_WANTS_HEADER_LINE_P (w))
14943 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
14944
14945 /* Start with the row the cursor was displayed during the last
14946 not paused redisplay. Give up if that row is not valid. */
14947 if (w->last_cursor.vpos < 0
14948 || w->last_cursor.vpos >= w->current_matrix->nrows)
14949 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14950 else
14951 {
14952 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
14953 if (row->mode_line_p)
14954 ++row;
14955 if (!row->enabled_p)
14956 rc = CURSOR_MOVEMENT_MUST_SCROLL;
14957 }
14958
14959 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
14960 {
14961 int scroll_p = 0, must_scroll = 0;
14962 int last_y = window_text_bottom_y (w) - this_scroll_margin;
14963
14964 if (PT > w->last_point)
14965 {
14966 /* Point has moved forward. */
14967 while (MATRIX_ROW_END_CHARPOS (row) < PT
14968 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
14969 {
14970 eassert (row->enabled_p);
14971 ++row;
14972 }
14973
14974 /* If the end position of a row equals the start
14975 position of the next row, and PT is at that position,
14976 we would rather display cursor in the next line. */
14977 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
14978 && MATRIX_ROW_END_CHARPOS (row) == PT
14979 && row < MATRIX_MODE_LINE_ROW (w->current_matrix)
14980 && MATRIX_ROW_START_CHARPOS (row+1) == PT
14981 && !cursor_row_p (row))
14982 ++row;
14983
14984 /* If within the scroll margin, scroll. Note that
14985 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
14986 the next line would be drawn, and that
14987 this_scroll_margin can be zero. */
14988 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
14989 || PT > MATRIX_ROW_END_CHARPOS (row)
14990 /* Line is completely visible last line in window
14991 and PT is to be set in the next line. */
14992 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
14993 && PT == MATRIX_ROW_END_CHARPOS (row)
14994 && !row->ends_at_zv_p
14995 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
14996 scroll_p = 1;
14997 }
14998 else if (PT < w->last_point)
14999 {
15000 /* Cursor has to be moved backward. Note that PT >=
15001 CHARPOS (startp) because of the outer if-statement. */
15002 while (!row->mode_line_p
15003 && (MATRIX_ROW_START_CHARPOS (row) > PT
15004 || (MATRIX_ROW_START_CHARPOS (row) == PT
15005 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15006 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15007 row > w->current_matrix->rows
15008 && (row-1)->ends_in_newline_from_string_p))))
15009 && (row->y > top_scroll_margin
15010 || CHARPOS (startp) == BEGV))
15011 {
15012 eassert (row->enabled_p);
15013 --row;
15014 }
15015
15016 /* Consider the following case: Window starts at BEGV,
15017 there is invisible, intangible text at BEGV, so that
15018 display starts at some point START > BEGV. It can
15019 happen that we are called with PT somewhere between
15020 BEGV and START. Try to handle that case. */
15021 if (row < w->current_matrix->rows
15022 || row->mode_line_p)
15023 {
15024 row = w->current_matrix->rows;
15025 if (row->mode_line_p)
15026 ++row;
15027 }
15028
15029 /* Due to newlines in overlay strings, we may have to
15030 skip forward over overlay strings. */
15031 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15032 && MATRIX_ROW_END_CHARPOS (row) == PT
15033 && !cursor_row_p (row))
15034 ++row;
15035
15036 /* If within the scroll margin, scroll. */
15037 if (row->y < top_scroll_margin
15038 && CHARPOS (startp) != BEGV)
15039 scroll_p = 1;
15040 }
15041 else
15042 {
15043 /* Cursor did not move. So don't scroll even if cursor line
15044 is partially visible, as it was so before. */
15045 rc = CURSOR_MOVEMENT_SUCCESS;
15046 }
15047
15048 if (PT < MATRIX_ROW_START_CHARPOS (row)
15049 || PT > MATRIX_ROW_END_CHARPOS (row))
15050 {
15051 /* if PT is not in the glyph row, give up. */
15052 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15053 must_scroll = 1;
15054 }
15055 else if (rc != CURSOR_MOVEMENT_SUCCESS
15056 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15057 {
15058 struct glyph_row *row1;
15059
15060 /* If rows are bidi-reordered and point moved, back up
15061 until we find a row that does not belong to a
15062 continuation line. This is because we must consider
15063 all rows of a continued line as candidates for the
15064 new cursor positioning, since row start and end
15065 positions change non-linearly with vertical position
15066 in such rows. */
15067 /* FIXME: Revisit this when glyph ``spilling'' in
15068 continuation lines' rows is implemented for
15069 bidi-reordered rows. */
15070 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15071 MATRIX_ROW_CONTINUATION_LINE_P (row);
15072 --row)
15073 {
15074 /* If we hit the beginning of the displayed portion
15075 without finding the first row of a continued
15076 line, give up. */
15077 if (row <= row1)
15078 {
15079 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15080 break;
15081 }
15082 eassert (row->enabled_p);
15083 }
15084 }
15085 if (must_scroll)
15086 ;
15087 else if (rc != CURSOR_MOVEMENT_SUCCESS
15088 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15089 /* Make sure this isn't a header line by any chance, since
15090 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15091 && !row->mode_line_p
15092 && make_cursor_line_fully_visible_p)
15093 {
15094 if (PT == MATRIX_ROW_END_CHARPOS (row)
15095 && !row->ends_at_zv_p
15096 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15097 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15098 else if (row->height > window_box_height (w))
15099 {
15100 /* If we end up in a partially visible line, let's
15101 make it fully visible, except when it's taller
15102 than the window, in which case we can't do much
15103 about it. */
15104 *scroll_step = 1;
15105 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15106 }
15107 else
15108 {
15109 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15110 if (!cursor_row_fully_visible_p (w, 0, 1))
15111 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15112 else
15113 rc = CURSOR_MOVEMENT_SUCCESS;
15114 }
15115 }
15116 else if (scroll_p)
15117 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15118 else if (rc != CURSOR_MOVEMENT_SUCCESS
15119 && !NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
15120 {
15121 /* With bidi-reordered rows, there could be more than
15122 one candidate row whose start and end positions
15123 occlude point. We need to let set_cursor_from_row
15124 find the best candidate. */
15125 /* FIXME: Revisit this when glyph ``spilling'' in
15126 continuation lines' rows is implemented for
15127 bidi-reordered rows. */
15128 int rv = 0;
15129
15130 do
15131 {
15132 int at_zv_p = 0, exact_match_p = 0;
15133
15134 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15135 && PT <= MATRIX_ROW_END_CHARPOS (row)
15136 && cursor_row_p (row))
15137 rv |= set_cursor_from_row (w, row, w->current_matrix,
15138 0, 0, 0, 0);
15139 /* As soon as we've found the exact match for point,
15140 or the first suitable row whose ends_at_zv_p flag
15141 is set, we are done. */
15142 at_zv_p =
15143 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15144 if (rv && !at_zv_p
15145 && w->cursor.hpos >= 0
15146 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15147 w->cursor.vpos))
15148 {
15149 struct glyph_row *candidate =
15150 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15151 struct glyph *g =
15152 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15153 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15154
15155 exact_match_p =
15156 (BUFFERP (g->object) && g->charpos == PT)
15157 || (INTEGERP (g->object)
15158 && (g->charpos == PT
15159 || (g->charpos == 0 && endpos - 1 == PT)));
15160 }
15161 if (rv && (at_zv_p || exact_match_p))
15162 {
15163 rc = CURSOR_MOVEMENT_SUCCESS;
15164 break;
15165 }
15166 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15167 break;
15168 ++row;
15169 }
15170 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15171 || row->continued_p)
15172 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15173 || (MATRIX_ROW_START_CHARPOS (row) == PT
15174 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15175 /* If we didn't find any candidate rows, or exited the
15176 loop before all the candidates were examined, signal
15177 to the caller that this method failed. */
15178 if (rc != CURSOR_MOVEMENT_SUCCESS
15179 && !(rv
15180 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15181 && !row->continued_p))
15182 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15183 else if (rv)
15184 rc = CURSOR_MOVEMENT_SUCCESS;
15185 }
15186 else
15187 {
15188 do
15189 {
15190 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15191 {
15192 rc = CURSOR_MOVEMENT_SUCCESS;
15193 break;
15194 }
15195 ++row;
15196 }
15197 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15198 && MATRIX_ROW_START_CHARPOS (row) == PT
15199 && cursor_row_p (row));
15200 }
15201 }
15202 }
15203
15204 return rc;
15205 }
15206
15207 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15208 static
15209 #endif
15210 void
15211 set_vertical_scroll_bar (struct window *w)
15212 {
15213 ptrdiff_t start, end, whole;
15214
15215 /* Calculate the start and end positions for the current window.
15216 At some point, it would be nice to choose between scrollbars
15217 which reflect the whole buffer size, with special markers
15218 indicating narrowing, and scrollbars which reflect only the
15219 visible region.
15220
15221 Note that mini-buffers sometimes aren't displaying any text. */
15222 if (!MINI_WINDOW_P (w)
15223 || (w == XWINDOW (minibuf_window)
15224 && NILP (echo_area_buffer[0])))
15225 {
15226 struct buffer *buf = XBUFFER (w->contents);
15227 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15228 start = marker_position (w->start) - BUF_BEGV (buf);
15229 /* I don't think this is guaranteed to be right. For the
15230 moment, we'll pretend it is. */
15231 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15232
15233 if (end < start)
15234 end = start;
15235 if (whole < (end - start))
15236 whole = end - start;
15237 }
15238 else
15239 start = end = whole = 0;
15240
15241 /* Indicate what this scroll bar ought to be displaying now. */
15242 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15243 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15244 (w, end - start, whole, start);
15245 }
15246
15247
15248 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15249 selected_window is redisplayed.
15250
15251 We can return without actually redisplaying the window if
15252 fonts_changed_p. In that case, redisplay_internal will
15253 retry. */
15254
15255 static void
15256 redisplay_window (Lisp_Object window, int just_this_one_p)
15257 {
15258 struct window *w = XWINDOW (window);
15259 struct frame *f = XFRAME (w->frame);
15260 struct buffer *buffer = XBUFFER (w->contents);
15261 struct buffer *old = current_buffer;
15262 struct text_pos lpoint, opoint, startp;
15263 int update_mode_line;
15264 int tem;
15265 struct it it;
15266 /* Record it now because it's overwritten. */
15267 int current_matrix_up_to_date_p = 0;
15268 int used_current_matrix_p = 0;
15269 /* This is less strict than current_matrix_up_to_date_p.
15270 It indicates that the buffer contents and narrowing are unchanged. */
15271 int buffer_unchanged_p = 0;
15272 int temp_scroll_step = 0;
15273 ptrdiff_t count = SPECPDL_INDEX ();
15274 int rc;
15275 int centering_position = -1;
15276 int last_line_misfit = 0;
15277 ptrdiff_t beg_unchanged, end_unchanged;
15278
15279 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15280 opoint = lpoint;
15281
15282 #ifdef GLYPH_DEBUG
15283 *w->desired_matrix->method = 0;
15284 #endif
15285
15286 /* Make sure that both W's markers are valid. */
15287 eassert (XMARKER (w->start)->buffer == buffer);
15288 eassert (XMARKER (w->pointm)->buffer == buffer);
15289
15290 restart:
15291 reconsider_clip_changes (w, buffer);
15292
15293 /* Has the mode line to be updated? */
15294 update_mode_line = (w->update_mode_line
15295 || update_mode_lines
15296 || buffer->clip_changed
15297 || buffer->prevent_redisplay_optimizations_p);
15298
15299 if (MINI_WINDOW_P (w))
15300 {
15301 if (w == XWINDOW (echo_area_window)
15302 && !NILP (echo_area_buffer[0]))
15303 {
15304 if (update_mode_line)
15305 /* We may have to update a tty frame's menu bar or a
15306 tool-bar. Example `M-x C-h C-h C-g'. */
15307 goto finish_menu_bars;
15308 else
15309 /* We've already displayed the echo area glyphs in this window. */
15310 goto finish_scroll_bars;
15311 }
15312 else if ((w != XWINDOW (minibuf_window)
15313 || minibuf_level == 0)
15314 /* When buffer is nonempty, redisplay window normally. */
15315 && BUF_Z (XBUFFER (w->contents)) == BUF_BEG (XBUFFER (w->contents))
15316 /* Quail displays non-mini buffers in minibuffer window.
15317 In that case, redisplay the window normally. */
15318 && !NILP (Fmemq (w->contents, Vminibuffer_list)))
15319 {
15320 /* W is a mini-buffer window, but it's not active, so clear
15321 it. */
15322 int yb = window_text_bottom_y (w);
15323 struct glyph_row *row;
15324 int y;
15325
15326 for (y = 0, row = w->desired_matrix->rows;
15327 y < yb;
15328 y += row->height, ++row)
15329 blank_row (w, row, y);
15330 goto finish_scroll_bars;
15331 }
15332
15333 clear_glyph_matrix (w->desired_matrix);
15334 }
15335
15336 /* Otherwise set up data on this window; select its buffer and point
15337 value. */
15338 /* Really select the buffer, for the sake of buffer-local
15339 variables. */
15340 set_buffer_internal_1 (XBUFFER (w->contents));
15341
15342 current_matrix_up_to_date_p
15343 = (w->window_end_valid
15344 && !current_buffer->clip_changed
15345 && !current_buffer->prevent_redisplay_optimizations_p
15346 && !window_outdated (w));
15347
15348 /* Run the window-bottom-change-functions
15349 if it is possible that the text on the screen has changed
15350 (either due to modification of the text, or any other reason). */
15351 if (!current_matrix_up_to_date_p
15352 && !NILP (Vwindow_text_change_functions))
15353 {
15354 safe_run_hooks (Qwindow_text_change_functions);
15355 goto restart;
15356 }
15357
15358 beg_unchanged = BEG_UNCHANGED;
15359 end_unchanged = END_UNCHANGED;
15360
15361 SET_TEXT_POS (opoint, PT, PT_BYTE);
15362
15363 specbind (Qinhibit_point_motion_hooks, Qt);
15364
15365 buffer_unchanged_p
15366 = (w->window_end_valid
15367 && !current_buffer->clip_changed
15368 && !window_outdated (w));
15369
15370 /* When windows_or_buffers_changed is non-zero, we can't rely on
15371 the window end being valid, so set it to nil there. */
15372 if (windows_or_buffers_changed)
15373 {
15374 /* If window starts on a continuation line, maybe adjust the
15375 window start in case the window's width changed. */
15376 if (XMARKER (w->start)->buffer == current_buffer)
15377 compute_window_start_on_continuation_line (w);
15378
15379 w->window_end_valid = 0;
15380 }
15381
15382 /* Some sanity checks. */
15383 CHECK_WINDOW_END (w);
15384 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15385 emacs_abort ();
15386 if (BYTEPOS (opoint) < CHARPOS (opoint))
15387 emacs_abort ();
15388
15389 if (mode_line_update_needed (w))
15390 update_mode_line = 1;
15391
15392 /* Point refers normally to the selected window. For any other
15393 window, set up appropriate value. */
15394 if (!EQ (window, selected_window))
15395 {
15396 ptrdiff_t new_pt = marker_position (w->pointm);
15397 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15398 if (new_pt < BEGV)
15399 {
15400 new_pt = BEGV;
15401 new_pt_byte = BEGV_BYTE;
15402 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15403 }
15404 else if (new_pt > (ZV - 1))
15405 {
15406 new_pt = ZV;
15407 new_pt_byte = ZV_BYTE;
15408 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15409 }
15410
15411 /* We don't use SET_PT so that the point-motion hooks don't run. */
15412 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15413 }
15414
15415 /* If any of the character widths specified in the display table
15416 have changed, invalidate the width run cache. It's true that
15417 this may be a bit late to catch such changes, but the rest of
15418 redisplay goes (non-fatally) haywire when the display table is
15419 changed, so why should we worry about doing any better? */
15420 if (current_buffer->width_run_cache)
15421 {
15422 struct Lisp_Char_Table *disptab = buffer_display_table ();
15423
15424 if (! disptab_matches_widthtab
15425 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15426 {
15427 invalidate_region_cache (current_buffer,
15428 current_buffer->width_run_cache,
15429 BEG, Z);
15430 recompute_width_table (current_buffer, disptab);
15431 }
15432 }
15433
15434 /* If window-start is screwed up, choose a new one. */
15435 if (XMARKER (w->start)->buffer != current_buffer)
15436 goto recenter;
15437
15438 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15439
15440 /* If someone specified a new starting point but did not insist,
15441 check whether it can be used. */
15442 if (w->optional_new_start
15443 && CHARPOS (startp) >= BEGV
15444 && CHARPOS (startp) <= ZV)
15445 {
15446 w->optional_new_start = 0;
15447 start_display (&it, w, startp);
15448 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15449 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15450 if (IT_CHARPOS (it) == PT)
15451 w->force_start = 1;
15452 /* IT may overshoot PT if text at PT is invisible. */
15453 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15454 w->force_start = 1;
15455 }
15456
15457 force_start:
15458
15459 /* Handle case where place to start displaying has been specified,
15460 unless the specified location is outside the accessible range. */
15461 if (w->force_start || w->frozen_window_start_p)
15462 {
15463 /* We set this later on if we have to adjust point. */
15464 int new_vpos = -1;
15465
15466 w->force_start = 0;
15467 w->vscroll = 0;
15468 w->window_end_valid = 0;
15469
15470 /* Forget any recorded base line for line number display. */
15471 if (!buffer_unchanged_p)
15472 w->base_line_number = 0;
15473
15474 /* Redisplay the mode line. Select the buffer properly for that.
15475 Also, run the hook window-scroll-functions
15476 because we have scrolled. */
15477 /* Note, we do this after clearing force_start because
15478 if there's an error, it is better to forget about force_start
15479 than to get into an infinite loop calling the hook functions
15480 and having them get more errors. */
15481 if (!update_mode_line
15482 || ! NILP (Vwindow_scroll_functions))
15483 {
15484 update_mode_line = 1;
15485 w->update_mode_line = 1;
15486 startp = run_window_scroll_functions (window, startp);
15487 }
15488
15489 w->last_modified = 0;
15490 w->last_overlay_modified = 0;
15491 if (CHARPOS (startp) < BEGV)
15492 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15493 else if (CHARPOS (startp) > ZV)
15494 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15495
15496 /* Redisplay, then check if cursor has been set during the
15497 redisplay. Give up if new fonts were loaded. */
15498 /* We used to issue a CHECK_MARGINS argument to try_window here,
15499 but this causes scrolling to fail when point begins inside
15500 the scroll margin (bug#148) -- cyd */
15501 if (!try_window (window, startp, 0))
15502 {
15503 w->force_start = 1;
15504 clear_glyph_matrix (w->desired_matrix);
15505 goto need_larger_matrices;
15506 }
15507
15508 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15509 {
15510 /* If point does not appear, try to move point so it does
15511 appear. The desired matrix has been built above, so we
15512 can use it here. */
15513 new_vpos = window_box_height (w) / 2;
15514 }
15515
15516 if (!cursor_row_fully_visible_p (w, 0, 0))
15517 {
15518 /* Point does appear, but on a line partly visible at end of window.
15519 Move it back to a fully-visible line. */
15520 new_vpos = window_box_height (w);
15521 }
15522 else if (w->cursor.vpos >=0)
15523 {
15524 /* Some people insist on not letting point enter the scroll
15525 margin, even though this part handles windows that didn't
15526 scroll at all. */
15527 int margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15528 int pixel_margin = margin * FRAME_LINE_HEIGHT (f);
15529 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15530
15531 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15532 below, which finds the row to move point to, advances by
15533 the Y coordinate of the _next_ row, see the definition of
15534 MATRIX_ROW_BOTTOM_Y. */
15535 if (w->cursor.vpos < margin + header_line)
15536 new_vpos
15537 = pixel_margin + (header_line
15538 ? CURRENT_HEADER_LINE_HEIGHT (w)
15539 : 0) + FRAME_LINE_HEIGHT (f);
15540 else
15541 {
15542 int window_height = window_box_height (w);
15543
15544 if (header_line)
15545 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15546 if (w->cursor.y >= window_height - pixel_margin)
15547 new_vpos = window_height - pixel_margin;
15548 }
15549 }
15550
15551 /* If we need to move point for either of the above reasons,
15552 now actually do it. */
15553 if (new_vpos >= 0)
15554 {
15555 struct glyph_row *row;
15556
15557 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15558 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15559 ++row;
15560
15561 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15562 MATRIX_ROW_START_BYTEPOS (row));
15563
15564 if (w != XWINDOW (selected_window))
15565 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15566 else if (current_buffer == old)
15567 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15568
15569 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15570
15571 /* If we are highlighting the region, then we just changed
15572 the region, so redisplay to show it. */
15573 if (markpos_of_region () >= 0)
15574 {
15575 clear_glyph_matrix (w->desired_matrix);
15576 if (!try_window (window, startp, 0))
15577 goto need_larger_matrices;
15578 }
15579 }
15580
15581 #ifdef GLYPH_DEBUG
15582 debug_method_add (w, "forced window start");
15583 #endif
15584 goto done;
15585 }
15586
15587 /* Handle case where text has not changed, only point, and it has
15588 not moved off the frame, and we are not retrying after hscroll.
15589 (current_matrix_up_to_date_p is nonzero when retrying.) */
15590 if (current_matrix_up_to_date_p
15591 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15592 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15593 {
15594 switch (rc)
15595 {
15596 case CURSOR_MOVEMENT_SUCCESS:
15597 used_current_matrix_p = 1;
15598 goto done;
15599
15600 case CURSOR_MOVEMENT_MUST_SCROLL:
15601 goto try_to_scroll;
15602
15603 default:
15604 emacs_abort ();
15605 }
15606 }
15607 /* If current starting point was originally the beginning of a line
15608 but no longer is, find a new starting point. */
15609 else if (w->start_at_line_beg
15610 && !(CHARPOS (startp) <= BEGV
15611 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15612 {
15613 #ifdef GLYPH_DEBUG
15614 debug_method_add (w, "recenter 1");
15615 #endif
15616 goto recenter;
15617 }
15618
15619 /* Try scrolling with try_window_id. Value is > 0 if update has
15620 been done, it is -1 if we know that the same window start will
15621 not work. It is 0 if unsuccessful for some other reason. */
15622 else if ((tem = try_window_id (w)) != 0)
15623 {
15624 #ifdef GLYPH_DEBUG
15625 debug_method_add (w, "try_window_id %d", tem);
15626 #endif
15627
15628 if (fonts_changed_p)
15629 goto need_larger_matrices;
15630 if (tem > 0)
15631 goto done;
15632
15633 /* Otherwise try_window_id has returned -1 which means that we
15634 don't want the alternative below this comment to execute. */
15635 }
15636 else if (CHARPOS (startp) >= BEGV
15637 && CHARPOS (startp) <= ZV
15638 && PT >= CHARPOS (startp)
15639 && (CHARPOS (startp) < ZV
15640 /* Avoid starting at end of buffer. */
15641 || CHARPOS (startp) == BEGV
15642 || !window_outdated (w)))
15643 {
15644 int d1, d2, d3, d4, d5, d6;
15645
15646 /* If first window line is a continuation line, and window start
15647 is inside the modified region, but the first change is before
15648 current window start, we must select a new window start.
15649
15650 However, if this is the result of a down-mouse event (e.g. by
15651 extending the mouse-drag-overlay), we don't want to select a
15652 new window start, since that would change the position under
15653 the mouse, resulting in an unwanted mouse-movement rather
15654 than a simple mouse-click. */
15655 if (!w->start_at_line_beg
15656 && NILP (do_mouse_tracking)
15657 && CHARPOS (startp) > BEGV
15658 && CHARPOS (startp) > BEG + beg_unchanged
15659 && CHARPOS (startp) <= Z - end_unchanged
15660 /* Even if w->start_at_line_beg is nil, a new window may
15661 start at a line_beg, since that's how set_buffer_window
15662 sets it. So, we need to check the return value of
15663 compute_window_start_on_continuation_line. (See also
15664 bug#197). */
15665 && XMARKER (w->start)->buffer == current_buffer
15666 && compute_window_start_on_continuation_line (w)
15667 /* It doesn't make sense to force the window start like we
15668 do at label force_start if it is already known that point
15669 will not be visible in the resulting window, because
15670 doing so will move point from its correct position
15671 instead of scrolling the window to bring point into view.
15672 See bug#9324. */
15673 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15674 {
15675 w->force_start = 1;
15676 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15677 goto force_start;
15678 }
15679
15680 #ifdef GLYPH_DEBUG
15681 debug_method_add (w, "same window start");
15682 #endif
15683
15684 /* Try to redisplay starting at same place as before.
15685 If point has not moved off frame, accept the results. */
15686 if (!current_matrix_up_to_date_p
15687 /* Don't use try_window_reusing_current_matrix in this case
15688 because a window scroll function can have changed the
15689 buffer. */
15690 || !NILP (Vwindow_scroll_functions)
15691 || MINI_WINDOW_P (w)
15692 || !(used_current_matrix_p
15693 = try_window_reusing_current_matrix (w)))
15694 {
15695 IF_DEBUG (debug_method_add (w, "1"));
15696 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15697 /* -1 means we need to scroll.
15698 0 means we need new matrices, but fonts_changed_p
15699 is set in that case, so we will detect it below. */
15700 goto try_to_scroll;
15701 }
15702
15703 if (fonts_changed_p)
15704 goto need_larger_matrices;
15705
15706 if (w->cursor.vpos >= 0)
15707 {
15708 if (!just_this_one_p
15709 || current_buffer->clip_changed
15710 || BEG_UNCHANGED < CHARPOS (startp))
15711 /* Forget any recorded base line for line number display. */
15712 w->base_line_number = 0;
15713
15714 if (!cursor_row_fully_visible_p (w, 1, 0))
15715 {
15716 clear_glyph_matrix (w->desired_matrix);
15717 last_line_misfit = 1;
15718 }
15719 /* Drop through and scroll. */
15720 else
15721 goto done;
15722 }
15723 else
15724 clear_glyph_matrix (w->desired_matrix);
15725 }
15726
15727 try_to_scroll:
15728
15729 w->last_modified = 0;
15730 w->last_overlay_modified = 0;
15731
15732 /* Redisplay the mode line. Select the buffer properly for that. */
15733 if (!update_mode_line)
15734 {
15735 update_mode_line = 1;
15736 w->update_mode_line = 1;
15737 }
15738
15739 /* Try to scroll by specified few lines. */
15740 if ((scroll_conservatively
15741 || emacs_scroll_step
15742 || temp_scroll_step
15743 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15744 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15745 && CHARPOS (startp) >= BEGV
15746 && CHARPOS (startp) <= ZV)
15747 {
15748 /* The function returns -1 if new fonts were loaded, 1 if
15749 successful, 0 if not successful. */
15750 int ss = try_scrolling (window, just_this_one_p,
15751 scroll_conservatively,
15752 emacs_scroll_step,
15753 temp_scroll_step, last_line_misfit);
15754 switch (ss)
15755 {
15756 case SCROLLING_SUCCESS:
15757 goto done;
15758
15759 case SCROLLING_NEED_LARGER_MATRICES:
15760 goto need_larger_matrices;
15761
15762 case SCROLLING_FAILED:
15763 break;
15764
15765 default:
15766 emacs_abort ();
15767 }
15768 }
15769
15770 /* Finally, just choose a place to start which positions point
15771 according to user preferences. */
15772
15773 recenter:
15774
15775 #ifdef GLYPH_DEBUG
15776 debug_method_add (w, "recenter");
15777 #endif
15778
15779 /* Forget any previously recorded base line for line number display. */
15780 if (!buffer_unchanged_p)
15781 w->base_line_number = 0;
15782
15783 /* Determine the window start relative to point. */
15784 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15785 it.current_y = it.last_visible_y;
15786 if (centering_position < 0)
15787 {
15788 int margin =
15789 scroll_margin > 0
15790 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15791 : 0;
15792 ptrdiff_t margin_pos = CHARPOS (startp);
15793 Lisp_Object aggressive;
15794 int scrolling_up;
15795
15796 /* If there is a scroll margin at the top of the window, find
15797 its character position. */
15798 if (margin
15799 /* Cannot call start_display if startp is not in the
15800 accessible region of the buffer. This can happen when we
15801 have just switched to a different buffer and/or changed
15802 its restriction. In that case, startp is initialized to
15803 the character position 1 (BEGV) because we did not yet
15804 have chance to display the buffer even once. */
15805 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15806 {
15807 struct it it1;
15808 void *it1data = NULL;
15809
15810 SAVE_IT (it1, it, it1data);
15811 start_display (&it1, w, startp);
15812 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15813 margin_pos = IT_CHARPOS (it1);
15814 RESTORE_IT (&it, &it, it1data);
15815 }
15816 scrolling_up = PT > margin_pos;
15817 aggressive =
15818 scrolling_up
15819 ? BVAR (current_buffer, scroll_up_aggressively)
15820 : BVAR (current_buffer, scroll_down_aggressively);
15821
15822 if (!MINI_WINDOW_P (w)
15823 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
15824 {
15825 int pt_offset = 0;
15826
15827 /* Setting scroll-conservatively overrides
15828 scroll-*-aggressively. */
15829 if (!scroll_conservatively && NUMBERP (aggressive))
15830 {
15831 double float_amount = XFLOATINT (aggressive);
15832
15833 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
15834 if (pt_offset == 0 && float_amount > 0)
15835 pt_offset = 1;
15836 if (pt_offset && margin > 0)
15837 margin -= 1;
15838 }
15839 /* Compute how much to move the window start backward from
15840 point so that point will be displayed where the user
15841 wants it. */
15842 if (scrolling_up)
15843 {
15844 centering_position = it.last_visible_y;
15845 if (pt_offset)
15846 centering_position -= pt_offset;
15847 centering_position -=
15848 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
15849 + WINDOW_HEADER_LINE_HEIGHT (w);
15850 /* Don't let point enter the scroll margin near top of
15851 the window. */
15852 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
15853 centering_position = margin * FRAME_LINE_HEIGHT (f);
15854 }
15855 else
15856 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
15857 }
15858 else
15859 /* Set the window start half the height of the window backward
15860 from point. */
15861 centering_position = window_box_height (w) / 2;
15862 }
15863 move_it_vertically_backward (&it, centering_position);
15864
15865 eassert (IT_CHARPOS (it) >= BEGV);
15866
15867 /* The function move_it_vertically_backward may move over more
15868 than the specified y-distance. If it->w is small, e.g. a
15869 mini-buffer window, we may end up in front of the window's
15870 display area. Start displaying at the start of the line
15871 containing PT in this case. */
15872 if (it.current_y <= 0)
15873 {
15874 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15875 move_it_vertically_backward (&it, 0);
15876 it.current_y = 0;
15877 }
15878
15879 it.current_x = it.hpos = 0;
15880
15881 /* Set the window start position here explicitly, to avoid an
15882 infinite loop in case the functions in window-scroll-functions
15883 get errors. */
15884 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
15885
15886 /* Run scroll hooks. */
15887 startp = run_window_scroll_functions (window, it.current.pos);
15888
15889 /* Redisplay the window. */
15890 if (!current_matrix_up_to_date_p
15891 || windows_or_buffers_changed
15892 || cursor_type_changed
15893 /* Don't use try_window_reusing_current_matrix in this case
15894 because it can have changed the buffer. */
15895 || !NILP (Vwindow_scroll_functions)
15896 || !just_this_one_p
15897 || MINI_WINDOW_P (w)
15898 || !(used_current_matrix_p
15899 = try_window_reusing_current_matrix (w)))
15900 try_window (window, startp, 0);
15901
15902 /* If new fonts have been loaded (due to fontsets), give up. We
15903 have to start a new redisplay since we need to re-adjust glyph
15904 matrices. */
15905 if (fonts_changed_p)
15906 goto need_larger_matrices;
15907
15908 /* If cursor did not appear assume that the middle of the window is
15909 in the first line of the window. Do it again with the next line.
15910 (Imagine a window of height 100, displaying two lines of height
15911 60. Moving back 50 from it->last_visible_y will end in the first
15912 line.) */
15913 if (w->cursor.vpos < 0)
15914 {
15915 if (w->window_end_valid && PT >= Z - XFASTINT (w->window_end_pos))
15916 {
15917 clear_glyph_matrix (w->desired_matrix);
15918 move_it_by_lines (&it, 1);
15919 try_window (window, it.current.pos, 0);
15920 }
15921 else if (PT < IT_CHARPOS (it))
15922 {
15923 clear_glyph_matrix (w->desired_matrix);
15924 move_it_by_lines (&it, -1);
15925 try_window (window, it.current.pos, 0);
15926 }
15927 else
15928 {
15929 /* Not much we can do about it. */
15930 }
15931 }
15932
15933 /* Consider the following case: Window starts at BEGV, there is
15934 invisible, intangible text at BEGV, so that display starts at
15935 some point START > BEGV. It can happen that we are called with
15936 PT somewhere between BEGV and START. Try to handle that case. */
15937 if (w->cursor.vpos < 0)
15938 {
15939 struct glyph_row *row = w->current_matrix->rows;
15940 if (row->mode_line_p)
15941 ++row;
15942 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15943 }
15944
15945 if (!cursor_row_fully_visible_p (w, 0, 0))
15946 {
15947 /* If vscroll is enabled, disable it and try again. */
15948 if (w->vscroll)
15949 {
15950 w->vscroll = 0;
15951 clear_glyph_matrix (w->desired_matrix);
15952 goto recenter;
15953 }
15954
15955 /* Users who set scroll-conservatively to a large number want
15956 point just above/below the scroll margin. If we ended up
15957 with point's row partially visible, move the window start to
15958 make that row fully visible and out of the margin. */
15959 if (scroll_conservatively > SCROLL_LIMIT)
15960 {
15961 int margin =
15962 scroll_margin > 0
15963 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15964 : 0;
15965 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
15966
15967 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
15968 clear_glyph_matrix (w->desired_matrix);
15969 if (1 == try_window (window, it.current.pos,
15970 TRY_WINDOW_CHECK_MARGINS))
15971 goto done;
15972 }
15973
15974 /* If centering point failed to make the whole line visible,
15975 put point at the top instead. That has to make the whole line
15976 visible, if it can be done. */
15977 if (centering_position == 0)
15978 goto done;
15979
15980 clear_glyph_matrix (w->desired_matrix);
15981 centering_position = 0;
15982 goto recenter;
15983 }
15984
15985 done:
15986
15987 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15988 w->start_at_line_beg = (CHARPOS (startp) == BEGV
15989 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
15990
15991 /* Display the mode line, if we must. */
15992 if ((update_mode_line
15993 /* If window not full width, must redo its mode line
15994 if (a) the window to its side is being redone and
15995 (b) we do a frame-based redisplay. This is a consequence
15996 of how inverted lines are drawn in frame-based redisplay. */
15997 || (!just_this_one_p
15998 && !FRAME_WINDOW_P (f)
15999 && !WINDOW_FULL_WIDTH_P (w))
16000 /* Line number to display. */
16001 || w->base_line_pos > 0
16002 /* Column number is displayed and different from the one displayed. */
16003 || (w->column_number_displayed != -1
16004 && (w->column_number_displayed != current_column ())))
16005 /* This means that the window has a mode line. */
16006 && (WINDOW_WANTS_MODELINE_P (w)
16007 || WINDOW_WANTS_HEADER_LINE_P (w)))
16008 {
16009 display_mode_lines (w);
16010
16011 /* If mode line height has changed, arrange for a thorough
16012 immediate redisplay using the correct mode line height. */
16013 if (WINDOW_WANTS_MODELINE_P (w)
16014 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16015 {
16016 fonts_changed_p = 1;
16017 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16018 = DESIRED_MODE_LINE_HEIGHT (w);
16019 }
16020
16021 /* If header line height has changed, arrange for a thorough
16022 immediate redisplay using the correct header line height. */
16023 if (WINDOW_WANTS_HEADER_LINE_P (w)
16024 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16025 {
16026 fonts_changed_p = 1;
16027 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16028 = DESIRED_HEADER_LINE_HEIGHT (w);
16029 }
16030
16031 if (fonts_changed_p)
16032 goto need_larger_matrices;
16033 }
16034
16035 if (!line_number_displayed && w->base_line_pos != -1)
16036 {
16037 w->base_line_pos = 0;
16038 w->base_line_number = 0;
16039 }
16040
16041 finish_menu_bars:
16042
16043 /* When we reach a frame's selected window, redo the frame's menu bar. */
16044 if (update_mode_line
16045 && EQ (FRAME_SELECTED_WINDOW (f), window))
16046 {
16047 int redisplay_menu_p = 0;
16048
16049 if (FRAME_WINDOW_P (f))
16050 {
16051 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16052 || defined (HAVE_NS) || defined (USE_GTK)
16053 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16054 #else
16055 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16056 #endif
16057 }
16058 else
16059 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16060
16061 if (redisplay_menu_p)
16062 display_menu_bar (w);
16063
16064 #ifdef HAVE_WINDOW_SYSTEM
16065 if (FRAME_WINDOW_P (f))
16066 {
16067 #if defined (USE_GTK) || defined (HAVE_NS)
16068 if (FRAME_EXTERNAL_TOOL_BAR (f))
16069 redisplay_tool_bar (f);
16070 #else
16071 if (WINDOWP (f->tool_bar_window)
16072 && (FRAME_TOOL_BAR_LINES (f) > 0
16073 || !NILP (Vauto_resize_tool_bars))
16074 && redisplay_tool_bar (f))
16075 ignore_mouse_drag_p = 1;
16076 #endif
16077 }
16078 #endif
16079 }
16080
16081 #ifdef HAVE_WINDOW_SYSTEM
16082 if (FRAME_WINDOW_P (f)
16083 && update_window_fringes (w, (just_this_one_p
16084 || (!used_current_matrix_p && !overlay_arrow_seen)
16085 || w->pseudo_window_p)))
16086 {
16087 update_begin (f);
16088 block_input ();
16089 if (draw_window_fringes (w, 1))
16090 x_draw_vertical_border (w);
16091 unblock_input ();
16092 update_end (f);
16093 }
16094 #endif /* HAVE_WINDOW_SYSTEM */
16095
16096 /* We go to this label, with fonts_changed_p set,
16097 if it is necessary to try again using larger glyph matrices.
16098 We have to redeem the scroll bar even in this case,
16099 because the loop in redisplay_internal expects that. */
16100 need_larger_matrices:
16101 ;
16102 finish_scroll_bars:
16103
16104 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16105 {
16106 /* Set the thumb's position and size. */
16107 set_vertical_scroll_bar (w);
16108
16109 /* Note that we actually used the scroll bar attached to this
16110 window, so it shouldn't be deleted at the end of redisplay. */
16111 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16112 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16113 }
16114
16115 /* Restore current_buffer and value of point in it. The window
16116 update may have changed the buffer, so first make sure `opoint'
16117 is still valid (Bug#6177). */
16118 if (CHARPOS (opoint) < BEGV)
16119 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16120 else if (CHARPOS (opoint) > ZV)
16121 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16122 else
16123 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16124
16125 set_buffer_internal_1 (old);
16126 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16127 shorter. This can be caused by log truncation in *Messages*. */
16128 if (CHARPOS (lpoint) <= ZV)
16129 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16130
16131 unbind_to (count, Qnil);
16132 }
16133
16134
16135 /* Build the complete desired matrix of WINDOW with a window start
16136 buffer position POS.
16137
16138 Value is 1 if successful. It is zero if fonts were loaded during
16139 redisplay which makes re-adjusting glyph matrices necessary, and -1
16140 if point would appear in the scroll margins.
16141 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16142 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16143 set in FLAGS.) */
16144
16145 int
16146 try_window (Lisp_Object window, struct text_pos pos, int flags)
16147 {
16148 struct window *w = XWINDOW (window);
16149 struct it it;
16150 struct glyph_row *last_text_row = NULL;
16151 struct frame *f = XFRAME (w->frame);
16152
16153 /* Make POS the new window start. */
16154 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16155
16156 /* Mark cursor position as unknown. No overlay arrow seen. */
16157 w->cursor.vpos = -1;
16158 overlay_arrow_seen = 0;
16159
16160 /* Initialize iterator and info to start at POS. */
16161 start_display (&it, w, pos);
16162
16163 /* Display all lines of W. */
16164 while (it.current_y < it.last_visible_y)
16165 {
16166 if (display_line (&it))
16167 last_text_row = it.glyph_row - 1;
16168 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16169 return 0;
16170 }
16171
16172 /* Don't let the cursor end in the scroll margins. */
16173 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16174 && !MINI_WINDOW_P (w))
16175 {
16176 int this_scroll_margin;
16177
16178 if (scroll_margin > 0)
16179 {
16180 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16181 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16182 }
16183 else
16184 this_scroll_margin = 0;
16185
16186 if ((w->cursor.y >= 0 /* not vscrolled */
16187 && w->cursor.y < this_scroll_margin
16188 && CHARPOS (pos) > BEGV
16189 && IT_CHARPOS (it) < ZV)
16190 /* rms: considering make_cursor_line_fully_visible_p here
16191 seems to give wrong results. We don't want to recenter
16192 when the last line is partly visible, we want to allow
16193 that case to be handled in the usual way. */
16194 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16195 {
16196 w->cursor.vpos = -1;
16197 clear_glyph_matrix (w->desired_matrix);
16198 return -1;
16199 }
16200 }
16201
16202 /* If bottom moved off end of frame, change mode line percentage. */
16203 if (XFASTINT (w->window_end_pos) <= 0
16204 && Z != IT_CHARPOS (it))
16205 w->update_mode_line = 1;
16206
16207 /* Set window_end_pos to the offset of the last character displayed
16208 on the window from the end of current_buffer. Set
16209 window_end_vpos to its row number. */
16210 if (last_text_row)
16211 {
16212 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16213 w->window_end_bytepos
16214 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16215 wset_window_end_pos
16216 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16217 wset_window_end_vpos
16218 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16219 eassert
16220 (MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->desired_matrix,
16221 XFASTINT (w->window_end_vpos))));
16222 }
16223 else
16224 {
16225 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16226 wset_window_end_pos (w, make_number (Z - ZV));
16227 wset_window_end_vpos (w, make_number (0));
16228 }
16229
16230 /* But that is not valid info until redisplay finishes. */
16231 w->window_end_valid = 0;
16232 return 1;
16233 }
16234
16235
16236 \f
16237 /************************************************************************
16238 Window redisplay reusing current matrix when buffer has not changed
16239 ************************************************************************/
16240
16241 /* Try redisplay of window W showing an unchanged buffer with a
16242 different window start than the last time it was displayed by
16243 reusing its current matrix. Value is non-zero if successful.
16244 W->start is the new window start. */
16245
16246 static int
16247 try_window_reusing_current_matrix (struct window *w)
16248 {
16249 struct frame *f = XFRAME (w->frame);
16250 struct glyph_row *bottom_row;
16251 struct it it;
16252 struct run run;
16253 struct text_pos start, new_start;
16254 int nrows_scrolled, i;
16255 struct glyph_row *last_text_row;
16256 struct glyph_row *last_reused_text_row;
16257 struct glyph_row *start_row;
16258 int start_vpos, min_y, max_y;
16259
16260 #ifdef GLYPH_DEBUG
16261 if (inhibit_try_window_reusing)
16262 return 0;
16263 #endif
16264
16265 if (/* This function doesn't handle terminal frames. */
16266 !FRAME_WINDOW_P (f)
16267 /* Don't try to reuse the display if windows have been split
16268 or such. */
16269 || windows_or_buffers_changed
16270 || cursor_type_changed)
16271 return 0;
16272
16273 /* Can't do this if region may have changed. */
16274 if (markpos_of_region () >= 0
16275 || w->region_showing
16276 || !NILP (Vshow_trailing_whitespace))
16277 return 0;
16278
16279 /* If top-line visibility has changed, give up. */
16280 if (WINDOW_WANTS_HEADER_LINE_P (w)
16281 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16282 return 0;
16283
16284 /* Give up if old or new display is scrolled vertically. We could
16285 make this function handle this, but right now it doesn't. */
16286 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16287 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16288 return 0;
16289
16290 /* The variable new_start now holds the new window start. The old
16291 start `start' can be determined from the current matrix. */
16292 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16293 start = start_row->minpos;
16294 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16295
16296 /* Clear the desired matrix for the display below. */
16297 clear_glyph_matrix (w->desired_matrix);
16298
16299 if (CHARPOS (new_start) <= CHARPOS (start))
16300 {
16301 /* Don't use this method if the display starts with an ellipsis
16302 displayed for invisible text. It's not easy to handle that case
16303 below, and it's certainly not worth the effort since this is
16304 not a frequent case. */
16305 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16306 return 0;
16307
16308 IF_DEBUG (debug_method_add (w, "twu1"));
16309
16310 /* Display up to a row that can be reused. The variable
16311 last_text_row is set to the last row displayed that displays
16312 text. Note that it.vpos == 0 if or if not there is a
16313 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16314 start_display (&it, w, new_start);
16315 w->cursor.vpos = -1;
16316 last_text_row = last_reused_text_row = NULL;
16317
16318 while (it.current_y < it.last_visible_y
16319 && !fonts_changed_p)
16320 {
16321 /* If we have reached into the characters in the START row,
16322 that means the line boundaries have changed. So we
16323 can't start copying with the row START. Maybe it will
16324 work to start copying with the following row. */
16325 while (IT_CHARPOS (it) > CHARPOS (start))
16326 {
16327 /* Advance to the next row as the "start". */
16328 start_row++;
16329 start = start_row->minpos;
16330 /* If there are no more rows to try, or just one, give up. */
16331 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16332 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16333 || CHARPOS (start) == ZV)
16334 {
16335 clear_glyph_matrix (w->desired_matrix);
16336 return 0;
16337 }
16338
16339 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16340 }
16341 /* If we have reached alignment, we can copy the rest of the
16342 rows. */
16343 if (IT_CHARPOS (it) == CHARPOS (start)
16344 /* Don't accept "alignment" inside a display vector,
16345 since start_row could have started in the middle of
16346 that same display vector (thus their character
16347 positions match), and we have no way of telling if
16348 that is the case. */
16349 && it.current.dpvec_index < 0)
16350 break;
16351
16352 if (display_line (&it))
16353 last_text_row = it.glyph_row - 1;
16354
16355 }
16356
16357 /* A value of current_y < last_visible_y means that we stopped
16358 at the previous window start, which in turn means that we
16359 have at least one reusable row. */
16360 if (it.current_y < it.last_visible_y)
16361 {
16362 struct glyph_row *row;
16363
16364 /* IT.vpos always starts from 0; it counts text lines. */
16365 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16366
16367 /* Find PT if not already found in the lines displayed. */
16368 if (w->cursor.vpos < 0)
16369 {
16370 int dy = it.current_y - start_row->y;
16371
16372 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16373 row = row_containing_pos (w, PT, row, NULL, dy);
16374 if (row)
16375 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16376 dy, nrows_scrolled);
16377 else
16378 {
16379 clear_glyph_matrix (w->desired_matrix);
16380 return 0;
16381 }
16382 }
16383
16384 /* Scroll the display. Do it before the current matrix is
16385 changed. The problem here is that update has not yet
16386 run, i.e. part of the current matrix is not up to date.
16387 scroll_run_hook will clear the cursor, and use the
16388 current matrix to get the height of the row the cursor is
16389 in. */
16390 run.current_y = start_row->y;
16391 run.desired_y = it.current_y;
16392 run.height = it.last_visible_y - it.current_y;
16393
16394 if (run.height > 0 && run.current_y != run.desired_y)
16395 {
16396 update_begin (f);
16397 FRAME_RIF (f)->update_window_begin_hook (w);
16398 FRAME_RIF (f)->clear_window_mouse_face (w);
16399 FRAME_RIF (f)->scroll_run_hook (w, &run);
16400 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16401 update_end (f);
16402 }
16403
16404 /* Shift current matrix down by nrows_scrolled lines. */
16405 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16406 rotate_matrix (w->current_matrix,
16407 start_vpos,
16408 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16409 nrows_scrolled);
16410
16411 /* Disable lines that must be updated. */
16412 for (i = 0; i < nrows_scrolled; ++i)
16413 (start_row + i)->enabled_p = 0;
16414
16415 /* Re-compute Y positions. */
16416 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16417 max_y = it.last_visible_y;
16418 for (row = start_row + nrows_scrolled;
16419 row < bottom_row;
16420 ++row)
16421 {
16422 row->y = it.current_y;
16423 row->visible_height = row->height;
16424
16425 if (row->y < min_y)
16426 row->visible_height -= min_y - row->y;
16427 if (row->y + row->height > max_y)
16428 row->visible_height -= row->y + row->height - max_y;
16429 if (row->fringe_bitmap_periodic_p)
16430 row->redraw_fringe_bitmaps_p = 1;
16431
16432 it.current_y += row->height;
16433
16434 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16435 last_reused_text_row = row;
16436 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16437 break;
16438 }
16439
16440 /* Disable lines in the current matrix which are now
16441 below the window. */
16442 for (++row; row < bottom_row; ++row)
16443 row->enabled_p = row->mode_line_p = 0;
16444 }
16445
16446 /* Update window_end_pos etc.; last_reused_text_row is the last
16447 reused row from the current matrix containing text, if any.
16448 The value of last_text_row is the last displayed line
16449 containing text. */
16450 if (last_reused_text_row)
16451 {
16452 w->window_end_bytepos
16453 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16454 wset_window_end_pos
16455 (w, make_number (Z
16456 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16457 wset_window_end_vpos
16458 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16459 w->current_matrix)));
16460 }
16461 else if (last_text_row)
16462 {
16463 w->window_end_bytepos
16464 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16465 wset_window_end_pos
16466 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16467 wset_window_end_vpos
16468 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16469 w->desired_matrix)));
16470 }
16471 else
16472 {
16473 /* This window must be completely empty. */
16474 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16475 wset_window_end_pos (w, make_number (Z - ZV));
16476 wset_window_end_vpos (w, make_number (0));
16477 }
16478 w->window_end_valid = 0;
16479
16480 /* Update hint: don't try scrolling again in update_window. */
16481 w->desired_matrix->no_scrolling_p = 1;
16482
16483 #ifdef GLYPH_DEBUG
16484 debug_method_add (w, "try_window_reusing_current_matrix 1");
16485 #endif
16486 return 1;
16487 }
16488 else if (CHARPOS (new_start) > CHARPOS (start))
16489 {
16490 struct glyph_row *pt_row, *row;
16491 struct glyph_row *first_reusable_row;
16492 struct glyph_row *first_row_to_display;
16493 int dy;
16494 int yb = window_text_bottom_y (w);
16495
16496 /* Find the row starting at new_start, if there is one. Don't
16497 reuse a partially visible line at the end. */
16498 first_reusable_row = start_row;
16499 while (first_reusable_row->enabled_p
16500 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16501 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16502 < CHARPOS (new_start)))
16503 ++first_reusable_row;
16504
16505 /* Give up if there is no row to reuse. */
16506 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16507 || !first_reusable_row->enabled_p
16508 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16509 != CHARPOS (new_start)))
16510 return 0;
16511
16512 /* We can reuse fully visible rows beginning with
16513 first_reusable_row to the end of the window. Set
16514 first_row_to_display to the first row that cannot be reused.
16515 Set pt_row to the row containing point, if there is any. */
16516 pt_row = NULL;
16517 for (first_row_to_display = first_reusable_row;
16518 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16519 ++first_row_to_display)
16520 {
16521 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16522 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16523 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16524 && first_row_to_display->ends_at_zv_p
16525 && pt_row == NULL)))
16526 pt_row = first_row_to_display;
16527 }
16528
16529 /* Start displaying at the start of first_row_to_display. */
16530 eassert (first_row_to_display->y < yb);
16531 init_to_row_start (&it, w, first_row_to_display);
16532
16533 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16534 - start_vpos);
16535 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16536 - nrows_scrolled);
16537 it.current_y = (first_row_to_display->y - first_reusable_row->y
16538 + WINDOW_HEADER_LINE_HEIGHT (w));
16539
16540 /* Display lines beginning with first_row_to_display in the
16541 desired matrix. Set last_text_row to the last row displayed
16542 that displays text. */
16543 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16544 if (pt_row == NULL)
16545 w->cursor.vpos = -1;
16546 last_text_row = NULL;
16547 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16548 if (display_line (&it))
16549 last_text_row = it.glyph_row - 1;
16550
16551 /* If point is in a reused row, adjust y and vpos of the cursor
16552 position. */
16553 if (pt_row)
16554 {
16555 w->cursor.vpos -= nrows_scrolled;
16556 w->cursor.y -= first_reusable_row->y - start_row->y;
16557 }
16558
16559 /* Give up if point isn't in a row displayed or reused. (This
16560 also handles the case where w->cursor.vpos < nrows_scrolled
16561 after the calls to display_line, which can happen with scroll
16562 margins. See bug#1295.) */
16563 if (w->cursor.vpos < 0)
16564 {
16565 clear_glyph_matrix (w->desired_matrix);
16566 return 0;
16567 }
16568
16569 /* Scroll the display. */
16570 run.current_y = first_reusable_row->y;
16571 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16572 run.height = it.last_visible_y - run.current_y;
16573 dy = run.current_y - run.desired_y;
16574
16575 if (run.height)
16576 {
16577 update_begin (f);
16578 FRAME_RIF (f)->update_window_begin_hook (w);
16579 FRAME_RIF (f)->clear_window_mouse_face (w);
16580 FRAME_RIF (f)->scroll_run_hook (w, &run);
16581 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16582 update_end (f);
16583 }
16584
16585 /* Adjust Y positions of reused rows. */
16586 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16587 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16588 max_y = it.last_visible_y;
16589 for (row = first_reusable_row; row < first_row_to_display; ++row)
16590 {
16591 row->y -= dy;
16592 row->visible_height = row->height;
16593 if (row->y < min_y)
16594 row->visible_height -= min_y - row->y;
16595 if (row->y + row->height > max_y)
16596 row->visible_height -= row->y + row->height - max_y;
16597 if (row->fringe_bitmap_periodic_p)
16598 row->redraw_fringe_bitmaps_p = 1;
16599 }
16600
16601 /* Scroll the current matrix. */
16602 eassert (nrows_scrolled > 0);
16603 rotate_matrix (w->current_matrix,
16604 start_vpos,
16605 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16606 -nrows_scrolled);
16607
16608 /* Disable rows not reused. */
16609 for (row -= nrows_scrolled; row < bottom_row; ++row)
16610 row->enabled_p = 0;
16611
16612 /* Point may have moved to a different line, so we cannot assume that
16613 the previous cursor position is valid; locate the correct row. */
16614 if (pt_row)
16615 {
16616 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16617 row < bottom_row
16618 && PT >= MATRIX_ROW_END_CHARPOS (row)
16619 && !row->ends_at_zv_p;
16620 row++)
16621 {
16622 w->cursor.vpos++;
16623 w->cursor.y = row->y;
16624 }
16625 if (row < bottom_row)
16626 {
16627 /* Can't simply scan the row for point with
16628 bidi-reordered glyph rows. Let set_cursor_from_row
16629 figure out where to put the cursor, and if it fails,
16630 give up. */
16631 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering)))
16632 {
16633 if (!set_cursor_from_row (w, row, w->current_matrix,
16634 0, 0, 0, 0))
16635 {
16636 clear_glyph_matrix (w->desired_matrix);
16637 return 0;
16638 }
16639 }
16640 else
16641 {
16642 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16643 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16644
16645 for (; glyph < end
16646 && (!BUFFERP (glyph->object)
16647 || glyph->charpos < PT);
16648 glyph++)
16649 {
16650 w->cursor.hpos++;
16651 w->cursor.x += glyph->pixel_width;
16652 }
16653 }
16654 }
16655 }
16656
16657 /* Adjust window end. A null value of last_text_row means that
16658 the window end is in reused rows which in turn means that
16659 only its vpos can have changed. */
16660 if (last_text_row)
16661 {
16662 w->window_end_bytepos
16663 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16664 wset_window_end_pos
16665 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16666 wset_window_end_vpos
16667 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16668 w->desired_matrix)));
16669 }
16670 else
16671 {
16672 wset_window_end_vpos
16673 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16674 }
16675
16676 w->window_end_valid = 0;
16677 w->desired_matrix->no_scrolling_p = 1;
16678
16679 #ifdef GLYPH_DEBUG
16680 debug_method_add (w, "try_window_reusing_current_matrix 2");
16681 #endif
16682 return 1;
16683 }
16684
16685 return 0;
16686 }
16687
16688
16689 \f
16690 /************************************************************************
16691 Window redisplay reusing current matrix when buffer has changed
16692 ************************************************************************/
16693
16694 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16695 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16696 ptrdiff_t *, ptrdiff_t *);
16697 static struct glyph_row *
16698 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16699 struct glyph_row *);
16700
16701
16702 /* Return the last row in MATRIX displaying text. If row START is
16703 non-null, start searching with that row. IT gives the dimensions
16704 of the display. Value is null if matrix is empty; otherwise it is
16705 a pointer to the row found. */
16706
16707 static struct glyph_row *
16708 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16709 struct glyph_row *start)
16710 {
16711 struct glyph_row *row, *row_found;
16712
16713 /* Set row_found to the last row in IT->w's current matrix
16714 displaying text. The loop looks funny but think of partially
16715 visible lines. */
16716 row_found = NULL;
16717 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16718 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16719 {
16720 eassert (row->enabled_p);
16721 row_found = row;
16722 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16723 break;
16724 ++row;
16725 }
16726
16727 return row_found;
16728 }
16729
16730
16731 /* Return the last row in the current matrix of W that is not affected
16732 by changes at the start of current_buffer that occurred since W's
16733 current matrix was built. Value is null if no such row exists.
16734
16735 BEG_UNCHANGED us the number of characters unchanged at the start of
16736 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16737 first changed character in current_buffer. Characters at positions <
16738 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16739 when the current matrix was built. */
16740
16741 static struct glyph_row *
16742 find_last_unchanged_at_beg_row (struct window *w)
16743 {
16744 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16745 struct glyph_row *row;
16746 struct glyph_row *row_found = NULL;
16747 int yb = window_text_bottom_y (w);
16748
16749 /* Find the last row displaying unchanged text. */
16750 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16751 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16752 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16753 ++row)
16754 {
16755 if (/* If row ends before first_changed_pos, it is unchanged,
16756 except in some case. */
16757 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16758 /* When row ends in ZV and we write at ZV it is not
16759 unchanged. */
16760 && !row->ends_at_zv_p
16761 /* When first_changed_pos is the end of a continued line,
16762 row is not unchanged because it may be no longer
16763 continued. */
16764 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16765 && (row->continued_p
16766 || row->exact_window_width_line_p))
16767 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16768 needs to be recomputed, so don't consider this row as
16769 unchanged. This happens when the last line was
16770 bidi-reordered and was killed immediately before this
16771 redisplay cycle. In that case, ROW->end stores the
16772 buffer position of the first visual-order character of
16773 the killed text, which is now beyond ZV. */
16774 && CHARPOS (row->end.pos) <= ZV)
16775 row_found = row;
16776
16777 /* Stop if last visible row. */
16778 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16779 break;
16780 }
16781
16782 return row_found;
16783 }
16784
16785
16786 /* Find the first glyph row in the current matrix of W that is not
16787 affected by changes at the end of current_buffer since the
16788 time W's current matrix was built.
16789
16790 Return in *DELTA the number of chars by which buffer positions in
16791 unchanged text at the end of current_buffer must be adjusted.
16792
16793 Return in *DELTA_BYTES the corresponding number of bytes.
16794
16795 Value is null if no such row exists, i.e. all rows are affected by
16796 changes. */
16797
16798 static struct glyph_row *
16799 find_first_unchanged_at_end_row (struct window *w,
16800 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16801 {
16802 struct glyph_row *row;
16803 struct glyph_row *row_found = NULL;
16804
16805 *delta = *delta_bytes = 0;
16806
16807 /* Display must not have been paused, otherwise the current matrix
16808 is not up to date. */
16809 eassert (w->window_end_valid);
16810
16811 /* A value of window_end_pos >= END_UNCHANGED means that the window
16812 end is in the range of changed text. If so, there is no
16813 unchanged row at the end of W's current matrix. */
16814 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
16815 return NULL;
16816
16817 /* Set row to the last row in W's current matrix displaying text. */
16818 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
16819
16820 /* If matrix is entirely empty, no unchanged row exists. */
16821 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16822 {
16823 /* The value of row is the last glyph row in the matrix having a
16824 meaningful buffer position in it. The end position of row
16825 corresponds to window_end_pos. This allows us to translate
16826 buffer positions in the current matrix to current buffer
16827 positions for characters not in changed text. */
16828 ptrdiff_t Z_old =
16829 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
16830 ptrdiff_t Z_BYTE_old =
16831 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
16832 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
16833 struct glyph_row *first_text_row
16834 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16835
16836 *delta = Z - Z_old;
16837 *delta_bytes = Z_BYTE - Z_BYTE_old;
16838
16839 /* Set last_unchanged_pos to the buffer position of the last
16840 character in the buffer that has not been changed. Z is the
16841 index + 1 of the last character in current_buffer, i.e. by
16842 subtracting END_UNCHANGED we get the index of the last
16843 unchanged character, and we have to add BEG to get its buffer
16844 position. */
16845 last_unchanged_pos = Z - END_UNCHANGED + BEG;
16846 last_unchanged_pos_old = last_unchanged_pos - *delta;
16847
16848 /* Search backward from ROW for a row displaying a line that
16849 starts at a minimum position >= last_unchanged_pos_old. */
16850 for (; row > first_text_row; --row)
16851 {
16852 /* This used to abort, but it can happen.
16853 It is ok to just stop the search instead here. KFS. */
16854 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
16855 break;
16856
16857 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
16858 row_found = row;
16859 }
16860 }
16861
16862 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
16863
16864 return row_found;
16865 }
16866
16867
16868 /* Make sure that glyph rows in the current matrix of window W
16869 reference the same glyph memory as corresponding rows in the
16870 frame's frame matrix. This function is called after scrolling W's
16871 current matrix on a terminal frame in try_window_id and
16872 try_window_reusing_current_matrix. */
16873
16874 static void
16875 sync_frame_with_window_matrix_rows (struct window *w)
16876 {
16877 struct frame *f = XFRAME (w->frame);
16878 struct glyph_row *window_row, *window_row_end, *frame_row;
16879
16880 /* Preconditions: W must be a leaf window and full-width. Its frame
16881 must have a frame matrix. */
16882 eassert (BUFFERP (w->contents));
16883 eassert (WINDOW_FULL_WIDTH_P (w));
16884 eassert (!FRAME_WINDOW_P (f));
16885
16886 /* If W is a full-width window, glyph pointers in W's current matrix
16887 have, by definition, to be the same as glyph pointers in the
16888 corresponding frame matrix. Note that frame matrices have no
16889 marginal areas (see build_frame_matrix). */
16890 window_row = w->current_matrix->rows;
16891 window_row_end = window_row + w->current_matrix->nrows;
16892 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
16893 while (window_row < window_row_end)
16894 {
16895 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
16896 struct glyph *end = window_row->glyphs[LAST_AREA];
16897
16898 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
16899 frame_row->glyphs[TEXT_AREA] = start;
16900 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
16901 frame_row->glyphs[LAST_AREA] = end;
16902
16903 /* Disable frame rows whose corresponding window rows have
16904 been disabled in try_window_id. */
16905 if (!window_row->enabled_p)
16906 frame_row->enabled_p = 0;
16907
16908 ++window_row, ++frame_row;
16909 }
16910 }
16911
16912
16913 /* Find the glyph row in window W containing CHARPOS. Consider all
16914 rows between START and END (not inclusive). END null means search
16915 all rows to the end of the display area of W. Value is the row
16916 containing CHARPOS or null. */
16917
16918 struct glyph_row *
16919 row_containing_pos (struct window *w, ptrdiff_t charpos,
16920 struct glyph_row *start, struct glyph_row *end, int dy)
16921 {
16922 struct glyph_row *row = start;
16923 struct glyph_row *best_row = NULL;
16924 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->contents)) + 1;
16925 int last_y;
16926
16927 /* If we happen to start on a header-line, skip that. */
16928 if (row->mode_line_p)
16929 ++row;
16930
16931 if ((end && row >= end) || !row->enabled_p)
16932 return NULL;
16933
16934 last_y = window_text_bottom_y (w) - dy;
16935
16936 while (1)
16937 {
16938 /* Give up if we have gone too far. */
16939 if (end && row >= end)
16940 return NULL;
16941 /* This formerly returned if they were equal.
16942 I think that both quantities are of a "last plus one" type;
16943 if so, when they are equal, the row is within the screen. -- rms. */
16944 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
16945 return NULL;
16946
16947 /* If it is in this row, return this row. */
16948 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
16949 || (MATRIX_ROW_END_CHARPOS (row) == charpos
16950 /* The end position of a row equals the start
16951 position of the next row. If CHARPOS is there, we
16952 would rather consider it displayed in the next
16953 line, except when this line ends in ZV. */
16954 && !row_for_charpos_p (row, charpos)))
16955 && charpos >= MATRIX_ROW_START_CHARPOS (row))
16956 {
16957 struct glyph *g;
16958
16959 if (NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
16960 || (!best_row && !row->continued_p))
16961 return row;
16962 /* In bidi-reordered rows, there could be several rows whose
16963 edges surround CHARPOS, all of these rows belonging to
16964 the same continued line. We need to find the row which
16965 fits CHARPOS the best. */
16966 for (g = row->glyphs[TEXT_AREA];
16967 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16968 g++)
16969 {
16970 if (!STRINGP (g->object))
16971 {
16972 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
16973 {
16974 mindif = eabs (g->charpos - charpos);
16975 best_row = row;
16976 /* Exact match always wins. */
16977 if (mindif == 0)
16978 return best_row;
16979 }
16980 }
16981 }
16982 }
16983 else if (best_row && !row->continued_p)
16984 return best_row;
16985 ++row;
16986 }
16987 }
16988
16989
16990 /* Try to redisplay window W by reusing its existing display. W's
16991 current matrix must be up to date when this function is called,
16992 i.e. window_end_valid must be nonzero.
16993
16994 Value is
16995
16996 1 if display has been updated
16997 0 if otherwise unsuccessful
16998 -1 if redisplay with same window start is known not to succeed
16999
17000 The following steps are performed:
17001
17002 1. Find the last row in the current matrix of W that is not
17003 affected by changes at the start of current_buffer. If no such row
17004 is found, give up.
17005
17006 2. Find the first row in W's current matrix that is not affected by
17007 changes at the end of current_buffer. Maybe there is no such row.
17008
17009 3. Display lines beginning with the row + 1 found in step 1 to the
17010 row found in step 2 or, if step 2 didn't find a row, to the end of
17011 the window.
17012
17013 4. If cursor is not known to appear on the window, give up.
17014
17015 5. If display stopped at the row found in step 2, scroll the
17016 display and current matrix as needed.
17017
17018 6. Maybe display some lines at the end of W, if we must. This can
17019 happen under various circumstances, like a partially visible line
17020 becoming fully visible, or because newly displayed lines are displayed
17021 in smaller font sizes.
17022
17023 7. Update W's window end information. */
17024
17025 static int
17026 try_window_id (struct window *w)
17027 {
17028 struct frame *f = XFRAME (w->frame);
17029 struct glyph_matrix *current_matrix = w->current_matrix;
17030 struct glyph_matrix *desired_matrix = w->desired_matrix;
17031 struct glyph_row *last_unchanged_at_beg_row;
17032 struct glyph_row *first_unchanged_at_end_row;
17033 struct glyph_row *row;
17034 struct glyph_row *bottom_row;
17035 int bottom_vpos;
17036 struct it it;
17037 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17038 int dvpos, dy;
17039 struct text_pos start_pos;
17040 struct run run;
17041 int first_unchanged_at_end_vpos = 0;
17042 struct glyph_row *last_text_row, *last_text_row_at_end;
17043 struct text_pos start;
17044 ptrdiff_t first_changed_charpos, last_changed_charpos;
17045
17046 #ifdef GLYPH_DEBUG
17047 if (inhibit_try_window_id)
17048 return 0;
17049 #endif
17050
17051 /* This is handy for debugging. */
17052 #if 0
17053 #define GIVE_UP(X) \
17054 do { \
17055 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17056 return 0; \
17057 } while (0)
17058 #else
17059 #define GIVE_UP(X) return 0
17060 #endif
17061
17062 SET_TEXT_POS_FROM_MARKER (start, w->start);
17063
17064 /* Don't use this for mini-windows because these can show
17065 messages and mini-buffers, and we don't handle that here. */
17066 if (MINI_WINDOW_P (w))
17067 GIVE_UP (1);
17068
17069 /* This flag is used to prevent redisplay optimizations. */
17070 if (windows_or_buffers_changed || cursor_type_changed)
17071 GIVE_UP (2);
17072
17073 /* Verify that narrowing has not changed.
17074 Also verify that we were not told to prevent redisplay optimizations.
17075 It would be nice to further
17076 reduce the number of cases where this prevents try_window_id. */
17077 if (current_buffer->clip_changed
17078 || current_buffer->prevent_redisplay_optimizations_p)
17079 GIVE_UP (3);
17080
17081 /* Window must either use window-based redisplay or be full width. */
17082 if (!FRAME_WINDOW_P (f)
17083 && (!FRAME_LINE_INS_DEL_OK (f)
17084 || !WINDOW_FULL_WIDTH_P (w)))
17085 GIVE_UP (4);
17086
17087 /* Give up if point is known NOT to appear in W. */
17088 if (PT < CHARPOS (start))
17089 GIVE_UP (5);
17090
17091 /* Another way to prevent redisplay optimizations. */
17092 if (w->last_modified == 0)
17093 GIVE_UP (6);
17094
17095 /* Verify that window is not hscrolled. */
17096 if (w->hscroll != 0)
17097 GIVE_UP (7);
17098
17099 /* Verify that display wasn't paused. */
17100 if (!w->window_end_valid)
17101 GIVE_UP (8);
17102
17103 /* Can't use this if highlighting a region because a cursor movement
17104 will do more than just set the cursor. */
17105 if (markpos_of_region () >= 0)
17106 GIVE_UP (9);
17107
17108 /* Likewise if highlighting trailing whitespace. */
17109 if (!NILP (Vshow_trailing_whitespace))
17110 GIVE_UP (11);
17111
17112 /* Likewise if showing a region. */
17113 if (w->region_showing)
17114 GIVE_UP (10);
17115
17116 /* Can't use this if overlay arrow position and/or string have
17117 changed. */
17118 if (overlay_arrows_changed_p ())
17119 GIVE_UP (12);
17120
17121 /* When word-wrap is on, adding a space to the first word of a
17122 wrapped line can change the wrap position, altering the line
17123 above it. It might be worthwhile to handle this more
17124 intelligently, but for now just redisplay from scratch. */
17125 if (!NILP (BVAR (XBUFFER (w->contents), word_wrap)))
17126 GIVE_UP (21);
17127
17128 /* Under bidi reordering, adding or deleting a character in the
17129 beginning of a paragraph, before the first strong directional
17130 character, can change the base direction of the paragraph (unless
17131 the buffer specifies a fixed paragraph direction), which will
17132 require to redisplay the whole paragraph. It might be worthwhile
17133 to find the paragraph limits and widen the range of redisplayed
17134 lines to that, but for now just give up this optimization and
17135 redisplay from scratch. */
17136 if (!NILP (BVAR (XBUFFER (w->contents), bidi_display_reordering))
17137 && NILP (BVAR (XBUFFER (w->contents), bidi_paragraph_direction)))
17138 GIVE_UP (22);
17139
17140 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17141 only if buffer has really changed. The reason is that the gap is
17142 initially at Z for freshly visited files. The code below would
17143 set end_unchanged to 0 in that case. */
17144 if (MODIFF > SAVE_MODIFF
17145 /* This seems to happen sometimes after saving a buffer. */
17146 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17147 {
17148 if (GPT - BEG < BEG_UNCHANGED)
17149 BEG_UNCHANGED = GPT - BEG;
17150 if (Z - GPT < END_UNCHANGED)
17151 END_UNCHANGED = Z - GPT;
17152 }
17153
17154 /* The position of the first and last character that has been changed. */
17155 first_changed_charpos = BEG + BEG_UNCHANGED;
17156 last_changed_charpos = Z - END_UNCHANGED;
17157
17158 /* If window starts after a line end, and the last change is in
17159 front of that newline, then changes don't affect the display.
17160 This case happens with stealth-fontification. Note that although
17161 the display is unchanged, glyph positions in the matrix have to
17162 be adjusted, of course. */
17163 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17164 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17165 && ((last_changed_charpos < CHARPOS (start)
17166 && CHARPOS (start) == BEGV)
17167 || (last_changed_charpos < CHARPOS (start) - 1
17168 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17169 {
17170 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17171 struct glyph_row *r0;
17172
17173 /* Compute how many chars/bytes have been added to or removed
17174 from the buffer. */
17175 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17176 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17177 Z_delta = Z - Z_old;
17178 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17179
17180 /* Give up if PT is not in the window. Note that it already has
17181 been checked at the start of try_window_id that PT is not in
17182 front of the window start. */
17183 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17184 GIVE_UP (13);
17185
17186 /* If window start is unchanged, we can reuse the whole matrix
17187 as is, after adjusting glyph positions. No need to compute
17188 the window end again, since its offset from Z hasn't changed. */
17189 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17190 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17191 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17192 /* PT must not be in a partially visible line. */
17193 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17194 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17195 {
17196 /* Adjust positions in the glyph matrix. */
17197 if (Z_delta || Z_delta_bytes)
17198 {
17199 struct glyph_row *r1
17200 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17201 increment_matrix_positions (w->current_matrix,
17202 MATRIX_ROW_VPOS (r0, current_matrix),
17203 MATRIX_ROW_VPOS (r1, current_matrix),
17204 Z_delta, Z_delta_bytes);
17205 }
17206
17207 /* Set the cursor. */
17208 row = row_containing_pos (w, PT, r0, NULL, 0);
17209 if (row)
17210 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17211 else
17212 emacs_abort ();
17213 return 1;
17214 }
17215 }
17216
17217 /* Handle the case that changes are all below what is displayed in
17218 the window, and that PT is in the window. This shortcut cannot
17219 be taken if ZV is visible in the window, and text has been added
17220 there that is visible in the window. */
17221 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17222 /* ZV is not visible in the window, or there are no
17223 changes at ZV, actually. */
17224 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17225 || first_changed_charpos == last_changed_charpos))
17226 {
17227 struct glyph_row *r0;
17228
17229 /* Give up if PT is not in the window. Note that it already has
17230 been checked at the start of try_window_id that PT is not in
17231 front of the window start. */
17232 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17233 GIVE_UP (14);
17234
17235 /* If window start is unchanged, we can reuse the whole matrix
17236 as is, without changing glyph positions since no text has
17237 been added/removed in front of the window end. */
17238 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17239 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17240 /* PT must not be in a partially visible line. */
17241 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17242 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17243 {
17244 /* We have to compute the window end anew since text
17245 could have been added/removed after it. */
17246 wset_window_end_pos
17247 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17248 w->window_end_bytepos
17249 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17250
17251 /* Set the cursor. */
17252 row = row_containing_pos (w, PT, r0, NULL, 0);
17253 if (row)
17254 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17255 else
17256 emacs_abort ();
17257 return 2;
17258 }
17259 }
17260
17261 /* Give up if window start is in the changed area.
17262
17263 The condition used to read
17264
17265 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17266
17267 but why that was tested escapes me at the moment. */
17268 if (CHARPOS (start) >= first_changed_charpos
17269 && CHARPOS (start) <= last_changed_charpos)
17270 GIVE_UP (15);
17271
17272 /* Check that window start agrees with the start of the first glyph
17273 row in its current matrix. Check this after we know the window
17274 start is not in changed text, otherwise positions would not be
17275 comparable. */
17276 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17277 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17278 GIVE_UP (16);
17279
17280 /* Give up if the window ends in strings. Overlay strings
17281 at the end are difficult to handle, so don't try. */
17282 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17283 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17284 GIVE_UP (20);
17285
17286 /* Compute the position at which we have to start displaying new
17287 lines. Some of the lines at the top of the window might be
17288 reusable because they are not displaying changed text. Find the
17289 last row in W's current matrix not affected by changes at the
17290 start of current_buffer. Value is null if changes start in the
17291 first line of window. */
17292 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17293 if (last_unchanged_at_beg_row)
17294 {
17295 /* Avoid starting to display in the middle of a character, a TAB
17296 for instance. This is easier than to set up the iterator
17297 exactly, and it's not a frequent case, so the additional
17298 effort wouldn't really pay off. */
17299 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17300 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17301 && last_unchanged_at_beg_row > w->current_matrix->rows)
17302 --last_unchanged_at_beg_row;
17303
17304 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17305 GIVE_UP (17);
17306
17307 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17308 GIVE_UP (18);
17309 start_pos = it.current.pos;
17310
17311 /* Start displaying new lines in the desired matrix at the same
17312 vpos we would use in the current matrix, i.e. below
17313 last_unchanged_at_beg_row. */
17314 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17315 current_matrix);
17316 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17317 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17318
17319 eassert (it.hpos == 0 && it.current_x == 0);
17320 }
17321 else
17322 {
17323 /* There are no reusable lines at the start of the window.
17324 Start displaying in the first text line. */
17325 start_display (&it, w, start);
17326 it.vpos = it.first_vpos;
17327 start_pos = it.current.pos;
17328 }
17329
17330 /* Find the first row that is not affected by changes at the end of
17331 the buffer. Value will be null if there is no unchanged row, in
17332 which case we must redisplay to the end of the window. delta
17333 will be set to the value by which buffer positions beginning with
17334 first_unchanged_at_end_row have to be adjusted due to text
17335 changes. */
17336 first_unchanged_at_end_row
17337 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17338 IF_DEBUG (debug_delta = delta);
17339 IF_DEBUG (debug_delta_bytes = delta_bytes);
17340
17341 /* Set stop_pos to the buffer position up to which we will have to
17342 display new lines. If first_unchanged_at_end_row != NULL, this
17343 is the buffer position of the start of the line displayed in that
17344 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17345 that we don't stop at a buffer position. */
17346 stop_pos = 0;
17347 if (first_unchanged_at_end_row)
17348 {
17349 eassert (last_unchanged_at_beg_row == NULL
17350 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17351
17352 /* If this is a continuation line, move forward to the next one
17353 that isn't. Changes in lines above affect this line.
17354 Caution: this may move first_unchanged_at_end_row to a row
17355 not displaying text. */
17356 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17357 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17358 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17359 < it.last_visible_y))
17360 ++first_unchanged_at_end_row;
17361
17362 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17363 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17364 >= it.last_visible_y))
17365 first_unchanged_at_end_row = NULL;
17366 else
17367 {
17368 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17369 + delta);
17370 first_unchanged_at_end_vpos
17371 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17372 eassert (stop_pos >= Z - END_UNCHANGED);
17373 }
17374 }
17375 else if (last_unchanged_at_beg_row == NULL)
17376 GIVE_UP (19);
17377
17378
17379 #ifdef GLYPH_DEBUG
17380
17381 /* Either there is no unchanged row at the end, or the one we have
17382 now displays text. This is a necessary condition for the window
17383 end pos calculation at the end of this function. */
17384 eassert (first_unchanged_at_end_row == NULL
17385 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17386
17387 debug_last_unchanged_at_beg_vpos
17388 = (last_unchanged_at_beg_row
17389 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17390 : -1);
17391 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17392
17393 #endif /* GLYPH_DEBUG */
17394
17395
17396 /* Display new lines. Set last_text_row to the last new line
17397 displayed which has text on it, i.e. might end up as being the
17398 line where the window_end_vpos is. */
17399 w->cursor.vpos = -1;
17400 last_text_row = NULL;
17401 overlay_arrow_seen = 0;
17402 while (it.current_y < it.last_visible_y
17403 && !fonts_changed_p
17404 && (first_unchanged_at_end_row == NULL
17405 || IT_CHARPOS (it) < stop_pos))
17406 {
17407 if (display_line (&it))
17408 last_text_row = it.glyph_row - 1;
17409 }
17410
17411 if (fonts_changed_p)
17412 return -1;
17413
17414
17415 /* Compute differences in buffer positions, y-positions etc. for
17416 lines reused at the bottom of the window. Compute what we can
17417 scroll. */
17418 if (first_unchanged_at_end_row
17419 /* No lines reused because we displayed everything up to the
17420 bottom of the window. */
17421 && it.current_y < it.last_visible_y)
17422 {
17423 dvpos = (it.vpos
17424 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17425 current_matrix));
17426 dy = it.current_y - first_unchanged_at_end_row->y;
17427 run.current_y = first_unchanged_at_end_row->y;
17428 run.desired_y = run.current_y + dy;
17429 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17430 }
17431 else
17432 {
17433 delta = delta_bytes = dvpos = dy
17434 = run.current_y = run.desired_y = run.height = 0;
17435 first_unchanged_at_end_row = NULL;
17436 }
17437 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17438
17439
17440 /* Find the cursor if not already found. We have to decide whether
17441 PT will appear on this window (it sometimes doesn't, but this is
17442 not a very frequent case.) This decision has to be made before
17443 the current matrix is altered. A value of cursor.vpos < 0 means
17444 that PT is either in one of the lines beginning at
17445 first_unchanged_at_end_row or below the window. Don't care for
17446 lines that might be displayed later at the window end; as
17447 mentioned, this is not a frequent case. */
17448 if (w->cursor.vpos < 0)
17449 {
17450 /* Cursor in unchanged rows at the top? */
17451 if (PT < CHARPOS (start_pos)
17452 && last_unchanged_at_beg_row)
17453 {
17454 row = row_containing_pos (w, PT,
17455 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17456 last_unchanged_at_beg_row + 1, 0);
17457 if (row)
17458 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17459 }
17460
17461 /* Start from first_unchanged_at_end_row looking for PT. */
17462 else if (first_unchanged_at_end_row)
17463 {
17464 row = row_containing_pos (w, PT - delta,
17465 first_unchanged_at_end_row, NULL, 0);
17466 if (row)
17467 set_cursor_from_row (w, row, w->current_matrix, delta,
17468 delta_bytes, dy, dvpos);
17469 }
17470
17471 /* Give up if cursor was not found. */
17472 if (w->cursor.vpos < 0)
17473 {
17474 clear_glyph_matrix (w->desired_matrix);
17475 return -1;
17476 }
17477 }
17478
17479 /* Don't let the cursor end in the scroll margins. */
17480 {
17481 int this_scroll_margin, cursor_height;
17482
17483 this_scroll_margin =
17484 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17485 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17486 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17487
17488 if ((w->cursor.y < this_scroll_margin
17489 && CHARPOS (start) > BEGV)
17490 /* Old redisplay didn't take scroll margin into account at the bottom,
17491 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17492 || (w->cursor.y + (make_cursor_line_fully_visible_p
17493 ? cursor_height + this_scroll_margin
17494 : 1)) > it.last_visible_y)
17495 {
17496 w->cursor.vpos = -1;
17497 clear_glyph_matrix (w->desired_matrix);
17498 return -1;
17499 }
17500 }
17501
17502 /* Scroll the display. Do it before changing the current matrix so
17503 that xterm.c doesn't get confused about where the cursor glyph is
17504 found. */
17505 if (dy && run.height)
17506 {
17507 update_begin (f);
17508
17509 if (FRAME_WINDOW_P (f))
17510 {
17511 FRAME_RIF (f)->update_window_begin_hook (w);
17512 FRAME_RIF (f)->clear_window_mouse_face (w);
17513 FRAME_RIF (f)->scroll_run_hook (w, &run);
17514 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17515 }
17516 else
17517 {
17518 /* Terminal frame. In this case, dvpos gives the number of
17519 lines to scroll by; dvpos < 0 means scroll up. */
17520 int from_vpos
17521 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17522 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17523 int end = (WINDOW_TOP_EDGE_LINE (w)
17524 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17525 + window_internal_height (w));
17526
17527 #if defined (HAVE_GPM) || defined (MSDOS)
17528 x_clear_window_mouse_face (w);
17529 #endif
17530 /* Perform the operation on the screen. */
17531 if (dvpos > 0)
17532 {
17533 /* Scroll last_unchanged_at_beg_row to the end of the
17534 window down dvpos lines. */
17535 set_terminal_window (f, end);
17536
17537 /* On dumb terminals delete dvpos lines at the end
17538 before inserting dvpos empty lines. */
17539 if (!FRAME_SCROLL_REGION_OK (f))
17540 ins_del_lines (f, end - dvpos, -dvpos);
17541
17542 /* Insert dvpos empty lines in front of
17543 last_unchanged_at_beg_row. */
17544 ins_del_lines (f, from, dvpos);
17545 }
17546 else if (dvpos < 0)
17547 {
17548 /* Scroll up last_unchanged_at_beg_vpos to the end of
17549 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17550 set_terminal_window (f, end);
17551
17552 /* Delete dvpos lines in front of
17553 last_unchanged_at_beg_vpos. ins_del_lines will set
17554 the cursor to the given vpos and emit |dvpos| delete
17555 line sequences. */
17556 ins_del_lines (f, from + dvpos, dvpos);
17557
17558 /* On a dumb terminal insert dvpos empty lines at the
17559 end. */
17560 if (!FRAME_SCROLL_REGION_OK (f))
17561 ins_del_lines (f, end + dvpos, -dvpos);
17562 }
17563
17564 set_terminal_window (f, 0);
17565 }
17566
17567 update_end (f);
17568 }
17569
17570 /* Shift reused rows of the current matrix to the right position.
17571 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17572 text. */
17573 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17574 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17575 if (dvpos < 0)
17576 {
17577 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17578 bottom_vpos, dvpos);
17579 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17580 bottom_vpos);
17581 }
17582 else if (dvpos > 0)
17583 {
17584 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17585 bottom_vpos, dvpos);
17586 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17587 first_unchanged_at_end_vpos + dvpos);
17588 }
17589
17590 /* For frame-based redisplay, make sure that current frame and window
17591 matrix are in sync with respect to glyph memory. */
17592 if (!FRAME_WINDOW_P (f))
17593 sync_frame_with_window_matrix_rows (w);
17594
17595 /* Adjust buffer positions in reused rows. */
17596 if (delta || delta_bytes)
17597 increment_matrix_positions (current_matrix,
17598 first_unchanged_at_end_vpos + dvpos,
17599 bottom_vpos, delta, delta_bytes);
17600
17601 /* Adjust Y positions. */
17602 if (dy)
17603 shift_glyph_matrix (w, current_matrix,
17604 first_unchanged_at_end_vpos + dvpos,
17605 bottom_vpos, dy);
17606
17607 if (first_unchanged_at_end_row)
17608 {
17609 first_unchanged_at_end_row += dvpos;
17610 if (first_unchanged_at_end_row->y >= it.last_visible_y
17611 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17612 first_unchanged_at_end_row = NULL;
17613 }
17614
17615 /* If scrolling up, there may be some lines to display at the end of
17616 the window. */
17617 last_text_row_at_end = NULL;
17618 if (dy < 0)
17619 {
17620 /* Scrolling up can leave for example a partially visible line
17621 at the end of the window to be redisplayed. */
17622 /* Set last_row to the glyph row in the current matrix where the
17623 window end line is found. It has been moved up or down in
17624 the matrix by dvpos. */
17625 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17626 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17627
17628 /* If last_row is the window end line, it should display text. */
17629 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_row));
17630
17631 /* If window end line was partially visible before, begin
17632 displaying at that line. Otherwise begin displaying with the
17633 line following it. */
17634 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17635 {
17636 init_to_row_start (&it, w, last_row);
17637 it.vpos = last_vpos;
17638 it.current_y = last_row->y;
17639 }
17640 else
17641 {
17642 init_to_row_end (&it, w, last_row);
17643 it.vpos = 1 + last_vpos;
17644 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17645 ++last_row;
17646 }
17647
17648 /* We may start in a continuation line. If so, we have to
17649 get the right continuation_lines_width and current_x. */
17650 it.continuation_lines_width = last_row->continuation_lines_width;
17651 it.hpos = it.current_x = 0;
17652
17653 /* Display the rest of the lines at the window end. */
17654 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17655 while (it.current_y < it.last_visible_y
17656 && !fonts_changed_p)
17657 {
17658 /* Is it always sure that the display agrees with lines in
17659 the current matrix? I don't think so, so we mark rows
17660 displayed invalid in the current matrix by setting their
17661 enabled_p flag to zero. */
17662 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17663 if (display_line (&it))
17664 last_text_row_at_end = it.glyph_row - 1;
17665 }
17666 }
17667
17668 /* Update window_end_pos and window_end_vpos. */
17669 if (first_unchanged_at_end_row
17670 && !last_text_row_at_end)
17671 {
17672 /* Window end line if one of the preserved rows from the current
17673 matrix. Set row to the last row displaying text in current
17674 matrix starting at first_unchanged_at_end_row, after
17675 scrolling. */
17676 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17677 row = find_last_row_displaying_text (w->current_matrix, &it,
17678 first_unchanged_at_end_row);
17679 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17680
17681 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17682 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17683 wset_window_end_vpos
17684 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17685 eassert (w->window_end_bytepos >= 0);
17686 IF_DEBUG (debug_method_add (w, "A"));
17687 }
17688 else if (last_text_row_at_end)
17689 {
17690 wset_window_end_pos
17691 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17692 w->window_end_bytepos
17693 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17694 wset_window_end_vpos
17695 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17696 desired_matrix)));
17697 eassert (w->window_end_bytepos >= 0);
17698 IF_DEBUG (debug_method_add (w, "B"));
17699 }
17700 else if (last_text_row)
17701 {
17702 /* We have displayed either to the end of the window or at the
17703 end of the window, i.e. the last row with text is to be found
17704 in the desired matrix. */
17705 wset_window_end_pos
17706 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17707 w->window_end_bytepos
17708 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17709 wset_window_end_vpos
17710 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17711 eassert (w->window_end_bytepos >= 0);
17712 }
17713 else if (first_unchanged_at_end_row == NULL
17714 && last_text_row == NULL
17715 && last_text_row_at_end == NULL)
17716 {
17717 /* Displayed to end of window, but no line containing text was
17718 displayed. Lines were deleted at the end of the window. */
17719 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17720 int vpos = XFASTINT (w->window_end_vpos);
17721 struct glyph_row *current_row = current_matrix->rows + vpos;
17722 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17723
17724 for (row = NULL;
17725 row == NULL && vpos >= first_vpos;
17726 --vpos, --current_row, --desired_row)
17727 {
17728 if (desired_row->enabled_p)
17729 {
17730 if (MATRIX_ROW_DISPLAYS_TEXT_P (desired_row))
17731 row = desired_row;
17732 }
17733 else if (MATRIX_ROW_DISPLAYS_TEXT_P (current_row))
17734 row = current_row;
17735 }
17736
17737 eassert (row != NULL);
17738 wset_window_end_vpos (w, make_number (vpos + 1));
17739 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17740 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17741 eassert (w->window_end_bytepos >= 0);
17742 IF_DEBUG (debug_method_add (w, "C"));
17743 }
17744 else
17745 emacs_abort ();
17746
17747 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17748 debug_end_vpos = XFASTINT (w->window_end_vpos));
17749
17750 /* Record that display has not been completed. */
17751 w->window_end_valid = 0;
17752 w->desired_matrix->no_scrolling_p = 1;
17753 return 3;
17754
17755 #undef GIVE_UP
17756 }
17757
17758
17759 \f
17760 /***********************************************************************
17761 More debugging support
17762 ***********************************************************************/
17763
17764 #ifdef GLYPH_DEBUG
17765
17766 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17767 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17768 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17769
17770
17771 /* Dump the contents of glyph matrix MATRIX on stderr.
17772
17773 GLYPHS 0 means don't show glyph contents.
17774 GLYPHS 1 means show glyphs in short form
17775 GLYPHS > 1 means show glyphs in long form. */
17776
17777 void
17778 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17779 {
17780 int i;
17781 for (i = 0; i < matrix->nrows; ++i)
17782 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17783 }
17784
17785
17786 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17787 the glyph row and area where the glyph comes from. */
17788
17789 void
17790 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17791 {
17792 if (glyph->type == CHAR_GLYPH
17793 || glyph->type == GLYPHLESS_GLYPH)
17794 {
17795 fprintf (stderr,
17796 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17797 glyph - row->glyphs[TEXT_AREA],
17798 (glyph->type == CHAR_GLYPH
17799 ? 'C'
17800 : 'G'),
17801 glyph->charpos,
17802 (BUFFERP (glyph->object)
17803 ? 'B'
17804 : (STRINGP (glyph->object)
17805 ? 'S'
17806 : (INTEGERP (glyph->object)
17807 ? '0'
17808 : '-'))),
17809 glyph->pixel_width,
17810 glyph->u.ch,
17811 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17812 ? glyph->u.ch
17813 : '.'),
17814 glyph->face_id,
17815 glyph->left_box_line_p,
17816 glyph->right_box_line_p);
17817 }
17818 else if (glyph->type == STRETCH_GLYPH)
17819 {
17820 fprintf (stderr,
17821 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17822 glyph - row->glyphs[TEXT_AREA],
17823 'S',
17824 glyph->charpos,
17825 (BUFFERP (glyph->object)
17826 ? 'B'
17827 : (STRINGP (glyph->object)
17828 ? 'S'
17829 : (INTEGERP (glyph->object)
17830 ? '0'
17831 : '-'))),
17832 glyph->pixel_width,
17833 0,
17834 ' ',
17835 glyph->face_id,
17836 glyph->left_box_line_p,
17837 glyph->right_box_line_p);
17838 }
17839 else if (glyph->type == IMAGE_GLYPH)
17840 {
17841 fprintf (stderr,
17842 " %5"pD"d %c %9"pI"d %c %3d 0x%06x %c %4d %1.1d%1.1d\n",
17843 glyph - row->glyphs[TEXT_AREA],
17844 'I',
17845 glyph->charpos,
17846 (BUFFERP (glyph->object)
17847 ? 'B'
17848 : (STRINGP (glyph->object)
17849 ? 'S'
17850 : (INTEGERP (glyph->object)
17851 ? '0'
17852 : '-'))),
17853 glyph->pixel_width,
17854 glyph->u.img_id,
17855 '.',
17856 glyph->face_id,
17857 glyph->left_box_line_p,
17858 glyph->right_box_line_p);
17859 }
17860 else if (glyph->type == COMPOSITE_GLYPH)
17861 {
17862 fprintf (stderr,
17863 " %5"pD"d %c %9"pI"d %c %3d 0x%06x",
17864 glyph - row->glyphs[TEXT_AREA],
17865 '+',
17866 glyph->charpos,
17867 (BUFFERP (glyph->object)
17868 ? 'B'
17869 : (STRINGP (glyph->object)
17870 ? 'S'
17871 : (INTEGERP (glyph->object)
17872 ? '0'
17873 : '-'))),
17874 glyph->pixel_width,
17875 glyph->u.cmp.id);
17876 if (glyph->u.cmp.automatic)
17877 fprintf (stderr,
17878 "[%d-%d]",
17879 glyph->slice.cmp.from, glyph->slice.cmp.to);
17880 fprintf (stderr, " . %4d %1.1d%1.1d\n",
17881 glyph->face_id,
17882 glyph->left_box_line_p,
17883 glyph->right_box_line_p);
17884 }
17885 }
17886
17887
17888 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
17889 GLYPHS 0 means don't show glyph contents.
17890 GLYPHS 1 means show glyphs in short form
17891 GLYPHS > 1 means show glyphs in long form. */
17892
17893 void
17894 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
17895 {
17896 if (glyphs != 1)
17897 {
17898 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
17899 fprintf (stderr, "==============================================================================\n");
17900
17901 fprintf (stderr, "%3d %9"pI"d %9"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
17902 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
17903 vpos,
17904 MATRIX_ROW_START_CHARPOS (row),
17905 MATRIX_ROW_END_CHARPOS (row),
17906 row->used[TEXT_AREA],
17907 row->contains_overlapping_glyphs_p,
17908 row->enabled_p,
17909 row->truncated_on_left_p,
17910 row->truncated_on_right_p,
17911 row->continued_p,
17912 MATRIX_ROW_CONTINUATION_LINE_P (row),
17913 MATRIX_ROW_DISPLAYS_TEXT_P (row),
17914 row->ends_at_zv_p,
17915 row->fill_line_p,
17916 row->ends_in_middle_of_char_p,
17917 row->starts_in_middle_of_char_p,
17918 row->mouse_face_p,
17919 row->x,
17920 row->y,
17921 row->pixel_width,
17922 row->height,
17923 row->visible_height,
17924 row->ascent,
17925 row->phys_ascent);
17926 /* The next 3 lines should align to "Start" in the header. */
17927 fprintf (stderr, " %9"pD"d %9"pD"d\t%5d\n", row->start.overlay_string_index,
17928 row->end.overlay_string_index,
17929 row->continuation_lines_width);
17930 fprintf (stderr, " %9"pI"d %9"pI"d\n",
17931 CHARPOS (row->start.string_pos),
17932 CHARPOS (row->end.string_pos));
17933 fprintf (stderr, " %9d %9d\n", row->start.dpvec_index,
17934 row->end.dpvec_index);
17935 }
17936
17937 if (glyphs > 1)
17938 {
17939 int area;
17940
17941 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17942 {
17943 struct glyph *glyph = row->glyphs[area];
17944 struct glyph *glyph_end = glyph + row->used[area];
17945
17946 /* Glyph for a line end in text. */
17947 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
17948 ++glyph_end;
17949
17950 if (glyph < glyph_end)
17951 fprintf (stderr, " Glyph# Type Pos O W Code C Face LR\n");
17952
17953 for (; glyph < glyph_end; ++glyph)
17954 dump_glyph (row, glyph, area);
17955 }
17956 }
17957 else if (glyphs == 1)
17958 {
17959 int area;
17960
17961 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
17962 {
17963 char *s = alloca (row->used[area] + 4);
17964 int i;
17965
17966 for (i = 0; i < row->used[area]; ++i)
17967 {
17968 struct glyph *glyph = row->glyphs[area] + i;
17969 if (i == row->used[area] - 1
17970 && area == TEXT_AREA
17971 && INTEGERP (glyph->object)
17972 && glyph->type == CHAR_GLYPH
17973 && glyph->u.ch == ' ')
17974 {
17975 strcpy (&s[i], "[\\n]");
17976 i += 4;
17977 }
17978 else if (glyph->type == CHAR_GLYPH
17979 && glyph->u.ch < 0x80
17980 && glyph->u.ch >= ' ')
17981 s[i] = glyph->u.ch;
17982 else
17983 s[i] = '.';
17984 }
17985
17986 s[i] = '\0';
17987 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
17988 }
17989 }
17990 }
17991
17992
17993 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
17994 Sdump_glyph_matrix, 0, 1, "p",
17995 doc: /* Dump the current matrix of the selected window to stderr.
17996 Shows contents of glyph row structures. With non-nil
17997 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
17998 glyphs in short form, otherwise show glyphs in long form. */)
17999 (Lisp_Object glyphs)
18000 {
18001 struct window *w = XWINDOW (selected_window);
18002 struct buffer *buffer = XBUFFER (w->contents);
18003
18004 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18005 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18006 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18007 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18008 fprintf (stderr, "=============================================\n");
18009 dump_glyph_matrix (w->current_matrix,
18010 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18011 return Qnil;
18012 }
18013
18014
18015 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18016 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18017 (void)
18018 {
18019 struct frame *f = XFRAME (selected_frame);
18020 dump_glyph_matrix (f->current_matrix, 1);
18021 return Qnil;
18022 }
18023
18024
18025 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18026 doc: /* Dump glyph row ROW to stderr.
18027 GLYPH 0 means don't dump glyphs.
18028 GLYPH 1 means dump glyphs in short form.
18029 GLYPH > 1 or omitted means dump glyphs in long form. */)
18030 (Lisp_Object row, Lisp_Object glyphs)
18031 {
18032 struct glyph_matrix *matrix;
18033 EMACS_INT vpos;
18034
18035 CHECK_NUMBER (row);
18036 matrix = XWINDOW (selected_window)->current_matrix;
18037 vpos = XINT (row);
18038 if (vpos >= 0 && vpos < matrix->nrows)
18039 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18040 vpos,
18041 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18042 return Qnil;
18043 }
18044
18045
18046 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18047 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18048 GLYPH 0 means don't dump glyphs.
18049 GLYPH 1 means dump glyphs in short form.
18050 GLYPH > 1 or omitted means dump glyphs in long form. */)
18051 (Lisp_Object row, Lisp_Object glyphs)
18052 {
18053 struct frame *sf = SELECTED_FRAME ();
18054 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18055 EMACS_INT vpos;
18056
18057 CHECK_NUMBER (row);
18058 vpos = XINT (row);
18059 if (vpos >= 0 && vpos < m->nrows)
18060 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18061 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18062 return Qnil;
18063 }
18064
18065
18066 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18067 doc: /* Toggle tracing of redisplay.
18068 With ARG, turn tracing on if and only if ARG is positive. */)
18069 (Lisp_Object arg)
18070 {
18071 if (NILP (arg))
18072 trace_redisplay_p = !trace_redisplay_p;
18073 else
18074 {
18075 arg = Fprefix_numeric_value (arg);
18076 trace_redisplay_p = XINT (arg) > 0;
18077 }
18078
18079 return Qnil;
18080 }
18081
18082
18083 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18084 doc: /* Like `format', but print result to stderr.
18085 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18086 (ptrdiff_t nargs, Lisp_Object *args)
18087 {
18088 Lisp_Object s = Fformat (nargs, args);
18089 fprintf (stderr, "%s", SDATA (s));
18090 return Qnil;
18091 }
18092
18093 #endif /* GLYPH_DEBUG */
18094
18095
18096 \f
18097 /***********************************************************************
18098 Building Desired Matrix Rows
18099 ***********************************************************************/
18100
18101 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18102 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18103
18104 static struct glyph_row *
18105 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18106 {
18107 struct frame *f = XFRAME (WINDOW_FRAME (w));
18108 struct buffer *buffer = XBUFFER (w->contents);
18109 struct buffer *old = current_buffer;
18110 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18111 int arrow_len = SCHARS (overlay_arrow_string);
18112 const unsigned char *arrow_end = arrow_string + arrow_len;
18113 const unsigned char *p;
18114 struct it it;
18115 bool multibyte_p;
18116 int n_glyphs_before;
18117
18118 set_buffer_temp (buffer);
18119 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18120 it.glyph_row->used[TEXT_AREA] = 0;
18121 SET_TEXT_POS (it.position, 0, 0);
18122
18123 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18124 p = arrow_string;
18125 while (p < arrow_end)
18126 {
18127 Lisp_Object face, ilisp;
18128
18129 /* Get the next character. */
18130 if (multibyte_p)
18131 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18132 else
18133 {
18134 it.c = it.char_to_display = *p, it.len = 1;
18135 if (! ASCII_CHAR_P (it.c))
18136 it.char_to_display = BYTE8_TO_CHAR (it.c);
18137 }
18138 p += it.len;
18139
18140 /* Get its face. */
18141 ilisp = make_number (p - arrow_string);
18142 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18143 it.face_id = compute_char_face (f, it.char_to_display, face);
18144
18145 /* Compute its width, get its glyphs. */
18146 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18147 SET_TEXT_POS (it.position, -1, -1);
18148 PRODUCE_GLYPHS (&it);
18149
18150 /* If this character doesn't fit any more in the line, we have
18151 to remove some glyphs. */
18152 if (it.current_x > it.last_visible_x)
18153 {
18154 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18155 break;
18156 }
18157 }
18158
18159 set_buffer_temp (old);
18160 return it.glyph_row;
18161 }
18162
18163
18164 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18165 glyphs to insert is determined by produce_special_glyphs. */
18166
18167 static void
18168 insert_left_trunc_glyphs (struct it *it)
18169 {
18170 struct it truncate_it;
18171 struct glyph *from, *end, *to, *toend;
18172
18173 eassert (!FRAME_WINDOW_P (it->f)
18174 || (!it->glyph_row->reversed_p
18175 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18176 || (it->glyph_row->reversed_p
18177 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18178
18179 /* Get the truncation glyphs. */
18180 truncate_it = *it;
18181 truncate_it.current_x = 0;
18182 truncate_it.face_id = DEFAULT_FACE_ID;
18183 truncate_it.glyph_row = &scratch_glyph_row;
18184 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18185 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18186 truncate_it.object = make_number (0);
18187 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18188
18189 /* Overwrite glyphs from IT with truncation glyphs. */
18190 if (!it->glyph_row->reversed_p)
18191 {
18192 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18193
18194 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18195 end = from + tused;
18196 to = it->glyph_row->glyphs[TEXT_AREA];
18197 toend = to + it->glyph_row->used[TEXT_AREA];
18198 if (FRAME_WINDOW_P (it->f))
18199 {
18200 /* On GUI frames, when variable-size fonts are displayed,
18201 the truncation glyphs may need more pixels than the row's
18202 glyphs they overwrite. We overwrite more glyphs to free
18203 enough screen real estate, and enlarge the stretch glyph
18204 on the right (see display_line), if there is one, to
18205 preserve the screen position of the truncation glyphs on
18206 the right. */
18207 int w = 0;
18208 struct glyph *g = to;
18209 short used;
18210
18211 /* The first glyph could be partially visible, in which case
18212 it->glyph_row->x will be negative. But we want the left
18213 truncation glyphs to be aligned at the left margin of the
18214 window, so we override the x coordinate at which the row
18215 will begin. */
18216 it->glyph_row->x = 0;
18217 while (g < toend && w < it->truncation_pixel_width)
18218 {
18219 w += g->pixel_width;
18220 ++g;
18221 }
18222 if (g - to - tused > 0)
18223 {
18224 memmove (to + tused, g, (toend - g) * sizeof(*g));
18225 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18226 }
18227 used = it->glyph_row->used[TEXT_AREA];
18228 if (it->glyph_row->truncated_on_right_p
18229 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18230 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18231 == STRETCH_GLYPH)
18232 {
18233 int extra = w - it->truncation_pixel_width;
18234
18235 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18236 }
18237 }
18238
18239 while (from < end)
18240 *to++ = *from++;
18241
18242 /* There may be padding glyphs left over. Overwrite them too. */
18243 if (!FRAME_WINDOW_P (it->f))
18244 {
18245 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18246 {
18247 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18248 while (from < end)
18249 *to++ = *from++;
18250 }
18251 }
18252
18253 if (to > toend)
18254 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18255 }
18256 else
18257 {
18258 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18259
18260 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18261 that back to front. */
18262 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18263 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18264 toend = it->glyph_row->glyphs[TEXT_AREA];
18265 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18266 if (FRAME_WINDOW_P (it->f))
18267 {
18268 int w = 0;
18269 struct glyph *g = to;
18270
18271 while (g >= toend && w < it->truncation_pixel_width)
18272 {
18273 w += g->pixel_width;
18274 --g;
18275 }
18276 if (to - g - tused > 0)
18277 to = g + tused;
18278 if (it->glyph_row->truncated_on_right_p
18279 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18280 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18281 {
18282 int extra = w - it->truncation_pixel_width;
18283
18284 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18285 }
18286 }
18287
18288 while (from >= end && to >= toend)
18289 *to-- = *from--;
18290 if (!FRAME_WINDOW_P (it->f))
18291 {
18292 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18293 {
18294 from =
18295 truncate_it.glyph_row->glyphs[TEXT_AREA]
18296 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18297 while (from >= end && to >= toend)
18298 *to-- = *from--;
18299 }
18300 }
18301 if (from >= end)
18302 {
18303 /* Need to free some room before prepending additional
18304 glyphs. */
18305 int move_by = from - end + 1;
18306 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18307 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18308
18309 for ( ; g >= g0; g--)
18310 g[move_by] = *g;
18311 while (from >= end)
18312 *to-- = *from--;
18313 it->glyph_row->used[TEXT_AREA] += move_by;
18314 }
18315 }
18316 }
18317
18318 /* Compute the hash code for ROW. */
18319 unsigned
18320 row_hash (struct glyph_row *row)
18321 {
18322 int area, k;
18323 unsigned hashval = 0;
18324
18325 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18326 for (k = 0; k < row->used[area]; ++k)
18327 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18328 + row->glyphs[area][k].u.val
18329 + row->glyphs[area][k].face_id
18330 + row->glyphs[area][k].padding_p
18331 + (row->glyphs[area][k].type << 2));
18332
18333 return hashval;
18334 }
18335
18336 /* Compute the pixel height and width of IT->glyph_row.
18337
18338 Most of the time, ascent and height of a display line will be equal
18339 to the max_ascent and max_height values of the display iterator
18340 structure. This is not the case if
18341
18342 1. We hit ZV without displaying anything. In this case, max_ascent
18343 and max_height will be zero.
18344
18345 2. We have some glyphs that don't contribute to the line height.
18346 (The glyph row flag contributes_to_line_height_p is for future
18347 pixmap extensions).
18348
18349 The first case is easily covered by using default values because in
18350 these cases, the line height does not really matter, except that it
18351 must not be zero. */
18352
18353 static void
18354 compute_line_metrics (struct it *it)
18355 {
18356 struct glyph_row *row = it->glyph_row;
18357
18358 if (FRAME_WINDOW_P (it->f))
18359 {
18360 int i, min_y, max_y;
18361
18362 /* The line may consist of one space only, that was added to
18363 place the cursor on it. If so, the row's height hasn't been
18364 computed yet. */
18365 if (row->height == 0)
18366 {
18367 if (it->max_ascent + it->max_descent == 0)
18368 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18369 row->ascent = it->max_ascent;
18370 row->height = it->max_ascent + it->max_descent;
18371 row->phys_ascent = it->max_phys_ascent;
18372 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18373 row->extra_line_spacing = it->max_extra_line_spacing;
18374 }
18375
18376 /* Compute the width of this line. */
18377 row->pixel_width = row->x;
18378 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18379 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18380
18381 eassert (row->pixel_width >= 0);
18382 eassert (row->ascent >= 0 && row->height > 0);
18383
18384 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18385 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18386
18387 /* If first line's physical ascent is larger than its logical
18388 ascent, use the physical ascent, and make the row taller.
18389 This makes accented characters fully visible. */
18390 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18391 && row->phys_ascent > row->ascent)
18392 {
18393 row->height += row->phys_ascent - row->ascent;
18394 row->ascent = row->phys_ascent;
18395 }
18396
18397 /* Compute how much of the line is visible. */
18398 row->visible_height = row->height;
18399
18400 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18401 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18402
18403 if (row->y < min_y)
18404 row->visible_height -= min_y - row->y;
18405 if (row->y + row->height > max_y)
18406 row->visible_height -= row->y + row->height - max_y;
18407 }
18408 else
18409 {
18410 row->pixel_width = row->used[TEXT_AREA];
18411 if (row->continued_p)
18412 row->pixel_width -= it->continuation_pixel_width;
18413 else if (row->truncated_on_right_p)
18414 row->pixel_width -= it->truncation_pixel_width;
18415 row->ascent = row->phys_ascent = 0;
18416 row->height = row->phys_height = row->visible_height = 1;
18417 row->extra_line_spacing = 0;
18418 }
18419
18420 /* Compute a hash code for this row. */
18421 row->hash = row_hash (row);
18422
18423 it->max_ascent = it->max_descent = 0;
18424 it->max_phys_ascent = it->max_phys_descent = 0;
18425 }
18426
18427
18428 /* Append one space to the glyph row of iterator IT if doing a
18429 window-based redisplay. The space has the same face as
18430 IT->face_id. Value is non-zero if a space was added.
18431
18432 This function is called to make sure that there is always one glyph
18433 at the end of a glyph row that the cursor can be set on under
18434 window-systems. (If there weren't such a glyph we would not know
18435 how wide and tall a box cursor should be displayed).
18436
18437 At the same time this space let's a nicely handle clearing to the
18438 end of the line if the row ends in italic text. */
18439
18440 static int
18441 append_space_for_newline (struct it *it, int default_face_p)
18442 {
18443 if (FRAME_WINDOW_P (it->f))
18444 {
18445 int n = it->glyph_row->used[TEXT_AREA];
18446
18447 if (it->glyph_row->glyphs[TEXT_AREA] + n
18448 < it->glyph_row->glyphs[1 + TEXT_AREA])
18449 {
18450 /* Save some values that must not be changed.
18451 Must save IT->c and IT->len because otherwise
18452 ITERATOR_AT_END_P wouldn't work anymore after
18453 append_space_for_newline has been called. */
18454 enum display_element_type saved_what = it->what;
18455 int saved_c = it->c, saved_len = it->len;
18456 int saved_char_to_display = it->char_to_display;
18457 int saved_x = it->current_x;
18458 int saved_face_id = it->face_id;
18459 int saved_box_end = it->end_of_box_run_p;
18460 struct text_pos saved_pos;
18461 Lisp_Object saved_object;
18462 struct face *face;
18463
18464 saved_object = it->object;
18465 saved_pos = it->position;
18466
18467 it->what = IT_CHARACTER;
18468 memset (&it->position, 0, sizeof it->position);
18469 it->object = make_number (0);
18470 it->c = it->char_to_display = ' ';
18471 it->len = 1;
18472
18473 /* If the default face was remapped, be sure to use the
18474 remapped face for the appended newline. */
18475 if (default_face_p)
18476 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18477 else if (it->face_before_selective_p)
18478 it->face_id = it->saved_face_id;
18479 face = FACE_FROM_ID (it->f, it->face_id);
18480 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18481 /* In R2L rows, we will prepend a stretch glyph that will
18482 have the end_of_box_run_p flag set for it, so there's no
18483 need for the appended newline glyph to have that flag
18484 set. */
18485 if (it->glyph_row->reversed_p
18486 /* But if the appended newline glyph goes all the way to
18487 the end of the row, there will be no stretch glyph,
18488 so leave the box flag set. */
18489 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18490 it->end_of_box_run_p = 0;
18491
18492 PRODUCE_GLYPHS (it);
18493
18494 it->override_ascent = -1;
18495 it->constrain_row_ascent_descent_p = 0;
18496 it->current_x = saved_x;
18497 it->object = saved_object;
18498 it->position = saved_pos;
18499 it->what = saved_what;
18500 it->face_id = saved_face_id;
18501 it->len = saved_len;
18502 it->c = saved_c;
18503 it->char_to_display = saved_char_to_display;
18504 it->end_of_box_run_p = saved_box_end;
18505 return 1;
18506 }
18507 }
18508
18509 return 0;
18510 }
18511
18512
18513 /* Extend the face of the last glyph in the text area of IT->glyph_row
18514 to the end of the display line. Called from display_line. If the
18515 glyph row is empty, add a space glyph to it so that we know the
18516 face to draw. Set the glyph row flag fill_line_p. If the glyph
18517 row is R2L, prepend a stretch glyph to cover the empty space to the
18518 left of the leftmost glyph. */
18519
18520 static void
18521 extend_face_to_end_of_line (struct it *it)
18522 {
18523 struct face *face, *default_face;
18524 struct frame *f = it->f;
18525
18526 /* If line is already filled, do nothing. Non window-system frames
18527 get a grace of one more ``pixel'' because their characters are
18528 1-``pixel'' wide, so they hit the equality too early. This grace
18529 is needed only for R2L rows that are not continued, to produce
18530 one extra blank where we could display the cursor. */
18531 if (it->current_x >= it->last_visible_x
18532 + (!FRAME_WINDOW_P (f)
18533 && it->glyph_row->reversed_p
18534 && !it->glyph_row->continued_p))
18535 return;
18536
18537 /* The default face, possibly remapped. */
18538 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18539
18540 /* Face extension extends the background and box of IT->face_id
18541 to the end of the line. If the background equals the background
18542 of the frame, we don't have to do anything. */
18543 if (it->face_before_selective_p)
18544 face = FACE_FROM_ID (f, it->saved_face_id);
18545 else
18546 face = FACE_FROM_ID (f, it->face_id);
18547
18548 if (FRAME_WINDOW_P (f)
18549 && MATRIX_ROW_DISPLAYS_TEXT_P (it->glyph_row)
18550 && face->box == FACE_NO_BOX
18551 && face->background == FRAME_BACKGROUND_PIXEL (f)
18552 && !face->stipple
18553 && !it->glyph_row->reversed_p)
18554 return;
18555
18556 /* Set the glyph row flag indicating that the face of the last glyph
18557 in the text area has to be drawn to the end of the text area. */
18558 it->glyph_row->fill_line_p = 1;
18559
18560 /* If current character of IT is not ASCII, make sure we have the
18561 ASCII face. This will be automatically undone the next time
18562 get_next_display_element returns a multibyte character. Note
18563 that the character will always be single byte in unibyte
18564 text. */
18565 if (!ASCII_CHAR_P (it->c))
18566 {
18567 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18568 }
18569
18570 if (FRAME_WINDOW_P (f))
18571 {
18572 /* If the row is empty, add a space with the current face of IT,
18573 so that we know which face to draw. */
18574 if (it->glyph_row->used[TEXT_AREA] == 0)
18575 {
18576 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18577 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18578 it->glyph_row->used[TEXT_AREA] = 1;
18579 }
18580 #ifdef HAVE_WINDOW_SYSTEM
18581 if (it->glyph_row->reversed_p)
18582 {
18583 /* Prepend a stretch glyph to the row, such that the
18584 rightmost glyph will be drawn flushed all the way to the
18585 right margin of the window. The stretch glyph that will
18586 occupy the empty space, if any, to the left of the
18587 glyphs. */
18588 struct font *font = face->font ? face->font : FRAME_FONT (f);
18589 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18590 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18591 struct glyph *g;
18592 int row_width, stretch_ascent, stretch_width;
18593 struct text_pos saved_pos;
18594 int saved_face_id, saved_avoid_cursor, saved_box_start;
18595
18596 for (row_width = 0, g = row_start; g < row_end; g++)
18597 row_width += g->pixel_width;
18598 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18599 if (stretch_width > 0)
18600 {
18601 stretch_ascent =
18602 (((it->ascent + it->descent)
18603 * FONT_BASE (font)) / FONT_HEIGHT (font));
18604 saved_pos = it->position;
18605 memset (&it->position, 0, sizeof it->position);
18606 saved_avoid_cursor = it->avoid_cursor_p;
18607 it->avoid_cursor_p = 1;
18608 saved_face_id = it->face_id;
18609 saved_box_start = it->start_of_box_run_p;
18610 /* The last row's stretch glyph should get the default
18611 face, to avoid painting the rest of the window with
18612 the region face, if the region ends at ZV. */
18613 if (it->glyph_row->ends_at_zv_p)
18614 it->face_id = default_face->id;
18615 else
18616 it->face_id = face->id;
18617 it->start_of_box_run_p = 0;
18618 append_stretch_glyph (it, make_number (0), stretch_width,
18619 it->ascent + it->descent, stretch_ascent);
18620 it->position = saved_pos;
18621 it->avoid_cursor_p = saved_avoid_cursor;
18622 it->face_id = saved_face_id;
18623 it->start_of_box_run_p = saved_box_start;
18624 }
18625 }
18626 #endif /* HAVE_WINDOW_SYSTEM */
18627 }
18628 else
18629 {
18630 /* Save some values that must not be changed. */
18631 int saved_x = it->current_x;
18632 struct text_pos saved_pos;
18633 Lisp_Object saved_object;
18634 enum display_element_type saved_what = it->what;
18635 int saved_face_id = it->face_id;
18636
18637 saved_object = it->object;
18638 saved_pos = it->position;
18639
18640 it->what = IT_CHARACTER;
18641 memset (&it->position, 0, sizeof it->position);
18642 it->object = make_number (0);
18643 it->c = it->char_to_display = ' ';
18644 it->len = 1;
18645 /* The last row's blank glyphs should get the default face, to
18646 avoid painting the rest of the window with the region face,
18647 if the region ends at ZV. */
18648 if (it->glyph_row->ends_at_zv_p)
18649 it->face_id = default_face->id;
18650 else
18651 it->face_id = face->id;
18652
18653 PRODUCE_GLYPHS (it);
18654
18655 while (it->current_x <= it->last_visible_x)
18656 PRODUCE_GLYPHS (it);
18657
18658 /* Don't count these blanks really. It would let us insert a left
18659 truncation glyph below and make us set the cursor on them, maybe. */
18660 it->current_x = saved_x;
18661 it->object = saved_object;
18662 it->position = saved_pos;
18663 it->what = saved_what;
18664 it->face_id = saved_face_id;
18665 }
18666 }
18667
18668
18669 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18670 trailing whitespace. */
18671
18672 static int
18673 trailing_whitespace_p (ptrdiff_t charpos)
18674 {
18675 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18676 int c = 0;
18677
18678 while (bytepos < ZV_BYTE
18679 && (c = FETCH_CHAR (bytepos),
18680 c == ' ' || c == '\t'))
18681 ++bytepos;
18682
18683 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18684 {
18685 if (bytepos != PT_BYTE)
18686 return 1;
18687 }
18688 return 0;
18689 }
18690
18691
18692 /* Highlight trailing whitespace, if any, in ROW. */
18693
18694 static void
18695 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18696 {
18697 int used = row->used[TEXT_AREA];
18698
18699 if (used)
18700 {
18701 struct glyph *start = row->glyphs[TEXT_AREA];
18702 struct glyph *glyph = start + used - 1;
18703
18704 if (row->reversed_p)
18705 {
18706 /* Right-to-left rows need to be processed in the opposite
18707 direction, so swap the edge pointers. */
18708 glyph = start;
18709 start = row->glyphs[TEXT_AREA] + used - 1;
18710 }
18711
18712 /* Skip over glyphs inserted to display the cursor at the
18713 end of a line, for extending the face of the last glyph
18714 to the end of the line on terminals, and for truncation
18715 and continuation glyphs. */
18716 if (!row->reversed_p)
18717 {
18718 while (glyph >= start
18719 && glyph->type == CHAR_GLYPH
18720 && INTEGERP (glyph->object))
18721 --glyph;
18722 }
18723 else
18724 {
18725 while (glyph <= start
18726 && glyph->type == CHAR_GLYPH
18727 && INTEGERP (glyph->object))
18728 ++glyph;
18729 }
18730
18731 /* If last glyph is a space or stretch, and it's trailing
18732 whitespace, set the face of all trailing whitespace glyphs in
18733 IT->glyph_row to `trailing-whitespace'. */
18734 if ((row->reversed_p ? glyph <= start : glyph >= start)
18735 && BUFFERP (glyph->object)
18736 && (glyph->type == STRETCH_GLYPH
18737 || (glyph->type == CHAR_GLYPH
18738 && glyph->u.ch == ' '))
18739 && trailing_whitespace_p (glyph->charpos))
18740 {
18741 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18742 if (face_id < 0)
18743 return;
18744
18745 if (!row->reversed_p)
18746 {
18747 while (glyph >= start
18748 && BUFFERP (glyph->object)
18749 && (glyph->type == STRETCH_GLYPH
18750 || (glyph->type == CHAR_GLYPH
18751 && glyph->u.ch == ' ')))
18752 (glyph--)->face_id = face_id;
18753 }
18754 else
18755 {
18756 while (glyph <= start
18757 && BUFFERP (glyph->object)
18758 && (glyph->type == STRETCH_GLYPH
18759 || (glyph->type == CHAR_GLYPH
18760 && glyph->u.ch == ' ')))
18761 (glyph++)->face_id = face_id;
18762 }
18763 }
18764 }
18765 }
18766
18767
18768 /* Value is non-zero if glyph row ROW should be
18769 considered to hold the buffer position CHARPOS. */
18770
18771 static int
18772 row_for_charpos_p (struct glyph_row *row, ptrdiff_t charpos)
18773 {
18774 int result = 1;
18775
18776 if (charpos == CHARPOS (row->end.pos)
18777 || charpos == MATRIX_ROW_END_CHARPOS (row))
18778 {
18779 /* Suppose the row ends on a string.
18780 Unless the row is continued, that means it ends on a newline
18781 in the string. If it's anything other than a display string
18782 (e.g., a before-string from an overlay), we don't want the
18783 cursor there. (This heuristic seems to give the optimal
18784 behavior for the various types of multi-line strings.)
18785 One exception: if the string has `cursor' property on one of
18786 its characters, we _do_ want the cursor there. */
18787 if (CHARPOS (row->end.string_pos) >= 0)
18788 {
18789 if (row->continued_p)
18790 result = 1;
18791 else
18792 {
18793 /* Check for `display' property. */
18794 struct glyph *beg = row->glyphs[TEXT_AREA];
18795 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18796 struct glyph *glyph;
18797
18798 result = 0;
18799 for (glyph = end; glyph >= beg; --glyph)
18800 if (STRINGP (glyph->object))
18801 {
18802 Lisp_Object prop
18803 = Fget_char_property (make_number (charpos),
18804 Qdisplay, Qnil);
18805 result =
18806 (!NILP (prop)
18807 && display_prop_string_p (prop, glyph->object));
18808 /* If there's a `cursor' property on one of the
18809 string's characters, this row is a cursor row,
18810 even though this is not a display string. */
18811 if (!result)
18812 {
18813 Lisp_Object s = glyph->object;
18814
18815 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18816 {
18817 ptrdiff_t gpos = glyph->charpos;
18818
18819 if (!NILP (Fget_char_property (make_number (gpos),
18820 Qcursor, s)))
18821 {
18822 result = 1;
18823 break;
18824 }
18825 }
18826 }
18827 break;
18828 }
18829 }
18830 }
18831 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18832 {
18833 /* If the row ends in middle of a real character,
18834 and the line is continued, we want the cursor here.
18835 That's because CHARPOS (ROW->end.pos) would equal
18836 PT if PT is before the character. */
18837 if (!row->ends_in_ellipsis_p)
18838 result = row->continued_p;
18839 else
18840 /* If the row ends in an ellipsis, then
18841 CHARPOS (ROW->end.pos) will equal point after the
18842 invisible text. We want that position to be displayed
18843 after the ellipsis. */
18844 result = 0;
18845 }
18846 /* If the row ends at ZV, display the cursor at the end of that
18847 row instead of at the start of the row below. */
18848 else if (row->ends_at_zv_p)
18849 result = 1;
18850 else
18851 result = 0;
18852 }
18853
18854 return result;
18855 }
18856
18857 /* Value is non-zero if glyph row ROW should be
18858 used to hold the cursor. */
18859
18860 static int
18861 cursor_row_p (struct glyph_row *row)
18862 {
18863 return row_for_charpos_p (row, PT);
18864 }
18865
18866 \f
18867
18868 /* Push the property PROP so that it will be rendered at the current
18869 position in IT. Return 1 if PROP was successfully pushed, 0
18870 otherwise. Called from handle_line_prefix to handle the
18871 `line-prefix' and `wrap-prefix' properties. */
18872
18873 static int
18874 push_prefix_prop (struct it *it, Lisp_Object prop)
18875 {
18876 struct text_pos pos =
18877 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
18878
18879 eassert (it->method == GET_FROM_BUFFER
18880 || it->method == GET_FROM_DISPLAY_VECTOR
18881 || it->method == GET_FROM_STRING);
18882
18883 /* We need to save the current buffer/string position, so it will be
18884 restored by pop_it, because iterate_out_of_display_property
18885 depends on that being set correctly, but some situations leave
18886 it->position not yet set when this function is called. */
18887 push_it (it, &pos);
18888
18889 if (STRINGP (prop))
18890 {
18891 if (SCHARS (prop) == 0)
18892 {
18893 pop_it (it);
18894 return 0;
18895 }
18896
18897 it->string = prop;
18898 it->string_from_prefix_prop_p = 1;
18899 it->multibyte_p = STRING_MULTIBYTE (it->string);
18900 it->current.overlay_string_index = -1;
18901 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
18902 it->end_charpos = it->string_nchars = SCHARS (it->string);
18903 it->method = GET_FROM_STRING;
18904 it->stop_charpos = 0;
18905 it->prev_stop = 0;
18906 it->base_level_stop = 0;
18907
18908 /* Force paragraph direction to be that of the parent
18909 buffer/string. */
18910 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
18911 it->paragraph_embedding = it->bidi_it.paragraph_dir;
18912 else
18913 it->paragraph_embedding = L2R;
18914
18915 /* Set up the bidi iterator for this display string. */
18916 if (it->bidi_p)
18917 {
18918 it->bidi_it.string.lstring = it->string;
18919 it->bidi_it.string.s = NULL;
18920 it->bidi_it.string.schars = it->end_charpos;
18921 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
18922 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
18923 it->bidi_it.string.unibyte = !it->multibyte_p;
18924 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
18925 }
18926 }
18927 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
18928 {
18929 it->method = GET_FROM_STRETCH;
18930 it->object = prop;
18931 }
18932 #ifdef HAVE_WINDOW_SYSTEM
18933 else if (IMAGEP (prop))
18934 {
18935 it->what = IT_IMAGE;
18936 it->image_id = lookup_image (it->f, prop);
18937 it->method = GET_FROM_IMAGE;
18938 }
18939 #endif /* HAVE_WINDOW_SYSTEM */
18940 else
18941 {
18942 pop_it (it); /* bogus display property, give up */
18943 return 0;
18944 }
18945
18946 return 1;
18947 }
18948
18949 /* Return the character-property PROP at the current position in IT. */
18950
18951 static Lisp_Object
18952 get_it_property (struct it *it, Lisp_Object prop)
18953 {
18954 Lisp_Object position;
18955
18956 if (STRINGP (it->object))
18957 position = make_number (IT_STRING_CHARPOS (*it));
18958 else if (BUFFERP (it->object))
18959 position = make_number (IT_CHARPOS (*it));
18960 else
18961 return Qnil;
18962
18963 return Fget_char_property (position, prop, it->object);
18964 }
18965
18966 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
18967
18968 static void
18969 handle_line_prefix (struct it *it)
18970 {
18971 Lisp_Object prefix;
18972
18973 if (it->continuation_lines_width > 0)
18974 {
18975 prefix = get_it_property (it, Qwrap_prefix);
18976 if (NILP (prefix))
18977 prefix = Vwrap_prefix;
18978 }
18979 else
18980 {
18981 prefix = get_it_property (it, Qline_prefix);
18982 if (NILP (prefix))
18983 prefix = Vline_prefix;
18984 }
18985 if (! NILP (prefix) && push_prefix_prop (it, prefix))
18986 {
18987 /* If the prefix is wider than the window, and we try to wrap
18988 it, it would acquire its own wrap prefix, and so on till the
18989 iterator stack overflows. So, don't wrap the prefix. */
18990 it->line_wrap = TRUNCATE;
18991 it->avoid_cursor_p = 1;
18992 }
18993 }
18994
18995 \f
18996
18997 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
18998 only for R2L lines from display_line and display_string, when they
18999 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19000 the line/string needs to be continued on the next glyph row. */
19001 static void
19002 unproduce_glyphs (struct it *it, int n)
19003 {
19004 struct glyph *glyph, *end;
19005
19006 eassert (it->glyph_row);
19007 eassert (it->glyph_row->reversed_p);
19008 eassert (it->area == TEXT_AREA);
19009 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19010
19011 if (n > it->glyph_row->used[TEXT_AREA])
19012 n = it->glyph_row->used[TEXT_AREA];
19013 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19014 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19015 for ( ; glyph < end; glyph++)
19016 glyph[-n] = *glyph;
19017 }
19018
19019 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19020 and ROW->maxpos. */
19021 static void
19022 find_row_edges (struct it *it, struct glyph_row *row,
19023 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19024 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19025 {
19026 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19027 lines' rows is implemented for bidi-reordered rows. */
19028
19029 /* ROW->minpos is the value of min_pos, the minimal buffer position
19030 we have in ROW, or ROW->start.pos if that is smaller. */
19031 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19032 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19033 else
19034 /* We didn't find buffer positions smaller than ROW->start, or
19035 didn't find _any_ valid buffer positions in any of the glyphs,
19036 so we must trust the iterator's computed positions. */
19037 row->minpos = row->start.pos;
19038 if (max_pos <= 0)
19039 {
19040 max_pos = CHARPOS (it->current.pos);
19041 max_bpos = BYTEPOS (it->current.pos);
19042 }
19043
19044 /* Here are the various use-cases for ending the row, and the
19045 corresponding values for ROW->maxpos:
19046
19047 Line ends in a newline from buffer eol_pos + 1
19048 Line is continued from buffer max_pos + 1
19049 Line is truncated on right it->current.pos
19050 Line ends in a newline from string max_pos + 1(*)
19051 (*) + 1 only when line ends in a forward scan
19052 Line is continued from string max_pos
19053 Line is continued from display vector max_pos
19054 Line is entirely from a string min_pos == max_pos
19055 Line is entirely from a display vector min_pos == max_pos
19056 Line that ends at ZV ZV
19057
19058 If you discover other use-cases, please add them here as
19059 appropriate. */
19060 if (row->ends_at_zv_p)
19061 row->maxpos = it->current.pos;
19062 else if (row->used[TEXT_AREA])
19063 {
19064 int seen_this_string = 0;
19065 struct glyph_row *r1 = row - 1;
19066
19067 /* Did we see the same display string on the previous row? */
19068 if (STRINGP (it->object)
19069 /* this is not the first row */
19070 && row > it->w->desired_matrix->rows
19071 /* previous row is not the header line */
19072 && !r1->mode_line_p
19073 /* previous row also ends in a newline from a string */
19074 && r1->ends_in_newline_from_string_p)
19075 {
19076 struct glyph *start, *end;
19077
19078 /* Search for the last glyph of the previous row that came
19079 from buffer or string. Depending on whether the row is
19080 L2R or R2L, we need to process it front to back or the
19081 other way round. */
19082 if (!r1->reversed_p)
19083 {
19084 start = r1->glyphs[TEXT_AREA];
19085 end = start + r1->used[TEXT_AREA];
19086 /* Glyphs inserted by redisplay have an integer (zero)
19087 as their object. */
19088 while (end > start
19089 && INTEGERP ((end - 1)->object)
19090 && (end - 1)->charpos <= 0)
19091 --end;
19092 if (end > start)
19093 {
19094 if (EQ ((end - 1)->object, it->object))
19095 seen_this_string = 1;
19096 }
19097 else
19098 /* If all the glyphs of the previous row were inserted
19099 by redisplay, it means the previous row was
19100 produced from a single newline, which is only
19101 possible if that newline came from the same string
19102 as the one which produced this ROW. */
19103 seen_this_string = 1;
19104 }
19105 else
19106 {
19107 end = r1->glyphs[TEXT_AREA] - 1;
19108 start = end + r1->used[TEXT_AREA];
19109 while (end < start
19110 && INTEGERP ((end + 1)->object)
19111 && (end + 1)->charpos <= 0)
19112 ++end;
19113 if (end < start)
19114 {
19115 if (EQ ((end + 1)->object, it->object))
19116 seen_this_string = 1;
19117 }
19118 else
19119 seen_this_string = 1;
19120 }
19121 }
19122 /* Take note of each display string that covers a newline only
19123 once, the first time we see it. This is for when a display
19124 string includes more than one newline in it. */
19125 if (row->ends_in_newline_from_string_p && !seen_this_string)
19126 {
19127 /* If we were scanning the buffer forward when we displayed
19128 the string, we want to account for at least one buffer
19129 position that belongs to this row (position covered by
19130 the display string), so that cursor positioning will
19131 consider this row as a candidate when point is at the end
19132 of the visual line represented by this row. This is not
19133 required when scanning back, because max_pos will already
19134 have a much larger value. */
19135 if (CHARPOS (row->end.pos) > max_pos)
19136 INC_BOTH (max_pos, max_bpos);
19137 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19138 }
19139 else if (CHARPOS (it->eol_pos) > 0)
19140 SET_TEXT_POS (row->maxpos,
19141 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19142 else if (row->continued_p)
19143 {
19144 /* If max_pos is different from IT's current position, it
19145 means IT->method does not belong to the display element
19146 at max_pos. However, it also means that the display
19147 element at max_pos was displayed in its entirety on this
19148 line, which is equivalent to saying that the next line
19149 starts at the next buffer position. */
19150 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19151 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19152 else
19153 {
19154 INC_BOTH (max_pos, max_bpos);
19155 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19156 }
19157 }
19158 else if (row->truncated_on_right_p)
19159 /* display_line already called reseat_at_next_visible_line_start,
19160 which puts the iterator at the beginning of the next line, in
19161 the logical order. */
19162 row->maxpos = it->current.pos;
19163 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19164 /* A line that is entirely from a string/image/stretch... */
19165 row->maxpos = row->minpos;
19166 else
19167 emacs_abort ();
19168 }
19169 else
19170 row->maxpos = it->current.pos;
19171 }
19172
19173 /* Construct the glyph row IT->glyph_row in the desired matrix of
19174 IT->w from text at the current position of IT. See dispextern.h
19175 for an overview of struct it. Value is non-zero if
19176 IT->glyph_row displays text, as opposed to a line displaying ZV
19177 only. */
19178
19179 static int
19180 display_line (struct it *it)
19181 {
19182 struct glyph_row *row = it->glyph_row;
19183 Lisp_Object overlay_arrow_string;
19184 struct it wrap_it;
19185 void *wrap_data = NULL;
19186 int may_wrap = 0, wrap_x IF_LINT (= 0);
19187 int wrap_row_used = -1;
19188 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19189 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19190 int wrap_row_extra_line_spacing IF_LINT (= 0);
19191 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19192 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19193 int cvpos;
19194 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19195 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19196
19197 /* We always start displaying at hpos zero even if hscrolled. */
19198 eassert (it->hpos == 0 && it->current_x == 0);
19199
19200 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19201 >= it->w->desired_matrix->nrows)
19202 {
19203 it->w->nrows_scale_factor++;
19204 fonts_changed_p = 1;
19205 return 0;
19206 }
19207
19208 /* Is IT->w showing the region? */
19209 it->w->region_showing = it->region_beg_charpos > 0 ? it->region_beg_charpos : 0;
19210
19211 /* Clear the result glyph row and enable it. */
19212 prepare_desired_row (row);
19213
19214 row->y = it->current_y;
19215 row->start = it->start;
19216 row->continuation_lines_width = it->continuation_lines_width;
19217 row->displays_text_p = 1;
19218 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19219 it->starts_in_middle_of_char_p = 0;
19220
19221 /* Arrange the overlays nicely for our purposes. Usually, we call
19222 display_line on only one line at a time, in which case this
19223 can't really hurt too much, or we call it on lines which appear
19224 one after another in the buffer, in which case all calls to
19225 recenter_overlay_lists but the first will be pretty cheap. */
19226 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19227
19228 /* Move over display elements that are not visible because we are
19229 hscrolled. This may stop at an x-position < IT->first_visible_x
19230 if the first glyph is partially visible or if we hit a line end. */
19231 if (it->current_x < it->first_visible_x)
19232 {
19233 enum move_it_result move_result;
19234
19235 this_line_min_pos = row->start.pos;
19236 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19237 MOVE_TO_POS | MOVE_TO_X);
19238 /* If we are under a large hscroll, move_it_in_display_line_to
19239 could hit the end of the line without reaching
19240 it->first_visible_x. Pretend that we did reach it. This is
19241 especially important on a TTY, where we will call
19242 extend_face_to_end_of_line, which needs to know how many
19243 blank glyphs to produce. */
19244 if (it->current_x < it->first_visible_x
19245 && (move_result == MOVE_NEWLINE_OR_CR
19246 || move_result == MOVE_POS_MATCH_OR_ZV))
19247 it->current_x = it->first_visible_x;
19248
19249 /* Record the smallest positions seen while we moved over
19250 display elements that are not visible. This is needed by
19251 redisplay_internal for optimizing the case where the cursor
19252 stays inside the same line. The rest of this function only
19253 considers positions that are actually displayed, so
19254 RECORD_MAX_MIN_POS will not otherwise record positions that
19255 are hscrolled to the left of the left edge of the window. */
19256 min_pos = CHARPOS (this_line_min_pos);
19257 min_bpos = BYTEPOS (this_line_min_pos);
19258 }
19259 else
19260 {
19261 /* We only do this when not calling `move_it_in_display_line_to'
19262 above, because move_it_in_display_line_to calls
19263 handle_line_prefix itself. */
19264 handle_line_prefix (it);
19265 }
19266
19267 /* Get the initial row height. This is either the height of the
19268 text hscrolled, if there is any, or zero. */
19269 row->ascent = it->max_ascent;
19270 row->height = it->max_ascent + it->max_descent;
19271 row->phys_ascent = it->max_phys_ascent;
19272 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19273 row->extra_line_spacing = it->max_extra_line_spacing;
19274
19275 /* Utility macro to record max and min buffer positions seen until now. */
19276 #define RECORD_MAX_MIN_POS(IT) \
19277 do \
19278 { \
19279 int composition_p = !STRINGP ((IT)->string) \
19280 && ((IT)->what == IT_COMPOSITION); \
19281 ptrdiff_t current_pos = \
19282 composition_p ? (IT)->cmp_it.charpos \
19283 : IT_CHARPOS (*(IT)); \
19284 ptrdiff_t current_bpos = \
19285 composition_p ? CHAR_TO_BYTE (current_pos) \
19286 : IT_BYTEPOS (*(IT)); \
19287 if (current_pos < min_pos) \
19288 { \
19289 min_pos = current_pos; \
19290 min_bpos = current_bpos; \
19291 } \
19292 if (IT_CHARPOS (*it) > max_pos) \
19293 { \
19294 max_pos = IT_CHARPOS (*it); \
19295 max_bpos = IT_BYTEPOS (*it); \
19296 } \
19297 } \
19298 while (0)
19299
19300 /* Loop generating characters. The loop is left with IT on the next
19301 character to display. */
19302 while (1)
19303 {
19304 int n_glyphs_before, hpos_before, x_before;
19305 int x, nglyphs;
19306 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19307
19308 /* Retrieve the next thing to display. Value is zero if end of
19309 buffer reached. */
19310 if (!get_next_display_element (it))
19311 {
19312 /* Maybe add a space at the end of this line that is used to
19313 display the cursor there under X. Set the charpos of the
19314 first glyph of blank lines not corresponding to any text
19315 to -1. */
19316 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19317 row->exact_window_width_line_p = 1;
19318 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19319 || row->used[TEXT_AREA] == 0)
19320 {
19321 row->glyphs[TEXT_AREA]->charpos = -1;
19322 row->displays_text_p = 0;
19323
19324 if (!NILP (BVAR (XBUFFER (it->w->contents), indicate_empty_lines))
19325 && (!MINI_WINDOW_P (it->w)
19326 || (minibuf_level && EQ (it->window, minibuf_window))))
19327 row->indicate_empty_line_p = 1;
19328 }
19329
19330 it->continuation_lines_width = 0;
19331 row->ends_at_zv_p = 1;
19332 /* A row that displays right-to-left text must always have
19333 its last face extended all the way to the end of line,
19334 even if this row ends in ZV, because we still write to
19335 the screen left to right. We also need to extend the
19336 last face if the default face is remapped to some
19337 different face, otherwise the functions that clear
19338 portions of the screen will clear with the default face's
19339 background color. */
19340 if (row->reversed_p
19341 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19342 extend_face_to_end_of_line (it);
19343 break;
19344 }
19345
19346 /* Now, get the metrics of what we want to display. This also
19347 generates glyphs in `row' (which is IT->glyph_row). */
19348 n_glyphs_before = row->used[TEXT_AREA];
19349 x = it->current_x;
19350
19351 /* Remember the line height so far in case the next element doesn't
19352 fit on the line. */
19353 if (it->line_wrap != TRUNCATE)
19354 {
19355 ascent = it->max_ascent;
19356 descent = it->max_descent;
19357 phys_ascent = it->max_phys_ascent;
19358 phys_descent = it->max_phys_descent;
19359
19360 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19361 {
19362 if (IT_DISPLAYING_WHITESPACE (it))
19363 may_wrap = 1;
19364 else if (may_wrap)
19365 {
19366 SAVE_IT (wrap_it, *it, wrap_data);
19367 wrap_x = x;
19368 wrap_row_used = row->used[TEXT_AREA];
19369 wrap_row_ascent = row->ascent;
19370 wrap_row_height = row->height;
19371 wrap_row_phys_ascent = row->phys_ascent;
19372 wrap_row_phys_height = row->phys_height;
19373 wrap_row_extra_line_spacing = row->extra_line_spacing;
19374 wrap_row_min_pos = min_pos;
19375 wrap_row_min_bpos = min_bpos;
19376 wrap_row_max_pos = max_pos;
19377 wrap_row_max_bpos = max_bpos;
19378 may_wrap = 0;
19379 }
19380 }
19381 }
19382
19383 PRODUCE_GLYPHS (it);
19384
19385 /* If this display element was in marginal areas, continue with
19386 the next one. */
19387 if (it->area != TEXT_AREA)
19388 {
19389 row->ascent = max (row->ascent, it->max_ascent);
19390 row->height = max (row->height, it->max_ascent + it->max_descent);
19391 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19392 row->phys_height = max (row->phys_height,
19393 it->max_phys_ascent + it->max_phys_descent);
19394 row->extra_line_spacing = max (row->extra_line_spacing,
19395 it->max_extra_line_spacing);
19396 set_iterator_to_next (it, 1);
19397 continue;
19398 }
19399
19400 /* Does the display element fit on the line? If we truncate
19401 lines, we should draw past the right edge of the window. If
19402 we don't truncate, we want to stop so that we can display the
19403 continuation glyph before the right margin. If lines are
19404 continued, there are two possible strategies for characters
19405 resulting in more than 1 glyph (e.g. tabs): Display as many
19406 glyphs as possible in this line and leave the rest for the
19407 continuation line, or display the whole element in the next
19408 line. Original redisplay did the former, so we do it also. */
19409 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19410 hpos_before = it->hpos;
19411 x_before = x;
19412
19413 if (/* Not a newline. */
19414 nglyphs > 0
19415 /* Glyphs produced fit entirely in the line. */
19416 && it->current_x < it->last_visible_x)
19417 {
19418 it->hpos += nglyphs;
19419 row->ascent = max (row->ascent, it->max_ascent);
19420 row->height = max (row->height, it->max_ascent + it->max_descent);
19421 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19422 row->phys_height = max (row->phys_height,
19423 it->max_phys_ascent + it->max_phys_descent);
19424 row->extra_line_spacing = max (row->extra_line_spacing,
19425 it->max_extra_line_spacing);
19426 if (it->current_x - it->pixel_width < it->first_visible_x)
19427 row->x = x - it->first_visible_x;
19428 /* Record the maximum and minimum buffer positions seen so
19429 far in glyphs that will be displayed by this row. */
19430 if (it->bidi_p)
19431 RECORD_MAX_MIN_POS (it);
19432 }
19433 else
19434 {
19435 int i, new_x;
19436 struct glyph *glyph;
19437
19438 for (i = 0; i < nglyphs; ++i, x = new_x)
19439 {
19440 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19441 new_x = x + glyph->pixel_width;
19442
19443 if (/* Lines are continued. */
19444 it->line_wrap != TRUNCATE
19445 && (/* Glyph doesn't fit on the line. */
19446 new_x > it->last_visible_x
19447 /* Or it fits exactly on a window system frame. */
19448 || (new_x == it->last_visible_x
19449 && FRAME_WINDOW_P (it->f)
19450 && (row->reversed_p
19451 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19452 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19453 {
19454 /* End of a continued line. */
19455
19456 if (it->hpos == 0
19457 || (new_x == it->last_visible_x
19458 && FRAME_WINDOW_P (it->f)
19459 && (row->reversed_p
19460 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19461 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19462 {
19463 /* Current glyph is the only one on the line or
19464 fits exactly on the line. We must continue
19465 the line because we can't draw the cursor
19466 after the glyph. */
19467 row->continued_p = 1;
19468 it->current_x = new_x;
19469 it->continuation_lines_width += new_x;
19470 ++it->hpos;
19471 if (i == nglyphs - 1)
19472 {
19473 /* If line-wrap is on, check if a previous
19474 wrap point was found. */
19475 if (wrap_row_used > 0
19476 /* Even if there is a previous wrap
19477 point, continue the line here as
19478 usual, if (i) the previous character
19479 was a space or tab AND (ii) the
19480 current character is not. */
19481 && (!may_wrap
19482 || IT_DISPLAYING_WHITESPACE (it)))
19483 goto back_to_wrap;
19484
19485 /* Record the maximum and minimum buffer
19486 positions seen so far in glyphs that will be
19487 displayed by this row. */
19488 if (it->bidi_p)
19489 RECORD_MAX_MIN_POS (it);
19490 set_iterator_to_next (it, 1);
19491 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19492 {
19493 if (!get_next_display_element (it))
19494 {
19495 row->exact_window_width_line_p = 1;
19496 it->continuation_lines_width = 0;
19497 row->continued_p = 0;
19498 row->ends_at_zv_p = 1;
19499 }
19500 else if (ITERATOR_AT_END_OF_LINE_P (it))
19501 {
19502 row->continued_p = 0;
19503 row->exact_window_width_line_p = 1;
19504 }
19505 }
19506 }
19507 else if (it->bidi_p)
19508 RECORD_MAX_MIN_POS (it);
19509 }
19510 else if (CHAR_GLYPH_PADDING_P (*glyph)
19511 && !FRAME_WINDOW_P (it->f))
19512 {
19513 /* A padding glyph that doesn't fit on this line.
19514 This means the whole character doesn't fit
19515 on the line. */
19516 if (row->reversed_p)
19517 unproduce_glyphs (it, row->used[TEXT_AREA]
19518 - n_glyphs_before);
19519 row->used[TEXT_AREA] = n_glyphs_before;
19520
19521 /* Fill the rest of the row with continuation
19522 glyphs like in 20.x. */
19523 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19524 < row->glyphs[1 + TEXT_AREA])
19525 produce_special_glyphs (it, IT_CONTINUATION);
19526
19527 row->continued_p = 1;
19528 it->current_x = x_before;
19529 it->continuation_lines_width += x_before;
19530
19531 /* Restore the height to what it was before the
19532 element not fitting on the line. */
19533 it->max_ascent = ascent;
19534 it->max_descent = descent;
19535 it->max_phys_ascent = phys_ascent;
19536 it->max_phys_descent = phys_descent;
19537 }
19538 else if (wrap_row_used > 0)
19539 {
19540 back_to_wrap:
19541 if (row->reversed_p)
19542 unproduce_glyphs (it,
19543 row->used[TEXT_AREA] - wrap_row_used);
19544 RESTORE_IT (it, &wrap_it, wrap_data);
19545 it->continuation_lines_width += wrap_x;
19546 row->used[TEXT_AREA] = wrap_row_used;
19547 row->ascent = wrap_row_ascent;
19548 row->height = wrap_row_height;
19549 row->phys_ascent = wrap_row_phys_ascent;
19550 row->phys_height = wrap_row_phys_height;
19551 row->extra_line_spacing = wrap_row_extra_line_spacing;
19552 min_pos = wrap_row_min_pos;
19553 min_bpos = wrap_row_min_bpos;
19554 max_pos = wrap_row_max_pos;
19555 max_bpos = wrap_row_max_bpos;
19556 row->continued_p = 1;
19557 row->ends_at_zv_p = 0;
19558 row->exact_window_width_line_p = 0;
19559 it->continuation_lines_width += x;
19560
19561 /* Make sure that a non-default face is extended
19562 up to the right margin of the window. */
19563 extend_face_to_end_of_line (it);
19564 }
19565 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19566 {
19567 /* A TAB that extends past the right edge of the
19568 window. This produces a single glyph on
19569 window system frames. We leave the glyph in
19570 this row and let it fill the row, but don't
19571 consume the TAB. */
19572 if ((row->reversed_p
19573 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19574 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19575 produce_special_glyphs (it, IT_CONTINUATION);
19576 it->continuation_lines_width += it->last_visible_x;
19577 row->ends_in_middle_of_char_p = 1;
19578 row->continued_p = 1;
19579 glyph->pixel_width = it->last_visible_x - x;
19580 it->starts_in_middle_of_char_p = 1;
19581 }
19582 else
19583 {
19584 /* Something other than a TAB that draws past
19585 the right edge of the window. Restore
19586 positions to values before the element. */
19587 if (row->reversed_p)
19588 unproduce_glyphs (it, row->used[TEXT_AREA]
19589 - (n_glyphs_before + i));
19590 row->used[TEXT_AREA] = n_glyphs_before + i;
19591
19592 /* Display continuation glyphs. */
19593 it->current_x = x_before;
19594 it->continuation_lines_width += x;
19595 if (!FRAME_WINDOW_P (it->f)
19596 || (row->reversed_p
19597 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19598 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19599 produce_special_glyphs (it, IT_CONTINUATION);
19600 row->continued_p = 1;
19601
19602 extend_face_to_end_of_line (it);
19603
19604 if (nglyphs > 1 && i > 0)
19605 {
19606 row->ends_in_middle_of_char_p = 1;
19607 it->starts_in_middle_of_char_p = 1;
19608 }
19609
19610 /* Restore the height to what it was before the
19611 element not fitting on the line. */
19612 it->max_ascent = ascent;
19613 it->max_descent = descent;
19614 it->max_phys_ascent = phys_ascent;
19615 it->max_phys_descent = phys_descent;
19616 }
19617
19618 break;
19619 }
19620 else if (new_x > it->first_visible_x)
19621 {
19622 /* Increment number of glyphs actually displayed. */
19623 ++it->hpos;
19624
19625 /* Record the maximum and minimum buffer positions
19626 seen so far in glyphs that will be displayed by
19627 this row. */
19628 if (it->bidi_p)
19629 RECORD_MAX_MIN_POS (it);
19630
19631 if (x < it->first_visible_x)
19632 /* Glyph is partially visible, i.e. row starts at
19633 negative X position. */
19634 row->x = x - it->first_visible_x;
19635 }
19636 else
19637 {
19638 /* Glyph is completely off the left margin of the
19639 window. This should not happen because of the
19640 move_it_in_display_line at the start of this
19641 function, unless the text display area of the
19642 window is empty. */
19643 eassert (it->first_visible_x <= it->last_visible_x);
19644 }
19645 }
19646 /* Even if this display element produced no glyphs at all,
19647 we want to record its position. */
19648 if (it->bidi_p && nglyphs == 0)
19649 RECORD_MAX_MIN_POS (it);
19650
19651 row->ascent = max (row->ascent, it->max_ascent);
19652 row->height = max (row->height, it->max_ascent + it->max_descent);
19653 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19654 row->phys_height = max (row->phys_height,
19655 it->max_phys_ascent + it->max_phys_descent);
19656 row->extra_line_spacing = max (row->extra_line_spacing,
19657 it->max_extra_line_spacing);
19658
19659 /* End of this display line if row is continued. */
19660 if (row->continued_p || row->ends_at_zv_p)
19661 break;
19662 }
19663
19664 at_end_of_line:
19665 /* Is this a line end? If yes, we're also done, after making
19666 sure that a non-default face is extended up to the right
19667 margin of the window. */
19668 if (ITERATOR_AT_END_OF_LINE_P (it))
19669 {
19670 int used_before = row->used[TEXT_AREA];
19671
19672 row->ends_in_newline_from_string_p = STRINGP (it->object);
19673
19674 /* Add a space at the end of the line that is used to
19675 display the cursor there. */
19676 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19677 append_space_for_newline (it, 0);
19678
19679 /* Extend the face to the end of the line. */
19680 extend_face_to_end_of_line (it);
19681
19682 /* Make sure we have the position. */
19683 if (used_before == 0)
19684 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19685
19686 /* Record the position of the newline, for use in
19687 find_row_edges. */
19688 it->eol_pos = it->current.pos;
19689
19690 /* Consume the line end. This skips over invisible lines. */
19691 set_iterator_to_next (it, 1);
19692 it->continuation_lines_width = 0;
19693 break;
19694 }
19695
19696 /* Proceed with next display element. Note that this skips
19697 over lines invisible because of selective display. */
19698 set_iterator_to_next (it, 1);
19699
19700 /* If we truncate lines, we are done when the last displayed
19701 glyphs reach past the right margin of the window. */
19702 if (it->line_wrap == TRUNCATE
19703 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19704 ? (it->current_x >= it->last_visible_x)
19705 : (it->current_x > it->last_visible_x)))
19706 {
19707 /* Maybe add truncation glyphs. */
19708 if (!FRAME_WINDOW_P (it->f)
19709 || (row->reversed_p
19710 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19711 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19712 {
19713 int i, n;
19714
19715 if (!row->reversed_p)
19716 {
19717 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19718 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19719 break;
19720 }
19721 else
19722 {
19723 for (i = 0; i < row->used[TEXT_AREA]; i++)
19724 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19725 break;
19726 /* Remove any padding glyphs at the front of ROW, to
19727 make room for the truncation glyphs we will be
19728 adding below. The loop below always inserts at
19729 least one truncation glyph, so also remove the
19730 last glyph added to ROW. */
19731 unproduce_glyphs (it, i + 1);
19732 /* Adjust i for the loop below. */
19733 i = row->used[TEXT_AREA] - (i + 1);
19734 }
19735
19736 it->current_x = x_before;
19737 if (!FRAME_WINDOW_P (it->f))
19738 {
19739 for (n = row->used[TEXT_AREA]; i < n; ++i)
19740 {
19741 row->used[TEXT_AREA] = i;
19742 produce_special_glyphs (it, IT_TRUNCATION);
19743 }
19744 }
19745 else
19746 {
19747 row->used[TEXT_AREA] = i;
19748 produce_special_glyphs (it, IT_TRUNCATION);
19749 }
19750 }
19751 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19752 {
19753 /* Don't truncate if we can overflow newline into fringe. */
19754 if (!get_next_display_element (it))
19755 {
19756 it->continuation_lines_width = 0;
19757 row->ends_at_zv_p = 1;
19758 row->exact_window_width_line_p = 1;
19759 break;
19760 }
19761 if (ITERATOR_AT_END_OF_LINE_P (it))
19762 {
19763 row->exact_window_width_line_p = 1;
19764 goto at_end_of_line;
19765 }
19766 it->current_x = x_before;
19767 }
19768
19769 row->truncated_on_right_p = 1;
19770 it->continuation_lines_width = 0;
19771 reseat_at_next_visible_line_start (it, 0);
19772 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19773 it->hpos = hpos_before;
19774 break;
19775 }
19776 }
19777
19778 if (wrap_data)
19779 bidi_unshelve_cache (wrap_data, 1);
19780
19781 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19782 at the left window margin. */
19783 if (it->first_visible_x
19784 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19785 {
19786 if (!FRAME_WINDOW_P (it->f)
19787 || (row->reversed_p
19788 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19789 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19790 insert_left_trunc_glyphs (it);
19791 row->truncated_on_left_p = 1;
19792 }
19793
19794 /* Remember the position at which this line ends.
19795
19796 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19797 cannot be before the call to find_row_edges below, since that is
19798 where these positions are determined. */
19799 row->end = it->current;
19800 if (!it->bidi_p)
19801 {
19802 row->minpos = row->start.pos;
19803 row->maxpos = row->end.pos;
19804 }
19805 else
19806 {
19807 /* ROW->minpos and ROW->maxpos must be the smallest and
19808 `1 + the largest' buffer positions in ROW. But if ROW was
19809 bidi-reordered, these two positions can be anywhere in the
19810 row, so we must determine them now. */
19811 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19812 }
19813
19814 /* If the start of this line is the overlay arrow-position, then
19815 mark this glyph row as the one containing the overlay arrow.
19816 This is clearly a mess with variable size fonts. It would be
19817 better to let it be displayed like cursors under X. */
19818 if ((MATRIX_ROW_DISPLAYS_TEXT_P (row) || !overlay_arrow_seen)
19819 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19820 !NILP (overlay_arrow_string)))
19821 {
19822 /* Overlay arrow in window redisplay is a fringe bitmap. */
19823 if (STRINGP (overlay_arrow_string))
19824 {
19825 struct glyph_row *arrow_row
19826 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19827 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19828 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19829 struct glyph *p = row->glyphs[TEXT_AREA];
19830 struct glyph *p2, *end;
19831
19832 /* Copy the arrow glyphs. */
19833 while (glyph < arrow_end)
19834 *p++ = *glyph++;
19835
19836 /* Throw away padding glyphs. */
19837 p2 = p;
19838 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19839 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19840 ++p2;
19841 if (p2 > p)
19842 {
19843 while (p2 < end)
19844 *p++ = *p2++;
19845 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
19846 }
19847 }
19848 else
19849 {
19850 eassert (INTEGERP (overlay_arrow_string));
19851 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
19852 }
19853 overlay_arrow_seen = 1;
19854 }
19855
19856 /* Highlight trailing whitespace. */
19857 if (!NILP (Vshow_trailing_whitespace))
19858 highlight_trailing_whitespace (it->f, it->glyph_row);
19859
19860 /* Compute pixel dimensions of this line. */
19861 compute_line_metrics (it);
19862
19863 /* Implementation note: No changes in the glyphs of ROW or in their
19864 faces can be done past this point, because compute_line_metrics
19865 computes ROW's hash value and stores it within the glyph_row
19866 structure. */
19867
19868 /* Record whether this row ends inside an ellipsis. */
19869 row->ends_in_ellipsis_p
19870 = (it->method == GET_FROM_DISPLAY_VECTOR
19871 && it->ellipsis_p);
19872
19873 /* Save fringe bitmaps in this row. */
19874 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
19875 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
19876 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
19877 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
19878
19879 it->left_user_fringe_bitmap = 0;
19880 it->left_user_fringe_face_id = 0;
19881 it->right_user_fringe_bitmap = 0;
19882 it->right_user_fringe_face_id = 0;
19883
19884 /* Maybe set the cursor. */
19885 cvpos = it->w->cursor.vpos;
19886 if ((cvpos < 0
19887 /* In bidi-reordered rows, keep checking for proper cursor
19888 position even if one has been found already, because buffer
19889 positions in such rows change non-linearly with ROW->VPOS,
19890 when a line is continued. One exception: when we are at ZV,
19891 display cursor on the first suitable glyph row, since all
19892 the empty rows after that also have their position set to ZV. */
19893 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19894 lines' rows is implemented for bidi-reordered rows. */
19895 || (it->bidi_p
19896 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
19897 && PT >= MATRIX_ROW_START_CHARPOS (row)
19898 && PT <= MATRIX_ROW_END_CHARPOS (row)
19899 && cursor_row_p (row))
19900 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
19901
19902 /* Prepare for the next line. This line starts horizontally at (X
19903 HPOS) = (0 0). Vertical positions are incremented. As a
19904 convenience for the caller, IT->glyph_row is set to the next
19905 row to be used. */
19906 it->current_x = it->hpos = 0;
19907 it->current_y += row->height;
19908 SET_TEXT_POS (it->eol_pos, 0, 0);
19909 ++it->vpos;
19910 ++it->glyph_row;
19911 /* The next row should by default use the same value of the
19912 reversed_p flag as this one. set_iterator_to_next decides when
19913 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
19914 the flag accordingly. */
19915 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
19916 it->glyph_row->reversed_p = row->reversed_p;
19917 it->start = row->end;
19918 return MATRIX_ROW_DISPLAYS_TEXT_P (row);
19919
19920 #undef RECORD_MAX_MIN_POS
19921 }
19922
19923 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
19924 Scurrent_bidi_paragraph_direction, 0, 1, 0,
19925 doc: /* Return paragraph direction at point in BUFFER.
19926 Value is either `left-to-right' or `right-to-left'.
19927 If BUFFER is omitted or nil, it defaults to the current buffer.
19928
19929 Paragraph direction determines how the text in the paragraph is displayed.
19930 In left-to-right paragraphs, text begins at the left margin of the window
19931 and the reading direction is generally left to right. In right-to-left
19932 paragraphs, text begins at the right margin and is read from right to left.
19933
19934 See also `bidi-paragraph-direction'. */)
19935 (Lisp_Object buffer)
19936 {
19937 struct buffer *buf = current_buffer;
19938 struct buffer *old = buf;
19939
19940 if (! NILP (buffer))
19941 {
19942 CHECK_BUFFER (buffer);
19943 buf = XBUFFER (buffer);
19944 }
19945
19946 if (NILP (BVAR (buf, bidi_display_reordering))
19947 || NILP (BVAR (buf, enable_multibyte_characters))
19948 /* When we are loading loadup.el, the character property tables
19949 needed for bidi iteration are not yet available. */
19950 || !NILP (Vpurify_flag))
19951 return Qleft_to_right;
19952 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
19953 return BVAR (buf, bidi_paragraph_direction);
19954 else
19955 {
19956 /* Determine the direction from buffer text. We could try to
19957 use current_matrix if it is up to date, but this seems fast
19958 enough as it is. */
19959 struct bidi_it itb;
19960 ptrdiff_t pos = BUF_PT (buf);
19961 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
19962 int c;
19963 void *itb_data = bidi_shelve_cache ();
19964
19965 set_buffer_temp (buf);
19966 /* bidi_paragraph_init finds the base direction of the paragraph
19967 by searching forward from paragraph start. We need the base
19968 direction of the current or _previous_ paragraph, so we need
19969 to make sure we are within that paragraph. To that end, find
19970 the previous non-empty line. */
19971 if (pos >= ZV && pos > BEGV)
19972 DEC_BOTH (pos, bytepos);
19973 if (fast_looking_at (build_string ("[\f\t ]*\n"),
19974 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
19975 {
19976 while ((c = FETCH_BYTE (bytepos)) == '\n'
19977 || c == ' ' || c == '\t' || c == '\f')
19978 {
19979 if (bytepos <= BEGV_BYTE)
19980 break;
19981 bytepos--;
19982 pos--;
19983 }
19984 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
19985 bytepos--;
19986 }
19987 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
19988 itb.paragraph_dir = NEUTRAL_DIR;
19989 itb.string.s = NULL;
19990 itb.string.lstring = Qnil;
19991 itb.string.bufpos = 0;
19992 itb.string.unibyte = 0;
19993 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
19994 bidi_unshelve_cache (itb_data, 0);
19995 set_buffer_temp (old);
19996 switch (itb.paragraph_dir)
19997 {
19998 case L2R:
19999 return Qleft_to_right;
20000 break;
20001 case R2L:
20002 return Qright_to_left;
20003 break;
20004 default:
20005 emacs_abort ();
20006 }
20007 }
20008 }
20009
20010
20011 \f
20012 /***********************************************************************
20013 Menu Bar
20014 ***********************************************************************/
20015
20016 /* Redisplay the menu bar in the frame for window W.
20017
20018 The menu bar of X frames that don't have X toolkit support is
20019 displayed in a special window W->frame->menu_bar_window.
20020
20021 The menu bar of terminal frames is treated specially as far as
20022 glyph matrices are concerned. Menu bar lines are not part of
20023 windows, so the update is done directly on the frame matrix rows
20024 for the menu bar. */
20025
20026 static void
20027 display_menu_bar (struct window *w)
20028 {
20029 struct frame *f = XFRAME (WINDOW_FRAME (w));
20030 struct it it;
20031 Lisp_Object items;
20032 int i;
20033
20034 /* Don't do all this for graphical frames. */
20035 #ifdef HAVE_NTGUI
20036 if (FRAME_W32_P (f))
20037 return;
20038 #endif
20039 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20040 if (FRAME_X_P (f))
20041 return;
20042 #endif
20043
20044 #ifdef HAVE_NS
20045 if (FRAME_NS_P (f))
20046 return;
20047 #endif /* HAVE_NS */
20048
20049 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20050 eassert (!FRAME_WINDOW_P (f));
20051 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20052 it.first_visible_x = 0;
20053 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20054 #elif defined (HAVE_X_WINDOWS) /* X without toolkit. */
20055 if (FRAME_WINDOW_P (f))
20056 {
20057 /* Menu bar lines are displayed in the desired matrix of the
20058 dummy window menu_bar_window. */
20059 struct window *menu_w;
20060 menu_w = XWINDOW (f->menu_bar_window);
20061 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20062 MENU_FACE_ID);
20063 it.first_visible_x = 0;
20064 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20065 }
20066 else
20067 #endif /* not USE_X_TOOLKIT and not USE_GTK */
20068 {
20069 /* This is a TTY frame, i.e. character hpos/vpos are used as
20070 pixel x/y. */
20071 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20072 MENU_FACE_ID);
20073 it.first_visible_x = 0;
20074 it.last_visible_x = FRAME_COLS (f);
20075 }
20076
20077 /* FIXME: This should be controlled by a user option. See the
20078 comments in redisplay_tool_bar and display_mode_line about
20079 this. */
20080 it.paragraph_embedding = L2R;
20081
20082 /* Clear all rows of the menu bar. */
20083 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20084 {
20085 struct glyph_row *row = it.glyph_row + i;
20086 clear_glyph_row (row);
20087 row->enabled_p = 1;
20088 row->full_width_p = 1;
20089 }
20090
20091 /* Display all items of the menu bar. */
20092 items = FRAME_MENU_BAR_ITEMS (it.f);
20093 for (i = 0; i < ASIZE (items); i += 4)
20094 {
20095 Lisp_Object string;
20096
20097 /* Stop at nil string. */
20098 string = AREF (items, i + 1);
20099 if (NILP (string))
20100 break;
20101
20102 /* Remember where item was displayed. */
20103 ASET (items, i + 3, make_number (it.hpos));
20104
20105 /* Display the item, pad with one space. */
20106 if (it.current_x < it.last_visible_x)
20107 display_string (NULL, string, Qnil, 0, 0, &it,
20108 SCHARS (string) + 1, 0, 0, -1);
20109 }
20110
20111 /* Fill out the line with spaces. */
20112 if (it.current_x < it.last_visible_x)
20113 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20114
20115 /* Compute the total height of the lines. */
20116 compute_line_metrics (&it);
20117 }
20118
20119
20120 \f
20121 /***********************************************************************
20122 Mode Line
20123 ***********************************************************************/
20124
20125 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20126 FORCE is non-zero, redisplay mode lines unconditionally.
20127 Otherwise, redisplay only mode lines that are garbaged. Value is
20128 the number of windows whose mode lines were redisplayed. */
20129
20130 static int
20131 redisplay_mode_lines (Lisp_Object window, int force)
20132 {
20133 int nwindows = 0;
20134
20135 while (!NILP (window))
20136 {
20137 struct window *w = XWINDOW (window);
20138
20139 if (WINDOWP (w->contents))
20140 nwindows += redisplay_mode_lines (w->contents, force);
20141 else if (force
20142 || FRAME_GARBAGED_P (XFRAME (w->frame))
20143 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20144 {
20145 struct text_pos lpoint;
20146 struct buffer *old = current_buffer;
20147
20148 /* Set the window's buffer for the mode line display. */
20149 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20150 set_buffer_internal_1 (XBUFFER (w->contents));
20151
20152 /* Point refers normally to the selected window. For any
20153 other window, set up appropriate value. */
20154 if (!EQ (window, selected_window))
20155 {
20156 struct text_pos pt;
20157
20158 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20159 if (CHARPOS (pt) < BEGV)
20160 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20161 else if (CHARPOS (pt) > (ZV - 1))
20162 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20163 else
20164 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20165 }
20166
20167 /* Display mode lines. */
20168 clear_glyph_matrix (w->desired_matrix);
20169 if (display_mode_lines (w))
20170 {
20171 ++nwindows;
20172 w->must_be_updated_p = 1;
20173 }
20174
20175 /* Restore old settings. */
20176 set_buffer_internal_1 (old);
20177 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20178 }
20179
20180 window = w->next;
20181 }
20182
20183 return nwindows;
20184 }
20185
20186
20187 /* Display the mode and/or header line of window W. Value is the
20188 sum number of mode lines and header lines displayed. */
20189
20190 static int
20191 display_mode_lines (struct window *w)
20192 {
20193 Lisp_Object old_selected_window = selected_window;
20194 Lisp_Object old_selected_frame = selected_frame;
20195 Lisp_Object new_frame = w->frame;
20196 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20197 int n = 0;
20198
20199 selected_frame = new_frame;
20200 /* FIXME: If we were to allow the mode-line's computation changing the buffer
20201 or window's point, then we'd need select_window_1 here as well. */
20202 XSETWINDOW (selected_window, w);
20203 XFRAME (new_frame)->selected_window = selected_window;
20204
20205 /* These will be set while the mode line specs are processed. */
20206 line_number_displayed = 0;
20207 w->column_number_displayed = -1;
20208
20209 if (WINDOW_WANTS_MODELINE_P (w))
20210 {
20211 struct window *sel_w = XWINDOW (old_selected_window);
20212
20213 /* Select mode line face based on the real selected window. */
20214 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20215 BVAR (current_buffer, mode_line_format));
20216 ++n;
20217 }
20218
20219 if (WINDOW_WANTS_HEADER_LINE_P (w))
20220 {
20221 display_mode_line (w, HEADER_LINE_FACE_ID,
20222 BVAR (current_buffer, header_line_format));
20223 ++n;
20224 }
20225
20226 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20227 selected_frame = old_selected_frame;
20228 selected_window = old_selected_window;
20229 return n;
20230 }
20231
20232
20233 /* Display mode or header line of window W. FACE_ID specifies which
20234 line to display; it is either MODE_LINE_FACE_ID or
20235 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20236 display. Value is the pixel height of the mode/header line
20237 displayed. */
20238
20239 static int
20240 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20241 {
20242 struct it it;
20243 struct face *face;
20244 ptrdiff_t count = SPECPDL_INDEX ();
20245
20246 init_iterator (&it, w, -1, -1, NULL, face_id);
20247 /* Don't extend on a previously drawn mode-line.
20248 This may happen if called from pos_visible_p. */
20249 it.glyph_row->enabled_p = 0;
20250 prepare_desired_row (it.glyph_row);
20251
20252 it.glyph_row->mode_line_p = 1;
20253
20254 /* FIXME: This should be controlled by a user option. But
20255 supporting such an option is not trivial, since the mode line is
20256 made up of many separate strings. */
20257 it.paragraph_embedding = L2R;
20258
20259 record_unwind_protect (unwind_format_mode_line,
20260 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20261
20262 mode_line_target = MODE_LINE_DISPLAY;
20263
20264 /* Temporarily make frame's keyboard the current kboard so that
20265 kboard-local variables in the mode_line_format will get the right
20266 values. */
20267 push_kboard (FRAME_KBOARD (it.f));
20268 record_unwind_save_match_data ();
20269 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20270 pop_kboard ();
20271
20272 unbind_to (count, Qnil);
20273
20274 /* Fill up with spaces. */
20275 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20276
20277 compute_line_metrics (&it);
20278 it.glyph_row->full_width_p = 1;
20279 it.glyph_row->continued_p = 0;
20280 it.glyph_row->truncated_on_left_p = 0;
20281 it.glyph_row->truncated_on_right_p = 0;
20282
20283 /* Make a 3D mode-line have a shadow at its right end. */
20284 face = FACE_FROM_ID (it.f, face_id);
20285 extend_face_to_end_of_line (&it);
20286 if (face->box != FACE_NO_BOX)
20287 {
20288 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20289 + it.glyph_row->used[TEXT_AREA] - 1);
20290 last->right_box_line_p = 1;
20291 }
20292
20293 return it.glyph_row->height;
20294 }
20295
20296 /* Move element ELT in LIST to the front of LIST.
20297 Return the updated list. */
20298
20299 static Lisp_Object
20300 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20301 {
20302 register Lisp_Object tail, prev;
20303 register Lisp_Object tem;
20304
20305 tail = list;
20306 prev = Qnil;
20307 while (CONSP (tail))
20308 {
20309 tem = XCAR (tail);
20310
20311 if (EQ (elt, tem))
20312 {
20313 /* Splice out the link TAIL. */
20314 if (NILP (prev))
20315 list = XCDR (tail);
20316 else
20317 Fsetcdr (prev, XCDR (tail));
20318
20319 /* Now make it the first. */
20320 Fsetcdr (tail, list);
20321 return tail;
20322 }
20323 else
20324 prev = tail;
20325 tail = XCDR (tail);
20326 QUIT;
20327 }
20328
20329 /* Not found--return unchanged LIST. */
20330 return list;
20331 }
20332
20333 /* Contribute ELT to the mode line for window IT->w. How it
20334 translates into text depends on its data type.
20335
20336 IT describes the display environment in which we display, as usual.
20337
20338 DEPTH is the depth in recursion. It is used to prevent
20339 infinite recursion here.
20340
20341 FIELD_WIDTH is the number of characters the display of ELT should
20342 occupy in the mode line, and PRECISION is the maximum number of
20343 characters to display from ELT's representation. See
20344 display_string for details.
20345
20346 Returns the hpos of the end of the text generated by ELT.
20347
20348 PROPS is a property list to add to any string we encounter.
20349
20350 If RISKY is nonzero, remove (disregard) any properties in any string
20351 we encounter, and ignore :eval and :propertize.
20352
20353 The global variable `mode_line_target' determines whether the
20354 output is passed to `store_mode_line_noprop',
20355 `store_mode_line_string', or `display_string'. */
20356
20357 static int
20358 display_mode_element (struct it *it, int depth, int field_width, int precision,
20359 Lisp_Object elt, Lisp_Object props, int risky)
20360 {
20361 int n = 0, field, prec;
20362 int literal = 0;
20363
20364 tail_recurse:
20365 if (depth > 100)
20366 elt = build_string ("*too-deep*");
20367
20368 depth++;
20369
20370 switch (XTYPE (elt))
20371 {
20372 case Lisp_String:
20373 {
20374 /* A string: output it and check for %-constructs within it. */
20375 unsigned char c;
20376 ptrdiff_t offset = 0;
20377
20378 if (SCHARS (elt) > 0
20379 && (!NILP (props) || risky))
20380 {
20381 Lisp_Object oprops, aelt;
20382 oprops = Ftext_properties_at (make_number (0), elt);
20383
20384 /* If the starting string's properties are not what
20385 we want, translate the string. Also, if the string
20386 is risky, do that anyway. */
20387
20388 if (NILP (Fequal (props, oprops)) || risky)
20389 {
20390 /* If the starting string has properties,
20391 merge the specified ones onto the existing ones. */
20392 if (! NILP (oprops) && !risky)
20393 {
20394 Lisp_Object tem;
20395
20396 oprops = Fcopy_sequence (oprops);
20397 tem = props;
20398 while (CONSP (tem))
20399 {
20400 oprops = Fplist_put (oprops, XCAR (tem),
20401 XCAR (XCDR (tem)));
20402 tem = XCDR (XCDR (tem));
20403 }
20404 props = oprops;
20405 }
20406
20407 aelt = Fassoc (elt, mode_line_proptrans_alist);
20408 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20409 {
20410 /* AELT is what we want. Move it to the front
20411 without consing. */
20412 elt = XCAR (aelt);
20413 mode_line_proptrans_alist
20414 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20415 }
20416 else
20417 {
20418 Lisp_Object tem;
20419
20420 /* If AELT has the wrong props, it is useless.
20421 so get rid of it. */
20422 if (! NILP (aelt))
20423 mode_line_proptrans_alist
20424 = Fdelq (aelt, mode_line_proptrans_alist);
20425
20426 elt = Fcopy_sequence (elt);
20427 Fset_text_properties (make_number (0), Flength (elt),
20428 props, elt);
20429 /* Add this item to mode_line_proptrans_alist. */
20430 mode_line_proptrans_alist
20431 = Fcons (Fcons (elt, props),
20432 mode_line_proptrans_alist);
20433 /* Truncate mode_line_proptrans_alist
20434 to at most 50 elements. */
20435 tem = Fnthcdr (make_number (50),
20436 mode_line_proptrans_alist);
20437 if (! NILP (tem))
20438 XSETCDR (tem, Qnil);
20439 }
20440 }
20441 }
20442
20443 offset = 0;
20444
20445 if (literal)
20446 {
20447 prec = precision - n;
20448 switch (mode_line_target)
20449 {
20450 case MODE_LINE_NOPROP:
20451 case MODE_LINE_TITLE:
20452 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20453 break;
20454 case MODE_LINE_STRING:
20455 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20456 break;
20457 case MODE_LINE_DISPLAY:
20458 n += display_string (NULL, elt, Qnil, 0, 0, it,
20459 0, prec, 0, STRING_MULTIBYTE (elt));
20460 break;
20461 }
20462
20463 break;
20464 }
20465
20466 /* Handle the non-literal case. */
20467
20468 while ((precision <= 0 || n < precision)
20469 && SREF (elt, offset) != 0
20470 && (mode_line_target != MODE_LINE_DISPLAY
20471 || it->current_x < it->last_visible_x))
20472 {
20473 ptrdiff_t last_offset = offset;
20474
20475 /* Advance to end of string or next format specifier. */
20476 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20477 ;
20478
20479 if (offset - 1 != last_offset)
20480 {
20481 ptrdiff_t nchars, nbytes;
20482
20483 /* Output to end of string or up to '%'. Field width
20484 is length of string. Don't output more than
20485 PRECISION allows us. */
20486 offset--;
20487
20488 prec = c_string_width (SDATA (elt) + last_offset,
20489 offset - last_offset, precision - n,
20490 &nchars, &nbytes);
20491
20492 switch (mode_line_target)
20493 {
20494 case MODE_LINE_NOPROP:
20495 case MODE_LINE_TITLE:
20496 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20497 break;
20498 case MODE_LINE_STRING:
20499 {
20500 ptrdiff_t bytepos = last_offset;
20501 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20502 ptrdiff_t endpos = (precision <= 0
20503 ? string_byte_to_char (elt, offset)
20504 : charpos + nchars);
20505
20506 n += store_mode_line_string (NULL,
20507 Fsubstring (elt, make_number (charpos),
20508 make_number (endpos)),
20509 0, 0, 0, Qnil);
20510 }
20511 break;
20512 case MODE_LINE_DISPLAY:
20513 {
20514 ptrdiff_t bytepos = last_offset;
20515 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20516
20517 if (precision <= 0)
20518 nchars = string_byte_to_char (elt, offset) - charpos;
20519 n += display_string (NULL, elt, Qnil, 0, charpos,
20520 it, 0, nchars, 0,
20521 STRING_MULTIBYTE (elt));
20522 }
20523 break;
20524 }
20525 }
20526 else /* c == '%' */
20527 {
20528 ptrdiff_t percent_position = offset;
20529
20530 /* Get the specified minimum width. Zero means
20531 don't pad. */
20532 field = 0;
20533 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20534 field = field * 10 + c - '0';
20535
20536 /* Don't pad beyond the total padding allowed. */
20537 if (field_width - n > 0 && field > field_width - n)
20538 field = field_width - n;
20539
20540 /* Note that either PRECISION <= 0 or N < PRECISION. */
20541 prec = precision - n;
20542
20543 if (c == 'M')
20544 n += display_mode_element (it, depth, field, prec,
20545 Vglobal_mode_string, props,
20546 risky);
20547 else if (c != 0)
20548 {
20549 bool multibyte;
20550 ptrdiff_t bytepos, charpos;
20551 const char *spec;
20552 Lisp_Object string;
20553
20554 bytepos = percent_position;
20555 charpos = (STRING_MULTIBYTE (elt)
20556 ? string_byte_to_char (elt, bytepos)
20557 : bytepos);
20558 spec = decode_mode_spec (it->w, c, field, &string);
20559 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20560
20561 switch (mode_line_target)
20562 {
20563 case MODE_LINE_NOPROP:
20564 case MODE_LINE_TITLE:
20565 n += store_mode_line_noprop (spec, field, prec);
20566 break;
20567 case MODE_LINE_STRING:
20568 {
20569 Lisp_Object tem = build_string (spec);
20570 props = Ftext_properties_at (make_number (charpos), elt);
20571 /* Should only keep face property in props */
20572 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20573 }
20574 break;
20575 case MODE_LINE_DISPLAY:
20576 {
20577 int nglyphs_before, nwritten;
20578
20579 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20580 nwritten = display_string (spec, string, elt,
20581 charpos, 0, it,
20582 field, prec, 0,
20583 multibyte);
20584
20585 /* Assign to the glyphs written above the
20586 string where the `%x' came from, position
20587 of the `%'. */
20588 if (nwritten > 0)
20589 {
20590 struct glyph *glyph
20591 = (it->glyph_row->glyphs[TEXT_AREA]
20592 + nglyphs_before);
20593 int i;
20594
20595 for (i = 0; i < nwritten; ++i)
20596 {
20597 glyph[i].object = elt;
20598 glyph[i].charpos = charpos;
20599 }
20600
20601 n += nwritten;
20602 }
20603 }
20604 break;
20605 }
20606 }
20607 else /* c == 0 */
20608 break;
20609 }
20610 }
20611 }
20612 break;
20613
20614 case Lisp_Symbol:
20615 /* A symbol: process the value of the symbol recursively
20616 as if it appeared here directly. Avoid error if symbol void.
20617 Special case: if value of symbol is a string, output the string
20618 literally. */
20619 {
20620 register Lisp_Object tem;
20621
20622 /* If the variable is not marked as risky to set
20623 then its contents are risky to use. */
20624 if (NILP (Fget (elt, Qrisky_local_variable)))
20625 risky = 1;
20626
20627 tem = Fboundp (elt);
20628 if (!NILP (tem))
20629 {
20630 tem = Fsymbol_value (elt);
20631 /* If value is a string, output that string literally:
20632 don't check for % within it. */
20633 if (STRINGP (tem))
20634 literal = 1;
20635
20636 if (!EQ (tem, elt))
20637 {
20638 /* Give up right away for nil or t. */
20639 elt = tem;
20640 goto tail_recurse;
20641 }
20642 }
20643 }
20644 break;
20645
20646 case Lisp_Cons:
20647 {
20648 register Lisp_Object car, tem;
20649
20650 /* A cons cell: five distinct cases.
20651 If first element is :eval or :propertize, do something special.
20652 If first element is a string or a cons, process all the elements
20653 and effectively concatenate them.
20654 If first element is a negative number, truncate displaying cdr to
20655 at most that many characters. If positive, pad (with spaces)
20656 to at least that many characters.
20657 If first element is a symbol, process the cadr or caddr recursively
20658 according to whether the symbol's value is non-nil or nil. */
20659 car = XCAR (elt);
20660 if (EQ (car, QCeval))
20661 {
20662 /* An element of the form (:eval FORM) means evaluate FORM
20663 and use the result as mode line elements. */
20664
20665 if (risky)
20666 break;
20667
20668 if (CONSP (XCDR (elt)))
20669 {
20670 Lisp_Object spec;
20671 spec = safe_eval (XCAR (XCDR (elt)));
20672 n += display_mode_element (it, depth, field_width - n,
20673 precision - n, spec, props,
20674 risky);
20675 }
20676 }
20677 else if (EQ (car, QCpropertize))
20678 {
20679 /* An element of the form (:propertize ELT PROPS...)
20680 means display ELT but applying properties PROPS. */
20681
20682 if (risky)
20683 break;
20684
20685 if (CONSP (XCDR (elt)))
20686 n += display_mode_element (it, depth, field_width - n,
20687 precision - n, XCAR (XCDR (elt)),
20688 XCDR (XCDR (elt)), risky);
20689 }
20690 else if (SYMBOLP (car))
20691 {
20692 tem = Fboundp (car);
20693 elt = XCDR (elt);
20694 if (!CONSP (elt))
20695 goto invalid;
20696 /* elt is now the cdr, and we know it is a cons cell.
20697 Use its car if CAR has a non-nil value. */
20698 if (!NILP (tem))
20699 {
20700 tem = Fsymbol_value (car);
20701 if (!NILP (tem))
20702 {
20703 elt = XCAR (elt);
20704 goto tail_recurse;
20705 }
20706 }
20707 /* Symbol's value is nil (or symbol is unbound)
20708 Get the cddr of the original list
20709 and if possible find the caddr and use that. */
20710 elt = XCDR (elt);
20711 if (NILP (elt))
20712 break;
20713 else if (!CONSP (elt))
20714 goto invalid;
20715 elt = XCAR (elt);
20716 goto tail_recurse;
20717 }
20718 else if (INTEGERP (car))
20719 {
20720 register int lim = XINT (car);
20721 elt = XCDR (elt);
20722 if (lim < 0)
20723 {
20724 /* Negative int means reduce maximum width. */
20725 if (precision <= 0)
20726 precision = -lim;
20727 else
20728 precision = min (precision, -lim);
20729 }
20730 else if (lim > 0)
20731 {
20732 /* Padding specified. Don't let it be more than
20733 current maximum. */
20734 if (precision > 0)
20735 lim = min (precision, lim);
20736
20737 /* If that's more padding than already wanted, queue it.
20738 But don't reduce padding already specified even if
20739 that is beyond the current truncation point. */
20740 field_width = max (lim, field_width);
20741 }
20742 goto tail_recurse;
20743 }
20744 else if (STRINGP (car) || CONSP (car))
20745 {
20746 Lisp_Object halftail = elt;
20747 int len = 0;
20748
20749 while (CONSP (elt)
20750 && (precision <= 0 || n < precision))
20751 {
20752 n += display_mode_element (it, depth,
20753 /* Do padding only after the last
20754 element in the list. */
20755 (! CONSP (XCDR (elt))
20756 ? field_width - n
20757 : 0),
20758 precision - n, XCAR (elt),
20759 props, risky);
20760 elt = XCDR (elt);
20761 len++;
20762 if ((len & 1) == 0)
20763 halftail = XCDR (halftail);
20764 /* Check for cycle. */
20765 if (EQ (halftail, elt))
20766 break;
20767 }
20768 }
20769 }
20770 break;
20771
20772 default:
20773 invalid:
20774 elt = build_string ("*invalid*");
20775 goto tail_recurse;
20776 }
20777
20778 /* Pad to FIELD_WIDTH. */
20779 if (field_width > 0 && n < field_width)
20780 {
20781 switch (mode_line_target)
20782 {
20783 case MODE_LINE_NOPROP:
20784 case MODE_LINE_TITLE:
20785 n += store_mode_line_noprop ("", field_width - n, 0);
20786 break;
20787 case MODE_LINE_STRING:
20788 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20789 break;
20790 case MODE_LINE_DISPLAY:
20791 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20792 0, 0, 0);
20793 break;
20794 }
20795 }
20796
20797 return n;
20798 }
20799
20800 /* Store a mode-line string element in mode_line_string_list.
20801
20802 If STRING is non-null, display that C string. Otherwise, the Lisp
20803 string LISP_STRING is displayed.
20804
20805 FIELD_WIDTH is the minimum number of output glyphs to produce.
20806 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20807 with spaces. FIELD_WIDTH <= 0 means don't pad.
20808
20809 PRECISION is the maximum number of characters to output from
20810 STRING. PRECISION <= 0 means don't truncate the string.
20811
20812 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20813 properties to the string.
20814
20815 PROPS are the properties to add to the string.
20816 The mode_line_string_face face property is always added to the string.
20817 */
20818
20819 static int
20820 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20821 int field_width, int precision, Lisp_Object props)
20822 {
20823 ptrdiff_t len;
20824 int n = 0;
20825
20826 if (string != NULL)
20827 {
20828 len = strlen (string);
20829 if (precision > 0 && len > precision)
20830 len = precision;
20831 lisp_string = make_string (string, len);
20832 if (NILP (props))
20833 props = mode_line_string_face_prop;
20834 else if (!NILP (mode_line_string_face))
20835 {
20836 Lisp_Object face = Fplist_get (props, Qface);
20837 props = Fcopy_sequence (props);
20838 if (NILP (face))
20839 face = mode_line_string_face;
20840 else
20841 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20842 props = Fplist_put (props, Qface, face);
20843 }
20844 Fadd_text_properties (make_number (0), make_number (len),
20845 props, lisp_string);
20846 }
20847 else
20848 {
20849 len = XFASTINT (Flength (lisp_string));
20850 if (precision > 0 && len > precision)
20851 {
20852 len = precision;
20853 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
20854 precision = -1;
20855 }
20856 if (!NILP (mode_line_string_face))
20857 {
20858 Lisp_Object face;
20859 if (NILP (props))
20860 props = Ftext_properties_at (make_number (0), lisp_string);
20861 face = Fplist_get (props, Qface);
20862 if (NILP (face))
20863 face = mode_line_string_face;
20864 else
20865 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
20866 props = Fcons (Qface, Fcons (face, Qnil));
20867 if (copy_string)
20868 lisp_string = Fcopy_sequence (lisp_string);
20869 }
20870 if (!NILP (props))
20871 Fadd_text_properties (make_number (0), make_number (len),
20872 props, lisp_string);
20873 }
20874
20875 if (len > 0)
20876 {
20877 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20878 n += len;
20879 }
20880
20881 if (field_width > len)
20882 {
20883 field_width -= len;
20884 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
20885 if (!NILP (props))
20886 Fadd_text_properties (make_number (0), make_number (field_width),
20887 props, lisp_string);
20888 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
20889 n += field_width;
20890 }
20891
20892 return n;
20893 }
20894
20895
20896 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
20897 1, 4, 0,
20898 doc: /* Format a string out of a mode line format specification.
20899 First arg FORMAT specifies the mode line format (see `mode-line-format'
20900 for details) to use.
20901
20902 By default, the format is evaluated for the currently selected window.
20903
20904 Optional second arg FACE specifies the face property to put on all
20905 characters for which no face is specified. The value nil means the
20906 default face. The value t means whatever face the window's mode line
20907 currently uses (either `mode-line' or `mode-line-inactive',
20908 depending on whether the window is the selected window or not).
20909 An integer value means the value string has no text
20910 properties.
20911
20912 Optional third and fourth args WINDOW and BUFFER specify the window
20913 and buffer to use as the context for the formatting (defaults
20914 are the selected window and the WINDOW's buffer). */)
20915 (Lisp_Object format, Lisp_Object face,
20916 Lisp_Object window, Lisp_Object buffer)
20917 {
20918 struct it it;
20919 int len;
20920 struct window *w;
20921 struct buffer *old_buffer = NULL;
20922 int face_id;
20923 int no_props = INTEGERP (face);
20924 ptrdiff_t count = SPECPDL_INDEX ();
20925 Lisp_Object str;
20926 int string_start = 0;
20927
20928 w = decode_any_window (window);
20929 XSETWINDOW (window, w);
20930
20931 if (NILP (buffer))
20932 buffer = w->contents;
20933 CHECK_BUFFER (buffer);
20934
20935 /* Make formatting the modeline a non-op when noninteractive, otherwise
20936 there will be problems later caused by a partially initialized frame. */
20937 if (NILP (format) || noninteractive)
20938 return empty_unibyte_string;
20939
20940 if (no_props)
20941 face = Qnil;
20942
20943 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
20944 : EQ (face, Qt) ? (EQ (window, selected_window)
20945 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
20946 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
20947 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
20948 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
20949 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
20950 : DEFAULT_FACE_ID;
20951
20952 old_buffer = current_buffer;
20953
20954 /* Save things including mode_line_proptrans_alist,
20955 and set that to nil so that we don't alter the outer value. */
20956 record_unwind_protect (unwind_format_mode_line,
20957 format_mode_line_unwind_data
20958 (XFRAME (WINDOW_FRAME (w)),
20959 old_buffer, selected_window, 1));
20960 mode_line_proptrans_alist = Qnil;
20961
20962 Fselect_window (window, Qt);
20963 set_buffer_internal_1 (XBUFFER (buffer));
20964
20965 init_iterator (&it, w, -1, -1, NULL, face_id);
20966
20967 if (no_props)
20968 {
20969 mode_line_target = MODE_LINE_NOPROP;
20970 mode_line_string_face_prop = Qnil;
20971 mode_line_string_list = Qnil;
20972 string_start = MODE_LINE_NOPROP_LEN (0);
20973 }
20974 else
20975 {
20976 mode_line_target = MODE_LINE_STRING;
20977 mode_line_string_list = Qnil;
20978 mode_line_string_face = face;
20979 mode_line_string_face_prop
20980 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
20981 }
20982
20983 push_kboard (FRAME_KBOARD (it.f));
20984 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20985 pop_kboard ();
20986
20987 if (no_props)
20988 {
20989 len = MODE_LINE_NOPROP_LEN (string_start);
20990 str = make_string (mode_line_noprop_buf + string_start, len);
20991 }
20992 else
20993 {
20994 mode_line_string_list = Fnreverse (mode_line_string_list);
20995 str = Fmapconcat (intern ("identity"), mode_line_string_list,
20996 empty_unibyte_string);
20997 }
20998
20999 unbind_to (count, Qnil);
21000 return str;
21001 }
21002
21003 /* Write a null-terminated, right justified decimal representation of
21004 the positive integer D to BUF using a minimal field width WIDTH. */
21005
21006 static void
21007 pint2str (register char *buf, register int width, register ptrdiff_t d)
21008 {
21009 register char *p = buf;
21010
21011 if (d <= 0)
21012 *p++ = '0';
21013 else
21014 {
21015 while (d > 0)
21016 {
21017 *p++ = d % 10 + '0';
21018 d /= 10;
21019 }
21020 }
21021
21022 for (width -= (int) (p - buf); width > 0; --width)
21023 *p++ = ' ';
21024 *p-- = '\0';
21025 while (p > buf)
21026 {
21027 d = *buf;
21028 *buf++ = *p;
21029 *p-- = d;
21030 }
21031 }
21032
21033 /* Write a null-terminated, right justified decimal and "human
21034 readable" representation of the nonnegative integer D to BUF using
21035 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21036
21037 static const char power_letter[] =
21038 {
21039 0, /* no letter */
21040 'k', /* kilo */
21041 'M', /* mega */
21042 'G', /* giga */
21043 'T', /* tera */
21044 'P', /* peta */
21045 'E', /* exa */
21046 'Z', /* zetta */
21047 'Y' /* yotta */
21048 };
21049
21050 static void
21051 pint2hrstr (char *buf, int width, ptrdiff_t d)
21052 {
21053 /* We aim to represent the nonnegative integer D as
21054 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21055 ptrdiff_t quotient = d;
21056 int remainder = 0;
21057 /* -1 means: do not use TENTHS. */
21058 int tenths = -1;
21059 int exponent = 0;
21060
21061 /* Length of QUOTIENT.TENTHS as a string. */
21062 int length;
21063
21064 char * psuffix;
21065 char * p;
21066
21067 if (quotient >= 1000)
21068 {
21069 /* Scale to the appropriate EXPONENT. */
21070 do
21071 {
21072 remainder = quotient % 1000;
21073 quotient /= 1000;
21074 exponent++;
21075 }
21076 while (quotient >= 1000);
21077
21078 /* Round to nearest and decide whether to use TENTHS or not. */
21079 if (quotient <= 9)
21080 {
21081 tenths = remainder / 100;
21082 if (remainder % 100 >= 50)
21083 {
21084 if (tenths < 9)
21085 tenths++;
21086 else
21087 {
21088 quotient++;
21089 if (quotient == 10)
21090 tenths = -1;
21091 else
21092 tenths = 0;
21093 }
21094 }
21095 }
21096 else
21097 if (remainder >= 500)
21098 {
21099 if (quotient < 999)
21100 quotient++;
21101 else
21102 {
21103 quotient = 1;
21104 exponent++;
21105 tenths = 0;
21106 }
21107 }
21108 }
21109
21110 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21111 if (tenths == -1 && quotient <= 99)
21112 if (quotient <= 9)
21113 length = 1;
21114 else
21115 length = 2;
21116 else
21117 length = 3;
21118 p = psuffix = buf + max (width, length);
21119
21120 /* Print EXPONENT. */
21121 *psuffix++ = power_letter[exponent];
21122 *psuffix = '\0';
21123
21124 /* Print TENTHS. */
21125 if (tenths >= 0)
21126 {
21127 *--p = '0' + tenths;
21128 *--p = '.';
21129 }
21130
21131 /* Print QUOTIENT. */
21132 do
21133 {
21134 int digit = quotient % 10;
21135 *--p = '0' + digit;
21136 }
21137 while ((quotient /= 10) != 0);
21138
21139 /* Print leading spaces. */
21140 while (buf < p)
21141 *--p = ' ';
21142 }
21143
21144 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21145 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21146 type of CODING_SYSTEM. Return updated pointer into BUF. */
21147
21148 static unsigned char invalid_eol_type[] = "(*invalid*)";
21149
21150 static char *
21151 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21152 {
21153 Lisp_Object val;
21154 bool multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21155 const unsigned char *eol_str;
21156 int eol_str_len;
21157 /* The EOL conversion we are using. */
21158 Lisp_Object eoltype;
21159
21160 val = CODING_SYSTEM_SPEC (coding_system);
21161 eoltype = Qnil;
21162
21163 if (!VECTORP (val)) /* Not yet decided. */
21164 {
21165 *buf++ = multibyte ? '-' : ' ';
21166 if (eol_flag)
21167 eoltype = eol_mnemonic_undecided;
21168 /* Don't mention EOL conversion if it isn't decided. */
21169 }
21170 else
21171 {
21172 Lisp_Object attrs;
21173 Lisp_Object eolvalue;
21174
21175 attrs = AREF (val, 0);
21176 eolvalue = AREF (val, 2);
21177
21178 *buf++ = multibyte
21179 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21180 : ' ';
21181
21182 if (eol_flag)
21183 {
21184 /* The EOL conversion that is normal on this system. */
21185
21186 if (NILP (eolvalue)) /* Not yet decided. */
21187 eoltype = eol_mnemonic_undecided;
21188 else if (VECTORP (eolvalue)) /* Not yet decided. */
21189 eoltype = eol_mnemonic_undecided;
21190 else /* eolvalue is Qunix, Qdos, or Qmac. */
21191 eoltype = (EQ (eolvalue, Qunix)
21192 ? eol_mnemonic_unix
21193 : (EQ (eolvalue, Qdos) == 1
21194 ? eol_mnemonic_dos : eol_mnemonic_mac));
21195 }
21196 }
21197
21198 if (eol_flag)
21199 {
21200 /* Mention the EOL conversion if it is not the usual one. */
21201 if (STRINGP (eoltype))
21202 {
21203 eol_str = SDATA (eoltype);
21204 eol_str_len = SBYTES (eoltype);
21205 }
21206 else if (CHARACTERP (eoltype))
21207 {
21208 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21209 int c = XFASTINT (eoltype);
21210 eol_str_len = CHAR_STRING (c, tmp);
21211 eol_str = tmp;
21212 }
21213 else
21214 {
21215 eol_str = invalid_eol_type;
21216 eol_str_len = sizeof (invalid_eol_type) - 1;
21217 }
21218 memcpy (buf, eol_str, eol_str_len);
21219 buf += eol_str_len;
21220 }
21221
21222 return buf;
21223 }
21224
21225 /* Return a string for the output of a mode line %-spec for window W,
21226 generated by character C. FIELD_WIDTH > 0 means pad the string
21227 returned with spaces to that value. Return a Lisp string in
21228 *STRING if the resulting string is taken from that Lisp string.
21229
21230 Note we operate on the current buffer for most purposes. */
21231
21232 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21233
21234 static const char *
21235 decode_mode_spec (struct window *w, register int c, int field_width,
21236 Lisp_Object *string)
21237 {
21238 Lisp_Object obj;
21239 struct frame *f = XFRAME (WINDOW_FRAME (w));
21240 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21241 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21242 produce strings from numerical values, so limit preposterously
21243 large values of FIELD_WIDTH to avoid overrunning the buffer's
21244 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21245 bytes plus the terminating null. */
21246 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21247 struct buffer *b = current_buffer;
21248
21249 obj = Qnil;
21250 *string = Qnil;
21251
21252 switch (c)
21253 {
21254 case '*':
21255 if (!NILP (BVAR (b, read_only)))
21256 return "%";
21257 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21258 return "*";
21259 return "-";
21260
21261 case '+':
21262 /* This differs from %* only for a modified read-only buffer. */
21263 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21264 return "*";
21265 if (!NILP (BVAR (b, read_only)))
21266 return "%";
21267 return "-";
21268
21269 case '&':
21270 /* This differs from %* in ignoring read-only-ness. */
21271 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21272 return "*";
21273 return "-";
21274
21275 case '%':
21276 return "%";
21277
21278 case '[':
21279 {
21280 int i;
21281 char *p;
21282
21283 if (command_loop_level > 5)
21284 return "[[[... ";
21285 p = decode_mode_spec_buf;
21286 for (i = 0; i < command_loop_level; i++)
21287 *p++ = '[';
21288 *p = 0;
21289 return decode_mode_spec_buf;
21290 }
21291
21292 case ']':
21293 {
21294 int i;
21295 char *p;
21296
21297 if (command_loop_level > 5)
21298 return " ...]]]";
21299 p = decode_mode_spec_buf;
21300 for (i = 0; i < command_loop_level; i++)
21301 *p++ = ']';
21302 *p = 0;
21303 return decode_mode_spec_buf;
21304 }
21305
21306 case '-':
21307 {
21308 register int i;
21309
21310 /* Let lots_of_dashes be a string of infinite length. */
21311 if (mode_line_target == MODE_LINE_NOPROP
21312 || mode_line_target == MODE_LINE_STRING)
21313 return "--";
21314 if (field_width <= 0
21315 || field_width > sizeof (lots_of_dashes))
21316 {
21317 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21318 decode_mode_spec_buf[i] = '-';
21319 decode_mode_spec_buf[i] = '\0';
21320 return decode_mode_spec_buf;
21321 }
21322 else
21323 return lots_of_dashes;
21324 }
21325
21326 case 'b':
21327 obj = BVAR (b, name);
21328 break;
21329
21330 case 'c':
21331 /* %c and %l are ignored in `frame-title-format'.
21332 (In redisplay_internal, the frame title is drawn _before_ the
21333 windows are updated, so the stuff which depends on actual
21334 window contents (such as %l) may fail to render properly, or
21335 even crash emacs.) */
21336 if (mode_line_target == MODE_LINE_TITLE)
21337 return "";
21338 else
21339 {
21340 ptrdiff_t col = current_column ();
21341 w->column_number_displayed = col;
21342 pint2str (decode_mode_spec_buf, width, col);
21343 return decode_mode_spec_buf;
21344 }
21345
21346 case 'e':
21347 #ifndef SYSTEM_MALLOC
21348 {
21349 if (NILP (Vmemory_full))
21350 return "";
21351 else
21352 return "!MEM FULL! ";
21353 }
21354 #else
21355 return "";
21356 #endif
21357
21358 case 'F':
21359 /* %F displays the frame name. */
21360 if (!NILP (f->title))
21361 return SSDATA (f->title);
21362 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21363 return SSDATA (f->name);
21364 return "Emacs";
21365
21366 case 'f':
21367 obj = BVAR (b, filename);
21368 break;
21369
21370 case 'i':
21371 {
21372 ptrdiff_t size = ZV - BEGV;
21373 pint2str (decode_mode_spec_buf, width, size);
21374 return decode_mode_spec_buf;
21375 }
21376
21377 case 'I':
21378 {
21379 ptrdiff_t size = ZV - BEGV;
21380 pint2hrstr (decode_mode_spec_buf, width, size);
21381 return decode_mode_spec_buf;
21382 }
21383
21384 case 'l':
21385 {
21386 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21387 ptrdiff_t topline, nlines, height;
21388 ptrdiff_t junk;
21389
21390 /* %c and %l are ignored in `frame-title-format'. */
21391 if (mode_line_target == MODE_LINE_TITLE)
21392 return "";
21393
21394 startpos = marker_position (w->start);
21395 startpos_byte = marker_byte_position (w->start);
21396 height = WINDOW_TOTAL_LINES (w);
21397
21398 /* If we decided that this buffer isn't suitable for line numbers,
21399 don't forget that too fast. */
21400 if (w->base_line_pos == -1)
21401 goto no_value;
21402
21403 /* If the buffer is very big, don't waste time. */
21404 if (INTEGERP (Vline_number_display_limit)
21405 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21406 {
21407 w->base_line_pos = 0;
21408 w->base_line_number = 0;
21409 goto no_value;
21410 }
21411
21412 if (w->base_line_number > 0
21413 && w->base_line_pos > 0
21414 && w->base_line_pos <= startpos)
21415 {
21416 line = w->base_line_number;
21417 linepos = w->base_line_pos;
21418 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21419 }
21420 else
21421 {
21422 line = 1;
21423 linepos = BUF_BEGV (b);
21424 linepos_byte = BUF_BEGV_BYTE (b);
21425 }
21426
21427 /* Count lines from base line to window start position. */
21428 nlines = display_count_lines (linepos_byte,
21429 startpos_byte,
21430 startpos, &junk);
21431
21432 topline = nlines + line;
21433
21434 /* Determine a new base line, if the old one is too close
21435 or too far away, or if we did not have one.
21436 "Too close" means it's plausible a scroll-down would
21437 go back past it. */
21438 if (startpos == BUF_BEGV (b))
21439 {
21440 w->base_line_number = topline;
21441 w->base_line_pos = BUF_BEGV (b);
21442 }
21443 else if (nlines < height + 25 || nlines > height * 3 + 50
21444 || linepos == BUF_BEGV (b))
21445 {
21446 ptrdiff_t limit = BUF_BEGV (b);
21447 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21448 ptrdiff_t position;
21449 ptrdiff_t distance =
21450 (height * 2 + 30) * line_number_display_limit_width;
21451
21452 if (startpos - distance > limit)
21453 {
21454 limit = startpos - distance;
21455 limit_byte = CHAR_TO_BYTE (limit);
21456 }
21457
21458 nlines = display_count_lines (startpos_byte,
21459 limit_byte,
21460 - (height * 2 + 30),
21461 &position);
21462 /* If we couldn't find the lines we wanted within
21463 line_number_display_limit_width chars per line,
21464 give up on line numbers for this window. */
21465 if (position == limit_byte && limit == startpos - distance)
21466 {
21467 w->base_line_pos = -1;
21468 w->base_line_number = 0;
21469 goto no_value;
21470 }
21471
21472 w->base_line_number = topline - nlines;
21473 w->base_line_pos = BYTE_TO_CHAR (position);
21474 }
21475
21476 /* Now count lines from the start pos to point. */
21477 nlines = display_count_lines (startpos_byte,
21478 PT_BYTE, PT, &junk);
21479
21480 /* Record that we did display the line number. */
21481 line_number_displayed = 1;
21482
21483 /* Make the string to show. */
21484 pint2str (decode_mode_spec_buf, width, topline + nlines);
21485 return decode_mode_spec_buf;
21486 no_value:
21487 {
21488 char* p = decode_mode_spec_buf;
21489 int pad = width - 2;
21490 while (pad-- > 0)
21491 *p++ = ' ';
21492 *p++ = '?';
21493 *p++ = '?';
21494 *p = '\0';
21495 return decode_mode_spec_buf;
21496 }
21497 }
21498 break;
21499
21500 case 'm':
21501 obj = BVAR (b, mode_name);
21502 break;
21503
21504 case 'n':
21505 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21506 return " Narrow";
21507 break;
21508
21509 case 'p':
21510 {
21511 ptrdiff_t pos = marker_position (w->start);
21512 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21513
21514 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21515 {
21516 if (pos <= BUF_BEGV (b))
21517 return "All";
21518 else
21519 return "Bottom";
21520 }
21521 else if (pos <= BUF_BEGV (b))
21522 return "Top";
21523 else
21524 {
21525 if (total > 1000000)
21526 /* Do it differently for a large value, to avoid overflow. */
21527 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21528 else
21529 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21530 /* We can't normally display a 3-digit number,
21531 so get us a 2-digit number that is close. */
21532 if (total == 100)
21533 total = 99;
21534 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21535 return decode_mode_spec_buf;
21536 }
21537 }
21538
21539 /* Display percentage of size above the bottom of the screen. */
21540 case 'P':
21541 {
21542 ptrdiff_t toppos = marker_position (w->start);
21543 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21544 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21545
21546 if (botpos >= BUF_ZV (b))
21547 {
21548 if (toppos <= BUF_BEGV (b))
21549 return "All";
21550 else
21551 return "Bottom";
21552 }
21553 else
21554 {
21555 if (total > 1000000)
21556 /* Do it differently for a large value, to avoid overflow. */
21557 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21558 else
21559 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21560 /* We can't normally display a 3-digit number,
21561 so get us a 2-digit number that is close. */
21562 if (total == 100)
21563 total = 99;
21564 if (toppos <= BUF_BEGV (b))
21565 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21566 else
21567 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21568 return decode_mode_spec_buf;
21569 }
21570 }
21571
21572 case 's':
21573 /* status of process */
21574 obj = Fget_buffer_process (Fcurrent_buffer ());
21575 if (NILP (obj))
21576 return "no process";
21577 #ifndef MSDOS
21578 obj = Fsymbol_name (Fprocess_status (obj));
21579 #endif
21580 break;
21581
21582 case '@':
21583 {
21584 ptrdiff_t count = inhibit_garbage_collection ();
21585 Lisp_Object val = call1 (intern ("file-remote-p"),
21586 BVAR (current_buffer, directory));
21587 unbind_to (count, Qnil);
21588
21589 if (NILP (val))
21590 return "-";
21591 else
21592 return "@";
21593 }
21594
21595 case 'z':
21596 /* coding-system (not including end-of-line format) */
21597 case 'Z':
21598 /* coding-system (including end-of-line type) */
21599 {
21600 int eol_flag = (c == 'Z');
21601 char *p = decode_mode_spec_buf;
21602
21603 if (! FRAME_WINDOW_P (f))
21604 {
21605 /* No need to mention EOL here--the terminal never needs
21606 to do EOL conversion. */
21607 p = decode_mode_spec_coding (CODING_ID_NAME
21608 (FRAME_KEYBOARD_CODING (f)->id),
21609 p, 0);
21610 p = decode_mode_spec_coding (CODING_ID_NAME
21611 (FRAME_TERMINAL_CODING (f)->id),
21612 p, 0);
21613 }
21614 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21615 p, eol_flag);
21616
21617 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21618 #ifdef subprocesses
21619 obj = Fget_buffer_process (Fcurrent_buffer ());
21620 if (PROCESSP (obj))
21621 {
21622 p = decode_mode_spec_coding
21623 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21624 p = decode_mode_spec_coding
21625 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21626 }
21627 #endif /* subprocesses */
21628 #endif /* 0 */
21629 *p = 0;
21630 return decode_mode_spec_buf;
21631 }
21632 }
21633
21634 if (STRINGP (obj))
21635 {
21636 *string = obj;
21637 return SSDATA (obj);
21638 }
21639 else
21640 return "";
21641 }
21642
21643
21644 /* Count up to COUNT lines starting from START_BYTE. COUNT negative
21645 means count lines back from START_BYTE. But don't go beyond
21646 LIMIT_BYTE. Return the number of lines thus found (always
21647 nonnegative).
21648
21649 Set *BYTE_POS_PTR to the byte position where we stopped. This is
21650 either the position COUNT lines after/before START_BYTE, if we
21651 found COUNT lines, or LIMIT_BYTE if we hit the limit before finding
21652 COUNT lines. */
21653
21654 static ptrdiff_t
21655 display_count_lines (ptrdiff_t start_byte,
21656 ptrdiff_t limit_byte, ptrdiff_t count,
21657 ptrdiff_t *byte_pos_ptr)
21658 {
21659 register unsigned char *cursor;
21660 unsigned char *base;
21661
21662 register ptrdiff_t ceiling;
21663 register unsigned char *ceiling_addr;
21664 ptrdiff_t orig_count = count;
21665
21666 /* If we are not in selective display mode,
21667 check only for newlines. */
21668 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21669 && !INTEGERP (BVAR (current_buffer, selective_display)));
21670
21671 if (count > 0)
21672 {
21673 while (start_byte < limit_byte)
21674 {
21675 ceiling = BUFFER_CEILING_OF (start_byte);
21676 ceiling = min (limit_byte - 1, ceiling);
21677 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21678 base = (cursor = BYTE_POS_ADDR (start_byte));
21679
21680 do
21681 {
21682 if (selective_display)
21683 {
21684 while (*cursor != '\n' && *cursor != 015
21685 && ++cursor != ceiling_addr)
21686 continue;
21687 if (cursor == ceiling_addr)
21688 break;
21689 }
21690 else
21691 {
21692 cursor = memchr (cursor, '\n', ceiling_addr - cursor);
21693 if (! cursor)
21694 break;
21695 }
21696
21697 cursor++;
21698
21699 if (--count == 0)
21700 {
21701 start_byte += cursor - base;
21702 *byte_pos_ptr = start_byte;
21703 return orig_count;
21704 }
21705 }
21706 while (cursor < ceiling_addr);
21707
21708 start_byte += ceiling_addr - base;
21709 }
21710 }
21711 else
21712 {
21713 while (start_byte > limit_byte)
21714 {
21715 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21716 ceiling = max (limit_byte, ceiling);
21717 ceiling_addr = BYTE_POS_ADDR (ceiling);
21718 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21719 while (1)
21720 {
21721 if (selective_display)
21722 {
21723 while (--cursor >= ceiling_addr
21724 && *cursor != '\n' && *cursor != 015)
21725 continue;
21726 if (cursor < ceiling_addr)
21727 break;
21728 }
21729 else
21730 {
21731 cursor = memrchr (ceiling_addr, '\n', cursor - ceiling_addr);
21732 if (! cursor)
21733 break;
21734 }
21735
21736 if (++count == 0)
21737 {
21738 start_byte += cursor - base + 1;
21739 *byte_pos_ptr = start_byte;
21740 /* When scanning backwards, we should
21741 not count the newline posterior to which we stop. */
21742 return - orig_count - 1;
21743 }
21744 }
21745 start_byte += ceiling_addr - base;
21746 }
21747 }
21748
21749 *byte_pos_ptr = limit_byte;
21750
21751 if (count < 0)
21752 return - orig_count + count;
21753 return orig_count - count;
21754
21755 }
21756
21757
21758 \f
21759 /***********************************************************************
21760 Displaying strings
21761 ***********************************************************************/
21762
21763 /* Display a NUL-terminated string, starting with index START.
21764
21765 If STRING is non-null, display that C string. Otherwise, the Lisp
21766 string LISP_STRING is displayed. There's a case that STRING is
21767 non-null and LISP_STRING is not nil. It means STRING is a string
21768 data of LISP_STRING. In that case, we display LISP_STRING while
21769 ignoring its text properties.
21770
21771 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21772 FACE_STRING. Display STRING or LISP_STRING with the face at
21773 FACE_STRING_POS in FACE_STRING:
21774
21775 Display the string in the environment given by IT, but use the
21776 standard display table, temporarily.
21777
21778 FIELD_WIDTH is the minimum number of output glyphs to produce.
21779 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21780 with spaces. If STRING has more characters, more than FIELD_WIDTH
21781 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21782
21783 PRECISION is the maximum number of characters to output from
21784 STRING. PRECISION < 0 means don't truncate the string.
21785
21786 This is roughly equivalent to printf format specifiers:
21787
21788 FIELD_WIDTH PRECISION PRINTF
21789 ----------------------------------------
21790 -1 -1 %s
21791 -1 10 %.10s
21792 10 -1 %10s
21793 20 10 %20.10s
21794
21795 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21796 display them, and < 0 means obey the current buffer's value of
21797 enable_multibyte_characters.
21798
21799 Value is the number of columns displayed. */
21800
21801 static int
21802 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21803 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21804 int field_width, int precision, int max_x, int multibyte)
21805 {
21806 int hpos_at_start = it->hpos;
21807 int saved_face_id = it->face_id;
21808 struct glyph_row *row = it->glyph_row;
21809 ptrdiff_t it_charpos;
21810
21811 /* Initialize the iterator IT for iteration over STRING beginning
21812 with index START. */
21813 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21814 precision, field_width, multibyte);
21815 if (string && STRINGP (lisp_string))
21816 /* LISP_STRING is the one returned by decode_mode_spec. We should
21817 ignore its text properties. */
21818 it->stop_charpos = it->end_charpos;
21819
21820 /* If displaying STRING, set up the face of the iterator from
21821 FACE_STRING, if that's given. */
21822 if (STRINGP (face_string))
21823 {
21824 ptrdiff_t endptr;
21825 struct face *face;
21826
21827 it->face_id
21828 = face_at_string_position (it->w, face_string, face_string_pos,
21829 0, it->region_beg_charpos,
21830 it->region_end_charpos,
21831 &endptr, it->base_face_id, 0);
21832 face = FACE_FROM_ID (it->f, it->face_id);
21833 it->face_box_p = face->box != FACE_NO_BOX;
21834 }
21835
21836 /* Set max_x to the maximum allowed X position. Don't let it go
21837 beyond the right edge of the window. */
21838 if (max_x <= 0)
21839 max_x = it->last_visible_x;
21840 else
21841 max_x = min (max_x, it->last_visible_x);
21842
21843 /* Skip over display elements that are not visible. because IT->w is
21844 hscrolled. */
21845 if (it->current_x < it->first_visible_x)
21846 move_it_in_display_line_to (it, 100000, it->first_visible_x,
21847 MOVE_TO_POS | MOVE_TO_X);
21848
21849 row->ascent = it->max_ascent;
21850 row->height = it->max_ascent + it->max_descent;
21851 row->phys_ascent = it->max_phys_ascent;
21852 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
21853 row->extra_line_spacing = it->max_extra_line_spacing;
21854
21855 if (STRINGP (it->string))
21856 it_charpos = IT_STRING_CHARPOS (*it);
21857 else
21858 it_charpos = IT_CHARPOS (*it);
21859
21860 /* This condition is for the case that we are called with current_x
21861 past last_visible_x. */
21862 while (it->current_x < max_x)
21863 {
21864 int x_before, x, n_glyphs_before, i, nglyphs;
21865
21866 /* Get the next display element. */
21867 if (!get_next_display_element (it))
21868 break;
21869
21870 /* Produce glyphs. */
21871 x_before = it->current_x;
21872 n_glyphs_before = row->used[TEXT_AREA];
21873 PRODUCE_GLYPHS (it);
21874
21875 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
21876 i = 0;
21877 x = x_before;
21878 while (i < nglyphs)
21879 {
21880 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
21881
21882 if (it->line_wrap != TRUNCATE
21883 && x + glyph->pixel_width > max_x)
21884 {
21885 /* End of continued line or max_x reached. */
21886 if (CHAR_GLYPH_PADDING_P (*glyph))
21887 {
21888 /* A wide character is unbreakable. */
21889 if (row->reversed_p)
21890 unproduce_glyphs (it, row->used[TEXT_AREA]
21891 - n_glyphs_before);
21892 row->used[TEXT_AREA] = n_glyphs_before;
21893 it->current_x = x_before;
21894 }
21895 else
21896 {
21897 if (row->reversed_p)
21898 unproduce_glyphs (it, row->used[TEXT_AREA]
21899 - (n_glyphs_before + i));
21900 row->used[TEXT_AREA] = n_glyphs_before + i;
21901 it->current_x = x;
21902 }
21903 break;
21904 }
21905 else if (x + glyph->pixel_width >= it->first_visible_x)
21906 {
21907 /* Glyph is at least partially visible. */
21908 ++it->hpos;
21909 if (x < it->first_visible_x)
21910 row->x = x - it->first_visible_x;
21911 }
21912 else
21913 {
21914 /* Glyph is off the left margin of the display area.
21915 Should not happen. */
21916 emacs_abort ();
21917 }
21918
21919 row->ascent = max (row->ascent, it->max_ascent);
21920 row->height = max (row->height, it->max_ascent + it->max_descent);
21921 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
21922 row->phys_height = max (row->phys_height,
21923 it->max_phys_ascent + it->max_phys_descent);
21924 row->extra_line_spacing = max (row->extra_line_spacing,
21925 it->max_extra_line_spacing);
21926 x += glyph->pixel_width;
21927 ++i;
21928 }
21929
21930 /* Stop if max_x reached. */
21931 if (i < nglyphs)
21932 break;
21933
21934 /* Stop at line ends. */
21935 if (ITERATOR_AT_END_OF_LINE_P (it))
21936 {
21937 it->continuation_lines_width = 0;
21938 break;
21939 }
21940
21941 set_iterator_to_next (it, 1);
21942 if (STRINGP (it->string))
21943 it_charpos = IT_STRING_CHARPOS (*it);
21944 else
21945 it_charpos = IT_CHARPOS (*it);
21946
21947 /* Stop if truncating at the right edge. */
21948 if (it->line_wrap == TRUNCATE
21949 && it->current_x >= it->last_visible_x)
21950 {
21951 /* Add truncation mark, but don't do it if the line is
21952 truncated at a padding space. */
21953 if (it_charpos < it->string_nchars)
21954 {
21955 if (!FRAME_WINDOW_P (it->f))
21956 {
21957 int ii, n;
21958
21959 if (it->current_x > it->last_visible_x)
21960 {
21961 if (!row->reversed_p)
21962 {
21963 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
21964 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21965 break;
21966 }
21967 else
21968 {
21969 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
21970 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
21971 break;
21972 unproduce_glyphs (it, ii + 1);
21973 ii = row->used[TEXT_AREA] - (ii + 1);
21974 }
21975 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
21976 {
21977 row->used[TEXT_AREA] = ii;
21978 produce_special_glyphs (it, IT_TRUNCATION);
21979 }
21980 }
21981 produce_special_glyphs (it, IT_TRUNCATION);
21982 }
21983 row->truncated_on_right_p = 1;
21984 }
21985 break;
21986 }
21987 }
21988
21989 /* Maybe insert a truncation at the left. */
21990 if (it->first_visible_x
21991 && it_charpos > 0)
21992 {
21993 if (!FRAME_WINDOW_P (it->f)
21994 || (row->reversed_p
21995 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
21996 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
21997 insert_left_trunc_glyphs (it);
21998 row->truncated_on_left_p = 1;
21999 }
22000
22001 it->face_id = saved_face_id;
22002
22003 /* Value is number of columns displayed. */
22004 return it->hpos - hpos_at_start;
22005 }
22006
22007
22008 \f
22009 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22010 appears as an element of LIST or as the car of an element of LIST.
22011 If PROPVAL is a list, compare each element against LIST in that
22012 way, and return 1/2 if any element of PROPVAL is found in LIST.
22013 Otherwise return 0. This function cannot quit.
22014 The return value is 2 if the text is invisible but with an ellipsis
22015 and 1 if it's invisible and without an ellipsis. */
22016
22017 int
22018 invisible_p (register Lisp_Object propval, Lisp_Object list)
22019 {
22020 register Lisp_Object tail, proptail;
22021
22022 for (tail = list; CONSP (tail); tail = XCDR (tail))
22023 {
22024 register Lisp_Object tem;
22025 tem = XCAR (tail);
22026 if (EQ (propval, tem))
22027 return 1;
22028 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22029 return NILP (XCDR (tem)) ? 1 : 2;
22030 }
22031
22032 if (CONSP (propval))
22033 {
22034 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22035 {
22036 Lisp_Object propelt;
22037 propelt = XCAR (proptail);
22038 for (tail = list; CONSP (tail); tail = XCDR (tail))
22039 {
22040 register Lisp_Object tem;
22041 tem = XCAR (tail);
22042 if (EQ (propelt, tem))
22043 return 1;
22044 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22045 return NILP (XCDR (tem)) ? 1 : 2;
22046 }
22047 }
22048 }
22049
22050 return 0;
22051 }
22052
22053 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22054 doc: /* Non-nil if the property makes the text invisible.
22055 POS-OR-PROP can be a marker or number, in which case it is taken to be
22056 a position in the current buffer and the value of the `invisible' property
22057 is checked; or it can be some other value, which is then presumed to be the
22058 value of the `invisible' property of the text of interest.
22059 The non-nil value returned can be t for truly invisible text or something
22060 else if the text is replaced by an ellipsis. */)
22061 (Lisp_Object pos_or_prop)
22062 {
22063 Lisp_Object prop
22064 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22065 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22066 : pos_or_prop);
22067 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22068 return (invis == 0 ? Qnil
22069 : invis == 1 ? Qt
22070 : make_number (invis));
22071 }
22072
22073 /* Calculate a width or height in pixels from a specification using
22074 the following elements:
22075
22076 SPEC ::=
22077 NUM - a (fractional) multiple of the default font width/height
22078 (NUM) - specifies exactly NUM pixels
22079 UNIT - a fixed number of pixels, see below.
22080 ELEMENT - size of a display element in pixels, see below.
22081 (NUM . SPEC) - equals NUM * SPEC
22082 (+ SPEC SPEC ...) - add pixel values
22083 (- SPEC SPEC ...) - subtract pixel values
22084 (- SPEC) - negate pixel value
22085
22086 NUM ::=
22087 INT or FLOAT - a number constant
22088 SYMBOL - use symbol's (buffer local) variable binding.
22089
22090 UNIT ::=
22091 in - pixels per inch *)
22092 mm - pixels per 1/1000 meter *)
22093 cm - pixels per 1/100 meter *)
22094 width - width of current font in pixels.
22095 height - height of current font in pixels.
22096
22097 *) using the ratio(s) defined in display-pixels-per-inch.
22098
22099 ELEMENT ::=
22100
22101 left-fringe - left fringe width in pixels
22102 right-fringe - right fringe width in pixels
22103
22104 left-margin - left margin width in pixels
22105 right-margin - right margin width in pixels
22106
22107 scroll-bar - scroll-bar area width in pixels
22108
22109 Examples:
22110
22111 Pixels corresponding to 5 inches:
22112 (5 . in)
22113
22114 Total width of non-text areas on left side of window (if scroll-bar is on left):
22115 '(space :width (+ left-fringe left-margin scroll-bar))
22116
22117 Align to first text column (in header line):
22118 '(space :align-to 0)
22119
22120 Align to middle of text area minus half the width of variable `my-image'
22121 containing a loaded image:
22122 '(space :align-to (0.5 . (- text my-image)))
22123
22124 Width of left margin minus width of 1 character in the default font:
22125 '(space :width (- left-margin 1))
22126
22127 Width of left margin minus width of 2 characters in the current font:
22128 '(space :width (- left-margin (2 . width)))
22129
22130 Center 1 character over left-margin (in header line):
22131 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22132
22133 Different ways to express width of left fringe plus left margin minus one pixel:
22134 '(space :width (- (+ left-fringe left-margin) (1)))
22135 '(space :width (+ left-fringe left-margin (- (1))))
22136 '(space :width (+ left-fringe left-margin (-1)))
22137
22138 */
22139
22140 static int
22141 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22142 struct font *font, int width_p, int *align_to)
22143 {
22144 double pixels;
22145
22146 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22147 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22148
22149 if (NILP (prop))
22150 return OK_PIXELS (0);
22151
22152 eassert (FRAME_LIVE_P (it->f));
22153
22154 if (SYMBOLP (prop))
22155 {
22156 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22157 {
22158 char *unit = SSDATA (SYMBOL_NAME (prop));
22159
22160 if (unit[0] == 'i' && unit[1] == 'n')
22161 pixels = 1.0;
22162 else if (unit[0] == 'm' && unit[1] == 'm')
22163 pixels = 25.4;
22164 else if (unit[0] == 'c' && unit[1] == 'm')
22165 pixels = 2.54;
22166 else
22167 pixels = 0;
22168 if (pixels > 0)
22169 {
22170 double ppi = (width_p ? FRAME_RES_X (it->f)
22171 : FRAME_RES_Y (it->f));
22172
22173 if (ppi > 0)
22174 return OK_PIXELS (ppi / pixels);
22175 return 0;
22176 }
22177 }
22178
22179 #ifdef HAVE_WINDOW_SYSTEM
22180 if (EQ (prop, Qheight))
22181 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22182 if (EQ (prop, Qwidth))
22183 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22184 #else
22185 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22186 return OK_PIXELS (1);
22187 #endif
22188
22189 if (EQ (prop, Qtext))
22190 return OK_PIXELS (width_p
22191 ? window_box_width (it->w, TEXT_AREA)
22192 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22193
22194 if (align_to && *align_to < 0)
22195 {
22196 *res = 0;
22197 if (EQ (prop, Qleft))
22198 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22199 if (EQ (prop, Qright))
22200 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22201 if (EQ (prop, Qcenter))
22202 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22203 + window_box_width (it->w, TEXT_AREA) / 2);
22204 if (EQ (prop, Qleft_fringe))
22205 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22206 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22207 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22208 if (EQ (prop, Qright_fringe))
22209 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22210 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22211 : window_box_right_offset (it->w, TEXT_AREA));
22212 if (EQ (prop, Qleft_margin))
22213 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22214 if (EQ (prop, Qright_margin))
22215 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22216 if (EQ (prop, Qscroll_bar))
22217 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22218 ? 0
22219 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22220 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22221 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22222 : 0)));
22223 }
22224 else
22225 {
22226 if (EQ (prop, Qleft_fringe))
22227 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22228 if (EQ (prop, Qright_fringe))
22229 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22230 if (EQ (prop, Qleft_margin))
22231 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22232 if (EQ (prop, Qright_margin))
22233 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22234 if (EQ (prop, Qscroll_bar))
22235 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22236 }
22237
22238 prop = buffer_local_value_1 (prop, it->w->contents);
22239 if (EQ (prop, Qunbound))
22240 prop = Qnil;
22241 }
22242
22243 if (INTEGERP (prop) || FLOATP (prop))
22244 {
22245 int base_unit = (width_p
22246 ? FRAME_COLUMN_WIDTH (it->f)
22247 : FRAME_LINE_HEIGHT (it->f));
22248 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22249 }
22250
22251 if (CONSP (prop))
22252 {
22253 Lisp_Object car = XCAR (prop);
22254 Lisp_Object cdr = XCDR (prop);
22255
22256 if (SYMBOLP (car))
22257 {
22258 #ifdef HAVE_WINDOW_SYSTEM
22259 if (FRAME_WINDOW_P (it->f)
22260 && valid_image_p (prop))
22261 {
22262 ptrdiff_t id = lookup_image (it->f, prop);
22263 struct image *img = IMAGE_FROM_ID (it->f, id);
22264
22265 return OK_PIXELS (width_p ? img->width : img->height);
22266 }
22267 #endif
22268 if (EQ (car, Qplus) || EQ (car, Qminus))
22269 {
22270 int first = 1;
22271 double px;
22272
22273 pixels = 0;
22274 while (CONSP (cdr))
22275 {
22276 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22277 font, width_p, align_to))
22278 return 0;
22279 if (first)
22280 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22281 else
22282 pixels += px;
22283 cdr = XCDR (cdr);
22284 }
22285 if (EQ (car, Qminus))
22286 pixels = -pixels;
22287 return OK_PIXELS (pixels);
22288 }
22289
22290 car = buffer_local_value_1 (car, it->w->contents);
22291 if (EQ (car, Qunbound))
22292 car = Qnil;
22293 }
22294
22295 if (INTEGERP (car) || FLOATP (car))
22296 {
22297 double fact;
22298 pixels = XFLOATINT (car);
22299 if (NILP (cdr))
22300 return OK_PIXELS (pixels);
22301 if (calc_pixel_width_or_height (&fact, it, cdr,
22302 font, width_p, align_to))
22303 return OK_PIXELS (pixels * fact);
22304 return 0;
22305 }
22306
22307 return 0;
22308 }
22309
22310 return 0;
22311 }
22312
22313 \f
22314 /***********************************************************************
22315 Glyph Display
22316 ***********************************************************************/
22317
22318 #ifdef HAVE_WINDOW_SYSTEM
22319
22320 #ifdef GLYPH_DEBUG
22321
22322 void
22323 dump_glyph_string (struct glyph_string *s)
22324 {
22325 fprintf (stderr, "glyph string\n");
22326 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22327 s->x, s->y, s->width, s->height);
22328 fprintf (stderr, " ybase = %d\n", s->ybase);
22329 fprintf (stderr, " hl = %d\n", s->hl);
22330 fprintf (stderr, " left overhang = %d, right = %d\n",
22331 s->left_overhang, s->right_overhang);
22332 fprintf (stderr, " nchars = %d\n", s->nchars);
22333 fprintf (stderr, " extends to end of line = %d\n",
22334 s->extends_to_end_of_line_p);
22335 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22336 fprintf (stderr, " bg width = %d\n", s->background_width);
22337 }
22338
22339 #endif /* GLYPH_DEBUG */
22340
22341 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22342 of XChar2b structures for S; it can't be allocated in
22343 init_glyph_string because it must be allocated via `alloca'. W
22344 is the window on which S is drawn. ROW and AREA are the glyph row
22345 and area within the row from which S is constructed. START is the
22346 index of the first glyph structure covered by S. HL is a
22347 face-override for drawing S. */
22348
22349 #ifdef HAVE_NTGUI
22350 #define OPTIONAL_HDC(hdc) HDC hdc,
22351 #define DECLARE_HDC(hdc) HDC hdc;
22352 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22353 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22354 #endif
22355
22356 #ifndef OPTIONAL_HDC
22357 #define OPTIONAL_HDC(hdc)
22358 #define DECLARE_HDC(hdc)
22359 #define ALLOCATE_HDC(hdc, f)
22360 #define RELEASE_HDC(hdc, f)
22361 #endif
22362
22363 static void
22364 init_glyph_string (struct glyph_string *s,
22365 OPTIONAL_HDC (hdc)
22366 XChar2b *char2b, struct window *w, struct glyph_row *row,
22367 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22368 {
22369 memset (s, 0, sizeof *s);
22370 s->w = w;
22371 s->f = XFRAME (w->frame);
22372 #ifdef HAVE_NTGUI
22373 s->hdc = hdc;
22374 #endif
22375 s->display = FRAME_X_DISPLAY (s->f);
22376 s->window = FRAME_X_WINDOW (s->f);
22377 s->char2b = char2b;
22378 s->hl = hl;
22379 s->row = row;
22380 s->area = area;
22381 s->first_glyph = row->glyphs[area] + start;
22382 s->height = row->height;
22383 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22384 s->ybase = s->y + row->ascent;
22385 }
22386
22387
22388 /* Append the list of glyph strings with head H and tail T to the list
22389 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22390
22391 static void
22392 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22393 struct glyph_string *h, struct glyph_string *t)
22394 {
22395 if (h)
22396 {
22397 if (*head)
22398 (*tail)->next = h;
22399 else
22400 *head = h;
22401 h->prev = *tail;
22402 *tail = t;
22403 }
22404 }
22405
22406
22407 /* Prepend the list of glyph strings with head H and tail T to the
22408 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22409 result. */
22410
22411 static void
22412 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22413 struct glyph_string *h, struct glyph_string *t)
22414 {
22415 if (h)
22416 {
22417 if (*head)
22418 (*head)->prev = t;
22419 else
22420 *tail = t;
22421 t->next = *head;
22422 *head = h;
22423 }
22424 }
22425
22426
22427 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22428 Set *HEAD and *TAIL to the resulting list. */
22429
22430 static void
22431 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22432 struct glyph_string *s)
22433 {
22434 s->next = s->prev = NULL;
22435 append_glyph_string_lists (head, tail, s, s);
22436 }
22437
22438
22439 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22440 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22441 make sure that X resources for the face returned are allocated.
22442 Value is a pointer to a realized face that is ready for display if
22443 DISPLAY_P is non-zero. */
22444
22445 static struct face *
22446 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22447 XChar2b *char2b, int display_p)
22448 {
22449 struct face *face = FACE_FROM_ID (f, face_id);
22450 unsigned code = 0;
22451
22452 if (face->font)
22453 {
22454 code = face->font->driver->encode_char (face->font, c);
22455
22456 if (code == FONT_INVALID_CODE)
22457 code = 0;
22458 }
22459 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22460
22461 /* Make sure X resources of the face are allocated. */
22462 #ifdef HAVE_X_WINDOWS
22463 if (display_p)
22464 #endif
22465 {
22466 eassert (face != NULL);
22467 PREPARE_FACE_FOR_DISPLAY (f, face);
22468 }
22469
22470 return face;
22471 }
22472
22473
22474 /* Get face and two-byte form of character glyph GLYPH on frame F.
22475 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22476 a pointer to a realized face that is ready for display. */
22477
22478 static struct face *
22479 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22480 XChar2b *char2b, int *two_byte_p)
22481 {
22482 struct face *face;
22483 unsigned code = 0;
22484
22485 eassert (glyph->type == CHAR_GLYPH);
22486 face = FACE_FROM_ID (f, glyph->face_id);
22487
22488 /* Make sure X resources of the face are allocated. */
22489 eassert (face != NULL);
22490 PREPARE_FACE_FOR_DISPLAY (f, face);
22491
22492 if (two_byte_p)
22493 *two_byte_p = 0;
22494
22495 if (face->font)
22496 {
22497 if (CHAR_BYTE8_P (glyph->u.ch))
22498 code = CHAR_TO_BYTE8 (glyph->u.ch);
22499 else
22500 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22501
22502 if (code == FONT_INVALID_CODE)
22503 code = 0;
22504 }
22505
22506 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22507 return face;
22508 }
22509
22510
22511 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22512 Return 1 if FONT has a glyph for C, otherwise return 0. */
22513
22514 static int
22515 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22516 {
22517 unsigned code;
22518
22519 if (CHAR_BYTE8_P (c))
22520 code = CHAR_TO_BYTE8 (c);
22521 else
22522 code = font->driver->encode_char (font, c);
22523
22524 if (code == FONT_INVALID_CODE)
22525 return 0;
22526 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22527 return 1;
22528 }
22529
22530
22531 /* Fill glyph string S with composition components specified by S->cmp.
22532
22533 BASE_FACE is the base face of the composition.
22534 S->cmp_from is the index of the first component for S.
22535
22536 OVERLAPS non-zero means S should draw the foreground only, and use
22537 its physical height for clipping. See also draw_glyphs.
22538
22539 Value is the index of a component not in S. */
22540
22541 static int
22542 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22543 int overlaps)
22544 {
22545 int i;
22546 /* For all glyphs of this composition, starting at the offset
22547 S->cmp_from, until we reach the end of the definition or encounter a
22548 glyph that requires the different face, add it to S. */
22549 struct face *face;
22550
22551 eassert (s);
22552
22553 s->for_overlaps = overlaps;
22554 s->face = NULL;
22555 s->font = NULL;
22556 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22557 {
22558 int c = COMPOSITION_GLYPH (s->cmp, i);
22559
22560 /* TAB in a composition means display glyphs with padding space
22561 on the left or right. */
22562 if (c != '\t')
22563 {
22564 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22565 -1, Qnil);
22566
22567 face = get_char_face_and_encoding (s->f, c, face_id,
22568 s->char2b + i, 1);
22569 if (face)
22570 {
22571 if (! s->face)
22572 {
22573 s->face = face;
22574 s->font = s->face->font;
22575 }
22576 else if (s->face != face)
22577 break;
22578 }
22579 }
22580 ++s->nchars;
22581 }
22582 s->cmp_to = i;
22583
22584 if (s->face == NULL)
22585 {
22586 s->face = base_face->ascii_face;
22587 s->font = s->face->font;
22588 }
22589
22590 /* All glyph strings for the same composition has the same width,
22591 i.e. the width set for the first component of the composition. */
22592 s->width = s->first_glyph->pixel_width;
22593
22594 /* If the specified font could not be loaded, use the frame's
22595 default font, but record the fact that we couldn't load it in
22596 the glyph string so that we can draw rectangles for the
22597 characters of the glyph string. */
22598 if (s->font == NULL)
22599 {
22600 s->font_not_found_p = 1;
22601 s->font = FRAME_FONT (s->f);
22602 }
22603
22604 /* Adjust base line for subscript/superscript text. */
22605 s->ybase += s->first_glyph->voffset;
22606
22607 /* This glyph string must always be drawn with 16-bit functions. */
22608 s->two_byte_p = 1;
22609
22610 return s->cmp_to;
22611 }
22612
22613 static int
22614 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22615 int start, int end, int overlaps)
22616 {
22617 struct glyph *glyph, *last;
22618 Lisp_Object lgstring;
22619 int i;
22620
22621 s->for_overlaps = overlaps;
22622 glyph = s->row->glyphs[s->area] + start;
22623 last = s->row->glyphs[s->area] + end;
22624 s->cmp_id = glyph->u.cmp.id;
22625 s->cmp_from = glyph->slice.cmp.from;
22626 s->cmp_to = glyph->slice.cmp.to + 1;
22627 s->face = FACE_FROM_ID (s->f, face_id);
22628 lgstring = composition_gstring_from_id (s->cmp_id);
22629 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22630 glyph++;
22631 while (glyph < last
22632 && glyph->u.cmp.automatic
22633 && glyph->u.cmp.id == s->cmp_id
22634 && s->cmp_to == glyph->slice.cmp.from)
22635 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22636
22637 for (i = s->cmp_from; i < s->cmp_to; i++)
22638 {
22639 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22640 unsigned code = LGLYPH_CODE (lglyph);
22641
22642 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22643 }
22644 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22645 return glyph - s->row->glyphs[s->area];
22646 }
22647
22648
22649 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22650 See the comment of fill_glyph_string for arguments.
22651 Value is the index of the first glyph not in S. */
22652
22653
22654 static int
22655 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22656 int start, int end, int overlaps)
22657 {
22658 struct glyph *glyph, *last;
22659 int voffset;
22660
22661 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22662 s->for_overlaps = overlaps;
22663 glyph = s->row->glyphs[s->area] + start;
22664 last = s->row->glyphs[s->area] + end;
22665 voffset = glyph->voffset;
22666 s->face = FACE_FROM_ID (s->f, face_id);
22667 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22668 s->nchars = 1;
22669 s->width = glyph->pixel_width;
22670 glyph++;
22671 while (glyph < last
22672 && glyph->type == GLYPHLESS_GLYPH
22673 && glyph->voffset == voffset
22674 && glyph->face_id == face_id)
22675 {
22676 s->nchars++;
22677 s->width += glyph->pixel_width;
22678 glyph++;
22679 }
22680 s->ybase += voffset;
22681 return glyph - s->row->glyphs[s->area];
22682 }
22683
22684
22685 /* Fill glyph string S from a sequence of character glyphs.
22686
22687 FACE_ID is the face id of the string. START is the index of the
22688 first glyph to consider, END is the index of the last + 1.
22689 OVERLAPS non-zero means S should draw the foreground only, and use
22690 its physical height for clipping. See also draw_glyphs.
22691
22692 Value is the index of the first glyph not in S. */
22693
22694 static int
22695 fill_glyph_string (struct glyph_string *s, int face_id,
22696 int start, int end, int overlaps)
22697 {
22698 struct glyph *glyph, *last;
22699 int voffset;
22700 int glyph_not_available_p;
22701
22702 eassert (s->f == XFRAME (s->w->frame));
22703 eassert (s->nchars == 0);
22704 eassert (start >= 0 && end > start);
22705
22706 s->for_overlaps = overlaps;
22707 glyph = s->row->glyphs[s->area] + start;
22708 last = s->row->glyphs[s->area] + end;
22709 voffset = glyph->voffset;
22710 s->padding_p = glyph->padding_p;
22711 glyph_not_available_p = glyph->glyph_not_available_p;
22712
22713 while (glyph < last
22714 && glyph->type == CHAR_GLYPH
22715 && glyph->voffset == voffset
22716 /* Same face id implies same font, nowadays. */
22717 && glyph->face_id == face_id
22718 && glyph->glyph_not_available_p == glyph_not_available_p)
22719 {
22720 int two_byte_p;
22721
22722 s->face = get_glyph_face_and_encoding (s->f, glyph,
22723 s->char2b + s->nchars,
22724 &two_byte_p);
22725 s->two_byte_p = two_byte_p;
22726 ++s->nchars;
22727 eassert (s->nchars <= end - start);
22728 s->width += glyph->pixel_width;
22729 if (glyph++->padding_p != s->padding_p)
22730 break;
22731 }
22732
22733 s->font = s->face->font;
22734
22735 /* If the specified font could not be loaded, use the frame's font,
22736 but record the fact that we couldn't load it in
22737 S->font_not_found_p so that we can draw rectangles for the
22738 characters of the glyph string. */
22739 if (s->font == NULL || glyph_not_available_p)
22740 {
22741 s->font_not_found_p = 1;
22742 s->font = FRAME_FONT (s->f);
22743 }
22744
22745 /* Adjust base line for subscript/superscript text. */
22746 s->ybase += voffset;
22747
22748 eassert (s->face && s->face->gc);
22749 return glyph - s->row->glyphs[s->area];
22750 }
22751
22752
22753 /* Fill glyph string S from image glyph S->first_glyph. */
22754
22755 static void
22756 fill_image_glyph_string (struct glyph_string *s)
22757 {
22758 eassert (s->first_glyph->type == IMAGE_GLYPH);
22759 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22760 eassert (s->img);
22761 s->slice = s->first_glyph->slice.img;
22762 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22763 s->font = s->face->font;
22764 s->width = s->first_glyph->pixel_width;
22765
22766 /* Adjust base line for subscript/superscript text. */
22767 s->ybase += s->first_glyph->voffset;
22768 }
22769
22770
22771 /* Fill glyph string S from a sequence of stretch glyphs.
22772
22773 START is the index of the first glyph to consider,
22774 END is the index of the last + 1.
22775
22776 Value is the index of the first glyph not in S. */
22777
22778 static int
22779 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22780 {
22781 struct glyph *glyph, *last;
22782 int voffset, face_id;
22783
22784 eassert (s->first_glyph->type == STRETCH_GLYPH);
22785
22786 glyph = s->row->glyphs[s->area] + start;
22787 last = s->row->glyphs[s->area] + end;
22788 face_id = glyph->face_id;
22789 s->face = FACE_FROM_ID (s->f, face_id);
22790 s->font = s->face->font;
22791 s->width = glyph->pixel_width;
22792 s->nchars = 1;
22793 voffset = glyph->voffset;
22794
22795 for (++glyph;
22796 (glyph < last
22797 && glyph->type == STRETCH_GLYPH
22798 && glyph->voffset == voffset
22799 && glyph->face_id == face_id);
22800 ++glyph)
22801 s->width += glyph->pixel_width;
22802
22803 /* Adjust base line for subscript/superscript text. */
22804 s->ybase += voffset;
22805
22806 /* The case that face->gc == 0 is handled when drawing the glyph
22807 string by calling PREPARE_FACE_FOR_DISPLAY. */
22808 eassert (s->face);
22809 return glyph - s->row->glyphs[s->area];
22810 }
22811
22812 static struct font_metrics *
22813 get_per_char_metric (struct font *font, XChar2b *char2b)
22814 {
22815 static struct font_metrics metrics;
22816 unsigned code;
22817
22818 if (! font)
22819 return NULL;
22820 code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22821 if (code == FONT_INVALID_CODE)
22822 return NULL;
22823 font->driver->text_extents (font, &code, 1, &metrics);
22824 return &metrics;
22825 }
22826
22827 /* EXPORT for RIF:
22828 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
22829 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
22830 assumed to be zero. */
22831
22832 void
22833 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
22834 {
22835 *left = *right = 0;
22836
22837 if (glyph->type == CHAR_GLYPH)
22838 {
22839 struct face *face;
22840 XChar2b char2b;
22841 struct font_metrics *pcm;
22842
22843 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
22844 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
22845 {
22846 if (pcm->rbearing > pcm->width)
22847 *right = pcm->rbearing - pcm->width;
22848 if (pcm->lbearing < 0)
22849 *left = -pcm->lbearing;
22850 }
22851 }
22852 else if (glyph->type == COMPOSITE_GLYPH)
22853 {
22854 if (! glyph->u.cmp.automatic)
22855 {
22856 struct composition *cmp = composition_table[glyph->u.cmp.id];
22857
22858 if (cmp->rbearing > cmp->pixel_width)
22859 *right = cmp->rbearing - cmp->pixel_width;
22860 if (cmp->lbearing < 0)
22861 *left = - cmp->lbearing;
22862 }
22863 else
22864 {
22865 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
22866 struct font_metrics metrics;
22867
22868 composition_gstring_width (gstring, glyph->slice.cmp.from,
22869 glyph->slice.cmp.to + 1, &metrics);
22870 if (metrics.rbearing > metrics.width)
22871 *right = metrics.rbearing - metrics.width;
22872 if (metrics.lbearing < 0)
22873 *left = - metrics.lbearing;
22874 }
22875 }
22876 }
22877
22878
22879 /* Return the index of the first glyph preceding glyph string S that
22880 is overwritten by S because of S's left overhang. Value is -1
22881 if no glyphs are overwritten. */
22882
22883 static int
22884 left_overwritten (struct glyph_string *s)
22885 {
22886 int k;
22887
22888 if (s->left_overhang)
22889 {
22890 int x = 0, i;
22891 struct glyph *glyphs = s->row->glyphs[s->area];
22892 int first = s->first_glyph - glyphs;
22893
22894 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
22895 x -= glyphs[i].pixel_width;
22896
22897 k = i + 1;
22898 }
22899 else
22900 k = -1;
22901
22902 return k;
22903 }
22904
22905
22906 /* Return the index of the first glyph preceding glyph string S that
22907 is overwriting S because of its right overhang. Value is -1 if no
22908 glyph in front of S overwrites S. */
22909
22910 static int
22911 left_overwriting (struct glyph_string *s)
22912 {
22913 int i, k, x;
22914 struct glyph *glyphs = s->row->glyphs[s->area];
22915 int first = s->first_glyph - glyphs;
22916
22917 k = -1;
22918 x = 0;
22919 for (i = first - 1; i >= 0; --i)
22920 {
22921 int left, right;
22922 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22923 if (x + right > 0)
22924 k = i;
22925 x -= glyphs[i].pixel_width;
22926 }
22927
22928 return k;
22929 }
22930
22931
22932 /* Return the index of the last glyph following glyph string S that is
22933 overwritten by S because of S's right overhang. Value is -1 if
22934 no such glyph is found. */
22935
22936 static int
22937 right_overwritten (struct glyph_string *s)
22938 {
22939 int k = -1;
22940
22941 if (s->right_overhang)
22942 {
22943 int x = 0, i;
22944 struct glyph *glyphs = s->row->glyphs[s->area];
22945 int first = (s->first_glyph - glyphs
22946 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
22947 int end = s->row->used[s->area];
22948
22949 for (i = first; i < end && s->right_overhang > x; ++i)
22950 x += glyphs[i].pixel_width;
22951
22952 k = i;
22953 }
22954
22955 return k;
22956 }
22957
22958
22959 /* Return the index of the last glyph following glyph string S that
22960 overwrites S because of its left overhang. Value is negative
22961 if no such glyph is found. */
22962
22963 static int
22964 right_overwriting (struct glyph_string *s)
22965 {
22966 int i, k, x;
22967 int end = s->row->used[s->area];
22968 struct glyph *glyphs = s->row->glyphs[s->area];
22969 int first = (s->first_glyph - glyphs
22970 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
22971
22972 k = -1;
22973 x = 0;
22974 for (i = first; i < end; ++i)
22975 {
22976 int left, right;
22977 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
22978 if (x - left < 0)
22979 k = i;
22980 x += glyphs[i].pixel_width;
22981 }
22982
22983 return k;
22984 }
22985
22986
22987 /* Set background width of glyph string S. START is the index of the
22988 first glyph following S. LAST_X is the right-most x-position + 1
22989 in the drawing area. */
22990
22991 static void
22992 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
22993 {
22994 /* If the face of this glyph string has to be drawn to the end of
22995 the drawing area, set S->extends_to_end_of_line_p. */
22996
22997 if (start == s->row->used[s->area]
22998 && s->area == TEXT_AREA
22999 && ((s->row->fill_line_p
23000 && (s->hl == DRAW_NORMAL_TEXT
23001 || s->hl == DRAW_IMAGE_RAISED
23002 || s->hl == DRAW_IMAGE_SUNKEN))
23003 || s->hl == DRAW_MOUSE_FACE))
23004 s->extends_to_end_of_line_p = 1;
23005
23006 /* If S extends its face to the end of the line, set its
23007 background_width to the distance to the right edge of the drawing
23008 area. */
23009 if (s->extends_to_end_of_line_p)
23010 s->background_width = last_x - s->x + 1;
23011 else
23012 s->background_width = s->width;
23013 }
23014
23015
23016 /* Compute overhangs and x-positions for glyph string S and its
23017 predecessors, or successors. X is the starting x-position for S.
23018 BACKWARD_P non-zero means process predecessors. */
23019
23020 static void
23021 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23022 {
23023 if (backward_p)
23024 {
23025 while (s)
23026 {
23027 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23028 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23029 x -= s->width;
23030 s->x = x;
23031 s = s->prev;
23032 }
23033 }
23034 else
23035 {
23036 while (s)
23037 {
23038 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23039 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23040 s->x = x;
23041 x += s->width;
23042 s = s->next;
23043 }
23044 }
23045 }
23046
23047
23048
23049 /* The following macros are only called from draw_glyphs below.
23050 They reference the following parameters of that function directly:
23051 `w', `row', `area', and `overlap_p'
23052 as well as the following local variables:
23053 `s', `f', and `hdc' (in W32) */
23054
23055 #ifdef HAVE_NTGUI
23056 /* On W32, silently add local `hdc' variable to argument list of
23057 init_glyph_string. */
23058 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23059 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23060 #else
23061 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23062 init_glyph_string (s, char2b, w, row, area, start, hl)
23063 #endif
23064
23065 /* Add a glyph string for a stretch glyph to the list of strings
23066 between HEAD and TAIL. START is the index of the stretch glyph in
23067 row area AREA of glyph row ROW. END is the index of the last glyph
23068 in that glyph row area. X is the current output position assigned
23069 to the new glyph string constructed. HL overrides that face of the
23070 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23071 is the right-most x-position of the drawing area. */
23072
23073 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23074 and below -- keep them on one line. */
23075 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23076 do \
23077 { \
23078 s = alloca (sizeof *s); \
23079 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23080 START = fill_stretch_glyph_string (s, START, END); \
23081 append_glyph_string (&HEAD, &TAIL, s); \
23082 s->x = (X); \
23083 } \
23084 while (0)
23085
23086
23087 /* Add a glyph string for an image glyph to the list of strings
23088 between HEAD and TAIL. START is the index of the image glyph in
23089 row area AREA of glyph row ROW. END is the index of the last glyph
23090 in that glyph row area. X is the current output position assigned
23091 to the new glyph string constructed. HL overrides that face of the
23092 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23093 is the right-most x-position of the drawing area. */
23094
23095 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23096 do \
23097 { \
23098 s = alloca (sizeof *s); \
23099 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23100 fill_image_glyph_string (s); \
23101 append_glyph_string (&HEAD, &TAIL, s); \
23102 ++START; \
23103 s->x = (X); \
23104 } \
23105 while (0)
23106
23107
23108 /* Add a glyph string for a sequence of character glyphs to the list
23109 of strings between HEAD and TAIL. START is the index of the first
23110 glyph in row area AREA of glyph row ROW that is part of the new
23111 glyph string. END is the index of the last glyph in that glyph row
23112 area. X is the current output position assigned to the new glyph
23113 string constructed. HL overrides that face of the glyph; e.g. it
23114 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23115 right-most x-position of the drawing area. */
23116
23117 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23118 do \
23119 { \
23120 int face_id; \
23121 XChar2b *char2b; \
23122 \
23123 face_id = (row)->glyphs[area][START].face_id; \
23124 \
23125 s = alloca (sizeof *s); \
23126 char2b = alloca ((END - START) * sizeof *char2b); \
23127 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23128 append_glyph_string (&HEAD, &TAIL, s); \
23129 s->x = (X); \
23130 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23131 } \
23132 while (0)
23133
23134
23135 /* Add a glyph string for a composite sequence to the list of strings
23136 between HEAD and TAIL. START is the index of the first glyph in
23137 row area AREA of glyph row ROW that is part of the new glyph
23138 string. END is the index of the last glyph in that glyph row area.
23139 X is the current output position assigned to the new glyph string
23140 constructed. HL overrides that face of the glyph; e.g. it is
23141 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23142 x-position of the drawing area. */
23143
23144 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23145 do { \
23146 int face_id = (row)->glyphs[area][START].face_id; \
23147 struct face *base_face = FACE_FROM_ID (f, face_id); \
23148 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23149 struct composition *cmp = composition_table[cmp_id]; \
23150 XChar2b *char2b; \
23151 struct glyph_string *first_s = NULL; \
23152 int n; \
23153 \
23154 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23155 \
23156 /* Make glyph_strings for each glyph sequence that is drawable by \
23157 the same face, and append them to HEAD/TAIL. */ \
23158 for (n = 0; n < cmp->glyph_len;) \
23159 { \
23160 s = alloca (sizeof *s); \
23161 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23162 append_glyph_string (&(HEAD), &(TAIL), s); \
23163 s->cmp = cmp; \
23164 s->cmp_from = n; \
23165 s->x = (X); \
23166 if (n == 0) \
23167 first_s = s; \
23168 n = fill_composite_glyph_string (s, base_face, overlaps); \
23169 } \
23170 \
23171 ++START; \
23172 s = first_s; \
23173 } while (0)
23174
23175
23176 /* Add a glyph string for a glyph-string sequence to the list of strings
23177 between HEAD and TAIL. */
23178
23179 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23180 do { \
23181 int face_id; \
23182 XChar2b *char2b; \
23183 Lisp_Object gstring; \
23184 \
23185 face_id = (row)->glyphs[area][START].face_id; \
23186 gstring = (composition_gstring_from_id \
23187 ((row)->glyphs[area][START].u.cmp.id)); \
23188 s = alloca (sizeof *s); \
23189 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23190 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23191 append_glyph_string (&(HEAD), &(TAIL), s); \
23192 s->x = (X); \
23193 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23194 } while (0)
23195
23196
23197 /* Add a glyph string for a sequence of glyphless character's glyphs
23198 to the list of strings between HEAD and TAIL. The meanings of
23199 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23200
23201 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23202 do \
23203 { \
23204 int face_id; \
23205 \
23206 face_id = (row)->glyphs[area][START].face_id; \
23207 \
23208 s = alloca (sizeof *s); \
23209 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23210 append_glyph_string (&HEAD, &TAIL, s); \
23211 s->x = (X); \
23212 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23213 overlaps); \
23214 } \
23215 while (0)
23216
23217
23218 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23219 of AREA of glyph row ROW on window W between indices START and END.
23220 HL overrides the face for drawing glyph strings, e.g. it is
23221 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23222 x-positions of the drawing area.
23223
23224 This is an ugly monster macro construct because we must use alloca
23225 to allocate glyph strings (because draw_glyphs can be called
23226 asynchronously). */
23227
23228 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23229 do \
23230 { \
23231 HEAD = TAIL = NULL; \
23232 while (START < END) \
23233 { \
23234 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23235 switch (first_glyph->type) \
23236 { \
23237 case CHAR_GLYPH: \
23238 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23239 HL, X, LAST_X); \
23240 break; \
23241 \
23242 case COMPOSITE_GLYPH: \
23243 if (first_glyph->u.cmp.automatic) \
23244 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23245 HL, X, LAST_X); \
23246 else \
23247 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23248 HL, X, LAST_X); \
23249 break; \
23250 \
23251 case STRETCH_GLYPH: \
23252 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23253 HL, X, LAST_X); \
23254 break; \
23255 \
23256 case IMAGE_GLYPH: \
23257 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23258 HL, X, LAST_X); \
23259 break; \
23260 \
23261 case GLYPHLESS_GLYPH: \
23262 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23263 HL, X, LAST_X); \
23264 break; \
23265 \
23266 default: \
23267 emacs_abort (); \
23268 } \
23269 \
23270 if (s) \
23271 { \
23272 set_glyph_string_background_width (s, START, LAST_X); \
23273 (X) += s->width; \
23274 } \
23275 } \
23276 } while (0)
23277
23278
23279 /* Draw glyphs between START and END in AREA of ROW on window W,
23280 starting at x-position X. X is relative to AREA in W. HL is a
23281 face-override with the following meaning:
23282
23283 DRAW_NORMAL_TEXT draw normally
23284 DRAW_CURSOR draw in cursor face
23285 DRAW_MOUSE_FACE draw in mouse face.
23286 DRAW_INVERSE_VIDEO draw in mode line face
23287 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23288 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23289
23290 If OVERLAPS is non-zero, draw only the foreground of characters and
23291 clip to the physical height of ROW. Non-zero value also defines
23292 the overlapping part to be drawn:
23293
23294 OVERLAPS_PRED overlap with preceding rows
23295 OVERLAPS_SUCC overlap with succeeding rows
23296 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23297 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23298
23299 Value is the x-position reached, relative to AREA of W. */
23300
23301 static int
23302 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23303 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23304 enum draw_glyphs_face hl, int overlaps)
23305 {
23306 struct glyph_string *head, *tail;
23307 struct glyph_string *s;
23308 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23309 int i, j, x_reached, last_x, area_left = 0;
23310 struct frame *f = XFRAME (WINDOW_FRAME (w));
23311 DECLARE_HDC (hdc);
23312
23313 ALLOCATE_HDC (hdc, f);
23314
23315 /* Let's rather be paranoid than getting a SEGV. */
23316 end = min (end, row->used[area]);
23317 start = clip_to_bounds (0, start, end);
23318
23319 /* Translate X to frame coordinates. Set last_x to the right
23320 end of the drawing area. */
23321 if (row->full_width_p)
23322 {
23323 /* X is relative to the left edge of W, without scroll bars
23324 or fringes. */
23325 area_left = WINDOW_LEFT_EDGE_X (w);
23326 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23327 }
23328 else
23329 {
23330 area_left = window_box_left (w, area);
23331 last_x = area_left + window_box_width (w, area);
23332 }
23333 x += area_left;
23334
23335 /* Build a doubly-linked list of glyph_string structures between
23336 head and tail from what we have to draw. Note that the macro
23337 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23338 the reason we use a separate variable `i'. */
23339 i = start;
23340 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23341 if (tail)
23342 x_reached = tail->x + tail->background_width;
23343 else
23344 x_reached = x;
23345
23346 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23347 the row, redraw some glyphs in front or following the glyph
23348 strings built above. */
23349 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23350 {
23351 struct glyph_string *h, *t;
23352 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23353 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23354 int check_mouse_face = 0;
23355 int dummy_x = 0;
23356
23357 /* If mouse highlighting is on, we may need to draw adjacent
23358 glyphs using mouse-face highlighting. */
23359 if (area == TEXT_AREA && row->mouse_face_p
23360 && hlinfo->mouse_face_beg_row >= 0
23361 && hlinfo->mouse_face_end_row >= 0)
23362 {
23363 struct glyph_row *mouse_beg_row, *mouse_end_row;
23364
23365 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23366 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23367
23368 if (row >= mouse_beg_row && row <= mouse_end_row)
23369 {
23370 check_mouse_face = 1;
23371 mouse_beg_col = (row == mouse_beg_row)
23372 ? hlinfo->mouse_face_beg_col : 0;
23373 mouse_end_col = (row == mouse_end_row)
23374 ? hlinfo->mouse_face_end_col
23375 : row->used[TEXT_AREA];
23376 }
23377 }
23378
23379 /* Compute overhangs for all glyph strings. */
23380 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23381 for (s = head; s; s = s->next)
23382 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23383
23384 /* Prepend glyph strings for glyphs in front of the first glyph
23385 string that are overwritten because of the first glyph
23386 string's left overhang. The background of all strings
23387 prepended must be drawn because the first glyph string
23388 draws over it. */
23389 i = left_overwritten (head);
23390 if (i >= 0)
23391 {
23392 enum draw_glyphs_face overlap_hl;
23393
23394 /* If this row contains mouse highlighting, attempt to draw
23395 the overlapped glyphs with the correct highlight. This
23396 code fails if the overlap encompasses more than one glyph
23397 and mouse-highlight spans only some of these glyphs.
23398 However, making it work perfectly involves a lot more
23399 code, and I don't know if the pathological case occurs in
23400 practice, so we'll stick to this for now. --- cyd */
23401 if (check_mouse_face
23402 && mouse_beg_col < start && mouse_end_col > i)
23403 overlap_hl = DRAW_MOUSE_FACE;
23404 else
23405 overlap_hl = DRAW_NORMAL_TEXT;
23406
23407 j = i;
23408 BUILD_GLYPH_STRINGS (j, start, h, t,
23409 overlap_hl, dummy_x, last_x);
23410 start = i;
23411 compute_overhangs_and_x (t, head->x, 1);
23412 prepend_glyph_string_lists (&head, &tail, h, t);
23413 clip_head = head;
23414 }
23415
23416 /* Prepend glyph strings for glyphs in front of the first glyph
23417 string that overwrite that glyph string because of their
23418 right overhang. For these strings, only the foreground must
23419 be drawn, because it draws over the glyph string at `head'.
23420 The background must not be drawn because this would overwrite
23421 right overhangs of preceding glyphs for which no glyph
23422 strings exist. */
23423 i = left_overwriting (head);
23424 if (i >= 0)
23425 {
23426 enum draw_glyphs_face overlap_hl;
23427
23428 if (check_mouse_face
23429 && mouse_beg_col < start && mouse_end_col > i)
23430 overlap_hl = DRAW_MOUSE_FACE;
23431 else
23432 overlap_hl = DRAW_NORMAL_TEXT;
23433
23434 clip_head = head;
23435 BUILD_GLYPH_STRINGS (i, start, h, t,
23436 overlap_hl, dummy_x, last_x);
23437 for (s = h; s; s = s->next)
23438 s->background_filled_p = 1;
23439 compute_overhangs_and_x (t, head->x, 1);
23440 prepend_glyph_string_lists (&head, &tail, h, t);
23441 }
23442
23443 /* Append glyphs strings for glyphs following the last glyph
23444 string tail that are overwritten by tail. The background of
23445 these strings has to be drawn because tail's foreground draws
23446 over it. */
23447 i = right_overwritten (tail);
23448 if (i >= 0)
23449 {
23450 enum draw_glyphs_face overlap_hl;
23451
23452 if (check_mouse_face
23453 && mouse_beg_col < i && mouse_end_col > end)
23454 overlap_hl = DRAW_MOUSE_FACE;
23455 else
23456 overlap_hl = DRAW_NORMAL_TEXT;
23457
23458 BUILD_GLYPH_STRINGS (end, i, h, t,
23459 overlap_hl, x, last_x);
23460 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23461 we don't have `end = i;' here. */
23462 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23463 append_glyph_string_lists (&head, &tail, h, t);
23464 clip_tail = tail;
23465 }
23466
23467 /* Append glyph strings for glyphs following the last glyph
23468 string tail that overwrite tail. The foreground of such
23469 glyphs has to be drawn because it writes into the background
23470 of tail. The background must not be drawn because it could
23471 paint over the foreground of following glyphs. */
23472 i = right_overwriting (tail);
23473 if (i >= 0)
23474 {
23475 enum draw_glyphs_face overlap_hl;
23476 if (check_mouse_face
23477 && mouse_beg_col < i && mouse_end_col > end)
23478 overlap_hl = DRAW_MOUSE_FACE;
23479 else
23480 overlap_hl = DRAW_NORMAL_TEXT;
23481
23482 clip_tail = tail;
23483 i++; /* We must include the Ith glyph. */
23484 BUILD_GLYPH_STRINGS (end, i, h, t,
23485 overlap_hl, x, last_x);
23486 for (s = h; s; s = s->next)
23487 s->background_filled_p = 1;
23488 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23489 append_glyph_string_lists (&head, &tail, h, t);
23490 }
23491 if (clip_head || clip_tail)
23492 for (s = head; s; s = s->next)
23493 {
23494 s->clip_head = clip_head;
23495 s->clip_tail = clip_tail;
23496 }
23497 }
23498
23499 /* Draw all strings. */
23500 for (s = head; s; s = s->next)
23501 FRAME_RIF (f)->draw_glyph_string (s);
23502
23503 #ifndef HAVE_NS
23504 /* When focus a sole frame and move horizontally, this sets on_p to 0
23505 causing a failure to erase prev cursor position. */
23506 if (area == TEXT_AREA
23507 && !row->full_width_p
23508 /* When drawing overlapping rows, only the glyph strings'
23509 foreground is drawn, which doesn't erase a cursor
23510 completely. */
23511 && !overlaps)
23512 {
23513 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23514 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23515 : (tail ? tail->x + tail->background_width : x));
23516 x0 -= area_left;
23517 x1 -= area_left;
23518
23519 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23520 row->y, MATRIX_ROW_BOTTOM_Y (row));
23521 }
23522 #endif
23523
23524 /* Value is the x-position up to which drawn, relative to AREA of W.
23525 This doesn't include parts drawn because of overhangs. */
23526 if (row->full_width_p)
23527 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23528 else
23529 x_reached -= area_left;
23530
23531 RELEASE_HDC (hdc, f);
23532
23533 return x_reached;
23534 }
23535
23536 /* Expand row matrix if too narrow. Don't expand if area
23537 is not present. */
23538
23539 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23540 { \
23541 if (!fonts_changed_p \
23542 && (it->glyph_row->glyphs[area] \
23543 < it->glyph_row->glyphs[area + 1])) \
23544 { \
23545 it->w->ncols_scale_factor++; \
23546 fonts_changed_p = 1; \
23547 } \
23548 }
23549
23550 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23551 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23552
23553 static void
23554 append_glyph (struct it *it)
23555 {
23556 struct glyph *glyph;
23557 enum glyph_row_area area = it->area;
23558
23559 eassert (it->glyph_row);
23560 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23561
23562 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23563 if (glyph < it->glyph_row->glyphs[area + 1])
23564 {
23565 /* If the glyph row is reversed, we need to prepend the glyph
23566 rather than append it. */
23567 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23568 {
23569 struct glyph *g;
23570
23571 /* Make room for the additional glyph. */
23572 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23573 g[1] = *g;
23574 glyph = it->glyph_row->glyphs[area];
23575 }
23576 glyph->charpos = CHARPOS (it->position);
23577 glyph->object = it->object;
23578 if (it->pixel_width > 0)
23579 {
23580 glyph->pixel_width = it->pixel_width;
23581 glyph->padding_p = 0;
23582 }
23583 else
23584 {
23585 /* Assure at least 1-pixel width. Otherwise, cursor can't
23586 be displayed correctly. */
23587 glyph->pixel_width = 1;
23588 glyph->padding_p = 1;
23589 }
23590 glyph->ascent = it->ascent;
23591 glyph->descent = it->descent;
23592 glyph->voffset = it->voffset;
23593 glyph->type = CHAR_GLYPH;
23594 glyph->avoid_cursor_p = it->avoid_cursor_p;
23595 glyph->multibyte_p = it->multibyte_p;
23596 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23597 {
23598 /* In R2L rows, the left and the right box edges need to be
23599 drawn in reverse direction. */
23600 glyph->right_box_line_p = it->start_of_box_run_p;
23601 glyph->left_box_line_p = it->end_of_box_run_p;
23602 }
23603 else
23604 {
23605 glyph->left_box_line_p = it->start_of_box_run_p;
23606 glyph->right_box_line_p = it->end_of_box_run_p;
23607 }
23608 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23609 || it->phys_descent > it->descent);
23610 glyph->glyph_not_available_p = it->glyph_not_available_p;
23611 glyph->face_id = it->face_id;
23612 glyph->u.ch = it->char_to_display;
23613 glyph->slice.img = null_glyph_slice;
23614 glyph->font_type = FONT_TYPE_UNKNOWN;
23615 if (it->bidi_p)
23616 {
23617 glyph->resolved_level = it->bidi_it.resolved_level;
23618 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23619 emacs_abort ();
23620 glyph->bidi_type = it->bidi_it.type;
23621 }
23622 else
23623 {
23624 glyph->resolved_level = 0;
23625 glyph->bidi_type = UNKNOWN_BT;
23626 }
23627 ++it->glyph_row->used[area];
23628 }
23629 else
23630 IT_EXPAND_MATRIX_WIDTH (it, area);
23631 }
23632
23633 /* Store one glyph for the composition IT->cmp_it.id in
23634 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23635 non-null. */
23636
23637 static void
23638 append_composite_glyph (struct it *it)
23639 {
23640 struct glyph *glyph;
23641 enum glyph_row_area area = it->area;
23642
23643 eassert (it->glyph_row);
23644
23645 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23646 if (glyph < it->glyph_row->glyphs[area + 1])
23647 {
23648 /* If the glyph row is reversed, we need to prepend the glyph
23649 rather than append it. */
23650 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23651 {
23652 struct glyph *g;
23653
23654 /* Make room for the new glyph. */
23655 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23656 g[1] = *g;
23657 glyph = it->glyph_row->glyphs[it->area];
23658 }
23659 glyph->charpos = it->cmp_it.charpos;
23660 glyph->object = it->object;
23661 glyph->pixel_width = it->pixel_width;
23662 glyph->ascent = it->ascent;
23663 glyph->descent = it->descent;
23664 glyph->voffset = it->voffset;
23665 glyph->type = COMPOSITE_GLYPH;
23666 if (it->cmp_it.ch < 0)
23667 {
23668 glyph->u.cmp.automatic = 0;
23669 glyph->u.cmp.id = it->cmp_it.id;
23670 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23671 }
23672 else
23673 {
23674 glyph->u.cmp.automatic = 1;
23675 glyph->u.cmp.id = it->cmp_it.id;
23676 glyph->slice.cmp.from = it->cmp_it.from;
23677 glyph->slice.cmp.to = it->cmp_it.to - 1;
23678 }
23679 glyph->avoid_cursor_p = it->avoid_cursor_p;
23680 glyph->multibyte_p = it->multibyte_p;
23681 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23682 {
23683 /* In R2L rows, the left and the right box edges need to be
23684 drawn in reverse direction. */
23685 glyph->right_box_line_p = it->start_of_box_run_p;
23686 glyph->left_box_line_p = it->end_of_box_run_p;
23687 }
23688 else
23689 {
23690 glyph->left_box_line_p = it->start_of_box_run_p;
23691 glyph->right_box_line_p = it->end_of_box_run_p;
23692 }
23693 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23694 || it->phys_descent > it->descent);
23695 glyph->padding_p = 0;
23696 glyph->glyph_not_available_p = 0;
23697 glyph->face_id = it->face_id;
23698 glyph->font_type = FONT_TYPE_UNKNOWN;
23699 if (it->bidi_p)
23700 {
23701 glyph->resolved_level = it->bidi_it.resolved_level;
23702 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23703 emacs_abort ();
23704 glyph->bidi_type = it->bidi_it.type;
23705 }
23706 ++it->glyph_row->used[area];
23707 }
23708 else
23709 IT_EXPAND_MATRIX_WIDTH (it, area);
23710 }
23711
23712
23713 /* Change IT->ascent and IT->height according to the setting of
23714 IT->voffset. */
23715
23716 static void
23717 take_vertical_position_into_account (struct it *it)
23718 {
23719 if (it->voffset)
23720 {
23721 if (it->voffset < 0)
23722 /* Increase the ascent so that we can display the text higher
23723 in the line. */
23724 it->ascent -= it->voffset;
23725 else
23726 /* Increase the descent so that we can display the text lower
23727 in the line. */
23728 it->descent += it->voffset;
23729 }
23730 }
23731
23732
23733 /* Produce glyphs/get display metrics for the image IT is loaded with.
23734 See the description of struct display_iterator in dispextern.h for
23735 an overview of struct display_iterator. */
23736
23737 static void
23738 produce_image_glyph (struct it *it)
23739 {
23740 struct image *img;
23741 struct face *face;
23742 int glyph_ascent, crop;
23743 struct glyph_slice slice;
23744
23745 eassert (it->what == IT_IMAGE);
23746
23747 face = FACE_FROM_ID (it->f, it->face_id);
23748 eassert (face);
23749 /* Make sure X resources of the face is loaded. */
23750 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23751
23752 if (it->image_id < 0)
23753 {
23754 /* Fringe bitmap. */
23755 it->ascent = it->phys_ascent = 0;
23756 it->descent = it->phys_descent = 0;
23757 it->pixel_width = 0;
23758 it->nglyphs = 0;
23759 return;
23760 }
23761
23762 img = IMAGE_FROM_ID (it->f, it->image_id);
23763 eassert (img);
23764 /* Make sure X resources of the image is loaded. */
23765 prepare_image_for_display (it->f, img);
23766
23767 slice.x = slice.y = 0;
23768 slice.width = img->width;
23769 slice.height = img->height;
23770
23771 if (INTEGERP (it->slice.x))
23772 slice.x = XINT (it->slice.x);
23773 else if (FLOATP (it->slice.x))
23774 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23775
23776 if (INTEGERP (it->slice.y))
23777 slice.y = XINT (it->slice.y);
23778 else if (FLOATP (it->slice.y))
23779 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23780
23781 if (INTEGERP (it->slice.width))
23782 slice.width = XINT (it->slice.width);
23783 else if (FLOATP (it->slice.width))
23784 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23785
23786 if (INTEGERP (it->slice.height))
23787 slice.height = XINT (it->slice.height);
23788 else if (FLOATP (it->slice.height))
23789 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23790
23791 if (slice.x >= img->width)
23792 slice.x = img->width;
23793 if (slice.y >= img->height)
23794 slice.y = img->height;
23795 if (slice.x + slice.width >= img->width)
23796 slice.width = img->width - slice.x;
23797 if (slice.y + slice.height > img->height)
23798 slice.height = img->height - slice.y;
23799
23800 if (slice.width == 0 || slice.height == 0)
23801 return;
23802
23803 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23804
23805 it->descent = slice.height - glyph_ascent;
23806 if (slice.y == 0)
23807 it->descent += img->vmargin;
23808 if (slice.y + slice.height == img->height)
23809 it->descent += img->vmargin;
23810 it->phys_descent = it->descent;
23811
23812 it->pixel_width = slice.width;
23813 if (slice.x == 0)
23814 it->pixel_width += img->hmargin;
23815 if (slice.x + slice.width == img->width)
23816 it->pixel_width += img->hmargin;
23817
23818 /* It's quite possible for images to have an ascent greater than
23819 their height, so don't get confused in that case. */
23820 if (it->descent < 0)
23821 it->descent = 0;
23822
23823 it->nglyphs = 1;
23824
23825 if (face->box != FACE_NO_BOX)
23826 {
23827 if (face->box_line_width > 0)
23828 {
23829 if (slice.y == 0)
23830 it->ascent += face->box_line_width;
23831 if (slice.y + slice.height == img->height)
23832 it->descent += face->box_line_width;
23833 }
23834
23835 if (it->start_of_box_run_p && slice.x == 0)
23836 it->pixel_width += eabs (face->box_line_width);
23837 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
23838 it->pixel_width += eabs (face->box_line_width);
23839 }
23840
23841 take_vertical_position_into_account (it);
23842
23843 /* Automatically crop wide image glyphs at right edge so we can
23844 draw the cursor on same display row. */
23845 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
23846 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
23847 {
23848 it->pixel_width -= crop;
23849 slice.width -= crop;
23850 }
23851
23852 if (it->glyph_row)
23853 {
23854 struct glyph *glyph;
23855 enum glyph_row_area area = it->area;
23856
23857 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23858 if (glyph < it->glyph_row->glyphs[area + 1])
23859 {
23860 glyph->charpos = CHARPOS (it->position);
23861 glyph->object = it->object;
23862 glyph->pixel_width = it->pixel_width;
23863 glyph->ascent = glyph_ascent;
23864 glyph->descent = it->descent;
23865 glyph->voffset = it->voffset;
23866 glyph->type = IMAGE_GLYPH;
23867 glyph->avoid_cursor_p = it->avoid_cursor_p;
23868 glyph->multibyte_p = it->multibyte_p;
23869 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23870 {
23871 /* In R2L rows, the left and the right box edges need to be
23872 drawn in reverse direction. */
23873 glyph->right_box_line_p = it->start_of_box_run_p;
23874 glyph->left_box_line_p = it->end_of_box_run_p;
23875 }
23876 else
23877 {
23878 glyph->left_box_line_p = it->start_of_box_run_p;
23879 glyph->right_box_line_p = it->end_of_box_run_p;
23880 }
23881 glyph->overlaps_vertically_p = 0;
23882 glyph->padding_p = 0;
23883 glyph->glyph_not_available_p = 0;
23884 glyph->face_id = it->face_id;
23885 glyph->u.img_id = img->id;
23886 glyph->slice.img = slice;
23887 glyph->font_type = FONT_TYPE_UNKNOWN;
23888 if (it->bidi_p)
23889 {
23890 glyph->resolved_level = it->bidi_it.resolved_level;
23891 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23892 emacs_abort ();
23893 glyph->bidi_type = it->bidi_it.type;
23894 }
23895 ++it->glyph_row->used[area];
23896 }
23897 else
23898 IT_EXPAND_MATRIX_WIDTH (it, area);
23899 }
23900 }
23901
23902
23903 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
23904 of the glyph, WIDTH and HEIGHT are the width and height of the
23905 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
23906
23907 static void
23908 append_stretch_glyph (struct it *it, Lisp_Object object,
23909 int width, int height, int ascent)
23910 {
23911 struct glyph *glyph;
23912 enum glyph_row_area area = it->area;
23913
23914 eassert (ascent >= 0 && ascent <= height);
23915
23916 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23917 if (glyph < it->glyph_row->glyphs[area + 1])
23918 {
23919 /* If the glyph row is reversed, we need to prepend the glyph
23920 rather than append it. */
23921 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23922 {
23923 struct glyph *g;
23924
23925 /* Make room for the additional glyph. */
23926 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23927 g[1] = *g;
23928 glyph = it->glyph_row->glyphs[area];
23929 }
23930 glyph->charpos = CHARPOS (it->position);
23931 glyph->object = object;
23932 glyph->pixel_width = width;
23933 glyph->ascent = ascent;
23934 glyph->descent = height - ascent;
23935 glyph->voffset = it->voffset;
23936 glyph->type = STRETCH_GLYPH;
23937 glyph->avoid_cursor_p = it->avoid_cursor_p;
23938 glyph->multibyte_p = it->multibyte_p;
23939 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23940 {
23941 /* In R2L rows, the left and the right box edges need to be
23942 drawn in reverse direction. */
23943 glyph->right_box_line_p = it->start_of_box_run_p;
23944 glyph->left_box_line_p = it->end_of_box_run_p;
23945 }
23946 else
23947 {
23948 glyph->left_box_line_p = it->start_of_box_run_p;
23949 glyph->right_box_line_p = it->end_of_box_run_p;
23950 }
23951 glyph->overlaps_vertically_p = 0;
23952 glyph->padding_p = 0;
23953 glyph->glyph_not_available_p = 0;
23954 glyph->face_id = it->face_id;
23955 glyph->u.stretch.ascent = ascent;
23956 glyph->u.stretch.height = height;
23957 glyph->slice.img = null_glyph_slice;
23958 glyph->font_type = FONT_TYPE_UNKNOWN;
23959 if (it->bidi_p)
23960 {
23961 glyph->resolved_level = it->bidi_it.resolved_level;
23962 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23963 emacs_abort ();
23964 glyph->bidi_type = it->bidi_it.type;
23965 }
23966 else
23967 {
23968 glyph->resolved_level = 0;
23969 glyph->bidi_type = UNKNOWN_BT;
23970 }
23971 ++it->glyph_row->used[area];
23972 }
23973 else
23974 IT_EXPAND_MATRIX_WIDTH (it, area);
23975 }
23976
23977 #endif /* HAVE_WINDOW_SYSTEM */
23978
23979 /* Produce a stretch glyph for iterator IT. IT->object is the value
23980 of the glyph property displayed. The value must be a list
23981 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
23982 being recognized:
23983
23984 1. `:width WIDTH' specifies that the space should be WIDTH *
23985 canonical char width wide. WIDTH may be an integer or floating
23986 point number.
23987
23988 2. `:relative-width FACTOR' specifies that the width of the stretch
23989 should be computed from the width of the first character having the
23990 `glyph' property, and should be FACTOR times that width.
23991
23992 3. `:align-to HPOS' specifies that the space should be wide enough
23993 to reach HPOS, a value in canonical character units.
23994
23995 Exactly one of the above pairs must be present.
23996
23997 4. `:height HEIGHT' specifies that the height of the stretch produced
23998 should be HEIGHT, measured in canonical character units.
23999
24000 5. `:relative-height FACTOR' specifies that the height of the
24001 stretch should be FACTOR times the height of the characters having
24002 the glyph property.
24003
24004 Either none or exactly one of 4 or 5 must be present.
24005
24006 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24007 of the stretch should be used for the ascent of the stretch.
24008 ASCENT must be in the range 0 <= ASCENT <= 100. */
24009
24010 void
24011 produce_stretch_glyph (struct it *it)
24012 {
24013 /* (space :width WIDTH :height HEIGHT ...) */
24014 Lisp_Object prop, plist;
24015 int width = 0, height = 0, align_to = -1;
24016 int zero_width_ok_p = 0;
24017 double tem;
24018 struct font *font = NULL;
24019
24020 #ifdef HAVE_WINDOW_SYSTEM
24021 int ascent = 0;
24022 int zero_height_ok_p = 0;
24023
24024 if (FRAME_WINDOW_P (it->f))
24025 {
24026 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24027 font = face->font ? face->font : FRAME_FONT (it->f);
24028 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24029 }
24030 #endif
24031
24032 /* List should start with `space'. */
24033 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24034 plist = XCDR (it->object);
24035
24036 /* Compute the width of the stretch. */
24037 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24038 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24039 {
24040 /* Absolute width `:width WIDTH' specified and valid. */
24041 zero_width_ok_p = 1;
24042 width = (int)tem;
24043 }
24044 #ifdef HAVE_WINDOW_SYSTEM
24045 else if (FRAME_WINDOW_P (it->f)
24046 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24047 {
24048 /* Relative width `:relative-width FACTOR' specified and valid.
24049 Compute the width of the characters having the `glyph'
24050 property. */
24051 struct it it2;
24052 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24053
24054 it2 = *it;
24055 if (it->multibyte_p)
24056 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24057 else
24058 {
24059 it2.c = it2.char_to_display = *p, it2.len = 1;
24060 if (! ASCII_CHAR_P (it2.c))
24061 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24062 }
24063
24064 it2.glyph_row = NULL;
24065 it2.what = IT_CHARACTER;
24066 x_produce_glyphs (&it2);
24067 width = NUMVAL (prop) * it2.pixel_width;
24068 }
24069 #endif /* HAVE_WINDOW_SYSTEM */
24070 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24071 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24072 {
24073 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24074 align_to = (align_to < 0
24075 ? 0
24076 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24077 else if (align_to < 0)
24078 align_to = window_box_left_offset (it->w, TEXT_AREA);
24079 width = max (0, (int)tem + align_to - it->current_x);
24080 zero_width_ok_p = 1;
24081 }
24082 else
24083 /* Nothing specified -> width defaults to canonical char width. */
24084 width = FRAME_COLUMN_WIDTH (it->f);
24085
24086 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24087 width = 1;
24088
24089 #ifdef HAVE_WINDOW_SYSTEM
24090 /* Compute height. */
24091 if (FRAME_WINDOW_P (it->f))
24092 {
24093 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24094 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24095 {
24096 height = (int)tem;
24097 zero_height_ok_p = 1;
24098 }
24099 else if (prop = Fplist_get (plist, QCrelative_height),
24100 NUMVAL (prop) > 0)
24101 height = FONT_HEIGHT (font) * NUMVAL (prop);
24102 else
24103 height = FONT_HEIGHT (font);
24104
24105 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24106 height = 1;
24107
24108 /* Compute percentage of height used for ascent. If
24109 `:ascent ASCENT' is present and valid, use that. Otherwise,
24110 derive the ascent from the font in use. */
24111 if (prop = Fplist_get (plist, QCascent),
24112 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24113 ascent = height * NUMVAL (prop) / 100.0;
24114 else if (!NILP (prop)
24115 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24116 ascent = min (max (0, (int)tem), height);
24117 else
24118 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24119 }
24120 else
24121 #endif /* HAVE_WINDOW_SYSTEM */
24122 height = 1;
24123
24124 if (width > 0 && it->line_wrap != TRUNCATE
24125 && it->current_x + width > it->last_visible_x)
24126 {
24127 width = it->last_visible_x - it->current_x;
24128 #ifdef HAVE_WINDOW_SYSTEM
24129 /* Subtract one more pixel from the stretch width, but only on
24130 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24131 width -= FRAME_WINDOW_P (it->f);
24132 #endif
24133 }
24134
24135 if (width > 0 && height > 0 && it->glyph_row)
24136 {
24137 Lisp_Object o_object = it->object;
24138 Lisp_Object object = it->stack[it->sp - 1].string;
24139 int n = width;
24140
24141 if (!STRINGP (object))
24142 object = it->w->contents;
24143 #ifdef HAVE_WINDOW_SYSTEM
24144 if (FRAME_WINDOW_P (it->f))
24145 append_stretch_glyph (it, object, width, height, ascent);
24146 else
24147 #endif
24148 {
24149 it->object = object;
24150 it->char_to_display = ' ';
24151 it->pixel_width = it->len = 1;
24152 while (n--)
24153 tty_append_glyph (it);
24154 it->object = o_object;
24155 }
24156 }
24157
24158 it->pixel_width = width;
24159 #ifdef HAVE_WINDOW_SYSTEM
24160 if (FRAME_WINDOW_P (it->f))
24161 {
24162 it->ascent = it->phys_ascent = ascent;
24163 it->descent = it->phys_descent = height - it->ascent;
24164 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24165 take_vertical_position_into_account (it);
24166 }
24167 else
24168 #endif
24169 it->nglyphs = width;
24170 }
24171
24172 /* Get information about special display element WHAT in an
24173 environment described by IT. WHAT is one of IT_TRUNCATION or
24174 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24175 non-null glyph_row member. This function ensures that fields like
24176 face_id, c, len of IT are left untouched. */
24177
24178 static void
24179 produce_special_glyphs (struct it *it, enum display_element_type what)
24180 {
24181 struct it temp_it;
24182 Lisp_Object gc;
24183 GLYPH glyph;
24184
24185 temp_it = *it;
24186 temp_it.object = make_number (0);
24187 memset (&temp_it.current, 0, sizeof temp_it.current);
24188
24189 if (what == IT_CONTINUATION)
24190 {
24191 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24192 if (it->bidi_it.paragraph_dir == R2L)
24193 SET_GLYPH_FROM_CHAR (glyph, '/');
24194 else
24195 SET_GLYPH_FROM_CHAR (glyph, '\\');
24196 if (it->dp
24197 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24198 {
24199 /* FIXME: Should we mirror GC for R2L lines? */
24200 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24201 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24202 }
24203 }
24204 else if (what == IT_TRUNCATION)
24205 {
24206 /* Truncation glyph. */
24207 SET_GLYPH_FROM_CHAR (glyph, '$');
24208 if (it->dp
24209 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24210 {
24211 /* FIXME: Should we mirror GC for R2L lines? */
24212 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24213 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24214 }
24215 }
24216 else
24217 emacs_abort ();
24218
24219 #ifdef HAVE_WINDOW_SYSTEM
24220 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24221 is turned off, we precede the truncation/continuation glyphs by a
24222 stretch glyph whose width is computed such that these special
24223 glyphs are aligned at the window margin, even when very different
24224 fonts are used in different glyph rows. */
24225 if (FRAME_WINDOW_P (temp_it.f)
24226 /* init_iterator calls this with it->glyph_row == NULL, and it
24227 wants only the pixel width of the truncation/continuation
24228 glyphs. */
24229 && temp_it.glyph_row
24230 /* insert_left_trunc_glyphs calls us at the beginning of the
24231 row, and it has its own calculation of the stretch glyph
24232 width. */
24233 && temp_it.glyph_row->used[TEXT_AREA] > 0
24234 && (temp_it.glyph_row->reversed_p
24235 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24236 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24237 {
24238 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24239
24240 if (stretch_width > 0)
24241 {
24242 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24243 struct font *font =
24244 face->font ? face->font : FRAME_FONT (temp_it.f);
24245 int stretch_ascent =
24246 (((temp_it.ascent + temp_it.descent)
24247 * FONT_BASE (font)) / FONT_HEIGHT (font));
24248
24249 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24250 temp_it.ascent + temp_it.descent,
24251 stretch_ascent);
24252 }
24253 }
24254 #endif
24255
24256 temp_it.dp = NULL;
24257 temp_it.what = IT_CHARACTER;
24258 temp_it.len = 1;
24259 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24260 temp_it.face_id = GLYPH_FACE (glyph);
24261 temp_it.len = CHAR_BYTES (temp_it.c);
24262
24263 PRODUCE_GLYPHS (&temp_it);
24264 it->pixel_width = temp_it.pixel_width;
24265 it->nglyphs = temp_it.pixel_width;
24266 }
24267
24268 #ifdef HAVE_WINDOW_SYSTEM
24269
24270 /* Calculate line-height and line-spacing properties.
24271 An integer value specifies explicit pixel value.
24272 A float value specifies relative value to current face height.
24273 A cons (float . face-name) specifies relative value to
24274 height of specified face font.
24275
24276 Returns height in pixels, or nil. */
24277
24278
24279 static Lisp_Object
24280 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24281 int boff, int override)
24282 {
24283 Lisp_Object face_name = Qnil;
24284 int ascent, descent, height;
24285
24286 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24287 return val;
24288
24289 if (CONSP (val))
24290 {
24291 face_name = XCAR (val);
24292 val = XCDR (val);
24293 if (!NUMBERP (val))
24294 val = make_number (1);
24295 if (NILP (face_name))
24296 {
24297 height = it->ascent + it->descent;
24298 goto scale;
24299 }
24300 }
24301
24302 if (NILP (face_name))
24303 {
24304 font = FRAME_FONT (it->f);
24305 boff = FRAME_BASELINE_OFFSET (it->f);
24306 }
24307 else if (EQ (face_name, Qt))
24308 {
24309 override = 0;
24310 }
24311 else
24312 {
24313 int face_id;
24314 struct face *face;
24315
24316 face_id = lookup_named_face (it->f, face_name, 0);
24317 if (face_id < 0)
24318 return make_number (-1);
24319
24320 face = FACE_FROM_ID (it->f, face_id);
24321 font = face->font;
24322 if (font == NULL)
24323 return make_number (-1);
24324 boff = font->baseline_offset;
24325 if (font->vertical_centering)
24326 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24327 }
24328
24329 ascent = FONT_BASE (font) + boff;
24330 descent = FONT_DESCENT (font) - boff;
24331
24332 if (override)
24333 {
24334 it->override_ascent = ascent;
24335 it->override_descent = descent;
24336 it->override_boff = boff;
24337 }
24338
24339 height = ascent + descent;
24340
24341 scale:
24342 if (FLOATP (val))
24343 height = (int)(XFLOAT_DATA (val) * height);
24344 else if (INTEGERP (val))
24345 height *= XINT (val);
24346
24347 return make_number (height);
24348 }
24349
24350
24351 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24352 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24353 and only if this is for a character for which no font was found.
24354
24355 If the display method (it->glyphless_method) is
24356 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24357 length of the acronym or the hexadecimal string, UPPER_XOFF and
24358 UPPER_YOFF are pixel offsets for the upper part of the string,
24359 LOWER_XOFF and LOWER_YOFF are for the lower part.
24360
24361 For the other display methods, LEN through LOWER_YOFF are zero. */
24362
24363 static void
24364 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24365 short upper_xoff, short upper_yoff,
24366 short lower_xoff, short lower_yoff)
24367 {
24368 struct glyph *glyph;
24369 enum glyph_row_area area = it->area;
24370
24371 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24372 if (glyph < it->glyph_row->glyphs[area + 1])
24373 {
24374 /* If the glyph row is reversed, we need to prepend the glyph
24375 rather than append it. */
24376 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24377 {
24378 struct glyph *g;
24379
24380 /* Make room for the additional glyph. */
24381 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24382 g[1] = *g;
24383 glyph = it->glyph_row->glyphs[area];
24384 }
24385 glyph->charpos = CHARPOS (it->position);
24386 glyph->object = it->object;
24387 glyph->pixel_width = it->pixel_width;
24388 glyph->ascent = it->ascent;
24389 glyph->descent = it->descent;
24390 glyph->voffset = it->voffset;
24391 glyph->type = GLYPHLESS_GLYPH;
24392 glyph->u.glyphless.method = it->glyphless_method;
24393 glyph->u.glyphless.for_no_font = for_no_font;
24394 glyph->u.glyphless.len = len;
24395 glyph->u.glyphless.ch = it->c;
24396 glyph->slice.glyphless.upper_xoff = upper_xoff;
24397 glyph->slice.glyphless.upper_yoff = upper_yoff;
24398 glyph->slice.glyphless.lower_xoff = lower_xoff;
24399 glyph->slice.glyphless.lower_yoff = lower_yoff;
24400 glyph->avoid_cursor_p = it->avoid_cursor_p;
24401 glyph->multibyte_p = it->multibyte_p;
24402 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24403 {
24404 /* In R2L rows, the left and the right box edges need to be
24405 drawn in reverse direction. */
24406 glyph->right_box_line_p = it->start_of_box_run_p;
24407 glyph->left_box_line_p = it->end_of_box_run_p;
24408 }
24409 else
24410 {
24411 glyph->left_box_line_p = it->start_of_box_run_p;
24412 glyph->right_box_line_p = it->end_of_box_run_p;
24413 }
24414 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24415 || it->phys_descent > it->descent);
24416 glyph->padding_p = 0;
24417 glyph->glyph_not_available_p = 0;
24418 glyph->face_id = face_id;
24419 glyph->font_type = FONT_TYPE_UNKNOWN;
24420 if (it->bidi_p)
24421 {
24422 glyph->resolved_level = it->bidi_it.resolved_level;
24423 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24424 emacs_abort ();
24425 glyph->bidi_type = it->bidi_it.type;
24426 }
24427 ++it->glyph_row->used[area];
24428 }
24429 else
24430 IT_EXPAND_MATRIX_WIDTH (it, area);
24431 }
24432
24433
24434 /* Produce a glyph for a glyphless character for iterator IT.
24435 IT->glyphless_method specifies which method to use for displaying
24436 the character. See the description of enum
24437 glyphless_display_method in dispextern.h for the detail.
24438
24439 FOR_NO_FONT is nonzero if and only if this is for a character for
24440 which no font was found. ACRONYM, if non-nil, is an acronym string
24441 for the character. */
24442
24443 static void
24444 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24445 {
24446 int face_id;
24447 struct face *face;
24448 struct font *font;
24449 int base_width, base_height, width, height;
24450 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24451 int len;
24452
24453 /* Get the metrics of the base font. We always refer to the current
24454 ASCII face. */
24455 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24456 font = face->font ? face->font : FRAME_FONT (it->f);
24457 it->ascent = FONT_BASE (font) + font->baseline_offset;
24458 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24459 base_height = it->ascent + it->descent;
24460 base_width = font->average_width;
24461
24462 /* Get a face ID for the glyph by utilizing a cache (the same way as
24463 done for `escape-glyph' in get_next_display_element). */
24464 if (it->f == last_glyphless_glyph_frame
24465 && it->face_id == last_glyphless_glyph_face_id)
24466 {
24467 face_id = last_glyphless_glyph_merged_face_id;
24468 }
24469 else
24470 {
24471 /* Merge the `glyphless-char' face into the current face. */
24472 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24473 last_glyphless_glyph_frame = it->f;
24474 last_glyphless_glyph_face_id = it->face_id;
24475 last_glyphless_glyph_merged_face_id = face_id;
24476 }
24477
24478 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24479 {
24480 it->pixel_width = THIN_SPACE_WIDTH;
24481 len = 0;
24482 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24483 }
24484 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24485 {
24486 width = CHAR_WIDTH (it->c);
24487 if (width == 0)
24488 width = 1;
24489 else if (width > 4)
24490 width = 4;
24491 it->pixel_width = base_width * width;
24492 len = 0;
24493 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24494 }
24495 else
24496 {
24497 char buf[7];
24498 const char *str;
24499 unsigned int code[6];
24500 int upper_len;
24501 int ascent, descent;
24502 struct font_metrics metrics_upper, metrics_lower;
24503
24504 face = FACE_FROM_ID (it->f, face_id);
24505 font = face->font ? face->font : FRAME_FONT (it->f);
24506 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24507
24508 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24509 {
24510 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24511 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24512 if (CONSP (acronym))
24513 acronym = XCAR (acronym);
24514 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24515 }
24516 else
24517 {
24518 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24519 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24520 str = buf;
24521 }
24522 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24523 code[len] = font->driver->encode_char (font, str[len]);
24524 upper_len = (len + 1) / 2;
24525 font->driver->text_extents (font, code, upper_len,
24526 &metrics_upper);
24527 font->driver->text_extents (font, code + upper_len, len - upper_len,
24528 &metrics_lower);
24529
24530
24531
24532 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24533 width = max (metrics_upper.width, metrics_lower.width) + 4;
24534 upper_xoff = upper_yoff = 2; /* the typical case */
24535 if (base_width >= width)
24536 {
24537 /* Align the upper to the left, the lower to the right. */
24538 it->pixel_width = base_width;
24539 lower_xoff = base_width - 2 - metrics_lower.width;
24540 }
24541 else
24542 {
24543 /* Center the shorter one. */
24544 it->pixel_width = width;
24545 if (metrics_upper.width >= metrics_lower.width)
24546 lower_xoff = (width - metrics_lower.width) / 2;
24547 else
24548 {
24549 /* FIXME: This code doesn't look right. It formerly was
24550 missing the "lower_xoff = 0;", which couldn't have
24551 been right since it left lower_xoff uninitialized. */
24552 lower_xoff = 0;
24553 upper_xoff = (width - metrics_upper.width) / 2;
24554 }
24555 }
24556
24557 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24558 top, bottom, and between upper and lower strings. */
24559 height = (metrics_upper.ascent + metrics_upper.descent
24560 + metrics_lower.ascent + metrics_lower.descent) + 5;
24561 /* Center vertically.
24562 H:base_height, D:base_descent
24563 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24564
24565 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24566 descent = D - H/2 + h/2;
24567 lower_yoff = descent - 2 - ld;
24568 upper_yoff = lower_yoff - la - 1 - ud; */
24569 ascent = - (it->descent - (base_height + height + 1) / 2);
24570 descent = it->descent - (base_height - height) / 2;
24571 lower_yoff = descent - 2 - metrics_lower.descent;
24572 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24573 - metrics_upper.descent);
24574 /* Don't make the height shorter than the base height. */
24575 if (height > base_height)
24576 {
24577 it->ascent = ascent;
24578 it->descent = descent;
24579 }
24580 }
24581
24582 it->phys_ascent = it->ascent;
24583 it->phys_descent = it->descent;
24584 if (it->glyph_row)
24585 append_glyphless_glyph (it, face_id, for_no_font, len,
24586 upper_xoff, upper_yoff,
24587 lower_xoff, lower_yoff);
24588 it->nglyphs = 1;
24589 take_vertical_position_into_account (it);
24590 }
24591
24592
24593 /* RIF:
24594 Produce glyphs/get display metrics for the display element IT is
24595 loaded with. See the description of struct it in dispextern.h
24596 for an overview of struct it. */
24597
24598 void
24599 x_produce_glyphs (struct it *it)
24600 {
24601 int extra_line_spacing = it->extra_line_spacing;
24602
24603 it->glyph_not_available_p = 0;
24604
24605 if (it->what == IT_CHARACTER)
24606 {
24607 XChar2b char2b;
24608 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24609 struct font *font = face->font;
24610 struct font_metrics *pcm = NULL;
24611 int boff; /* baseline offset */
24612
24613 if (font == NULL)
24614 {
24615 /* When no suitable font is found, display this character by
24616 the method specified in the first extra slot of
24617 Vglyphless_char_display. */
24618 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24619
24620 eassert (it->what == IT_GLYPHLESS);
24621 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24622 goto done;
24623 }
24624
24625 boff = font->baseline_offset;
24626 if (font->vertical_centering)
24627 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24628
24629 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24630 {
24631 int stretched_p;
24632
24633 it->nglyphs = 1;
24634
24635 if (it->override_ascent >= 0)
24636 {
24637 it->ascent = it->override_ascent;
24638 it->descent = it->override_descent;
24639 boff = it->override_boff;
24640 }
24641 else
24642 {
24643 it->ascent = FONT_BASE (font) + boff;
24644 it->descent = FONT_DESCENT (font) - boff;
24645 }
24646
24647 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24648 {
24649 pcm = get_per_char_metric (font, &char2b);
24650 if (pcm->width == 0
24651 && pcm->rbearing == 0 && pcm->lbearing == 0)
24652 pcm = NULL;
24653 }
24654
24655 if (pcm)
24656 {
24657 it->phys_ascent = pcm->ascent + boff;
24658 it->phys_descent = pcm->descent - boff;
24659 it->pixel_width = pcm->width;
24660 }
24661 else
24662 {
24663 it->glyph_not_available_p = 1;
24664 it->phys_ascent = it->ascent;
24665 it->phys_descent = it->descent;
24666 it->pixel_width = font->space_width;
24667 }
24668
24669 if (it->constrain_row_ascent_descent_p)
24670 {
24671 if (it->descent > it->max_descent)
24672 {
24673 it->ascent += it->descent - it->max_descent;
24674 it->descent = it->max_descent;
24675 }
24676 if (it->ascent > it->max_ascent)
24677 {
24678 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24679 it->ascent = it->max_ascent;
24680 }
24681 it->phys_ascent = min (it->phys_ascent, it->ascent);
24682 it->phys_descent = min (it->phys_descent, it->descent);
24683 extra_line_spacing = 0;
24684 }
24685
24686 /* If this is a space inside a region of text with
24687 `space-width' property, change its width. */
24688 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24689 if (stretched_p)
24690 it->pixel_width *= XFLOATINT (it->space_width);
24691
24692 /* If face has a box, add the box thickness to the character
24693 height. If character has a box line to the left and/or
24694 right, add the box line width to the character's width. */
24695 if (face->box != FACE_NO_BOX)
24696 {
24697 int thick = face->box_line_width;
24698
24699 if (thick > 0)
24700 {
24701 it->ascent += thick;
24702 it->descent += thick;
24703 }
24704 else
24705 thick = -thick;
24706
24707 if (it->start_of_box_run_p)
24708 it->pixel_width += thick;
24709 if (it->end_of_box_run_p)
24710 it->pixel_width += thick;
24711 }
24712
24713 /* If face has an overline, add the height of the overline
24714 (1 pixel) and a 1 pixel margin to the character height. */
24715 if (face->overline_p)
24716 it->ascent += overline_margin;
24717
24718 if (it->constrain_row_ascent_descent_p)
24719 {
24720 if (it->ascent > it->max_ascent)
24721 it->ascent = it->max_ascent;
24722 if (it->descent > it->max_descent)
24723 it->descent = it->max_descent;
24724 }
24725
24726 take_vertical_position_into_account (it);
24727
24728 /* If we have to actually produce glyphs, do it. */
24729 if (it->glyph_row)
24730 {
24731 if (stretched_p)
24732 {
24733 /* Translate a space with a `space-width' property
24734 into a stretch glyph. */
24735 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24736 / FONT_HEIGHT (font));
24737 append_stretch_glyph (it, it->object, it->pixel_width,
24738 it->ascent + it->descent, ascent);
24739 }
24740 else
24741 append_glyph (it);
24742
24743 /* If characters with lbearing or rbearing are displayed
24744 in this line, record that fact in a flag of the
24745 glyph row. This is used to optimize X output code. */
24746 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24747 it->glyph_row->contains_overlapping_glyphs_p = 1;
24748 }
24749 if (! stretched_p && it->pixel_width == 0)
24750 /* We assure that all visible glyphs have at least 1-pixel
24751 width. */
24752 it->pixel_width = 1;
24753 }
24754 else if (it->char_to_display == '\n')
24755 {
24756 /* A newline has no width, but we need the height of the
24757 line. But if previous part of the line sets a height,
24758 don't increase that height */
24759
24760 Lisp_Object height;
24761 Lisp_Object total_height = Qnil;
24762
24763 it->override_ascent = -1;
24764 it->pixel_width = 0;
24765 it->nglyphs = 0;
24766
24767 height = get_it_property (it, Qline_height);
24768 /* Split (line-height total-height) list */
24769 if (CONSP (height)
24770 && CONSP (XCDR (height))
24771 && NILP (XCDR (XCDR (height))))
24772 {
24773 total_height = XCAR (XCDR (height));
24774 height = XCAR (height);
24775 }
24776 height = calc_line_height_property (it, height, font, boff, 1);
24777
24778 if (it->override_ascent >= 0)
24779 {
24780 it->ascent = it->override_ascent;
24781 it->descent = it->override_descent;
24782 boff = it->override_boff;
24783 }
24784 else
24785 {
24786 it->ascent = FONT_BASE (font) + boff;
24787 it->descent = FONT_DESCENT (font) - boff;
24788 }
24789
24790 if (EQ (height, Qt))
24791 {
24792 if (it->descent > it->max_descent)
24793 {
24794 it->ascent += it->descent - it->max_descent;
24795 it->descent = it->max_descent;
24796 }
24797 if (it->ascent > it->max_ascent)
24798 {
24799 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24800 it->ascent = it->max_ascent;
24801 }
24802 it->phys_ascent = min (it->phys_ascent, it->ascent);
24803 it->phys_descent = min (it->phys_descent, it->descent);
24804 it->constrain_row_ascent_descent_p = 1;
24805 extra_line_spacing = 0;
24806 }
24807 else
24808 {
24809 Lisp_Object spacing;
24810
24811 it->phys_ascent = it->ascent;
24812 it->phys_descent = it->descent;
24813
24814 if ((it->max_ascent > 0 || it->max_descent > 0)
24815 && face->box != FACE_NO_BOX
24816 && face->box_line_width > 0)
24817 {
24818 it->ascent += face->box_line_width;
24819 it->descent += face->box_line_width;
24820 }
24821 if (!NILP (height)
24822 && XINT (height) > it->ascent + it->descent)
24823 it->ascent = XINT (height) - it->descent;
24824
24825 if (!NILP (total_height))
24826 spacing = calc_line_height_property (it, total_height, font, boff, 0);
24827 else
24828 {
24829 spacing = get_it_property (it, Qline_spacing);
24830 spacing = calc_line_height_property (it, spacing, font, boff, 0);
24831 }
24832 if (INTEGERP (spacing))
24833 {
24834 extra_line_spacing = XINT (spacing);
24835 if (!NILP (total_height))
24836 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
24837 }
24838 }
24839 }
24840 else /* i.e. (it->char_to_display == '\t') */
24841 {
24842 if (font->space_width > 0)
24843 {
24844 int tab_width = it->tab_width * font->space_width;
24845 int x = it->current_x + it->continuation_lines_width;
24846 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
24847
24848 /* If the distance from the current position to the next tab
24849 stop is less than a space character width, use the
24850 tab stop after that. */
24851 if (next_tab_x - x < font->space_width)
24852 next_tab_x += tab_width;
24853
24854 it->pixel_width = next_tab_x - x;
24855 it->nglyphs = 1;
24856 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
24857 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
24858
24859 if (it->glyph_row)
24860 {
24861 append_stretch_glyph (it, it->object, it->pixel_width,
24862 it->ascent + it->descent, it->ascent);
24863 }
24864 }
24865 else
24866 {
24867 it->pixel_width = 0;
24868 it->nglyphs = 1;
24869 }
24870 }
24871 }
24872 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
24873 {
24874 /* A static composition.
24875
24876 Note: A composition is represented as one glyph in the
24877 glyph matrix. There are no padding glyphs.
24878
24879 Important note: pixel_width, ascent, and descent are the
24880 values of what is drawn by draw_glyphs (i.e. the values of
24881 the overall glyphs composed). */
24882 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24883 int boff; /* baseline offset */
24884 struct composition *cmp = composition_table[it->cmp_it.id];
24885 int glyph_len = cmp->glyph_len;
24886 struct font *font = face->font;
24887
24888 it->nglyphs = 1;
24889
24890 /* If we have not yet calculated pixel size data of glyphs of
24891 the composition for the current face font, calculate them
24892 now. Theoretically, we have to check all fonts for the
24893 glyphs, but that requires much time and memory space. So,
24894 here we check only the font of the first glyph. This may
24895 lead to incorrect display, but it's very rare, and C-l
24896 (recenter-top-bottom) can correct the display anyway. */
24897 if (! cmp->font || cmp->font != font)
24898 {
24899 /* Ascent and descent of the font of the first character
24900 of this composition (adjusted by baseline offset).
24901 Ascent and descent of overall glyphs should not be less
24902 than these, respectively. */
24903 int font_ascent, font_descent, font_height;
24904 /* Bounding box of the overall glyphs. */
24905 int leftmost, rightmost, lowest, highest;
24906 int lbearing, rbearing;
24907 int i, width, ascent, descent;
24908 int left_padded = 0, right_padded = 0;
24909 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
24910 XChar2b char2b;
24911 struct font_metrics *pcm;
24912 int font_not_found_p;
24913 ptrdiff_t pos;
24914
24915 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
24916 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
24917 break;
24918 if (glyph_len < cmp->glyph_len)
24919 right_padded = 1;
24920 for (i = 0; i < glyph_len; i++)
24921 {
24922 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
24923 break;
24924 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
24925 }
24926 if (i > 0)
24927 left_padded = 1;
24928
24929 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
24930 : IT_CHARPOS (*it));
24931 /* If no suitable font is found, use the default font. */
24932 font_not_found_p = font == NULL;
24933 if (font_not_found_p)
24934 {
24935 face = face->ascii_face;
24936 font = face->font;
24937 }
24938 boff = font->baseline_offset;
24939 if (font->vertical_centering)
24940 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24941 font_ascent = FONT_BASE (font) + boff;
24942 font_descent = FONT_DESCENT (font) - boff;
24943 font_height = FONT_HEIGHT (font);
24944
24945 cmp->font = font;
24946
24947 pcm = NULL;
24948 if (! font_not_found_p)
24949 {
24950 get_char_face_and_encoding (it->f, c, it->face_id,
24951 &char2b, 0);
24952 pcm = get_per_char_metric (font, &char2b);
24953 }
24954
24955 /* Initialize the bounding box. */
24956 if (pcm)
24957 {
24958 width = cmp->glyph_len > 0 ? pcm->width : 0;
24959 ascent = pcm->ascent;
24960 descent = pcm->descent;
24961 lbearing = pcm->lbearing;
24962 rbearing = pcm->rbearing;
24963 }
24964 else
24965 {
24966 width = cmp->glyph_len > 0 ? font->space_width : 0;
24967 ascent = FONT_BASE (font);
24968 descent = FONT_DESCENT (font);
24969 lbearing = 0;
24970 rbearing = width;
24971 }
24972
24973 rightmost = width;
24974 leftmost = 0;
24975 lowest = - descent + boff;
24976 highest = ascent + boff;
24977
24978 if (! font_not_found_p
24979 && font->default_ascent
24980 && CHAR_TABLE_P (Vuse_default_ascent)
24981 && !NILP (Faref (Vuse_default_ascent,
24982 make_number (it->char_to_display))))
24983 highest = font->default_ascent + boff;
24984
24985 /* Draw the first glyph at the normal position. It may be
24986 shifted to right later if some other glyphs are drawn
24987 at the left. */
24988 cmp->offsets[i * 2] = 0;
24989 cmp->offsets[i * 2 + 1] = boff;
24990 cmp->lbearing = lbearing;
24991 cmp->rbearing = rbearing;
24992
24993 /* Set cmp->offsets for the remaining glyphs. */
24994 for (i++; i < glyph_len; i++)
24995 {
24996 int left, right, btm, top;
24997 int ch = COMPOSITION_GLYPH (cmp, i);
24998 int face_id;
24999 struct face *this_face;
25000
25001 if (ch == '\t')
25002 ch = ' ';
25003 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25004 this_face = FACE_FROM_ID (it->f, face_id);
25005 font = this_face->font;
25006
25007 if (font == NULL)
25008 pcm = NULL;
25009 else
25010 {
25011 get_char_face_and_encoding (it->f, ch, face_id,
25012 &char2b, 0);
25013 pcm = get_per_char_metric (font, &char2b);
25014 }
25015 if (! pcm)
25016 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25017 else
25018 {
25019 width = pcm->width;
25020 ascent = pcm->ascent;
25021 descent = pcm->descent;
25022 lbearing = pcm->lbearing;
25023 rbearing = pcm->rbearing;
25024 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25025 {
25026 /* Relative composition with or without
25027 alternate chars. */
25028 left = (leftmost + rightmost - width) / 2;
25029 btm = - descent + boff;
25030 if (font->relative_compose
25031 && (! CHAR_TABLE_P (Vignore_relative_composition)
25032 || NILP (Faref (Vignore_relative_composition,
25033 make_number (ch)))))
25034 {
25035
25036 if (- descent >= font->relative_compose)
25037 /* One extra pixel between two glyphs. */
25038 btm = highest + 1;
25039 else if (ascent <= 0)
25040 /* One extra pixel between two glyphs. */
25041 btm = lowest - 1 - ascent - descent;
25042 }
25043 }
25044 else
25045 {
25046 /* A composition rule is specified by an integer
25047 value that encodes global and new reference
25048 points (GREF and NREF). GREF and NREF are
25049 specified by numbers as below:
25050
25051 0---1---2 -- ascent
25052 | |
25053 | |
25054 | |
25055 9--10--11 -- center
25056 | |
25057 ---3---4---5--- baseline
25058 | |
25059 6---7---8 -- descent
25060 */
25061 int rule = COMPOSITION_RULE (cmp, i);
25062 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25063
25064 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25065 grefx = gref % 3, nrefx = nref % 3;
25066 grefy = gref / 3, nrefy = nref / 3;
25067 if (xoff)
25068 xoff = font_height * (xoff - 128) / 256;
25069 if (yoff)
25070 yoff = font_height * (yoff - 128) / 256;
25071
25072 left = (leftmost
25073 + grefx * (rightmost - leftmost) / 2
25074 - nrefx * width / 2
25075 + xoff);
25076
25077 btm = ((grefy == 0 ? highest
25078 : grefy == 1 ? 0
25079 : grefy == 2 ? lowest
25080 : (highest + lowest) / 2)
25081 - (nrefy == 0 ? ascent + descent
25082 : nrefy == 1 ? descent - boff
25083 : nrefy == 2 ? 0
25084 : (ascent + descent) / 2)
25085 + yoff);
25086 }
25087
25088 cmp->offsets[i * 2] = left;
25089 cmp->offsets[i * 2 + 1] = btm + descent;
25090
25091 /* Update the bounding box of the overall glyphs. */
25092 if (width > 0)
25093 {
25094 right = left + width;
25095 if (left < leftmost)
25096 leftmost = left;
25097 if (right > rightmost)
25098 rightmost = right;
25099 }
25100 top = btm + descent + ascent;
25101 if (top > highest)
25102 highest = top;
25103 if (btm < lowest)
25104 lowest = btm;
25105
25106 if (cmp->lbearing > left + lbearing)
25107 cmp->lbearing = left + lbearing;
25108 if (cmp->rbearing < left + rbearing)
25109 cmp->rbearing = left + rbearing;
25110 }
25111 }
25112
25113 /* If there are glyphs whose x-offsets are negative,
25114 shift all glyphs to the right and make all x-offsets
25115 non-negative. */
25116 if (leftmost < 0)
25117 {
25118 for (i = 0; i < cmp->glyph_len; i++)
25119 cmp->offsets[i * 2] -= leftmost;
25120 rightmost -= leftmost;
25121 cmp->lbearing -= leftmost;
25122 cmp->rbearing -= leftmost;
25123 }
25124
25125 if (left_padded && cmp->lbearing < 0)
25126 {
25127 for (i = 0; i < cmp->glyph_len; i++)
25128 cmp->offsets[i * 2] -= cmp->lbearing;
25129 rightmost -= cmp->lbearing;
25130 cmp->rbearing -= cmp->lbearing;
25131 cmp->lbearing = 0;
25132 }
25133 if (right_padded && rightmost < cmp->rbearing)
25134 {
25135 rightmost = cmp->rbearing;
25136 }
25137
25138 cmp->pixel_width = rightmost;
25139 cmp->ascent = highest;
25140 cmp->descent = - lowest;
25141 if (cmp->ascent < font_ascent)
25142 cmp->ascent = font_ascent;
25143 if (cmp->descent < font_descent)
25144 cmp->descent = font_descent;
25145 }
25146
25147 if (it->glyph_row
25148 && (cmp->lbearing < 0
25149 || cmp->rbearing > cmp->pixel_width))
25150 it->glyph_row->contains_overlapping_glyphs_p = 1;
25151
25152 it->pixel_width = cmp->pixel_width;
25153 it->ascent = it->phys_ascent = cmp->ascent;
25154 it->descent = it->phys_descent = cmp->descent;
25155 if (face->box != FACE_NO_BOX)
25156 {
25157 int thick = face->box_line_width;
25158
25159 if (thick > 0)
25160 {
25161 it->ascent += thick;
25162 it->descent += thick;
25163 }
25164 else
25165 thick = - thick;
25166
25167 if (it->start_of_box_run_p)
25168 it->pixel_width += thick;
25169 if (it->end_of_box_run_p)
25170 it->pixel_width += thick;
25171 }
25172
25173 /* If face has an overline, add the height of the overline
25174 (1 pixel) and a 1 pixel margin to the character height. */
25175 if (face->overline_p)
25176 it->ascent += overline_margin;
25177
25178 take_vertical_position_into_account (it);
25179 if (it->ascent < 0)
25180 it->ascent = 0;
25181 if (it->descent < 0)
25182 it->descent = 0;
25183
25184 if (it->glyph_row && cmp->glyph_len > 0)
25185 append_composite_glyph (it);
25186 }
25187 else if (it->what == IT_COMPOSITION)
25188 {
25189 /* A dynamic (automatic) composition. */
25190 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25191 Lisp_Object gstring;
25192 struct font_metrics metrics;
25193
25194 it->nglyphs = 1;
25195
25196 gstring = composition_gstring_from_id (it->cmp_it.id);
25197 it->pixel_width
25198 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25199 &metrics);
25200 if (it->glyph_row
25201 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25202 it->glyph_row->contains_overlapping_glyphs_p = 1;
25203 it->ascent = it->phys_ascent = metrics.ascent;
25204 it->descent = it->phys_descent = metrics.descent;
25205 if (face->box != FACE_NO_BOX)
25206 {
25207 int thick = face->box_line_width;
25208
25209 if (thick > 0)
25210 {
25211 it->ascent += thick;
25212 it->descent += thick;
25213 }
25214 else
25215 thick = - thick;
25216
25217 if (it->start_of_box_run_p)
25218 it->pixel_width += thick;
25219 if (it->end_of_box_run_p)
25220 it->pixel_width += thick;
25221 }
25222 /* If face has an overline, add the height of the overline
25223 (1 pixel) and a 1 pixel margin to the character height. */
25224 if (face->overline_p)
25225 it->ascent += overline_margin;
25226 take_vertical_position_into_account (it);
25227 if (it->ascent < 0)
25228 it->ascent = 0;
25229 if (it->descent < 0)
25230 it->descent = 0;
25231
25232 if (it->glyph_row)
25233 append_composite_glyph (it);
25234 }
25235 else if (it->what == IT_GLYPHLESS)
25236 produce_glyphless_glyph (it, 0, Qnil);
25237 else if (it->what == IT_IMAGE)
25238 produce_image_glyph (it);
25239 else if (it->what == IT_STRETCH)
25240 produce_stretch_glyph (it);
25241
25242 done:
25243 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25244 because this isn't true for images with `:ascent 100'. */
25245 eassert (it->ascent >= 0 && it->descent >= 0);
25246 if (it->area == TEXT_AREA)
25247 it->current_x += it->pixel_width;
25248
25249 if (extra_line_spacing > 0)
25250 {
25251 it->descent += extra_line_spacing;
25252 if (extra_line_spacing > it->max_extra_line_spacing)
25253 it->max_extra_line_spacing = extra_line_spacing;
25254 }
25255
25256 it->max_ascent = max (it->max_ascent, it->ascent);
25257 it->max_descent = max (it->max_descent, it->descent);
25258 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25259 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25260 }
25261
25262 /* EXPORT for RIF:
25263 Output LEN glyphs starting at START at the nominal cursor position.
25264 Advance the nominal cursor over the text. The global variable
25265 updated_window contains the window being updated, updated_row is
25266 the glyph row being updated, and updated_area is the area of that
25267 row being updated. */
25268
25269 void
25270 x_write_glyphs (struct glyph *start, int len)
25271 {
25272 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25273
25274 eassert (updated_window && updated_row);
25275 /* When the window is hscrolled, cursor hpos can legitimately be out
25276 of bounds, but we draw the cursor at the corresponding window
25277 margin in that case. */
25278 if (!updated_row->reversed_p && chpos < 0)
25279 chpos = 0;
25280 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25281 chpos = updated_row->used[TEXT_AREA] - 1;
25282
25283 block_input ();
25284
25285 /* Write glyphs. */
25286
25287 hpos = start - updated_row->glyphs[updated_area];
25288 x = draw_glyphs (updated_window, output_cursor.x,
25289 updated_row, updated_area,
25290 hpos, hpos + len,
25291 DRAW_NORMAL_TEXT, 0);
25292
25293 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25294 if (updated_area == TEXT_AREA
25295 && updated_window->phys_cursor_on_p
25296 && updated_window->phys_cursor.vpos == output_cursor.vpos
25297 && chpos >= hpos
25298 && chpos < hpos + len)
25299 updated_window->phys_cursor_on_p = 0;
25300
25301 unblock_input ();
25302
25303 /* Advance the output cursor. */
25304 output_cursor.hpos += len;
25305 output_cursor.x = x;
25306 }
25307
25308
25309 /* EXPORT for RIF:
25310 Insert LEN glyphs from START at the nominal cursor position. */
25311
25312 void
25313 x_insert_glyphs (struct glyph *start, int len)
25314 {
25315 struct frame *f;
25316 struct window *w;
25317 int line_height, shift_by_width, shifted_region_width;
25318 struct glyph_row *row;
25319 struct glyph *glyph;
25320 int frame_x, frame_y;
25321 ptrdiff_t hpos;
25322
25323 eassert (updated_window && updated_row);
25324 block_input ();
25325 w = updated_window;
25326 f = XFRAME (WINDOW_FRAME (w));
25327
25328 /* Get the height of the line we are in. */
25329 row = updated_row;
25330 line_height = row->height;
25331
25332 /* Get the width of the glyphs to insert. */
25333 shift_by_width = 0;
25334 for (glyph = start; glyph < start + len; ++glyph)
25335 shift_by_width += glyph->pixel_width;
25336
25337 /* Get the width of the region to shift right. */
25338 shifted_region_width = (window_box_width (w, updated_area)
25339 - output_cursor.x
25340 - shift_by_width);
25341
25342 /* Shift right. */
25343 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25344 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25345
25346 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25347 line_height, shift_by_width);
25348
25349 /* Write the glyphs. */
25350 hpos = start - row->glyphs[updated_area];
25351 draw_glyphs (w, output_cursor.x, row, updated_area,
25352 hpos, hpos + len,
25353 DRAW_NORMAL_TEXT, 0);
25354
25355 /* Advance the output cursor. */
25356 output_cursor.hpos += len;
25357 output_cursor.x += shift_by_width;
25358 unblock_input ();
25359 }
25360
25361
25362 /* EXPORT for RIF:
25363 Erase the current text line from the nominal cursor position
25364 (inclusive) to pixel column TO_X (exclusive). The idea is that
25365 everything from TO_X onward is already erased.
25366
25367 TO_X is a pixel position relative to updated_area of
25368 updated_window. TO_X == -1 means clear to the end of this area. */
25369
25370 void
25371 x_clear_end_of_line (int to_x)
25372 {
25373 struct frame *f;
25374 struct window *w = updated_window;
25375 int max_x, min_y, max_y;
25376 int from_x, from_y, to_y;
25377
25378 eassert (updated_window && updated_row);
25379 f = XFRAME (w->frame);
25380
25381 if (updated_row->full_width_p)
25382 max_x = WINDOW_TOTAL_WIDTH (w);
25383 else
25384 max_x = window_box_width (w, updated_area);
25385 max_y = window_text_bottom_y (w);
25386
25387 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25388 of window. For TO_X > 0, truncate to end of drawing area. */
25389 if (to_x == 0)
25390 return;
25391 else if (to_x < 0)
25392 to_x = max_x;
25393 else
25394 to_x = min (to_x, max_x);
25395
25396 to_y = min (max_y, output_cursor.y + updated_row->height);
25397
25398 /* Notice if the cursor will be cleared by this operation. */
25399 if (!updated_row->full_width_p)
25400 notice_overwritten_cursor (w, updated_area,
25401 output_cursor.x, -1,
25402 updated_row->y,
25403 MATRIX_ROW_BOTTOM_Y (updated_row));
25404
25405 from_x = output_cursor.x;
25406
25407 /* Translate to frame coordinates. */
25408 if (updated_row->full_width_p)
25409 {
25410 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25411 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25412 }
25413 else
25414 {
25415 int area_left = window_box_left (w, updated_area);
25416 from_x += area_left;
25417 to_x += area_left;
25418 }
25419
25420 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25421 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25422 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25423
25424 /* Prevent inadvertently clearing to end of the X window. */
25425 if (to_x > from_x && to_y > from_y)
25426 {
25427 block_input ();
25428 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25429 to_x - from_x, to_y - from_y);
25430 unblock_input ();
25431 }
25432 }
25433
25434 #endif /* HAVE_WINDOW_SYSTEM */
25435
25436
25437 \f
25438 /***********************************************************************
25439 Cursor types
25440 ***********************************************************************/
25441
25442 /* Value is the internal representation of the specified cursor type
25443 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25444 of the bar cursor. */
25445
25446 static enum text_cursor_kinds
25447 get_specified_cursor_type (Lisp_Object arg, int *width)
25448 {
25449 enum text_cursor_kinds type;
25450
25451 if (NILP (arg))
25452 return NO_CURSOR;
25453
25454 if (EQ (arg, Qbox))
25455 return FILLED_BOX_CURSOR;
25456
25457 if (EQ (arg, Qhollow))
25458 return HOLLOW_BOX_CURSOR;
25459
25460 if (EQ (arg, Qbar))
25461 {
25462 *width = 2;
25463 return BAR_CURSOR;
25464 }
25465
25466 if (CONSP (arg)
25467 && EQ (XCAR (arg), Qbar)
25468 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25469 {
25470 *width = XINT (XCDR (arg));
25471 return BAR_CURSOR;
25472 }
25473
25474 if (EQ (arg, Qhbar))
25475 {
25476 *width = 2;
25477 return HBAR_CURSOR;
25478 }
25479
25480 if (CONSP (arg)
25481 && EQ (XCAR (arg), Qhbar)
25482 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25483 {
25484 *width = XINT (XCDR (arg));
25485 return HBAR_CURSOR;
25486 }
25487
25488 /* Treat anything unknown as "hollow box cursor".
25489 It was bad to signal an error; people have trouble fixing
25490 .Xdefaults with Emacs, when it has something bad in it. */
25491 type = HOLLOW_BOX_CURSOR;
25492
25493 return type;
25494 }
25495
25496 /* Set the default cursor types for specified frame. */
25497 void
25498 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25499 {
25500 int width = 1;
25501 Lisp_Object tem;
25502
25503 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25504 FRAME_CURSOR_WIDTH (f) = width;
25505
25506 /* By default, set up the blink-off state depending on the on-state. */
25507
25508 tem = Fassoc (arg, Vblink_cursor_alist);
25509 if (!NILP (tem))
25510 {
25511 FRAME_BLINK_OFF_CURSOR (f)
25512 = get_specified_cursor_type (XCDR (tem), &width);
25513 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25514 }
25515 else
25516 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25517 }
25518
25519
25520 #ifdef HAVE_WINDOW_SYSTEM
25521
25522 /* Return the cursor we want to be displayed in window W. Return
25523 width of bar/hbar cursor through WIDTH arg. Return with
25524 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25525 (i.e. if the `system caret' should track this cursor).
25526
25527 In a mini-buffer window, we want the cursor only to appear if we
25528 are reading input from this window. For the selected window, we
25529 want the cursor type given by the frame parameter or buffer local
25530 setting of cursor-type. If explicitly marked off, draw no cursor.
25531 In all other cases, we want a hollow box cursor. */
25532
25533 static enum text_cursor_kinds
25534 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25535 int *active_cursor)
25536 {
25537 struct frame *f = XFRAME (w->frame);
25538 struct buffer *b = XBUFFER (w->contents);
25539 int cursor_type = DEFAULT_CURSOR;
25540 Lisp_Object alt_cursor;
25541 int non_selected = 0;
25542
25543 *active_cursor = 1;
25544
25545 /* Echo area */
25546 if (cursor_in_echo_area
25547 && FRAME_HAS_MINIBUF_P (f)
25548 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25549 {
25550 if (w == XWINDOW (echo_area_window))
25551 {
25552 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25553 {
25554 *width = FRAME_CURSOR_WIDTH (f);
25555 return FRAME_DESIRED_CURSOR (f);
25556 }
25557 else
25558 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25559 }
25560
25561 *active_cursor = 0;
25562 non_selected = 1;
25563 }
25564
25565 /* Detect a nonselected window or nonselected frame. */
25566 else if (w != XWINDOW (f->selected_window)
25567 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25568 {
25569 *active_cursor = 0;
25570
25571 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25572 return NO_CURSOR;
25573
25574 non_selected = 1;
25575 }
25576
25577 /* Never display a cursor in a window in which cursor-type is nil. */
25578 if (NILP (BVAR (b, cursor_type)))
25579 return NO_CURSOR;
25580
25581 /* Get the normal cursor type for this window. */
25582 if (EQ (BVAR (b, cursor_type), Qt))
25583 {
25584 cursor_type = FRAME_DESIRED_CURSOR (f);
25585 *width = FRAME_CURSOR_WIDTH (f);
25586 }
25587 else
25588 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25589
25590 /* Use cursor-in-non-selected-windows instead
25591 for non-selected window or frame. */
25592 if (non_selected)
25593 {
25594 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25595 if (!EQ (Qt, alt_cursor))
25596 return get_specified_cursor_type (alt_cursor, width);
25597 /* t means modify the normal cursor type. */
25598 if (cursor_type == FILLED_BOX_CURSOR)
25599 cursor_type = HOLLOW_BOX_CURSOR;
25600 else if (cursor_type == BAR_CURSOR && *width > 1)
25601 --*width;
25602 return cursor_type;
25603 }
25604
25605 /* Use normal cursor if not blinked off. */
25606 if (!w->cursor_off_p)
25607 {
25608 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25609 {
25610 if (cursor_type == FILLED_BOX_CURSOR)
25611 {
25612 /* Using a block cursor on large images can be very annoying.
25613 So use a hollow cursor for "large" images.
25614 If image is not transparent (no mask), also use hollow cursor. */
25615 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25616 if (img != NULL && IMAGEP (img->spec))
25617 {
25618 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25619 where N = size of default frame font size.
25620 This should cover most of the "tiny" icons people may use. */
25621 if (!img->mask
25622 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25623 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25624 cursor_type = HOLLOW_BOX_CURSOR;
25625 }
25626 }
25627 else if (cursor_type != NO_CURSOR)
25628 {
25629 /* Display current only supports BOX and HOLLOW cursors for images.
25630 So for now, unconditionally use a HOLLOW cursor when cursor is
25631 not a solid box cursor. */
25632 cursor_type = HOLLOW_BOX_CURSOR;
25633 }
25634 }
25635 return cursor_type;
25636 }
25637
25638 /* Cursor is blinked off, so determine how to "toggle" it. */
25639
25640 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25641 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25642 return get_specified_cursor_type (XCDR (alt_cursor), width);
25643
25644 /* Then see if frame has specified a specific blink off cursor type. */
25645 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25646 {
25647 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25648 return FRAME_BLINK_OFF_CURSOR (f);
25649 }
25650
25651 #if 0
25652 /* Some people liked having a permanently visible blinking cursor,
25653 while others had very strong opinions against it. So it was
25654 decided to remove it. KFS 2003-09-03 */
25655
25656 /* Finally perform built-in cursor blinking:
25657 filled box <-> hollow box
25658 wide [h]bar <-> narrow [h]bar
25659 narrow [h]bar <-> no cursor
25660 other type <-> no cursor */
25661
25662 if (cursor_type == FILLED_BOX_CURSOR)
25663 return HOLLOW_BOX_CURSOR;
25664
25665 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25666 {
25667 *width = 1;
25668 return cursor_type;
25669 }
25670 #endif
25671
25672 return NO_CURSOR;
25673 }
25674
25675
25676 /* Notice when the text cursor of window W has been completely
25677 overwritten by a drawing operation that outputs glyphs in AREA
25678 starting at X0 and ending at X1 in the line starting at Y0 and
25679 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25680 the rest of the line after X0 has been written. Y coordinates
25681 are window-relative. */
25682
25683 static void
25684 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25685 int x0, int x1, int y0, int y1)
25686 {
25687 int cx0, cx1, cy0, cy1;
25688 struct glyph_row *row;
25689
25690 if (!w->phys_cursor_on_p)
25691 return;
25692 if (area != TEXT_AREA)
25693 return;
25694
25695 if (w->phys_cursor.vpos < 0
25696 || w->phys_cursor.vpos >= w->current_matrix->nrows
25697 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25698 !(row->enabled_p && MATRIX_ROW_DISPLAYS_TEXT_P (row))))
25699 return;
25700
25701 if (row->cursor_in_fringe_p)
25702 {
25703 row->cursor_in_fringe_p = 0;
25704 draw_fringe_bitmap (w, row, row->reversed_p);
25705 w->phys_cursor_on_p = 0;
25706 return;
25707 }
25708
25709 cx0 = w->phys_cursor.x;
25710 cx1 = cx0 + w->phys_cursor_width;
25711 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25712 return;
25713
25714 /* The cursor image will be completely removed from the
25715 screen if the output area intersects the cursor area in
25716 y-direction. When we draw in [y0 y1[, and some part of
25717 the cursor is at y < y0, that part must have been drawn
25718 before. When scrolling, the cursor is erased before
25719 actually scrolling, so we don't come here. When not
25720 scrolling, the rows above the old cursor row must have
25721 changed, and in this case these rows must have written
25722 over the cursor image.
25723
25724 Likewise if part of the cursor is below y1, with the
25725 exception of the cursor being in the first blank row at
25726 the buffer and window end because update_text_area
25727 doesn't draw that row. (Except when it does, but
25728 that's handled in update_text_area.) */
25729
25730 cy0 = w->phys_cursor.y;
25731 cy1 = cy0 + w->phys_cursor_height;
25732 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25733 return;
25734
25735 w->phys_cursor_on_p = 0;
25736 }
25737
25738 #endif /* HAVE_WINDOW_SYSTEM */
25739
25740 \f
25741 /************************************************************************
25742 Mouse Face
25743 ************************************************************************/
25744
25745 #ifdef HAVE_WINDOW_SYSTEM
25746
25747 /* EXPORT for RIF:
25748 Fix the display of area AREA of overlapping row ROW in window W
25749 with respect to the overlapping part OVERLAPS. */
25750
25751 void
25752 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25753 enum glyph_row_area area, int overlaps)
25754 {
25755 int i, x;
25756
25757 block_input ();
25758
25759 x = 0;
25760 for (i = 0; i < row->used[area];)
25761 {
25762 if (row->glyphs[area][i].overlaps_vertically_p)
25763 {
25764 int start = i, start_x = x;
25765
25766 do
25767 {
25768 x += row->glyphs[area][i].pixel_width;
25769 ++i;
25770 }
25771 while (i < row->used[area]
25772 && row->glyphs[area][i].overlaps_vertically_p);
25773
25774 draw_glyphs (w, start_x, row, area,
25775 start, i,
25776 DRAW_NORMAL_TEXT, overlaps);
25777 }
25778 else
25779 {
25780 x += row->glyphs[area][i].pixel_width;
25781 ++i;
25782 }
25783 }
25784
25785 unblock_input ();
25786 }
25787
25788
25789 /* EXPORT:
25790 Draw the cursor glyph of window W in glyph row ROW. See the
25791 comment of draw_glyphs for the meaning of HL. */
25792
25793 void
25794 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25795 enum draw_glyphs_face hl)
25796 {
25797 /* If cursor hpos is out of bounds, don't draw garbage. This can
25798 happen in mini-buffer windows when switching between echo area
25799 glyphs and mini-buffer. */
25800 if ((row->reversed_p
25801 ? (w->phys_cursor.hpos >= 0)
25802 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25803 {
25804 int on_p = w->phys_cursor_on_p;
25805 int x1;
25806 int hpos = w->phys_cursor.hpos;
25807
25808 /* When the window is hscrolled, cursor hpos can legitimately be
25809 out of bounds, but we draw the cursor at the corresponding
25810 window margin in that case. */
25811 if (!row->reversed_p && hpos < 0)
25812 hpos = 0;
25813 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25814 hpos = row->used[TEXT_AREA] - 1;
25815
25816 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25817 hl, 0);
25818 w->phys_cursor_on_p = on_p;
25819
25820 if (hl == DRAW_CURSOR)
25821 w->phys_cursor_width = x1 - w->phys_cursor.x;
25822 /* When we erase the cursor, and ROW is overlapped by other
25823 rows, make sure that these overlapping parts of other rows
25824 are redrawn. */
25825 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
25826 {
25827 w->phys_cursor_width = x1 - w->phys_cursor.x;
25828
25829 if (row > w->current_matrix->rows
25830 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
25831 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
25832 OVERLAPS_ERASED_CURSOR);
25833
25834 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
25835 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
25836 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
25837 OVERLAPS_ERASED_CURSOR);
25838 }
25839 }
25840 }
25841
25842
25843 /* EXPORT:
25844 Erase the image of a cursor of window W from the screen. */
25845
25846 void
25847 erase_phys_cursor (struct window *w)
25848 {
25849 struct frame *f = XFRAME (w->frame);
25850 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
25851 int hpos = w->phys_cursor.hpos;
25852 int vpos = w->phys_cursor.vpos;
25853 int mouse_face_here_p = 0;
25854 struct glyph_matrix *active_glyphs = w->current_matrix;
25855 struct glyph_row *cursor_row;
25856 struct glyph *cursor_glyph;
25857 enum draw_glyphs_face hl;
25858
25859 /* No cursor displayed or row invalidated => nothing to do on the
25860 screen. */
25861 if (w->phys_cursor_type == NO_CURSOR)
25862 goto mark_cursor_off;
25863
25864 /* VPOS >= active_glyphs->nrows means that window has been resized.
25865 Don't bother to erase the cursor. */
25866 if (vpos >= active_glyphs->nrows)
25867 goto mark_cursor_off;
25868
25869 /* If row containing cursor is marked invalid, there is nothing we
25870 can do. */
25871 cursor_row = MATRIX_ROW (active_glyphs, vpos);
25872 if (!cursor_row->enabled_p)
25873 goto mark_cursor_off;
25874
25875 /* If line spacing is > 0, old cursor may only be partially visible in
25876 window after split-window. So adjust visible height. */
25877 cursor_row->visible_height = min (cursor_row->visible_height,
25878 window_text_bottom_y (w) - cursor_row->y);
25879
25880 /* If row is completely invisible, don't attempt to delete a cursor which
25881 isn't there. This can happen if cursor is at top of a window, and
25882 we switch to a buffer with a header line in that window. */
25883 if (cursor_row->visible_height <= 0)
25884 goto mark_cursor_off;
25885
25886 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
25887 if (cursor_row->cursor_in_fringe_p)
25888 {
25889 cursor_row->cursor_in_fringe_p = 0;
25890 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
25891 goto mark_cursor_off;
25892 }
25893
25894 /* This can happen when the new row is shorter than the old one.
25895 In this case, either draw_glyphs or clear_end_of_line
25896 should have cleared the cursor. Note that we wouldn't be
25897 able to erase the cursor in this case because we don't have a
25898 cursor glyph at hand. */
25899 if ((cursor_row->reversed_p
25900 ? (w->phys_cursor.hpos < 0)
25901 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
25902 goto mark_cursor_off;
25903
25904 /* When the window is hscrolled, cursor hpos can legitimately be out
25905 of bounds, but we draw the cursor at the corresponding window
25906 margin in that case. */
25907 if (!cursor_row->reversed_p && hpos < 0)
25908 hpos = 0;
25909 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
25910 hpos = cursor_row->used[TEXT_AREA] - 1;
25911
25912 /* If the cursor is in the mouse face area, redisplay that when
25913 we clear the cursor. */
25914 if (! NILP (hlinfo->mouse_face_window)
25915 && coords_in_mouse_face_p (w, hpos, vpos)
25916 /* Don't redraw the cursor's spot in mouse face if it is at the
25917 end of a line (on a newline). The cursor appears there, but
25918 mouse highlighting does not. */
25919 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
25920 mouse_face_here_p = 1;
25921
25922 /* Maybe clear the display under the cursor. */
25923 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
25924 {
25925 int x, y, left_x;
25926 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
25927 int width;
25928
25929 cursor_glyph = get_phys_cursor_glyph (w);
25930 if (cursor_glyph == NULL)
25931 goto mark_cursor_off;
25932
25933 width = cursor_glyph->pixel_width;
25934 left_x = window_box_left_offset (w, TEXT_AREA);
25935 x = w->phys_cursor.x;
25936 if (x < left_x)
25937 width -= left_x - x;
25938 width = min (width, window_box_width (w, TEXT_AREA) - x);
25939 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
25940 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
25941
25942 if (width > 0)
25943 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
25944 }
25945
25946 /* Erase the cursor by redrawing the character underneath it. */
25947 if (mouse_face_here_p)
25948 hl = DRAW_MOUSE_FACE;
25949 else
25950 hl = DRAW_NORMAL_TEXT;
25951 draw_phys_cursor_glyph (w, cursor_row, hl);
25952
25953 mark_cursor_off:
25954 w->phys_cursor_on_p = 0;
25955 w->phys_cursor_type = NO_CURSOR;
25956 }
25957
25958
25959 /* EXPORT:
25960 Display or clear cursor of window W. If ON is zero, clear the
25961 cursor. If it is non-zero, display the cursor. If ON is nonzero,
25962 where to put the cursor is specified by HPOS, VPOS, X and Y. */
25963
25964 void
25965 display_and_set_cursor (struct window *w, int on,
25966 int hpos, int vpos, int x, int y)
25967 {
25968 struct frame *f = XFRAME (w->frame);
25969 int new_cursor_type;
25970 int new_cursor_width;
25971 int active_cursor;
25972 struct glyph_row *glyph_row;
25973 struct glyph *glyph;
25974
25975 /* This is pointless on invisible frames, and dangerous on garbaged
25976 windows and frames; in the latter case, the frame or window may
25977 be in the midst of changing its size, and x and y may be off the
25978 window. */
25979 if (! FRAME_VISIBLE_P (f)
25980 || FRAME_GARBAGED_P (f)
25981 || vpos >= w->current_matrix->nrows
25982 || hpos >= w->current_matrix->matrix_w)
25983 return;
25984
25985 /* If cursor is off and we want it off, return quickly. */
25986 if (!on && !w->phys_cursor_on_p)
25987 return;
25988
25989 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
25990 /* If cursor row is not enabled, we don't really know where to
25991 display the cursor. */
25992 if (!glyph_row->enabled_p)
25993 {
25994 w->phys_cursor_on_p = 0;
25995 return;
25996 }
25997
25998 glyph = NULL;
25999 if (!glyph_row->exact_window_width_line_p
26000 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26001 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26002
26003 eassert (input_blocked_p ());
26004
26005 /* Set new_cursor_type to the cursor we want to be displayed. */
26006 new_cursor_type = get_window_cursor_type (w, glyph,
26007 &new_cursor_width, &active_cursor);
26008
26009 /* If cursor is currently being shown and we don't want it to be or
26010 it is in the wrong place, or the cursor type is not what we want,
26011 erase it. */
26012 if (w->phys_cursor_on_p
26013 && (!on
26014 || w->phys_cursor.x != x
26015 || w->phys_cursor.y != y
26016 || new_cursor_type != w->phys_cursor_type
26017 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26018 && new_cursor_width != w->phys_cursor_width)))
26019 erase_phys_cursor (w);
26020
26021 /* Don't check phys_cursor_on_p here because that flag is only set
26022 to zero in some cases where we know that the cursor has been
26023 completely erased, to avoid the extra work of erasing the cursor
26024 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26025 still not be visible, or it has only been partly erased. */
26026 if (on)
26027 {
26028 w->phys_cursor_ascent = glyph_row->ascent;
26029 w->phys_cursor_height = glyph_row->height;
26030
26031 /* Set phys_cursor_.* before x_draw_.* is called because some
26032 of them may need the information. */
26033 w->phys_cursor.x = x;
26034 w->phys_cursor.y = glyph_row->y;
26035 w->phys_cursor.hpos = hpos;
26036 w->phys_cursor.vpos = vpos;
26037 }
26038
26039 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26040 new_cursor_type, new_cursor_width,
26041 on, active_cursor);
26042 }
26043
26044
26045 /* Switch the display of W's cursor on or off, according to the value
26046 of ON. */
26047
26048 static void
26049 update_window_cursor (struct window *w, int on)
26050 {
26051 /* Don't update cursor in windows whose frame is in the process
26052 of being deleted. */
26053 if (w->current_matrix)
26054 {
26055 int hpos = w->phys_cursor.hpos;
26056 int vpos = w->phys_cursor.vpos;
26057 struct glyph_row *row;
26058
26059 if (vpos >= w->current_matrix->nrows
26060 || hpos >= w->current_matrix->matrix_w)
26061 return;
26062
26063 row = MATRIX_ROW (w->current_matrix, vpos);
26064
26065 /* When the window is hscrolled, cursor hpos can legitimately be
26066 out of bounds, but we draw the cursor at the corresponding
26067 window margin in that case. */
26068 if (!row->reversed_p && hpos < 0)
26069 hpos = 0;
26070 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26071 hpos = row->used[TEXT_AREA] - 1;
26072
26073 block_input ();
26074 display_and_set_cursor (w, on, hpos, vpos,
26075 w->phys_cursor.x, w->phys_cursor.y);
26076 unblock_input ();
26077 }
26078 }
26079
26080
26081 /* Call update_window_cursor with parameter ON_P on all leaf windows
26082 in the window tree rooted at W. */
26083
26084 static void
26085 update_cursor_in_window_tree (struct window *w, int on_p)
26086 {
26087 while (w)
26088 {
26089 if (WINDOWP (w->contents))
26090 update_cursor_in_window_tree (XWINDOW (w->contents), on_p);
26091 else
26092 update_window_cursor (w, on_p);
26093
26094 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26095 }
26096 }
26097
26098
26099 /* EXPORT:
26100 Display the cursor on window W, or clear it, according to ON_P.
26101 Don't change the cursor's position. */
26102
26103 void
26104 x_update_cursor (struct frame *f, int on_p)
26105 {
26106 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26107 }
26108
26109
26110 /* EXPORT:
26111 Clear the cursor of window W to background color, and mark the
26112 cursor as not shown. This is used when the text where the cursor
26113 is about to be rewritten. */
26114
26115 void
26116 x_clear_cursor (struct window *w)
26117 {
26118 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26119 update_window_cursor (w, 0);
26120 }
26121
26122 #endif /* HAVE_WINDOW_SYSTEM */
26123
26124 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26125 and MSDOS. */
26126 static void
26127 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26128 int start_hpos, int end_hpos,
26129 enum draw_glyphs_face draw)
26130 {
26131 #ifdef HAVE_WINDOW_SYSTEM
26132 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26133 {
26134 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26135 return;
26136 }
26137 #endif
26138 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26139 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26140 #endif
26141 }
26142
26143 /* Display the active region described by mouse_face_* according to DRAW. */
26144
26145 static void
26146 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26147 {
26148 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26149 struct frame *f = XFRAME (WINDOW_FRAME (w));
26150
26151 if (/* If window is in the process of being destroyed, don't bother
26152 to do anything. */
26153 w->current_matrix != NULL
26154 /* Don't update mouse highlight if hidden */
26155 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26156 /* Recognize when we are called to operate on rows that don't exist
26157 anymore. This can happen when a window is split. */
26158 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26159 {
26160 int phys_cursor_on_p = w->phys_cursor_on_p;
26161 struct glyph_row *row, *first, *last;
26162
26163 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26164 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26165
26166 for (row = first; row <= last && row->enabled_p; ++row)
26167 {
26168 int start_hpos, end_hpos, start_x;
26169
26170 /* For all but the first row, the highlight starts at column 0. */
26171 if (row == first)
26172 {
26173 /* R2L rows have BEG and END in reversed order, but the
26174 screen drawing geometry is always left to right. So
26175 we need to mirror the beginning and end of the
26176 highlighted area in R2L rows. */
26177 if (!row->reversed_p)
26178 {
26179 start_hpos = hlinfo->mouse_face_beg_col;
26180 start_x = hlinfo->mouse_face_beg_x;
26181 }
26182 else if (row == last)
26183 {
26184 start_hpos = hlinfo->mouse_face_end_col;
26185 start_x = hlinfo->mouse_face_end_x;
26186 }
26187 else
26188 {
26189 start_hpos = 0;
26190 start_x = 0;
26191 }
26192 }
26193 else if (row->reversed_p && row == last)
26194 {
26195 start_hpos = hlinfo->mouse_face_end_col;
26196 start_x = hlinfo->mouse_face_end_x;
26197 }
26198 else
26199 {
26200 start_hpos = 0;
26201 start_x = 0;
26202 }
26203
26204 if (row == last)
26205 {
26206 if (!row->reversed_p)
26207 end_hpos = hlinfo->mouse_face_end_col;
26208 else if (row == first)
26209 end_hpos = hlinfo->mouse_face_beg_col;
26210 else
26211 {
26212 end_hpos = row->used[TEXT_AREA];
26213 if (draw == DRAW_NORMAL_TEXT)
26214 row->fill_line_p = 1; /* Clear to end of line */
26215 }
26216 }
26217 else if (row->reversed_p && row == first)
26218 end_hpos = hlinfo->mouse_face_beg_col;
26219 else
26220 {
26221 end_hpos = row->used[TEXT_AREA];
26222 if (draw == DRAW_NORMAL_TEXT)
26223 row->fill_line_p = 1; /* Clear to end of line */
26224 }
26225
26226 if (end_hpos > start_hpos)
26227 {
26228 draw_row_with_mouse_face (w, start_x, row,
26229 start_hpos, end_hpos, draw);
26230
26231 row->mouse_face_p
26232 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26233 }
26234 }
26235
26236 #ifdef HAVE_WINDOW_SYSTEM
26237 /* When we've written over the cursor, arrange for it to
26238 be displayed again. */
26239 if (FRAME_WINDOW_P (f)
26240 && phys_cursor_on_p && !w->phys_cursor_on_p)
26241 {
26242 int hpos = w->phys_cursor.hpos;
26243
26244 /* When the window is hscrolled, cursor hpos can legitimately be
26245 out of bounds, but we draw the cursor at the corresponding
26246 window margin in that case. */
26247 if (!row->reversed_p && hpos < 0)
26248 hpos = 0;
26249 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26250 hpos = row->used[TEXT_AREA] - 1;
26251
26252 block_input ();
26253 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26254 w->phys_cursor.x, w->phys_cursor.y);
26255 unblock_input ();
26256 }
26257 #endif /* HAVE_WINDOW_SYSTEM */
26258 }
26259
26260 #ifdef HAVE_WINDOW_SYSTEM
26261 /* Change the mouse cursor. */
26262 if (FRAME_WINDOW_P (f))
26263 {
26264 if (draw == DRAW_NORMAL_TEXT
26265 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26266 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26267 else if (draw == DRAW_MOUSE_FACE)
26268 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26269 else
26270 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26271 }
26272 #endif /* HAVE_WINDOW_SYSTEM */
26273 }
26274
26275 /* EXPORT:
26276 Clear out the mouse-highlighted active region.
26277 Redraw it un-highlighted first. Value is non-zero if mouse
26278 face was actually drawn unhighlighted. */
26279
26280 int
26281 clear_mouse_face (Mouse_HLInfo *hlinfo)
26282 {
26283 int cleared = 0;
26284
26285 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26286 {
26287 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26288 cleared = 1;
26289 }
26290
26291 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26292 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26293 hlinfo->mouse_face_window = Qnil;
26294 hlinfo->mouse_face_overlay = Qnil;
26295 return cleared;
26296 }
26297
26298 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26299 within the mouse face on that window. */
26300 static int
26301 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26302 {
26303 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26304
26305 /* Quickly resolve the easy cases. */
26306 if (!(WINDOWP (hlinfo->mouse_face_window)
26307 && XWINDOW (hlinfo->mouse_face_window) == w))
26308 return 0;
26309 if (vpos < hlinfo->mouse_face_beg_row
26310 || vpos > hlinfo->mouse_face_end_row)
26311 return 0;
26312 if (vpos > hlinfo->mouse_face_beg_row
26313 && vpos < hlinfo->mouse_face_end_row)
26314 return 1;
26315
26316 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26317 {
26318 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26319 {
26320 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26321 return 1;
26322 }
26323 else if ((vpos == hlinfo->mouse_face_beg_row
26324 && hpos >= hlinfo->mouse_face_beg_col)
26325 || (vpos == hlinfo->mouse_face_end_row
26326 && hpos < hlinfo->mouse_face_end_col))
26327 return 1;
26328 }
26329 else
26330 {
26331 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26332 {
26333 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26334 return 1;
26335 }
26336 else if ((vpos == hlinfo->mouse_face_beg_row
26337 && hpos <= hlinfo->mouse_face_beg_col)
26338 || (vpos == hlinfo->mouse_face_end_row
26339 && hpos > hlinfo->mouse_face_end_col))
26340 return 1;
26341 }
26342 return 0;
26343 }
26344
26345
26346 /* EXPORT:
26347 Non-zero if physical cursor of window W is within mouse face. */
26348
26349 int
26350 cursor_in_mouse_face_p (struct window *w)
26351 {
26352 int hpos = w->phys_cursor.hpos;
26353 int vpos = w->phys_cursor.vpos;
26354 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26355
26356 /* When the window is hscrolled, cursor hpos can legitimately be out
26357 of bounds, but we draw the cursor at the corresponding window
26358 margin in that case. */
26359 if (!row->reversed_p && hpos < 0)
26360 hpos = 0;
26361 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26362 hpos = row->used[TEXT_AREA] - 1;
26363
26364 return coords_in_mouse_face_p (w, hpos, vpos);
26365 }
26366
26367
26368 \f
26369 /* Find the glyph rows START_ROW and END_ROW of window W that display
26370 characters between buffer positions START_CHARPOS and END_CHARPOS
26371 (excluding END_CHARPOS). DISP_STRING is a display string that
26372 covers these buffer positions. This is similar to
26373 row_containing_pos, but is more accurate when bidi reordering makes
26374 buffer positions change non-linearly with glyph rows. */
26375 static void
26376 rows_from_pos_range (struct window *w,
26377 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26378 Lisp_Object disp_string,
26379 struct glyph_row **start, struct glyph_row **end)
26380 {
26381 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26382 int last_y = window_text_bottom_y (w);
26383 struct glyph_row *row;
26384
26385 *start = NULL;
26386 *end = NULL;
26387
26388 while (!first->enabled_p
26389 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26390 first++;
26391
26392 /* Find the START row. */
26393 for (row = first;
26394 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26395 row++)
26396 {
26397 /* A row can potentially be the START row if the range of the
26398 characters it displays intersects the range
26399 [START_CHARPOS..END_CHARPOS). */
26400 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26401 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26402 /* See the commentary in row_containing_pos, for the
26403 explanation of the complicated way to check whether
26404 some position is beyond the end of the characters
26405 displayed by a row. */
26406 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26407 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26408 && !row->ends_at_zv_p
26409 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26410 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26411 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26412 && !row->ends_at_zv_p
26413 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26414 {
26415 /* Found a candidate row. Now make sure at least one of the
26416 glyphs it displays has a charpos from the range
26417 [START_CHARPOS..END_CHARPOS).
26418
26419 This is not obvious because bidi reordering could make
26420 buffer positions of a row be 1,2,3,102,101,100, and if we
26421 want to highlight characters in [50..60), we don't want
26422 this row, even though [50..60) does intersect [1..103),
26423 the range of character positions given by the row's start
26424 and end positions. */
26425 struct glyph *g = row->glyphs[TEXT_AREA];
26426 struct glyph *e = g + row->used[TEXT_AREA];
26427
26428 while (g < e)
26429 {
26430 if (((BUFFERP (g->object) || INTEGERP (g->object))
26431 && start_charpos <= g->charpos && g->charpos < end_charpos)
26432 /* A glyph that comes from DISP_STRING is by
26433 definition to be highlighted. */
26434 || EQ (g->object, disp_string))
26435 *start = row;
26436 g++;
26437 }
26438 if (*start)
26439 break;
26440 }
26441 }
26442
26443 /* Find the END row. */
26444 if (!*start
26445 /* If the last row is partially visible, start looking for END
26446 from that row, instead of starting from FIRST. */
26447 && !(row->enabled_p
26448 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26449 row = first;
26450 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26451 {
26452 struct glyph_row *next = row + 1;
26453 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26454
26455 if (!next->enabled_p
26456 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26457 /* The first row >= START whose range of displayed characters
26458 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26459 is the row END + 1. */
26460 || (start_charpos < next_start
26461 && end_charpos < next_start)
26462 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26463 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26464 && !next->ends_at_zv_p
26465 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26466 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26467 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26468 && !next->ends_at_zv_p
26469 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26470 {
26471 *end = row;
26472 break;
26473 }
26474 else
26475 {
26476 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26477 but none of the characters it displays are in the range, it is
26478 also END + 1. */
26479 struct glyph *g = next->glyphs[TEXT_AREA];
26480 struct glyph *s = g;
26481 struct glyph *e = g + next->used[TEXT_AREA];
26482
26483 while (g < e)
26484 {
26485 if (((BUFFERP (g->object) || INTEGERP (g->object))
26486 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26487 /* If the buffer position of the first glyph in
26488 the row is equal to END_CHARPOS, it means
26489 the last character to be highlighted is the
26490 newline of ROW, and we must consider NEXT as
26491 END, not END+1. */
26492 || (((!next->reversed_p && g == s)
26493 || (next->reversed_p && g == e - 1))
26494 && (g->charpos == end_charpos
26495 /* Special case for when NEXT is an
26496 empty line at ZV. */
26497 || (g->charpos == -1
26498 && !row->ends_at_zv_p
26499 && next_start == end_charpos)))))
26500 /* A glyph that comes from DISP_STRING is by
26501 definition to be highlighted. */
26502 || EQ (g->object, disp_string))
26503 break;
26504 g++;
26505 }
26506 if (g == e)
26507 {
26508 *end = row;
26509 break;
26510 }
26511 /* The first row that ends at ZV must be the last to be
26512 highlighted. */
26513 else if (next->ends_at_zv_p)
26514 {
26515 *end = next;
26516 break;
26517 }
26518 }
26519 }
26520 }
26521
26522 /* This function sets the mouse_face_* elements of HLINFO, assuming
26523 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26524 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26525 for the overlay or run of text properties specifying the mouse
26526 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26527 before-string and after-string that must also be highlighted.
26528 DISP_STRING, if non-nil, is a display string that may cover some
26529 or all of the highlighted text. */
26530
26531 static void
26532 mouse_face_from_buffer_pos (Lisp_Object window,
26533 Mouse_HLInfo *hlinfo,
26534 ptrdiff_t mouse_charpos,
26535 ptrdiff_t start_charpos,
26536 ptrdiff_t end_charpos,
26537 Lisp_Object before_string,
26538 Lisp_Object after_string,
26539 Lisp_Object disp_string)
26540 {
26541 struct window *w = XWINDOW (window);
26542 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26543 struct glyph_row *r1, *r2;
26544 struct glyph *glyph, *end;
26545 ptrdiff_t ignore, pos;
26546 int x;
26547
26548 eassert (NILP (disp_string) || STRINGP (disp_string));
26549 eassert (NILP (before_string) || STRINGP (before_string));
26550 eassert (NILP (after_string) || STRINGP (after_string));
26551
26552 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26553 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26554 if (r1 == NULL)
26555 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26556 /* If the before-string or display-string contains newlines,
26557 rows_from_pos_range skips to its last row. Move back. */
26558 if (!NILP (before_string) || !NILP (disp_string))
26559 {
26560 struct glyph_row *prev;
26561 while ((prev = r1 - 1, prev >= first)
26562 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26563 && prev->used[TEXT_AREA] > 0)
26564 {
26565 struct glyph *beg = prev->glyphs[TEXT_AREA];
26566 glyph = beg + prev->used[TEXT_AREA];
26567 while (--glyph >= beg && INTEGERP (glyph->object));
26568 if (glyph < beg
26569 || !(EQ (glyph->object, before_string)
26570 || EQ (glyph->object, disp_string)))
26571 break;
26572 r1 = prev;
26573 }
26574 }
26575 if (r2 == NULL)
26576 {
26577 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26578 hlinfo->mouse_face_past_end = 1;
26579 }
26580 else if (!NILP (after_string))
26581 {
26582 /* If the after-string has newlines, advance to its last row. */
26583 struct glyph_row *next;
26584 struct glyph_row *last
26585 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26586
26587 for (next = r2 + 1;
26588 next <= last
26589 && next->used[TEXT_AREA] > 0
26590 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26591 ++next)
26592 r2 = next;
26593 }
26594 /* The rest of the display engine assumes that mouse_face_beg_row is
26595 either above mouse_face_end_row or identical to it. But with
26596 bidi-reordered continued lines, the row for START_CHARPOS could
26597 be below the row for END_CHARPOS. If so, swap the rows and store
26598 them in correct order. */
26599 if (r1->y > r2->y)
26600 {
26601 struct glyph_row *tem = r2;
26602
26603 r2 = r1;
26604 r1 = tem;
26605 }
26606
26607 hlinfo->mouse_face_beg_y = r1->y;
26608 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26609 hlinfo->mouse_face_end_y = r2->y;
26610 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26611
26612 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26613 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26614 could be anywhere in the row and in any order. The strategy
26615 below is to find the leftmost and the rightmost glyph that
26616 belongs to either of these 3 strings, or whose position is
26617 between START_CHARPOS and END_CHARPOS, and highlight all the
26618 glyphs between those two. This may cover more than just the text
26619 between START_CHARPOS and END_CHARPOS if the range of characters
26620 strides the bidi level boundary, e.g. if the beginning is in R2L
26621 text while the end is in L2R text or vice versa. */
26622 if (!r1->reversed_p)
26623 {
26624 /* This row is in a left to right paragraph. Scan it left to
26625 right. */
26626 glyph = r1->glyphs[TEXT_AREA];
26627 end = glyph + r1->used[TEXT_AREA];
26628 x = r1->x;
26629
26630 /* Skip truncation glyphs at the start of the glyph row. */
26631 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
26632 for (; glyph < end
26633 && INTEGERP (glyph->object)
26634 && glyph->charpos < 0;
26635 ++glyph)
26636 x += glyph->pixel_width;
26637
26638 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26639 or DISP_STRING, and the first glyph from buffer whose
26640 position is between START_CHARPOS and END_CHARPOS. */
26641 for (; glyph < end
26642 && !INTEGERP (glyph->object)
26643 && !EQ (glyph->object, disp_string)
26644 && !(BUFFERP (glyph->object)
26645 && (glyph->charpos >= start_charpos
26646 && glyph->charpos < end_charpos));
26647 ++glyph)
26648 {
26649 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26650 are present at buffer positions between START_CHARPOS and
26651 END_CHARPOS, or if they come from an overlay. */
26652 if (EQ (glyph->object, before_string))
26653 {
26654 pos = string_buffer_position (before_string,
26655 start_charpos);
26656 /* If pos == 0, it means before_string came from an
26657 overlay, not from a buffer position. */
26658 if (!pos || (pos >= start_charpos && pos < end_charpos))
26659 break;
26660 }
26661 else if (EQ (glyph->object, after_string))
26662 {
26663 pos = string_buffer_position (after_string, end_charpos);
26664 if (!pos || (pos >= start_charpos && pos < end_charpos))
26665 break;
26666 }
26667 x += glyph->pixel_width;
26668 }
26669 hlinfo->mouse_face_beg_x = x;
26670 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26671 }
26672 else
26673 {
26674 /* This row is in a right to left paragraph. Scan it right to
26675 left. */
26676 struct glyph *g;
26677
26678 end = r1->glyphs[TEXT_AREA] - 1;
26679 glyph = end + r1->used[TEXT_AREA];
26680
26681 /* Skip truncation glyphs at the start of the glyph row. */
26682 if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
26683 for (; glyph > end
26684 && INTEGERP (glyph->object)
26685 && glyph->charpos < 0;
26686 --glyph)
26687 ;
26688
26689 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26690 or DISP_STRING, and the first glyph from buffer whose
26691 position is between START_CHARPOS and END_CHARPOS. */
26692 for (; glyph > end
26693 && !INTEGERP (glyph->object)
26694 && !EQ (glyph->object, disp_string)
26695 && !(BUFFERP (glyph->object)
26696 && (glyph->charpos >= start_charpos
26697 && glyph->charpos < end_charpos));
26698 --glyph)
26699 {
26700 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26701 are present at buffer positions between START_CHARPOS and
26702 END_CHARPOS, or if they come from an overlay. */
26703 if (EQ (glyph->object, before_string))
26704 {
26705 pos = string_buffer_position (before_string, start_charpos);
26706 /* If pos == 0, it means before_string came from an
26707 overlay, not from a buffer position. */
26708 if (!pos || (pos >= start_charpos && pos < end_charpos))
26709 break;
26710 }
26711 else if (EQ (glyph->object, after_string))
26712 {
26713 pos = string_buffer_position (after_string, end_charpos);
26714 if (!pos || (pos >= start_charpos && pos < end_charpos))
26715 break;
26716 }
26717 }
26718
26719 glyph++; /* first glyph to the right of the highlighted area */
26720 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26721 x += g->pixel_width;
26722 hlinfo->mouse_face_beg_x = x;
26723 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26724 }
26725
26726 /* If the highlight ends in a different row, compute GLYPH and END
26727 for the end row. Otherwise, reuse the values computed above for
26728 the row where the highlight begins. */
26729 if (r2 != r1)
26730 {
26731 if (!r2->reversed_p)
26732 {
26733 glyph = r2->glyphs[TEXT_AREA];
26734 end = glyph + r2->used[TEXT_AREA];
26735 x = r2->x;
26736 }
26737 else
26738 {
26739 end = r2->glyphs[TEXT_AREA] - 1;
26740 glyph = end + r2->used[TEXT_AREA];
26741 }
26742 }
26743
26744 if (!r2->reversed_p)
26745 {
26746 /* Skip truncation and continuation glyphs near the end of the
26747 row, and also blanks and stretch glyphs inserted by
26748 extend_face_to_end_of_line. */
26749 while (end > glyph
26750 && INTEGERP ((end - 1)->object))
26751 --end;
26752 /* Scan the rest of the glyph row from the end, looking for the
26753 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26754 DISP_STRING, or whose position is between START_CHARPOS
26755 and END_CHARPOS */
26756 for (--end;
26757 end > glyph
26758 && !INTEGERP (end->object)
26759 && !EQ (end->object, disp_string)
26760 && !(BUFFERP (end->object)
26761 && (end->charpos >= start_charpos
26762 && end->charpos < end_charpos));
26763 --end)
26764 {
26765 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26766 are present at buffer positions between START_CHARPOS and
26767 END_CHARPOS, or if they come from an overlay. */
26768 if (EQ (end->object, before_string))
26769 {
26770 pos = string_buffer_position (before_string, start_charpos);
26771 if (!pos || (pos >= start_charpos && pos < end_charpos))
26772 break;
26773 }
26774 else if (EQ (end->object, after_string))
26775 {
26776 pos = string_buffer_position (after_string, end_charpos);
26777 if (!pos || (pos >= start_charpos && pos < end_charpos))
26778 break;
26779 }
26780 }
26781 /* Find the X coordinate of the last glyph to be highlighted. */
26782 for (; glyph <= end; ++glyph)
26783 x += glyph->pixel_width;
26784
26785 hlinfo->mouse_face_end_x = x;
26786 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26787 }
26788 else
26789 {
26790 /* Skip truncation and continuation glyphs near the end of the
26791 row, and also blanks and stretch glyphs inserted by
26792 extend_face_to_end_of_line. */
26793 x = r2->x;
26794 end++;
26795 while (end < glyph
26796 && INTEGERP (end->object))
26797 {
26798 x += end->pixel_width;
26799 ++end;
26800 }
26801 /* Scan the rest of the glyph row from the end, looking for the
26802 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26803 DISP_STRING, or whose position is between START_CHARPOS
26804 and END_CHARPOS */
26805 for ( ;
26806 end < glyph
26807 && !INTEGERP (end->object)
26808 && !EQ (end->object, disp_string)
26809 && !(BUFFERP (end->object)
26810 && (end->charpos >= start_charpos
26811 && end->charpos < end_charpos));
26812 ++end)
26813 {
26814 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26815 are present at buffer positions between START_CHARPOS and
26816 END_CHARPOS, or if they come from an overlay. */
26817 if (EQ (end->object, before_string))
26818 {
26819 pos = string_buffer_position (before_string, start_charpos);
26820 if (!pos || (pos >= start_charpos && pos < end_charpos))
26821 break;
26822 }
26823 else if (EQ (end->object, after_string))
26824 {
26825 pos = string_buffer_position (after_string, end_charpos);
26826 if (!pos || (pos >= start_charpos && pos < end_charpos))
26827 break;
26828 }
26829 x += end->pixel_width;
26830 }
26831 /* If we exited the above loop because we arrived at the last
26832 glyph of the row, and its buffer position is still not in
26833 range, it means the last character in range is the preceding
26834 newline. Bump the end column and x values to get past the
26835 last glyph. */
26836 if (end == glyph
26837 && BUFFERP (end->object)
26838 && (end->charpos < start_charpos
26839 || end->charpos >= end_charpos))
26840 {
26841 x += end->pixel_width;
26842 ++end;
26843 }
26844 hlinfo->mouse_face_end_x = x;
26845 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
26846 }
26847
26848 hlinfo->mouse_face_window = window;
26849 hlinfo->mouse_face_face_id
26850 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
26851 mouse_charpos + 1,
26852 !hlinfo->mouse_face_hidden, -1);
26853 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
26854 }
26855
26856 /* The following function is not used anymore (replaced with
26857 mouse_face_from_string_pos), but I leave it here for the time
26858 being, in case someone would. */
26859
26860 #if 0 /* not used */
26861
26862 /* Find the position of the glyph for position POS in OBJECT in
26863 window W's current matrix, and return in *X, *Y the pixel
26864 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
26865
26866 RIGHT_P non-zero means return the position of the right edge of the
26867 glyph, RIGHT_P zero means return the left edge position.
26868
26869 If no glyph for POS exists in the matrix, return the position of
26870 the glyph with the next smaller position that is in the matrix, if
26871 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
26872 exists in the matrix, return the position of the glyph with the
26873 next larger position in OBJECT.
26874
26875 Value is non-zero if a glyph was found. */
26876
26877 static int
26878 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
26879 int *hpos, int *vpos, int *x, int *y, int right_p)
26880 {
26881 int yb = window_text_bottom_y (w);
26882 struct glyph_row *r;
26883 struct glyph *best_glyph = NULL;
26884 struct glyph_row *best_row = NULL;
26885 int best_x = 0;
26886
26887 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26888 r->enabled_p && r->y < yb;
26889 ++r)
26890 {
26891 struct glyph *g = r->glyphs[TEXT_AREA];
26892 struct glyph *e = g + r->used[TEXT_AREA];
26893 int gx;
26894
26895 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26896 if (EQ (g->object, object))
26897 {
26898 if (g->charpos == pos)
26899 {
26900 best_glyph = g;
26901 best_x = gx;
26902 best_row = r;
26903 goto found;
26904 }
26905 else if (best_glyph == NULL
26906 || ((eabs (g->charpos - pos)
26907 < eabs (best_glyph->charpos - pos))
26908 && (right_p
26909 ? g->charpos < pos
26910 : g->charpos > pos)))
26911 {
26912 best_glyph = g;
26913 best_x = gx;
26914 best_row = r;
26915 }
26916 }
26917 }
26918
26919 found:
26920
26921 if (best_glyph)
26922 {
26923 *x = best_x;
26924 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
26925
26926 if (right_p)
26927 {
26928 *x += best_glyph->pixel_width;
26929 ++*hpos;
26930 }
26931
26932 *y = best_row->y;
26933 *vpos = MATRIX_ROW_VPOS (best_row, w->current_matrix);
26934 }
26935
26936 return best_glyph != NULL;
26937 }
26938 #endif /* not used */
26939
26940 /* Find the positions of the first and the last glyphs in window W's
26941 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
26942 (assumed to be a string), and return in HLINFO's mouse_face_*
26943 members the pixel and column/row coordinates of those glyphs. */
26944
26945 static void
26946 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
26947 Lisp_Object object,
26948 ptrdiff_t startpos, ptrdiff_t endpos)
26949 {
26950 int yb = window_text_bottom_y (w);
26951 struct glyph_row *r;
26952 struct glyph *g, *e;
26953 int gx;
26954 int found = 0;
26955
26956 /* Find the glyph row with at least one position in the range
26957 [STARTPOS..ENDPOS], and the first glyph in that row whose
26958 position belongs to that range. */
26959 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26960 r->enabled_p && r->y < yb;
26961 ++r)
26962 {
26963 if (!r->reversed_p)
26964 {
26965 g = r->glyphs[TEXT_AREA];
26966 e = g + r->used[TEXT_AREA];
26967 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
26968 if (EQ (g->object, object)
26969 && startpos <= g->charpos && g->charpos <= endpos)
26970 {
26971 hlinfo->mouse_face_beg_row
26972 = MATRIX_ROW_VPOS (r, w->current_matrix);
26973 hlinfo->mouse_face_beg_y = r->y;
26974 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26975 hlinfo->mouse_face_beg_x = gx;
26976 found = 1;
26977 break;
26978 }
26979 }
26980 else
26981 {
26982 struct glyph *g1;
26983
26984 e = r->glyphs[TEXT_AREA];
26985 g = e + r->used[TEXT_AREA];
26986 for ( ; g > e; --g)
26987 if (EQ ((g-1)->object, object)
26988 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
26989 {
26990 hlinfo->mouse_face_beg_row
26991 = MATRIX_ROW_VPOS (r, w->current_matrix);
26992 hlinfo->mouse_face_beg_y = r->y;
26993 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
26994 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
26995 gx += g1->pixel_width;
26996 hlinfo->mouse_face_beg_x = gx;
26997 found = 1;
26998 break;
26999 }
27000 }
27001 if (found)
27002 break;
27003 }
27004
27005 if (!found)
27006 return;
27007
27008 /* Starting with the next row, look for the first row which does NOT
27009 include any glyphs whose positions are in the range. */
27010 for (++r; r->enabled_p && r->y < yb; ++r)
27011 {
27012 g = r->glyphs[TEXT_AREA];
27013 e = g + r->used[TEXT_AREA];
27014 found = 0;
27015 for ( ; g < e; ++g)
27016 if (EQ (g->object, object)
27017 && startpos <= g->charpos && g->charpos <= endpos)
27018 {
27019 found = 1;
27020 break;
27021 }
27022 if (!found)
27023 break;
27024 }
27025
27026 /* The highlighted region ends on the previous row. */
27027 r--;
27028
27029 /* Set the end row and its vertical pixel coordinate. */
27030 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r, w->current_matrix);
27031 hlinfo->mouse_face_end_y = r->y;
27032
27033 /* Compute and set the end column and the end column's horizontal
27034 pixel coordinate. */
27035 if (!r->reversed_p)
27036 {
27037 g = r->glyphs[TEXT_AREA];
27038 e = g + r->used[TEXT_AREA];
27039 for ( ; e > g; --e)
27040 if (EQ ((e-1)->object, object)
27041 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27042 break;
27043 hlinfo->mouse_face_end_col = e - g;
27044
27045 for (gx = r->x; g < e; ++g)
27046 gx += g->pixel_width;
27047 hlinfo->mouse_face_end_x = gx;
27048 }
27049 else
27050 {
27051 e = r->glyphs[TEXT_AREA];
27052 g = e + r->used[TEXT_AREA];
27053 for (gx = r->x ; e < g; ++e)
27054 {
27055 if (EQ (e->object, object)
27056 && startpos <= e->charpos && e->charpos <= endpos)
27057 break;
27058 gx += e->pixel_width;
27059 }
27060 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27061 hlinfo->mouse_face_end_x = gx;
27062 }
27063 }
27064
27065 #ifdef HAVE_WINDOW_SYSTEM
27066
27067 /* See if position X, Y is within a hot-spot of an image. */
27068
27069 static int
27070 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27071 {
27072 if (!CONSP (hot_spot))
27073 return 0;
27074
27075 if (EQ (XCAR (hot_spot), Qrect))
27076 {
27077 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27078 Lisp_Object rect = XCDR (hot_spot);
27079 Lisp_Object tem;
27080 if (!CONSP (rect))
27081 return 0;
27082 if (!CONSP (XCAR (rect)))
27083 return 0;
27084 if (!CONSP (XCDR (rect)))
27085 return 0;
27086 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27087 return 0;
27088 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27089 return 0;
27090 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27091 return 0;
27092 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27093 return 0;
27094 return 1;
27095 }
27096 else if (EQ (XCAR (hot_spot), Qcircle))
27097 {
27098 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27099 Lisp_Object circ = XCDR (hot_spot);
27100 Lisp_Object lr, lx0, ly0;
27101 if (CONSP (circ)
27102 && CONSP (XCAR (circ))
27103 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27104 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27105 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27106 {
27107 double r = XFLOATINT (lr);
27108 double dx = XINT (lx0) - x;
27109 double dy = XINT (ly0) - y;
27110 return (dx * dx + dy * dy <= r * r);
27111 }
27112 }
27113 else if (EQ (XCAR (hot_spot), Qpoly))
27114 {
27115 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27116 if (VECTORP (XCDR (hot_spot)))
27117 {
27118 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27119 Lisp_Object *poly = v->contents;
27120 ptrdiff_t n = v->header.size;
27121 ptrdiff_t i;
27122 int inside = 0;
27123 Lisp_Object lx, ly;
27124 int x0, y0;
27125
27126 /* Need an even number of coordinates, and at least 3 edges. */
27127 if (n < 6 || n & 1)
27128 return 0;
27129
27130 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27131 If count is odd, we are inside polygon. Pixels on edges
27132 may or may not be included depending on actual geometry of the
27133 polygon. */
27134 if ((lx = poly[n-2], !INTEGERP (lx))
27135 || (ly = poly[n-1], !INTEGERP (lx)))
27136 return 0;
27137 x0 = XINT (lx), y0 = XINT (ly);
27138 for (i = 0; i < n; i += 2)
27139 {
27140 int x1 = x0, y1 = y0;
27141 if ((lx = poly[i], !INTEGERP (lx))
27142 || (ly = poly[i+1], !INTEGERP (ly)))
27143 return 0;
27144 x0 = XINT (lx), y0 = XINT (ly);
27145
27146 /* Does this segment cross the X line? */
27147 if (x0 >= x)
27148 {
27149 if (x1 >= x)
27150 continue;
27151 }
27152 else if (x1 < x)
27153 continue;
27154 if (y > y0 && y > y1)
27155 continue;
27156 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27157 inside = !inside;
27158 }
27159 return inside;
27160 }
27161 }
27162 return 0;
27163 }
27164
27165 Lisp_Object
27166 find_hot_spot (Lisp_Object map, int x, int y)
27167 {
27168 while (CONSP (map))
27169 {
27170 if (CONSP (XCAR (map))
27171 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27172 return XCAR (map);
27173 map = XCDR (map);
27174 }
27175
27176 return Qnil;
27177 }
27178
27179 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27180 3, 3, 0,
27181 doc: /* Lookup in image map MAP coordinates X and Y.
27182 An image map is an alist where each element has the format (AREA ID PLIST).
27183 An AREA is specified as either a rectangle, a circle, or a polygon:
27184 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27185 pixel coordinates of the upper left and bottom right corners.
27186 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27187 and the radius of the circle; r may be a float or integer.
27188 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27189 vector describes one corner in the polygon.
27190 Returns the alist element for the first matching AREA in MAP. */)
27191 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27192 {
27193 if (NILP (map))
27194 return Qnil;
27195
27196 CHECK_NUMBER (x);
27197 CHECK_NUMBER (y);
27198
27199 return find_hot_spot (map,
27200 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27201 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27202 }
27203
27204
27205 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27206 static void
27207 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27208 {
27209 /* Do not change cursor shape while dragging mouse. */
27210 if (!NILP (do_mouse_tracking))
27211 return;
27212
27213 if (!NILP (pointer))
27214 {
27215 if (EQ (pointer, Qarrow))
27216 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27217 else if (EQ (pointer, Qhand))
27218 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27219 else if (EQ (pointer, Qtext))
27220 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27221 else if (EQ (pointer, intern ("hdrag")))
27222 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27223 #ifdef HAVE_X_WINDOWS
27224 else if (EQ (pointer, intern ("vdrag")))
27225 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27226 #endif
27227 else if (EQ (pointer, intern ("hourglass")))
27228 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27229 else if (EQ (pointer, Qmodeline))
27230 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27231 else
27232 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27233 }
27234
27235 if (cursor != No_Cursor)
27236 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27237 }
27238
27239 #endif /* HAVE_WINDOW_SYSTEM */
27240
27241 /* Take proper action when mouse has moved to the mode or header line
27242 or marginal area AREA of window W, x-position X and y-position Y.
27243 X is relative to the start of the text display area of W, so the
27244 width of bitmap areas and scroll bars must be subtracted to get a
27245 position relative to the start of the mode line. */
27246
27247 static void
27248 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27249 enum window_part area)
27250 {
27251 struct window *w = XWINDOW (window);
27252 struct frame *f = XFRAME (w->frame);
27253 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27254 #ifdef HAVE_WINDOW_SYSTEM
27255 Display_Info *dpyinfo;
27256 #endif
27257 Cursor cursor = No_Cursor;
27258 Lisp_Object pointer = Qnil;
27259 int dx, dy, width, height;
27260 ptrdiff_t charpos;
27261 Lisp_Object string, object = Qnil;
27262 Lisp_Object pos IF_LINT (= Qnil), help;
27263
27264 Lisp_Object mouse_face;
27265 int original_x_pixel = x;
27266 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27267 struct glyph_row *row IF_LINT (= 0);
27268
27269 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27270 {
27271 int x0;
27272 struct glyph *end;
27273
27274 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27275 returns them in row/column units! */
27276 string = mode_line_string (w, area, &x, &y, &charpos,
27277 &object, &dx, &dy, &width, &height);
27278
27279 row = (area == ON_MODE_LINE
27280 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27281 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27282
27283 /* Find the glyph under the mouse pointer. */
27284 if (row->mode_line_p && row->enabled_p)
27285 {
27286 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27287 end = glyph + row->used[TEXT_AREA];
27288
27289 for (x0 = original_x_pixel;
27290 glyph < end && x0 >= glyph->pixel_width;
27291 ++glyph)
27292 x0 -= glyph->pixel_width;
27293
27294 if (glyph >= end)
27295 glyph = NULL;
27296 }
27297 }
27298 else
27299 {
27300 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27301 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27302 returns them in row/column units! */
27303 string = marginal_area_string (w, area, &x, &y, &charpos,
27304 &object, &dx, &dy, &width, &height);
27305 }
27306
27307 help = Qnil;
27308
27309 #ifdef HAVE_WINDOW_SYSTEM
27310 if (IMAGEP (object))
27311 {
27312 Lisp_Object image_map, hotspot;
27313 if ((image_map = Fplist_get (XCDR (object), QCmap),
27314 !NILP (image_map))
27315 && (hotspot = find_hot_spot (image_map, dx, dy),
27316 CONSP (hotspot))
27317 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27318 {
27319 Lisp_Object plist;
27320
27321 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27322 If so, we could look for mouse-enter, mouse-leave
27323 properties in PLIST (and do something...). */
27324 hotspot = XCDR (hotspot);
27325 if (CONSP (hotspot)
27326 && (plist = XCAR (hotspot), CONSP (plist)))
27327 {
27328 pointer = Fplist_get (plist, Qpointer);
27329 if (NILP (pointer))
27330 pointer = Qhand;
27331 help = Fplist_get (plist, Qhelp_echo);
27332 if (!NILP (help))
27333 {
27334 help_echo_string = help;
27335 XSETWINDOW (help_echo_window, w);
27336 help_echo_object = w->contents;
27337 help_echo_pos = charpos;
27338 }
27339 }
27340 }
27341 if (NILP (pointer))
27342 pointer = Fplist_get (XCDR (object), QCpointer);
27343 }
27344 #endif /* HAVE_WINDOW_SYSTEM */
27345
27346 if (STRINGP (string))
27347 pos = make_number (charpos);
27348
27349 /* Set the help text and mouse pointer. If the mouse is on a part
27350 of the mode line without any text (e.g. past the right edge of
27351 the mode line text), use the default help text and pointer. */
27352 if (STRINGP (string) || area == ON_MODE_LINE)
27353 {
27354 /* Arrange to display the help by setting the global variables
27355 help_echo_string, help_echo_object, and help_echo_pos. */
27356 if (NILP (help))
27357 {
27358 if (STRINGP (string))
27359 help = Fget_text_property (pos, Qhelp_echo, string);
27360
27361 if (!NILP (help))
27362 {
27363 help_echo_string = help;
27364 XSETWINDOW (help_echo_window, w);
27365 help_echo_object = string;
27366 help_echo_pos = charpos;
27367 }
27368 else if (area == ON_MODE_LINE)
27369 {
27370 Lisp_Object default_help
27371 = buffer_local_value_1 (Qmode_line_default_help_echo,
27372 w->contents);
27373
27374 if (STRINGP (default_help))
27375 {
27376 help_echo_string = default_help;
27377 XSETWINDOW (help_echo_window, w);
27378 help_echo_object = Qnil;
27379 help_echo_pos = -1;
27380 }
27381 }
27382 }
27383
27384 #ifdef HAVE_WINDOW_SYSTEM
27385 /* Change the mouse pointer according to what is under it. */
27386 if (FRAME_WINDOW_P (f))
27387 {
27388 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27389 if (STRINGP (string))
27390 {
27391 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27392
27393 if (NILP (pointer))
27394 pointer = Fget_text_property (pos, Qpointer, string);
27395
27396 /* Change the mouse pointer according to what is under X/Y. */
27397 if (NILP (pointer)
27398 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27399 {
27400 Lisp_Object map;
27401 map = Fget_text_property (pos, Qlocal_map, string);
27402 if (!KEYMAPP (map))
27403 map = Fget_text_property (pos, Qkeymap, string);
27404 if (!KEYMAPP (map))
27405 cursor = dpyinfo->vertical_scroll_bar_cursor;
27406 }
27407 }
27408 else
27409 /* Default mode-line pointer. */
27410 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27411 }
27412 #endif
27413 }
27414
27415 /* Change the mouse face according to what is under X/Y. */
27416 if (STRINGP (string))
27417 {
27418 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27419 if (!NILP (Vmouse_highlight) && !NILP (mouse_face)
27420 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27421 && glyph)
27422 {
27423 Lisp_Object b, e;
27424
27425 struct glyph * tmp_glyph;
27426
27427 int gpos;
27428 int gseq_length;
27429 int total_pixel_width;
27430 ptrdiff_t begpos, endpos, ignore;
27431
27432 int vpos, hpos;
27433
27434 b = Fprevious_single_property_change (make_number (charpos + 1),
27435 Qmouse_face, string, Qnil);
27436 if (NILP (b))
27437 begpos = 0;
27438 else
27439 begpos = XINT (b);
27440
27441 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27442 if (NILP (e))
27443 endpos = SCHARS (string);
27444 else
27445 endpos = XINT (e);
27446
27447 /* Calculate the glyph position GPOS of GLYPH in the
27448 displayed string, relative to the beginning of the
27449 highlighted part of the string.
27450
27451 Note: GPOS is different from CHARPOS. CHARPOS is the
27452 position of GLYPH in the internal string object. A mode
27453 line string format has structures which are converted to
27454 a flattened string by the Emacs Lisp interpreter. The
27455 internal string is an element of those structures. The
27456 displayed string is the flattened string. */
27457 tmp_glyph = row_start_glyph;
27458 while (tmp_glyph < glyph
27459 && (!(EQ (tmp_glyph->object, glyph->object)
27460 && begpos <= tmp_glyph->charpos
27461 && tmp_glyph->charpos < endpos)))
27462 tmp_glyph++;
27463 gpos = glyph - tmp_glyph;
27464
27465 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27466 the highlighted part of the displayed string to which
27467 GLYPH belongs. Note: GSEQ_LENGTH is different from
27468 SCHARS (STRING), because the latter returns the length of
27469 the internal string. */
27470 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27471 tmp_glyph > glyph
27472 && (!(EQ (tmp_glyph->object, glyph->object)
27473 && begpos <= tmp_glyph->charpos
27474 && tmp_glyph->charpos < endpos));
27475 tmp_glyph--)
27476 ;
27477 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27478
27479 /* Calculate the total pixel width of all the glyphs between
27480 the beginning of the highlighted area and GLYPH. */
27481 total_pixel_width = 0;
27482 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27483 total_pixel_width += tmp_glyph->pixel_width;
27484
27485 /* Pre calculation of re-rendering position. Note: X is in
27486 column units here, after the call to mode_line_string or
27487 marginal_area_string. */
27488 hpos = x - gpos;
27489 vpos = (area == ON_MODE_LINE
27490 ? (w->current_matrix)->nrows - 1
27491 : 0);
27492
27493 /* If GLYPH's position is included in the region that is
27494 already drawn in mouse face, we have nothing to do. */
27495 if ( EQ (window, hlinfo->mouse_face_window)
27496 && (!row->reversed_p
27497 ? (hlinfo->mouse_face_beg_col <= hpos
27498 && hpos < hlinfo->mouse_face_end_col)
27499 /* In R2L rows we swap BEG and END, see below. */
27500 : (hlinfo->mouse_face_end_col <= hpos
27501 && hpos < hlinfo->mouse_face_beg_col))
27502 && hlinfo->mouse_face_beg_row == vpos )
27503 return;
27504
27505 if (clear_mouse_face (hlinfo))
27506 cursor = No_Cursor;
27507
27508 if (!row->reversed_p)
27509 {
27510 hlinfo->mouse_face_beg_col = hpos;
27511 hlinfo->mouse_face_beg_x = original_x_pixel
27512 - (total_pixel_width + dx);
27513 hlinfo->mouse_face_end_col = hpos + gseq_length;
27514 hlinfo->mouse_face_end_x = 0;
27515 }
27516 else
27517 {
27518 /* In R2L rows, show_mouse_face expects BEG and END
27519 coordinates to be swapped. */
27520 hlinfo->mouse_face_end_col = hpos;
27521 hlinfo->mouse_face_end_x = original_x_pixel
27522 - (total_pixel_width + dx);
27523 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27524 hlinfo->mouse_face_beg_x = 0;
27525 }
27526
27527 hlinfo->mouse_face_beg_row = vpos;
27528 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27529 hlinfo->mouse_face_beg_y = 0;
27530 hlinfo->mouse_face_end_y = 0;
27531 hlinfo->mouse_face_past_end = 0;
27532 hlinfo->mouse_face_window = window;
27533
27534 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27535 charpos,
27536 0, 0, 0,
27537 &ignore,
27538 glyph->face_id,
27539 1);
27540 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27541
27542 if (NILP (pointer))
27543 pointer = Qhand;
27544 }
27545 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27546 clear_mouse_face (hlinfo);
27547 }
27548 #ifdef HAVE_WINDOW_SYSTEM
27549 if (FRAME_WINDOW_P (f))
27550 define_frame_cursor1 (f, cursor, pointer);
27551 #endif
27552 }
27553
27554
27555 /* EXPORT:
27556 Take proper action when the mouse has moved to position X, Y on
27557 frame F with regards to highlighting portions of display that have
27558 mouse-face properties. Also de-highlight portions of display where
27559 the mouse was before, set the mouse pointer shape as appropriate
27560 for the mouse coordinates, and activate help echo (tooltips).
27561 X and Y can be negative or out of range. */
27562
27563 void
27564 note_mouse_highlight (struct frame *f, int x, int y)
27565 {
27566 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27567 enum window_part part = ON_NOTHING;
27568 Lisp_Object window;
27569 struct window *w;
27570 Cursor cursor = No_Cursor;
27571 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27572 struct buffer *b;
27573
27574 /* When a menu is active, don't highlight because this looks odd. */
27575 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27576 if (popup_activated ())
27577 return;
27578 #endif
27579
27580 if (!f->glyphs_initialized_p
27581 || f->pointer_invisible)
27582 return;
27583
27584 hlinfo->mouse_face_mouse_x = x;
27585 hlinfo->mouse_face_mouse_y = y;
27586 hlinfo->mouse_face_mouse_frame = f;
27587
27588 if (hlinfo->mouse_face_defer)
27589 return;
27590
27591 /* Which window is that in? */
27592 window = window_from_coordinates (f, x, y, &part, 1);
27593
27594 /* If displaying active text in another window, clear that. */
27595 if (! EQ (window, hlinfo->mouse_face_window)
27596 /* Also clear if we move out of text area in same window. */
27597 || (!NILP (hlinfo->mouse_face_window)
27598 && !NILP (window)
27599 && part != ON_TEXT
27600 && part != ON_MODE_LINE
27601 && part != ON_HEADER_LINE))
27602 clear_mouse_face (hlinfo);
27603
27604 /* Not on a window -> return. */
27605 if (!WINDOWP (window))
27606 return;
27607
27608 /* Reset help_echo_string. It will get recomputed below. */
27609 help_echo_string = Qnil;
27610
27611 /* Convert to window-relative pixel coordinates. */
27612 w = XWINDOW (window);
27613 frame_to_window_pixel_xy (w, &x, &y);
27614
27615 #ifdef HAVE_WINDOW_SYSTEM
27616 /* Handle tool-bar window differently since it doesn't display a
27617 buffer. */
27618 if (EQ (window, f->tool_bar_window))
27619 {
27620 note_tool_bar_highlight (f, x, y);
27621 return;
27622 }
27623 #endif
27624
27625 /* Mouse is on the mode, header line or margin? */
27626 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27627 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27628 {
27629 note_mode_line_or_margin_highlight (window, x, y, part);
27630 return;
27631 }
27632
27633 #ifdef HAVE_WINDOW_SYSTEM
27634 if (part == ON_VERTICAL_BORDER)
27635 {
27636 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27637 help_echo_string = build_string ("drag-mouse-1: resize");
27638 }
27639 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27640 || part == ON_SCROLL_BAR)
27641 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27642 else
27643 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27644 #endif
27645
27646 /* Are we in a window whose display is up to date?
27647 And verify the buffer's text has not changed. */
27648 b = XBUFFER (w->contents);
27649 if (part == ON_TEXT
27650 && w->window_end_valid
27651 && w->last_modified == BUF_MODIFF (b)
27652 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27653 {
27654 int hpos, vpos, dx, dy, area = LAST_AREA;
27655 ptrdiff_t pos;
27656 struct glyph *glyph;
27657 Lisp_Object object;
27658 Lisp_Object mouse_face = Qnil, position;
27659 Lisp_Object *overlay_vec = NULL;
27660 ptrdiff_t i, noverlays;
27661 struct buffer *obuf;
27662 ptrdiff_t obegv, ozv;
27663 int same_region;
27664
27665 /* Find the glyph under X/Y. */
27666 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27667
27668 #ifdef HAVE_WINDOW_SYSTEM
27669 /* Look for :pointer property on image. */
27670 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27671 {
27672 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27673 if (img != NULL && IMAGEP (img->spec))
27674 {
27675 Lisp_Object image_map, hotspot;
27676 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27677 !NILP (image_map))
27678 && (hotspot = find_hot_spot (image_map,
27679 glyph->slice.img.x + dx,
27680 glyph->slice.img.y + dy),
27681 CONSP (hotspot))
27682 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27683 {
27684 Lisp_Object plist;
27685
27686 /* Could check XCAR (hotspot) to see if we enter/leave
27687 this hot-spot.
27688 If so, we could look for mouse-enter, mouse-leave
27689 properties in PLIST (and do something...). */
27690 hotspot = XCDR (hotspot);
27691 if (CONSP (hotspot)
27692 && (plist = XCAR (hotspot), CONSP (plist)))
27693 {
27694 pointer = Fplist_get (plist, Qpointer);
27695 if (NILP (pointer))
27696 pointer = Qhand;
27697 help_echo_string = Fplist_get (plist, Qhelp_echo);
27698 if (!NILP (help_echo_string))
27699 {
27700 help_echo_window = window;
27701 help_echo_object = glyph->object;
27702 help_echo_pos = glyph->charpos;
27703 }
27704 }
27705 }
27706 if (NILP (pointer))
27707 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27708 }
27709 }
27710 #endif /* HAVE_WINDOW_SYSTEM */
27711
27712 /* Clear mouse face if X/Y not over text. */
27713 if (glyph == NULL
27714 || area != TEXT_AREA
27715 || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
27716 /* Glyph's OBJECT is an integer for glyphs inserted by the
27717 display engine for its internal purposes, like truncation
27718 and continuation glyphs and blanks beyond the end of
27719 line's text on text terminals. If we are over such a
27720 glyph, we are not over any text. */
27721 || INTEGERP (glyph->object)
27722 /* R2L rows have a stretch glyph at their front, which
27723 stands for no text, whereas L2R rows have no glyphs at
27724 all beyond the end of text. Treat such stretch glyphs
27725 like we do with NULL glyphs in L2R rows. */
27726 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27727 && glyph == MATRIX_ROW_GLYPH_START (w->current_matrix, vpos)
27728 && glyph->type == STRETCH_GLYPH
27729 && glyph->avoid_cursor_p))
27730 {
27731 if (clear_mouse_face (hlinfo))
27732 cursor = No_Cursor;
27733 #ifdef HAVE_WINDOW_SYSTEM
27734 if (FRAME_WINDOW_P (f) && NILP (pointer))
27735 {
27736 if (area != TEXT_AREA)
27737 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27738 else
27739 pointer = Vvoid_text_area_pointer;
27740 }
27741 #endif
27742 goto set_cursor;
27743 }
27744
27745 pos = glyph->charpos;
27746 object = glyph->object;
27747 if (!STRINGP (object) && !BUFFERP (object))
27748 goto set_cursor;
27749
27750 /* If we get an out-of-range value, return now; avoid an error. */
27751 if (BUFFERP (object) && pos > BUF_Z (b))
27752 goto set_cursor;
27753
27754 /* Make the window's buffer temporarily current for
27755 overlays_at and compute_char_face. */
27756 obuf = current_buffer;
27757 current_buffer = b;
27758 obegv = BEGV;
27759 ozv = ZV;
27760 BEGV = BEG;
27761 ZV = Z;
27762
27763 /* Is this char mouse-active or does it have help-echo? */
27764 position = make_number (pos);
27765
27766 if (BUFFERP (object))
27767 {
27768 /* Put all the overlays we want in a vector in overlay_vec. */
27769 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27770 /* Sort overlays into increasing priority order. */
27771 noverlays = sort_overlays (overlay_vec, noverlays, w);
27772 }
27773 else
27774 noverlays = 0;
27775
27776 if (NILP (Vmouse_highlight))
27777 {
27778 clear_mouse_face (hlinfo);
27779 goto check_help_echo;
27780 }
27781
27782 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27783
27784 if (same_region)
27785 cursor = No_Cursor;
27786
27787 /* Check mouse-face highlighting. */
27788 if (! same_region
27789 /* If there exists an overlay with mouse-face overlapping
27790 the one we are currently highlighting, we have to
27791 check if we enter the overlapping overlay, and then
27792 highlight only that. */
27793 || (OVERLAYP (hlinfo->mouse_face_overlay)
27794 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27795 {
27796 /* Find the highest priority overlay with a mouse-face. */
27797 Lisp_Object overlay = Qnil;
27798 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27799 {
27800 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27801 if (!NILP (mouse_face))
27802 overlay = overlay_vec[i];
27803 }
27804
27805 /* If we're highlighting the same overlay as before, there's
27806 no need to do that again. */
27807 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27808 goto check_help_echo;
27809 hlinfo->mouse_face_overlay = overlay;
27810
27811 /* Clear the display of the old active region, if any. */
27812 if (clear_mouse_face (hlinfo))
27813 cursor = No_Cursor;
27814
27815 /* If no overlay applies, get a text property. */
27816 if (NILP (overlay))
27817 mouse_face = Fget_text_property (position, Qmouse_face, object);
27818
27819 /* Next, compute the bounds of the mouse highlighting and
27820 display it. */
27821 if (!NILP (mouse_face) && STRINGP (object))
27822 {
27823 /* The mouse-highlighting comes from a display string
27824 with a mouse-face. */
27825 Lisp_Object s, e;
27826 ptrdiff_t ignore;
27827
27828 s = Fprevious_single_property_change
27829 (make_number (pos + 1), Qmouse_face, object, Qnil);
27830 e = Fnext_single_property_change
27831 (position, Qmouse_face, object, Qnil);
27832 if (NILP (s))
27833 s = make_number (0);
27834 if (NILP (e))
27835 e = make_number (SCHARS (object) - 1);
27836 mouse_face_from_string_pos (w, hlinfo, object,
27837 XINT (s), XINT (e));
27838 hlinfo->mouse_face_past_end = 0;
27839 hlinfo->mouse_face_window = window;
27840 hlinfo->mouse_face_face_id
27841 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
27842 glyph->face_id, 1);
27843 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27844 cursor = No_Cursor;
27845 }
27846 else
27847 {
27848 /* The mouse-highlighting, if any, comes from an overlay
27849 or text property in the buffer. */
27850 Lisp_Object buffer IF_LINT (= Qnil);
27851 Lisp_Object disp_string IF_LINT (= Qnil);
27852
27853 if (STRINGP (object))
27854 {
27855 /* If we are on a display string with no mouse-face,
27856 check if the text under it has one. */
27857 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
27858 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27859 pos = string_buffer_position (object, start);
27860 if (pos > 0)
27861 {
27862 mouse_face = get_char_property_and_overlay
27863 (make_number (pos), Qmouse_face, w->contents, &overlay);
27864 buffer = w->contents;
27865 disp_string = object;
27866 }
27867 }
27868 else
27869 {
27870 buffer = object;
27871 disp_string = Qnil;
27872 }
27873
27874 if (!NILP (mouse_face))
27875 {
27876 Lisp_Object before, after;
27877 Lisp_Object before_string, after_string;
27878 /* To correctly find the limits of mouse highlight
27879 in a bidi-reordered buffer, we must not use the
27880 optimization of limiting the search in
27881 previous-single-property-change and
27882 next-single-property-change, because
27883 rows_from_pos_range needs the real start and end
27884 positions to DTRT in this case. That's because
27885 the first row visible in a window does not
27886 necessarily display the character whose position
27887 is the smallest. */
27888 Lisp_Object lim1 =
27889 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27890 ? Fmarker_position (w->start)
27891 : Qnil;
27892 Lisp_Object lim2 =
27893 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
27894 ? make_number (BUF_Z (XBUFFER (buffer))
27895 - XFASTINT (w->window_end_pos))
27896 : Qnil;
27897
27898 if (NILP (overlay))
27899 {
27900 /* Handle the text property case. */
27901 before = Fprevious_single_property_change
27902 (make_number (pos + 1), Qmouse_face, buffer, lim1);
27903 after = Fnext_single_property_change
27904 (make_number (pos), Qmouse_face, buffer, lim2);
27905 before_string = after_string = Qnil;
27906 }
27907 else
27908 {
27909 /* Handle the overlay case. */
27910 before = Foverlay_start (overlay);
27911 after = Foverlay_end (overlay);
27912 before_string = Foverlay_get (overlay, Qbefore_string);
27913 after_string = Foverlay_get (overlay, Qafter_string);
27914
27915 if (!STRINGP (before_string)) before_string = Qnil;
27916 if (!STRINGP (after_string)) after_string = Qnil;
27917 }
27918
27919 mouse_face_from_buffer_pos (window, hlinfo, pos,
27920 NILP (before)
27921 ? 1
27922 : XFASTINT (before),
27923 NILP (after)
27924 ? BUF_Z (XBUFFER (buffer))
27925 : XFASTINT (after),
27926 before_string, after_string,
27927 disp_string);
27928 cursor = No_Cursor;
27929 }
27930 }
27931 }
27932
27933 check_help_echo:
27934
27935 /* Look for a `help-echo' property. */
27936 if (NILP (help_echo_string)) {
27937 Lisp_Object help, overlay;
27938
27939 /* Check overlays first. */
27940 help = overlay = Qnil;
27941 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
27942 {
27943 overlay = overlay_vec[i];
27944 help = Foverlay_get (overlay, Qhelp_echo);
27945 }
27946
27947 if (!NILP (help))
27948 {
27949 help_echo_string = help;
27950 help_echo_window = window;
27951 help_echo_object = overlay;
27952 help_echo_pos = pos;
27953 }
27954 else
27955 {
27956 Lisp_Object obj = glyph->object;
27957 ptrdiff_t charpos = glyph->charpos;
27958
27959 /* Try text properties. */
27960 if (STRINGP (obj)
27961 && charpos >= 0
27962 && charpos < SCHARS (obj))
27963 {
27964 help = Fget_text_property (make_number (charpos),
27965 Qhelp_echo, obj);
27966 if (NILP (help))
27967 {
27968 /* If the string itself doesn't specify a help-echo,
27969 see if the buffer text ``under'' it does. */
27970 struct glyph_row *r
27971 = MATRIX_ROW (w->current_matrix, vpos);
27972 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
27973 ptrdiff_t p = string_buffer_position (obj, start);
27974 if (p > 0)
27975 {
27976 help = Fget_char_property (make_number (p),
27977 Qhelp_echo, w->contents);
27978 if (!NILP (help))
27979 {
27980 charpos = p;
27981 obj = w->contents;
27982 }
27983 }
27984 }
27985 }
27986 else if (BUFFERP (obj)
27987 && charpos >= BEGV
27988 && charpos < ZV)
27989 help = Fget_text_property (make_number (charpos), Qhelp_echo,
27990 obj);
27991
27992 if (!NILP (help))
27993 {
27994 help_echo_string = help;
27995 help_echo_window = window;
27996 help_echo_object = obj;
27997 help_echo_pos = charpos;
27998 }
27999 }
28000 }
28001
28002 #ifdef HAVE_WINDOW_SYSTEM
28003 /* Look for a `pointer' property. */
28004 if (FRAME_WINDOW_P (f) && NILP (pointer))
28005 {
28006 /* Check overlays first. */
28007 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28008 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28009
28010 if (NILP (pointer))
28011 {
28012 Lisp_Object obj = glyph->object;
28013 ptrdiff_t charpos = glyph->charpos;
28014
28015 /* Try text properties. */
28016 if (STRINGP (obj)
28017 && charpos >= 0
28018 && charpos < SCHARS (obj))
28019 {
28020 pointer = Fget_text_property (make_number (charpos),
28021 Qpointer, obj);
28022 if (NILP (pointer))
28023 {
28024 /* If the string itself doesn't specify a pointer,
28025 see if the buffer text ``under'' it does. */
28026 struct glyph_row *r
28027 = MATRIX_ROW (w->current_matrix, vpos);
28028 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28029 ptrdiff_t p = string_buffer_position (obj, start);
28030 if (p > 0)
28031 pointer = Fget_char_property (make_number (p),
28032 Qpointer, w->contents);
28033 }
28034 }
28035 else if (BUFFERP (obj)
28036 && charpos >= BEGV
28037 && charpos < ZV)
28038 pointer = Fget_text_property (make_number (charpos),
28039 Qpointer, obj);
28040 }
28041 }
28042 #endif /* HAVE_WINDOW_SYSTEM */
28043
28044 BEGV = obegv;
28045 ZV = ozv;
28046 current_buffer = obuf;
28047 }
28048
28049 set_cursor:
28050
28051 #ifdef HAVE_WINDOW_SYSTEM
28052 if (FRAME_WINDOW_P (f))
28053 define_frame_cursor1 (f, cursor, pointer);
28054 #else
28055 /* This is here to prevent a compiler error, about "label at end of
28056 compound statement". */
28057 return;
28058 #endif
28059 }
28060
28061
28062 /* EXPORT for RIF:
28063 Clear any mouse-face on window W. This function is part of the
28064 redisplay interface, and is called from try_window_id and similar
28065 functions to ensure the mouse-highlight is off. */
28066
28067 void
28068 x_clear_window_mouse_face (struct window *w)
28069 {
28070 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28071 Lisp_Object window;
28072
28073 block_input ();
28074 XSETWINDOW (window, w);
28075 if (EQ (window, hlinfo->mouse_face_window))
28076 clear_mouse_face (hlinfo);
28077 unblock_input ();
28078 }
28079
28080
28081 /* EXPORT:
28082 Just discard the mouse face information for frame F, if any.
28083 This is used when the size of F is changed. */
28084
28085 void
28086 cancel_mouse_face (struct frame *f)
28087 {
28088 Lisp_Object window;
28089 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28090
28091 window = hlinfo->mouse_face_window;
28092 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28093 {
28094 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28095 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28096 hlinfo->mouse_face_window = Qnil;
28097 }
28098 }
28099
28100
28101 \f
28102 /***********************************************************************
28103 Exposure Events
28104 ***********************************************************************/
28105
28106 #ifdef HAVE_WINDOW_SYSTEM
28107
28108 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28109 which intersects rectangle R. R is in window-relative coordinates. */
28110
28111 static void
28112 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28113 enum glyph_row_area area)
28114 {
28115 struct glyph *first = row->glyphs[area];
28116 struct glyph *end = row->glyphs[area] + row->used[area];
28117 struct glyph *last;
28118 int first_x, start_x, x;
28119
28120 if (area == TEXT_AREA && row->fill_line_p)
28121 /* If row extends face to end of line write the whole line. */
28122 draw_glyphs (w, 0, row, area,
28123 0, row->used[area],
28124 DRAW_NORMAL_TEXT, 0);
28125 else
28126 {
28127 /* Set START_X to the window-relative start position for drawing glyphs of
28128 AREA. The first glyph of the text area can be partially visible.
28129 The first glyphs of other areas cannot. */
28130 start_x = window_box_left_offset (w, area);
28131 x = start_x;
28132 if (area == TEXT_AREA)
28133 x += row->x;
28134
28135 /* Find the first glyph that must be redrawn. */
28136 while (first < end
28137 && x + first->pixel_width < r->x)
28138 {
28139 x += first->pixel_width;
28140 ++first;
28141 }
28142
28143 /* Find the last one. */
28144 last = first;
28145 first_x = x;
28146 while (last < end
28147 && x < r->x + r->width)
28148 {
28149 x += last->pixel_width;
28150 ++last;
28151 }
28152
28153 /* Repaint. */
28154 if (last > first)
28155 draw_glyphs (w, first_x - start_x, row, area,
28156 first - row->glyphs[area], last - row->glyphs[area],
28157 DRAW_NORMAL_TEXT, 0);
28158 }
28159 }
28160
28161
28162 /* Redraw the parts of the glyph row ROW on window W intersecting
28163 rectangle R. R is in window-relative coordinates. Value is
28164 non-zero if mouse-face was overwritten. */
28165
28166 static int
28167 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28168 {
28169 eassert (row->enabled_p);
28170
28171 if (row->mode_line_p || w->pseudo_window_p)
28172 draw_glyphs (w, 0, row, TEXT_AREA,
28173 0, row->used[TEXT_AREA],
28174 DRAW_NORMAL_TEXT, 0);
28175 else
28176 {
28177 if (row->used[LEFT_MARGIN_AREA])
28178 expose_area (w, row, r, LEFT_MARGIN_AREA);
28179 if (row->used[TEXT_AREA])
28180 expose_area (w, row, r, TEXT_AREA);
28181 if (row->used[RIGHT_MARGIN_AREA])
28182 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28183 draw_row_fringe_bitmaps (w, row);
28184 }
28185
28186 return row->mouse_face_p;
28187 }
28188
28189
28190 /* Redraw those parts of glyphs rows during expose event handling that
28191 overlap other rows. Redrawing of an exposed line writes over parts
28192 of lines overlapping that exposed line; this function fixes that.
28193
28194 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28195 row in W's current matrix that is exposed and overlaps other rows.
28196 LAST_OVERLAPPING_ROW is the last such row. */
28197
28198 static void
28199 expose_overlaps (struct window *w,
28200 struct glyph_row *first_overlapping_row,
28201 struct glyph_row *last_overlapping_row,
28202 XRectangle *r)
28203 {
28204 struct glyph_row *row;
28205
28206 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28207 if (row->overlapping_p)
28208 {
28209 eassert (row->enabled_p && !row->mode_line_p);
28210
28211 row->clip = r;
28212 if (row->used[LEFT_MARGIN_AREA])
28213 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28214
28215 if (row->used[TEXT_AREA])
28216 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28217
28218 if (row->used[RIGHT_MARGIN_AREA])
28219 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28220 row->clip = NULL;
28221 }
28222 }
28223
28224
28225 /* Return non-zero if W's cursor intersects rectangle R. */
28226
28227 static int
28228 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28229 {
28230 XRectangle cr, result;
28231 struct glyph *cursor_glyph;
28232 struct glyph_row *row;
28233
28234 if (w->phys_cursor.vpos >= 0
28235 && w->phys_cursor.vpos < w->current_matrix->nrows
28236 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28237 row->enabled_p)
28238 && row->cursor_in_fringe_p)
28239 {
28240 /* Cursor is in the fringe. */
28241 cr.x = window_box_right_offset (w,
28242 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28243 ? RIGHT_MARGIN_AREA
28244 : TEXT_AREA));
28245 cr.y = row->y;
28246 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28247 cr.height = row->height;
28248 return x_intersect_rectangles (&cr, r, &result);
28249 }
28250
28251 cursor_glyph = get_phys_cursor_glyph (w);
28252 if (cursor_glyph)
28253 {
28254 /* r is relative to W's box, but w->phys_cursor.x is relative
28255 to left edge of W's TEXT area. Adjust it. */
28256 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28257 cr.y = w->phys_cursor.y;
28258 cr.width = cursor_glyph->pixel_width;
28259 cr.height = w->phys_cursor_height;
28260 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28261 I assume the effect is the same -- and this is portable. */
28262 return x_intersect_rectangles (&cr, r, &result);
28263 }
28264 /* If we don't understand the format, pretend we're not in the hot-spot. */
28265 return 0;
28266 }
28267
28268
28269 /* EXPORT:
28270 Draw a vertical window border to the right of window W if W doesn't
28271 have vertical scroll bars. */
28272
28273 void
28274 x_draw_vertical_border (struct window *w)
28275 {
28276 struct frame *f = XFRAME (WINDOW_FRAME (w));
28277
28278 /* We could do better, if we knew what type of scroll-bar the adjacent
28279 windows (on either side) have... But we don't :-(
28280 However, I think this works ok. ++KFS 2003-04-25 */
28281
28282 /* Redraw borders between horizontally adjacent windows. Don't
28283 do it for frames with vertical scroll bars because either the
28284 right scroll bar of a window, or the left scroll bar of its
28285 neighbor will suffice as a border. */
28286 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28287 return;
28288
28289 /* Note: It is necessary to redraw both the left and the right
28290 borders, for when only this single window W is being
28291 redisplayed. */
28292 if (!WINDOW_RIGHTMOST_P (w)
28293 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28294 {
28295 int x0, x1, y0, y1;
28296
28297 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28298 y1 -= 1;
28299
28300 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28301 x1 -= 1;
28302
28303 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28304 }
28305 if (!WINDOW_LEFTMOST_P (w)
28306 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28307 {
28308 int x0, x1, y0, y1;
28309
28310 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28311 y1 -= 1;
28312
28313 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28314 x0 -= 1;
28315
28316 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28317 }
28318 }
28319
28320
28321 /* Redraw the part of window W intersection rectangle FR. Pixel
28322 coordinates in FR are frame-relative. Call this function with
28323 input blocked. Value is non-zero if the exposure overwrites
28324 mouse-face. */
28325
28326 static int
28327 expose_window (struct window *w, XRectangle *fr)
28328 {
28329 struct frame *f = XFRAME (w->frame);
28330 XRectangle wr, r;
28331 int mouse_face_overwritten_p = 0;
28332
28333 /* If window is not yet fully initialized, do nothing. This can
28334 happen when toolkit scroll bars are used and a window is split.
28335 Reconfiguring the scroll bar will generate an expose for a newly
28336 created window. */
28337 if (w->current_matrix == NULL)
28338 return 0;
28339
28340 /* When we're currently updating the window, display and current
28341 matrix usually don't agree. Arrange for a thorough display
28342 later. */
28343 if (w == updated_window)
28344 {
28345 SET_FRAME_GARBAGED (f);
28346 return 0;
28347 }
28348
28349 /* Frame-relative pixel rectangle of W. */
28350 wr.x = WINDOW_LEFT_EDGE_X (w);
28351 wr.y = WINDOW_TOP_EDGE_Y (w);
28352 wr.width = WINDOW_TOTAL_WIDTH (w);
28353 wr.height = WINDOW_TOTAL_HEIGHT (w);
28354
28355 if (x_intersect_rectangles (fr, &wr, &r))
28356 {
28357 int yb = window_text_bottom_y (w);
28358 struct glyph_row *row;
28359 int cursor_cleared_p, phys_cursor_on_p;
28360 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28361
28362 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28363 r.x, r.y, r.width, r.height));
28364
28365 /* Convert to window coordinates. */
28366 r.x -= WINDOW_LEFT_EDGE_X (w);
28367 r.y -= WINDOW_TOP_EDGE_Y (w);
28368
28369 /* Turn off the cursor. */
28370 if (!w->pseudo_window_p
28371 && phys_cursor_in_rect_p (w, &r))
28372 {
28373 x_clear_cursor (w);
28374 cursor_cleared_p = 1;
28375 }
28376 else
28377 cursor_cleared_p = 0;
28378
28379 /* If the row containing the cursor extends face to end of line,
28380 then expose_area might overwrite the cursor outside the
28381 rectangle and thus notice_overwritten_cursor might clear
28382 w->phys_cursor_on_p. We remember the original value and
28383 check later if it is changed. */
28384 phys_cursor_on_p = w->phys_cursor_on_p;
28385
28386 /* Update lines intersecting rectangle R. */
28387 first_overlapping_row = last_overlapping_row = NULL;
28388 for (row = w->current_matrix->rows;
28389 row->enabled_p;
28390 ++row)
28391 {
28392 int y0 = row->y;
28393 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28394
28395 if ((y0 >= r.y && y0 < r.y + r.height)
28396 || (y1 > r.y && y1 < r.y + r.height)
28397 || (r.y >= y0 && r.y < y1)
28398 || (r.y + r.height > y0 && r.y + r.height < y1))
28399 {
28400 /* A header line may be overlapping, but there is no need
28401 to fix overlapping areas for them. KFS 2005-02-12 */
28402 if (row->overlapping_p && !row->mode_line_p)
28403 {
28404 if (first_overlapping_row == NULL)
28405 first_overlapping_row = row;
28406 last_overlapping_row = row;
28407 }
28408
28409 row->clip = fr;
28410 if (expose_line (w, row, &r))
28411 mouse_face_overwritten_p = 1;
28412 row->clip = NULL;
28413 }
28414 else if (row->overlapping_p)
28415 {
28416 /* We must redraw a row overlapping the exposed area. */
28417 if (y0 < r.y
28418 ? y0 + row->phys_height > r.y
28419 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28420 {
28421 if (first_overlapping_row == NULL)
28422 first_overlapping_row = row;
28423 last_overlapping_row = row;
28424 }
28425 }
28426
28427 if (y1 >= yb)
28428 break;
28429 }
28430
28431 /* Display the mode line if there is one. */
28432 if (WINDOW_WANTS_MODELINE_P (w)
28433 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28434 row->enabled_p)
28435 && row->y < r.y + r.height)
28436 {
28437 if (expose_line (w, row, &r))
28438 mouse_face_overwritten_p = 1;
28439 }
28440
28441 if (!w->pseudo_window_p)
28442 {
28443 /* Fix the display of overlapping rows. */
28444 if (first_overlapping_row)
28445 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28446 fr);
28447
28448 /* Draw border between windows. */
28449 x_draw_vertical_border (w);
28450
28451 /* Turn the cursor on again. */
28452 if (cursor_cleared_p
28453 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28454 update_window_cursor (w, 1);
28455 }
28456 }
28457
28458 return mouse_face_overwritten_p;
28459 }
28460
28461
28462
28463 /* Redraw (parts) of all windows in the window tree rooted at W that
28464 intersect R. R contains frame pixel coordinates. Value is
28465 non-zero if the exposure overwrites mouse-face. */
28466
28467 static int
28468 expose_window_tree (struct window *w, XRectangle *r)
28469 {
28470 struct frame *f = XFRAME (w->frame);
28471 int mouse_face_overwritten_p = 0;
28472
28473 while (w && !FRAME_GARBAGED_P (f))
28474 {
28475 if (WINDOWP (w->contents))
28476 mouse_face_overwritten_p
28477 |= expose_window_tree (XWINDOW (w->contents), r);
28478 else
28479 mouse_face_overwritten_p |= expose_window (w, r);
28480
28481 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28482 }
28483
28484 return mouse_face_overwritten_p;
28485 }
28486
28487
28488 /* EXPORT:
28489 Redisplay an exposed area of frame F. X and Y are the upper-left
28490 corner of the exposed rectangle. W and H are width and height of
28491 the exposed area. All are pixel values. W or H zero means redraw
28492 the entire frame. */
28493
28494 void
28495 expose_frame (struct frame *f, int x, int y, int w, int h)
28496 {
28497 XRectangle r;
28498 int mouse_face_overwritten_p = 0;
28499
28500 TRACE ((stderr, "expose_frame "));
28501
28502 /* No need to redraw if frame will be redrawn soon. */
28503 if (FRAME_GARBAGED_P (f))
28504 {
28505 TRACE ((stderr, " garbaged\n"));
28506 return;
28507 }
28508
28509 /* If basic faces haven't been realized yet, there is no point in
28510 trying to redraw anything. This can happen when we get an expose
28511 event while Emacs is starting, e.g. by moving another window. */
28512 if (FRAME_FACE_CACHE (f) == NULL
28513 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28514 {
28515 TRACE ((stderr, " no faces\n"));
28516 return;
28517 }
28518
28519 if (w == 0 || h == 0)
28520 {
28521 r.x = r.y = 0;
28522 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28523 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28524 }
28525 else
28526 {
28527 r.x = x;
28528 r.y = y;
28529 r.width = w;
28530 r.height = h;
28531 }
28532
28533 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28534 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28535
28536 if (WINDOWP (f->tool_bar_window))
28537 mouse_face_overwritten_p
28538 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28539
28540 #ifdef HAVE_X_WINDOWS
28541 #ifndef MSDOS
28542 #if ! defined (USE_X_TOOLKIT) && ! defined (USE_GTK)
28543 if (WINDOWP (f->menu_bar_window))
28544 mouse_face_overwritten_p
28545 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28546 #endif /* not USE_X_TOOLKIT and not USE_GTK */
28547 #endif
28548 #endif
28549
28550 /* Some window managers support a focus-follows-mouse style with
28551 delayed raising of frames. Imagine a partially obscured frame,
28552 and moving the mouse into partially obscured mouse-face on that
28553 frame. The visible part of the mouse-face will be highlighted,
28554 then the WM raises the obscured frame. With at least one WM, KDE
28555 2.1, Emacs is not getting any event for the raising of the frame
28556 (even tried with SubstructureRedirectMask), only Expose events.
28557 These expose events will draw text normally, i.e. not
28558 highlighted. Which means we must redo the highlight here.
28559 Subsume it under ``we love X''. --gerd 2001-08-15 */
28560 /* Included in Windows version because Windows most likely does not
28561 do the right thing if any third party tool offers
28562 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28563 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28564 {
28565 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28566 if (f == hlinfo->mouse_face_mouse_frame)
28567 {
28568 int mouse_x = hlinfo->mouse_face_mouse_x;
28569 int mouse_y = hlinfo->mouse_face_mouse_y;
28570 clear_mouse_face (hlinfo);
28571 note_mouse_highlight (f, mouse_x, mouse_y);
28572 }
28573 }
28574 }
28575
28576
28577 /* EXPORT:
28578 Determine the intersection of two rectangles R1 and R2. Return
28579 the intersection in *RESULT. Value is non-zero if RESULT is not
28580 empty. */
28581
28582 int
28583 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28584 {
28585 XRectangle *left, *right;
28586 XRectangle *upper, *lower;
28587 int intersection_p = 0;
28588
28589 /* Rearrange so that R1 is the left-most rectangle. */
28590 if (r1->x < r2->x)
28591 left = r1, right = r2;
28592 else
28593 left = r2, right = r1;
28594
28595 /* X0 of the intersection is right.x0, if this is inside R1,
28596 otherwise there is no intersection. */
28597 if (right->x <= left->x + left->width)
28598 {
28599 result->x = right->x;
28600
28601 /* The right end of the intersection is the minimum of
28602 the right ends of left and right. */
28603 result->width = (min (left->x + left->width, right->x + right->width)
28604 - result->x);
28605
28606 /* Same game for Y. */
28607 if (r1->y < r2->y)
28608 upper = r1, lower = r2;
28609 else
28610 upper = r2, lower = r1;
28611
28612 /* The upper end of the intersection is lower.y0, if this is inside
28613 of upper. Otherwise, there is no intersection. */
28614 if (lower->y <= upper->y + upper->height)
28615 {
28616 result->y = lower->y;
28617
28618 /* The lower end of the intersection is the minimum of the lower
28619 ends of upper and lower. */
28620 result->height = (min (lower->y + lower->height,
28621 upper->y + upper->height)
28622 - result->y);
28623 intersection_p = 1;
28624 }
28625 }
28626
28627 return intersection_p;
28628 }
28629
28630 #endif /* HAVE_WINDOW_SYSTEM */
28631
28632 \f
28633 /***********************************************************************
28634 Initialization
28635 ***********************************************************************/
28636
28637 void
28638 syms_of_xdisp (void)
28639 {
28640 Vwith_echo_area_save_vector = Qnil;
28641 staticpro (&Vwith_echo_area_save_vector);
28642
28643 Vmessage_stack = Qnil;
28644 staticpro (&Vmessage_stack);
28645
28646 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28647 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
28648
28649 message_dolog_marker1 = Fmake_marker ();
28650 staticpro (&message_dolog_marker1);
28651 message_dolog_marker2 = Fmake_marker ();
28652 staticpro (&message_dolog_marker2);
28653 message_dolog_marker3 = Fmake_marker ();
28654 staticpro (&message_dolog_marker3);
28655
28656 #ifdef GLYPH_DEBUG
28657 defsubr (&Sdump_frame_glyph_matrix);
28658 defsubr (&Sdump_glyph_matrix);
28659 defsubr (&Sdump_glyph_row);
28660 defsubr (&Sdump_tool_bar_row);
28661 defsubr (&Strace_redisplay);
28662 defsubr (&Strace_to_stderr);
28663 #endif
28664 #ifdef HAVE_WINDOW_SYSTEM
28665 defsubr (&Stool_bar_lines_needed);
28666 defsubr (&Slookup_image_map);
28667 #endif
28668 defsubr (&Sformat_mode_line);
28669 defsubr (&Sinvisible_p);
28670 defsubr (&Scurrent_bidi_paragraph_direction);
28671
28672 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28673 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28674 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28675 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28676 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28677 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28678 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28679 DEFSYM (Qeval, "eval");
28680 DEFSYM (QCdata, ":data");
28681 DEFSYM (Qdisplay, "display");
28682 DEFSYM (Qspace_width, "space-width");
28683 DEFSYM (Qraise, "raise");
28684 DEFSYM (Qslice, "slice");
28685 DEFSYM (Qspace, "space");
28686 DEFSYM (Qmargin, "margin");
28687 DEFSYM (Qpointer, "pointer");
28688 DEFSYM (Qleft_margin, "left-margin");
28689 DEFSYM (Qright_margin, "right-margin");
28690 DEFSYM (Qcenter, "center");
28691 DEFSYM (Qline_height, "line-height");
28692 DEFSYM (QCalign_to, ":align-to");
28693 DEFSYM (QCrelative_width, ":relative-width");
28694 DEFSYM (QCrelative_height, ":relative-height");
28695 DEFSYM (QCeval, ":eval");
28696 DEFSYM (QCpropertize, ":propertize");
28697 DEFSYM (QCfile, ":file");
28698 DEFSYM (Qfontified, "fontified");
28699 DEFSYM (Qfontification_functions, "fontification-functions");
28700 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28701 DEFSYM (Qescape_glyph, "escape-glyph");
28702 DEFSYM (Qnobreak_space, "nobreak-space");
28703 DEFSYM (Qimage, "image");
28704 DEFSYM (Qtext, "text");
28705 DEFSYM (Qboth, "both");
28706 DEFSYM (Qboth_horiz, "both-horiz");
28707 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28708 DEFSYM (QCmap, ":map");
28709 DEFSYM (QCpointer, ":pointer");
28710 DEFSYM (Qrect, "rect");
28711 DEFSYM (Qcircle, "circle");
28712 DEFSYM (Qpoly, "poly");
28713 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28714 DEFSYM (Qgrow_only, "grow-only");
28715 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28716 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28717 DEFSYM (Qposition, "position");
28718 DEFSYM (Qbuffer_position, "buffer-position");
28719 DEFSYM (Qobject, "object");
28720 DEFSYM (Qbar, "bar");
28721 DEFSYM (Qhbar, "hbar");
28722 DEFSYM (Qbox, "box");
28723 DEFSYM (Qhollow, "hollow");
28724 DEFSYM (Qhand, "hand");
28725 DEFSYM (Qarrow, "arrow");
28726 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28727
28728 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28729 Fcons (intern_c_string ("void-variable"), Qnil)),
28730 Qnil);
28731 staticpro (&list_of_error);
28732
28733 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28734 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28735 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28736 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28737
28738 echo_buffer[0] = echo_buffer[1] = Qnil;
28739 staticpro (&echo_buffer[0]);
28740 staticpro (&echo_buffer[1]);
28741
28742 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28743 staticpro (&echo_area_buffer[0]);
28744 staticpro (&echo_area_buffer[1]);
28745
28746 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28747 staticpro (&Vmessages_buffer_name);
28748
28749 mode_line_proptrans_alist = Qnil;
28750 staticpro (&mode_line_proptrans_alist);
28751 mode_line_string_list = Qnil;
28752 staticpro (&mode_line_string_list);
28753 mode_line_string_face = Qnil;
28754 staticpro (&mode_line_string_face);
28755 mode_line_string_face_prop = Qnil;
28756 staticpro (&mode_line_string_face_prop);
28757 Vmode_line_unwind_vector = Qnil;
28758 staticpro (&Vmode_line_unwind_vector);
28759
28760 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28761
28762 help_echo_string = Qnil;
28763 staticpro (&help_echo_string);
28764 help_echo_object = Qnil;
28765 staticpro (&help_echo_object);
28766 help_echo_window = Qnil;
28767 staticpro (&help_echo_window);
28768 previous_help_echo_string = Qnil;
28769 staticpro (&previous_help_echo_string);
28770 help_echo_pos = -1;
28771
28772 DEFSYM (Qright_to_left, "right-to-left");
28773 DEFSYM (Qleft_to_right, "left-to-right");
28774
28775 #ifdef HAVE_WINDOW_SYSTEM
28776 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28777 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28778 For example, if a block cursor is over a tab, it will be drawn as
28779 wide as that tab on the display. */);
28780 x_stretch_cursor_p = 0;
28781 #endif
28782
28783 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28784 doc: /* Non-nil means highlight trailing whitespace.
28785 The face used for trailing whitespace is `trailing-whitespace'. */);
28786 Vshow_trailing_whitespace = Qnil;
28787
28788 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28789 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28790 If the value is t, Emacs highlights non-ASCII chars which have the
28791 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28792 or `escape-glyph' face respectively.
28793
28794 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28795 U+2011 (non-breaking hyphen) are affected.
28796
28797 Any other non-nil value means to display these characters as a escape
28798 glyph followed by an ordinary space or hyphen.
28799
28800 A value of nil means no special handling of these characters. */);
28801 Vnobreak_char_display = Qt;
28802
28803 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28804 doc: /* The pointer shape to show in void text areas.
28805 A value of nil means to show the text pointer. Other options are `arrow',
28806 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28807 Vvoid_text_area_pointer = Qarrow;
28808
28809 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28810 doc: /* Non-nil means don't actually do any redisplay.
28811 This is used for internal purposes. */);
28812 Vinhibit_redisplay = Qnil;
28813
28814 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28815 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28816 Vglobal_mode_string = Qnil;
28817
28818 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28819 doc: /* Marker for where to display an arrow on top of the buffer text.
28820 This must be the beginning of a line in order to work.
28821 See also `overlay-arrow-string'. */);
28822 Voverlay_arrow_position = Qnil;
28823
28824 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28825 doc: /* String to display as an arrow in non-window frames.
28826 See also `overlay-arrow-position'. */);
28827 Voverlay_arrow_string = build_pure_c_string ("=>");
28828
28829 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28830 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28831 The symbols on this list are examined during redisplay to determine
28832 where to display overlay arrows. */);
28833 Voverlay_arrow_variable_list
28834 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
28835
28836 DEFVAR_INT ("scroll-step", emacs_scroll_step,
28837 doc: /* The number of lines to try scrolling a window by when point moves out.
28838 If that fails to bring point back on frame, point is centered instead.
28839 If this is zero, point is always centered after it moves off frame.
28840 If you want scrolling to always be a line at a time, you should set
28841 `scroll-conservatively' to a large value rather than set this to 1. */);
28842
28843 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
28844 doc: /* Scroll up to this many lines, to bring point back on screen.
28845 If point moves off-screen, redisplay will scroll by up to
28846 `scroll-conservatively' lines in order to bring point just barely
28847 onto the screen again. If that cannot be done, then redisplay
28848 recenters point as usual.
28849
28850 If the value is greater than 100, redisplay will never recenter point,
28851 but will always scroll just enough text to bring point into view, even
28852 if you move far away.
28853
28854 A value of zero means always recenter point if it moves off screen. */);
28855 scroll_conservatively = 0;
28856
28857 DEFVAR_INT ("scroll-margin", scroll_margin,
28858 doc: /* Number of lines of margin at the top and bottom of a window.
28859 Recenter the window whenever point gets within this many lines
28860 of the top or bottom of the window. */);
28861 scroll_margin = 0;
28862
28863 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
28864 doc: /* Pixels per inch value for non-window system displays.
28865 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
28866 Vdisplay_pixels_per_inch = make_float (72.0);
28867
28868 #ifdef GLYPH_DEBUG
28869 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
28870 #endif
28871
28872 DEFVAR_LISP ("truncate-partial-width-windows",
28873 Vtruncate_partial_width_windows,
28874 doc: /* Non-nil means truncate lines in windows narrower than the frame.
28875 For an integer value, truncate lines in each window narrower than the
28876 full frame width, provided the window width is less than that integer;
28877 otherwise, respect the value of `truncate-lines'.
28878
28879 For any other non-nil value, truncate lines in all windows that do
28880 not span the full frame width.
28881
28882 A value of nil means to respect the value of `truncate-lines'.
28883
28884 If `word-wrap' is enabled, you might want to reduce this. */);
28885 Vtruncate_partial_width_windows = make_number (50);
28886
28887 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
28888 doc: /* Maximum buffer size for which line number should be displayed.
28889 If the buffer is bigger than this, the line number does not appear
28890 in the mode line. A value of nil means no limit. */);
28891 Vline_number_display_limit = Qnil;
28892
28893 DEFVAR_INT ("line-number-display-limit-width",
28894 line_number_display_limit_width,
28895 doc: /* Maximum line width (in characters) for line number display.
28896 If the average length of the lines near point is bigger than this, then the
28897 line number may be omitted from the mode line. */);
28898 line_number_display_limit_width = 200;
28899
28900 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
28901 doc: /* Non-nil means highlight region even in nonselected windows. */);
28902 highlight_nonselected_windows = 0;
28903
28904 DEFVAR_BOOL ("multiple-frames", multiple_frames,
28905 doc: /* Non-nil if more than one frame is visible on this display.
28906 Minibuffer-only frames don't count, but iconified frames do.
28907 This variable is not guaranteed to be accurate except while processing
28908 `frame-title-format' and `icon-title-format'. */);
28909
28910 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
28911 doc: /* Template for displaying the title bar of visible frames.
28912 \(Assuming the window manager supports this feature.)
28913
28914 This variable has the same structure as `mode-line-format', except that
28915 the %c and %l constructs are ignored. It is used only on frames for
28916 which no explicit name has been set \(see `modify-frame-parameters'). */);
28917
28918 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
28919 doc: /* Template for displaying the title bar of an iconified frame.
28920 \(Assuming the window manager supports this feature.)
28921 This variable has the same structure as `mode-line-format' (which see),
28922 and is used only on frames for which no explicit name has been set
28923 \(see `modify-frame-parameters'). */);
28924 Vicon_title_format
28925 = Vframe_title_format
28926 = listn (CONSTYPE_PURE, 3,
28927 intern_c_string ("multiple-frames"),
28928 build_pure_c_string ("%b"),
28929 listn (CONSTYPE_PURE, 4,
28930 empty_unibyte_string,
28931 intern_c_string ("invocation-name"),
28932 build_pure_c_string ("@"),
28933 intern_c_string ("system-name")));
28934
28935 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
28936 doc: /* Maximum number of lines to keep in the message log buffer.
28937 If nil, disable message logging. If t, log messages but don't truncate
28938 the buffer when it becomes large. */);
28939 Vmessage_log_max = make_number (1000);
28940
28941 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
28942 doc: /* Functions called before redisplay, if window sizes have changed.
28943 The value should be a list of functions that take one argument.
28944 Just before redisplay, for each frame, if any of its windows have changed
28945 size since the last redisplay, or have been split or deleted,
28946 all the functions in the list are called, with the frame as argument. */);
28947 Vwindow_size_change_functions = Qnil;
28948
28949 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
28950 doc: /* List of functions to call before redisplaying a window with scrolling.
28951 Each function is called with two arguments, the window and its new
28952 display-start position. Note that these functions are also called by
28953 `set-window-buffer'. Also note that the value of `window-end' is not
28954 valid when these functions are called.
28955
28956 Warning: Do not use this feature to alter the way the window
28957 is scrolled. It is not designed for that, and such use probably won't
28958 work. */);
28959 Vwindow_scroll_functions = Qnil;
28960
28961 DEFVAR_LISP ("window-text-change-functions",
28962 Vwindow_text_change_functions,
28963 doc: /* Functions to call in redisplay when text in the window might change. */);
28964 Vwindow_text_change_functions = Qnil;
28965
28966 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
28967 doc: /* Functions called when redisplay of a window reaches the end trigger.
28968 Each function is called with two arguments, the window and the end trigger value.
28969 See `set-window-redisplay-end-trigger'. */);
28970 Vredisplay_end_trigger_functions = Qnil;
28971
28972 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
28973 doc: /* Non-nil means autoselect window with mouse pointer.
28974 If nil, do not autoselect windows.
28975 A positive number means delay autoselection by that many seconds: a
28976 window is autoselected only after the mouse has remained in that
28977 window for the duration of the delay.
28978 A negative number has a similar effect, but causes windows to be
28979 autoselected only after the mouse has stopped moving. \(Because of
28980 the way Emacs compares mouse events, you will occasionally wait twice
28981 that time before the window gets selected.\)
28982 Any other value means to autoselect window instantaneously when the
28983 mouse pointer enters it.
28984
28985 Autoselection selects the minibuffer only if it is active, and never
28986 unselects the minibuffer if it is active.
28987
28988 When customizing this variable make sure that the actual value of
28989 `focus-follows-mouse' matches the behavior of your window manager. */);
28990 Vmouse_autoselect_window = Qnil;
28991
28992 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
28993 doc: /* Non-nil means automatically resize tool-bars.
28994 This dynamically changes the tool-bar's height to the minimum height
28995 that is needed to make all tool-bar items visible.
28996 If value is `grow-only', the tool-bar's height is only increased
28997 automatically; to decrease the tool-bar height, use \\[recenter]. */);
28998 Vauto_resize_tool_bars = Qt;
28999
29000 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29001 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29002 auto_raise_tool_bar_buttons_p = 1;
29003
29004 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29005 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29006 make_cursor_line_fully_visible_p = 1;
29007
29008 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29009 doc: /* Border below tool-bar in pixels.
29010 If an integer, use it as the height of the border.
29011 If it is one of `internal-border-width' or `border-width', use the
29012 value of the corresponding frame parameter.
29013 Otherwise, no border is added below the tool-bar. */);
29014 Vtool_bar_border = Qinternal_border_width;
29015
29016 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29017 doc: /* Margin around tool-bar buttons in pixels.
29018 If an integer, use that for both horizontal and vertical margins.
29019 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29020 HORZ specifying the horizontal margin, and VERT specifying the
29021 vertical margin. */);
29022 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29023
29024 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29025 doc: /* Relief thickness of tool-bar buttons. */);
29026 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29027
29028 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29029 doc: /* Tool bar style to use.
29030 It can be one of
29031 image - show images only
29032 text - show text only
29033 both - show both, text below image
29034 both-horiz - show text to the right of the image
29035 text-image-horiz - show text to the left of the image
29036 any other - use system default or image if no system default.
29037
29038 This variable only affects the GTK+ toolkit version of Emacs. */);
29039 Vtool_bar_style = Qnil;
29040
29041 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29042 doc: /* Maximum number of characters a label can have to be shown.
29043 The tool bar style must also show labels for this to have any effect, see
29044 `tool-bar-style'. */);
29045 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29046
29047 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29048 doc: /* List of functions to call to fontify regions of text.
29049 Each function is called with one argument POS. Functions must
29050 fontify a region starting at POS in the current buffer, and give
29051 fontified regions the property `fontified'. */);
29052 Vfontification_functions = Qnil;
29053 Fmake_variable_buffer_local (Qfontification_functions);
29054
29055 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29056 unibyte_display_via_language_environment,
29057 doc: /* Non-nil means display unibyte text according to language environment.
29058 Specifically, this means that raw bytes in the range 160-255 decimal
29059 are displayed by converting them to the equivalent multibyte characters
29060 according to the current language environment. As a result, they are
29061 displayed according to the current fontset.
29062
29063 Note that this variable affects only how these bytes are displayed,
29064 but does not change the fact they are interpreted as raw bytes. */);
29065 unibyte_display_via_language_environment = 0;
29066
29067 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29068 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29069 If a float, it specifies a fraction of the mini-window frame's height.
29070 If an integer, it specifies a number of lines. */);
29071 Vmax_mini_window_height = make_float (0.25);
29072
29073 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29074 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29075 A value of nil means don't automatically resize mini-windows.
29076 A value of t means resize them to fit the text displayed in them.
29077 A value of `grow-only', the default, means let mini-windows grow only;
29078 they return to their normal size when the minibuffer is closed, or the
29079 echo area becomes empty. */);
29080 Vresize_mini_windows = Qgrow_only;
29081
29082 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29083 doc: /* Alist specifying how to blink the cursor off.
29084 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29085 `cursor-type' frame-parameter or variable equals ON-STATE,
29086 comparing using `equal', Emacs uses OFF-STATE to specify
29087 how to blink it off. ON-STATE and OFF-STATE are values for
29088 the `cursor-type' frame parameter.
29089
29090 If a frame's ON-STATE has no entry in this list,
29091 the frame's other specifications determine how to blink the cursor off. */);
29092 Vblink_cursor_alist = Qnil;
29093
29094 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29095 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29096 If non-nil, windows are automatically scrolled horizontally to make
29097 point visible. */);
29098 automatic_hscrolling_p = 1;
29099 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29100
29101 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29102 doc: /* How many columns away from the window edge point is allowed to get
29103 before automatic hscrolling will horizontally scroll the window. */);
29104 hscroll_margin = 5;
29105
29106 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29107 doc: /* How many columns to scroll the window when point gets too close to the edge.
29108 When point is less than `hscroll-margin' columns from the window
29109 edge, automatic hscrolling will scroll the window by the amount of columns
29110 determined by this variable. If its value is a positive integer, scroll that
29111 many columns. If it's a positive floating-point number, it specifies the
29112 fraction of the window's width to scroll. If it's nil or zero, point will be
29113 centered horizontally after the scroll. Any other value, including negative
29114 numbers, are treated as if the value were zero.
29115
29116 Automatic hscrolling always moves point outside the scroll margin, so if
29117 point was more than scroll step columns inside the margin, the window will
29118 scroll more than the value given by the scroll step.
29119
29120 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29121 and `scroll-right' overrides this variable's effect. */);
29122 Vhscroll_step = make_number (0);
29123
29124 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29125 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29126 Bind this around calls to `message' to let it take effect. */);
29127 message_truncate_lines = 0;
29128
29129 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29130 doc: /* Normal hook run to update the menu bar definitions.
29131 Redisplay runs this hook before it redisplays the menu bar.
29132 This is used to update submenus such as Buffers,
29133 whose contents depend on various data. */);
29134 Vmenu_bar_update_hook = Qnil;
29135
29136 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29137 doc: /* Frame for which we are updating a menu.
29138 The enable predicate for a menu binding should check this variable. */);
29139 Vmenu_updating_frame = Qnil;
29140
29141 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29142 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29143 inhibit_menubar_update = 0;
29144
29145 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29146 doc: /* Prefix prepended to all continuation lines at display time.
29147 The value may be a string, an image, or a stretch-glyph; it is
29148 interpreted in the same way as the value of a `display' text property.
29149
29150 This variable is overridden by any `wrap-prefix' text or overlay
29151 property.
29152
29153 To add a prefix to non-continuation lines, use `line-prefix'. */);
29154 Vwrap_prefix = Qnil;
29155 DEFSYM (Qwrap_prefix, "wrap-prefix");
29156 Fmake_variable_buffer_local (Qwrap_prefix);
29157
29158 DEFVAR_LISP ("line-prefix", Vline_prefix,
29159 doc: /* Prefix prepended to all non-continuation lines at display time.
29160 The value may be a string, an image, or a stretch-glyph; it is
29161 interpreted in the same way as the value of a `display' text property.
29162
29163 This variable is overridden by any `line-prefix' text or overlay
29164 property.
29165
29166 To add a prefix to continuation lines, use `wrap-prefix'. */);
29167 Vline_prefix = Qnil;
29168 DEFSYM (Qline_prefix, "line-prefix");
29169 Fmake_variable_buffer_local (Qline_prefix);
29170
29171 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29172 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29173 inhibit_eval_during_redisplay = 0;
29174
29175 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29176 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29177 inhibit_free_realized_faces = 0;
29178
29179 #ifdef GLYPH_DEBUG
29180 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29181 doc: /* Inhibit try_window_id display optimization. */);
29182 inhibit_try_window_id = 0;
29183
29184 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29185 doc: /* Inhibit try_window_reusing display optimization. */);
29186 inhibit_try_window_reusing = 0;
29187
29188 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29189 doc: /* Inhibit try_cursor_movement display optimization. */);
29190 inhibit_try_cursor_movement = 0;
29191 #endif /* GLYPH_DEBUG */
29192
29193 DEFVAR_INT ("overline-margin", overline_margin,
29194 doc: /* Space between overline and text, in pixels.
29195 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29196 margin to the character height. */);
29197 overline_margin = 2;
29198
29199 DEFVAR_INT ("underline-minimum-offset",
29200 underline_minimum_offset,
29201 doc: /* Minimum distance between baseline and underline.
29202 This can improve legibility of underlined text at small font sizes,
29203 particularly when using variable `x-use-underline-position-properties'
29204 with fonts that specify an UNDERLINE_POSITION relatively close to the
29205 baseline. The default value is 1. */);
29206 underline_minimum_offset = 1;
29207
29208 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29209 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29210 This feature only works when on a window system that can change
29211 cursor shapes. */);
29212 display_hourglass_p = 1;
29213
29214 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29215 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29216 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29217
29218 hourglass_atimer = NULL;
29219 hourglass_shown_p = 0;
29220
29221 DEFSYM (Qglyphless_char, "glyphless-char");
29222 DEFSYM (Qhex_code, "hex-code");
29223 DEFSYM (Qempty_box, "empty-box");
29224 DEFSYM (Qthin_space, "thin-space");
29225 DEFSYM (Qzero_width, "zero-width");
29226
29227 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29228 /* Intern this now in case it isn't already done.
29229 Setting this variable twice is harmless.
29230 But don't staticpro it here--that is done in alloc.c. */
29231 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29232 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29233
29234 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29235 doc: /* Char-table defining glyphless characters.
29236 Each element, if non-nil, should be one of the following:
29237 an ASCII acronym string: display this string in a box
29238 `hex-code': display the hexadecimal code of a character in a box
29239 `empty-box': display as an empty box
29240 `thin-space': display as 1-pixel width space
29241 `zero-width': don't display
29242 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29243 display method for graphical terminals and text terminals respectively.
29244 GRAPHICAL and TEXT should each have one of the values listed above.
29245
29246 The char-table has one extra slot to control the display of a character for
29247 which no font is found. This slot only takes effect on graphical terminals.
29248 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29249 `thin-space'. The default is `empty-box'. */);
29250 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29251 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29252 Qempty_box);
29253
29254 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29255 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29256 Vdebug_on_message = Qnil;
29257 }
29258
29259
29260 /* Initialize this module when Emacs starts. */
29261
29262 void
29263 init_xdisp (void)
29264 {
29265 current_header_line_height = current_mode_line_height = -1;
29266
29267 CHARPOS (this_line_start_pos) = 0;
29268
29269 if (!noninteractive)
29270 {
29271 struct window *m = XWINDOW (minibuf_window);
29272 Lisp_Object frame = m->frame;
29273 struct frame *f = XFRAME (frame);
29274 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29275 struct window *r = XWINDOW (root);
29276 int i;
29277
29278 echo_area_window = minibuf_window;
29279
29280 r->top_line = FRAME_TOP_MARGIN (f);
29281 r->total_lines = FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f);
29282 r->total_cols = FRAME_COLS (f);
29283
29284 m->top_line = FRAME_LINES (f) - 1;
29285 m->total_lines = 1;
29286 m->total_cols = FRAME_COLS (f);
29287
29288 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29289 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29290 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29291
29292 /* The default ellipsis glyphs `...'. */
29293 for (i = 0; i < 3; ++i)
29294 default_invis_vector[i] = make_number ('.');
29295 }
29296
29297 {
29298 /* Allocate the buffer for frame titles.
29299 Also used for `format-mode-line'. */
29300 int size = 100;
29301 mode_line_noprop_buf = xmalloc (size);
29302 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29303 mode_line_noprop_ptr = mode_line_noprop_buf;
29304 mode_line_target = MODE_LINE_DISPLAY;
29305 }
29306
29307 help_echo_showing_p = 0;
29308 }
29309
29310 /* Platform-independent portion of hourglass implementation. */
29311
29312 /* Cancel a currently active hourglass timer, and start a new one. */
29313 void
29314 start_hourglass (void)
29315 {
29316 #if defined (HAVE_WINDOW_SYSTEM)
29317 EMACS_TIME delay;
29318
29319 cancel_hourglass ();
29320
29321 if (INTEGERP (Vhourglass_delay)
29322 && XINT (Vhourglass_delay) > 0)
29323 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29324 TYPE_MAXIMUM (time_t)),
29325 0);
29326 else if (FLOATP (Vhourglass_delay)
29327 && XFLOAT_DATA (Vhourglass_delay) > 0)
29328 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29329 else
29330 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29331
29332 #ifdef HAVE_NTGUI
29333 {
29334 extern void w32_note_current_window (void);
29335 w32_note_current_window ();
29336 }
29337 #endif /* HAVE_NTGUI */
29338
29339 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29340 show_hourglass, NULL);
29341 #endif
29342 }
29343
29344
29345 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29346 shown. */
29347 void
29348 cancel_hourglass (void)
29349 {
29350 #if defined (HAVE_WINDOW_SYSTEM)
29351 if (hourglass_atimer)
29352 {
29353 cancel_atimer (hourglass_atimer);
29354 hourglass_atimer = NULL;
29355 }
29356
29357 if (hourglass_shown_p)
29358 hide_hourglass ();
29359 #endif
29360 }