* src/xdisp.c (display_mode_lines): Keep selected_window and selected_frame
[bpt/emacs.git] / src / xdisp.c
1 /* Display generation from window structure and buffer text.
2
3 Copyright (C) 1985-1988, 1993-1995, 1997-2012 Free Software Foundation, Inc.
4
5 This file is part of GNU Emacs.
6
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
19
20 /* New redisplay written by Gerd Moellmann <gerd@gnu.org>.
21
22 Redisplay.
23
24 Emacs separates the task of updating the display from code
25 modifying global state, e.g. buffer text. This way functions
26 operating on buffers don't also have to be concerned with updating
27 the display.
28
29 Updating the display is triggered by the Lisp interpreter when it
30 decides it's time to do it. This is done either automatically for
31 you as part of the interpreter's command loop or as the result of
32 calling Lisp functions like `sit-for'. The C function `redisplay'
33 in xdisp.c is the only entry into the inner redisplay code.
34
35 The following diagram shows how redisplay code is invoked. As you
36 can see, Lisp calls redisplay and vice versa. Under window systems
37 like X, some portions of the redisplay code are also called
38 asynchronously during mouse movement or expose events. It is very
39 important that these code parts do NOT use the C library (malloc,
40 free) because many C libraries under Unix are not reentrant. They
41 may also NOT call functions of the Lisp interpreter which could
42 change the interpreter's state. If you don't follow these rules,
43 you will encounter bugs which are very hard to explain.
44
45 +--------------+ redisplay +----------------+
46 | Lisp machine |---------------->| Redisplay code |<--+
47 +--------------+ (xdisp.c) +----------------+ |
48 ^ | |
49 +----------------------------------+ |
50 Don't use this path when called |
51 asynchronously! |
52 |
53 expose_window (asynchronous) |
54 |
55 X expose events -----+
56
57 What does redisplay do? Obviously, it has to figure out somehow what
58 has been changed since the last time the display has been updated,
59 and to make these changes visible. Preferably it would do that in
60 a moderately intelligent way, i.e. fast.
61
62 Changes in buffer text can be deduced from window and buffer
63 structures, and from some global variables like `beg_unchanged' and
64 `end_unchanged'. The contents of the display are additionally
65 recorded in a `glyph matrix', a two-dimensional matrix of glyph
66 structures. Each row in such a matrix corresponds to a line on the
67 display, and each glyph in a row corresponds to a column displaying
68 a character, an image, or what else. This matrix is called the
69 `current glyph matrix' or `current matrix' in redisplay
70 terminology.
71
72 For buffer parts that have been changed since the last update, a
73 second glyph matrix is constructed, the so called `desired glyph
74 matrix' or short `desired matrix'. Current and desired matrix are
75 then compared to find a cheap way to update the display, e.g. by
76 reusing part of the display by scrolling lines.
77
78 You will find a lot of redisplay optimizations when you start
79 looking at the innards of redisplay. The overall goal of all these
80 optimizations is to make redisplay fast because it is done
81 frequently. Some of these optimizations are implemented by the
82 following functions:
83
84 . try_cursor_movement
85
86 This function tries to update the display if the text in the
87 window did not change and did not scroll, only point moved, and
88 it did not move off the displayed portion of the text.
89
90 . try_window_reusing_current_matrix
91
92 This function reuses the current matrix of a window when text
93 has not changed, but the window start changed (e.g., due to
94 scrolling).
95
96 . try_window_id
97
98 This function attempts to redisplay a window by reusing parts of
99 its existing display. It finds and reuses the part that was not
100 changed, and redraws the rest.
101
102 . try_window
103
104 This function performs the full redisplay of a single window
105 assuming that its fonts were not changed and that the cursor
106 will not end up in the scroll margins. (Loading fonts requires
107 re-adjustment of dimensions of glyph matrices, which makes this
108 method impossible to use.)
109
110 These optimizations are tried in sequence (some can be skipped if
111 it is known that they are not applicable). If none of the
112 optimizations were successful, redisplay calls redisplay_windows,
113 which performs a full redisplay of all windows.
114
115 Desired matrices.
116
117 Desired matrices are always built per Emacs window. The function
118 `display_line' is the central function to look at if you are
119 interested. It constructs one row in a desired matrix given an
120 iterator structure containing both a buffer position and a
121 description of the environment in which the text is to be
122 displayed. But this is too early, read on.
123
124 Characters and pixmaps displayed for a range of buffer text depend
125 on various settings of buffers and windows, on overlays and text
126 properties, on display tables, on selective display. The good news
127 is that all this hairy stuff is hidden behind a small set of
128 interface functions taking an iterator structure (struct it)
129 argument.
130
131 Iteration over things to be displayed is then simple. It is
132 started by initializing an iterator with a call to init_iterator,
133 passing it the buffer position where to start iteration. For
134 iteration over strings, pass -1 as the position to init_iterator,
135 and call reseat_to_string when the string is ready, to initialize
136 the iterator for that string. Thereafter, calls to
137 get_next_display_element fill the iterator structure with relevant
138 information about the next thing to display. Calls to
139 set_iterator_to_next move the iterator to the next thing.
140
141 Besides this, an iterator also contains information about the
142 display environment in which glyphs for display elements are to be
143 produced. It has fields for the width and height of the display,
144 the information whether long lines are truncated or continued, a
145 current X and Y position, and lots of other stuff you can better
146 see in dispextern.h.
147
148 Glyphs in a desired matrix are normally constructed in a loop
149 calling get_next_display_element and then PRODUCE_GLYPHS. The call
150 to PRODUCE_GLYPHS will fill the iterator structure with pixel
151 information about the element being displayed and at the same time
152 produce glyphs for it. If the display element fits on the line
153 being displayed, set_iterator_to_next is called next, otherwise the
154 glyphs produced are discarded. The function display_line is the
155 workhorse of filling glyph rows in the desired matrix with glyphs.
156 In addition to producing glyphs, it also handles line truncation
157 and continuation, word wrap, and cursor positioning (for the
158 latter, see also set_cursor_from_row).
159
160 Frame matrices.
161
162 That just couldn't be all, could it? What about terminal types not
163 supporting operations on sub-windows of the screen? To update the
164 display on such a terminal, window-based glyph matrices are not
165 well suited. To be able to reuse part of the display (scrolling
166 lines up and down), we must instead have a view of the whole
167 screen. This is what `frame matrices' are for. They are a trick.
168
169 Frames on terminals like above have a glyph pool. Windows on such
170 a frame sub-allocate their glyph memory from their frame's glyph
171 pool. The frame itself is given its own glyph matrices. By
172 coincidence---or maybe something else---rows in window glyph
173 matrices are slices of corresponding rows in frame matrices. Thus
174 writing to window matrices implicitly updates a frame matrix which
175 provides us with the view of the whole screen that we originally
176 wanted to have without having to move many bytes around. To be
177 honest, there is a little bit more done, but not much more. If you
178 plan to extend that code, take a look at dispnew.c. The function
179 build_frame_matrix is a good starting point.
180
181 Bidirectional display.
182
183 Bidirectional display adds quite some hair to this already complex
184 design. The good news are that a large portion of that hairy stuff
185 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
186 reordering engine which is called by set_iterator_to_next and
187 returns the next character to display in the visual order. See
188 commentary on bidi.c for more details. As far as redisplay is
189 concerned, the effect of calling bidi_move_to_visually_next, the
190 main interface of the reordering engine, is that the iterator gets
191 magically placed on the buffer or string position that is to be
192 displayed next. In other words, a linear iteration through the
193 buffer/string is replaced with a non-linear one. All the rest of
194 the redisplay is oblivious to the bidi reordering.
195
196 Well, almost oblivious---there are still complications, most of
197 them due to the fact that buffer and string positions no longer
198 change monotonously with glyph indices in a glyph row. Moreover,
199 for continued lines, the buffer positions may not even be
200 monotonously changing with vertical positions. Also, accounting
201 for face changes, overlays, etc. becomes more complex because
202 non-linear iteration could potentially skip many positions with
203 changes, and then cross them again on the way back...
204
205 One other prominent effect of bidirectional display is that some
206 paragraphs of text need to be displayed starting at the right
207 margin of the window---the so-called right-to-left, or R2L
208 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
209 which have their reversed_p flag set. The bidi reordering engine
210 produces characters in such rows starting from the character which
211 should be the rightmost on display. PRODUCE_GLYPHS then reverses
212 the order, when it fills up the glyph row whose reversed_p flag is
213 set, by prepending each new glyph to what is already there, instead
214 of appending it. When the glyph row is complete, the function
215 extend_face_to_end_of_line fills the empty space to the left of the
216 leftmost character with special glyphs, which will display as,
217 well, empty. On text terminals, these special glyphs are simply
218 blank characters. On graphics terminals, there's a single stretch
219 glyph of a suitably computed width. Both the blanks and the
220 stretch glyph are given the face of the background of the line.
221 This way, the terminal-specific back-end can still draw the glyphs
222 left to right, even for R2L lines.
223
224 Bidirectional display and character compositions
225
226 Some scripts cannot be displayed by drawing each character
227 individually, because adjacent characters change each other's shape
228 on display. For example, Arabic and Indic scripts belong to this
229 category.
230
231 Emacs display supports this by providing "character compositions",
232 most of which is implemented in composite.c. During the buffer
233 scan that delivers characters to PRODUCE_GLYPHS, if the next
234 character to be delivered is a composed character, the iteration
235 calls composition_reseat_it and next_element_from_composition. If
236 they succeed to compose the character with one or more of the
237 following characters, the whole sequence of characters that where
238 composed is recorded in the `struct composition_it' object that is
239 part of the buffer iterator. The composed sequence could produce
240 one or more font glyphs (called "grapheme clusters") on the screen.
241 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS
242 in the direction corresponding to the current bidi scan direction
243 (recorded in the scan_dir member of the `struct bidi_it' object
244 that is part of the buffer iterator). In particular, if the bidi
245 iterator currently scans the buffer backwards, the grapheme
246 clusters are delivered back to front. This reorders the grapheme
247 clusters as appropriate for the current bidi context. Note that
248 this means that the grapheme clusters are always stored in the
249 LGSTRING object (see composite.c) in the logical order.
250
251 Moving an iterator in bidirectional text
252 without producing glyphs
253
254 Note one important detail mentioned above: that the bidi reordering
255 engine, driven by the iterator, produces characters in R2L rows
256 starting at the character that will be the rightmost on display.
257 As far as the iterator is concerned, the geometry of such rows is
258 still left to right, i.e. the iterator "thinks" the first character
259 is at the leftmost pixel position. The iterator does not know that
260 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator
261 delivers. This is important when functions from the move_it_*
262 family are used to get to certain screen position or to match
263 screen coordinates with buffer coordinates: these functions use the
264 iterator geometry, which is left to right even in R2L paragraphs.
265 This works well with most callers of move_it_*, because they need
266 to get to a specific column, and columns are still numbered in the
267 reading order, i.e. the rightmost character in a R2L paragraph is
268 still column zero. But some callers do not get well with this; a
269 notable example is mouse clicks that need to find the character
270 that corresponds to certain pixel coordinates. See
271 buffer_posn_from_coords in dispnew.c for how this is handled. */
272
273 #include <config.h>
274 #include <stdio.h>
275 #include <limits.h>
276
277 #include "lisp.h"
278 #include "atimer.h"
279 #include "keyboard.h"
280 #include "frame.h"
281 #include "window.h"
282 #include "termchar.h"
283 #include "dispextern.h"
284 #include "character.h"
285 #include "buffer.h"
286 #include "charset.h"
287 #include "indent.h"
288 #include "commands.h"
289 #include "keymap.h"
290 #include "macros.h"
291 #include "disptab.h"
292 #include "termhooks.h"
293 #include "termopts.h"
294 #include "intervals.h"
295 #include "coding.h"
296 #include "process.h"
297 #include "region-cache.h"
298 #include "font.h"
299 #include "fontset.h"
300 #include "blockinput.h"
301
302 #ifdef HAVE_X_WINDOWS
303 #include "xterm.h"
304 #endif
305 #ifdef HAVE_NTGUI
306 #include "w32term.h"
307 #endif
308 #ifdef HAVE_NS
309 #include "nsterm.h"
310 #endif
311 #ifdef USE_GTK
312 #include "gtkutil.h"
313 #endif
314
315 #include "font.h"
316
317 #ifndef FRAME_X_OUTPUT
318 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
319 #endif
320
321 #define INFINITY 10000000
322
323 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
324 Lisp_Object Qwindow_scroll_functions;
325 static Lisp_Object Qwindow_text_change_functions;
326 static Lisp_Object Qredisplay_end_trigger_functions;
327 Lisp_Object Qinhibit_point_motion_hooks;
328 static Lisp_Object QCeval, QCpropertize;
329 Lisp_Object QCfile, QCdata;
330 static Lisp_Object Qfontified;
331 static Lisp_Object Qgrow_only;
332 static Lisp_Object Qinhibit_eval_during_redisplay;
333 static Lisp_Object Qbuffer_position, Qposition, Qobject;
334 static Lisp_Object Qright_to_left, Qleft_to_right;
335
336 /* Cursor shapes. */
337 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
338
339 /* Pointer shapes. */
340 static Lisp_Object Qarrow, Qhand;
341 Lisp_Object Qtext;
342
343 /* Holds the list (error). */
344 static Lisp_Object list_of_error;
345
346 static Lisp_Object Qfontification_functions;
347
348 static Lisp_Object Qwrap_prefix;
349 static Lisp_Object Qline_prefix;
350 static Lisp_Object Qredisplay_internal;
351
352 /* Non-nil means don't actually do any redisplay. */
353
354 Lisp_Object Qinhibit_redisplay;
355
356 /* Names of text properties relevant for redisplay. */
357
358 Lisp_Object Qdisplay;
359
360 Lisp_Object Qspace, QCalign_to;
361 static Lisp_Object QCrelative_width, QCrelative_height;
362 Lisp_Object Qleft_margin, Qright_margin;
363 static Lisp_Object Qspace_width, Qraise;
364 static Lisp_Object Qslice;
365 Lisp_Object Qcenter;
366 static Lisp_Object Qmargin, Qpointer;
367 static Lisp_Object Qline_height;
368
369 /* These setters are used only in this file, so they can be private. */
370 static void
371 wset_base_line_number (struct window *w, Lisp_Object val)
372 {
373 w->base_line_number = val;
374 }
375 static void
376 wset_base_line_pos (struct window *w, Lisp_Object val)
377 {
378 w->base_line_pos = val;
379 }
380 static void
381 wset_column_number_displayed (struct window *w, Lisp_Object val)
382 {
383 w->column_number_displayed = val;
384 }
385 static void
386 wset_region_showing (struct window *w, Lisp_Object val)
387 {
388 w->region_showing = val;
389 }
390
391 #ifdef HAVE_WINDOW_SYSTEM
392
393 /* Test if overflow newline into fringe. Called with iterator IT
394 at or past right window margin, and with IT->current_x set. */
395
396 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
397 (!NILP (Voverflow_newline_into_fringe) \
398 && FRAME_WINDOW_P ((IT)->f) \
399 && ((IT)->bidi_it.paragraph_dir == R2L \
400 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
401 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
402 && (IT)->current_x == (IT)->last_visible_x \
403 && (IT)->line_wrap != WORD_WRAP)
404
405 #else /* !HAVE_WINDOW_SYSTEM */
406 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
407 #endif /* HAVE_WINDOW_SYSTEM */
408
409 /* Test if the display element loaded in IT, or the underlying buffer
410 or string character, is a space or a TAB character. This is used
411 to determine where word wrapping can occur. */
412
413 #define IT_DISPLAYING_WHITESPACE(it) \
414 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
415 || ((STRINGP (it->string) \
416 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
417 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
418 || (it->s \
419 && (it->s[IT_BYTEPOS (*it)] == ' ' \
420 || it->s[IT_BYTEPOS (*it)] == '\t')) \
421 || (IT_BYTEPOS (*it) < ZV_BYTE \
422 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
423 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
424
425 /* Name of the face used to highlight trailing whitespace. */
426
427 static Lisp_Object Qtrailing_whitespace;
428
429 /* Name and number of the face used to highlight escape glyphs. */
430
431 static Lisp_Object Qescape_glyph;
432
433 /* Name and number of the face used to highlight non-breaking spaces. */
434
435 static Lisp_Object Qnobreak_space;
436
437 /* The symbol `image' which is the car of the lists used to represent
438 images in Lisp. Also a tool bar style. */
439
440 Lisp_Object Qimage;
441
442 /* The image map types. */
443 Lisp_Object QCmap;
444 static Lisp_Object QCpointer;
445 static Lisp_Object Qrect, Qcircle, Qpoly;
446
447 /* Tool bar styles */
448 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
449
450 /* Non-zero means print newline to stdout before next mini-buffer
451 message. */
452
453 int noninteractive_need_newline;
454
455 /* Non-zero means print newline to message log before next message. */
456
457 static int message_log_need_newline;
458
459 /* Three markers that message_dolog uses.
460 It could allocate them itself, but that causes trouble
461 in handling memory-full errors. */
462 static Lisp_Object message_dolog_marker1;
463 static Lisp_Object message_dolog_marker2;
464 static Lisp_Object message_dolog_marker3;
465 \f
466 /* The buffer position of the first character appearing entirely or
467 partially on the line of the selected window which contains the
468 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
469 redisplay optimization in redisplay_internal. */
470
471 static struct text_pos this_line_start_pos;
472
473 /* Number of characters past the end of the line above, including the
474 terminating newline. */
475
476 static struct text_pos this_line_end_pos;
477
478 /* The vertical positions and the height of this line. */
479
480 static int this_line_vpos;
481 static int this_line_y;
482 static int this_line_pixel_height;
483
484 /* X position at which this display line starts. Usually zero;
485 negative if first character is partially visible. */
486
487 static int this_line_start_x;
488
489 /* The smallest character position seen by move_it_* functions as they
490 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
491 hscrolled lines, see display_line. */
492
493 static struct text_pos this_line_min_pos;
494
495 /* Buffer that this_line_.* variables are referring to. */
496
497 static struct buffer *this_line_buffer;
498
499
500 /* Values of those variables at last redisplay are stored as
501 properties on `overlay-arrow-position' symbol. However, if
502 Voverlay_arrow_position is a marker, last-arrow-position is its
503 numerical position. */
504
505 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
506
507 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
508 properties on a symbol in overlay-arrow-variable-list. */
509
510 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
511
512 Lisp_Object Qmenu_bar_update_hook;
513
514 /* Nonzero if an overlay arrow has been displayed in this window. */
515
516 static int overlay_arrow_seen;
517
518 /* Vector containing glyphs for an ellipsis `...'. */
519
520 static Lisp_Object default_invis_vector[3];
521
522 /* This is the window where the echo area message was displayed. It
523 is always a mini-buffer window, but it may not be the same window
524 currently active as a mini-buffer. */
525
526 Lisp_Object echo_area_window;
527
528 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
529 pushes the current message and the value of
530 message_enable_multibyte on the stack, the function restore_message
531 pops the stack and displays MESSAGE again. */
532
533 static Lisp_Object Vmessage_stack;
534
535 /* Nonzero means multibyte characters were enabled when the echo area
536 message was specified. */
537
538 static int message_enable_multibyte;
539
540 /* Nonzero if we should redraw the mode lines on the next redisplay. */
541
542 int update_mode_lines;
543
544 /* Nonzero if window sizes or contents have changed since last
545 redisplay that finished. */
546
547 int windows_or_buffers_changed;
548
549 /* Nonzero means a frame's cursor type has been changed. */
550
551 int cursor_type_changed;
552
553 /* Nonzero after display_mode_line if %l was used and it displayed a
554 line number. */
555
556 static int line_number_displayed;
557
558 /* The name of the *Messages* buffer, a string. */
559
560 static Lisp_Object Vmessages_buffer_name;
561
562 /* Current, index 0, and last displayed echo area message. Either
563 buffers from echo_buffers, or nil to indicate no message. */
564
565 Lisp_Object echo_area_buffer[2];
566
567 /* The buffers referenced from echo_area_buffer. */
568
569 static Lisp_Object echo_buffer[2];
570
571 /* A vector saved used in with_area_buffer to reduce consing. */
572
573 static Lisp_Object Vwith_echo_area_save_vector;
574
575 /* Non-zero means display_echo_area should display the last echo area
576 message again. Set by redisplay_preserve_echo_area. */
577
578 static int display_last_displayed_message_p;
579
580 /* Nonzero if echo area is being used by print; zero if being used by
581 message. */
582
583 static int message_buf_print;
584
585 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
586
587 static Lisp_Object Qinhibit_menubar_update;
588 static Lisp_Object Qmessage_truncate_lines;
589
590 /* Set to 1 in clear_message to make redisplay_internal aware
591 of an emptied echo area. */
592
593 static int message_cleared_p;
594
595 /* A scratch glyph row with contents used for generating truncation
596 glyphs. Also used in direct_output_for_insert. */
597
598 #define MAX_SCRATCH_GLYPHS 100
599 static struct glyph_row scratch_glyph_row;
600 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
601
602 /* Ascent and height of the last line processed by move_it_to. */
603
604 static int last_max_ascent, last_height;
605
606 /* Non-zero if there's a help-echo in the echo area. */
607
608 int help_echo_showing_p;
609
610 /* If >= 0, computed, exact values of mode-line and header-line height
611 to use in the macros CURRENT_MODE_LINE_HEIGHT and
612 CURRENT_HEADER_LINE_HEIGHT. */
613
614 int current_mode_line_height, current_header_line_height;
615
616 /* The maximum distance to look ahead for text properties. Values
617 that are too small let us call compute_char_face and similar
618 functions too often which is expensive. Values that are too large
619 let us call compute_char_face and alike too often because we
620 might not be interested in text properties that far away. */
621
622 #define TEXT_PROP_DISTANCE_LIMIT 100
623
624 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
625 iterator state and later restore it. This is needed because the
626 bidi iterator on bidi.c keeps a stacked cache of its states, which
627 is really a singleton. When we use scratch iterator objects to
628 move around the buffer, we can cause the bidi cache to be pushed or
629 popped, and therefore we need to restore the cache state when we
630 return to the original iterator. */
631 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
632 do { \
633 if (CACHE) \
634 bidi_unshelve_cache (CACHE, 1); \
635 ITCOPY = ITORIG; \
636 CACHE = bidi_shelve_cache (); \
637 } while (0)
638
639 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
640 do { \
641 if (pITORIG != pITCOPY) \
642 *(pITORIG) = *(pITCOPY); \
643 bidi_unshelve_cache (CACHE, 0); \
644 CACHE = NULL; \
645 } while (0)
646
647 #ifdef GLYPH_DEBUG
648
649 /* Non-zero means print traces of redisplay if compiled with
650 GLYPH_DEBUG defined. */
651
652 int trace_redisplay_p;
653
654 #endif /* GLYPH_DEBUG */
655
656 #ifdef DEBUG_TRACE_MOVE
657 /* Non-zero means trace with TRACE_MOVE to stderr. */
658 int trace_move;
659
660 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
661 #else
662 #define TRACE_MOVE(x) (void) 0
663 #endif
664
665 static Lisp_Object Qauto_hscroll_mode;
666
667 /* Buffer being redisplayed -- for redisplay_window_error. */
668
669 static struct buffer *displayed_buffer;
670
671 /* Value returned from text property handlers (see below). */
672
673 enum prop_handled
674 {
675 HANDLED_NORMALLY,
676 HANDLED_RECOMPUTE_PROPS,
677 HANDLED_OVERLAY_STRING_CONSUMED,
678 HANDLED_RETURN
679 };
680
681 /* A description of text properties that redisplay is interested
682 in. */
683
684 struct props
685 {
686 /* The name of the property. */
687 Lisp_Object *name;
688
689 /* A unique index for the property. */
690 enum prop_idx idx;
691
692 /* A handler function called to set up iterator IT from the property
693 at IT's current position. Value is used to steer handle_stop. */
694 enum prop_handled (*handler) (struct it *it);
695 };
696
697 static enum prop_handled handle_face_prop (struct it *);
698 static enum prop_handled handle_invisible_prop (struct it *);
699 static enum prop_handled handle_display_prop (struct it *);
700 static enum prop_handled handle_composition_prop (struct it *);
701 static enum prop_handled handle_overlay_change (struct it *);
702 static enum prop_handled handle_fontified_prop (struct it *);
703
704 /* Properties handled by iterators. */
705
706 static struct props it_props[] =
707 {
708 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
709 /* Handle `face' before `display' because some sub-properties of
710 `display' need to know the face. */
711 {&Qface, FACE_PROP_IDX, handle_face_prop},
712 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
713 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
714 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
715 {NULL, 0, NULL}
716 };
717
718 /* Value is the position described by X. If X is a marker, value is
719 the marker_position of X. Otherwise, value is X. */
720
721 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
722
723 /* Enumeration returned by some move_it_.* functions internally. */
724
725 enum move_it_result
726 {
727 /* Not used. Undefined value. */
728 MOVE_UNDEFINED,
729
730 /* Move ended at the requested buffer position or ZV. */
731 MOVE_POS_MATCH_OR_ZV,
732
733 /* Move ended at the requested X pixel position. */
734 MOVE_X_REACHED,
735
736 /* Move within a line ended at the end of a line that must be
737 continued. */
738 MOVE_LINE_CONTINUED,
739
740 /* Move within a line ended at the end of a line that would
741 be displayed truncated. */
742 MOVE_LINE_TRUNCATED,
743
744 /* Move within a line ended at a line end. */
745 MOVE_NEWLINE_OR_CR
746 };
747
748 /* This counter is used to clear the face cache every once in a while
749 in redisplay_internal. It is incremented for each redisplay.
750 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
751 cleared. */
752
753 #define CLEAR_FACE_CACHE_COUNT 500
754 static int clear_face_cache_count;
755
756 /* Similarly for the image cache. */
757
758 #ifdef HAVE_WINDOW_SYSTEM
759 #define CLEAR_IMAGE_CACHE_COUNT 101
760 static int clear_image_cache_count;
761
762 /* Null glyph slice */
763 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
764 #endif
765
766 /* True while redisplay_internal is in progress. */
767
768 bool redisplaying_p;
769
770 static Lisp_Object Qinhibit_free_realized_faces;
771 static Lisp_Object Qmode_line_default_help_echo;
772
773 /* If a string, XTread_socket generates an event to display that string.
774 (The display is done in read_char.) */
775
776 Lisp_Object help_echo_string;
777 Lisp_Object help_echo_window;
778 Lisp_Object help_echo_object;
779 ptrdiff_t help_echo_pos;
780
781 /* Temporary variable for XTread_socket. */
782
783 Lisp_Object previous_help_echo_string;
784
785 /* Platform-independent portion of hourglass implementation. */
786
787 /* Non-zero means an hourglass cursor is currently shown. */
788 int hourglass_shown_p;
789
790 /* If non-null, an asynchronous timer that, when it expires, displays
791 an hourglass cursor on all frames. */
792 struct atimer *hourglass_atimer;
793
794 /* Name of the face used to display glyphless characters. */
795 Lisp_Object Qglyphless_char;
796
797 /* Symbol for the purpose of Vglyphless_char_display. */
798 static Lisp_Object Qglyphless_char_display;
799
800 /* Method symbols for Vglyphless_char_display. */
801 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
802
803 /* Default pixel width of `thin-space' display method. */
804 #define THIN_SPACE_WIDTH 1
805
806 /* Default number of seconds to wait before displaying an hourglass
807 cursor. */
808 #define DEFAULT_HOURGLASS_DELAY 1
809
810 \f
811 /* Function prototypes. */
812
813 static void setup_for_ellipsis (struct it *, int);
814 static void set_iterator_to_next (struct it *, int);
815 static void mark_window_display_accurate_1 (struct window *, int);
816 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
817 static int display_prop_string_p (Lisp_Object, Lisp_Object);
818 static int cursor_row_p (struct glyph_row *);
819 static int redisplay_mode_lines (Lisp_Object, int);
820 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
821
822 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
823
824 static void handle_line_prefix (struct it *);
825
826 static void pint2str (char *, int, ptrdiff_t);
827 static void pint2hrstr (char *, int, ptrdiff_t);
828 static struct text_pos run_window_scroll_functions (Lisp_Object,
829 struct text_pos);
830 static void reconsider_clip_changes (struct window *, struct buffer *);
831 static int text_outside_line_unchanged_p (struct window *,
832 ptrdiff_t, ptrdiff_t);
833 static void store_mode_line_noprop_char (char);
834 static int store_mode_line_noprop (const char *, int, int);
835 static void handle_stop (struct it *);
836 static void handle_stop_backwards (struct it *, ptrdiff_t);
837 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
838 static void ensure_echo_area_buffers (void);
839 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
840 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
841 static int with_echo_area_buffer (struct window *, int,
842 int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
843 ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
844 static void clear_garbaged_frames (void);
845 static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
846 static void pop_message (void);
847 static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
848 static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
849 static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
850 static int display_echo_area (struct window *);
851 static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
852 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
853 static Lisp_Object unwind_redisplay (Lisp_Object);
854 static int string_char_and_length (const unsigned char *, int *);
855 static struct text_pos display_prop_end (struct it *, Lisp_Object,
856 struct text_pos);
857 static int compute_window_start_on_continuation_line (struct window *);
858 static void insert_left_trunc_glyphs (struct it *);
859 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
860 Lisp_Object);
861 static void extend_face_to_end_of_line (struct it *);
862 static int append_space_for_newline (struct it *, int);
863 static int cursor_row_fully_visible_p (struct window *, int, int);
864 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
865 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
866 static int trailing_whitespace_p (ptrdiff_t);
867 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
868 static void push_it (struct it *, struct text_pos *);
869 static void iterate_out_of_display_property (struct it *);
870 static void pop_it (struct it *);
871 static void sync_frame_with_window_matrix_rows (struct window *);
872 static void select_frame_for_redisplay (Lisp_Object);
873 static void redisplay_internal (void);
874 static int echo_area_display (int);
875 static void redisplay_windows (Lisp_Object);
876 static void redisplay_window (Lisp_Object, int);
877 static Lisp_Object redisplay_window_error (Lisp_Object);
878 static Lisp_Object redisplay_window_0 (Lisp_Object);
879 static Lisp_Object redisplay_window_1 (Lisp_Object);
880 static int set_cursor_from_row (struct window *, struct glyph_row *,
881 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
882 int, int);
883 static int update_menu_bar (struct frame *, int, int);
884 static int try_window_reusing_current_matrix (struct window *);
885 static int try_window_id (struct window *);
886 static int display_line (struct it *);
887 static int display_mode_lines (struct window *);
888 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
889 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
890 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
891 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
892 static void display_menu_bar (struct window *);
893 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
894 ptrdiff_t *);
895 static int display_string (const char *, Lisp_Object, Lisp_Object,
896 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
897 static void compute_line_metrics (struct it *);
898 static void run_redisplay_end_trigger_hook (struct it *);
899 static int get_overlay_strings (struct it *, ptrdiff_t);
900 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
901 static void next_overlay_string (struct it *);
902 static void reseat (struct it *, struct text_pos, int);
903 static void reseat_1 (struct it *, struct text_pos, int);
904 static void back_to_previous_visible_line_start (struct it *);
905 void reseat_at_previous_visible_line_start (struct it *);
906 static void reseat_at_next_visible_line_start (struct it *, int);
907 static int next_element_from_ellipsis (struct it *);
908 static int next_element_from_display_vector (struct it *);
909 static int next_element_from_string (struct it *);
910 static int next_element_from_c_string (struct it *);
911 static int next_element_from_buffer (struct it *);
912 static int next_element_from_composition (struct it *);
913 static int next_element_from_image (struct it *);
914 static int next_element_from_stretch (struct it *);
915 static void load_overlay_strings (struct it *, ptrdiff_t);
916 static int init_from_display_pos (struct it *, struct window *,
917 struct display_pos *);
918 static void reseat_to_string (struct it *, const char *,
919 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
920 static int get_next_display_element (struct it *);
921 static enum move_it_result
922 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
923 enum move_operation_enum);
924 void move_it_vertically_backward (struct it *, int);
925 static void get_visually_first_element (struct it *);
926 static void init_to_row_start (struct it *, struct window *,
927 struct glyph_row *);
928 static int init_to_row_end (struct it *, struct window *,
929 struct glyph_row *);
930 static void back_to_previous_line_start (struct it *);
931 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
932 static struct text_pos string_pos_nchars_ahead (struct text_pos,
933 Lisp_Object, ptrdiff_t);
934 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
935 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
936 static ptrdiff_t number_of_chars (const char *, int);
937 static void compute_stop_pos (struct it *);
938 static void compute_string_pos (struct text_pos *, struct text_pos,
939 Lisp_Object);
940 static int face_before_or_after_it_pos (struct it *, int);
941 static ptrdiff_t next_overlay_change (ptrdiff_t);
942 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
943 Lisp_Object, struct text_pos *, ptrdiff_t, int);
944 static int handle_single_display_spec (struct it *, Lisp_Object,
945 Lisp_Object, Lisp_Object,
946 struct text_pos *, ptrdiff_t, int, int);
947 static int underlying_face_id (struct it *);
948 static int in_ellipses_for_invisible_text_p (struct display_pos *,
949 struct window *);
950
951 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
952 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
953
954 #ifdef HAVE_WINDOW_SYSTEM
955
956 static void x_consider_frame_title (Lisp_Object);
957 static int tool_bar_lines_needed (struct frame *, int *);
958 static void update_tool_bar (struct frame *, int);
959 static void build_desired_tool_bar_string (struct frame *f);
960 static int redisplay_tool_bar (struct frame *);
961 static void display_tool_bar_line (struct it *, int);
962 static void notice_overwritten_cursor (struct window *,
963 enum glyph_row_area,
964 int, int, int, int);
965 static void append_stretch_glyph (struct it *, Lisp_Object,
966 int, int, int);
967
968
969 #endif /* HAVE_WINDOW_SYSTEM */
970
971 static void produce_special_glyphs (struct it *, enum display_element_type);
972 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
973 static int coords_in_mouse_face_p (struct window *, int, int);
974
975
976 \f
977 /***********************************************************************
978 Window display dimensions
979 ***********************************************************************/
980
981 /* Return the bottom boundary y-position for text lines in window W.
982 This is the first y position at which a line cannot start.
983 It is relative to the top of the window.
984
985 This is the height of W minus the height of a mode line, if any. */
986
987 int
988 window_text_bottom_y (struct window *w)
989 {
990 int height = WINDOW_TOTAL_HEIGHT (w);
991
992 if (WINDOW_WANTS_MODELINE_P (w))
993 height -= CURRENT_MODE_LINE_HEIGHT (w);
994 return height;
995 }
996
997 /* Return the pixel width of display area AREA of window W. AREA < 0
998 means return the total width of W, not including fringes to
999 the left and right of the window. */
1000
1001 int
1002 window_box_width (struct window *w, int area)
1003 {
1004 int cols = XFASTINT (w->total_cols);
1005 int pixels = 0;
1006
1007 if (!w->pseudo_window_p)
1008 {
1009 cols -= WINDOW_SCROLL_BAR_COLS (w);
1010
1011 if (area == TEXT_AREA)
1012 {
1013 if (INTEGERP (w->left_margin_cols))
1014 cols -= XFASTINT (w->left_margin_cols);
1015 if (INTEGERP (w->right_margin_cols))
1016 cols -= XFASTINT (w->right_margin_cols);
1017 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1018 }
1019 else if (area == LEFT_MARGIN_AREA)
1020 {
1021 cols = (INTEGERP (w->left_margin_cols)
1022 ? XFASTINT (w->left_margin_cols) : 0);
1023 pixels = 0;
1024 }
1025 else if (area == RIGHT_MARGIN_AREA)
1026 {
1027 cols = (INTEGERP (w->right_margin_cols)
1028 ? XFASTINT (w->right_margin_cols) : 0);
1029 pixels = 0;
1030 }
1031 }
1032
1033 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1034 }
1035
1036
1037 /* Return the pixel height of the display area of window W, not
1038 including mode lines of W, if any. */
1039
1040 int
1041 window_box_height (struct window *w)
1042 {
1043 struct frame *f = XFRAME (w->frame);
1044 int height = WINDOW_TOTAL_HEIGHT (w);
1045
1046 eassert (height >= 0);
1047
1048 /* Note: the code below that determines the mode-line/header-line
1049 height is essentially the same as that contained in the macro
1050 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1051 the appropriate glyph row has its `mode_line_p' flag set,
1052 and if it doesn't, uses estimate_mode_line_height instead. */
1053
1054 if (WINDOW_WANTS_MODELINE_P (w))
1055 {
1056 struct glyph_row *ml_row
1057 = (w->current_matrix && w->current_matrix->rows
1058 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1059 : 0);
1060 if (ml_row && ml_row->mode_line_p)
1061 height -= ml_row->height;
1062 else
1063 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1064 }
1065
1066 if (WINDOW_WANTS_HEADER_LINE_P (w))
1067 {
1068 struct glyph_row *hl_row
1069 = (w->current_matrix && w->current_matrix->rows
1070 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1071 : 0);
1072 if (hl_row && hl_row->mode_line_p)
1073 height -= hl_row->height;
1074 else
1075 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1076 }
1077
1078 /* With a very small font and a mode-line that's taller than
1079 default, we might end up with a negative height. */
1080 return max (0, height);
1081 }
1082
1083 /* Return the window-relative coordinate of the left edge of display
1084 area AREA of window W. AREA < 0 means return the left edge of the
1085 whole window, to the right of the left fringe of W. */
1086
1087 int
1088 window_box_left_offset (struct window *w, int area)
1089 {
1090 int x;
1091
1092 if (w->pseudo_window_p)
1093 return 0;
1094
1095 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1096
1097 if (area == TEXT_AREA)
1098 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1099 + window_box_width (w, LEFT_MARGIN_AREA));
1100 else if (area == RIGHT_MARGIN_AREA)
1101 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1102 + window_box_width (w, LEFT_MARGIN_AREA)
1103 + window_box_width (w, TEXT_AREA)
1104 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1105 ? 0
1106 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1107 else if (area == LEFT_MARGIN_AREA
1108 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1109 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1110
1111 return x;
1112 }
1113
1114
1115 /* Return the window-relative coordinate of the right edge of display
1116 area AREA of window W. AREA < 0 means return the right edge of the
1117 whole window, to the left of the right fringe of W. */
1118
1119 int
1120 window_box_right_offset (struct window *w, int area)
1121 {
1122 return window_box_left_offset (w, area) + window_box_width (w, area);
1123 }
1124
1125 /* Return the frame-relative coordinate of the left edge of display
1126 area AREA of window W. AREA < 0 means return the left edge of the
1127 whole window, to the right of the left fringe of W. */
1128
1129 int
1130 window_box_left (struct window *w, int area)
1131 {
1132 struct frame *f = XFRAME (w->frame);
1133 int x;
1134
1135 if (w->pseudo_window_p)
1136 return FRAME_INTERNAL_BORDER_WIDTH (f);
1137
1138 x = (WINDOW_LEFT_EDGE_X (w)
1139 + window_box_left_offset (w, area));
1140
1141 return x;
1142 }
1143
1144
1145 /* Return the frame-relative coordinate of the right edge of display
1146 area AREA of window W. AREA < 0 means return the right edge of the
1147 whole window, to the left of the right fringe of W. */
1148
1149 int
1150 window_box_right (struct window *w, int area)
1151 {
1152 return window_box_left (w, area) + window_box_width (w, area);
1153 }
1154
1155 /* Get the bounding box of the display area AREA of window W, without
1156 mode lines, in frame-relative coordinates. AREA < 0 means the
1157 whole window, not including the left and right fringes of
1158 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1159 coordinates of the upper-left corner of the box. Return in
1160 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1161
1162 void
1163 window_box (struct window *w, int area, int *box_x, int *box_y,
1164 int *box_width, int *box_height)
1165 {
1166 if (box_width)
1167 *box_width = window_box_width (w, area);
1168 if (box_height)
1169 *box_height = window_box_height (w);
1170 if (box_x)
1171 *box_x = window_box_left (w, area);
1172 if (box_y)
1173 {
1174 *box_y = WINDOW_TOP_EDGE_Y (w);
1175 if (WINDOW_WANTS_HEADER_LINE_P (w))
1176 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1177 }
1178 }
1179
1180
1181 /* Get the bounding box of the display area AREA of window W, without
1182 mode lines. AREA < 0 means the whole window, not including the
1183 left and right fringe of the window. Return in *TOP_LEFT_X
1184 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1185 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1186 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1187 box. */
1188
1189 static void
1190 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1191 int *bottom_right_x, int *bottom_right_y)
1192 {
1193 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1194 bottom_right_y);
1195 *bottom_right_x += *top_left_x;
1196 *bottom_right_y += *top_left_y;
1197 }
1198
1199
1200 \f
1201 /***********************************************************************
1202 Utilities
1203 ***********************************************************************/
1204
1205 /* Return the bottom y-position of the line the iterator IT is in.
1206 This can modify IT's settings. */
1207
1208 int
1209 line_bottom_y (struct it *it)
1210 {
1211 int line_height = it->max_ascent + it->max_descent;
1212 int line_top_y = it->current_y;
1213
1214 if (line_height == 0)
1215 {
1216 if (last_height)
1217 line_height = last_height;
1218 else if (IT_CHARPOS (*it) < ZV)
1219 {
1220 move_it_by_lines (it, 1);
1221 line_height = (it->max_ascent || it->max_descent
1222 ? it->max_ascent + it->max_descent
1223 : last_height);
1224 }
1225 else
1226 {
1227 struct glyph_row *row = it->glyph_row;
1228
1229 /* Use the default character height. */
1230 it->glyph_row = NULL;
1231 it->what = IT_CHARACTER;
1232 it->c = ' ';
1233 it->len = 1;
1234 PRODUCE_GLYPHS (it);
1235 line_height = it->ascent + it->descent;
1236 it->glyph_row = row;
1237 }
1238 }
1239
1240 return line_top_y + line_height;
1241 }
1242
1243 /* Subroutine of pos_visible_p below. Extracts a display string, if
1244 any, from the display spec given as its argument. */
1245 static Lisp_Object
1246 string_from_display_spec (Lisp_Object spec)
1247 {
1248 if (CONSP (spec))
1249 {
1250 while (CONSP (spec))
1251 {
1252 if (STRINGP (XCAR (spec)))
1253 return XCAR (spec);
1254 spec = XCDR (spec);
1255 }
1256 }
1257 else if (VECTORP (spec))
1258 {
1259 ptrdiff_t i;
1260
1261 for (i = 0; i < ASIZE (spec); i++)
1262 {
1263 if (STRINGP (AREF (spec, i)))
1264 return AREF (spec, i);
1265 }
1266 return Qnil;
1267 }
1268
1269 return spec;
1270 }
1271
1272
1273 /* Limit insanely large values of W->hscroll on frame F to the largest
1274 value that will still prevent first_visible_x and last_visible_x of
1275 'struct it' from overflowing an int. */
1276 static int
1277 window_hscroll_limited (struct window *w, struct frame *f)
1278 {
1279 ptrdiff_t window_hscroll = w->hscroll;
1280 int window_text_width = window_box_width (w, TEXT_AREA);
1281 int colwidth = FRAME_COLUMN_WIDTH (f);
1282
1283 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1284 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1285
1286 return window_hscroll;
1287 }
1288
1289 /* Return 1 if position CHARPOS is visible in window W.
1290 CHARPOS < 0 means return info about WINDOW_END position.
1291 If visible, set *X and *Y to pixel coordinates of top left corner.
1292 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1293 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1294
1295 int
1296 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1297 int *rtop, int *rbot, int *rowh, int *vpos)
1298 {
1299 struct it it;
1300 void *itdata = bidi_shelve_cache ();
1301 struct text_pos top;
1302 int visible_p = 0;
1303 struct buffer *old_buffer = NULL;
1304
1305 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1306 return visible_p;
1307
1308 if (XBUFFER (w->buffer) != current_buffer)
1309 {
1310 old_buffer = current_buffer;
1311 set_buffer_internal_1 (XBUFFER (w->buffer));
1312 }
1313
1314 SET_TEXT_POS_FROM_MARKER (top, w->start);
1315 /* Scrolling a minibuffer window via scroll bar when the echo area
1316 shows long text sometimes resets the minibuffer contents behind
1317 our backs. */
1318 if (CHARPOS (top) > ZV)
1319 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1320
1321 /* Compute exact mode line heights. */
1322 if (WINDOW_WANTS_MODELINE_P (w))
1323 current_mode_line_height
1324 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1325 BVAR (current_buffer, mode_line_format));
1326
1327 if (WINDOW_WANTS_HEADER_LINE_P (w))
1328 current_header_line_height
1329 = display_mode_line (w, HEADER_LINE_FACE_ID,
1330 BVAR (current_buffer, header_line_format));
1331
1332 start_display (&it, w, top);
1333 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1334 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1335
1336 if (charpos >= 0
1337 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1338 && IT_CHARPOS (it) >= charpos)
1339 /* When scanning backwards under bidi iteration, move_it_to
1340 stops at or _before_ CHARPOS, because it stops at or to
1341 the _right_ of the character at CHARPOS. */
1342 || (it.bidi_p && it.bidi_it.scan_dir == -1
1343 && IT_CHARPOS (it) <= charpos)))
1344 {
1345 /* We have reached CHARPOS, or passed it. How the call to
1346 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1347 or covered by a display property, move_it_to stops at the end
1348 of the invisible text, to the right of CHARPOS. (ii) If
1349 CHARPOS is in a display vector, move_it_to stops on its last
1350 glyph. */
1351 int top_x = it.current_x;
1352 int top_y = it.current_y;
1353 /* Calling line_bottom_y may change it.method, it.position, etc. */
1354 enum it_method it_method = it.method;
1355 int bottom_y = (last_height = 0, line_bottom_y (&it));
1356 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1357
1358 if (top_y < window_top_y)
1359 visible_p = bottom_y > window_top_y;
1360 else if (top_y < it.last_visible_y)
1361 visible_p = 1;
1362 if (bottom_y >= it.last_visible_y
1363 && it.bidi_p && it.bidi_it.scan_dir == -1
1364 && IT_CHARPOS (it) < charpos)
1365 {
1366 /* When the last line of the window is scanned backwards
1367 under bidi iteration, we could be duped into thinking
1368 that we have passed CHARPOS, when in fact move_it_to
1369 simply stopped short of CHARPOS because it reached
1370 last_visible_y. To see if that's what happened, we call
1371 move_it_to again with a slightly larger vertical limit,
1372 and see if it actually moved vertically; if it did, we
1373 didn't really reach CHARPOS, which is beyond window end. */
1374 struct it save_it = it;
1375 /* Why 10? because we don't know how many canonical lines
1376 will the height of the next line(s) be. So we guess. */
1377 int ten_more_lines =
1378 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1379
1380 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1381 MOVE_TO_POS | MOVE_TO_Y);
1382 if (it.current_y > top_y)
1383 visible_p = 0;
1384
1385 it = save_it;
1386 }
1387 if (visible_p)
1388 {
1389 if (it_method == GET_FROM_DISPLAY_VECTOR)
1390 {
1391 /* We stopped on the last glyph of a display vector.
1392 Try and recompute. Hack alert! */
1393 if (charpos < 2 || top.charpos >= charpos)
1394 top_x = it.glyph_row->x;
1395 else
1396 {
1397 struct it it2;
1398 start_display (&it2, w, top);
1399 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1400 get_next_display_element (&it2);
1401 PRODUCE_GLYPHS (&it2);
1402 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1403 || it2.current_x > it2.last_visible_x)
1404 top_x = it.glyph_row->x;
1405 else
1406 {
1407 top_x = it2.current_x;
1408 top_y = it2.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 int newline_in_string = 0;
1418
1419 if (STRINGP (string))
1420 {
1421 const char *s = SSDATA (string);
1422 const char *e = s + SBYTES (string);
1423 while (s < e)
1424 {
1425 if (*s++ == '\n')
1426 {
1427 newline_in_string = 1;
1428 break;
1429 }
1430 }
1431 }
1432 /* The tricky code below is needed because there's a
1433 discrepancy between move_it_to and how we set cursor
1434 when the display line ends in a newline from a
1435 display string. move_it_to will stop _after_ such
1436 display strings, whereas set_cursor_from_row
1437 conspires with cursor_row_p to place the cursor on
1438 the first glyph produced from the display string. */
1439
1440 /* We have overshoot PT because it is covered by a
1441 display property whose value is a string. If the
1442 string includes embedded newlines, we are also in the
1443 wrong display line. Backtrack to the correct line,
1444 where the display string begins. */
1445 if (newline_in_string)
1446 {
1447 Lisp_Object startpos, endpos;
1448 EMACS_INT start, end;
1449 struct it it3;
1450 int it3_moved;
1451
1452 /* Find the first and the last buffer positions
1453 covered by the display string. */
1454 endpos =
1455 Fnext_single_char_property_change (cpos, Qdisplay,
1456 Qnil, Qnil);
1457 startpos =
1458 Fprevious_single_char_property_change (endpos, Qdisplay,
1459 Qnil, Qnil);
1460 start = XFASTINT (startpos);
1461 end = XFASTINT (endpos);
1462 /* Move to the last buffer position before the
1463 display property. */
1464 start_display (&it3, w, top);
1465 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1466 /* Move forward one more line if the position before
1467 the display string is a newline or if it is the
1468 rightmost character on a line that is
1469 continued or word-wrapped. */
1470 if (it3.method == GET_FROM_BUFFER
1471 && it3.c == '\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 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1540 {
1541 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1542 + it3.glyph_row->used[TEXT_AREA];
1543
1544 while (EQ ((g - 1)->object, string))
1545 {
1546 --g;
1547 top_x -= g->pixel_width;
1548 }
1549 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1550 + it3.glyph_row->used[TEXT_AREA]);
1551 }
1552 }
1553 }
1554
1555 *x = top_x;
1556 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1557 *rtop = max (0, window_top_y - top_y);
1558 *rbot = max (0, bottom_y - it.last_visible_y);
1559 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1560 - max (top_y, window_top_y)));
1561 *vpos = it.vpos;
1562 }
1563 }
1564 else
1565 {
1566 /* We were asked to provide info about WINDOW_END. */
1567 struct it it2;
1568 void *it2data = NULL;
1569
1570 SAVE_IT (it2, it, it2data);
1571 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1572 move_it_by_lines (&it, 1);
1573 if (charpos < IT_CHARPOS (it)
1574 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1575 {
1576 visible_p = 1;
1577 RESTORE_IT (&it2, &it2, it2data);
1578 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1579 *x = it2.current_x;
1580 *y = it2.current_y + it2.max_ascent - it2.ascent;
1581 *rtop = max (0, -it2.current_y);
1582 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1583 - it.last_visible_y));
1584 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1585 it.last_visible_y)
1586 - max (it2.current_y,
1587 WINDOW_HEADER_LINE_HEIGHT (w))));
1588 *vpos = it2.vpos;
1589 }
1590 else
1591 bidi_unshelve_cache (it2data, 1);
1592 }
1593 bidi_unshelve_cache (itdata, 0);
1594
1595 if (old_buffer)
1596 set_buffer_internal_1 (old_buffer);
1597
1598 current_header_line_height = current_mode_line_height = -1;
1599
1600 if (visible_p && w->hscroll > 0)
1601 *x -=
1602 window_hscroll_limited (w, WINDOW_XFRAME (w))
1603 * WINDOW_FRAME_COLUMN_WIDTH (w);
1604
1605 #if 0
1606 /* Debugging code. */
1607 if (visible_p)
1608 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1609 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1610 else
1611 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1612 #endif
1613
1614 return visible_p;
1615 }
1616
1617
1618 /* Return the next character from STR. Return in *LEN the length of
1619 the character. This is like STRING_CHAR_AND_LENGTH but never
1620 returns an invalid character. If we find one, we return a `?', but
1621 with the length of the invalid character. */
1622
1623 static int
1624 string_char_and_length (const unsigned char *str, int *len)
1625 {
1626 int c;
1627
1628 c = STRING_CHAR_AND_LENGTH (str, *len);
1629 if (!CHAR_VALID_P (c))
1630 /* We may not change the length here because other places in Emacs
1631 don't use this function, i.e. they silently accept invalid
1632 characters. */
1633 c = '?';
1634
1635 return c;
1636 }
1637
1638
1639
1640 /* Given a position POS containing a valid character and byte position
1641 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1642
1643 static struct text_pos
1644 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1645 {
1646 eassert (STRINGP (string) && nchars >= 0);
1647
1648 if (STRING_MULTIBYTE (string))
1649 {
1650 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1651 int len;
1652
1653 while (nchars--)
1654 {
1655 string_char_and_length (p, &len);
1656 p += len;
1657 CHARPOS (pos) += 1;
1658 BYTEPOS (pos) += len;
1659 }
1660 }
1661 else
1662 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1663
1664 return pos;
1665 }
1666
1667
1668 /* Value is the text position, i.e. character and byte position,
1669 for character position CHARPOS in STRING. */
1670
1671 static struct text_pos
1672 string_pos (ptrdiff_t charpos, Lisp_Object string)
1673 {
1674 struct text_pos pos;
1675 eassert (STRINGP (string));
1676 eassert (charpos >= 0);
1677 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1678 return pos;
1679 }
1680
1681
1682 /* Value is a text position, i.e. character and byte position, for
1683 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1684 means recognize multibyte characters. */
1685
1686 static struct text_pos
1687 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1688 {
1689 struct text_pos pos;
1690
1691 eassert (s != NULL);
1692 eassert (charpos >= 0);
1693
1694 if (multibyte_p)
1695 {
1696 int len;
1697
1698 SET_TEXT_POS (pos, 0, 0);
1699 while (charpos--)
1700 {
1701 string_char_and_length ((const unsigned char *) s, &len);
1702 s += len;
1703 CHARPOS (pos) += 1;
1704 BYTEPOS (pos) += len;
1705 }
1706 }
1707 else
1708 SET_TEXT_POS (pos, charpos, charpos);
1709
1710 return pos;
1711 }
1712
1713
1714 /* Value is the number of characters in C string S. MULTIBYTE_P
1715 non-zero means recognize multibyte characters. */
1716
1717 static ptrdiff_t
1718 number_of_chars (const char *s, int multibyte_p)
1719 {
1720 ptrdiff_t nchars;
1721
1722 if (multibyte_p)
1723 {
1724 ptrdiff_t rest = strlen (s);
1725 int len;
1726 const unsigned char *p = (const unsigned char *) s;
1727
1728 for (nchars = 0; rest > 0; ++nchars)
1729 {
1730 string_char_and_length (p, &len);
1731 rest -= len, p += len;
1732 }
1733 }
1734 else
1735 nchars = strlen (s);
1736
1737 return nchars;
1738 }
1739
1740
1741 /* Compute byte position NEWPOS->bytepos corresponding to
1742 NEWPOS->charpos. POS is a known position in string STRING.
1743 NEWPOS->charpos must be >= POS.charpos. */
1744
1745 static void
1746 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1747 {
1748 eassert (STRINGP (string));
1749 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1750
1751 if (STRING_MULTIBYTE (string))
1752 *newpos = string_pos_nchars_ahead (pos, string,
1753 CHARPOS (*newpos) - CHARPOS (pos));
1754 else
1755 BYTEPOS (*newpos) = CHARPOS (*newpos);
1756 }
1757
1758 /* EXPORT:
1759 Return an estimation of the pixel height of mode or header lines on
1760 frame F. FACE_ID specifies what line's height to estimate. */
1761
1762 int
1763 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1764 {
1765 #ifdef HAVE_WINDOW_SYSTEM
1766 if (FRAME_WINDOW_P (f))
1767 {
1768 int height = FONT_HEIGHT (FRAME_FONT (f));
1769
1770 /* This function is called so early when Emacs starts that the face
1771 cache and mode line face are not yet initialized. */
1772 if (FRAME_FACE_CACHE (f))
1773 {
1774 struct face *face = FACE_FROM_ID (f, face_id);
1775 if (face)
1776 {
1777 if (face->font)
1778 height = FONT_HEIGHT (face->font);
1779 if (face->box_line_width > 0)
1780 height += 2 * face->box_line_width;
1781 }
1782 }
1783
1784 return height;
1785 }
1786 #endif
1787
1788 return 1;
1789 }
1790
1791 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1792 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1793 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1794 not force the value into range. */
1795
1796 void
1797 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1798 int *x, int *y, NativeRectangle *bounds, int noclip)
1799 {
1800
1801 #ifdef HAVE_WINDOW_SYSTEM
1802 if (FRAME_WINDOW_P (f))
1803 {
1804 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1805 even for negative values. */
1806 if (pix_x < 0)
1807 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1808 if (pix_y < 0)
1809 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1810
1811 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1812 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1813
1814 if (bounds)
1815 STORE_NATIVE_RECT (*bounds,
1816 FRAME_COL_TO_PIXEL_X (f, pix_x),
1817 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1818 FRAME_COLUMN_WIDTH (f) - 1,
1819 FRAME_LINE_HEIGHT (f) - 1);
1820
1821 if (!noclip)
1822 {
1823 if (pix_x < 0)
1824 pix_x = 0;
1825 else if (pix_x > FRAME_TOTAL_COLS (f))
1826 pix_x = FRAME_TOTAL_COLS (f);
1827
1828 if (pix_y < 0)
1829 pix_y = 0;
1830 else if (pix_y > FRAME_LINES (f))
1831 pix_y = FRAME_LINES (f);
1832 }
1833 }
1834 #endif
1835
1836 *x = pix_x;
1837 *y = pix_y;
1838 }
1839
1840
1841 /* Find the glyph under window-relative coordinates X/Y in window W.
1842 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1843 strings. Return in *HPOS and *VPOS the row and column number of
1844 the glyph found. Return in *AREA the glyph area containing X.
1845 Value is a pointer to the glyph found or null if X/Y is not on
1846 text, or we can't tell because W's current matrix is not up to
1847 date. */
1848
1849 static
1850 struct glyph *
1851 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1852 int *dx, int *dy, int *area)
1853 {
1854 struct glyph *glyph, *end;
1855 struct glyph_row *row = NULL;
1856 int x0, i;
1857
1858 /* Find row containing Y. Give up if some row is not enabled. */
1859 for (i = 0; i < w->current_matrix->nrows; ++i)
1860 {
1861 row = MATRIX_ROW (w->current_matrix, i);
1862 if (!row->enabled_p)
1863 return NULL;
1864 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1865 break;
1866 }
1867
1868 *vpos = i;
1869 *hpos = 0;
1870
1871 /* Give up if Y is not in the window. */
1872 if (i == w->current_matrix->nrows)
1873 return NULL;
1874
1875 /* Get the glyph area containing X. */
1876 if (w->pseudo_window_p)
1877 {
1878 *area = TEXT_AREA;
1879 x0 = 0;
1880 }
1881 else
1882 {
1883 if (x < window_box_left_offset (w, TEXT_AREA))
1884 {
1885 *area = LEFT_MARGIN_AREA;
1886 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1887 }
1888 else if (x < window_box_right_offset (w, TEXT_AREA))
1889 {
1890 *area = TEXT_AREA;
1891 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1892 }
1893 else
1894 {
1895 *area = RIGHT_MARGIN_AREA;
1896 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1897 }
1898 }
1899
1900 /* Find glyph containing X. */
1901 glyph = row->glyphs[*area];
1902 end = glyph + row->used[*area];
1903 x -= x0;
1904 while (glyph < end && x >= glyph->pixel_width)
1905 {
1906 x -= glyph->pixel_width;
1907 ++glyph;
1908 }
1909
1910 if (glyph == end)
1911 return NULL;
1912
1913 if (dx)
1914 {
1915 *dx = x;
1916 *dy = y - (row->y + row->ascent - glyph->ascent);
1917 }
1918
1919 *hpos = glyph - row->glyphs[*area];
1920 return glyph;
1921 }
1922
1923 /* Convert frame-relative x/y to coordinates relative to window W.
1924 Takes pseudo-windows into account. */
1925
1926 static void
1927 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1928 {
1929 if (w->pseudo_window_p)
1930 {
1931 /* A pseudo-window is always full-width, and starts at the
1932 left edge of the frame, plus a frame border. */
1933 struct frame *f = XFRAME (w->frame);
1934 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1935 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1936 }
1937 else
1938 {
1939 *x -= WINDOW_LEFT_EDGE_X (w);
1940 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1941 }
1942 }
1943
1944 #ifdef HAVE_WINDOW_SYSTEM
1945
1946 /* EXPORT:
1947 Return in RECTS[] at most N clipping rectangles for glyph string S.
1948 Return the number of stored rectangles. */
1949
1950 int
1951 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1952 {
1953 XRectangle r;
1954
1955 if (n <= 0)
1956 return 0;
1957
1958 if (s->row->full_width_p)
1959 {
1960 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1961 r.x = WINDOW_LEFT_EDGE_X (s->w);
1962 r.width = WINDOW_TOTAL_WIDTH (s->w);
1963
1964 /* Unless displaying a mode or menu bar line, which are always
1965 fully visible, clip to the visible part of the row. */
1966 if (s->w->pseudo_window_p)
1967 r.height = s->row->visible_height;
1968 else
1969 r.height = s->height;
1970 }
1971 else
1972 {
1973 /* This is a text line that may be partially visible. */
1974 r.x = window_box_left (s->w, s->area);
1975 r.width = window_box_width (s->w, s->area);
1976 r.height = s->row->visible_height;
1977 }
1978
1979 if (s->clip_head)
1980 if (r.x < s->clip_head->x)
1981 {
1982 if (r.width >= s->clip_head->x - r.x)
1983 r.width -= s->clip_head->x - r.x;
1984 else
1985 r.width = 0;
1986 r.x = s->clip_head->x;
1987 }
1988 if (s->clip_tail)
1989 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
1990 {
1991 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
1992 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
1993 else
1994 r.width = 0;
1995 }
1996
1997 /* If S draws overlapping rows, it's sufficient to use the top and
1998 bottom of the window for clipping because this glyph string
1999 intentionally draws over other lines. */
2000 if (s->for_overlaps)
2001 {
2002 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2003 r.height = window_text_bottom_y (s->w) - r.y;
2004
2005 /* Alas, the above simple strategy does not work for the
2006 environments with anti-aliased text: if the same text is
2007 drawn onto the same place multiple times, it gets thicker.
2008 If the overlap we are processing is for the erased cursor, we
2009 take the intersection with the rectangle of the cursor. */
2010 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2011 {
2012 XRectangle rc, r_save = r;
2013
2014 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2015 rc.y = s->w->phys_cursor.y;
2016 rc.width = s->w->phys_cursor_width;
2017 rc.height = s->w->phys_cursor_height;
2018
2019 x_intersect_rectangles (&r_save, &rc, &r);
2020 }
2021 }
2022 else
2023 {
2024 /* Don't use S->y for clipping because it doesn't take partially
2025 visible lines into account. For example, it can be negative for
2026 partially visible lines at the top of a window. */
2027 if (!s->row->full_width_p
2028 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2029 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2030 else
2031 r.y = max (0, s->row->y);
2032 }
2033
2034 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2035
2036 /* If drawing the cursor, don't let glyph draw outside its
2037 advertised boundaries. Cleartype does this under some circumstances. */
2038 if (s->hl == DRAW_CURSOR)
2039 {
2040 struct glyph *glyph = s->first_glyph;
2041 int height, max_y;
2042
2043 if (s->x > r.x)
2044 {
2045 r.width -= s->x - r.x;
2046 r.x = s->x;
2047 }
2048 r.width = min (r.width, glyph->pixel_width);
2049
2050 /* If r.y is below window bottom, ensure that we still see a cursor. */
2051 height = min (glyph->ascent + glyph->descent,
2052 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2053 max_y = window_text_bottom_y (s->w) - height;
2054 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2055 if (s->ybase - glyph->ascent > max_y)
2056 {
2057 r.y = max_y;
2058 r.height = height;
2059 }
2060 else
2061 {
2062 /* Don't draw cursor glyph taller than our actual glyph. */
2063 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2064 if (height < r.height)
2065 {
2066 max_y = r.y + r.height;
2067 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2068 r.height = min (max_y - r.y, height);
2069 }
2070 }
2071 }
2072
2073 if (s->row->clip)
2074 {
2075 XRectangle r_save = r;
2076
2077 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2078 r.width = 0;
2079 }
2080
2081 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2082 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2083 {
2084 #ifdef CONVERT_FROM_XRECT
2085 CONVERT_FROM_XRECT (r, *rects);
2086 #else
2087 *rects = r;
2088 #endif
2089 return 1;
2090 }
2091 else
2092 {
2093 /* If we are processing overlapping and allowed to return
2094 multiple clipping rectangles, we exclude the row of the glyph
2095 string from the clipping rectangle. This is to avoid drawing
2096 the same text on the environment with anti-aliasing. */
2097 #ifdef CONVERT_FROM_XRECT
2098 XRectangle rs[2];
2099 #else
2100 XRectangle *rs = rects;
2101 #endif
2102 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2103
2104 if (s->for_overlaps & OVERLAPS_PRED)
2105 {
2106 rs[i] = r;
2107 if (r.y + r.height > row_y)
2108 {
2109 if (r.y < row_y)
2110 rs[i].height = row_y - r.y;
2111 else
2112 rs[i].height = 0;
2113 }
2114 i++;
2115 }
2116 if (s->for_overlaps & OVERLAPS_SUCC)
2117 {
2118 rs[i] = r;
2119 if (r.y < row_y + s->row->visible_height)
2120 {
2121 if (r.y + r.height > row_y + s->row->visible_height)
2122 {
2123 rs[i].y = row_y + s->row->visible_height;
2124 rs[i].height = r.y + r.height - rs[i].y;
2125 }
2126 else
2127 rs[i].height = 0;
2128 }
2129 i++;
2130 }
2131
2132 n = i;
2133 #ifdef CONVERT_FROM_XRECT
2134 for (i = 0; i < n; i++)
2135 CONVERT_FROM_XRECT (rs[i], rects[i]);
2136 #endif
2137 return n;
2138 }
2139 }
2140
2141 /* EXPORT:
2142 Return in *NR the clipping rectangle for glyph string S. */
2143
2144 void
2145 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2146 {
2147 get_glyph_string_clip_rects (s, nr, 1);
2148 }
2149
2150
2151 /* EXPORT:
2152 Return the position and height of the phys cursor in window W.
2153 Set w->phys_cursor_width to width of phys cursor.
2154 */
2155
2156 void
2157 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2158 struct glyph *glyph, int *xp, int *yp, int *heightp)
2159 {
2160 struct frame *f = XFRAME (WINDOW_FRAME (w));
2161 int x, y, wd, h, h0, y0;
2162
2163 /* Compute the width of the rectangle to draw. If on a stretch
2164 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2165 rectangle as wide as the glyph, but use a canonical character
2166 width instead. */
2167 wd = glyph->pixel_width - 1;
2168 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2169 wd++; /* Why? */
2170 #endif
2171
2172 x = w->phys_cursor.x;
2173 if (x < 0)
2174 {
2175 wd += x;
2176 x = 0;
2177 }
2178
2179 if (glyph->type == STRETCH_GLYPH
2180 && !x_stretch_cursor_p)
2181 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2182 w->phys_cursor_width = wd;
2183
2184 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2185
2186 /* If y is below window bottom, ensure that we still see a cursor. */
2187 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2188
2189 h = max (h0, glyph->ascent + glyph->descent);
2190 h0 = min (h0, glyph->ascent + glyph->descent);
2191
2192 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2193 if (y < y0)
2194 {
2195 h = max (h - (y0 - y) + 1, h0);
2196 y = y0 - 1;
2197 }
2198 else
2199 {
2200 y0 = window_text_bottom_y (w) - h0;
2201 if (y > y0)
2202 {
2203 h += y - y0;
2204 y = y0;
2205 }
2206 }
2207
2208 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2209 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2210 *heightp = h;
2211 }
2212
2213 /*
2214 * Remember which glyph the mouse is over.
2215 */
2216
2217 void
2218 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2219 {
2220 Lisp_Object window;
2221 struct window *w;
2222 struct glyph_row *r, *gr, *end_row;
2223 enum window_part part;
2224 enum glyph_row_area area;
2225 int x, y, width, height;
2226
2227 /* Try to determine frame pixel position and size of the glyph under
2228 frame pixel coordinates X/Y on frame F. */
2229
2230 if (!f->glyphs_initialized_p
2231 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2232 NILP (window)))
2233 {
2234 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2235 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2236 goto virtual_glyph;
2237 }
2238
2239 w = XWINDOW (window);
2240 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2241 height = WINDOW_FRAME_LINE_HEIGHT (w);
2242
2243 x = window_relative_x_coord (w, part, gx);
2244 y = gy - WINDOW_TOP_EDGE_Y (w);
2245
2246 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2247 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2248
2249 if (w->pseudo_window_p)
2250 {
2251 area = TEXT_AREA;
2252 part = ON_MODE_LINE; /* Don't adjust margin. */
2253 goto text_glyph;
2254 }
2255
2256 switch (part)
2257 {
2258 case ON_LEFT_MARGIN:
2259 area = LEFT_MARGIN_AREA;
2260 goto text_glyph;
2261
2262 case ON_RIGHT_MARGIN:
2263 area = RIGHT_MARGIN_AREA;
2264 goto text_glyph;
2265
2266 case ON_HEADER_LINE:
2267 case ON_MODE_LINE:
2268 gr = (part == ON_HEADER_LINE
2269 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2270 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2271 gy = gr->y;
2272 area = TEXT_AREA;
2273 goto text_glyph_row_found;
2274
2275 case ON_TEXT:
2276 area = TEXT_AREA;
2277
2278 text_glyph:
2279 gr = 0; gy = 0;
2280 for (; r <= end_row && r->enabled_p; ++r)
2281 if (r->y + r->height > y)
2282 {
2283 gr = r; gy = r->y;
2284 break;
2285 }
2286
2287 text_glyph_row_found:
2288 if (gr && gy <= y)
2289 {
2290 struct glyph *g = gr->glyphs[area];
2291 struct glyph *end = g + gr->used[area];
2292
2293 height = gr->height;
2294 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2295 if (gx + g->pixel_width > x)
2296 break;
2297
2298 if (g < end)
2299 {
2300 if (g->type == IMAGE_GLYPH)
2301 {
2302 /* Don't remember when mouse is over image, as
2303 image may have hot-spots. */
2304 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2305 return;
2306 }
2307 width = g->pixel_width;
2308 }
2309 else
2310 {
2311 /* Use nominal char spacing at end of line. */
2312 x -= gx;
2313 gx += (x / width) * width;
2314 }
2315
2316 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2317 gx += window_box_left_offset (w, area);
2318 }
2319 else
2320 {
2321 /* Use nominal line height at end of window. */
2322 gx = (x / width) * width;
2323 y -= gy;
2324 gy += (y / height) * height;
2325 }
2326 break;
2327
2328 case ON_LEFT_FRINGE:
2329 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2330 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2331 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2332 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2333 goto row_glyph;
2334
2335 case ON_RIGHT_FRINGE:
2336 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2337 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2338 : window_box_right_offset (w, TEXT_AREA));
2339 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2340 goto row_glyph;
2341
2342 case ON_SCROLL_BAR:
2343 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2344 ? 0
2345 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2346 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2347 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2348 : 0)));
2349 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2350
2351 row_glyph:
2352 gr = 0, gy = 0;
2353 for (; r <= end_row && r->enabled_p; ++r)
2354 if (r->y + r->height > y)
2355 {
2356 gr = r; gy = r->y;
2357 break;
2358 }
2359
2360 if (gr && gy <= y)
2361 height = gr->height;
2362 else
2363 {
2364 /* Use nominal line height at end of window. */
2365 y -= gy;
2366 gy += (y / height) * height;
2367 }
2368 break;
2369
2370 default:
2371 ;
2372 virtual_glyph:
2373 /* If there is no glyph under the mouse, then we divide the screen
2374 into a grid of the smallest glyph in the frame, and use that
2375 as our "glyph". */
2376
2377 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2378 round down even for negative values. */
2379 if (gx < 0)
2380 gx -= width - 1;
2381 if (gy < 0)
2382 gy -= height - 1;
2383
2384 gx = (gx / width) * width;
2385 gy = (gy / height) * height;
2386
2387 goto store_rect;
2388 }
2389
2390 gx += WINDOW_LEFT_EDGE_X (w);
2391 gy += WINDOW_TOP_EDGE_Y (w);
2392
2393 store_rect:
2394 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2395
2396 /* Visible feedback for debugging. */
2397 #if 0
2398 #if HAVE_X_WINDOWS
2399 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2400 f->output_data.x->normal_gc,
2401 gx, gy, width, height);
2402 #endif
2403 #endif
2404 }
2405
2406
2407 #endif /* HAVE_WINDOW_SYSTEM */
2408
2409 \f
2410 /***********************************************************************
2411 Lisp form evaluation
2412 ***********************************************************************/
2413
2414 /* Error handler for safe_eval and safe_call. */
2415
2416 static Lisp_Object
2417 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2418 {
2419 add_to_log ("Error during redisplay: %S signaled %S",
2420 Flist (nargs, args), arg);
2421 return Qnil;
2422 }
2423
2424 /* Call function FUNC with the rest of NARGS - 1 arguments
2425 following. Return the result, or nil if something went
2426 wrong. Prevent redisplay during the evaluation. */
2427
2428 Lisp_Object
2429 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2430 {
2431 Lisp_Object val;
2432
2433 if (inhibit_eval_during_redisplay)
2434 val = Qnil;
2435 else
2436 {
2437 va_list ap;
2438 ptrdiff_t i;
2439 ptrdiff_t count = SPECPDL_INDEX ();
2440 struct gcpro gcpro1;
2441 Lisp_Object *args = alloca (nargs * word_size);
2442
2443 args[0] = func;
2444 va_start (ap, func);
2445 for (i = 1; i < nargs; i++)
2446 args[i] = va_arg (ap, Lisp_Object);
2447 va_end (ap);
2448
2449 GCPRO1 (args[0]);
2450 gcpro1.nvars = nargs;
2451 specbind (Qinhibit_redisplay, Qt);
2452 /* Use Qt to ensure debugger does not run,
2453 so there is no possibility of wanting to redisplay. */
2454 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2455 safe_eval_handler);
2456 UNGCPRO;
2457 val = unbind_to (count, val);
2458 }
2459
2460 return val;
2461 }
2462
2463
2464 /* Call function FN with one argument ARG.
2465 Return the result, or nil if something went wrong. */
2466
2467 Lisp_Object
2468 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2469 {
2470 return safe_call (2, fn, arg);
2471 }
2472
2473 static Lisp_Object Qeval;
2474
2475 Lisp_Object
2476 safe_eval (Lisp_Object sexpr)
2477 {
2478 return safe_call1 (Qeval, sexpr);
2479 }
2480
2481 /* Call function FN with two arguments ARG1 and ARG2.
2482 Return the result, or nil if something went wrong. */
2483
2484 Lisp_Object
2485 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2486 {
2487 return safe_call (3, fn, arg1, arg2);
2488 }
2489
2490
2491 \f
2492 /***********************************************************************
2493 Debugging
2494 ***********************************************************************/
2495
2496 #if 0
2497
2498 /* Define CHECK_IT to perform sanity checks on iterators.
2499 This is for debugging. It is too slow to do unconditionally. */
2500
2501 static void
2502 check_it (struct it *it)
2503 {
2504 if (it->method == GET_FROM_STRING)
2505 {
2506 eassert (STRINGP (it->string));
2507 eassert (IT_STRING_CHARPOS (*it) >= 0);
2508 }
2509 else
2510 {
2511 eassert (IT_STRING_CHARPOS (*it) < 0);
2512 if (it->method == GET_FROM_BUFFER)
2513 {
2514 /* Check that character and byte positions agree. */
2515 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2516 }
2517 }
2518
2519 if (it->dpvec)
2520 eassert (it->current.dpvec_index >= 0);
2521 else
2522 eassert (it->current.dpvec_index < 0);
2523 }
2524
2525 #define CHECK_IT(IT) check_it ((IT))
2526
2527 #else /* not 0 */
2528
2529 #define CHECK_IT(IT) (void) 0
2530
2531 #endif /* not 0 */
2532
2533
2534 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2535
2536 /* Check that the window end of window W is what we expect it
2537 to be---the last row in the current matrix displaying text. */
2538
2539 static void
2540 check_window_end (struct window *w)
2541 {
2542 if (!MINI_WINDOW_P (w)
2543 && !NILP (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. BYTEPOS < 0 means compute it from 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 (0 <= markpos
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 IT_CHARPOS (*it) = charpos;
2867
2868 /* We will rely on `reseat' to set this up properly, via
2869 handle_face_prop. */
2870 it->face_id = it->base_face_id;
2871
2872 /* Compute byte position if not specified. */
2873 if (bytepos < charpos)
2874 IT_BYTEPOS (*it) = CHAR_TO_BYTE (charpos);
2875 else
2876 IT_BYTEPOS (*it) = bytepos;
2877
2878 it->start = it->current;
2879 /* Do we need to reorder bidirectional text? Not if this is a
2880 unibyte buffer: by definition, none of the single-byte
2881 characters are strong R2L, so no reordering is needed. And
2882 bidi.c doesn't support unibyte buffers anyway. Also, don't
2883 reorder while we are loading loadup.el, since the tables of
2884 character properties needed for reordering are not yet
2885 available. */
2886 it->bidi_p =
2887 NILP (Vpurify_flag)
2888 && !NILP (BVAR (current_buffer, bidi_display_reordering))
2889 && it->multibyte_p;
2890
2891 /* If we are to reorder bidirectional text, init the bidi
2892 iterator. */
2893 if (it->bidi_p)
2894 {
2895 /* Note the paragraph direction that this buffer wants to
2896 use. */
2897 if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2898 Qleft_to_right))
2899 it->paragraph_embedding = L2R;
2900 else if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
2901 Qright_to_left))
2902 it->paragraph_embedding = R2L;
2903 else
2904 it->paragraph_embedding = NEUTRAL_DIR;
2905 bidi_unshelve_cache (NULL, 0);
2906 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
2907 &it->bidi_it);
2908 }
2909
2910 /* Compute faces etc. */
2911 reseat (it, it->current.pos, 1);
2912 }
2913
2914 CHECK_IT (it);
2915 }
2916
2917
2918 /* Initialize IT for the display of window W with window start POS. */
2919
2920 void
2921 start_display (struct it *it, struct window *w, struct text_pos pos)
2922 {
2923 struct glyph_row *row;
2924 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
2925
2926 row = w->desired_matrix->rows + first_vpos;
2927 init_iterator (it, w, CHARPOS (pos), BYTEPOS (pos), row, DEFAULT_FACE_ID);
2928 it->first_vpos = first_vpos;
2929
2930 /* Don't reseat to previous visible line start if current start
2931 position is in a string or image. */
2932 if (it->method == GET_FROM_BUFFER && it->line_wrap != TRUNCATE)
2933 {
2934 int start_at_line_beg_p;
2935 int first_y = it->current_y;
2936
2937 /* If window start is not at a line start, skip forward to POS to
2938 get the correct continuation lines width. */
2939 start_at_line_beg_p = (CHARPOS (pos) == BEGV
2940 || FETCH_BYTE (BYTEPOS (pos) - 1) == '\n');
2941 if (!start_at_line_beg_p)
2942 {
2943 int new_x;
2944
2945 reseat_at_previous_visible_line_start (it);
2946 move_it_to (it, CHARPOS (pos), -1, -1, -1, MOVE_TO_POS);
2947
2948 new_x = it->current_x + it->pixel_width;
2949
2950 /* If lines are continued, this line may end in the middle
2951 of a multi-glyph character (e.g. a control character
2952 displayed as \003, or in the middle of an overlay
2953 string). In this case move_it_to above will not have
2954 taken us to the start of the continuation line but to the
2955 end of the continued line. */
2956 if (it->current_x > 0
2957 && it->line_wrap != TRUNCATE /* Lines are continued. */
2958 && (/* And glyph doesn't fit on the line. */
2959 new_x > it->last_visible_x
2960 /* Or it fits exactly and we're on a window
2961 system frame. */
2962 || (new_x == it->last_visible_x
2963 && FRAME_WINDOW_P (it->f)
2964 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
2965 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
2966 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
2967 {
2968 if ((it->current.dpvec_index >= 0
2969 || it->current.overlay_string_index >= 0)
2970 /* If we are on a newline from a display vector or
2971 overlay string, then we are already at the end of
2972 a screen line; no need to go to the next line in
2973 that case, as this line is not really continued.
2974 (If we do go to the next line, C-e will not DTRT.) */
2975 && it->c != '\n')
2976 {
2977 set_iterator_to_next (it, 1);
2978 move_it_in_display_line_to (it, -1, -1, 0);
2979 }
2980
2981 it->continuation_lines_width += it->current_x;
2982 }
2983 /* If the character at POS is displayed via a display
2984 vector, move_it_to above stops at the final glyph of
2985 IT->dpvec. To make the caller redisplay that character
2986 again (a.k.a. start at POS), we need to reset the
2987 dpvec_index to the beginning of IT->dpvec. */
2988 else if (it->current.dpvec_index >= 0)
2989 it->current.dpvec_index = 0;
2990
2991 /* We're starting a new display line, not affected by the
2992 height of the continued line, so clear the appropriate
2993 fields in the iterator structure. */
2994 it->max_ascent = it->max_descent = 0;
2995 it->max_phys_ascent = it->max_phys_descent = 0;
2996
2997 it->current_y = first_y;
2998 it->vpos = 0;
2999 it->current_x = it->hpos = 0;
3000 }
3001 }
3002 }
3003
3004
3005 /* Return 1 if POS is a position in ellipses displayed for invisible
3006 text. W is the window we display, for text property lookup. */
3007
3008 static int
3009 in_ellipses_for_invisible_text_p (struct display_pos *pos, struct window *w)
3010 {
3011 Lisp_Object prop, window;
3012 int ellipses_p = 0;
3013 ptrdiff_t charpos = CHARPOS (pos->pos);
3014
3015 /* If POS specifies a position in a display vector, this might
3016 be for an ellipsis displayed for invisible text. We won't
3017 get the iterator set up for delivering that ellipsis unless
3018 we make sure that it gets aware of the invisible text. */
3019 if (pos->dpvec_index >= 0
3020 && pos->overlay_string_index < 0
3021 && CHARPOS (pos->string_pos) < 0
3022 && charpos > BEGV
3023 && (XSETWINDOW (window, w),
3024 prop = Fget_char_property (make_number (charpos),
3025 Qinvisible, window),
3026 !TEXT_PROP_MEANS_INVISIBLE (prop)))
3027 {
3028 prop = Fget_char_property (make_number (charpos - 1), Qinvisible,
3029 window);
3030 ellipses_p = 2 == TEXT_PROP_MEANS_INVISIBLE (prop);
3031 }
3032
3033 return ellipses_p;
3034 }
3035
3036
3037 /* Initialize IT for stepping through current_buffer in window W,
3038 starting at position POS that includes overlay string and display
3039 vector/ control character translation position information. Value
3040 is zero if there are overlay strings with newlines at POS. */
3041
3042 static int
3043 init_from_display_pos (struct it *it, struct window *w, struct display_pos *pos)
3044 {
3045 ptrdiff_t charpos = CHARPOS (pos->pos), bytepos = BYTEPOS (pos->pos);
3046 int i, overlay_strings_with_newlines = 0;
3047
3048 /* If POS specifies a position in a display vector, this might
3049 be for an ellipsis displayed for invisible text. We won't
3050 get the iterator set up for delivering that ellipsis unless
3051 we make sure that it gets aware of the invisible text. */
3052 if (in_ellipses_for_invisible_text_p (pos, w))
3053 {
3054 --charpos;
3055 bytepos = 0;
3056 }
3057
3058 /* Keep in mind: the call to reseat in init_iterator skips invisible
3059 text, so we might end up at a position different from POS. This
3060 is only a problem when POS is a row start after a newline and an
3061 overlay starts there with an after-string, and the overlay has an
3062 invisible property. Since we don't skip invisible text in
3063 display_line and elsewhere immediately after consuming the
3064 newline before the row start, such a POS will not be in a string,
3065 but the call to init_iterator below will move us to the
3066 after-string. */
3067 init_iterator (it, w, charpos, bytepos, NULL, DEFAULT_FACE_ID);
3068
3069 /* This only scans the current chunk -- it should scan all chunks.
3070 However, OVERLAY_STRING_CHUNK_SIZE has been increased from 3 in 21.1
3071 to 16 in 22.1 to make this a lesser problem. */
3072 for (i = 0; i < it->n_overlay_strings && i < OVERLAY_STRING_CHUNK_SIZE; ++i)
3073 {
3074 const char *s = SSDATA (it->overlay_strings[i]);
3075 const char *e = s + SBYTES (it->overlay_strings[i]);
3076
3077 while (s < e && *s != '\n')
3078 ++s;
3079
3080 if (s < e)
3081 {
3082 overlay_strings_with_newlines = 1;
3083 break;
3084 }
3085 }
3086
3087 /* If position is within an overlay string, set up IT to the right
3088 overlay string. */
3089 if (pos->overlay_string_index >= 0)
3090 {
3091 int relative_index;
3092
3093 /* If the first overlay string happens to have a `display'
3094 property for an image, the iterator will be set up for that
3095 image, and we have to undo that setup first before we can
3096 correct the overlay string index. */
3097 if (it->method == GET_FROM_IMAGE)
3098 pop_it (it);
3099
3100 /* We already have the first chunk of overlay strings in
3101 IT->overlay_strings. Load more until the one for
3102 pos->overlay_string_index is in IT->overlay_strings. */
3103 if (pos->overlay_string_index >= OVERLAY_STRING_CHUNK_SIZE)
3104 {
3105 ptrdiff_t n = pos->overlay_string_index / OVERLAY_STRING_CHUNK_SIZE;
3106 it->current.overlay_string_index = 0;
3107 while (n--)
3108 {
3109 load_overlay_strings (it, 0);
3110 it->current.overlay_string_index += OVERLAY_STRING_CHUNK_SIZE;
3111 }
3112 }
3113
3114 it->current.overlay_string_index = pos->overlay_string_index;
3115 relative_index = (it->current.overlay_string_index
3116 % OVERLAY_STRING_CHUNK_SIZE);
3117 it->string = it->overlay_strings[relative_index];
3118 eassert (STRINGP (it->string));
3119 it->current.string_pos = pos->string_pos;
3120 it->method = GET_FROM_STRING;
3121 it->end_charpos = SCHARS (it->string);
3122 /* Set up the bidi iterator for this overlay string. */
3123 if (it->bidi_p)
3124 {
3125 it->bidi_it.string.lstring = it->string;
3126 it->bidi_it.string.s = NULL;
3127 it->bidi_it.string.schars = SCHARS (it->string);
3128 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
3129 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
3130 it->bidi_it.string.unibyte = !it->multibyte_p;
3131 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3132 FRAME_WINDOW_P (it->f), &it->bidi_it);
3133
3134 /* Synchronize the state of the bidi iterator with
3135 pos->string_pos. For any string position other than
3136 zero, this will be done automagically when we resume
3137 iteration over the string and get_visually_first_element
3138 is called. But if string_pos is zero, and the string is
3139 to be reordered for display, we need to resync manually,
3140 since it could be that the iteration state recorded in
3141 pos ended at string_pos of 0 moving backwards in string. */
3142 if (CHARPOS (pos->string_pos) == 0)
3143 {
3144 get_visually_first_element (it);
3145 if (IT_STRING_CHARPOS (*it) != 0)
3146 do {
3147 /* Paranoia. */
3148 eassert (it->bidi_it.charpos < it->bidi_it.string.schars);
3149 bidi_move_to_visually_next (&it->bidi_it);
3150 } while (it->bidi_it.charpos != 0);
3151 }
3152 eassert (IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
3153 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos);
3154 }
3155 }
3156
3157 if (CHARPOS (pos->string_pos) >= 0)
3158 {
3159 /* Recorded position is not in an overlay string, but in another
3160 string. This can only be a string from a `display' property.
3161 IT should already be filled with that string. */
3162 it->current.string_pos = pos->string_pos;
3163 eassert (STRINGP (it->string));
3164 if (it->bidi_p)
3165 bidi_init_it (IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it),
3166 FRAME_WINDOW_P (it->f), &it->bidi_it);
3167 }
3168
3169 /* Restore position in display vector translations, control
3170 character translations or ellipses. */
3171 if (pos->dpvec_index >= 0)
3172 {
3173 if (it->dpvec == NULL)
3174 get_next_display_element (it);
3175 eassert (it->dpvec && it->current.dpvec_index == 0);
3176 it->current.dpvec_index = pos->dpvec_index;
3177 }
3178
3179 CHECK_IT (it);
3180 return !overlay_strings_with_newlines;
3181 }
3182
3183
3184 /* Initialize IT for stepping through current_buffer in window W
3185 starting at ROW->start. */
3186
3187 static void
3188 init_to_row_start (struct it *it, struct window *w, struct glyph_row *row)
3189 {
3190 init_from_display_pos (it, w, &row->start);
3191 it->start = row->start;
3192 it->continuation_lines_width = row->continuation_lines_width;
3193 CHECK_IT (it);
3194 }
3195
3196
3197 /* Initialize IT for stepping through current_buffer in window W
3198 starting in the line following ROW, i.e. starting at ROW->end.
3199 Value is zero if there are overlay strings with newlines at ROW's
3200 end position. */
3201
3202 static int
3203 init_to_row_end (struct it *it, struct window *w, struct glyph_row *row)
3204 {
3205 int success = 0;
3206
3207 if (init_from_display_pos (it, w, &row->end))
3208 {
3209 if (row->continued_p)
3210 it->continuation_lines_width
3211 = row->continuation_lines_width + row->pixel_width;
3212 CHECK_IT (it);
3213 success = 1;
3214 }
3215
3216 return success;
3217 }
3218
3219
3220
3221 \f
3222 /***********************************************************************
3223 Text properties
3224 ***********************************************************************/
3225
3226 /* Called when IT reaches IT->stop_charpos. Handle text property and
3227 overlay changes. Set IT->stop_charpos to the next position where
3228 to stop. */
3229
3230 static void
3231 handle_stop (struct it *it)
3232 {
3233 enum prop_handled handled;
3234 int handle_overlay_change_p;
3235 struct props *p;
3236
3237 it->dpvec = NULL;
3238 it->current.dpvec_index = -1;
3239 handle_overlay_change_p = !it->ignore_overlay_strings_at_pos_p;
3240 it->ignore_overlay_strings_at_pos_p = 0;
3241 it->ellipsis_p = 0;
3242
3243 /* Use face of preceding text for ellipsis (if invisible) */
3244 if (it->selective_display_ellipsis_p)
3245 it->saved_face_id = it->face_id;
3246
3247 do
3248 {
3249 handled = HANDLED_NORMALLY;
3250
3251 /* Call text property handlers. */
3252 for (p = it_props; p->handler; ++p)
3253 {
3254 handled = p->handler (it);
3255
3256 if (handled == HANDLED_RECOMPUTE_PROPS)
3257 break;
3258 else if (handled == HANDLED_RETURN)
3259 {
3260 /* We still want to show before and after strings from
3261 overlays even if the actual buffer text is replaced. */
3262 if (!handle_overlay_change_p
3263 || it->sp > 1
3264 /* Don't call get_overlay_strings_1 if we already
3265 have overlay strings loaded, because doing so
3266 will load them again and push the iterator state
3267 onto the stack one more time, which is not
3268 expected by the rest of the code that processes
3269 overlay strings. */
3270 || (it->current.overlay_string_index < 0
3271 ? !get_overlay_strings_1 (it, 0, 0)
3272 : 0))
3273 {
3274 if (it->ellipsis_p)
3275 setup_for_ellipsis (it, 0);
3276 /* When handling a display spec, we might load an
3277 empty string. In that case, discard it here. We
3278 used to discard it in handle_single_display_spec,
3279 but that causes get_overlay_strings_1, above, to
3280 ignore overlay strings that we must check. */
3281 if (STRINGP (it->string) && !SCHARS (it->string))
3282 pop_it (it);
3283 return;
3284 }
3285 else if (STRINGP (it->string) && !SCHARS (it->string))
3286 pop_it (it);
3287 else
3288 {
3289 it->ignore_overlay_strings_at_pos_p = 1;
3290 it->string_from_display_prop_p = 0;
3291 it->from_disp_prop_p = 0;
3292 handle_overlay_change_p = 0;
3293 }
3294 handled = HANDLED_RECOMPUTE_PROPS;
3295 break;
3296 }
3297 else if (handled == HANDLED_OVERLAY_STRING_CONSUMED)
3298 handle_overlay_change_p = 0;
3299 }
3300
3301 if (handled != HANDLED_RECOMPUTE_PROPS)
3302 {
3303 /* Don't check for overlay strings below when set to deliver
3304 characters from a display vector. */
3305 if (it->method == GET_FROM_DISPLAY_VECTOR)
3306 handle_overlay_change_p = 0;
3307
3308 /* Handle overlay changes.
3309 This sets HANDLED to HANDLED_RECOMPUTE_PROPS
3310 if it finds overlays. */
3311 if (handle_overlay_change_p)
3312 handled = handle_overlay_change (it);
3313 }
3314
3315 if (it->ellipsis_p)
3316 {
3317 setup_for_ellipsis (it, 0);
3318 break;
3319 }
3320 }
3321 while (handled == HANDLED_RECOMPUTE_PROPS);
3322
3323 /* Determine where to stop next. */
3324 if (handled == HANDLED_NORMALLY)
3325 compute_stop_pos (it);
3326 }
3327
3328
3329 /* Compute IT->stop_charpos from text property and overlay change
3330 information for IT's current position. */
3331
3332 static void
3333 compute_stop_pos (struct it *it)
3334 {
3335 register INTERVAL iv, next_iv;
3336 Lisp_Object object, limit, position;
3337 ptrdiff_t charpos, bytepos;
3338
3339 if (STRINGP (it->string))
3340 {
3341 /* Strings are usually short, so don't limit the search for
3342 properties. */
3343 it->stop_charpos = it->end_charpos;
3344 object = it->string;
3345 limit = Qnil;
3346 charpos = IT_STRING_CHARPOS (*it);
3347 bytepos = IT_STRING_BYTEPOS (*it);
3348 }
3349 else
3350 {
3351 ptrdiff_t pos;
3352
3353 /* If end_charpos is out of range for some reason, such as a
3354 misbehaving display function, rationalize it (Bug#5984). */
3355 if (it->end_charpos > ZV)
3356 it->end_charpos = ZV;
3357 it->stop_charpos = it->end_charpos;
3358
3359 /* If next overlay change is in front of the current stop pos
3360 (which is IT->end_charpos), stop there. Note: value of
3361 next_overlay_change is point-max if no overlay change
3362 follows. */
3363 charpos = IT_CHARPOS (*it);
3364 bytepos = IT_BYTEPOS (*it);
3365 pos = next_overlay_change (charpos);
3366 if (pos < it->stop_charpos)
3367 it->stop_charpos = pos;
3368
3369 /* If showing the region, we have to stop at the region
3370 start or end because the face might change there. */
3371 if (it->region_beg_charpos > 0)
3372 {
3373 if (IT_CHARPOS (*it) < it->region_beg_charpos)
3374 it->stop_charpos = min (it->stop_charpos, it->region_beg_charpos);
3375 else if (IT_CHARPOS (*it) < it->region_end_charpos)
3376 it->stop_charpos = min (it->stop_charpos, it->region_end_charpos);
3377 }
3378
3379 /* Set up variables for computing the stop position from text
3380 property changes. */
3381 XSETBUFFER (object, current_buffer);
3382 limit = make_number (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT);
3383 }
3384
3385 /* Get the interval containing IT's position. Value is a null
3386 interval if there isn't such an interval. */
3387 position = make_number (charpos);
3388 iv = validate_interval_range (object, &position, &position, 0);
3389 if (iv)
3390 {
3391 Lisp_Object values_here[LAST_PROP_IDX];
3392 struct props *p;
3393
3394 /* Get properties here. */
3395 for (p = it_props; p->handler; ++p)
3396 values_here[p->idx] = textget (iv->plist, *p->name);
3397
3398 /* Look for an interval following iv that has different
3399 properties. */
3400 for (next_iv = next_interval (iv);
3401 (next_iv
3402 && (NILP (limit)
3403 || XFASTINT (limit) > next_iv->position));
3404 next_iv = next_interval (next_iv))
3405 {
3406 for (p = it_props; p->handler; ++p)
3407 {
3408 Lisp_Object new_value;
3409
3410 new_value = textget (next_iv->plist, *p->name);
3411 if (!EQ (values_here[p->idx], new_value))
3412 break;
3413 }
3414
3415 if (p->handler)
3416 break;
3417 }
3418
3419 if (next_iv)
3420 {
3421 if (INTEGERP (limit)
3422 && next_iv->position >= XFASTINT (limit))
3423 /* No text property change up to limit. */
3424 it->stop_charpos = min (XFASTINT (limit), it->stop_charpos);
3425 else
3426 /* Text properties change in next_iv. */
3427 it->stop_charpos = min (it->stop_charpos, next_iv->position);
3428 }
3429 }
3430
3431 if (it->cmp_it.id < 0)
3432 {
3433 ptrdiff_t stoppos = it->end_charpos;
3434
3435 if (it->bidi_p && it->bidi_it.scan_dir < 0)
3436 stoppos = -1;
3437 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos,
3438 stoppos, it->string);
3439 }
3440
3441 eassert (STRINGP (it->string)
3442 || (it->stop_charpos >= BEGV
3443 && it->stop_charpos >= IT_CHARPOS (*it)));
3444 }
3445
3446
3447 /* Return the position of the next overlay change after POS in
3448 current_buffer. Value is point-max if no overlay change
3449 follows. This is like `next-overlay-change' but doesn't use
3450 xmalloc. */
3451
3452 static ptrdiff_t
3453 next_overlay_change (ptrdiff_t pos)
3454 {
3455 ptrdiff_t i, noverlays;
3456 ptrdiff_t endpos;
3457 Lisp_Object *overlays;
3458
3459 /* Get all overlays at the given position. */
3460 GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
3461
3462 /* If any of these overlays ends before endpos,
3463 use its ending point instead. */
3464 for (i = 0; i < noverlays; ++i)
3465 {
3466 Lisp_Object oend;
3467 ptrdiff_t oendpos;
3468
3469 oend = OVERLAY_END (overlays[i]);
3470 oendpos = OVERLAY_POSITION (oend);
3471 endpos = min (endpos, oendpos);
3472 }
3473
3474 return endpos;
3475 }
3476
3477 /* How many characters forward to search for a display property or
3478 display string. Searching too far forward makes the bidi display
3479 sluggish, especially in small windows. */
3480 #define MAX_DISP_SCAN 250
3481
3482 /* Return the character position of a display string at or after
3483 position specified by POSITION. If no display string exists at or
3484 after POSITION, return ZV. A display string is either an overlay
3485 with `display' property whose value is a string, or a `display'
3486 text property whose value is a string. STRING is data about the
3487 string to iterate; if STRING->lstring is nil, we are iterating a
3488 buffer. FRAME_WINDOW_P is non-zero when we are displaying a window
3489 on a GUI frame. DISP_PROP is set to zero if we searched
3490 MAX_DISP_SCAN characters forward without finding any display
3491 strings, non-zero otherwise. It is set to 2 if the display string
3492 uses any kind of `(space ...)' spec that will produce a stretch of
3493 white space in the text area. */
3494 ptrdiff_t
3495 compute_display_string_pos (struct text_pos *position,
3496 struct bidi_string_data *string,
3497 int frame_window_p, int *disp_prop)
3498 {
3499 /* OBJECT = nil means current buffer. */
3500 Lisp_Object object =
3501 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3502 Lisp_Object pos, spec, limpos;
3503 int string_p = (string && (STRINGP (string->lstring) || string->s));
3504 ptrdiff_t eob = string_p ? string->schars : ZV;
3505 ptrdiff_t begb = string_p ? 0 : BEGV;
3506 ptrdiff_t bufpos, charpos = CHARPOS (*position);
3507 ptrdiff_t lim =
3508 (charpos < eob - MAX_DISP_SCAN) ? charpos + MAX_DISP_SCAN : eob;
3509 struct text_pos tpos;
3510 int rv = 0;
3511
3512 *disp_prop = 1;
3513
3514 if (charpos >= eob
3515 /* We don't support display properties whose values are strings
3516 that have display string properties. */
3517 || string->from_disp_str
3518 /* C strings cannot have display properties. */
3519 || (string->s && !STRINGP (object)))
3520 {
3521 *disp_prop = 0;
3522 return eob;
3523 }
3524
3525 /* If the character at CHARPOS is where the display string begins,
3526 return CHARPOS. */
3527 pos = make_number (charpos);
3528 if (STRINGP (object))
3529 bufpos = string->bufpos;
3530 else
3531 bufpos = charpos;
3532 tpos = *position;
3533 if (!NILP (spec = Fget_char_property (pos, Qdisplay, object))
3534 && (charpos <= begb
3535 || !EQ (Fget_char_property (make_number (charpos - 1), Qdisplay,
3536 object),
3537 spec))
3538 && (rv = handle_display_spec (NULL, spec, object, Qnil, &tpos, bufpos,
3539 frame_window_p)))
3540 {
3541 if (rv == 2)
3542 *disp_prop = 2;
3543 return charpos;
3544 }
3545
3546 /* Look forward for the first character with a `display' property
3547 that will replace the underlying text when displayed. */
3548 limpos = make_number (lim);
3549 do {
3550 pos = Fnext_single_char_property_change (pos, Qdisplay, object, limpos);
3551 CHARPOS (tpos) = XFASTINT (pos);
3552 if (CHARPOS (tpos) >= lim)
3553 {
3554 *disp_prop = 0;
3555 break;
3556 }
3557 if (STRINGP (object))
3558 BYTEPOS (tpos) = string_char_to_byte (object, CHARPOS (tpos));
3559 else
3560 BYTEPOS (tpos) = CHAR_TO_BYTE (CHARPOS (tpos));
3561 spec = Fget_char_property (pos, Qdisplay, object);
3562 if (!STRINGP (object))
3563 bufpos = CHARPOS (tpos);
3564 } while (NILP (spec)
3565 || !(rv = handle_display_spec (NULL, spec, object, Qnil, &tpos,
3566 bufpos, frame_window_p)));
3567 if (rv == 2)
3568 *disp_prop = 2;
3569
3570 return CHARPOS (tpos);
3571 }
3572
3573 /* Return the character position of the end of the display string that
3574 started at CHARPOS. If there's no display string at CHARPOS,
3575 return -1. A display string is either an overlay with `display'
3576 property whose value is a string or a `display' text property whose
3577 value is a string. */
3578 ptrdiff_t
3579 compute_display_string_end (ptrdiff_t charpos, struct bidi_string_data *string)
3580 {
3581 /* OBJECT = nil means current buffer. */
3582 Lisp_Object object =
3583 (string && STRINGP (string->lstring)) ? string->lstring : Qnil;
3584 Lisp_Object pos = make_number (charpos);
3585 ptrdiff_t eob =
3586 (STRINGP (object) || (string && string->s)) ? string->schars : ZV;
3587
3588 if (charpos >= eob || (string->s && !STRINGP (object)))
3589 return eob;
3590
3591 /* It could happen that the display property or overlay was removed
3592 since we found it in compute_display_string_pos above. One way
3593 this can happen is if JIT font-lock was called (through
3594 handle_fontified_prop), and jit-lock-functions remove text
3595 properties or overlays from the portion of buffer that includes
3596 CHARPOS. Muse mode is known to do that, for example. In this
3597 case, we return -1 to the caller, to signal that no display
3598 string is actually present at CHARPOS. See bidi_fetch_char for
3599 how this is handled.
3600
3601 An alternative would be to never look for display properties past
3602 it->stop_charpos. But neither compute_display_string_pos nor
3603 bidi_fetch_char that calls it know or care where the next
3604 stop_charpos is. */
3605 if (NILP (Fget_char_property (pos, Qdisplay, object)))
3606 return -1;
3607
3608 /* Look forward for the first character where the `display' property
3609 changes. */
3610 pos = Fnext_single_char_property_change (pos, Qdisplay, object, Qnil);
3611
3612 return XFASTINT (pos);
3613 }
3614
3615
3616 \f
3617 /***********************************************************************
3618 Fontification
3619 ***********************************************************************/
3620
3621 /* Handle changes in the `fontified' property of the current buffer by
3622 calling hook functions from Qfontification_functions to fontify
3623 regions of text. */
3624
3625 static enum prop_handled
3626 handle_fontified_prop (struct it *it)
3627 {
3628 Lisp_Object prop, pos;
3629 enum prop_handled handled = HANDLED_NORMALLY;
3630
3631 if (!NILP (Vmemory_full))
3632 return handled;
3633
3634 /* Get the value of the `fontified' property at IT's current buffer
3635 position. (The `fontified' property doesn't have a special
3636 meaning in strings.) If the value is nil, call functions from
3637 Qfontification_functions. */
3638 if (!STRINGP (it->string)
3639 && it->s == NULL
3640 && !NILP (Vfontification_functions)
3641 && !NILP (Vrun_hooks)
3642 && (pos = make_number (IT_CHARPOS (*it)),
3643 prop = Fget_char_property (pos, Qfontified, Qnil),
3644 /* Ignore the special cased nil value always present at EOB since
3645 no amount of fontifying will be able to change it. */
3646 NILP (prop) && IT_CHARPOS (*it) < Z))
3647 {
3648 ptrdiff_t count = SPECPDL_INDEX ();
3649 Lisp_Object val;
3650 struct buffer *obuf = current_buffer;
3651 int begv = BEGV, zv = ZV;
3652 int old_clip_changed = current_buffer->clip_changed;
3653
3654 val = Vfontification_functions;
3655 specbind (Qfontification_functions, Qnil);
3656
3657 eassert (it->end_charpos == ZV);
3658
3659 if (!CONSP (val) || EQ (XCAR (val), Qlambda))
3660 safe_call1 (val, pos);
3661 else
3662 {
3663 Lisp_Object fns, fn;
3664 struct gcpro gcpro1, gcpro2;
3665
3666 fns = Qnil;
3667 GCPRO2 (val, fns);
3668
3669 for (; CONSP (val); val = XCDR (val))
3670 {
3671 fn = XCAR (val);
3672
3673 if (EQ (fn, Qt))
3674 {
3675 /* A value of t indicates this hook has a local
3676 binding; it means to run the global binding too.
3677 In a global value, t should not occur. If it
3678 does, we must ignore it to avoid an endless
3679 loop. */
3680 for (fns = Fdefault_value (Qfontification_functions);
3681 CONSP (fns);
3682 fns = XCDR (fns))
3683 {
3684 fn = XCAR (fns);
3685 if (!EQ (fn, Qt))
3686 safe_call1 (fn, pos);
3687 }
3688 }
3689 else
3690 safe_call1 (fn, pos);
3691 }
3692
3693 UNGCPRO;
3694 }
3695
3696 unbind_to (count, Qnil);
3697
3698 /* Fontification functions routinely call `save-restriction'.
3699 Normally, this tags clip_changed, which can confuse redisplay
3700 (see discussion in Bug#6671). Since we don't perform any
3701 special handling of fontification changes in the case where
3702 `save-restriction' isn't called, there's no point doing so in
3703 this case either. So, if the buffer's restrictions are
3704 actually left unchanged, reset clip_changed. */
3705 if (obuf == current_buffer)
3706 {
3707 if (begv == BEGV && zv == ZV)
3708 current_buffer->clip_changed = old_clip_changed;
3709 }
3710 /* There isn't much we can reasonably do to protect against
3711 misbehaving fontification, but here's a fig leaf. */
3712 else if (BUFFER_LIVE_P (obuf))
3713 set_buffer_internal_1 (obuf);
3714
3715 /* The fontification code may have added/removed text.
3716 It could do even a lot worse, but let's at least protect against
3717 the most obvious case where only the text past `pos' gets changed',
3718 as is/was done in grep.el where some escapes sequences are turned
3719 into face properties (bug#7876). */
3720 it->end_charpos = ZV;
3721
3722 /* Return HANDLED_RECOMPUTE_PROPS only if function fontified
3723 something. This avoids an endless loop if they failed to
3724 fontify the text for which reason ever. */
3725 if (!NILP (Fget_char_property (pos, Qfontified, Qnil)))
3726 handled = HANDLED_RECOMPUTE_PROPS;
3727 }
3728
3729 return handled;
3730 }
3731
3732
3733 \f
3734 /***********************************************************************
3735 Faces
3736 ***********************************************************************/
3737
3738 /* Set up iterator IT from face properties at its current position.
3739 Called from handle_stop. */
3740
3741 static enum prop_handled
3742 handle_face_prop (struct it *it)
3743 {
3744 int new_face_id;
3745 ptrdiff_t next_stop;
3746
3747 if (!STRINGP (it->string))
3748 {
3749 new_face_id
3750 = face_at_buffer_position (it->w,
3751 IT_CHARPOS (*it),
3752 it->region_beg_charpos,
3753 it->region_end_charpos,
3754 &next_stop,
3755 (IT_CHARPOS (*it)
3756 + TEXT_PROP_DISTANCE_LIMIT),
3757 0, it->base_face_id);
3758
3759 /* Is this a start of a run of characters with box face?
3760 Caveat: this can be called for a freshly initialized
3761 iterator; face_id is -1 in this case. We know that the new
3762 face will not change until limit, i.e. if the new face has a
3763 box, all characters up to limit will have one. But, as
3764 usual, we don't know whether limit is really the end. */
3765 if (new_face_id != it->face_id)
3766 {
3767 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3768 /* If it->face_id is -1, old_face below will be NULL, see
3769 the definition of FACE_FROM_ID. This will happen if this
3770 is the initial call that gets the face. */
3771 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3772
3773 /* If the value of face_id of the iterator is -1, we have to
3774 look in front of IT's position and see whether there is a
3775 face there that's different from new_face_id. */
3776 if (!old_face && IT_CHARPOS (*it) > BEG)
3777 {
3778 int prev_face_id = face_before_it_pos (it);
3779
3780 old_face = FACE_FROM_ID (it->f, prev_face_id);
3781 }
3782
3783 /* If the new face has a box, but the old face does not,
3784 this is the start of a run of characters with box face,
3785 i.e. this character has a shadow on the left side. */
3786 it->start_of_box_run_p = (new_face->box != FACE_NO_BOX
3787 && (old_face == NULL || !old_face->box));
3788 it->face_box_p = new_face->box != FACE_NO_BOX;
3789 }
3790 }
3791 else
3792 {
3793 int base_face_id;
3794 ptrdiff_t bufpos;
3795 int i;
3796 Lisp_Object from_overlay
3797 = (it->current.overlay_string_index >= 0
3798 ? it->string_overlays[it->current.overlay_string_index
3799 % OVERLAY_STRING_CHUNK_SIZE]
3800 : Qnil);
3801
3802 /* See if we got to this string directly or indirectly from
3803 an overlay property. That includes the before-string or
3804 after-string of an overlay, strings in display properties
3805 provided by an overlay, their text properties, etc.
3806
3807 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3808 if (! NILP (from_overlay))
3809 for (i = it->sp - 1; i >= 0; i--)
3810 {
3811 if (it->stack[i].current.overlay_string_index >= 0)
3812 from_overlay
3813 = it->string_overlays[it->stack[i].current.overlay_string_index
3814 % OVERLAY_STRING_CHUNK_SIZE];
3815 else if (! NILP (it->stack[i].from_overlay))
3816 from_overlay = it->stack[i].from_overlay;
3817
3818 if (!NILP (from_overlay))
3819 break;
3820 }
3821
3822 if (! NILP (from_overlay))
3823 {
3824 bufpos = IT_CHARPOS (*it);
3825 /* For a string from an overlay, the base face depends
3826 only on text properties and ignores overlays. */
3827 base_face_id
3828 = face_for_overlay_string (it->w,
3829 IT_CHARPOS (*it),
3830 it->region_beg_charpos,
3831 it->region_end_charpos,
3832 &next_stop,
3833 (IT_CHARPOS (*it)
3834 + TEXT_PROP_DISTANCE_LIMIT),
3835 0,
3836 from_overlay);
3837 }
3838 else
3839 {
3840 bufpos = 0;
3841
3842 /* For strings from a `display' property, use the face at
3843 IT's current buffer position as the base face to merge
3844 with, so that overlay strings appear in the same face as
3845 surrounding text, unless they specify their own
3846 faces. */
3847 base_face_id = it->string_from_prefix_prop_p
3848 ? DEFAULT_FACE_ID
3849 : underlying_face_id (it);
3850 }
3851
3852 new_face_id = face_at_string_position (it->w,
3853 it->string,
3854 IT_STRING_CHARPOS (*it),
3855 bufpos,
3856 it->region_beg_charpos,
3857 it->region_end_charpos,
3858 &next_stop,
3859 base_face_id, 0);
3860
3861 /* Is this a start of a run of characters with box? Caveat:
3862 this can be called for a freshly allocated iterator; face_id
3863 is -1 is this case. We know that the new face will not
3864 change until the next check pos, i.e. if the new face has a
3865 box, all characters up to that position will have a
3866 box. But, as usual, we don't know whether that position
3867 is really the end. */
3868 if (new_face_id != it->face_id)
3869 {
3870 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3871 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3872
3873 /* If new face has a box but old face hasn't, this is the
3874 start of a run of characters with box, i.e. it has a
3875 shadow on the left side. */
3876 it->start_of_box_run_p
3877 = new_face->box && (old_face == NULL || !old_face->box);
3878 it->face_box_p = new_face->box != FACE_NO_BOX;
3879 }
3880 }
3881
3882 it->face_id = new_face_id;
3883 return HANDLED_NORMALLY;
3884 }
3885
3886
3887 /* Return the ID of the face ``underlying'' IT's current position,
3888 which is in a string. If the iterator is associated with a
3889 buffer, return the face at IT's current buffer position.
3890 Otherwise, use the iterator's base_face_id. */
3891
3892 static int
3893 underlying_face_id (struct it *it)
3894 {
3895 int face_id = it->base_face_id, i;
3896
3897 eassert (STRINGP (it->string));
3898
3899 for (i = it->sp - 1; i >= 0; --i)
3900 if (NILP (it->stack[i].string))
3901 face_id = it->stack[i].face_id;
3902
3903 return face_id;
3904 }
3905
3906
3907 /* Compute the face one character before or after the current position
3908 of IT, in the visual order. BEFORE_P non-zero means get the face
3909 in front (to the left in L2R paragraphs, to the right in R2L
3910 paragraphs) of IT's screen position. Value is the ID of the face. */
3911
3912 static int
3913 face_before_or_after_it_pos (struct it *it, int before_p)
3914 {
3915 int face_id, limit;
3916 ptrdiff_t next_check_charpos;
3917 struct it it_copy;
3918 void *it_copy_data = NULL;
3919
3920 eassert (it->s == NULL);
3921
3922 if (STRINGP (it->string))
3923 {
3924 ptrdiff_t bufpos, charpos;
3925 int base_face_id;
3926
3927 /* No face change past the end of the string (for the case
3928 we are padding with spaces). No face change before the
3929 string start. */
3930 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3931 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3932 return it->face_id;
3933
3934 if (!it->bidi_p)
3935 {
3936 /* Set charpos to the position before or after IT's current
3937 position, in the logical order, which in the non-bidi
3938 case is the same as the visual order. */
3939 if (before_p)
3940 charpos = IT_STRING_CHARPOS (*it) - 1;
3941 else if (it->what == IT_COMPOSITION)
3942 /* For composition, we must check the character after the
3943 composition. */
3944 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3945 else
3946 charpos = IT_STRING_CHARPOS (*it) + 1;
3947 }
3948 else
3949 {
3950 if (before_p)
3951 {
3952 /* With bidi iteration, the character before the current
3953 in the visual order cannot be found by simple
3954 iteration, because "reverse" reordering is not
3955 supported. Instead, we need to use the move_it_*
3956 family of functions. */
3957 /* Ignore face changes before the first visible
3958 character on this display line. */
3959 if (it->current_x <= it->first_visible_x)
3960 return it->face_id;
3961 SAVE_IT (it_copy, *it, it_copy_data);
3962 /* Implementation note: Since move_it_in_display_line
3963 works in the iterator geometry, and thinks the first
3964 character is always the leftmost, even in R2L lines,
3965 we don't need to distinguish between the R2L and L2R
3966 cases here. */
3967 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3968 it_copy.current_x - 1, MOVE_TO_X);
3969 charpos = IT_STRING_CHARPOS (it_copy);
3970 RESTORE_IT (it, it, it_copy_data);
3971 }
3972 else
3973 {
3974 /* Set charpos to the string position of the character
3975 that comes after IT's current position in the visual
3976 order. */
3977 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3978
3979 it_copy = *it;
3980 while (n--)
3981 bidi_move_to_visually_next (&it_copy.bidi_it);
3982
3983 charpos = it_copy.bidi_it.charpos;
3984 }
3985 }
3986 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3987
3988 if (it->current.overlay_string_index >= 0)
3989 bufpos = IT_CHARPOS (*it);
3990 else
3991 bufpos = 0;
3992
3993 base_face_id = underlying_face_id (it);
3994
3995 /* Get the face for ASCII, or unibyte. */
3996 face_id = face_at_string_position (it->w,
3997 it->string,
3998 charpos,
3999 bufpos,
4000 it->region_beg_charpos,
4001 it->region_end_charpos,
4002 &next_check_charpos,
4003 base_face_id, 0);
4004
4005 /* Correct the face for charsets different from ASCII. Do it
4006 for the multibyte case only. The face returned above is
4007 suitable for unibyte text if IT->string is unibyte. */
4008 if (STRING_MULTIBYTE (it->string))
4009 {
4010 struct text_pos pos1 = string_pos (charpos, it->string);
4011 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4012 int c, len;
4013 struct face *face = FACE_FROM_ID (it->f, face_id);
4014
4015 c = string_char_and_length (p, &len);
4016 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4017 }
4018 }
4019 else
4020 {
4021 struct text_pos pos;
4022
4023 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4024 || (IT_CHARPOS (*it) <= BEGV && before_p))
4025 return it->face_id;
4026
4027 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4028 pos = it->current.pos;
4029
4030 if (!it->bidi_p)
4031 {
4032 if (before_p)
4033 DEC_TEXT_POS (pos, it->multibyte_p);
4034 else
4035 {
4036 if (it->what == IT_COMPOSITION)
4037 {
4038 /* For composition, we must check the position after
4039 the composition. */
4040 pos.charpos += it->cmp_it.nchars;
4041 pos.bytepos += it->len;
4042 }
4043 else
4044 INC_TEXT_POS (pos, it->multibyte_p);
4045 }
4046 }
4047 else
4048 {
4049 if (before_p)
4050 {
4051 /* With bidi iteration, the character before the current
4052 in the visual order cannot be found by simple
4053 iteration, because "reverse" reordering is not
4054 supported. Instead, we need to use the move_it_*
4055 family of functions. */
4056 /* Ignore face changes before the first visible
4057 character on this display line. */
4058 if (it->current_x <= it->first_visible_x)
4059 return it->face_id;
4060 SAVE_IT (it_copy, *it, it_copy_data);
4061 /* Implementation note: Since move_it_in_display_line
4062 works in the iterator geometry, and thinks the first
4063 character is always the leftmost, even in R2L lines,
4064 we don't need to distinguish between the R2L and L2R
4065 cases here. */
4066 move_it_in_display_line (&it_copy, ZV,
4067 it_copy.current_x - 1, MOVE_TO_X);
4068 pos = it_copy.current.pos;
4069 RESTORE_IT (it, it, it_copy_data);
4070 }
4071 else
4072 {
4073 /* Set charpos to the buffer position of the character
4074 that comes after IT's current position in the visual
4075 order. */
4076 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4077
4078 it_copy = *it;
4079 while (n--)
4080 bidi_move_to_visually_next (&it_copy.bidi_it);
4081
4082 SET_TEXT_POS (pos,
4083 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4084 }
4085 }
4086 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4087
4088 /* Determine face for CHARSET_ASCII, or unibyte. */
4089 face_id = face_at_buffer_position (it->w,
4090 CHARPOS (pos),
4091 it->region_beg_charpos,
4092 it->region_end_charpos,
4093 &next_check_charpos,
4094 limit, 0, -1);
4095
4096 /* Correct the face for charsets different from ASCII. Do it
4097 for the multibyte case only. The face returned above is
4098 suitable for unibyte text if current_buffer is unibyte. */
4099 if (it->multibyte_p)
4100 {
4101 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4102 struct face *face = FACE_FROM_ID (it->f, face_id);
4103 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4104 }
4105 }
4106
4107 return face_id;
4108 }
4109
4110
4111 \f
4112 /***********************************************************************
4113 Invisible text
4114 ***********************************************************************/
4115
4116 /* Set up iterator IT from invisible properties at its current
4117 position. Called from handle_stop. */
4118
4119 static enum prop_handled
4120 handle_invisible_prop (struct it *it)
4121 {
4122 enum prop_handled handled = HANDLED_NORMALLY;
4123 int invis_p;
4124 Lisp_Object prop;
4125
4126 if (STRINGP (it->string))
4127 {
4128 Lisp_Object end_charpos, limit, charpos;
4129
4130 /* Get the value of the invisible text property at the
4131 current position. Value will be nil if there is no such
4132 property. */
4133 charpos = make_number (IT_STRING_CHARPOS (*it));
4134 prop = Fget_text_property (charpos, Qinvisible, it->string);
4135 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4136
4137 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4138 {
4139 /* Record whether we have to display an ellipsis for the
4140 invisible text. */
4141 int display_ellipsis_p = (invis_p == 2);
4142 ptrdiff_t len, endpos;
4143
4144 handled = HANDLED_RECOMPUTE_PROPS;
4145
4146 /* Get the position at which the next visible text can be
4147 found in IT->string, if any. */
4148 endpos = len = SCHARS (it->string);
4149 XSETINT (limit, len);
4150 do
4151 {
4152 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4153 it->string, limit);
4154 if (INTEGERP (end_charpos))
4155 {
4156 endpos = XFASTINT (end_charpos);
4157 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4158 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4159 if (invis_p == 2)
4160 display_ellipsis_p = 1;
4161 }
4162 }
4163 while (invis_p && endpos < len);
4164
4165 if (display_ellipsis_p)
4166 it->ellipsis_p = 1;
4167
4168 if (endpos < len)
4169 {
4170 /* Text at END_CHARPOS is visible. Move IT there. */
4171 struct text_pos old;
4172 ptrdiff_t oldpos;
4173
4174 old = it->current.string_pos;
4175 oldpos = CHARPOS (old);
4176 if (it->bidi_p)
4177 {
4178 if (it->bidi_it.first_elt
4179 && it->bidi_it.charpos < SCHARS (it->string))
4180 bidi_paragraph_init (it->paragraph_embedding,
4181 &it->bidi_it, 1);
4182 /* Bidi-iterate out of the invisible text. */
4183 do
4184 {
4185 bidi_move_to_visually_next (&it->bidi_it);
4186 }
4187 while (oldpos <= it->bidi_it.charpos
4188 && it->bidi_it.charpos < endpos);
4189
4190 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4191 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4192 if (IT_CHARPOS (*it) >= endpos)
4193 it->prev_stop = endpos;
4194 }
4195 else
4196 {
4197 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4198 compute_string_pos (&it->current.string_pos, old, it->string);
4199 }
4200 }
4201 else
4202 {
4203 /* The rest of the string is invisible. If this is an
4204 overlay string, proceed with the next overlay string
4205 or whatever comes and return a character from there. */
4206 if (it->current.overlay_string_index >= 0
4207 && !display_ellipsis_p)
4208 {
4209 next_overlay_string (it);
4210 /* Don't check for overlay strings when we just
4211 finished processing them. */
4212 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4213 }
4214 else
4215 {
4216 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4217 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4218 }
4219 }
4220 }
4221 }
4222 else
4223 {
4224 ptrdiff_t newpos, next_stop, start_charpos, tem;
4225 Lisp_Object pos, overlay;
4226
4227 /* First of all, is there invisible text at this position? */
4228 tem = start_charpos = IT_CHARPOS (*it);
4229 pos = make_number (tem);
4230 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4231 &overlay);
4232 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4233
4234 /* If we are on invisible text, skip over it. */
4235 if (invis_p && start_charpos < it->end_charpos)
4236 {
4237 /* Record whether we have to display an ellipsis for the
4238 invisible text. */
4239 int display_ellipsis_p = invis_p == 2;
4240
4241 handled = HANDLED_RECOMPUTE_PROPS;
4242
4243 /* Loop skipping over invisible text. The loop is left at
4244 ZV or with IT on the first char being visible again. */
4245 do
4246 {
4247 /* Try to skip some invisible text. Return value is the
4248 position reached which can be equal to where we start
4249 if there is nothing invisible there. This skips both
4250 over invisible text properties and overlays with
4251 invisible property. */
4252 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4253
4254 /* If we skipped nothing at all we weren't at invisible
4255 text in the first place. If everything to the end of
4256 the buffer was skipped, end the loop. */
4257 if (newpos == tem || newpos >= ZV)
4258 invis_p = 0;
4259 else
4260 {
4261 /* We skipped some characters but not necessarily
4262 all there are. Check if we ended up on visible
4263 text. Fget_char_property returns the property of
4264 the char before the given position, i.e. if we
4265 get invis_p = 0, this means that the char at
4266 newpos is visible. */
4267 pos = make_number (newpos);
4268 prop = Fget_char_property (pos, Qinvisible, it->window);
4269 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4270 }
4271
4272 /* If we ended up on invisible text, proceed to
4273 skip starting with next_stop. */
4274 if (invis_p)
4275 tem = next_stop;
4276
4277 /* If there are adjacent invisible texts, don't lose the
4278 second one's ellipsis. */
4279 if (invis_p == 2)
4280 display_ellipsis_p = 1;
4281 }
4282 while (invis_p);
4283
4284 /* The position newpos is now either ZV or on visible text. */
4285 if (it->bidi_p)
4286 {
4287 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4288 int on_newline =
4289 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4290 int after_newline =
4291 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4292
4293 /* If the invisible text ends on a newline or on a
4294 character after a newline, we can avoid the costly,
4295 character by character, bidi iteration to NEWPOS, and
4296 instead simply reseat the iterator there. That's
4297 because all bidi reordering information is tossed at
4298 the newline. This is a big win for modes that hide
4299 complete lines, like Outline, Org, etc. */
4300 if (on_newline || after_newline)
4301 {
4302 struct text_pos tpos;
4303 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4304
4305 SET_TEXT_POS (tpos, newpos, bpos);
4306 reseat_1 (it, tpos, 0);
4307 /* If we reseat on a newline/ZV, we need to prep the
4308 bidi iterator for advancing to the next character
4309 after the newline/EOB, keeping the current paragraph
4310 direction (so that PRODUCE_GLYPHS does TRT wrt
4311 prepending/appending glyphs to a glyph row). */
4312 if (on_newline)
4313 {
4314 it->bidi_it.first_elt = 0;
4315 it->bidi_it.paragraph_dir = pdir;
4316 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4317 it->bidi_it.nchars = 1;
4318 it->bidi_it.ch_len = 1;
4319 }
4320 }
4321 else /* Must use the slow method. */
4322 {
4323 /* With bidi iteration, the region of invisible text
4324 could start and/or end in the middle of a
4325 non-base embedding level. Therefore, we need to
4326 skip invisible text using the bidi iterator,
4327 starting at IT's current position, until we find
4328 ourselves outside of the invisible text.
4329 Skipping invisible text _after_ bidi iteration
4330 avoids affecting the visual order of the
4331 displayed text when invisible properties are
4332 added or removed. */
4333 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4334 {
4335 /* If we were `reseat'ed to a new paragraph,
4336 determine the paragraph base direction. We
4337 need to do it now because
4338 next_element_from_buffer may not have a
4339 chance to do it, if we are going to skip any
4340 text at the beginning, which resets the
4341 FIRST_ELT flag. */
4342 bidi_paragraph_init (it->paragraph_embedding,
4343 &it->bidi_it, 1);
4344 }
4345 do
4346 {
4347 bidi_move_to_visually_next (&it->bidi_it);
4348 }
4349 while (it->stop_charpos <= it->bidi_it.charpos
4350 && it->bidi_it.charpos < newpos);
4351 IT_CHARPOS (*it) = it->bidi_it.charpos;
4352 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4353 /* If we overstepped NEWPOS, record its position in
4354 the iterator, so that we skip invisible text if
4355 later the bidi iteration lands us in the
4356 invisible region again. */
4357 if (IT_CHARPOS (*it) >= newpos)
4358 it->prev_stop = newpos;
4359 }
4360 }
4361 else
4362 {
4363 IT_CHARPOS (*it) = newpos;
4364 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4365 }
4366
4367 /* If there are before-strings at the start of invisible
4368 text, and the text is invisible because of a text
4369 property, arrange to show before-strings because 20.x did
4370 it that way. (If the text is invisible because of an
4371 overlay property instead of a text property, this is
4372 already handled in the overlay code.) */
4373 if (NILP (overlay)
4374 && get_overlay_strings (it, it->stop_charpos))
4375 {
4376 handled = HANDLED_RECOMPUTE_PROPS;
4377 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4378 }
4379 else if (display_ellipsis_p)
4380 {
4381 /* Make sure that the glyphs of the ellipsis will get
4382 correct `charpos' values. If we would not update
4383 it->position here, the glyphs would belong to the
4384 last visible character _before_ the invisible
4385 text, which confuses `set_cursor_from_row'.
4386
4387 We use the last invisible position instead of the
4388 first because this way the cursor is always drawn on
4389 the first "." of the ellipsis, whenever PT is inside
4390 the invisible text. Otherwise the cursor would be
4391 placed _after_ the ellipsis when the point is after the
4392 first invisible character. */
4393 if (!STRINGP (it->object))
4394 {
4395 it->position.charpos = newpos - 1;
4396 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4397 }
4398 it->ellipsis_p = 1;
4399 /* Let the ellipsis display before
4400 considering any properties of the following char.
4401 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4402 handled = HANDLED_RETURN;
4403 }
4404 }
4405 }
4406
4407 return handled;
4408 }
4409
4410
4411 /* Make iterator IT return `...' next.
4412 Replaces LEN characters from buffer. */
4413
4414 static void
4415 setup_for_ellipsis (struct it *it, int len)
4416 {
4417 /* Use the display table definition for `...'. Invalid glyphs
4418 will be handled by the method returning elements from dpvec. */
4419 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4420 {
4421 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4422 it->dpvec = v->contents;
4423 it->dpend = v->contents + v->header.size;
4424 }
4425 else
4426 {
4427 /* Default `...'. */
4428 it->dpvec = default_invis_vector;
4429 it->dpend = default_invis_vector + 3;
4430 }
4431
4432 it->dpvec_char_len = len;
4433 it->current.dpvec_index = 0;
4434 it->dpvec_face_id = -1;
4435
4436 /* Remember the current face id in case glyphs specify faces.
4437 IT's face is restored in set_iterator_to_next.
4438 saved_face_id was set to preceding char's face in handle_stop. */
4439 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4440 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4441
4442 it->method = GET_FROM_DISPLAY_VECTOR;
4443 it->ellipsis_p = 1;
4444 }
4445
4446
4447 \f
4448 /***********************************************************************
4449 'display' property
4450 ***********************************************************************/
4451
4452 /* Set up iterator IT from `display' property at its current position.
4453 Called from handle_stop.
4454 We return HANDLED_RETURN if some part of the display property
4455 overrides the display of the buffer text itself.
4456 Otherwise we return HANDLED_NORMALLY. */
4457
4458 static enum prop_handled
4459 handle_display_prop (struct it *it)
4460 {
4461 Lisp_Object propval, object, overlay;
4462 struct text_pos *position;
4463 ptrdiff_t bufpos;
4464 /* Nonzero if some property replaces the display of the text itself. */
4465 int display_replaced_p = 0;
4466
4467 if (STRINGP (it->string))
4468 {
4469 object = it->string;
4470 position = &it->current.string_pos;
4471 bufpos = CHARPOS (it->current.pos);
4472 }
4473 else
4474 {
4475 XSETWINDOW (object, it->w);
4476 position = &it->current.pos;
4477 bufpos = CHARPOS (*position);
4478 }
4479
4480 /* Reset those iterator values set from display property values. */
4481 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4482 it->space_width = Qnil;
4483 it->font_height = Qnil;
4484 it->voffset = 0;
4485
4486 /* We don't support recursive `display' properties, i.e. string
4487 values that have a string `display' property, that have a string
4488 `display' property etc. */
4489 if (!it->string_from_display_prop_p)
4490 it->area = TEXT_AREA;
4491
4492 propval = get_char_property_and_overlay (make_number (position->charpos),
4493 Qdisplay, object, &overlay);
4494 if (NILP (propval))
4495 return HANDLED_NORMALLY;
4496 /* Now OVERLAY is the overlay that gave us this property, or nil
4497 if it was a text property. */
4498
4499 if (!STRINGP (it->string))
4500 object = it->w->buffer;
4501
4502 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4503 position, bufpos,
4504 FRAME_WINDOW_P (it->f));
4505
4506 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4507 }
4508
4509 /* Subroutine of handle_display_prop. Returns non-zero if the display
4510 specification in SPEC is a replacing specification, i.e. it would
4511 replace the text covered by `display' property with something else,
4512 such as an image or a display string. If SPEC includes any kind or
4513 `(space ...) specification, the value is 2; this is used by
4514 compute_display_string_pos, which see.
4515
4516 See handle_single_display_spec for documentation of arguments.
4517 frame_window_p is non-zero if the window being redisplayed is on a
4518 GUI frame; this argument is used only if IT is NULL, see below.
4519
4520 IT can be NULL, if this is called by the bidi reordering code
4521 through compute_display_string_pos, which see. In that case, this
4522 function only examines SPEC, but does not otherwise "handle" it, in
4523 the sense that it doesn't set up members of IT from the display
4524 spec. */
4525 static int
4526 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4527 Lisp_Object overlay, struct text_pos *position,
4528 ptrdiff_t bufpos, int frame_window_p)
4529 {
4530 int replacing_p = 0;
4531 int rv;
4532
4533 if (CONSP (spec)
4534 /* Simple specifications. */
4535 && !EQ (XCAR (spec), Qimage)
4536 && !EQ (XCAR (spec), Qspace)
4537 && !EQ (XCAR (spec), Qwhen)
4538 && !EQ (XCAR (spec), Qslice)
4539 && !EQ (XCAR (spec), Qspace_width)
4540 && !EQ (XCAR (spec), Qheight)
4541 && !EQ (XCAR (spec), Qraise)
4542 /* Marginal area specifications. */
4543 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4544 && !EQ (XCAR (spec), Qleft_fringe)
4545 && !EQ (XCAR (spec), Qright_fringe)
4546 && !NILP (XCAR (spec)))
4547 {
4548 for (; CONSP (spec); spec = XCDR (spec))
4549 {
4550 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4551 overlay, position, bufpos,
4552 replacing_p, frame_window_p)))
4553 {
4554 replacing_p = rv;
4555 /* If some text in a string is replaced, `position' no
4556 longer points to the position of `object'. */
4557 if (!it || STRINGP (object))
4558 break;
4559 }
4560 }
4561 }
4562 else if (VECTORP (spec))
4563 {
4564 ptrdiff_t i;
4565 for (i = 0; i < ASIZE (spec); ++i)
4566 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4567 overlay, position, bufpos,
4568 replacing_p, frame_window_p)))
4569 {
4570 replacing_p = rv;
4571 /* If some text in a string is replaced, `position' no
4572 longer points to the position of `object'. */
4573 if (!it || STRINGP (object))
4574 break;
4575 }
4576 }
4577 else
4578 {
4579 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4580 position, bufpos, 0,
4581 frame_window_p)))
4582 replacing_p = rv;
4583 }
4584
4585 return replacing_p;
4586 }
4587
4588 /* Value is the position of the end of the `display' property starting
4589 at START_POS in OBJECT. */
4590
4591 static struct text_pos
4592 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4593 {
4594 Lisp_Object end;
4595 struct text_pos end_pos;
4596
4597 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4598 Qdisplay, object, Qnil);
4599 CHARPOS (end_pos) = XFASTINT (end);
4600 if (STRINGP (object))
4601 compute_string_pos (&end_pos, start_pos, it->string);
4602 else
4603 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4604
4605 return end_pos;
4606 }
4607
4608
4609 /* Set up IT from a single `display' property specification SPEC. OBJECT
4610 is the object in which the `display' property was found. *POSITION
4611 is the position in OBJECT at which the `display' property was found.
4612 BUFPOS is the buffer position of OBJECT (different from POSITION if
4613 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4614 previously saw a display specification which already replaced text
4615 display with something else, for example an image; we ignore such
4616 properties after the first one has been processed.
4617
4618 OVERLAY is the overlay this `display' property came from,
4619 or nil if it was a text property.
4620
4621 If SPEC is a `space' or `image' specification, and in some other
4622 cases too, set *POSITION to the position where the `display'
4623 property ends.
4624
4625 If IT is NULL, only examine the property specification in SPEC, but
4626 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4627 is intended to be displayed in a window on a GUI frame.
4628
4629 Value is non-zero if something was found which replaces the display
4630 of buffer or string text. */
4631
4632 static int
4633 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4634 Lisp_Object overlay, struct text_pos *position,
4635 ptrdiff_t bufpos, int display_replaced_p,
4636 int frame_window_p)
4637 {
4638 Lisp_Object form;
4639 Lisp_Object location, value;
4640 struct text_pos start_pos = *position;
4641 int valid_p;
4642
4643 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4644 If the result is non-nil, use VALUE instead of SPEC. */
4645 form = Qt;
4646 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4647 {
4648 spec = XCDR (spec);
4649 if (!CONSP (spec))
4650 return 0;
4651 form = XCAR (spec);
4652 spec = XCDR (spec);
4653 }
4654
4655 if (!NILP (form) && !EQ (form, Qt))
4656 {
4657 ptrdiff_t count = SPECPDL_INDEX ();
4658 struct gcpro gcpro1;
4659
4660 /* Bind `object' to the object having the `display' property, a
4661 buffer or string. Bind `position' to the position in the
4662 object where the property was found, and `buffer-position'
4663 to the current position in the buffer. */
4664
4665 if (NILP (object))
4666 XSETBUFFER (object, current_buffer);
4667 specbind (Qobject, object);
4668 specbind (Qposition, make_number (CHARPOS (*position)));
4669 specbind (Qbuffer_position, make_number (bufpos));
4670 GCPRO1 (form);
4671 form = safe_eval (form);
4672 UNGCPRO;
4673 unbind_to (count, Qnil);
4674 }
4675
4676 if (NILP (form))
4677 return 0;
4678
4679 /* Handle `(height HEIGHT)' specifications. */
4680 if (CONSP (spec)
4681 && EQ (XCAR (spec), Qheight)
4682 && CONSP (XCDR (spec)))
4683 {
4684 if (it)
4685 {
4686 if (!FRAME_WINDOW_P (it->f))
4687 return 0;
4688
4689 it->font_height = XCAR (XCDR (spec));
4690 if (!NILP (it->font_height))
4691 {
4692 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4693 int new_height = -1;
4694
4695 if (CONSP (it->font_height)
4696 && (EQ (XCAR (it->font_height), Qplus)
4697 || EQ (XCAR (it->font_height), Qminus))
4698 && CONSP (XCDR (it->font_height))
4699 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4700 {
4701 /* `(+ N)' or `(- N)' where N is an integer. */
4702 int steps = XINT (XCAR (XCDR (it->font_height)));
4703 if (EQ (XCAR (it->font_height), Qplus))
4704 steps = - steps;
4705 it->face_id = smaller_face (it->f, it->face_id, steps);
4706 }
4707 else if (FUNCTIONP (it->font_height))
4708 {
4709 /* Call function with current height as argument.
4710 Value is the new height. */
4711 Lisp_Object height;
4712 height = safe_call1 (it->font_height,
4713 face->lface[LFACE_HEIGHT_INDEX]);
4714 if (NUMBERP (height))
4715 new_height = XFLOATINT (height);
4716 }
4717 else if (NUMBERP (it->font_height))
4718 {
4719 /* Value is a multiple of the canonical char height. */
4720 struct face *f;
4721
4722 f = FACE_FROM_ID (it->f,
4723 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4724 new_height = (XFLOATINT (it->font_height)
4725 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4726 }
4727 else
4728 {
4729 /* Evaluate IT->font_height with `height' bound to the
4730 current specified height to get the new height. */
4731 ptrdiff_t count = SPECPDL_INDEX ();
4732
4733 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4734 value = safe_eval (it->font_height);
4735 unbind_to (count, Qnil);
4736
4737 if (NUMBERP (value))
4738 new_height = XFLOATINT (value);
4739 }
4740
4741 if (new_height > 0)
4742 it->face_id = face_with_height (it->f, it->face_id, new_height);
4743 }
4744 }
4745
4746 return 0;
4747 }
4748
4749 /* Handle `(space-width WIDTH)'. */
4750 if (CONSP (spec)
4751 && EQ (XCAR (spec), Qspace_width)
4752 && CONSP (XCDR (spec)))
4753 {
4754 if (it)
4755 {
4756 if (!FRAME_WINDOW_P (it->f))
4757 return 0;
4758
4759 value = XCAR (XCDR (spec));
4760 if (NUMBERP (value) && XFLOATINT (value) > 0)
4761 it->space_width = value;
4762 }
4763
4764 return 0;
4765 }
4766
4767 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4768 if (CONSP (spec)
4769 && EQ (XCAR (spec), Qslice))
4770 {
4771 Lisp_Object tem;
4772
4773 if (it)
4774 {
4775 if (!FRAME_WINDOW_P (it->f))
4776 return 0;
4777
4778 if (tem = XCDR (spec), CONSP (tem))
4779 {
4780 it->slice.x = XCAR (tem);
4781 if (tem = XCDR (tem), CONSP (tem))
4782 {
4783 it->slice.y = XCAR (tem);
4784 if (tem = XCDR (tem), CONSP (tem))
4785 {
4786 it->slice.width = XCAR (tem);
4787 if (tem = XCDR (tem), CONSP (tem))
4788 it->slice.height = XCAR (tem);
4789 }
4790 }
4791 }
4792 }
4793
4794 return 0;
4795 }
4796
4797 /* Handle `(raise FACTOR)'. */
4798 if (CONSP (spec)
4799 && EQ (XCAR (spec), Qraise)
4800 && CONSP (XCDR (spec)))
4801 {
4802 if (it)
4803 {
4804 if (!FRAME_WINDOW_P (it->f))
4805 return 0;
4806
4807 #ifdef HAVE_WINDOW_SYSTEM
4808 value = XCAR (XCDR (spec));
4809 if (NUMBERP (value))
4810 {
4811 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4812 it->voffset = - (XFLOATINT (value)
4813 * (FONT_HEIGHT (face->font)));
4814 }
4815 #endif /* HAVE_WINDOW_SYSTEM */
4816 }
4817
4818 return 0;
4819 }
4820
4821 /* Don't handle the other kinds of display specifications
4822 inside a string that we got from a `display' property. */
4823 if (it && it->string_from_display_prop_p)
4824 return 0;
4825
4826 /* Characters having this form of property are not displayed, so
4827 we have to find the end of the property. */
4828 if (it)
4829 {
4830 start_pos = *position;
4831 *position = display_prop_end (it, object, start_pos);
4832 }
4833 value = Qnil;
4834
4835 /* Stop the scan at that end position--we assume that all
4836 text properties change there. */
4837 if (it)
4838 it->stop_charpos = position->charpos;
4839
4840 /* Handle `(left-fringe BITMAP [FACE])'
4841 and `(right-fringe BITMAP [FACE])'. */
4842 if (CONSP (spec)
4843 && (EQ (XCAR (spec), Qleft_fringe)
4844 || EQ (XCAR (spec), Qright_fringe))
4845 && CONSP (XCDR (spec)))
4846 {
4847 int fringe_bitmap;
4848
4849 if (it)
4850 {
4851 if (!FRAME_WINDOW_P (it->f))
4852 /* If we return here, POSITION has been advanced
4853 across the text with this property. */
4854 {
4855 /* Synchronize the bidi iterator with POSITION. This is
4856 needed because we are not going to push the iterator
4857 on behalf of this display property, so there will be
4858 no pop_it call to do this synchronization for us. */
4859 if (it->bidi_p)
4860 {
4861 it->position = *position;
4862 iterate_out_of_display_property (it);
4863 *position = it->position;
4864 }
4865 return 1;
4866 }
4867 }
4868 else if (!frame_window_p)
4869 return 1;
4870
4871 #ifdef HAVE_WINDOW_SYSTEM
4872 value = XCAR (XCDR (spec));
4873 if (!SYMBOLP (value)
4874 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4875 /* If we return here, POSITION has been advanced
4876 across the text with this property. */
4877 {
4878 if (it && it->bidi_p)
4879 {
4880 it->position = *position;
4881 iterate_out_of_display_property (it);
4882 *position = it->position;
4883 }
4884 return 1;
4885 }
4886
4887 if (it)
4888 {
4889 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4890
4891 if (CONSP (XCDR (XCDR (spec))))
4892 {
4893 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4894 int face_id2 = lookup_derived_face (it->f, face_name,
4895 FRINGE_FACE_ID, 0);
4896 if (face_id2 >= 0)
4897 face_id = face_id2;
4898 }
4899
4900 /* Save current settings of IT so that we can restore them
4901 when we are finished with the glyph property value. */
4902 push_it (it, position);
4903
4904 it->area = TEXT_AREA;
4905 it->what = IT_IMAGE;
4906 it->image_id = -1; /* no image */
4907 it->position = start_pos;
4908 it->object = NILP (object) ? it->w->buffer : object;
4909 it->method = GET_FROM_IMAGE;
4910 it->from_overlay = Qnil;
4911 it->face_id = face_id;
4912 it->from_disp_prop_p = 1;
4913
4914 /* Say that we haven't consumed the characters with
4915 `display' property yet. The call to pop_it in
4916 set_iterator_to_next will clean this up. */
4917 *position = start_pos;
4918
4919 if (EQ (XCAR (spec), Qleft_fringe))
4920 {
4921 it->left_user_fringe_bitmap = fringe_bitmap;
4922 it->left_user_fringe_face_id = face_id;
4923 }
4924 else
4925 {
4926 it->right_user_fringe_bitmap = fringe_bitmap;
4927 it->right_user_fringe_face_id = face_id;
4928 }
4929 }
4930 #endif /* HAVE_WINDOW_SYSTEM */
4931 return 1;
4932 }
4933
4934 /* Prepare to handle `((margin left-margin) ...)',
4935 `((margin right-margin) ...)' and `((margin nil) ...)'
4936 prefixes for display specifications. */
4937 location = Qunbound;
4938 if (CONSP (spec) && CONSP (XCAR (spec)))
4939 {
4940 Lisp_Object tem;
4941
4942 value = XCDR (spec);
4943 if (CONSP (value))
4944 value = XCAR (value);
4945
4946 tem = XCAR (spec);
4947 if (EQ (XCAR (tem), Qmargin)
4948 && (tem = XCDR (tem),
4949 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4950 (NILP (tem)
4951 || EQ (tem, Qleft_margin)
4952 || EQ (tem, Qright_margin))))
4953 location = tem;
4954 }
4955
4956 if (EQ (location, Qunbound))
4957 {
4958 location = Qnil;
4959 value = spec;
4960 }
4961
4962 /* After this point, VALUE is the property after any
4963 margin prefix has been stripped. It must be a string,
4964 an image specification, or `(space ...)'.
4965
4966 LOCATION specifies where to display: `left-margin',
4967 `right-margin' or nil. */
4968
4969 valid_p = (STRINGP (value)
4970 #ifdef HAVE_WINDOW_SYSTEM
4971 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4972 && valid_image_p (value))
4973 #endif /* not HAVE_WINDOW_SYSTEM */
4974 || (CONSP (value) && EQ (XCAR (value), Qspace)));
4975
4976 if (valid_p && !display_replaced_p)
4977 {
4978 int retval = 1;
4979
4980 if (!it)
4981 {
4982 /* Callers need to know whether the display spec is any kind
4983 of `(space ...)' spec that is about to affect text-area
4984 display. */
4985 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4986 retval = 2;
4987 return retval;
4988 }
4989
4990 /* Save current settings of IT so that we can restore them
4991 when we are finished with the glyph property value. */
4992 push_it (it, position);
4993 it->from_overlay = overlay;
4994 it->from_disp_prop_p = 1;
4995
4996 if (NILP (location))
4997 it->area = TEXT_AREA;
4998 else if (EQ (location, Qleft_margin))
4999 it->area = LEFT_MARGIN_AREA;
5000 else
5001 it->area = RIGHT_MARGIN_AREA;
5002
5003 if (STRINGP (value))
5004 {
5005 it->string = value;
5006 it->multibyte_p = STRING_MULTIBYTE (it->string);
5007 it->current.overlay_string_index = -1;
5008 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5009 it->end_charpos = it->string_nchars = SCHARS (it->string);
5010 it->method = GET_FROM_STRING;
5011 it->stop_charpos = 0;
5012 it->prev_stop = 0;
5013 it->base_level_stop = 0;
5014 it->string_from_display_prop_p = 1;
5015 /* Say that we haven't consumed the characters with
5016 `display' property yet. The call to pop_it in
5017 set_iterator_to_next will clean this up. */
5018 if (BUFFERP (object))
5019 *position = start_pos;
5020
5021 /* Force paragraph direction to be that of the parent
5022 object. If the parent object's paragraph direction is
5023 not yet determined, default to L2R. */
5024 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5025 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5026 else
5027 it->paragraph_embedding = L2R;
5028
5029 /* Set up the bidi iterator for this display string. */
5030 if (it->bidi_p)
5031 {
5032 it->bidi_it.string.lstring = it->string;
5033 it->bidi_it.string.s = NULL;
5034 it->bidi_it.string.schars = it->end_charpos;
5035 it->bidi_it.string.bufpos = bufpos;
5036 it->bidi_it.string.from_disp_str = 1;
5037 it->bidi_it.string.unibyte = !it->multibyte_p;
5038 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5039 }
5040 }
5041 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5042 {
5043 it->method = GET_FROM_STRETCH;
5044 it->object = value;
5045 *position = it->position = start_pos;
5046 retval = 1 + (it->area == TEXT_AREA);
5047 }
5048 #ifdef HAVE_WINDOW_SYSTEM
5049 else
5050 {
5051 it->what = IT_IMAGE;
5052 it->image_id = lookup_image (it->f, value);
5053 it->position = start_pos;
5054 it->object = NILP (object) ? it->w->buffer : object;
5055 it->method = GET_FROM_IMAGE;
5056
5057 /* Say that we haven't consumed the characters with
5058 `display' property yet. The call to pop_it in
5059 set_iterator_to_next will clean this up. */
5060 *position = start_pos;
5061 }
5062 #endif /* HAVE_WINDOW_SYSTEM */
5063
5064 return retval;
5065 }
5066
5067 /* Invalid property or property not supported. Restore
5068 POSITION to what it was before. */
5069 *position = start_pos;
5070 return 0;
5071 }
5072
5073 /* Check if PROP is a display property value whose text should be
5074 treated as intangible. OVERLAY is the overlay from which PROP
5075 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5076 specify the buffer position covered by PROP. */
5077
5078 int
5079 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5080 ptrdiff_t charpos, ptrdiff_t bytepos)
5081 {
5082 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5083 struct text_pos position;
5084
5085 SET_TEXT_POS (position, charpos, bytepos);
5086 return handle_display_spec (NULL, prop, Qnil, overlay,
5087 &position, charpos, frame_window_p);
5088 }
5089
5090
5091 /* Return 1 if PROP is a display sub-property value containing STRING.
5092
5093 Implementation note: this and the following function are really
5094 special cases of handle_display_spec and
5095 handle_single_display_spec, and should ideally use the same code.
5096 Until they do, these two pairs must be consistent and must be
5097 modified in sync. */
5098
5099 static int
5100 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5101 {
5102 if (EQ (string, prop))
5103 return 1;
5104
5105 /* Skip over `when FORM'. */
5106 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5107 {
5108 prop = XCDR (prop);
5109 if (!CONSP (prop))
5110 return 0;
5111 /* Actually, the condition following `when' should be eval'ed,
5112 like handle_single_display_spec does, and we should return
5113 zero if it evaluates to nil. However, this function is
5114 called only when the buffer was already displayed and some
5115 glyph in the glyph matrix was found to come from a display
5116 string. Therefore, the condition was already evaluated, and
5117 the result was non-nil, otherwise the display string wouldn't
5118 have been displayed and we would have never been called for
5119 this property. Thus, we can skip the evaluation and assume
5120 its result is non-nil. */
5121 prop = XCDR (prop);
5122 }
5123
5124 if (CONSP (prop))
5125 /* Skip over `margin LOCATION'. */
5126 if (EQ (XCAR (prop), Qmargin))
5127 {
5128 prop = XCDR (prop);
5129 if (!CONSP (prop))
5130 return 0;
5131
5132 prop = XCDR (prop);
5133 if (!CONSP (prop))
5134 return 0;
5135 }
5136
5137 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5138 }
5139
5140
5141 /* Return 1 if STRING appears in the `display' property PROP. */
5142
5143 static int
5144 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5145 {
5146 if (CONSP (prop)
5147 && !EQ (XCAR (prop), Qwhen)
5148 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5149 {
5150 /* A list of sub-properties. */
5151 while (CONSP (prop))
5152 {
5153 if (single_display_spec_string_p (XCAR (prop), string))
5154 return 1;
5155 prop = XCDR (prop);
5156 }
5157 }
5158 else if (VECTORP (prop))
5159 {
5160 /* A vector of sub-properties. */
5161 ptrdiff_t i;
5162 for (i = 0; i < ASIZE (prop); ++i)
5163 if (single_display_spec_string_p (AREF (prop, i), string))
5164 return 1;
5165 }
5166 else
5167 return single_display_spec_string_p (prop, string);
5168
5169 return 0;
5170 }
5171
5172 /* Look for STRING in overlays and text properties in the current
5173 buffer, between character positions FROM and TO (excluding TO).
5174 BACK_P non-zero means look back (in this case, TO is supposed to be
5175 less than FROM).
5176 Value is the first character position where STRING was found, or
5177 zero if it wasn't found before hitting TO.
5178
5179 This function may only use code that doesn't eval because it is
5180 called asynchronously from note_mouse_highlight. */
5181
5182 static ptrdiff_t
5183 string_buffer_position_lim (Lisp_Object string,
5184 ptrdiff_t from, ptrdiff_t to, int back_p)
5185 {
5186 Lisp_Object limit, prop, pos;
5187 int found = 0;
5188
5189 pos = make_number (max (from, BEGV));
5190
5191 if (!back_p) /* looking forward */
5192 {
5193 limit = make_number (min (to, ZV));
5194 while (!found && !EQ (pos, limit))
5195 {
5196 prop = Fget_char_property (pos, Qdisplay, Qnil);
5197 if (!NILP (prop) && display_prop_string_p (prop, string))
5198 found = 1;
5199 else
5200 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5201 limit);
5202 }
5203 }
5204 else /* looking back */
5205 {
5206 limit = make_number (max (to, BEGV));
5207 while (!found && !EQ (pos, limit))
5208 {
5209 prop = Fget_char_property (pos, Qdisplay, Qnil);
5210 if (!NILP (prop) && display_prop_string_p (prop, string))
5211 found = 1;
5212 else
5213 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5214 limit);
5215 }
5216 }
5217
5218 return found ? XINT (pos) : 0;
5219 }
5220
5221 /* Determine which buffer position in current buffer STRING comes from.
5222 AROUND_CHARPOS is an approximate position where it could come from.
5223 Value is the buffer position or 0 if it couldn't be determined.
5224
5225 This function is necessary because we don't record buffer positions
5226 in glyphs generated from strings (to keep struct glyph small).
5227 This function may only use code that doesn't eval because it is
5228 called asynchronously from note_mouse_highlight. */
5229
5230 static ptrdiff_t
5231 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5232 {
5233 const int MAX_DISTANCE = 1000;
5234 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5235 around_charpos + MAX_DISTANCE,
5236 0);
5237
5238 if (!found)
5239 found = string_buffer_position_lim (string, around_charpos,
5240 around_charpos - MAX_DISTANCE, 1);
5241 return found;
5242 }
5243
5244
5245 \f
5246 /***********************************************************************
5247 `composition' property
5248 ***********************************************************************/
5249
5250 /* Set up iterator IT from `composition' property at its current
5251 position. Called from handle_stop. */
5252
5253 static enum prop_handled
5254 handle_composition_prop (struct it *it)
5255 {
5256 Lisp_Object prop, string;
5257 ptrdiff_t pos, pos_byte, start, end;
5258
5259 if (STRINGP (it->string))
5260 {
5261 unsigned char *s;
5262
5263 pos = IT_STRING_CHARPOS (*it);
5264 pos_byte = IT_STRING_BYTEPOS (*it);
5265 string = it->string;
5266 s = SDATA (string) + pos_byte;
5267 it->c = STRING_CHAR (s);
5268 }
5269 else
5270 {
5271 pos = IT_CHARPOS (*it);
5272 pos_byte = IT_BYTEPOS (*it);
5273 string = Qnil;
5274 it->c = FETCH_CHAR (pos_byte);
5275 }
5276
5277 /* If there's a valid composition and point is not inside of the
5278 composition (in the case that the composition is from the current
5279 buffer), draw a glyph composed from the composition components. */
5280 if (find_composition (pos, -1, &start, &end, &prop, string)
5281 && COMPOSITION_VALID_P (start, end, prop)
5282 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5283 {
5284 if (start < pos)
5285 /* As we can't handle this situation (perhaps font-lock added
5286 a new composition), we just return here hoping that next
5287 redisplay will detect this composition much earlier. */
5288 return HANDLED_NORMALLY;
5289 if (start != pos)
5290 {
5291 if (STRINGP (it->string))
5292 pos_byte = string_char_to_byte (it->string, start);
5293 else
5294 pos_byte = CHAR_TO_BYTE (start);
5295 }
5296 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5297 prop, string);
5298
5299 if (it->cmp_it.id >= 0)
5300 {
5301 it->cmp_it.ch = -1;
5302 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5303 it->cmp_it.nglyphs = -1;
5304 }
5305 }
5306
5307 return HANDLED_NORMALLY;
5308 }
5309
5310
5311 \f
5312 /***********************************************************************
5313 Overlay strings
5314 ***********************************************************************/
5315
5316 /* The following structure is used to record overlay strings for
5317 later sorting in load_overlay_strings. */
5318
5319 struct overlay_entry
5320 {
5321 Lisp_Object overlay;
5322 Lisp_Object string;
5323 EMACS_INT priority;
5324 int after_string_p;
5325 };
5326
5327
5328 /* Set up iterator IT from overlay strings at its current position.
5329 Called from handle_stop. */
5330
5331 static enum prop_handled
5332 handle_overlay_change (struct it *it)
5333 {
5334 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5335 return HANDLED_RECOMPUTE_PROPS;
5336 else
5337 return HANDLED_NORMALLY;
5338 }
5339
5340
5341 /* Set up the next overlay string for delivery by IT, if there is an
5342 overlay string to deliver. Called by set_iterator_to_next when the
5343 end of the current overlay string is reached. If there are more
5344 overlay strings to display, IT->string and
5345 IT->current.overlay_string_index are set appropriately here.
5346 Otherwise IT->string is set to nil. */
5347
5348 static void
5349 next_overlay_string (struct it *it)
5350 {
5351 ++it->current.overlay_string_index;
5352 if (it->current.overlay_string_index == it->n_overlay_strings)
5353 {
5354 /* No more overlay strings. Restore IT's settings to what
5355 they were before overlay strings were processed, and
5356 continue to deliver from current_buffer. */
5357
5358 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5359 pop_it (it);
5360 eassert (it->sp > 0
5361 || (NILP (it->string)
5362 && it->method == GET_FROM_BUFFER
5363 && it->stop_charpos >= BEGV
5364 && it->stop_charpos <= it->end_charpos));
5365 it->current.overlay_string_index = -1;
5366 it->n_overlay_strings = 0;
5367 it->overlay_strings_charpos = -1;
5368 /* If there's an empty display string on the stack, pop the
5369 stack, to resync the bidi iterator with IT's position. Such
5370 empty strings are pushed onto the stack in
5371 get_overlay_strings_1. */
5372 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5373 pop_it (it);
5374
5375 /* If we're at the end of the buffer, record that we have
5376 processed the overlay strings there already, so that
5377 next_element_from_buffer doesn't try it again. */
5378 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5379 it->overlay_strings_at_end_processed_p = 1;
5380 }
5381 else
5382 {
5383 /* There are more overlay strings to process. If
5384 IT->current.overlay_string_index has advanced to a position
5385 where we must load IT->overlay_strings with more strings, do
5386 it. We must load at the IT->overlay_strings_charpos where
5387 IT->n_overlay_strings was originally computed; when invisible
5388 text is present, this might not be IT_CHARPOS (Bug#7016). */
5389 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5390
5391 if (it->current.overlay_string_index && i == 0)
5392 load_overlay_strings (it, it->overlay_strings_charpos);
5393
5394 /* Initialize IT to deliver display elements from the overlay
5395 string. */
5396 it->string = it->overlay_strings[i];
5397 it->multibyte_p = STRING_MULTIBYTE (it->string);
5398 SET_TEXT_POS (it->current.string_pos, 0, 0);
5399 it->method = GET_FROM_STRING;
5400 it->stop_charpos = 0;
5401 it->end_charpos = SCHARS (it->string);
5402 if (it->cmp_it.stop_pos >= 0)
5403 it->cmp_it.stop_pos = 0;
5404 it->prev_stop = 0;
5405 it->base_level_stop = 0;
5406
5407 /* Set up the bidi iterator for this overlay string. */
5408 if (it->bidi_p)
5409 {
5410 it->bidi_it.string.lstring = it->string;
5411 it->bidi_it.string.s = NULL;
5412 it->bidi_it.string.schars = SCHARS (it->string);
5413 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5414 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5415 it->bidi_it.string.unibyte = !it->multibyte_p;
5416 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5417 }
5418 }
5419
5420 CHECK_IT (it);
5421 }
5422
5423
5424 /* Compare two overlay_entry structures E1 and E2. Used as a
5425 comparison function for qsort in load_overlay_strings. Overlay
5426 strings for the same position are sorted so that
5427
5428 1. All after-strings come in front of before-strings, except
5429 when they come from the same overlay.
5430
5431 2. Within after-strings, strings are sorted so that overlay strings
5432 from overlays with higher priorities come first.
5433
5434 2. Within before-strings, strings are sorted so that overlay
5435 strings from overlays with higher priorities come last.
5436
5437 Value is analogous to strcmp. */
5438
5439
5440 static int
5441 compare_overlay_entries (const void *e1, const void *e2)
5442 {
5443 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5444 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5445 int result;
5446
5447 if (entry1->after_string_p != entry2->after_string_p)
5448 {
5449 /* Let after-strings appear in front of before-strings if
5450 they come from different overlays. */
5451 if (EQ (entry1->overlay, entry2->overlay))
5452 result = entry1->after_string_p ? 1 : -1;
5453 else
5454 result = entry1->after_string_p ? -1 : 1;
5455 }
5456 else if (entry1->priority != entry2->priority)
5457 {
5458 if (entry1->after_string_p)
5459 /* After-strings sorted in order of decreasing priority. */
5460 result = entry2->priority < entry1->priority ? -1 : 1;
5461 else
5462 /* Before-strings sorted in order of increasing priority. */
5463 result = entry1->priority < entry2->priority ? -1 : 1;
5464 }
5465 else
5466 result = 0;
5467
5468 return result;
5469 }
5470
5471
5472 /* Load the vector IT->overlay_strings with overlay strings from IT's
5473 current buffer position, or from CHARPOS if that is > 0. Set
5474 IT->n_overlays to the total number of overlay strings found.
5475
5476 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5477 a time. On entry into load_overlay_strings,
5478 IT->current.overlay_string_index gives the number of overlay
5479 strings that have already been loaded by previous calls to this
5480 function.
5481
5482 IT->add_overlay_start contains an additional overlay start
5483 position to consider for taking overlay strings from, if non-zero.
5484 This position comes into play when the overlay has an `invisible'
5485 property, and both before and after-strings. When we've skipped to
5486 the end of the overlay, because of its `invisible' property, we
5487 nevertheless want its before-string to appear.
5488 IT->add_overlay_start will contain the overlay start position
5489 in this case.
5490
5491 Overlay strings are sorted so that after-string strings come in
5492 front of before-string strings. Within before and after-strings,
5493 strings are sorted by overlay priority. See also function
5494 compare_overlay_entries. */
5495
5496 static void
5497 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5498 {
5499 Lisp_Object overlay, window, str, invisible;
5500 struct Lisp_Overlay *ov;
5501 ptrdiff_t start, end;
5502 ptrdiff_t size = 20;
5503 ptrdiff_t n = 0, i, j;
5504 int invis_p;
5505 struct overlay_entry *entries = alloca (size * sizeof *entries);
5506 USE_SAFE_ALLOCA;
5507
5508 if (charpos <= 0)
5509 charpos = IT_CHARPOS (*it);
5510
5511 /* Append the overlay string STRING of overlay OVERLAY to vector
5512 `entries' which has size `size' and currently contains `n'
5513 elements. AFTER_P non-zero means STRING is an after-string of
5514 OVERLAY. */
5515 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5516 do \
5517 { \
5518 Lisp_Object priority; \
5519 \
5520 if (n == size) \
5521 { \
5522 struct overlay_entry *old = entries; \
5523 SAFE_NALLOCA (entries, 2, size); \
5524 memcpy (entries, old, size * sizeof *entries); \
5525 size *= 2; \
5526 } \
5527 \
5528 entries[n].string = (STRING); \
5529 entries[n].overlay = (OVERLAY); \
5530 priority = Foverlay_get ((OVERLAY), Qpriority); \
5531 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5532 entries[n].after_string_p = (AFTER_P); \
5533 ++n; \
5534 } \
5535 while (0)
5536
5537 /* Process overlay before the overlay center. */
5538 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5539 {
5540 XSETMISC (overlay, ov);
5541 eassert (OVERLAYP (overlay));
5542 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5543 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5544
5545 if (end < charpos)
5546 break;
5547
5548 /* Skip this overlay if it doesn't start or end at IT's current
5549 position. */
5550 if (end != charpos && start != charpos)
5551 continue;
5552
5553 /* Skip this overlay if it doesn't apply to IT->w. */
5554 window = Foverlay_get (overlay, Qwindow);
5555 if (WINDOWP (window) && XWINDOW (window) != it->w)
5556 continue;
5557
5558 /* If the text ``under'' the overlay is invisible, both before-
5559 and after-strings from this overlay are visible; start and
5560 end position are indistinguishable. */
5561 invisible = Foverlay_get (overlay, Qinvisible);
5562 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5563
5564 /* If overlay has a non-empty before-string, record it. */
5565 if ((start == charpos || (end == charpos && invis_p))
5566 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5567 && SCHARS (str))
5568 RECORD_OVERLAY_STRING (overlay, str, 0);
5569
5570 /* If overlay has a non-empty after-string, record it. */
5571 if ((end == charpos || (start == charpos && invis_p))
5572 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5573 && SCHARS (str))
5574 RECORD_OVERLAY_STRING (overlay, str, 1);
5575 }
5576
5577 /* Process overlays after the overlay center. */
5578 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5579 {
5580 XSETMISC (overlay, ov);
5581 eassert (OVERLAYP (overlay));
5582 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5583 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5584
5585 if (start > charpos)
5586 break;
5587
5588 /* Skip this overlay if it doesn't start or end at IT's current
5589 position. */
5590 if (end != charpos && start != charpos)
5591 continue;
5592
5593 /* Skip this overlay if it doesn't apply to IT->w. */
5594 window = Foverlay_get (overlay, Qwindow);
5595 if (WINDOWP (window) && XWINDOW (window) != it->w)
5596 continue;
5597
5598 /* If the text ``under'' the overlay is invisible, it has a zero
5599 dimension, and both before- and after-strings apply. */
5600 invisible = Foverlay_get (overlay, Qinvisible);
5601 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5602
5603 /* If overlay has a non-empty before-string, record it. */
5604 if ((start == charpos || (end == charpos && invis_p))
5605 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5606 && SCHARS (str))
5607 RECORD_OVERLAY_STRING (overlay, str, 0);
5608
5609 /* If overlay has a non-empty after-string, record it. */
5610 if ((end == charpos || (start == charpos && invis_p))
5611 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5612 && SCHARS (str))
5613 RECORD_OVERLAY_STRING (overlay, str, 1);
5614 }
5615
5616 #undef RECORD_OVERLAY_STRING
5617
5618 /* Sort entries. */
5619 if (n > 1)
5620 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5621
5622 /* Record number of overlay strings, and where we computed it. */
5623 it->n_overlay_strings = n;
5624 it->overlay_strings_charpos = charpos;
5625
5626 /* IT->current.overlay_string_index is the number of overlay strings
5627 that have already been consumed by IT. Copy some of the
5628 remaining overlay strings to IT->overlay_strings. */
5629 i = 0;
5630 j = it->current.overlay_string_index;
5631 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5632 {
5633 it->overlay_strings[i] = entries[j].string;
5634 it->string_overlays[i++] = entries[j++].overlay;
5635 }
5636
5637 CHECK_IT (it);
5638 SAFE_FREE ();
5639 }
5640
5641
5642 /* Get the first chunk of overlay strings at IT's current buffer
5643 position, or at CHARPOS if that is > 0. Value is non-zero if at
5644 least one overlay string was found. */
5645
5646 static int
5647 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5648 {
5649 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5650 process. This fills IT->overlay_strings with strings, and sets
5651 IT->n_overlay_strings to the total number of strings to process.
5652 IT->pos.overlay_string_index has to be set temporarily to zero
5653 because load_overlay_strings needs this; it must be set to -1
5654 when no overlay strings are found because a zero value would
5655 indicate a position in the first overlay string. */
5656 it->current.overlay_string_index = 0;
5657 load_overlay_strings (it, charpos);
5658
5659 /* If we found overlay strings, set up IT to deliver display
5660 elements from the first one. Otherwise set up IT to deliver
5661 from current_buffer. */
5662 if (it->n_overlay_strings)
5663 {
5664 /* Make sure we know settings in current_buffer, so that we can
5665 restore meaningful values when we're done with the overlay
5666 strings. */
5667 if (compute_stop_p)
5668 compute_stop_pos (it);
5669 eassert (it->face_id >= 0);
5670
5671 /* Save IT's settings. They are restored after all overlay
5672 strings have been processed. */
5673 eassert (!compute_stop_p || it->sp == 0);
5674
5675 /* When called from handle_stop, there might be an empty display
5676 string loaded. In that case, don't bother saving it. But
5677 don't use this optimization with the bidi iterator, since we
5678 need the corresponding pop_it call to resync the bidi
5679 iterator's position with IT's position, after we are done
5680 with the overlay strings. (The corresponding call to pop_it
5681 in case of an empty display string is in
5682 next_overlay_string.) */
5683 if (!(!it->bidi_p
5684 && STRINGP (it->string) && !SCHARS (it->string)))
5685 push_it (it, NULL);
5686
5687 /* Set up IT to deliver display elements from the first overlay
5688 string. */
5689 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5690 it->string = it->overlay_strings[0];
5691 it->from_overlay = Qnil;
5692 it->stop_charpos = 0;
5693 eassert (STRINGP (it->string));
5694 it->end_charpos = SCHARS (it->string);
5695 it->prev_stop = 0;
5696 it->base_level_stop = 0;
5697 it->multibyte_p = STRING_MULTIBYTE (it->string);
5698 it->method = GET_FROM_STRING;
5699 it->from_disp_prop_p = 0;
5700
5701 /* Force paragraph direction to be that of the parent
5702 buffer. */
5703 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5704 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5705 else
5706 it->paragraph_embedding = L2R;
5707
5708 /* Set up the bidi iterator for this overlay string. */
5709 if (it->bidi_p)
5710 {
5711 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5712
5713 it->bidi_it.string.lstring = it->string;
5714 it->bidi_it.string.s = NULL;
5715 it->bidi_it.string.schars = SCHARS (it->string);
5716 it->bidi_it.string.bufpos = pos;
5717 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5718 it->bidi_it.string.unibyte = !it->multibyte_p;
5719 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5720 }
5721 return 1;
5722 }
5723
5724 it->current.overlay_string_index = -1;
5725 return 0;
5726 }
5727
5728 static int
5729 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5730 {
5731 it->string = Qnil;
5732 it->method = GET_FROM_BUFFER;
5733
5734 (void) get_overlay_strings_1 (it, charpos, 1);
5735
5736 CHECK_IT (it);
5737
5738 /* Value is non-zero if we found at least one overlay string. */
5739 return STRINGP (it->string);
5740 }
5741
5742
5743 \f
5744 /***********************************************************************
5745 Saving and restoring state
5746 ***********************************************************************/
5747
5748 /* Save current settings of IT on IT->stack. Called, for example,
5749 before setting up IT for an overlay string, to be able to restore
5750 IT's settings to what they were after the overlay string has been
5751 processed. If POSITION is non-NULL, it is the position to save on
5752 the stack instead of IT->position. */
5753
5754 static void
5755 push_it (struct it *it, struct text_pos *position)
5756 {
5757 struct iterator_stack_entry *p;
5758
5759 eassert (it->sp < IT_STACK_SIZE);
5760 p = it->stack + it->sp;
5761
5762 p->stop_charpos = it->stop_charpos;
5763 p->prev_stop = it->prev_stop;
5764 p->base_level_stop = it->base_level_stop;
5765 p->cmp_it = it->cmp_it;
5766 eassert (it->face_id >= 0);
5767 p->face_id = it->face_id;
5768 p->string = it->string;
5769 p->method = it->method;
5770 p->from_overlay = it->from_overlay;
5771 switch (p->method)
5772 {
5773 case GET_FROM_IMAGE:
5774 p->u.image.object = it->object;
5775 p->u.image.image_id = it->image_id;
5776 p->u.image.slice = it->slice;
5777 break;
5778 case GET_FROM_STRETCH:
5779 p->u.stretch.object = it->object;
5780 break;
5781 }
5782 p->position = position ? *position : it->position;
5783 p->current = it->current;
5784 p->end_charpos = it->end_charpos;
5785 p->string_nchars = it->string_nchars;
5786 p->area = it->area;
5787 p->multibyte_p = it->multibyte_p;
5788 p->avoid_cursor_p = it->avoid_cursor_p;
5789 p->space_width = it->space_width;
5790 p->font_height = it->font_height;
5791 p->voffset = it->voffset;
5792 p->string_from_display_prop_p = it->string_from_display_prop_p;
5793 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5794 p->display_ellipsis_p = 0;
5795 p->line_wrap = it->line_wrap;
5796 p->bidi_p = it->bidi_p;
5797 p->paragraph_embedding = it->paragraph_embedding;
5798 p->from_disp_prop_p = it->from_disp_prop_p;
5799 ++it->sp;
5800
5801 /* Save the state of the bidi iterator as well. */
5802 if (it->bidi_p)
5803 bidi_push_it (&it->bidi_it);
5804 }
5805
5806 static void
5807 iterate_out_of_display_property (struct it *it)
5808 {
5809 int buffer_p = !STRINGP (it->string);
5810 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5811 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5812
5813 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5814
5815 /* Maybe initialize paragraph direction. If we are at the beginning
5816 of a new paragraph, next_element_from_buffer may not have a
5817 chance to do that. */
5818 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5819 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5820 /* prev_stop can be zero, so check against BEGV as well. */
5821 while (it->bidi_it.charpos >= bob
5822 && it->prev_stop <= it->bidi_it.charpos
5823 && it->bidi_it.charpos < CHARPOS (it->position)
5824 && it->bidi_it.charpos < eob)
5825 bidi_move_to_visually_next (&it->bidi_it);
5826 /* Record the stop_pos we just crossed, for when we cross it
5827 back, maybe. */
5828 if (it->bidi_it.charpos > CHARPOS (it->position))
5829 it->prev_stop = CHARPOS (it->position);
5830 /* If we ended up not where pop_it put us, resync IT's
5831 positional members with the bidi iterator. */
5832 if (it->bidi_it.charpos != CHARPOS (it->position))
5833 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5834 if (buffer_p)
5835 it->current.pos = it->position;
5836 else
5837 it->current.string_pos = it->position;
5838 }
5839
5840 /* Restore IT's settings from IT->stack. Called, for example, when no
5841 more overlay strings must be processed, and we return to delivering
5842 display elements from a buffer, or when the end of a string from a
5843 `display' property is reached and we return to delivering display
5844 elements from an overlay string, or from a buffer. */
5845
5846 static void
5847 pop_it (struct it *it)
5848 {
5849 struct iterator_stack_entry *p;
5850 int from_display_prop = it->from_disp_prop_p;
5851
5852 eassert (it->sp > 0);
5853 --it->sp;
5854 p = it->stack + it->sp;
5855 it->stop_charpos = p->stop_charpos;
5856 it->prev_stop = p->prev_stop;
5857 it->base_level_stop = p->base_level_stop;
5858 it->cmp_it = p->cmp_it;
5859 it->face_id = p->face_id;
5860 it->current = p->current;
5861 it->position = p->position;
5862 it->string = p->string;
5863 it->from_overlay = p->from_overlay;
5864 if (NILP (it->string))
5865 SET_TEXT_POS (it->current.string_pos, -1, -1);
5866 it->method = p->method;
5867 switch (it->method)
5868 {
5869 case GET_FROM_IMAGE:
5870 it->image_id = p->u.image.image_id;
5871 it->object = p->u.image.object;
5872 it->slice = p->u.image.slice;
5873 break;
5874 case GET_FROM_STRETCH:
5875 it->object = p->u.stretch.object;
5876 break;
5877 case GET_FROM_BUFFER:
5878 it->object = it->w->buffer;
5879 break;
5880 case GET_FROM_STRING:
5881 it->object = it->string;
5882 break;
5883 case GET_FROM_DISPLAY_VECTOR:
5884 if (it->s)
5885 it->method = GET_FROM_C_STRING;
5886 else if (STRINGP (it->string))
5887 it->method = GET_FROM_STRING;
5888 else
5889 {
5890 it->method = GET_FROM_BUFFER;
5891 it->object = it->w->buffer;
5892 }
5893 }
5894 it->end_charpos = p->end_charpos;
5895 it->string_nchars = p->string_nchars;
5896 it->area = p->area;
5897 it->multibyte_p = p->multibyte_p;
5898 it->avoid_cursor_p = p->avoid_cursor_p;
5899 it->space_width = p->space_width;
5900 it->font_height = p->font_height;
5901 it->voffset = p->voffset;
5902 it->string_from_display_prop_p = p->string_from_display_prop_p;
5903 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5904 it->line_wrap = p->line_wrap;
5905 it->bidi_p = p->bidi_p;
5906 it->paragraph_embedding = p->paragraph_embedding;
5907 it->from_disp_prop_p = p->from_disp_prop_p;
5908 if (it->bidi_p)
5909 {
5910 bidi_pop_it (&it->bidi_it);
5911 /* Bidi-iterate until we get out of the portion of text, if any,
5912 covered by a `display' text property or by an overlay with
5913 `display' property. (We cannot just jump there, because the
5914 internal coherency of the bidi iterator state can not be
5915 preserved across such jumps.) We also must determine the
5916 paragraph base direction if the overlay we just processed is
5917 at the beginning of a new paragraph. */
5918 if (from_display_prop
5919 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5920 iterate_out_of_display_property (it);
5921
5922 eassert ((BUFFERP (it->object)
5923 && IT_CHARPOS (*it) == it->bidi_it.charpos
5924 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5925 || (STRINGP (it->object)
5926 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5927 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5928 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5929 }
5930 }
5931
5932
5933 \f
5934 /***********************************************************************
5935 Moving over lines
5936 ***********************************************************************/
5937
5938 /* Set IT's current position to the previous line start. */
5939
5940 static void
5941 back_to_previous_line_start (struct it *it)
5942 {
5943 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5944 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5945 }
5946
5947
5948 /* Move IT to the next line start.
5949
5950 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5951 we skipped over part of the text (as opposed to moving the iterator
5952 continuously over the text). Otherwise, don't change the value
5953 of *SKIPPED_P.
5954
5955 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5956 iterator on the newline, if it was found.
5957
5958 Newlines may come from buffer text, overlay strings, or strings
5959 displayed via the `display' property. That's the reason we can't
5960 simply use find_next_newline_no_quit.
5961
5962 Note that this function may not skip over invisible text that is so
5963 because of text properties and immediately follows a newline. If
5964 it would, function reseat_at_next_visible_line_start, when called
5965 from set_iterator_to_next, would effectively make invisible
5966 characters following a newline part of the wrong glyph row, which
5967 leads to wrong cursor motion. */
5968
5969 static int
5970 forward_to_next_line_start (struct it *it, int *skipped_p,
5971 struct bidi_it *bidi_it_prev)
5972 {
5973 ptrdiff_t old_selective;
5974 int newline_found_p, n;
5975 const int MAX_NEWLINE_DISTANCE = 500;
5976
5977 /* If already on a newline, just consume it to avoid unintended
5978 skipping over invisible text below. */
5979 if (it->what == IT_CHARACTER
5980 && it->c == '\n'
5981 && CHARPOS (it->position) == IT_CHARPOS (*it))
5982 {
5983 if (it->bidi_p && bidi_it_prev)
5984 *bidi_it_prev = it->bidi_it;
5985 set_iterator_to_next (it, 0);
5986 it->c = 0;
5987 return 1;
5988 }
5989
5990 /* Don't handle selective display in the following. It's (a)
5991 unnecessary because it's done by the caller, and (b) leads to an
5992 infinite recursion because next_element_from_ellipsis indirectly
5993 calls this function. */
5994 old_selective = it->selective;
5995 it->selective = 0;
5996
5997 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
5998 from buffer text. */
5999 for (n = newline_found_p = 0;
6000 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6001 n += STRINGP (it->string) ? 0 : 1)
6002 {
6003 if (!get_next_display_element (it))
6004 return 0;
6005 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6006 if (newline_found_p && it->bidi_p && bidi_it_prev)
6007 *bidi_it_prev = it->bidi_it;
6008 set_iterator_to_next (it, 0);
6009 }
6010
6011 /* If we didn't find a newline near enough, see if we can use a
6012 short-cut. */
6013 if (!newline_found_p)
6014 {
6015 ptrdiff_t start = IT_CHARPOS (*it);
6016 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
6017 Lisp_Object pos;
6018
6019 eassert (!STRINGP (it->string));
6020
6021 /* If there isn't any `display' property in sight, and no
6022 overlays, we can just use the position of the newline in
6023 buffer text. */
6024 if (it->stop_charpos >= limit
6025 || ((pos = Fnext_single_property_change (make_number (start),
6026 Qdisplay, Qnil,
6027 make_number (limit)),
6028 NILP (pos))
6029 && next_overlay_change (start) == ZV))
6030 {
6031 if (!it->bidi_p)
6032 {
6033 IT_CHARPOS (*it) = limit;
6034 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
6035 }
6036 else
6037 {
6038 struct bidi_it bprev;
6039
6040 /* Help bidi.c avoid expensive searches for display
6041 properties and overlays, by telling it that there are
6042 none up to `limit'. */
6043 if (it->bidi_it.disp_pos < limit)
6044 {
6045 it->bidi_it.disp_pos = limit;
6046 it->bidi_it.disp_prop = 0;
6047 }
6048 do {
6049 bprev = it->bidi_it;
6050 bidi_move_to_visually_next (&it->bidi_it);
6051 } while (it->bidi_it.charpos != limit);
6052 IT_CHARPOS (*it) = limit;
6053 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6054 if (bidi_it_prev)
6055 *bidi_it_prev = bprev;
6056 }
6057 *skipped_p = newline_found_p = 1;
6058 }
6059 else
6060 {
6061 while (get_next_display_element (it)
6062 && !newline_found_p)
6063 {
6064 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6065 if (newline_found_p && it->bidi_p && bidi_it_prev)
6066 *bidi_it_prev = it->bidi_it;
6067 set_iterator_to_next (it, 0);
6068 }
6069 }
6070 }
6071
6072 it->selective = old_selective;
6073 return newline_found_p;
6074 }
6075
6076
6077 /* Set IT's current position to the previous visible line start. Skip
6078 invisible text that is so either due to text properties or due to
6079 selective display. Caution: this does not change IT->current_x and
6080 IT->hpos. */
6081
6082 static void
6083 back_to_previous_visible_line_start (struct it *it)
6084 {
6085 while (IT_CHARPOS (*it) > BEGV)
6086 {
6087 back_to_previous_line_start (it);
6088
6089 if (IT_CHARPOS (*it) <= BEGV)
6090 break;
6091
6092 /* If selective > 0, then lines indented more than its value are
6093 invisible. */
6094 if (it->selective > 0
6095 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6096 it->selective))
6097 continue;
6098
6099 /* Check the newline before point for invisibility. */
6100 {
6101 Lisp_Object prop;
6102 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6103 Qinvisible, it->window);
6104 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6105 continue;
6106 }
6107
6108 if (IT_CHARPOS (*it) <= BEGV)
6109 break;
6110
6111 {
6112 struct it it2;
6113 void *it2data = NULL;
6114 ptrdiff_t pos;
6115 ptrdiff_t beg, end;
6116 Lisp_Object val, overlay;
6117
6118 SAVE_IT (it2, *it, it2data);
6119
6120 /* If newline is part of a composition, continue from start of composition */
6121 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6122 && beg < IT_CHARPOS (*it))
6123 goto replaced;
6124
6125 /* If newline is replaced by a display property, find start of overlay
6126 or interval and continue search from that point. */
6127 pos = --IT_CHARPOS (it2);
6128 --IT_BYTEPOS (it2);
6129 it2.sp = 0;
6130 bidi_unshelve_cache (NULL, 0);
6131 it2.string_from_display_prop_p = 0;
6132 it2.from_disp_prop_p = 0;
6133 if (handle_display_prop (&it2) == HANDLED_RETURN
6134 && !NILP (val = get_char_property_and_overlay
6135 (make_number (pos), Qdisplay, Qnil, &overlay))
6136 && (OVERLAYP (overlay)
6137 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6138 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6139 {
6140 RESTORE_IT (it, it, it2data);
6141 goto replaced;
6142 }
6143
6144 /* Newline is not replaced by anything -- so we are done. */
6145 RESTORE_IT (it, it, it2data);
6146 break;
6147
6148 replaced:
6149 if (beg < BEGV)
6150 beg = BEGV;
6151 IT_CHARPOS (*it) = beg;
6152 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6153 }
6154 }
6155
6156 it->continuation_lines_width = 0;
6157
6158 eassert (IT_CHARPOS (*it) >= BEGV);
6159 eassert (IT_CHARPOS (*it) == BEGV
6160 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6161 CHECK_IT (it);
6162 }
6163
6164
6165 /* Reseat iterator IT at the previous visible line start. Skip
6166 invisible text that is so either due to text properties or due to
6167 selective display. At the end, update IT's overlay information,
6168 face information etc. */
6169
6170 void
6171 reseat_at_previous_visible_line_start (struct it *it)
6172 {
6173 back_to_previous_visible_line_start (it);
6174 reseat (it, it->current.pos, 1);
6175 CHECK_IT (it);
6176 }
6177
6178
6179 /* Reseat iterator IT on the next visible line start in the current
6180 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6181 preceding the line start. Skip over invisible text that is so
6182 because of selective display. Compute faces, overlays etc at the
6183 new position. Note that this function does not skip over text that
6184 is invisible because of text properties. */
6185
6186 static void
6187 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6188 {
6189 int newline_found_p, skipped_p = 0;
6190 struct bidi_it bidi_it_prev;
6191
6192 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6193
6194 /* Skip over lines that are invisible because they are indented
6195 more than the value of IT->selective. */
6196 if (it->selective > 0)
6197 while (IT_CHARPOS (*it) < ZV
6198 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6199 it->selective))
6200 {
6201 eassert (IT_BYTEPOS (*it) == BEGV
6202 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6203 newline_found_p =
6204 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6205 }
6206
6207 /* Position on the newline if that's what's requested. */
6208 if (on_newline_p && newline_found_p)
6209 {
6210 if (STRINGP (it->string))
6211 {
6212 if (IT_STRING_CHARPOS (*it) > 0)
6213 {
6214 if (!it->bidi_p)
6215 {
6216 --IT_STRING_CHARPOS (*it);
6217 --IT_STRING_BYTEPOS (*it);
6218 }
6219 else
6220 {
6221 /* We need to restore the bidi iterator to the state
6222 it had on the newline, and resync the IT's
6223 position with that. */
6224 it->bidi_it = bidi_it_prev;
6225 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6226 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6227 }
6228 }
6229 }
6230 else if (IT_CHARPOS (*it) > BEGV)
6231 {
6232 if (!it->bidi_p)
6233 {
6234 --IT_CHARPOS (*it);
6235 --IT_BYTEPOS (*it);
6236 }
6237 else
6238 {
6239 /* We need to restore the bidi iterator to the state it
6240 had on the newline and resync IT with that. */
6241 it->bidi_it = bidi_it_prev;
6242 IT_CHARPOS (*it) = it->bidi_it.charpos;
6243 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6244 }
6245 reseat (it, it->current.pos, 0);
6246 }
6247 }
6248 else if (skipped_p)
6249 reseat (it, it->current.pos, 0);
6250
6251 CHECK_IT (it);
6252 }
6253
6254
6255 \f
6256 /***********************************************************************
6257 Changing an iterator's position
6258 ***********************************************************************/
6259
6260 /* Change IT's current position to POS in current_buffer. If FORCE_P
6261 is non-zero, always check for text properties at the new position.
6262 Otherwise, text properties are only looked up if POS >=
6263 IT->check_charpos of a property. */
6264
6265 static void
6266 reseat (struct it *it, struct text_pos pos, int force_p)
6267 {
6268 ptrdiff_t original_pos = IT_CHARPOS (*it);
6269
6270 reseat_1 (it, pos, 0);
6271
6272 /* Determine where to check text properties. Avoid doing it
6273 where possible because text property lookup is very expensive. */
6274 if (force_p
6275 || CHARPOS (pos) > it->stop_charpos
6276 || CHARPOS (pos) < original_pos)
6277 {
6278 if (it->bidi_p)
6279 {
6280 /* For bidi iteration, we need to prime prev_stop and
6281 base_level_stop with our best estimations. */
6282 /* Implementation note: Of course, POS is not necessarily a
6283 stop position, so assigning prev_pos to it is a lie; we
6284 should have called compute_stop_backwards. However, if
6285 the current buffer does not include any R2L characters,
6286 that call would be a waste of cycles, because the
6287 iterator will never move back, and thus never cross this
6288 "fake" stop position. So we delay that backward search
6289 until the time we really need it, in next_element_from_buffer. */
6290 if (CHARPOS (pos) != it->prev_stop)
6291 it->prev_stop = CHARPOS (pos);
6292 if (CHARPOS (pos) < it->base_level_stop)
6293 it->base_level_stop = 0; /* meaning it's unknown */
6294 handle_stop (it);
6295 }
6296 else
6297 {
6298 handle_stop (it);
6299 it->prev_stop = it->base_level_stop = 0;
6300 }
6301
6302 }
6303
6304 CHECK_IT (it);
6305 }
6306
6307
6308 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6309 IT->stop_pos to POS, also. */
6310
6311 static void
6312 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6313 {
6314 /* Don't call this function when scanning a C string. */
6315 eassert (it->s == NULL);
6316
6317 /* POS must be a reasonable value. */
6318 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6319
6320 it->current.pos = it->position = pos;
6321 it->end_charpos = ZV;
6322 it->dpvec = NULL;
6323 it->current.dpvec_index = -1;
6324 it->current.overlay_string_index = -1;
6325 IT_STRING_CHARPOS (*it) = -1;
6326 IT_STRING_BYTEPOS (*it) = -1;
6327 it->string = Qnil;
6328 it->method = GET_FROM_BUFFER;
6329 it->object = it->w->buffer;
6330 it->area = TEXT_AREA;
6331 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6332 it->sp = 0;
6333 it->string_from_display_prop_p = 0;
6334 it->string_from_prefix_prop_p = 0;
6335
6336 it->from_disp_prop_p = 0;
6337 it->face_before_selective_p = 0;
6338 if (it->bidi_p)
6339 {
6340 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6341 &it->bidi_it);
6342 bidi_unshelve_cache (NULL, 0);
6343 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6344 it->bidi_it.string.s = NULL;
6345 it->bidi_it.string.lstring = Qnil;
6346 it->bidi_it.string.bufpos = 0;
6347 it->bidi_it.string.unibyte = 0;
6348 }
6349
6350 if (set_stop_p)
6351 {
6352 it->stop_charpos = CHARPOS (pos);
6353 it->base_level_stop = CHARPOS (pos);
6354 }
6355 /* This make the information stored in it->cmp_it invalidate. */
6356 it->cmp_it.id = -1;
6357 }
6358
6359
6360 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6361 If S is non-null, it is a C string to iterate over. Otherwise,
6362 STRING gives a Lisp string to iterate over.
6363
6364 If PRECISION > 0, don't return more then PRECISION number of
6365 characters from the string.
6366
6367 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6368 characters have been returned. FIELD_WIDTH < 0 means an infinite
6369 field width.
6370
6371 MULTIBYTE = 0 means disable processing of multibyte characters,
6372 MULTIBYTE > 0 means enable it,
6373 MULTIBYTE < 0 means use IT->multibyte_p.
6374
6375 IT must be initialized via a prior call to init_iterator before
6376 calling this function. */
6377
6378 static void
6379 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6380 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6381 int multibyte)
6382 {
6383 /* No region in strings. */
6384 it->region_beg_charpos = it->region_end_charpos = -1;
6385
6386 /* No text property checks performed by default, but see below. */
6387 it->stop_charpos = -1;
6388
6389 /* Set iterator position and end position. */
6390 memset (&it->current, 0, sizeof it->current);
6391 it->current.overlay_string_index = -1;
6392 it->current.dpvec_index = -1;
6393 eassert (charpos >= 0);
6394
6395 /* If STRING is specified, use its multibyteness, otherwise use the
6396 setting of MULTIBYTE, if specified. */
6397 if (multibyte >= 0)
6398 it->multibyte_p = multibyte > 0;
6399
6400 /* Bidirectional reordering of strings is controlled by the default
6401 value of bidi-display-reordering. Don't try to reorder while
6402 loading loadup.el, as the necessary character property tables are
6403 not yet available. */
6404 it->bidi_p =
6405 NILP (Vpurify_flag)
6406 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6407
6408 if (s == NULL)
6409 {
6410 eassert (STRINGP (string));
6411 it->string = string;
6412 it->s = NULL;
6413 it->end_charpos = it->string_nchars = SCHARS (string);
6414 it->method = GET_FROM_STRING;
6415 it->current.string_pos = string_pos (charpos, string);
6416
6417 if (it->bidi_p)
6418 {
6419 it->bidi_it.string.lstring = string;
6420 it->bidi_it.string.s = NULL;
6421 it->bidi_it.string.schars = it->end_charpos;
6422 it->bidi_it.string.bufpos = 0;
6423 it->bidi_it.string.from_disp_str = 0;
6424 it->bidi_it.string.unibyte = !it->multibyte_p;
6425 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6426 FRAME_WINDOW_P (it->f), &it->bidi_it);
6427 }
6428 }
6429 else
6430 {
6431 it->s = (const unsigned char *) s;
6432 it->string = Qnil;
6433
6434 /* Note that we use IT->current.pos, not it->current.string_pos,
6435 for displaying C strings. */
6436 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6437 if (it->multibyte_p)
6438 {
6439 it->current.pos = c_string_pos (charpos, s, 1);
6440 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6441 }
6442 else
6443 {
6444 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6445 it->end_charpos = it->string_nchars = strlen (s);
6446 }
6447
6448 if (it->bidi_p)
6449 {
6450 it->bidi_it.string.lstring = Qnil;
6451 it->bidi_it.string.s = (const unsigned char *) s;
6452 it->bidi_it.string.schars = it->end_charpos;
6453 it->bidi_it.string.bufpos = 0;
6454 it->bidi_it.string.from_disp_str = 0;
6455 it->bidi_it.string.unibyte = !it->multibyte_p;
6456 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6457 &it->bidi_it);
6458 }
6459 it->method = GET_FROM_C_STRING;
6460 }
6461
6462 /* PRECISION > 0 means don't return more than PRECISION characters
6463 from the string. */
6464 if (precision > 0 && it->end_charpos - charpos > precision)
6465 {
6466 it->end_charpos = it->string_nchars = charpos + precision;
6467 if (it->bidi_p)
6468 it->bidi_it.string.schars = it->end_charpos;
6469 }
6470
6471 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6472 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6473 FIELD_WIDTH < 0 means infinite field width. This is useful for
6474 padding with `-' at the end of a mode line. */
6475 if (field_width < 0)
6476 field_width = INFINITY;
6477 /* Implementation note: We deliberately don't enlarge
6478 it->bidi_it.string.schars here to fit it->end_charpos, because
6479 the bidi iterator cannot produce characters out of thin air. */
6480 if (field_width > it->end_charpos - charpos)
6481 it->end_charpos = charpos + field_width;
6482
6483 /* Use the standard display table for displaying strings. */
6484 if (DISP_TABLE_P (Vstandard_display_table))
6485 it->dp = XCHAR_TABLE (Vstandard_display_table);
6486
6487 it->stop_charpos = charpos;
6488 it->prev_stop = charpos;
6489 it->base_level_stop = 0;
6490 if (it->bidi_p)
6491 {
6492 it->bidi_it.first_elt = 1;
6493 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6494 it->bidi_it.disp_pos = -1;
6495 }
6496 if (s == NULL && it->multibyte_p)
6497 {
6498 ptrdiff_t endpos = SCHARS (it->string);
6499 if (endpos > it->end_charpos)
6500 endpos = it->end_charpos;
6501 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6502 it->string);
6503 }
6504 CHECK_IT (it);
6505 }
6506
6507
6508 \f
6509 /***********************************************************************
6510 Iteration
6511 ***********************************************************************/
6512
6513 /* Map enum it_method value to corresponding next_element_from_* function. */
6514
6515 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6516 {
6517 next_element_from_buffer,
6518 next_element_from_display_vector,
6519 next_element_from_string,
6520 next_element_from_c_string,
6521 next_element_from_image,
6522 next_element_from_stretch
6523 };
6524
6525 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6526
6527
6528 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6529 (possibly with the following characters). */
6530
6531 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6532 ((IT)->cmp_it.id >= 0 \
6533 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6534 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6535 END_CHARPOS, (IT)->w, \
6536 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6537 (IT)->string)))
6538
6539
6540 /* Lookup the char-table Vglyphless_char_display for character C (-1
6541 if we want information for no-font case), and return the display
6542 method symbol. By side-effect, update it->what and
6543 it->glyphless_method. This function is called from
6544 get_next_display_element for each character element, and from
6545 x_produce_glyphs when no suitable font was found. */
6546
6547 Lisp_Object
6548 lookup_glyphless_char_display (int c, struct it *it)
6549 {
6550 Lisp_Object glyphless_method = Qnil;
6551
6552 if (CHAR_TABLE_P (Vglyphless_char_display)
6553 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6554 {
6555 if (c >= 0)
6556 {
6557 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6558 if (CONSP (glyphless_method))
6559 glyphless_method = FRAME_WINDOW_P (it->f)
6560 ? XCAR (glyphless_method)
6561 : XCDR (glyphless_method);
6562 }
6563 else
6564 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6565 }
6566
6567 retry:
6568 if (NILP (glyphless_method))
6569 {
6570 if (c >= 0)
6571 /* The default is to display the character by a proper font. */
6572 return Qnil;
6573 /* The default for the no-font case is to display an empty box. */
6574 glyphless_method = Qempty_box;
6575 }
6576 if (EQ (glyphless_method, Qzero_width))
6577 {
6578 if (c >= 0)
6579 return glyphless_method;
6580 /* This method can't be used for the no-font case. */
6581 glyphless_method = Qempty_box;
6582 }
6583 if (EQ (glyphless_method, Qthin_space))
6584 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6585 else if (EQ (glyphless_method, Qempty_box))
6586 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6587 else if (EQ (glyphless_method, Qhex_code))
6588 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6589 else if (STRINGP (glyphless_method))
6590 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6591 else
6592 {
6593 /* Invalid value. We use the default method. */
6594 glyphless_method = Qnil;
6595 goto retry;
6596 }
6597 it->what = IT_GLYPHLESS;
6598 return glyphless_method;
6599 }
6600
6601 /* Load IT's display element fields with information about the next
6602 display element from the current position of IT. Value is zero if
6603 end of buffer (or C string) is reached. */
6604
6605 static struct frame *last_escape_glyph_frame = NULL;
6606 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6607 static int last_escape_glyph_merged_face_id = 0;
6608
6609 struct frame *last_glyphless_glyph_frame = NULL;
6610 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6611 int last_glyphless_glyph_merged_face_id = 0;
6612
6613 static int
6614 get_next_display_element (struct it *it)
6615 {
6616 /* Non-zero means that we found a display element. Zero means that
6617 we hit the end of what we iterate over. Performance note: the
6618 function pointer `method' used here turns out to be faster than
6619 using a sequence of if-statements. */
6620 int success_p;
6621
6622 get_next:
6623 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6624
6625 if (it->what == IT_CHARACTER)
6626 {
6627 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6628 and only if (a) the resolved directionality of that character
6629 is R..." */
6630 /* FIXME: Do we need an exception for characters from display
6631 tables? */
6632 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6633 it->c = bidi_mirror_char (it->c);
6634 /* Map via display table or translate control characters.
6635 IT->c, IT->len etc. have been set to the next character by
6636 the function call above. If we have a display table, and it
6637 contains an entry for IT->c, translate it. Don't do this if
6638 IT->c itself comes from a display table, otherwise we could
6639 end up in an infinite recursion. (An alternative could be to
6640 count the recursion depth of this function and signal an
6641 error when a certain maximum depth is reached.) Is it worth
6642 it? */
6643 if (success_p && it->dpvec == NULL)
6644 {
6645 Lisp_Object dv;
6646 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6647 int nonascii_space_p = 0;
6648 int nonascii_hyphen_p = 0;
6649 int c = it->c; /* This is the character to display. */
6650
6651 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6652 {
6653 eassert (SINGLE_BYTE_CHAR_P (c));
6654 if (unibyte_display_via_language_environment)
6655 {
6656 c = DECODE_CHAR (unibyte, c);
6657 if (c < 0)
6658 c = BYTE8_TO_CHAR (it->c);
6659 }
6660 else
6661 c = BYTE8_TO_CHAR (it->c);
6662 }
6663
6664 if (it->dp
6665 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6666 VECTORP (dv)))
6667 {
6668 struct Lisp_Vector *v = XVECTOR (dv);
6669
6670 /* Return the first character from the display table
6671 entry, if not empty. If empty, don't display the
6672 current character. */
6673 if (v->header.size)
6674 {
6675 it->dpvec_char_len = it->len;
6676 it->dpvec = v->contents;
6677 it->dpend = v->contents + v->header.size;
6678 it->current.dpvec_index = 0;
6679 it->dpvec_face_id = -1;
6680 it->saved_face_id = it->face_id;
6681 it->method = GET_FROM_DISPLAY_VECTOR;
6682 it->ellipsis_p = 0;
6683 }
6684 else
6685 {
6686 set_iterator_to_next (it, 0);
6687 }
6688 goto get_next;
6689 }
6690
6691 if (! NILP (lookup_glyphless_char_display (c, it)))
6692 {
6693 if (it->what == IT_GLYPHLESS)
6694 goto done;
6695 /* Don't display this character. */
6696 set_iterator_to_next (it, 0);
6697 goto get_next;
6698 }
6699
6700 /* If `nobreak-char-display' is non-nil, we display
6701 non-ASCII spaces and hyphens specially. */
6702 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6703 {
6704 if (c == 0xA0)
6705 nonascii_space_p = 1;
6706 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6707 nonascii_hyphen_p = 1;
6708 }
6709
6710 /* Translate control characters into `\003' or `^C' form.
6711 Control characters coming from a display table entry are
6712 currently not translated because we use IT->dpvec to hold
6713 the translation. This could easily be changed but I
6714 don't believe that it is worth doing.
6715
6716 The characters handled by `nobreak-char-display' must be
6717 translated too.
6718
6719 Non-printable characters and raw-byte characters are also
6720 translated to octal form. */
6721 if (((c < ' ' || c == 127) /* ASCII control chars */
6722 ? (it->area != TEXT_AREA
6723 /* In mode line, treat \n, \t like other crl chars. */
6724 || (c != '\t'
6725 && it->glyph_row
6726 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6727 || (c != '\n' && c != '\t'))
6728 : (nonascii_space_p
6729 || nonascii_hyphen_p
6730 || CHAR_BYTE8_P (c)
6731 || ! CHAR_PRINTABLE_P (c))))
6732 {
6733 /* C is a control character, non-ASCII space/hyphen,
6734 raw-byte, or a non-printable character which must be
6735 displayed either as '\003' or as `^C' where the '\\'
6736 and '^' can be defined in the display table. Fill
6737 IT->ctl_chars with glyphs for what we have to
6738 display. Then, set IT->dpvec to these glyphs. */
6739 Lisp_Object gc;
6740 int ctl_len;
6741 int face_id;
6742 int lface_id = 0;
6743 int escape_glyph;
6744
6745 /* Handle control characters with ^. */
6746
6747 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6748 {
6749 int g;
6750
6751 g = '^'; /* default glyph for Control */
6752 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6753 if (it->dp
6754 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6755 {
6756 g = GLYPH_CODE_CHAR (gc);
6757 lface_id = GLYPH_CODE_FACE (gc);
6758 }
6759 if (lface_id)
6760 {
6761 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6762 }
6763 else if (it->f == last_escape_glyph_frame
6764 && it->face_id == last_escape_glyph_face_id)
6765 {
6766 face_id = last_escape_glyph_merged_face_id;
6767 }
6768 else
6769 {
6770 /* Merge the escape-glyph face into the current face. */
6771 face_id = merge_faces (it->f, Qescape_glyph, 0,
6772 it->face_id);
6773 last_escape_glyph_frame = it->f;
6774 last_escape_glyph_face_id = it->face_id;
6775 last_escape_glyph_merged_face_id = face_id;
6776 }
6777
6778 XSETINT (it->ctl_chars[0], g);
6779 XSETINT (it->ctl_chars[1], c ^ 0100);
6780 ctl_len = 2;
6781 goto display_control;
6782 }
6783
6784 /* Handle non-ascii space in the mode where it only gets
6785 highlighting. */
6786
6787 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6788 {
6789 /* Merge `nobreak-space' into the current face. */
6790 face_id = merge_faces (it->f, Qnobreak_space, 0,
6791 it->face_id);
6792 XSETINT (it->ctl_chars[0], ' ');
6793 ctl_len = 1;
6794 goto display_control;
6795 }
6796
6797 /* Handle sequences that start with the "escape glyph". */
6798
6799 /* the default escape glyph is \. */
6800 escape_glyph = '\\';
6801
6802 if (it->dp
6803 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6804 {
6805 escape_glyph = GLYPH_CODE_CHAR (gc);
6806 lface_id = GLYPH_CODE_FACE (gc);
6807 }
6808 if (lface_id)
6809 {
6810 /* The display table specified a face.
6811 Merge it into face_id and also into escape_glyph. */
6812 face_id = merge_faces (it->f, Qt, lface_id,
6813 it->face_id);
6814 }
6815 else if (it->f == last_escape_glyph_frame
6816 && it->face_id == last_escape_glyph_face_id)
6817 {
6818 face_id = last_escape_glyph_merged_face_id;
6819 }
6820 else
6821 {
6822 /* Merge the escape-glyph face into the current face. */
6823 face_id = merge_faces (it->f, Qescape_glyph, 0,
6824 it->face_id);
6825 last_escape_glyph_frame = it->f;
6826 last_escape_glyph_face_id = it->face_id;
6827 last_escape_glyph_merged_face_id = face_id;
6828 }
6829
6830 /* Draw non-ASCII hyphen with just highlighting: */
6831
6832 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6833 {
6834 XSETINT (it->ctl_chars[0], '-');
6835 ctl_len = 1;
6836 goto display_control;
6837 }
6838
6839 /* Draw non-ASCII space/hyphen with escape glyph: */
6840
6841 if (nonascii_space_p || nonascii_hyphen_p)
6842 {
6843 XSETINT (it->ctl_chars[0], escape_glyph);
6844 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6845 ctl_len = 2;
6846 goto display_control;
6847 }
6848
6849 {
6850 char str[10];
6851 int len, i;
6852
6853 if (CHAR_BYTE8_P (c))
6854 /* Display \200 instead of \17777600. */
6855 c = CHAR_TO_BYTE8 (c);
6856 len = sprintf (str, "%03o", c);
6857
6858 XSETINT (it->ctl_chars[0], escape_glyph);
6859 for (i = 0; i < len; i++)
6860 XSETINT (it->ctl_chars[i + 1], str[i]);
6861 ctl_len = len + 1;
6862 }
6863
6864 display_control:
6865 /* Set up IT->dpvec and return first character from it. */
6866 it->dpvec_char_len = it->len;
6867 it->dpvec = it->ctl_chars;
6868 it->dpend = it->dpvec + ctl_len;
6869 it->current.dpvec_index = 0;
6870 it->dpvec_face_id = face_id;
6871 it->saved_face_id = it->face_id;
6872 it->method = GET_FROM_DISPLAY_VECTOR;
6873 it->ellipsis_p = 0;
6874 goto get_next;
6875 }
6876 it->char_to_display = c;
6877 }
6878 else if (success_p)
6879 {
6880 it->char_to_display = it->c;
6881 }
6882 }
6883
6884 /* Adjust face id for a multibyte character. There are no multibyte
6885 character in unibyte text. */
6886 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6887 && it->multibyte_p
6888 && success_p
6889 && FRAME_WINDOW_P (it->f))
6890 {
6891 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6892
6893 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6894 {
6895 /* Automatic composition with glyph-string. */
6896 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6897
6898 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6899 }
6900 else
6901 {
6902 ptrdiff_t pos = (it->s ? -1
6903 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6904 : IT_CHARPOS (*it));
6905 int c;
6906
6907 if (it->what == IT_CHARACTER)
6908 c = it->char_to_display;
6909 else
6910 {
6911 struct composition *cmp = composition_table[it->cmp_it.id];
6912 int i;
6913
6914 c = ' ';
6915 for (i = 0; i < cmp->glyph_len; i++)
6916 /* TAB in a composition means display glyphs with
6917 padding space on the left or right. */
6918 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6919 break;
6920 }
6921 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6922 }
6923 }
6924
6925 done:
6926 /* Is this character the last one of a run of characters with
6927 box? If yes, set IT->end_of_box_run_p to 1. */
6928 if (it->face_box_p
6929 && it->s == NULL)
6930 {
6931 if (it->method == GET_FROM_STRING && it->sp)
6932 {
6933 int face_id = underlying_face_id (it);
6934 struct face *face = FACE_FROM_ID (it->f, face_id);
6935
6936 if (face)
6937 {
6938 if (face->box == FACE_NO_BOX)
6939 {
6940 /* If the box comes from face properties in a
6941 display string, check faces in that string. */
6942 int string_face_id = face_after_it_pos (it);
6943 it->end_of_box_run_p
6944 = (FACE_FROM_ID (it->f, string_face_id)->box
6945 == FACE_NO_BOX);
6946 }
6947 /* Otherwise, the box comes from the underlying face.
6948 If this is the last string character displayed, check
6949 the next buffer location. */
6950 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6951 && (it->current.overlay_string_index
6952 == it->n_overlay_strings - 1))
6953 {
6954 ptrdiff_t ignore;
6955 int next_face_id;
6956 struct text_pos pos = it->current.pos;
6957 INC_TEXT_POS (pos, it->multibyte_p);
6958
6959 next_face_id = face_at_buffer_position
6960 (it->w, CHARPOS (pos), it->region_beg_charpos,
6961 it->region_end_charpos, &ignore,
6962 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6963 -1);
6964 it->end_of_box_run_p
6965 = (FACE_FROM_ID (it->f, next_face_id)->box
6966 == FACE_NO_BOX);
6967 }
6968 }
6969 }
6970 else
6971 {
6972 int face_id = face_after_it_pos (it);
6973 it->end_of_box_run_p
6974 = (face_id != it->face_id
6975 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
6976 }
6977 }
6978 /* If we reached the end of the object we've been iterating (e.g., a
6979 display string or an overlay string), and there's something on
6980 IT->stack, proceed with what's on the stack. It doesn't make
6981 sense to return zero if there's unprocessed stuff on the stack,
6982 because otherwise that stuff will never be displayed. */
6983 if (!success_p && it->sp > 0)
6984 {
6985 set_iterator_to_next (it, 0);
6986 success_p = get_next_display_element (it);
6987 }
6988
6989 /* Value is 0 if end of buffer or string reached. */
6990 return success_p;
6991 }
6992
6993
6994 /* Move IT to the next display element.
6995
6996 RESEAT_P non-zero means if called on a newline in buffer text,
6997 skip to the next visible line start.
6998
6999 Functions get_next_display_element and set_iterator_to_next are
7000 separate because I find this arrangement easier to handle than a
7001 get_next_display_element function that also increments IT's
7002 position. The way it is we can first look at an iterator's current
7003 display element, decide whether it fits on a line, and if it does,
7004 increment the iterator position. The other way around we probably
7005 would either need a flag indicating whether the iterator has to be
7006 incremented the next time, or we would have to implement a
7007 decrement position function which would not be easy to write. */
7008
7009 void
7010 set_iterator_to_next (struct it *it, int reseat_p)
7011 {
7012 /* Reset flags indicating start and end of a sequence of characters
7013 with box. Reset them at the start of this function because
7014 moving the iterator to a new position might set them. */
7015 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7016
7017 switch (it->method)
7018 {
7019 case GET_FROM_BUFFER:
7020 /* The current display element of IT is a character from
7021 current_buffer. Advance in the buffer, and maybe skip over
7022 invisible lines that are so because of selective display. */
7023 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7024 reseat_at_next_visible_line_start (it, 0);
7025 else if (it->cmp_it.id >= 0)
7026 {
7027 /* We are currently getting glyphs from a composition. */
7028 int i;
7029
7030 if (! it->bidi_p)
7031 {
7032 IT_CHARPOS (*it) += it->cmp_it.nchars;
7033 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7034 if (it->cmp_it.to < it->cmp_it.nglyphs)
7035 {
7036 it->cmp_it.from = it->cmp_it.to;
7037 }
7038 else
7039 {
7040 it->cmp_it.id = -1;
7041 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7042 IT_BYTEPOS (*it),
7043 it->end_charpos, Qnil);
7044 }
7045 }
7046 else if (! it->cmp_it.reversed_p)
7047 {
7048 /* Composition created while scanning forward. */
7049 /* Update IT's char/byte positions to point to the first
7050 character of the next grapheme cluster, or to the
7051 character visually after the current composition. */
7052 for (i = 0; i < it->cmp_it.nchars; i++)
7053 bidi_move_to_visually_next (&it->bidi_it);
7054 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7055 IT_CHARPOS (*it) = it->bidi_it.charpos;
7056
7057 if (it->cmp_it.to < it->cmp_it.nglyphs)
7058 {
7059 /* Proceed to the next grapheme cluster. */
7060 it->cmp_it.from = it->cmp_it.to;
7061 }
7062 else
7063 {
7064 /* No more grapheme clusters in this composition.
7065 Find the next stop position. */
7066 ptrdiff_t stop = it->end_charpos;
7067 if (it->bidi_it.scan_dir < 0)
7068 /* Now we are scanning backward and don't know
7069 where to stop. */
7070 stop = -1;
7071 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7072 IT_BYTEPOS (*it), stop, Qnil);
7073 }
7074 }
7075 else
7076 {
7077 /* Composition created while scanning backward. */
7078 /* Update IT's char/byte positions to point to the last
7079 character of the previous grapheme cluster, or the
7080 character visually after the current composition. */
7081 for (i = 0; i < it->cmp_it.nchars; i++)
7082 bidi_move_to_visually_next (&it->bidi_it);
7083 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7084 IT_CHARPOS (*it) = it->bidi_it.charpos;
7085 if (it->cmp_it.from > 0)
7086 {
7087 /* Proceed to the previous grapheme cluster. */
7088 it->cmp_it.to = it->cmp_it.from;
7089 }
7090 else
7091 {
7092 /* No more grapheme clusters in this composition.
7093 Find the next stop position. */
7094 ptrdiff_t stop = it->end_charpos;
7095 if (it->bidi_it.scan_dir < 0)
7096 /* Now we are scanning backward and don't know
7097 where to stop. */
7098 stop = -1;
7099 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7100 IT_BYTEPOS (*it), stop, Qnil);
7101 }
7102 }
7103 }
7104 else
7105 {
7106 eassert (it->len != 0);
7107
7108 if (!it->bidi_p)
7109 {
7110 IT_BYTEPOS (*it) += it->len;
7111 IT_CHARPOS (*it) += 1;
7112 }
7113 else
7114 {
7115 int prev_scan_dir = it->bidi_it.scan_dir;
7116 /* If this is a new paragraph, determine its base
7117 direction (a.k.a. its base embedding level). */
7118 if (it->bidi_it.new_paragraph)
7119 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7120 bidi_move_to_visually_next (&it->bidi_it);
7121 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7122 IT_CHARPOS (*it) = it->bidi_it.charpos;
7123 if (prev_scan_dir != it->bidi_it.scan_dir)
7124 {
7125 /* As the scan direction was changed, we must
7126 re-compute the stop position for composition. */
7127 ptrdiff_t stop = it->end_charpos;
7128 if (it->bidi_it.scan_dir < 0)
7129 stop = -1;
7130 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7131 IT_BYTEPOS (*it), stop, Qnil);
7132 }
7133 }
7134 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7135 }
7136 break;
7137
7138 case GET_FROM_C_STRING:
7139 /* Current display element of IT is from a C string. */
7140 if (!it->bidi_p
7141 /* If the string position is beyond string's end, it means
7142 next_element_from_c_string is padding the string with
7143 blanks, in which case we bypass the bidi iterator,
7144 because it cannot deal with such virtual characters. */
7145 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7146 {
7147 IT_BYTEPOS (*it) += it->len;
7148 IT_CHARPOS (*it) += 1;
7149 }
7150 else
7151 {
7152 bidi_move_to_visually_next (&it->bidi_it);
7153 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7154 IT_CHARPOS (*it) = it->bidi_it.charpos;
7155 }
7156 break;
7157
7158 case GET_FROM_DISPLAY_VECTOR:
7159 /* Current display element of IT is from a display table entry.
7160 Advance in the display table definition. Reset it to null if
7161 end reached, and continue with characters from buffers/
7162 strings. */
7163 ++it->current.dpvec_index;
7164
7165 /* Restore face of the iterator to what they were before the
7166 display vector entry (these entries may contain faces). */
7167 it->face_id = it->saved_face_id;
7168
7169 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7170 {
7171 int recheck_faces = it->ellipsis_p;
7172
7173 if (it->s)
7174 it->method = GET_FROM_C_STRING;
7175 else if (STRINGP (it->string))
7176 it->method = GET_FROM_STRING;
7177 else
7178 {
7179 it->method = GET_FROM_BUFFER;
7180 it->object = it->w->buffer;
7181 }
7182
7183 it->dpvec = NULL;
7184 it->current.dpvec_index = -1;
7185
7186 /* Skip over characters which were displayed via IT->dpvec. */
7187 if (it->dpvec_char_len < 0)
7188 reseat_at_next_visible_line_start (it, 1);
7189 else if (it->dpvec_char_len > 0)
7190 {
7191 if (it->method == GET_FROM_STRING
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 {
7471 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7472 -1);
7473 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7474 }
7475 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7476 do
7477 {
7478 /* Now return to buffer/string position where we were asked
7479 to get the next display element, and produce that. */
7480 bidi_move_to_visually_next (&it->bidi_it);
7481 }
7482 while (it->bidi_it.bytepos != orig_bytepos
7483 && it->bidi_it.charpos < eob);
7484 }
7485
7486 /* Adjust IT's position information to where we ended up. */
7487 if (STRINGP (it->string))
7488 {
7489 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7490 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7491 }
7492 else
7493 {
7494 IT_CHARPOS (*it) = it->bidi_it.charpos;
7495 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7496 }
7497
7498 if (STRINGP (it->string) || !it->s)
7499 {
7500 ptrdiff_t stop, charpos, bytepos;
7501
7502 if (STRINGP (it->string))
7503 {
7504 eassert (!it->s);
7505 stop = SCHARS (it->string);
7506 if (stop > it->end_charpos)
7507 stop = it->end_charpos;
7508 charpos = IT_STRING_CHARPOS (*it);
7509 bytepos = IT_STRING_BYTEPOS (*it);
7510 }
7511 else
7512 {
7513 stop = it->end_charpos;
7514 charpos = IT_CHARPOS (*it);
7515 bytepos = IT_BYTEPOS (*it);
7516 }
7517 if (it->bidi_it.scan_dir < 0)
7518 stop = -1;
7519 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7520 it->string);
7521 }
7522 }
7523
7524 /* Load IT with the next display element from Lisp string IT->string.
7525 IT->current.string_pos is the current position within the string.
7526 If IT->current.overlay_string_index >= 0, the Lisp string is an
7527 overlay string. */
7528
7529 static int
7530 next_element_from_string (struct it *it)
7531 {
7532 struct text_pos position;
7533
7534 eassert (STRINGP (it->string));
7535 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7536 eassert (IT_STRING_CHARPOS (*it) >= 0);
7537 position = it->current.string_pos;
7538
7539 /* With bidi reordering, the character to display might not be the
7540 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7541 that we were reseat()ed to a new string, whose paragraph
7542 direction is not known. */
7543 if (it->bidi_p && it->bidi_it.first_elt)
7544 {
7545 get_visually_first_element (it);
7546 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7547 }
7548
7549 /* Time to check for invisible text? */
7550 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7551 {
7552 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7553 {
7554 if (!(!it->bidi_p
7555 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7556 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7557 {
7558 /* With bidi non-linear iteration, we could find
7559 ourselves far beyond the last computed stop_charpos,
7560 with several other stop positions in between that we
7561 missed. Scan them all now, in buffer's logical
7562 order, until we find and handle the last stop_charpos
7563 that precedes our current position. */
7564 handle_stop_backwards (it, it->stop_charpos);
7565 return GET_NEXT_DISPLAY_ELEMENT (it);
7566 }
7567 else
7568 {
7569 if (it->bidi_p)
7570 {
7571 /* Take note of the stop position we just moved
7572 across, for when we will move back across it. */
7573 it->prev_stop = it->stop_charpos;
7574 /* If we are at base paragraph embedding level, take
7575 note of the last stop position seen at this
7576 level. */
7577 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7578 it->base_level_stop = it->stop_charpos;
7579 }
7580 handle_stop (it);
7581
7582 /* Since a handler may have changed IT->method, we must
7583 recurse here. */
7584 return GET_NEXT_DISPLAY_ELEMENT (it);
7585 }
7586 }
7587 else if (it->bidi_p
7588 /* If we are before prev_stop, we may have overstepped
7589 on our way backwards a stop_pos, and if so, we need
7590 to handle that stop_pos. */
7591 && IT_STRING_CHARPOS (*it) < it->prev_stop
7592 /* We can sometimes back up for reasons that have nothing
7593 to do with bidi reordering. E.g., compositions. The
7594 code below is only needed when we are above the base
7595 embedding level, so test for that explicitly. */
7596 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7597 {
7598 /* If we lost track of base_level_stop, we have no better
7599 place for handle_stop_backwards to start from than string
7600 beginning. This happens, e.g., when we were reseated to
7601 the previous screenful of text by vertical-motion. */
7602 if (it->base_level_stop <= 0
7603 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7604 it->base_level_stop = 0;
7605 handle_stop_backwards (it, it->base_level_stop);
7606 return GET_NEXT_DISPLAY_ELEMENT (it);
7607 }
7608 }
7609
7610 if (it->current.overlay_string_index >= 0)
7611 {
7612 /* Get the next character from an overlay string. In overlay
7613 strings, there is no field width or padding with spaces to
7614 do. */
7615 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7616 {
7617 it->what = IT_EOB;
7618 return 0;
7619 }
7620 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7621 IT_STRING_BYTEPOS (*it),
7622 it->bidi_it.scan_dir < 0
7623 ? -1
7624 : SCHARS (it->string))
7625 && next_element_from_composition (it))
7626 {
7627 return 1;
7628 }
7629 else if (STRING_MULTIBYTE (it->string))
7630 {
7631 const unsigned char *s = (SDATA (it->string)
7632 + IT_STRING_BYTEPOS (*it));
7633 it->c = string_char_and_length (s, &it->len);
7634 }
7635 else
7636 {
7637 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7638 it->len = 1;
7639 }
7640 }
7641 else
7642 {
7643 /* Get the next character from a Lisp string that is not an
7644 overlay string. Such strings come from the mode line, for
7645 example. We may have to pad with spaces, or truncate the
7646 string. See also next_element_from_c_string. */
7647 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7648 {
7649 it->what = IT_EOB;
7650 return 0;
7651 }
7652 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7653 {
7654 /* Pad with spaces. */
7655 it->c = ' ', it->len = 1;
7656 CHARPOS (position) = BYTEPOS (position) = -1;
7657 }
7658 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7659 IT_STRING_BYTEPOS (*it),
7660 it->bidi_it.scan_dir < 0
7661 ? -1
7662 : it->string_nchars)
7663 && next_element_from_composition (it))
7664 {
7665 return 1;
7666 }
7667 else if (STRING_MULTIBYTE (it->string))
7668 {
7669 const unsigned char *s = (SDATA (it->string)
7670 + IT_STRING_BYTEPOS (*it));
7671 it->c = string_char_and_length (s, &it->len);
7672 }
7673 else
7674 {
7675 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7676 it->len = 1;
7677 }
7678 }
7679
7680 /* Record what we have and where it came from. */
7681 it->what = IT_CHARACTER;
7682 it->object = it->string;
7683 it->position = position;
7684 return 1;
7685 }
7686
7687
7688 /* Load IT with next display element from C string IT->s.
7689 IT->string_nchars is the maximum number of characters to return
7690 from the string. IT->end_charpos may be greater than
7691 IT->string_nchars when this function is called, in which case we
7692 may have to return padding spaces. Value is zero if end of string
7693 reached, including padding spaces. */
7694
7695 static int
7696 next_element_from_c_string (struct it *it)
7697 {
7698 int success_p = 1;
7699
7700 eassert (it->s);
7701 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7702 it->what = IT_CHARACTER;
7703 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7704 it->object = Qnil;
7705
7706 /* With bidi reordering, the character to display might not be the
7707 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7708 we were reseated to a new string, whose paragraph direction is
7709 not known. */
7710 if (it->bidi_p && it->bidi_it.first_elt)
7711 get_visually_first_element (it);
7712
7713 /* IT's position can be greater than IT->string_nchars in case a
7714 field width or precision has been specified when the iterator was
7715 initialized. */
7716 if (IT_CHARPOS (*it) >= it->end_charpos)
7717 {
7718 /* End of the game. */
7719 it->what = IT_EOB;
7720 success_p = 0;
7721 }
7722 else if (IT_CHARPOS (*it) >= it->string_nchars)
7723 {
7724 /* Pad with spaces. */
7725 it->c = ' ', it->len = 1;
7726 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7727 }
7728 else if (it->multibyte_p)
7729 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7730 else
7731 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7732
7733 return success_p;
7734 }
7735
7736
7737 /* Set up IT to return characters from an ellipsis, if appropriate.
7738 The definition of the ellipsis glyphs may come from a display table
7739 entry. This function fills IT with the first glyph from the
7740 ellipsis if an ellipsis is to be displayed. */
7741
7742 static int
7743 next_element_from_ellipsis (struct it *it)
7744 {
7745 if (it->selective_display_ellipsis_p)
7746 setup_for_ellipsis (it, it->len);
7747 else
7748 {
7749 /* The face at the current position may be different from the
7750 face we find after the invisible text. Remember what it
7751 was in IT->saved_face_id, and signal that it's there by
7752 setting face_before_selective_p. */
7753 it->saved_face_id = it->face_id;
7754 it->method = GET_FROM_BUFFER;
7755 it->object = it->w->buffer;
7756 reseat_at_next_visible_line_start (it, 1);
7757 it->face_before_selective_p = 1;
7758 }
7759
7760 return GET_NEXT_DISPLAY_ELEMENT (it);
7761 }
7762
7763
7764 /* Deliver an image display element. The iterator IT is already
7765 filled with image information (done in handle_display_prop). Value
7766 is always 1. */
7767
7768
7769 static int
7770 next_element_from_image (struct it *it)
7771 {
7772 it->what = IT_IMAGE;
7773 it->ignore_overlay_strings_at_pos_p = 0;
7774 return 1;
7775 }
7776
7777
7778 /* Fill iterator IT with next display element from a stretch glyph
7779 property. IT->object is the value of the text property. Value is
7780 always 1. */
7781
7782 static int
7783 next_element_from_stretch (struct it *it)
7784 {
7785 it->what = IT_STRETCH;
7786 return 1;
7787 }
7788
7789 /* Scan backwards from IT's current position until we find a stop
7790 position, or until BEGV. This is called when we find ourself
7791 before both the last known prev_stop and base_level_stop while
7792 reordering bidirectional text. */
7793
7794 static void
7795 compute_stop_pos_backwards (struct it *it)
7796 {
7797 const int SCAN_BACK_LIMIT = 1000;
7798 struct text_pos pos;
7799 struct display_pos save_current = it->current;
7800 struct text_pos save_position = it->position;
7801 ptrdiff_t charpos = IT_CHARPOS (*it);
7802 ptrdiff_t where_we_are = charpos;
7803 ptrdiff_t save_stop_pos = it->stop_charpos;
7804 ptrdiff_t save_end_pos = it->end_charpos;
7805
7806 eassert (NILP (it->string) && !it->s);
7807 eassert (it->bidi_p);
7808 it->bidi_p = 0;
7809 do
7810 {
7811 it->end_charpos = min (charpos + 1, ZV);
7812 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7813 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7814 reseat_1 (it, pos, 0);
7815 compute_stop_pos (it);
7816 /* We must advance forward, right? */
7817 if (it->stop_charpos <= charpos)
7818 emacs_abort ();
7819 }
7820 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7821
7822 if (it->stop_charpos <= where_we_are)
7823 it->prev_stop = it->stop_charpos;
7824 else
7825 it->prev_stop = BEGV;
7826 it->bidi_p = 1;
7827 it->current = save_current;
7828 it->position = save_position;
7829 it->stop_charpos = save_stop_pos;
7830 it->end_charpos = save_end_pos;
7831 }
7832
7833 /* Scan forward from CHARPOS in the current buffer/string, until we
7834 find a stop position > current IT's position. Then handle the stop
7835 position before that. This is called when we bump into a stop
7836 position while reordering bidirectional text. CHARPOS should be
7837 the last previously processed stop_pos (or BEGV/0, if none were
7838 processed yet) whose position is less that IT's current
7839 position. */
7840
7841 static void
7842 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7843 {
7844 int bufp = !STRINGP (it->string);
7845 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7846 struct display_pos save_current = it->current;
7847 struct text_pos save_position = it->position;
7848 struct text_pos pos1;
7849 ptrdiff_t next_stop;
7850
7851 /* Scan in strict logical order. */
7852 eassert (it->bidi_p);
7853 it->bidi_p = 0;
7854 do
7855 {
7856 it->prev_stop = charpos;
7857 if (bufp)
7858 {
7859 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7860 reseat_1 (it, pos1, 0);
7861 }
7862 else
7863 it->current.string_pos = string_pos (charpos, it->string);
7864 compute_stop_pos (it);
7865 /* We must advance forward, right? */
7866 if (it->stop_charpos <= it->prev_stop)
7867 emacs_abort ();
7868 charpos = it->stop_charpos;
7869 }
7870 while (charpos <= where_we_are);
7871
7872 it->bidi_p = 1;
7873 it->current = save_current;
7874 it->position = save_position;
7875 next_stop = it->stop_charpos;
7876 it->stop_charpos = it->prev_stop;
7877 handle_stop (it);
7878 it->stop_charpos = next_stop;
7879 }
7880
7881 /* Load IT with the next display element from current_buffer. Value
7882 is zero if end of buffer reached. IT->stop_charpos is the next
7883 position at which to stop and check for text properties or buffer
7884 end. */
7885
7886 static int
7887 next_element_from_buffer (struct it *it)
7888 {
7889 int success_p = 1;
7890
7891 eassert (IT_CHARPOS (*it) >= BEGV);
7892 eassert (NILP (it->string) && !it->s);
7893 eassert (!it->bidi_p
7894 || (EQ (it->bidi_it.string.lstring, Qnil)
7895 && it->bidi_it.string.s == NULL));
7896
7897 /* With bidi reordering, the character to display might not be the
7898 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7899 we were reseat()ed to a new buffer position, which is potentially
7900 a different paragraph. */
7901 if (it->bidi_p && it->bidi_it.first_elt)
7902 {
7903 get_visually_first_element (it);
7904 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7905 }
7906
7907 if (IT_CHARPOS (*it) >= it->stop_charpos)
7908 {
7909 if (IT_CHARPOS (*it) >= it->end_charpos)
7910 {
7911 int overlay_strings_follow_p;
7912
7913 /* End of the game, except when overlay strings follow that
7914 haven't been returned yet. */
7915 if (it->overlay_strings_at_end_processed_p)
7916 overlay_strings_follow_p = 0;
7917 else
7918 {
7919 it->overlay_strings_at_end_processed_p = 1;
7920 overlay_strings_follow_p = get_overlay_strings (it, 0);
7921 }
7922
7923 if (overlay_strings_follow_p)
7924 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7925 else
7926 {
7927 it->what = IT_EOB;
7928 it->position = it->current.pos;
7929 success_p = 0;
7930 }
7931 }
7932 else if (!(!it->bidi_p
7933 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7934 || IT_CHARPOS (*it) == it->stop_charpos))
7935 {
7936 /* With bidi non-linear iteration, we could find ourselves
7937 far beyond the last computed stop_charpos, with several
7938 other stop positions in between that we missed. Scan
7939 them all now, in buffer's logical order, until we find
7940 and handle the last stop_charpos that precedes our
7941 current position. */
7942 handle_stop_backwards (it, it->stop_charpos);
7943 return GET_NEXT_DISPLAY_ELEMENT (it);
7944 }
7945 else
7946 {
7947 if (it->bidi_p)
7948 {
7949 /* Take note of the stop position we just moved across,
7950 for when we will move back across it. */
7951 it->prev_stop = it->stop_charpos;
7952 /* If we are at base paragraph embedding level, take
7953 note of the last stop position seen at this
7954 level. */
7955 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7956 it->base_level_stop = it->stop_charpos;
7957 }
7958 handle_stop (it);
7959 return GET_NEXT_DISPLAY_ELEMENT (it);
7960 }
7961 }
7962 else if (it->bidi_p
7963 /* If we are before prev_stop, we may have overstepped on
7964 our way backwards a stop_pos, and if so, we need to
7965 handle that stop_pos. */
7966 && IT_CHARPOS (*it) < it->prev_stop
7967 /* We can sometimes back up for reasons that have nothing
7968 to do with bidi reordering. E.g., compositions. The
7969 code below is only needed when we are above the base
7970 embedding level, so test for that explicitly. */
7971 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7972 {
7973 if (it->base_level_stop <= 0
7974 || IT_CHARPOS (*it) < it->base_level_stop)
7975 {
7976 /* If we lost track of base_level_stop, we need to find
7977 prev_stop by looking backwards. This happens, e.g., when
7978 we were reseated to the previous screenful of text by
7979 vertical-motion. */
7980 it->base_level_stop = BEGV;
7981 compute_stop_pos_backwards (it);
7982 handle_stop_backwards (it, it->prev_stop);
7983 }
7984 else
7985 handle_stop_backwards (it, it->base_level_stop);
7986 return GET_NEXT_DISPLAY_ELEMENT (it);
7987 }
7988 else
7989 {
7990 /* No face changes, overlays etc. in sight, so just return a
7991 character from current_buffer. */
7992 unsigned char *p;
7993 ptrdiff_t stop;
7994
7995 /* Maybe run the redisplay end trigger hook. Performance note:
7996 This doesn't seem to cost measurable time. */
7997 if (it->redisplay_end_trigger_charpos
7998 && it->glyph_row
7999 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8000 run_redisplay_end_trigger_hook (it);
8001
8002 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8003 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8004 stop)
8005 && next_element_from_composition (it))
8006 {
8007 return 1;
8008 }
8009
8010 /* Get the next character, maybe multibyte. */
8011 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8012 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8013 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8014 else
8015 it->c = *p, it->len = 1;
8016
8017 /* Record what we have and where it came from. */
8018 it->what = IT_CHARACTER;
8019 it->object = it->w->buffer;
8020 it->position = it->current.pos;
8021
8022 /* Normally we return the character found above, except when we
8023 really want to return an ellipsis for selective display. */
8024 if (it->selective)
8025 {
8026 if (it->c == '\n')
8027 {
8028 /* A value of selective > 0 means hide lines indented more
8029 than that number of columns. */
8030 if (it->selective > 0
8031 && IT_CHARPOS (*it) + 1 < ZV
8032 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8033 IT_BYTEPOS (*it) + 1,
8034 it->selective))
8035 {
8036 success_p = next_element_from_ellipsis (it);
8037 it->dpvec_char_len = -1;
8038 }
8039 }
8040 else if (it->c == '\r' && it->selective == -1)
8041 {
8042 /* A value of selective == -1 means that everything from the
8043 CR to the end of the line is invisible, with maybe an
8044 ellipsis displayed for it. */
8045 success_p = next_element_from_ellipsis (it);
8046 it->dpvec_char_len = -1;
8047 }
8048 }
8049 }
8050
8051 /* Value is zero if end of buffer reached. */
8052 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8053 return success_p;
8054 }
8055
8056
8057 /* Run the redisplay end trigger hook for IT. */
8058
8059 static void
8060 run_redisplay_end_trigger_hook (struct it *it)
8061 {
8062 Lisp_Object args[3];
8063
8064 /* IT->glyph_row should be non-null, i.e. we should be actually
8065 displaying something, or otherwise we should not run the hook. */
8066 eassert (it->glyph_row);
8067
8068 /* Set up hook arguments. */
8069 args[0] = Qredisplay_end_trigger_functions;
8070 args[1] = it->window;
8071 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8072 it->redisplay_end_trigger_charpos = 0;
8073
8074 /* Since we are *trying* to run these functions, don't try to run
8075 them again, even if they get an error. */
8076 wset_redisplay_end_trigger (it->w, Qnil);
8077 Frun_hook_with_args (3, args);
8078
8079 /* Notice if it changed the face of the character we are on. */
8080 handle_face_prop (it);
8081 }
8082
8083
8084 /* Deliver a composition display element. Unlike the other
8085 next_element_from_XXX, this function is not registered in the array
8086 get_next_element[]. It is called from next_element_from_buffer and
8087 next_element_from_string when necessary. */
8088
8089 static int
8090 next_element_from_composition (struct it *it)
8091 {
8092 it->what = IT_COMPOSITION;
8093 it->len = it->cmp_it.nbytes;
8094 if (STRINGP (it->string))
8095 {
8096 if (it->c < 0)
8097 {
8098 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8099 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8100 return 0;
8101 }
8102 it->position = it->current.string_pos;
8103 it->object = it->string;
8104 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8105 IT_STRING_BYTEPOS (*it), it->string);
8106 }
8107 else
8108 {
8109 if (it->c < 0)
8110 {
8111 IT_CHARPOS (*it) += it->cmp_it.nchars;
8112 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8113 if (it->bidi_p)
8114 {
8115 if (it->bidi_it.new_paragraph)
8116 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8117 /* Resync the bidi iterator with IT's new position.
8118 FIXME: this doesn't support bidirectional text. */
8119 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8120 bidi_move_to_visually_next (&it->bidi_it);
8121 }
8122 return 0;
8123 }
8124 it->position = it->current.pos;
8125 it->object = it->w->buffer;
8126 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8127 IT_BYTEPOS (*it), Qnil);
8128 }
8129 return 1;
8130 }
8131
8132
8133 \f
8134 /***********************************************************************
8135 Moving an iterator without producing glyphs
8136 ***********************************************************************/
8137
8138 /* Check if iterator is at a position corresponding to a valid buffer
8139 position after some move_it_ call. */
8140
8141 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8142 ((it)->method == GET_FROM_STRING \
8143 ? IT_STRING_CHARPOS (*it) == 0 \
8144 : 1)
8145
8146
8147 /* Move iterator IT to a specified buffer or X position within one
8148 line on the display without producing glyphs.
8149
8150 OP should be a bit mask including some or all of these bits:
8151 MOVE_TO_X: Stop upon reaching x-position TO_X.
8152 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8153 Regardless of OP's value, stop upon reaching the end of the display line.
8154
8155 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8156 This means, in particular, that TO_X includes window's horizontal
8157 scroll amount.
8158
8159 The return value has several possible values that
8160 say what condition caused the scan to stop:
8161
8162 MOVE_POS_MATCH_OR_ZV
8163 - when TO_POS or ZV was reached.
8164
8165 MOVE_X_REACHED
8166 -when TO_X was reached before TO_POS or ZV were reached.
8167
8168 MOVE_LINE_CONTINUED
8169 - when we reached the end of the display area and the line must
8170 be continued.
8171
8172 MOVE_LINE_TRUNCATED
8173 - when we reached the end of the display area and the line is
8174 truncated.
8175
8176 MOVE_NEWLINE_OR_CR
8177 - when we stopped at a line end, i.e. a newline or a CR and selective
8178 display is on. */
8179
8180 static enum move_it_result
8181 move_it_in_display_line_to (struct it *it,
8182 ptrdiff_t to_charpos, int to_x,
8183 enum move_operation_enum op)
8184 {
8185 enum move_it_result result = MOVE_UNDEFINED;
8186 struct glyph_row *saved_glyph_row;
8187 struct it wrap_it, atpos_it, atx_it, ppos_it;
8188 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8189 void *ppos_data = NULL;
8190 int may_wrap = 0;
8191 enum it_method prev_method = it->method;
8192 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8193 int saw_smaller_pos = prev_pos < to_charpos;
8194
8195 /* Don't produce glyphs in produce_glyphs. */
8196 saved_glyph_row = it->glyph_row;
8197 it->glyph_row = NULL;
8198
8199 /* Use wrap_it to save a copy of IT wherever a word wrap could
8200 occur. Use atpos_it to save a copy of IT at the desired buffer
8201 position, if found, so that we can scan ahead and check if the
8202 word later overshoots the window edge. Use atx_it similarly, for
8203 pixel positions. */
8204 wrap_it.sp = -1;
8205 atpos_it.sp = -1;
8206 atx_it.sp = -1;
8207
8208 /* Use ppos_it under bidi reordering to save a copy of IT for the
8209 position > CHARPOS that is the closest to CHARPOS. We restore
8210 that position in IT when we have scanned the entire display line
8211 without finding a match for CHARPOS and all the character
8212 positions are greater than CHARPOS. */
8213 if (it->bidi_p)
8214 {
8215 SAVE_IT (ppos_it, *it, ppos_data);
8216 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8217 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8218 SAVE_IT (ppos_it, *it, ppos_data);
8219 }
8220
8221 #define BUFFER_POS_REACHED_P() \
8222 ((op & MOVE_TO_POS) != 0 \
8223 && BUFFERP (it->object) \
8224 && (IT_CHARPOS (*it) == to_charpos \
8225 || ((!it->bidi_p \
8226 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8227 && IT_CHARPOS (*it) > to_charpos) \
8228 || (it->what == IT_COMPOSITION \
8229 && ((IT_CHARPOS (*it) > to_charpos \
8230 && to_charpos >= it->cmp_it.charpos) \
8231 || (IT_CHARPOS (*it) < to_charpos \
8232 && to_charpos <= it->cmp_it.charpos)))) \
8233 && (it->method == GET_FROM_BUFFER \
8234 || (it->method == GET_FROM_DISPLAY_VECTOR \
8235 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8236
8237 /* If there's a line-/wrap-prefix, handle it. */
8238 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8239 && it->current_y < it->last_visible_y)
8240 handle_line_prefix (it);
8241
8242 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8243 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8244
8245 while (1)
8246 {
8247 int x, i, ascent = 0, descent = 0;
8248
8249 /* Utility macro to reset an iterator with x, ascent, and descent. */
8250 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8251 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8252 (IT)->max_descent = descent)
8253
8254 /* Stop if we move beyond TO_CHARPOS (after an image or a
8255 display string or stretch glyph). */
8256 if ((op & MOVE_TO_POS) != 0
8257 && BUFFERP (it->object)
8258 && it->method == GET_FROM_BUFFER
8259 && (((!it->bidi_p
8260 /* When the iterator is at base embedding level, we
8261 are guaranteed that characters are delivered for
8262 display in strictly increasing order of their
8263 buffer positions. */
8264 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8265 && IT_CHARPOS (*it) > to_charpos)
8266 || (it->bidi_p
8267 && (prev_method == GET_FROM_IMAGE
8268 || prev_method == GET_FROM_STRETCH
8269 || prev_method == GET_FROM_STRING)
8270 /* Passed TO_CHARPOS from left to right. */
8271 && ((prev_pos < to_charpos
8272 && IT_CHARPOS (*it) > to_charpos)
8273 /* Passed TO_CHARPOS from right to left. */
8274 || (prev_pos > to_charpos
8275 && IT_CHARPOS (*it) < to_charpos)))))
8276 {
8277 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8278 {
8279 result = MOVE_POS_MATCH_OR_ZV;
8280 break;
8281 }
8282 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8283 /* If wrap_it is valid, the current position might be in a
8284 word that is wrapped. So, save the iterator in
8285 atpos_it and continue to see if wrapping happens. */
8286 SAVE_IT (atpos_it, *it, atpos_data);
8287 }
8288
8289 /* Stop when ZV reached.
8290 We used to stop here when TO_CHARPOS reached as well, but that is
8291 too soon if this glyph does not fit on this line. So we handle it
8292 explicitly below. */
8293 if (!get_next_display_element (it))
8294 {
8295 result = MOVE_POS_MATCH_OR_ZV;
8296 break;
8297 }
8298
8299 if (it->line_wrap == TRUNCATE)
8300 {
8301 if (BUFFER_POS_REACHED_P ())
8302 {
8303 result = MOVE_POS_MATCH_OR_ZV;
8304 break;
8305 }
8306 }
8307 else
8308 {
8309 if (it->line_wrap == WORD_WRAP)
8310 {
8311 if (IT_DISPLAYING_WHITESPACE (it))
8312 may_wrap = 1;
8313 else if (may_wrap)
8314 {
8315 /* We have reached a glyph that follows one or more
8316 whitespace characters. If the position is
8317 already found, we are done. */
8318 if (atpos_it.sp >= 0)
8319 {
8320 RESTORE_IT (it, &atpos_it, atpos_data);
8321 result = MOVE_POS_MATCH_OR_ZV;
8322 goto done;
8323 }
8324 if (atx_it.sp >= 0)
8325 {
8326 RESTORE_IT (it, &atx_it, atx_data);
8327 result = MOVE_X_REACHED;
8328 goto done;
8329 }
8330 /* Otherwise, we can wrap here. */
8331 SAVE_IT (wrap_it, *it, wrap_data);
8332 may_wrap = 0;
8333 }
8334 }
8335 }
8336
8337 /* Remember the line height for the current line, in case
8338 the next element doesn't fit on the line. */
8339 ascent = it->max_ascent;
8340 descent = it->max_descent;
8341
8342 /* The call to produce_glyphs will get the metrics of the
8343 display element IT is loaded with. Record the x-position
8344 before this display element, in case it doesn't fit on the
8345 line. */
8346 x = it->current_x;
8347
8348 PRODUCE_GLYPHS (it);
8349
8350 if (it->area != TEXT_AREA)
8351 {
8352 prev_method = it->method;
8353 if (it->method == GET_FROM_BUFFER)
8354 prev_pos = IT_CHARPOS (*it);
8355 set_iterator_to_next (it, 1);
8356 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8357 SET_TEXT_POS (this_line_min_pos,
8358 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8359 if (it->bidi_p
8360 && (op & MOVE_TO_POS)
8361 && IT_CHARPOS (*it) > to_charpos
8362 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8363 SAVE_IT (ppos_it, *it, ppos_data);
8364 continue;
8365 }
8366
8367 /* The number of glyphs we get back in IT->nglyphs will normally
8368 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8369 character on a terminal frame, or (iii) a line end. For the
8370 second case, IT->nglyphs - 1 padding glyphs will be present.
8371 (On X frames, there is only one glyph produced for a
8372 composite character.)
8373
8374 The behavior implemented below means, for continuation lines,
8375 that as many spaces of a TAB as fit on the current line are
8376 displayed there. For terminal frames, as many glyphs of a
8377 multi-glyph character are displayed in the current line, too.
8378 This is what the old redisplay code did, and we keep it that
8379 way. Under X, the whole shape of a complex character must
8380 fit on the line or it will be completely displayed in the
8381 next line.
8382
8383 Note that both for tabs and padding glyphs, all glyphs have
8384 the same width. */
8385 if (it->nglyphs)
8386 {
8387 /* More than one glyph or glyph doesn't fit on line. All
8388 glyphs have the same width. */
8389 int single_glyph_width = it->pixel_width / it->nglyphs;
8390 int new_x;
8391 int x_before_this_char = x;
8392 int hpos_before_this_char = it->hpos;
8393
8394 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8395 {
8396 new_x = x + single_glyph_width;
8397
8398 /* We want to leave anything reaching TO_X to the caller. */
8399 if ((op & MOVE_TO_X) && new_x > to_x)
8400 {
8401 if (BUFFER_POS_REACHED_P ())
8402 {
8403 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8404 goto buffer_pos_reached;
8405 if (atpos_it.sp < 0)
8406 {
8407 SAVE_IT (atpos_it, *it, atpos_data);
8408 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8409 }
8410 }
8411 else
8412 {
8413 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8414 {
8415 it->current_x = x;
8416 result = MOVE_X_REACHED;
8417 break;
8418 }
8419 if (atx_it.sp < 0)
8420 {
8421 SAVE_IT (atx_it, *it, atx_data);
8422 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8423 }
8424 }
8425 }
8426
8427 if (/* Lines are continued. */
8428 it->line_wrap != TRUNCATE
8429 && (/* And glyph doesn't fit on the line. */
8430 new_x > it->last_visible_x
8431 /* Or it fits exactly and we're on a window
8432 system frame. */
8433 || (new_x == it->last_visible_x
8434 && FRAME_WINDOW_P (it->f)
8435 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8436 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8437 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8438 {
8439 if (/* IT->hpos == 0 means the very first glyph
8440 doesn't fit on the line, e.g. a wide image. */
8441 it->hpos == 0
8442 || (new_x == it->last_visible_x
8443 && FRAME_WINDOW_P (it->f)))
8444 {
8445 ++it->hpos;
8446 it->current_x = new_x;
8447
8448 /* The character's last glyph just barely fits
8449 in this row. */
8450 if (i == it->nglyphs - 1)
8451 {
8452 /* If this is the destination position,
8453 return a position *before* it in this row,
8454 now that we know it fits in this row. */
8455 if (BUFFER_POS_REACHED_P ())
8456 {
8457 if (it->line_wrap != WORD_WRAP
8458 || wrap_it.sp < 0)
8459 {
8460 it->hpos = hpos_before_this_char;
8461 it->current_x = x_before_this_char;
8462 result = MOVE_POS_MATCH_OR_ZV;
8463 break;
8464 }
8465 if (it->line_wrap == WORD_WRAP
8466 && atpos_it.sp < 0)
8467 {
8468 SAVE_IT (atpos_it, *it, atpos_data);
8469 atpos_it.current_x = x_before_this_char;
8470 atpos_it.hpos = hpos_before_this_char;
8471 }
8472 }
8473
8474 prev_method = it->method;
8475 if (it->method == GET_FROM_BUFFER)
8476 prev_pos = IT_CHARPOS (*it);
8477 set_iterator_to_next (it, 1);
8478 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8479 SET_TEXT_POS (this_line_min_pos,
8480 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8481 /* On graphical terminals, newlines may
8482 "overflow" into the fringe if
8483 overflow-newline-into-fringe is non-nil.
8484 On text terminals, and on graphical
8485 terminals with no right margin, newlines
8486 may overflow into the last glyph on the
8487 display line.*/
8488 if (!FRAME_WINDOW_P (it->f)
8489 || ((it->bidi_p
8490 && it->bidi_it.paragraph_dir == R2L)
8491 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8492 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8493 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8494 {
8495 if (!get_next_display_element (it))
8496 {
8497 result = MOVE_POS_MATCH_OR_ZV;
8498 break;
8499 }
8500 if (BUFFER_POS_REACHED_P ())
8501 {
8502 if (ITERATOR_AT_END_OF_LINE_P (it))
8503 result = MOVE_POS_MATCH_OR_ZV;
8504 else
8505 result = MOVE_LINE_CONTINUED;
8506 break;
8507 }
8508 if (ITERATOR_AT_END_OF_LINE_P (it))
8509 {
8510 result = MOVE_NEWLINE_OR_CR;
8511 break;
8512 }
8513 }
8514 }
8515 }
8516 else
8517 IT_RESET_X_ASCENT_DESCENT (it);
8518
8519 if (wrap_it.sp >= 0)
8520 {
8521 RESTORE_IT (it, &wrap_it, wrap_data);
8522 atpos_it.sp = -1;
8523 atx_it.sp = -1;
8524 }
8525
8526 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8527 IT_CHARPOS (*it)));
8528 result = MOVE_LINE_CONTINUED;
8529 break;
8530 }
8531
8532 if (BUFFER_POS_REACHED_P ())
8533 {
8534 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8535 goto buffer_pos_reached;
8536 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8537 {
8538 SAVE_IT (atpos_it, *it, atpos_data);
8539 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8540 }
8541 }
8542
8543 if (new_x > it->first_visible_x)
8544 {
8545 /* Glyph is visible. Increment number of glyphs that
8546 would be displayed. */
8547 ++it->hpos;
8548 }
8549 }
8550
8551 if (result != MOVE_UNDEFINED)
8552 break;
8553 }
8554 else if (BUFFER_POS_REACHED_P ())
8555 {
8556 buffer_pos_reached:
8557 IT_RESET_X_ASCENT_DESCENT (it);
8558 result = MOVE_POS_MATCH_OR_ZV;
8559 break;
8560 }
8561 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8562 {
8563 /* Stop when TO_X specified and reached. This check is
8564 necessary here because of lines consisting of a line end,
8565 only. The line end will not produce any glyphs and we
8566 would never get MOVE_X_REACHED. */
8567 eassert (it->nglyphs == 0);
8568 result = MOVE_X_REACHED;
8569 break;
8570 }
8571
8572 /* Is this a line end? If yes, we're done. */
8573 if (ITERATOR_AT_END_OF_LINE_P (it))
8574 {
8575 /* If we are past TO_CHARPOS, but never saw any character
8576 positions smaller than TO_CHARPOS, return
8577 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8578 did. */
8579 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8580 {
8581 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8582 {
8583 if (IT_CHARPOS (ppos_it) < ZV)
8584 {
8585 RESTORE_IT (it, &ppos_it, ppos_data);
8586 result = MOVE_POS_MATCH_OR_ZV;
8587 }
8588 else
8589 goto buffer_pos_reached;
8590 }
8591 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8592 && IT_CHARPOS (*it) > to_charpos)
8593 goto buffer_pos_reached;
8594 else
8595 result = MOVE_NEWLINE_OR_CR;
8596 }
8597 else
8598 result = MOVE_NEWLINE_OR_CR;
8599 break;
8600 }
8601
8602 prev_method = it->method;
8603 if (it->method == GET_FROM_BUFFER)
8604 prev_pos = IT_CHARPOS (*it);
8605 /* The current display element has been consumed. Advance
8606 to the next. */
8607 set_iterator_to_next (it, 1);
8608 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8609 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8610 if (IT_CHARPOS (*it) < to_charpos)
8611 saw_smaller_pos = 1;
8612 if (it->bidi_p
8613 && (op & MOVE_TO_POS)
8614 && IT_CHARPOS (*it) >= to_charpos
8615 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8616 SAVE_IT (ppos_it, *it, ppos_data);
8617
8618 /* Stop if lines are truncated and IT's current x-position is
8619 past the right edge of the window now. */
8620 if (it->line_wrap == TRUNCATE
8621 && it->current_x >= it->last_visible_x)
8622 {
8623 if (!FRAME_WINDOW_P (it->f)
8624 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8625 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8626 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8627 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8628 {
8629 int at_eob_p = 0;
8630
8631 if ((at_eob_p = !get_next_display_element (it))
8632 || BUFFER_POS_REACHED_P ()
8633 /* If we are past TO_CHARPOS, but never saw any
8634 character positions smaller than TO_CHARPOS,
8635 return MOVE_POS_MATCH_OR_ZV, like the
8636 unidirectional display did. */
8637 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8638 && !saw_smaller_pos
8639 && IT_CHARPOS (*it) > to_charpos))
8640 {
8641 if (it->bidi_p
8642 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8643 RESTORE_IT (it, &ppos_it, ppos_data);
8644 result = MOVE_POS_MATCH_OR_ZV;
8645 break;
8646 }
8647 if (ITERATOR_AT_END_OF_LINE_P (it))
8648 {
8649 result = MOVE_NEWLINE_OR_CR;
8650 break;
8651 }
8652 }
8653 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8654 && !saw_smaller_pos
8655 && IT_CHARPOS (*it) > to_charpos)
8656 {
8657 if (IT_CHARPOS (ppos_it) < ZV)
8658 RESTORE_IT (it, &ppos_it, ppos_data);
8659 result = MOVE_POS_MATCH_OR_ZV;
8660 break;
8661 }
8662 result = MOVE_LINE_TRUNCATED;
8663 break;
8664 }
8665 #undef IT_RESET_X_ASCENT_DESCENT
8666 }
8667
8668 #undef BUFFER_POS_REACHED_P
8669
8670 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8671 restore the saved iterator. */
8672 if (atpos_it.sp >= 0)
8673 RESTORE_IT (it, &atpos_it, atpos_data);
8674 else if (atx_it.sp >= 0)
8675 RESTORE_IT (it, &atx_it, atx_data);
8676
8677 done:
8678
8679 if (atpos_data)
8680 bidi_unshelve_cache (atpos_data, 1);
8681 if (atx_data)
8682 bidi_unshelve_cache (atx_data, 1);
8683 if (wrap_data)
8684 bidi_unshelve_cache (wrap_data, 1);
8685 if (ppos_data)
8686 bidi_unshelve_cache (ppos_data, 1);
8687
8688 /* Restore the iterator settings altered at the beginning of this
8689 function. */
8690 it->glyph_row = saved_glyph_row;
8691 return result;
8692 }
8693
8694 /* For external use. */
8695 void
8696 move_it_in_display_line (struct it *it,
8697 ptrdiff_t to_charpos, int to_x,
8698 enum move_operation_enum op)
8699 {
8700 if (it->line_wrap == WORD_WRAP
8701 && (op & MOVE_TO_X))
8702 {
8703 struct it save_it;
8704 void *save_data = NULL;
8705 int skip;
8706
8707 SAVE_IT (save_it, *it, save_data);
8708 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8709 /* When word-wrap is on, TO_X may lie past the end
8710 of a wrapped line. Then it->current is the
8711 character on the next line, so backtrack to the
8712 space before the wrap point. */
8713 if (skip == MOVE_LINE_CONTINUED)
8714 {
8715 int prev_x = max (it->current_x - 1, 0);
8716 RESTORE_IT (it, &save_it, save_data);
8717 move_it_in_display_line_to
8718 (it, -1, prev_x, MOVE_TO_X);
8719 }
8720 else
8721 bidi_unshelve_cache (save_data, 1);
8722 }
8723 else
8724 move_it_in_display_line_to (it, to_charpos, to_x, op);
8725 }
8726
8727
8728 /* Move IT forward until it satisfies one or more of the criteria in
8729 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8730
8731 OP is a bit-mask that specifies where to stop, and in particular,
8732 which of those four position arguments makes a difference. See the
8733 description of enum move_operation_enum.
8734
8735 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8736 screen line, this function will set IT to the next position that is
8737 displayed to the right of TO_CHARPOS on the screen. */
8738
8739 void
8740 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8741 {
8742 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8743 int line_height, line_start_x = 0, reached = 0;
8744 void *backup_data = NULL;
8745
8746 for (;;)
8747 {
8748 if (op & MOVE_TO_VPOS)
8749 {
8750 /* If no TO_CHARPOS and no TO_X specified, stop at the
8751 start of the line TO_VPOS. */
8752 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8753 {
8754 if (it->vpos == to_vpos)
8755 {
8756 reached = 1;
8757 break;
8758 }
8759 else
8760 skip = move_it_in_display_line_to (it, -1, -1, 0);
8761 }
8762 else
8763 {
8764 /* TO_VPOS >= 0 means stop at TO_X in the line at
8765 TO_VPOS, or at TO_POS, whichever comes first. */
8766 if (it->vpos == to_vpos)
8767 {
8768 reached = 2;
8769 break;
8770 }
8771
8772 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8773
8774 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8775 {
8776 reached = 3;
8777 break;
8778 }
8779 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8780 {
8781 /* We have reached TO_X but not in the line we want. */
8782 skip = move_it_in_display_line_to (it, to_charpos,
8783 -1, MOVE_TO_POS);
8784 if (skip == MOVE_POS_MATCH_OR_ZV)
8785 {
8786 reached = 4;
8787 break;
8788 }
8789 }
8790 }
8791 }
8792 else if (op & MOVE_TO_Y)
8793 {
8794 struct it it_backup;
8795
8796 if (it->line_wrap == WORD_WRAP)
8797 SAVE_IT (it_backup, *it, backup_data);
8798
8799 /* TO_Y specified means stop at TO_X in the line containing
8800 TO_Y---or at TO_CHARPOS if this is reached first. The
8801 problem is that we can't really tell whether the line
8802 contains TO_Y before we have completely scanned it, and
8803 this may skip past TO_X. What we do is to first scan to
8804 TO_X.
8805
8806 If TO_X is not specified, use a TO_X of zero. The reason
8807 is to make the outcome of this function more predictable.
8808 If we didn't use TO_X == 0, we would stop at the end of
8809 the line which is probably not what a caller would expect
8810 to happen. */
8811 skip = move_it_in_display_line_to
8812 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8813 (MOVE_TO_X | (op & MOVE_TO_POS)));
8814
8815 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8816 if (skip == MOVE_POS_MATCH_OR_ZV)
8817 reached = 5;
8818 else if (skip == MOVE_X_REACHED)
8819 {
8820 /* If TO_X was reached, we want to know whether TO_Y is
8821 in the line. We know this is the case if the already
8822 scanned glyphs make the line tall enough. Otherwise,
8823 we must check by scanning the rest of the line. */
8824 line_height = it->max_ascent + it->max_descent;
8825 if (to_y >= it->current_y
8826 && to_y < it->current_y + line_height)
8827 {
8828 reached = 6;
8829 break;
8830 }
8831 SAVE_IT (it_backup, *it, backup_data);
8832 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8833 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8834 op & MOVE_TO_POS);
8835 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8836 line_height = it->max_ascent + it->max_descent;
8837 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8838
8839 if (to_y >= it->current_y
8840 && to_y < it->current_y + line_height)
8841 {
8842 /* If TO_Y is in this line and TO_X was reached
8843 above, we scanned too far. We have to restore
8844 IT's settings to the ones before skipping. But
8845 keep the more accurate values of max_ascent and
8846 max_descent we've found while skipping the rest
8847 of the line, for the sake of callers, such as
8848 pos_visible_p, that need to know the line
8849 height. */
8850 int max_ascent = it->max_ascent;
8851 int max_descent = it->max_descent;
8852
8853 RESTORE_IT (it, &it_backup, backup_data);
8854 it->max_ascent = max_ascent;
8855 it->max_descent = max_descent;
8856 reached = 6;
8857 }
8858 else
8859 {
8860 skip = skip2;
8861 if (skip == MOVE_POS_MATCH_OR_ZV)
8862 reached = 7;
8863 }
8864 }
8865 else
8866 {
8867 /* Check whether TO_Y is in this line. */
8868 line_height = it->max_ascent + it->max_descent;
8869 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8870
8871 if (to_y >= it->current_y
8872 && to_y < it->current_y + line_height)
8873 {
8874 /* When word-wrap is on, TO_X may lie past the end
8875 of a wrapped line. Then it->current is the
8876 character on the next line, so backtrack to the
8877 space before the wrap point. */
8878 if (skip == MOVE_LINE_CONTINUED
8879 && it->line_wrap == WORD_WRAP)
8880 {
8881 int prev_x = max (it->current_x - 1, 0);
8882 RESTORE_IT (it, &it_backup, backup_data);
8883 skip = move_it_in_display_line_to
8884 (it, -1, prev_x, MOVE_TO_X);
8885 }
8886 reached = 6;
8887 }
8888 }
8889
8890 if (reached)
8891 break;
8892 }
8893 else if (BUFFERP (it->object)
8894 && (it->method == GET_FROM_BUFFER
8895 || it->method == GET_FROM_STRETCH)
8896 && IT_CHARPOS (*it) >= to_charpos
8897 /* Under bidi iteration, a call to set_iterator_to_next
8898 can scan far beyond to_charpos if the initial
8899 portion of the next line needs to be reordered. In
8900 that case, give move_it_in_display_line_to another
8901 chance below. */
8902 && !(it->bidi_p
8903 && it->bidi_it.scan_dir == -1))
8904 skip = MOVE_POS_MATCH_OR_ZV;
8905 else
8906 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8907
8908 switch (skip)
8909 {
8910 case MOVE_POS_MATCH_OR_ZV:
8911 reached = 8;
8912 goto out;
8913
8914 case MOVE_NEWLINE_OR_CR:
8915 set_iterator_to_next (it, 1);
8916 it->continuation_lines_width = 0;
8917 break;
8918
8919 case MOVE_LINE_TRUNCATED:
8920 it->continuation_lines_width = 0;
8921 reseat_at_next_visible_line_start (it, 0);
8922 if ((op & MOVE_TO_POS) != 0
8923 && IT_CHARPOS (*it) > to_charpos)
8924 {
8925 reached = 9;
8926 goto out;
8927 }
8928 break;
8929
8930 case MOVE_LINE_CONTINUED:
8931 /* For continued lines ending in a tab, some of the glyphs
8932 associated with the tab are displayed on the current
8933 line. Since it->current_x does not include these glyphs,
8934 we use it->last_visible_x instead. */
8935 if (it->c == '\t')
8936 {
8937 it->continuation_lines_width += it->last_visible_x;
8938 /* When moving by vpos, ensure that the iterator really
8939 advances to the next line (bug#847, bug#969). Fixme:
8940 do we need to do this in other circumstances? */
8941 if (it->current_x != it->last_visible_x
8942 && (op & MOVE_TO_VPOS)
8943 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8944 {
8945 line_start_x = it->current_x + it->pixel_width
8946 - it->last_visible_x;
8947 set_iterator_to_next (it, 0);
8948 }
8949 }
8950 else
8951 it->continuation_lines_width += it->current_x;
8952 break;
8953
8954 default:
8955 emacs_abort ();
8956 }
8957
8958 /* Reset/increment for the next run. */
8959 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
8960 it->current_x = line_start_x;
8961 line_start_x = 0;
8962 it->hpos = 0;
8963 it->current_y += it->max_ascent + it->max_descent;
8964 ++it->vpos;
8965 last_height = it->max_ascent + it->max_descent;
8966 last_max_ascent = it->max_ascent;
8967 it->max_ascent = it->max_descent = 0;
8968 }
8969
8970 out:
8971
8972 /* On text terminals, we may stop at the end of a line in the middle
8973 of a multi-character glyph. If the glyph itself is continued,
8974 i.e. it is actually displayed on the next line, don't treat this
8975 stopping point as valid; move to the next line instead (unless
8976 that brings us offscreen). */
8977 if (!FRAME_WINDOW_P (it->f)
8978 && op & MOVE_TO_POS
8979 && IT_CHARPOS (*it) == to_charpos
8980 && it->what == IT_CHARACTER
8981 && it->nglyphs > 1
8982 && it->line_wrap == WINDOW_WRAP
8983 && it->current_x == it->last_visible_x - 1
8984 && it->c != '\n'
8985 && it->c != '\t'
8986 && it->vpos < XFASTINT (it->w->window_end_vpos))
8987 {
8988 it->continuation_lines_width += it->current_x;
8989 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
8990 it->current_y += it->max_ascent + it->max_descent;
8991 ++it->vpos;
8992 last_height = it->max_ascent + it->max_descent;
8993 last_max_ascent = it->max_ascent;
8994 }
8995
8996 if (backup_data)
8997 bidi_unshelve_cache (backup_data, 1);
8998
8999 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9000 }
9001
9002
9003 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9004
9005 If DY > 0, move IT backward at least that many pixels. DY = 0
9006 means move IT backward to the preceding line start or BEGV. This
9007 function may move over more than DY pixels if IT->current_y - DY
9008 ends up in the middle of a line; in this case IT->current_y will be
9009 set to the top of the line moved to. */
9010
9011 void
9012 move_it_vertically_backward (struct it *it, int dy)
9013 {
9014 int nlines, h;
9015 struct it it2, it3;
9016 void *it2data = NULL, *it3data = NULL;
9017 ptrdiff_t start_pos;
9018
9019 move_further_back:
9020 eassert (dy >= 0);
9021
9022 start_pos = IT_CHARPOS (*it);
9023
9024 /* Estimate how many newlines we must move back. */
9025 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
9026
9027 /* Set the iterator's position that many lines back. */
9028 while (nlines-- && IT_CHARPOS (*it) > BEGV)
9029 back_to_previous_visible_line_start (it);
9030
9031 /* Reseat the iterator here. When moving backward, we don't want
9032 reseat to skip forward over invisible text, set up the iterator
9033 to deliver from overlay strings at the new position etc. So,
9034 use reseat_1 here. */
9035 reseat_1 (it, it->current.pos, 1);
9036
9037 /* We are now surely at a line start. */
9038 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9039 reordering is in effect. */
9040 it->continuation_lines_width = 0;
9041
9042 /* Move forward and see what y-distance we moved. First move to the
9043 start of the next line so that we get its height. We need this
9044 height to be able to tell whether we reached the specified
9045 y-distance. */
9046 SAVE_IT (it2, *it, it2data);
9047 it2.max_ascent = it2.max_descent = 0;
9048 do
9049 {
9050 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9051 MOVE_TO_POS | MOVE_TO_VPOS);
9052 }
9053 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9054 /* If we are in a display string which starts at START_POS,
9055 and that display string includes a newline, and we are
9056 right after that newline (i.e. at the beginning of a
9057 display line), exit the loop, because otherwise we will
9058 infloop, since move_it_to will see that it is already at
9059 START_POS and will not move. */
9060 || (it2.method == GET_FROM_STRING
9061 && IT_CHARPOS (it2) == start_pos
9062 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9063 eassert (IT_CHARPOS (*it) >= BEGV);
9064 SAVE_IT (it3, it2, it3data);
9065
9066 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9067 eassert (IT_CHARPOS (*it) >= BEGV);
9068 /* H is the actual vertical distance from the position in *IT
9069 and the starting position. */
9070 h = it2.current_y - it->current_y;
9071 /* NLINES is the distance in number of lines. */
9072 nlines = it2.vpos - it->vpos;
9073
9074 /* Correct IT's y and vpos position
9075 so that they are relative to the starting point. */
9076 it->vpos -= nlines;
9077 it->current_y -= h;
9078
9079 if (dy == 0)
9080 {
9081 /* DY == 0 means move to the start of the screen line. The
9082 value of nlines is > 0 if continuation lines were involved,
9083 or if the original IT position was at start of a line. */
9084 RESTORE_IT (it, it, it2data);
9085 if (nlines > 0)
9086 move_it_by_lines (it, nlines);
9087 /* The above code moves us to some position NLINES down,
9088 usually to its first glyph (leftmost in an L2R line), but
9089 that's not necessarily the start of the line, under bidi
9090 reordering. We want to get to the character position
9091 that is immediately after the newline of the previous
9092 line. */
9093 if (it->bidi_p
9094 && !it->continuation_lines_width
9095 && !STRINGP (it->string)
9096 && IT_CHARPOS (*it) > BEGV
9097 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9098 {
9099 ptrdiff_t nl_pos =
9100 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
9101
9102 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
9103 }
9104 bidi_unshelve_cache (it3data, 1);
9105 }
9106 else
9107 {
9108 /* The y-position we try to reach, relative to *IT.
9109 Note that H has been subtracted in front of the if-statement. */
9110 int target_y = it->current_y + h - dy;
9111 int y0 = it3.current_y;
9112 int y1;
9113 int line_height;
9114
9115 RESTORE_IT (&it3, &it3, it3data);
9116 y1 = line_bottom_y (&it3);
9117 line_height = y1 - y0;
9118 RESTORE_IT (it, it, it2data);
9119 /* If we did not reach target_y, try to move further backward if
9120 we can. If we moved too far backward, try to move forward. */
9121 if (target_y < it->current_y
9122 /* This is heuristic. In a window that's 3 lines high, with
9123 a line height of 13 pixels each, recentering with point
9124 on the bottom line will try to move -39/2 = 19 pixels
9125 backward. Try to avoid moving into the first line. */
9126 && (it->current_y - target_y
9127 > min (window_box_height (it->w), line_height * 2 / 3))
9128 && IT_CHARPOS (*it) > BEGV)
9129 {
9130 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9131 target_y - it->current_y));
9132 dy = it->current_y - target_y;
9133 goto move_further_back;
9134 }
9135 else if (target_y >= it->current_y + line_height
9136 && IT_CHARPOS (*it) < ZV)
9137 {
9138 /* Should move forward by at least one line, maybe more.
9139
9140 Note: Calling move_it_by_lines can be expensive on
9141 terminal frames, where compute_motion is used (via
9142 vmotion) to do the job, when there are very long lines
9143 and truncate-lines is nil. That's the reason for
9144 treating terminal frames specially here. */
9145
9146 if (!FRAME_WINDOW_P (it->f))
9147 move_it_vertically (it, target_y - (it->current_y + line_height));
9148 else
9149 {
9150 do
9151 {
9152 move_it_by_lines (it, 1);
9153 }
9154 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9155 }
9156 }
9157 }
9158 }
9159
9160
9161 /* Move IT by a specified amount of pixel lines DY. DY negative means
9162 move backwards. DY = 0 means move to start of screen line. At the
9163 end, IT will be on the start of a screen line. */
9164
9165 void
9166 move_it_vertically (struct it *it, int dy)
9167 {
9168 if (dy <= 0)
9169 move_it_vertically_backward (it, -dy);
9170 else
9171 {
9172 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9173 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9174 MOVE_TO_POS | MOVE_TO_Y);
9175 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9176
9177 /* If buffer ends in ZV without a newline, move to the start of
9178 the line to satisfy the post-condition. */
9179 if (IT_CHARPOS (*it) == ZV
9180 && ZV > BEGV
9181 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9182 move_it_by_lines (it, 0);
9183 }
9184 }
9185
9186
9187 /* Move iterator IT past the end of the text line it is in. */
9188
9189 void
9190 move_it_past_eol (struct it *it)
9191 {
9192 enum move_it_result rc;
9193
9194 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9195 if (rc == MOVE_NEWLINE_OR_CR)
9196 set_iterator_to_next (it, 0);
9197 }
9198
9199
9200 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9201 negative means move up. DVPOS == 0 means move to the start of the
9202 screen line.
9203
9204 Optimization idea: If we would know that IT->f doesn't use
9205 a face with proportional font, we could be faster for
9206 truncate-lines nil. */
9207
9208 void
9209 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9210 {
9211
9212 /* The commented-out optimization uses vmotion on terminals. This
9213 gives bad results, because elements like it->what, on which
9214 callers such as pos_visible_p rely, aren't updated. */
9215 /* struct position pos;
9216 if (!FRAME_WINDOW_P (it->f))
9217 {
9218 struct text_pos textpos;
9219
9220 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9221 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9222 reseat (it, textpos, 1);
9223 it->vpos += pos.vpos;
9224 it->current_y += pos.vpos;
9225 }
9226 else */
9227
9228 if (dvpos == 0)
9229 {
9230 /* DVPOS == 0 means move to the start of the screen line. */
9231 move_it_vertically_backward (it, 0);
9232 /* Let next call to line_bottom_y calculate real line height */
9233 last_height = 0;
9234 }
9235 else if (dvpos > 0)
9236 {
9237 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9238 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9239 {
9240 /* Only move to the next buffer position if we ended up in a
9241 string from display property, not in an overlay string
9242 (before-string or after-string). That is because the
9243 latter don't conceal the underlying buffer position, so
9244 we can ask to move the iterator to the exact position we
9245 are interested in. Note that, even if we are already at
9246 IT_CHARPOS (*it), the call below is not a no-op, as it
9247 will detect that we are at the end of the string, pop the
9248 iterator, and compute it->current_x and it->hpos
9249 correctly. */
9250 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9251 -1, -1, -1, MOVE_TO_POS);
9252 }
9253 }
9254 else
9255 {
9256 struct it it2;
9257 void *it2data = NULL;
9258 ptrdiff_t start_charpos, i;
9259
9260 /* Start at the beginning of the screen line containing IT's
9261 position. This may actually move vertically backwards,
9262 in case of overlays, so adjust dvpos accordingly. */
9263 dvpos += it->vpos;
9264 move_it_vertically_backward (it, 0);
9265 dvpos -= it->vpos;
9266
9267 /* Go back -DVPOS visible lines and reseat the iterator there. */
9268 start_charpos = IT_CHARPOS (*it);
9269 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9270 back_to_previous_visible_line_start (it);
9271 reseat (it, it->current.pos, 1);
9272
9273 /* Move further back if we end up in a string or an image. */
9274 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9275 {
9276 /* First try to move to start of display line. */
9277 dvpos += it->vpos;
9278 move_it_vertically_backward (it, 0);
9279 dvpos -= it->vpos;
9280 if (IT_POS_VALID_AFTER_MOVE_P (it))
9281 break;
9282 /* If start of line is still in string or image,
9283 move further back. */
9284 back_to_previous_visible_line_start (it);
9285 reseat (it, it->current.pos, 1);
9286 dvpos--;
9287 }
9288
9289 it->current_x = it->hpos = 0;
9290
9291 /* Above call may have moved too far if continuation lines
9292 are involved. Scan forward and see if it did. */
9293 SAVE_IT (it2, *it, it2data);
9294 it2.vpos = it2.current_y = 0;
9295 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9296 it->vpos -= it2.vpos;
9297 it->current_y -= it2.current_y;
9298 it->current_x = it->hpos = 0;
9299
9300 /* If we moved too far back, move IT some lines forward. */
9301 if (it2.vpos > -dvpos)
9302 {
9303 int delta = it2.vpos + dvpos;
9304
9305 RESTORE_IT (&it2, &it2, it2data);
9306 SAVE_IT (it2, *it, it2data);
9307 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9308 /* Move back again if we got too far ahead. */
9309 if (IT_CHARPOS (*it) >= start_charpos)
9310 RESTORE_IT (it, &it2, it2data);
9311 else
9312 bidi_unshelve_cache (it2data, 1);
9313 }
9314 else
9315 RESTORE_IT (it, it, it2data);
9316 }
9317 }
9318
9319 /* Return 1 if IT points into the middle of a display vector. */
9320
9321 int
9322 in_display_vector_p (struct it *it)
9323 {
9324 return (it->method == GET_FROM_DISPLAY_VECTOR
9325 && it->current.dpvec_index > 0
9326 && it->dpvec + it->current.dpvec_index != it->dpend);
9327 }
9328
9329 \f
9330 /***********************************************************************
9331 Messages
9332 ***********************************************************************/
9333
9334
9335 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9336 to *Messages*. */
9337
9338 void
9339 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9340 {
9341 Lisp_Object args[3];
9342 Lisp_Object msg, fmt;
9343 char *buffer;
9344 ptrdiff_t len;
9345 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9346 USE_SAFE_ALLOCA;
9347
9348 fmt = msg = Qnil;
9349 GCPRO4 (fmt, msg, arg1, arg2);
9350
9351 args[0] = fmt = build_string (format);
9352 args[1] = arg1;
9353 args[2] = arg2;
9354 msg = Fformat (3, args);
9355
9356 len = SBYTES (msg) + 1;
9357 buffer = SAFE_ALLOCA (len);
9358 memcpy (buffer, SDATA (msg), len);
9359
9360 message_dolog (buffer, len - 1, 1, 0);
9361 SAFE_FREE ();
9362
9363 UNGCPRO;
9364 }
9365
9366
9367 /* Output a newline in the *Messages* buffer if "needs" one. */
9368
9369 void
9370 message_log_maybe_newline (void)
9371 {
9372 if (message_log_need_newline)
9373 message_dolog ("", 0, 1, 0);
9374 }
9375
9376
9377 /* Add a string M of length NBYTES to the message log, optionally
9378 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9379 nonzero, means interpret the contents of M as multibyte. This
9380 function calls low-level routines in order to bypass text property
9381 hooks, etc. which might not be safe to run.
9382
9383 This may GC (insert may run before/after change hooks),
9384 so the buffer M must NOT point to a Lisp string. */
9385
9386 void
9387 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9388 {
9389 const unsigned char *msg = (const unsigned char *) m;
9390
9391 if (!NILP (Vmemory_full))
9392 return;
9393
9394 if (!NILP (Vmessage_log_max))
9395 {
9396 struct buffer *oldbuf;
9397 Lisp_Object oldpoint, oldbegv, oldzv;
9398 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9399 ptrdiff_t point_at_end = 0;
9400 ptrdiff_t zv_at_end = 0;
9401 Lisp_Object old_deactivate_mark, tem;
9402 struct gcpro gcpro1;
9403
9404 old_deactivate_mark = Vdeactivate_mark;
9405 oldbuf = current_buffer;
9406 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9407 bset_undo_list (current_buffer, Qt);
9408
9409 oldpoint = message_dolog_marker1;
9410 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9411 oldbegv = message_dolog_marker2;
9412 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9413 oldzv = message_dolog_marker3;
9414 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9415 GCPRO1 (old_deactivate_mark);
9416
9417 if (PT == Z)
9418 point_at_end = 1;
9419 if (ZV == Z)
9420 zv_at_end = 1;
9421
9422 BEGV = BEG;
9423 BEGV_BYTE = BEG_BYTE;
9424 ZV = Z;
9425 ZV_BYTE = Z_BYTE;
9426 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9427
9428 /* Insert the string--maybe converting multibyte to single byte
9429 or vice versa, so that all the text fits the buffer. */
9430 if (multibyte
9431 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9432 {
9433 ptrdiff_t i;
9434 int c, char_bytes;
9435 char work[1];
9436
9437 /* Convert a multibyte string to single-byte
9438 for the *Message* buffer. */
9439 for (i = 0; i < nbytes; i += char_bytes)
9440 {
9441 c = string_char_and_length (msg + i, &char_bytes);
9442 work[0] = (ASCII_CHAR_P (c)
9443 ? c
9444 : multibyte_char_to_unibyte (c));
9445 insert_1_both (work, 1, 1, 1, 0, 0);
9446 }
9447 }
9448 else if (! multibyte
9449 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9450 {
9451 ptrdiff_t i;
9452 int c, char_bytes;
9453 unsigned char str[MAX_MULTIBYTE_LENGTH];
9454 /* Convert a single-byte string to multibyte
9455 for the *Message* buffer. */
9456 for (i = 0; i < nbytes; i++)
9457 {
9458 c = msg[i];
9459 MAKE_CHAR_MULTIBYTE (c);
9460 char_bytes = CHAR_STRING (c, str);
9461 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9462 }
9463 }
9464 else if (nbytes)
9465 insert_1 (m, nbytes, 1, 0, 0);
9466
9467 if (nlflag)
9468 {
9469 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9470 printmax_t dups;
9471 insert_1 ("\n", 1, 1, 0, 0);
9472
9473 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9474 this_bol = PT;
9475 this_bol_byte = PT_BYTE;
9476
9477 /* See if this line duplicates the previous one.
9478 If so, combine duplicates. */
9479 if (this_bol > BEG)
9480 {
9481 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9482 prev_bol = PT;
9483 prev_bol_byte = PT_BYTE;
9484
9485 dups = message_log_check_duplicate (prev_bol_byte,
9486 this_bol_byte);
9487 if (dups)
9488 {
9489 del_range_both (prev_bol, prev_bol_byte,
9490 this_bol, this_bol_byte, 0);
9491 if (dups > 1)
9492 {
9493 char dupstr[sizeof " [ times]"
9494 + INT_STRLEN_BOUND (printmax_t)];
9495
9496 /* If you change this format, don't forget to also
9497 change message_log_check_duplicate. */
9498 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9499 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9500 insert_1 (dupstr, duplen, 1, 0, 1);
9501 }
9502 }
9503 }
9504
9505 /* If we have more than the desired maximum number of lines
9506 in the *Messages* buffer now, delete the oldest ones.
9507 This is safe because we don't have undo in this buffer. */
9508
9509 if (NATNUMP (Vmessage_log_max))
9510 {
9511 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9512 -XFASTINT (Vmessage_log_max) - 1, 0);
9513 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9514 }
9515 }
9516 BEGV = marker_position (oldbegv);
9517 BEGV_BYTE = marker_byte_position (oldbegv);
9518
9519 if (zv_at_end)
9520 {
9521 ZV = Z;
9522 ZV_BYTE = Z_BYTE;
9523 }
9524 else
9525 {
9526 ZV = marker_position (oldzv);
9527 ZV_BYTE = marker_byte_position (oldzv);
9528 }
9529
9530 if (point_at_end)
9531 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9532 else
9533 /* We can't do Fgoto_char (oldpoint) because it will run some
9534 Lisp code. */
9535 TEMP_SET_PT_BOTH (marker_position (oldpoint),
9536 marker_byte_position (oldpoint));
9537
9538 UNGCPRO;
9539 unchain_marker (XMARKER (oldpoint));
9540 unchain_marker (XMARKER (oldbegv));
9541 unchain_marker (XMARKER (oldzv));
9542
9543 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9544 set_buffer_internal (oldbuf);
9545 if (NILP (tem))
9546 windows_or_buffers_changed = old_windows_or_buffers_changed;
9547 message_log_need_newline = !nlflag;
9548 Vdeactivate_mark = old_deactivate_mark;
9549 }
9550 }
9551
9552
9553 /* We are at the end of the buffer after just having inserted a newline.
9554 (Note: We depend on the fact we won't be crossing the gap.)
9555 Check to see if the most recent message looks a lot like the previous one.
9556 Return 0 if different, 1 if the new one should just replace it, or a
9557 value N > 1 if we should also append " [N times]". */
9558
9559 static intmax_t
9560 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9561 {
9562 ptrdiff_t i;
9563 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9564 int seen_dots = 0;
9565 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9566 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9567
9568 for (i = 0; i < len; i++)
9569 {
9570 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9571 seen_dots = 1;
9572 if (p1[i] != p2[i])
9573 return seen_dots;
9574 }
9575 p1 += len;
9576 if (*p1 == '\n')
9577 return 2;
9578 if (*p1++ == ' ' && *p1++ == '[')
9579 {
9580 char *pend;
9581 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9582 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9583 return n+1;
9584 }
9585 return 0;
9586 }
9587 \f
9588
9589 /* Display an echo area message M with a specified length of NBYTES
9590 bytes. The string may include null characters. If M is 0, clear
9591 out any existing message, and let the mini-buffer text show
9592 through.
9593
9594 This may GC, so the buffer M must NOT point to a Lisp string. */
9595
9596 void
9597 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9598 {
9599 /* First flush out any partial line written with print. */
9600 message_log_maybe_newline ();
9601 if (m)
9602 message_dolog (m, nbytes, 1, multibyte);
9603 message2_nolog (m, nbytes, multibyte);
9604 }
9605
9606
9607 /* The non-logging counterpart of message2. */
9608
9609 void
9610 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9611 {
9612 struct frame *sf = SELECTED_FRAME ();
9613 message_enable_multibyte = multibyte;
9614
9615 if (FRAME_INITIAL_P (sf))
9616 {
9617 if (noninteractive_need_newline)
9618 putc ('\n', stderr);
9619 noninteractive_need_newline = 0;
9620 if (m)
9621 fwrite (m, nbytes, 1, stderr);
9622 if (cursor_in_echo_area == 0)
9623 fprintf (stderr, "\n");
9624 fflush (stderr);
9625 }
9626 /* A null message buffer means that the frame hasn't really been
9627 initialized yet. Error messages get reported properly by
9628 cmd_error, so this must be just an informative message; toss it. */
9629 else if (INTERACTIVE
9630 && sf->glyphs_initialized_p
9631 && FRAME_MESSAGE_BUF (sf))
9632 {
9633 Lisp_Object mini_window;
9634 struct frame *f;
9635
9636 /* Get the frame containing the mini-buffer
9637 that the selected frame is using. */
9638 mini_window = FRAME_MINIBUF_WINDOW (sf);
9639 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9640
9641 FRAME_SAMPLE_VISIBILITY (f);
9642 if (FRAME_VISIBLE_P (sf)
9643 && ! FRAME_VISIBLE_P (f))
9644 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9645
9646 if (m)
9647 {
9648 set_message (m, Qnil, nbytes, multibyte);
9649 if (minibuffer_auto_raise)
9650 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9651 }
9652 else
9653 clear_message (1, 1);
9654
9655 do_pending_window_change (0);
9656 echo_area_display (1);
9657 do_pending_window_change (0);
9658 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9659 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9660 }
9661 }
9662
9663
9664 /* Display an echo area message M with a specified length of NBYTES
9665 bytes. The string may include null characters. If M is not a
9666 string, clear out any existing message, and let the mini-buffer
9667 text show through.
9668
9669 This function cancels echoing. */
9670
9671 void
9672 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9673 {
9674 struct gcpro gcpro1;
9675
9676 GCPRO1 (m);
9677 clear_message (1,1);
9678 cancel_echoing ();
9679
9680 /* First flush out any partial line written with print. */
9681 message_log_maybe_newline ();
9682 if (STRINGP (m))
9683 {
9684 USE_SAFE_ALLOCA;
9685 char *buffer = SAFE_ALLOCA (nbytes);
9686 memcpy (buffer, SDATA (m), nbytes);
9687 message_dolog (buffer, nbytes, 1, multibyte);
9688 SAFE_FREE ();
9689 }
9690 message3_nolog (m, nbytes, multibyte);
9691
9692 UNGCPRO;
9693 }
9694
9695
9696 /* The non-logging version of message3.
9697 This does not cancel echoing, because it is used for echoing.
9698 Perhaps we need to make a separate function for echoing
9699 and make this cancel echoing. */
9700
9701 void
9702 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9703 {
9704 struct frame *sf = SELECTED_FRAME ();
9705 message_enable_multibyte = multibyte;
9706
9707 if (FRAME_INITIAL_P (sf))
9708 {
9709 if (noninteractive_need_newline)
9710 putc ('\n', stderr);
9711 noninteractive_need_newline = 0;
9712 if (STRINGP (m))
9713 fwrite (SDATA (m), nbytes, 1, stderr);
9714 if (cursor_in_echo_area == 0)
9715 fprintf (stderr, "\n");
9716 fflush (stderr);
9717 }
9718 /* A null message buffer means that the frame hasn't really been
9719 initialized yet. Error messages get reported properly by
9720 cmd_error, so this must be just an informative message; toss it. */
9721 else if (INTERACTIVE
9722 && sf->glyphs_initialized_p
9723 && FRAME_MESSAGE_BUF (sf))
9724 {
9725 Lisp_Object mini_window;
9726 Lisp_Object frame;
9727 struct frame *f;
9728
9729 /* Get the frame containing the mini-buffer
9730 that the selected frame is using. */
9731 mini_window = FRAME_MINIBUF_WINDOW (sf);
9732 frame = XWINDOW (mini_window)->frame;
9733 f = XFRAME (frame);
9734
9735 FRAME_SAMPLE_VISIBILITY (f);
9736 if (FRAME_VISIBLE_P (sf)
9737 && !FRAME_VISIBLE_P (f))
9738 Fmake_frame_visible (frame);
9739
9740 if (STRINGP (m) && SCHARS (m) > 0)
9741 {
9742 set_message (NULL, m, nbytes, multibyte);
9743 if (minibuffer_auto_raise)
9744 Fraise_frame (frame);
9745 /* Assume we are not echoing.
9746 (If we are, echo_now will override this.) */
9747 echo_message_buffer = Qnil;
9748 }
9749 else
9750 clear_message (1, 1);
9751
9752 do_pending_window_change (0);
9753 echo_area_display (1);
9754 do_pending_window_change (0);
9755 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9756 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9757 }
9758 }
9759
9760
9761 /* Display a null-terminated echo area message M. If M is 0, clear
9762 out any existing message, and let the mini-buffer text show through.
9763
9764 The buffer M must continue to exist until after the echo area gets
9765 cleared or some other message gets displayed there. Do not pass
9766 text that is stored in a Lisp string. Do not pass text in a buffer
9767 that was alloca'd. */
9768
9769 void
9770 message1 (const char *m)
9771 {
9772 message2 (m, (m ? strlen (m) : 0), 0);
9773 }
9774
9775
9776 /* The non-logging counterpart of message1. */
9777
9778 void
9779 message1_nolog (const char *m)
9780 {
9781 message2_nolog (m, (m ? strlen (m) : 0), 0);
9782 }
9783
9784 /* Display a message M which contains a single %s
9785 which gets replaced with STRING. */
9786
9787 void
9788 message_with_string (const char *m, Lisp_Object string, int log)
9789 {
9790 CHECK_STRING (string);
9791
9792 if (noninteractive)
9793 {
9794 if (m)
9795 {
9796 if (noninteractive_need_newline)
9797 putc ('\n', stderr);
9798 noninteractive_need_newline = 0;
9799 fprintf (stderr, m, SDATA (string));
9800 if (!cursor_in_echo_area)
9801 fprintf (stderr, "\n");
9802 fflush (stderr);
9803 }
9804 }
9805 else if (INTERACTIVE)
9806 {
9807 /* The frame whose minibuffer we're going to display the message on.
9808 It may be larger than the selected frame, so we need
9809 to use its buffer, not the selected frame's buffer. */
9810 Lisp_Object mini_window;
9811 struct frame *f, *sf = SELECTED_FRAME ();
9812
9813 /* Get the frame containing the minibuffer
9814 that the selected frame is using. */
9815 mini_window = FRAME_MINIBUF_WINDOW (sf);
9816 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9817
9818 /* A null message buffer means that the frame hasn't really been
9819 initialized yet. Error messages get reported properly by
9820 cmd_error, so this must be just an informative message; toss it. */
9821 if (FRAME_MESSAGE_BUF (f))
9822 {
9823 Lisp_Object args[2], msg;
9824 struct gcpro gcpro1, gcpro2;
9825
9826 args[0] = build_string (m);
9827 args[1] = msg = string;
9828 GCPRO2 (args[0], msg);
9829 gcpro1.nvars = 2;
9830
9831 msg = Fformat (2, args);
9832
9833 if (log)
9834 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9835 else
9836 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9837
9838 UNGCPRO;
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
9848 /* Dump an informative message to the minibuf. If M is 0, clear out
9849 any existing message, and let the mini-buffer text show through. */
9850
9851 static void
9852 vmessage (const char *m, va_list ap)
9853 {
9854 if (noninteractive)
9855 {
9856 if (m)
9857 {
9858 if (noninteractive_need_newline)
9859 putc ('\n', stderr);
9860 noninteractive_need_newline = 0;
9861 vfprintf (stderr, m, ap);
9862 if (cursor_in_echo_area == 0)
9863 fprintf (stderr, "\n");
9864 fflush (stderr);
9865 }
9866 }
9867 else if (INTERACTIVE)
9868 {
9869 /* The frame whose mini-buffer we're going to display the message
9870 on. It may be larger than the selected frame, so we need to
9871 use its buffer, not the selected frame's buffer. */
9872 Lisp_Object mini_window;
9873 struct frame *f, *sf = SELECTED_FRAME ();
9874
9875 /* Get the frame containing the mini-buffer
9876 that the selected frame is using. */
9877 mini_window = FRAME_MINIBUF_WINDOW (sf);
9878 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9879
9880 /* A null message buffer means that the frame hasn't really been
9881 initialized yet. Error messages get reported properly by
9882 cmd_error, so this must be just an informative message; toss
9883 it. */
9884 if (FRAME_MESSAGE_BUF (f))
9885 {
9886 if (m)
9887 {
9888 ptrdiff_t len;
9889
9890 len = doprnt (FRAME_MESSAGE_BUF (f),
9891 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9892
9893 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9894 }
9895 else
9896 message1 (0);
9897
9898 /* Print should start at the beginning of the message
9899 buffer next time. */
9900 message_buf_print = 0;
9901 }
9902 }
9903 }
9904
9905 void
9906 message (const char *m, ...)
9907 {
9908 va_list ap;
9909 va_start (ap, m);
9910 vmessage (m, ap);
9911 va_end (ap);
9912 }
9913
9914
9915 #if 0
9916 /* The non-logging version of message. */
9917
9918 void
9919 message_nolog (const char *m, ...)
9920 {
9921 Lisp_Object old_log_max;
9922 va_list ap;
9923 va_start (ap, m);
9924 old_log_max = Vmessage_log_max;
9925 Vmessage_log_max = Qnil;
9926 vmessage (m, ap);
9927 Vmessage_log_max = old_log_max;
9928 va_end (ap);
9929 }
9930 #endif
9931
9932
9933 /* Display the current message in the current mini-buffer. This is
9934 only called from error handlers in process.c, and is not time
9935 critical. */
9936
9937 void
9938 update_echo_area (void)
9939 {
9940 if (!NILP (echo_area_buffer[0]))
9941 {
9942 Lisp_Object string;
9943 string = Fcurrent_message ();
9944 message3 (string, SBYTES (string),
9945 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9946 }
9947 }
9948
9949
9950 /* Make sure echo area buffers in `echo_buffers' are live.
9951 If they aren't, make new ones. */
9952
9953 static void
9954 ensure_echo_area_buffers (void)
9955 {
9956 int i;
9957
9958 for (i = 0; i < 2; ++i)
9959 if (!BUFFERP (echo_buffer[i])
9960 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
9961 {
9962 char name[30];
9963 Lisp_Object old_buffer;
9964 int j;
9965
9966 old_buffer = echo_buffer[i];
9967 echo_buffer[i] = Fget_buffer_create
9968 (make_formatted_string (name, " *Echo Area %d*", i));
9969 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
9970 /* to force word wrap in echo area -
9971 it was decided to postpone this*/
9972 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
9973
9974 for (j = 0; j < 2; ++j)
9975 if (EQ (old_buffer, echo_area_buffer[j]))
9976 echo_area_buffer[j] = echo_buffer[i];
9977 }
9978 }
9979
9980
9981 /* Call FN with args A1..A4 with either the current or last displayed
9982 echo_area_buffer as current buffer.
9983
9984 WHICH zero means use the current message buffer
9985 echo_area_buffer[0]. If that is nil, choose a suitable buffer
9986 from echo_buffer[] and clear it.
9987
9988 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
9989 suitable buffer from echo_buffer[] and clear it.
9990
9991 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
9992 that the current message becomes the last displayed one, make
9993 choose a suitable buffer for echo_area_buffer[0], and clear it.
9994
9995 Value is what FN returns. */
9996
9997 static int
9998 with_echo_area_buffer (struct window *w, int which,
9999 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
10000 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10001 {
10002 Lisp_Object buffer;
10003 int this_one, the_other, clear_buffer_p, rc;
10004 ptrdiff_t count = SPECPDL_INDEX ();
10005
10006 /* If buffers aren't live, make new ones. */
10007 ensure_echo_area_buffers ();
10008
10009 clear_buffer_p = 0;
10010
10011 if (which == 0)
10012 this_one = 0, the_other = 1;
10013 else if (which > 0)
10014 this_one = 1, the_other = 0;
10015 else
10016 {
10017 this_one = 0, the_other = 1;
10018 clear_buffer_p = 1;
10019
10020 /* We need a fresh one in case the current echo buffer equals
10021 the one containing the last displayed echo area message. */
10022 if (!NILP (echo_area_buffer[this_one])
10023 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10024 echo_area_buffer[this_one] = Qnil;
10025 }
10026
10027 /* Choose a suitable buffer from echo_buffer[] is we don't
10028 have one. */
10029 if (NILP (echo_area_buffer[this_one]))
10030 {
10031 echo_area_buffer[this_one]
10032 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10033 ? echo_buffer[the_other]
10034 : echo_buffer[this_one]);
10035 clear_buffer_p = 1;
10036 }
10037
10038 buffer = echo_area_buffer[this_one];
10039
10040 /* Don't get confused by reusing the buffer used for echoing
10041 for a different purpose. */
10042 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10043 cancel_echoing ();
10044
10045 record_unwind_protect (unwind_with_echo_area_buffer,
10046 with_echo_area_buffer_unwind_data (w));
10047
10048 /* Make the echo area buffer current. Note that for display
10049 purposes, it is not necessary that the displayed window's buffer
10050 == current_buffer, except for text property lookup. So, let's
10051 only set that buffer temporarily here without doing a full
10052 Fset_window_buffer. We must also change w->pointm, though,
10053 because otherwise an assertions in unshow_buffer fails, and Emacs
10054 aborts. */
10055 set_buffer_internal_1 (XBUFFER (buffer));
10056 if (w)
10057 {
10058 wset_buffer (w, buffer);
10059 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10060 }
10061
10062 bset_undo_list (current_buffer, Qt);
10063 bset_read_only (current_buffer, Qnil);
10064 specbind (Qinhibit_read_only, Qt);
10065 specbind (Qinhibit_modification_hooks, Qt);
10066
10067 if (clear_buffer_p && Z > BEG)
10068 del_range (BEG, Z);
10069
10070 eassert (BEGV >= BEG);
10071 eassert (ZV <= Z && ZV >= BEGV);
10072
10073 rc = fn (a1, a2, a3, a4);
10074
10075 eassert (BEGV >= BEG);
10076 eassert (ZV <= Z && ZV >= BEGV);
10077
10078 unbind_to (count, Qnil);
10079 return rc;
10080 }
10081
10082
10083 /* Save state that should be preserved around the call to the function
10084 FN called in with_echo_area_buffer. */
10085
10086 static Lisp_Object
10087 with_echo_area_buffer_unwind_data (struct window *w)
10088 {
10089 int i = 0;
10090 Lisp_Object vector, tmp;
10091
10092 /* Reduce consing by keeping one vector in
10093 Vwith_echo_area_save_vector. */
10094 vector = Vwith_echo_area_save_vector;
10095 Vwith_echo_area_save_vector = Qnil;
10096
10097 if (NILP (vector))
10098 vector = Fmake_vector (make_number (7), Qnil);
10099
10100 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10101 ASET (vector, i, Vdeactivate_mark); ++i;
10102 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10103
10104 if (w)
10105 {
10106 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10107 ASET (vector, i, w->buffer); ++i;
10108 ASET (vector, i, make_number (marker_position (w->pointm))); ++i;
10109 ASET (vector, i, make_number (marker_byte_position (w->pointm))); ++i;
10110 }
10111 else
10112 {
10113 int end = i + 4;
10114 for (; i < end; ++i)
10115 ASET (vector, i, Qnil);
10116 }
10117
10118 eassert (i == ASIZE (vector));
10119 return vector;
10120 }
10121
10122
10123 /* Restore global state from VECTOR which was created by
10124 with_echo_area_buffer_unwind_data. */
10125
10126 static Lisp_Object
10127 unwind_with_echo_area_buffer (Lisp_Object vector)
10128 {
10129 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10130 Vdeactivate_mark = AREF (vector, 1);
10131 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10132
10133 if (WINDOWP (AREF (vector, 3)))
10134 {
10135 struct window *w;
10136 Lisp_Object buffer, charpos, bytepos;
10137
10138 w = XWINDOW (AREF (vector, 3));
10139 buffer = AREF (vector, 4);
10140 charpos = AREF (vector, 5);
10141 bytepos = AREF (vector, 6);
10142
10143 wset_buffer (w, buffer);
10144 set_marker_both (w->pointm, buffer,
10145 XFASTINT (charpos), XFASTINT (bytepos));
10146 }
10147
10148 Vwith_echo_area_save_vector = vector;
10149 return Qnil;
10150 }
10151
10152
10153 /* Set up the echo area for use by print functions. MULTIBYTE_P
10154 non-zero means we will print multibyte. */
10155
10156 void
10157 setup_echo_area_for_printing (int multibyte_p)
10158 {
10159 /* If we can't find an echo area any more, exit. */
10160 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10161 Fkill_emacs (Qnil);
10162
10163 ensure_echo_area_buffers ();
10164
10165 if (!message_buf_print)
10166 {
10167 /* A message has been output since the last time we printed.
10168 Choose a fresh echo area buffer. */
10169 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10170 echo_area_buffer[0] = echo_buffer[1];
10171 else
10172 echo_area_buffer[0] = echo_buffer[0];
10173
10174 /* Switch to that buffer and clear it. */
10175 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10176 bset_truncate_lines (current_buffer, Qnil);
10177
10178 if (Z > BEG)
10179 {
10180 ptrdiff_t count = SPECPDL_INDEX ();
10181 specbind (Qinhibit_read_only, Qt);
10182 /* Note that undo recording is always disabled. */
10183 del_range (BEG, Z);
10184 unbind_to (count, Qnil);
10185 }
10186 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10187
10188 /* Set up the buffer for the multibyteness we need. */
10189 if (multibyte_p
10190 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10191 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10192
10193 /* Raise the frame containing the echo area. */
10194 if (minibuffer_auto_raise)
10195 {
10196 struct frame *sf = SELECTED_FRAME ();
10197 Lisp_Object mini_window;
10198 mini_window = FRAME_MINIBUF_WINDOW (sf);
10199 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10200 }
10201
10202 message_log_maybe_newline ();
10203 message_buf_print = 1;
10204 }
10205 else
10206 {
10207 if (NILP (echo_area_buffer[0]))
10208 {
10209 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10210 echo_area_buffer[0] = echo_buffer[1];
10211 else
10212 echo_area_buffer[0] = echo_buffer[0];
10213 }
10214
10215 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10216 {
10217 /* Someone switched buffers between print requests. */
10218 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10219 bset_truncate_lines (current_buffer, Qnil);
10220 }
10221 }
10222 }
10223
10224
10225 /* Display an echo area message in window W. Value is non-zero if W's
10226 height is changed. If display_last_displayed_message_p is
10227 non-zero, display the message that was last displayed, otherwise
10228 display the current message. */
10229
10230 static int
10231 display_echo_area (struct window *w)
10232 {
10233 int i, no_message_p, window_height_changed_p;
10234
10235 /* Temporarily disable garbage collections while displaying the echo
10236 area. This is done because a GC can print a message itself.
10237 That message would modify the echo area buffer's contents while a
10238 redisplay of the buffer is going on, and seriously confuse
10239 redisplay. */
10240 ptrdiff_t count = inhibit_garbage_collection ();
10241
10242 /* If there is no message, we must call display_echo_area_1
10243 nevertheless because it resizes the window. But we will have to
10244 reset the echo_area_buffer in question to nil at the end because
10245 with_echo_area_buffer will sets it to an empty buffer. */
10246 i = display_last_displayed_message_p ? 1 : 0;
10247 no_message_p = NILP (echo_area_buffer[i]);
10248
10249 window_height_changed_p
10250 = with_echo_area_buffer (w, display_last_displayed_message_p,
10251 display_echo_area_1,
10252 (intptr_t) w, Qnil, 0, 0);
10253
10254 if (no_message_p)
10255 echo_area_buffer[i] = Qnil;
10256
10257 unbind_to (count, Qnil);
10258 return window_height_changed_p;
10259 }
10260
10261
10262 /* Helper for display_echo_area. Display the current buffer which
10263 contains the current echo area message in window W, a mini-window,
10264 a pointer to which is passed in A1. A2..A4 are currently not used.
10265 Change the height of W so that all of the message is displayed.
10266 Value is non-zero if height of W was changed. */
10267
10268 static int
10269 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10270 {
10271 intptr_t i1 = a1;
10272 struct window *w = (struct window *) i1;
10273 Lisp_Object window;
10274 struct text_pos start;
10275 int window_height_changed_p = 0;
10276
10277 /* Do this before displaying, so that we have a large enough glyph
10278 matrix for the display. If we can't get enough space for the
10279 whole text, display the last N lines. That works by setting w->start. */
10280 window_height_changed_p = resize_mini_window (w, 0);
10281
10282 /* Use the starting position chosen by resize_mini_window. */
10283 SET_TEXT_POS_FROM_MARKER (start, w->start);
10284
10285 /* Display. */
10286 clear_glyph_matrix (w->desired_matrix);
10287 XSETWINDOW (window, w);
10288 try_window (window, start, 0);
10289
10290 return window_height_changed_p;
10291 }
10292
10293
10294 /* Resize the echo area window to exactly the size needed for the
10295 currently displayed message, if there is one. If a mini-buffer
10296 is active, don't shrink it. */
10297
10298 void
10299 resize_echo_area_exactly (void)
10300 {
10301 if (BUFFERP (echo_area_buffer[0])
10302 && WINDOWP (echo_area_window))
10303 {
10304 struct window *w = XWINDOW (echo_area_window);
10305 int resized_p;
10306 Lisp_Object resize_exactly;
10307
10308 if (minibuf_level == 0)
10309 resize_exactly = Qt;
10310 else
10311 resize_exactly = Qnil;
10312
10313 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10314 (intptr_t) w, resize_exactly,
10315 0, 0);
10316 if (resized_p)
10317 {
10318 ++windows_or_buffers_changed;
10319 ++update_mode_lines;
10320 redisplay_internal ();
10321 }
10322 }
10323 }
10324
10325
10326 /* Callback function for with_echo_area_buffer, when used from
10327 resize_echo_area_exactly. A1 contains a pointer to the window to
10328 resize, EXACTLY non-nil means resize the mini-window exactly to the
10329 size of the text displayed. A3 and A4 are not used. Value is what
10330 resize_mini_window returns. */
10331
10332 static int
10333 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10334 {
10335 intptr_t i1 = a1;
10336 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10337 }
10338
10339
10340 /* Resize mini-window W to fit the size of its contents. EXACT_P
10341 means size the window exactly to the size needed. Otherwise, it's
10342 only enlarged until W's buffer is empty.
10343
10344 Set W->start to the right place to begin display. If the whole
10345 contents fit, start at the beginning. Otherwise, start so as
10346 to make the end of the contents appear. This is particularly
10347 important for y-or-n-p, but seems desirable generally.
10348
10349 Value is non-zero if the window height has been changed. */
10350
10351 int
10352 resize_mini_window (struct window *w, int exact_p)
10353 {
10354 struct frame *f = XFRAME (w->frame);
10355 int window_height_changed_p = 0;
10356
10357 eassert (MINI_WINDOW_P (w));
10358
10359 /* By default, start display at the beginning. */
10360 set_marker_both (w->start, w->buffer,
10361 BUF_BEGV (XBUFFER (w->buffer)),
10362 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10363
10364 /* Don't resize windows while redisplaying a window; it would
10365 confuse redisplay functions when the size of the window they are
10366 displaying changes from under them. Such a resizing can happen,
10367 for instance, when which-func prints a long message while
10368 we are running fontification-functions. We're running these
10369 functions with safe_call which binds inhibit-redisplay to t. */
10370 if (!NILP (Vinhibit_redisplay))
10371 return 0;
10372
10373 /* Nil means don't try to resize. */
10374 if (NILP (Vresize_mini_windows)
10375 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10376 return 0;
10377
10378 if (!FRAME_MINIBUF_ONLY_P (f))
10379 {
10380 struct it it;
10381 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10382 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10383 int height;
10384 EMACS_INT max_height;
10385 int unit = FRAME_LINE_HEIGHT (f);
10386 struct text_pos start;
10387 struct buffer *old_current_buffer = NULL;
10388
10389 if (current_buffer != XBUFFER (w->buffer))
10390 {
10391 old_current_buffer = current_buffer;
10392 set_buffer_internal (XBUFFER (w->buffer));
10393 }
10394
10395 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10396
10397 /* Compute the max. number of lines specified by the user. */
10398 if (FLOATP (Vmax_mini_window_height))
10399 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10400 else if (INTEGERP (Vmax_mini_window_height))
10401 max_height = XINT (Vmax_mini_window_height);
10402 else
10403 max_height = total_height / 4;
10404
10405 /* Correct that max. height if it's bogus. */
10406 max_height = clip_to_bounds (1, max_height, total_height);
10407
10408 /* Find out the height of the text in the window. */
10409 if (it.line_wrap == TRUNCATE)
10410 height = 1;
10411 else
10412 {
10413 last_height = 0;
10414 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10415 if (it.max_ascent == 0 && it.max_descent == 0)
10416 height = it.current_y + last_height;
10417 else
10418 height = it.current_y + it.max_ascent + it.max_descent;
10419 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10420 height = (height + unit - 1) / unit;
10421 }
10422
10423 /* Compute a suitable window start. */
10424 if (height > max_height)
10425 {
10426 height = max_height;
10427 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10428 move_it_vertically_backward (&it, (height - 1) * unit);
10429 start = it.current.pos;
10430 }
10431 else
10432 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10433 SET_MARKER_FROM_TEXT_POS (w->start, start);
10434
10435 if (EQ (Vresize_mini_windows, Qgrow_only))
10436 {
10437 /* Let it grow only, until we display an empty message, in which
10438 case the window shrinks again. */
10439 if (height > WINDOW_TOTAL_LINES (w))
10440 {
10441 int old_height = WINDOW_TOTAL_LINES (w);
10442 freeze_window_starts (f, 1);
10443 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10444 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10445 }
10446 else if (height < WINDOW_TOTAL_LINES (w)
10447 && (exact_p || BEGV == ZV))
10448 {
10449 int old_height = WINDOW_TOTAL_LINES (w);
10450 freeze_window_starts (f, 0);
10451 shrink_mini_window (w);
10452 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10453 }
10454 }
10455 else
10456 {
10457 /* Always resize to exact size needed. */
10458 if (height > WINDOW_TOTAL_LINES (w))
10459 {
10460 int old_height = WINDOW_TOTAL_LINES (w);
10461 freeze_window_starts (f, 1);
10462 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10463 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10464 }
10465 else if (height < WINDOW_TOTAL_LINES (w))
10466 {
10467 int old_height = WINDOW_TOTAL_LINES (w);
10468 freeze_window_starts (f, 0);
10469 shrink_mini_window (w);
10470
10471 if (height)
10472 {
10473 freeze_window_starts (f, 1);
10474 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10475 }
10476
10477 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10478 }
10479 }
10480
10481 if (old_current_buffer)
10482 set_buffer_internal (old_current_buffer);
10483 }
10484
10485 return window_height_changed_p;
10486 }
10487
10488
10489 /* Value is the current message, a string, or nil if there is no
10490 current message. */
10491
10492 Lisp_Object
10493 current_message (void)
10494 {
10495 Lisp_Object msg;
10496
10497 if (!BUFFERP (echo_area_buffer[0]))
10498 msg = Qnil;
10499 else
10500 {
10501 with_echo_area_buffer (0, 0, current_message_1,
10502 (intptr_t) &msg, Qnil, 0, 0);
10503 if (NILP (msg))
10504 echo_area_buffer[0] = Qnil;
10505 }
10506
10507 return msg;
10508 }
10509
10510
10511 static int
10512 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10513 {
10514 intptr_t i1 = a1;
10515 Lisp_Object *msg = (Lisp_Object *) i1;
10516
10517 if (Z > BEG)
10518 *msg = make_buffer_string (BEG, Z, 1);
10519 else
10520 *msg = Qnil;
10521 return 0;
10522 }
10523
10524
10525 /* Push the current message on Vmessage_stack for later restoration
10526 by restore_message. Value is non-zero if the current message isn't
10527 empty. This is a relatively infrequent operation, so it's not
10528 worth optimizing. */
10529
10530 bool
10531 push_message (void)
10532 {
10533 Lisp_Object msg = current_message ();
10534 Vmessage_stack = Fcons (msg, Vmessage_stack);
10535 return STRINGP (msg);
10536 }
10537
10538
10539 /* Restore message display from the top of Vmessage_stack. */
10540
10541 void
10542 restore_message (void)
10543 {
10544 Lisp_Object msg;
10545
10546 eassert (CONSP (Vmessage_stack));
10547 msg = XCAR (Vmessage_stack);
10548 if (STRINGP (msg))
10549 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10550 else
10551 message3_nolog (msg, 0, 0);
10552 }
10553
10554
10555 /* Handler for record_unwind_protect calling pop_message. */
10556
10557 Lisp_Object
10558 pop_message_unwind (Lisp_Object dummy)
10559 {
10560 pop_message ();
10561 return Qnil;
10562 }
10563
10564 /* Pop the top-most entry off Vmessage_stack. */
10565
10566 static void
10567 pop_message (void)
10568 {
10569 eassert (CONSP (Vmessage_stack));
10570 Vmessage_stack = XCDR (Vmessage_stack);
10571 }
10572
10573
10574 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10575 exits. If the stack is not empty, we have a missing pop_message
10576 somewhere. */
10577
10578 void
10579 check_message_stack (void)
10580 {
10581 if (!NILP (Vmessage_stack))
10582 emacs_abort ();
10583 }
10584
10585
10586 /* Truncate to NCHARS what will be displayed in the echo area the next
10587 time we display it---but don't redisplay it now. */
10588
10589 void
10590 truncate_echo_area (ptrdiff_t nchars)
10591 {
10592 if (nchars == 0)
10593 echo_area_buffer[0] = Qnil;
10594 /* A null message buffer means that the frame hasn't really been
10595 initialized yet. Error messages get reported properly by
10596 cmd_error, so this must be just an informative message; toss it. */
10597 else if (!noninteractive
10598 && INTERACTIVE
10599 && !NILP (echo_area_buffer[0]))
10600 {
10601 struct frame *sf = SELECTED_FRAME ();
10602 if (FRAME_MESSAGE_BUF (sf))
10603 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10604 }
10605 }
10606
10607
10608 /* Helper function for truncate_echo_area. Truncate the current
10609 message to at most NCHARS characters. */
10610
10611 static int
10612 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10613 {
10614 if (BEG + nchars < Z)
10615 del_range (BEG + nchars, Z);
10616 if (Z == BEG)
10617 echo_area_buffer[0] = Qnil;
10618 return 0;
10619 }
10620
10621 /* Set the current message to a substring of S or STRING.
10622
10623 If STRING is a Lisp string, set the message to the first NBYTES
10624 bytes from STRING. NBYTES zero means use the whole string. If
10625 STRING is multibyte, the message will be displayed multibyte.
10626
10627 If S is not null, set the message to the first LEN bytes of S. LEN
10628 zero means use the whole string. MULTIBYTE_P non-zero means S is
10629 multibyte. Display the message multibyte in that case.
10630
10631 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10632 to t before calling set_message_1 (which calls insert).
10633 */
10634
10635 static void
10636 set_message (const char *s, Lisp_Object string,
10637 ptrdiff_t nbytes, int multibyte_p)
10638 {
10639 message_enable_multibyte
10640 = ((s && multibyte_p)
10641 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10642
10643 with_echo_area_buffer (0, -1, set_message_1,
10644 (intptr_t) s, string, nbytes, multibyte_p);
10645 message_buf_print = 0;
10646 help_echo_showing_p = 0;
10647
10648 if (STRINGP (Vdebug_on_message)
10649 && fast_string_match (Vdebug_on_message, string) >= 0)
10650 call_debugger (list2 (Qerror, string));
10651 }
10652
10653
10654 /* Helper function for set_message. Arguments have the same meaning
10655 as there, with A1 corresponding to S and A2 corresponding to STRING
10656 This function is called with the echo area buffer being
10657 current. */
10658
10659 static int
10660 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10661 {
10662 intptr_t i1 = a1;
10663 const char *s = (const char *) i1;
10664 const unsigned char *msg = (const unsigned char *) s;
10665 Lisp_Object string = a2;
10666
10667 /* Change multibyteness of the echo buffer appropriately. */
10668 if (message_enable_multibyte
10669 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10670 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10671
10672 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10673 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10674 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10675
10676 /* Insert new message at BEG. */
10677 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10678
10679 if (STRINGP (string))
10680 {
10681 ptrdiff_t nchars;
10682
10683 if (nbytes == 0)
10684 nbytes = SBYTES (string);
10685 nchars = string_byte_to_char (string, nbytes);
10686
10687 /* This function takes care of single/multibyte conversion. We
10688 just have to ensure that the echo area buffer has the right
10689 setting of enable_multibyte_characters. */
10690 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10691 }
10692 else if (s)
10693 {
10694 if (nbytes == 0)
10695 nbytes = strlen (s);
10696
10697 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10698 {
10699 /* Convert from multi-byte to single-byte. */
10700 ptrdiff_t i;
10701 int c, n;
10702 char work[1];
10703
10704 /* Convert a multibyte string to single-byte. */
10705 for (i = 0; i < nbytes; i += n)
10706 {
10707 c = string_char_and_length (msg + i, &n);
10708 work[0] = (ASCII_CHAR_P (c)
10709 ? c
10710 : multibyte_char_to_unibyte (c));
10711 insert_1_both (work, 1, 1, 1, 0, 0);
10712 }
10713 }
10714 else if (!multibyte_p
10715 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10716 {
10717 /* Convert from single-byte to multi-byte. */
10718 ptrdiff_t i;
10719 int c, n;
10720 unsigned char str[MAX_MULTIBYTE_LENGTH];
10721
10722 /* Convert a single-byte string to multibyte. */
10723 for (i = 0; i < nbytes; i++)
10724 {
10725 c = msg[i];
10726 MAKE_CHAR_MULTIBYTE (c);
10727 n = CHAR_STRING (c, str);
10728 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10729 }
10730 }
10731 else
10732 insert_1 (s, nbytes, 1, 0, 0);
10733 }
10734
10735 return 0;
10736 }
10737
10738
10739 /* Clear messages. CURRENT_P non-zero means clear the current
10740 message. LAST_DISPLAYED_P non-zero means clear the message
10741 last displayed. */
10742
10743 void
10744 clear_message (int current_p, int last_displayed_p)
10745 {
10746 if (current_p)
10747 {
10748 echo_area_buffer[0] = Qnil;
10749 message_cleared_p = 1;
10750 }
10751
10752 if (last_displayed_p)
10753 echo_area_buffer[1] = Qnil;
10754
10755 message_buf_print = 0;
10756 }
10757
10758 /* Clear garbaged frames.
10759
10760 This function is used where the old redisplay called
10761 redraw_garbaged_frames which in turn called redraw_frame which in
10762 turn called clear_frame. The call to clear_frame was a source of
10763 flickering. I believe a clear_frame is not necessary. It should
10764 suffice in the new redisplay to invalidate all current matrices,
10765 and ensure a complete redisplay of all windows. */
10766
10767 static void
10768 clear_garbaged_frames (void)
10769 {
10770 if (frame_garbaged)
10771 {
10772 Lisp_Object tail, frame;
10773 int changed_count = 0;
10774
10775 FOR_EACH_FRAME (tail, frame)
10776 {
10777 struct frame *f = XFRAME (frame);
10778
10779 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10780 {
10781 if (f->resized_p)
10782 {
10783 redraw_frame (f);
10784 f->force_flush_display_p = 1;
10785 }
10786 clear_current_matrices (f);
10787 changed_count++;
10788 f->garbaged = 0;
10789 f->resized_p = 0;
10790 }
10791 }
10792
10793 frame_garbaged = 0;
10794 if (changed_count)
10795 ++windows_or_buffers_changed;
10796 }
10797 }
10798
10799
10800 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10801 is non-zero update selected_frame. Value is non-zero if the
10802 mini-windows height has been changed. */
10803
10804 static int
10805 echo_area_display (int update_frame_p)
10806 {
10807 Lisp_Object mini_window;
10808 struct window *w;
10809 struct frame *f;
10810 int window_height_changed_p = 0;
10811 struct frame *sf = SELECTED_FRAME ();
10812
10813 mini_window = FRAME_MINIBUF_WINDOW (sf);
10814 w = XWINDOW (mini_window);
10815 f = XFRAME (WINDOW_FRAME (w));
10816
10817 /* Don't display if frame is invisible or not yet initialized. */
10818 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10819 return 0;
10820
10821 #ifdef HAVE_WINDOW_SYSTEM
10822 /* When Emacs starts, selected_frame may be the initial terminal
10823 frame. If we let this through, a message would be displayed on
10824 the terminal. */
10825 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10826 return 0;
10827 #endif /* HAVE_WINDOW_SYSTEM */
10828
10829 /* Redraw garbaged frames. */
10830 clear_garbaged_frames ();
10831
10832 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10833 {
10834 echo_area_window = mini_window;
10835 window_height_changed_p = display_echo_area (w);
10836 w->must_be_updated_p = 1;
10837
10838 /* Update the display, unless called from redisplay_internal.
10839 Also don't update the screen during redisplay itself. The
10840 update will happen at the end of redisplay, and an update
10841 here could cause confusion. */
10842 if (update_frame_p && !redisplaying_p)
10843 {
10844 int n = 0;
10845
10846 /* If the display update has been interrupted by pending
10847 input, update mode lines in the frame. Due to the
10848 pending input, it might have been that redisplay hasn't
10849 been called, so that mode lines above the echo area are
10850 garbaged. This looks odd, so we prevent it here. */
10851 if (!display_completed)
10852 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10853
10854 if (window_height_changed_p
10855 /* Don't do this if Emacs is shutting down. Redisplay
10856 needs to run hooks. */
10857 && !NILP (Vrun_hooks))
10858 {
10859 /* Must update other windows. Likewise as in other
10860 cases, don't let this update be interrupted by
10861 pending input. */
10862 ptrdiff_t count = SPECPDL_INDEX ();
10863 specbind (Qredisplay_dont_pause, Qt);
10864 windows_or_buffers_changed = 1;
10865 redisplay_internal ();
10866 unbind_to (count, Qnil);
10867 }
10868 else if (FRAME_WINDOW_P (f) && n == 0)
10869 {
10870 /* Window configuration is the same as before.
10871 Can do with a display update of the echo area,
10872 unless we displayed some mode lines. */
10873 update_single_window (w, 1);
10874 FRAME_RIF (f)->flush_display (f);
10875 }
10876 else
10877 update_frame (f, 1, 1);
10878
10879 /* If cursor is in the echo area, make sure that the next
10880 redisplay displays the minibuffer, so that the cursor will
10881 be replaced with what the minibuffer wants. */
10882 if (cursor_in_echo_area)
10883 ++windows_or_buffers_changed;
10884 }
10885 }
10886 else if (!EQ (mini_window, selected_window))
10887 windows_or_buffers_changed++;
10888
10889 /* Last displayed message is now the current message. */
10890 echo_area_buffer[1] = echo_area_buffer[0];
10891 /* Inform read_char that we're not echoing. */
10892 echo_message_buffer = Qnil;
10893
10894 /* Prevent redisplay optimization in redisplay_internal by resetting
10895 this_line_start_pos. This is done because the mini-buffer now
10896 displays the message instead of its buffer text. */
10897 if (EQ (mini_window, selected_window))
10898 CHARPOS (this_line_start_pos) = 0;
10899
10900 return window_height_changed_p;
10901 }
10902
10903 /* Nonzero if the current window's buffer is shown in more than one
10904 window and was modified since last redisplay. */
10905
10906 static int
10907 buffer_shared_and_changed (void)
10908 {
10909 return (buffer_window_count (current_buffer) > 1
10910 && UNCHANGED_MODIFIED < MODIFF);
10911 }
10912
10913 /* Nonzero if W doesn't reflect the actual state of current buffer due
10914 to its text or overlays change. FIXME: this may be called when
10915 XBUFFER (w->buffer) != current_buffer, which looks suspicious. */
10916
10917 static int
10918 window_outdated (struct window *w)
10919 {
10920 return (w->last_modified < MODIFF
10921 || w->last_overlay_modified < OVERLAY_MODIFF);
10922 }
10923
10924 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10925 is enabled and mark of W's buffer was changed since last W's update. */
10926
10927 static int
10928 window_buffer_changed (struct window *w)
10929 {
10930 struct buffer *b = XBUFFER (w->buffer);
10931
10932 eassert (BUFFER_LIVE_P (b));
10933
10934 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10935 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10936 != !NILP (w->region_showing)));
10937 }
10938
10939 /* Nonzero if W has %c in its mode line and mode line should be updated. */
10940
10941 static int
10942 mode_line_update_needed (struct window *w)
10943 {
10944 return (!NILP (w->column_number_displayed)
10945 && !(PT == w->last_point && !window_outdated (w))
10946 && (XFASTINT (w->column_number_displayed) != current_column ()));
10947 }
10948
10949 /***********************************************************************
10950 Mode Lines and Frame Titles
10951 ***********************************************************************/
10952
10953 /* A buffer for constructing non-propertized mode-line strings and
10954 frame titles in it; allocated from the heap in init_xdisp and
10955 resized as needed in store_mode_line_noprop_char. */
10956
10957 static char *mode_line_noprop_buf;
10958
10959 /* The buffer's end, and a current output position in it. */
10960
10961 static char *mode_line_noprop_buf_end;
10962 static char *mode_line_noprop_ptr;
10963
10964 #define MODE_LINE_NOPROP_LEN(start) \
10965 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10966
10967 static enum {
10968 MODE_LINE_DISPLAY = 0,
10969 MODE_LINE_TITLE,
10970 MODE_LINE_NOPROP,
10971 MODE_LINE_STRING
10972 } mode_line_target;
10973
10974 /* Alist that caches the results of :propertize.
10975 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
10976 static Lisp_Object mode_line_proptrans_alist;
10977
10978 /* List of strings making up the mode-line. */
10979 static Lisp_Object mode_line_string_list;
10980
10981 /* Base face property when building propertized mode line string. */
10982 static Lisp_Object mode_line_string_face;
10983 static Lisp_Object mode_line_string_face_prop;
10984
10985
10986 /* Unwind data for mode line strings */
10987
10988 static Lisp_Object Vmode_line_unwind_vector;
10989
10990 static Lisp_Object
10991 format_mode_line_unwind_data (struct frame *target_frame,
10992 struct buffer *obuf,
10993 Lisp_Object owin,
10994 int save_proptrans)
10995 {
10996 Lisp_Object vector, tmp;
10997
10998 /* Reduce consing by keeping one vector in
10999 Vwith_echo_area_save_vector. */
11000 vector = Vmode_line_unwind_vector;
11001 Vmode_line_unwind_vector = Qnil;
11002
11003 if (NILP (vector))
11004 vector = Fmake_vector (make_number (10), Qnil);
11005
11006 ASET (vector, 0, make_number (mode_line_target));
11007 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11008 ASET (vector, 2, mode_line_string_list);
11009 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11010 ASET (vector, 4, mode_line_string_face);
11011 ASET (vector, 5, mode_line_string_face_prop);
11012
11013 if (obuf)
11014 XSETBUFFER (tmp, obuf);
11015 else
11016 tmp = Qnil;
11017 ASET (vector, 6, tmp);
11018 ASET (vector, 7, owin);
11019 if (target_frame)
11020 {
11021 /* Similarly to `with-selected-window', if the operation selects
11022 a window on another frame, we must restore that frame's
11023 selected window, and (for a tty) the top-frame. */
11024 ASET (vector, 8, target_frame->selected_window);
11025 if (FRAME_TERMCAP_P (target_frame))
11026 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11027 }
11028
11029 return vector;
11030 }
11031
11032 static Lisp_Object
11033 unwind_format_mode_line (Lisp_Object vector)
11034 {
11035 Lisp_Object old_window = AREF (vector, 7);
11036 Lisp_Object target_frame_window = AREF (vector, 8);
11037 Lisp_Object old_top_frame = AREF (vector, 9);
11038
11039 mode_line_target = XINT (AREF (vector, 0));
11040 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11041 mode_line_string_list = AREF (vector, 2);
11042 if (! EQ (AREF (vector, 3), Qt))
11043 mode_line_proptrans_alist = AREF (vector, 3);
11044 mode_line_string_face = AREF (vector, 4);
11045 mode_line_string_face_prop = AREF (vector, 5);
11046
11047 /* Select window before buffer, since it may change the buffer. */
11048 if (!NILP (old_window))
11049 {
11050 /* If the operation that we are unwinding had selected a window
11051 on a different frame, reset its frame-selected-window. For a
11052 text terminal, reset its top-frame if necessary. */
11053 if (!NILP (target_frame_window))
11054 {
11055 Lisp_Object frame
11056 = WINDOW_FRAME (XWINDOW (target_frame_window));
11057
11058 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11059 Fselect_window (target_frame_window, Qt);
11060
11061 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11062 Fselect_frame (old_top_frame, Qt);
11063 }
11064
11065 Fselect_window (old_window, Qt);
11066 }
11067
11068 if (!NILP (AREF (vector, 6)))
11069 {
11070 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11071 ASET (vector, 6, Qnil);
11072 }
11073
11074 Vmode_line_unwind_vector = vector;
11075 return Qnil;
11076 }
11077
11078
11079 /* Store a single character C for the frame title in mode_line_noprop_buf.
11080 Re-allocate mode_line_noprop_buf if necessary. */
11081
11082 static void
11083 store_mode_line_noprop_char (char c)
11084 {
11085 /* If output position has reached the end of the allocated buffer,
11086 increase the buffer's size. */
11087 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11088 {
11089 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11090 ptrdiff_t size = len;
11091 mode_line_noprop_buf =
11092 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11093 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11094 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11095 }
11096
11097 *mode_line_noprop_ptr++ = c;
11098 }
11099
11100
11101 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11102 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11103 characters that yield more columns than PRECISION; PRECISION <= 0
11104 means copy the whole string. Pad with spaces until FIELD_WIDTH
11105 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11106 pad. Called from display_mode_element when it is used to build a
11107 frame title. */
11108
11109 static int
11110 store_mode_line_noprop (const char *string, int field_width, int precision)
11111 {
11112 const unsigned char *str = (const unsigned char *) string;
11113 int n = 0;
11114 ptrdiff_t dummy, nbytes;
11115
11116 /* Copy at most PRECISION chars from STR. */
11117 nbytes = strlen (string);
11118 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11119 while (nbytes--)
11120 store_mode_line_noprop_char (*str++);
11121
11122 /* Fill up with spaces until FIELD_WIDTH reached. */
11123 while (field_width > 0
11124 && n < field_width)
11125 {
11126 store_mode_line_noprop_char (' ');
11127 ++n;
11128 }
11129
11130 return n;
11131 }
11132
11133 /***********************************************************************
11134 Frame Titles
11135 ***********************************************************************/
11136
11137 #ifdef HAVE_WINDOW_SYSTEM
11138
11139 /* Set the title of FRAME, if it has changed. The title format is
11140 Vicon_title_format if FRAME is iconified, otherwise it is
11141 frame_title_format. */
11142
11143 static void
11144 x_consider_frame_title (Lisp_Object frame)
11145 {
11146 struct frame *f = XFRAME (frame);
11147
11148 if (FRAME_WINDOW_P (f)
11149 || FRAME_MINIBUF_ONLY_P (f)
11150 || f->explicit_name)
11151 {
11152 /* Do we have more than one visible frame on this X display? */
11153 Lisp_Object tail, other_frame, fmt;
11154 ptrdiff_t title_start;
11155 char *title;
11156 ptrdiff_t len;
11157 struct it it;
11158 ptrdiff_t count = SPECPDL_INDEX ();
11159
11160 FOR_EACH_FRAME (tail, other_frame)
11161 {
11162 struct frame *tf = XFRAME (other_frame);
11163
11164 if (tf != f
11165 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11166 && !FRAME_MINIBUF_ONLY_P (tf)
11167 && !EQ (other_frame, tip_frame)
11168 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11169 break;
11170 }
11171
11172 /* Set global variable indicating that multiple frames exist. */
11173 multiple_frames = CONSP (tail);
11174
11175 /* Switch to the buffer of selected window of the frame. Set up
11176 mode_line_target so that display_mode_element will output into
11177 mode_line_noprop_buf; then display the title. */
11178 record_unwind_protect (unwind_format_mode_line,
11179 format_mode_line_unwind_data
11180 (f, current_buffer, selected_window, 0));
11181
11182 Fselect_window (f->selected_window, Qt);
11183 set_buffer_internal_1
11184 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11185 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11186
11187 mode_line_target = MODE_LINE_TITLE;
11188 title_start = MODE_LINE_NOPROP_LEN (0);
11189 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11190 NULL, DEFAULT_FACE_ID);
11191 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11192 len = MODE_LINE_NOPROP_LEN (title_start);
11193 title = mode_line_noprop_buf + title_start;
11194 unbind_to (count, Qnil);
11195
11196 /* Set the title only if it's changed. This avoids consing in
11197 the common case where it hasn't. (If it turns out that we've
11198 already wasted too much time by walking through the list with
11199 display_mode_element, then we might need to optimize at a
11200 higher level than this.) */
11201 if (! STRINGP (f->name)
11202 || SBYTES (f->name) != len
11203 || memcmp (title, SDATA (f->name), len) != 0)
11204 x_implicitly_set_name (f, make_string (title, len), Qnil);
11205 }
11206 }
11207
11208 #endif /* not HAVE_WINDOW_SYSTEM */
11209
11210 \f
11211 /***********************************************************************
11212 Menu Bars
11213 ***********************************************************************/
11214
11215
11216 /* Prepare for redisplay by updating menu-bar item lists when
11217 appropriate. This can call eval. */
11218
11219 void
11220 prepare_menu_bars (void)
11221 {
11222 int all_windows;
11223 struct gcpro gcpro1, gcpro2;
11224 struct frame *f;
11225 Lisp_Object tooltip_frame;
11226
11227 #ifdef HAVE_WINDOW_SYSTEM
11228 tooltip_frame = tip_frame;
11229 #else
11230 tooltip_frame = Qnil;
11231 #endif
11232
11233 /* Update all frame titles based on their buffer names, etc. We do
11234 this before the menu bars so that the buffer-menu will show the
11235 up-to-date frame titles. */
11236 #ifdef HAVE_WINDOW_SYSTEM
11237 if (windows_or_buffers_changed || update_mode_lines)
11238 {
11239 Lisp_Object tail, frame;
11240
11241 FOR_EACH_FRAME (tail, frame)
11242 {
11243 f = XFRAME (frame);
11244 if (!EQ (frame, tooltip_frame)
11245 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11246 x_consider_frame_title (frame);
11247 }
11248 }
11249 #endif /* HAVE_WINDOW_SYSTEM */
11250
11251 /* Update the menu bar item lists, if appropriate. This has to be
11252 done before any actual redisplay or generation of display lines. */
11253 all_windows = (update_mode_lines
11254 || buffer_shared_and_changed ()
11255 || windows_or_buffers_changed);
11256 if (all_windows)
11257 {
11258 Lisp_Object tail, frame;
11259 ptrdiff_t count = SPECPDL_INDEX ();
11260 /* 1 means that update_menu_bar has run its hooks
11261 so any further calls to update_menu_bar shouldn't do so again. */
11262 int menu_bar_hooks_run = 0;
11263
11264 record_unwind_save_match_data ();
11265
11266 FOR_EACH_FRAME (tail, frame)
11267 {
11268 f = XFRAME (frame);
11269
11270 /* Ignore tooltip frame. */
11271 if (EQ (frame, tooltip_frame))
11272 continue;
11273
11274 /* If a window on this frame changed size, report that to
11275 the user and clear the size-change flag. */
11276 if (FRAME_WINDOW_SIZES_CHANGED (f))
11277 {
11278 Lisp_Object functions;
11279
11280 /* Clear flag first in case we get an error below. */
11281 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11282 functions = Vwindow_size_change_functions;
11283 GCPRO2 (tail, functions);
11284
11285 while (CONSP (functions))
11286 {
11287 if (!EQ (XCAR (functions), Qt))
11288 call1 (XCAR (functions), frame);
11289 functions = XCDR (functions);
11290 }
11291 UNGCPRO;
11292 }
11293
11294 GCPRO1 (tail);
11295 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11296 #ifdef HAVE_WINDOW_SYSTEM
11297 update_tool_bar (f, 0);
11298 #endif
11299 #ifdef HAVE_NS
11300 if (windows_or_buffers_changed
11301 && FRAME_NS_P (f))
11302 ns_set_doc_edited
11303 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->buffer));
11304 #endif
11305 UNGCPRO;
11306 }
11307
11308 unbind_to (count, Qnil);
11309 }
11310 else
11311 {
11312 struct frame *sf = SELECTED_FRAME ();
11313 update_menu_bar (sf, 1, 0);
11314 #ifdef HAVE_WINDOW_SYSTEM
11315 update_tool_bar (sf, 1);
11316 #endif
11317 }
11318 }
11319
11320
11321 /* Update the menu bar item list for frame F. This has to be done
11322 before we start to fill in any display lines, because it can call
11323 eval.
11324
11325 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11326
11327 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11328 already ran the menu bar hooks for this redisplay, so there
11329 is no need to run them again. The return value is the
11330 updated value of this flag, to pass to the next call. */
11331
11332 static int
11333 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11334 {
11335 Lisp_Object window;
11336 register struct window *w;
11337
11338 /* If called recursively during a menu update, do nothing. This can
11339 happen when, for instance, an activate-menubar-hook causes a
11340 redisplay. */
11341 if (inhibit_menubar_update)
11342 return hooks_run;
11343
11344 window = FRAME_SELECTED_WINDOW (f);
11345 w = XWINDOW (window);
11346
11347 if (FRAME_WINDOW_P (f)
11348 ?
11349 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11350 || defined (HAVE_NS) || defined (USE_GTK)
11351 FRAME_EXTERNAL_MENU_BAR (f)
11352 #else
11353 FRAME_MENU_BAR_LINES (f) > 0
11354 #endif
11355 : FRAME_MENU_BAR_LINES (f) > 0)
11356 {
11357 /* If the user has switched buffers or windows, we need to
11358 recompute to reflect the new bindings. But we'll
11359 recompute when update_mode_lines is set too; that means
11360 that people can use force-mode-line-update to request
11361 that the menu bar be recomputed. The adverse effect on
11362 the rest of the redisplay algorithm is about the same as
11363 windows_or_buffers_changed anyway. */
11364 if (windows_or_buffers_changed
11365 /* This used to test w->update_mode_line, but we believe
11366 there is no need to recompute the menu in that case. */
11367 || update_mode_lines
11368 || window_buffer_changed (w))
11369 {
11370 struct buffer *prev = current_buffer;
11371 ptrdiff_t count = SPECPDL_INDEX ();
11372
11373 specbind (Qinhibit_menubar_update, Qt);
11374
11375 set_buffer_internal_1 (XBUFFER (w->buffer));
11376 if (save_match_data)
11377 record_unwind_save_match_data ();
11378 if (NILP (Voverriding_local_map_menu_flag))
11379 {
11380 specbind (Qoverriding_terminal_local_map, Qnil);
11381 specbind (Qoverriding_local_map, Qnil);
11382 }
11383
11384 if (!hooks_run)
11385 {
11386 /* Run the Lucid hook. */
11387 safe_run_hooks (Qactivate_menubar_hook);
11388
11389 /* If it has changed current-menubar from previous value,
11390 really recompute the menu-bar from the value. */
11391 if (! NILP (Vlucid_menu_bar_dirty_flag))
11392 call0 (Qrecompute_lucid_menubar);
11393
11394 safe_run_hooks (Qmenu_bar_update_hook);
11395
11396 hooks_run = 1;
11397 }
11398
11399 XSETFRAME (Vmenu_updating_frame, f);
11400 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11401
11402 /* Redisplay the menu bar in case we changed it. */
11403 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11404 || defined (HAVE_NS) || defined (USE_GTK)
11405 if (FRAME_WINDOW_P (f))
11406 {
11407 #if defined (HAVE_NS)
11408 /* All frames on Mac OS share the same menubar. So only
11409 the selected frame should be allowed to set it. */
11410 if (f == SELECTED_FRAME ())
11411 #endif
11412 set_frame_menubar (f, 0, 0);
11413 }
11414 else
11415 /* On a terminal screen, the menu bar is an ordinary screen
11416 line, and this makes it get updated. */
11417 w->update_mode_line = 1;
11418 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11419 /* In the non-toolkit version, the menu bar is an ordinary screen
11420 line, and this makes it get updated. */
11421 w->update_mode_line = 1;
11422 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11423
11424 unbind_to (count, Qnil);
11425 set_buffer_internal_1 (prev);
11426 }
11427 }
11428
11429 return hooks_run;
11430 }
11431
11432
11433 \f
11434 /***********************************************************************
11435 Output Cursor
11436 ***********************************************************************/
11437
11438 #ifdef HAVE_WINDOW_SYSTEM
11439
11440 /* EXPORT:
11441 Nominal cursor position -- where to draw output.
11442 HPOS and VPOS are window relative glyph matrix coordinates.
11443 X and Y are window relative pixel coordinates. */
11444
11445 struct cursor_pos output_cursor;
11446
11447
11448 /* EXPORT:
11449 Set the global variable output_cursor to CURSOR. All cursor
11450 positions are relative to updated_window. */
11451
11452 void
11453 set_output_cursor (struct cursor_pos *cursor)
11454 {
11455 output_cursor.hpos = cursor->hpos;
11456 output_cursor.vpos = cursor->vpos;
11457 output_cursor.x = cursor->x;
11458 output_cursor.y = cursor->y;
11459 }
11460
11461
11462 /* EXPORT for RIF:
11463 Set a nominal cursor position.
11464
11465 HPOS and VPOS are column/row positions in a window glyph matrix. X
11466 and Y are window text area relative pixel positions.
11467
11468 If this is done during an update, updated_window will contain the
11469 window that is being updated and the position is the future output
11470 cursor position for that window. If updated_window is null, use
11471 selected_window and display the cursor at the given position. */
11472
11473 void
11474 x_cursor_to (int vpos, int hpos, int y, int x)
11475 {
11476 struct window *w;
11477
11478 /* If updated_window is not set, work on selected_window. */
11479 if (updated_window)
11480 w = updated_window;
11481 else
11482 w = XWINDOW (selected_window);
11483
11484 /* Set the output cursor. */
11485 output_cursor.hpos = hpos;
11486 output_cursor.vpos = vpos;
11487 output_cursor.x = x;
11488 output_cursor.y = y;
11489
11490 /* If not called as part of an update, really display the cursor.
11491 This will also set the cursor position of W. */
11492 if (updated_window == NULL)
11493 {
11494 block_input ();
11495 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11496 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11497 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11498 unblock_input ();
11499 }
11500 }
11501
11502 #endif /* HAVE_WINDOW_SYSTEM */
11503
11504 \f
11505 /***********************************************************************
11506 Tool-bars
11507 ***********************************************************************/
11508
11509 #ifdef HAVE_WINDOW_SYSTEM
11510
11511 /* Where the mouse was last time we reported a mouse event. */
11512
11513 FRAME_PTR last_mouse_frame;
11514
11515 /* Tool-bar item index of the item on which a mouse button was pressed
11516 or -1. */
11517
11518 int last_tool_bar_item;
11519
11520 /* Select `frame' temporarily without running all the code in
11521 do_switch_frame.
11522 FIXME: Maybe do_switch_frame should be trimmed down similarly
11523 when `norecord' is set. */
11524 static Lisp_Object
11525 fast_set_selected_frame (Lisp_Object frame)
11526 {
11527 if (!EQ (selected_frame, frame))
11528 {
11529 selected_frame = frame;
11530 selected_window = XFRAME (frame)->selected_window;
11531 }
11532 return Qnil;
11533 }
11534
11535 /* Update the tool-bar item list for frame F. This has to be done
11536 before we start to fill in any display lines. Called from
11537 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11538 and restore it here. */
11539
11540 static void
11541 update_tool_bar (struct frame *f, int save_match_data)
11542 {
11543 #if defined (USE_GTK) || defined (HAVE_NS)
11544 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11545 #else
11546 int do_update = WINDOWP (f->tool_bar_window)
11547 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11548 #endif
11549
11550 if (do_update)
11551 {
11552 Lisp_Object window;
11553 struct window *w;
11554
11555 window = FRAME_SELECTED_WINDOW (f);
11556 w = XWINDOW (window);
11557
11558 /* If the user has switched buffers or windows, we need to
11559 recompute to reflect the new bindings. But we'll
11560 recompute when update_mode_lines is set too; that means
11561 that people can use force-mode-line-update to request
11562 that the menu bar be recomputed. The adverse effect on
11563 the rest of the redisplay algorithm is about the same as
11564 windows_or_buffers_changed anyway. */
11565 if (windows_or_buffers_changed
11566 || w->update_mode_line
11567 || update_mode_lines
11568 || window_buffer_changed (w))
11569 {
11570 struct buffer *prev = current_buffer;
11571 ptrdiff_t count = SPECPDL_INDEX ();
11572 Lisp_Object frame, new_tool_bar;
11573 int new_n_tool_bar;
11574 struct gcpro gcpro1;
11575
11576 /* Set current_buffer to the buffer of the selected
11577 window of the frame, so that we get the right local
11578 keymaps. */
11579 set_buffer_internal_1 (XBUFFER (w->buffer));
11580
11581 /* Save match data, if we must. */
11582 if (save_match_data)
11583 record_unwind_save_match_data ();
11584
11585 /* Make sure that we don't accidentally use bogus keymaps. */
11586 if (NILP (Voverriding_local_map_menu_flag))
11587 {
11588 specbind (Qoverriding_terminal_local_map, Qnil);
11589 specbind (Qoverriding_local_map, Qnil);
11590 }
11591
11592 GCPRO1 (new_tool_bar);
11593
11594 /* We must temporarily set the selected frame to this frame
11595 before calling tool_bar_items, because the calculation of
11596 the tool-bar keymap uses the selected frame (see
11597 `tool-bar-make-keymap' in tool-bar.el). */
11598 eassert (EQ (selected_window,
11599 /* Since we only explicitly preserve selected_frame,
11600 check that selected_window would be redundant. */
11601 XFRAME (selected_frame)->selected_window));
11602 record_unwind_protect (fast_set_selected_frame, selected_frame);
11603 XSETFRAME (frame, f);
11604 fast_set_selected_frame (frame);
11605
11606 /* Build desired tool-bar items from keymaps. */
11607 new_tool_bar
11608 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11609 &new_n_tool_bar);
11610
11611 /* Redisplay the tool-bar if we changed it. */
11612 if (new_n_tool_bar != f->n_tool_bar_items
11613 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11614 {
11615 /* Redisplay that happens asynchronously due to an expose event
11616 may access f->tool_bar_items. Make sure we update both
11617 variables within BLOCK_INPUT so no such event interrupts. */
11618 block_input ();
11619 fset_tool_bar_items (f, new_tool_bar);
11620 f->n_tool_bar_items = new_n_tool_bar;
11621 w->update_mode_line = 1;
11622 unblock_input ();
11623 }
11624
11625 UNGCPRO;
11626
11627 unbind_to (count, Qnil);
11628 set_buffer_internal_1 (prev);
11629 }
11630 }
11631 }
11632
11633
11634 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11635 F's desired tool-bar contents. F->tool_bar_items must have
11636 been set up previously by calling prepare_menu_bars. */
11637
11638 static void
11639 build_desired_tool_bar_string (struct frame *f)
11640 {
11641 int i, size, size_needed;
11642 struct gcpro gcpro1, gcpro2, gcpro3;
11643 Lisp_Object image, plist, props;
11644
11645 image = plist = props = Qnil;
11646 GCPRO3 (image, plist, props);
11647
11648 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11649 Otherwise, make a new string. */
11650
11651 /* The size of the string we might be able to reuse. */
11652 size = (STRINGP (f->desired_tool_bar_string)
11653 ? SCHARS (f->desired_tool_bar_string)
11654 : 0);
11655
11656 /* We need one space in the string for each image. */
11657 size_needed = f->n_tool_bar_items;
11658
11659 /* Reuse f->desired_tool_bar_string, if possible. */
11660 if (size < size_needed || NILP (f->desired_tool_bar_string))
11661 fset_desired_tool_bar_string
11662 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11663 else
11664 {
11665 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11666 Fremove_text_properties (make_number (0), make_number (size),
11667 props, f->desired_tool_bar_string);
11668 }
11669
11670 /* Put a `display' property on the string for the images to display,
11671 put a `menu_item' property on tool-bar items with a value that
11672 is the index of the item in F's tool-bar item vector. */
11673 for (i = 0; i < f->n_tool_bar_items; ++i)
11674 {
11675 #define PROP(IDX) \
11676 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11677
11678 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11679 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11680 int hmargin, vmargin, relief, idx, end;
11681
11682 /* If image is a vector, choose the image according to the
11683 button state. */
11684 image = PROP (TOOL_BAR_ITEM_IMAGES);
11685 if (VECTORP (image))
11686 {
11687 if (enabled_p)
11688 idx = (selected_p
11689 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11690 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11691 else
11692 idx = (selected_p
11693 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11694 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11695
11696 eassert (ASIZE (image) >= idx);
11697 image = AREF (image, idx);
11698 }
11699 else
11700 idx = -1;
11701
11702 /* Ignore invalid image specifications. */
11703 if (!valid_image_p (image))
11704 continue;
11705
11706 /* Display the tool-bar button pressed, or depressed. */
11707 plist = Fcopy_sequence (XCDR (image));
11708
11709 /* Compute margin and relief to draw. */
11710 relief = (tool_bar_button_relief >= 0
11711 ? tool_bar_button_relief
11712 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11713 hmargin = vmargin = relief;
11714
11715 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11716 INT_MAX - max (hmargin, vmargin)))
11717 {
11718 hmargin += XFASTINT (Vtool_bar_button_margin);
11719 vmargin += XFASTINT (Vtool_bar_button_margin);
11720 }
11721 else if (CONSP (Vtool_bar_button_margin))
11722 {
11723 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11724 INT_MAX - hmargin))
11725 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11726
11727 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11728 INT_MAX - vmargin))
11729 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11730 }
11731
11732 if (auto_raise_tool_bar_buttons_p)
11733 {
11734 /* Add a `:relief' property to the image spec if the item is
11735 selected. */
11736 if (selected_p)
11737 {
11738 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11739 hmargin -= relief;
11740 vmargin -= relief;
11741 }
11742 }
11743 else
11744 {
11745 /* If image is selected, display it pressed, i.e. with a
11746 negative relief. If it's not selected, display it with a
11747 raised relief. */
11748 plist = Fplist_put (plist, QCrelief,
11749 (selected_p
11750 ? make_number (-relief)
11751 : make_number (relief)));
11752 hmargin -= relief;
11753 vmargin -= relief;
11754 }
11755
11756 /* Put a margin around the image. */
11757 if (hmargin || vmargin)
11758 {
11759 if (hmargin == vmargin)
11760 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11761 else
11762 plist = Fplist_put (plist, QCmargin,
11763 Fcons (make_number (hmargin),
11764 make_number (vmargin)));
11765 }
11766
11767 /* If button is not enabled, and we don't have special images
11768 for the disabled state, make the image appear disabled by
11769 applying an appropriate algorithm to it. */
11770 if (!enabled_p && idx < 0)
11771 plist = Fplist_put (plist, QCconversion, Qdisabled);
11772
11773 /* Put a `display' text property on the string for the image to
11774 display. Put a `menu-item' property on the string that gives
11775 the start of this item's properties in the tool-bar items
11776 vector. */
11777 image = Fcons (Qimage, plist);
11778 props = list4 (Qdisplay, image,
11779 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11780
11781 /* Let the last image hide all remaining spaces in the tool bar
11782 string. The string can be longer than needed when we reuse a
11783 previous string. */
11784 if (i + 1 == f->n_tool_bar_items)
11785 end = SCHARS (f->desired_tool_bar_string);
11786 else
11787 end = i + 1;
11788 Fadd_text_properties (make_number (i), make_number (end),
11789 props, f->desired_tool_bar_string);
11790 #undef PROP
11791 }
11792
11793 UNGCPRO;
11794 }
11795
11796
11797 /* Display one line of the tool-bar of frame IT->f.
11798
11799 HEIGHT specifies the desired height of the tool-bar line.
11800 If the actual height of the glyph row is less than HEIGHT, the
11801 row's height is increased to HEIGHT, and the icons are centered
11802 vertically in the new height.
11803
11804 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11805 count a final empty row in case the tool-bar width exactly matches
11806 the window width.
11807 */
11808
11809 static void
11810 display_tool_bar_line (struct it *it, int height)
11811 {
11812 struct glyph_row *row = it->glyph_row;
11813 int max_x = it->last_visible_x;
11814 struct glyph *last;
11815
11816 prepare_desired_row (row);
11817 row->y = it->current_y;
11818
11819 /* Note that this isn't made use of if the face hasn't a box,
11820 so there's no need to check the face here. */
11821 it->start_of_box_run_p = 1;
11822
11823 while (it->current_x < max_x)
11824 {
11825 int x, n_glyphs_before, i, nglyphs;
11826 struct it it_before;
11827
11828 /* Get the next display element. */
11829 if (!get_next_display_element (it))
11830 {
11831 /* Don't count empty row if we are counting needed tool-bar lines. */
11832 if (height < 0 && !it->hpos)
11833 return;
11834 break;
11835 }
11836
11837 /* Produce glyphs. */
11838 n_glyphs_before = row->used[TEXT_AREA];
11839 it_before = *it;
11840
11841 PRODUCE_GLYPHS (it);
11842
11843 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11844 i = 0;
11845 x = it_before.current_x;
11846 while (i < nglyphs)
11847 {
11848 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11849
11850 if (x + glyph->pixel_width > max_x)
11851 {
11852 /* Glyph doesn't fit on line. Backtrack. */
11853 row->used[TEXT_AREA] = n_glyphs_before;
11854 *it = it_before;
11855 /* If this is the only glyph on this line, it will never fit on the
11856 tool-bar, so skip it. But ensure there is at least one glyph,
11857 so we don't accidentally disable the tool-bar. */
11858 if (n_glyphs_before == 0
11859 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11860 break;
11861 goto out;
11862 }
11863
11864 ++it->hpos;
11865 x += glyph->pixel_width;
11866 ++i;
11867 }
11868
11869 /* Stop at line end. */
11870 if (ITERATOR_AT_END_OF_LINE_P (it))
11871 break;
11872
11873 set_iterator_to_next (it, 1);
11874 }
11875
11876 out:;
11877
11878 row->displays_text_p = row->used[TEXT_AREA] != 0;
11879
11880 /* Use default face for the border below the tool bar.
11881
11882 FIXME: When auto-resize-tool-bars is grow-only, there is
11883 no additional border below the possibly empty tool-bar lines.
11884 So to make the extra empty lines look "normal", we have to
11885 use the tool-bar face for the border too. */
11886 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11887 it->face_id = DEFAULT_FACE_ID;
11888
11889 extend_face_to_end_of_line (it);
11890 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11891 last->right_box_line_p = 1;
11892 if (last == row->glyphs[TEXT_AREA])
11893 last->left_box_line_p = 1;
11894
11895 /* Make line the desired height and center it vertically. */
11896 if ((height -= it->max_ascent + it->max_descent) > 0)
11897 {
11898 /* Don't add more than one line height. */
11899 height %= FRAME_LINE_HEIGHT (it->f);
11900 it->max_ascent += height / 2;
11901 it->max_descent += (height + 1) / 2;
11902 }
11903
11904 compute_line_metrics (it);
11905
11906 /* If line is empty, make it occupy the rest of the tool-bar. */
11907 if (!row->displays_text_p)
11908 {
11909 row->height = row->phys_height = it->last_visible_y - row->y;
11910 row->visible_height = row->height;
11911 row->ascent = row->phys_ascent = 0;
11912 row->extra_line_spacing = 0;
11913 }
11914
11915 row->full_width_p = 1;
11916 row->continued_p = 0;
11917 row->truncated_on_left_p = 0;
11918 row->truncated_on_right_p = 0;
11919
11920 it->current_x = it->hpos = 0;
11921 it->current_y += row->height;
11922 ++it->vpos;
11923 ++it->glyph_row;
11924 }
11925
11926
11927 /* Max tool-bar height. */
11928
11929 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11930 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11931
11932 /* Value is the number of screen lines needed to make all tool-bar
11933 items of frame F visible. The number of actual rows needed is
11934 returned in *N_ROWS if non-NULL. */
11935
11936 static int
11937 tool_bar_lines_needed (struct frame *f, int *n_rows)
11938 {
11939 struct window *w = XWINDOW (f->tool_bar_window);
11940 struct it it;
11941 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11942 the desired matrix, so use (unused) mode-line row as temporary row to
11943 avoid destroying the first tool-bar row. */
11944 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11945
11946 /* Initialize an iterator for iteration over
11947 F->desired_tool_bar_string in the tool-bar window of frame F. */
11948 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11949 it.first_visible_x = 0;
11950 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11951 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11952 it.paragraph_embedding = L2R;
11953
11954 while (!ITERATOR_AT_END_P (&it))
11955 {
11956 clear_glyph_row (temp_row);
11957 it.glyph_row = temp_row;
11958 display_tool_bar_line (&it, -1);
11959 }
11960 clear_glyph_row (temp_row);
11961
11962 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11963 if (n_rows)
11964 *n_rows = it.vpos > 0 ? it.vpos : -1;
11965
11966 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11967 }
11968
11969
11970 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
11971 0, 1, 0,
11972 doc: /* Return the number of lines occupied by the tool bar of FRAME.
11973 If FRAME is nil or omitted, use the selected frame. */)
11974 (Lisp_Object frame)
11975 {
11976 struct frame *f = decode_any_frame (frame);
11977 struct window *w;
11978 int nlines = 0;
11979
11980 if (WINDOWP (f->tool_bar_window)
11981 && (w = XWINDOW (f->tool_bar_window),
11982 WINDOW_TOTAL_LINES (w) > 0))
11983 {
11984 update_tool_bar (f, 1);
11985 if (f->n_tool_bar_items)
11986 {
11987 build_desired_tool_bar_string (f);
11988 nlines = tool_bar_lines_needed (f, NULL);
11989 }
11990 }
11991
11992 return make_number (nlines);
11993 }
11994
11995
11996 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
11997 height should be changed. */
11998
11999 static int
12000 redisplay_tool_bar (struct frame *f)
12001 {
12002 struct window *w;
12003 struct it it;
12004 struct glyph_row *row;
12005
12006 #if defined (USE_GTK) || defined (HAVE_NS)
12007 if (FRAME_EXTERNAL_TOOL_BAR (f))
12008 update_frame_tool_bar (f);
12009 return 0;
12010 #endif
12011
12012 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12013 do anything. This means you must start with tool-bar-lines
12014 non-zero to get the auto-sizing effect. Or in other words, you
12015 can turn off tool-bars by specifying tool-bar-lines zero. */
12016 if (!WINDOWP (f->tool_bar_window)
12017 || (w = XWINDOW (f->tool_bar_window),
12018 WINDOW_TOTAL_LINES (w) == 0))
12019 return 0;
12020
12021 /* Set up an iterator for the tool-bar window. */
12022 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12023 it.first_visible_x = 0;
12024 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
12025 row = it.glyph_row;
12026
12027 /* Build a string that represents the contents of the tool-bar. */
12028 build_desired_tool_bar_string (f);
12029 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12030 /* FIXME: This should be controlled by a user option. But it
12031 doesn't make sense to have an R2L tool bar if the menu bar cannot
12032 be drawn also R2L, and making the menu bar R2L is tricky due
12033 toolkit-specific code that implements it. If an R2L tool bar is
12034 ever supported, display_tool_bar_line should also be augmented to
12035 call unproduce_glyphs like display_line and display_string
12036 do. */
12037 it.paragraph_embedding = L2R;
12038
12039 if (f->n_tool_bar_rows == 0)
12040 {
12041 int nlines;
12042
12043 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
12044 nlines != WINDOW_TOTAL_LINES (w)))
12045 {
12046 Lisp_Object frame;
12047 int old_height = WINDOW_TOTAL_LINES (w);
12048
12049 XSETFRAME (frame, f);
12050 Fmodify_frame_parameters (frame,
12051 Fcons (Fcons (Qtool_bar_lines,
12052 make_number (nlines)),
12053 Qnil));
12054 if (WINDOW_TOTAL_LINES (w) != old_height)
12055 {
12056 clear_glyph_matrix (w->desired_matrix);
12057 fonts_changed_p = 1;
12058 return 1;
12059 }
12060 }
12061 }
12062
12063 /* Display as many lines as needed to display all tool-bar items. */
12064
12065 if (f->n_tool_bar_rows > 0)
12066 {
12067 int border, rows, height, extra;
12068
12069 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12070 border = XINT (Vtool_bar_border);
12071 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12072 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12073 else if (EQ (Vtool_bar_border, Qborder_width))
12074 border = f->border_width;
12075 else
12076 border = 0;
12077 if (border < 0)
12078 border = 0;
12079
12080 rows = f->n_tool_bar_rows;
12081 height = max (1, (it.last_visible_y - border) / rows);
12082 extra = it.last_visible_y - border - height * rows;
12083
12084 while (it.current_y < it.last_visible_y)
12085 {
12086 int h = 0;
12087 if (extra > 0 && rows-- > 0)
12088 {
12089 h = (extra + rows - 1) / rows;
12090 extra -= h;
12091 }
12092 display_tool_bar_line (&it, height + h);
12093 }
12094 }
12095 else
12096 {
12097 while (it.current_y < it.last_visible_y)
12098 display_tool_bar_line (&it, 0);
12099 }
12100
12101 /* It doesn't make much sense to try scrolling in the tool-bar
12102 window, so don't do it. */
12103 w->desired_matrix->no_scrolling_p = 1;
12104 w->must_be_updated_p = 1;
12105
12106 if (!NILP (Vauto_resize_tool_bars))
12107 {
12108 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12109 int change_height_p = 0;
12110
12111 /* If we couldn't display everything, change the tool-bar's
12112 height if there is room for more. */
12113 if (IT_STRING_CHARPOS (it) < it.end_charpos
12114 && it.current_y < max_tool_bar_height)
12115 change_height_p = 1;
12116
12117 row = it.glyph_row - 1;
12118
12119 /* If there are blank lines at the end, except for a partially
12120 visible blank line at the end that is smaller than
12121 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12122 if (!row->displays_text_p
12123 && row->height >= FRAME_LINE_HEIGHT (f))
12124 change_height_p = 1;
12125
12126 /* If row displays tool-bar items, but is partially visible,
12127 change the tool-bar's height. */
12128 if (row->displays_text_p
12129 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12130 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12131 change_height_p = 1;
12132
12133 /* Resize windows as needed by changing the `tool-bar-lines'
12134 frame parameter. */
12135 if (change_height_p)
12136 {
12137 Lisp_Object frame;
12138 int old_height = WINDOW_TOTAL_LINES (w);
12139 int nrows;
12140 int nlines = tool_bar_lines_needed (f, &nrows);
12141
12142 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12143 && !f->minimize_tool_bar_window_p)
12144 ? (nlines > old_height)
12145 : (nlines != old_height));
12146 f->minimize_tool_bar_window_p = 0;
12147
12148 if (change_height_p)
12149 {
12150 XSETFRAME (frame, f);
12151 Fmodify_frame_parameters (frame,
12152 Fcons (Fcons (Qtool_bar_lines,
12153 make_number (nlines)),
12154 Qnil));
12155 if (WINDOW_TOTAL_LINES (w) != old_height)
12156 {
12157 clear_glyph_matrix (w->desired_matrix);
12158 f->n_tool_bar_rows = nrows;
12159 fonts_changed_p = 1;
12160 return 1;
12161 }
12162 }
12163 }
12164 }
12165
12166 f->minimize_tool_bar_window_p = 0;
12167 return 0;
12168 }
12169
12170
12171 /* Get information about the tool-bar item which is displayed in GLYPH
12172 on frame F. Return in *PROP_IDX the index where tool-bar item
12173 properties start in F->tool_bar_items. Value is zero if
12174 GLYPH doesn't display a tool-bar item. */
12175
12176 static int
12177 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12178 {
12179 Lisp_Object prop;
12180 int success_p;
12181 int charpos;
12182
12183 /* This function can be called asynchronously, which means we must
12184 exclude any possibility that Fget_text_property signals an
12185 error. */
12186 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12187 charpos = max (0, charpos);
12188
12189 /* Get the text property `menu-item' at pos. The value of that
12190 property is the start index of this item's properties in
12191 F->tool_bar_items. */
12192 prop = Fget_text_property (make_number (charpos),
12193 Qmenu_item, f->current_tool_bar_string);
12194 if (INTEGERP (prop))
12195 {
12196 *prop_idx = XINT (prop);
12197 success_p = 1;
12198 }
12199 else
12200 success_p = 0;
12201
12202 return success_p;
12203 }
12204
12205 \f
12206 /* Get information about the tool-bar item at position X/Y on frame F.
12207 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12208 the current matrix of the tool-bar window of F, or NULL if not
12209 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12210 item in F->tool_bar_items. Value is
12211
12212 -1 if X/Y is not on a tool-bar item
12213 0 if X/Y is on the same item that was highlighted before.
12214 1 otherwise. */
12215
12216 static int
12217 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12218 int *hpos, int *vpos, int *prop_idx)
12219 {
12220 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12221 struct window *w = XWINDOW (f->tool_bar_window);
12222 int area;
12223
12224 /* Find the glyph under X/Y. */
12225 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12226 if (*glyph == NULL)
12227 return -1;
12228
12229 /* Get the start of this tool-bar item's properties in
12230 f->tool_bar_items. */
12231 if (!tool_bar_item_info (f, *glyph, prop_idx))
12232 return -1;
12233
12234 /* Is mouse on the highlighted item? */
12235 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12236 && *vpos >= hlinfo->mouse_face_beg_row
12237 && *vpos <= hlinfo->mouse_face_end_row
12238 && (*vpos > hlinfo->mouse_face_beg_row
12239 || *hpos >= hlinfo->mouse_face_beg_col)
12240 && (*vpos < hlinfo->mouse_face_end_row
12241 || *hpos < hlinfo->mouse_face_end_col
12242 || hlinfo->mouse_face_past_end))
12243 return 0;
12244
12245 return 1;
12246 }
12247
12248
12249 /* EXPORT:
12250 Handle mouse button event on the tool-bar of frame F, at
12251 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12252 0 for button release. MODIFIERS is event modifiers for button
12253 release. */
12254
12255 void
12256 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12257 int modifiers)
12258 {
12259 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12260 struct window *w = XWINDOW (f->tool_bar_window);
12261 int hpos, vpos, prop_idx;
12262 struct glyph *glyph;
12263 Lisp_Object enabled_p;
12264
12265 /* If not on the highlighted tool-bar item, return. */
12266 frame_to_window_pixel_xy (w, &x, &y);
12267 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12268 return;
12269
12270 /* If item is disabled, do nothing. */
12271 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12272 if (NILP (enabled_p))
12273 return;
12274
12275 if (down_p)
12276 {
12277 /* Show item in pressed state. */
12278 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12279 last_tool_bar_item = prop_idx;
12280 }
12281 else
12282 {
12283 Lisp_Object key, frame;
12284 struct input_event event;
12285 EVENT_INIT (event);
12286
12287 /* Show item in released state. */
12288 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12289
12290 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12291
12292 XSETFRAME (frame, f);
12293 event.kind = TOOL_BAR_EVENT;
12294 event.frame_or_window = frame;
12295 event.arg = frame;
12296 kbd_buffer_store_event (&event);
12297
12298 event.kind = TOOL_BAR_EVENT;
12299 event.frame_or_window = frame;
12300 event.arg = key;
12301 event.modifiers = modifiers;
12302 kbd_buffer_store_event (&event);
12303 last_tool_bar_item = -1;
12304 }
12305 }
12306
12307
12308 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12309 tool-bar window-relative coordinates X/Y. Called from
12310 note_mouse_highlight. */
12311
12312 static void
12313 note_tool_bar_highlight (struct frame *f, int x, int y)
12314 {
12315 Lisp_Object window = f->tool_bar_window;
12316 struct window *w = XWINDOW (window);
12317 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12318 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12319 int hpos, vpos;
12320 struct glyph *glyph;
12321 struct glyph_row *row;
12322 int i;
12323 Lisp_Object enabled_p;
12324 int prop_idx;
12325 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12326 int mouse_down_p, rc;
12327
12328 /* Function note_mouse_highlight is called with negative X/Y
12329 values when mouse moves outside of the frame. */
12330 if (x <= 0 || y <= 0)
12331 {
12332 clear_mouse_face (hlinfo);
12333 return;
12334 }
12335
12336 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12337 if (rc < 0)
12338 {
12339 /* Not on tool-bar item. */
12340 clear_mouse_face (hlinfo);
12341 return;
12342 }
12343 else if (rc == 0)
12344 /* On same tool-bar item as before. */
12345 goto set_help_echo;
12346
12347 clear_mouse_face (hlinfo);
12348
12349 /* Mouse is down, but on different tool-bar item? */
12350 mouse_down_p = (dpyinfo->grabbed
12351 && f == last_mouse_frame
12352 && FRAME_LIVE_P (f));
12353 if (mouse_down_p
12354 && last_tool_bar_item != prop_idx)
12355 return;
12356
12357 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12358
12359 /* If tool-bar item is not enabled, don't highlight it. */
12360 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12361 if (!NILP (enabled_p))
12362 {
12363 /* Compute the x-position of the glyph. In front and past the
12364 image is a space. We include this in the highlighted area. */
12365 row = MATRIX_ROW (w->current_matrix, vpos);
12366 for (i = x = 0; i < hpos; ++i)
12367 x += row->glyphs[TEXT_AREA][i].pixel_width;
12368
12369 /* Record this as the current active region. */
12370 hlinfo->mouse_face_beg_col = hpos;
12371 hlinfo->mouse_face_beg_row = vpos;
12372 hlinfo->mouse_face_beg_x = x;
12373 hlinfo->mouse_face_beg_y = row->y;
12374 hlinfo->mouse_face_past_end = 0;
12375
12376 hlinfo->mouse_face_end_col = hpos + 1;
12377 hlinfo->mouse_face_end_row = vpos;
12378 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12379 hlinfo->mouse_face_end_y = row->y;
12380 hlinfo->mouse_face_window = window;
12381 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12382
12383 /* Display it as active. */
12384 show_mouse_face (hlinfo, draw);
12385 }
12386
12387 set_help_echo:
12388
12389 /* Set help_echo_string to a help string to display for this tool-bar item.
12390 XTread_socket does the rest. */
12391 help_echo_object = help_echo_window = Qnil;
12392 help_echo_pos = -1;
12393 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12394 if (NILP (help_echo_string))
12395 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12396 }
12397
12398 #endif /* HAVE_WINDOW_SYSTEM */
12399
12400
12401 \f
12402 /************************************************************************
12403 Horizontal scrolling
12404 ************************************************************************/
12405
12406 static int hscroll_window_tree (Lisp_Object);
12407 static int hscroll_windows (Lisp_Object);
12408
12409 /* For all leaf windows in the window tree rooted at WINDOW, set their
12410 hscroll value so that PT is (i) visible in the window, and (ii) so
12411 that it is not within a certain margin at the window's left and
12412 right border. Value is non-zero if any window's hscroll has been
12413 changed. */
12414
12415 static int
12416 hscroll_window_tree (Lisp_Object window)
12417 {
12418 int hscrolled_p = 0;
12419 int hscroll_relative_p = FLOATP (Vhscroll_step);
12420 int hscroll_step_abs = 0;
12421 double hscroll_step_rel = 0;
12422
12423 if (hscroll_relative_p)
12424 {
12425 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12426 if (hscroll_step_rel < 0)
12427 {
12428 hscroll_relative_p = 0;
12429 hscroll_step_abs = 0;
12430 }
12431 }
12432 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12433 {
12434 hscroll_step_abs = XINT (Vhscroll_step);
12435 if (hscroll_step_abs < 0)
12436 hscroll_step_abs = 0;
12437 }
12438 else
12439 hscroll_step_abs = 0;
12440
12441 while (WINDOWP (window))
12442 {
12443 struct window *w = XWINDOW (window);
12444
12445 if (WINDOWP (w->hchild))
12446 hscrolled_p |= hscroll_window_tree (w->hchild);
12447 else if (WINDOWP (w->vchild))
12448 hscrolled_p |= hscroll_window_tree (w->vchild);
12449 else if (w->cursor.vpos >= 0)
12450 {
12451 int h_margin;
12452 int text_area_width;
12453 struct glyph_row *current_cursor_row
12454 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12455 struct glyph_row *desired_cursor_row
12456 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12457 struct glyph_row *cursor_row
12458 = (desired_cursor_row->enabled_p
12459 ? desired_cursor_row
12460 : current_cursor_row);
12461 int row_r2l_p = cursor_row->reversed_p;
12462
12463 text_area_width = window_box_width (w, TEXT_AREA);
12464
12465 /* Scroll when cursor is inside this scroll margin. */
12466 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12467
12468 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12469 /* For left-to-right rows, hscroll when cursor is either
12470 (i) inside the right hscroll margin, or (ii) if it is
12471 inside the left margin and the window is already
12472 hscrolled. */
12473 && ((!row_r2l_p
12474 && ((w->hscroll
12475 && w->cursor.x <= h_margin)
12476 || (cursor_row->enabled_p
12477 && cursor_row->truncated_on_right_p
12478 && (w->cursor.x >= text_area_width - h_margin))))
12479 /* For right-to-left rows, the logic is similar,
12480 except that rules for scrolling to left and right
12481 are reversed. E.g., if cursor.x <= h_margin, we
12482 need to hscroll "to the right" unconditionally,
12483 and that will scroll the screen to the left so as
12484 to reveal the next portion of the row. */
12485 || (row_r2l_p
12486 && ((cursor_row->enabled_p
12487 /* FIXME: It is confusing to set the
12488 truncated_on_right_p flag when R2L rows
12489 are actually truncated on the left. */
12490 && cursor_row->truncated_on_right_p
12491 && w->cursor.x <= h_margin)
12492 || (w->hscroll
12493 && (w->cursor.x >= text_area_width - h_margin))))))
12494 {
12495 struct it it;
12496 ptrdiff_t hscroll;
12497 struct buffer *saved_current_buffer;
12498 ptrdiff_t pt;
12499 int wanted_x;
12500
12501 /* Find point in a display of infinite width. */
12502 saved_current_buffer = current_buffer;
12503 current_buffer = XBUFFER (w->buffer);
12504
12505 if (w == XWINDOW (selected_window))
12506 pt = PT;
12507 else
12508 pt = clip_to_bounds (BEGV, marker_position (w->pointm), ZV);
12509
12510 /* Move iterator to pt starting at cursor_row->start in
12511 a line with infinite width. */
12512 init_to_row_start (&it, w, cursor_row);
12513 it.last_visible_x = INFINITY;
12514 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12515 current_buffer = saved_current_buffer;
12516
12517 /* Position cursor in window. */
12518 if (!hscroll_relative_p && hscroll_step_abs == 0)
12519 hscroll = max (0, (it.current_x
12520 - (ITERATOR_AT_END_OF_LINE_P (&it)
12521 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12522 : (text_area_width / 2))))
12523 / FRAME_COLUMN_WIDTH (it.f);
12524 else if ((!row_r2l_p
12525 && w->cursor.x >= text_area_width - h_margin)
12526 || (row_r2l_p && w->cursor.x <= h_margin))
12527 {
12528 if (hscroll_relative_p)
12529 wanted_x = text_area_width * (1 - hscroll_step_rel)
12530 - h_margin;
12531 else
12532 wanted_x = text_area_width
12533 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12534 - h_margin;
12535 hscroll
12536 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12537 }
12538 else
12539 {
12540 if (hscroll_relative_p)
12541 wanted_x = text_area_width * hscroll_step_rel
12542 + h_margin;
12543 else
12544 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12545 + h_margin;
12546 hscroll
12547 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12548 }
12549 hscroll = max (hscroll, w->min_hscroll);
12550
12551 /* Don't prevent redisplay optimizations if hscroll
12552 hasn't changed, as it will unnecessarily slow down
12553 redisplay. */
12554 if (w->hscroll != hscroll)
12555 {
12556 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12557 w->hscroll = hscroll;
12558 hscrolled_p = 1;
12559 }
12560 }
12561 }
12562
12563 window = w->next;
12564 }
12565
12566 /* Value is non-zero if hscroll of any leaf window has been changed. */
12567 return hscrolled_p;
12568 }
12569
12570
12571 /* Set hscroll so that cursor is visible and not inside horizontal
12572 scroll margins for all windows in the tree rooted at WINDOW. See
12573 also hscroll_window_tree above. Value is non-zero if any window's
12574 hscroll has been changed. If it has, desired matrices on the frame
12575 of WINDOW are cleared. */
12576
12577 static int
12578 hscroll_windows (Lisp_Object window)
12579 {
12580 int hscrolled_p = hscroll_window_tree (window);
12581 if (hscrolled_p)
12582 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12583 return hscrolled_p;
12584 }
12585
12586
12587 \f
12588 /************************************************************************
12589 Redisplay
12590 ************************************************************************/
12591
12592 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12593 to a non-zero value. This is sometimes handy to have in a debugger
12594 session. */
12595
12596 #ifdef GLYPH_DEBUG
12597
12598 /* First and last unchanged row for try_window_id. */
12599
12600 static int debug_first_unchanged_at_end_vpos;
12601 static int debug_last_unchanged_at_beg_vpos;
12602
12603 /* Delta vpos and y. */
12604
12605 static int debug_dvpos, debug_dy;
12606
12607 /* Delta in characters and bytes for try_window_id. */
12608
12609 static ptrdiff_t debug_delta, debug_delta_bytes;
12610
12611 /* Values of window_end_pos and window_end_vpos at the end of
12612 try_window_id. */
12613
12614 static ptrdiff_t debug_end_vpos;
12615
12616 /* Append a string to W->desired_matrix->method. FMT is a printf
12617 format string. If trace_redisplay_p is non-zero also printf the
12618 resulting string to stderr. */
12619
12620 static void debug_method_add (struct window *, char const *, ...)
12621 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12622
12623 static void
12624 debug_method_add (struct window *w, char const *fmt, ...)
12625 {
12626 char *method = w->desired_matrix->method;
12627 int len = strlen (method);
12628 int size = sizeof w->desired_matrix->method;
12629 int remaining = size - len - 1;
12630 va_list ap;
12631
12632 if (len && remaining)
12633 {
12634 method[len] = '|';
12635 --remaining, ++len;
12636 }
12637
12638 va_start (ap, fmt);
12639 vsnprintf (method + len, remaining + 1, fmt, ap);
12640 va_end (ap);
12641
12642 if (trace_redisplay_p)
12643 fprintf (stderr, "%p (%s): %s\n",
12644 w,
12645 ((BUFFERP (w->buffer)
12646 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12647 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12648 : "no buffer"),
12649 method + len);
12650 }
12651
12652 #endif /* GLYPH_DEBUG */
12653
12654
12655 /* Value is non-zero if all changes in window W, which displays
12656 current_buffer, are in the text between START and END. START is a
12657 buffer position, END is given as a distance from Z. Used in
12658 redisplay_internal for display optimization. */
12659
12660 static int
12661 text_outside_line_unchanged_p (struct window *w,
12662 ptrdiff_t start, ptrdiff_t end)
12663 {
12664 int unchanged_p = 1;
12665
12666 /* If text or overlays have changed, see where. */
12667 if (window_outdated (w))
12668 {
12669 /* Gap in the line? */
12670 if (GPT < start || Z - GPT < end)
12671 unchanged_p = 0;
12672
12673 /* Changes start in front of the line, or end after it? */
12674 if (unchanged_p
12675 && (BEG_UNCHANGED < start - 1
12676 || END_UNCHANGED < end))
12677 unchanged_p = 0;
12678
12679 /* If selective display, can't optimize if changes start at the
12680 beginning of the line. */
12681 if (unchanged_p
12682 && INTEGERP (BVAR (current_buffer, selective_display))
12683 && XINT (BVAR (current_buffer, selective_display)) > 0
12684 && (BEG_UNCHANGED < start || GPT <= start))
12685 unchanged_p = 0;
12686
12687 /* If there are overlays at the start or end of the line, these
12688 may have overlay strings with newlines in them. A change at
12689 START, for instance, may actually concern the display of such
12690 overlay strings as well, and they are displayed on different
12691 lines. So, quickly rule out this case. (For the future, it
12692 might be desirable to implement something more telling than
12693 just BEG/END_UNCHANGED.) */
12694 if (unchanged_p)
12695 {
12696 if (BEG + BEG_UNCHANGED == start
12697 && overlay_touches_p (start))
12698 unchanged_p = 0;
12699 if (END_UNCHANGED == end
12700 && overlay_touches_p (Z - end))
12701 unchanged_p = 0;
12702 }
12703
12704 /* Under bidi reordering, adding or deleting a character in the
12705 beginning of a paragraph, before the first strong directional
12706 character, can change the base direction of the paragraph (unless
12707 the buffer specifies a fixed paragraph direction), which will
12708 require to redisplay the whole paragraph. It might be worthwhile
12709 to find the paragraph limits and widen the range of redisplayed
12710 lines to that, but for now just give up this optimization. */
12711 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12712 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12713 unchanged_p = 0;
12714 }
12715
12716 return unchanged_p;
12717 }
12718
12719
12720 /* Do a frame update, taking possible shortcuts into account. This is
12721 the main external entry point for redisplay.
12722
12723 If the last redisplay displayed an echo area message and that message
12724 is no longer requested, we clear the echo area or bring back the
12725 mini-buffer if that is in use. */
12726
12727 void
12728 redisplay (void)
12729 {
12730 redisplay_internal ();
12731 }
12732
12733
12734 static Lisp_Object
12735 overlay_arrow_string_or_property (Lisp_Object var)
12736 {
12737 Lisp_Object val;
12738
12739 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12740 return val;
12741
12742 return Voverlay_arrow_string;
12743 }
12744
12745 /* Return 1 if there are any overlay-arrows in current_buffer. */
12746 static int
12747 overlay_arrow_in_current_buffer_p (void)
12748 {
12749 Lisp_Object vlist;
12750
12751 for (vlist = Voverlay_arrow_variable_list;
12752 CONSP (vlist);
12753 vlist = XCDR (vlist))
12754 {
12755 Lisp_Object var = XCAR (vlist);
12756 Lisp_Object val;
12757
12758 if (!SYMBOLP (var))
12759 continue;
12760 val = find_symbol_value (var);
12761 if (MARKERP (val)
12762 && current_buffer == XMARKER (val)->buffer)
12763 return 1;
12764 }
12765 return 0;
12766 }
12767
12768
12769 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12770 has changed. */
12771
12772 static int
12773 overlay_arrows_changed_p (void)
12774 {
12775 Lisp_Object vlist;
12776
12777 for (vlist = Voverlay_arrow_variable_list;
12778 CONSP (vlist);
12779 vlist = XCDR (vlist))
12780 {
12781 Lisp_Object var = XCAR (vlist);
12782 Lisp_Object val, pstr;
12783
12784 if (!SYMBOLP (var))
12785 continue;
12786 val = find_symbol_value (var);
12787 if (!MARKERP (val))
12788 continue;
12789 if (! EQ (COERCE_MARKER (val),
12790 Fget (var, Qlast_arrow_position))
12791 || ! (pstr = overlay_arrow_string_or_property (var),
12792 EQ (pstr, Fget (var, Qlast_arrow_string))))
12793 return 1;
12794 }
12795 return 0;
12796 }
12797
12798 /* Mark overlay arrows to be updated on next redisplay. */
12799
12800 static void
12801 update_overlay_arrows (int up_to_date)
12802 {
12803 Lisp_Object vlist;
12804
12805 for (vlist = Voverlay_arrow_variable_list;
12806 CONSP (vlist);
12807 vlist = XCDR (vlist))
12808 {
12809 Lisp_Object var = XCAR (vlist);
12810
12811 if (!SYMBOLP (var))
12812 continue;
12813
12814 if (up_to_date > 0)
12815 {
12816 Lisp_Object val = find_symbol_value (var);
12817 Fput (var, Qlast_arrow_position,
12818 COERCE_MARKER (val));
12819 Fput (var, Qlast_arrow_string,
12820 overlay_arrow_string_or_property (var));
12821 }
12822 else if (up_to_date < 0
12823 || !NILP (Fget (var, Qlast_arrow_position)))
12824 {
12825 Fput (var, Qlast_arrow_position, Qt);
12826 Fput (var, Qlast_arrow_string, Qt);
12827 }
12828 }
12829 }
12830
12831
12832 /* Return overlay arrow string to display at row.
12833 Return integer (bitmap number) for arrow bitmap in left fringe.
12834 Return nil if no overlay arrow. */
12835
12836 static Lisp_Object
12837 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12838 {
12839 Lisp_Object vlist;
12840
12841 for (vlist = Voverlay_arrow_variable_list;
12842 CONSP (vlist);
12843 vlist = XCDR (vlist))
12844 {
12845 Lisp_Object var = XCAR (vlist);
12846 Lisp_Object val;
12847
12848 if (!SYMBOLP (var))
12849 continue;
12850
12851 val = find_symbol_value (var);
12852
12853 if (MARKERP (val)
12854 && current_buffer == XMARKER (val)->buffer
12855 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12856 {
12857 if (FRAME_WINDOW_P (it->f)
12858 /* FIXME: if ROW->reversed_p is set, this should test
12859 the right fringe, not the left one. */
12860 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12861 {
12862 #ifdef HAVE_WINDOW_SYSTEM
12863 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12864 {
12865 int fringe_bitmap;
12866 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12867 return make_number (fringe_bitmap);
12868 }
12869 #endif
12870 return make_number (-1); /* Use default arrow bitmap. */
12871 }
12872 return overlay_arrow_string_or_property (var);
12873 }
12874 }
12875
12876 return Qnil;
12877 }
12878
12879 /* Return 1 if point moved out of or into a composition. Otherwise
12880 return 0. PREV_BUF and PREV_PT are the last point buffer and
12881 position. BUF and PT are the current point buffer and position. */
12882
12883 static int
12884 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12885 struct buffer *buf, ptrdiff_t pt)
12886 {
12887 ptrdiff_t start, end;
12888 Lisp_Object prop;
12889 Lisp_Object buffer;
12890
12891 XSETBUFFER (buffer, buf);
12892 /* Check a composition at the last point if point moved within the
12893 same buffer. */
12894 if (prev_buf == buf)
12895 {
12896 if (prev_pt == pt)
12897 /* Point didn't move. */
12898 return 0;
12899
12900 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12901 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12902 && COMPOSITION_VALID_P (start, end, prop)
12903 && start < prev_pt && end > prev_pt)
12904 /* The last point was within the composition. Return 1 iff
12905 point moved out of the composition. */
12906 return (pt <= start || pt >= end);
12907 }
12908
12909 /* Check a composition at the current point. */
12910 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12911 && find_composition (pt, -1, &start, &end, &prop, buffer)
12912 && COMPOSITION_VALID_P (start, end, prop)
12913 && start < pt && end > pt);
12914 }
12915
12916
12917 /* Reconsider the setting of B->clip_changed which is displayed
12918 in window W. */
12919
12920 static void
12921 reconsider_clip_changes (struct window *w, struct buffer *b)
12922 {
12923 if (b->clip_changed
12924 && !NILP (w->window_end_valid)
12925 && w->current_matrix->buffer == b
12926 && w->current_matrix->zv == BUF_ZV (b)
12927 && w->current_matrix->begv == BUF_BEGV (b))
12928 b->clip_changed = 0;
12929
12930 /* If display wasn't paused, and W is not a tool bar window, see if
12931 point has been moved into or out of a composition. In that case,
12932 we set b->clip_changed to 1 to force updating the screen. If
12933 b->clip_changed has already been set to 1, we can skip this
12934 check. */
12935 if (!b->clip_changed
12936 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12937 {
12938 ptrdiff_t pt;
12939
12940 if (w == XWINDOW (selected_window))
12941 pt = PT;
12942 else
12943 pt = marker_position (w->pointm);
12944
12945 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12946 || pt != w->last_point)
12947 && check_point_in_composition (w->current_matrix->buffer,
12948 w->last_point,
12949 XBUFFER (w->buffer), pt))
12950 b->clip_changed = 1;
12951 }
12952 }
12953 \f
12954
12955 /* Select FRAME to forward the values of frame-local variables into C
12956 variables so that the redisplay routines can access those values
12957 directly. */
12958
12959 static void
12960 select_frame_for_redisplay (Lisp_Object frame)
12961 {
12962 Lisp_Object tail, tem;
12963 Lisp_Object old = selected_frame;
12964 struct Lisp_Symbol *sym;
12965
12966 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
12967
12968 selected_frame = frame;
12969 selected_window = XFRAME (frame)->selected_window;
12970
12971 do {
12972 for (tail = XFRAME (frame)->param_alist;
12973 CONSP (tail); tail = XCDR (tail))
12974 if (CONSP (XCAR (tail))
12975 && (tem = XCAR (XCAR (tail)),
12976 SYMBOLP (tem))
12977 && (sym = indirect_variable (XSYMBOL (tem)),
12978 sym->redirect == SYMBOL_LOCALIZED)
12979 && sym->val.blv->frame_local)
12980 /* Use find_symbol_value rather than Fsymbol_value
12981 to avoid an error if it is void. */
12982 find_symbol_value (tem);
12983 } while (!EQ (frame, old) && (frame = old, 1));
12984 }
12985
12986 /* Make sure that previously selected OLD_FRAME is selected unless it has been
12987 deleted (by an X connection failure during redisplay, for example). */
12988
12989 static void
12990 ensure_selected_frame (Lisp_Object frame)
12991 {
12992 if (!EQ (frame, selected_frame) && FRAME_LIVE_P (XFRAME (frame)))
12993 select_frame_for_redisplay (frame);
12994 }
12995
12996 #define STOP_POLLING \
12997 do { if (! polling_stopped_here) stop_polling (); \
12998 polling_stopped_here = 1; } while (0)
12999
13000 #define RESUME_POLLING \
13001 do { if (polling_stopped_here) start_polling (); \
13002 polling_stopped_here = 0; } while (0)
13003
13004
13005 /* Perhaps in the future avoid recentering windows if it
13006 is not necessary; currently that causes some problems. */
13007
13008 static void
13009 redisplay_internal (void)
13010 {
13011 struct window *w = XWINDOW (selected_window);
13012 struct window *sw;
13013 struct frame *fr;
13014 int pending;
13015 int must_finish = 0;
13016 struct text_pos tlbufpos, tlendpos;
13017 int number_of_visible_frames;
13018 ptrdiff_t count, count1;
13019 struct frame *sf;
13020 int polling_stopped_here = 0;
13021 Lisp_Object tail, frame, old_frame = selected_frame;
13022 struct backtrace backtrace;
13023
13024 /* Non-zero means redisplay has to consider all windows on all
13025 frames. Zero means, only selected_window is considered. */
13026 int consider_all_windows_p;
13027
13028 /* Non-zero means redisplay has to redisplay the miniwindow. */
13029 int update_miniwindow_p = 0;
13030
13031 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13032
13033 /* No redisplay if running in batch mode or frame is not yet fully
13034 initialized, or redisplay is explicitly turned off by setting
13035 Vinhibit_redisplay. */
13036 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13037 || !NILP (Vinhibit_redisplay))
13038 return;
13039
13040 /* Don't examine these until after testing Vinhibit_redisplay.
13041 When Emacs is shutting down, perhaps because its connection to
13042 X has dropped, we should not look at them at all. */
13043 fr = XFRAME (w->frame);
13044 sf = SELECTED_FRAME ();
13045
13046 if (!fr->glyphs_initialized_p)
13047 return;
13048
13049 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13050 if (popup_activated ())
13051 return;
13052 #endif
13053
13054 /* I don't think this happens but let's be paranoid. */
13055 if (redisplaying_p)
13056 return;
13057
13058 /* Record a function that clears redisplaying_p
13059 when we leave this function. */
13060 count = SPECPDL_INDEX ();
13061 record_unwind_protect (unwind_redisplay, selected_frame);
13062 redisplaying_p = 1;
13063 specbind (Qinhibit_free_realized_faces, Qnil);
13064
13065 /* Record this function, so it appears on the profiler's backtraces. */
13066 backtrace.next = backtrace_list;
13067 backtrace.function = Qredisplay_internal;
13068 backtrace.args = &Qnil;
13069 backtrace.nargs = 0;
13070 backtrace.debug_on_exit = 0;
13071 backtrace_list = &backtrace;
13072
13073 FOR_EACH_FRAME (tail, frame)
13074 XFRAME (frame)->already_hscrolled_p = 0;
13075
13076 retry:
13077 /* Remember the currently selected window. */
13078 sw = w;
13079
13080 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
13081 selected_frame and selected_window to be temporarily out-of-sync so
13082 when we come back here via `goto retry', we need to resync because we
13083 may need to run Elisp code (via prepare_menu_bars). */
13084 ensure_selected_frame (old_frame);
13085
13086 pending = 0;
13087 reconsider_clip_changes (w, current_buffer);
13088 last_escape_glyph_frame = NULL;
13089 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13090 last_glyphless_glyph_frame = NULL;
13091 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13092
13093 /* If new fonts have been loaded that make a glyph matrix adjustment
13094 necessary, do it. */
13095 if (fonts_changed_p)
13096 {
13097 adjust_glyphs (NULL);
13098 ++windows_or_buffers_changed;
13099 fonts_changed_p = 0;
13100 }
13101
13102 /* If face_change_count is non-zero, init_iterator will free all
13103 realized faces, which includes the faces referenced from current
13104 matrices. So, we can't reuse current matrices in this case. */
13105 if (face_change_count)
13106 ++windows_or_buffers_changed;
13107
13108 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13109 && FRAME_TTY (sf)->previous_frame != sf)
13110 {
13111 /* Since frames on a single ASCII terminal share the same
13112 display area, displaying a different frame means redisplay
13113 the whole thing. */
13114 windows_or_buffers_changed++;
13115 SET_FRAME_GARBAGED (sf);
13116 #ifndef DOS_NT
13117 set_tty_color_mode (FRAME_TTY (sf), sf);
13118 #endif
13119 FRAME_TTY (sf)->previous_frame = sf;
13120 }
13121
13122 /* Set the visible flags for all frames. Do this before checking for
13123 resized or garbaged frames; they want to know if their frames are
13124 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13125 number_of_visible_frames = 0;
13126
13127 FOR_EACH_FRAME (tail, frame)
13128 {
13129 struct frame *f = XFRAME (frame);
13130
13131 FRAME_SAMPLE_VISIBILITY (f);
13132 if (FRAME_VISIBLE_P (f))
13133 ++number_of_visible_frames;
13134 clear_desired_matrices (f);
13135 }
13136
13137 /* Notice any pending interrupt request to change frame size. */
13138 do_pending_window_change (1);
13139
13140 /* do_pending_window_change could change the selected_window due to
13141 frame resizing which makes the selected window too small. */
13142 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13143 {
13144 sw = w;
13145 reconsider_clip_changes (w, current_buffer);
13146 }
13147
13148 /* Clear frames marked as garbaged. */
13149 clear_garbaged_frames ();
13150
13151 /* Build menubar and tool-bar items. */
13152 if (NILP (Vmemory_full))
13153 prepare_menu_bars ();
13154
13155 if (windows_or_buffers_changed)
13156 update_mode_lines++;
13157
13158 /* Detect case that we need to write or remove a star in the mode line. */
13159 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13160 {
13161 w->update_mode_line = 1;
13162 if (buffer_shared_and_changed ())
13163 update_mode_lines++;
13164 }
13165
13166 /* Avoid invocation of point motion hooks by `current_column' below. */
13167 count1 = SPECPDL_INDEX ();
13168 specbind (Qinhibit_point_motion_hooks, Qt);
13169
13170 if (mode_line_update_needed (w))
13171 w->update_mode_line = 1;
13172
13173 unbind_to (count1, Qnil);
13174
13175 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13176
13177 consider_all_windows_p = (update_mode_lines
13178 || buffer_shared_and_changed ()
13179 || cursor_type_changed);
13180
13181 /* If specs for an arrow have changed, do thorough redisplay
13182 to ensure we remove any arrow that should no longer exist. */
13183 if (overlay_arrows_changed_p ())
13184 consider_all_windows_p = windows_or_buffers_changed = 1;
13185
13186 /* Normally the message* functions will have already displayed and
13187 updated the echo area, but the frame may have been trashed, or
13188 the update may have been preempted, so display the echo area
13189 again here. Checking message_cleared_p captures the case that
13190 the echo area should be cleared. */
13191 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13192 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13193 || (message_cleared_p
13194 && minibuf_level == 0
13195 /* If the mini-window is currently selected, this means the
13196 echo-area doesn't show through. */
13197 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13198 {
13199 int window_height_changed_p = echo_area_display (0);
13200
13201 if (message_cleared_p)
13202 update_miniwindow_p = 1;
13203
13204 must_finish = 1;
13205
13206 /* If we don't display the current message, don't clear the
13207 message_cleared_p flag, because, if we did, we wouldn't clear
13208 the echo area in the next redisplay which doesn't preserve
13209 the echo area. */
13210 if (!display_last_displayed_message_p)
13211 message_cleared_p = 0;
13212
13213 if (fonts_changed_p)
13214 goto retry;
13215 else if (window_height_changed_p)
13216 {
13217 consider_all_windows_p = 1;
13218 ++update_mode_lines;
13219 ++windows_or_buffers_changed;
13220
13221 /* If window configuration was changed, frames may have been
13222 marked garbaged. Clear them or we will experience
13223 surprises wrt scrolling. */
13224 clear_garbaged_frames ();
13225 }
13226 }
13227 else if (EQ (selected_window, minibuf_window)
13228 && (current_buffer->clip_changed || window_outdated (w))
13229 && resize_mini_window (w, 0))
13230 {
13231 /* Resized active mini-window to fit the size of what it is
13232 showing if its contents might have changed. */
13233 must_finish = 1;
13234 /* FIXME: this causes all frames to be updated, which seems unnecessary
13235 since only the current frame needs to be considered. This function
13236 needs to be rewritten with two variables, consider_all_windows and
13237 consider_all_frames. */
13238 consider_all_windows_p = 1;
13239 ++windows_or_buffers_changed;
13240 ++update_mode_lines;
13241
13242 /* If window configuration was changed, frames may have been
13243 marked garbaged. Clear them or we will experience
13244 surprises wrt scrolling. */
13245 clear_garbaged_frames ();
13246 }
13247
13248
13249 /* If showing the region, and mark has changed, we must redisplay
13250 the whole window. The assignment to this_line_start_pos prevents
13251 the optimization directly below this if-statement. */
13252 if (((!NILP (Vtransient_mark_mode)
13253 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13254 != !NILP (w->region_showing))
13255 || (!NILP (w->region_showing)
13256 && !EQ (w->region_showing,
13257 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13258 CHARPOS (this_line_start_pos) = 0;
13259
13260 /* Optimize the case that only the line containing the cursor in the
13261 selected window has changed. Variables starting with this_ are
13262 set in display_line and record information about the line
13263 containing the cursor. */
13264 tlbufpos = this_line_start_pos;
13265 tlendpos = this_line_end_pos;
13266 if (!consider_all_windows_p
13267 && CHARPOS (tlbufpos) > 0
13268 && !w->update_mode_line
13269 && !current_buffer->clip_changed
13270 && !current_buffer->prevent_redisplay_optimizations_p
13271 && FRAME_VISIBLE_P (XFRAME (w->frame))
13272 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13273 /* Make sure recorded data applies to current buffer, etc. */
13274 && this_line_buffer == current_buffer
13275 && current_buffer == XBUFFER (w->buffer)
13276 && !w->force_start
13277 && !w->optional_new_start
13278 /* Point must be on the line that we have info recorded about. */
13279 && PT >= CHARPOS (tlbufpos)
13280 && PT <= Z - CHARPOS (tlendpos)
13281 /* All text outside that line, including its final newline,
13282 must be unchanged. */
13283 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13284 CHARPOS (tlendpos)))
13285 {
13286 if (CHARPOS (tlbufpos) > BEGV
13287 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13288 && (CHARPOS (tlbufpos) == ZV
13289 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13290 /* Former continuation line has disappeared by becoming empty. */
13291 goto cancel;
13292 else if (window_outdated (w) || MINI_WINDOW_P (w))
13293 {
13294 /* We have to handle the case of continuation around a
13295 wide-column character (see the comment in indent.c around
13296 line 1340).
13297
13298 For instance, in the following case:
13299
13300 -------- Insert --------
13301 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13302 J_I_ ==> J_I_ `^^' are cursors.
13303 ^^ ^^
13304 -------- --------
13305
13306 As we have to redraw the line above, we cannot use this
13307 optimization. */
13308
13309 struct it it;
13310 int line_height_before = this_line_pixel_height;
13311
13312 /* Note that start_display will handle the case that the
13313 line starting at tlbufpos is a continuation line. */
13314 start_display (&it, w, tlbufpos);
13315
13316 /* Implementation note: It this still necessary? */
13317 if (it.current_x != this_line_start_x)
13318 goto cancel;
13319
13320 TRACE ((stderr, "trying display optimization 1\n"));
13321 w->cursor.vpos = -1;
13322 overlay_arrow_seen = 0;
13323 it.vpos = this_line_vpos;
13324 it.current_y = this_line_y;
13325 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13326 display_line (&it);
13327
13328 /* If line contains point, is not continued,
13329 and ends at same distance from eob as before, we win. */
13330 if (w->cursor.vpos >= 0
13331 /* Line is not continued, otherwise this_line_start_pos
13332 would have been set to 0 in display_line. */
13333 && CHARPOS (this_line_start_pos)
13334 /* Line ends as before. */
13335 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13336 /* Line has same height as before. Otherwise other lines
13337 would have to be shifted up or down. */
13338 && this_line_pixel_height == line_height_before)
13339 {
13340 /* If this is not the window's last line, we must adjust
13341 the charstarts of the lines below. */
13342 if (it.current_y < it.last_visible_y)
13343 {
13344 struct glyph_row *row
13345 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13346 ptrdiff_t delta, delta_bytes;
13347
13348 /* We used to distinguish between two cases here,
13349 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13350 when the line ends in a newline or the end of the
13351 buffer's accessible portion. But both cases did
13352 the same, so they were collapsed. */
13353 delta = (Z
13354 - CHARPOS (tlendpos)
13355 - MATRIX_ROW_START_CHARPOS (row));
13356 delta_bytes = (Z_BYTE
13357 - BYTEPOS (tlendpos)
13358 - MATRIX_ROW_START_BYTEPOS (row));
13359
13360 increment_matrix_positions (w->current_matrix,
13361 this_line_vpos + 1,
13362 w->current_matrix->nrows,
13363 delta, delta_bytes);
13364 }
13365
13366 /* If this row displays text now but previously didn't,
13367 or vice versa, w->window_end_vpos may have to be
13368 adjusted. */
13369 if ((it.glyph_row - 1)->displays_text_p)
13370 {
13371 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13372 wset_window_end_vpos (w, make_number (this_line_vpos));
13373 }
13374 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13375 && this_line_vpos > 0)
13376 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13377 wset_window_end_valid (w, Qnil);
13378
13379 /* Update hint: No need to try to scroll in update_window. */
13380 w->desired_matrix->no_scrolling_p = 1;
13381
13382 #ifdef GLYPH_DEBUG
13383 *w->desired_matrix->method = 0;
13384 debug_method_add (w, "optimization 1");
13385 #endif
13386 #ifdef HAVE_WINDOW_SYSTEM
13387 update_window_fringes (w, 0);
13388 #endif
13389 goto update;
13390 }
13391 else
13392 goto cancel;
13393 }
13394 else if (/* Cursor position hasn't changed. */
13395 PT == w->last_point
13396 /* Make sure the cursor was last displayed
13397 in this window. Otherwise we have to reposition it. */
13398 && 0 <= w->cursor.vpos
13399 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13400 {
13401 if (!must_finish)
13402 {
13403 do_pending_window_change (1);
13404 /* If selected_window changed, redisplay again. */
13405 if (WINDOWP (selected_window)
13406 && (w = XWINDOW (selected_window)) != sw)
13407 goto retry;
13408
13409 /* We used to always goto end_of_redisplay here, but this
13410 isn't enough if we have a blinking cursor. */
13411 if (w->cursor_off_p == w->last_cursor_off_p)
13412 goto end_of_redisplay;
13413 }
13414 goto update;
13415 }
13416 /* If highlighting the region, or if the cursor is in the echo area,
13417 then we can't just move the cursor. */
13418 else if (! (!NILP (Vtransient_mark_mode)
13419 && !NILP (BVAR (current_buffer, mark_active)))
13420 && (EQ (selected_window,
13421 BVAR (current_buffer, last_selected_window))
13422 || highlight_nonselected_windows)
13423 && NILP (w->region_showing)
13424 && NILP (Vshow_trailing_whitespace)
13425 && !cursor_in_echo_area)
13426 {
13427 struct it it;
13428 struct glyph_row *row;
13429
13430 /* Skip from tlbufpos to PT and see where it is. Note that
13431 PT may be in invisible text. If so, we will end at the
13432 next visible position. */
13433 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13434 NULL, DEFAULT_FACE_ID);
13435 it.current_x = this_line_start_x;
13436 it.current_y = this_line_y;
13437 it.vpos = this_line_vpos;
13438
13439 /* The call to move_it_to stops in front of PT, but
13440 moves over before-strings. */
13441 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13442
13443 if (it.vpos == this_line_vpos
13444 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13445 row->enabled_p))
13446 {
13447 eassert (this_line_vpos == it.vpos);
13448 eassert (this_line_y == it.current_y);
13449 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13450 #ifdef GLYPH_DEBUG
13451 *w->desired_matrix->method = 0;
13452 debug_method_add (w, "optimization 3");
13453 #endif
13454 goto update;
13455 }
13456 else
13457 goto cancel;
13458 }
13459
13460 cancel:
13461 /* Text changed drastically or point moved off of line. */
13462 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13463 }
13464
13465 CHARPOS (this_line_start_pos) = 0;
13466 consider_all_windows_p |= buffer_shared_and_changed ();
13467 ++clear_face_cache_count;
13468 #ifdef HAVE_WINDOW_SYSTEM
13469 ++clear_image_cache_count;
13470 #endif
13471
13472 /* Build desired matrices, and update the display. If
13473 consider_all_windows_p is non-zero, do it for all windows on all
13474 frames. Otherwise do it for selected_window, only. */
13475
13476 if (consider_all_windows_p)
13477 {
13478 FOR_EACH_FRAME (tail, frame)
13479 XFRAME (frame)->updated_p = 0;
13480
13481 FOR_EACH_FRAME (tail, frame)
13482 {
13483 struct frame *f = XFRAME (frame);
13484
13485 /* We don't have to do anything for unselected terminal
13486 frames. */
13487 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13488 && !EQ (FRAME_TTY (f)->top_frame, frame))
13489 continue;
13490
13491 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13492 {
13493 if (! EQ (frame, selected_frame))
13494 /* Select the frame, for the sake of frame-local
13495 variables. */
13496 select_frame_for_redisplay (frame);
13497
13498 /* Mark all the scroll bars to be removed; we'll redeem
13499 the ones we want when we redisplay their windows. */
13500 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13501 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13502
13503 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13504 redisplay_windows (FRAME_ROOT_WINDOW (f));
13505
13506 /* The X error handler may have deleted that frame. */
13507 if (!FRAME_LIVE_P (f))
13508 continue;
13509
13510 /* Any scroll bars which redisplay_windows should have
13511 nuked should now go away. */
13512 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13513 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13514
13515 /* If fonts changed, display again. */
13516 /* ??? rms: I suspect it is a mistake to jump all the way
13517 back to retry here. It should just retry this frame. */
13518 if (fonts_changed_p)
13519 goto retry;
13520
13521 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13522 {
13523 /* See if we have to hscroll. */
13524 if (!f->already_hscrolled_p)
13525 {
13526 f->already_hscrolled_p = 1;
13527 if (hscroll_windows (f->root_window))
13528 goto retry;
13529 }
13530
13531 /* Prevent various kinds of signals during display
13532 update. stdio is not robust about handling
13533 signals, which can cause an apparent I/O
13534 error. */
13535 if (interrupt_input)
13536 unrequest_sigio ();
13537 STOP_POLLING;
13538
13539 /* Update the display. */
13540 set_window_update_flags (XWINDOW (f->root_window), 1);
13541 pending |= update_frame (f, 0, 0);
13542 f->updated_p = 1;
13543 }
13544 }
13545 }
13546
13547 /* We played a bit fast-and-loose above and allowed selected_frame
13548 and selected_window to be temporarily out-of-sync but let's make
13549 sure this stays contained. */
13550 ensure_selected_frame (old_frame);
13551 eassert (EQ (XFRAME (selected_frame)->selected_window, selected_window));
13552
13553 if (!pending)
13554 {
13555 /* Do the mark_window_display_accurate after all windows have
13556 been redisplayed because this call resets flags in buffers
13557 which are needed for proper redisplay. */
13558 FOR_EACH_FRAME (tail, frame)
13559 {
13560 struct frame *f = XFRAME (frame);
13561 if (f->updated_p)
13562 {
13563 mark_window_display_accurate (f->root_window, 1);
13564 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13565 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13566 }
13567 }
13568 }
13569 }
13570 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13571 {
13572 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13573 struct frame *mini_frame;
13574
13575 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13576 /* Use list_of_error, not Qerror, so that
13577 we catch only errors and don't run the debugger. */
13578 internal_condition_case_1 (redisplay_window_1, selected_window,
13579 list_of_error,
13580 redisplay_window_error);
13581 if (update_miniwindow_p)
13582 internal_condition_case_1 (redisplay_window_1, mini_window,
13583 list_of_error,
13584 redisplay_window_error);
13585
13586 /* Compare desired and current matrices, perform output. */
13587
13588 update:
13589 /* If fonts changed, display again. */
13590 if (fonts_changed_p)
13591 goto retry;
13592
13593 /* Prevent various kinds of signals during display update.
13594 stdio is not robust about handling signals,
13595 which can cause an apparent I/O error. */
13596 if (interrupt_input)
13597 unrequest_sigio ();
13598 STOP_POLLING;
13599
13600 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13601 {
13602 if (hscroll_windows (selected_window))
13603 goto retry;
13604
13605 XWINDOW (selected_window)->must_be_updated_p = 1;
13606 pending = update_frame (sf, 0, 0);
13607 }
13608
13609 /* We may have called echo_area_display at the top of this
13610 function. If the echo area is on another frame, that may
13611 have put text on a frame other than the selected one, so the
13612 above call to update_frame would not have caught it. Catch
13613 it here. */
13614 mini_window = FRAME_MINIBUF_WINDOW (sf);
13615 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13616
13617 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13618 {
13619 XWINDOW (mini_window)->must_be_updated_p = 1;
13620 pending |= update_frame (mini_frame, 0, 0);
13621 if (!pending && hscroll_windows (mini_window))
13622 goto retry;
13623 }
13624 }
13625
13626 /* If display was paused because of pending input, make sure we do a
13627 thorough update the next time. */
13628 if (pending)
13629 {
13630 /* Prevent the optimization at the beginning of
13631 redisplay_internal that tries a single-line update of the
13632 line containing the cursor in the selected window. */
13633 CHARPOS (this_line_start_pos) = 0;
13634
13635 /* Let the overlay arrow be updated the next time. */
13636 update_overlay_arrows (0);
13637
13638 /* If we pause after scrolling, some rows in the current
13639 matrices of some windows are not valid. */
13640 if (!WINDOW_FULL_WIDTH_P (w)
13641 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13642 update_mode_lines = 1;
13643 }
13644 else
13645 {
13646 if (!consider_all_windows_p)
13647 {
13648 /* This has already been done above if
13649 consider_all_windows_p is set. */
13650 mark_window_display_accurate_1 (w, 1);
13651
13652 /* Say overlay arrows are up to date. */
13653 update_overlay_arrows (1);
13654
13655 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13656 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13657 }
13658
13659 update_mode_lines = 0;
13660 windows_or_buffers_changed = 0;
13661 cursor_type_changed = 0;
13662 }
13663
13664 /* Start SIGIO interrupts coming again. Having them off during the
13665 code above makes it less likely one will discard output, but not
13666 impossible, since there might be stuff in the system buffer here.
13667 But it is much hairier to try to do anything about that. */
13668 if (interrupt_input)
13669 request_sigio ();
13670 RESUME_POLLING;
13671
13672 /* If a frame has become visible which was not before, redisplay
13673 again, so that we display it. Expose events for such a frame
13674 (which it gets when becoming visible) don't call the parts of
13675 redisplay constructing glyphs, so simply exposing a frame won't
13676 display anything in this case. So, we have to display these
13677 frames here explicitly. */
13678 if (!pending)
13679 {
13680 int new_count = 0;
13681
13682 FOR_EACH_FRAME (tail, frame)
13683 {
13684 int this_is_visible = 0;
13685
13686 if (XFRAME (frame)->visible)
13687 this_is_visible = 1;
13688 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13689 if (XFRAME (frame)->visible)
13690 this_is_visible = 1;
13691
13692 if (this_is_visible)
13693 new_count++;
13694 }
13695
13696 if (new_count != number_of_visible_frames)
13697 windows_or_buffers_changed++;
13698 }
13699
13700 /* Change frame size now if a change is pending. */
13701 do_pending_window_change (1);
13702
13703 /* If we just did a pending size change, or have additional
13704 visible frames, or selected_window changed, redisplay again. */
13705 if ((windows_or_buffers_changed && !pending)
13706 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13707 goto retry;
13708
13709 /* Clear the face and image caches.
13710
13711 We used to do this only if consider_all_windows_p. But the cache
13712 needs to be cleared if a timer creates images in the current
13713 buffer (e.g. the test case in Bug#6230). */
13714
13715 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13716 {
13717 clear_face_cache (0);
13718 clear_face_cache_count = 0;
13719 }
13720
13721 #ifdef HAVE_WINDOW_SYSTEM
13722 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13723 {
13724 clear_image_caches (Qnil);
13725 clear_image_cache_count = 0;
13726 }
13727 #endif /* HAVE_WINDOW_SYSTEM */
13728
13729 end_of_redisplay:
13730 backtrace_list = backtrace.next;
13731 unbind_to (count, Qnil);
13732 RESUME_POLLING;
13733 }
13734
13735
13736 /* Redisplay, but leave alone any recent echo area message unless
13737 another message has been requested in its place.
13738
13739 This is useful in situations where you need to redisplay but no
13740 user action has occurred, making it inappropriate for the message
13741 area to be cleared. See tracking_off and
13742 wait_reading_process_output for examples of these situations.
13743
13744 FROM_WHERE is an integer saying from where this function was
13745 called. This is useful for debugging. */
13746
13747 void
13748 redisplay_preserve_echo_area (int from_where)
13749 {
13750 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13751
13752 if (!NILP (echo_area_buffer[1]))
13753 {
13754 /* We have a previously displayed message, but no current
13755 message. Redisplay the previous message. */
13756 display_last_displayed_message_p = 1;
13757 redisplay_internal ();
13758 display_last_displayed_message_p = 0;
13759 }
13760 else
13761 redisplay_internal ();
13762
13763 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13764 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13765 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13766 }
13767
13768
13769 /* Function registered with record_unwind_protect in redisplay_internal.
13770 Clear redisplaying_p. Also select the previously selected frame. */
13771
13772 static Lisp_Object
13773 unwind_redisplay (Lisp_Object old_frame)
13774 {
13775 redisplaying_p = 0;
13776 ensure_selected_frame (old_frame);
13777 return Qnil;
13778 }
13779
13780
13781 /* Mark the display of window W as accurate or inaccurate. If
13782 ACCURATE_P is non-zero mark display of W as accurate. If
13783 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13784 redisplay_internal is called. */
13785
13786 static void
13787 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13788 {
13789 if (BUFFERP (w->buffer))
13790 {
13791 struct buffer *b = XBUFFER (w->buffer);
13792
13793 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13794 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13795 w->last_had_star
13796 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13797
13798 if (accurate_p)
13799 {
13800 b->clip_changed = 0;
13801 b->prevent_redisplay_optimizations_p = 0;
13802
13803 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13804 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13805 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13806 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13807
13808 w->current_matrix->buffer = b;
13809 w->current_matrix->begv = BUF_BEGV (b);
13810 w->current_matrix->zv = BUF_ZV (b);
13811
13812 w->last_cursor = w->cursor;
13813 w->last_cursor_off_p = w->cursor_off_p;
13814
13815 if (w == XWINDOW (selected_window))
13816 w->last_point = BUF_PT (b);
13817 else
13818 w->last_point = marker_position (w->pointm);
13819 }
13820 }
13821
13822 if (accurate_p)
13823 {
13824 wset_window_end_valid (w, w->buffer);
13825 w->update_mode_line = 0;
13826 }
13827 }
13828
13829
13830 /* Mark the display of windows in the window tree rooted at WINDOW as
13831 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13832 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13833 be redisplayed the next time redisplay_internal is called. */
13834
13835 void
13836 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13837 {
13838 struct window *w;
13839
13840 for (; !NILP (window); window = w->next)
13841 {
13842 w = XWINDOW (window);
13843 mark_window_display_accurate_1 (w, accurate_p);
13844
13845 if (!NILP (w->vchild))
13846 mark_window_display_accurate (w->vchild, accurate_p);
13847 if (!NILP (w->hchild))
13848 mark_window_display_accurate (w->hchild, accurate_p);
13849 }
13850
13851 if (accurate_p)
13852 {
13853 update_overlay_arrows (1);
13854 }
13855 else
13856 {
13857 /* Force a thorough redisplay the next time by setting
13858 last_arrow_position and last_arrow_string to t, which is
13859 unequal to any useful value of Voverlay_arrow_... */
13860 update_overlay_arrows (-1);
13861 }
13862 }
13863
13864
13865 /* Return value in display table DP (Lisp_Char_Table *) for character
13866 C. Since a display table doesn't have any parent, we don't have to
13867 follow parent. Do not call this function directly but use the
13868 macro DISP_CHAR_VECTOR. */
13869
13870 Lisp_Object
13871 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13872 {
13873 Lisp_Object val;
13874
13875 if (ASCII_CHAR_P (c))
13876 {
13877 val = dp->ascii;
13878 if (SUB_CHAR_TABLE_P (val))
13879 val = XSUB_CHAR_TABLE (val)->contents[c];
13880 }
13881 else
13882 {
13883 Lisp_Object table;
13884
13885 XSETCHAR_TABLE (table, dp);
13886 val = char_table_ref (table, c);
13887 }
13888 if (NILP (val))
13889 val = dp->defalt;
13890 return val;
13891 }
13892
13893
13894 \f
13895 /***********************************************************************
13896 Window Redisplay
13897 ***********************************************************************/
13898
13899 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13900
13901 static void
13902 redisplay_windows (Lisp_Object window)
13903 {
13904 while (!NILP (window))
13905 {
13906 struct window *w = XWINDOW (window);
13907
13908 if (!NILP (w->hchild))
13909 redisplay_windows (w->hchild);
13910 else if (!NILP (w->vchild))
13911 redisplay_windows (w->vchild);
13912 else if (!NILP (w->buffer))
13913 {
13914 displayed_buffer = XBUFFER (w->buffer);
13915 /* Use list_of_error, not Qerror, so that
13916 we catch only errors and don't run the debugger. */
13917 internal_condition_case_1 (redisplay_window_0, window,
13918 list_of_error,
13919 redisplay_window_error);
13920 }
13921
13922 window = w->next;
13923 }
13924 }
13925
13926 static Lisp_Object
13927 redisplay_window_error (Lisp_Object ignore)
13928 {
13929 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13930 return Qnil;
13931 }
13932
13933 static Lisp_Object
13934 redisplay_window_0 (Lisp_Object window)
13935 {
13936 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13937 redisplay_window (window, 0);
13938 return Qnil;
13939 }
13940
13941 static Lisp_Object
13942 redisplay_window_1 (Lisp_Object window)
13943 {
13944 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13945 redisplay_window (window, 1);
13946 return Qnil;
13947 }
13948 \f
13949
13950 /* Set cursor position of W. PT is assumed to be displayed in ROW.
13951 DELTA and DELTA_BYTES are the numbers of characters and bytes by
13952 which positions recorded in ROW differ from current buffer
13953 positions.
13954
13955 Return 0 if cursor is not on this row, 1 otherwise. */
13956
13957 static int
13958 set_cursor_from_row (struct window *w, struct glyph_row *row,
13959 struct glyph_matrix *matrix,
13960 ptrdiff_t delta, ptrdiff_t delta_bytes,
13961 int dy, int dvpos)
13962 {
13963 struct glyph *glyph = row->glyphs[TEXT_AREA];
13964 struct glyph *end = glyph + row->used[TEXT_AREA];
13965 struct glyph *cursor = NULL;
13966 /* The last known character position in row. */
13967 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
13968 int x = row->x;
13969 ptrdiff_t pt_old = PT - delta;
13970 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
13971 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
13972 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
13973 /* A glyph beyond the edge of TEXT_AREA which we should never
13974 touch. */
13975 struct glyph *glyphs_end = end;
13976 /* Non-zero means we've found a match for cursor position, but that
13977 glyph has the avoid_cursor_p flag set. */
13978 int match_with_avoid_cursor = 0;
13979 /* Non-zero means we've seen at least one glyph that came from a
13980 display string. */
13981 int string_seen = 0;
13982 /* Largest and smallest buffer positions seen so far during scan of
13983 glyph row. */
13984 ptrdiff_t bpos_max = pos_before;
13985 ptrdiff_t bpos_min = pos_after;
13986 /* Last buffer position covered by an overlay string with an integer
13987 `cursor' property. */
13988 ptrdiff_t bpos_covered = 0;
13989 /* Non-zero means the display string on which to display the cursor
13990 comes from a text property, not from an overlay. */
13991 int string_from_text_prop = 0;
13992
13993 /* Don't even try doing anything if called for a mode-line or
13994 header-line row, since the rest of the code isn't prepared to
13995 deal with such calamities. */
13996 eassert (!row->mode_line_p);
13997 if (row->mode_line_p)
13998 return 0;
13999
14000 /* Skip over glyphs not having an object at the start and the end of
14001 the row. These are special glyphs like truncation marks on
14002 terminal frames. */
14003 if (row->displays_text_p)
14004 {
14005 if (!row->reversed_p)
14006 {
14007 while (glyph < end
14008 && INTEGERP (glyph->object)
14009 && glyph->charpos < 0)
14010 {
14011 x += glyph->pixel_width;
14012 ++glyph;
14013 }
14014 while (end > glyph
14015 && INTEGERP ((end - 1)->object)
14016 /* CHARPOS is zero for blanks and stretch glyphs
14017 inserted by extend_face_to_end_of_line. */
14018 && (end - 1)->charpos <= 0)
14019 --end;
14020 glyph_before = glyph - 1;
14021 glyph_after = end;
14022 }
14023 else
14024 {
14025 struct glyph *g;
14026
14027 /* If the glyph row is reversed, we need to process it from back
14028 to front, so swap the edge pointers. */
14029 glyphs_end = end = glyph - 1;
14030 glyph += row->used[TEXT_AREA] - 1;
14031
14032 while (glyph > end + 1
14033 && INTEGERP (glyph->object)
14034 && glyph->charpos < 0)
14035 {
14036 --glyph;
14037 x -= glyph->pixel_width;
14038 }
14039 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14040 --glyph;
14041 /* By default, in reversed rows we put the cursor on the
14042 rightmost (first in the reading order) glyph. */
14043 for (g = end + 1; g < glyph; g++)
14044 x += g->pixel_width;
14045 while (end < glyph
14046 && INTEGERP ((end + 1)->object)
14047 && (end + 1)->charpos <= 0)
14048 ++end;
14049 glyph_before = glyph + 1;
14050 glyph_after = end;
14051 }
14052 }
14053 else if (row->reversed_p)
14054 {
14055 /* In R2L rows that don't display text, put the cursor on the
14056 rightmost glyph. Case in point: an empty last line that is
14057 part of an R2L paragraph. */
14058 cursor = end - 1;
14059 /* Avoid placing the cursor on the last glyph of the row, where
14060 on terminal frames we hold the vertical border between
14061 adjacent windows. */
14062 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14063 && !WINDOW_RIGHTMOST_P (w)
14064 && cursor == row->glyphs[LAST_AREA] - 1)
14065 cursor--;
14066 x = -1; /* will be computed below, at label compute_x */
14067 }
14068
14069 /* Step 1: Try to find the glyph whose character position
14070 corresponds to point. If that's not possible, find 2 glyphs
14071 whose character positions are the closest to point, one before
14072 point, the other after it. */
14073 if (!row->reversed_p)
14074 while (/* not marched to end of glyph row */
14075 glyph < end
14076 /* glyph was not inserted by redisplay for internal purposes */
14077 && !INTEGERP (glyph->object))
14078 {
14079 if (BUFFERP (glyph->object))
14080 {
14081 ptrdiff_t dpos = glyph->charpos - pt_old;
14082
14083 if (glyph->charpos > bpos_max)
14084 bpos_max = glyph->charpos;
14085 if (glyph->charpos < bpos_min)
14086 bpos_min = glyph->charpos;
14087 if (!glyph->avoid_cursor_p)
14088 {
14089 /* If we hit point, we've found the glyph on which to
14090 display the cursor. */
14091 if (dpos == 0)
14092 {
14093 match_with_avoid_cursor = 0;
14094 break;
14095 }
14096 /* See if we've found a better approximation to
14097 POS_BEFORE or to POS_AFTER. */
14098 if (0 > dpos && dpos > pos_before - pt_old)
14099 {
14100 pos_before = glyph->charpos;
14101 glyph_before = glyph;
14102 }
14103 else if (0 < dpos && dpos < pos_after - pt_old)
14104 {
14105 pos_after = glyph->charpos;
14106 glyph_after = glyph;
14107 }
14108 }
14109 else if (dpos == 0)
14110 match_with_avoid_cursor = 1;
14111 }
14112 else if (STRINGP (glyph->object))
14113 {
14114 Lisp_Object chprop;
14115 ptrdiff_t glyph_pos = glyph->charpos;
14116
14117 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14118 glyph->object);
14119 if (!NILP (chprop))
14120 {
14121 /* If the string came from a `display' text property,
14122 look up the buffer position of that property and
14123 use that position to update bpos_max, as if we
14124 actually saw such a position in one of the row's
14125 glyphs. This helps with supporting integer values
14126 of `cursor' property on the display string in
14127 situations where most or all of the row's buffer
14128 text is completely covered by display properties,
14129 so that no glyph with valid buffer positions is
14130 ever seen in the row. */
14131 ptrdiff_t prop_pos =
14132 string_buffer_position_lim (glyph->object, pos_before,
14133 pos_after, 0);
14134
14135 if (prop_pos >= pos_before)
14136 bpos_max = prop_pos - 1;
14137 }
14138 if (INTEGERP (chprop))
14139 {
14140 bpos_covered = bpos_max + XINT (chprop);
14141 /* If the `cursor' property covers buffer positions up
14142 to and including point, we should display cursor on
14143 this glyph. Note that, if a `cursor' property on one
14144 of the string's characters has an integer value, we
14145 will break out of the loop below _before_ we get to
14146 the position match above. IOW, integer values of
14147 the `cursor' property override the "exact match for
14148 point" strategy of positioning the cursor. */
14149 /* Implementation note: bpos_max == pt_old when, e.g.,
14150 we are in an empty line, where bpos_max is set to
14151 MATRIX_ROW_START_CHARPOS, see above. */
14152 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14153 {
14154 cursor = glyph;
14155 break;
14156 }
14157 }
14158
14159 string_seen = 1;
14160 }
14161 x += glyph->pixel_width;
14162 ++glyph;
14163 }
14164 else if (glyph > end) /* row is reversed */
14165 while (!INTEGERP (glyph->object))
14166 {
14167 if (BUFFERP (glyph->object))
14168 {
14169 ptrdiff_t dpos = glyph->charpos - pt_old;
14170
14171 if (glyph->charpos > bpos_max)
14172 bpos_max = glyph->charpos;
14173 if (glyph->charpos < bpos_min)
14174 bpos_min = glyph->charpos;
14175 if (!glyph->avoid_cursor_p)
14176 {
14177 if (dpos == 0)
14178 {
14179 match_with_avoid_cursor = 0;
14180 break;
14181 }
14182 if (0 > dpos && dpos > pos_before - pt_old)
14183 {
14184 pos_before = glyph->charpos;
14185 glyph_before = glyph;
14186 }
14187 else if (0 < dpos && dpos < pos_after - pt_old)
14188 {
14189 pos_after = glyph->charpos;
14190 glyph_after = glyph;
14191 }
14192 }
14193 else if (dpos == 0)
14194 match_with_avoid_cursor = 1;
14195 }
14196 else if (STRINGP (glyph->object))
14197 {
14198 Lisp_Object chprop;
14199 ptrdiff_t glyph_pos = glyph->charpos;
14200
14201 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14202 glyph->object);
14203 if (!NILP (chprop))
14204 {
14205 ptrdiff_t prop_pos =
14206 string_buffer_position_lim (glyph->object, pos_before,
14207 pos_after, 0);
14208
14209 if (prop_pos >= pos_before)
14210 bpos_max = prop_pos - 1;
14211 }
14212 if (INTEGERP (chprop))
14213 {
14214 bpos_covered = bpos_max + XINT (chprop);
14215 /* If the `cursor' property covers buffer positions up
14216 to and including point, we should display cursor on
14217 this glyph. */
14218 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14219 {
14220 cursor = glyph;
14221 break;
14222 }
14223 }
14224 string_seen = 1;
14225 }
14226 --glyph;
14227 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14228 {
14229 x--; /* can't use any pixel_width */
14230 break;
14231 }
14232 x -= glyph->pixel_width;
14233 }
14234
14235 /* Step 2: If we didn't find an exact match for point, we need to
14236 look for a proper place to put the cursor among glyphs between
14237 GLYPH_BEFORE and GLYPH_AFTER. */
14238 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14239 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14240 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14241 {
14242 /* An empty line has a single glyph whose OBJECT is zero and
14243 whose CHARPOS is the position of a newline on that line.
14244 Note that on a TTY, there are more glyphs after that, which
14245 were produced by extend_face_to_end_of_line, but their
14246 CHARPOS is zero or negative. */
14247 int empty_line_p =
14248 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14249 && INTEGERP (glyph->object) && glyph->charpos > 0;
14250
14251 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14252 {
14253 ptrdiff_t ellipsis_pos;
14254
14255 /* Scan back over the ellipsis glyphs. */
14256 if (!row->reversed_p)
14257 {
14258 ellipsis_pos = (glyph - 1)->charpos;
14259 while (glyph > row->glyphs[TEXT_AREA]
14260 && (glyph - 1)->charpos == ellipsis_pos)
14261 glyph--, x -= glyph->pixel_width;
14262 /* That loop always goes one position too far, including
14263 the glyph before the ellipsis. So scan forward over
14264 that one. */
14265 x += glyph->pixel_width;
14266 glyph++;
14267 }
14268 else /* row is reversed */
14269 {
14270 ellipsis_pos = (glyph + 1)->charpos;
14271 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14272 && (glyph + 1)->charpos == ellipsis_pos)
14273 glyph++, x += glyph->pixel_width;
14274 x -= glyph->pixel_width;
14275 glyph--;
14276 }
14277 }
14278 else if (match_with_avoid_cursor)
14279 {
14280 cursor = glyph_after;
14281 x = -1;
14282 }
14283 else if (string_seen)
14284 {
14285 int incr = row->reversed_p ? -1 : +1;
14286
14287 /* Need to find the glyph that came out of a string which is
14288 present at point. That glyph is somewhere between
14289 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14290 positioned between POS_BEFORE and POS_AFTER in the
14291 buffer. */
14292 struct glyph *start, *stop;
14293 ptrdiff_t pos = pos_before;
14294
14295 x = -1;
14296
14297 /* If the row ends in a newline from a display string,
14298 reordering could have moved the glyphs belonging to the
14299 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14300 in this case we extend the search to the last glyph in
14301 the row that was not inserted by redisplay. */
14302 if (row->ends_in_newline_from_string_p)
14303 {
14304 glyph_after = end;
14305 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14306 }
14307
14308 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14309 correspond to POS_BEFORE and POS_AFTER, respectively. We
14310 need START and STOP in the order that corresponds to the
14311 row's direction as given by its reversed_p flag. If the
14312 directionality of characters between POS_BEFORE and
14313 POS_AFTER is the opposite of the row's base direction,
14314 these characters will have been reordered for display,
14315 and we need to reverse START and STOP. */
14316 if (!row->reversed_p)
14317 {
14318 start = min (glyph_before, glyph_after);
14319 stop = max (glyph_before, glyph_after);
14320 }
14321 else
14322 {
14323 start = max (glyph_before, glyph_after);
14324 stop = min (glyph_before, glyph_after);
14325 }
14326 for (glyph = start + incr;
14327 row->reversed_p ? glyph > stop : glyph < stop; )
14328 {
14329
14330 /* Any glyphs that come from the buffer are here because
14331 of bidi reordering. Skip them, and only pay
14332 attention to glyphs that came from some string. */
14333 if (STRINGP (glyph->object))
14334 {
14335 Lisp_Object str;
14336 ptrdiff_t tem;
14337 /* If the display property covers the newline, we
14338 need to search for it one position farther. */
14339 ptrdiff_t lim = pos_after
14340 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14341
14342 string_from_text_prop = 0;
14343 str = glyph->object;
14344 tem = string_buffer_position_lim (str, pos, lim, 0);
14345 if (tem == 0 /* from overlay */
14346 || pos <= tem)
14347 {
14348 /* If the string from which this glyph came is
14349 found in the buffer at point, or at position
14350 that is closer to point than pos_after, then
14351 we've found the glyph we've been looking for.
14352 If it comes from an overlay (tem == 0), and
14353 it has the `cursor' property on one of its
14354 glyphs, record that glyph as a candidate for
14355 displaying the cursor. (As in the
14356 unidirectional version, we will display the
14357 cursor on the last candidate we find.) */
14358 if (tem == 0
14359 || tem == pt_old
14360 || (tem - pt_old > 0 && tem < pos_after))
14361 {
14362 /* The glyphs from this string could have
14363 been reordered. Find the one with the
14364 smallest string position. Or there could
14365 be a character in the string with the
14366 `cursor' property, which means display
14367 cursor on that character's glyph. */
14368 ptrdiff_t strpos = glyph->charpos;
14369
14370 if (tem)
14371 {
14372 cursor = glyph;
14373 string_from_text_prop = 1;
14374 }
14375 for ( ;
14376 (row->reversed_p ? glyph > stop : glyph < stop)
14377 && EQ (glyph->object, str);
14378 glyph += incr)
14379 {
14380 Lisp_Object cprop;
14381 ptrdiff_t gpos = glyph->charpos;
14382
14383 cprop = Fget_char_property (make_number (gpos),
14384 Qcursor,
14385 glyph->object);
14386 if (!NILP (cprop))
14387 {
14388 cursor = glyph;
14389 break;
14390 }
14391 if (tem && glyph->charpos < strpos)
14392 {
14393 strpos = glyph->charpos;
14394 cursor = glyph;
14395 }
14396 }
14397
14398 if (tem == pt_old
14399 || (tem - pt_old > 0 && tem < pos_after))
14400 goto compute_x;
14401 }
14402 if (tem)
14403 pos = tem + 1; /* don't find previous instances */
14404 }
14405 /* This string is not what we want; skip all of the
14406 glyphs that came from it. */
14407 while ((row->reversed_p ? glyph > stop : glyph < stop)
14408 && EQ (glyph->object, str))
14409 glyph += incr;
14410 }
14411 else
14412 glyph += incr;
14413 }
14414
14415 /* If we reached the end of the line, and END was from a string,
14416 the cursor is not on this line. */
14417 if (cursor == NULL
14418 && (row->reversed_p ? glyph <= end : glyph >= end)
14419 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14420 && STRINGP (end->object)
14421 && row->continued_p)
14422 return 0;
14423 }
14424 /* A truncated row may not include PT among its character positions.
14425 Setting the cursor inside the scroll margin will trigger
14426 recalculation of hscroll in hscroll_window_tree. But if a
14427 display string covers point, defer to the string-handling
14428 code below to figure this out. */
14429 else if (row->truncated_on_left_p && pt_old < bpos_min)
14430 {
14431 cursor = glyph_before;
14432 x = -1;
14433 }
14434 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14435 /* Zero-width characters produce no glyphs. */
14436 || (!empty_line_p
14437 && (row->reversed_p
14438 ? glyph_after > glyphs_end
14439 : glyph_after < glyphs_end)))
14440 {
14441 cursor = glyph_after;
14442 x = -1;
14443 }
14444 }
14445
14446 compute_x:
14447 if (cursor != NULL)
14448 glyph = cursor;
14449 else if (glyph == glyphs_end
14450 && pos_before == pos_after
14451 && STRINGP ((row->reversed_p
14452 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14453 : row->glyphs[TEXT_AREA])->object))
14454 {
14455 /* If all the glyphs of this row came from strings, put the
14456 cursor on the first glyph of the row. This avoids having the
14457 cursor outside of the text area in this very rare and hard
14458 use case. */
14459 glyph =
14460 row->reversed_p
14461 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14462 : row->glyphs[TEXT_AREA];
14463 }
14464 if (x < 0)
14465 {
14466 struct glyph *g;
14467
14468 /* Need to compute x that corresponds to GLYPH. */
14469 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14470 {
14471 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14472 emacs_abort ();
14473 x += g->pixel_width;
14474 }
14475 }
14476
14477 /* ROW could be part of a continued line, which, under bidi
14478 reordering, might have other rows whose start and end charpos
14479 occlude point. Only set w->cursor if we found a better
14480 approximation to the cursor position than we have from previously
14481 examined candidate rows belonging to the same continued line. */
14482 if (/* we already have a candidate row */
14483 w->cursor.vpos >= 0
14484 /* that candidate is not the row we are processing */
14485 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14486 /* Make sure cursor.vpos specifies a row whose start and end
14487 charpos occlude point, and it is valid candidate for being a
14488 cursor-row. This is because some callers of this function
14489 leave cursor.vpos at the row where the cursor was displayed
14490 during the last redisplay cycle. */
14491 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14492 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14493 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14494 {
14495 struct glyph *g1 =
14496 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14497
14498 /* Don't consider glyphs that are outside TEXT_AREA. */
14499 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14500 return 0;
14501 /* Keep the candidate whose buffer position is the closest to
14502 point or has the `cursor' property. */
14503 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14504 w->cursor.hpos >= 0
14505 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14506 && ((BUFFERP (g1->object)
14507 && (g1->charpos == pt_old /* an exact match always wins */
14508 || (BUFFERP (glyph->object)
14509 && eabs (g1->charpos - pt_old)
14510 < eabs (glyph->charpos - pt_old))))
14511 /* previous candidate is a glyph from a string that has
14512 a non-nil `cursor' property */
14513 || (STRINGP (g1->object)
14514 && (!NILP (Fget_char_property (make_number (g1->charpos),
14515 Qcursor, g1->object))
14516 /* previous candidate is from the same display
14517 string as this one, and the display string
14518 came from a text property */
14519 || (EQ (g1->object, glyph->object)
14520 && string_from_text_prop)
14521 /* this candidate is from newline and its
14522 position is not an exact match */
14523 || (INTEGERP (glyph->object)
14524 && glyph->charpos != pt_old)))))
14525 return 0;
14526 /* If this candidate gives an exact match, use that. */
14527 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14528 /* If this candidate is a glyph created for the
14529 terminating newline of a line, and point is on that
14530 newline, it wins because it's an exact match. */
14531 || (!row->continued_p
14532 && INTEGERP (glyph->object)
14533 && glyph->charpos == 0
14534 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14535 /* Otherwise, keep the candidate that comes from a row
14536 spanning less buffer positions. This may win when one or
14537 both candidate positions are on glyphs that came from
14538 display strings, for which we cannot compare buffer
14539 positions. */
14540 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14541 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14542 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14543 return 0;
14544 }
14545 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14546 w->cursor.x = x;
14547 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14548 w->cursor.y = row->y + dy;
14549
14550 if (w == XWINDOW (selected_window))
14551 {
14552 if (!row->continued_p
14553 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14554 && row->x == 0)
14555 {
14556 this_line_buffer = XBUFFER (w->buffer);
14557
14558 CHARPOS (this_line_start_pos)
14559 = MATRIX_ROW_START_CHARPOS (row) + delta;
14560 BYTEPOS (this_line_start_pos)
14561 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14562
14563 CHARPOS (this_line_end_pos)
14564 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14565 BYTEPOS (this_line_end_pos)
14566 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14567
14568 this_line_y = w->cursor.y;
14569 this_line_pixel_height = row->height;
14570 this_line_vpos = w->cursor.vpos;
14571 this_line_start_x = row->x;
14572 }
14573 else
14574 CHARPOS (this_line_start_pos) = 0;
14575 }
14576
14577 return 1;
14578 }
14579
14580
14581 /* Run window scroll functions, if any, for WINDOW with new window
14582 start STARTP. Sets the window start of WINDOW to that position.
14583
14584 We assume that the window's buffer is really current. */
14585
14586 static struct text_pos
14587 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14588 {
14589 struct window *w = XWINDOW (window);
14590 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14591
14592 if (current_buffer != XBUFFER (w->buffer))
14593 emacs_abort ();
14594
14595 if (!NILP (Vwindow_scroll_functions))
14596 {
14597 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14598 make_number (CHARPOS (startp)));
14599 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14600 /* In case the hook functions switch buffers. */
14601 set_buffer_internal (XBUFFER (w->buffer));
14602 }
14603
14604 return startp;
14605 }
14606
14607
14608 /* Make sure the line containing the cursor is fully visible.
14609 A value of 1 means there is nothing to be done.
14610 (Either the line is fully visible, or it cannot be made so,
14611 or we cannot tell.)
14612
14613 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14614 is higher than window.
14615
14616 A value of 0 means the caller should do scrolling
14617 as if point had gone off the screen. */
14618
14619 static int
14620 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14621 {
14622 struct glyph_matrix *matrix;
14623 struct glyph_row *row;
14624 int window_height;
14625
14626 if (!make_cursor_line_fully_visible_p)
14627 return 1;
14628
14629 /* It's not always possible to find the cursor, e.g, when a window
14630 is full of overlay strings. Don't do anything in that case. */
14631 if (w->cursor.vpos < 0)
14632 return 1;
14633
14634 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14635 row = MATRIX_ROW (matrix, w->cursor.vpos);
14636
14637 /* If the cursor row is not partially visible, there's nothing to do. */
14638 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14639 return 1;
14640
14641 /* If the row the cursor is in is taller than the window's height,
14642 it's not clear what to do, so do nothing. */
14643 window_height = window_box_height (w);
14644 if (row->height >= window_height)
14645 {
14646 if (!force_p || MINI_WINDOW_P (w)
14647 || w->vscroll || w->cursor.vpos == 0)
14648 return 1;
14649 }
14650 return 0;
14651 }
14652
14653
14654 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14655 non-zero means only WINDOW is redisplayed in redisplay_internal.
14656 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14657 in redisplay_window to bring a partially visible line into view in
14658 the case that only the cursor has moved.
14659
14660 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14661 last screen line's vertical height extends past the end of the screen.
14662
14663 Value is
14664
14665 1 if scrolling succeeded
14666
14667 0 if scrolling didn't find point.
14668
14669 -1 if new fonts have been loaded so that we must interrupt
14670 redisplay, adjust glyph matrices, and try again. */
14671
14672 enum
14673 {
14674 SCROLLING_SUCCESS,
14675 SCROLLING_FAILED,
14676 SCROLLING_NEED_LARGER_MATRICES
14677 };
14678
14679 /* If scroll-conservatively is more than this, never recenter.
14680
14681 If you change this, don't forget to update the doc string of
14682 `scroll-conservatively' and the Emacs manual. */
14683 #define SCROLL_LIMIT 100
14684
14685 static int
14686 try_scrolling (Lisp_Object window, int just_this_one_p,
14687 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14688 int temp_scroll_step, int last_line_misfit)
14689 {
14690 struct window *w = XWINDOW (window);
14691 struct frame *f = XFRAME (w->frame);
14692 struct text_pos pos, startp;
14693 struct it it;
14694 int this_scroll_margin, scroll_max, rc, height;
14695 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14696 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14697 Lisp_Object aggressive;
14698 /* We will never try scrolling more than this number of lines. */
14699 int scroll_limit = SCROLL_LIMIT;
14700
14701 #ifdef GLYPH_DEBUG
14702 debug_method_add (w, "try_scrolling");
14703 #endif
14704
14705 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14706
14707 /* Compute scroll margin height in pixels. We scroll when point is
14708 within this distance from the top or bottom of the window. */
14709 if (scroll_margin > 0)
14710 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14711 * FRAME_LINE_HEIGHT (f);
14712 else
14713 this_scroll_margin = 0;
14714
14715 /* Force arg_scroll_conservatively to have a reasonable value, to
14716 avoid scrolling too far away with slow move_it_* functions. Note
14717 that the user can supply scroll-conservatively equal to
14718 `most-positive-fixnum', which can be larger than INT_MAX. */
14719 if (arg_scroll_conservatively > scroll_limit)
14720 {
14721 arg_scroll_conservatively = scroll_limit + 1;
14722 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14723 }
14724 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14725 /* Compute how much we should try to scroll maximally to bring
14726 point into view. */
14727 scroll_max = (max (scroll_step,
14728 max (arg_scroll_conservatively, temp_scroll_step))
14729 * FRAME_LINE_HEIGHT (f));
14730 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14731 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14732 /* We're trying to scroll because of aggressive scrolling but no
14733 scroll_step is set. Choose an arbitrary one. */
14734 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14735 else
14736 scroll_max = 0;
14737
14738 too_near_end:
14739
14740 /* Decide whether to scroll down. */
14741 if (PT > CHARPOS (startp))
14742 {
14743 int scroll_margin_y;
14744
14745 /* Compute the pixel ypos of the scroll margin, then move IT to
14746 either that ypos or PT, whichever comes first. */
14747 start_display (&it, w, startp);
14748 scroll_margin_y = it.last_visible_y - this_scroll_margin
14749 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14750 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14751 (MOVE_TO_POS | MOVE_TO_Y));
14752
14753 if (PT > CHARPOS (it.current.pos))
14754 {
14755 int y0 = line_bottom_y (&it);
14756 /* Compute how many pixels below window bottom to stop searching
14757 for PT. This avoids costly search for PT that is far away if
14758 the user limited scrolling by a small number of lines, but
14759 always finds PT if scroll_conservatively is set to a large
14760 number, such as most-positive-fixnum. */
14761 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14762 int y_to_move = it.last_visible_y + slack;
14763
14764 /* Compute the distance from the scroll margin to PT or to
14765 the scroll limit, whichever comes first. This should
14766 include the height of the cursor line, to make that line
14767 fully visible. */
14768 move_it_to (&it, PT, -1, y_to_move,
14769 -1, MOVE_TO_POS | MOVE_TO_Y);
14770 dy = line_bottom_y (&it) - y0;
14771
14772 if (dy > scroll_max)
14773 return SCROLLING_FAILED;
14774
14775 if (dy > 0)
14776 scroll_down_p = 1;
14777 }
14778 }
14779
14780 if (scroll_down_p)
14781 {
14782 /* Point is in or below the bottom scroll margin, so move the
14783 window start down. If scrolling conservatively, move it just
14784 enough down to make point visible. If scroll_step is set,
14785 move it down by scroll_step. */
14786 if (arg_scroll_conservatively)
14787 amount_to_scroll
14788 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14789 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14790 else if (scroll_step || temp_scroll_step)
14791 amount_to_scroll = scroll_max;
14792 else
14793 {
14794 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14795 height = WINDOW_BOX_TEXT_HEIGHT (w);
14796 if (NUMBERP (aggressive))
14797 {
14798 double float_amount = XFLOATINT (aggressive) * height;
14799 int aggressive_scroll = float_amount;
14800 if (aggressive_scroll == 0 && float_amount > 0)
14801 aggressive_scroll = 1;
14802 /* Don't let point enter the scroll margin near top of
14803 the window. This could happen if the value of
14804 scroll_up_aggressively is too large and there are
14805 non-zero margins, because scroll_up_aggressively
14806 means put point that fraction of window height
14807 _from_the_bottom_margin_. */
14808 if (aggressive_scroll + 2*this_scroll_margin > height)
14809 aggressive_scroll = height - 2*this_scroll_margin;
14810 amount_to_scroll = dy + aggressive_scroll;
14811 }
14812 }
14813
14814 if (amount_to_scroll <= 0)
14815 return SCROLLING_FAILED;
14816
14817 start_display (&it, w, startp);
14818 if (arg_scroll_conservatively <= scroll_limit)
14819 move_it_vertically (&it, amount_to_scroll);
14820 else
14821 {
14822 /* Extra precision for users who set scroll-conservatively
14823 to a large number: make sure the amount we scroll
14824 the window start is never less than amount_to_scroll,
14825 which was computed as distance from window bottom to
14826 point. This matters when lines at window top and lines
14827 below window bottom have different height. */
14828 struct it it1;
14829 void *it1data = NULL;
14830 /* We use a temporary it1 because line_bottom_y can modify
14831 its argument, if it moves one line down; see there. */
14832 int start_y;
14833
14834 SAVE_IT (it1, it, it1data);
14835 start_y = line_bottom_y (&it1);
14836 do {
14837 RESTORE_IT (&it, &it, it1data);
14838 move_it_by_lines (&it, 1);
14839 SAVE_IT (it1, it, it1data);
14840 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14841 }
14842
14843 /* If STARTP is unchanged, move it down another screen line. */
14844 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14845 move_it_by_lines (&it, 1);
14846 startp = it.current.pos;
14847 }
14848 else
14849 {
14850 struct text_pos scroll_margin_pos = startp;
14851
14852 /* See if point is inside the scroll margin at the top of the
14853 window. */
14854 if (this_scroll_margin)
14855 {
14856 start_display (&it, w, startp);
14857 move_it_vertically (&it, this_scroll_margin);
14858 scroll_margin_pos = it.current.pos;
14859 }
14860
14861 if (PT < CHARPOS (scroll_margin_pos))
14862 {
14863 /* Point is in the scroll margin at the top of the window or
14864 above what is displayed in the window. */
14865 int y0, y_to_move;
14866
14867 /* Compute the vertical distance from PT to the scroll
14868 margin position. Move as far as scroll_max allows, or
14869 one screenful, or 10 screen lines, whichever is largest.
14870 Give up if distance is greater than scroll_max or if we
14871 didn't reach the scroll margin position. */
14872 SET_TEXT_POS (pos, PT, PT_BYTE);
14873 start_display (&it, w, pos);
14874 y0 = it.current_y;
14875 y_to_move = max (it.last_visible_y,
14876 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14877 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14878 y_to_move, -1,
14879 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14880 dy = it.current_y - y0;
14881 if (dy > scroll_max
14882 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14883 return SCROLLING_FAILED;
14884
14885 /* Compute new window start. */
14886 start_display (&it, w, startp);
14887
14888 if (arg_scroll_conservatively)
14889 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14890 max (scroll_step, temp_scroll_step));
14891 else if (scroll_step || temp_scroll_step)
14892 amount_to_scroll = scroll_max;
14893 else
14894 {
14895 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14896 height = WINDOW_BOX_TEXT_HEIGHT (w);
14897 if (NUMBERP (aggressive))
14898 {
14899 double float_amount = XFLOATINT (aggressive) * height;
14900 int aggressive_scroll = float_amount;
14901 if (aggressive_scroll == 0 && float_amount > 0)
14902 aggressive_scroll = 1;
14903 /* Don't let point enter the scroll margin near
14904 bottom of the window, if the value of
14905 scroll_down_aggressively happens to be too
14906 large. */
14907 if (aggressive_scroll + 2*this_scroll_margin > height)
14908 aggressive_scroll = height - 2*this_scroll_margin;
14909 amount_to_scroll = dy + aggressive_scroll;
14910 }
14911 }
14912
14913 if (amount_to_scroll <= 0)
14914 return SCROLLING_FAILED;
14915
14916 move_it_vertically_backward (&it, amount_to_scroll);
14917 startp = it.current.pos;
14918 }
14919 }
14920
14921 /* Run window scroll functions. */
14922 startp = run_window_scroll_functions (window, startp);
14923
14924 /* Display the window. Give up if new fonts are loaded, or if point
14925 doesn't appear. */
14926 if (!try_window (window, startp, 0))
14927 rc = SCROLLING_NEED_LARGER_MATRICES;
14928 else if (w->cursor.vpos < 0)
14929 {
14930 clear_glyph_matrix (w->desired_matrix);
14931 rc = SCROLLING_FAILED;
14932 }
14933 else
14934 {
14935 /* Maybe forget recorded base line for line number display. */
14936 if (!just_this_one_p
14937 || current_buffer->clip_changed
14938 || BEG_UNCHANGED < CHARPOS (startp))
14939 wset_base_line_number (w, Qnil);
14940
14941 /* If cursor ends up on a partially visible line,
14942 treat that as being off the bottom of the screen. */
14943 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
14944 /* It's possible that the cursor is on the first line of the
14945 buffer, which is partially obscured due to a vscroll
14946 (Bug#7537). In that case, avoid looping forever . */
14947 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
14948 {
14949 clear_glyph_matrix (w->desired_matrix);
14950 ++extra_scroll_margin_lines;
14951 goto too_near_end;
14952 }
14953 rc = SCROLLING_SUCCESS;
14954 }
14955
14956 return rc;
14957 }
14958
14959
14960 /* Compute a suitable window start for window W if display of W starts
14961 on a continuation line. Value is non-zero if a new window start
14962 was computed.
14963
14964 The new window start will be computed, based on W's width, starting
14965 from the start of the continued line. It is the start of the
14966 screen line with the minimum distance from the old start W->start. */
14967
14968 static int
14969 compute_window_start_on_continuation_line (struct window *w)
14970 {
14971 struct text_pos pos, start_pos;
14972 int window_start_changed_p = 0;
14973
14974 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
14975
14976 /* If window start is on a continuation line... Window start may be
14977 < BEGV in case there's invisible text at the start of the
14978 buffer (M-x rmail, for example). */
14979 if (CHARPOS (start_pos) > BEGV
14980 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
14981 {
14982 struct it it;
14983 struct glyph_row *row;
14984
14985 /* Handle the case that the window start is out of range. */
14986 if (CHARPOS (start_pos) < BEGV)
14987 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
14988 else if (CHARPOS (start_pos) > ZV)
14989 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
14990
14991 /* Find the start of the continued line. This should be fast
14992 because scan_buffer is fast (newline cache). */
14993 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
14994 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
14995 row, DEFAULT_FACE_ID);
14996 reseat_at_previous_visible_line_start (&it);
14997
14998 /* If the line start is "too far" away from the window start,
14999 say it takes too much time to compute a new window start. */
15000 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15001 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15002 {
15003 int min_distance, distance;
15004
15005 /* Move forward by display lines to find the new window
15006 start. If window width was enlarged, the new start can
15007 be expected to be > the old start. If window width was
15008 decreased, the new window start will be < the old start.
15009 So, we're looking for the display line start with the
15010 minimum distance from the old window start. */
15011 pos = it.current.pos;
15012 min_distance = INFINITY;
15013 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15014 distance < min_distance)
15015 {
15016 min_distance = distance;
15017 pos = it.current.pos;
15018 move_it_by_lines (&it, 1);
15019 }
15020
15021 /* Set the window start there. */
15022 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15023 window_start_changed_p = 1;
15024 }
15025 }
15026
15027 return window_start_changed_p;
15028 }
15029
15030
15031 /* Try cursor movement in case text has not changed in window WINDOW,
15032 with window start STARTP. Value is
15033
15034 CURSOR_MOVEMENT_SUCCESS if successful
15035
15036 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15037
15038 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15039 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15040 we want to scroll as if scroll-step were set to 1. See the code.
15041
15042 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15043 which case we have to abort this redisplay, and adjust matrices
15044 first. */
15045
15046 enum
15047 {
15048 CURSOR_MOVEMENT_SUCCESS,
15049 CURSOR_MOVEMENT_CANNOT_BE_USED,
15050 CURSOR_MOVEMENT_MUST_SCROLL,
15051 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15052 };
15053
15054 static int
15055 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15056 {
15057 struct window *w = XWINDOW (window);
15058 struct frame *f = XFRAME (w->frame);
15059 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15060
15061 #ifdef GLYPH_DEBUG
15062 if (inhibit_try_cursor_movement)
15063 return rc;
15064 #endif
15065
15066 /* Previously, there was a check for Lisp integer in the
15067 if-statement below. Now, this field is converted to
15068 ptrdiff_t, thus zero means invalid position in a buffer. */
15069 eassert (w->last_point > 0);
15070
15071 /* Handle case where text has not changed, only point, and it has
15072 not moved off the frame. */
15073 if (/* Point may be in this window. */
15074 PT >= CHARPOS (startp)
15075 /* Selective display hasn't changed. */
15076 && !current_buffer->clip_changed
15077 /* Function force-mode-line-update is used to force a thorough
15078 redisplay. It sets either windows_or_buffers_changed or
15079 update_mode_lines. So don't take a shortcut here for these
15080 cases. */
15081 && !update_mode_lines
15082 && !windows_or_buffers_changed
15083 && !cursor_type_changed
15084 /* Can't use this case if highlighting a region. When a
15085 region exists, cursor movement has to do more than just
15086 set the cursor. */
15087 && markpos_of_region () < 0
15088 && NILP (w->region_showing)
15089 && NILP (Vshow_trailing_whitespace)
15090 /* This code is not used for mini-buffer for the sake of the case
15091 of redisplaying to replace an echo area message; since in
15092 that case the mini-buffer contents per se are usually
15093 unchanged. This code is of no real use in the mini-buffer
15094 since the handling of this_line_start_pos, etc., in redisplay
15095 handles the same cases. */
15096 && !EQ (window, minibuf_window)
15097 /* When splitting windows or for new windows, it happens that
15098 redisplay is called with a nil window_end_vpos or one being
15099 larger than the window. This should really be fixed in
15100 window.c. I don't have this on my list, now, so we do
15101 approximately the same as the old redisplay code. --gerd. */
15102 && INTEGERP (w->window_end_vpos)
15103 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
15104 && (FRAME_WINDOW_P (f)
15105 || !overlay_arrow_in_current_buffer_p ()))
15106 {
15107 int this_scroll_margin, top_scroll_margin;
15108 struct glyph_row *row = NULL;
15109
15110 #ifdef GLYPH_DEBUG
15111 debug_method_add (w, "cursor movement");
15112 #endif
15113
15114 /* Scroll if point within this distance from the top or bottom
15115 of the window. This is a pixel value. */
15116 if (scroll_margin > 0)
15117 {
15118 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15119 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15120 }
15121 else
15122 this_scroll_margin = 0;
15123
15124 top_scroll_margin = this_scroll_margin;
15125 if (WINDOW_WANTS_HEADER_LINE_P (w))
15126 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15127
15128 /* Start with the row the cursor was displayed during the last
15129 not paused redisplay. Give up if that row is not valid. */
15130 if (w->last_cursor.vpos < 0
15131 || w->last_cursor.vpos >= w->current_matrix->nrows)
15132 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15133 else
15134 {
15135 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15136 if (row->mode_line_p)
15137 ++row;
15138 if (!row->enabled_p)
15139 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15140 }
15141
15142 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15143 {
15144 int scroll_p = 0, must_scroll = 0;
15145 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15146
15147 if (PT > w->last_point)
15148 {
15149 /* Point has moved forward. */
15150 while (MATRIX_ROW_END_CHARPOS (row) < PT
15151 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15152 {
15153 eassert (row->enabled_p);
15154 ++row;
15155 }
15156
15157 /* If the end position of a row equals the start
15158 position of the next row, and PT is at that position,
15159 we would rather display cursor in the next line. */
15160 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15161 && MATRIX_ROW_END_CHARPOS (row) == PT
15162 && row < w->current_matrix->rows
15163 + w->current_matrix->nrows - 1
15164 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15165 && !cursor_row_p (row))
15166 ++row;
15167
15168 /* If within the scroll margin, scroll. Note that
15169 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15170 the next line would be drawn, and that
15171 this_scroll_margin can be zero. */
15172 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15173 || PT > MATRIX_ROW_END_CHARPOS (row)
15174 /* Line is completely visible last line in window
15175 and PT is to be set in the next line. */
15176 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15177 && PT == MATRIX_ROW_END_CHARPOS (row)
15178 && !row->ends_at_zv_p
15179 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15180 scroll_p = 1;
15181 }
15182 else if (PT < w->last_point)
15183 {
15184 /* Cursor has to be moved backward. Note that PT >=
15185 CHARPOS (startp) because of the outer if-statement. */
15186 while (!row->mode_line_p
15187 && (MATRIX_ROW_START_CHARPOS (row) > PT
15188 || (MATRIX_ROW_START_CHARPOS (row) == PT
15189 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15190 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15191 row > w->current_matrix->rows
15192 && (row-1)->ends_in_newline_from_string_p))))
15193 && (row->y > top_scroll_margin
15194 || CHARPOS (startp) == BEGV))
15195 {
15196 eassert (row->enabled_p);
15197 --row;
15198 }
15199
15200 /* Consider the following case: Window starts at BEGV,
15201 there is invisible, intangible text at BEGV, so that
15202 display starts at some point START > BEGV. It can
15203 happen that we are called with PT somewhere between
15204 BEGV and START. Try to handle that case. */
15205 if (row < w->current_matrix->rows
15206 || row->mode_line_p)
15207 {
15208 row = w->current_matrix->rows;
15209 if (row->mode_line_p)
15210 ++row;
15211 }
15212
15213 /* Due to newlines in overlay strings, we may have to
15214 skip forward over overlay strings. */
15215 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15216 && MATRIX_ROW_END_CHARPOS (row) == PT
15217 && !cursor_row_p (row))
15218 ++row;
15219
15220 /* If within the scroll margin, scroll. */
15221 if (row->y < top_scroll_margin
15222 && CHARPOS (startp) != BEGV)
15223 scroll_p = 1;
15224 }
15225 else
15226 {
15227 /* Cursor did not move. So don't scroll even if cursor line
15228 is partially visible, as it was so before. */
15229 rc = CURSOR_MOVEMENT_SUCCESS;
15230 }
15231
15232 if (PT < MATRIX_ROW_START_CHARPOS (row)
15233 || PT > MATRIX_ROW_END_CHARPOS (row))
15234 {
15235 /* if PT is not in the glyph row, give up. */
15236 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15237 must_scroll = 1;
15238 }
15239 else if (rc != CURSOR_MOVEMENT_SUCCESS
15240 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15241 {
15242 struct glyph_row *row1;
15243
15244 /* If rows are bidi-reordered and point moved, back up
15245 until we find a row that does not belong to a
15246 continuation line. This is because we must consider
15247 all rows of a continued line as candidates for the
15248 new cursor positioning, since row start and end
15249 positions change non-linearly with vertical position
15250 in such rows. */
15251 /* FIXME: Revisit this when glyph ``spilling'' in
15252 continuation lines' rows is implemented for
15253 bidi-reordered rows. */
15254 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15255 MATRIX_ROW_CONTINUATION_LINE_P (row);
15256 --row)
15257 {
15258 /* If we hit the beginning of the displayed portion
15259 without finding the first row of a continued
15260 line, give up. */
15261 if (row <= row1)
15262 {
15263 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15264 break;
15265 }
15266 eassert (row->enabled_p);
15267 }
15268 }
15269 if (must_scroll)
15270 ;
15271 else if (rc != CURSOR_MOVEMENT_SUCCESS
15272 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15273 /* Make sure this isn't a header line by any chance, since
15274 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15275 && !row->mode_line_p
15276 && make_cursor_line_fully_visible_p)
15277 {
15278 if (PT == MATRIX_ROW_END_CHARPOS (row)
15279 && !row->ends_at_zv_p
15280 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15281 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15282 else if (row->height > window_box_height (w))
15283 {
15284 /* If we end up in a partially visible line, let's
15285 make it fully visible, except when it's taller
15286 than the window, in which case we can't do much
15287 about it. */
15288 *scroll_step = 1;
15289 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15290 }
15291 else
15292 {
15293 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15294 if (!cursor_row_fully_visible_p (w, 0, 1))
15295 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15296 else
15297 rc = CURSOR_MOVEMENT_SUCCESS;
15298 }
15299 }
15300 else if (scroll_p)
15301 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15302 else if (rc != CURSOR_MOVEMENT_SUCCESS
15303 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15304 {
15305 /* With bidi-reordered rows, there could be more than
15306 one candidate row whose start and end positions
15307 occlude point. We need to let set_cursor_from_row
15308 find the best candidate. */
15309 /* FIXME: Revisit this when glyph ``spilling'' in
15310 continuation lines' rows is implemented for
15311 bidi-reordered rows. */
15312 int rv = 0;
15313
15314 do
15315 {
15316 int at_zv_p = 0, exact_match_p = 0;
15317
15318 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15319 && PT <= MATRIX_ROW_END_CHARPOS (row)
15320 && cursor_row_p (row))
15321 rv |= set_cursor_from_row (w, row, w->current_matrix,
15322 0, 0, 0, 0);
15323 /* As soon as we've found the exact match for point,
15324 or the first suitable row whose ends_at_zv_p flag
15325 is set, we are done. */
15326 at_zv_p =
15327 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15328 if (rv && !at_zv_p
15329 && w->cursor.hpos >= 0
15330 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15331 w->cursor.vpos))
15332 {
15333 struct glyph_row *candidate =
15334 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15335 struct glyph *g =
15336 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15337 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15338
15339 exact_match_p =
15340 (BUFFERP (g->object) && g->charpos == PT)
15341 || (INTEGERP (g->object)
15342 && (g->charpos == PT
15343 || (g->charpos == 0 && endpos - 1 == PT)));
15344 }
15345 if (rv && (at_zv_p || exact_match_p))
15346 {
15347 rc = CURSOR_MOVEMENT_SUCCESS;
15348 break;
15349 }
15350 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15351 break;
15352 ++row;
15353 }
15354 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15355 || row->continued_p)
15356 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15357 || (MATRIX_ROW_START_CHARPOS (row) == PT
15358 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15359 /* If we didn't find any candidate rows, or exited the
15360 loop before all the candidates were examined, signal
15361 to the caller that this method failed. */
15362 if (rc != CURSOR_MOVEMENT_SUCCESS
15363 && !(rv
15364 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15365 && !row->continued_p))
15366 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15367 else if (rv)
15368 rc = CURSOR_MOVEMENT_SUCCESS;
15369 }
15370 else
15371 {
15372 do
15373 {
15374 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15375 {
15376 rc = CURSOR_MOVEMENT_SUCCESS;
15377 break;
15378 }
15379 ++row;
15380 }
15381 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15382 && MATRIX_ROW_START_CHARPOS (row) == PT
15383 && cursor_row_p (row));
15384 }
15385 }
15386 }
15387
15388 return rc;
15389 }
15390
15391 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15392 static
15393 #endif
15394 void
15395 set_vertical_scroll_bar (struct window *w)
15396 {
15397 ptrdiff_t start, end, whole;
15398
15399 /* Calculate the start and end positions for the current window.
15400 At some point, it would be nice to choose between scrollbars
15401 which reflect the whole buffer size, with special markers
15402 indicating narrowing, and scrollbars which reflect only the
15403 visible region.
15404
15405 Note that mini-buffers sometimes aren't displaying any text. */
15406 if (!MINI_WINDOW_P (w)
15407 || (w == XWINDOW (minibuf_window)
15408 && NILP (echo_area_buffer[0])))
15409 {
15410 struct buffer *buf = XBUFFER (w->buffer);
15411 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15412 start = marker_position (w->start) - BUF_BEGV (buf);
15413 /* I don't think this is guaranteed to be right. For the
15414 moment, we'll pretend it is. */
15415 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15416
15417 if (end < start)
15418 end = start;
15419 if (whole < (end - start))
15420 whole = end - start;
15421 }
15422 else
15423 start = end = whole = 0;
15424
15425 /* Indicate what this scroll bar ought to be displaying now. */
15426 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15427 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15428 (w, end - start, whole, start);
15429 }
15430
15431
15432 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15433 selected_window is redisplayed.
15434
15435 We can return without actually redisplaying the window if
15436 fonts_changed_p. In that case, redisplay_internal will
15437 retry. */
15438
15439 static void
15440 redisplay_window (Lisp_Object window, int just_this_one_p)
15441 {
15442 struct window *w = XWINDOW (window);
15443 struct frame *f = XFRAME (w->frame);
15444 struct buffer *buffer = XBUFFER (w->buffer);
15445 struct buffer *old = current_buffer;
15446 struct text_pos lpoint, opoint, startp;
15447 int update_mode_line;
15448 int tem;
15449 struct it it;
15450 /* Record it now because it's overwritten. */
15451 int current_matrix_up_to_date_p = 0;
15452 int used_current_matrix_p = 0;
15453 /* This is less strict than current_matrix_up_to_date_p.
15454 It indicates that the buffer contents and narrowing are unchanged. */
15455 int buffer_unchanged_p = 0;
15456 int temp_scroll_step = 0;
15457 ptrdiff_t count = SPECPDL_INDEX ();
15458 int rc;
15459 int centering_position = -1;
15460 int last_line_misfit = 0;
15461 ptrdiff_t beg_unchanged, end_unchanged;
15462
15463 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15464 opoint = lpoint;
15465
15466 /* W must be a leaf window here. */
15467 eassert (!NILP (w->buffer));
15468 #ifdef GLYPH_DEBUG
15469 *w->desired_matrix->method = 0;
15470 #endif
15471
15472 restart:
15473 reconsider_clip_changes (w, buffer);
15474
15475 /* Has the mode line to be updated? */
15476 update_mode_line = (w->update_mode_line
15477 || update_mode_lines
15478 || buffer->clip_changed
15479 || buffer->prevent_redisplay_optimizations_p);
15480
15481 if (MINI_WINDOW_P (w))
15482 {
15483 if (w == XWINDOW (echo_area_window)
15484 && !NILP (echo_area_buffer[0]))
15485 {
15486 if (update_mode_line)
15487 /* We may have to update a tty frame's menu bar or a
15488 tool-bar. Example `M-x C-h C-h C-g'. */
15489 goto finish_menu_bars;
15490 else
15491 /* We've already displayed the echo area glyphs in this window. */
15492 goto finish_scroll_bars;
15493 }
15494 else if ((w != XWINDOW (minibuf_window)
15495 || minibuf_level == 0)
15496 /* When buffer is nonempty, redisplay window normally. */
15497 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15498 /* Quail displays non-mini buffers in minibuffer window.
15499 In that case, redisplay the window normally. */
15500 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15501 {
15502 /* W is a mini-buffer window, but it's not active, so clear
15503 it. */
15504 int yb = window_text_bottom_y (w);
15505 struct glyph_row *row;
15506 int y;
15507
15508 for (y = 0, row = w->desired_matrix->rows;
15509 y < yb;
15510 y += row->height, ++row)
15511 blank_row (w, row, y);
15512 goto finish_scroll_bars;
15513 }
15514
15515 clear_glyph_matrix (w->desired_matrix);
15516 }
15517
15518 /* Otherwise set up data on this window; select its buffer and point
15519 value. */
15520 /* Really select the buffer, for the sake of buffer-local
15521 variables. */
15522 set_buffer_internal_1 (XBUFFER (w->buffer));
15523
15524 current_matrix_up_to_date_p
15525 = (!NILP (w->window_end_valid)
15526 && !current_buffer->clip_changed
15527 && !current_buffer->prevent_redisplay_optimizations_p
15528 && !window_outdated (w));
15529
15530 /* Run the window-bottom-change-functions
15531 if it is possible that the text on the screen has changed
15532 (either due to modification of the text, or any other reason). */
15533 if (!current_matrix_up_to_date_p
15534 && !NILP (Vwindow_text_change_functions))
15535 {
15536 safe_run_hooks (Qwindow_text_change_functions);
15537 goto restart;
15538 }
15539
15540 beg_unchanged = BEG_UNCHANGED;
15541 end_unchanged = END_UNCHANGED;
15542
15543 SET_TEXT_POS (opoint, PT, PT_BYTE);
15544
15545 specbind (Qinhibit_point_motion_hooks, Qt);
15546
15547 buffer_unchanged_p
15548 = (!NILP (w->window_end_valid)
15549 && !current_buffer->clip_changed
15550 && !window_outdated (w));
15551
15552 /* When windows_or_buffers_changed is non-zero, we can't rely on
15553 the window end being valid, so set it to nil there. */
15554 if (windows_or_buffers_changed)
15555 {
15556 /* If window starts on a continuation line, maybe adjust the
15557 window start in case the window's width changed. */
15558 if (XMARKER (w->start)->buffer == current_buffer)
15559 compute_window_start_on_continuation_line (w);
15560
15561 wset_window_end_valid (w, Qnil);
15562 }
15563
15564 /* Some sanity checks. */
15565 CHECK_WINDOW_END (w);
15566 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15567 emacs_abort ();
15568 if (BYTEPOS (opoint) < CHARPOS (opoint))
15569 emacs_abort ();
15570
15571 if (mode_line_update_needed (w))
15572 update_mode_line = 1;
15573
15574 /* Point refers normally to the selected window. For any other
15575 window, set up appropriate value. */
15576 if (!EQ (window, selected_window))
15577 {
15578 ptrdiff_t new_pt = marker_position (w->pointm);
15579 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15580 if (new_pt < BEGV)
15581 {
15582 new_pt = BEGV;
15583 new_pt_byte = BEGV_BYTE;
15584 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15585 }
15586 else if (new_pt > (ZV - 1))
15587 {
15588 new_pt = ZV;
15589 new_pt_byte = ZV_BYTE;
15590 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15591 }
15592
15593 /* We don't use SET_PT so that the point-motion hooks don't run. */
15594 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15595 }
15596
15597 /* If any of the character widths specified in the display table
15598 have changed, invalidate the width run cache. It's true that
15599 this may be a bit late to catch such changes, but the rest of
15600 redisplay goes (non-fatally) haywire when the display table is
15601 changed, so why should we worry about doing any better? */
15602 if (current_buffer->width_run_cache)
15603 {
15604 struct Lisp_Char_Table *disptab = buffer_display_table ();
15605
15606 if (! disptab_matches_widthtab
15607 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15608 {
15609 invalidate_region_cache (current_buffer,
15610 current_buffer->width_run_cache,
15611 BEG, Z);
15612 recompute_width_table (current_buffer, disptab);
15613 }
15614 }
15615
15616 /* If window-start is screwed up, choose a new one. */
15617 if (XMARKER (w->start)->buffer != current_buffer)
15618 goto recenter;
15619
15620 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15621
15622 /* If someone specified a new starting point but did not insist,
15623 check whether it can be used. */
15624 if (w->optional_new_start
15625 && CHARPOS (startp) >= BEGV
15626 && CHARPOS (startp) <= ZV)
15627 {
15628 w->optional_new_start = 0;
15629 start_display (&it, w, startp);
15630 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15631 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15632 if (IT_CHARPOS (it) == PT)
15633 w->force_start = 1;
15634 /* IT may overshoot PT if text at PT is invisible. */
15635 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15636 w->force_start = 1;
15637 }
15638
15639 force_start:
15640
15641 /* Handle case where place to start displaying has been specified,
15642 unless the specified location is outside the accessible range. */
15643 if (w->force_start || w->frozen_window_start_p)
15644 {
15645 /* We set this later on if we have to adjust point. */
15646 int new_vpos = -1;
15647
15648 w->force_start = 0;
15649 w->vscroll = 0;
15650 wset_window_end_valid (w, Qnil);
15651
15652 /* Forget any recorded base line for line number display. */
15653 if (!buffer_unchanged_p)
15654 wset_base_line_number (w, Qnil);
15655
15656 /* Redisplay the mode line. Select the buffer properly for that.
15657 Also, run the hook window-scroll-functions
15658 because we have scrolled. */
15659 /* Note, we do this after clearing force_start because
15660 if there's an error, it is better to forget about force_start
15661 than to get into an infinite loop calling the hook functions
15662 and having them get more errors. */
15663 if (!update_mode_line
15664 || ! NILP (Vwindow_scroll_functions))
15665 {
15666 update_mode_line = 1;
15667 w->update_mode_line = 1;
15668 startp = run_window_scroll_functions (window, startp);
15669 }
15670
15671 w->last_modified = 0;
15672 w->last_overlay_modified = 0;
15673 if (CHARPOS (startp) < BEGV)
15674 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15675 else if (CHARPOS (startp) > ZV)
15676 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15677
15678 /* Redisplay, then check if cursor has been set during the
15679 redisplay. Give up if new fonts were loaded. */
15680 /* We used to issue a CHECK_MARGINS argument to try_window here,
15681 but this causes scrolling to fail when point begins inside
15682 the scroll margin (bug#148) -- cyd */
15683 if (!try_window (window, startp, 0))
15684 {
15685 w->force_start = 1;
15686 clear_glyph_matrix (w->desired_matrix);
15687 goto need_larger_matrices;
15688 }
15689
15690 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15691 {
15692 /* If point does not appear, try to move point so it does
15693 appear. The desired matrix has been built above, so we
15694 can use it here. */
15695 new_vpos = window_box_height (w) / 2;
15696 }
15697
15698 if (!cursor_row_fully_visible_p (w, 0, 0))
15699 {
15700 /* Point does appear, but on a line partly visible at end of window.
15701 Move it back to a fully-visible line. */
15702 new_vpos = window_box_height (w);
15703 }
15704 else if (w->cursor.vpos >=0)
15705 {
15706 /* Some people insist on not letting point enter the scroll
15707 margin, even though this part handles windows that didn't
15708 scroll at all. */
15709 struct frame *f = XFRAME (w->frame);
15710 int margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15711 int pixel_margin = margin * FRAME_LINE_HEIGHT (f);
15712 bool header_line = WINDOW_WANTS_HEADER_LINE_P (w);
15713
15714 /* Note: We add an extra FRAME_LINE_HEIGHT, because the loop
15715 below, which finds the row to move point to, advances by
15716 the Y coordinate of the _next_ row, see the definition of
15717 MATRIX_ROW_BOTTOM_Y. */
15718 if (w->cursor.vpos < margin + header_line)
15719 new_vpos
15720 = pixel_margin + (header_line
15721 ? CURRENT_HEADER_LINE_HEIGHT (w)
15722 : 0) + FRAME_LINE_HEIGHT (f);
15723 else
15724 {
15725 int window_height = window_box_height (w);
15726
15727 if (header_line)
15728 window_height += CURRENT_HEADER_LINE_HEIGHT (w);
15729 if (w->cursor.y >= window_height - pixel_margin)
15730 new_vpos = window_height - pixel_margin;
15731 }
15732 }
15733
15734 /* If we need to move point for either of the above reasons,
15735 now actually do it. */
15736 if (new_vpos >= 0)
15737 {
15738 struct glyph_row *row;
15739
15740 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15741 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15742 ++row;
15743
15744 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15745 MATRIX_ROW_START_BYTEPOS (row));
15746
15747 if (w != XWINDOW (selected_window))
15748 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15749 else if (current_buffer == old)
15750 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15751
15752 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15753
15754 /* If we are highlighting the region, then we just changed
15755 the region, so redisplay to show it. */
15756 if (0 <= markpos_of_region ())
15757 {
15758 clear_glyph_matrix (w->desired_matrix);
15759 if (!try_window (window, startp, 0))
15760 goto need_larger_matrices;
15761 }
15762 }
15763
15764 #ifdef GLYPH_DEBUG
15765 debug_method_add (w, "forced window start");
15766 #endif
15767 goto done;
15768 }
15769
15770 /* Handle case where text has not changed, only point, and it has
15771 not moved off the frame, and we are not retrying after hscroll.
15772 (current_matrix_up_to_date_p is nonzero when retrying.) */
15773 if (current_matrix_up_to_date_p
15774 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15775 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15776 {
15777 switch (rc)
15778 {
15779 case CURSOR_MOVEMENT_SUCCESS:
15780 used_current_matrix_p = 1;
15781 goto done;
15782
15783 case CURSOR_MOVEMENT_MUST_SCROLL:
15784 goto try_to_scroll;
15785
15786 default:
15787 emacs_abort ();
15788 }
15789 }
15790 /* If current starting point was originally the beginning of a line
15791 but no longer is, find a new starting point. */
15792 else if (w->start_at_line_beg
15793 && !(CHARPOS (startp) <= BEGV
15794 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15795 {
15796 #ifdef GLYPH_DEBUG
15797 debug_method_add (w, "recenter 1");
15798 #endif
15799 goto recenter;
15800 }
15801
15802 /* Try scrolling with try_window_id. Value is > 0 if update has
15803 been done, it is -1 if we know that the same window start will
15804 not work. It is 0 if unsuccessful for some other reason. */
15805 else if ((tem = try_window_id (w)) != 0)
15806 {
15807 #ifdef GLYPH_DEBUG
15808 debug_method_add (w, "try_window_id %d", tem);
15809 #endif
15810
15811 if (fonts_changed_p)
15812 goto need_larger_matrices;
15813 if (tem > 0)
15814 goto done;
15815
15816 /* Otherwise try_window_id has returned -1 which means that we
15817 don't want the alternative below this comment to execute. */
15818 }
15819 else if (CHARPOS (startp) >= BEGV
15820 && CHARPOS (startp) <= ZV
15821 && PT >= CHARPOS (startp)
15822 && (CHARPOS (startp) < ZV
15823 /* Avoid starting at end of buffer. */
15824 || CHARPOS (startp) == BEGV
15825 || !window_outdated (w)))
15826 {
15827 int d1, d2, d3, d4, d5, d6;
15828
15829 /* If first window line is a continuation line, and window start
15830 is inside the modified region, but the first change is before
15831 current window start, we must select a new window start.
15832
15833 However, if this is the result of a down-mouse event (e.g. by
15834 extending the mouse-drag-overlay), we don't want to select a
15835 new window start, since that would change the position under
15836 the mouse, resulting in an unwanted mouse-movement rather
15837 than a simple mouse-click. */
15838 if (!w->start_at_line_beg
15839 && NILP (do_mouse_tracking)
15840 && CHARPOS (startp) > BEGV
15841 && CHARPOS (startp) > BEG + beg_unchanged
15842 && CHARPOS (startp) <= Z - end_unchanged
15843 /* Even if w->start_at_line_beg is nil, a new window may
15844 start at a line_beg, since that's how set_buffer_window
15845 sets it. So, we need to check the return value of
15846 compute_window_start_on_continuation_line. (See also
15847 bug#197). */
15848 && XMARKER (w->start)->buffer == current_buffer
15849 && compute_window_start_on_continuation_line (w)
15850 /* It doesn't make sense to force the window start like we
15851 do at label force_start if it is already known that point
15852 will not be visible in the resulting window, because
15853 doing so will move point from its correct position
15854 instead of scrolling the window to bring point into view.
15855 See bug#9324. */
15856 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15857 {
15858 w->force_start = 1;
15859 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15860 goto force_start;
15861 }
15862
15863 #ifdef GLYPH_DEBUG
15864 debug_method_add (w, "same window start");
15865 #endif
15866
15867 /* Try to redisplay starting at same place as before.
15868 If point has not moved off frame, accept the results. */
15869 if (!current_matrix_up_to_date_p
15870 /* Don't use try_window_reusing_current_matrix in this case
15871 because a window scroll function can have changed the
15872 buffer. */
15873 || !NILP (Vwindow_scroll_functions)
15874 || MINI_WINDOW_P (w)
15875 || !(used_current_matrix_p
15876 = try_window_reusing_current_matrix (w)))
15877 {
15878 IF_DEBUG (debug_method_add (w, "1"));
15879 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15880 /* -1 means we need to scroll.
15881 0 means we need new matrices, but fonts_changed_p
15882 is set in that case, so we will detect it below. */
15883 goto try_to_scroll;
15884 }
15885
15886 if (fonts_changed_p)
15887 goto need_larger_matrices;
15888
15889 if (w->cursor.vpos >= 0)
15890 {
15891 if (!just_this_one_p
15892 || current_buffer->clip_changed
15893 || BEG_UNCHANGED < CHARPOS (startp))
15894 /* Forget any recorded base line for line number display. */
15895 wset_base_line_number (w, Qnil);
15896
15897 if (!cursor_row_fully_visible_p (w, 1, 0))
15898 {
15899 clear_glyph_matrix (w->desired_matrix);
15900 last_line_misfit = 1;
15901 }
15902 /* Drop through and scroll. */
15903 else
15904 goto done;
15905 }
15906 else
15907 clear_glyph_matrix (w->desired_matrix);
15908 }
15909
15910 try_to_scroll:
15911
15912 w->last_modified = 0;
15913 w->last_overlay_modified = 0;
15914
15915 /* Redisplay the mode line. Select the buffer properly for that. */
15916 if (!update_mode_line)
15917 {
15918 update_mode_line = 1;
15919 w->update_mode_line = 1;
15920 }
15921
15922 /* Try to scroll by specified few lines. */
15923 if ((scroll_conservatively
15924 || emacs_scroll_step
15925 || temp_scroll_step
15926 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15927 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15928 && CHARPOS (startp) >= BEGV
15929 && CHARPOS (startp) <= ZV)
15930 {
15931 /* The function returns -1 if new fonts were loaded, 1 if
15932 successful, 0 if not successful. */
15933 int ss = try_scrolling (window, just_this_one_p,
15934 scroll_conservatively,
15935 emacs_scroll_step,
15936 temp_scroll_step, last_line_misfit);
15937 switch (ss)
15938 {
15939 case SCROLLING_SUCCESS:
15940 goto done;
15941
15942 case SCROLLING_NEED_LARGER_MATRICES:
15943 goto need_larger_matrices;
15944
15945 case SCROLLING_FAILED:
15946 break;
15947
15948 default:
15949 emacs_abort ();
15950 }
15951 }
15952
15953 /* Finally, just choose a place to start which positions point
15954 according to user preferences. */
15955
15956 recenter:
15957
15958 #ifdef GLYPH_DEBUG
15959 debug_method_add (w, "recenter");
15960 #endif
15961
15962 /* w->vscroll = 0; */
15963
15964 /* Forget any previously recorded base line for line number display. */
15965 if (!buffer_unchanged_p)
15966 wset_base_line_number (w, Qnil);
15967
15968 /* Determine the window start relative to point. */
15969 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
15970 it.current_y = it.last_visible_y;
15971 if (centering_position < 0)
15972 {
15973 int margin =
15974 scroll_margin > 0
15975 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
15976 : 0;
15977 ptrdiff_t margin_pos = CHARPOS (startp);
15978 Lisp_Object aggressive;
15979 int scrolling_up;
15980
15981 /* If there is a scroll margin at the top of the window, find
15982 its character position. */
15983 if (margin
15984 /* Cannot call start_display if startp is not in the
15985 accessible region of the buffer. This can happen when we
15986 have just switched to a different buffer and/or changed
15987 its restriction. In that case, startp is initialized to
15988 the character position 1 (BEGV) because we did not yet
15989 have chance to display the buffer even once. */
15990 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
15991 {
15992 struct it it1;
15993 void *it1data = NULL;
15994
15995 SAVE_IT (it1, it, it1data);
15996 start_display (&it1, w, startp);
15997 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
15998 margin_pos = IT_CHARPOS (it1);
15999 RESTORE_IT (&it, &it, it1data);
16000 }
16001 scrolling_up = PT > margin_pos;
16002 aggressive =
16003 scrolling_up
16004 ? BVAR (current_buffer, scroll_up_aggressively)
16005 : BVAR (current_buffer, scroll_down_aggressively);
16006
16007 if (!MINI_WINDOW_P (w)
16008 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16009 {
16010 int pt_offset = 0;
16011
16012 /* Setting scroll-conservatively overrides
16013 scroll-*-aggressively. */
16014 if (!scroll_conservatively && NUMBERP (aggressive))
16015 {
16016 double float_amount = XFLOATINT (aggressive);
16017
16018 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16019 if (pt_offset == 0 && float_amount > 0)
16020 pt_offset = 1;
16021 if (pt_offset && margin > 0)
16022 margin -= 1;
16023 }
16024 /* Compute how much to move the window start backward from
16025 point so that point will be displayed where the user
16026 wants it. */
16027 if (scrolling_up)
16028 {
16029 centering_position = it.last_visible_y;
16030 if (pt_offset)
16031 centering_position -= pt_offset;
16032 centering_position -=
16033 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
16034 + WINDOW_HEADER_LINE_HEIGHT (w);
16035 /* Don't let point enter the scroll margin near top of
16036 the window. */
16037 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
16038 centering_position = margin * FRAME_LINE_HEIGHT (f);
16039 }
16040 else
16041 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
16042 }
16043 else
16044 /* Set the window start half the height of the window backward
16045 from point. */
16046 centering_position = window_box_height (w) / 2;
16047 }
16048 move_it_vertically_backward (&it, centering_position);
16049
16050 eassert (IT_CHARPOS (it) >= BEGV);
16051
16052 /* The function move_it_vertically_backward may move over more
16053 than the specified y-distance. If it->w is small, e.g. a
16054 mini-buffer window, we may end up in front of the window's
16055 display area. Start displaying at the start of the line
16056 containing PT in this case. */
16057 if (it.current_y <= 0)
16058 {
16059 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16060 move_it_vertically_backward (&it, 0);
16061 it.current_y = 0;
16062 }
16063
16064 it.current_x = it.hpos = 0;
16065
16066 /* Set the window start position here explicitly, to avoid an
16067 infinite loop in case the functions in window-scroll-functions
16068 get errors. */
16069 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16070
16071 /* Run scroll hooks. */
16072 startp = run_window_scroll_functions (window, it.current.pos);
16073
16074 /* Redisplay the window. */
16075 if (!current_matrix_up_to_date_p
16076 || windows_or_buffers_changed
16077 || cursor_type_changed
16078 /* Don't use try_window_reusing_current_matrix in this case
16079 because it can have changed the buffer. */
16080 || !NILP (Vwindow_scroll_functions)
16081 || !just_this_one_p
16082 || MINI_WINDOW_P (w)
16083 || !(used_current_matrix_p
16084 = try_window_reusing_current_matrix (w)))
16085 try_window (window, startp, 0);
16086
16087 /* If new fonts have been loaded (due to fontsets), give up. We
16088 have to start a new redisplay since we need to re-adjust glyph
16089 matrices. */
16090 if (fonts_changed_p)
16091 goto need_larger_matrices;
16092
16093 /* If cursor did not appear assume that the middle of the window is
16094 in the first line of the window. Do it again with the next line.
16095 (Imagine a window of height 100, displaying two lines of height
16096 60. Moving back 50 from it->last_visible_y will end in the first
16097 line.) */
16098 if (w->cursor.vpos < 0)
16099 {
16100 if (!NILP (w->window_end_valid)
16101 && PT >= Z - XFASTINT (w->window_end_pos))
16102 {
16103 clear_glyph_matrix (w->desired_matrix);
16104 move_it_by_lines (&it, 1);
16105 try_window (window, it.current.pos, 0);
16106 }
16107 else if (PT < IT_CHARPOS (it))
16108 {
16109 clear_glyph_matrix (w->desired_matrix);
16110 move_it_by_lines (&it, -1);
16111 try_window (window, it.current.pos, 0);
16112 }
16113 else
16114 {
16115 /* Not much we can do about it. */
16116 }
16117 }
16118
16119 /* Consider the following case: Window starts at BEGV, there is
16120 invisible, intangible text at BEGV, so that display starts at
16121 some point START > BEGV. It can happen that we are called with
16122 PT somewhere between BEGV and START. Try to handle that case. */
16123 if (w->cursor.vpos < 0)
16124 {
16125 struct glyph_row *row = w->current_matrix->rows;
16126 if (row->mode_line_p)
16127 ++row;
16128 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16129 }
16130
16131 if (!cursor_row_fully_visible_p (w, 0, 0))
16132 {
16133 /* If vscroll is enabled, disable it and try again. */
16134 if (w->vscroll)
16135 {
16136 w->vscroll = 0;
16137 clear_glyph_matrix (w->desired_matrix);
16138 goto recenter;
16139 }
16140
16141 /* Users who set scroll-conservatively to a large number want
16142 point just above/below the scroll margin. If we ended up
16143 with point's row partially visible, move the window start to
16144 make that row fully visible and out of the margin. */
16145 if (scroll_conservatively > SCROLL_LIMIT)
16146 {
16147 int margin =
16148 scroll_margin > 0
16149 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16150 : 0;
16151 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16152
16153 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16154 clear_glyph_matrix (w->desired_matrix);
16155 if (1 == try_window (window, it.current.pos,
16156 TRY_WINDOW_CHECK_MARGINS))
16157 goto done;
16158 }
16159
16160 /* If centering point failed to make the whole line visible,
16161 put point at the top instead. That has to make the whole line
16162 visible, if it can be done. */
16163 if (centering_position == 0)
16164 goto done;
16165
16166 clear_glyph_matrix (w->desired_matrix);
16167 centering_position = 0;
16168 goto recenter;
16169 }
16170
16171 done:
16172
16173 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16174 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16175 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16176
16177 /* Display the mode line, if we must. */
16178 if ((update_mode_line
16179 /* If window not full width, must redo its mode line
16180 if (a) the window to its side is being redone and
16181 (b) we do a frame-based redisplay. This is a consequence
16182 of how inverted lines are drawn in frame-based redisplay. */
16183 || (!just_this_one_p
16184 && !FRAME_WINDOW_P (f)
16185 && !WINDOW_FULL_WIDTH_P (w))
16186 /* Line number to display. */
16187 || INTEGERP (w->base_line_pos)
16188 /* Column number is displayed and different from the one displayed. */
16189 || (!NILP (w->column_number_displayed)
16190 && (XFASTINT (w->column_number_displayed) != current_column ())))
16191 /* This means that the window has a mode line. */
16192 && (WINDOW_WANTS_MODELINE_P (w)
16193 || WINDOW_WANTS_HEADER_LINE_P (w)))
16194 {
16195 display_mode_lines (w);
16196
16197 /* If mode line height has changed, arrange for a thorough
16198 immediate redisplay using the correct mode line height. */
16199 if (WINDOW_WANTS_MODELINE_P (w)
16200 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16201 {
16202 fonts_changed_p = 1;
16203 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16204 = DESIRED_MODE_LINE_HEIGHT (w);
16205 }
16206
16207 /* If header line height has changed, arrange for a thorough
16208 immediate redisplay using the correct header line height. */
16209 if (WINDOW_WANTS_HEADER_LINE_P (w)
16210 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16211 {
16212 fonts_changed_p = 1;
16213 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16214 = DESIRED_HEADER_LINE_HEIGHT (w);
16215 }
16216
16217 if (fonts_changed_p)
16218 goto need_larger_matrices;
16219 }
16220
16221 if (!line_number_displayed
16222 && !BUFFERP (w->base_line_pos))
16223 {
16224 wset_base_line_pos (w, Qnil);
16225 wset_base_line_number (w, Qnil);
16226 }
16227
16228 finish_menu_bars:
16229
16230 /* When we reach a frame's selected window, redo the frame's menu bar. */
16231 if (update_mode_line
16232 && EQ (FRAME_SELECTED_WINDOW (f), window))
16233 {
16234 int redisplay_menu_p = 0;
16235
16236 if (FRAME_WINDOW_P (f))
16237 {
16238 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16239 || defined (HAVE_NS) || defined (USE_GTK)
16240 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16241 #else
16242 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16243 #endif
16244 }
16245 else
16246 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16247
16248 if (redisplay_menu_p)
16249 display_menu_bar (w);
16250
16251 #ifdef HAVE_WINDOW_SYSTEM
16252 if (FRAME_WINDOW_P (f))
16253 {
16254 #if defined (USE_GTK) || defined (HAVE_NS)
16255 if (FRAME_EXTERNAL_TOOL_BAR (f))
16256 redisplay_tool_bar (f);
16257 #else
16258 if (WINDOWP (f->tool_bar_window)
16259 && (FRAME_TOOL_BAR_LINES (f) > 0
16260 || !NILP (Vauto_resize_tool_bars))
16261 && redisplay_tool_bar (f))
16262 ignore_mouse_drag_p = 1;
16263 #endif
16264 }
16265 #endif
16266 }
16267
16268 #ifdef HAVE_WINDOW_SYSTEM
16269 if (FRAME_WINDOW_P (f)
16270 && update_window_fringes (w, (just_this_one_p
16271 || (!used_current_matrix_p && !overlay_arrow_seen)
16272 || w->pseudo_window_p)))
16273 {
16274 update_begin (f);
16275 block_input ();
16276 if (draw_window_fringes (w, 1))
16277 x_draw_vertical_border (w);
16278 unblock_input ();
16279 update_end (f);
16280 }
16281 #endif /* HAVE_WINDOW_SYSTEM */
16282
16283 /* We go to this label, with fonts_changed_p set,
16284 if it is necessary to try again using larger glyph matrices.
16285 We have to redeem the scroll bar even in this case,
16286 because the loop in redisplay_internal expects that. */
16287 need_larger_matrices:
16288 ;
16289 finish_scroll_bars:
16290
16291 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16292 {
16293 /* Set the thumb's position and size. */
16294 set_vertical_scroll_bar (w);
16295
16296 /* Note that we actually used the scroll bar attached to this
16297 window, so it shouldn't be deleted at the end of redisplay. */
16298 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16299 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16300 }
16301
16302 /* Restore current_buffer and value of point in it. The window
16303 update may have changed the buffer, so first make sure `opoint'
16304 is still valid (Bug#6177). */
16305 if (CHARPOS (opoint) < BEGV)
16306 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16307 else if (CHARPOS (opoint) > ZV)
16308 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16309 else
16310 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16311
16312 set_buffer_internal_1 (old);
16313 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16314 shorter. This can be caused by log truncation in *Messages*. */
16315 if (CHARPOS (lpoint) <= ZV)
16316 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16317
16318 unbind_to (count, Qnil);
16319 }
16320
16321
16322 /* Build the complete desired matrix of WINDOW with a window start
16323 buffer position POS.
16324
16325 Value is 1 if successful. It is zero if fonts were loaded during
16326 redisplay which makes re-adjusting glyph matrices necessary, and -1
16327 if point would appear in the scroll margins.
16328 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16329 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16330 set in FLAGS.) */
16331
16332 int
16333 try_window (Lisp_Object window, struct text_pos pos, int flags)
16334 {
16335 struct window *w = XWINDOW (window);
16336 struct it it;
16337 struct glyph_row *last_text_row = NULL;
16338 struct frame *f = XFRAME (w->frame);
16339
16340 /* Make POS the new window start. */
16341 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16342
16343 /* Mark cursor position as unknown. No overlay arrow seen. */
16344 w->cursor.vpos = -1;
16345 overlay_arrow_seen = 0;
16346
16347 /* Initialize iterator and info to start at POS. */
16348 start_display (&it, w, pos);
16349
16350 /* Display all lines of W. */
16351 while (it.current_y < it.last_visible_y)
16352 {
16353 if (display_line (&it))
16354 last_text_row = it.glyph_row - 1;
16355 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16356 return 0;
16357 }
16358
16359 /* Don't let the cursor end in the scroll margins. */
16360 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16361 && !MINI_WINDOW_P (w))
16362 {
16363 int this_scroll_margin;
16364
16365 if (scroll_margin > 0)
16366 {
16367 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16368 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16369 }
16370 else
16371 this_scroll_margin = 0;
16372
16373 if ((w->cursor.y >= 0 /* not vscrolled */
16374 && w->cursor.y < this_scroll_margin
16375 && CHARPOS (pos) > BEGV
16376 && IT_CHARPOS (it) < ZV)
16377 /* rms: considering make_cursor_line_fully_visible_p here
16378 seems to give wrong results. We don't want to recenter
16379 when the last line is partly visible, we want to allow
16380 that case to be handled in the usual way. */
16381 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16382 {
16383 w->cursor.vpos = -1;
16384 clear_glyph_matrix (w->desired_matrix);
16385 return -1;
16386 }
16387 }
16388
16389 /* If bottom moved off end of frame, change mode line percentage. */
16390 if (XFASTINT (w->window_end_pos) <= 0
16391 && Z != IT_CHARPOS (it))
16392 w->update_mode_line = 1;
16393
16394 /* Set window_end_pos to the offset of the last character displayed
16395 on the window from the end of current_buffer. Set
16396 window_end_vpos to its row number. */
16397 if (last_text_row)
16398 {
16399 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16400 w->window_end_bytepos
16401 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16402 wset_window_end_pos
16403 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16404 wset_window_end_vpos
16405 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16406 eassert
16407 (MATRIX_ROW (w->desired_matrix,
16408 XFASTINT (w->window_end_vpos))->displays_text_p);
16409 }
16410 else
16411 {
16412 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16413 wset_window_end_pos (w, make_number (Z - ZV));
16414 wset_window_end_vpos (w, make_number (0));
16415 }
16416
16417 /* But that is not valid info until redisplay finishes. */
16418 wset_window_end_valid (w, Qnil);
16419 return 1;
16420 }
16421
16422
16423 \f
16424 /************************************************************************
16425 Window redisplay reusing current matrix when buffer has not changed
16426 ************************************************************************/
16427
16428 /* Try redisplay of window W showing an unchanged buffer with a
16429 different window start than the last time it was displayed by
16430 reusing its current matrix. Value is non-zero if successful.
16431 W->start is the new window start. */
16432
16433 static int
16434 try_window_reusing_current_matrix (struct window *w)
16435 {
16436 struct frame *f = XFRAME (w->frame);
16437 struct glyph_row *bottom_row;
16438 struct it it;
16439 struct run run;
16440 struct text_pos start, new_start;
16441 int nrows_scrolled, i;
16442 struct glyph_row *last_text_row;
16443 struct glyph_row *last_reused_text_row;
16444 struct glyph_row *start_row;
16445 int start_vpos, min_y, max_y;
16446
16447 #ifdef GLYPH_DEBUG
16448 if (inhibit_try_window_reusing)
16449 return 0;
16450 #endif
16451
16452 if (/* This function doesn't handle terminal frames. */
16453 !FRAME_WINDOW_P (f)
16454 /* Don't try to reuse the display if windows have been split
16455 or such. */
16456 || windows_or_buffers_changed
16457 || cursor_type_changed)
16458 return 0;
16459
16460 /* Can't do this if region may have changed. */
16461 if (0 <= markpos_of_region ()
16462 || !NILP (w->region_showing)
16463 || !NILP (Vshow_trailing_whitespace))
16464 return 0;
16465
16466 /* If top-line visibility has changed, give up. */
16467 if (WINDOW_WANTS_HEADER_LINE_P (w)
16468 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16469 return 0;
16470
16471 /* Give up if old or new display is scrolled vertically. We could
16472 make this function handle this, but right now it doesn't. */
16473 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16474 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16475 return 0;
16476
16477 /* The variable new_start now holds the new window start. The old
16478 start `start' can be determined from the current matrix. */
16479 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16480 start = start_row->minpos;
16481 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16482
16483 /* Clear the desired matrix for the display below. */
16484 clear_glyph_matrix (w->desired_matrix);
16485
16486 if (CHARPOS (new_start) <= CHARPOS (start))
16487 {
16488 /* Don't use this method if the display starts with an ellipsis
16489 displayed for invisible text. It's not easy to handle that case
16490 below, and it's certainly not worth the effort since this is
16491 not a frequent case. */
16492 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16493 return 0;
16494
16495 IF_DEBUG (debug_method_add (w, "twu1"));
16496
16497 /* Display up to a row that can be reused. The variable
16498 last_text_row is set to the last row displayed that displays
16499 text. Note that it.vpos == 0 if or if not there is a
16500 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16501 start_display (&it, w, new_start);
16502 w->cursor.vpos = -1;
16503 last_text_row = last_reused_text_row = NULL;
16504
16505 while (it.current_y < it.last_visible_y
16506 && !fonts_changed_p)
16507 {
16508 /* If we have reached into the characters in the START row,
16509 that means the line boundaries have changed. So we
16510 can't start copying with the row START. Maybe it will
16511 work to start copying with the following row. */
16512 while (IT_CHARPOS (it) > CHARPOS (start))
16513 {
16514 /* Advance to the next row as the "start". */
16515 start_row++;
16516 start = start_row->minpos;
16517 /* If there are no more rows to try, or just one, give up. */
16518 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16519 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16520 || CHARPOS (start) == ZV)
16521 {
16522 clear_glyph_matrix (w->desired_matrix);
16523 return 0;
16524 }
16525
16526 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16527 }
16528 /* If we have reached alignment, we can copy the rest of the
16529 rows. */
16530 if (IT_CHARPOS (it) == CHARPOS (start)
16531 /* Don't accept "alignment" inside a display vector,
16532 since start_row could have started in the middle of
16533 that same display vector (thus their character
16534 positions match), and we have no way of telling if
16535 that is the case. */
16536 && it.current.dpvec_index < 0)
16537 break;
16538
16539 if (display_line (&it))
16540 last_text_row = it.glyph_row - 1;
16541
16542 }
16543
16544 /* A value of current_y < last_visible_y means that we stopped
16545 at the previous window start, which in turn means that we
16546 have at least one reusable row. */
16547 if (it.current_y < it.last_visible_y)
16548 {
16549 struct glyph_row *row;
16550
16551 /* IT.vpos always starts from 0; it counts text lines. */
16552 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16553
16554 /* Find PT if not already found in the lines displayed. */
16555 if (w->cursor.vpos < 0)
16556 {
16557 int dy = it.current_y - start_row->y;
16558
16559 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16560 row = row_containing_pos (w, PT, row, NULL, dy);
16561 if (row)
16562 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16563 dy, nrows_scrolled);
16564 else
16565 {
16566 clear_glyph_matrix (w->desired_matrix);
16567 return 0;
16568 }
16569 }
16570
16571 /* Scroll the display. Do it before the current matrix is
16572 changed. The problem here is that update has not yet
16573 run, i.e. part of the current matrix is not up to date.
16574 scroll_run_hook will clear the cursor, and use the
16575 current matrix to get the height of the row the cursor is
16576 in. */
16577 run.current_y = start_row->y;
16578 run.desired_y = it.current_y;
16579 run.height = it.last_visible_y - it.current_y;
16580
16581 if (run.height > 0 && run.current_y != run.desired_y)
16582 {
16583 update_begin (f);
16584 FRAME_RIF (f)->update_window_begin_hook (w);
16585 FRAME_RIF (f)->clear_window_mouse_face (w);
16586 FRAME_RIF (f)->scroll_run_hook (w, &run);
16587 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16588 update_end (f);
16589 }
16590
16591 /* Shift current matrix down by nrows_scrolled lines. */
16592 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16593 rotate_matrix (w->current_matrix,
16594 start_vpos,
16595 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16596 nrows_scrolled);
16597
16598 /* Disable lines that must be updated. */
16599 for (i = 0; i < nrows_scrolled; ++i)
16600 (start_row + i)->enabled_p = 0;
16601
16602 /* Re-compute Y positions. */
16603 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16604 max_y = it.last_visible_y;
16605 for (row = start_row + nrows_scrolled;
16606 row < bottom_row;
16607 ++row)
16608 {
16609 row->y = it.current_y;
16610 row->visible_height = row->height;
16611
16612 if (row->y < min_y)
16613 row->visible_height -= min_y - row->y;
16614 if (row->y + row->height > max_y)
16615 row->visible_height -= row->y + row->height - max_y;
16616 if (row->fringe_bitmap_periodic_p)
16617 row->redraw_fringe_bitmaps_p = 1;
16618
16619 it.current_y += row->height;
16620
16621 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16622 last_reused_text_row = row;
16623 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16624 break;
16625 }
16626
16627 /* Disable lines in the current matrix which are now
16628 below the window. */
16629 for (++row; row < bottom_row; ++row)
16630 row->enabled_p = row->mode_line_p = 0;
16631 }
16632
16633 /* Update window_end_pos etc.; last_reused_text_row is the last
16634 reused row from the current matrix containing text, if any.
16635 The value of last_text_row is the last displayed line
16636 containing text. */
16637 if (last_reused_text_row)
16638 {
16639 w->window_end_bytepos
16640 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16641 wset_window_end_pos
16642 (w, make_number (Z
16643 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16644 wset_window_end_vpos
16645 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16646 w->current_matrix)));
16647 }
16648 else if (last_text_row)
16649 {
16650 w->window_end_bytepos
16651 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16652 wset_window_end_pos
16653 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16654 wset_window_end_vpos
16655 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16656 w->desired_matrix)));
16657 }
16658 else
16659 {
16660 /* This window must be completely empty. */
16661 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16662 wset_window_end_pos (w, make_number (Z - ZV));
16663 wset_window_end_vpos (w, make_number (0));
16664 }
16665 wset_window_end_valid (w, Qnil);
16666
16667 /* Update hint: don't try scrolling again in update_window. */
16668 w->desired_matrix->no_scrolling_p = 1;
16669
16670 #ifdef GLYPH_DEBUG
16671 debug_method_add (w, "try_window_reusing_current_matrix 1");
16672 #endif
16673 return 1;
16674 }
16675 else if (CHARPOS (new_start) > CHARPOS (start))
16676 {
16677 struct glyph_row *pt_row, *row;
16678 struct glyph_row *first_reusable_row;
16679 struct glyph_row *first_row_to_display;
16680 int dy;
16681 int yb = window_text_bottom_y (w);
16682
16683 /* Find the row starting at new_start, if there is one. Don't
16684 reuse a partially visible line at the end. */
16685 first_reusable_row = start_row;
16686 while (first_reusable_row->enabled_p
16687 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16688 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16689 < CHARPOS (new_start)))
16690 ++first_reusable_row;
16691
16692 /* Give up if there is no row to reuse. */
16693 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16694 || !first_reusable_row->enabled_p
16695 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16696 != CHARPOS (new_start)))
16697 return 0;
16698
16699 /* We can reuse fully visible rows beginning with
16700 first_reusable_row to the end of the window. Set
16701 first_row_to_display to the first row that cannot be reused.
16702 Set pt_row to the row containing point, if there is any. */
16703 pt_row = NULL;
16704 for (first_row_to_display = first_reusable_row;
16705 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16706 ++first_row_to_display)
16707 {
16708 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16709 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16710 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16711 && first_row_to_display->ends_at_zv_p
16712 && pt_row == NULL)))
16713 pt_row = first_row_to_display;
16714 }
16715
16716 /* Start displaying at the start of first_row_to_display. */
16717 eassert (first_row_to_display->y < yb);
16718 init_to_row_start (&it, w, first_row_to_display);
16719
16720 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16721 - start_vpos);
16722 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16723 - nrows_scrolled);
16724 it.current_y = (first_row_to_display->y - first_reusable_row->y
16725 + WINDOW_HEADER_LINE_HEIGHT (w));
16726
16727 /* Display lines beginning with first_row_to_display in the
16728 desired matrix. Set last_text_row to the last row displayed
16729 that displays text. */
16730 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16731 if (pt_row == NULL)
16732 w->cursor.vpos = -1;
16733 last_text_row = NULL;
16734 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16735 if (display_line (&it))
16736 last_text_row = it.glyph_row - 1;
16737
16738 /* If point is in a reused row, adjust y and vpos of the cursor
16739 position. */
16740 if (pt_row)
16741 {
16742 w->cursor.vpos -= nrows_scrolled;
16743 w->cursor.y -= first_reusable_row->y - start_row->y;
16744 }
16745
16746 /* Give up if point isn't in a row displayed or reused. (This
16747 also handles the case where w->cursor.vpos < nrows_scrolled
16748 after the calls to display_line, which can happen with scroll
16749 margins. See bug#1295.) */
16750 if (w->cursor.vpos < 0)
16751 {
16752 clear_glyph_matrix (w->desired_matrix);
16753 return 0;
16754 }
16755
16756 /* Scroll the display. */
16757 run.current_y = first_reusable_row->y;
16758 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16759 run.height = it.last_visible_y - run.current_y;
16760 dy = run.current_y - run.desired_y;
16761
16762 if (run.height)
16763 {
16764 update_begin (f);
16765 FRAME_RIF (f)->update_window_begin_hook (w);
16766 FRAME_RIF (f)->clear_window_mouse_face (w);
16767 FRAME_RIF (f)->scroll_run_hook (w, &run);
16768 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16769 update_end (f);
16770 }
16771
16772 /* Adjust Y positions of reused rows. */
16773 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16774 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16775 max_y = it.last_visible_y;
16776 for (row = first_reusable_row; row < first_row_to_display; ++row)
16777 {
16778 row->y -= dy;
16779 row->visible_height = row->height;
16780 if (row->y < min_y)
16781 row->visible_height -= min_y - row->y;
16782 if (row->y + row->height > max_y)
16783 row->visible_height -= row->y + row->height - max_y;
16784 if (row->fringe_bitmap_periodic_p)
16785 row->redraw_fringe_bitmaps_p = 1;
16786 }
16787
16788 /* Scroll the current matrix. */
16789 eassert (nrows_scrolled > 0);
16790 rotate_matrix (w->current_matrix,
16791 start_vpos,
16792 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16793 -nrows_scrolled);
16794
16795 /* Disable rows not reused. */
16796 for (row -= nrows_scrolled; row < bottom_row; ++row)
16797 row->enabled_p = 0;
16798
16799 /* Point may have moved to a different line, so we cannot assume that
16800 the previous cursor position is valid; locate the correct row. */
16801 if (pt_row)
16802 {
16803 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16804 row < bottom_row
16805 && PT >= MATRIX_ROW_END_CHARPOS (row)
16806 && !row->ends_at_zv_p;
16807 row++)
16808 {
16809 w->cursor.vpos++;
16810 w->cursor.y = row->y;
16811 }
16812 if (row < bottom_row)
16813 {
16814 /* Can't simply scan the row for point with
16815 bidi-reordered glyph rows. Let set_cursor_from_row
16816 figure out where to put the cursor, and if it fails,
16817 give up. */
16818 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16819 {
16820 if (!set_cursor_from_row (w, row, w->current_matrix,
16821 0, 0, 0, 0))
16822 {
16823 clear_glyph_matrix (w->desired_matrix);
16824 return 0;
16825 }
16826 }
16827 else
16828 {
16829 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16830 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16831
16832 for (; glyph < end
16833 && (!BUFFERP (glyph->object)
16834 || glyph->charpos < PT);
16835 glyph++)
16836 {
16837 w->cursor.hpos++;
16838 w->cursor.x += glyph->pixel_width;
16839 }
16840 }
16841 }
16842 }
16843
16844 /* Adjust window end. A null value of last_text_row means that
16845 the window end is in reused rows which in turn means that
16846 only its vpos can have changed. */
16847 if (last_text_row)
16848 {
16849 w->window_end_bytepos
16850 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16851 wset_window_end_pos
16852 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16853 wset_window_end_vpos
16854 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16855 w->desired_matrix)));
16856 }
16857 else
16858 {
16859 wset_window_end_vpos
16860 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16861 }
16862
16863 wset_window_end_valid (w, Qnil);
16864 w->desired_matrix->no_scrolling_p = 1;
16865
16866 #ifdef GLYPH_DEBUG
16867 debug_method_add (w, "try_window_reusing_current_matrix 2");
16868 #endif
16869 return 1;
16870 }
16871
16872 return 0;
16873 }
16874
16875
16876 \f
16877 /************************************************************************
16878 Window redisplay reusing current matrix when buffer has changed
16879 ************************************************************************/
16880
16881 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16882 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16883 ptrdiff_t *, ptrdiff_t *);
16884 static struct glyph_row *
16885 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16886 struct glyph_row *);
16887
16888
16889 /* Return the last row in MATRIX displaying text. If row START is
16890 non-null, start searching with that row. IT gives the dimensions
16891 of the display. Value is null if matrix is empty; otherwise it is
16892 a pointer to the row found. */
16893
16894 static struct glyph_row *
16895 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16896 struct glyph_row *start)
16897 {
16898 struct glyph_row *row, *row_found;
16899
16900 /* Set row_found to the last row in IT->w's current matrix
16901 displaying text. The loop looks funny but think of partially
16902 visible lines. */
16903 row_found = NULL;
16904 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16905 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16906 {
16907 eassert (row->enabled_p);
16908 row_found = row;
16909 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16910 break;
16911 ++row;
16912 }
16913
16914 return row_found;
16915 }
16916
16917
16918 /* Return the last row in the current matrix of W that is not affected
16919 by changes at the start of current_buffer that occurred since W's
16920 current matrix was built. Value is null if no such row exists.
16921
16922 BEG_UNCHANGED us the number of characters unchanged at the start of
16923 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16924 first changed character in current_buffer. Characters at positions <
16925 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16926 when the current matrix was built. */
16927
16928 static struct glyph_row *
16929 find_last_unchanged_at_beg_row (struct window *w)
16930 {
16931 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16932 struct glyph_row *row;
16933 struct glyph_row *row_found = NULL;
16934 int yb = window_text_bottom_y (w);
16935
16936 /* Find the last row displaying unchanged text. */
16937 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16938 MATRIX_ROW_DISPLAYS_TEXT_P (row)
16939 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
16940 ++row)
16941 {
16942 if (/* If row ends before first_changed_pos, it is unchanged,
16943 except in some case. */
16944 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
16945 /* When row ends in ZV and we write at ZV it is not
16946 unchanged. */
16947 && !row->ends_at_zv_p
16948 /* When first_changed_pos is the end of a continued line,
16949 row is not unchanged because it may be no longer
16950 continued. */
16951 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
16952 && (row->continued_p
16953 || row->exact_window_width_line_p))
16954 /* If ROW->end is beyond ZV, then ROW->end is outdated and
16955 needs to be recomputed, so don't consider this row as
16956 unchanged. This happens when the last line was
16957 bidi-reordered and was killed immediately before this
16958 redisplay cycle. In that case, ROW->end stores the
16959 buffer position of the first visual-order character of
16960 the killed text, which is now beyond ZV. */
16961 && CHARPOS (row->end.pos) <= ZV)
16962 row_found = row;
16963
16964 /* Stop if last visible row. */
16965 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
16966 break;
16967 }
16968
16969 return row_found;
16970 }
16971
16972
16973 /* Find the first glyph row in the current matrix of W that is not
16974 affected by changes at the end of current_buffer since the
16975 time W's current matrix was built.
16976
16977 Return in *DELTA the number of chars by which buffer positions in
16978 unchanged text at the end of current_buffer must be adjusted.
16979
16980 Return in *DELTA_BYTES the corresponding number of bytes.
16981
16982 Value is null if no such row exists, i.e. all rows are affected by
16983 changes. */
16984
16985 static struct glyph_row *
16986 find_first_unchanged_at_end_row (struct window *w,
16987 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
16988 {
16989 struct glyph_row *row;
16990 struct glyph_row *row_found = NULL;
16991
16992 *delta = *delta_bytes = 0;
16993
16994 /* Display must not have been paused, otherwise the current matrix
16995 is not up to date. */
16996 eassert (!NILP (w->window_end_valid));
16997
16998 /* A value of window_end_pos >= END_UNCHANGED means that the window
16999 end is in the range of changed text. If so, there is no
17000 unchanged row at the end of W's current matrix. */
17001 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
17002 return NULL;
17003
17004 /* Set row to the last row in W's current matrix displaying text. */
17005 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17006
17007 /* If matrix is entirely empty, no unchanged row exists. */
17008 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17009 {
17010 /* The value of row is the last glyph row in the matrix having a
17011 meaningful buffer position in it. The end position of row
17012 corresponds to window_end_pos. This allows us to translate
17013 buffer positions in the current matrix to current buffer
17014 positions for characters not in changed text. */
17015 ptrdiff_t Z_old =
17016 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17017 ptrdiff_t Z_BYTE_old =
17018 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17019 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17020 struct glyph_row *first_text_row
17021 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17022
17023 *delta = Z - Z_old;
17024 *delta_bytes = Z_BYTE - Z_BYTE_old;
17025
17026 /* Set last_unchanged_pos to the buffer position of the last
17027 character in the buffer that has not been changed. Z is the
17028 index + 1 of the last character in current_buffer, i.e. by
17029 subtracting END_UNCHANGED we get the index of the last
17030 unchanged character, and we have to add BEG to get its buffer
17031 position. */
17032 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17033 last_unchanged_pos_old = last_unchanged_pos - *delta;
17034
17035 /* Search backward from ROW for a row displaying a line that
17036 starts at a minimum position >= last_unchanged_pos_old. */
17037 for (; row > first_text_row; --row)
17038 {
17039 /* This used to abort, but it can happen.
17040 It is ok to just stop the search instead here. KFS. */
17041 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17042 break;
17043
17044 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17045 row_found = row;
17046 }
17047 }
17048
17049 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17050
17051 return row_found;
17052 }
17053
17054
17055 /* Make sure that glyph rows in the current matrix of window W
17056 reference the same glyph memory as corresponding rows in the
17057 frame's frame matrix. This function is called after scrolling W's
17058 current matrix on a terminal frame in try_window_id and
17059 try_window_reusing_current_matrix. */
17060
17061 static void
17062 sync_frame_with_window_matrix_rows (struct window *w)
17063 {
17064 struct frame *f = XFRAME (w->frame);
17065 struct glyph_row *window_row, *window_row_end, *frame_row;
17066
17067 /* Preconditions: W must be a leaf window and full-width. Its frame
17068 must have a frame matrix. */
17069 eassert (NILP (w->hchild) && NILP (w->vchild));
17070 eassert (WINDOW_FULL_WIDTH_P (w));
17071 eassert (!FRAME_WINDOW_P (f));
17072
17073 /* If W is a full-width window, glyph pointers in W's current matrix
17074 have, by definition, to be the same as glyph pointers in the
17075 corresponding frame matrix. Note that frame matrices have no
17076 marginal areas (see build_frame_matrix). */
17077 window_row = w->current_matrix->rows;
17078 window_row_end = window_row + w->current_matrix->nrows;
17079 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17080 while (window_row < window_row_end)
17081 {
17082 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17083 struct glyph *end = window_row->glyphs[LAST_AREA];
17084
17085 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17086 frame_row->glyphs[TEXT_AREA] = start;
17087 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17088 frame_row->glyphs[LAST_AREA] = end;
17089
17090 /* Disable frame rows whose corresponding window rows have
17091 been disabled in try_window_id. */
17092 if (!window_row->enabled_p)
17093 frame_row->enabled_p = 0;
17094
17095 ++window_row, ++frame_row;
17096 }
17097 }
17098
17099
17100 /* Find the glyph row in window W containing CHARPOS. Consider all
17101 rows between START and END (not inclusive). END null means search
17102 all rows to the end of the display area of W. Value is the row
17103 containing CHARPOS or null. */
17104
17105 struct glyph_row *
17106 row_containing_pos (struct window *w, ptrdiff_t charpos,
17107 struct glyph_row *start, struct glyph_row *end, int dy)
17108 {
17109 struct glyph_row *row = start;
17110 struct glyph_row *best_row = NULL;
17111 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
17112 int last_y;
17113
17114 /* If we happen to start on a header-line, skip that. */
17115 if (row->mode_line_p)
17116 ++row;
17117
17118 if ((end && row >= end) || !row->enabled_p)
17119 return NULL;
17120
17121 last_y = window_text_bottom_y (w) - dy;
17122
17123 while (1)
17124 {
17125 /* Give up if we have gone too far. */
17126 if (end && row >= end)
17127 return NULL;
17128 /* This formerly returned if they were equal.
17129 I think that both quantities are of a "last plus one" type;
17130 if so, when they are equal, the row is within the screen. -- rms. */
17131 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17132 return NULL;
17133
17134 /* If it is in this row, return this row. */
17135 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17136 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17137 /* The end position of a row equals the start
17138 position of the next row. If CHARPOS is there, we
17139 would rather display it in the next line, except
17140 when this line ends in ZV. */
17141 && !row->ends_at_zv_p
17142 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17143 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17144 {
17145 struct glyph *g;
17146
17147 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17148 || (!best_row && !row->continued_p))
17149 return row;
17150 /* In bidi-reordered rows, there could be several rows
17151 occluding point, all of them belonging to the same
17152 continued line. We need to find the row which fits
17153 CHARPOS the best. */
17154 for (g = row->glyphs[TEXT_AREA];
17155 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17156 g++)
17157 {
17158 if (!STRINGP (g->object))
17159 {
17160 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17161 {
17162 mindif = eabs (g->charpos - charpos);
17163 best_row = row;
17164 /* Exact match always wins. */
17165 if (mindif == 0)
17166 return best_row;
17167 }
17168 }
17169 }
17170 }
17171 else if (best_row && !row->continued_p)
17172 return best_row;
17173 ++row;
17174 }
17175 }
17176
17177
17178 /* Try to redisplay window W by reusing its existing display. W's
17179 current matrix must be up to date when this function is called,
17180 i.e. window_end_valid must not be nil.
17181
17182 Value is
17183
17184 1 if display has been updated
17185 0 if otherwise unsuccessful
17186 -1 if redisplay with same window start is known not to succeed
17187
17188 The following steps are performed:
17189
17190 1. Find the last row in the current matrix of W that is not
17191 affected by changes at the start of current_buffer. If no such row
17192 is found, give up.
17193
17194 2. Find the first row in W's current matrix that is not affected by
17195 changes at the end of current_buffer. Maybe there is no such row.
17196
17197 3. Display lines beginning with the row + 1 found in step 1 to the
17198 row found in step 2 or, if step 2 didn't find a row, to the end of
17199 the window.
17200
17201 4. If cursor is not known to appear on the window, give up.
17202
17203 5. If display stopped at the row found in step 2, scroll the
17204 display and current matrix as needed.
17205
17206 6. Maybe display some lines at the end of W, if we must. This can
17207 happen under various circumstances, like a partially visible line
17208 becoming fully visible, or because newly displayed lines are displayed
17209 in smaller font sizes.
17210
17211 7. Update W's window end information. */
17212
17213 static int
17214 try_window_id (struct window *w)
17215 {
17216 struct frame *f = XFRAME (w->frame);
17217 struct glyph_matrix *current_matrix = w->current_matrix;
17218 struct glyph_matrix *desired_matrix = w->desired_matrix;
17219 struct glyph_row *last_unchanged_at_beg_row;
17220 struct glyph_row *first_unchanged_at_end_row;
17221 struct glyph_row *row;
17222 struct glyph_row *bottom_row;
17223 int bottom_vpos;
17224 struct it it;
17225 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17226 int dvpos, dy;
17227 struct text_pos start_pos;
17228 struct run run;
17229 int first_unchanged_at_end_vpos = 0;
17230 struct glyph_row *last_text_row, *last_text_row_at_end;
17231 struct text_pos start;
17232 ptrdiff_t first_changed_charpos, last_changed_charpos;
17233
17234 #ifdef GLYPH_DEBUG
17235 if (inhibit_try_window_id)
17236 return 0;
17237 #endif
17238
17239 /* This is handy for debugging. */
17240 #if 0
17241 #define GIVE_UP(X) \
17242 do { \
17243 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17244 return 0; \
17245 } while (0)
17246 #else
17247 #define GIVE_UP(X) return 0
17248 #endif
17249
17250 SET_TEXT_POS_FROM_MARKER (start, w->start);
17251
17252 /* Don't use this for mini-windows because these can show
17253 messages and mini-buffers, and we don't handle that here. */
17254 if (MINI_WINDOW_P (w))
17255 GIVE_UP (1);
17256
17257 /* This flag is used to prevent redisplay optimizations. */
17258 if (windows_or_buffers_changed || cursor_type_changed)
17259 GIVE_UP (2);
17260
17261 /* Verify that narrowing has not changed.
17262 Also verify that we were not told to prevent redisplay optimizations.
17263 It would be nice to further
17264 reduce the number of cases where this prevents try_window_id. */
17265 if (current_buffer->clip_changed
17266 || current_buffer->prevent_redisplay_optimizations_p)
17267 GIVE_UP (3);
17268
17269 /* Window must either use window-based redisplay or be full width. */
17270 if (!FRAME_WINDOW_P (f)
17271 && (!FRAME_LINE_INS_DEL_OK (f)
17272 || !WINDOW_FULL_WIDTH_P (w)))
17273 GIVE_UP (4);
17274
17275 /* Give up if point is known NOT to appear in W. */
17276 if (PT < CHARPOS (start))
17277 GIVE_UP (5);
17278
17279 /* Another way to prevent redisplay optimizations. */
17280 if (w->last_modified == 0)
17281 GIVE_UP (6);
17282
17283 /* Verify that window is not hscrolled. */
17284 if (w->hscroll != 0)
17285 GIVE_UP (7);
17286
17287 /* Verify that display wasn't paused. */
17288 if (NILP (w->window_end_valid))
17289 GIVE_UP (8);
17290
17291 /* Can't use this if highlighting a region because a cursor movement
17292 will do more than just set the cursor. */
17293 if (0 <= markpos_of_region ())
17294 GIVE_UP (9);
17295
17296 /* Likewise if highlighting trailing whitespace. */
17297 if (!NILP (Vshow_trailing_whitespace))
17298 GIVE_UP (11);
17299
17300 /* Likewise if showing a region. */
17301 if (!NILP (w->region_showing))
17302 GIVE_UP (10);
17303
17304 /* Can't use this if overlay arrow position and/or string have
17305 changed. */
17306 if (overlay_arrows_changed_p ())
17307 GIVE_UP (12);
17308
17309 /* When word-wrap is on, adding a space to the first word of a
17310 wrapped line can change the wrap position, altering the line
17311 above it. It might be worthwhile to handle this more
17312 intelligently, but for now just redisplay from scratch. */
17313 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17314 GIVE_UP (21);
17315
17316 /* Under bidi reordering, adding or deleting a character in the
17317 beginning of a paragraph, before the first strong directional
17318 character, can change the base direction of the paragraph (unless
17319 the buffer specifies a fixed paragraph direction), which will
17320 require to redisplay the whole paragraph. It might be worthwhile
17321 to find the paragraph limits and widen the range of redisplayed
17322 lines to that, but for now just give up this optimization and
17323 redisplay from scratch. */
17324 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17325 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17326 GIVE_UP (22);
17327
17328 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17329 only if buffer has really changed. The reason is that the gap is
17330 initially at Z for freshly visited files. The code below would
17331 set end_unchanged to 0 in that case. */
17332 if (MODIFF > SAVE_MODIFF
17333 /* This seems to happen sometimes after saving a buffer. */
17334 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17335 {
17336 if (GPT - BEG < BEG_UNCHANGED)
17337 BEG_UNCHANGED = GPT - BEG;
17338 if (Z - GPT < END_UNCHANGED)
17339 END_UNCHANGED = Z - GPT;
17340 }
17341
17342 /* The position of the first and last character that has been changed. */
17343 first_changed_charpos = BEG + BEG_UNCHANGED;
17344 last_changed_charpos = Z - END_UNCHANGED;
17345
17346 /* If window starts after a line end, and the last change is in
17347 front of that newline, then changes don't affect the display.
17348 This case happens with stealth-fontification. Note that although
17349 the display is unchanged, glyph positions in the matrix have to
17350 be adjusted, of course. */
17351 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17352 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17353 && ((last_changed_charpos < CHARPOS (start)
17354 && CHARPOS (start) == BEGV)
17355 || (last_changed_charpos < CHARPOS (start) - 1
17356 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17357 {
17358 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17359 struct glyph_row *r0;
17360
17361 /* Compute how many chars/bytes have been added to or removed
17362 from the buffer. */
17363 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17364 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17365 Z_delta = Z - Z_old;
17366 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17367
17368 /* Give up if PT is not in the window. Note that it already has
17369 been checked at the start of try_window_id that PT is not in
17370 front of the window start. */
17371 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17372 GIVE_UP (13);
17373
17374 /* If window start is unchanged, we can reuse the whole matrix
17375 as is, after adjusting glyph positions. No need to compute
17376 the window end again, since its offset from Z hasn't changed. */
17377 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17378 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17379 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17380 /* PT must not be in a partially visible line. */
17381 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17382 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17383 {
17384 /* Adjust positions in the glyph matrix. */
17385 if (Z_delta || Z_delta_bytes)
17386 {
17387 struct glyph_row *r1
17388 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17389 increment_matrix_positions (w->current_matrix,
17390 MATRIX_ROW_VPOS (r0, current_matrix),
17391 MATRIX_ROW_VPOS (r1, current_matrix),
17392 Z_delta, Z_delta_bytes);
17393 }
17394
17395 /* Set the cursor. */
17396 row = row_containing_pos (w, PT, r0, NULL, 0);
17397 if (row)
17398 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17399 else
17400 emacs_abort ();
17401 return 1;
17402 }
17403 }
17404
17405 /* Handle the case that changes are all below what is displayed in
17406 the window, and that PT is in the window. This shortcut cannot
17407 be taken if ZV is visible in the window, and text has been added
17408 there that is visible in the window. */
17409 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17410 /* ZV is not visible in the window, or there are no
17411 changes at ZV, actually. */
17412 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17413 || first_changed_charpos == last_changed_charpos))
17414 {
17415 struct glyph_row *r0;
17416
17417 /* Give up if PT is not in the window. Note that it already has
17418 been checked at the start of try_window_id that PT is not in
17419 front of the window start. */
17420 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17421 GIVE_UP (14);
17422
17423 /* If window start is unchanged, we can reuse the whole matrix
17424 as is, without changing glyph positions since no text has
17425 been added/removed in front of the window end. */
17426 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17427 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17428 /* PT must not be in a partially visible line. */
17429 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17430 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17431 {
17432 /* We have to compute the window end anew since text
17433 could have been added/removed after it. */
17434 wset_window_end_pos
17435 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17436 w->window_end_bytepos
17437 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17438
17439 /* Set the cursor. */
17440 row = row_containing_pos (w, PT, r0, NULL, 0);
17441 if (row)
17442 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17443 else
17444 emacs_abort ();
17445 return 2;
17446 }
17447 }
17448
17449 /* Give up if window start is in the changed area.
17450
17451 The condition used to read
17452
17453 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17454
17455 but why that was tested escapes me at the moment. */
17456 if (CHARPOS (start) >= first_changed_charpos
17457 && CHARPOS (start) <= last_changed_charpos)
17458 GIVE_UP (15);
17459
17460 /* Check that window start agrees with the start of the first glyph
17461 row in its current matrix. Check this after we know the window
17462 start is not in changed text, otherwise positions would not be
17463 comparable. */
17464 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17465 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17466 GIVE_UP (16);
17467
17468 /* Give up if the window ends in strings. Overlay strings
17469 at the end are difficult to handle, so don't try. */
17470 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17471 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17472 GIVE_UP (20);
17473
17474 /* Compute the position at which we have to start displaying new
17475 lines. Some of the lines at the top of the window might be
17476 reusable because they are not displaying changed text. Find the
17477 last row in W's current matrix not affected by changes at the
17478 start of current_buffer. Value is null if changes start in the
17479 first line of window. */
17480 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17481 if (last_unchanged_at_beg_row)
17482 {
17483 /* Avoid starting to display in the middle of a character, a TAB
17484 for instance. This is easier than to set up the iterator
17485 exactly, and it's not a frequent case, so the additional
17486 effort wouldn't really pay off. */
17487 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17488 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17489 && last_unchanged_at_beg_row > w->current_matrix->rows)
17490 --last_unchanged_at_beg_row;
17491
17492 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17493 GIVE_UP (17);
17494
17495 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17496 GIVE_UP (18);
17497 start_pos = it.current.pos;
17498
17499 /* Start displaying new lines in the desired matrix at the same
17500 vpos we would use in the current matrix, i.e. below
17501 last_unchanged_at_beg_row. */
17502 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17503 current_matrix);
17504 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17505 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17506
17507 eassert (it.hpos == 0 && it.current_x == 0);
17508 }
17509 else
17510 {
17511 /* There are no reusable lines at the start of the window.
17512 Start displaying in the first text line. */
17513 start_display (&it, w, start);
17514 it.vpos = it.first_vpos;
17515 start_pos = it.current.pos;
17516 }
17517
17518 /* Find the first row that is not affected by changes at the end of
17519 the buffer. Value will be null if there is no unchanged row, in
17520 which case we must redisplay to the end of the window. delta
17521 will be set to the value by which buffer positions beginning with
17522 first_unchanged_at_end_row have to be adjusted due to text
17523 changes. */
17524 first_unchanged_at_end_row
17525 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17526 IF_DEBUG (debug_delta = delta);
17527 IF_DEBUG (debug_delta_bytes = delta_bytes);
17528
17529 /* Set stop_pos to the buffer position up to which we will have to
17530 display new lines. If first_unchanged_at_end_row != NULL, this
17531 is the buffer position of the start of the line displayed in that
17532 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17533 that we don't stop at a buffer position. */
17534 stop_pos = 0;
17535 if (first_unchanged_at_end_row)
17536 {
17537 eassert (last_unchanged_at_beg_row == NULL
17538 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17539
17540 /* If this is a continuation line, move forward to the next one
17541 that isn't. Changes in lines above affect this line.
17542 Caution: this may move first_unchanged_at_end_row to a row
17543 not displaying text. */
17544 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17545 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17546 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17547 < it.last_visible_y))
17548 ++first_unchanged_at_end_row;
17549
17550 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17551 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17552 >= it.last_visible_y))
17553 first_unchanged_at_end_row = NULL;
17554 else
17555 {
17556 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17557 + delta);
17558 first_unchanged_at_end_vpos
17559 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17560 eassert (stop_pos >= Z - END_UNCHANGED);
17561 }
17562 }
17563 else if (last_unchanged_at_beg_row == NULL)
17564 GIVE_UP (19);
17565
17566
17567 #ifdef GLYPH_DEBUG
17568
17569 /* Either there is no unchanged row at the end, or the one we have
17570 now displays text. This is a necessary condition for the window
17571 end pos calculation at the end of this function. */
17572 eassert (first_unchanged_at_end_row == NULL
17573 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17574
17575 debug_last_unchanged_at_beg_vpos
17576 = (last_unchanged_at_beg_row
17577 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17578 : -1);
17579 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17580
17581 #endif /* GLYPH_DEBUG */
17582
17583
17584 /* Display new lines. Set last_text_row to the last new line
17585 displayed which has text on it, i.e. might end up as being the
17586 line where the window_end_vpos is. */
17587 w->cursor.vpos = -1;
17588 last_text_row = NULL;
17589 overlay_arrow_seen = 0;
17590 while (it.current_y < it.last_visible_y
17591 && !fonts_changed_p
17592 && (first_unchanged_at_end_row == NULL
17593 || IT_CHARPOS (it) < stop_pos))
17594 {
17595 if (display_line (&it))
17596 last_text_row = it.glyph_row - 1;
17597 }
17598
17599 if (fonts_changed_p)
17600 return -1;
17601
17602
17603 /* Compute differences in buffer positions, y-positions etc. for
17604 lines reused at the bottom of the window. Compute what we can
17605 scroll. */
17606 if (first_unchanged_at_end_row
17607 /* No lines reused because we displayed everything up to the
17608 bottom of the window. */
17609 && it.current_y < it.last_visible_y)
17610 {
17611 dvpos = (it.vpos
17612 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17613 current_matrix));
17614 dy = it.current_y - first_unchanged_at_end_row->y;
17615 run.current_y = first_unchanged_at_end_row->y;
17616 run.desired_y = run.current_y + dy;
17617 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17618 }
17619 else
17620 {
17621 delta = delta_bytes = dvpos = dy
17622 = run.current_y = run.desired_y = run.height = 0;
17623 first_unchanged_at_end_row = NULL;
17624 }
17625 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17626
17627
17628 /* Find the cursor if not already found. We have to decide whether
17629 PT will appear on this window (it sometimes doesn't, but this is
17630 not a very frequent case.) This decision has to be made before
17631 the current matrix is altered. A value of cursor.vpos < 0 means
17632 that PT is either in one of the lines beginning at
17633 first_unchanged_at_end_row or below the window. Don't care for
17634 lines that might be displayed later at the window end; as
17635 mentioned, this is not a frequent case. */
17636 if (w->cursor.vpos < 0)
17637 {
17638 /* Cursor in unchanged rows at the top? */
17639 if (PT < CHARPOS (start_pos)
17640 && last_unchanged_at_beg_row)
17641 {
17642 row = row_containing_pos (w, PT,
17643 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17644 last_unchanged_at_beg_row + 1, 0);
17645 if (row)
17646 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17647 }
17648
17649 /* Start from first_unchanged_at_end_row looking for PT. */
17650 else if (first_unchanged_at_end_row)
17651 {
17652 row = row_containing_pos (w, PT - delta,
17653 first_unchanged_at_end_row, NULL, 0);
17654 if (row)
17655 set_cursor_from_row (w, row, w->current_matrix, delta,
17656 delta_bytes, dy, dvpos);
17657 }
17658
17659 /* Give up if cursor was not found. */
17660 if (w->cursor.vpos < 0)
17661 {
17662 clear_glyph_matrix (w->desired_matrix);
17663 return -1;
17664 }
17665 }
17666
17667 /* Don't let the cursor end in the scroll margins. */
17668 {
17669 int this_scroll_margin, cursor_height;
17670
17671 this_scroll_margin =
17672 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17673 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17674 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17675
17676 if ((w->cursor.y < this_scroll_margin
17677 && CHARPOS (start) > BEGV)
17678 /* Old redisplay didn't take scroll margin into account at the bottom,
17679 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17680 || (w->cursor.y + (make_cursor_line_fully_visible_p
17681 ? cursor_height + this_scroll_margin
17682 : 1)) > it.last_visible_y)
17683 {
17684 w->cursor.vpos = -1;
17685 clear_glyph_matrix (w->desired_matrix);
17686 return -1;
17687 }
17688 }
17689
17690 /* Scroll the display. Do it before changing the current matrix so
17691 that xterm.c doesn't get confused about where the cursor glyph is
17692 found. */
17693 if (dy && run.height)
17694 {
17695 update_begin (f);
17696
17697 if (FRAME_WINDOW_P (f))
17698 {
17699 FRAME_RIF (f)->update_window_begin_hook (w);
17700 FRAME_RIF (f)->clear_window_mouse_face (w);
17701 FRAME_RIF (f)->scroll_run_hook (w, &run);
17702 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17703 }
17704 else
17705 {
17706 /* Terminal frame. In this case, dvpos gives the number of
17707 lines to scroll by; dvpos < 0 means scroll up. */
17708 int from_vpos
17709 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17710 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17711 int end = (WINDOW_TOP_EDGE_LINE (w)
17712 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17713 + window_internal_height (w));
17714
17715 #if defined (HAVE_GPM) || defined (MSDOS)
17716 x_clear_window_mouse_face (w);
17717 #endif
17718 /* Perform the operation on the screen. */
17719 if (dvpos > 0)
17720 {
17721 /* Scroll last_unchanged_at_beg_row to the end of the
17722 window down dvpos lines. */
17723 set_terminal_window (f, end);
17724
17725 /* On dumb terminals delete dvpos lines at the end
17726 before inserting dvpos empty lines. */
17727 if (!FRAME_SCROLL_REGION_OK (f))
17728 ins_del_lines (f, end - dvpos, -dvpos);
17729
17730 /* Insert dvpos empty lines in front of
17731 last_unchanged_at_beg_row. */
17732 ins_del_lines (f, from, dvpos);
17733 }
17734 else if (dvpos < 0)
17735 {
17736 /* Scroll up last_unchanged_at_beg_vpos to the end of
17737 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17738 set_terminal_window (f, end);
17739
17740 /* Delete dvpos lines in front of
17741 last_unchanged_at_beg_vpos. ins_del_lines will set
17742 the cursor to the given vpos and emit |dvpos| delete
17743 line sequences. */
17744 ins_del_lines (f, from + dvpos, dvpos);
17745
17746 /* On a dumb terminal insert dvpos empty lines at the
17747 end. */
17748 if (!FRAME_SCROLL_REGION_OK (f))
17749 ins_del_lines (f, end + dvpos, -dvpos);
17750 }
17751
17752 set_terminal_window (f, 0);
17753 }
17754
17755 update_end (f);
17756 }
17757
17758 /* Shift reused rows of the current matrix to the right position.
17759 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17760 text. */
17761 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17762 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17763 if (dvpos < 0)
17764 {
17765 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17766 bottom_vpos, dvpos);
17767 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17768 bottom_vpos);
17769 }
17770 else if (dvpos > 0)
17771 {
17772 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17773 bottom_vpos, dvpos);
17774 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17775 first_unchanged_at_end_vpos + dvpos);
17776 }
17777
17778 /* For frame-based redisplay, make sure that current frame and window
17779 matrix are in sync with respect to glyph memory. */
17780 if (!FRAME_WINDOW_P (f))
17781 sync_frame_with_window_matrix_rows (w);
17782
17783 /* Adjust buffer positions in reused rows. */
17784 if (delta || delta_bytes)
17785 increment_matrix_positions (current_matrix,
17786 first_unchanged_at_end_vpos + dvpos,
17787 bottom_vpos, delta, delta_bytes);
17788
17789 /* Adjust Y positions. */
17790 if (dy)
17791 shift_glyph_matrix (w, current_matrix,
17792 first_unchanged_at_end_vpos + dvpos,
17793 bottom_vpos, dy);
17794
17795 if (first_unchanged_at_end_row)
17796 {
17797 first_unchanged_at_end_row += dvpos;
17798 if (first_unchanged_at_end_row->y >= it.last_visible_y
17799 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17800 first_unchanged_at_end_row = NULL;
17801 }
17802
17803 /* If scrolling up, there may be some lines to display at the end of
17804 the window. */
17805 last_text_row_at_end = NULL;
17806 if (dy < 0)
17807 {
17808 /* Scrolling up can leave for example a partially visible line
17809 at the end of the window to be redisplayed. */
17810 /* Set last_row to the glyph row in the current matrix where the
17811 window end line is found. It has been moved up or down in
17812 the matrix by dvpos. */
17813 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17814 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17815
17816 /* If last_row is the window end line, it should display text. */
17817 eassert (last_row->displays_text_p);
17818
17819 /* If window end line was partially visible before, begin
17820 displaying at that line. Otherwise begin displaying with the
17821 line following it. */
17822 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17823 {
17824 init_to_row_start (&it, w, last_row);
17825 it.vpos = last_vpos;
17826 it.current_y = last_row->y;
17827 }
17828 else
17829 {
17830 init_to_row_end (&it, w, last_row);
17831 it.vpos = 1 + last_vpos;
17832 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17833 ++last_row;
17834 }
17835
17836 /* We may start in a continuation line. If so, we have to
17837 get the right continuation_lines_width and current_x. */
17838 it.continuation_lines_width = last_row->continuation_lines_width;
17839 it.hpos = it.current_x = 0;
17840
17841 /* Display the rest of the lines at the window end. */
17842 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17843 while (it.current_y < it.last_visible_y
17844 && !fonts_changed_p)
17845 {
17846 /* Is it always sure that the display agrees with lines in
17847 the current matrix? I don't think so, so we mark rows
17848 displayed invalid in the current matrix by setting their
17849 enabled_p flag to zero. */
17850 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17851 if (display_line (&it))
17852 last_text_row_at_end = it.glyph_row - 1;
17853 }
17854 }
17855
17856 /* Update window_end_pos and window_end_vpos. */
17857 if (first_unchanged_at_end_row
17858 && !last_text_row_at_end)
17859 {
17860 /* Window end line if one of the preserved rows from the current
17861 matrix. Set row to the last row displaying text in current
17862 matrix starting at first_unchanged_at_end_row, after
17863 scrolling. */
17864 eassert (first_unchanged_at_end_row->displays_text_p);
17865 row = find_last_row_displaying_text (w->current_matrix, &it,
17866 first_unchanged_at_end_row);
17867 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17868
17869 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17870 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17871 wset_window_end_vpos
17872 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17873 eassert (w->window_end_bytepos >= 0);
17874 IF_DEBUG (debug_method_add (w, "A"));
17875 }
17876 else if (last_text_row_at_end)
17877 {
17878 wset_window_end_pos
17879 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17880 w->window_end_bytepos
17881 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17882 wset_window_end_vpos
17883 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17884 desired_matrix)));
17885 eassert (w->window_end_bytepos >= 0);
17886 IF_DEBUG (debug_method_add (w, "B"));
17887 }
17888 else if (last_text_row)
17889 {
17890 /* We have displayed either to the end of the window or at the
17891 end of the window, i.e. the last row with text is to be found
17892 in the desired matrix. */
17893 wset_window_end_pos
17894 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17895 w->window_end_bytepos
17896 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17897 wset_window_end_vpos
17898 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17899 eassert (w->window_end_bytepos >= 0);
17900 }
17901 else if (first_unchanged_at_end_row == NULL
17902 && last_text_row == NULL
17903 && last_text_row_at_end == NULL)
17904 {
17905 /* Displayed to end of window, but no line containing text was
17906 displayed. Lines were deleted at the end of the window. */
17907 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17908 int vpos = XFASTINT (w->window_end_vpos);
17909 struct glyph_row *current_row = current_matrix->rows + vpos;
17910 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17911
17912 for (row = NULL;
17913 row == NULL && vpos >= first_vpos;
17914 --vpos, --current_row, --desired_row)
17915 {
17916 if (desired_row->enabled_p)
17917 {
17918 if (desired_row->displays_text_p)
17919 row = desired_row;
17920 }
17921 else if (current_row->displays_text_p)
17922 row = current_row;
17923 }
17924
17925 eassert (row != NULL);
17926 wset_window_end_vpos (w, make_number (vpos + 1));
17927 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17928 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17929 eassert (w->window_end_bytepos >= 0);
17930 IF_DEBUG (debug_method_add (w, "C"));
17931 }
17932 else
17933 emacs_abort ();
17934
17935 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
17936 debug_end_vpos = XFASTINT (w->window_end_vpos));
17937
17938 /* Record that display has not been completed. */
17939 wset_window_end_valid (w, Qnil);
17940 w->desired_matrix->no_scrolling_p = 1;
17941 return 3;
17942
17943 #undef GIVE_UP
17944 }
17945
17946
17947 \f
17948 /***********************************************************************
17949 More debugging support
17950 ***********************************************************************/
17951
17952 #ifdef GLYPH_DEBUG
17953
17954 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
17955 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
17956 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
17957
17958
17959 /* Dump the contents of glyph matrix MATRIX on stderr.
17960
17961 GLYPHS 0 means don't show glyph contents.
17962 GLYPHS 1 means show glyphs in short form
17963 GLYPHS > 1 means show glyphs in long form. */
17964
17965 void
17966 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
17967 {
17968 int i;
17969 for (i = 0; i < matrix->nrows; ++i)
17970 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
17971 }
17972
17973
17974 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
17975 the glyph row and area where the glyph comes from. */
17976
17977 void
17978 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
17979 {
17980 if (glyph->type == CHAR_GLYPH)
17981 {
17982 fprintf (stderr,
17983 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
17984 glyph - row->glyphs[TEXT_AREA],
17985 'C',
17986 glyph->charpos,
17987 (BUFFERP (glyph->object)
17988 ? 'B'
17989 : (STRINGP (glyph->object)
17990 ? 'S'
17991 : '-')),
17992 glyph->pixel_width,
17993 glyph->u.ch,
17994 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
17995 ? glyph->u.ch
17996 : '.'),
17997 glyph->face_id,
17998 glyph->left_box_line_p,
17999 glyph->right_box_line_p);
18000 }
18001 else if (glyph->type == STRETCH_GLYPH)
18002 {
18003 fprintf (stderr,
18004 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18005 glyph - row->glyphs[TEXT_AREA],
18006 'S',
18007 glyph->charpos,
18008 (BUFFERP (glyph->object)
18009 ? 'B'
18010 : (STRINGP (glyph->object)
18011 ? 'S'
18012 : '-')),
18013 glyph->pixel_width,
18014 0,
18015 '.',
18016 glyph->face_id,
18017 glyph->left_box_line_p,
18018 glyph->right_box_line_p);
18019 }
18020 else if (glyph->type == IMAGE_GLYPH)
18021 {
18022 fprintf (stderr,
18023 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18024 glyph - row->glyphs[TEXT_AREA],
18025 'I',
18026 glyph->charpos,
18027 (BUFFERP (glyph->object)
18028 ? 'B'
18029 : (STRINGP (glyph->object)
18030 ? 'S'
18031 : '-')),
18032 glyph->pixel_width,
18033 glyph->u.img_id,
18034 '.',
18035 glyph->face_id,
18036 glyph->left_box_line_p,
18037 glyph->right_box_line_p);
18038 }
18039 else if (glyph->type == COMPOSITE_GLYPH)
18040 {
18041 fprintf (stderr,
18042 " %5td %4c %6"pI"d %c %3d 0x%05x",
18043 glyph - row->glyphs[TEXT_AREA],
18044 '+',
18045 glyph->charpos,
18046 (BUFFERP (glyph->object)
18047 ? 'B'
18048 : (STRINGP (glyph->object)
18049 ? 'S'
18050 : '-')),
18051 glyph->pixel_width,
18052 glyph->u.cmp.id);
18053 if (glyph->u.cmp.automatic)
18054 fprintf (stderr,
18055 "[%d-%d]",
18056 glyph->slice.cmp.from, glyph->slice.cmp.to);
18057 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18058 glyph->face_id,
18059 glyph->left_box_line_p,
18060 glyph->right_box_line_p);
18061 }
18062 }
18063
18064
18065 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18066 GLYPHS 0 means don't show glyph contents.
18067 GLYPHS 1 means show glyphs in short form
18068 GLYPHS > 1 means show glyphs in long form. */
18069
18070 void
18071 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18072 {
18073 if (glyphs != 1)
18074 {
18075 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18076 fprintf (stderr, "======================================================================\n");
18077
18078 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18079 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18080 vpos,
18081 MATRIX_ROW_START_CHARPOS (row),
18082 MATRIX_ROW_END_CHARPOS (row),
18083 row->used[TEXT_AREA],
18084 row->contains_overlapping_glyphs_p,
18085 row->enabled_p,
18086 row->truncated_on_left_p,
18087 row->truncated_on_right_p,
18088 row->continued_p,
18089 MATRIX_ROW_CONTINUATION_LINE_P (row),
18090 row->displays_text_p,
18091 row->ends_at_zv_p,
18092 row->fill_line_p,
18093 row->ends_in_middle_of_char_p,
18094 row->starts_in_middle_of_char_p,
18095 row->mouse_face_p,
18096 row->x,
18097 row->y,
18098 row->pixel_width,
18099 row->height,
18100 row->visible_height,
18101 row->ascent,
18102 row->phys_ascent);
18103 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
18104 row->end.overlay_string_index,
18105 row->continuation_lines_width);
18106 fprintf (stderr, "%9"pI"d %5"pI"d\n",
18107 CHARPOS (row->start.string_pos),
18108 CHARPOS (row->end.string_pos));
18109 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
18110 row->end.dpvec_index);
18111 }
18112
18113 if (glyphs > 1)
18114 {
18115 int area;
18116
18117 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18118 {
18119 struct glyph *glyph = row->glyphs[area];
18120 struct glyph *glyph_end = glyph + row->used[area];
18121
18122 /* Glyph for a line end in text. */
18123 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18124 ++glyph_end;
18125
18126 if (glyph < glyph_end)
18127 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
18128
18129 for (; glyph < glyph_end; ++glyph)
18130 dump_glyph (row, glyph, area);
18131 }
18132 }
18133 else if (glyphs == 1)
18134 {
18135 int area;
18136
18137 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18138 {
18139 char *s = alloca (row->used[area] + 1);
18140 int i;
18141
18142 for (i = 0; i < row->used[area]; ++i)
18143 {
18144 struct glyph *glyph = row->glyphs[area] + i;
18145 if (glyph->type == CHAR_GLYPH
18146 && glyph->u.ch < 0x80
18147 && glyph->u.ch >= ' ')
18148 s[i] = glyph->u.ch;
18149 else
18150 s[i] = '.';
18151 }
18152
18153 s[i] = '\0';
18154 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18155 }
18156 }
18157 }
18158
18159
18160 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18161 Sdump_glyph_matrix, 0, 1, "p",
18162 doc: /* Dump the current matrix of the selected window to stderr.
18163 Shows contents of glyph row structures. With non-nil
18164 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18165 glyphs in short form, otherwise show glyphs in long form. */)
18166 (Lisp_Object glyphs)
18167 {
18168 struct window *w = XWINDOW (selected_window);
18169 struct buffer *buffer = XBUFFER (w->buffer);
18170
18171 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18172 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18173 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18174 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18175 fprintf (stderr, "=============================================\n");
18176 dump_glyph_matrix (w->current_matrix,
18177 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18178 return Qnil;
18179 }
18180
18181
18182 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18183 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18184 (void)
18185 {
18186 struct frame *f = XFRAME (selected_frame);
18187 dump_glyph_matrix (f->current_matrix, 1);
18188 return Qnil;
18189 }
18190
18191
18192 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18193 doc: /* Dump glyph row ROW to stderr.
18194 GLYPH 0 means don't dump glyphs.
18195 GLYPH 1 means dump glyphs in short form.
18196 GLYPH > 1 or omitted means dump glyphs in long form. */)
18197 (Lisp_Object row, Lisp_Object glyphs)
18198 {
18199 struct glyph_matrix *matrix;
18200 EMACS_INT vpos;
18201
18202 CHECK_NUMBER (row);
18203 matrix = XWINDOW (selected_window)->current_matrix;
18204 vpos = XINT (row);
18205 if (vpos >= 0 && vpos < matrix->nrows)
18206 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18207 vpos,
18208 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18209 return Qnil;
18210 }
18211
18212
18213 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18214 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18215 GLYPH 0 means don't dump glyphs.
18216 GLYPH 1 means dump glyphs in short form.
18217 GLYPH > 1 or omitted means dump glyphs in long form. */)
18218 (Lisp_Object row, Lisp_Object glyphs)
18219 {
18220 struct frame *sf = SELECTED_FRAME ();
18221 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18222 EMACS_INT vpos;
18223
18224 CHECK_NUMBER (row);
18225 vpos = XINT (row);
18226 if (vpos >= 0 && vpos < m->nrows)
18227 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18228 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18229 return Qnil;
18230 }
18231
18232
18233 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18234 doc: /* Toggle tracing of redisplay.
18235 With ARG, turn tracing on if and only if ARG is positive. */)
18236 (Lisp_Object arg)
18237 {
18238 if (NILP (arg))
18239 trace_redisplay_p = !trace_redisplay_p;
18240 else
18241 {
18242 arg = Fprefix_numeric_value (arg);
18243 trace_redisplay_p = XINT (arg) > 0;
18244 }
18245
18246 return Qnil;
18247 }
18248
18249
18250 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18251 doc: /* Like `format', but print result to stderr.
18252 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18253 (ptrdiff_t nargs, Lisp_Object *args)
18254 {
18255 Lisp_Object s = Fformat (nargs, args);
18256 fprintf (stderr, "%s", SDATA (s));
18257 return Qnil;
18258 }
18259
18260 #endif /* GLYPH_DEBUG */
18261
18262
18263 \f
18264 /***********************************************************************
18265 Building Desired Matrix Rows
18266 ***********************************************************************/
18267
18268 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18269 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18270
18271 static struct glyph_row *
18272 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18273 {
18274 struct frame *f = XFRAME (WINDOW_FRAME (w));
18275 struct buffer *buffer = XBUFFER (w->buffer);
18276 struct buffer *old = current_buffer;
18277 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18278 int arrow_len = SCHARS (overlay_arrow_string);
18279 const unsigned char *arrow_end = arrow_string + arrow_len;
18280 const unsigned char *p;
18281 struct it it;
18282 int multibyte_p;
18283 int n_glyphs_before;
18284
18285 set_buffer_temp (buffer);
18286 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18287 it.glyph_row->used[TEXT_AREA] = 0;
18288 SET_TEXT_POS (it.position, 0, 0);
18289
18290 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18291 p = arrow_string;
18292 while (p < arrow_end)
18293 {
18294 Lisp_Object face, ilisp;
18295
18296 /* Get the next character. */
18297 if (multibyte_p)
18298 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18299 else
18300 {
18301 it.c = it.char_to_display = *p, it.len = 1;
18302 if (! ASCII_CHAR_P (it.c))
18303 it.char_to_display = BYTE8_TO_CHAR (it.c);
18304 }
18305 p += it.len;
18306
18307 /* Get its face. */
18308 ilisp = make_number (p - arrow_string);
18309 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18310 it.face_id = compute_char_face (f, it.char_to_display, face);
18311
18312 /* Compute its width, get its glyphs. */
18313 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18314 SET_TEXT_POS (it.position, -1, -1);
18315 PRODUCE_GLYPHS (&it);
18316
18317 /* If this character doesn't fit any more in the line, we have
18318 to remove some glyphs. */
18319 if (it.current_x > it.last_visible_x)
18320 {
18321 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18322 break;
18323 }
18324 }
18325
18326 set_buffer_temp (old);
18327 return it.glyph_row;
18328 }
18329
18330
18331 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18332 glyphs to insert is determined by produce_special_glyphs. */
18333
18334 static void
18335 insert_left_trunc_glyphs (struct it *it)
18336 {
18337 struct it truncate_it;
18338 struct glyph *from, *end, *to, *toend;
18339
18340 eassert (!FRAME_WINDOW_P (it->f)
18341 || (!it->glyph_row->reversed_p
18342 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18343 || (it->glyph_row->reversed_p
18344 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18345
18346 /* Get the truncation glyphs. */
18347 truncate_it = *it;
18348 truncate_it.current_x = 0;
18349 truncate_it.face_id = DEFAULT_FACE_ID;
18350 truncate_it.glyph_row = &scratch_glyph_row;
18351 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18352 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18353 truncate_it.object = make_number (0);
18354 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18355
18356 /* Overwrite glyphs from IT with truncation glyphs. */
18357 if (!it->glyph_row->reversed_p)
18358 {
18359 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18360
18361 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18362 end = from + tused;
18363 to = it->glyph_row->glyphs[TEXT_AREA];
18364 toend = to + it->glyph_row->used[TEXT_AREA];
18365 if (FRAME_WINDOW_P (it->f))
18366 {
18367 /* On GUI frames, when variable-size fonts are displayed,
18368 the truncation glyphs may need more pixels than the row's
18369 glyphs they overwrite. We overwrite more glyphs to free
18370 enough screen real estate, and enlarge the stretch glyph
18371 on the right (see display_line), if there is one, to
18372 preserve the screen position of the truncation glyphs on
18373 the right. */
18374 int w = 0;
18375 struct glyph *g = to;
18376 short used;
18377
18378 /* The first glyph could be partially visible, in which case
18379 it->glyph_row->x will be negative. But we want the left
18380 truncation glyphs to be aligned at the left margin of the
18381 window, so we override the x coordinate at which the row
18382 will begin. */
18383 it->glyph_row->x = 0;
18384 while (g < toend && w < it->truncation_pixel_width)
18385 {
18386 w += g->pixel_width;
18387 ++g;
18388 }
18389 if (g - to - tused > 0)
18390 {
18391 memmove (to + tused, g, (toend - g) * sizeof(*g));
18392 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18393 }
18394 used = it->glyph_row->used[TEXT_AREA];
18395 if (it->glyph_row->truncated_on_right_p
18396 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18397 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18398 == STRETCH_GLYPH)
18399 {
18400 int extra = w - it->truncation_pixel_width;
18401
18402 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18403 }
18404 }
18405
18406 while (from < end)
18407 *to++ = *from++;
18408
18409 /* There may be padding glyphs left over. Overwrite them too. */
18410 if (!FRAME_WINDOW_P (it->f))
18411 {
18412 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18413 {
18414 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18415 while (from < end)
18416 *to++ = *from++;
18417 }
18418 }
18419
18420 if (to > toend)
18421 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18422 }
18423 else
18424 {
18425 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18426
18427 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18428 that back to front. */
18429 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18430 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18431 toend = it->glyph_row->glyphs[TEXT_AREA];
18432 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18433 if (FRAME_WINDOW_P (it->f))
18434 {
18435 int w = 0;
18436 struct glyph *g = to;
18437
18438 while (g >= toend && w < it->truncation_pixel_width)
18439 {
18440 w += g->pixel_width;
18441 --g;
18442 }
18443 if (to - g - tused > 0)
18444 to = g + tused;
18445 if (it->glyph_row->truncated_on_right_p
18446 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18447 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18448 {
18449 int extra = w - it->truncation_pixel_width;
18450
18451 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18452 }
18453 }
18454
18455 while (from >= end && to >= toend)
18456 *to-- = *from--;
18457 if (!FRAME_WINDOW_P (it->f))
18458 {
18459 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18460 {
18461 from =
18462 truncate_it.glyph_row->glyphs[TEXT_AREA]
18463 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18464 while (from >= end && to >= toend)
18465 *to-- = *from--;
18466 }
18467 }
18468 if (from >= end)
18469 {
18470 /* Need to free some room before prepending additional
18471 glyphs. */
18472 int move_by = from - end + 1;
18473 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18474 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18475
18476 for ( ; g >= g0; g--)
18477 g[move_by] = *g;
18478 while (from >= end)
18479 *to-- = *from--;
18480 it->glyph_row->used[TEXT_AREA] += move_by;
18481 }
18482 }
18483 }
18484
18485 /* Compute the hash code for ROW. */
18486 unsigned
18487 row_hash (struct glyph_row *row)
18488 {
18489 int area, k;
18490 unsigned hashval = 0;
18491
18492 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18493 for (k = 0; k < row->used[area]; ++k)
18494 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18495 + row->glyphs[area][k].u.val
18496 + row->glyphs[area][k].face_id
18497 + row->glyphs[area][k].padding_p
18498 + (row->glyphs[area][k].type << 2));
18499
18500 return hashval;
18501 }
18502
18503 /* Compute the pixel height and width of IT->glyph_row.
18504
18505 Most of the time, ascent and height of a display line will be equal
18506 to the max_ascent and max_height values of the display iterator
18507 structure. This is not the case if
18508
18509 1. We hit ZV without displaying anything. In this case, max_ascent
18510 and max_height will be zero.
18511
18512 2. We have some glyphs that don't contribute to the line height.
18513 (The glyph row flag contributes_to_line_height_p is for future
18514 pixmap extensions).
18515
18516 The first case is easily covered by using default values because in
18517 these cases, the line height does not really matter, except that it
18518 must not be zero. */
18519
18520 static void
18521 compute_line_metrics (struct it *it)
18522 {
18523 struct glyph_row *row = it->glyph_row;
18524
18525 if (FRAME_WINDOW_P (it->f))
18526 {
18527 int i, min_y, max_y;
18528
18529 /* The line may consist of one space only, that was added to
18530 place the cursor on it. If so, the row's height hasn't been
18531 computed yet. */
18532 if (row->height == 0)
18533 {
18534 if (it->max_ascent + it->max_descent == 0)
18535 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18536 row->ascent = it->max_ascent;
18537 row->height = it->max_ascent + it->max_descent;
18538 row->phys_ascent = it->max_phys_ascent;
18539 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18540 row->extra_line_spacing = it->max_extra_line_spacing;
18541 }
18542
18543 /* Compute the width of this line. */
18544 row->pixel_width = row->x;
18545 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18546 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18547
18548 eassert (row->pixel_width >= 0);
18549 eassert (row->ascent >= 0 && row->height > 0);
18550
18551 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18552 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18553
18554 /* If first line's physical ascent is larger than its logical
18555 ascent, use the physical ascent, and make the row taller.
18556 This makes accented characters fully visible. */
18557 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18558 && row->phys_ascent > row->ascent)
18559 {
18560 row->height += row->phys_ascent - row->ascent;
18561 row->ascent = row->phys_ascent;
18562 }
18563
18564 /* Compute how much of the line is visible. */
18565 row->visible_height = row->height;
18566
18567 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18568 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18569
18570 if (row->y < min_y)
18571 row->visible_height -= min_y - row->y;
18572 if (row->y + row->height > max_y)
18573 row->visible_height -= row->y + row->height - max_y;
18574 }
18575 else
18576 {
18577 row->pixel_width = row->used[TEXT_AREA];
18578 if (row->continued_p)
18579 row->pixel_width -= it->continuation_pixel_width;
18580 else if (row->truncated_on_right_p)
18581 row->pixel_width -= it->truncation_pixel_width;
18582 row->ascent = row->phys_ascent = 0;
18583 row->height = row->phys_height = row->visible_height = 1;
18584 row->extra_line_spacing = 0;
18585 }
18586
18587 /* Compute a hash code for this row. */
18588 row->hash = row_hash (row);
18589
18590 it->max_ascent = it->max_descent = 0;
18591 it->max_phys_ascent = it->max_phys_descent = 0;
18592 }
18593
18594
18595 /* Append one space to the glyph row of iterator IT if doing a
18596 window-based redisplay. The space has the same face as
18597 IT->face_id. Value is non-zero if a space was added.
18598
18599 This function is called to make sure that there is always one glyph
18600 at the end of a glyph row that the cursor can be set on under
18601 window-systems. (If there weren't such a glyph we would not know
18602 how wide and tall a box cursor should be displayed).
18603
18604 At the same time this space let's a nicely handle clearing to the
18605 end of the line if the row ends in italic text. */
18606
18607 static int
18608 append_space_for_newline (struct it *it, int default_face_p)
18609 {
18610 if (FRAME_WINDOW_P (it->f))
18611 {
18612 int n = it->glyph_row->used[TEXT_AREA];
18613
18614 if (it->glyph_row->glyphs[TEXT_AREA] + n
18615 < it->glyph_row->glyphs[1 + TEXT_AREA])
18616 {
18617 /* Save some values that must not be changed.
18618 Must save IT->c and IT->len because otherwise
18619 ITERATOR_AT_END_P wouldn't work anymore after
18620 append_space_for_newline has been called. */
18621 enum display_element_type saved_what = it->what;
18622 int saved_c = it->c, saved_len = it->len;
18623 int saved_char_to_display = it->char_to_display;
18624 int saved_x = it->current_x;
18625 int saved_face_id = it->face_id;
18626 int saved_box_end = it->end_of_box_run_p;
18627 struct text_pos saved_pos;
18628 Lisp_Object saved_object;
18629 struct face *face;
18630
18631 saved_object = it->object;
18632 saved_pos = it->position;
18633
18634 it->what = IT_CHARACTER;
18635 memset (&it->position, 0, sizeof it->position);
18636 it->object = make_number (0);
18637 it->c = it->char_to_display = ' ';
18638 it->len = 1;
18639
18640 /* If the default face was remapped, be sure to use the
18641 remapped face for the appended newline. */
18642 if (default_face_p)
18643 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18644 else if (it->face_before_selective_p)
18645 it->face_id = it->saved_face_id;
18646 face = FACE_FROM_ID (it->f, it->face_id);
18647 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18648 /* In R2L rows, we will prepend a stretch glyph that will
18649 have the end_of_box_run_p flag set for it, so there's no
18650 need for the appended newline glyph to have that flag
18651 set. */
18652 if (it->glyph_row->reversed_p
18653 /* But if the appended newline glyph goes all the way to
18654 the end of the row, there will be no stretch glyph,
18655 so leave the box flag set. */
18656 && saved_x + FRAME_COLUMN_WIDTH (it->f) < it->last_visible_x)
18657 it->end_of_box_run_p = 0;
18658
18659 PRODUCE_GLYPHS (it);
18660
18661 it->override_ascent = -1;
18662 it->constrain_row_ascent_descent_p = 0;
18663 it->current_x = saved_x;
18664 it->object = saved_object;
18665 it->position = saved_pos;
18666 it->what = saved_what;
18667 it->face_id = saved_face_id;
18668 it->len = saved_len;
18669 it->c = saved_c;
18670 it->char_to_display = saved_char_to_display;
18671 it->end_of_box_run_p = saved_box_end;
18672 return 1;
18673 }
18674 }
18675
18676 return 0;
18677 }
18678
18679
18680 /* Extend the face of the last glyph in the text area of IT->glyph_row
18681 to the end of the display line. Called from display_line. If the
18682 glyph row is empty, add a space glyph to it so that we know the
18683 face to draw. Set the glyph row flag fill_line_p. If the glyph
18684 row is R2L, prepend a stretch glyph to cover the empty space to the
18685 left of the leftmost glyph. */
18686
18687 static void
18688 extend_face_to_end_of_line (struct it *it)
18689 {
18690 struct face *face, *default_face;
18691 struct frame *f = it->f;
18692
18693 /* If line is already filled, do nothing. Non window-system frames
18694 get a grace of one more ``pixel'' because their characters are
18695 1-``pixel'' wide, so they hit the equality too early. This grace
18696 is needed only for R2L rows that are not continued, to produce
18697 one extra blank where we could display the cursor. */
18698 if (it->current_x >= it->last_visible_x
18699 + (!FRAME_WINDOW_P (f)
18700 && it->glyph_row->reversed_p
18701 && !it->glyph_row->continued_p))
18702 return;
18703
18704 /* The default face, possibly remapped. */
18705 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18706
18707 /* Face extension extends the background and box of IT->face_id
18708 to the end of the line. If the background equals the background
18709 of the frame, we don't have to do anything. */
18710 if (it->face_before_selective_p)
18711 face = FACE_FROM_ID (f, it->saved_face_id);
18712 else
18713 face = FACE_FROM_ID (f, it->face_id);
18714
18715 if (FRAME_WINDOW_P (f)
18716 && it->glyph_row->displays_text_p
18717 && face->box == FACE_NO_BOX
18718 && face->background == FRAME_BACKGROUND_PIXEL (f)
18719 && !face->stipple
18720 && !it->glyph_row->reversed_p)
18721 return;
18722
18723 /* Set the glyph row flag indicating that the face of the last glyph
18724 in the text area has to be drawn to the end of the text area. */
18725 it->glyph_row->fill_line_p = 1;
18726
18727 /* If current character of IT is not ASCII, make sure we have the
18728 ASCII face. This will be automatically undone the next time
18729 get_next_display_element returns a multibyte character. Note
18730 that the character will always be single byte in unibyte
18731 text. */
18732 if (!ASCII_CHAR_P (it->c))
18733 {
18734 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18735 }
18736
18737 if (FRAME_WINDOW_P (f))
18738 {
18739 /* If the row is empty, add a space with the current face of IT,
18740 so that we know which face to draw. */
18741 if (it->glyph_row->used[TEXT_AREA] == 0)
18742 {
18743 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18744 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18745 it->glyph_row->used[TEXT_AREA] = 1;
18746 }
18747 #ifdef HAVE_WINDOW_SYSTEM
18748 if (it->glyph_row->reversed_p)
18749 {
18750 /* Prepend a stretch glyph to the row, such that the
18751 rightmost glyph will be drawn flushed all the way to the
18752 right margin of the window. The stretch glyph that will
18753 occupy the empty space, if any, to the left of the
18754 glyphs. */
18755 struct font *font = face->font ? face->font : FRAME_FONT (f);
18756 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18757 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18758 struct glyph *g;
18759 int row_width, stretch_ascent, stretch_width;
18760 struct text_pos saved_pos;
18761 int saved_face_id, saved_avoid_cursor, saved_box_start;
18762
18763 for (row_width = 0, g = row_start; g < row_end; g++)
18764 row_width += g->pixel_width;
18765 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18766 if (stretch_width > 0)
18767 {
18768 stretch_ascent =
18769 (((it->ascent + it->descent)
18770 * FONT_BASE (font)) / FONT_HEIGHT (font));
18771 saved_pos = it->position;
18772 memset (&it->position, 0, sizeof it->position);
18773 saved_avoid_cursor = it->avoid_cursor_p;
18774 it->avoid_cursor_p = 1;
18775 saved_face_id = it->face_id;
18776 saved_box_start = it->start_of_box_run_p;
18777 /* The last row's stretch glyph should get the default
18778 face, to avoid painting the rest of the window with
18779 the region face, if the region ends at ZV. */
18780 if (it->glyph_row->ends_at_zv_p)
18781 it->face_id = default_face->id;
18782 else
18783 it->face_id = face->id;
18784 it->start_of_box_run_p = 0;
18785 append_stretch_glyph (it, make_number (0), stretch_width,
18786 it->ascent + it->descent, stretch_ascent);
18787 it->position = saved_pos;
18788 it->avoid_cursor_p = saved_avoid_cursor;
18789 it->face_id = saved_face_id;
18790 it->start_of_box_run_p = saved_box_start;
18791 }
18792 }
18793 #endif /* HAVE_WINDOW_SYSTEM */
18794 }
18795 else
18796 {
18797 /* Save some values that must not be changed. */
18798 int saved_x = it->current_x;
18799 struct text_pos saved_pos;
18800 Lisp_Object saved_object;
18801 enum display_element_type saved_what = it->what;
18802 int saved_face_id = it->face_id;
18803
18804 saved_object = it->object;
18805 saved_pos = it->position;
18806
18807 it->what = IT_CHARACTER;
18808 memset (&it->position, 0, sizeof it->position);
18809 it->object = make_number (0);
18810 it->c = it->char_to_display = ' ';
18811 it->len = 1;
18812 /* The last row's blank glyphs should get the default face, to
18813 avoid painting the rest of the window with the region face,
18814 if the region ends at ZV. */
18815 if (it->glyph_row->ends_at_zv_p)
18816 it->face_id = default_face->id;
18817 else
18818 it->face_id = face->id;
18819
18820 PRODUCE_GLYPHS (it);
18821
18822 while (it->current_x <= it->last_visible_x)
18823 PRODUCE_GLYPHS (it);
18824
18825 /* Don't count these blanks really. It would let us insert a left
18826 truncation glyph below and make us set the cursor on them, maybe. */
18827 it->current_x = saved_x;
18828 it->object = saved_object;
18829 it->position = saved_pos;
18830 it->what = saved_what;
18831 it->face_id = saved_face_id;
18832 }
18833 }
18834
18835
18836 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18837 trailing whitespace. */
18838
18839 static int
18840 trailing_whitespace_p (ptrdiff_t charpos)
18841 {
18842 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18843 int c = 0;
18844
18845 while (bytepos < ZV_BYTE
18846 && (c = FETCH_CHAR (bytepos),
18847 c == ' ' || c == '\t'))
18848 ++bytepos;
18849
18850 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18851 {
18852 if (bytepos != PT_BYTE)
18853 return 1;
18854 }
18855 return 0;
18856 }
18857
18858
18859 /* Highlight trailing whitespace, if any, in ROW. */
18860
18861 static void
18862 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18863 {
18864 int used = row->used[TEXT_AREA];
18865
18866 if (used)
18867 {
18868 struct glyph *start = row->glyphs[TEXT_AREA];
18869 struct glyph *glyph = start + used - 1;
18870
18871 if (row->reversed_p)
18872 {
18873 /* Right-to-left rows need to be processed in the opposite
18874 direction, so swap the edge pointers. */
18875 glyph = start;
18876 start = row->glyphs[TEXT_AREA] + used - 1;
18877 }
18878
18879 /* Skip over glyphs inserted to display the cursor at the
18880 end of a line, for extending the face of the last glyph
18881 to the end of the line on terminals, and for truncation
18882 and continuation glyphs. */
18883 if (!row->reversed_p)
18884 {
18885 while (glyph >= start
18886 && glyph->type == CHAR_GLYPH
18887 && INTEGERP (glyph->object))
18888 --glyph;
18889 }
18890 else
18891 {
18892 while (glyph <= start
18893 && glyph->type == CHAR_GLYPH
18894 && INTEGERP (glyph->object))
18895 ++glyph;
18896 }
18897
18898 /* If last glyph is a space or stretch, and it's trailing
18899 whitespace, set the face of all trailing whitespace glyphs in
18900 IT->glyph_row to `trailing-whitespace'. */
18901 if ((row->reversed_p ? glyph <= start : glyph >= start)
18902 && BUFFERP (glyph->object)
18903 && (glyph->type == STRETCH_GLYPH
18904 || (glyph->type == CHAR_GLYPH
18905 && glyph->u.ch == ' '))
18906 && trailing_whitespace_p (glyph->charpos))
18907 {
18908 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18909 if (face_id < 0)
18910 return;
18911
18912 if (!row->reversed_p)
18913 {
18914 while (glyph >= start
18915 && BUFFERP (glyph->object)
18916 && (glyph->type == STRETCH_GLYPH
18917 || (glyph->type == CHAR_GLYPH
18918 && glyph->u.ch == ' ')))
18919 (glyph--)->face_id = face_id;
18920 }
18921 else
18922 {
18923 while (glyph <= start
18924 && BUFFERP (glyph->object)
18925 && (glyph->type == STRETCH_GLYPH
18926 || (glyph->type == CHAR_GLYPH
18927 && glyph->u.ch == ' ')))
18928 (glyph++)->face_id = face_id;
18929 }
18930 }
18931 }
18932 }
18933
18934
18935 /* Value is non-zero if glyph row ROW should be
18936 used to hold the cursor. */
18937
18938 static int
18939 cursor_row_p (struct glyph_row *row)
18940 {
18941 int result = 1;
18942
18943 if (PT == CHARPOS (row->end.pos)
18944 || PT == MATRIX_ROW_END_CHARPOS (row))
18945 {
18946 /* Suppose the row ends on a string.
18947 Unless the row is continued, that means it ends on a newline
18948 in the string. If it's anything other than a display string
18949 (e.g., a before-string from an overlay), we don't want the
18950 cursor there. (This heuristic seems to give the optimal
18951 behavior for the various types of multi-line strings.)
18952 One exception: if the string has `cursor' property on one of
18953 its characters, we _do_ want the cursor there. */
18954 if (CHARPOS (row->end.string_pos) >= 0)
18955 {
18956 if (row->continued_p)
18957 result = 1;
18958 else
18959 {
18960 /* Check for `display' property. */
18961 struct glyph *beg = row->glyphs[TEXT_AREA];
18962 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
18963 struct glyph *glyph;
18964
18965 result = 0;
18966 for (glyph = end; glyph >= beg; --glyph)
18967 if (STRINGP (glyph->object))
18968 {
18969 Lisp_Object prop
18970 = Fget_char_property (make_number (PT),
18971 Qdisplay, Qnil);
18972 result =
18973 (!NILP (prop)
18974 && display_prop_string_p (prop, glyph->object));
18975 /* If there's a `cursor' property on one of the
18976 string's characters, this row is a cursor row,
18977 even though this is not a display string. */
18978 if (!result)
18979 {
18980 Lisp_Object s = glyph->object;
18981
18982 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
18983 {
18984 ptrdiff_t gpos = glyph->charpos;
18985
18986 if (!NILP (Fget_char_property (make_number (gpos),
18987 Qcursor, s)))
18988 {
18989 result = 1;
18990 break;
18991 }
18992 }
18993 }
18994 break;
18995 }
18996 }
18997 }
18998 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
18999 {
19000 /* If the row ends in middle of a real character,
19001 and the line is continued, we want the cursor here.
19002 That's because CHARPOS (ROW->end.pos) would equal
19003 PT if PT is before the character. */
19004 if (!row->ends_in_ellipsis_p)
19005 result = row->continued_p;
19006 else
19007 /* If the row ends in an ellipsis, then
19008 CHARPOS (ROW->end.pos) will equal point after the
19009 invisible text. We want that position to be displayed
19010 after the ellipsis. */
19011 result = 0;
19012 }
19013 /* If the row ends at ZV, display the cursor at the end of that
19014 row instead of at the start of the row below. */
19015 else if (row->ends_at_zv_p)
19016 result = 1;
19017 else
19018 result = 0;
19019 }
19020
19021 return result;
19022 }
19023
19024 \f
19025
19026 /* Push the property PROP so that it will be rendered at the current
19027 position in IT. Return 1 if PROP was successfully pushed, 0
19028 otherwise. Called from handle_line_prefix to handle the
19029 `line-prefix' and `wrap-prefix' properties. */
19030
19031 static int
19032 push_prefix_prop (struct it *it, Lisp_Object prop)
19033 {
19034 struct text_pos pos =
19035 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19036
19037 eassert (it->method == GET_FROM_BUFFER
19038 || it->method == GET_FROM_DISPLAY_VECTOR
19039 || it->method == GET_FROM_STRING);
19040
19041 /* We need to save the current buffer/string position, so it will be
19042 restored by pop_it, because iterate_out_of_display_property
19043 depends on that being set correctly, but some situations leave
19044 it->position not yet set when this function is called. */
19045 push_it (it, &pos);
19046
19047 if (STRINGP (prop))
19048 {
19049 if (SCHARS (prop) == 0)
19050 {
19051 pop_it (it);
19052 return 0;
19053 }
19054
19055 it->string = prop;
19056 it->string_from_prefix_prop_p = 1;
19057 it->multibyte_p = STRING_MULTIBYTE (it->string);
19058 it->current.overlay_string_index = -1;
19059 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19060 it->end_charpos = it->string_nchars = SCHARS (it->string);
19061 it->method = GET_FROM_STRING;
19062 it->stop_charpos = 0;
19063 it->prev_stop = 0;
19064 it->base_level_stop = 0;
19065
19066 /* Force paragraph direction to be that of the parent
19067 buffer/string. */
19068 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19069 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19070 else
19071 it->paragraph_embedding = L2R;
19072
19073 /* Set up the bidi iterator for this display string. */
19074 if (it->bidi_p)
19075 {
19076 it->bidi_it.string.lstring = it->string;
19077 it->bidi_it.string.s = NULL;
19078 it->bidi_it.string.schars = it->end_charpos;
19079 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19080 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19081 it->bidi_it.string.unibyte = !it->multibyte_p;
19082 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19083 }
19084 }
19085 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19086 {
19087 it->method = GET_FROM_STRETCH;
19088 it->object = prop;
19089 }
19090 #ifdef HAVE_WINDOW_SYSTEM
19091 else if (IMAGEP (prop))
19092 {
19093 it->what = IT_IMAGE;
19094 it->image_id = lookup_image (it->f, prop);
19095 it->method = GET_FROM_IMAGE;
19096 }
19097 #endif /* HAVE_WINDOW_SYSTEM */
19098 else
19099 {
19100 pop_it (it); /* bogus display property, give up */
19101 return 0;
19102 }
19103
19104 return 1;
19105 }
19106
19107 /* Return the character-property PROP at the current position in IT. */
19108
19109 static Lisp_Object
19110 get_it_property (struct it *it, Lisp_Object prop)
19111 {
19112 Lisp_Object position;
19113
19114 if (STRINGP (it->object))
19115 position = make_number (IT_STRING_CHARPOS (*it));
19116 else if (BUFFERP (it->object))
19117 position = make_number (IT_CHARPOS (*it));
19118 else
19119 return Qnil;
19120
19121 return Fget_char_property (position, prop, it->object);
19122 }
19123
19124 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19125
19126 static void
19127 handle_line_prefix (struct it *it)
19128 {
19129 Lisp_Object prefix;
19130
19131 if (it->continuation_lines_width > 0)
19132 {
19133 prefix = get_it_property (it, Qwrap_prefix);
19134 if (NILP (prefix))
19135 prefix = Vwrap_prefix;
19136 }
19137 else
19138 {
19139 prefix = get_it_property (it, Qline_prefix);
19140 if (NILP (prefix))
19141 prefix = Vline_prefix;
19142 }
19143 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19144 {
19145 /* If the prefix is wider than the window, and we try to wrap
19146 it, it would acquire its own wrap prefix, and so on till the
19147 iterator stack overflows. So, don't wrap the prefix. */
19148 it->line_wrap = TRUNCATE;
19149 it->avoid_cursor_p = 1;
19150 }
19151 }
19152
19153 \f
19154
19155 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19156 only for R2L lines from display_line and display_string, when they
19157 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19158 the line/string needs to be continued on the next glyph row. */
19159 static void
19160 unproduce_glyphs (struct it *it, int n)
19161 {
19162 struct glyph *glyph, *end;
19163
19164 eassert (it->glyph_row);
19165 eassert (it->glyph_row->reversed_p);
19166 eassert (it->area == TEXT_AREA);
19167 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19168
19169 if (n > it->glyph_row->used[TEXT_AREA])
19170 n = it->glyph_row->used[TEXT_AREA];
19171 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19172 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19173 for ( ; glyph < end; glyph++)
19174 glyph[-n] = *glyph;
19175 }
19176
19177 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19178 and ROW->maxpos. */
19179 static void
19180 find_row_edges (struct it *it, struct glyph_row *row,
19181 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19182 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19183 {
19184 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19185 lines' rows is implemented for bidi-reordered rows. */
19186
19187 /* ROW->minpos is the value of min_pos, the minimal buffer position
19188 we have in ROW, or ROW->start.pos if that is smaller. */
19189 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19190 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19191 else
19192 /* We didn't find buffer positions smaller than ROW->start, or
19193 didn't find _any_ valid buffer positions in any of the glyphs,
19194 so we must trust the iterator's computed positions. */
19195 row->minpos = row->start.pos;
19196 if (max_pos <= 0)
19197 {
19198 max_pos = CHARPOS (it->current.pos);
19199 max_bpos = BYTEPOS (it->current.pos);
19200 }
19201
19202 /* Here are the various use-cases for ending the row, and the
19203 corresponding values for ROW->maxpos:
19204
19205 Line ends in a newline from buffer eol_pos + 1
19206 Line is continued from buffer max_pos + 1
19207 Line is truncated on right it->current.pos
19208 Line ends in a newline from string max_pos + 1(*)
19209 (*) + 1 only when line ends in a forward scan
19210 Line is continued from string max_pos
19211 Line is continued from display vector max_pos
19212 Line is entirely from a string min_pos == max_pos
19213 Line is entirely from a display vector min_pos == max_pos
19214 Line that ends at ZV ZV
19215
19216 If you discover other use-cases, please add them here as
19217 appropriate. */
19218 if (row->ends_at_zv_p)
19219 row->maxpos = it->current.pos;
19220 else if (row->used[TEXT_AREA])
19221 {
19222 int seen_this_string = 0;
19223 struct glyph_row *r1 = row - 1;
19224
19225 /* Did we see the same display string on the previous row? */
19226 if (STRINGP (it->object)
19227 /* this is not the first row */
19228 && row > it->w->desired_matrix->rows
19229 /* previous row is not the header line */
19230 && !r1->mode_line_p
19231 /* previous row also ends in a newline from a string */
19232 && r1->ends_in_newline_from_string_p)
19233 {
19234 struct glyph *start, *end;
19235
19236 /* Search for the last glyph of the previous row that came
19237 from buffer or string. Depending on whether the row is
19238 L2R or R2L, we need to process it front to back or the
19239 other way round. */
19240 if (!r1->reversed_p)
19241 {
19242 start = r1->glyphs[TEXT_AREA];
19243 end = start + r1->used[TEXT_AREA];
19244 /* Glyphs inserted by redisplay have an integer (zero)
19245 as their object. */
19246 while (end > start
19247 && INTEGERP ((end - 1)->object)
19248 && (end - 1)->charpos <= 0)
19249 --end;
19250 if (end > start)
19251 {
19252 if (EQ ((end - 1)->object, it->object))
19253 seen_this_string = 1;
19254 }
19255 else
19256 /* If all the glyphs of the previous row were inserted
19257 by redisplay, it means the previous row was
19258 produced from a single newline, which is only
19259 possible if that newline came from the same string
19260 as the one which produced this ROW. */
19261 seen_this_string = 1;
19262 }
19263 else
19264 {
19265 end = r1->glyphs[TEXT_AREA] - 1;
19266 start = end + r1->used[TEXT_AREA];
19267 while (end < start
19268 && INTEGERP ((end + 1)->object)
19269 && (end + 1)->charpos <= 0)
19270 ++end;
19271 if (end < start)
19272 {
19273 if (EQ ((end + 1)->object, it->object))
19274 seen_this_string = 1;
19275 }
19276 else
19277 seen_this_string = 1;
19278 }
19279 }
19280 /* Take note of each display string that covers a newline only
19281 once, the first time we see it. This is for when a display
19282 string includes more than one newline in it. */
19283 if (row->ends_in_newline_from_string_p && !seen_this_string)
19284 {
19285 /* If we were scanning the buffer forward when we displayed
19286 the string, we want to account for at least one buffer
19287 position that belongs to this row (position covered by
19288 the display string), so that cursor positioning will
19289 consider this row as a candidate when point is at the end
19290 of the visual line represented by this row. This is not
19291 required when scanning back, because max_pos will already
19292 have a much larger value. */
19293 if (CHARPOS (row->end.pos) > max_pos)
19294 INC_BOTH (max_pos, max_bpos);
19295 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19296 }
19297 else if (CHARPOS (it->eol_pos) > 0)
19298 SET_TEXT_POS (row->maxpos,
19299 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19300 else if (row->continued_p)
19301 {
19302 /* If max_pos is different from IT's current position, it
19303 means IT->method does not belong to the display element
19304 at max_pos. However, it also means that the display
19305 element at max_pos was displayed in its entirety on this
19306 line, which is equivalent to saying that the next line
19307 starts at the next buffer position. */
19308 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19309 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19310 else
19311 {
19312 INC_BOTH (max_pos, max_bpos);
19313 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19314 }
19315 }
19316 else if (row->truncated_on_right_p)
19317 /* display_line already called reseat_at_next_visible_line_start,
19318 which puts the iterator at the beginning of the next line, in
19319 the logical order. */
19320 row->maxpos = it->current.pos;
19321 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19322 /* A line that is entirely from a string/image/stretch... */
19323 row->maxpos = row->minpos;
19324 else
19325 emacs_abort ();
19326 }
19327 else
19328 row->maxpos = it->current.pos;
19329 }
19330
19331 /* Construct the glyph row IT->glyph_row in the desired matrix of
19332 IT->w from text at the current position of IT. See dispextern.h
19333 for an overview of struct it. Value is non-zero if
19334 IT->glyph_row displays text, as opposed to a line displaying ZV
19335 only. */
19336
19337 static int
19338 display_line (struct it *it)
19339 {
19340 struct glyph_row *row = it->glyph_row;
19341 Lisp_Object overlay_arrow_string;
19342 struct it wrap_it;
19343 void *wrap_data = NULL;
19344 int may_wrap = 0, wrap_x IF_LINT (= 0);
19345 int wrap_row_used = -1;
19346 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19347 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19348 int wrap_row_extra_line_spacing IF_LINT (= 0);
19349 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19350 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19351 int cvpos;
19352 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19353 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19354
19355 /* We always start displaying at hpos zero even if hscrolled. */
19356 eassert (it->hpos == 0 && it->current_x == 0);
19357
19358 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19359 >= it->w->desired_matrix->nrows)
19360 {
19361 it->w->nrows_scale_factor++;
19362 fonts_changed_p = 1;
19363 return 0;
19364 }
19365
19366 /* Is IT->w showing the region? */
19367 wset_region_showing (it->w, it->region_beg_charpos > 0 ? Qt : Qnil);
19368
19369 /* Clear the result glyph row and enable it. */
19370 prepare_desired_row (row);
19371
19372 row->y = it->current_y;
19373 row->start = it->start;
19374 row->continuation_lines_width = it->continuation_lines_width;
19375 row->displays_text_p = 1;
19376 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19377 it->starts_in_middle_of_char_p = 0;
19378
19379 /* Arrange the overlays nicely for our purposes. Usually, we call
19380 display_line on only one line at a time, in which case this
19381 can't really hurt too much, or we call it on lines which appear
19382 one after another in the buffer, in which case all calls to
19383 recenter_overlay_lists but the first will be pretty cheap. */
19384 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19385
19386 /* Move over display elements that are not visible because we are
19387 hscrolled. This may stop at an x-position < IT->first_visible_x
19388 if the first glyph is partially visible or if we hit a line end. */
19389 if (it->current_x < it->first_visible_x)
19390 {
19391 enum move_it_result move_result;
19392
19393 this_line_min_pos = row->start.pos;
19394 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19395 MOVE_TO_POS | MOVE_TO_X);
19396 /* If we are under a large hscroll, move_it_in_display_line_to
19397 could hit the end of the line without reaching
19398 it->first_visible_x. Pretend that we did reach it. This is
19399 especially important on a TTY, where we will call
19400 extend_face_to_end_of_line, which needs to know how many
19401 blank glyphs to produce. */
19402 if (it->current_x < it->first_visible_x
19403 && (move_result == MOVE_NEWLINE_OR_CR
19404 || move_result == MOVE_POS_MATCH_OR_ZV))
19405 it->current_x = it->first_visible_x;
19406
19407 /* Record the smallest positions seen while we moved over
19408 display elements that are not visible. This is needed by
19409 redisplay_internal for optimizing the case where the cursor
19410 stays inside the same line. The rest of this function only
19411 considers positions that are actually displayed, so
19412 RECORD_MAX_MIN_POS will not otherwise record positions that
19413 are hscrolled to the left of the left edge of the window. */
19414 min_pos = CHARPOS (this_line_min_pos);
19415 min_bpos = BYTEPOS (this_line_min_pos);
19416 }
19417 else
19418 {
19419 /* We only do this when not calling `move_it_in_display_line_to'
19420 above, because move_it_in_display_line_to calls
19421 handle_line_prefix itself. */
19422 handle_line_prefix (it);
19423 }
19424
19425 /* Get the initial row height. This is either the height of the
19426 text hscrolled, if there is any, or zero. */
19427 row->ascent = it->max_ascent;
19428 row->height = it->max_ascent + it->max_descent;
19429 row->phys_ascent = it->max_phys_ascent;
19430 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19431 row->extra_line_spacing = it->max_extra_line_spacing;
19432
19433 /* Utility macro to record max and min buffer positions seen until now. */
19434 #define RECORD_MAX_MIN_POS(IT) \
19435 do \
19436 { \
19437 int composition_p = !STRINGP ((IT)->string) \
19438 && ((IT)->what == IT_COMPOSITION); \
19439 ptrdiff_t current_pos = \
19440 composition_p ? (IT)->cmp_it.charpos \
19441 : IT_CHARPOS (*(IT)); \
19442 ptrdiff_t current_bpos = \
19443 composition_p ? CHAR_TO_BYTE (current_pos) \
19444 : IT_BYTEPOS (*(IT)); \
19445 if (current_pos < min_pos) \
19446 { \
19447 min_pos = current_pos; \
19448 min_bpos = current_bpos; \
19449 } \
19450 if (IT_CHARPOS (*it) > max_pos) \
19451 { \
19452 max_pos = IT_CHARPOS (*it); \
19453 max_bpos = IT_BYTEPOS (*it); \
19454 } \
19455 } \
19456 while (0)
19457
19458 /* Loop generating characters. The loop is left with IT on the next
19459 character to display. */
19460 while (1)
19461 {
19462 int n_glyphs_before, hpos_before, x_before;
19463 int x, nglyphs;
19464 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19465
19466 /* Retrieve the next thing to display. Value is zero if end of
19467 buffer reached. */
19468 if (!get_next_display_element (it))
19469 {
19470 /* Maybe add a space at the end of this line that is used to
19471 display the cursor there under X. Set the charpos of the
19472 first glyph of blank lines not corresponding to any text
19473 to -1. */
19474 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19475 row->exact_window_width_line_p = 1;
19476 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19477 || row->used[TEXT_AREA] == 0)
19478 {
19479 row->glyphs[TEXT_AREA]->charpos = -1;
19480 row->displays_text_p = 0;
19481
19482 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19483 && (!MINI_WINDOW_P (it->w)
19484 || (minibuf_level && EQ (it->window, minibuf_window))))
19485 row->indicate_empty_line_p = 1;
19486 }
19487
19488 it->continuation_lines_width = 0;
19489 row->ends_at_zv_p = 1;
19490 /* A row that displays right-to-left text must always have
19491 its last face extended all the way to the end of line,
19492 even if this row ends in ZV, because we still write to
19493 the screen left to right. We also need to extend the
19494 last face if the default face is remapped to some
19495 different face, otherwise the functions that clear
19496 portions of the screen will clear with the default face's
19497 background color. */
19498 if (row->reversed_p
19499 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19500 extend_face_to_end_of_line (it);
19501 break;
19502 }
19503
19504 /* Now, get the metrics of what we want to display. This also
19505 generates glyphs in `row' (which is IT->glyph_row). */
19506 n_glyphs_before = row->used[TEXT_AREA];
19507 x = it->current_x;
19508
19509 /* Remember the line height so far in case the next element doesn't
19510 fit on the line. */
19511 if (it->line_wrap != TRUNCATE)
19512 {
19513 ascent = it->max_ascent;
19514 descent = it->max_descent;
19515 phys_ascent = it->max_phys_ascent;
19516 phys_descent = it->max_phys_descent;
19517
19518 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19519 {
19520 if (IT_DISPLAYING_WHITESPACE (it))
19521 may_wrap = 1;
19522 else if (may_wrap)
19523 {
19524 SAVE_IT (wrap_it, *it, wrap_data);
19525 wrap_x = x;
19526 wrap_row_used = row->used[TEXT_AREA];
19527 wrap_row_ascent = row->ascent;
19528 wrap_row_height = row->height;
19529 wrap_row_phys_ascent = row->phys_ascent;
19530 wrap_row_phys_height = row->phys_height;
19531 wrap_row_extra_line_spacing = row->extra_line_spacing;
19532 wrap_row_min_pos = min_pos;
19533 wrap_row_min_bpos = min_bpos;
19534 wrap_row_max_pos = max_pos;
19535 wrap_row_max_bpos = max_bpos;
19536 may_wrap = 0;
19537 }
19538 }
19539 }
19540
19541 PRODUCE_GLYPHS (it);
19542
19543 /* If this display element was in marginal areas, continue with
19544 the next one. */
19545 if (it->area != TEXT_AREA)
19546 {
19547 row->ascent = max (row->ascent, it->max_ascent);
19548 row->height = max (row->height, it->max_ascent + it->max_descent);
19549 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19550 row->phys_height = max (row->phys_height,
19551 it->max_phys_ascent + it->max_phys_descent);
19552 row->extra_line_spacing = max (row->extra_line_spacing,
19553 it->max_extra_line_spacing);
19554 set_iterator_to_next (it, 1);
19555 continue;
19556 }
19557
19558 /* Does the display element fit on the line? If we truncate
19559 lines, we should draw past the right edge of the window. If
19560 we don't truncate, we want to stop so that we can display the
19561 continuation glyph before the right margin. If lines are
19562 continued, there are two possible strategies for characters
19563 resulting in more than 1 glyph (e.g. tabs): Display as many
19564 glyphs as possible in this line and leave the rest for the
19565 continuation line, or display the whole element in the next
19566 line. Original redisplay did the former, so we do it also. */
19567 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19568 hpos_before = it->hpos;
19569 x_before = x;
19570
19571 if (/* Not a newline. */
19572 nglyphs > 0
19573 /* Glyphs produced fit entirely in the line. */
19574 && it->current_x < it->last_visible_x)
19575 {
19576 it->hpos += nglyphs;
19577 row->ascent = max (row->ascent, it->max_ascent);
19578 row->height = max (row->height, it->max_ascent + it->max_descent);
19579 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19580 row->phys_height = max (row->phys_height,
19581 it->max_phys_ascent + it->max_phys_descent);
19582 row->extra_line_spacing = max (row->extra_line_spacing,
19583 it->max_extra_line_spacing);
19584 if (it->current_x - it->pixel_width < it->first_visible_x)
19585 row->x = x - it->first_visible_x;
19586 /* Record the maximum and minimum buffer positions seen so
19587 far in glyphs that will be displayed by this row. */
19588 if (it->bidi_p)
19589 RECORD_MAX_MIN_POS (it);
19590 }
19591 else
19592 {
19593 int i, new_x;
19594 struct glyph *glyph;
19595
19596 for (i = 0; i < nglyphs; ++i, x = new_x)
19597 {
19598 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19599 new_x = x + glyph->pixel_width;
19600
19601 if (/* Lines are continued. */
19602 it->line_wrap != TRUNCATE
19603 && (/* Glyph doesn't fit on the line. */
19604 new_x > it->last_visible_x
19605 /* Or it fits exactly on a window system frame. */
19606 || (new_x == it->last_visible_x
19607 && FRAME_WINDOW_P (it->f)
19608 && (row->reversed_p
19609 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19610 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19611 {
19612 /* End of a continued line. */
19613
19614 if (it->hpos == 0
19615 || (new_x == it->last_visible_x
19616 && FRAME_WINDOW_P (it->f)
19617 && (row->reversed_p
19618 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19619 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19620 {
19621 /* Current glyph is the only one on the line or
19622 fits exactly on the line. We must continue
19623 the line because we can't draw the cursor
19624 after the glyph. */
19625 row->continued_p = 1;
19626 it->current_x = new_x;
19627 it->continuation_lines_width += new_x;
19628 ++it->hpos;
19629 if (i == nglyphs - 1)
19630 {
19631 /* If line-wrap is on, check if a previous
19632 wrap point was found. */
19633 if (wrap_row_used > 0
19634 /* Even if there is a previous wrap
19635 point, continue the line here as
19636 usual, if (i) the previous character
19637 was a space or tab AND (ii) the
19638 current character is not. */
19639 && (!may_wrap
19640 || IT_DISPLAYING_WHITESPACE (it)))
19641 goto back_to_wrap;
19642
19643 /* Record the maximum and minimum buffer
19644 positions seen so far in glyphs that will be
19645 displayed by this row. */
19646 if (it->bidi_p)
19647 RECORD_MAX_MIN_POS (it);
19648 set_iterator_to_next (it, 1);
19649 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19650 {
19651 if (!get_next_display_element (it))
19652 {
19653 row->exact_window_width_line_p = 1;
19654 it->continuation_lines_width = 0;
19655 row->continued_p = 0;
19656 row->ends_at_zv_p = 1;
19657 }
19658 else if (ITERATOR_AT_END_OF_LINE_P (it))
19659 {
19660 row->continued_p = 0;
19661 row->exact_window_width_line_p = 1;
19662 }
19663 }
19664 }
19665 else if (it->bidi_p)
19666 RECORD_MAX_MIN_POS (it);
19667 }
19668 else if (CHAR_GLYPH_PADDING_P (*glyph)
19669 && !FRAME_WINDOW_P (it->f))
19670 {
19671 /* A padding glyph that doesn't fit on this line.
19672 This means the whole character doesn't fit
19673 on the line. */
19674 if (row->reversed_p)
19675 unproduce_glyphs (it, row->used[TEXT_AREA]
19676 - n_glyphs_before);
19677 row->used[TEXT_AREA] = n_glyphs_before;
19678
19679 /* Fill the rest of the row with continuation
19680 glyphs like in 20.x. */
19681 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19682 < row->glyphs[1 + TEXT_AREA])
19683 produce_special_glyphs (it, IT_CONTINUATION);
19684
19685 row->continued_p = 1;
19686 it->current_x = x_before;
19687 it->continuation_lines_width += x_before;
19688
19689 /* Restore the height to what it was before the
19690 element not fitting on the line. */
19691 it->max_ascent = ascent;
19692 it->max_descent = descent;
19693 it->max_phys_ascent = phys_ascent;
19694 it->max_phys_descent = phys_descent;
19695 }
19696 else if (wrap_row_used > 0)
19697 {
19698 back_to_wrap:
19699 if (row->reversed_p)
19700 unproduce_glyphs (it,
19701 row->used[TEXT_AREA] - wrap_row_used);
19702 RESTORE_IT (it, &wrap_it, wrap_data);
19703 it->continuation_lines_width += wrap_x;
19704 row->used[TEXT_AREA] = wrap_row_used;
19705 row->ascent = wrap_row_ascent;
19706 row->height = wrap_row_height;
19707 row->phys_ascent = wrap_row_phys_ascent;
19708 row->phys_height = wrap_row_phys_height;
19709 row->extra_line_spacing = wrap_row_extra_line_spacing;
19710 min_pos = wrap_row_min_pos;
19711 min_bpos = wrap_row_min_bpos;
19712 max_pos = wrap_row_max_pos;
19713 max_bpos = wrap_row_max_bpos;
19714 row->continued_p = 1;
19715 row->ends_at_zv_p = 0;
19716 row->exact_window_width_line_p = 0;
19717 it->continuation_lines_width += x;
19718
19719 /* Make sure that a non-default face is extended
19720 up to the right margin of the window. */
19721 extend_face_to_end_of_line (it);
19722 }
19723 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19724 {
19725 /* A TAB that extends past the right edge of the
19726 window. This produces a single glyph on
19727 window system frames. We leave the glyph in
19728 this row and let it fill the row, but don't
19729 consume the TAB. */
19730 if ((row->reversed_p
19731 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19732 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19733 produce_special_glyphs (it, IT_CONTINUATION);
19734 it->continuation_lines_width += it->last_visible_x;
19735 row->ends_in_middle_of_char_p = 1;
19736 row->continued_p = 1;
19737 glyph->pixel_width = it->last_visible_x - x;
19738 it->starts_in_middle_of_char_p = 1;
19739 }
19740 else
19741 {
19742 /* Something other than a TAB that draws past
19743 the right edge of the window. Restore
19744 positions to values before the element. */
19745 if (row->reversed_p)
19746 unproduce_glyphs (it, row->used[TEXT_AREA]
19747 - (n_glyphs_before + i));
19748 row->used[TEXT_AREA] = n_glyphs_before + i;
19749
19750 /* Display continuation glyphs. */
19751 it->current_x = x_before;
19752 it->continuation_lines_width += x;
19753 if (!FRAME_WINDOW_P (it->f)
19754 || (row->reversed_p
19755 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19756 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19757 produce_special_glyphs (it, IT_CONTINUATION);
19758 row->continued_p = 1;
19759
19760 extend_face_to_end_of_line (it);
19761
19762 if (nglyphs > 1 && i > 0)
19763 {
19764 row->ends_in_middle_of_char_p = 1;
19765 it->starts_in_middle_of_char_p = 1;
19766 }
19767
19768 /* Restore the height to what it was before the
19769 element not fitting on the line. */
19770 it->max_ascent = ascent;
19771 it->max_descent = descent;
19772 it->max_phys_ascent = phys_ascent;
19773 it->max_phys_descent = phys_descent;
19774 }
19775
19776 break;
19777 }
19778 else if (new_x > it->first_visible_x)
19779 {
19780 /* Increment number of glyphs actually displayed. */
19781 ++it->hpos;
19782
19783 /* Record the maximum and minimum buffer positions
19784 seen so far in glyphs that will be displayed by
19785 this row. */
19786 if (it->bidi_p)
19787 RECORD_MAX_MIN_POS (it);
19788
19789 if (x < it->first_visible_x)
19790 /* Glyph is partially visible, i.e. row starts at
19791 negative X position. */
19792 row->x = x - it->first_visible_x;
19793 }
19794 else
19795 {
19796 /* Glyph is completely off the left margin of the
19797 window. This should not happen because of the
19798 move_it_in_display_line at the start of this
19799 function, unless the text display area of the
19800 window is empty. */
19801 eassert (it->first_visible_x <= it->last_visible_x);
19802 }
19803 }
19804 /* Even if this display element produced no glyphs at all,
19805 we want to record its position. */
19806 if (it->bidi_p && nglyphs == 0)
19807 RECORD_MAX_MIN_POS (it);
19808
19809 row->ascent = max (row->ascent, it->max_ascent);
19810 row->height = max (row->height, it->max_ascent + it->max_descent);
19811 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19812 row->phys_height = max (row->phys_height,
19813 it->max_phys_ascent + it->max_phys_descent);
19814 row->extra_line_spacing = max (row->extra_line_spacing,
19815 it->max_extra_line_spacing);
19816
19817 /* End of this display line if row is continued. */
19818 if (row->continued_p || row->ends_at_zv_p)
19819 break;
19820 }
19821
19822 at_end_of_line:
19823 /* Is this a line end? If yes, we're also done, after making
19824 sure that a non-default face is extended up to the right
19825 margin of the window. */
19826 if (ITERATOR_AT_END_OF_LINE_P (it))
19827 {
19828 int used_before = row->used[TEXT_AREA];
19829
19830 row->ends_in_newline_from_string_p = STRINGP (it->object);
19831
19832 /* Add a space at the end of the line that is used to
19833 display the cursor there. */
19834 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19835 append_space_for_newline (it, 0);
19836
19837 /* Extend the face to the end of the line. */
19838 extend_face_to_end_of_line (it);
19839
19840 /* Make sure we have the position. */
19841 if (used_before == 0)
19842 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19843
19844 /* Record the position of the newline, for use in
19845 find_row_edges. */
19846 it->eol_pos = it->current.pos;
19847
19848 /* Consume the line end. This skips over invisible lines. */
19849 set_iterator_to_next (it, 1);
19850 it->continuation_lines_width = 0;
19851 break;
19852 }
19853
19854 /* Proceed with next display element. Note that this skips
19855 over lines invisible because of selective display. */
19856 set_iterator_to_next (it, 1);
19857
19858 /* If we truncate lines, we are done when the last displayed
19859 glyphs reach past the right margin of the window. */
19860 if (it->line_wrap == TRUNCATE
19861 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19862 ? (it->current_x >= it->last_visible_x)
19863 : (it->current_x > it->last_visible_x)))
19864 {
19865 /* Maybe add truncation glyphs. */
19866 if (!FRAME_WINDOW_P (it->f)
19867 || (row->reversed_p
19868 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19869 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19870 {
19871 int i, n;
19872
19873 if (!row->reversed_p)
19874 {
19875 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19876 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19877 break;
19878 }
19879 else
19880 {
19881 for (i = 0; i < row->used[TEXT_AREA]; i++)
19882 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19883 break;
19884 /* Remove any padding glyphs at the front of ROW, to
19885 make room for the truncation glyphs we will be
19886 adding below. The loop below always inserts at
19887 least one truncation glyph, so also remove the
19888 last glyph added to ROW. */
19889 unproduce_glyphs (it, i + 1);
19890 /* Adjust i for the loop below. */
19891 i = row->used[TEXT_AREA] - (i + 1);
19892 }
19893
19894 it->current_x = x_before;
19895 if (!FRAME_WINDOW_P (it->f))
19896 {
19897 for (n = row->used[TEXT_AREA]; i < n; ++i)
19898 {
19899 row->used[TEXT_AREA] = i;
19900 produce_special_glyphs (it, IT_TRUNCATION);
19901 }
19902 }
19903 else
19904 {
19905 row->used[TEXT_AREA] = i;
19906 produce_special_glyphs (it, IT_TRUNCATION);
19907 }
19908 }
19909 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19910 {
19911 /* Don't truncate if we can overflow newline into fringe. */
19912 if (!get_next_display_element (it))
19913 {
19914 it->continuation_lines_width = 0;
19915 row->ends_at_zv_p = 1;
19916 row->exact_window_width_line_p = 1;
19917 break;
19918 }
19919 if (ITERATOR_AT_END_OF_LINE_P (it))
19920 {
19921 row->exact_window_width_line_p = 1;
19922 goto at_end_of_line;
19923 }
19924 it->current_x = x_before;
19925 }
19926
19927 row->truncated_on_right_p = 1;
19928 it->continuation_lines_width = 0;
19929 reseat_at_next_visible_line_start (it, 0);
19930 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
19931 it->hpos = hpos_before;
19932 break;
19933 }
19934 }
19935
19936 if (wrap_data)
19937 bidi_unshelve_cache (wrap_data, 1);
19938
19939 /* If line is not empty and hscrolled, maybe insert truncation glyphs
19940 at the left window margin. */
19941 if (it->first_visible_x
19942 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
19943 {
19944 if (!FRAME_WINDOW_P (it->f)
19945 || (row->reversed_p
19946 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19947 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
19948 insert_left_trunc_glyphs (it);
19949 row->truncated_on_left_p = 1;
19950 }
19951
19952 /* Remember the position at which this line ends.
19953
19954 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
19955 cannot be before the call to find_row_edges below, since that is
19956 where these positions are determined. */
19957 row->end = it->current;
19958 if (!it->bidi_p)
19959 {
19960 row->minpos = row->start.pos;
19961 row->maxpos = row->end.pos;
19962 }
19963 else
19964 {
19965 /* ROW->minpos and ROW->maxpos must be the smallest and
19966 `1 + the largest' buffer positions in ROW. But if ROW was
19967 bidi-reordered, these two positions can be anywhere in the
19968 row, so we must determine them now. */
19969 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
19970 }
19971
19972 /* If the start of this line is the overlay arrow-position, then
19973 mark this glyph row as the one containing the overlay arrow.
19974 This is clearly a mess with variable size fonts. It would be
19975 better to let it be displayed like cursors under X. */
19976 if ((row->displays_text_p || !overlay_arrow_seen)
19977 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
19978 !NILP (overlay_arrow_string)))
19979 {
19980 /* Overlay arrow in window redisplay is a fringe bitmap. */
19981 if (STRINGP (overlay_arrow_string))
19982 {
19983 struct glyph_row *arrow_row
19984 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
19985 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
19986 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
19987 struct glyph *p = row->glyphs[TEXT_AREA];
19988 struct glyph *p2, *end;
19989
19990 /* Copy the arrow glyphs. */
19991 while (glyph < arrow_end)
19992 *p++ = *glyph++;
19993
19994 /* Throw away padding glyphs. */
19995 p2 = p;
19996 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
19997 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
19998 ++p2;
19999 if (p2 > p)
20000 {
20001 while (p2 < end)
20002 *p++ = *p2++;
20003 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20004 }
20005 }
20006 else
20007 {
20008 eassert (INTEGERP (overlay_arrow_string));
20009 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20010 }
20011 overlay_arrow_seen = 1;
20012 }
20013
20014 /* Highlight trailing whitespace. */
20015 if (!NILP (Vshow_trailing_whitespace))
20016 highlight_trailing_whitespace (it->f, it->glyph_row);
20017
20018 /* Compute pixel dimensions of this line. */
20019 compute_line_metrics (it);
20020
20021 /* Implementation note: No changes in the glyphs of ROW or in their
20022 faces can be done past this point, because compute_line_metrics
20023 computes ROW's hash value and stores it within the glyph_row
20024 structure. */
20025
20026 /* Record whether this row ends inside an ellipsis. */
20027 row->ends_in_ellipsis_p
20028 = (it->method == GET_FROM_DISPLAY_VECTOR
20029 && it->ellipsis_p);
20030
20031 /* Save fringe bitmaps in this row. */
20032 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20033 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20034 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20035 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20036
20037 it->left_user_fringe_bitmap = 0;
20038 it->left_user_fringe_face_id = 0;
20039 it->right_user_fringe_bitmap = 0;
20040 it->right_user_fringe_face_id = 0;
20041
20042 /* Maybe set the cursor. */
20043 cvpos = it->w->cursor.vpos;
20044 if ((cvpos < 0
20045 /* In bidi-reordered rows, keep checking for proper cursor
20046 position even if one has been found already, because buffer
20047 positions in such rows change non-linearly with ROW->VPOS,
20048 when a line is continued. One exception: when we are at ZV,
20049 display cursor on the first suitable glyph row, since all
20050 the empty rows after that also have their position set to ZV. */
20051 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20052 lines' rows is implemented for bidi-reordered rows. */
20053 || (it->bidi_p
20054 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20055 && PT >= MATRIX_ROW_START_CHARPOS (row)
20056 && PT <= MATRIX_ROW_END_CHARPOS (row)
20057 && cursor_row_p (row))
20058 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20059
20060 /* Prepare for the next line. This line starts horizontally at (X
20061 HPOS) = (0 0). Vertical positions are incremented. As a
20062 convenience for the caller, IT->glyph_row is set to the next
20063 row to be used. */
20064 it->current_x = it->hpos = 0;
20065 it->current_y += row->height;
20066 SET_TEXT_POS (it->eol_pos, 0, 0);
20067 ++it->vpos;
20068 ++it->glyph_row;
20069 /* The next row should by default use the same value of the
20070 reversed_p flag as this one. set_iterator_to_next decides when
20071 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20072 the flag accordingly. */
20073 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20074 it->glyph_row->reversed_p = row->reversed_p;
20075 it->start = row->end;
20076 return row->displays_text_p;
20077
20078 #undef RECORD_MAX_MIN_POS
20079 }
20080
20081 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20082 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20083 doc: /* Return paragraph direction at point in BUFFER.
20084 Value is either `left-to-right' or `right-to-left'.
20085 If BUFFER is omitted or nil, it defaults to the current buffer.
20086
20087 Paragraph direction determines how the text in the paragraph is displayed.
20088 In left-to-right paragraphs, text begins at the left margin of the window
20089 and the reading direction is generally left to right. In right-to-left
20090 paragraphs, text begins at the right margin and is read from right to left.
20091
20092 See also `bidi-paragraph-direction'. */)
20093 (Lisp_Object buffer)
20094 {
20095 struct buffer *buf = current_buffer;
20096 struct buffer *old = buf;
20097
20098 if (! NILP (buffer))
20099 {
20100 CHECK_BUFFER (buffer);
20101 buf = XBUFFER (buffer);
20102 }
20103
20104 if (NILP (BVAR (buf, bidi_display_reordering))
20105 || NILP (BVAR (buf, enable_multibyte_characters))
20106 /* When we are loading loadup.el, the character property tables
20107 needed for bidi iteration are not yet available. */
20108 || !NILP (Vpurify_flag))
20109 return Qleft_to_right;
20110 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20111 return BVAR (buf, bidi_paragraph_direction);
20112 else
20113 {
20114 /* Determine the direction from buffer text. We could try to
20115 use current_matrix if it is up to date, but this seems fast
20116 enough as it is. */
20117 struct bidi_it itb;
20118 ptrdiff_t pos = BUF_PT (buf);
20119 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20120 int c;
20121 void *itb_data = bidi_shelve_cache ();
20122
20123 set_buffer_temp (buf);
20124 /* bidi_paragraph_init finds the base direction of the paragraph
20125 by searching forward from paragraph start. We need the base
20126 direction of the current or _previous_ paragraph, so we need
20127 to make sure we are within that paragraph. To that end, find
20128 the previous non-empty line. */
20129 if (pos >= ZV && pos > BEGV)
20130 {
20131 pos--;
20132 bytepos = CHAR_TO_BYTE (pos);
20133 }
20134 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20135 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20136 {
20137 while ((c = FETCH_BYTE (bytepos)) == '\n'
20138 || c == ' ' || c == '\t' || c == '\f')
20139 {
20140 if (bytepos <= BEGV_BYTE)
20141 break;
20142 bytepos--;
20143 pos--;
20144 }
20145 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20146 bytepos--;
20147 }
20148 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20149 itb.paragraph_dir = NEUTRAL_DIR;
20150 itb.string.s = NULL;
20151 itb.string.lstring = Qnil;
20152 itb.string.bufpos = 0;
20153 itb.string.unibyte = 0;
20154 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20155 bidi_unshelve_cache (itb_data, 0);
20156 set_buffer_temp (old);
20157 switch (itb.paragraph_dir)
20158 {
20159 case L2R:
20160 return Qleft_to_right;
20161 break;
20162 case R2L:
20163 return Qright_to_left;
20164 break;
20165 default:
20166 emacs_abort ();
20167 }
20168 }
20169 }
20170
20171
20172 \f
20173 /***********************************************************************
20174 Menu Bar
20175 ***********************************************************************/
20176
20177 /* Redisplay the menu bar in the frame for window W.
20178
20179 The menu bar of X frames that don't have X toolkit support is
20180 displayed in a special window W->frame->menu_bar_window.
20181
20182 The menu bar of terminal frames is treated specially as far as
20183 glyph matrices are concerned. Menu bar lines are not part of
20184 windows, so the update is done directly on the frame matrix rows
20185 for the menu bar. */
20186
20187 static void
20188 display_menu_bar (struct window *w)
20189 {
20190 struct frame *f = XFRAME (WINDOW_FRAME (w));
20191 struct it it;
20192 Lisp_Object items;
20193 int i;
20194
20195 /* Don't do all this for graphical frames. */
20196 #ifdef HAVE_NTGUI
20197 if (FRAME_W32_P (f))
20198 return;
20199 #endif
20200 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20201 if (FRAME_X_P (f))
20202 return;
20203 #endif
20204
20205 #ifdef HAVE_NS
20206 if (FRAME_NS_P (f))
20207 return;
20208 #endif /* HAVE_NS */
20209
20210 #ifdef USE_X_TOOLKIT
20211 eassert (!FRAME_WINDOW_P (f));
20212 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20213 it.first_visible_x = 0;
20214 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20215 #else /* not USE_X_TOOLKIT */
20216 if (FRAME_WINDOW_P (f))
20217 {
20218 /* Menu bar lines are displayed in the desired matrix of the
20219 dummy window menu_bar_window. */
20220 struct window *menu_w;
20221 eassert (WINDOWP (f->menu_bar_window));
20222 menu_w = XWINDOW (f->menu_bar_window);
20223 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20224 MENU_FACE_ID);
20225 it.first_visible_x = 0;
20226 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20227 }
20228 else
20229 {
20230 /* This is a TTY frame, i.e. character hpos/vpos are used as
20231 pixel x/y. */
20232 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20233 MENU_FACE_ID);
20234 it.first_visible_x = 0;
20235 it.last_visible_x = FRAME_COLS (f);
20236 }
20237 #endif /* not USE_X_TOOLKIT */
20238
20239 /* FIXME: This should be controlled by a user option. See the
20240 comments in redisplay_tool_bar and display_mode_line about
20241 this. */
20242 it.paragraph_embedding = L2R;
20243
20244 /* Clear all rows of the menu bar. */
20245 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20246 {
20247 struct glyph_row *row = it.glyph_row + i;
20248 clear_glyph_row (row);
20249 row->enabled_p = 1;
20250 row->full_width_p = 1;
20251 }
20252
20253 /* Display all items of the menu bar. */
20254 items = FRAME_MENU_BAR_ITEMS (it.f);
20255 for (i = 0; i < ASIZE (items); i += 4)
20256 {
20257 Lisp_Object string;
20258
20259 /* Stop at nil string. */
20260 string = AREF (items, i + 1);
20261 if (NILP (string))
20262 break;
20263
20264 /* Remember where item was displayed. */
20265 ASET (items, i + 3, make_number (it.hpos));
20266
20267 /* Display the item, pad with one space. */
20268 if (it.current_x < it.last_visible_x)
20269 display_string (NULL, string, Qnil, 0, 0, &it,
20270 SCHARS (string) + 1, 0, 0, -1);
20271 }
20272
20273 /* Fill out the line with spaces. */
20274 if (it.current_x < it.last_visible_x)
20275 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20276
20277 /* Compute the total height of the lines. */
20278 compute_line_metrics (&it);
20279 }
20280
20281
20282 \f
20283 /***********************************************************************
20284 Mode Line
20285 ***********************************************************************/
20286
20287 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20288 FORCE is non-zero, redisplay mode lines unconditionally.
20289 Otherwise, redisplay only mode lines that are garbaged. Value is
20290 the number of windows whose mode lines were redisplayed. */
20291
20292 static int
20293 redisplay_mode_lines (Lisp_Object window, int force)
20294 {
20295 int nwindows = 0;
20296
20297 while (!NILP (window))
20298 {
20299 struct window *w = XWINDOW (window);
20300
20301 if (WINDOWP (w->hchild))
20302 nwindows += redisplay_mode_lines (w->hchild, force);
20303 else if (WINDOWP (w->vchild))
20304 nwindows += redisplay_mode_lines (w->vchild, force);
20305 else if (force
20306 || FRAME_GARBAGED_P (XFRAME (w->frame))
20307 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20308 {
20309 struct text_pos lpoint;
20310 struct buffer *old = current_buffer;
20311
20312 /* Set the window's buffer for the mode line display. */
20313 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20314 set_buffer_internal_1 (XBUFFER (w->buffer));
20315
20316 /* Point refers normally to the selected window. For any
20317 other window, set up appropriate value. */
20318 if (!EQ (window, selected_window))
20319 {
20320 struct text_pos pt;
20321
20322 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20323 if (CHARPOS (pt) < BEGV)
20324 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20325 else if (CHARPOS (pt) > (ZV - 1))
20326 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20327 else
20328 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20329 }
20330
20331 /* Display mode lines. */
20332 clear_glyph_matrix (w->desired_matrix);
20333 if (display_mode_lines (w))
20334 {
20335 ++nwindows;
20336 w->must_be_updated_p = 1;
20337 }
20338
20339 /* Restore old settings. */
20340 set_buffer_internal_1 (old);
20341 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20342 }
20343
20344 window = w->next;
20345 }
20346
20347 return nwindows;
20348 }
20349
20350
20351 /* Display the mode and/or header line of window W. Value is the
20352 sum number of mode lines and header lines displayed. */
20353
20354 static int
20355 display_mode_lines (struct window *w)
20356 {
20357 Lisp_Object old_selected_window = selected_window;
20358 Lisp_Object old_selected_frame = selected_frame;
20359 Lisp_Object new_frame = w->frame;
20360 Lisp_Object old_frame_selected_window = XFRAME (new_frame)->selected_window;
20361 int n = 0;
20362
20363 selected_frame = w->frame;
20364 XSETWINDOW (selected_window, w);
20365
20366 /* These will be set while the mode line specs are processed. */
20367 line_number_displayed = 0;
20368 wset_column_number_displayed (w, Qnil);
20369
20370 if (WINDOW_WANTS_MODELINE_P (w))
20371 {
20372 struct window *sel_w = XWINDOW (old_selected_window);
20373
20374 /* Select mode line face based on the real selected window. */
20375 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20376 BVAR (current_buffer, mode_line_format));
20377 ++n;
20378 }
20379
20380 if (WINDOW_WANTS_HEADER_LINE_P (w))
20381 {
20382 display_mode_line (w, HEADER_LINE_FACE_ID,
20383 BVAR (current_buffer, header_line_format));
20384 ++n;
20385 }
20386
20387 XFRAME (new_frame)->selected_window = old_frame_selected_window;
20388 selected_frame = old_selected_frame;
20389 selected_window = old_selected_window;
20390 return n;
20391 }
20392
20393
20394 /* Display mode or header line of window W. FACE_ID specifies which
20395 line to display; it is either MODE_LINE_FACE_ID or
20396 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20397 display. Value is the pixel height of the mode/header line
20398 displayed. */
20399
20400 static int
20401 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20402 {
20403 struct it it;
20404 struct face *face;
20405 ptrdiff_t count = SPECPDL_INDEX ();
20406
20407 init_iterator (&it, w, -1, -1, NULL, face_id);
20408 /* Don't extend on a previously drawn mode-line.
20409 This may happen if called from pos_visible_p. */
20410 it.glyph_row->enabled_p = 0;
20411 prepare_desired_row (it.glyph_row);
20412
20413 it.glyph_row->mode_line_p = 1;
20414
20415 /* FIXME: This should be controlled by a user option. But
20416 supporting such an option is not trivial, since the mode line is
20417 made up of many separate strings. */
20418 it.paragraph_embedding = L2R;
20419
20420 record_unwind_protect (unwind_format_mode_line,
20421 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20422
20423 mode_line_target = MODE_LINE_DISPLAY;
20424
20425 /* Temporarily make frame's keyboard the current kboard so that
20426 kboard-local variables in the mode_line_format will get the right
20427 values. */
20428 push_kboard (FRAME_KBOARD (it.f));
20429 record_unwind_save_match_data ();
20430 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20431 pop_kboard ();
20432
20433 unbind_to (count, Qnil);
20434
20435 /* Fill up with spaces. */
20436 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20437
20438 compute_line_metrics (&it);
20439 it.glyph_row->full_width_p = 1;
20440 it.glyph_row->continued_p = 0;
20441 it.glyph_row->truncated_on_left_p = 0;
20442 it.glyph_row->truncated_on_right_p = 0;
20443
20444 /* Make a 3D mode-line have a shadow at its right end. */
20445 face = FACE_FROM_ID (it.f, face_id);
20446 extend_face_to_end_of_line (&it);
20447 if (face->box != FACE_NO_BOX)
20448 {
20449 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20450 + it.glyph_row->used[TEXT_AREA] - 1);
20451 last->right_box_line_p = 1;
20452 }
20453
20454 return it.glyph_row->height;
20455 }
20456
20457 /* Move element ELT in LIST to the front of LIST.
20458 Return the updated list. */
20459
20460 static Lisp_Object
20461 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20462 {
20463 register Lisp_Object tail, prev;
20464 register Lisp_Object tem;
20465
20466 tail = list;
20467 prev = Qnil;
20468 while (CONSP (tail))
20469 {
20470 tem = XCAR (tail);
20471
20472 if (EQ (elt, tem))
20473 {
20474 /* Splice out the link TAIL. */
20475 if (NILP (prev))
20476 list = XCDR (tail);
20477 else
20478 Fsetcdr (prev, XCDR (tail));
20479
20480 /* Now make it the first. */
20481 Fsetcdr (tail, list);
20482 return tail;
20483 }
20484 else
20485 prev = tail;
20486 tail = XCDR (tail);
20487 QUIT;
20488 }
20489
20490 /* Not found--return unchanged LIST. */
20491 return list;
20492 }
20493
20494 /* Contribute ELT to the mode line for window IT->w. How it
20495 translates into text depends on its data type.
20496
20497 IT describes the display environment in which we display, as usual.
20498
20499 DEPTH is the depth in recursion. It is used to prevent
20500 infinite recursion here.
20501
20502 FIELD_WIDTH is the number of characters the display of ELT should
20503 occupy in the mode line, and PRECISION is the maximum number of
20504 characters to display from ELT's representation. See
20505 display_string for details.
20506
20507 Returns the hpos of the end of the text generated by ELT.
20508
20509 PROPS is a property list to add to any string we encounter.
20510
20511 If RISKY is nonzero, remove (disregard) any properties in any string
20512 we encounter, and ignore :eval and :propertize.
20513
20514 The global variable `mode_line_target' determines whether the
20515 output is passed to `store_mode_line_noprop',
20516 `store_mode_line_string', or `display_string'. */
20517
20518 static int
20519 display_mode_element (struct it *it, int depth, int field_width, int precision,
20520 Lisp_Object elt, Lisp_Object props, int risky)
20521 {
20522 int n = 0, field, prec;
20523 int literal = 0;
20524
20525 tail_recurse:
20526 if (depth > 100)
20527 elt = build_string ("*too-deep*");
20528
20529 depth++;
20530
20531 switch (XTYPE (elt))
20532 {
20533 case Lisp_String:
20534 {
20535 /* A string: output it and check for %-constructs within it. */
20536 unsigned char c;
20537 ptrdiff_t offset = 0;
20538
20539 if (SCHARS (elt) > 0
20540 && (!NILP (props) || risky))
20541 {
20542 Lisp_Object oprops, aelt;
20543 oprops = Ftext_properties_at (make_number (0), elt);
20544
20545 /* If the starting string's properties are not what
20546 we want, translate the string. Also, if the string
20547 is risky, do that anyway. */
20548
20549 if (NILP (Fequal (props, oprops)) || risky)
20550 {
20551 /* If the starting string has properties,
20552 merge the specified ones onto the existing ones. */
20553 if (! NILP (oprops) && !risky)
20554 {
20555 Lisp_Object tem;
20556
20557 oprops = Fcopy_sequence (oprops);
20558 tem = props;
20559 while (CONSP (tem))
20560 {
20561 oprops = Fplist_put (oprops, XCAR (tem),
20562 XCAR (XCDR (tem)));
20563 tem = XCDR (XCDR (tem));
20564 }
20565 props = oprops;
20566 }
20567
20568 aelt = Fassoc (elt, mode_line_proptrans_alist);
20569 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20570 {
20571 /* AELT is what we want. Move it to the front
20572 without consing. */
20573 elt = XCAR (aelt);
20574 mode_line_proptrans_alist
20575 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20576 }
20577 else
20578 {
20579 Lisp_Object tem;
20580
20581 /* If AELT has the wrong props, it is useless.
20582 so get rid of it. */
20583 if (! NILP (aelt))
20584 mode_line_proptrans_alist
20585 = Fdelq (aelt, mode_line_proptrans_alist);
20586
20587 elt = Fcopy_sequence (elt);
20588 Fset_text_properties (make_number (0), Flength (elt),
20589 props, elt);
20590 /* Add this item to mode_line_proptrans_alist. */
20591 mode_line_proptrans_alist
20592 = Fcons (Fcons (elt, props),
20593 mode_line_proptrans_alist);
20594 /* Truncate mode_line_proptrans_alist
20595 to at most 50 elements. */
20596 tem = Fnthcdr (make_number (50),
20597 mode_line_proptrans_alist);
20598 if (! NILP (tem))
20599 XSETCDR (tem, Qnil);
20600 }
20601 }
20602 }
20603
20604 offset = 0;
20605
20606 if (literal)
20607 {
20608 prec = precision - n;
20609 switch (mode_line_target)
20610 {
20611 case MODE_LINE_NOPROP:
20612 case MODE_LINE_TITLE:
20613 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20614 break;
20615 case MODE_LINE_STRING:
20616 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20617 break;
20618 case MODE_LINE_DISPLAY:
20619 n += display_string (NULL, elt, Qnil, 0, 0, it,
20620 0, prec, 0, STRING_MULTIBYTE (elt));
20621 break;
20622 }
20623
20624 break;
20625 }
20626
20627 /* Handle the non-literal case. */
20628
20629 while ((precision <= 0 || n < precision)
20630 && SREF (elt, offset) != 0
20631 && (mode_line_target != MODE_LINE_DISPLAY
20632 || it->current_x < it->last_visible_x))
20633 {
20634 ptrdiff_t last_offset = offset;
20635
20636 /* Advance to end of string or next format specifier. */
20637 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20638 ;
20639
20640 if (offset - 1 != last_offset)
20641 {
20642 ptrdiff_t nchars, nbytes;
20643
20644 /* Output to end of string or up to '%'. Field width
20645 is length of string. Don't output more than
20646 PRECISION allows us. */
20647 offset--;
20648
20649 prec = c_string_width (SDATA (elt) + last_offset,
20650 offset - last_offset, precision - n,
20651 &nchars, &nbytes);
20652
20653 switch (mode_line_target)
20654 {
20655 case MODE_LINE_NOPROP:
20656 case MODE_LINE_TITLE:
20657 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20658 break;
20659 case MODE_LINE_STRING:
20660 {
20661 ptrdiff_t bytepos = last_offset;
20662 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20663 ptrdiff_t endpos = (precision <= 0
20664 ? string_byte_to_char (elt, offset)
20665 : charpos + nchars);
20666
20667 n += store_mode_line_string (NULL,
20668 Fsubstring (elt, make_number (charpos),
20669 make_number (endpos)),
20670 0, 0, 0, Qnil);
20671 }
20672 break;
20673 case MODE_LINE_DISPLAY:
20674 {
20675 ptrdiff_t bytepos = last_offset;
20676 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20677
20678 if (precision <= 0)
20679 nchars = string_byte_to_char (elt, offset) - charpos;
20680 n += display_string (NULL, elt, Qnil, 0, charpos,
20681 it, 0, nchars, 0,
20682 STRING_MULTIBYTE (elt));
20683 }
20684 break;
20685 }
20686 }
20687 else /* c == '%' */
20688 {
20689 ptrdiff_t percent_position = offset;
20690
20691 /* Get the specified minimum width. Zero means
20692 don't pad. */
20693 field = 0;
20694 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20695 field = field * 10 + c - '0';
20696
20697 /* Don't pad beyond the total padding allowed. */
20698 if (field_width - n > 0 && field > field_width - n)
20699 field = field_width - n;
20700
20701 /* Note that either PRECISION <= 0 or N < PRECISION. */
20702 prec = precision - n;
20703
20704 if (c == 'M')
20705 n += display_mode_element (it, depth, field, prec,
20706 Vglobal_mode_string, props,
20707 risky);
20708 else if (c != 0)
20709 {
20710 int multibyte;
20711 ptrdiff_t bytepos, charpos;
20712 const char *spec;
20713 Lisp_Object string;
20714
20715 bytepos = percent_position;
20716 charpos = (STRING_MULTIBYTE (elt)
20717 ? string_byte_to_char (elt, bytepos)
20718 : bytepos);
20719 spec = decode_mode_spec (it->w, c, field, &string);
20720 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20721
20722 switch (mode_line_target)
20723 {
20724 case MODE_LINE_NOPROP:
20725 case MODE_LINE_TITLE:
20726 n += store_mode_line_noprop (spec, field, prec);
20727 break;
20728 case MODE_LINE_STRING:
20729 {
20730 Lisp_Object tem = build_string (spec);
20731 props = Ftext_properties_at (make_number (charpos), elt);
20732 /* Should only keep face property in props */
20733 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20734 }
20735 break;
20736 case MODE_LINE_DISPLAY:
20737 {
20738 int nglyphs_before, nwritten;
20739
20740 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20741 nwritten = display_string (spec, string, elt,
20742 charpos, 0, it,
20743 field, prec, 0,
20744 multibyte);
20745
20746 /* Assign to the glyphs written above the
20747 string where the `%x' came from, position
20748 of the `%'. */
20749 if (nwritten > 0)
20750 {
20751 struct glyph *glyph
20752 = (it->glyph_row->glyphs[TEXT_AREA]
20753 + nglyphs_before);
20754 int i;
20755
20756 for (i = 0; i < nwritten; ++i)
20757 {
20758 glyph[i].object = elt;
20759 glyph[i].charpos = charpos;
20760 }
20761
20762 n += nwritten;
20763 }
20764 }
20765 break;
20766 }
20767 }
20768 else /* c == 0 */
20769 break;
20770 }
20771 }
20772 }
20773 break;
20774
20775 case Lisp_Symbol:
20776 /* A symbol: process the value of the symbol recursively
20777 as if it appeared here directly. Avoid error if symbol void.
20778 Special case: if value of symbol is a string, output the string
20779 literally. */
20780 {
20781 register Lisp_Object tem;
20782
20783 /* If the variable is not marked as risky to set
20784 then its contents are risky to use. */
20785 if (NILP (Fget (elt, Qrisky_local_variable)))
20786 risky = 1;
20787
20788 tem = Fboundp (elt);
20789 if (!NILP (tem))
20790 {
20791 tem = Fsymbol_value (elt);
20792 /* If value is a string, output that string literally:
20793 don't check for % within it. */
20794 if (STRINGP (tem))
20795 literal = 1;
20796
20797 if (!EQ (tem, elt))
20798 {
20799 /* Give up right away for nil or t. */
20800 elt = tem;
20801 goto tail_recurse;
20802 }
20803 }
20804 }
20805 break;
20806
20807 case Lisp_Cons:
20808 {
20809 register Lisp_Object car, tem;
20810
20811 /* A cons cell: five distinct cases.
20812 If first element is :eval or :propertize, do something special.
20813 If first element is a string or a cons, process all the elements
20814 and effectively concatenate them.
20815 If first element is a negative number, truncate displaying cdr to
20816 at most that many characters. If positive, pad (with spaces)
20817 to at least that many characters.
20818 If first element is a symbol, process the cadr or caddr recursively
20819 according to whether the symbol's value is non-nil or nil. */
20820 car = XCAR (elt);
20821 if (EQ (car, QCeval))
20822 {
20823 /* An element of the form (:eval FORM) means evaluate FORM
20824 and use the result as mode line elements. */
20825
20826 if (risky)
20827 break;
20828
20829 if (CONSP (XCDR (elt)))
20830 {
20831 Lisp_Object spec;
20832 spec = safe_eval (XCAR (XCDR (elt)));
20833 n += display_mode_element (it, depth, field_width - n,
20834 precision - n, spec, props,
20835 risky);
20836 }
20837 }
20838 else if (EQ (car, QCpropertize))
20839 {
20840 /* An element of the form (:propertize ELT PROPS...)
20841 means display ELT but applying properties PROPS. */
20842
20843 if (risky)
20844 break;
20845
20846 if (CONSP (XCDR (elt)))
20847 n += display_mode_element (it, depth, field_width - n,
20848 precision - n, XCAR (XCDR (elt)),
20849 XCDR (XCDR (elt)), risky);
20850 }
20851 else if (SYMBOLP (car))
20852 {
20853 tem = Fboundp (car);
20854 elt = XCDR (elt);
20855 if (!CONSP (elt))
20856 goto invalid;
20857 /* elt is now the cdr, and we know it is a cons cell.
20858 Use its car if CAR has a non-nil value. */
20859 if (!NILP (tem))
20860 {
20861 tem = Fsymbol_value (car);
20862 if (!NILP (tem))
20863 {
20864 elt = XCAR (elt);
20865 goto tail_recurse;
20866 }
20867 }
20868 /* Symbol's value is nil (or symbol is unbound)
20869 Get the cddr of the original list
20870 and if possible find the caddr and use that. */
20871 elt = XCDR (elt);
20872 if (NILP (elt))
20873 break;
20874 else if (!CONSP (elt))
20875 goto invalid;
20876 elt = XCAR (elt);
20877 goto tail_recurse;
20878 }
20879 else if (INTEGERP (car))
20880 {
20881 register int lim = XINT (car);
20882 elt = XCDR (elt);
20883 if (lim < 0)
20884 {
20885 /* Negative int means reduce maximum width. */
20886 if (precision <= 0)
20887 precision = -lim;
20888 else
20889 precision = min (precision, -lim);
20890 }
20891 else if (lim > 0)
20892 {
20893 /* Padding specified. Don't let it be more than
20894 current maximum. */
20895 if (precision > 0)
20896 lim = min (precision, lim);
20897
20898 /* If that's more padding than already wanted, queue it.
20899 But don't reduce padding already specified even if
20900 that is beyond the current truncation point. */
20901 field_width = max (lim, field_width);
20902 }
20903 goto tail_recurse;
20904 }
20905 else if (STRINGP (car) || CONSP (car))
20906 {
20907 Lisp_Object halftail = elt;
20908 int len = 0;
20909
20910 while (CONSP (elt)
20911 && (precision <= 0 || n < precision))
20912 {
20913 n += display_mode_element (it, depth,
20914 /* Do padding only after the last
20915 element in the list. */
20916 (! CONSP (XCDR (elt))
20917 ? field_width - n
20918 : 0),
20919 precision - n, XCAR (elt),
20920 props, risky);
20921 elt = XCDR (elt);
20922 len++;
20923 if ((len & 1) == 0)
20924 halftail = XCDR (halftail);
20925 /* Check for cycle. */
20926 if (EQ (halftail, elt))
20927 break;
20928 }
20929 }
20930 }
20931 break;
20932
20933 default:
20934 invalid:
20935 elt = build_string ("*invalid*");
20936 goto tail_recurse;
20937 }
20938
20939 /* Pad to FIELD_WIDTH. */
20940 if (field_width > 0 && n < field_width)
20941 {
20942 switch (mode_line_target)
20943 {
20944 case MODE_LINE_NOPROP:
20945 case MODE_LINE_TITLE:
20946 n += store_mode_line_noprop ("", field_width - n, 0);
20947 break;
20948 case MODE_LINE_STRING:
20949 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
20950 break;
20951 case MODE_LINE_DISPLAY:
20952 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
20953 0, 0, 0);
20954 break;
20955 }
20956 }
20957
20958 return n;
20959 }
20960
20961 /* Store a mode-line string element in mode_line_string_list.
20962
20963 If STRING is non-null, display that C string. Otherwise, the Lisp
20964 string LISP_STRING is displayed.
20965
20966 FIELD_WIDTH is the minimum number of output glyphs to produce.
20967 If STRING has fewer characters than FIELD_WIDTH, pad to the right
20968 with spaces. FIELD_WIDTH <= 0 means don't pad.
20969
20970 PRECISION is the maximum number of characters to output from
20971 STRING. PRECISION <= 0 means don't truncate the string.
20972
20973 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
20974 properties to the string.
20975
20976 PROPS are the properties to add to the string.
20977 The mode_line_string_face face property is always added to the string.
20978 */
20979
20980 static int
20981 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
20982 int field_width, int precision, Lisp_Object props)
20983 {
20984 ptrdiff_t len;
20985 int n = 0;
20986
20987 if (string != NULL)
20988 {
20989 len = strlen (string);
20990 if (precision > 0 && len > precision)
20991 len = precision;
20992 lisp_string = make_string (string, len);
20993 if (NILP (props))
20994 props = mode_line_string_face_prop;
20995 else if (!NILP (mode_line_string_face))
20996 {
20997 Lisp_Object face = Fplist_get (props, Qface);
20998 props = Fcopy_sequence (props);
20999 if (NILP (face))
21000 face = mode_line_string_face;
21001 else
21002 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
21003 props = Fplist_put (props, Qface, face);
21004 }
21005 Fadd_text_properties (make_number (0), make_number (len),
21006 props, lisp_string);
21007 }
21008 else
21009 {
21010 len = XFASTINT (Flength (lisp_string));
21011 if (precision > 0 && len > precision)
21012 {
21013 len = precision;
21014 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21015 precision = -1;
21016 }
21017 if (!NILP (mode_line_string_face))
21018 {
21019 Lisp_Object face;
21020 if (NILP (props))
21021 props = Ftext_properties_at (make_number (0), lisp_string);
21022 face = Fplist_get (props, Qface);
21023 if (NILP (face))
21024 face = mode_line_string_face;
21025 else
21026 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
21027 props = Fcons (Qface, Fcons (face, Qnil));
21028 if (copy_string)
21029 lisp_string = Fcopy_sequence (lisp_string);
21030 }
21031 if (!NILP (props))
21032 Fadd_text_properties (make_number (0), make_number (len),
21033 props, lisp_string);
21034 }
21035
21036 if (len > 0)
21037 {
21038 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21039 n += len;
21040 }
21041
21042 if (field_width > len)
21043 {
21044 field_width -= len;
21045 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21046 if (!NILP (props))
21047 Fadd_text_properties (make_number (0), make_number (field_width),
21048 props, lisp_string);
21049 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21050 n += field_width;
21051 }
21052
21053 return n;
21054 }
21055
21056
21057 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21058 1, 4, 0,
21059 doc: /* Format a string out of a mode line format specification.
21060 First arg FORMAT specifies the mode line format (see `mode-line-format'
21061 for details) to use.
21062
21063 By default, the format is evaluated for the currently selected window.
21064
21065 Optional second arg FACE specifies the face property to put on all
21066 characters for which no face is specified. The value nil means the
21067 default face. The value t means whatever face the window's mode line
21068 currently uses (either `mode-line' or `mode-line-inactive',
21069 depending on whether the window is the selected window or not).
21070 An integer value means the value string has no text
21071 properties.
21072
21073 Optional third and fourth args WINDOW and BUFFER specify the window
21074 and buffer to use as the context for the formatting (defaults
21075 are the selected window and the WINDOW's buffer). */)
21076 (Lisp_Object format, Lisp_Object face,
21077 Lisp_Object window, Lisp_Object buffer)
21078 {
21079 struct it it;
21080 int len;
21081 struct window *w;
21082 struct buffer *old_buffer = NULL;
21083 int face_id;
21084 int no_props = INTEGERP (face);
21085 ptrdiff_t count = SPECPDL_INDEX ();
21086 Lisp_Object str;
21087 int string_start = 0;
21088
21089 w = decode_any_window (window);
21090 XSETWINDOW (window, w);
21091
21092 if (NILP (buffer))
21093 buffer = w->buffer;
21094 CHECK_BUFFER (buffer);
21095
21096 /* Make formatting the modeline a non-op when noninteractive, otherwise
21097 there will be problems later caused by a partially initialized frame. */
21098 if (NILP (format) || noninteractive)
21099 return empty_unibyte_string;
21100
21101 if (no_props)
21102 face = Qnil;
21103
21104 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21105 : EQ (face, Qt) ? (EQ (window, selected_window)
21106 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21107 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21108 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21109 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21110 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21111 : DEFAULT_FACE_ID;
21112
21113 old_buffer = current_buffer;
21114
21115 /* Save things including mode_line_proptrans_alist,
21116 and set that to nil so that we don't alter the outer value. */
21117 record_unwind_protect (unwind_format_mode_line,
21118 format_mode_line_unwind_data
21119 (XFRAME (WINDOW_FRAME (w)),
21120 old_buffer, selected_window, 1));
21121 mode_line_proptrans_alist = Qnil;
21122
21123 Fselect_window (window, Qt);
21124 set_buffer_internal_1 (XBUFFER (buffer));
21125
21126 init_iterator (&it, w, -1, -1, NULL, face_id);
21127
21128 if (no_props)
21129 {
21130 mode_line_target = MODE_LINE_NOPROP;
21131 mode_line_string_face_prop = Qnil;
21132 mode_line_string_list = Qnil;
21133 string_start = MODE_LINE_NOPROP_LEN (0);
21134 }
21135 else
21136 {
21137 mode_line_target = MODE_LINE_STRING;
21138 mode_line_string_list = Qnil;
21139 mode_line_string_face = face;
21140 mode_line_string_face_prop
21141 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21142 }
21143
21144 push_kboard (FRAME_KBOARD (it.f));
21145 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21146 pop_kboard ();
21147
21148 if (no_props)
21149 {
21150 len = MODE_LINE_NOPROP_LEN (string_start);
21151 str = make_string (mode_line_noprop_buf + string_start, len);
21152 }
21153 else
21154 {
21155 mode_line_string_list = Fnreverse (mode_line_string_list);
21156 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21157 empty_unibyte_string);
21158 }
21159
21160 unbind_to (count, Qnil);
21161 return str;
21162 }
21163
21164 /* Write a null-terminated, right justified decimal representation of
21165 the positive integer D to BUF using a minimal field width WIDTH. */
21166
21167 static void
21168 pint2str (register char *buf, register int width, register ptrdiff_t d)
21169 {
21170 register char *p = buf;
21171
21172 if (d <= 0)
21173 *p++ = '0';
21174 else
21175 {
21176 while (d > 0)
21177 {
21178 *p++ = d % 10 + '0';
21179 d /= 10;
21180 }
21181 }
21182
21183 for (width -= (int) (p - buf); width > 0; --width)
21184 *p++ = ' ';
21185 *p-- = '\0';
21186 while (p > buf)
21187 {
21188 d = *buf;
21189 *buf++ = *p;
21190 *p-- = d;
21191 }
21192 }
21193
21194 /* Write a null-terminated, right justified decimal and "human
21195 readable" representation of the nonnegative integer D to BUF using
21196 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21197
21198 static const char power_letter[] =
21199 {
21200 0, /* no letter */
21201 'k', /* kilo */
21202 'M', /* mega */
21203 'G', /* giga */
21204 'T', /* tera */
21205 'P', /* peta */
21206 'E', /* exa */
21207 'Z', /* zetta */
21208 'Y' /* yotta */
21209 };
21210
21211 static void
21212 pint2hrstr (char *buf, int width, ptrdiff_t d)
21213 {
21214 /* We aim to represent the nonnegative integer D as
21215 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21216 ptrdiff_t quotient = d;
21217 int remainder = 0;
21218 /* -1 means: do not use TENTHS. */
21219 int tenths = -1;
21220 int exponent = 0;
21221
21222 /* Length of QUOTIENT.TENTHS as a string. */
21223 int length;
21224
21225 char * psuffix;
21226 char * p;
21227
21228 if (1000 <= quotient)
21229 {
21230 /* Scale to the appropriate EXPONENT. */
21231 do
21232 {
21233 remainder = quotient % 1000;
21234 quotient /= 1000;
21235 exponent++;
21236 }
21237 while (1000 <= quotient);
21238
21239 /* Round to nearest and decide whether to use TENTHS or not. */
21240 if (quotient <= 9)
21241 {
21242 tenths = remainder / 100;
21243 if (50 <= remainder % 100)
21244 {
21245 if (tenths < 9)
21246 tenths++;
21247 else
21248 {
21249 quotient++;
21250 if (quotient == 10)
21251 tenths = -1;
21252 else
21253 tenths = 0;
21254 }
21255 }
21256 }
21257 else
21258 if (500 <= remainder)
21259 {
21260 if (quotient < 999)
21261 quotient++;
21262 else
21263 {
21264 quotient = 1;
21265 exponent++;
21266 tenths = 0;
21267 }
21268 }
21269 }
21270
21271 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21272 if (tenths == -1 && quotient <= 99)
21273 if (quotient <= 9)
21274 length = 1;
21275 else
21276 length = 2;
21277 else
21278 length = 3;
21279 p = psuffix = buf + max (width, length);
21280
21281 /* Print EXPONENT. */
21282 *psuffix++ = power_letter[exponent];
21283 *psuffix = '\0';
21284
21285 /* Print TENTHS. */
21286 if (tenths >= 0)
21287 {
21288 *--p = '0' + tenths;
21289 *--p = '.';
21290 }
21291
21292 /* Print QUOTIENT. */
21293 do
21294 {
21295 int digit = quotient % 10;
21296 *--p = '0' + digit;
21297 }
21298 while ((quotient /= 10) != 0);
21299
21300 /* Print leading spaces. */
21301 while (buf < p)
21302 *--p = ' ';
21303 }
21304
21305 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21306 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21307 type of CODING_SYSTEM. Return updated pointer into BUF. */
21308
21309 static unsigned char invalid_eol_type[] = "(*invalid*)";
21310
21311 static char *
21312 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21313 {
21314 Lisp_Object val;
21315 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21316 const unsigned char *eol_str;
21317 int eol_str_len;
21318 /* The EOL conversion we are using. */
21319 Lisp_Object eoltype;
21320
21321 val = CODING_SYSTEM_SPEC (coding_system);
21322 eoltype = Qnil;
21323
21324 if (!VECTORP (val)) /* Not yet decided. */
21325 {
21326 *buf++ = multibyte ? '-' : ' ';
21327 if (eol_flag)
21328 eoltype = eol_mnemonic_undecided;
21329 /* Don't mention EOL conversion if it isn't decided. */
21330 }
21331 else
21332 {
21333 Lisp_Object attrs;
21334 Lisp_Object eolvalue;
21335
21336 attrs = AREF (val, 0);
21337 eolvalue = AREF (val, 2);
21338
21339 *buf++ = multibyte
21340 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21341 : ' ';
21342
21343 if (eol_flag)
21344 {
21345 /* The EOL conversion that is normal on this system. */
21346
21347 if (NILP (eolvalue)) /* Not yet decided. */
21348 eoltype = eol_mnemonic_undecided;
21349 else if (VECTORP (eolvalue)) /* Not yet decided. */
21350 eoltype = eol_mnemonic_undecided;
21351 else /* eolvalue is Qunix, Qdos, or Qmac. */
21352 eoltype = (EQ (eolvalue, Qunix)
21353 ? eol_mnemonic_unix
21354 : (EQ (eolvalue, Qdos) == 1
21355 ? eol_mnemonic_dos : eol_mnemonic_mac));
21356 }
21357 }
21358
21359 if (eol_flag)
21360 {
21361 /* Mention the EOL conversion if it is not the usual one. */
21362 if (STRINGP (eoltype))
21363 {
21364 eol_str = SDATA (eoltype);
21365 eol_str_len = SBYTES (eoltype);
21366 }
21367 else if (CHARACTERP (eoltype))
21368 {
21369 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21370 int c = XFASTINT (eoltype);
21371 eol_str_len = CHAR_STRING (c, tmp);
21372 eol_str = tmp;
21373 }
21374 else
21375 {
21376 eol_str = invalid_eol_type;
21377 eol_str_len = sizeof (invalid_eol_type) - 1;
21378 }
21379 memcpy (buf, eol_str, eol_str_len);
21380 buf += eol_str_len;
21381 }
21382
21383 return buf;
21384 }
21385
21386 /* Return a string for the output of a mode line %-spec for window W,
21387 generated by character C. FIELD_WIDTH > 0 means pad the string
21388 returned with spaces to that value. Return a Lisp string in
21389 *STRING if the resulting string is taken from that Lisp string.
21390
21391 Note we operate on the current buffer for most purposes,
21392 the exception being w->base_line_pos. */
21393
21394 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21395
21396 static const char *
21397 decode_mode_spec (struct window *w, register int c, int field_width,
21398 Lisp_Object *string)
21399 {
21400 Lisp_Object obj;
21401 struct frame *f = XFRAME (WINDOW_FRAME (w));
21402 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21403 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21404 produce strings from numerical values, so limit preposterously
21405 large values of FIELD_WIDTH to avoid overrunning the buffer's
21406 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21407 bytes plus the terminating null. */
21408 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21409 struct buffer *b = current_buffer;
21410
21411 obj = Qnil;
21412 *string = Qnil;
21413
21414 switch (c)
21415 {
21416 case '*':
21417 if (!NILP (BVAR (b, read_only)))
21418 return "%";
21419 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21420 return "*";
21421 return "-";
21422
21423 case '+':
21424 /* This differs from %* only for a modified read-only buffer. */
21425 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21426 return "*";
21427 if (!NILP (BVAR (b, read_only)))
21428 return "%";
21429 return "-";
21430
21431 case '&':
21432 /* This differs from %* in ignoring read-only-ness. */
21433 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21434 return "*";
21435 return "-";
21436
21437 case '%':
21438 return "%";
21439
21440 case '[':
21441 {
21442 int i;
21443 char *p;
21444
21445 if (command_loop_level > 5)
21446 return "[[[... ";
21447 p = decode_mode_spec_buf;
21448 for (i = 0; i < command_loop_level; i++)
21449 *p++ = '[';
21450 *p = 0;
21451 return decode_mode_spec_buf;
21452 }
21453
21454 case ']':
21455 {
21456 int i;
21457 char *p;
21458
21459 if (command_loop_level > 5)
21460 return " ...]]]";
21461 p = decode_mode_spec_buf;
21462 for (i = 0; i < command_loop_level; i++)
21463 *p++ = ']';
21464 *p = 0;
21465 return decode_mode_spec_buf;
21466 }
21467
21468 case '-':
21469 {
21470 register int i;
21471
21472 /* Let lots_of_dashes be a string of infinite length. */
21473 if (mode_line_target == MODE_LINE_NOPROP
21474 || mode_line_target == MODE_LINE_STRING)
21475 return "--";
21476 if (field_width <= 0
21477 || field_width > sizeof (lots_of_dashes))
21478 {
21479 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21480 decode_mode_spec_buf[i] = '-';
21481 decode_mode_spec_buf[i] = '\0';
21482 return decode_mode_spec_buf;
21483 }
21484 else
21485 return lots_of_dashes;
21486 }
21487
21488 case 'b':
21489 obj = BVAR (b, name);
21490 break;
21491
21492 case 'c':
21493 /* %c and %l are ignored in `frame-title-format'.
21494 (In redisplay_internal, the frame title is drawn _before_ the
21495 windows are updated, so the stuff which depends on actual
21496 window contents (such as %l) may fail to render properly, or
21497 even crash emacs.) */
21498 if (mode_line_target == MODE_LINE_TITLE)
21499 return "";
21500 else
21501 {
21502 ptrdiff_t col = current_column ();
21503 wset_column_number_displayed (w, make_number (col));
21504 pint2str (decode_mode_spec_buf, width, col);
21505 return decode_mode_spec_buf;
21506 }
21507
21508 case 'e':
21509 #ifndef SYSTEM_MALLOC
21510 {
21511 if (NILP (Vmemory_full))
21512 return "";
21513 else
21514 return "!MEM FULL! ";
21515 }
21516 #else
21517 return "";
21518 #endif
21519
21520 case 'F':
21521 /* %F displays the frame name. */
21522 if (!NILP (f->title))
21523 return SSDATA (f->title);
21524 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21525 return SSDATA (f->name);
21526 return "Emacs";
21527
21528 case 'f':
21529 obj = BVAR (b, filename);
21530 break;
21531
21532 case 'i':
21533 {
21534 ptrdiff_t size = ZV - BEGV;
21535 pint2str (decode_mode_spec_buf, width, size);
21536 return decode_mode_spec_buf;
21537 }
21538
21539 case 'I':
21540 {
21541 ptrdiff_t size = ZV - BEGV;
21542 pint2hrstr (decode_mode_spec_buf, width, size);
21543 return decode_mode_spec_buf;
21544 }
21545
21546 case 'l':
21547 {
21548 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21549 ptrdiff_t topline, nlines, height;
21550 ptrdiff_t junk;
21551
21552 /* %c and %l are ignored in `frame-title-format'. */
21553 if (mode_line_target == MODE_LINE_TITLE)
21554 return "";
21555
21556 startpos = marker_position (w->start);
21557 startpos_byte = marker_byte_position (w->start);
21558 height = WINDOW_TOTAL_LINES (w);
21559
21560 /* If we decided that this buffer isn't suitable for line numbers,
21561 don't forget that too fast. */
21562 if (EQ (w->base_line_pos, w->buffer))
21563 goto no_value;
21564 /* But do forget it, if the window shows a different buffer now. */
21565 else if (BUFFERP (w->base_line_pos))
21566 wset_base_line_pos (w, Qnil);
21567
21568 /* If the buffer is very big, don't waste time. */
21569 if (INTEGERP (Vline_number_display_limit)
21570 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21571 {
21572 wset_base_line_pos (w, Qnil);
21573 wset_base_line_number (w, Qnil);
21574 goto no_value;
21575 }
21576
21577 if (INTEGERP (w->base_line_number)
21578 && INTEGERP (w->base_line_pos)
21579 && XFASTINT (w->base_line_pos) <= startpos)
21580 {
21581 line = XFASTINT (w->base_line_number);
21582 linepos = XFASTINT (w->base_line_pos);
21583 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21584 }
21585 else
21586 {
21587 line = 1;
21588 linepos = BUF_BEGV (b);
21589 linepos_byte = BUF_BEGV_BYTE (b);
21590 }
21591
21592 /* Count lines from base line to window start position. */
21593 nlines = display_count_lines (linepos_byte,
21594 startpos_byte,
21595 startpos, &junk);
21596
21597 topline = nlines + line;
21598
21599 /* Determine a new base line, if the old one is too close
21600 or too far away, or if we did not have one.
21601 "Too close" means it's plausible a scroll-down would
21602 go back past it. */
21603 if (startpos == BUF_BEGV (b))
21604 {
21605 wset_base_line_number (w, make_number (topline));
21606 wset_base_line_pos (w, make_number (BUF_BEGV (b)));
21607 }
21608 else if (nlines < height + 25 || nlines > height * 3 + 50
21609 || linepos == BUF_BEGV (b))
21610 {
21611 ptrdiff_t limit = BUF_BEGV (b);
21612 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21613 ptrdiff_t position;
21614 ptrdiff_t distance =
21615 (height * 2 + 30) * line_number_display_limit_width;
21616
21617 if (startpos - distance > limit)
21618 {
21619 limit = startpos - distance;
21620 limit_byte = CHAR_TO_BYTE (limit);
21621 }
21622
21623 nlines = display_count_lines (startpos_byte,
21624 limit_byte,
21625 - (height * 2 + 30),
21626 &position);
21627 /* If we couldn't find the lines we wanted within
21628 line_number_display_limit_width chars per line,
21629 give up on line numbers for this window. */
21630 if (position == limit_byte && limit == startpos - distance)
21631 {
21632 wset_base_line_pos (w, w->buffer);
21633 wset_base_line_number (w, Qnil);
21634 goto no_value;
21635 }
21636
21637 wset_base_line_number (w, make_number (topline - nlines));
21638 wset_base_line_pos (w, make_number (BYTE_TO_CHAR (position)));
21639 }
21640
21641 /* Now count lines from the start pos to point. */
21642 nlines = display_count_lines (startpos_byte,
21643 PT_BYTE, PT, &junk);
21644
21645 /* Record that we did display the line number. */
21646 line_number_displayed = 1;
21647
21648 /* Make the string to show. */
21649 pint2str (decode_mode_spec_buf, width, topline + nlines);
21650 return decode_mode_spec_buf;
21651 no_value:
21652 {
21653 char* p = decode_mode_spec_buf;
21654 int pad = width - 2;
21655 while (pad-- > 0)
21656 *p++ = ' ';
21657 *p++ = '?';
21658 *p++ = '?';
21659 *p = '\0';
21660 return decode_mode_spec_buf;
21661 }
21662 }
21663 break;
21664
21665 case 'm':
21666 obj = BVAR (b, mode_name);
21667 break;
21668
21669 case 'n':
21670 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21671 return " Narrow";
21672 break;
21673
21674 case 'p':
21675 {
21676 ptrdiff_t pos = marker_position (w->start);
21677 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21678
21679 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21680 {
21681 if (pos <= BUF_BEGV (b))
21682 return "All";
21683 else
21684 return "Bottom";
21685 }
21686 else if (pos <= BUF_BEGV (b))
21687 return "Top";
21688 else
21689 {
21690 if (total > 1000000)
21691 /* Do it differently for a large value, to avoid overflow. */
21692 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21693 else
21694 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21695 /* We can't normally display a 3-digit number,
21696 so get us a 2-digit number that is close. */
21697 if (total == 100)
21698 total = 99;
21699 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21700 return decode_mode_spec_buf;
21701 }
21702 }
21703
21704 /* Display percentage of size above the bottom of the screen. */
21705 case 'P':
21706 {
21707 ptrdiff_t toppos = marker_position (w->start);
21708 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21709 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21710
21711 if (botpos >= BUF_ZV (b))
21712 {
21713 if (toppos <= BUF_BEGV (b))
21714 return "All";
21715 else
21716 return "Bottom";
21717 }
21718 else
21719 {
21720 if (total > 1000000)
21721 /* Do it differently for a large value, to avoid overflow. */
21722 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21723 else
21724 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21725 /* We can't normally display a 3-digit number,
21726 so get us a 2-digit number that is close. */
21727 if (total == 100)
21728 total = 99;
21729 if (toppos <= BUF_BEGV (b))
21730 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21731 else
21732 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21733 return decode_mode_spec_buf;
21734 }
21735 }
21736
21737 case 's':
21738 /* status of process */
21739 obj = Fget_buffer_process (Fcurrent_buffer ());
21740 if (NILP (obj))
21741 return "no process";
21742 #ifndef MSDOS
21743 obj = Fsymbol_name (Fprocess_status (obj));
21744 #endif
21745 break;
21746
21747 case '@':
21748 {
21749 ptrdiff_t count = inhibit_garbage_collection ();
21750 Lisp_Object val = call1 (intern ("file-remote-p"),
21751 BVAR (current_buffer, directory));
21752 unbind_to (count, Qnil);
21753
21754 if (NILP (val))
21755 return "-";
21756 else
21757 return "@";
21758 }
21759
21760 case 't': /* indicate TEXT or BINARY */
21761 return "T";
21762
21763 case 'z':
21764 /* coding-system (not including end-of-line format) */
21765 case 'Z':
21766 /* coding-system (including end-of-line type) */
21767 {
21768 int eol_flag = (c == 'Z');
21769 char *p = decode_mode_spec_buf;
21770
21771 if (! FRAME_WINDOW_P (f))
21772 {
21773 /* No need to mention EOL here--the terminal never needs
21774 to do EOL conversion. */
21775 p = decode_mode_spec_coding (CODING_ID_NAME
21776 (FRAME_KEYBOARD_CODING (f)->id),
21777 p, 0);
21778 p = decode_mode_spec_coding (CODING_ID_NAME
21779 (FRAME_TERMINAL_CODING (f)->id),
21780 p, 0);
21781 }
21782 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21783 p, eol_flag);
21784
21785 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21786 #ifdef subprocesses
21787 obj = Fget_buffer_process (Fcurrent_buffer ());
21788 if (PROCESSP (obj))
21789 {
21790 p = decode_mode_spec_coding
21791 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21792 p = decode_mode_spec_coding
21793 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21794 }
21795 #endif /* subprocesses */
21796 #endif /* 0 */
21797 *p = 0;
21798 return decode_mode_spec_buf;
21799 }
21800 }
21801
21802 if (STRINGP (obj))
21803 {
21804 *string = obj;
21805 return SSDATA (obj);
21806 }
21807 else
21808 return "";
21809 }
21810
21811
21812 /* Count up to COUNT lines starting from START_BYTE.
21813 But don't go beyond LIMIT_BYTE.
21814 Return the number of lines thus found (always nonnegative).
21815
21816 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21817
21818 static ptrdiff_t
21819 display_count_lines (ptrdiff_t start_byte,
21820 ptrdiff_t limit_byte, ptrdiff_t count,
21821 ptrdiff_t *byte_pos_ptr)
21822 {
21823 register unsigned char *cursor;
21824 unsigned char *base;
21825
21826 register ptrdiff_t ceiling;
21827 register unsigned char *ceiling_addr;
21828 ptrdiff_t orig_count = count;
21829
21830 /* If we are not in selective display mode,
21831 check only for newlines. */
21832 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21833 && !INTEGERP (BVAR (current_buffer, selective_display)));
21834
21835 if (count > 0)
21836 {
21837 while (start_byte < limit_byte)
21838 {
21839 ceiling = BUFFER_CEILING_OF (start_byte);
21840 ceiling = min (limit_byte - 1, ceiling);
21841 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21842 base = (cursor = BYTE_POS_ADDR (start_byte));
21843 while (1)
21844 {
21845 if (selective_display)
21846 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21847 ;
21848 else
21849 while (*cursor != '\n' && ++cursor != ceiling_addr)
21850 ;
21851
21852 if (cursor != ceiling_addr)
21853 {
21854 if (--count == 0)
21855 {
21856 start_byte += cursor - base + 1;
21857 *byte_pos_ptr = start_byte;
21858 return orig_count;
21859 }
21860 else
21861 if (++cursor == ceiling_addr)
21862 break;
21863 }
21864 else
21865 break;
21866 }
21867 start_byte += cursor - base;
21868 }
21869 }
21870 else
21871 {
21872 while (start_byte > limit_byte)
21873 {
21874 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21875 ceiling = max (limit_byte, ceiling);
21876 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21877 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21878 while (1)
21879 {
21880 if (selective_display)
21881 while (--cursor != ceiling_addr
21882 && *cursor != '\n' && *cursor != 015)
21883 ;
21884 else
21885 while (--cursor != ceiling_addr && *cursor != '\n')
21886 ;
21887
21888 if (cursor != ceiling_addr)
21889 {
21890 if (++count == 0)
21891 {
21892 start_byte += cursor - base + 1;
21893 *byte_pos_ptr = start_byte;
21894 /* When scanning backwards, we should
21895 not count the newline posterior to which we stop. */
21896 return - orig_count - 1;
21897 }
21898 }
21899 else
21900 break;
21901 }
21902 /* Here we add 1 to compensate for the last decrement
21903 of CURSOR, which took it past the valid range. */
21904 start_byte += cursor - base + 1;
21905 }
21906 }
21907
21908 *byte_pos_ptr = limit_byte;
21909
21910 if (count < 0)
21911 return - orig_count + count;
21912 return orig_count - count;
21913
21914 }
21915
21916
21917 \f
21918 /***********************************************************************
21919 Displaying strings
21920 ***********************************************************************/
21921
21922 /* Display a NUL-terminated string, starting with index START.
21923
21924 If STRING is non-null, display that C string. Otherwise, the Lisp
21925 string LISP_STRING is displayed. There's a case that STRING is
21926 non-null and LISP_STRING is not nil. It means STRING is a string
21927 data of LISP_STRING. In that case, we display LISP_STRING while
21928 ignoring its text properties.
21929
21930 If FACE_STRING is not nil, FACE_STRING_POS is a position in
21931 FACE_STRING. Display STRING or LISP_STRING with the face at
21932 FACE_STRING_POS in FACE_STRING:
21933
21934 Display the string in the environment given by IT, but use the
21935 standard display table, temporarily.
21936
21937 FIELD_WIDTH is the minimum number of output glyphs to produce.
21938 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21939 with spaces. If STRING has more characters, more than FIELD_WIDTH
21940 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
21941
21942 PRECISION is the maximum number of characters to output from
21943 STRING. PRECISION < 0 means don't truncate the string.
21944
21945 This is roughly equivalent to printf format specifiers:
21946
21947 FIELD_WIDTH PRECISION PRINTF
21948 ----------------------------------------
21949 -1 -1 %s
21950 -1 10 %.10s
21951 10 -1 %10s
21952 20 10 %20.10s
21953
21954 MULTIBYTE zero means do not display multibyte chars, > 0 means do
21955 display them, and < 0 means obey the current buffer's value of
21956 enable_multibyte_characters.
21957
21958 Value is the number of columns displayed. */
21959
21960 static int
21961 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
21962 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
21963 int field_width, int precision, int max_x, int multibyte)
21964 {
21965 int hpos_at_start = it->hpos;
21966 int saved_face_id = it->face_id;
21967 struct glyph_row *row = it->glyph_row;
21968 ptrdiff_t it_charpos;
21969
21970 /* Initialize the iterator IT for iteration over STRING beginning
21971 with index START. */
21972 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
21973 precision, field_width, multibyte);
21974 if (string && STRINGP (lisp_string))
21975 /* LISP_STRING is the one returned by decode_mode_spec. We should
21976 ignore its text properties. */
21977 it->stop_charpos = it->end_charpos;
21978
21979 /* If displaying STRING, set up the face of the iterator from
21980 FACE_STRING, if that's given. */
21981 if (STRINGP (face_string))
21982 {
21983 ptrdiff_t endptr;
21984 struct face *face;
21985
21986 it->face_id
21987 = face_at_string_position (it->w, face_string, face_string_pos,
21988 0, it->region_beg_charpos,
21989 it->region_end_charpos,
21990 &endptr, it->base_face_id, 0);
21991 face = FACE_FROM_ID (it->f, it->face_id);
21992 it->face_box_p = face->box != FACE_NO_BOX;
21993 }
21994
21995 /* Set max_x to the maximum allowed X position. Don't let it go
21996 beyond the right edge of the window. */
21997 if (max_x <= 0)
21998 max_x = it->last_visible_x;
21999 else
22000 max_x = min (max_x, it->last_visible_x);
22001
22002 /* Skip over display elements that are not visible. because IT->w is
22003 hscrolled. */
22004 if (it->current_x < it->first_visible_x)
22005 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22006 MOVE_TO_POS | MOVE_TO_X);
22007
22008 row->ascent = it->max_ascent;
22009 row->height = it->max_ascent + it->max_descent;
22010 row->phys_ascent = it->max_phys_ascent;
22011 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22012 row->extra_line_spacing = it->max_extra_line_spacing;
22013
22014 if (STRINGP (it->string))
22015 it_charpos = IT_STRING_CHARPOS (*it);
22016 else
22017 it_charpos = IT_CHARPOS (*it);
22018
22019 /* This condition is for the case that we are called with current_x
22020 past last_visible_x. */
22021 while (it->current_x < max_x)
22022 {
22023 int x_before, x, n_glyphs_before, i, nglyphs;
22024
22025 /* Get the next display element. */
22026 if (!get_next_display_element (it))
22027 break;
22028
22029 /* Produce glyphs. */
22030 x_before = it->current_x;
22031 n_glyphs_before = row->used[TEXT_AREA];
22032 PRODUCE_GLYPHS (it);
22033
22034 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22035 i = 0;
22036 x = x_before;
22037 while (i < nglyphs)
22038 {
22039 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22040
22041 if (it->line_wrap != TRUNCATE
22042 && x + glyph->pixel_width > max_x)
22043 {
22044 /* End of continued line or max_x reached. */
22045 if (CHAR_GLYPH_PADDING_P (*glyph))
22046 {
22047 /* A wide character is unbreakable. */
22048 if (row->reversed_p)
22049 unproduce_glyphs (it, row->used[TEXT_AREA]
22050 - n_glyphs_before);
22051 row->used[TEXT_AREA] = n_glyphs_before;
22052 it->current_x = x_before;
22053 }
22054 else
22055 {
22056 if (row->reversed_p)
22057 unproduce_glyphs (it, row->used[TEXT_AREA]
22058 - (n_glyphs_before + i));
22059 row->used[TEXT_AREA] = n_glyphs_before + i;
22060 it->current_x = x;
22061 }
22062 break;
22063 }
22064 else if (x + glyph->pixel_width >= it->first_visible_x)
22065 {
22066 /* Glyph is at least partially visible. */
22067 ++it->hpos;
22068 if (x < it->first_visible_x)
22069 row->x = x - it->first_visible_x;
22070 }
22071 else
22072 {
22073 /* Glyph is off the left margin of the display area.
22074 Should not happen. */
22075 emacs_abort ();
22076 }
22077
22078 row->ascent = max (row->ascent, it->max_ascent);
22079 row->height = max (row->height, it->max_ascent + it->max_descent);
22080 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22081 row->phys_height = max (row->phys_height,
22082 it->max_phys_ascent + it->max_phys_descent);
22083 row->extra_line_spacing = max (row->extra_line_spacing,
22084 it->max_extra_line_spacing);
22085 x += glyph->pixel_width;
22086 ++i;
22087 }
22088
22089 /* Stop if max_x reached. */
22090 if (i < nglyphs)
22091 break;
22092
22093 /* Stop at line ends. */
22094 if (ITERATOR_AT_END_OF_LINE_P (it))
22095 {
22096 it->continuation_lines_width = 0;
22097 break;
22098 }
22099
22100 set_iterator_to_next (it, 1);
22101 if (STRINGP (it->string))
22102 it_charpos = IT_STRING_CHARPOS (*it);
22103 else
22104 it_charpos = IT_CHARPOS (*it);
22105
22106 /* Stop if truncating at the right edge. */
22107 if (it->line_wrap == TRUNCATE
22108 && it->current_x >= it->last_visible_x)
22109 {
22110 /* Add truncation mark, but don't do it if the line is
22111 truncated at a padding space. */
22112 if (it_charpos < it->string_nchars)
22113 {
22114 if (!FRAME_WINDOW_P (it->f))
22115 {
22116 int ii, n;
22117
22118 if (it->current_x > it->last_visible_x)
22119 {
22120 if (!row->reversed_p)
22121 {
22122 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22123 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22124 break;
22125 }
22126 else
22127 {
22128 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22129 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22130 break;
22131 unproduce_glyphs (it, ii + 1);
22132 ii = row->used[TEXT_AREA] - (ii + 1);
22133 }
22134 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22135 {
22136 row->used[TEXT_AREA] = ii;
22137 produce_special_glyphs (it, IT_TRUNCATION);
22138 }
22139 }
22140 produce_special_glyphs (it, IT_TRUNCATION);
22141 }
22142 row->truncated_on_right_p = 1;
22143 }
22144 break;
22145 }
22146 }
22147
22148 /* Maybe insert a truncation at the left. */
22149 if (it->first_visible_x
22150 && it_charpos > 0)
22151 {
22152 if (!FRAME_WINDOW_P (it->f)
22153 || (row->reversed_p
22154 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22155 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22156 insert_left_trunc_glyphs (it);
22157 row->truncated_on_left_p = 1;
22158 }
22159
22160 it->face_id = saved_face_id;
22161
22162 /* Value is number of columns displayed. */
22163 return it->hpos - hpos_at_start;
22164 }
22165
22166
22167 \f
22168 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22169 appears as an element of LIST or as the car of an element of LIST.
22170 If PROPVAL is a list, compare each element against LIST in that
22171 way, and return 1/2 if any element of PROPVAL is found in LIST.
22172 Otherwise return 0. This function cannot quit.
22173 The return value is 2 if the text is invisible but with an ellipsis
22174 and 1 if it's invisible and without an ellipsis. */
22175
22176 int
22177 invisible_p (register Lisp_Object propval, Lisp_Object list)
22178 {
22179 register Lisp_Object tail, proptail;
22180
22181 for (tail = list; CONSP (tail); tail = XCDR (tail))
22182 {
22183 register Lisp_Object tem;
22184 tem = XCAR (tail);
22185 if (EQ (propval, tem))
22186 return 1;
22187 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22188 return NILP (XCDR (tem)) ? 1 : 2;
22189 }
22190
22191 if (CONSP (propval))
22192 {
22193 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22194 {
22195 Lisp_Object propelt;
22196 propelt = XCAR (proptail);
22197 for (tail = list; CONSP (tail); tail = XCDR (tail))
22198 {
22199 register Lisp_Object tem;
22200 tem = XCAR (tail);
22201 if (EQ (propelt, tem))
22202 return 1;
22203 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22204 return NILP (XCDR (tem)) ? 1 : 2;
22205 }
22206 }
22207 }
22208
22209 return 0;
22210 }
22211
22212 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22213 doc: /* Non-nil if the property makes the text invisible.
22214 POS-OR-PROP can be a marker or number, in which case it is taken to be
22215 a position in the current buffer and the value of the `invisible' property
22216 is checked; or it can be some other value, which is then presumed to be the
22217 value of the `invisible' property of the text of interest.
22218 The non-nil value returned can be t for truly invisible text or something
22219 else if the text is replaced by an ellipsis. */)
22220 (Lisp_Object pos_or_prop)
22221 {
22222 Lisp_Object prop
22223 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22224 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22225 : pos_or_prop);
22226 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22227 return (invis == 0 ? Qnil
22228 : invis == 1 ? Qt
22229 : make_number (invis));
22230 }
22231
22232 /* Calculate a width or height in pixels from a specification using
22233 the following elements:
22234
22235 SPEC ::=
22236 NUM - a (fractional) multiple of the default font width/height
22237 (NUM) - specifies exactly NUM pixels
22238 UNIT - a fixed number of pixels, see below.
22239 ELEMENT - size of a display element in pixels, see below.
22240 (NUM . SPEC) - equals NUM * SPEC
22241 (+ SPEC SPEC ...) - add pixel values
22242 (- SPEC SPEC ...) - subtract pixel values
22243 (- SPEC) - negate pixel value
22244
22245 NUM ::=
22246 INT or FLOAT - a number constant
22247 SYMBOL - use symbol's (buffer local) variable binding.
22248
22249 UNIT ::=
22250 in - pixels per inch *)
22251 mm - pixels per 1/1000 meter *)
22252 cm - pixels per 1/100 meter *)
22253 width - width of current font in pixels.
22254 height - height of current font in pixels.
22255
22256 *) using the ratio(s) defined in display-pixels-per-inch.
22257
22258 ELEMENT ::=
22259
22260 left-fringe - left fringe width in pixels
22261 right-fringe - right fringe width in pixels
22262
22263 left-margin - left margin width in pixels
22264 right-margin - right margin width in pixels
22265
22266 scroll-bar - scroll-bar area width in pixels
22267
22268 Examples:
22269
22270 Pixels corresponding to 5 inches:
22271 (5 . in)
22272
22273 Total width of non-text areas on left side of window (if scroll-bar is on left):
22274 '(space :width (+ left-fringe left-margin scroll-bar))
22275
22276 Align to first text column (in header line):
22277 '(space :align-to 0)
22278
22279 Align to middle of text area minus half the width of variable `my-image'
22280 containing a loaded image:
22281 '(space :align-to (0.5 . (- text my-image)))
22282
22283 Width of left margin minus width of 1 character in the default font:
22284 '(space :width (- left-margin 1))
22285
22286 Width of left margin minus width of 2 characters in the current font:
22287 '(space :width (- left-margin (2 . width)))
22288
22289 Center 1 character over left-margin (in header line):
22290 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22291
22292 Different ways to express width of left fringe plus left margin minus one pixel:
22293 '(space :width (- (+ left-fringe left-margin) (1)))
22294 '(space :width (+ left-fringe left-margin (- (1))))
22295 '(space :width (+ left-fringe left-margin (-1)))
22296
22297 */
22298
22299 #define NUMVAL(X) \
22300 ((INTEGERP (X) || FLOATP (X)) \
22301 ? XFLOATINT (X) \
22302 : - 1)
22303
22304 static int
22305 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22306 struct font *font, int width_p, int *align_to)
22307 {
22308 double pixels;
22309
22310 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22311 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22312
22313 if (NILP (prop))
22314 return OK_PIXELS (0);
22315
22316 eassert (FRAME_LIVE_P (it->f));
22317
22318 if (SYMBOLP (prop))
22319 {
22320 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22321 {
22322 char *unit = SSDATA (SYMBOL_NAME (prop));
22323
22324 if (unit[0] == 'i' && unit[1] == 'n')
22325 pixels = 1.0;
22326 else if (unit[0] == 'm' && unit[1] == 'm')
22327 pixels = 25.4;
22328 else if (unit[0] == 'c' && unit[1] == 'm')
22329 pixels = 2.54;
22330 else
22331 pixels = 0;
22332 if (pixels > 0)
22333 {
22334 double ppi;
22335 #ifdef HAVE_WINDOW_SYSTEM
22336 if (FRAME_WINDOW_P (it->f)
22337 && (ppi = (width_p
22338 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22339 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22340 ppi > 0))
22341 return OK_PIXELS (ppi / pixels);
22342 #endif
22343
22344 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22345 || (CONSP (Vdisplay_pixels_per_inch)
22346 && (ppi = (width_p
22347 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22348 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22349 ppi > 0)))
22350 return OK_PIXELS (ppi / pixels);
22351
22352 return 0;
22353 }
22354 }
22355
22356 #ifdef HAVE_WINDOW_SYSTEM
22357 if (EQ (prop, Qheight))
22358 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22359 if (EQ (prop, Qwidth))
22360 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22361 #else
22362 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22363 return OK_PIXELS (1);
22364 #endif
22365
22366 if (EQ (prop, Qtext))
22367 return OK_PIXELS (width_p
22368 ? window_box_width (it->w, TEXT_AREA)
22369 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22370
22371 if (align_to && *align_to < 0)
22372 {
22373 *res = 0;
22374 if (EQ (prop, Qleft))
22375 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22376 if (EQ (prop, Qright))
22377 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22378 if (EQ (prop, Qcenter))
22379 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22380 + window_box_width (it->w, TEXT_AREA) / 2);
22381 if (EQ (prop, Qleft_fringe))
22382 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22383 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22384 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22385 if (EQ (prop, Qright_fringe))
22386 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22387 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22388 : window_box_right_offset (it->w, TEXT_AREA));
22389 if (EQ (prop, Qleft_margin))
22390 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22391 if (EQ (prop, Qright_margin))
22392 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22393 if (EQ (prop, Qscroll_bar))
22394 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22395 ? 0
22396 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22397 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22398 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22399 : 0)));
22400 }
22401 else
22402 {
22403 if (EQ (prop, Qleft_fringe))
22404 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22405 if (EQ (prop, Qright_fringe))
22406 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22407 if (EQ (prop, Qleft_margin))
22408 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22409 if (EQ (prop, Qright_margin))
22410 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22411 if (EQ (prop, Qscroll_bar))
22412 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22413 }
22414
22415 prop = buffer_local_value_1 (prop, it->w->buffer);
22416 if (EQ (prop, Qunbound))
22417 prop = Qnil;
22418 }
22419
22420 if (INTEGERP (prop) || FLOATP (prop))
22421 {
22422 int base_unit = (width_p
22423 ? FRAME_COLUMN_WIDTH (it->f)
22424 : FRAME_LINE_HEIGHT (it->f));
22425 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22426 }
22427
22428 if (CONSP (prop))
22429 {
22430 Lisp_Object car = XCAR (prop);
22431 Lisp_Object cdr = XCDR (prop);
22432
22433 if (SYMBOLP (car))
22434 {
22435 #ifdef HAVE_WINDOW_SYSTEM
22436 if (FRAME_WINDOW_P (it->f)
22437 && valid_image_p (prop))
22438 {
22439 ptrdiff_t id = lookup_image (it->f, prop);
22440 struct image *img = IMAGE_FROM_ID (it->f, id);
22441
22442 return OK_PIXELS (width_p ? img->width : img->height);
22443 }
22444 #endif
22445 if (EQ (car, Qplus) || EQ (car, Qminus))
22446 {
22447 int first = 1;
22448 double px;
22449
22450 pixels = 0;
22451 while (CONSP (cdr))
22452 {
22453 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22454 font, width_p, align_to))
22455 return 0;
22456 if (first)
22457 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22458 else
22459 pixels += px;
22460 cdr = XCDR (cdr);
22461 }
22462 if (EQ (car, Qminus))
22463 pixels = -pixels;
22464 return OK_PIXELS (pixels);
22465 }
22466
22467 car = buffer_local_value_1 (car, it->w->buffer);
22468 if (EQ (car, Qunbound))
22469 car = Qnil;
22470 }
22471
22472 if (INTEGERP (car) || FLOATP (car))
22473 {
22474 double fact;
22475 pixels = XFLOATINT (car);
22476 if (NILP (cdr))
22477 return OK_PIXELS (pixels);
22478 if (calc_pixel_width_or_height (&fact, it, cdr,
22479 font, width_p, align_to))
22480 return OK_PIXELS (pixels * fact);
22481 return 0;
22482 }
22483
22484 return 0;
22485 }
22486
22487 return 0;
22488 }
22489
22490 \f
22491 /***********************************************************************
22492 Glyph Display
22493 ***********************************************************************/
22494
22495 #ifdef HAVE_WINDOW_SYSTEM
22496
22497 #ifdef GLYPH_DEBUG
22498
22499 void
22500 dump_glyph_string (struct glyph_string *s)
22501 {
22502 fprintf (stderr, "glyph string\n");
22503 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22504 s->x, s->y, s->width, s->height);
22505 fprintf (stderr, " ybase = %d\n", s->ybase);
22506 fprintf (stderr, " hl = %d\n", s->hl);
22507 fprintf (stderr, " left overhang = %d, right = %d\n",
22508 s->left_overhang, s->right_overhang);
22509 fprintf (stderr, " nchars = %d\n", s->nchars);
22510 fprintf (stderr, " extends to end of line = %d\n",
22511 s->extends_to_end_of_line_p);
22512 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22513 fprintf (stderr, " bg width = %d\n", s->background_width);
22514 }
22515
22516 #endif /* GLYPH_DEBUG */
22517
22518 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22519 of XChar2b structures for S; it can't be allocated in
22520 init_glyph_string because it must be allocated via `alloca'. W
22521 is the window on which S is drawn. ROW and AREA are the glyph row
22522 and area within the row from which S is constructed. START is the
22523 index of the first glyph structure covered by S. HL is a
22524 face-override for drawing S. */
22525
22526 #ifdef HAVE_NTGUI
22527 #define OPTIONAL_HDC(hdc) HDC hdc,
22528 #define DECLARE_HDC(hdc) HDC hdc;
22529 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22530 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22531 #endif
22532
22533 #ifndef OPTIONAL_HDC
22534 #define OPTIONAL_HDC(hdc)
22535 #define DECLARE_HDC(hdc)
22536 #define ALLOCATE_HDC(hdc, f)
22537 #define RELEASE_HDC(hdc, f)
22538 #endif
22539
22540 static void
22541 init_glyph_string (struct glyph_string *s,
22542 OPTIONAL_HDC (hdc)
22543 XChar2b *char2b, struct window *w, struct glyph_row *row,
22544 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22545 {
22546 memset (s, 0, sizeof *s);
22547 s->w = w;
22548 s->f = XFRAME (w->frame);
22549 #ifdef HAVE_NTGUI
22550 s->hdc = hdc;
22551 #endif
22552 s->display = FRAME_X_DISPLAY (s->f);
22553 s->window = FRAME_X_WINDOW (s->f);
22554 s->char2b = char2b;
22555 s->hl = hl;
22556 s->row = row;
22557 s->area = area;
22558 s->first_glyph = row->glyphs[area] + start;
22559 s->height = row->height;
22560 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22561 s->ybase = s->y + row->ascent;
22562 }
22563
22564
22565 /* Append the list of glyph strings with head H and tail T to the list
22566 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22567
22568 static void
22569 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22570 struct glyph_string *h, struct glyph_string *t)
22571 {
22572 if (h)
22573 {
22574 if (*head)
22575 (*tail)->next = h;
22576 else
22577 *head = h;
22578 h->prev = *tail;
22579 *tail = t;
22580 }
22581 }
22582
22583
22584 /* Prepend the list of glyph strings with head H and tail T to the
22585 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22586 result. */
22587
22588 static void
22589 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22590 struct glyph_string *h, struct glyph_string *t)
22591 {
22592 if (h)
22593 {
22594 if (*head)
22595 (*head)->prev = t;
22596 else
22597 *tail = t;
22598 t->next = *head;
22599 *head = h;
22600 }
22601 }
22602
22603
22604 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22605 Set *HEAD and *TAIL to the resulting list. */
22606
22607 static void
22608 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22609 struct glyph_string *s)
22610 {
22611 s->next = s->prev = NULL;
22612 append_glyph_string_lists (head, tail, s, s);
22613 }
22614
22615
22616 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22617 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22618 make sure that X resources for the face returned are allocated.
22619 Value is a pointer to a realized face that is ready for display if
22620 DISPLAY_P is non-zero. */
22621
22622 static struct face *
22623 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22624 XChar2b *char2b, int display_p)
22625 {
22626 struct face *face = FACE_FROM_ID (f, face_id);
22627
22628 if (face->font)
22629 {
22630 unsigned code = face->font->driver->encode_char (face->font, c);
22631
22632 if (code != FONT_INVALID_CODE)
22633 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22634 else
22635 STORE_XCHAR2B (char2b, 0, 0);
22636 }
22637
22638 /* Make sure X resources of the face are allocated. */
22639 #ifdef HAVE_X_WINDOWS
22640 if (display_p)
22641 #endif
22642 {
22643 eassert (face != NULL);
22644 PREPARE_FACE_FOR_DISPLAY (f, face);
22645 }
22646
22647 return face;
22648 }
22649
22650
22651 /* Get face and two-byte form of character glyph GLYPH on frame F.
22652 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22653 a pointer to a realized face that is ready for display. */
22654
22655 static struct face *
22656 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22657 XChar2b *char2b, int *two_byte_p)
22658 {
22659 struct face *face;
22660
22661 eassert (glyph->type == CHAR_GLYPH);
22662 face = FACE_FROM_ID (f, glyph->face_id);
22663
22664 if (two_byte_p)
22665 *two_byte_p = 0;
22666
22667 if (face->font)
22668 {
22669 unsigned code;
22670
22671 if (CHAR_BYTE8_P (glyph->u.ch))
22672 code = CHAR_TO_BYTE8 (glyph->u.ch);
22673 else
22674 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22675
22676 if (code != FONT_INVALID_CODE)
22677 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22678 else
22679 STORE_XCHAR2B (char2b, 0, 0);
22680 }
22681
22682 /* Make sure X resources of the face are allocated. */
22683 eassert (face != NULL);
22684 PREPARE_FACE_FOR_DISPLAY (f, face);
22685 return face;
22686 }
22687
22688
22689 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22690 Return 1 if FONT has a glyph for C, otherwise return 0. */
22691
22692 static int
22693 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22694 {
22695 unsigned code;
22696
22697 if (CHAR_BYTE8_P (c))
22698 code = CHAR_TO_BYTE8 (c);
22699 else
22700 code = font->driver->encode_char (font, c);
22701
22702 if (code == FONT_INVALID_CODE)
22703 return 0;
22704 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22705 return 1;
22706 }
22707
22708
22709 /* Fill glyph string S with composition components specified by S->cmp.
22710
22711 BASE_FACE is the base face of the composition.
22712 S->cmp_from is the index of the first component for S.
22713
22714 OVERLAPS non-zero means S should draw the foreground only, and use
22715 its physical height for clipping. See also draw_glyphs.
22716
22717 Value is the index of a component not in S. */
22718
22719 static int
22720 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22721 int overlaps)
22722 {
22723 int i;
22724 /* For all glyphs of this composition, starting at the offset
22725 S->cmp_from, until we reach the end of the definition or encounter a
22726 glyph that requires the different face, add it to S. */
22727 struct face *face;
22728
22729 eassert (s);
22730
22731 s->for_overlaps = overlaps;
22732 s->face = NULL;
22733 s->font = NULL;
22734 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22735 {
22736 int c = COMPOSITION_GLYPH (s->cmp, i);
22737
22738 /* TAB in a composition means display glyphs with padding space
22739 on the left or right. */
22740 if (c != '\t')
22741 {
22742 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22743 -1, Qnil);
22744
22745 face = get_char_face_and_encoding (s->f, c, face_id,
22746 s->char2b + i, 1);
22747 if (face)
22748 {
22749 if (! s->face)
22750 {
22751 s->face = face;
22752 s->font = s->face->font;
22753 }
22754 else if (s->face != face)
22755 break;
22756 }
22757 }
22758 ++s->nchars;
22759 }
22760 s->cmp_to = i;
22761
22762 if (s->face == NULL)
22763 {
22764 s->face = base_face->ascii_face;
22765 s->font = s->face->font;
22766 }
22767
22768 /* All glyph strings for the same composition has the same width,
22769 i.e. the width set for the first component of the composition. */
22770 s->width = s->first_glyph->pixel_width;
22771
22772 /* If the specified font could not be loaded, use the frame's
22773 default font, but record the fact that we couldn't load it in
22774 the glyph string so that we can draw rectangles for the
22775 characters of the glyph string. */
22776 if (s->font == NULL)
22777 {
22778 s->font_not_found_p = 1;
22779 s->font = FRAME_FONT (s->f);
22780 }
22781
22782 /* Adjust base line for subscript/superscript text. */
22783 s->ybase += s->first_glyph->voffset;
22784
22785 /* This glyph string must always be drawn with 16-bit functions. */
22786 s->two_byte_p = 1;
22787
22788 return s->cmp_to;
22789 }
22790
22791 static int
22792 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22793 int start, int end, int overlaps)
22794 {
22795 struct glyph *glyph, *last;
22796 Lisp_Object lgstring;
22797 int i;
22798
22799 s->for_overlaps = overlaps;
22800 glyph = s->row->glyphs[s->area] + start;
22801 last = s->row->glyphs[s->area] + end;
22802 s->cmp_id = glyph->u.cmp.id;
22803 s->cmp_from = glyph->slice.cmp.from;
22804 s->cmp_to = glyph->slice.cmp.to + 1;
22805 s->face = FACE_FROM_ID (s->f, face_id);
22806 lgstring = composition_gstring_from_id (s->cmp_id);
22807 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22808 glyph++;
22809 while (glyph < last
22810 && glyph->u.cmp.automatic
22811 && glyph->u.cmp.id == s->cmp_id
22812 && s->cmp_to == glyph->slice.cmp.from)
22813 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22814
22815 for (i = s->cmp_from; i < s->cmp_to; i++)
22816 {
22817 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22818 unsigned code = LGLYPH_CODE (lglyph);
22819
22820 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22821 }
22822 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22823 return glyph - s->row->glyphs[s->area];
22824 }
22825
22826
22827 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22828 See the comment of fill_glyph_string for arguments.
22829 Value is the index of the first glyph not in S. */
22830
22831
22832 static int
22833 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22834 int start, int end, int overlaps)
22835 {
22836 struct glyph *glyph, *last;
22837 int voffset;
22838
22839 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22840 s->for_overlaps = overlaps;
22841 glyph = s->row->glyphs[s->area] + start;
22842 last = s->row->glyphs[s->area] + end;
22843 voffset = glyph->voffset;
22844 s->face = FACE_FROM_ID (s->f, face_id);
22845 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22846 s->nchars = 1;
22847 s->width = glyph->pixel_width;
22848 glyph++;
22849 while (glyph < last
22850 && glyph->type == GLYPHLESS_GLYPH
22851 && glyph->voffset == voffset
22852 && glyph->face_id == face_id)
22853 {
22854 s->nchars++;
22855 s->width += glyph->pixel_width;
22856 glyph++;
22857 }
22858 s->ybase += voffset;
22859 return glyph - s->row->glyphs[s->area];
22860 }
22861
22862
22863 /* Fill glyph string S from a sequence of character glyphs.
22864
22865 FACE_ID is the face id of the string. START is the index of the
22866 first glyph to consider, END is the index of the last + 1.
22867 OVERLAPS non-zero means S should draw the foreground only, and use
22868 its physical height for clipping. See also draw_glyphs.
22869
22870 Value is the index of the first glyph not in S. */
22871
22872 static int
22873 fill_glyph_string (struct glyph_string *s, int face_id,
22874 int start, int end, int overlaps)
22875 {
22876 struct glyph *glyph, *last;
22877 int voffset;
22878 int glyph_not_available_p;
22879
22880 eassert (s->f == XFRAME (s->w->frame));
22881 eassert (s->nchars == 0);
22882 eassert (start >= 0 && end > start);
22883
22884 s->for_overlaps = overlaps;
22885 glyph = s->row->glyphs[s->area] + start;
22886 last = s->row->glyphs[s->area] + end;
22887 voffset = glyph->voffset;
22888 s->padding_p = glyph->padding_p;
22889 glyph_not_available_p = glyph->glyph_not_available_p;
22890
22891 while (glyph < last
22892 && glyph->type == CHAR_GLYPH
22893 && glyph->voffset == voffset
22894 /* Same face id implies same font, nowadays. */
22895 && glyph->face_id == face_id
22896 && glyph->glyph_not_available_p == glyph_not_available_p)
22897 {
22898 int two_byte_p;
22899
22900 s->face = get_glyph_face_and_encoding (s->f, glyph,
22901 s->char2b + s->nchars,
22902 &two_byte_p);
22903 s->two_byte_p = two_byte_p;
22904 ++s->nchars;
22905 eassert (s->nchars <= end - start);
22906 s->width += glyph->pixel_width;
22907 if (glyph++->padding_p != s->padding_p)
22908 break;
22909 }
22910
22911 s->font = s->face->font;
22912
22913 /* If the specified font could not be loaded, use the frame's font,
22914 but record the fact that we couldn't load it in
22915 S->font_not_found_p so that we can draw rectangles for the
22916 characters of the glyph string. */
22917 if (s->font == NULL || glyph_not_available_p)
22918 {
22919 s->font_not_found_p = 1;
22920 s->font = FRAME_FONT (s->f);
22921 }
22922
22923 /* Adjust base line for subscript/superscript text. */
22924 s->ybase += voffset;
22925
22926 eassert (s->face && s->face->gc);
22927 return glyph - s->row->glyphs[s->area];
22928 }
22929
22930
22931 /* Fill glyph string S from image glyph S->first_glyph. */
22932
22933 static void
22934 fill_image_glyph_string (struct glyph_string *s)
22935 {
22936 eassert (s->first_glyph->type == IMAGE_GLYPH);
22937 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
22938 eassert (s->img);
22939 s->slice = s->first_glyph->slice.img;
22940 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
22941 s->font = s->face->font;
22942 s->width = s->first_glyph->pixel_width;
22943
22944 /* Adjust base line for subscript/superscript text. */
22945 s->ybase += s->first_glyph->voffset;
22946 }
22947
22948
22949 /* Fill glyph string S from a sequence of stretch glyphs.
22950
22951 START is the index of the first glyph to consider,
22952 END is the index of the last + 1.
22953
22954 Value is the index of the first glyph not in S. */
22955
22956 static int
22957 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
22958 {
22959 struct glyph *glyph, *last;
22960 int voffset, face_id;
22961
22962 eassert (s->first_glyph->type == STRETCH_GLYPH);
22963
22964 glyph = s->row->glyphs[s->area] + start;
22965 last = s->row->glyphs[s->area] + end;
22966 face_id = glyph->face_id;
22967 s->face = FACE_FROM_ID (s->f, face_id);
22968 s->font = s->face->font;
22969 s->width = glyph->pixel_width;
22970 s->nchars = 1;
22971 voffset = glyph->voffset;
22972
22973 for (++glyph;
22974 (glyph < last
22975 && glyph->type == STRETCH_GLYPH
22976 && glyph->voffset == voffset
22977 && glyph->face_id == face_id);
22978 ++glyph)
22979 s->width += glyph->pixel_width;
22980
22981 /* Adjust base line for subscript/superscript text. */
22982 s->ybase += voffset;
22983
22984 /* The case that face->gc == 0 is handled when drawing the glyph
22985 string by calling PREPARE_FACE_FOR_DISPLAY. */
22986 eassert (s->face);
22987 return glyph - s->row->glyphs[s->area];
22988 }
22989
22990 static struct font_metrics *
22991 get_per_char_metric (struct font *font, XChar2b *char2b)
22992 {
22993 static struct font_metrics metrics;
22994 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
22995
22996 if (! font || code == FONT_INVALID_CODE)
22997 return NULL;
22998 font->driver->text_extents (font, &code, 1, &metrics);
22999 return &metrics;
23000 }
23001
23002 /* EXPORT for RIF:
23003 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23004 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23005 assumed to be zero. */
23006
23007 void
23008 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23009 {
23010 *left = *right = 0;
23011
23012 if (glyph->type == CHAR_GLYPH)
23013 {
23014 struct face *face;
23015 XChar2b char2b;
23016 struct font_metrics *pcm;
23017
23018 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23019 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23020 {
23021 if (pcm->rbearing > pcm->width)
23022 *right = pcm->rbearing - pcm->width;
23023 if (pcm->lbearing < 0)
23024 *left = -pcm->lbearing;
23025 }
23026 }
23027 else if (glyph->type == COMPOSITE_GLYPH)
23028 {
23029 if (! glyph->u.cmp.automatic)
23030 {
23031 struct composition *cmp = composition_table[glyph->u.cmp.id];
23032
23033 if (cmp->rbearing > cmp->pixel_width)
23034 *right = cmp->rbearing - cmp->pixel_width;
23035 if (cmp->lbearing < 0)
23036 *left = - cmp->lbearing;
23037 }
23038 else
23039 {
23040 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23041 struct font_metrics metrics;
23042
23043 composition_gstring_width (gstring, glyph->slice.cmp.from,
23044 glyph->slice.cmp.to + 1, &metrics);
23045 if (metrics.rbearing > metrics.width)
23046 *right = metrics.rbearing - metrics.width;
23047 if (metrics.lbearing < 0)
23048 *left = - metrics.lbearing;
23049 }
23050 }
23051 }
23052
23053
23054 /* Return the index of the first glyph preceding glyph string S that
23055 is overwritten by S because of S's left overhang. Value is -1
23056 if no glyphs are overwritten. */
23057
23058 static int
23059 left_overwritten (struct glyph_string *s)
23060 {
23061 int k;
23062
23063 if (s->left_overhang)
23064 {
23065 int x = 0, i;
23066 struct glyph *glyphs = s->row->glyphs[s->area];
23067 int first = s->first_glyph - glyphs;
23068
23069 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23070 x -= glyphs[i].pixel_width;
23071
23072 k = i + 1;
23073 }
23074 else
23075 k = -1;
23076
23077 return k;
23078 }
23079
23080
23081 /* Return the index of the first glyph preceding glyph string S that
23082 is overwriting S because of its right overhang. Value is -1 if no
23083 glyph in front of S overwrites S. */
23084
23085 static int
23086 left_overwriting (struct glyph_string *s)
23087 {
23088 int i, k, x;
23089 struct glyph *glyphs = s->row->glyphs[s->area];
23090 int first = s->first_glyph - glyphs;
23091
23092 k = -1;
23093 x = 0;
23094 for (i = first - 1; i >= 0; --i)
23095 {
23096 int left, right;
23097 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23098 if (x + right > 0)
23099 k = i;
23100 x -= glyphs[i].pixel_width;
23101 }
23102
23103 return k;
23104 }
23105
23106
23107 /* Return the index of the last glyph following glyph string S that is
23108 overwritten by S because of S's right overhang. Value is -1 if
23109 no such glyph is found. */
23110
23111 static int
23112 right_overwritten (struct glyph_string *s)
23113 {
23114 int k = -1;
23115
23116 if (s->right_overhang)
23117 {
23118 int x = 0, i;
23119 struct glyph *glyphs = s->row->glyphs[s->area];
23120 int first = (s->first_glyph - glyphs
23121 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23122 int end = s->row->used[s->area];
23123
23124 for (i = first; i < end && s->right_overhang > x; ++i)
23125 x += glyphs[i].pixel_width;
23126
23127 k = i;
23128 }
23129
23130 return k;
23131 }
23132
23133
23134 /* Return the index of the last glyph following glyph string S that
23135 overwrites S because of its left overhang. Value is negative
23136 if no such glyph is found. */
23137
23138 static int
23139 right_overwriting (struct glyph_string *s)
23140 {
23141 int i, k, x;
23142 int end = s->row->used[s->area];
23143 struct glyph *glyphs = s->row->glyphs[s->area];
23144 int first = (s->first_glyph - glyphs
23145 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23146
23147 k = -1;
23148 x = 0;
23149 for (i = first; i < end; ++i)
23150 {
23151 int left, right;
23152 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23153 if (x - left < 0)
23154 k = i;
23155 x += glyphs[i].pixel_width;
23156 }
23157
23158 return k;
23159 }
23160
23161
23162 /* Set background width of glyph string S. START is the index of the
23163 first glyph following S. LAST_X is the right-most x-position + 1
23164 in the drawing area. */
23165
23166 static void
23167 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23168 {
23169 /* If the face of this glyph string has to be drawn to the end of
23170 the drawing area, set S->extends_to_end_of_line_p. */
23171
23172 if (start == s->row->used[s->area]
23173 && s->area == TEXT_AREA
23174 && ((s->row->fill_line_p
23175 && (s->hl == DRAW_NORMAL_TEXT
23176 || s->hl == DRAW_IMAGE_RAISED
23177 || s->hl == DRAW_IMAGE_SUNKEN))
23178 || s->hl == DRAW_MOUSE_FACE))
23179 s->extends_to_end_of_line_p = 1;
23180
23181 /* If S extends its face to the end of the line, set its
23182 background_width to the distance to the right edge of the drawing
23183 area. */
23184 if (s->extends_to_end_of_line_p)
23185 s->background_width = last_x - s->x + 1;
23186 else
23187 s->background_width = s->width;
23188 }
23189
23190
23191 /* Compute overhangs and x-positions for glyph string S and its
23192 predecessors, or successors. X is the starting x-position for S.
23193 BACKWARD_P non-zero means process predecessors. */
23194
23195 static void
23196 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23197 {
23198 if (backward_p)
23199 {
23200 while (s)
23201 {
23202 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23203 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23204 x -= s->width;
23205 s->x = x;
23206 s = s->prev;
23207 }
23208 }
23209 else
23210 {
23211 while (s)
23212 {
23213 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23214 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23215 s->x = x;
23216 x += s->width;
23217 s = s->next;
23218 }
23219 }
23220 }
23221
23222
23223
23224 /* The following macros are only called from draw_glyphs below.
23225 They reference the following parameters of that function directly:
23226 `w', `row', `area', and `overlap_p'
23227 as well as the following local variables:
23228 `s', `f', and `hdc' (in W32) */
23229
23230 #ifdef HAVE_NTGUI
23231 /* On W32, silently add local `hdc' variable to argument list of
23232 init_glyph_string. */
23233 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23234 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23235 #else
23236 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23237 init_glyph_string (s, char2b, w, row, area, start, hl)
23238 #endif
23239
23240 /* Add a glyph string for a stretch glyph to the list of strings
23241 between HEAD and TAIL. START is the index of the stretch glyph in
23242 row area AREA of glyph row ROW. END is the index of the last glyph
23243 in that glyph row area. X is the current output position assigned
23244 to the new glyph string constructed. HL overrides that face of the
23245 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23246 is the right-most x-position of the drawing area. */
23247
23248 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23249 and below -- keep them on one line. */
23250 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23251 do \
23252 { \
23253 s = alloca (sizeof *s); \
23254 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23255 START = fill_stretch_glyph_string (s, START, END); \
23256 append_glyph_string (&HEAD, &TAIL, s); \
23257 s->x = (X); \
23258 } \
23259 while (0)
23260
23261
23262 /* Add a glyph string for an image glyph to the list of strings
23263 between HEAD and TAIL. START is the index of the image glyph in
23264 row area AREA of glyph row ROW. END is the index of the last glyph
23265 in that glyph row area. X is the current output position assigned
23266 to the new glyph string constructed. HL overrides that face of the
23267 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23268 is the right-most x-position of the drawing area. */
23269
23270 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23271 do \
23272 { \
23273 s = alloca (sizeof *s); \
23274 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23275 fill_image_glyph_string (s); \
23276 append_glyph_string (&HEAD, &TAIL, s); \
23277 ++START; \
23278 s->x = (X); \
23279 } \
23280 while (0)
23281
23282
23283 /* Add a glyph string for a sequence of character glyphs to the list
23284 of strings between HEAD and TAIL. START is the index of the first
23285 glyph in row area AREA of glyph row ROW that is part of the new
23286 glyph string. END is the index of the last glyph in that glyph row
23287 area. X is the current output position assigned to the new glyph
23288 string constructed. HL overrides that face of the glyph; e.g. it
23289 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23290 right-most x-position of the drawing area. */
23291
23292 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23293 do \
23294 { \
23295 int face_id; \
23296 XChar2b *char2b; \
23297 \
23298 face_id = (row)->glyphs[area][START].face_id; \
23299 \
23300 s = alloca (sizeof *s); \
23301 char2b = alloca ((END - START) * sizeof *char2b); \
23302 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23303 append_glyph_string (&HEAD, &TAIL, s); \
23304 s->x = (X); \
23305 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23306 } \
23307 while (0)
23308
23309
23310 /* Add a glyph string for a composite sequence to the list of strings
23311 between HEAD and TAIL. START is the index of the first glyph in
23312 row area AREA of glyph row ROW that is part of the new glyph
23313 string. END is the index of the last glyph in that glyph row area.
23314 X is the current output position assigned to the new glyph string
23315 constructed. HL overrides that face of the glyph; e.g. it is
23316 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23317 x-position of the drawing area. */
23318
23319 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23320 do { \
23321 int face_id = (row)->glyphs[area][START].face_id; \
23322 struct face *base_face = FACE_FROM_ID (f, face_id); \
23323 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23324 struct composition *cmp = composition_table[cmp_id]; \
23325 XChar2b *char2b; \
23326 struct glyph_string *first_s = NULL; \
23327 int n; \
23328 \
23329 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23330 \
23331 /* Make glyph_strings for each glyph sequence that is drawable by \
23332 the same face, and append them to HEAD/TAIL. */ \
23333 for (n = 0; n < cmp->glyph_len;) \
23334 { \
23335 s = alloca (sizeof *s); \
23336 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23337 append_glyph_string (&(HEAD), &(TAIL), s); \
23338 s->cmp = cmp; \
23339 s->cmp_from = n; \
23340 s->x = (X); \
23341 if (n == 0) \
23342 first_s = s; \
23343 n = fill_composite_glyph_string (s, base_face, overlaps); \
23344 } \
23345 \
23346 ++START; \
23347 s = first_s; \
23348 } while (0)
23349
23350
23351 /* Add a glyph string for a glyph-string sequence to the list of strings
23352 between HEAD and TAIL. */
23353
23354 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23355 do { \
23356 int face_id; \
23357 XChar2b *char2b; \
23358 Lisp_Object gstring; \
23359 \
23360 face_id = (row)->glyphs[area][START].face_id; \
23361 gstring = (composition_gstring_from_id \
23362 ((row)->glyphs[area][START].u.cmp.id)); \
23363 s = alloca (sizeof *s); \
23364 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23365 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23366 append_glyph_string (&(HEAD), &(TAIL), s); \
23367 s->x = (X); \
23368 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23369 } while (0)
23370
23371
23372 /* Add a glyph string for a sequence of glyphless character's glyphs
23373 to the list of strings between HEAD and TAIL. The meanings of
23374 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23375
23376 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23377 do \
23378 { \
23379 int face_id; \
23380 \
23381 face_id = (row)->glyphs[area][START].face_id; \
23382 \
23383 s = alloca (sizeof *s); \
23384 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23385 append_glyph_string (&HEAD, &TAIL, s); \
23386 s->x = (X); \
23387 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23388 overlaps); \
23389 } \
23390 while (0)
23391
23392
23393 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23394 of AREA of glyph row ROW on window W between indices START and END.
23395 HL overrides the face for drawing glyph strings, e.g. it is
23396 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23397 x-positions of the drawing area.
23398
23399 This is an ugly monster macro construct because we must use alloca
23400 to allocate glyph strings (because draw_glyphs can be called
23401 asynchronously). */
23402
23403 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23404 do \
23405 { \
23406 HEAD = TAIL = NULL; \
23407 while (START < END) \
23408 { \
23409 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23410 switch (first_glyph->type) \
23411 { \
23412 case CHAR_GLYPH: \
23413 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23414 HL, X, LAST_X); \
23415 break; \
23416 \
23417 case COMPOSITE_GLYPH: \
23418 if (first_glyph->u.cmp.automatic) \
23419 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23420 HL, X, LAST_X); \
23421 else \
23422 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23423 HL, X, LAST_X); \
23424 break; \
23425 \
23426 case STRETCH_GLYPH: \
23427 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23428 HL, X, LAST_X); \
23429 break; \
23430 \
23431 case IMAGE_GLYPH: \
23432 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23433 HL, X, LAST_X); \
23434 break; \
23435 \
23436 case GLYPHLESS_GLYPH: \
23437 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23438 HL, X, LAST_X); \
23439 break; \
23440 \
23441 default: \
23442 emacs_abort (); \
23443 } \
23444 \
23445 if (s) \
23446 { \
23447 set_glyph_string_background_width (s, START, LAST_X); \
23448 (X) += s->width; \
23449 } \
23450 } \
23451 } while (0)
23452
23453
23454 /* Draw glyphs between START and END in AREA of ROW on window W,
23455 starting at x-position X. X is relative to AREA in W. HL is a
23456 face-override with the following meaning:
23457
23458 DRAW_NORMAL_TEXT draw normally
23459 DRAW_CURSOR draw in cursor face
23460 DRAW_MOUSE_FACE draw in mouse face.
23461 DRAW_INVERSE_VIDEO draw in mode line face
23462 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23463 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23464
23465 If OVERLAPS is non-zero, draw only the foreground of characters and
23466 clip to the physical height of ROW. Non-zero value also defines
23467 the overlapping part to be drawn:
23468
23469 OVERLAPS_PRED overlap with preceding rows
23470 OVERLAPS_SUCC overlap with succeeding rows
23471 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23472 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23473
23474 Value is the x-position reached, relative to AREA of W. */
23475
23476 static int
23477 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23478 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23479 enum draw_glyphs_face hl, int overlaps)
23480 {
23481 struct glyph_string *head, *tail;
23482 struct glyph_string *s;
23483 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23484 int i, j, x_reached, last_x, area_left = 0;
23485 struct frame *f = XFRAME (WINDOW_FRAME (w));
23486 DECLARE_HDC (hdc);
23487
23488 ALLOCATE_HDC (hdc, f);
23489
23490 /* Let's rather be paranoid than getting a SEGV. */
23491 end = min (end, row->used[area]);
23492 start = clip_to_bounds (0, start, end);
23493
23494 /* Translate X to frame coordinates. Set last_x to the right
23495 end of the drawing area. */
23496 if (row->full_width_p)
23497 {
23498 /* X is relative to the left edge of W, without scroll bars
23499 or fringes. */
23500 area_left = WINDOW_LEFT_EDGE_X (w);
23501 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23502 }
23503 else
23504 {
23505 area_left = window_box_left (w, area);
23506 last_x = area_left + window_box_width (w, area);
23507 }
23508 x += area_left;
23509
23510 /* Build a doubly-linked list of glyph_string structures between
23511 head and tail from what we have to draw. Note that the macro
23512 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23513 the reason we use a separate variable `i'. */
23514 i = start;
23515 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23516 if (tail)
23517 x_reached = tail->x + tail->background_width;
23518 else
23519 x_reached = x;
23520
23521 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23522 the row, redraw some glyphs in front or following the glyph
23523 strings built above. */
23524 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23525 {
23526 struct glyph_string *h, *t;
23527 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23528 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23529 int check_mouse_face = 0;
23530 int dummy_x = 0;
23531
23532 /* If mouse highlighting is on, we may need to draw adjacent
23533 glyphs using mouse-face highlighting. */
23534 if (area == TEXT_AREA && row->mouse_face_p
23535 && hlinfo->mouse_face_beg_row >= 0
23536 && hlinfo->mouse_face_end_row >= 0)
23537 {
23538 struct glyph_row *mouse_beg_row, *mouse_end_row;
23539
23540 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23541 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23542
23543 if (row >= mouse_beg_row && row <= mouse_end_row)
23544 {
23545 check_mouse_face = 1;
23546 mouse_beg_col = (row == mouse_beg_row)
23547 ? hlinfo->mouse_face_beg_col : 0;
23548 mouse_end_col = (row == mouse_end_row)
23549 ? hlinfo->mouse_face_end_col
23550 : row->used[TEXT_AREA];
23551 }
23552 }
23553
23554 /* Compute overhangs for all glyph strings. */
23555 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23556 for (s = head; s; s = s->next)
23557 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23558
23559 /* Prepend glyph strings for glyphs in front of the first glyph
23560 string that are overwritten because of the first glyph
23561 string's left overhang. The background of all strings
23562 prepended must be drawn because the first glyph string
23563 draws over it. */
23564 i = left_overwritten (head);
23565 if (i >= 0)
23566 {
23567 enum draw_glyphs_face overlap_hl;
23568
23569 /* If this row contains mouse highlighting, attempt to draw
23570 the overlapped glyphs with the correct highlight. This
23571 code fails if the overlap encompasses more than one glyph
23572 and mouse-highlight spans only some of these glyphs.
23573 However, making it work perfectly involves a lot more
23574 code, and I don't know if the pathological case occurs in
23575 practice, so we'll stick to this for now. --- cyd */
23576 if (check_mouse_face
23577 && mouse_beg_col < start && mouse_end_col > i)
23578 overlap_hl = DRAW_MOUSE_FACE;
23579 else
23580 overlap_hl = DRAW_NORMAL_TEXT;
23581
23582 j = i;
23583 BUILD_GLYPH_STRINGS (j, start, h, t,
23584 overlap_hl, dummy_x, last_x);
23585 start = i;
23586 compute_overhangs_and_x (t, head->x, 1);
23587 prepend_glyph_string_lists (&head, &tail, h, t);
23588 clip_head = head;
23589 }
23590
23591 /* Prepend glyph strings for glyphs in front of the first glyph
23592 string that overwrite that glyph string because of their
23593 right overhang. For these strings, only the foreground must
23594 be drawn, because it draws over the glyph string at `head'.
23595 The background must not be drawn because this would overwrite
23596 right overhangs of preceding glyphs for which no glyph
23597 strings exist. */
23598 i = left_overwriting (head);
23599 if (i >= 0)
23600 {
23601 enum draw_glyphs_face overlap_hl;
23602
23603 if (check_mouse_face
23604 && mouse_beg_col < start && mouse_end_col > i)
23605 overlap_hl = DRAW_MOUSE_FACE;
23606 else
23607 overlap_hl = DRAW_NORMAL_TEXT;
23608
23609 clip_head = head;
23610 BUILD_GLYPH_STRINGS (i, start, h, t,
23611 overlap_hl, dummy_x, last_x);
23612 for (s = h; s; s = s->next)
23613 s->background_filled_p = 1;
23614 compute_overhangs_and_x (t, head->x, 1);
23615 prepend_glyph_string_lists (&head, &tail, h, t);
23616 }
23617
23618 /* Append glyphs strings for glyphs following the last glyph
23619 string tail that are overwritten by tail. The background of
23620 these strings has to be drawn because tail's foreground draws
23621 over it. */
23622 i = right_overwritten (tail);
23623 if (i >= 0)
23624 {
23625 enum draw_glyphs_face overlap_hl;
23626
23627 if (check_mouse_face
23628 && mouse_beg_col < i && mouse_end_col > end)
23629 overlap_hl = DRAW_MOUSE_FACE;
23630 else
23631 overlap_hl = DRAW_NORMAL_TEXT;
23632
23633 BUILD_GLYPH_STRINGS (end, i, h, t,
23634 overlap_hl, x, last_x);
23635 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23636 we don't have `end = i;' here. */
23637 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23638 append_glyph_string_lists (&head, &tail, h, t);
23639 clip_tail = tail;
23640 }
23641
23642 /* Append glyph strings for glyphs following the last glyph
23643 string tail that overwrite tail. The foreground of such
23644 glyphs has to be drawn because it writes into the background
23645 of tail. The background must not be drawn because it could
23646 paint over the foreground of following glyphs. */
23647 i = right_overwriting (tail);
23648 if (i >= 0)
23649 {
23650 enum draw_glyphs_face overlap_hl;
23651 if (check_mouse_face
23652 && mouse_beg_col < i && mouse_end_col > end)
23653 overlap_hl = DRAW_MOUSE_FACE;
23654 else
23655 overlap_hl = DRAW_NORMAL_TEXT;
23656
23657 clip_tail = tail;
23658 i++; /* We must include the Ith glyph. */
23659 BUILD_GLYPH_STRINGS (end, i, h, t,
23660 overlap_hl, x, last_x);
23661 for (s = h; s; s = s->next)
23662 s->background_filled_p = 1;
23663 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23664 append_glyph_string_lists (&head, &tail, h, t);
23665 }
23666 if (clip_head || clip_tail)
23667 for (s = head; s; s = s->next)
23668 {
23669 s->clip_head = clip_head;
23670 s->clip_tail = clip_tail;
23671 }
23672 }
23673
23674 /* Draw all strings. */
23675 for (s = head; s; s = s->next)
23676 FRAME_RIF (f)->draw_glyph_string (s);
23677
23678 #ifndef HAVE_NS
23679 /* When focus a sole frame and move horizontally, this sets on_p to 0
23680 causing a failure to erase prev cursor position. */
23681 if (area == TEXT_AREA
23682 && !row->full_width_p
23683 /* When drawing overlapping rows, only the glyph strings'
23684 foreground is drawn, which doesn't erase a cursor
23685 completely. */
23686 && !overlaps)
23687 {
23688 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23689 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23690 : (tail ? tail->x + tail->background_width : x));
23691 x0 -= area_left;
23692 x1 -= area_left;
23693
23694 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23695 row->y, MATRIX_ROW_BOTTOM_Y (row));
23696 }
23697 #endif
23698
23699 /* Value is the x-position up to which drawn, relative to AREA of W.
23700 This doesn't include parts drawn because of overhangs. */
23701 if (row->full_width_p)
23702 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23703 else
23704 x_reached -= area_left;
23705
23706 RELEASE_HDC (hdc, f);
23707
23708 return x_reached;
23709 }
23710
23711 /* Expand row matrix if too narrow. Don't expand if area
23712 is not present. */
23713
23714 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23715 { \
23716 if (!fonts_changed_p \
23717 && (it->glyph_row->glyphs[area] \
23718 < it->glyph_row->glyphs[area + 1])) \
23719 { \
23720 it->w->ncols_scale_factor++; \
23721 fonts_changed_p = 1; \
23722 } \
23723 }
23724
23725 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23726 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23727
23728 static void
23729 append_glyph (struct it *it)
23730 {
23731 struct glyph *glyph;
23732 enum glyph_row_area area = it->area;
23733
23734 eassert (it->glyph_row);
23735 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23736
23737 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23738 if (glyph < it->glyph_row->glyphs[area + 1])
23739 {
23740 /* If the glyph row is reversed, we need to prepend the glyph
23741 rather than append it. */
23742 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23743 {
23744 struct glyph *g;
23745
23746 /* Make room for the additional glyph. */
23747 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23748 g[1] = *g;
23749 glyph = it->glyph_row->glyphs[area];
23750 }
23751 glyph->charpos = CHARPOS (it->position);
23752 glyph->object = it->object;
23753 if (it->pixel_width > 0)
23754 {
23755 glyph->pixel_width = it->pixel_width;
23756 glyph->padding_p = 0;
23757 }
23758 else
23759 {
23760 /* Assure at least 1-pixel width. Otherwise, cursor can't
23761 be displayed correctly. */
23762 glyph->pixel_width = 1;
23763 glyph->padding_p = 1;
23764 }
23765 glyph->ascent = it->ascent;
23766 glyph->descent = it->descent;
23767 glyph->voffset = it->voffset;
23768 glyph->type = CHAR_GLYPH;
23769 glyph->avoid_cursor_p = it->avoid_cursor_p;
23770 glyph->multibyte_p = it->multibyte_p;
23771 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23772 {
23773 /* In R2L rows, the left and the right box edges need to be
23774 drawn in reverse direction. */
23775 glyph->right_box_line_p = it->start_of_box_run_p;
23776 glyph->left_box_line_p = it->end_of_box_run_p;
23777 }
23778 else
23779 {
23780 glyph->left_box_line_p = it->start_of_box_run_p;
23781 glyph->right_box_line_p = it->end_of_box_run_p;
23782 }
23783 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23784 || it->phys_descent > it->descent);
23785 glyph->glyph_not_available_p = it->glyph_not_available_p;
23786 glyph->face_id = it->face_id;
23787 glyph->u.ch = it->char_to_display;
23788 glyph->slice.img = null_glyph_slice;
23789 glyph->font_type = FONT_TYPE_UNKNOWN;
23790 if (it->bidi_p)
23791 {
23792 glyph->resolved_level = it->bidi_it.resolved_level;
23793 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23794 emacs_abort ();
23795 glyph->bidi_type = it->bidi_it.type;
23796 }
23797 else
23798 {
23799 glyph->resolved_level = 0;
23800 glyph->bidi_type = UNKNOWN_BT;
23801 }
23802 ++it->glyph_row->used[area];
23803 }
23804 else
23805 IT_EXPAND_MATRIX_WIDTH (it, area);
23806 }
23807
23808 /* Store one glyph for the composition IT->cmp_it.id in
23809 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23810 non-null. */
23811
23812 static void
23813 append_composite_glyph (struct it *it)
23814 {
23815 struct glyph *glyph;
23816 enum glyph_row_area area = it->area;
23817
23818 eassert (it->glyph_row);
23819
23820 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23821 if (glyph < it->glyph_row->glyphs[area + 1])
23822 {
23823 /* If the glyph row is reversed, we need to prepend the glyph
23824 rather than append it. */
23825 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23826 {
23827 struct glyph *g;
23828
23829 /* Make room for the new glyph. */
23830 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23831 g[1] = *g;
23832 glyph = it->glyph_row->glyphs[it->area];
23833 }
23834 glyph->charpos = it->cmp_it.charpos;
23835 glyph->object = it->object;
23836 glyph->pixel_width = it->pixel_width;
23837 glyph->ascent = it->ascent;
23838 glyph->descent = it->descent;
23839 glyph->voffset = it->voffset;
23840 glyph->type = COMPOSITE_GLYPH;
23841 if (it->cmp_it.ch < 0)
23842 {
23843 glyph->u.cmp.automatic = 0;
23844 glyph->u.cmp.id = it->cmp_it.id;
23845 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23846 }
23847 else
23848 {
23849 glyph->u.cmp.automatic = 1;
23850 glyph->u.cmp.id = it->cmp_it.id;
23851 glyph->slice.cmp.from = it->cmp_it.from;
23852 glyph->slice.cmp.to = it->cmp_it.to - 1;
23853 }
23854 glyph->avoid_cursor_p = it->avoid_cursor_p;
23855 glyph->multibyte_p = it->multibyte_p;
23856 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23857 {
23858 /* In R2L rows, the left and the right box edges need to be
23859 drawn in reverse direction. */
23860 glyph->right_box_line_p = it->start_of_box_run_p;
23861 glyph->left_box_line_p = it->end_of_box_run_p;
23862 }
23863 else
23864 {
23865 glyph->left_box_line_p = it->start_of_box_run_p;
23866 glyph->right_box_line_p = it->end_of_box_run_p;
23867 }
23868 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23869 || it->phys_descent > it->descent);
23870 glyph->padding_p = 0;
23871 glyph->glyph_not_available_p = 0;
23872 glyph->face_id = it->face_id;
23873 glyph->font_type = FONT_TYPE_UNKNOWN;
23874 if (it->bidi_p)
23875 {
23876 glyph->resolved_level = it->bidi_it.resolved_level;
23877 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23878 emacs_abort ();
23879 glyph->bidi_type = it->bidi_it.type;
23880 }
23881 ++it->glyph_row->used[area];
23882 }
23883 else
23884 IT_EXPAND_MATRIX_WIDTH (it, area);
23885 }
23886
23887
23888 /* Change IT->ascent and IT->height according to the setting of
23889 IT->voffset. */
23890
23891 static void
23892 take_vertical_position_into_account (struct it *it)
23893 {
23894 if (it->voffset)
23895 {
23896 if (it->voffset < 0)
23897 /* Increase the ascent so that we can display the text higher
23898 in the line. */
23899 it->ascent -= it->voffset;
23900 else
23901 /* Increase the descent so that we can display the text lower
23902 in the line. */
23903 it->descent += it->voffset;
23904 }
23905 }
23906
23907
23908 /* Produce glyphs/get display metrics for the image IT is loaded with.
23909 See the description of struct display_iterator in dispextern.h for
23910 an overview of struct display_iterator. */
23911
23912 static void
23913 produce_image_glyph (struct it *it)
23914 {
23915 struct image *img;
23916 struct face *face;
23917 int glyph_ascent, crop;
23918 struct glyph_slice slice;
23919
23920 eassert (it->what == IT_IMAGE);
23921
23922 face = FACE_FROM_ID (it->f, it->face_id);
23923 eassert (face);
23924 /* Make sure X resources of the face is loaded. */
23925 PREPARE_FACE_FOR_DISPLAY (it->f, face);
23926
23927 if (it->image_id < 0)
23928 {
23929 /* Fringe bitmap. */
23930 it->ascent = it->phys_ascent = 0;
23931 it->descent = it->phys_descent = 0;
23932 it->pixel_width = 0;
23933 it->nglyphs = 0;
23934 return;
23935 }
23936
23937 img = IMAGE_FROM_ID (it->f, it->image_id);
23938 eassert (img);
23939 /* Make sure X resources of the image is loaded. */
23940 prepare_image_for_display (it->f, img);
23941
23942 slice.x = slice.y = 0;
23943 slice.width = img->width;
23944 slice.height = img->height;
23945
23946 if (INTEGERP (it->slice.x))
23947 slice.x = XINT (it->slice.x);
23948 else if (FLOATP (it->slice.x))
23949 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
23950
23951 if (INTEGERP (it->slice.y))
23952 slice.y = XINT (it->slice.y);
23953 else if (FLOATP (it->slice.y))
23954 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
23955
23956 if (INTEGERP (it->slice.width))
23957 slice.width = XINT (it->slice.width);
23958 else if (FLOATP (it->slice.width))
23959 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
23960
23961 if (INTEGERP (it->slice.height))
23962 slice.height = XINT (it->slice.height);
23963 else if (FLOATP (it->slice.height))
23964 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
23965
23966 if (slice.x >= img->width)
23967 slice.x = img->width;
23968 if (slice.y >= img->height)
23969 slice.y = img->height;
23970 if (slice.x + slice.width >= img->width)
23971 slice.width = img->width - slice.x;
23972 if (slice.y + slice.height > img->height)
23973 slice.height = img->height - slice.y;
23974
23975 if (slice.width == 0 || slice.height == 0)
23976 return;
23977
23978 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
23979
23980 it->descent = slice.height - glyph_ascent;
23981 if (slice.y == 0)
23982 it->descent += img->vmargin;
23983 if (slice.y + slice.height == img->height)
23984 it->descent += img->vmargin;
23985 it->phys_descent = it->descent;
23986
23987 it->pixel_width = slice.width;
23988 if (slice.x == 0)
23989 it->pixel_width += img->hmargin;
23990 if (slice.x + slice.width == img->width)
23991 it->pixel_width += img->hmargin;
23992
23993 /* It's quite possible for images to have an ascent greater than
23994 their height, so don't get confused in that case. */
23995 if (it->descent < 0)
23996 it->descent = 0;
23997
23998 it->nglyphs = 1;
23999
24000 if (face->box != FACE_NO_BOX)
24001 {
24002 if (face->box_line_width > 0)
24003 {
24004 if (slice.y == 0)
24005 it->ascent += face->box_line_width;
24006 if (slice.y + slice.height == img->height)
24007 it->descent += face->box_line_width;
24008 }
24009
24010 if (it->start_of_box_run_p && slice.x == 0)
24011 it->pixel_width += eabs (face->box_line_width);
24012 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24013 it->pixel_width += eabs (face->box_line_width);
24014 }
24015
24016 take_vertical_position_into_account (it);
24017
24018 /* Automatically crop wide image glyphs at right edge so we can
24019 draw the cursor on same display row. */
24020 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24021 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24022 {
24023 it->pixel_width -= crop;
24024 slice.width -= crop;
24025 }
24026
24027 if (it->glyph_row)
24028 {
24029 struct glyph *glyph;
24030 enum glyph_row_area area = it->area;
24031
24032 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24033 if (glyph < it->glyph_row->glyphs[area + 1])
24034 {
24035 glyph->charpos = CHARPOS (it->position);
24036 glyph->object = it->object;
24037 glyph->pixel_width = it->pixel_width;
24038 glyph->ascent = glyph_ascent;
24039 glyph->descent = it->descent;
24040 glyph->voffset = it->voffset;
24041 glyph->type = IMAGE_GLYPH;
24042 glyph->avoid_cursor_p = it->avoid_cursor_p;
24043 glyph->multibyte_p = it->multibyte_p;
24044 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24045 {
24046 /* In R2L rows, the left and the right box edges need to be
24047 drawn in reverse direction. */
24048 glyph->right_box_line_p = it->start_of_box_run_p;
24049 glyph->left_box_line_p = it->end_of_box_run_p;
24050 }
24051 else
24052 {
24053 glyph->left_box_line_p = it->start_of_box_run_p;
24054 glyph->right_box_line_p = it->end_of_box_run_p;
24055 }
24056 glyph->overlaps_vertically_p = 0;
24057 glyph->padding_p = 0;
24058 glyph->glyph_not_available_p = 0;
24059 glyph->face_id = it->face_id;
24060 glyph->u.img_id = img->id;
24061 glyph->slice.img = slice;
24062 glyph->font_type = FONT_TYPE_UNKNOWN;
24063 if (it->bidi_p)
24064 {
24065 glyph->resolved_level = it->bidi_it.resolved_level;
24066 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24067 emacs_abort ();
24068 glyph->bidi_type = it->bidi_it.type;
24069 }
24070 ++it->glyph_row->used[area];
24071 }
24072 else
24073 IT_EXPAND_MATRIX_WIDTH (it, area);
24074 }
24075 }
24076
24077
24078 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24079 of the glyph, WIDTH and HEIGHT are the width and height of the
24080 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24081
24082 static void
24083 append_stretch_glyph (struct it *it, Lisp_Object object,
24084 int width, int height, int ascent)
24085 {
24086 struct glyph *glyph;
24087 enum glyph_row_area area = it->area;
24088
24089 eassert (ascent >= 0 && ascent <= height);
24090
24091 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24092 if (glyph < it->glyph_row->glyphs[area + 1])
24093 {
24094 /* If the glyph row is reversed, we need to prepend the glyph
24095 rather than append it. */
24096 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24097 {
24098 struct glyph *g;
24099
24100 /* Make room for the additional glyph. */
24101 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24102 g[1] = *g;
24103 glyph = it->glyph_row->glyphs[area];
24104 }
24105 glyph->charpos = CHARPOS (it->position);
24106 glyph->object = object;
24107 glyph->pixel_width = width;
24108 glyph->ascent = ascent;
24109 glyph->descent = height - ascent;
24110 glyph->voffset = it->voffset;
24111 glyph->type = STRETCH_GLYPH;
24112 glyph->avoid_cursor_p = it->avoid_cursor_p;
24113 glyph->multibyte_p = it->multibyte_p;
24114 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24115 {
24116 /* In R2L rows, the left and the right box edges need to be
24117 drawn in reverse direction. */
24118 glyph->right_box_line_p = it->start_of_box_run_p;
24119 glyph->left_box_line_p = it->end_of_box_run_p;
24120 }
24121 else
24122 {
24123 glyph->left_box_line_p = it->start_of_box_run_p;
24124 glyph->right_box_line_p = it->end_of_box_run_p;
24125 }
24126 glyph->overlaps_vertically_p = 0;
24127 glyph->padding_p = 0;
24128 glyph->glyph_not_available_p = 0;
24129 glyph->face_id = it->face_id;
24130 glyph->u.stretch.ascent = ascent;
24131 glyph->u.stretch.height = height;
24132 glyph->slice.img = null_glyph_slice;
24133 glyph->font_type = FONT_TYPE_UNKNOWN;
24134 if (it->bidi_p)
24135 {
24136 glyph->resolved_level = it->bidi_it.resolved_level;
24137 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24138 emacs_abort ();
24139 glyph->bidi_type = it->bidi_it.type;
24140 }
24141 else
24142 {
24143 glyph->resolved_level = 0;
24144 glyph->bidi_type = UNKNOWN_BT;
24145 }
24146 ++it->glyph_row->used[area];
24147 }
24148 else
24149 IT_EXPAND_MATRIX_WIDTH (it, area);
24150 }
24151
24152 #endif /* HAVE_WINDOW_SYSTEM */
24153
24154 /* Produce a stretch glyph for iterator IT. IT->object is the value
24155 of the glyph property displayed. The value must be a list
24156 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24157 being recognized:
24158
24159 1. `:width WIDTH' specifies that the space should be WIDTH *
24160 canonical char width wide. WIDTH may be an integer or floating
24161 point number.
24162
24163 2. `:relative-width FACTOR' specifies that the width of the stretch
24164 should be computed from the width of the first character having the
24165 `glyph' property, and should be FACTOR times that width.
24166
24167 3. `:align-to HPOS' specifies that the space should be wide enough
24168 to reach HPOS, a value in canonical character units.
24169
24170 Exactly one of the above pairs must be present.
24171
24172 4. `:height HEIGHT' specifies that the height of the stretch produced
24173 should be HEIGHT, measured in canonical character units.
24174
24175 5. `:relative-height FACTOR' specifies that the height of the
24176 stretch should be FACTOR times the height of the characters having
24177 the glyph property.
24178
24179 Either none or exactly one of 4 or 5 must be present.
24180
24181 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24182 of the stretch should be used for the ascent of the stretch.
24183 ASCENT must be in the range 0 <= ASCENT <= 100. */
24184
24185 void
24186 produce_stretch_glyph (struct it *it)
24187 {
24188 /* (space :width WIDTH :height HEIGHT ...) */
24189 Lisp_Object prop, plist;
24190 int width = 0, height = 0, align_to = -1;
24191 int zero_width_ok_p = 0;
24192 double tem;
24193 struct font *font = NULL;
24194
24195 #ifdef HAVE_WINDOW_SYSTEM
24196 int ascent = 0;
24197 int zero_height_ok_p = 0;
24198
24199 if (FRAME_WINDOW_P (it->f))
24200 {
24201 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24202 font = face->font ? face->font : FRAME_FONT (it->f);
24203 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24204 }
24205 #endif
24206
24207 /* List should start with `space'. */
24208 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24209 plist = XCDR (it->object);
24210
24211 /* Compute the width of the stretch. */
24212 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24213 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24214 {
24215 /* Absolute width `:width WIDTH' specified and valid. */
24216 zero_width_ok_p = 1;
24217 width = (int)tem;
24218 }
24219 #ifdef HAVE_WINDOW_SYSTEM
24220 else if (FRAME_WINDOW_P (it->f)
24221 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24222 {
24223 /* Relative width `:relative-width FACTOR' specified and valid.
24224 Compute the width of the characters having the `glyph'
24225 property. */
24226 struct it it2;
24227 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24228
24229 it2 = *it;
24230 if (it->multibyte_p)
24231 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24232 else
24233 {
24234 it2.c = it2.char_to_display = *p, it2.len = 1;
24235 if (! ASCII_CHAR_P (it2.c))
24236 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24237 }
24238
24239 it2.glyph_row = NULL;
24240 it2.what = IT_CHARACTER;
24241 x_produce_glyphs (&it2);
24242 width = NUMVAL (prop) * it2.pixel_width;
24243 }
24244 #endif /* HAVE_WINDOW_SYSTEM */
24245 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24246 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24247 {
24248 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24249 align_to = (align_to < 0
24250 ? 0
24251 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24252 else if (align_to < 0)
24253 align_to = window_box_left_offset (it->w, TEXT_AREA);
24254 width = max (0, (int)tem + align_to - it->current_x);
24255 zero_width_ok_p = 1;
24256 }
24257 else
24258 /* Nothing specified -> width defaults to canonical char width. */
24259 width = FRAME_COLUMN_WIDTH (it->f);
24260
24261 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24262 width = 1;
24263
24264 #ifdef HAVE_WINDOW_SYSTEM
24265 /* Compute height. */
24266 if (FRAME_WINDOW_P (it->f))
24267 {
24268 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24269 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24270 {
24271 height = (int)tem;
24272 zero_height_ok_p = 1;
24273 }
24274 else if (prop = Fplist_get (plist, QCrelative_height),
24275 NUMVAL (prop) > 0)
24276 height = FONT_HEIGHT (font) * NUMVAL (prop);
24277 else
24278 height = FONT_HEIGHT (font);
24279
24280 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24281 height = 1;
24282
24283 /* Compute percentage of height used for ascent. If
24284 `:ascent ASCENT' is present and valid, use that. Otherwise,
24285 derive the ascent from the font in use. */
24286 if (prop = Fplist_get (plist, QCascent),
24287 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24288 ascent = height * NUMVAL (prop) / 100.0;
24289 else if (!NILP (prop)
24290 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24291 ascent = min (max (0, (int)tem), height);
24292 else
24293 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24294 }
24295 else
24296 #endif /* HAVE_WINDOW_SYSTEM */
24297 height = 1;
24298
24299 if (width > 0 && it->line_wrap != TRUNCATE
24300 && it->current_x + width > it->last_visible_x)
24301 {
24302 width = it->last_visible_x - it->current_x;
24303 #ifdef HAVE_WINDOW_SYSTEM
24304 /* Subtract one more pixel from the stretch width, but only on
24305 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24306 width -= FRAME_WINDOW_P (it->f);
24307 #endif
24308 }
24309
24310 if (width > 0 && height > 0 && it->glyph_row)
24311 {
24312 Lisp_Object o_object = it->object;
24313 Lisp_Object object = it->stack[it->sp - 1].string;
24314 int n = width;
24315
24316 if (!STRINGP (object))
24317 object = it->w->buffer;
24318 #ifdef HAVE_WINDOW_SYSTEM
24319 if (FRAME_WINDOW_P (it->f))
24320 append_stretch_glyph (it, object, width, height, ascent);
24321 else
24322 #endif
24323 {
24324 it->object = object;
24325 it->char_to_display = ' ';
24326 it->pixel_width = it->len = 1;
24327 while (n--)
24328 tty_append_glyph (it);
24329 it->object = o_object;
24330 }
24331 }
24332
24333 it->pixel_width = width;
24334 #ifdef HAVE_WINDOW_SYSTEM
24335 if (FRAME_WINDOW_P (it->f))
24336 {
24337 it->ascent = it->phys_ascent = ascent;
24338 it->descent = it->phys_descent = height - it->ascent;
24339 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24340 take_vertical_position_into_account (it);
24341 }
24342 else
24343 #endif
24344 it->nglyphs = width;
24345 }
24346
24347 /* Get information about special display element WHAT in an
24348 environment described by IT. WHAT is one of IT_TRUNCATION or
24349 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24350 non-null glyph_row member. This function ensures that fields like
24351 face_id, c, len of IT are left untouched. */
24352
24353 static void
24354 produce_special_glyphs (struct it *it, enum display_element_type what)
24355 {
24356 struct it temp_it;
24357 Lisp_Object gc;
24358 GLYPH glyph;
24359
24360 temp_it = *it;
24361 temp_it.object = make_number (0);
24362 memset (&temp_it.current, 0, sizeof temp_it.current);
24363
24364 if (what == IT_CONTINUATION)
24365 {
24366 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24367 if (it->bidi_it.paragraph_dir == R2L)
24368 SET_GLYPH_FROM_CHAR (glyph, '/');
24369 else
24370 SET_GLYPH_FROM_CHAR (glyph, '\\');
24371 if (it->dp
24372 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24373 {
24374 /* FIXME: Should we mirror GC for R2L lines? */
24375 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24376 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24377 }
24378 }
24379 else if (what == IT_TRUNCATION)
24380 {
24381 /* Truncation glyph. */
24382 SET_GLYPH_FROM_CHAR (glyph, '$');
24383 if (it->dp
24384 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24385 {
24386 /* FIXME: Should we mirror GC for R2L lines? */
24387 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24388 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24389 }
24390 }
24391 else
24392 emacs_abort ();
24393
24394 #ifdef HAVE_WINDOW_SYSTEM
24395 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24396 is turned off, we precede the truncation/continuation glyphs by a
24397 stretch glyph whose width is computed such that these special
24398 glyphs are aligned at the window margin, even when very different
24399 fonts are used in different glyph rows. */
24400 if (FRAME_WINDOW_P (temp_it.f)
24401 /* init_iterator calls this with it->glyph_row == NULL, and it
24402 wants only the pixel width of the truncation/continuation
24403 glyphs. */
24404 && temp_it.glyph_row
24405 /* insert_left_trunc_glyphs calls us at the beginning of the
24406 row, and it has its own calculation of the stretch glyph
24407 width. */
24408 && temp_it.glyph_row->used[TEXT_AREA] > 0
24409 && (temp_it.glyph_row->reversed_p
24410 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24411 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24412 {
24413 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24414
24415 if (stretch_width > 0)
24416 {
24417 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24418 struct font *font =
24419 face->font ? face->font : FRAME_FONT (temp_it.f);
24420 int stretch_ascent =
24421 (((temp_it.ascent + temp_it.descent)
24422 * FONT_BASE (font)) / FONT_HEIGHT (font));
24423
24424 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24425 temp_it.ascent + temp_it.descent,
24426 stretch_ascent);
24427 }
24428 }
24429 #endif
24430
24431 temp_it.dp = NULL;
24432 temp_it.what = IT_CHARACTER;
24433 temp_it.len = 1;
24434 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24435 temp_it.face_id = GLYPH_FACE (glyph);
24436 temp_it.len = CHAR_BYTES (temp_it.c);
24437
24438 PRODUCE_GLYPHS (&temp_it);
24439 it->pixel_width = temp_it.pixel_width;
24440 it->nglyphs = temp_it.pixel_width;
24441 }
24442
24443 #ifdef HAVE_WINDOW_SYSTEM
24444
24445 /* Calculate line-height and line-spacing properties.
24446 An integer value specifies explicit pixel value.
24447 A float value specifies relative value to current face height.
24448 A cons (float . face-name) specifies relative value to
24449 height of specified face font.
24450
24451 Returns height in pixels, or nil. */
24452
24453
24454 static Lisp_Object
24455 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24456 int boff, int override)
24457 {
24458 Lisp_Object face_name = Qnil;
24459 int ascent, descent, height;
24460
24461 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24462 return val;
24463
24464 if (CONSP (val))
24465 {
24466 face_name = XCAR (val);
24467 val = XCDR (val);
24468 if (!NUMBERP (val))
24469 val = make_number (1);
24470 if (NILP (face_name))
24471 {
24472 height = it->ascent + it->descent;
24473 goto scale;
24474 }
24475 }
24476
24477 if (NILP (face_name))
24478 {
24479 font = FRAME_FONT (it->f);
24480 boff = FRAME_BASELINE_OFFSET (it->f);
24481 }
24482 else if (EQ (face_name, Qt))
24483 {
24484 override = 0;
24485 }
24486 else
24487 {
24488 int face_id;
24489 struct face *face;
24490
24491 face_id = lookup_named_face (it->f, face_name, 0);
24492 if (face_id < 0)
24493 return make_number (-1);
24494
24495 face = FACE_FROM_ID (it->f, face_id);
24496 font = face->font;
24497 if (font == NULL)
24498 return make_number (-1);
24499 boff = font->baseline_offset;
24500 if (font->vertical_centering)
24501 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24502 }
24503
24504 ascent = FONT_BASE (font) + boff;
24505 descent = FONT_DESCENT (font) - boff;
24506
24507 if (override)
24508 {
24509 it->override_ascent = ascent;
24510 it->override_descent = descent;
24511 it->override_boff = boff;
24512 }
24513
24514 height = ascent + descent;
24515
24516 scale:
24517 if (FLOATP (val))
24518 height = (int)(XFLOAT_DATA (val) * height);
24519 else if (INTEGERP (val))
24520 height *= XINT (val);
24521
24522 return make_number (height);
24523 }
24524
24525
24526 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24527 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24528 and only if this is for a character for which no font was found.
24529
24530 If the display method (it->glyphless_method) is
24531 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24532 length of the acronym or the hexadecimal string, UPPER_XOFF and
24533 UPPER_YOFF are pixel offsets for the upper part of the string,
24534 LOWER_XOFF and LOWER_YOFF are for the lower part.
24535
24536 For the other display methods, LEN through LOWER_YOFF are zero. */
24537
24538 static void
24539 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24540 short upper_xoff, short upper_yoff,
24541 short lower_xoff, short lower_yoff)
24542 {
24543 struct glyph *glyph;
24544 enum glyph_row_area area = it->area;
24545
24546 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24547 if (glyph < it->glyph_row->glyphs[area + 1])
24548 {
24549 /* If the glyph row is reversed, we need to prepend the glyph
24550 rather than append it. */
24551 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24552 {
24553 struct glyph *g;
24554
24555 /* Make room for the additional glyph. */
24556 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24557 g[1] = *g;
24558 glyph = it->glyph_row->glyphs[area];
24559 }
24560 glyph->charpos = CHARPOS (it->position);
24561 glyph->object = it->object;
24562 glyph->pixel_width = it->pixel_width;
24563 glyph->ascent = it->ascent;
24564 glyph->descent = it->descent;
24565 glyph->voffset = it->voffset;
24566 glyph->type = GLYPHLESS_GLYPH;
24567 glyph->u.glyphless.method = it->glyphless_method;
24568 glyph->u.glyphless.for_no_font = for_no_font;
24569 glyph->u.glyphless.len = len;
24570 glyph->u.glyphless.ch = it->c;
24571 glyph->slice.glyphless.upper_xoff = upper_xoff;
24572 glyph->slice.glyphless.upper_yoff = upper_yoff;
24573 glyph->slice.glyphless.lower_xoff = lower_xoff;
24574 glyph->slice.glyphless.lower_yoff = lower_yoff;
24575 glyph->avoid_cursor_p = it->avoid_cursor_p;
24576 glyph->multibyte_p = it->multibyte_p;
24577 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24578 {
24579 /* In R2L rows, the left and the right box edges need to be
24580 drawn in reverse direction. */
24581 glyph->right_box_line_p = it->start_of_box_run_p;
24582 glyph->left_box_line_p = it->end_of_box_run_p;
24583 }
24584 else
24585 {
24586 glyph->left_box_line_p = it->start_of_box_run_p;
24587 glyph->right_box_line_p = it->end_of_box_run_p;
24588 }
24589 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24590 || it->phys_descent > it->descent);
24591 glyph->padding_p = 0;
24592 glyph->glyph_not_available_p = 0;
24593 glyph->face_id = face_id;
24594 glyph->font_type = FONT_TYPE_UNKNOWN;
24595 if (it->bidi_p)
24596 {
24597 glyph->resolved_level = it->bidi_it.resolved_level;
24598 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24599 emacs_abort ();
24600 glyph->bidi_type = it->bidi_it.type;
24601 }
24602 ++it->glyph_row->used[area];
24603 }
24604 else
24605 IT_EXPAND_MATRIX_WIDTH (it, area);
24606 }
24607
24608
24609 /* Produce a glyph for a glyphless character for iterator IT.
24610 IT->glyphless_method specifies which method to use for displaying
24611 the character. See the description of enum
24612 glyphless_display_method in dispextern.h for the detail.
24613
24614 FOR_NO_FONT is nonzero if and only if this is for a character for
24615 which no font was found. ACRONYM, if non-nil, is an acronym string
24616 for the character. */
24617
24618 static void
24619 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24620 {
24621 int face_id;
24622 struct face *face;
24623 struct font *font;
24624 int base_width, base_height, width, height;
24625 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24626 int len;
24627
24628 /* Get the metrics of the base font. We always refer to the current
24629 ASCII face. */
24630 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24631 font = face->font ? face->font : FRAME_FONT (it->f);
24632 it->ascent = FONT_BASE (font) + font->baseline_offset;
24633 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24634 base_height = it->ascent + it->descent;
24635 base_width = font->average_width;
24636
24637 /* Get a face ID for the glyph by utilizing a cache (the same way as
24638 done for `escape-glyph' in get_next_display_element). */
24639 if (it->f == last_glyphless_glyph_frame
24640 && it->face_id == last_glyphless_glyph_face_id)
24641 {
24642 face_id = last_glyphless_glyph_merged_face_id;
24643 }
24644 else
24645 {
24646 /* Merge the `glyphless-char' face into the current face. */
24647 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24648 last_glyphless_glyph_frame = it->f;
24649 last_glyphless_glyph_face_id = it->face_id;
24650 last_glyphless_glyph_merged_face_id = face_id;
24651 }
24652
24653 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24654 {
24655 it->pixel_width = THIN_SPACE_WIDTH;
24656 len = 0;
24657 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24658 }
24659 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24660 {
24661 width = CHAR_WIDTH (it->c);
24662 if (width == 0)
24663 width = 1;
24664 else if (width > 4)
24665 width = 4;
24666 it->pixel_width = base_width * width;
24667 len = 0;
24668 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24669 }
24670 else
24671 {
24672 char buf[7];
24673 const char *str;
24674 unsigned int code[6];
24675 int upper_len;
24676 int ascent, descent;
24677 struct font_metrics metrics_upper, metrics_lower;
24678
24679 face = FACE_FROM_ID (it->f, face_id);
24680 font = face->font ? face->font : FRAME_FONT (it->f);
24681 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24682
24683 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24684 {
24685 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24686 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24687 if (CONSP (acronym))
24688 acronym = XCAR (acronym);
24689 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24690 }
24691 else
24692 {
24693 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24694 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24695 str = buf;
24696 }
24697 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24698 code[len] = font->driver->encode_char (font, str[len]);
24699 upper_len = (len + 1) / 2;
24700 font->driver->text_extents (font, code, upper_len,
24701 &metrics_upper);
24702 font->driver->text_extents (font, code + upper_len, len - upper_len,
24703 &metrics_lower);
24704
24705
24706
24707 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24708 width = max (metrics_upper.width, metrics_lower.width) + 4;
24709 upper_xoff = upper_yoff = 2; /* the typical case */
24710 if (base_width >= width)
24711 {
24712 /* Align the upper to the left, the lower to the right. */
24713 it->pixel_width = base_width;
24714 lower_xoff = base_width - 2 - metrics_lower.width;
24715 }
24716 else
24717 {
24718 /* Center the shorter one. */
24719 it->pixel_width = width;
24720 if (metrics_upper.width >= metrics_lower.width)
24721 lower_xoff = (width - metrics_lower.width) / 2;
24722 else
24723 {
24724 /* FIXME: This code doesn't look right. It formerly was
24725 missing the "lower_xoff = 0;", which couldn't have
24726 been right since it left lower_xoff uninitialized. */
24727 lower_xoff = 0;
24728 upper_xoff = (width - metrics_upper.width) / 2;
24729 }
24730 }
24731
24732 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24733 top, bottom, and between upper and lower strings. */
24734 height = (metrics_upper.ascent + metrics_upper.descent
24735 + metrics_lower.ascent + metrics_lower.descent) + 5;
24736 /* Center vertically.
24737 H:base_height, D:base_descent
24738 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24739
24740 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24741 descent = D - H/2 + h/2;
24742 lower_yoff = descent - 2 - ld;
24743 upper_yoff = lower_yoff - la - 1 - ud; */
24744 ascent = - (it->descent - (base_height + height + 1) / 2);
24745 descent = it->descent - (base_height - height) / 2;
24746 lower_yoff = descent - 2 - metrics_lower.descent;
24747 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24748 - metrics_upper.descent);
24749 /* Don't make the height shorter than the base height. */
24750 if (height > base_height)
24751 {
24752 it->ascent = ascent;
24753 it->descent = descent;
24754 }
24755 }
24756
24757 it->phys_ascent = it->ascent;
24758 it->phys_descent = it->descent;
24759 if (it->glyph_row)
24760 append_glyphless_glyph (it, face_id, for_no_font, len,
24761 upper_xoff, upper_yoff,
24762 lower_xoff, lower_yoff);
24763 it->nglyphs = 1;
24764 take_vertical_position_into_account (it);
24765 }
24766
24767
24768 /* RIF:
24769 Produce glyphs/get display metrics for the display element IT is
24770 loaded with. See the description of struct it in dispextern.h
24771 for an overview of struct it. */
24772
24773 void
24774 x_produce_glyphs (struct it *it)
24775 {
24776 int extra_line_spacing = it->extra_line_spacing;
24777
24778 it->glyph_not_available_p = 0;
24779
24780 if (it->what == IT_CHARACTER)
24781 {
24782 XChar2b char2b;
24783 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24784 struct font *font = face->font;
24785 struct font_metrics *pcm = NULL;
24786 int boff; /* baseline offset */
24787
24788 if (font == NULL)
24789 {
24790 /* When no suitable font is found, display this character by
24791 the method specified in the first extra slot of
24792 Vglyphless_char_display. */
24793 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24794
24795 eassert (it->what == IT_GLYPHLESS);
24796 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24797 goto done;
24798 }
24799
24800 boff = font->baseline_offset;
24801 if (font->vertical_centering)
24802 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24803
24804 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24805 {
24806 int stretched_p;
24807
24808 it->nglyphs = 1;
24809
24810 if (it->override_ascent >= 0)
24811 {
24812 it->ascent = it->override_ascent;
24813 it->descent = it->override_descent;
24814 boff = it->override_boff;
24815 }
24816 else
24817 {
24818 it->ascent = FONT_BASE (font) + boff;
24819 it->descent = FONT_DESCENT (font) - boff;
24820 }
24821
24822 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24823 {
24824 pcm = get_per_char_metric (font, &char2b);
24825 if (pcm->width == 0
24826 && pcm->rbearing == 0 && pcm->lbearing == 0)
24827 pcm = NULL;
24828 }
24829
24830 if (pcm)
24831 {
24832 it->phys_ascent = pcm->ascent + boff;
24833 it->phys_descent = pcm->descent - boff;
24834 it->pixel_width = pcm->width;
24835 }
24836 else
24837 {
24838 it->glyph_not_available_p = 1;
24839 it->phys_ascent = it->ascent;
24840 it->phys_descent = it->descent;
24841 it->pixel_width = font->space_width;
24842 }
24843
24844 if (it->constrain_row_ascent_descent_p)
24845 {
24846 if (it->descent > it->max_descent)
24847 {
24848 it->ascent += it->descent - it->max_descent;
24849 it->descent = it->max_descent;
24850 }
24851 if (it->ascent > it->max_ascent)
24852 {
24853 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24854 it->ascent = it->max_ascent;
24855 }
24856 it->phys_ascent = min (it->phys_ascent, it->ascent);
24857 it->phys_descent = min (it->phys_descent, it->descent);
24858 extra_line_spacing = 0;
24859 }
24860
24861 /* If this is a space inside a region of text with
24862 `space-width' property, change its width. */
24863 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
24864 if (stretched_p)
24865 it->pixel_width *= XFLOATINT (it->space_width);
24866
24867 /* If face has a box, add the box thickness to the character
24868 height. If character has a box line to the left and/or
24869 right, add the box line width to the character's width. */
24870 if (face->box != FACE_NO_BOX)
24871 {
24872 int thick = face->box_line_width;
24873
24874 if (thick > 0)
24875 {
24876 it->ascent += thick;
24877 it->descent += thick;
24878 }
24879 else
24880 thick = -thick;
24881
24882 if (it->start_of_box_run_p)
24883 it->pixel_width += thick;
24884 if (it->end_of_box_run_p)
24885 it->pixel_width += thick;
24886 }
24887
24888 /* If face has an overline, add the height of the overline
24889 (1 pixel) and a 1 pixel margin to the character height. */
24890 if (face->overline_p)
24891 it->ascent += overline_margin;
24892
24893 if (it->constrain_row_ascent_descent_p)
24894 {
24895 if (it->ascent > it->max_ascent)
24896 it->ascent = it->max_ascent;
24897 if (it->descent > it->max_descent)
24898 it->descent = it->max_descent;
24899 }
24900
24901 take_vertical_position_into_account (it);
24902
24903 /* If we have to actually produce glyphs, do it. */
24904 if (it->glyph_row)
24905 {
24906 if (stretched_p)
24907 {
24908 /* Translate a space with a `space-width' property
24909 into a stretch glyph. */
24910 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
24911 / FONT_HEIGHT (font));
24912 append_stretch_glyph (it, it->object, it->pixel_width,
24913 it->ascent + it->descent, ascent);
24914 }
24915 else
24916 append_glyph (it);
24917
24918 /* If characters with lbearing or rbearing are displayed
24919 in this line, record that fact in a flag of the
24920 glyph row. This is used to optimize X output code. */
24921 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
24922 it->glyph_row->contains_overlapping_glyphs_p = 1;
24923 }
24924 if (! stretched_p && it->pixel_width == 0)
24925 /* We assure that all visible glyphs have at least 1-pixel
24926 width. */
24927 it->pixel_width = 1;
24928 }
24929 else if (it->char_to_display == '\n')
24930 {
24931 /* A newline has no width, but we need the height of the
24932 line. But if previous part of the line sets a height,
24933 don't increase that height */
24934
24935 Lisp_Object height;
24936 Lisp_Object total_height = Qnil;
24937
24938 it->override_ascent = -1;
24939 it->pixel_width = 0;
24940 it->nglyphs = 0;
24941
24942 height = get_it_property (it, Qline_height);
24943 /* Split (line-height total-height) list */
24944 if (CONSP (height)
24945 && CONSP (XCDR (height))
24946 && NILP (XCDR (XCDR (height))))
24947 {
24948 total_height = XCAR (XCDR (height));
24949 height = XCAR (height);
24950 }
24951 height = calc_line_height_property (it, height, font, boff, 1);
24952
24953 if (it->override_ascent >= 0)
24954 {
24955 it->ascent = it->override_ascent;
24956 it->descent = it->override_descent;
24957 boff = it->override_boff;
24958 }
24959 else
24960 {
24961 it->ascent = FONT_BASE (font) + boff;
24962 it->descent = FONT_DESCENT (font) - boff;
24963 }
24964
24965 if (EQ (height, Qt))
24966 {
24967 if (it->descent > it->max_descent)
24968 {
24969 it->ascent += it->descent - it->max_descent;
24970 it->descent = it->max_descent;
24971 }
24972 if (it->ascent > it->max_ascent)
24973 {
24974 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
24975 it->ascent = it->max_ascent;
24976 }
24977 it->phys_ascent = min (it->phys_ascent, it->ascent);
24978 it->phys_descent = min (it->phys_descent, it->descent);
24979 it->constrain_row_ascent_descent_p = 1;
24980 extra_line_spacing = 0;
24981 }
24982 else
24983 {
24984 Lisp_Object spacing;
24985
24986 it->phys_ascent = it->ascent;
24987 it->phys_descent = it->descent;
24988
24989 if ((it->max_ascent > 0 || it->max_descent > 0)
24990 && face->box != FACE_NO_BOX
24991 && face->box_line_width > 0)
24992 {
24993 it->ascent += face->box_line_width;
24994 it->descent += face->box_line_width;
24995 }
24996 if (!NILP (height)
24997 && XINT (height) > it->ascent + it->descent)
24998 it->ascent = XINT (height) - it->descent;
24999
25000 if (!NILP (total_height))
25001 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25002 else
25003 {
25004 spacing = get_it_property (it, Qline_spacing);
25005 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25006 }
25007 if (INTEGERP (spacing))
25008 {
25009 extra_line_spacing = XINT (spacing);
25010 if (!NILP (total_height))
25011 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25012 }
25013 }
25014 }
25015 else /* i.e. (it->char_to_display == '\t') */
25016 {
25017 if (font->space_width > 0)
25018 {
25019 int tab_width = it->tab_width * font->space_width;
25020 int x = it->current_x + it->continuation_lines_width;
25021 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25022
25023 /* If the distance from the current position to the next tab
25024 stop is less than a space character width, use the
25025 tab stop after that. */
25026 if (next_tab_x - x < font->space_width)
25027 next_tab_x += tab_width;
25028
25029 it->pixel_width = next_tab_x - x;
25030 it->nglyphs = 1;
25031 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25032 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25033
25034 if (it->glyph_row)
25035 {
25036 append_stretch_glyph (it, it->object, it->pixel_width,
25037 it->ascent + it->descent, it->ascent);
25038 }
25039 }
25040 else
25041 {
25042 it->pixel_width = 0;
25043 it->nglyphs = 1;
25044 }
25045 }
25046 }
25047 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25048 {
25049 /* A static composition.
25050
25051 Note: A composition is represented as one glyph in the
25052 glyph matrix. There are no padding glyphs.
25053
25054 Important note: pixel_width, ascent, and descent are the
25055 values of what is drawn by draw_glyphs (i.e. the values of
25056 the overall glyphs composed). */
25057 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25058 int boff; /* baseline offset */
25059 struct composition *cmp = composition_table[it->cmp_it.id];
25060 int glyph_len = cmp->glyph_len;
25061 struct font *font = face->font;
25062
25063 it->nglyphs = 1;
25064
25065 /* If we have not yet calculated pixel size data of glyphs of
25066 the composition for the current face font, calculate them
25067 now. Theoretically, we have to check all fonts for the
25068 glyphs, but that requires much time and memory space. So,
25069 here we check only the font of the first glyph. This may
25070 lead to incorrect display, but it's very rare, and C-l
25071 (recenter-top-bottom) can correct the display anyway. */
25072 if (! cmp->font || cmp->font != font)
25073 {
25074 /* Ascent and descent of the font of the first character
25075 of this composition (adjusted by baseline offset).
25076 Ascent and descent of overall glyphs should not be less
25077 than these, respectively. */
25078 int font_ascent, font_descent, font_height;
25079 /* Bounding box of the overall glyphs. */
25080 int leftmost, rightmost, lowest, highest;
25081 int lbearing, rbearing;
25082 int i, width, ascent, descent;
25083 int left_padded = 0, right_padded = 0;
25084 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25085 XChar2b char2b;
25086 struct font_metrics *pcm;
25087 int font_not_found_p;
25088 ptrdiff_t pos;
25089
25090 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25091 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25092 break;
25093 if (glyph_len < cmp->glyph_len)
25094 right_padded = 1;
25095 for (i = 0; i < glyph_len; i++)
25096 {
25097 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25098 break;
25099 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25100 }
25101 if (i > 0)
25102 left_padded = 1;
25103
25104 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25105 : IT_CHARPOS (*it));
25106 /* If no suitable font is found, use the default font. */
25107 font_not_found_p = font == NULL;
25108 if (font_not_found_p)
25109 {
25110 face = face->ascii_face;
25111 font = face->font;
25112 }
25113 boff = font->baseline_offset;
25114 if (font->vertical_centering)
25115 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25116 font_ascent = FONT_BASE (font) + boff;
25117 font_descent = FONT_DESCENT (font) - boff;
25118 font_height = FONT_HEIGHT (font);
25119
25120 cmp->font = font;
25121
25122 pcm = NULL;
25123 if (! font_not_found_p)
25124 {
25125 get_char_face_and_encoding (it->f, c, it->face_id,
25126 &char2b, 0);
25127 pcm = get_per_char_metric (font, &char2b);
25128 }
25129
25130 /* Initialize the bounding box. */
25131 if (pcm)
25132 {
25133 width = cmp->glyph_len > 0 ? pcm->width : 0;
25134 ascent = pcm->ascent;
25135 descent = pcm->descent;
25136 lbearing = pcm->lbearing;
25137 rbearing = pcm->rbearing;
25138 }
25139 else
25140 {
25141 width = cmp->glyph_len > 0 ? font->space_width : 0;
25142 ascent = FONT_BASE (font);
25143 descent = FONT_DESCENT (font);
25144 lbearing = 0;
25145 rbearing = width;
25146 }
25147
25148 rightmost = width;
25149 leftmost = 0;
25150 lowest = - descent + boff;
25151 highest = ascent + boff;
25152
25153 if (! font_not_found_p
25154 && font->default_ascent
25155 && CHAR_TABLE_P (Vuse_default_ascent)
25156 && !NILP (Faref (Vuse_default_ascent,
25157 make_number (it->char_to_display))))
25158 highest = font->default_ascent + boff;
25159
25160 /* Draw the first glyph at the normal position. It may be
25161 shifted to right later if some other glyphs are drawn
25162 at the left. */
25163 cmp->offsets[i * 2] = 0;
25164 cmp->offsets[i * 2 + 1] = boff;
25165 cmp->lbearing = lbearing;
25166 cmp->rbearing = rbearing;
25167
25168 /* Set cmp->offsets for the remaining glyphs. */
25169 for (i++; i < glyph_len; i++)
25170 {
25171 int left, right, btm, top;
25172 int ch = COMPOSITION_GLYPH (cmp, i);
25173 int face_id;
25174 struct face *this_face;
25175
25176 if (ch == '\t')
25177 ch = ' ';
25178 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25179 this_face = FACE_FROM_ID (it->f, face_id);
25180 font = this_face->font;
25181
25182 if (font == NULL)
25183 pcm = NULL;
25184 else
25185 {
25186 get_char_face_and_encoding (it->f, ch, face_id,
25187 &char2b, 0);
25188 pcm = get_per_char_metric (font, &char2b);
25189 }
25190 if (! pcm)
25191 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25192 else
25193 {
25194 width = pcm->width;
25195 ascent = pcm->ascent;
25196 descent = pcm->descent;
25197 lbearing = pcm->lbearing;
25198 rbearing = pcm->rbearing;
25199 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25200 {
25201 /* Relative composition with or without
25202 alternate chars. */
25203 left = (leftmost + rightmost - width) / 2;
25204 btm = - descent + boff;
25205 if (font->relative_compose
25206 && (! CHAR_TABLE_P (Vignore_relative_composition)
25207 || NILP (Faref (Vignore_relative_composition,
25208 make_number (ch)))))
25209 {
25210
25211 if (- descent >= font->relative_compose)
25212 /* One extra pixel between two glyphs. */
25213 btm = highest + 1;
25214 else if (ascent <= 0)
25215 /* One extra pixel between two glyphs. */
25216 btm = lowest - 1 - ascent - descent;
25217 }
25218 }
25219 else
25220 {
25221 /* A composition rule is specified by an integer
25222 value that encodes global and new reference
25223 points (GREF and NREF). GREF and NREF are
25224 specified by numbers as below:
25225
25226 0---1---2 -- ascent
25227 | |
25228 | |
25229 | |
25230 9--10--11 -- center
25231 | |
25232 ---3---4---5--- baseline
25233 | |
25234 6---7---8 -- descent
25235 */
25236 int rule = COMPOSITION_RULE (cmp, i);
25237 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25238
25239 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25240 grefx = gref % 3, nrefx = nref % 3;
25241 grefy = gref / 3, nrefy = nref / 3;
25242 if (xoff)
25243 xoff = font_height * (xoff - 128) / 256;
25244 if (yoff)
25245 yoff = font_height * (yoff - 128) / 256;
25246
25247 left = (leftmost
25248 + grefx * (rightmost - leftmost) / 2
25249 - nrefx * width / 2
25250 + xoff);
25251
25252 btm = ((grefy == 0 ? highest
25253 : grefy == 1 ? 0
25254 : grefy == 2 ? lowest
25255 : (highest + lowest) / 2)
25256 - (nrefy == 0 ? ascent + descent
25257 : nrefy == 1 ? descent - boff
25258 : nrefy == 2 ? 0
25259 : (ascent + descent) / 2)
25260 + yoff);
25261 }
25262
25263 cmp->offsets[i * 2] = left;
25264 cmp->offsets[i * 2 + 1] = btm + descent;
25265
25266 /* Update the bounding box of the overall glyphs. */
25267 if (width > 0)
25268 {
25269 right = left + width;
25270 if (left < leftmost)
25271 leftmost = left;
25272 if (right > rightmost)
25273 rightmost = right;
25274 }
25275 top = btm + descent + ascent;
25276 if (top > highest)
25277 highest = top;
25278 if (btm < lowest)
25279 lowest = btm;
25280
25281 if (cmp->lbearing > left + lbearing)
25282 cmp->lbearing = left + lbearing;
25283 if (cmp->rbearing < left + rbearing)
25284 cmp->rbearing = left + rbearing;
25285 }
25286 }
25287
25288 /* If there are glyphs whose x-offsets are negative,
25289 shift all glyphs to the right and make all x-offsets
25290 non-negative. */
25291 if (leftmost < 0)
25292 {
25293 for (i = 0; i < cmp->glyph_len; i++)
25294 cmp->offsets[i * 2] -= leftmost;
25295 rightmost -= leftmost;
25296 cmp->lbearing -= leftmost;
25297 cmp->rbearing -= leftmost;
25298 }
25299
25300 if (left_padded && cmp->lbearing < 0)
25301 {
25302 for (i = 0; i < cmp->glyph_len; i++)
25303 cmp->offsets[i * 2] -= cmp->lbearing;
25304 rightmost -= cmp->lbearing;
25305 cmp->rbearing -= cmp->lbearing;
25306 cmp->lbearing = 0;
25307 }
25308 if (right_padded && rightmost < cmp->rbearing)
25309 {
25310 rightmost = cmp->rbearing;
25311 }
25312
25313 cmp->pixel_width = rightmost;
25314 cmp->ascent = highest;
25315 cmp->descent = - lowest;
25316 if (cmp->ascent < font_ascent)
25317 cmp->ascent = font_ascent;
25318 if (cmp->descent < font_descent)
25319 cmp->descent = font_descent;
25320 }
25321
25322 if (it->glyph_row
25323 && (cmp->lbearing < 0
25324 || cmp->rbearing > cmp->pixel_width))
25325 it->glyph_row->contains_overlapping_glyphs_p = 1;
25326
25327 it->pixel_width = cmp->pixel_width;
25328 it->ascent = it->phys_ascent = cmp->ascent;
25329 it->descent = it->phys_descent = cmp->descent;
25330 if (face->box != FACE_NO_BOX)
25331 {
25332 int thick = face->box_line_width;
25333
25334 if (thick > 0)
25335 {
25336 it->ascent += thick;
25337 it->descent += thick;
25338 }
25339 else
25340 thick = - thick;
25341
25342 if (it->start_of_box_run_p)
25343 it->pixel_width += thick;
25344 if (it->end_of_box_run_p)
25345 it->pixel_width += thick;
25346 }
25347
25348 /* If face has an overline, add the height of the overline
25349 (1 pixel) and a 1 pixel margin to the character height. */
25350 if (face->overline_p)
25351 it->ascent += overline_margin;
25352
25353 take_vertical_position_into_account (it);
25354 if (it->ascent < 0)
25355 it->ascent = 0;
25356 if (it->descent < 0)
25357 it->descent = 0;
25358
25359 if (it->glyph_row && cmp->glyph_len > 0)
25360 append_composite_glyph (it);
25361 }
25362 else if (it->what == IT_COMPOSITION)
25363 {
25364 /* A dynamic (automatic) composition. */
25365 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25366 Lisp_Object gstring;
25367 struct font_metrics metrics;
25368
25369 it->nglyphs = 1;
25370
25371 gstring = composition_gstring_from_id (it->cmp_it.id);
25372 it->pixel_width
25373 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25374 &metrics);
25375 if (it->glyph_row
25376 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25377 it->glyph_row->contains_overlapping_glyphs_p = 1;
25378 it->ascent = it->phys_ascent = metrics.ascent;
25379 it->descent = it->phys_descent = metrics.descent;
25380 if (face->box != FACE_NO_BOX)
25381 {
25382 int thick = face->box_line_width;
25383
25384 if (thick > 0)
25385 {
25386 it->ascent += thick;
25387 it->descent += thick;
25388 }
25389 else
25390 thick = - thick;
25391
25392 if (it->start_of_box_run_p)
25393 it->pixel_width += thick;
25394 if (it->end_of_box_run_p)
25395 it->pixel_width += thick;
25396 }
25397 /* If face has an overline, add the height of the overline
25398 (1 pixel) and a 1 pixel margin to the character height. */
25399 if (face->overline_p)
25400 it->ascent += overline_margin;
25401 take_vertical_position_into_account (it);
25402 if (it->ascent < 0)
25403 it->ascent = 0;
25404 if (it->descent < 0)
25405 it->descent = 0;
25406
25407 if (it->glyph_row)
25408 append_composite_glyph (it);
25409 }
25410 else if (it->what == IT_GLYPHLESS)
25411 produce_glyphless_glyph (it, 0, Qnil);
25412 else if (it->what == IT_IMAGE)
25413 produce_image_glyph (it);
25414 else if (it->what == IT_STRETCH)
25415 produce_stretch_glyph (it);
25416
25417 done:
25418 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25419 because this isn't true for images with `:ascent 100'. */
25420 eassert (it->ascent >= 0 && it->descent >= 0);
25421 if (it->area == TEXT_AREA)
25422 it->current_x += it->pixel_width;
25423
25424 if (extra_line_spacing > 0)
25425 {
25426 it->descent += extra_line_spacing;
25427 if (extra_line_spacing > it->max_extra_line_spacing)
25428 it->max_extra_line_spacing = extra_line_spacing;
25429 }
25430
25431 it->max_ascent = max (it->max_ascent, it->ascent);
25432 it->max_descent = max (it->max_descent, it->descent);
25433 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25434 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25435 }
25436
25437 /* EXPORT for RIF:
25438 Output LEN glyphs starting at START at the nominal cursor position.
25439 Advance the nominal cursor over the text. The global variable
25440 updated_window contains the window being updated, updated_row is
25441 the glyph row being updated, and updated_area is the area of that
25442 row being updated. */
25443
25444 void
25445 x_write_glyphs (struct glyph *start, int len)
25446 {
25447 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25448
25449 eassert (updated_window && updated_row);
25450 /* When the window is hscrolled, cursor hpos can legitimately be out
25451 of bounds, but we draw the cursor at the corresponding window
25452 margin in that case. */
25453 if (!updated_row->reversed_p && chpos < 0)
25454 chpos = 0;
25455 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25456 chpos = updated_row->used[TEXT_AREA] - 1;
25457
25458 block_input ();
25459
25460 /* Write glyphs. */
25461
25462 hpos = start - updated_row->glyphs[updated_area];
25463 x = draw_glyphs (updated_window, output_cursor.x,
25464 updated_row, updated_area,
25465 hpos, hpos + len,
25466 DRAW_NORMAL_TEXT, 0);
25467
25468 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25469 if (updated_area == TEXT_AREA
25470 && updated_window->phys_cursor_on_p
25471 && updated_window->phys_cursor.vpos == output_cursor.vpos
25472 && chpos >= hpos
25473 && chpos < hpos + len)
25474 updated_window->phys_cursor_on_p = 0;
25475
25476 unblock_input ();
25477
25478 /* Advance the output cursor. */
25479 output_cursor.hpos += len;
25480 output_cursor.x = x;
25481 }
25482
25483
25484 /* EXPORT for RIF:
25485 Insert LEN glyphs from START at the nominal cursor position. */
25486
25487 void
25488 x_insert_glyphs (struct glyph *start, int len)
25489 {
25490 struct frame *f;
25491 struct window *w;
25492 int line_height, shift_by_width, shifted_region_width;
25493 struct glyph_row *row;
25494 struct glyph *glyph;
25495 int frame_x, frame_y;
25496 ptrdiff_t hpos;
25497
25498 eassert (updated_window && updated_row);
25499 block_input ();
25500 w = updated_window;
25501 f = XFRAME (WINDOW_FRAME (w));
25502
25503 /* Get the height of the line we are in. */
25504 row = updated_row;
25505 line_height = row->height;
25506
25507 /* Get the width of the glyphs to insert. */
25508 shift_by_width = 0;
25509 for (glyph = start; glyph < start + len; ++glyph)
25510 shift_by_width += glyph->pixel_width;
25511
25512 /* Get the width of the region to shift right. */
25513 shifted_region_width = (window_box_width (w, updated_area)
25514 - output_cursor.x
25515 - shift_by_width);
25516
25517 /* Shift right. */
25518 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25519 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25520
25521 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25522 line_height, shift_by_width);
25523
25524 /* Write the glyphs. */
25525 hpos = start - row->glyphs[updated_area];
25526 draw_glyphs (w, output_cursor.x, row, updated_area,
25527 hpos, hpos + len,
25528 DRAW_NORMAL_TEXT, 0);
25529
25530 /* Advance the output cursor. */
25531 output_cursor.hpos += len;
25532 output_cursor.x += shift_by_width;
25533 unblock_input ();
25534 }
25535
25536
25537 /* EXPORT for RIF:
25538 Erase the current text line from the nominal cursor position
25539 (inclusive) to pixel column TO_X (exclusive). The idea is that
25540 everything from TO_X onward is already erased.
25541
25542 TO_X is a pixel position relative to updated_area of
25543 updated_window. TO_X == -1 means clear to the end of this area. */
25544
25545 void
25546 x_clear_end_of_line (int to_x)
25547 {
25548 struct frame *f;
25549 struct window *w = updated_window;
25550 int max_x, min_y, max_y;
25551 int from_x, from_y, to_y;
25552
25553 eassert (updated_window && updated_row);
25554 f = XFRAME (w->frame);
25555
25556 if (updated_row->full_width_p)
25557 max_x = WINDOW_TOTAL_WIDTH (w);
25558 else
25559 max_x = window_box_width (w, updated_area);
25560 max_y = window_text_bottom_y (w);
25561
25562 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25563 of window. For TO_X > 0, truncate to end of drawing area. */
25564 if (to_x == 0)
25565 return;
25566 else if (to_x < 0)
25567 to_x = max_x;
25568 else
25569 to_x = min (to_x, max_x);
25570
25571 to_y = min (max_y, output_cursor.y + updated_row->height);
25572
25573 /* Notice if the cursor will be cleared by this operation. */
25574 if (!updated_row->full_width_p)
25575 notice_overwritten_cursor (w, updated_area,
25576 output_cursor.x, -1,
25577 updated_row->y,
25578 MATRIX_ROW_BOTTOM_Y (updated_row));
25579
25580 from_x = output_cursor.x;
25581
25582 /* Translate to frame coordinates. */
25583 if (updated_row->full_width_p)
25584 {
25585 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25586 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25587 }
25588 else
25589 {
25590 int area_left = window_box_left (w, updated_area);
25591 from_x += area_left;
25592 to_x += area_left;
25593 }
25594
25595 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25596 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25597 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25598
25599 /* Prevent inadvertently clearing to end of the X window. */
25600 if (to_x > from_x && to_y > from_y)
25601 {
25602 block_input ();
25603 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25604 to_x - from_x, to_y - from_y);
25605 unblock_input ();
25606 }
25607 }
25608
25609 #endif /* HAVE_WINDOW_SYSTEM */
25610
25611
25612 \f
25613 /***********************************************************************
25614 Cursor types
25615 ***********************************************************************/
25616
25617 /* Value is the internal representation of the specified cursor type
25618 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25619 of the bar cursor. */
25620
25621 static enum text_cursor_kinds
25622 get_specified_cursor_type (Lisp_Object arg, int *width)
25623 {
25624 enum text_cursor_kinds type;
25625
25626 if (NILP (arg))
25627 return NO_CURSOR;
25628
25629 if (EQ (arg, Qbox))
25630 return FILLED_BOX_CURSOR;
25631
25632 if (EQ (arg, Qhollow))
25633 return HOLLOW_BOX_CURSOR;
25634
25635 if (EQ (arg, Qbar))
25636 {
25637 *width = 2;
25638 return BAR_CURSOR;
25639 }
25640
25641 if (CONSP (arg)
25642 && EQ (XCAR (arg), Qbar)
25643 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25644 {
25645 *width = XINT (XCDR (arg));
25646 return BAR_CURSOR;
25647 }
25648
25649 if (EQ (arg, Qhbar))
25650 {
25651 *width = 2;
25652 return HBAR_CURSOR;
25653 }
25654
25655 if (CONSP (arg)
25656 && EQ (XCAR (arg), Qhbar)
25657 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25658 {
25659 *width = XINT (XCDR (arg));
25660 return HBAR_CURSOR;
25661 }
25662
25663 /* Treat anything unknown as "hollow box cursor".
25664 It was bad to signal an error; people have trouble fixing
25665 .Xdefaults with Emacs, when it has something bad in it. */
25666 type = HOLLOW_BOX_CURSOR;
25667
25668 return type;
25669 }
25670
25671 /* Set the default cursor types for specified frame. */
25672 void
25673 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25674 {
25675 int width = 1;
25676 Lisp_Object tem;
25677
25678 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25679 FRAME_CURSOR_WIDTH (f) = width;
25680
25681 /* By default, set up the blink-off state depending on the on-state. */
25682
25683 tem = Fassoc (arg, Vblink_cursor_alist);
25684 if (!NILP (tem))
25685 {
25686 FRAME_BLINK_OFF_CURSOR (f)
25687 = get_specified_cursor_type (XCDR (tem), &width);
25688 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25689 }
25690 else
25691 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25692 }
25693
25694
25695 #ifdef HAVE_WINDOW_SYSTEM
25696
25697 /* Return the cursor we want to be displayed in window W. Return
25698 width of bar/hbar cursor through WIDTH arg. Return with
25699 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25700 (i.e. if the `system caret' should track this cursor).
25701
25702 In a mini-buffer window, we want the cursor only to appear if we
25703 are reading input from this window. For the selected window, we
25704 want the cursor type given by the frame parameter or buffer local
25705 setting of cursor-type. If explicitly marked off, draw no cursor.
25706 In all other cases, we want a hollow box cursor. */
25707
25708 static enum text_cursor_kinds
25709 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25710 int *active_cursor)
25711 {
25712 struct frame *f = XFRAME (w->frame);
25713 struct buffer *b = XBUFFER (w->buffer);
25714 int cursor_type = DEFAULT_CURSOR;
25715 Lisp_Object alt_cursor;
25716 int non_selected = 0;
25717
25718 *active_cursor = 1;
25719
25720 /* Echo area */
25721 if (cursor_in_echo_area
25722 && FRAME_HAS_MINIBUF_P (f)
25723 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25724 {
25725 if (w == XWINDOW (echo_area_window))
25726 {
25727 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25728 {
25729 *width = FRAME_CURSOR_WIDTH (f);
25730 return FRAME_DESIRED_CURSOR (f);
25731 }
25732 else
25733 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25734 }
25735
25736 *active_cursor = 0;
25737 non_selected = 1;
25738 }
25739
25740 /* Detect a nonselected window or nonselected frame. */
25741 else if (w != XWINDOW (f->selected_window)
25742 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25743 {
25744 *active_cursor = 0;
25745
25746 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25747 return NO_CURSOR;
25748
25749 non_selected = 1;
25750 }
25751
25752 /* Never display a cursor in a window in which cursor-type is nil. */
25753 if (NILP (BVAR (b, cursor_type)))
25754 return NO_CURSOR;
25755
25756 /* Get the normal cursor type for this window. */
25757 if (EQ (BVAR (b, cursor_type), Qt))
25758 {
25759 cursor_type = FRAME_DESIRED_CURSOR (f);
25760 *width = FRAME_CURSOR_WIDTH (f);
25761 }
25762 else
25763 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25764
25765 /* Use cursor-in-non-selected-windows instead
25766 for non-selected window or frame. */
25767 if (non_selected)
25768 {
25769 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25770 if (!EQ (Qt, alt_cursor))
25771 return get_specified_cursor_type (alt_cursor, width);
25772 /* t means modify the normal cursor type. */
25773 if (cursor_type == FILLED_BOX_CURSOR)
25774 cursor_type = HOLLOW_BOX_CURSOR;
25775 else if (cursor_type == BAR_CURSOR && *width > 1)
25776 --*width;
25777 return cursor_type;
25778 }
25779
25780 /* Use normal cursor if not blinked off. */
25781 if (!w->cursor_off_p)
25782 {
25783 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25784 {
25785 if (cursor_type == FILLED_BOX_CURSOR)
25786 {
25787 /* Using a block cursor on large images can be very annoying.
25788 So use a hollow cursor for "large" images.
25789 If image is not transparent (no mask), also use hollow cursor. */
25790 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25791 if (img != NULL && IMAGEP (img->spec))
25792 {
25793 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25794 where N = size of default frame font size.
25795 This should cover most of the "tiny" icons people may use. */
25796 if (!img->mask
25797 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25798 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25799 cursor_type = HOLLOW_BOX_CURSOR;
25800 }
25801 }
25802 else if (cursor_type != NO_CURSOR)
25803 {
25804 /* Display current only supports BOX and HOLLOW cursors for images.
25805 So for now, unconditionally use a HOLLOW cursor when cursor is
25806 not a solid box cursor. */
25807 cursor_type = HOLLOW_BOX_CURSOR;
25808 }
25809 }
25810 return cursor_type;
25811 }
25812
25813 /* Cursor is blinked off, so determine how to "toggle" it. */
25814
25815 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25816 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25817 return get_specified_cursor_type (XCDR (alt_cursor), width);
25818
25819 /* Then see if frame has specified a specific blink off cursor type. */
25820 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25821 {
25822 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25823 return FRAME_BLINK_OFF_CURSOR (f);
25824 }
25825
25826 #if 0
25827 /* Some people liked having a permanently visible blinking cursor,
25828 while others had very strong opinions against it. So it was
25829 decided to remove it. KFS 2003-09-03 */
25830
25831 /* Finally perform built-in cursor blinking:
25832 filled box <-> hollow box
25833 wide [h]bar <-> narrow [h]bar
25834 narrow [h]bar <-> no cursor
25835 other type <-> no cursor */
25836
25837 if (cursor_type == FILLED_BOX_CURSOR)
25838 return HOLLOW_BOX_CURSOR;
25839
25840 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
25841 {
25842 *width = 1;
25843 return cursor_type;
25844 }
25845 #endif
25846
25847 return NO_CURSOR;
25848 }
25849
25850
25851 /* Notice when the text cursor of window W has been completely
25852 overwritten by a drawing operation that outputs glyphs in AREA
25853 starting at X0 and ending at X1 in the line starting at Y0 and
25854 ending at Y1. X coordinates are area-relative. X1 < 0 means all
25855 the rest of the line after X0 has been written. Y coordinates
25856 are window-relative. */
25857
25858 static void
25859 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
25860 int x0, int x1, int y0, int y1)
25861 {
25862 int cx0, cx1, cy0, cy1;
25863 struct glyph_row *row;
25864
25865 if (!w->phys_cursor_on_p)
25866 return;
25867 if (area != TEXT_AREA)
25868 return;
25869
25870 if (w->phys_cursor.vpos < 0
25871 || w->phys_cursor.vpos >= w->current_matrix->nrows
25872 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
25873 !(row->enabled_p && row->displays_text_p)))
25874 return;
25875
25876 if (row->cursor_in_fringe_p)
25877 {
25878 row->cursor_in_fringe_p = 0;
25879 draw_fringe_bitmap (w, row, row->reversed_p);
25880 w->phys_cursor_on_p = 0;
25881 return;
25882 }
25883
25884 cx0 = w->phys_cursor.x;
25885 cx1 = cx0 + w->phys_cursor_width;
25886 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
25887 return;
25888
25889 /* The cursor image will be completely removed from the
25890 screen if the output area intersects the cursor area in
25891 y-direction. When we draw in [y0 y1[, and some part of
25892 the cursor is at y < y0, that part must have been drawn
25893 before. When scrolling, the cursor is erased before
25894 actually scrolling, so we don't come here. When not
25895 scrolling, the rows above the old cursor row must have
25896 changed, and in this case these rows must have written
25897 over the cursor image.
25898
25899 Likewise if part of the cursor is below y1, with the
25900 exception of the cursor being in the first blank row at
25901 the buffer and window end because update_text_area
25902 doesn't draw that row. (Except when it does, but
25903 that's handled in update_text_area.) */
25904
25905 cy0 = w->phys_cursor.y;
25906 cy1 = cy0 + w->phys_cursor_height;
25907 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
25908 return;
25909
25910 w->phys_cursor_on_p = 0;
25911 }
25912
25913 #endif /* HAVE_WINDOW_SYSTEM */
25914
25915 \f
25916 /************************************************************************
25917 Mouse Face
25918 ************************************************************************/
25919
25920 #ifdef HAVE_WINDOW_SYSTEM
25921
25922 /* EXPORT for RIF:
25923 Fix the display of area AREA of overlapping row ROW in window W
25924 with respect to the overlapping part OVERLAPS. */
25925
25926 void
25927 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
25928 enum glyph_row_area area, int overlaps)
25929 {
25930 int i, x;
25931
25932 block_input ();
25933
25934 x = 0;
25935 for (i = 0; i < row->used[area];)
25936 {
25937 if (row->glyphs[area][i].overlaps_vertically_p)
25938 {
25939 int start = i, start_x = x;
25940
25941 do
25942 {
25943 x += row->glyphs[area][i].pixel_width;
25944 ++i;
25945 }
25946 while (i < row->used[area]
25947 && row->glyphs[area][i].overlaps_vertically_p);
25948
25949 draw_glyphs (w, start_x, row, area,
25950 start, i,
25951 DRAW_NORMAL_TEXT, overlaps);
25952 }
25953 else
25954 {
25955 x += row->glyphs[area][i].pixel_width;
25956 ++i;
25957 }
25958 }
25959
25960 unblock_input ();
25961 }
25962
25963
25964 /* EXPORT:
25965 Draw the cursor glyph of window W in glyph row ROW. See the
25966 comment of draw_glyphs for the meaning of HL. */
25967
25968 void
25969 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
25970 enum draw_glyphs_face hl)
25971 {
25972 /* If cursor hpos is out of bounds, don't draw garbage. This can
25973 happen in mini-buffer windows when switching between echo area
25974 glyphs and mini-buffer. */
25975 if ((row->reversed_p
25976 ? (w->phys_cursor.hpos >= 0)
25977 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
25978 {
25979 int on_p = w->phys_cursor_on_p;
25980 int x1;
25981 int hpos = w->phys_cursor.hpos;
25982
25983 /* When the window is hscrolled, cursor hpos can legitimately be
25984 out of bounds, but we draw the cursor at the corresponding
25985 window margin in that case. */
25986 if (!row->reversed_p && hpos < 0)
25987 hpos = 0;
25988 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
25989 hpos = row->used[TEXT_AREA] - 1;
25990
25991 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
25992 hl, 0);
25993 w->phys_cursor_on_p = on_p;
25994
25995 if (hl == DRAW_CURSOR)
25996 w->phys_cursor_width = x1 - w->phys_cursor.x;
25997 /* When we erase the cursor, and ROW is overlapped by other
25998 rows, make sure that these overlapping parts of other rows
25999 are redrawn. */
26000 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26001 {
26002 w->phys_cursor_width = x1 - w->phys_cursor.x;
26003
26004 if (row > w->current_matrix->rows
26005 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26006 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26007 OVERLAPS_ERASED_CURSOR);
26008
26009 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26010 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26011 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26012 OVERLAPS_ERASED_CURSOR);
26013 }
26014 }
26015 }
26016
26017
26018 /* EXPORT:
26019 Erase the image of a cursor of window W from the screen. */
26020
26021 void
26022 erase_phys_cursor (struct window *w)
26023 {
26024 struct frame *f = XFRAME (w->frame);
26025 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26026 int hpos = w->phys_cursor.hpos;
26027 int vpos = w->phys_cursor.vpos;
26028 int mouse_face_here_p = 0;
26029 struct glyph_matrix *active_glyphs = w->current_matrix;
26030 struct glyph_row *cursor_row;
26031 struct glyph *cursor_glyph;
26032 enum draw_glyphs_face hl;
26033
26034 /* No cursor displayed or row invalidated => nothing to do on the
26035 screen. */
26036 if (w->phys_cursor_type == NO_CURSOR)
26037 goto mark_cursor_off;
26038
26039 /* VPOS >= active_glyphs->nrows means that window has been resized.
26040 Don't bother to erase the cursor. */
26041 if (vpos >= active_glyphs->nrows)
26042 goto mark_cursor_off;
26043
26044 /* If row containing cursor is marked invalid, there is nothing we
26045 can do. */
26046 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26047 if (!cursor_row->enabled_p)
26048 goto mark_cursor_off;
26049
26050 /* If line spacing is > 0, old cursor may only be partially visible in
26051 window after split-window. So adjust visible height. */
26052 cursor_row->visible_height = min (cursor_row->visible_height,
26053 window_text_bottom_y (w) - cursor_row->y);
26054
26055 /* If row is completely invisible, don't attempt to delete a cursor which
26056 isn't there. This can happen if cursor is at top of a window, and
26057 we switch to a buffer with a header line in that window. */
26058 if (cursor_row->visible_height <= 0)
26059 goto mark_cursor_off;
26060
26061 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26062 if (cursor_row->cursor_in_fringe_p)
26063 {
26064 cursor_row->cursor_in_fringe_p = 0;
26065 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26066 goto mark_cursor_off;
26067 }
26068
26069 /* This can happen when the new row is shorter than the old one.
26070 In this case, either draw_glyphs or clear_end_of_line
26071 should have cleared the cursor. Note that we wouldn't be
26072 able to erase the cursor in this case because we don't have a
26073 cursor glyph at hand. */
26074 if ((cursor_row->reversed_p
26075 ? (w->phys_cursor.hpos < 0)
26076 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26077 goto mark_cursor_off;
26078
26079 /* When the window is hscrolled, cursor hpos can legitimately be out
26080 of bounds, but we draw the cursor at the corresponding window
26081 margin in that case. */
26082 if (!cursor_row->reversed_p && hpos < 0)
26083 hpos = 0;
26084 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26085 hpos = cursor_row->used[TEXT_AREA] - 1;
26086
26087 /* If the cursor is in the mouse face area, redisplay that when
26088 we clear the cursor. */
26089 if (! NILP (hlinfo->mouse_face_window)
26090 && coords_in_mouse_face_p (w, hpos, vpos)
26091 /* Don't redraw the cursor's spot in mouse face if it is at the
26092 end of a line (on a newline). The cursor appears there, but
26093 mouse highlighting does not. */
26094 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26095 mouse_face_here_p = 1;
26096
26097 /* Maybe clear the display under the cursor. */
26098 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26099 {
26100 int x, y, left_x;
26101 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26102 int width;
26103
26104 cursor_glyph = get_phys_cursor_glyph (w);
26105 if (cursor_glyph == NULL)
26106 goto mark_cursor_off;
26107
26108 width = cursor_glyph->pixel_width;
26109 left_x = window_box_left_offset (w, TEXT_AREA);
26110 x = w->phys_cursor.x;
26111 if (x < left_x)
26112 width -= left_x - x;
26113 width = min (width, window_box_width (w, TEXT_AREA) - x);
26114 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26115 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26116
26117 if (width > 0)
26118 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26119 }
26120
26121 /* Erase the cursor by redrawing the character underneath it. */
26122 if (mouse_face_here_p)
26123 hl = DRAW_MOUSE_FACE;
26124 else
26125 hl = DRAW_NORMAL_TEXT;
26126 draw_phys_cursor_glyph (w, cursor_row, hl);
26127
26128 mark_cursor_off:
26129 w->phys_cursor_on_p = 0;
26130 w->phys_cursor_type = NO_CURSOR;
26131 }
26132
26133
26134 /* EXPORT:
26135 Display or clear cursor of window W. If ON is zero, clear the
26136 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26137 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26138
26139 void
26140 display_and_set_cursor (struct window *w, int on,
26141 int hpos, int vpos, int x, int y)
26142 {
26143 struct frame *f = XFRAME (w->frame);
26144 int new_cursor_type;
26145 int new_cursor_width;
26146 int active_cursor;
26147 struct glyph_row *glyph_row;
26148 struct glyph *glyph;
26149
26150 /* This is pointless on invisible frames, and dangerous on garbaged
26151 windows and frames; in the latter case, the frame or window may
26152 be in the midst of changing its size, and x and y may be off the
26153 window. */
26154 if (! FRAME_VISIBLE_P (f)
26155 || FRAME_GARBAGED_P (f)
26156 || vpos >= w->current_matrix->nrows
26157 || hpos >= w->current_matrix->matrix_w)
26158 return;
26159
26160 /* If cursor is off and we want it off, return quickly. */
26161 if (!on && !w->phys_cursor_on_p)
26162 return;
26163
26164 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26165 /* If cursor row is not enabled, we don't really know where to
26166 display the cursor. */
26167 if (!glyph_row->enabled_p)
26168 {
26169 w->phys_cursor_on_p = 0;
26170 return;
26171 }
26172
26173 glyph = NULL;
26174 if (!glyph_row->exact_window_width_line_p
26175 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26176 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26177
26178 eassert (input_blocked_p ());
26179
26180 /* Set new_cursor_type to the cursor we want to be displayed. */
26181 new_cursor_type = get_window_cursor_type (w, glyph,
26182 &new_cursor_width, &active_cursor);
26183
26184 /* If cursor is currently being shown and we don't want it to be or
26185 it is in the wrong place, or the cursor type is not what we want,
26186 erase it. */
26187 if (w->phys_cursor_on_p
26188 && (!on
26189 || w->phys_cursor.x != x
26190 || w->phys_cursor.y != y
26191 || new_cursor_type != w->phys_cursor_type
26192 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26193 && new_cursor_width != w->phys_cursor_width)))
26194 erase_phys_cursor (w);
26195
26196 /* Don't check phys_cursor_on_p here because that flag is only set
26197 to zero in some cases where we know that the cursor has been
26198 completely erased, to avoid the extra work of erasing the cursor
26199 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26200 still not be visible, or it has only been partly erased. */
26201 if (on)
26202 {
26203 w->phys_cursor_ascent = glyph_row->ascent;
26204 w->phys_cursor_height = glyph_row->height;
26205
26206 /* Set phys_cursor_.* before x_draw_.* is called because some
26207 of them may need the information. */
26208 w->phys_cursor.x = x;
26209 w->phys_cursor.y = glyph_row->y;
26210 w->phys_cursor.hpos = hpos;
26211 w->phys_cursor.vpos = vpos;
26212 }
26213
26214 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26215 new_cursor_type, new_cursor_width,
26216 on, active_cursor);
26217 }
26218
26219
26220 /* Switch the display of W's cursor on or off, according to the value
26221 of ON. */
26222
26223 static void
26224 update_window_cursor (struct window *w, int on)
26225 {
26226 /* Don't update cursor in windows whose frame is in the process
26227 of being deleted. */
26228 if (w->current_matrix)
26229 {
26230 int hpos = w->phys_cursor.hpos;
26231 int vpos = w->phys_cursor.vpos;
26232 struct glyph_row *row;
26233
26234 if (vpos >= w->current_matrix->nrows
26235 || hpos >= w->current_matrix->matrix_w)
26236 return;
26237
26238 row = MATRIX_ROW (w->current_matrix, vpos);
26239
26240 /* When the window is hscrolled, cursor hpos can legitimately be
26241 out of bounds, but we draw the cursor at the corresponding
26242 window margin in that case. */
26243 if (!row->reversed_p && hpos < 0)
26244 hpos = 0;
26245 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26246 hpos = row->used[TEXT_AREA] - 1;
26247
26248 block_input ();
26249 display_and_set_cursor (w, on, hpos, vpos,
26250 w->phys_cursor.x, w->phys_cursor.y);
26251 unblock_input ();
26252 }
26253 }
26254
26255
26256 /* Call update_window_cursor with parameter ON_P on all leaf windows
26257 in the window tree rooted at W. */
26258
26259 static void
26260 update_cursor_in_window_tree (struct window *w, int on_p)
26261 {
26262 while (w)
26263 {
26264 if (!NILP (w->hchild))
26265 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26266 else if (!NILP (w->vchild))
26267 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26268 else
26269 update_window_cursor (w, on_p);
26270
26271 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26272 }
26273 }
26274
26275
26276 /* EXPORT:
26277 Display the cursor on window W, or clear it, according to ON_P.
26278 Don't change the cursor's position. */
26279
26280 void
26281 x_update_cursor (struct frame *f, int on_p)
26282 {
26283 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26284 }
26285
26286
26287 /* EXPORT:
26288 Clear the cursor of window W to background color, and mark the
26289 cursor as not shown. This is used when the text where the cursor
26290 is about to be rewritten. */
26291
26292 void
26293 x_clear_cursor (struct window *w)
26294 {
26295 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26296 update_window_cursor (w, 0);
26297 }
26298
26299 #endif /* HAVE_WINDOW_SYSTEM */
26300
26301 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26302 and MSDOS. */
26303 static void
26304 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26305 int start_hpos, int end_hpos,
26306 enum draw_glyphs_face draw)
26307 {
26308 #ifdef HAVE_WINDOW_SYSTEM
26309 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26310 {
26311 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26312 return;
26313 }
26314 #endif
26315 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26316 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26317 #endif
26318 }
26319
26320 /* Display the active region described by mouse_face_* according to DRAW. */
26321
26322 static void
26323 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26324 {
26325 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26326 struct frame *f = XFRAME (WINDOW_FRAME (w));
26327
26328 if (/* If window is in the process of being destroyed, don't bother
26329 to do anything. */
26330 w->current_matrix != NULL
26331 /* Don't update mouse highlight if hidden */
26332 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26333 /* Recognize when we are called to operate on rows that don't exist
26334 anymore. This can happen when a window is split. */
26335 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26336 {
26337 int phys_cursor_on_p = w->phys_cursor_on_p;
26338 struct glyph_row *row, *first, *last;
26339
26340 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26341 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26342
26343 for (row = first; row <= last && row->enabled_p; ++row)
26344 {
26345 int start_hpos, end_hpos, start_x;
26346
26347 /* For all but the first row, the highlight starts at column 0. */
26348 if (row == first)
26349 {
26350 /* R2L rows have BEG and END in reversed order, but the
26351 screen drawing geometry is always left to right. So
26352 we need to mirror the beginning and end of the
26353 highlighted area in R2L rows. */
26354 if (!row->reversed_p)
26355 {
26356 start_hpos = hlinfo->mouse_face_beg_col;
26357 start_x = hlinfo->mouse_face_beg_x;
26358 }
26359 else if (row == last)
26360 {
26361 start_hpos = hlinfo->mouse_face_end_col;
26362 start_x = hlinfo->mouse_face_end_x;
26363 }
26364 else
26365 {
26366 start_hpos = 0;
26367 start_x = 0;
26368 }
26369 }
26370 else if (row->reversed_p && row == last)
26371 {
26372 start_hpos = hlinfo->mouse_face_end_col;
26373 start_x = hlinfo->mouse_face_end_x;
26374 }
26375 else
26376 {
26377 start_hpos = 0;
26378 start_x = 0;
26379 }
26380
26381 if (row == last)
26382 {
26383 if (!row->reversed_p)
26384 end_hpos = hlinfo->mouse_face_end_col;
26385 else if (row == first)
26386 end_hpos = hlinfo->mouse_face_beg_col;
26387 else
26388 {
26389 end_hpos = row->used[TEXT_AREA];
26390 if (draw == DRAW_NORMAL_TEXT)
26391 row->fill_line_p = 1; /* Clear to end of line */
26392 }
26393 }
26394 else if (row->reversed_p && row == first)
26395 end_hpos = hlinfo->mouse_face_beg_col;
26396 else
26397 {
26398 end_hpos = row->used[TEXT_AREA];
26399 if (draw == DRAW_NORMAL_TEXT)
26400 row->fill_line_p = 1; /* Clear to end of line */
26401 }
26402
26403 if (end_hpos > start_hpos)
26404 {
26405 draw_row_with_mouse_face (w, start_x, row,
26406 start_hpos, end_hpos, draw);
26407
26408 row->mouse_face_p
26409 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26410 }
26411 }
26412
26413 #ifdef HAVE_WINDOW_SYSTEM
26414 /* When we've written over the cursor, arrange for it to
26415 be displayed again. */
26416 if (FRAME_WINDOW_P (f)
26417 && phys_cursor_on_p && !w->phys_cursor_on_p)
26418 {
26419 int hpos = w->phys_cursor.hpos;
26420
26421 /* When the window is hscrolled, cursor hpos can legitimately be
26422 out of bounds, but we draw the cursor at the corresponding
26423 window margin in that case. */
26424 if (!row->reversed_p && hpos < 0)
26425 hpos = 0;
26426 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26427 hpos = row->used[TEXT_AREA] - 1;
26428
26429 block_input ();
26430 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26431 w->phys_cursor.x, w->phys_cursor.y);
26432 unblock_input ();
26433 }
26434 #endif /* HAVE_WINDOW_SYSTEM */
26435 }
26436
26437 #ifdef HAVE_WINDOW_SYSTEM
26438 /* Change the mouse cursor. */
26439 if (FRAME_WINDOW_P (f))
26440 {
26441 if (draw == DRAW_NORMAL_TEXT
26442 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26443 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26444 else if (draw == DRAW_MOUSE_FACE)
26445 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26446 else
26447 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26448 }
26449 #endif /* HAVE_WINDOW_SYSTEM */
26450 }
26451
26452 /* EXPORT:
26453 Clear out the mouse-highlighted active region.
26454 Redraw it un-highlighted first. Value is non-zero if mouse
26455 face was actually drawn unhighlighted. */
26456
26457 int
26458 clear_mouse_face (Mouse_HLInfo *hlinfo)
26459 {
26460 int cleared = 0;
26461
26462 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26463 {
26464 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26465 cleared = 1;
26466 }
26467
26468 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26469 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26470 hlinfo->mouse_face_window = Qnil;
26471 hlinfo->mouse_face_overlay = Qnil;
26472 return cleared;
26473 }
26474
26475 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26476 within the mouse face on that window. */
26477 static int
26478 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26479 {
26480 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26481
26482 /* Quickly resolve the easy cases. */
26483 if (!(WINDOWP (hlinfo->mouse_face_window)
26484 && XWINDOW (hlinfo->mouse_face_window) == w))
26485 return 0;
26486 if (vpos < hlinfo->mouse_face_beg_row
26487 || vpos > hlinfo->mouse_face_end_row)
26488 return 0;
26489 if (vpos > hlinfo->mouse_face_beg_row
26490 && vpos < hlinfo->mouse_face_end_row)
26491 return 1;
26492
26493 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26494 {
26495 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26496 {
26497 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26498 return 1;
26499 }
26500 else if ((vpos == hlinfo->mouse_face_beg_row
26501 && hpos >= hlinfo->mouse_face_beg_col)
26502 || (vpos == hlinfo->mouse_face_end_row
26503 && hpos < hlinfo->mouse_face_end_col))
26504 return 1;
26505 }
26506 else
26507 {
26508 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26509 {
26510 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26511 return 1;
26512 }
26513 else if ((vpos == hlinfo->mouse_face_beg_row
26514 && hpos <= hlinfo->mouse_face_beg_col)
26515 || (vpos == hlinfo->mouse_face_end_row
26516 && hpos > hlinfo->mouse_face_end_col))
26517 return 1;
26518 }
26519 return 0;
26520 }
26521
26522
26523 /* EXPORT:
26524 Non-zero if physical cursor of window W is within mouse face. */
26525
26526 int
26527 cursor_in_mouse_face_p (struct window *w)
26528 {
26529 int hpos = w->phys_cursor.hpos;
26530 int vpos = w->phys_cursor.vpos;
26531 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26532
26533 /* When the window is hscrolled, cursor hpos can legitimately be out
26534 of bounds, but we draw the cursor at the corresponding window
26535 margin in that case. */
26536 if (!row->reversed_p && hpos < 0)
26537 hpos = 0;
26538 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26539 hpos = row->used[TEXT_AREA] - 1;
26540
26541 return coords_in_mouse_face_p (w, hpos, vpos);
26542 }
26543
26544
26545 \f
26546 /* Find the glyph rows START_ROW and END_ROW of window W that display
26547 characters between buffer positions START_CHARPOS and END_CHARPOS
26548 (excluding END_CHARPOS). DISP_STRING is a display string that
26549 covers these buffer positions. This is similar to
26550 row_containing_pos, but is more accurate when bidi reordering makes
26551 buffer positions change non-linearly with glyph rows. */
26552 static void
26553 rows_from_pos_range (struct window *w,
26554 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26555 Lisp_Object disp_string,
26556 struct glyph_row **start, struct glyph_row **end)
26557 {
26558 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26559 int last_y = window_text_bottom_y (w);
26560 struct glyph_row *row;
26561
26562 *start = NULL;
26563 *end = NULL;
26564
26565 while (!first->enabled_p
26566 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26567 first++;
26568
26569 /* Find the START row. */
26570 for (row = first;
26571 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26572 row++)
26573 {
26574 /* A row can potentially be the START row if the range of the
26575 characters it displays intersects the range
26576 [START_CHARPOS..END_CHARPOS). */
26577 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26578 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26579 /* See the commentary in row_containing_pos, for the
26580 explanation of the complicated way to check whether
26581 some position is beyond the end of the characters
26582 displayed by a row. */
26583 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26584 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26585 && !row->ends_at_zv_p
26586 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26587 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26588 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26589 && !row->ends_at_zv_p
26590 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26591 {
26592 /* Found a candidate row. Now make sure at least one of the
26593 glyphs it displays has a charpos from the range
26594 [START_CHARPOS..END_CHARPOS).
26595
26596 This is not obvious because bidi reordering could make
26597 buffer positions of a row be 1,2,3,102,101,100, and if we
26598 want to highlight characters in [50..60), we don't want
26599 this row, even though [50..60) does intersect [1..103),
26600 the range of character positions given by the row's start
26601 and end positions. */
26602 struct glyph *g = row->glyphs[TEXT_AREA];
26603 struct glyph *e = g + row->used[TEXT_AREA];
26604
26605 while (g < e)
26606 {
26607 if (((BUFFERP (g->object) || INTEGERP (g->object))
26608 && start_charpos <= g->charpos && g->charpos < end_charpos)
26609 /* A glyph that comes from DISP_STRING is by
26610 definition to be highlighted. */
26611 || EQ (g->object, disp_string))
26612 *start = row;
26613 g++;
26614 }
26615 if (*start)
26616 break;
26617 }
26618 }
26619
26620 /* Find the END row. */
26621 if (!*start
26622 /* If the last row is partially visible, start looking for END
26623 from that row, instead of starting from FIRST. */
26624 && !(row->enabled_p
26625 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26626 row = first;
26627 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26628 {
26629 struct glyph_row *next = row + 1;
26630 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26631
26632 if (!next->enabled_p
26633 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26634 /* The first row >= START whose range of displayed characters
26635 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26636 is the row END + 1. */
26637 || (start_charpos < next_start
26638 && end_charpos < next_start)
26639 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26640 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26641 && !next->ends_at_zv_p
26642 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26643 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26644 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26645 && !next->ends_at_zv_p
26646 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26647 {
26648 *end = row;
26649 break;
26650 }
26651 else
26652 {
26653 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26654 but none of the characters it displays are in the range, it is
26655 also END + 1. */
26656 struct glyph *g = next->glyphs[TEXT_AREA];
26657 struct glyph *s = g;
26658 struct glyph *e = g + next->used[TEXT_AREA];
26659
26660 while (g < e)
26661 {
26662 if (((BUFFERP (g->object) || INTEGERP (g->object))
26663 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26664 /* If the buffer position of the first glyph in
26665 the row is equal to END_CHARPOS, it means
26666 the last character to be highlighted is the
26667 newline of ROW, and we must consider NEXT as
26668 END, not END+1. */
26669 || (((!next->reversed_p && g == s)
26670 || (next->reversed_p && g == e - 1))
26671 && (g->charpos == end_charpos
26672 /* Special case for when NEXT is an
26673 empty line at ZV. */
26674 || (g->charpos == -1
26675 && !row->ends_at_zv_p
26676 && next_start == end_charpos)))))
26677 /* A glyph that comes from DISP_STRING is by
26678 definition to be highlighted. */
26679 || EQ (g->object, disp_string))
26680 break;
26681 g++;
26682 }
26683 if (g == e)
26684 {
26685 *end = row;
26686 break;
26687 }
26688 /* The first row that ends at ZV must be the last to be
26689 highlighted. */
26690 else if (next->ends_at_zv_p)
26691 {
26692 *end = next;
26693 break;
26694 }
26695 }
26696 }
26697 }
26698
26699 /* This function sets the mouse_face_* elements of HLINFO, assuming
26700 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26701 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26702 for the overlay or run of text properties specifying the mouse
26703 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26704 before-string and after-string that must also be highlighted.
26705 DISP_STRING, if non-nil, is a display string that may cover some
26706 or all of the highlighted text. */
26707
26708 static void
26709 mouse_face_from_buffer_pos (Lisp_Object window,
26710 Mouse_HLInfo *hlinfo,
26711 ptrdiff_t mouse_charpos,
26712 ptrdiff_t start_charpos,
26713 ptrdiff_t end_charpos,
26714 Lisp_Object before_string,
26715 Lisp_Object after_string,
26716 Lisp_Object disp_string)
26717 {
26718 struct window *w = XWINDOW (window);
26719 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26720 struct glyph_row *r1, *r2;
26721 struct glyph *glyph, *end;
26722 ptrdiff_t ignore, pos;
26723 int x;
26724
26725 eassert (NILP (disp_string) || STRINGP (disp_string));
26726 eassert (NILP (before_string) || STRINGP (before_string));
26727 eassert (NILP (after_string) || STRINGP (after_string));
26728
26729 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26730 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26731 if (r1 == NULL)
26732 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26733 /* If the before-string or display-string contains newlines,
26734 rows_from_pos_range skips to its last row. Move back. */
26735 if (!NILP (before_string) || !NILP (disp_string))
26736 {
26737 struct glyph_row *prev;
26738 while ((prev = r1 - 1, prev >= first)
26739 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26740 && prev->used[TEXT_AREA] > 0)
26741 {
26742 struct glyph *beg = prev->glyphs[TEXT_AREA];
26743 glyph = beg + prev->used[TEXT_AREA];
26744 while (--glyph >= beg && INTEGERP (glyph->object));
26745 if (glyph < beg
26746 || !(EQ (glyph->object, before_string)
26747 || EQ (glyph->object, disp_string)))
26748 break;
26749 r1 = prev;
26750 }
26751 }
26752 if (r2 == NULL)
26753 {
26754 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26755 hlinfo->mouse_face_past_end = 1;
26756 }
26757 else if (!NILP (after_string))
26758 {
26759 /* If the after-string has newlines, advance to its last row. */
26760 struct glyph_row *next;
26761 struct glyph_row *last
26762 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26763
26764 for (next = r2 + 1;
26765 next <= last
26766 && next->used[TEXT_AREA] > 0
26767 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26768 ++next)
26769 r2 = next;
26770 }
26771 /* The rest of the display engine assumes that mouse_face_beg_row is
26772 either above mouse_face_end_row or identical to it. But with
26773 bidi-reordered continued lines, the row for START_CHARPOS could
26774 be below the row for END_CHARPOS. If so, swap the rows and store
26775 them in correct order. */
26776 if (r1->y > r2->y)
26777 {
26778 struct glyph_row *tem = r2;
26779
26780 r2 = r1;
26781 r1 = tem;
26782 }
26783
26784 hlinfo->mouse_face_beg_y = r1->y;
26785 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26786 hlinfo->mouse_face_end_y = r2->y;
26787 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26788
26789 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26790 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26791 could be anywhere in the row and in any order. The strategy
26792 below is to find the leftmost and the rightmost glyph that
26793 belongs to either of these 3 strings, or whose position is
26794 between START_CHARPOS and END_CHARPOS, and highlight all the
26795 glyphs between those two. This may cover more than just the text
26796 between START_CHARPOS and END_CHARPOS if the range of characters
26797 strides the bidi level boundary, e.g. if the beginning is in R2L
26798 text while the end is in L2R text or vice versa. */
26799 if (!r1->reversed_p)
26800 {
26801 /* This row is in a left to right paragraph. Scan it left to
26802 right. */
26803 glyph = r1->glyphs[TEXT_AREA];
26804 end = glyph + r1->used[TEXT_AREA];
26805 x = r1->x;
26806
26807 /* Skip truncation glyphs at the start of the glyph row. */
26808 if (r1->displays_text_p)
26809 for (; glyph < end
26810 && INTEGERP (glyph->object)
26811 && glyph->charpos < 0;
26812 ++glyph)
26813 x += glyph->pixel_width;
26814
26815 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26816 or DISP_STRING, and the first glyph from buffer whose
26817 position is between START_CHARPOS and END_CHARPOS. */
26818 for (; glyph < end
26819 && !INTEGERP (glyph->object)
26820 && !EQ (glyph->object, disp_string)
26821 && !(BUFFERP (glyph->object)
26822 && (glyph->charpos >= start_charpos
26823 && glyph->charpos < end_charpos));
26824 ++glyph)
26825 {
26826 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26827 are present at buffer positions between START_CHARPOS and
26828 END_CHARPOS, or if they come from an overlay. */
26829 if (EQ (glyph->object, before_string))
26830 {
26831 pos = string_buffer_position (before_string,
26832 start_charpos);
26833 /* If pos == 0, it means before_string came from an
26834 overlay, not from a buffer position. */
26835 if (!pos || (pos >= start_charpos && pos < end_charpos))
26836 break;
26837 }
26838 else if (EQ (glyph->object, after_string))
26839 {
26840 pos = string_buffer_position (after_string, end_charpos);
26841 if (!pos || (pos >= start_charpos && pos < end_charpos))
26842 break;
26843 }
26844 x += glyph->pixel_width;
26845 }
26846 hlinfo->mouse_face_beg_x = x;
26847 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26848 }
26849 else
26850 {
26851 /* This row is in a right to left paragraph. Scan it right to
26852 left. */
26853 struct glyph *g;
26854
26855 end = r1->glyphs[TEXT_AREA] - 1;
26856 glyph = end + r1->used[TEXT_AREA];
26857
26858 /* Skip truncation glyphs at the start of the glyph row. */
26859 if (r1->displays_text_p)
26860 for (; glyph > end
26861 && INTEGERP (glyph->object)
26862 && glyph->charpos < 0;
26863 --glyph)
26864 ;
26865
26866 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26867 or DISP_STRING, and the first glyph from buffer whose
26868 position is between START_CHARPOS and END_CHARPOS. */
26869 for (; glyph > end
26870 && !INTEGERP (glyph->object)
26871 && !EQ (glyph->object, disp_string)
26872 && !(BUFFERP (glyph->object)
26873 && (glyph->charpos >= start_charpos
26874 && glyph->charpos < end_charpos));
26875 --glyph)
26876 {
26877 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26878 are present at buffer positions between START_CHARPOS and
26879 END_CHARPOS, or if they come from an overlay. */
26880 if (EQ (glyph->object, before_string))
26881 {
26882 pos = string_buffer_position (before_string, start_charpos);
26883 /* If pos == 0, it means before_string came from an
26884 overlay, not from a buffer position. */
26885 if (!pos || (pos >= start_charpos && pos < end_charpos))
26886 break;
26887 }
26888 else if (EQ (glyph->object, after_string))
26889 {
26890 pos = string_buffer_position (after_string, end_charpos);
26891 if (!pos || (pos >= start_charpos && pos < end_charpos))
26892 break;
26893 }
26894 }
26895
26896 glyph++; /* first glyph to the right of the highlighted area */
26897 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
26898 x += g->pixel_width;
26899 hlinfo->mouse_face_beg_x = x;
26900 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
26901 }
26902
26903 /* If the highlight ends in a different row, compute GLYPH and END
26904 for the end row. Otherwise, reuse the values computed above for
26905 the row where the highlight begins. */
26906 if (r2 != r1)
26907 {
26908 if (!r2->reversed_p)
26909 {
26910 glyph = r2->glyphs[TEXT_AREA];
26911 end = glyph + r2->used[TEXT_AREA];
26912 x = r2->x;
26913 }
26914 else
26915 {
26916 end = r2->glyphs[TEXT_AREA] - 1;
26917 glyph = end + r2->used[TEXT_AREA];
26918 }
26919 }
26920
26921 if (!r2->reversed_p)
26922 {
26923 /* Skip truncation and continuation glyphs near the end of the
26924 row, and also blanks and stretch glyphs inserted by
26925 extend_face_to_end_of_line. */
26926 while (end > glyph
26927 && INTEGERP ((end - 1)->object))
26928 --end;
26929 /* Scan the rest of the glyph row from the end, looking for the
26930 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26931 DISP_STRING, or whose position is between START_CHARPOS
26932 and END_CHARPOS */
26933 for (--end;
26934 end > glyph
26935 && !INTEGERP (end->object)
26936 && !EQ (end->object, disp_string)
26937 && !(BUFFERP (end->object)
26938 && (end->charpos >= start_charpos
26939 && end->charpos < end_charpos));
26940 --end)
26941 {
26942 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26943 are present at buffer positions between START_CHARPOS and
26944 END_CHARPOS, or if they come from an overlay. */
26945 if (EQ (end->object, before_string))
26946 {
26947 pos = string_buffer_position (before_string, start_charpos);
26948 if (!pos || (pos >= start_charpos && pos < end_charpos))
26949 break;
26950 }
26951 else if (EQ (end->object, after_string))
26952 {
26953 pos = string_buffer_position (after_string, end_charpos);
26954 if (!pos || (pos >= start_charpos && pos < end_charpos))
26955 break;
26956 }
26957 }
26958 /* Find the X coordinate of the last glyph to be highlighted. */
26959 for (; glyph <= end; ++glyph)
26960 x += glyph->pixel_width;
26961
26962 hlinfo->mouse_face_end_x = x;
26963 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
26964 }
26965 else
26966 {
26967 /* Skip truncation and continuation glyphs near the end of the
26968 row, and also blanks and stretch glyphs inserted by
26969 extend_face_to_end_of_line. */
26970 x = r2->x;
26971 end++;
26972 while (end < glyph
26973 && INTEGERP (end->object))
26974 {
26975 x += end->pixel_width;
26976 ++end;
26977 }
26978 /* Scan the rest of the glyph row from the end, looking for the
26979 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
26980 DISP_STRING, or whose position is between START_CHARPOS
26981 and END_CHARPOS */
26982 for ( ;
26983 end < glyph
26984 && !INTEGERP (end->object)
26985 && !EQ (end->object, disp_string)
26986 && !(BUFFERP (end->object)
26987 && (end->charpos >= start_charpos
26988 && end->charpos < end_charpos));
26989 ++end)
26990 {
26991 /* BEFORE_STRING or AFTER_STRING are only relevant if they
26992 are present at buffer positions between START_CHARPOS and
26993 END_CHARPOS, or if they come from an overlay. */
26994 if (EQ (end->object, before_string))
26995 {
26996 pos = string_buffer_position (before_string, start_charpos);
26997 if (!pos || (pos >= start_charpos && pos < end_charpos))
26998 break;
26999 }
27000 else if (EQ (end->object, after_string))
27001 {
27002 pos = string_buffer_position (after_string, end_charpos);
27003 if (!pos || (pos >= start_charpos && pos < end_charpos))
27004 break;
27005 }
27006 x += end->pixel_width;
27007 }
27008 /* If we exited the above loop because we arrived at the last
27009 glyph of the row, and its buffer position is still not in
27010 range, it means the last character in range is the preceding
27011 newline. Bump the end column and x values to get past the
27012 last glyph. */
27013 if (end == glyph
27014 && BUFFERP (end->object)
27015 && (end->charpos < start_charpos
27016 || end->charpos >= end_charpos))
27017 {
27018 x += end->pixel_width;
27019 ++end;
27020 }
27021 hlinfo->mouse_face_end_x = x;
27022 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27023 }
27024
27025 hlinfo->mouse_face_window = window;
27026 hlinfo->mouse_face_face_id
27027 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27028 mouse_charpos + 1,
27029 !hlinfo->mouse_face_hidden, -1);
27030 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27031 }
27032
27033 /* The following function is not used anymore (replaced with
27034 mouse_face_from_string_pos), but I leave it here for the time
27035 being, in case someone would. */
27036
27037 #if 0 /* not used */
27038
27039 /* Find the position of the glyph for position POS in OBJECT in
27040 window W's current matrix, and return in *X, *Y the pixel
27041 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27042
27043 RIGHT_P non-zero means return the position of the right edge of the
27044 glyph, RIGHT_P zero means return the left edge position.
27045
27046 If no glyph for POS exists in the matrix, return the position of
27047 the glyph with the next smaller position that is in the matrix, if
27048 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27049 exists in the matrix, return the position of the glyph with the
27050 next larger position in OBJECT.
27051
27052 Value is non-zero if a glyph was found. */
27053
27054 static int
27055 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27056 int *hpos, int *vpos, int *x, int *y, int right_p)
27057 {
27058 int yb = window_text_bottom_y (w);
27059 struct glyph_row *r;
27060 struct glyph *best_glyph = NULL;
27061 struct glyph_row *best_row = NULL;
27062 int best_x = 0;
27063
27064 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27065 r->enabled_p && r->y < yb;
27066 ++r)
27067 {
27068 struct glyph *g = r->glyphs[TEXT_AREA];
27069 struct glyph *e = g + r->used[TEXT_AREA];
27070 int gx;
27071
27072 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27073 if (EQ (g->object, object))
27074 {
27075 if (g->charpos == pos)
27076 {
27077 best_glyph = g;
27078 best_x = gx;
27079 best_row = r;
27080 goto found;
27081 }
27082 else if (best_glyph == NULL
27083 || ((eabs (g->charpos - pos)
27084 < eabs (best_glyph->charpos - pos))
27085 && (right_p
27086 ? g->charpos < pos
27087 : g->charpos > pos)))
27088 {
27089 best_glyph = g;
27090 best_x = gx;
27091 best_row = r;
27092 }
27093 }
27094 }
27095
27096 found:
27097
27098 if (best_glyph)
27099 {
27100 *x = best_x;
27101 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27102
27103 if (right_p)
27104 {
27105 *x += best_glyph->pixel_width;
27106 ++*hpos;
27107 }
27108
27109 *y = best_row->y;
27110 *vpos = best_row - w->current_matrix->rows;
27111 }
27112
27113 return best_glyph != NULL;
27114 }
27115 #endif /* not used */
27116
27117 /* Find the positions of the first and the last glyphs in window W's
27118 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27119 (assumed to be a string), and return in HLINFO's mouse_face_*
27120 members the pixel and column/row coordinates of those glyphs. */
27121
27122 static void
27123 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27124 Lisp_Object object,
27125 ptrdiff_t startpos, ptrdiff_t endpos)
27126 {
27127 int yb = window_text_bottom_y (w);
27128 struct glyph_row *r;
27129 struct glyph *g, *e;
27130 int gx;
27131 int found = 0;
27132
27133 /* Find the glyph row with at least one position in the range
27134 [STARTPOS..ENDPOS], and the first glyph in that row whose
27135 position belongs to that range. */
27136 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27137 r->enabled_p && r->y < yb;
27138 ++r)
27139 {
27140 if (!r->reversed_p)
27141 {
27142 g = r->glyphs[TEXT_AREA];
27143 e = g + r->used[TEXT_AREA];
27144 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27145 if (EQ (g->object, object)
27146 && startpos <= g->charpos && g->charpos <= endpos)
27147 {
27148 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27149 hlinfo->mouse_face_beg_y = r->y;
27150 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27151 hlinfo->mouse_face_beg_x = gx;
27152 found = 1;
27153 break;
27154 }
27155 }
27156 else
27157 {
27158 struct glyph *g1;
27159
27160 e = r->glyphs[TEXT_AREA];
27161 g = e + r->used[TEXT_AREA];
27162 for ( ; g > e; --g)
27163 if (EQ ((g-1)->object, object)
27164 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27165 {
27166 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27167 hlinfo->mouse_face_beg_y = r->y;
27168 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27169 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27170 gx += g1->pixel_width;
27171 hlinfo->mouse_face_beg_x = gx;
27172 found = 1;
27173 break;
27174 }
27175 }
27176 if (found)
27177 break;
27178 }
27179
27180 if (!found)
27181 return;
27182
27183 /* Starting with the next row, look for the first row which does NOT
27184 include any glyphs whose positions are in the range. */
27185 for (++r; r->enabled_p && r->y < yb; ++r)
27186 {
27187 g = r->glyphs[TEXT_AREA];
27188 e = g + r->used[TEXT_AREA];
27189 found = 0;
27190 for ( ; g < e; ++g)
27191 if (EQ (g->object, object)
27192 && startpos <= g->charpos && g->charpos <= endpos)
27193 {
27194 found = 1;
27195 break;
27196 }
27197 if (!found)
27198 break;
27199 }
27200
27201 /* The highlighted region ends on the previous row. */
27202 r--;
27203
27204 /* Set the end row and its vertical pixel coordinate. */
27205 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
27206 hlinfo->mouse_face_end_y = r->y;
27207
27208 /* Compute and set the end column and the end column's horizontal
27209 pixel coordinate. */
27210 if (!r->reversed_p)
27211 {
27212 g = r->glyphs[TEXT_AREA];
27213 e = g + r->used[TEXT_AREA];
27214 for ( ; e > g; --e)
27215 if (EQ ((e-1)->object, object)
27216 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27217 break;
27218 hlinfo->mouse_face_end_col = e - g;
27219
27220 for (gx = r->x; g < e; ++g)
27221 gx += g->pixel_width;
27222 hlinfo->mouse_face_end_x = gx;
27223 }
27224 else
27225 {
27226 e = r->glyphs[TEXT_AREA];
27227 g = e + r->used[TEXT_AREA];
27228 for (gx = r->x ; e < g; ++e)
27229 {
27230 if (EQ (e->object, object)
27231 && startpos <= e->charpos && e->charpos <= endpos)
27232 break;
27233 gx += e->pixel_width;
27234 }
27235 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27236 hlinfo->mouse_face_end_x = gx;
27237 }
27238 }
27239
27240 #ifdef HAVE_WINDOW_SYSTEM
27241
27242 /* See if position X, Y is within a hot-spot of an image. */
27243
27244 static int
27245 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27246 {
27247 if (!CONSP (hot_spot))
27248 return 0;
27249
27250 if (EQ (XCAR (hot_spot), Qrect))
27251 {
27252 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27253 Lisp_Object rect = XCDR (hot_spot);
27254 Lisp_Object tem;
27255 if (!CONSP (rect))
27256 return 0;
27257 if (!CONSP (XCAR (rect)))
27258 return 0;
27259 if (!CONSP (XCDR (rect)))
27260 return 0;
27261 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27262 return 0;
27263 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27264 return 0;
27265 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27266 return 0;
27267 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27268 return 0;
27269 return 1;
27270 }
27271 else if (EQ (XCAR (hot_spot), Qcircle))
27272 {
27273 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27274 Lisp_Object circ = XCDR (hot_spot);
27275 Lisp_Object lr, lx0, ly0;
27276 if (CONSP (circ)
27277 && CONSP (XCAR (circ))
27278 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27279 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27280 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27281 {
27282 double r = XFLOATINT (lr);
27283 double dx = XINT (lx0) - x;
27284 double dy = XINT (ly0) - y;
27285 return (dx * dx + dy * dy <= r * r);
27286 }
27287 }
27288 else if (EQ (XCAR (hot_spot), Qpoly))
27289 {
27290 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27291 if (VECTORP (XCDR (hot_spot)))
27292 {
27293 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27294 Lisp_Object *poly = v->contents;
27295 ptrdiff_t n = v->header.size;
27296 ptrdiff_t i;
27297 int inside = 0;
27298 Lisp_Object lx, ly;
27299 int x0, y0;
27300
27301 /* Need an even number of coordinates, and at least 3 edges. */
27302 if (n < 6 || n & 1)
27303 return 0;
27304
27305 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27306 If count is odd, we are inside polygon. Pixels on edges
27307 may or may not be included depending on actual geometry of the
27308 polygon. */
27309 if ((lx = poly[n-2], !INTEGERP (lx))
27310 || (ly = poly[n-1], !INTEGERP (lx)))
27311 return 0;
27312 x0 = XINT (lx), y0 = XINT (ly);
27313 for (i = 0; i < n; i += 2)
27314 {
27315 int x1 = x0, y1 = y0;
27316 if ((lx = poly[i], !INTEGERP (lx))
27317 || (ly = poly[i+1], !INTEGERP (ly)))
27318 return 0;
27319 x0 = XINT (lx), y0 = XINT (ly);
27320
27321 /* Does this segment cross the X line? */
27322 if (x0 >= x)
27323 {
27324 if (x1 >= x)
27325 continue;
27326 }
27327 else if (x1 < x)
27328 continue;
27329 if (y > y0 && y > y1)
27330 continue;
27331 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27332 inside = !inside;
27333 }
27334 return inside;
27335 }
27336 }
27337 return 0;
27338 }
27339
27340 Lisp_Object
27341 find_hot_spot (Lisp_Object map, int x, int y)
27342 {
27343 while (CONSP (map))
27344 {
27345 if (CONSP (XCAR (map))
27346 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27347 return XCAR (map);
27348 map = XCDR (map);
27349 }
27350
27351 return Qnil;
27352 }
27353
27354 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27355 3, 3, 0,
27356 doc: /* Lookup in image map MAP coordinates X and Y.
27357 An image map is an alist where each element has the format (AREA ID PLIST).
27358 An AREA is specified as either a rectangle, a circle, or a polygon:
27359 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27360 pixel coordinates of the upper left and bottom right corners.
27361 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27362 and the radius of the circle; r may be a float or integer.
27363 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27364 vector describes one corner in the polygon.
27365 Returns the alist element for the first matching AREA in MAP. */)
27366 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27367 {
27368 if (NILP (map))
27369 return Qnil;
27370
27371 CHECK_NUMBER (x);
27372 CHECK_NUMBER (y);
27373
27374 return find_hot_spot (map,
27375 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27376 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27377 }
27378
27379
27380 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27381 static void
27382 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27383 {
27384 /* Do not change cursor shape while dragging mouse. */
27385 if (!NILP (do_mouse_tracking))
27386 return;
27387
27388 if (!NILP (pointer))
27389 {
27390 if (EQ (pointer, Qarrow))
27391 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27392 else if (EQ (pointer, Qhand))
27393 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27394 else if (EQ (pointer, Qtext))
27395 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27396 else if (EQ (pointer, intern ("hdrag")))
27397 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27398 #ifdef HAVE_X_WINDOWS
27399 else if (EQ (pointer, intern ("vdrag")))
27400 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27401 #endif
27402 else if (EQ (pointer, intern ("hourglass")))
27403 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27404 else if (EQ (pointer, Qmodeline))
27405 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27406 else
27407 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27408 }
27409
27410 if (cursor != No_Cursor)
27411 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27412 }
27413
27414 #endif /* HAVE_WINDOW_SYSTEM */
27415
27416 /* Take proper action when mouse has moved to the mode or header line
27417 or marginal area AREA of window W, x-position X and y-position Y.
27418 X is relative to the start of the text display area of W, so the
27419 width of bitmap areas and scroll bars must be subtracted to get a
27420 position relative to the start of the mode line. */
27421
27422 static void
27423 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27424 enum window_part area)
27425 {
27426 struct window *w = XWINDOW (window);
27427 struct frame *f = XFRAME (w->frame);
27428 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27429 #ifdef HAVE_WINDOW_SYSTEM
27430 Display_Info *dpyinfo;
27431 #endif
27432 Cursor cursor = No_Cursor;
27433 Lisp_Object pointer = Qnil;
27434 int dx, dy, width, height;
27435 ptrdiff_t charpos;
27436 Lisp_Object string, object = Qnil;
27437 Lisp_Object pos IF_LINT (= Qnil), help;
27438
27439 Lisp_Object mouse_face;
27440 int original_x_pixel = x;
27441 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27442 struct glyph_row *row IF_LINT (= 0);
27443
27444 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27445 {
27446 int x0;
27447 struct glyph *end;
27448
27449 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27450 returns them in row/column units! */
27451 string = mode_line_string (w, area, &x, &y, &charpos,
27452 &object, &dx, &dy, &width, &height);
27453
27454 row = (area == ON_MODE_LINE
27455 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27456 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27457
27458 /* Find the glyph under the mouse pointer. */
27459 if (row->mode_line_p && row->enabled_p)
27460 {
27461 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27462 end = glyph + row->used[TEXT_AREA];
27463
27464 for (x0 = original_x_pixel;
27465 glyph < end && x0 >= glyph->pixel_width;
27466 ++glyph)
27467 x0 -= glyph->pixel_width;
27468
27469 if (glyph >= end)
27470 glyph = NULL;
27471 }
27472 }
27473 else
27474 {
27475 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27476 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27477 returns them in row/column units! */
27478 string = marginal_area_string (w, area, &x, &y, &charpos,
27479 &object, &dx, &dy, &width, &height);
27480 }
27481
27482 help = Qnil;
27483
27484 #ifdef HAVE_WINDOW_SYSTEM
27485 if (IMAGEP (object))
27486 {
27487 Lisp_Object image_map, hotspot;
27488 if ((image_map = Fplist_get (XCDR (object), QCmap),
27489 !NILP (image_map))
27490 && (hotspot = find_hot_spot (image_map, dx, dy),
27491 CONSP (hotspot))
27492 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27493 {
27494 Lisp_Object plist;
27495
27496 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27497 If so, we could look for mouse-enter, mouse-leave
27498 properties in PLIST (and do something...). */
27499 hotspot = XCDR (hotspot);
27500 if (CONSP (hotspot)
27501 && (plist = XCAR (hotspot), CONSP (plist)))
27502 {
27503 pointer = Fplist_get (plist, Qpointer);
27504 if (NILP (pointer))
27505 pointer = Qhand;
27506 help = Fplist_get (plist, Qhelp_echo);
27507 if (!NILP (help))
27508 {
27509 help_echo_string = help;
27510 XSETWINDOW (help_echo_window, w);
27511 help_echo_object = w->buffer;
27512 help_echo_pos = charpos;
27513 }
27514 }
27515 }
27516 if (NILP (pointer))
27517 pointer = Fplist_get (XCDR (object), QCpointer);
27518 }
27519 #endif /* HAVE_WINDOW_SYSTEM */
27520
27521 if (STRINGP (string))
27522 pos = make_number (charpos);
27523
27524 /* Set the help text and mouse pointer. If the mouse is on a part
27525 of the mode line without any text (e.g. past the right edge of
27526 the mode line text), use the default help text and pointer. */
27527 if (STRINGP (string) || area == ON_MODE_LINE)
27528 {
27529 /* Arrange to display the help by setting the global variables
27530 help_echo_string, help_echo_object, and help_echo_pos. */
27531 if (NILP (help))
27532 {
27533 if (STRINGP (string))
27534 help = Fget_text_property (pos, Qhelp_echo, string);
27535
27536 if (!NILP (help))
27537 {
27538 help_echo_string = help;
27539 XSETWINDOW (help_echo_window, w);
27540 help_echo_object = string;
27541 help_echo_pos = charpos;
27542 }
27543 else if (area == ON_MODE_LINE)
27544 {
27545 Lisp_Object default_help
27546 = buffer_local_value_1 (Qmode_line_default_help_echo,
27547 w->buffer);
27548
27549 if (STRINGP (default_help))
27550 {
27551 help_echo_string = default_help;
27552 XSETWINDOW (help_echo_window, w);
27553 help_echo_object = Qnil;
27554 help_echo_pos = -1;
27555 }
27556 }
27557 }
27558
27559 #ifdef HAVE_WINDOW_SYSTEM
27560 /* Change the mouse pointer according to what is under it. */
27561 if (FRAME_WINDOW_P (f))
27562 {
27563 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27564 if (STRINGP (string))
27565 {
27566 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27567
27568 if (NILP (pointer))
27569 pointer = Fget_text_property (pos, Qpointer, string);
27570
27571 /* Change the mouse pointer according to what is under X/Y. */
27572 if (NILP (pointer)
27573 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27574 {
27575 Lisp_Object map;
27576 map = Fget_text_property (pos, Qlocal_map, string);
27577 if (!KEYMAPP (map))
27578 map = Fget_text_property (pos, Qkeymap, string);
27579 if (!KEYMAPP (map))
27580 cursor = dpyinfo->vertical_scroll_bar_cursor;
27581 }
27582 }
27583 else
27584 /* Default mode-line pointer. */
27585 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27586 }
27587 #endif
27588 }
27589
27590 /* Change the mouse face according to what is under X/Y. */
27591 if (STRINGP (string))
27592 {
27593 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27594 if (!NILP (mouse_face)
27595 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27596 && glyph)
27597 {
27598 Lisp_Object b, e;
27599
27600 struct glyph * tmp_glyph;
27601
27602 int gpos;
27603 int gseq_length;
27604 int total_pixel_width;
27605 ptrdiff_t begpos, endpos, ignore;
27606
27607 int vpos, hpos;
27608
27609 b = Fprevious_single_property_change (make_number (charpos + 1),
27610 Qmouse_face, string, Qnil);
27611 if (NILP (b))
27612 begpos = 0;
27613 else
27614 begpos = XINT (b);
27615
27616 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27617 if (NILP (e))
27618 endpos = SCHARS (string);
27619 else
27620 endpos = XINT (e);
27621
27622 /* Calculate the glyph position GPOS of GLYPH in the
27623 displayed string, relative to the beginning of the
27624 highlighted part of the string.
27625
27626 Note: GPOS is different from CHARPOS. CHARPOS is the
27627 position of GLYPH in the internal string object. A mode
27628 line string format has structures which are converted to
27629 a flattened string by the Emacs Lisp interpreter. The
27630 internal string is an element of those structures. The
27631 displayed string is the flattened string. */
27632 tmp_glyph = row_start_glyph;
27633 while (tmp_glyph < glyph
27634 && (!(EQ (tmp_glyph->object, glyph->object)
27635 && begpos <= tmp_glyph->charpos
27636 && tmp_glyph->charpos < endpos)))
27637 tmp_glyph++;
27638 gpos = glyph - tmp_glyph;
27639
27640 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27641 the highlighted part of the displayed string to which
27642 GLYPH belongs. Note: GSEQ_LENGTH is different from
27643 SCHARS (STRING), because the latter returns the length of
27644 the internal string. */
27645 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27646 tmp_glyph > glyph
27647 && (!(EQ (tmp_glyph->object, glyph->object)
27648 && begpos <= tmp_glyph->charpos
27649 && tmp_glyph->charpos < endpos));
27650 tmp_glyph--)
27651 ;
27652 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27653
27654 /* Calculate the total pixel width of all the glyphs between
27655 the beginning of the highlighted area and GLYPH. */
27656 total_pixel_width = 0;
27657 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27658 total_pixel_width += tmp_glyph->pixel_width;
27659
27660 /* Pre calculation of re-rendering position. Note: X is in
27661 column units here, after the call to mode_line_string or
27662 marginal_area_string. */
27663 hpos = x - gpos;
27664 vpos = (area == ON_MODE_LINE
27665 ? (w->current_matrix)->nrows - 1
27666 : 0);
27667
27668 /* If GLYPH's position is included in the region that is
27669 already drawn in mouse face, we have nothing to do. */
27670 if ( EQ (window, hlinfo->mouse_face_window)
27671 && (!row->reversed_p
27672 ? (hlinfo->mouse_face_beg_col <= hpos
27673 && hpos < hlinfo->mouse_face_end_col)
27674 /* In R2L rows we swap BEG and END, see below. */
27675 : (hlinfo->mouse_face_end_col <= hpos
27676 && hpos < hlinfo->mouse_face_beg_col))
27677 && hlinfo->mouse_face_beg_row == vpos )
27678 return;
27679
27680 if (clear_mouse_face (hlinfo))
27681 cursor = No_Cursor;
27682
27683 if (!row->reversed_p)
27684 {
27685 hlinfo->mouse_face_beg_col = hpos;
27686 hlinfo->mouse_face_beg_x = original_x_pixel
27687 - (total_pixel_width + dx);
27688 hlinfo->mouse_face_end_col = hpos + gseq_length;
27689 hlinfo->mouse_face_end_x = 0;
27690 }
27691 else
27692 {
27693 /* In R2L rows, show_mouse_face expects BEG and END
27694 coordinates to be swapped. */
27695 hlinfo->mouse_face_end_col = hpos;
27696 hlinfo->mouse_face_end_x = original_x_pixel
27697 - (total_pixel_width + dx);
27698 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27699 hlinfo->mouse_face_beg_x = 0;
27700 }
27701
27702 hlinfo->mouse_face_beg_row = vpos;
27703 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27704 hlinfo->mouse_face_beg_y = 0;
27705 hlinfo->mouse_face_end_y = 0;
27706 hlinfo->mouse_face_past_end = 0;
27707 hlinfo->mouse_face_window = window;
27708
27709 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27710 charpos,
27711 0, 0, 0,
27712 &ignore,
27713 glyph->face_id,
27714 1);
27715 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27716
27717 if (NILP (pointer))
27718 pointer = Qhand;
27719 }
27720 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27721 clear_mouse_face (hlinfo);
27722 }
27723 #ifdef HAVE_WINDOW_SYSTEM
27724 if (FRAME_WINDOW_P (f))
27725 define_frame_cursor1 (f, cursor, pointer);
27726 #endif
27727 }
27728
27729
27730 /* EXPORT:
27731 Take proper action when the mouse has moved to position X, Y on
27732 frame F as regards highlighting characters that have mouse-face
27733 properties. Also de-highlighting chars where the mouse was before.
27734 X and Y can be negative or out of range. */
27735
27736 void
27737 note_mouse_highlight (struct frame *f, int x, int y)
27738 {
27739 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27740 enum window_part part = ON_NOTHING;
27741 Lisp_Object window;
27742 struct window *w;
27743 Cursor cursor = No_Cursor;
27744 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27745 struct buffer *b;
27746
27747 /* When a menu is active, don't highlight because this looks odd. */
27748 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27749 if (popup_activated ())
27750 return;
27751 #endif
27752
27753 if (NILP (Vmouse_highlight)
27754 || !f->glyphs_initialized_p
27755 || f->pointer_invisible)
27756 return;
27757
27758 hlinfo->mouse_face_mouse_x = x;
27759 hlinfo->mouse_face_mouse_y = y;
27760 hlinfo->mouse_face_mouse_frame = f;
27761
27762 if (hlinfo->mouse_face_defer)
27763 return;
27764
27765 /* Which window is that in? */
27766 window = window_from_coordinates (f, x, y, &part, 1);
27767
27768 /* If displaying active text in another window, clear that. */
27769 if (! EQ (window, hlinfo->mouse_face_window)
27770 /* Also clear if we move out of text area in same window. */
27771 || (!NILP (hlinfo->mouse_face_window)
27772 && !NILP (window)
27773 && part != ON_TEXT
27774 && part != ON_MODE_LINE
27775 && part != ON_HEADER_LINE))
27776 clear_mouse_face (hlinfo);
27777
27778 /* Not on a window -> return. */
27779 if (!WINDOWP (window))
27780 return;
27781
27782 /* Reset help_echo_string. It will get recomputed below. */
27783 help_echo_string = Qnil;
27784
27785 /* Convert to window-relative pixel coordinates. */
27786 w = XWINDOW (window);
27787 frame_to_window_pixel_xy (w, &x, &y);
27788
27789 #ifdef HAVE_WINDOW_SYSTEM
27790 /* Handle tool-bar window differently since it doesn't display a
27791 buffer. */
27792 if (EQ (window, f->tool_bar_window))
27793 {
27794 note_tool_bar_highlight (f, x, y);
27795 return;
27796 }
27797 #endif
27798
27799 /* Mouse is on the mode, header line or margin? */
27800 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27801 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27802 {
27803 note_mode_line_or_margin_highlight (window, x, y, part);
27804 return;
27805 }
27806
27807 #ifdef HAVE_WINDOW_SYSTEM
27808 if (part == ON_VERTICAL_BORDER)
27809 {
27810 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27811 help_echo_string = build_string ("drag-mouse-1: resize");
27812 }
27813 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27814 || part == ON_SCROLL_BAR)
27815 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27816 else
27817 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27818 #endif
27819
27820 /* Are we in a window whose display is up to date?
27821 And verify the buffer's text has not changed. */
27822 b = XBUFFER (w->buffer);
27823 if (part == ON_TEXT
27824 && EQ (w->window_end_valid, w->buffer)
27825 && w->last_modified == BUF_MODIFF (b)
27826 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
27827 {
27828 int hpos, vpos, dx, dy, area = LAST_AREA;
27829 ptrdiff_t pos;
27830 struct glyph *glyph;
27831 Lisp_Object object;
27832 Lisp_Object mouse_face = Qnil, position;
27833 Lisp_Object *overlay_vec = NULL;
27834 ptrdiff_t i, noverlays;
27835 struct buffer *obuf;
27836 ptrdiff_t obegv, ozv;
27837 int same_region;
27838
27839 /* Find the glyph under X/Y. */
27840 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
27841
27842 #ifdef HAVE_WINDOW_SYSTEM
27843 /* Look for :pointer property on image. */
27844 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
27845 {
27846 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
27847 if (img != NULL && IMAGEP (img->spec))
27848 {
27849 Lisp_Object image_map, hotspot;
27850 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
27851 !NILP (image_map))
27852 && (hotspot = find_hot_spot (image_map,
27853 glyph->slice.img.x + dx,
27854 glyph->slice.img.y + dy),
27855 CONSP (hotspot))
27856 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27857 {
27858 Lisp_Object plist;
27859
27860 /* Could check XCAR (hotspot) to see if we enter/leave
27861 this hot-spot.
27862 If so, we could look for mouse-enter, mouse-leave
27863 properties in PLIST (and do something...). */
27864 hotspot = XCDR (hotspot);
27865 if (CONSP (hotspot)
27866 && (plist = XCAR (hotspot), CONSP (plist)))
27867 {
27868 pointer = Fplist_get (plist, Qpointer);
27869 if (NILP (pointer))
27870 pointer = Qhand;
27871 help_echo_string = Fplist_get (plist, Qhelp_echo);
27872 if (!NILP (help_echo_string))
27873 {
27874 help_echo_window = window;
27875 help_echo_object = glyph->object;
27876 help_echo_pos = glyph->charpos;
27877 }
27878 }
27879 }
27880 if (NILP (pointer))
27881 pointer = Fplist_get (XCDR (img->spec), QCpointer);
27882 }
27883 }
27884 #endif /* HAVE_WINDOW_SYSTEM */
27885
27886 /* Clear mouse face if X/Y not over text. */
27887 if (glyph == NULL
27888 || area != TEXT_AREA
27889 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
27890 /* Glyph's OBJECT is an integer for glyphs inserted by the
27891 display engine for its internal purposes, like truncation
27892 and continuation glyphs and blanks beyond the end of
27893 line's text on text terminals. If we are over such a
27894 glyph, we are not over any text. */
27895 || INTEGERP (glyph->object)
27896 /* R2L rows have a stretch glyph at their front, which
27897 stands for no text, whereas L2R rows have no glyphs at
27898 all beyond the end of text. Treat such stretch glyphs
27899 like we do with NULL glyphs in L2R rows. */
27900 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
27901 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
27902 && glyph->type == STRETCH_GLYPH
27903 && glyph->avoid_cursor_p))
27904 {
27905 if (clear_mouse_face (hlinfo))
27906 cursor = No_Cursor;
27907 #ifdef HAVE_WINDOW_SYSTEM
27908 if (FRAME_WINDOW_P (f) && NILP (pointer))
27909 {
27910 if (area != TEXT_AREA)
27911 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27912 else
27913 pointer = Vvoid_text_area_pointer;
27914 }
27915 #endif
27916 goto set_cursor;
27917 }
27918
27919 pos = glyph->charpos;
27920 object = glyph->object;
27921 if (!STRINGP (object) && !BUFFERP (object))
27922 goto set_cursor;
27923
27924 /* If we get an out-of-range value, return now; avoid an error. */
27925 if (BUFFERP (object) && pos > BUF_Z (b))
27926 goto set_cursor;
27927
27928 /* Make the window's buffer temporarily current for
27929 overlays_at and compute_char_face. */
27930 obuf = current_buffer;
27931 current_buffer = b;
27932 obegv = BEGV;
27933 ozv = ZV;
27934 BEGV = BEG;
27935 ZV = Z;
27936
27937 /* Is this char mouse-active or does it have help-echo? */
27938 position = make_number (pos);
27939
27940 if (BUFFERP (object))
27941 {
27942 /* Put all the overlays we want in a vector in overlay_vec. */
27943 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
27944 /* Sort overlays into increasing priority order. */
27945 noverlays = sort_overlays (overlay_vec, noverlays, w);
27946 }
27947 else
27948 noverlays = 0;
27949
27950 same_region = coords_in_mouse_face_p (w, hpos, vpos);
27951
27952 if (same_region)
27953 cursor = No_Cursor;
27954
27955 /* Check mouse-face highlighting. */
27956 if (! same_region
27957 /* If there exists an overlay with mouse-face overlapping
27958 the one we are currently highlighting, we have to
27959 check if we enter the overlapping overlay, and then
27960 highlight only that. */
27961 || (OVERLAYP (hlinfo->mouse_face_overlay)
27962 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
27963 {
27964 /* Find the highest priority overlay with a mouse-face. */
27965 Lisp_Object overlay = Qnil;
27966 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
27967 {
27968 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
27969 if (!NILP (mouse_face))
27970 overlay = overlay_vec[i];
27971 }
27972
27973 /* If we're highlighting the same overlay as before, there's
27974 no need to do that again. */
27975 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
27976 goto check_help_echo;
27977 hlinfo->mouse_face_overlay = overlay;
27978
27979 /* Clear the display of the old active region, if any. */
27980 if (clear_mouse_face (hlinfo))
27981 cursor = No_Cursor;
27982
27983 /* If no overlay applies, get a text property. */
27984 if (NILP (overlay))
27985 mouse_face = Fget_text_property (position, Qmouse_face, object);
27986
27987 /* Next, compute the bounds of the mouse highlighting and
27988 display it. */
27989 if (!NILP (mouse_face) && STRINGP (object))
27990 {
27991 /* The mouse-highlighting comes from a display string
27992 with a mouse-face. */
27993 Lisp_Object s, e;
27994 ptrdiff_t ignore;
27995
27996 s = Fprevious_single_property_change
27997 (make_number (pos + 1), Qmouse_face, object, Qnil);
27998 e = Fnext_single_property_change
27999 (position, Qmouse_face, object, Qnil);
28000 if (NILP (s))
28001 s = make_number (0);
28002 if (NILP (e))
28003 e = make_number (SCHARS (object) - 1);
28004 mouse_face_from_string_pos (w, hlinfo, object,
28005 XINT (s), XINT (e));
28006 hlinfo->mouse_face_past_end = 0;
28007 hlinfo->mouse_face_window = window;
28008 hlinfo->mouse_face_face_id
28009 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28010 glyph->face_id, 1);
28011 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28012 cursor = No_Cursor;
28013 }
28014 else
28015 {
28016 /* The mouse-highlighting, if any, comes from an overlay
28017 or text property in the buffer. */
28018 Lisp_Object buffer IF_LINT (= Qnil);
28019 Lisp_Object disp_string IF_LINT (= Qnil);
28020
28021 if (STRINGP (object))
28022 {
28023 /* If we are on a display string with no mouse-face,
28024 check if the text under it has one. */
28025 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28026 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28027 pos = string_buffer_position (object, start);
28028 if (pos > 0)
28029 {
28030 mouse_face = get_char_property_and_overlay
28031 (make_number (pos), Qmouse_face, w->buffer, &overlay);
28032 buffer = w->buffer;
28033 disp_string = object;
28034 }
28035 }
28036 else
28037 {
28038 buffer = object;
28039 disp_string = Qnil;
28040 }
28041
28042 if (!NILP (mouse_face))
28043 {
28044 Lisp_Object before, after;
28045 Lisp_Object before_string, after_string;
28046 /* To correctly find the limits of mouse highlight
28047 in a bidi-reordered buffer, we must not use the
28048 optimization of limiting the search in
28049 previous-single-property-change and
28050 next-single-property-change, because
28051 rows_from_pos_range needs the real start and end
28052 positions to DTRT in this case. That's because
28053 the first row visible in a window does not
28054 necessarily display the character whose position
28055 is the smallest. */
28056 Lisp_Object lim1 =
28057 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28058 ? Fmarker_position (w->start)
28059 : Qnil;
28060 Lisp_Object lim2 =
28061 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28062 ? make_number (BUF_Z (XBUFFER (buffer))
28063 - XFASTINT (w->window_end_pos))
28064 : Qnil;
28065
28066 if (NILP (overlay))
28067 {
28068 /* Handle the text property case. */
28069 before = Fprevious_single_property_change
28070 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28071 after = Fnext_single_property_change
28072 (make_number (pos), Qmouse_face, buffer, lim2);
28073 before_string = after_string = Qnil;
28074 }
28075 else
28076 {
28077 /* Handle the overlay case. */
28078 before = Foverlay_start (overlay);
28079 after = Foverlay_end (overlay);
28080 before_string = Foverlay_get (overlay, Qbefore_string);
28081 after_string = Foverlay_get (overlay, Qafter_string);
28082
28083 if (!STRINGP (before_string)) before_string = Qnil;
28084 if (!STRINGP (after_string)) after_string = Qnil;
28085 }
28086
28087 mouse_face_from_buffer_pos (window, hlinfo, pos,
28088 NILP (before)
28089 ? 1
28090 : XFASTINT (before),
28091 NILP (after)
28092 ? BUF_Z (XBUFFER (buffer))
28093 : XFASTINT (after),
28094 before_string, after_string,
28095 disp_string);
28096 cursor = No_Cursor;
28097 }
28098 }
28099 }
28100
28101 check_help_echo:
28102
28103 /* Look for a `help-echo' property. */
28104 if (NILP (help_echo_string)) {
28105 Lisp_Object help, overlay;
28106
28107 /* Check overlays first. */
28108 help = overlay = Qnil;
28109 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28110 {
28111 overlay = overlay_vec[i];
28112 help = Foverlay_get (overlay, Qhelp_echo);
28113 }
28114
28115 if (!NILP (help))
28116 {
28117 help_echo_string = help;
28118 help_echo_window = window;
28119 help_echo_object = overlay;
28120 help_echo_pos = pos;
28121 }
28122 else
28123 {
28124 Lisp_Object obj = glyph->object;
28125 ptrdiff_t charpos = glyph->charpos;
28126
28127 /* Try text properties. */
28128 if (STRINGP (obj)
28129 && charpos >= 0
28130 && charpos < SCHARS (obj))
28131 {
28132 help = Fget_text_property (make_number (charpos),
28133 Qhelp_echo, obj);
28134 if (NILP (help))
28135 {
28136 /* If the string itself doesn't specify a help-echo,
28137 see if the buffer text ``under'' it does. */
28138 struct glyph_row *r
28139 = MATRIX_ROW (w->current_matrix, vpos);
28140 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28141 ptrdiff_t p = string_buffer_position (obj, start);
28142 if (p > 0)
28143 {
28144 help = Fget_char_property (make_number (p),
28145 Qhelp_echo, w->buffer);
28146 if (!NILP (help))
28147 {
28148 charpos = p;
28149 obj = w->buffer;
28150 }
28151 }
28152 }
28153 }
28154 else if (BUFFERP (obj)
28155 && charpos >= BEGV
28156 && charpos < ZV)
28157 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28158 obj);
28159
28160 if (!NILP (help))
28161 {
28162 help_echo_string = help;
28163 help_echo_window = window;
28164 help_echo_object = obj;
28165 help_echo_pos = charpos;
28166 }
28167 }
28168 }
28169
28170 #ifdef HAVE_WINDOW_SYSTEM
28171 /* Look for a `pointer' property. */
28172 if (FRAME_WINDOW_P (f) && NILP (pointer))
28173 {
28174 /* Check overlays first. */
28175 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28176 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28177
28178 if (NILP (pointer))
28179 {
28180 Lisp_Object obj = glyph->object;
28181 ptrdiff_t charpos = glyph->charpos;
28182
28183 /* Try text properties. */
28184 if (STRINGP (obj)
28185 && charpos >= 0
28186 && charpos < SCHARS (obj))
28187 {
28188 pointer = Fget_text_property (make_number (charpos),
28189 Qpointer, obj);
28190 if (NILP (pointer))
28191 {
28192 /* If the string itself doesn't specify a pointer,
28193 see if the buffer text ``under'' it does. */
28194 struct glyph_row *r
28195 = MATRIX_ROW (w->current_matrix, vpos);
28196 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28197 ptrdiff_t p = string_buffer_position (obj, start);
28198 if (p > 0)
28199 pointer = Fget_char_property (make_number (p),
28200 Qpointer, w->buffer);
28201 }
28202 }
28203 else if (BUFFERP (obj)
28204 && charpos >= BEGV
28205 && charpos < ZV)
28206 pointer = Fget_text_property (make_number (charpos),
28207 Qpointer, obj);
28208 }
28209 }
28210 #endif /* HAVE_WINDOW_SYSTEM */
28211
28212 BEGV = obegv;
28213 ZV = ozv;
28214 current_buffer = obuf;
28215 }
28216
28217 set_cursor:
28218
28219 #ifdef HAVE_WINDOW_SYSTEM
28220 if (FRAME_WINDOW_P (f))
28221 define_frame_cursor1 (f, cursor, pointer);
28222 #else
28223 /* This is here to prevent a compiler error, about "label at end of
28224 compound statement". */
28225 return;
28226 #endif
28227 }
28228
28229
28230 /* EXPORT for RIF:
28231 Clear any mouse-face on window W. This function is part of the
28232 redisplay interface, and is called from try_window_id and similar
28233 functions to ensure the mouse-highlight is off. */
28234
28235 void
28236 x_clear_window_mouse_face (struct window *w)
28237 {
28238 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28239 Lisp_Object window;
28240
28241 block_input ();
28242 XSETWINDOW (window, w);
28243 if (EQ (window, hlinfo->mouse_face_window))
28244 clear_mouse_face (hlinfo);
28245 unblock_input ();
28246 }
28247
28248
28249 /* EXPORT:
28250 Just discard the mouse face information for frame F, if any.
28251 This is used when the size of F is changed. */
28252
28253 void
28254 cancel_mouse_face (struct frame *f)
28255 {
28256 Lisp_Object window;
28257 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28258
28259 window = hlinfo->mouse_face_window;
28260 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28261 {
28262 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28263 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28264 hlinfo->mouse_face_window = Qnil;
28265 }
28266 }
28267
28268
28269 \f
28270 /***********************************************************************
28271 Exposure Events
28272 ***********************************************************************/
28273
28274 #ifdef HAVE_WINDOW_SYSTEM
28275
28276 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28277 which intersects rectangle R. R is in window-relative coordinates. */
28278
28279 static void
28280 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28281 enum glyph_row_area area)
28282 {
28283 struct glyph *first = row->glyphs[area];
28284 struct glyph *end = row->glyphs[area] + row->used[area];
28285 struct glyph *last;
28286 int first_x, start_x, x;
28287
28288 if (area == TEXT_AREA && row->fill_line_p)
28289 /* If row extends face to end of line write the whole line. */
28290 draw_glyphs (w, 0, row, area,
28291 0, row->used[area],
28292 DRAW_NORMAL_TEXT, 0);
28293 else
28294 {
28295 /* Set START_X to the window-relative start position for drawing glyphs of
28296 AREA. The first glyph of the text area can be partially visible.
28297 The first glyphs of other areas cannot. */
28298 start_x = window_box_left_offset (w, area);
28299 x = start_x;
28300 if (area == TEXT_AREA)
28301 x += row->x;
28302
28303 /* Find the first glyph that must be redrawn. */
28304 while (first < end
28305 && x + first->pixel_width < r->x)
28306 {
28307 x += first->pixel_width;
28308 ++first;
28309 }
28310
28311 /* Find the last one. */
28312 last = first;
28313 first_x = x;
28314 while (last < end
28315 && x < r->x + r->width)
28316 {
28317 x += last->pixel_width;
28318 ++last;
28319 }
28320
28321 /* Repaint. */
28322 if (last > first)
28323 draw_glyphs (w, first_x - start_x, row, area,
28324 first - row->glyphs[area], last - row->glyphs[area],
28325 DRAW_NORMAL_TEXT, 0);
28326 }
28327 }
28328
28329
28330 /* Redraw the parts of the glyph row ROW on window W intersecting
28331 rectangle R. R is in window-relative coordinates. Value is
28332 non-zero if mouse-face was overwritten. */
28333
28334 static int
28335 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28336 {
28337 eassert (row->enabled_p);
28338
28339 if (row->mode_line_p || w->pseudo_window_p)
28340 draw_glyphs (w, 0, row, TEXT_AREA,
28341 0, row->used[TEXT_AREA],
28342 DRAW_NORMAL_TEXT, 0);
28343 else
28344 {
28345 if (row->used[LEFT_MARGIN_AREA])
28346 expose_area (w, row, r, LEFT_MARGIN_AREA);
28347 if (row->used[TEXT_AREA])
28348 expose_area (w, row, r, TEXT_AREA);
28349 if (row->used[RIGHT_MARGIN_AREA])
28350 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28351 draw_row_fringe_bitmaps (w, row);
28352 }
28353
28354 return row->mouse_face_p;
28355 }
28356
28357
28358 /* Redraw those parts of glyphs rows during expose event handling that
28359 overlap other rows. Redrawing of an exposed line writes over parts
28360 of lines overlapping that exposed line; this function fixes that.
28361
28362 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28363 row in W's current matrix that is exposed and overlaps other rows.
28364 LAST_OVERLAPPING_ROW is the last such row. */
28365
28366 static void
28367 expose_overlaps (struct window *w,
28368 struct glyph_row *first_overlapping_row,
28369 struct glyph_row *last_overlapping_row,
28370 XRectangle *r)
28371 {
28372 struct glyph_row *row;
28373
28374 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28375 if (row->overlapping_p)
28376 {
28377 eassert (row->enabled_p && !row->mode_line_p);
28378
28379 row->clip = r;
28380 if (row->used[LEFT_MARGIN_AREA])
28381 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28382
28383 if (row->used[TEXT_AREA])
28384 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28385
28386 if (row->used[RIGHT_MARGIN_AREA])
28387 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28388 row->clip = NULL;
28389 }
28390 }
28391
28392
28393 /* Return non-zero if W's cursor intersects rectangle R. */
28394
28395 static int
28396 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28397 {
28398 XRectangle cr, result;
28399 struct glyph *cursor_glyph;
28400 struct glyph_row *row;
28401
28402 if (w->phys_cursor.vpos >= 0
28403 && w->phys_cursor.vpos < w->current_matrix->nrows
28404 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28405 row->enabled_p)
28406 && row->cursor_in_fringe_p)
28407 {
28408 /* Cursor is in the fringe. */
28409 cr.x = window_box_right_offset (w,
28410 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28411 ? RIGHT_MARGIN_AREA
28412 : TEXT_AREA));
28413 cr.y = row->y;
28414 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28415 cr.height = row->height;
28416 return x_intersect_rectangles (&cr, r, &result);
28417 }
28418
28419 cursor_glyph = get_phys_cursor_glyph (w);
28420 if (cursor_glyph)
28421 {
28422 /* r is relative to W's box, but w->phys_cursor.x is relative
28423 to left edge of W's TEXT area. Adjust it. */
28424 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28425 cr.y = w->phys_cursor.y;
28426 cr.width = cursor_glyph->pixel_width;
28427 cr.height = w->phys_cursor_height;
28428 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28429 I assume the effect is the same -- and this is portable. */
28430 return x_intersect_rectangles (&cr, r, &result);
28431 }
28432 /* If we don't understand the format, pretend we're not in the hot-spot. */
28433 return 0;
28434 }
28435
28436
28437 /* EXPORT:
28438 Draw a vertical window border to the right of window W if W doesn't
28439 have vertical scroll bars. */
28440
28441 void
28442 x_draw_vertical_border (struct window *w)
28443 {
28444 struct frame *f = XFRAME (WINDOW_FRAME (w));
28445
28446 /* We could do better, if we knew what type of scroll-bar the adjacent
28447 windows (on either side) have... But we don't :-(
28448 However, I think this works ok. ++KFS 2003-04-25 */
28449
28450 /* Redraw borders between horizontally adjacent windows. Don't
28451 do it for frames with vertical scroll bars because either the
28452 right scroll bar of a window, or the left scroll bar of its
28453 neighbor will suffice as a border. */
28454 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28455 return;
28456
28457 if (!WINDOW_RIGHTMOST_P (w)
28458 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28459 {
28460 int x0, x1, y0, y1;
28461
28462 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28463 y1 -= 1;
28464
28465 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28466 x1 -= 1;
28467
28468 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28469 }
28470 else if (!WINDOW_LEFTMOST_P (w)
28471 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28472 {
28473 int x0, x1, y0, y1;
28474
28475 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28476 y1 -= 1;
28477
28478 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28479 x0 -= 1;
28480
28481 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28482 }
28483 }
28484
28485
28486 /* Redraw the part of window W intersection rectangle FR. Pixel
28487 coordinates in FR are frame-relative. Call this function with
28488 input blocked. Value is non-zero if the exposure overwrites
28489 mouse-face. */
28490
28491 static int
28492 expose_window (struct window *w, XRectangle *fr)
28493 {
28494 struct frame *f = XFRAME (w->frame);
28495 XRectangle wr, r;
28496 int mouse_face_overwritten_p = 0;
28497
28498 /* If window is not yet fully initialized, do nothing. This can
28499 happen when toolkit scroll bars are used and a window is split.
28500 Reconfiguring the scroll bar will generate an expose for a newly
28501 created window. */
28502 if (w->current_matrix == NULL)
28503 return 0;
28504
28505 /* When we're currently updating the window, display and current
28506 matrix usually don't agree. Arrange for a thorough display
28507 later. */
28508 if (w == updated_window)
28509 {
28510 SET_FRAME_GARBAGED (f);
28511 return 0;
28512 }
28513
28514 /* Frame-relative pixel rectangle of W. */
28515 wr.x = WINDOW_LEFT_EDGE_X (w);
28516 wr.y = WINDOW_TOP_EDGE_Y (w);
28517 wr.width = WINDOW_TOTAL_WIDTH (w);
28518 wr.height = WINDOW_TOTAL_HEIGHT (w);
28519
28520 if (x_intersect_rectangles (fr, &wr, &r))
28521 {
28522 int yb = window_text_bottom_y (w);
28523 struct glyph_row *row;
28524 int cursor_cleared_p, phys_cursor_on_p;
28525 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28526
28527 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28528 r.x, r.y, r.width, r.height));
28529
28530 /* Convert to window coordinates. */
28531 r.x -= WINDOW_LEFT_EDGE_X (w);
28532 r.y -= WINDOW_TOP_EDGE_Y (w);
28533
28534 /* Turn off the cursor. */
28535 if (!w->pseudo_window_p
28536 && phys_cursor_in_rect_p (w, &r))
28537 {
28538 x_clear_cursor (w);
28539 cursor_cleared_p = 1;
28540 }
28541 else
28542 cursor_cleared_p = 0;
28543
28544 /* If the row containing the cursor extends face to end of line,
28545 then expose_area might overwrite the cursor outside the
28546 rectangle and thus notice_overwritten_cursor might clear
28547 w->phys_cursor_on_p. We remember the original value and
28548 check later if it is changed. */
28549 phys_cursor_on_p = w->phys_cursor_on_p;
28550
28551 /* Update lines intersecting rectangle R. */
28552 first_overlapping_row = last_overlapping_row = NULL;
28553 for (row = w->current_matrix->rows;
28554 row->enabled_p;
28555 ++row)
28556 {
28557 int y0 = row->y;
28558 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28559
28560 if ((y0 >= r.y && y0 < r.y + r.height)
28561 || (y1 > r.y && y1 < r.y + r.height)
28562 || (r.y >= y0 && r.y < y1)
28563 || (r.y + r.height > y0 && r.y + r.height < y1))
28564 {
28565 /* A header line may be overlapping, but there is no need
28566 to fix overlapping areas for them. KFS 2005-02-12 */
28567 if (row->overlapping_p && !row->mode_line_p)
28568 {
28569 if (first_overlapping_row == NULL)
28570 first_overlapping_row = row;
28571 last_overlapping_row = row;
28572 }
28573
28574 row->clip = fr;
28575 if (expose_line (w, row, &r))
28576 mouse_face_overwritten_p = 1;
28577 row->clip = NULL;
28578 }
28579 else if (row->overlapping_p)
28580 {
28581 /* We must redraw a row overlapping the exposed area. */
28582 if (y0 < r.y
28583 ? y0 + row->phys_height > r.y
28584 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28585 {
28586 if (first_overlapping_row == NULL)
28587 first_overlapping_row = row;
28588 last_overlapping_row = row;
28589 }
28590 }
28591
28592 if (y1 >= yb)
28593 break;
28594 }
28595
28596 /* Display the mode line if there is one. */
28597 if (WINDOW_WANTS_MODELINE_P (w)
28598 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28599 row->enabled_p)
28600 && row->y < r.y + r.height)
28601 {
28602 if (expose_line (w, row, &r))
28603 mouse_face_overwritten_p = 1;
28604 }
28605
28606 if (!w->pseudo_window_p)
28607 {
28608 /* Fix the display of overlapping rows. */
28609 if (first_overlapping_row)
28610 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28611 fr);
28612
28613 /* Draw border between windows. */
28614 x_draw_vertical_border (w);
28615
28616 /* Turn the cursor on again. */
28617 if (cursor_cleared_p
28618 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28619 update_window_cursor (w, 1);
28620 }
28621 }
28622
28623 return mouse_face_overwritten_p;
28624 }
28625
28626
28627
28628 /* Redraw (parts) of all windows in the window tree rooted at W that
28629 intersect R. R contains frame pixel coordinates. Value is
28630 non-zero if the exposure overwrites mouse-face. */
28631
28632 static int
28633 expose_window_tree (struct window *w, XRectangle *r)
28634 {
28635 struct frame *f = XFRAME (w->frame);
28636 int mouse_face_overwritten_p = 0;
28637
28638 while (w && !FRAME_GARBAGED_P (f))
28639 {
28640 if (!NILP (w->hchild))
28641 mouse_face_overwritten_p
28642 |= expose_window_tree (XWINDOW (w->hchild), r);
28643 else if (!NILP (w->vchild))
28644 mouse_face_overwritten_p
28645 |= expose_window_tree (XWINDOW (w->vchild), r);
28646 else
28647 mouse_face_overwritten_p |= expose_window (w, r);
28648
28649 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28650 }
28651
28652 return mouse_face_overwritten_p;
28653 }
28654
28655
28656 /* EXPORT:
28657 Redisplay an exposed area of frame F. X and Y are the upper-left
28658 corner of the exposed rectangle. W and H are width and height of
28659 the exposed area. All are pixel values. W or H zero means redraw
28660 the entire frame. */
28661
28662 void
28663 expose_frame (struct frame *f, int x, int y, int w, int h)
28664 {
28665 XRectangle r;
28666 int mouse_face_overwritten_p = 0;
28667
28668 TRACE ((stderr, "expose_frame "));
28669
28670 /* No need to redraw if frame will be redrawn soon. */
28671 if (FRAME_GARBAGED_P (f))
28672 {
28673 TRACE ((stderr, " garbaged\n"));
28674 return;
28675 }
28676
28677 /* If basic faces haven't been realized yet, there is no point in
28678 trying to redraw anything. This can happen when we get an expose
28679 event while Emacs is starting, e.g. by moving another window. */
28680 if (FRAME_FACE_CACHE (f) == NULL
28681 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28682 {
28683 TRACE ((stderr, " no faces\n"));
28684 return;
28685 }
28686
28687 if (w == 0 || h == 0)
28688 {
28689 r.x = r.y = 0;
28690 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28691 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28692 }
28693 else
28694 {
28695 r.x = x;
28696 r.y = y;
28697 r.width = w;
28698 r.height = h;
28699 }
28700
28701 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28702 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28703
28704 if (WINDOWP (f->tool_bar_window))
28705 mouse_face_overwritten_p
28706 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28707
28708 #ifdef HAVE_X_WINDOWS
28709 #ifndef MSDOS
28710 #ifndef USE_X_TOOLKIT
28711 if (WINDOWP (f->menu_bar_window))
28712 mouse_face_overwritten_p
28713 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28714 #endif /* not USE_X_TOOLKIT */
28715 #endif
28716 #endif
28717
28718 /* Some window managers support a focus-follows-mouse style with
28719 delayed raising of frames. Imagine a partially obscured frame,
28720 and moving the mouse into partially obscured mouse-face on that
28721 frame. The visible part of the mouse-face will be highlighted,
28722 then the WM raises the obscured frame. With at least one WM, KDE
28723 2.1, Emacs is not getting any event for the raising of the frame
28724 (even tried with SubstructureRedirectMask), only Expose events.
28725 These expose events will draw text normally, i.e. not
28726 highlighted. Which means we must redo the highlight here.
28727 Subsume it under ``we love X''. --gerd 2001-08-15 */
28728 /* Included in Windows version because Windows most likely does not
28729 do the right thing if any third party tool offers
28730 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28731 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28732 {
28733 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28734 if (f == hlinfo->mouse_face_mouse_frame)
28735 {
28736 int mouse_x = hlinfo->mouse_face_mouse_x;
28737 int mouse_y = hlinfo->mouse_face_mouse_y;
28738 clear_mouse_face (hlinfo);
28739 note_mouse_highlight (f, mouse_x, mouse_y);
28740 }
28741 }
28742 }
28743
28744
28745 /* EXPORT:
28746 Determine the intersection of two rectangles R1 and R2. Return
28747 the intersection in *RESULT. Value is non-zero if RESULT is not
28748 empty. */
28749
28750 int
28751 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28752 {
28753 XRectangle *left, *right;
28754 XRectangle *upper, *lower;
28755 int intersection_p = 0;
28756
28757 /* Rearrange so that R1 is the left-most rectangle. */
28758 if (r1->x < r2->x)
28759 left = r1, right = r2;
28760 else
28761 left = r2, right = r1;
28762
28763 /* X0 of the intersection is right.x0, if this is inside R1,
28764 otherwise there is no intersection. */
28765 if (right->x <= left->x + left->width)
28766 {
28767 result->x = right->x;
28768
28769 /* The right end of the intersection is the minimum of
28770 the right ends of left and right. */
28771 result->width = (min (left->x + left->width, right->x + right->width)
28772 - result->x);
28773
28774 /* Same game for Y. */
28775 if (r1->y < r2->y)
28776 upper = r1, lower = r2;
28777 else
28778 upper = r2, lower = r1;
28779
28780 /* The upper end of the intersection is lower.y0, if this is inside
28781 of upper. Otherwise, there is no intersection. */
28782 if (lower->y <= upper->y + upper->height)
28783 {
28784 result->y = lower->y;
28785
28786 /* The lower end of the intersection is the minimum of the lower
28787 ends of upper and lower. */
28788 result->height = (min (lower->y + lower->height,
28789 upper->y + upper->height)
28790 - result->y);
28791 intersection_p = 1;
28792 }
28793 }
28794
28795 return intersection_p;
28796 }
28797
28798 #endif /* HAVE_WINDOW_SYSTEM */
28799
28800 \f
28801 /***********************************************************************
28802 Initialization
28803 ***********************************************************************/
28804
28805 void
28806 syms_of_xdisp (void)
28807 {
28808 Vwith_echo_area_save_vector = Qnil;
28809 staticpro (&Vwith_echo_area_save_vector);
28810
28811 Vmessage_stack = Qnil;
28812 staticpro (&Vmessage_stack);
28813
28814 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28815 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
28816
28817 message_dolog_marker1 = Fmake_marker ();
28818 staticpro (&message_dolog_marker1);
28819 message_dolog_marker2 = Fmake_marker ();
28820 staticpro (&message_dolog_marker2);
28821 message_dolog_marker3 = Fmake_marker ();
28822 staticpro (&message_dolog_marker3);
28823
28824 #ifdef GLYPH_DEBUG
28825 defsubr (&Sdump_frame_glyph_matrix);
28826 defsubr (&Sdump_glyph_matrix);
28827 defsubr (&Sdump_glyph_row);
28828 defsubr (&Sdump_tool_bar_row);
28829 defsubr (&Strace_redisplay);
28830 defsubr (&Strace_to_stderr);
28831 #endif
28832 #ifdef HAVE_WINDOW_SYSTEM
28833 defsubr (&Stool_bar_lines_needed);
28834 defsubr (&Slookup_image_map);
28835 #endif
28836 defsubr (&Sformat_mode_line);
28837 defsubr (&Sinvisible_p);
28838 defsubr (&Scurrent_bidi_paragraph_direction);
28839
28840 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
28841 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
28842 DEFSYM (Qoverriding_local_map, "overriding-local-map");
28843 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
28844 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
28845 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
28846 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
28847 DEFSYM (Qeval, "eval");
28848 DEFSYM (QCdata, ":data");
28849 DEFSYM (Qdisplay, "display");
28850 DEFSYM (Qspace_width, "space-width");
28851 DEFSYM (Qraise, "raise");
28852 DEFSYM (Qslice, "slice");
28853 DEFSYM (Qspace, "space");
28854 DEFSYM (Qmargin, "margin");
28855 DEFSYM (Qpointer, "pointer");
28856 DEFSYM (Qleft_margin, "left-margin");
28857 DEFSYM (Qright_margin, "right-margin");
28858 DEFSYM (Qcenter, "center");
28859 DEFSYM (Qline_height, "line-height");
28860 DEFSYM (QCalign_to, ":align-to");
28861 DEFSYM (QCrelative_width, ":relative-width");
28862 DEFSYM (QCrelative_height, ":relative-height");
28863 DEFSYM (QCeval, ":eval");
28864 DEFSYM (QCpropertize, ":propertize");
28865 DEFSYM (QCfile, ":file");
28866 DEFSYM (Qfontified, "fontified");
28867 DEFSYM (Qfontification_functions, "fontification-functions");
28868 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
28869 DEFSYM (Qescape_glyph, "escape-glyph");
28870 DEFSYM (Qnobreak_space, "nobreak-space");
28871 DEFSYM (Qimage, "image");
28872 DEFSYM (Qtext, "text");
28873 DEFSYM (Qboth, "both");
28874 DEFSYM (Qboth_horiz, "both-horiz");
28875 DEFSYM (Qtext_image_horiz, "text-image-horiz");
28876 DEFSYM (QCmap, ":map");
28877 DEFSYM (QCpointer, ":pointer");
28878 DEFSYM (Qrect, "rect");
28879 DEFSYM (Qcircle, "circle");
28880 DEFSYM (Qpoly, "poly");
28881 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
28882 DEFSYM (Qgrow_only, "grow-only");
28883 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
28884 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
28885 DEFSYM (Qposition, "position");
28886 DEFSYM (Qbuffer_position, "buffer-position");
28887 DEFSYM (Qobject, "object");
28888 DEFSYM (Qbar, "bar");
28889 DEFSYM (Qhbar, "hbar");
28890 DEFSYM (Qbox, "box");
28891 DEFSYM (Qhollow, "hollow");
28892 DEFSYM (Qhand, "hand");
28893 DEFSYM (Qarrow, "arrow");
28894 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
28895
28896 list_of_error = Fcons (Fcons (intern_c_string ("error"),
28897 Fcons (intern_c_string ("void-variable"), Qnil)),
28898 Qnil);
28899 staticpro (&list_of_error);
28900
28901 DEFSYM (Qlast_arrow_position, "last-arrow-position");
28902 DEFSYM (Qlast_arrow_string, "last-arrow-string");
28903 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
28904 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
28905
28906 echo_buffer[0] = echo_buffer[1] = Qnil;
28907 staticpro (&echo_buffer[0]);
28908 staticpro (&echo_buffer[1]);
28909
28910 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
28911 staticpro (&echo_area_buffer[0]);
28912 staticpro (&echo_area_buffer[1]);
28913
28914 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
28915 staticpro (&Vmessages_buffer_name);
28916
28917 mode_line_proptrans_alist = Qnil;
28918 staticpro (&mode_line_proptrans_alist);
28919 mode_line_string_list = Qnil;
28920 staticpro (&mode_line_string_list);
28921 mode_line_string_face = Qnil;
28922 staticpro (&mode_line_string_face);
28923 mode_line_string_face_prop = Qnil;
28924 staticpro (&mode_line_string_face_prop);
28925 Vmode_line_unwind_vector = Qnil;
28926 staticpro (&Vmode_line_unwind_vector);
28927
28928 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
28929
28930 help_echo_string = Qnil;
28931 staticpro (&help_echo_string);
28932 help_echo_object = Qnil;
28933 staticpro (&help_echo_object);
28934 help_echo_window = Qnil;
28935 staticpro (&help_echo_window);
28936 previous_help_echo_string = Qnil;
28937 staticpro (&previous_help_echo_string);
28938 help_echo_pos = -1;
28939
28940 DEFSYM (Qright_to_left, "right-to-left");
28941 DEFSYM (Qleft_to_right, "left-to-right");
28942
28943 #ifdef HAVE_WINDOW_SYSTEM
28944 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
28945 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
28946 For example, if a block cursor is over a tab, it will be drawn as
28947 wide as that tab on the display. */);
28948 x_stretch_cursor_p = 0;
28949 #endif
28950
28951 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
28952 doc: /* Non-nil means highlight trailing whitespace.
28953 The face used for trailing whitespace is `trailing-whitespace'. */);
28954 Vshow_trailing_whitespace = Qnil;
28955
28956 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
28957 doc: /* Control highlighting of non-ASCII space and hyphen chars.
28958 If the value is t, Emacs highlights non-ASCII chars which have the
28959 same appearance as an ASCII space or hyphen, using the `nobreak-space'
28960 or `escape-glyph' face respectively.
28961
28962 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
28963 U+2011 (non-breaking hyphen) are affected.
28964
28965 Any other non-nil value means to display these characters as a escape
28966 glyph followed by an ordinary space or hyphen.
28967
28968 A value of nil means no special handling of these characters. */);
28969 Vnobreak_char_display = Qt;
28970
28971 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
28972 doc: /* The pointer shape to show in void text areas.
28973 A value of nil means to show the text pointer. Other options are `arrow',
28974 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
28975 Vvoid_text_area_pointer = Qarrow;
28976
28977 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
28978 doc: /* Non-nil means don't actually do any redisplay.
28979 This is used for internal purposes. */);
28980 Vinhibit_redisplay = Qnil;
28981
28982 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
28983 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
28984 Vglobal_mode_string = Qnil;
28985
28986 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
28987 doc: /* Marker for where to display an arrow on top of the buffer text.
28988 This must be the beginning of a line in order to work.
28989 See also `overlay-arrow-string'. */);
28990 Voverlay_arrow_position = Qnil;
28991
28992 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
28993 doc: /* String to display as an arrow in non-window frames.
28994 See also `overlay-arrow-position'. */);
28995 Voverlay_arrow_string = build_pure_c_string ("=>");
28996
28997 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
28998 doc: /* List of variables (symbols) which hold markers for overlay arrows.
28999 The symbols on this list are examined during redisplay to determine
29000 where to display overlay arrows. */);
29001 Voverlay_arrow_variable_list
29002 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
29003
29004 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29005 doc: /* The number of lines to try scrolling a window by when point moves out.
29006 If that fails to bring point back on frame, point is centered instead.
29007 If this is zero, point is always centered after it moves off frame.
29008 If you want scrolling to always be a line at a time, you should set
29009 `scroll-conservatively' to a large value rather than set this to 1. */);
29010
29011 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29012 doc: /* Scroll up to this many lines, to bring point back on screen.
29013 If point moves off-screen, redisplay will scroll by up to
29014 `scroll-conservatively' lines in order to bring point just barely
29015 onto the screen again. If that cannot be done, then redisplay
29016 recenters point as usual.
29017
29018 If the value is greater than 100, redisplay will never recenter point,
29019 but will always scroll just enough text to bring point into view, even
29020 if you move far away.
29021
29022 A value of zero means always recenter point if it moves off screen. */);
29023 scroll_conservatively = 0;
29024
29025 DEFVAR_INT ("scroll-margin", scroll_margin,
29026 doc: /* Number of lines of margin at the top and bottom of a window.
29027 Recenter the window whenever point gets within this many lines
29028 of the top or bottom of the window. */);
29029 scroll_margin = 0;
29030
29031 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29032 doc: /* Pixels per inch value for non-window system displays.
29033 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29034 Vdisplay_pixels_per_inch = make_float (72.0);
29035
29036 #ifdef GLYPH_DEBUG
29037 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29038 #endif
29039
29040 DEFVAR_LISP ("truncate-partial-width-windows",
29041 Vtruncate_partial_width_windows,
29042 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29043 For an integer value, truncate lines in each window narrower than the
29044 full frame width, provided the window width is less than that integer;
29045 otherwise, respect the value of `truncate-lines'.
29046
29047 For any other non-nil value, truncate lines in all windows that do
29048 not span the full frame width.
29049
29050 A value of nil means to respect the value of `truncate-lines'.
29051
29052 If `word-wrap' is enabled, you might want to reduce this. */);
29053 Vtruncate_partial_width_windows = make_number (50);
29054
29055 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29056 doc: /* Maximum buffer size for which line number should be displayed.
29057 If the buffer is bigger than this, the line number does not appear
29058 in the mode line. A value of nil means no limit. */);
29059 Vline_number_display_limit = Qnil;
29060
29061 DEFVAR_INT ("line-number-display-limit-width",
29062 line_number_display_limit_width,
29063 doc: /* Maximum line width (in characters) for line number display.
29064 If the average length of the lines near point is bigger than this, then the
29065 line number may be omitted from the mode line. */);
29066 line_number_display_limit_width = 200;
29067
29068 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29069 doc: /* Non-nil means highlight region even in nonselected windows. */);
29070 highlight_nonselected_windows = 0;
29071
29072 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29073 doc: /* Non-nil if more than one frame is visible on this display.
29074 Minibuffer-only frames don't count, but iconified frames do.
29075 This variable is not guaranteed to be accurate except while processing
29076 `frame-title-format' and `icon-title-format'. */);
29077
29078 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29079 doc: /* Template for displaying the title bar of visible frames.
29080 \(Assuming the window manager supports this feature.)
29081
29082 This variable has the same structure as `mode-line-format', except that
29083 the %c and %l constructs are ignored. It is used only on frames for
29084 which no explicit name has been set \(see `modify-frame-parameters'). */);
29085
29086 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29087 doc: /* Template for displaying the title bar of an iconified frame.
29088 \(Assuming the window manager supports this feature.)
29089 This variable has the same structure as `mode-line-format' (which see),
29090 and is used only on frames for which no explicit name has been set
29091 \(see `modify-frame-parameters'). */);
29092 Vicon_title_format
29093 = Vframe_title_format
29094 = listn (CONSTYPE_PURE, 3,
29095 intern_c_string ("multiple-frames"),
29096 build_pure_c_string ("%b"),
29097 listn (CONSTYPE_PURE, 4,
29098 empty_unibyte_string,
29099 intern_c_string ("invocation-name"),
29100 build_pure_c_string ("@"),
29101 intern_c_string ("system-name")));
29102
29103 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29104 doc: /* Maximum number of lines to keep in the message log buffer.
29105 If nil, disable message logging. If t, log messages but don't truncate
29106 the buffer when it becomes large. */);
29107 Vmessage_log_max = make_number (1000);
29108
29109 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29110 doc: /* Functions called before redisplay, if window sizes have changed.
29111 The value should be a list of functions that take one argument.
29112 Just before redisplay, for each frame, if any of its windows have changed
29113 size since the last redisplay, or have been split or deleted,
29114 all the functions in the list are called, with the frame as argument. */);
29115 Vwindow_size_change_functions = Qnil;
29116
29117 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29118 doc: /* List of functions to call before redisplaying a window with scrolling.
29119 Each function is called with two arguments, the window and its new
29120 display-start position. Note that these functions are also called by
29121 `set-window-buffer'. Also note that the value of `window-end' is not
29122 valid when these functions are called.
29123
29124 Warning: Do not use this feature to alter the way the window
29125 is scrolled. It is not designed for that, and such use probably won't
29126 work. */);
29127 Vwindow_scroll_functions = Qnil;
29128
29129 DEFVAR_LISP ("window-text-change-functions",
29130 Vwindow_text_change_functions,
29131 doc: /* Functions to call in redisplay when text in the window might change. */);
29132 Vwindow_text_change_functions = Qnil;
29133
29134 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29135 doc: /* Functions called when redisplay of a window reaches the end trigger.
29136 Each function is called with two arguments, the window and the end trigger value.
29137 See `set-window-redisplay-end-trigger'. */);
29138 Vredisplay_end_trigger_functions = Qnil;
29139
29140 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29141 doc: /* Non-nil means autoselect window with mouse pointer.
29142 If nil, do not autoselect windows.
29143 A positive number means delay autoselection by that many seconds: a
29144 window is autoselected only after the mouse has remained in that
29145 window for the duration of the delay.
29146 A negative number has a similar effect, but causes windows to be
29147 autoselected only after the mouse has stopped moving. \(Because of
29148 the way Emacs compares mouse events, you will occasionally wait twice
29149 that time before the window gets selected.\)
29150 Any other value means to autoselect window instantaneously when the
29151 mouse pointer enters it.
29152
29153 Autoselection selects the minibuffer only if it is active, and never
29154 unselects the minibuffer if it is active.
29155
29156 When customizing this variable make sure that the actual value of
29157 `focus-follows-mouse' matches the behavior of your window manager. */);
29158 Vmouse_autoselect_window = Qnil;
29159
29160 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29161 doc: /* Non-nil means automatically resize tool-bars.
29162 This dynamically changes the tool-bar's height to the minimum height
29163 that is needed to make all tool-bar items visible.
29164 If value is `grow-only', the tool-bar's height is only increased
29165 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29166 Vauto_resize_tool_bars = Qt;
29167
29168 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29169 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29170 auto_raise_tool_bar_buttons_p = 1;
29171
29172 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29173 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29174 make_cursor_line_fully_visible_p = 1;
29175
29176 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29177 doc: /* Border below tool-bar in pixels.
29178 If an integer, use it as the height of the border.
29179 If it is one of `internal-border-width' or `border-width', use the
29180 value of the corresponding frame parameter.
29181 Otherwise, no border is added below the tool-bar. */);
29182 Vtool_bar_border = Qinternal_border_width;
29183
29184 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29185 doc: /* Margin around tool-bar buttons in pixels.
29186 If an integer, use that for both horizontal and vertical margins.
29187 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29188 HORZ specifying the horizontal margin, and VERT specifying the
29189 vertical margin. */);
29190 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29191
29192 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29193 doc: /* Relief thickness of tool-bar buttons. */);
29194 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29195
29196 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29197 doc: /* Tool bar style to use.
29198 It can be one of
29199 image - show images only
29200 text - show text only
29201 both - show both, text below image
29202 both-horiz - show text to the right of the image
29203 text-image-horiz - show text to the left of the image
29204 any other - use system default or image if no system default.
29205
29206 This variable only affects the GTK+ toolkit version of Emacs. */);
29207 Vtool_bar_style = Qnil;
29208
29209 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29210 doc: /* Maximum number of characters a label can have to be shown.
29211 The tool bar style must also show labels for this to have any effect, see
29212 `tool-bar-style'. */);
29213 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29214
29215 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29216 doc: /* List of functions to call to fontify regions of text.
29217 Each function is called with one argument POS. Functions must
29218 fontify a region starting at POS in the current buffer, and give
29219 fontified regions the property `fontified'. */);
29220 Vfontification_functions = Qnil;
29221 Fmake_variable_buffer_local (Qfontification_functions);
29222
29223 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29224 unibyte_display_via_language_environment,
29225 doc: /* Non-nil means display unibyte text according to language environment.
29226 Specifically, this means that raw bytes in the range 160-255 decimal
29227 are displayed by converting them to the equivalent multibyte characters
29228 according to the current language environment. As a result, they are
29229 displayed according to the current fontset.
29230
29231 Note that this variable affects only how these bytes are displayed,
29232 but does not change the fact they are interpreted as raw bytes. */);
29233 unibyte_display_via_language_environment = 0;
29234
29235 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29236 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29237 If a float, it specifies a fraction of the mini-window frame's height.
29238 If an integer, it specifies a number of lines. */);
29239 Vmax_mini_window_height = make_float (0.25);
29240
29241 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29242 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29243 A value of nil means don't automatically resize mini-windows.
29244 A value of t means resize them to fit the text displayed in them.
29245 A value of `grow-only', the default, means let mini-windows grow only;
29246 they return to their normal size when the minibuffer is closed, or the
29247 echo area becomes empty. */);
29248 Vresize_mini_windows = Qgrow_only;
29249
29250 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29251 doc: /* Alist specifying how to blink the cursor off.
29252 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29253 `cursor-type' frame-parameter or variable equals ON-STATE,
29254 comparing using `equal', Emacs uses OFF-STATE to specify
29255 how to blink it off. ON-STATE and OFF-STATE are values for
29256 the `cursor-type' frame parameter.
29257
29258 If a frame's ON-STATE has no entry in this list,
29259 the frame's other specifications determine how to blink the cursor off. */);
29260 Vblink_cursor_alist = Qnil;
29261
29262 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29263 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29264 If non-nil, windows are automatically scrolled horizontally to make
29265 point visible. */);
29266 automatic_hscrolling_p = 1;
29267 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29268
29269 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29270 doc: /* How many columns away from the window edge point is allowed to get
29271 before automatic hscrolling will horizontally scroll the window. */);
29272 hscroll_margin = 5;
29273
29274 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29275 doc: /* How many columns to scroll the window when point gets too close to the edge.
29276 When point is less than `hscroll-margin' columns from the window
29277 edge, automatic hscrolling will scroll the window by the amount of columns
29278 determined by this variable. If its value is a positive integer, scroll that
29279 many columns. If it's a positive floating-point number, it specifies the
29280 fraction of the window's width to scroll. If it's nil or zero, point will be
29281 centered horizontally after the scroll. Any other value, including negative
29282 numbers, are treated as if the value were zero.
29283
29284 Automatic hscrolling always moves point outside the scroll margin, so if
29285 point was more than scroll step columns inside the margin, the window will
29286 scroll more than the value given by the scroll step.
29287
29288 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29289 and `scroll-right' overrides this variable's effect. */);
29290 Vhscroll_step = make_number (0);
29291
29292 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29293 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29294 Bind this around calls to `message' to let it take effect. */);
29295 message_truncate_lines = 0;
29296
29297 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29298 doc: /* Normal hook run to update the menu bar definitions.
29299 Redisplay runs this hook before it redisplays the menu bar.
29300 This is used to update submenus such as Buffers,
29301 whose contents depend on various data. */);
29302 Vmenu_bar_update_hook = Qnil;
29303
29304 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29305 doc: /* Frame for which we are updating a menu.
29306 The enable predicate for a menu binding should check this variable. */);
29307 Vmenu_updating_frame = Qnil;
29308
29309 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29310 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29311 inhibit_menubar_update = 0;
29312
29313 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29314 doc: /* Prefix prepended to all continuation lines at display time.
29315 The value may be a string, an image, or a stretch-glyph; it is
29316 interpreted in the same way as the value of a `display' text property.
29317
29318 This variable is overridden by any `wrap-prefix' text or overlay
29319 property.
29320
29321 To add a prefix to non-continuation lines, use `line-prefix'. */);
29322 Vwrap_prefix = Qnil;
29323 DEFSYM (Qwrap_prefix, "wrap-prefix");
29324 Fmake_variable_buffer_local (Qwrap_prefix);
29325
29326 DEFVAR_LISP ("line-prefix", Vline_prefix,
29327 doc: /* Prefix prepended to all non-continuation lines at display time.
29328 The value may be a string, an image, or a stretch-glyph; it is
29329 interpreted in the same way as the value of a `display' text property.
29330
29331 This variable is overridden by any `line-prefix' text or overlay
29332 property.
29333
29334 To add a prefix to continuation lines, use `wrap-prefix'. */);
29335 Vline_prefix = Qnil;
29336 DEFSYM (Qline_prefix, "line-prefix");
29337 Fmake_variable_buffer_local (Qline_prefix);
29338
29339 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29340 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29341 inhibit_eval_during_redisplay = 0;
29342
29343 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29344 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29345 inhibit_free_realized_faces = 0;
29346
29347 #ifdef GLYPH_DEBUG
29348 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29349 doc: /* Inhibit try_window_id display optimization. */);
29350 inhibit_try_window_id = 0;
29351
29352 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29353 doc: /* Inhibit try_window_reusing display optimization. */);
29354 inhibit_try_window_reusing = 0;
29355
29356 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29357 doc: /* Inhibit try_cursor_movement display optimization. */);
29358 inhibit_try_cursor_movement = 0;
29359 #endif /* GLYPH_DEBUG */
29360
29361 DEFVAR_INT ("overline-margin", overline_margin,
29362 doc: /* Space between overline and text, in pixels.
29363 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29364 margin to the character height. */);
29365 overline_margin = 2;
29366
29367 DEFVAR_INT ("underline-minimum-offset",
29368 underline_minimum_offset,
29369 doc: /* Minimum distance between baseline and underline.
29370 This can improve legibility of underlined text at small font sizes,
29371 particularly when using variable `x-use-underline-position-properties'
29372 with fonts that specify an UNDERLINE_POSITION relatively close to the
29373 baseline. The default value is 1. */);
29374 underline_minimum_offset = 1;
29375
29376 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29377 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29378 This feature only works when on a window system that can change
29379 cursor shapes. */);
29380 display_hourglass_p = 1;
29381
29382 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29383 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29384 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29385
29386 hourglass_atimer = NULL;
29387 hourglass_shown_p = 0;
29388
29389 DEFSYM (Qglyphless_char, "glyphless-char");
29390 DEFSYM (Qhex_code, "hex-code");
29391 DEFSYM (Qempty_box, "empty-box");
29392 DEFSYM (Qthin_space, "thin-space");
29393 DEFSYM (Qzero_width, "zero-width");
29394
29395 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29396 /* Intern this now in case it isn't already done.
29397 Setting this variable twice is harmless.
29398 But don't staticpro it here--that is done in alloc.c. */
29399 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29400 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29401
29402 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29403 doc: /* Char-table defining glyphless characters.
29404 Each element, if non-nil, should be one of the following:
29405 an ASCII acronym string: display this string in a box
29406 `hex-code': display the hexadecimal code of a character in a box
29407 `empty-box': display as an empty box
29408 `thin-space': display as 1-pixel width space
29409 `zero-width': don't display
29410 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29411 display method for graphical terminals and text terminals respectively.
29412 GRAPHICAL and TEXT should each have one of the values listed above.
29413
29414 The char-table has one extra slot to control the display of a character for
29415 which no font is found. This slot only takes effect on graphical terminals.
29416 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29417 `thin-space'. The default is `empty-box'. */);
29418 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29419 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29420 Qempty_box);
29421
29422 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29423 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29424 Vdebug_on_message = Qnil;
29425 }
29426
29427
29428 /* Initialize this module when Emacs starts. */
29429
29430 void
29431 init_xdisp (void)
29432 {
29433 current_header_line_height = current_mode_line_height = -1;
29434
29435 CHARPOS (this_line_start_pos) = 0;
29436
29437 if (!noninteractive)
29438 {
29439 struct window *m = XWINDOW (minibuf_window);
29440 Lisp_Object frame = m->frame;
29441 struct frame *f = XFRAME (frame);
29442 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29443 struct window *r = XWINDOW (root);
29444 int i;
29445
29446 echo_area_window = minibuf_window;
29447
29448 wset_top_line (r, make_number (FRAME_TOP_MARGIN (f)));
29449 wset_total_lines
29450 (r, make_number (FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f)));
29451 wset_total_cols (r, make_number (FRAME_COLS (f)));
29452 wset_top_line (m, make_number (FRAME_LINES (f) - 1));
29453 wset_total_lines (m, make_number (1));
29454 wset_total_cols (m, make_number (FRAME_COLS (f)));
29455
29456 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29457 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29458 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29459
29460 /* The default ellipsis glyphs `...'. */
29461 for (i = 0; i < 3; ++i)
29462 default_invis_vector[i] = make_number ('.');
29463 }
29464
29465 {
29466 /* Allocate the buffer for frame titles.
29467 Also used for `format-mode-line'. */
29468 int size = 100;
29469 mode_line_noprop_buf = xmalloc (size);
29470 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29471 mode_line_noprop_ptr = mode_line_noprop_buf;
29472 mode_line_target = MODE_LINE_DISPLAY;
29473 }
29474
29475 help_echo_showing_p = 0;
29476 }
29477
29478 /* Platform-independent portion of hourglass implementation. */
29479
29480 /* Cancel a currently active hourglass timer, and start a new one. */
29481 void
29482 start_hourglass (void)
29483 {
29484 #if defined (HAVE_WINDOW_SYSTEM)
29485 EMACS_TIME delay;
29486
29487 cancel_hourglass ();
29488
29489 if (INTEGERP (Vhourglass_delay)
29490 && XINT (Vhourglass_delay) > 0)
29491 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29492 TYPE_MAXIMUM (time_t)),
29493 0);
29494 else if (FLOATP (Vhourglass_delay)
29495 && XFLOAT_DATA (Vhourglass_delay) > 0)
29496 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29497 else
29498 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29499
29500 #ifdef HAVE_NTGUI
29501 {
29502 extern void w32_note_current_window (void);
29503 w32_note_current_window ();
29504 }
29505 #endif /* HAVE_NTGUI */
29506
29507 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29508 show_hourglass, NULL);
29509 #endif
29510 }
29511
29512
29513 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29514 shown. */
29515 void
29516 cancel_hourglass (void)
29517 {
29518 #if defined (HAVE_WINDOW_SYSTEM)
29519 if (hourglass_atimer)
29520 {
29521 cancel_atimer (hourglass_atimer);
29522 hourglass_atimer = NULL;
29523 }
29524
29525 if (hourglass_shown_p)
29526 hide_hourglass ();
29527 #endif
29528 }