]> code.delx.au - gnu-emacs/blob - src/xdisp.c
upstream
[gnu-emacs] / 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 #ifdef HAVE_XWIDGETS
317 #include "xwidget.h"
318 #endif
319 #ifndef FRAME_X_OUTPUT
320 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
321 #endif
322
323 #define INFINITY 10000000
324
325 Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
326 Lisp_Object Qwindow_scroll_functions;
327 static Lisp_Object Qwindow_text_change_functions;
328 static Lisp_Object Qredisplay_end_trigger_functions;
329 Lisp_Object Qinhibit_point_motion_hooks;
330 static Lisp_Object QCeval, QCpropertize;
331 Lisp_Object QCfile, QCdata;
332 static Lisp_Object Qfontified;
333 static Lisp_Object Qgrow_only;
334 static Lisp_Object Qinhibit_eval_during_redisplay;
335 static Lisp_Object Qbuffer_position, Qposition, Qobject;
336 static Lisp_Object Qright_to_left, Qleft_to_right;
337
338 /* Cursor shapes. */
339 Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
340
341 /* Pointer shapes. */
342 static Lisp_Object Qarrow, Qhand;
343 Lisp_Object Qtext;
344
345 /* Holds the list (error). */
346 static Lisp_Object list_of_error;
347
348 static Lisp_Object Qfontification_functions;
349
350 static Lisp_Object Qwrap_prefix;
351 static Lisp_Object Qline_prefix;
352 static Lisp_Object Qredisplay_internal;
353
354 /* Non-nil means don't actually do any redisplay. */
355
356 Lisp_Object Qinhibit_redisplay;
357
358 /* Names of text properties relevant for redisplay. */
359
360 Lisp_Object Qdisplay;
361
362 Lisp_Object Qspace, QCalign_to;
363 static Lisp_Object QCrelative_width, QCrelative_height;
364 Lisp_Object Qleft_margin, Qright_margin;
365 static Lisp_Object Qspace_width, Qraise;
366 static Lisp_Object Qslice;
367 Lisp_Object Qcenter;
368 static Lisp_Object Qmargin, Qpointer;
369 static Lisp_Object Qline_height;
370
371 /* These setters are used only in this file, so they can be private. */
372 static void
373 wset_base_line_number (struct window *w, Lisp_Object val)
374 {
375 w->base_line_number = val;
376 }
377 static void
378 wset_base_line_pos (struct window *w, Lisp_Object val)
379 {
380 w->base_line_pos = val;
381 }
382 static void
383 wset_column_number_displayed (struct window *w, Lisp_Object val)
384 {
385 w->column_number_displayed = val;
386 }
387 static void
388 wset_region_showing (struct window *w, Lisp_Object val)
389 {
390 w->region_showing = val;
391 }
392
393 #ifdef HAVE_WINDOW_SYSTEM
394
395 /* Test if overflow newline into fringe. Called with iterator IT
396 at or past right window margin, and with IT->current_x set. */
397
398 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(IT) \
399 (!NILP (Voverflow_newline_into_fringe) \
400 && FRAME_WINDOW_P ((IT)->f) \
401 && ((IT)->bidi_it.paragraph_dir == R2L \
402 ? (WINDOW_LEFT_FRINGE_WIDTH ((IT)->w) > 0) \
403 : (WINDOW_RIGHT_FRINGE_WIDTH ((IT)->w) > 0)) \
404 && (IT)->current_x == (IT)->last_visible_x \
405 && (IT)->line_wrap != WORD_WRAP)
406
407 #else /* !HAVE_WINDOW_SYSTEM */
408 #define IT_OVERFLOW_NEWLINE_INTO_FRINGE(it) 0
409 #endif /* HAVE_WINDOW_SYSTEM */
410
411 /* Test if the display element loaded in IT, or the underlying buffer
412 or string character, is a space or a TAB character. This is used
413 to determine where word wrapping can occur. */
414
415 #define IT_DISPLAYING_WHITESPACE(it) \
416 ((it->what == IT_CHARACTER && (it->c == ' ' || it->c == '\t')) \
417 || ((STRINGP (it->string) \
418 && (SREF (it->string, IT_STRING_BYTEPOS (*it)) == ' ' \
419 || SREF (it->string, IT_STRING_BYTEPOS (*it)) == '\t')) \
420 || (it->s \
421 && (it->s[IT_BYTEPOS (*it)] == ' ' \
422 || it->s[IT_BYTEPOS (*it)] == '\t')) \
423 || (IT_BYTEPOS (*it) < ZV_BYTE \
424 && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' ' \
425 || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t')))) \
426
427 /* Name of the face used to highlight trailing whitespace. */
428
429 static Lisp_Object Qtrailing_whitespace;
430
431 /* Name and number of the face used to highlight escape glyphs. */
432
433 static Lisp_Object Qescape_glyph;
434
435 /* Name and number of the face used to highlight non-breaking spaces. */
436
437 static Lisp_Object Qnobreak_space;
438
439 /* The symbol `image' which is the car of the lists used to represent
440 images in Lisp. Also a tool bar style. */
441
442 Lisp_Object Qimage;
443
444 /* The image map types. */
445 Lisp_Object QCmap;
446 static Lisp_Object QCpointer;
447 static Lisp_Object Qrect, Qcircle, Qpoly;
448
449 /* Tool bar styles */
450 Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
451
452 /* Non-zero means print newline to stdout before next mini-buffer
453 message. */
454
455 int noninteractive_need_newline;
456
457 /* Non-zero means print newline to message log before next message. */
458
459 static int message_log_need_newline;
460
461 /* Three markers that message_dolog uses.
462 It could allocate them itself, but that causes trouble
463 in handling memory-full errors. */
464 static Lisp_Object message_dolog_marker1;
465 static Lisp_Object message_dolog_marker2;
466 static Lisp_Object message_dolog_marker3;
467 \f
468 /* The buffer position of the first character appearing entirely or
469 partially on the line of the selected window which contains the
470 cursor; <= 0 if not known. Set by set_cursor_from_row, used for
471 redisplay optimization in redisplay_internal. */
472
473 static struct text_pos this_line_start_pos;
474
475 /* Number of characters past the end of the line above, including the
476 terminating newline. */
477
478 static struct text_pos this_line_end_pos;
479
480 /* The vertical positions and the height of this line. */
481
482 static int this_line_vpos;
483 static int this_line_y;
484 static int this_line_pixel_height;
485
486 /* X position at which this display line starts. Usually zero;
487 negative if first character is partially visible. */
488
489 static int this_line_start_x;
490
491 /* The smallest character position seen by move_it_* functions as they
492 move across display lines. Used to set MATRIX_ROW_START_CHARPOS of
493 hscrolled lines, see display_line. */
494
495 static struct text_pos this_line_min_pos;
496
497 /* Buffer that this_line_.* variables are referring to. */
498
499 static struct buffer *this_line_buffer;
500
501
502 /* Values of those variables at last redisplay are stored as
503 properties on `overlay-arrow-position' symbol. However, if
504 Voverlay_arrow_position is a marker, last-arrow-position is its
505 numerical position. */
506
507 static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
508
509 /* Alternative overlay-arrow-string and overlay-arrow-bitmap
510 properties on a symbol in overlay-arrow-variable-list. */
511
512 static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
513
514 Lisp_Object Qmenu_bar_update_hook;
515
516 /* Nonzero if an overlay arrow has been displayed in this window. */
517
518 static int overlay_arrow_seen;
519
520 /* Number of windows showing the buffer of the selected
521 window (or another buffer with the same base buffer). */
522
523 int buffer_shared;
524
525 /* Vector containing glyphs for an ellipsis `...'. */
526
527 static Lisp_Object default_invis_vector[3];
528
529 /* This is the window where the echo area message was displayed. It
530 is always a mini-buffer window, but it may not be the same window
531 currently active as a mini-buffer. */
532
533 Lisp_Object echo_area_window;
534
535 /* List of pairs (MESSAGE . MULTIBYTE). The function save_message
536 pushes the current message and the value of
537 message_enable_multibyte on the stack, the function restore_message
538 pops the stack and displays MESSAGE again. */
539
540 static Lisp_Object Vmessage_stack;
541
542 /* Nonzero means multibyte characters were enabled when the echo area
543 message was specified. */
544
545 static int message_enable_multibyte;
546
547 /* Nonzero if we should redraw the mode lines on the next redisplay. */
548
549 int update_mode_lines;
550
551 /* Nonzero if window sizes or contents have changed since last
552 redisplay that finished. */
553
554 int windows_or_buffers_changed;
555
556 /* Nonzero means a frame's cursor type has been changed. */
557
558 int cursor_type_changed;
559
560 /* Nonzero after display_mode_line if %l was used and it displayed a
561 line number. */
562
563 static int line_number_displayed;
564
565 /* The name of the *Messages* buffer, a string. */
566
567 static Lisp_Object Vmessages_buffer_name;
568
569 /* Current, index 0, and last displayed echo area message. Either
570 buffers from echo_buffers, or nil to indicate no message. */
571
572 Lisp_Object echo_area_buffer[2];
573
574 /* The buffers referenced from echo_area_buffer. */
575
576 static Lisp_Object echo_buffer[2];
577
578 /* A vector saved used in with_area_buffer to reduce consing. */
579
580 static Lisp_Object Vwith_echo_area_save_vector;
581
582 /* Non-zero means display_echo_area should display the last echo area
583 message again. Set by redisplay_preserve_echo_area. */
584
585 static int display_last_displayed_message_p;
586
587 /* Nonzero if echo area is being used by print; zero if being used by
588 message. */
589
590 static int message_buf_print;
591
592 /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable. */
593
594 static Lisp_Object Qinhibit_menubar_update;
595 static Lisp_Object Qmessage_truncate_lines;
596
597 /* Set to 1 in clear_message to make redisplay_internal aware
598 of an emptied echo area. */
599
600 static int message_cleared_p;
601
602 /* A scratch glyph row with contents used for generating truncation
603 glyphs. Also used in direct_output_for_insert. */
604
605 #define MAX_SCRATCH_GLYPHS 100
606 static struct glyph_row scratch_glyph_row;
607 static struct glyph scratch_glyphs[MAX_SCRATCH_GLYPHS];
608
609 /* Ascent and height of the last line processed by move_it_to. */
610
611 static int last_max_ascent, last_height;
612
613 /* Non-zero if there's a help-echo in the echo area. */
614
615 int help_echo_showing_p;
616
617 /* If >= 0, computed, exact values of mode-line and header-line height
618 to use in the macros CURRENT_MODE_LINE_HEIGHT and
619 CURRENT_HEADER_LINE_HEIGHT. */
620
621 int current_mode_line_height, current_header_line_height;
622
623 /* The maximum distance to look ahead for text properties. Values
624 that are too small let us call compute_char_face and similar
625 functions too often which is expensive. Values that are too large
626 let us call compute_char_face and alike too often because we
627 might not be interested in text properties that far away. */
628
629 #define TEXT_PROP_DISTANCE_LIMIT 100
630
631 /* SAVE_IT and RESTORE_IT are called when we save a snapshot of the
632 iterator state and later restore it. This is needed because the
633 bidi iterator on bidi.c keeps a stacked cache of its states, which
634 is really a singleton. When we use scratch iterator objects to
635 move around the buffer, we can cause the bidi cache to be pushed or
636 popped, and therefore we need to restore the cache state when we
637 return to the original iterator. */
638 #define SAVE_IT(ITCOPY,ITORIG,CACHE) \
639 do { \
640 if (CACHE) \
641 bidi_unshelve_cache (CACHE, 1); \
642 ITCOPY = ITORIG; \
643 CACHE = bidi_shelve_cache (); \
644 } while (0)
645
646 #define RESTORE_IT(pITORIG,pITCOPY,CACHE) \
647 do { \
648 if (pITORIG != pITCOPY) \
649 *(pITORIG) = *(pITCOPY); \
650 bidi_unshelve_cache (CACHE, 0); \
651 CACHE = NULL; \
652 } while (0)
653
654 #ifdef GLYPH_DEBUG
655
656 /* Non-zero means print traces of redisplay if compiled with
657 GLYPH_DEBUG defined. */
658
659 int trace_redisplay_p;
660
661 #endif /* GLYPH_DEBUG */
662
663 #ifdef DEBUG_TRACE_MOVE
664 /* Non-zero means trace with TRACE_MOVE to stderr. */
665 int trace_move;
666
667 #define TRACE_MOVE(x) if (trace_move) fprintf x; else (void) 0
668 #else
669 #define TRACE_MOVE(x) (void) 0
670 #endif
671
672 static Lisp_Object Qauto_hscroll_mode;
673
674 /* Buffer being redisplayed -- for redisplay_window_error. */
675
676 static struct buffer *displayed_buffer;
677
678 /* Value returned from text property handlers (see below). */
679
680 enum prop_handled
681 {
682 HANDLED_NORMALLY,
683 HANDLED_RECOMPUTE_PROPS,
684 HANDLED_OVERLAY_STRING_CONSUMED,
685 HANDLED_RETURN
686 };
687
688 /* A description of text properties that redisplay is interested
689 in. */
690
691 struct props
692 {
693 /* The name of the property. */
694 Lisp_Object *name;
695
696 /* A unique index for the property. */
697 enum prop_idx idx;
698
699 /* A handler function called to set up iterator IT from the property
700 at IT's current position. Value is used to steer handle_stop. */
701 enum prop_handled (*handler) (struct it *it);
702 };
703
704 static enum prop_handled handle_face_prop (struct it *);
705 static enum prop_handled handle_invisible_prop (struct it *);
706 static enum prop_handled handle_display_prop (struct it *);
707 static enum prop_handled handle_composition_prop (struct it *);
708 static enum prop_handled handle_overlay_change (struct it *);
709 static enum prop_handled handle_fontified_prop (struct it *);
710
711 /* Properties handled by iterators. */
712
713 static struct props it_props[] =
714 {
715 {&Qfontified, FONTIFIED_PROP_IDX, handle_fontified_prop},
716 /* Handle `face' before `display' because some sub-properties of
717 `display' need to know the face. */
718 {&Qface, FACE_PROP_IDX, handle_face_prop},
719 {&Qdisplay, DISPLAY_PROP_IDX, handle_display_prop},
720 {&Qinvisible, INVISIBLE_PROP_IDX, handle_invisible_prop},
721 {&Qcomposition, COMPOSITION_PROP_IDX, handle_composition_prop},
722 {NULL, 0, NULL}
723 };
724
725 /* Value is the position described by X. If X is a marker, value is
726 the marker_position of X. Otherwise, value is X. */
727
728 #define COERCE_MARKER(X) (MARKERP ((X)) ? Fmarker_position (X) : (X))
729
730 /* Enumeration returned by some move_it_.* functions internally. */
731
732 enum move_it_result
733 {
734 /* Not used. Undefined value. */
735 MOVE_UNDEFINED,
736
737 /* Move ended at the requested buffer position or ZV. */
738 MOVE_POS_MATCH_OR_ZV,
739
740 /* Move ended at the requested X pixel position. */
741 MOVE_X_REACHED,
742
743 /* Move within a line ended at the end of a line that must be
744 continued. */
745 MOVE_LINE_CONTINUED,
746
747 /* Move within a line ended at the end of a line that would
748 be displayed truncated. */
749 MOVE_LINE_TRUNCATED,
750
751 /* Move within a line ended at a line end. */
752 MOVE_NEWLINE_OR_CR
753 };
754
755 /* This counter is used to clear the face cache every once in a while
756 in redisplay_internal. It is incremented for each redisplay.
757 Every CLEAR_FACE_CACHE_COUNT full redisplays, the face cache is
758 cleared. */
759
760 #define CLEAR_FACE_CACHE_COUNT 500
761 static int clear_face_cache_count;
762
763 /* Similarly for the image cache. */
764
765 #ifdef HAVE_WINDOW_SYSTEM
766 #define CLEAR_IMAGE_CACHE_COUNT 101
767 static int clear_image_cache_count;
768
769 /* Null glyph slice */
770 static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
771 #endif
772
773 /* True while redisplay_internal is in progress. */
774
775 bool redisplaying_p;
776
777 static Lisp_Object Qinhibit_free_realized_faces;
778 static Lisp_Object Qmode_line_default_help_echo;
779
780 /* If a string, XTread_socket generates an event to display that string.
781 (The display is done in read_char.) */
782
783 Lisp_Object help_echo_string;
784 Lisp_Object help_echo_window;
785 Lisp_Object help_echo_object;
786 ptrdiff_t help_echo_pos;
787
788 /* Temporary variable for XTread_socket. */
789
790 Lisp_Object previous_help_echo_string;
791
792 /* Platform-independent portion of hourglass implementation. */
793
794 /* Non-zero means an hourglass cursor is currently shown. */
795 int hourglass_shown_p;
796
797 /* If non-null, an asynchronous timer that, when it expires, displays
798 an hourglass cursor on all frames. */
799 struct atimer *hourglass_atimer;
800
801 /* Name of the face used to display glyphless characters. */
802 Lisp_Object Qglyphless_char;
803
804 /* Symbol for the purpose of Vglyphless_char_display. */
805 static Lisp_Object Qglyphless_char_display;
806
807 /* Method symbols for Vglyphless_char_display. */
808 static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
809
810 /* Default pixel width of `thin-space' display method. */
811 #define THIN_SPACE_WIDTH 1
812
813 /* Default number of seconds to wait before displaying an hourglass
814 cursor. */
815 #define DEFAULT_HOURGLASS_DELAY 1
816
817 \f
818 /* Function prototypes. */
819
820 static void setup_for_ellipsis (struct it *, int);
821 static void set_iterator_to_next (struct it *, int);
822 static void mark_window_display_accurate_1 (struct window *, int);
823 static int single_display_spec_string_p (Lisp_Object, Lisp_Object);
824 static int display_prop_string_p (Lisp_Object, Lisp_Object);
825 static int cursor_row_p (struct glyph_row *);
826 static int redisplay_mode_lines (Lisp_Object, int);
827 static char *decode_mode_spec_coding (Lisp_Object, char *, int);
828
829 static Lisp_Object get_it_property (struct it *it, Lisp_Object prop);
830
831 static void handle_line_prefix (struct it *);
832
833 static void pint2str (char *, int, ptrdiff_t);
834 static void pint2hrstr (char *, int, ptrdiff_t);
835 static struct text_pos run_window_scroll_functions (Lisp_Object,
836 struct text_pos);
837 static void reconsider_clip_changes (struct window *, struct buffer *);
838 static int text_outside_line_unchanged_p (struct window *,
839 ptrdiff_t, ptrdiff_t);
840 static void store_mode_line_noprop_char (char);
841 static int store_mode_line_noprop (const char *, int, int);
842 static void handle_stop (struct it *);
843 static void handle_stop_backwards (struct it *, ptrdiff_t);
844 static void vmessage (const char *, va_list) ATTRIBUTE_FORMAT_PRINTF (1, 0);
845 static void ensure_echo_area_buffers (void);
846 static Lisp_Object unwind_with_echo_area_buffer (Lisp_Object);
847 static Lisp_Object with_echo_area_buffer_unwind_data (struct window *);
848 static int with_echo_area_buffer (struct window *, int,
849 int (*) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
850 ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
851 static void clear_garbaged_frames (void);
852 static int current_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
853 static void pop_message (void);
854 static int truncate_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
855 static void set_message (const char *, Lisp_Object, ptrdiff_t, int);
856 static int set_message_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
857 static int display_echo_area (struct window *);
858 static int display_echo_area_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
859 static int resize_mini_window_1 (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t);
860 static Lisp_Object unwind_redisplay (Lisp_Object);
861 static int string_char_and_length (const unsigned char *, int *);
862 static struct text_pos display_prop_end (struct it *, Lisp_Object,
863 struct text_pos);
864 static int compute_window_start_on_continuation_line (struct window *);
865 static void insert_left_trunc_glyphs (struct it *);
866 static struct glyph_row *get_overlay_arrow_glyph_row (struct window *,
867 Lisp_Object);
868 static void extend_face_to_end_of_line (struct it *);
869 static int append_space_for_newline (struct it *, int);
870 static int cursor_row_fully_visible_p (struct window *, int, int);
871 static int try_scrolling (Lisp_Object, int, ptrdiff_t, ptrdiff_t, int, int);
872 static int try_cursor_movement (Lisp_Object, struct text_pos, int *);
873 static int trailing_whitespace_p (ptrdiff_t);
874 static intmax_t message_log_check_duplicate (ptrdiff_t, ptrdiff_t);
875 static void push_it (struct it *, struct text_pos *);
876 static void iterate_out_of_display_property (struct it *);
877 static void pop_it (struct it *);
878 static void sync_frame_with_window_matrix_rows (struct window *);
879 static void select_frame_for_redisplay (Lisp_Object);
880 static void redisplay_internal (void);
881 static int echo_area_display (int);
882 static void redisplay_windows (Lisp_Object);
883 static void redisplay_window (Lisp_Object, int);
884 static Lisp_Object redisplay_window_error (Lisp_Object);
885 static Lisp_Object redisplay_window_0 (Lisp_Object);
886 static Lisp_Object redisplay_window_1 (Lisp_Object);
887 static int set_cursor_from_row (struct window *, struct glyph_row *,
888 struct glyph_matrix *, ptrdiff_t, ptrdiff_t,
889 int, int);
890 static int update_menu_bar (struct frame *, int, int);
891 static int try_window_reusing_current_matrix (struct window *);
892 static int try_window_id (struct window *);
893 static int display_line (struct it *);
894 static int display_mode_lines (struct window *);
895 static int display_mode_line (struct window *, enum face_id, Lisp_Object);
896 static int display_mode_element (struct it *, int, int, int, Lisp_Object, Lisp_Object, int);
897 static int store_mode_line_string (const char *, Lisp_Object, int, int, int, Lisp_Object);
898 static const char *decode_mode_spec (struct window *, int, int, Lisp_Object *);
899 static void display_menu_bar (struct window *);
900 static ptrdiff_t display_count_lines (ptrdiff_t, ptrdiff_t, ptrdiff_t,
901 ptrdiff_t *);
902 static int display_string (const char *, Lisp_Object, Lisp_Object,
903 ptrdiff_t, ptrdiff_t, struct it *, int, int, int, int);
904 static void compute_line_metrics (struct it *);
905 static void run_redisplay_end_trigger_hook (struct it *);
906 static int get_overlay_strings (struct it *, ptrdiff_t);
907 static int get_overlay_strings_1 (struct it *, ptrdiff_t, int);
908 static void next_overlay_string (struct it *);
909 static void reseat (struct it *, struct text_pos, int);
910 static void reseat_1 (struct it *, struct text_pos, int);
911 static void back_to_previous_visible_line_start (struct it *);
912 void reseat_at_previous_visible_line_start (struct it *);
913 static void reseat_at_next_visible_line_start (struct it *, int);
914 static int next_element_from_ellipsis (struct it *);
915 static int next_element_from_display_vector (struct it *);
916 static int next_element_from_string (struct it *);
917 static int next_element_from_c_string (struct it *);
918 static int next_element_from_buffer (struct it *);
919 static int next_element_from_composition (struct it *);
920 static int next_element_from_image (struct it *);
921 #ifdef HAVE_XWIDGETS
922 static int next_element_from_xwidget(struct it *);
923 #endif
924 static int next_element_from_stretch (struct it *);
925 static void load_overlay_strings (struct it *, ptrdiff_t);
926 static int init_from_display_pos (struct it *, struct window *,
927 struct display_pos *);
928 static void reseat_to_string (struct it *, const char *,
929 Lisp_Object, ptrdiff_t, ptrdiff_t, int, int);
930 static int get_next_display_element (struct it *);
931 static enum move_it_result
932 move_it_in_display_line_to (struct it *, ptrdiff_t, int,
933 enum move_operation_enum);
934 void move_it_vertically_backward (struct it *, int);
935 static void get_visually_first_element (struct it *);
936 static void init_to_row_start (struct it *, struct window *,
937 struct glyph_row *);
938 static int init_to_row_end (struct it *, struct window *,
939 struct glyph_row *);
940 static void back_to_previous_line_start (struct it *);
941 static int forward_to_next_line_start (struct it *, int *, struct bidi_it *);
942 static struct text_pos string_pos_nchars_ahead (struct text_pos,
943 Lisp_Object, ptrdiff_t);
944 static struct text_pos string_pos (ptrdiff_t, Lisp_Object);
945 static struct text_pos c_string_pos (ptrdiff_t, const char *, int);
946 static ptrdiff_t number_of_chars (const char *, int);
947 static void compute_stop_pos (struct it *);
948 static void compute_string_pos (struct text_pos *, struct text_pos,
949 Lisp_Object);
950 static int face_before_or_after_it_pos (struct it *, int);
951 static ptrdiff_t next_overlay_change (ptrdiff_t);
952 static int handle_display_spec (struct it *, Lisp_Object, Lisp_Object,
953 Lisp_Object, struct text_pos *, ptrdiff_t, int);
954 static int handle_single_display_spec (struct it *, Lisp_Object,
955 Lisp_Object, Lisp_Object,
956 struct text_pos *, ptrdiff_t, int, int);
957 static int underlying_face_id (struct it *);
958 static int in_ellipses_for_invisible_text_p (struct display_pos *,
959 struct window *);
960
961 #define face_before_it_pos(IT) face_before_or_after_it_pos ((IT), 1)
962 #define face_after_it_pos(IT) face_before_or_after_it_pos ((IT), 0)
963
964 #ifdef HAVE_WINDOW_SYSTEM
965
966 static void x_consider_frame_title (Lisp_Object);
967 static int tool_bar_lines_needed (struct frame *, int *);
968 static void update_tool_bar (struct frame *, int);
969 static void build_desired_tool_bar_string (struct frame *f);
970 static int redisplay_tool_bar (struct frame *);
971 static void display_tool_bar_line (struct it *, int);
972 static void notice_overwritten_cursor (struct window *,
973 enum glyph_row_area,
974 int, int, int, int);
975 static void append_stretch_glyph (struct it *, Lisp_Object,
976 int, int, int);
977
978
979 #endif /* HAVE_WINDOW_SYSTEM */
980
981 static void produce_special_glyphs (struct it *, enum display_element_type);
982 static void show_mouse_face (Mouse_HLInfo *, enum draw_glyphs_face);
983 static int coords_in_mouse_face_p (struct window *, int, int);
984
985
986 \f
987 /***********************************************************************
988 Window display dimensions
989 ***********************************************************************/
990
991 /* Return the bottom boundary y-position for text lines in window W.
992 This is the first y position at which a line cannot start.
993 It is relative to the top of the window.
994
995 This is the height of W minus the height of a mode line, if any. */
996
997 int
998 window_text_bottom_y (struct window *w)
999 {
1000 int height = WINDOW_TOTAL_HEIGHT (w);
1001
1002 if (WINDOW_WANTS_MODELINE_P (w))
1003 height -= CURRENT_MODE_LINE_HEIGHT (w);
1004 return height;
1005 }
1006
1007 /* Return the pixel width of display area AREA of window W. AREA < 0
1008 means return the total width of W, not including fringes to
1009 the left and right of the window. */
1010
1011 int
1012 window_box_width (struct window *w, int area)
1013 {
1014 int cols = XFASTINT (w->total_cols);
1015 int pixels = 0;
1016
1017 if (!w->pseudo_window_p)
1018 {
1019 cols -= WINDOW_SCROLL_BAR_COLS (w);
1020
1021 if (area == TEXT_AREA)
1022 {
1023 if (INTEGERP (w->left_margin_cols))
1024 cols -= XFASTINT (w->left_margin_cols);
1025 if (INTEGERP (w->right_margin_cols))
1026 cols -= XFASTINT (w->right_margin_cols);
1027 pixels = -WINDOW_TOTAL_FRINGE_WIDTH (w);
1028 }
1029 else if (area == LEFT_MARGIN_AREA)
1030 {
1031 cols = (INTEGERP (w->left_margin_cols)
1032 ? XFASTINT (w->left_margin_cols) : 0);
1033 pixels = 0;
1034 }
1035 else if (area == RIGHT_MARGIN_AREA)
1036 {
1037 cols = (INTEGERP (w->right_margin_cols)
1038 ? XFASTINT (w->right_margin_cols) : 0);
1039 pixels = 0;
1040 }
1041 }
1042
1043 return cols * WINDOW_FRAME_COLUMN_WIDTH (w) + pixels;
1044 }
1045
1046
1047 /* Return the pixel height of the display area of window W, not
1048 including mode lines of W, if any. */
1049
1050 int
1051 window_box_height (struct window *w)
1052 {
1053 struct frame *f = XFRAME (w->frame);
1054 int height = WINDOW_TOTAL_HEIGHT (w);
1055
1056 eassert (height >= 0);
1057
1058 /* Note: the code below that determines the mode-line/header-line
1059 height is essentially the same as that contained in the macro
1060 CURRENT_{MODE,HEADER}_LINE_HEIGHT, except that it checks whether
1061 the appropriate glyph row has its `mode_line_p' flag set,
1062 and if it doesn't, uses estimate_mode_line_height instead. */
1063
1064 if (WINDOW_WANTS_MODELINE_P (w))
1065 {
1066 struct glyph_row *ml_row
1067 = (w->current_matrix && w->current_matrix->rows
1068 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
1069 : 0);
1070 if (ml_row && ml_row->mode_line_p)
1071 height -= ml_row->height;
1072 else
1073 height -= estimate_mode_line_height (f, CURRENT_MODE_LINE_FACE_ID (w));
1074 }
1075
1076 if (WINDOW_WANTS_HEADER_LINE_P (w))
1077 {
1078 struct glyph_row *hl_row
1079 = (w->current_matrix && w->current_matrix->rows
1080 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
1081 : 0);
1082 if (hl_row && hl_row->mode_line_p)
1083 height -= hl_row->height;
1084 else
1085 height -= estimate_mode_line_height (f, HEADER_LINE_FACE_ID);
1086 }
1087
1088 /* With a very small font and a mode-line that's taller than
1089 default, we might end up with a negative height. */
1090 return max (0, height);
1091 }
1092
1093 /* Return the window-relative coordinate of the left edge of display
1094 area AREA of window W. AREA < 0 means return the left edge of the
1095 whole window, to the right of the left fringe of W. */
1096
1097 int
1098 window_box_left_offset (struct window *w, int area)
1099 {
1100 int x;
1101
1102 if (w->pseudo_window_p)
1103 return 0;
1104
1105 x = WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
1106
1107 if (area == TEXT_AREA)
1108 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1109 + window_box_width (w, LEFT_MARGIN_AREA));
1110 else if (area == RIGHT_MARGIN_AREA)
1111 x += (WINDOW_LEFT_FRINGE_WIDTH (w)
1112 + window_box_width (w, LEFT_MARGIN_AREA)
1113 + window_box_width (w, TEXT_AREA)
1114 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
1115 ? 0
1116 : WINDOW_RIGHT_FRINGE_WIDTH (w)));
1117 else if (area == LEFT_MARGIN_AREA
1118 && WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w))
1119 x += WINDOW_LEFT_FRINGE_WIDTH (w);
1120
1121 return x;
1122 }
1123
1124
1125 /* Return the window-relative coordinate of the right edge of display
1126 area AREA of window W. AREA < 0 means return the right edge of the
1127 whole window, to the left of the right fringe of W. */
1128
1129 int
1130 window_box_right_offset (struct window *w, int area)
1131 {
1132 return window_box_left_offset (w, area) + window_box_width (w, area);
1133 }
1134
1135 /* Return the frame-relative coordinate of the left edge of display
1136 area AREA of window W. AREA < 0 means return the left edge of the
1137 whole window, to the right of the left fringe of W. */
1138
1139 int
1140 window_box_left (struct window *w, int area)
1141 {
1142 struct frame *f = XFRAME (w->frame);
1143 int x;
1144
1145 if (w->pseudo_window_p)
1146 return FRAME_INTERNAL_BORDER_WIDTH (f);
1147
1148 x = (WINDOW_LEFT_EDGE_X (w)
1149 + window_box_left_offset (w, area));
1150
1151 return x;
1152 }
1153
1154
1155 /* Return the frame-relative coordinate of the right edge of display
1156 area AREA of window W. AREA < 0 means return the right edge of the
1157 whole window, to the left of the right fringe of W. */
1158
1159 int
1160 window_box_right (struct window *w, int area)
1161 {
1162 return window_box_left (w, area) + window_box_width (w, area);
1163 }
1164
1165 /* Get the bounding box of the display area AREA of window W, without
1166 mode lines, in frame-relative coordinates. AREA < 0 means the
1167 whole window, not including the left and right fringes of
1168 the window. Return in *BOX_X and *BOX_Y the frame-relative pixel
1169 coordinates of the upper-left corner of the box. Return in
1170 *BOX_WIDTH, and *BOX_HEIGHT the pixel width and height of the box. */
1171
1172 void
1173 window_box (struct window *w, int area, int *box_x, int *box_y,
1174 int *box_width, int *box_height)
1175 {
1176 if (box_width)
1177 *box_width = window_box_width (w, area);
1178 if (box_height)
1179 *box_height = window_box_height (w);
1180 if (box_x)
1181 *box_x = window_box_left (w, area);
1182 if (box_y)
1183 {
1184 *box_y = WINDOW_TOP_EDGE_Y (w);
1185 if (WINDOW_WANTS_HEADER_LINE_P (w))
1186 *box_y += CURRENT_HEADER_LINE_HEIGHT (w);
1187 }
1188 }
1189
1190
1191 /* Get the bounding box of the display area AREA of window W, without
1192 mode lines. AREA < 0 means the whole window, not including the
1193 left and right fringe of the window. Return in *TOP_LEFT_X
1194 and TOP_LEFT_Y the frame-relative pixel coordinates of the
1195 upper-left corner of the box. Return in *BOTTOM_RIGHT_X, and
1196 *BOTTOM_RIGHT_Y the coordinates of the bottom-right corner of the
1197 box. */
1198
1199 static void
1200 window_box_edges (struct window *w, int area, int *top_left_x, int *top_left_y,
1201 int *bottom_right_x, int *bottom_right_y)
1202 {
1203 window_box (w, area, top_left_x, top_left_y, bottom_right_x,
1204 bottom_right_y);
1205 *bottom_right_x += *top_left_x;
1206 *bottom_right_y += *top_left_y;
1207 }
1208
1209
1210 \f
1211 /***********************************************************************
1212 Utilities
1213 ***********************************************************************/
1214
1215 /* Return the bottom y-position of the line the iterator IT is in.
1216 This can modify IT's settings. */
1217
1218 int
1219 line_bottom_y (struct it *it)
1220 {
1221 int line_height = it->max_ascent + it->max_descent;
1222 int line_top_y = it->current_y;
1223
1224 if (line_height == 0)
1225 {
1226 if (last_height)
1227 line_height = last_height;
1228 else if (IT_CHARPOS (*it) < ZV)
1229 {
1230 move_it_by_lines (it, 1);
1231 line_height = (it->max_ascent || it->max_descent
1232 ? it->max_ascent + it->max_descent
1233 : last_height);
1234 }
1235 else
1236 {
1237 struct glyph_row *row = it->glyph_row;
1238
1239 /* Use the default character height. */
1240 it->glyph_row = NULL;
1241 it->what = IT_CHARACTER;
1242 it->c = ' ';
1243 it->len = 1;
1244 PRODUCE_GLYPHS (it);
1245 line_height = it->ascent + it->descent;
1246 it->glyph_row = row;
1247 }
1248 }
1249
1250 return line_top_y + line_height;
1251 }
1252
1253 /* Subroutine of pos_visible_p below. Extracts a display string, if
1254 any, from the display spec given as its argument. */
1255 static Lisp_Object
1256 string_from_display_spec (Lisp_Object spec)
1257 {
1258 if (CONSP (spec))
1259 {
1260 while (CONSP (spec))
1261 {
1262 if (STRINGP (XCAR (spec)))
1263 return XCAR (spec);
1264 spec = XCDR (spec);
1265 }
1266 }
1267 else if (VECTORP (spec))
1268 {
1269 ptrdiff_t i;
1270
1271 for (i = 0; i < ASIZE (spec); i++)
1272 {
1273 if (STRINGP (AREF (spec, i)))
1274 return AREF (spec, i);
1275 }
1276 return Qnil;
1277 }
1278
1279 return spec;
1280 }
1281
1282
1283 /* Limit insanely large values of W->hscroll on frame F to the largest
1284 value that will still prevent first_visible_x and last_visible_x of
1285 'struct it' from overflowing an int. */
1286 static int
1287 window_hscroll_limited (struct window *w, struct frame *f)
1288 {
1289 ptrdiff_t window_hscroll = w->hscroll;
1290 int window_text_width = window_box_width (w, TEXT_AREA);
1291 int colwidth = FRAME_COLUMN_WIDTH (f);
1292
1293 if (window_hscroll > (INT_MAX - window_text_width) / colwidth - 1)
1294 window_hscroll = (INT_MAX - window_text_width) / colwidth - 1;
1295
1296 return window_hscroll;
1297 }
1298
1299 /* Return 1 if position CHARPOS is visible in window W.
1300 CHARPOS < 0 means return info about WINDOW_END position.
1301 If visible, set *X and *Y to pixel coordinates of top left corner.
1302 Set *RTOP and *RBOT to pixel height of an invisible area of glyph at POS.
1303 Set *ROWH and *VPOS to row's visible height and VPOS (row number). */
1304
1305 int
1306 pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
1307 int *rtop, int *rbot, int *rowh, int *vpos)
1308 {
1309 struct it it;
1310 void *itdata = bidi_shelve_cache ();
1311 struct text_pos top;
1312 int visible_p = 0;
1313 struct buffer *old_buffer = NULL;
1314
1315 if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
1316 return visible_p;
1317
1318 if (XBUFFER (w->buffer) != current_buffer)
1319 {
1320 old_buffer = current_buffer;
1321 set_buffer_internal_1 (XBUFFER (w->buffer));
1322 }
1323
1324 SET_TEXT_POS_FROM_MARKER (top, w->start);
1325 /* Scrolling a minibuffer window via scroll bar when the echo area
1326 shows long text sometimes resets the minibuffer contents behind
1327 our backs. */
1328 if (CHARPOS (top) > ZV)
1329 SET_TEXT_POS (top, BEGV, BEGV_BYTE);
1330
1331 /* Compute exact mode line heights. */
1332 if (WINDOW_WANTS_MODELINE_P (w))
1333 current_mode_line_height
1334 = display_mode_line (w, CURRENT_MODE_LINE_FACE_ID (w),
1335 BVAR (current_buffer, mode_line_format));
1336
1337 if (WINDOW_WANTS_HEADER_LINE_P (w))
1338 current_header_line_height
1339 = display_mode_line (w, HEADER_LINE_FACE_ID,
1340 BVAR (current_buffer, header_line_format));
1341
1342 start_display (&it, w, top);
1343 move_it_to (&it, charpos, -1, it.last_visible_y-1, -1,
1344 (charpos >= 0 ? MOVE_TO_POS : 0) | MOVE_TO_Y);
1345
1346 if (charpos >= 0
1347 && (((!it.bidi_p || it.bidi_it.scan_dir == 1)
1348 && IT_CHARPOS (it) >= charpos)
1349 /* When scanning backwards under bidi iteration, move_it_to
1350 stops at or _before_ CHARPOS, because it stops at or to
1351 the _right_ of the character at CHARPOS. */
1352 || (it.bidi_p && it.bidi_it.scan_dir == -1
1353 && IT_CHARPOS (it) <= charpos)))
1354 {
1355 /* We have reached CHARPOS, or passed it. How the call to
1356 move_it_to can overshoot: (i) If CHARPOS is on invisible text
1357 or covered by a display property, move_it_to stops at the end
1358 of the invisible text, to the right of CHARPOS. (ii) If
1359 CHARPOS is in a display vector, move_it_to stops on its last
1360 glyph. */
1361 int top_x = it.current_x;
1362 int top_y = it.current_y;
1363 /* Calling line_bottom_y may change it.method, it.position, etc. */
1364 enum it_method it_method = it.method;
1365 int bottom_y = (last_height = 0, line_bottom_y (&it));
1366 int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
1367
1368 if (top_y < window_top_y)
1369 visible_p = bottom_y > window_top_y;
1370 else if (top_y < it.last_visible_y)
1371 visible_p = 1;
1372 if (bottom_y >= it.last_visible_y
1373 && it.bidi_p && it.bidi_it.scan_dir == -1
1374 && IT_CHARPOS (it) < charpos)
1375 {
1376 /* When the last line of the window is scanned backwards
1377 under bidi iteration, we could be duped into thinking
1378 that we have passed CHARPOS, when in fact move_it_to
1379 simply stopped short of CHARPOS because it reached
1380 last_visible_y. To see if that's what happened, we call
1381 move_it_to again with a slightly larger vertical limit,
1382 and see if it actually moved vertically; if it did, we
1383 didn't really reach CHARPOS, which is beyond window end. */
1384 struct it save_it = it;
1385 /* Why 10? because we don't know how many canonical lines
1386 will the height of the next line(s) be. So we guess. */
1387 int ten_more_lines =
1388 10 * FRAME_LINE_HEIGHT (XFRAME (WINDOW_FRAME (w)));
1389
1390 move_it_to (&it, charpos, -1, bottom_y + ten_more_lines, -1,
1391 MOVE_TO_POS | MOVE_TO_Y);
1392 if (it.current_y > top_y)
1393 visible_p = 0;
1394
1395 it = save_it;
1396 }
1397 if (visible_p)
1398 {
1399 if (it_method == GET_FROM_DISPLAY_VECTOR)
1400 {
1401 /* We stopped on the last glyph of a display vector.
1402 Try and recompute. Hack alert! */
1403 if (charpos < 2 || top.charpos >= charpos)
1404 top_x = it.glyph_row->x;
1405 else
1406 {
1407 struct it it2;
1408 start_display (&it2, w, top);
1409 move_it_to (&it2, charpos - 1, -1, -1, -1, MOVE_TO_POS);
1410 get_next_display_element (&it2);
1411 PRODUCE_GLYPHS (&it2);
1412 if (ITERATOR_AT_END_OF_LINE_P (&it2)
1413 || it2.current_x > it2.last_visible_x)
1414 top_x = it.glyph_row->x;
1415 else
1416 {
1417 top_x = it2.current_x;
1418 top_y = it2.current_y;
1419 }
1420 }
1421 }
1422 else if (IT_CHARPOS (it) != charpos)
1423 {
1424 Lisp_Object cpos = make_number (charpos);
1425 Lisp_Object spec = Fget_char_property (cpos, Qdisplay, Qnil);
1426 Lisp_Object string = string_from_display_spec (spec);
1427 int newline_in_string = 0;
1428
1429 if (STRINGP (string))
1430 {
1431 const char *s = SSDATA (string);
1432 const char *e = s + SBYTES (string);
1433 while (s < e)
1434 {
1435 if (*s++ == '\n')
1436 {
1437 newline_in_string = 1;
1438 break;
1439 }
1440 }
1441 }
1442 /* The tricky code below is needed because there's a
1443 discrepancy between move_it_to and how we set cursor
1444 when the display line ends in a newline from a
1445 display string. move_it_to will stop _after_ such
1446 display strings, whereas set_cursor_from_row
1447 conspires with cursor_row_p to place the cursor on
1448 the first glyph produced from the display string. */
1449
1450 /* We have overshoot PT because it is covered by a
1451 display property whose value is a string. If the
1452 string includes embedded newlines, we are also in the
1453 wrong display line. Backtrack to the correct line,
1454 where the display string begins. */
1455 if (newline_in_string)
1456 {
1457 Lisp_Object startpos, endpos;
1458 EMACS_INT start, end;
1459 struct it it3;
1460 int it3_moved;
1461
1462 /* Find the first and the last buffer positions
1463 covered by the display string. */
1464 endpos =
1465 Fnext_single_char_property_change (cpos, Qdisplay,
1466 Qnil, Qnil);
1467 startpos =
1468 Fprevious_single_char_property_change (endpos, Qdisplay,
1469 Qnil, Qnil);
1470 start = XFASTINT (startpos);
1471 end = XFASTINT (endpos);
1472 /* Move to the last buffer position before the
1473 display property. */
1474 start_display (&it3, w, top);
1475 move_it_to (&it3, start - 1, -1, -1, -1, MOVE_TO_POS);
1476 /* Move forward one more line if the position before
1477 the display string is a newline or if it is the
1478 rightmost character on a line that is
1479 continued or word-wrapped. */
1480 if (it3.method == GET_FROM_BUFFER
1481 && it3.c == '\n')
1482 move_it_by_lines (&it3, 1);
1483 else if (move_it_in_display_line_to (&it3, -1,
1484 it3.current_x
1485 + it3.pixel_width,
1486 MOVE_TO_X)
1487 == MOVE_LINE_CONTINUED)
1488 {
1489 move_it_by_lines (&it3, 1);
1490 /* When we are under word-wrap, the #$@%!
1491 move_it_by_lines moves 2 lines, so we need to
1492 fix that up. */
1493 if (it3.line_wrap == WORD_WRAP)
1494 move_it_by_lines (&it3, -1);
1495 }
1496
1497 /* Record the vertical coordinate of the display
1498 line where we wound up. */
1499 top_y = it3.current_y;
1500 if (it3.bidi_p)
1501 {
1502 /* When characters are reordered for display,
1503 the character displayed to the left of the
1504 display string could be _after_ the display
1505 property in the logical order. Use the
1506 smallest vertical position of these two. */
1507 start_display (&it3, w, top);
1508 move_it_to (&it3, end + 1, -1, -1, -1, MOVE_TO_POS);
1509 if (it3.current_y < top_y)
1510 top_y = it3.current_y;
1511 }
1512 /* Move from the top of the window to the beginning
1513 of the display line where the display string
1514 begins. */
1515 start_display (&it3, w, top);
1516 move_it_to (&it3, -1, 0, top_y, -1, MOVE_TO_X | MOVE_TO_Y);
1517 /* If it3_moved stays zero after the 'while' loop
1518 below, that means we already were at a newline
1519 before the loop (e.g., the display string begins
1520 with a newline), so we don't need to (and cannot)
1521 inspect the glyphs of it3.glyph_row, because
1522 PRODUCE_GLYPHS will not produce anything for a
1523 newline, and thus it3.glyph_row stays at its
1524 stale content it got at top of the window. */
1525 it3_moved = 0;
1526 /* Finally, advance the iterator until we hit the
1527 first display element whose character position is
1528 CHARPOS, or until the first newline from the
1529 display string, which signals the end of the
1530 display line. */
1531 while (get_next_display_element (&it3))
1532 {
1533 PRODUCE_GLYPHS (&it3);
1534 if (IT_CHARPOS (it3) == charpos
1535 || ITERATOR_AT_END_OF_LINE_P (&it3))
1536 break;
1537 it3_moved = 1;
1538 set_iterator_to_next (&it3, 0);
1539 }
1540 top_x = it3.current_x - it3.pixel_width;
1541 /* Normally, we would exit the above loop because we
1542 found the display element whose character
1543 position is CHARPOS. For the contingency that we
1544 didn't, and stopped at the first newline from the
1545 display string, move back over the glyphs
1546 produced from the string, until we find the
1547 rightmost glyph not from the string. */
1548 if (it3_moved
1549 && IT_CHARPOS (it3) != charpos && EQ (it3.object, string))
1550 {
1551 struct glyph *g = it3.glyph_row->glyphs[TEXT_AREA]
1552 + it3.glyph_row->used[TEXT_AREA];
1553
1554 while (EQ ((g - 1)->object, string))
1555 {
1556 --g;
1557 top_x -= g->pixel_width;
1558 }
1559 eassert (g < it3.glyph_row->glyphs[TEXT_AREA]
1560 + it3.glyph_row->used[TEXT_AREA]);
1561 }
1562 }
1563 }
1564
1565 *x = top_x;
1566 *y = max (top_y + max (0, it.max_ascent - it.ascent), window_top_y);
1567 *rtop = max (0, window_top_y - top_y);
1568 *rbot = max (0, bottom_y - it.last_visible_y);
1569 *rowh = max (0, (min (bottom_y, it.last_visible_y)
1570 - max (top_y, window_top_y)));
1571 *vpos = it.vpos;
1572 }
1573 }
1574 else
1575 {
1576 /* We were asked to provide info about WINDOW_END. */
1577 struct it it2;
1578 void *it2data = NULL;
1579
1580 SAVE_IT (it2, it, it2data);
1581 if (IT_CHARPOS (it) < ZV && FETCH_BYTE (IT_BYTEPOS (it)) != '\n')
1582 move_it_by_lines (&it, 1);
1583 if (charpos < IT_CHARPOS (it)
1584 || (it.what == IT_EOB && charpos == IT_CHARPOS (it)))
1585 {
1586 visible_p = 1;
1587 RESTORE_IT (&it2, &it2, it2data);
1588 move_it_to (&it2, charpos, -1, -1, -1, MOVE_TO_POS);
1589 *x = it2.current_x;
1590 *y = it2.current_y + it2.max_ascent - it2.ascent;
1591 *rtop = max (0, -it2.current_y);
1592 *rbot = max (0, ((it2.current_y + it2.max_ascent + it2.max_descent)
1593 - it.last_visible_y));
1594 *rowh = max (0, (min (it2.current_y + it2.max_ascent + it2.max_descent,
1595 it.last_visible_y)
1596 - max (it2.current_y,
1597 WINDOW_HEADER_LINE_HEIGHT (w))));
1598 *vpos = it2.vpos;
1599 }
1600 else
1601 bidi_unshelve_cache (it2data, 1);
1602 }
1603 bidi_unshelve_cache (itdata, 0);
1604
1605 if (old_buffer)
1606 set_buffer_internal_1 (old_buffer);
1607
1608 current_header_line_height = current_mode_line_height = -1;
1609
1610 if (visible_p && w->hscroll > 0)
1611 *x -=
1612 window_hscroll_limited (w, WINDOW_XFRAME (w))
1613 * WINDOW_FRAME_COLUMN_WIDTH (w);
1614
1615 #if 0
1616 /* Debugging code. */
1617 if (visible_p)
1618 fprintf (stderr, "+pv pt=%d vs=%d --> x=%d y=%d rt=%d rb=%d rh=%d vp=%d\n",
1619 charpos, w->vscroll, *x, *y, *rtop, *rbot, *rowh, *vpos);
1620 else
1621 fprintf (stderr, "-pv pt=%d vs=%d\n", charpos, w->vscroll);
1622 #endif
1623
1624 return visible_p;
1625 }
1626
1627
1628 /* Return the next character from STR. Return in *LEN the length of
1629 the character. This is like STRING_CHAR_AND_LENGTH but never
1630 returns an invalid character. If we find one, we return a `?', but
1631 with the length of the invalid character. */
1632
1633 static int
1634 string_char_and_length (const unsigned char *str, int *len)
1635 {
1636 int c;
1637
1638 c = STRING_CHAR_AND_LENGTH (str, *len);
1639 if (!CHAR_VALID_P (c))
1640 /* We may not change the length here because other places in Emacs
1641 don't use this function, i.e. they silently accept invalid
1642 characters. */
1643 c = '?';
1644
1645 return c;
1646 }
1647
1648
1649
1650 /* Given a position POS containing a valid character and byte position
1651 in STRING, return the position NCHARS ahead (NCHARS >= 0). */
1652
1653 static struct text_pos
1654 string_pos_nchars_ahead (struct text_pos pos, Lisp_Object string, ptrdiff_t nchars)
1655 {
1656 eassert (STRINGP (string) && nchars >= 0);
1657
1658 if (STRING_MULTIBYTE (string))
1659 {
1660 const unsigned char *p = SDATA (string) + BYTEPOS (pos);
1661 int len;
1662
1663 while (nchars--)
1664 {
1665 string_char_and_length (p, &len);
1666 p += len;
1667 CHARPOS (pos) += 1;
1668 BYTEPOS (pos) += len;
1669 }
1670 }
1671 else
1672 SET_TEXT_POS (pos, CHARPOS (pos) + nchars, BYTEPOS (pos) + nchars);
1673
1674 return pos;
1675 }
1676
1677
1678 /* Value is the text position, i.e. character and byte position,
1679 for character position CHARPOS in STRING. */
1680
1681 static struct text_pos
1682 string_pos (ptrdiff_t charpos, Lisp_Object string)
1683 {
1684 struct text_pos pos;
1685 eassert (STRINGP (string));
1686 eassert (charpos >= 0);
1687 SET_TEXT_POS (pos, charpos, string_char_to_byte (string, charpos));
1688 return pos;
1689 }
1690
1691
1692 /* Value is a text position, i.e. character and byte position, for
1693 character position CHARPOS in C string S. MULTIBYTE_P non-zero
1694 means recognize multibyte characters. */
1695
1696 static struct text_pos
1697 c_string_pos (ptrdiff_t charpos, const char *s, int multibyte_p)
1698 {
1699 struct text_pos pos;
1700
1701 eassert (s != NULL);
1702 eassert (charpos >= 0);
1703
1704 if (multibyte_p)
1705 {
1706 int len;
1707
1708 SET_TEXT_POS (pos, 0, 0);
1709 while (charpos--)
1710 {
1711 string_char_and_length ((const unsigned char *) s, &len);
1712 s += len;
1713 CHARPOS (pos) += 1;
1714 BYTEPOS (pos) += len;
1715 }
1716 }
1717 else
1718 SET_TEXT_POS (pos, charpos, charpos);
1719
1720 return pos;
1721 }
1722
1723
1724 /* Value is the number of characters in C string S. MULTIBYTE_P
1725 non-zero means recognize multibyte characters. */
1726
1727 static ptrdiff_t
1728 number_of_chars (const char *s, int multibyte_p)
1729 {
1730 ptrdiff_t nchars;
1731
1732 if (multibyte_p)
1733 {
1734 ptrdiff_t rest = strlen (s);
1735 int len;
1736 const unsigned char *p = (const unsigned char *) s;
1737
1738 for (nchars = 0; rest > 0; ++nchars)
1739 {
1740 string_char_and_length (p, &len);
1741 rest -= len, p += len;
1742 }
1743 }
1744 else
1745 nchars = strlen (s);
1746
1747 return nchars;
1748 }
1749
1750
1751 /* Compute byte position NEWPOS->bytepos corresponding to
1752 NEWPOS->charpos. POS is a known position in string STRING.
1753 NEWPOS->charpos must be >= POS.charpos. */
1754
1755 static void
1756 compute_string_pos (struct text_pos *newpos, struct text_pos pos, Lisp_Object string)
1757 {
1758 eassert (STRINGP (string));
1759 eassert (CHARPOS (*newpos) >= CHARPOS (pos));
1760
1761 if (STRING_MULTIBYTE (string))
1762 *newpos = string_pos_nchars_ahead (pos, string,
1763 CHARPOS (*newpos) - CHARPOS (pos));
1764 else
1765 BYTEPOS (*newpos) = CHARPOS (*newpos);
1766 }
1767
1768 /* EXPORT:
1769 Return an estimation of the pixel height of mode or header lines on
1770 frame F. FACE_ID specifies what line's height to estimate. */
1771
1772 int
1773 estimate_mode_line_height (struct frame *f, enum face_id face_id)
1774 {
1775 #ifdef HAVE_WINDOW_SYSTEM
1776 if (FRAME_WINDOW_P (f))
1777 {
1778 int height = FONT_HEIGHT (FRAME_FONT (f));
1779
1780 /* This function is called so early when Emacs starts that the face
1781 cache and mode line face are not yet initialized. */
1782 if (FRAME_FACE_CACHE (f))
1783 {
1784 struct face *face = FACE_FROM_ID (f, face_id);
1785 if (face)
1786 {
1787 if (face->font)
1788 height = FONT_HEIGHT (face->font);
1789 if (face->box_line_width > 0)
1790 height += 2 * face->box_line_width;
1791 }
1792 }
1793
1794 return height;
1795 }
1796 #endif
1797
1798 return 1;
1799 }
1800
1801 /* Given a pixel position (PIX_X, PIX_Y) on frame F, return glyph
1802 co-ordinates in (*X, *Y). Set *BOUNDS to the rectangle that the
1803 glyph at X, Y occupies, if BOUNDS != 0. If NOCLIP is non-zero, do
1804 not force the value into range. */
1805
1806 void
1807 pixel_to_glyph_coords (FRAME_PTR f, register int pix_x, register int pix_y,
1808 int *x, int *y, NativeRectangle *bounds, int noclip)
1809 {
1810
1811 #ifdef HAVE_WINDOW_SYSTEM
1812 if (FRAME_WINDOW_P (f))
1813 {
1814 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to round down
1815 even for negative values. */
1816 if (pix_x < 0)
1817 pix_x -= FRAME_COLUMN_WIDTH (f) - 1;
1818 if (pix_y < 0)
1819 pix_y -= FRAME_LINE_HEIGHT (f) - 1;
1820
1821 pix_x = FRAME_PIXEL_X_TO_COL (f, pix_x);
1822 pix_y = FRAME_PIXEL_Y_TO_LINE (f, pix_y);
1823
1824 if (bounds)
1825 STORE_NATIVE_RECT (*bounds,
1826 FRAME_COL_TO_PIXEL_X (f, pix_x),
1827 FRAME_LINE_TO_PIXEL_Y (f, pix_y),
1828 FRAME_COLUMN_WIDTH (f) - 1,
1829 FRAME_LINE_HEIGHT (f) - 1);
1830
1831 if (!noclip)
1832 {
1833 if (pix_x < 0)
1834 pix_x = 0;
1835 else if (pix_x > FRAME_TOTAL_COLS (f))
1836 pix_x = FRAME_TOTAL_COLS (f);
1837
1838 if (pix_y < 0)
1839 pix_y = 0;
1840 else if (pix_y > FRAME_LINES (f))
1841 pix_y = FRAME_LINES (f);
1842 }
1843 }
1844 #endif
1845
1846 *x = pix_x;
1847 *y = pix_y;
1848 }
1849
1850
1851 /* Find the glyph under window-relative coordinates X/Y in window W.
1852 Consider only glyphs from buffer text, i.e. no glyphs from overlay
1853 strings. Return in *HPOS and *VPOS the row and column number of
1854 the glyph found. Return in *AREA the glyph area containing X.
1855 Value is a pointer to the glyph found or null if X/Y is not on
1856 text, or we can't tell because W's current matrix is not up to
1857 date. */
1858
1859 static
1860 struct glyph *
1861 x_y_to_hpos_vpos (struct window *w, int x, int y, int *hpos, int *vpos,
1862 int *dx, int *dy, int *area)
1863 {
1864 struct glyph *glyph, *end;
1865 struct glyph_row *row = NULL;
1866 int x0, i;
1867
1868 /* Find row containing Y. Give up if some row is not enabled. */
1869 for (i = 0; i < w->current_matrix->nrows; ++i)
1870 {
1871 row = MATRIX_ROW (w->current_matrix, i);
1872 if (!row->enabled_p)
1873 return NULL;
1874 if (y >= row->y && y < MATRIX_ROW_BOTTOM_Y (row))
1875 break;
1876 }
1877
1878 *vpos = i;
1879 *hpos = 0;
1880
1881 /* Give up if Y is not in the window. */
1882 if (i == w->current_matrix->nrows)
1883 return NULL;
1884
1885 /* Get the glyph area containing X. */
1886 if (w->pseudo_window_p)
1887 {
1888 *area = TEXT_AREA;
1889 x0 = 0;
1890 }
1891 else
1892 {
1893 if (x < window_box_left_offset (w, TEXT_AREA))
1894 {
1895 *area = LEFT_MARGIN_AREA;
1896 x0 = window_box_left_offset (w, LEFT_MARGIN_AREA);
1897 }
1898 else if (x < window_box_right_offset (w, TEXT_AREA))
1899 {
1900 *area = TEXT_AREA;
1901 x0 = window_box_left_offset (w, TEXT_AREA) + min (row->x, 0);
1902 }
1903 else
1904 {
1905 *area = RIGHT_MARGIN_AREA;
1906 x0 = window_box_left_offset (w, RIGHT_MARGIN_AREA);
1907 }
1908 }
1909
1910 /* Find glyph containing X. */
1911 glyph = row->glyphs[*area];
1912 end = glyph + row->used[*area];
1913 x -= x0;
1914 while (glyph < end && x >= glyph->pixel_width)
1915 {
1916 x -= glyph->pixel_width;
1917 ++glyph;
1918 }
1919
1920 if (glyph == end)
1921 return NULL;
1922
1923 if (dx)
1924 {
1925 *dx = x;
1926 *dy = y - (row->y + row->ascent - glyph->ascent);
1927 }
1928
1929 *hpos = glyph - row->glyphs[*area];
1930 return glyph;
1931 }
1932
1933 /* Convert frame-relative x/y to coordinates relative to window W.
1934 Takes pseudo-windows into account. */
1935
1936 static void
1937 frame_to_window_pixel_xy (struct window *w, int *x, int *y)
1938 {
1939 if (w->pseudo_window_p)
1940 {
1941 /* A pseudo-window is always full-width, and starts at the
1942 left edge of the frame, plus a frame border. */
1943 struct frame *f = XFRAME (w->frame);
1944 *x -= FRAME_INTERNAL_BORDER_WIDTH (f);
1945 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1946 }
1947 else
1948 {
1949 *x -= WINDOW_LEFT_EDGE_X (w);
1950 *y = FRAME_TO_WINDOW_PIXEL_Y (w, *y);
1951 }
1952 }
1953
1954 #ifdef HAVE_WINDOW_SYSTEM
1955
1956 /* EXPORT:
1957 Return in RECTS[] at most N clipping rectangles for glyph string S.
1958 Return the number of stored rectangles. */
1959
1960 int
1961 get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int n)
1962 {
1963 XRectangle r;
1964
1965 if (n <= 0)
1966 return 0;
1967
1968 if (s->row->full_width_p)
1969 {
1970 /* Draw full-width. X coordinates are relative to S->w->left_col. */
1971 r.x = WINDOW_LEFT_EDGE_X (s->w);
1972 r.width = WINDOW_TOTAL_WIDTH (s->w);
1973
1974 /* Unless displaying a mode or menu bar line, which are always
1975 fully visible, clip to the visible part of the row. */
1976 if (s->w->pseudo_window_p)
1977 r.height = s->row->visible_height;
1978 else
1979 r.height = s->height;
1980 }
1981 else
1982 {
1983 /* This is a text line that may be partially visible. */
1984 r.x = window_box_left (s->w, s->area);
1985 r.width = window_box_width (s->w, s->area);
1986 r.height = s->row->visible_height;
1987 }
1988
1989 if (s->clip_head)
1990 if (r.x < s->clip_head->x)
1991 {
1992 if (r.width >= s->clip_head->x - r.x)
1993 r.width -= s->clip_head->x - r.x;
1994 else
1995 r.width = 0;
1996 r.x = s->clip_head->x;
1997 }
1998 if (s->clip_tail)
1999 if (r.x + r.width > s->clip_tail->x + s->clip_tail->background_width)
2000 {
2001 if (s->clip_tail->x + s->clip_tail->background_width >= r.x)
2002 r.width = s->clip_tail->x + s->clip_tail->background_width - r.x;
2003 else
2004 r.width = 0;
2005 }
2006
2007 /* If S draws overlapping rows, it's sufficient to use the top and
2008 bottom of the window for clipping because this glyph string
2009 intentionally draws over other lines. */
2010 if (s->for_overlaps)
2011 {
2012 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2013 r.height = window_text_bottom_y (s->w) - r.y;
2014
2015 /* Alas, the above simple strategy does not work for the
2016 environments with anti-aliased text: if the same text is
2017 drawn onto the same place multiple times, it gets thicker.
2018 If the overlap we are processing is for the erased cursor, we
2019 take the intersection with the rectangle of the cursor. */
2020 if (s->for_overlaps & OVERLAPS_ERASED_CURSOR)
2021 {
2022 XRectangle rc, r_save = r;
2023
2024 rc.x = WINDOW_TEXT_TO_FRAME_PIXEL_X (s->w, s->w->phys_cursor.x);
2025 rc.y = s->w->phys_cursor.y;
2026 rc.width = s->w->phys_cursor_width;
2027 rc.height = s->w->phys_cursor_height;
2028
2029 x_intersect_rectangles (&r_save, &rc, &r);
2030 }
2031 }
2032 else
2033 {
2034 /* Don't use S->y for clipping because it doesn't take partially
2035 visible lines into account. For example, it can be negative for
2036 partially visible lines at the top of a window. */
2037 if (!s->row->full_width_p
2038 && MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (s->w, s->row))
2039 r.y = WINDOW_HEADER_LINE_HEIGHT (s->w);
2040 else
2041 r.y = max (0, s->row->y);
2042 }
2043
2044 r.y = WINDOW_TO_FRAME_PIXEL_Y (s->w, r.y);
2045
2046 /* If drawing the cursor, don't let glyph draw outside its
2047 advertised boundaries. Cleartype does this under some circumstances. */
2048 if (s->hl == DRAW_CURSOR)
2049 {
2050 struct glyph *glyph = s->first_glyph;
2051 int height, max_y;
2052
2053 if (s->x > r.x)
2054 {
2055 r.width -= s->x - r.x;
2056 r.x = s->x;
2057 }
2058 r.width = min (r.width, glyph->pixel_width);
2059
2060 /* If r.y is below window bottom, ensure that we still see a cursor. */
2061 height = min (glyph->ascent + glyph->descent,
2062 min (FRAME_LINE_HEIGHT (s->f), s->row->visible_height));
2063 max_y = window_text_bottom_y (s->w) - height;
2064 max_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, max_y);
2065 if (s->ybase - glyph->ascent > max_y)
2066 {
2067 r.y = max_y;
2068 r.height = height;
2069 }
2070 else
2071 {
2072 /* Don't draw cursor glyph taller than our actual glyph. */
2073 height = max (FRAME_LINE_HEIGHT (s->f), glyph->ascent + glyph->descent);
2074 if (height < r.height)
2075 {
2076 max_y = r.y + r.height;
2077 r.y = min (max_y, max (r.y, s->ybase + glyph->descent - height));
2078 r.height = min (max_y - r.y, height);
2079 }
2080 }
2081 }
2082
2083 if (s->row->clip)
2084 {
2085 XRectangle r_save = r;
2086
2087 if (! x_intersect_rectangles (&r_save, s->row->clip, &r))
2088 r.width = 0;
2089 }
2090
2091 if ((s->for_overlaps & OVERLAPS_BOTH) == 0
2092 || ((s->for_overlaps & OVERLAPS_BOTH) == OVERLAPS_BOTH && n == 1))
2093 {
2094 #ifdef CONVERT_FROM_XRECT
2095 CONVERT_FROM_XRECT (r, *rects);
2096 #else
2097 *rects = r;
2098 #endif
2099 return 1;
2100 }
2101 else
2102 {
2103 /* If we are processing overlapping and allowed to return
2104 multiple clipping rectangles, we exclude the row of the glyph
2105 string from the clipping rectangle. This is to avoid drawing
2106 the same text on the environment with anti-aliasing. */
2107 #ifdef CONVERT_FROM_XRECT
2108 XRectangle rs[2];
2109 #else
2110 XRectangle *rs = rects;
2111 #endif
2112 int i = 0, row_y = WINDOW_TO_FRAME_PIXEL_Y (s->w, s->row->y);
2113
2114 if (s->for_overlaps & OVERLAPS_PRED)
2115 {
2116 rs[i] = r;
2117 if (r.y + r.height > row_y)
2118 {
2119 if (r.y < row_y)
2120 rs[i].height = row_y - r.y;
2121 else
2122 rs[i].height = 0;
2123 }
2124 i++;
2125 }
2126 if (s->for_overlaps & OVERLAPS_SUCC)
2127 {
2128 rs[i] = r;
2129 if (r.y < row_y + s->row->visible_height)
2130 {
2131 if (r.y + r.height > row_y + s->row->visible_height)
2132 {
2133 rs[i].y = row_y + s->row->visible_height;
2134 rs[i].height = r.y + r.height - rs[i].y;
2135 }
2136 else
2137 rs[i].height = 0;
2138 }
2139 i++;
2140 }
2141
2142 n = i;
2143 #ifdef CONVERT_FROM_XRECT
2144 for (i = 0; i < n; i++)
2145 CONVERT_FROM_XRECT (rs[i], rects[i]);
2146 #endif
2147 return n;
2148 }
2149 }
2150
2151 /* EXPORT:
2152 Return in *NR the clipping rectangle for glyph string S. */
2153
2154 void
2155 get_glyph_string_clip_rect (struct glyph_string *s, NativeRectangle *nr)
2156 {
2157 get_glyph_string_clip_rects (s, nr, 1);
2158 }
2159
2160
2161 /* EXPORT:
2162 Return the position and height of the phys cursor in window W.
2163 Set w->phys_cursor_width to width of phys cursor.
2164 */
2165
2166 void
2167 get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
2168 struct glyph *glyph, int *xp, int *yp, int *heightp)
2169 {
2170 struct frame *f = XFRAME (WINDOW_FRAME (w));
2171 int x, y, wd, h, h0, y0;
2172
2173 /* Compute the width of the rectangle to draw. If on a stretch
2174 glyph, and `x-stretch-block-cursor' is nil, don't draw a
2175 rectangle as wide as the glyph, but use a canonical character
2176 width instead. */
2177 wd = glyph->pixel_width - 1;
2178 #if defined (HAVE_NTGUI) || defined (HAVE_NS)
2179 wd++; /* Why? */
2180 #endif
2181
2182 x = w->phys_cursor.x;
2183 if (x < 0)
2184 {
2185 wd += x;
2186 x = 0;
2187 }
2188
2189 if (glyph->type == STRETCH_GLYPH
2190 && !x_stretch_cursor_p)
2191 wd = min (FRAME_COLUMN_WIDTH (f), wd);
2192 w->phys_cursor_width = wd;
2193
2194 y = w->phys_cursor.y + row->ascent - glyph->ascent;
2195
2196 /* If y is below window bottom, ensure that we still see a cursor. */
2197 h0 = min (FRAME_LINE_HEIGHT (f), row->visible_height);
2198
2199 h = max (h0, glyph->ascent + glyph->descent);
2200 h0 = min (h0, glyph->ascent + glyph->descent);
2201
2202 y0 = WINDOW_HEADER_LINE_HEIGHT (w);
2203 if (y < y0)
2204 {
2205 h = max (h - (y0 - y) + 1, h0);
2206 y = y0 - 1;
2207 }
2208 else
2209 {
2210 y0 = window_text_bottom_y (w) - h0;
2211 if (y > y0)
2212 {
2213 h += y - y0;
2214 y = y0;
2215 }
2216 }
2217
2218 *xp = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
2219 *yp = WINDOW_TO_FRAME_PIXEL_Y (w, y);
2220 *heightp = h;
2221 }
2222
2223 /*
2224 * Remember which glyph the mouse is over.
2225 */
2226
2227 void
2228 remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
2229 {
2230 Lisp_Object window;
2231 struct window *w;
2232 struct glyph_row *r, *gr, *end_row;
2233 enum window_part part;
2234 enum glyph_row_area area;
2235 int x, y, width, height;
2236
2237 /* Try to determine frame pixel position and size of the glyph under
2238 frame pixel coordinates X/Y on frame F. */
2239
2240 if (!f->glyphs_initialized_p
2241 || (window = window_from_coordinates (f, gx, gy, &part, 0),
2242 NILP (window)))
2243 {
2244 width = FRAME_SMALLEST_CHAR_WIDTH (f);
2245 height = FRAME_SMALLEST_FONT_HEIGHT (f);
2246 goto virtual_glyph;
2247 }
2248
2249 w = XWINDOW (window);
2250 width = WINDOW_FRAME_COLUMN_WIDTH (w);
2251 height = WINDOW_FRAME_LINE_HEIGHT (w);
2252
2253 x = window_relative_x_coord (w, part, gx);
2254 y = gy - WINDOW_TOP_EDGE_Y (w);
2255
2256 r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
2257 end_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
2258
2259 if (w->pseudo_window_p)
2260 {
2261 area = TEXT_AREA;
2262 part = ON_MODE_LINE; /* Don't adjust margin. */
2263 goto text_glyph;
2264 }
2265
2266 switch (part)
2267 {
2268 case ON_LEFT_MARGIN:
2269 area = LEFT_MARGIN_AREA;
2270 goto text_glyph;
2271
2272 case ON_RIGHT_MARGIN:
2273 area = RIGHT_MARGIN_AREA;
2274 goto text_glyph;
2275
2276 case ON_HEADER_LINE:
2277 case ON_MODE_LINE:
2278 gr = (part == ON_HEADER_LINE
2279 ? MATRIX_HEADER_LINE_ROW (w->current_matrix)
2280 : MATRIX_MODE_LINE_ROW (w->current_matrix));
2281 gy = gr->y;
2282 area = TEXT_AREA;
2283 goto text_glyph_row_found;
2284
2285 case ON_TEXT:
2286 area = TEXT_AREA;
2287
2288 text_glyph:
2289 gr = 0; gy = 0;
2290 for (; r <= end_row && r->enabled_p; ++r)
2291 if (r->y + r->height > y)
2292 {
2293 gr = r; gy = r->y;
2294 break;
2295 }
2296
2297 text_glyph_row_found:
2298 if (gr && gy <= y)
2299 {
2300 struct glyph *g = gr->glyphs[area];
2301 struct glyph *end = g + gr->used[area];
2302
2303 height = gr->height;
2304 for (gx = gr->x; g < end; gx += g->pixel_width, ++g)
2305 if (gx + g->pixel_width > x)
2306 break;
2307
2308 if (g < end)
2309 {
2310 if (g->type == IMAGE_GLYPH)
2311 {
2312 /* Don't remember when mouse is over image, as
2313 image may have hot-spots. */
2314 STORE_NATIVE_RECT (*rect, 0, 0, 0, 0);
2315 return;
2316 }
2317 width = g->pixel_width;
2318 }
2319 else
2320 {
2321 /* Use nominal char spacing at end of line. */
2322 x -= gx;
2323 gx += (x / width) * width;
2324 }
2325
2326 if (part != ON_MODE_LINE && part != ON_HEADER_LINE)
2327 gx += window_box_left_offset (w, area);
2328 }
2329 else
2330 {
2331 /* Use nominal line height at end of window. */
2332 gx = (x / width) * width;
2333 y -= gy;
2334 gy += (y / height) * height;
2335 }
2336 break;
2337
2338 case ON_LEFT_FRINGE:
2339 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2340 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w)
2341 : window_box_right_offset (w, LEFT_MARGIN_AREA));
2342 width = WINDOW_LEFT_FRINGE_WIDTH (w);
2343 goto row_glyph;
2344
2345 case ON_RIGHT_FRINGE:
2346 gx = (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2347 ? window_box_right_offset (w, RIGHT_MARGIN_AREA)
2348 : window_box_right_offset (w, TEXT_AREA));
2349 width = WINDOW_RIGHT_FRINGE_WIDTH (w);
2350 goto row_glyph;
2351
2352 case ON_SCROLL_BAR:
2353 gx = (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w)
2354 ? 0
2355 : (window_box_right_offset (w, RIGHT_MARGIN_AREA)
2356 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
2357 ? WINDOW_RIGHT_FRINGE_WIDTH (w)
2358 : 0)));
2359 width = WINDOW_SCROLL_BAR_AREA_WIDTH (w);
2360
2361 row_glyph:
2362 gr = 0, gy = 0;
2363 for (; r <= end_row && r->enabled_p; ++r)
2364 if (r->y + r->height > y)
2365 {
2366 gr = r; gy = r->y;
2367 break;
2368 }
2369
2370 if (gr && gy <= y)
2371 height = gr->height;
2372 else
2373 {
2374 /* Use nominal line height at end of window. */
2375 y -= gy;
2376 gy += (y / height) * height;
2377 }
2378 break;
2379
2380 default:
2381 ;
2382 virtual_glyph:
2383 /* If there is no glyph under the mouse, then we divide the screen
2384 into a grid of the smallest glyph in the frame, and use that
2385 as our "glyph". */
2386
2387 /* Arrange for the division in FRAME_PIXEL_X_TO_COL etc. to
2388 round down even for negative values. */
2389 if (gx < 0)
2390 gx -= width - 1;
2391 if (gy < 0)
2392 gy -= height - 1;
2393
2394 gx = (gx / width) * width;
2395 gy = (gy / height) * height;
2396
2397 goto store_rect;
2398 }
2399
2400 gx += WINDOW_LEFT_EDGE_X (w);
2401 gy += WINDOW_TOP_EDGE_Y (w);
2402
2403 store_rect:
2404 STORE_NATIVE_RECT (*rect, gx, gy, width, height);
2405
2406 /* Visible feedback for debugging. */
2407 #if 0
2408 #if HAVE_X_WINDOWS
2409 XDrawRectangle (FRAME_X_DISPLAY (f), FRAME_X_WINDOW (f),
2410 f->output_data.x->normal_gc,
2411 gx, gy, width, height);
2412 #endif
2413 #endif
2414 }
2415
2416
2417 #endif /* HAVE_WINDOW_SYSTEM */
2418
2419 \f
2420 /***********************************************************************
2421 Lisp form evaluation
2422 ***********************************************************************/
2423
2424 /* Error handler for safe_eval and safe_call. */
2425
2426 static Lisp_Object
2427 safe_eval_handler (Lisp_Object arg, ptrdiff_t nargs, Lisp_Object *args)
2428 {
2429 add_to_log ("Error during redisplay: %S signaled %S",
2430 Flist (nargs, args), arg);
2431 return Qnil;
2432 }
2433
2434 /* Call function FUNC with the rest of NARGS - 1 arguments
2435 following. Return the result, or nil if something went
2436 wrong. Prevent redisplay during the evaluation. */
2437
2438 Lisp_Object
2439 safe_call (ptrdiff_t nargs, Lisp_Object func, ...)
2440 {
2441 Lisp_Object val;
2442
2443 if (inhibit_eval_during_redisplay)
2444 val = Qnil;
2445 else
2446 {
2447 va_list ap;
2448 ptrdiff_t i;
2449 ptrdiff_t count = SPECPDL_INDEX ();
2450 struct gcpro gcpro1;
2451 Lisp_Object *args = alloca (nargs * word_size);
2452
2453 args[0] = func;
2454 va_start (ap, func);
2455 for (i = 1; i < nargs; i++)
2456 args[i] = va_arg (ap, Lisp_Object);
2457 va_end (ap);
2458
2459 GCPRO1 (args[0]);
2460 gcpro1.nvars = nargs;
2461 specbind (Qinhibit_redisplay, Qt);
2462 /* Use Qt to ensure debugger does not run,
2463 so there is no possibility of wanting to redisplay. */
2464 val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
2465 safe_eval_handler);
2466 UNGCPRO;
2467 val = unbind_to (count, val);
2468 }
2469
2470 return val;
2471 }
2472
2473
2474 /* Call function FN with one argument ARG.
2475 Return the result, or nil if something went wrong. */
2476
2477 Lisp_Object
2478 safe_call1 (Lisp_Object fn, Lisp_Object arg)
2479 {
2480 return safe_call (2, fn, arg);
2481 }
2482
2483 static Lisp_Object Qeval;
2484
2485 Lisp_Object
2486 safe_eval (Lisp_Object sexpr)
2487 {
2488 return safe_call1 (Qeval, sexpr);
2489 }
2490
2491 /* Call function FN with two arguments ARG1 and ARG2.
2492 Return the result, or nil if something went wrong. */
2493
2494 Lisp_Object
2495 safe_call2 (Lisp_Object fn, Lisp_Object arg1, Lisp_Object arg2)
2496 {
2497 return safe_call (3, fn, arg1, arg2);
2498 }
2499
2500
2501 \f
2502 /***********************************************************************
2503 Debugging
2504 ***********************************************************************/
2505
2506 #if 0
2507
2508 /* Define CHECK_IT to perform sanity checks on iterators.
2509 This is for debugging. It is too slow to do unconditionally. */
2510
2511 static void
2512 check_it (struct it *it)
2513 {
2514 if (it->method == GET_FROM_STRING)
2515 {
2516 eassert (STRINGP (it->string));
2517 eassert (IT_STRING_CHARPOS (*it) >= 0);
2518 }
2519 else
2520 {
2521 eassert (IT_STRING_CHARPOS (*it) < 0);
2522 if (it->method == GET_FROM_BUFFER)
2523 {
2524 /* Check that character and byte positions agree. */
2525 eassert (IT_CHARPOS (*it) == BYTE_TO_CHAR (IT_BYTEPOS (*it)));
2526 }
2527 }
2528
2529 if (it->dpvec)
2530 eassert (it->current.dpvec_index >= 0);
2531 else
2532 eassert (it->current.dpvec_index < 0);
2533 }
2534
2535 #define CHECK_IT(IT) check_it ((IT))
2536
2537 #else /* not 0 */
2538
2539 #define CHECK_IT(IT) (void) 0
2540
2541 #endif /* not 0 */
2542
2543
2544 #if defined GLYPH_DEBUG && defined ENABLE_CHECKING
2545
2546 /* Check that the window end of window W is what we expect it
2547 to be---the last row in the current matrix displaying text. */
2548
2549 static void
2550 check_window_end (struct window *w)
2551 {
2552 if (!MINI_WINDOW_P (w)
2553 && !NILP (w->window_end_valid))
2554 {
2555 struct glyph_row *row;
2556 eassert ((row = MATRIX_ROW (w->current_matrix,
2557 XFASTINT (w->window_end_vpos)),
2558 !row->enabled_p
2559 || MATRIX_ROW_DISPLAYS_TEXT_P (row)
2560 || MATRIX_ROW_VPOS (row, w->current_matrix) == 0));
2561 }
2562 }
2563
2564 #define CHECK_WINDOW_END(W) check_window_end ((W))
2565
2566 #else
2567
2568 #define CHECK_WINDOW_END(W) (void) 0
2569
2570 #endif /* GLYPH_DEBUG and ENABLE_CHECKING */
2571
2572
2573 \f
2574 /***********************************************************************
2575 Iterator initialization
2576 ***********************************************************************/
2577
2578 /* Initialize IT for displaying current_buffer in window W, starting
2579 at character position CHARPOS. CHARPOS < 0 means that no buffer
2580 position is specified which is useful when the iterator is assigned
2581 a position later. BYTEPOS is the byte position corresponding to
2582 CHARPOS. BYTEPOS < 0 means compute it from CHARPOS.
2583
2584 If ROW is not null, calls to produce_glyphs with IT as parameter
2585 will produce glyphs in that row.
2586
2587 BASE_FACE_ID is the id of a base face to use. It must be one of
2588 DEFAULT_FACE_ID for normal text, MODE_LINE_FACE_ID,
2589 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID for displaying
2590 mode lines, or TOOL_BAR_FACE_ID for displaying the tool-bar.
2591
2592 If ROW is null and BASE_FACE_ID is equal to MODE_LINE_FACE_ID,
2593 MODE_LINE_INACTIVE_FACE_ID, or HEADER_LINE_FACE_ID, the iterator
2594 will be initialized to use the corresponding mode line glyph row of
2595 the desired matrix of W. */
2596
2597 void
2598 init_iterator (struct it *it, struct window *w,
2599 ptrdiff_t charpos, ptrdiff_t bytepos,
2600 struct glyph_row *row, enum face_id base_face_id)
2601 {
2602 int highlight_region_p;
2603 enum face_id remapped_base_face_id = base_face_id;
2604
2605 /* Some precondition checks. */
2606 eassert (w != NULL && it != NULL);
2607 eassert (charpos < 0 || (charpos >= BUF_BEG (current_buffer)
2608 && charpos <= ZV));
2609
2610 /* If face attributes have been changed since the last redisplay,
2611 free realized faces now because they depend on face definitions
2612 that might have changed. Don't free faces while there might be
2613 desired matrices pending which reference these faces. */
2614 if (face_change_count && !inhibit_free_realized_faces)
2615 {
2616 face_change_count = 0;
2617 free_all_realized_faces (Qnil);
2618 }
2619
2620 /* Perhaps remap BASE_FACE_ID to a user-specified alternative. */
2621 if (! NILP (Vface_remapping_alist))
2622 remapped_base_face_id
2623 = lookup_basic_face (XFRAME (w->frame), base_face_id);
2624
2625 /* Use one of the mode line rows of W's desired matrix if
2626 appropriate. */
2627 if (row == NULL)
2628 {
2629 if (base_face_id == MODE_LINE_FACE_ID
2630 || base_face_id == MODE_LINE_INACTIVE_FACE_ID)
2631 row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
2632 else if (base_face_id == HEADER_LINE_FACE_ID)
2633 row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
2634 }
2635
2636 /* Clear IT. */
2637 memset (it, 0, sizeof *it);
2638 it->current.overlay_string_index = -1;
2639 it->current.dpvec_index = -1;
2640 it->base_face_id = remapped_base_face_id;
2641 it->string = Qnil;
2642 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
2643 it->paragraph_embedding = L2R;
2644 it->bidi_it.string.lstring = Qnil;
2645 it->bidi_it.string.s = NULL;
2646 it->bidi_it.string.bufpos = 0;
2647
2648 /* The window in which we iterate over current_buffer: */
2649 XSETWINDOW (it->window, w);
2650 it->w = w;
2651 it->f = XFRAME (w->frame);
2652
2653 it->cmp_it.id = -1;
2654
2655 /* Extra space between lines (on window systems only). */
2656 if (base_face_id == DEFAULT_FACE_ID
2657 && FRAME_WINDOW_P (it->f))
2658 {
2659 if (NATNUMP (BVAR (current_buffer, extra_line_spacing)))
2660 it->extra_line_spacing = XFASTINT (BVAR (current_buffer, extra_line_spacing));
2661 else if (FLOATP (BVAR (current_buffer, extra_line_spacing)))
2662 it->extra_line_spacing = (XFLOAT_DATA (BVAR (current_buffer, extra_line_spacing))
2663 * FRAME_LINE_HEIGHT (it->f));
2664 else if (it->f->extra_line_spacing > 0)
2665 it->extra_line_spacing = it->f->extra_line_spacing;
2666 it->max_extra_line_spacing = 0;
2667 }
2668
2669 /* If realized faces have been removed, e.g. because of face
2670 attribute changes of named faces, recompute them. When running
2671 in batch mode, the face cache of the initial frame is null. If
2672 we happen to get called, make a dummy face cache. */
2673 if (FRAME_FACE_CACHE (it->f) == NULL)
2674 init_frame_faces (it->f);
2675 if (FRAME_FACE_CACHE (it->f)->used == 0)
2676 recompute_basic_faces (it->f);
2677
2678 /* Current value of the `slice', `space-width', and 'height' properties. */
2679 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
2680 it->space_width = Qnil;
2681 it->font_height = Qnil;
2682 it->override_ascent = -1;
2683
2684 /* Are control characters displayed as `^C'? */
2685 it->ctl_arrow_p = !NILP (BVAR (current_buffer, ctl_arrow));
2686
2687 /* -1 means everything between a CR and the following line end
2688 is invisible. >0 means lines indented more than this value are
2689 invisible. */
2690 it->selective = (INTEGERP (BVAR (current_buffer, selective_display))
2691 ? (clip_to_bounds
2692 (-1, XINT (BVAR (current_buffer, selective_display)),
2693 PTRDIFF_MAX))
2694 : (!NILP (BVAR (current_buffer, selective_display))
2695 ? -1 : 0));
2696 it->selective_display_ellipsis_p
2697 = !NILP (BVAR (current_buffer, selective_display_ellipses));
2698
2699 /* Display table to use. */
2700 it->dp = window_display_table (w);
2701
2702 /* Are multibyte characters enabled in current_buffer? */
2703 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
2704
2705 /* Non-zero if we should highlight the region. */
2706 highlight_region_p
2707 = (!NILP (Vtransient_mark_mode)
2708 && !NILP (BVAR (current_buffer, mark_active))
2709 && XMARKER (BVAR (current_buffer, mark))->buffer != 0);
2710
2711 /* Set IT->region_beg_charpos and IT->region_end_charpos to the
2712 start and end of a visible region in window IT->w. Set both to
2713 -1 to indicate no region. */
2714 if (highlight_region_p
2715 /* Maybe highlight only in selected window. */
2716 && (/* Either show region everywhere. */
2717 highlight_nonselected_windows
2718 /* Or show region in the selected window. */
2719 || w == XWINDOW (selected_window)
2720 /* Or show the region if we are in the mini-buffer and W is
2721 the window the mini-buffer refers to. */
2722 || (MINI_WINDOW_P (XWINDOW (selected_window))
2723 && WINDOWP (minibuf_selected_window)
2724 && w == XWINDOW (minibuf_selected_window))))
2725 {
2726 ptrdiff_t markpos = marker_position (BVAR (current_buffer, mark));
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
3769 /* If new face has a box but old face has not, this is
3770 the start of a run of characters with box, i.e. it has
3771 a shadow on the left side. The value of face_id of the
3772 iterator will be -1 if this is the initial call that gets
3773 the face. In this case, we have to look in front of IT's
3774 position and see whether there is a face != new_face_id. */
3775 it->start_of_box_run_p
3776 = (new_face->box != FACE_NO_BOX
3777 && (it->face_id >= 0
3778 || IT_CHARPOS (*it) == BEG
3779 || new_face_id != face_before_it_pos (it)));
3780 it->face_box_p = new_face->box != FACE_NO_BOX;
3781 }
3782 }
3783 else
3784 {
3785 int base_face_id;
3786 ptrdiff_t bufpos;
3787 int i;
3788 Lisp_Object from_overlay
3789 = (it->current.overlay_string_index >= 0
3790 ? it->string_overlays[it->current.overlay_string_index
3791 % OVERLAY_STRING_CHUNK_SIZE]
3792 : Qnil);
3793
3794 /* See if we got to this string directly or indirectly from
3795 an overlay property. That includes the before-string or
3796 after-string of an overlay, strings in display properties
3797 provided by an overlay, their text properties, etc.
3798
3799 FROM_OVERLAY is the overlay that brought us here, or nil if none. */
3800 if (! NILP (from_overlay))
3801 for (i = it->sp - 1; i >= 0; i--)
3802 {
3803 if (it->stack[i].current.overlay_string_index >= 0)
3804 from_overlay
3805 = it->string_overlays[it->stack[i].current.overlay_string_index
3806 % OVERLAY_STRING_CHUNK_SIZE];
3807 else if (! NILP (it->stack[i].from_overlay))
3808 from_overlay = it->stack[i].from_overlay;
3809
3810 if (!NILP (from_overlay))
3811 break;
3812 }
3813
3814 if (! NILP (from_overlay))
3815 {
3816 bufpos = IT_CHARPOS (*it);
3817 /* For a string from an overlay, the base face depends
3818 only on text properties and ignores overlays. */
3819 base_face_id
3820 = face_for_overlay_string (it->w,
3821 IT_CHARPOS (*it),
3822 it->region_beg_charpos,
3823 it->region_end_charpos,
3824 &next_stop,
3825 (IT_CHARPOS (*it)
3826 + TEXT_PROP_DISTANCE_LIMIT),
3827 0,
3828 from_overlay);
3829 }
3830 else
3831 {
3832 bufpos = 0;
3833
3834 /* For strings from a `display' property, use the face at
3835 IT's current buffer position as the base face to merge
3836 with, so that overlay strings appear in the same face as
3837 surrounding text, unless they specify their own
3838 faces. */
3839 base_face_id = it->string_from_prefix_prop_p
3840 ? DEFAULT_FACE_ID
3841 : underlying_face_id (it);
3842 }
3843
3844 new_face_id = face_at_string_position (it->w,
3845 it->string,
3846 IT_STRING_CHARPOS (*it),
3847 bufpos,
3848 it->region_beg_charpos,
3849 it->region_end_charpos,
3850 &next_stop,
3851 base_face_id, 0);
3852
3853 /* Is this a start of a run of characters with box? Caveat:
3854 this can be called for a freshly allocated iterator; face_id
3855 is -1 is this case. We know that the new face will not
3856 change until the next check pos, i.e. if the new face has a
3857 box, all characters up to that position will have a
3858 box. But, as usual, we don't know whether that position
3859 is really the end. */
3860 if (new_face_id != it->face_id)
3861 {
3862 struct face *new_face = FACE_FROM_ID (it->f, new_face_id);
3863 struct face *old_face = FACE_FROM_ID (it->f, it->face_id);
3864
3865 /* If new face has a box but old face hasn't, this is the
3866 start of a run of characters with box, i.e. it has a
3867 shadow on the left side. */
3868 it->start_of_box_run_p
3869 = new_face->box && (old_face == NULL || !old_face->box);
3870 it->face_box_p = new_face->box != FACE_NO_BOX;
3871 }
3872 }
3873
3874 it->face_id = new_face_id;
3875 return HANDLED_NORMALLY;
3876 }
3877
3878
3879 /* Return the ID of the face ``underlying'' IT's current position,
3880 which is in a string. If the iterator is associated with a
3881 buffer, return the face at IT's current buffer position.
3882 Otherwise, use the iterator's base_face_id. */
3883
3884 static int
3885 underlying_face_id (struct it *it)
3886 {
3887 int face_id = it->base_face_id, i;
3888
3889 eassert (STRINGP (it->string));
3890
3891 for (i = it->sp - 1; i >= 0; --i)
3892 if (NILP (it->stack[i].string))
3893 face_id = it->stack[i].face_id;
3894
3895 return face_id;
3896 }
3897
3898
3899 /* Compute the face one character before or after the current position
3900 of IT, in the visual order. BEFORE_P non-zero means get the face
3901 in front (to the left in L2R paragraphs, to the right in R2L
3902 paragraphs) of IT's screen position. Value is the ID of the face. */
3903
3904 static int
3905 face_before_or_after_it_pos (struct it *it, int before_p)
3906 {
3907 int face_id, limit;
3908 ptrdiff_t next_check_charpos;
3909 struct it it_copy;
3910 void *it_copy_data = NULL;
3911
3912 eassert (it->s == NULL);
3913
3914 if (STRINGP (it->string))
3915 {
3916 ptrdiff_t bufpos, charpos;
3917 int base_face_id;
3918
3919 /* No face change past the end of the string (for the case
3920 we are padding with spaces). No face change before the
3921 string start. */
3922 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string)
3923 || (IT_STRING_CHARPOS (*it) == 0 && before_p))
3924 return it->face_id;
3925
3926 if (!it->bidi_p)
3927 {
3928 /* Set charpos to the position before or after IT's current
3929 position, in the logical order, which in the non-bidi
3930 case is the same as the visual order. */
3931 if (before_p)
3932 charpos = IT_STRING_CHARPOS (*it) - 1;
3933 else if (it->what == IT_COMPOSITION)
3934 /* For composition, we must check the character after the
3935 composition. */
3936 charpos = IT_STRING_CHARPOS (*it) + it->cmp_it.nchars;
3937 else
3938 charpos = IT_STRING_CHARPOS (*it) + 1;
3939 }
3940 else
3941 {
3942 if (before_p)
3943 {
3944 /* With bidi iteration, the character before the current
3945 in the visual order cannot be found by simple
3946 iteration, because "reverse" reordering is not
3947 supported. Instead, we need to use the move_it_*
3948 family of functions. */
3949 /* Ignore face changes before the first visible
3950 character on this display line. */
3951 if (it->current_x <= it->first_visible_x)
3952 return it->face_id;
3953 SAVE_IT (it_copy, *it, it_copy_data);
3954 /* Implementation note: Since move_it_in_display_line
3955 works in the iterator geometry, and thinks the first
3956 character is always the leftmost, even in R2L lines,
3957 we don't need to distinguish between the R2L and L2R
3958 cases here. */
3959 move_it_in_display_line (&it_copy, SCHARS (it_copy.string),
3960 it_copy.current_x - 1, MOVE_TO_X);
3961 charpos = IT_STRING_CHARPOS (it_copy);
3962 RESTORE_IT (it, it, it_copy_data);
3963 }
3964 else
3965 {
3966 /* Set charpos to the string position of the character
3967 that comes after IT's current position in the visual
3968 order. */
3969 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
3970
3971 it_copy = *it;
3972 while (n--)
3973 bidi_move_to_visually_next (&it_copy.bidi_it);
3974
3975 charpos = it_copy.bidi_it.charpos;
3976 }
3977 }
3978 eassert (0 <= charpos && charpos <= SCHARS (it->string));
3979
3980 if (it->current.overlay_string_index >= 0)
3981 bufpos = IT_CHARPOS (*it);
3982 else
3983 bufpos = 0;
3984
3985 base_face_id = underlying_face_id (it);
3986
3987 /* Get the face for ASCII, or unibyte. */
3988 face_id = face_at_string_position (it->w,
3989 it->string,
3990 charpos,
3991 bufpos,
3992 it->region_beg_charpos,
3993 it->region_end_charpos,
3994 &next_check_charpos,
3995 base_face_id, 0);
3996
3997 /* Correct the face for charsets different from ASCII. Do it
3998 for the multibyte case only. The face returned above is
3999 suitable for unibyte text if IT->string is unibyte. */
4000 if (STRING_MULTIBYTE (it->string))
4001 {
4002 struct text_pos pos1 = string_pos (charpos, it->string);
4003 const unsigned char *p = SDATA (it->string) + BYTEPOS (pos1);
4004 int c, len;
4005 struct face *face = FACE_FROM_ID (it->f, face_id);
4006
4007 c = string_char_and_length (p, &len);
4008 face_id = FACE_FOR_CHAR (it->f, face, c, charpos, it->string);
4009 }
4010 }
4011 else
4012 {
4013 struct text_pos pos;
4014
4015 if ((IT_CHARPOS (*it) >= ZV && !before_p)
4016 || (IT_CHARPOS (*it) <= BEGV && before_p))
4017 return it->face_id;
4018
4019 limit = IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT;
4020 pos = it->current.pos;
4021
4022 if (!it->bidi_p)
4023 {
4024 if (before_p)
4025 DEC_TEXT_POS (pos, it->multibyte_p);
4026 else
4027 {
4028 if (it->what == IT_COMPOSITION)
4029 {
4030 /* For composition, we must check the position after
4031 the composition. */
4032 pos.charpos += it->cmp_it.nchars;
4033 pos.bytepos += it->len;
4034 }
4035 else
4036 INC_TEXT_POS (pos, it->multibyte_p);
4037 }
4038 }
4039 else
4040 {
4041 if (before_p)
4042 {
4043 /* With bidi iteration, the character before the current
4044 in the visual order cannot be found by simple
4045 iteration, because "reverse" reordering is not
4046 supported. Instead, we need to use the move_it_*
4047 family of functions. */
4048 /* Ignore face changes before the first visible
4049 character on this display line. */
4050 if (it->current_x <= it->first_visible_x)
4051 return it->face_id;
4052 SAVE_IT (it_copy, *it, it_copy_data);
4053 /* Implementation note: Since move_it_in_display_line
4054 works in the iterator geometry, and thinks the first
4055 character is always the leftmost, even in R2L lines,
4056 we don't need to distinguish between the R2L and L2R
4057 cases here. */
4058 move_it_in_display_line (&it_copy, ZV,
4059 it_copy.current_x - 1, MOVE_TO_X);
4060 pos = it_copy.current.pos;
4061 RESTORE_IT (it, it, it_copy_data);
4062 }
4063 else
4064 {
4065 /* Set charpos to the buffer position of the character
4066 that comes after IT's current position in the visual
4067 order. */
4068 int n = (it->what == IT_COMPOSITION ? it->cmp_it.nchars : 1);
4069
4070 it_copy = *it;
4071 while (n--)
4072 bidi_move_to_visually_next (&it_copy.bidi_it);
4073
4074 SET_TEXT_POS (pos,
4075 it_copy.bidi_it.charpos, it_copy.bidi_it.bytepos);
4076 }
4077 }
4078 eassert (BEGV <= CHARPOS (pos) && CHARPOS (pos) <= ZV);
4079
4080 /* Determine face for CHARSET_ASCII, or unibyte. */
4081 face_id = face_at_buffer_position (it->w,
4082 CHARPOS (pos),
4083 it->region_beg_charpos,
4084 it->region_end_charpos,
4085 &next_check_charpos,
4086 limit, 0, -1);
4087
4088 /* Correct the face for charsets different from ASCII. Do it
4089 for the multibyte case only. The face returned above is
4090 suitable for unibyte text if current_buffer is unibyte. */
4091 if (it->multibyte_p)
4092 {
4093 int c = FETCH_MULTIBYTE_CHAR (BYTEPOS (pos));
4094 struct face *face = FACE_FROM_ID (it->f, face_id);
4095 face_id = FACE_FOR_CHAR (it->f, face, c, CHARPOS (pos), Qnil);
4096 }
4097 }
4098
4099 return face_id;
4100 }
4101
4102
4103 \f
4104 /***********************************************************************
4105 Invisible text
4106 ***********************************************************************/
4107
4108 /* Set up iterator IT from invisible properties at its current
4109 position. Called from handle_stop. */
4110
4111 static enum prop_handled
4112 handle_invisible_prop (struct it *it)
4113 {
4114 enum prop_handled handled = HANDLED_NORMALLY;
4115 int invis_p;
4116 Lisp_Object prop;
4117
4118 if (STRINGP (it->string))
4119 {
4120 Lisp_Object end_charpos, limit, charpos;
4121
4122 /* Get the value of the invisible text property at the
4123 current position. Value will be nil if there is no such
4124 property. */
4125 charpos = make_number (IT_STRING_CHARPOS (*it));
4126 prop = Fget_text_property (charpos, Qinvisible, it->string);
4127 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4128
4129 if (invis_p && IT_STRING_CHARPOS (*it) < it->end_charpos)
4130 {
4131 /* Record whether we have to display an ellipsis for the
4132 invisible text. */
4133 int display_ellipsis_p = (invis_p == 2);
4134 ptrdiff_t len, endpos;
4135
4136 handled = HANDLED_RECOMPUTE_PROPS;
4137
4138 /* Get the position at which the next visible text can be
4139 found in IT->string, if any. */
4140 endpos = len = SCHARS (it->string);
4141 XSETINT (limit, len);
4142 do
4143 {
4144 end_charpos = Fnext_single_property_change (charpos, Qinvisible,
4145 it->string, limit);
4146 if (INTEGERP (end_charpos))
4147 {
4148 endpos = XFASTINT (end_charpos);
4149 prop = Fget_text_property (end_charpos, Qinvisible, it->string);
4150 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4151 if (invis_p == 2)
4152 display_ellipsis_p = 1;
4153 }
4154 }
4155 while (invis_p && endpos < len);
4156
4157 if (display_ellipsis_p)
4158 it->ellipsis_p = 1;
4159
4160 if (endpos < len)
4161 {
4162 /* Text at END_CHARPOS is visible. Move IT there. */
4163 struct text_pos old;
4164 ptrdiff_t oldpos;
4165
4166 old = it->current.string_pos;
4167 oldpos = CHARPOS (old);
4168 if (it->bidi_p)
4169 {
4170 if (it->bidi_it.first_elt
4171 && it->bidi_it.charpos < SCHARS (it->string))
4172 bidi_paragraph_init (it->paragraph_embedding,
4173 &it->bidi_it, 1);
4174 /* Bidi-iterate out of the invisible text. */
4175 do
4176 {
4177 bidi_move_to_visually_next (&it->bidi_it);
4178 }
4179 while (oldpos <= it->bidi_it.charpos
4180 && it->bidi_it.charpos < endpos);
4181
4182 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
4183 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
4184 if (IT_CHARPOS (*it) >= endpos)
4185 it->prev_stop = endpos;
4186 }
4187 else
4188 {
4189 IT_STRING_CHARPOS (*it) = XFASTINT (end_charpos);
4190 compute_string_pos (&it->current.string_pos, old, it->string);
4191 }
4192 }
4193 else
4194 {
4195 /* The rest of the string is invisible. If this is an
4196 overlay string, proceed with the next overlay string
4197 or whatever comes and return a character from there. */
4198 if (it->current.overlay_string_index >= 0
4199 && !display_ellipsis_p)
4200 {
4201 next_overlay_string (it);
4202 /* Don't check for overlay strings when we just
4203 finished processing them. */
4204 handled = HANDLED_OVERLAY_STRING_CONSUMED;
4205 }
4206 else
4207 {
4208 IT_STRING_CHARPOS (*it) = SCHARS (it->string);
4209 IT_STRING_BYTEPOS (*it) = SBYTES (it->string);
4210 }
4211 }
4212 }
4213 }
4214 else
4215 {
4216 ptrdiff_t newpos, next_stop, start_charpos, tem;
4217 Lisp_Object pos, overlay;
4218
4219 /* First of all, is there invisible text at this position? */
4220 tem = start_charpos = IT_CHARPOS (*it);
4221 pos = make_number (tem);
4222 prop = get_char_property_and_overlay (pos, Qinvisible, it->window,
4223 &overlay);
4224 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4225
4226 /* If we are on invisible text, skip over it. */
4227 if (invis_p && start_charpos < it->end_charpos)
4228 {
4229 /* Record whether we have to display an ellipsis for the
4230 invisible text. */
4231 int display_ellipsis_p = invis_p == 2;
4232
4233 handled = HANDLED_RECOMPUTE_PROPS;
4234
4235 /* Loop skipping over invisible text. The loop is left at
4236 ZV or with IT on the first char being visible again. */
4237 do
4238 {
4239 /* Try to skip some invisible text. Return value is the
4240 position reached which can be equal to where we start
4241 if there is nothing invisible there. This skips both
4242 over invisible text properties and overlays with
4243 invisible property. */
4244 newpos = skip_invisible (tem, &next_stop, ZV, it->window);
4245
4246 /* If we skipped nothing at all we weren't at invisible
4247 text in the first place. If everything to the end of
4248 the buffer was skipped, end the loop. */
4249 if (newpos == tem || newpos >= ZV)
4250 invis_p = 0;
4251 else
4252 {
4253 /* We skipped some characters but not necessarily
4254 all there are. Check if we ended up on visible
4255 text. Fget_char_property returns the property of
4256 the char before the given position, i.e. if we
4257 get invis_p = 0, this means that the char at
4258 newpos is visible. */
4259 pos = make_number (newpos);
4260 prop = Fget_char_property (pos, Qinvisible, it->window);
4261 invis_p = TEXT_PROP_MEANS_INVISIBLE (prop);
4262 }
4263
4264 /* If we ended up on invisible text, proceed to
4265 skip starting with next_stop. */
4266 if (invis_p)
4267 tem = next_stop;
4268
4269 /* If there are adjacent invisible texts, don't lose the
4270 second one's ellipsis. */
4271 if (invis_p == 2)
4272 display_ellipsis_p = 1;
4273 }
4274 while (invis_p);
4275
4276 /* The position newpos is now either ZV or on visible text. */
4277 if (it->bidi_p)
4278 {
4279 ptrdiff_t bpos = CHAR_TO_BYTE (newpos);
4280 int on_newline =
4281 bpos == ZV_BYTE || FETCH_BYTE (bpos) == '\n';
4282 int after_newline =
4283 newpos <= BEGV || FETCH_BYTE (bpos - 1) == '\n';
4284
4285 /* If the invisible text ends on a newline or on a
4286 character after a newline, we can avoid the costly,
4287 character by character, bidi iteration to NEWPOS, and
4288 instead simply reseat the iterator there. That's
4289 because all bidi reordering information is tossed at
4290 the newline. This is a big win for modes that hide
4291 complete lines, like Outline, Org, etc. */
4292 if (on_newline || after_newline)
4293 {
4294 struct text_pos tpos;
4295 bidi_dir_t pdir = it->bidi_it.paragraph_dir;
4296
4297 SET_TEXT_POS (tpos, newpos, bpos);
4298 reseat_1 (it, tpos, 0);
4299 /* If we reseat on a newline/ZV, we need to prep the
4300 bidi iterator for advancing to the next character
4301 after the newline/EOB, keeping the current paragraph
4302 direction (so that PRODUCE_GLYPHS does TRT wrt
4303 prepending/appending glyphs to a glyph row). */
4304 if (on_newline)
4305 {
4306 it->bidi_it.first_elt = 0;
4307 it->bidi_it.paragraph_dir = pdir;
4308 it->bidi_it.ch = (bpos == ZV_BYTE) ? -1 : '\n';
4309 it->bidi_it.nchars = 1;
4310 it->bidi_it.ch_len = 1;
4311 }
4312 }
4313 else /* Must use the slow method. */
4314 {
4315 /* With bidi iteration, the region of invisible text
4316 could start and/or end in the middle of a
4317 non-base embedding level. Therefore, we need to
4318 skip invisible text using the bidi iterator,
4319 starting at IT's current position, until we find
4320 ourselves outside of the invisible text.
4321 Skipping invisible text _after_ bidi iteration
4322 avoids affecting the visual order of the
4323 displayed text when invisible properties are
4324 added or removed. */
4325 if (it->bidi_it.first_elt && it->bidi_it.charpos < ZV)
4326 {
4327 /* If we were `reseat'ed to a new paragraph,
4328 determine the paragraph base direction. We
4329 need to do it now because
4330 next_element_from_buffer may not have a
4331 chance to do it, if we are going to skip any
4332 text at the beginning, which resets the
4333 FIRST_ELT flag. */
4334 bidi_paragraph_init (it->paragraph_embedding,
4335 &it->bidi_it, 1);
4336 }
4337 do
4338 {
4339 bidi_move_to_visually_next (&it->bidi_it);
4340 }
4341 while (it->stop_charpos <= it->bidi_it.charpos
4342 && it->bidi_it.charpos < newpos);
4343 IT_CHARPOS (*it) = it->bidi_it.charpos;
4344 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
4345 /* If we overstepped NEWPOS, record its position in
4346 the iterator, so that we skip invisible text if
4347 later the bidi iteration lands us in the
4348 invisible region again. */
4349 if (IT_CHARPOS (*it) >= newpos)
4350 it->prev_stop = newpos;
4351 }
4352 }
4353 else
4354 {
4355 IT_CHARPOS (*it) = newpos;
4356 IT_BYTEPOS (*it) = CHAR_TO_BYTE (newpos);
4357 }
4358
4359 /* If there are before-strings at the start of invisible
4360 text, and the text is invisible because of a text
4361 property, arrange to show before-strings because 20.x did
4362 it that way. (If the text is invisible because of an
4363 overlay property instead of a text property, this is
4364 already handled in the overlay code.) */
4365 if (NILP (overlay)
4366 && get_overlay_strings (it, it->stop_charpos))
4367 {
4368 handled = HANDLED_RECOMPUTE_PROPS;
4369 it->stack[it->sp - 1].display_ellipsis_p = display_ellipsis_p;
4370 }
4371 else if (display_ellipsis_p)
4372 {
4373 /* Make sure that the glyphs of the ellipsis will get
4374 correct `charpos' values. If we would not update
4375 it->position here, the glyphs would belong to the
4376 last visible character _before_ the invisible
4377 text, which confuses `set_cursor_from_row'.
4378
4379 We use the last invisible position instead of the
4380 first because this way the cursor is always drawn on
4381 the first "." of the ellipsis, whenever PT is inside
4382 the invisible text. Otherwise the cursor would be
4383 placed _after_ the ellipsis when the point is after the
4384 first invisible character. */
4385 if (!STRINGP (it->object))
4386 {
4387 it->position.charpos = newpos - 1;
4388 it->position.bytepos = CHAR_TO_BYTE (it->position.charpos);
4389 }
4390 it->ellipsis_p = 1;
4391 /* Let the ellipsis display before
4392 considering any properties of the following char.
4393 Fixes jasonr@gnu.org 01 Oct 07 bug. */
4394 handled = HANDLED_RETURN;
4395 }
4396 }
4397 }
4398
4399 return handled;
4400 }
4401
4402
4403 /* Make iterator IT return `...' next.
4404 Replaces LEN characters from buffer. */
4405
4406 static void
4407 setup_for_ellipsis (struct it *it, int len)
4408 {
4409 /* Use the display table definition for `...'. Invalid glyphs
4410 will be handled by the method returning elements from dpvec. */
4411 if (it->dp && VECTORP (DISP_INVIS_VECTOR (it->dp)))
4412 {
4413 struct Lisp_Vector *v = XVECTOR (DISP_INVIS_VECTOR (it->dp));
4414 it->dpvec = v->contents;
4415 it->dpend = v->contents + v->header.size;
4416 }
4417 else
4418 {
4419 /* Default `...'. */
4420 it->dpvec = default_invis_vector;
4421 it->dpend = default_invis_vector + 3;
4422 }
4423
4424 it->dpvec_char_len = len;
4425 it->current.dpvec_index = 0;
4426 it->dpvec_face_id = -1;
4427
4428 /* Remember the current face id in case glyphs specify faces.
4429 IT's face is restored in set_iterator_to_next.
4430 saved_face_id was set to preceding char's face in handle_stop. */
4431 if (it->saved_face_id < 0 || it->saved_face_id != it->face_id)
4432 it->saved_face_id = it->face_id = DEFAULT_FACE_ID;
4433
4434 it->method = GET_FROM_DISPLAY_VECTOR;
4435 it->ellipsis_p = 1;
4436 }
4437
4438
4439 \f
4440 /***********************************************************************
4441 'display' property
4442 ***********************************************************************/
4443
4444 /* Set up iterator IT from `display' property at its current position.
4445 Called from handle_stop.
4446 We return HANDLED_RETURN if some part of the display property
4447 overrides the display of the buffer text itself.
4448 Otherwise we return HANDLED_NORMALLY. */
4449
4450 static enum prop_handled
4451 handle_display_prop (struct it *it)
4452 {
4453 Lisp_Object propval, object, overlay;
4454 struct text_pos *position;
4455 ptrdiff_t bufpos;
4456 /* Nonzero if some property replaces the display of the text itself. */
4457 int display_replaced_p = 0;
4458
4459 if (STRINGP (it->string))
4460 {
4461 object = it->string;
4462 position = &it->current.string_pos;
4463 bufpos = CHARPOS (it->current.pos);
4464 }
4465 else
4466 {
4467 XSETWINDOW (object, it->w);
4468 position = &it->current.pos;
4469 bufpos = CHARPOS (*position);
4470 }
4471
4472 /* Reset those iterator values set from display property values. */
4473 it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
4474 it->space_width = Qnil;
4475 it->font_height = Qnil;
4476 it->voffset = 0;
4477
4478 /* We don't support recursive `display' properties, i.e. string
4479 values that have a string `display' property, that have a string
4480 `display' property etc. */
4481 if (!it->string_from_display_prop_p)
4482 it->area = TEXT_AREA;
4483
4484 propval = get_char_property_and_overlay (make_number (position->charpos),
4485 Qdisplay, object, &overlay);
4486 if (NILP (propval))
4487 return HANDLED_NORMALLY;
4488 /* Now OVERLAY is the overlay that gave us this property, or nil
4489 if it was a text property. */
4490
4491 if (!STRINGP (it->string))
4492 object = it->w->buffer;
4493
4494 display_replaced_p = handle_display_spec (it, propval, object, overlay,
4495 position, bufpos,
4496 FRAME_WINDOW_P (it->f));
4497
4498 return display_replaced_p ? HANDLED_RETURN : HANDLED_NORMALLY;
4499 }
4500
4501 /* Subroutine of handle_display_prop. Returns non-zero if the display
4502 specification in SPEC is a replacing specification, i.e. it would
4503 replace the text covered by `display' property with something else,
4504 such as an image or a display string. If SPEC includes any kind or
4505 `(space ...) specification, the value is 2; this is used by
4506 compute_display_string_pos, which see.
4507
4508 See handle_single_display_spec for documentation of arguments.
4509 frame_window_p is non-zero if the window being redisplayed is on a
4510 GUI frame; this argument is used only if IT is NULL, see below.
4511
4512 IT can be NULL, if this is called by the bidi reordering code
4513 through compute_display_string_pos, which see. In that case, this
4514 function only examines SPEC, but does not otherwise "handle" it, in
4515 the sense that it doesn't set up members of IT from the display
4516 spec. */
4517 static int
4518 handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4519 Lisp_Object overlay, struct text_pos *position,
4520 ptrdiff_t bufpos, int frame_window_p)
4521 {
4522 int replacing_p = 0;
4523 int rv;
4524
4525 if (CONSP (spec)
4526 /* Simple specifications. */
4527 && !EQ (XCAR (spec), Qimage)
4528 #ifdef HAVE_XWIDGETS
4529 && !EQ (XCAR (spec), Qxwidget)
4530 #endif
4531 && !EQ (XCAR (spec), Qspace)
4532 && !EQ (XCAR (spec), Qwhen)
4533 && !EQ (XCAR (spec), Qslice)
4534 && !EQ (XCAR (spec), Qspace_width)
4535 && !EQ (XCAR (spec), Qheight)
4536 && !EQ (XCAR (spec), Qraise)
4537 /* Marginal area specifications. */
4538 && !(CONSP (XCAR (spec)) && EQ (XCAR (XCAR (spec)), Qmargin))
4539 && !EQ (XCAR (spec), Qleft_fringe)
4540 && !EQ (XCAR (spec), Qright_fringe)
4541 && !NILP (XCAR (spec)))
4542 {
4543 for (; CONSP (spec); spec = XCDR (spec))
4544 {
4545 if ((rv = handle_single_display_spec (it, XCAR (spec), object,
4546 overlay, position, bufpos,
4547 replacing_p, frame_window_p)))
4548 {
4549 replacing_p = rv;
4550 /* If some text in a string is replaced, `position' no
4551 longer points to the position of `object'. */
4552 if (!it || STRINGP (object))
4553 break;
4554 }
4555 }
4556 }
4557 else if (VECTORP (spec))
4558 {
4559 ptrdiff_t i;
4560 for (i = 0; i < ASIZE (spec); ++i)
4561 if ((rv = handle_single_display_spec (it, AREF (spec, i), object,
4562 overlay, position, bufpos,
4563 replacing_p, frame_window_p)))
4564 {
4565 replacing_p = rv;
4566 /* If some text in a string is replaced, `position' no
4567 longer points to the position of `object'. */
4568 if (!it || STRINGP (object))
4569 break;
4570 }
4571 }
4572 else
4573 {
4574 if ((rv = handle_single_display_spec (it, spec, object, overlay,
4575 position, bufpos, 0,
4576 frame_window_p)))
4577 replacing_p = rv;
4578 }
4579
4580 return replacing_p;
4581 }
4582
4583 /* Value is the position of the end of the `display' property starting
4584 at START_POS in OBJECT. */
4585
4586 static struct text_pos
4587 display_prop_end (struct it *it, Lisp_Object object, struct text_pos start_pos)
4588 {
4589 Lisp_Object end;
4590 struct text_pos end_pos;
4591
4592 end = Fnext_single_char_property_change (make_number (CHARPOS (start_pos)),
4593 Qdisplay, object, Qnil);
4594 CHARPOS (end_pos) = XFASTINT (end);
4595 if (STRINGP (object))
4596 compute_string_pos (&end_pos, start_pos, it->string);
4597 else
4598 BYTEPOS (end_pos) = CHAR_TO_BYTE (XFASTINT (end));
4599
4600 return end_pos;
4601 }
4602
4603
4604 /* Set up IT from a single `display' property specification SPEC. OBJECT
4605 is the object in which the `display' property was found. *POSITION
4606 is the position in OBJECT at which the `display' property was found.
4607 BUFPOS is the buffer position of OBJECT (different from POSITION if
4608 OBJECT is not a buffer). DISPLAY_REPLACED_P non-zero means that we
4609 previously saw a display specification which already replaced text
4610 display with something else, for example an image; we ignore such
4611 properties after the first one has been processed.
4612
4613 OVERLAY is the overlay this `display' property came from,
4614 or nil if it was a text property.
4615
4616 If SPEC is a `space' or `image' specification, and in some other
4617 cases too, set *POSITION to the position where the `display'
4618 property ends.
4619
4620 If IT is NULL, only examine the property specification in SPEC, but
4621 don't set up IT. In that case, FRAME_WINDOW_P non-zero means SPEC
4622 is intended to be displayed in a window on a GUI frame.
4623
4624 Value is non-zero if something was found which replaces the display
4625 of buffer or string text. */
4626
4627 static int
4628 handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
4629 Lisp_Object overlay, struct text_pos *position,
4630 ptrdiff_t bufpos, int display_replaced_p,
4631 int frame_window_p)
4632 {
4633 Lisp_Object form;
4634 Lisp_Object location, value;
4635 struct text_pos start_pos = *position;
4636 int valid_p;
4637
4638 /* If SPEC is a list of the form `(when FORM . VALUE)', evaluate FORM.
4639 If the result is non-nil, use VALUE instead of SPEC. */
4640 form = Qt;
4641 if (CONSP (spec) && EQ (XCAR (spec), Qwhen))
4642 {
4643 spec = XCDR (spec);
4644 if (!CONSP (spec))
4645 return 0;
4646 form = XCAR (spec);
4647 spec = XCDR (spec);
4648 }
4649
4650 if (!NILP (form) && !EQ (form, Qt))
4651 {
4652 ptrdiff_t count = SPECPDL_INDEX ();
4653 struct gcpro gcpro1;
4654
4655 /* Bind `object' to the object having the `display' property, a
4656 buffer or string. Bind `position' to the position in the
4657 object where the property was found, and `buffer-position'
4658 to the current position in the buffer. */
4659
4660 if (NILP (object))
4661 XSETBUFFER (object, current_buffer);
4662 specbind (Qobject, object);
4663 specbind (Qposition, make_number (CHARPOS (*position)));
4664 specbind (Qbuffer_position, make_number (bufpos));
4665 GCPRO1 (form);
4666 form = safe_eval (form);
4667 UNGCPRO;
4668 unbind_to (count, Qnil);
4669 }
4670
4671 if (NILP (form))
4672 return 0;
4673
4674 /* Handle `(height HEIGHT)' specifications. */
4675 if (CONSP (spec)
4676 && EQ (XCAR (spec), Qheight)
4677 && CONSP (XCDR (spec)))
4678 {
4679 if (it)
4680 {
4681 if (!FRAME_WINDOW_P (it->f))
4682 return 0;
4683
4684 it->font_height = XCAR (XCDR (spec));
4685 if (!NILP (it->font_height))
4686 {
4687 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4688 int new_height = -1;
4689
4690 if (CONSP (it->font_height)
4691 && (EQ (XCAR (it->font_height), Qplus)
4692 || EQ (XCAR (it->font_height), Qminus))
4693 && CONSP (XCDR (it->font_height))
4694 && RANGED_INTEGERP (0, XCAR (XCDR (it->font_height)), INT_MAX))
4695 {
4696 /* `(+ N)' or `(- N)' where N is an integer. */
4697 int steps = XINT (XCAR (XCDR (it->font_height)));
4698 if (EQ (XCAR (it->font_height), Qplus))
4699 steps = - steps;
4700 it->face_id = smaller_face (it->f, it->face_id, steps);
4701 }
4702 else if (FUNCTIONP (it->font_height))
4703 {
4704 /* Call function with current height as argument.
4705 Value is the new height. */
4706 Lisp_Object height;
4707 height = safe_call1 (it->font_height,
4708 face->lface[LFACE_HEIGHT_INDEX]);
4709 if (NUMBERP (height))
4710 new_height = XFLOATINT (height);
4711 }
4712 else if (NUMBERP (it->font_height))
4713 {
4714 /* Value is a multiple of the canonical char height. */
4715 struct face *f;
4716
4717 f = FACE_FROM_ID (it->f,
4718 lookup_basic_face (it->f, DEFAULT_FACE_ID));
4719 new_height = (XFLOATINT (it->font_height)
4720 * XINT (f->lface[LFACE_HEIGHT_INDEX]));
4721 }
4722 else
4723 {
4724 /* Evaluate IT->font_height with `height' bound to the
4725 current specified height to get the new height. */
4726 ptrdiff_t count = SPECPDL_INDEX ();
4727
4728 specbind (Qheight, face->lface[LFACE_HEIGHT_INDEX]);
4729 value = safe_eval (it->font_height);
4730 unbind_to (count, Qnil);
4731
4732 if (NUMBERP (value))
4733 new_height = XFLOATINT (value);
4734 }
4735
4736 if (new_height > 0)
4737 it->face_id = face_with_height (it->f, it->face_id, new_height);
4738 }
4739 }
4740
4741 return 0;
4742 }
4743
4744 /* Handle `(space-width WIDTH)'. */
4745 if (CONSP (spec)
4746 && EQ (XCAR (spec), Qspace_width)
4747 && CONSP (XCDR (spec)))
4748 {
4749 if (it)
4750 {
4751 if (!FRAME_WINDOW_P (it->f))
4752 return 0;
4753
4754 value = XCAR (XCDR (spec));
4755 if (NUMBERP (value) && XFLOATINT (value) > 0)
4756 it->space_width = value;
4757 }
4758
4759 return 0;
4760 }
4761
4762 /* Handle `(slice X Y WIDTH HEIGHT)'. */
4763 if (CONSP (spec)
4764 && EQ (XCAR (spec), Qslice))
4765 {
4766 Lisp_Object tem;
4767
4768 if (it)
4769 {
4770 if (!FRAME_WINDOW_P (it->f))
4771 return 0;
4772
4773 if (tem = XCDR (spec), CONSP (tem))
4774 {
4775 it->slice.x = XCAR (tem);
4776 if (tem = XCDR (tem), CONSP (tem))
4777 {
4778 it->slice.y = XCAR (tem);
4779 if (tem = XCDR (tem), CONSP (tem))
4780 {
4781 it->slice.width = XCAR (tem);
4782 if (tem = XCDR (tem), CONSP (tem))
4783 it->slice.height = XCAR (tem);
4784 }
4785 }
4786 }
4787 }
4788
4789 return 0;
4790 }
4791
4792 /* Handle `(raise FACTOR)'. */
4793 if (CONSP (spec)
4794 && EQ (XCAR (spec), Qraise)
4795 && CONSP (XCDR (spec)))
4796 {
4797 if (it)
4798 {
4799 if (!FRAME_WINDOW_P (it->f))
4800 return 0;
4801
4802 #ifdef HAVE_WINDOW_SYSTEM
4803 value = XCAR (XCDR (spec));
4804 if (NUMBERP (value))
4805 {
4806 struct face *face = FACE_FROM_ID (it->f, it->face_id);
4807 it->voffset = - (XFLOATINT (value)
4808 * (FONT_HEIGHT (face->font)));
4809 }
4810 #endif /* HAVE_WINDOW_SYSTEM */
4811 }
4812
4813 return 0;
4814 }
4815
4816 /* Don't handle the other kinds of display specifications
4817 inside a string that we got from a `display' property. */
4818 if (it && it->string_from_display_prop_p)
4819 return 0;
4820
4821 /* Characters having this form of property are not displayed, so
4822 we have to find the end of the property. */
4823 if (it)
4824 {
4825 start_pos = *position;
4826 *position = display_prop_end (it, object, start_pos);
4827 }
4828 value = Qnil;
4829
4830 /* Stop the scan at that end position--we assume that all
4831 text properties change there. */
4832 if (it)
4833 it->stop_charpos = position->charpos;
4834
4835 /* Handle `(left-fringe BITMAP [FACE])'
4836 and `(right-fringe BITMAP [FACE])'. */
4837 if (CONSP (spec)
4838 && (EQ (XCAR (spec), Qleft_fringe)
4839 || EQ (XCAR (spec), Qright_fringe))
4840 && CONSP (XCDR (spec)))
4841 {
4842 int fringe_bitmap;
4843
4844 if (it)
4845 {
4846 if (!FRAME_WINDOW_P (it->f))
4847 /* If we return here, POSITION has been advanced
4848 across the text with this property. */
4849 {
4850 /* Synchronize the bidi iterator with POSITION. This is
4851 needed because we are not going to push the iterator
4852 on behalf of this display property, so there will be
4853 no pop_it call to do this synchronization for us. */
4854 if (it->bidi_p)
4855 {
4856 it->position = *position;
4857 iterate_out_of_display_property (it);
4858 *position = it->position;
4859 }
4860 return 1;
4861 }
4862 }
4863 else if (!frame_window_p)
4864 return 1;
4865
4866 #ifdef HAVE_WINDOW_SYSTEM
4867 value = XCAR (XCDR (spec));
4868 if (!SYMBOLP (value)
4869 || !(fringe_bitmap = lookup_fringe_bitmap (value)))
4870 /* If we return here, POSITION has been advanced
4871 across the text with this property. */
4872 {
4873 if (it && it->bidi_p)
4874 {
4875 it->position = *position;
4876 iterate_out_of_display_property (it);
4877 *position = it->position;
4878 }
4879 return 1;
4880 }
4881
4882 if (it)
4883 {
4884 int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
4885
4886 if (CONSP (XCDR (XCDR (spec))))
4887 {
4888 Lisp_Object face_name = XCAR (XCDR (XCDR (spec)));
4889 int face_id2 = lookup_derived_face (it->f, face_name,
4890 FRINGE_FACE_ID, 0);
4891 if (face_id2 >= 0)
4892 face_id = face_id2;
4893 }
4894
4895 /* Save current settings of IT so that we can restore them
4896 when we are finished with the glyph property value. */
4897 push_it (it, position);
4898
4899 it->area = TEXT_AREA;
4900 it->what = IT_IMAGE;
4901 it->image_id = -1; /* no image */
4902 it->position = start_pos;
4903 it->object = NILP (object) ? it->w->buffer : object;
4904 it->method = GET_FROM_IMAGE;
4905 it->from_overlay = Qnil;
4906 it->face_id = face_id;
4907 it->from_disp_prop_p = 1;
4908
4909 /* Say that we haven't consumed the characters with
4910 `display' property yet. The call to pop_it in
4911 set_iterator_to_next will clean this up. */
4912 *position = start_pos;
4913
4914 if (EQ (XCAR (spec), Qleft_fringe))
4915 {
4916 it->left_user_fringe_bitmap = fringe_bitmap;
4917 it->left_user_fringe_face_id = face_id;
4918 }
4919 else
4920 {
4921 it->right_user_fringe_bitmap = fringe_bitmap;
4922 it->right_user_fringe_face_id = face_id;
4923 }
4924 }
4925 #endif /* HAVE_WINDOW_SYSTEM */
4926 return 1;
4927 }
4928
4929 /* Prepare to handle `((margin left-margin) ...)',
4930 `((margin right-margin) ...)' and `((margin nil) ...)'
4931 prefixes for display specifications. */
4932 location = Qunbound;
4933 if (CONSP (spec) && CONSP (XCAR (spec)))
4934 {
4935 Lisp_Object tem;
4936
4937 value = XCDR (spec);
4938 if (CONSP (value))
4939 value = XCAR (value);
4940
4941 tem = XCAR (spec);
4942 if (EQ (XCAR (tem), Qmargin)
4943 && (tem = XCDR (tem),
4944 tem = CONSP (tem) ? XCAR (tem) : Qnil,
4945 (NILP (tem)
4946 || EQ (tem, Qleft_margin)
4947 || EQ (tem, Qright_margin))))
4948 location = tem;
4949 }
4950
4951 if (EQ (location, Qunbound))
4952 {
4953 location = Qnil;
4954 value = spec;
4955 }
4956
4957 /* After this point, VALUE is the property after any
4958 margin prefix has been stripped. It must be a string,
4959 an image specification, or `(space ...)'.
4960
4961 LOCATION specifies where to display: `left-margin',
4962 `right-margin' or nil. */
4963
4964 valid_p = (STRINGP (value)
4965 #ifdef HAVE_WINDOW_SYSTEM
4966 || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
4967 && valid_image_p (value))
4968 #endif /* not HAVE_WINDOW_SYSTEM */
4969 || (CONSP (value) && EQ (XCAR (value), Qspace))
4970 #ifdef HAVE_XWIDGETS
4971 || XWIDGETP(value)
4972 #endif
4973 );
4974
4975 if (valid_p && !display_replaced_p)
4976 {
4977 int retval = 1;
4978
4979 if (!it)
4980 {
4981 /* Callers need to know whether the display spec is any kind
4982 of `(space ...)' spec that is about to affect text-area
4983 display. */
4984 if (CONSP (value) && EQ (XCAR (value), Qspace) && NILP (location))
4985 retval = 2;
4986 return retval;
4987 }
4988
4989 /* Save current settings of IT so that we can restore them
4990 when we are finished with the glyph property value. */
4991 push_it (it, position);
4992 it->from_overlay = overlay;
4993 it->from_disp_prop_p = 1;
4994
4995 if (NILP (location))
4996 it->area = TEXT_AREA;
4997 else if (EQ (location, Qleft_margin))
4998 it->area = LEFT_MARGIN_AREA;
4999 else
5000 it->area = RIGHT_MARGIN_AREA;
5001
5002 if (STRINGP (value))
5003 {
5004 it->string = value;
5005 it->multibyte_p = STRING_MULTIBYTE (it->string);
5006 it->current.overlay_string_index = -1;
5007 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5008 it->end_charpos = it->string_nchars = SCHARS (it->string);
5009 it->method = GET_FROM_STRING;
5010 it->stop_charpos = 0;
5011 it->prev_stop = 0;
5012 it->base_level_stop = 0;
5013 it->string_from_display_prop_p = 1;
5014 /* Say that we haven't consumed the characters with
5015 `display' property yet. The call to pop_it in
5016 set_iterator_to_next will clean this up. */
5017 if (BUFFERP (object))
5018 *position = start_pos;
5019
5020 /* Force paragraph direction to be that of the parent
5021 object. If the parent object's paragraph direction is
5022 not yet determined, default to L2R. */
5023 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5024 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5025 else
5026 it->paragraph_embedding = L2R;
5027
5028 /* Set up the bidi iterator for this display string. */
5029 if (it->bidi_p)
5030 {
5031 it->bidi_it.string.lstring = it->string;
5032 it->bidi_it.string.s = NULL;
5033 it->bidi_it.string.schars = it->end_charpos;
5034 it->bidi_it.string.bufpos = bufpos;
5035 it->bidi_it.string.from_disp_str = 1;
5036 it->bidi_it.string.unibyte = !it->multibyte_p;
5037 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5038 }
5039 }
5040 else if (CONSP (value) && EQ (XCAR (value), Qspace))
5041 {
5042 it->method = GET_FROM_STRETCH;
5043 it->object = value;
5044 *position = it->position = start_pos;
5045 retval = 1 + (it->area == TEXT_AREA);
5046 }
5047 #ifdef HAVE_XWIDGETS
5048 else if (XWIDGETP(value))
5049 {
5050 //printf("handle_single_display_spec: im an xwidget!!\n");
5051 it->what = IT_XWIDGET;
5052 it->method = GET_FROM_XWIDGET;
5053 it->position = start_pos;
5054 it->object = NILP (object) ? it->w->buffer : object;
5055 *position = start_pos;
5056
5057 it->xwidget = lookup_xwidget(value);
5058 }
5059 #endif
5060 #ifdef HAVE_WINDOW_SYSTEM
5061 else
5062 {
5063 it->what = IT_IMAGE;
5064 it->image_id = lookup_image (it->f, value);
5065 it->position = start_pos;
5066 it->object = NILP (object) ? it->w->buffer : object;
5067 it->method = GET_FROM_IMAGE;
5068
5069 /* Say that we haven't consumed the characters with
5070 `display' property yet. The call to pop_it in
5071 set_iterator_to_next will clean this up. */
5072 *position = start_pos;
5073 }
5074 #endif /* HAVE_WINDOW_SYSTEM */
5075
5076 return retval;
5077 }
5078
5079 /* Invalid property or property not supported. Restore
5080 POSITION to what it was before. */
5081 *position = start_pos;
5082 return 0;
5083 }
5084
5085 /* Check if PROP is a display property value whose text should be
5086 treated as intangible. OVERLAY is the overlay from which PROP
5087 came, or nil if it came from a text property. CHARPOS and BYTEPOS
5088 specify the buffer position covered by PROP. */
5089
5090 int
5091 display_prop_intangible_p (Lisp_Object prop, Lisp_Object overlay,
5092 ptrdiff_t charpos, ptrdiff_t bytepos)
5093 {
5094 int frame_window_p = FRAME_WINDOW_P (XFRAME (selected_frame));
5095 struct text_pos position;
5096
5097 SET_TEXT_POS (position, charpos, bytepos);
5098 return handle_display_spec (NULL, prop, Qnil, overlay,
5099 &position, charpos, frame_window_p);
5100 }
5101
5102
5103 /* Return 1 if PROP is a display sub-property value containing STRING.
5104
5105 Implementation note: this and the following function are really
5106 special cases of handle_display_spec and
5107 handle_single_display_spec, and should ideally use the same code.
5108 Until they do, these two pairs must be consistent and must be
5109 modified in sync. */
5110
5111 static int
5112 single_display_spec_string_p (Lisp_Object prop, Lisp_Object string)
5113 {
5114 if (EQ (string, prop))
5115 return 1;
5116
5117 /* Skip over `when FORM'. */
5118 if (CONSP (prop) && EQ (XCAR (prop), Qwhen))
5119 {
5120 prop = XCDR (prop);
5121 if (!CONSP (prop))
5122 return 0;
5123 /* Actually, the condition following `when' should be eval'ed,
5124 like handle_single_display_spec does, and we should return
5125 zero if it evaluates to nil. However, this function is
5126 called only when the buffer was already displayed and some
5127 glyph in the glyph matrix was found to come from a display
5128 string. Therefore, the condition was already evaluated, and
5129 the result was non-nil, otherwise the display string wouldn't
5130 have been displayed and we would have never been called for
5131 this property. Thus, we can skip the evaluation and assume
5132 its result is non-nil. */
5133 prop = XCDR (prop);
5134 }
5135
5136 if (CONSP (prop))
5137 /* Skip over `margin LOCATION'. */
5138 if (EQ (XCAR (prop), Qmargin))
5139 {
5140 prop = XCDR (prop);
5141 if (!CONSP (prop))
5142 return 0;
5143
5144 prop = XCDR (prop);
5145 if (!CONSP (prop))
5146 return 0;
5147 }
5148
5149 return EQ (prop, string) || (CONSP (prop) && EQ (XCAR (prop), string));
5150 }
5151
5152
5153 /* Return 1 if STRING appears in the `display' property PROP. */
5154
5155 static int
5156 display_prop_string_p (Lisp_Object prop, Lisp_Object string)
5157 {
5158 if (CONSP (prop)
5159 && !EQ (XCAR (prop), Qwhen)
5160 && !(CONSP (XCAR (prop)) && EQ (Qmargin, XCAR (XCAR (prop)))))
5161 {
5162 /* A list of sub-properties. */
5163 while (CONSP (prop))
5164 {
5165 if (single_display_spec_string_p (XCAR (prop), string))
5166 return 1;
5167 prop = XCDR (prop);
5168 }
5169 }
5170 else if (VECTORP (prop))
5171 {
5172 /* A vector of sub-properties. */
5173 ptrdiff_t i;
5174 for (i = 0; i < ASIZE (prop); ++i)
5175 if (single_display_spec_string_p (AREF (prop, i), string))
5176 return 1;
5177 }
5178 else
5179 return single_display_spec_string_p (prop, string);
5180
5181 return 0;
5182 }
5183
5184 /* Look for STRING in overlays and text properties in the current
5185 buffer, between character positions FROM and TO (excluding TO).
5186 BACK_P non-zero means look back (in this case, TO is supposed to be
5187 less than FROM).
5188 Value is the first character position where STRING was found, or
5189 zero if it wasn't found before hitting TO.
5190
5191 This function may only use code that doesn't eval because it is
5192 called asynchronously from note_mouse_highlight. */
5193
5194 static ptrdiff_t
5195 string_buffer_position_lim (Lisp_Object string,
5196 ptrdiff_t from, ptrdiff_t to, int back_p)
5197 {
5198 Lisp_Object limit, prop, pos;
5199 int found = 0;
5200
5201 pos = make_number (max (from, BEGV));
5202
5203 if (!back_p) /* looking forward */
5204 {
5205 limit = make_number (min (to, ZV));
5206 while (!found && !EQ (pos, limit))
5207 {
5208 prop = Fget_char_property (pos, Qdisplay, Qnil);
5209 if (!NILP (prop) && display_prop_string_p (prop, string))
5210 found = 1;
5211 else
5212 pos = Fnext_single_char_property_change (pos, Qdisplay, Qnil,
5213 limit);
5214 }
5215 }
5216 else /* looking back */
5217 {
5218 limit = make_number (max (to, BEGV));
5219 while (!found && !EQ (pos, limit))
5220 {
5221 prop = Fget_char_property (pos, Qdisplay, Qnil);
5222 if (!NILP (prop) && display_prop_string_p (prop, string))
5223 found = 1;
5224 else
5225 pos = Fprevious_single_char_property_change (pos, Qdisplay, Qnil,
5226 limit);
5227 }
5228 }
5229
5230 return found ? XINT (pos) : 0;
5231 }
5232
5233 /* Determine which buffer position in current buffer STRING comes from.
5234 AROUND_CHARPOS is an approximate position where it could come from.
5235 Value is the buffer position or 0 if it couldn't be determined.
5236
5237 This function is necessary because we don't record buffer positions
5238 in glyphs generated from strings (to keep struct glyph small).
5239 This function may only use code that doesn't eval because it is
5240 called asynchronously from note_mouse_highlight. */
5241
5242 static ptrdiff_t
5243 string_buffer_position (Lisp_Object string, ptrdiff_t around_charpos)
5244 {
5245 const int MAX_DISTANCE = 1000;
5246 ptrdiff_t found = string_buffer_position_lim (string, around_charpos,
5247 around_charpos + MAX_DISTANCE,
5248 0);
5249
5250 if (!found)
5251 found = string_buffer_position_lim (string, around_charpos,
5252 around_charpos - MAX_DISTANCE, 1);
5253 return found;
5254 }
5255
5256
5257 \f
5258 /***********************************************************************
5259 `composition' property
5260 ***********************************************************************/
5261
5262 /* Set up iterator IT from `composition' property at its current
5263 position. Called from handle_stop. */
5264
5265 static enum prop_handled
5266 handle_composition_prop (struct it *it)
5267 {
5268 Lisp_Object prop, string;
5269 ptrdiff_t pos, pos_byte, start, end;
5270
5271 if (STRINGP (it->string))
5272 {
5273 unsigned char *s;
5274
5275 pos = IT_STRING_CHARPOS (*it);
5276 pos_byte = IT_STRING_BYTEPOS (*it);
5277 string = it->string;
5278 s = SDATA (string) + pos_byte;
5279 it->c = STRING_CHAR (s);
5280 }
5281 else
5282 {
5283 pos = IT_CHARPOS (*it);
5284 pos_byte = IT_BYTEPOS (*it);
5285 string = Qnil;
5286 it->c = FETCH_CHAR (pos_byte);
5287 }
5288
5289 /* If there's a valid composition and point is not inside of the
5290 composition (in the case that the composition is from the current
5291 buffer), draw a glyph composed from the composition components. */
5292 if (find_composition (pos, -1, &start, &end, &prop, string)
5293 && COMPOSITION_VALID_P (start, end, prop)
5294 && (STRINGP (it->string) || (PT <= start || PT >= end)))
5295 {
5296 if (start < pos)
5297 /* As we can't handle this situation (perhaps font-lock added
5298 a new composition), we just return here hoping that next
5299 redisplay will detect this composition much earlier. */
5300 return HANDLED_NORMALLY;
5301 if (start != pos)
5302 {
5303 if (STRINGP (it->string))
5304 pos_byte = string_char_to_byte (it->string, start);
5305 else
5306 pos_byte = CHAR_TO_BYTE (start);
5307 }
5308 it->cmp_it.id = get_composition_id (start, pos_byte, end - start,
5309 prop, string);
5310
5311 if (it->cmp_it.id >= 0)
5312 {
5313 it->cmp_it.ch = -1;
5314 it->cmp_it.nchars = COMPOSITION_LENGTH (prop);
5315 it->cmp_it.nglyphs = -1;
5316 }
5317 }
5318
5319 return HANDLED_NORMALLY;
5320 }
5321
5322
5323 \f
5324 /***********************************************************************
5325 Overlay strings
5326 ***********************************************************************/
5327
5328 /* The following structure is used to record overlay strings for
5329 later sorting in load_overlay_strings. */
5330
5331 struct overlay_entry
5332 {
5333 Lisp_Object overlay;
5334 Lisp_Object string;
5335 EMACS_INT priority;
5336 int after_string_p;
5337 };
5338
5339
5340 /* Set up iterator IT from overlay strings at its current position.
5341 Called from handle_stop. */
5342
5343 static enum prop_handled
5344 handle_overlay_change (struct it *it)
5345 {
5346 if (!STRINGP (it->string) && get_overlay_strings (it, 0))
5347 return HANDLED_RECOMPUTE_PROPS;
5348 else
5349 return HANDLED_NORMALLY;
5350 }
5351
5352
5353 /* Set up the next overlay string for delivery by IT, if there is an
5354 overlay string to deliver. Called by set_iterator_to_next when the
5355 end of the current overlay string is reached. If there are more
5356 overlay strings to display, IT->string and
5357 IT->current.overlay_string_index are set appropriately here.
5358 Otherwise IT->string is set to nil. */
5359
5360 static void
5361 next_overlay_string (struct it *it)
5362 {
5363 ++it->current.overlay_string_index;
5364 if (it->current.overlay_string_index == it->n_overlay_strings)
5365 {
5366 /* No more overlay strings. Restore IT's settings to what
5367 they were before overlay strings were processed, and
5368 continue to deliver from current_buffer. */
5369
5370 it->ellipsis_p = (it->stack[it->sp - 1].display_ellipsis_p != 0);
5371 pop_it (it);
5372 eassert (it->sp > 0
5373 || (NILP (it->string)
5374 && it->method == GET_FROM_BUFFER
5375 && it->stop_charpos >= BEGV
5376 && it->stop_charpos <= it->end_charpos));
5377 it->current.overlay_string_index = -1;
5378 it->n_overlay_strings = 0;
5379 it->overlay_strings_charpos = -1;
5380 /* If there's an empty display string on the stack, pop the
5381 stack, to resync the bidi iterator with IT's position. Such
5382 empty strings are pushed onto the stack in
5383 get_overlay_strings_1. */
5384 if (it->sp > 0 && STRINGP (it->string) && !SCHARS (it->string))
5385 pop_it (it);
5386
5387 /* If we're at the end of the buffer, record that we have
5388 processed the overlay strings there already, so that
5389 next_element_from_buffer doesn't try it again. */
5390 if (NILP (it->string) && IT_CHARPOS (*it) >= it->end_charpos)
5391 it->overlay_strings_at_end_processed_p = 1;
5392 }
5393 else
5394 {
5395 /* There are more overlay strings to process. If
5396 IT->current.overlay_string_index has advanced to a position
5397 where we must load IT->overlay_strings with more strings, do
5398 it. We must load at the IT->overlay_strings_charpos where
5399 IT->n_overlay_strings was originally computed; when invisible
5400 text is present, this might not be IT_CHARPOS (Bug#7016). */
5401 int i = it->current.overlay_string_index % OVERLAY_STRING_CHUNK_SIZE;
5402
5403 if (it->current.overlay_string_index && i == 0)
5404 load_overlay_strings (it, it->overlay_strings_charpos);
5405
5406 /* Initialize IT to deliver display elements from the overlay
5407 string. */
5408 it->string = it->overlay_strings[i];
5409 it->multibyte_p = STRING_MULTIBYTE (it->string);
5410 SET_TEXT_POS (it->current.string_pos, 0, 0);
5411 it->method = GET_FROM_STRING;
5412 it->stop_charpos = 0;
5413 it->end_charpos = SCHARS (it->string);
5414 if (it->cmp_it.stop_pos >= 0)
5415 it->cmp_it.stop_pos = 0;
5416 it->prev_stop = 0;
5417 it->base_level_stop = 0;
5418
5419 /* Set up the bidi iterator for this overlay string. */
5420 if (it->bidi_p)
5421 {
5422 it->bidi_it.string.lstring = it->string;
5423 it->bidi_it.string.s = NULL;
5424 it->bidi_it.string.schars = SCHARS (it->string);
5425 it->bidi_it.string.bufpos = it->overlay_strings_charpos;
5426 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5427 it->bidi_it.string.unibyte = !it->multibyte_p;
5428 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5429 }
5430 }
5431
5432 CHECK_IT (it);
5433 }
5434
5435
5436 /* Compare two overlay_entry structures E1 and E2. Used as a
5437 comparison function for qsort in load_overlay_strings. Overlay
5438 strings for the same position are sorted so that
5439
5440 1. All after-strings come in front of before-strings, except
5441 when they come from the same overlay.
5442
5443 2. Within after-strings, strings are sorted so that overlay strings
5444 from overlays with higher priorities come first.
5445
5446 2. Within before-strings, strings are sorted so that overlay
5447 strings from overlays with higher priorities come last.
5448
5449 Value is analogous to strcmp. */
5450
5451
5452 static int
5453 compare_overlay_entries (const void *e1, const void *e2)
5454 {
5455 struct overlay_entry *entry1 = (struct overlay_entry *) e1;
5456 struct overlay_entry *entry2 = (struct overlay_entry *) e2;
5457 int result;
5458
5459 if (entry1->after_string_p != entry2->after_string_p)
5460 {
5461 /* Let after-strings appear in front of before-strings if
5462 they come from different overlays. */
5463 if (EQ (entry1->overlay, entry2->overlay))
5464 result = entry1->after_string_p ? 1 : -1;
5465 else
5466 result = entry1->after_string_p ? -1 : 1;
5467 }
5468 else if (entry1->priority != entry2->priority)
5469 {
5470 if (entry1->after_string_p)
5471 /* After-strings sorted in order of decreasing priority. */
5472 result = entry2->priority < entry1->priority ? -1 : 1;
5473 else
5474 /* Before-strings sorted in order of increasing priority. */
5475 result = entry1->priority < entry2->priority ? -1 : 1;
5476 }
5477 else
5478 result = 0;
5479
5480 return result;
5481 }
5482
5483
5484 /* Load the vector IT->overlay_strings with overlay strings from IT's
5485 current buffer position, or from CHARPOS if that is > 0. Set
5486 IT->n_overlays to the total number of overlay strings found.
5487
5488 Overlay strings are processed OVERLAY_STRING_CHUNK_SIZE strings at
5489 a time. On entry into load_overlay_strings,
5490 IT->current.overlay_string_index gives the number of overlay
5491 strings that have already been loaded by previous calls to this
5492 function.
5493
5494 IT->add_overlay_start contains an additional overlay start
5495 position to consider for taking overlay strings from, if non-zero.
5496 This position comes into play when the overlay has an `invisible'
5497 property, and both before and after-strings. When we've skipped to
5498 the end of the overlay, because of its `invisible' property, we
5499 nevertheless want its before-string to appear.
5500 IT->add_overlay_start will contain the overlay start position
5501 in this case.
5502
5503 Overlay strings are sorted so that after-string strings come in
5504 front of before-string strings. Within before and after-strings,
5505 strings are sorted by overlay priority. See also function
5506 compare_overlay_entries. */
5507
5508 static void
5509 load_overlay_strings (struct it *it, ptrdiff_t charpos)
5510 {
5511 Lisp_Object overlay, window, str, invisible;
5512 struct Lisp_Overlay *ov;
5513 ptrdiff_t start, end;
5514 ptrdiff_t size = 20;
5515 ptrdiff_t n = 0, i, j;
5516 int invis_p;
5517 struct overlay_entry *entries = alloca (size * sizeof *entries);
5518 USE_SAFE_ALLOCA;
5519
5520 if (charpos <= 0)
5521 charpos = IT_CHARPOS (*it);
5522
5523 /* Append the overlay string STRING of overlay OVERLAY to vector
5524 `entries' which has size `size' and currently contains `n'
5525 elements. AFTER_P non-zero means STRING is an after-string of
5526 OVERLAY. */
5527 #define RECORD_OVERLAY_STRING(OVERLAY, STRING, AFTER_P) \
5528 do \
5529 { \
5530 Lisp_Object priority; \
5531 \
5532 if (n == size) \
5533 { \
5534 struct overlay_entry *old = entries; \
5535 SAFE_NALLOCA (entries, 2, size); \
5536 memcpy (entries, old, size * sizeof *entries); \
5537 size *= 2; \
5538 } \
5539 \
5540 entries[n].string = (STRING); \
5541 entries[n].overlay = (OVERLAY); \
5542 priority = Foverlay_get ((OVERLAY), Qpriority); \
5543 entries[n].priority = INTEGERP (priority) ? XINT (priority) : 0; \
5544 entries[n].after_string_p = (AFTER_P); \
5545 ++n; \
5546 } \
5547 while (0)
5548
5549 /* Process overlay before the overlay center. */
5550 for (ov = current_buffer->overlays_before; ov; ov = ov->next)
5551 {
5552 XSETMISC (overlay, ov);
5553 eassert (OVERLAYP (overlay));
5554 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5555 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5556
5557 if (end < charpos)
5558 break;
5559
5560 /* Skip this overlay if it doesn't start or end at IT's current
5561 position. */
5562 if (end != charpos && start != charpos)
5563 continue;
5564
5565 /* Skip this overlay if it doesn't apply to IT->w. */
5566 window = Foverlay_get (overlay, Qwindow);
5567 if (WINDOWP (window) && XWINDOW (window) != it->w)
5568 continue;
5569
5570 /* If the text ``under'' the overlay is invisible, both before-
5571 and after-strings from this overlay are visible; start and
5572 end position are indistinguishable. */
5573 invisible = Foverlay_get (overlay, Qinvisible);
5574 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5575
5576 /* If overlay has a non-empty before-string, record it. */
5577 if ((start == charpos || (end == charpos && invis_p))
5578 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5579 && SCHARS (str))
5580 RECORD_OVERLAY_STRING (overlay, str, 0);
5581
5582 /* If overlay has a non-empty after-string, record it. */
5583 if ((end == charpos || (start == charpos && invis_p))
5584 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5585 && SCHARS (str))
5586 RECORD_OVERLAY_STRING (overlay, str, 1);
5587 }
5588
5589 /* Process overlays after the overlay center. */
5590 for (ov = current_buffer->overlays_after; ov; ov = ov->next)
5591 {
5592 XSETMISC (overlay, ov);
5593 eassert (OVERLAYP (overlay));
5594 start = OVERLAY_POSITION (OVERLAY_START (overlay));
5595 end = OVERLAY_POSITION (OVERLAY_END (overlay));
5596
5597 if (start > charpos)
5598 break;
5599
5600 /* Skip this overlay if it doesn't start or end at IT's current
5601 position. */
5602 if (end != charpos && start != charpos)
5603 continue;
5604
5605 /* Skip this overlay if it doesn't apply to IT->w. */
5606 window = Foverlay_get (overlay, Qwindow);
5607 if (WINDOWP (window) && XWINDOW (window) != it->w)
5608 continue;
5609
5610 /* If the text ``under'' the overlay is invisible, it has a zero
5611 dimension, and both before- and after-strings apply. */
5612 invisible = Foverlay_get (overlay, Qinvisible);
5613 invis_p = TEXT_PROP_MEANS_INVISIBLE (invisible);
5614
5615 /* If overlay has a non-empty before-string, record it. */
5616 if ((start == charpos || (end == charpos && invis_p))
5617 && (str = Foverlay_get (overlay, Qbefore_string), STRINGP (str))
5618 && SCHARS (str))
5619 RECORD_OVERLAY_STRING (overlay, str, 0);
5620
5621 /* If overlay has a non-empty after-string, record it. */
5622 if ((end == charpos || (start == charpos && invis_p))
5623 && (str = Foverlay_get (overlay, Qafter_string), STRINGP (str))
5624 && SCHARS (str))
5625 RECORD_OVERLAY_STRING (overlay, str, 1);
5626 }
5627
5628 #undef RECORD_OVERLAY_STRING
5629
5630 /* Sort entries. */
5631 if (n > 1)
5632 qsort (entries, n, sizeof *entries, compare_overlay_entries);
5633
5634 /* Record number of overlay strings, and where we computed it. */
5635 it->n_overlay_strings = n;
5636 it->overlay_strings_charpos = charpos;
5637
5638 /* IT->current.overlay_string_index is the number of overlay strings
5639 that have already been consumed by IT. Copy some of the
5640 remaining overlay strings to IT->overlay_strings. */
5641 i = 0;
5642 j = it->current.overlay_string_index;
5643 while (i < OVERLAY_STRING_CHUNK_SIZE && j < n)
5644 {
5645 it->overlay_strings[i] = entries[j].string;
5646 it->string_overlays[i++] = entries[j++].overlay;
5647 }
5648
5649 CHECK_IT (it);
5650 SAFE_FREE ();
5651 }
5652
5653
5654 /* Get the first chunk of overlay strings at IT's current buffer
5655 position, or at CHARPOS if that is > 0. Value is non-zero if at
5656 least one overlay string was found. */
5657
5658 static int
5659 get_overlay_strings_1 (struct it *it, ptrdiff_t charpos, int compute_stop_p)
5660 {
5661 /* Get the first OVERLAY_STRING_CHUNK_SIZE overlay strings to
5662 process. This fills IT->overlay_strings with strings, and sets
5663 IT->n_overlay_strings to the total number of strings to process.
5664 IT->pos.overlay_string_index has to be set temporarily to zero
5665 because load_overlay_strings needs this; it must be set to -1
5666 when no overlay strings are found because a zero value would
5667 indicate a position in the first overlay string. */
5668 it->current.overlay_string_index = 0;
5669 load_overlay_strings (it, charpos);
5670
5671 /* If we found overlay strings, set up IT to deliver display
5672 elements from the first one. Otherwise set up IT to deliver
5673 from current_buffer. */
5674 if (it->n_overlay_strings)
5675 {
5676 /* Make sure we know settings in current_buffer, so that we can
5677 restore meaningful values when we're done with the overlay
5678 strings. */
5679 if (compute_stop_p)
5680 compute_stop_pos (it);
5681 eassert (it->face_id >= 0);
5682
5683 /* Save IT's settings. They are restored after all overlay
5684 strings have been processed. */
5685 eassert (!compute_stop_p || it->sp == 0);
5686
5687 /* When called from handle_stop, there might be an empty display
5688 string loaded. In that case, don't bother saving it. But
5689 don't use this optimization with the bidi iterator, since we
5690 need the corresponding pop_it call to resync the bidi
5691 iterator's position with IT's position, after we are done
5692 with the overlay strings. (The corresponding call to pop_it
5693 in case of an empty display string is in
5694 next_overlay_string.) */
5695 if (!(!it->bidi_p
5696 && STRINGP (it->string) && !SCHARS (it->string)))
5697 push_it (it, NULL);
5698
5699 /* Set up IT to deliver display elements from the first overlay
5700 string. */
5701 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
5702 it->string = it->overlay_strings[0];
5703 it->from_overlay = Qnil;
5704 it->stop_charpos = 0;
5705 eassert (STRINGP (it->string));
5706 it->end_charpos = SCHARS (it->string);
5707 it->prev_stop = 0;
5708 it->base_level_stop = 0;
5709 it->multibyte_p = STRING_MULTIBYTE (it->string);
5710 it->method = GET_FROM_STRING;
5711 it->from_disp_prop_p = 0;
5712
5713 /* Force paragraph direction to be that of the parent
5714 buffer. */
5715 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
5716 it->paragraph_embedding = it->bidi_it.paragraph_dir;
5717 else
5718 it->paragraph_embedding = L2R;
5719
5720 /* Set up the bidi iterator for this overlay string. */
5721 if (it->bidi_p)
5722 {
5723 ptrdiff_t pos = (charpos > 0 ? charpos : IT_CHARPOS (*it));
5724
5725 it->bidi_it.string.lstring = it->string;
5726 it->bidi_it.string.s = NULL;
5727 it->bidi_it.string.schars = SCHARS (it->string);
5728 it->bidi_it.string.bufpos = pos;
5729 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
5730 it->bidi_it.string.unibyte = !it->multibyte_p;
5731 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
5732 }
5733 return 1;
5734 }
5735
5736 it->current.overlay_string_index = -1;
5737 return 0;
5738 }
5739
5740 static int
5741 get_overlay_strings (struct it *it, ptrdiff_t charpos)
5742 {
5743 it->string = Qnil;
5744 it->method = GET_FROM_BUFFER;
5745
5746 (void) get_overlay_strings_1 (it, charpos, 1);
5747
5748 CHECK_IT (it);
5749
5750 /* Value is non-zero if we found at least one overlay string. */
5751 return STRINGP (it->string);
5752 }
5753
5754
5755 \f
5756 /***********************************************************************
5757 Saving and restoring state
5758 ***********************************************************************/
5759
5760 /* Save current settings of IT on IT->stack. Called, for example,
5761 before setting up IT for an overlay string, to be able to restore
5762 IT's settings to what they were after the overlay string has been
5763 processed. If POSITION is non-NULL, it is the position to save on
5764 the stack instead of IT->position. */
5765
5766 static void
5767 push_it (struct it *it, struct text_pos *position)
5768 {
5769 struct iterator_stack_entry *p;
5770
5771 eassert (it->sp < IT_STACK_SIZE);
5772 p = it->stack + it->sp;
5773
5774 p->stop_charpos = it->stop_charpos;
5775 p->prev_stop = it->prev_stop;
5776 p->base_level_stop = it->base_level_stop;
5777 p->cmp_it = it->cmp_it;
5778 eassert (it->face_id >= 0);
5779 p->face_id = it->face_id;
5780 p->string = it->string;
5781 p->method = it->method;
5782 p->from_overlay = it->from_overlay;
5783 switch (p->method)
5784 {
5785 case GET_FROM_IMAGE:
5786 p->u.image.object = it->object;
5787 p->u.image.image_id = it->image_id;
5788 p->u.image.slice = it->slice;
5789 break;
5790 case GET_FROM_STRETCH:
5791 p->u.stretch.object = it->object;
5792 break;
5793 #ifdef HAVE_XWIDGETS
5794 case GET_FROM_XWIDGET:
5795 p->u.xwidget.object = it->object;
5796 break;
5797 #endif
5798 }
5799 p->position = position ? *position : it->position;
5800 p->current = it->current;
5801 p->end_charpos = it->end_charpos;
5802 p->string_nchars = it->string_nchars;
5803 p->area = it->area;
5804 p->multibyte_p = it->multibyte_p;
5805 p->avoid_cursor_p = it->avoid_cursor_p;
5806 p->space_width = it->space_width;
5807 p->font_height = it->font_height;
5808 p->voffset = it->voffset;
5809 p->string_from_display_prop_p = it->string_from_display_prop_p;
5810 p->string_from_prefix_prop_p = it->string_from_prefix_prop_p;
5811 p->display_ellipsis_p = 0;
5812 p->line_wrap = it->line_wrap;
5813 p->bidi_p = it->bidi_p;
5814 p->paragraph_embedding = it->paragraph_embedding;
5815 p->from_disp_prop_p = it->from_disp_prop_p;
5816 ++it->sp;
5817
5818 /* Save the state of the bidi iterator as well. */
5819 if (it->bidi_p)
5820 bidi_push_it (&it->bidi_it);
5821 }
5822
5823 static void
5824 iterate_out_of_display_property (struct it *it)
5825 {
5826 int buffer_p = !STRINGP (it->string);
5827 ptrdiff_t eob = (buffer_p ? ZV : it->end_charpos);
5828 ptrdiff_t bob = (buffer_p ? BEGV : 0);
5829
5830 eassert (eob >= CHARPOS (it->position) && CHARPOS (it->position) >= bob);
5831
5832 /* Maybe initialize paragraph direction. If we are at the beginning
5833 of a new paragraph, next_element_from_buffer may not have a
5834 chance to do that. */
5835 if (it->bidi_it.first_elt && it->bidi_it.charpos < eob)
5836 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
5837 /* prev_stop can be zero, so check against BEGV as well. */
5838 while (it->bidi_it.charpos >= bob
5839 && it->prev_stop <= it->bidi_it.charpos
5840 && it->bidi_it.charpos < CHARPOS (it->position)
5841 && it->bidi_it.charpos < eob)
5842 bidi_move_to_visually_next (&it->bidi_it);
5843 /* Record the stop_pos we just crossed, for when we cross it
5844 back, maybe. */
5845 if (it->bidi_it.charpos > CHARPOS (it->position))
5846 it->prev_stop = CHARPOS (it->position);
5847 /* If we ended up not where pop_it put us, resync IT's
5848 positional members with the bidi iterator. */
5849 if (it->bidi_it.charpos != CHARPOS (it->position))
5850 SET_TEXT_POS (it->position, it->bidi_it.charpos, it->bidi_it.bytepos);
5851 if (buffer_p)
5852 it->current.pos = it->position;
5853 else
5854 it->current.string_pos = it->position;
5855 }
5856
5857 /* Restore IT's settings from IT->stack. Called, for example, when no
5858 more overlay strings must be processed, and we return to delivering
5859 display elements from a buffer, or when the end of a string from a
5860 `display' property is reached and we return to delivering display
5861 elements from an overlay string, or from a buffer. */
5862
5863 static void
5864 pop_it (struct it *it)
5865 {
5866 struct iterator_stack_entry *p;
5867 int from_display_prop = it->from_disp_prop_p;
5868
5869 eassert (it->sp > 0);
5870 --it->sp;
5871 p = it->stack + it->sp;
5872 it->stop_charpos = p->stop_charpos;
5873 it->prev_stop = p->prev_stop;
5874 it->base_level_stop = p->base_level_stop;
5875 it->cmp_it = p->cmp_it;
5876 it->face_id = p->face_id;
5877 it->current = p->current;
5878 it->position = p->position;
5879 it->string = p->string;
5880 it->from_overlay = p->from_overlay;
5881 if (NILP (it->string))
5882 SET_TEXT_POS (it->current.string_pos, -1, -1);
5883 it->method = p->method;
5884 switch (it->method)
5885 {
5886 case GET_FROM_IMAGE:
5887 it->image_id = p->u.image.image_id;
5888 it->object = p->u.image.object;
5889 it->slice = p->u.image.slice;
5890 break;
5891 #ifdef HAVE_XWIDGETS
5892 case GET_FROM_XWIDGET:
5893 it->object = p->u.xwidget.object;
5894 break;
5895 #endif
5896 case GET_FROM_STRETCH:
5897 it->object = p->u.stretch.object;
5898 break;
5899 case GET_FROM_BUFFER:
5900 it->object = it->w->buffer;
5901 break;
5902 case GET_FROM_STRING:
5903 it->object = it->string;
5904 break;
5905 case GET_FROM_DISPLAY_VECTOR:
5906 if (it->s)
5907 it->method = GET_FROM_C_STRING;
5908 else if (STRINGP (it->string))
5909 it->method = GET_FROM_STRING;
5910 else
5911 {
5912 it->method = GET_FROM_BUFFER;
5913 it->object = it->w->buffer;
5914 }
5915 }
5916 it->end_charpos = p->end_charpos;
5917 it->string_nchars = p->string_nchars;
5918 it->area = p->area;
5919 it->multibyte_p = p->multibyte_p;
5920 it->avoid_cursor_p = p->avoid_cursor_p;
5921 it->space_width = p->space_width;
5922 it->font_height = p->font_height;
5923 it->voffset = p->voffset;
5924 it->string_from_display_prop_p = p->string_from_display_prop_p;
5925 it->string_from_prefix_prop_p = p->string_from_prefix_prop_p;
5926 it->line_wrap = p->line_wrap;
5927 it->bidi_p = p->bidi_p;
5928 it->paragraph_embedding = p->paragraph_embedding;
5929 it->from_disp_prop_p = p->from_disp_prop_p;
5930 if (it->bidi_p)
5931 {
5932 bidi_pop_it (&it->bidi_it);
5933 /* Bidi-iterate until we get out of the portion of text, if any,
5934 covered by a `display' text property or by an overlay with
5935 `display' property. (We cannot just jump there, because the
5936 internal coherency of the bidi iterator state can not be
5937 preserved across such jumps.) We also must determine the
5938 paragraph base direction if the overlay we just processed is
5939 at the beginning of a new paragraph. */
5940 if (from_display_prop
5941 && (it->method == GET_FROM_BUFFER || it->method == GET_FROM_STRING))
5942 iterate_out_of_display_property (it);
5943
5944 eassert ((BUFFERP (it->object)
5945 && IT_CHARPOS (*it) == it->bidi_it.charpos
5946 && IT_BYTEPOS (*it) == it->bidi_it.bytepos)
5947 || (STRINGP (it->object)
5948 && IT_STRING_CHARPOS (*it) == it->bidi_it.charpos
5949 && IT_STRING_BYTEPOS (*it) == it->bidi_it.bytepos)
5950 || (CONSP (it->object) && it->method == GET_FROM_STRETCH));
5951 }
5952 }
5953
5954
5955 \f
5956 /***********************************************************************
5957 Moving over lines
5958 ***********************************************************************/
5959
5960 /* Set IT's current position to the previous line start. */
5961
5962 static void
5963 back_to_previous_line_start (struct it *it)
5964 {
5965 IT_CHARPOS (*it) = find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
5966 IT_BYTEPOS (*it) = CHAR_TO_BYTE (IT_CHARPOS (*it));
5967 }
5968
5969
5970 /* Move IT to the next line start.
5971
5972 Value is non-zero if a newline was found. Set *SKIPPED_P to 1 if
5973 we skipped over part of the text (as opposed to moving the iterator
5974 continuously over the text). Otherwise, don't change the value
5975 of *SKIPPED_P.
5976
5977 If BIDI_IT_PREV is non-NULL, store into it the state of the bidi
5978 iterator on the newline, if it was found.
5979
5980 Newlines may come from buffer text, overlay strings, or strings
5981 displayed via the `display' property. That's the reason we can't
5982 simply use find_next_newline_no_quit.
5983
5984 Note that this function may not skip over invisible text that is so
5985 because of text properties and immediately follows a newline. If
5986 it would, function reseat_at_next_visible_line_start, when called
5987 from set_iterator_to_next, would effectively make invisible
5988 characters following a newline part of the wrong glyph row, which
5989 leads to wrong cursor motion. */
5990
5991 static int
5992 forward_to_next_line_start (struct it *it, int *skipped_p,
5993 struct bidi_it *bidi_it_prev)
5994 {
5995 ptrdiff_t old_selective;
5996 int newline_found_p, n;
5997 const int MAX_NEWLINE_DISTANCE = 500;
5998
5999 /* If already on a newline, just consume it to avoid unintended
6000 skipping over invisible text below. */
6001 if (it->what == IT_CHARACTER
6002 && it->c == '\n'
6003 && CHARPOS (it->position) == IT_CHARPOS (*it))
6004 {
6005 if (it->bidi_p && bidi_it_prev)
6006 *bidi_it_prev = it->bidi_it;
6007 set_iterator_to_next (it, 0);
6008 it->c = 0;
6009 return 1;
6010 }
6011
6012 /* Don't handle selective display in the following. It's (a)
6013 unnecessary because it's done by the caller, and (b) leads to an
6014 infinite recursion because next_element_from_ellipsis indirectly
6015 calls this function. */
6016 old_selective = it->selective;
6017 it->selective = 0;
6018
6019 /* Scan for a newline within MAX_NEWLINE_DISTANCE display elements
6020 from buffer text. */
6021 for (n = newline_found_p = 0;
6022 !newline_found_p && n < MAX_NEWLINE_DISTANCE;
6023 n += STRINGP (it->string) ? 0 : 1)
6024 {
6025 if (!get_next_display_element (it))
6026 return 0;
6027 newline_found_p = it->what == IT_CHARACTER && it->c == '\n';
6028 if (newline_found_p && it->bidi_p && bidi_it_prev)
6029 *bidi_it_prev = it->bidi_it;
6030 set_iterator_to_next (it, 0);
6031 }
6032
6033 /* If we didn't find a newline near enough, see if we can use a
6034 short-cut. */
6035 if (!newline_found_p)
6036 {
6037 ptrdiff_t start = IT_CHARPOS (*it);
6038 ptrdiff_t limit = find_next_newline_no_quit (start, 1);
6039 Lisp_Object pos;
6040
6041 eassert (!STRINGP (it->string));
6042
6043 /* If there isn't any `display' property in sight, and no
6044 overlays, we can just use the position of the newline in
6045 buffer text. */
6046 if (it->stop_charpos >= limit
6047 || ((pos = Fnext_single_property_change (make_number (start),
6048 Qdisplay, Qnil,
6049 make_number (limit)),
6050 NILP (pos))
6051 && next_overlay_change (start) == ZV))
6052 {
6053 if (!it->bidi_p)
6054 {
6055 IT_CHARPOS (*it) = limit;
6056 IT_BYTEPOS (*it) = CHAR_TO_BYTE (limit);
6057 }
6058 else
6059 {
6060 struct bidi_it bprev;
6061
6062 /* Help bidi.c avoid expensive searches for display
6063 properties and overlays, by telling it that there are
6064 none up to `limit'. */
6065 if (it->bidi_it.disp_pos < limit)
6066 {
6067 it->bidi_it.disp_pos = limit;
6068 it->bidi_it.disp_prop = 0;
6069 }
6070 do {
6071 bprev = it->bidi_it;
6072 bidi_move_to_visually_next (&it->bidi_it);
6073 } while (it->bidi_it.charpos != limit);
6074 IT_CHARPOS (*it) = limit;
6075 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6076 if (bidi_it_prev)
6077 *bidi_it_prev = bprev;
6078 }
6079 *skipped_p = newline_found_p = 1;
6080 }
6081 else
6082 {
6083 while (get_next_display_element (it)
6084 && !newline_found_p)
6085 {
6086 newline_found_p = ITERATOR_AT_END_OF_LINE_P (it);
6087 if (newline_found_p && it->bidi_p && bidi_it_prev)
6088 *bidi_it_prev = it->bidi_it;
6089 set_iterator_to_next (it, 0);
6090 }
6091 }
6092 }
6093
6094 it->selective = old_selective;
6095 return newline_found_p;
6096 }
6097
6098
6099 /* Set IT's current position to the previous visible line start. Skip
6100 invisible text that is so either due to text properties or due to
6101 selective display. Caution: this does not change IT->current_x and
6102 IT->hpos. */
6103
6104 static void
6105 back_to_previous_visible_line_start (struct it *it)
6106 {
6107 while (IT_CHARPOS (*it) > BEGV)
6108 {
6109 back_to_previous_line_start (it);
6110
6111 if (IT_CHARPOS (*it) <= BEGV)
6112 break;
6113
6114 /* If selective > 0, then lines indented more than its value are
6115 invisible. */
6116 if (it->selective > 0
6117 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6118 it->selective))
6119 continue;
6120
6121 /* Check the newline before point for invisibility. */
6122 {
6123 Lisp_Object prop;
6124 prop = Fget_char_property (make_number (IT_CHARPOS (*it) - 1),
6125 Qinvisible, it->window);
6126 if (TEXT_PROP_MEANS_INVISIBLE (prop))
6127 continue;
6128 }
6129
6130 if (IT_CHARPOS (*it) <= BEGV)
6131 break;
6132
6133 {
6134 struct it it2;
6135 void *it2data = NULL;
6136 ptrdiff_t pos;
6137 ptrdiff_t beg, end;
6138 Lisp_Object val, overlay;
6139
6140 SAVE_IT (it2, *it, it2data);
6141
6142 /* If newline is part of a composition, continue from start of composition */
6143 if (find_composition (IT_CHARPOS (*it), -1, &beg, &end, &val, Qnil)
6144 && beg < IT_CHARPOS (*it))
6145 goto replaced;
6146
6147 /* If newline is replaced by a display property, find start of overlay
6148 or interval and continue search from that point. */
6149 pos = --IT_CHARPOS (it2);
6150 --IT_BYTEPOS (it2);
6151 it2.sp = 0;
6152 bidi_unshelve_cache (NULL, 0);
6153 it2.string_from_display_prop_p = 0;
6154 it2.from_disp_prop_p = 0;
6155 if (handle_display_prop (&it2) == HANDLED_RETURN
6156 && !NILP (val = get_char_property_and_overlay
6157 (make_number (pos), Qdisplay, Qnil, &overlay))
6158 && (OVERLAYP (overlay)
6159 ? (beg = OVERLAY_POSITION (OVERLAY_START (overlay)))
6160 : get_property_and_range (pos, Qdisplay, &val, &beg, &end, Qnil)))
6161 {
6162 RESTORE_IT (it, it, it2data);
6163 goto replaced;
6164 }
6165
6166 /* Newline is not replaced by anything -- so we are done. */
6167 RESTORE_IT (it, it, it2data);
6168 break;
6169
6170 replaced:
6171 if (beg < BEGV)
6172 beg = BEGV;
6173 IT_CHARPOS (*it) = beg;
6174 IT_BYTEPOS (*it) = buf_charpos_to_bytepos (current_buffer, beg);
6175 }
6176 }
6177
6178 it->continuation_lines_width = 0;
6179
6180 eassert (IT_CHARPOS (*it) >= BEGV);
6181 eassert (IT_CHARPOS (*it) == BEGV
6182 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6183 CHECK_IT (it);
6184 }
6185
6186
6187 /* Reseat iterator IT at the previous visible line start. Skip
6188 invisible text that is so either due to text properties or due to
6189 selective display. At the end, update IT's overlay information,
6190 face information etc. */
6191
6192 void
6193 reseat_at_previous_visible_line_start (struct it *it)
6194 {
6195 back_to_previous_visible_line_start (it);
6196 reseat (it, it->current.pos, 1);
6197 CHECK_IT (it);
6198 }
6199
6200
6201 /* Reseat iterator IT on the next visible line start in the current
6202 buffer. ON_NEWLINE_P non-zero means position IT on the newline
6203 preceding the line start. Skip over invisible text that is so
6204 because of selective display. Compute faces, overlays etc at the
6205 new position. Note that this function does not skip over text that
6206 is invisible because of text properties. */
6207
6208 static void
6209 reseat_at_next_visible_line_start (struct it *it, int on_newline_p)
6210 {
6211 int newline_found_p, skipped_p = 0;
6212 struct bidi_it bidi_it_prev;
6213
6214 newline_found_p = forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6215
6216 /* Skip over lines that are invisible because they are indented
6217 more than the value of IT->selective. */
6218 if (it->selective > 0)
6219 while (IT_CHARPOS (*it) < ZV
6220 && indented_beyond_p (IT_CHARPOS (*it), IT_BYTEPOS (*it),
6221 it->selective))
6222 {
6223 eassert (IT_BYTEPOS (*it) == BEGV
6224 || FETCH_BYTE (IT_BYTEPOS (*it) - 1) == '\n');
6225 newline_found_p =
6226 forward_to_next_line_start (it, &skipped_p, &bidi_it_prev);
6227 }
6228
6229 /* Position on the newline if that's what's requested. */
6230 if (on_newline_p && newline_found_p)
6231 {
6232 if (STRINGP (it->string))
6233 {
6234 if (IT_STRING_CHARPOS (*it) > 0)
6235 {
6236 if (!it->bidi_p)
6237 {
6238 --IT_STRING_CHARPOS (*it);
6239 --IT_STRING_BYTEPOS (*it);
6240 }
6241 else
6242 {
6243 /* We need to restore the bidi iterator to the state
6244 it had on the newline, and resync the IT's
6245 position with that. */
6246 it->bidi_it = bidi_it_prev;
6247 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
6248 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
6249 }
6250 }
6251 }
6252 else if (IT_CHARPOS (*it) > BEGV)
6253 {
6254 if (!it->bidi_p)
6255 {
6256 --IT_CHARPOS (*it);
6257 --IT_BYTEPOS (*it);
6258 }
6259 else
6260 {
6261 /* We need to restore the bidi iterator to the state it
6262 had on the newline and resync IT with that. */
6263 it->bidi_it = bidi_it_prev;
6264 IT_CHARPOS (*it) = it->bidi_it.charpos;
6265 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
6266 }
6267 reseat (it, it->current.pos, 0);
6268 }
6269 }
6270 else if (skipped_p)
6271 reseat (it, it->current.pos, 0);
6272
6273 CHECK_IT (it);
6274 }
6275
6276
6277 \f
6278 /***********************************************************************
6279 Changing an iterator's position
6280 ***********************************************************************/
6281
6282 /* Change IT's current position to POS in current_buffer. If FORCE_P
6283 is non-zero, always check for text properties at the new position.
6284 Otherwise, text properties are only looked up if POS >=
6285 IT->check_charpos of a property. */
6286
6287 static void
6288 reseat (struct it *it, struct text_pos pos, int force_p)
6289 {
6290 ptrdiff_t original_pos = IT_CHARPOS (*it);
6291
6292 reseat_1 (it, pos, 0);
6293
6294 /* Determine where to check text properties. Avoid doing it
6295 where possible because text property lookup is very expensive. */
6296 if (force_p
6297 || CHARPOS (pos) > it->stop_charpos
6298 || CHARPOS (pos) < original_pos)
6299 {
6300 if (it->bidi_p)
6301 {
6302 /* For bidi iteration, we need to prime prev_stop and
6303 base_level_stop with our best estimations. */
6304 /* Implementation note: Of course, POS is not necessarily a
6305 stop position, so assigning prev_pos to it is a lie; we
6306 should have called compute_stop_backwards. However, if
6307 the current buffer does not include any R2L characters,
6308 that call would be a waste of cycles, because the
6309 iterator will never move back, and thus never cross this
6310 "fake" stop position. So we delay that backward search
6311 until the time we really need it, in next_element_from_buffer. */
6312 if (CHARPOS (pos) != it->prev_stop)
6313 it->prev_stop = CHARPOS (pos);
6314 if (CHARPOS (pos) < it->base_level_stop)
6315 it->base_level_stop = 0; /* meaning it's unknown */
6316 handle_stop (it);
6317 }
6318 else
6319 {
6320 handle_stop (it);
6321 it->prev_stop = it->base_level_stop = 0;
6322 }
6323
6324 }
6325
6326 CHECK_IT (it);
6327 }
6328
6329
6330 /* Change IT's buffer position to POS. SET_STOP_P non-zero means set
6331 IT->stop_pos to POS, also. */
6332
6333 static void
6334 reseat_1 (struct it *it, struct text_pos pos, int set_stop_p)
6335 {
6336 /* Don't call this function when scanning a C string. */
6337 eassert (it->s == NULL);
6338
6339 /* POS must be a reasonable value. */
6340 eassert (CHARPOS (pos) >= BEGV && CHARPOS (pos) <= ZV);
6341
6342 it->current.pos = it->position = pos;
6343 it->end_charpos = ZV;
6344 it->dpvec = NULL;
6345 it->current.dpvec_index = -1;
6346 it->current.overlay_string_index = -1;
6347 IT_STRING_CHARPOS (*it) = -1;
6348 IT_STRING_BYTEPOS (*it) = -1;
6349 it->string = Qnil;
6350 it->method = GET_FROM_BUFFER;
6351 it->object = it->w->buffer;
6352 it->area = TEXT_AREA;
6353 it->multibyte_p = !NILP (BVAR (current_buffer, enable_multibyte_characters));
6354 it->sp = 0;
6355 it->string_from_display_prop_p = 0;
6356 it->string_from_prefix_prop_p = 0;
6357
6358 it->from_disp_prop_p = 0;
6359 it->face_before_selective_p = 0;
6360 if (it->bidi_p)
6361 {
6362 bidi_init_it (IT_CHARPOS (*it), IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6363 &it->bidi_it);
6364 bidi_unshelve_cache (NULL, 0);
6365 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6366 it->bidi_it.string.s = NULL;
6367 it->bidi_it.string.lstring = Qnil;
6368 it->bidi_it.string.bufpos = 0;
6369 it->bidi_it.string.unibyte = 0;
6370 }
6371
6372 if (set_stop_p)
6373 {
6374 it->stop_charpos = CHARPOS (pos);
6375 it->base_level_stop = CHARPOS (pos);
6376 }
6377 /* This make the information stored in it->cmp_it invalidate. */
6378 it->cmp_it.id = -1;
6379 }
6380
6381
6382 /* Set up IT for displaying a string, starting at CHARPOS in window W.
6383 If S is non-null, it is a C string to iterate over. Otherwise,
6384 STRING gives a Lisp string to iterate over.
6385
6386 If PRECISION > 0, don't return more then PRECISION number of
6387 characters from the string.
6388
6389 If FIELD_WIDTH > 0, return padding spaces until FIELD_WIDTH
6390 characters have been returned. FIELD_WIDTH < 0 means an infinite
6391 field width.
6392
6393 MULTIBYTE = 0 means disable processing of multibyte characters,
6394 MULTIBYTE > 0 means enable it,
6395 MULTIBYTE < 0 means use IT->multibyte_p.
6396
6397 IT must be initialized via a prior call to init_iterator before
6398 calling this function. */
6399
6400 static void
6401 reseat_to_string (struct it *it, const char *s, Lisp_Object string,
6402 ptrdiff_t charpos, ptrdiff_t precision, int field_width,
6403 int multibyte)
6404 {
6405 /* No region in strings. */
6406 it->region_beg_charpos = it->region_end_charpos = -1;
6407
6408 /* No text property checks performed by default, but see below. */
6409 it->stop_charpos = -1;
6410
6411 /* Set iterator position and end position. */
6412 memset (&it->current, 0, sizeof it->current);
6413 it->current.overlay_string_index = -1;
6414 it->current.dpvec_index = -1;
6415 eassert (charpos >= 0);
6416
6417 /* If STRING is specified, use its multibyteness, otherwise use the
6418 setting of MULTIBYTE, if specified. */
6419 if (multibyte >= 0)
6420 it->multibyte_p = multibyte > 0;
6421
6422 /* Bidirectional reordering of strings is controlled by the default
6423 value of bidi-display-reordering. Don't try to reorder while
6424 loading loadup.el, as the necessary character property tables are
6425 not yet available. */
6426 it->bidi_p =
6427 NILP (Vpurify_flag)
6428 && !NILP (BVAR (&buffer_defaults, bidi_display_reordering));
6429
6430 if (s == NULL)
6431 {
6432 eassert (STRINGP (string));
6433 it->string = string;
6434 it->s = NULL;
6435 it->end_charpos = it->string_nchars = SCHARS (string);
6436 it->method = GET_FROM_STRING;
6437 it->current.string_pos = string_pos (charpos, string);
6438
6439 if (it->bidi_p)
6440 {
6441 it->bidi_it.string.lstring = string;
6442 it->bidi_it.string.s = NULL;
6443 it->bidi_it.string.schars = it->end_charpos;
6444 it->bidi_it.string.bufpos = 0;
6445 it->bidi_it.string.from_disp_str = 0;
6446 it->bidi_it.string.unibyte = !it->multibyte_p;
6447 bidi_init_it (charpos, IT_STRING_BYTEPOS (*it),
6448 FRAME_WINDOW_P (it->f), &it->bidi_it);
6449 }
6450 }
6451 else
6452 {
6453 it->s = (const unsigned char *) s;
6454 it->string = Qnil;
6455
6456 /* Note that we use IT->current.pos, not it->current.string_pos,
6457 for displaying C strings. */
6458 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
6459 if (it->multibyte_p)
6460 {
6461 it->current.pos = c_string_pos (charpos, s, 1);
6462 it->end_charpos = it->string_nchars = number_of_chars (s, 1);
6463 }
6464 else
6465 {
6466 IT_CHARPOS (*it) = IT_BYTEPOS (*it) = charpos;
6467 it->end_charpos = it->string_nchars = strlen (s);
6468 }
6469
6470 if (it->bidi_p)
6471 {
6472 it->bidi_it.string.lstring = Qnil;
6473 it->bidi_it.string.s = (const unsigned char *) s;
6474 it->bidi_it.string.schars = it->end_charpos;
6475 it->bidi_it.string.bufpos = 0;
6476 it->bidi_it.string.from_disp_str = 0;
6477 it->bidi_it.string.unibyte = !it->multibyte_p;
6478 bidi_init_it (charpos, IT_BYTEPOS (*it), FRAME_WINDOW_P (it->f),
6479 &it->bidi_it);
6480 }
6481 it->method = GET_FROM_C_STRING;
6482 }
6483
6484 /* PRECISION > 0 means don't return more than PRECISION characters
6485 from the string. */
6486 if (precision > 0 && it->end_charpos - charpos > precision)
6487 {
6488 it->end_charpos = it->string_nchars = charpos + precision;
6489 if (it->bidi_p)
6490 it->bidi_it.string.schars = it->end_charpos;
6491 }
6492
6493 /* FIELD_WIDTH > 0 means pad with spaces until FIELD_WIDTH
6494 characters have been returned. FIELD_WIDTH == 0 means don't pad,
6495 FIELD_WIDTH < 0 means infinite field width. This is useful for
6496 padding with `-' at the end of a mode line. */
6497 if (field_width < 0)
6498 field_width = INFINITY;
6499 /* Implementation note: We deliberately don't enlarge
6500 it->bidi_it.string.schars here to fit it->end_charpos, because
6501 the bidi iterator cannot produce characters out of thin air. */
6502 if (field_width > it->end_charpos - charpos)
6503 it->end_charpos = charpos + field_width;
6504
6505 /* Use the standard display table for displaying strings. */
6506 if (DISP_TABLE_P (Vstandard_display_table))
6507 it->dp = XCHAR_TABLE (Vstandard_display_table);
6508
6509 it->stop_charpos = charpos;
6510 it->prev_stop = charpos;
6511 it->base_level_stop = 0;
6512 if (it->bidi_p)
6513 {
6514 it->bidi_it.first_elt = 1;
6515 it->bidi_it.paragraph_dir = NEUTRAL_DIR;
6516 it->bidi_it.disp_pos = -1;
6517 }
6518 if (s == NULL && it->multibyte_p)
6519 {
6520 ptrdiff_t endpos = SCHARS (it->string);
6521 if (endpos > it->end_charpos)
6522 endpos = it->end_charpos;
6523 composition_compute_stop_pos (&it->cmp_it, charpos, -1, endpos,
6524 it->string);
6525 }
6526 CHECK_IT (it);
6527 }
6528
6529
6530 \f
6531 /***********************************************************************
6532 Iteration
6533 ***********************************************************************/
6534
6535 /* Map enum it_method value to corresponding next_element_from_* function. */
6536
6537 static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
6538 {
6539 next_element_from_buffer,
6540 next_element_from_display_vector,
6541 next_element_from_string,
6542 next_element_from_c_string,
6543 next_element_from_image,
6544 next_element_from_stretch
6545 #ifdef HAVE_XWIDGETS
6546 ,next_element_from_xwidget
6547 #endif
6548 };
6549
6550 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
6551
6552
6553 /* Return 1 iff a character at CHARPOS (and BYTEPOS) is composed
6554 (possibly with the following characters). */
6555
6556 #define CHAR_COMPOSED_P(IT,CHARPOS,BYTEPOS,END_CHARPOS) \
6557 ((IT)->cmp_it.id >= 0 \
6558 || ((IT)->cmp_it.stop_pos == (CHARPOS) \
6559 && composition_reseat_it (&(IT)->cmp_it, CHARPOS, BYTEPOS, \
6560 END_CHARPOS, (IT)->w, \
6561 FACE_FROM_ID ((IT)->f, (IT)->face_id), \
6562 (IT)->string)))
6563
6564
6565 /* Lookup the char-table Vglyphless_char_display for character C (-1
6566 if we want information for no-font case), and return the display
6567 method symbol. By side-effect, update it->what and
6568 it->glyphless_method. This function is called from
6569 get_next_display_element for each character element, and from
6570 x_produce_glyphs when no suitable font was found. */
6571
6572 Lisp_Object
6573 lookup_glyphless_char_display (int c, struct it *it)
6574 {
6575 Lisp_Object glyphless_method = Qnil;
6576
6577 if (CHAR_TABLE_P (Vglyphless_char_display)
6578 && CHAR_TABLE_EXTRA_SLOTS (XCHAR_TABLE (Vglyphless_char_display)) >= 1)
6579 {
6580 if (c >= 0)
6581 {
6582 glyphless_method = CHAR_TABLE_REF (Vglyphless_char_display, c);
6583 if (CONSP (glyphless_method))
6584 glyphless_method = FRAME_WINDOW_P (it->f)
6585 ? XCAR (glyphless_method)
6586 : XCDR (glyphless_method);
6587 }
6588 else
6589 glyphless_method = XCHAR_TABLE (Vglyphless_char_display)->extras[0];
6590 }
6591
6592 retry:
6593 if (NILP (glyphless_method))
6594 {
6595 if (c >= 0)
6596 /* The default is to display the character by a proper font. */
6597 return Qnil;
6598 /* The default for the no-font case is to display an empty box. */
6599 glyphless_method = Qempty_box;
6600 }
6601 if (EQ (glyphless_method, Qzero_width))
6602 {
6603 if (c >= 0)
6604 return glyphless_method;
6605 /* This method can't be used for the no-font case. */
6606 glyphless_method = Qempty_box;
6607 }
6608 if (EQ (glyphless_method, Qthin_space))
6609 it->glyphless_method = GLYPHLESS_DISPLAY_THIN_SPACE;
6610 else if (EQ (glyphless_method, Qempty_box))
6611 it->glyphless_method = GLYPHLESS_DISPLAY_EMPTY_BOX;
6612 else if (EQ (glyphless_method, Qhex_code))
6613 it->glyphless_method = GLYPHLESS_DISPLAY_HEX_CODE;
6614 else if (STRINGP (glyphless_method))
6615 it->glyphless_method = GLYPHLESS_DISPLAY_ACRONYM;
6616 else
6617 {
6618 /* Invalid value. We use the default method. */
6619 glyphless_method = Qnil;
6620 goto retry;
6621 }
6622 it->what = IT_GLYPHLESS;
6623 return glyphless_method;
6624 }
6625
6626 /* Load IT's display element fields with information about the next
6627 display element from the current position of IT. Value is zero if
6628 end of buffer (or C string) is reached. */
6629
6630 static struct frame *last_escape_glyph_frame = NULL;
6631 static int last_escape_glyph_face_id = (1 << FACE_ID_BITS);
6632 static int last_escape_glyph_merged_face_id = 0;
6633
6634 struct frame *last_glyphless_glyph_frame = NULL;
6635 int last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
6636 int last_glyphless_glyph_merged_face_id = 0;
6637
6638 static int
6639 get_next_display_element (struct it *it)
6640 {
6641 /* Non-zero means that we found a display element. Zero means that
6642 we hit the end of what we iterate over. Performance note: the
6643 function pointer `method' used here turns out to be faster than
6644 using a sequence of if-statements. */
6645 int success_p;
6646
6647 get_next:
6648 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
6649
6650 if (it->what == IT_CHARACTER)
6651 {
6652 /* UAX#9, L4: "A character is depicted by a mirrored glyph if
6653 and only if (a) the resolved directionality of that character
6654 is R..." */
6655 /* FIXME: Do we need an exception for characters from display
6656 tables? */
6657 if (it->bidi_p && it->bidi_it.type == STRONG_R)
6658 it->c = bidi_mirror_char (it->c);
6659 /* Map via display table or translate control characters.
6660 IT->c, IT->len etc. have been set to the next character by
6661 the function call above. If we have a display table, and it
6662 contains an entry for IT->c, translate it. Don't do this if
6663 IT->c itself comes from a display table, otherwise we could
6664 end up in an infinite recursion. (An alternative could be to
6665 count the recursion depth of this function and signal an
6666 error when a certain maximum depth is reached.) Is it worth
6667 it? */
6668 if (success_p && it->dpvec == NULL)
6669 {
6670 Lisp_Object dv;
6671 struct charset *unibyte = CHARSET_FROM_ID (charset_unibyte);
6672 int nonascii_space_p = 0;
6673 int nonascii_hyphen_p = 0;
6674 int c = it->c; /* This is the character to display. */
6675
6676 if (! it->multibyte_p && ! ASCII_CHAR_P (c))
6677 {
6678 eassert (SINGLE_BYTE_CHAR_P (c));
6679 if (unibyte_display_via_language_environment)
6680 {
6681 c = DECODE_CHAR (unibyte, c);
6682 if (c < 0)
6683 c = BYTE8_TO_CHAR (it->c);
6684 }
6685 else
6686 c = BYTE8_TO_CHAR (it->c);
6687 }
6688
6689 if (it->dp
6690 && (dv = DISP_CHAR_VECTOR (it->dp, c),
6691 VECTORP (dv)))
6692 {
6693 struct Lisp_Vector *v = XVECTOR (dv);
6694
6695 /* Return the first character from the display table
6696 entry, if not empty. If empty, don't display the
6697 current character. */
6698 if (v->header.size)
6699 {
6700 it->dpvec_char_len = it->len;
6701 it->dpvec = v->contents;
6702 it->dpend = v->contents + v->header.size;
6703 it->current.dpvec_index = 0;
6704 it->dpvec_face_id = -1;
6705 it->saved_face_id = it->face_id;
6706 it->method = GET_FROM_DISPLAY_VECTOR;
6707 it->ellipsis_p = 0;
6708 }
6709 else
6710 {
6711 set_iterator_to_next (it, 0);
6712 }
6713 goto get_next;
6714 }
6715
6716 if (! NILP (lookup_glyphless_char_display (c, it)))
6717 {
6718 if (it->what == IT_GLYPHLESS)
6719 goto done;
6720 /* Don't display this character. */
6721 set_iterator_to_next (it, 0);
6722 goto get_next;
6723 }
6724
6725 /* If `nobreak-char-display' is non-nil, we display
6726 non-ASCII spaces and hyphens specially. */
6727 if (! ASCII_CHAR_P (c) && ! NILP (Vnobreak_char_display))
6728 {
6729 if (c == 0xA0)
6730 nonascii_space_p = 1;
6731 else if (c == 0xAD || c == 0x2010 || c == 0x2011)
6732 nonascii_hyphen_p = 1;
6733 }
6734
6735 /* Translate control characters into `\003' or `^C' form.
6736 Control characters coming from a display table entry are
6737 currently not translated because we use IT->dpvec to hold
6738 the translation. This could easily be changed but I
6739 don't believe that it is worth doing.
6740
6741 The characters handled by `nobreak-char-display' must be
6742 translated too.
6743
6744 Non-printable characters and raw-byte characters are also
6745 translated to octal form. */
6746 if (((c < ' ' || c == 127) /* ASCII control chars */
6747 ? (it->area != TEXT_AREA
6748 /* In mode line, treat \n, \t like other crl chars. */
6749 || (c != '\t'
6750 && it->glyph_row
6751 && (it->glyph_row->mode_line_p || it->avoid_cursor_p))
6752 || (c != '\n' && c != '\t'))
6753 : (nonascii_space_p
6754 || nonascii_hyphen_p
6755 || CHAR_BYTE8_P (c)
6756 || ! CHAR_PRINTABLE_P (c))))
6757 {
6758 /* C is a control character, non-ASCII space/hyphen,
6759 raw-byte, or a non-printable character which must be
6760 displayed either as '\003' or as `^C' where the '\\'
6761 and '^' can be defined in the display table. Fill
6762 IT->ctl_chars with glyphs for what we have to
6763 display. Then, set IT->dpvec to these glyphs. */
6764 Lisp_Object gc;
6765 int ctl_len;
6766 int face_id;
6767 int lface_id = 0;
6768 int escape_glyph;
6769
6770 /* Handle control characters with ^. */
6771
6772 if (ASCII_CHAR_P (c) && it->ctl_arrow_p)
6773 {
6774 int g;
6775
6776 g = '^'; /* default glyph for Control */
6777 /* Set IT->ctl_chars[0] to the glyph for `^'. */
6778 if (it->dp
6779 && (gc = DISP_CTRL_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6780 {
6781 g = GLYPH_CODE_CHAR (gc);
6782 lface_id = GLYPH_CODE_FACE (gc);
6783 }
6784 if (lface_id)
6785 {
6786 face_id = merge_faces (it->f, Qt, lface_id, it->face_id);
6787 }
6788 else if (it->f == last_escape_glyph_frame
6789 && it->face_id == last_escape_glyph_face_id)
6790 {
6791 face_id = last_escape_glyph_merged_face_id;
6792 }
6793 else
6794 {
6795 /* Merge the escape-glyph face into the current face. */
6796 face_id = merge_faces (it->f, Qescape_glyph, 0,
6797 it->face_id);
6798 last_escape_glyph_frame = it->f;
6799 last_escape_glyph_face_id = it->face_id;
6800 last_escape_glyph_merged_face_id = face_id;
6801 }
6802
6803 XSETINT (it->ctl_chars[0], g);
6804 XSETINT (it->ctl_chars[1], c ^ 0100);
6805 ctl_len = 2;
6806 goto display_control;
6807 }
6808
6809 /* Handle non-ascii space in the mode where it only gets
6810 highlighting. */
6811
6812 if (nonascii_space_p && EQ (Vnobreak_char_display, Qt))
6813 {
6814 /* Merge `nobreak-space' into the current face. */
6815 face_id = merge_faces (it->f, Qnobreak_space, 0,
6816 it->face_id);
6817 XSETINT (it->ctl_chars[0], ' ');
6818 ctl_len = 1;
6819 goto display_control;
6820 }
6821
6822 /* Handle sequences that start with the "escape glyph". */
6823
6824 /* the default escape glyph is \. */
6825 escape_glyph = '\\';
6826
6827 if (it->dp
6828 && (gc = DISP_ESCAPE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
6829 {
6830 escape_glyph = GLYPH_CODE_CHAR (gc);
6831 lface_id = GLYPH_CODE_FACE (gc);
6832 }
6833 if (lface_id)
6834 {
6835 /* The display table specified a face.
6836 Merge it into face_id and also into escape_glyph. */
6837 face_id = merge_faces (it->f, Qt, lface_id,
6838 it->face_id);
6839 }
6840 else if (it->f == last_escape_glyph_frame
6841 && it->face_id == last_escape_glyph_face_id)
6842 {
6843 face_id = last_escape_glyph_merged_face_id;
6844 }
6845 else
6846 {
6847 /* Merge the escape-glyph face into the current face. */
6848 face_id = merge_faces (it->f, Qescape_glyph, 0,
6849 it->face_id);
6850 last_escape_glyph_frame = it->f;
6851 last_escape_glyph_face_id = it->face_id;
6852 last_escape_glyph_merged_face_id = face_id;
6853 }
6854
6855 /* Draw non-ASCII hyphen with just highlighting: */
6856
6857 if (nonascii_hyphen_p && EQ (Vnobreak_char_display, Qt))
6858 {
6859 XSETINT (it->ctl_chars[0], '-');
6860 ctl_len = 1;
6861 goto display_control;
6862 }
6863
6864 /* Draw non-ASCII space/hyphen with escape glyph: */
6865
6866 if (nonascii_space_p || nonascii_hyphen_p)
6867 {
6868 XSETINT (it->ctl_chars[0], escape_glyph);
6869 XSETINT (it->ctl_chars[1], nonascii_space_p ? ' ' : '-');
6870 ctl_len = 2;
6871 goto display_control;
6872 }
6873
6874 {
6875 char str[10];
6876 int len, i;
6877
6878 if (CHAR_BYTE8_P (c))
6879 /* Display \200 instead of \17777600. */
6880 c = CHAR_TO_BYTE8 (c);
6881 len = sprintf (str, "%03o", c);
6882
6883 XSETINT (it->ctl_chars[0], escape_glyph);
6884 for (i = 0; i < len; i++)
6885 XSETINT (it->ctl_chars[i + 1], str[i]);
6886 ctl_len = len + 1;
6887 }
6888
6889 display_control:
6890 /* Set up IT->dpvec and return first character from it. */
6891 it->dpvec_char_len = it->len;
6892 it->dpvec = it->ctl_chars;
6893 it->dpend = it->dpvec + ctl_len;
6894 it->current.dpvec_index = 0;
6895 it->dpvec_face_id = face_id;
6896 it->saved_face_id = it->face_id;
6897 it->method = GET_FROM_DISPLAY_VECTOR;
6898 it->ellipsis_p = 0;
6899 goto get_next;
6900 }
6901 it->char_to_display = c;
6902 }
6903 else if (success_p)
6904 {
6905 it->char_to_display = it->c;
6906 }
6907 }
6908
6909 /* Adjust face id for a multibyte character. There are no multibyte
6910 character in unibyte text. */
6911 if ((it->what == IT_CHARACTER || it->what == IT_COMPOSITION)
6912 && it->multibyte_p
6913 && success_p
6914 && FRAME_WINDOW_P (it->f))
6915 {
6916 struct face *face = FACE_FROM_ID (it->f, it->face_id);
6917
6918 if (it->what == IT_COMPOSITION && it->cmp_it.ch >= 0)
6919 {
6920 /* Automatic composition with glyph-string. */
6921 Lisp_Object gstring = composition_gstring_from_id (it->cmp_it.id);
6922
6923 it->face_id = face_for_font (it->f, LGSTRING_FONT (gstring), face);
6924 }
6925 else
6926 {
6927 ptrdiff_t pos = (it->s ? -1
6928 : STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
6929 : IT_CHARPOS (*it));
6930 int c;
6931
6932 if (it->what == IT_CHARACTER)
6933 c = it->char_to_display;
6934 else
6935 {
6936 struct composition *cmp = composition_table[it->cmp_it.id];
6937 int i;
6938
6939 c = ' ';
6940 for (i = 0; i < cmp->glyph_len; i++)
6941 /* TAB in a composition means display glyphs with
6942 padding space on the left or right. */
6943 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
6944 break;
6945 }
6946 it->face_id = FACE_FOR_CHAR (it->f, face, c, pos, it->string);
6947 }
6948 }
6949
6950 done:
6951 /* Is this character the last one of a run of characters with
6952 box? If yes, set IT->end_of_box_run_p to 1. */
6953 if (it->face_box_p
6954 && it->s == NULL)
6955 {
6956 if (it->method == GET_FROM_STRING && it->sp)
6957 {
6958 int face_id = underlying_face_id (it);
6959 struct face *face = FACE_FROM_ID (it->f, face_id);
6960
6961 if (face)
6962 {
6963 if (face->box == FACE_NO_BOX)
6964 {
6965 /* If the box comes from face properties in a
6966 display string, check faces in that string. */
6967 int string_face_id = face_after_it_pos (it);
6968 it->end_of_box_run_p
6969 = (FACE_FROM_ID (it->f, string_face_id)->box
6970 == FACE_NO_BOX);
6971 }
6972 /* Otherwise, the box comes from the underlying face.
6973 If this is the last string character displayed, check
6974 the next buffer location. */
6975 else if ((IT_STRING_CHARPOS (*it) >= SCHARS (it->string) - 1)
6976 && (it->current.overlay_string_index
6977 == it->n_overlay_strings - 1))
6978 {
6979 ptrdiff_t ignore;
6980 int next_face_id;
6981 struct text_pos pos = it->current.pos;
6982 INC_TEXT_POS (pos, it->multibyte_p);
6983
6984 next_face_id = face_at_buffer_position
6985 (it->w, CHARPOS (pos), it->region_beg_charpos,
6986 it->region_end_charpos, &ignore,
6987 (IT_CHARPOS (*it) + TEXT_PROP_DISTANCE_LIMIT), 0,
6988 -1);
6989 it->end_of_box_run_p
6990 = (FACE_FROM_ID (it->f, next_face_id)->box
6991 == FACE_NO_BOX);
6992 }
6993 }
6994 }
6995 else
6996 {
6997 int face_id = face_after_it_pos (it);
6998 it->end_of_box_run_p
6999 = (face_id != it->face_id
7000 && FACE_FROM_ID (it->f, face_id)->box == FACE_NO_BOX);
7001 }
7002 }
7003 /* If we reached the end of the object we've been iterating (e.g., a
7004 display string or an overlay string), and there's something on
7005 IT->stack, proceed with what's on the stack. It doesn't make
7006 sense to return zero if there's unprocessed stuff on the stack,
7007 because otherwise that stuff will never be displayed. */
7008 if (!success_p && it->sp > 0)
7009 {
7010 set_iterator_to_next (it, 0);
7011 success_p = get_next_display_element (it);
7012 }
7013
7014 /* Value is 0 if end of buffer or string reached. */
7015 return success_p;
7016 }
7017
7018
7019 /* Move IT to the next display element.
7020
7021 RESEAT_P non-zero means if called on a newline in buffer text,
7022 skip to the next visible line start.
7023
7024 Functions get_next_display_element and set_iterator_to_next are
7025 separate because I find this arrangement easier to handle than a
7026 get_next_display_element function that also increments IT's
7027 position. The way it is we can first look at an iterator's current
7028 display element, decide whether it fits on a line, and if it does,
7029 increment the iterator position. The other way around we probably
7030 would either need a flag indicating whether the iterator has to be
7031 incremented the next time, or we would have to implement a
7032 decrement position function which would not be easy to write. */
7033
7034 void
7035 set_iterator_to_next (struct it *it, int reseat_p)
7036 {
7037 /* Reset flags indicating start and end of a sequence of characters
7038 with box. Reset them at the start of this function because
7039 moving the iterator to a new position might set them. */
7040 it->start_of_box_run_p = it->end_of_box_run_p = 0;
7041
7042 switch (it->method)
7043 {
7044 case GET_FROM_BUFFER:
7045 /* The current display element of IT is a character from
7046 current_buffer. Advance in the buffer, and maybe skip over
7047 invisible lines that are so because of selective display. */
7048 if (ITERATOR_AT_END_OF_LINE_P (it) && reseat_p)
7049 reseat_at_next_visible_line_start (it, 0);
7050 else if (it->cmp_it.id >= 0)
7051 {
7052 /* We are currently getting glyphs from a composition. */
7053 int i;
7054
7055 if (! it->bidi_p)
7056 {
7057 IT_CHARPOS (*it) += it->cmp_it.nchars;
7058 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
7059 if (it->cmp_it.to < it->cmp_it.nglyphs)
7060 {
7061 it->cmp_it.from = it->cmp_it.to;
7062 }
7063 else
7064 {
7065 it->cmp_it.id = -1;
7066 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7067 IT_BYTEPOS (*it),
7068 it->end_charpos, Qnil);
7069 }
7070 }
7071 else if (! it->cmp_it.reversed_p)
7072 {
7073 /* Composition created while scanning forward. */
7074 /* Update IT's char/byte positions to point to the first
7075 character of the next grapheme cluster, or to the
7076 character visually after the current composition. */
7077 for (i = 0; i < it->cmp_it.nchars; i++)
7078 bidi_move_to_visually_next (&it->bidi_it);
7079 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7080 IT_CHARPOS (*it) = it->bidi_it.charpos;
7081
7082 if (it->cmp_it.to < it->cmp_it.nglyphs)
7083 {
7084 /* Proceed to the next grapheme cluster. */
7085 it->cmp_it.from = it->cmp_it.to;
7086 }
7087 else
7088 {
7089 /* No more grapheme clusters in this composition.
7090 Find the next stop position. */
7091 ptrdiff_t stop = it->end_charpos;
7092 if (it->bidi_it.scan_dir < 0)
7093 /* Now we are scanning backward and don't know
7094 where to stop. */
7095 stop = -1;
7096 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7097 IT_BYTEPOS (*it), stop, Qnil);
7098 }
7099 }
7100 else
7101 {
7102 /* Composition created while scanning backward. */
7103 /* Update IT's char/byte positions to point to the last
7104 character of the previous grapheme cluster, or the
7105 character visually after the current composition. */
7106 for (i = 0; i < it->cmp_it.nchars; i++)
7107 bidi_move_to_visually_next (&it->bidi_it);
7108 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7109 IT_CHARPOS (*it) = it->bidi_it.charpos;
7110 if (it->cmp_it.from > 0)
7111 {
7112 /* Proceed to the previous grapheme cluster. */
7113 it->cmp_it.to = it->cmp_it.from;
7114 }
7115 else
7116 {
7117 /* No more grapheme clusters in this composition.
7118 Find the next stop position. */
7119 ptrdiff_t stop = it->end_charpos;
7120 if (it->bidi_it.scan_dir < 0)
7121 /* Now we are scanning backward and don't know
7122 where to stop. */
7123 stop = -1;
7124 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7125 IT_BYTEPOS (*it), stop, Qnil);
7126 }
7127 }
7128 }
7129 else
7130 {
7131 eassert (it->len != 0);
7132
7133 if (!it->bidi_p)
7134 {
7135 IT_BYTEPOS (*it) += it->len;
7136 IT_CHARPOS (*it) += 1;
7137 }
7138 else
7139 {
7140 int prev_scan_dir = it->bidi_it.scan_dir;
7141 /* If this is a new paragraph, determine its base
7142 direction (a.k.a. its base embedding level). */
7143 if (it->bidi_it.new_paragraph)
7144 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
7145 bidi_move_to_visually_next (&it->bidi_it);
7146 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7147 IT_CHARPOS (*it) = it->bidi_it.charpos;
7148 if (prev_scan_dir != it->bidi_it.scan_dir)
7149 {
7150 /* As the scan direction was changed, we must
7151 re-compute the stop position for composition. */
7152 ptrdiff_t stop = it->end_charpos;
7153 if (it->bidi_it.scan_dir < 0)
7154 stop = -1;
7155 composition_compute_stop_pos (&it->cmp_it, IT_CHARPOS (*it),
7156 IT_BYTEPOS (*it), stop, Qnil);
7157 }
7158 }
7159 eassert (IT_BYTEPOS (*it) == CHAR_TO_BYTE (IT_CHARPOS (*it)));
7160 }
7161 break;
7162
7163 case GET_FROM_C_STRING:
7164 /* Current display element of IT is from a C string. */
7165 if (!it->bidi_p
7166 /* If the string position is beyond string's end, it means
7167 next_element_from_c_string is padding the string with
7168 blanks, in which case we bypass the bidi iterator,
7169 because it cannot deal with such virtual characters. */
7170 || IT_CHARPOS (*it) >= it->bidi_it.string.schars)
7171 {
7172 IT_BYTEPOS (*it) += it->len;
7173 IT_CHARPOS (*it) += 1;
7174 }
7175 else
7176 {
7177 bidi_move_to_visually_next (&it->bidi_it);
7178 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7179 IT_CHARPOS (*it) = it->bidi_it.charpos;
7180 }
7181 break;
7182
7183 case GET_FROM_DISPLAY_VECTOR:
7184 /* Current display element of IT is from a display table entry.
7185 Advance in the display table definition. Reset it to null if
7186 end reached, and continue with characters from buffers/
7187 strings. */
7188 ++it->current.dpvec_index;
7189
7190 /* Restore face of the iterator to what they were before the
7191 display vector entry (these entries may contain faces). */
7192 it->face_id = it->saved_face_id;
7193
7194 if (it->dpvec + it->current.dpvec_index >= it->dpend)
7195 {
7196 int recheck_faces = it->ellipsis_p;
7197
7198 if (it->s)
7199 it->method = GET_FROM_C_STRING;
7200 else if (STRINGP (it->string))
7201 it->method = GET_FROM_STRING;
7202 else
7203 {
7204 it->method = GET_FROM_BUFFER;
7205 it->object = it->w->buffer;
7206 }
7207
7208 it->dpvec = NULL;
7209 it->current.dpvec_index = -1;
7210
7211 /* Skip over characters which were displayed via IT->dpvec. */
7212 if (it->dpvec_char_len < 0)
7213 reseat_at_next_visible_line_start (it, 1);
7214 else if (it->dpvec_char_len > 0)
7215 {
7216 if (it->method == GET_FROM_STRING
7217 && it->n_overlay_strings > 0)
7218 it->ignore_overlay_strings_at_pos_p = 1;
7219 it->len = it->dpvec_char_len;
7220 set_iterator_to_next (it, reseat_p);
7221 }
7222
7223 /* Maybe recheck faces after display vector */
7224 if (recheck_faces)
7225 it->stop_charpos = IT_CHARPOS (*it);
7226 }
7227 break;
7228
7229 case GET_FROM_STRING:
7230 /* Current display element is a character from a Lisp string. */
7231 eassert (it->s == NULL && STRINGP (it->string));
7232 /* Don't advance past string end. These conditions are true
7233 when set_iterator_to_next is called at the end of
7234 get_next_display_element, in which case the Lisp string is
7235 already exhausted, and all we want is pop the iterator
7236 stack. */
7237 if (it->current.overlay_string_index >= 0)
7238 {
7239 /* This is an overlay string, so there's no padding with
7240 spaces, and the number of characters in the string is
7241 where the string ends. */
7242 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7243 goto consider_string_end;
7244 }
7245 else
7246 {
7247 /* Not an overlay string. There could be padding, so test
7248 against it->end_charpos . */
7249 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7250 goto consider_string_end;
7251 }
7252 if (it->cmp_it.id >= 0)
7253 {
7254 int i;
7255
7256 if (! it->bidi_p)
7257 {
7258 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
7259 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
7260 if (it->cmp_it.to < it->cmp_it.nglyphs)
7261 it->cmp_it.from = it->cmp_it.to;
7262 else
7263 {
7264 it->cmp_it.id = -1;
7265 composition_compute_stop_pos (&it->cmp_it,
7266 IT_STRING_CHARPOS (*it),
7267 IT_STRING_BYTEPOS (*it),
7268 it->end_charpos, it->string);
7269 }
7270 }
7271 else if (! it->cmp_it.reversed_p)
7272 {
7273 for (i = 0; i < it->cmp_it.nchars; i++)
7274 bidi_move_to_visually_next (&it->bidi_it);
7275 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7276 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7277
7278 if (it->cmp_it.to < it->cmp_it.nglyphs)
7279 it->cmp_it.from = it->cmp_it.to;
7280 else
7281 {
7282 ptrdiff_t stop = it->end_charpos;
7283 if (it->bidi_it.scan_dir < 0)
7284 stop = -1;
7285 composition_compute_stop_pos (&it->cmp_it,
7286 IT_STRING_CHARPOS (*it),
7287 IT_STRING_BYTEPOS (*it), stop,
7288 it->string);
7289 }
7290 }
7291 else
7292 {
7293 for (i = 0; i < it->cmp_it.nchars; i++)
7294 bidi_move_to_visually_next (&it->bidi_it);
7295 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7296 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7297 if (it->cmp_it.from > 0)
7298 it->cmp_it.to = it->cmp_it.from;
7299 else
7300 {
7301 ptrdiff_t stop = it->end_charpos;
7302 if (it->bidi_it.scan_dir < 0)
7303 stop = -1;
7304 composition_compute_stop_pos (&it->cmp_it,
7305 IT_STRING_CHARPOS (*it),
7306 IT_STRING_BYTEPOS (*it), stop,
7307 it->string);
7308 }
7309 }
7310 }
7311 else
7312 {
7313 if (!it->bidi_p
7314 /* If the string position is beyond string's end, it
7315 means next_element_from_string is padding the string
7316 with blanks, in which case we bypass the bidi
7317 iterator, because it cannot deal with such virtual
7318 characters. */
7319 || IT_STRING_CHARPOS (*it) >= it->bidi_it.string.schars)
7320 {
7321 IT_STRING_BYTEPOS (*it) += it->len;
7322 IT_STRING_CHARPOS (*it) += 1;
7323 }
7324 else
7325 {
7326 int prev_scan_dir = it->bidi_it.scan_dir;
7327
7328 bidi_move_to_visually_next (&it->bidi_it);
7329 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7330 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7331 if (prev_scan_dir != it->bidi_it.scan_dir)
7332 {
7333 ptrdiff_t stop = it->end_charpos;
7334
7335 if (it->bidi_it.scan_dir < 0)
7336 stop = -1;
7337 composition_compute_stop_pos (&it->cmp_it,
7338 IT_STRING_CHARPOS (*it),
7339 IT_STRING_BYTEPOS (*it), stop,
7340 it->string);
7341 }
7342 }
7343 }
7344
7345 consider_string_end:
7346
7347 if (it->current.overlay_string_index >= 0)
7348 {
7349 /* IT->string is an overlay string. Advance to the
7350 next, if there is one. */
7351 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7352 {
7353 it->ellipsis_p = 0;
7354 next_overlay_string (it);
7355 if (it->ellipsis_p)
7356 setup_for_ellipsis (it, 0);
7357 }
7358 }
7359 else
7360 {
7361 /* IT->string is not an overlay string. If we reached
7362 its end, and there is something on IT->stack, proceed
7363 with what is on the stack. This can be either another
7364 string, this time an overlay string, or a buffer. */
7365 if (IT_STRING_CHARPOS (*it) == SCHARS (it->string)
7366 && it->sp > 0)
7367 {
7368 pop_it (it);
7369 if (it->method == GET_FROM_STRING)
7370 goto consider_string_end;
7371 }
7372 }
7373 break;
7374
7375 case GET_FROM_IMAGE:
7376 case GET_FROM_STRETCH:
7377 #ifdef HAVE_XWIDGETS
7378 case GET_FROM_XWIDGET:
7379
7380 /* The position etc with which we have to proceed are on
7381 the stack. The position may be at the end of a string,
7382 if the `display' property takes up the whole string. */
7383 eassert (it->sp > 0);
7384 pop_it (it);
7385 if (it->method == GET_FROM_STRING)
7386 goto consider_string_end;
7387 break;
7388 #endif
7389 default:
7390 /* There are no other methods defined, so this should be a bug. */
7391 emacs_abort ();
7392 }
7393
7394 eassert (it->method != GET_FROM_STRING
7395 || (STRINGP (it->string)
7396 && IT_STRING_CHARPOS (*it) >= 0));
7397 }
7398
7399 /* Load IT's display element fields with information about the next
7400 display element which comes from a display table entry or from the
7401 result of translating a control character to one of the forms `^C'
7402 or `\003'.
7403
7404 IT->dpvec holds the glyphs to return as characters.
7405 IT->saved_face_id holds the face id before the display vector--it
7406 is restored into IT->face_id in set_iterator_to_next. */
7407
7408 static int
7409 next_element_from_display_vector (struct it *it)
7410 {
7411 Lisp_Object gc;
7412
7413 /* Precondition. */
7414 eassert (it->dpvec && it->current.dpvec_index >= 0);
7415
7416 it->face_id = it->saved_face_id;
7417
7418 /* KFS: This code used to check ip->dpvec[0] instead of the current element.
7419 That seemed totally bogus - so I changed it... */
7420 gc = it->dpvec[it->current.dpvec_index];
7421
7422 if (GLYPH_CODE_P (gc))
7423 {
7424 it->c = GLYPH_CODE_CHAR (gc);
7425 it->len = CHAR_BYTES (it->c);
7426
7427 /* The entry may contain a face id to use. Such a face id is
7428 the id of a Lisp face, not a realized face. A face id of
7429 zero means no face is specified. */
7430 if (it->dpvec_face_id >= 0)
7431 it->face_id = it->dpvec_face_id;
7432 else
7433 {
7434 int lface_id = GLYPH_CODE_FACE (gc);
7435 if (lface_id > 0)
7436 it->face_id = merge_faces (it->f, Qt, lface_id,
7437 it->saved_face_id);
7438 }
7439 }
7440 else
7441 /* Display table entry is invalid. Return a space. */
7442 it->c = ' ', it->len = 1;
7443
7444 /* Don't change position and object of the iterator here. They are
7445 still the values of the character that had this display table
7446 entry or was translated, and that's what we want. */
7447 it->what = IT_CHARACTER;
7448 return 1;
7449 }
7450
7451 /* Get the first element of string/buffer in the visual order, after
7452 being reseated to a new position in a string or a buffer. */
7453 static void
7454 get_visually_first_element (struct it *it)
7455 {
7456 int string_p = STRINGP (it->string) || it->s;
7457 ptrdiff_t eob = (string_p ? it->bidi_it.string.schars : ZV);
7458 ptrdiff_t bob = (string_p ? 0 : BEGV);
7459
7460 if (STRINGP (it->string))
7461 {
7462 it->bidi_it.charpos = IT_STRING_CHARPOS (*it);
7463 it->bidi_it.bytepos = IT_STRING_BYTEPOS (*it);
7464 }
7465 else
7466 {
7467 it->bidi_it.charpos = IT_CHARPOS (*it);
7468 it->bidi_it.bytepos = IT_BYTEPOS (*it);
7469 }
7470
7471 if (it->bidi_it.charpos == eob)
7472 {
7473 /* Nothing to do, but reset the FIRST_ELT flag, like
7474 bidi_paragraph_init does, because we are not going to
7475 call it. */
7476 it->bidi_it.first_elt = 0;
7477 }
7478 else if (it->bidi_it.charpos == bob
7479 || (!string_p
7480 && (FETCH_CHAR (it->bidi_it.bytepos - 1) == '\n'
7481 || FETCH_CHAR (it->bidi_it.bytepos) == '\n')))
7482 {
7483 /* If we are at the beginning of a line/string, we can produce
7484 the next element right away. */
7485 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7486 bidi_move_to_visually_next (&it->bidi_it);
7487 }
7488 else
7489 {
7490 ptrdiff_t orig_bytepos = it->bidi_it.bytepos;
7491
7492 /* We need to prime the bidi iterator starting at the line's or
7493 string's beginning, before we will be able to produce the
7494 next element. */
7495 if (string_p)
7496 it->bidi_it.charpos = it->bidi_it.bytepos = 0;
7497 else
7498 {
7499 it->bidi_it.charpos = find_next_newline_no_quit (IT_CHARPOS (*it),
7500 -1);
7501 it->bidi_it.bytepos = CHAR_TO_BYTE (it->bidi_it.charpos);
7502 }
7503 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 1);
7504 do
7505 {
7506 /* Now return to buffer/string position where we were asked
7507 to get the next display element, and produce that. */
7508 bidi_move_to_visually_next (&it->bidi_it);
7509 }
7510 while (it->bidi_it.bytepos != orig_bytepos
7511 && it->bidi_it.charpos < eob);
7512 }
7513
7514 /* Adjust IT's position information to where we ended up. */
7515 if (STRINGP (it->string))
7516 {
7517 IT_STRING_CHARPOS (*it) = it->bidi_it.charpos;
7518 IT_STRING_BYTEPOS (*it) = it->bidi_it.bytepos;
7519 }
7520 else
7521 {
7522 IT_CHARPOS (*it) = it->bidi_it.charpos;
7523 IT_BYTEPOS (*it) = it->bidi_it.bytepos;
7524 }
7525
7526 if (STRINGP (it->string) || !it->s)
7527 {
7528 ptrdiff_t stop, charpos, bytepos;
7529
7530 if (STRINGP (it->string))
7531 {
7532 eassert (!it->s);
7533 stop = SCHARS (it->string);
7534 if (stop > it->end_charpos)
7535 stop = it->end_charpos;
7536 charpos = IT_STRING_CHARPOS (*it);
7537 bytepos = IT_STRING_BYTEPOS (*it);
7538 }
7539 else
7540 {
7541 stop = it->end_charpos;
7542 charpos = IT_CHARPOS (*it);
7543 bytepos = IT_BYTEPOS (*it);
7544 }
7545 if (it->bidi_it.scan_dir < 0)
7546 stop = -1;
7547 composition_compute_stop_pos (&it->cmp_it, charpos, bytepos, stop,
7548 it->string);
7549 }
7550 }
7551
7552 /* Load IT with the next display element from Lisp string IT->string.
7553 IT->current.string_pos is the current position within the string.
7554 If IT->current.overlay_string_index >= 0, the Lisp string is an
7555 overlay string. */
7556
7557 static int
7558 next_element_from_string (struct it *it)
7559 {
7560 struct text_pos position;
7561
7562 eassert (STRINGP (it->string));
7563 eassert (!it->bidi_p || EQ (it->string, it->bidi_it.string.lstring));
7564 eassert (IT_STRING_CHARPOS (*it) >= 0);
7565 position = it->current.string_pos;
7566
7567 /* With bidi reordering, the character to display might not be the
7568 character at IT_STRING_CHARPOS. BIDI_IT.FIRST_ELT non-zero means
7569 that we were reseat()ed to a new string, whose paragraph
7570 direction is not known. */
7571 if (it->bidi_p && it->bidi_it.first_elt)
7572 {
7573 get_visually_first_element (it);
7574 SET_TEXT_POS (position, IT_STRING_CHARPOS (*it), IT_STRING_BYTEPOS (*it));
7575 }
7576
7577 /* Time to check for invisible text? */
7578 if (IT_STRING_CHARPOS (*it) < it->end_charpos)
7579 {
7580 if (IT_STRING_CHARPOS (*it) >= it->stop_charpos)
7581 {
7582 if (!(!it->bidi_p
7583 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7584 || IT_STRING_CHARPOS (*it) == it->stop_charpos))
7585 {
7586 /* With bidi non-linear iteration, we could find
7587 ourselves far beyond the last computed stop_charpos,
7588 with several other stop positions in between that we
7589 missed. Scan them all now, in buffer's logical
7590 order, until we find and handle the last stop_charpos
7591 that precedes our current position. */
7592 handle_stop_backwards (it, it->stop_charpos);
7593 return GET_NEXT_DISPLAY_ELEMENT (it);
7594 }
7595 else
7596 {
7597 if (it->bidi_p)
7598 {
7599 /* Take note of the stop position we just moved
7600 across, for when we will move back across it. */
7601 it->prev_stop = it->stop_charpos;
7602 /* If we are at base paragraph embedding level, take
7603 note of the last stop position seen at this
7604 level. */
7605 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7606 it->base_level_stop = it->stop_charpos;
7607 }
7608 handle_stop (it);
7609
7610 /* Since a handler may have changed IT->method, we must
7611 recurse here. */
7612 return GET_NEXT_DISPLAY_ELEMENT (it);
7613 }
7614 }
7615 else if (it->bidi_p
7616 /* If we are before prev_stop, we may have overstepped
7617 on our way backwards a stop_pos, and if so, we need
7618 to handle that stop_pos. */
7619 && IT_STRING_CHARPOS (*it) < it->prev_stop
7620 /* We can sometimes back up for reasons that have nothing
7621 to do with bidi reordering. E.g., compositions. The
7622 code below is only needed when we are above the base
7623 embedding level, so test for that explicitly. */
7624 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
7625 {
7626 /* If we lost track of base_level_stop, we have no better
7627 place for handle_stop_backwards to start from than string
7628 beginning. This happens, e.g., when we were reseated to
7629 the previous screenful of text by vertical-motion. */
7630 if (it->base_level_stop <= 0
7631 || IT_STRING_CHARPOS (*it) < it->base_level_stop)
7632 it->base_level_stop = 0;
7633 handle_stop_backwards (it, it->base_level_stop);
7634 return GET_NEXT_DISPLAY_ELEMENT (it);
7635 }
7636 }
7637
7638 if (it->current.overlay_string_index >= 0)
7639 {
7640 /* Get the next character from an overlay string. In overlay
7641 strings, there is no field width or padding with spaces to
7642 do. */
7643 if (IT_STRING_CHARPOS (*it) >= SCHARS (it->string))
7644 {
7645 it->what = IT_EOB;
7646 return 0;
7647 }
7648 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7649 IT_STRING_BYTEPOS (*it),
7650 it->bidi_it.scan_dir < 0
7651 ? -1
7652 : SCHARS (it->string))
7653 && next_element_from_composition (it))
7654 {
7655 return 1;
7656 }
7657 else if (STRING_MULTIBYTE (it->string))
7658 {
7659 const unsigned char *s = (SDATA (it->string)
7660 + IT_STRING_BYTEPOS (*it));
7661 it->c = string_char_and_length (s, &it->len);
7662 }
7663 else
7664 {
7665 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7666 it->len = 1;
7667 }
7668 }
7669 else
7670 {
7671 /* Get the next character from a Lisp string that is not an
7672 overlay string. Such strings come from the mode line, for
7673 example. We may have to pad with spaces, or truncate the
7674 string. See also next_element_from_c_string. */
7675 if (IT_STRING_CHARPOS (*it) >= it->end_charpos)
7676 {
7677 it->what = IT_EOB;
7678 return 0;
7679 }
7680 else if (IT_STRING_CHARPOS (*it) >= it->string_nchars)
7681 {
7682 /* Pad with spaces. */
7683 it->c = ' ', it->len = 1;
7684 CHARPOS (position) = BYTEPOS (position) = -1;
7685 }
7686 else if (CHAR_COMPOSED_P (it, IT_STRING_CHARPOS (*it),
7687 IT_STRING_BYTEPOS (*it),
7688 it->bidi_it.scan_dir < 0
7689 ? -1
7690 : it->string_nchars)
7691 && next_element_from_composition (it))
7692 {
7693 return 1;
7694 }
7695 else if (STRING_MULTIBYTE (it->string))
7696 {
7697 const unsigned char *s = (SDATA (it->string)
7698 + IT_STRING_BYTEPOS (*it));
7699 it->c = string_char_and_length (s, &it->len);
7700 }
7701 else
7702 {
7703 it->c = SREF (it->string, IT_STRING_BYTEPOS (*it));
7704 it->len = 1;
7705 }
7706 }
7707
7708 /* Record what we have and where it came from. */
7709 it->what = IT_CHARACTER;
7710 it->object = it->string;
7711 it->position = position;
7712 return 1;
7713 }
7714
7715
7716 /* Load IT with next display element from C string IT->s.
7717 IT->string_nchars is the maximum number of characters to return
7718 from the string. IT->end_charpos may be greater than
7719 IT->string_nchars when this function is called, in which case we
7720 may have to return padding spaces. Value is zero if end of string
7721 reached, including padding spaces. */
7722
7723 static int
7724 next_element_from_c_string (struct it *it)
7725 {
7726 int success_p = 1;
7727
7728 eassert (it->s);
7729 eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
7730 it->what = IT_CHARACTER;
7731 BYTEPOS (it->position) = CHARPOS (it->position) = 0;
7732 it->object = Qnil;
7733
7734 /* With bidi reordering, the character to display might not be the
7735 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7736 we were reseated to a new string, whose paragraph direction is
7737 not known. */
7738 if (it->bidi_p && it->bidi_it.first_elt)
7739 get_visually_first_element (it);
7740
7741 /* IT's position can be greater than IT->string_nchars in case a
7742 field width or precision has been specified when the iterator was
7743 initialized. */
7744 if (IT_CHARPOS (*it) >= it->end_charpos)
7745 {
7746 /* End of the game. */
7747 it->what = IT_EOB;
7748 success_p = 0;
7749 }
7750 else if (IT_CHARPOS (*it) >= it->string_nchars)
7751 {
7752 /* Pad with spaces. */
7753 it->c = ' ', it->len = 1;
7754 BYTEPOS (it->position) = CHARPOS (it->position) = -1;
7755 }
7756 else if (it->multibyte_p)
7757 it->c = string_char_and_length (it->s + IT_BYTEPOS (*it), &it->len);
7758 else
7759 it->c = it->s[IT_BYTEPOS (*it)], it->len = 1;
7760
7761 return success_p;
7762 }
7763
7764
7765 /* Set up IT to return characters from an ellipsis, if appropriate.
7766 The definition of the ellipsis glyphs may come from a display table
7767 entry. This function fills IT with the first glyph from the
7768 ellipsis if an ellipsis is to be displayed. */
7769
7770 static int
7771 next_element_from_ellipsis (struct it *it)
7772 {
7773 if (it->selective_display_ellipsis_p)
7774 setup_for_ellipsis (it, it->len);
7775 else
7776 {
7777 /* The face at the current position may be different from the
7778 face we find after the invisible text. Remember what it
7779 was in IT->saved_face_id, and signal that it's there by
7780 setting face_before_selective_p. */
7781 it->saved_face_id = it->face_id;
7782 it->method = GET_FROM_BUFFER;
7783 it->object = it->w->buffer;
7784 reseat_at_next_visible_line_start (it, 1);
7785 it->face_before_selective_p = 1;
7786 }
7787
7788 return GET_NEXT_DISPLAY_ELEMENT (it);
7789 }
7790
7791
7792 /* Deliver an image display element. The iterator IT is already
7793 filled with image information (done in handle_display_prop). Value
7794 is always 1. */
7795
7796
7797 static int
7798 next_element_from_image (struct it *it)
7799 {
7800 it->what = IT_IMAGE;
7801 it->ignore_overlay_strings_at_pos_p = 0;
7802 return 1;
7803 }
7804
7805 #ifdef HAVE_XWIDGETS
7806 /* im not sure about this FIXME JAVE*/
7807 static int
7808 next_element_from_xwidget (struct it *it)
7809 {
7810 it->what = IT_XWIDGET;
7811 //assert_valid_xwidget_id(it->xwidget_id,"next_element_from_xwidget");
7812 //this is shaky because why do we set "what" if we dont set the other parts??
7813 //printf("xwidget_id %d: in next_element_from_xwidget: FIXME \n", it->xwidget_id);
7814 return 1;
7815 }
7816 #endif
7817
7818
7819 /* Fill iterator IT with next display element from a stretch glyph
7820 property. IT->object is the value of the text property. Value is
7821 always 1. */
7822
7823 static int
7824 next_element_from_stretch (struct it *it)
7825 {
7826 it->what = IT_STRETCH;
7827 return 1;
7828 }
7829
7830 /* Scan backwards from IT's current position until we find a stop
7831 position, or until BEGV. This is called when we find ourself
7832 before both the last known prev_stop and base_level_stop while
7833 reordering bidirectional text. */
7834
7835 static void
7836 compute_stop_pos_backwards (struct it *it)
7837 {
7838 const int SCAN_BACK_LIMIT = 1000;
7839 struct text_pos pos;
7840 struct display_pos save_current = it->current;
7841 struct text_pos save_position = it->position;
7842 ptrdiff_t charpos = IT_CHARPOS (*it);
7843 ptrdiff_t where_we_are = charpos;
7844 ptrdiff_t save_stop_pos = it->stop_charpos;
7845 ptrdiff_t save_end_pos = it->end_charpos;
7846
7847 eassert (NILP (it->string) && !it->s);
7848 eassert (it->bidi_p);
7849 it->bidi_p = 0;
7850 do
7851 {
7852 it->end_charpos = min (charpos + 1, ZV);
7853 charpos = max (charpos - SCAN_BACK_LIMIT, BEGV);
7854 SET_TEXT_POS (pos, charpos, CHAR_TO_BYTE (charpos));
7855 reseat_1 (it, pos, 0);
7856 compute_stop_pos (it);
7857 /* We must advance forward, right? */
7858 if (it->stop_charpos <= charpos)
7859 emacs_abort ();
7860 }
7861 while (charpos > BEGV && it->stop_charpos >= it->end_charpos);
7862
7863 if (it->stop_charpos <= where_we_are)
7864 it->prev_stop = it->stop_charpos;
7865 else
7866 it->prev_stop = BEGV;
7867 it->bidi_p = 1;
7868 it->current = save_current;
7869 it->position = save_position;
7870 it->stop_charpos = save_stop_pos;
7871 it->end_charpos = save_end_pos;
7872 }
7873
7874 /* Scan forward from CHARPOS in the current buffer/string, until we
7875 find a stop position > current IT's position. Then handle the stop
7876 position before that. This is called when we bump into a stop
7877 position while reordering bidirectional text. CHARPOS should be
7878 the last previously processed stop_pos (or BEGV/0, if none were
7879 processed yet) whose position is less that IT's current
7880 position. */
7881
7882 static void
7883 handle_stop_backwards (struct it *it, ptrdiff_t charpos)
7884 {
7885 int bufp = !STRINGP (it->string);
7886 ptrdiff_t where_we_are = (bufp ? IT_CHARPOS (*it) : IT_STRING_CHARPOS (*it));
7887 struct display_pos save_current = it->current;
7888 struct text_pos save_position = it->position;
7889 struct text_pos pos1;
7890 ptrdiff_t next_stop;
7891
7892 /* Scan in strict logical order. */
7893 eassert (it->bidi_p);
7894 it->bidi_p = 0;
7895 do
7896 {
7897 it->prev_stop = charpos;
7898 if (bufp)
7899 {
7900 SET_TEXT_POS (pos1, charpos, CHAR_TO_BYTE (charpos));
7901 reseat_1 (it, pos1, 0);
7902 }
7903 else
7904 it->current.string_pos = string_pos (charpos, it->string);
7905 compute_stop_pos (it);
7906 /* We must advance forward, right? */
7907 if (it->stop_charpos <= it->prev_stop)
7908 emacs_abort ();
7909 charpos = it->stop_charpos;
7910 }
7911 while (charpos <= where_we_are);
7912
7913 it->bidi_p = 1;
7914 it->current = save_current;
7915 it->position = save_position;
7916 next_stop = it->stop_charpos;
7917 it->stop_charpos = it->prev_stop;
7918 handle_stop (it);
7919 it->stop_charpos = next_stop;
7920 }
7921
7922 /* Load IT with the next display element from current_buffer. Value
7923 is zero if end of buffer reached. IT->stop_charpos is the next
7924 position at which to stop and check for text properties or buffer
7925 end. */
7926
7927 static int
7928 next_element_from_buffer (struct it *it)
7929 {
7930 int success_p = 1;
7931
7932 eassert (IT_CHARPOS (*it) >= BEGV);
7933 eassert (NILP (it->string) && !it->s);
7934 eassert (!it->bidi_p
7935 || (EQ (it->bidi_it.string.lstring, Qnil)
7936 && it->bidi_it.string.s == NULL));
7937
7938 /* With bidi reordering, the character to display might not be the
7939 character at IT_CHARPOS. BIDI_IT.FIRST_ELT non-zero means that
7940 we were reseat()ed to a new buffer position, which is potentially
7941 a different paragraph. */
7942 if (it->bidi_p && it->bidi_it.first_elt)
7943 {
7944 get_visually_first_element (it);
7945 SET_TEXT_POS (it->position, IT_CHARPOS (*it), IT_BYTEPOS (*it));
7946 }
7947
7948 if (IT_CHARPOS (*it) >= it->stop_charpos)
7949 {
7950 if (IT_CHARPOS (*it) >= it->end_charpos)
7951 {
7952 int overlay_strings_follow_p;
7953
7954 /* End of the game, except when overlay strings follow that
7955 haven't been returned yet. */
7956 if (it->overlay_strings_at_end_processed_p)
7957 overlay_strings_follow_p = 0;
7958 else
7959 {
7960 it->overlay_strings_at_end_processed_p = 1;
7961 overlay_strings_follow_p = get_overlay_strings (it, 0);
7962 }
7963
7964 if (overlay_strings_follow_p)
7965 success_p = GET_NEXT_DISPLAY_ELEMENT (it);
7966 else
7967 {
7968 it->what = IT_EOB;
7969 it->position = it->current.pos;
7970 success_p = 0;
7971 }
7972 }
7973 else if (!(!it->bidi_p
7974 || BIDI_AT_BASE_LEVEL (it->bidi_it)
7975 || IT_CHARPOS (*it) == it->stop_charpos))
7976 {
7977 /* With bidi non-linear iteration, we could find ourselves
7978 far beyond the last computed stop_charpos, with several
7979 other stop positions in between that we missed. Scan
7980 them all now, in buffer's logical order, until we find
7981 and handle the last stop_charpos that precedes our
7982 current position. */
7983 handle_stop_backwards (it, it->stop_charpos);
7984 return GET_NEXT_DISPLAY_ELEMENT (it);
7985 }
7986 else
7987 {
7988 if (it->bidi_p)
7989 {
7990 /* Take note of the stop position we just moved across,
7991 for when we will move back across it. */
7992 it->prev_stop = it->stop_charpos;
7993 /* If we are at base paragraph embedding level, take
7994 note of the last stop position seen at this
7995 level. */
7996 if (BIDI_AT_BASE_LEVEL (it->bidi_it))
7997 it->base_level_stop = it->stop_charpos;
7998 }
7999 handle_stop (it);
8000 return GET_NEXT_DISPLAY_ELEMENT (it);
8001 }
8002 }
8003 else if (it->bidi_p
8004 /* If we are before prev_stop, we may have overstepped on
8005 our way backwards a stop_pos, and if so, we need to
8006 handle that stop_pos. */
8007 && IT_CHARPOS (*it) < it->prev_stop
8008 /* We can sometimes back up for reasons that have nothing
8009 to do with bidi reordering. E.g., compositions. The
8010 code below is only needed when we are above the base
8011 embedding level, so test for that explicitly. */
8012 && !BIDI_AT_BASE_LEVEL (it->bidi_it))
8013 {
8014 if (it->base_level_stop <= 0
8015 || IT_CHARPOS (*it) < it->base_level_stop)
8016 {
8017 /* If we lost track of base_level_stop, we need to find
8018 prev_stop by looking backwards. This happens, e.g., when
8019 we were reseated to the previous screenful of text by
8020 vertical-motion. */
8021 it->base_level_stop = BEGV;
8022 compute_stop_pos_backwards (it);
8023 handle_stop_backwards (it, it->prev_stop);
8024 }
8025 else
8026 handle_stop_backwards (it, it->base_level_stop);
8027 return GET_NEXT_DISPLAY_ELEMENT (it);
8028 }
8029 else
8030 {
8031 /* No face changes, overlays etc. in sight, so just return a
8032 character from current_buffer. */
8033 unsigned char *p;
8034 ptrdiff_t stop;
8035
8036 /* Maybe run the redisplay end trigger hook. Performance note:
8037 This doesn't seem to cost measurable time. */
8038 if (it->redisplay_end_trigger_charpos
8039 && it->glyph_row
8040 && IT_CHARPOS (*it) >= it->redisplay_end_trigger_charpos)
8041 run_redisplay_end_trigger_hook (it);
8042
8043 stop = it->bidi_it.scan_dir < 0 ? -1 : it->end_charpos;
8044 if (CHAR_COMPOSED_P (it, IT_CHARPOS (*it), IT_BYTEPOS (*it),
8045 stop)
8046 && next_element_from_composition (it))
8047 {
8048 return 1;
8049 }
8050
8051 /* Get the next character, maybe multibyte. */
8052 p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
8053 if (it->multibyte_p && !ASCII_BYTE_P (*p))
8054 it->c = STRING_CHAR_AND_LENGTH (p, it->len);
8055 else
8056 it->c = *p, it->len = 1;
8057
8058 /* Record what we have and where it came from. */
8059 it->what = IT_CHARACTER;
8060 it->object = it->w->buffer;
8061 it->position = it->current.pos;
8062
8063 /* Normally we return the character found above, except when we
8064 really want to return an ellipsis for selective display. */
8065 if (it->selective)
8066 {
8067 if (it->c == '\n')
8068 {
8069 /* A value of selective > 0 means hide lines indented more
8070 than that number of columns. */
8071 if (it->selective > 0
8072 && IT_CHARPOS (*it) + 1 < ZV
8073 && indented_beyond_p (IT_CHARPOS (*it) + 1,
8074 IT_BYTEPOS (*it) + 1,
8075 it->selective))
8076 {
8077 success_p = next_element_from_ellipsis (it);
8078 it->dpvec_char_len = -1;
8079 }
8080 }
8081 else if (it->c == '\r' && it->selective == -1)
8082 {
8083 /* A value of selective == -1 means that everything from the
8084 CR to the end of the line is invisible, with maybe an
8085 ellipsis displayed for it. */
8086 success_p = next_element_from_ellipsis (it);
8087 it->dpvec_char_len = -1;
8088 }
8089 }
8090 }
8091
8092 /* Value is zero if end of buffer reached. */
8093 eassert (!success_p || it->what != IT_CHARACTER || it->len > 0);
8094 return success_p;
8095 }
8096
8097
8098 /* Run the redisplay end trigger hook for IT. */
8099
8100 static void
8101 run_redisplay_end_trigger_hook (struct it *it)
8102 {
8103 Lisp_Object args[3];
8104
8105 /* IT->glyph_row should be non-null, i.e. we should be actually
8106 displaying something, or otherwise we should not run the hook. */
8107 eassert (it->glyph_row);
8108
8109 /* Set up hook arguments. */
8110 args[0] = Qredisplay_end_trigger_functions;
8111 args[1] = it->window;
8112 XSETINT (args[2], it->redisplay_end_trigger_charpos);
8113 it->redisplay_end_trigger_charpos = 0;
8114
8115 /* Since we are *trying* to run these functions, don't try to run
8116 them again, even if they get an error. */
8117 wset_redisplay_end_trigger (it->w, Qnil);
8118 Frun_hook_with_args (3, args);
8119
8120 /* Notice if it changed the face of the character we are on. */
8121 handle_face_prop (it);
8122 }
8123
8124
8125 /* Deliver a composition display element. Unlike the other
8126 next_element_from_XXX, this function is not registered in the array
8127 get_next_element[]. It is called from next_element_from_buffer and
8128 next_element_from_string when necessary. */
8129
8130 static int
8131 next_element_from_composition (struct it *it)
8132 {
8133 it->what = IT_COMPOSITION;
8134 it->len = it->cmp_it.nbytes;
8135 if (STRINGP (it->string))
8136 {
8137 if (it->c < 0)
8138 {
8139 IT_STRING_CHARPOS (*it) += it->cmp_it.nchars;
8140 IT_STRING_BYTEPOS (*it) += it->cmp_it.nbytes;
8141 return 0;
8142 }
8143 it->position = it->current.string_pos;
8144 it->object = it->string;
8145 it->c = composition_update_it (&it->cmp_it, IT_STRING_CHARPOS (*it),
8146 IT_STRING_BYTEPOS (*it), it->string);
8147 }
8148 else
8149 {
8150 if (it->c < 0)
8151 {
8152 IT_CHARPOS (*it) += it->cmp_it.nchars;
8153 IT_BYTEPOS (*it) += it->cmp_it.nbytes;
8154 if (it->bidi_p)
8155 {
8156 if (it->bidi_it.new_paragraph)
8157 bidi_paragraph_init (it->paragraph_embedding, &it->bidi_it, 0);
8158 /* Resync the bidi iterator with IT's new position.
8159 FIXME: this doesn't support bidirectional text. */
8160 while (it->bidi_it.charpos < IT_CHARPOS (*it))
8161 bidi_move_to_visually_next (&it->bidi_it);
8162 }
8163 return 0;
8164 }
8165 it->position = it->current.pos;
8166 it->object = it->w->buffer;
8167 it->c = composition_update_it (&it->cmp_it, IT_CHARPOS (*it),
8168 IT_BYTEPOS (*it), Qnil);
8169 }
8170 return 1;
8171 }
8172
8173
8174 \f
8175 /***********************************************************************
8176 Moving an iterator without producing glyphs
8177 ***********************************************************************/
8178
8179 /* Check if iterator is at a position corresponding to a valid buffer
8180 position after some move_it_ call. */
8181
8182 #define IT_POS_VALID_AFTER_MOVE_P(it) \
8183 ((it)->method == GET_FROM_STRING \
8184 ? IT_STRING_CHARPOS (*it) == 0 \
8185 : 1)
8186
8187
8188 /* Move iterator IT to a specified buffer or X position within one
8189 line on the display without producing glyphs.
8190
8191 OP should be a bit mask including some or all of these bits:
8192 MOVE_TO_X: Stop upon reaching x-position TO_X.
8193 MOVE_TO_POS: Stop upon reaching buffer or string position TO_CHARPOS.
8194 Regardless of OP's value, stop upon reaching the end of the display line.
8195
8196 TO_X is normally a value 0 <= TO_X <= IT->last_visible_x.
8197 This means, in particular, that TO_X includes window's horizontal
8198 scroll amount.
8199
8200 The return value has several possible values that
8201 say what condition caused the scan to stop:
8202
8203 MOVE_POS_MATCH_OR_ZV
8204 - when TO_POS or ZV was reached.
8205
8206 MOVE_X_REACHED
8207 -when TO_X was reached before TO_POS or ZV were reached.
8208
8209 MOVE_LINE_CONTINUED
8210 - when we reached the end of the display area and the line must
8211 be continued.
8212
8213 MOVE_LINE_TRUNCATED
8214 - when we reached the end of the display area and the line is
8215 truncated.
8216
8217 MOVE_NEWLINE_OR_CR
8218 - when we stopped at a line end, i.e. a newline or a CR and selective
8219 display is on. */
8220
8221 static enum move_it_result
8222 move_it_in_display_line_to (struct it *it,
8223 ptrdiff_t to_charpos, int to_x,
8224 enum move_operation_enum op)
8225 {
8226 enum move_it_result result = MOVE_UNDEFINED;
8227 struct glyph_row *saved_glyph_row;
8228 struct it wrap_it, atpos_it, atx_it, ppos_it;
8229 void *wrap_data = NULL, *atpos_data = NULL, *atx_data = NULL;
8230 void *ppos_data = NULL;
8231 int may_wrap = 0;
8232 enum it_method prev_method = it->method;
8233 ptrdiff_t prev_pos = IT_CHARPOS (*it);
8234 int saw_smaller_pos = prev_pos < to_charpos;
8235
8236 /* Don't produce glyphs in produce_glyphs. */
8237 saved_glyph_row = it->glyph_row;
8238 it->glyph_row = NULL;
8239
8240 /* Use wrap_it to save a copy of IT wherever a word wrap could
8241 occur. Use atpos_it to save a copy of IT at the desired buffer
8242 position, if found, so that we can scan ahead and check if the
8243 word later overshoots the window edge. Use atx_it similarly, for
8244 pixel positions. */
8245 wrap_it.sp = -1;
8246 atpos_it.sp = -1;
8247 atx_it.sp = -1;
8248
8249 /* Use ppos_it under bidi reordering to save a copy of IT for the
8250 position > CHARPOS that is the closest to CHARPOS. We restore
8251 that position in IT when we have scanned the entire display line
8252 without finding a match for CHARPOS and all the character
8253 positions are greater than CHARPOS. */
8254 if (it->bidi_p)
8255 {
8256 SAVE_IT (ppos_it, *it, ppos_data);
8257 SET_TEXT_POS (ppos_it.current.pos, ZV, ZV_BYTE);
8258 if ((op & MOVE_TO_POS) && IT_CHARPOS (*it) >= to_charpos)
8259 SAVE_IT (ppos_it, *it, ppos_data);
8260 }
8261
8262 #define BUFFER_POS_REACHED_P() \
8263 ((op & MOVE_TO_POS) != 0 \
8264 && BUFFERP (it->object) \
8265 && (IT_CHARPOS (*it) == to_charpos \
8266 || ((!it->bidi_p \
8267 || BIDI_AT_BASE_LEVEL (it->bidi_it)) \
8268 && IT_CHARPOS (*it) > to_charpos) \
8269 || (it->what == IT_COMPOSITION \
8270 && ((IT_CHARPOS (*it) > to_charpos \
8271 && to_charpos >= it->cmp_it.charpos) \
8272 || (IT_CHARPOS (*it) < to_charpos \
8273 && to_charpos <= it->cmp_it.charpos)))) \
8274 && (it->method == GET_FROM_BUFFER \
8275 || (it->method == GET_FROM_DISPLAY_VECTOR \
8276 && it->dpvec + it->current.dpvec_index + 1 >= it->dpend)))
8277
8278 /* If there's a line-/wrap-prefix, handle it. */
8279 if (it->hpos == 0 && it->method == GET_FROM_BUFFER
8280 && it->current_y < it->last_visible_y)
8281 handle_line_prefix (it);
8282
8283 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8284 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8285
8286 while (1)
8287 {
8288 int x, i, ascent = 0, descent = 0;
8289
8290 /* Utility macro to reset an iterator with x, ascent, and descent. */
8291 #define IT_RESET_X_ASCENT_DESCENT(IT) \
8292 ((IT)->current_x = x, (IT)->max_ascent = ascent, \
8293 (IT)->max_descent = descent)
8294
8295 /* Stop if we move beyond TO_CHARPOS (after an image or a
8296 display string or stretch glyph). */
8297 if ((op & MOVE_TO_POS) != 0
8298 && BUFFERP (it->object)
8299 && it->method == GET_FROM_BUFFER
8300 && (((!it->bidi_p
8301 /* When the iterator is at base embedding level, we
8302 are guaranteed that characters are delivered for
8303 display in strictly increasing order of their
8304 buffer positions. */
8305 || BIDI_AT_BASE_LEVEL (it->bidi_it))
8306 && IT_CHARPOS (*it) > to_charpos)
8307 || (it->bidi_p
8308 && (prev_method == GET_FROM_IMAGE
8309 || prev_method == GET_FROM_STRETCH
8310 || prev_method == GET_FROM_STRING)
8311 /* Passed TO_CHARPOS from left to right. */
8312 && ((prev_pos < to_charpos
8313 && IT_CHARPOS (*it) > to_charpos)
8314 /* Passed TO_CHARPOS from right to left. */
8315 || (prev_pos > to_charpos
8316 && IT_CHARPOS (*it) < to_charpos)))))
8317 {
8318 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8319 {
8320 result = MOVE_POS_MATCH_OR_ZV;
8321 break;
8322 }
8323 else if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8324 /* If wrap_it is valid, the current position might be in a
8325 word that is wrapped. So, save the iterator in
8326 atpos_it and continue to see if wrapping happens. */
8327 SAVE_IT (atpos_it, *it, atpos_data);
8328 }
8329
8330 /* Stop when ZV reached.
8331 We used to stop here when TO_CHARPOS reached as well, but that is
8332 too soon if this glyph does not fit on this line. So we handle it
8333 explicitly below. */
8334 if (!get_next_display_element (it))
8335 {
8336 result = MOVE_POS_MATCH_OR_ZV;
8337 break;
8338 }
8339
8340 if (it->line_wrap == TRUNCATE)
8341 {
8342 if (BUFFER_POS_REACHED_P ())
8343 {
8344 result = MOVE_POS_MATCH_OR_ZV;
8345 break;
8346 }
8347 }
8348 else
8349 {
8350 if (it->line_wrap == WORD_WRAP)
8351 {
8352 if (IT_DISPLAYING_WHITESPACE (it))
8353 may_wrap = 1;
8354 else if (may_wrap)
8355 {
8356 /* We have reached a glyph that follows one or more
8357 whitespace characters. If the position is
8358 already found, we are done. */
8359 if (atpos_it.sp >= 0)
8360 {
8361 RESTORE_IT (it, &atpos_it, atpos_data);
8362 result = MOVE_POS_MATCH_OR_ZV;
8363 goto done;
8364 }
8365 if (atx_it.sp >= 0)
8366 {
8367 RESTORE_IT (it, &atx_it, atx_data);
8368 result = MOVE_X_REACHED;
8369 goto done;
8370 }
8371 /* Otherwise, we can wrap here. */
8372 SAVE_IT (wrap_it, *it, wrap_data);
8373 may_wrap = 0;
8374 }
8375 }
8376 }
8377
8378 /* Remember the line height for the current line, in case
8379 the next element doesn't fit on the line. */
8380 ascent = it->max_ascent;
8381 descent = it->max_descent;
8382
8383 /* The call to produce_glyphs will get the metrics of the
8384 display element IT is loaded with. Record the x-position
8385 before this display element, in case it doesn't fit on the
8386 line. */
8387 x = it->current_x;
8388
8389 PRODUCE_GLYPHS (it);
8390
8391 if (it->area != TEXT_AREA)
8392 {
8393 prev_method = it->method;
8394 if (it->method == GET_FROM_BUFFER)
8395 prev_pos = IT_CHARPOS (*it);
8396 set_iterator_to_next (it, 1);
8397 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8398 SET_TEXT_POS (this_line_min_pos,
8399 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8400 if (it->bidi_p
8401 && (op & MOVE_TO_POS)
8402 && IT_CHARPOS (*it) > to_charpos
8403 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8404 SAVE_IT (ppos_it, *it, ppos_data);
8405 continue;
8406 }
8407
8408 /* The number of glyphs we get back in IT->nglyphs will normally
8409 be 1 except when IT->c is (i) a TAB, or (ii) a multi-glyph
8410 character on a terminal frame, or (iii) a line end. For the
8411 second case, IT->nglyphs - 1 padding glyphs will be present.
8412 (On X frames, there is only one glyph produced for a
8413 composite character.)
8414
8415 The behavior implemented below means, for continuation lines,
8416 that as many spaces of a TAB as fit on the current line are
8417 displayed there. For terminal frames, as many glyphs of a
8418 multi-glyph character are displayed in the current line, too.
8419 This is what the old redisplay code did, and we keep it that
8420 way. Under X, the whole shape of a complex character must
8421 fit on the line or it will be completely displayed in the
8422 next line.
8423
8424 Note that both for tabs and padding glyphs, all glyphs have
8425 the same width. */
8426 if (it->nglyphs)
8427 {
8428 /* More than one glyph or glyph doesn't fit on line. All
8429 glyphs have the same width. */
8430 int single_glyph_width = it->pixel_width / it->nglyphs;
8431 int new_x;
8432 int x_before_this_char = x;
8433 int hpos_before_this_char = it->hpos;
8434
8435 for (i = 0; i < it->nglyphs; ++i, x = new_x)
8436 {
8437 new_x = x + single_glyph_width;
8438
8439 /* We want to leave anything reaching TO_X to the caller. */
8440 if ((op & MOVE_TO_X) && new_x > to_x)
8441 {
8442 if (BUFFER_POS_REACHED_P ())
8443 {
8444 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8445 goto buffer_pos_reached;
8446 if (atpos_it.sp < 0)
8447 {
8448 SAVE_IT (atpos_it, *it, atpos_data);
8449 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8450 }
8451 }
8452 else
8453 {
8454 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8455 {
8456 it->current_x = x;
8457 result = MOVE_X_REACHED;
8458 break;
8459 }
8460 if (atx_it.sp < 0)
8461 {
8462 SAVE_IT (atx_it, *it, atx_data);
8463 IT_RESET_X_ASCENT_DESCENT (&atx_it);
8464 }
8465 }
8466 }
8467
8468 if (/* Lines are continued. */
8469 it->line_wrap != TRUNCATE
8470 && (/* And glyph doesn't fit on the line. */
8471 new_x > it->last_visible_x
8472 /* Or it fits exactly and we're on a window
8473 system frame. */
8474 || (new_x == it->last_visible_x
8475 && FRAME_WINDOW_P (it->f)
8476 && ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8477 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8478 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
8479 {
8480 if (/* IT->hpos == 0 means the very first glyph
8481 doesn't fit on the line, e.g. a wide image. */
8482 it->hpos == 0
8483 || (new_x == it->last_visible_x
8484 && FRAME_WINDOW_P (it->f)))
8485 {
8486 ++it->hpos;
8487 it->current_x = new_x;
8488
8489 /* The character's last glyph just barely fits
8490 in this row. */
8491 if (i == it->nglyphs - 1)
8492 {
8493 /* If this is the destination position,
8494 return a position *before* it in this row,
8495 now that we know it fits in this row. */
8496 if (BUFFER_POS_REACHED_P ())
8497 {
8498 if (it->line_wrap != WORD_WRAP
8499 || wrap_it.sp < 0)
8500 {
8501 it->hpos = hpos_before_this_char;
8502 it->current_x = x_before_this_char;
8503 result = MOVE_POS_MATCH_OR_ZV;
8504 break;
8505 }
8506 if (it->line_wrap == WORD_WRAP
8507 && atpos_it.sp < 0)
8508 {
8509 SAVE_IT (atpos_it, *it, atpos_data);
8510 atpos_it.current_x = x_before_this_char;
8511 atpos_it.hpos = hpos_before_this_char;
8512 }
8513 }
8514
8515 prev_method = it->method;
8516 if (it->method == GET_FROM_BUFFER)
8517 prev_pos = IT_CHARPOS (*it);
8518 set_iterator_to_next (it, 1);
8519 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8520 SET_TEXT_POS (this_line_min_pos,
8521 IT_CHARPOS (*it), IT_BYTEPOS (*it));
8522 /* On graphical terminals, newlines may
8523 "overflow" into the fringe if
8524 overflow-newline-into-fringe is non-nil.
8525 On text terminals, and on graphical
8526 terminals with no right margin, newlines
8527 may overflow into the last glyph on the
8528 display line.*/
8529 if (!FRAME_WINDOW_P (it->f)
8530 || ((it->bidi_p
8531 && it->bidi_it.paragraph_dir == R2L)
8532 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8533 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8534 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8535 {
8536 if (!get_next_display_element (it))
8537 {
8538 result = MOVE_POS_MATCH_OR_ZV;
8539 break;
8540 }
8541 if (BUFFER_POS_REACHED_P ())
8542 {
8543 if (ITERATOR_AT_END_OF_LINE_P (it))
8544 result = MOVE_POS_MATCH_OR_ZV;
8545 else
8546 result = MOVE_LINE_CONTINUED;
8547 break;
8548 }
8549 if (ITERATOR_AT_END_OF_LINE_P (it))
8550 {
8551 result = MOVE_NEWLINE_OR_CR;
8552 break;
8553 }
8554 }
8555 }
8556 }
8557 else
8558 IT_RESET_X_ASCENT_DESCENT (it);
8559
8560 if (wrap_it.sp >= 0)
8561 {
8562 RESTORE_IT (it, &wrap_it, wrap_data);
8563 atpos_it.sp = -1;
8564 atx_it.sp = -1;
8565 }
8566
8567 TRACE_MOVE ((stderr, "move_it_in: continued at %d\n",
8568 IT_CHARPOS (*it)));
8569 result = MOVE_LINE_CONTINUED;
8570 break;
8571 }
8572
8573 if (BUFFER_POS_REACHED_P ())
8574 {
8575 if (it->line_wrap != WORD_WRAP || wrap_it.sp < 0)
8576 goto buffer_pos_reached;
8577 if (it->line_wrap == WORD_WRAP && atpos_it.sp < 0)
8578 {
8579 SAVE_IT (atpos_it, *it, atpos_data);
8580 IT_RESET_X_ASCENT_DESCENT (&atpos_it);
8581 }
8582 }
8583
8584 if (new_x > it->first_visible_x)
8585 {
8586 /* Glyph is visible. Increment number of glyphs that
8587 would be displayed. */
8588 ++it->hpos;
8589 }
8590 }
8591
8592 if (result != MOVE_UNDEFINED)
8593 break;
8594 }
8595 else if (BUFFER_POS_REACHED_P ())
8596 {
8597 buffer_pos_reached:
8598 IT_RESET_X_ASCENT_DESCENT (it);
8599 result = MOVE_POS_MATCH_OR_ZV;
8600 break;
8601 }
8602 else if ((op & MOVE_TO_X) && it->current_x >= to_x)
8603 {
8604 /* Stop when TO_X specified and reached. This check is
8605 necessary here because of lines consisting of a line end,
8606 only. The line end will not produce any glyphs and we
8607 would never get MOVE_X_REACHED. */
8608 eassert (it->nglyphs == 0);
8609 result = MOVE_X_REACHED;
8610 break;
8611 }
8612
8613 /* Is this a line end? If yes, we're done. */
8614 if (ITERATOR_AT_END_OF_LINE_P (it))
8615 {
8616 /* If we are past TO_CHARPOS, but never saw any character
8617 positions smaller than TO_CHARPOS, return
8618 MOVE_POS_MATCH_OR_ZV, like the unidirectional display
8619 did. */
8620 if (it->bidi_p && (op & MOVE_TO_POS) != 0)
8621 {
8622 if (!saw_smaller_pos && IT_CHARPOS (*it) > to_charpos)
8623 {
8624 if (IT_CHARPOS (ppos_it) < ZV)
8625 {
8626 RESTORE_IT (it, &ppos_it, ppos_data);
8627 result = MOVE_POS_MATCH_OR_ZV;
8628 }
8629 else
8630 goto buffer_pos_reached;
8631 }
8632 else if (it->line_wrap == WORD_WRAP && atpos_it.sp >= 0
8633 && IT_CHARPOS (*it) > to_charpos)
8634 goto buffer_pos_reached;
8635 else
8636 result = MOVE_NEWLINE_OR_CR;
8637 }
8638 else
8639 result = MOVE_NEWLINE_OR_CR;
8640 break;
8641 }
8642
8643 prev_method = it->method;
8644 if (it->method == GET_FROM_BUFFER)
8645 prev_pos = IT_CHARPOS (*it);
8646 /* The current display element has been consumed. Advance
8647 to the next. */
8648 set_iterator_to_next (it, 1);
8649 if (IT_CHARPOS (*it) < CHARPOS (this_line_min_pos))
8650 SET_TEXT_POS (this_line_min_pos, IT_CHARPOS (*it), IT_BYTEPOS (*it));
8651 if (IT_CHARPOS (*it) < to_charpos)
8652 saw_smaller_pos = 1;
8653 if (it->bidi_p
8654 && (op & MOVE_TO_POS)
8655 && IT_CHARPOS (*it) >= to_charpos
8656 && IT_CHARPOS (*it) < IT_CHARPOS (ppos_it))
8657 SAVE_IT (ppos_it, *it, ppos_data);
8658
8659 /* Stop if lines are truncated and IT's current x-position is
8660 past the right edge of the window now. */
8661 if (it->line_wrap == TRUNCATE
8662 && it->current_x >= it->last_visible_x)
8663 {
8664 if (!FRAME_WINDOW_P (it->f)
8665 || ((it->bidi_p && it->bidi_it.paragraph_dir == R2L)
8666 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
8667 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0
8668 || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
8669 {
8670 int at_eob_p = 0;
8671
8672 if ((at_eob_p = !get_next_display_element (it))
8673 || BUFFER_POS_REACHED_P ()
8674 /* If we are past TO_CHARPOS, but never saw any
8675 character positions smaller than TO_CHARPOS,
8676 return MOVE_POS_MATCH_OR_ZV, like the
8677 unidirectional display did. */
8678 || (it->bidi_p && (op & MOVE_TO_POS) != 0
8679 && !saw_smaller_pos
8680 && IT_CHARPOS (*it) > to_charpos))
8681 {
8682 if (it->bidi_p
8683 && !at_eob_p && IT_CHARPOS (ppos_it) < ZV)
8684 RESTORE_IT (it, &ppos_it, ppos_data);
8685 result = MOVE_POS_MATCH_OR_ZV;
8686 break;
8687 }
8688 if (ITERATOR_AT_END_OF_LINE_P (it))
8689 {
8690 result = MOVE_NEWLINE_OR_CR;
8691 break;
8692 }
8693 }
8694 else if (it->bidi_p && (op & MOVE_TO_POS) != 0
8695 && !saw_smaller_pos
8696 && IT_CHARPOS (*it) > to_charpos)
8697 {
8698 if (IT_CHARPOS (ppos_it) < ZV)
8699 RESTORE_IT (it, &ppos_it, ppos_data);
8700 result = MOVE_POS_MATCH_OR_ZV;
8701 break;
8702 }
8703 result = MOVE_LINE_TRUNCATED;
8704 break;
8705 }
8706 #undef IT_RESET_X_ASCENT_DESCENT
8707 }
8708
8709 #undef BUFFER_POS_REACHED_P
8710
8711 /* If we scanned beyond to_pos and didn't find a point to wrap at,
8712 restore the saved iterator. */
8713 if (atpos_it.sp >= 0)
8714 RESTORE_IT (it, &atpos_it, atpos_data);
8715 else if (atx_it.sp >= 0)
8716 RESTORE_IT (it, &atx_it, atx_data);
8717
8718 done:
8719
8720 if (atpos_data)
8721 bidi_unshelve_cache (atpos_data, 1);
8722 if (atx_data)
8723 bidi_unshelve_cache (atx_data, 1);
8724 if (wrap_data)
8725 bidi_unshelve_cache (wrap_data, 1);
8726 if (ppos_data)
8727 bidi_unshelve_cache (ppos_data, 1);
8728
8729 /* Restore the iterator settings altered at the beginning of this
8730 function. */
8731 it->glyph_row = saved_glyph_row;
8732 return result;
8733 }
8734
8735 /* For external use. */
8736 void
8737 move_it_in_display_line (struct it *it,
8738 ptrdiff_t to_charpos, int to_x,
8739 enum move_operation_enum op)
8740 {
8741 if (it->line_wrap == WORD_WRAP
8742 && (op & MOVE_TO_X))
8743 {
8744 struct it save_it;
8745 void *save_data = NULL;
8746 int skip;
8747
8748 SAVE_IT (save_it, *it, save_data);
8749 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8750 /* When word-wrap is on, TO_X may lie past the end
8751 of a wrapped line. Then it->current is the
8752 character on the next line, so backtrack to the
8753 space before the wrap point. */
8754 if (skip == MOVE_LINE_CONTINUED)
8755 {
8756 int prev_x = max (it->current_x - 1, 0);
8757 RESTORE_IT (it, &save_it, save_data);
8758 move_it_in_display_line_to
8759 (it, -1, prev_x, MOVE_TO_X);
8760 }
8761 else
8762 bidi_unshelve_cache (save_data, 1);
8763 }
8764 else
8765 move_it_in_display_line_to (it, to_charpos, to_x, op);
8766 }
8767
8768
8769 /* Move IT forward until it satisfies one or more of the criteria in
8770 TO_CHARPOS, TO_X, TO_Y, and TO_VPOS.
8771
8772 OP is a bit-mask that specifies where to stop, and in particular,
8773 which of those four position arguments makes a difference. See the
8774 description of enum move_operation_enum.
8775
8776 If TO_CHARPOS is in invisible text, e.g. a truncated part of a
8777 screen line, this function will set IT to the next position that is
8778 displayed to the right of TO_CHARPOS on the screen. */
8779
8780 void
8781 move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos, int op)
8782 {
8783 enum move_it_result skip, skip2 = MOVE_X_REACHED;
8784 int line_height, line_start_x = 0, reached = 0;
8785 void *backup_data = NULL;
8786
8787 for (;;)
8788 {
8789 if (op & MOVE_TO_VPOS)
8790 {
8791 /* If no TO_CHARPOS and no TO_X specified, stop at the
8792 start of the line TO_VPOS. */
8793 if ((op & (MOVE_TO_X | MOVE_TO_POS)) == 0)
8794 {
8795 if (it->vpos == to_vpos)
8796 {
8797 reached = 1;
8798 break;
8799 }
8800 else
8801 skip = move_it_in_display_line_to (it, -1, -1, 0);
8802 }
8803 else
8804 {
8805 /* TO_VPOS >= 0 means stop at TO_X in the line at
8806 TO_VPOS, or at TO_POS, whichever comes first. */
8807 if (it->vpos == to_vpos)
8808 {
8809 reached = 2;
8810 break;
8811 }
8812
8813 skip = move_it_in_display_line_to (it, to_charpos, to_x, op);
8814
8815 if (skip == MOVE_POS_MATCH_OR_ZV || it->vpos == to_vpos)
8816 {
8817 reached = 3;
8818 break;
8819 }
8820 else if (skip == MOVE_X_REACHED && it->vpos != to_vpos)
8821 {
8822 /* We have reached TO_X but not in the line we want. */
8823 skip = move_it_in_display_line_to (it, to_charpos,
8824 -1, MOVE_TO_POS);
8825 if (skip == MOVE_POS_MATCH_OR_ZV)
8826 {
8827 reached = 4;
8828 break;
8829 }
8830 }
8831 }
8832 }
8833 else if (op & MOVE_TO_Y)
8834 {
8835 struct it it_backup;
8836
8837 if (it->line_wrap == WORD_WRAP)
8838 SAVE_IT (it_backup, *it, backup_data);
8839
8840 /* TO_Y specified means stop at TO_X in the line containing
8841 TO_Y---or at TO_CHARPOS if this is reached first. The
8842 problem is that we can't really tell whether the line
8843 contains TO_Y before we have completely scanned it, and
8844 this may skip past TO_X. What we do is to first scan to
8845 TO_X.
8846
8847 If TO_X is not specified, use a TO_X of zero. The reason
8848 is to make the outcome of this function more predictable.
8849 If we didn't use TO_X == 0, we would stop at the end of
8850 the line which is probably not what a caller would expect
8851 to happen. */
8852 skip = move_it_in_display_line_to
8853 (it, to_charpos, ((op & MOVE_TO_X) ? to_x : 0),
8854 (MOVE_TO_X | (op & MOVE_TO_POS)));
8855
8856 /* If TO_CHARPOS is reached or ZV, we don't have to do more. */
8857 if (skip == MOVE_POS_MATCH_OR_ZV)
8858 reached = 5;
8859 else if (skip == MOVE_X_REACHED)
8860 {
8861 /* If TO_X was reached, we want to know whether TO_Y is
8862 in the line. We know this is the case if the already
8863 scanned glyphs make the line tall enough. Otherwise,
8864 we must check by scanning the rest of the line. */
8865 line_height = it->max_ascent + it->max_descent;
8866 if (to_y >= it->current_y
8867 && to_y < it->current_y + line_height)
8868 {
8869 reached = 6;
8870 break;
8871 }
8872 SAVE_IT (it_backup, *it, backup_data);
8873 TRACE_MOVE ((stderr, "move_it: from %d\n", IT_CHARPOS (*it)));
8874 skip2 = move_it_in_display_line_to (it, to_charpos, -1,
8875 op & MOVE_TO_POS);
8876 TRACE_MOVE ((stderr, "move_it: to %d\n", IT_CHARPOS (*it)));
8877 line_height = it->max_ascent + it->max_descent;
8878 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8879
8880 if (to_y >= it->current_y
8881 && to_y < it->current_y + line_height)
8882 {
8883 /* If TO_Y is in this line and TO_X was reached
8884 above, we scanned too far. We have to restore
8885 IT's settings to the ones before skipping. But
8886 keep the more accurate values of max_ascent and
8887 max_descent we've found while skipping the rest
8888 of the line, for the sake of callers, such as
8889 pos_visible_p, that need to know the line
8890 height. */
8891 int max_ascent = it->max_ascent;
8892 int max_descent = it->max_descent;
8893
8894 RESTORE_IT (it, &it_backup, backup_data);
8895 it->max_ascent = max_ascent;
8896 it->max_descent = max_descent;
8897 reached = 6;
8898 }
8899 else
8900 {
8901 skip = skip2;
8902 if (skip == MOVE_POS_MATCH_OR_ZV)
8903 reached = 7;
8904 }
8905 }
8906 else
8907 {
8908 /* Check whether TO_Y is in this line. */
8909 line_height = it->max_ascent + it->max_descent;
8910 TRACE_MOVE ((stderr, "move_it: line_height = %d\n", line_height));
8911
8912 if (to_y >= it->current_y
8913 && to_y < it->current_y + line_height)
8914 {
8915 /* When word-wrap is on, TO_X may lie past the end
8916 of a wrapped line. Then it->current is the
8917 character on the next line, so backtrack to the
8918 space before the wrap point. */
8919 if (skip == MOVE_LINE_CONTINUED
8920 && it->line_wrap == WORD_WRAP)
8921 {
8922 int prev_x = max (it->current_x - 1, 0);
8923 RESTORE_IT (it, &it_backup, backup_data);
8924 skip = move_it_in_display_line_to
8925 (it, -1, prev_x, MOVE_TO_X);
8926 }
8927 reached = 6;
8928 }
8929 }
8930
8931 if (reached)
8932 break;
8933 }
8934 else if (BUFFERP (it->object)
8935 && (it->method == GET_FROM_BUFFER
8936 || it->method == GET_FROM_STRETCH)
8937 && IT_CHARPOS (*it) >= to_charpos
8938 /* Under bidi iteration, a call to set_iterator_to_next
8939 can scan far beyond to_charpos if the initial
8940 portion of the next line needs to be reordered. In
8941 that case, give move_it_in_display_line_to another
8942 chance below. */
8943 && !(it->bidi_p
8944 && it->bidi_it.scan_dir == -1))
8945 skip = MOVE_POS_MATCH_OR_ZV;
8946 else
8947 skip = move_it_in_display_line_to (it, to_charpos, -1, MOVE_TO_POS);
8948
8949 switch (skip)
8950 {
8951 case MOVE_POS_MATCH_OR_ZV:
8952 reached = 8;
8953 goto out;
8954
8955 case MOVE_NEWLINE_OR_CR:
8956 set_iterator_to_next (it, 1);
8957 it->continuation_lines_width = 0;
8958 break;
8959
8960 case MOVE_LINE_TRUNCATED:
8961 it->continuation_lines_width = 0;
8962 reseat_at_next_visible_line_start (it, 0);
8963 if ((op & MOVE_TO_POS) != 0
8964 && IT_CHARPOS (*it) > to_charpos)
8965 {
8966 reached = 9;
8967 goto out;
8968 }
8969 break;
8970
8971 case MOVE_LINE_CONTINUED:
8972 /* For continued lines ending in a tab, some of the glyphs
8973 associated with the tab are displayed on the current
8974 line. Since it->current_x does not include these glyphs,
8975 we use it->last_visible_x instead. */
8976 if (it->c == '\t')
8977 {
8978 it->continuation_lines_width += it->last_visible_x;
8979 /* When moving by vpos, ensure that the iterator really
8980 advances to the next line (bug#847, bug#969). Fixme:
8981 do we need to do this in other circumstances? */
8982 if (it->current_x != it->last_visible_x
8983 && (op & MOVE_TO_VPOS)
8984 && !(op & (MOVE_TO_X | MOVE_TO_POS)))
8985 {
8986 line_start_x = it->current_x + it->pixel_width
8987 - it->last_visible_x;
8988 set_iterator_to_next (it, 0);
8989 }
8990 }
8991 else
8992 it->continuation_lines_width += it->current_x;
8993 break;
8994
8995 default:
8996 emacs_abort ();
8997 }
8998
8999 /* Reset/increment for the next run. */
9000 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
9001 it->current_x = line_start_x;
9002 line_start_x = 0;
9003 it->hpos = 0;
9004 it->current_y += it->max_ascent + it->max_descent;
9005 ++it->vpos;
9006 last_height = it->max_ascent + it->max_descent;
9007 last_max_ascent = it->max_ascent;
9008 it->max_ascent = it->max_descent = 0;
9009 }
9010
9011 out:
9012
9013 /* On text terminals, we may stop at the end of a line in the middle
9014 of a multi-character glyph. If the glyph itself is continued,
9015 i.e. it is actually displayed on the next line, don't treat this
9016 stopping point as valid; move to the next line instead (unless
9017 that brings us offscreen). */
9018 if (!FRAME_WINDOW_P (it->f)
9019 && op & MOVE_TO_POS
9020 && IT_CHARPOS (*it) == to_charpos
9021 && it->what == IT_CHARACTER
9022 && it->nglyphs > 1
9023 && it->line_wrap == WINDOW_WRAP
9024 && it->current_x == it->last_visible_x - 1
9025 && it->c != '\n'
9026 && it->c != '\t'
9027 && it->vpos < XFASTINT (it->w->window_end_vpos))
9028 {
9029 it->continuation_lines_width += it->current_x;
9030 it->current_x = it->hpos = it->max_ascent = it->max_descent = 0;
9031 it->current_y += it->max_ascent + it->max_descent;
9032 ++it->vpos;
9033 last_height = it->max_ascent + it->max_descent;
9034 last_max_ascent = it->max_ascent;
9035 }
9036
9037 if (backup_data)
9038 bidi_unshelve_cache (backup_data, 1);
9039
9040 TRACE_MOVE ((stderr, "move_it_to: reached %d\n", reached));
9041 }
9042
9043
9044 /* Move iterator IT backward by a specified y-distance DY, DY >= 0.
9045
9046 If DY > 0, move IT backward at least that many pixels. DY = 0
9047 means move IT backward to the preceding line start or BEGV. This
9048 function may move over more than DY pixels if IT->current_y - DY
9049 ends up in the middle of a line; in this case IT->current_y will be
9050 set to the top of the line moved to. */
9051
9052 void
9053 move_it_vertically_backward (struct it *it, int dy)
9054 {
9055 int nlines, h;
9056 struct it it2, it3;
9057 void *it2data = NULL, *it3data = NULL;
9058 ptrdiff_t start_pos;
9059
9060 move_further_back:
9061 eassert (dy >= 0);
9062
9063 start_pos = IT_CHARPOS (*it);
9064
9065 /* Estimate how many newlines we must move back. */
9066 nlines = max (1, dy / FRAME_LINE_HEIGHT (it->f));
9067
9068 /* Set the iterator's position that many lines back. */
9069 while (nlines-- && IT_CHARPOS (*it) > BEGV)
9070 back_to_previous_visible_line_start (it);
9071
9072 /* Reseat the iterator here. When moving backward, we don't want
9073 reseat to skip forward over invisible text, set up the iterator
9074 to deliver from overlay strings at the new position etc. So,
9075 use reseat_1 here. */
9076 reseat_1 (it, it->current.pos, 1);
9077
9078 /* We are now surely at a line start. */
9079 it->current_x = it->hpos = 0; /* FIXME: this is incorrect when bidi
9080 reordering is in effect. */
9081 it->continuation_lines_width = 0;
9082
9083 /* Move forward and see what y-distance we moved. First move to the
9084 start of the next line so that we get its height. We need this
9085 height to be able to tell whether we reached the specified
9086 y-distance. */
9087 SAVE_IT (it2, *it, it2data);
9088 it2.max_ascent = it2.max_descent = 0;
9089 do
9090 {
9091 move_it_to (&it2, start_pos, -1, -1, it2.vpos + 1,
9092 MOVE_TO_POS | MOVE_TO_VPOS);
9093 }
9094 while (!(IT_POS_VALID_AFTER_MOVE_P (&it2)
9095 /* If we are in a display string which starts at START_POS,
9096 and that display string includes a newline, and we are
9097 right after that newline (i.e. at the beginning of a
9098 display line), exit the loop, because otherwise we will
9099 infloop, since move_it_to will see that it is already at
9100 START_POS and will not move. */
9101 || (it2.method == GET_FROM_STRING
9102 && IT_CHARPOS (it2) == start_pos
9103 && SREF (it2.string, IT_STRING_BYTEPOS (it2) - 1) == '\n')));
9104 eassert (IT_CHARPOS (*it) >= BEGV);
9105 SAVE_IT (it3, it2, it3data);
9106
9107 move_it_to (&it2, start_pos, -1, -1, -1, MOVE_TO_POS);
9108 eassert (IT_CHARPOS (*it) >= BEGV);
9109 /* H is the actual vertical distance from the position in *IT
9110 and the starting position. */
9111 h = it2.current_y - it->current_y;
9112 /* NLINES is the distance in number of lines. */
9113 nlines = it2.vpos - it->vpos;
9114
9115 /* Correct IT's y and vpos position
9116 so that they are relative to the starting point. */
9117 it->vpos -= nlines;
9118 it->current_y -= h;
9119
9120 if (dy == 0)
9121 {
9122 /* DY == 0 means move to the start of the screen line. The
9123 value of nlines is > 0 if continuation lines were involved,
9124 or if the original IT position was at start of a line. */
9125 RESTORE_IT (it, it, it2data);
9126 if (nlines > 0)
9127 move_it_by_lines (it, nlines);
9128 /* The above code moves us to some position NLINES down,
9129 usually to its first glyph (leftmost in an L2R line), but
9130 that's not necessarily the start of the line, under bidi
9131 reordering. We want to get to the character position
9132 that is immediately after the newline of the previous
9133 line. */
9134 if (it->bidi_p
9135 && !it->continuation_lines_width
9136 && !STRINGP (it->string)
9137 && IT_CHARPOS (*it) > BEGV
9138 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9139 {
9140 ptrdiff_t nl_pos =
9141 find_next_newline_no_quit (IT_CHARPOS (*it) - 1, -1);
9142
9143 move_it_to (it, nl_pos, -1, -1, -1, MOVE_TO_POS);
9144 }
9145 bidi_unshelve_cache (it3data, 1);
9146 }
9147 else
9148 {
9149 /* The y-position we try to reach, relative to *IT.
9150 Note that H has been subtracted in front of the if-statement. */
9151 int target_y = it->current_y + h - dy;
9152 int y0 = it3.current_y;
9153 int y1;
9154 int line_height;
9155
9156 RESTORE_IT (&it3, &it3, it3data);
9157 y1 = line_bottom_y (&it3);
9158 line_height = y1 - y0;
9159 RESTORE_IT (it, it, it2data);
9160 /* If we did not reach target_y, try to move further backward if
9161 we can. If we moved too far backward, try to move forward. */
9162 if (target_y < it->current_y
9163 /* This is heuristic. In a window that's 3 lines high, with
9164 a line height of 13 pixels each, recentering with point
9165 on the bottom line will try to move -39/2 = 19 pixels
9166 backward. Try to avoid moving into the first line. */
9167 && (it->current_y - target_y
9168 > min (window_box_height (it->w), line_height * 2 / 3))
9169 && IT_CHARPOS (*it) > BEGV)
9170 {
9171 TRACE_MOVE ((stderr, " not far enough -> move_vert %d\n",
9172 target_y - it->current_y));
9173 dy = it->current_y - target_y;
9174 goto move_further_back;
9175 }
9176 else if (target_y >= it->current_y + line_height
9177 && IT_CHARPOS (*it) < ZV)
9178 {
9179 /* Should move forward by at least one line, maybe more.
9180
9181 Note: Calling move_it_by_lines can be expensive on
9182 terminal frames, where compute_motion is used (via
9183 vmotion) to do the job, when there are very long lines
9184 and truncate-lines is nil. That's the reason for
9185 treating terminal frames specially here. */
9186
9187 if (!FRAME_WINDOW_P (it->f))
9188 move_it_vertically (it, target_y - (it->current_y + line_height));
9189 else
9190 {
9191 do
9192 {
9193 move_it_by_lines (it, 1);
9194 }
9195 while (target_y >= line_bottom_y (it) && IT_CHARPOS (*it) < ZV);
9196 }
9197 }
9198 }
9199 }
9200
9201
9202 /* Move IT by a specified amount of pixel lines DY. DY negative means
9203 move backwards. DY = 0 means move to start of screen line. At the
9204 end, IT will be on the start of a screen line. */
9205
9206 void
9207 move_it_vertically (struct it *it, int dy)
9208 {
9209 if (dy <= 0)
9210 move_it_vertically_backward (it, -dy);
9211 else
9212 {
9213 TRACE_MOVE ((stderr, "move_it_v: from %d, %d\n", IT_CHARPOS (*it), dy));
9214 move_it_to (it, ZV, -1, it->current_y + dy, -1,
9215 MOVE_TO_POS | MOVE_TO_Y);
9216 TRACE_MOVE ((stderr, "move_it_v: to %d\n", IT_CHARPOS (*it)));
9217
9218 /* If buffer ends in ZV without a newline, move to the start of
9219 the line to satisfy the post-condition. */
9220 if (IT_CHARPOS (*it) == ZV
9221 && ZV > BEGV
9222 && FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n')
9223 move_it_by_lines (it, 0);
9224 }
9225 }
9226
9227
9228 /* Move iterator IT past the end of the text line it is in. */
9229
9230 void
9231 move_it_past_eol (struct it *it)
9232 {
9233 enum move_it_result rc;
9234
9235 rc = move_it_in_display_line_to (it, Z, 0, MOVE_TO_POS);
9236 if (rc == MOVE_NEWLINE_OR_CR)
9237 set_iterator_to_next (it, 0);
9238 }
9239
9240
9241 /* Move IT by a specified number DVPOS of screen lines down. DVPOS
9242 negative means move up. DVPOS == 0 means move to the start of the
9243 screen line.
9244
9245 Optimization idea: If we would know that IT->f doesn't use
9246 a face with proportional font, we could be faster for
9247 truncate-lines nil. */
9248
9249 void
9250 move_it_by_lines (struct it *it, ptrdiff_t dvpos)
9251 {
9252
9253 /* The commented-out optimization uses vmotion on terminals. This
9254 gives bad results, because elements like it->what, on which
9255 callers such as pos_visible_p rely, aren't updated. */
9256 /* struct position pos;
9257 if (!FRAME_WINDOW_P (it->f))
9258 {
9259 struct text_pos textpos;
9260
9261 pos = *vmotion (IT_CHARPOS (*it), dvpos, it->w);
9262 SET_TEXT_POS (textpos, pos.bufpos, pos.bytepos);
9263 reseat (it, textpos, 1);
9264 it->vpos += pos.vpos;
9265 it->current_y += pos.vpos;
9266 }
9267 else */
9268
9269 if (dvpos == 0)
9270 {
9271 /* DVPOS == 0 means move to the start of the screen line. */
9272 move_it_vertically_backward (it, 0);
9273 /* Let next call to line_bottom_y calculate real line height */
9274 last_height = 0;
9275 }
9276 else if (dvpos > 0)
9277 {
9278 move_it_to (it, -1, -1, -1, it->vpos + dvpos, MOVE_TO_VPOS);
9279 if (!IT_POS_VALID_AFTER_MOVE_P (it))
9280 {
9281 /* Only move to the next buffer position if we ended up in a
9282 string from display property, not in an overlay string
9283 (before-string or after-string). That is because the
9284 latter don't conceal the underlying buffer position, so
9285 we can ask to move the iterator to the exact position we
9286 are interested in. Note that, even if we are already at
9287 IT_CHARPOS (*it), the call below is not a no-op, as it
9288 will detect that we are at the end of the string, pop the
9289 iterator, and compute it->current_x and it->hpos
9290 correctly. */
9291 move_it_to (it, IT_CHARPOS (*it) + it->string_from_display_prop_p,
9292 -1, -1, -1, MOVE_TO_POS);
9293 }
9294 }
9295 else
9296 {
9297 struct it it2;
9298 void *it2data = NULL;
9299 ptrdiff_t start_charpos, i;
9300
9301 /* Start at the beginning of the screen line containing IT's
9302 position. This may actually move vertically backwards,
9303 in case of overlays, so adjust dvpos accordingly. */
9304 dvpos += it->vpos;
9305 move_it_vertically_backward (it, 0);
9306 dvpos -= it->vpos;
9307
9308 /* Go back -DVPOS visible lines and reseat the iterator there. */
9309 start_charpos = IT_CHARPOS (*it);
9310 for (i = -dvpos; i > 0 && IT_CHARPOS (*it) > BEGV; --i)
9311 back_to_previous_visible_line_start (it);
9312 reseat (it, it->current.pos, 1);
9313
9314 /* Move further back if we end up in a string or an image. */
9315 while (!IT_POS_VALID_AFTER_MOVE_P (it))
9316 {
9317 /* First try to move to start of display line. */
9318 dvpos += it->vpos;
9319 move_it_vertically_backward (it, 0);
9320 dvpos -= it->vpos;
9321 if (IT_POS_VALID_AFTER_MOVE_P (it))
9322 break;
9323 /* If start of line is still in string or image,
9324 move further back. */
9325 back_to_previous_visible_line_start (it);
9326 reseat (it, it->current.pos, 1);
9327 dvpos--;
9328 }
9329
9330 it->current_x = it->hpos = 0;
9331
9332 /* Above call may have moved too far if continuation lines
9333 are involved. Scan forward and see if it did. */
9334 SAVE_IT (it2, *it, it2data);
9335 it2.vpos = it2.current_y = 0;
9336 move_it_to (&it2, start_charpos, -1, -1, -1, MOVE_TO_POS);
9337 it->vpos -= it2.vpos;
9338 it->current_y -= it2.current_y;
9339 it->current_x = it->hpos = 0;
9340
9341 /* If we moved too far back, move IT some lines forward. */
9342 if (it2.vpos > -dvpos)
9343 {
9344 int delta = it2.vpos + dvpos;
9345
9346 RESTORE_IT (&it2, &it2, it2data);
9347 SAVE_IT (it2, *it, it2data);
9348 move_it_to (it, -1, -1, -1, it->vpos + delta, MOVE_TO_VPOS);
9349 /* Move back again if we got too far ahead. */
9350 if (IT_CHARPOS (*it) >= start_charpos)
9351 RESTORE_IT (it, &it2, it2data);
9352 else
9353 bidi_unshelve_cache (it2data, 1);
9354 }
9355 else
9356 RESTORE_IT (it, it, it2data);
9357 }
9358 }
9359
9360 /* Return 1 if IT points into the middle of a display vector. */
9361
9362 int
9363 in_display_vector_p (struct it *it)
9364 {
9365 return (it->method == GET_FROM_DISPLAY_VECTOR
9366 && it->current.dpvec_index > 0
9367 && it->dpvec + it->current.dpvec_index != it->dpend);
9368 }
9369
9370 \f
9371 /***********************************************************************
9372 Messages
9373 ***********************************************************************/
9374
9375
9376 /* Add a message with format string FORMAT and arguments ARG1 and ARG2
9377 to *Messages*. */
9378
9379 void
9380 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
9381 {
9382 Lisp_Object args[3];
9383 Lisp_Object msg, fmt;
9384 char *buffer;
9385 ptrdiff_t len;
9386 struct gcpro gcpro1, gcpro2, gcpro3, gcpro4;
9387 USE_SAFE_ALLOCA;
9388
9389 fmt = msg = Qnil;
9390 GCPRO4 (fmt, msg, arg1, arg2);
9391
9392 args[0] = fmt = build_string (format);
9393 args[1] = arg1;
9394 args[2] = arg2;
9395 msg = Fformat (3, args);
9396
9397 len = SBYTES (msg) + 1;
9398 buffer = SAFE_ALLOCA (len);
9399 memcpy (buffer, SDATA (msg), len);
9400
9401 message_dolog (buffer, len - 1, 1, 0);
9402 SAFE_FREE ();
9403
9404 UNGCPRO;
9405 }
9406
9407
9408 /* Output a newline in the *Messages* buffer if "needs" one. */
9409
9410 void
9411 message_log_maybe_newline (void)
9412 {
9413 if (message_log_need_newline)
9414 message_dolog ("", 0, 1, 0);
9415 }
9416
9417
9418 /* Add a string M of length NBYTES to the message log, optionally
9419 terminated with a newline when NLFLAG is non-zero. MULTIBYTE, if
9420 nonzero, means interpret the contents of M as multibyte. This
9421 function calls low-level routines in order to bypass text property
9422 hooks, etc. which might not be safe to run.
9423
9424 This may GC (insert may run before/after change hooks),
9425 so the buffer M must NOT point to a Lisp string. */
9426
9427 void
9428 message_dolog (const char *m, ptrdiff_t nbytes, int nlflag, int multibyte)
9429 {
9430 const unsigned char *msg = (const unsigned char *) m;
9431
9432 if (!NILP (Vmemory_full))
9433 return;
9434
9435 if (!NILP (Vmessage_log_max))
9436 {
9437 struct buffer *oldbuf;
9438 Lisp_Object oldpoint, oldbegv, oldzv;
9439 int old_windows_or_buffers_changed = windows_or_buffers_changed;
9440 ptrdiff_t point_at_end = 0;
9441 ptrdiff_t zv_at_end = 0;
9442 Lisp_Object old_deactivate_mark, tem;
9443 struct gcpro gcpro1;
9444
9445 old_deactivate_mark = Vdeactivate_mark;
9446 oldbuf = current_buffer;
9447 Fset_buffer (Fget_buffer_create (Vmessages_buffer_name));
9448 bset_undo_list (current_buffer, Qt);
9449
9450 oldpoint = message_dolog_marker1;
9451 set_marker_restricted (oldpoint, make_number (PT), Qnil);
9452 oldbegv = message_dolog_marker2;
9453 set_marker_restricted (oldbegv, make_number (BEGV), Qnil);
9454 oldzv = message_dolog_marker3;
9455 set_marker_restricted (oldzv, make_number (ZV), Qnil);
9456 GCPRO1 (old_deactivate_mark);
9457
9458 if (PT == Z)
9459 point_at_end = 1;
9460 if (ZV == Z)
9461 zv_at_end = 1;
9462
9463 BEGV = BEG;
9464 BEGV_BYTE = BEG_BYTE;
9465 ZV = Z;
9466 ZV_BYTE = Z_BYTE;
9467 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9468
9469 /* Insert the string--maybe converting multibyte to single byte
9470 or vice versa, so that all the text fits the buffer. */
9471 if (multibyte
9472 && NILP (BVAR (current_buffer, enable_multibyte_characters)))
9473 {
9474 ptrdiff_t i;
9475 int c, char_bytes;
9476 char work[1];
9477
9478 /* Convert a multibyte string to single-byte
9479 for the *Message* buffer. */
9480 for (i = 0; i < nbytes; i += char_bytes)
9481 {
9482 c = string_char_and_length (msg + i, &char_bytes);
9483 work[0] = (ASCII_CHAR_P (c)
9484 ? c
9485 : multibyte_char_to_unibyte (c));
9486 insert_1_both (work, 1, 1, 1, 0, 0);
9487 }
9488 }
9489 else if (! multibyte
9490 && ! NILP (BVAR (current_buffer, enable_multibyte_characters)))
9491 {
9492 ptrdiff_t i;
9493 int c, char_bytes;
9494 unsigned char str[MAX_MULTIBYTE_LENGTH];
9495 /* Convert a single-byte string to multibyte
9496 for the *Message* buffer. */
9497 for (i = 0; i < nbytes; i++)
9498 {
9499 c = msg[i];
9500 MAKE_CHAR_MULTIBYTE (c);
9501 char_bytes = CHAR_STRING (c, str);
9502 insert_1_both ((char *) str, 1, char_bytes, 1, 0, 0);
9503 }
9504 }
9505 else if (nbytes)
9506 insert_1 (m, nbytes, 1, 0, 0);
9507
9508 if (nlflag)
9509 {
9510 ptrdiff_t this_bol, this_bol_byte, prev_bol, prev_bol_byte;
9511 printmax_t dups;
9512 insert_1 ("\n", 1, 1, 0, 0);
9513
9514 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE, -2, 0);
9515 this_bol = PT;
9516 this_bol_byte = PT_BYTE;
9517
9518 /* See if this line duplicates the previous one.
9519 If so, combine duplicates. */
9520 if (this_bol > BEG)
9521 {
9522 scan_newline (PT, PT_BYTE, BEG, BEG_BYTE, -2, 0);
9523 prev_bol = PT;
9524 prev_bol_byte = PT_BYTE;
9525
9526 dups = message_log_check_duplicate (prev_bol_byte,
9527 this_bol_byte);
9528 if (dups)
9529 {
9530 del_range_both (prev_bol, prev_bol_byte,
9531 this_bol, this_bol_byte, 0);
9532 if (dups > 1)
9533 {
9534 char dupstr[sizeof " [ times]"
9535 + INT_STRLEN_BOUND (printmax_t)];
9536
9537 /* If you change this format, don't forget to also
9538 change message_log_check_duplicate. */
9539 int duplen = sprintf (dupstr, " [%"pMd" times]", dups);
9540 TEMP_SET_PT_BOTH (Z - 1, Z_BYTE - 1);
9541 insert_1 (dupstr, duplen, 1, 0, 1);
9542 }
9543 }
9544 }
9545
9546 /* If we have more than the desired maximum number of lines
9547 in the *Messages* buffer now, delete the oldest ones.
9548 This is safe because we don't have undo in this buffer. */
9549
9550 if (NATNUMP (Vmessage_log_max))
9551 {
9552 scan_newline (Z, Z_BYTE, BEG, BEG_BYTE,
9553 -XFASTINT (Vmessage_log_max) - 1, 0);
9554 del_range_both (BEG, BEG_BYTE, PT, PT_BYTE, 0);
9555 }
9556 }
9557 BEGV = XMARKER (oldbegv)->charpos;
9558 BEGV_BYTE = marker_byte_position (oldbegv);
9559
9560 if (zv_at_end)
9561 {
9562 ZV = Z;
9563 ZV_BYTE = Z_BYTE;
9564 }
9565 else
9566 {
9567 ZV = XMARKER (oldzv)->charpos;
9568 ZV_BYTE = marker_byte_position (oldzv);
9569 }
9570
9571 if (point_at_end)
9572 TEMP_SET_PT_BOTH (Z, Z_BYTE);
9573 else
9574 /* We can't do Fgoto_char (oldpoint) because it will run some
9575 Lisp code. */
9576 TEMP_SET_PT_BOTH (XMARKER (oldpoint)->charpos,
9577 XMARKER (oldpoint)->bytepos);
9578
9579 UNGCPRO;
9580 unchain_marker (XMARKER (oldpoint));
9581 unchain_marker (XMARKER (oldbegv));
9582 unchain_marker (XMARKER (oldzv));
9583
9584 tem = Fget_buffer_window (Fcurrent_buffer (), Qt);
9585 set_buffer_internal (oldbuf);
9586 if (NILP (tem))
9587 windows_or_buffers_changed = old_windows_or_buffers_changed;
9588 message_log_need_newline = !nlflag;
9589 Vdeactivate_mark = old_deactivate_mark;
9590 }
9591 }
9592
9593
9594 /* We are at the end of the buffer after just having inserted a newline.
9595 (Note: We depend on the fact we won't be crossing the gap.)
9596 Check to see if the most recent message looks a lot like the previous one.
9597 Return 0 if different, 1 if the new one should just replace it, or a
9598 value N > 1 if we should also append " [N times]". */
9599
9600 static intmax_t
9601 message_log_check_duplicate (ptrdiff_t prev_bol_byte, ptrdiff_t this_bol_byte)
9602 {
9603 ptrdiff_t i;
9604 ptrdiff_t len = Z_BYTE - 1 - this_bol_byte;
9605 int seen_dots = 0;
9606 unsigned char *p1 = BUF_BYTE_ADDRESS (current_buffer, prev_bol_byte);
9607 unsigned char *p2 = BUF_BYTE_ADDRESS (current_buffer, this_bol_byte);
9608
9609 for (i = 0; i < len; i++)
9610 {
9611 if (i >= 3 && p1[i-3] == '.' && p1[i-2] == '.' && p1[i-1] == '.')
9612 seen_dots = 1;
9613 if (p1[i] != p2[i])
9614 return seen_dots;
9615 }
9616 p1 += len;
9617 if (*p1 == '\n')
9618 return 2;
9619 if (*p1++ == ' ' && *p1++ == '[')
9620 {
9621 char *pend;
9622 intmax_t n = strtoimax ((char *) p1, &pend, 10);
9623 if (0 < n && n < INTMAX_MAX && strncmp (pend, " times]\n", 8) == 0)
9624 return n+1;
9625 }
9626 return 0;
9627 }
9628 \f
9629
9630 /* Display an echo area message M with a specified length of NBYTES
9631 bytes. The string may include null characters. If M is 0, clear
9632 out any existing message, and let the mini-buffer text show
9633 through.
9634
9635 This may GC, so the buffer M must NOT point to a Lisp string. */
9636
9637 void
9638 message2 (const char *m, ptrdiff_t nbytes, int multibyte)
9639 {
9640 /* First flush out any partial line written with print. */
9641 message_log_maybe_newline ();
9642 if (m)
9643 message_dolog (m, nbytes, 1, multibyte);
9644 message2_nolog (m, nbytes, multibyte);
9645 }
9646
9647
9648 /* The non-logging counterpart of message2. */
9649
9650 void
9651 message2_nolog (const char *m, ptrdiff_t nbytes, int multibyte)
9652 {
9653 struct frame *sf = SELECTED_FRAME ();
9654 message_enable_multibyte = multibyte;
9655
9656 if (FRAME_INITIAL_P (sf))
9657 {
9658 if (noninteractive_need_newline)
9659 putc ('\n', stderr);
9660 noninteractive_need_newline = 0;
9661 if (m)
9662 fwrite (m, nbytes, 1, stderr);
9663 if (cursor_in_echo_area == 0)
9664 fprintf (stderr, "\n");
9665 fflush (stderr);
9666 }
9667 /* A null message buffer means that the frame hasn't really been
9668 initialized yet. Error messages get reported properly by
9669 cmd_error, so this must be just an informative message; toss it. */
9670 else if (INTERACTIVE
9671 && sf->glyphs_initialized_p
9672 && FRAME_MESSAGE_BUF (sf))
9673 {
9674 Lisp_Object mini_window;
9675 struct frame *f;
9676
9677 /* Get the frame containing the mini-buffer
9678 that the selected frame is using. */
9679 mini_window = FRAME_MINIBUF_WINDOW (sf);
9680 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9681
9682 FRAME_SAMPLE_VISIBILITY (f);
9683 if (FRAME_VISIBLE_P (sf)
9684 && ! FRAME_VISIBLE_P (f))
9685 Fmake_frame_visible (WINDOW_FRAME (XWINDOW (mini_window)));
9686
9687 if (m)
9688 {
9689 set_message (m, Qnil, nbytes, multibyte);
9690 if (minibuffer_auto_raise)
9691 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
9692 }
9693 else
9694 clear_message (1, 1);
9695
9696 do_pending_window_change (0);
9697 echo_area_display (1);
9698 do_pending_window_change (0);
9699 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9700 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9701 }
9702 }
9703
9704
9705 /* Display an echo area message M with a specified length of NBYTES
9706 bytes. The string may include null characters. If M is not a
9707 string, clear out any existing message, and let the mini-buffer
9708 text show through.
9709
9710 This function cancels echoing. */
9711
9712 void
9713 message3 (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9714 {
9715 struct gcpro gcpro1;
9716
9717 GCPRO1 (m);
9718 clear_message (1,1);
9719 cancel_echoing ();
9720
9721 /* First flush out any partial line written with print. */
9722 message_log_maybe_newline ();
9723 if (STRINGP (m))
9724 {
9725 USE_SAFE_ALLOCA;
9726 char *buffer = SAFE_ALLOCA (nbytes);
9727 memcpy (buffer, SDATA (m), nbytes);
9728 message_dolog (buffer, nbytes, 1, multibyte);
9729 SAFE_FREE ();
9730 }
9731 message3_nolog (m, nbytes, multibyte);
9732
9733 UNGCPRO;
9734 }
9735
9736
9737 /* The non-logging version of message3.
9738 This does not cancel echoing, because it is used for echoing.
9739 Perhaps we need to make a separate function for echoing
9740 and make this cancel echoing. */
9741
9742 void
9743 message3_nolog (Lisp_Object m, ptrdiff_t nbytes, int multibyte)
9744 {
9745 struct frame *sf = SELECTED_FRAME ();
9746 message_enable_multibyte = multibyte;
9747
9748 if (FRAME_INITIAL_P (sf))
9749 {
9750 if (noninteractive_need_newline)
9751 putc ('\n', stderr);
9752 noninteractive_need_newline = 0;
9753 if (STRINGP (m))
9754 fwrite (SDATA (m), nbytes, 1, stderr);
9755 if (cursor_in_echo_area == 0)
9756 fprintf (stderr, "\n");
9757 fflush (stderr);
9758 }
9759 /* A null message buffer means that the frame hasn't really been
9760 initialized yet. Error messages get reported properly by
9761 cmd_error, so this must be just an informative message; toss it. */
9762 else if (INTERACTIVE
9763 && sf->glyphs_initialized_p
9764 && FRAME_MESSAGE_BUF (sf))
9765 {
9766 Lisp_Object mini_window;
9767 Lisp_Object frame;
9768 struct frame *f;
9769
9770 /* Get the frame containing the mini-buffer
9771 that the selected frame is using. */
9772 mini_window = FRAME_MINIBUF_WINDOW (sf);
9773 frame = XWINDOW (mini_window)->frame;
9774 f = XFRAME (frame);
9775
9776 FRAME_SAMPLE_VISIBILITY (f);
9777 if (FRAME_VISIBLE_P (sf)
9778 && !FRAME_VISIBLE_P (f))
9779 Fmake_frame_visible (frame);
9780
9781 if (STRINGP (m) && SCHARS (m) > 0)
9782 {
9783 set_message (NULL, m, nbytes, multibyte);
9784 if (minibuffer_auto_raise)
9785 Fraise_frame (frame);
9786 /* Assume we are not echoing.
9787 (If we are, echo_now will override this.) */
9788 echo_message_buffer = Qnil;
9789 }
9790 else
9791 clear_message (1, 1);
9792
9793 do_pending_window_change (0);
9794 echo_area_display (1);
9795 do_pending_window_change (0);
9796 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
9797 (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
9798 }
9799 }
9800
9801
9802 /* Display a null-terminated echo area message M. If M is 0, clear
9803 out any existing message, and let the mini-buffer text show through.
9804
9805 The buffer M must continue to exist until after the echo area gets
9806 cleared or some other message gets displayed there. Do not pass
9807 text that is stored in a Lisp string. Do not pass text in a buffer
9808 that was alloca'd. */
9809
9810 void
9811 message1 (const char *m)
9812 {
9813 message2 (m, (m ? strlen (m) : 0), 0);
9814 }
9815
9816
9817 /* The non-logging counterpart of message1. */
9818
9819 void
9820 message1_nolog (const char *m)
9821 {
9822 message2_nolog (m, (m ? strlen (m) : 0), 0);
9823 }
9824
9825 /* Display a message M which contains a single %s
9826 which gets replaced with STRING. */
9827
9828 void
9829 message_with_string (const char *m, Lisp_Object string, int log)
9830 {
9831 CHECK_STRING (string);
9832
9833 if (noninteractive)
9834 {
9835 if (m)
9836 {
9837 if (noninteractive_need_newline)
9838 putc ('\n', stderr);
9839 noninteractive_need_newline = 0;
9840 fprintf (stderr, m, SDATA (string));
9841 if (!cursor_in_echo_area)
9842 fprintf (stderr, "\n");
9843 fflush (stderr);
9844 }
9845 }
9846 else if (INTERACTIVE)
9847 {
9848 /* The frame whose minibuffer we're going to display the message on.
9849 It may be larger than the selected frame, so we need
9850 to use its buffer, not the selected frame's buffer. */
9851 Lisp_Object mini_window;
9852 struct frame *f, *sf = SELECTED_FRAME ();
9853
9854 /* Get the frame containing the minibuffer
9855 that the selected frame is using. */
9856 mini_window = FRAME_MINIBUF_WINDOW (sf);
9857 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9858
9859 /* A null message buffer means that the frame hasn't really been
9860 initialized yet. Error messages get reported properly by
9861 cmd_error, so this must be just an informative message; toss it. */
9862 if (FRAME_MESSAGE_BUF (f))
9863 {
9864 Lisp_Object args[2], msg;
9865 struct gcpro gcpro1, gcpro2;
9866
9867 args[0] = build_string (m);
9868 args[1] = msg = string;
9869 GCPRO2 (args[0], msg);
9870 gcpro1.nvars = 2;
9871
9872 msg = Fformat (2, args);
9873
9874 if (log)
9875 message3 (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9876 else
9877 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
9878
9879 UNGCPRO;
9880
9881 /* Print should start at the beginning of the message
9882 buffer next time. */
9883 message_buf_print = 0;
9884 }
9885 }
9886 }
9887
9888
9889 /* Dump an informative message to the minibuf. If M is 0, clear out
9890 any existing message, and let the mini-buffer text show through. */
9891
9892 static void
9893 vmessage (const char *m, va_list ap)
9894 {
9895 if (noninteractive)
9896 {
9897 if (m)
9898 {
9899 if (noninteractive_need_newline)
9900 putc ('\n', stderr);
9901 noninteractive_need_newline = 0;
9902 vfprintf (stderr, m, ap);
9903 if (cursor_in_echo_area == 0)
9904 fprintf (stderr, "\n");
9905 fflush (stderr);
9906 }
9907 }
9908 else if (INTERACTIVE)
9909 {
9910 /* The frame whose mini-buffer we're going to display the message
9911 on. It may be larger than the selected frame, so we need to
9912 use its buffer, not the selected frame's buffer. */
9913 Lisp_Object mini_window;
9914 struct frame *f, *sf = SELECTED_FRAME ();
9915
9916 /* Get the frame containing the mini-buffer
9917 that the selected frame is using. */
9918 mini_window = FRAME_MINIBUF_WINDOW (sf);
9919 f = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
9920
9921 /* A null message buffer means that the frame hasn't really been
9922 initialized yet. Error messages get reported properly by
9923 cmd_error, so this must be just an informative message; toss
9924 it. */
9925 if (FRAME_MESSAGE_BUF (f))
9926 {
9927 if (m)
9928 {
9929 ptrdiff_t len;
9930
9931 len = doprnt (FRAME_MESSAGE_BUF (f),
9932 FRAME_MESSAGE_BUF_SIZE (f), m, (char *)0, ap);
9933
9934 message2 (FRAME_MESSAGE_BUF (f), len, 1);
9935 }
9936 else
9937 message1 (0);
9938
9939 /* Print should start at the beginning of the message
9940 buffer next time. */
9941 message_buf_print = 0;
9942 }
9943 }
9944 }
9945
9946 void
9947 message (const char *m, ...)
9948 {
9949 va_list ap;
9950 va_start (ap, m);
9951 vmessage (m, ap);
9952 va_end (ap);
9953 }
9954
9955
9956 #if 0
9957 /* The non-logging version of message. */
9958
9959 void
9960 message_nolog (const char *m, ...)
9961 {
9962 Lisp_Object old_log_max;
9963 va_list ap;
9964 va_start (ap, m);
9965 old_log_max = Vmessage_log_max;
9966 Vmessage_log_max = Qnil;
9967 vmessage (m, ap);
9968 Vmessage_log_max = old_log_max;
9969 va_end (ap);
9970 }
9971 #endif
9972
9973
9974 /* Display the current message in the current mini-buffer. This is
9975 only called from error handlers in process.c, and is not time
9976 critical. */
9977
9978 void
9979 update_echo_area (void)
9980 {
9981 if (!NILP (echo_area_buffer[0]))
9982 {
9983 Lisp_Object string;
9984 string = Fcurrent_message ();
9985 message3 (string, SBYTES (string),
9986 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
9987 }
9988 }
9989
9990
9991 /* Make sure echo area buffers in `echo_buffers' are live.
9992 If they aren't, make new ones. */
9993
9994 static void
9995 ensure_echo_area_buffers (void)
9996 {
9997 int i;
9998
9999 for (i = 0; i < 2; ++i)
10000 if (!BUFFERP (echo_buffer[i])
10001 || !BUFFER_LIVE_P (XBUFFER (echo_buffer[i])))
10002 {
10003 char name[30];
10004 Lisp_Object old_buffer;
10005 int j;
10006
10007 old_buffer = echo_buffer[i];
10008 echo_buffer[i] = Fget_buffer_create
10009 (make_formatted_string (name, " *Echo Area %d*", i));
10010 bset_truncate_lines (XBUFFER (echo_buffer[i]), Qnil);
10011 /* to force word wrap in echo area -
10012 it was decided to postpone this*/
10013 /* XBUFFER (echo_buffer[i])->word_wrap = Qt; */
10014
10015 for (j = 0; j < 2; ++j)
10016 if (EQ (old_buffer, echo_area_buffer[j]))
10017 echo_area_buffer[j] = echo_buffer[i];
10018 }
10019 }
10020
10021
10022 /* Call FN with args A1..A4 with either the current or last displayed
10023 echo_area_buffer as current buffer.
10024
10025 WHICH zero means use the current message buffer
10026 echo_area_buffer[0]. If that is nil, choose a suitable buffer
10027 from echo_buffer[] and clear it.
10028
10029 WHICH > 0 means use echo_area_buffer[1]. If that is nil, choose a
10030 suitable buffer from echo_buffer[] and clear it.
10031
10032 If WHICH < 0, set echo_area_buffer[1] to echo_area_buffer[0], so
10033 that the current message becomes the last displayed one, make
10034 choose a suitable buffer for echo_area_buffer[0], and clear it.
10035
10036 Value is what FN returns. */
10037
10038 static int
10039 with_echo_area_buffer (struct window *w, int which,
10040 int (*fn) (ptrdiff_t, Lisp_Object, ptrdiff_t, ptrdiff_t),
10041 ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10042 {
10043 Lisp_Object buffer;
10044 int this_one, the_other, clear_buffer_p, rc;
10045 ptrdiff_t count = SPECPDL_INDEX ();
10046
10047 /* If buffers aren't live, make new ones. */
10048 ensure_echo_area_buffers ();
10049
10050 clear_buffer_p = 0;
10051
10052 if (which == 0)
10053 this_one = 0, the_other = 1;
10054 else if (which > 0)
10055 this_one = 1, the_other = 0;
10056 else
10057 {
10058 this_one = 0, the_other = 1;
10059 clear_buffer_p = 1;
10060
10061 /* We need a fresh one in case the current echo buffer equals
10062 the one containing the last displayed echo area message. */
10063 if (!NILP (echo_area_buffer[this_one])
10064 && EQ (echo_area_buffer[this_one], echo_area_buffer[the_other]))
10065 echo_area_buffer[this_one] = Qnil;
10066 }
10067
10068 /* Choose a suitable buffer from echo_buffer[] is we don't
10069 have one. */
10070 if (NILP (echo_area_buffer[this_one]))
10071 {
10072 echo_area_buffer[this_one]
10073 = (EQ (echo_area_buffer[the_other], echo_buffer[this_one])
10074 ? echo_buffer[the_other]
10075 : echo_buffer[this_one]);
10076 clear_buffer_p = 1;
10077 }
10078
10079 buffer = echo_area_buffer[this_one];
10080
10081 /* Don't get confused by reusing the buffer used for echoing
10082 for a different purpose. */
10083 if (echo_kboard == NULL && EQ (buffer, echo_message_buffer))
10084 cancel_echoing ();
10085
10086 record_unwind_protect (unwind_with_echo_area_buffer,
10087 with_echo_area_buffer_unwind_data (w));
10088
10089 /* Make the echo area buffer current. Note that for display
10090 purposes, it is not necessary that the displayed window's buffer
10091 == current_buffer, except for text property lookup. So, let's
10092 only set that buffer temporarily here without doing a full
10093 Fset_window_buffer. We must also change w->pointm, though,
10094 because otherwise an assertions in unshow_buffer fails, and Emacs
10095 aborts. */
10096 set_buffer_internal_1 (XBUFFER (buffer));
10097 if (w)
10098 {
10099 wset_buffer (w, buffer);
10100 set_marker_both (w->pointm, buffer, BEG, BEG_BYTE);
10101 }
10102
10103 bset_undo_list (current_buffer, Qt);
10104 bset_read_only (current_buffer, Qnil);
10105 specbind (Qinhibit_read_only, Qt);
10106 specbind (Qinhibit_modification_hooks, Qt);
10107
10108 if (clear_buffer_p && Z > BEG)
10109 del_range (BEG, Z);
10110
10111 eassert (BEGV >= BEG);
10112 eassert (ZV <= Z && ZV >= BEGV);
10113
10114 rc = fn (a1, a2, a3, a4);
10115
10116 eassert (BEGV >= BEG);
10117 eassert (ZV <= Z && ZV >= BEGV);
10118
10119 unbind_to (count, Qnil);
10120 return rc;
10121 }
10122
10123
10124 /* Save state that should be preserved around the call to the function
10125 FN called in with_echo_area_buffer. */
10126
10127 static Lisp_Object
10128 with_echo_area_buffer_unwind_data (struct window *w)
10129 {
10130 int i = 0;
10131 Lisp_Object vector, tmp;
10132
10133 /* Reduce consing by keeping one vector in
10134 Vwith_echo_area_save_vector. */
10135 vector = Vwith_echo_area_save_vector;
10136 Vwith_echo_area_save_vector = Qnil;
10137
10138 if (NILP (vector))
10139 vector = Fmake_vector (make_number (7), Qnil);
10140
10141 XSETBUFFER (tmp, current_buffer); ASET (vector, i, tmp); ++i;
10142 ASET (vector, i, Vdeactivate_mark); ++i;
10143 ASET (vector, i, make_number (windows_or_buffers_changed)); ++i;
10144
10145 if (w)
10146 {
10147 XSETWINDOW (tmp, w); ASET (vector, i, tmp); ++i;
10148 ASET (vector, i, w->buffer); ++i;
10149 ASET (vector, i, make_number (XMARKER (w->pointm)->charpos)); ++i;
10150 ASET (vector, i, make_number (XMARKER (w->pointm)->bytepos)); ++i;
10151 }
10152 else
10153 {
10154 int end = i + 4;
10155 for (; i < end; ++i)
10156 ASET (vector, i, Qnil);
10157 }
10158
10159 eassert (i == ASIZE (vector));
10160 return vector;
10161 }
10162
10163
10164 /* Restore global state from VECTOR which was created by
10165 with_echo_area_buffer_unwind_data. */
10166
10167 static Lisp_Object
10168 unwind_with_echo_area_buffer (Lisp_Object vector)
10169 {
10170 set_buffer_internal_1 (XBUFFER (AREF (vector, 0)));
10171 Vdeactivate_mark = AREF (vector, 1);
10172 windows_or_buffers_changed = XFASTINT (AREF (vector, 2));
10173
10174 if (WINDOWP (AREF (vector, 3)))
10175 {
10176 struct window *w;
10177 Lisp_Object buffer, charpos, bytepos;
10178
10179 w = XWINDOW (AREF (vector, 3));
10180 buffer = AREF (vector, 4);
10181 charpos = AREF (vector, 5);
10182 bytepos = AREF (vector, 6);
10183
10184 wset_buffer (w, buffer);
10185 set_marker_both (w->pointm, buffer,
10186 XFASTINT (charpos), XFASTINT (bytepos));
10187 }
10188
10189 Vwith_echo_area_save_vector = vector;
10190 return Qnil;
10191 }
10192
10193
10194 /* Set up the echo area for use by print functions. MULTIBYTE_P
10195 non-zero means we will print multibyte. */
10196
10197 void
10198 setup_echo_area_for_printing (int multibyte_p)
10199 {
10200 /* If we can't find an echo area any more, exit. */
10201 if (! FRAME_LIVE_P (XFRAME (selected_frame)))
10202 Fkill_emacs (Qnil);
10203
10204 ensure_echo_area_buffers ();
10205
10206 if (!message_buf_print)
10207 {
10208 /* A message has been output since the last time we printed.
10209 Choose a fresh echo area buffer. */
10210 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10211 echo_area_buffer[0] = echo_buffer[1];
10212 else
10213 echo_area_buffer[0] = echo_buffer[0];
10214
10215 /* Switch to that buffer and clear it. */
10216 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10217 bset_truncate_lines (current_buffer, Qnil);
10218
10219 if (Z > BEG)
10220 {
10221 ptrdiff_t count = SPECPDL_INDEX ();
10222 specbind (Qinhibit_read_only, Qt);
10223 /* Note that undo recording is always disabled. */
10224 del_range (BEG, Z);
10225 unbind_to (count, Qnil);
10226 }
10227 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10228
10229 /* Set up the buffer for the multibyteness we need. */
10230 if (multibyte_p
10231 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10232 Fset_buffer_multibyte (multibyte_p ? Qt : Qnil);
10233
10234 /* Raise the frame containing the echo area. */
10235 if (minibuffer_auto_raise)
10236 {
10237 struct frame *sf = SELECTED_FRAME ();
10238 Lisp_Object mini_window;
10239 mini_window = FRAME_MINIBUF_WINDOW (sf);
10240 Fraise_frame (WINDOW_FRAME (XWINDOW (mini_window)));
10241 }
10242
10243 message_log_maybe_newline ();
10244 message_buf_print = 1;
10245 }
10246 else
10247 {
10248 if (NILP (echo_area_buffer[0]))
10249 {
10250 if (EQ (echo_area_buffer[1], echo_buffer[0]))
10251 echo_area_buffer[0] = echo_buffer[1];
10252 else
10253 echo_area_buffer[0] = echo_buffer[0];
10254 }
10255
10256 if (current_buffer != XBUFFER (echo_area_buffer[0]))
10257 {
10258 /* Someone switched buffers between print requests. */
10259 set_buffer_internal (XBUFFER (echo_area_buffer[0]));
10260 bset_truncate_lines (current_buffer, Qnil);
10261 }
10262 }
10263 }
10264
10265
10266 /* Display an echo area message in window W. Value is non-zero if W's
10267 height is changed. If display_last_displayed_message_p is
10268 non-zero, display the message that was last displayed, otherwise
10269 display the current message. */
10270
10271 static int
10272 display_echo_area (struct window *w)
10273 {
10274 int i, no_message_p, window_height_changed_p;
10275
10276 /* Temporarily disable garbage collections while displaying the echo
10277 area. This is done because a GC can print a message itself.
10278 That message would modify the echo area buffer's contents while a
10279 redisplay of the buffer is going on, and seriously confuse
10280 redisplay. */
10281 ptrdiff_t count = inhibit_garbage_collection ();
10282
10283 /* If there is no message, we must call display_echo_area_1
10284 nevertheless because it resizes the window. But we will have to
10285 reset the echo_area_buffer in question to nil at the end because
10286 with_echo_area_buffer will sets it to an empty buffer. */
10287 i = display_last_displayed_message_p ? 1 : 0;
10288 no_message_p = NILP (echo_area_buffer[i]);
10289
10290 window_height_changed_p
10291 = with_echo_area_buffer (w, display_last_displayed_message_p,
10292 display_echo_area_1,
10293 (intptr_t) w, Qnil, 0, 0);
10294
10295 if (no_message_p)
10296 echo_area_buffer[i] = Qnil;
10297
10298 unbind_to (count, Qnil);
10299 return window_height_changed_p;
10300 }
10301
10302
10303 /* Helper for display_echo_area. Display the current buffer which
10304 contains the current echo area message in window W, a mini-window,
10305 a pointer to which is passed in A1. A2..A4 are currently not used.
10306 Change the height of W so that all of the message is displayed.
10307 Value is non-zero if height of W was changed. */
10308
10309 static int
10310 display_echo_area_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10311 {
10312 intptr_t i1 = a1;
10313 struct window *w = (struct window *) i1;
10314 Lisp_Object window;
10315 struct text_pos start;
10316 int window_height_changed_p = 0;
10317
10318 /* Do this before displaying, so that we have a large enough glyph
10319 matrix for the display. If we can't get enough space for the
10320 whole text, display the last N lines. That works by setting w->start. */
10321 window_height_changed_p = resize_mini_window (w, 0);
10322
10323 /* Use the starting position chosen by resize_mini_window. */
10324 SET_TEXT_POS_FROM_MARKER (start, w->start);
10325
10326 /* Display. */
10327 clear_glyph_matrix (w->desired_matrix);
10328 XSETWINDOW (window, w);
10329 try_window (window, start, 0);
10330
10331 return window_height_changed_p;
10332 }
10333
10334
10335 /* Resize the echo area window to exactly the size needed for the
10336 currently displayed message, if there is one. If a mini-buffer
10337 is active, don't shrink it. */
10338
10339 void
10340 resize_echo_area_exactly (void)
10341 {
10342 if (BUFFERP (echo_area_buffer[0])
10343 && WINDOWP (echo_area_window))
10344 {
10345 struct window *w = XWINDOW (echo_area_window);
10346 int resized_p;
10347 Lisp_Object resize_exactly;
10348
10349 if (minibuf_level == 0)
10350 resize_exactly = Qt;
10351 else
10352 resize_exactly = Qnil;
10353
10354 resized_p = with_echo_area_buffer (w, 0, resize_mini_window_1,
10355 (intptr_t) w, resize_exactly,
10356 0, 0);
10357 if (resized_p)
10358 {
10359 ++windows_or_buffers_changed;
10360 ++update_mode_lines;
10361 redisplay_internal ();
10362 }
10363 }
10364 }
10365
10366
10367 /* Callback function for with_echo_area_buffer, when used from
10368 resize_echo_area_exactly. A1 contains a pointer to the window to
10369 resize, EXACTLY non-nil means resize the mini-window exactly to the
10370 size of the text displayed. A3 and A4 are not used. Value is what
10371 resize_mini_window returns. */
10372
10373 static int
10374 resize_mini_window_1 (ptrdiff_t a1, Lisp_Object exactly, ptrdiff_t a3, ptrdiff_t a4)
10375 {
10376 intptr_t i1 = a1;
10377 return resize_mini_window ((struct window *) i1, !NILP (exactly));
10378 }
10379
10380
10381 /* Resize mini-window W to fit the size of its contents. EXACT_P
10382 means size the window exactly to the size needed. Otherwise, it's
10383 only enlarged until W's buffer is empty.
10384
10385 Set W->start to the right place to begin display. If the whole
10386 contents fit, start at the beginning. Otherwise, start so as
10387 to make the end of the contents appear. This is particularly
10388 important for y-or-n-p, but seems desirable generally.
10389
10390 Value is non-zero if the window height has been changed. */
10391
10392 int
10393 resize_mini_window (struct window *w, int exact_p)
10394 {
10395 struct frame *f = XFRAME (w->frame);
10396 int window_height_changed_p = 0;
10397
10398 eassert (MINI_WINDOW_P (w));
10399
10400 /* By default, start display at the beginning. */
10401 set_marker_both (w->start, w->buffer,
10402 BUF_BEGV (XBUFFER (w->buffer)),
10403 BUF_BEGV_BYTE (XBUFFER (w->buffer)));
10404
10405 /* Don't resize windows while redisplaying a window; it would
10406 confuse redisplay functions when the size of the window they are
10407 displaying changes from under them. Such a resizing can happen,
10408 for instance, when which-func prints a long message while
10409 we are running fontification-functions. We're running these
10410 functions with safe_call which binds inhibit-redisplay to t. */
10411 if (!NILP (Vinhibit_redisplay))
10412 return 0;
10413
10414 /* Nil means don't try to resize. */
10415 if (NILP (Vresize_mini_windows)
10416 || (FRAME_X_P (f) && FRAME_X_OUTPUT (f) == NULL))
10417 return 0;
10418
10419 if (!FRAME_MINIBUF_ONLY_P (f))
10420 {
10421 struct it it;
10422 struct window *root = XWINDOW (FRAME_ROOT_WINDOW (f));
10423 int total_height = WINDOW_TOTAL_LINES (root) + WINDOW_TOTAL_LINES (w);
10424 int height;
10425 EMACS_INT max_height;
10426 int unit = FRAME_LINE_HEIGHT (f);
10427 struct text_pos start;
10428 struct buffer *old_current_buffer = NULL;
10429
10430 if (current_buffer != XBUFFER (w->buffer))
10431 {
10432 old_current_buffer = current_buffer;
10433 set_buffer_internal (XBUFFER (w->buffer));
10434 }
10435
10436 init_iterator (&it, w, BEGV, BEGV_BYTE, NULL, DEFAULT_FACE_ID);
10437
10438 /* Compute the max. number of lines specified by the user. */
10439 if (FLOATP (Vmax_mini_window_height))
10440 max_height = XFLOATINT (Vmax_mini_window_height) * FRAME_LINES (f);
10441 else if (INTEGERP (Vmax_mini_window_height))
10442 max_height = XINT (Vmax_mini_window_height);
10443 else
10444 max_height = total_height / 4;
10445
10446 /* Correct that max. height if it's bogus. */
10447 max_height = max (1, max_height);
10448 max_height = min (total_height, max_height);
10449
10450 /* Find out the height of the text in the window. */
10451 if (it.line_wrap == TRUNCATE)
10452 height = 1;
10453 else
10454 {
10455 last_height = 0;
10456 move_it_to (&it, ZV, -1, -1, -1, MOVE_TO_POS);
10457 if (it.max_ascent == 0 && it.max_descent == 0)
10458 height = it.current_y + last_height;
10459 else
10460 height = it.current_y + it.max_ascent + it.max_descent;
10461 height -= min (it.extra_line_spacing, it.max_extra_line_spacing);
10462 height = (height + unit - 1) / unit;
10463 }
10464
10465 /* Compute a suitable window start. */
10466 if (height > max_height)
10467 {
10468 height = max_height;
10469 init_iterator (&it, w, ZV, ZV_BYTE, NULL, DEFAULT_FACE_ID);
10470 move_it_vertically_backward (&it, (height - 1) * unit);
10471 start = it.current.pos;
10472 }
10473 else
10474 SET_TEXT_POS (start, BEGV, BEGV_BYTE);
10475 SET_MARKER_FROM_TEXT_POS (w->start, start);
10476
10477 if (EQ (Vresize_mini_windows, Qgrow_only))
10478 {
10479 /* Let it grow only, until we display an empty message, in which
10480 case the window shrinks again. */
10481 if (height > WINDOW_TOTAL_LINES (w))
10482 {
10483 int old_height = WINDOW_TOTAL_LINES (w);
10484 freeze_window_starts (f, 1);
10485 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10486 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10487 }
10488 else if (height < WINDOW_TOTAL_LINES (w)
10489 && (exact_p || BEGV == ZV))
10490 {
10491 int old_height = WINDOW_TOTAL_LINES (w);
10492 freeze_window_starts (f, 0);
10493 shrink_mini_window (w);
10494 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10495 }
10496 }
10497 else
10498 {
10499 /* Always resize to exact size needed. */
10500 if (height > WINDOW_TOTAL_LINES (w))
10501 {
10502 int old_height = WINDOW_TOTAL_LINES (w);
10503 freeze_window_starts (f, 1);
10504 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10505 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10506 }
10507 else if (height < WINDOW_TOTAL_LINES (w))
10508 {
10509 int old_height = WINDOW_TOTAL_LINES (w);
10510 freeze_window_starts (f, 0);
10511 shrink_mini_window (w);
10512
10513 if (height)
10514 {
10515 freeze_window_starts (f, 1);
10516 grow_mini_window (w, height - WINDOW_TOTAL_LINES (w));
10517 }
10518
10519 window_height_changed_p = WINDOW_TOTAL_LINES (w) != old_height;
10520 }
10521 }
10522
10523 if (old_current_buffer)
10524 set_buffer_internal (old_current_buffer);
10525 }
10526
10527 return window_height_changed_p;
10528 }
10529
10530
10531 /* Value is the current message, a string, or nil if there is no
10532 current message. */
10533
10534 Lisp_Object
10535 current_message (void)
10536 {
10537 Lisp_Object msg;
10538
10539 if (!BUFFERP (echo_area_buffer[0]))
10540 msg = Qnil;
10541 else
10542 {
10543 with_echo_area_buffer (0, 0, current_message_1,
10544 (intptr_t) &msg, Qnil, 0, 0);
10545 if (NILP (msg))
10546 echo_area_buffer[0] = Qnil;
10547 }
10548
10549 return msg;
10550 }
10551
10552
10553 static int
10554 current_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10555 {
10556 intptr_t i1 = a1;
10557 Lisp_Object *msg = (Lisp_Object *) i1;
10558
10559 if (Z > BEG)
10560 *msg = make_buffer_string (BEG, Z, 1);
10561 else
10562 *msg = Qnil;
10563 return 0;
10564 }
10565
10566
10567 /* Push the current message on Vmessage_stack for later restoration
10568 by restore_message. Value is non-zero if the current message isn't
10569 empty. This is a relatively infrequent operation, so it's not
10570 worth optimizing. */
10571
10572 bool
10573 push_message (void)
10574 {
10575 Lisp_Object msg = current_message ();
10576 Vmessage_stack = Fcons (msg, Vmessage_stack);
10577 return STRINGP (msg);
10578 }
10579
10580
10581 /* Restore message display from the top of Vmessage_stack. */
10582
10583 void
10584 restore_message (void)
10585 {
10586 Lisp_Object msg;
10587
10588 eassert (CONSP (Vmessage_stack));
10589 msg = XCAR (Vmessage_stack);
10590 if (STRINGP (msg))
10591 message3_nolog (msg, SBYTES (msg), STRING_MULTIBYTE (msg));
10592 else
10593 message3_nolog (msg, 0, 0);
10594 }
10595
10596
10597 /* Handler for record_unwind_protect calling pop_message. */
10598
10599 Lisp_Object
10600 pop_message_unwind (Lisp_Object dummy)
10601 {
10602 pop_message ();
10603 return Qnil;
10604 }
10605
10606 /* Pop the top-most entry off Vmessage_stack. */
10607
10608 static void
10609 pop_message (void)
10610 {
10611 eassert (CONSP (Vmessage_stack));
10612 Vmessage_stack = XCDR (Vmessage_stack);
10613 }
10614
10615
10616 /* Check that Vmessage_stack is nil. Called from emacs.c when Emacs
10617 exits. If the stack is not empty, we have a missing pop_message
10618 somewhere. */
10619
10620 void
10621 check_message_stack (void)
10622 {
10623 if (!NILP (Vmessage_stack))
10624 emacs_abort ();
10625 }
10626
10627
10628 /* Truncate to NCHARS what will be displayed in the echo area the next
10629 time we display it---but don't redisplay it now. */
10630
10631 void
10632 truncate_echo_area (ptrdiff_t nchars)
10633 {
10634 if (nchars == 0)
10635 echo_area_buffer[0] = Qnil;
10636 /* A null message buffer means that the frame hasn't really been
10637 initialized yet. Error messages get reported properly by
10638 cmd_error, so this must be just an informative message; toss it. */
10639 else if (!noninteractive
10640 && INTERACTIVE
10641 && !NILP (echo_area_buffer[0]))
10642 {
10643 struct frame *sf = SELECTED_FRAME ();
10644 if (FRAME_MESSAGE_BUF (sf))
10645 with_echo_area_buffer (0, 0, truncate_message_1, nchars, Qnil, 0, 0);
10646 }
10647 }
10648
10649
10650 /* Helper function for truncate_echo_area. Truncate the current
10651 message to at most NCHARS characters. */
10652
10653 static int
10654 truncate_message_1 (ptrdiff_t nchars, Lisp_Object a2, ptrdiff_t a3, ptrdiff_t a4)
10655 {
10656 if (BEG + nchars < Z)
10657 del_range (BEG + nchars, Z);
10658 if (Z == BEG)
10659 echo_area_buffer[0] = Qnil;
10660 return 0;
10661 }
10662
10663 /* Set the current message to a substring of S or STRING.
10664
10665 If STRING is a Lisp string, set the message to the first NBYTES
10666 bytes from STRING. NBYTES zero means use the whole string. If
10667 STRING is multibyte, the message will be displayed multibyte.
10668
10669 If S is not null, set the message to the first LEN bytes of S. LEN
10670 zero means use the whole string. MULTIBYTE_P non-zero means S is
10671 multibyte. Display the message multibyte in that case.
10672
10673 Doesn't GC, as with_echo_area_buffer binds Qinhibit_modification_hooks
10674 to t before calling set_message_1 (which calls insert).
10675 */
10676
10677 static void
10678 set_message (const char *s, Lisp_Object string,
10679 ptrdiff_t nbytes, int multibyte_p)
10680 {
10681 message_enable_multibyte
10682 = ((s && multibyte_p)
10683 || (STRINGP (string) && STRING_MULTIBYTE (string)));
10684
10685 with_echo_area_buffer (0, -1, set_message_1,
10686 (intptr_t) s, string, nbytes, multibyte_p);
10687 message_buf_print = 0;
10688 help_echo_showing_p = 0;
10689
10690 if (STRINGP (Vdebug_on_message)
10691 && fast_string_match (Vdebug_on_message, string) >= 0)
10692 call_debugger (list2 (Qerror, string));
10693 }
10694
10695
10696 /* Helper function for set_message. Arguments have the same meaning
10697 as there, with A1 corresponding to S and A2 corresponding to STRING
10698 This function is called with the echo area buffer being
10699 current. */
10700
10701 static int
10702 set_message_1 (ptrdiff_t a1, Lisp_Object a2, ptrdiff_t nbytes, ptrdiff_t multibyte_p)
10703 {
10704 intptr_t i1 = a1;
10705 const char *s = (const char *) i1;
10706 const unsigned char *msg = (const unsigned char *) s;
10707 Lisp_Object string = a2;
10708
10709 /* Change multibyteness of the echo buffer appropriately. */
10710 if (message_enable_multibyte
10711 != !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10712 Fset_buffer_multibyte (message_enable_multibyte ? Qt : Qnil);
10713
10714 bset_truncate_lines (current_buffer, message_truncate_lines ? Qt : Qnil);
10715 if (!NILP (BVAR (current_buffer, bidi_display_reordering)))
10716 bset_bidi_paragraph_direction (current_buffer, Qleft_to_right);
10717
10718 /* Insert new message at BEG. */
10719 TEMP_SET_PT_BOTH (BEG, BEG_BYTE);
10720
10721 if (STRINGP (string))
10722 {
10723 ptrdiff_t nchars;
10724
10725 if (nbytes == 0)
10726 nbytes = SBYTES (string);
10727 nchars = string_byte_to_char (string, nbytes);
10728
10729 /* This function takes care of single/multibyte conversion. We
10730 just have to ensure that the echo area buffer has the right
10731 setting of enable_multibyte_characters. */
10732 insert_from_string (string, 0, 0, nchars, nbytes, 1);
10733 }
10734 else if (s)
10735 {
10736 if (nbytes == 0)
10737 nbytes = strlen (s);
10738
10739 if (multibyte_p && NILP (BVAR (current_buffer, enable_multibyte_characters)))
10740 {
10741 /* Convert from multi-byte to single-byte. */
10742 ptrdiff_t i;
10743 int c, n;
10744 char work[1];
10745
10746 /* Convert a multibyte string to single-byte. */
10747 for (i = 0; i < nbytes; i += n)
10748 {
10749 c = string_char_and_length (msg + i, &n);
10750 work[0] = (ASCII_CHAR_P (c)
10751 ? c
10752 : multibyte_char_to_unibyte (c));
10753 insert_1_both (work, 1, 1, 1, 0, 0);
10754 }
10755 }
10756 else if (!multibyte_p
10757 && !NILP (BVAR (current_buffer, enable_multibyte_characters)))
10758 {
10759 /* Convert from single-byte to multi-byte. */
10760 ptrdiff_t i;
10761 int c, n;
10762 unsigned char str[MAX_MULTIBYTE_LENGTH];
10763
10764 /* Convert a single-byte string to multibyte. */
10765 for (i = 0; i < nbytes; i++)
10766 {
10767 c = msg[i];
10768 MAKE_CHAR_MULTIBYTE (c);
10769 n = CHAR_STRING (c, str);
10770 insert_1_both ((char *) str, 1, n, 1, 0, 0);
10771 }
10772 }
10773 else
10774 insert_1 (s, nbytes, 1, 0, 0);
10775 }
10776
10777 return 0;
10778 }
10779
10780
10781 /* Clear messages. CURRENT_P non-zero means clear the current
10782 message. LAST_DISPLAYED_P non-zero means clear the message
10783 last displayed. */
10784
10785 void
10786 clear_message (int current_p, int last_displayed_p)
10787 {
10788 if (current_p)
10789 {
10790 echo_area_buffer[0] = Qnil;
10791 message_cleared_p = 1;
10792 }
10793
10794 if (last_displayed_p)
10795 echo_area_buffer[1] = Qnil;
10796
10797 message_buf_print = 0;
10798 }
10799
10800 /* Clear garbaged frames.
10801
10802 This function is used where the old redisplay called
10803 redraw_garbaged_frames which in turn called redraw_frame which in
10804 turn called clear_frame. The call to clear_frame was a source of
10805 flickering. I believe a clear_frame is not necessary. It should
10806 suffice in the new redisplay to invalidate all current matrices,
10807 and ensure a complete redisplay of all windows. */
10808
10809 static void
10810 clear_garbaged_frames (void)
10811 {
10812 if (frame_garbaged)
10813 {
10814 Lisp_Object tail, frame;
10815 int changed_count = 0;
10816
10817 FOR_EACH_FRAME (tail, frame)
10818 {
10819 struct frame *f = XFRAME (frame);
10820
10821 if (FRAME_VISIBLE_P (f) && FRAME_GARBAGED_P (f))
10822 {
10823 if (f->resized_p)
10824 {
10825 redraw_frame (f);
10826 f->force_flush_display_p = 1;
10827 }
10828 clear_current_matrices (f);
10829 changed_count++;
10830 f->garbaged = 0;
10831 f->resized_p = 0;
10832 }
10833 }
10834
10835 frame_garbaged = 0;
10836 if (changed_count)
10837 ++windows_or_buffers_changed;
10838 }
10839 }
10840
10841
10842 /* Redisplay the echo area of the selected frame. If UPDATE_FRAME_P
10843 is non-zero update selected_frame. Value is non-zero if the
10844 mini-windows height has been changed. */
10845
10846 static int
10847 echo_area_display (int update_frame_p)
10848 {
10849 Lisp_Object mini_window;
10850 struct window *w;
10851 struct frame *f;
10852 int window_height_changed_p = 0;
10853 struct frame *sf = SELECTED_FRAME ();
10854
10855 mini_window = FRAME_MINIBUF_WINDOW (sf);
10856 w = XWINDOW (mini_window);
10857 f = XFRAME (WINDOW_FRAME (w));
10858
10859 /* Don't display if frame is invisible or not yet initialized. */
10860 if (!FRAME_VISIBLE_P (f) || !f->glyphs_initialized_p)
10861 return 0;
10862
10863 #ifdef HAVE_WINDOW_SYSTEM
10864 /* When Emacs starts, selected_frame may be the initial terminal
10865 frame. If we let this through, a message would be displayed on
10866 the terminal. */
10867 if (FRAME_INITIAL_P (XFRAME (selected_frame)))
10868 return 0;
10869 #endif /* HAVE_WINDOW_SYSTEM */
10870
10871 /* Redraw garbaged frames. */
10872 clear_garbaged_frames ();
10873
10874 if (!NILP (echo_area_buffer[0]) || minibuf_level == 0)
10875 {
10876 echo_area_window = mini_window;
10877 window_height_changed_p = display_echo_area (w);
10878 w->must_be_updated_p = 1;
10879
10880 /* Update the display, unless called from redisplay_internal.
10881 Also don't update the screen during redisplay itself. The
10882 update will happen at the end of redisplay, and an update
10883 here could cause confusion. */
10884 if (update_frame_p && !redisplaying_p)
10885 {
10886 int n = 0;
10887
10888 /* If the display update has been interrupted by pending
10889 input, update mode lines in the frame. Due to the
10890 pending input, it might have been that redisplay hasn't
10891 been called, so that mode lines above the echo area are
10892 garbaged. This looks odd, so we prevent it here. */
10893 if (!display_completed)
10894 n = redisplay_mode_lines (FRAME_ROOT_WINDOW (f), 0);
10895
10896 if (window_height_changed_p
10897 /* Don't do this if Emacs is shutting down. Redisplay
10898 needs to run hooks. */
10899 && !NILP (Vrun_hooks))
10900 {
10901 /* Must update other windows. Likewise as in other
10902 cases, don't let this update be interrupted by
10903 pending input. */
10904 ptrdiff_t count = SPECPDL_INDEX ();
10905 specbind (Qredisplay_dont_pause, Qt);
10906 windows_or_buffers_changed = 1;
10907 redisplay_internal ();
10908 unbind_to (count, Qnil);
10909 }
10910 else if (FRAME_WINDOW_P (f) && n == 0)
10911 {
10912 /* Window configuration is the same as before.
10913 Can do with a display update of the echo area,
10914 unless we displayed some mode lines. */
10915 update_single_window (w, 1);
10916 FRAME_RIF (f)->flush_display (f);
10917 }
10918 else
10919 update_frame (f, 1, 1);
10920
10921 /* If cursor is in the echo area, make sure that the next
10922 redisplay displays the minibuffer, so that the cursor will
10923 be replaced with what the minibuffer wants. */
10924 if (cursor_in_echo_area)
10925 ++windows_or_buffers_changed;
10926 }
10927 }
10928 else if (!EQ (mini_window, selected_window))
10929 windows_or_buffers_changed++;
10930
10931 /* Last displayed message is now the current message. */
10932 echo_area_buffer[1] = echo_area_buffer[0];
10933 /* Inform read_char that we're not echoing. */
10934 echo_message_buffer = Qnil;
10935
10936 /* Prevent redisplay optimization in redisplay_internal by resetting
10937 this_line_start_pos. This is done because the mini-buffer now
10938 displays the message instead of its buffer text. */
10939 if (EQ (mini_window, selected_window))
10940 CHARPOS (this_line_start_pos) = 0;
10941
10942 return window_height_changed_p;
10943 }
10944
10945 /* Nonzero if the current buffer is shown in more than
10946 one window and was modified since last display. */
10947
10948 static int
10949 buffer_shared_and_changed (void)
10950 {
10951 return (buffer_shared > 1 && UNCHANGED_MODIFIED < MODIFF);
10952 }
10953
10954 /* Nonzero if W doesn't reflect the actual state of
10955 current buffer due to its text or overlays change. */
10956
10957 static int
10958 window_outdated (struct window *w)
10959 {
10960 eassert (XBUFFER (w->buffer) == current_buffer);
10961 return (w->last_modified < MODIFF
10962 || w->last_overlay_modified < OVERLAY_MODIFF);
10963 }
10964
10965 /* Nonzero if W's buffer was changed but not saved or Transient Mark mode
10966 is enabled and mark of W's buffer was changed since last W's update. */
10967
10968 static int
10969 window_buffer_changed (struct window *w)
10970 {
10971 struct buffer *b = XBUFFER (w->buffer);
10972
10973 eassert (BUFFER_LIVE_P (b));
10974
10975 return (((BUF_SAVE_MODIFF (b) < BUF_MODIFF (b)) != w->last_had_star)
10976 || ((!NILP (Vtransient_mark_mode) && !NILP (BVAR (b, mark_active)))
10977 != !NILP (w->region_showing)));
10978 }
10979
10980 /***********************************************************************
10981 Mode Lines and Frame Titles
10982 ***********************************************************************/
10983
10984 /* A buffer for constructing non-propertized mode-line strings and
10985 frame titles in it; allocated from the heap in init_xdisp and
10986 resized as needed in store_mode_line_noprop_char. */
10987
10988 static char *mode_line_noprop_buf;
10989
10990 /* The buffer's end, and a current output position in it. */
10991
10992 static char *mode_line_noprop_buf_end;
10993 static char *mode_line_noprop_ptr;
10994
10995 #define MODE_LINE_NOPROP_LEN(start) \
10996 ((mode_line_noprop_ptr - mode_line_noprop_buf) - start)
10997
10998 static enum {
10999 MODE_LINE_DISPLAY = 0,
11000 MODE_LINE_TITLE,
11001 MODE_LINE_NOPROP,
11002 MODE_LINE_STRING
11003 } mode_line_target;
11004
11005 /* Alist that caches the results of :propertize.
11006 Each element is (PROPERTIZED-STRING . PROPERTY-LIST). */
11007 static Lisp_Object mode_line_proptrans_alist;
11008
11009 /* List of strings making up the mode-line. */
11010 static Lisp_Object mode_line_string_list;
11011
11012 /* Base face property when building propertized mode line string. */
11013 static Lisp_Object mode_line_string_face;
11014 static Lisp_Object mode_line_string_face_prop;
11015
11016
11017 /* Unwind data for mode line strings */
11018
11019 static Lisp_Object Vmode_line_unwind_vector;
11020
11021 static Lisp_Object
11022 format_mode_line_unwind_data (struct frame *target_frame,
11023 struct buffer *obuf,
11024 Lisp_Object owin,
11025 int save_proptrans)
11026 {
11027 Lisp_Object vector, tmp;
11028
11029 /* Reduce consing by keeping one vector in
11030 Vwith_echo_area_save_vector. */
11031 vector = Vmode_line_unwind_vector;
11032 Vmode_line_unwind_vector = Qnil;
11033
11034 if (NILP (vector))
11035 vector = Fmake_vector (make_number (10), Qnil);
11036
11037 ASET (vector, 0, make_number (mode_line_target));
11038 ASET (vector, 1, make_number (MODE_LINE_NOPROP_LEN (0)));
11039 ASET (vector, 2, mode_line_string_list);
11040 ASET (vector, 3, save_proptrans ? mode_line_proptrans_alist : Qt);
11041 ASET (vector, 4, mode_line_string_face);
11042 ASET (vector, 5, mode_line_string_face_prop);
11043
11044 if (obuf)
11045 XSETBUFFER (tmp, obuf);
11046 else
11047 tmp = Qnil;
11048 ASET (vector, 6, tmp);
11049 ASET (vector, 7, owin);
11050 if (target_frame)
11051 {
11052 /* Similarly to `with-selected-window', if the operation selects
11053 a window on another frame, we must restore that frame's
11054 selected window, and (for a tty) the top-frame. */
11055 ASET (vector, 8, target_frame->selected_window);
11056 if (FRAME_TERMCAP_P (target_frame))
11057 ASET (vector, 9, FRAME_TTY (target_frame)->top_frame);
11058 }
11059
11060 return vector;
11061 }
11062
11063 static Lisp_Object
11064 unwind_format_mode_line (Lisp_Object vector)
11065 {
11066 Lisp_Object old_window = AREF (vector, 7);
11067 Lisp_Object target_frame_window = AREF (vector, 8);
11068 Lisp_Object old_top_frame = AREF (vector, 9);
11069
11070 mode_line_target = XINT (AREF (vector, 0));
11071 mode_line_noprop_ptr = mode_line_noprop_buf + XINT (AREF (vector, 1));
11072 mode_line_string_list = AREF (vector, 2);
11073 if (! EQ (AREF (vector, 3), Qt))
11074 mode_line_proptrans_alist = AREF (vector, 3);
11075 mode_line_string_face = AREF (vector, 4);
11076 mode_line_string_face_prop = AREF (vector, 5);
11077
11078 /* Select window before buffer, since it may change the buffer. */
11079 if (!NILP (old_window))
11080 {
11081 /* If the operation that we are unwinding had selected a window
11082 on a different frame, reset its frame-selected-window. For a
11083 text terminal, reset its top-frame if necessary. */
11084 if (!NILP (target_frame_window))
11085 {
11086 Lisp_Object frame
11087 = WINDOW_FRAME (XWINDOW (target_frame_window));
11088
11089 if (!EQ (frame, WINDOW_FRAME (XWINDOW (old_window))))
11090 Fselect_window (target_frame_window, Qt);
11091
11092 if (!NILP (old_top_frame) && !EQ (old_top_frame, frame))
11093 Fselect_frame (old_top_frame, Qt);
11094 }
11095
11096 Fselect_window (old_window, Qt);
11097 }
11098
11099 if (!NILP (AREF (vector, 6)))
11100 {
11101 set_buffer_internal_1 (XBUFFER (AREF (vector, 6)));
11102 ASET (vector, 6, Qnil);
11103 }
11104
11105 Vmode_line_unwind_vector = vector;
11106 return Qnil;
11107 }
11108
11109
11110 /* Store a single character C for the frame title in mode_line_noprop_buf.
11111 Re-allocate mode_line_noprop_buf if necessary. */
11112
11113 static void
11114 store_mode_line_noprop_char (char c)
11115 {
11116 /* If output position has reached the end of the allocated buffer,
11117 increase the buffer's size. */
11118 if (mode_line_noprop_ptr == mode_line_noprop_buf_end)
11119 {
11120 ptrdiff_t len = MODE_LINE_NOPROP_LEN (0);
11121 ptrdiff_t size = len;
11122 mode_line_noprop_buf =
11123 xpalloc (mode_line_noprop_buf, &size, 1, STRING_BYTES_BOUND, 1);
11124 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
11125 mode_line_noprop_ptr = mode_line_noprop_buf + len;
11126 }
11127
11128 *mode_line_noprop_ptr++ = c;
11129 }
11130
11131
11132 /* Store part of a frame title in mode_line_noprop_buf, beginning at
11133 mode_line_noprop_ptr. STRING is the string to store. Do not copy
11134 characters that yield more columns than PRECISION; PRECISION <= 0
11135 means copy the whole string. Pad with spaces until FIELD_WIDTH
11136 number of characters have been copied; FIELD_WIDTH <= 0 means don't
11137 pad. Called from display_mode_element when it is used to build a
11138 frame title. */
11139
11140 static int
11141 store_mode_line_noprop (const char *string, int field_width, int precision)
11142 {
11143 const unsigned char *str = (const unsigned char *) string;
11144 int n = 0;
11145 ptrdiff_t dummy, nbytes;
11146
11147 /* Copy at most PRECISION chars from STR. */
11148 nbytes = strlen (string);
11149 n += c_string_width (str, nbytes, precision, &dummy, &nbytes);
11150 while (nbytes--)
11151 store_mode_line_noprop_char (*str++);
11152
11153 /* Fill up with spaces until FIELD_WIDTH reached. */
11154 while (field_width > 0
11155 && n < field_width)
11156 {
11157 store_mode_line_noprop_char (' ');
11158 ++n;
11159 }
11160
11161 return n;
11162 }
11163
11164 /***********************************************************************
11165 Frame Titles
11166 ***********************************************************************/
11167
11168 #ifdef HAVE_WINDOW_SYSTEM
11169
11170 /* Set the title of FRAME, if it has changed. The title format is
11171 Vicon_title_format if FRAME is iconified, otherwise it is
11172 frame_title_format. */
11173
11174 static void
11175 x_consider_frame_title (Lisp_Object frame)
11176 {
11177 struct frame *f = XFRAME (frame);
11178
11179 if (FRAME_WINDOW_P (f)
11180 || FRAME_MINIBUF_ONLY_P (f)
11181 || f->explicit_name)
11182 {
11183 /* Do we have more than one visible frame on this X display? */
11184 Lisp_Object tail, other_frame, fmt;
11185 ptrdiff_t title_start;
11186 char *title;
11187 ptrdiff_t len;
11188 struct it it;
11189 ptrdiff_t count = SPECPDL_INDEX ();
11190
11191 FOR_EACH_FRAME (tail, other_frame)
11192 {
11193 struct frame *tf = XFRAME (other_frame);
11194
11195 if (tf != f
11196 && FRAME_KBOARD (tf) == FRAME_KBOARD (f)
11197 && !FRAME_MINIBUF_ONLY_P (tf)
11198 && !EQ (other_frame, tip_frame)
11199 && (FRAME_VISIBLE_P (tf) || FRAME_ICONIFIED_P (tf)))
11200 break;
11201 }
11202
11203 /* Set global variable indicating that multiple frames exist. */
11204 multiple_frames = CONSP (tail);
11205
11206 /* Switch to the buffer of selected window of the frame. Set up
11207 mode_line_target so that display_mode_element will output into
11208 mode_line_noprop_buf; then display the title. */
11209 record_unwind_protect (unwind_format_mode_line,
11210 format_mode_line_unwind_data
11211 (f, current_buffer, selected_window, 0));
11212
11213 Fselect_window (f->selected_window, Qt);
11214 set_buffer_internal_1
11215 (XBUFFER (XWINDOW (f->selected_window)->buffer));
11216 fmt = FRAME_ICONIFIED_P (f) ? Vicon_title_format : Vframe_title_format;
11217
11218 mode_line_target = MODE_LINE_TITLE;
11219 title_start = MODE_LINE_NOPROP_LEN (0);
11220 init_iterator (&it, XWINDOW (f->selected_window), -1, -1,
11221 NULL, DEFAULT_FACE_ID);
11222 display_mode_element (&it, 0, -1, -1, fmt, Qnil, 0);
11223 len = MODE_LINE_NOPROP_LEN (title_start);
11224 title = mode_line_noprop_buf + title_start;
11225 unbind_to (count, Qnil);
11226
11227 /* Set the title only if it's changed. This avoids consing in
11228 the common case where it hasn't. (If it turns out that we've
11229 already wasted too much time by walking through the list with
11230 display_mode_element, then we might need to optimize at a
11231 higher level than this.) */
11232 if (! STRINGP (f->name)
11233 || SBYTES (f->name) != len
11234 || memcmp (title, SDATA (f->name), len) != 0)
11235 x_implicitly_set_name (f, make_string (title, len), Qnil);
11236 }
11237 }
11238
11239 #endif /* not HAVE_WINDOW_SYSTEM */
11240
11241 \f
11242 /***********************************************************************
11243 Menu Bars
11244 ***********************************************************************/
11245
11246
11247 /* Prepare for redisplay by updating menu-bar item lists when
11248 appropriate. This can call eval. */
11249
11250 void
11251 prepare_menu_bars (void)
11252 {
11253 int all_windows;
11254 struct gcpro gcpro1, gcpro2;
11255 struct frame *f;
11256 Lisp_Object tooltip_frame;
11257
11258 #ifdef HAVE_WINDOW_SYSTEM
11259 tooltip_frame = tip_frame;
11260 #else
11261 tooltip_frame = Qnil;
11262 #endif
11263
11264 /* Update all frame titles based on their buffer names, etc. We do
11265 this before the menu bars so that the buffer-menu will show the
11266 up-to-date frame titles. */
11267 #ifdef HAVE_WINDOW_SYSTEM
11268 if (windows_or_buffers_changed || update_mode_lines)
11269 {
11270 Lisp_Object tail, frame;
11271
11272 FOR_EACH_FRAME (tail, frame)
11273 {
11274 f = XFRAME (frame);
11275 if (!EQ (frame, tooltip_frame)
11276 && (FRAME_VISIBLE_P (f) || FRAME_ICONIFIED_P (f)))
11277 x_consider_frame_title (frame);
11278 }
11279 }
11280 #endif /* HAVE_WINDOW_SYSTEM */
11281
11282 /* Update the menu bar item lists, if appropriate. This has to be
11283 done before any actual redisplay or generation of display lines. */
11284 all_windows = (update_mode_lines
11285 || buffer_shared_and_changed ()
11286 || windows_or_buffers_changed);
11287 if (all_windows)
11288 {
11289 Lisp_Object tail, frame;
11290 ptrdiff_t count = SPECPDL_INDEX ();
11291 /* 1 means that update_menu_bar has run its hooks
11292 so any further calls to update_menu_bar shouldn't do so again. */
11293 int menu_bar_hooks_run = 0;
11294
11295 record_unwind_save_match_data ();
11296
11297 FOR_EACH_FRAME (tail, frame)
11298 {
11299 f = XFRAME (frame);
11300
11301 /* Ignore tooltip frame. */
11302 if (EQ (frame, tooltip_frame))
11303 continue;
11304
11305 /* If a window on this frame changed size, report that to
11306 the user and clear the size-change flag. */
11307 if (FRAME_WINDOW_SIZES_CHANGED (f))
11308 {
11309 Lisp_Object functions;
11310
11311 /* Clear flag first in case we get an error below. */
11312 FRAME_WINDOW_SIZES_CHANGED (f) = 0;
11313 functions = Vwindow_size_change_functions;
11314 GCPRO2 (tail, functions);
11315
11316 while (CONSP (functions))
11317 {
11318 if (!EQ (XCAR (functions), Qt))
11319 call1 (XCAR (functions), frame);
11320 functions = XCDR (functions);
11321 }
11322 UNGCPRO;
11323 }
11324
11325 GCPRO1 (tail);
11326 menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
11327 #ifdef HAVE_WINDOW_SYSTEM
11328 update_tool_bar (f, 0);
11329 #endif
11330 #ifdef HAVE_NS
11331 if (windows_or_buffers_changed
11332 && FRAME_NS_P (f))
11333 ns_set_doc_edited
11334 (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->buffer));
11335 #endif
11336 UNGCPRO;
11337 }
11338
11339 unbind_to (count, Qnil);
11340 }
11341 else
11342 {
11343 struct frame *sf = SELECTED_FRAME ();
11344 update_menu_bar (sf, 1, 0);
11345 #ifdef HAVE_WINDOW_SYSTEM
11346 update_tool_bar (sf, 1);
11347 #endif
11348 }
11349 }
11350
11351
11352 /* Update the menu bar item list for frame F. This has to be done
11353 before we start to fill in any display lines, because it can call
11354 eval.
11355
11356 If SAVE_MATCH_DATA is non-zero, we must save and restore it here.
11357
11358 If HOOKS_RUN is 1, that means a previous call to update_menu_bar
11359 already ran the menu bar hooks for this redisplay, so there
11360 is no need to run them again. The return value is the
11361 updated value of this flag, to pass to the next call. */
11362
11363 static int
11364 update_menu_bar (struct frame *f, int save_match_data, int hooks_run)
11365 {
11366 Lisp_Object window;
11367 register struct window *w;
11368
11369 /* If called recursively during a menu update, do nothing. This can
11370 happen when, for instance, an activate-menubar-hook causes a
11371 redisplay. */
11372 if (inhibit_menubar_update)
11373 return hooks_run;
11374
11375 window = FRAME_SELECTED_WINDOW (f);
11376 w = XWINDOW (window);
11377
11378 if (FRAME_WINDOW_P (f)
11379 ?
11380 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11381 || defined (HAVE_NS) || defined (USE_GTK)
11382 FRAME_EXTERNAL_MENU_BAR (f)
11383 #else
11384 FRAME_MENU_BAR_LINES (f) > 0
11385 #endif
11386 : FRAME_MENU_BAR_LINES (f) > 0)
11387 {
11388 /* If the user has switched buffers or windows, we need to
11389 recompute to reflect the new bindings. But we'll
11390 recompute when update_mode_lines is set too; that means
11391 that people can use force-mode-line-update to request
11392 that the menu bar be recomputed. The adverse effect on
11393 the rest of the redisplay algorithm is about the same as
11394 windows_or_buffers_changed anyway. */
11395 if (windows_or_buffers_changed
11396 /* This used to test w->update_mode_line, but we believe
11397 there is no need to recompute the menu in that case. */
11398 || update_mode_lines
11399 || window_buffer_changed (w))
11400 {
11401 struct buffer *prev = current_buffer;
11402 ptrdiff_t count = SPECPDL_INDEX ();
11403
11404 specbind (Qinhibit_menubar_update, Qt);
11405
11406 set_buffer_internal_1 (XBUFFER (w->buffer));
11407 if (save_match_data)
11408 record_unwind_save_match_data ();
11409 if (NILP (Voverriding_local_map_menu_flag))
11410 {
11411 specbind (Qoverriding_terminal_local_map, Qnil);
11412 specbind (Qoverriding_local_map, Qnil);
11413 }
11414
11415 if (!hooks_run)
11416 {
11417 /* Run the Lucid hook. */
11418 safe_run_hooks (Qactivate_menubar_hook);
11419
11420 /* If it has changed current-menubar from previous value,
11421 really recompute the menu-bar from the value. */
11422 if (! NILP (Vlucid_menu_bar_dirty_flag))
11423 call0 (Qrecompute_lucid_menubar);
11424
11425 safe_run_hooks (Qmenu_bar_update_hook);
11426
11427 hooks_run = 1;
11428 }
11429
11430 XSETFRAME (Vmenu_updating_frame, f);
11431 fset_menu_bar_items (f, menu_bar_items (FRAME_MENU_BAR_ITEMS (f)));
11432
11433 /* Redisplay the menu bar in case we changed it. */
11434 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
11435 || defined (HAVE_NS) || defined (USE_GTK)
11436 if (FRAME_WINDOW_P (f))
11437 {
11438 #if defined (HAVE_NS)
11439 /* All frames on Mac OS share the same menubar. So only
11440 the selected frame should be allowed to set it. */
11441 if (f == SELECTED_FRAME ())
11442 #endif
11443 set_frame_menubar (f, 0, 0);
11444 }
11445 else
11446 /* On a terminal screen, the menu bar is an ordinary screen
11447 line, and this makes it get updated. */
11448 w->update_mode_line = 1;
11449 #else /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11450 /* In the non-toolkit version, the menu bar is an ordinary screen
11451 line, and this makes it get updated. */
11452 w->update_mode_line = 1;
11453 #endif /* ! (USE_X_TOOLKIT || HAVE_NTGUI || HAVE_NS || USE_GTK) */
11454
11455 unbind_to (count, Qnil);
11456 set_buffer_internal_1 (prev);
11457 }
11458 }
11459
11460 return hooks_run;
11461 }
11462
11463
11464 \f
11465 /***********************************************************************
11466 Output Cursor
11467 ***********************************************************************/
11468
11469 #ifdef HAVE_WINDOW_SYSTEM
11470
11471 /* EXPORT:
11472 Nominal cursor position -- where to draw output.
11473 HPOS and VPOS are window relative glyph matrix coordinates.
11474 X and Y are window relative pixel coordinates. */
11475
11476 struct cursor_pos output_cursor;
11477
11478
11479 /* EXPORT:
11480 Set the global variable output_cursor to CURSOR. All cursor
11481 positions are relative to updated_window. */
11482
11483 void
11484 set_output_cursor (struct cursor_pos *cursor)
11485 {
11486 output_cursor.hpos = cursor->hpos;
11487 output_cursor.vpos = cursor->vpos;
11488 output_cursor.x = cursor->x;
11489 output_cursor.y = cursor->y;
11490 }
11491
11492
11493 /* EXPORT for RIF:
11494 Set a nominal cursor position.
11495
11496 HPOS and VPOS are column/row positions in a window glyph matrix. X
11497 and Y are window text area relative pixel positions.
11498
11499 If this is done during an update, updated_window will contain the
11500 window that is being updated and the position is the future output
11501 cursor position for that window. If updated_window is null, use
11502 selected_window and display the cursor at the given position. */
11503
11504 void
11505 x_cursor_to (int vpos, int hpos, int y, int x)
11506 {
11507 struct window *w;
11508
11509 /* If updated_window is not set, work on selected_window. */
11510 if (updated_window)
11511 w = updated_window;
11512 else
11513 w = XWINDOW (selected_window);
11514
11515 /* Set the output cursor. */
11516 output_cursor.hpos = hpos;
11517 output_cursor.vpos = vpos;
11518 output_cursor.x = x;
11519 output_cursor.y = y;
11520
11521 /* If not called as part of an update, really display the cursor.
11522 This will also set the cursor position of W. */
11523 if (updated_window == NULL)
11524 {
11525 block_input ();
11526 display_and_set_cursor (w, 1, hpos, vpos, x, y);
11527 if (FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
11528 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (SELECTED_FRAME ());
11529 unblock_input ();
11530 }
11531 }
11532
11533 #endif /* HAVE_WINDOW_SYSTEM */
11534
11535 \f
11536 /***********************************************************************
11537 Tool-bars
11538 ***********************************************************************/
11539
11540 #ifdef HAVE_WINDOW_SYSTEM
11541
11542 /* Where the mouse was last time we reported a mouse event. */
11543
11544 FRAME_PTR last_mouse_frame;
11545
11546 /* Tool-bar item index of the item on which a mouse button was pressed
11547 or -1. */
11548
11549 int last_tool_bar_item;
11550
11551 /* Select `frame' temporarily without running all the code in
11552 do_switch_frame.
11553 FIXME: Maybe do_switch_frame should be trimmed down similarly
11554 when `norecord' is set. */
11555 static Lisp_Object
11556 fast_set_selected_frame (Lisp_Object frame)
11557 {
11558 if (!EQ (selected_frame, frame))
11559 {
11560 selected_frame = frame;
11561 selected_window = XFRAME (frame)->selected_window;
11562 }
11563 return Qnil;
11564 }
11565
11566 /* Update the tool-bar item list for frame F. This has to be done
11567 before we start to fill in any display lines. Called from
11568 prepare_menu_bars. If SAVE_MATCH_DATA is non-zero, we must save
11569 and restore it here. */
11570
11571 static void
11572 update_tool_bar (struct frame *f, int save_match_data)
11573 {
11574 #if defined (USE_GTK) || defined (HAVE_NS)
11575 int do_update = FRAME_EXTERNAL_TOOL_BAR (f);
11576 #else
11577 int do_update = WINDOWP (f->tool_bar_window)
11578 && WINDOW_TOTAL_LINES (XWINDOW (f->tool_bar_window)) > 0;
11579 #endif
11580
11581 if (do_update)
11582 {
11583 Lisp_Object window;
11584 struct window *w;
11585
11586 window = FRAME_SELECTED_WINDOW (f);
11587 w = XWINDOW (window);
11588
11589 /* If the user has switched buffers or windows, we need to
11590 recompute to reflect the new bindings. But we'll
11591 recompute when update_mode_lines is set too; that means
11592 that people can use force-mode-line-update to request
11593 that the menu bar be recomputed. The adverse effect on
11594 the rest of the redisplay algorithm is about the same as
11595 windows_or_buffers_changed anyway. */
11596 if (windows_or_buffers_changed
11597 || w->update_mode_line
11598 || update_mode_lines
11599 || window_buffer_changed (w))
11600 {
11601 struct buffer *prev = current_buffer;
11602 ptrdiff_t count = SPECPDL_INDEX ();
11603 Lisp_Object frame, new_tool_bar;
11604 int new_n_tool_bar;
11605 struct gcpro gcpro1;
11606
11607 /* Set current_buffer to the buffer of the selected
11608 window of the frame, so that we get the right local
11609 keymaps. */
11610 set_buffer_internal_1 (XBUFFER (w->buffer));
11611
11612 /* Save match data, if we must. */
11613 if (save_match_data)
11614 record_unwind_save_match_data ();
11615
11616 /* Make sure that we don't accidentally use bogus keymaps. */
11617 if (NILP (Voverriding_local_map_menu_flag))
11618 {
11619 specbind (Qoverriding_terminal_local_map, Qnil);
11620 specbind (Qoverriding_local_map, Qnil);
11621 }
11622
11623 GCPRO1 (new_tool_bar);
11624
11625 /* We must temporarily set the selected frame to this frame
11626 before calling tool_bar_items, because the calculation of
11627 the tool-bar keymap uses the selected frame (see
11628 `tool-bar-make-keymap' in tool-bar.el). */
11629 eassert (EQ (selected_window,
11630 /* Since we only explicitly preserve selected_frame,
11631 check that selected_window would be redundant. */
11632 XFRAME (selected_frame)->selected_window));
11633 record_unwind_protect (fast_set_selected_frame, selected_frame);
11634 XSETFRAME (frame, f);
11635 fast_set_selected_frame (frame);
11636
11637 /* Build desired tool-bar items from keymaps. */
11638 new_tool_bar
11639 = tool_bar_items (Fcopy_sequence (f->tool_bar_items),
11640 &new_n_tool_bar);
11641
11642 /* Redisplay the tool-bar if we changed it. */
11643 if (new_n_tool_bar != f->n_tool_bar_items
11644 || NILP (Fequal (new_tool_bar, f->tool_bar_items)))
11645 {
11646 /* Redisplay that happens asynchronously due to an expose event
11647 may access f->tool_bar_items. Make sure we update both
11648 variables within BLOCK_INPUT so no such event interrupts. */
11649 block_input ();
11650 fset_tool_bar_items (f, new_tool_bar);
11651 f->n_tool_bar_items = new_n_tool_bar;
11652 w->update_mode_line = 1;
11653 unblock_input ();
11654 }
11655
11656 UNGCPRO;
11657
11658 unbind_to (count, Qnil);
11659 set_buffer_internal_1 (prev);
11660 }
11661 }
11662 }
11663
11664
11665 /* Set F->desired_tool_bar_string to a Lisp string representing frame
11666 F's desired tool-bar contents. F->tool_bar_items must have
11667 been set up previously by calling prepare_menu_bars. */
11668
11669 static void
11670 build_desired_tool_bar_string (struct frame *f)
11671 {
11672 int i, size, size_needed;
11673 struct gcpro gcpro1, gcpro2, gcpro3;
11674 Lisp_Object image, plist, props;
11675
11676 image = plist = props = Qnil;
11677 GCPRO3 (image, plist, props);
11678
11679 /* Prepare F->desired_tool_bar_string. If we can reuse it, do so.
11680 Otherwise, make a new string. */
11681
11682 /* The size of the string we might be able to reuse. */
11683 size = (STRINGP (f->desired_tool_bar_string)
11684 ? SCHARS (f->desired_tool_bar_string)
11685 : 0);
11686
11687 /* We need one space in the string for each image. */
11688 size_needed = f->n_tool_bar_items;
11689
11690 /* Reuse f->desired_tool_bar_string, if possible. */
11691 if (size < size_needed || NILP (f->desired_tool_bar_string))
11692 fset_desired_tool_bar_string
11693 (f, Fmake_string (make_number (size_needed), make_number (' ')));
11694 else
11695 {
11696 props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
11697 Fremove_text_properties (make_number (0), make_number (size),
11698 props, f->desired_tool_bar_string);
11699 }
11700
11701 /* Put a `display' property on the string for the images to display,
11702 put a `menu_item' property on tool-bar items with a value that
11703 is the index of the item in F's tool-bar item vector. */
11704 for (i = 0; i < f->n_tool_bar_items; ++i)
11705 {
11706 #define PROP(IDX) \
11707 AREF (f->tool_bar_items, i * TOOL_BAR_ITEM_NSLOTS + (IDX))
11708
11709 int enabled_p = !NILP (PROP (TOOL_BAR_ITEM_ENABLED_P));
11710 int selected_p = !NILP (PROP (TOOL_BAR_ITEM_SELECTED_P));
11711 int hmargin, vmargin, relief, idx, end;
11712
11713 /* If image is a vector, choose the image according to the
11714 button state. */
11715 image = PROP (TOOL_BAR_ITEM_IMAGES);
11716 if (VECTORP (image))
11717 {
11718 if (enabled_p)
11719 idx = (selected_p
11720 ? TOOL_BAR_IMAGE_ENABLED_SELECTED
11721 : TOOL_BAR_IMAGE_ENABLED_DESELECTED);
11722 else
11723 idx = (selected_p
11724 ? TOOL_BAR_IMAGE_DISABLED_SELECTED
11725 : TOOL_BAR_IMAGE_DISABLED_DESELECTED);
11726
11727 eassert (ASIZE (image) >= idx);
11728 image = AREF (image, idx);
11729 }
11730 else
11731 idx = -1;
11732
11733 /* Ignore invalid image specifications. */
11734 if (!valid_image_p (image))
11735 continue;
11736
11737 /* Display the tool-bar button pressed, or depressed. */
11738 plist = Fcopy_sequence (XCDR (image));
11739
11740 /* Compute margin and relief to draw. */
11741 relief = (tool_bar_button_relief >= 0
11742 ? tool_bar_button_relief
11743 : DEFAULT_TOOL_BAR_BUTTON_RELIEF);
11744 hmargin = vmargin = relief;
11745
11746 if (RANGED_INTEGERP (1, Vtool_bar_button_margin,
11747 INT_MAX - max (hmargin, vmargin)))
11748 {
11749 hmargin += XFASTINT (Vtool_bar_button_margin);
11750 vmargin += XFASTINT (Vtool_bar_button_margin);
11751 }
11752 else if (CONSP (Vtool_bar_button_margin))
11753 {
11754 if (RANGED_INTEGERP (1, XCAR (Vtool_bar_button_margin),
11755 INT_MAX - hmargin))
11756 hmargin += XFASTINT (XCAR (Vtool_bar_button_margin));
11757
11758 if (RANGED_INTEGERP (1, XCDR (Vtool_bar_button_margin),
11759 INT_MAX - vmargin))
11760 vmargin += XFASTINT (XCDR (Vtool_bar_button_margin));
11761 }
11762
11763 if (auto_raise_tool_bar_buttons_p)
11764 {
11765 /* Add a `:relief' property to the image spec if the item is
11766 selected. */
11767 if (selected_p)
11768 {
11769 plist = Fplist_put (plist, QCrelief, make_number (-relief));
11770 hmargin -= relief;
11771 vmargin -= relief;
11772 }
11773 }
11774 else
11775 {
11776 /* If image is selected, display it pressed, i.e. with a
11777 negative relief. If it's not selected, display it with a
11778 raised relief. */
11779 plist = Fplist_put (plist, QCrelief,
11780 (selected_p
11781 ? make_number (-relief)
11782 : make_number (relief)));
11783 hmargin -= relief;
11784 vmargin -= relief;
11785 }
11786
11787 /* Put a margin around the image. */
11788 if (hmargin || vmargin)
11789 {
11790 if (hmargin == vmargin)
11791 plist = Fplist_put (plist, QCmargin, make_number (hmargin));
11792 else
11793 plist = Fplist_put (plist, QCmargin,
11794 Fcons (make_number (hmargin),
11795 make_number (vmargin)));
11796 }
11797
11798 /* If button is not enabled, and we don't have special images
11799 for the disabled state, make the image appear disabled by
11800 applying an appropriate algorithm to it. */
11801 if (!enabled_p && idx < 0)
11802 plist = Fplist_put (plist, QCconversion, Qdisabled);
11803
11804 /* Put a `display' text property on the string for the image to
11805 display. Put a `menu-item' property on the string that gives
11806 the start of this item's properties in the tool-bar items
11807 vector. */
11808 image = Fcons (Qimage, plist);
11809 props = list4 (Qdisplay, image,
11810 Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
11811
11812 /* Let the last image hide all remaining spaces in the tool bar
11813 string. The string can be longer than needed when we reuse a
11814 previous string. */
11815 if (i + 1 == f->n_tool_bar_items)
11816 end = SCHARS (f->desired_tool_bar_string);
11817 else
11818 end = i + 1;
11819 Fadd_text_properties (make_number (i), make_number (end),
11820 props, f->desired_tool_bar_string);
11821 #undef PROP
11822 }
11823
11824 UNGCPRO;
11825 }
11826
11827
11828 /* Display one line of the tool-bar of frame IT->f.
11829
11830 HEIGHT specifies the desired height of the tool-bar line.
11831 If the actual height of the glyph row is less than HEIGHT, the
11832 row's height is increased to HEIGHT, and the icons are centered
11833 vertically in the new height.
11834
11835 If HEIGHT is -1, we are counting needed tool-bar lines, so don't
11836 count a final empty row in case the tool-bar width exactly matches
11837 the window width.
11838 */
11839
11840 static void
11841 display_tool_bar_line (struct it *it, int height)
11842 {
11843 struct glyph_row *row = it->glyph_row;
11844 int max_x = it->last_visible_x;
11845 struct glyph *last;
11846
11847 prepare_desired_row (row);
11848 row->y = it->current_y;
11849
11850 /* Note that this isn't made use of if the face hasn't a box,
11851 so there's no need to check the face here. */
11852 it->start_of_box_run_p = 1;
11853
11854 while (it->current_x < max_x)
11855 {
11856 int x, n_glyphs_before, i, nglyphs;
11857 struct it it_before;
11858
11859 /* Get the next display element. */
11860 if (!get_next_display_element (it))
11861 {
11862 /* Don't count empty row if we are counting needed tool-bar lines. */
11863 if (height < 0 && !it->hpos)
11864 return;
11865 break;
11866 }
11867
11868 /* Produce glyphs. */
11869 n_glyphs_before = row->used[TEXT_AREA];
11870 it_before = *it;
11871
11872 PRODUCE_GLYPHS (it);
11873
11874 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
11875 i = 0;
11876 x = it_before.current_x;
11877 while (i < nglyphs)
11878 {
11879 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
11880
11881 if (x + glyph->pixel_width > max_x)
11882 {
11883 /* Glyph doesn't fit on line. Backtrack. */
11884 row->used[TEXT_AREA] = n_glyphs_before;
11885 *it = it_before;
11886 /* If this is the only glyph on this line, it will never fit on the
11887 tool-bar, so skip it. But ensure there is at least one glyph,
11888 so we don't accidentally disable the tool-bar. */
11889 if (n_glyphs_before == 0
11890 && (it->vpos > 0 || IT_STRING_CHARPOS (*it) < it->end_charpos-1))
11891 break;
11892 goto out;
11893 }
11894
11895 ++it->hpos;
11896 x += glyph->pixel_width;
11897 ++i;
11898 }
11899
11900 /* Stop at line end. */
11901 if (ITERATOR_AT_END_OF_LINE_P (it))
11902 break;
11903
11904 set_iterator_to_next (it, 1);
11905 }
11906
11907 out:;
11908
11909 row->displays_text_p = row->used[TEXT_AREA] != 0;
11910
11911 /* Use default face for the border below the tool bar.
11912
11913 FIXME: When auto-resize-tool-bars is grow-only, there is
11914 no additional border below the possibly empty tool-bar lines.
11915 So to make the extra empty lines look "normal", we have to
11916 use the tool-bar face for the border too. */
11917 if (!row->displays_text_p && !EQ (Vauto_resize_tool_bars, Qgrow_only))
11918 it->face_id = DEFAULT_FACE_ID;
11919
11920 extend_face_to_end_of_line (it);
11921 last = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
11922 last->right_box_line_p = 1;
11923 if (last == row->glyphs[TEXT_AREA])
11924 last->left_box_line_p = 1;
11925
11926 /* Make line the desired height and center it vertically. */
11927 if ((height -= it->max_ascent + it->max_descent) > 0)
11928 {
11929 /* Don't add more than one line height. */
11930 height %= FRAME_LINE_HEIGHT (it->f);
11931 it->max_ascent += height / 2;
11932 it->max_descent += (height + 1) / 2;
11933 }
11934
11935 compute_line_metrics (it);
11936
11937 /* If line is empty, make it occupy the rest of the tool-bar. */
11938 if (!row->displays_text_p)
11939 {
11940 row->height = row->phys_height = it->last_visible_y - row->y;
11941 row->visible_height = row->height;
11942 row->ascent = row->phys_ascent = 0;
11943 row->extra_line_spacing = 0;
11944 }
11945
11946 row->full_width_p = 1;
11947 row->continued_p = 0;
11948 row->truncated_on_left_p = 0;
11949 row->truncated_on_right_p = 0;
11950
11951 it->current_x = it->hpos = 0;
11952 it->current_y += row->height;
11953 ++it->vpos;
11954 ++it->glyph_row;
11955 }
11956
11957
11958 /* Max tool-bar height. */
11959
11960 #define MAX_FRAME_TOOL_BAR_HEIGHT(f) \
11961 ((FRAME_LINE_HEIGHT (f) * FRAME_LINES (f)))
11962
11963 /* Value is the number of screen lines needed to make all tool-bar
11964 items of frame F visible. The number of actual rows needed is
11965 returned in *N_ROWS if non-NULL. */
11966
11967 static int
11968 tool_bar_lines_needed (struct frame *f, int *n_rows)
11969 {
11970 struct window *w = XWINDOW (f->tool_bar_window);
11971 struct it it;
11972 /* tool_bar_lines_needed is called from redisplay_tool_bar after building
11973 the desired matrix, so use (unused) mode-line row as temporary row to
11974 avoid destroying the first tool-bar row. */
11975 struct glyph_row *temp_row = MATRIX_MODE_LINE_ROW (w->desired_matrix);
11976
11977 /* Initialize an iterator for iteration over
11978 F->desired_tool_bar_string in the tool-bar window of frame F. */
11979 init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
11980 it.first_visible_x = 0;
11981 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
11982 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
11983 it.paragraph_embedding = L2R;
11984
11985 while (!ITERATOR_AT_END_P (&it))
11986 {
11987 clear_glyph_row (temp_row);
11988 it.glyph_row = temp_row;
11989 display_tool_bar_line (&it, -1);
11990 }
11991 clear_glyph_row (temp_row);
11992
11993 /* f->n_tool_bar_rows == 0 means "unknown"; -1 means no tool-bar. */
11994 if (n_rows)
11995 *n_rows = it.vpos > 0 ? it.vpos : -1;
11996
11997 return (it.current_y + FRAME_LINE_HEIGHT (f) - 1) / FRAME_LINE_HEIGHT (f);
11998 }
11999
12000
12001 DEFUN ("tool-bar-lines-needed", Ftool_bar_lines_needed, Stool_bar_lines_needed,
12002 0, 1, 0,
12003 doc: /* Return the number of lines occupied by the tool bar of FRAME.
12004 If FRAME is nil or omitted, use the selected frame. */)
12005 (Lisp_Object frame)
12006 {
12007 struct frame *f = decode_any_frame (frame);
12008 struct window *w;
12009 int nlines = 0;
12010
12011 if (WINDOWP (f->tool_bar_window)
12012 && (w = XWINDOW (f->tool_bar_window),
12013 WINDOW_TOTAL_LINES (w) > 0))
12014 {
12015 update_tool_bar (f, 1);
12016 if (f->n_tool_bar_items)
12017 {
12018 build_desired_tool_bar_string (f);
12019 nlines = tool_bar_lines_needed (f, NULL);
12020 }
12021 }
12022
12023 return make_number (nlines);
12024 }
12025
12026
12027 /* Display the tool-bar of frame F. Value is non-zero if tool-bar's
12028 height should be changed. */
12029
12030 static int
12031 redisplay_tool_bar (struct frame *f)
12032 {
12033 struct window *w;
12034 struct it it;
12035 struct glyph_row *row;
12036
12037 #if defined (USE_GTK) || defined (HAVE_NS)
12038 if (FRAME_EXTERNAL_TOOL_BAR (f))
12039 update_frame_tool_bar (f);
12040 return 0;
12041 #endif
12042
12043 /* If frame hasn't a tool-bar window or if it is zero-height, don't
12044 do anything. This means you must start with tool-bar-lines
12045 non-zero to get the auto-sizing effect. Or in other words, you
12046 can turn off tool-bars by specifying tool-bar-lines zero. */
12047 if (!WINDOWP (f->tool_bar_window)
12048 || (w = XWINDOW (f->tool_bar_window),
12049 WINDOW_TOTAL_LINES (w) == 0))
12050 return 0;
12051
12052 /* Set up an iterator for the tool-bar window. */
12053 init_iterator (&it, w, -1, -1, w->desired_matrix->rows, TOOL_BAR_FACE_ID);
12054 it.first_visible_x = 0;
12055 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
12056 row = it.glyph_row;
12057
12058 /* Build a string that represents the contents of the tool-bar. */
12059 build_desired_tool_bar_string (f);
12060 reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
12061 /* FIXME: This should be controlled by a user option. But it
12062 doesn't make sense to have an R2L tool bar if the menu bar cannot
12063 be drawn also R2L, and making the menu bar R2L is tricky due
12064 toolkit-specific code that implements it. If an R2L tool bar is
12065 ever supported, display_tool_bar_line should also be augmented to
12066 call unproduce_glyphs like display_line and display_string
12067 do. */
12068 it.paragraph_embedding = L2R;
12069
12070 if (f->n_tool_bar_rows == 0)
12071 {
12072 int nlines;
12073
12074 if ((nlines = tool_bar_lines_needed (f, &f->n_tool_bar_rows),
12075 nlines != WINDOW_TOTAL_LINES (w)))
12076 {
12077 Lisp_Object frame;
12078 int old_height = WINDOW_TOTAL_LINES (w);
12079
12080 XSETFRAME (frame, f);
12081 Fmodify_frame_parameters (frame,
12082 Fcons (Fcons (Qtool_bar_lines,
12083 make_number (nlines)),
12084 Qnil));
12085 if (WINDOW_TOTAL_LINES (w) != old_height)
12086 {
12087 clear_glyph_matrix (w->desired_matrix);
12088 fonts_changed_p = 1;
12089 return 1;
12090 }
12091 }
12092 }
12093
12094 /* Display as many lines as needed to display all tool-bar items. */
12095
12096 if (f->n_tool_bar_rows > 0)
12097 {
12098 int border, rows, height, extra;
12099
12100 if (TYPE_RANGED_INTEGERP (int, Vtool_bar_border))
12101 border = XINT (Vtool_bar_border);
12102 else if (EQ (Vtool_bar_border, Qinternal_border_width))
12103 border = FRAME_INTERNAL_BORDER_WIDTH (f);
12104 else if (EQ (Vtool_bar_border, Qborder_width))
12105 border = f->border_width;
12106 else
12107 border = 0;
12108 if (border < 0)
12109 border = 0;
12110
12111 rows = f->n_tool_bar_rows;
12112 height = max (1, (it.last_visible_y - border) / rows);
12113 extra = it.last_visible_y - border - height * rows;
12114
12115 while (it.current_y < it.last_visible_y)
12116 {
12117 int h = 0;
12118 if (extra > 0 && rows-- > 0)
12119 {
12120 h = (extra + rows - 1) / rows;
12121 extra -= h;
12122 }
12123 display_tool_bar_line (&it, height + h);
12124 }
12125 }
12126 else
12127 {
12128 while (it.current_y < it.last_visible_y)
12129 display_tool_bar_line (&it, 0);
12130 }
12131
12132 /* It doesn't make much sense to try scrolling in the tool-bar
12133 window, so don't do it. */
12134 w->desired_matrix->no_scrolling_p = 1;
12135 w->must_be_updated_p = 1;
12136
12137 if (!NILP (Vauto_resize_tool_bars))
12138 {
12139 int max_tool_bar_height = MAX_FRAME_TOOL_BAR_HEIGHT (f);
12140 int change_height_p = 0;
12141
12142 /* If we couldn't display everything, change the tool-bar's
12143 height if there is room for more. */
12144 if (IT_STRING_CHARPOS (it) < it.end_charpos
12145 && it.current_y < max_tool_bar_height)
12146 change_height_p = 1;
12147
12148 row = it.glyph_row - 1;
12149
12150 /* If there are blank lines at the end, except for a partially
12151 visible blank line at the end that is smaller than
12152 FRAME_LINE_HEIGHT, change the tool-bar's height. */
12153 if (!row->displays_text_p
12154 && row->height >= FRAME_LINE_HEIGHT (f))
12155 change_height_p = 1;
12156
12157 /* If row displays tool-bar items, but is partially visible,
12158 change the tool-bar's height. */
12159 if (row->displays_text_p
12160 && MATRIX_ROW_BOTTOM_Y (row) > it.last_visible_y
12161 && MATRIX_ROW_BOTTOM_Y (row) < max_tool_bar_height)
12162 change_height_p = 1;
12163
12164 /* Resize windows as needed by changing the `tool-bar-lines'
12165 frame parameter. */
12166 if (change_height_p)
12167 {
12168 Lisp_Object frame;
12169 int old_height = WINDOW_TOTAL_LINES (w);
12170 int nrows;
12171 int nlines = tool_bar_lines_needed (f, &nrows);
12172
12173 change_height_p = ((EQ (Vauto_resize_tool_bars, Qgrow_only)
12174 && !f->minimize_tool_bar_window_p)
12175 ? (nlines > old_height)
12176 : (nlines != old_height));
12177 f->minimize_tool_bar_window_p = 0;
12178
12179 if (change_height_p)
12180 {
12181 XSETFRAME (frame, f);
12182 Fmodify_frame_parameters (frame,
12183 Fcons (Fcons (Qtool_bar_lines,
12184 make_number (nlines)),
12185 Qnil));
12186 if (WINDOW_TOTAL_LINES (w) != old_height)
12187 {
12188 clear_glyph_matrix (w->desired_matrix);
12189 f->n_tool_bar_rows = nrows;
12190 fonts_changed_p = 1;
12191 return 1;
12192 }
12193 }
12194 }
12195 }
12196
12197 f->minimize_tool_bar_window_p = 0;
12198 return 0;
12199 }
12200
12201
12202 /* Get information about the tool-bar item which is displayed in GLYPH
12203 on frame F. Return in *PROP_IDX the index where tool-bar item
12204 properties start in F->tool_bar_items. Value is zero if
12205 GLYPH doesn't display a tool-bar item. */
12206
12207 static int
12208 tool_bar_item_info (struct frame *f, struct glyph *glyph, int *prop_idx)
12209 {
12210 Lisp_Object prop;
12211 int success_p;
12212 int charpos;
12213
12214 /* This function can be called asynchronously, which means we must
12215 exclude any possibility that Fget_text_property signals an
12216 error. */
12217 charpos = min (SCHARS (f->current_tool_bar_string), glyph->charpos);
12218 charpos = max (0, charpos);
12219
12220 /* Get the text property `menu-item' at pos. The value of that
12221 property is the start index of this item's properties in
12222 F->tool_bar_items. */
12223 prop = Fget_text_property (make_number (charpos),
12224 Qmenu_item, f->current_tool_bar_string);
12225 if (INTEGERP (prop))
12226 {
12227 *prop_idx = XINT (prop);
12228 success_p = 1;
12229 }
12230 else
12231 success_p = 0;
12232
12233 return success_p;
12234 }
12235
12236 \f
12237 /* Get information about the tool-bar item at position X/Y on frame F.
12238 Return in *GLYPH a pointer to the glyph of the tool-bar item in
12239 the current matrix of the tool-bar window of F, or NULL if not
12240 on a tool-bar item. Return in *PROP_IDX the index of the tool-bar
12241 item in F->tool_bar_items. Value is
12242
12243 -1 if X/Y is not on a tool-bar item
12244 0 if X/Y is on the same item that was highlighted before.
12245 1 otherwise. */
12246
12247 static int
12248 get_tool_bar_item (struct frame *f, int x, int y, struct glyph **glyph,
12249 int *hpos, int *vpos, int *prop_idx)
12250 {
12251 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12252 struct window *w = XWINDOW (f->tool_bar_window);
12253 int area;
12254
12255 /* Find the glyph under X/Y. */
12256 *glyph = x_y_to_hpos_vpos (w, x, y, hpos, vpos, 0, 0, &area);
12257 if (*glyph == NULL)
12258 return -1;
12259
12260 /* Get the start of this tool-bar item's properties in
12261 f->tool_bar_items. */
12262 if (!tool_bar_item_info (f, *glyph, prop_idx))
12263 return -1;
12264
12265 /* Is mouse on the highlighted item? */
12266 if (EQ (f->tool_bar_window, hlinfo->mouse_face_window)
12267 && *vpos >= hlinfo->mouse_face_beg_row
12268 && *vpos <= hlinfo->mouse_face_end_row
12269 && (*vpos > hlinfo->mouse_face_beg_row
12270 || *hpos >= hlinfo->mouse_face_beg_col)
12271 && (*vpos < hlinfo->mouse_face_end_row
12272 || *hpos < hlinfo->mouse_face_end_col
12273 || hlinfo->mouse_face_past_end))
12274 return 0;
12275
12276 return 1;
12277 }
12278
12279
12280 /* EXPORT:
12281 Handle mouse button event on the tool-bar of frame F, at
12282 frame-relative coordinates X/Y. DOWN_P is 1 for a button press,
12283 0 for button release. MODIFIERS is event modifiers for button
12284 release. */
12285
12286 void
12287 handle_tool_bar_click (struct frame *f, int x, int y, int down_p,
12288 int modifiers)
12289 {
12290 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12291 struct window *w = XWINDOW (f->tool_bar_window);
12292 int hpos, vpos, prop_idx;
12293 struct glyph *glyph;
12294 Lisp_Object enabled_p;
12295
12296 /* If not on the highlighted tool-bar item, return. */
12297 frame_to_window_pixel_xy (w, &x, &y);
12298 if (get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx) != 0)
12299 return;
12300
12301 /* If item is disabled, do nothing. */
12302 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12303 if (NILP (enabled_p))
12304 return;
12305
12306 if (down_p)
12307 {
12308 /* Show item in pressed state. */
12309 show_mouse_face (hlinfo, DRAW_IMAGE_SUNKEN);
12310 hlinfo->mouse_face_image_state = DRAW_IMAGE_SUNKEN;
12311 last_tool_bar_item = prop_idx;
12312 }
12313 else
12314 {
12315 Lisp_Object key, frame;
12316 struct input_event event;
12317 EVENT_INIT (event);
12318
12319 /* Show item in released state. */
12320 show_mouse_face (hlinfo, DRAW_IMAGE_RAISED);
12321 hlinfo->mouse_face_image_state = DRAW_IMAGE_RAISED;
12322
12323 key = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_KEY);
12324
12325 XSETFRAME (frame, f);
12326 event.kind = TOOL_BAR_EVENT;
12327 event.frame_or_window = frame;
12328 event.arg = frame;
12329 kbd_buffer_store_event (&event);
12330
12331 event.kind = TOOL_BAR_EVENT;
12332 event.frame_or_window = frame;
12333 event.arg = key;
12334 event.modifiers = modifiers;
12335 kbd_buffer_store_event (&event);
12336 last_tool_bar_item = -1;
12337 }
12338 }
12339
12340
12341 /* Possibly highlight a tool-bar item on frame F when mouse moves to
12342 tool-bar window-relative coordinates X/Y. Called from
12343 note_mouse_highlight. */
12344
12345 static void
12346 note_tool_bar_highlight (struct frame *f, int x, int y)
12347 {
12348 Lisp_Object window = f->tool_bar_window;
12349 struct window *w = XWINDOW (window);
12350 Display_Info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
12351 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
12352 int hpos, vpos;
12353 struct glyph *glyph;
12354 struct glyph_row *row;
12355 int i;
12356 Lisp_Object enabled_p;
12357 int prop_idx;
12358 enum draw_glyphs_face draw = DRAW_IMAGE_RAISED;
12359 int mouse_down_p, rc;
12360
12361 /* Function note_mouse_highlight is called with negative X/Y
12362 values when mouse moves outside of the frame. */
12363 if (x <= 0 || y <= 0)
12364 {
12365 clear_mouse_face (hlinfo);
12366 return;
12367 }
12368
12369 rc = get_tool_bar_item (f, x, y, &glyph, &hpos, &vpos, &prop_idx);
12370 if (rc < 0)
12371 {
12372 /* Not on tool-bar item. */
12373 clear_mouse_face (hlinfo);
12374 return;
12375 }
12376 else if (rc == 0)
12377 /* On same tool-bar item as before. */
12378 goto set_help_echo;
12379
12380 clear_mouse_face (hlinfo);
12381
12382 /* Mouse is down, but on different tool-bar item? */
12383 mouse_down_p = (dpyinfo->grabbed
12384 && f == last_mouse_frame
12385 && FRAME_LIVE_P (f));
12386 if (mouse_down_p
12387 && last_tool_bar_item != prop_idx)
12388 return;
12389
12390 hlinfo->mouse_face_image_state = DRAW_NORMAL_TEXT;
12391 draw = mouse_down_p ? DRAW_IMAGE_SUNKEN : DRAW_IMAGE_RAISED;
12392
12393 /* If tool-bar item is not enabled, don't highlight it. */
12394 enabled_p = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_ENABLED_P);
12395 if (!NILP (enabled_p))
12396 {
12397 /* Compute the x-position of the glyph. In front and past the
12398 image is a space. We include this in the highlighted area. */
12399 row = MATRIX_ROW (w->current_matrix, vpos);
12400 for (i = x = 0; i < hpos; ++i)
12401 x += row->glyphs[TEXT_AREA][i].pixel_width;
12402
12403 /* Record this as the current active region. */
12404 hlinfo->mouse_face_beg_col = hpos;
12405 hlinfo->mouse_face_beg_row = vpos;
12406 hlinfo->mouse_face_beg_x = x;
12407 hlinfo->mouse_face_beg_y = row->y;
12408 hlinfo->mouse_face_past_end = 0;
12409
12410 hlinfo->mouse_face_end_col = hpos + 1;
12411 hlinfo->mouse_face_end_row = vpos;
12412 hlinfo->mouse_face_end_x = x + glyph->pixel_width;
12413 hlinfo->mouse_face_end_y = row->y;
12414 hlinfo->mouse_face_window = window;
12415 hlinfo->mouse_face_face_id = TOOL_BAR_FACE_ID;
12416
12417 /* Display it as active. */
12418 show_mouse_face (hlinfo, draw);
12419 hlinfo->mouse_face_image_state = draw;
12420 }
12421
12422 set_help_echo:
12423
12424 /* Set help_echo_string to a help string to display for this tool-bar item.
12425 XTread_socket does the rest. */
12426 help_echo_object = help_echo_window = Qnil;
12427 help_echo_pos = -1;
12428 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_HELP);
12429 if (NILP (help_echo_string))
12430 help_echo_string = AREF (f->tool_bar_items, prop_idx + TOOL_BAR_ITEM_CAPTION);
12431 }
12432
12433 #endif /* HAVE_WINDOW_SYSTEM */
12434
12435
12436 \f
12437 /************************************************************************
12438 Horizontal scrolling
12439 ************************************************************************/
12440
12441 static int hscroll_window_tree (Lisp_Object);
12442 static int hscroll_windows (Lisp_Object);
12443
12444 /* For all leaf windows in the window tree rooted at WINDOW, set their
12445 hscroll value so that PT is (i) visible in the window, and (ii) so
12446 that it is not within a certain margin at the window's left and
12447 right border. Value is non-zero if any window's hscroll has been
12448 changed. */
12449
12450 static int
12451 hscroll_window_tree (Lisp_Object window)
12452 {
12453 int hscrolled_p = 0;
12454 int hscroll_relative_p = FLOATP (Vhscroll_step);
12455 int hscroll_step_abs = 0;
12456 double hscroll_step_rel = 0;
12457
12458 if (hscroll_relative_p)
12459 {
12460 hscroll_step_rel = XFLOAT_DATA (Vhscroll_step);
12461 if (hscroll_step_rel < 0)
12462 {
12463 hscroll_relative_p = 0;
12464 hscroll_step_abs = 0;
12465 }
12466 }
12467 else if (TYPE_RANGED_INTEGERP (int, Vhscroll_step))
12468 {
12469 hscroll_step_abs = XINT (Vhscroll_step);
12470 if (hscroll_step_abs < 0)
12471 hscroll_step_abs = 0;
12472 }
12473 else
12474 hscroll_step_abs = 0;
12475
12476 while (WINDOWP (window))
12477 {
12478 struct window *w = XWINDOW (window);
12479
12480 if (WINDOWP (w->hchild))
12481 hscrolled_p |= hscroll_window_tree (w->hchild);
12482 else if (WINDOWP (w->vchild))
12483 hscrolled_p |= hscroll_window_tree (w->vchild);
12484 else if (w->cursor.vpos >= 0)
12485 {
12486 int h_margin;
12487 int text_area_width;
12488 struct glyph_row *current_cursor_row
12489 = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
12490 struct glyph_row *desired_cursor_row
12491 = MATRIX_ROW (w->desired_matrix, w->cursor.vpos);
12492 struct glyph_row *cursor_row
12493 = (desired_cursor_row->enabled_p
12494 ? desired_cursor_row
12495 : current_cursor_row);
12496 int row_r2l_p = cursor_row->reversed_p;
12497
12498 text_area_width = window_box_width (w, TEXT_AREA);
12499
12500 /* Scroll when cursor is inside this scroll margin. */
12501 h_margin = hscroll_margin * WINDOW_FRAME_COLUMN_WIDTH (w);
12502
12503 if (!NILP (Fbuffer_local_value (Qauto_hscroll_mode, w->buffer))
12504 /* For left-to-right rows, hscroll when cursor is either
12505 (i) inside the right hscroll margin, or (ii) if it is
12506 inside the left margin and the window is already
12507 hscrolled. */
12508 && ((!row_r2l_p
12509 && ((w->hscroll
12510 && w->cursor.x <= h_margin)
12511 || (cursor_row->enabled_p
12512 && cursor_row->truncated_on_right_p
12513 && (w->cursor.x >= text_area_width - h_margin))))
12514 /* For right-to-left rows, the logic is similar,
12515 except that rules for scrolling to left and right
12516 are reversed. E.g., if cursor.x <= h_margin, we
12517 need to hscroll "to the right" unconditionally,
12518 and that will scroll the screen to the left so as
12519 to reveal the next portion of the row. */
12520 || (row_r2l_p
12521 && ((cursor_row->enabled_p
12522 /* FIXME: It is confusing to set the
12523 truncated_on_right_p flag when R2L rows
12524 are actually truncated on the left. */
12525 && cursor_row->truncated_on_right_p
12526 && w->cursor.x <= h_margin)
12527 || (w->hscroll
12528 && (w->cursor.x >= text_area_width - h_margin))))))
12529 {
12530 struct it it;
12531 ptrdiff_t hscroll;
12532 struct buffer *saved_current_buffer;
12533 ptrdiff_t pt;
12534 int wanted_x;
12535
12536 /* Find point in a display of infinite width. */
12537 saved_current_buffer = current_buffer;
12538 current_buffer = XBUFFER (w->buffer);
12539
12540 if (w == XWINDOW (selected_window))
12541 pt = PT;
12542 else
12543 {
12544 pt = marker_position (w->pointm);
12545 pt = max (BEGV, pt);
12546 pt = min (ZV, pt);
12547 }
12548
12549 /* Move iterator to pt starting at cursor_row->start in
12550 a line with infinite width. */
12551 init_to_row_start (&it, w, cursor_row);
12552 it.last_visible_x = INFINITY;
12553 move_it_in_display_line_to (&it, pt, -1, MOVE_TO_POS);
12554 current_buffer = saved_current_buffer;
12555
12556 /* Position cursor in window. */
12557 if (!hscroll_relative_p && hscroll_step_abs == 0)
12558 hscroll = max (0, (it.current_x
12559 - (ITERATOR_AT_END_OF_LINE_P (&it)
12560 ? (text_area_width - 4 * FRAME_COLUMN_WIDTH (it.f))
12561 : (text_area_width / 2))))
12562 / FRAME_COLUMN_WIDTH (it.f);
12563 else if ((!row_r2l_p
12564 && w->cursor.x >= text_area_width - h_margin)
12565 || (row_r2l_p && w->cursor.x <= h_margin))
12566 {
12567 if (hscroll_relative_p)
12568 wanted_x = text_area_width * (1 - hscroll_step_rel)
12569 - h_margin;
12570 else
12571 wanted_x = text_area_width
12572 - hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12573 - h_margin;
12574 hscroll
12575 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12576 }
12577 else
12578 {
12579 if (hscroll_relative_p)
12580 wanted_x = text_area_width * hscroll_step_rel
12581 + h_margin;
12582 else
12583 wanted_x = hscroll_step_abs * FRAME_COLUMN_WIDTH (it.f)
12584 + h_margin;
12585 hscroll
12586 = max (0, it.current_x - wanted_x) / FRAME_COLUMN_WIDTH (it.f);
12587 }
12588 hscroll = max (hscroll, w->min_hscroll);
12589
12590 /* Don't prevent redisplay optimizations if hscroll
12591 hasn't changed, as it will unnecessarily slow down
12592 redisplay. */
12593 if (w->hscroll != hscroll)
12594 {
12595 XBUFFER (w->buffer)->prevent_redisplay_optimizations_p = 1;
12596 w->hscroll = hscroll;
12597 hscrolled_p = 1;
12598 }
12599 }
12600 }
12601
12602 window = w->next;
12603 }
12604
12605 /* Value is non-zero if hscroll of any leaf window has been changed. */
12606 return hscrolled_p;
12607 }
12608
12609
12610 /* Set hscroll so that cursor is visible and not inside horizontal
12611 scroll margins for all windows in the tree rooted at WINDOW. See
12612 also hscroll_window_tree above. Value is non-zero if any window's
12613 hscroll has been changed. If it has, desired matrices on the frame
12614 of WINDOW are cleared. */
12615
12616 static int
12617 hscroll_windows (Lisp_Object window)
12618 {
12619 int hscrolled_p = hscroll_window_tree (window);
12620 if (hscrolled_p)
12621 clear_desired_matrices (XFRAME (WINDOW_FRAME (XWINDOW (window))));
12622 return hscrolled_p;
12623 }
12624
12625
12626 \f
12627 /************************************************************************
12628 Redisplay
12629 ************************************************************************/
12630
12631 /* Variables holding some state of redisplay if GLYPH_DEBUG is defined
12632 to a non-zero value. This is sometimes handy to have in a debugger
12633 session. */
12634
12635 #ifdef GLYPH_DEBUG
12636
12637 /* First and last unchanged row for try_window_id. */
12638
12639 static int debug_first_unchanged_at_end_vpos;
12640 static int debug_last_unchanged_at_beg_vpos;
12641
12642 /* Delta vpos and y. */
12643
12644 static int debug_dvpos, debug_dy;
12645
12646 /* Delta in characters and bytes for try_window_id. */
12647
12648 static ptrdiff_t debug_delta, debug_delta_bytes;
12649
12650 /* Values of window_end_pos and window_end_vpos at the end of
12651 try_window_id. */
12652
12653 static ptrdiff_t debug_end_vpos;
12654
12655 /* Append a string to W->desired_matrix->method. FMT is a printf
12656 format string. If trace_redisplay_p is non-zero also printf the
12657 resulting string to stderr. */
12658
12659 static void debug_method_add (struct window *, char const *, ...)
12660 ATTRIBUTE_FORMAT_PRINTF (2, 3);
12661
12662 static void
12663 debug_method_add (struct window *w, char const *fmt, ...)
12664 {
12665 char *method = w->desired_matrix->method;
12666 int len = strlen (method);
12667 int size = sizeof w->desired_matrix->method;
12668 int remaining = size - len - 1;
12669 va_list ap;
12670
12671 if (len && remaining)
12672 {
12673 method[len] = '|';
12674 --remaining, ++len;
12675 }
12676
12677 va_start (ap, fmt);
12678 vsnprintf (method + len, remaining + 1, fmt, ap);
12679 va_end (ap);
12680
12681 if (trace_redisplay_p)
12682 fprintf (stderr, "%p (%s): %s\n",
12683 w,
12684 ((BUFFERP (w->buffer)
12685 && STRINGP (BVAR (XBUFFER (w->buffer), name)))
12686 ? SSDATA (BVAR (XBUFFER (w->buffer), name))
12687 : "no buffer"),
12688 method + len);
12689 }
12690
12691 #endif /* GLYPH_DEBUG */
12692
12693
12694 /* Value is non-zero if all changes in window W, which displays
12695 current_buffer, are in the text between START and END. START is a
12696 buffer position, END is given as a distance from Z. Used in
12697 redisplay_internal for display optimization. */
12698
12699 static int
12700 text_outside_line_unchanged_p (struct window *w,
12701 ptrdiff_t start, ptrdiff_t end)
12702 {
12703 int unchanged_p = 1;
12704
12705 /* If text or overlays have changed, see where. */
12706 if (window_outdated (w))
12707 {
12708 /* Gap in the line? */
12709 if (GPT < start || Z - GPT < end)
12710 unchanged_p = 0;
12711
12712 /* Changes start in front of the line, or end after it? */
12713 if (unchanged_p
12714 && (BEG_UNCHANGED < start - 1
12715 || END_UNCHANGED < end))
12716 unchanged_p = 0;
12717
12718 /* If selective display, can't optimize if changes start at the
12719 beginning of the line. */
12720 if (unchanged_p
12721 && INTEGERP (BVAR (current_buffer, selective_display))
12722 && XINT (BVAR (current_buffer, selective_display)) > 0
12723 && (BEG_UNCHANGED < start || GPT <= start))
12724 unchanged_p = 0;
12725
12726 /* If there are overlays at the start or end of the line, these
12727 may have overlay strings with newlines in them. A change at
12728 START, for instance, may actually concern the display of such
12729 overlay strings as well, and they are displayed on different
12730 lines. So, quickly rule out this case. (For the future, it
12731 might be desirable to implement something more telling than
12732 just BEG/END_UNCHANGED.) */
12733 if (unchanged_p)
12734 {
12735 if (BEG + BEG_UNCHANGED == start
12736 && overlay_touches_p (start))
12737 unchanged_p = 0;
12738 if (END_UNCHANGED == end
12739 && overlay_touches_p (Z - end))
12740 unchanged_p = 0;
12741 }
12742
12743 /* Under bidi reordering, adding or deleting a character in the
12744 beginning of a paragraph, before the first strong directional
12745 character, can change the base direction of the paragraph (unless
12746 the buffer specifies a fixed paragraph direction), which will
12747 require to redisplay the whole paragraph. It might be worthwhile
12748 to find the paragraph limits and widen the range of redisplayed
12749 lines to that, but for now just give up this optimization. */
12750 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
12751 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
12752 unchanged_p = 0;
12753 }
12754
12755 return unchanged_p;
12756 }
12757
12758
12759 /* Do a frame update, taking possible shortcuts into account. This is
12760 the main external entry point for redisplay.
12761
12762 If the last redisplay displayed an echo area message and that message
12763 is no longer requested, we clear the echo area or bring back the
12764 mini-buffer if that is in use. */
12765
12766 void
12767 redisplay (void)
12768 {
12769 redisplay_internal ();
12770 }
12771
12772
12773 static Lisp_Object
12774 overlay_arrow_string_or_property (Lisp_Object var)
12775 {
12776 Lisp_Object val;
12777
12778 if (val = Fget (var, Qoverlay_arrow_string), STRINGP (val))
12779 return val;
12780
12781 return Voverlay_arrow_string;
12782 }
12783
12784 /* Return 1 if there are any overlay-arrows in current_buffer. */
12785 static int
12786 overlay_arrow_in_current_buffer_p (void)
12787 {
12788 Lisp_Object vlist;
12789
12790 for (vlist = Voverlay_arrow_variable_list;
12791 CONSP (vlist);
12792 vlist = XCDR (vlist))
12793 {
12794 Lisp_Object var = XCAR (vlist);
12795 Lisp_Object val;
12796
12797 if (!SYMBOLP (var))
12798 continue;
12799 val = find_symbol_value (var);
12800 if (MARKERP (val)
12801 && current_buffer == XMARKER (val)->buffer)
12802 return 1;
12803 }
12804 return 0;
12805 }
12806
12807
12808 /* Return 1 if any overlay_arrows have moved or overlay-arrow-string
12809 has changed. */
12810
12811 static int
12812 overlay_arrows_changed_p (void)
12813 {
12814 Lisp_Object vlist;
12815
12816 for (vlist = Voverlay_arrow_variable_list;
12817 CONSP (vlist);
12818 vlist = XCDR (vlist))
12819 {
12820 Lisp_Object var = XCAR (vlist);
12821 Lisp_Object val, pstr;
12822
12823 if (!SYMBOLP (var))
12824 continue;
12825 val = find_symbol_value (var);
12826 if (!MARKERP (val))
12827 continue;
12828 if (! EQ (COERCE_MARKER (val),
12829 Fget (var, Qlast_arrow_position))
12830 || ! (pstr = overlay_arrow_string_or_property (var),
12831 EQ (pstr, Fget (var, Qlast_arrow_string))))
12832 return 1;
12833 }
12834 return 0;
12835 }
12836
12837 /* Mark overlay arrows to be updated on next redisplay. */
12838
12839 static void
12840 update_overlay_arrows (int up_to_date)
12841 {
12842 Lisp_Object vlist;
12843
12844 for (vlist = Voverlay_arrow_variable_list;
12845 CONSP (vlist);
12846 vlist = XCDR (vlist))
12847 {
12848 Lisp_Object var = XCAR (vlist);
12849
12850 if (!SYMBOLP (var))
12851 continue;
12852
12853 if (up_to_date > 0)
12854 {
12855 Lisp_Object val = find_symbol_value (var);
12856 Fput (var, Qlast_arrow_position,
12857 COERCE_MARKER (val));
12858 Fput (var, Qlast_arrow_string,
12859 overlay_arrow_string_or_property (var));
12860 }
12861 else if (up_to_date < 0
12862 || !NILP (Fget (var, Qlast_arrow_position)))
12863 {
12864 Fput (var, Qlast_arrow_position, Qt);
12865 Fput (var, Qlast_arrow_string, Qt);
12866 }
12867 }
12868 }
12869
12870
12871 /* Return overlay arrow string to display at row.
12872 Return integer (bitmap number) for arrow bitmap in left fringe.
12873 Return nil if no overlay arrow. */
12874
12875 static Lisp_Object
12876 overlay_arrow_at_row (struct it *it, struct glyph_row *row)
12877 {
12878 Lisp_Object vlist;
12879
12880 for (vlist = Voverlay_arrow_variable_list;
12881 CONSP (vlist);
12882 vlist = XCDR (vlist))
12883 {
12884 Lisp_Object var = XCAR (vlist);
12885 Lisp_Object val;
12886
12887 if (!SYMBOLP (var))
12888 continue;
12889
12890 val = find_symbol_value (var);
12891
12892 if (MARKERP (val)
12893 && current_buffer == XMARKER (val)->buffer
12894 && (MATRIX_ROW_START_CHARPOS (row) == marker_position (val)))
12895 {
12896 if (FRAME_WINDOW_P (it->f)
12897 /* FIXME: if ROW->reversed_p is set, this should test
12898 the right fringe, not the left one. */
12899 && WINDOW_LEFT_FRINGE_WIDTH (it->w) > 0)
12900 {
12901 #ifdef HAVE_WINDOW_SYSTEM
12902 if (val = Fget (var, Qoverlay_arrow_bitmap), SYMBOLP (val))
12903 {
12904 int fringe_bitmap;
12905 if ((fringe_bitmap = lookup_fringe_bitmap (val)) != 0)
12906 return make_number (fringe_bitmap);
12907 }
12908 #endif
12909 return make_number (-1); /* Use default arrow bitmap. */
12910 }
12911 return overlay_arrow_string_or_property (var);
12912 }
12913 }
12914
12915 return Qnil;
12916 }
12917
12918 /* Return 1 if point moved out of or into a composition. Otherwise
12919 return 0. PREV_BUF and PREV_PT are the last point buffer and
12920 position. BUF and PT are the current point buffer and position. */
12921
12922 static int
12923 check_point_in_composition (struct buffer *prev_buf, ptrdiff_t prev_pt,
12924 struct buffer *buf, ptrdiff_t pt)
12925 {
12926 ptrdiff_t start, end;
12927 Lisp_Object prop;
12928 Lisp_Object buffer;
12929
12930 XSETBUFFER (buffer, buf);
12931 /* Check a composition at the last point if point moved within the
12932 same buffer. */
12933 if (prev_buf == buf)
12934 {
12935 if (prev_pt == pt)
12936 /* Point didn't move. */
12937 return 0;
12938
12939 if (prev_pt > BUF_BEGV (buf) && prev_pt < BUF_ZV (buf)
12940 && find_composition (prev_pt, -1, &start, &end, &prop, buffer)
12941 && COMPOSITION_VALID_P (start, end, prop)
12942 && start < prev_pt && end > prev_pt)
12943 /* The last point was within the composition. Return 1 iff
12944 point moved out of the composition. */
12945 return (pt <= start || pt >= end);
12946 }
12947
12948 /* Check a composition at the current point. */
12949 return (pt > BUF_BEGV (buf) && pt < BUF_ZV (buf)
12950 && find_composition (pt, -1, &start, &end, &prop, buffer)
12951 && COMPOSITION_VALID_P (start, end, prop)
12952 && start < pt && end > pt);
12953 }
12954
12955
12956 /* Reconsider the setting of B->clip_changed which is displayed
12957 in window W. */
12958
12959 static void
12960 reconsider_clip_changes (struct window *w, struct buffer *b)
12961 {
12962 if (b->clip_changed
12963 && !NILP (w->window_end_valid)
12964 && w->current_matrix->buffer == b
12965 && w->current_matrix->zv == BUF_ZV (b)
12966 && w->current_matrix->begv == BUF_BEGV (b))
12967 b->clip_changed = 0;
12968
12969 /* If display wasn't paused, and W is not a tool bar window, see if
12970 point has been moved into or out of a composition. In that case,
12971 we set b->clip_changed to 1 to force updating the screen. If
12972 b->clip_changed has already been set to 1, we can skip this
12973 check. */
12974 if (!b->clip_changed
12975 && BUFFERP (w->buffer) && !NILP (w->window_end_valid))
12976 {
12977 ptrdiff_t pt;
12978
12979 if (w == XWINDOW (selected_window))
12980 pt = PT;
12981 else
12982 pt = marker_position (w->pointm);
12983
12984 if ((w->current_matrix->buffer != XBUFFER (w->buffer)
12985 || pt != w->last_point)
12986 && check_point_in_composition (w->current_matrix->buffer,
12987 w->last_point,
12988 XBUFFER (w->buffer), pt))
12989 b->clip_changed = 1;
12990 }
12991 }
12992 \f
12993
12994 /* Select FRAME to forward the values of frame-local variables into C
12995 variables so that the redisplay routines can access those values
12996 directly. */
12997
12998 static void
12999 select_frame_for_redisplay (Lisp_Object frame)
13000 {
13001 Lisp_Object tail, tem;
13002 Lisp_Object old = selected_frame;
13003 struct Lisp_Symbol *sym;
13004
13005 eassert (FRAMEP (frame) && FRAME_LIVE_P (XFRAME (frame)));
13006
13007 selected_frame = frame;
13008
13009 do {
13010 for (tail = XFRAME (frame)->param_alist;
13011 CONSP (tail); tail = XCDR (tail))
13012 if (CONSP (XCAR (tail))
13013 && (tem = XCAR (XCAR (tail)),
13014 SYMBOLP (tem))
13015 && (sym = indirect_variable (XSYMBOL (tem)),
13016 sym->redirect == SYMBOL_LOCALIZED)
13017 && sym->val.blv->frame_local)
13018 /* Use find_symbol_value rather than Fsymbol_value
13019 to avoid an error if it is void. */
13020 find_symbol_value (tem);
13021 } while (!EQ (frame, old) && (frame = old, 1));
13022 }
13023
13024
13025 #define STOP_POLLING \
13026 do { if (! polling_stopped_here) stop_polling (); \
13027 polling_stopped_here = 1; } while (0)
13028
13029 #define RESUME_POLLING \
13030 do { if (polling_stopped_here) start_polling (); \
13031 polling_stopped_here = 0; } while (0)
13032
13033
13034 /* Perhaps in the future avoid recentering windows if it
13035 is not necessary; currently that causes some problems. */
13036
13037 static void
13038 redisplay_internal (void)
13039 {
13040 struct window *w = XWINDOW (selected_window);
13041 struct window *sw;
13042 struct frame *fr;
13043 int pending;
13044 int must_finish = 0;
13045 struct text_pos tlbufpos, tlendpos;
13046 int number_of_visible_frames;
13047 ptrdiff_t count, count1;
13048 struct frame *sf;
13049 int polling_stopped_here = 0;
13050 Lisp_Object tail, frame, old_frame = selected_frame;
13051 struct backtrace backtrace;
13052
13053 /* Non-zero means redisplay has to consider all windows on all
13054 frames. Zero means, only selected_window is considered. */
13055 int consider_all_windows_p;
13056
13057 /* Non-zero means redisplay has to redisplay the miniwindow. */
13058 int update_miniwindow_p = 0;
13059
13060 TRACE ((stderr, "redisplay_internal %d\n", redisplaying_p));
13061
13062 /* No redisplay if running in batch mode or frame is not yet fully
13063 initialized, or redisplay is explicitly turned off by setting
13064 Vinhibit_redisplay. */
13065 if (FRAME_INITIAL_P (SELECTED_FRAME ())
13066 || !NILP (Vinhibit_redisplay))
13067 return;
13068
13069 /* Don't examine these until after testing Vinhibit_redisplay.
13070 When Emacs is shutting down, perhaps because its connection to
13071 X has dropped, we should not look at them at all. */
13072 fr = XFRAME (w->frame);
13073 sf = SELECTED_FRAME ();
13074
13075 if (!fr->glyphs_initialized_p)
13076 return;
13077
13078 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS)
13079 if (popup_activated ())
13080 return;
13081 #endif
13082
13083 /* I don't think this happens but let's be paranoid. */
13084 if (redisplaying_p)
13085 return;
13086
13087 /* Record a function that clears redisplaying_p
13088 when we leave this function. */
13089 count = SPECPDL_INDEX ();
13090 record_unwind_protect (unwind_redisplay, selected_frame);
13091 redisplaying_p = 1;
13092 specbind (Qinhibit_free_realized_faces, Qnil);
13093
13094 /* Record this function, so it appears on the profiler's backtraces. */
13095 backtrace.next = backtrace_list;
13096 backtrace.function = Qredisplay_internal;
13097 backtrace.args = &Qnil;
13098 backtrace.nargs = 0;
13099 backtrace.debug_on_exit = 0;
13100 backtrace_list = &backtrace;
13101
13102 FOR_EACH_FRAME (tail, frame)
13103 XFRAME (frame)->already_hscrolled_p = 0;
13104
13105 retry:
13106 /* Remember the currently selected window. */
13107 sw = w;
13108
13109 if (!EQ (old_frame, selected_frame)
13110 && FRAME_LIVE_P (XFRAME (old_frame)))
13111 /* When running redisplay, we play a bit fast-and-loose and allow e.g.
13112 selected_frame and selected_window to be temporarily out-of-sync so
13113 when we come back here via `goto retry', we need to resync because we
13114 may need to run Elisp code (via prepare_menu_bars). */
13115 select_frame_for_redisplay (old_frame);
13116
13117 pending = 0;
13118 reconsider_clip_changes (w, current_buffer);
13119 last_escape_glyph_frame = NULL;
13120 last_escape_glyph_face_id = (1 << FACE_ID_BITS);
13121 last_glyphless_glyph_frame = NULL;
13122 last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
13123
13124 /* If new fonts have been loaded that make a glyph matrix adjustment
13125 necessary, do it. */
13126 if (fonts_changed_p)
13127 {
13128 adjust_glyphs (NULL);
13129 ++windows_or_buffers_changed;
13130 fonts_changed_p = 0;
13131 }
13132
13133 /* If face_change_count is non-zero, init_iterator will free all
13134 realized faces, which includes the faces referenced from current
13135 matrices. So, we can't reuse current matrices in this case. */
13136 if (face_change_count)
13137 ++windows_or_buffers_changed;
13138
13139 if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
13140 && FRAME_TTY (sf)->previous_frame != sf)
13141 {
13142 /* Since frames on a single ASCII terminal share the same
13143 display area, displaying a different frame means redisplay
13144 the whole thing. */
13145 windows_or_buffers_changed++;
13146 SET_FRAME_GARBAGED (sf);
13147 #ifndef DOS_NT
13148 set_tty_color_mode (FRAME_TTY (sf), sf);
13149 #endif
13150 FRAME_TTY (sf)->previous_frame = sf;
13151 }
13152
13153 /* Set the visible flags for all frames. Do this before checking for
13154 resized or garbaged frames; they want to know if their frames are
13155 visible. See the comment in frame.h for FRAME_SAMPLE_VISIBILITY. */
13156 number_of_visible_frames = 0;
13157
13158 FOR_EACH_FRAME (tail, frame)
13159 {
13160 struct frame *f = XFRAME (frame);
13161
13162 FRAME_SAMPLE_VISIBILITY (f);
13163 if (FRAME_VISIBLE_P (f))
13164 ++number_of_visible_frames;
13165 clear_desired_matrices (f);
13166 }
13167
13168 /* Notice any pending interrupt request to change frame size. */
13169 do_pending_window_change (1);
13170
13171 /* do_pending_window_change could change the selected_window due to
13172 frame resizing which makes the selected window too small. */
13173 if (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw)
13174 {
13175 sw = w;
13176 reconsider_clip_changes (w, current_buffer);
13177 }
13178
13179 /* Clear frames marked as garbaged. */
13180 clear_garbaged_frames ();
13181
13182 /* Build menubar and tool-bar items. */
13183 if (NILP (Vmemory_full))
13184 prepare_menu_bars ();
13185
13186 if (windows_or_buffers_changed)
13187 update_mode_lines++;
13188
13189 /* Detect case that we need to write or remove a star in the mode line. */
13190 if ((SAVE_MODIFF < MODIFF) != w->last_had_star)
13191 {
13192 w->update_mode_line = 1;
13193 if (buffer_shared_and_changed ())
13194 update_mode_lines++;
13195 }
13196
13197 /* Avoid invocation of point motion hooks by `current_column' below. */
13198 count1 = SPECPDL_INDEX ();
13199 specbind (Qinhibit_point_motion_hooks, Qt);
13200
13201 /* If %c is in the mode line, update it if needed. */
13202 if (!NILP (w->column_number_displayed)
13203 /* This alternative quickly identifies a common case
13204 where no change is needed. */
13205 && !(PT == w->last_point && !window_outdated (w))
13206 && (XFASTINT (w->column_number_displayed) != current_column ()))
13207 w->update_mode_line = 1;
13208
13209 unbind_to (count1, Qnil);
13210
13211 FRAME_SCROLL_BOTTOM_VPOS (XFRAME (w->frame)) = -1;
13212
13213 /* The variable buffer_shared is set in redisplay_window and
13214 indicates that we redisplay a buffer in different windows. See
13215 there. */
13216 consider_all_windows_p = (update_mode_lines
13217 || buffer_shared_and_changed ()
13218 || cursor_type_changed);
13219
13220 /* If specs for an arrow have changed, do thorough redisplay
13221 to ensure we remove any arrow that should no longer exist. */
13222 if (overlay_arrows_changed_p ())
13223 consider_all_windows_p = windows_or_buffers_changed = 1;
13224
13225 /* Normally the message* functions will have already displayed and
13226 updated the echo area, but the frame may have been trashed, or
13227 the update may have been preempted, so display the echo area
13228 again here. Checking message_cleared_p captures the case that
13229 the echo area should be cleared. */
13230 if ((!NILP (echo_area_buffer[0]) && !display_last_displayed_message_p)
13231 || (!NILP (echo_area_buffer[1]) && display_last_displayed_message_p)
13232 || (message_cleared_p
13233 && minibuf_level == 0
13234 /* If the mini-window is currently selected, this means the
13235 echo-area doesn't show through. */
13236 && !MINI_WINDOW_P (XWINDOW (selected_window))))
13237 {
13238 int window_height_changed_p = echo_area_display (0);
13239
13240 if (message_cleared_p)
13241 update_miniwindow_p = 1;
13242
13243 must_finish = 1;
13244
13245 /* If we don't display the current message, don't clear the
13246 message_cleared_p flag, because, if we did, we wouldn't clear
13247 the echo area in the next redisplay which doesn't preserve
13248 the echo area. */
13249 if (!display_last_displayed_message_p)
13250 message_cleared_p = 0;
13251
13252 if (fonts_changed_p)
13253 goto retry;
13254 else if (window_height_changed_p)
13255 {
13256 consider_all_windows_p = 1;
13257 ++update_mode_lines;
13258 ++windows_or_buffers_changed;
13259
13260 /* If window configuration was changed, frames may have been
13261 marked garbaged. Clear them or we will experience
13262 surprises wrt scrolling. */
13263 clear_garbaged_frames ();
13264 }
13265 }
13266 else if (EQ (selected_window, minibuf_window)
13267 && (current_buffer->clip_changed || window_outdated (w))
13268 && resize_mini_window (w, 0))
13269 {
13270 /* Resized active mini-window to fit the size of what it is
13271 showing if its contents might have changed. */
13272 must_finish = 1;
13273 /* FIXME: this causes all frames to be updated, which seems unnecessary
13274 since only the current frame needs to be considered. This function
13275 needs to be rewritten with two variables, consider_all_windows and
13276 consider_all_frames. */
13277 consider_all_windows_p = 1;
13278 ++windows_or_buffers_changed;
13279 ++update_mode_lines;
13280
13281 /* If window configuration was changed, frames may have been
13282 marked garbaged. Clear them or we will experience
13283 surprises wrt scrolling. */
13284 clear_garbaged_frames ();
13285 }
13286
13287
13288 /* If showing the region, and mark has changed, we must redisplay
13289 the whole window. The assignment to this_line_start_pos prevents
13290 the optimization directly below this if-statement. */
13291 if (((!NILP (Vtransient_mark_mode)
13292 && !NILP (BVAR (XBUFFER (w->buffer), mark_active)))
13293 != !NILP (w->region_showing))
13294 || (!NILP (w->region_showing)
13295 && !EQ (w->region_showing,
13296 Fmarker_position (BVAR (XBUFFER (w->buffer), mark)))))
13297 CHARPOS (this_line_start_pos) = 0;
13298
13299 /* Optimize the case that only the line containing the cursor in the
13300 selected window has changed. Variables starting with this_ are
13301 set in display_line and record information about the line
13302 containing the cursor. */
13303 tlbufpos = this_line_start_pos;
13304 tlendpos = this_line_end_pos;
13305 if (!consider_all_windows_p
13306 && CHARPOS (tlbufpos) > 0
13307 && !w->update_mode_line
13308 && !current_buffer->clip_changed
13309 && !current_buffer->prevent_redisplay_optimizations_p
13310 && FRAME_VISIBLE_P (XFRAME (w->frame))
13311 && !FRAME_OBSCURED_P (XFRAME (w->frame))
13312 /* Make sure recorded data applies to current buffer, etc. */
13313 && this_line_buffer == current_buffer
13314 && current_buffer == XBUFFER (w->buffer)
13315 && !w->force_start
13316 && !w->optional_new_start
13317 /* Point must be on the line that we have info recorded about. */
13318 && PT >= CHARPOS (tlbufpos)
13319 && PT <= Z - CHARPOS (tlendpos)
13320 /* All text outside that line, including its final newline,
13321 must be unchanged. */
13322 && text_outside_line_unchanged_p (w, CHARPOS (tlbufpos),
13323 CHARPOS (tlendpos)))
13324 {
13325 if (CHARPOS (tlbufpos) > BEGV
13326 && FETCH_BYTE (BYTEPOS (tlbufpos) - 1) != '\n'
13327 && (CHARPOS (tlbufpos) == ZV
13328 || FETCH_BYTE (BYTEPOS (tlbufpos)) == '\n'))
13329 /* Former continuation line has disappeared by becoming empty. */
13330 goto cancel;
13331 else if (window_outdated (w) || MINI_WINDOW_P (w))
13332 {
13333 /* We have to handle the case of continuation around a
13334 wide-column character (see the comment in indent.c around
13335 line 1340).
13336
13337 For instance, in the following case:
13338
13339 -------- Insert --------
13340 K_A_N_\\ `a' K_A_N_a\ `X_' are wide-column chars.
13341 J_I_ ==> J_I_ `^^' are cursors.
13342 ^^ ^^
13343 -------- --------
13344
13345 As we have to redraw the line above, we cannot use this
13346 optimization. */
13347
13348 struct it it;
13349 int line_height_before = this_line_pixel_height;
13350
13351 /* Note that start_display will handle the case that the
13352 line starting at tlbufpos is a continuation line. */
13353 start_display (&it, w, tlbufpos);
13354
13355 /* Implementation note: It this still necessary? */
13356 if (it.current_x != this_line_start_x)
13357 goto cancel;
13358
13359 TRACE ((stderr, "trying display optimization 1\n"));
13360 w->cursor.vpos = -1;
13361 overlay_arrow_seen = 0;
13362 it.vpos = this_line_vpos;
13363 it.current_y = this_line_y;
13364 it.glyph_row = MATRIX_ROW (w->desired_matrix, this_line_vpos);
13365 display_line (&it);
13366
13367 /* If line contains point, is not continued,
13368 and ends at same distance from eob as before, we win. */
13369 if (w->cursor.vpos >= 0
13370 /* Line is not continued, otherwise this_line_start_pos
13371 would have been set to 0 in display_line. */
13372 && CHARPOS (this_line_start_pos)
13373 /* Line ends as before. */
13374 && CHARPOS (this_line_end_pos) == CHARPOS (tlendpos)
13375 /* Line has same height as before. Otherwise other lines
13376 would have to be shifted up or down. */
13377 && this_line_pixel_height == line_height_before)
13378 {
13379 /* If this is not the window's last line, we must adjust
13380 the charstarts of the lines below. */
13381 if (it.current_y < it.last_visible_y)
13382 {
13383 struct glyph_row *row
13384 = MATRIX_ROW (w->current_matrix, this_line_vpos + 1);
13385 ptrdiff_t delta, delta_bytes;
13386
13387 /* We used to distinguish between two cases here,
13388 conditioned by Z - CHARPOS (tlendpos) == ZV, for
13389 when the line ends in a newline or the end of the
13390 buffer's accessible portion. But both cases did
13391 the same, so they were collapsed. */
13392 delta = (Z
13393 - CHARPOS (tlendpos)
13394 - MATRIX_ROW_START_CHARPOS (row));
13395 delta_bytes = (Z_BYTE
13396 - BYTEPOS (tlendpos)
13397 - MATRIX_ROW_START_BYTEPOS (row));
13398
13399 increment_matrix_positions (w->current_matrix,
13400 this_line_vpos + 1,
13401 w->current_matrix->nrows,
13402 delta, delta_bytes);
13403 }
13404
13405 /* If this row displays text now but previously didn't,
13406 or vice versa, w->window_end_vpos may have to be
13407 adjusted. */
13408 if ((it.glyph_row - 1)->displays_text_p)
13409 {
13410 if (XFASTINT (w->window_end_vpos) < this_line_vpos)
13411 wset_window_end_vpos (w, make_number (this_line_vpos));
13412 }
13413 else if (XFASTINT (w->window_end_vpos) == this_line_vpos
13414 && this_line_vpos > 0)
13415 wset_window_end_vpos (w, make_number (this_line_vpos - 1));
13416 wset_window_end_valid (w, Qnil);
13417
13418 /* Update hint: No need to try to scroll in update_window. */
13419 w->desired_matrix->no_scrolling_p = 1;
13420
13421 #ifdef GLYPH_DEBUG
13422 *w->desired_matrix->method = 0;
13423 debug_method_add (w, "optimization 1");
13424 #endif
13425 #if HAVE_XWIDGETS
13426 //debug optimization movement issue
13427 //w->desired_matrix->no_scrolling_p = 1;
13428 //*w->desired_matrix->method = 0;
13429 //debug_method_add (w, "optimization 1");
13430 #endif
13431
13432 #ifdef HAVE_WINDOW_SYSTEM
13433 update_window_fringes (w, 0);
13434 #endif
13435 goto update;
13436 }
13437 else
13438 goto cancel;
13439 }
13440 else if (/* Cursor position hasn't changed. */
13441 PT == w->last_point
13442 /* Make sure the cursor was last displayed
13443 in this window. Otherwise we have to reposition it. */
13444 && 0 <= w->cursor.vpos
13445 && WINDOW_TOTAL_LINES (w) > w->cursor.vpos)
13446 {
13447 if (!must_finish)
13448 {
13449 do_pending_window_change (1);
13450 /* If selected_window changed, redisplay again. */
13451 if (WINDOWP (selected_window)
13452 && (w = XWINDOW (selected_window)) != sw)
13453 goto retry;
13454
13455 /* We used to always goto end_of_redisplay here, but this
13456 isn't enough if we have a blinking cursor. */
13457 if (w->cursor_off_p == w->last_cursor_off_p)
13458 goto end_of_redisplay;
13459 }
13460 goto update;
13461 }
13462 /* If highlighting the region, or if the cursor is in the echo area,
13463 then we can't just move the cursor. */
13464 else if (! (!NILP (Vtransient_mark_mode)
13465 && !NILP (BVAR (current_buffer, mark_active)))
13466 && (EQ (selected_window,
13467 BVAR (current_buffer, last_selected_window))
13468 || highlight_nonselected_windows)
13469 && NILP (w->region_showing)
13470 && NILP (Vshow_trailing_whitespace)
13471 && !cursor_in_echo_area)
13472 {
13473 struct it it;
13474 struct glyph_row *row;
13475
13476 /* Skip from tlbufpos to PT and see where it is. Note that
13477 PT may be in invisible text. If so, we will end at the
13478 next visible position. */
13479 init_iterator (&it, w, CHARPOS (tlbufpos), BYTEPOS (tlbufpos),
13480 NULL, DEFAULT_FACE_ID);
13481 it.current_x = this_line_start_x;
13482 it.current_y = this_line_y;
13483 it.vpos = this_line_vpos;
13484
13485 /* The call to move_it_to stops in front of PT, but
13486 moves over before-strings. */
13487 move_it_to (&it, PT, -1, -1, -1, MOVE_TO_POS);
13488
13489 if (it.vpos == this_line_vpos
13490 && (row = MATRIX_ROW (w->current_matrix, this_line_vpos),
13491 row->enabled_p))
13492 {
13493 eassert (this_line_vpos == it.vpos);
13494 eassert (this_line_y == it.current_y);
13495 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
13496 #ifdef GLYPH_DEBUG
13497 *w->desired_matrix->method = 0;
13498 debug_method_add (w, "optimization 3");
13499 #endif
13500 goto update;
13501 }
13502 else
13503 goto cancel;
13504 }
13505
13506 cancel:
13507 /* Text changed drastically or point moved off of line. */
13508 SET_MATRIX_ROW_ENABLED_P (w->desired_matrix, this_line_vpos, 0);
13509 }
13510
13511 CHARPOS (this_line_start_pos) = 0;
13512 consider_all_windows_p |= buffer_shared_and_changed ();
13513 ++clear_face_cache_count;
13514 #ifdef HAVE_WINDOW_SYSTEM
13515 ++clear_image_cache_count;
13516 #endif
13517
13518 /* Build desired matrices, and update the display. If
13519 consider_all_windows_p is non-zero, do it for all windows on all
13520 frames. Otherwise do it for selected_window, only. */
13521
13522 if (consider_all_windows_p)
13523 {
13524 FOR_EACH_FRAME (tail, frame)
13525 XFRAME (frame)->updated_p = 0;
13526
13527 /* Recompute # windows showing selected buffer. This will be
13528 incremented each time such a window is displayed. */
13529 buffer_shared = 0;
13530
13531 FOR_EACH_FRAME (tail, frame)
13532 {
13533 struct frame *f = XFRAME (frame);
13534
13535 /* We don't have to do anything for unselected terminal
13536 frames. */
13537 if ((FRAME_TERMCAP_P (f) || FRAME_MSDOS_P (f))
13538 && !EQ (FRAME_TTY (f)->top_frame, frame))
13539 continue;
13540
13541 if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
13542 {
13543 if (! EQ (frame, selected_frame))
13544 /* Select the frame, for the sake of frame-local
13545 variables. */
13546 select_frame_for_redisplay (frame);
13547
13548 /* Mark all the scroll bars to be removed; we'll redeem
13549 the ones we want when we redisplay their windows. */
13550 if (FRAME_TERMINAL (f)->condemn_scroll_bars_hook)
13551 FRAME_TERMINAL (f)->condemn_scroll_bars_hook (f);
13552
13553 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13554 redisplay_windows (FRAME_ROOT_WINDOW (f));
13555
13556 /* The X error handler may have deleted that frame. */
13557 if (!FRAME_LIVE_P (f))
13558 continue;
13559
13560 /* Any scroll bars which redisplay_windows should have
13561 nuked should now go away. */
13562 if (FRAME_TERMINAL (f)->judge_scroll_bars_hook)
13563 FRAME_TERMINAL (f)->judge_scroll_bars_hook (f);
13564
13565 /* If fonts changed, display again. */
13566 /* ??? rms: I suspect it is a mistake to jump all the way
13567 back to retry here. It should just retry this frame. */
13568 if (fonts_changed_p)
13569 goto retry;
13570
13571 if (FRAME_VISIBLE_P (f) && !FRAME_OBSCURED_P (f))
13572 {
13573 /* See if we have to hscroll. */
13574 if (!f->already_hscrolled_p)
13575 {
13576 f->already_hscrolled_p = 1;
13577 if (hscroll_windows (f->root_window))
13578 goto retry;
13579 }
13580
13581 /* Prevent various kinds of signals during display
13582 update. stdio is not robust about handling
13583 signals, which can cause an apparent I/O
13584 error. */
13585 if (interrupt_input)
13586 unrequest_sigio ();
13587 STOP_POLLING;
13588
13589 /* Update the display. */
13590 set_window_update_flags (XWINDOW (f->root_window), 1);
13591 pending |= update_frame (f, 0, 0);
13592 f->updated_p = 1;
13593 }
13594 }
13595 }
13596
13597 if (!EQ (old_frame, selected_frame)
13598 && FRAME_LIVE_P (XFRAME (old_frame)))
13599 /* We played a bit fast-and-loose above and allowed selected_frame
13600 and selected_window to be temporarily out-of-sync but let's make
13601 sure this stays contained. */
13602 select_frame_for_redisplay (old_frame);
13603 eassert (EQ (XFRAME (selected_frame)->selected_window,
13604 selected_window));
13605
13606 if (!pending)
13607 {
13608 /* Do the mark_window_display_accurate after all windows have
13609 been redisplayed because this call resets flags in buffers
13610 which are needed for proper redisplay. */
13611 FOR_EACH_FRAME (tail, frame)
13612 {
13613 struct frame *f = XFRAME (frame);
13614 if (f->updated_p)
13615 {
13616 mark_window_display_accurate (f->root_window, 1);
13617 if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
13618 FRAME_TERMINAL (f)->frame_up_to_date_hook (f);
13619 }
13620 }
13621 }
13622 }
13623 else if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13624 {
13625 Lisp_Object mini_window = FRAME_MINIBUF_WINDOW (sf);
13626 struct frame *mini_frame;
13627
13628 displayed_buffer = XBUFFER (XWINDOW (selected_window)->buffer);
13629 /* Use list_of_error, not Qerror, so that
13630 we catch only errors and don't run the debugger. */
13631 internal_condition_case_1 (redisplay_window_1, selected_window,
13632 list_of_error,
13633 redisplay_window_error);
13634 if (update_miniwindow_p)
13635 internal_condition_case_1 (redisplay_window_1, mini_window,
13636 list_of_error,
13637 redisplay_window_error);
13638
13639 /* Compare desired and current matrices, perform output. */
13640
13641 update:
13642 /* If fonts changed, display again. */
13643 if (fonts_changed_p)
13644 goto retry;
13645
13646 /* Prevent various kinds of signals during display update.
13647 stdio is not robust about handling signals,
13648 which can cause an apparent I/O error. */
13649 if (interrupt_input)
13650 unrequest_sigio ();
13651 STOP_POLLING;
13652
13653 if (FRAME_VISIBLE_P (sf) && !FRAME_OBSCURED_P (sf))
13654 {
13655 if (hscroll_windows (selected_window))
13656 goto retry;
13657
13658 XWINDOW (selected_window)->must_be_updated_p = 1;
13659 pending = update_frame (sf, 0, 0);
13660 }
13661
13662 /* We may have called echo_area_display at the top of this
13663 function. If the echo area is on another frame, that may
13664 have put text on a frame other than the selected one, so the
13665 above call to update_frame would not have caught it. Catch
13666 it here. */
13667 mini_window = FRAME_MINIBUF_WINDOW (sf);
13668 mini_frame = XFRAME (WINDOW_FRAME (XWINDOW (mini_window)));
13669
13670 if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
13671 {
13672 XWINDOW (mini_window)->must_be_updated_p = 1;
13673 pending |= update_frame (mini_frame, 0, 0);
13674 if (!pending && hscroll_windows (mini_window))
13675 goto retry;
13676 }
13677 }
13678
13679 /* If display was paused because of pending input, make sure we do a
13680 thorough update the next time. */
13681 if (pending)
13682 {
13683 /* Prevent the optimization at the beginning of
13684 redisplay_internal that tries a single-line update of the
13685 line containing the cursor in the selected window. */
13686 CHARPOS (this_line_start_pos) = 0;
13687
13688 /* Let the overlay arrow be updated the next time. */
13689 update_overlay_arrows (0);
13690
13691 /* If we pause after scrolling, some rows in the current
13692 matrices of some windows are not valid. */
13693 if (!WINDOW_FULL_WIDTH_P (w)
13694 && !FRAME_WINDOW_P (XFRAME (w->frame)))
13695 update_mode_lines = 1;
13696 }
13697 else
13698 {
13699 if (!consider_all_windows_p)
13700 {
13701 /* This has already been done above if
13702 consider_all_windows_p is set. */
13703 mark_window_display_accurate_1 (w, 1);
13704
13705 /* Say overlay arrows are up to date. */
13706 update_overlay_arrows (1);
13707
13708 if (FRAME_TERMINAL (sf)->frame_up_to_date_hook != 0)
13709 FRAME_TERMINAL (sf)->frame_up_to_date_hook (sf);
13710 }
13711
13712 update_mode_lines = 0;
13713 windows_or_buffers_changed = 0;
13714 cursor_type_changed = 0;
13715 }
13716
13717 /* Start SIGIO interrupts coming again. Having them off during the
13718 code above makes it less likely one will discard output, but not
13719 impossible, since there might be stuff in the system buffer here.
13720 But it is much hairier to try to do anything about that. */
13721 if (interrupt_input)
13722 request_sigio ();
13723 RESUME_POLLING;
13724
13725 /* If a frame has become visible which was not before, redisplay
13726 again, so that we display it. Expose events for such a frame
13727 (which it gets when becoming visible) don't call the parts of
13728 redisplay constructing glyphs, so simply exposing a frame won't
13729 display anything in this case. So, we have to display these
13730 frames here explicitly. */
13731 if (!pending)
13732 {
13733 int new_count = 0;
13734
13735 FOR_EACH_FRAME (tail, frame)
13736 {
13737 int this_is_visible = 0;
13738
13739 if (XFRAME (frame)->visible)
13740 this_is_visible = 1;
13741 FRAME_SAMPLE_VISIBILITY (XFRAME (frame));
13742 if (XFRAME (frame)->visible)
13743 this_is_visible = 1;
13744
13745 if (this_is_visible)
13746 new_count++;
13747 }
13748
13749 if (new_count != number_of_visible_frames)
13750 windows_or_buffers_changed++;
13751 }
13752
13753 /* Change frame size now if a change is pending. */
13754 do_pending_window_change (1);
13755
13756 /* If we just did a pending size change, or have additional
13757 visible frames, or selected_window changed, redisplay again. */
13758 if ((windows_or_buffers_changed && !pending)
13759 || (WINDOWP (selected_window) && (w = XWINDOW (selected_window)) != sw))
13760 goto retry;
13761
13762 /* Clear the face and image caches.
13763
13764 We used to do this only if consider_all_windows_p. But the cache
13765 needs to be cleared if a timer creates images in the current
13766 buffer (e.g. the test case in Bug#6230). */
13767
13768 if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
13769 {
13770 clear_face_cache (0);
13771 clear_face_cache_count = 0;
13772 }
13773
13774 #ifdef HAVE_WINDOW_SYSTEM
13775 if (clear_image_cache_count > CLEAR_IMAGE_CACHE_COUNT)
13776 {
13777 clear_image_caches (Qnil);
13778 clear_image_cache_count = 0;
13779 }
13780 #endif /* HAVE_WINDOW_SYSTEM */
13781
13782 end_of_redisplay:
13783 backtrace_list = backtrace.next;
13784 unbind_to (count, Qnil);
13785 RESUME_POLLING;
13786 }
13787
13788
13789 /* Redisplay, but leave alone any recent echo area message unless
13790 another message has been requested in its place.
13791
13792 This is useful in situations where you need to redisplay but no
13793 user action has occurred, making it inappropriate for the message
13794 area to be cleared. See tracking_off and
13795 wait_reading_process_output for examples of these situations.
13796
13797 FROM_WHERE is an integer saying from where this function was
13798 called. This is useful for debugging. */
13799
13800 void
13801 redisplay_preserve_echo_area (int from_where)
13802 {
13803 TRACE ((stderr, "redisplay_preserve_echo_area (%d)\n", from_where));
13804
13805 if (!NILP (echo_area_buffer[1]))
13806 {
13807 /* We have a previously displayed message, but no current
13808 message. Redisplay the previous message. */
13809 display_last_displayed_message_p = 1;
13810 redisplay_internal ();
13811 display_last_displayed_message_p = 0;
13812 }
13813 else
13814 redisplay_internal ();
13815
13816 if (FRAME_RIF (SELECTED_FRAME ()) != NULL
13817 && FRAME_RIF (SELECTED_FRAME ())->flush_display_optional)
13818 FRAME_RIF (SELECTED_FRAME ())->flush_display_optional (NULL);
13819 }
13820
13821
13822 /* Function registered with record_unwind_protect in redisplay_internal.
13823 Clear redisplaying_p. Also, select the previously
13824 selected frame, unless it has been deleted (by an X connection
13825 failure during redisplay, for example). */
13826
13827 static Lisp_Object
13828 unwind_redisplay (Lisp_Object old_frame)
13829 {
13830 redisplaying_p = 0;
13831 if (! EQ (old_frame, selected_frame)
13832 && FRAME_LIVE_P (XFRAME (old_frame)))
13833 select_frame_for_redisplay (old_frame);
13834 return Qnil;
13835 }
13836
13837
13838 /* Mark the display of window W as accurate or inaccurate. If
13839 ACCURATE_P is non-zero mark display of W as accurate. If
13840 ACCURATE_P is zero, arrange for W to be redisplayed the next time
13841 redisplay_internal is called. */
13842
13843 static void
13844 mark_window_display_accurate_1 (struct window *w, int accurate_p)
13845 {
13846 if (BUFFERP (w->buffer))
13847 {
13848 struct buffer *b = XBUFFER (w->buffer);
13849
13850 w->last_modified = accurate_p ? BUF_MODIFF(b) : 0;
13851 w->last_overlay_modified = accurate_p ? BUF_OVERLAY_MODIFF(b) : 0;
13852 w->last_had_star
13853 = BUF_MODIFF (b) > BUF_SAVE_MODIFF (b);
13854
13855 if (accurate_p)
13856 {
13857 b->clip_changed = 0;
13858 b->prevent_redisplay_optimizations_p = 0;
13859
13860 BUF_UNCHANGED_MODIFIED (b) = BUF_MODIFF (b);
13861 BUF_OVERLAY_UNCHANGED_MODIFIED (b) = BUF_OVERLAY_MODIFF (b);
13862 BUF_BEG_UNCHANGED (b) = BUF_GPT (b) - BUF_BEG (b);
13863 BUF_END_UNCHANGED (b) = BUF_Z (b) - BUF_GPT (b);
13864
13865 w->current_matrix->buffer = b;
13866 w->current_matrix->begv = BUF_BEGV (b);
13867 w->current_matrix->zv = BUF_ZV (b);
13868
13869 w->last_cursor = w->cursor;
13870 w->last_cursor_off_p = w->cursor_off_p;
13871
13872 if (w == XWINDOW (selected_window))
13873 w->last_point = BUF_PT (b);
13874 else
13875 w->last_point = XMARKER (w->pointm)->charpos;
13876 }
13877 }
13878
13879 if (accurate_p)
13880 {
13881 wset_window_end_valid (w, w->buffer);
13882 w->update_mode_line = 0;
13883 }
13884 }
13885
13886
13887 /* Mark the display of windows in the window tree rooted at WINDOW as
13888 accurate or inaccurate. If ACCURATE_P is non-zero mark display of
13889 windows as accurate. If ACCURATE_P is zero, arrange for windows to
13890 be redisplayed the next time redisplay_internal is called. */
13891
13892 void
13893 mark_window_display_accurate (Lisp_Object window, int accurate_p)
13894 {
13895 struct window *w;
13896
13897 for (; !NILP (window); window = w->next)
13898 {
13899 w = XWINDOW (window);
13900 mark_window_display_accurate_1 (w, accurate_p);
13901
13902 if (!NILP (w->vchild))
13903 mark_window_display_accurate (w->vchild, accurate_p);
13904 if (!NILP (w->hchild))
13905 mark_window_display_accurate (w->hchild, accurate_p);
13906 }
13907
13908 if (accurate_p)
13909 {
13910 update_overlay_arrows (1);
13911 }
13912 else
13913 {
13914 /* Force a thorough redisplay the next time by setting
13915 last_arrow_position and last_arrow_string to t, which is
13916 unequal to any useful value of Voverlay_arrow_... */
13917 update_overlay_arrows (-1);
13918 }
13919 }
13920
13921
13922 /* Return value in display table DP (Lisp_Char_Table *) for character
13923 C. Since a display table doesn't have any parent, we don't have to
13924 follow parent. Do not call this function directly but use the
13925 macro DISP_CHAR_VECTOR. */
13926
13927 Lisp_Object
13928 disp_char_vector (struct Lisp_Char_Table *dp, int c)
13929 {
13930 Lisp_Object val;
13931
13932 if (ASCII_CHAR_P (c))
13933 {
13934 val = dp->ascii;
13935 if (SUB_CHAR_TABLE_P (val))
13936 val = XSUB_CHAR_TABLE (val)->contents[c];
13937 }
13938 else
13939 {
13940 Lisp_Object table;
13941
13942 XSETCHAR_TABLE (table, dp);
13943 val = char_table_ref (table, c);
13944 }
13945 if (NILP (val))
13946 val = dp->defalt;
13947 return val;
13948 }
13949
13950
13951 \f
13952 /***********************************************************************
13953 Window Redisplay
13954 ***********************************************************************/
13955
13956 /* Redisplay all leaf windows in the window tree rooted at WINDOW. */
13957
13958 static void
13959 redisplay_windows (Lisp_Object window)
13960 {
13961 while (!NILP (window))
13962 {
13963 struct window *w = XWINDOW (window);
13964
13965 if (!NILP (w->hchild))
13966 redisplay_windows (w->hchild);
13967 else if (!NILP (w->vchild))
13968 redisplay_windows (w->vchild);
13969 else if (!NILP (w->buffer))
13970 {
13971 displayed_buffer = XBUFFER (w->buffer);
13972 /* Use list_of_error, not Qerror, so that
13973 we catch only errors and don't run the debugger. */
13974 internal_condition_case_1 (redisplay_window_0, window,
13975 list_of_error,
13976 redisplay_window_error);
13977 }
13978
13979 window = w->next;
13980 }
13981 }
13982
13983 static Lisp_Object
13984 redisplay_window_error (Lisp_Object ignore)
13985 {
13986 displayed_buffer->display_error_modiff = BUF_MODIFF (displayed_buffer);
13987 return Qnil;
13988 }
13989
13990 static Lisp_Object
13991 redisplay_window_0 (Lisp_Object window)
13992 {
13993 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
13994 redisplay_window (window, 0);
13995 return Qnil;
13996 }
13997
13998 static Lisp_Object
13999 redisplay_window_1 (Lisp_Object window)
14000 {
14001 if (displayed_buffer->display_error_modiff < BUF_MODIFF (displayed_buffer))
14002 redisplay_window (window, 1);
14003 return Qnil;
14004 }
14005 \f
14006
14007 /* Set cursor position of W. PT is assumed to be displayed in ROW.
14008 DELTA and DELTA_BYTES are the numbers of characters and bytes by
14009 which positions recorded in ROW differ from current buffer
14010 positions.
14011
14012 Return 0 if cursor is not on this row, 1 otherwise. */
14013
14014 static int
14015 set_cursor_from_row (struct window *w, struct glyph_row *row,
14016 struct glyph_matrix *matrix,
14017 ptrdiff_t delta, ptrdiff_t delta_bytes,
14018 int dy, int dvpos)
14019 {
14020 struct glyph *glyph = row->glyphs[TEXT_AREA];
14021 struct glyph *end = glyph + row->used[TEXT_AREA];
14022 struct glyph *cursor = NULL;
14023 /* The last known character position in row. */
14024 ptrdiff_t last_pos = MATRIX_ROW_START_CHARPOS (row) + delta;
14025 int x = row->x;
14026 ptrdiff_t pt_old = PT - delta;
14027 ptrdiff_t pos_before = MATRIX_ROW_START_CHARPOS (row) + delta;
14028 ptrdiff_t pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14029 struct glyph *glyph_before = glyph - 1, *glyph_after = end;
14030 /* A glyph beyond the edge of TEXT_AREA which we should never
14031 touch. */
14032 struct glyph *glyphs_end = end;
14033 /* Non-zero means we've found a match for cursor position, but that
14034 glyph has the avoid_cursor_p flag set. */
14035 int match_with_avoid_cursor = 0;
14036 /* Non-zero means we've seen at least one glyph that came from a
14037 display string. */
14038 int string_seen = 0;
14039 /* Largest and smallest buffer positions seen so far during scan of
14040 glyph row. */
14041 ptrdiff_t bpos_max = pos_before;
14042 ptrdiff_t bpos_min = pos_after;
14043 /* Last buffer position covered by an overlay string with an integer
14044 `cursor' property. */
14045 ptrdiff_t bpos_covered = 0;
14046 /* Non-zero means the display string on which to display the cursor
14047 comes from a text property, not from an overlay. */
14048 int string_from_text_prop = 0;
14049
14050 /* Don't even try doing anything if called for a mode-line or
14051 header-line row, since the rest of the code isn't prepared to
14052 deal with such calamities. */
14053 eassert (!row->mode_line_p);
14054 if (row->mode_line_p)
14055 return 0;
14056
14057 /* Skip over glyphs not having an object at the start and the end of
14058 the row. These are special glyphs like truncation marks on
14059 terminal frames. */
14060 if (row->displays_text_p)
14061 {
14062 if (!row->reversed_p)
14063 {
14064 while (glyph < end
14065 && INTEGERP (glyph->object)
14066 && glyph->charpos < 0)
14067 {
14068 x += glyph->pixel_width;
14069 ++glyph;
14070 }
14071 while (end > glyph
14072 && INTEGERP ((end - 1)->object)
14073 /* CHARPOS is zero for blanks and stretch glyphs
14074 inserted by extend_face_to_end_of_line. */
14075 && (end - 1)->charpos <= 0)
14076 --end;
14077 glyph_before = glyph - 1;
14078 glyph_after = end;
14079 }
14080 else
14081 {
14082 struct glyph *g;
14083
14084 /* If the glyph row is reversed, we need to process it from back
14085 to front, so swap the edge pointers. */
14086 glyphs_end = end = glyph - 1;
14087 glyph += row->used[TEXT_AREA] - 1;
14088
14089 while (glyph > end + 1
14090 && INTEGERP (glyph->object)
14091 && glyph->charpos < 0)
14092 {
14093 --glyph;
14094 x -= glyph->pixel_width;
14095 }
14096 if (INTEGERP (glyph->object) && glyph->charpos < 0)
14097 --glyph;
14098 /* By default, in reversed rows we put the cursor on the
14099 rightmost (first in the reading order) glyph. */
14100 for (g = end + 1; g < glyph; g++)
14101 x += g->pixel_width;
14102 while (end < glyph
14103 && INTEGERP ((end + 1)->object)
14104 && (end + 1)->charpos <= 0)
14105 ++end;
14106 glyph_before = glyph + 1;
14107 glyph_after = end;
14108 }
14109 }
14110 else if (row->reversed_p)
14111 {
14112 /* In R2L rows that don't display text, put the cursor on the
14113 rightmost glyph. Case in point: an empty last line that is
14114 part of an R2L paragraph. */
14115 cursor = end - 1;
14116 /* Avoid placing the cursor on the last glyph of the row, where
14117 on terminal frames we hold the vertical border between
14118 adjacent windows. */
14119 if (!FRAME_WINDOW_P (WINDOW_XFRAME (w))
14120 && !WINDOW_RIGHTMOST_P (w)
14121 && cursor == row->glyphs[LAST_AREA] - 1)
14122 cursor--;
14123 x = -1; /* will be computed below, at label compute_x */
14124 }
14125
14126 /* Step 1: Try to find the glyph whose character position
14127 corresponds to point. If that's not possible, find 2 glyphs
14128 whose character positions are the closest to point, one before
14129 point, the other after it. */
14130 if (!row->reversed_p)
14131 while (/* not marched to end of glyph row */
14132 glyph < end
14133 /* glyph was not inserted by redisplay for internal purposes */
14134 && !INTEGERP (glyph->object))
14135 {
14136 if (BUFFERP (glyph->object))
14137 {
14138 ptrdiff_t dpos = glyph->charpos - pt_old;
14139
14140 if (glyph->charpos > bpos_max)
14141 bpos_max = glyph->charpos;
14142 if (glyph->charpos < bpos_min)
14143 bpos_min = glyph->charpos;
14144 if (!glyph->avoid_cursor_p)
14145 {
14146 /* If we hit point, we've found the glyph on which to
14147 display the cursor. */
14148 if (dpos == 0)
14149 {
14150 match_with_avoid_cursor = 0;
14151 break;
14152 }
14153 /* See if we've found a better approximation to
14154 POS_BEFORE or to POS_AFTER. */
14155 if (0 > dpos && dpos > pos_before - pt_old)
14156 {
14157 pos_before = glyph->charpos;
14158 glyph_before = glyph;
14159 }
14160 else if (0 < dpos && dpos < pos_after - pt_old)
14161 {
14162 pos_after = glyph->charpos;
14163 glyph_after = glyph;
14164 }
14165 }
14166 else if (dpos == 0)
14167 match_with_avoid_cursor = 1;
14168 }
14169 else if (STRINGP (glyph->object))
14170 {
14171 Lisp_Object chprop;
14172 ptrdiff_t glyph_pos = glyph->charpos;
14173
14174 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14175 glyph->object);
14176 if (!NILP (chprop))
14177 {
14178 /* If the string came from a `display' text property,
14179 look up the buffer position of that property and
14180 use that position to update bpos_max, as if we
14181 actually saw such a position in one of the row's
14182 glyphs. This helps with supporting integer values
14183 of `cursor' property on the display string in
14184 situations where most or all of the row's buffer
14185 text is completely covered by display properties,
14186 so that no glyph with valid buffer positions is
14187 ever seen in the row. */
14188 ptrdiff_t prop_pos =
14189 string_buffer_position_lim (glyph->object, pos_before,
14190 pos_after, 0);
14191
14192 if (prop_pos >= pos_before)
14193 bpos_max = prop_pos - 1;
14194 }
14195 if (INTEGERP (chprop))
14196 {
14197 bpos_covered = bpos_max + XINT (chprop);
14198 /* If the `cursor' property covers buffer positions up
14199 to and including point, we should display cursor on
14200 this glyph. Note that, if a `cursor' property on one
14201 of the string's characters has an integer value, we
14202 will break out of the loop below _before_ we get to
14203 the position match above. IOW, integer values of
14204 the `cursor' property override the "exact match for
14205 point" strategy of positioning the cursor. */
14206 /* Implementation note: bpos_max == pt_old when, e.g.,
14207 we are in an empty line, where bpos_max is set to
14208 MATRIX_ROW_START_CHARPOS, see above. */
14209 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14210 {
14211 cursor = glyph;
14212 break;
14213 }
14214 }
14215
14216 string_seen = 1;
14217 }
14218 x += glyph->pixel_width;
14219 ++glyph;
14220 }
14221 else if (glyph > end) /* row is reversed */
14222 while (!INTEGERP (glyph->object))
14223 {
14224 if (BUFFERP (glyph->object))
14225 {
14226 ptrdiff_t dpos = glyph->charpos - pt_old;
14227
14228 if (glyph->charpos > bpos_max)
14229 bpos_max = glyph->charpos;
14230 if (glyph->charpos < bpos_min)
14231 bpos_min = glyph->charpos;
14232 if (!glyph->avoid_cursor_p)
14233 {
14234 if (dpos == 0)
14235 {
14236 match_with_avoid_cursor = 0;
14237 break;
14238 }
14239 if (0 > dpos && dpos > pos_before - pt_old)
14240 {
14241 pos_before = glyph->charpos;
14242 glyph_before = glyph;
14243 }
14244 else if (0 < dpos && dpos < pos_after - pt_old)
14245 {
14246 pos_after = glyph->charpos;
14247 glyph_after = glyph;
14248 }
14249 }
14250 else if (dpos == 0)
14251 match_with_avoid_cursor = 1;
14252 }
14253 else if (STRINGP (glyph->object))
14254 {
14255 Lisp_Object chprop;
14256 ptrdiff_t glyph_pos = glyph->charpos;
14257
14258 chprop = Fget_char_property (make_number (glyph_pos), Qcursor,
14259 glyph->object);
14260 if (!NILP (chprop))
14261 {
14262 ptrdiff_t prop_pos =
14263 string_buffer_position_lim (glyph->object, pos_before,
14264 pos_after, 0);
14265
14266 if (prop_pos >= pos_before)
14267 bpos_max = prop_pos - 1;
14268 }
14269 if (INTEGERP (chprop))
14270 {
14271 bpos_covered = bpos_max + XINT (chprop);
14272 /* If the `cursor' property covers buffer positions up
14273 to and including point, we should display cursor on
14274 this glyph. */
14275 if (bpos_max <= pt_old && bpos_covered >= pt_old)
14276 {
14277 cursor = glyph;
14278 break;
14279 }
14280 }
14281 string_seen = 1;
14282 }
14283 --glyph;
14284 if (glyph == glyphs_end) /* don't dereference outside TEXT_AREA */
14285 {
14286 x--; /* can't use any pixel_width */
14287 break;
14288 }
14289 x -= glyph->pixel_width;
14290 }
14291
14292 /* Step 2: If we didn't find an exact match for point, we need to
14293 look for a proper place to put the cursor among glyphs between
14294 GLYPH_BEFORE and GLYPH_AFTER. */
14295 if (!((row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14296 && BUFFERP (glyph->object) && glyph->charpos == pt_old)
14297 && !(bpos_max < pt_old && pt_old <= bpos_covered))
14298 {
14299 /* An empty line has a single glyph whose OBJECT is zero and
14300 whose CHARPOS is the position of a newline on that line.
14301 Note that on a TTY, there are more glyphs after that, which
14302 were produced by extend_face_to_end_of_line, but their
14303 CHARPOS is zero or negative. */
14304 int empty_line_p =
14305 (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
14306 && INTEGERP (glyph->object) && glyph->charpos > 0;
14307
14308 if (row->ends_in_ellipsis_p && pos_after == last_pos)
14309 {
14310 ptrdiff_t ellipsis_pos;
14311
14312 /* Scan back over the ellipsis glyphs. */
14313 if (!row->reversed_p)
14314 {
14315 ellipsis_pos = (glyph - 1)->charpos;
14316 while (glyph > row->glyphs[TEXT_AREA]
14317 && (glyph - 1)->charpos == ellipsis_pos)
14318 glyph--, x -= glyph->pixel_width;
14319 /* That loop always goes one position too far, including
14320 the glyph before the ellipsis. So scan forward over
14321 that one. */
14322 x += glyph->pixel_width;
14323 glyph++;
14324 }
14325 else /* row is reversed */
14326 {
14327 ellipsis_pos = (glyph + 1)->charpos;
14328 while (glyph < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14329 && (glyph + 1)->charpos == ellipsis_pos)
14330 glyph++, x += glyph->pixel_width;
14331 x -= glyph->pixel_width;
14332 glyph--;
14333 }
14334 }
14335 else if (match_with_avoid_cursor)
14336 {
14337 cursor = glyph_after;
14338 x = -1;
14339 }
14340 else if (string_seen)
14341 {
14342 int incr = row->reversed_p ? -1 : +1;
14343
14344 /* Need to find the glyph that came out of a string which is
14345 present at point. That glyph is somewhere between
14346 GLYPH_BEFORE and GLYPH_AFTER, and it came from a string
14347 positioned between POS_BEFORE and POS_AFTER in the
14348 buffer. */
14349 struct glyph *start, *stop;
14350 ptrdiff_t pos = pos_before;
14351
14352 x = -1;
14353
14354 /* If the row ends in a newline from a display string,
14355 reordering could have moved the glyphs belonging to the
14356 string out of the [GLYPH_BEFORE..GLYPH_AFTER] range. So
14357 in this case we extend the search to the last glyph in
14358 the row that was not inserted by redisplay. */
14359 if (row->ends_in_newline_from_string_p)
14360 {
14361 glyph_after = end;
14362 pos_after = MATRIX_ROW_END_CHARPOS (row) + delta;
14363 }
14364
14365 /* GLYPH_BEFORE and GLYPH_AFTER are the glyphs that
14366 correspond to POS_BEFORE and POS_AFTER, respectively. We
14367 need START and STOP in the order that corresponds to the
14368 row's direction as given by its reversed_p flag. If the
14369 directionality of characters between POS_BEFORE and
14370 POS_AFTER is the opposite of the row's base direction,
14371 these characters will have been reordered for display,
14372 and we need to reverse START and STOP. */
14373 if (!row->reversed_p)
14374 {
14375 start = min (glyph_before, glyph_after);
14376 stop = max (glyph_before, glyph_after);
14377 }
14378 else
14379 {
14380 start = max (glyph_before, glyph_after);
14381 stop = min (glyph_before, glyph_after);
14382 }
14383 for (glyph = start + incr;
14384 row->reversed_p ? glyph > stop : glyph < stop; )
14385 {
14386
14387 /* Any glyphs that come from the buffer are here because
14388 of bidi reordering. Skip them, and only pay
14389 attention to glyphs that came from some string. */
14390 if (STRINGP (glyph->object))
14391 {
14392 Lisp_Object str;
14393 ptrdiff_t tem;
14394 /* If the display property covers the newline, we
14395 need to search for it one position farther. */
14396 ptrdiff_t lim = pos_after
14397 + (pos_after == MATRIX_ROW_END_CHARPOS (row) + delta);
14398
14399 string_from_text_prop = 0;
14400 str = glyph->object;
14401 tem = string_buffer_position_lim (str, pos, lim, 0);
14402 if (tem == 0 /* from overlay */
14403 || pos <= tem)
14404 {
14405 /* If the string from which this glyph came is
14406 found in the buffer at point, or at position
14407 that is closer to point than pos_after, then
14408 we've found the glyph we've been looking for.
14409 If it comes from an overlay (tem == 0), and
14410 it has the `cursor' property on one of its
14411 glyphs, record that glyph as a candidate for
14412 displaying the cursor. (As in the
14413 unidirectional version, we will display the
14414 cursor on the last candidate we find.) */
14415 if (tem == 0
14416 || tem == pt_old
14417 || (tem - pt_old > 0 && tem < pos_after))
14418 {
14419 /* The glyphs from this string could have
14420 been reordered. Find the one with the
14421 smallest string position. Or there could
14422 be a character in the string with the
14423 `cursor' property, which means display
14424 cursor on that character's glyph. */
14425 ptrdiff_t strpos = glyph->charpos;
14426
14427 if (tem)
14428 {
14429 cursor = glyph;
14430 string_from_text_prop = 1;
14431 }
14432 for ( ;
14433 (row->reversed_p ? glyph > stop : glyph < stop)
14434 && EQ (glyph->object, str);
14435 glyph += incr)
14436 {
14437 Lisp_Object cprop;
14438 ptrdiff_t gpos = glyph->charpos;
14439
14440 cprop = Fget_char_property (make_number (gpos),
14441 Qcursor,
14442 glyph->object);
14443 if (!NILP (cprop))
14444 {
14445 cursor = glyph;
14446 break;
14447 }
14448 if (tem && glyph->charpos < strpos)
14449 {
14450 strpos = glyph->charpos;
14451 cursor = glyph;
14452 }
14453 }
14454
14455 if (tem == pt_old
14456 || (tem - pt_old > 0 && tem < pos_after))
14457 goto compute_x;
14458 }
14459 if (tem)
14460 pos = tem + 1; /* don't find previous instances */
14461 }
14462 /* This string is not what we want; skip all of the
14463 glyphs that came from it. */
14464 while ((row->reversed_p ? glyph > stop : glyph < stop)
14465 && EQ (glyph->object, str))
14466 glyph += incr;
14467 }
14468 else
14469 glyph += incr;
14470 }
14471
14472 /* If we reached the end of the line, and END was from a string,
14473 the cursor is not on this line. */
14474 if (cursor == NULL
14475 && (row->reversed_p ? glyph <= end : glyph >= end)
14476 && (row->reversed_p ? end > glyphs_end : end < glyphs_end)
14477 && STRINGP (end->object)
14478 && row->continued_p)
14479 return 0;
14480 }
14481 /* A truncated row may not include PT among its character positions.
14482 Setting the cursor inside the scroll margin will trigger
14483 recalculation of hscroll in hscroll_window_tree. But if a
14484 display string covers point, defer to the string-handling
14485 code below to figure this out. */
14486 else if (row->truncated_on_left_p && pt_old < bpos_min)
14487 {
14488 cursor = glyph_before;
14489 x = -1;
14490 }
14491 else if ((row->truncated_on_right_p && pt_old > bpos_max)
14492 /* Zero-width characters produce no glyphs. */
14493 || (!empty_line_p
14494 && (row->reversed_p
14495 ? glyph_after > glyphs_end
14496 : glyph_after < glyphs_end)))
14497 {
14498 cursor = glyph_after;
14499 x = -1;
14500 }
14501 }
14502
14503 compute_x:
14504 if (cursor != NULL)
14505 glyph = cursor;
14506 else if (glyph == glyphs_end
14507 && pos_before == pos_after
14508 && STRINGP ((row->reversed_p
14509 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14510 : row->glyphs[TEXT_AREA])->object))
14511 {
14512 /* If all the glyphs of this row came from strings, put the
14513 cursor on the first glyph of the row. This avoids having the
14514 cursor outside of the text area in this very rare and hard
14515 use case. */
14516 glyph =
14517 row->reversed_p
14518 ? row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1
14519 : row->glyphs[TEXT_AREA];
14520 }
14521 if (x < 0)
14522 {
14523 struct glyph *g;
14524
14525 /* Need to compute x that corresponds to GLYPH. */
14526 for (g = row->glyphs[TEXT_AREA], x = row->x; g < glyph; g++)
14527 {
14528 if (g >= row->glyphs[TEXT_AREA] + row->used[TEXT_AREA])
14529 emacs_abort ();
14530 x += g->pixel_width;
14531 }
14532 }
14533
14534 /* ROW could be part of a continued line, which, under bidi
14535 reordering, might have other rows whose start and end charpos
14536 occlude point. Only set w->cursor if we found a better
14537 approximation to the cursor position than we have from previously
14538 examined candidate rows belonging to the same continued line. */
14539 if (/* we already have a candidate row */
14540 w->cursor.vpos >= 0
14541 /* that candidate is not the row we are processing */
14542 && MATRIX_ROW (matrix, w->cursor.vpos) != row
14543 /* Make sure cursor.vpos specifies a row whose start and end
14544 charpos occlude point, and it is valid candidate for being a
14545 cursor-row. This is because some callers of this function
14546 leave cursor.vpos at the row where the cursor was displayed
14547 during the last redisplay cycle. */
14548 && MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos)) <= pt_old
14549 && pt_old <= MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14550 && cursor_row_p (MATRIX_ROW (matrix, w->cursor.vpos)))
14551 {
14552 struct glyph *g1 =
14553 MATRIX_ROW_GLYPH_START (matrix, w->cursor.vpos) + w->cursor.hpos;
14554
14555 /* Don't consider glyphs that are outside TEXT_AREA. */
14556 if (!(row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end))
14557 return 0;
14558 /* Keep the candidate whose buffer position is the closest to
14559 point or has the `cursor' property. */
14560 if (/* previous candidate is a glyph in TEXT_AREA of that row */
14561 w->cursor.hpos >= 0
14562 && w->cursor.hpos < MATRIX_ROW_USED (matrix, w->cursor.vpos)
14563 && ((BUFFERP (g1->object)
14564 && (g1->charpos == pt_old /* an exact match always wins */
14565 || (BUFFERP (glyph->object)
14566 && eabs (g1->charpos - pt_old)
14567 < eabs (glyph->charpos - pt_old))))
14568 /* previous candidate is a glyph from a string that has
14569 a non-nil `cursor' property */
14570 || (STRINGP (g1->object)
14571 && (!NILP (Fget_char_property (make_number (g1->charpos),
14572 Qcursor, g1->object))
14573 /* previous candidate is from the same display
14574 string as this one, and the display string
14575 came from a text property */
14576 || (EQ (g1->object, glyph->object)
14577 && string_from_text_prop)
14578 /* this candidate is from newline and its
14579 position is not an exact match */
14580 || (INTEGERP (glyph->object)
14581 && glyph->charpos != pt_old)))))
14582 return 0;
14583 /* If this candidate gives an exact match, use that. */
14584 if (!((BUFFERP (glyph->object) && glyph->charpos == pt_old)
14585 /* If this candidate is a glyph created for the
14586 terminating newline of a line, and point is on that
14587 newline, it wins because it's an exact match. */
14588 || (!row->continued_p
14589 && INTEGERP (glyph->object)
14590 && glyph->charpos == 0
14591 && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
14592 /* Otherwise, keep the candidate that comes from a row
14593 spanning less buffer positions. This may win when one or
14594 both candidate positions are on glyphs that came from
14595 display strings, for which we cannot compare buffer
14596 positions. */
14597 && MATRIX_ROW_END_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14598 - MATRIX_ROW_START_CHARPOS (MATRIX_ROW (matrix, w->cursor.vpos))
14599 < MATRIX_ROW_END_CHARPOS (row) - MATRIX_ROW_START_CHARPOS (row))
14600 return 0;
14601 }
14602 w->cursor.hpos = glyph - row->glyphs[TEXT_AREA];
14603 w->cursor.x = x;
14604 w->cursor.vpos = MATRIX_ROW_VPOS (row, matrix) + dvpos;
14605 w->cursor.y = row->y + dy;
14606
14607 if (w == XWINDOW (selected_window))
14608 {
14609 if (!row->continued_p
14610 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
14611 && row->x == 0)
14612 {
14613 this_line_buffer = XBUFFER (w->buffer);
14614
14615 CHARPOS (this_line_start_pos)
14616 = MATRIX_ROW_START_CHARPOS (row) + delta;
14617 BYTEPOS (this_line_start_pos)
14618 = MATRIX_ROW_START_BYTEPOS (row) + delta_bytes;
14619
14620 CHARPOS (this_line_end_pos)
14621 = Z - (MATRIX_ROW_END_CHARPOS (row) + delta);
14622 BYTEPOS (this_line_end_pos)
14623 = Z_BYTE - (MATRIX_ROW_END_BYTEPOS (row) + delta_bytes);
14624
14625 this_line_y = w->cursor.y;
14626 this_line_pixel_height = row->height;
14627 this_line_vpos = w->cursor.vpos;
14628 this_line_start_x = row->x;
14629 }
14630 else
14631 CHARPOS (this_line_start_pos) = 0;
14632 }
14633
14634 return 1;
14635 }
14636
14637
14638 /* Run window scroll functions, if any, for WINDOW with new window
14639 start STARTP. Sets the window start of WINDOW to that position.
14640
14641 We assume that the window's buffer is really current. */
14642
14643 static struct text_pos
14644 run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
14645 {
14646 struct window *w = XWINDOW (window);
14647 SET_MARKER_FROM_TEXT_POS (w->start, startp);
14648
14649 if (current_buffer != XBUFFER (w->buffer))
14650 emacs_abort ();
14651
14652 if (!NILP (Vwindow_scroll_functions))
14653 {
14654 run_hook_with_args_2 (Qwindow_scroll_functions, window,
14655 make_number (CHARPOS (startp)));
14656 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14657 /* In case the hook functions switch buffers. */
14658 set_buffer_internal (XBUFFER (w->buffer));
14659 }
14660
14661 return startp;
14662 }
14663
14664
14665 /* Make sure the line containing the cursor is fully visible.
14666 A value of 1 means there is nothing to be done.
14667 (Either the line is fully visible, or it cannot be made so,
14668 or we cannot tell.)
14669
14670 If FORCE_P is non-zero, return 0 even if partial visible cursor row
14671 is higher than window.
14672
14673 A value of 0 means the caller should do scrolling
14674 as if point had gone off the screen. */
14675
14676 static int
14677 cursor_row_fully_visible_p (struct window *w, int force_p, int current_matrix_p)
14678 {
14679 struct glyph_matrix *matrix;
14680 struct glyph_row *row;
14681 int window_height;
14682
14683 if (!make_cursor_line_fully_visible_p)
14684 return 1;
14685
14686 /* It's not always possible to find the cursor, e.g, when a window
14687 is full of overlay strings. Don't do anything in that case. */
14688 if (w->cursor.vpos < 0)
14689 return 1;
14690
14691 matrix = current_matrix_p ? w->current_matrix : w->desired_matrix;
14692 row = MATRIX_ROW (matrix, w->cursor.vpos);
14693
14694 /* If the cursor row is not partially visible, there's nothing to do. */
14695 if (!MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row))
14696 return 1;
14697
14698 /* If the row the cursor is in is taller than the window's height,
14699 it's not clear what to do, so do nothing. */
14700 window_height = window_box_height (w);
14701 if (row->height >= window_height)
14702 {
14703 if (!force_p || MINI_WINDOW_P (w)
14704 || w->vscroll || w->cursor.vpos == 0)
14705 return 1;
14706 }
14707 return 0;
14708 }
14709
14710
14711 /* Try scrolling PT into view in window WINDOW. JUST_THIS_ONE_P
14712 non-zero means only WINDOW is redisplayed in redisplay_internal.
14713 TEMP_SCROLL_STEP has the same meaning as emacs_scroll_step, and is used
14714 in redisplay_window to bring a partially visible line into view in
14715 the case that only the cursor has moved.
14716
14717 LAST_LINE_MISFIT should be nonzero if we're scrolling because the
14718 last screen line's vertical height extends past the end of the screen.
14719
14720 Value is
14721
14722 1 if scrolling succeeded
14723
14724 0 if scrolling didn't find point.
14725
14726 -1 if new fonts have been loaded so that we must interrupt
14727 redisplay, adjust glyph matrices, and try again. */
14728
14729 enum
14730 {
14731 SCROLLING_SUCCESS,
14732 SCROLLING_FAILED,
14733 SCROLLING_NEED_LARGER_MATRICES
14734 };
14735
14736 /* If scroll-conservatively is more than this, never recenter.
14737
14738 If you change this, don't forget to update the doc string of
14739 `scroll-conservatively' and the Emacs manual. */
14740 #define SCROLL_LIMIT 100
14741
14742 static int
14743 try_scrolling (Lisp_Object window, int just_this_one_p,
14744 ptrdiff_t arg_scroll_conservatively, ptrdiff_t scroll_step,
14745 int temp_scroll_step, int last_line_misfit)
14746 {
14747 struct window *w = XWINDOW (window);
14748 struct frame *f = XFRAME (w->frame);
14749 struct text_pos pos, startp;
14750 struct it it;
14751 int this_scroll_margin, scroll_max, rc, height;
14752 int dy = 0, amount_to_scroll = 0, scroll_down_p = 0;
14753 int extra_scroll_margin_lines = last_line_misfit ? 1 : 0;
14754 Lisp_Object aggressive;
14755 /* We will never try scrolling more than this number of lines. */
14756 int scroll_limit = SCROLL_LIMIT;
14757
14758 #ifdef GLYPH_DEBUG
14759 debug_method_add (w, "try_scrolling");
14760 #endif
14761
14762 SET_TEXT_POS_FROM_MARKER (startp, w->start);
14763
14764 /* Compute scroll margin height in pixels. We scroll when point is
14765 within this distance from the top or bottom of the window. */
14766 if (scroll_margin > 0)
14767 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
14768 * FRAME_LINE_HEIGHT (f);
14769 else
14770 this_scroll_margin = 0;
14771
14772 /* Force arg_scroll_conservatively to have a reasonable value, to
14773 avoid scrolling too far away with slow move_it_* functions. Note
14774 that the user can supply scroll-conservatively equal to
14775 `most-positive-fixnum', which can be larger than INT_MAX. */
14776 if (arg_scroll_conservatively > scroll_limit)
14777 {
14778 arg_scroll_conservatively = scroll_limit + 1;
14779 scroll_max = scroll_limit * FRAME_LINE_HEIGHT (f);
14780 }
14781 else if (scroll_step || arg_scroll_conservatively || temp_scroll_step)
14782 /* Compute how much we should try to scroll maximally to bring
14783 point into view. */
14784 scroll_max = (max (scroll_step,
14785 max (arg_scroll_conservatively, temp_scroll_step))
14786 * FRAME_LINE_HEIGHT (f));
14787 else if (NUMBERP (BVAR (current_buffer, scroll_down_aggressively))
14788 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively)))
14789 /* We're trying to scroll because of aggressive scrolling but no
14790 scroll_step is set. Choose an arbitrary one. */
14791 scroll_max = 10 * FRAME_LINE_HEIGHT (f);
14792 else
14793 scroll_max = 0;
14794
14795 too_near_end:
14796
14797 /* Decide whether to scroll down. */
14798 if (PT > CHARPOS (startp))
14799 {
14800 int scroll_margin_y;
14801
14802 /* Compute the pixel ypos of the scroll margin, then move IT to
14803 either that ypos or PT, whichever comes first. */
14804 start_display (&it, w, startp);
14805 scroll_margin_y = it.last_visible_y - this_scroll_margin
14806 - FRAME_LINE_HEIGHT (f) * extra_scroll_margin_lines;
14807 move_it_to (&it, PT, -1, scroll_margin_y - 1, -1,
14808 (MOVE_TO_POS | MOVE_TO_Y));
14809
14810 if (PT > CHARPOS (it.current.pos))
14811 {
14812 int y0 = line_bottom_y (&it);
14813 /* Compute how many pixels below window bottom to stop searching
14814 for PT. This avoids costly search for PT that is far away if
14815 the user limited scrolling by a small number of lines, but
14816 always finds PT if scroll_conservatively is set to a large
14817 number, such as most-positive-fixnum. */
14818 int slack = max (scroll_max, 10 * FRAME_LINE_HEIGHT (f));
14819 int y_to_move = it.last_visible_y + slack;
14820
14821 /* Compute the distance from the scroll margin to PT or to
14822 the scroll limit, whichever comes first. This should
14823 include the height of the cursor line, to make that line
14824 fully visible. */
14825 move_it_to (&it, PT, -1, y_to_move,
14826 -1, MOVE_TO_POS | MOVE_TO_Y);
14827 dy = line_bottom_y (&it) - y0;
14828
14829 if (dy > scroll_max)
14830 return SCROLLING_FAILED;
14831
14832 if (dy > 0)
14833 scroll_down_p = 1;
14834 }
14835 }
14836
14837 if (scroll_down_p)
14838 {
14839 /* Point is in or below the bottom scroll margin, so move the
14840 window start down. If scrolling conservatively, move it just
14841 enough down to make point visible. If scroll_step is set,
14842 move it down by scroll_step. */
14843 if (arg_scroll_conservatively)
14844 amount_to_scroll
14845 = min (max (dy, FRAME_LINE_HEIGHT (f)),
14846 FRAME_LINE_HEIGHT (f) * arg_scroll_conservatively);
14847 else if (scroll_step || temp_scroll_step)
14848 amount_to_scroll = scroll_max;
14849 else
14850 {
14851 aggressive = BVAR (current_buffer, scroll_up_aggressively);
14852 height = WINDOW_BOX_TEXT_HEIGHT (w);
14853 if (NUMBERP (aggressive))
14854 {
14855 double float_amount = XFLOATINT (aggressive) * height;
14856 int aggressive_scroll = float_amount;
14857 if (aggressive_scroll == 0 && float_amount > 0)
14858 aggressive_scroll = 1;
14859 /* Don't let point enter the scroll margin near top of
14860 the window. This could happen if the value of
14861 scroll_up_aggressively is too large and there are
14862 non-zero margins, because scroll_up_aggressively
14863 means put point that fraction of window height
14864 _from_the_bottom_margin_. */
14865 if (aggressive_scroll + 2*this_scroll_margin > height)
14866 aggressive_scroll = height - 2*this_scroll_margin;
14867 amount_to_scroll = dy + aggressive_scroll;
14868 }
14869 }
14870
14871 if (amount_to_scroll <= 0)
14872 return SCROLLING_FAILED;
14873
14874 start_display (&it, w, startp);
14875 if (arg_scroll_conservatively <= scroll_limit)
14876 move_it_vertically (&it, amount_to_scroll);
14877 else
14878 {
14879 /* Extra precision for users who set scroll-conservatively
14880 to a large number: make sure the amount we scroll
14881 the window start is never less than amount_to_scroll,
14882 which was computed as distance from window bottom to
14883 point. This matters when lines at window top and lines
14884 below window bottom have different height. */
14885 struct it it1;
14886 void *it1data = NULL;
14887 /* We use a temporary it1 because line_bottom_y can modify
14888 its argument, if it moves one line down; see there. */
14889 int start_y;
14890
14891 SAVE_IT (it1, it, it1data);
14892 start_y = line_bottom_y (&it1);
14893 do {
14894 RESTORE_IT (&it, &it, it1data);
14895 move_it_by_lines (&it, 1);
14896 SAVE_IT (it1, it, it1data);
14897 } while (line_bottom_y (&it1) - start_y < amount_to_scroll);
14898 }
14899
14900 /* If STARTP is unchanged, move it down another screen line. */
14901 if (CHARPOS (it.current.pos) == CHARPOS (startp))
14902 move_it_by_lines (&it, 1);
14903 startp = it.current.pos;
14904 }
14905 else
14906 {
14907 struct text_pos scroll_margin_pos = startp;
14908
14909 /* See if point is inside the scroll margin at the top of the
14910 window. */
14911 if (this_scroll_margin)
14912 {
14913 start_display (&it, w, startp);
14914 move_it_vertically (&it, this_scroll_margin);
14915 scroll_margin_pos = it.current.pos;
14916 }
14917
14918 if (PT < CHARPOS (scroll_margin_pos))
14919 {
14920 /* Point is in the scroll margin at the top of the window or
14921 above what is displayed in the window. */
14922 int y0, y_to_move;
14923
14924 /* Compute the vertical distance from PT to the scroll
14925 margin position. Move as far as scroll_max allows, or
14926 one screenful, or 10 screen lines, whichever is largest.
14927 Give up if distance is greater than scroll_max or if we
14928 didn't reach the scroll margin position. */
14929 SET_TEXT_POS (pos, PT, PT_BYTE);
14930 start_display (&it, w, pos);
14931 y0 = it.current_y;
14932 y_to_move = max (it.last_visible_y,
14933 max (scroll_max, 10 * FRAME_LINE_HEIGHT (f)));
14934 move_it_to (&it, CHARPOS (scroll_margin_pos), 0,
14935 y_to_move, -1,
14936 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
14937 dy = it.current_y - y0;
14938 if (dy > scroll_max
14939 || IT_CHARPOS (it) < CHARPOS (scroll_margin_pos))
14940 return SCROLLING_FAILED;
14941
14942 /* Compute new window start. */
14943 start_display (&it, w, startp);
14944
14945 if (arg_scroll_conservatively)
14946 amount_to_scroll = max (dy, FRAME_LINE_HEIGHT (f) *
14947 max (scroll_step, temp_scroll_step));
14948 else if (scroll_step || temp_scroll_step)
14949 amount_to_scroll = scroll_max;
14950 else
14951 {
14952 aggressive = BVAR (current_buffer, scroll_down_aggressively);
14953 height = WINDOW_BOX_TEXT_HEIGHT (w);
14954 if (NUMBERP (aggressive))
14955 {
14956 double float_amount = XFLOATINT (aggressive) * height;
14957 int aggressive_scroll = float_amount;
14958 if (aggressive_scroll == 0 && float_amount > 0)
14959 aggressive_scroll = 1;
14960 /* Don't let point enter the scroll margin near
14961 bottom of the window, if the value of
14962 scroll_down_aggressively happens to be too
14963 large. */
14964 if (aggressive_scroll + 2*this_scroll_margin > height)
14965 aggressive_scroll = height - 2*this_scroll_margin;
14966 amount_to_scroll = dy + aggressive_scroll;
14967 }
14968 }
14969
14970 if (amount_to_scroll <= 0)
14971 return SCROLLING_FAILED;
14972
14973 move_it_vertically_backward (&it, amount_to_scroll);
14974 startp = it.current.pos;
14975 }
14976 }
14977
14978 /* Run window scroll functions. */
14979 startp = run_window_scroll_functions (window, startp);
14980
14981 /* Display the window. Give up if new fonts are loaded, or if point
14982 doesn't appear. */
14983 if (!try_window (window, startp, 0))
14984 rc = SCROLLING_NEED_LARGER_MATRICES;
14985 else if (w->cursor.vpos < 0)
14986 {
14987 clear_glyph_matrix (w->desired_matrix);
14988 rc = SCROLLING_FAILED;
14989 }
14990 else
14991 {
14992 /* Maybe forget recorded base line for line number display. */
14993 if (!just_this_one_p
14994 || current_buffer->clip_changed
14995 || BEG_UNCHANGED < CHARPOS (startp))
14996 wset_base_line_number (w, Qnil);
14997
14998 /* If cursor ends up on a partially visible line,
14999 treat that as being off the bottom of the screen. */
15000 if (! cursor_row_fully_visible_p (w, extra_scroll_margin_lines <= 1, 0)
15001 /* It's possible that the cursor is on the first line of the
15002 buffer, which is partially obscured due to a vscroll
15003 (Bug#7537). In that case, avoid looping forever . */
15004 && extra_scroll_margin_lines < w->desired_matrix->nrows - 1)
15005 {
15006 clear_glyph_matrix (w->desired_matrix);
15007 ++extra_scroll_margin_lines;
15008 goto too_near_end;
15009 }
15010 rc = SCROLLING_SUCCESS;
15011 }
15012
15013 return rc;
15014 }
15015
15016
15017 /* Compute a suitable window start for window W if display of W starts
15018 on a continuation line. Value is non-zero if a new window start
15019 was computed.
15020
15021 The new window start will be computed, based on W's width, starting
15022 from the start of the continued line. It is the start of the
15023 screen line with the minimum distance from the old start W->start. */
15024
15025 static int
15026 compute_window_start_on_continuation_line (struct window *w)
15027 {
15028 struct text_pos pos, start_pos;
15029 int window_start_changed_p = 0;
15030
15031 SET_TEXT_POS_FROM_MARKER (start_pos, w->start);
15032
15033 /* If window start is on a continuation line... Window start may be
15034 < BEGV in case there's invisible text at the start of the
15035 buffer (M-x rmail, for example). */
15036 if (CHARPOS (start_pos) > BEGV
15037 && FETCH_BYTE (BYTEPOS (start_pos) - 1) != '\n')
15038 {
15039 struct it it;
15040 struct glyph_row *row;
15041
15042 /* Handle the case that the window start is out of range. */
15043 if (CHARPOS (start_pos) < BEGV)
15044 SET_TEXT_POS (start_pos, BEGV, BEGV_BYTE);
15045 else if (CHARPOS (start_pos) > ZV)
15046 SET_TEXT_POS (start_pos, ZV, ZV_BYTE);
15047
15048 /* Find the start of the continued line. This should be fast
15049 because scan_buffer is fast (newline cache). */
15050 row = w->desired_matrix->rows + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0);
15051 init_iterator (&it, w, CHARPOS (start_pos), BYTEPOS (start_pos),
15052 row, DEFAULT_FACE_ID);
15053 reseat_at_previous_visible_line_start (&it);
15054
15055 /* If the line start is "too far" away from the window start,
15056 say it takes too much time to compute a new window start. */
15057 if (CHARPOS (start_pos) - IT_CHARPOS (it)
15058 < WINDOW_TOTAL_LINES (w) * WINDOW_TOTAL_COLS (w))
15059 {
15060 int min_distance, distance;
15061
15062 /* Move forward by display lines to find the new window
15063 start. If window width was enlarged, the new start can
15064 be expected to be > the old start. If window width was
15065 decreased, the new window start will be < the old start.
15066 So, we're looking for the display line start with the
15067 minimum distance from the old window start. */
15068 pos = it.current.pos;
15069 min_distance = INFINITY;
15070 while ((distance = eabs (CHARPOS (start_pos) - IT_CHARPOS (it))),
15071 distance < min_distance)
15072 {
15073 min_distance = distance;
15074 pos = it.current.pos;
15075 move_it_by_lines (&it, 1);
15076 }
15077
15078 /* Set the window start there. */
15079 SET_MARKER_FROM_TEXT_POS (w->start, pos);
15080 window_start_changed_p = 1;
15081 }
15082 }
15083
15084 return window_start_changed_p;
15085 }
15086
15087
15088 /* Try cursor movement in case text has not changed in window WINDOW,
15089 with window start STARTP. Value is
15090
15091 CURSOR_MOVEMENT_SUCCESS if successful
15092
15093 CURSOR_MOVEMENT_CANNOT_BE_USED if this method cannot be used
15094
15095 CURSOR_MOVEMENT_MUST_SCROLL if we know we have to scroll the
15096 display. *SCROLL_STEP is set to 1, under certain circumstances, if
15097 we want to scroll as if scroll-step were set to 1. See the code.
15098
15099 CURSOR_MOVEMENT_NEED_LARGER_MATRICES if we need larger matrices, in
15100 which case we have to abort this redisplay, and adjust matrices
15101 first. */
15102
15103 enum
15104 {
15105 CURSOR_MOVEMENT_SUCCESS,
15106 CURSOR_MOVEMENT_CANNOT_BE_USED,
15107 CURSOR_MOVEMENT_MUST_SCROLL,
15108 CURSOR_MOVEMENT_NEED_LARGER_MATRICES
15109 };
15110
15111 static int
15112 try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_step)
15113 {
15114 struct window *w = XWINDOW (window);
15115 struct frame *f = XFRAME (w->frame);
15116 int rc = CURSOR_MOVEMENT_CANNOT_BE_USED;
15117
15118 #ifdef GLYPH_DEBUG
15119 if (inhibit_try_cursor_movement)
15120 return rc;
15121 #endif
15122
15123 /* Previously, there was a check for Lisp integer in the
15124 if-statement below. Now, this field is converted to
15125 ptrdiff_t, thus zero means invalid position in a buffer. */
15126 eassert (w->last_point > 0);
15127
15128 /* Handle case where text has not changed, only point, and it has
15129 not moved off the frame. */
15130 if (/* Point may be in this window. */
15131 PT >= CHARPOS (startp)
15132 /* Selective display hasn't changed. */
15133 && !current_buffer->clip_changed
15134 /* Function force-mode-line-update is used to force a thorough
15135 redisplay. It sets either windows_or_buffers_changed or
15136 update_mode_lines. So don't take a shortcut here for these
15137 cases. */
15138 && !update_mode_lines
15139 && !windows_or_buffers_changed
15140 && !cursor_type_changed
15141 /* Can't use this case if highlighting a region. When a
15142 region exists, cursor movement has to do more than just
15143 set the cursor. */
15144 && !(!NILP (Vtransient_mark_mode)
15145 && !NILP (BVAR (current_buffer, mark_active)))
15146 && NILP (w->region_showing)
15147 && NILP (Vshow_trailing_whitespace)
15148 /* This code is not used for mini-buffer for the sake of the case
15149 of redisplaying to replace an echo area message; since in
15150 that case the mini-buffer contents per se are usually
15151 unchanged. This code is of no real use in the mini-buffer
15152 since the handling of this_line_start_pos, etc., in redisplay
15153 handles the same cases. */
15154 && !EQ (window, minibuf_window)
15155 /* When splitting windows or for new windows, it happens that
15156 redisplay is called with a nil window_end_vpos or one being
15157 larger than the window. This should really be fixed in
15158 window.c. I don't have this on my list, now, so we do
15159 approximately the same as the old redisplay code. --gerd. */
15160 && INTEGERP (w->window_end_vpos)
15161 && XFASTINT (w->window_end_vpos) < w->current_matrix->nrows
15162 && (FRAME_WINDOW_P (f)
15163 || !overlay_arrow_in_current_buffer_p ()))
15164 {
15165 int this_scroll_margin, top_scroll_margin;
15166 struct glyph_row *row = NULL;
15167
15168 #ifdef GLYPH_DEBUG
15169 debug_method_add (w, "cursor movement");
15170 #endif
15171
15172 /* Scroll if point within this distance from the top or bottom
15173 of the window. This is a pixel value. */
15174 if (scroll_margin > 0)
15175 {
15176 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
15177 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
15178 }
15179 else
15180 this_scroll_margin = 0;
15181
15182 top_scroll_margin = this_scroll_margin;
15183 if (WINDOW_WANTS_HEADER_LINE_P (w))
15184 top_scroll_margin += CURRENT_HEADER_LINE_HEIGHT (w);
15185
15186 /* Start with the row the cursor was displayed during the last
15187 not paused redisplay. Give up if that row is not valid. */
15188 if (w->last_cursor.vpos < 0
15189 || w->last_cursor.vpos >= w->current_matrix->nrows)
15190 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15191 else
15192 {
15193 row = MATRIX_ROW (w->current_matrix, w->last_cursor.vpos);
15194 if (row->mode_line_p)
15195 ++row;
15196 if (!row->enabled_p)
15197 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15198 }
15199
15200 if (rc == CURSOR_MOVEMENT_CANNOT_BE_USED)
15201 {
15202 int scroll_p = 0, must_scroll = 0;
15203 int last_y = window_text_bottom_y (w) - this_scroll_margin;
15204
15205 if (PT > w->last_point)
15206 {
15207 /* Point has moved forward. */
15208 while (MATRIX_ROW_END_CHARPOS (row) < PT
15209 && MATRIX_ROW_BOTTOM_Y (row) < last_y)
15210 {
15211 eassert (row->enabled_p);
15212 ++row;
15213 }
15214
15215 /* If the end position of a row equals the start
15216 position of the next row, and PT is at that position,
15217 we would rather display cursor in the next line. */
15218 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15219 && MATRIX_ROW_END_CHARPOS (row) == PT
15220 && row < w->current_matrix->rows
15221 + w->current_matrix->nrows - 1
15222 && MATRIX_ROW_START_CHARPOS (row+1) == PT
15223 && !cursor_row_p (row))
15224 ++row;
15225
15226 /* If within the scroll margin, scroll. Note that
15227 MATRIX_ROW_BOTTOM_Y gives the pixel position at which
15228 the next line would be drawn, and that
15229 this_scroll_margin can be zero. */
15230 if (MATRIX_ROW_BOTTOM_Y (row) > last_y
15231 || PT > MATRIX_ROW_END_CHARPOS (row)
15232 /* Line is completely visible last line in window
15233 and PT is to be set in the next line. */
15234 || (MATRIX_ROW_BOTTOM_Y (row) == last_y
15235 && PT == MATRIX_ROW_END_CHARPOS (row)
15236 && !row->ends_at_zv_p
15237 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
15238 scroll_p = 1;
15239 }
15240 else if (PT < w->last_point)
15241 {
15242 /* Cursor has to be moved backward. Note that PT >=
15243 CHARPOS (startp) because of the outer if-statement. */
15244 while (!row->mode_line_p
15245 && (MATRIX_ROW_START_CHARPOS (row) > PT
15246 || (MATRIX_ROW_START_CHARPOS (row) == PT
15247 && (MATRIX_ROW_STARTS_IN_MIDDLE_OF_CHAR_P (row)
15248 || (/* STARTS_IN_MIDDLE_OF_STRING_P (row) */
15249 row > w->current_matrix->rows
15250 && (row-1)->ends_in_newline_from_string_p))))
15251 && (row->y > top_scroll_margin
15252 || CHARPOS (startp) == BEGV))
15253 {
15254 eassert (row->enabled_p);
15255 --row;
15256 }
15257
15258 /* Consider the following case: Window starts at BEGV,
15259 there is invisible, intangible text at BEGV, so that
15260 display starts at some point START > BEGV. It can
15261 happen that we are called with PT somewhere between
15262 BEGV and START. Try to handle that case. */
15263 if (row < w->current_matrix->rows
15264 || row->mode_line_p)
15265 {
15266 row = w->current_matrix->rows;
15267 if (row->mode_line_p)
15268 ++row;
15269 }
15270
15271 /* Due to newlines in overlay strings, we may have to
15272 skip forward over overlay strings. */
15273 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15274 && MATRIX_ROW_END_CHARPOS (row) == PT
15275 && !cursor_row_p (row))
15276 ++row;
15277
15278 /* If within the scroll margin, scroll. */
15279 if (row->y < top_scroll_margin
15280 && CHARPOS (startp) != BEGV)
15281 scroll_p = 1;
15282 }
15283 else
15284 {
15285 /* Cursor did not move. So don't scroll even if cursor line
15286 is partially visible, as it was so before. */
15287 rc = CURSOR_MOVEMENT_SUCCESS;
15288 }
15289
15290 if (PT < MATRIX_ROW_START_CHARPOS (row)
15291 || PT > MATRIX_ROW_END_CHARPOS (row))
15292 {
15293 /* if PT is not in the glyph row, give up. */
15294 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15295 must_scroll = 1;
15296 }
15297 else if (rc != CURSOR_MOVEMENT_SUCCESS
15298 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15299 {
15300 struct glyph_row *row1;
15301
15302 /* If rows are bidi-reordered and point moved, back up
15303 until we find a row that does not belong to a
15304 continuation line. This is because we must consider
15305 all rows of a continued line as candidates for the
15306 new cursor positioning, since row start and end
15307 positions change non-linearly with vertical position
15308 in such rows. */
15309 /* FIXME: Revisit this when glyph ``spilling'' in
15310 continuation lines' rows is implemented for
15311 bidi-reordered rows. */
15312 for (row1 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
15313 MATRIX_ROW_CONTINUATION_LINE_P (row);
15314 --row)
15315 {
15316 /* If we hit the beginning of the displayed portion
15317 without finding the first row of a continued
15318 line, give up. */
15319 if (row <= row1)
15320 {
15321 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15322 break;
15323 }
15324 eassert (row->enabled_p);
15325 }
15326 }
15327 if (must_scroll)
15328 ;
15329 else if (rc != CURSOR_MOVEMENT_SUCCESS
15330 && MATRIX_ROW_PARTIALLY_VISIBLE_P (w, row)
15331 /* Make sure this isn't a header line by any chance, since
15332 then MATRIX_ROW_PARTIALLY_VISIBLE_P might yield non-zero. */
15333 && !row->mode_line_p
15334 && make_cursor_line_fully_visible_p)
15335 {
15336 if (PT == MATRIX_ROW_END_CHARPOS (row)
15337 && !row->ends_at_zv_p
15338 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
15339 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15340 else if (row->height > window_box_height (w))
15341 {
15342 /* If we end up in a partially visible line, let's
15343 make it fully visible, except when it's taller
15344 than the window, in which case we can't do much
15345 about it. */
15346 *scroll_step = 1;
15347 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15348 }
15349 else
15350 {
15351 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
15352 if (!cursor_row_fully_visible_p (w, 0, 1))
15353 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15354 else
15355 rc = CURSOR_MOVEMENT_SUCCESS;
15356 }
15357 }
15358 else if (scroll_p)
15359 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15360 else if (rc != CURSOR_MOVEMENT_SUCCESS
15361 && !NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
15362 {
15363 /* With bidi-reordered rows, there could be more than
15364 one candidate row whose start and end positions
15365 occlude point. We need to let set_cursor_from_row
15366 find the best candidate. */
15367 /* FIXME: Revisit this when glyph ``spilling'' in
15368 continuation lines' rows is implemented for
15369 bidi-reordered rows. */
15370 int rv = 0;
15371
15372 do
15373 {
15374 int at_zv_p = 0, exact_match_p = 0;
15375
15376 if (MATRIX_ROW_START_CHARPOS (row) <= PT
15377 && PT <= MATRIX_ROW_END_CHARPOS (row)
15378 && cursor_row_p (row))
15379 rv |= set_cursor_from_row (w, row, w->current_matrix,
15380 0, 0, 0, 0);
15381 /* As soon as we've found the exact match for point,
15382 or the first suitable row whose ends_at_zv_p flag
15383 is set, we are done. */
15384 at_zv_p =
15385 MATRIX_ROW (w->current_matrix, w->cursor.vpos)->ends_at_zv_p;
15386 if (rv && !at_zv_p
15387 && w->cursor.hpos >= 0
15388 && w->cursor.hpos < MATRIX_ROW_USED (w->current_matrix,
15389 w->cursor.vpos))
15390 {
15391 struct glyph_row *candidate =
15392 MATRIX_ROW (w->current_matrix, w->cursor.vpos);
15393 struct glyph *g =
15394 candidate->glyphs[TEXT_AREA] + w->cursor.hpos;
15395 ptrdiff_t endpos = MATRIX_ROW_END_CHARPOS (candidate);
15396
15397 exact_match_p =
15398 (BUFFERP (g->object) && g->charpos == PT)
15399 || (INTEGERP (g->object)
15400 && (g->charpos == PT
15401 || (g->charpos == 0 && endpos - 1 == PT)));
15402 }
15403 if (rv && (at_zv_p || exact_match_p))
15404 {
15405 rc = CURSOR_MOVEMENT_SUCCESS;
15406 break;
15407 }
15408 if (MATRIX_ROW_BOTTOM_Y (row) == last_y)
15409 break;
15410 ++row;
15411 }
15412 while (((MATRIX_ROW_CONTINUATION_LINE_P (row)
15413 || row->continued_p)
15414 && MATRIX_ROW_BOTTOM_Y (row) <= last_y)
15415 || (MATRIX_ROW_START_CHARPOS (row) == PT
15416 && MATRIX_ROW_BOTTOM_Y (row) < last_y));
15417 /* If we didn't find any candidate rows, or exited the
15418 loop before all the candidates were examined, signal
15419 to the caller that this method failed. */
15420 if (rc != CURSOR_MOVEMENT_SUCCESS
15421 && !(rv
15422 && !MATRIX_ROW_CONTINUATION_LINE_P (row)
15423 && !row->continued_p))
15424 rc = CURSOR_MOVEMENT_MUST_SCROLL;
15425 else if (rv)
15426 rc = CURSOR_MOVEMENT_SUCCESS;
15427 }
15428 else
15429 {
15430 do
15431 {
15432 if (set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0))
15433 {
15434 rc = CURSOR_MOVEMENT_SUCCESS;
15435 break;
15436 }
15437 ++row;
15438 }
15439 while (MATRIX_ROW_BOTTOM_Y (row) < last_y
15440 && MATRIX_ROW_START_CHARPOS (row) == PT
15441 && cursor_row_p (row));
15442 }
15443 }
15444 }
15445
15446 return rc;
15447 }
15448
15449 #if !defined USE_TOOLKIT_SCROLL_BARS || defined USE_GTK
15450 static
15451 #endif
15452 void
15453 set_vertical_scroll_bar (struct window *w)
15454 {
15455 ptrdiff_t start, end, whole;
15456
15457 /* Calculate the start and end positions for the current window.
15458 At some point, it would be nice to choose between scrollbars
15459 which reflect the whole buffer size, with special markers
15460 indicating narrowing, and scrollbars which reflect only the
15461 visible region.
15462
15463 Note that mini-buffers sometimes aren't displaying any text. */
15464 if (!MINI_WINDOW_P (w)
15465 || (w == XWINDOW (minibuf_window)
15466 && NILP (echo_area_buffer[0])))
15467 {
15468 struct buffer *buf = XBUFFER (w->buffer);
15469 whole = BUF_ZV (buf) - BUF_BEGV (buf);
15470 start = marker_position (w->start) - BUF_BEGV (buf);
15471 /* I don't think this is guaranteed to be right. For the
15472 moment, we'll pretend it is. */
15473 end = BUF_Z (buf) - XFASTINT (w->window_end_pos) - BUF_BEGV (buf);
15474
15475 if (end < start)
15476 end = start;
15477 if (whole < (end - start))
15478 whole = end - start;
15479 }
15480 else
15481 start = end = whole = 0;
15482
15483 /* Indicate what this scroll bar ought to be displaying now. */
15484 if (FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15485 (*FRAME_TERMINAL (XFRAME (w->frame))->set_vertical_scroll_bar_hook)
15486 (w, end - start, whole, start);
15487 }
15488
15489
15490 /* Redisplay leaf window WINDOW. JUST_THIS_ONE_P non-zero means only
15491 selected_window is redisplayed.
15492
15493 We can return without actually redisplaying the window if
15494 fonts_changed_p. In that case, redisplay_internal will
15495 retry. */
15496
15497 static void
15498 redisplay_window (Lisp_Object window, int just_this_one_p)
15499 {
15500 struct window *w = XWINDOW (window);
15501 struct frame *f = XFRAME (w->frame);
15502 struct buffer *buffer = XBUFFER (w->buffer);
15503 struct buffer *old = current_buffer;
15504 struct text_pos lpoint, opoint, startp;
15505 int update_mode_line;
15506 int tem;
15507 struct it it;
15508 /* Record it now because it's overwritten. */
15509 int current_matrix_up_to_date_p = 0;
15510 int used_current_matrix_p = 0;
15511 /* This is less strict than current_matrix_up_to_date_p.
15512 It indicates that the buffer contents and narrowing are unchanged. */
15513 int buffer_unchanged_p = 0;
15514 int temp_scroll_step = 0;
15515 ptrdiff_t count = SPECPDL_INDEX ();
15516 int rc;
15517 int centering_position = -1;
15518 int last_line_misfit = 0;
15519 ptrdiff_t beg_unchanged, end_unchanged;
15520
15521 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15522 opoint = lpoint;
15523
15524 /* W must be a leaf window here. */
15525 eassert (!NILP (w->buffer));
15526 #ifdef GLYPH_DEBUG
15527 *w->desired_matrix->method = 0;
15528 #endif
15529
15530 restart:
15531 reconsider_clip_changes (w, buffer);
15532
15533 /* Has the mode line to be updated? */
15534 update_mode_line = (w->update_mode_line
15535 || update_mode_lines
15536 || buffer->clip_changed
15537 || buffer->prevent_redisplay_optimizations_p);
15538
15539 if (MINI_WINDOW_P (w))
15540 {
15541 if (w == XWINDOW (echo_area_window)
15542 && !NILP (echo_area_buffer[0]))
15543 {
15544 if (update_mode_line)
15545 /* We may have to update a tty frame's menu bar or a
15546 tool-bar. Example `M-x C-h C-h C-g'. */
15547 goto finish_menu_bars;
15548 else
15549 /* We've already displayed the echo area glyphs in this window. */
15550 goto finish_scroll_bars;
15551 }
15552 else if ((w != XWINDOW (minibuf_window)
15553 || minibuf_level == 0)
15554 /* When buffer is nonempty, redisplay window normally. */
15555 && BUF_Z (XBUFFER (w->buffer)) == BUF_BEG (XBUFFER (w->buffer))
15556 /* Quail displays non-mini buffers in minibuffer window.
15557 In that case, redisplay the window normally. */
15558 && !NILP (Fmemq (w->buffer, Vminibuffer_list)))
15559 {
15560 /* W is a mini-buffer window, but it's not active, so clear
15561 it. */
15562 int yb = window_text_bottom_y (w);
15563 struct glyph_row *row;
15564 int y;
15565
15566 for (y = 0, row = w->desired_matrix->rows;
15567 y < yb;
15568 y += row->height, ++row)
15569 blank_row (w, row, y);
15570 goto finish_scroll_bars;
15571 }
15572
15573 clear_glyph_matrix (w->desired_matrix);
15574 }
15575
15576 /* Otherwise set up data on this window; select its buffer and point
15577 value. */
15578 /* Really select the buffer, for the sake of buffer-local
15579 variables. */
15580 set_buffer_internal_1 (XBUFFER (w->buffer));
15581
15582 current_matrix_up_to_date_p
15583 = (!NILP (w->window_end_valid)
15584 && !current_buffer->clip_changed
15585 && !current_buffer->prevent_redisplay_optimizations_p
15586 && !window_outdated (w));
15587
15588 /* Run the window-bottom-change-functions
15589 if it is possible that the text on the screen has changed
15590 (either due to modification of the text, or any other reason). */
15591 if (!current_matrix_up_to_date_p
15592 && !NILP (Vwindow_text_change_functions))
15593 {
15594 safe_run_hooks (Qwindow_text_change_functions);
15595 goto restart;
15596 }
15597
15598 beg_unchanged = BEG_UNCHANGED;
15599 end_unchanged = END_UNCHANGED;
15600
15601 SET_TEXT_POS (opoint, PT, PT_BYTE);
15602
15603 specbind (Qinhibit_point_motion_hooks, Qt);
15604
15605 buffer_unchanged_p
15606 = (!NILP (w->window_end_valid)
15607 && !current_buffer->clip_changed
15608 && !window_outdated (w));
15609
15610 /* When windows_or_buffers_changed is non-zero, we can't rely on
15611 the window end being valid, so set it to nil there. */
15612 if (windows_or_buffers_changed)
15613 {
15614 /* If window starts on a continuation line, maybe adjust the
15615 window start in case the window's width changed. */
15616 if (XMARKER (w->start)->buffer == current_buffer)
15617 compute_window_start_on_continuation_line (w);
15618
15619 wset_window_end_valid (w, Qnil);
15620 }
15621
15622 /* Some sanity checks. */
15623 CHECK_WINDOW_END (w);
15624 if (Z == Z_BYTE && CHARPOS (opoint) != BYTEPOS (opoint))
15625 emacs_abort ();
15626 if (BYTEPOS (opoint) < CHARPOS (opoint))
15627 emacs_abort ();
15628
15629 /* If %c is in mode line, update it if needed. */
15630 if (!NILP (w->column_number_displayed)
15631 /* This alternative quickly identifies a common case
15632 where no change is needed. */
15633 && !(PT == w->last_point && !window_outdated (w))
15634 && (XFASTINT (w->column_number_displayed) != current_column ()))
15635 update_mode_line = 1;
15636
15637 /* Count number of windows showing the selected buffer. An indirect
15638 buffer counts as its base buffer. */
15639 if (!just_this_one_p)
15640 {
15641 struct buffer *current_base, *window_base;
15642 current_base = current_buffer;
15643 window_base = XBUFFER (XWINDOW (selected_window)->buffer);
15644 if (current_base->base_buffer)
15645 current_base = current_base->base_buffer;
15646 if (window_base->base_buffer)
15647 window_base = window_base->base_buffer;
15648 if (current_base == window_base)
15649 buffer_shared++;
15650 }
15651
15652 /* Point refers normally to the selected window. For any other
15653 window, set up appropriate value. */
15654 if (!EQ (window, selected_window))
15655 {
15656 ptrdiff_t new_pt = XMARKER (w->pointm)->charpos;
15657 ptrdiff_t new_pt_byte = marker_byte_position (w->pointm);
15658 if (new_pt < BEGV)
15659 {
15660 new_pt = BEGV;
15661 new_pt_byte = BEGV_BYTE;
15662 set_marker_both (w->pointm, Qnil, BEGV, BEGV_BYTE);
15663 }
15664 else if (new_pt > (ZV - 1))
15665 {
15666 new_pt = ZV;
15667 new_pt_byte = ZV_BYTE;
15668 set_marker_both (w->pointm, Qnil, ZV, ZV_BYTE);
15669 }
15670
15671 /* We don't use SET_PT so that the point-motion hooks don't run. */
15672 TEMP_SET_PT_BOTH (new_pt, new_pt_byte);
15673 }
15674
15675 /* If any of the character widths specified in the display table
15676 have changed, invalidate the width run cache. It's true that
15677 this may be a bit late to catch such changes, but the rest of
15678 redisplay goes (non-fatally) haywire when the display table is
15679 changed, so why should we worry about doing any better? */
15680 if (current_buffer->width_run_cache)
15681 {
15682 struct Lisp_Char_Table *disptab = buffer_display_table ();
15683
15684 if (! disptab_matches_widthtab
15685 (disptab, XVECTOR (BVAR (current_buffer, width_table))))
15686 {
15687 invalidate_region_cache (current_buffer,
15688 current_buffer->width_run_cache,
15689 BEG, Z);
15690 recompute_width_table (current_buffer, disptab);
15691 }
15692 }
15693
15694 /* If window-start is screwed up, choose a new one. */
15695 if (XMARKER (w->start)->buffer != current_buffer)
15696 goto recenter;
15697
15698 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15699
15700 /* If someone specified a new starting point but did not insist,
15701 check whether it can be used. */
15702 if (w->optional_new_start
15703 && CHARPOS (startp) >= BEGV
15704 && CHARPOS (startp) <= ZV)
15705 {
15706 w->optional_new_start = 0;
15707 start_display (&it, w, startp);
15708 move_it_to (&it, PT, 0, it.last_visible_y, -1,
15709 MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
15710 if (IT_CHARPOS (it) == PT)
15711 w->force_start = 1;
15712 /* IT may overshoot PT if text at PT is invisible. */
15713 else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
15714 w->force_start = 1;
15715 }
15716
15717 force_start:
15718
15719 /* Handle case where place to start displaying has been specified,
15720 unless the specified location is outside the accessible range. */
15721 if (w->force_start || w->frozen_window_start_p)
15722 {
15723 /* We set this later on if we have to adjust point. */
15724 int new_vpos = -1;
15725
15726 w->force_start = 0;
15727 w->vscroll = 0;
15728 wset_window_end_valid (w, Qnil);
15729
15730 /* Forget any recorded base line for line number display. */
15731 if (!buffer_unchanged_p)
15732 wset_base_line_number (w, Qnil);
15733
15734 /* Redisplay the mode line. Select the buffer properly for that.
15735 Also, run the hook window-scroll-functions
15736 because we have scrolled. */
15737 /* Note, we do this after clearing force_start because
15738 if there's an error, it is better to forget about force_start
15739 than to get into an infinite loop calling the hook functions
15740 and having them get more errors. */
15741 if (!update_mode_line
15742 || ! NILP (Vwindow_scroll_functions))
15743 {
15744 update_mode_line = 1;
15745 w->update_mode_line = 1;
15746 startp = run_window_scroll_functions (window, startp);
15747 }
15748
15749 w->last_modified = 0;
15750 w->last_overlay_modified = 0;
15751 if (CHARPOS (startp) < BEGV)
15752 SET_TEXT_POS (startp, BEGV, BEGV_BYTE);
15753 else if (CHARPOS (startp) > ZV)
15754 SET_TEXT_POS (startp, ZV, ZV_BYTE);
15755
15756 /* Redisplay, then check if cursor has been set during the
15757 redisplay. Give up if new fonts were loaded. */
15758 /* We used to issue a CHECK_MARGINS argument to try_window here,
15759 but this causes scrolling to fail when point begins inside
15760 the scroll margin (bug#148) -- cyd */
15761 if (!try_window (window, startp, 0))
15762 {
15763 w->force_start = 1;
15764 clear_glyph_matrix (w->desired_matrix);
15765 goto need_larger_matrices;
15766 }
15767
15768 if (w->cursor.vpos < 0 && !w->frozen_window_start_p)
15769 {
15770 /* If point does not appear, try to move point so it does
15771 appear. The desired matrix has been built above, so we
15772 can use it here. */
15773 new_vpos = window_box_height (w) / 2;
15774 }
15775
15776 if (!cursor_row_fully_visible_p (w, 0, 0))
15777 {
15778 /* Point does appear, but on a line partly visible at end of window.
15779 Move it back to a fully-visible line. */
15780 new_vpos = window_box_height (w);
15781 }
15782
15783 /* If we need to move point for either of the above reasons,
15784 now actually do it. */
15785 if (new_vpos >= 0)
15786 {
15787 struct glyph_row *row;
15788
15789 row = MATRIX_FIRST_TEXT_ROW (w->desired_matrix);
15790 while (MATRIX_ROW_BOTTOM_Y (row) < new_vpos)
15791 ++row;
15792
15793 TEMP_SET_PT_BOTH (MATRIX_ROW_START_CHARPOS (row),
15794 MATRIX_ROW_START_BYTEPOS (row));
15795
15796 if (w != XWINDOW (selected_window))
15797 set_marker_both (w->pointm, Qnil, PT, PT_BYTE);
15798 else if (current_buffer == old)
15799 SET_TEXT_POS (lpoint, PT, PT_BYTE);
15800
15801 set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
15802
15803 /* If we are highlighting the region, then we just changed
15804 the region, so redisplay to show it. */
15805 if (!NILP (Vtransient_mark_mode)
15806 && !NILP (BVAR (current_buffer, mark_active)))
15807 {
15808 clear_glyph_matrix (w->desired_matrix);
15809 if (!try_window (window, startp, 0))
15810 goto need_larger_matrices;
15811 }
15812 }
15813
15814 #ifdef GLYPH_DEBUG
15815 debug_method_add (w, "forced window start");
15816 #endif
15817 goto done;
15818 }
15819
15820 /* Handle case where text has not changed, only point, and it has
15821 not moved off the frame, and we are not retrying after hscroll.
15822 (current_matrix_up_to_date_p is nonzero when retrying.) */
15823 if (current_matrix_up_to_date_p
15824 && (rc = try_cursor_movement (window, startp, &temp_scroll_step),
15825 rc != CURSOR_MOVEMENT_CANNOT_BE_USED))
15826 {
15827 switch (rc)
15828 {
15829 case CURSOR_MOVEMENT_SUCCESS:
15830 used_current_matrix_p = 1;
15831 goto done;
15832
15833 case CURSOR_MOVEMENT_MUST_SCROLL:
15834 goto try_to_scroll;
15835
15836 default:
15837 emacs_abort ();
15838 }
15839 }
15840 /* If current starting point was originally the beginning of a line
15841 but no longer is, find a new starting point. */
15842 else if (w->start_at_line_beg
15843 && !(CHARPOS (startp) <= BEGV
15844 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n'))
15845 {
15846 #ifdef GLYPH_DEBUG
15847 debug_method_add (w, "recenter 1");
15848 #endif
15849 goto recenter;
15850 }
15851
15852 /* Try scrolling with try_window_id. Value is > 0 if update has
15853 been done, it is -1 if we know that the same window start will
15854 not work. It is 0 if unsuccessful for some other reason. */
15855 else if ((tem = try_window_id (w)) != 0)
15856 {
15857 #ifdef GLYPH_DEBUG
15858 debug_method_add (w, "try_window_id %d", tem);
15859 #endif
15860
15861 if (fonts_changed_p)
15862 goto need_larger_matrices;
15863 if (tem > 0)
15864 goto done;
15865
15866 /* Otherwise try_window_id has returned -1 which means that we
15867 don't want the alternative below this comment to execute. */
15868 }
15869 else if (CHARPOS (startp) >= BEGV
15870 && CHARPOS (startp) <= ZV
15871 && PT >= CHARPOS (startp)
15872 && (CHARPOS (startp) < ZV
15873 /* Avoid starting at end of buffer. */
15874 || CHARPOS (startp) == BEGV
15875 || !window_outdated (w)))
15876 {
15877 int d1, d2, d3, d4, d5, d6;
15878
15879 /* If first window line is a continuation line, and window start
15880 is inside the modified region, but the first change is before
15881 current window start, we must select a new window start.
15882
15883 However, if this is the result of a down-mouse event (e.g. by
15884 extending the mouse-drag-overlay), we don't want to select a
15885 new window start, since that would change the position under
15886 the mouse, resulting in an unwanted mouse-movement rather
15887 than a simple mouse-click. */
15888 if (!w->start_at_line_beg
15889 && NILP (do_mouse_tracking)
15890 && CHARPOS (startp) > BEGV
15891 && CHARPOS (startp) > BEG + beg_unchanged
15892 && CHARPOS (startp) <= Z - end_unchanged
15893 /* Even if w->start_at_line_beg is nil, a new window may
15894 start at a line_beg, since that's how set_buffer_window
15895 sets it. So, we need to check the return value of
15896 compute_window_start_on_continuation_line. (See also
15897 bug#197). */
15898 && XMARKER (w->start)->buffer == current_buffer
15899 && compute_window_start_on_continuation_line (w)
15900 /* It doesn't make sense to force the window start like we
15901 do at label force_start if it is already known that point
15902 will not be visible in the resulting window, because
15903 doing so will move point from its correct position
15904 instead of scrolling the window to bring point into view.
15905 See bug#9324. */
15906 && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
15907 {
15908 w->force_start = 1;
15909 SET_TEXT_POS_FROM_MARKER (startp, w->start);
15910 goto force_start;
15911 }
15912
15913 #ifdef GLYPH_DEBUG
15914 debug_method_add (w, "same window start");
15915 #endif
15916
15917 /* Try to redisplay starting at same place as before.
15918 If point has not moved off frame, accept the results. */
15919 if (!current_matrix_up_to_date_p
15920 /* Don't use try_window_reusing_current_matrix in this case
15921 because a window scroll function can have changed the
15922 buffer. */
15923 || !NILP (Vwindow_scroll_functions)
15924 || MINI_WINDOW_P (w)
15925 || !(used_current_matrix_p
15926 = try_window_reusing_current_matrix (w)))
15927 {
15928 IF_DEBUG (debug_method_add (w, "1"));
15929 if (try_window (window, startp, TRY_WINDOW_CHECK_MARGINS) < 0)
15930 /* -1 means we need to scroll.
15931 0 means we need new matrices, but fonts_changed_p
15932 is set in that case, so we will detect it below. */
15933 goto try_to_scroll;
15934 }
15935
15936 if (fonts_changed_p)
15937 goto need_larger_matrices;
15938
15939 if (w->cursor.vpos >= 0)
15940 {
15941 if (!just_this_one_p
15942 || current_buffer->clip_changed
15943 || BEG_UNCHANGED < CHARPOS (startp))
15944 /* Forget any recorded base line for line number display. */
15945 wset_base_line_number (w, Qnil);
15946
15947 if (!cursor_row_fully_visible_p (w, 1, 0))
15948 {
15949 clear_glyph_matrix (w->desired_matrix);
15950 last_line_misfit = 1;
15951 }
15952 /* Drop through and scroll. */
15953 else
15954 goto done;
15955 }
15956 else
15957 clear_glyph_matrix (w->desired_matrix);
15958 }
15959
15960 try_to_scroll:
15961
15962 w->last_modified = 0;
15963 w->last_overlay_modified = 0;
15964
15965 /* Redisplay the mode line. Select the buffer properly for that. */
15966 if (!update_mode_line)
15967 {
15968 update_mode_line = 1;
15969 w->update_mode_line = 1;
15970 }
15971
15972 /* Try to scroll by specified few lines. */
15973 if ((scroll_conservatively
15974 || emacs_scroll_step
15975 || temp_scroll_step
15976 || NUMBERP (BVAR (current_buffer, scroll_up_aggressively))
15977 || NUMBERP (BVAR (current_buffer, scroll_down_aggressively)))
15978 && CHARPOS (startp) >= BEGV
15979 && CHARPOS (startp) <= ZV)
15980 {
15981 /* The function returns -1 if new fonts were loaded, 1 if
15982 successful, 0 if not successful. */
15983 int ss = try_scrolling (window, just_this_one_p,
15984 scroll_conservatively,
15985 emacs_scroll_step,
15986 temp_scroll_step, last_line_misfit);
15987 switch (ss)
15988 {
15989 case SCROLLING_SUCCESS:
15990 goto done;
15991
15992 case SCROLLING_NEED_LARGER_MATRICES:
15993 goto need_larger_matrices;
15994
15995 case SCROLLING_FAILED:
15996 break;
15997
15998 default:
15999 emacs_abort ();
16000 }
16001 }
16002
16003 /* Finally, just choose a place to start which positions point
16004 according to user preferences. */
16005
16006 recenter:
16007
16008 #ifdef GLYPH_DEBUG
16009 debug_method_add (w, "recenter");
16010 #endif
16011
16012 /* w->vscroll = 0; */
16013
16014 /* Forget any previously recorded base line for line number display. */
16015 if (!buffer_unchanged_p)
16016 wset_base_line_number (w, Qnil);
16017
16018 /* Determine the window start relative to point. */
16019 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16020 it.current_y = it.last_visible_y;
16021 if (centering_position < 0)
16022 {
16023 int margin =
16024 scroll_margin > 0
16025 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16026 : 0;
16027 ptrdiff_t margin_pos = CHARPOS (startp);
16028 Lisp_Object aggressive;
16029 int scrolling_up;
16030
16031 /* If there is a scroll margin at the top of the window, find
16032 its character position. */
16033 if (margin
16034 /* Cannot call start_display if startp is not in the
16035 accessible region of the buffer. This can happen when we
16036 have just switched to a different buffer and/or changed
16037 its restriction. In that case, startp is initialized to
16038 the character position 1 (BEGV) because we did not yet
16039 have chance to display the buffer even once. */
16040 && BEGV <= CHARPOS (startp) && CHARPOS (startp) <= ZV)
16041 {
16042 struct it it1;
16043 void *it1data = NULL;
16044
16045 SAVE_IT (it1, it, it1data);
16046 start_display (&it1, w, startp);
16047 move_it_vertically (&it1, margin * FRAME_LINE_HEIGHT (f));
16048 margin_pos = IT_CHARPOS (it1);
16049 RESTORE_IT (&it, &it, it1data);
16050 }
16051 scrolling_up = PT > margin_pos;
16052 aggressive =
16053 scrolling_up
16054 ? BVAR (current_buffer, scroll_up_aggressively)
16055 : BVAR (current_buffer, scroll_down_aggressively);
16056
16057 if (!MINI_WINDOW_P (w)
16058 && (scroll_conservatively > SCROLL_LIMIT || NUMBERP (aggressive)))
16059 {
16060 int pt_offset = 0;
16061
16062 /* Setting scroll-conservatively overrides
16063 scroll-*-aggressively. */
16064 if (!scroll_conservatively && NUMBERP (aggressive))
16065 {
16066 double float_amount = XFLOATINT (aggressive);
16067
16068 pt_offset = float_amount * WINDOW_BOX_TEXT_HEIGHT (w);
16069 if (pt_offset == 0 && float_amount > 0)
16070 pt_offset = 1;
16071 if (pt_offset && margin > 0)
16072 margin -= 1;
16073 }
16074 /* Compute how much to move the window start backward from
16075 point so that point will be displayed where the user
16076 wants it. */
16077 if (scrolling_up)
16078 {
16079 centering_position = it.last_visible_y;
16080 if (pt_offset)
16081 centering_position -= pt_offset;
16082 centering_position -=
16083 FRAME_LINE_HEIGHT (f) * (1 + margin + (last_line_misfit != 0))
16084 + WINDOW_HEADER_LINE_HEIGHT (w);
16085 /* Don't let point enter the scroll margin near top of
16086 the window. */
16087 if (centering_position < margin * FRAME_LINE_HEIGHT (f))
16088 centering_position = margin * FRAME_LINE_HEIGHT (f);
16089 }
16090 else
16091 centering_position = margin * FRAME_LINE_HEIGHT (f) + pt_offset;
16092 }
16093 else
16094 /* Set the window start half the height of the window backward
16095 from point. */
16096 centering_position = window_box_height (w) / 2;
16097 }
16098 move_it_vertically_backward (&it, centering_position);
16099
16100 eassert (IT_CHARPOS (it) >= BEGV);
16101
16102 /* The function move_it_vertically_backward may move over more
16103 than the specified y-distance. If it->w is small, e.g. a
16104 mini-buffer window, we may end up in front of the window's
16105 display area. Start displaying at the start of the line
16106 containing PT in this case. */
16107 if (it.current_y <= 0)
16108 {
16109 init_iterator (&it, w, PT, PT_BYTE, NULL, DEFAULT_FACE_ID);
16110 move_it_vertically_backward (&it, 0);
16111 it.current_y = 0;
16112 }
16113
16114 it.current_x = it.hpos = 0;
16115
16116 /* Set the window start position here explicitly, to avoid an
16117 infinite loop in case the functions in window-scroll-functions
16118 get errors. */
16119 set_marker_both (w->start, Qnil, IT_CHARPOS (it), IT_BYTEPOS (it));
16120
16121 /* Run scroll hooks. */
16122 startp = run_window_scroll_functions (window, it.current.pos);
16123
16124 /* Redisplay the window. */
16125 if (!current_matrix_up_to_date_p
16126 || windows_or_buffers_changed
16127 || cursor_type_changed
16128 /* Don't use try_window_reusing_current_matrix in this case
16129 because it can have changed the buffer. */
16130 || !NILP (Vwindow_scroll_functions)
16131 || !just_this_one_p
16132 || MINI_WINDOW_P (w)
16133 || !(used_current_matrix_p
16134 = try_window_reusing_current_matrix (w)))
16135 try_window (window, startp, 0);
16136
16137 /* If new fonts have been loaded (due to fontsets), give up. We
16138 have to start a new redisplay since we need to re-adjust glyph
16139 matrices. */
16140 if (fonts_changed_p)
16141 goto need_larger_matrices;
16142
16143 /* If cursor did not appear assume that the middle of the window is
16144 in the first line of the window. Do it again with the next line.
16145 (Imagine a window of height 100, displaying two lines of height
16146 60. Moving back 50 from it->last_visible_y will end in the first
16147 line.) */
16148 if (w->cursor.vpos < 0)
16149 {
16150 if (!NILP (w->window_end_valid)
16151 && PT >= Z - XFASTINT (w->window_end_pos))
16152 {
16153 clear_glyph_matrix (w->desired_matrix);
16154 move_it_by_lines (&it, 1);
16155 try_window (window, it.current.pos, 0);
16156 }
16157 else if (PT < IT_CHARPOS (it))
16158 {
16159 clear_glyph_matrix (w->desired_matrix);
16160 move_it_by_lines (&it, -1);
16161 try_window (window, it.current.pos, 0);
16162 }
16163 else
16164 {
16165 /* Not much we can do about it. */
16166 }
16167 }
16168
16169 /* Consider the following case: Window starts at BEGV, there is
16170 invisible, intangible text at BEGV, so that display starts at
16171 some point START > BEGV. It can happen that we are called with
16172 PT somewhere between BEGV and START. Try to handle that case. */
16173 if (w->cursor.vpos < 0)
16174 {
16175 struct glyph_row *row = w->current_matrix->rows;
16176 if (row->mode_line_p)
16177 ++row;
16178 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
16179 }
16180
16181 if (!cursor_row_fully_visible_p (w, 0, 0))
16182 {
16183 /* If vscroll is enabled, disable it and try again. */
16184 if (w->vscroll)
16185 {
16186 w->vscroll = 0;
16187 clear_glyph_matrix (w->desired_matrix);
16188 goto recenter;
16189 }
16190
16191 /* Users who set scroll-conservatively to a large number want
16192 point just above/below the scroll margin. If we ended up
16193 with point's row partially visible, move the window start to
16194 make that row fully visible and out of the margin. */
16195 if (scroll_conservatively > SCROLL_LIMIT)
16196 {
16197 int margin =
16198 scroll_margin > 0
16199 ? min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4)
16200 : 0;
16201 int move_down = w->cursor.vpos >= WINDOW_TOTAL_LINES (w) / 2;
16202
16203 move_it_by_lines (&it, move_down ? margin + 1 : -(margin + 1));
16204 clear_glyph_matrix (w->desired_matrix);
16205 if (1 == try_window (window, it.current.pos,
16206 TRY_WINDOW_CHECK_MARGINS))
16207 goto done;
16208 }
16209
16210 /* If centering point failed to make the whole line visible,
16211 put point at the top instead. That has to make the whole line
16212 visible, if it can be done. */
16213 if (centering_position == 0)
16214 goto done;
16215
16216 clear_glyph_matrix (w->desired_matrix);
16217 centering_position = 0;
16218 goto recenter;
16219 }
16220
16221 done:
16222
16223 SET_TEXT_POS_FROM_MARKER (startp, w->start);
16224 w->start_at_line_beg = (CHARPOS (startp) == BEGV
16225 || FETCH_BYTE (BYTEPOS (startp) - 1) == '\n');
16226
16227 /* Display the mode line, if we must. */
16228 if ((update_mode_line
16229 /* If window not full width, must redo its mode line
16230 if (a) the window to its side is being redone and
16231 (b) we do a frame-based redisplay. This is a consequence
16232 of how inverted lines are drawn in frame-based redisplay. */
16233 || (!just_this_one_p
16234 && !FRAME_WINDOW_P (f)
16235 && !WINDOW_FULL_WIDTH_P (w))
16236 /* Line number to display. */
16237 || INTEGERP (w->base_line_pos)
16238 /* Column number is displayed and different from the one displayed. */
16239 || (!NILP (w->column_number_displayed)
16240 && (XFASTINT (w->column_number_displayed) != current_column ())))
16241 /* This means that the window has a mode line. */
16242 && (WINDOW_WANTS_MODELINE_P (w)
16243 || WINDOW_WANTS_HEADER_LINE_P (w)))
16244 {
16245 display_mode_lines (w);
16246
16247 /* If mode line height has changed, arrange for a thorough
16248 immediate redisplay using the correct mode line height. */
16249 if (WINDOW_WANTS_MODELINE_P (w)
16250 && CURRENT_MODE_LINE_HEIGHT (w) != DESIRED_MODE_LINE_HEIGHT (w))
16251 {
16252 fonts_changed_p = 1;
16253 MATRIX_MODE_LINE_ROW (w->current_matrix)->height
16254 = DESIRED_MODE_LINE_HEIGHT (w);
16255 }
16256
16257 /* If header line height has changed, arrange for a thorough
16258 immediate redisplay using the correct header line height. */
16259 if (WINDOW_WANTS_HEADER_LINE_P (w)
16260 && CURRENT_HEADER_LINE_HEIGHT (w) != DESIRED_HEADER_LINE_HEIGHT (w))
16261 {
16262 fonts_changed_p = 1;
16263 MATRIX_HEADER_LINE_ROW (w->current_matrix)->height
16264 = DESIRED_HEADER_LINE_HEIGHT (w);
16265 }
16266
16267 if (fonts_changed_p)
16268 goto need_larger_matrices;
16269 }
16270
16271 if (!line_number_displayed
16272 && !BUFFERP (w->base_line_pos))
16273 {
16274 wset_base_line_pos (w, Qnil);
16275 wset_base_line_number (w, Qnil);
16276 }
16277
16278 finish_menu_bars:
16279
16280 /* When we reach a frame's selected window, redo the frame's menu bar. */
16281 if (update_mode_line
16282 && EQ (FRAME_SELECTED_WINDOW (f), window))
16283 {
16284 int redisplay_menu_p = 0;
16285
16286 if (FRAME_WINDOW_P (f))
16287 {
16288 #if defined (USE_X_TOOLKIT) || defined (HAVE_NTGUI) \
16289 || defined (HAVE_NS) || defined (USE_GTK)
16290 redisplay_menu_p = FRAME_EXTERNAL_MENU_BAR (f);
16291 #else
16292 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16293 #endif
16294 }
16295 else
16296 redisplay_menu_p = FRAME_MENU_BAR_LINES (f) > 0;
16297
16298 if (redisplay_menu_p)
16299 display_menu_bar (w);
16300
16301 #ifdef HAVE_WINDOW_SYSTEM
16302 if (FRAME_WINDOW_P (f))
16303 {
16304 #if defined (USE_GTK) || defined (HAVE_NS)
16305 if (FRAME_EXTERNAL_TOOL_BAR (f))
16306 redisplay_tool_bar (f);
16307 #else
16308 if (WINDOWP (f->tool_bar_window)
16309 && (FRAME_TOOL_BAR_LINES (f) > 0
16310 || !NILP (Vauto_resize_tool_bars))
16311 && redisplay_tool_bar (f))
16312 ignore_mouse_drag_p = 1;
16313 #endif
16314 }
16315 #endif
16316 }
16317
16318 #ifdef HAVE_WINDOW_SYSTEM
16319 if (FRAME_WINDOW_P (f)
16320 && update_window_fringes (w, (just_this_one_p
16321 || (!used_current_matrix_p && !overlay_arrow_seen)
16322 || w->pseudo_window_p)))
16323 {
16324 update_begin (f);
16325 block_input ();
16326 if (draw_window_fringes (w, 1))
16327 x_draw_vertical_border (w);
16328 unblock_input ();
16329 update_end (f);
16330 }
16331 #endif /* HAVE_WINDOW_SYSTEM */
16332
16333 /* We go to this label, with fonts_changed_p set,
16334 if it is necessary to try again using larger glyph matrices.
16335 We have to redeem the scroll bar even in this case,
16336 because the loop in redisplay_internal expects that. */
16337 need_larger_matrices:
16338 ;
16339 finish_scroll_bars:
16340
16341 if (WINDOW_HAS_VERTICAL_SCROLL_BAR (w))
16342 {
16343 /* Set the thumb's position and size. */
16344 set_vertical_scroll_bar (w);
16345
16346 /* Note that we actually used the scroll bar attached to this
16347 window, so it shouldn't be deleted at the end of redisplay. */
16348 if (FRAME_TERMINAL (f)->redeem_scroll_bar_hook)
16349 (*FRAME_TERMINAL (f)->redeem_scroll_bar_hook) (w);
16350 }
16351
16352 /* Restore current_buffer and value of point in it. The window
16353 update may have changed the buffer, so first make sure `opoint'
16354 is still valid (Bug#6177). */
16355 if (CHARPOS (opoint) < BEGV)
16356 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
16357 else if (CHARPOS (opoint) > ZV)
16358 TEMP_SET_PT_BOTH (Z, Z_BYTE);
16359 else
16360 TEMP_SET_PT_BOTH (CHARPOS (opoint), BYTEPOS (opoint));
16361
16362 set_buffer_internal_1 (old);
16363 /* Avoid an abort in TEMP_SET_PT_BOTH if the buffer has become
16364 shorter. This can be caused by log truncation in *Messages*. */
16365 if (CHARPOS (lpoint) <= ZV)
16366 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
16367
16368 unbind_to (count, Qnil);
16369 }
16370
16371
16372 /* Build the complete desired matrix of WINDOW with a window start
16373 buffer position POS.
16374
16375 Value is 1 if successful. It is zero if fonts were loaded during
16376 redisplay which makes re-adjusting glyph matrices necessary, and -1
16377 if point would appear in the scroll margins.
16378 (We check the former only if TRY_WINDOW_IGNORE_FONTS_CHANGE is
16379 unset in FLAGS, and the latter only if TRY_WINDOW_CHECK_MARGINS is
16380 set in FLAGS.) */
16381
16382 int
16383 try_window (Lisp_Object window, struct text_pos pos, int flags)
16384 {
16385 struct window *w = XWINDOW (window);
16386 struct it it;
16387 struct glyph_row *last_text_row = NULL;
16388 struct frame *f = XFRAME (w->frame);
16389
16390 /* Make POS the new window start. */
16391 set_marker_both (w->start, Qnil, CHARPOS (pos), BYTEPOS (pos));
16392
16393 /* Mark cursor position as unknown. No overlay arrow seen. */
16394 w->cursor.vpos = -1;
16395 overlay_arrow_seen = 0;
16396
16397 /* Initialize iterator and info to start at POS. */
16398 start_display (&it, w, pos);
16399
16400
16401
16402 /* Display all lines of W. */
16403 while (it.current_y < it.last_visible_y)
16404 {
16405 if (display_line (&it))
16406 last_text_row = it.glyph_row - 1;
16407 if (fonts_changed_p && !(flags & TRY_WINDOW_IGNORE_FONTS_CHANGE))
16408 return 0;
16409 }
16410 #ifdef HAVE_XWIDGETS_xxx
16411 //currently this is needed to detect xwidget movement reliably. or probably not.
16412 printf("try_window\n");
16413 return 0;
16414 #endif
16415
16416 /* Don't let the cursor end in the scroll margins. */
16417 if ((flags & TRY_WINDOW_CHECK_MARGINS)
16418 && !MINI_WINDOW_P (w))
16419 {
16420 int this_scroll_margin;
16421
16422 if (scroll_margin > 0)
16423 {
16424 this_scroll_margin = min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4);
16425 this_scroll_margin *= FRAME_LINE_HEIGHT (f);
16426 }
16427 else
16428 this_scroll_margin = 0;
16429
16430 if ((w->cursor.y >= 0 /* not vscrolled */
16431 && w->cursor.y < this_scroll_margin
16432 && CHARPOS (pos) > BEGV
16433 && IT_CHARPOS (it) < ZV)
16434 /* rms: considering make_cursor_line_fully_visible_p here
16435 seems to give wrong results. We don't want to recenter
16436 when the last line is partly visible, we want to allow
16437 that case to be handled in the usual way. */
16438 || w->cursor.y > it.last_visible_y - this_scroll_margin - 1)
16439 {
16440 w->cursor.vpos = -1;
16441 clear_glyph_matrix (w->desired_matrix);
16442 return -1;
16443 }
16444 }
16445
16446 /* If bottom moved off end of frame, change mode line percentage. */
16447 if (XFASTINT (w->window_end_pos) <= 0
16448 && Z != IT_CHARPOS (it))
16449 w->update_mode_line = 1;
16450
16451 /* Set window_end_pos to the offset of the last character displayed
16452 on the window from the end of current_buffer. Set
16453 window_end_vpos to its row number. */
16454 if (last_text_row)
16455 {
16456 eassert (MATRIX_ROW_DISPLAYS_TEXT_P (last_text_row));
16457 w->window_end_bytepos
16458 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16459 wset_window_end_pos
16460 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16461 wset_window_end_vpos
16462 (w, make_number (MATRIX_ROW_VPOS (last_text_row, w->desired_matrix)));
16463 eassert
16464 (MATRIX_ROW (w->desired_matrix,
16465 XFASTINT (w->window_end_vpos))->displays_text_p);
16466 }
16467 else
16468 {
16469 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16470 wset_window_end_pos (w, make_number (Z - ZV));
16471 wset_window_end_vpos (w, make_number (0));
16472 }
16473
16474 /* But that is not valid info until redisplay finishes. */
16475 wset_window_end_valid (w, Qnil);
16476 return 1;
16477 }
16478
16479
16480 \f
16481 /************************************************************************
16482 Window redisplay reusing current matrix when buffer has not changed
16483 ************************************************************************/
16484
16485 /* Try redisplay of window W showing an unchanged buffer with a
16486 different window start than the last time it was displayed by
16487 reusing its current matrix. Value is non-zero if successful.
16488 W->start is the new window start. */
16489
16490 static int
16491 try_window_reusing_current_matrix (struct window *w)
16492 {
16493 struct frame *f = XFRAME (w->frame);
16494 struct glyph_row *bottom_row;
16495 struct it it;
16496 struct run run;
16497 struct text_pos start, new_start;
16498 int nrows_scrolled, i;
16499 struct glyph_row *last_text_row;
16500 struct glyph_row *last_reused_text_row;
16501 struct glyph_row *start_row;
16502 int start_vpos, min_y, max_y;
16503
16504 #ifdef GLYPH_DEBUG
16505 if (inhibit_try_window_reusing)
16506 return 0;
16507 #endif
16508
16509 #ifdef HAVE_XWIDGETS_xxx
16510 //currently this is needed to detect xwidget movement reliably. or probably not.
16511 printf("try_window_reusing_current_matrix\n");
16512 return 0;
16513 #endif
16514
16515
16516 if (/* This function doesn't handle terminal frames. */
16517 !FRAME_WINDOW_P (f)
16518 /* Don't try to reuse the display if windows have been split
16519 or such. */
16520 || windows_or_buffers_changed
16521 || cursor_type_changed)
16522 return 0;
16523
16524 /* Can't do this if region may have changed. */
16525 if ((!NILP (Vtransient_mark_mode)
16526 && !NILP (BVAR (current_buffer, mark_active)))
16527 || !NILP (w->region_showing)
16528 || !NILP (Vshow_trailing_whitespace))
16529 return 0;
16530
16531 /* If top-line visibility has changed, give up. */
16532 if (WINDOW_WANTS_HEADER_LINE_P (w)
16533 != MATRIX_HEADER_LINE_ROW (w->current_matrix)->mode_line_p)
16534 return 0;
16535
16536 /* Give up if old or new display is scrolled vertically. We could
16537 make this function handle this, but right now it doesn't. */
16538 start_row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16539 if (w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row))
16540 return 0;
16541
16542 /* The variable new_start now holds the new window start. The old
16543 start `start' can be determined from the current matrix. */
16544 SET_TEXT_POS_FROM_MARKER (new_start, w->start);
16545 start = start_row->minpos;
16546 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16547
16548 /* Clear the desired matrix for the display below. */
16549 clear_glyph_matrix (w->desired_matrix);
16550
16551 if (CHARPOS (new_start) <= CHARPOS (start))
16552 {
16553 /* Don't use this method if the display starts with an ellipsis
16554 displayed for invisible text. It's not easy to handle that case
16555 below, and it's certainly not worth the effort since this is
16556 not a frequent case. */
16557 if (in_ellipses_for_invisible_text_p (&start_row->start, w))
16558 return 0;
16559
16560 IF_DEBUG (debug_method_add (w, "twu1"));
16561
16562 /* Display up to a row that can be reused. The variable
16563 last_text_row is set to the last row displayed that displays
16564 text. Note that it.vpos == 0 if or if not there is a
16565 header-line; it's not the same as the MATRIX_ROW_VPOS! */
16566 start_display (&it, w, new_start);
16567 w->cursor.vpos = -1;
16568 last_text_row = last_reused_text_row = NULL;
16569
16570 while (it.current_y < it.last_visible_y
16571 && !fonts_changed_p)
16572 {
16573 /* If we have reached into the characters in the START row,
16574 that means the line boundaries have changed. So we
16575 can't start copying with the row START. Maybe it will
16576 work to start copying with the following row. */
16577 while (IT_CHARPOS (it) > CHARPOS (start))
16578 {
16579 /* Advance to the next row as the "start". */
16580 start_row++;
16581 start = start_row->minpos;
16582 /* If there are no more rows to try, or just one, give up. */
16583 if (start_row == MATRIX_MODE_LINE_ROW (w->current_matrix) - 1
16584 || w->vscroll || MATRIX_ROW_PARTIALLY_VISIBLE_P (w, start_row)
16585 || CHARPOS (start) == ZV)
16586 {
16587 clear_glyph_matrix (w->desired_matrix);
16588 return 0;
16589 }
16590
16591 start_vpos = MATRIX_ROW_VPOS (start_row, w->current_matrix);
16592 }
16593 /* If we have reached alignment, we can copy the rest of the
16594 rows. */
16595 if (IT_CHARPOS (it) == CHARPOS (start)
16596 /* Don't accept "alignment" inside a display vector,
16597 since start_row could have started in the middle of
16598 that same display vector (thus their character
16599 positions match), and we have no way of telling if
16600 that is the case. */
16601 && it.current.dpvec_index < 0)
16602 break;
16603
16604 if (display_line (&it))
16605 last_text_row = it.glyph_row - 1;
16606
16607 }
16608
16609 /* A value of current_y < last_visible_y means that we stopped
16610 at the previous window start, which in turn means that we
16611 have at least one reusable row. */
16612 if (it.current_y < it.last_visible_y)
16613 {
16614 struct glyph_row *row;
16615
16616 /* IT.vpos always starts from 0; it counts text lines. */
16617 nrows_scrolled = it.vpos - (start_row - MATRIX_FIRST_TEXT_ROW (w->current_matrix));
16618
16619 /* Find PT if not already found in the lines displayed. */
16620 if (w->cursor.vpos < 0)
16621 {
16622 int dy = it.current_y - start_row->y;
16623
16624 row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
16625 row = row_containing_pos (w, PT, row, NULL, dy);
16626 if (row)
16627 set_cursor_from_row (w, row, w->current_matrix, 0, 0,
16628 dy, nrows_scrolled);
16629 else
16630 {
16631 clear_glyph_matrix (w->desired_matrix);
16632 return 0;
16633 }
16634 }
16635
16636 /* Scroll the display. Do it before the current matrix is
16637 changed. The problem here is that update has not yet
16638 run, i.e. part of the current matrix is not up to date.
16639 scroll_run_hook will clear the cursor, and use the
16640 current matrix to get the height of the row the cursor is
16641 in. */
16642 run.current_y = start_row->y;
16643 run.desired_y = it.current_y;
16644 run.height = it.last_visible_y - it.current_y;
16645
16646 if (run.height > 0 && run.current_y != run.desired_y)
16647 {
16648 update_begin (f);
16649 FRAME_RIF (f)->update_window_begin_hook (w);
16650 FRAME_RIF (f)->clear_window_mouse_face (w);
16651 FRAME_RIF (f)->scroll_run_hook (w, &run);
16652 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16653 update_end (f);
16654 }
16655
16656 /* Shift current matrix down by nrows_scrolled lines. */
16657 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16658 rotate_matrix (w->current_matrix,
16659 start_vpos,
16660 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16661 nrows_scrolled);
16662
16663 /* Disable lines that must be updated. */
16664 for (i = 0; i < nrows_scrolled; ++i)
16665 (start_row + i)->enabled_p = 0;
16666
16667 /* Re-compute Y positions. */
16668 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16669 max_y = it.last_visible_y;
16670 for (row = start_row + nrows_scrolled;
16671 row < bottom_row;
16672 ++row)
16673 {
16674 row->y = it.current_y;
16675 row->visible_height = row->height;
16676
16677 if (row->y < min_y)
16678 row->visible_height -= min_y - row->y;
16679 if (row->y + row->height > max_y)
16680 row->visible_height -= row->y + row->height - max_y;
16681 if (row->fringe_bitmap_periodic_p)
16682 row->redraw_fringe_bitmaps_p = 1;
16683
16684 it.current_y += row->height;
16685
16686 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16687 last_reused_text_row = row;
16688 if (MATRIX_ROW_BOTTOM_Y (row) >= it.last_visible_y)
16689 break;
16690 }
16691
16692 /* Disable lines in the current matrix which are now
16693 below the window. */
16694 for (++row; row < bottom_row; ++row)
16695 row->enabled_p = row->mode_line_p = 0;
16696 }
16697
16698 /* Update window_end_pos etc.; last_reused_text_row is the last
16699 reused row from the current matrix containing text, if any.
16700 The value of last_text_row is the last displayed line
16701 containing text. */
16702 if (last_reused_text_row)
16703 {
16704 w->window_end_bytepos
16705 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_reused_text_row);
16706 wset_window_end_pos
16707 (w, make_number (Z
16708 - MATRIX_ROW_END_CHARPOS (last_reused_text_row)));
16709 wset_window_end_vpos
16710 (w, make_number (MATRIX_ROW_VPOS (last_reused_text_row,
16711 w->current_matrix)));
16712 }
16713 else if (last_text_row)
16714 {
16715 w->window_end_bytepos
16716 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16717 wset_window_end_pos
16718 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16719 wset_window_end_vpos
16720 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16721 w->desired_matrix)));
16722 }
16723 else
16724 {
16725 /* This window must be completely empty. */
16726 w->window_end_bytepos = Z_BYTE - ZV_BYTE;
16727 wset_window_end_pos (w, make_number (Z - ZV));
16728 wset_window_end_vpos (w, make_number (0));
16729 }
16730 wset_window_end_valid (w, Qnil);
16731
16732 /* Update hint: don't try scrolling again in update_window. */
16733 w->desired_matrix->no_scrolling_p = 1;
16734
16735 #ifdef GLYPH_DEBUG
16736 debug_method_add (w, "try_window_reusing_current_matrix 1");
16737 #endif
16738 return 1;
16739 }
16740 else if (CHARPOS (new_start) > CHARPOS (start))
16741 {
16742 struct glyph_row *pt_row, *row;
16743 struct glyph_row *first_reusable_row;
16744 struct glyph_row *first_row_to_display;
16745 int dy;
16746 int yb = window_text_bottom_y (w);
16747
16748 /* Find the row starting at new_start, if there is one. Don't
16749 reuse a partially visible line at the end. */
16750 first_reusable_row = start_row;
16751 while (first_reusable_row->enabled_p
16752 && MATRIX_ROW_BOTTOM_Y (first_reusable_row) < yb
16753 && (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16754 < CHARPOS (new_start)))
16755 ++first_reusable_row;
16756
16757 /* Give up if there is no row to reuse. */
16758 if (MATRIX_ROW_BOTTOM_Y (first_reusable_row) >= yb
16759 || !first_reusable_row->enabled_p
16760 || (MATRIX_ROW_START_CHARPOS (first_reusable_row)
16761 != CHARPOS (new_start)))
16762 return 0;
16763
16764 /* We can reuse fully visible rows beginning with
16765 first_reusable_row to the end of the window. Set
16766 first_row_to_display to the first row that cannot be reused.
16767 Set pt_row to the row containing point, if there is any. */
16768 pt_row = NULL;
16769 for (first_row_to_display = first_reusable_row;
16770 MATRIX_ROW_BOTTOM_Y (first_row_to_display) < yb;
16771 ++first_row_to_display)
16772 {
16773 if (PT >= MATRIX_ROW_START_CHARPOS (first_row_to_display)
16774 && (PT < MATRIX_ROW_END_CHARPOS (first_row_to_display)
16775 || (PT == MATRIX_ROW_END_CHARPOS (first_row_to_display)
16776 && first_row_to_display->ends_at_zv_p
16777 && pt_row == NULL)))
16778 pt_row = first_row_to_display;
16779 }
16780
16781 /* Start displaying at the start of first_row_to_display. */
16782 eassert (first_row_to_display->y < yb);
16783 init_to_row_start (&it, w, first_row_to_display);
16784
16785 nrows_scrolled = (MATRIX_ROW_VPOS (first_reusable_row, w->current_matrix)
16786 - start_vpos);
16787 it.vpos = (MATRIX_ROW_VPOS (first_row_to_display, w->current_matrix)
16788 - nrows_scrolled);
16789 it.current_y = (first_row_to_display->y - first_reusable_row->y
16790 + WINDOW_HEADER_LINE_HEIGHT (w));
16791
16792 /* Display lines beginning with first_row_to_display in the
16793 desired matrix. Set last_text_row to the last row displayed
16794 that displays text. */
16795 it.glyph_row = MATRIX_ROW (w->desired_matrix, it.vpos);
16796 if (pt_row == NULL)
16797 w->cursor.vpos = -1;
16798 last_text_row = NULL;
16799 while (it.current_y < it.last_visible_y && !fonts_changed_p)
16800 if (display_line (&it))
16801 last_text_row = it.glyph_row - 1;
16802
16803 /* If point is in a reused row, adjust y and vpos of the cursor
16804 position. */
16805 if (pt_row)
16806 {
16807 w->cursor.vpos -= nrows_scrolled;
16808 w->cursor.y -= first_reusable_row->y - start_row->y;
16809 }
16810
16811 /* Give up if point isn't in a row displayed or reused. (This
16812 also handles the case where w->cursor.vpos < nrows_scrolled
16813 after the calls to display_line, which can happen with scroll
16814 margins. See bug#1295.) */
16815 if (w->cursor.vpos < 0)
16816 {
16817 clear_glyph_matrix (w->desired_matrix);
16818 return 0;
16819 }
16820
16821 /* Scroll the display. */
16822 run.current_y = first_reusable_row->y;
16823 run.desired_y = WINDOW_HEADER_LINE_HEIGHT (w);
16824 run.height = it.last_visible_y - run.current_y;
16825 dy = run.current_y - run.desired_y;
16826
16827 if (run.height)
16828 {
16829 update_begin (f);
16830 FRAME_RIF (f)->update_window_begin_hook (w);
16831 FRAME_RIF (f)->clear_window_mouse_face (w);
16832 FRAME_RIF (f)->scroll_run_hook (w, &run);
16833 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
16834 update_end (f);
16835 }
16836
16837 /* Adjust Y positions of reused rows. */
16838 bottom_row = MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w);
16839 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
16840 max_y = it.last_visible_y;
16841 for (row = first_reusable_row; row < first_row_to_display; ++row)
16842 {
16843 row->y -= dy;
16844 row->visible_height = row->height;
16845 if (row->y < min_y)
16846 row->visible_height -= min_y - row->y;
16847 if (row->y + row->height > max_y)
16848 row->visible_height -= row->y + row->height - max_y;
16849 if (row->fringe_bitmap_periodic_p)
16850 row->redraw_fringe_bitmaps_p = 1;
16851 }
16852
16853 /* Scroll the current matrix. */
16854 eassert (nrows_scrolled > 0);
16855 rotate_matrix (w->current_matrix,
16856 start_vpos,
16857 MATRIX_ROW_VPOS (bottom_row, w->current_matrix),
16858 -nrows_scrolled);
16859
16860 /* Disable rows not reused. */
16861 for (row -= nrows_scrolled; row < bottom_row; ++row)
16862 row->enabled_p = 0;
16863
16864 /* Point may have moved to a different line, so we cannot assume that
16865 the previous cursor position is valid; locate the correct row. */
16866 if (pt_row)
16867 {
16868 for (row = MATRIX_ROW (w->current_matrix, w->cursor.vpos);
16869 row < bottom_row
16870 && PT >= MATRIX_ROW_END_CHARPOS (row)
16871 && !row->ends_at_zv_p;
16872 row++)
16873 {
16874 w->cursor.vpos++;
16875 w->cursor.y = row->y;
16876 }
16877 if (row < bottom_row)
16878 {
16879 /* Can't simply scan the row for point with
16880 bidi-reordered glyph rows. Let set_cursor_from_row
16881 figure out where to put the cursor, and if it fails,
16882 give up. */
16883 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering)))
16884 {
16885 if (!set_cursor_from_row (w, row, w->current_matrix,
16886 0, 0, 0, 0))
16887 {
16888 clear_glyph_matrix (w->desired_matrix);
16889 return 0;
16890 }
16891 }
16892 else
16893 {
16894 struct glyph *glyph = row->glyphs[TEXT_AREA] + w->cursor.hpos;
16895 struct glyph *end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
16896
16897 for (; glyph < end
16898 && (!BUFFERP (glyph->object)
16899 || glyph->charpos < PT);
16900 glyph++)
16901 {
16902 w->cursor.hpos++;
16903 w->cursor.x += glyph->pixel_width;
16904 }
16905 }
16906 }
16907 }
16908
16909 /* Adjust window end. A null value of last_text_row means that
16910 the window end is in reused rows which in turn means that
16911 only its vpos can have changed. */
16912 if (last_text_row)
16913 {
16914 w->window_end_bytepos
16915 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
16916 wset_window_end_pos
16917 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
16918 wset_window_end_vpos
16919 (w, make_number (MATRIX_ROW_VPOS (last_text_row,
16920 w->desired_matrix)));
16921 }
16922 else
16923 {
16924 wset_window_end_vpos
16925 (w, make_number (XFASTINT (w->window_end_vpos) - nrows_scrolled));
16926 }
16927
16928 wset_window_end_valid (w, Qnil);
16929 w->desired_matrix->no_scrolling_p = 1;
16930
16931 #ifdef GLYPH_DEBUG
16932 debug_method_add (w, "try_window_reusing_current_matrix 2");
16933 #endif
16934 return 1;
16935 }
16936
16937 return 0;
16938 }
16939
16940
16941 \f
16942 /************************************************************************
16943 Window redisplay reusing current matrix when buffer has changed
16944 ************************************************************************/
16945
16946 static struct glyph_row *find_last_unchanged_at_beg_row (struct window *);
16947 static struct glyph_row *find_first_unchanged_at_end_row (struct window *,
16948 ptrdiff_t *, ptrdiff_t *);
16949 static struct glyph_row *
16950 find_last_row_displaying_text (struct glyph_matrix *, struct it *,
16951 struct glyph_row *);
16952
16953
16954 /* Return the last row in MATRIX displaying text. If row START is
16955 non-null, start searching with that row. IT gives the dimensions
16956 of the display. Value is null if matrix is empty; otherwise it is
16957 a pointer to the row found. */
16958
16959 static struct glyph_row *
16960 find_last_row_displaying_text (struct glyph_matrix *matrix, struct it *it,
16961 struct glyph_row *start)
16962 {
16963 struct glyph_row *row, *row_found;
16964
16965 /* Set row_found to the last row in IT->w's current matrix
16966 displaying text. The loop looks funny but think of partially
16967 visible lines. */
16968 row_found = NULL;
16969 row = start ? start : MATRIX_FIRST_TEXT_ROW (matrix);
16970 while (MATRIX_ROW_DISPLAYS_TEXT_P (row))
16971 {
16972 eassert (row->enabled_p);
16973 row_found = row;
16974 if (MATRIX_ROW_BOTTOM_Y (row) >= it->last_visible_y)
16975 break;
16976 ++row;
16977 }
16978
16979 return row_found;
16980 }
16981
16982
16983 /* Return the last row in the current matrix of W that is not affected
16984 by changes at the start of current_buffer that occurred since W's
16985 current matrix was built. Value is null if no such row exists.
16986
16987 BEG_UNCHANGED us the number of characters unchanged at the start of
16988 current_buffer. BEG + BEG_UNCHANGED is the buffer position of the
16989 first changed character in current_buffer. Characters at positions <
16990 BEG + BEG_UNCHANGED are at the same buffer positions as they were
16991 when the current matrix was built. */
16992
16993 static struct glyph_row *
16994 find_last_unchanged_at_beg_row (struct window *w)
16995 {
16996 ptrdiff_t first_changed_pos = BEG + BEG_UNCHANGED;
16997 struct glyph_row *row;
16998 struct glyph_row *row_found = NULL;
16999 int yb = window_text_bottom_y (w);
17000
17001 /* Find the last row displaying unchanged text. */
17002 for (row = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17003 MATRIX_ROW_DISPLAYS_TEXT_P (row)
17004 && MATRIX_ROW_START_CHARPOS (row) < first_changed_pos;
17005 ++row)
17006 {
17007 if (/* If row ends before first_changed_pos, it is unchanged,
17008 except in some case. */
17009 MATRIX_ROW_END_CHARPOS (row) <= first_changed_pos
17010 /* When row ends in ZV and we write at ZV it is not
17011 unchanged. */
17012 && !row->ends_at_zv_p
17013 /* When first_changed_pos is the end of a continued line,
17014 row is not unchanged because it may be no longer
17015 continued. */
17016 && !(MATRIX_ROW_END_CHARPOS (row) == first_changed_pos
17017 && (row->continued_p
17018 || row->exact_window_width_line_p))
17019 /* If ROW->end is beyond ZV, then ROW->end is outdated and
17020 needs to be recomputed, so don't consider this row as
17021 unchanged. This happens when the last line was
17022 bidi-reordered and was killed immediately before this
17023 redisplay cycle. In that case, ROW->end stores the
17024 buffer position of the first visual-order character of
17025 the killed text, which is now beyond ZV. */
17026 && CHARPOS (row->end.pos) <= ZV)
17027 row_found = row;
17028
17029 /* Stop if last visible row. */
17030 if (MATRIX_ROW_BOTTOM_Y (row) >= yb)
17031 break;
17032 }
17033
17034 return row_found;
17035 }
17036
17037
17038 /* Find the first glyph row in the current matrix of W that is not
17039 affected by changes at the end of current_buffer since the
17040 time W's current matrix was built.
17041
17042 Return in *DELTA the number of chars by which buffer positions in
17043 unchanged text at the end of current_buffer must be adjusted.
17044
17045 Return in *DELTA_BYTES the corresponding number of bytes.
17046
17047 Value is null if no such row exists, i.e. all rows are affected by
17048 changes. */
17049
17050 static struct glyph_row *
17051 find_first_unchanged_at_end_row (struct window *w,
17052 ptrdiff_t *delta, ptrdiff_t *delta_bytes)
17053 {
17054 struct glyph_row *row;
17055 struct glyph_row *row_found = NULL;
17056
17057 *delta = *delta_bytes = 0;
17058
17059 /* Display must not have been paused, otherwise the current matrix
17060 is not up to date. */
17061 eassert (!NILP (w->window_end_valid));
17062
17063 /* A value of window_end_pos >= END_UNCHANGED means that the window
17064 end is in the range of changed text. If so, there is no
17065 unchanged row at the end of W's current matrix. */
17066 if (XFASTINT (w->window_end_pos) >= END_UNCHANGED)
17067 return NULL;
17068
17069 /* Set row to the last row in W's current matrix displaying text. */
17070 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17071
17072 /* If matrix is entirely empty, no unchanged row exists. */
17073 if (MATRIX_ROW_DISPLAYS_TEXT_P (row))
17074 {
17075 /* The value of row is the last glyph row in the matrix having a
17076 meaningful buffer position in it. The end position of row
17077 corresponds to window_end_pos. This allows us to translate
17078 buffer positions in the current matrix to current buffer
17079 positions for characters not in changed text. */
17080 ptrdiff_t Z_old =
17081 MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17082 ptrdiff_t Z_BYTE_old =
17083 MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17084 ptrdiff_t last_unchanged_pos, last_unchanged_pos_old;
17085 struct glyph_row *first_text_row
17086 = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
17087
17088 *delta = Z - Z_old;
17089 *delta_bytes = Z_BYTE - Z_BYTE_old;
17090
17091 /* Set last_unchanged_pos to the buffer position of the last
17092 character in the buffer that has not been changed. Z is the
17093 index + 1 of the last character in current_buffer, i.e. by
17094 subtracting END_UNCHANGED we get the index of the last
17095 unchanged character, and we have to add BEG to get its buffer
17096 position. */
17097 last_unchanged_pos = Z - END_UNCHANGED + BEG;
17098 last_unchanged_pos_old = last_unchanged_pos - *delta;
17099
17100 /* Search backward from ROW for a row displaying a line that
17101 starts at a minimum position >= last_unchanged_pos_old. */
17102 for (; row > first_text_row; --row)
17103 {
17104 /* This used to abort, but it can happen.
17105 It is ok to just stop the search instead here. KFS. */
17106 if (!row->enabled_p || !MATRIX_ROW_DISPLAYS_TEXT_P (row))
17107 break;
17108
17109 if (MATRIX_ROW_START_CHARPOS (row) >= last_unchanged_pos_old)
17110 row_found = row;
17111 }
17112 }
17113
17114 eassert (!row_found || MATRIX_ROW_DISPLAYS_TEXT_P (row_found));
17115
17116 return row_found;
17117 }
17118
17119
17120 /* Make sure that glyph rows in the current matrix of window W
17121 reference the same glyph memory as corresponding rows in the
17122 frame's frame matrix. This function is called after scrolling W's
17123 current matrix on a terminal frame in try_window_id and
17124 try_window_reusing_current_matrix. */
17125
17126 static void
17127 sync_frame_with_window_matrix_rows (struct window *w)
17128 {
17129 struct frame *f = XFRAME (w->frame);
17130 struct glyph_row *window_row, *window_row_end, *frame_row;
17131
17132 /* Preconditions: W must be a leaf window and full-width. Its frame
17133 must have a frame matrix. */
17134 eassert (NILP (w->hchild) && NILP (w->vchild));
17135 eassert (WINDOW_FULL_WIDTH_P (w));
17136 eassert (!FRAME_WINDOW_P (f));
17137
17138 /* If W is a full-width window, glyph pointers in W's current matrix
17139 have, by definition, to be the same as glyph pointers in the
17140 corresponding frame matrix. Note that frame matrices have no
17141 marginal areas (see build_frame_matrix). */
17142 window_row = w->current_matrix->rows;
17143 window_row_end = window_row + w->current_matrix->nrows;
17144 frame_row = f->current_matrix->rows + WINDOW_TOP_EDGE_LINE (w);
17145 while (window_row < window_row_end)
17146 {
17147 struct glyph *start = window_row->glyphs[LEFT_MARGIN_AREA];
17148 struct glyph *end = window_row->glyphs[LAST_AREA];
17149
17150 frame_row->glyphs[LEFT_MARGIN_AREA] = start;
17151 frame_row->glyphs[TEXT_AREA] = start;
17152 frame_row->glyphs[RIGHT_MARGIN_AREA] = end;
17153 frame_row->glyphs[LAST_AREA] = end;
17154
17155 /* Disable frame rows whose corresponding window rows have
17156 been disabled in try_window_id. */
17157 if (!window_row->enabled_p)
17158 frame_row->enabled_p = 0;
17159
17160 ++window_row, ++frame_row;
17161 }
17162 }
17163
17164
17165 /* Find the glyph row in window W containing CHARPOS. Consider all
17166 rows between START and END (not inclusive). END null means search
17167 all rows to the end of the display area of W. Value is the row
17168 containing CHARPOS or null. */
17169
17170 struct glyph_row *
17171 row_containing_pos (struct window *w, ptrdiff_t charpos,
17172 struct glyph_row *start, struct glyph_row *end, int dy)
17173 {
17174 struct glyph_row *row = start;
17175 struct glyph_row *best_row = NULL;
17176 ptrdiff_t mindif = BUF_ZV (XBUFFER (w->buffer)) + 1;
17177 int last_y;
17178
17179 /* If we happen to start on a header-line, skip that. */
17180 if (row->mode_line_p)
17181 ++row;
17182
17183 if ((end && row >= end) || !row->enabled_p)
17184 return NULL;
17185
17186 last_y = window_text_bottom_y (w) - dy;
17187
17188 while (1)
17189 {
17190 /* Give up if we have gone too far. */
17191 if (end && row >= end)
17192 return NULL;
17193 /* This formerly returned if they were equal.
17194 I think that both quantities are of a "last plus one" type;
17195 if so, when they are equal, the row is within the screen. -- rms. */
17196 if (MATRIX_ROW_BOTTOM_Y (row) > last_y)
17197 return NULL;
17198
17199 /* If it is in this row, return this row. */
17200 if (! (MATRIX_ROW_END_CHARPOS (row) < charpos
17201 || (MATRIX_ROW_END_CHARPOS (row) == charpos
17202 /* The end position of a row equals the start
17203 position of the next row. If CHARPOS is there, we
17204 would rather display it in the next line, except
17205 when this line ends in ZV. */
17206 && !row->ends_at_zv_p
17207 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
17208 && charpos >= MATRIX_ROW_START_CHARPOS (row))
17209 {
17210 struct glyph *g;
17211
17212 if (NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17213 || (!best_row && !row->continued_p))
17214 return row;
17215 /* In bidi-reordered rows, there could be several rows
17216 occluding point, all of them belonging to the same
17217 continued line. We need to find the row which fits
17218 CHARPOS the best. */
17219 for (g = row->glyphs[TEXT_AREA];
17220 g < row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
17221 g++)
17222 {
17223 if (!STRINGP (g->object))
17224 {
17225 if (g->charpos > 0 && eabs (g->charpos - charpos) < mindif)
17226 {
17227 mindif = eabs (g->charpos - charpos);
17228 best_row = row;
17229 /* Exact match always wins. */
17230 if (mindif == 0)
17231 return best_row;
17232 }
17233 }
17234 }
17235 }
17236 else if (best_row && !row->continued_p)
17237 return best_row;
17238 ++row;
17239 }
17240 }
17241
17242
17243 /* Try to redisplay window W by reusing its existing display. W's
17244 current matrix must be up to date when this function is called,
17245 i.e. window_end_valid must not be nil.
17246
17247 Value is
17248
17249 1 if display has been updated
17250 0 if otherwise unsuccessful
17251 -1 if redisplay with same window start is known not to succeed
17252
17253 The following steps are performed:
17254
17255 1. Find the last row in the current matrix of W that is not
17256 affected by changes at the start of current_buffer. If no such row
17257 is found, give up.
17258
17259 2. Find the first row in W's current matrix that is not affected by
17260 changes at the end of current_buffer. Maybe there is no such row.
17261
17262 3. Display lines beginning with the row + 1 found in step 1 to the
17263 row found in step 2 or, if step 2 didn't find a row, to the end of
17264 the window.
17265
17266 4. If cursor is not known to appear on the window, give up.
17267
17268 5. If display stopped at the row found in step 2, scroll the
17269 display and current matrix as needed.
17270
17271 6. Maybe display some lines at the end of W, if we must. This can
17272 happen under various circumstances, like a partially visible line
17273 becoming fully visible, or because newly displayed lines are displayed
17274 in smaller font sizes.
17275
17276 7. Update W's window end information. */
17277
17278 static int
17279 try_window_id (struct window *w)
17280 {
17281 struct frame *f = XFRAME (w->frame);
17282 struct glyph_matrix *current_matrix = w->current_matrix;
17283 struct glyph_matrix *desired_matrix = w->desired_matrix;
17284 struct glyph_row *last_unchanged_at_beg_row;
17285 struct glyph_row *first_unchanged_at_end_row;
17286 struct glyph_row *row;
17287 struct glyph_row *bottom_row;
17288 int bottom_vpos;
17289 struct it it;
17290 ptrdiff_t delta = 0, delta_bytes = 0, stop_pos;
17291 int dvpos, dy;
17292 struct text_pos start_pos;
17293 struct run run;
17294 int first_unchanged_at_end_vpos = 0;
17295 struct glyph_row *last_text_row, *last_text_row_at_end;
17296 struct text_pos start;
17297 ptrdiff_t first_changed_charpos, last_changed_charpos;
17298
17299 #ifdef GLYPH_DEBUG
17300 if (inhibit_try_window_id)
17301 return 0;
17302 #endif
17303
17304 #ifdef HAVE_XWIDGETS_xxx
17305 //maybe needed for proper xwidget movement
17306 printf("try_window_id\n");
17307 return -1;
17308 #endif
17309
17310
17311 /* This is handy for debugging. */
17312 #if 0
17313 #define GIVE_UP(X) \
17314 do { \
17315 fprintf (stderr, "try_window_id give up %d\n", (X)); \
17316 return 0; \
17317 } while (0)
17318 #else
17319 #define GIVE_UP(X) return 0
17320 #endif
17321
17322 SET_TEXT_POS_FROM_MARKER (start, w->start);
17323
17324 /* Don't use this for mini-windows because these can show
17325 messages and mini-buffers, and we don't handle that here. */
17326 if (MINI_WINDOW_P (w))
17327 GIVE_UP (1);
17328
17329 /* This flag is used to prevent redisplay optimizations. */
17330 if (windows_or_buffers_changed || cursor_type_changed)
17331 GIVE_UP (2);
17332
17333 /* Verify that narrowing has not changed.
17334 Also verify that we were not told to prevent redisplay optimizations.
17335 It would be nice to further
17336 reduce the number of cases where this prevents try_window_id. */
17337 if (current_buffer->clip_changed
17338 || current_buffer->prevent_redisplay_optimizations_p)
17339 GIVE_UP (3);
17340
17341 /* Window must either use window-based redisplay or be full width. */
17342 if (!FRAME_WINDOW_P (f)
17343 && (!FRAME_LINE_INS_DEL_OK (f)
17344 || !WINDOW_FULL_WIDTH_P (w)))
17345 GIVE_UP (4);
17346
17347 /* Give up if point is known NOT to appear in W. */
17348 if (PT < CHARPOS (start))
17349 GIVE_UP (5);
17350
17351 /* Another way to prevent redisplay optimizations. */
17352 if (w->last_modified == 0)
17353 GIVE_UP (6);
17354
17355 /* Verify that window is not hscrolled. */
17356 if (w->hscroll != 0)
17357 GIVE_UP (7);
17358
17359 /* Verify that display wasn't paused. */
17360 if (NILP (w->window_end_valid))
17361 GIVE_UP (8);
17362
17363 /* Can't use this if highlighting a region because a cursor movement
17364 will do more than just set the cursor. */
17365 if (!NILP (Vtransient_mark_mode)
17366 && !NILP (BVAR (current_buffer, mark_active)))
17367 GIVE_UP (9);
17368
17369 /* Likewise if highlighting trailing whitespace. */
17370 if (!NILP (Vshow_trailing_whitespace))
17371 GIVE_UP (11);
17372
17373 /* Likewise if showing a region. */
17374 if (!NILP (w->region_showing))
17375 GIVE_UP (10);
17376
17377 /* Can't use this if overlay arrow position and/or string have
17378 changed. */
17379 if (overlay_arrows_changed_p ())
17380 GIVE_UP (12);
17381
17382 /* When word-wrap is on, adding a space to the first word of a
17383 wrapped line can change the wrap position, altering the line
17384 above it. It might be worthwhile to handle this more
17385 intelligently, but for now just redisplay from scratch. */
17386 if (!NILP (BVAR (XBUFFER (w->buffer), word_wrap)))
17387 GIVE_UP (21);
17388
17389 /* Under bidi reordering, adding or deleting a character in the
17390 beginning of a paragraph, before the first strong directional
17391 character, can change the base direction of the paragraph (unless
17392 the buffer specifies a fixed paragraph direction), which will
17393 require to redisplay the whole paragraph. It might be worthwhile
17394 to find the paragraph limits and widen the range of redisplayed
17395 lines to that, but for now just give up this optimization and
17396 redisplay from scratch. */
17397 if (!NILP (BVAR (XBUFFER (w->buffer), bidi_display_reordering))
17398 && NILP (BVAR (XBUFFER (w->buffer), bidi_paragraph_direction)))
17399 GIVE_UP (22);
17400
17401 /* Make sure beg_unchanged and end_unchanged are up to date. Do it
17402 only if buffer has really changed. The reason is that the gap is
17403 initially at Z for freshly visited files. The code below would
17404 set end_unchanged to 0 in that case. */
17405 if (MODIFF > SAVE_MODIFF
17406 /* This seems to happen sometimes after saving a buffer. */
17407 || BEG_UNCHANGED + END_UNCHANGED > Z_BYTE)
17408 {
17409 if (GPT - BEG < BEG_UNCHANGED)
17410 BEG_UNCHANGED = GPT - BEG;
17411 if (Z - GPT < END_UNCHANGED)
17412 END_UNCHANGED = Z - GPT;
17413 }
17414
17415 /* The position of the first and last character that has been changed. */
17416 first_changed_charpos = BEG + BEG_UNCHANGED;
17417 last_changed_charpos = Z - END_UNCHANGED;
17418
17419 /* If window starts after a line end, and the last change is in
17420 front of that newline, then changes don't affect the display.
17421 This case happens with stealth-fontification. Note that although
17422 the display is unchanged, glyph positions in the matrix have to
17423 be adjusted, of course. */
17424 row = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
17425 if (MATRIX_ROW_DISPLAYS_TEXT_P (row)
17426 && ((last_changed_charpos < CHARPOS (start)
17427 && CHARPOS (start) == BEGV)
17428 || (last_changed_charpos < CHARPOS (start) - 1
17429 && FETCH_BYTE (BYTEPOS (start) - 1) == '\n')))
17430 {
17431 ptrdiff_t Z_old, Z_delta, Z_BYTE_old, Z_delta_bytes;
17432 struct glyph_row *r0;
17433
17434 /* Compute how many chars/bytes have been added to or removed
17435 from the buffer. */
17436 Z_old = MATRIX_ROW_END_CHARPOS (row) + XFASTINT (w->window_end_pos);
17437 Z_BYTE_old = MATRIX_ROW_END_BYTEPOS (row) + w->window_end_bytepos;
17438 Z_delta = Z - Z_old;
17439 Z_delta_bytes = Z_BYTE - Z_BYTE_old;
17440
17441 /* Give up if PT is not in the window. Note that it already has
17442 been checked at the start of try_window_id that PT is not in
17443 front of the window start. */
17444 if (PT >= MATRIX_ROW_END_CHARPOS (row) + Z_delta)
17445 GIVE_UP (13);
17446
17447 /* If window start is unchanged, we can reuse the whole matrix
17448 as is, after adjusting glyph positions. No need to compute
17449 the window end again, since its offset from Z hasn't changed. */
17450 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17451 if (CHARPOS (start) == MATRIX_ROW_START_CHARPOS (r0) + Z_delta
17452 && BYTEPOS (start) == MATRIX_ROW_START_BYTEPOS (r0) + Z_delta_bytes
17453 /* PT must not be in a partially visible line. */
17454 && !(PT >= MATRIX_ROW_START_CHARPOS (row) + Z_delta
17455 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17456 {
17457 /* Adjust positions in the glyph matrix. */
17458 if (Z_delta || Z_delta_bytes)
17459 {
17460 struct glyph_row *r1
17461 = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17462 increment_matrix_positions (w->current_matrix,
17463 MATRIX_ROW_VPOS (r0, current_matrix),
17464 MATRIX_ROW_VPOS (r1, current_matrix),
17465 Z_delta, Z_delta_bytes);
17466 }
17467
17468 /* Set the cursor. */
17469 row = row_containing_pos (w, PT, r0, NULL, 0);
17470 if (row)
17471 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17472 else
17473 emacs_abort ();
17474 return 1;
17475 }
17476 }
17477
17478 /* Handle the case that changes are all below what is displayed in
17479 the window, and that PT is in the window. This shortcut cannot
17480 be taken if ZV is visible in the window, and text has been added
17481 there that is visible in the window. */
17482 if (first_changed_charpos >= MATRIX_ROW_END_CHARPOS (row)
17483 /* ZV is not visible in the window, or there are no
17484 changes at ZV, actually. */
17485 && (current_matrix->zv > MATRIX_ROW_END_CHARPOS (row)
17486 || first_changed_charpos == last_changed_charpos))
17487 {
17488 struct glyph_row *r0;
17489
17490 /* Give up if PT is not in the window. Note that it already has
17491 been checked at the start of try_window_id that PT is not in
17492 front of the window start. */
17493 if (PT >= MATRIX_ROW_END_CHARPOS (row))
17494 GIVE_UP (14);
17495
17496 /* If window start is unchanged, we can reuse the whole matrix
17497 as is, without changing glyph positions since no text has
17498 been added/removed in front of the window end. */
17499 r0 = MATRIX_FIRST_TEXT_ROW (current_matrix);
17500 if (TEXT_POS_EQUAL_P (start, r0->minpos)
17501 /* PT must not be in a partially visible line. */
17502 && !(PT >= MATRIX_ROW_START_CHARPOS (row)
17503 && MATRIX_ROW_BOTTOM_Y (row) > window_text_bottom_y (w)))
17504 {
17505 /* We have to compute the window end anew since text
17506 could have been added/removed after it. */
17507 wset_window_end_pos
17508 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17509 w->window_end_bytepos
17510 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17511
17512 /* Set the cursor. */
17513 row = row_containing_pos (w, PT, r0, NULL, 0);
17514 if (row)
17515 set_cursor_from_row (w, row, current_matrix, 0, 0, 0, 0);
17516 else
17517 emacs_abort ();
17518 return 2;
17519 }
17520 }
17521
17522 /* Give up if window start is in the changed area.
17523
17524 The condition used to read
17525
17526 (BEG_UNCHANGED + END_UNCHANGED != Z - BEG && ...)
17527
17528 but why that was tested escapes me at the moment. */
17529 if (CHARPOS (start) >= first_changed_charpos
17530 && CHARPOS (start) <= last_changed_charpos)
17531 GIVE_UP (15);
17532
17533 /* Check that window start agrees with the start of the first glyph
17534 row in its current matrix. Check this after we know the window
17535 start is not in changed text, otherwise positions would not be
17536 comparable. */
17537 row = MATRIX_FIRST_TEXT_ROW (current_matrix);
17538 if (!TEXT_POS_EQUAL_P (start, row->minpos))
17539 GIVE_UP (16);
17540
17541 /* Give up if the window ends in strings. Overlay strings
17542 at the end are difficult to handle, so don't try. */
17543 row = MATRIX_ROW (current_matrix, XFASTINT (w->window_end_vpos));
17544 if (MATRIX_ROW_START_CHARPOS (row) == MATRIX_ROW_END_CHARPOS (row))
17545 GIVE_UP (20);
17546
17547 /* Compute the position at which we have to start displaying new
17548 lines. Some of the lines at the top of the window might be
17549 reusable because they are not displaying changed text. Find the
17550 last row in W's current matrix not affected by changes at the
17551 start of current_buffer. Value is null if changes start in the
17552 first line of window. */
17553 last_unchanged_at_beg_row = find_last_unchanged_at_beg_row (w);
17554 if (last_unchanged_at_beg_row)
17555 {
17556 /* Avoid starting to display in the middle of a character, a TAB
17557 for instance. This is easier than to set up the iterator
17558 exactly, and it's not a frequent case, so the additional
17559 effort wouldn't really pay off. */
17560 while ((MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row)
17561 || last_unchanged_at_beg_row->ends_in_newline_from_string_p)
17562 && last_unchanged_at_beg_row > w->current_matrix->rows)
17563 --last_unchanged_at_beg_row;
17564
17565 if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (last_unchanged_at_beg_row))
17566 GIVE_UP (17);
17567
17568 if (init_to_row_end (&it, w, last_unchanged_at_beg_row) == 0)
17569 GIVE_UP (18);
17570 start_pos = it.current.pos;
17571
17572 /* Start displaying new lines in the desired matrix at the same
17573 vpos we would use in the current matrix, i.e. below
17574 last_unchanged_at_beg_row. */
17575 it.vpos = 1 + MATRIX_ROW_VPOS (last_unchanged_at_beg_row,
17576 current_matrix);
17577 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17578 it.current_y = MATRIX_ROW_BOTTOM_Y (last_unchanged_at_beg_row);
17579
17580 eassert (it.hpos == 0 && it.current_x == 0);
17581 }
17582 else
17583 {
17584 /* There are no reusable lines at the start of the window.
17585 Start displaying in the first text line. */
17586 start_display (&it, w, start);
17587 it.vpos = it.first_vpos;
17588 start_pos = it.current.pos;
17589 }
17590
17591 /* Find the first row that is not affected by changes at the end of
17592 the buffer. Value will be null if there is no unchanged row, in
17593 which case we must redisplay to the end of the window. delta
17594 will be set to the value by which buffer positions beginning with
17595 first_unchanged_at_end_row have to be adjusted due to text
17596 changes. */
17597 first_unchanged_at_end_row
17598 = find_first_unchanged_at_end_row (w, &delta, &delta_bytes);
17599 IF_DEBUG (debug_delta = delta);
17600 IF_DEBUG (debug_delta_bytes = delta_bytes);
17601
17602 /* Set stop_pos to the buffer position up to which we will have to
17603 display new lines. If first_unchanged_at_end_row != NULL, this
17604 is the buffer position of the start of the line displayed in that
17605 row. For first_unchanged_at_end_row == NULL, use 0 to indicate
17606 that we don't stop at a buffer position. */
17607 stop_pos = 0;
17608 if (first_unchanged_at_end_row)
17609 {
17610 eassert (last_unchanged_at_beg_row == NULL
17611 || first_unchanged_at_end_row >= last_unchanged_at_beg_row);
17612
17613 /* If this is a continuation line, move forward to the next one
17614 that isn't. Changes in lines above affect this line.
17615 Caution: this may move first_unchanged_at_end_row to a row
17616 not displaying text. */
17617 while (MATRIX_ROW_CONTINUATION_LINE_P (first_unchanged_at_end_row)
17618 && MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17619 && (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17620 < it.last_visible_y))
17621 ++first_unchanged_at_end_row;
17622
17623 if (!MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row)
17624 || (MATRIX_ROW_BOTTOM_Y (first_unchanged_at_end_row)
17625 >= it.last_visible_y))
17626 first_unchanged_at_end_row = NULL;
17627 else
17628 {
17629 stop_pos = (MATRIX_ROW_START_CHARPOS (first_unchanged_at_end_row)
17630 + delta);
17631 first_unchanged_at_end_vpos
17632 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, current_matrix);
17633 eassert (stop_pos >= Z - END_UNCHANGED);
17634 }
17635 }
17636 else if (last_unchanged_at_beg_row == NULL)
17637 GIVE_UP (19);
17638
17639
17640 #ifdef GLYPH_DEBUG
17641
17642 /* Either there is no unchanged row at the end, or the one we have
17643 now displays text. This is a necessary condition for the window
17644 end pos calculation at the end of this function. */
17645 eassert (first_unchanged_at_end_row == NULL
17646 || MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row));
17647
17648 debug_last_unchanged_at_beg_vpos
17649 = (last_unchanged_at_beg_row
17650 ? MATRIX_ROW_VPOS (last_unchanged_at_beg_row, current_matrix)
17651 : -1);
17652 debug_first_unchanged_at_end_vpos = first_unchanged_at_end_vpos;
17653
17654 #endif /* GLYPH_DEBUG */
17655
17656
17657 /* Display new lines. Set last_text_row to the last new line
17658 displayed which has text on it, i.e. might end up as being the
17659 line where the window_end_vpos is. */
17660 w->cursor.vpos = -1;
17661 last_text_row = NULL;
17662 overlay_arrow_seen = 0;
17663 while (it.current_y < it.last_visible_y
17664 && !fonts_changed_p
17665 && (first_unchanged_at_end_row == NULL
17666 || IT_CHARPOS (it) < stop_pos))
17667 {
17668 if (display_line (&it))
17669 last_text_row = it.glyph_row - 1;
17670 }
17671
17672 if (fonts_changed_p)
17673 return -1;
17674
17675
17676 /* Compute differences in buffer positions, y-positions etc. for
17677 lines reused at the bottom of the window. Compute what we can
17678 scroll. */
17679 if (first_unchanged_at_end_row
17680 /* No lines reused because we displayed everything up to the
17681 bottom of the window. */
17682 && it.current_y < it.last_visible_y)
17683 {
17684 dvpos = (it.vpos
17685 - MATRIX_ROW_VPOS (first_unchanged_at_end_row,
17686 current_matrix));
17687 dy = it.current_y - first_unchanged_at_end_row->y;
17688 run.current_y = first_unchanged_at_end_row->y;
17689 run.desired_y = run.current_y + dy;
17690 run.height = it.last_visible_y - max (run.current_y, run.desired_y);
17691 }
17692 else
17693 {
17694 delta = delta_bytes = dvpos = dy
17695 = run.current_y = run.desired_y = run.height = 0;
17696 first_unchanged_at_end_row = NULL;
17697 }
17698 IF_DEBUG (debug_dvpos = dvpos; debug_dy = dy);
17699
17700
17701 /* Find the cursor if not already found. We have to decide whether
17702 PT will appear on this window (it sometimes doesn't, but this is
17703 not a very frequent case.) This decision has to be made before
17704 the current matrix is altered. A value of cursor.vpos < 0 means
17705 that PT is either in one of the lines beginning at
17706 first_unchanged_at_end_row or below the window. Don't care for
17707 lines that might be displayed later at the window end; as
17708 mentioned, this is not a frequent case. */
17709 if (w->cursor.vpos < 0)
17710 {
17711 /* Cursor in unchanged rows at the top? */
17712 if (PT < CHARPOS (start_pos)
17713 && last_unchanged_at_beg_row)
17714 {
17715 row = row_containing_pos (w, PT,
17716 MATRIX_FIRST_TEXT_ROW (w->current_matrix),
17717 last_unchanged_at_beg_row + 1, 0);
17718 if (row)
17719 set_cursor_from_row (w, row, w->current_matrix, 0, 0, 0, 0);
17720 }
17721
17722 /* Start from first_unchanged_at_end_row looking for PT. */
17723 else if (first_unchanged_at_end_row)
17724 {
17725 row = row_containing_pos (w, PT - delta,
17726 first_unchanged_at_end_row, NULL, 0);
17727 if (row)
17728 set_cursor_from_row (w, row, w->current_matrix, delta,
17729 delta_bytes, dy, dvpos);
17730 }
17731
17732 /* Give up if cursor was not found. */
17733 if (w->cursor.vpos < 0)
17734 {
17735 clear_glyph_matrix (w->desired_matrix);
17736 return -1;
17737 }
17738 }
17739
17740 /* Don't let the cursor end in the scroll margins. */
17741 {
17742 int this_scroll_margin, cursor_height;
17743
17744 this_scroll_margin =
17745 max (0, min (scroll_margin, WINDOW_TOTAL_LINES (w) / 4));
17746 this_scroll_margin *= FRAME_LINE_HEIGHT (it.f);
17747 cursor_height = MATRIX_ROW (w->desired_matrix, w->cursor.vpos)->height;
17748
17749 if ((w->cursor.y < this_scroll_margin
17750 && CHARPOS (start) > BEGV)
17751 /* Old redisplay didn't take scroll margin into account at the bottom,
17752 but then global-hl-line-mode doesn't scroll. KFS 2004-06-14 */
17753 || (w->cursor.y + (make_cursor_line_fully_visible_p
17754 ? cursor_height + this_scroll_margin
17755 : 1)) > it.last_visible_y)
17756 {
17757 w->cursor.vpos = -1;
17758 clear_glyph_matrix (w->desired_matrix);
17759 return -1;
17760 }
17761 }
17762
17763 /* Scroll the display. Do it before changing the current matrix so
17764 that xterm.c doesn't get confused about where the cursor glyph is
17765 found. */
17766 if (dy && run.height)
17767 {
17768 update_begin (f);
17769
17770 if (FRAME_WINDOW_P (f))
17771 {
17772 FRAME_RIF (f)->update_window_begin_hook (w);
17773 FRAME_RIF (f)->clear_window_mouse_face (w);
17774 FRAME_RIF (f)->scroll_run_hook (w, &run);
17775 FRAME_RIF (f)->update_window_end_hook (w, 0, 0);
17776 }
17777 else
17778 {
17779 /* Terminal frame. In this case, dvpos gives the number of
17780 lines to scroll by; dvpos < 0 means scroll up. */
17781 int from_vpos
17782 = MATRIX_ROW_VPOS (first_unchanged_at_end_row, w->current_matrix);
17783 int from = WINDOW_TOP_EDGE_LINE (w) + from_vpos;
17784 int end = (WINDOW_TOP_EDGE_LINE (w)
17785 + (WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0)
17786 + window_internal_height (w));
17787
17788 #if defined (HAVE_GPM) || defined (MSDOS)
17789 x_clear_window_mouse_face (w);
17790 #endif
17791 /* Perform the operation on the screen. */
17792 if (dvpos > 0)
17793 {
17794 /* Scroll last_unchanged_at_beg_row to the end of the
17795 window down dvpos lines. */
17796 set_terminal_window (f, end);
17797
17798 /* On dumb terminals delete dvpos lines at the end
17799 before inserting dvpos empty lines. */
17800 if (!FRAME_SCROLL_REGION_OK (f))
17801 ins_del_lines (f, end - dvpos, -dvpos);
17802
17803 /* Insert dvpos empty lines in front of
17804 last_unchanged_at_beg_row. */
17805 ins_del_lines (f, from, dvpos);
17806 }
17807 else if (dvpos < 0)
17808 {
17809 /* Scroll up last_unchanged_at_beg_vpos to the end of
17810 the window to last_unchanged_at_beg_vpos - |dvpos|. */
17811 set_terminal_window (f, end);
17812
17813 /* Delete dvpos lines in front of
17814 last_unchanged_at_beg_vpos. ins_del_lines will set
17815 the cursor to the given vpos and emit |dvpos| delete
17816 line sequences. */
17817 ins_del_lines (f, from + dvpos, dvpos);
17818
17819 /* On a dumb terminal insert dvpos empty lines at the
17820 end. */
17821 if (!FRAME_SCROLL_REGION_OK (f))
17822 ins_del_lines (f, end + dvpos, -dvpos);
17823 }
17824
17825 set_terminal_window (f, 0);
17826 }
17827
17828 update_end (f);
17829 }
17830
17831 /* Shift reused rows of the current matrix to the right position.
17832 BOTTOM_ROW is the last + 1 row in the current matrix reserved for
17833 text. */
17834 bottom_row = MATRIX_BOTTOM_TEXT_ROW (current_matrix, w);
17835 bottom_vpos = MATRIX_ROW_VPOS (bottom_row, current_matrix);
17836 if (dvpos < 0)
17837 {
17838 rotate_matrix (current_matrix, first_unchanged_at_end_vpos + dvpos,
17839 bottom_vpos, dvpos);
17840 clear_glyph_matrix_rows (current_matrix, bottom_vpos + dvpos,
17841 bottom_vpos);
17842 }
17843 else if (dvpos > 0)
17844 {
17845 rotate_matrix (current_matrix, first_unchanged_at_end_vpos,
17846 bottom_vpos, dvpos);
17847 clear_glyph_matrix_rows (current_matrix, first_unchanged_at_end_vpos,
17848 first_unchanged_at_end_vpos + dvpos);
17849 }
17850
17851 /* For frame-based redisplay, make sure that current frame and window
17852 matrix are in sync with respect to glyph memory. */
17853 if (!FRAME_WINDOW_P (f))
17854 sync_frame_with_window_matrix_rows (w);
17855
17856 /* Adjust buffer positions in reused rows. */
17857 if (delta || delta_bytes)
17858 increment_matrix_positions (current_matrix,
17859 first_unchanged_at_end_vpos + dvpos,
17860 bottom_vpos, delta, delta_bytes);
17861
17862 /* Adjust Y positions. */
17863 if (dy)
17864 shift_glyph_matrix (w, current_matrix,
17865 first_unchanged_at_end_vpos + dvpos,
17866 bottom_vpos, dy);
17867
17868 if (first_unchanged_at_end_row)
17869 {
17870 first_unchanged_at_end_row += dvpos;
17871 if (first_unchanged_at_end_row->y >= it.last_visible_y
17872 || !MATRIX_ROW_DISPLAYS_TEXT_P (first_unchanged_at_end_row))
17873 first_unchanged_at_end_row = NULL;
17874 }
17875
17876 /* If scrolling up, there may be some lines to display at the end of
17877 the window. */
17878 last_text_row_at_end = NULL;
17879 if (dy < 0)
17880 {
17881 /* Scrolling up can leave for example a partially visible line
17882 at the end of the window to be redisplayed. */
17883 /* Set last_row to the glyph row in the current matrix where the
17884 window end line is found. It has been moved up or down in
17885 the matrix by dvpos. */
17886 int last_vpos = XFASTINT (w->window_end_vpos) + dvpos;
17887 struct glyph_row *last_row = MATRIX_ROW (current_matrix, last_vpos);
17888
17889 /* If last_row is the window end line, it should display text. */
17890 eassert (last_row->displays_text_p);
17891
17892 /* If window end line was partially visible before, begin
17893 displaying at that line. Otherwise begin displaying with the
17894 line following it. */
17895 if (MATRIX_ROW_BOTTOM_Y (last_row) - dy >= it.last_visible_y)
17896 {
17897 init_to_row_start (&it, w, last_row);
17898 it.vpos = last_vpos;
17899 it.current_y = last_row->y;
17900 }
17901 else
17902 {
17903 init_to_row_end (&it, w, last_row);
17904 it.vpos = 1 + last_vpos;
17905 it.current_y = MATRIX_ROW_BOTTOM_Y (last_row);
17906 ++last_row;
17907 }
17908
17909 /* We may start in a continuation line. If so, we have to
17910 get the right continuation_lines_width and current_x. */
17911 it.continuation_lines_width = last_row->continuation_lines_width;
17912 it.hpos = it.current_x = 0;
17913
17914 /* Display the rest of the lines at the window end. */
17915 it.glyph_row = MATRIX_ROW (desired_matrix, it.vpos);
17916 while (it.current_y < it.last_visible_y
17917 && !fonts_changed_p)
17918 {
17919 /* Is it always sure that the display agrees with lines in
17920 the current matrix? I don't think so, so we mark rows
17921 displayed invalid in the current matrix by setting their
17922 enabled_p flag to zero. */
17923 MATRIX_ROW (w->current_matrix, it.vpos)->enabled_p = 0;
17924 if (display_line (&it))
17925 last_text_row_at_end = it.glyph_row - 1;
17926 }
17927 }
17928
17929 /* Update window_end_pos and window_end_vpos. */
17930 if (first_unchanged_at_end_row
17931 && !last_text_row_at_end)
17932 {
17933 /* Window end line if one of the preserved rows from the current
17934 matrix. Set row to the last row displaying text in current
17935 matrix starting at first_unchanged_at_end_row, after
17936 scrolling. */
17937 eassert (first_unchanged_at_end_row->displays_text_p);
17938 row = find_last_row_displaying_text (w->current_matrix, &it,
17939 first_unchanged_at_end_row);
17940 eassert (row && MATRIX_ROW_DISPLAYS_TEXT_P (row));
17941
17942 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
17943 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
17944 wset_window_end_vpos
17945 (w, make_number (MATRIX_ROW_VPOS (row, w->current_matrix)));
17946 eassert (w->window_end_bytepos >= 0);
17947 IF_DEBUG (debug_method_add (w, "A"));
17948 }
17949 else if (last_text_row_at_end)
17950 {
17951 wset_window_end_pos
17952 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row_at_end)));
17953 w->window_end_bytepos
17954 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row_at_end);
17955 wset_window_end_vpos
17956 (w, make_number (MATRIX_ROW_VPOS (last_text_row_at_end,
17957 desired_matrix)));
17958 eassert (w->window_end_bytepos >= 0);
17959 IF_DEBUG (debug_method_add (w, "B"));
17960 }
17961 else if (last_text_row)
17962 {
17963 /* We have displayed either to the end of the window or at the
17964 end of the window, i.e. the last row with text is to be found
17965 in the desired matrix. */
17966 wset_window_end_pos
17967 (w, make_number (Z - MATRIX_ROW_END_CHARPOS (last_text_row)));
17968 w->window_end_bytepos
17969 = Z_BYTE - MATRIX_ROW_END_BYTEPOS (last_text_row);
17970 wset_window_end_vpos
17971 (w, make_number (MATRIX_ROW_VPOS (last_text_row, desired_matrix)));
17972 eassert (w->window_end_bytepos >= 0);
17973 }
17974 else if (first_unchanged_at_end_row == NULL
17975 && last_text_row == NULL
17976 && last_text_row_at_end == NULL)
17977 {
17978 /* Displayed to end of window, but no line containing text was
17979 displayed. Lines were deleted at the end of the window. */
17980 int first_vpos = WINDOW_WANTS_HEADER_LINE_P (w) ? 1 : 0;
17981 int vpos = XFASTINT (w->window_end_vpos);
17982 struct glyph_row *current_row = current_matrix->rows + vpos;
17983 struct glyph_row *desired_row = desired_matrix->rows + vpos;
17984
17985 for (row = NULL;
17986 row == NULL && vpos >= first_vpos;
17987 --vpos, --current_row, --desired_row)
17988 {
17989 if (desired_row->enabled_p)
17990 {
17991 if (desired_row->displays_text_p)
17992 row = desired_row;
17993 }
17994 else if (current_row->displays_text_p)
17995 row = current_row;
17996 }
17997
17998 eassert (row != NULL);
17999 wset_window_end_vpos (w, make_number (vpos + 1));
18000 wset_window_end_pos (w, make_number (Z - MATRIX_ROW_END_CHARPOS (row)));
18001 w->window_end_bytepos = Z_BYTE - MATRIX_ROW_END_BYTEPOS (row);
18002 eassert (w->window_end_bytepos >= 0);
18003 IF_DEBUG (debug_method_add (w, "C"));
18004 }
18005 else
18006 emacs_abort ();
18007
18008 IF_DEBUG (debug_end_pos = XFASTINT (w->window_end_pos);
18009 debug_end_vpos = XFASTINT (w->window_end_vpos));
18010
18011 /* Record that display has not been completed. */
18012 wset_window_end_valid (w, Qnil);
18013 w->desired_matrix->no_scrolling_p = 1;
18014 return 3;
18015
18016 #undef GIVE_UP
18017 }
18018
18019
18020 \f
18021 /***********************************************************************
18022 More debugging support
18023 ***********************************************************************/
18024
18025 #ifdef GLYPH_DEBUG
18026
18027 void dump_glyph_row (struct glyph_row *, int, int) EXTERNALLY_VISIBLE;
18028 void dump_glyph_matrix (struct glyph_matrix *, int) EXTERNALLY_VISIBLE;
18029 void dump_glyph (struct glyph_row *, struct glyph *, int) EXTERNALLY_VISIBLE;
18030
18031
18032 /* Dump the contents of glyph matrix MATRIX on stderr.
18033
18034 GLYPHS 0 means don't show glyph contents.
18035 GLYPHS 1 means show glyphs in short form
18036 GLYPHS > 1 means show glyphs in long form. */
18037
18038 void
18039 dump_glyph_matrix (struct glyph_matrix *matrix, int glyphs)
18040 {
18041 int i;
18042 for (i = 0; i < matrix->nrows; ++i)
18043 dump_glyph_row (MATRIX_ROW (matrix, i), i, glyphs);
18044 }
18045
18046
18047 /* Dump contents of glyph GLYPH to stderr. ROW and AREA are
18048 the glyph row and area where the glyph comes from. */
18049
18050 void
18051 dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
18052 {
18053 if (glyph->type == CHAR_GLYPH)
18054 {
18055 fprintf (stderr,
18056 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18057 glyph - row->glyphs[TEXT_AREA],
18058 'C',
18059 glyph->charpos,
18060 (BUFFERP (glyph->object)
18061 ? 'B'
18062 : (STRINGP (glyph->object)
18063 ? 'S'
18064 : '-')),
18065 glyph->pixel_width,
18066 glyph->u.ch,
18067 (glyph->u.ch < 0x80 && glyph->u.ch >= ' '
18068 ? glyph->u.ch
18069 : '.'),
18070 glyph->face_id,
18071 glyph->left_box_line_p,
18072 glyph->right_box_line_p);
18073 }
18074 else if (glyph->type == STRETCH_GLYPH)
18075 {
18076 fprintf (stderr,
18077 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18078 glyph - row->glyphs[TEXT_AREA],
18079 'S',
18080 glyph->charpos,
18081 (BUFFERP (glyph->object)
18082 ? 'B'
18083 : (STRINGP (glyph->object)
18084 ? 'S'
18085 : '-')),
18086 glyph->pixel_width,
18087 0,
18088 '.',
18089 glyph->face_id,
18090 glyph->left_box_line_p,
18091 glyph->right_box_line_p);
18092 }
18093 else if (glyph->type == IMAGE_GLYPH)
18094 {
18095 fprintf (stderr,
18096 " %5td %4c %6"pI"d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18097 glyph - row->glyphs[TEXT_AREA],
18098 'I',
18099 glyph->charpos,
18100 (BUFFERP (glyph->object)
18101 ? 'B'
18102 : (STRINGP (glyph->object)
18103 ? 'S'
18104 : '-')),
18105 glyph->pixel_width,
18106 glyph->u.img_id,
18107 '.',
18108 glyph->face_id,
18109 glyph->left_box_line_p,
18110 glyph->right_box_line_p);
18111 }
18112 else if (glyph->type == COMPOSITE_GLYPH)
18113 {
18114 fprintf (stderr,
18115 " %5td %4c %6"pI"d %c %3d 0x%05x",
18116 glyph - row->glyphs[TEXT_AREA],
18117 '+',
18118 glyph->charpos,
18119 (BUFFERP (glyph->object)
18120 ? 'B'
18121 : (STRINGP (glyph->object)
18122 ? 'S'
18123 : '-')),
18124 glyph->pixel_width,
18125 glyph->u.cmp.id);
18126 if (glyph->u.cmp.automatic)
18127 fprintf (stderr,
18128 "[%d-%d]",
18129 glyph->slice.cmp.from, glyph->slice.cmp.to);
18130 fprintf (stderr, " . %4d %1.1d%1.1d\n",
18131 glyph->face_id,
18132 glyph->left_box_line_p,
18133 glyph->right_box_line_p);
18134 }
18135 #ifdef HAVE_XWIDGETS
18136 else if (glyph->type == XWIDGET_GLYPH)
18137 {
18138 fprintf (stderr,
18139 " %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
18140 glyph - row->glyphs[TEXT_AREA],
18141 'X',
18142 glyph->charpos,
18143 (BUFFERP (glyph->object)
18144 ? 'B'
18145 : (STRINGP (glyph->object)
18146 ? 'S'
18147 : '-')),
18148 glyph->pixel_width,
18149 glyph->u.xwidget,
18150 '.',
18151 glyph->face_id,
18152 glyph->left_box_line_p,
18153 glyph->right_box_line_p);
18154
18155 // printf("dump xwidget glyph\n");
18156 }
18157 #endif
18158 }
18159
18160
18161 /* Dump the contents of glyph row at VPOS in MATRIX to stderr.
18162 GLYPHS 0 means don't show glyph contents.
18163 GLYPHS 1 means show glyphs in short form
18164 GLYPHS > 1 means show glyphs in long form. */
18165
18166 void
18167 dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
18168 {
18169 if (glyphs != 1)
18170 {
18171 fprintf (stderr, "Row Start End Used oE><\\CTZFesm X Y W H V A P\n");
18172 fprintf (stderr, "======================================================================\n");
18173
18174 fprintf (stderr, "%3d %5"pI"d %5"pI"d %4d %1.1d%1.1d%1.1d%1.1d\
18175 %1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d%1.1d %4d %4d %4d %4d %4d %4d %4d\n",
18176 vpos,
18177 MATRIX_ROW_START_CHARPOS (row),
18178 MATRIX_ROW_END_CHARPOS (row),
18179 row->used[TEXT_AREA],
18180 row->contains_overlapping_glyphs_p,
18181 row->enabled_p,
18182 row->truncated_on_left_p,
18183 row->truncated_on_right_p,
18184 row->continued_p,
18185 MATRIX_ROW_CONTINUATION_LINE_P (row),
18186 row->displays_text_p,
18187 row->ends_at_zv_p,
18188 row->fill_line_p,
18189 row->ends_in_middle_of_char_p,
18190 row->starts_in_middle_of_char_p,
18191 row->mouse_face_p,
18192 row->x,
18193 row->y,
18194 row->pixel_width,
18195 row->height,
18196 row->visible_height,
18197 row->ascent,
18198 row->phys_ascent);
18199 fprintf (stderr, "%9"pD"d %5"pD"d\t%5d\n", row->start.overlay_string_index,
18200 row->end.overlay_string_index,
18201 row->continuation_lines_width);
18202 fprintf (stderr, "%9"pI"d %5"pI"d\n",
18203 CHARPOS (row->start.string_pos),
18204 CHARPOS (row->end.string_pos));
18205 fprintf (stderr, "%9d %5d\n", row->start.dpvec_index,
18206 row->end.dpvec_index);
18207 }
18208
18209 if (glyphs > 1)
18210 {
18211 int area;
18212
18213 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18214 {
18215 struct glyph *glyph = row->glyphs[area];
18216 struct glyph *glyph_end = glyph + row->used[area];
18217
18218 /* Glyph for a line end in text. */
18219 if (area == TEXT_AREA && glyph == glyph_end && glyph->charpos > 0)
18220 ++glyph_end;
18221
18222 if (glyph < glyph_end)
18223 fprintf (stderr, " Glyph Type Pos O W Code C Face LR\n");
18224
18225 for (; glyph < glyph_end; ++glyph)
18226 dump_glyph (row, glyph, area);
18227 }
18228 }
18229 else if (glyphs == 1)
18230 {
18231 int area;
18232
18233 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18234 {
18235 char *s = alloca (row->used[area] + 1);
18236 int i;
18237
18238 for (i = 0; i < row->used[area]; ++i)
18239 {
18240 struct glyph *glyph = row->glyphs[area] + i;
18241 if (glyph->type == CHAR_GLYPH
18242 && glyph->u.ch < 0x80
18243 && glyph->u.ch >= ' ')
18244 s[i] = glyph->u.ch;
18245 else
18246 s[i] = '.';
18247 }
18248
18249 s[i] = '\0';
18250 fprintf (stderr, "%3d: (%d) '%s'\n", vpos, row->enabled_p, s);
18251 }
18252 }
18253 }
18254
18255
18256 DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
18257 Sdump_glyph_matrix, 0, 1, "p",
18258 doc: /* Dump the current matrix of the selected window to stderr.
18259 Shows contents of glyph row structures. With non-nil
18260 parameter GLYPHS, dump glyphs as well. If GLYPHS is 1 show
18261 glyphs in short form, otherwise show glyphs in long form. */)
18262 (Lisp_Object glyphs)
18263 {
18264 struct window *w = XWINDOW (selected_window);
18265 struct buffer *buffer = XBUFFER (w->buffer);
18266
18267 fprintf (stderr, "PT = %"pI"d, BEGV = %"pI"d. ZV = %"pI"d\n",
18268 BUF_PT (buffer), BUF_BEGV (buffer), BUF_ZV (buffer));
18269 fprintf (stderr, "Cursor x = %d, y = %d, hpos = %d, vpos = %d\n",
18270 w->cursor.x, w->cursor.y, w->cursor.hpos, w->cursor.vpos);
18271 fprintf (stderr, "=============================================\n");
18272 dump_glyph_matrix (w->current_matrix,
18273 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 0);
18274 return Qnil;
18275 }
18276
18277
18278 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
18279 Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
18280 (void)
18281 {
18282 struct frame *f = XFRAME (selected_frame);
18283 dump_glyph_matrix (f->current_matrix, 1);
18284 return Qnil;
18285 }
18286
18287
18288 DEFUN ("dump-glyph-row", Fdump_glyph_row, Sdump_glyph_row, 1, 2, "",
18289 doc: /* Dump glyph row ROW to stderr.
18290 GLYPH 0 means don't dump glyphs.
18291 GLYPH 1 means dump glyphs in short form.
18292 GLYPH > 1 or omitted means dump glyphs in long form. */)
18293 (Lisp_Object row, Lisp_Object glyphs)
18294 {
18295 struct glyph_matrix *matrix;
18296 EMACS_INT vpos;
18297
18298 CHECK_NUMBER (row);
18299 matrix = XWINDOW (selected_window)->current_matrix;
18300 vpos = XINT (row);
18301 if (vpos >= 0 && vpos < matrix->nrows)
18302 dump_glyph_row (MATRIX_ROW (matrix, vpos),
18303 vpos,
18304 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18305 return Qnil;
18306 }
18307
18308
18309 DEFUN ("dump-tool-bar-row", Fdump_tool_bar_row, Sdump_tool_bar_row, 1, 2, "",
18310 doc: /* Dump glyph row ROW of the tool-bar of the current frame to stderr.
18311 GLYPH 0 means don't dump glyphs.
18312 GLYPH 1 means dump glyphs in short form.
18313 GLYPH > 1 or omitted means dump glyphs in long form. */)
18314 (Lisp_Object row, Lisp_Object glyphs)
18315 {
18316 struct frame *sf = SELECTED_FRAME ();
18317 struct glyph_matrix *m = XWINDOW (sf->tool_bar_window)->current_matrix;
18318 EMACS_INT vpos;
18319
18320 CHECK_NUMBER (row);
18321 vpos = XINT (row);
18322 if (vpos >= 0 && vpos < m->nrows)
18323 dump_glyph_row (MATRIX_ROW (m, vpos), vpos,
18324 TYPE_RANGED_INTEGERP (int, glyphs) ? XINT (glyphs) : 2);
18325 return Qnil;
18326 }
18327
18328
18329 DEFUN ("trace-redisplay", Ftrace_redisplay, Strace_redisplay, 0, 1, "P",
18330 doc: /* Toggle tracing of redisplay.
18331 With ARG, turn tracing on if and only if ARG is positive. */)
18332 (Lisp_Object arg)
18333 {
18334 if (NILP (arg))
18335 trace_redisplay_p = !trace_redisplay_p;
18336 else
18337 {
18338 arg = Fprefix_numeric_value (arg);
18339 trace_redisplay_p = XINT (arg) > 0;
18340 }
18341
18342 return Qnil;
18343 }
18344
18345
18346 DEFUN ("trace-to-stderr", Ftrace_to_stderr, Strace_to_stderr, 1, MANY, "",
18347 doc: /* Like `format', but print result to stderr.
18348 usage: (trace-to-stderr STRING &rest OBJECTS) */)
18349 (ptrdiff_t nargs, Lisp_Object *args)
18350 {
18351 Lisp_Object s = Fformat (nargs, args);
18352 fprintf (stderr, "%s", SDATA (s));
18353 return Qnil;
18354 }
18355
18356 #endif /* GLYPH_DEBUG */
18357
18358
18359 \f
18360 /***********************************************************************
18361 Building Desired Matrix Rows
18362 ***********************************************************************/
18363
18364 /* Return a temporary glyph row holding the glyphs of an overlay arrow.
18365 Used for non-window-redisplay windows, and for windows w/o left fringe. */
18366
18367 static struct glyph_row *
18368 get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
18369 {
18370 struct frame *f = XFRAME (WINDOW_FRAME (w));
18371 struct buffer *buffer = XBUFFER (w->buffer);
18372 struct buffer *old = current_buffer;
18373 const unsigned char *arrow_string = SDATA (overlay_arrow_string);
18374 int arrow_len = SCHARS (overlay_arrow_string);
18375 const unsigned char *arrow_end = arrow_string + arrow_len;
18376 const unsigned char *p;
18377 struct it it;
18378 int multibyte_p;
18379 int n_glyphs_before;
18380
18381 set_buffer_temp (buffer);
18382 init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
18383 it.glyph_row->used[TEXT_AREA] = 0;
18384 SET_TEXT_POS (it.position, 0, 0);
18385
18386 multibyte_p = !NILP (BVAR (buffer, enable_multibyte_characters));
18387 p = arrow_string;
18388 while (p < arrow_end)
18389 {
18390 Lisp_Object face, ilisp;
18391
18392 /* Get the next character. */
18393 if (multibyte_p)
18394 it.c = it.char_to_display = string_char_and_length (p, &it.len);
18395 else
18396 {
18397 it.c = it.char_to_display = *p, it.len = 1;
18398 if (! ASCII_CHAR_P (it.c))
18399 it.char_to_display = BYTE8_TO_CHAR (it.c);
18400 }
18401 p += it.len;
18402
18403 /* Get its face. */
18404 ilisp = make_number (p - arrow_string);
18405 face = Fget_text_property (ilisp, Qface, overlay_arrow_string);
18406 it.face_id = compute_char_face (f, it.char_to_display, face);
18407
18408 /* Compute its width, get its glyphs. */
18409 n_glyphs_before = it.glyph_row->used[TEXT_AREA];
18410 SET_TEXT_POS (it.position, -1, -1);
18411 PRODUCE_GLYPHS (&it);
18412
18413 /* If this character doesn't fit any more in the line, we have
18414 to remove some glyphs. */
18415 if (it.current_x > it.last_visible_x)
18416 {
18417 it.glyph_row->used[TEXT_AREA] = n_glyphs_before;
18418 break;
18419 }
18420 }
18421
18422 set_buffer_temp (old);
18423 return it.glyph_row;
18424 }
18425
18426
18427 /* Insert truncation glyphs at the start of IT->glyph_row. Which
18428 glyphs to insert is determined by produce_special_glyphs. */
18429
18430 static void
18431 insert_left_trunc_glyphs (struct it *it)
18432 {
18433 struct it truncate_it;
18434 struct glyph *from, *end, *to, *toend;
18435
18436 eassert (!FRAME_WINDOW_P (it->f)
18437 || (!it->glyph_row->reversed_p
18438 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
18439 || (it->glyph_row->reversed_p
18440 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0));
18441
18442 /* Get the truncation glyphs. */
18443 truncate_it = *it;
18444 truncate_it.current_x = 0;
18445 truncate_it.face_id = DEFAULT_FACE_ID;
18446 truncate_it.glyph_row = &scratch_glyph_row;
18447 truncate_it.glyph_row->used[TEXT_AREA] = 0;
18448 CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
18449 truncate_it.object = make_number (0);
18450 produce_special_glyphs (&truncate_it, IT_TRUNCATION);
18451
18452 /* Overwrite glyphs from IT with truncation glyphs. */
18453 if (!it->glyph_row->reversed_p)
18454 {
18455 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18456
18457 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18458 end = from + tused;
18459 to = it->glyph_row->glyphs[TEXT_AREA];
18460 toend = to + it->glyph_row->used[TEXT_AREA];
18461 if (FRAME_WINDOW_P (it->f))
18462 {
18463 /* On GUI frames, when variable-size fonts are displayed,
18464 the truncation glyphs may need more pixels than the row's
18465 glyphs they overwrite. We overwrite more glyphs to free
18466 enough screen real estate, and enlarge the stretch glyph
18467 on the right (see display_line), if there is one, to
18468 preserve the screen position of the truncation glyphs on
18469 the right. */
18470 int w = 0;
18471 struct glyph *g = to;
18472 short used;
18473
18474 /* The first glyph could be partially visible, in which case
18475 it->glyph_row->x will be negative. But we want the left
18476 truncation glyphs to be aligned at the left margin of the
18477 window, so we override the x coordinate at which the row
18478 will begin. */
18479 it->glyph_row->x = 0;
18480 while (g < toend && w < it->truncation_pixel_width)
18481 {
18482 w += g->pixel_width;
18483 ++g;
18484 }
18485 if (g - to - tused > 0)
18486 {
18487 memmove (to + tused, g, (toend - g) * sizeof(*g));
18488 it->glyph_row->used[TEXT_AREA] -= g - to - tused;
18489 }
18490 used = it->glyph_row->used[TEXT_AREA];
18491 if (it->glyph_row->truncated_on_right_p
18492 && WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
18493 && it->glyph_row->glyphs[TEXT_AREA][used - 2].type
18494 == STRETCH_GLYPH)
18495 {
18496 int extra = w - it->truncation_pixel_width;
18497
18498 it->glyph_row->glyphs[TEXT_AREA][used - 2].pixel_width += extra;
18499 }
18500 }
18501
18502 while (from < end)
18503 *to++ = *from++;
18504
18505 /* There may be padding glyphs left over. Overwrite them too. */
18506 if (!FRAME_WINDOW_P (it->f))
18507 {
18508 while (to < toend && CHAR_GLYPH_PADDING_P (*to))
18509 {
18510 from = truncate_it.glyph_row->glyphs[TEXT_AREA];
18511 while (from < end)
18512 *to++ = *from++;
18513 }
18514 }
18515
18516 if (to > toend)
18517 it->glyph_row->used[TEXT_AREA] = to - it->glyph_row->glyphs[TEXT_AREA];
18518 }
18519 else
18520 {
18521 short tused = truncate_it.glyph_row->used[TEXT_AREA];
18522
18523 /* In R2L rows, overwrite the last (rightmost) glyphs, and do
18524 that back to front. */
18525 end = truncate_it.glyph_row->glyphs[TEXT_AREA];
18526 from = end + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18527 toend = it->glyph_row->glyphs[TEXT_AREA];
18528 to = toend + it->glyph_row->used[TEXT_AREA] - 1;
18529 if (FRAME_WINDOW_P (it->f))
18530 {
18531 int w = 0;
18532 struct glyph *g = to;
18533
18534 while (g >= toend && w < it->truncation_pixel_width)
18535 {
18536 w += g->pixel_width;
18537 --g;
18538 }
18539 if (to - g - tused > 0)
18540 to = g + tused;
18541 if (it->glyph_row->truncated_on_right_p
18542 && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
18543 && it->glyph_row->glyphs[TEXT_AREA][1].type == STRETCH_GLYPH)
18544 {
18545 int extra = w - it->truncation_pixel_width;
18546
18547 it->glyph_row->glyphs[TEXT_AREA][1].pixel_width += extra;
18548 }
18549 }
18550
18551 while (from >= end && to >= toend)
18552 *to-- = *from--;
18553 if (!FRAME_WINDOW_P (it->f))
18554 {
18555 while (to >= toend && CHAR_GLYPH_PADDING_P (*to))
18556 {
18557 from =
18558 truncate_it.glyph_row->glyphs[TEXT_AREA]
18559 + truncate_it.glyph_row->used[TEXT_AREA] - 1;
18560 while (from >= end && to >= toend)
18561 *to-- = *from--;
18562 }
18563 }
18564 if (from >= end)
18565 {
18566 /* Need to free some room before prepending additional
18567 glyphs. */
18568 int move_by = from - end + 1;
18569 struct glyph *g0 = it->glyph_row->glyphs[TEXT_AREA];
18570 struct glyph *g = g0 + it->glyph_row->used[TEXT_AREA] - 1;
18571
18572 for ( ; g >= g0; g--)
18573 g[move_by] = *g;
18574 while (from >= end)
18575 *to-- = *from--;
18576 it->glyph_row->used[TEXT_AREA] += move_by;
18577 }
18578 }
18579 }
18580
18581 /* Compute the hash code for ROW. */
18582 unsigned
18583 row_hash (struct glyph_row *row)
18584 {
18585 int area, k;
18586 unsigned hashval = 0;
18587
18588 for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
18589 for (k = 0; k < row->used[area]; ++k)
18590 hashval = ((((hashval << 4) + (hashval >> 24)) & 0x0fffffff)
18591 + row->glyphs[area][k].u.val
18592 + row->glyphs[area][k].face_id
18593 + row->glyphs[area][k].padding_p
18594 + (row->glyphs[area][k].type << 2));
18595
18596 return hashval;
18597 }
18598
18599 /* Compute the pixel height and width of IT->glyph_row.
18600
18601 Most of the time, ascent and height of a display line will be equal
18602 to the max_ascent and max_height values of the display iterator
18603 structure. This is not the case if
18604
18605 1. We hit ZV without displaying anything. In this case, max_ascent
18606 and max_height will be zero.
18607
18608 2. We have some glyphs that don't contribute to the line height.
18609 (The glyph row flag contributes_to_line_height_p is for future
18610 pixmap extensions).
18611
18612 The first case is easily covered by using default values because in
18613 these cases, the line height does not really matter, except that it
18614 must not be zero. */
18615
18616 static void
18617 compute_line_metrics (struct it *it)
18618 {
18619 struct glyph_row *row = it->glyph_row;
18620
18621 if (FRAME_WINDOW_P (it->f))
18622 {
18623 int i, min_y, max_y;
18624
18625 /* The line may consist of one space only, that was added to
18626 place the cursor on it. If so, the row's height hasn't been
18627 computed yet. */
18628 if (row->height == 0)
18629 {
18630 if (it->max_ascent + it->max_descent == 0)
18631 it->max_descent = it->max_phys_descent = FRAME_LINE_HEIGHT (it->f);
18632 row->ascent = it->max_ascent;
18633 row->height = it->max_ascent + it->max_descent;
18634 row->phys_ascent = it->max_phys_ascent;
18635 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
18636 row->extra_line_spacing = it->max_extra_line_spacing;
18637 }
18638
18639 /* Compute the width of this line. */
18640 row->pixel_width = row->x;
18641 for (i = 0; i < row->used[TEXT_AREA]; ++i)
18642 row->pixel_width += row->glyphs[TEXT_AREA][i].pixel_width;
18643
18644 eassert (row->pixel_width >= 0);
18645 eassert (row->ascent >= 0 && row->height > 0);
18646
18647 row->overlapping_p = (MATRIX_ROW_OVERLAPS_SUCC_P (row)
18648 || MATRIX_ROW_OVERLAPS_PRED_P (row));
18649
18650 /* If first line's physical ascent is larger than its logical
18651 ascent, use the physical ascent, and make the row taller.
18652 This makes accented characters fully visible. */
18653 if (row == MATRIX_FIRST_TEXT_ROW (it->w->desired_matrix)
18654 && row->phys_ascent > row->ascent)
18655 {
18656 row->height += row->phys_ascent - row->ascent;
18657 row->ascent = row->phys_ascent;
18658 }
18659
18660 /* Compute how much of the line is visible. */
18661 row->visible_height = row->height;
18662
18663 min_y = WINDOW_HEADER_LINE_HEIGHT (it->w);
18664 max_y = WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w);
18665
18666 if (row->y < min_y)
18667 row->visible_height -= min_y - row->y;
18668 if (row->y + row->height > max_y)
18669 row->visible_height -= row->y + row->height - max_y;
18670 }
18671 else
18672 {
18673 row->pixel_width = row->used[TEXT_AREA];
18674 if (row->continued_p)
18675 row->pixel_width -= it->continuation_pixel_width;
18676 else if (row->truncated_on_right_p)
18677 row->pixel_width -= it->truncation_pixel_width;
18678 row->ascent = row->phys_ascent = 0;
18679 row->height = row->phys_height = row->visible_height = 1;
18680 row->extra_line_spacing = 0;
18681 }
18682
18683 /* Compute a hash code for this row. */
18684 row->hash = row_hash (row);
18685
18686 it->max_ascent = it->max_descent = 0;
18687 it->max_phys_ascent = it->max_phys_descent = 0;
18688 }
18689
18690
18691 /* Append one space to the glyph row of iterator IT if doing a
18692 window-based redisplay. The space has the same face as
18693 IT->face_id. Value is non-zero if a space was added.
18694
18695 This function is called to make sure that there is always one glyph
18696 at the end of a glyph row that the cursor can be set on under
18697 window-systems. (If there weren't such a glyph we would not know
18698 how wide and tall a box cursor should be displayed).
18699
18700 At the same time this space let's a nicely handle clearing to the
18701 end of the line if the row ends in italic text. */
18702
18703 static int
18704 append_space_for_newline (struct it *it, int default_face_p)
18705 {
18706 if (FRAME_WINDOW_P (it->f))
18707 {
18708 int n = it->glyph_row->used[TEXT_AREA];
18709
18710 if (it->glyph_row->glyphs[TEXT_AREA] + n
18711 < it->glyph_row->glyphs[1 + TEXT_AREA])
18712 {
18713 /* Save some values that must not be changed.
18714 Must save IT->c and IT->len because otherwise
18715 ITERATOR_AT_END_P wouldn't work anymore after
18716 append_space_for_newline has been called. */
18717 enum display_element_type saved_what = it->what;
18718 int saved_c = it->c, saved_len = it->len;
18719 int saved_char_to_display = it->char_to_display;
18720 int saved_x = it->current_x;
18721 int saved_face_id = it->face_id;
18722 struct text_pos saved_pos;
18723 Lisp_Object saved_object;
18724 struct face *face;
18725
18726 saved_object = it->object;
18727 saved_pos = it->position;
18728
18729 it->what = IT_CHARACTER;
18730 memset (&it->position, 0, sizeof it->position);
18731 it->object = make_number (0);
18732 it->c = it->char_to_display = ' ';
18733 it->len = 1;
18734
18735 /* If the default face was remapped, be sure to use the
18736 remapped face for the appended newline. */
18737 if (default_face_p)
18738 it->face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
18739 else if (it->face_before_selective_p)
18740 it->face_id = it->saved_face_id;
18741 face = FACE_FROM_ID (it->f, it->face_id);
18742 it->face_id = FACE_FOR_CHAR (it->f, face, 0, -1, Qnil);
18743
18744 PRODUCE_GLYPHS (it);
18745
18746 it->override_ascent = -1;
18747 it->constrain_row_ascent_descent_p = 0;
18748 it->current_x = saved_x;
18749 it->object = saved_object;
18750 it->position = saved_pos;
18751 it->what = saved_what;
18752 it->face_id = saved_face_id;
18753 it->len = saved_len;
18754 it->c = saved_c;
18755 it->char_to_display = saved_char_to_display;
18756 return 1;
18757 }
18758 }
18759
18760 return 0;
18761 }
18762
18763
18764 /* Extend the face of the last glyph in the text area of IT->glyph_row
18765 to the end of the display line. Called from display_line. If the
18766 glyph row is empty, add a space glyph to it so that we know the
18767 face to draw. Set the glyph row flag fill_line_p. If the glyph
18768 row is R2L, prepend a stretch glyph to cover the empty space to the
18769 left of the leftmost glyph. */
18770
18771 static void
18772 extend_face_to_end_of_line (struct it *it)
18773 {
18774 struct face *face, *default_face;
18775 struct frame *f = it->f;
18776
18777 /* If line is already filled, do nothing. Non window-system frames
18778 get a grace of one more ``pixel'' because their characters are
18779 1-``pixel'' wide, so they hit the equality too early. This grace
18780 is needed only for R2L rows that are not continued, to produce
18781 one extra blank where we could display the cursor. */
18782 if (it->current_x >= it->last_visible_x
18783 + (!FRAME_WINDOW_P (f)
18784 && it->glyph_row->reversed_p
18785 && !it->glyph_row->continued_p))
18786 return;
18787
18788 /* The default face, possibly remapped. */
18789 default_face = FACE_FROM_ID (f, lookup_basic_face (f, DEFAULT_FACE_ID));
18790
18791 /* Face extension extends the background and box of IT->face_id
18792 to the end of the line. If the background equals the background
18793 of the frame, we don't have to do anything. */
18794 if (it->face_before_selective_p)
18795 face = FACE_FROM_ID (f, it->saved_face_id);
18796 else
18797 face = FACE_FROM_ID (f, it->face_id);
18798
18799 if (FRAME_WINDOW_P (f)
18800 && it->glyph_row->displays_text_p
18801 && face->box == FACE_NO_BOX
18802 && face->background == FRAME_BACKGROUND_PIXEL (f)
18803 && !face->stipple
18804 && !it->glyph_row->reversed_p)
18805 return;
18806
18807 /* Set the glyph row flag indicating that the face of the last glyph
18808 in the text area has to be drawn to the end of the text area. */
18809 it->glyph_row->fill_line_p = 1;
18810
18811 /* If current character of IT is not ASCII, make sure we have the
18812 ASCII face. This will be automatically undone the next time
18813 get_next_display_element returns a multibyte character. Note
18814 that the character will always be single byte in unibyte
18815 text. */
18816 if (!ASCII_CHAR_P (it->c))
18817 {
18818 it->face_id = FACE_FOR_CHAR (f, face, 0, -1, Qnil);
18819 }
18820
18821 if (FRAME_WINDOW_P (f))
18822 {
18823 /* If the row is empty, add a space with the current face of IT,
18824 so that we know which face to draw. */
18825 if (it->glyph_row->used[TEXT_AREA] == 0)
18826 {
18827 it->glyph_row->glyphs[TEXT_AREA][0] = space_glyph;
18828 it->glyph_row->glyphs[TEXT_AREA][0].face_id = face->id;
18829 it->glyph_row->used[TEXT_AREA] = 1;
18830 }
18831 #ifdef HAVE_WINDOW_SYSTEM
18832 if (it->glyph_row->reversed_p)
18833 {
18834 /* Prepend a stretch glyph to the row, such that the
18835 rightmost glyph will be drawn flushed all the way to the
18836 right margin of the window. The stretch glyph that will
18837 occupy the empty space, if any, to the left of the
18838 glyphs. */
18839 struct font *font = face->font ? face->font : FRAME_FONT (f);
18840 struct glyph *row_start = it->glyph_row->glyphs[TEXT_AREA];
18841 struct glyph *row_end = row_start + it->glyph_row->used[TEXT_AREA];
18842 struct glyph *g;
18843 int row_width, stretch_ascent, stretch_width;
18844 struct text_pos saved_pos;
18845 int saved_face_id, saved_avoid_cursor;
18846
18847 for (row_width = 0, g = row_start; g < row_end; g++)
18848 row_width += g->pixel_width;
18849 stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
18850 if (stretch_width > 0)
18851 {
18852 stretch_ascent =
18853 (((it->ascent + it->descent)
18854 * FONT_BASE (font)) / FONT_HEIGHT (font));
18855 saved_pos = it->position;
18856 memset (&it->position, 0, sizeof it->position);
18857 saved_avoid_cursor = it->avoid_cursor_p;
18858 it->avoid_cursor_p = 1;
18859 saved_face_id = it->face_id;
18860 /* The last row's stretch glyph should get the default
18861 face, to avoid painting the rest of the window with
18862 the region face, if the region ends at ZV. */
18863 if (it->glyph_row->ends_at_zv_p)
18864 it->face_id = default_face->id;
18865 else
18866 it->face_id = face->id;
18867 append_stretch_glyph (it, make_number (0), stretch_width,
18868 it->ascent + it->descent, stretch_ascent);
18869 it->position = saved_pos;
18870 it->avoid_cursor_p = saved_avoid_cursor;
18871 it->face_id = saved_face_id;
18872 }
18873 }
18874 #endif /* HAVE_WINDOW_SYSTEM */
18875 }
18876 else
18877 {
18878 /* Save some values that must not be changed. */
18879 int saved_x = it->current_x;
18880 struct text_pos saved_pos;
18881 Lisp_Object saved_object;
18882 enum display_element_type saved_what = it->what;
18883 int saved_face_id = it->face_id;
18884
18885 saved_object = it->object;
18886 saved_pos = it->position;
18887
18888 it->what = IT_CHARACTER;
18889 memset (&it->position, 0, sizeof it->position);
18890 it->object = make_number (0);
18891 it->c = it->char_to_display = ' ';
18892 it->len = 1;
18893 /* The last row's blank glyphs should get the default face, to
18894 avoid painting the rest of the window with the region face,
18895 if the region ends at ZV. */
18896 if (it->glyph_row->ends_at_zv_p)
18897 it->face_id = default_face->id;
18898 else
18899 it->face_id = face->id;
18900
18901 PRODUCE_GLYPHS (it);
18902
18903 while (it->current_x <= it->last_visible_x)
18904 PRODUCE_GLYPHS (it);
18905
18906 /* Don't count these blanks really. It would let us insert a left
18907 truncation glyph below and make us set the cursor on them, maybe. */
18908 it->current_x = saved_x;
18909 it->object = saved_object;
18910 it->position = saved_pos;
18911 it->what = saved_what;
18912 it->face_id = saved_face_id;
18913 }
18914 }
18915
18916
18917 /* Value is non-zero if text starting at CHARPOS in current_buffer is
18918 trailing whitespace. */
18919
18920 static int
18921 trailing_whitespace_p (ptrdiff_t charpos)
18922 {
18923 ptrdiff_t bytepos = CHAR_TO_BYTE (charpos);
18924 int c = 0;
18925
18926 while (bytepos < ZV_BYTE
18927 && (c = FETCH_CHAR (bytepos),
18928 c == ' ' || c == '\t'))
18929 ++bytepos;
18930
18931 if (bytepos >= ZV_BYTE || c == '\n' || c == '\r')
18932 {
18933 if (bytepos != PT_BYTE)
18934 return 1;
18935 }
18936 return 0;
18937 }
18938
18939
18940 /* Highlight trailing whitespace, if any, in ROW. */
18941
18942 static void
18943 highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
18944 {
18945 int used = row->used[TEXT_AREA];
18946
18947 if (used)
18948 {
18949 struct glyph *start = row->glyphs[TEXT_AREA];
18950 struct glyph *glyph = start + used - 1;
18951
18952 if (row->reversed_p)
18953 {
18954 /* Right-to-left rows need to be processed in the opposite
18955 direction, so swap the edge pointers. */
18956 glyph = start;
18957 start = row->glyphs[TEXT_AREA] + used - 1;
18958 }
18959
18960 /* Skip over glyphs inserted to display the cursor at the
18961 end of a line, for extending the face of the last glyph
18962 to the end of the line on terminals, and for truncation
18963 and continuation glyphs. */
18964 if (!row->reversed_p)
18965 {
18966 while (glyph >= start
18967 && glyph->type == CHAR_GLYPH
18968 && INTEGERP (glyph->object))
18969 --glyph;
18970 }
18971 else
18972 {
18973 while (glyph <= start
18974 && glyph->type == CHAR_GLYPH
18975 && INTEGERP (glyph->object))
18976 ++glyph;
18977 }
18978
18979 /* If last glyph is a space or stretch, and it's trailing
18980 whitespace, set the face of all trailing whitespace glyphs in
18981 IT->glyph_row to `trailing-whitespace'. */
18982 if ((row->reversed_p ? glyph <= start : glyph >= start)
18983 && BUFFERP (glyph->object)
18984 && (glyph->type == STRETCH_GLYPH
18985 || (glyph->type == CHAR_GLYPH
18986 && glyph->u.ch == ' '))
18987 && trailing_whitespace_p (glyph->charpos))
18988 {
18989 int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
18990 if (face_id < 0)
18991 return;
18992
18993 if (!row->reversed_p)
18994 {
18995 while (glyph >= start
18996 && BUFFERP (glyph->object)
18997 && (glyph->type == STRETCH_GLYPH
18998 || (glyph->type == CHAR_GLYPH
18999 && glyph->u.ch == ' ')))
19000 (glyph--)->face_id = face_id;
19001 }
19002 else
19003 {
19004 while (glyph <= start
19005 && BUFFERP (glyph->object)
19006 && (glyph->type == STRETCH_GLYPH
19007 || (glyph->type == CHAR_GLYPH
19008 && glyph->u.ch == ' ')))
19009 (glyph++)->face_id = face_id;
19010 }
19011 }
19012 }
19013 }
19014
19015
19016 /* Value is non-zero if glyph row ROW should be
19017 used to hold the cursor. */
19018
19019 static int
19020 cursor_row_p (struct glyph_row *row)
19021 {
19022 int result = 1;
19023
19024 if (PT == CHARPOS (row->end.pos)
19025 || PT == MATRIX_ROW_END_CHARPOS (row))
19026 {
19027 /* Suppose the row ends on a string.
19028 Unless the row is continued, that means it ends on a newline
19029 in the string. If it's anything other than a display string
19030 (e.g., a before-string from an overlay), we don't want the
19031 cursor there. (This heuristic seems to give the optimal
19032 behavior for the various types of multi-line strings.)
19033 One exception: if the string has `cursor' property on one of
19034 its characters, we _do_ want the cursor there. */
19035 if (CHARPOS (row->end.string_pos) >= 0)
19036 {
19037 if (row->continued_p)
19038 result = 1;
19039 else
19040 {
19041 /* Check for `display' property. */
19042 struct glyph *beg = row->glyphs[TEXT_AREA];
19043 struct glyph *end = beg + row->used[TEXT_AREA] - 1;
19044 struct glyph *glyph;
19045
19046 result = 0;
19047 for (glyph = end; glyph >= beg; --glyph)
19048 if (STRINGP (glyph->object))
19049 {
19050 Lisp_Object prop
19051 = Fget_char_property (make_number (PT),
19052 Qdisplay, Qnil);
19053 result =
19054 (!NILP (prop)
19055 && display_prop_string_p (prop, glyph->object));
19056 /* If there's a `cursor' property on one of the
19057 string's characters, this row is a cursor row,
19058 even though this is not a display string. */
19059 if (!result)
19060 {
19061 Lisp_Object s = glyph->object;
19062
19063 for ( ; glyph >= beg && EQ (glyph->object, s); --glyph)
19064 {
19065 ptrdiff_t gpos = glyph->charpos;
19066
19067 if (!NILP (Fget_char_property (make_number (gpos),
19068 Qcursor, s)))
19069 {
19070 result = 1;
19071 break;
19072 }
19073 }
19074 }
19075 break;
19076 }
19077 }
19078 }
19079 else if (MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))
19080 {
19081 /* If the row ends in middle of a real character,
19082 and the line is continued, we want the cursor here.
19083 That's because CHARPOS (ROW->end.pos) would equal
19084 PT if PT is before the character. */
19085 if (!row->ends_in_ellipsis_p)
19086 result = row->continued_p;
19087 else
19088 /* If the row ends in an ellipsis, then
19089 CHARPOS (ROW->end.pos) will equal point after the
19090 invisible text. We want that position to be displayed
19091 after the ellipsis. */
19092 result = 0;
19093 }
19094 /* If the row ends at ZV, display the cursor at the end of that
19095 row instead of at the start of the row below. */
19096 else if (row->ends_at_zv_p)
19097 result = 1;
19098 else
19099 result = 0;
19100 }
19101
19102 return result;
19103 }
19104
19105 \f
19106
19107 /* Push the property PROP so that it will be rendered at the current
19108 position in IT. Return 1 if PROP was successfully pushed, 0
19109 otherwise. Called from handle_line_prefix to handle the
19110 `line-prefix' and `wrap-prefix' properties. */
19111
19112 static int
19113 push_prefix_prop (struct it *it, Lisp_Object prop)
19114 {
19115 struct text_pos pos =
19116 STRINGP (it->string) ? it->current.string_pos : it->current.pos;
19117
19118 eassert (it->method == GET_FROM_BUFFER
19119 || it->method == GET_FROM_DISPLAY_VECTOR
19120 || it->method == GET_FROM_STRING);
19121
19122 /* We need to save the current buffer/string position, so it will be
19123 restored by pop_it, because iterate_out_of_display_property
19124 depends on that being set correctly, but some situations leave
19125 it->position not yet set when this function is called. */
19126 push_it (it, &pos);
19127
19128 if (STRINGP (prop))
19129 {
19130 if (SCHARS (prop) == 0)
19131 {
19132 pop_it (it);
19133 return 0;
19134 }
19135
19136 it->string = prop;
19137 it->string_from_prefix_prop_p = 1;
19138 it->multibyte_p = STRING_MULTIBYTE (it->string);
19139 it->current.overlay_string_index = -1;
19140 IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = 0;
19141 it->end_charpos = it->string_nchars = SCHARS (it->string);
19142 it->method = GET_FROM_STRING;
19143 it->stop_charpos = 0;
19144 it->prev_stop = 0;
19145 it->base_level_stop = 0;
19146
19147 /* Force paragraph direction to be that of the parent
19148 buffer/string. */
19149 if (it->bidi_p && it->bidi_it.paragraph_dir == R2L)
19150 it->paragraph_embedding = it->bidi_it.paragraph_dir;
19151 else
19152 it->paragraph_embedding = L2R;
19153
19154 /* Set up the bidi iterator for this display string. */
19155 if (it->bidi_p)
19156 {
19157 it->bidi_it.string.lstring = it->string;
19158 it->bidi_it.string.s = NULL;
19159 it->bidi_it.string.schars = it->end_charpos;
19160 it->bidi_it.string.bufpos = IT_CHARPOS (*it);
19161 it->bidi_it.string.from_disp_str = it->string_from_display_prop_p;
19162 it->bidi_it.string.unibyte = !it->multibyte_p;
19163 bidi_init_it (0, 0, FRAME_WINDOW_P (it->f), &it->bidi_it);
19164 }
19165 }
19166 else if (CONSP (prop) && EQ (XCAR (prop), Qspace))
19167 {
19168 it->method = GET_FROM_STRETCH;
19169 it->object = prop;
19170 }
19171 #ifdef HAVE_WINDOW_SYSTEM
19172 else if (IMAGEP (prop))
19173 {
19174 it->what = IT_IMAGE;
19175 it->image_id = lookup_image (it->f, prop);
19176 it->method = GET_FROM_IMAGE;
19177 }
19178 #endif /* HAVE_WINDOW_SYSTEM */
19179 else
19180 {
19181 pop_it (it); /* bogus display property, give up */
19182 return 0;
19183 }
19184
19185 return 1;
19186 }
19187
19188 /* Return the character-property PROP at the current position in IT. */
19189
19190 static Lisp_Object
19191 get_it_property (struct it *it, Lisp_Object prop)
19192 {
19193 Lisp_Object position;
19194
19195 if (STRINGP (it->object))
19196 position = make_number (IT_STRING_CHARPOS (*it));
19197 else if (BUFFERP (it->object))
19198 position = make_number (IT_CHARPOS (*it));
19199 else
19200 return Qnil;
19201
19202 return Fget_char_property (position, prop, it->object);
19203 }
19204
19205 /* See if there's a line- or wrap-prefix, and if so, push it on IT. */
19206
19207 static void
19208 handle_line_prefix (struct it *it)
19209 {
19210 Lisp_Object prefix;
19211
19212 if (it->continuation_lines_width > 0)
19213 {
19214 prefix = get_it_property (it, Qwrap_prefix);
19215 if (NILP (prefix))
19216 prefix = Vwrap_prefix;
19217 }
19218 else
19219 {
19220 prefix = get_it_property (it, Qline_prefix);
19221 if (NILP (prefix))
19222 prefix = Vline_prefix;
19223 }
19224 if (! NILP (prefix) && push_prefix_prop (it, prefix))
19225 {
19226 /* If the prefix is wider than the window, and we try to wrap
19227 it, it would acquire its own wrap prefix, and so on till the
19228 iterator stack overflows. So, don't wrap the prefix. */
19229 it->line_wrap = TRUNCATE;
19230 it->avoid_cursor_p = 1;
19231 }
19232 }
19233
19234 \f
19235
19236 /* Remove N glyphs at the start of a reversed IT->glyph_row. Called
19237 only for R2L lines from display_line and display_string, when they
19238 decide that too many glyphs were produced by PRODUCE_GLYPHS, and
19239 the line/string needs to be continued on the next glyph row. */
19240 static void
19241 unproduce_glyphs (struct it *it, int n)
19242 {
19243 struct glyph *glyph, *end;
19244
19245 eassert (it->glyph_row);
19246 eassert (it->glyph_row->reversed_p);
19247 eassert (it->area == TEXT_AREA);
19248 eassert (n <= it->glyph_row->used[TEXT_AREA]);
19249
19250 if (n > it->glyph_row->used[TEXT_AREA])
19251 n = it->glyph_row->used[TEXT_AREA];
19252 glyph = it->glyph_row->glyphs[TEXT_AREA] + n;
19253 end = it->glyph_row->glyphs[TEXT_AREA] + it->glyph_row->used[TEXT_AREA];
19254 for ( ; glyph < end; glyph++)
19255 glyph[-n] = *glyph;
19256 }
19257
19258 /* Find the positions in a bidi-reordered ROW to serve as ROW->minpos
19259 and ROW->maxpos. */
19260 static void
19261 find_row_edges (struct it *it, struct glyph_row *row,
19262 ptrdiff_t min_pos, ptrdiff_t min_bpos,
19263 ptrdiff_t max_pos, ptrdiff_t max_bpos)
19264 {
19265 /* FIXME: Revisit this when glyph ``spilling'' in continuation
19266 lines' rows is implemented for bidi-reordered rows. */
19267
19268 /* ROW->minpos is the value of min_pos, the minimal buffer position
19269 we have in ROW, or ROW->start.pos if that is smaller. */
19270 if (min_pos <= ZV && min_pos < row->start.pos.charpos)
19271 SET_TEXT_POS (row->minpos, min_pos, min_bpos);
19272 else
19273 /* We didn't find buffer positions smaller than ROW->start, or
19274 didn't find _any_ valid buffer positions in any of the glyphs,
19275 so we must trust the iterator's computed positions. */
19276 row->minpos = row->start.pos;
19277 if (max_pos <= 0)
19278 {
19279 max_pos = CHARPOS (it->current.pos);
19280 max_bpos = BYTEPOS (it->current.pos);
19281 }
19282
19283 /* Here are the various use-cases for ending the row, and the
19284 corresponding values for ROW->maxpos:
19285
19286 Line ends in a newline from buffer eol_pos + 1
19287 Line is continued from buffer max_pos + 1
19288 Line is truncated on right it->current.pos
19289 Line ends in a newline from string max_pos + 1(*)
19290 (*) + 1 only when line ends in a forward scan
19291 Line is continued from string max_pos
19292 Line is continued from display vector max_pos
19293 Line is entirely from a string min_pos == max_pos
19294 Line is entirely from a display vector min_pos == max_pos
19295 Line that ends at ZV ZV
19296
19297 If you discover other use-cases, please add them here as
19298 appropriate. */
19299 if (row->ends_at_zv_p)
19300 row->maxpos = it->current.pos;
19301 else if (row->used[TEXT_AREA])
19302 {
19303 int seen_this_string = 0;
19304 struct glyph_row *r1 = row - 1;
19305
19306 /* Did we see the same display string on the previous row? */
19307 if (STRINGP (it->object)
19308 /* this is not the first row */
19309 && row > it->w->desired_matrix->rows
19310 /* previous row is not the header line */
19311 && !r1->mode_line_p
19312 /* previous row also ends in a newline from a string */
19313 && r1->ends_in_newline_from_string_p)
19314 {
19315 struct glyph *start, *end;
19316
19317 /* Search for the last glyph of the previous row that came
19318 from buffer or string. Depending on whether the row is
19319 L2R or R2L, we need to process it front to back or the
19320 other way round. */
19321 if (!r1->reversed_p)
19322 {
19323 start = r1->glyphs[TEXT_AREA];
19324 end = start + r1->used[TEXT_AREA];
19325 /* Glyphs inserted by redisplay have an integer (zero)
19326 as their object. */
19327 while (end > start
19328 && INTEGERP ((end - 1)->object)
19329 && (end - 1)->charpos <= 0)
19330 --end;
19331 if (end > start)
19332 {
19333 if (EQ ((end - 1)->object, it->object))
19334 seen_this_string = 1;
19335 }
19336 else
19337 /* If all the glyphs of the previous row were inserted
19338 by redisplay, it means the previous row was
19339 produced from a single newline, which is only
19340 possible if that newline came from the same string
19341 as the one which produced this ROW. */
19342 seen_this_string = 1;
19343 }
19344 else
19345 {
19346 end = r1->glyphs[TEXT_AREA] - 1;
19347 start = end + r1->used[TEXT_AREA];
19348 while (end < start
19349 && INTEGERP ((end + 1)->object)
19350 && (end + 1)->charpos <= 0)
19351 ++end;
19352 if (end < start)
19353 {
19354 if (EQ ((end + 1)->object, it->object))
19355 seen_this_string = 1;
19356 }
19357 else
19358 seen_this_string = 1;
19359 }
19360 }
19361 /* Take note of each display string that covers a newline only
19362 once, the first time we see it. This is for when a display
19363 string includes more than one newline in it. */
19364 if (row->ends_in_newline_from_string_p && !seen_this_string)
19365 {
19366 /* If we were scanning the buffer forward when we displayed
19367 the string, we want to account for at least one buffer
19368 position that belongs to this row (position covered by
19369 the display string), so that cursor positioning will
19370 consider this row as a candidate when point is at the end
19371 of the visual line represented by this row. This is not
19372 required when scanning back, because max_pos will already
19373 have a much larger value. */
19374 if (CHARPOS (row->end.pos) > max_pos)
19375 INC_BOTH (max_pos, max_bpos);
19376 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19377 }
19378 else if (CHARPOS (it->eol_pos) > 0)
19379 SET_TEXT_POS (row->maxpos,
19380 CHARPOS (it->eol_pos) + 1, BYTEPOS (it->eol_pos) + 1);
19381 else if (row->continued_p)
19382 {
19383 /* If max_pos is different from IT's current position, it
19384 means IT->method does not belong to the display element
19385 at max_pos. However, it also means that the display
19386 element at max_pos was displayed in its entirety on this
19387 line, which is equivalent to saying that the next line
19388 starts at the next buffer position. */
19389 if (IT_CHARPOS (*it) == max_pos && it->method != GET_FROM_BUFFER)
19390 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19391 else
19392 {
19393 INC_BOTH (max_pos, max_bpos);
19394 SET_TEXT_POS (row->maxpos, max_pos, max_bpos);
19395 }
19396 }
19397 else if (row->truncated_on_right_p)
19398 /* display_line already called reseat_at_next_visible_line_start,
19399 which puts the iterator at the beginning of the next line, in
19400 the logical order. */
19401 row->maxpos = it->current.pos;
19402 else if (max_pos == min_pos && it->method != GET_FROM_BUFFER)
19403 /* A line that is entirely from a string/image/stretch... */
19404 row->maxpos = row->minpos;
19405 else
19406 emacs_abort ();
19407 }
19408 else
19409 row->maxpos = it->current.pos;
19410 }
19411
19412 /* Construct the glyph row IT->glyph_row in the desired matrix of
19413 IT->w from text at the current position of IT. See dispextern.h
19414 for an overview of struct it. Value is non-zero if
19415 IT->glyph_row displays text, as opposed to a line displaying ZV
19416 only. */
19417
19418 static int
19419 display_line (struct it *it)
19420 {
19421 struct glyph_row *row = it->glyph_row;
19422 Lisp_Object overlay_arrow_string;
19423 struct it wrap_it;
19424 void *wrap_data = NULL;
19425 int may_wrap = 0, wrap_x IF_LINT (= 0);
19426 int wrap_row_used = -1;
19427 int wrap_row_ascent IF_LINT (= 0), wrap_row_height IF_LINT (= 0);
19428 int wrap_row_phys_ascent IF_LINT (= 0), wrap_row_phys_height IF_LINT (= 0);
19429 int wrap_row_extra_line_spacing IF_LINT (= 0);
19430 ptrdiff_t wrap_row_min_pos IF_LINT (= 0), wrap_row_min_bpos IF_LINT (= 0);
19431 ptrdiff_t wrap_row_max_pos IF_LINT (= 0), wrap_row_max_bpos IF_LINT (= 0);
19432 int cvpos;
19433 ptrdiff_t min_pos = ZV + 1, max_pos = 0;
19434 ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
19435
19436 /* We always start displaying at hpos zero even if hscrolled. */
19437 eassert (it->hpos == 0 && it->current_x == 0);
19438
19439 if (MATRIX_ROW_VPOS (row, it->w->desired_matrix)
19440 >= it->w->desired_matrix->nrows)
19441 {
19442 it->w->nrows_scale_factor++;
19443 fonts_changed_p = 1;
19444 return 0;
19445 }
19446
19447 /* Is IT->w showing the region? */
19448 wset_region_showing (it->w, it->region_beg_charpos > 0 ? Qt : Qnil);
19449
19450 /* Clear the result glyph row and enable it. */
19451 prepare_desired_row (row);
19452
19453 row->y = it->current_y;
19454 row->start = it->start;
19455 row->continuation_lines_width = it->continuation_lines_width;
19456 row->displays_text_p = 1;
19457 row->starts_in_middle_of_char_p = it->starts_in_middle_of_char_p;
19458 it->starts_in_middle_of_char_p = 0;
19459
19460 /* Arrange the overlays nicely for our purposes. Usually, we call
19461 display_line on only one line at a time, in which case this
19462 can't really hurt too much, or we call it on lines which appear
19463 one after another in the buffer, in which case all calls to
19464 recenter_overlay_lists but the first will be pretty cheap. */
19465 recenter_overlay_lists (current_buffer, IT_CHARPOS (*it));
19466
19467 /* Move over display elements that are not visible because we are
19468 hscrolled. This may stop at an x-position < IT->first_visible_x
19469 if the first glyph is partially visible or if we hit a line end. */
19470 if (it->current_x < it->first_visible_x)
19471 {
19472 enum move_it_result move_result;
19473
19474 this_line_min_pos = row->start.pos;
19475 move_result = move_it_in_display_line_to (it, ZV, it->first_visible_x,
19476 MOVE_TO_POS | MOVE_TO_X);
19477 /* If we are under a large hscroll, move_it_in_display_line_to
19478 could hit the end of the line without reaching
19479 it->first_visible_x. Pretend that we did reach it. This is
19480 especially important on a TTY, where we will call
19481 extend_face_to_end_of_line, which needs to know how many
19482 blank glyphs to produce. */
19483 if (it->current_x < it->first_visible_x
19484 && (move_result == MOVE_NEWLINE_OR_CR
19485 || move_result == MOVE_POS_MATCH_OR_ZV))
19486 it->current_x = it->first_visible_x;
19487
19488 /* Record the smallest positions seen while we moved over
19489 display elements that are not visible. This is needed by
19490 redisplay_internal for optimizing the case where the cursor
19491 stays inside the same line. The rest of this function only
19492 considers positions that are actually displayed, so
19493 RECORD_MAX_MIN_POS will not otherwise record positions that
19494 are hscrolled to the left of the left edge of the window. */
19495 min_pos = CHARPOS (this_line_min_pos);
19496 min_bpos = BYTEPOS (this_line_min_pos);
19497 }
19498 else
19499 {
19500 /* We only do this when not calling `move_it_in_display_line_to'
19501 above, because move_it_in_display_line_to calls
19502 handle_line_prefix itself. */
19503 handle_line_prefix (it);
19504 }
19505
19506 /* Get the initial row height. This is either the height of the
19507 text hscrolled, if there is any, or zero. */
19508 row->ascent = it->max_ascent;
19509 row->height = it->max_ascent + it->max_descent;
19510 row->phys_ascent = it->max_phys_ascent;
19511 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
19512 row->extra_line_spacing = it->max_extra_line_spacing;
19513
19514 /* Utility macro to record max and min buffer positions seen until now. */
19515 #define RECORD_MAX_MIN_POS(IT) \
19516 do \
19517 { \
19518 int composition_p = !STRINGP ((IT)->string) \
19519 && ((IT)->what == IT_COMPOSITION); \
19520 ptrdiff_t current_pos = \
19521 composition_p ? (IT)->cmp_it.charpos \
19522 : IT_CHARPOS (*(IT)); \
19523 ptrdiff_t current_bpos = \
19524 composition_p ? CHAR_TO_BYTE (current_pos) \
19525 : IT_BYTEPOS (*(IT)); \
19526 if (current_pos < min_pos) \
19527 { \
19528 min_pos = current_pos; \
19529 min_bpos = current_bpos; \
19530 } \
19531 if (IT_CHARPOS (*it) > max_pos) \
19532 { \
19533 max_pos = IT_CHARPOS (*it); \
19534 max_bpos = IT_BYTEPOS (*it); \
19535 } \
19536 } \
19537 while (0)
19538
19539 /* Loop generating characters. The loop is left with IT on the next
19540 character to display. */
19541 while (1)
19542 {
19543 int n_glyphs_before, hpos_before, x_before;
19544 int x, nglyphs;
19545 int ascent = 0, descent = 0, phys_ascent = 0, phys_descent = 0;
19546
19547 /* Retrieve the next thing to display. Value is zero if end of
19548 buffer reached. */
19549 if (!get_next_display_element (it))
19550 {
19551 /* Maybe add a space at the end of this line that is used to
19552 display the cursor there under X. Set the charpos of the
19553 first glyph of blank lines not corresponding to any text
19554 to -1. */
19555 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19556 row->exact_window_width_line_p = 1;
19557 else if ((append_space_for_newline (it, 1) && row->used[TEXT_AREA] == 1)
19558 || row->used[TEXT_AREA] == 0)
19559 {
19560 row->glyphs[TEXT_AREA]->charpos = -1;
19561 row->displays_text_p = 0;
19562
19563 if (!NILP (BVAR (XBUFFER (it->w->buffer), indicate_empty_lines))
19564 && (!MINI_WINDOW_P (it->w)
19565 || (minibuf_level && EQ (it->window, minibuf_window))))
19566 row->indicate_empty_line_p = 1;
19567 }
19568
19569 it->continuation_lines_width = 0;
19570 row->ends_at_zv_p = 1;
19571 /* A row that displays right-to-left text must always have
19572 its last face extended all the way to the end of line,
19573 even if this row ends in ZV, because we still write to
19574 the screen left to right. We also need to extend the
19575 last face if the default face is remapped to some
19576 different face, otherwise the functions that clear
19577 portions of the screen will clear with the default face's
19578 background color. */
19579 if (row->reversed_p
19580 || lookup_basic_face (it->f, DEFAULT_FACE_ID) != DEFAULT_FACE_ID)
19581 extend_face_to_end_of_line (it);
19582 break;
19583 }
19584
19585 /* Now, get the metrics of what we want to display. This also
19586 generates glyphs in `row' (which is IT->glyph_row). */
19587 n_glyphs_before = row->used[TEXT_AREA];
19588 x = it->current_x;
19589
19590 /* Remember the line height so far in case the next element doesn't
19591 fit on the line. */
19592 if (it->line_wrap != TRUNCATE)
19593 {
19594 ascent = it->max_ascent;
19595 descent = it->max_descent;
19596 phys_ascent = it->max_phys_ascent;
19597 phys_descent = it->max_phys_descent;
19598
19599 if (it->line_wrap == WORD_WRAP && it->area == TEXT_AREA)
19600 {
19601 if (IT_DISPLAYING_WHITESPACE (it))
19602 may_wrap = 1;
19603 else if (may_wrap)
19604 {
19605 SAVE_IT (wrap_it, *it, wrap_data);
19606 wrap_x = x;
19607 wrap_row_used = row->used[TEXT_AREA];
19608 wrap_row_ascent = row->ascent;
19609 wrap_row_height = row->height;
19610 wrap_row_phys_ascent = row->phys_ascent;
19611 wrap_row_phys_height = row->phys_height;
19612 wrap_row_extra_line_spacing = row->extra_line_spacing;
19613 wrap_row_min_pos = min_pos;
19614 wrap_row_min_bpos = min_bpos;
19615 wrap_row_max_pos = max_pos;
19616 wrap_row_max_bpos = max_bpos;
19617 may_wrap = 0;
19618 }
19619 }
19620 }
19621
19622 PRODUCE_GLYPHS (it);
19623
19624 /* If this display element was in marginal areas, continue with
19625 the next one. */
19626 if (it->area != TEXT_AREA)
19627 {
19628 row->ascent = max (row->ascent, it->max_ascent);
19629 row->height = max (row->height, it->max_ascent + it->max_descent);
19630 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19631 row->phys_height = max (row->phys_height,
19632 it->max_phys_ascent + it->max_phys_descent);
19633 row->extra_line_spacing = max (row->extra_line_spacing,
19634 it->max_extra_line_spacing);
19635 set_iterator_to_next (it, 1);
19636 continue;
19637 }
19638
19639 /* Does the display element fit on the line? If we truncate
19640 lines, we should draw past the right edge of the window. If
19641 we don't truncate, we want to stop so that we can display the
19642 continuation glyph before the right margin. If lines are
19643 continued, there are two possible strategies for characters
19644 resulting in more than 1 glyph (e.g. tabs): Display as many
19645 glyphs as possible in this line and leave the rest for the
19646 continuation line, or display the whole element in the next
19647 line. Original redisplay did the former, so we do it also. */
19648 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
19649 hpos_before = it->hpos;
19650 x_before = x;
19651
19652 if (/* Not a newline. */
19653 nglyphs > 0
19654 /* Glyphs produced fit entirely in the line. */
19655 && it->current_x < it->last_visible_x)
19656 {
19657 it->hpos += nglyphs;
19658 row->ascent = max (row->ascent, it->max_ascent);
19659 row->height = max (row->height, it->max_ascent + it->max_descent);
19660 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19661 row->phys_height = max (row->phys_height,
19662 it->max_phys_ascent + it->max_phys_descent);
19663 row->extra_line_spacing = max (row->extra_line_spacing,
19664 it->max_extra_line_spacing);
19665 if (it->current_x - it->pixel_width < it->first_visible_x)
19666 row->x = x - it->first_visible_x;
19667 /* Record the maximum and minimum buffer positions seen so
19668 far in glyphs that will be displayed by this row. */
19669 if (it->bidi_p)
19670 RECORD_MAX_MIN_POS (it);
19671 }
19672 else
19673 {
19674 int i, new_x;
19675 struct glyph *glyph;
19676
19677 for (i = 0; i < nglyphs; ++i, x = new_x)
19678 {
19679 glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
19680 new_x = x + glyph->pixel_width;
19681
19682 if (/* Lines are continued. */
19683 it->line_wrap != TRUNCATE
19684 && (/* Glyph doesn't fit on the line. */
19685 new_x > it->last_visible_x
19686 /* Or it fits exactly on a window system frame. */
19687 || (new_x == it->last_visible_x
19688 && FRAME_WINDOW_P (it->f)
19689 && (row->reversed_p
19690 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19691 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)))))
19692 {
19693 /* End of a continued line. */
19694
19695 if (it->hpos == 0
19696 || (new_x == it->last_visible_x
19697 && FRAME_WINDOW_P (it->f)
19698 && (row->reversed_p
19699 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19700 : WINDOW_RIGHT_FRINGE_WIDTH (it->w))))
19701 {
19702 /* Current glyph is the only one on the line or
19703 fits exactly on the line. We must continue
19704 the line because we can't draw the cursor
19705 after the glyph. */
19706 row->continued_p = 1;
19707 it->current_x = new_x;
19708 it->continuation_lines_width += new_x;
19709 ++it->hpos;
19710 if (i == nglyphs - 1)
19711 {
19712 /* If line-wrap is on, check if a previous
19713 wrap point was found. */
19714 if (wrap_row_used > 0
19715 /* Even if there is a previous wrap
19716 point, continue the line here as
19717 usual, if (i) the previous character
19718 was a space or tab AND (ii) the
19719 current character is not. */
19720 && (!may_wrap
19721 || IT_DISPLAYING_WHITESPACE (it)))
19722 goto back_to_wrap;
19723
19724 /* Record the maximum and minimum buffer
19725 positions seen so far in glyphs that will be
19726 displayed by this row. */
19727 if (it->bidi_p)
19728 RECORD_MAX_MIN_POS (it);
19729 set_iterator_to_next (it, 1);
19730 if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19731 {
19732 if (!get_next_display_element (it))
19733 {
19734 row->exact_window_width_line_p = 1;
19735 it->continuation_lines_width = 0;
19736 row->continued_p = 0;
19737 row->ends_at_zv_p = 1;
19738 }
19739 else if (ITERATOR_AT_END_OF_LINE_P (it))
19740 {
19741 row->continued_p = 0;
19742 row->exact_window_width_line_p = 1;
19743 }
19744 }
19745 }
19746 else if (it->bidi_p)
19747 RECORD_MAX_MIN_POS (it);
19748 }
19749 else if (CHAR_GLYPH_PADDING_P (*glyph)
19750 && !FRAME_WINDOW_P (it->f))
19751 {
19752 /* A padding glyph that doesn't fit on this line.
19753 This means the whole character doesn't fit
19754 on the line. */
19755 if (row->reversed_p)
19756 unproduce_glyphs (it, row->used[TEXT_AREA]
19757 - n_glyphs_before);
19758 row->used[TEXT_AREA] = n_glyphs_before;
19759
19760 /* Fill the rest of the row with continuation
19761 glyphs like in 20.x. */
19762 while (row->glyphs[TEXT_AREA] + row->used[TEXT_AREA]
19763 < row->glyphs[1 + TEXT_AREA])
19764 produce_special_glyphs (it, IT_CONTINUATION);
19765
19766 row->continued_p = 1;
19767 it->current_x = x_before;
19768 it->continuation_lines_width += x_before;
19769
19770 /* Restore the height to what it was before the
19771 element not fitting on the line. */
19772 it->max_ascent = ascent;
19773 it->max_descent = descent;
19774 it->max_phys_ascent = phys_ascent;
19775 it->max_phys_descent = phys_descent;
19776 }
19777 else if (wrap_row_used > 0)
19778 {
19779 back_to_wrap:
19780 if (row->reversed_p)
19781 unproduce_glyphs (it,
19782 row->used[TEXT_AREA] - wrap_row_used);
19783 RESTORE_IT (it, &wrap_it, wrap_data);
19784 it->continuation_lines_width += wrap_x;
19785 row->used[TEXT_AREA] = wrap_row_used;
19786 row->ascent = wrap_row_ascent;
19787 row->height = wrap_row_height;
19788 row->phys_ascent = wrap_row_phys_ascent;
19789 row->phys_height = wrap_row_phys_height;
19790 row->extra_line_spacing = wrap_row_extra_line_spacing;
19791 min_pos = wrap_row_min_pos;
19792 min_bpos = wrap_row_min_bpos;
19793 max_pos = wrap_row_max_pos;
19794 max_bpos = wrap_row_max_bpos;
19795 row->continued_p = 1;
19796 row->ends_at_zv_p = 0;
19797 row->exact_window_width_line_p = 0;
19798 it->continuation_lines_width += x;
19799
19800 /* Make sure that a non-default face is extended
19801 up to the right margin of the window. */
19802 extend_face_to_end_of_line (it);
19803 }
19804 else if (it->c == '\t' && FRAME_WINDOW_P (it->f))
19805 {
19806 /* A TAB that extends past the right edge of the
19807 window. This produces a single glyph on
19808 window system frames. We leave the glyph in
19809 this row and let it fill the row, but don't
19810 consume the TAB. */
19811 if ((row->reversed_p
19812 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19813 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19814 produce_special_glyphs (it, IT_CONTINUATION);
19815 it->continuation_lines_width += it->last_visible_x;
19816 row->ends_in_middle_of_char_p = 1;
19817 row->continued_p = 1;
19818 glyph->pixel_width = it->last_visible_x - x;
19819 it->starts_in_middle_of_char_p = 1;
19820 }
19821 else
19822 {
19823 /* Something other than a TAB that draws past
19824 the right edge of the window. Restore
19825 positions to values before the element. */
19826 if (row->reversed_p)
19827 unproduce_glyphs (it, row->used[TEXT_AREA]
19828 - (n_glyphs_before + i));
19829 row->used[TEXT_AREA] = n_glyphs_before + i;
19830
19831 /* Display continuation glyphs. */
19832 it->current_x = x_before;
19833 it->continuation_lines_width += x;
19834 if (!FRAME_WINDOW_P (it->f)
19835 || (row->reversed_p
19836 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19837 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19838 produce_special_glyphs (it, IT_CONTINUATION);
19839 row->continued_p = 1;
19840
19841 extend_face_to_end_of_line (it);
19842
19843 if (nglyphs > 1 && i > 0)
19844 {
19845 row->ends_in_middle_of_char_p = 1;
19846 it->starts_in_middle_of_char_p = 1;
19847 }
19848
19849 /* Restore the height to what it was before the
19850 element not fitting on the line. */
19851 it->max_ascent = ascent;
19852 it->max_descent = descent;
19853 it->max_phys_ascent = phys_ascent;
19854 it->max_phys_descent = phys_descent;
19855 }
19856
19857 break;
19858 }
19859 else if (new_x > it->first_visible_x)
19860 {
19861 /* Increment number of glyphs actually displayed. */
19862 ++it->hpos;
19863
19864 /* Record the maximum and minimum buffer positions
19865 seen so far in glyphs that will be displayed by
19866 this row. */
19867 if (it->bidi_p)
19868 RECORD_MAX_MIN_POS (it);
19869
19870 if (x < it->first_visible_x)
19871 /* Glyph is partially visible, i.e. row starts at
19872 negative X position. */
19873 row->x = x - it->first_visible_x;
19874 }
19875 else
19876 {
19877 /* Glyph is completely off the left margin of the
19878 window. This should not happen because of the
19879 move_it_in_display_line at the start of this
19880 function, unless the text display area of the
19881 window is empty. */
19882 eassert (it->first_visible_x <= it->last_visible_x);
19883 }
19884 }
19885 /* Even if this display element produced no glyphs at all,
19886 we want to record its position. */
19887 if (it->bidi_p && nglyphs == 0)
19888 RECORD_MAX_MIN_POS (it);
19889
19890 row->ascent = max (row->ascent, it->max_ascent);
19891 row->height = max (row->height, it->max_ascent + it->max_descent);
19892 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
19893 row->phys_height = max (row->phys_height,
19894 it->max_phys_ascent + it->max_phys_descent);
19895 row->extra_line_spacing = max (row->extra_line_spacing,
19896 it->max_extra_line_spacing);
19897
19898 /* End of this display line if row is continued. */
19899 if (row->continued_p || row->ends_at_zv_p)
19900 break;
19901 }
19902
19903 at_end_of_line:
19904 /* Is this a line end? If yes, we're also done, after making
19905 sure that a non-default face is extended up to the right
19906 margin of the window. */
19907 if (ITERATOR_AT_END_OF_LINE_P (it))
19908 {
19909 int used_before = row->used[TEXT_AREA];
19910
19911 row->ends_in_newline_from_string_p = STRINGP (it->object);
19912
19913 /* Add a space at the end of the line that is used to
19914 display the cursor there. */
19915 if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19916 append_space_for_newline (it, 0);
19917
19918 /* Extend the face to the end of the line. */
19919 extend_face_to_end_of_line (it);
19920
19921 /* Make sure we have the position. */
19922 if (used_before == 0)
19923 row->glyphs[TEXT_AREA]->charpos = CHARPOS (it->position);
19924
19925 /* Record the position of the newline, for use in
19926 find_row_edges. */
19927 it->eol_pos = it->current.pos;
19928
19929 /* Consume the line end. This skips over invisible lines. */
19930 set_iterator_to_next (it, 1);
19931 it->continuation_lines_width = 0;
19932 break;
19933 }
19934
19935 /* Proceed with next display element. Note that this skips
19936 over lines invisible because of selective display. */
19937 set_iterator_to_next (it, 1);
19938
19939 /* If we truncate lines, we are done when the last displayed
19940 glyphs reach past the right margin of the window. */
19941 if (it->line_wrap == TRUNCATE
19942 && (FRAME_WINDOW_P (it->f) && WINDOW_RIGHT_FRINGE_WIDTH (it->w)
19943 ? (it->current_x >= it->last_visible_x)
19944 : (it->current_x > it->last_visible_x)))
19945 {
19946 /* Maybe add truncation glyphs. */
19947 if (!FRAME_WINDOW_P (it->f)
19948 || (row->reversed_p
19949 ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
19950 : WINDOW_RIGHT_FRINGE_WIDTH (it->w)) == 0)
19951 {
19952 int i, n;
19953
19954 if (!row->reversed_p)
19955 {
19956 for (i = row->used[TEXT_AREA] - 1; i > 0; --i)
19957 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19958 break;
19959 }
19960 else
19961 {
19962 for (i = 0; i < row->used[TEXT_AREA]; i++)
19963 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][i]))
19964 break;
19965 /* Remove any padding glyphs at the front of ROW, to
19966 make room for the truncation glyphs we will be
19967 adding below. The loop below always inserts at
19968 least one truncation glyph, so also remove the
19969 last glyph added to ROW. */
19970 unproduce_glyphs (it, i + 1);
19971 /* Adjust i for the loop below. */
19972 i = row->used[TEXT_AREA] - (i + 1);
19973 }
19974
19975 it->current_x = x_before;
19976 if (!FRAME_WINDOW_P (it->f))
19977 {
19978 for (n = row->used[TEXT_AREA]; i < n; ++i)
19979 {
19980 row->used[TEXT_AREA] = i;
19981 produce_special_glyphs (it, IT_TRUNCATION);
19982 }
19983 }
19984 else
19985 {
19986 row->used[TEXT_AREA] = i;
19987 produce_special_glyphs (it, IT_TRUNCATION);
19988 }
19989 }
19990 else if (IT_OVERFLOW_NEWLINE_INTO_FRINGE (it))
19991 {
19992 /* Don't truncate if we can overflow newline into fringe. */
19993 if (!get_next_display_element (it))
19994 {
19995 it->continuation_lines_width = 0;
19996 row->ends_at_zv_p = 1;
19997 row->exact_window_width_line_p = 1;
19998 break;
19999 }
20000 if (ITERATOR_AT_END_OF_LINE_P (it))
20001 {
20002 row->exact_window_width_line_p = 1;
20003 goto at_end_of_line;
20004 }
20005 it->current_x = x_before;
20006 }
20007
20008 row->truncated_on_right_p = 1;
20009 it->continuation_lines_width = 0;
20010 reseat_at_next_visible_line_start (it, 0);
20011 row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
20012 it->hpos = hpos_before;
20013 break;
20014 }
20015 }
20016
20017 if (wrap_data)
20018 bidi_unshelve_cache (wrap_data, 1);
20019
20020 /* If line is not empty and hscrolled, maybe insert truncation glyphs
20021 at the left window margin. */
20022 if (it->first_visible_x
20023 && IT_CHARPOS (*it) != CHARPOS (row->start.pos))
20024 {
20025 if (!FRAME_WINDOW_P (it->f)
20026 || (row->reversed_p
20027 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
20028 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
20029 insert_left_trunc_glyphs (it);
20030 row->truncated_on_left_p = 1;
20031 }
20032
20033 /* Remember the position at which this line ends.
20034
20035 BIDI Note: any code that needs MATRIX_ROW_START/END_CHARPOS
20036 cannot be before the call to find_row_edges below, since that is
20037 where these positions are determined. */
20038 row->end = it->current;
20039 if (!it->bidi_p)
20040 {
20041 row->minpos = row->start.pos;
20042 row->maxpos = row->end.pos;
20043 }
20044 else
20045 {
20046 /* ROW->minpos and ROW->maxpos must be the smallest and
20047 `1 + the largest' buffer positions in ROW. But if ROW was
20048 bidi-reordered, these two positions can be anywhere in the
20049 row, so we must determine them now. */
20050 find_row_edges (it, row, min_pos, min_bpos, max_pos, max_bpos);
20051 }
20052
20053 /* If the start of this line is the overlay arrow-position, then
20054 mark this glyph row as the one containing the overlay arrow.
20055 This is clearly a mess with variable size fonts. It would be
20056 better to let it be displayed like cursors under X. */
20057 if ((row->displays_text_p || !overlay_arrow_seen)
20058 && (overlay_arrow_string = overlay_arrow_at_row (it, row),
20059 !NILP (overlay_arrow_string)))
20060 {
20061 /* Overlay arrow in window redisplay is a fringe bitmap. */
20062 if (STRINGP (overlay_arrow_string))
20063 {
20064 struct glyph_row *arrow_row
20065 = get_overlay_arrow_glyph_row (it->w, overlay_arrow_string);
20066 struct glyph *glyph = arrow_row->glyphs[TEXT_AREA];
20067 struct glyph *arrow_end = glyph + arrow_row->used[TEXT_AREA];
20068 struct glyph *p = row->glyphs[TEXT_AREA];
20069 struct glyph *p2, *end;
20070
20071 /* Copy the arrow glyphs. */
20072 while (glyph < arrow_end)
20073 *p++ = *glyph++;
20074
20075 /* Throw away padding glyphs. */
20076 p2 = p;
20077 end = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA];
20078 while (p2 < end && CHAR_GLYPH_PADDING_P (*p2))
20079 ++p2;
20080 if (p2 > p)
20081 {
20082 while (p2 < end)
20083 *p++ = *p2++;
20084 row->used[TEXT_AREA] = p2 - row->glyphs[TEXT_AREA];
20085 }
20086 }
20087 else
20088 {
20089 eassert (INTEGERP (overlay_arrow_string));
20090 row->overlay_arrow_bitmap = XINT (overlay_arrow_string);
20091 }
20092 overlay_arrow_seen = 1;
20093 }
20094
20095 /* Highlight trailing whitespace. */
20096 if (!NILP (Vshow_trailing_whitespace))
20097 highlight_trailing_whitespace (it->f, it->glyph_row);
20098
20099 /* Compute pixel dimensions of this line. */
20100 compute_line_metrics (it);
20101
20102 /* Implementation note: No changes in the glyphs of ROW or in their
20103 faces can be done past this point, because compute_line_metrics
20104 computes ROW's hash value and stores it within the glyph_row
20105 structure. */
20106
20107 /* Record whether this row ends inside an ellipsis. */
20108 row->ends_in_ellipsis_p
20109 = (it->method == GET_FROM_DISPLAY_VECTOR
20110 && it->ellipsis_p);
20111
20112 /* Save fringe bitmaps in this row. */
20113 row->left_user_fringe_bitmap = it->left_user_fringe_bitmap;
20114 row->left_user_fringe_face_id = it->left_user_fringe_face_id;
20115 row->right_user_fringe_bitmap = it->right_user_fringe_bitmap;
20116 row->right_user_fringe_face_id = it->right_user_fringe_face_id;
20117
20118 it->left_user_fringe_bitmap = 0;
20119 it->left_user_fringe_face_id = 0;
20120 it->right_user_fringe_bitmap = 0;
20121 it->right_user_fringe_face_id = 0;
20122
20123 /* Maybe set the cursor. */
20124 cvpos = it->w->cursor.vpos;
20125 if ((cvpos < 0
20126 /* In bidi-reordered rows, keep checking for proper cursor
20127 position even if one has been found already, because buffer
20128 positions in such rows change non-linearly with ROW->VPOS,
20129 when a line is continued. One exception: when we are at ZV,
20130 display cursor on the first suitable glyph row, since all
20131 the empty rows after that also have their position set to ZV. */
20132 /* FIXME: Revisit this when glyph ``spilling'' in continuation
20133 lines' rows is implemented for bidi-reordered rows. */
20134 || (it->bidi_p
20135 && !MATRIX_ROW (it->w->desired_matrix, cvpos)->ends_at_zv_p))
20136 && PT >= MATRIX_ROW_START_CHARPOS (row)
20137 && PT <= MATRIX_ROW_END_CHARPOS (row)
20138 && cursor_row_p (row))
20139 set_cursor_from_row (it->w, row, it->w->desired_matrix, 0, 0, 0, 0);
20140
20141 /* Prepare for the next line. This line starts horizontally at (X
20142 HPOS) = (0 0). Vertical positions are incremented. As a
20143 convenience for the caller, IT->glyph_row is set to the next
20144 row to be used. */
20145 it->current_x = it->hpos = 0;
20146 it->current_y += row->height;
20147 SET_TEXT_POS (it->eol_pos, 0, 0);
20148 ++it->vpos;
20149 ++it->glyph_row;
20150 /* The next row should by default use the same value of the
20151 reversed_p flag as this one. set_iterator_to_next decides when
20152 it's a new paragraph, and PRODUCE_GLYPHS recomputes the value of
20153 the flag accordingly. */
20154 if (it->glyph_row < MATRIX_BOTTOM_TEXT_ROW (it->w->desired_matrix, it->w))
20155 it->glyph_row->reversed_p = row->reversed_p;
20156 it->start = row->end;
20157 return row->displays_text_p;
20158
20159 #undef RECORD_MAX_MIN_POS
20160 }
20161
20162 DEFUN ("current-bidi-paragraph-direction", Fcurrent_bidi_paragraph_direction,
20163 Scurrent_bidi_paragraph_direction, 0, 1, 0,
20164 doc: /* Return paragraph direction at point in BUFFER.
20165 Value is either `left-to-right' or `right-to-left'.
20166 If BUFFER is omitted or nil, it defaults to the current buffer.
20167
20168 Paragraph direction determines how the text in the paragraph is displayed.
20169 In left-to-right paragraphs, text begins at the left margin of the window
20170 and the reading direction is generally left to right. In right-to-left
20171 paragraphs, text begins at the right margin and is read from right to left.
20172
20173 See also `bidi-paragraph-direction'. */)
20174 (Lisp_Object buffer)
20175 {
20176 struct buffer *buf = current_buffer;
20177 struct buffer *old = buf;
20178
20179 if (! NILP (buffer))
20180 {
20181 CHECK_BUFFER (buffer);
20182 buf = XBUFFER (buffer);
20183 }
20184
20185 if (NILP (BVAR (buf, bidi_display_reordering))
20186 || NILP (BVAR (buf, enable_multibyte_characters))
20187 /* When we are loading loadup.el, the character property tables
20188 needed for bidi iteration are not yet available. */
20189 || !NILP (Vpurify_flag))
20190 return Qleft_to_right;
20191 else if (!NILP (BVAR (buf, bidi_paragraph_direction)))
20192 return BVAR (buf, bidi_paragraph_direction);
20193 else
20194 {
20195 /* Determine the direction from buffer text. We could try to
20196 use current_matrix if it is up to date, but this seems fast
20197 enough as it is. */
20198 struct bidi_it itb;
20199 ptrdiff_t pos = BUF_PT (buf);
20200 ptrdiff_t bytepos = BUF_PT_BYTE (buf);
20201 int c;
20202 void *itb_data = bidi_shelve_cache ();
20203
20204 set_buffer_temp (buf);
20205 /* bidi_paragraph_init finds the base direction of the paragraph
20206 by searching forward from paragraph start. We need the base
20207 direction of the current or _previous_ paragraph, so we need
20208 to make sure we are within that paragraph. To that end, find
20209 the previous non-empty line. */
20210 if (pos >= ZV && pos > BEGV)
20211 {
20212 pos--;
20213 bytepos = CHAR_TO_BYTE (pos);
20214 }
20215 if (fast_looking_at (build_string ("[\f\t ]*\n"),
20216 pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
20217 {
20218 while ((c = FETCH_BYTE (bytepos)) == '\n'
20219 || c == ' ' || c == '\t' || c == '\f')
20220 {
20221 if (bytepos <= BEGV_BYTE)
20222 break;
20223 bytepos--;
20224 pos--;
20225 }
20226 while (!CHAR_HEAD_P (FETCH_BYTE (bytepos)))
20227 bytepos--;
20228 }
20229 bidi_init_it (pos, bytepos, FRAME_WINDOW_P (SELECTED_FRAME ()), &itb);
20230 itb.paragraph_dir = NEUTRAL_DIR;
20231 itb.string.s = NULL;
20232 itb.string.lstring = Qnil;
20233 itb.string.bufpos = 0;
20234 itb.string.unibyte = 0;
20235 bidi_paragraph_init (NEUTRAL_DIR, &itb, 1);
20236 bidi_unshelve_cache (itb_data, 0);
20237 set_buffer_temp (old);
20238 switch (itb.paragraph_dir)
20239 {
20240 case L2R:
20241 return Qleft_to_right;
20242 break;
20243 case R2L:
20244 return Qright_to_left;
20245 break;
20246 default:
20247 emacs_abort ();
20248 }
20249 }
20250 }
20251
20252
20253 \f
20254 /***********************************************************************
20255 Menu Bar
20256 ***********************************************************************/
20257
20258 /* Redisplay the menu bar in the frame for window W.
20259
20260 The menu bar of X frames that don't have X toolkit support is
20261 displayed in a special window W->frame->menu_bar_window.
20262
20263 The menu bar of terminal frames is treated specially as far as
20264 glyph matrices are concerned. Menu bar lines are not part of
20265 windows, so the update is done directly on the frame matrix rows
20266 for the menu bar. */
20267
20268 static void
20269 display_menu_bar (struct window *w)
20270 {
20271 struct frame *f = XFRAME (WINDOW_FRAME (w));
20272 struct it it;
20273 Lisp_Object items;
20274 int i;
20275
20276 /* Don't do all this for graphical frames. */
20277 #ifdef HAVE_NTGUI
20278 if (FRAME_W32_P (f))
20279 return;
20280 #endif
20281 #if defined (USE_X_TOOLKIT) || defined (USE_GTK)
20282 if (FRAME_X_P (f))
20283 return;
20284 #endif
20285
20286 #ifdef HAVE_NS
20287 if (FRAME_NS_P (f))
20288 return;
20289 #endif /* HAVE_NS */
20290
20291 #ifdef USE_X_TOOLKIT
20292 eassert (!FRAME_WINDOW_P (f));
20293 init_iterator (&it, w, -1, -1, f->desired_matrix->rows, MENU_FACE_ID);
20294 it.first_visible_x = 0;
20295 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20296 #else /* not USE_X_TOOLKIT */
20297 if (FRAME_WINDOW_P (f))
20298 {
20299 /* Menu bar lines are displayed in the desired matrix of the
20300 dummy window menu_bar_window. */
20301 struct window *menu_w;
20302 eassert (WINDOWP (f->menu_bar_window));
20303 menu_w = XWINDOW (f->menu_bar_window);
20304 init_iterator (&it, menu_w, -1, -1, menu_w->desired_matrix->rows,
20305 MENU_FACE_ID);
20306 it.first_visible_x = 0;
20307 it.last_visible_x = FRAME_TOTAL_COLS (f) * FRAME_COLUMN_WIDTH (f);
20308 }
20309 else
20310 {
20311 /* This is a TTY frame, i.e. character hpos/vpos are used as
20312 pixel x/y. */
20313 init_iterator (&it, w, -1, -1, f->desired_matrix->rows,
20314 MENU_FACE_ID);
20315 it.first_visible_x = 0;
20316 it.last_visible_x = FRAME_COLS (f);
20317 }
20318 #endif /* not USE_X_TOOLKIT */
20319
20320 /* FIXME: This should be controlled by a user option. See the
20321 comments in redisplay_tool_bar and display_mode_line about
20322 this. */
20323 it.paragraph_embedding = L2R;
20324
20325 /* Clear all rows of the menu bar. */
20326 for (i = 0; i < FRAME_MENU_BAR_LINES (f); ++i)
20327 {
20328 struct glyph_row *row = it.glyph_row + i;
20329 clear_glyph_row (row);
20330 row->enabled_p = 1;
20331 row->full_width_p = 1;
20332 }
20333
20334 /* Display all items of the menu bar. */
20335 items = FRAME_MENU_BAR_ITEMS (it.f);
20336 for (i = 0; i < ASIZE (items); i += 4)
20337 {
20338 Lisp_Object string;
20339
20340 /* Stop at nil string. */
20341 string = AREF (items, i + 1);
20342 if (NILP (string))
20343 break;
20344
20345 /* Remember where item was displayed. */
20346 ASET (items, i + 3, make_number (it.hpos));
20347
20348 /* Display the item, pad with one space. */
20349 if (it.current_x < it.last_visible_x)
20350 display_string (NULL, string, Qnil, 0, 0, &it,
20351 SCHARS (string) + 1, 0, 0, -1);
20352 }
20353
20354 /* Fill out the line with spaces. */
20355 if (it.current_x < it.last_visible_x)
20356 display_string ("", Qnil, Qnil, 0, 0, &it, -1, 0, 0, -1);
20357
20358 /* Compute the total height of the lines. */
20359 compute_line_metrics (&it);
20360 }
20361
20362
20363 \f
20364 /***********************************************************************
20365 Mode Line
20366 ***********************************************************************/
20367
20368 /* Redisplay mode lines in the window tree whose root is WINDOW. If
20369 FORCE is non-zero, redisplay mode lines unconditionally.
20370 Otherwise, redisplay only mode lines that are garbaged. Value is
20371 the number of windows whose mode lines were redisplayed. */
20372
20373 static int
20374 redisplay_mode_lines (Lisp_Object window, int force)
20375 {
20376 int nwindows = 0;
20377
20378 while (!NILP (window))
20379 {
20380 struct window *w = XWINDOW (window);
20381
20382 if (WINDOWP (w->hchild))
20383 nwindows += redisplay_mode_lines (w->hchild, force);
20384 else if (WINDOWP (w->vchild))
20385 nwindows += redisplay_mode_lines (w->vchild, force);
20386 else if (force
20387 || FRAME_GARBAGED_P (XFRAME (w->frame))
20388 || !MATRIX_MODE_LINE_ROW (w->current_matrix)->enabled_p)
20389 {
20390 struct text_pos lpoint;
20391 struct buffer *old = current_buffer;
20392
20393 /* Set the window's buffer for the mode line display. */
20394 SET_TEXT_POS (lpoint, PT, PT_BYTE);
20395 set_buffer_internal_1 (XBUFFER (w->buffer));
20396
20397 /* Point refers normally to the selected window. For any
20398 other window, set up appropriate value. */
20399 if (!EQ (window, selected_window))
20400 {
20401 struct text_pos pt;
20402
20403 SET_TEXT_POS_FROM_MARKER (pt, w->pointm);
20404 if (CHARPOS (pt) < BEGV)
20405 TEMP_SET_PT_BOTH (BEGV, BEGV_BYTE);
20406 else if (CHARPOS (pt) > (ZV - 1))
20407 TEMP_SET_PT_BOTH (ZV, ZV_BYTE);
20408 else
20409 TEMP_SET_PT_BOTH (CHARPOS (pt), BYTEPOS (pt));
20410 }
20411
20412 /* Display mode lines. */
20413 clear_glyph_matrix (w->desired_matrix);
20414 if (display_mode_lines (w))
20415 {
20416 ++nwindows;
20417 w->must_be_updated_p = 1;
20418 }
20419
20420 /* Restore old settings. */
20421 set_buffer_internal_1 (old);
20422 TEMP_SET_PT_BOTH (CHARPOS (lpoint), BYTEPOS (lpoint));
20423 }
20424
20425 window = w->next;
20426 }
20427
20428 return nwindows;
20429 }
20430
20431
20432 /* Display the mode and/or header line of window W. Value is the
20433 sum number of mode lines and header lines displayed. */
20434
20435 static int
20436 display_mode_lines (struct window *w)
20437 {
20438 Lisp_Object old_selected_window, old_selected_frame;
20439 int n = 0;
20440
20441 old_selected_frame = selected_frame;
20442 selected_frame = w->frame;
20443 old_selected_window = selected_window;
20444 XSETWINDOW (selected_window, w);
20445
20446 /* These will be set while the mode line specs are processed. */
20447 line_number_displayed = 0;
20448 wset_column_number_displayed (w, Qnil);
20449
20450 if (WINDOW_WANTS_MODELINE_P (w))
20451 {
20452 struct window *sel_w = XWINDOW (old_selected_window);
20453
20454 /* Select mode line face based on the real selected window. */
20455 display_mode_line (w, CURRENT_MODE_LINE_FACE_ID_3 (sel_w, sel_w, w),
20456 BVAR (current_buffer, mode_line_format));
20457 ++n;
20458 }
20459
20460 if (WINDOW_WANTS_HEADER_LINE_P (w))
20461 {
20462 display_mode_line (w, HEADER_LINE_FACE_ID,
20463 BVAR (current_buffer, header_line_format));
20464 ++n;
20465 }
20466
20467 selected_frame = old_selected_frame;
20468 selected_window = old_selected_window;
20469 return n;
20470 }
20471
20472
20473 /* Display mode or header line of window W. FACE_ID specifies which
20474 line to display; it is either MODE_LINE_FACE_ID or
20475 HEADER_LINE_FACE_ID. FORMAT is the mode/header line format to
20476 display. Value is the pixel height of the mode/header line
20477 displayed. */
20478
20479 static int
20480 display_mode_line (struct window *w, enum face_id face_id, Lisp_Object format)
20481 {
20482 struct it it;
20483 struct face *face;
20484 ptrdiff_t count = SPECPDL_INDEX ();
20485
20486 init_iterator (&it, w, -1, -1, NULL, face_id);
20487 /* Don't extend on a previously drawn mode-line.
20488 This may happen if called from pos_visible_p. */
20489 it.glyph_row->enabled_p = 0;
20490 prepare_desired_row (it.glyph_row);
20491
20492 it.glyph_row->mode_line_p = 1;
20493
20494 /* FIXME: This should be controlled by a user option. But
20495 supporting such an option is not trivial, since the mode line is
20496 made up of many separate strings. */
20497 it.paragraph_embedding = L2R;
20498
20499 record_unwind_protect (unwind_format_mode_line,
20500 format_mode_line_unwind_data (NULL, NULL, Qnil, 0));
20501
20502 mode_line_target = MODE_LINE_DISPLAY;
20503
20504 /* Temporarily make frame's keyboard the current kboard so that
20505 kboard-local variables in the mode_line_format will get the right
20506 values. */
20507 push_kboard (FRAME_KBOARD (it.f));
20508 record_unwind_save_match_data ();
20509 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
20510 pop_kboard ();
20511
20512 unbind_to (count, Qnil);
20513
20514 /* Fill up with spaces. */
20515 display_string (" ", Qnil, Qnil, 0, 0, &it, 10000, -1, -1, 0);
20516
20517 compute_line_metrics (&it);
20518 it.glyph_row->full_width_p = 1;
20519 it.glyph_row->continued_p = 0;
20520 it.glyph_row->truncated_on_left_p = 0;
20521 it.glyph_row->truncated_on_right_p = 0;
20522
20523 /* Make a 3D mode-line have a shadow at its right end. */
20524 face = FACE_FROM_ID (it.f, face_id);
20525 extend_face_to_end_of_line (&it);
20526 if (face->box != FACE_NO_BOX)
20527 {
20528 struct glyph *last = (it.glyph_row->glyphs[TEXT_AREA]
20529 + it.glyph_row->used[TEXT_AREA] - 1);
20530 last->right_box_line_p = 1;
20531 }
20532
20533 return it.glyph_row->height;
20534 }
20535
20536 /* Move element ELT in LIST to the front of LIST.
20537 Return the updated list. */
20538
20539 static Lisp_Object
20540 move_elt_to_front (Lisp_Object elt, Lisp_Object list)
20541 {
20542 register Lisp_Object tail, prev;
20543 register Lisp_Object tem;
20544
20545 tail = list;
20546 prev = Qnil;
20547 while (CONSP (tail))
20548 {
20549 tem = XCAR (tail);
20550
20551 if (EQ (elt, tem))
20552 {
20553 /* Splice out the link TAIL. */
20554 if (NILP (prev))
20555 list = XCDR (tail);
20556 else
20557 Fsetcdr (prev, XCDR (tail));
20558
20559 /* Now make it the first. */
20560 Fsetcdr (tail, list);
20561 return tail;
20562 }
20563 else
20564 prev = tail;
20565 tail = XCDR (tail);
20566 QUIT;
20567 }
20568
20569 /* Not found--return unchanged LIST. */
20570 return list;
20571 }
20572
20573 /* Contribute ELT to the mode line for window IT->w. How it
20574 translates into text depends on its data type.
20575
20576 IT describes the display environment in which we display, as usual.
20577
20578 DEPTH is the depth in recursion. It is used to prevent
20579 infinite recursion here.
20580
20581 FIELD_WIDTH is the number of characters the display of ELT should
20582 occupy in the mode line, and PRECISION is the maximum number of
20583 characters to display from ELT's representation. See
20584 display_string for details.
20585
20586 Returns the hpos of the end of the text generated by ELT.
20587
20588 PROPS is a property list to add to any string we encounter.
20589
20590 If RISKY is nonzero, remove (disregard) any properties in any string
20591 we encounter, and ignore :eval and :propertize.
20592
20593 The global variable `mode_line_target' determines whether the
20594 output is passed to `store_mode_line_noprop',
20595 `store_mode_line_string', or `display_string'. */
20596
20597 static int
20598 display_mode_element (struct it *it, int depth, int field_width, int precision,
20599 Lisp_Object elt, Lisp_Object props, int risky)
20600 {
20601 int n = 0, field, prec;
20602 int literal = 0;
20603
20604 tail_recurse:
20605 if (depth > 100)
20606 elt = build_string ("*too-deep*");
20607
20608 depth++;
20609
20610 switch (XTYPE (elt))
20611 {
20612 case Lisp_String:
20613 {
20614 /* A string: output it and check for %-constructs within it. */
20615 unsigned char c;
20616 ptrdiff_t offset = 0;
20617
20618 if (SCHARS (elt) > 0
20619 && (!NILP (props) || risky))
20620 {
20621 Lisp_Object oprops, aelt;
20622 oprops = Ftext_properties_at (make_number (0), elt);
20623
20624 /* If the starting string's properties are not what
20625 we want, translate the string. Also, if the string
20626 is risky, do that anyway. */
20627
20628 if (NILP (Fequal (props, oprops)) || risky)
20629 {
20630 /* If the starting string has properties,
20631 merge the specified ones onto the existing ones. */
20632 if (! NILP (oprops) && !risky)
20633 {
20634 Lisp_Object tem;
20635
20636 oprops = Fcopy_sequence (oprops);
20637 tem = props;
20638 while (CONSP (tem))
20639 {
20640 oprops = Fplist_put (oprops, XCAR (tem),
20641 XCAR (XCDR (tem)));
20642 tem = XCDR (XCDR (tem));
20643 }
20644 props = oprops;
20645 }
20646
20647 aelt = Fassoc (elt, mode_line_proptrans_alist);
20648 if (! NILP (aelt) && !NILP (Fequal (props, XCDR (aelt))))
20649 {
20650 /* AELT is what we want. Move it to the front
20651 without consing. */
20652 elt = XCAR (aelt);
20653 mode_line_proptrans_alist
20654 = move_elt_to_front (aelt, mode_line_proptrans_alist);
20655 }
20656 else
20657 {
20658 Lisp_Object tem;
20659
20660 /* If AELT has the wrong props, it is useless.
20661 so get rid of it. */
20662 if (! NILP (aelt))
20663 mode_line_proptrans_alist
20664 = Fdelq (aelt, mode_line_proptrans_alist);
20665
20666 elt = Fcopy_sequence (elt);
20667 Fset_text_properties (make_number (0), Flength (elt),
20668 props, elt);
20669 /* Add this item to mode_line_proptrans_alist. */
20670 mode_line_proptrans_alist
20671 = Fcons (Fcons (elt, props),
20672 mode_line_proptrans_alist);
20673 /* Truncate mode_line_proptrans_alist
20674 to at most 50 elements. */
20675 tem = Fnthcdr (make_number (50),
20676 mode_line_proptrans_alist);
20677 if (! NILP (tem))
20678 XSETCDR (tem, Qnil);
20679 }
20680 }
20681 }
20682
20683 offset = 0;
20684
20685 if (literal)
20686 {
20687 prec = precision - n;
20688 switch (mode_line_target)
20689 {
20690 case MODE_LINE_NOPROP:
20691 case MODE_LINE_TITLE:
20692 n += store_mode_line_noprop (SSDATA (elt), -1, prec);
20693 break;
20694 case MODE_LINE_STRING:
20695 n += store_mode_line_string (NULL, elt, 1, 0, prec, Qnil);
20696 break;
20697 case MODE_LINE_DISPLAY:
20698 n += display_string (NULL, elt, Qnil, 0, 0, it,
20699 0, prec, 0, STRING_MULTIBYTE (elt));
20700 break;
20701 }
20702
20703 break;
20704 }
20705
20706 /* Handle the non-literal case. */
20707
20708 while ((precision <= 0 || n < precision)
20709 && SREF (elt, offset) != 0
20710 && (mode_line_target != MODE_LINE_DISPLAY
20711 || it->current_x < it->last_visible_x))
20712 {
20713 ptrdiff_t last_offset = offset;
20714
20715 /* Advance to end of string or next format specifier. */
20716 while ((c = SREF (elt, offset++)) != '\0' && c != '%')
20717 ;
20718
20719 if (offset - 1 != last_offset)
20720 {
20721 ptrdiff_t nchars, nbytes;
20722
20723 /* Output to end of string or up to '%'. Field width
20724 is length of string. Don't output more than
20725 PRECISION allows us. */
20726 offset--;
20727
20728 prec = c_string_width (SDATA (elt) + last_offset,
20729 offset - last_offset, precision - n,
20730 &nchars, &nbytes);
20731
20732 switch (mode_line_target)
20733 {
20734 case MODE_LINE_NOPROP:
20735 case MODE_LINE_TITLE:
20736 n += store_mode_line_noprop (SSDATA (elt) + last_offset, 0, prec);
20737 break;
20738 case MODE_LINE_STRING:
20739 {
20740 ptrdiff_t bytepos = last_offset;
20741 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20742 ptrdiff_t endpos = (precision <= 0
20743 ? string_byte_to_char (elt, offset)
20744 : charpos + nchars);
20745
20746 n += store_mode_line_string (NULL,
20747 Fsubstring (elt, make_number (charpos),
20748 make_number (endpos)),
20749 0, 0, 0, Qnil);
20750 }
20751 break;
20752 case MODE_LINE_DISPLAY:
20753 {
20754 ptrdiff_t bytepos = last_offset;
20755 ptrdiff_t charpos = string_byte_to_char (elt, bytepos);
20756
20757 if (precision <= 0)
20758 nchars = string_byte_to_char (elt, offset) - charpos;
20759 n += display_string (NULL, elt, Qnil, 0, charpos,
20760 it, 0, nchars, 0,
20761 STRING_MULTIBYTE (elt));
20762 }
20763 break;
20764 }
20765 }
20766 else /* c == '%' */
20767 {
20768 ptrdiff_t percent_position = offset;
20769
20770 /* Get the specified minimum width. Zero means
20771 don't pad. */
20772 field = 0;
20773 while ((c = SREF (elt, offset++)) >= '0' && c <= '9')
20774 field = field * 10 + c - '0';
20775
20776 /* Don't pad beyond the total padding allowed. */
20777 if (field_width - n > 0 && field > field_width - n)
20778 field = field_width - n;
20779
20780 /* Note that either PRECISION <= 0 or N < PRECISION. */
20781 prec = precision - n;
20782
20783 if (c == 'M')
20784 n += display_mode_element (it, depth, field, prec,
20785 Vglobal_mode_string, props,
20786 risky);
20787 else if (c != 0)
20788 {
20789 int multibyte;
20790 ptrdiff_t bytepos, charpos;
20791 const char *spec;
20792 Lisp_Object string;
20793
20794 bytepos = percent_position;
20795 charpos = (STRING_MULTIBYTE (elt)
20796 ? string_byte_to_char (elt, bytepos)
20797 : bytepos);
20798 spec = decode_mode_spec (it->w, c, field, &string);
20799 multibyte = STRINGP (string) && STRING_MULTIBYTE (string);
20800
20801 switch (mode_line_target)
20802 {
20803 case MODE_LINE_NOPROP:
20804 case MODE_LINE_TITLE:
20805 n += store_mode_line_noprop (spec, field, prec);
20806 break;
20807 case MODE_LINE_STRING:
20808 {
20809 Lisp_Object tem = build_string (spec);
20810 props = Ftext_properties_at (make_number (charpos), elt);
20811 /* Should only keep face property in props */
20812 n += store_mode_line_string (NULL, tem, 0, field, prec, props);
20813 }
20814 break;
20815 case MODE_LINE_DISPLAY:
20816 {
20817 int nglyphs_before, nwritten;
20818
20819 nglyphs_before = it->glyph_row->used[TEXT_AREA];
20820 nwritten = display_string (spec, string, elt,
20821 charpos, 0, it,
20822 field, prec, 0,
20823 multibyte);
20824
20825 /* Assign to the glyphs written above the
20826 string where the `%x' came from, position
20827 of the `%'. */
20828 if (nwritten > 0)
20829 {
20830 struct glyph *glyph
20831 = (it->glyph_row->glyphs[TEXT_AREA]
20832 + nglyphs_before);
20833 int i;
20834
20835 for (i = 0; i < nwritten; ++i)
20836 {
20837 glyph[i].object = elt;
20838 glyph[i].charpos = charpos;
20839 }
20840
20841 n += nwritten;
20842 }
20843 }
20844 break;
20845 }
20846 }
20847 else /* c == 0 */
20848 break;
20849 }
20850 }
20851 }
20852 break;
20853
20854 case Lisp_Symbol:
20855 /* A symbol: process the value of the symbol recursively
20856 as if it appeared here directly. Avoid error if symbol void.
20857 Special case: if value of symbol is a string, output the string
20858 literally. */
20859 {
20860 register Lisp_Object tem;
20861
20862 /* If the variable is not marked as risky to set
20863 then its contents are risky to use. */
20864 if (NILP (Fget (elt, Qrisky_local_variable)))
20865 risky = 1;
20866
20867 tem = Fboundp (elt);
20868 if (!NILP (tem))
20869 {
20870 tem = Fsymbol_value (elt);
20871 /* If value is a string, output that string literally:
20872 don't check for % within it. */
20873 if (STRINGP (tem))
20874 literal = 1;
20875
20876 if (!EQ (tem, elt))
20877 {
20878 /* Give up right away for nil or t. */
20879 elt = tem;
20880 goto tail_recurse;
20881 }
20882 }
20883 }
20884 break;
20885
20886 case Lisp_Cons:
20887 {
20888 register Lisp_Object car, tem;
20889
20890 /* A cons cell: five distinct cases.
20891 If first element is :eval or :propertize, do something special.
20892 If first element is a string or a cons, process all the elements
20893 and effectively concatenate them.
20894 If first element is a negative number, truncate displaying cdr to
20895 at most that many characters. If positive, pad (with spaces)
20896 to at least that many characters.
20897 If first element is a symbol, process the cadr or caddr recursively
20898 according to whether the symbol's value is non-nil or nil. */
20899 car = XCAR (elt);
20900 if (EQ (car, QCeval))
20901 {
20902 /* An element of the form (:eval FORM) means evaluate FORM
20903 and use the result as mode line elements. */
20904
20905 if (risky)
20906 break;
20907
20908 if (CONSP (XCDR (elt)))
20909 {
20910 Lisp_Object spec;
20911 spec = safe_eval (XCAR (XCDR (elt)));
20912 n += display_mode_element (it, depth, field_width - n,
20913 precision - n, spec, props,
20914 risky);
20915 }
20916 }
20917 else if (EQ (car, QCpropertize))
20918 {
20919 /* An element of the form (:propertize ELT PROPS...)
20920 means display ELT but applying properties PROPS. */
20921
20922 if (risky)
20923 break;
20924
20925 if (CONSP (XCDR (elt)))
20926 n += display_mode_element (it, depth, field_width - n,
20927 precision - n, XCAR (XCDR (elt)),
20928 XCDR (XCDR (elt)), risky);
20929 }
20930 else if (SYMBOLP (car))
20931 {
20932 tem = Fboundp (car);
20933 elt = XCDR (elt);
20934 if (!CONSP (elt))
20935 goto invalid;
20936 /* elt is now the cdr, and we know it is a cons cell.
20937 Use its car if CAR has a non-nil value. */
20938 if (!NILP (tem))
20939 {
20940 tem = Fsymbol_value (car);
20941 if (!NILP (tem))
20942 {
20943 elt = XCAR (elt);
20944 goto tail_recurse;
20945 }
20946 }
20947 /* Symbol's value is nil (or symbol is unbound)
20948 Get the cddr of the original list
20949 and if possible find the caddr and use that. */
20950 elt = XCDR (elt);
20951 if (NILP (elt))
20952 break;
20953 else if (!CONSP (elt))
20954 goto invalid;
20955 elt = XCAR (elt);
20956 goto tail_recurse;
20957 }
20958 else if (INTEGERP (car))
20959 {
20960 register int lim = XINT (car);
20961 elt = XCDR (elt);
20962 if (lim < 0)
20963 {
20964 /* Negative int means reduce maximum width. */
20965 if (precision <= 0)
20966 precision = -lim;
20967 else
20968 precision = min (precision, -lim);
20969 }
20970 else if (lim > 0)
20971 {
20972 /* Padding specified. Don't let it be more than
20973 current maximum. */
20974 if (precision > 0)
20975 lim = min (precision, lim);
20976
20977 /* If that's more padding than already wanted, queue it.
20978 But don't reduce padding already specified even if
20979 that is beyond the current truncation point. */
20980 field_width = max (lim, field_width);
20981 }
20982 goto tail_recurse;
20983 }
20984 else if (STRINGP (car) || CONSP (car))
20985 {
20986 Lisp_Object halftail = elt;
20987 int len = 0;
20988
20989 while (CONSP (elt)
20990 && (precision <= 0 || n < precision))
20991 {
20992 n += display_mode_element (it, depth,
20993 /* Do padding only after the last
20994 element in the list. */
20995 (! CONSP (XCDR (elt))
20996 ? field_width - n
20997 : 0),
20998 precision - n, XCAR (elt),
20999 props, risky);
21000 elt = XCDR (elt);
21001 len++;
21002 if ((len & 1) == 0)
21003 halftail = XCDR (halftail);
21004 /* Check for cycle. */
21005 if (EQ (halftail, elt))
21006 break;
21007 }
21008 }
21009 }
21010 break;
21011
21012 default:
21013 invalid:
21014 elt = build_string ("*invalid*");
21015 goto tail_recurse;
21016 }
21017
21018 /* Pad to FIELD_WIDTH. */
21019 if (field_width > 0 && n < field_width)
21020 {
21021 switch (mode_line_target)
21022 {
21023 case MODE_LINE_NOPROP:
21024 case MODE_LINE_TITLE:
21025 n += store_mode_line_noprop ("", field_width - n, 0);
21026 break;
21027 case MODE_LINE_STRING:
21028 n += store_mode_line_string ("", Qnil, 0, field_width - n, 0, Qnil);
21029 break;
21030 case MODE_LINE_DISPLAY:
21031 n += display_string ("", Qnil, Qnil, 0, 0, it, field_width - n,
21032 0, 0, 0);
21033 break;
21034 }
21035 }
21036
21037 return n;
21038 }
21039
21040 /* Store a mode-line string element in mode_line_string_list.
21041
21042 If STRING is non-null, display that C string. Otherwise, the Lisp
21043 string LISP_STRING is displayed.
21044
21045 FIELD_WIDTH is the minimum number of output glyphs to produce.
21046 If STRING has fewer characters than FIELD_WIDTH, pad to the right
21047 with spaces. FIELD_WIDTH <= 0 means don't pad.
21048
21049 PRECISION is the maximum number of characters to output from
21050 STRING. PRECISION <= 0 means don't truncate the string.
21051
21052 If COPY_STRING is non-zero, make a copy of LISP_STRING before adding
21053 properties to the string.
21054
21055 PROPS are the properties to add to the string.
21056 The mode_line_string_face face property is always added to the string.
21057 */
21058
21059 static int
21060 store_mode_line_string (const char *string, Lisp_Object lisp_string, int copy_string,
21061 int field_width, int precision, Lisp_Object props)
21062 {
21063 ptrdiff_t len;
21064 int n = 0;
21065
21066 if (string != NULL)
21067 {
21068 len = strlen (string);
21069 if (precision > 0 && len > precision)
21070 len = precision;
21071 lisp_string = make_string (string, len);
21072 if (NILP (props))
21073 props = mode_line_string_face_prop;
21074 else if (!NILP (mode_line_string_face))
21075 {
21076 Lisp_Object face = Fplist_get (props, Qface);
21077 props = Fcopy_sequence (props);
21078 if (NILP (face))
21079 face = mode_line_string_face;
21080 else
21081 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
21082 props = Fplist_put (props, Qface, face);
21083 }
21084 Fadd_text_properties (make_number (0), make_number (len),
21085 props, lisp_string);
21086 }
21087 else
21088 {
21089 len = XFASTINT (Flength (lisp_string));
21090 if (precision > 0 && len > precision)
21091 {
21092 len = precision;
21093 lisp_string = Fsubstring (lisp_string, make_number (0), make_number (len));
21094 precision = -1;
21095 }
21096 if (!NILP (mode_line_string_face))
21097 {
21098 Lisp_Object face;
21099 if (NILP (props))
21100 props = Ftext_properties_at (make_number (0), lisp_string);
21101 face = Fplist_get (props, Qface);
21102 if (NILP (face))
21103 face = mode_line_string_face;
21104 else
21105 face = Fcons (face, Fcons (mode_line_string_face, Qnil));
21106 props = Fcons (Qface, Fcons (face, Qnil));
21107 if (copy_string)
21108 lisp_string = Fcopy_sequence (lisp_string);
21109 }
21110 if (!NILP (props))
21111 Fadd_text_properties (make_number (0), make_number (len),
21112 props, lisp_string);
21113 }
21114
21115 if (len > 0)
21116 {
21117 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21118 n += len;
21119 }
21120
21121 if (field_width > len)
21122 {
21123 field_width -= len;
21124 lisp_string = Fmake_string (make_number (field_width), make_number (' '));
21125 if (!NILP (props))
21126 Fadd_text_properties (make_number (0), make_number (field_width),
21127 props, lisp_string);
21128 mode_line_string_list = Fcons (lisp_string, mode_line_string_list);
21129 n += field_width;
21130 }
21131
21132 return n;
21133 }
21134
21135
21136 DEFUN ("format-mode-line", Fformat_mode_line, Sformat_mode_line,
21137 1, 4, 0,
21138 doc: /* Format a string out of a mode line format specification.
21139 First arg FORMAT specifies the mode line format (see `mode-line-format'
21140 for details) to use.
21141
21142 By default, the format is evaluated for the currently selected window.
21143
21144 Optional second arg FACE specifies the face property to put on all
21145 characters for which no face is specified. The value nil means the
21146 default face. The value t means whatever face the window's mode line
21147 currently uses (either `mode-line' or `mode-line-inactive',
21148 depending on whether the window is the selected window or not).
21149 An integer value means the value string has no text
21150 properties.
21151
21152 Optional third and fourth args WINDOW and BUFFER specify the window
21153 and buffer to use as the context for the formatting (defaults
21154 are the selected window and the WINDOW's buffer). */)
21155 (Lisp_Object format, Lisp_Object face,
21156 Lisp_Object window, Lisp_Object buffer)
21157 {
21158 struct it it;
21159 int len;
21160 struct window *w;
21161 struct buffer *old_buffer = NULL;
21162 int face_id;
21163 int no_props = INTEGERP (face);
21164 ptrdiff_t count = SPECPDL_INDEX ();
21165 Lisp_Object str;
21166 int string_start = 0;
21167
21168 w = decode_any_window (window);
21169 XSETWINDOW (window, w);
21170
21171 if (NILP (buffer))
21172 buffer = w->buffer;
21173 CHECK_BUFFER (buffer);
21174
21175 /* Make formatting the modeline a non-op when noninteractive, otherwise
21176 there will be problems later caused by a partially initialized frame. */
21177 if (NILP (format) || noninteractive)
21178 return empty_unibyte_string;
21179
21180 if (no_props)
21181 face = Qnil;
21182
21183 face_id = (NILP (face) || EQ (face, Qdefault)) ? DEFAULT_FACE_ID
21184 : EQ (face, Qt) ? (EQ (window, selected_window)
21185 ? MODE_LINE_FACE_ID : MODE_LINE_INACTIVE_FACE_ID)
21186 : EQ (face, Qmode_line) ? MODE_LINE_FACE_ID
21187 : EQ (face, Qmode_line_inactive) ? MODE_LINE_INACTIVE_FACE_ID
21188 : EQ (face, Qheader_line) ? HEADER_LINE_FACE_ID
21189 : EQ (face, Qtool_bar) ? TOOL_BAR_FACE_ID
21190 : DEFAULT_FACE_ID;
21191
21192 old_buffer = current_buffer;
21193
21194 /* Save things including mode_line_proptrans_alist,
21195 and set that to nil so that we don't alter the outer value. */
21196 record_unwind_protect (unwind_format_mode_line,
21197 format_mode_line_unwind_data
21198 (XFRAME (WINDOW_FRAME (w)),
21199 old_buffer, selected_window, 1));
21200 mode_line_proptrans_alist = Qnil;
21201
21202 Fselect_window (window, Qt);
21203 set_buffer_internal_1 (XBUFFER (buffer));
21204
21205 init_iterator (&it, w, -1, -1, NULL, face_id);
21206
21207 if (no_props)
21208 {
21209 mode_line_target = MODE_LINE_NOPROP;
21210 mode_line_string_face_prop = Qnil;
21211 mode_line_string_list = Qnil;
21212 string_start = MODE_LINE_NOPROP_LEN (0);
21213 }
21214 else
21215 {
21216 mode_line_target = MODE_LINE_STRING;
21217 mode_line_string_list = Qnil;
21218 mode_line_string_face = face;
21219 mode_line_string_face_prop
21220 = (NILP (face) ? Qnil : Fcons (Qface, Fcons (face, Qnil)));
21221 }
21222
21223 push_kboard (FRAME_KBOARD (it.f));
21224 display_mode_element (&it, 0, 0, 0, format, Qnil, 0);
21225 pop_kboard ();
21226
21227 if (no_props)
21228 {
21229 len = MODE_LINE_NOPROP_LEN (string_start);
21230 str = make_string (mode_line_noprop_buf + string_start, len);
21231 }
21232 else
21233 {
21234 mode_line_string_list = Fnreverse (mode_line_string_list);
21235 str = Fmapconcat (intern ("identity"), mode_line_string_list,
21236 empty_unibyte_string);
21237 }
21238
21239 unbind_to (count, Qnil);
21240 return str;
21241 }
21242
21243 /* Write a null-terminated, right justified decimal representation of
21244 the positive integer D to BUF using a minimal field width WIDTH. */
21245
21246 static void
21247 pint2str (register char *buf, register int width, register ptrdiff_t d)
21248 {
21249 register char *p = buf;
21250
21251 if (d <= 0)
21252 *p++ = '0';
21253 else
21254 {
21255 while (d > 0)
21256 {
21257 *p++ = d % 10 + '0';
21258 d /= 10;
21259 }
21260 }
21261
21262 for (width -= (int) (p - buf); width > 0; --width)
21263 *p++ = ' ';
21264 *p-- = '\0';
21265 while (p > buf)
21266 {
21267 d = *buf;
21268 *buf++ = *p;
21269 *p-- = d;
21270 }
21271 }
21272
21273 /* Write a null-terminated, right justified decimal and "human
21274 readable" representation of the nonnegative integer D to BUF using
21275 a minimal field width WIDTH. D should be smaller than 999.5e24. */
21276
21277 static const char power_letter[] =
21278 {
21279 0, /* no letter */
21280 'k', /* kilo */
21281 'M', /* mega */
21282 'G', /* giga */
21283 'T', /* tera */
21284 'P', /* peta */
21285 'E', /* exa */
21286 'Z', /* zetta */
21287 'Y' /* yotta */
21288 };
21289
21290 static void
21291 pint2hrstr (char *buf, int width, ptrdiff_t d)
21292 {
21293 /* We aim to represent the nonnegative integer D as
21294 QUOTIENT.TENTHS * 10 ^ (3 * EXPONENT). */
21295 ptrdiff_t quotient = d;
21296 int remainder = 0;
21297 /* -1 means: do not use TENTHS. */
21298 int tenths = -1;
21299 int exponent = 0;
21300
21301 /* Length of QUOTIENT.TENTHS as a string. */
21302 int length;
21303
21304 char * psuffix;
21305 char * p;
21306
21307 if (1000 <= quotient)
21308 {
21309 /* Scale to the appropriate EXPONENT. */
21310 do
21311 {
21312 remainder = quotient % 1000;
21313 quotient /= 1000;
21314 exponent++;
21315 }
21316 while (1000 <= quotient);
21317
21318 /* Round to nearest and decide whether to use TENTHS or not. */
21319 if (quotient <= 9)
21320 {
21321 tenths = remainder / 100;
21322 if (50 <= remainder % 100)
21323 {
21324 if (tenths < 9)
21325 tenths++;
21326 else
21327 {
21328 quotient++;
21329 if (quotient == 10)
21330 tenths = -1;
21331 else
21332 tenths = 0;
21333 }
21334 }
21335 }
21336 else
21337 if (500 <= remainder)
21338 {
21339 if (quotient < 999)
21340 quotient++;
21341 else
21342 {
21343 quotient = 1;
21344 exponent++;
21345 tenths = 0;
21346 }
21347 }
21348 }
21349
21350 /* Calculate the LENGTH of QUOTIENT.TENTHS as a string. */
21351 if (tenths == -1 && quotient <= 99)
21352 if (quotient <= 9)
21353 length = 1;
21354 else
21355 length = 2;
21356 else
21357 length = 3;
21358 p = psuffix = buf + max (width, length);
21359
21360 /* Print EXPONENT. */
21361 *psuffix++ = power_letter[exponent];
21362 *psuffix = '\0';
21363
21364 /* Print TENTHS. */
21365 if (tenths >= 0)
21366 {
21367 *--p = '0' + tenths;
21368 *--p = '.';
21369 }
21370
21371 /* Print QUOTIENT. */
21372 do
21373 {
21374 int digit = quotient % 10;
21375 *--p = '0' + digit;
21376 }
21377 while ((quotient /= 10) != 0);
21378
21379 /* Print leading spaces. */
21380 while (buf < p)
21381 *--p = ' ';
21382 }
21383
21384 /* Set a mnemonic character for coding_system (Lisp symbol) in BUF.
21385 If EOL_FLAG is 1, set also a mnemonic character for end-of-line
21386 type of CODING_SYSTEM. Return updated pointer into BUF. */
21387
21388 static unsigned char invalid_eol_type[] = "(*invalid*)";
21389
21390 static char *
21391 decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_flag)
21392 {
21393 Lisp_Object val;
21394 int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters));
21395 const unsigned char *eol_str;
21396 int eol_str_len;
21397 /* The EOL conversion we are using. */
21398 Lisp_Object eoltype;
21399
21400 val = CODING_SYSTEM_SPEC (coding_system);
21401 eoltype = Qnil;
21402
21403 if (!VECTORP (val)) /* Not yet decided. */
21404 {
21405 *buf++ = multibyte ? '-' : ' ';
21406 if (eol_flag)
21407 eoltype = eol_mnemonic_undecided;
21408 /* Don't mention EOL conversion if it isn't decided. */
21409 }
21410 else
21411 {
21412 Lisp_Object attrs;
21413 Lisp_Object eolvalue;
21414
21415 attrs = AREF (val, 0);
21416 eolvalue = AREF (val, 2);
21417
21418 *buf++ = multibyte
21419 ? XFASTINT (CODING_ATTR_MNEMONIC (attrs))
21420 : ' ';
21421
21422 if (eol_flag)
21423 {
21424 /* The EOL conversion that is normal on this system. */
21425
21426 if (NILP (eolvalue)) /* Not yet decided. */
21427 eoltype = eol_mnemonic_undecided;
21428 else if (VECTORP (eolvalue)) /* Not yet decided. */
21429 eoltype = eol_mnemonic_undecided;
21430 else /* eolvalue is Qunix, Qdos, or Qmac. */
21431 eoltype = (EQ (eolvalue, Qunix)
21432 ? eol_mnemonic_unix
21433 : (EQ (eolvalue, Qdos) == 1
21434 ? eol_mnemonic_dos : eol_mnemonic_mac));
21435 }
21436 }
21437
21438 if (eol_flag)
21439 {
21440 /* Mention the EOL conversion if it is not the usual one. */
21441 if (STRINGP (eoltype))
21442 {
21443 eol_str = SDATA (eoltype);
21444 eol_str_len = SBYTES (eoltype);
21445 }
21446 else if (CHARACTERP (eoltype))
21447 {
21448 unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
21449 int c = XFASTINT (eoltype);
21450 eol_str_len = CHAR_STRING (c, tmp);
21451 eol_str = tmp;
21452 }
21453 else
21454 {
21455 eol_str = invalid_eol_type;
21456 eol_str_len = sizeof (invalid_eol_type) - 1;
21457 }
21458 memcpy (buf, eol_str, eol_str_len);
21459 buf += eol_str_len;
21460 }
21461
21462 return buf;
21463 }
21464
21465 /* Return a string for the output of a mode line %-spec for window W,
21466 generated by character C. FIELD_WIDTH > 0 means pad the string
21467 returned with spaces to that value. Return a Lisp string in
21468 *STRING if the resulting string is taken from that Lisp string.
21469
21470 Note we operate on the current buffer for most purposes,
21471 the exception being w->base_line_pos. */
21472
21473 static char lots_of_dashes[] = "--------------------------------------------------------------------------------------------------------------------------------------------";
21474
21475 static const char *
21476 decode_mode_spec (struct window *w, register int c, int field_width,
21477 Lisp_Object *string)
21478 {
21479 Lisp_Object obj;
21480 struct frame *f = XFRAME (WINDOW_FRAME (w));
21481 char *decode_mode_spec_buf = f->decode_mode_spec_buffer;
21482 /* We are going to use f->decode_mode_spec_buffer as the buffer to
21483 produce strings from numerical values, so limit preposterously
21484 large values of FIELD_WIDTH to avoid overrunning the buffer's
21485 end. The size of the buffer is enough for FRAME_MESSAGE_BUF_SIZE
21486 bytes plus the terminating null. */
21487 int width = min (field_width, FRAME_MESSAGE_BUF_SIZE (f));
21488 struct buffer *b = current_buffer;
21489
21490 obj = Qnil;
21491 *string = Qnil;
21492
21493 switch (c)
21494 {
21495 case '*':
21496 if (!NILP (BVAR (b, read_only)))
21497 return "%";
21498 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21499 return "*";
21500 return "-";
21501
21502 case '+':
21503 /* This differs from %* only for a modified read-only buffer. */
21504 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21505 return "*";
21506 if (!NILP (BVAR (b, read_only)))
21507 return "%";
21508 return "-";
21509
21510 case '&':
21511 /* This differs from %* in ignoring read-only-ness. */
21512 if (BUF_MODIFF (b) > BUF_SAVE_MODIFF (b))
21513 return "*";
21514 return "-";
21515
21516 case '%':
21517 return "%";
21518
21519 case '[':
21520 {
21521 int i;
21522 char *p;
21523
21524 if (command_loop_level > 5)
21525 return "[[[... ";
21526 p = decode_mode_spec_buf;
21527 for (i = 0; i < command_loop_level; i++)
21528 *p++ = '[';
21529 *p = 0;
21530 return decode_mode_spec_buf;
21531 }
21532
21533 case ']':
21534 {
21535 int i;
21536 char *p;
21537
21538 if (command_loop_level > 5)
21539 return " ...]]]";
21540 p = decode_mode_spec_buf;
21541 for (i = 0; i < command_loop_level; i++)
21542 *p++ = ']';
21543 *p = 0;
21544 return decode_mode_spec_buf;
21545 }
21546
21547 case '-':
21548 {
21549 register int i;
21550
21551 /* Let lots_of_dashes be a string of infinite length. */
21552 if (mode_line_target == MODE_LINE_NOPROP ||
21553 mode_line_target == MODE_LINE_STRING)
21554 return "--";
21555 if (field_width <= 0
21556 || field_width > sizeof (lots_of_dashes))
21557 {
21558 for (i = 0; i < FRAME_MESSAGE_BUF_SIZE (f) - 1; ++i)
21559 decode_mode_spec_buf[i] = '-';
21560 decode_mode_spec_buf[i] = '\0';
21561 return decode_mode_spec_buf;
21562 }
21563 else
21564 return lots_of_dashes;
21565 }
21566
21567 case 'b':
21568 obj = BVAR (b, name);
21569 break;
21570
21571 case 'c':
21572 /* %c and %l are ignored in `frame-title-format'.
21573 (In redisplay_internal, the frame title is drawn _before_ the
21574 windows are updated, so the stuff which depends on actual
21575 window contents (such as %l) may fail to render properly, or
21576 even crash emacs.) */
21577 if (mode_line_target == MODE_LINE_TITLE)
21578 return "";
21579 else
21580 {
21581 ptrdiff_t col = current_column ();
21582 wset_column_number_displayed (w, make_number (col));
21583 pint2str (decode_mode_spec_buf, width, col);
21584 return decode_mode_spec_buf;
21585 }
21586
21587 case 'e':
21588 #ifndef SYSTEM_MALLOC
21589 {
21590 if (NILP (Vmemory_full))
21591 return "";
21592 else
21593 return "!MEM FULL! ";
21594 }
21595 #else
21596 return "";
21597 #endif
21598
21599 case 'F':
21600 /* %F displays the frame name. */
21601 if (!NILP (f->title))
21602 return SSDATA (f->title);
21603 if (f->explicit_name || ! FRAME_WINDOW_P (f))
21604 return SSDATA (f->name);
21605 return "Emacs";
21606
21607 case 'f':
21608 obj = BVAR (b, filename);
21609 break;
21610
21611 case 'i':
21612 {
21613 ptrdiff_t size = ZV - BEGV;
21614 pint2str (decode_mode_spec_buf, width, size);
21615 return decode_mode_spec_buf;
21616 }
21617
21618 case 'I':
21619 {
21620 ptrdiff_t size = ZV - BEGV;
21621 pint2hrstr (decode_mode_spec_buf, width, size);
21622 return decode_mode_spec_buf;
21623 }
21624
21625 case 'l':
21626 {
21627 ptrdiff_t startpos, startpos_byte, line, linepos, linepos_byte;
21628 ptrdiff_t topline, nlines, height;
21629 ptrdiff_t junk;
21630
21631 /* %c and %l are ignored in `frame-title-format'. */
21632 if (mode_line_target == MODE_LINE_TITLE)
21633 return "";
21634
21635 startpos = XMARKER (w->start)->charpos;
21636 startpos_byte = marker_byte_position (w->start);
21637 height = WINDOW_TOTAL_LINES (w);
21638
21639 /* If we decided that this buffer isn't suitable for line numbers,
21640 don't forget that too fast. */
21641 if (EQ (w->base_line_pos, w->buffer))
21642 goto no_value;
21643 /* But do forget it, if the window shows a different buffer now. */
21644 else if (BUFFERP (w->base_line_pos))
21645 wset_base_line_pos (w, Qnil);
21646
21647 /* If the buffer is very big, don't waste time. */
21648 if (INTEGERP (Vline_number_display_limit)
21649 && BUF_ZV (b) - BUF_BEGV (b) > XINT (Vline_number_display_limit))
21650 {
21651 wset_base_line_pos (w, Qnil);
21652 wset_base_line_number (w, Qnil);
21653 goto no_value;
21654 }
21655
21656 if (INTEGERP (w->base_line_number)
21657 && INTEGERP (w->base_line_pos)
21658 && XFASTINT (w->base_line_pos) <= startpos)
21659 {
21660 line = XFASTINT (w->base_line_number);
21661 linepos = XFASTINT (w->base_line_pos);
21662 linepos_byte = buf_charpos_to_bytepos (b, linepos);
21663 }
21664 else
21665 {
21666 line = 1;
21667 linepos = BUF_BEGV (b);
21668 linepos_byte = BUF_BEGV_BYTE (b);
21669 }
21670
21671 /* Count lines from base line to window start position. */
21672 nlines = display_count_lines (linepos_byte,
21673 startpos_byte,
21674 startpos, &junk);
21675
21676 topline = nlines + line;
21677
21678 /* Determine a new base line, if the old one is too close
21679 or too far away, or if we did not have one.
21680 "Too close" means it's plausible a scroll-down would
21681 go back past it. */
21682 if (startpos == BUF_BEGV (b))
21683 {
21684 wset_base_line_number (w, make_number (topline));
21685 wset_base_line_pos (w, make_number (BUF_BEGV (b)));
21686 }
21687 else if (nlines < height + 25 || nlines > height * 3 + 50
21688 || linepos == BUF_BEGV (b))
21689 {
21690 ptrdiff_t limit = BUF_BEGV (b);
21691 ptrdiff_t limit_byte = BUF_BEGV_BYTE (b);
21692 ptrdiff_t position;
21693 ptrdiff_t distance =
21694 (height * 2 + 30) * line_number_display_limit_width;
21695
21696 if (startpos - distance > limit)
21697 {
21698 limit = startpos - distance;
21699 limit_byte = CHAR_TO_BYTE (limit);
21700 }
21701
21702 nlines = display_count_lines (startpos_byte,
21703 limit_byte,
21704 - (height * 2 + 30),
21705 &position);
21706 /* If we couldn't find the lines we wanted within
21707 line_number_display_limit_width chars per line,
21708 give up on line numbers for this window. */
21709 if (position == limit_byte && limit == startpos - distance)
21710 {
21711 wset_base_line_pos (w, w->buffer);
21712 wset_base_line_number (w, Qnil);
21713 goto no_value;
21714 }
21715
21716 wset_base_line_number (w, make_number (topline - nlines));
21717 wset_base_line_pos (w, make_number (BYTE_TO_CHAR (position)));
21718 }
21719
21720 /* Now count lines from the start pos to point. */
21721 nlines = display_count_lines (startpos_byte,
21722 PT_BYTE, PT, &junk);
21723
21724 /* Record that we did display the line number. */
21725 line_number_displayed = 1;
21726
21727 /* Make the string to show. */
21728 pint2str (decode_mode_spec_buf, width, topline + nlines);
21729 return decode_mode_spec_buf;
21730 no_value:
21731 {
21732 char* p = decode_mode_spec_buf;
21733 int pad = width - 2;
21734 while (pad-- > 0)
21735 *p++ = ' ';
21736 *p++ = '?';
21737 *p++ = '?';
21738 *p = '\0';
21739 return decode_mode_spec_buf;
21740 }
21741 }
21742 break;
21743
21744 case 'm':
21745 obj = BVAR (b, mode_name);
21746 break;
21747
21748 case 'n':
21749 if (BUF_BEGV (b) > BUF_BEG (b) || BUF_ZV (b) < BUF_Z (b))
21750 return " Narrow";
21751 break;
21752
21753 case 'p':
21754 {
21755 ptrdiff_t pos = marker_position (w->start);
21756 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21757
21758 if (XFASTINT (w->window_end_pos) <= BUF_Z (b) - BUF_ZV (b))
21759 {
21760 if (pos <= BUF_BEGV (b))
21761 return "All";
21762 else
21763 return "Bottom";
21764 }
21765 else if (pos <= BUF_BEGV (b))
21766 return "Top";
21767 else
21768 {
21769 if (total > 1000000)
21770 /* Do it differently for a large value, to avoid overflow. */
21771 total = ((pos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21772 else
21773 total = ((pos - BUF_BEGV (b)) * 100 + total - 1) / total;
21774 /* We can't normally display a 3-digit number,
21775 so get us a 2-digit number that is close. */
21776 if (total == 100)
21777 total = 99;
21778 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21779 return decode_mode_spec_buf;
21780 }
21781 }
21782
21783 /* Display percentage of size above the bottom of the screen. */
21784 case 'P':
21785 {
21786 ptrdiff_t toppos = marker_position (w->start);
21787 ptrdiff_t botpos = BUF_Z (b) - XFASTINT (w->window_end_pos);
21788 ptrdiff_t total = BUF_ZV (b) - BUF_BEGV (b);
21789
21790 if (botpos >= BUF_ZV (b))
21791 {
21792 if (toppos <= BUF_BEGV (b))
21793 return "All";
21794 else
21795 return "Bottom";
21796 }
21797 else
21798 {
21799 if (total > 1000000)
21800 /* Do it differently for a large value, to avoid overflow. */
21801 total = ((botpos - BUF_BEGV (b)) + (total / 100) - 1) / (total / 100);
21802 else
21803 total = ((botpos - BUF_BEGV (b)) * 100 + total - 1) / total;
21804 /* We can't normally display a 3-digit number,
21805 so get us a 2-digit number that is close. */
21806 if (total == 100)
21807 total = 99;
21808 if (toppos <= BUF_BEGV (b))
21809 sprintf (decode_mode_spec_buf, "Top%2"pD"d%%", total);
21810 else
21811 sprintf (decode_mode_spec_buf, "%2"pD"d%%", total);
21812 return decode_mode_spec_buf;
21813 }
21814 }
21815
21816 case 's':
21817 /* status of process */
21818 obj = Fget_buffer_process (Fcurrent_buffer ());
21819 if (NILP (obj))
21820 return "no process";
21821 #ifndef MSDOS
21822 obj = Fsymbol_name (Fprocess_status (obj));
21823 #endif
21824 break;
21825
21826 case '@':
21827 {
21828 ptrdiff_t count = inhibit_garbage_collection ();
21829 Lisp_Object val = call1 (intern ("file-remote-p"),
21830 BVAR (current_buffer, directory));
21831 unbind_to (count, Qnil);
21832
21833 if (NILP (val))
21834 return "-";
21835 else
21836 return "@";
21837 }
21838
21839 case 't': /* indicate TEXT or BINARY */
21840 return "T";
21841
21842 case 'z':
21843 /* coding-system (not including end-of-line format) */
21844 case 'Z':
21845 /* coding-system (including end-of-line type) */
21846 {
21847 int eol_flag = (c == 'Z');
21848 char *p = decode_mode_spec_buf;
21849
21850 if (! FRAME_WINDOW_P (f))
21851 {
21852 /* No need to mention EOL here--the terminal never needs
21853 to do EOL conversion. */
21854 p = decode_mode_spec_coding (CODING_ID_NAME
21855 (FRAME_KEYBOARD_CODING (f)->id),
21856 p, 0);
21857 p = decode_mode_spec_coding (CODING_ID_NAME
21858 (FRAME_TERMINAL_CODING (f)->id),
21859 p, 0);
21860 }
21861 p = decode_mode_spec_coding (BVAR (b, buffer_file_coding_system),
21862 p, eol_flag);
21863
21864 #if 0 /* This proves to be annoying; I think we can do without. -- rms. */
21865 #ifdef subprocesses
21866 obj = Fget_buffer_process (Fcurrent_buffer ());
21867 if (PROCESSP (obj))
21868 {
21869 p = decode_mode_spec_coding
21870 (XPROCESS (obj)->decode_coding_system, p, eol_flag);
21871 p = decode_mode_spec_coding
21872 (XPROCESS (obj)->encode_coding_system, p, eol_flag);
21873 }
21874 #endif /* subprocesses */
21875 #endif /* 0 */
21876 *p = 0;
21877 return decode_mode_spec_buf;
21878 }
21879 }
21880
21881 if (STRINGP (obj))
21882 {
21883 *string = obj;
21884 return SSDATA (obj);
21885 }
21886 else
21887 return "";
21888 }
21889
21890
21891 /* Count up to COUNT lines starting from START_BYTE.
21892 But don't go beyond LIMIT_BYTE.
21893 Return the number of lines thus found (always nonnegative).
21894
21895 Set *BYTE_POS_PTR to 1 if we found COUNT lines, 0 if we hit LIMIT. */
21896
21897 static ptrdiff_t
21898 display_count_lines (ptrdiff_t start_byte,
21899 ptrdiff_t limit_byte, ptrdiff_t count,
21900 ptrdiff_t *byte_pos_ptr)
21901 {
21902 register unsigned char *cursor;
21903 unsigned char *base;
21904
21905 register ptrdiff_t ceiling;
21906 register unsigned char *ceiling_addr;
21907 ptrdiff_t orig_count = count;
21908
21909 /* If we are not in selective display mode,
21910 check only for newlines. */
21911 int selective_display = (!NILP (BVAR (current_buffer, selective_display))
21912 && !INTEGERP (BVAR (current_buffer, selective_display)));
21913
21914 if (count > 0)
21915 {
21916 while (start_byte < limit_byte)
21917 {
21918 ceiling = BUFFER_CEILING_OF (start_byte);
21919 ceiling = min (limit_byte - 1, ceiling);
21920 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
21921 base = (cursor = BYTE_POS_ADDR (start_byte));
21922 while (1)
21923 {
21924 if (selective_display)
21925 while (*cursor != '\n' && *cursor != 015 && ++cursor != ceiling_addr)
21926 ;
21927 else
21928 while (*cursor != '\n' && ++cursor != ceiling_addr)
21929 ;
21930
21931 if (cursor != ceiling_addr)
21932 {
21933 if (--count == 0)
21934 {
21935 start_byte += cursor - base + 1;
21936 *byte_pos_ptr = start_byte;
21937 return orig_count;
21938 }
21939 else
21940 if (++cursor == ceiling_addr)
21941 break;
21942 }
21943 else
21944 break;
21945 }
21946 start_byte += cursor - base;
21947 }
21948 }
21949 else
21950 {
21951 while (start_byte > limit_byte)
21952 {
21953 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
21954 ceiling = max (limit_byte, ceiling);
21955 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
21956 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
21957 while (1)
21958 {
21959 if (selective_display)
21960 while (--cursor != ceiling_addr
21961 && *cursor != '\n' && *cursor != 015)
21962 ;
21963 else
21964 while (--cursor != ceiling_addr && *cursor != '\n')
21965 ;
21966
21967 if (cursor != ceiling_addr)
21968 {
21969 if (++count == 0)
21970 {
21971 start_byte += cursor - base + 1;
21972 *byte_pos_ptr = start_byte;
21973 /* When scanning backwards, we should
21974 not count the newline posterior to which we stop. */
21975 return - orig_count - 1;
21976 }
21977 }
21978 else
21979 break;
21980 }
21981 /* Here we add 1 to compensate for the last decrement
21982 of CURSOR, which took it past the valid range. */
21983 start_byte += cursor - base + 1;
21984 }
21985 }
21986
21987 *byte_pos_ptr = limit_byte;
21988
21989 if (count < 0)
21990 return - orig_count + count;
21991 return orig_count - count;
21992
21993 }
21994
21995
21996 \f
21997 /***********************************************************************
21998 Displaying strings
21999 ***********************************************************************/
22000
22001 /* Display a NUL-terminated string, starting with index START.
22002
22003 If STRING is non-null, display that C string. Otherwise, the Lisp
22004 string LISP_STRING is displayed. There's a case that STRING is
22005 non-null and LISP_STRING is not nil. It means STRING is a string
22006 data of LISP_STRING. In that case, we display LISP_STRING while
22007 ignoring its text properties.
22008
22009 If FACE_STRING is not nil, FACE_STRING_POS is a position in
22010 FACE_STRING. Display STRING or LISP_STRING with the face at
22011 FACE_STRING_POS in FACE_STRING:
22012
22013 Display the string in the environment given by IT, but use the
22014 standard display table, temporarily.
22015
22016 FIELD_WIDTH is the minimum number of output glyphs to produce.
22017 If STRING has fewer characters than FIELD_WIDTH, pad to the right
22018 with spaces. If STRING has more characters, more than FIELD_WIDTH
22019 glyphs will be produced. FIELD_WIDTH <= 0 means don't pad.
22020
22021 PRECISION is the maximum number of characters to output from
22022 STRING. PRECISION < 0 means don't truncate the string.
22023
22024 This is roughly equivalent to printf format specifiers:
22025
22026 FIELD_WIDTH PRECISION PRINTF
22027 ----------------------------------------
22028 -1 -1 %s
22029 -1 10 %.10s
22030 10 -1 %10s
22031 20 10 %20.10s
22032
22033 MULTIBYTE zero means do not display multibyte chars, > 0 means do
22034 display them, and < 0 means obey the current buffer's value of
22035 enable_multibyte_characters.
22036
22037 Value is the number of columns displayed. */
22038
22039 static int
22040 display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_string,
22041 ptrdiff_t face_string_pos, ptrdiff_t start, struct it *it,
22042 int field_width, int precision, int max_x, int multibyte)
22043 {
22044 int hpos_at_start = it->hpos;
22045 int saved_face_id = it->face_id;
22046 struct glyph_row *row = it->glyph_row;
22047 ptrdiff_t it_charpos;
22048
22049 /* Initialize the iterator IT for iteration over STRING beginning
22050 with index START. */
22051 reseat_to_string (it, NILP (lisp_string) ? string : NULL, lisp_string, start,
22052 precision, field_width, multibyte);
22053 if (string && STRINGP (lisp_string))
22054 /* LISP_STRING is the one returned by decode_mode_spec. We should
22055 ignore its text properties. */
22056 it->stop_charpos = it->end_charpos;
22057
22058 /* If displaying STRING, set up the face of the iterator from
22059 FACE_STRING, if that's given. */
22060 if (STRINGP (face_string))
22061 {
22062 ptrdiff_t endptr;
22063 struct face *face;
22064
22065 it->face_id
22066 = face_at_string_position (it->w, face_string, face_string_pos,
22067 0, it->region_beg_charpos,
22068 it->region_end_charpos,
22069 &endptr, it->base_face_id, 0);
22070 face = FACE_FROM_ID (it->f, it->face_id);
22071 it->face_box_p = face->box != FACE_NO_BOX;
22072 }
22073
22074 /* Set max_x to the maximum allowed X position. Don't let it go
22075 beyond the right edge of the window. */
22076 if (max_x <= 0)
22077 max_x = it->last_visible_x;
22078 else
22079 max_x = min (max_x, it->last_visible_x);
22080
22081 /* Skip over display elements that are not visible. because IT->w is
22082 hscrolled. */
22083 if (it->current_x < it->first_visible_x)
22084 move_it_in_display_line_to (it, 100000, it->first_visible_x,
22085 MOVE_TO_POS | MOVE_TO_X);
22086
22087 row->ascent = it->max_ascent;
22088 row->height = it->max_ascent + it->max_descent;
22089 row->phys_ascent = it->max_phys_ascent;
22090 row->phys_height = it->max_phys_ascent + it->max_phys_descent;
22091 row->extra_line_spacing = it->max_extra_line_spacing;
22092
22093 if (STRINGP (it->string))
22094 it_charpos = IT_STRING_CHARPOS (*it);
22095 else
22096 it_charpos = IT_CHARPOS (*it);
22097
22098 /* This condition is for the case that we are called with current_x
22099 past last_visible_x. */
22100 while (it->current_x < max_x)
22101 {
22102 int x_before, x, n_glyphs_before, i, nglyphs;
22103
22104 /* Get the next display element. */
22105 if (!get_next_display_element (it))
22106 break;
22107
22108 /* Produce glyphs. */
22109 x_before = it->current_x;
22110 n_glyphs_before = row->used[TEXT_AREA];
22111 PRODUCE_GLYPHS (it);
22112
22113 nglyphs = row->used[TEXT_AREA] - n_glyphs_before;
22114 i = 0;
22115 x = x_before;
22116 while (i < nglyphs)
22117 {
22118 struct glyph *glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
22119
22120 if (it->line_wrap != TRUNCATE
22121 && x + glyph->pixel_width > max_x)
22122 {
22123 /* End of continued line or max_x reached. */
22124 if (CHAR_GLYPH_PADDING_P (*glyph))
22125 {
22126 /* A wide character is unbreakable. */
22127 if (row->reversed_p)
22128 unproduce_glyphs (it, row->used[TEXT_AREA]
22129 - n_glyphs_before);
22130 row->used[TEXT_AREA] = n_glyphs_before;
22131 it->current_x = x_before;
22132 }
22133 else
22134 {
22135 if (row->reversed_p)
22136 unproduce_glyphs (it, row->used[TEXT_AREA]
22137 - (n_glyphs_before + i));
22138 row->used[TEXT_AREA] = n_glyphs_before + i;
22139 it->current_x = x;
22140 }
22141 break;
22142 }
22143 else if (x + glyph->pixel_width >= it->first_visible_x)
22144 {
22145 /* Glyph is at least partially visible. */
22146 ++it->hpos;
22147 if (x < it->first_visible_x)
22148 row->x = x - it->first_visible_x;
22149 }
22150 else
22151 {
22152 /* Glyph is off the left margin of the display area.
22153 Should not happen. */
22154 emacs_abort ();
22155 }
22156
22157 row->ascent = max (row->ascent, it->max_ascent);
22158 row->height = max (row->height, it->max_ascent + it->max_descent);
22159 row->phys_ascent = max (row->phys_ascent, it->max_phys_ascent);
22160 row->phys_height = max (row->phys_height,
22161 it->max_phys_ascent + it->max_phys_descent);
22162 row->extra_line_spacing = max (row->extra_line_spacing,
22163 it->max_extra_line_spacing);
22164 x += glyph->pixel_width;
22165 ++i;
22166 }
22167
22168 /* Stop if max_x reached. */
22169 if (i < nglyphs)
22170 break;
22171
22172 /* Stop at line ends. */
22173 if (ITERATOR_AT_END_OF_LINE_P (it))
22174 {
22175 it->continuation_lines_width = 0;
22176 break;
22177 }
22178
22179 set_iterator_to_next (it, 1);
22180 if (STRINGP (it->string))
22181 it_charpos = IT_STRING_CHARPOS (*it);
22182 else
22183 it_charpos = IT_CHARPOS (*it);
22184
22185 /* Stop if truncating at the right edge. */
22186 if (it->line_wrap == TRUNCATE
22187 && it->current_x >= it->last_visible_x)
22188 {
22189 /* Add truncation mark, but don't do it if the line is
22190 truncated at a padding space. */
22191 if (it_charpos < it->string_nchars)
22192 {
22193 if (!FRAME_WINDOW_P (it->f))
22194 {
22195 int ii, n;
22196
22197 if (it->current_x > it->last_visible_x)
22198 {
22199 if (!row->reversed_p)
22200 {
22201 for (ii = row->used[TEXT_AREA] - 1; ii > 0; --ii)
22202 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22203 break;
22204 }
22205 else
22206 {
22207 for (ii = 0; ii < row->used[TEXT_AREA]; ii++)
22208 if (!CHAR_GLYPH_PADDING_P (row->glyphs[TEXT_AREA][ii]))
22209 break;
22210 unproduce_glyphs (it, ii + 1);
22211 ii = row->used[TEXT_AREA] - (ii + 1);
22212 }
22213 for (n = row->used[TEXT_AREA]; ii < n; ++ii)
22214 {
22215 row->used[TEXT_AREA] = ii;
22216 produce_special_glyphs (it, IT_TRUNCATION);
22217 }
22218 }
22219 produce_special_glyphs (it, IT_TRUNCATION);
22220 }
22221 row->truncated_on_right_p = 1;
22222 }
22223 break;
22224 }
22225 }
22226
22227 /* Maybe insert a truncation at the left. */
22228 if (it->first_visible_x
22229 && it_charpos > 0)
22230 {
22231 if (!FRAME_WINDOW_P (it->f)
22232 || (row->reversed_p
22233 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22234 : WINDOW_LEFT_FRINGE_WIDTH (it->w)) == 0)
22235 insert_left_trunc_glyphs (it);
22236 row->truncated_on_left_p = 1;
22237 }
22238
22239 it->face_id = saved_face_id;
22240
22241 /* Value is number of columns displayed. */
22242 return it->hpos - hpos_at_start;
22243 }
22244
22245
22246 \f
22247 /* This is like a combination of memq and assq. Return 1/2 if PROPVAL
22248 appears as an element of LIST or as the car of an element of LIST.
22249 If PROPVAL is a list, compare each element against LIST in that
22250 way, and return 1/2 if any element of PROPVAL is found in LIST.
22251 Otherwise return 0. This function cannot quit.
22252 The return value is 2 if the text is invisible but with an ellipsis
22253 and 1 if it's invisible and without an ellipsis. */
22254
22255 int
22256 invisible_p (register Lisp_Object propval, Lisp_Object list)
22257 {
22258 register Lisp_Object tail, proptail;
22259
22260 for (tail = list; CONSP (tail); tail = XCDR (tail))
22261 {
22262 register Lisp_Object tem;
22263 tem = XCAR (tail);
22264 if (EQ (propval, tem))
22265 return 1;
22266 if (CONSP (tem) && EQ (propval, XCAR (tem)))
22267 return NILP (XCDR (tem)) ? 1 : 2;
22268 }
22269
22270 if (CONSP (propval))
22271 {
22272 for (proptail = propval; CONSP (proptail); proptail = XCDR (proptail))
22273 {
22274 Lisp_Object propelt;
22275 propelt = XCAR (proptail);
22276 for (tail = list; CONSP (tail); tail = XCDR (tail))
22277 {
22278 register Lisp_Object tem;
22279 tem = XCAR (tail);
22280 if (EQ (propelt, tem))
22281 return 1;
22282 if (CONSP (tem) && EQ (propelt, XCAR (tem)))
22283 return NILP (XCDR (tem)) ? 1 : 2;
22284 }
22285 }
22286 }
22287
22288 return 0;
22289 }
22290
22291 DEFUN ("invisible-p", Finvisible_p, Sinvisible_p, 1, 1, 0,
22292 doc: /* Non-nil if the property makes the text invisible.
22293 POS-OR-PROP can be a marker or number, in which case it is taken to be
22294 a position in the current buffer and the value of the `invisible' property
22295 is checked; or it can be some other value, which is then presumed to be the
22296 value of the `invisible' property of the text of interest.
22297 The non-nil value returned can be t for truly invisible text or something
22298 else if the text is replaced by an ellipsis. */)
22299 (Lisp_Object pos_or_prop)
22300 {
22301 Lisp_Object prop
22302 = (NATNUMP (pos_or_prop) || MARKERP (pos_or_prop)
22303 ? Fget_char_property (pos_or_prop, Qinvisible, Qnil)
22304 : pos_or_prop);
22305 int invis = TEXT_PROP_MEANS_INVISIBLE (prop);
22306 return (invis == 0 ? Qnil
22307 : invis == 1 ? Qt
22308 : make_number (invis));
22309 }
22310
22311 /* Calculate a width or height in pixels from a specification using
22312 the following elements:
22313
22314 SPEC ::=
22315 NUM - a (fractional) multiple of the default font width/height
22316 (NUM) - specifies exactly NUM pixels
22317 UNIT - a fixed number of pixels, see below.
22318 ELEMENT - size of a display element in pixels, see below.
22319 (NUM . SPEC) - equals NUM * SPEC
22320 (+ SPEC SPEC ...) - add pixel values
22321 (- SPEC SPEC ...) - subtract pixel values
22322 (- SPEC) - negate pixel value
22323
22324 NUM ::=
22325 INT or FLOAT - a number constant
22326 SYMBOL - use symbol's (buffer local) variable binding.
22327
22328 UNIT ::=
22329 in - pixels per inch *)
22330 mm - pixels per 1/1000 meter *)
22331 cm - pixels per 1/100 meter *)
22332 width - width of current font in pixels.
22333 height - height of current font in pixels.
22334
22335 *) using the ratio(s) defined in display-pixels-per-inch.
22336
22337 ELEMENT ::=
22338
22339 left-fringe - left fringe width in pixels
22340 right-fringe - right fringe width in pixels
22341
22342 left-margin - left margin width in pixels
22343 right-margin - right margin width in pixels
22344
22345 scroll-bar - scroll-bar area width in pixels
22346
22347 Examples:
22348
22349 Pixels corresponding to 5 inches:
22350 (5 . in)
22351
22352 Total width of non-text areas on left side of window (if scroll-bar is on left):
22353 '(space :width (+ left-fringe left-margin scroll-bar))
22354
22355 Align to first text column (in header line):
22356 '(space :align-to 0)
22357
22358 Align to middle of text area minus half the width of variable `my-image'
22359 containing a loaded image:
22360 '(space :align-to (0.5 . (- text my-image)))
22361
22362 Width of left margin minus width of 1 character in the default font:
22363 '(space :width (- left-margin 1))
22364
22365 Width of left margin minus width of 2 characters in the current font:
22366 '(space :width (- left-margin (2 . width)))
22367
22368 Center 1 character over left-margin (in header line):
22369 '(space :align-to (+ left-margin (0.5 . left-margin) -0.5))
22370
22371 Different ways to express width of left fringe plus left margin minus one pixel:
22372 '(space :width (- (+ left-fringe left-margin) (1)))
22373 '(space :width (+ left-fringe left-margin (- (1))))
22374 '(space :width (+ left-fringe left-margin (-1)))
22375
22376 */
22377
22378 #define NUMVAL(X) \
22379 ((INTEGERP (X) || FLOATP (X)) \
22380 ? XFLOATINT (X) \
22381 : - 1)
22382
22383 static int
22384 calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
22385 struct font *font, int width_p, int *align_to)
22386 {
22387 double pixels;
22388
22389 #define OK_PIXELS(val) ((*res = (double)(val)), 1)
22390 #define OK_ALIGN_TO(val) ((*align_to = (int)(val)), 1)
22391
22392 if (NILP (prop))
22393 return OK_PIXELS (0);
22394
22395 eassert (FRAME_LIVE_P (it->f));
22396
22397 if (SYMBOLP (prop))
22398 {
22399 if (SCHARS (SYMBOL_NAME (prop)) == 2)
22400 {
22401 char *unit = SSDATA (SYMBOL_NAME (prop));
22402
22403 if (unit[0] == 'i' && unit[1] == 'n')
22404 pixels = 1.0;
22405 else if (unit[0] == 'm' && unit[1] == 'm')
22406 pixels = 25.4;
22407 else if (unit[0] == 'c' && unit[1] == 'm')
22408 pixels = 2.54;
22409 else
22410 pixels = 0;
22411 if (pixels > 0)
22412 {
22413 double ppi;
22414 #ifdef HAVE_WINDOW_SYSTEM
22415 if (FRAME_WINDOW_P (it->f)
22416 && (ppi = (width_p
22417 ? FRAME_X_DISPLAY_INFO (it->f)->resx
22418 : FRAME_X_DISPLAY_INFO (it->f)->resy),
22419 ppi > 0))
22420 return OK_PIXELS (ppi / pixels);
22421 #endif
22422
22423 if ((ppi = NUMVAL (Vdisplay_pixels_per_inch), ppi > 0)
22424 || (CONSP (Vdisplay_pixels_per_inch)
22425 && (ppi = (width_p
22426 ? NUMVAL (XCAR (Vdisplay_pixels_per_inch))
22427 : NUMVAL (XCDR (Vdisplay_pixels_per_inch))),
22428 ppi > 0)))
22429 return OK_PIXELS (ppi / pixels);
22430
22431 return 0;
22432 }
22433 }
22434
22435 #ifdef HAVE_WINDOW_SYSTEM
22436 if (EQ (prop, Qheight))
22437 return OK_PIXELS (font ? FONT_HEIGHT (font) : FRAME_LINE_HEIGHT (it->f));
22438 if (EQ (prop, Qwidth))
22439 return OK_PIXELS (font ? FONT_WIDTH (font) : FRAME_COLUMN_WIDTH (it->f));
22440 #else
22441 if (EQ (prop, Qheight) || EQ (prop, Qwidth))
22442 return OK_PIXELS (1);
22443 #endif
22444
22445 if (EQ (prop, Qtext))
22446 return OK_PIXELS (width_p
22447 ? window_box_width (it->w, TEXT_AREA)
22448 : WINDOW_BOX_HEIGHT_NO_MODE_LINE (it->w));
22449
22450 if (align_to && *align_to < 0)
22451 {
22452 *res = 0;
22453 if (EQ (prop, Qleft))
22454 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA));
22455 if (EQ (prop, Qright))
22456 return OK_ALIGN_TO (window_box_right_offset (it->w, TEXT_AREA));
22457 if (EQ (prop, Qcenter))
22458 return OK_ALIGN_TO (window_box_left_offset (it->w, TEXT_AREA)
22459 + window_box_width (it->w, TEXT_AREA) / 2);
22460 if (EQ (prop, Qleft_fringe))
22461 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22462 ? WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (it->w)
22463 : window_box_right_offset (it->w, LEFT_MARGIN_AREA));
22464 if (EQ (prop, Qright_fringe))
22465 return OK_ALIGN_TO (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22466 ? window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22467 : window_box_right_offset (it->w, TEXT_AREA));
22468 if (EQ (prop, Qleft_margin))
22469 return OK_ALIGN_TO (window_box_left_offset (it->w, LEFT_MARGIN_AREA));
22470 if (EQ (prop, Qright_margin))
22471 return OK_ALIGN_TO (window_box_left_offset (it->w, RIGHT_MARGIN_AREA));
22472 if (EQ (prop, Qscroll_bar))
22473 return OK_ALIGN_TO (WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (it->w)
22474 ? 0
22475 : (window_box_right_offset (it->w, RIGHT_MARGIN_AREA)
22476 + (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (it->w)
22477 ? WINDOW_RIGHT_FRINGE_WIDTH (it->w)
22478 : 0)));
22479 }
22480 else
22481 {
22482 if (EQ (prop, Qleft_fringe))
22483 return OK_PIXELS (WINDOW_LEFT_FRINGE_WIDTH (it->w));
22484 if (EQ (prop, Qright_fringe))
22485 return OK_PIXELS (WINDOW_RIGHT_FRINGE_WIDTH (it->w));
22486 if (EQ (prop, Qleft_margin))
22487 return OK_PIXELS (WINDOW_LEFT_MARGIN_WIDTH (it->w));
22488 if (EQ (prop, Qright_margin))
22489 return OK_PIXELS (WINDOW_RIGHT_MARGIN_WIDTH (it->w));
22490 if (EQ (prop, Qscroll_bar))
22491 return OK_PIXELS (WINDOW_SCROLL_BAR_AREA_WIDTH (it->w));
22492 }
22493
22494 prop = buffer_local_value_1 (prop, it->w->buffer);
22495 if (EQ (prop, Qunbound))
22496 prop = Qnil;
22497 }
22498
22499 if (INTEGERP (prop) || FLOATP (prop))
22500 {
22501 int base_unit = (width_p
22502 ? FRAME_COLUMN_WIDTH (it->f)
22503 : FRAME_LINE_HEIGHT (it->f));
22504 return OK_PIXELS (XFLOATINT (prop) * base_unit);
22505 }
22506
22507 if (CONSP (prop))
22508 {
22509 Lisp_Object car = XCAR (prop);
22510 Lisp_Object cdr = XCDR (prop);
22511
22512 if (SYMBOLP (car))
22513 {
22514 #ifdef HAVE_WINDOW_SYSTEM
22515 if (FRAME_WINDOW_P (it->f)
22516 && valid_image_p (prop))
22517 {
22518 ptrdiff_t id = lookup_image (it->f, prop);
22519 struct image *img = IMAGE_FROM_ID (it->f, id);
22520
22521 return OK_PIXELS (width_p ? img->width : img->height);
22522 }
22523 #ifdef HAVE_XWIDGETS
22524 if (FRAME_WINDOW_P (it->f) && valid_xwidget_p (prop))
22525 {
22526 printf("calc_pixel_width_or_height: return dummy size FIXME\n");
22527 return OK_PIXELS (width_p ? 100 : 100);
22528 }
22529 #endif
22530 #endif
22531 if (EQ (car, Qplus) || EQ (car, Qminus))
22532 {
22533 int first = 1;
22534 double px;
22535
22536 pixels = 0;
22537 while (CONSP (cdr))
22538 {
22539 if (!calc_pixel_width_or_height (&px, it, XCAR (cdr),
22540 font, width_p, align_to))
22541 return 0;
22542 if (first)
22543 pixels = (EQ (car, Qplus) ? px : -px), first = 0;
22544 else
22545 pixels += px;
22546 cdr = XCDR (cdr);
22547 }
22548 if (EQ (car, Qminus))
22549 pixels = -pixels;
22550 return OK_PIXELS (pixels);
22551 }
22552
22553 car = buffer_local_value_1 (car, it->w->buffer);
22554 if (EQ (car, Qunbound))
22555 car = Qnil;
22556 }
22557
22558 if (INTEGERP (car) || FLOATP (car))
22559 {
22560 double fact;
22561 pixels = XFLOATINT (car);
22562 if (NILP (cdr))
22563 return OK_PIXELS (pixels);
22564 if (calc_pixel_width_or_height (&fact, it, cdr,
22565 font, width_p, align_to))
22566 return OK_PIXELS (pixels * fact);
22567 return 0;
22568 }
22569
22570 return 0;
22571 }
22572
22573 return 0;
22574 }
22575
22576 \f
22577 /***********************************************************************
22578 Glyph Display
22579 ***********************************************************************/
22580
22581 #ifdef HAVE_WINDOW_SYSTEM
22582
22583 #ifdef GLYPH_DEBUG
22584
22585 void
22586 dump_glyph_string (struct glyph_string *s)
22587 {
22588 fprintf (stderr, "glyph string\n");
22589 fprintf (stderr, " x, y, w, h = %d, %d, %d, %d\n",
22590 s->x, s->y, s->width, s->height);
22591 fprintf (stderr, " ybase = %d\n", s->ybase);
22592 fprintf (stderr, " hl = %d\n", s->hl);
22593 fprintf (stderr, " left overhang = %d, right = %d\n",
22594 s->left_overhang, s->right_overhang);
22595 fprintf (stderr, " nchars = %d\n", s->nchars);
22596 fprintf (stderr, " extends to end of line = %d\n",
22597 s->extends_to_end_of_line_p);
22598 fprintf (stderr, " font height = %d\n", FONT_HEIGHT (s->font));
22599 fprintf (stderr, " bg width = %d\n", s->background_width);
22600 }
22601
22602 #endif /* GLYPH_DEBUG */
22603
22604 /* Initialize glyph string S. CHAR2B is a suitably allocated vector
22605 of XChar2b structures for S; it can't be allocated in
22606 init_glyph_string because it must be allocated via `alloca'. W
22607 is the window on which S is drawn. ROW and AREA are the glyph row
22608 and area within the row from which S is constructed. START is the
22609 index of the first glyph structure covered by S. HL is a
22610 face-override for drawing S. */
22611
22612 #ifdef HAVE_NTGUI
22613 #define OPTIONAL_HDC(hdc) HDC hdc,
22614 #define DECLARE_HDC(hdc) HDC hdc;
22615 #define ALLOCATE_HDC(hdc, f) hdc = get_frame_dc ((f))
22616 #define RELEASE_HDC(hdc, f) release_frame_dc ((f), (hdc))
22617 #endif
22618
22619 #ifndef OPTIONAL_HDC
22620 #define OPTIONAL_HDC(hdc)
22621 #define DECLARE_HDC(hdc)
22622 #define ALLOCATE_HDC(hdc, f)
22623 #define RELEASE_HDC(hdc, f)
22624 #endif
22625
22626 static void
22627 init_glyph_string (struct glyph_string *s,
22628 OPTIONAL_HDC (hdc)
22629 XChar2b *char2b, struct window *w, struct glyph_row *row,
22630 enum glyph_row_area area, int start, enum draw_glyphs_face hl)
22631 {
22632 memset (s, 0, sizeof *s);
22633 s->w = w;
22634 s->f = XFRAME (w->frame);
22635 #ifdef HAVE_NTGUI
22636 s->hdc = hdc;
22637 #endif
22638 s->display = FRAME_X_DISPLAY (s->f);
22639 s->window = FRAME_X_WINDOW (s->f);
22640 s->char2b = char2b;
22641 s->hl = hl;
22642 s->row = row;
22643 s->area = area;
22644 s->first_glyph = row->glyphs[area] + start;
22645 s->height = row->height;
22646 s->y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y);
22647 s->ybase = s->y + row->ascent;
22648 }
22649
22650
22651 /* Append the list of glyph strings with head H and tail T to the list
22652 with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the result. */
22653
22654 static void
22655 append_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22656 struct glyph_string *h, struct glyph_string *t)
22657 {
22658 if (h)
22659 {
22660 if (*head)
22661 (*tail)->next = h;
22662 else
22663 *head = h;
22664 h->prev = *tail;
22665 *tail = t;
22666 }
22667 }
22668
22669
22670 /* Prepend the list of glyph strings with head H and tail T to the
22671 list with head *HEAD and tail *TAIL. Set *HEAD and *TAIL to the
22672 result. */
22673
22674 static void
22675 prepend_glyph_string_lists (struct glyph_string **head, struct glyph_string **tail,
22676 struct glyph_string *h, struct glyph_string *t)
22677 {
22678 if (h)
22679 {
22680 if (*head)
22681 (*head)->prev = t;
22682 else
22683 *tail = t;
22684 t->next = *head;
22685 *head = h;
22686 }
22687 }
22688
22689
22690 /* Append glyph string S to the list with head *HEAD and tail *TAIL.
22691 Set *HEAD and *TAIL to the resulting list. */
22692
22693 static void
22694 append_glyph_string (struct glyph_string **head, struct glyph_string **tail,
22695 struct glyph_string *s)
22696 {
22697 s->next = s->prev = NULL;
22698 append_glyph_string_lists (head, tail, s, s);
22699 }
22700
22701
22702 /* Get face and two-byte form of character C in face FACE_ID on frame F.
22703 The encoding of C is returned in *CHAR2B. DISPLAY_P non-zero means
22704 make sure that X resources for the face returned are allocated.
22705 Value is a pointer to a realized face that is ready for display if
22706 DISPLAY_P is non-zero. */
22707
22708 static struct face *
22709 get_char_face_and_encoding (struct frame *f, int c, int face_id,
22710 XChar2b *char2b, int display_p)
22711 {
22712 struct face *face = FACE_FROM_ID (f, face_id);
22713
22714 if (face->font)
22715 {
22716 unsigned code = face->font->driver->encode_char (face->font, c);
22717
22718 if (code != FONT_INVALID_CODE)
22719 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22720 else
22721 STORE_XCHAR2B (char2b, 0, 0);
22722 }
22723
22724 /* Make sure X resources of the face are allocated. */
22725 #ifdef HAVE_X_WINDOWS
22726 if (display_p)
22727 #endif
22728 {
22729 eassert (face != NULL);
22730 PREPARE_FACE_FOR_DISPLAY (f, face);
22731 }
22732
22733 return face;
22734 }
22735
22736
22737 /* Get face and two-byte form of character glyph GLYPH on frame F.
22738 The encoding of GLYPH->u.ch is returned in *CHAR2B. Value is
22739 a pointer to a realized face that is ready for display. */
22740
22741 static struct face *
22742 get_glyph_face_and_encoding (struct frame *f, struct glyph *glyph,
22743 XChar2b *char2b, int *two_byte_p)
22744 {
22745 struct face *face;
22746
22747 eassert (glyph->type == CHAR_GLYPH);
22748 face = FACE_FROM_ID (f, glyph->face_id);
22749
22750 if (two_byte_p)
22751 *two_byte_p = 0;
22752
22753 if (face->font)
22754 {
22755 unsigned code;
22756
22757 if (CHAR_BYTE8_P (glyph->u.ch))
22758 code = CHAR_TO_BYTE8 (glyph->u.ch);
22759 else
22760 code = face->font->driver->encode_char (face->font, glyph->u.ch);
22761
22762 if (code != FONT_INVALID_CODE)
22763 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22764 else
22765 STORE_XCHAR2B (char2b, 0, 0);
22766 }
22767
22768 /* Make sure X resources of the face are allocated. */
22769 eassert (face != NULL);
22770 PREPARE_FACE_FOR_DISPLAY (f, face);
22771 return face;
22772 }
22773
22774
22775 /* Get glyph code of character C in FONT in the two-byte form CHAR2B.
22776 Return 1 if FONT has a glyph for C, otherwise return 0. */
22777
22778 static int
22779 get_char_glyph_code (int c, struct font *font, XChar2b *char2b)
22780 {
22781 unsigned code;
22782
22783 if (CHAR_BYTE8_P (c))
22784 code = CHAR_TO_BYTE8 (c);
22785 else
22786 code = font->driver->encode_char (font, c);
22787
22788 if (code == FONT_INVALID_CODE)
22789 return 0;
22790 STORE_XCHAR2B (char2b, (code >> 8), (code & 0xFF));
22791 return 1;
22792 }
22793
22794
22795 /* Fill glyph string S with composition components specified by S->cmp.
22796
22797 BASE_FACE is the base face of the composition.
22798 S->cmp_from is the index of the first component for S.
22799
22800 OVERLAPS non-zero means S should draw the foreground only, and use
22801 its physical height for clipping. See also draw_glyphs.
22802
22803 Value is the index of a component not in S. */
22804
22805 static int
22806 fill_composite_glyph_string (struct glyph_string *s, struct face *base_face,
22807 int overlaps)
22808 {
22809 int i;
22810 /* For all glyphs of this composition, starting at the offset
22811 S->cmp_from, until we reach the end of the definition or encounter a
22812 glyph that requires the different face, add it to S. */
22813 struct face *face;
22814
22815 eassert (s);
22816
22817 s->for_overlaps = overlaps;
22818 s->face = NULL;
22819 s->font = NULL;
22820 for (i = s->cmp_from; i < s->cmp->glyph_len; i++)
22821 {
22822 int c = COMPOSITION_GLYPH (s->cmp, i);
22823
22824 /* TAB in a composition means display glyphs with padding space
22825 on the left or right. */
22826 if (c != '\t')
22827 {
22828 int face_id = FACE_FOR_CHAR (s->f, base_face->ascii_face, c,
22829 -1, Qnil);
22830
22831 face = get_char_face_and_encoding (s->f, c, face_id,
22832 s->char2b + i, 1);
22833 if (face)
22834 {
22835 if (! s->face)
22836 {
22837 s->face = face;
22838 s->font = s->face->font;
22839 }
22840 else if (s->face != face)
22841 break;
22842 }
22843 }
22844 ++s->nchars;
22845 }
22846 s->cmp_to = i;
22847
22848 if (s->face == NULL)
22849 {
22850 s->face = base_face->ascii_face;
22851 s->font = s->face->font;
22852 }
22853
22854 /* All glyph strings for the same composition has the same width,
22855 i.e. the width set for the first component of the composition. */
22856 s->width = s->first_glyph->pixel_width;
22857
22858 /* If the specified font could not be loaded, use the frame's
22859 default font, but record the fact that we couldn't load it in
22860 the glyph string so that we can draw rectangles for the
22861 characters of the glyph string. */
22862 if (s->font == NULL)
22863 {
22864 s->font_not_found_p = 1;
22865 s->font = FRAME_FONT (s->f);
22866 }
22867
22868 /* Adjust base line for subscript/superscript text. */
22869 s->ybase += s->first_glyph->voffset;
22870
22871 /* This glyph string must always be drawn with 16-bit functions. */
22872 s->two_byte_p = 1;
22873
22874 return s->cmp_to;
22875 }
22876
22877 static int
22878 fill_gstring_glyph_string (struct glyph_string *s, int face_id,
22879 int start, int end, int overlaps)
22880 {
22881 struct glyph *glyph, *last;
22882 Lisp_Object lgstring;
22883 int i;
22884
22885 s->for_overlaps = overlaps;
22886 glyph = s->row->glyphs[s->area] + start;
22887 last = s->row->glyphs[s->area] + end;
22888 s->cmp_id = glyph->u.cmp.id;
22889 s->cmp_from = glyph->slice.cmp.from;
22890 s->cmp_to = glyph->slice.cmp.to + 1;
22891 s->face = FACE_FROM_ID (s->f, face_id);
22892 lgstring = composition_gstring_from_id (s->cmp_id);
22893 s->font = XFONT_OBJECT (LGSTRING_FONT (lgstring));
22894 glyph++;
22895 while (glyph < last
22896 && glyph->u.cmp.automatic
22897 && glyph->u.cmp.id == s->cmp_id
22898 && s->cmp_to == glyph->slice.cmp.from)
22899 s->cmp_to = (glyph++)->slice.cmp.to + 1;
22900
22901 for (i = s->cmp_from; i < s->cmp_to; i++)
22902 {
22903 Lisp_Object lglyph = LGSTRING_GLYPH (lgstring, i);
22904 unsigned code = LGLYPH_CODE (lglyph);
22905
22906 STORE_XCHAR2B ((s->char2b + i), code >> 8, code & 0xFF);
22907 }
22908 s->width = composition_gstring_width (lgstring, s->cmp_from, s->cmp_to, NULL);
22909 return glyph - s->row->glyphs[s->area];
22910 }
22911
22912
22913 /* Fill glyph string S from a sequence glyphs for glyphless characters.
22914 See the comment of fill_glyph_string for arguments.
22915 Value is the index of the first glyph not in S. */
22916
22917
22918 static int
22919 fill_glyphless_glyph_string (struct glyph_string *s, int face_id,
22920 int start, int end, int overlaps)
22921 {
22922 struct glyph *glyph, *last;
22923 int voffset;
22924
22925 eassert (s->first_glyph->type == GLYPHLESS_GLYPH);
22926 s->for_overlaps = overlaps;
22927 glyph = s->row->glyphs[s->area] + start;
22928 last = s->row->glyphs[s->area] + end;
22929 voffset = glyph->voffset;
22930 s->face = FACE_FROM_ID (s->f, face_id);
22931 s->font = s->face->font ? s->face->font : FRAME_FONT (s->f);
22932 s->nchars = 1;
22933 s->width = glyph->pixel_width;
22934 glyph++;
22935 while (glyph < last
22936 && glyph->type == GLYPHLESS_GLYPH
22937 && glyph->voffset == voffset
22938 && glyph->face_id == face_id)
22939 {
22940 s->nchars++;
22941 s->width += glyph->pixel_width;
22942 glyph++;
22943 }
22944 s->ybase += voffset;
22945 return glyph - s->row->glyphs[s->area];
22946 }
22947
22948
22949 /* Fill glyph string S from a sequence of character glyphs.
22950
22951 FACE_ID is the face id of the string. START is the index of the
22952 first glyph to consider, END is the index of the last + 1.
22953 OVERLAPS non-zero means S should draw the foreground only, and use
22954 its physical height for clipping. See also draw_glyphs.
22955
22956 Value is the index of the first glyph not in S. */
22957
22958 static int
22959 fill_glyph_string (struct glyph_string *s, int face_id,
22960 int start, int end, int overlaps)
22961 {
22962 struct glyph *glyph, *last;
22963 int voffset;
22964 int glyph_not_available_p;
22965
22966 eassert (s->f == XFRAME (s->w->frame));
22967 eassert (s->nchars == 0);
22968 eassert (start >= 0 && end > start);
22969
22970 s->for_overlaps = overlaps;
22971 glyph = s->row->glyphs[s->area] + start;
22972 last = s->row->glyphs[s->area] + end;
22973 voffset = glyph->voffset;
22974 s->padding_p = glyph->padding_p;
22975 glyph_not_available_p = glyph->glyph_not_available_p;
22976
22977 while (glyph < last
22978 && glyph->type == CHAR_GLYPH
22979 && glyph->voffset == voffset
22980 /* Same face id implies same font, nowadays. */
22981 && glyph->face_id == face_id
22982 && glyph->glyph_not_available_p == glyph_not_available_p)
22983 {
22984 int two_byte_p;
22985
22986 s->face = get_glyph_face_and_encoding (s->f, glyph,
22987 s->char2b + s->nchars,
22988 &two_byte_p);
22989 s->two_byte_p = two_byte_p;
22990 ++s->nchars;
22991 eassert (s->nchars <= end - start);
22992 s->width += glyph->pixel_width;
22993 if (glyph++->padding_p != s->padding_p)
22994 break;
22995 }
22996
22997 s->font = s->face->font;
22998
22999 /* If the specified font could not be loaded, use the frame's font,
23000 but record the fact that we couldn't load it in
23001 S->font_not_found_p so that we can draw rectangles for the
23002 characters of the glyph string. */
23003 if (s->font == NULL || glyph_not_available_p)
23004 {
23005 s->font_not_found_p = 1;
23006 s->font = FRAME_FONT (s->f);
23007 }
23008
23009 /* Adjust base line for subscript/superscript text. */
23010 s->ybase += voffset;
23011
23012 eassert (s->face && s->face->gc);
23013 return glyph - s->row->glyphs[s->area];
23014 }
23015
23016
23017 /* Fill glyph string S from image glyph S->first_glyph. */
23018
23019 static void
23020 fill_image_glyph_string (struct glyph_string *s)
23021 {
23022 eassert (s->first_glyph->type == IMAGE_GLYPH);
23023 s->img = IMAGE_FROM_ID (s->f, s->first_glyph->u.img_id);
23024 eassert (s->img);
23025 s->slice = s->first_glyph->slice.img;
23026 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23027 s->font = s->face->font;
23028 s->width = s->first_glyph->pixel_width;
23029
23030 /* Adjust base line for subscript/superscript text. */
23031 s->ybase += s->first_glyph->voffset;
23032 }
23033
23034 #ifdef HAVE_XWIDGETS
23035 static void
23036 fill_xwidget_glyph_string (struct glyph_string *s)
23037 {
23038 eassert (s->first_glyph->type == XWIDGET_GLYPH);
23039 printf("fill_xwidget_glyph_string: width:%d \n",s->first_glyph->pixel_width);
23040 s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
23041 s->font = s->face->font;
23042 s->width = s->first_glyph->pixel_width;
23043 s->ybase += s->first_glyph->voffset;
23044 s->xwidget = s->first_glyph->u.xwidget;
23045 //assert_valid_xwidget_id ( s->xwidget, "fill_xwidget_glyph_string");
23046 }
23047 #endif
23048 /* Fill glyph string S from a sequence of stretch glyphs.
23049
23050 START is the index of the first glyph to consider,
23051 END is the index of the last + 1.
23052
23053 Value is the index of the first glyph not in S. */
23054
23055 static int
23056 fill_stretch_glyph_string (struct glyph_string *s, int start, int end)
23057 {
23058 struct glyph *glyph, *last;
23059 int voffset, face_id;
23060
23061 eassert (s->first_glyph->type == STRETCH_GLYPH);
23062
23063 glyph = s->row->glyphs[s->area] + start;
23064 last = s->row->glyphs[s->area] + end;
23065 face_id = glyph->face_id;
23066 s->face = FACE_FROM_ID (s->f, face_id);
23067 s->font = s->face->font;
23068 s->width = glyph->pixel_width;
23069 s->nchars = 1;
23070 voffset = glyph->voffset;
23071
23072 for (++glyph;
23073 (glyph < last
23074 && glyph->type == STRETCH_GLYPH
23075 && glyph->voffset == voffset
23076 && glyph->face_id == face_id);
23077 ++glyph)
23078 s->width += glyph->pixel_width;
23079
23080 /* Adjust base line for subscript/superscript text. */
23081 s->ybase += voffset;
23082
23083 /* The case that face->gc == 0 is handled when drawing the glyph
23084 string by calling PREPARE_FACE_FOR_DISPLAY. */
23085 eassert (s->face);
23086 return glyph - s->row->glyphs[s->area];
23087 }
23088
23089 static struct font_metrics *
23090 get_per_char_metric (struct font *font, XChar2b *char2b)
23091 {
23092 static struct font_metrics metrics;
23093 unsigned code = (XCHAR2B_BYTE1 (char2b) << 8) | XCHAR2B_BYTE2 (char2b);
23094
23095 if (! font || code == FONT_INVALID_CODE)
23096 return NULL;
23097 font->driver->text_extents (font, &code, 1, &metrics);
23098 return &metrics;
23099 }
23100
23101 /* EXPORT for RIF:
23102 Set *LEFT and *RIGHT to the left and right overhang of GLYPH on
23103 frame F. Overhangs of glyphs other than type CHAR_GLYPH are
23104 assumed to be zero. */
23105
23106 void
23107 x_get_glyph_overhangs (struct glyph *glyph, struct frame *f, int *left, int *right)
23108 {
23109 *left = *right = 0;
23110
23111 if (glyph->type == CHAR_GLYPH)
23112 {
23113 struct face *face;
23114 XChar2b char2b;
23115 struct font_metrics *pcm;
23116
23117 face = get_glyph_face_and_encoding (f, glyph, &char2b, NULL);
23118 if (face->font && (pcm = get_per_char_metric (face->font, &char2b)))
23119 {
23120 if (pcm->rbearing > pcm->width)
23121 *right = pcm->rbearing - pcm->width;
23122 if (pcm->lbearing < 0)
23123 *left = -pcm->lbearing;
23124 }
23125 }
23126 else if (glyph->type == COMPOSITE_GLYPH)
23127 {
23128 if (! glyph->u.cmp.automatic)
23129 {
23130 struct composition *cmp = composition_table[glyph->u.cmp.id];
23131
23132 if (cmp->rbearing > cmp->pixel_width)
23133 *right = cmp->rbearing - cmp->pixel_width;
23134 if (cmp->lbearing < 0)
23135 *left = - cmp->lbearing;
23136 }
23137 else
23138 {
23139 Lisp_Object gstring = composition_gstring_from_id (glyph->u.cmp.id);
23140 struct font_metrics metrics;
23141
23142 composition_gstring_width (gstring, glyph->slice.cmp.from,
23143 glyph->slice.cmp.to + 1, &metrics);
23144 if (metrics.rbearing > metrics.width)
23145 *right = metrics.rbearing - metrics.width;
23146 if (metrics.lbearing < 0)
23147 *left = - metrics.lbearing;
23148 }
23149 }
23150 }
23151
23152
23153 /* Return the index of the first glyph preceding glyph string S that
23154 is overwritten by S because of S's left overhang. Value is -1
23155 if no glyphs are overwritten. */
23156
23157 static int
23158 left_overwritten (struct glyph_string *s)
23159 {
23160 int k;
23161
23162 if (s->left_overhang)
23163 {
23164 int x = 0, i;
23165 struct glyph *glyphs = s->row->glyphs[s->area];
23166 int first = s->first_glyph - glyphs;
23167
23168 for (i = first - 1; i >= 0 && x > -s->left_overhang; --i)
23169 x -= glyphs[i].pixel_width;
23170
23171 k = i + 1;
23172 }
23173 else
23174 k = -1;
23175
23176 return k;
23177 }
23178
23179
23180 /* Return the index of the first glyph preceding glyph string S that
23181 is overwriting S because of its right overhang. Value is -1 if no
23182 glyph in front of S overwrites S. */
23183
23184 static int
23185 left_overwriting (struct glyph_string *s)
23186 {
23187 int i, k, x;
23188 struct glyph *glyphs = s->row->glyphs[s->area];
23189 int first = s->first_glyph - glyphs;
23190
23191 k = -1;
23192 x = 0;
23193 for (i = first - 1; i >= 0; --i)
23194 {
23195 int left, right;
23196 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23197 if (x + right > 0)
23198 k = i;
23199 x -= glyphs[i].pixel_width;
23200 }
23201
23202 return k;
23203 }
23204
23205
23206 /* Return the index of the last glyph following glyph string S that is
23207 overwritten by S because of S's right overhang. Value is -1 if
23208 no such glyph is found. */
23209
23210 static int
23211 right_overwritten (struct glyph_string *s)
23212 {
23213 int k = -1;
23214
23215 if (s->right_overhang)
23216 {
23217 int x = 0, i;
23218 struct glyph *glyphs = s->row->glyphs[s->area];
23219 int first = (s->first_glyph - glyphs
23220 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23221 int end = s->row->used[s->area];
23222
23223 for (i = first; i < end && s->right_overhang > x; ++i)
23224 x += glyphs[i].pixel_width;
23225
23226 k = i;
23227 }
23228
23229 return k;
23230 }
23231
23232
23233 /* Return the index of the last glyph following glyph string S that
23234 overwrites S because of its left overhang. Value is negative
23235 if no such glyph is found. */
23236
23237 static int
23238 right_overwriting (struct glyph_string *s)
23239 {
23240 int i, k, x;
23241 int end = s->row->used[s->area];
23242 struct glyph *glyphs = s->row->glyphs[s->area];
23243 int first = (s->first_glyph - glyphs
23244 + (s->first_glyph->type == COMPOSITE_GLYPH ? 1 : s->nchars));
23245
23246 k = -1;
23247 x = 0;
23248 for (i = first; i < end; ++i)
23249 {
23250 int left, right;
23251 x_get_glyph_overhangs (glyphs + i, s->f, &left, &right);
23252 if (x - left < 0)
23253 k = i;
23254 x += glyphs[i].pixel_width;
23255 }
23256
23257 return k;
23258 }
23259
23260
23261 /* Set background width of glyph string S. START is the index of the
23262 first glyph following S. LAST_X is the right-most x-position + 1
23263 in the drawing area. */
23264
23265 static void
23266 set_glyph_string_background_width (struct glyph_string *s, int start, int last_x)
23267 {
23268 /* If the face of this glyph string has to be drawn to the end of
23269 the drawing area, set S->extends_to_end_of_line_p. */
23270
23271 if (start == s->row->used[s->area]
23272 && s->area == TEXT_AREA
23273 && ((s->row->fill_line_p
23274 && (s->hl == DRAW_NORMAL_TEXT
23275 || s->hl == DRAW_IMAGE_RAISED
23276 || s->hl == DRAW_IMAGE_SUNKEN))
23277 || s->hl == DRAW_MOUSE_FACE))
23278 s->extends_to_end_of_line_p = 1;
23279
23280 /* If S extends its face to the end of the line, set its
23281 background_width to the distance to the right edge of the drawing
23282 area. */
23283 if (s->extends_to_end_of_line_p)
23284 s->background_width = last_x - s->x + 1;
23285 else
23286 s->background_width = s->width;
23287 }
23288
23289
23290 /* Compute overhangs and x-positions for glyph string S and its
23291 predecessors, or successors. X is the starting x-position for S.
23292 BACKWARD_P non-zero means process predecessors. */
23293
23294 static void
23295 compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
23296 {
23297 if (backward_p)
23298 {
23299 while (s)
23300 {
23301 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23302 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23303 x -= s->width;
23304 s->x = x;
23305 s = s->prev;
23306 }
23307 }
23308 else
23309 {
23310 while (s)
23311 {
23312 if (FRAME_RIF (s->f)->compute_glyph_string_overhangs)
23313 FRAME_RIF (s->f)->compute_glyph_string_overhangs (s);
23314 s->x = x;
23315 x += s->width;
23316 s = s->next;
23317 }
23318 }
23319 }
23320
23321
23322
23323 /* The following macros are only called from draw_glyphs below.
23324 They reference the following parameters of that function directly:
23325 `w', `row', `area', and `overlap_p'
23326 as well as the following local variables:
23327 `s', `f', and `hdc' (in W32) */
23328
23329 #ifdef HAVE_NTGUI
23330 /* On W32, silently add local `hdc' variable to argument list of
23331 init_glyph_string. */
23332 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23333 init_glyph_string (s, hdc, char2b, w, row, area, start, hl)
23334 #else
23335 #define INIT_GLYPH_STRING(s, char2b, w, row, area, start, hl) \
23336 init_glyph_string (s, char2b, w, row, area, start, hl)
23337 #endif
23338
23339 /* Add a glyph string for a stretch glyph to the list of strings
23340 between HEAD and TAIL. START is the index of the stretch glyph in
23341 row area AREA of glyph row ROW. END is the index of the last glyph
23342 in that glyph row area. X is the current output position assigned
23343 to the new glyph string constructed. HL overrides that face of the
23344 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23345 is the right-most x-position of the drawing area. */
23346
23347 /* SunOS 4 bundled cc, barfed on continuations in the arg lists here
23348 and below -- keep them on one line. */
23349 #define BUILD_STRETCH_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23350 do \
23351 { \
23352 s = alloca (sizeof *s); \
23353 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23354 START = fill_stretch_glyph_string (s, START, END); \
23355 append_glyph_string (&HEAD, &TAIL, s); \
23356 s->x = (X); \
23357 } \
23358 while (0)
23359
23360
23361 /* Add a glyph string for an image glyph to the list of strings
23362 between HEAD and TAIL. START is the index of the image glyph in
23363 row area AREA of glyph row ROW. END is the index of the last glyph
23364 in that glyph row area. X is the current output position assigned
23365 to the new glyph string constructed. HL overrides that face of the
23366 glyph; e.g. it is DRAW_CURSOR if a cursor has to be drawn. LAST_X
23367 is the right-most x-position of the drawing area. */
23368
23369 #define BUILD_IMAGE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23370 do \
23371 { \
23372 s = alloca (sizeof *s); \
23373 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23374 fill_image_glyph_string (s); \
23375 append_glyph_string (&HEAD, &TAIL, s); \
23376 ++START; \
23377 s->x = (X); \
23378 } \
23379 while (0)
23380
23381 #ifdef HAVE_XWIDGETS
23382 #define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23383 do \
23384 { \
23385 printf("BUILD_XWIDGET_GLYPH_STRING\n"); \
23386 s = (struct glyph_string *) alloca (sizeof *s); \
23387 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23388 fill_xwidget_glyph_string (s); \
23389 append_glyph_string (&HEAD, &TAIL, s); \
23390 ++START; \
23391 s->x = (X); \
23392 } \
23393 while (0)
23394 #endif
23395
23396
23397 /* Add a glyph string for a sequence of character glyphs to the list
23398 of strings between HEAD and TAIL. START is the index of the first
23399 glyph in row area AREA of glyph row ROW that is part of the new
23400 glyph string. END is the index of the last glyph in that glyph row
23401 area. X is the current output position assigned to the new glyph
23402 string constructed. HL overrides that face of the glyph; e.g. it
23403 is DRAW_CURSOR if a cursor has to be drawn. LAST_X is the
23404 right-most x-position of the drawing area. */
23405
23406 #define BUILD_CHAR_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23407 do \
23408 { \
23409 int face_id; \
23410 XChar2b *char2b; \
23411 \
23412 face_id = (row)->glyphs[area][START].face_id; \
23413 \
23414 s = alloca (sizeof *s); \
23415 char2b = alloca ((END - START) * sizeof *char2b); \
23416 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23417 append_glyph_string (&HEAD, &TAIL, s); \
23418 s->x = (X); \
23419 START = fill_glyph_string (s, face_id, START, END, overlaps); \
23420 } \
23421 while (0)
23422
23423
23424 /* Add a glyph string for a composite sequence to the list of strings
23425 between HEAD and TAIL. START is the index of the first glyph in
23426 row area AREA of glyph row ROW that is part of the new glyph
23427 string. END is the index of the last glyph in that glyph row area.
23428 X is the current output position assigned to the new glyph string
23429 constructed. HL overrides that face of the glyph; e.g. it is
23430 DRAW_CURSOR if a cursor has to be drawn. LAST_X is the right-most
23431 x-position of the drawing area. */
23432
23433 #define BUILD_COMPOSITE_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23434 do { \
23435 int face_id = (row)->glyphs[area][START].face_id; \
23436 struct face *base_face = FACE_FROM_ID (f, face_id); \
23437 ptrdiff_t cmp_id = (row)->glyphs[area][START].u.cmp.id; \
23438 struct composition *cmp = composition_table[cmp_id]; \
23439 XChar2b *char2b; \
23440 struct glyph_string *first_s = NULL; \
23441 int n; \
23442 \
23443 char2b = alloca (cmp->glyph_len * sizeof *char2b); \
23444 \
23445 /* Make glyph_strings for each glyph sequence that is drawable by \
23446 the same face, and append them to HEAD/TAIL. */ \
23447 for (n = 0; n < cmp->glyph_len;) \
23448 { \
23449 s = alloca (sizeof *s); \
23450 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23451 append_glyph_string (&(HEAD), &(TAIL), s); \
23452 s->cmp = cmp; \
23453 s->cmp_from = n; \
23454 s->x = (X); \
23455 if (n == 0) \
23456 first_s = s; \
23457 n = fill_composite_glyph_string (s, base_face, overlaps); \
23458 } \
23459 \
23460 ++START; \
23461 s = first_s; \
23462 } while (0)
23463
23464
23465 /* Add a glyph string for a glyph-string sequence to the list of strings
23466 between HEAD and TAIL. */
23467
23468 #define BUILD_GSTRING_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23469 do { \
23470 int face_id; \
23471 XChar2b *char2b; \
23472 Lisp_Object gstring; \
23473 \
23474 face_id = (row)->glyphs[area][START].face_id; \
23475 gstring = (composition_gstring_from_id \
23476 ((row)->glyphs[area][START].u.cmp.id)); \
23477 s = alloca (sizeof *s); \
23478 char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b); \
23479 INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL); \
23480 append_glyph_string (&(HEAD), &(TAIL), s); \
23481 s->x = (X); \
23482 START = fill_gstring_glyph_string (s, face_id, START, END, overlaps); \
23483 } while (0)
23484
23485
23486 /* Add a glyph string for a sequence of glyphless character's glyphs
23487 to the list of strings between HEAD and TAIL. The meanings of
23488 arguments are the same as those of BUILD_CHAR_GLYPH_STRINGS. */
23489
23490 #define BUILD_GLYPHLESS_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
23491 do \
23492 { \
23493 int face_id; \
23494 \
23495 face_id = (row)->glyphs[area][START].face_id; \
23496 \
23497 s = alloca (sizeof *s); \
23498 INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL); \
23499 append_glyph_string (&HEAD, &TAIL, s); \
23500 s->x = (X); \
23501 START = fill_glyphless_glyph_string (s, face_id, START, END, \
23502 overlaps); \
23503 } \
23504 while (0)
23505
23506
23507 /* Build a list of glyph strings between HEAD and TAIL for the glyphs
23508 of AREA of glyph row ROW on window W between indices START and END.
23509 HL overrides the face for drawing glyph strings, e.g. it is
23510 DRAW_CURSOR to draw a cursor. X and LAST_X are start and end
23511 x-positions of the drawing area.
23512
23513 This is an ugly monster macro construct because we must use alloca
23514 to allocate glyph strings (because draw_glyphs can be called
23515 asynchronously). */
23516
23517 #define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
23518 do \
23519 { \
23520 HEAD = TAIL = NULL; \
23521 while (START < END) \
23522 { \
23523 struct glyph *first_glyph = (row)->glyphs[area] + START; \
23524 switch (first_glyph->type) \
23525 { \
23526 case CHAR_GLYPH: \
23527 BUILD_CHAR_GLYPH_STRINGS (START, END, HEAD, TAIL, \
23528 HL, X, LAST_X); \
23529 break; \
23530 \
23531 case COMPOSITE_GLYPH: \
23532 if (first_glyph->u.cmp.automatic) \
23533 BUILD_GSTRING_GLYPH_STRING (START, END, HEAD, TAIL, \
23534 HL, X, LAST_X); \
23535 else \
23536 BUILD_COMPOSITE_GLYPH_STRING (START, END, HEAD, TAIL, \
23537 HL, X, LAST_X); \
23538 break; \
23539 \
23540 case STRETCH_GLYPH: \
23541 BUILD_STRETCH_GLYPH_STRING (START, END, HEAD, TAIL, \
23542 HL, X, LAST_X); \
23543 break; \
23544 \
23545 case IMAGE_GLYPH: \
23546 BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL, \
23547 HL, X, LAST_X); \
23548 break;
23549
23550 #define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
23551 case XWIDGET_GLYPH: \
23552 BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL, \
23553 HL, X, LAST_X); \
23554 break;
23555
23556 #define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X) \
23557 case GLYPHLESS_GLYPH: \
23558 BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL, \
23559 HL, X, LAST_X); \
23560 break; \
23561 \
23562 default: \
23563 emacs_abort (); \
23564 } \
23565 \
23566 if (s) \
23567 { \
23568 set_glyph_string_background_width (s, START, LAST_X); \
23569 (X) += s->width; \
23570 } \
23571 } \
23572 } while (0)
23573
23574
23575 #ifdef HAVE_XWIDGETS
23576 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23577 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
23578 BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X) \
23579 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
23580 #else
23581 #define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X) \
23582 BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X) \
23583 BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
23584 #endif
23585
23586
23587 /* Draw glyphs between START and END in AREA of ROW on window W,
23588 starting at x-position X. X is relative to AREA in W. HL is a
23589 face-override with the following meaning:
23590
23591 DRAW_NORMAL_TEXT draw normally
23592 DRAW_CURSOR draw in cursor face
23593 DRAW_MOUSE_FACE draw in mouse face.
23594 DRAW_INVERSE_VIDEO draw in mode line face
23595 DRAW_IMAGE_SUNKEN draw an image with a sunken relief around it
23596 DRAW_IMAGE_RAISED draw an image with a raised relief around it
23597
23598 If OVERLAPS is non-zero, draw only the foreground of characters and
23599 clip to the physical height of ROW. Non-zero value also defines
23600 the overlapping part to be drawn:
23601
23602 OVERLAPS_PRED overlap with preceding rows
23603 OVERLAPS_SUCC overlap with succeeding rows
23604 OVERLAPS_BOTH overlap with both preceding/succeeding rows
23605 OVERLAPS_ERASED_CURSOR overlap with erased cursor area
23606
23607 Value is the x-position reached, relative to AREA of W. */
23608
23609 static int
23610 draw_glyphs (struct window *w, int x, struct glyph_row *row,
23611 enum glyph_row_area area, ptrdiff_t start, ptrdiff_t end,
23612 enum draw_glyphs_face hl, int overlaps)
23613 {
23614 struct glyph_string *head, *tail;
23615 struct glyph_string *s;
23616 struct glyph_string *clip_head = NULL, *clip_tail = NULL;
23617 int i, j, x_reached, last_x, area_left = 0;
23618 struct frame *f = XFRAME (WINDOW_FRAME (w));
23619 DECLARE_HDC (hdc);
23620
23621 ALLOCATE_HDC (hdc, f);
23622
23623 /* Let's rather be paranoid than getting a SEGV. */
23624 end = min (end, row->used[area]);
23625 start = max (0, start);
23626 start = min (end, start);
23627
23628 /* Translate X to frame coordinates. Set last_x to the right
23629 end of the drawing area. */
23630 if (row->full_width_p)
23631 {
23632 /* X is relative to the left edge of W, without scroll bars
23633 or fringes. */
23634 area_left = WINDOW_LEFT_EDGE_X (w);
23635 last_x = WINDOW_LEFT_EDGE_X (w) + WINDOW_TOTAL_WIDTH (w);
23636 }
23637 else
23638 {
23639 area_left = window_box_left (w, area);
23640 last_x = area_left + window_box_width (w, area);
23641 }
23642 x += area_left;
23643
23644 /* Build a doubly-linked list of glyph_string structures between
23645 head and tail from what we have to draw. Note that the macro
23646 BUILD_GLYPH_STRINGS will modify its start parameter. That's
23647 the reason we use a separate variable `i'. */
23648 i = start;
23649 BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
23650 if (tail)
23651 x_reached = tail->x + tail->background_width;
23652 else
23653 x_reached = x;
23654
23655 /* If there are any glyphs with lbearing < 0 or rbearing > width in
23656 the row, redraw some glyphs in front or following the glyph
23657 strings built above. */
23658 if (head && !overlaps && row->contains_overlapping_glyphs_p)
23659 {
23660 struct glyph_string *h, *t;
23661 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
23662 int mouse_beg_col IF_LINT (= 0), mouse_end_col IF_LINT (= 0);
23663 int check_mouse_face = 0;
23664 int dummy_x = 0;
23665
23666 /* If mouse highlighting is on, we may need to draw adjacent
23667 glyphs using mouse-face highlighting. */
23668 if (area == TEXT_AREA && row->mouse_face_p)
23669 {
23670 struct glyph_row *mouse_beg_row, *mouse_end_row;
23671
23672 mouse_beg_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
23673 mouse_end_row = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
23674
23675 if (row >= mouse_beg_row && row <= mouse_end_row)
23676 {
23677 check_mouse_face = 1;
23678 mouse_beg_col = (row == mouse_beg_row)
23679 ? hlinfo->mouse_face_beg_col : 0;
23680 mouse_end_col = (row == mouse_end_row)
23681 ? hlinfo->mouse_face_end_col
23682 : row->used[TEXT_AREA];
23683 }
23684 }
23685
23686 /* Compute overhangs for all glyph strings. */
23687 if (FRAME_RIF (f)->compute_glyph_string_overhangs)
23688 for (s = head; s; s = s->next)
23689 FRAME_RIF (f)->compute_glyph_string_overhangs (s);
23690
23691 /* Prepend glyph strings for glyphs in front of the first glyph
23692 string that are overwritten because of the first glyph
23693 string's left overhang. The background of all strings
23694 prepended must be drawn because the first glyph string
23695 draws over it. */
23696 i = left_overwritten (head);
23697 if (i >= 0)
23698 {
23699 enum draw_glyphs_face overlap_hl;
23700
23701 /* If this row contains mouse highlighting, attempt to draw
23702 the overlapped glyphs with the correct highlight. This
23703 code fails if the overlap encompasses more than one glyph
23704 and mouse-highlight spans only some of these glyphs.
23705 However, making it work perfectly involves a lot more
23706 code, and I don't know if the pathological case occurs in
23707 practice, so we'll stick to this for now. --- cyd */
23708 if (check_mouse_face
23709 && mouse_beg_col < start && mouse_end_col > i)
23710 overlap_hl = DRAW_MOUSE_FACE;
23711 else
23712 overlap_hl = DRAW_NORMAL_TEXT;
23713
23714 j = i;
23715 BUILD_GLYPH_STRINGS (j, start, h, t,
23716 overlap_hl, dummy_x, last_x);
23717 start = i;
23718 compute_overhangs_and_x (t, head->x, 1);
23719 prepend_glyph_string_lists (&head, &tail, h, t);
23720 clip_head = head;
23721 }
23722
23723 /* Prepend glyph strings for glyphs in front of the first glyph
23724 string that overwrite that glyph string because of their
23725 right overhang. For these strings, only the foreground must
23726 be drawn, because it draws over the glyph string at `head'.
23727 The background must not be drawn because this would overwrite
23728 right overhangs of preceding glyphs for which no glyph
23729 strings exist. */
23730 i = left_overwriting (head);
23731 if (i >= 0)
23732 {
23733 enum draw_glyphs_face overlap_hl;
23734
23735 if (check_mouse_face
23736 && mouse_beg_col < start && mouse_end_col > i)
23737 overlap_hl = DRAW_MOUSE_FACE;
23738 else
23739 overlap_hl = DRAW_NORMAL_TEXT;
23740
23741 clip_head = head;
23742 BUILD_GLYPH_STRINGS (i, start, h, t,
23743 overlap_hl, dummy_x, last_x);
23744 for (s = h; s; s = s->next)
23745 s->background_filled_p = 1;
23746 compute_overhangs_and_x (t, head->x, 1);
23747 prepend_glyph_string_lists (&head, &tail, h, t);
23748 }
23749
23750 /* Append glyphs strings for glyphs following the last glyph
23751 string tail that are overwritten by tail. The background of
23752 these strings has to be drawn because tail's foreground draws
23753 over it. */
23754 i = right_overwritten (tail);
23755 if (i >= 0)
23756 {
23757 enum draw_glyphs_face overlap_hl;
23758
23759 if (check_mouse_face
23760 && mouse_beg_col < i && mouse_end_col > end)
23761 overlap_hl = DRAW_MOUSE_FACE;
23762 else
23763 overlap_hl = DRAW_NORMAL_TEXT;
23764
23765 BUILD_GLYPH_STRINGS (end, i, h, t,
23766 overlap_hl, x, last_x);
23767 /* Because BUILD_GLYPH_STRINGS updates the first argument,
23768 we don't have `end = i;' here. */
23769 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23770 append_glyph_string_lists (&head, &tail, h, t);
23771 clip_tail = tail;
23772 }
23773
23774 /* Append glyph strings for glyphs following the last glyph
23775 string tail that overwrite tail. The foreground of such
23776 glyphs has to be drawn because it writes into the background
23777 of tail. The background must not be drawn because it could
23778 paint over the foreground of following glyphs. */
23779 i = right_overwriting (tail);
23780 if (i >= 0)
23781 {
23782 enum draw_glyphs_face overlap_hl;
23783 if (check_mouse_face
23784 && mouse_beg_col < i && mouse_end_col > end)
23785 overlap_hl = DRAW_MOUSE_FACE;
23786 else
23787 overlap_hl = DRAW_NORMAL_TEXT;
23788
23789 clip_tail = tail;
23790 i++; /* We must include the Ith glyph. */
23791 BUILD_GLYPH_STRINGS (end, i, h, t,
23792 overlap_hl, x, last_x);
23793 for (s = h; s; s = s->next)
23794 s->background_filled_p = 1;
23795 compute_overhangs_and_x (h, tail->x + tail->width, 0);
23796 append_glyph_string_lists (&head, &tail, h, t);
23797 }
23798 if (clip_head || clip_tail)
23799 for (s = head; s; s = s->next)
23800 {
23801 s->clip_head = clip_head;
23802 s->clip_tail = clip_tail;
23803 }
23804 }
23805
23806 /* Draw all strings. */
23807 for (s = head; s; s = s->next)
23808 FRAME_RIF (f)->draw_glyph_string (s);
23809
23810 #ifndef HAVE_NS
23811 /* When focus a sole frame and move horizontally, this sets on_p to 0
23812 causing a failure to erase prev cursor position. */
23813 if (area == TEXT_AREA
23814 && !row->full_width_p
23815 /* When drawing overlapping rows, only the glyph strings'
23816 foreground is drawn, which doesn't erase a cursor
23817 completely. */
23818 && !overlaps)
23819 {
23820 int x0 = clip_head ? clip_head->x : (head ? head->x : x);
23821 int x1 = (clip_tail ? clip_tail->x + clip_tail->background_width
23822 : (tail ? tail->x + tail->background_width : x));
23823 x0 -= area_left;
23824 x1 -= area_left;
23825
23826 notice_overwritten_cursor (w, TEXT_AREA, x0, x1,
23827 row->y, MATRIX_ROW_BOTTOM_Y (row));
23828 }
23829 #endif
23830
23831 /* Value is the x-position up to which drawn, relative to AREA of W.
23832 This doesn't include parts drawn because of overhangs. */
23833 if (row->full_width_p)
23834 x_reached = FRAME_TO_WINDOW_PIXEL_X (w, x_reached);
23835 else
23836 x_reached -= area_left;
23837
23838 RELEASE_HDC (hdc, f);
23839
23840 return x_reached;
23841 }
23842
23843 /* Expand row matrix if too narrow. Don't expand if area
23844 is not present. */
23845
23846 #define IT_EXPAND_MATRIX_WIDTH(it, area) \
23847 { \
23848 if (!fonts_changed_p \
23849 && (it->glyph_row->glyphs[area] \
23850 < it->glyph_row->glyphs[area + 1])) \
23851 { \
23852 it->w->ncols_scale_factor++; \
23853 fonts_changed_p = 1; \
23854 } \
23855 }
23856
23857 /* Store one glyph for IT->char_to_display in IT->glyph_row.
23858 Called from x_produce_glyphs when IT->glyph_row is non-null. */
23859
23860 static void
23861 append_glyph (struct it *it)
23862 {
23863 struct glyph *glyph;
23864 enum glyph_row_area area = it->area;
23865
23866 eassert (it->glyph_row);
23867 eassert (it->char_to_display != '\n' && it->char_to_display != '\t');
23868
23869 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23870 if (glyph < it->glyph_row->glyphs[area + 1])
23871 {
23872 /* If the glyph row is reversed, we need to prepend the glyph
23873 rather than append it. */
23874 if (it->glyph_row->reversed_p && area == TEXT_AREA)
23875 {
23876 struct glyph *g;
23877
23878 /* Make room for the additional glyph. */
23879 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
23880 g[1] = *g;
23881 glyph = it->glyph_row->glyphs[area];
23882 }
23883 glyph->charpos = CHARPOS (it->position);
23884 glyph->object = it->object;
23885 if (it->pixel_width > 0)
23886 {
23887 glyph->pixel_width = it->pixel_width;
23888 glyph->padding_p = 0;
23889 }
23890 else
23891 {
23892 /* Assure at least 1-pixel width. Otherwise, cursor can't
23893 be displayed correctly. */
23894 glyph->pixel_width = 1;
23895 glyph->padding_p = 1;
23896 }
23897 glyph->ascent = it->ascent;
23898 glyph->descent = it->descent;
23899 glyph->voffset = it->voffset;
23900 glyph->type = CHAR_GLYPH;
23901 glyph->avoid_cursor_p = it->avoid_cursor_p;
23902 glyph->multibyte_p = it->multibyte_p;
23903 glyph->left_box_line_p = it->start_of_box_run_p;
23904 glyph->right_box_line_p = it->end_of_box_run_p;
23905 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23906 || it->phys_descent > it->descent);
23907 glyph->glyph_not_available_p = it->glyph_not_available_p;
23908 glyph->face_id = it->face_id;
23909 glyph->u.ch = it->char_to_display;
23910 glyph->slice.img = null_glyph_slice;
23911 glyph->font_type = FONT_TYPE_UNKNOWN;
23912 if (it->bidi_p)
23913 {
23914 glyph->resolved_level = it->bidi_it.resolved_level;
23915 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23916 emacs_abort ();
23917 glyph->bidi_type = it->bidi_it.type;
23918 }
23919 else
23920 {
23921 glyph->resolved_level = 0;
23922 glyph->bidi_type = UNKNOWN_BT;
23923 }
23924 ++it->glyph_row->used[area];
23925 }
23926 else
23927 IT_EXPAND_MATRIX_WIDTH (it, area);
23928 }
23929
23930 /* Store one glyph for the composition IT->cmp_it.id in
23931 IT->glyph_row. Called from x_produce_glyphs when IT->glyph_row is
23932 non-null. */
23933
23934 static void
23935 append_composite_glyph (struct it *it)
23936 {
23937 struct glyph *glyph;
23938 enum glyph_row_area area = it->area;
23939
23940 eassert (it->glyph_row);
23941
23942 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
23943 if (glyph < it->glyph_row->glyphs[area + 1])
23944 {
23945 /* If the glyph row is reversed, we need to prepend the glyph
23946 rather than append it. */
23947 if (it->glyph_row->reversed_p && it->area == TEXT_AREA)
23948 {
23949 struct glyph *g;
23950
23951 /* Make room for the new glyph. */
23952 for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
23953 g[1] = *g;
23954 glyph = it->glyph_row->glyphs[it->area];
23955 }
23956 glyph->charpos = it->cmp_it.charpos;
23957 glyph->object = it->object;
23958 glyph->pixel_width = it->pixel_width;
23959 glyph->ascent = it->ascent;
23960 glyph->descent = it->descent;
23961 glyph->voffset = it->voffset;
23962 glyph->type = COMPOSITE_GLYPH;
23963 if (it->cmp_it.ch < 0)
23964 {
23965 glyph->u.cmp.automatic = 0;
23966 glyph->u.cmp.id = it->cmp_it.id;
23967 glyph->slice.cmp.from = glyph->slice.cmp.to = 0;
23968 }
23969 else
23970 {
23971 glyph->u.cmp.automatic = 1;
23972 glyph->u.cmp.id = it->cmp_it.id;
23973 glyph->slice.cmp.from = it->cmp_it.from;
23974 glyph->slice.cmp.to = it->cmp_it.to - 1;
23975 }
23976 glyph->avoid_cursor_p = it->avoid_cursor_p;
23977 glyph->multibyte_p = it->multibyte_p;
23978 glyph->left_box_line_p = it->start_of_box_run_p;
23979 glyph->right_box_line_p = it->end_of_box_run_p;
23980 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
23981 || it->phys_descent > it->descent);
23982 glyph->padding_p = 0;
23983 glyph->glyph_not_available_p = 0;
23984 glyph->face_id = it->face_id;
23985 glyph->font_type = FONT_TYPE_UNKNOWN;
23986 if (it->bidi_p)
23987 {
23988 glyph->resolved_level = it->bidi_it.resolved_level;
23989 if ((it->bidi_it.type & 7) != it->bidi_it.type)
23990 emacs_abort ();
23991 glyph->bidi_type = it->bidi_it.type;
23992 }
23993 ++it->glyph_row->used[area];
23994 }
23995 else
23996 IT_EXPAND_MATRIX_WIDTH (it, area);
23997 }
23998
23999
24000 /* Change IT->ascent and IT->height according to the setting of
24001 IT->voffset. */
24002
24003 static void
24004 take_vertical_position_into_account (struct it *it)
24005 {
24006 if (it->voffset)
24007 {
24008 if (it->voffset < 0)
24009 /* Increase the ascent so that we can display the text higher
24010 in the line. */
24011 it->ascent -= it->voffset;
24012 else
24013 /* Increase the descent so that we can display the text lower
24014 in the line. */
24015 it->descent += it->voffset;
24016 }
24017 }
24018
24019
24020 /* Produce glyphs/get display metrics for the image IT is loaded with.
24021 See the description of struct display_iterator in dispextern.h for
24022 an overview of struct display_iterator. */
24023
24024 static void
24025 produce_image_glyph (struct it *it)
24026 {
24027 struct image *img;
24028 struct face *face;
24029 int glyph_ascent, crop;
24030 struct glyph_slice slice;
24031
24032 eassert (it->what == IT_IMAGE);
24033
24034 face = FACE_FROM_ID (it->f, it->face_id);
24035 eassert (face);
24036 /* Make sure X resources of the face is loaded. */
24037 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24038
24039 if (it->image_id < 0)
24040 {
24041 /* Fringe bitmap. */
24042 it->ascent = it->phys_ascent = 0;
24043 it->descent = it->phys_descent = 0;
24044 it->pixel_width = 0;
24045 it->nglyphs = 0;
24046 return;
24047 }
24048
24049 img = IMAGE_FROM_ID (it->f, it->image_id);
24050 eassert (img);
24051 /* Make sure X resources of the image is loaded. */
24052 prepare_image_for_display (it->f, img);
24053
24054 slice.x = slice.y = 0;
24055 slice.width = img->width;
24056 slice.height = img->height;
24057
24058 if (INTEGERP (it->slice.x))
24059 slice.x = XINT (it->slice.x);
24060 else if (FLOATP (it->slice.x))
24061 slice.x = XFLOAT_DATA (it->slice.x) * img->width;
24062
24063 if (INTEGERP (it->slice.y))
24064 slice.y = XINT (it->slice.y);
24065 else if (FLOATP (it->slice.y))
24066 slice.y = XFLOAT_DATA (it->slice.y) * img->height;
24067
24068 if (INTEGERP (it->slice.width))
24069 slice.width = XINT (it->slice.width);
24070 else if (FLOATP (it->slice.width))
24071 slice.width = XFLOAT_DATA (it->slice.width) * img->width;
24072
24073 if (INTEGERP (it->slice.height))
24074 slice.height = XINT (it->slice.height);
24075 else if (FLOATP (it->slice.height))
24076 slice.height = XFLOAT_DATA (it->slice.height) * img->height;
24077
24078 if (slice.x >= img->width)
24079 slice.x = img->width;
24080 if (slice.y >= img->height)
24081 slice.y = img->height;
24082 if (slice.x + slice.width >= img->width)
24083 slice.width = img->width - slice.x;
24084 if (slice.y + slice.height > img->height)
24085 slice.height = img->height - slice.y;
24086
24087 if (slice.width == 0 || slice.height == 0)
24088 return;
24089
24090 it->ascent = it->phys_ascent = glyph_ascent = image_ascent (img, face, &slice);
24091
24092 it->descent = slice.height - glyph_ascent;
24093 if (slice.y == 0)
24094 it->descent += img->vmargin;
24095 if (slice.y + slice.height == img->height)
24096 it->descent += img->vmargin;
24097 it->phys_descent = it->descent;
24098
24099 it->pixel_width = slice.width;
24100 if (slice.x == 0)
24101 it->pixel_width += img->hmargin;
24102 if (slice.x + slice.width == img->width)
24103 it->pixel_width += img->hmargin;
24104
24105 /* It's quite possible for images to have an ascent greater than
24106 their height, so don't get confused in that case. */
24107 if (it->descent < 0)
24108 it->descent = 0;
24109
24110 it->nglyphs = 1;
24111
24112 if (face->box != FACE_NO_BOX)
24113 {
24114 if (face->box_line_width > 0)
24115 {
24116 if (slice.y == 0)
24117 it->ascent += face->box_line_width;
24118 if (slice.y + slice.height == img->height)
24119 it->descent += face->box_line_width;
24120 }
24121
24122 if (it->start_of_box_run_p && slice.x == 0)
24123 it->pixel_width += eabs (face->box_line_width);
24124 if (it->end_of_box_run_p && slice.x + slice.width == img->width)
24125 it->pixel_width += eabs (face->box_line_width);
24126 }
24127
24128 take_vertical_position_into_account (it);
24129
24130 /* Automatically crop wide image glyphs at right edge so we can
24131 draw the cursor on same display row. */
24132 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24133 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24134 {
24135 it->pixel_width -= crop;
24136 slice.width -= crop;
24137 }
24138
24139 if (it->glyph_row)
24140 {
24141 struct glyph *glyph;
24142 enum glyph_row_area area = it->area;
24143
24144 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24145 if (glyph < it->glyph_row->glyphs[area + 1])
24146 {
24147 glyph->charpos = CHARPOS (it->position);
24148 glyph->object = it->object;
24149 glyph->pixel_width = it->pixel_width;
24150 glyph->ascent = glyph_ascent;
24151 glyph->descent = it->descent;
24152 glyph->voffset = it->voffset;
24153 glyph->type = IMAGE_GLYPH;
24154 glyph->avoid_cursor_p = it->avoid_cursor_p;
24155 glyph->multibyte_p = it->multibyte_p;
24156 glyph->left_box_line_p = it->start_of_box_run_p;
24157 glyph->right_box_line_p = it->end_of_box_run_p;
24158 glyph->overlaps_vertically_p = 0;
24159 glyph->padding_p = 0;
24160 glyph->glyph_not_available_p = 0;
24161 glyph->face_id = it->face_id;
24162 glyph->u.img_id = img->id;
24163 glyph->slice.img = slice;
24164 glyph->font_type = FONT_TYPE_UNKNOWN;
24165 if (it->bidi_p)
24166 {
24167 glyph->resolved_level = it->bidi_it.resolved_level;
24168 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24169 emacs_abort ();
24170 glyph->bidi_type = it->bidi_it.type;
24171 }
24172 ++it->glyph_row->used[area];
24173 }
24174 else
24175 IT_EXPAND_MATRIX_WIDTH (it, area);
24176 }
24177 }
24178
24179 #ifdef HAVE_XWIDGETS
24180 static void
24181 produce_xwidget_glyph (struct it *it)
24182 {
24183 struct xwidget* xw;
24184 struct face *face;
24185 int glyph_ascent, crop;
24186 printf("produce_xwidget_glyph:\n");
24187 eassert (it->what == IT_XWIDGET);
24188
24189 face = FACE_FROM_ID (it->f, it->face_id);
24190 eassert (face);
24191 /* Make sure X resources of the face is loaded. */
24192 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24193
24194 xw = it->xwidget;
24195 it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
24196 it->descent = xw->height/2;
24197 it->phys_descent = it->descent;
24198 it->pixel_width = xw->width;
24199 /* It's quite possible for images to have an ascent greater than
24200 their height, so don't get confused in that case. */
24201 if (it->descent < 0)
24202 it->descent = 0;
24203
24204 it->nglyphs = 1;
24205
24206 if (face->box != FACE_NO_BOX)
24207 {
24208 if (face->box_line_width > 0)
24209 {
24210 it->ascent += face->box_line_width;
24211 it->descent += face->box_line_width;
24212 }
24213
24214 if (it->start_of_box_run_p)
24215 it->pixel_width += eabs (face->box_line_width);
24216 it->pixel_width += eabs (face->box_line_width);
24217 }
24218
24219 take_vertical_position_into_account (it);
24220
24221 /* Automatically crop wide image glyphs at right edge so we can
24222 draw the cursor on same display row. */
24223 if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
24224 && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
24225 {
24226 it->pixel_width -= crop;
24227 }
24228
24229 if (it->glyph_row)
24230 {
24231 struct glyph *glyph;
24232 enum glyph_row_area area = it->area;
24233
24234 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24235 if (glyph < it->glyph_row->glyphs[area + 1])
24236 {
24237 glyph->charpos = CHARPOS (it->position);
24238 glyph->object = it->object;
24239 glyph->pixel_width = it->pixel_width;
24240 glyph->ascent = glyph_ascent;
24241 glyph->descent = it->descent;
24242 glyph->voffset = it->voffset;
24243 glyph->type = XWIDGET_GLYPH;
24244
24245 glyph->multibyte_p = it->multibyte_p;
24246 glyph->left_box_line_p = it->start_of_box_run_p;
24247 glyph->right_box_line_p = it->end_of_box_run_p;
24248 glyph->overlaps_vertically_p = 0;
24249 glyph->padding_p = 0;
24250 glyph->glyph_not_available_p = 0;
24251 glyph->face_id = it->face_id;
24252 glyph->u.xwidget = it->xwidget;
24253 //assert_valid_xwidget_id(glyph->u.xwidget_id,"produce_xwidget_glyph");
24254 glyph->font_type = FONT_TYPE_UNKNOWN;
24255 ++it->glyph_row->used[area];
24256 }
24257 else
24258 IT_EXPAND_MATRIX_WIDTH (it, area);
24259 }
24260 }
24261 #endif
24262
24263 /* Append a stretch glyph to IT->glyph_row. OBJECT is the source
24264 of the glyph, WIDTH and HEIGHT are the width and height of the
24265 stretch. ASCENT is the ascent of the glyph (0 <= ASCENT <= HEIGHT). */
24266
24267 static void
24268 append_stretch_glyph (struct it *it, Lisp_Object object,
24269 int width, int height, int ascent)
24270 {
24271 struct glyph *glyph;
24272 enum glyph_row_area area = it->area;
24273
24274 eassert (ascent >= 0 && ascent <= height);
24275
24276 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24277 if (glyph < it->glyph_row->glyphs[area + 1])
24278 {
24279 /* If the glyph row is reversed, we need to prepend the glyph
24280 rather than append it. */
24281 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24282 {
24283 struct glyph *g;
24284
24285 /* Make room for the additional glyph. */
24286 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24287 g[1] = *g;
24288 glyph = it->glyph_row->glyphs[area];
24289 }
24290 glyph->charpos = CHARPOS (it->position);
24291 glyph->object = object;
24292 glyph->pixel_width = width;
24293 glyph->ascent = ascent;
24294 glyph->descent = height - ascent;
24295 glyph->voffset = it->voffset;
24296 glyph->type = STRETCH_GLYPH;
24297 glyph->avoid_cursor_p = it->avoid_cursor_p;
24298 glyph->multibyte_p = it->multibyte_p;
24299 glyph->left_box_line_p = it->start_of_box_run_p;
24300 glyph->right_box_line_p = it->end_of_box_run_p;
24301 glyph->overlaps_vertically_p = 0;
24302 glyph->padding_p = 0;
24303 glyph->glyph_not_available_p = 0;
24304 glyph->face_id = it->face_id;
24305 glyph->u.stretch.ascent = ascent;
24306 glyph->u.stretch.height = height;
24307 glyph->slice.img = null_glyph_slice;
24308 glyph->font_type = FONT_TYPE_UNKNOWN;
24309 if (it->bidi_p)
24310 {
24311 glyph->resolved_level = it->bidi_it.resolved_level;
24312 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24313 emacs_abort ();
24314 glyph->bidi_type = it->bidi_it.type;
24315 }
24316 else
24317 {
24318 glyph->resolved_level = 0;
24319 glyph->bidi_type = UNKNOWN_BT;
24320 }
24321 ++it->glyph_row->used[area];
24322 }
24323 else
24324 IT_EXPAND_MATRIX_WIDTH (it, area);
24325 }
24326
24327 #endif /* HAVE_WINDOW_SYSTEM */
24328
24329 /* Produce a stretch glyph for iterator IT. IT->object is the value
24330 of the glyph property displayed. The value must be a list
24331 `(space KEYWORD VALUE ...)' with the following KEYWORD/VALUE pairs
24332 being recognized:
24333
24334 1. `:width WIDTH' specifies that the space should be WIDTH *
24335 canonical char width wide. WIDTH may be an integer or floating
24336 point number.
24337
24338 2. `:relative-width FACTOR' specifies that the width of the stretch
24339 should be computed from the width of the first character having the
24340 `glyph' property, and should be FACTOR times that width.
24341
24342 3. `:align-to HPOS' specifies that the space should be wide enough
24343 to reach HPOS, a value in canonical character units.
24344
24345 Exactly one of the above pairs must be present.
24346
24347 4. `:height HEIGHT' specifies that the height of the stretch produced
24348 should be HEIGHT, measured in canonical character units.
24349
24350 5. `:relative-height FACTOR' specifies that the height of the
24351 stretch should be FACTOR times the height of the characters having
24352 the glyph property.
24353
24354 Either none or exactly one of 4 or 5 must be present.
24355
24356 6. `:ascent ASCENT' specifies that ASCENT percent of the height
24357 of the stretch should be used for the ascent of the stretch.
24358 ASCENT must be in the range 0 <= ASCENT <= 100. */
24359
24360 void
24361 produce_stretch_glyph (struct it *it)
24362 {
24363 /* (space :width WIDTH :height HEIGHT ...) */
24364 Lisp_Object prop, plist;
24365 int width = 0, height = 0, align_to = -1;
24366 int zero_width_ok_p = 0;
24367 double tem;
24368 struct font *font = NULL;
24369
24370 #ifdef HAVE_WINDOW_SYSTEM
24371 int ascent = 0;
24372 int zero_height_ok_p = 0;
24373
24374 if (FRAME_WINDOW_P (it->f))
24375 {
24376 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24377 font = face->font ? face->font : FRAME_FONT (it->f);
24378 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24379 }
24380 #endif
24381
24382 /* List should start with `space'. */
24383 eassert (CONSP (it->object) && EQ (XCAR (it->object), Qspace));
24384 plist = XCDR (it->object);
24385
24386 /* Compute the width of the stretch. */
24387 if ((prop = Fplist_get (plist, QCwidth), !NILP (prop))
24388 && calc_pixel_width_or_height (&tem, it, prop, font, 1, 0))
24389 {
24390 /* Absolute width `:width WIDTH' specified and valid. */
24391 zero_width_ok_p = 1;
24392 width = (int)tem;
24393 }
24394 #ifdef HAVE_WINDOW_SYSTEM
24395 else if (FRAME_WINDOW_P (it->f)
24396 && (prop = Fplist_get (plist, QCrelative_width), NUMVAL (prop) > 0))
24397 {
24398 /* Relative width `:relative-width FACTOR' specified and valid.
24399 Compute the width of the characters having the `glyph'
24400 property. */
24401 struct it it2;
24402 unsigned char *p = BYTE_POS_ADDR (IT_BYTEPOS (*it));
24403
24404 it2 = *it;
24405 if (it->multibyte_p)
24406 it2.c = it2.char_to_display = STRING_CHAR_AND_LENGTH (p, it2.len);
24407 else
24408 {
24409 it2.c = it2.char_to_display = *p, it2.len = 1;
24410 if (! ASCII_CHAR_P (it2.c))
24411 it2.char_to_display = BYTE8_TO_CHAR (it2.c);
24412 }
24413
24414 it2.glyph_row = NULL;
24415 it2.what = IT_CHARACTER;
24416 x_produce_glyphs (&it2);
24417 width = NUMVAL (prop) * it2.pixel_width;
24418 }
24419 #endif /* HAVE_WINDOW_SYSTEM */
24420 else if ((prop = Fplist_get (plist, QCalign_to), !NILP (prop))
24421 && calc_pixel_width_or_height (&tem, it, prop, font, 1, &align_to))
24422 {
24423 if (it->glyph_row == NULL || !it->glyph_row->mode_line_p)
24424 align_to = (align_to < 0
24425 ? 0
24426 : align_to - window_box_left_offset (it->w, TEXT_AREA));
24427 else if (align_to < 0)
24428 align_to = window_box_left_offset (it->w, TEXT_AREA);
24429 width = max (0, (int)tem + align_to - it->current_x);
24430 zero_width_ok_p = 1;
24431 }
24432 else
24433 /* Nothing specified -> width defaults to canonical char width. */
24434 width = FRAME_COLUMN_WIDTH (it->f);
24435
24436 if (width <= 0 && (width < 0 || !zero_width_ok_p))
24437 width = 1;
24438
24439 #ifdef HAVE_WINDOW_SYSTEM
24440 /* Compute height. */
24441 if (FRAME_WINDOW_P (it->f))
24442 {
24443 if ((prop = Fplist_get (plist, QCheight), !NILP (prop))
24444 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24445 {
24446 height = (int)tem;
24447 zero_height_ok_p = 1;
24448 }
24449 else if (prop = Fplist_get (plist, QCrelative_height),
24450 NUMVAL (prop) > 0)
24451 height = FONT_HEIGHT (font) * NUMVAL (prop);
24452 else
24453 height = FONT_HEIGHT (font);
24454
24455 if (height <= 0 && (height < 0 || !zero_height_ok_p))
24456 height = 1;
24457
24458 /* Compute percentage of height used for ascent. If
24459 `:ascent ASCENT' is present and valid, use that. Otherwise,
24460 derive the ascent from the font in use. */
24461 if (prop = Fplist_get (plist, QCascent),
24462 NUMVAL (prop) > 0 && NUMVAL (prop) <= 100)
24463 ascent = height * NUMVAL (prop) / 100.0;
24464 else if (!NILP (prop)
24465 && calc_pixel_width_or_height (&tem, it, prop, font, 0, 0))
24466 ascent = min (max (0, (int)tem), height);
24467 else
24468 ascent = (height * FONT_BASE (font)) / FONT_HEIGHT (font);
24469 }
24470 else
24471 #endif /* HAVE_WINDOW_SYSTEM */
24472 height = 1;
24473
24474 if (width > 0 && it->line_wrap != TRUNCATE
24475 && it->current_x + width > it->last_visible_x)
24476 {
24477 width = it->last_visible_x - it->current_x;
24478 #ifdef HAVE_WINDOW_SYSTEM
24479 /* Subtract one more pixel from the stretch width, but only on
24480 GUI frames, since on a TTY each glyph is one "pixel" wide. */
24481 width -= FRAME_WINDOW_P (it->f);
24482 #endif
24483 }
24484
24485 if (width > 0 && height > 0 && it->glyph_row)
24486 {
24487 Lisp_Object o_object = it->object;
24488 Lisp_Object object = it->stack[it->sp - 1].string;
24489 int n = width;
24490
24491 if (!STRINGP (object))
24492 object = it->w->buffer;
24493 #ifdef HAVE_WINDOW_SYSTEM
24494 if (FRAME_WINDOW_P (it->f))
24495 append_stretch_glyph (it, object, width, height, ascent);
24496 else
24497 #endif
24498 {
24499 it->object = object;
24500 it->char_to_display = ' ';
24501 it->pixel_width = it->len = 1;
24502 while (n--)
24503 tty_append_glyph (it);
24504 it->object = o_object;
24505 }
24506 }
24507
24508 it->pixel_width = width;
24509 #ifdef HAVE_WINDOW_SYSTEM
24510 if (FRAME_WINDOW_P (it->f))
24511 {
24512 it->ascent = it->phys_ascent = ascent;
24513 it->descent = it->phys_descent = height - it->ascent;
24514 it->nglyphs = width > 0 && height > 0 ? 1 : 0;
24515 take_vertical_position_into_account (it);
24516 }
24517 else
24518 #endif
24519 it->nglyphs = width;
24520 }
24521
24522 /* Get information about special display element WHAT in an
24523 environment described by IT. WHAT is one of IT_TRUNCATION or
24524 IT_CONTINUATION. Maybe produce glyphs for WHAT if IT has a
24525 non-null glyph_row member. This function ensures that fields like
24526 face_id, c, len of IT are left untouched. */
24527
24528 static void
24529 produce_special_glyphs (struct it *it, enum display_element_type what)
24530 {
24531 struct it temp_it;
24532 Lisp_Object gc;
24533 GLYPH glyph;
24534
24535 temp_it = *it;
24536 temp_it.object = make_number (0);
24537 memset (&temp_it.current, 0, sizeof temp_it.current);
24538
24539 if (what == IT_CONTINUATION)
24540 {
24541 /* Continuation glyph. For R2L lines, we mirror it by hand. */
24542 if (it->bidi_it.paragraph_dir == R2L)
24543 SET_GLYPH_FROM_CHAR (glyph, '/');
24544 else
24545 SET_GLYPH_FROM_CHAR (glyph, '\\');
24546 if (it->dp
24547 && (gc = DISP_CONTINUE_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24548 {
24549 /* FIXME: Should we mirror GC for R2L lines? */
24550 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24551 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24552 }
24553 }
24554 else if (what == IT_TRUNCATION)
24555 {
24556 /* Truncation glyph. */
24557 SET_GLYPH_FROM_CHAR (glyph, '$');
24558 if (it->dp
24559 && (gc = DISP_TRUNC_GLYPH (it->dp), GLYPH_CODE_P (gc)))
24560 {
24561 /* FIXME: Should we mirror GC for R2L lines? */
24562 SET_GLYPH_FROM_GLYPH_CODE (glyph, gc);
24563 spec_glyph_lookup_face (XWINDOW (it->window), &glyph);
24564 }
24565 }
24566 else
24567 emacs_abort ();
24568
24569 #ifdef HAVE_WINDOW_SYSTEM
24570 /* On a GUI frame, when the right fringe (left fringe for R2L rows)
24571 is turned off, we precede the truncation/continuation glyphs by a
24572 stretch glyph whose width is computed such that these special
24573 glyphs are aligned at the window margin, even when very different
24574 fonts are used in different glyph rows. */
24575 if (FRAME_WINDOW_P (temp_it.f)
24576 /* init_iterator calls this with it->glyph_row == NULL, and it
24577 wants only the pixel width of the truncation/continuation
24578 glyphs. */
24579 && temp_it.glyph_row
24580 /* insert_left_trunc_glyphs calls us at the beginning of the
24581 row, and it has its own calculation of the stretch glyph
24582 width. */
24583 && temp_it.glyph_row->used[TEXT_AREA] > 0
24584 && (temp_it.glyph_row->reversed_p
24585 ? WINDOW_LEFT_FRINGE_WIDTH (temp_it.w)
24586 : WINDOW_RIGHT_FRINGE_WIDTH (temp_it.w)) == 0)
24587 {
24588 int stretch_width = temp_it.last_visible_x - temp_it.current_x;
24589
24590 if (stretch_width > 0)
24591 {
24592 struct face *face = FACE_FROM_ID (temp_it.f, temp_it.face_id);
24593 struct font *font =
24594 face->font ? face->font : FRAME_FONT (temp_it.f);
24595 int stretch_ascent =
24596 (((temp_it.ascent + temp_it.descent)
24597 * FONT_BASE (font)) / FONT_HEIGHT (font));
24598
24599 append_stretch_glyph (&temp_it, make_number (0), stretch_width,
24600 temp_it.ascent + temp_it.descent,
24601 stretch_ascent);
24602 }
24603 }
24604 #endif
24605
24606 temp_it.dp = NULL;
24607 temp_it.what = IT_CHARACTER;
24608 temp_it.len = 1;
24609 temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
24610 temp_it.face_id = GLYPH_FACE (glyph);
24611 temp_it.len = CHAR_BYTES (temp_it.c);
24612
24613 PRODUCE_GLYPHS (&temp_it);
24614 it->pixel_width = temp_it.pixel_width;
24615 it->nglyphs = temp_it.pixel_width;
24616 }
24617
24618 #ifdef HAVE_WINDOW_SYSTEM
24619
24620 /* Calculate line-height and line-spacing properties.
24621 An integer value specifies explicit pixel value.
24622 A float value specifies relative value to current face height.
24623 A cons (float . face-name) specifies relative value to
24624 height of specified face font.
24625
24626 Returns height in pixels, or nil. */
24627
24628
24629 static Lisp_Object
24630 calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
24631 int boff, int override)
24632 {
24633 Lisp_Object face_name = Qnil;
24634 int ascent, descent, height;
24635
24636 if (NILP (val) || INTEGERP (val) || (override && EQ (val, Qt)))
24637 return val;
24638
24639 if (CONSP (val))
24640 {
24641 face_name = XCAR (val);
24642 val = XCDR (val);
24643 if (!NUMBERP (val))
24644 val = make_number (1);
24645 if (NILP (face_name))
24646 {
24647 height = it->ascent + it->descent;
24648 goto scale;
24649 }
24650 }
24651
24652 if (NILP (face_name))
24653 {
24654 font = FRAME_FONT (it->f);
24655 boff = FRAME_BASELINE_OFFSET (it->f);
24656 }
24657 else if (EQ (face_name, Qt))
24658 {
24659 override = 0;
24660 }
24661 else
24662 {
24663 int face_id;
24664 struct face *face;
24665
24666 face_id = lookup_named_face (it->f, face_name, 0);
24667 if (face_id < 0)
24668 return make_number (-1);
24669
24670 face = FACE_FROM_ID (it->f, face_id);
24671 font = face->font;
24672 if (font == NULL)
24673 return make_number (-1);
24674 boff = font->baseline_offset;
24675 if (font->vertical_centering)
24676 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24677 }
24678
24679 ascent = FONT_BASE (font) + boff;
24680 descent = FONT_DESCENT (font) - boff;
24681
24682 if (override)
24683 {
24684 it->override_ascent = ascent;
24685 it->override_descent = descent;
24686 it->override_boff = boff;
24687 }
24688
24689 height = ascent + descent;
24690
24691 scale:
24692 if (FLOATP (val))
24693 height = (int)(XFLOAT_DATA (val) * height);
24694 else if (INTEGERP (val))
24695 height *= XINT (val);
24696
24697 return make_number (height);
24698 }
24699
24700
24701 /* Append a glyph for a glyphless character to IT->glyph_row. FACE_ID
24702 is a face ID to be used for the glyph. FOR_NO_FONT is nonzero if
24703 and only if this is for a character for which no font was found.
24704
24705 If the display method (it->glyphless_method) is
24706 GLYPHLESS_DISPLAY_ACRONYM or GLYPHLESS_DISPLAY_HEX_CODE, LEN is a
24707 length of the acronym or the hexadecimal string, UPPER_XOFF and
24708 UPPER_YOFF are pixel offsets for the upper part of the string,
24709 LOWER_XOFF and LOWER_YOFF are for the lower part.
24710
24711 For the other display methods, LEN through LOWER_YOFF are zero. */
24712
24713 static void
24714 append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
24715 short upper_xoff, short upper_yoff,
24716 short lower_xoff, short lower_yoff)
24717 {
24718 struct glyph *glyph;
24719 enum glyph_row_area area = it->area;
24720
24721 glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
24722 if (glyph < it->glyph_row->glyphs[area + 1])
24723 {
24724 /* If the glyph row is reversed, we need to prepend the glyph
24725 rather than append it. */
24726 if (it->glyph_row->reversed_p && area == TEXT_AREA)
24727 {
24728 struct glyph *g;
24729
24730 /* Make room for the additional glyph. */
24731 for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
24732 g[1] = *g;
24733 glyph = it->glyph_row->glyphs[area];
24734 }
24735 glyph->charpos = CHARPOS (it->position);
24736 glyph->object = it->object;
24737 glyph->pixel_width = it->pixel_width;
24738 glyph->ascent = it->ascent;
24739 glyph->descent = it->descent;
24740 glyph->voffset = it->voffset;
24741 glyph->type = GLYPHLESS_GLYPH;
24742 glyph->u.glyphless.method = it->glyphless_method;
24743 glyph->u.glyphless.for_no_font = for_no_font;
24744 glyph->u.glyphless.len = len;
24745 glyph->u.glyphless.ch = it->c;
24746 glyph->slice.glyphless.upper_xoff = upper_xoff;
24747 glyph->slice.glyphless.upper_yoff = upper_yoff;
24748 glyph->slice.glyphless.lower_xoff = lower_xoff;
24749 glyph->slice.glyphless.lower_yoff = lower_yoff;
24750 glyph->avoid_cursor_p = it->avoid_cursor_p;
24751 glyph->multibyte_p = it->multibyte_p;
24752 glyph->left_box_line_p = it->start_of_box_run_p;
24753 glyph->right_box_line_p = it->end_of_box_run_p;
24754 glyph->overlaps_vertically_p = (it->phys_ascent > it->ascent
24755 || it->phys_descent > it->descent);
24756 glyph->padding_p = 0;
24757 glyph->glyph_not_available_p = 0;
24758 glyph->face_id = face_id;
24759 glyph->font_type = FONT_TYPE_UNKNOWN;
24760 if (it->bidi_p)
24761 {
24762 glyph->resolved_level = it->bidi_it.resolved_level;
24763 if ((it->bidi_it.type & 7) != it->bidi_it.type)
24764 emacs_abort ();
24765 glyph->bidi_type = it->bidi_it.type;
24766 }
24767 ++it->glyph_row->used[area];
24768 }
24769 else
24770 IT_EXPAND_MATRIX_WIDTH (it, area);
24771 }
24772
24773
24774 /* Produce a glyph for a glyphless character for iterator IT.
24775 IT->glyphless_method specifies which method to use for displaying
24776 the character. See the description of enum
24777 glyphless_display_method in dispextern.h for the detail.
24778
24779 FOR_NO_FONT is nonzero if and only if this is for a character for
24780 which no font was found. ACRONYM, if non-nil, is an acronym string
24781 for the character. */
24782
24783 static void
24784 produce_glyphless_glyph (struct it *it, int for_no_font, Lisp_Object acronym)
24785 {
24786 int face_id;
24787 struct face *face;
24788 struct font *font;
24789 int base_width, base_height, width, height;
24790 short upper_xoff, upper_yoff, lower_xoff, lower_yoff;
24791 int len;
24792
24793 /* Get the metrics of the base font. We always refer to the current
24794 ASCII face. */
24795 face = FACE_FROM_ID (it->f, it->face_id)->ascii_face;
24796 font = face->font ? face->font : FRAME_FONT (it->f);
24797 it->ascent = FONT_BASE (font) + font->baseline_offset;
24798 it->descent = FONT_DESCENT (font) - font->baseline_offset;
24799 base_height = it->ascent + it->descent;
24800 base_width = font->average_width;
24801
24802 /* Get a face ID for the glyph by utilizing a cache (the same way as
24803 done for `escape-glyph' in get_next_display_element). */
24804 if (it->f == last_glyphless_glyph_frame
24805 && it->face_id == last_glyphless_glyph_face_id)
24806 {
24807 face_id = last_glyphless_glyph_merged_face_id;
24808 }
24809 else
24810 {
24811 /* Merge the `glyphless-char' face into the current face. */
24812 face_id = merge_faces (it->f, Qglyphless_char, 0, it->face_id);
24813 last_glyphless_glyph_frame = it->f;
24814 last_glyphless_glyph_face_id = it->face_id;
24815 last_glyphless_glyph_merged_face_id = face_id;
24816 }
24817
24818 if (it->glyphless_method == GLYPHLESS_DISPLAY_THIN_SPACE)
24819 {
24820 it->pixel_width = THIN_SPACE_WIDTH;
24821 len = 0;
24822 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24823 }
24824 else if (it->glyphless_method == GLYPHLESS_DISPLAY_EMPTY_BOX)
24825 {
24826 width = CHAR_WIDTH (it->c);
24827 if (width == 0)
24828 width = 1;
24829 else if (width > 4)
24830 width = 4;
24831 it->pixel_width = base_width * width;
24832 len = 0;
24833 upper_xoff = upper_yoff = lower_xoff = lower_yoff = 0;
24834 }
24835 else
24836 {
24837 char buf[7];
24838 const char *str;
24839 unsigned int code[6];
24840 int upper_len;
24841 int ascent, descent;
24842 struct font_metrics metrics_upper, metrics_lower;
24843
24844 face = FACE_FROM_ID (it->f, face_id);
24845 font = face->font ? face->font : FRAME_FONT (it->f);
24846 PREPARE_FACE_FOR_DISPLAY (it->f, face);
24847
24848 if (it->glyphless_method == GLYPHLESS_DISPLAY_ACRONYM)
24849 {
24850 if (! STRINGP (acronym) && CHAR_TABLE_P (Vglyphless_char_display))
24851 acronym = CHAR_TABLE_REF (Vglyphless_char_display, it->c);
24852 if (CONSP (acronym))
24853 acronym = XCAR (acronym);
24854 str = STRINGP (acronym) ? SSDATA (acronym) : "";
24855 }
24856 else
24857 {
24858 eassert (it->glyphless_method == GLYPHLESS_DISPLAY_HEX_CODE);
24859 sprintf (buf, "%0*X", it->c < 0x10000 ? 4 : 6, it->c);
24860 str = buf;
24861 }
24862 for (len = 0; str[len] && ASCII_BYTE_P (str[len]) && len < 6; len++)
24863 code[len] = font->driver->encode_char (font, str[len]);
24864 upper_len = (len + 1) / 2;
24865 font->driver->text_extents (font, code, upper_len,
24866 &metrics_upper);
24867 font->driver->text_extents (font, code + upper_len, len - upper_len,
24868 &metrics_lower);
24869
24870
24871
24872 /* +4 is for vertical bars of a box plus 1-pixel spaces at both side. */
24873 width = max (metrics_upper.width, metrics_lower.width) + 4;
24874 upper_xoff = upper_yoff = 2; /* the typical case */
24875 if (base_width >= width)
24876 {
24877 /* Align the upper to the left, the lower to the right. */
24878 it->pixel_width = base_width;
24879 lower_xoff = base_width - 2 - metrics_lower.width;
24880 }
24881 else
24882 {
24883 /* Center the shorter one. */
24884 it->pixel_width = width;
24885 if (metrics_upper.width >= metrics_lower.width)
24886 lower_xoff = (width - metrics_lower.width) / 2;
24887 else
24888 {
24889 /* FIXME: This code doesn't look right. It formerly was
24890 missing the "lower_xoff = 0;", which couldn't have
24891 been right since it left lower_xoff uninitialized. */
24892 lower_xoff = 0;
24893 upper_xoff = (width - metrics_upper.width) / 2;
24894 }
24895 }
24896
24897 /* +5 is for horizontal bars of a box plus 1-pixel spaces at
24898 top, bottom, and between upper and lower strings. */
24899 height = (metrics_upper.ascent + metrics_upper.descent
24900 + metrics_lower.ascent + metrics_lower.descent) + 5;
24901 /* Center vertically.
24902 H:base_height, D:base_descent
24903 h:height, ld:lower_descent, la:lower_ascent, ud:upper_descent
24904
24905 ascent = - (D - H/2 - h/2 + 1); "+ 1" for rounding up
24906 descent = D - H/2 + h/2;
24907 lower_yoff = descent - 2 - ld;
24908 upper_yoff = lower_yoff - la - 1 - ud; */
24909 ascent = - (it->descent - (base_height + height + 1) / 2);
24910 descent = it->descent - (base_height - height) / 2;
24911 lower_yoff = descent - 2 - metrics_lower.descent;
24912 upper_yoff = (lower_yoff - metrics_lower.ascent - 1
24913 - metrics_upper.descent);
24914 /* Don't make the height shorter than the base height. */
24915 if (height > base_height)
24916 {
24917 it->ascent = ascent;
24918 it->descent = descent;
24919 }
24920 }
24921
24922 it->phys_ascent = it->ascent;
24923 it->phys_descent = it->descent;
24924 if (it->glyph_row)
24925 append_glyphless_glyph (it, face_id, for_no_font, len,
24926 upper_xoff, upper_yoff,
24927 lower_xoff, lower_yoff);
24928 it->nglyphs = 1;
24929 take_vertical_position_into_account (it);
24930 }
24931
24932
24933 /* RIF:
24934 Produce glyphs/get display metrics for the display element IT is
24935 loaded with. See the description of struct it in dispextern.h
24936 for an overview of struct it. */
24937
24938 void
24939 x_produce_glyphs (struct it *it)
24940 {
24941 int extra_line_spacing = it->extra_line_spacing;
24942
24943 it->glyph_not_available_p = 0;
24944
24945 if (it->what == IT_CHARACTER)
24946 {
24947 XChar2b char2b;
24948 struct face *face = FACE_FROM_ID (it->f, it->face_id);
24949 struct font *font = face->font;
24950 struct font_metrics *pcm = NULL;
24951 int boff; /* baseline offset */
24952
24953 if (font == NULL)
24954 {
24955 /* When no suitable font is found, display this character by
24956 the method specified in the first extra slot of
24957 Vglyphless_char_display. */
24958 Lisp_Object acronym = lookup_glyphless_char_display (-1, it);
24959
24960 eassert (it->what == IT_GLYPHLESS);
24961 produce_glyphless_glyph (it, 1, STRINGP (acronym) ? acronym : Qnil);
24962 goto done;
24963 }
24964
24965 boff = font->baseline_offset;
24966 if (font->vertical_centering)
24967 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
24968
24969 if (it->char_to_display != '\n' && it->char_to_display != '\t')
24970 {
24971 int stretched_p;
24972
24973 it->nglyphs = 1;
24974
24975 if (it->override_ascent >= 0)
24976 {
24977 it->ascent = it->override_ascent;
24978 it->descent = it->override_descent;
24979 boff = it->override_boff;
24980 }
24981 else
24982 {
24983 it->ascent = FONT_BASE (font) + boff;
24984 it->descent = FONT_DESCENT (font) - boff;
24985 }
24986
24987 if (get_char_glyph_code (it->char_to_display, font, &char2b))
24988 {
24989 pcm = get_per_char_metric (font, &char2b);
24990 if (pcm->width == 0
24991 && pcm->rbearing == 0 && pcm->lbearing == 0)
24992 pcm = NULL;
24993 }
24994
24995 if (pcm)
24996 {
24997 it->phys_ascent = pcm->ascent + boff;
24998 it->phys_descent = pcm->descent - boff;
24999 it->pixel_width = pcm->width;
25000 }
25001 else
25002 {
25003 it->glyph_not_available_p = 1;
25004 it->phys_ascent = it->ascent;
25005 it->phys_descent = it->descent;
25006 it->pixel_width = font->space_width;
25007 }
25008
25009 if (it->constrain_row_ascent_descent_p)
25010 {
25011 if (it->descent > it->max_descent)
25012 {
25013 it->ascent += it->descent - it->max_descent;
25014 it->descent = it->max_descent;
25015 }
25016 if (it->ascent > it->max_ascent)
25017 {
25018 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25019 it->ascent = it->max_ascent;
25020 }
25021 it->phys_ascent = min (it->phys_ascent, it->ascent);
25022 it->phys_descent = min (it->phys_descent, it->descent);
25023 extra_line_spacing = 0;
25024 }
25025
25026 /* If this is a space inside a region of text with
25027 `space-width' property, change its width. */
25028 stretched_p = it->char_to_display == ' ' && !NILP (it->space_width);
25029 if (stretched_p)
25030 it->pixel_width *= XFLOATINT (it->space_width);
25031
25032 /* If face has a box, add the box thickness to the character
25033 height. If character has a box line to the left and/or
25034 right, add the box line width to the character's width. */
25035 if (face->box != FACE_NO_BOX)
25036 {
25037 int thick = face->box_line_width;
25038
25039 if (thick > 0)
25040 {
25041 it->ascent += thick;
25042 it->descent += thick;
25043 }
25044 else
25045 thick = -thick;
25046
25047 if (it->start_of_box_run_p)
25048 it->pixel_width += thick;
25049 if (it->end_of_box_run_p)
25050 it->pixel_width += thick;
25051 }
25052
25053 /* If face has an overline, add the height of the overline
25054 (1 pixel) and a 1 pixel margin to the character height. */
25055 if (face->overline_p)
25056 it->ascent += overline_margin;
25057
25058 if (it->constrain_row_ascent_descent_p)
25059 {
25060 if (it->ascent > it->max_ascent)
25061 it->ascent = it->max_ascent;
25062 if (it->descent > it->max_descent)
25063 it->descent = it->max_descent;
25064 }
25065
25066 take_vertical_position_into_account (it);
25067
25068 /* If we have to actually produce glyphs, do it. */
25069 if (it->glyph_row)
25070 {
25071 if (stretched_p)
25072 {
25073 /* Translate a space with a `space-width' property
25074 into a stretch glyph. */
25075 int ascent = (((it->ascent + it->descent) * FONT_BASE (font))
25076 / FONT_HEIGHT (font));
25077 append_stretch_glyph (it, it->object, it->pixel_width,
25078 it->ascent + it->descent, ascent);
25079 }
25080 else
25081 append_glyph (it);
25082
25083 /* If characters with lbearing or rbearing are displayed
25084 in this line, record that fact in a flag of the
25085 glyph row. This is used to optimize X output code. */
25086 if (pcm && (pcm->lbearing < 0 || pcm->rbearing > pcm->width))
25087 it->glyph_row->contains_overlapping_glyphs_p = 1;
25088 }
25089 if (! stretched_p && it->pixel_width == 0)
25090 /* We assure that all visible glyphs have at least 1-pixel
25091 width. */
25092 it->pixel_width = 1;
25093 }
25094 else if (it->char_to_display == '\n')
25095 {
25096 /* A newline has no width, but we need the height of the
25097 line. But if previous part of the line sets a height,
25098 don't increase that height */
25099
25100 Lisp_Object height;
25101 Lisp_Object total_height = Qnil;
25102
25103 it->override_ascent = -1;
25104 it->pixel_width = 0;
25105 it->nglyphs = 0;
25106
25107 height = get_it_property (it, Qline_height);
25108 /* Split (line-height total-height) list */
25109 if (CONSP (height)
25110 && CONSP (XCDR (height))
25111 && NILP (XCDR (XCDR (height))))
25112 {
25113 total_height = XCAR (XCDR (height));
25114 height = XCAR (height);
25115 }
25116 height = calc_line_height_property (it, height, font, boff, 1);
25117
25118 if (it->override_ascent >= 0)
25119 {
25120 it->ascent = it->override_ascent;
25121 it->descent = it->override_descent;
25122 boff = it->override_boff;
25123 }
25124 else
25125 {
25126 it->ascent = FONT_BASE (font) + boff;
25127 it->descent = FONT_DESCENT (font) - boff;
25128 }
25129
25130 if (EQ (height, Qt))
25131 {
25132 if (it->descent > it->max_descent)
25133 {
25134 it->ascent += it->descent - it->max_descent;
25135 it->descent = it->max_descent;
25136 }
25137 if (it->ascent > it->max_ascent)
25138 {
25139 it->descent = min (it->max_descent, it->descent + it->ascent - it->max_ascent);
25140 it->ascent = it->max_ascent;
25141 }
25142 it->phys_ascent = min (it->phys_ascent, it->ascent);
25143 it->phys_descent = min (it->phys_descent, it->descent);
25144 it->constrain_row_ascent_descent_p = 1;
25145 extra_line_spacing = 0;
25146 }
25147 else
25148 {
25149 Lisp_Object spacing;
25150
25151 it->phys_ascent = it->ascent;
25152 it->phys_descent = it->descent;
25153
25154 if ((it->max_ascent > 0 || it->max_descent > 0)
25155 && face->box != FACE_NO_BOX
25156 && face->box_line_width > 0)
25157 {
25158 it->ascent += face->box_line_width;
25159 it->descent += face->box_line_width;
25160 }
25161 if (!NILP (height)
25162 && XINT (height) > it->ascent + it->descent)
25163 it->ascent = XINT (height) - it->descent;
25164
25165 if (!NILP (total_height))
25166 spacing = calc_line_height_property (it, total_height, font, boff, 0);
25167 else
25168 {
25169 spacing = get_it_property (it, Qline_spacing);
25170 spacing = calc_line_height_property (it, spacing, font, boff, 0);
25171 }
25172 if (INTEGERP (spacing))
25173 {
25174 extra_line_spacing = XINT (spacing);
25175 if (!NILP (total_height))
25176 extra_line_spacing -= (it->phys_ascent + it->phys_descent);
25177 }
25178 }
25179 }
25180 else /* i.e. (it->char_to_display == '\t') */
25181 {
25182 if (font->space_width > 0)
25183 {
25184 int tab_width = it->tab_width * font->space_width;
25185 int x = it->current_x + it->continuation_lines_width;
25186 int next_tab_x = ((1 + x + tab_width - 1) / tab_width) * tab_width;
25187
25188 /* If the distance from the current position to the next tab
25189 stop is less than a space character width, use the
25190 tab stop after that. */
25191 if (next_tab_x - x < font->space_width)
25192 next_tab_x += tab_width;
25193
25194 it->pixel_width = next_tab_x - x;
25195 it->nglyphs = 1;
25196 it->ascent = it->phys_ascent = FONT_BASE (font) + boff;
25197 it->descent = it->phys_descent = FONT_DESCENT (font) - boff;
25198
25199 if (it->glyph_row)
25200 {
25201 append_stretch_glyph (it, it->object, it->pixel_width,
25202 it->ascent + it->descent, it->ascent);
25203 }
25204 }
25205 else
25206 {
25207 it->pixel_width = 0;
25208 it->nglyphs = 1;
25209 }
25210 }
25211 }
25212 else if (it->what == IT_COMPOSITION && it->cmp_it.ch < 0)
25213 {
25214 /* A static composition.
25215
25216 Note: A composition is represented as one glyph in the
25217 glyph matrix. There are no padding glyphs.
25218
25219 Important note: pixel_width, ascent, and descent are the
25220 values of what is drawn by draw_glyphs (i.e. the values of
25221 the overall glyphs composed). */
25222 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25223 int boff; /* baseline offset */
25224 struct composition *cmp = composition_table[it->cmp_it.id];
25225 int glyph_len = cmp->glyph_len;
25226 struct font *font = face->font;
25227
25228 it->nglyphs = 1;
25229
25230 /* If we have not yet calculated pixel size data of glyphs of
25231 the composition for the current face font, calculate them
25232 now. Theoretically, we have to check all fonts for the
25233 glyphs, but that requires much time and memory space. So,
25234 here we check only the font of the first glyph. This may
25235 lead to incorrect display, but it's very rare, and C-l
25236 (recenter-top-bottom) can correct the display anyway. */
25237 if (! cmp->font || cmp->font != font)
25238 {
25239 /* Ascent and descent of the font of the first character
25240 of this composition (adjusted by baseline offset).
25241 Ascent and descent of overall glyphs should not be less
25242 than these, respectively. */
25243 int font_ascent, font_descent, font_height;
25244 /* Bounding box of the overall glyphs. */
25245 int leftmost, rightmost, lowest, highest;
25246 int lbearing, rbearing;
25247 int i, width, ascent, descent;
25248 int left_padded = 0, right_padded = 0;
25249 int c IF_LINT (= 0); /* cmp->glyph_len can't be zero; see Bug#8512 */
25250 XChar2b char2b;
25251 struct font_metrics *pcm;
25252 int font_not_found_p;
25253 ptrdiff_t pos;
25254
25255 for (glyph_len = cmp->glyph_len; glyph_len > 0; glyph_len--)
25256 if ((c = COMPOSITION_GLYPH (cmp, glyph_len - 1)) != '\t')
25257 break;
25258 if (glyph_len < cmp->glyph_len)
25259 right_padded = 1;
25260 for (i = 0; i < glyph_len; i++)
25261 {
25262 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
25263 break;
25264 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25265 }
25266 if (i > 0)
25267 left_padded = 1;
25268
25269 pos = (STRINGP (it->string) ? IT_STRING_CHARPOS (*it)
25270 : IT_CHARPOS (*it));
25271 /* If no suitable font is found, use the default font. */
25272 font_not_found_p = font == NULL;
25273 if (font_not_found_p)
25274 {
25275 face = face->ascii_face;
25276 font = face->font;
25277 }
25278 boff = font->baseline_offset;
25279 if (font->vertical_centering)
25280 boff = VCENTER_BASELINE_OFFSET (font, it->f) - boff;
25281 font_ascent = FONT_BASE (font) + boff;
25282 font_descent = FONT_DESCENT (font) - boff;
25283 font_height = FONT_HEIGHT (font);
25284
25285 cmp->font = font;
25286
25287 pcm = NULL;
25288 if (! font_not_found_p)
25289 {
25290 get_char_face_and_encoding (it->f, c, it->face_id,
25291 &char2b, 0);
25292 pcm = get_per_char_metric (font, &char2b);
25293 }
25294
25295 /* Initialize the bounding box. */
25296 if (pcm)
25297 {
25298 width = cmp->glyph_len > 0 ? pcm->width : 0;
25299 ascent = pcm->ascent;
25300 descent = pcm->descent;
25301 lbearing = pcm->lbearing;
25302 rbearing = pcm->rbearing;
25303 }
25304 else
25305 {
25306 width = cmp->glyph_len > 0 ? font->space_width : 0;
25307 ascent = FONT_BASE (font);
25308 descent = FONT_DESCENT (font);
25309 lbearing = 0;
25310 rbearing = width;
25311 }
25312
25313 rightmost = width;
25314 leftmost = 0;
25315 lowest = - descent + boff;
25316 highest = ascent + boff;
25317
25318 if (! font_not_found_p
25319 && font->default_ascent
25320 && CHAR_TABLE_P (Vuse_default_ascent)
25321 && !NILP (Faref (Vuse_default_ascent,
25322 make_number (it->char_to_display))))
25323 highest = font->default_ascent + boff;
25324
25325 /* Draw the first glyph at the normal position. It may be
25326 shifted to right later if some other glyphs are drawn
25327 at the left. */
25328 cmp->offsets[i * 2] = 0;
25329 cmp->offsets[i * 2 + 1] = boff;
25330 cmp->lbearing = lbearing;
25331 cmp->rbearing = rbearing;
25332
25333 /* Set cmp->offsets for the remaining glyphs. */
25334 for (i++; i < glyph_len; i++)
25335 {
25336 int left, right, btm, top;
25337 int ch = COMPOSITION_GLYPH (cmp, i);
25338 int face_id;
25339 struct face *this_face;
25340
25341 if (ch == '\t')
25342 ch = ' ';
25343 face_id = FACE_FOR_CHAR (it->f, face, ch, pos, it->string);
25344 this_face = FACE_FROM_ID (it->f, face_id);
25345 font = this_face->font;
25346
25347 if (font == NULL)
25348 pcm = NULL;
25349 else
25350 {
25351 get_char_face_and_encoding (it->f, ch, face_id,
25352 &char2b, 0);
25353 pcm = get_per_char_metric (font, &char2b);
25354 }
25355 if (! pcm)
25356 cmp->offsets[i * 2] = cmp->offsets[i * 2 + 1] = 0;
25357 else
25358 {
25359 width = pcm->width;
25360 ascent = pcm->ascent;
25361 descent = pcm->descent;
25362 lbearing = pcm->lbearing;
25363 rbearing = pcm->rbearing;
25364 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
25365 {
25366 /* Relative composition with or without
25367 alternate chars. */
25368 left = (leftmost + rightmost - width) / 2;
25369 btm = - descent + boff;
25370 if (font->relative_compose
25371 && (! CHAR_TABLE_P (Vignore_relative_composition)
25372 || NILP (Faref (Vignore_relative_composition,
25373 make_number (ch)))))
25374 {
25375
25376 if (- descent >= font->relative_compose)
25377 /* One extra pixel between two glyphs. */
25378 btm = highest + 1;
25379 else if (ascent <= 0)
25380 /* One extra pixel between two glyphs. */
25381 btm = lowest - 1 - ascent - descent;
25382 }
25383 }
25384 else
25385 {
25386 /* A composition rule is specified by an integer
25387 value that encodes global and new reference
25388 points (GREF and NREF). GREF and NREF are
25389 specified by numbers as below:
25390
25391 0---1---2 -- ascent
25392 | |
25393 | |
25394 | |
25395 9--10--11 -- center
25396 | |
25397 ---3---4---5--- baseline
25398 | |
25399 6---7---8 -- descent
25400 */
25401 int rule = COMPOSITION_RULE (cmp, i);
25402 int gref, nref, grefx, grefy, nrefx, nrefy, xoff, yoff;
25403
25404 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
25405 grefx = gref % 3, nrefx = nref % 3;
25406 grefy = gref / 3, nrefy = nref / 3;
25407 if (xoff)
25408 xoff = font_height * (xoff - 128) / 256;
25409 if (yoff)
25410 yoff = font_height * (yoff - 128) / 256;
25411
25412 left = (leftmost
25413 + grefx * (rightmost - leftmost) / 2
25414 - nrefx * width / 2
25415 + xoff);
25416
25417 btm = ((grefy == 0 ? highest
25418 : grefy == 1 ? 0
25419 : grefy == 2 ? lowest
25420 : (highest + lowest) / 2)
25421 - (nrefy == 0 ? ascent + descent
25422 : nrefy == 1 ? descent - boff
25423 : nrefy == 2 ? 0
25424 : (ascent + descent) / 2)
25425 + yoff);
25426 }
25427
25428 cmp->offsets[i * 2] = left;
25429 cmp->offsets[i * 2 + 1] = btm + descent;
25430
25431 /* Update the bounding box of the overall glyphs. */
25432 if (width > 0)
25433 {
25434 right = left + width;
25435 if (left < leftmost)
25436 leftmost = left;
25437 if (right > rightmost)
25438 rightmost = right;
25439 }
25440 top = btm + descent + ascent;
25441 if (top > highest)
25442 highest = top;
25443 if (btm < lowest)
25444 lowest = btm;
25445
25446 if (cmp->lbearing > left + lbearing)
25447 cmp->lbearing = left + lbearing;
25448 if (cmp->rbearing < left + rbearing)
25449 cmp->rbearing = left + rbearing;
25450 }
25451 }
25452
25453 /* If there are glyphs whose x-offsets are negative,
25454 shift all glyphs to the right and make all x-offsets
25455 non-negative. */
25456 if (leftmost < 0)
25457 {
25458 for (i = 0; i < cmp->glyph_len; i++)
25459 cmp->offsets[i * 2] -= leftmost;
25460 rightmost -= leftmost;
25461 cmp->lbearing -= leftmost;
25462 cmp->rbearing -= leftmost;
25463 }
25464
25465 if (left_padded && cmp->lbearing < 0)
25466 {
25467 for (i = 0; i < cmp->glyph_len; i++)
25468 cmp->offsets[i * 2] -= cmp->lbearing;
25469 rightmost -= cmp->lbearing;
25470 cmp->rbearing -= cmp->lbearing;
25471 cmp->lbearing = 0;
25472 }
25473 if (right_padded && rightmost < cmp->rbearing)
25474 {
25475 rightmost = cmp->rbearing;
25476 }
25477
25478 cmp->pixel_width = rightmost;
25479 cmp->ascent = highest;
25480 cmp->descent = - lowest;
25481 if (cmp->ascent < font_ascent)
25482 cmp->ascent = font_ascent;
25483 if (cmp->descent < font_descent)
25484 cmp->descent = font_descent;
25485 }
25486
25487 if (it->glyph_row
25488 && (cmp->lbearing < 0
25489 || cmp->rbearing > cmp->pixel_width))
25490 it->glyph_row->contains_overlapping_glyphs_p = 1;
25491
25492 it->pixel_width = cmp->pixel_width;
25493 it->ascent = it->phys_ascent = cmp->ascent;
25494 it->descent = it->phys_descent = cmp->descent;
25495 if (face->box != FACE_NO_BOX)
25496 {
25497 int thick = face->box_line_width;
25498
25499 if (thick > 0)
25500 {
25501 it->ascent += thick;
25502 it->descent += thick;
25503 }
25504 else
25505 thick = - thick;
25506
25507 if (it->start_of_box_run_p)
25508 it->pixel_width += thick;
25509 if (it->end_of_box_run_p)
25510 it->pixel_width += thick;
25511 }
25512
25513 /* If face has an overline, add the height of the overline
25514 (1 pixel) and a 1 pixel margin to the character height. */
25515 if (face->overline_p)
25516 it->ascent += overline_margin;
25517
25518 take_vertical_position_into_account (it);
25519 if (it->ascent < 0)
25520 it->ascent = 0;
25521 if (it->descent < 0)
25522 it->descent = 0;
25523
25524 if (it->glyph_row && cmp->glyph_len > 0)
25525 append_composite_glyph (it);
25526 }
25527 else if (it->what == IT_COMPOSITION)
25528 {
25529 /* A dynamic (automatic) composition. */
25530 struct face *face = FACE_FROM_ID (it->f, it->face_id);
25531 Lisp_Object gstring;
25532 struct font_metrics metrics;
25533
25534 it->nglyphs = 1;
25535
25536 gstring = composition_gstring_from_id (it->cmp_it.id);
25537 it->pixel_width
25538 = composition_gstring_width (gstring, it->cmp_it.from, it->cmp_it.to,
25539 &metrics);
25540 if (it->glyph_row
25541 && (metrics.lbearing < 0 || metrics.rbearing > metrics.width))
25542 it->glyph_row->contains_overlapping_glyphs_p = 1;
25543 it->ascent = it->phys_ascent = metrics.ascent;
25544 it->descent = it->phys_descent = metrics.descent;
25545 if (face->box != FACE_NO_BOX)
25546 {
25547 int thick = face->box_line_width;
25548
25549 if (thick > 0)
25550 {
25551 it->ascent += thick;
25552 it->descent += thick;
25553 }
25554 else
25555 thick = - thick;
25556
25557 if (it->start_of_box_run_p)
25558 it->pixel_width += thick;
25559 if (it->end_of_box_run_p)
25560 it->pixel_width += thick;
25561 }
25562 /* If face has an overline, add the height of the overline
25563 (1 pixel) and a 1 pixel margin to the character height. */
25564 if (face->overline_p)
25565 it->ascent += overline_margin;
25566 take_vertical_position_into_account (it);
25567 if (it->ascent < 0)
25568 it->ascent = 0;
25569 if (it->descent < 0)
25570 it->descent = 0;
25571
25572 if (it->glyph_row)
25573 append_composite_glyph (it);
25574 }
25575 else if (it->what == IT_GLYPHLESS)
25576 produce_glyphless_glyph (it, 0, Qnil);
25577 else if (it->what == IT_IMAGE)
25578 produce_image_glyph (it);
25579 else if (it->what == IT_STRETCH)
25580 produce_stretch_glyph (it);
25581 #ifdef HAVE_XWIDGETS
25582 else if (it->what == IT_XWIDGET)
25583 produce_xwidget_glyph (it);
25584 #endif
25585 done:
25586 /* Accumulate dimensions. Note: can't assume that it->descent > 0
25587 because this isn't true for images with `:ascent 100'. */
25588 eassert (it->ascent >= 0 && it->descent >= 0);
25589 if (it->area == TEXT_AREA)
25590 it->current_x += it->pixel_width;
25591
25592 if (extra_line_spacing > 0)
25593 {
25594 it->descent += extra_line_spacing;
25595 if (extra_line_spacing > it->max_extra_line_spacing)
25596 it->max_extra_line_spacing = extra_line_spacing;
25597 }
25598
25599 it->max_ascent = max (it->max_ascent, it->ascent);
25600 it->max_descent = max (it->max_descent, it->descent);
25601 it->max_phys_ascent = max (it->max_phys_ascent, it->phys_ascent);
25602 it->max_phys_descent = max (it->max_phys_descent, it->phys_descent);
25603 }
25604
25605 /* EXPORT for RIF:
25606 Output LEN glyphs starting at START at the nominal cursor position.
25607 Advance the nominal cursor over the text. The global variable
25608 updated_window contains the window being updated, updated_row is
25609 the glyph row being updated, and updated_area is the area of that
25610 row being updated. */
25611
25612 void
25613 x_write_glyphs (struct glyph *start, int len)
25614 {
25615 int x, hpos, chpos = updated_window->phys_cursor.hpos;
25616
25617 eassert (updated_window && updated_row);
25618 /* When the window is hscrolled, cursor hpos can legitimately be out
25619 of bounds, but we draw the cursor at the corresponding window
25620 margin in that case. */
25621 if (!updated_row->reversed_p && chpos < 0)
25622 chpos = 0;
25623 if (updated_row->reversed_p && chpos >= updated_row->used[TEXT_AREA])
25624 chpos = updated_row->used[TEXT_AREA] - 1;
25625
25626 block_input ();
25627
25628 /* Write glyphs. */
25629
25630 hpos = start - updated_row->glyphs[updated_area];
25631 x = draw_glyphs (updated_window, output_cursor.x,
25632 updated_row, updated_area,
25633 hpos, hpos + len,
25634 DRAW_NORMAL_TEXT, 0);
25635
25636 /* Invalidate old phys cursor if the glyph at its hpos is redrawn. */
25637 if (updated_area == TEXT_AREA
25638 && updated_window->phys_cursor_on_p
25639 && updated_window->phys_cursor.vpos == output_cursor.vpos
25640 && chpos >= hpos
25641 && chpos < hpos + len)
25642 updated_window->phys_cursor_on_p = 0;
25643
25644 unblock_input ();
25645
25646 /* Advance the output cursor. */
25647 output_cursor.hpos += len;
25648 output_cursor.x = x;
25649 }
25650
25651
25652 /* EXPORT for RIF:
25653 Insert LEN glyphs from START at the nominal cursor position. */
25654
25655 void
25656 x_insert_glyphs (struct glyph *start, int len)
25657 {
25658 struct frame *f;
25659 struct window *w;
25660 int line_height, shift_by_width, shifted_region_width;
25661 struct glyph_row *row;
25662 struct glyph *glyph;
25663 int frame_x, frame_y;
25664 ptrdiff_t hpos;
25665
25666 eassert (updated_window && updated_row);
25667 block_input ();
25668 w = updated_window;
25669 f = XFRAME (WINDOW_FRAME (w));
25670
25671 /* Get the height of the line we are in. */
25672 row = updated_row;
25673 line_height = row->height;
25674
25675 /* Get the width of the glyphs to insert. */
25676 shift_by_width = 0;
25677 for (glyph = start; glyph < start + len; ++glyph)
25678 shift_by_width += glyph->pixel_width;
25679
25680 /* Get the width of the region to shift right. */
25681 shifted_region_width = (window_box_width (w, updated_area)
25682 - output_cursor.x
25683 - shift_by_width);
25684
25685 /* Shift right. */
25686 frame_x = window_box_left (w, updated_area) + output_cursor.x;
25687 frame_y = WINDOW_TO_FRAME_PIXEL_Y (w, output_cursor.y);
25688
25689 FRAME_RIF (f)->shift_glyphs_for_insert (f, frame_x, frame_y, shifted_region_width,
25690 line_height, shift_by_width);
25691
25692 /* Write the glyphs. */
25693 hpos = start - row->glyphs[updated_area];
25694 draw_glyphs (w, output_cursor.x, row, updated_area,
25695 hpos, hpos + len,
25696 DRAW_NORMAL_TEXT, 0);
25697
25698 /* Advance the output cursor. */
25699 output_cursor.hpos += len;
25700 output_cursor.x += shift_by_width;
25701 unblock_input ();
25702 }
25703
25704
25705 /* EXPORT for RIF:
25706 Erase the current text line from the nominal cursor position
25707 (inclusive) to pixel column TO_X (exclusive). The idea is that
25708 everything from TO_X onward is already erased.
25709
25710 TO_X is a pixel position relative to updated_area of
25711 updated_window. TO_X == -1 means clear to the end of this area. */
25712
25713 void
25714 x_clear_end_of_line (int to_x)
25715 {
25716 struct frame *f;
25717 struct window *w = updated_window;
25718 int max_x, min_y, max_y;
25719 int from_x, from_y, to_y;
25720
25721 eassert (updated_window && updated_row);
25722 f = XFRAME (w->frame);
25723
25724 if (updated_row->full_width_p)
25725 max_x = WINDOW_TOTAL_WIDTH (w);
25726 else
25727 max_x = window_box_width (w, updated_area);
25728 max_y = window_text_bottom_y (w);
25729
25730 /* TO_X == 0 means don't do anything. TO_X < 0 means clear to end
25731 of window. For TO_X > 0, truncate to end of drawing area. */
25732 if (to_x == 0)
25733 return;
25734 else if (to_x < 0)
25735 to_x = max_x;
25736 else
25737 to_x = min (to_x, max_x);
25738
25739 to_y = min (max_y, output_cursor.y + updated_row->height);
25740
25741 /* Notice if the cursor will be cleared by this operation. */
25742 if (!updated_row->full_width_p)
25743 notice_overwritten_cursor (w, updated_area,
25744 output_cursor.x, -1,
25745 updated_row->y,
25746 MATRIX_ROW_BOTTOM_Y (updated_row));
25747
25748 from_x = output_cursor.x;
25749
25750 /* Translate to frame coordinates. */
25751 if (updated_row->full_width_p)
25752 {
25753 from_x = WINDOW_TO_FRAME_PIXEL_X (w, from_x);
25754 to_x = WINDOW_TO_FRAME_PIXEL_X (w, to_x);
25755 }
25756 else
25757 {
25758 int area_left = window_box_left (w, updated_area);
25759 from_x += area_left;
25760 to_x += area_left;
25761 }
25762
25763 min_y = WINDOW_HEADER_LINE_HEIGHT (w);
25764 from_y = WINDOW_TO_FRAME_PIXEL_Y (w, max (min_y, output_cursor.y));
25765 to_y = WINDOW_TO_FRAME_PIXEL_Y (w, to_y);
25766
25767 /* Prevent inadvertently clearing to end of the X window. */
25768 if (to_x > from_x && to_y > from_y)
25769 {
25770 block_input ();
25771 FRAME_RIF (f)->clear_frame_area (f, from_x, from_y,
25772 to_x - from_x, to_y - from_y);
25773 unblock_input ();
25774 }
25775 }
25776
25777 #endif /* HAVE_WINDOW_SYSTEM */
25778
25779
25780 \f
25781 /***********************************************************************
25782 Cursor types
25783 ***********************************************************************/
25784
25785 /* Value is the internal representation of the specified cursor type
25786 ARG. If type is BAR_CURSOR, return in *WIDTH the specified width
25787 of the bar cursor. */
25788
25789 static enum text_cursor_kinds
25790 get_specified_cursor_type (Lisp_Object arg, int *width)
25791 {
25792 enum text_cursor_kinds type;
25793
25794 if (NILP (arg))
25795 return NO_CURSOR;
25796
25797 if (EQ (arg, Qbox))
25798 return FILLED_BOX_CURSOR;
25799
25800 if (EQ (arg, Qhollow))
25801 return HOLLOW_BOX_CURSOR;
25802
25803 if (EQ (arg, Qbar))
25804 {
25805 *width = 2;
25806 return BAR_CURSOR;
25807 }
25808
25809 if (CONSP (arg)
25810 && EQ (XCAR (arg), Qbar)
25811 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25812 {
25813 *width = XINT (XCDR (arg));
25814 return BAR_CURSOR;
25815 }
25816
25817 if (EQ (arg, Qhbar))
25818 {
25819 *width = 2;
25820 return HBAR_CURSOR;
25821 }
25822
25823 if (CONSP (arg)
25824 && EQ (XCAR (arg), Qhbar)
25825 && RANGED_INTEGERP (0, XCDR (arg), INT_MAX))
25826 {
25827 *width = XINT (XCDR (arg));
25828 return HBAR_CURSOR;
25829 }
25830
25831 /* Treat anything unknown as "hollow box cursor".
25832 It was bad to signal an error; people have trouble fixing
25833 .Xdefaults with Emacs, when it has something bad in it. */
25834 type = HOLLOW_BOX_CURSOR;
25835
25836 return type;
25837 }
25838
25839 /* Set the default cursor types for specified frame. */
25840 void
25841 set_frame_cursor_types (struct frame *f, Lisp_Object arg)
25842 {
25843 int width = 1;
25844 Lisp_Object tem;
25845
25846 FRAME_DESIRED_CURSOR (f) = get_specified_cursor_type (arg, &width);
25847 FRAME_CURSOR_WIDTH (f) = width;
25848
25849 /* By default, set up the blink-off state depending on the on-state. */
25850
25851 tem = Fassoc (arg, Vblink_cursor_alist);
25852 if (!NILP (tem))
25853 {
25854 FRAME_BLINK_OFF_CURSOR (f)
25855 = get_specified_cursor_type (XCDR (tem), &width);
25856 FRAME_BLINK_OFF_CURSOR_WIDTH (f) = width;
25857 }
25858 else
25859 FRAME_BLINK_OFF_CURSOR (f) = DEFAULT_CURSOR;
25860 }
25861
25862
25863 #ifdef HAVE_WINDOW_SYSTEM
25864
25865 /* Return the cursor we want to be displayed in window W. Return
25866 width of bar/hbar cursor through WIDTH arg. Return with
25867 ACTIVE_CURSOR arg set to 1 if cursor in window W is `active'
25868 (i.e. if the `system caret' should track this cursor).
25869
25870 In a mini-buffer window, we want the cursor only to appear if we
25871 are reading input from this window. For the selected window, we
25872 want the cursor type given by the frame parameter or buffer local
25873 setting of cursor-type. If explicitly marked off, draw no cursor.
25874 In all other cases, we want a hollow box cursor. */
25875
25876 static enum text_cursor_kinds
25877 get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
25878 int *active_cursor)
25879 {
25880 struct frame *f = XFRAME (w->frame);
25881 struct buffer *b = XBUFFER (w->buffer);
25882 int cursor_type = DEFAULT_CURSOR;
25883 Lisp_Object alt_cursor;
25884 int non_selected = 0;
25885
25886 *active_cursor = 1;
25887
25888 /* Echo area */
25889 if (cursor_in_echo_area
25890 && FRAME_HAS_MINIBUF_P (f)
25891 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window))
25892 {
25893 if (w == XWINDOW (echo_area_window))
25894 {
25895 if (EQ (BVAR (b, cursor_type), Qt) || NILP (BVAR (b, cursor_type)))
25896 {
25897 *width = FRAME_CURSOR_WIDTH (f);
25898 return FRAME_DESIRED_CURSOR (f);
25899 }
25900 else
25901 return get_specified_cursor_type (BVAR (b, cursor_type), width);
25902 }
25903
25904 *active_cursor = 0;
25905 non_selected = 1;
25906 }
25907
25908 /* Detect a nonselected window or nonselected frame. */
25909 else if (w != XWINDOW (f->selected_window)
25910 || f != FRAME_X_DISPLAY_INFO (f)->x_highlight_frame)
25911 {
25912 *active_cursor = 0;
25913
25914 if (MINI_WINDOW_P (w) && minibuf_level == 0)
25915 return NO_CURSOR;
25916
25917 non_selected = 1;
25918 }
25919
25920 /* Never display a cursor in a window in which cursor-type is nil. */
25921 if (NILP (BVAR (b, cursor_type)))
25922 return NO_CURSOR;
25923
25924 /* Get the normal cursor type for this window. */
25925 if (EQ (BVAR (b, cursor_type), Qt))
25926 {
25927 cursor_type = FRAME_DESIRED_CURSOR (f);
25928 *width = FRAME_CURSOR_WIDTH (f);
25929 }
25930 else
25931 cursor_type = get_specified_cursor_type (BVAR (b, cursor_type), width);
25932
25933 /* Use cursor-in-non-selected-windows instead
25934 for non-selected window or frame. */
25935 if (non_selected)
25936 {
25937 alt_cursor = BVAR (b, cursor_in_non_selected_windows);
25938 if (!EQ (Qt, alt_cursor))
25939 return get_specified_cursor_type (alt_cursor, width);
25940 /* t means modify the normal cursor type. */
25941 if (cursor_type == FILLED_BOX_CURSOR)
25942 cursor_type = HOLLOW_BOX_CURSOR;
25943 else if (cursor_type == BAR_CURSOR && *width > 1)
25944 --*width;
25945 return cursor_type;
25946 }
25947
25948 /* Use normal cursor if not blinked off. */
25949 if (!w->cursor_off_p)
25950 {
25951
25952 #ifdef HAVE_XWIDGETS
25953 if (glyph != NULL && glyph->type == XWIDGET_GLYPH){
25954 //printf("attempt xwidget cursor avoidance in get_window_cursor_type\n");
25955 return NO_CURSOR;
25956 }
25957 #endif
25958 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
25959 {
25960 if (cursor_type == FILLED_BOX_CURSOR)
25961 {
25962 /* Using a block cursor on large images can be very annoying.
25963 So use a hollow cursor for "large" images.
25964 If image is not transparent (no mask), also use hollow cursor. */
25965 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
25966 if (img != NULL && IMAGEP (img->spec))
25967 {
25968 /* Arbitrarily, interpret "Large" as >32x32 and >NxN
25969 where N = size of default frame font size.
25970 This should cover most of the "tiny" icons people may use. */
25971 if (!img->mask
25972 || img->width > max (32, WINDOW_FRAME_COLUMN_WIDTH (w))
25973 || img->height > max (32, WINDOW_FRAME_LINE_HEIGHT (w)))
25974 cursor_type = HOLLOW_BOX_CURSOR;
25975 }
25976 }
25977 else if (cursor_type != NO_CURSOR)
25978 {
25979 /* Display current only supports BOX and HOLLOW cursors for images.
25980 So for now, unconditionally use a HOLLOW cursor when cursor is
25981 not a solid box cursor. */
25982 cursor_type = HOLLOW_BOX_CURSOR;
25983 }
25984 }
25985 return cursor_type;
25986 }
25987
25988 /* Cursor is blinked off, so determine how to "toggle" it. */
25989
25990 /* First look for an entry matching the buffer's cursor-type in blink-cursor-alist. */
25991 if ((alt_cursor = Fassoc (BVAR (b, cursor_type), Vblink_cursor_alist), !NILP (alt_cursor)))
25992 return get_specified_cursor_type (XCDR (alt_cursor), width);
25993
25994 /* Then see if frame has specified a specific blink off cursor type. */
25995 if (FRAME_BLINK_OFF_CURSOR (f) != DEFAULT_CURSOR)
25996 {
25997 *width = FRAME_BLINK_OFF_CURSOR_WIDTH (f);
25998 return FRAME_BLINK_OFF_CURSOR (f);
25999 }
26000
26001 #if 0
26002 /* Some people liked having a permanently visible blinking cursor,
26003 while others had very strong opinions against it. So it was
26004 decided to remove it. KFS 2003-09-03 */
26005
26006 /* Finally perform built-in cursor blinking:
26007 filled box <-> hollow box
26008 wide [h]bar <-> narrow [h]bar
26009 narrow [h]bar <-> no cursor
26010 other type <-> no cursor */
26011
26012 if (cursor_type == FILLED_BOX_CURSOR)
26013 return HOLLOW_BOX_CURSOR;
26014
26015 if ((cursor_type == BAR_CURSOR || cursor_type == HBAR_CURSOR) && *width > 1)
26016 {
26017 *width = 1;
26018 return cursor_type;
26019 }
26020 #endif
26021
26022 return NO_CURSOR;
26023 }
26024
26025
26026 /* Notice when the text cursor of window W has been completely
26027 overwritten by a drawing operation that outputs glyphs in AREA
26028 starting at X0 and ending at X1 in the line starting at Y0 and
26029 ending at Y1. X coordinates are area-relative. X1 < 0 means all
26030 the rest of the line after X0 has been written. Y coordinates
26031 are window-relative. */
26032
26033 static void
26034 notice_overwritten_cursor (struct window *w, enum glyph_row_area area,
26035 int x0, int x1, int y0, int y1)
26036 {
26037 int cx0, cx1, cy0, cy1;
26038 struct glyph_row *row;
26039
26040 if (!w->phys_cursor_on_p)
26041 return;
26042 if (area != TEXT_AREA)
26043 return;
26044
26045 if (w->phys_cursor.vpos < 0
26046 || w->phys_cursor.vpos >= w->current_matrix->nrows
26047 || (row = w->current_matrix->rows + w->phys_cursor.vpos,
26048 !(row->enabled_p && row->displays_text_p)))
26049 return;
26050
26051 if (row->cursor_in_fringe_p)
26052 {
26053 row->cursor_in_fringe_p = 0;
26054 draw_fringe_bitmap (w, row, row->reversed_p);
26055 w->phys_cursor_on_p = 0;
26056 return;
26057 }
26058
26059 cx0 = w->phys_cursor.x;
26060 cx1 = cx0 + w->phys_cursor_width;
26061 if (x0 > cx0 || (x1 >= 0 && x1 < cx1))
26062 return;
26063
26064 /* The cursor image will be completely removed from the
26065 screen if the output area intersects the cursor area in
26066 y-direction. When we draw in [y0 y1[, and some part of
26067 the cursor is at y < y0, that part must have been drawn
26068 before. When scrolling, the cursor is erased before
26069 actually scrolling, so we don't come here. When not
26070 scrolling, the rows above the old cursor row must have
26071 changed, and in this case these rows must have written
26072 over the cursor image.
26073
26074 Likewise if part of the cursor is below y1, with the
26075 exception of the cursor being in the first blank row at
26076 the buffer and window end because update_text_area
26077 doesn't draw that row. (Except when it does, but
26078 that's handled in update_text_area.) */
26079
26080 cy0 = w->phys_cursor.y;
26081 cy1 = cy0 + w->phys_cursor_height;
26082 if ((y0 < cy0 || y0 >= cy1) && (y1 <= cy0 || y1 >= cy1))
26083 return;
26084
26085 w->phys_cursor_on_p = 0;
26086 }
26087
26088 #endif /* HAVE_WINDOW_SYSTEM */
26089
26090 \f
26091 /************************************************************************
26092 Mouse Face
26093 ************************************************************************/
26094
26095 #ifdef HAVE_WINDOW_SYSTEM
26096
26097 /* EXPORT for RIF:
26098 Fix the display of area AREA of overlapping row ROW in window W
26099 with respect to the overlapping part OVERLAPS. */
26100
26101 void
26102 x_fix_overlapping_area (struct window *w, struct glyph_row *row,
26103 enum glyph_row_area area, int overlaps)
26104 {
26105 int i, x;
26106
26107 block_input ();
26108
26109 x = 0;
26110 for (i = 0; i < row->used[area];)
26111 {
26112 if (row->glyphs[area][i].overlaps_vertically_p)
26113 {
26114 int start = i, start_x = x;
26115
26116 do
26117 {
26118 x += row->glyphs[area][i].pixel_width;
26119 ++i;
26120 }
26121 while (i < row->used[area]
26122 && row->glyphs[area][i].overlaps_vertically_p);
26123
26124 draw_glyphs (w, start_x, row, area,
26125 start, i,
26126 DRAW_NORMAL_TEXT, overlaps);
26127 }
26128 else
26129 {
26130 x += row->glyphs[area][i].pixel_width;
26131 ++i;
26132 }
26133 }
26134
26135 unblock_input ();
26136 }
26137
26138
26139 /* EXPORT:
26140 Draw the cursor glyph of window W in glyph row ROW. See the
26141 comment of draw_glyphs for the meaning of HL. */
26142
26143 void
26144 draw_phys_cursor_glyph (struct window *w, struct glyph_row *row,
26145 enum draw_glyphs_face hl)
26146 {
26147 /* If cursor hpos is out of bounds, don't draw garbage. This can
26148 happen in mini-buffer windows when switching between echo area
26149 glyphs and mini-buffer. */
26150 if ((row->reversed_p
26151 ? (w->phys_cursor.hpos >= 0)
26152 : (w->phys_cursor.hpos < row->used[TEXT_AREA])))
26153 {
26154 int on_p = w->phys_cursor_on_p;
26155 int x1;
26156 int hpos = w->phys_cursor.hpos;
26157
26158 /* When the window is hscrolled, cursor hpos can legitimately be
26159 out of bounds, but we draw the cursor at the corresponding
26160 window margin in that case. */
26161 if (!row->reversed_p && hpos < 0)
26162 hpos = 0;
26163 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26164 hpos = row->used[TEXT_AREA] - 1;
26165
26166 x1 = draw_glyphs (w, w->phys_cursor.x, row, TEXT_AREA, hpos, hpos + 1,
26167 hl, 0);
26168 w->phys_cursor_on_p = on_p;
26169
26170 if (hl == DRAW_CURSOR)
26171 w->phys_cursor_width = x1 - w->phys_cursor.x;
26172 /* When we erase the cursor, and ROW is overlapped by other
26173 rows, make sure that these overlapping parts of other rows
26174 are redrawn. */
26175 else if (hl == DRAW_NORMAL_TEXT && row->overlapped_p)
26176 {
26177 w->phys_cursor_width = x1 - w->phys_cursor.x;
26178
26179 if (row > w->current_matrix->rows
26180 && MATRIX_ROW_OVERLAPS_SUCC_P (row - 1))
26181 x_fix_overlapping_area (w, row - 1, TEXT_AREA,
26182 OVERLAPS_ERASED_CURSOR);
26183
26184 if (MATRIX_ROW_BOTTOM_Y (row) < window_text_bottom_y (w)
26185 && MATRIX_ROW_OVERLAPS_PRED_P (row + 1))
26186 x_fix_overlapping_area (w, row + 1, TEXT_AREA,
26187 OVERLAPS_ERASED_CURSOR);
26188 }
26189 }
26190 }
26191
26192
26193 /* EXPORT:
26194 Erase the image of a cursor of window W from the screen. */
26195
26196 void
26197 erase_phys_cursor (struct window *w)
26198 {
26199 struct frame *f = XFRAME (w->frame);
26200 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
26201 int hpos = w->phys_cursor.hpos;
26202 int vpos = w->phys_cursor.vpos;
26203 int mouse_face_here_p = 0;
26204 struct glyph_matrix *active_glyphs = w->current_matrix;
26205 struct glyph_row *cursor_row;
26206 struct glyph *cursor_glyph;
26207 enum draw_glyphs_face hl;
26208
26209 /* No cursor displayed or row invalidated => nothing to do on the
26210 screen. */
26211 if (w->phys_cursor_type == NO_CURSOR)
26212 goto mark_cursor_off;
26213
26214 /* VPOS >= active_glyphs->nrows means that window has been resized.
26215 Don't bother to erase the cursor. */
26216 if (vpos >= active_glyphs->nrows)
26217 goto mark_cursor_off;
26218
26219 /* If row containing cursor is marked invalid, there is nothing we
26220 can do. */
26221 cursor_row = MATRIX_ROW (active_glyphs, vpos);
26222 if (!cursor_row->enabled_p)
26223 goto mark_cursor_off;
26224
26225 /* If line spacing is > 0, old cursor may only be partially visible in
26226 window after split-window. So adjust visible height. */
26227 cursor_row->visible_height = min (cursor_row->visible_height,
26228 window_text_bottom_y (w) - cursor_row->y);
26229
26230 /* If row is completely invisible, don't attempt to delete a cursor which
26231 isn't there. This can happen if cursor is at top of a window, and
26232 we switch to a buffer with a header line in that window. */
26233 if (cursor_row->visible_height <= 0)
26234 goto mark_cursor_off;
26235
26236 /* If cursor is in the fringe, erase by drawing actual bitmap there. */
26237 if (cursor_row->cursor_in_fringe_p)
26238 {
26239 cursor_row->cursor_in_fringe_p = 0;
26240 draw_fringe_bitmap (w, cursor_row, cursor_row->reversed_p);
26241 goto mark_cursor_off;
26242 }
26243
26244 /* This can happen when the new row is shorter than the old one.
26245 In this case, either draw_glyphs or clear_end_of_line
26246 should have cleared the cursor. Note that we wouldn't be
26247 able to erase the cursor in this case because we don't have a
26248 cursor glyph at hand. */
26249 if ((cursor_row->reversed_p
26250 ? (w->phys_cursor.hpos < 0)
26251 : (w->phys_cursor.hpos >= cursor_row->used[TEXT_AREA])))
26252 goto mark_cursor_off;
26253
26254 /* When the window is hscrolled, cursor hpos can legitimately be out
26255 of bounds, but we draw the cursor at the corresponding window
26256 margin in that case. */
26257 if (!cursor_row->reversed_p && hpos < 0)
26258 hpos = 0;
26259 if (cursor_row->reversed_p && hpos >= cursor_row->used[TEXT_AREA])
26260 hpos = cursor_row->used[TEXT_AREA] - 1;
26261
26262 /* If the cursor is in the mouse face area, redisplay that when
26263 we clear the cursor. */
26264 if (! NILP (hlinfo->mouse_face_window)
26265 && coords_in_mouse_face_p (w, hpos, vpos)
26266 /* Don't redraw the cursor's spot in mouse face if it is at the
26267 end of a line (on a newline). The cursor appears there, but
26268 mouse highlighting does not. */
26269 && cursor_row->used[TEXT_AREA] > hpos && hpos >= 0)
26270 mouse_face_here_p = 1;
26271
26272 /* Maybe clear the display under the cursor. */
26273 if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
26274 {
26275 int x, y, left_x;
26276 int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
26277 int width;
26278
26279 cursor_glyph = get_phys_cursor_glyph (w);
26280 if (cursor_glyph == NULL)
26281 goto mark_cursor_off;
26282
26283 width = cursor_glyph->pixel_width;
26284 left_x = window_box_left_offset (w, TEXT_AREA);
26285 x = w->phys_cursor.x;
26286 if (x < left_x)
26287 width -= left_x - x;
26288 width = min (width, window_box_width (w, TEXT_AREA) - x);
26289 y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
26290 x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
26291
26292 if (width > 0)
26293 FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
26294 }
26295
26296 /* Erase the cursor by redrawing the character underneath it. */
26297 if (mouse_face_here_p)
26298 hl = DRAW_MOUSE_FACE;
26299 else
26300 hl = DRAW_NORMAL_TEXT;
26301 draw_phys_cursor_glyph (w, cursor_row, hl);
26302
26303 mark_cursor_off:
26304 w->phys_cursor_on_p = 0;
26305 w->phys_cursor_type = NO_CURSOR;
26306 }
26307
26308
26309 /* EXPORT:
26310 Display or clear cursor of window W. If ON is zero, clear the
26311 cursor. If it is non-zero, display the cursor. If ON is nonzero,
26312 where to put the cursor is specified by HPOS, VPOS, X and Y. */
26313
26314 void
26315 display_and_set_cursor (struct window *w, int on,
26316 int hpos, int vpos, int x, int y)
26317 {
26318 struct frame *f = XFRAME (w->frame);
26319 int new_cursor_type;
26320 int new_cursor_width;
26321 int active_cursor;
26322 struct glyph_row *glyph_row;
26323 struct glyph *glyph;
26324
26325 /* This is pointless on invisible frames, and dangerous on garbaged
26326 windows and frames; in the latter case, the frame or window may
26327 be in the midst of changing its size, and x and y may be off the
26328 window. */
26329 if (! FRAME_VISIBLE_P (f)
26330 || FRAME_GARBAGED_P (f)
26331 || vpos >= w->current_matrix->nrows
26332 || hpos >= w->current_matrix->matrix_w)
26333 return;
26334
26335 /* If cursor is off and we want it off, return quickly. */
26336 if (!on && !w->phys_cursor_on_p)
26337 return;
26338
26339 glyph_row = MATRIX_ROW (w->current_matrix, vpos);
26340 /* If cursor row is not enabled, we don't really know where to
26341 display the cursor. */
26342 if (!glyph_row->enabled_p)
26343 {
26344 w->phys_cursor_on_p = 0;
26345 return;
26346 }
26347
26348 glyph = NULL;
26349 if (!glyph_row->exact_window_width_line_p
26350 || (0 <= hpos && hpos < glyph_row->used[TEXT_AREA]))
26351 glyph = glyph_row->glyphs[TEXT_AREA] + hpos;
26352
26353 eassert (input_blocked_p ());
26354
26355 /* Set new_cursor_type to the cursor we want to be displayed. */
26356 new_cursor_type = get_window_cursor_type (w, glyph,
26357 &new_cursor_width, &active_cursor);
26358
26359 /* If cursor is currently being shown and we don't want it to be or
26360 it is in the wrong place, or the cursor type is not what we want,
26361 erase it. */
26362 if (w->phys_cursor_on_p
26363 && (!on
26364 || w->phys_cursor.x != x
26365 || w->phys_cursor.y != y
26366 || new_cursor_type != w->phys_cursor_type
26367 || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
26368 && new_cursor_width != w->phys_cursor_width)))
26369 erase_phys_cursor (w);
26370
26371 /* Don't check phys_cursor_on_p here because that flag is only set
26372 to zero in some cases where we know that the cursor has been
26373 completely erased, to avoid the extra work of erasing the cursor
26374 twice. In other words, phys_cursor_on_p can be 1 and the cursor
26375 still not be visible, or it has only been partly erased. */
26376 if (on)
26377 {
26378 w->phys_cursor_ascent = glyph_row->ascent;
26379 w->phys_cursor_height = glyph_row->height;
26380
26381 /* Set phys_cursor_.* before x_draw_.* is called because some
26382 of them may need the information. */
26383 w->phys_cursor.x = x;
26384 w->phys_cursor.y = glyph_row->y;
26385 w->phys_cursor.hpos = hpos;
26386 w->phys_cursor.vpos = vpos;
26387 }
26388
26389 FRAME_RIF (f)->draw_window_cursor (w, glyph_row, x, y,
26390 new_cursor_type, new_cursor_width,
26391 on, active_cursor);
26392 }
26393
26394
26395 /* Switch the display of W's cursor on or off, according to the value
26396 of ON. */
26397
26398 static void
26399 update_window_cursor (struct window *w, int on)
26400 {
26401 /* Don't update cursor in windows whose frame is in the process
26402 of being deleted. */
26403 if (w->current_matrix)
26404 {
26405 int hpos = w->phys_cursor.hpos;
26406 int vpos = w->phys_cursor.vpos;
26407 struct glyph_row *row;
26408
26409 if (vpos >= w->current_matrix->nrows
26410 || hpos >= w->current_matrix->matrix_w)
26411 return;
26412
26413 row = MATRIX_ROW (w->current_matrix, vpos);
26414
26415 /* When the window is hscrolled, cursor hpos can legitimately be
26416 out of bounds, but we draw the cursor at the corresponding
26417 window margin in that case. */
26418 if (!row->reversed_p && hpos < 0)
26419 hpos = 0;
26420 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26421 hpos = row->used[TEXT_AREA] - 1;
26422
26423 block_input ();
26424 display_and_set_cursor (w, on, hpos, vpos,
26425 w->phys_cursor.x, w->phys_cursor.y);
26426 unblock_input ();
26427 }
26428 }
26429
26430
26431 /* Call update_window_cursor with parameter ON_P on all leaf windows
26432 in the window tree rooted at W. */
26433
26434 static void
26435 update_cursor_in_window_tree (struct window *w, int on_p)
26436 {
26437 while (w)
26438 {
26439 if (!NILP (w->hchild))
26440 update_cursor_in_window_tree (XWINDOW (w->hchild), on_p);
26441 else if (!NILP (w->vchild))
26442 update_cursor_in_window_tree (XWINDOW (w->vchild), on_p);
26443 else
26444 update_window_cursor (w, on_p);
26445
26446 w = NILP (w->next) ? 0 : XWINDOW (w->next);
26447 }
26448 }
26449
26450
26451 /* EXPORT:
26452 Display the cursor on window W, or clear it, according to ON_P.
26453 Don't change the cursor's position. */
26454
26455 void
26456 x_update_cursor (struct frame *f, int on_p)
26457 {
26458 update_cursor_in_window_tree (XWINDOW (f->root_window), on_p);
26459 }
26460
26461
26462 /* EXPORT:
26463 Clear the cursor of window W to background color, and mark the
26464 cursor as not shown. This is used when the text where the cursor
26465 is about to be rewritten. */
26466
26467 void
26468 x_clear_cursor (struct window *w)
26469 {
26470 if (FRAME_VISIBLE_P (XFRAME (w->frame)) && w->phys_cursor_on_p)
26471 update_window_cursor (w, 0);
26472 }
26473
26474 #endif /* HAVE_WINDOW_SYSTEM */
26475
26476 /* Implementation of draw_row_with_mouse_face for GUI sessions, GPM,
26477 and MSDOS. */
26478 static void
26479 draw_row_with_mouse_face (struct window *w, int start_x, struct glyph_row *row,
26480 int start_hpos, int end_hpos,
26481 enum draw_glyphs_face draw)
26482 {
26483 #ifdef HAVE_WINDOW_SYSTEM
26484 if (FRAME_WINDOW_P (XFRAME (w->frame)))
26485 {
26486 draw_glyphs (w, start_x, row, TEXT_AREA, start_hpos, end_hpos, draw, 0);
26487 return;
26488 }
26489 #endif
26490 #if defined (HAVE_GPM) || defined (MSDOS) || defined (WINDOWSNT)
26491 tty_draw_row_with_mouse_face (w, row, start_hpos, end_hpos, draw);
26492 #endif
26493 }
26494
26495 /* Display the active region described by mouse_face_* according to DRAW. */
26496
26497 static void
26498 show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
26499 {
26500 struct window *w = XWINDOW (hlinfo->mouse_face_window);
26501 struct frame *f = XFRAME (WINDOW_FRAME (w));
26502
26503 if (/* If window is in the process of being destroyed, don't bother
26504 to do anything. */
26505 w->current_matrix != NULL
26506 /* Don't update mouse highlight if hidden */
26507 && (draw != DRAW_MOUSE_FACE || !hlinfo->mouse_face_hidden)
26508 /* Recognize when we are called to operate on rows that don't exist
26509 anymore. This can happen when a window is split. */
26510 && hlinfo->mouse_face_end_row < w->current_matrix->nrows)
26511 {
26512 int phys_cursor_on_p = w->phys_cursor_on_p;
26513 struct glyph_row *row, *first, *last;
26514
26515 first = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_beg_row);
26516 last = MATRIX_ROW (w->current_matrix, hlinfo->mouse_face_end_row);
26517
26518 for (row = first; row <= last && row->enabled_p; ++row)
26519 {
26520 int start_hpos, end_hpos, start_x;
26521
26522 /* For all but the first row, the highlight starts at column 0. */
26523 if (row == first)
26524 {
26525 /* R2L rows have BEG and END in reversed order, but the
26526 screen drawing geometry is always left to right. So
26527 we need to mirror the beginning and end of the
26528 highlighted area in R2L rows. */
26529 if (!row->reversed_p)
26530 {
26531 start_hpos = hlinfo->mouse_face_beg_col;
26532 start_x = hlinfo->mouse_face_beg_x;
26533 }
26534 else if (row == last)
26535 {
26536 start_hpos = hlinfo->mouse_face_end_col;
26537 start_x = hlinfo->mouse_face_end_x;
26538 }
26539 else
26540 {
26541 start_hpos = 0;
26542 start_x = 0;
26543 }
26544 }
26545 else if (row->reversed_p && row == last)
26546 {
26547 start_hpos = hlinfo->mouse_face_end_col;
26548 start_x = hlinfo->mouse_face_end_x;
26549 }
26550 else
26551 {
26552 start_hpos = 0;
26553 start_x = 0;
26554 }
26555
26556 if (row == last)
26557 {
26558 if (!row->reversed_p)
26559 end_hpos = hlinfo->mouse_face_end_col;
26560 else if (row == first)
26561 end_hpos = hlinfo->mouse_face_beg_col;
26562 else
26563 {
26564 end_hpos = row->used[TEXT_AREA];
26565 if (draw == DRAW_NORMAL_TEXT)
26566 row->fill_line_p = 1; /* Clear to end of line */
26567 }
26568 }
26569 else if (row->reversed_p && row == first)
26570 end_hpos = hlinfo->mouse_face_beg_col;
26571 else
26572 {
26573 end_hpos = row->used[TEXT_AREA];
26574 if (draw == DRAW_NORMAL_TEXT)
26575 row->fill_line_p = 1; /* Clear to end of line */
26576 }
26577
26578 if (end_hpos > start_hpos)
26579 {
26580 draw_row_with_mouse_face (w, start_x, row,
26581 start_hpos, end_hpos, draw);
26582
26583 row->mouse_face_p
26584 = draw == DRAW_MOUSE_FACE || draw == DRAW_IMAGE_RAISED;
26585 }
26586 }
26587
26588 #ifdef HAVE_WINDOW_SYSTEM
26589 /* When we've written over the cursor, arrange for it to
26590 be displayed again. */
26591 if (FRAME_WINDOW_P (f)
26592 && phys_cursor_on_p && !w->phys_cursor_on_p)
26593 {
26594 int hpos = w->phys_cursor.hpos;
26595
26596 /* When the window is hscrolled, cursor hpos can legitimately be
26597 out of bounds, but we draw the cursor at the corresponding
26598 window margin in that case. */
26599 if (!row->reversed_p && hpos < 0)
26600 hpos = 0;
26601 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26602 hpos = row->used[TEXT_AREA] - 1;
26603
26604 block_input ();
26605 display_and_set_cursor (w, 1, hpos, w->phys_cursor.vpos,
26606 w->phys_cursor.x, w->phys_cursor.y);
26607 unblock_input ();
26608 }
26609 #endif /* HAVE_WINDOW_SYSTEM */
26610 }
26611
26612 #ifdef HAVE_WINDOW_SYSTEM
26613 /* Change the mouse cursor. */
26614 if (FRAME_WINDOW_P (f))
26615 {
26616 if (draw == DRAW_NORMAL_TEXT
26617 && !EQ (hlinfo->mouse_face_window, f->tool_bar_window))
26618 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->text_cursor);
26619 else if (draw == DRAW_MOUSE_FACE)
26620 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->hand_cursor);
26621 else
26622 FRAME_RIF (f)->define_frame_cursor (f, FRAME_X_OUTPUT (f)->nontext_cursor);
26623 }
26624 #endif /* HAVE_WINDOW_SYSTEM */
26625 }
26626
26627 /* EXPORT:
26628 Clear out the mouse-highlighted active region.
26629 Redraw it un-highlighted first. Value is non-zero if mouse
26630 face was actually drawn unhighlighted. */
26631
26632 int
26633 clear_mouse_face (Mouse_HLInfo *hlinfo)
26634 {
26635 int cleared = 0;
26636
26637 if (!hlinfo->mouse_face_hidden && !NILP (hlinfo->mouse_face_window))
26638 {
26639 show_mouse_face (hlinfo, DRAW_NORMAL_TEXT);
26640 cleared = 1;
26641 }
26642
26643 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
26644 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
26645 hlinfo->mouse_face_window = Qnil;
26646 hlinfo->mouse_face_overlay = Qnil;
26647 return cleared;
26648 }
26649
26650 /* Return non-zero if the coordinates HPOS and VPOS on windows W are
26651 within the mouse face on that window. */
26652 static int
26653 coords_in_mouse_face_p (struct window *w, int hpos, int vpos)
26654 {
26655 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
26656
26657 /* Quickly resolve the easy cases. */
26658 if (!(WINDOWP (hlinfo->mouse_face_window)
26659 && XWINDOW (hlinfo->mouse_face_window) == w))
26660 return 0;
26661 if (vpos < hlinfo->mouse_face_beg_row
26662 || vpos > hlinfo->mouse_face_end_row)
26663 return 0;
26664 if (vpos > hlinfo->mouse_face_beg_row
26665 && vpos < hlinfo->mouse_face_end_row)
26666 return 1;
26667
26668 if (!MATRIX_ROW (w->current_matrix, vpos)->reversed_p)
26669 {
26670 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26671 {
26672 if (hlinfo->mouse_face_beg_col <= hpos && hpos < hlinfo->mouse_face_end_col)
26673 return 1;
26674 }
26675 else if ((vpos == hlinfo->mouse_face_beg_row
26676 && hpos >= hlinfo->mouse_face_beg_col)
26677 || (vpos == hlinfo->mouse_face_end_row
26678 && hpos < hlinfo->mouse_face_end_col))
26679 return 1;
26680 }
26681 else
26682 {
26683 if (hlinfo->mouse_face_beg_row == hlinfo->mouse_face_end_row)
26684 {
26685 if (hlinfo->mouse_face_end_col < hpos && hpos <= hlinfo->mouse_face_beg_col)
26686 return 1;
26687 }
26688 else if ((vpos == hlinfo->mouse_face_beg_row
26689 && hpos <= hlinfo->mouse_face_beg_col)
26690 || (vpos == hlinfo->mouse_face_end_row
26691 && hpos > hlinfo->mouse_face_end_col))
26692 return 1;
26693 }
26694 return 0;
26695 }
26696
26697
26698 /* EXPORT:
26699 Non-zero if physical cursor of window W is within mouse face. */
26700
26701 int
26702 cursor_in_mouse_face_p (struct window *w)
26703 {
26704 int hpos = w->phys_cursor.hpos;
26705 int vpos = w->phys_cursor.vpos;
26706 struct glyph_row *row = MATRIX_ROW (w->current_matrix, vpos);
26707
26708 /* When the window is hscrolled, cursor hpos can legitimately be out
26709 of bounds, but we draw the cursor at the corresponding window
26710 margin in that case. */
26711 if (!row->reversed_p && hpos < 0)
26712 hpos = 0;
26713 if (row->reversed_p && hpos >= row->used[TEXT_AREA])
26714 hpos = row->used[TEXT_AREA] - 1;
26715
26716 return coords_in_mouse_face_p (w, hpos, vpos);
26717 }
26718
26719
26720 \f
26721 /* Find the glyph rows START_ROW and END_ROW of window W that display
26722 characters between buffer positions START_CHARPOS and END_CHARPOS
26723 (excluding END_CHARPOS). DISP_STRING is a display string that
26724 covers these buffer positions. This is similar to
26725 row_containing_pos, but is more accurate when bidi reordering makes
26726 buffer positions change non-linearly with glyph rows. */
26727 static void
26728 rows_from_pos_range (struct window *w,
26729 ptrdiff_t start_charpos, ptrdiff_t end_charpos,
26730 Lisp_Object disp_string,
26731 struct glyph_row **start, struct glyph_row **end)
26732 {
26733 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26734 int last_y = window_text_bottom_y (w);
26735 struct glyph_row *row;
26736
26737 *start = NULL;
26738 *end = NULL;
26739
26740 while (!first->enabled_p
26741 && first < MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w))
26742 first++;
26743
26744 /* Find the START row. */
26745 for (row = first;
26746 row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y;
26747 row++)
26748 {
26749 /* A row can potentially be the START row if the range of the
26750 characters it displays intersects the range
26751 [START_CHARPOS..END_CHARPOS). */
26752 if (! ((start_charpos < MATRIX_ROW_START_CHARPOS (row)
26753 && end_charpos < MATRIX_ROW_START_CHARPOS (row))
26754 /* See the commentary in row_containing_pos, for the
26755 explanation of the complicated way to check whether
26756 some position is beyond the end of the characters
26757 displayed by a row. */
26758 || ((start_charpos > MATRIX_ROW_END_CHARPOS (row)
26759 || (start_charpos == MATRIX_ROW_END_CHARPOS (row)
26760 && !row->ends_at_zv_p
26761 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row)))
26762 && (end_charpos > MATRIX_ROW_END_CHARPOS (row)
26763 || (end_charpos == MATRIX_ROW_END_CHARPOS (row)
26764 && !row->ends_at_zv_p
26765 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (row))))))
26766 {
26767 /* Found a candidate row. Now make sure at least one of the
26768 glyphs it displays has a charpos from the range
26769 [START_CHARPOS..END_CHARPOS).
26770
26771 This is not obvious because bidi reordering could make
26772 buffer positions of a row be 1,2,3,102,101,100, and if we
26773 want to highlight characters in [50..60), we don't want
26774 this row, even though [50..60) does intersect [1..103),
26775 the range of character positions given by the row's start
26776 and end positions. */
26777 struct glyph *g = row->glyphs[TEXT_AREA];
26778 struct glyph *e = g + row->used[TEXT_AREA];
26779
26780 while (g < e)
26781 {
26782 if (((BUFFERP (g->object) || INTEGERP (g->object))
26783 && start_charpos <= g->charpos && g->charpos < end_charpos)
26784 /* A glyph that comes from DISP_STRING is by
26785 definition to be highlighted. */
26786 || EQ (g->object, disp_string))
26787 *start = row;
26788 g++;
26789 }
26790 if (*start)
26791 break;
26792 }
26793 }
26794
26795 /* Find the END row. */
26796 if (!*start
26797 /* If the last row is partially visible, start looking for END
26798 from that row, instead of starting from FIRST. */
26799 && !(row->enabled_p
26800 && row->y < last_y && MATRIX_ROW_BOTTOM_Y (row) > last_y))
26801 row = first;
26802 for ( ; row->enabled_p && MATRIX_ROW_BOTTOM_Y (row) <= last_y; row++)
26803 {
26804 struct glyph_row *next = row + 1;
26805 ptrdiff_t next_start = MATRIX_ROW_START_CHARPOS (next);
26806
26807 if (!next->enabled_p
26808 || next >= MATRIX_BOTTOM_TEXT_ROW (w->current_matrix, w)
26809 /* The first row >= START whose range of displayed characters
26810 does NOT intersect the range [START_CHARPOS..END_CHARPOS]
26811 is the row END + 1. */
26812 || (start_charpos < next_start
26813 && end_charpos < next_start)
26814 || ((start_charpos > MATRIX_ROW_END_CHARPOS (next)
26815 || (start_charpos == MATRIX_ROW_END_CHARPOS (next)
26816 && !next->ends_at_zv_p
26817 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))
26818 && (end_charpos > MATRIX_ROW_END_CHARPOS (next)
26819 || (end_charpos == MATRIX_ROW_END_CHARPOS (next)
26820 && !next->ends_at_zv_p
26821 && !MATRIX_ROW_ENDS_IN_MIDDLE_OF_CHAR_P (next)))))
26822 {
26823 *end = row;
26824 break;
26825 }
26826 else
26827 {
26828 /* If the next row's edges intersect [START_CHARPOS..END_CHARPOS],
26829 but none of the characters it displays are in the range, it is
26830 also END + 1. */
26831 struct glyph *g = next->glyphs[TEXT_AREA];
26832 struct glyph *s = g;
26833 struct glyph *e = g + next->used[TEXT_AREA];
26834
26835 while (g < e)
26836 {
26837 if (((BUFFERP (g->object) || INTEGERP (g->object))
26838 && ((start_charpos <= g->charpos && g->charpos < end_charpos)
26839 /* If the buffer position of the first glyph in
26840 the row is equal to END_CHARPOS, it means
26841 the last character to be highlighted is the
26842 newline of ROW, and we must consider NEXT as
26843 END, not END+1. */
26844 || (((!next->reversed_p && g == s)
26845 || (next->reversed_p && g == e - 1))
26846 && (g->charpos == end_charpos
26847 /* Special case for when NEXT is an
26848 empty line at ZV. */
26849 || (g->charpos == -1
26850 && !row->ends_at_zv_p
26851 && next_start == end_charpos)))))
26852 /* A glyph that comes from DISP_STRING is by
26853 definition to be highlighted. */
26854 || EQ (g->object, disp_string))
26855 break;
26856 g++;
26857 }
26858 if (g == e)
26859 {
26860 *end = row;
26861 break;
26862 }
26863 /* The first row that ends at ZV must be the last to be
26864 highlighted. */
26865 else if (next->ends_at_zv_p)
26866 {
26867 *end = next;
26868 break;
26869 }
26870 }
26871 }
26872 }
26873
26874 /* This function sets the mouse_face_* elements of HLINFO, assuming
26875 the mouse cursor is on a glyph with buffer charpos MOUSE_CHARPOS in
26876 window WINDOW. START_CHARPOS and END_CHARPOS are buffer positions
26877 for the overlay or run of text properties specifying the mouse
26878 face. BEFORE_STRING and AFTER_STRING, if non-nil, are a
26879 before-string and after-string that must also be highlighted.
26880 DISP_STRING, if non-nil, is a display string that may cover some
26881 or all of the highlighted text. */
26882
26883 static void
26884 mouse_face_from_buffer_pos (Lisp_Object window,
26885 Mouse_HLInfo *hlinfo,
26886 ptrdiff_t mouse_charpos,
26887 ptrdiff_t start_charpos,
26888 ptrdiff_t end_charpos,
26889 Lisp_Object before_string,
26890 Lisp_Object after_string,
26891 Lisp_Object disp_string)
26892 {
26893 struct window *w = XWINDOW (window);
26894 struct glyph_row *first = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
26895 struct glyph_row *r1, *r2;
26896 struct glyph *glyph, *end;
26897 ptrdiff_t ignore, pos;
26898 int x;
26899
26900 eassert (NILP (disp_string) || STRINGP (disp_string));
26901 eassert (NILP (before_string) || STRINGP (before_string));
26902 eassert (NILP (after_string) || STRINGP (after_string));
26903
26904 /* Find the rows corresponding to START_CHARPOS and END_CHARPOS. */
26905 rows_from_pos_range (w, start_charpos, end_charpos, disp_string, &r1, &r2);
26906 if (r1 == NULL)
26907 r1 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26908 /* If the before-string or display-string contains newlines,
26909 rows_from_pos_range skips to its last row. Move back. */
26910 if (!NILP (before_string) || !NILP (disp_string))
26911 {
26912 struct glyph_row *prev;
26913 while ((prev = r1 - 1, prev >= first)
26914 && MATRIX_ROW_END_CHARPOS (prev) == start_charpos
26915 && prev->used[TEXT_AREA] > 0)
26916 {
26917 struct glyph *beg = prev->glyphs[TEXT_AREA];
26918 glyph = beg + prev->used[TEXT_AREA];
26919 while (--glyph >= beg && INTEGERP (glyph->object));
26920 if (glyph < beg
26921 || !(EQ (glyph->object, before_string)
26922 || EQ (glyph->object, disp_string)))
26923 break;
26924 r1 = prev;
26925 }
26926 }
26927 if (r2 == NULL)
26928 {
26929 r2 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26930 hlinfo->mouse_face_past_end = 1;
26931 }
26932 else if (!NILP (after_string))
26933 {
26934 /* If the after-string has newlines, advance to its last row. */
26935 struct glyph_row *next;
26936 struct glyph_row *last
26937 = MATRIX_ROW (w->current_matrix, XFASTINT (w->window_end_vpos));
26938
26939 for (next = r2 + 1;
26940 next <= last
26941 && next->used[TEXT_AREA] > 0
26942 && EQ (next->glyphs[TEXT_AREA]->object, after_string);
26943 ++next)
26944 r2 = next;
26945 }
26946 /* The rest of the display engine assumes that mouse_face_beg_row is
26947 either above mouse_face_end_row or identical to it. But with
26948 bidi-reordered continued lines, the row for START_CHARPOS could
26949 be below the row for END_CHARPOS. If so, swap the rows and store
26950 them in correct order. */
26951 if (r1->y > r2->y)
26952 {
26953 struct glyph_row *tem = r2;
26954
26955 r2 = r1;
26956 r1 = tem;
26957 }
26958
26959 hlinfo->mouse_face_beg_y = r1->y;
26960 hlinfo->mouse_face_beg_row = MATRIX_ROW_VPOS (r1, w->current_matrix);
26961 hlinfo->mouse_face_end_y = r2->y;
26962 hlinfo->mouse_face_end_row = MATRIX_ROW_VPOS (r2, w->current_matrix);
26963
26964 /* For a bidi-reordered row, the positions of BEFORE_STRING,
26965 AFTER_STRING, DISP_STRING, START_CHARPOS, and END_CHARPOS
26966 could be anywhere in the row and in any order. The strategy
26967 below is to find the leftmost and the rightmost glyph that
26968 belongs to either of these 3 strings, or whose position is
26969 between START_CHARPOS and END_CHARPOS, and highlight all the
26970 glyphs between those two. This may cover more than just the text
26971 between START_CHARPOS and END_CHARPOS if the range of characters
26972 strides the bidi level boundary, e.g. if the beginning is in R2L
26973 text while the end is in L2R text or vice versa. */
26974 if (!r1->reversed_p)
26975 {
26976 /* This row is in a left to right paragraph. Scan it left to
26977 right. */
26978 glyph = r1->glyphs[TEXT_AREA];
26979 end = glyph + r1->used[TEXT_AREA];
26980 x = r1->x;
26981
26982 /* Skip truncation glyphs at the start of the glyph row. */
26983 if (r1->displays_text_p)
26984 for (; glyph < end
26985 && INTEGERP (glyph->object)
26986 && glyph->charpos < 0;
26987 ++glyph)
26988 x += glyph->pixel_width;
26989
26990 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
26991 or DISP_STRING, and the first glyph from buffer whose
26992 position is between START_CHARPOS and END_CHARPOS. */
26993 for (; glyph < end
26994 && !INTEGERP (glyph->object)
26995 && !EQ (glyph->object, disp_string)
26996 && !(BUFFERP (glyph->object)
26997 && (glyph->charpos >= start_charpos
26998 && glyph->charpos < end_charpos));
26999 ++glyph)
27000 {
27001 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27002 are present at buffer positions between START_CHARPOS and
27003 END_CHARPOS, or if they come from an overlay. */
27004 if (EQ (glyph->object, before_string))
27005 {
27006 pos = string_buffer_position (before_string,
27007 start_charpos);
27008 /* If pos == 0, it means before_string came from an
27009 overlay, not from a buffer position. */
27010 if (!pos || (pos >= start_charpos && pos < end_charpos))
27011 break;
27012 }
27013 else if (EQ (glyph->object, after_string))
27014 {
27015 pos = string_buffer_position (after_string, end_charpos);
27016 if (!pos || (pos >= start_charpos && pos < end_charpos))
27017 break;
27018 }
27019 x += glyph->pixel_width;
27020 }
27021 hlinfo->mouse_face_beg_x = x;
27022 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27023 }
27024 else
27025 {
27026 /* This row is in a right to left paragraph. Scan it right to
27027 left. */
27028 struct glyph *g;
27029
27030 end = r1->glyphs[TEXT_AREA] - 1;
27031 glyph = end + r1->used[TEXT_AREA];
27032
27033 /* Skip truncation glyphs at the start of the glyph row. */
27034 if (r1->displays_text_p)
27035 for (; glyph > end
27036 && INTEGERP (glyph->object)
27037 && glyph->charpos < 0;
27038 --glyph)
27039 ;
27040
27041 /* Scan the glyph row, looking for BEFORE_STRING, AFTER_STRING,
27042 or DISP_STRING, and the first glyph from buffer whose
27043 position is between START_CHARPOS and END_CHARPOS. */
27044 for (; glyph > end
27045 && !INTEGERP (glyph->object)
27046 && !EQ (glyph->object, disp_string)
27047 && !(BUFFERP (glyph->object)
27048 && (glyph->charpos >= start_charpos
27049 && glyph->charpos < end_charpos));
27050 --glyph)
27051 {
27052 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27053 are present at buffer positions between START_CHARPOS and
27054 END_CHARPOS, or if they come from an overlay. */
27055 if (EQ (glyph->object, before_string))
27056 {
27057 pos = string_buffer_position (before_string, start_charpos);
27058 /* If pos == 0, it means before_string came from an
27059 overlay, not from a buffer position. */
27060 if (!pos || (pos >= start_charpos && pos < end_charpos))
27061 break;
27062 }
27063 else if (EQ (glyph->object, after_string))
27064 {
27065 pos = string_buffer_position (after_string, end_charpos);
27066 if (!pos || (pos >= start_charpos && pos < end_charpos))
27067 break;
27068 }
27069 }
27070
27071 glyph++; /* first glyph to the right of the highlighted area */
27072 for (g = r1->glyphs[TEXT_AREA], x = r1->x; g < glyph; g++)
27073 x += g->pixel_width;
27074 hlinfo->mouse_face_beg_x = x;
27075 hlinfo->mouse_face_beg_col = glyph - r1->glyphs[TEXT_AREA];
27076 }
27077
27078 /* If the highlight ends in a different row, compute GLYPH and END
27079 for the end row. Otherwise, reuse the values computed above for
27080 the row where the highlight begins. */
27081 if (r2 != r1)
27082 {
27083 if (!r2->reversed_p)
27084 {
27085 glyph = r2->glyphs[TEXT_AREA];
27086 end = glyph + r2->used[TEXT_AREA];
27087 x = r2->x;
27088 }
27089 else
27090 {
27091 end = r2->glyphs[TEXT_AREA] - 1;
27092 glyph = end + r2->used[TEXT_AREA];
27093 }
27094 }
27095
27096 if (!r2->reversed_p)
27097 {
27098 /* Skip truncation and continuation glyphs near the end of the
27099 row, and also blanks and stretch glyphs inserted by
27100 extend_face_to_end_of_line. */
27101 while (end > glyph
27102 && INTEGERP ((end - 1)->object))
27103 --end;
27104 /* Scan the rest of the glyph row from the end, looking for the
27105 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27106 DISP_STRING, or whose position is between START_CHARPOS
27107 and END_CHARPOS */
27108 for (--end;
27109 end > glyph
27110 && !INTEGERP (end->object)
27111 && !EQ (end->object, disp_string)
27112 && !(BUFFERP (end->object)
27113 && (end->charpos >= start_charpos
27114 && end->charpos < end_charpos));
27115 --end)
27116 {
27117 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27118 are present at buffer positions between START_CHARPOS and
27119 END_CHARPOS, or if they come from an overlay. */
27120 if (EQ (end->object, before_string))
27121 {
27122 pos = string_buffer_position (before_string, start_charpos);
27123 if (!pos || (pos >= start_charpos && pos < end_charpos))
27124 break;
27125 }
27126 else if (EQ (end->object, after_string))
27127 {
27128 pos = string_buffer_position (after_string, end_charpos);
27129 if (!pos || (pos >= start_charpos && pos < end_charpos))
27130 break;
27131 }
27132 }
27133 /* Find the X coordinate of the last glyph to be highlighted. */
27134 for (; glyph <= end; ++glyph)
27135 x += glyph->pixel_width;
27136
27137 hlinfo->mouse_face_end_x = x;
27138 hlinfo->mouse_face_end_col = glyph - r2->glyphs[TEXT_AREA];
27139 }
27140 else
27141 {
27142 /* Skip truncation and continuation glyphs near the end of the
27143 row, and also blanks and stretch glyphs inserted by
27144 extend_face_to_end_of_line. */
27145 x = r2->x;
27146 end++;
27147 while (end < glyph
27148 && INTEGERP (end->object))
27149 {
27150 x += end->pixel_width;
27151 ++end;
27152 }
27153 /* Scan the rest of the glyph row from the end, looking for the
27154 first glyph that comes from BEFORE_STRING, AFTER_STRING, or
27155 DISP_STRING, or whose position is between START_CHARPOS
27156 and END_CHARPOS */
27157 for ( ;
27158 end < glyph
27159 && !INTEGERP (end->object)
27160 && !EQ (end->object, disp_string)
27161 && !(BUFFERP (end->object)
27162 && (end->charpos >= start_charpos
27163 && end->charpos < end_charpos));
27164 ++end)
27165 {
27166 /* BEFORE_STRING or AFTER_STRING are only relevant if they
27167 are present at buffer positions between START_CHARPOS and
27168 END_CHARPOS, or if they come from an overlay. */
27169 if (EQ (end->object, before_string))
27170 {
27171 pos = string_buffer_position (before_string, start_charpos);
27172 if (!pos || (pos >= start_charpos && pos < end_charpos))
27173 break;
27174 }
27175 else if (EQ (end->object, after_string))
27176 {
27177 pos = string_buffer_position (after_string, end_charpos);
27178 if (!pos || (pos >= start_charpos && pos < end_charpos))
27179 break;
27180 }
27181 x += end->pixel_width;
27182 }
27183 /* If we exited the above loop because we arrived at the last
27184 glyph of the row, and its buffer position is still not in
27185 range, it means the last character in range is the preceding
27186 newline. Bump the end column and x values to get past the
27187 last glyph. */
27188 if (end == glyph
27189 && BUFFERP (end->object)
27190 && (end->charpos < start_charpos
27191 || end->charpos >= end_charpos))
27192 {
27193 x += end->pixel_width;
27194 ++end;
27195 }
27196 hlinfo->mouse_face_end_x = x;
27197 hlinfo->mouse_face_end_col = end - r2->glyphs[TEXT_AREA];
27198 }
27199
27200 hlinfo->mouse_face_window = window;
27201 hlinfo->mouse_face_face_id
27202 = face_at_buffer_position (w, mouse_charpos, 0, 0, &ignore,
27203 mouse_charpos + 1,
27204 !hlinfo->mouse_face_hidden, -1);
27205 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27206 }
27207
27208 /* The following function is not used anymore (replaced with
27209 mouse_face_from_string_pos), but I leave it here for the time
27210 being, in case someone would. */
27211
27212 #if 0 /* not used */
27213
27214 /* Find the position of the glyph for position POS in OBJECT in
27215 window W's current matrix, and return in *X, *Y the pixel
27216 coordinates, and return in *HPOS, *VPOS the column/row of the glyph.
27217
27218 RIGHT_P non-zero means return the position of the right edge of the
27219 glyph, RIGHT_P zero means return the left edge position.
27220
27221 If no glyph for POS exists in the matrix, return the position of
27222 the glyph with the next smaller position that is in the matrix, if
27223 RIGHT_P is zero. If RIGHT_P is non-zero, and no glyph for POS
27224 exists in the matrix, return the position of the glyph with the
27225 next larger position in OBJECT.
27226
27227 Value is non-zero if a glyph was found. */
27228
27229 static int
27230 fast_find_string_pos (struct window *w, ptrdiff_t pos, Lisp_Object object,
27231 int *hpos, int *vpos, int *x, int *y, int right_p)
27232 {
27233 int yb = window_text_bottom_y (w);
27234 struct glyph_row *r;
27235 struct glyph *best_glyph = NULL;
27236 struct glyph_row *best_row = NULL;
27237 int best_x = 0;
27238
27239 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27240 r->enabled_p && r->y < yb;
27241 ++r)
27242 {
27243 struct glyph *g = r->glyphs[TEXT_AREA];
27244 struct glyph *e = g + r->used[TEXT_AREA];
27245 int gx;
27246
27247 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27248 if (EQ (g->object, object))
27249 {
27250 if (g->charpos == pos)
27251 {
27252 best_glyph = g;
27253 best_x = gx;
27254 best_row = r;
27255 goto found;
27256 }
27257 else if (best_glyph == NULL
27258 || ((eabs (g->charpos - pos)
27259 < eabs (best_glyph->charpos - pos))
27260 && (right_p
27261 ? g->charpos < pos
27262 : g->charpos > pos)))
27263 {
27264 best_glyph = g;
27265 best_x = gx;
27266 best_row = r;
27267 }
27268 }
27269 }
27270
27271 found:
27272
27273 if (best_glyph)
27274 {
27275 *x = best_x;
27276 *hpos = best_glyph - best_row->glyphs[TEXT_AREA];
27277
27278 if (right_p)
27279 {
27280 *x += best_glyph->pixel_width;
27281 ++*hpos;
27282 }
27283
27284 *y = best_row->y;
27285 *vpos = best_row - w->current_matrix->rows;
27286 }
27287
27288 return best_glyph != NULL;
27289 }
27290 #endif /* not used */
27291
27292 /* Find the positions of the first and the last glyphs in window W's
27293 current matrix that occlude positions [STARTPOS..ENDPOS] in OBJECT
27294 (assumed to be a string), and return in HLINFO's mouse_face_*
27295 members the pixel and column/row coordinates of those glyphs. */
27296
27297 static void
27298 mouse_face_from_string_pos (struct window *w, Mouse_HLInfo *hlinfo,
27299 Lisp_Object object,
27300 ptrdiff_t startpos, ptrdiff_t endpos)
27301 {
27302 int yb = window_text_bottom_y (w);
27303 struct glyph_row *r;
27304 struct glyph *g, *e;
27305 int gx;
27306 int found = 0;
27307
27308 /* Find the glyph row with at least one position in the range
27309 [STARTPOS..ENDPOS], and the first glyph in that row whose
27310 position belongs to that range. */
27311 for (r = MATRIX_FIRST_TEXT_ROW (w->current_matrix);
27312 r->enabled_p && r->y < yb;
27313 ++r)
27314 {
27315 if (!r->reversed_p)
27316 {
27317 g = r->glyphs[TEXT_AREA];
27318 e = g + r->used[TEXT_AREA];
27319 for (gx = r->x; g < e; gx += g->pixel_width, ++g)
27320 if (EQ (g->object, object)
27321 && startpos <= g->charpos && g->charpos <= endpos)
27322 {
27323 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27324 hlinfo->mouse_face_beg_y = r->y;
27325 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27326 hlinfo->mouse_face_beg_x = gx;
27327 found = 1;
27328 break;
27329 }
27330 }
27331 else
27332 {
27333 struct glyph *g1;
27334
27335 e = r->glyphs[TEXT_AREA];
27336 g = e + r->used[TEXT_AREA];
27337 for ( ; g > e; --g)
27338 if (EQ ((g-1)->object, object)
27339 && startpos <= (g-1)->charpos && (g-1)->charpos <= endpos)
27340 {
27341 hlinfo->mouse_face_beg_row = r - w->current_matrix->rows;
27342 hlinfo->mouse_face_beg_y = r->y;
27343 hlinfo->mouse_face_beg_col = g - r->glyphs[TEXT_AREA];
27344 for (gx = r->x, g1 = r->glyphs[TEXT_AREA]; g1 < g; ++g1)
27345 gx += g1->pixel_width;
27346 hlinfo->mouse_face_beg_x = gx;
27347 found = 1;
27348 break;
27349 }
27350 }
27351 if (found)
27352 break;
27353 }
27354
27355 if (!found)
27356 return;
27357
27358 /* Starting with the next row, look for the first row which does NOT
27359 include any glyphs whose positions are in the range. */
27360 for (++r; r->enabled_p && r->y < yb; ++r)
27361 {
27362 g = r->glyphs[TEXT_AREA];
27363 e = g + r->used[TEXT_AREA];
27364 found = 0;
27365 for ( ; g < e; ++g)
27366 if (EQ (g->object, object)
27367 && startpos <= g->charpos && g->charpos <= endpos)
27368 {
27369 found = 1;
27370 break;
27371 }
27372 if (!found)
27373 break;
27374 }
27375
27376 /* The highlighted region ends on the previous row. */
27377 r--;
27378
27379 /* Set the end row and its vertical pixel coordinate. */
27380 hlinfo->mouse_face_end_row = r - w->current_matrix->rows;
27381 hlinfo->mouse_face_end_y = r->y;
27382
27383 /* Compute and set the end column and the end column's horizontal
27384 pixel coordinate. */
27385 if (!r->reversed_p)
27386 {
27387 g = r->glyphs[TEXT_AREA];
27388 e = g + r->used[TEXT_AREA];
27389 for ( ; e > g; --e)
27390 if (EQ ((e-1)->object, object)
27391 && startpos <= (e-1)->charpos && (e-1)->charpos <= endpos)
27392 break;
27393 hlinfo->mouse_face_end_col = e - g;
27394
27395 for (gx = r->x; g < e; ++g)
27396 gx += g->pixel_width;
27397 hlinfo->mouse_face_end_x = gx;
27398 }
27399 else
27400 {
27401 e = r->glyphs[TEXT_AREA];
27402 g = e + r->used[TEXT_AREA];
27403 for (gx = r->x ; e < g; ++e)
27404 {
27405 if (EQ (e->object, object)
27406 && startpos <= e->charpos && e->charpos <= endpos)
27407 break;
27408 gx += e->pixel_width;
27409 }
27410 hlinfo->mouse_face_end_col = e - r->glyphs[TEXT_AREA];
27411 hlinfo->mouse_face_end_x = gx;
27412 }
27413 }
27414
27415 #ifdef HAVE_WINDOW_SYSTEM
27416
27417 /* See if position X, Y is within a hot-spot of an image. */
27418
27419 static int
27420 on_hot_spot_p (Lisp_Object hot_spot, int x, int y)
27421 {
27422 if (!CONSP (hot_spot))
27423 return 0;
27424
27425 if (EQ (XCAR (hot_spot), Qrect))
27426 {
27427 /* CDR is (Top-Left . Bottom-Right) = ((x0 . y0) . (x1 . y1)) */
27428 Lisp_Object rect = XCDR (hot_spot);
27429 Lisp_Object tem;
27430 if (!CONSP (rect))
27431 return 0;
27432 if (!CONSP (XCAR (rect)))
27433 return 0;
27434 if (!CONSP (XCDR (rect)))
27435 return 0;
27436 if (!(tem = XCAR (XCAR (rect)), INTEGERP (tem) && x >= XINT (tem)))
27437 return 0;
27438 if (!(tem = XCDR (XCAR (rect)), INTEGERP (tem) && y >= XINT (tem)))
27439 return 0;
27440 if (!(tem = XCAR (XCDR (rect)), INTEGERP (tem) && x <= XINT (tem)))
27441 return 0;
27442 if (!(tem = XCDR (XCDR (rect)), INTEGERP (tem) && y <= XINT (tem)))
27443 return 0;
27444 return 1;
27445 }
27446 else if (EQ (XCAR (hot_spot), Qcircle))
27447 {
27448 /* CDR is (Center . Radius) = ((x0 . y0) . r) */
27449 Lisp_Object circ = XCDR (hot_spot);
27450 Lisp_Object lr, lx0, ly0;
27451 if (CONSP (circ)
27452 && CONSP (XCAR (circ))
27453 && (lr = XCDR (circ), INTEGERP (lr) || FLOATP (lr))
27454 && (lx0 = XCAR (XCAR (circ)), INTEGERP (lx0))
27455 && (ly0 = XCDR (XCAR (circ)), INTEGERP (ly0)))
27456 {
27457 double r = XFLOATINT (lr);
27458 double dx = XINT (lx0) - x;
27459 double dy = XINT (ly0) - y;
27460 return (dx * dx + dy * dy <= r * r);
27461 }
27462 }
27463 else if (EQ (XCAR (hot_spot), Qpoly))
27464 {
27465 /* CDR is [x0 y0 x1 y1 x2 y2 ...x(n-1) y(n-1)] */
27466 if (VECTORP (XCDR (hot_spot)))
27467 {
27468 struct Lisp_Vector *v = XVECTOR (XCDR (hot_spot));
27469 Lisp_Object *poly = v->contents;
27470 ptrdiff_t n = v->header.size;
27471 ptrdiff_t i;
27472 int inside = 0;
27473 Lisp_Object lx, ly;
27474 int x0, y0;
27475
27476 /* Need an even number of coordinates, and at least 3 edges. */
27477 if (n < 6 || n & 1)
27478 return 0;
27479
27480 /* Count edge segments intersecting line from (X,Y) to (X,infinity).
27481 If count is odd, we are inside polygon. Pixels on edges
27482 may or may not be included depending on actual geometry of the
27483 polygon. */
27484 if ((lx = poly[n-2], !INTEGERP (lx))
27485 || (ly = poly[n-1], !INTEGERP (lx)))
27486 return 0;
27487 x0 = XINT (lx), y0 = XINT (ly);
27488 for (i = 0; i < n; i += 2)
27489 {
27490 int x1 = x0, y1 = y0;
27491 if ((lx = poly[i], !INTEGERP (lx))
27492 || (ly = poly[i+1], !INTEGERP (ly)))
27493 return 0;
27494 x0 = XINT (lx), y0 = XINT (ly);
27495
27496 /* Does this segment cross the X line? */
27497 if (x0 >= x)
27498 {
27499 if (x1 >= x)
27500 continue;
27501 }
27502 else if (x1 < x)
27503 continue;
27504 if (y > y0 && y > y1)
27505 continue;
27506 if (y < y0 + ((y1 - y0) * (x - x0)) / (x1 - x0))
27507 inside = !inside;
27508 }
27509 return inside;
27510 }
27511 }
27512 return 0;
27513 }
27514
27515 Lisp_Object
27516 find_hot_spot (Lisp_Object map, int x, int y)
27517 {
27518 while (CONSP (map))
27519 {
27520 if (CONSP (XCAR (map))
27521 && on_hot_spot_p (XCAR (XCAR (map)), x, y))
27522 return XCAR (map);
27523 map = XCDR (map);
27524 }
27525
27526 return Qnil;
27527 }
27528
27529 DEFUN ("lookup-image-map", Flookup_image_map, Slookup_image_map,
27530 3, 3, 0,
27531 doc: /* Lookup in image map MAP coordinates X and Y.
27532 An image map is an alist where each element has the format (AREA ID PLIST).
27533 An AREA is specified as either a rectangle, a circle, or a polygon:
27534 A rectangle is a cons (rect . ((x0 . y0) . (x1 . y1))) specifying the
27535 pixel coordinates of the upper left and bottom right corners.
27536 A circle is a cons (circle . ((x0 . y0) . r)) specifying the center
27537 and the radius of the circle; r may be a float or integer.
27538 A polygon is a cons (poly . [x0 y0 x1 y1 ...]) where each pair in the
27539 vector describes one corner in the polygon.
27540 Returns the alist element for the first matching AREA in MAP. */)
27541 (Lisp_Object map, Lisp_Object x, Lisp_Object y)
27542 {
27543 if (NILP (map))
27544 return Qnil;
27545
27546 CHECK_NUMBER (x);
27547 CHECK_NUMBER (y);
27548
27549 return find_hot_spot (map,
27550 clip_to_bounds (INT_MIN, XINT (x), INT_MAX),
27551 clip_to_bounds (INT_MIN, XINT (y), INT_MAX));
27552 }
27553
27554
27555 /* Display frame CURSOR, optionally using shape defined by POINTER. */
27556 static void
27557 define_frame_cursor1 (struct frame *f, Cursor cursor, Lisp_Object pointer)
27558 {
27559 /* Do not change cursor shape while dragging mouse. */
27560 if (!NILP (do_mouse_tracking))
27561 return;
27562
27563 if (!NILP (pointer))
27564 {
27565 if (EQ (pointer, Qarrow))
27566 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27567 else if (EQ (pointer, Qhand))
27568 cursor = FRAME_X_OUTPUT (f)->hand_cursor;
27569 else if (EQ (pointer, Qtext))
27570 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27571 else if (EQ (pointer, intern ("hdrag")))
27572 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27573 #ifdef HAVE_X_WINDOWS
27574 else if (EQ (pointer, intern ("vdrag")))
27575 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27576 #endif
27577 else if (EQ (pointer, intern ("hourglass")))
27578 cursor = FRAME_X_OUTPUT (f)->hourglass_cursor;
27579 else if (EQ (pointer, Qmodeline))
27580 cursor = FRAME_X_OUTPUT (f)->modeline_cursor;
27581 else
27582 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27583 }
27584
27585 if (cursor != No_Cursor)
27586 FRAME_RIF (f)->define_frame_cursor (f, cursor);
27587 }
27588
27589 #endif /* HAVE_WINDOW_SYSTEM */
27590
27591 /* Take proper action when mouse has moved to the mode or header line
27592 or marginal area AREA of window W, x-position X and y-position Y.
27593 X is relative to the start of the text display area of W, so the
27594 width of bitmap areas and scroll bars must be subtracted to get a
27595 position relative to the start of the mode line. */
27596
27597 static void
27598 note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
27599 enum window_part area)
27600 {
27601 struct window *w = XWINDOW (window);
27602 struct frame *f = XFRAME (w->frame);
27603 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27604 #ifdef HAVE_WINDOW_SYSTEM
27605 Display_Info *dpyinfo;
27606 #endif
27607 Cursor cursor = No_Cursor;
27608 Lisp_Object pointer = Qnil;
27609 int dx, dy, width, height;
27610 ptrdiff_t charpos;
27611 Lisp_Object string, object = Qnil;
27612 Lisp_Object pos IF_LINT (= Qnil), help;
27613
27614 Lisp_Object mouse_face;
27615 int original_x_pixel = x;
27616 struct glyph * glyph = NULL, * row_start_glyph = NULL;
27617 struct glyph_row *row IF_LINT (= 0);
27618
27619 if (area == ON_MODE_LINE || area == ON_HEADER_LINE)
27620 {
27621 int x0;
27622 struct glyph *end;
27623
27624 /* Kludge alert: mode_line_string takes X/Y in pixels, but
27625 returns them in row/column units! */
27626 string = mode_line_string (w, area, &x, &y, &charpos,
27627 &object, &dx, &dy, &width, &height);
27628
27629 row = (area == ON_MODE_LINE
27630 ? MATRIX_MODE_LINE_ROW (w->current_matrix)
27631 : MATRIX_HEADER_LINE_ROW (w->current_matrix));
27632
27633 /* Find the glyph under the mouse pointer. */
27634 if (row->mode_line_p && row->enabled_p)
27635 {
27636 glyph = row_start_glyph = row->glyphs[TEXT_AREA];
27637 end = glyph + row->used[TEXT_AREA];
27638
27639 for (x0 = original_x_pixel;
27640 glyph < end && x0 >= glyph->pixel_width;
27641 ++glyph)
27642 x0 -= glyph->pixel_width;
27643
27644 if (glyph >= end)
27645 glyph = NULL;
27646 }
27647 }
27648 else
27649 {
27650 x -= WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w);
27651 /* Kludge alert: marginal_area_string takes X/Y in pixels, but
27652 returns them in row/column units! */
27653 string = marginal_area_string (w, area, &x, &y, &charpos,
27654 &object, &dx, &dy, &width, &height);
27655 }
27656
27657 help = Qnil;
27658
27659 #ifdef HAVE_WINDOW_SYSTEM
27660 if (IMAGEP (object))
27661 {
27662 Lisp_Object image_map, hotspot;
27663 if ((image_map = Fplist_get (XCDR (object), QCmap),
27664 !NILP (image_map))
27665 && (hotspot = find_hot_spot (image_map, dx, dy),
27666 CONSP (hotspot))
27667 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
27668 {
27669 Lisp_Object plist;
27670
27671 /* Could check XCAR (hotspot) to see if we enter/leave this hot-spot.
27672 If so, we could look for mouse-enter, mouse-leave
27673 properties in PLIST (and do something...). */
27674 hotspot = XCDR (hotspot);
27675 if (CONSP (hotspot)
27676 && (plist = XCAR (hotspot), CONSP (plist)))
27677 {
27678 pointer = Fplist_get (plist, Qpointer);
27679 if (NILP (pointer))
27680 pointer = Qhand;
27681 help = Fplist_get (plist, Qhelp_echo);
27682 if (!NILP (help))
27683 {
27684 help_echo_string = help;
27685 XSETWINDOW (help_echo_window, w);
27686 help_echo_object = w->buffer;
27687 help_echo_pos = charpos;
27688 }
27689 }
27690 }
27691 if (NILP (pointer))
27692 pointer = Fplist_get (XCDR (object), QCpointer);
27693 }
27694 #endif /* HAVE_WINDOW_SYSTEM */
27695
27696 if (STRINGP (string))
27697 pos = make_number (charpos);
27698
27699 /* Set the help text and mouse pointer. If the mouse is on a part
27700 of the mode line without any text (e.g. past the right edge of
27701 the mode line text), use the default help text and pointer. */
27702 if (STRINGP (string) || area == ON_MODE_LINE)
27703 {
27704 /* Arrange to display the help by setting the global variables
27705 help_echo_string, help_echo_object, and help_echo_pos. */
27706 if (NILP (help))
27707 {
27708 if (STRINGP (string))
27709 help = Fget_text_property (pos, Qhelp_echo, string);
27710
27711 if (!NILP (help))
27712 {
27713 help_echo_string = help;
27714 XSETWINDOW (help_echo_window, w);
27715 help_echo_object = string;
27716 help_echo_pos = charpos;
27717 }
27718 else if (area == ON_MODE_LINE)
27719 {
27720 Lisp_Object default_help
27721 = buffer_local_value_1 (Qmode_line_default_help_echo,
27722 w->buffer);
27723
27724 if (STRINGP (default_help))
27725 {
27726 help_echo_string = default_help;
27727 XSETWINDOW (help_echo_window, w);
27728 help_echo_object = Qnil;
27729 help_echo_pos = -1;
27730 }
27731 }
27732 }
27733
27734 #ifdef HAVE_WINDOW_SYSTEM
27735 /* Change the mouse pointer according to what is under it. */
27736 if (FRAME_WINDOW_P (f))
27737 {
27738 dpyinfo = FRAME_X_DISPLAY_INFO (f);
27739 if (STRINGP (string))
27740 {
27741 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27742
27743 if (NILP (pointer))
27744 pointer = Fget_text_property (pos, Qpointer, string);
27745
27746 /* Change the mouse pointer according to what is under X/Y. */
27747 if (NILP (pointer)
27748 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE)))
27749 {
27750 Lisp_Object map;
27751 map = Fget_text_property (pos, Qlocal_map, string);
27752 if (!KEYMAPP (map))
27753 map = Fget_text_property (pos, Qkeymap, string);
27754 if (!KEYMAPP (map))
27755 cursor = dpyinfo->vertical_scroll_bar_cursor;
27756 }
27757 }
27758 else
27759 /* Default mode-line pointer. */
27760 cursor = FRAME_X_DISPLAY_INFO (f)->vertical_scroll_bar_cursor;
27761 }
27762 #endif
27763 }
27764
27765 /* Change the mouse face according to what is under X/Y. */
27766 if (STRINGP (string))
27767 {
27768 mouse_face = Fget_text_property (pos, Qmouse_face, string);
27769 if (!NILP (mouse_face)
27770 && ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27771 && glyph)
27772 {
27773 Lisp_Object b, e;
27774
27775 struct glyph * tmp_glyph;
27776
27777 int gpos;
27778 int gseq_length;
27779 int total_pixel_width;
27780 ptrdiff_t begpos, endpos, ignore;
27781
27782 int vpos, hpos;
27783
27784 b = Fprevious_single_property_change (make_number (charpos + 1),
27785 Qmouse_face, string, Qnil);
27786 if (NILP (b))
27787 begpos = 0;
27788 else
27789 begpos = XINT (b);
27790
27791 e = Fnext_single_property_change (pos, Qmouse_face, string, Qnil);
27792 if (NILP (e))
27793 endpos = SCHARS (string);
27794 else
27795 endpos = XINT (e);
27796
27797 /* Calculate the glyph position GPOS of GLYPH in the
27798 displayed string, relative to the beginning of the
27799 highlighted part of the string.
27800
27801 Note: GPOS is different from CHARPOS. CHARPOS is the
27802 position of GLYPH in the internal string object. A mode
27803 line string format has structures which are converted to
27804 a flattened string by the Emacs Lisp interpreter. The
27805 internal string is an element of those structures. The
27806 displayed string is the flattened string. */
27807 tmp_glyph = row_start_glyph;
27808 while (tmp_glyph < glyph
27809 && (!(EQ (tmp_glyph->object, glyph->object)
27810 && begpos <= tmp_glyph->charpos
27811 && tmp_glyph->charpos < endpos)))
27812 tmp_glyph++;
27813 gpos = glyph - tmp_glyph;
27814
27815 /* Calculate the length GSEQ_LENGTH of the glyph sequence of
27816 the highlighted part of the displayed string to which
27817 GLYPH belongs. Note: GSEQ_LENGTH is different from
27818 SCHARS (STRING), because the latter returns the length of
27819 the internal string. */
27820 for (tmp_glyph = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
27821 tmp_glyph > glyph
27822 && (!(EQ (tmp_glyph->object, glyph->object)
27823 && begpos <= tmp_glyph->charpos
27824 && tmp_glyph->charpos < endpos));
27825 tmp_glyph--)
27826 ;
27827 gseq_length = gpos + (tmp_glyph - glyph) + 1;
27828
27829 /* Calculate the total pixel width of all the glyphs between
27830 the beginning of the highlighted area and GLYPH. */
27831 total_pixel_width = 0;
27832 for (tmp_glyph = glyph - gpos; tmp_glyph != glyph; tmp_glyph++)
27833 total_pixel_width += tmp_glyph->pixel_width;
27834
27835 /* Pre calculation of re-rendering position. Note: X is in
27836 column units here, after the call to mode_line_string or
27837 marginal_area_string. */
27838 hpos = x - gpos;
27839 vpos = (area == ON_MODE_LINE
27840 ? (w->current_matrix)->nrows - 1
27841 : 0);
27842
27843 /* If GLYPH's position is included in the region that is
27844 already drawn in mouse face, we have nothing to do. */
27845 if ( EQ (window, hlinfo->mouse_face_window)
27846 && (!row->reversed_p
27847 ? (hlinfo->mouse_face_beg_col <= hpos
27848 && hpos < hlinfo->mouse_face_end_col)
27849 /* In R2L rows we swap BEG and END, see below. */
27850 : (hlinfo->mouse_face_end_col <= hpos
27851 && hpos < hlinfo->mouse_face_beg_col))
27852 && hlinfo->mouse_face_beg_row == vpos )
27853 return;
27854
27855 if (clear_mouse_face (hlinfo))
27856 cursor = No_Cursor;
27857
27858 if (!row->reversed_p)
27859 {
27860 hlinfo->mouse_face_beg_col = hpos;
27861 hlinfo->mouse_face_beg_x = original_x_pixel
27862 - (total_pixel_width + dx);
27863 hlinfo->mouse_face_end_col = hpos + gseq_length;
27864 hlinfo->mouse_face_end_x = 0;
27865 }
27866 else
27867 {
27868 /* In R2L rows, show_mouse_face expects BEG and END
27869 coordinates to be swapped. */
27870 hlinfo->mouse_face_end_col = hpos;
27871 hlinfo->mouse_face_end_x = original_x_pixel
27872 - (total_pixel_width + dx);
27873 hlinfo->mouse_face_beg_col = hpos + gseq_length;
27874 hlinfo->mouse_face_beg_x = 0;
27875 }
27876
27877 hlinfo->mouse_face_beg_row = vpos;
27878 hlinfo->mouse_face_end_row = hlinfo->mouse_face_beg_row;
27879 hlinfo->mouse_face_beg_y = 0;
27880 hlinfo->mouse_face_end_y = 0;
27881 hlinfo->mouse_face_past_end = 0;
27882 hlinfo->mouse_face_window = window;
27883
27884 hlinfo->mouse_face_face_id = face_at_string_position (w, string,
27885 charpos,
27886 0, 0, 0,
27887 &ignore,
27888 glyph->face_id,
27889 1);
27890 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
27891
27892 if (NILP (pointer))
27893 pointer = Qhand;
27894 }
27895 else if ((area == ON_MODE_LINE) || (area == ON_HEADER_LINE))
27896 clear_mouse_face (hlinfo);
27897 }
27898 #ifdef HAVE_WINDOW_SYSTEM
27899 if (FRAME_WINDOW_P (f))
27900 define_frame_cursor1 (f, cursor, pointer);
27901 #endif
27902 }
27903
27904
27905 /* EXPORT:
27906 Take proper action when the mouse has moved to position X, Y on
27907 frame F as regards highlighting characters that have mouse-face
27908 properties. Also de-highlighting chars where the mouse was before.
27909 X and Y can be negative or out of range. */
27910
27911 void
27912 note_mouse_highlight (struct frame *f, int x, int y)
27913 {
27914 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
27915 enum window_part part = ON_NOTHING;
27916 Lisp_Object window;
27917 struct window *w;
27918 Cursor cursor = No_Cursor;
27919 Lisp_Object pointer = Qnil; /* Takes precedence over cursor! */
27920 struct buffer *b;
27921
27922 /* When a menu is active, don't highlight because this looks odd. */
27923 #if defined (USE_X_TOOLKIT) || defined (USE_GTK) || defined (HAVE_NS) || defined (MSDOS)
27924 if (popup_activated ())
27925 return;
27926 #endif
27927
27928 if (NILP (Vmouse_highlight)
27929 || !f->glyphs_initialized_p
27930 || f->pointer_invisible)
27931 return;
27932
27933 hlinfo->mouse_face_mouse_x = x;
27934 hlinfo->mouse_face_mouse_y = y;
27935 hlinfo->mouse_face_mouse_frame = f;
27936
27937 if (hlinfo->mouse_face_defer)
27938 return;
27939
27940 /* Which window is that in? */
27941 window = window_from_coordinates (f, x, y, &part, 1);
27942
27943 /* If displaying active text in another window, clear that. */
27944 if (! EQ (window, hlinfo->mouse_face_window)
27945 /* Also clear if we move out of text area in same window. */
27946 || (!NILP (hlinfo->mouse_face_window)
27947 && !NILP (window)
27948 && part != ON_TEXT
27949 && part != ON_MODE_LINE
27950 && part != ON_HEADER_LINE))
27951 clear_mouse_face (hlinfo);
27952
27953 /* Not on a window -> return. */
27954 if (!WINDOWP (window))
27955 return;
27956
27957 /* Reset help_echo_string. It will get recomputed below. */
27958 help_echo_string = Qnil;
27959
27960 /* Convert to window-relative pixel coordinates. */
27961 w = XWINDOW (window);
27962 frame_to_window_pixel_xy (w, &x, &y);
27963
27964 #ifdef HAVE_WINDOW_SYSTEM
27965 /* Handle tool-bar window differently since it doesn't display a
27966 buffer. */
27967 if (EQ (window, f->tool_bar_window))
27968 {
27969 note_tool_bar_highlight (f, x, y);
27970 return;
27971 }
27972 #endif
27973
27974 /* Mouse is on the mode, header line or margin? */
27975 if (part == ON_MODE_LINE || part == ON_HEADER_LINE
27976 || part == ON_LEFT_MARGIN || part == ON_RIGHT_MARGIN)
27977 {
27978 note_mode_line_or_margin_highlight (window, x, y, part);
27979 return;
27980 }
27981
27982 #ifdef HAVE_WINDOW_SYSTEM
27983 if (part == ON_VERTICAL_BORDER)
27984 {
27985 cursor = FRAME_X_OUTPUT (f)->horizontal_drag_cursor;
27986 help_echo_string = build_string ("drag-mouse-1: resize");
27987 }
27988 else if (part == ON_LEFT_FRINGE || part == ON_RIGHT_FRINGE
27989 || part == ON_SCROLL_BAR)
27990 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
27991 else
27992 cursor = FRAME_X_OUTPUT (f)->text_cursor;
27993 #endif
27994
27995 /* Are we in a window whose display is up to date?
27996 And verify the buffer's text has not changed. */
27997 b = XBUFFER (w->buffer);
27998 if (part == ON_TEXT
27999 && EQ (w->window_end_valid, w->buffer)
28000 && w->last_modified == BUF_MODIFF (b)
28001 && w->last_overlay_modified == BUF_OVERLAY_MODIFF (b))
28002 {
28003 int hpos, vpos, dx, dy, area = LAST_AREA;
28004 ptrdiff_t pos;
28005 struct glyph *glyph;
28006 Lisp_Object object;
28007 Lisp_Object mouse_face = Qnil, position;
28008 Lisp_Object *overlay_vec = NULL;
28009 ptrdiff_t i, noverlays;
28010 struct buffer *obuf;
28011 ptrdiff_t obegv, ozv;
28012 int same_region;
28013
28014 /* Find the glyph under X/Y. */
28015 glyph = x_y_to_hpos_vpos (w, x, y, &hpos, &vpos, &dx, &dy, &area);
28016
28017 #ifdef HAVE_WINDOW_SYSTEM
28018 /* Look for :pointer property on image. */
28019 if (glyph != NULL && glyph->type == IMAGE_GLYPH)
28020 {
28021 struct image *img = IMAGE_FROM_ID (f, glyph->u.img_id);
28022 if (img != NULL && IMAGEP (img->spec))
28023 {
28024 Lisp_Object image_map, hotspot;
28025 if ((image_map = Fplist_get (XCDR (img->spec), QCmap),
28026 !NILP (image_map))
28027 && (hotspot = find_hot_spot (image_map,
28028 glyph->slice.img.x + dx,
28029 glyph->slice.img.y + dy),
28030 CONSP (hotspot))
28031 && (hotspot = XCDR (hotspot), CONSP (hotspot)))
28032 {
28033 Lisp_Object plist;
28034
28035 /* Could check XCAR (hotspot) to see if we enter/leave
28036 this hot-spot.
28037 If so, we could look for mouse-enter, mouse-leave
28038 properties in PLIST (and do something...). */
28039 hotspot = XCDR (hotspot);
28040 if (CONSP (hotspot)
28041 && (plist = XCAR (hotspot), CONSP (plist)))
28042 {
28043 pointer = Fplist_get (plist, Qpointer);
28044 if (NILP (pointer))
28045 pointer = Qhand;
28046 help_echo_string = Fplist_get (plist, Qhelp_echo);
28047 if (!NILP (help_echo_string))
28048 {
28049 help_echo_window = window;
28050 help_echo_object = glyph->object;
28051 help_echo_pos = glyph->charpos;
28052 }
28053 }
28054 }
28055 if (NILP (pointer))
28056 pointer = Fplist_get (XCDR (img->spec), QCpointer);
28057 }
28058 }
28059 #endif /* HAVE_WINDOW_SYSTEM */
28060
28061 /* Clear mouse face if X/Y not over text. */
28062 if (glyph == NULL
28063 || area != TEXT_AREA
28064 || !MATRIX_ROW (w->current_matrix, vpos)->displays_text_p
28065 /* Glyph's OBJECT is an integer for glyphs inserted by the
28066 display engine for its internal purposes, like truncation
28067 and continuation glyphs and blanks beyond the end of
28068 line's text on text terminals. If we are over such a
28069 glyph, we are not over any text. */
28070 || INTEGERP (glyph->object)
28071 /* R2L rows have a stretch glyph at their front, which
28072 stands for no text, whereas L2R rows have no glyphs at
28073 all beyond the end of text. Treat such stretch glyphs
28074 like we do with NULL glyphs in L2R rows. */
28075 || (MATRIX_ROW (w->current_matrix, vpos)->reversed_p
28076 && glyph == MATRIX_ROW (w->current_matrix, vpos)->glyphs[TEXT_AREA]
28077 && glyph->type == STRETCH_GLYPH
28078 && glyph->avoid_cursor_p))
28079 {
28080 if (clear_mouse_face (hlinfo))
28081 cursor = No_Cursor;
28082 #ifdef HAVE_WINDOW_SYSTEM
28083 if (FRAME_WINDOW_P (f) && NILP (pointer))
28084 {
28085 if (area != TEXT_AREA)
28086 cursor = FRAME_X_OUTPUT (f)->nontext_cursor;
28087 else
28088 pointer = Vvoid_text_area_pointer;
28089 }
28090 #endif
28091 goto set_cursor;
28092 }
28093
28094 pos = glyph->charpos;
28095 object = glyph->object;
28096 if (!STRINGP (object) && !BUFFERP (object))
28097 goto set_cursor;
28098
28099 /* If we get an out-of-range value, return now; avoid an error. */
28100 if (BUFFERP (object) && pos > BUF_Z (b))
28101 goto set_cursor;
28102
28103 /* Make the window's buffer temporarily current for
28104 overlays_at and compute_char_face. */
28105 obuf = current_buffer;
28106 current_buffer = b;
28107 obegv = BEGV;
28108 ozv = ZV;
28109 BEGV = BEG;
28110 ZV = Z;
28111
28112 /* Is this char mouse-active or does it have help-echo? */
28113 position = make_number (pos);
28114
28115 if (BUFFERP (object))
28116 {
28117 /* Put all the overlays we want in a vector in overlay_vec. */
28118 GET_OVERLAYS_AT (pos, overlay_vec, noverlays, NULL, 0);
28119 /* Sort overlays into increasing priority order. */
28120 noverlays = sort_overlays (overlay_vec, noverlays, w);
28121 }
28122 else
28123 noverlays = 0;
28124
28125 same_region = coords_in_mouse_face_p (w, hpos, vpos);
28126
28127 if (same_region)
28128 cursor = No_Cursor;
28129
28130 /* Check mouse-face highlighting. */
28131 if (! same_region
28132 /* If there exists an overlay with mouse-face overlapping
28133 the one we are currently highlighting, we have to
28134 check if we enter the overlapping overlay, and then
28135 highlight only that. */
28136 || (OVERLAYP (hlinfo->mouse_face_overlay)
28137 && mouse_face_overlay_overlaps (hlinfo->mouse_face_overlay)))
28138 {
28139 /* Find the highest priority overlay with a mouse-face. */
28140 Lisp_Object overlay = Qnil;
28141 for (i = noverlays - 1; i >= 0 && NILP (overlay); --i)
28142 {
28143 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
28144 if (!NILP (mouse_face))
28145 overlay = overlay_vec[i];
28146 }
28147
28148 /* If we're highlighting the same overlay as before, there's
28149 no need to do that again. */
28150 if (!NILP (overlay) && EQ (overlay, hlinfo->mouse_face_overlay))
28151 goto check_help_echo;
28152 hlinfo->mouse_face_overlay = overlay;
28153
28154 /* Clear the display of the old active region, if any. */
28155 if (clear_mouse_face (hlinfo))
28156 cursor = No_Cursor;
28157
28158 /* If no overlay applies, get a text property. */
28159 if (NILP (overlay))
28160 mouse_face = Fget_text_property (position, Qmouse_face, object);
28161
28162 /* Next, compute the bounds of the mouse highlighting and
28163 display it. */
28164 if (!NILP (mouse_face) && STRINGP (object))
28165 {
28166 /* The mouse-highlighting comes from a display string
28167 with a mouse-face. */
28168 Lisp_Object s, e;
28169 ptrdiff_t ignore;
28170
28171 s = Fprevious_single_property_change
28172 (make_number (pos + 1), Qmouse_face, object, Qnil);
28173 e = Fnext_single_property_change
28174 (position, Qmouse_face, object, Qnil);
28175 if (NILP (s))
28176 s = make_number (0);
28177 if (NILP (e))
28178 e = make_number (SCHARS (object) - 1);
28179 mouse_face_from_string_pos (w, hlinfo, object,
28180 XINT (s), XINT (e));
28181 hlinfo->mouse_face_past_end = 0;
28182 hlinfo->mouse_face_window = window;
28183 hlinfo->mouse_face_face_id
28184 = face_at_string_position (w, object, pos, 0, 0, 0, &ignore,
28185 glyph->face_id, 1);
28186 show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
28187 cursor = No_Cursor;
28188 }
28189 else
28190 {
28191 /* The mouse-highlighting, if any, comes from an overlay
28192 or text property in the buffer. */
28193 Lisp_Object buffer IF_LINT (= Qnil);
28194 Lisp_Object disp_string IF_LINT (= Qnil);
28195
28196 if (STRINGP (object))
28197 {
28198 /* If we are on a display string with no mouse-face,
28199 check if the text under it has one. */
28200 struct glyph_row *r = MATRIX_ROW (w->current_matrix, vpos);
28201 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28202 pos = string_buffer_position (object, start);
28203 if (pos > 0)
28204 {
28205 mouse_face = get_char_property_and_overlay
28206 (make_number (pos), Qmouse_face, w->buffer, &overlay);
28207 buffer = w->buffer;
28208 disp_string = object;
28209 }
28210 }
28211 else
28212 {
28213 buffer = object;
28214 disp_string = Qnil;
28215 }
28216
28217 if (!NILP (mouse_face))
28218 {
28219 Lisp_Object before, after;
28220 Lisp_Object before_string, after_string;
28221 /* To correctly find the limits of mouse highlight
28222 in a bidi-reordered buffer, we must not use the
28223 optimization of limiting the search in
28224 previous-single-property-change and
28225 next-single-property-change, because
28226 rows_from_pos_range needs the real start and end
28227 positions to DTRT in this case. That's because
28228 the first row visible in a window does not
28229 necessarily display the character whose position
28230 is the smallest. */
28231 Lisp_Object lim1 =
28232 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28233 ? Fmarker_position (w->start)
28234 : Qnil;
28235 Lisp_Object lim2 =
28236 NILP (BVAR (XBUFFER (buffer), bidi_display_reordering))
28237 ? make_number (BUF_Z (XBUFFER (buffer))
28238 - XFASTINT (w->window_end_pos))
28239 : Qnil;
28240
28241 if (NILP (overlay))
28242 {
28243 /* Handle the text property case. */
28244 before = Fprevious_single_property_change
28245 (make_number (pos + 1), Qmouse_face, buffer, lim1);
28246 after = Fnext_single_property_change
28247 (make_number (pos), Qmouse_face, buffer, lim2);
28248 before_string = after_string = Qnil;
28249 }
28250 else
28251 {
28252 /* Handle the overlay case. */
28253 before = Foverlay_start (overlay);
28254 after = Foverlay_end (overlay);
28255 before_string = Foverlay_get (overlay, Qbefore_string);
28256 after_string = Foverlay_get (overlay, Qafter_string);
28257
28258 if (!STRINGP (before_string)) before_string = Qnil;
28259 if (!STRINGP (after_string)) after_string = Qnil;
28260 }
28261
28262 mouse_face_from_buffer_pos (window, hlinfo, pos,
28263 NILP (before)
28264 ? 1
28265 : XFASTINT (before),
28266 NILP (after)
28267 ? BUF_Z (XBUFFER (buffer))
28268 : XFASTINT (after),
28269 before_string, after_string,
28270 disp_string);
28271 cursor = No_Cursor;
28272 }
28273 }
28274 }
28275
28276 check_help_echo:
28277
28278 /* Look for a `help-echo' property. */
28279 if (NILP (help_echo_string)) {
28280 Lisp_Object help, overlay;
28281
28282 /* Check overlays first. */
28283 help = overlay = Qnil;
28284 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
28285 {
28286 overlay = overlay_vec[i];
28287 help = Foverlay_get (overlay, Qhelp_echo);
28288 }
28289
28290 if (!NILP (help))
28291 {
28292 help_echo_string = help;
28293 help_echo_window = window;
28294 help_echo_object = overlay;
28295 help_echo_pos = pos;
28296 }
28297 else
28298 {
28299 Lisp_Object obj = glyph->object;
28300 ptrdiff_t charpos = glyph->charpos;
28301
28302 /* Try text properties. */
28303 if (STRINGP (obj)
28304 && charpos >= 0
28305 && charpos < SCHARS (obj))
28306 {
28307 help = Fget_text_property (make_number (charpos),
28308 Qhelp_echo, obj);
28309 if (NILP (help))
28310 {
28311 /* If the string itself doesn't specify a help-echo,
28312 see if the buffer text ``under'' it does. */
28313 struct glyph_row *r
28314 = MATRIX_ROW (w->current_matrix, vpos);
28315 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28316 ptrdiff_t p = string_buffer_position (obj, start);
28317 if (p > 0)
28318 {
28319 help = Fget_char_property (make_number (p),
28320 Qhelp_echo, w->buffer);
28321 if (!NILP (help))
28322 {
28323 charpos = p;
28324 obj = w->buffer;
28325 }
28326 }
28327 }
28328 }
28329 else if (BUFFERP (obj)
28330 && charpos >= BEGV
28331 && charpos < ZV)
28332 help = Fget_text_property (make_number (charpos), Qhelp_echo,
28333 obj);
28334
28335 if (!NILP (help))
28336 {
28337 help_echo_string = help;
28338 help_echo_window = window;
28339 help_echo_object = obj;
28340 help_echo_pos = charpos;
28341 }
28342 }
28343 }
28344
28345 #ifdef HAVE_WINDOW_SYSTEM
28346 /* Look for a `pointer' property. */
28347 if (FRAME_WINDOW_P (f) && NILP (pointer))
28348 {
28349 /* Check overlays first. */
28350 for (i = noverlays - 1; i >= 0 && NILP (pointer); --i)
28351 pointer = Foverlay_get (overlay_vec[i], Qpointer);
28352
28353 if (NILP (pointer))
28354 {
28355 Lisp_Object obj = glyph->object;
28356 ptrdiff_t charpos = glyph->charpos;
28357
28358 /* Try text properties. */
28359 if (STRINGP (obj)
28360 && charpos >= 0
28361 && charpos < SCHARS (obj))
28362 {
28363 pointer = Fget_text_property (make_number (charpos),
28364 Qpointer, obj);
28365 if (NILP (pointer))
28366 {
28367 /* If the string itself doesn't specify a pointer,
28368 see if the buffer text ``under'' it does. */
28369 struct glyph_row *r
28370 = MATRIX_ROW (w->current_matrix, vpos);
28371 ptrdiff_t start = MATRIX_ROW_START_CHARPOS (r);
28372 ptrdiff_t p = string_buffer_position (obj, start);
28373 if (p > 0)
28374 pointer = Fget_char_property (make_number (p),
28375 Qpointer, w->buffer);
28376 }
28377 }
28378 else if (BUFFERP (obj)
28379 && charpos >= BEGV
28380 && charpos < ZV)
28381 pointer = Fget_text_property (make_number (charpos),
28382 Qpointer, obj);
28383 }
28384 }
28385 #endif /* HAVE_WINDOW_SYSTEM */
28386
28387 BEGV = obegv;
28388 ZV = ozv;
28389 current_buffer = obuf;
28390 }
28391
28392 set_cursor:
28393
28394 #ifdef HAVE_WINDOW_SYSTEM
28395 if (FRAME_WINDOW_P (f))
28396 define_frame_cursor1 (f, cursor, pointer);
28397 #else
28398 /* This is here to prevent a compiler error, about "label at end of
28399 compound statement". */
28400 return;
28401 #endif
28402 }
28403
28404
28405 /* EXPORT for RIF:
28406 Clear any mouse-face on window W. This function is part of the
28407 redisplay interface, and is called from try_window_id and similar
28408 functions to ensure the mouse-highlight is off. */
28409
28410 void
28411 x_clear_window_mouse_face (struct window *w)
28412 {
28413 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (XFRAME (w->frame));
28414 Lisp_Object window;
28415
28416 block_input ();
28417 XSETWINDOW (window, w);
28418 if (EQ (window, hlinfo->mouse_face_window))
28419 clear_mouse_face (hlinfo);
28420 unblock_input ();
28421 }
28422
28423
28424 /* EXPORT:
28425 Just discard the mouse face information for frame F, if any.
28426 This is used when the size of F is changed. */
28427
28428 void
28429 cancel_mouse_face (struct frame *f)
28430 {
28431 Lisp_Object window;
28432 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28433
28434 window = hlinfo->mouse_face_window;
28435 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
28436 {
28437 hlinfo->mouse_face_beg_row = hlinfo->mouse_face_beg_col = -1;
28438 hlinfo->mouse_face_end_row = hlinfo->mouse_face_end_col = -1;
28439 hlinfo->mouse_face_window = Qnil;
28440 }
28441 }
28442
28443
28444 \f
28445 /***********************************************************************
28446 Exposure Events
28447 ***********************************************************************/
28448
28449 #ifdef HAVE_WINDOW_SYSTEM
28450
28451 /* Redraw the part of glyph row area AREA of glyph row ROW on window W
28452 which intersects rectangle R. R is in window-relative coordinates. */
28453
28454 static void
28455 expose_area (struct window *w, struct glyph_row *row, XRectangle *r,
28456 enum glyph_row_area area)
28457 {
28458 struct glyph *first = row->glyphs[area];
28459 struct glyph *end = row->glyphs[area] + row->used[area];
28460 struct glyph *last;
28461 int first_x, start_x, x;
28462
28463 if (area == TEXT_AREA && row->fill_line_p)
28464 /* If row extends face to end of line write the whole line. */
28465 draw_glyphs (w, 0, row, area,
28466 0, row->used[area],
28467 DRAW_NORMAL_TEXT, 0);
28468 else
28469 {
28470 /* Set START_X to the window-relative start position for drawing glyphs of
28471 AREA. The first glyph of the text area can be partially visible.
28472 The first glyphs of other areas cannot. */
28473 start_x = window_box_left_offset (w, area);
28474 x = start_x;
28475 if (area == TEXT_AREA)
28476 x += row->x;
28477
28478 /* Find the first glyph that must be redrawn. */
28479 while (first < end
28480 && x + first->pixel_width < r->x)
28481 {
28482 x += first->pixel_width;
28483 ++first;
28484 }
28485
28486 /* Find the last one. */
28487 last = first;
28488 first_x = x;
28489 while (last < end
28490 && x < r->x + r->width)
28491 {
28492 x += last->pixel_width;
28493 ++last;
28494 }
28495
28496 /* Repaint. */
28497 if (last > first)
28498 draw_glyphs (w, first_x - start_x, row, area,
28499 first - row->glyphs[area], last - row->glyphs[area],
28500 DRAW_NORMAL_TEXT, 0);
28501 }
28502 }
28503
28504
28505 /* Redraw the parts of the glyph row ROW on window W intersecting
28506 rectangle R. R is in window-relative coordinates. Value is
28507 non-zero if mouse-face was overwritten. */
28508
28509 static int
28510 expose_line (struct window *w, struct glyph_row *row, XRectangle *r)
28511 {
28512 eassert (row->enabled_p);
28513
28514 if (row->mode_line_p || w->pseudo_window_p)
28515 draw_glyphs (w, 0, row, TEXT_AREA,
28516 0, row->used[TEXT_AREA],
28517 DRAW_NORMAL_TEXT, 0);
28518 else
28519 {
28520 if (row->used[LEFT_MARGIN_AREA])
28521 expose_area (w, row, r, LEFT_MARGIN_AREA);
28522 if (row->used[TEXT_AREA])
28523 expose_area (w, row, r, TEXT_AREA);
28524 if (row->used[RIGHT_MARGIN_AREA])
28525 expose_area (w, row, r, RIGHT_MARGIN_AREA);
28526 draw_row_fringe_bitmaps (w, row);
28527 }
28528
28529 return row->mouse_face_p;
28530 }
28531
28532
28533 /* Redraw those parts of glyphs rows during expose event handling that
28534 overlap other rows. Redrawing of an exposed line writes over parts
28535 of lines overlapping that exposed line; this function fixes that.
28536
28537 W is the window being exposed. FIRST_OVERLAPPING_ROW is the first
28538 row in W's current matrix that is exposed and overlaps other rows.
28539 LAST_OVERLAPPING_ROW is the last such row. */
28540
28541 static void
28542 expose_overlaps (struct window *w,
28543 struct glyph_row *first_overlapping_row,
28544 struct glyph_row *last_overlapping_row,
28545 XRectangle *r)
28546 {
28547 struct glyph_row *row;
28548
28549 for (row = first_overlapping_row; row <= last_overlapping_row; ++row)
28550 if (row->overlapping_p)
28551 {
28552 eassert (row->enabled_p && !row->mode_line_p);
28553
28554 row->clip = r;
28555 if (row->used[LEFT_MARGIN_AREA])
28556 x_fix_overlapping_area (w, row, LEFT_MARGIN_AREA, OVERLAPS_BOTH);
28557
28558 if (row->used[TEXT_AREA])
28559 x_fix_overlapping_area (w, row, TEXT_AREA, OVERLAPS_BOTH);
28560
28561 if (row->used[RIGHT_MARGIN_AREA])
28562 x_fix_overlapping_area (w, row, RIGHT_MARGIN_AREA, OVERLAPS_BOTH);
28563 row->clip = NULL;
28564 }
28565 }
28566
28567
28568 /* Return non-zero if W's cursor intersects rectangle R. */
28569
28570 static int
28571 phys_cursor_in_rect_p (struct window *w, XRectangle *r)
28572 {
28573 XRectangle cr, result;
28574 struct glyph *cursor_glyph;
28575 struct glyph_row *row;
28576
28577 if (w->phys_cursor.vpos >= 0
28578 && w->phys_cursor.vpos < w->current_matrix->nrows
28579 && (row = MATRIX_ROW (w->current_matrix, w->phys_cursor.vpos),
28580 row->enabled_p)
28581 && row->cursor_in_fringe_p)
28582 {
28583 /* Cursor is in the fringe. */
28584 cr.x = window_box_right_offset (w,
28585 (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
28586 ? RIGHT_MARGIN_AREA
28587 : TEXT_AREA));
28588 cr.y = row->y;
28589 cr.width = WINDOW_RIGHT_FRINGE_WIDTH (w);
28590 cr.height = row->height;
28591 return x_intersect_rectangles (&cr, r, &result);
28592 }
28593
28594 cursor_glyph = get_phys_cursor_glyph (w);
28595 if (cursor_glyph)
28596 {
28597 /* r is relative to W's box, but w->phys_cursor.x is relative
28598 to left edge of W's TEXT area. Adjust it. */
28599 cr.x = window_box_left_offset (w, TEXT_AREA) + w->phys_cursor.x;
28600 cr.y = w->phys_cursor.y;
28601 cr.width = cursor_glyph->pixel_width;
28602 cr.height = w->phys_cursor_height;
28603 /* ++KFS: W32 version used W32-specific IntersectRect here, but
28604 I assume the effect is the same -- and this is portable. */
28605 return x_intersect_rectangles (&cr, r, &result);
28606 }
28607 /* If we don't understand the format, pretend we're not in the hot-spot. */
28608 return 0;
28609 }
28610
28611
28612 /* EXPORT:
28613 Draw a vertical window border to the right of window W if W doesn't
28614 have vertical scroll bars. */
28615
28616 void
28617 x_draw_vertical_border (struct window *w)
28618 {
28619 struct frame *f = XFRAME (WINDOW_FRAME (w));
28620
28621 /* We could do better, if we knew what type of scroll-bar the adjacent
28622 windows (on either side) have... But we don't :-(
28623 However, I think this works ok. ++KFS 2003-04-25 */
28624
28625 /* Redraw borders between horizontally adjacent windows. Don't
28626 do it for frames with vertical scroll bars because either the
28627 right scroll bar of a window, or the left scroll bar of its
28628 neighbor will suffice as a border. */
28629 if (FRAME_HAS_VERTICAL_SCROLL_BARS (XFRAME (w->frame)))
28630 return;
28631
28632 if (!WINDOW_RIGHTMOST_P (w)
28633 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_RIGHT (w))
28634 {
28635 int x0, x1, y0, y1;
28636
28637 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28638 y1 -= 1;
28639
28640 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28641 x1 -= 1;
28642
28643 FRAME_RIF (f)->draw_vertical_window_border (w, x1, y0, y1);
28644 }
28645 else if (!WINDOW_LEFTMOST_P (w)
28646 && !WINDOW_HAS_VERTICAL_SCROLL_BAR_ON_LEFT (w))
28647 {
28648 int x0, x1, y0, y1;
28649
28650 window_box_edges (w, -1, &x0, &y0, &x1, &y1);
28651 y1 -= 1;
28652
28653 if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
28654 x0 -= 1;
28655
28656 FRAME_RIF (f)->draw_vertical_window_border (w, x0, y0, y1);
28657 }
28658 }
28659
28660
28661 /* Redraw the part of window W intersection rectangle FR. Pixel
28662 coordinates in FR are frame-relative. Call this function with
28663 input blocked. Value is non-zero if the exposure overwrites
28664 mouse-face. */
28665
28666 static int
28667 expose_window (struct window *w, XRectangle *fr)
28668 {
28669 struct frame *f = XFRAME (w->frame);
28670 XRectangle wr, r;
28671 int mouse_face_overwritten_p = 0;
28672
28673 /* If window is not yet fully initialized, do nothing. This can
28674 happen when toolkit scroll bars are used and a window is split.
28675 Reconfiguring the scroll bar will generate an expose for a newly
28676 created window. */
28677 if (w->current_matrix == NULL)
28678 return 0;
28679
28680 /* When we're currently updating the window, display and current
28681 matrix usually don't agree. Arrange for a thorough display
28682 later. */
28683 if (w == updated_window)
28684 {
28685 SET_FRAME_GARBAGED (f);
28686 return 0;
28687 }
28688
28689 /* Frame-relative pixel rectangle of W. */
28690 wr.x = WINDOW_LEFT_EDGE_X (w);
28691 wr.y = WINDOW_TOP_EDGE_Y (w);
28692 wr.width = WINDOW_TOTAL_WIDTH (w);
28693 wr.height = WINDOW_TOTAL_HEIGHT (w);
28694
28695 if (x_intersect_rectangles (fr, &wr, &r))
28696 {
28697 int yb = window_text_bottom_y (w);
28698 struct glyph_row *row;
28699 int cursor_cleared_p, phys_cursor_on_p;
28700 struct glyph_row *first_overlapping_row, *last_overlapping_row;
28701
28702 TRACE ((stderr, "expose_window (%d, %d, %d, %d)\n",
28703 r.x, r.y, r.width, r.height));
28704
28705 /* Convert to window coordinates. */
28706 r.x -= WINDOW_LEFT_EDGE_X (w);
28707 r.y -= WINDOW_TOP_EDGE_Y (w);
28708
28709 /* Turn off the cursor. */
28710 if (!w->pseudo_window_p
28711 && phys_cursor_in_rect_p (w, &r))
28712 {
28713 x_clear_cursor (w);
28714 cursor_cleared_p = 1;
28715 }
28716 else
28717 cursor_cleared_p = 0;
28718
28719 /* If the row containing the cursor extends face to end of line,
28720 then expose_area might overwrite the cursor outside the
28721 rectangle and thus notice_overwritten_cursor might clear
28722 w->phys_cursor_on_p. We remember the original value and
28723 check later if it is changed. */
28724 phys_cursor_on_p = w->phys_cursor_on_p;
28725
28726 /* Update lines intersecting rectangle R. */
28727 first_overlapping_row = last_overlapping_row = NULL;
28728 for (row = w->current_matrix->rows;
28729 row->enabled_p;
28730 ++row)
28731 {
28732 int y0 = row->y;
28733 int y1 = MATRIX_ROW_BOTTOM_Y (row);
28734
28735 if ((y0 >= r.y && y0 < r.y + r.height)
28736 || (y1 > r.y && y1 < r.y + r.height)
28737 || (r.y >= y0 && r.y < y1)
28738 || (r.y + r.height > y0 && r.y + r.height < y1))
28739 {
28740 /* A header line may be overlapping, but there is no need
28741 to fix overlapping areas for them. KFS 2005-02-12 */
28742 if (row->overlapping_p && !row->mode_line_p)
28743 {
28744 if (first_overlapping_row == NULL)
28745 first_overlapping_row = row;
28746 last_overlapping_row = row;
28747 }
28748
28749 row->clip = fr;
28750 if (expose_line (w, row, &r))
28751 mouse_face_overwritten_p = 1;
28752 row->clip = NULL;
28753 }
28754 else if (row->overlapping_p)
28755 {
28756 /* We must redraw a row overlapping the exposed area. */
28757 if (y0 < r.y
28758 ? y0 + row->phys_height > r.y
28759 : y0 + row->ascent - row->phys_ascent < r.y +r.height)
28760 {
28761 if (first_overlapping_row == NULL)
28762 first_overlapping_row = row;
28763 last_overlapping_row = row;
28764 }
28765 }
28766
28767 if (y1 >= yb)
28768 break;
28769 }
28770
28771 /* Display the mode line if there is one. */
28772 if (WINDOW_WANTS_MODELINE_P (w)
28773 && (row = MATRIX_MODE_LINE_ROW (w->current_matrix),
28774 row->enabled_p)
28775 && row->y < r.y + r.height)
28776 {
28777 if (expose_line (w, row, &r))
28778 mouse_face_overwritten_p = 1;
28779 }
28780
28781 if (!w->pseudo_window_p)
28782 {
28783 /* Fix the display of overlapping rows. */
28784 if (first_overlapping_row)
28785 expose_overlaps (w, first_overlapping_row, last_overlapping_row,
28786 fr);
28787
28788 /* Draw border between windows. */
28789 x_draw_vertical_border (w);
28790
28791 /* Turn the cursor on again. */
28792 if (cursor_cleared_p
28793 || (phys_cursor_on_p && !w->phys_cursor_on_p))
28794 update_window_cursor (w, 1);
28795 }
28796 }
28797
28798 return mouse_face_overwritten_p;
28799 }
28800
28801
28802
28803 /* Redraw (parts) of all windows in the window tree rooted at W that
28804 intersect R. R contains frame pixel coordinates. Value is
28805 non-zero if the exposure overwrites mouse-face. */
28806
28807 static int
28808 expose_window_tree (struct window *w, XRectangle *r)
28809 {
28810 struct frame *f = XFRAME (w->frame);
28811 int mouse_face_overwritten_p = 0;
28812
28813 while (w && !FRAME_GARBAGED_P (f))
28814 {
28815 if (!NILP (w->hchild))
28816 mouse_face_overwritten_p
28817 |= expose_window_tree (XWINDOW (w->hchild), r);
28818 else if (!NILP (w->vchild))
28819 mouse_face_overwritten_p
28820 |= expose_window_tree (XWINDOW (w->vchild), r);
28821 else
28822 mouse_face_overwritten_p |= expose_window (w, r);
28823
28824 w = NILP (w->next) ? NULL : XWINDOW (w->next);
28825 }
28826
28827 return mouse_face_overwritten_p;
28828 }
28829
28830
28831 /* EXPORT:
28832 Redisplay an exposed area of frame F. X and Y are the upper-left
28833 corner of the exposed rectangle. W and H are width and height of
28834 the exposed area. All are pixel values. W or H zero means redraw
28835 the entire frame. */
28836
28837 void
28838 expose_frame (struct frame *f, int x, int y, int w, int h)
28839 {
28840 XRectangle r;
28841 int mouse_face_overwritten_p = 0;
28842
28843 TRACE ((stderr, "expose_frame "));
28844
28845 /* No need to redraw if frame will be redrawn soon. */
28846 if (FRAME_GARBAGED_P (f))
28847 {
28848 TRACE ((stderr, " garbaged\n"));
28849 return;
28850 }
28851
28852 /* If basic faces haven't been realized yet, there is no point in
28853 trying to redraw anything. This can happen when we get an expose
28854 event while Emacs is starting, e.g. by moving another window. */
28855 if (FRAME_FACE_CACHE (f) == NULL
28856 || FRAME_FACE_CACHE (f)->used < BASIC_FACE_ID_SENTINEL)
28857 {
28858 TRACE ((stderr, " no faces\n"));
28859 return;
28860 }
28861
28862 if (w == 0 || h == 0)
28863 {
28864 r.x = r.y = 0;
28865 r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f);
28866 r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f);
28867 }
28868 else
28869 {
28870 r.x = x;
28871 r.y = y;
28872 r.width = w;
28873 r.height = h;
28874 }
28875
28876 TRACE ((stderr, "(%d, %d, %d, %d)\n", r.x, r.y, r.width, r.height));
28877 mouse_face_overwritten_p = expose_window_tree (XWINDOW (f->root_window), &r);
28878
28879 if (WINDOWP (f->tool_bar_window))
28880 mouse_face_overwritten_p
28881 |= expose_window (XWINDOW (f->tool_bar_window), &r);
28882
28883 #ifdef HAVE_X_WINDOWS
28884 #ifndef MSDOS
28885 #ifndef USE_X_TOOLKIT
28886 if (WINDOWP (f->menu_bar_window))
28887 mouse_face_overwritten_p
28888 |= expose_window (XWINDOW (f->menu_bar_window), &r);
28889 #endif /* not USE_X_TOOLKIT */
28890 #endif
28891 #endif
28892
28893 /* Some window managers support a focus-follows-mouse style with
28894 delayed raising of frames. Imagine a partially obscured frame,
28895 and moving the mouse into partially obscured mouse-face on that
28896 frame. The visible part of the mouse-face will be highlighted,
28897 then the WM raises the obscured frame. With at least one WM, KDE
28898 2.1, Emacs is not getting any event for the raising of the frame
28899 (even tried with SubstructureRedirectMask), only Expose events.
28900 These expose events will draw text normally, i.e. not
28901 highlighted. Which means we must redo the highlight here.
28902 Subsume it under ``we love X''. --gerd 2001-08-15 */
28903 /* Included in Windows version because Windows most likely does not
28904 do the right thing if any third party tool offers
28905 focus-follows-mouse with delayed raise. --jason 2001-10-12 */
28906 if (mouse_face_overwritten_p && !FRAME_GARBAGED_P (f))
28907 {
28908 Mouse_HLInfo *hlinfo = MOUSE_HL_INFO (f);
28909 if (f == hlinfo->mouse_face_mouse_frame)
28910 {
28911 int mouse_x = hlinfo->mouse_face_mouse_x;
28912 int mouse_y = hlinfo->mouse_face_mouse_y;
28913 clear_mouse_face (hlinfo);
28914 note_mouse_highlight (f, mouse_x, mouse_y);
28915 }
28916 }
28917 }
28918
28919
28920 /* EXPORT:
28921 Determine the intersection of two rectangles R1 and R2. Return
28922 the intersection in *RESULT. Value is non-zero if RESULT is not
28923 empty. */
28924
28925 int
28926 x_intersect_rectangles (XRectangle *r1, XRectangle *r2, XRectangle *result)
28927 {
28928 XRectangle *left, *right;
28929 XRectangle *upper, *lower;
28930 int intersection_p = 0;
28931
28932 /* Rearrange so that R1 is the left-most rectangle. */
28933 if (r1->x < r2->x)
28934 left = r1, right = r2;
28935 else
28936 left = r2, right = r1;
28937
28938 /* X0 of the intersection is right.x0, if this is inside R1,
28939 otherwise there is no intersection. */
28940 if (right->x <= left->x + left->width)
28941 {
28942 result->x = right->x;
28943
28944 /* The right end of the intersection is the minimum of
28945 the right ends of left and right. */
28946 result->width = (min (left->x + left->width, right->x + right->width)
28947 - result->x);
28948
28949 /* Same game for Y. */
28950 if (r1->y < r2->y)
28951 upper = r1, lower = r2;
28952 else
28953 upper = r2, lower = r1;
28954
28955 /* The upper end of the intersection is lower.y0, if this is inside
28956 of upper. Otherwise, there is no intersection. */
28957 if (lower->y <= upper->y + upper->height)
28958 {
28959 result->y = lower->y;
28960
28961 /* The lower end of the intersection is the minimum of the lower
28962 ends of upper and lower. */
28963 result->height = (min (lower->y + lower->height,
28964 upper->y + upper->height)
28965 - result->y);
28966 intersection_p = 1;
28967 }
28968 }
28969
28970 return intersection_p;
28971 }
28972
28973 #endif /* HAVE_WINDOW_SYSTEM */
28974
28975 \f
28976 /***********************************************************************
28977 Initialization
28978 ***********************************************************************/
28979
28980 void
28981 syms_of_xdisp (void)
28982 {
28983 Vwith_echo_area_save_vector = Qnil;
28984 staticpro (&Vwith_echo_area_save_vector);
28985
28986 Vmessage_stack = Qnil;
28987 staticpro (&Vmessage_stack);
28988
28989 DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
28990 DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
28991
28992 message_dolog_marker1 = Fmake_marker ();
28993 staticpro (&message_dolog_marker1);
28994 message_dolog_marker2 = Fmake_marker ();
28995 staticpro (&message_dolog_marker2);
28996 message_dolog_marker3 = Fmake_marker ();
28997 staticpro (&message_dolog_marker3);
28998
28999 #ifdef GLYPH_DEBUG
29000 defsubr (&Sdump_frame_glyph_matrix);
29001 defsubr (&Sdump_glyph_matrix);
29002 defsubr (&Sdump_glyph_row);
29003 defsubr (&Sdump_tool_bar_row);
29004 defsubr (&Strace_redisplay);
29005 defsubr (&Strace_to_stderr);
29006 #endif
29007 #ifdef HAVE_WINDOW_SYSTEM
29008 defsubr (&Stool_bar_lines_needed);
29009 defsubr (&Slookup_image_map);
29010 #endif
29011 defsubr (&Sformat_mode_line);
29012 defsubr (&Sinvisible_p);
29013 defsubr (&Scurrent_bidi_paragraph_direction);
29014
29015 DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
29016 DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
29017 DEFSYM (Qoverriding_local_map, "overriding-local-map");
29018 DEFSYM (Qwindow_scroll_functions, "window-scroll-functions");
29019 DEFSYM (Qwindow_text_change_functions, "window-text-change-functions");
29020 DEFSYM (Qredisplay_end_trigger_functions, "redisplay-end-trigger-functions");
29021 DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
29022 DEFSYM (Qeval, "eval");
29023 DEFSYM (QCdata, ":data");
29024 DEFSYM (Qdisplay, "display");
29025 DEFSYM (Qspace_width, "space-width");
29026 DEFSYM (Qraise, "raise");
29027 DEFSYM (Qslice, "slice");
29028 DEFSYM (Qspace, "space");
29029 DEFSYM (Qmargin, "margin");
29030 DEFSYM (Qpointer, "pointer");
29031 DEFSYM (Qleft_margin, "left-margin");
29032 DEFSYM (Qright_margin, "right-margin");
29033 DEFSYM (Qcenter, "center");
29034 DEFSYM (Qline_height, "line-height");
29035 DEFSYM (QCalign_to, ":align-to");
29036 DEFSYM (QCrelative_width, ":relative-width");
29037 DEFSYM (QCrelative_height, ":relative-height");
29038 DEFSYM (QCeval, ":eval");
29039 DEFSYM (QCpropertize, ":propertize");
29040 DEFSYM (QCfile, ":file");
29041 DEFSYM (Qfontified, "fontified");
29042 DEFSYM (Qfontification_functions, "fontification-functions");
29043 DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
29044 DEFSYM (Qescape_glyph, "escape-glyph");
29045 DEFSYM (Qnobreak_space, "nobreak-space");
29046 DEFSYM (Qimage, "image");
29047 DEFSYM (Qtext, "text");
29048 DEFSYM (Qboth, "both");
29049 DEFSYM (Qboth_horiz, "both-horiz");
29050 DEFSYM (Qtext_image_horiz, "text-image-horiz");
29051 DEFSYM (QCmap, ":map");
29052 DEFSYM (QCpointer, ":pointer");
29053 DEFSYM (Qrect, "rect");
29054 DEFSYM (Qcircle, "circle");
29055 DEFSYM (Qpoly, "poly");
29056 DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
29057 DEFSYM (Qgrow_only, "grow-only");
29058 DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
29059 DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
29060 DEFSYM (Qposition, "position");
29061 DEFSYM (Qbuffer_position, "buffer-position");
29062 DEFSYM (Qobject, "object");
29063 DEFSYM (Qbar, "bar");
29064 DEFSYM (Qhbar, "hbar");
29065 DEFSYM (Qbox, "box");
29066 DEFSYM (Qhollow, "hollow");
29067 DEFSYM (Qhand, "hand");
29068 DEFSYM (Qarrow, "arrow");
29069 DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
29070
29071 list_of_error = Fcons (Fcons (intern_c_string ("error"),
29072 Fcons (intern_c_string ("void-variable"), Qnil)),
29073 Qnil);
29074 staticpro (&list_of_error);
29075
29076 DEFSYM (Qlast_arrow_position, "last-arrow-position");
29077 DEFSYM (Qlast_arrow_string, "last-arrow-string");
29078 DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
29079 DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
29080
29081 echo_buffer[0] = echo_buffer[1] = Qnil;
29082 staticpro (&echo_buffer[0]);
29083 staticpro (&echo_buffer[1]);
29084
29085 echo_area_buffer[0] = echo_area_buffer[1] = Qnil;
29086 staticpro (&echo_area_buffer[0]);
29087 staticpro (&echo_area_buffer[1]);
29088
29089 Vmessages_buffer_name = build_pure_c_string ("*Messages*");
29090 staticpro (&Vmessages_buffer_name);
29091
29092 mode_line_proptrans_alist = Qnil;
29093 staticpro (&mode_line_proptrans_alist);
29094 mode_line_string_list = Qnil;
29095 staticpro (&mode_line_string_list);
29096 mode_line_string_face = Qnil;
29097 staticpro (&mode_line_string_face);
29098 mode_line_string_face_prop = Qnil;
29099 staticpro (&mode_line_string_face_prop);
29100 Vmode_line_unwind_vector = Qnil;
29101 staticpro (&Vmode_line_unwind_vector);
29102
29103 DEFSYM (Qmode_line_default_help_echo, "mode-line-default-help-echo");
29104
29105 help_echo_string = Qnil;
29106 staticpro (&help_echo_string);
29107 help_echo_object = Qnil;
29108 staticpro (&help_echo_object);
29109 help_echo_window = Qnil;
29110 staticpro (&help_echo_window);
29111 previous_help_echo_string = Qnil;
29112 staticpro (&previous_help_echo_string);
29113 help_echo_pos = -1;
29114
29115 DEFSYM (Qright_to_left, "right-to-left");
29116 DEFSYM (Qleft_to_right, "left-to-right");
29117
29118 #ifdef HAVE_WINDOW_SYSTEM
29119 DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
29120 doc: /* Non-nil means draw block cursor as wide as the glyph under it.
29121 For example, if a block cursor is over a tab, it will be drawn as
29122 wide as that tab on the display. */);
29123 x_stretch_cursor_p = 0;
29124 #endif
29125
29126 DEFVAR_LISP ("show-trailing-whitespace", Vshow_trailing_whitespace,
29127 doc: /* Non-nil means highlight trailing whitespace.
29128 The face used for trailing whitespace is `trailing-whitespace'. */);
29129 Vshow_trailing_whitespace = Qnil;
29130
29131 DEFVAR_LISP ("nobreak-char-display", Vnobreak_char_display,
29132 doc: /* Control highlighting of non-ASCII space and hyphen chars.
29133 If the value is t, Emacs highlights non-ASCII chars which have the
29134 same appearance as an ASCII space or hyphen, using the `nobreak-space'
29135 or `escape-glyph' face respectively.
29136
29137 U+00A0 (no-break space), U+00AD (soft hyphen), U+2010 (hyphen), and
29138 U+2011 (non-breaking hyphen) are affected.
29139
29140 Any other non-nil value means to display these characters as a escape
29141 glyph followed by an ordinary space or hyphen.
29142
29143 A value of nil means no special handling of these characters. */);
29144 Vnobreak_char_display = Qt;
29145
29146 DEFVAR_LISP ("void-text-area-pointer", Vvoid_text_area_pointer,
29147 doc: /* The pointer shape to show in void text areas.
29148 A value of nil means to show the text pointer. Other options are `arrow',
29149 `text', `hand', `vdrag', `hdrag', `modeline', and `hourglass'. */);
29150 Vvoid_text_area_pointer = Qarrow;
29151
29152 DEFVAR_LISP ("inhibit-redisplay", Vinhibit_redisplay,
29153 doc: /* Non-nil means don't actually do any redisplay.
29154 This is used for internal purposes. */);
29155 Vinhibit_redisplay = Qnil;
29156
29157 DEFVAR_LISP ("global-mode-string", Vglobal_mode_string,
29158 doc: /* String (or mode line construct) included (normally) in `mode-line-format'. */);
29159 Vglobal_mode_string = Qnil;
29160
29161 DEFVAR_LISP ("overlay-arrow-position", Voverlay_arrow_position,
29162 doc: /* Marker for where to display an arrow on top of the buffer text.
29163 This must be the beginning of a line in order to work.
29164 See also `overlay-arrow-string'. */);
29165 Voverlay_arrow_position = Qnil;
29166
29167 DEFVAR_LISP ("overlay-arrow-string", Voverlay_arrow_string,
29168 doc: /* String to display as an arrow in non-window frames.
29169 See also `overlay-arrow-position'. */);
29170 Voverlay_arrow_string = build_pure_c_string ("=>");
29171
29172 DEFVAR_LISP ("overlay-arrow-variable-list", Voverlay_arrow_variable_list,
29173 doc: /* List of variables (symbols) which hold markers for overlay arrows.
29174 The symbols on this list are examined during redisplay to determine
29175 where to display overlay arrows. */);
29176 Voverlay_arrow_variable_list
29177 = Fcons (intern_c_string ("overlay-arrow-position"), Qnil);
29178
29179 DEFVAR_INT ("scroll-step", emacs_scroll_step,
29180 doc: /* The number of lines to try scrolling a window by when point moves out.
29181 If that fails to bring point back on frame, point is centered instead.
29182 If this is zero, point is always centered after it moves off frame.
29183 If you want scrolling to always be a line at a time, you should set
29184 `scroll-conservatively' to a large value rather than set this to 1. */);
29185
29186 DEFVAR_INT ("scroll-conservatively", scroll_conservatively,
29187 doc: /* Scroll up to this many lines, to bring point back on screen.
29188 If point moves off-screen, redisplay will scroll by up to
29189 `scroll-conservatively' lines in order to bring point just barely
29190 onto the screen again. If that cannot be done, then redisplay
29191 recenters point as usual.
29192
29193 If the value is greater than 100, redisplay will never recenter point,
29194 but will always scroll just enough text to bring point into view, even
29195 if you move far away.
29196
29197 A value of zero means always recenter point if it moves off screen. */);
29198 scroll_conservatively = 0;
29199
29200 DEFVAR_INT ("scroll-margin", scroll_margin,
29201 doc: /* Number of lines of margin at the top and bottom of a window.
29202 Recenter the window whenever point gets within this many lines
29203 of the top or bottom of the window. */);
29204 scroll_margin = 0;
29205
29206 DEFVAR_LISP ("display-pixels-per-inch", Vdisplay_pixels_per_inch,
29207 doc: /* Pixels per inch value for non-window system displays.
29208 Value is a number or a cons (WIDTH-DPI . HEIGHT-DPI). */);
29209 Vdisplay_pixels_per_inch = make_float (72.0);
29210
29211 #ifdef GLYPH_DEBUG
29212 DEFVAR_INT ("debug-end-pos", debug_end_pos, doc: /* Don't ask. */);
29213 #endif
29214
29215 DEFVAR_LISP ("truncate-partial-width-windows",
29216 Vtruncate_partial_width_windows,
29217 doc: /* Non-nil means truncate lines in windows narrower than the frame.
29218 For an integer value, truncate lines in each window narrower than the
29219 full frame width, provided the window width is less than that integer;
29220 otherwise, respect the value of `truncate-lines'.
29221
29222 For any other non-nil value, truncate lines in all windows that do
29223 not span the full frame width.
29224
29225 A value of nil means to respect the value of `truncate-lines'.
29226
29227 If `word-wrap' is enabled, you might want to reduce this. */);
29228 Vtruncate_partial_width_windows = make_number (50);
29229
29230 DEFVAR_LISP ("line-number-display-limit", Vline_number_display_limit,
29231 doc: /* Maximum buffer size for which line number should be displayed.
29232 If the buffer is bigger than this, the line number does not appear
29233 in the mode line. A value of nil means no limit. */);
29234 Vline_number_display_limit = Qnil;
29235
29236 DEFVAR_INT ("line-number-display-limit-width",
29237 line_number_display_limit_width,
29238 doc: /* Maximum line width (in characters) for line number display.
29239 If the average length of the lines near point is bigger than this, then the
29240 line number may be omitted from the mode line. */);
29241 line_number_display_limit_width = 200;
29242
29243 DEFVAR_BOOL ("highlight-nonselected-windows", highlight_nonselected_windows,
29244 doc: /* Non-nil means highlight region even in nonselected windows. */);
29245 highlight_nonselected_windows = 0;
29246
29247 DEFVAR_BOOL ("multiple-frames", multiple_frames,
29248 doc: /* Non-nil if more than one frame is visible on this display.
29249 Minibuffer-only frames don't count, but iconified frames do.
29250 This variable is not guaranteed to be accurate except while processing
29251 `frame-title-format' and `icon-title-format'. */);
29252
29253 DEFVAR_LISP ("frame-title-format", Vframe_title_format,
29254 doc: /* Template for displaying the title bar of visible frames.
29255 \(Assuming the window manager supports this feature.)
29256
29257 This variable has the same structure as `mode-line-format', except that
29258 the %c and %l constructs are ignored. It is used only on frames for
29259 which no explicit name has been set \(see `modify-frame-parameters'). */);
29260
29261 DEFVAR_LISP ("icon-title-format", Vicon_title_format,
29262 doc: /* Template for displaying the title bar of an iconified frame.
29263 \(Assuming the window manager supports this feature.)
29264 This variable has the same structure as `mode-line-format' (which see),
29265 and is used only on frames for which no explicit name has been set
29266 \(see `modify-frame-parameters'). */);
29267 Vicon_title_format
29268 = Vframe_title_format
29269 = listn (CONSTYPE_PURE, 3,
29270 intern_c_string ("multiple-frames"),
29271 build_pure_c_string ("%b"),
29272 listn (CONSTYPE_PURE, 4,
29273 empty_unibyte_string,
29274 intern_c_string ("invocation-name"),
29275 build_pure_c_string ("@"),
29276 intern_c_string ("system-name")));
29277
29278 DEFVAR_LISP ("message-log-max", Vmessage_log_max,
29279 doc: /* Maximum number of lines to keep in the message log buffer.
29280 If nil, disable message logging. If t, log messages but don't truncate
29281 the buffer when it becomes large. */);
29282 Vmessage_log_max = make_number (1000);
29283
29284 DEFVAR_LISP ("window-size-change-functions", Vwindow_size_change_functions,
29285 doc: /* Functions called before redisplay, if window sizes have changed.
29286 The value should be a list of functions that take one argument.
29287 Just before redisplay, for each frame, if any of its windows have changed
29288 size since the last redisplay, or have been split or deleted,
29289 all the functions in the list are called, with the frame as argument. */);
29290 Vwindow_size_change_functions = Qnil;
29291
29292 DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
29293 doc: /* List of functions to call before redisplaying a window with scrolling.
29294 Each function is called with two arguments, the window and its new
29295 display-start position. Note that these functions are also called by
29296 `set-window-buffer'. Also note that the value of `window-end' is not
29297 valid when these functions are called.
29298
29299 Warning: Do not use this feature to alter the way the window
29300 is scrolled. It is not designed for that, and such use probably won't
29301 work. */);
29302 Vwindow_scroll_functions = Qnil;
29303
29304 DEFVAR_LISP ("window-text-change-functions",
29305 Vwindow_text_change_functions,
29306 doc: /* Functions to call in redisplay when text in the window might change. */);
29307 Vwindow_text_change_functions = Qnil;
29308
29309 DEFVAR_LISP ("redisplay-end-trigger-functions", Vredisplay_end_trigger_functions,
29310 doc: /* Functions called when redisplay of a window reaches the end trigger.
29311 Each function is called with two arguments, the window and the end trigger value.
29312 See `set-window-redisplay-end-trigger'. */);
29313 Vredisplay_end_trigger_functions = Qnil;
29314
29315 DEFVAR_LISP ("mouse-autoselect-window", Vmouse_autoselect_window,
29316 doc: /* Non-nil means autoselect window with mouse pointer.
29317 If nil, do not autoselect windows.
29318 A positive number means delay autoselection by that many seconds: a
29319 window is autoselected only after the mouse has remained in that
29320 window for the duration of the delay.
29321 A negative number has a similar effect, but causes windows to be
29322 autoselected only after the mouse has stopped moving. \(Because of
29323 the way Emacs compares mouse events, you will occasionally wait twice
29324 that time before the window gets selected.\)
29325 Any other value means to autoselect window instantaneously when the
29326 mouse pointer enters it.
29327
29328 Autoselection selects the minibuffer only if it is active, and never
29329 unselects the minibuffer if it is active.
29330
29331 When customizing this variable make sure that the actual value of
29332 `focus-follows-mouse' matches the behavior of your window manager. */);
29333 Vmouse_autoselect_window = Qnil;
29334
29335 DEFVAR_LISP ("auto-resize-tool-bars", Vauto_resize_tool_bars,
29336 doc: /* Non-nil means automatically resize tool-bars.
29337 This dynamically changes the tool-bar's height to the minimum height
29338 that is needed to make all tool-bar items visible.
29339 If value is `grow-only', the tool-bar's height is only increased
29340 automatically; to decrease the tool-bar height, use \\[recenter]. */);
29341 Vauto_resize_tool_bars = Qt;
29342
29343 DEFVAR_BOOL ("auto-raise-tool-bar-buttons", auto_raise_tool_bar_buttons_p,
29344 doc: /* Non-nil means raise tool-bar buttons when the mouse moves over them. */);
29345 auto_raise_tool_bar_buttons_p = 1;
29346
29347 DEFVAR_BOOL ("make-cursor-line-fully-visible", make_cursor_line_fully_visible_p,
29348 doc: /* Non-nil means to scroll (recenter) cursor line if it is not fully visible. */);
29349 make_cursor_line_fully_visible_p = 1;
29350
29351 DEFVAR_LISP ("tool-bar-border", Vtool_bar_border,
29352 doc: /* Border below tool-bar in pixels.
29353 If an integer, use it as the height of the border.
29354 If it is one of `internal-border-width' or `border-width', use the
29355 value of the corresponding frame parameter.
29356 Otherwise, no border is added below the tool-bar. */);
29357 Vtool_bar_border = Qinternal_border_width;
29358
29359 DEFVAR_LISP ("tool-bar-button-margin", Vtool_bar_button_margin,
29360 doc: /* Margin around tool-bar buttons in pixels.
29361 If an integer, use that for both horizontal and vertical margins.
29362 Otherwise, value should be a pair of integers `(HORZ . VERT)' with
29363 HORZ specifying the horizontal margin, and VERT specifying the
29364 vertical margin. */);
29365 Vtool_bar_button_margin = make_number (DEFAULT_TOOL_BAR_BUTTON_MARGIN);
29366
29367 DEFVAR_INT ("tool-bar-button-relief", tool_bar_button_relief,
29368 doc: /* Relief thickness of tool-bar buttons. */);
29369 tool_bar_button_relief = DEFAULT_TOOL_BAR_BUTTON_RELIEF;
29370
29371 DEFVAR_LISP ("tool-bar-style", Vtool_bar_style,
29372 doc: /* Tool bar style to use.
29373 It can be one of
29374 image - show images only
29375 text - show text only
29376 both - show both, text below image
29377 both-horiz - show text to the right of the image
29378 text-image-horiz - show text to the left of the image
29379 any other - use system default or image if no system default.
29380
29381 This variable only affects the GTK+ toolkit version of Emacs. */);
29382 Vtool_bar_style = Qnil;
29383
29384 DEFVAR_INT ("tool-bar-max-label-size", tool_bar_max_label_size,
29385 doc: /* Maximum number of characters a label can have to be shown.
29386 The tool bar style must also show labels for this to have any effect, see
29387 `tool-bar-style'. */);
29388 tool_bar_max_label_size = DEFAULT_TOOL_BAR_LABEL_SIZE;
29389
29390 DEFVAR_LISP ("fontification-functions", Vfontification_functions,
29391 doc: /* List of functions to call to fontify regions of text.
29392 Each function is called with one argument POS. Functions must
29393 fontify a region starting at POS in the current buffer, and give
29394 fontified regions the property `fontified'. */);
29395 Vfontification_functions = Qnil;
29396 Fmake_variable_buffer_local (Qfontification_functions);
29397
29398 DEFVAR_BOOL ("unibyte-display-via-language-environment",
29399 unibyte_display_via_language_environment,
29400 doc: /* Non-nil means display unibyte text according to language environment.
29401 Specifically, this means that raw bytes in the range 160-255 decimal
29402 are displayed by converting them to the equivalent multibyte characters
29403 according to the current language environment. As a result, they are
29404 displayed according to the current fontset.
29405
29406 Note that this variable affects only how these bytes are displayed,
29407 but does not change the fact they are interpreted as raw bytes. */);
29408 unibyte_display_via_language_environment = 0;
29409
29410 DEFVAR_LISP ("max-mini-window-height", Vmax_mini_window_height,
29411 doc: /* Maximum height for resizing mini-windows (the minibuffer and the echo area).
29412 If a float, it specifies a fraction of the mini-window frame's height.
29413 If an integer, it specifies a number of lines. */);
29414 Vmax_mini_window_height = make_float (0.25);
29415
29416 DEFVAR_LISP ("resize-mini-windows", Vresize_mini_windows,
29417 doc: /* How to resize mini-windows (the minibuffer and the echo area).
29418 A value of nil means don't automatically resize mini-windows.
29419 A value of t means resize them to fit the text displayed in them.
29420 A value of `grow-only', the default, means let mini-windows grow only;
29421 they return to their normal size when the minibuffer is closed, or the
29422 echo area becomes empty. */);
29423 Vresize_mini_windows = Qgrow_only;
29424
29425 DEFVAR_LISP ("blink-cursor-alist", Vblink_cursor_alist,
29426 doc: /* Alist specifying how to blink the cursor off.
29427 Each element has the form (ON-STATE . OFF-STATE). Whenever the
29428 `cursor-type' frame-parameter or variable equals ON-STATE,
29429 comparing using `equal', Emacs uses OFF-STATE to specify
29430 how to blink it off. ON-STATE and OFF-STATE are values for
29431 the `cursor-type' frame parameter.
29432
29433 If a frame's ON-STATE has no entry in this list,
29434 the frame's other specifications determine how to blink the cursor off. */);
29435 Vblink_cursor_alist = Qnil;
29436
29437 DEFVAR_BOOL ("auto-hscroll-mode", automatic_hscrolling_p,
29438 doc: /* Allow or disallow automatic horizontal scrolling of windows.
29439 If non-nil, windows are automatically scrolled horizontally to make
29440 point visible. */);
29441 automatic_hscrolling_p = 1;
29442 DEFSYM (Qauto_hscroll_mode, "auto-hscroll-mode");
29443
29444 DEFVAR_INT ("hscroll-margin", hscroll_margin,
29445 doc: /* How many columns away from the window edge point is allowed to get
29446 before automatic hscrolling will horizontally scroll the window. */);
29447 hscroll_margin = 5;
29448
29449 DEFVAR_LISP ("hscroll-step", Vhscroll_step,
29450 doc: /* How many columns to scroll the window when point gets too close to the edge.
29451 When point is less than `hscroll-margin' columns from the window
29452 edge, automatic hscrolling will scroll the window by the amount of columns
29453 determined by this variable. If its value is a positive integer, scroll that
29454 many columns. If it's a positive floating-point number, it specifies the
29455 fraction of the window's width to scroll. If it's nil or zero, point will be
29456 centered horizontally after the scroll. Any other value, including negative
29457 numbers, are treated as if the value were zero.
29458
29459 Automatic hscrolling always moves point outside the scroll margin, so if
29460 point was more than scroll step columns inside the margin, the window will
29461 scroll more than the value given by the scroll step.
29462
29463 Note that the lower bound for automatic hscrolling specified by `scroll-left'
29464 and `scroll-right' overrides this variable's effect. */);
29465 Vhscroll_step = make_number (0);
29466
29467 DEFVAR_BOOL ("message-truncate-lines", message_truncate_lines,
29468 doc: /* If non-nil, messages are truncated instead of resizing the echo area.
29469 Bind this around calls to `message' to let it take effect. */);
29470 message_truncate_lines = 0;
29471
29472 DEFVAR_LISP ("menu-bar-update-hook", Vmenu_bar_update_hook,
29473 doc: /* Normal hook run to update the menu bar definitions.
29474 Redisplay runs this hook before it redisplays the menu bar.
29475 This is used to update submenus such as Buffers,
29476 whose contents depend on various data. */);
29477 Vmenu_bar_update_hook = Qnil;
29478
29479 DEFVAR_LISP ("menu-updating-frame", Vmenu_updating_frame,
29480 doc: /* Frame for which we are updating a menu.
29481 The enable predicate for a menu binding should check this variable. */);
29482 Vmenu_updating_frame = Qnil;
29483
29484 DEFVAR_BOOL ("inhibit-menubar-update", inhibit_menubar_update,
29485 doc: /* Non-nil means don't update menu bars. Internal use only. */);
29486 inhibit_menubar_update = 0;
29487
29488 DEFVAR_LISP ("wrap-prefix", Vwrap_prefix,
29489 doc: /* Prefix prepended to all continuation lines at display time.
29490 The value may be a string, an image, or a stretch-glyph; it is
29491 interpreted in the same way as the value of a `display' text property.
29492
29493 This variable is overridden by any `wrap-prefix' text or overlay
29494 property.
29495
29496 To add a prefix to non-continuation lines, use `line-prefix'. */);
29497 Vwrap_prefix = Qnil;
29498 DEFSYM (Qwrap_prefix, "wrap-prefix");
29499 Fmake_variable_buffer_local (Qwrap_prefix);
29500
29501 DEFVAR_LISP ("line-prefix", Vline_prefix,
29502 doc: /* Prefix prepended to all non-continuation lines at display time.
29503 The value may be a string, an image, or a stretch-glyph; it is
29504 interpreted in the same way as the value of a `display' text property.
29505
29506 This variable is overridden by any `line-prefix' text or overlay
29507 property.
29508
29509 To add a prefix to continuation lines, use `wrap-prefix'. */);
29510 Vline_prefix = Qnil;
29511 DEFSYM (Qline_prefix, "line-prefix");
29512 Fmake_variable_buffer_local (Qline_prefix);
29513
29514 DEFVAR_BOOL ("inhibit-eval-during-redisplay", inhibit_eval_during_redisplay,
29515 doc: /* Non-nil means don't eval Lisp during redisplay. */);
29516 inhibit_eval_during_redisplay = 0;
29517
29518 DEFVAR_BOOL ("inhibit-free-realized-faces", inhibit_free_realized_faces,
29519 doc: /* Non-nil means don't free realized faces. Internal use only. */);
29520 inhibit_free_realized_faces = 0;
29521
29522 #ifdef GLYPH_DEBUG
29523 DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
29524 doc: /* Inhibit try_window_id display optimization. */);
29525 inhibit_try_window_id = 0;
29526
29527 DEFVAR_BOOL ("inhibit-try-window-reusing", inhibit_try_window_reusing,
29528 doc: /* Inhibit try_window_reusing display optimization. */);
29529 inhibit_try_window_reusing = 0;
29530
29531 DEFVAR_BOOL ("inhibit-try-cursor-movement", inhibit_try_cursor_movement,
29532 doc: /* Inhibit try_cursor_movement display optimization. */);
29533 inhibit_try_cursor_movement = 0;
29534 #endif /* GLYPH_DEBUG */
29535
29536 DEFVAR_INT ("overline-margin", overline_margin,
29537 doc: /* Space between overline and text, in pixels.
29538 The default value is 2: the height of the overline (1 pixel) plus 1 pixel
29539 margin to the character height. */);
29540 overline_margin = 2;
29541
29542 DEFVAR_INT ("underline-minimum-offset",
29543 underline_minimum_offset,
29544 doc: /* Minimum distance between baseline and underline.
29545 This can improve legibility of underlined text at small font sizes,
29546 particularly when using variable `x-use-underline-position-properties'
29547 with fonts that specify an UNDERLINE_POSITION relatively close to the
29548 baseline. The default value is 1. */);
29549 underline_minimum_offset = 1;
29550
29551 DEFVAR_BOOL ("display-hourglass", display_hourglass_p,
29552 doc: /* Non-nil means show an hourglass pointer, when Emacs is busy.
29553 This feature only works when on a window system that can change
29554 cursor shapes. */);
29555 display_hourglass_p = 1;
29556
29557 DEFVAR_LISP ("hourglass-delay", Vhourglass_delay,
29558 doc: /* Seconds to wait before displaying an hourglass pointer when Emacs is busy. */);
29559 Vhourglass_delay = make_number (DEFAULT_HOURGLASS_DELAY);
29560
29561 hourglass_atimer = NULL;
29562 hourglass_shown_p = 0;
29563
29564 DEFSYM (Qglyphless_char, "glyphless-char");
29565 DEFSYM (Qhex_code, "hex-code");
29566 DEFSYM (Qempty_box, "empty-box");
29567 DEFSYM (Qthin_space, "thin-space");
29568 DEFSYM (Qzero_width, "zero-width");
29569
29570 DEFSYM (Qglyphless_char_display, "glyphless-char-display");
29571 /* Intern this now in case it isn't already done.
29572 Setting this variable twice is harmless.
29573 But don't staticpro it here--that is done in alloc.c. */
29574 Qchar_table_extra_slots = intern_c_string ("char-table-extra-slots");
29575 Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));
29576
29577 DEFVAR_LISP ("glyphless-char-display", Vglyphless_char_display,
29578 doc: /* Char-table defining glyphless characters.
29579 Each element, if non-nil, should be one of the following:
29580 an ASCII acronym string: display this string in a box
29581 `hex-code': display the hexadecimal code of a character in a box
29582 `empty-box': display as an empty box
29583 `thin-space': display as 1-pixel width space
29584 `zero-width': don't display
29585 An element may also be a cons cell (GRAPHICAL . TEXT), which specifies the
29586 display method for graphical terminals and text terminals respectively.
29587 GRAPHICAL and TEXT should each have one of the values listed above.
29588
29589 The char-table has one extra slot to control the display of a character for
29590 which no font is found. This slot only takes effect on graphical terminals.
29591 Its value should be an ASCII acronym string, `hex-code', `empty-box', or
29592 `thin-space'. The default is `empty-box'. */);
29593 Vglyphless_char_display = Fmake_char_table (Qglyphless_char_display, Qnil);
29594 Fset_char_table_extra_slot (Vglyphless_char_display, make_number (0),
29595 Qempty_box);
29596
29597 DEFVAR_LISP ("debug-on-message", Vdebug_on_message,
29598 doc: /* If non-nil, debug if a message matching this regexp is displayed. */);
29599 Vdebug_on_message = Qnil;
29600 }
29601
29602
29603 /* Initialize this module when Emacs starts. */
29604
29605 void
29606 init_xdisp (void)
29607 {
29608 current_header_line_height = current_mode_line_height = -1;
29609
29610 CHARPOS (this_line_start_pos) = 0;
29611
29612 if (!noninteractive)
29613 {
29614 struct window *m = XWINDOW (minibuf_window);
29615 Lisp_Object frame = m->frame;
29616 struct frame *f = XFRAME (frame);
29617 Lisp_Object root = FRAME_ROOT_WINDOW (f);
29618 struct window *r = XWINDOW (root);
29619 int i;
29620
29621 echo_area_window = minibuf_window;
29622
29623 wset_top_line (r, make_number (FRAME_TOP_MARGIN (f)));
29624 wset_total_lines
29625 (r, make_number (FRAME_LINES (f) - 1 - FRAME_TOP_MARGIN (f)));
29626 wset_total_cols (r, make_number (FRAME_COLS (f)));
29627 wset_top_line (m, make_number (FRAME_LINES (f) - 1));
29628 wset_total_lines (m, make_number (1));
29629 wset_total_cols (m, make_number (FRAME_COLS (f)));
29630
29631 scratch_glyph_row.glyphs[TEXT_AREA] = scratch_glyphs;
29632 scratch_glyph_row.glyphs[TEXT_AREA + 1]
29633 = scratch_glyphs + MAX_SCRATCH_GLYPHS;
29634
29635 /* The default ellipsis glyphs `...'. */
29636 for (i = 0; i < 3; ++i)
29637 default_invis_vector[i] = make_number ('.');
29638 }
29639
29640 {
29641 /* Allocate the buffer for frame titles.
29642 Also used for `format-mode-line'. */
29643 int size = 100;
29644 mode_line_noprop_buf = xmalloc (size);
29645 mode_line_noprop_buf_end = mode_line_noprop_buf + size;
29646 mode_line_noprop_ptr = mode_line_noprop_buf;
29647 mode_line_target = MODE_LINE_DISPLAY;
29648 }
29649
29650 help_echo_showing_p = 0;
29651 }
29652
29653 /* Platform-independent portion of hourglass implementation. */
29654
29655 /* Cancel a currently active hourglass timer, and start a new one. */
29656 void
29657 start_hourglass (void)
29658 {
29659 #if defined (HAVE_WINDOW_SYSTEM)
29660 EMACS_TIME delay;
29661
29662 cancel_hourglass ();
29663
29664 if (INTEGERP (Vhourglass_delay)
29665 && XINT (Vhourglass_delay) > 0)
29666 delay = make_emacs_time (min (XINT (Vhourglass_delay),
29667 TYPE_MAXIMUM (time_t)),
29668 0);
29669 else if (FLOATP (Vhourglass_delay)
29670 && XFLOAT_DATA (Vhourglass_delay) > 0)
29671 delay = EMACS_TIME_FROM_DOUBLE (XFLOAT_DATA (Vhourglass_delay));
29672 else
29673 delay = make_emacs_time (DEFAULT_HOURGLASS_DELAY, 0);
29674
29675 #ifdef HAVE_NTGUI
29676 {
29677 extern void w32_note_current_window (void);
29678 w32_note_current_window ();
29679 }
29680 #endif /* HAVE_NTGUI */
29681
29682 hourglass_atimer = start_atimer (ATIMER_RELATIVE, delay,
29683 show_hourglass, NULL);
29684 #endif
29685 }
29686
29687
29688 /* Cancel the hourglass cursor timer if active, hide a busy cursor if
29689 shown. */
29690 void
29691 cancel_hourglass (void)
29692 {
29693 #if defined (HAVE_WINDOW_SYSTEM)
29694 if (hourglass_atimer)
29695 {
29696 cancel_atimer (hourglass_atimer);
29697 hourglass_atimer = NULL;
29698 }
29699
29700 if (hourglass_shown_p)
29701 hide_hourglass ();
29702 #endif
29703 }